Commit | Line | Data |
---|---|---|
ac89a8f0 | 1 | /* |
2 | * Copyright (C) 2017, The LineageOS 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 | #define LOG_NDEBUG 0 | |
18 | #define LOG_PARAMETERS | |
19 | ||
20 | #define LOG_TAG "Camera2Wrapper" | |
21 | #include <cutils/log.h> | |
22 | ||
fd1f0757 | 23 | #include <unistd.h> |
443860a7 | 24 | #include <stdatomic.h> |
fd1f0757 | 25 | |
ac89a8f0 | 26 | #include "CameraWrapper.h" |
27 | #include "Camera2Wrapper.h" | |
443860a7 DW |
28 | #include "CallbackWorkerThread.h" |
29 | ||
30 | CallbackWorkerThread cbThread; | |
ac89a8f0 | 31 | |
fd1f0757 DW |
32 | #include <sys/time.h> |
33 | ||
34 | /* current_timestamp() function from stack overflow: | |
35 | * https://stackoverflow.com/questions/3756323/how-to-get-the-current-time-in-milliseconds-from-c-in-linux/17083824 | |
36 | */ | |
37 | ||
38 | long long current_timestamp() { | |
39 | struct timeval te; | |
40 | gettimeofday(&te, NULL); // get current time | |
41 | long long milliseconds = te.tv_sec*1000LL + te.tv_usec/1000; // calculate milliseconds | |
42 | // printf("milliseconds: %lld\n", milliseconds); | |
43 | return milliseconds; | |
44 | } | |
45 | ||
ac89a8f0 | 46 | typedef struct wrapper_camera2_device { |
47 | camera_device_t base; | |
48 | int id; | |
49 | camera_device_t *vendor; | |
50 | } wrapper_camera2_device_t; | |
51 | ||
52 | #define VENDOR_CALL(device, func, ...) ({ \ | |
53 | wrapper_camera2_device_t *__wrapper_dev = (wrapper_camera2_device_t*) device; \ | |
54 | __wrapper_dev->vendor->ops->func(__wrapper_dev->vendor, ##__VA_ARGS__); \ | |
55 | }) | |
56 | ||
57 | #define CAMERA_ID(device) (((wrapper_camera2_device_t *)(device))->id) | |
58 | ||
59 | static camera_module_t *gVendorModule = 0; | |
b274a0fa BL |
60 | static preview_stream_ops *gPreviewWindow = 0; |
61 | static bool gPreviewStartDeferred = false; | |
ac89a8f0 | 62 | |
63 | static int check_vendor_module() | |
64 | { | |
65 | int rv = 0; | |
66 | ALOGV("%s", __FUNCTION__); | |
67 | ||
68 | if(gVendorModule) | |
69 | return 0; | |
70 | ||
71 | rv = hw_get_module_by_class("camera", "vendor", (const hw_module_t **)&gVendorModule); | |
72 | if (rv) | |
73 | ALOGE("failed to open vendor camera module"); | |
74 | return rv; | |
75 | } | |
76 | ||
77 | /******************************************************************* | |
78 | * Camera2 wrapper fixup functions | |
79 | *******************************************************************/ | |
80 | ||
47e12ed8 | 81 | static char * camera2_fixup_getparams(int id __unused, const char * settings) { |
ac89a8f0 | 82 | android::CameraParameters params; |
83 | params.unflatten(android::String8(settings)); | |
84 | ||
85 | #ifdef LOG_PARAMETERS | |
86 | ALOGV("%s: Original parameters:", __FUNCTION__); | |
87 | params.dump(); | |
88 | #endif | |
89 | ||
90 | #ifdef LOG_PARAMETERS | |
91 | ALOGV("%s: Fixed parameters:", __FUNCTION__); | |
92 | params.dump(); | |
93 | #endif | |
94 | ||
95 | android::String8 strParams = params.flatten(); | |
96 | char *ret = strdup(strParams.string()); | |
97 | ||
98 | return ret; | |
99 | } | |
100 | ||
47e12ed8 | 101 | static char * camera2_fixup_setparams(int id __unused, const char * settings) { |
ac89a8f0 | 102 | android::CameraParameters params; |
103 | params.unflatten(android::String8(settings)); | |
104 | ||
105 | #ifdef LOG_PARAMETERS | |
106 | ALOGV("%s: Original parameters:", __FUNCTION__); | |
107 | params.dump(); | |
108 | #endif | |
109 | ||
110 | #ifdef LOG_PARAMETERS | |
111 | ALOGV("%s: Fixed parameters:", __FUNCTION__); | |
112 | params.dump(); | |
113 | #endif | |
114 | ||
115 | android::String8 strParams = params.flatten(); | |
116 | char *ret = strdup(strParams.string()); | |
117 | ||
118 | return ret; | |
119 | } | |
120 | ||
121 | /******************************************************************* | |
122 | * implementation of camera_device_ops functions | |
123 | *******************************************************************/ | |
124 | ||
125 | static int camera2_set_preview_window(struct camera_device * device, | |
126 | struct preview_stream_ops *window) | |
127 | { | |
b274a0fa | 128 | int rc = 0; |
ac89a8f0 | 129 | ALOGV("%s->%08X->%08X", __FUNCTION__, (uintptr_t)device, (uintptr_t)(((wrapper_camera2_device_t*)device)->vendor)); |
130 | ||
b274a0fa | 131 | if(!device) |
ac89a8f0 | 132 | return -EINVAL; |
133 | ||
b274a0fa BL |
134 | gPreviewWindow = window; |
135 | ||
136 | if (gPreviewWindow != 0) { | |
137 | rc = VENDOR_CALL(device, set_preview_window, window); | |
138 | ||
139 | if (gPreviewStartDeferred) { | |
140 | ALOGV("%s call deferred start_preview", __FUNCTION__); | |
141 | gPreviewStartDeferred = false; | |
142 | VENDOR_CALL(device, start_preview); | |
143 | } | |
144 | } | |
145 | ||
146 | return rc; | |
ac89a8f0 | 147 | } |
148 | ||
443860a7 | 149 | atomic_int BlockCbs; |
84ae447d DW |
150 | |
151 | void WrappedNotifyCb (int32_t msg_type, int32_t ext1, int32_t ext2, void *user) { | |
152 | ALOGV("%s->In", __FUNCTION__); | |
153 | ||
443860a7 DW |
154 | /* Print a log message and return if we currently blocking adding callbacks */ |
155 | if(BlockCbs == 1) { | |
156 | ALOGV("%s->BlockCbs == 1", __FUNCTION__); | |
157 | return; | |
84ae447d | 158 | } |
443860a7 DW |
159 | |
160 | /* Create message to send to the callback worker */ | |
161 | WorkerMessage* newWorkerMessage = new WorkerMessage(); | |
162 | newWorkerMessage->CbType = CB_TYPE_NOTIFY; | |
163 | ||
164 | /* Copy the callback data to our worker message */ | |
165 | newWorkerMessage->msg_type = msg_type; | |
166 | newWorkerMessage->ext1 = ext1; | |
167 | newWorkerMessage->ext2 = ext2; | |
168 | newWorkerMessage->user = user; | |
169 | ||
170 | /* Post the message to the callback worker */ | |
171 | cbThread.AddCallback(newWorkerMessage); | |
172 | ||
173 | /* 5mS delay to slow down the camera hal thread */ | |
174 | usleep(5000); | |
175 | ALOGV("%s->Out", __FUNCTION__); | |
176 | } | |
177 | ||
178 | void WrappedDataCb (int32_t msg_type, const camera_memory_t *data, unsigned int index, | |
179 | camera_frame_metadata_t *metadata, void *user) { | |
180 | ALOGV("%s->In, %i, %u", __FUNCTION__, msg_type, index); | |
181 | ||
182 | /* Print a log message and return if we currently blocking adding callbacks */ | |
183 | if(BlockCbs == 1) { | |
184 | ALOGV("%s->BlockCbs == 1", __FUNCTION__); | |
185 | return; | |
186 | } | |
187 | ||
188 | /* Create message to send to the callback worker */ | |
189 | WorkerMessage* newWorkerMessage = new WorkerMessage(); | |
190 | newWorkerMessage->CbType = CB_TYPE_DATA; | |
191 | ||
192 | /* Copy the callback data to our worker message */ | |
193 | newWorkerMessage->msg_type = msg_type; | |
194 | newWorkerMessage->data = data; | |
195 | newWorkerMessage->index= index; | |
196 | newWorkerMessage->metadata = metadata; | |
197 | newWorkerMessage->user = user; | |
198 | ||
199 | /* Post the message to the callback worker */ | |
200 | cbThread.AddCallback(newWorkerMessage); | |
201 | ||
202 | /* 20mS delay to slow down the camera hal thread */ | |
203 | usleep(20000); | |
204 | ALOGV("%s->Out", __FUNCTION__); | |
84ae447d DW |
205 | } |
206 | ||
ac89a8f0 | 207 | static void camera2_set_callbacks(struct camera_device * device, |
208 | camera_notify_callback notify_cb, | |
209 | camera_data_callback data_cb, | |
210 | camera_data_timestamp_callback data_cb_timestamp, | |
211 | camera_request_memory get_memory, | |
212 | void *user) | |
213 | { | |
214 | ALOGV("%s->%08X->%08X", __FUNCTION__, (uintptr_t)device, (uintptr_t)(((wrapper_camera2_device_t*)device)->vendor)); | |
215 | ||
216 | if(!device) | |
217 | return; | |
218 | ||
443860a7 DW |
219 | /* Create and populate a new callback data structure */ |
220 | CallbackData* newCallbackData = new CallbackData(); | |
221 | newCallbackData->NewUserNotifyCb = notify_cb; | |
222 | newCallbackData->NewUserDataCb = data_cb; | |
223 | ||
224 | /* Send it to our worker thread */ | |
225 | cbThread.SetCallbacks(newCallbackData); | |
84ae447d DW |
226 | |
227 | /* Call the set_callbacks function substituting the notify callback with our wrapper */ | |
443860a7 | 228 | VENDOR_CALL(device, set_callbacks, WrappedNotifyCb, WrappedDataCb, data_cb_timestamp, get_memory, user); |
ac89a8f0 | 229 | } |
230 | ||
231 | static void camera2_enable_msg_type(struct camera_device * device, int32_t msg_type) | |
232 | { | |
233 | ALOGV("%s->%08X->%08X", __FUNCTION__, (uintptr_t)device, (uintptr_t)(((wrapper_camera2_device_t*)device)->vendor)); | |
234 | ||
235 | if(!device) | |
236 | return; | |
237 | ||
238 | VENDOR_CALL(device, enable_msg_type, msg_type); | |
239 | } | |
240 | ||
241 | static void camera2_disable_msg_type(struct camera_device * device, int32_t msg_type) | |
242 | { | |
243 | ALOGV("%s->%08X->%08X", __FUNCTION__, (uintptr_t)device, (uintptr_t)(((wrapper_camera2_device_t*)device)->vendor)); | |
244 | ||
245 | if(!device) | |
246 | return; | |
247 | ||
248 | VENDOR_CALL(device, disable_msg_type, msg_type); | |
249 | } | |
250 | ||
251 | static int camera2_msg_type_enabled(struct camera_device * device, int32_t msg_type) | |
252 | { | |
253 | ALOGV("%s->%08X->%08X", __FUNCTION__, (uintptr_t)device, (uintptr_t)(((wrapper_camera2_device_t*)device)->vendor)); | |
254 | ||
255 | if(!device) | |
256 | return 0; | |
257 | ||
258 | return VENDOR_CALL(device, msg_type_enabled, msg_type); | |
259 | } | |
260 | ||
261 | static int camera2_start_preview(struct camera_device * device) | |
262 | { | |
263 | int rc = 0; | |
264 | ALOGV("%s->%08X->%08X", __FUNCTION__, (uintptr_t)device, (uintptr_t)(((wrapper_camera2_device_t*)device)->vendor)); | |
265 | ||
266 | if(!device) | |
267 | return -EINVAL; | |
268 | ||
b274a0fa BL |
269 | if (gPreviewWindow != 0) { |
270 | rc = VENDOR_CALL(device, start_preview); | |
271 | } else { | |
272 | ALOGV("%s invalid preview window, defer start_preview", __FUNCTION__); | |
273 | gPreviewStartDeferred = true; | |
274 | } | |
275 | ||
ac89a8f0 | 276 | return rc; |
277 | } | |
278 | ||
279 | static void camera2_stop_preview(struct camera_device * device) | |
280 | { | |
281 | ALOGV("%s->%08X->%08X", __FUNCTION__, (uintptr_t)device, (uintptr_t)(((wrapper_camera2_device_t*)device)->vendor)); | |
282 | ||
283 | if(!device) | |
284 | return; | |
285 | ||
443860a7 DW |
286 | /* Block queueing more callbacks */ |
287 | BlockCbs = 1; | |
288 | ||
289 | /* Clear the callback queue */ | |
290 | cbThread.ClearCallbacks(); | |
291 | /* Execute stop_preview */ | |
ac89a8f0 | 292 | VENDOR_CALL(device, stop_preview); |
443860a7 DW |
293 | |
294 | /* Unblock queueing more callbacks */ | |
295 | BlockCbs = 0; | |
ac89a8f0 | 296 | } |
297 | ||
298 | static int camera2_preview_enabled(struct camera_device * device) | |
299 | { | |
300 | ALOGV("%s->%08X->%08X", __FUNCTION__, (uintptr_t)device, (uintptr_t)(((wrapper_camera2_device_t*)device)->vendor)); | |
301 | ||
302 | if(!device) | |
303 | return -EINVAL; | |
304 | ||
b274a0fa BL |
305 | if (gPreviewStartDeferred) { |
306 | ALOGV("%s deferred start_preview, return 1", __FUNCTION__); | |
307 | return 1; | |
308 | } else { | |
309 | return VENDOR_CALL(device, preview_enabled); | |
310 | } | |
ac89a8f0 | 311 | } |
312 | ||
313 | static int camera2_store_meta_data_in_buffers(struct camera_device * device, int enable) | |
314 | { | |
315 | ALOGV("%s->%08X->%08X", __FUNCTION__, (uintptr_t)device, (uintptr_t)(((wrapper_camera2_device_t*)device)->vendor)); | |
316 | ||
317 | if(!device) | |
318 | return -EINVAL; | |
319 | ||
320 | return VENDOR_CALL(device, store_meta_data_in_buffers, enable); | |
321 | } | |
322 | ||
323 | static int camera2_start_recording(struct camera_device * device) | |
324 | { | |
325 | ALOGV("%s->%08X->%08X", __FUNCTION__, (uintptr_t)device, (uintptr_t)(((wrapper_camera2_device_t*)device)->vendor)); | |
326 | ||
327 | if(!device) | |
328 | return EINVAL; | |
329 | ||
330 | return VENDOR_CALL(device, start_recording); | |
331 | } | |
332 | ||
333 | static void camera2_stop_recording(struct camera_device * device) | |
334 | { | |
335 | ALOGV("%s->%08X->%08X", __FUNCTION__, (uintptr_t)device, (uintptr_t)(((wrapper_camera2_device_t*)device)->vendor)); | |
336 | ||
337 | if(!device) | |
338 | return; | |
339 | ||
340 | ||
341 | VENDOR_CALL(device, stop_recording); | |
342 | } | |
343 | ||
344 | static int camera2_recording_enabled(struct camera_device * device) | |
345 | { | |
346 | ALOGV("%s->%08X->%08X", __FUNCTION__, (uintptr_t)device, (uintptr_t)(((wrapper_camera2_device_t*)device)->vendor)); | |
347 | ||
348 | if(!device) | |
349 | return -EINVAL; | |
350 | ||
351 | return VENDOR_CALL(device, recording_enabled); | |
352 | } | |
353 | ||
354 | static void camera2_release_recording_frame(struct camera_device * device, | |
355 | const void *opaque) | |
356 | { | |
357 | ALOGV("%s->%08X->%08X", __FUNCTION__, (uintptr_t)device, (uintptr_t)(((wrapper_camera2_device_t*)device)->vendor)); | |
358 | ||
359 | if(!device) | |
360 | return; | |
361 | ||
362 | VENDOR_CALL(device, release_recording_frame, opaque); | |
363 | } | |
364 | ||
fd1f0757 DW |
365 | long long CancelAFTimeGuard = 0; |
366 | ||
ac89a8f0 | 367 | static int camera2_auto_focus(struct camera_device * device) |
368 | { | |
fd1f0757 | 369 | int Ret; |
ac89a8f0 | 370 | ALOGV("%s->%08X->%08X", __FUNCTION__, (uintptr_t)device, (uintptr_t)(((wrapper_camera2_device_t*)device)->vendor)); |
371 | ||
372 | if(!device) | |
373 | return -EINVAL; | |
374 | ||
443860a7 DW |
375 | /* Clear the callback queue */ |
376 | cbThread.ClearCallbacks(); | |
377 | ||
fd1f0757 DW |
378 | /* Call the auto_focus function */ |
379 | Ret = VENDOR_CALL(device, auto_focus); | |
380 | ||
84ae447d DW |
381 | /* Set the cancel_auto_focus time guard to now plus 500mS */ |
382 | CancelAFTimeGuard = current_timestamp() + 500; | |
ac89a8f0 | 383 | |
fd1f0757 | 384 | return Ret; |
ac89a8f0 | 385 | } |
386 | ||
387 | static int camera2_cancel_auto_focus(struct camera_device * device) | |
388 | { | |
fd1f0757 | 389 | int Ret; |
ac89a8f0 | 390 | ALOGV("%s->%08X->%08X", __FUNCTION__, (uintptr_t)device, (uintptr_t)(((wrapper_camera2_device_t*)device)->vendor)); |
391 | ||
392 | if(!device) | |
393 | return -EINVAL; | |
394 | ||
443860a7 DW |
395 | /* Block queueing more callbacks */ |
396 | BlockCbs = 1; | |
397 | ||
398 | /* Clear the callback queue */ | |
399 | cbThread.ClearCallbacks(); | |
400 | ||
fd1f0757 DW |
401 | /* Calculate the difference between our guard time and now */ |
402 | long long TimeDiff = CancelAFTimeGuard - current_timestamp(); | |
403 | /* Post a log message and return success (skipping the call) if the diff is greater than 0 */ | |
404 | if(TimeDiff > 0) { | |
405 | ALOGV("%s: CancelAFTimeGuard for %lli mS\n", __FUNCTION__, TimeDiff * 1000); | |
406 | return 0; | |
407 | } | |
408 | ||
409 | /* No active time guard so call the vendor function */ | |
410 | Ret = VENDOR_CALL(device, cancel_auto_focus); | |
411 | ||
443860a7 DW |
412 | /* Unblock queueing more callbacks */ |
413 | BlockCbs = 0; | |
84ae447d | 414 | |
fd1f0757 | 415 | return Ret; |
ac89a8f0 | 416 | } |
417 | ||
418 | static int camera2_take_picture(struct camera_device * device) | |
419 | { | |
420 | ALOGV("%s->%08X->%08X", __FUNCTION__, (uintptr_t)device, (uintptr_t)(((wrapper_camera2_device_t*)device)->vendor)); | |
421 | ||
422 | if(!device) | |
423 | return -EINVAL; | |
424 | ||
425 | return VENDOR_CALL(device, take_picture); | |
426 | } | |
427 | ||
428 | static int camera2_cancel_picture(struct camera_device * device) | |
429 | { | |
430 | ALOGV("%s->%08X->%08X", __FUNCTION__, (uintptr_t)device, (uintptr_t)(((wrapper_camera2_device_t*)device)->vendor)); | |
431 | ||
432 | if(!device) | |
433 | return -EINVAL; | |
434 | ||
435 | return VENDOR_CALL(device, cancel_picture); | |
436 | } | |
437 | ||
438 | static int camera2_set_parameters(struct camera_device * device, const char *params) | |
439 | { | |
440 | ALOGV("%s->%08X->%08X", __FUNCTION__, (uintptr_t)device, (uintptr_t)(((wrapper_camera2_device_t*)device)->vendor)); | |
441 | ||
442 | if(!device) | |
443 | return -EINVAL; | |
444 | ||
445 | char *tmp = NULL; | |
446 | tmp = camera2_fixup_setparams(CAMERA_ID(device), params); | |
447 | ||
448 | int ret = VENDOR_CALL(device, set_parameters, tmp); | |
449 | ||
450 | return ret; | |
451 | } | |
452 | ||
453 | static char* camera2_get_parameters(struct camera_device * device) | |
454 | { | |
455 | ALOGV("%s->%08X->%08X", __FUNCTION__, (uintptr_t)device, (uintptr_t)(((wrapper_camera2_device_t*)device)->vendor)); | |
456 | ||
457 | if(!device) | |
458 | return NULL; | |
459 | ||
460 | char* params = VENDOR_CALL(device, get_parameters); | |
461 | ||
462 | char * tmp = camera2_fixup_getparams(CAMERA_ID(device), params); | |
463 | VENDOR_CALL(device, put_parameters, params); | |
464 | params = tmp; | |
465 | ||
466 | return params; | |
467 | } | |
468 | ||
469 | static void camera2_put_parameters(struct camera_device *device, char *params) | |
470 | { | |
471 | ALOGV("%s->%08X->%08X", __FUNCTION__, (uintptr_t)device, (uintptr_t)(((wrapper_camera2_device_t*)device)->vendor)); | |
472 | ||
473 | if(params) | |
474 | free(params); | |
475 | } | |
476 | ||
477 | static int camera2_send_command(struct camera_device * device, | |
478 | int32_t cmd, int32_t arg1, int32_t arg2) | |
479 | { | |
480 | ALOGV("%s->%08X->%08X", __FUNCTION__, (uintptr_t)device, (uintptr_t)(((wrapper_camera2_device_t*)device)->vendor)); | |
481 | ||
482 | if(!device) | |
483 | return -EINVAL; | |
484 | ||
485 | return VENDOR_CALL(device, send_command, cmd, arg1, arg2); | |
486 | } | |
487 | ||
488 | static void camera2_release(struct camera_device * device) | |
489 | { | |
490 | ALOGV("%s->%08X->%08X", __FUNCTION__, (uintptr_t)device, (uintptr_t)(((wrapper_camera2_device_t*)device)->vendor)); | |
491 | ||
492 | if(!device) | |
493 | return; | |
494 | ||
495 | VENDOR_CALL(device, release); | |
496 | } | |
497 | ||
498 | static int camera2_dump(struct camera_device * device, int fd) | |
499 | { | |
500 | ALOGV("%s->%08X->%08X", __FUNCTION__, (uintptr_t)device, (uintptr_t)(((wrapper_camera2_device_t*)device)->vendor)); | |
501 | ||
502 | if(!device) | |
503 | return -EINVAL; | |
504 | ||
505 | return VENDOR_CALL(device, dump, fd); | |
506 | } | |
507 | ||
508 | static int camera2_device_close(hw_device_t* device) | |
509 | { | |
510 | int ret = 0; | |
511 | wrapper_camera2_device_t *wrapper_dev = NULL; | |
512 | ||
513 | ALOGV("%s", __FUNCTION__); | |
514 | ||
515 | android::Mutex::Autolock lock(gCameraWrapperLock); | |
516 | ||
517 | if (!device) { | |
518 | ret = -EINVAL; | |
519 | goto done; | |
520 | } | |
521 | ||
522 | wrapper_dev = (wrapper_camera2_device_t*) device; | |
523 | ||
524 | wrapper_dev->vendor->common.close((hw_device_t*)wrapper_dev->vendor); | |
525 | if (wrapper_dev->base.ops) | |
526 | free(wrapper_dev->base.ops); | |
527 | free(wrapper_dev); | |
528 | done: | |
b274a0fa BL |
529 | gPreviewWindow = 0; |
530 | gPreviewStartDeferred = false; | |
443860a7 DW |
531 | |
532 | /* Exit our callback dispatch thread */ | |
533 | cbThread.ExitThread(); | |
534 | ||
ac89a8f0 | 535 | return ret; |
536 | } | |
537 | ||
538 | /******************************************************************* | |
539 | * implementation of camera_module functions | |
540 | *******************************************************************/ | |
541 | ||
542 | /* open device handle to one of the cameras | |
543 | * | |
544 | * assume camera service will keep singleton of each camera | |
545 | * so this function will always only be called once per camera instance | |
546 | */ | |
547 | ||
548 | int camera2_device_open(const hw_module_t* module, const char* name, | |
549 | hw_device_t** device) | |
550 | { | |
551 | int rv = 0; | |
552 | int num_cameras = 0; | |
553 | int cameraid; | |
554 | wrapper_camera2_device_t* camera2_device = NULL; | |
555 | camera_device_ops_t* camera2_ops = NULL; | |
556 | ||
557 | android::Mutex::Autolock lock(gCameraWrapperLock); | |
558 | ||
443860a7 DW |
559 | /* Create our callback dispatch thread */ |
560 | cbThread.CreateThread(); | |
561 | BlockCbs = 0; | |
562 | ||
ac89a8f0 | 563 | ALOGV("%s", __FUNCTION__); |
564 | ||
565 | if (name != NULL) { | |
566 | if (check_vendor_module()) | |
567 | return -EINVAL; | |
568 | ||
569 | cameraid = atoi(name); | |
570 | num_cameras = gVendorModule->get_number_of_cameras(); | |
571 | ||
572 | if (cameraid > num_cameras) { | |
573 | ALOGE("camera service provided cameraid out of bounds, " | |
574 | "cameraid = %d, num supported = %d", | |
575 | cameraid, num_cameras); | |
576 | rv = -EINVAL; | |
577 | goto fail; | |
578 | } | |
579 | ||
580 | camera2_device = (wrapper_camera2_device_t*)malloc(sizeof(*camera2_device)); | |
581 | if (!camera2_device) { | |
582 | ALOGE("camera2_device allocation fail"); | |
583 | rv = -ENOMEM; | |
584 | goto fail; | |
585 | } | |
586 | memset(camera2_device, 0, sizeof(*camera2_device)); | |
587 | camera2_device->id = cameraid; | |
588 | ||
589 | rv = gVendorModule->common.methods->open((const hw_module_t*)gVendorModule, name,(hw_device_t**)&(camera2_device->vendor)); | |
590 | if (rv) | |
591 | { | |
592 | ALOGE("vendor camera open fail"); | |
593 | goto fail; | |
594 | } | |
595 | ALOGV("%s: got vendor camera device 0x%08X", __FUNCTION__, (uintptr_t)(camera2_device->vendor)); | |
596 | ||
597 | camera2_ops = (camera_device_ops_t*)malloc(sizeof(*camera2_ops)); | |
598 | if (!camera2_ops) { | |
599 | ALOGE("camera_ops allocation fail"); | |
600 | rv = -ENOMEM; | |
601 | goto fail; | |
602 | } | |
603 | ||
604 | memset(camera2_ops, 0, sizeof(*camera2_ops)); | |
605 | ||
606 | camera2_device->base.common.tag = HARDWARE_DEVICE_TAG; | |
607 | camera2_device->base.common.version = CAMERA_DEVICE_API_VERSION_1_0; | |
608 | camera2_device->base.common.module = (hw_module_t *)(module); | |
609 | camera2_device->base.common.close = camera2_device_close; | |
610 | camera2_device->base.ops = camera2_ops; | |
611 | ||
612 | camera2_ops->set_preview_window = camera2_set_preview_window; | |
613 | camera2_ops->set_callbacks = camera2_set_callbacks; | |
614 | camera2_ops->enable_msg_type = camera2_enable_msg_type; | |
615 | camera2_ops->disable_msg_type = camera2_disable_msg_type; | |
616 | camera2_ops->msg_type_enabled = camera2_msg_type_enabled; | |
617 | camera2_ops->start_preview = camera2_start_preview; | |
618 | camera2_ops->stop_preview = camera2_stop_preview; | |
619 | camera2_ops->preview_enabled = camera2_preview_enabled; | |
620 | camera2_ops->store_meta_data_in_buffers = camera2_store_meta_data_in_buffers; | |
621 | camera2_ops->start_recording = camera2_start_recording; | |
622 | camera2_ops->stop_recording = camera2_stop_recording; | |
623 | camera2_ops->recording_enabled = camera2_recording_enabled; | |
624 | camera2_ops->release_recording_frame = camera2_release_recording_frame; | |
625 | camera2_ops->auto_focus = camera2_auto_focus; | |
626 | camera2_ops->cancel_auto_focus = camera2_cancel_auto_focus; | |
627 | camera2_ops->take_picture = camera2_take_picture; | |
628 | camera2_ops->cancel_picture = camera2_cancel_picture; | |
629 | camera2_ops->set_parameters = camera2_set_parameters; | |
630 | camera2_ops->get_parameters = camera2_get_parameters; | |
631 | camera2_ops->put_parameters = camera2_put_parameters; | |
632 | camera2_ops->send_command = camera2_send_command; | |
633 | camera2_ops->release = camera2_release; | |
634 | camera2_ops->dump = camera2_dump; | |
635 | ||
636 | *device = &camera2_device->base.common; | |
637 | } | |
638 | ||
639 | return rv; | |
640 | ||
641 | fail: | |
642 | if(camera2_device) { | |
643 | free(camera2_device); | |
644 | camera2_device = NULL; | |
645 | } | |
646 | if(camera2_ops) { | |
647 | free(camera2_ops); | |
648 | camera2_ops = NULL; | |
649 | } | |
650 | *device = NULL; | |
651 | return rv; | |
652 | } |