我们知道,google为了保护硬件厂商的信息,在Android中添加了一层,也就是大名鼎鼎的HAL
层。
在看HAL的编写方法的过程中,会发现整个模块貌似没有一个入口。一般说来模块都要有个入口,
比如应用程序有main函数,可以为加载器进行加载执行,dll文件有dllmain,而对于我们自己写的
动态链接库,我们可以对库中导出的任何符号进行调用。
问题来了,Android中的HAL是比较具有通用性的,需要上层的函数对其进行加载调用,Android的
HAL加载器是如何实现对不同的Hardware Module进行通用性的调用的呢?
带着这个疑问查看Android源码,会发现Android中实现调用HAL是通过hw_get_module实现的。
1.Camera 服务启动流程概览
cameraserver 与 provider 这两个进程启动、初始化的调用逻辑,如下图
总体逻辑顺序:
provider 进程启动,注册;
cameraserver 进程启动,注册,初始化;
cameraserver 获取远端 provider(此时实例化 CameraProvider 并初始化)。
上图中,实线箭头是调用关系。左边是 cameraserver 进程中的动作,右边则是 provider 进程中的
动作,它们之间通过 ICameraProvider 联系在了一起,而这个东西与 HIDL 相关,我们可以不用关
心它的实现方式。
由图可见:
Cameraserver 一侧,Cameraservice 类依旧是主体。它通过 CameraProviderManager 来管理对
CameraProvider 的操作。此处初始化的最终目的是连接上 CameraProvider。
provider 一侧,最终主体是 CameraProvider。初始化最终目的是得到一个 mModule(Camera 模
块),通过它可以直接与 HAL 接口定义层进行交互。
2.获取camx模块接口
路径:android\hardware\interfaces\camera\provider\2.4\default\LegacyCameraProviderImpl_2_4.cpp
通过hw_get_module接口获取camera模块入口结构体。
#define CAMERA_HARDWARE_MODULE_ID "camera"
step1
注意:rawModule类型为camera_module_t,被强制转换为hw_module_t类型去获取结构体。原因
是camera_module_t的第一个成员变量即为hw_module_t类型,所以地址相同,获取到该成员变量
地址即获取整个结构体地址。
bool LegacyCameraProviderImpl_2_4::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;
}
......
}
step2
int hw_get_module(const char *id, const struct hw_module_t **module)
{
return hw_get_module_by_class(id, NULL, module); // id为"camera"
}
step3:hw_get_module_by_class
int hw_get_module_by_class(const char *class_id, const char *inst,
const struct hw_module_t **module)
{
int i = 0;
char prop[PATH_MAX] = {0};
char path[PATH_MAX] = {0};
char name[PATH_MAX] = {0};
char prop_name[PATH_MAX] = {0};
if (inst)
snprintf(name, PATH_MAX, "%s.%s", class_id, inst);
else
strlcpy(name, class_id, PATH_MAX); // name为"camera"
/*
* Here we rely on the fact that calling dlopen multiple times on
* the same .so will simply increment a refcount (and not load
* a new copy of the library).
* We also assume that dlopen() is thread-safe.
*/
/* First try a property specific to the class and possibly instance */
snprintf(prop_name, sizeof(prop_name), "ro.hardware.%s", name); // 获取ro.hardware.camera属性,实测为空
if (property_get(prop_name, prop, NULL) > 0) {
if (hw_module_exists(path, sizeof(path), name, prop) == 0) {
goto found;
}
}
/* Loop through the configuration variants looking for a module */
for (i=0 ; i<HAL_VARIANT_KEYS_COUNT; i++) {
if (property_get(variant_keys[i], prop, NULL) == 0) {
continue;
}
if (hw_module_exists(path, sizeof(path), name, prop) == 0) {
goto found;
}
}
/* Nothing found, try the default */
if (hw_module_exists(path, sizeof(path), name, "default") == 0) {
goto found;
}
return -ENOENT;
found:
/* load the module, if this fails, we're doomed, and we should not try
* to load a different variant. */
return load(class_id, path, module);
}
step4:hw_module_exists
遍历各个路径下是否存在camera.qcom.so文件。一般在/vendor/lib64/hw下。
#define HAL_LIBRARY_PATH1 "/system/lib64/hw"
#define HAL_LIBRARY_PATH2 "/vendor/lib64/hw"
#define HAL_LIBRARY_PATH3 "/odm/lib64/hw"
/*
* Check if a HAL with given name and subname exists, if so return 0, otherwise
* otherwise return negative. On success path will contain the path to the HAL.
*/
static int hw_module_exists(char *path, size_t path_len, const char *name,
const char *subname)
{
snprintf(path, path_len, "%s/%s.%s.so",
HAL_LIBRARY_PATH3, name, subname);
if (path_in_path(path, HAL_LIBRARY_PATH3) && access(path, R_OK) == 0)
return 0;
snprintf(path, path_len, "%s/%s.%s.so",
HAL_LIBRARY_PATH2, name, subname);
if (path_in_path(path, HAL_LIBRARY_PATH2) && access(path, R_OK) == 0)
return 0;
#ifndef __ANDROID_VNDK__
snprintf(path, path_len, "%s/%s.%s.so",
HAL_LIBRARY_PATH1, name, subname);
if (path_in_path(path, HAL_LIBRARY_PATH1) && access(path, R_OK) == 0)
return 0;
#endif
return -ENOENT;
}
step5:load
dlopen camera.qcom.so,dlsym获取HAL_MODULE_INFO_SYM符号的地址,即camx模块的入口
结构体地址。
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;
#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 {
#if defined(__ANDROID_RECOVERY__)
handle = dlopen(path, RTLD_NOW);
#else
handle = android_load_sphal_library(path, RTLD_NOW);
#endif
}
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"
hmi = (struct hw_module_t *)dlsym(handle, sym);
if (hmi == NULL) {
ALOGE("load: couldn't find symbol %s", sym);
status = -EINVAL;
goto done;
}
/* Check that the id matches */
if (strcmp(id, hmi->id) != 0) { // 检查id是否相同即“camera”
ALOGE("load: id=%s != hmi->id=%s", id, hmi->id);
status = -EINVAL;
goto done;
}
hmi->dso = handle;
/* success */
status = 0;
done:
if (status != 0) {
hmi = NULL;
if (handle != NULL) {
dlclose(handle);
handle = NULL;
}
} else {
ALOGV("loaded HAL id=%s path=%s hmi=%p handle=%p",
id, path, hmi, handle);
}
*pHmi = hmi;
return status;
}
CameraServer是通过dlopen的方式打开camera.qcom.so的,进而打开camx。
3.dlopen / dlsym函数(动态链接库)
void * dlopen( const char * pathname, int mode);
功能 :打开一个动态链接库
包含头文件 : #include <dlfcn.h> 编译时候要加入 -ldl (指定dl库)
函数描述 :使用dlopen函数以指定模式(mode)打开指定的动态连接库文件,并返回一个句柄给调
用进程。使用dlclose来卸载打开的库。
返回值 : 打开错误返回NULL 成功,返回库引用
使用 : dlopen
dlopen()是一个强大的库函数。该函数将打开一个新库,并把它装入内存。该函数主要用来加
载库中的符号,这些符号在编译的时候是不知道的。比如 Apache Web 服务器利用这个函数在运
行过程中加载模块,这为它提供了额外的能力。一个配置文件控制了加载模块的过程。这种机制使
得在系统中添加或者删除一个模块时,都 不需要重新编译了。
可以在自己的程序中使用 dlopen()。dlopen() 在 dlfcn.h 中定义,并在 dl 库中实现。它需要两个参
数:一个文件名和一个标志。文件名可以是我们学习过的库中的 soname。标志指明是否立刻计算
库的依赖性。如果设置为 RTLD_NOW 的话,则立刻计算;如果设置的是 RTLD_LAZY,则在需要
的时候才计算。另外,可以指定 RTLD_GLOBAL,它使得那些在以后才加载的库可以获得其中的
符号。 当库被装入后,可以把 dlopen() 返回的句柄作为给 dlsym() 的第一个参数,以获得符号在
库中的地址。使用这个地址,就可以获得库中特定函数的指针,并且调用装载库中的相应函数。
void* dlsym(void* handle,const char* symbol)
该函数handle参数是由dlopen打开动态链接库后返回的指针,symbol就是要求获取的函数的
名称,函数返回值是void*,指向函数的地址,供调用使用。
dlsym根据动态链接库操作句柄(pHandle)与符号(symbol),返回符号对应的地址。
使用这个函数不但可以获取函数地址,也可以获取变量地址。比如,假设在so中
定义了一个void mytest()函数,那在使用so时先声明一个函数指针:
void (*pMytest)() ,然后使用dlsym函数将函数指针pMytest指向mytest函数,
pMytest = (void (*)())dlsym(pHandle, "mytest");
int dlclose (void *handle);
dlclose用于关闭指定句柄的动态链接库,只有当此动态链接库的使用计数为0时,才会真正被系统卸
载。
const char *dlerror(void);
函数描述: 当动态链接库操作函数执行失败时,dlerror可以返回出错信息,返回值为NULL时表示
操作函数执行成功