AndroidO Treble架构下HIDL服务查询过程

通过前面的分析我们知道,Hal进程启动时,会向hwservicemanager进程注册hidl服务,那么当Framework Server需要通过hal访问硬件设备时,首先需要查询对应的hidl服务,那么Client进程是如何查询hidl服务的呢?这篇文章将展开分析,这里再次以IComposer为例进行展开。

frameworks\native\services\surfaceflinger\DisplayHardware\ComposerHal.cpp

  1. Composer::Composer(bool useVrComposer)  
  2.     : mWriter(kWriterInitialSize),  
  3.       mIsUsingVrComposer(useVrComposer)  
  4. {  
  5.     if (mIsUsingVrComposer) {  
  6.         mComposer = IComposer::getService(”vr”);  
  7.     } else {  
  8.         mComposer = IComposer::getService(); // use default name  
  9.     }  
  10.   
  11.     if (mComposer == nullptr) {  
  12.         LOG_ALWAYS_FATAL(”failed to get hwcomposer service”);  
  13.     }  
  14.   
  15.     mComposer->createClient(  
  16.             [&](const auto& tmpError, const auto& tmpClient)  
  17.             {  
  18.                 if (tmpError == Error::NONE) {  
  19.                     mClient = tmpClient;  
  20.                 }  
  21.             });  
  22.     if (mClient == nullptr) {  
  23.         LOG_ALWAYS_FATAL(”failed to create composer client”);  
  24.     }  
  25. }  
Composer::Composer(bool useVrComposer)
    : mWriter(kWriterInitialSize),
      mIsUsingVrComposer(useVrComposer)
{
    if (mIsUsingVrComposer) {
        mComposer = IComposer::getService("vr");
    } else {
        mComposer = IComposer::getService(); // use default name
    }

    if (mComposer == nullptr) {
        LOG_ALWAYS_FATAL("failed to get hwcomposer service");
    }

    mComposer->createClient(
            [&](const auto& tmpError, const auto& tmpClient)
            {
                if (tmpError == Error::NONE) {
                    mClient = tmpClient;
                }
            });
    if (mClient == nullptr) {
        LOG_ALWAYS_FATAL("failed to create composer client");
    }
}

这里通过IComposer::getService()函数来查询IComposer这个HIDL服务,由于这里没有传递任何参数,因此函数最终会调用:

composer\2.1\[email protected]_genc++_headers\gen\android\hardware\graphics\composer\2.1\IComposer.h

  1. static ::android::sp<IComposer> getService(const std::string &serviceName=“default”bool getStub=false);  
static ::android::sp<IComposer> getService(const std::string &serviceName="default", bool getStub=false);

注意,这里的getStub为false,说明加载hidl服务方式是由当前hidl服务的transport类型决定。

  1. <hal format=“hidl”>  
  2.     <name>android.hardware.graphics.composer</name>  
  3.     <transport>hwbinder</transport>  
  4.     <version>2.1</version>  
  5.     <interface>  
  6.         <name>IComposer</name>  
  7.         <instance>vr</instance>  
  8.     </interface>  
  9. </hal>  
    <hal format="hidl">
        <name>android.hardware.graphics.composer</name>
        <transport>hwbinder</transport>
        <version>2.1</version>
        <interface>
            <name>IComposer</name>
            <instance>vr</instance>
        </interface>
    </hal>

由于IComposer的transport是hwbinder类型,那么将从hwservicemanager中查询hidl服务。

composer\2.1\[email protected]_genc++\gen\android\hardware\graphics\composer\2.1\ComposerAll.cpp  

  1. ::android::sp<IComposer> IComposer::getService(const std::string &serviceName, const bool getStub) {  
  2.     using ::android::hardware::defaultServiceManager;  
  3.     using ::android::hardware::details::waitForHwService;  
  4.     using ::android::hardware::getPassthroughServiceManager;  
  5.     using ::android::hardware::Return;  
  6.     using ::android::sp;  
  7.     using Transport = ::android::hidl::manager::V1_0::IServiceManager::Transport;  
  8.   
  9.     sp<IComposer> iface = nullptr;  
  10.   
  11.     const sp<::android::hidl::manager::V1_0::IServiceManager> sm = defaultServiceManager();  
  12.     if (sm == nullptr) {  
  13.         ALOGE(”getService: defaultServiceManager() is null”);  
  14.         return nullptr;  
  15.     }  
  16.   
  17.     Return<Transport> transportRet = sm->getTransport(IComposer::descriptor, serviceName);  
  18.   
  19.     if (!transportRet.isOk()) {  
  20.         ALOGE(”getService: defaultServiceManager()->getTransport returns %s”, transportRet.description().c_str());  
  21.         return nullptr;  
  22.     }  
  23.     Transport transport = transportRet;  
  24.     const bool vintfHwbinder = (transport == Transport::HWBINDER);  
  25.     const bool vintfPassthru = (transport == Transport::PASSTHROUGH);  
  26.   
  27.     #ifdef __ANDROID_TREBLE__  
  28.   
  29.     #ifdef __ANDROID_DEBUGGABLE__  
  30.     const char* env = std::getenv(“TREBLE_TESTING_OVERRIDE”);  
  31.     const bool trebleTestingOverride =  env && !strcmp(env, “true”);  
  32.     const bool vintfLegacy = (transport == Transport::EMPTY) && trebleTestingOverride;  
  33.     #else // __ANDROID_TREBLE__ but not __ANDROID_DEBUGGABLE__  
  34.     const bool trebleTestingOverride = false;  
  35.     const bool vintfLegacy = false;  
  36.     #endif // __ANDROID_DEBUGGABLE__  
  37.   
  38.     #else // not __ANDROID_TREBLE__  
  39.     const char* env = std::getenv(“TREBLE_TESTING_OVERRIDE”);  
  40.     const bool trebleTestingOverride =  env && !strcmp(env, “true”);  
  41.     const bool vintfLegacy = (transport == Transport::EMPTY);  
  42.   
  43.     #endif // __ANDROID_TREBLE__  
  44.   
  45.     for (int tries = 0; !getStub && (vintfHwbinder || (vintfLegacy && tries == 0)); tries++) {  
  46.         if (tries > 1) {  
  47.             ALOGI(”getService: Will do try %d for %s/%s in 1s…”, tries, IComposer::descriptor, serviceName.c_str());  
  48.             sleep(1);  
  49.         }  
  50.         if (vintfHwbinder && tries > 0) {  
  51.             waitForHwService(IComposer::descriptor, serviceName);  
  52.         }  
  53.         Return<sp<::android::hidl::base::V1_0::IBase>> ret =   
  54.                 sm->get(IComposer::descriptor, serviceName);  
  55.         if (!ret.isOk()) {  
  56.             ALOGE(”IComposer: defaultServiceManager()->get returns %s”, ret.description().c_str());  
  57.             break;  
  58.         }  
  59.         sp<::android::hidl::base::V1_0::IBase> base = ret;  
  60.         if (base == nullptr) {  
  61.             if (tries > 0) {  
  62.                 ALOGW(”IComposer: found null hwbinder interface”);  
  63.             }continue;  
  64.         }  
  65.         Return<sp<IComposer>> castRet = IComposer::castFrom(base, true /* emitError */);  
  66.         if (!castRet.isOk()) {  
  67.             if (castRet.isDeadObject()) {  
  68.                 ALOGW(”IComposer: found dead hwbinder service”);  
  69.                 continue;  
  70.             } else {  
  71.                 ALOGW(”IComposer: cannot call into hwbinder service: %s; No permission? Check for selinux denials.”, castRet.description().c_str());  
  72.                 break;  
  73.             }  
  74.         }  
  75.         iface = castRet;  
  76.         if (iface == nullptr) {  
  77.             ALOGW(”IComposer: received incompatible service; bug in hwservicemanager?”);  
  78.             break;  
  79.         }  
  80.         return iface;  
  81.     }  
  82.     return iface;  
  83. }  
::android::sp<IComposer> IComposer::getService(const std::string &serviceName, const bool getStub) {
    using ::android::hardware::defaultServiceManager;
    using ::android::hardware::details::waitForHwService;
    using ::android::hardware::getPassthroughServiceManager;
    using ::android::hardware::Return;
    using ::android::sp;
    using Transport = ::android::hidl::manager::V1_0::IServiceManager::Transport;

    sp<IComposer> iface = nullptr;

    const sp<::android::hidl::manager::V1_0::IServiceManager> sm = defaultServiceManager();
    if (sm == nullptr) {
        ALOGE("getService: defaultServiceManager() is null");
        return nullptr;
    }

    Return<Transport> transportRet = sm->getTransport(IComposer::descriptor, serviceName);

    if (!transportRet.isOk()) {
        ALOGE("getService: defaultServiceManager()->getTransport returns %s", transportRet.description().c_str());
        return nullptr;
    }
    Transport transport = transportRet;
    const bool vintfHwbinder = (transport == Transport::HWBINDER);
    const bool vintfPassthru = (transport == Transport::PASSTHROUGH);

    #ifdef __ANDROID_TREBLE__

    #ifdef __ANDROID_DEBUGGABLE__
    const char* env = std::getenv("TREBLE_TESTING_OVERRIDE");
    const bool trebleTestingOverride =  env && !strcmp(env, "true");
    const bool vintfLegacy = (transport == Transport::EMPTY) && trebleTestingOverride;
    #else // __ANDROID_TREBLE__ but not __ANDROID_DEBUGGABLE__
    const bool trebleTestingOverride = false;
    const bool vintfLegacy = false;
    #endif // __ANDROID_DEBUGGABLE__

    #else // not __ANDROID_TREBLE__
    const char* env = std::getenv("TREBLE_TESTING_OVERRIDE");
    const bool trebleTestingOverride =  env && !strcmp(env, "true");
    const bool vintfLegacy = (transport == Transport::EMPTY);

    #endif // __ANDROID_TREBLE__

    for (int tries = 0; !getStub && (vintfHwbinder || (vintfLegacy && tries == 0)); tries++) {
        if (tries > 1) {
            ALOGI("getService: Will do try %d for %s/%s in 1s...", tries, IComposer::descriptor, serviceName.c_str());
            sleep(1);
        }
        if (vintfHwbinder && tries > 0) {
            waitForHwService(IComposer::descriptor, serviceName);
        }
        Return<sp<::android::hidl::base::V1_0::IBase>> ret = 
                sm->get(IComposer::descriptor, serviceName);
        if (!ret.isOk()) {
            ALOGE("IComposer: defaultServiceManager()->get returns %s", ret.description().c_str());
            break;
        }
        sp<::android::hidl::base::V1_0::IBase> base = ret;
        if (base == nullptr) {
            if (tries > 0) {
                ALOGW("IComposer: found null hwbinder interface");
            }continue;
        }
        Return<sp<IComposer>> castRet = IComposer::castFrom(base, true /* emitError */);
        if (!castRet.isOk()) {
            if (castRet.isDeadObject()) {
                ALOGW("IComposer: found dead hwbinder service");
                continue;
            } else {
                ALOGW("IComposer: cannot call into hwbinder service: %s; No permission? Check for selinux denials.", castRet.description().c_str());
                break;
            }
        }
        iface = castRet;
        if (iface == nullptr) {
            ALOGW("IComposer: received incompatible service; bug in hwservicemanager?");
            break;
        }
        return iface;
    }
    return iface;
}

