klte-common: Clean up camera wrapper

- Clean up formatting
 - Clean up logging
 - Clean up method calls
 - Staticise all the functions

Change-Id: If14611e9fdd3ee7f83611204ea51d8b004209ad4
This commit is contained in:
Zhao Wei Liew 2016-07-10 22:46:02 +08:00 committed by Kevin Haggerty
parent 7ca5b82bf1
commit b30d5b0952
1 changed files with 65 additions and 71 deletions

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (C) 2014, The CyanogenMod Project * Copyright (C) 2012-2016, The CyanogenMod Project
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -18,20 +18,20 @@
* @file CameraWrapper.cpp * @file CameraWrapper.cpp
* *
* This file wraps a vendor camera module. * This file wraps a vendor camera module.
*
*/ */
//#define LOG_NDEBUG 0
#define LOG_TAG "CameraWrapper" #define LOG_TAG "CameraWrapper"
#include <cutils/log.h>
#include <utils/threads.h>
#include <utils/String8.h>
#include <hardware/hardware.h>
#include <hardware/camera.h>
#include <camera/Camera.h>
#include <camera/CameraParameters.h> #include <camera/CameraParameters.h>
#include <camera/Camera.h>
#include <cutils/log.h>
#include <hardware/camera.h>
#include <hardware/hardware.h>
#include <utils/String8.h>
#include <utils/threads.h>
#define BACK_CAMERA_ID 0
#define FRONT_CAMERA_ID 1
using namespace android; using namespace android;
@ -41,13 +41,12 @@ static camera_module_t *gVendorModule = 0;
static char **fixed_set_params = NULL; static char **fixed_set_params = NULL;
static int camera_device_open(const hw_module_t *module, const char *name, static int camera_device_open(const hw_module_t *module, const char *name,
hw_device_t **device); hw_device_t **device);
static int camera_device_close(hw_device_t *device);
static int camera_get_number_of_cameras(void); static int camera_get_number_of_cameras(void);
static int camera_get_camera_info(int camera_id, struct camera_info *info); static int camera_get_camera_info(int camera_id, struct camera_info *info);
static struct hw_module_methods_t camera_module_methods = { static struct hw_module_methods_t camera_module_methods = {
.open = camera_device_open .open = camera_device_open,
}; };
camera_module_t HAL_MODULE_INFO_SYM = { camera_module_t HAL_MODULE_INFO_SYM = {
@ -59,17 +58,17 @@ camera_module_t HAL_MODULE_INFO_SYM = {
.name = "MSM8974 Camera Wrapper", .name = "MSM8974 Camera Wrapper",
.author = "The CyanogenMod Project", .author = "The CyanogenMod Project",
.methods = &camera_module_methods, .methods = &camera_module_methods,
.dso = NULL, /* remove compilation warnings */ .dso = NULL,
.reserved = {0}, /* remove compilation warnings */ .reserved = {0},
}, },
.get_number_of_cameras = camera_get_number_of_cameras, .get_number_of_cameras = camera_get_number_of_cameras,
.get_camera_info = camera_get_camera_info, .get_camera_info = camera_get_camera_info,
.set_callbacks = NULL, /* remove compilation warnings */ .set_callbacks = NULL,
.get_vendor_tag_ops = NULL, /* remove compilation warnings */ .get_vendor_tag_ops = NULL,
.open_legacy = NULL, /* remove compilation warnings */ .open_legacy = NULL,
.set_torch_mode = NULL, .set_torch_mode = NULL,
.init = NULL, .init = NULL,
.reserved = {0}, /* remove compilation warnings */ .reserved = {0},
}; };
typedef struct wrapper_camera_device { typedef struct wrapper_camera_device {
@ -83,21 +82,21 @@ typedef struct wrapper_camera_device {
__wrapper_dev->vendor->ops->func(__wrapper_dev->vendor, ##__VA_ARGS__); \ __wrapper_dev->vendor->ops->func(__wrapper_dev->vendor, ##__VA_ARGS__); \
}) })
#define CAMERA_ID(device) (((wrapper_camera_device_t *)(device))->id) #define CAMERA_ID(device) (((wrapper_camera_device_t *)device)->id)
static int check_vendor_module() static int check_vendor_module()
{ {
int rv = 0; int rv;
ALOGV("%s", __FUNCTION__); ALOGV("%s", __FUNCTION__);
if (gVendorModule) if (gVendorModule)
return 0; return 0;
rv = hw_get_module_by_class("camera", "vendor", rv = hw_get_module_by_class("camera", "vendor",
(const hw_module_t **)&gVendorModule); (const hw_module_t**)&gVendorModule);
if (rv) if (rv)
ALOGE("failed to open vendor camera module"); ALOGE("Failed to open vendor camera module %d", rv);
return rv; return rv;
} }
@ -120,10 +119,8 @@ static char *camera_fixup_getparams(int __attribute__((unused)) id,
CameraParameters params; CameraParameters params;
params.unflatten(String8(settings)); params.unflatten(String8(settings));
#if !LOG_NDEBUG
ALOGV("%s: original parameters:", __FUNCTION__); ALOGV("%s: original parameters:", __FUNCTION__);
params.dump(); params.dump();
#endif
//Hide nv12-venus from Android. //Hide nv12-venus from Android.
if (strcmp (params.getPreviewFormat(), PIXEL_FORMAT_NV12_VENUS) == 0) if (strcmp (params.getPreviewFormat(), PIXEL_FORMAT_NV12_VENUS) == 0)
@ -152,15 +149,11 @@ static char *camera_fixup_getparams(int __attribute__((unused)) id,
/* Enforce video-snapshot-supported to true */ /* Enforce video-snapshot-supported to true */
params.set(CameraParameters::KEY_VIDEO_SNAPSHOT_SUPPORTED, "true"); params.set(CameraParameters::KEY_VIDEO_SNAPSHOT_SUPPORTED, "true");
String8 strParams = params.flatten(); ALOGV("%s: Fixed parameters:", __FUNCTION__);
char *ret = strdup(strParams.string());
#if !LOG_NDEBUG
ALOGV("%s: fixed parameters:", __FUNCTION__);
params.dump(); params.dump();
#endif
return ret; String8 strParams = params.flatten();
return strdup(strParams.string());
} }
static char *camera_fixup_setparams(int id, const char *settings) static char *camera_fixup_setparams(int id, const char *settings)
@ -168,10 +161,8 @@ static char *camera_fixup_setparams(int id, const char *settings)
CameraParameters params; CameraParameters params;
params.unflatten(String8(settings)); params.unflatten(String8(settings));
#if !LOG_NDEBUG
ALOGV("%s: original parameters:", __FUNCTION__); ALOGV("%s: original parameters:", __FUNCTION__);
params.dump(); params.dump();
#endif
const char *recordingHint = params.get(CameraParameters::KEY_RECORDING_HINT); const char *recordingHint = params.get(CameraParameters::KEY_RECORDING_HINT);
bool isVideo = recordingHint && !strcmp(recordingHint, "true"); bool isVideo = recordingHint && !strcmp(recordingHint, "true");
@ -183,26 +174,24 @@ static char *camera_fixup_setparams(int id, const char *settings)
params.set(CameraParameters::KEY_ZSL, CameraParameters::ZSL_ON); params.set(CameraParameters::KEY_ZSL, CameraParameters::ZSL_ON);
} }
String8 strParams = params.flatten(); ALOGV("%s: Fixed parameters:", __FUNCTION__);
params.dump();
String8 strParams = params.flatten();
if (fixed_set_params[id]) if (fixed_set_params[id])
free(fixed_set_params[id]); free(fixed_set_params[id]);
fixed_set_params[id] = strdup(strParams.string()); fixed_set_params[id] = strdup(strParams.string());
char *ret = fixed_set_params[id]; char *ret = fixed_set_params[id];
#if !LOG_NDEBUG
ALOGV("%s: fixed parameters:", __FUNCTION__);
params.dump();
#endif
return ret; return ret;
} }
/******************************************************************* /*******************************************************************
* implementation of camera_device_ops functions * Implementation of camera_device_ops functions
*******************************************************************/ *******************************************************************/
static char *camera_get_parameters(struct camera_device *device); static char *camera_get_parameters(struct camera_device *device);
static int camera_set_parameters(struct camera_device *device, const char *params); static int camera_set_parameters(struct camera_device *device, const char *params);
static int camera_set_preview_window(struct camera_device *device, static int camera_set_preview_window(struct camera_device *device,
struct preview_stream_ops *window) struct preview_stream_ops *window)
{ {
@ -228,7 +217,8 @@ static void camera_set_callbacks(struct camera_device *device,
ALOGV("%s->%08X->%08X", __FUNCTION__, (uintptr_t)device, ALOGV("%s->%08X->%08X", __FUNCTION__, (uintptr_t)device,
(uintptr_t)(((wrapper_camera_device_t*)device)->vendor)); (uintptr_t)(((wrapper_camera_device_t*)device)->vendor));
VENDOR_CALL(device, set_callbacks, notify_cb, data_cb, data_cb_timestamp, get_memory, user); VENDOR_CALL(device, set_callbacks, notify_cb, data_cb, data_cb_timestamp,
get_memory, user);
} }
static void camera_enable_msg_type(struct camera_device *device, static void camera_enable_msg_type(struct camera_device *device,
@ -410,7 +400,8 @@ static int camera_cancel_picture(struct camera_device *device)
return VENDOR_CALL(device, cancel_picture); return VENDOR_CALL(device, cancel_picture);
} }
static int camera_set_parameters(struct camera_device *device, const char *params) static int camera_set_parameters(struct camera_device *device,
const char *params)
{ {
if (!device) if (!device)
return -EINVAL; return -EINVAL;
@ -418,11 +409,9 @@ static int camera_set_parameters(struct camera_device *device, const char *param
ALOGV("%s->%08X->%08X", __FUNCTION__, (uintptr_t)device, ALOGV("%s->%08X->%08X", __FUNCTION__, (uintptr_t)device,
(uintptr_t)(((wrapper_camera_device_t*)device)->vendor)); (uintptr_t)(((wrapper_camera_device_t*)device)->vendor));
char *tmp = NULL; char *tmp = camera_fixup_setparams(CAMERA_ID(device), params);
tmp = camera_fixup_setparams(CAMERA_ID(device), params);
int ret = VENDOR_CALL(device, set_parameters, tmp); return VENDOR_CALL(device, set_parameters, tmp);
return ret;
} }
static char *camera_get_parameters(struct camera_device *device) static char *camera_get_parameters(struct camera_device *device)
@ -436,13 +425,15 @@ static char *camera_get_parameters(struct camera_device *device)
char *params = VENDOR_CALL(device, get_parameters); char *params = VENDOR_CALL(device, get_parameters);
char *tmp = camera_fixup_getparams(CAMERA_ID(device), params); char *tmp = camera_fixup_getparams(CAMERA_ID(device), params);
VENDOR_CALL(device, put_parameters, params); VENDOR_CALL(device, put_parameters, params);
params = tmp; params = tmp;
return params; return params;
} }
static void camera_put_parameters(struct camera_device *device, char *params) static void camera_put_parameters(__unused struct camera_device *device,
char *params)
{ {
if (params) if (params)
free(params); free(params);
@ -462,23 +453,23 @@ static int camera_send_command(struct camera_device *device,
static void camera_release(struct camera_device *device) static void camera_release(struct camera_device *device)
{ {
ALOGV("%s->%08X->%08X", __FUNCTION__, (uintptr_t)device,
(uintptr_t)(((wrapper_camera_device_t*)device)->vendor));
if (!device) if (!device)
return; return;
ALOGV("%s->%08X->%08X", __FUNCTION__, (uintptr_t)device,
(uintptr_t)(((wrapper_camera_device_t*)device)->vendor));
VENDOR_CALL(device, release); VENDOR_CALL(device, release);
} }
static int camera_dump(struct camera_device *device, int fd) static int camera_dump(struct camera_device *device, int fd)
{ {
ALOGV("%s->%08X->%08X", __FUNCTION__, (uintptr_t)device,
(uintptr_t)(((wrapper_camera_device_t*)device)->vendor));
if (!device) if (!device)
return -EINVAL; return -EINVAL;
ALOGV("%s->%08X->%08X", __FUNCTION__, (uintptr_t)device,
(uintptr_t)(((wrapper_camera_device_t*)device)->vendor));
return VENDOR_CALL(device, dump, fd); return VENDOR_CALL(device, dump, fd);
} }
@ -509,6 +500,7 @@ static int camera_device_close(hw_device_t *device)
if (wrapper_dev->base.ops) if (wrapper_dev->base.ops)
free(wrapper_dev->base.ops); free(wrapper_dev->base.ops);
free(wrapper_dev); free(wrapper_dev);
done: done:
#ifdef HEAPTRACKER #ifdef HEAPTRACKER
heaptracker_free_leaked_memory(); heaptracker_free_leaked_memory();
@ -517,12 +509,13 @@ done:
} }
/******************************************************************* /*******************************************************************
* implementation of camera_module functions * Implementation of camera_module functions
*******************************************************************/ *******************************************************************/
/* open device handle to one of the cameras /*
* Open device handle to one of the cameras
* *
* assume camera service will keep singleton of each camera * Assume camera service will keep singleton of each camera
* so this function will always only be called once per camera instance * so this function will always only be called once per camera instance
*/ */
@ -531,54 +524,55 @@ static int camera_device_open(const hw_module_t *module, const char *name,
{ {
int rv = 0; int rv = 0;
int num_cameras = 0; int num_cameras = 0;
int cameraid; int camera_id;
wrapper_camera_device_t *camera_device = NULL; wrapper_camera_device_t *camera_device = NULL;
camera_device_ops_t *camera_ops = NULL; camera_device_ops_t *camera_ops = NULL;
Mutex::Autolock lock(gCameraWrapperLock); Mutex::Autolock lock(gCameraWrapperLock);
ALOGV("camera_device open"); ALOGV("%s", __FUNCTION__);
if (name != NULL) { if (name != NULL) {
if (check_vendor_module()) if (check_vendor_module())
return -EINVAL; return -EINVAL;
cameraid = atoi(name); camera_id = atoi(name);
num_cameras = gVendorModule->get_number_of_cameras(); num_cameras = gVendorModule->get_number_of_cameras();
fixed_set_params = (char **) malloc(sizeof(char *) * num_cameras); fixed_set_params = (char **) malloc(sizeof(char *) *num_cameras);
if (!fixed_set_params) { if (!fixed_set_params) {
ALOGE("parameter memory allocation fail"); ALOGE("Parameter memory allocation fail");
rv = -ENOMEM; rv = -ENOMEM;
goto fail; goto fail;
} }
memset(fixed_set_params, 0, sizeof(char *) * num_cameras); memset(fixed_set_params, 0, sizeof(char *) * num_cameras);
if (cameraid > num_cameras) { if (camera_id > num_cameras) {
ALOGE("camera service provided cameraid out of bounds, " ALOGE("Camera service provided camera_id out of bounds, "
"cameraid = %d, num supported = %d", "camera_id = %d, num supported = %d",
cameraid, num_cameras); camera_id, num_cameras);
rv = -EINVAL; rv = -EINVAL;
goto fail; goto fail;
} }
camera_device = (wrapper_camera_device_t*)malloc(sizeof(*camera_device)); camera_device = (wrapper_camera_device_t*)
malloc(sizeof(*camera_device));
if (!camera_device) { if (!camera_device) {
ALOGE("camera_device allocation fail"); ALOGE("camera_device allocation fail");
rv = -ENOMEM; rv = -ENOMEM;
goto fail; goto fail;
} }
memset(camera_device, 0, sizeof(*camera_device)); memset(camera_device, 0, sizeof(*camera_device));
camera_device->id = cameraid; camera_device->id = camera_id;
rv = gVendorModule->common.methods->open( rv = gVendorModule->common.methods->open(
(const hw_module_t*)gVendorModule, name, (const hw_module_t*)gVendorModule, name,
(hw_device_t**)&(camera_device->vendor)); (hw_device_t**)&(camera_device->vendor));
if (rv) { if (rv) {
ALOGE("vendor camera open fail"); ALOGE("Vendor camera open fail");
goto fail; goto fail;
} }
ALOGV("%s: got vendor camera device 0x%08X", ALOGV("%s: Got vendor camera device 0x%08X",
__FUNCTION__, (uintptr_t)(camera_device->vendor)); __FUNCTION__, (uintptr_t)(camera_device->vendor));
camera_ops = (camera_device_ops_t*)malloc(sizeof(*camera_ops)); camera_ops = (camera_device_ops_t*)malloc(sizeof(*camera_ops));