2011-11-16 android 编译出hal so的命名规则和调用过程,使用hw_get_module查找相应的so和加载,定义的so库名一定是MODULE_ID+default.so的形式

一、android 的jni 层是使用 hw_get_module 查找相应的so和加载。

二、来看hw_get_module 的代码 hardware\libhardware\hardware.c。

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

    /*
     * 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);
    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);
}

const static hw_module_t* s_gralloc_module = NULL;
int hw_get_module(const char *id, const struct hw_module_t **module)
{
    int ret = 0;

    if ( 0 == strcmp(id, GRALLOC_HARDWARE_MODULE_ID) )
    {
        if (NULL != s_gralloc_module )
        {
            *module = s_gralloc_module;
            return 0;
        }
    }

    ret = hw_get_module_by_class(id, NULL, module);

    if ( 0 == strcmp(id, GRALLOC_HARDWARE_MODULE_ID) )
    {
        s_gralloc_module = *module;
    }

    return ret;
}

三、编译出来的so库名一定是MODULE_ID+default.so的形式,不然会找不到,我之前一直以为只要是xxx.default.so的形式就好,实际调试的时候发现不能乱修改这个名称。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值