这里通过sm->get(IComposer::descriptor, serviceName)查询IComposer这个hidl服务,得到IBase对象后,在通过IComposer::castFrom转换为IComposer对象。

服务查询

[email protected]_genc++\gen\android\hidl\manager\1.0\ServiceManagerAll.cpp
  1. ::android::hardware::Return<::android::sp<::android::hidl::base::V1_0::IBase>> BpHwServiceManager::get(const ::android::hardware::hidl_string& fqName, const ::android::hardware::hidl_string& name){  
  2.     ::android::hardware::Return<::android::sp<::android::hidl::base::V1_0::IBase>>  _hidl_out = ::android::hidl::manager::V1_0::BpHwServiceManager::_hidl_get(thisthis, fqName, name);  
  3.   
  4.     return _hidl_out;  
  5. }  
::android::hardware::Return<::android::sp<::android::hidl::base::V1_0::IBase>> BpHwServiceManager::get(const ::android::hardware::hidl_string& fqName, const ::android::hardware::hidl_string& name){
    ::android::hardware::Return<::android::sp<::android::hidl::base::V1_0::IBase>>  _hidl_out = ::android::hidl::manager::V1_0::BpHwServiceManager::_hidl_get(this, this, fqName, name);

    return _hidl_out;
}
  1. ::android::hardware::Return<::android::sp<::android::hidl::base::V1_0::IBase>> BpHwServiceManager::_hidl_get(::android::hardware::IInterface *_hidl_this, ::android::hardware::details::HidlInstrumentor *_hidl_this_instrumentor, const ::android::hardware::hidl_string& fqName, const ::android::hardware::hidl_string& name) {  
  2.     #ifdef __ANDROID_DEBUGGABLE__  
  3.     bool mEnableInstrumentation = _hidl_this_instrumentor->isInstrumentationEnabled();  
  4.     const auto &mInstrumentationCallbacks = _hidl_this_instrumentor->getInstrumentationCallbacks();  
  5.     #else  
  6.     (void) _hidl_this_instrumentor;  
  7.     #endif // __ANDROID_DEBUGGABLE__  
  8.     atrace_begin(ATRACE_TAG_HAL, ”HIDL::IServiceManager::get::client”);  
  9.     #ifdef __ANDROID_DEBUGGABLE__  
  10.     if (UNLIKELY(mEnableInstrumentation)) {  
  11.         std::vector<void *> _hidl_args;  
  12.         _hidl_args.push_back((void *)&fqName);  
  13.         _hidl_args.push_back((void *)&name);  
  14.         for (const auto &callback: mInstrumentationCallbacks) {  
  15.             callback(InstrumentationEvent::CLIENT_API_ENTRY, ”android.hidl.manager”“1.0”“IServiceManager”“get”, &_hidl_args);  
  16.         }  
  17.     }  
  18.     #endif // __ANDROID_DEBUGGABLE__  
  19.   
  20.     ::android::hardware::Parcel _hidl_data;  
  21.     ::android::hardware::Parcel _hidl_reply;  
  22.     ::android::status_t _hidl_err;  
  23.     ::android::hardware::Status _hidl_status;  
  24.   
  25.     ::android::sp<::android::hidl::base::V1_0::IBase> _hidl_out_service;  
  26.   
  27.     _hidl_err = _hidl_data.writeInterfaceToken(BpHwServiceManager::descriptor);  
  28.     if (_hidl_err != ::android::OK) { goto _hidl_error; }  
  29.   
  30.     size_t _hidl_fqName_parent;  
  31.   
  32.     _hidl_err = _hidl_data.writeBuffer(&fqName, sizeof(fqName), &_hidl_fqName_parent);  
  33.     if (_hidl_err != ::android::OK) { goto _hidl_error; }  
  34.   
  35.     _hidl_err = ::android::hardware::writeEmbeddedToParcel(  
  36.             fqName,  
  37.             &_hidl_data,  
  38.             _hidl_fqName_parent,  
  39.             0 /* parentOffset */);  
  40.   
  41.     if (_hidl_err != ::android::OK) { goto _hidl_error; }  
  42.   
  43.     size_t _hidl_name_parent;  
  44.   
  45.     _hidl_err = _hidl_data.writeBuffer(&name, sizeof(name), &_hidl_name_parent);  
  46.     if (_hidl_err != ::android::OK) { goto _hidl_error; }  
  47.   
  48.     _hidl_err = ::android::hardware::writeEmbeddedToParcel(  
  49.             name,  
  50.             &_hidl_data,  
  51.             _hidl_name_parent,  
  52.             0 /* parentOffset */);  
  53.   
  54.     if (_hidl_err != ::android::OK) { goto _hidl_error; }  
  55.   
  56.     _hidl_err = ::android::hardware::IInterface::asBinder(_hidl_this)->transact(1 /* get */, _hidl_data, &_hidl_reply);  
  57.     if (_hidl_err != ::android::OK) { goto _hidl_error; }  
  58.   
  59.     _hidl_err = ::android::hardware::readFromParcel(&_hidl_status, _hidl_reply);  
  60.     if (_hidl_err != ::android::OK) { goto _hidl_error; }  
  61.   
  62.     if (!_hidl_status.isOk()) { return _hidl_status; }  
  63.   
  64.     {  
  65.         ::android::sp<::android::hardware::IBinder> _hidl__hidl_out_service_binder;  
  66.         _hidl_err = _hidl_reply.readNullableStrongBinder(&_hidl__hidl_out_service_binder);  
  67.         if (_hidl_err != ::android::OK) { goto _hidl_error; }  
  68.   
  69.         _hidl_out_service = ::android::hardware::fromBinder<::android::hidl::base::V1_0::IBase,::android::hidl::base::V1_0::BpHwBase,::android::hidl::base::V1_0::BnHwBase>(_hidl__hidl_out_service_binder);  
  70.     }  
  71.   
  72.     atrace_end(ATRACE_TAG_HAL);  
  73.     #ifdef __ANDROID_DEBUGGABLE__  
  74.     if (UNLIKELY(mEnableInstrumentation)) {  
  75.         std::vector<void *> _hidl_args;  
  76.         _hidl_args.push_back((void *)&_hidl_out_service);  
  77.         for (const auto &callback: mInstrumentationCallbacks) {  
  78.             callback(InstrumentationEvent::CLIENT_API_EXIT, ”android.hidl.manager”“1.0”“IServiceManager”“get”, &_hidl_args);  
  79.         }  
  80.     }  
  81.     #endif // __ANDROID_DEBUGGABLE__  
  82.   
  83.     _hidl_status.setFromStatusT(_hidl_err);  
  84.     return ::android::hardware::Return<::android::sp<::android::hidl::base::V1_0::IBase>>(_hidl_out_service);  
  85.   
  86. _hidl_error:  
  87.     _hidl_status.setFromStatusT(_hidl_err);  
  88.     return ::android::hardware::Return<::android::sp<::android::hidl::base::V1_0::IBase>>(_hidl_status);  
  89. }  
::android::hardware::Return<::android::sp<::android::hidl::base::V1_0::IBase>> BpHwServiceManager::_hidl_get(::android::hardware::IInterface *_hidl_this, ::android::hardware::details::HidlInstrumentor *_hidl_this_instrumentor, const ::android::hardware::hidl_string& fqName, const ::android::hardware::hidl_string& name) {
    #ifdef __ANDROID_DEBUGGABLE__
    bool mEnableInstrumentation = _hidl_this_instrumentor->isInstrumentationEnabled();
    const auto &mInstrumentationCallbacks = _hidl_this_instrumentor->getInstrumentationCallbacks();
    #else
    (void) _hidl_this_instrumentor;
    #endif // __ANDROID_DEBUGGABLE__
    atrace_begin(ATRACE_TAG_HAL, "HIDL::IServiceManager::get::client");
    #ifdef __ANDROID_DEBUGGABLE__
    if (UNLIKELY(mEnableInstrumentation)) {
        std::vector<void *> _hidl_args;
        _hidl_args.push_back((void *)&fqName);
        _hidl_args.push_back((void *)&name);
        for (const auto &callback: mInstrumentationCallbacks) {
            callback(InstrumentationEvent::CLIENT_API_ENTRY, "android.hidl.manager", "1.0", "IServiceManager", "get", &_hidl_args);
        }
    }
    #endif // __ANDROID_DEBUGGABLE__

    ::android::hardware::Parcel _hidl_data;
    ::android::hardware::Parcel _hidl_reply;
    ::android::status_t _hidl_err;
    ::android::hardware::Status _hidl_status;

    ::android::sp<::android::hidl::base::V1_0::IBase> _hidl_out_service;

    _hidl_err = _hidl_data.writeInterfaceToken(BpHwServiceManager::descriptor);
    if (_hidl_err != ::android::OK) { goto _hidl_error; }

    size_t _hidl_fqName_parent;

    _hidl_err = _hidl_data.writeBuffer(&fqName, sizeof(fqName), &_hidl_fqName_parent);
    if (_hidl_err != ::android::OK) { goto _hidl_error; }

    _hidl_err = ::android::hardware::writeEmbeddedToParcel(
            fqName,
            &_hidl_data,
            _hidl_fqName_parent,
            0 /* parentOffset */);

    if (_hidl_err != ::android::OK) { goto _hidl_error; }

    size_t _hidl_name_parent;

    _hidl_err = _hidl_data.writeBuffer(&name, sizeof(name), &_hidl_name_parent);
    if (_hidl_err != ::android::OK) { goto _hidl_error; }

    _hidl_err = ::android::hardware::writeEmbeddedToParcel(
            name,
            &_hidl_data,
            _hidl_name_parent,
            0 /* parentOffset */);

    if (_hidl_err != ::android::OK) { goto _hidl_error; }

    _hidl_err = ::android::hardware::IInterface::asBinder(_hidl_this)->transact(1 /* get */, _hidl_data, &_hidl_reply);
    if (_hidl_err != ::android::OK) { goto _hidl_error; }

    _hidl_err = ::android::hardware::readFromParcel(&_hidl_status, _hidl_reply);
    if (_hidl_err != ::android::OK) { goto _hidl_error; }

    if (!_hidl_status.isOk()) { return _hidl_status; }

    {
        ::android::sp<::android::hardware::IBinder> _hidl__hidl_out_service_binder;
        _hidl_err = _hidl_reply.readNullableStrongBinder(&_hidl__hidl_out_service_binder);
        if (_hidl_err != ::android::OK) { goto _hidl_error; }

        _hidl_out_service = ::android::hardware::fromBinder<::android::hidl::base::V1_0::IBase,::android::hidl::base::V1_0::BpHwBase,::android::hidl::base::V1_0::BnHwBase>(_hidl__hidl_out_service_binder);
    }

    atrace_end(ATRACE_TAG_HAL);
    #ifdef __ANDROID_DEBUGGABLE__
    if (UNLIKELY(mEnableInstrumentation)) {
        std::vector<void *> _hidl_args;
        _hidl_args.push_back((void *)&_hidl_out_service);
        for (const auto &callback: mInstrumentationCallbacks) {
            callback(InstrumentationEvent::CLIENT_API_EXIT, "android.hidl.manager", "1.0", "IServiceManager", "get", &_hidl_args);
        }
    }
    #endif // __ANDROID_DEBUGGABLE__

    _hidl_status.setFromStatusT(_hidl_err);
    return ::android::hardware::Return<::android::sp<::android::hidl::base::V1_0::IBase>>(_hidl_out_service);

_hidl_error:
    _hidl_status.setFromStatusT(_hidl_err);
    return ::android::hardware::Return<::android::sp<::android::hidl::base::V1_0::IBase>>(_hidl_status);
}

