Skip to content

Instantly share code, notes, and snippets.

@invisiblek
Created October 24, 2014 02:44
Show Gist options
  • Save invisiblek/9b21519c7df8889c5437 to your computer and use it in GitHub Desktop.
Save invisiblek/9b21519c7df8889c5437 to your computer and use it in GitHub Desktop.
/*
* Copyright (C) 2014, The CyanogenMod Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* @file CameraWrapper.cpp
*
* This file wraps a vendor camera module.
*
*/
#define LOG_NDEBUG 0
#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>
static android::Mutex gCameraWrapperLock;
static camera_module_t *gVendorModule = 0;
static char **fixed_set_params = NULL;
static int camera_device_open(const hw_module_t *module, const char *name,
hw_device_t **device);
static int camera_get_number_of_cameras(void);
static int camera_get_camera_info(int camera_id, struct camera_info *info);
static struct hw_module_methods_t camera_module_methods = {
.open = camera_device_open
};
camera_module_t HAL_MODULE_INFO_SYM = {
.common = {
.tag = HARDWARE_MODULE_TAG,
.module_api_version = CAMERA_MODULE_API_VERSION_1_0,
.hal_api_version = HARDWARE_HAL_API_VERSION,
.id = CAMERA_HARDWARE_MODULE_ID,
.name = "G3 Camera Wrapper",
.author = "The CyanogenMod Project",
.methods = &camera_module_methods,
.dso = NULL, /* remove compilation warnings */
.reserved = {0}, /* remove compilation warnings */
},
.get_number_of_cameras = camera_get_number_of_cameras,
.get_camera_info = camera_get_camera_info,
.set_callbacks = NULL, /* remove compilation warnings */
.get_vendor_tag_ops = NULL, /* remove compilation warnings */
.reserved = {0}, /* remove compilation warnings */
};
typedef struct wrapper_camera_device {
camera_device_t base;
int id;
camera_device_t *vendor;
} wrapper_camera_device_t;
#define VENDOR_CALL(device, func, ...) ({ \
wrapper_camera_device_t *__wrapper_dev = (wrapper_camera_device_t*) device; \
__wrapper_dev->vendor->ops->func(__wrapper_dev->vendor, ##__VA_ARGS__); \
})
#define CAMERA_ID(device) (((wrapper_camera_device_t *)(device))->id)
static int check_vendor_module()
{
int rv = 0;
ALOGV("%s", __FUNCTION__);
if (gVendorModule)
return 0;
rv = hw_get_module_by_class("camera", "vendor",
(const hw_module_t**)&gVendorModule);
if (rv)
ALOGE("failed to open vendor camera module");
return rv;
}
static char *camera_fixup_getparams(int id, const char *settings)
{
android::CameraParameters params;
params.unflatten(android::String8(settings));
#if !LOG_NDEBUG
ALOGV("%s: original parameters:", __FUNCTION__);
params.dump();
#endif
#if !LOG_NDEBUG
ALOGV("%s: fixed parameters:", __FUNCTION__);
params.dump();
#endif
android::String8 strParams = params.flatten();
char *ret = strdup(strParams.string());
return ret;
}
static char *camera_fixup_setparams(int id, const char *settings)
{
android::CameraParameters params;
params.unflatten(android::String8(settings));
#if !LOG_NDEBUG
ALOGV("%s: original parameters:", __FUNCTION__);
params.dump();
#endif
#if !LOG_NDEBUG
ALOGV("%s: fixed parameters:", __FUNCTION__);
params.dump();
#endif
android::String8 strParams = params.flatten();
if (fixed_set_params[id])
free(fixed_set_params[id]);
fixed_set_params[id] = strdup(strParams.string());
char *ret = fixed_set_params[id];
return ret;
}
/*******************************************************************
* implementation of camera_device_ops functions
*******************************************************************/
static int camera_set_preview_window(struct camera_device *device,
struct preview_stream_ops *window)
{
ALOGV("%s->%08X->%08X", __FUNCTION__, (uintptr_t)device,
(uintptr_t)(((wrapper_camera_device_t*)device)->vendor));
if (!device)
return -EINVAL;
return VENDOR_CALL(device, set_preview_window, window);
}
static void camera_set_callbacks(struct camera_device *device,
camera_notify_callback notify_cb,
camera_data_callback data_cb,
camera_data_timestamp_callback data_cb_timestamp,
camera_request_memory get_memory,
void *user)
{
ALOGV("%s->%08X->%08X", __FUNCTION__, (uintptr_t)device,
(uintptr_t)(((wrapper_camera_device_t*)device)->vendor));
if (!device)
return;
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,
int32_t msg_type)
{
ALOGV("%s->%08X->%08X", __FUNCTION__, (uintptr_t)device,
(uintptr_t)(((wrapper_camera_device_t*)device)->vendor));
if (!device)
return;
VENDOR_CALL(device, enable_msg_type, msg_type);
}
static void camera_disable_msg_type(struct camera_device *device,
int32_t msg_type)
{
ALOGV("%s->%08X->%08X", __FUNCTION__, (uintptr_t)device,
(uintptr_t)(((wrapper_camera_device_t*)device)->vendor));
if (!device)
return;
VENDOR_CALL(device, disable_msg_type, msg_type);
}
static int camera_msg_type_enabled(struct camera_device *device,
int32_t msg_type)
{
ALOGV("%s->%08X->%08X", __FUNCTION__, (uintptr_t)device,
(uintptr_t)(((wrapper_camera_device_t*)device)->vendor));
if (!device)
return 0;
return VENDOR_CALL(device, msg_type_enabled, msg_type);
}
static int camera_start_preview(struct camera_device *device)
{
ALOGV("%s->%08X->%08X", __FUNCTION__, (uintptr_t)device,
(uintptr_t)(((wrapper_camera_device_t*)device)->vendor));
if (!device)
return -EINVAL;
return VENDOR_CALL(device, start_preview);
}
static void camera_stop_preview(struct camera_device *device)
{
ALOGV("%s->%08X->%08X", __FUNCTION__, (uintptr_t)device,
(uintptr_t)(((wrapper_camera_device_t*)device)->vendor));
if (!device)
return;
VENDOR_CALL(device, stop_preview);
}
static int camera_preview_enabled(struct camera_device *device)
{
ALOGV("%s->%08X->%08X", __FUNCTION__, (uintptr_t)device,
(uintptr_t)(((wrapper_camera_device_t*)device)->vendor));
if (!device)
return -EINVAL;
return VENDOR_CALL(device, preview_enabled);
}
static int camera_store_meta_data_in_buffers(struct camera_device *device,
int enable)
{
ALOGV("%s->%08X->%08X", __FUNCTION__, (uintptr_t)device,
(uintptr_t)(((wrapper_camera_device_t*)device)->vendor));
if (!device)
return -EINVAL;
return VENDOR_CALL(device, store_meta_data_in_buffers, enable);
}
static int camera_start_recording(struct camera_device *device)
{
ALOGV("%s->%08X->%08X", __FUNCTION__, (uintptr_t)device,
(uintptr_t)(((wrapper_camera_device_t*)device)->vendor));
if (!device)
return EINVAL;
return VENDOR_CALL(device, start_recording);
}
static void camera_stop_recording(struct camera_device *device)
{
ALOGV("%s->%08X->%08X", __FUNCTION__, (uintptr_t)device,
(uintptr_t)(((wrapper_camera_device_t*)device)->vendor));
if (!device)
return;
VENDOR_CALL(device, stop_recording);
}
static int camera_recording_enabled(struct camera_device *device)
{
ALOGV("%s->%08X->%08X", __FUNCTION__, (uintptr_t)device,
(uintptr_t)(((wrapper_camera_device_t*)device)->vendor));
if (!device)
return -EINVAL;
return VENDOR_CALL(device, recording_enabled);
}
static void camera_release_recording_frame(struct camera_device *device,
const void *opaque)
{
ALOGV("%s->%08X->%08X", __FUNCTION__, (uintptr_t)device,
(uintptr_t)(((wrapper_camera_device_t*)device)->vendor));
if (!device)
return;
VENDOR_CALL(device, release_recording_frame, opaque);
}
static int camera_auto_focus(struct camera_device *device)
{
ALOGV("%s->%08X->%08X", __FUNCTION__, (uintptr_t)device,
(uintptr_t)(((wrapper_camera_device_t*)device)->vendor));
if (!device)
return -EINVAL;
return VENDOR_CALL(device, auto_focus);
}
static int camera_cancel_auto_focus(struct camera_device *device)
{
ALOGV("%s->%08X->%08X", __FUNCTION__, (uintptr_t)device,
(uintptr_t)(((wrapper_camera_device_t*)device)->vendor));
if (!device)
return -EINVAL;
return VENDOR_CALL(device, cancel_auto_focus);
}
static int camera_take_picture(struct camera_device *device)
{
ALOGV("%s->%08X->%08X", __FUNCTION__, (uintptr_t)device,
(uintptr_t)(((wrapper_camera_device_t*)device)->vendor));
if (!device)
return -EINVAL;
return VENDOR_CALL(device, take_picture);
}
static int camera_cancel_picture(struct camera_device *device)
{
ALOGV("%s->%08X->%08X", __FUNCTION__, (uintptr_t)device,
(uintptr_t)(((wrapper_camera_device_t*)device)->vendor));
if (!device)
return -EINVAL;
return VENDOR_CALL(device, cancel_picture);
}
static int camera_set_parameters(struct camera_device *device,
const char *params)
{
ALOGV("%s->%08X->%08X", __FUNCTION__, (uintptr_t)device,
(uintptr_t)(((wrapper_camera_device_t*)device)->vendor));
if (!device)
return -EINVAL;
char *tmp = NULL;
tmp = camera_fixup_setparams(CAMERA_ID(device), params);
int ret = VENDOR_CALL(device, set_parameters, tmp);
return ret;
}
static char *camera_get_parameters(struct camera_device *device)
{
ALOGV("%s->%08X->%08X", __FUNCTION__, (uintptr_t)device,
(uintptr_t)(((wrapper_camera_device_t*)device)->vendor));
if (!device)
return NULL;
char *params; // = VENDOR_CALL(device, get_parameters);
char *tmp = camera_fixup_getparams(CAMERA_ID(device), params);
VENDOR_CALL(device, put_parameters, params);
params = "ae-bracket-hdr=Off;ae-bracket-hdr-values=Off,AE-Bracket;antibanding=auto;antibanding-values=off,60hz,50hz,auto;auto-exposure=frame-average;auto-exposure-lock=false;auto-exposure-lock-supported=true;auto-exposure-values=frame-average,center-weighted,spot-metering,center-weighted,spot-metering-adv,center-weighted-adv;auto-hdr-enable=disable;auto-whitebalance-lock=false;auto-whitebalance-lock-supported=true;avtimer=disable;camera-mode=1;capture-burst-interval=1;capture-burst-queue-depth=5;capture-burst-retroactive=5;capture-mode=normal;capture-mode-values=auto,asd,landscape,snow,beach,sunset,night,portrait,backlight,sports,steadyphoto,flowers,candlelight,fireworks,party,night-portrait,theatre,action,AR,hdr,text,eis,zoe,panorama,burst,manual,panorama360;contiburst-support-mode=take;contrast=5;contrast-step=1;denoise=denoise-off;denoise-values=denoise-off,denoise-on;dis=disable;dis-values=enable,disable;dual-camera-supported=true;effect=none;effect-values=none,mono,negative,solarize,sepia,posterize,whiteboard,blackboard,aqua,emboss,sketch,neon;enable_depth=1;exposure-compensation=0;exposure-compensation-step=0.166667;exposure-step=10;face-detection=on;face-detection-values=off,on;flash-mode=auto;flash-mode-values=off,auto,on,torch;flip-mode-values=off,flip-v,flip-h,flip-vh;focal-length=3.82;focus-areas=(0,0,0,0,0);focus-distances=Infinity,Infinity,Infinity;focus-mode=continuous-picture;focus-mode-values=auto,infinity,macro,continuous-video,continuous-picture;focus-step=1;hdr-need-1x=false;hdr-supported=true;hfr-size-values=2120x1196,1344x760;histogram=disable;histogram-values=enable,disable;horizontal-view-angle=69.6;iso=auto;iso-values=auto,ISO_HJR,ISO100,ISO200,ISO400,ISO800,ISO1600;jpeg-quality=85;jpeg-thumbnail-height=288;jpeg-thumbnail-quality=95;jpeg-thumbnail-size-values=512x288,480x288,256x154,432x288,320x240,176x144,288x288,0x0;jpeg-thumbnail-width=512;lensshade=enable;lensshade-values=enable,disable;max-contrast=10;max-exposure=392;max-exposure-compensation=12;max-focus=20;max-num-detected-faces-hw=off;max-num-detected-faces-sw=off;max-num-focus-areas=1;max-num-metering-areas=10;max-saturation=10;max-sce-factor=100;max-sharpness=36;max-wb-ct=7500;max-zoom=59;mce=enable;mce-values=enable,disable;metering-areas=(0,0,0,0,0);min-contrast=0;min-exposure=0;min-exposure-compensation=-12;min-focus=0;min-saturation=0;min-sce-factor=-100;min-sharpness=0;min-wb-ct=2300;num-snaps-per-shutter=1;picture-format=jpeg;picture-format-values=jpeg,bayer-mipi-10grbg,bayer-ideal-qcom-10grbg,bayer-qcom-10grbg,yuv422sp;picture-size=2688x1520;picture-size-values=2688x1520,2592x1472,2560x1440,2048x1520,2048x1216,2048x1152,1952x1456,1920x1088,1920x1080,1600x1200,1600x896,1520x1520,1456x1088,1456x1456,1456x880,1456x832,1440x1088,1280x960,1280x768,1280x720,1088x1088,1024x768,800x600,800x480,768x432,720x720,720x480,640x480,640x384,640x368,352x288,320x240;preferred-preview-size-for-video=1920x1088;preview-flip=off;preview-format=yuv420sp;preview-format-values=yuv420sp,yuv420p,nv12-venus;preview-fps-range=7000,30000;preview-fps-range-values=(7000,30000);preview-frame-rate=30;preview-frame-rate-mode=frame-rate-fixed;preview-frame-rate-values=7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30;preview-size=1920x1080;preview-size-values=1920x1088,1920x1080,1808x1080,1440x1088,1280x720,1088x1088,960x720,960x544,864x480,800x480,768x432,720x720,720x480,640x360,640x480,576x432,544x544,480x480,480x320,384x288,352x288,320x240,240x160,176x144;qc-camera-features=1407;qc-max-num-requested-faces=5;raw-size=2688x1520;recording-hint=false;redeye-reduction=disable;redeye-reduction-values=enable,disable;rotation=90;saturation=5;saturation-step=1;sce-factor-step=10;scene-detect=off;scene-detect-values=off,on;scene-mode=auto;scene-mode-values=auto,asd,landscape,snow,beach,sunset,night,portrait,backlight,sports,steadyphoto,flowers,candlelight,fireworks,party,night-portrait,theatre,action,AR,hdr,text,eis,zoe,panorama,burst,manual,panorama360;selectable-zone-af=auto;selectable-zone-af-values=auto,spot-metering,center-weighted,frame-average;sharpness=12;sharpness-step=6;shutter-threshold=0.200000;skinToneEnhancement=0;skinToneEnhancement-values=enable,disable;slow-motion-res=1280x720;slow-motion-x=4;smooth-zoom-supported=false;snapshot-fd-data-enable=0;snapshot-picture-flip=off;supported-live-snapshot-sizes=2688x1520,1920x1080,1600x1200,1280x768,1280x720,1024x768,800x600,864x480,800x480,768x432,720x480,640x480,352x288,320x240,176x144;tintless=enable;touch-af-aec=touch-off;touch-af-aec-values=touch-off,touch-on;vertical-view-angle=43.0;video-1080p60fps-supported=true;video-720p60fps-supported=true;video-flip=off;video-frame-format=yuv420sp;video-hdr=off;video-hdr-supported=true;video-hdr-values=off,on;video-hfr=off;video-hfr-values=60,90,off;video-size=1920x1088;video-size-values=1920x1088,1920x1080,1280x720,960x544,864x480,800x480,768x432,720x480,640x480,480x320,352x288,320x240,176x144;video-slow-motion-supported=true;video-snapshot-supported=true;video-stabilization=false;video-stabilization-supported=true;wb-ct-step=10;whitebalance=auto;whitebalance-values=auto,incandescent,fluorescent,warm-fluorescent,daylight,cloudy-daylight,twilight,shade;zoe-support-mode=auto;zoe-supported=true;zoom=0;zoom-ratios=100,102,104,107,109,112,114,117,120,123,125,128,131,135,138,141,144,148,151,155,158,162,166,170,174,178,182,186,190,195,200,204,209,214,219,224,229,235,240,246,251,257,263,270,276,282,289,296,303,310,317,324,332,340,348,356,364,373,381,390;zoom-supported=true;zsl=on;zsl-values=off,on;";
return params;
}
static void camera_put_parameters(struct camera_device *device, char *params)
{
ALOGV("%s->%08X->%08X", __FUNCTION__, (uintptr_t)device,
(uintptr_t)(((wrapper_camera_device_t*)device)->vendor));
if (params)
free(params);
}
static int camera_send_command(struct camera_device *device,
int32_t cmd, int32_t arg1, int32_t arg2)
{
ALOGV("%s->%08X->%08X", __FUNCTION__, (uintptr_t)device,
(uintptr_t)(((wrapper_camera_device_t*)device)->vendor));
if (!device)
return -EINVAL;
return VENDOR_CALL(device, send_command, cmd, arg1, arg2);
}
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)
return;
VENDOR_CALL(device, release);
}
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)
return -EINVAL;
return VENDOR_CALL(device, dump, fd);
}
extern "C" void heaptracker_free_leaked_memory(void);
static int camera_device_close(hw_device_t *device)
{
int ret = 0;
wrapper_camera_device_t *wrapper_dev = NULL;
ALOGV("%s", __FUNCTION__);
android::Mutex::Autolock lock(gCameraWrapperLock);
if (!device) {
ret = -EINVAL;
goto done;
}
for (int i = 0; i < camera_get_number_of_cameras(); i++) {
if (fixed_set_params[i])
free(fixed_set_params[i]);
}
wrapper_dev = (wrapper_camera_device_t*) device;
wrapper_dev->vendor->common.close((hw_device_t*)wrapper_dev->vendor);
if (wrapper_dev->base.ops)
free(wrapper_dev->base.ops);
free(wrapper_dev);
done:
#ifdef HEAPTRACKER
heaptracker_free_leaked_memory();
#endif
return ret;
}
/*******************************************************************
* implementation of camera_module functions
*******************************************************************/
/* open device handle to one of the cameras
*
* assume camera service will keep singleton of each camera
* so this function will always only be called once per camera instance
*/
static int camera_device_open(const hw_module_t *module, const char *name,
hw_device_t **device)
{
int rv = 0;
int num_cameras = 0;
int cameraid;
wrapper_camera_device_t *camera_device = NULL;
camera_device_ops_t *camera_ops = NULL;
android::Mutex::Autolock lock(gCameraWrapperLock);
ALOGV("%s", __FUNCTION__);
if (name != NULL) {
if (check_vendor_module())
return -EINVAL;
cameraid = atoi(name);
num_cameras = gVendorModule->get_number_of_cameras();
fixed_set_params = (char **) malloc(sizeof(char *) * num_cameras);
if (!fixed_set_params) {
ALOGE("parameter memory allocation fail");
rv = -ENOMEM;
goto fail;
}
memset(fixed_set_params, 0, sizeof(char *) * num_cameras);
if (cameraid > num_cameras) {
ALOGE("camera service provided cameraid out of bounds, "
"cameraid = %d, num supported = %d",
cameraid, num_cameras);
rv = -EINVAL;
goto fail;
}
camera_device = (wrapper_camera_device_t*)malloc(sizeof(*camera_device));
if (!camera_device) {
ALOGE("camera_device allocation fail");
rv = -ENOMEM;
goto fail;
}
memset(camera_device, 0, sizeof(*camera_device));
camera_device->id = cameraid;
rv = gVendorModule->common.methods->open(
(const hw_module_t*)gVendorModule, name,
(hw_device_t**)&(camera_device->vendor));
if (rv) {
ALOGE("vendor camera open fail");
goto fail;
}
ALOGV("%s: got vendor camera device 0x%08X",
__FUNCTION__, (uintptr_t)(camera_device->vendor));
camera_ops = (camera_device_ops_t*)malloc(sizeof(*camera_ops));
if (!camera_ops) {
ALOGE("camera_ops allocation fail");
rv = -ENOMEM;
goto fail;
}
memset(camera_ops, 0, sizeof(*camera_ops));
camera_device->base.common.tag = HARDWARE_DEVICE_TAG;
camera_device->base.common.version = CAMERA_DEVICE_API_VERSION_1_0;
camera_device->base.common.module = (hw_module_t *)(module);
camera_device->base.common.close = camera_device_close;
camera_device->base.ops = camera_ops;
camera_ops->set_preview_window = camera_set_preview_window;
camera_ops->set_callbacks = camera_set_callbacks;
camera_ops->enable_msg_type = camera_enable_msg_type;
camera_ops->disable_msg_type = camera_disable_msg_type;
camera_ops->msg_type_enabled = camera_msg_type_enabled;
camera_ops->start_preview = camera_start_preview;
camera_ops->stop_preview = camera_stop_preview;
camera_ops->preview_enabled = camera_preview_enabled;
camera_ops->store_meta_data_in_buffers = camera_store_meta_data_in_buffers;
camera_ops->start_recording = camera_start_recording;
camera_ops->stop_recording = camera_stop_recording;
camera_ops->recording_enabled = camera_recording_enabled;
camera_ops->release_recording_frame = camera_release_recording_frame;
camera_ops->auto_focus = camera_auto_focus;
camera_ops->cancel_auto_focus = camera_cancel_auto_focus;
camera_ops->take_picture = camera_take_picture;
camera_ops->cancel_picture = camera_cancel_picture;
camera_ops->set_parameters = camera_set_parameters;
camera_ops->get_parameters = camera_get_parameters;
camera_ops->put_parameters = camera_put_parameters;
camera_ops->send_command = camera_send_command;
camera_ops->release = camera_release;
camera_ops->dump = camera_dump;
*device = &camera_device->base.common;
}
return rv;
fail:
if (camera_device) {
free(camera_device);
camera_device = NULL;
}
if (camera_ops) {
free(camera_ops);
camera_ops = NULL;
}
*device = NULL;
return rv;
}
static int camera_get_number_of_cameras(void)
{
ALOGV("%s", __FUNCTION__);
if (check_vendor_module())
return 0;
return gVendorModule->get_number_of_cameras();
}
static int camera_get_camera_info(int camera_id, struct camera_info *info)
{
ALOGV("%s", __FUNCTION__);
if (check_vendor_module())
return 0;
return gVendorModule->get_camera_info(camera_id, info);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment