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

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

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

static struct hw_module_methods_t camera_module_methods = { 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: "Freescale CameraHal Module",
		author: "Freescale"
		methods: &camera_module_methods,
		dso: NULL
		reserved: {0}
	},
	get_number_of_cameras: camera_get_number_of_cameras,
	get_camera_info: camera_get_camera_info,
};

typedef struct fsl_camera_device {
	camera_device_t base;
	int cameraid;
} fsl_camera_device_t;

我们知道前面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)

typedef struct hw_device_t {
	uint32_t tag;
	uint32_t version;
	struct hw_module_t* module;
	uint32_t reserved[12];
	int (*close)(struct hw_device_t* device);
} hw_device_t;

 

typedef struct camera_device {
	hw_device_t common;
	camera_device_opts_t *ops;
	void* priv;
} camera_device_t;

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

我们来看看camera_device_open这个函数:

int camera_device_open(const hw_module_t* module, const char* name, hw_device_t** device)
{
    int num_cameras = 0;
    int cameraid;
    fsl_camera_device_t* camera_device = NULL;
    camera_device_ops_t* camera_ops = NULL;
    android::CameraHal* camera = NULL;
    char *SelectedCameraName;
    android::sp pCaptureDevice = NULL;
    android::sppJpegEncoder = NULL;

    android::Mutex::Autolock lock(gCameraHalDeviceLock);

    LOGI("camera_device open: %s", name);

    if (name != NULL) {
        cameraid = atoi(name);
        num_cameras = camera_get_number_of_cameras();
		//判断是否有前摄像头gCameraName[0]、后摄像头gCameraName[1], 返回gCameraNum摄像头总个数(最大为2),摄像头文件路径gCameraDevPath[2]
        
        camera_device = (fsl_camera_device_t*)malloc(sizeof(*camera_device));        
        camera_ops = (camera_device_ops_t*)malloc(sizeof(*camera_ops));

        memset(camera_device, 0, sizeof(*camera_device));
        memset(camera_ops, 0, sizeof(*camera_ops));

        camera_device->base.common.tag = HARDWARE_DEVICE_TAG;
        camera_device->base.common.version = 0;
        camera_device->base.common.module = (hw_module_t *)(module);
        camera_device->base.common.close = camera_device_close;
        camera_device->base.ops = camera_ops;
		// 下面这些函数非常重要,是我们apk中最终会调用到的业务函数,都是与驱动直接交互
        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;

        camera_device->cameraid = cameraid;
        SelectedCameraName = gCameraName[sCameraInfo[cameraid].facing];

		// 根据当前选中的cameraName和设备路径来创建一个合适的捕获图像的设备
        pCaptureDevice = android::createCaptureDevice(SelectedCameraName,
                gCameraDevPath[sCameraInfo[cameraid].facing]);
		// 创建相应的图片编码器
        pJpegEncoder = android::createJpegEncoder(android::SOFTWARE_JPEG_ENC);
		
		// 创建HAL层的代理
        camera = new android::CameraHal(cameraid);
		
		// 设置为HAL层
        if (camera->setCaptureDevice(pCaptureDevice) < 0 ||
                camera->setJpegEncoder(pJpegEncoder) < 0) {
            rv = -EINVAL;
            goto fail;
        }
		
		// HAL层初始化
        if (camera->Init() < 0) {
            rv = -EINVAL;
            goto fail;
        }

        gCameraHals[cameraid] = camera;
        gCamerasOpen++;
    }
    return rv;
}

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

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()

CAMERA_HAL_RET CameraHal::Init()
{
	mCameraReady == true;
	mCaptureDevice->GetDevType(&mSensorType);

	if ((ret = AllocInterBuf())<0)
		return ret;
	if ((ret = InitCameraHalParam()) < 0)
		return ret;
	if ((ret = CameraMiscInit()) < 0)
		return ret;

	return ret;
}


 

 

 

你可能感兴趣的:(Android,源码分析)