整个调用过程和hidl服务过程完全一致,就是一个从BpHwServiceManager –> BnHwServiceManager –> ServiceManager的过程。但需要注意,BpHwServiceManager得到BnHwServiceManager返回过来的binder代理后,会通过fromBinder函数进行对象转换:

::android::hardware::fromBinder<::android::hidl::base::V1_0::IBase,::android::hidl::base::V1_0::BpHwBase,::android::hidl::base::V1_0::BnHwBase>(_hidl__hidl_out_service_binder)

hwservicemanager将IComposer的binder代理BpHwBinder发给Framework Server进程,Framework Server进程拿到的依然是IComposer的binder代理BpHwBinder对象,因此在fromBinder函数中将创建BpHwBase对象来封装BpHwBinder。

  1. ::android::status_t BnHwServiceManager::_hidl_get(  
  2.         ::android::hidl::base::V1_0::BnHwBase* _hidl_this,  
  3.         const ::android::hardware::Parcel &_hidl_data,  
  4.         ::android::hardware::Parcel *_hidl_reply,  
  5.         TransactCallback _hidl_cb) {  
  6.     #ifdef __ANDROID_DEBUGGABLE__  
  7.     bool mEnableInstrumentation = _hidl_this->isInstrumentationEnabled();  
  8.     const auto &mInstrumentationCallbacks = _hidl_this->getInstrumentationCallbacks();  
  9.     #endif // __ANDROID_DEBUGGABLE__  
  10.   
  11.     ::android::status_t _hidl_err = ::android::OK;  
  12.     if (!_hidl_data.enforceInterface(BnHwServiceManager::Pure::descriptor)) {  
  13.         _hidl_err = ::android::BAD_TYPE;  
  14.         return _hidl_err;  
  15.     }  
  16.   
  17.     const ::android::hardware::hidl_string* fqName;  
  18.     const ::android::hardware::hidl_string* name;  
  19.   
  20.     size_t _hidl_fqName_parent;  
  21.   
  22.     _hidl_err = _hidl_data.readBuffer(sizeof(*fqName), &_hidl_fqName_parent,  reinterpret_cast<const void **>(&fqName));  
  23.   
  24.     if (_hidl_err != ::android::OK) { return _hidl_err; }  
  25.   
  26.     _hidl_err = ::android::hardware::readEmbeddedFromParcel(  
  27.             const_cast<::android::hardware::hidl_string &>(*fqName),  
  28.             _hidl_data,  
  29.             _hidl_fqName_parent,  
  30.             0 /* parentOffset */);  
  31.   
  32.     if (_hidl_err != ::android::OK) { return _hidl_err; }  
  33.   
  34.     size_t _hidl_name_parent;  
  35.   
  36.     _hidl_err = _hidl_data.readBuffer(sizeof(*name), &_hidl_name_parent,  reinterpret_cast<const void **>(&name));  
  37.   
  38.     if (_hidl_err != ::android::OK) { return _hidl_err; }  
  39.   
  40.     _hidl_err = ::android::hardware::readEmbeddedFromParcel(  
  41.             const_cast<::android::hardware::hidl_string &>(*name),  
  42.             _hidl_data,  
  43.             _hidl_name_parent,  
  44.             0 /* parentOffset */);  
  45.   
  46.     if (_hidl_err != ::android::OK) { return _hidl_err; }  
  47.   
  48.     atrace_begin(ATRACE_TAG_HAL, ”HIDL::IServiceManager::get::server”);  
  49.     #ifdef __ANDROID_DEBUGGABLE__  
  50.     if (UNLIKELY(mEnableInstrumentation)) {  
  51.         std::vector<void *> _hidl_args;  
  52.         _hidl_args.push_back((void *)fqName);  
  53.         _hidl_args.push_back((void *)name);  
  54.         for (const auto &callback: mInstrumentationCallbacks) {  
  55.             callback(InstrumentationEvent::SERVER_API_ENTRY, ”android.hidl.manager”“1.0”“IServiceManager”“get”, &_hidl_args);  
  56.         }  
  57.     }  
  58.     #endif // __ANDROID_DEBUGGABLE__  
  59.   
  60.     ::android::sp<::android::hidl::base::V1_0::IBase> _hidl_out_service = static_cast<BnHwServiceManager*>(_hidl_this)->_hidl_mImpl->get(*fqName, *name);  
  61.   
  62.     ::android::hardware::writeToParcel(::android::hardware::Status::ok(), _hidl_reply);  
  63.   
  64.     if (_hidl_out_service == nullptr) {  
  65.         _hidl_err = _hidl_reply->writeStrongBinder(nullptr);  
  66.     } else {  
  67.         ::android::sp<::android::hardware::IBinder> _hidl_binder = ::android::hardware::toBinder<  
  68.                 ::android::hidl::base::V1_0::IBase>(_hidl_out_service);  
  69.         if (_hidl_binder.get() != nullptr) {  
  70.             _hidl_err = _hidl_reply->writeStrongBinder(_hidl_binder);  
  71.         } else {  
  72.             _hidl_err = ::android::UNKNOWN_ERROR;  
  73.         }  
  74.     }  
  75.     /* _hidl_err ignored! */  
  76.   
  77.     atrace_end(ATRACE_TAG_HAL);  
  78.     #ifdef __ANDROID_DEBUGGABLE__  
  79.     if (UNLIKELY(mEnableInstrumentation)) {  
  80.         std::vector<void *> _hidl_args;  
  81.         _hidl_args.push_back((void *)&_hidl_out_service);  
  82.         for (const auto &callback: mInstrumentationCallbacks) {  
  83.             callback(InstrumentationEvent::SERVER_API_EXIT, ”android.hidl.manager”“1.0”“IServiceManager”“get”, &_hidl_args);  
  84.         }  
  85.     }  
  86.     #endif // __ANDROID_DEBUGGABLE__  
  87.   
  88.     _hidl_cb(*_hidl_reply);  
  89.     return _hidl_err;  
  90. }  
