camera hardware module

ICS 中 沿用了linux驱动模块化的方式,把camera hal 形成一个hardware module,这点跟HC明显不同。

打开camera时, cameraservice 会先打开camera hw_moudle, 如下代码所示:

  1. void CameraService::onFirstRef()  
  2.     {  
  3.         BnCameraService::onFirstRef();  
  4.         if (hw_get_module(CAMERA_HARDWARE_MODULE_ID,      //获得camera 模块,  
  5.                     (const hw_module_t **)&mModule) < 0) {    //&mModule 应该就是camera.medfield.so.  
  6.             LOGE("Could not load camera HAL module");  
  7.             mNumberOfCameras = 0;  
  8.         }  
  9.         else {  
  10.             mNumberOfCameras = mModule->get_number_of_cameras();  //调用camera hal 层函数。  
  11.             if (mNumberOfCameras > MAX_CAMERAS) {  
  12.                 LOGE("Number of cameras(%d) > MAX_CAMERAS(%d).",  
  13.                         mNumberOfCameras, MAX_CAMERAS);  
  14.                 mNumberOfCameras = MAX_CAMERAS;  
  15.             }  
  16.             for (int i = 0; i < mNumberOfCameras; i++) {  
  17.                 setCameraFree(i);  
  18.             }  
  19.         }  
  20.         // Read the system property to determine if we have to use the  
  21.         // AUDIO_STREAM_ENFORCED_AUDIBLE type.  
  22.         char value[PROPERTY_VALUE_MAX];  
  23.         property_get("ro.camera.sound.forced", value, "0");  
  24.         if (strcmp(value, "0") != 0) {  
  25.             mAudioStreamType = AUDIO_STREAM_ENFORCED_AUDIBLE;  
  26.         } else {  
  27.             mAudioStreamType = AUDIO_STREAM_MUSIC;  
  28.         }  
  29.     }  
1. 这里的
hw_get_module(CAMERA_HARDWARE_MODULE_ID,
(const hw_module_t**)&mModule)
调用了hardware/libhardware/hardware.c
  1. int hw_get_module(const char *id, const struct hw_module_t **module)  
  2.     {  
  3.         return hw_get_module_by_class(id, NULL, module);  
  4.     }  
  5.   
  6. const char *id ------- 应该是module name  
  7. const struct hw_module_t **module -----应该是module模块地址。  
  8.   
  9.     int hw_get_module_by_class(const char *class_id, const char *inst,  
  10.                                const struct hw_module_t **module)  
  11.     {  
  12.         int status;  
  13.         int i;  
  14.         const struct hw_module_t *hmi = NULL;  
  15.         char prop[PATH_MAX];  
  16.         char path[PATH_MAX];  
  17.         char name[PATH_MAX];  
  18.         if (inst)  
  19.             snprintf(name, PATH_MAX, "%s.%s", class_id, inst);  // 这里class_id 是camera,inst 应该是medfield,  
  20.         else  
  21.             strlcpy(name, class_id, PATH_MAX);  
  22.         /* 
  23.          * Here we rely on the fact that calling dlopen multiple times on 
  24.          * the same .so will simply increment a refcount (and not load 
  25.          * a new copy of the library). 
  26.          * We also assume that dlopen() is thread-safe. 
  27.          */  
  28.           
  29.         /* Loop through the configuration variants looking for a module */  
  30.         for (i=0 ; i<HAL_VARIANT_KEYS_COUNT+1 ; i++) {  
  31.             if (i < HAL_VARIANT_KEYS_COUNT) {  
  32.                 if (property_get(variant_keys[i], prop, NULL) == 0) {  
  33.                     continue;  
  34.                 }  
  35.                 snprintf(path, sizeof(path), "%s/%s.%s.so",  
  36.                          HAL_LIBRARY_PATH2, name, prop);  
  37.                 if (access(path, R_OK) == 0) break;  
  38.                 snprintf(path, sizeof(path), "%s/%s.%s.so",  
  39.                          HAL_LIBRARY_PATH1, name, prop);  
  40.                 if (access(path, R_OK) == 0) break;  
  41.             } else {  
  42.                 snprintf(path, sizeof(path), "%s/%s.default.so",  
  43.                          HAL_LIBRARY_PATH1, name);  
  44.                 if (access(path, R_OK) == 0) break;  
  45.             }  
  46.         }  
  47.           
  48.         status = -ENOENT;  
  49.         if (i < HAL_VARIANT_KEYS_COUNT+1) {  
  50.             /* load the module, if this fails, we're doomed, and we should not try 
  51.              * to load a different variant. */  
  52.             status = load(class_id, path, module);  
  53.         }  
  54.           
  55.         return status;  
  56.     }  
