Android开发之fsl在HAL层Camera的实现

通过前面分析Camera我们知道在framework层主要就用到了两个结构体:

struct camera_module_t;struct camera_device_t;通过这两个结构体我们就和hal层联系起来了。下面我们通过fsl的HAL层源码来分析下Camera在HAL的具体实现。

[html]  view plain copy print ?
  1. <span style="font-size:14px;">static struct hw_module_methods_t camera_module_methods = { open: camera_device_open };  
  2. camera_module_t HAL_MODULE_INFO_SYM = {  
  3.     common: {  
  4.         tag: HARDWARE_MODULE_TAG,  
  5.         version_major: 1,  
  6.         version_minor: 0,  
  7.         id: CAMERA_HARDWARE_MODULE_ID,  
  8.         name: "Freescale CameraHal Module",  
  9.         author: "Freescale"  
  10.         methods: &camera_module_methods,  
  11.         dso: NULL  
  12.         reserved: {0}  
  13.     },  
  14.     get_number_of_cameras: camera_get_number_of_cameras,  
  15.     get_camera_info: camera_get_camera_info,  
  16. };  
  17.   
  18. typedef struct fsl_camera_device {  
  19.     camera_device_t base;  
  20.     int cameraid;  
  21. } fsl_camera_device_t;span>  

我们知道前面hw_get_modules就是从/system/lib/hw/camera.xx.so加载动态库,查找HMI符号的地址,保存到hw_module_t结构体中。找到的就是这个地方的camera_module_t HAL_MODULE_INFO_SYM。

1)mModule->get_camera_info

  调用的就是当前文件中的camera_get_camera_info函数

2)mModule->common->methods->open(mModule->common, name, (hw_device_t**)mDevice);

  调用的就是当前文件的camera_device_open(hw_module_t* module, char* name, hw_device_t** device)

[html]  view plain copy print ?
  1. typedef struct hw_device_t {  
  2.     uint32_t tag;  
  3.     uint32_t version;  
  4.     struct hw_module_t* module;  
  5.     uint32_t reserved[12];  
  6.     int (*close)(struct hw_device_t* device);  
  7. } hw_device_t;  

 

[html]  view plain copy print ?
  1. typedef struct camera_device {  
  2.     hw_device_t common;  
  3.     camera_device_opts_t *ops;  
  4.     void* priv;  
  5. } camera_device_t;  

这里根据主要初始化话hw_device_t **device和camera_device_opts_t *ops这两个指针。

我们来看看camera_device_open这个函数:

[html]  view plain copy print ?
  1. int camera_device_open(const hw_module_t* module, const char* name, hw_device_t** device)  
  2. {  
  3.     int num_cameras = 0;  
  4.     int cameraid;  
  5.     fsl_camera_device_t* camera_device = NULL;  
  6.     camera_device_ops_t* camera_ops = NULL;  
  7.     android::CameraHal* camera = NULL;  
  8.     char *SelectedCameraName;  
  9.     android::sp<android::CaptureDeviceInterface> pCaptureDevice = NULL;  
  10.     android::sp<android::JpegEncoderInterface>pJpegEncoder = NULL;  
  11.   
  12.     android::Mutex::Autolock lock(gCameraHalDeviceLock);  
  13.   
  14.     LOGI("camera_device open: %s", name);  
  15.   
  16.     if (name != NULL) {  
  17.         cameraid = atoi(name);  
  18.         num_cameras = camera_get_number_of_cameras();  
  19.         //判断是否有前摄像头gCameraName[0]、后摄像头gCameraName[1], 返回gCameraNum摄像头总个数(最大为2),摄像头文件路径gCameraDevPath[2]  
  20.           
  21.         camera_device = (fsl_camera_device_t*)malloc(sizeof(*camera_device));          
  22.         camera_ops = (camera_device_ops_t*)malloc(sizeof(*camera_ops));  
  23.   
  24.         memset(camera_device, 0, sizeof(*camera_device));  
  25.         memset(camera_ops, 0, sizeof(*camera_ops));  
  26.   
  27.         camera_device->base.common.tag = HARDWARE_DEVICE_TAG;  
  28.         camera_device->base.common.version = 0;  
  29.         camera_device->base.common.module = (hw_module_t *)(module);  
  30.         camera_device->base.common.close = camera_device_close;  
  31.         camera_device->base.ops = camera_ops;  
  32.         // 下面这些函数非常重要,是我们apk中最终会调用到的业务函数,都是与驱动直接交互  
  33.         camera_ops->set_preview_window = camera_set_preview_window;  
  34.         camera_ops->set_callbacks = camera_set_callbacks;  
  35.         camera_ops->enable_msg_type = camera_enable_msg_type;  
  36.         camera_ops->disable_msg_type = camera_disable_msg_type;  
  37.         camera_ops->msg_type_enabled = camera_msg_type_enabled;  
  38.         camera_ops->start_preview = camera_start_preview;  
  39.         camera_ops->stop_preview = camera_stop_preview;  
  40.         camera_ops->preview_enabled = camera_preview_enabled;  
  41.         camera_ops->store_meta_data_in_buffers = camera_store_meta_data_in_buffers;  
  42.         camera_ops->start_recording = camera_start_recording;  
  43.         camera_ops->stop_recording = camera_stop_recording;  
  44.         camera_ops->recording_enabled = camera_recording_enabled;  
  45.         camera_ops->release_recording_frame = camera_release_recording_frame;  
  46.         camera_ops->auto_focus = camera_auto_focus;  
  47.         camera_ops->cancel_auto_focus = camera_cancel_auto_focus;  
  48.         camera_ops->take_picture = camera_take_picture;  
  49.         camera_ops->cancel_picture = camera_cancel_picture;  
  50.         camera_ops->set_parameters = camera_set_parameters;  
  51.         camera_ops->get_parameters = camera_get_parameters;  
  52.         camera_ops->put_parameters = camera_put_parameters;  
  53.         camera_ops->send_command = camera_send_command;  
  54.         camera_ops->release = camera_release;  
  55.         camera_ops->dump = camera_dump;  
  56.   
  57.         *device = &camera_device->base.common;  
  58.   
  59.         camera_device->cameraid = cameraid;  
  60.         SelectedCameraName = gCameraName[sCameraInfo[cameraid].facing];  
  61.   
  62.         // 根据当前选中的cameraName和设备路径来创建一个合适的捕获图像的设备  
  63.         pCaptureDevice = android::createCaptureDevice(SelectedCameraName,  
  64.                 gCameraDevPath[sCameraInfo[cameraid].facing]);  
  65.         // 创建相应的图片编码器  
  66.         pJpegEncoder = android::createJpegEncoder(android::SOFTWARE_JPEG_ENC);  
  67.           
  68.         // 创建HAL层的代理  
  69.         camera = new android::CameraHal(cameraid);  
  70.           
  71.         // 设置为HAL层  
  72.         if (camera->setCaptureDevice(pCaptureDevice) < 0 ||  
  73.                 camera->setJpegEncoder(pJpegEncoder) < 0) {  
  74.             rv = -EINVAL;  
  75.             goto fail;  
  76.         }  
  77.           
  78.         // HAL层初始化  
  79.         if (camera->Init() < 0) {  
  80.             rv = -EINVAL;  
  81.             goto fail;  
  82.         }  
  83.   
  84.         gCameraHals[cameraid] = camera;  
  85.         gCamerasOpen++;  
  86.     }  
  87.     return rv;  
  88. }  