::android::status_t BnHwServiceManager::_hidl_get(
        ::android::hidl::base::V1_0::BnHwBase* _hidl_this,
        const ::android::hardware::Parcel &_hidl_data,
        ::android::hardware::Parcel *_hidl_reply,
        TransactCallback _hidl_cb) {
    #ifdef __ANDROID_DEBUGGABLE__
    bool mEnableInstrumentation = _hidl_this->isInstrumentationEnabled();
    const auto &mInstrumentationCallbacks = _hidl_this->getInstrumentationCallbacks();
    #endif // __ANDROID_DEBUGGABLE__

    ::android::status_t _hidl_err = ::android::OK;
    if (!_hidl_data.enforceInterface(BnHwServiceManager::Pure::descriptor)) {
        _hidl_err = ::android::BAD_TYPE;
        return _hidl_err;
    }

    const ::android::hardware::hidl_string* fqName;
    const ::android::hardware::hidl_string* name;

    size_t _hidl_fqName_parent;

    _hidl_err = _hidl_data.readBuffer(sizeof(*fqName), &_hidl_fqName_parent,  reinterpret_cast<const void **>(&fqName));

    if (_hidl_err != ::android::OK) { return _hidl_err; }

    _hidl_err = ::android::hardware::readEmbeddedFromParcel(
            const_cast<::android::hardware::hidl_string &>(*fqName),
            _hidl_data,
            _hidl_fqName_parent,
            0 /* parentOffset */);

    if (_hidl_err != ::android::OK) { return _hidl_err; }

    size_t _hidl_name_parent;

    _hidl_err = _hidl_data.readBuffer(sizeof(*name), &_hidl_name_parent,  reinterpret_cast<const void **>(&name));

    if (_hidl_err != ::android::OK) { return _hidl_err; }

    _hidl_err = ::android::hardware::readEmbeddedFromParcel(
            const_cast<::android::hardware::hidl_string &>(*name),
            _hidl_data,
            _hidl_name_parent,
            0 /* parentOffset */);

    if (_hidl_err != ::android::OK) { return _hidl_err; }

    atrace_begin(ATRACE_TAG_HAL, "HIDL::IServiceManager::get::server");
    #ifdef __ANDROID_DEBUGGABLE__
    if (UNLIKELY(mEnableInstrumentation)) {
        std::vector<void *> _hidl_args;
        _hidl_args.push_back((void *)fqName);
        _hidl_args.push_back((void *)name);
        for (const auto &callback: mInstrumentationCallbacks) {
            callback(InstrumentationEvent::SERVER_API_ENTRY, "android.hidl.manager", "1.0", "IServiceManager", "get", &_hidl_args);
        }
    }
    #endif // __ANDROID_DEBUGGABLE__

    ::android::sp<::android::hidl::base::V1_0::IBase> _hidl_out_service = static_cast<BnHwServiceManager*>(_hidl_this)->_hidl_mImpl->get(*fqName, *name);

    ::android::hardware::writeToParcel(::android::hardware::Status::ok(), _hidl_reply);

    if (_hidl_out_service == nullptr) {
        _hidl_err = _hidl_reply->writeStrongBinder(nullptr);
    } else {
        ::android::sp<::android::hardware::IBinder> _hidl_binder = ::android::hardware::toBinder<
                ::android::hidl::base::V1_0::IBase>(_hidl_out_service);
        if (_hidl_binder.get() != nullptr) {
            _hidl_err = _hidl_reply->writeStrongBinder(_hidl_binder);
        } else {
            _hidl_err = ::android::UNKNOWN_ERROR;
        }
    }
    /* _hidl_err ignored! */

    atrace_end(ATRACE_TAG_HAL);
    #ifdef __ANDROID_DEBUGGABLE__
    if (UNLIKELY(mEnableInstrumentation)) {
        std::vector<void *> _hidl_args;
        _hidl_args.push_back((void *)&_hidl_out_service);
        for (const auto &callback: mInstrumentationCallbacks) {
            callback(InstrumentationEvent::SERVER_API_EXIT, "android.hidl.manager", "1.0", "IServiceManager", "get", &_hidl_args);
        }
    }
    #endif // __ANDROID_DEBUGGABLE__

    _hidl_cb(*_hidl_reply);
    return _hidl_err;
}

