本文主要是介绍Android Camera从Camera HAL1到Camera HAL3的过渡(已更新到Android6.0 HAL3.3),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
1. Android系统中Camera模块版本号的历史演变进度
- /**
- * All module versions <= HARDWARE_MODULE_API_VERSION(1, 0xFF) must be treated
- * as CAMERA_MODULE_API_VERSION_1_0
- */
- #define CAMERA_MODULE_API_VERSION_1_0 HARDWARE_MODULE_API_VERSION(1, 0)
- #define CAMERA_MODULE_API_VERSION_2_0 HARDWARE_MODULE_API_VERSION(2, 0)
- #define CAMERA_MODULE_API_VERSION_2_1 HARDWARE_MODULE_API_VERSION(2, 1)
- #define CAMERA_MODULE_API_VERSION_2_2 HARDWARE_MODULE_API_VERSION(2, 2)
- #define CAMERA_MODULE_API_VERSION_2_3 HARDWARE_MODULE_API_VERSION(2, 3)
- #define CAMERA_MODULE_API_VERSION_CURRENT CAMERA_MODULE_API_VERSION_2_3
- /**
- * All device versions <= HARDWARE_DEVICE_API_VERSION(1, 0xFF) must be treated
- * as CAMERA_DEVICE_API_VERSION_1_0
- */
- #define CAMERA_DEVICE_API_VERSION_1_0 HARDWARE_DEVICE_API_VERSION(1, 0)
- #define CAMERA_DEVICE_API_VERSION_2_0 HARDWARE_DEVICE_API_VERSION(2, 0)
- #define CAMERA_DEVICE_API_VERSION_2_1 HARDWARE_DEVICE_API_VERSION(2, 1)
- #define CAMERA_DEVICE_API_VERSION_3_0 HARDWARE_DEVICE_API_VERSION(3, 0)
- #define CAMERA_DEVICE_API_VERSION_3_1 HARDWARE_DEVICE_API_VERSION(3, 1)
- #define CAMERA_DEVICE_API_VERSION_3_2 HARDWARE_DEVICE_API_VERSION(3, 2)
- // Device version 3.2 is current, older HAL camera device versions are not
- // recommended for new devices.
- #define CAMERA_DEVICE_API_VERSION_CURRENT CAMERA_DEVICE_API_VERSION_3_2
对于Module API的区别主要在于2.1之后增加了set_callbacks接口,用于HAL检测当前module的工作状态,低版本的module不需要实现。
此外对于Module API版本号高于2.2时可支持get_vendor_tag_ops,Framework调用后课获取对厂商自己扩展定义的tag ops,便于操作厂商自己私有定义的camera所需的metadata信息,同理低版本的module不需要去实现该接口。
此外对于Module API 为2.3的版本增加了open_legacy接口:
- int (*open_legacy)(const struct hw_module_t* module, const char* id,
- uint32_t halVersion, struct hw_device_t** device);
他描述的是可以根据自己所选的device version,比如对于一个camera hal他支持并实现了CAMERA_DEVICE_API_VERSION_1_0和CAMERA_DEVICE_API_VERSION_3_2两种版本下的hal接口的实现,只有module api定义的是2.3的版本的话,在Framework中就可以通过对open_legacy()来指定hal_version,从而获取不同版本下的hal device的实现接口即hw_device_t。但目前来看,Android提供了该接口,却在Framework层都没有给出直接的调用,此外可见的hal module api2.3的版本,也都不会去实现该接口。说明hw_module_t所属的open目前依旧还是获取对camera device控制权的唯一路径与方法,也许对于一个camera device而言实现一种版本的接口就已经够用和麻烦的了。
2 Camera Client
关于CameraService相关的内容可以参考博文Android4.2.2 CameraService服务启动和应用端camera初始化记录来梳理整个Camera.so模块的加载与处理过程。每一个应用端的camera在connect到CameraService处会以一个Camera Client形式存在,该类继承并实现CameraService::Client的一个内部类,通过匿名的Binder服务与应用端进行交互。在CamerService启动并建立的时候,就会自动加载一个camera.xxx.so的模块,提取module相关的handle。
每当connect时,CameraService会执行一个getDeviceVersion的函数:
- int CameraService::getDeviceVersion(int cameraId, int* facing) {
- struct camera_info info;
- if (mModule->get_camera_info(cameraId, &info) != OK) {
- return -1;
- }
- int deviceVersion;
- if (mModule->common.module_api_version >= CAMERA_MODULE_API_VERSION_2_0) {
- deviceVersion = info.device_version;
- } else {
- deviceVersion = CAMERA_DEVICE_API_VERSION_1_0;
- }
- if (facing) {
- *facing = info.facing;
- }
- return deviceVersion;
- }
- case CAMERA_DEVICE_API_VERSION_1_0:
- client = new CameraClient(this, cameraClient,
- clientPackageName, cameraId,
- facing, callingPid, clientUid, getpid(), legacyMode);
- break;
- case CAMERA_DEVICE_API_VERSION_2_0:
- case CAMERA_DEVICE_API_VERSION_2_1:
- case CAMERA_DEVICE_API_VERSION_3_0:
- case CAMERA_DEVICE_API_VERSION_3_1:
- case CAMERA_DEVICE_API_VERSION_3_2:
- client = new Camera2Client(this, cameraClient,
- clientPackageName, cameraId,
- facing, callingPid, clientUid, getpid(), legacyMode);//CameraService端新建Camera2Client
- break;
对于Camera模块不同版本的DEVICE_API来说,其区别本质上就是在HAL所需要实现的接口不同,分别由以下三个版本的device接口:
- typedef struct camera_device {
- /**
- * camera_device.common.version must be in the range
- * HARDWARE_DEVICE_API_VERSION(0,0)-(1,FF). CAMERA_DEVICE_API_VERSION_1_0 is
- * recommended.
- */
- hw_device_t common;
- camera_device_ops_t *ops;
- void *priv;
- } camera_device_t;
- typedef struct camera2_device {
- /**
- * common.version must equal CAMERA_DEVICE_API_VERSION_2_0 to identify
- * this device as implementing version 2.0 of the camera device HAL.
- */
- hw_device_t common;
- camera2_device_ops_t *ops;
- void *priv;
- } camera2_device_t;
- typedef struct camera3_device {
- /**
- * common.version must equal CAMERA_DEVICE_API_VERSION_3_0 to identify this
- * device as implementing version 3.0 of the camera device HAL.
- *
- * Performance requirements:
- *
- * Camera open (common.module->common.methods->open) should return in 200ms, and must return
- * in 500ms.
- * Camera close (common.close) should return in 200ms, and must return in 500ms.
- *
- */
- hw_device_t common;
- camera3_device_ops_t *ops;
- void *priv;
- } camera3_device_t;
本质上来说,三个不同的版本都进行了全盘的升级,则重点则是归于在HAL中按照Google在Framework层定义的camerax_device_ops来实现该种接口,相关接口的实现形式在分析高通的HAL架构时再去分析。
3 Camera device
对于三个不同版本的hal层接口,在Camera的Framework中实现的过程略有不同,可以分为device1、device2、device3。
对于1.0版本的Device API,在创建CameraClient时,是以CameraHardwareInterface(可以认为是Camera Device1)来维护队整个对HAL层中Camera Device的控制权,即 camera_device_t归CameraHardwareInterface来调用。
对于2.0以上版本的Device API时,在创建Camera2Client时,会通过CameraDeviceFactory::createDevice来创建相应的Camera Device
- sp<CameraDeviceBase> CameraDeviceFactory::createDevice(int cameraId) {
- sp<CameraService> svc = sService.promote();
- if (svc == 0) {
- ALOGE("%s: No service registered", __FUNCTION__);
- return NULL;
- }
- int deviceVersion = svc->getDeviceVersion(cameraId, /*facing*/NULL);
- sp<CameraDeviceBase> device;
- switch (deviceVersion) {
- case CAMERA_DEVICE_API_VERSION_2_0:
- case CAMERA_DEVICE_API_VERSION_2_1:
- device = new Camera2Device(cameraId);
- break;
- case CAMERA_DEVICE_API_VERSION_3_0:
- case CAMERA_DEVICE_API_VERSION_3_1:
- case CAMERA_DEVICE_API_VERSION_3_2:
- device = new Camera3Device(cameraId);
- break;
- default:
- ALOGE("%s: Camera %d: Unknown HAL device version %d",
- __FUNCTION__, cameraId, deviceVersion);
- device = NULL;
- break;
- }
- ALOGV_IF(device != 0, "Created a new camera device for version %d",
- deviceVersion);
- return device;
- }
4.小结
Camera不断的过渡,也算是Android系统源码中,变化较大的一个部分了,虽然Framework层核心的CameraService、Camera等的实现逻辑都没有发生很大的变化,但和底层HAL的交互却有着质的变异。而目前主流的IC厂商都在朝着Camera HAL3.0的方向进行发展,所有有必要对3.0实现的原理与本质进行探究与学习。
附图:camera在Android Framework层中的变迁
100# define CAMERA_DEVICE_API_VERSION_2_1 HARDWARE_DEVICE_API_VERSION( 2, 1)
101# define CAMERA_DEVICE_API_VERSION_3_0 HARDWARE_DEVICE_API_VERSION( 3, 0)
这篇关于Android Camera从Camera HAL1到Camera HAL3的过渡(已更新到Android6.0 HAL3.3)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!