camera hal 之 CameraModule
一、CameraModule的作用
我理解的CameraModule的主要作用就是承上启下的作用,在Android 开源规则下hal层各个厂商可以根据知识产权将自
己的hal层实现不对外公开,这个CameraModule就是负责加载厂商hal camera so,然后调用相关方法的。
二、加载camera hal so
在 CameraProvider 中持有 CameraModule的对象,在CameraProvider::initialize() 中 加载camera的so
-> hw_get_module(CAMERA_HARDWARE_MODULE_ID,(const hw_module_t **)&rawModule); 并且 new CameraModule
bool CameraProvider::initialize() {
camera_module_t *rawModule;
int err = hw_get_module(CAMERA_HARDWARE_MODULE_ID,
(const hw_module_t **)&rawModule);
if (err < 0) {
ALOGE("Could not load camera HAL module: %d (%s)", err, strerror(-err));
return true;
}
mModule = new CameraModule(rawModule);
........
}
我们看看怎么加载的camera so
// CAMERA_HARDWARE_MODULE_ID = camera, hw_module_t 是结构体 camera_module
int hw_get_module(const char *id, const struct hw_module_t **module)
{
// 在 hw_get_module_by_class里面主要会调用 load() 方法来加载 hal camera so
return hw_get_module_by_class(id, NULL, module);
}
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;
int hw_get_module_by_class(const char *class_id, const char *inst,
const struct hw_module_t **module)
{
...........
return load(class_id, path, module);
}
static int load(const char *id,
const char *path,
const struct hw_module_t **pHmi)
{
int status = -EINVAL;
void *handle = NULL;
struct hw_module_t *hmi = NULL;
// 注意这边开了__ANDROID_VNDK__就会去走vendor下面找这个so
#ifdef __ANDROID_VNDK__
const bool try_system = false;
#else
const bool try_system = true;
#endif
/*
* load the symbols resolving undefined symbols before
* dlopen returns. Since RTLD_GLOBAL is not or'd in with
* RTLD_NOW the external symbols will not be global
*/
if (try_system &&
strncmp(path, HAL_LIBRARY_PATH1, strlen(HAL_LIBRARY_PATH1)) == 0) {
/* If the library is in system partition, no need to check
* sphal namespace. Open it with dlopen.
*/
handle = dlopen(path, RTLD_NOW);
} else {
// 这个path就是so的加载路径
handle = android_load_sphal_library(path, RTLD_NOW);
}
if (handle == NULL) {
char const *err_str = dlerror();
ALOGE("load: module=%s\n%s", path, err_str?err_str:"unknown");
status = -EINVAL;
goto done;
}
/* Get the address of the struct hal_module_info. */
const char *sym = HAL_MODULE_INFO_SYM_AS_STR;
hmi = (struct hw_module_t *)dlsym(handle, sym);
if (hmi == NULL) {
ALOGE("load: couldn't find symbol %s", sym);
status = -EINVAL;
goto done;
}
............
}