fsl Camera 调试第一篇 CameraModule.cpp

       CameraModule.cpp文件不需要修改;只是作为代码的开始;CameraModule.cpp之上的Android源码都不用修改;

static struct hw_module_methods_t camera_module_methods = {
        open: camera_device_open	//最主要就这个函数,初始化,指针函数赋值;从它开始看起
};
//这就是HAL模块;Android HAL固定写法;
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, /* remove compilation warnings */
         reserved: {0}, /* remove compilation warnings */
    },

int camera_device_open(const hw_module_t* module, const char* name,
                hw_device_t** device)
{
    int rv = 0;
    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::CameraProperties::Properties* properties = NULL;

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

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

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

        if(cameraid > num_cameras)
        {
            LOGE("camera service provided cameraid out of bounds, "
                    "cameraid = %d, num supported = %d",
                    cameraid, num_cameras);
            rv = -EINVAL;
            goto fail;
        }
#if 0
        if(gCamerasOpen >= MAX_SIMUL_CAMERAS_SUPPORTED)
        {
            LOGE("maximum number of cameras already open");
            rv = -ENOMEM;
            goto fail;
        }
#endif
        camera_device = (fsl_camera_device_t*)malloc(sizeof(*camera_device));
        if(!camera_device)
        {
            LOGE("camera_device allocation fail");
            rv = -ENOMEM;
            goto fail;
        }

        camera_ops = (camera_device_ops_t*)malloc(sizeof(*camera_ops));
        if(!camera_ops)
        {
            LOGE("camera_ops allocation fail");
            rv = -ENOMEM;
            goto fail;
        }

        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;
		//各个Camera操作函数的指针函数赋值;很重要;后面的函数都是HAL文件实现;
        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];

		//createCaptureDevice是和V4L2沟通的桥梁;
		// 根据当前选中的cameraName和设备路径来创建一个合适的捕获图像的设备  
		pCaptureDevice = android::createCaptureDevice(SelectedCameraName/*设备名字*/,
                gCameraDevPath[sCameraInfo[cameraid].facing]/*设备路径*/);

		// 创建相应的图片编码器  
		pJpegEncoder = android::createJpegEncoder(android::SOFTWARE_JPEG_ENC);

		// 创建HAL层的代理
        camera = new android::CameraHal(cameraid);	//一个Camera设备完全产生了;

        if(!camera)
        {
            LOGE("Couldn't create instance of CameraHal class");
            rv = -ENOMEM;
            goto fail;
        }
		// 设置为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;

fail:
    if(camera_device) {
        free(camera_device);
        camera_device = NULL;
    }
    if(camera_ops) {
        free(camera_ops);
        camera_ops = NULL;
    }
    if(camera) {
        delete camera;
        camera = NULL;
    }
    *device = NULL;
    return rv;
}
一下是几个需要注意的地方;很重要;看懂代码的关键;

fsl Camera 调试第一篇 CameraModule.cpp_第1张图片

fsl Camera 调试第一篇 CameraModule.cpp_第2张图片

fsl Camera 调试第一篇 CameraModule.cpp_第3张图片

fsl Camera 调试第一篇 CameraModule.cpp_第4张图片



你可能感兴趣的:(fsl Camera 调试第一篇 CameraModule.cpp)