load(class_id, path, module) 代码如下:
  1. /** 
  2.      * Load the file defined by the variant and if successful 
  3.      * return the dlopen handle and the hmi. 
  4.      * @return 0 = success, !0 = failure. 
  5.      */  
  6.     static int load(const char *id,  
  7.             const char *path,  
  8.             const struct hw_module_t **pHmi)  
  9.     {  
  10.         int status;  
  11.         void *handle;  
  12.         struct hw_module_t *hmi;  
  13.         /* 
  14.          * load the symbols resolving undefined symbols before 
  15.          * dlopen returns. Since RTLD_GLOBAL is not or'd in with 
  16.          * RTLD_NOW the external symbols will not be global 
  17.          */  
  18.         handle = dlopen(path, RTLD_NOW);  
  19.         if (handle == NULL) {  
  20.             char const *err_str = dlerror();  
  21.             LOGE("load: module=%s\n%s", path, err_str?err_str:"unknown");  
  22.             status = -EINVAL;  
  23.             goto done;  
  24.         }  
  25.         /* Get the address of the struct hal_module_info. */  
  26.         const char *sym = HAL_MODULE_INFO_SYM_AS_STR;  
  27.         hmi = (struct hw_module_t *)dlsym(handle, sym);  
  28.         if (hmi == NULL) {  
  29.             LOGE("load: couldn't find symbol %s", sym);  
  30.             status = -EINVAL;  
  31.             goto done;  
  32.         }  
  33.         /* Check that the id matches */  
  34.         if (strcmp(id, hmi->id) != 0) {  
  35.             LOGE("load: id=%s != hmi->id=%s", id, hmi->id);  
  36.             status = -EINVAL;  
  37.             goto done;  
  38.         }  
  39.         hmi->dso = handle;  
  40.         /* success */  
  41.         status = 0;  
  42.         done:  
  43.         if (status != 0) {  
  44.             hmi = NULL;  
  45.             if (handle != NULL) {  
  46.                 dlclose(handle);  
  47.                 handle = NULL;  
  48.             }  
  49.         } else {  
  50.             LOGV("loaded HAL id=%s path=%s hmi=%p handle=%p",  
  51.                     id, path, *pHmi, handle);  
  52.         }  
  53.         *pHmi = hmi;  
  54.         return status;  
  55.     }  
2.  mNumberOfCameras = mModule->get_number_of_cameras();
调用了 hardware/intel/libcamera/IntelCameraHAL.cpp 中的
  1. camera_module_t HAL_MODULE_INFO_SYM = {  
  2.         common: {  
  3.              tag: HARDWARE_MODULE_TAG,  
  4.              version_major: 1,  
  5.              version_minor: 0,  
  6.              id: CAMERA_HARDWARE_MODULE_ID,  
  7.              name: "Intel CameraHardware Module",  
  8.              author: "Intel",  
  9.              methods: &camera_module_methods,     //这里面定义了各种函数接口。  
  10.              dso: NULL, /* remove compilation warnings */  
  11.              reserved: {0}, /* remove compilation warnings */  
  12.         },  
  13.         get_number_of_cameras: HAL_GetNumberOfCameras,   //cameraservice 中会调用这两个函数。  
  14.         get_camera_info: HAL_GetCameraInfo,  
  15.     };  
