blob: 2f7145b8a0445413f0bf9a52a58b2e49b0ccb45a
1 | /* |
2 | * Copyright (C) 2011 The Android Open Source Project |
3 | * |
4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
5 | * you may not use this file except in compliance with the License. |
6 | * You may obtain a copy of the License at |
7 | * |
8 | * http://www.apache.org/licenses/LICENSE-2.0 |
9 | * |
10 | * Unless required by applicable law or agreed to in writing, software |
11 | * distributed under the License is distributed on an "AS IS" BASIS, |
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
13 | * See the License for the specific language governing permissions and |
14 | * limitations under the License. |
15 | */ |
16 | |
17 | |
18 | #define LOG_TAG "CameraHALUtilClasses " |
19 | |
20 | #include "CameraHal.h" |
21 | |
22 | namespace android { |
23 | |
24 | /*--------------------FrameProvider Class STARTS here-----------------------------*/ |
25 | |
26 | int FrameProvider::enableFrameNotification(int32_t frameTypes) |
27 | { |
28 | LOG_FUNCTION_NAME; |
29 | status_t ret = NO_ERROR; |
30 | |
31 | ///Enable the frame notification to CameraAdapter (which implements FrameNotifier interface) |
32 | mFrameNotifier->enableMsgType(frameTypes<<MessageNotifier::FRAME_BIT_FIELD_POSITION |
33 | , mFrameCallback |
34 | , NULL |
35 | , mCookie |
36 | ); |
37 | |
38 | LOG_FUNCTION_NAME_EXIT; |
39 | return ret; |
40 | } |
41 | |
42 | int FrameProvider::disableFrameNotification(int32_t frameTypes) |
43 | { |
44 | LOG_FUNCTION_NAME; |
45 | status_t ret = NO_ERROR; |
46 | |
47 | mFrameNotifier->disableMsgType(frameTypes<<MessageNotifier::FRAME_BIT_FIELD_POSITION |
48 | , mCookie |
49 | ); |
50 | |
51 | LOG_FUNCTION_NAME_EXIT; |
52 | return ret; |
53 | } |
54 | |
55 | int FrameProvider::returnFrame(void *frameBuf, CameraFrame::FrameType frameType) |
56 | { |
57 | status_t ret = NO_ERROR; |
58 | |
59 | mFrameNotifier->returnFrame(frameBuf, frameType); |
60 | |
61 | return ret; |
62 | } |
63 | |
64 | void FrameProvider::addFramePointers(void *frameBuf, void *buf) |
65 | { |
66 | mFrameNotifier->addFramePointers(frameBuf, buf); |
67 | return; |
68 | } |
69 | |
70 | void FrameProvider::removeFramePointers() |
71 | { |
72 | mFrameNotifier->removeFramePointers(); |
73 | return; |
74 | } |
75 | |
76 | /*--------------------FrameProvider Class ENDS here-----------------------------*/ |
77 | |
78 | /*--------------------EventProvider Class STARTS here-----------------------------*/ |
79 | |
80 | int EventProvider::enableEventNotification(int32_t frameTypes) |
81 | { |
82 | LOG_FUNCTION_NAME; |
83 | status_t ret = NO_ERROR; |
84 | |
85 | ///Enable the frame notification to CameraAdapter (which implements FrameNotifier interface) |
86 | mEventNotifier->enableMsgType(frameTypes<<MessageNotifier::EVENT_BIT_FIELD_POSITION |
87 | , NULL |
88 | , mEventCallback |
89 | , mCookie |
90 | ); |
91 | |
92 | LOG_FUNCTION_NAME_EXIT; |
93 | return ret; |
94 | } |
95 | |
96 | int EventProvider::disableEventNotification(int32_t frameTypes) |
97 | { |
98 | LOG_FUNCTION_NAME; |
99 | status_t ret = NO_ERROR; |
100 | |
101 | mEventNotifier->disableMsgType(frameTypes<<MessageNotifier::FRAME_BIT_FIELD_POSITION |
102 | , mCookie |
103 | ); |
104 | |
105 | LOG_FUNCTION_NAME_EXIT; |
106 | return ret; |
107 | } |
108 | |
109 | /*--------------------EventProvider Class ENDS here-----------------------------*/ |
110 | |
111 | /*--------------------CameraArea Class STARTS here-----------------------------*/ |
112 | |
113 | status_t CameraArea::transfrom(size_t width, |
114 | size_t height, |
115 | size_t &top, |
116 | size_t &left, |
117 | size_t &areaWidth, |
118 | size_t &areaHeight) |
119 | { |
120 | status_t ret = NO_ERROR; |
121 | size_t hRange, vRange; |
122 | double hScale, vScale; |
123 | |
124 | LOG_FUNCTION_NAME; |
125 | |
126 | hRange = CameraArea::RIGHT - CameraArea::LEFT; |
127 | vRange = CameraArea::BOTTOM - CameraArea::TOP; |
128 | hScale = ( double ) width / ( double ) hRange; |
129 | vScale = ( double ) height / ( double ) vRange; |
130 | |
131 | top = ( mTop + vRange / 2 ) * vScale; |
132 | left = ( mLeft + hRange / 2 ) * hScale; |
133 | areaHeight = ( mBottom + vRange / 2 ) * vScale; |
134 | areaHeight -= top; |
135 | areaWidth = ( mRight + hRange / 2) * hScale; |
136 | areaWidth -= left; |
137 | |
138 | LOG_FUNCTION_NAME_EXIT; |
139 | |
140 | return ret; |
141 | } |
142 | |
143 | status_t CameraArea::checkArea(ssize_t top, |
144 | ssize_t left, |
145 | ssize_t bottom, |
146 | ssize_t right, |
147 | ssize_t weight) |
148 | { |
149 | |
150 | //Handles the invalid regin corner case. |
151 | if ( ( 0 == top ) && ( 0 == left ) && ( 0 == bottom ) && ( 0 == right ) && ( 0 == weight ) ) { |
152 | return NO_ERROR; |
153 | } |
154 | |
155 | if ( ( CameraArea::WEIGHT_MIN > weight ) || ( CameraArea::WEIGHT_MAX < weight ) ) { |
156 | CAMHAL_LOGEB("Camera area weight is invalid %d", (uint32_t)weight); |
157 | return -EINVAL; |
158 | } |
159 | |
160 | if ( ( CameraArea::TOP > top ) || ( CameraArea::BOTTOM < top ) ) { |
161 | CAMHAL_LOGEB("Camera area top coordinate is invalid %d", (uint32_t)top ); |
162 | return -EINVAL; |
163 | } |
164 | |
165 | if ( ( CameraArea::TOP > bottom ) || ( CameraArea::BOTTOM < bottom ) ) { |
166 | CAMHAL_LOGEB("Camera area bottom coordinate is invalid %d", (uint32_t)bottom ); |
167 | return -EINVAL; |
168 | } |
169 | |
170 | if ( ( CameraArea::LEFT > left ) || ( CameraArea::RIGHT < left ) ) { |
171 | CAMHAL_LOGEB("Camera area left coordinate is invalid %d", (uint32_t)left ); |
172 | return -EINVAL; |
173 | } |
174 | |
175 | if ( ( CameraArea::LEFT > right ) || ( CameraArea::RIGHT < right ) ) { |
176 | CAMHAL_LOGEB("Camera area right coordinate is invalid %d", (uint32_t)right ); |
177 | return -EINVAL; |
178 | } |
179 | |
180 | if ( left >= right ) { |
181 | CAMHAL_LOGEA("Camera area left larger than right"); |
182 | return -EINVAL; |
183 | } |
184 | |
185 | if ( top >= bottom ) { |
186 | CAMHAL_LOGEA("Camera area top larger than bottom"); |
187 | return -EINVAL; |
188 | } |
189 | |
190 | return NO_ERROR; |
191 | } |
192 | |
193 | status_t CameraArea::parseAreas(const char *area, |
194 | size_t areaLength, |
195 | Vector< sp<CameraArea> > &areas) |
196 | { |
197 | status_t ret = NO_ERROR; |
198 | char *ctx; |
199 | char *pArea = NULL; |
200 | char *pStart = NULL; |
201 | char *pEnd = NULL; |
202 | const char *startToken = "("; |
203 | const char endToken = ')'; |
204 | const char sep = ','; |
205 | ssize_t top, left, bottom, right, weight; |
206 | char *tmpBuffer = NULL; |
207 | sp<CameraArea> currentArea; |
208 | |
209 | LOG_FUNCTION_NAME; |
210 | |
211 | if ( ( NULL == area ) || |
212 | ( 0 >= areaLength ) ) |
213 | { |
214 | return -EINVAL; |
215 | } |
216 | |
217 | tmpBuffer = ( char * ) malloc(areaLength); |
218 | if ( NULL == tmpBuffer ) |
219 | { |
220 | return -ENOMEM; |
221 | } |
222 | |
223 | memcpy(tmpBuffer, area, areaLength); |
224 | |
225 | pArea = strtok_r(tmpBuffer, startToken, &ctx); |
226 | |
227 | do |
228 | { |
229 | |
230 | pStart = pArea; |
231 | if ( NULL == pStart ) |
232 | { |
233 | CAMHAL_LOGEA("Parsing of the left area coordinate failed!"); |
234 | ret = -EINVAL; |
235 | break; |
236 | } |
237 | else |
238 | { |
239 | left = static_cast<ssize_t>(strtol(pStart, &pEnd, 10)); |
240 | } |
241 | |
242 | if ( sep != *pEnd ) |
243 | { |
244 | CAMHAL_LOGEA("Parsing of the top area coordinate failed!"); |
245 | ret = -EINVAL; |
246 | break; |
247 | } |
248 | else |
249 | { |
250 | top = static_cast<ssize_t>(strtol(pEnd+1, &pEnd, 10)); |
251 | } |
252 | |
253 | if ( sep != *pEnd ) |
254 | { |
255 | CAMHAL_LOGEA("Parsing of the right area coordinate failed!"); |
256 | ret = -EINVAL; |
257 | break; |
258 | } |
259 | else |
260 | { |
261 | right = static_cast<ssize_t>(strtol(pEnd+1, &pEnd, 10)); |
262 | } |
263 | |
264 | if ( sep != *pEnd ) |
265 | { |
266 | CAMHAL_LOGEA("Parsing of the bottom area coordinate failed!"); |
267 | ret = -EINVAL; |
268 | break; |
269 | } |
270 | else |
271 | { |
272 | bottom = static_cast<ssize_t>(strtol(pEnd+1, &pEnd, 10)); |
273 | } |
274 | |
275 | if ( sep != *pEnd ) |
276 | { |
277 | CAMHAL_LOGEA("Parsing of the weight area coordinate failed!"); |
278 | ret = -EINVAL; |
279 | break; |
280 | } |
281 | else |
282 | { |
283 | weight = static_cast<ssize_t>(strtol(pEnd+1, &pEnd, 10)); |
284 | } |
285 | |
286 | if ( endToken != *pEnd ) |
287 | { |
288 | CAMHAL_LOGEA("Malformed area!"); |
289 | ret = -EINVAL; |
290 | break; |
291 | } |
292 | |
293 | ret = checkArea(top, left, bottom, right, weight); |
294 | if ( NO_ERROR != ret ) { |
295 | break; |
296 | } |
297 | |
298 | currentArea = new CameraArea(top, left, bottom, right, weight); |
299 | CAMHAL_LOGDB("Area parsed [%dx%d, %dx%d] %d", |
300 | ( int ) top, |
301 | ( int ) left, |
302 | ( int ) bottom, |
303 | ( int ) right, |
304 | ( int ) weight); |
305 | if ( NULL != currentArea.get() ) |
306 | { |
307 | areas.add(currentArea); |
308 | } |
309 | else |
310 | { |
311 | ret = -ENOMEM; |
312 | break; |
313 | } |
314 | |
315 | pArea = strtok_r(NULL, startToken, &ctx); |
316 | |
317 | } |
318 | while ( NULL != pArea ); |
319 | |
320 | if ( NULL != tmpBuffer ) |
321 | { |
322 | free(tmpBuffer); |
323 | } |
324 | |
325 | LOG_FUNCTION_NAME_EXIT; |
326 | |
327 | return ret; |
328 | } |
329 | |
330 | bool CameraArea::areAreasDifferent(Vector< sp<CameraArea> > &area1, |
331 | Vector< sp<CameraArea> > &area2) { |
332 | if (area1.size() != area2.size()) { |
333 | return true; |
334 | } |
335 | |
336 | // not going to care about sorting order for now |
337 | for (uint32_t i = 0; i < area1.size(); i++) { |
338 | if (!area1.itemAt(i)->compare(area2.itemAt(i))) { |
339 | return true; |
340 | } |
341 | } |
342 | |
343 | return false; |
344 | } |
345 | |
346 | bool CameraArea::compare(const sp<CameraArea> &area) { |
347 | return ((mTop == area->mTop) && (mLeft == area->mLeft) && |
348 | (mBottom == area->mBottom) && (mRight == area->mRight) && |
349 | (mWeight == area->mWeight)); |
350 | } |
351 | |
352 | |
353 | /*--------------------CameraArea Class ENDS here-----------------------------*/ |
354 | |
355 | }; |
356 |