下面我主要分析几个函数:

1)sp pCaptureDevice = createCaptureDevice()

      sp device(new V412UVCDevice());

       device->SetDevName(deviceName);

       return device;

   这里就是通过V412UVCDevice类来构造一个对象,初始化sp device。

   我们发现CaptureDeviceInterface 这个类里面包含很多业务函数,但是都是纯虚函数,肯定有子类负责实现它。找到它的子类V412CapDeviceBase,的确实现了这些函数。但是我们的V412UVCDevice类又继承于V412CapDeviceBase,而这里我们是new 一个V412UVCDevice对象,所以根据C++的动态联编我们确定最终调用的函数都是类V412UVCDevice的实现。

 

2)sp pJpegEncoder = createJpegEncoder()

        return JpegEncoderSoftware::createInstance();

     这里也是通过new 一个JpegEncoderSoftware对象,初始化sp hardware这个指针。

      而JpegEncoderSoftware也是继承与JpegEncoderInterface类。

 

3)CameraHal *camera = new CameraHal(cameraid)

      这个类特别复杂,仅仅是构造函数中初始化的变量就有50多个,我们主要关注几个内部线程类:

       注意:下面几个类中的mHardware指向CameraHal* camera。

      (1) class CaptureFrameThread : public Thread

             线程函数为:mHardware->captureframeThreadWrapper();

             负责处理的消息队列为:CMessageQueue mCaptureThreadQueue

 

      (2) class PostProcessThread : public Thread

             线程函数为:mHardware->postprocessThreadWrapper();

             负责处理的消息队列为:CMessageQueue mPostProcessThreadQueue

 

      (3) class PreviewShowFrameThread : public Thread

             线程函数为:mHardware->previewshowframeThreadWrapper()             负责处理的消息队列为:CMessageQueue mPreviewThreadQueue

 

      (4) class EncoderFrameThread : public Thread

            线程函数为:mHardware->encoderframeThreadWrapper()           负责处理的消息队列为:CMessageQueue mEncoderThreadQueue

 

      (5) class AutoFocusThread : public Thread

             线程函数为:mHardware->autoFocusThread();

 

      (6) class TakePicThread : public Thread

             线程函数为:mHardware->takepicThread();

 

4)camera->setCaptureDevice(pCaptureDevice)

          mCaptureDevice = pCaptureDevice;

 

5)camera->setJpegEncoder(pJpegEncoder)

           mJpegEncoder = pJpegEncoder;

 

6)camera->Init()

[html]  view plain copy print ?
  1. CAMERA_HAL_RET CameraHal::Init()  
  2. {  
  3.     mCameraReady == true;  
  4.     mCaptureDevice->GetDevType(&mSensorType);  
  5.   
  6.     if ((ret = AllocInterBuf())<0)  
  7.         return ret;  
  8.     if ((ret = InitCameraHalParam()) < 0)  
  9.         return ret;  
  10.     if ((ret = CameraMiscInit()) < 0)  
  11.         return ret;  
  12.   
  13.     return ret;  
  14. }  

你可能感兴趣的:(vfl2-camer)