msm8226: switch to klte camerawrapper

This commit is contained in:
DarkJoker360 2018-09-16 20:47:33 +00:00
parent b550d5cc17
commit 367cc69e78
2 changed files with 170 additions and 106 deletions

View file

@ -2,17 +2,21 @@ LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_C_INCLUDES := \
system/media/camera/include \
frameworks/native/include/media/openmax
system/media/camera/include
LOCAL_SRC_FILES := \
CameraWrapper.cpp
LOCAL_STATIC_LIBRARIES := libbase libarect
LOCAL_SHARED_LIBRARIES := \
libhardware liblog libcamera_client libutils libcutils libgui libsensor
libhardware liblog libcamera_client libutils libcutils libdl \
android.hidl.token@1.0-utils \
android.hardware.graphics.bufferqueue@1.0
LOCAL_MODULE_PATH := $(TARGET_OUT_VENDOR_SHARED_LIBRARIES)/hw
LOCAL_MODULE := camera.msm8226
LOCAL_HEADER_LIBRARIES := libnativebase_headers
LOCAL_MODULE := camera.$(TARGET_BOARD_PLATFORM)
LOCAL_MODULE_RELATIVE_PATH := hw
LOCAL_MODULE_TAGS := optional
LOCAL_PROPRIETARY_MODULE := true

View file

