blob: 002276d1d31685dd561d95a1b841ac0c5366c716 [file] [log] [blame]
Zvikomborero VIncent Zvikaramba93777912016-07-24 00:51:04 -04001/*
2 * Copyright (C) 2014, The CyanogenMod 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* @file CameraWrapper.cpp
19*
20* This file wraps a vendor camera module.
21*
22*/
23
24//#define LOG_NDEBUG 0
25
26#define LOG_TAG "CameraWrapper"
27#include <cutils/log.h>
28
29#include <utils/threads.h>
30#include <utils/String8.h>
31#include <hardware/hardware.h>
32#include <hardware/camera.h>
33#include <camera/Camera.h>
34#include <camera/CameraParameters.h>
35
36static android::Mutex gCameraWrapperLock;
37static camera_module_t *gVendorModule = 0;
38
39static char **fixed_set_params = NULL;
40
41static int camera_device_open(const hw_module_t *module, const char *name,
42 hw_device_t **device);
43static int camera_get_number_of_cameras(void);
44static int camera_get_camera_info(int camera_id, struct camera_info *info);
45static int camera_send_command(struct camera_device *device, int32_t cmd,
46 int32_t arg1, int32_t arg2);
47
48static struct hw_module_methods_t camera_module_methods = {
49 .open = camera_device_open,
50};
51
52camera_module_t HAL_MODULE_INFO_SYM = {
53 .common = {
54 .tag = HARDWARE_MODULE_TAG,
55 .module_api_version = CAMERA_MODULE_API_VERSION_1_0,
56 .hal_api_version = HARDWARE_HAL_API_VERSION,
57 .id = CAMERA_HARDWARE_MODULE_ID,
58 .name = "Samsung MSM8916 Camera Wrapper",
59 .author = "The CyanogenMod Project",
60 .methods = &camera_module_methods,
61 .dso = NULL, /* remove compilation warnings */
62 .reserved = {0}, /* remove compilation warnings */
63 },
64
65 .get_number_of_cameras = camera_get_number_of_cameras,
66 .get_camera_info = camera_get_camera_info,
67 .set_callbacks = NULL, /* remove compilation warnings */
68 .get_vendor_tag_ops = NULL, /* remove compilation warnings */
69 .open_legacy = NULL, /* remove compilation warnings */
70 .reserved = {0}, /* remove compilation warnings */
71};
72
73typedef struct wrapper_camera_device {
74 camera_device_t base;
75 int id;
76 camera_device_t *vendor;
77} wrapper_camera_device_t;
78
79#define VENDOR_CALL(device, func, ...) ({ \
80 wrapper_camera_device_t *__wrapper_dev = (wrapper_camera_device_t*) device; \
81 __wrapper_dev->vendor->ops->func(__wrapper_dev->vendor, ##__VA_ARGS__); \
82})
83
84#define CAMERA_ID(device) (((wrapper_camera_device_t *)(device))->id)
85
86static int check_vendor_module()
87{
88 int rv = 0;
89 ALOGV("%s", __FUNCTION__);
90
91 if (gVendorModule)
92 return 0;
93
94 rv = hw_get_module_by_class("camera", "vendor",
95 (const hw_module_t**)&gVendorModule);
96 if (rv)
97 ALOGE("failed to open vendor camera module");
98 return rv;
99}
100
101static bool needYUV420preview(android::CameraParameters &params) {
102 int video_width, video_height;
103 params.getPreviewSize(&video_width, &video_height);
104 ALOGV("%s : PreviewSize is %x", __FUNCTION__, video_width*video_height);
105 return video_width*video_height <= 720*720;
106}
107
108#define KEY_VIDEO_HFR_VALUES "video-hfr-values"
109
110const static char * iso_values[] = {"auto,ISO_HJR,ISO100,ISO200,ISO400,ISO800,ISO1600,auto"};
111
112static char *camera_fixup_getparams(int id, const char *settings)
113{
114 android::CameraParameters params;
115 params.unflatten(android::String8(settings));
116
117#if !LOG_NDEBUG
118 ALOGV("%s: original parameters:", __FUNCTION__);
119 params.dump();
120#endif
121
122 // fix params here
123 params.set(android::CameraParameters::KEY_SUPPORTED_ISO_MODES, iso_values[id]);
124 params.set(android::CameraParameters::KEY_EXPOSURE_COMPENSATION_STEP, "0.5");
125 params.set(android::CameraParameters::KEY_MIN_EXPOSURE_COMPENSATION, "-4");
126 params.set(android::CameraParameters::KEY_MAX_EXPOSURE_COMPENSATION, "4");
127
128 /* If the vendor has HFR values but doesn't also expose that
129 * this can be turned off, fixup the params to tell the Camera
130 * that it really is okay to turn it off.
131 */
132 const char *hfrValues = params.get(KEY_VIDEO_HFR_VALUES);
133 if (hfrValues && *hfrValues && ! strstr(hfrValues, "off")) {
134 char tmp[strlen(hfrValues) + 4 + 1];
135 sprintf(tmp, "%s,off", hfrValues);
136 params.set(KEY_VIDEO_HFR_VALUES, tmp);
137 }
138
139 /* Enforce video-snapshot-supported to true */
140 params.set(android::CameraParameters::KEY_VIDEO_SNAPSHOT_SUPPORTED, "true");
141
142 android::String8 strParams = params.flatten();
143 char *ret = strdup(strParams.string());
144
145#if !LOG_NDEBUG
146 ALOGV("%s: fixed parameters:", __FUNCTION__);
147 params.dump();
148#endif
149
150 return ret;
151}
152
153static char *camera_fixup_setparams(struct camera_device *device, const char *settings)
154{
155 int id = CAMERA_ID(device);
156 android::CameraParameters params;
157 params.unflatten(android::String8(settings));
158
159#if !LOG_NDEBUG
160 ALOGV("%s: original parameters:", __FUNCTION__);
161 params.dump();
162#endif
163
164 const char *recordingHint = params.get(android::CameraParameters::KEY_RECORDING_HINT);
165 bool isVideo = recordingHint && !strcmp(recordingHint, "true");
166
167 if (isVideo) {
168 params.set(android::CameraParameters::KEY_ZSL, android::CameraParameters::ZSL_OFF);
169 } else {
170 params.set(android::CameraParameters::KEY_ZSL, android::CameraParameters::ZSL_ON);
171 }
172
173 if (needYUV420preview(params)) {
174 ALOGV("%s: switching preview format to yuv420p", __FUNCTION__);
175 params.set("preview-format", "yuv420p");
176 }
177
178 // fix params here
179 // No need to fix-up ISO_HJR, it is the same for userspace and the camera lib
180 if (params.get("iso")) {
181 const char *isoMode = params.get(android::CameraParameters::KEY_ISO_MODE);
182 if (strcmp(isoMode, "ISO100") == 0)
183 params.set(android::CameraParameters::KEY_ISO_MODE, "100");
184 else if (strcmp(isoMode, "ISO200") == 0)
185 params.set(android::CameraParameters::KEY_ISO_MODE, "200");
186 else if (strcmp(isoMode, "ISO400") == 0)
187 params.set(android::CameraParameters::KEY_ISO_MODE, "400");
188 else if (strcmp(isoMode, "ISO800") == 0)
189 params.set(android::CameraParameters::KEY_ISO_MODE, "800");
190 else if (strcmp(isoMode, "ISO1600") == 0)
191 params.set(android::CameraParameters::KEY_ISO_MODE, "1600");
192 }
193
194 android::String8 strParams = params.flatten();
195
196 if (fixed_set_params[id])
197 free(fixed_set_params[id]);
198 fixed_set_params[id] = strdup(strParams.string());
199 char *ret = fixed_set_params[id];
200
201#if !LOG_NDEBUG
202 ALOGV("%s: fixed parameters:", __FUNCTION__);
203 params.dump();
204#endif
205
206 return ret;
207}
208
209/*******************************************************************
210 * implementation of camera_device_ops functions
211 *******************************************************************/
212
213static int camera_set_preview_window(struct camera_device *device,
214 struct preview_stream_ops *window)
215{
216 ALOGV("%s->%08X->%08X", __FUNCTION__, (uintptr_t)device,
217 (uintptr_t)(((wrapper_camera_device_t*)device)->vendor));
218
219 if (!device)
220 return -EINVAL;
221
222 return VENDOR_CALL(device, set_preview_window, window);
223}
224
225static void camera_set_callbacks(struct camera_device *device,
226 camera_notify_callback notify_cb,
227 camera_data_callback data_cb,
228 camera_data_timestamp_callback data_cb_timestamp,
229 camera_request_memory get_memory,
230 void *user)
231{
232 ALOGV("%s->%08X->%08X", __FUNCTION__, (uintptr_t)device,
233 (uintptr_t)(((wrapper_camera_device_t*)device)->vendor));
234
235 if (!device)
236 return;
237
238 VENDOR_CALL(device, set_callbacks, notify_cb, data_cb, data_cb_timestamp,
239 get_memory, user);
240}
241
242static void camera_enable_msg_type(struct camera_device *device,
243 int32_t msg_type)
244{
245 ALOGV("%s->%08X->%08X", __FUNCTION__, (uintptr_t)device,
246 (uintptr_t)(((wrapper_camera_device_t*)device)->vendor));
247
248 if (!device)
249 return;
250
251 VENDOR_CALL(device, enable_msg_type, msg_type);
252}
253
254static void camera_disable_msg_type(struct camera_device *device,
255 int32_t msg_type)
256{
257 ALOGV("%s->%08X->%08X", __FUNCTION__, (uintptr_t)device,
258 (uintptr_t)(((wrapper_camera_device_t*)device)->vendor));
259
260 if (!device)
261 return;
262
263 VENDOR_CALL(device, disable_msg_type, msg_type);
264}
265
266static int camera_msg_type_enabled(struct camera_device *device,
267 int32_t msg_type)
268{
269 ALOGV("%s->%08X->%08X", __FUNCTION__, (uintptr_t)device,
270 (uintptr_t)(((wrapper_camera_device_t*)device)->vendor));
271
272 if (!device)
273 return 0;
274
275 return VENDOR_CALL(device, msg_type_enabled, msg_type);
276}
277
278static int camera_start_preview(struct camera_device *device)
279{
280 ALOGV("%s->%08X->%08X", __FUNCTION__, (uintptr_t)device,
281 (uintptr_t)(((wrapper_camera_device_t*)device)->vendor));
282
283 if (!device)
284 return -EINVAL;
285
286 return VENDOR_CALL(device, start_preview);
287}
288
289static void camera_stop_preview(struct camera_device *device)
290{
291 ALOGV("%s->%08X->%08X", __FUNCTION__, (uintptr_t)device,
292 (uintptr_t)(((wrapper_camera_device_t*)device)->vendor));
293
294 if (!device)
295 return;
296
297 VENDOR_CALL(device, stop_preview);
298}
299
300static int camera_preview_enabled(struct camera_device *device)
301{
302 ALOGV("%s->%08X->%08X", __FUNCTION__, (uintptr_t)device,
303 (uintptr_t)(((wrapper_camera_device_t*)device)->vendor));
304
305 if (!device)
306 return -EINVAL;
307
308 return VENDOR_CALL(device, preview_enabled);
309}
310
311static int camera_store_meta_data_in_buffers(struct camera_device *device,
312 int enable)
313{
314 ALOGV("%s->%08X->%08X", __FUNCTION__, (uintptr_t)device,
315 (uintptr_t)(((wrapper_camera_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
323static int camera_start_recording(struct camera_device *device)
324{
325 ALOGV("%s->%08X->%08X", __FUNCTION__, (uintptr_t)device,
326 (uintptr_t)(((wrapper_camera_device_t*)device)->vendor));
327
328 if (!device)
329 return EINVAL;
330
331 return VENDOR_CALL(device, start_recording);
332}
333
334static void camera_stop_recording(struct camera_device *device)
335{
336 ALOGV("%s->%08X->%08X", __FUNCTION__, (uintptr_t)device,
337 (uintptr_t)(((wrapper_camera_device_t*)device)->vendor));
338
339 if (!device)
340 return;
341
342 VENDOR_CALL(device, stop_recording);
343}
344
345static int camera_recording_enabled(struct camera_device *device)
346{
347 ALOGV("%s->%08X->%08X", __FUNCTION__, (uintptr_t)device,
348 (uintptr_t)(((wrapper_camera_device_t*)device)->vendor));
349
350 if (!device)
351 return -EINVAL;
352
353 return VENDOR_CALL(device, recording_enabled);
354}
355
356static void camera_release_recording_frame(struct camera_device *device,
357 const void *opaque)
358{
359 ALOGV("%s->%08X->%08X", __FUNCTION__, (uintptr_t)device,
360 (uintptr_t)(((wrapper_camera_device_t*)device)->vendor));
361
362 if (!device)
363 return;
364
365 VENDOR_CALL(device, release_recording_frame, opaque);
366}
367
368static int camera_auto_focus(struct camera_device *device)
369{
370 ALOGV("%s->%08X->%08X", __FUNCTION__, (uintptr_t)device,
371 (uintptr_t)(((wrapper_camera_device_t*)device)->vendor));
372
373 if (!device)
374 return -EINVAL;
375
376 return VENDOR_CALL(device, auto_focus);
377}
378
379static int camera_cancel_auto_focus(struct camera_device *device)
380{
381 ALOGV("%s->%08X->%08X", __FUNCTION__, (uintptr_t)device,
382 (uintptr_t)(((wrapper_camera_device_t*)device)->vendor));
383
384 if (!device)
385 return -EINVAL;
386
387 return VENDOR_CALL(device, cancel_auto_focus);
388}
389
390static int camera_take_picture(struct camera_device *device)
391{
392 ALOGV("%s->%08X->%08X", __FUNCTION__, (uintptr_t)device,
393 (uintptr_t)(((wrapper_camera_device_t*)device)->vendor));
394
395 if (!device)
396 return -EINVAL;
397
398 return VENDOR_CALL(device, take_picture);
399}
400
401static int camera_cancel_picture(struct camera_device *device)
402{
403 ALOGV("%s->%08X->%08X", __FUNCTION__, (uintptr_t)device,
404 (uintptr_t)(((wrapper_camera_device_t*)device)->vendor));
405
406 if (!device)
407 return -EINVAL;
408
409 return VENDOR_CALL(device, cancel_picture);
410}
411
412static int camera_set_parameters(struct camera_device *device,
413 const char *params)
414{
415 ALOGV("%s->%08X->%08X", __FUNCTION__, (uintptr_t)device,
416 (uintptr_t)(((wrapper_camera_device_t*)device)->vendor));
417
418 if (!device)
419 return -EINVAL;
420
421 char *tmp = NULL;
422 tmp = camera_fixup_setparams(device, params);
423
424 int ret = VENDOR_CALL(device, set_parameters, tmp);
425 return ret;
426}
427
428static char *camera_get_parameters(struct camera_device *device)
429{
430 ALOGV("%s->%08X->%08X", __FUNCTION__, (uintptr_t)device,
431 (uintptr_t)(((wrapper_camera_device_t*)device)->vendor));
432
433 if (!device)
434 return NULL;
435
436 char *params = VENDOR_CALL(device, get_parameters);
437
438 char *tmp = camera_fixup_getparams(CAMERA_ID(device), params);
439 VENDOR_CALL(device, put_parameters, params);
440 params = tmp;
441
442 return params;
443}
444
445static void camera_put_parameters(struct camera_device *device, char *params)
446{
447 ALOGV("%s->%08X->%08X", __FUNCTION__, (uintptr_t)device,
448 (uintptr_t)(((wrapper_camera_device_t*)device)->vendor));
449
450 if (params)
451 free(params);
452}
453
454static int camera_send_command(struct camera_device *device,
455 int32_t cmd, int32_t arg1, int32_t arg2)
456{
457 ALOGV("%s->%08X->%08X", __FUNCTION__, (uintptr_t)device,
458 (uintptr_t)(((wrapper_camera_device_t*)device)->vendor));
459
460 if (!device)
461 return -EINVAL;
462
463 return VENDOR_CALL(device, send_command, cmd, arg1, arg2);
464}
465
466static void camera_release(struct camera_device *device)
467{
468 ALOGV("%s->%08X->%08X", __FUNCTION__, (uintptr_t)device,
469 (uintptr_t)(((wrapper_camera_device_t*)device)->vendor));
470
471 if (!device)
472 return;
473
474 VENDOR_CALL(device, release);
475}
476
477static int camera_dump(struct camera_device *device, int fd)
478{
479 ALOGV("%s->%08X->%08X", __FUNCTION__, (uintptr_t)device,
480 (uintptr_t)(((wrapper_camera_device_t*)device)->vendor));
481
482 if (!device)
483 return -EINVAL;
484
485 return VENDOR_CALL(device, dump, fd);
486}
487
488extern "C" void heaptracker_free_leaked_memory(void);
489
490static int camera_device_close(hw_device_t *device)
491{
492 int ret = 0;
493 wrapper_camera_device_t *wrapper_dev = NULL;
494
495 ALOGV("%s", __FUNCTION__);
496
497 android::Mutex::Autolock lock(gCameraWrapperLock);
498
499 if (!device) {
500 ret = -EINVAL;
501 goto done;
502 }
503
504 for (int i = 0; i < camera_get_number_of_cameras(); i++) {
505 if (fixed_set_params[i])
506 free(fixed_set_params[i]);
507 }
508
509 wrapper_dev = (wrapper_camera_device_t*) device;
510
511 wrapper_dev->vendor->common.close((hw_device_t*)wrapper_dev->vendor);
512 if (wrapper_dev->base.ops)
513 free(wrapper_dev->base.ops);
514 free(wrapper_dev);
515done:
516#ifdef HEAPTRACKER
517 heaptracker_free_leaked_memory();
518#endif
519 return ret;
520}
521
522/*******************************************************************
523 * implementation of camera_module functions
524 *******************************************************************/
525
526/* open device handle to one of the cameras
527 *
528 * assume camera service will keep singleton of each camera
529 * so this function will always only be called once per camera instance
530 */
531
532static int camera_device_open(const hw_module_t *module, const char *name,
533 hw_device_t **device)
534{
535 int rv = 0;
536 int num_cameras = 0;
537 int cameraid;
538 wrapper_camera_device_t *camera_device = NULL;
539 camera_device_ops_t *camera_ops = NULL;
540
541 android::Mutex::Autolock lock(gCameraWrapperLock);
542
543 ALOGV("%s", __FUNCTION__);
544
545 if (name != NULL) {
546 if (check_vendor_module())
547 return -EINVAL;
548
549 cameraid = atoi(name);
550 num_cameras = gVendorModule->get_number_of_cameras();
551
552 fixed_set_params = (char **) malloc(sizeof(char *) * num_cameras);
553 if (!fixed_set_params) {
554 ALOGE("parameter memory allocation fail");
555 rv = -ENOMEM;
556 goto fail;
557 }
558 memset(fixed_set_params, 0, sizeof(char *) * num_cameras);
559
560 if (cameraid > num_cameras) {
561 ALOGE("camera service provided cameraid out of bounds, "
562 "cameraid = %d, num supported = %d",
563 cameraid, num_cameras);
564 rv = -EINVAL;
565 goto fail;
566 }
567
568 camera_device = (wrapper_camera_device_t*)malloc(sizeof(*camera_device));
569 if (!camera_device) {
570 ALOGE("camera_device allocation fail");
571 rv = -ENOMEM;
572 goto fail;
573 }
574 memset(camera_device, 0, sizeof(*camera_device));
575 camera_device->id = cameraid;
576
577 rv = gVendorModule->common.methods->open(
578 (const hw_module_t*)gVendorModule, name,
579 (hw_device_t**)&(camera_device->vendor));
580 if (rv) {
581 ALOGE("vendor camera open fail");
582 goto fail;
583 }
584 ALOGV("%s: got vendor camera device 0x%08X",
585 __FUNCTION__, (uintptr_t)(camera_device->vendor));
586
587 camera_ops = (camera_device_ops_t*)malloc(sizeof(*camera_ops));
588 if (!camera_ops) {
589 ALOGE("camera_ops allocation fail");
590 rv = -ENOMEM;
591 goto fail;
592 }
593
594 memset(camera_ops, 0, sizeof(*camera_ops));
595
596 camera_device->base.common.tag = HARDWARE_DEVICE_TAG;
597 camera_device->base.common.version = HARDWARE_DEVICE_API_VERSION(1, 0);
598 camera_device->base.common.module = (hw_module_t *)(module);
599 camera_device->base.common.close = camera_device_close;
600 camera_device->base.ops = camera_ops;
601
602 camera_ops->set_preview_window = camera_set_preview_window;
603 camera_ops->set_callbacks = camera_set_callbacks;
604 camera_ops->enable_msg_type = camera_enable_msg_type;
605 camera_ops->disable_msg_type = camera_disable_msg_type;
606 camera_ops->msg_type_enabled = camera_msg_type_enabled;
607 camera_ops->start_preview = camera_start_preview;
608 camera_ops->stop_preview = camera_stop_preview;
609 camera_ops->preview_enabled = camera_preview_enabled;
610 camera_ops->store_meta_data_in_buffers = camera_store_meta_data_in_buffers;
611 camera_ops->start_recording = camera_start_recording;
612 camera_ops->stop_recording = camera_stop_recording;
613 camera_ops->recording_enabled = camera_recording_enabled;
614 camera_ops->release_recording_frame = camera_release_recording_frame;
615 camera_ops->auto_focus = camera_auto_focus;
616 camera_ops->cancel_auto_focus = camera_cancel_auto_focus;
617 camera_ops->take_picture = camera_take_picture;
618 camera_ops->cancel_picture = camera_cancel_picture;
619 camera_ops->set_parameters = camera_set_parameters;
620 camera_ops->get_parameters = camera_get_parameters;
621 camera_ops->put_parameters = camera_put_parameters;
622 camera_ops->send_command = camera_send_command;
623 camera_ops->release = camera_release;
624 camera_ops->dump = camera_dump;
625
626 *device = &camera_device->base.common;
627 }
628
629 return rv;
630
631fail:
632 if (camera_device) {
633 free(camera_device);
634 camera_device = NULL;
635 }
636 if (camera_ops) {
637 free(camera_ops);
638 camera_ops = NULL;
639 }
640 *device = NULL;
641 return rv;
642}
643
644static int camera_get_number_of_cameras(void)
645{
646 ALOGV("%s", __FUNCTION__);
647 if (check_vendor_module())
648 return 0;
649 return gVendorModule->get_number_of_cameras();
650}
651
652static int camera_get_camera_info(int camera_id, struct camera_info *info)
653{
654 ALOGV("%s", __FUNCTION__);
655 if (check_vendor_module())
656 return 0;
657 return gVendorModule->get_camera_info(camera_id, info);
658}
659