BnHwServiceManager通过ServiceManager对象查询到对应的hidl服务,返回IBase对象后,会调用toBinder函数转换为IBinder类型对象:

::android::hardware::toBinder< ::android::hidl::base::V1_0::IBase>(_hidl_out_service)

由于在hwservicemanager这边,保存的是IComposer的BpHwBase对象,因此在toBinder函数中将调用IInterface::asBinder来得到BpHwBase的成员变量中的BpHwBinder对象。

  1. if (ifacePtr->isRemote()) {  
  2.     return ::android::hardware::IInterface::asBinder(static_cast<ProxyType *>(ifacePtr));  
  3. }  
if (ifacePtr->isRemote()) {
    return ::android::hardware::IInterface::asBinder(static_cast<ProxyType *>(ifacePtr));
}

fromBinder和toBinder函数在之前的文章中已经详细分析了,这里就不再展开。

服务查询过程其实就是根据接口包名及服务名称,从hwservicemanager管理的表中查询对应的IBase服务对象,然后在Client进程空间分别创建BpHwBinder和BpHwBase对象。

接口转换

Framework Server进程通过上述hidl服务查询,得到了BpHwBase对象后,需要将其转换为与业务相关的代理对象,这就是通过:Return<sp<IComposer>> castRet = IComposer::castFrom(base, true /* emitError */);

composer\2.1\[email protected]_genc++\gen\android\hardware\graphics\composer\2.1\ComposerAll.cpp

  1. ::android::hardware::Return<::android::sp<IComposer>> IComposer::castFrom(const ::android::sp<::android::hidl::base::V1_0::IBase>& parent, bool emitError) {  
  2.     return ::android::hardware::details::castInterface<IComposer, ::android::hidl::base::V1_0::IBase, BpHwComposer>(  
  3.             parent, [email protected]::IComposer”, emitError);  
  4. }  
::android::hardware::Return<::android::sp<IComposer>> IComposer::castFrom(const ::android::sp<::android::hidl::base::V1_0::IBase>& parent, bool emitError) {
    return ::android::hardware::details::castInterface<IComposer, ::android::hidl::base::V1_0::IBase, BpHwComposer>(
            parent, "[email protected]::IComposer", emitError);
}