a.  get_number_of_cameras: HAL_GetNumberOfCameras,
  1. int HAL_GetNumberOfCameras(void)  
  2.     {  
  3.         return android::CameraHardware::getNumberOfCameras();  
  4.     }  
  5.   
  6.     /* This function will be called when the camera service is created. 
  7.      * Do some init work in this function. 
  8.      */  
  9.     int CameraHardware::getNumberOfCameras()  
  10.     {  
  11.         LogEntry(LOG_TAG, __FUNCTION__);  
  12.         if (num_cameras != 0)  
  13.             return num_cameras;  
  14.         int ret;  
  15.         struct v4l2_input input;  
  16.         int fd = -1;  
  17.         char *dev_name = "/dev/video0";  
  18.         fd = open(dev_name, O_RDWR);  
  19.         if (fd <= 0) {  
  20.             LogError("Error opening video device %s: %s",  
  21.                  dev_name, strerror(errno));  
  22.             return 0;  
  23.         }  
  24.         int i;  
  25.         for (i = 0; i < MAX_CAMERAS; i++) {  
  26.             memset(&input, 0, sizeof(input));  
  27.             input.index = i;  
  28.             ret = ioctl(fd, VIDIOC_ENUMINPUT, &input);  //从kernel 获得相应信息,枚举input通道信息。  
  29.             if (ret < 0) {  
  30.                 break;  
  31.             }  
  32.             camInfo[i].port = input.reserved[1];  
  33.             strncpy(camInfo[i].name, (const char *)input.name, MAX_SENSOR_NAME_LENGTH);  
  34.         }  
  35.         close(fd);  
  36.         num_cameras = i;  
  37.         return num_cameras;  
  38.     }  
b. get_camera_info: HAL_GetCameraInfo,
  1. int HAL_GetCameraInfo(int camera_id, struct camera_info *info)  
  2.     {  
  3.         return android::CameraHardware::getCameraInfo(camera_id, info);  
  4.     }  
  5.   
  6.     int CameraHardware::getCameraInfo(int cameraId, struct camera_info* cameraInfo)  
  7.     {  
  8.         LogEntry(LOG_TAG, __FUNCTION__);  
  9.         if (cameraId >= MAX_CAMERAS)  
  10.             return -EINVAL;  
  11.         memcpy(cameraInfo, &HAL_cameraInfo[cameraId], sizeof(camera_info));  //从HAL_cameraInfo[cameraId] 中获得信息。  
  12.         return 0;  
  13.     }  
HAL_cameraInfo[cameraId] 定义如下:
  1. static camera_info HAL_cameraInfo[MAX_CAMERAS] = {  
  2.         {  
  3.             CAMERA_FACING_FRONT,  
  4.             180,  
  5.         },  
  6.         {  
  7.             CAMERA_FACING_BACK,  
  8.             0,  
  9.         }  
  10.     };  