@ -1,6 +1,5 @@
/*
* Copyright (C) 2016 The CyanogenMod Project
* Copyright (C) 2018 The LineageOS Project
* Copyright (C) 2012-2016, 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.
@ -19,25 +18,31 @@
* @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>
#include <camera/Camera.h>
#include <cutils/log.h>
#include <hardware/camera.h>
#include <hardware/hardware.h>
#include <utils/String8.h>
#include <utils/threads.h>
#include "CameraWrapper.h"
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";
static const char KEY_PHASE_AF[] = "phase-af";
static const char KEY_DYNAMIC_RANGE_CONTROL[] = "dynamic-range-control";
static const char KEY_QC_RT_HDR[] = "rt-hdr";
static const char KEY_QC_VIDEO_HDR[] = "video-hdr";
static const char KEY_QC_VIDEO_HDR_VALUES[] = "video-hdr-values";
static const char PIXEL_FORMAT_YUV420SP_NV21E[] = "yuv420sp-nv21e";
#define BACK_CAMERA 0
#define FRONT_CAMERA 1
#define BACK_CAMERA_ID 0
#define FRONT_CAMERA_ID 1
using namespace android;
@ -58,35 +63,33 @@ 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
.open = camera_device_open,
};
camera_module_t HAL_MODULE_INFO_SYM = {
.common = {
.tag = HARDWARE_MODULE_TAG,
.version_major = 1,
.version_minor = 0,
.id = CAMERA_HARDWARE_MODULE_ID,
.name = "msm8226 Camera Wrapper",
.author = "The CyanogenMod Project",
.methods = &camera_module_methods,
.dso = NULL, /* remove compilation warnings */
.reserved = {0}, /* remove compilation warnings */
.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 = "MSM8974 Camera Wrapper",
.author = "The CyanogenMod Project",
.methods = &camera_module_methods,
.dso = NULL,
.reserved = {0},
},
.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 */
.open_legacy = NULL, /* remove compilation warnings */
.set_torch_mode = camera_set_torch_mode,
.init = NULL, /* remove compilation warnings */
.reserved = {0}, /* remove compilation warnings */
.set_callbacks = NULL,
.get_vendor_tag_ops = NULL,
.open_legacy = NULL,
.set_torch_mode = NULL,
.init = NULL,
.reserved = {0},
};
typedef struct wrapper_camera_device {
camera_device_t base;
int camera_released;
int id;
camera_device_t *vendor;
} wrapper_camera_device_t;
@ -96,11 +99,11 @@ typedef struct wrapper_camera_device {
__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()
{
int rv = 0;
int rv;
ALOGV("%s", __FUNCTION__);
if (gVendorModule)
@ -109,34 +112,71 @@ static int check_vendor_module()
rv = hw_get_module_by_class("camera", "vendor",
(const hw_module_t**)&gVendorModule);
if (rv)
ALOGE("failed to open vendor camera module");
ALOGE("Failed to open vendor camera module %d", rv);
return rv;
}
static char *camera_fixup_getparams(int id, const char *settings)
#define KEY_VIDEO_HFR_VALUES "video-hfr-values"
// nv12-venus is needed for blobs, but
// framework has no idea what it is
#define PIXEL_FORMAT_NV12_VENUS "nv12-venus"
static bool is_4k_video(CameraParameters &params) {
int video_width, video_height;
params.getVideoSize(&video_width, &video_height);
ALOGV("%s : VideoSize is %x", __FUNCTION__, video_width * video_height);
return video_width * video_height == 3840 * 2160;
}
static char *camera_fixup_getparams(int __attribute__((unused)) id,
const char *settings)
{
CameraParameters params;
params.unflatten(String8(settings));
#if !LOG_NDEBUG
ALOGV("%s: original parameters:", __FUNCTION__);
params.dump();
#endif
// fix params here
params.set(android::CameraParameters::KEY_EXPOSURE_COMPENSATION_STEP, "0.5");
params.set(android::CameraParameters::KEY_MIN_EXPOSURE_COMPENSATION, "-2");
params.set(android::CameraParameters::KEY_MAX_EXPOSURE_COMPENSATION, "2");
const char *recordHint = params.get(CameraParameters::KEY_RECORDING_HINT);
bool videoMode = recordHint ? !strcmp(recordHint, "true") : false;
params.set(android::CameraParameters::KEY_SUPPORTED_PREVIEW_SIZES, "640x360,640x480,352x288,320x240,176x144");
//Hide nv12-venus from Android.
if (strcmp (params.getPreviewFormat(), PIXEL_FORMAT_NV12_VENUS) == 0)
params.setPreviewFormat(params.PIXEL_FORMAT_YUV420SP);
const char *videoSizeValues = params.get(
CameraParameters::KEY_SUPPORTED_VIDEO_SIZES);
if (videoSizeValues) {
char videoSizes[strlen(videoSizeValues) + 10 + 1];
sprintf(videoSizes, "3840x2160,%s", videoSizeValues);
params.set(CameraParameters::KEY_SUPPORTED_VIDEO_SIZES,
videoSizes);
}
/* 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);
}
/* Enforce video-snapshot-supported to true */
params.set(android::CameraParameters::KEY_VIDEO_SNAPSHOT_SUPPORTED, "true");
if (videoMode) {
params.set(CameraParameters::KEY_VIDEO_SNAPSHOT_SUPPORTED, "true");
}
#if !LOG_NDEBUG
ALOGV("%s: fixed parameters:", __FUNCTION__);
/* advertise video HDR mode support */
if (id == BACK_CAMERA_ID)
params.set(KEY_QC_VIDEO_HDR_VALUES, "on,off");
ALOGV("%s: Fixed parameters:", __FUNCTION__);
params.dump();
#endif
String8 strParams = params.flatten();
char *ret = strdup(strParams.string());
@ -149,18 +189,47 @@ static char *camera_fixup_setparams(int id, const char *settings)
CameraParameters params;
params.unflatten(String8(settings));
#if !LOG_NDEBUG
ALOGV("%s: original parameters:", __FUNCTION__);
params.dump();
#endif
params.set(android::CameraParameters::KEY_SUPPORTED_PREVIEW_SIZES, "640x360,640x480,528x432,352x288,320x240,176x144");
params.set(android::CameraParameters::KEY_PREVIEW_FPS_RANGE, "7500,30000");
bool wasTorch = false;
if (fixed_set_params[id]) {
/* When torch mode is switched off, it is important not to set ZSL, to
avoid a segmentation violation in libcameraservice.so. Hence, check
if the last call to setparams enabled torch mode */
CameraParameters old_params;
old_params.unflatten(String8(fixed_set_params[id]));
#if !LOG_NDEBUG
ALOGV("%s: fixed parameters:", __FUNCTION__);
const char *old_flashMode = old_params.get(CameraParameters::KEY_FLASH_MODE);
wasTorch = old_flashMode && !strcmp(old_flashMode, CameraParameters::FLASH_MODE_TORCH);
}
const char *recordingHint = params.get(CameraParameters::KEY_RECORDING_HINT);
bool isVideo = recordingHint && !strcmp(recordingHint, "true");
const char *flashMode = params.get(CameraParameters::KEY_FLASH_MODE);
bool isTorch = flashMode && !strcmp(flashMode, CameraParameters::FLASH_MODE_TORCH);
if (!isTorch && !wasTorch) {
if (isVideo) {
params.set(KEY_DIS, DIS_DISABLE);
params.set(KEY_ZSL, OFF);
} else {
params.set(KEY_ZSL, ON);
}
}
if (id == BACK_CAMERA_ID) {
params.set(KEY_PHASE_AF, ON);
params.set(KEY_DYNAMIC_RANGE_CONTROL, ON);
if (isVideo) {
/* need to translate video-hdr to rt-hdr */
const char *vhdr = params.get(KEY_QC_VIDEO_HDR);
params.set(KEY_QC_RT_HDR, vhdr && !strcmp(vhdr, "on") ? ON : OFF);
}
}
ALOGV("%s: Fixed parameters:", __FUNCTION__);
params.dump();
#endif
String8 strParams = params.flatten();
if (fixed_set_params[id])
@ -172,8 +241,10 @@ static char *camera_fixup_setparams(int id, const char *settings)
}
/*******************************************************************
* implementation of camera_device_ops functions
* Implementation of camera_device_ops functions
*******************************************************************/
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_preview_window(struct camera_device *device,
struct preview_stream_ops *window)
@ -318,6 +389,15 @@ 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));
CameraParameters parameters;
parameters.unflatten(String8(camera_get_parameters(device)));
if (is_4k_video(parameters)) {
ALOGV("%s : UHD detected, switching preview-format to nv12-venus", __FUNCTION__);
parameters.setPreviewFormat(PIXEL_FORMAT_NV12_VENUS);
camera_set_parameters(device, strdup(parameters.flatten().string()));
}
return VENDOR_CALL(device, start_recording);
}
@ -408,11 +488,9 @@ static int camera_set_parameters(struct camera_device *device,
ALOGV("%s->%08X->%08X", __FUNCTION__, (uintptr_t)device,
(uintptr_t)(((wrapper_camera_device_t*)device)->vendor));
char *tmp = NULL;
tmp = camera_fixup_setparams(CAMERA_ID(device), params);
char *tmp = camera_fixup_setparams(CAMERA_ID(device), params);
int ret = VENDOR_CALL(device, set_parameters, tmp);
return ret;
return VENDOR_CALL(device, set_parameters, tmp);
}
static char *camera_get_parameters(struct camera_device *device)
@ -426,12 +504,15 @@ static char *camera_get_parameters(struct camera_device *device)
char *params = VENDOR_CALL(device, get_parameters);
char *tmp = camera_fixup_getparams(CAMERA_ID(device), params);
VENDOR_CALL(device, put_parameters, params);
params = tmp;
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)
free(params);
@ -451,19 +532,13 @@ static int camera_send_command(struct camera_device *device,
static void camera_release(struct camera_device *device)
{
wrapper_camera_device_t* wrapper_dev = NULL;
if (!device)
return;
wrapper_dev = (wrapper_camera_device_t*) device;
ALOGV("%s->%08X->%08X", __FUNCTION__, (uintptr_t)device,
(uintptr_t)(((wrapper_camera_device_t*)device)->vendor));
VENDOR_CALL(device, release);
wrapper_dev->camera_released = true;
}
static int camera_dump(struct camera_device *device, int fd)
@ -500,19 +575,11 @@ static int camera_device_close(hw_device_t *device)
wrapper_dev = (wrapper_camera_device_t*) device;
if (!wrapper_dev->camera_released) {
ALOGI("%s: releasing camera device with id %d", __FUNCTION__,
wrapper_dev->id);
VENDOR_CALL(wrapper_dev, release);
wrapper_dev->camera_released = true;
}
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();
@ -521,12 +588,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
*/
@ -535,7 +603,7 @@ static int camera_device_open(const hw_module_t *module, const char *name,
{
int rv = 0;
int num_cameras = 0;
int cameraid;
int camera_id;
wrapper_camera_device_t *camera_device = NULL;
camera_device_ops_t *camera_ops = NULL;
@ -547,43 +615,43 @@ static int camera_device_open(const hw_module_t *module, const char *name,
if (check_vendor_module())
return -EINVAL;
cameraid = atoi(name);
camera_id = atoi(name);
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) {
ALOGE("parameter memory allocation fail");
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);
if (camera_id > num_cameras) {
ALOGE("Camera service provided camera_id out of bounds, "
"camera_id = %d, num supported = %d",
camera_id, num_cameras);
rv = -EINVAL;
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) {
ALOGE("camera_device allocation fail");
rv = -ENOMEM;
goto fail;
}
memset(camera_device, 0, sizeof(*camera_device));
camera_device->camera_released = false;
camera_device->id = cameraid;
camera_device->id = camera_id;
rv = gVendorModule->common.methods->open(
(const hw_module_t*)gVendorModule, name,
(hw_device_t**)&(camera_device->vendor));
if (rv) {
ALOGE("vendor camera open fail");
ALOGE("Vendor camera open 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));
camera_ops = (camera_device_ops_t*)malloc(sizeof(*camera_ops));
@ -596,7 +664,7 @@ static int camera_device_open(const hw_module_t *module, const char *name,
memset(camera_ops, 0, sizeof(*camera_ops));
camera_device->base.common.tag = HARDWARE_DEVICE_TAG;
camera_device->base.common.version = CAMERA_MODULE_API_VERSION_1_0;
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;
@ -658,11 +726,3 @@ static int camera_get_camera_info(int camera_id, struct camera_info *info)
return 0;
return gVendorModule->get_camera_info(camera_id, info);
}
static int camera_set_torch_mode(const char* camera_id, bool enabled)
{
ALOGV("%s", __FUNCTION__);
if (check_vendor_module())
return 0;
return gVendorModule->set_torch_mode(camera_id, enabled);
}