android_device_samsung_mond.../camera/CameraWrapper.cpp

639 lines
18 KiB
C++
Raw Normal View History

2021-03-05 09:06:27 +01:00
/*
* Copyright (C) 2012-2016, The CyanogenMod Project
2021-03-05 09:06:27 +01:00
*
* 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_TAG "CameraWrapper"
#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>
2021-03-05 09:06:27 +01:00
static const char KEY_DIS[] = "dis";
static const char DIS_DISABLE[] = "disable";
static const char KEY_ZSL[] = "zsl";
static const char ON[] = "on";
static const char OFF[] = "off";
using namespace android;
2021-03-05 09:06:27 +01:00
static Mutex gCameraWrapperLock;
2021-03-05 09:06:27 +01:00
static camera_module_t *gVendorModule = 0;
static camera_notify_callback gUserNotifyCb = NULL;
static camera_data_callback gUserDataCb = NULL;
static camera_data_timestamp_callback gUserDataCbTimestamp = NULL;
static camera_request_memory gUserGetMemory = NULL;
static void *gUserCameraDevice = NULL;
static char **fixed_set_params = NULL;
static int camera_device_open(const hw_module_t *module, const char *name,
hw_device_t **device);
2021-03-05 09:06:27 +01:00
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,
2021-03-05 09:06:27 +01:00
};
camera_module_t HAL_MODULE_INFO_SYM = {
.common = {
.tag = HARDWARE_MODULE_TAG,
2021-03-05 09:06:27 +01:00
.module_api_version = CAMERA_MODULE_API_VERSION_1_0,
.hal_api_version = HARDWARE_HAL_API_VERSION,
.id = CAMERA_HARDWARE_MODULE_ID,
.name = "MSM8974 Camera Wrapper",
.author = "The CyanogenMod Project",
.methods = &camera_module_methods,
.dso = NULL,
.reserved = {0},
2021-03-05 09:06:27 +01:00
},
.get_number_of_cameras = camera_get_number_of_cameras,
.get_camera_info = camera_get_camera_info,
.set_callbacks = NULL,
.get_vendor_tag_ops = NULL,
.open_legacy = NULL,
.set_torch_mode = NULL,
.init = NULL,
.reserved = {0},
2021-03-05 09:06:27 +01:00
};
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)
2021-03-05 09:06:27 +01:00
static int check_vendor_module()
{
int rv;
ALOGV("%s", __FUNCTION__);
2021-03-05 09:06:27 +01:00
if (gVendorModule)
2021-03-05 09:06:27 +01:00
return 0;
rv = hw_get_module_by_class("camera", "vendor",
(const hw_module_t**)&gVendorModule);
2021-03-05 09:06:27 +01:00
if (rv)
ALOGE("Failed to open vendor camera module %d", rv);
2021-03-05 09:06:27 +01:00
return rv;
}
#define KEY_VIDEO_HFR_VALUES "video-hfr-values"
static char *camera_fixup_getparams(int __attribute__((unused)) id,
const char *settings)
2021-03-05 09:06:27 +01:00
{
CameraParameters params;
params.unflatten(String8(settings));
2021-03-05 09:06:27 +01:00
ALOGV("%s: original parameters:", __FUNCTION__);
params.dump();
/* If the vendor has HFR values but doesn't also expose that
* this can be turned off, fixup the params to tell the Camera
* that it really is okay to turn it off.
*/
const char *hfrModeValues = params.get(KEY_VIDEO_HFR_VALUES);
if (hfrModeValues && !strstr(hfrModeValues, "off")) {
char hfrModes[strlen(hfrModeValues) + 4 + 1];
sprintf(hfrModes, "%s,off", hfrModeValues);
params.set(KEY_VIDEO_HFR_VALUES, hfrModes);
2021-03-05 09:06:27 +01:00
}
ALOGV("%s: Fixed parameters:", __FUNCTION__);
params.dump();
2021-03-05 09:06:27 +01:00
String8 strParams = params.flatten();
2021-03-05 09:06:27 +01:00
char *ret = strdup(strParams.string());
return ret;
}
static char *camera_fixup_setparams(int id, const char *settings)
2021-03-05 09:06:27 +01:00
{
CameraParameters params;
params.unflatten(String8(settings));
2021-03-05 09:06:27 +01:00
ALOGV("%s: original parameters:", __FUNCTION__);
params.dump();
ALOGV("%s: Fixed parameters:", __FUNCTION__);
params.dump();
2021-03-05 09:06:27 +01:00
String8 strParams = params.flatten();
2021-03-05 09:06:27 +01:00
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
2021-03-05 09:06:27 +01:00
*******************************************************************/
static char *camera_get_parameters(struct camera_device *device);
static int camera_set_parameters(struct camera_device *device, const char *params);
2021-03-05 09:06:27 +01:00
static int camera_set_preview_window(struct camera_device *device,
2021-03-05 09:06:27 +01:00
struct preview_stream_ops *window)
{
if (!device)
2021-03-05 09:06:27 +01:00
return -EINVAL;
ALOGV("%s->%08X->%08X", __FUNCTION__, (uintptr_t)device,
(uintptr_t)(((wrapper_camera_device_t*)device)->vendor));
2021-03-05 09:06:27 +01:00
return VENDOR_CALL(device, set_preview_window, window);
}
void camera_notify_cb(int32_t msg_type, int32_t ext1, int32_t ext2, void *user) {
gUserNotifyCb(msg_type, ext1, ext2, gUserCameraDevice);
}
void camera_data_cb(int32_t msg_type, const camera_memory_t *data, unsigned int index,
camera_frame_metadata_t *metadata, void *user) {
gUserDataCb(msg_type, data, index, metadata, gUserCameraDevice);
}
void camera_data_cb_timestamp(nsecs_t timestamp, int32_t msg_type,
const camera_memory_t *data, unsigned index, void *user) {
gUserDataCbTimestamp(timestamp, msg_type, data, index, gUserCameraDevice);
}
camera_memory_t* camera_get_memory(int fd, size_t buf_size,
uint_t num_bufs, void *user) {
return gUserGetMemory(fd, buf_size, num_bufs, gUserCameraDevice);
}
static void camera_set_callbacks(struct camera_device *device,
2021-03-05 09:06:27 +01:00
camera_notify_callback notify_cb,
camera_data_callback data_cb,
camera_data_timestamp_callback data_cb_timestamp,
camera_request_memory get_memory,
void *user)
{
if (!device)
2021-03-05 09:06:27 +01:00
return;
ALOGV("%s->%08X->%08X", __FUNCTION__, (uintptr_t)device,
(uintptr_t)(((wrapper_camera_device_t*)device)->vendor));
2021-03-05 09:06:27 +01:00
gUserNotifyCb = notify_cb;
gUserDataCb = data_cb;
gUserDataCbTimestamp = data_cb_timestamp;
gUserGetMemory = get_memory;
gUserCameraDevice = user;
VENDOR_CALL(device, set_callbacks, camera_notify_cb, camera_data_cb,
camera_data_cb_timestamp, camera_get_memory, user);
}
static void camera_enable_msg_type(struct camera_device *device,
int32_t msg_type)
2021-03-05 09:06:27 +01:00
{
if (!device)
2021-03-05 09:06:27 +01:00
return;
ALOGV("%s->%08X->%08X", __FUNCTION__, (uintptr_t)device,
(uintptr_t)(((wrapper_camera_device_t*)device)->vendor));
2021-03-05 09:06:27 +01:00
VENDOR_CALL(device, enable_msg_type, msg_type);
}
static void camera_disable_msg_type(struct camera_device *device,
int32_t msg_type)
2021-03-05 09:06:27 +01:00
{
if (!device)
2021-03-05 09:06:27 +01:00
return;
ALOGV("%s->%08X->%08X", __FUNCTION__, (uintptr_t)device,
(uintptr_t)(((wrapper_camera_device_t*)device)->vendor));
2021-03-05 09:06:27 +01:00
VENDOR_CALL(device, disable_msg_type, msg_type);
}
static int camera_msg_type_enabled(struct camera_device *device,
int32_t msg_type)
2021-03-05 09:06:27 +01:00
{
if (!device)
2021-03-05 09:06:27 +01:00
return 0;
ALOGV("%s->%08X->%08X", __FUNCTION__, (uintptr_t)device,
(uintptr_t)(((wrapper_camera_device_t*)device)->vendor));
2021-03-05 09:06:27 +01:00
return VENDOR_CALL(device, msg_type_enabled, msg_type);
}
static int camera_start_preview(struct camera_device *device)
2021-03-05 09:06:27 +01:00
{
if (!device)
2021-03-05 09:06:27 +01:00
return -EINVAL;
ALOGV("%s->%08X->%08X", __FUNCTION__, (uintptr_t)device,
(uintptr_t)(((wrapper_camera_device_t*)device)->vendor));
2021-03-05 09:06:27 +01:00
return VENDOR_CALL(device, start_preview);
}
static void camera_stop_preview(struct camera_device *device)
2021-03-05 09:06:27 +01:00
{
if (!device)
2021-03-05 09:06:27 +01:00
return;
ALOGV("%s->%08X->%08X", __FUNCTION__, (uintptr_t)device,
(uintptr_t)(((wrapper_camera_device_t*)device)->vendor));
2021-03-05 09:06:27 +01:00
VENDOR_CALL(device, stop_preview);
}
static int camera_preview_enabled(struct camera_device *device)
2021-03-05 09:06:27 +01:00
{
if (!device)
2021-03-05 09:06:27 +01:00
return -EINVAL;
ALOGV("%s->%08X->%08X", __FUNCTION__, (uintptr_t)device,
(uintptr_t)(((wrapper_camera_device_t*)device)->vendor));
2021-03-05 09:06:27 +01:00
return VENDOR_CALL(device, preview_enabled);
}
static int camera_store_meta_data_in_buffers(struct camera_device *device,
int enable)
2021-03-05 09:06:27 +01:00
{
if (!device)
2021-03-05 09:06:27 +01:00
return -EINVAL;
ALOGV("%s->%08X->%08X", __FUNCTION__, (uintptr_t)device,
(uintptr_t)(((wrapper_camera_device_t*)device)->vendor));
2021-03-05 09:06:27 +01:00
return VENDOR_CALL(device, store_meta_data_in_buffers, enable);
}
static int camera_start_recording(struct camera_device *device)
2021-03-05 09:06:27 +01:00
{
if (!device)
2021-03-05 09:06:27 +01:00
return EINVAL;
ALOGV("%s->%08X->%08X", __FUNCTION__, (uintptr_t)device,
(uintptr_t)(((wrapper_camera_device_t*)device)->vendor));
2021-03-05 09:06:27 +01:00
return VENDOR_CALL(device, start_recording);
}
static void camera_stop_recording(struct camera_device *device)
2021-03-05 09:06:27 +01:00
{
if (!device)
2021-03-05 09:06:27 +01:00
return;
ALOGV("%s->%08X->%08X", __FUNCTION__, (uintptr_t)device,
(uintptr_t)(((wrapper_camera_device_t*)device)->vendor));
2021-03-05 09:06:27 +01:00
VENDOR_CALL(device, stop_recording);
}
static int camera_recording_enabled(struct camera_device *device)
2021-03-05 09:06:27 +01:00
{
if (!device)
2021-03-05 09:06:27 +01:00
return -EINVAL;
ALOGV("%s->%08X->%08X", __FUNCTION__, (uintptr_t)device,
(uintptr_t)(((wrapper_camera_device_t*)device)->vendor));
2021-03-05 09:06:27 +01:00
return VENDOR_CALL(device, recording_enabled);
}
static void camera_release_recording_frame(struct camera_device *device,
2021-03-05 09:06:27 +01:00
const void *opaque)
{
if (!device)
2021-03-05 09:06:27 +01:00
return;
ALOGV("%s->%08X->%08X", __FUNCTION__, (uintptr_t)device,
(uintptr_t)(((wrapper_camera_device_t*)device)->vendor));
2021-03-05 09:06:27 +01:00
VENDOR_CALL(device, release_recording_frame, opaque);
}
static int camera_auto_focus(struct camera_device *device)
2021-03-05 09:06:27 +01:00
{
if (!device)
2021-03-05 09:06:27 +01:00
return -EINVAL;
ALOGV("%s->%08X->%08X", __FUNCTION__, (uintptr_t)device,
(uintptr_t)(((wrapper_camera_device_t*)device)->vendor));
2021-03-05 09:06:27 +01:00
return VENDOR_CALL(device, auto_focus);
}
static int camera_cancel_auto_focus(struct camera_device *device)
2021-03-05 09:06:27 +01:00
{
if (!device)
2021-03-05 09:06:27 +01:00
return -EINVAL;
ALOGV("%s->%08X->%08X", __FUNCTION__, (uintptr_t)device,
(uintptr_t)(((wrapper_camera_device_t*)device)->vendor));
2021-03-05 09:06:27 +01:00
return VENDOR_CALL(device, cancel_auto_focus);
}
static int camera_take_picture(struct camera_device *device)
2021-03-05 09:06:27 +01:00
{
if (!device)
2021-03-05 09:06:27 +01:00
return -EINVAL;
ALOGV("%s->%08X->%08X", __FUNCTION__, (uintptr_t)device,
(uintptr_t)(((wrapper_camera_device_t*)device)->vendor));
2021-03-05 09:06:27 +01:00
return VENDOR_CALL(device, take_picture);
}
static int camera_cancel_picture(struct camera_device *device)
2021-03-05 09:06:27 +01:00
{
if (!device)
2021-03-05 09:06:27 +01:00
return -EINVAL;
ALOGV("%s->%08X->%08X", __FUNCTION__, (uintptr_t)device,
(uintptr_t)(((wrapper_camera_device_t*)device)->vendor));
2021-03-05 09:06:27 +01:00
return VENDOR_CALL(device, cancel_picture);
}
static int camera_set_parameters(struct camera_device *device,
const char *params)
2021-03-05 09:06:27 +01:00
{
if (!device)
2021-03-05 09:06:27 +01:00
return -EINVAL;
ALOGV("%s->%08X->%08X", __FUNCTION__, (uintptr_t)device,
(uintptr_t)(((wrapper_camera_device_t*)device)->vendor));
2021-03-05 09:06:27 +01:00
char *tmp = camera_fixup_setparams(CAMERA_ID(device), params);
return VENDOR_CALL(device, set_parameters, tmp);
2021-03-05 09:06:27 +01:00
}
static char *camera_get_parameters(struct camera_device *device)
2021-03-05 09:06:27 +01:00
{
if (!device)
2021-03-05 09:06:27 +01:00
return NULL;
ALOGV("%s->%08X->%08X", __FUNCTION__, (uintptr_t)device,
(uintptr_t)(((wrapper_camera_device_t*)device)->vendor));
char *params = VENDOR_CALL(device, get_parameters);
char *tmp = camera_fixup_getparams(CAMERA_ID(device), params);
2021-03-05 09:06:27 +01:00
VENDOR_CALL(device, put_parameters, params);
params = tmp;
return params;
}
static void camera_put_parameters(__unused struct camera_device *device,
char *params)
2021-03-05 09:06:27 +01:00
{
if (params)
2021-03-05 09:06:27 +01:00
free(params);
}
static int camera_send_command(struct camera_device *device,
2021-03-05 09:06:27 +01:00
int32_t cmd, int32_t arg1, int32_t arg2)
{
if (!device)
2021-03-05 09:06:27 +01:00
return -EINVAL;
ALOGV("%s->%08X->%08X", __FUNCTION__, (uintptr_t)device,
(uintptr_t)(((wrapper_camera_device_t*)device)->vendor));
2021-03-05 09:06:27 +01:00
return VENDOR_CALL(device, send_command, cmd, arg1, arg2);
}
static void camera_release(struct camera_device *device)
2021-03-05 09:06:27 +01:00
{
if (!device)
2021-03-05 09:06:27 +01:00
return;
ALOGV("%s->%08X->%08X", __FUNCTION__, (uintptr_t)device,
(uintptr_t)(((wrapper_camera_device_t*)device)->vendor));
2021-03-05 09:06:27 +01:00
VENDOR_CALL(device, release);
}
static int camera_dump(struct camera_device *device, int fd)
2021-03-05 09:06:27 +01:00
{
if (!device)
2021-03-05 09:06:27 +01:00
return -EINVAL;
ALOGV("%s->%08X->%08X", __FUNCTION__, (uintptr_t)device,
(uintptr_t)(((wrapper_camera_device_t*)device)->vendor));
2021-03-05 09:06:27 +01:00
return VENDOR_CALL(device, dump, fd);
}
extern "C" void heaptracker_free_leaked_memory(void);
static int camera_device_close(hw_device_t *device)
2021-03-05 09:06:27 +01:00
{
int ret = 0;
wrapper_camera_device_t *wrapper_dev = NULL;
ALOGV("%s", __FUNCTION__);
2021-03-05 09:06:27 +01:00
Mutex::Autolock lock(gCameraWrapperLock);
2021-03-05 09:06:27 +01:00
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
2021-03-05 09:06:27 +01:00
*******************************************************************/
/*
* Open device handle to one of the cameras
2021-03-05 09:06:27 +01:00
*
* Assume camera service will keep singleton of each camera
2021-03-05 09:06:27 +01:00
* 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)
2021-03-05 09:06:27 +01:00
{
int rv = 0;
int num_cameras = 0;
int camera_id;
wrapper_camera_device_t *camera_device = NULL;
camera_device_ops_t *camera_ops = NULL;
2021-03-05 09:06:27 +01:00
Mutex::Autolock lock(gCameraWrapperLock);
2021-03-05 09:06:27 +01:00
ALOGV("%s", __FUNCTION__);
2021-03-05 09:06:27 +01:00
if (name != NULL) {
if (check_vendor_module())
return -EINVAL;
camera_id = atoi(name);
2021-03-05 09:06:27 +01:00
num_cameras = gVendorModule->get_number_of_cameras();
fixed_set_params = (char **) malloc(sizeof(char *) *num_cameras);
2021-03-05 09:06:27 +01:00
if (!fixed_set_params) {
ALOGE("Parameter memory allocation fail");
2021-03-05 09:06:27 +01:00
rv = -ENOMEM;
goto fail;
}
memset(fixed_set_params, 0, sizeof(char *) * num_cameras);
if (camera_id > num_cameras) {
ALOGE("Camera service provided camera_id out of bounds, "
"camera_id = %d, num supported = %d",
camera_id, num_cameras);
2021-03-05 09:06:27 +01:00
rv = -EINVAL;
goto fail;
}
camera_device = (wrapper_camera_device_t*)
malloc(sizeof(*camera_device));
if (!camera_device) {
2021-03-05 09:06:27 +01:00
ALOGE("camera_device allocation fail");
rv = -ENOMEM;
goto fail;
}
memset(camera_device, 0, sizeof(*camera_device));
camera_device->id = camera_id;
2021-03-05 09:06:27 +01:00
rv = gVendorModule->common.methods->open(
(const hw_module_t*)gVendorModule, name,
(hw_device_t**)&(camera_device->vendor));
2021-03-05 09:06:27 +01:00
if (rv) {
ALOGE("Vendor camera open fail");
2021-03-05 09:06:27 +01:00
goto fail;
}
ALOGV("%s: Got vendor camera device 0x%08X",
__FUNCTION__, (uintptr_t)(camera_device->vendor));
2021-03-05 09:06:27 +01:00
camera_ops = (camera_device_ops_t*)malloc(sizeof(*camera_ops));
if (!camera_ops) {
2021-03-05 09:06:27 +01:00
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;
2021-03-05 09:06:27 +01:00
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) {
2021-03-05 09:06:27 +01:00
free(camera_device);
camera_device = NULL;
}
if (camera_ops) {
2021-03-05 09:06:27 +01:00
free(camera_ops);
camera_ops = NULL;
}
*device = NULL;
return rv;
}
static int camera_get_number_of_cameras(void)
2021-03-05 09:06:27 +01:00
{
ALOGV("%s", __FUNCTION__);
2021-03-05 09:06:27 +01:00
if (check_vendor_module())
return 0;
return gVendorModule->get_number_of_cameras();
2021-03-05 09:06:27 +01:00
}
static int camera_get_camera_info(int camera_id, struct camera_info *info)
2021-03-05 09:06:27 +01:00
{
ALOGV("%s", __FUNCTION__);
2021-03-05 09:06:27 +01:00
if (check_vendor_module())
return 0;
return gVendorModule->get_camera_info(camera_id, info);
}