2.类的架构

作者: Wi1ls努力努力再努力 | 来源:发表于2019-07-02 09:25 被阅读0次

    支持 Binder 通讯的架构在 Java 和 native 是不同的架构。
    先来看 native 的架构,以 SurfaceFlinger 为例先来看类的继承,先来看本地服务。

    //SurfaceFlinger.h
    class SurfaceFlinger : public BnSurfaceComposer
    
    //ISurfaceComposer.h
    class BnSurfaceComposer : public BnInterface<ISurfaceComposer>
    class ISurfaceComposer : public IInterface
    
    //IInterface.h
    template<typename INTERFACE>
    class BnInterface : public INTERFACE, public BBinder
    
    //Binder.h
    class BBinder : public IBinder
    
    //IBinder.h
    class IBinder : public virtual RefBase
    
    //IInterface.h
    class IInterface : public virtual RefBase
    
    SurfaceFlinger 服务端的架构

    接下来看提供给 Client 使用的代理类

    //ISurfaceComposer.cpp
    class BpSurfaceComposer : public BpInterface<ISurfaceComposer>
    
    //IInterface.cpp
    template<typename INTERFACE>
    class BpInterface : public INTERFACE, public BpRefBase
    
    //Binder.h
    class BpRefBase : public virtual RefBase
    
    SurfaceFlinger Client 端的架构

    在 IInterface.h 中有两个重要的宏

    #define DECLARE_META_INTERFACE(INTERFACE)                               \
        static const android::String16 descriptor;                          \
        static android::sp<I##INTERFACE> asInterface(                       \
                const android::sp<android::IBinder>& obj);                  \
        virtual const android::String16& getInterfaceDescriptor() const;    \
        I##INTERFACE();                                                     \
        virtual ~I##INTERFACE();                                            \
    
    
    #define IMPLEMENT_META_INTERFACE(INTERFACE, NAME)                       \
        const android::String16 I##INTERFACE::descriptor(NAME);             \
        const android::String16&                                            \
                I##INTERFACE::getInterfaceDescriptor() const {              \
            return I##INTERFACE::descriptor;                                \
        }                                                                   \
        android::sp<I##INTERFACE> I##INTERFACE::asInterface(                \
                const android::sp<android::IBinder>& obj)                   \
        {                                                                   \
            android::sp<I##INTERFACE> intr;                                 \
            if (obj != NULL) {                                              \
                intr = static_cast<I##INTERFACE*>(                          \
                    obj->queryLocalInterface(                               \
                            I##INTERFACE::descriptor).get());               \
                if (intr == NULL) {                                         \
                    intr = new Bp##INTERFACE(obj);                          \
                }                                                           \
            }                                                               \
            return intr;                                                    \
        }                                                                   \
        I##INTERFACE::I##INTERFACE() { }                                    \
        I##INTERFACE::~I##INTERFACE() { }  
    

    这两个宏在进行本地类和代理类转换的时候异常重要,以 SurfaceFlinger 服务为例。在 ISurfaceComposer.h 中申明了DECLARE_META_INTERFACE,在ISurfaceComposer.cpp 中申明了IMPLEMENT_META_INTERFACE

    //ISurfaceComposer.h
    class ISurfaceComposer : public IInterface {
    public:
      DECLARE_META_INTERFACE(SurfaceComposer);
    ...
    }
    
    //ISurfaceComposer.cpp
    IMPLEMENT_META_INTERFACE(SurfaceComposer, "android.ui.ISurfaceComposer");
    

    进行宏替换后

    //ISurfaceComposer.h
    class ISurfaceComposer : public IInterface {
    public:
      static const android::String16 descriptor;
      static android::sp<ISurfaceComposer> asInterface(const android::sp<android::IBinder>& obj);
      virtual const android::String16& getInterfaceDescriptor() const;
      ISurfaceComposer( );
      virtual ~ISurfaceComposer 
      ...
    }
    //在ISurfaceComposer.cpp中实现
    const android::String16 ISurfaceComposer::descriptor("android.ui.ISurfaceComposer");
    const android::String16& ISurfaceComposer::getInterfaceDescriptor( ) const {
      return ISurfaceComposer::descriptor;
    }
    android:sp<ISurfaceComposer> ISurfaceComposer::asInterface(const android::sp<android::IBinder>& obj) {
      android::sp<ISurfaceComposer> intr;
      if (obj != NULL ) {
        intr = static_Cast<ISurfaceComposer*>(obj->queryLocalInterface(ISurfaceComposer::descriptor).get( ));
        if( intr == NULL) {
          intr = new BpSurfaceComposer(obj);
        }
      }
      return intr;  
    }
    
    //其中几个方法
    //IInterface.h
    inline sp<IInterface> BnInterface<ISurfaceComposer::queryLocakInterface>(const String16& _descritprot) {
      if(_descriptor == ISurfaceComposer::descriptor) return this;
      return NULL;
    }
    
    inline const String16& BnInterface<ISurfaceComposer>::getInterfaceDescriptor() const
    {
      return ISurfaceComposer::getInterfaceDescriptor( );
    }
    
    IBinder* BnInterface<ISurfaceComposer>::onAsBinder( )
    {
      return this;
    }
    
    ---
    
    inline BpInterface<ISurfaceComposer>::BpInterface(const sp<IBinder>& remote)
        : BpRefbase(remote)
    {
    }
    
    inline IBinder* BpInterface<ISurfaceComposer>::onAsBinder(){
      return remote( );
    }
    

    有了这些数据结构和重要的函数作为基础,下面就方便我们进行分析了

    相关文章

      网友评论

        本文标题:2.类的架构

        本文链接:https://www.haomeiwen.com/subject/xyyacctx.html