system\libhidl\transport\include\hidl\HidlTransportSupport.h

  1. // cast the interface IParent to IChild.  
  2. // Return nonnull if cast successful.  
  3. // Return nullptr if:  
  4. // 1. parent is null  
  5. // 2. cast failed because IChild is not a child type of IParent.  
  6. // 3. !emitError, calling into parent fails.  
  7. // Return an error Return object if:  
  8. // 1. emitError, calling into parent fails.  
  9. template<typename IChild, typename IParent, typename BpChild, typename BpParent>  
  10. Return<sp<IChild>> castInterface(sp<IParent> parent, const char *childIndicator, bool emitError) {  
  11.     if (parent.get() == nullptr) {  
  12.         // casts always succeed with nullptrs.  
  13.         return nullptr;  
  14.     }  
  15.     Return<bool> canCastRet = details::canCastInterface(parent.get(), childIndicator, emitError);  
  16.     if (!canCastRet.isOk()) {  
  17.         // call fails, propagate the error if emitError  
  18.         return emitError  
  19.                 ? details::StatusOf<bool, sp<IChild>>(canCastRet)  
  20.                 : Return<sp<IChild>>(sp<IChild>(nullptr));  
  21.     }  
  22.   
  23.     if (!canCastRet) {  
  24.         return sp<IChild>(nullptr); // cast failed.  
  25.     }  
  26.     // TODO b/32001926 Needs to be fixed for socket mode.  
  27.     if (parent->isRemote()) {  
  28.         // binderized mode. Got BpChild. grab the remote and wrap it.  
  29.         return sp<IChild>(new BpChild(toBinder<IParent, BpParent>(parent)));  
  30.     }  
  31.     // Passthrough mode. Got BnChild and BsChild.  
  32.     return sp<IChild>(static_cast<IChild *>(parent.get()));  
  33. }  
  34.   
  35. }  // namespace details  
// cast the interface IParent to IChild.
// Return nonnull if cast successful.
// Return nullptr if:
// 1. parent is null
// 2. cast failed because IChild is not a child type of IParent.
// 3. !emitError, calling into parent fails.
// Return an error Return object if:
// 1. emitError, calling into parent fails.
template<typename IChild, typename IParent, typename BpChild, typename BpParent>
Return<sp<IChild>> castInterface(sp<IParent> parent, const char *childIndicator, bool emitError) {
    if (parent.get() == nullptr) {
        // casts always succeed with nullptrs.
        return nullptr;
    }
    Return<bool> canCastRet = details::canCastInterface(parent.get(), childIndicator, emitError);
    if (!canCastRet.isOk()) {
        // call fails, propagate the error if emitError
        return emitError
                ? details::StatusOf<bool, sp<IChild>>(canCastRet)
                : Return<sp<IChild>>(sp<IChild>(nullptr));
    }

    if (!canCastRet) {
        return sp<IChild>(nullptr); // cast failed.
    }
    // TODO b/32001926 Needs to be fixed for socket mode.
    if (parent->isRemote()) {
        // binderized mode. Got BpChild. grab the remote and wrap it.
        return sp<IChild>(new BpChild(toBinder<IParent, BpParent>(parent)));
    }
    // Passthrough mode. Got BnChild and BsChild.
    return sp<IChild>(static_cast<IChild *>(parent.get()));
}

}  // namespace details

这个模板函数展开后如下:

  1. Return<sp<IComposer>> castInterface(sp<IBase> parent, const char *childIndicator, bool emitError) {  
  2.     if (parent.get() == nullptr) {  
  3.         // casts always succeed with nullptrs.  
  4.         return nullptr;  
  5.     }  
  6.     Return<bool> canCastRet = details::canCastInterface(parent.get(), childIndicator, emitError);  
  7.     if (!canCastRet.isOk()) {  
  8.         // call fails, propagate the error if emitError  
  9.         return emitError  
  10.                 ? details::StatusOf<bool, sp<IComposer>>(canCastRet)  
  11.                 : Return<sp<IComposer>>(sp<IComposer>(nullptr));  
  12.     }  
  13.   
  14.     if (!canCastRet) {  
  15.         return sp<IComposer>(nullptr); // cast failed.  
  16.     }  
  17.     // TODO b/32001926 Needs to be fixed for socket mode.  
  18.     if (parent->isRemote()) {  
  19.         // binderized mode. Got BpChild. grab the remote and wrap it.  
  20.         return sp<IComposer>(new BpHwComposer(toBinder<IBase, BpParent>(parent)));  
  21.     }  
  22.     // Passthrough mode. Got BnChild and BsChild.  
  23.     return sp<IComposer>(static_cast<IComposer *>(parent.get()));  
  24. }  
Return<sp<IComposer>> castInterface(sp<IBase> parent, const char *childIndicator, bool emitError) {
    if (parent.get() == nullptr) {
        // casts always succeed with nullptrs.
        return nullptr;
    }
    Return<bool> canCastRet = details::canCastInterface(parent.get(), childIndicator, emitError);
    if (!canCastRet.isOk()) {
        // call fails, propagate the error if emitError
        return emitError
                ? details::StatusOf<bool, sp<IComposer>>(canCastRet)
                : Return<sp<IComposer>>(sp<IComposer>(nullptr));
    }

    if (!canCastRet) {
        return sp<IComposer>(nullptr); // cast failed.
    }
    // TODO b/32001926 Needs to be fixed for socket mode.
    if (parent->isRemote()) {
        // binderized mode. Got BpChild. grab the remote and wrap it.
        return sp<IComposer>(new BpHwComposer(toBinder<IBase, BpParent>(parent)));
    }
    // Passthrough mode. Got BnChild and BsChild.
    return sp<IComposer>(static_cast<IComposer *>(parent.get()));
}

因此最终会创建一个BpHwComposer对象。

new BpHwComposer(toBinder<IBase, BpParent>(parent))

            </div>

猜你喜欢

转载自blog.csdn.net/marshal_zsx/article/details/80293203