c. methods:&camera_module_methods,     //这里面定义了各种函数接口。
  1. static struct hw_module_methods_t camera_module_methods = {  
  2.         open: HAL_OpenCameraHardware   // 也就是这个函数  
  3.     };  
  4.   
  5. open: HAL_OpenCameraHardware,  
  6.   
  7.     int HAL_OpenCameraHardware(const hw_module_t* module, const char* name,  
  8.                     hw_device_t** device)  
  9.     {  
  10.         int rv = 0;  
  11.         int num_cameras = 0;  
  12.         int cameraid;  
  13.         intel_camera_device_t* camera_device = NULL;  
  14.         camera_device_ops_t* camera_ops = NULL;  
  15.         android::CameraHardware* camera = NULL;  
  16.         android::Mutex::Autolock lock(gCameraHalDeviceLock);  
  17.         LOGI("camera_device open");  
  18.         if (name != NULL) {  
  19.             cameraid = atoi(name);  
  20.             num_cameras = android::CameraHardware::getNumberOfCameras();  
  21.             if(cameraid > num_cameras)  
  22.             {  
  23.                 LOGE("camera service provided cameraid out of bounds, "  
  24.                         "cameraid = %d, num supported = %d",  
  25.                         cameraid, num_cameras);  
  26.                 rv = -EINVAL;  
  27.                 goto fail;  
  28.             }  
  29.             if(gCamerasOpen >= MAX_CAMERAS)  
  30.             {  
  31.                 LOGE("maximum number of cameras already open");  
  32.                 rv = -ENOMEM;  
  33.                 goto fail;  
  34.             }  
  35.             camera_device = (intel_camera_device_t*)malloc(sizeof(*camera_device));  
  36.             if(!camera_device)  
  37.             {  
  38.                 LOGE("camera_device allocation fail");  
  39.                 rv = -ENOMEM;  
  40.                 goto fail;  
  41.             }  
  42.             camera_ops = (camera_device_ops_t*)malloc(sizeof(*camera_ops));  
  43.             if(!camera_ops)  
  44.             {  
  45.                 LOGE("camera_ops allocation fail");  
  46.                 rv = -ENOMEM;  
  47.                 goto fail;  
  48.             }  
  49.             memset(camera_device, 0, sizeof(*camera_device));  
  50.             memset(camera_ops, 0, sizeof(*camera_ops));  
  51.             camera_device->device.common.tag = HARDWARE_DEVICE_TAG;  
  52.             camera_device->device.common.version = 0;  
  53.             camera_device->device.common.module = (hw_module_t *)(module);  
  54.             camera_device->device.common.close = HAL_CloseCameraHardware;  
  55.             camera_device->device.ops = camera_ops;  
  56.             camera_ops->set_preview_window = camera_set_preview_window;  
  57.             camera_ops->set_callbacks = camera_set_callbacks;  
  58.             camera_ops->enable_msg_type = camera_enable_msg_type;  
  59.             camera_ops->disable_msg_type = camera_disable_msg_type;  
  60.             camera_ops->msg_type_enabled = camera_msg_type_enabled;  
  61.             camera_ops->start_preview = camera_start_preview;  
  62.             camera_ops->stop_preview = camera_stop_preview;  
  63.             camera_ops->preview_enabled = camera_preview_enabled;  
  64.             camera_ops->store_meta_data_in_buffers = camera_store_meta_data_in_buffers;  
  65.             camera_ops->start_recording = camera_start_recording;  
  66.             camera_ops->stop_recording = camera_stop_recording;  
  67.             camera_ops->recording_enabled = camera_recording_enabled;  
  68.             camera_ops->release_recording_frame = camera_release_recording_frame;  
  69.             camera_ops->auto_focus = camera_auto_focus;  
  70.             camera_ops->cancel_auto_focus = camera_cancel_auto_focus;  
  71.             camera_ops->take_picture = camera_take_picture;  
  72.             camera_ops->cancel_picture = camera_cancel_picture;  
  73.             camera_ops->set_parameters = camera_set_parameters;  
  74.             camera_ops->get_parameters = camera_get_parameters;  
  75.             camera_ops->put_parameters = camera_put_parameters;  
  76.             camera_ops->send_command = camera_send_command;  
  77.             camera_ops->release = camera_release;  
  78.             camera_ops->dump = camera_dump;  
  79.             *device = &camera_device->device.common;  
  80.             camera_device->cameraId = cameraid;  
  81.             camera = new android::CameraHardware(cameraid);  
  82.             if(!camera)  
  83.             {  
  84.                 LOGE("Couldn't create instance of CameraHardware class!");  
  85.                 rv = -ENOMEM;  
  86.                 goto fail;  
  87.             }  
  88.             gCameraHals[cameraid] = camera;  
  89.             gCamerasOpen++;  
  90.         }  
  91.         return rv;  
  92.     fail:  
  93.         if(camera_device) {  
  94.             free(camera_device);  
  95.             camera_device = NULL;  
  96.         }  
  97.         if(camera_ops) {  
  98.             free(camera_ops);  
  99.             camera_ops = NULL;  
  100.         }  
  101.         if(camera) {  
  102.             delete camera;  
  103.             camera = NULL;  
  104.         }  
  105.         *device = NULL;  
  106.         return rv;  
  107.     }  
这个函数里 封装了一系列 camera interface。 但是cameraservice 并没有用到这些接口,还是老的调用方式,直接调用了 camerahardware, 而没经过这里的接口。
只有
HAL_GetCameraInfo HAL_GetNumberOfCameras 通过这些接口来调用。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值