cameraserver 736 1 274664 69564 binder_thread_read f3de10cc S android.hardware.camera.provider@2.4-serviceandroid
其中的pid是736,說明camera provider進程啓動的時機比較早,並且權限組是 cameraserverapi
手機上運行的android.hardware.camera.provider@2.4-service進程是支持camera運行的重要進程。數據結構
camera架構.jpg架構
上面這張圖比較清楚的表現了camera provider進程在camera架構中位置,做爲承上啓下的部分,和cameraserver進程和底層的驅動交互,camera provider進程很是重要,camera HAL層幾乎所有運行在camera provider進程中完成。app
android.hardware.camera.provider@2.4-service在手機啓動的時候就會啓動起來,下面總體分析一下android.hardware.camera.provider@2.4-service進程的啓動過程。ide
首先看下camera provider所在源碼中的位置:hardware/interfaces/camera/provider/函數
cameraprovider代碼目錄.jpgui
能夠看出來在hardware/interfaces/camera/provider/2.4/default/ 下面有幾個rc文件,Android初始化就是執行這些rc文件,這裏執行的是android.hardware.camera.provider@2.4-service.rc文件,看看其中的執行代碼。spa
service vendor.camera-provider-2-4 /vendor/bin/hw/android.hardware.camera.provider@2.4-service class hal user cameraserver group audio camera input drmrpc ioprio rt 4 capabilities SYS_NICE writepid /dev/cpuset/camera-daemon/tasks /dev/stune/top-app/tasks
第一行就看到了啓動一個 /vendor/bin/hw/android.hardware.camera.provider@2.4-service 進程。3d
下面列出camera provider進程的啓動流程,你們能夠先看一下。
camera provider進程啓動流程.jpg
service.cpp : hardware/interfaces/camera/provider/2.4/default/service.cpp
CameraProvider : hardware/interfaces/camera/provider/2.4/default/CameraProvider.cpp
hardware.c : hardware/libhardware/hardware.c
CameraModule : hardware/interfaces/camera/common/1.0/default/CameraModule.cpp
QCamera2Factory : hardware/qcom/camera/QCamera2Factory.cpp
QCameraFlash : hardware/qcom/camera/QCamera2/util/QCameraFlash.cpp
QCamera3HardwareInterface : hardware/qcom/camera/QCamera2/HAL3/QCamera3HWI.cpp
這兒值得講一講的就是native中的函數指針的問題,從camera interfaces中將函數指針映射到camera hal層中。
主要從CameraModule中調用到QCamera2Factory中。期間使用了camera_module_t這個結構體。能夠看下 CameraModule::init函數。
int CameraModule::init() { ATRACE_CALL(); int res = OK; if (getModuleApiVersion() >= CAMERA_MODULE_API_VERSION_2_4 && mModule->init != NULL) { ATRACE_BEGIN("camera_module->init"); res = mModule->init(); ATRACE_END(); } mCameraInfoMap.setCapacity(getNumberOfCameras()); return res; }
這兒調用的mModule->init(); 這個mModule就是camera_module_t結構體對象,這時候翻看一下《Android Camera原理之camera HAL底層數據結構與類總結》
typedef struct camera_module { hw_module_t common; int (*get_number_of_cameras)(void); int (*get_camera_info)(int camera_id, struct camera_info *info); int (*set_callbacks)(const camera_module_callbacks_t *callbacks); void (*get_vendor_tag_ops)(vendor_tag_ops_t* ops); int (*open_legacy)(const struct hw_module_t* module, const char* id, uint32_t halVersion, struct hw_device_t** device); int (*set_torch_mode)(const char* camera_id, bool enabled); int (*init)(); void* reserved[5]; } camera_module_t;
結構體中有一些函數指針,咱們如今須要搞清楚這些函數指針被映射到什麼地方了?
hardware中interface中提供了原生的映射方法,在hardware/libhardware/modules/camera/3_0/CameraHAL.cpp中。
camera_module_t HAL_MODULE_INFO_SYM __attribute__ ((visibility("default"))) = { .common = { .tag = HARDWARE_MODULE_TAG, .module_api_version = CAMERA_MODULE_API_VERSION_2_2, .hal_api_version = HARDWARE_HAL_API_VERSION, .id = CAMERA_HARDWARE_MODULE_ID, .name = "Default Camera HAL", .author = "The Android Open Source Project", .methods = &gCameraModuleMethods, .dso = NULL, .reserved = {0}, }, .get_number_of_cameras = get_number_of_cameras, .get_camera_info = get_camera_info, .set_callbacks = set_callbacks, .get_vendor_tag_ops = get_vendor_tag_ops, .open_legacy = NULL, .set_torch_mode = NULL, .init = NULL, .reserved = {0}, };
這是通用的映射,咱們手機的芯片會從新這個HAL層接口,例如使用高通芯片的話,會在hardware/qcom/camera/QCamera2/QCamera2Hal.cpp中重寫這個函數映射。
實際上可能不是這個路徑,根據你本地實際的hal層從新方法來查找這些函數映射。
static hw_module_t camera_common = { .tag = HARDWARE_MODULE_TAG, .module_api_version = CAMERA_MODULE_API_VERSION_2_4, .hal_api_version = HARDWARE_HAL_API_VERSION, .id = CAMERA_HARDWARE_MODULE_ID, .name = "QCamera Module", .author = "Qualcomm Innovation Center Inc", .methods = &qcamera::QCamera2Factory::mModuleMethods, .dso = NULL, .reserved = {0} }; camera_module_t HAL_MODULE_INFO_SYM = { .common = camera_common, .get_number_of_cameras = qcamera::QCamera2Factory::get_number_of_cameras, .get_camera_info = qcamera::QCamera2Factory::get_camera_info, .set_callbacks = qcamera::QCamera2Factory::set_callbacks, .get_vendor_tag_ops = qcamera::QCamera3VendorTags::get_vendor_tag_ops, .open_legacy = NULL, .set_torch_mode = qcamera::QCamera2Factory::set_torch_mode, .init = NULL, .reserved = {0} };
明確這些函數映射以後,接下來能夠直接調用到hal層了。再底層就是芯片中複寫的核心方法了,暫時不做深刻介紹了。