显示系统[2] APP创建SurfaceFlinger客户端(client)的过程

Posted we1less

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了显示系统[2] APP创建SurfaceFlinger客户端(client)的过程相关的知识,希望对你有一定的参考价值。

分析上一篇例子应用程序发创建client 

对于创建client  首先是要获得  SurfaceFlinger服务 其次通过binder调用其中函数创建 


SurfaceFlinger服务对应的应用程序代码

  路径frameworks/native/services/surfaceflinger/main_surfaceflinger.cpp

int main(int, char**) {
    ...

    // instantiate surfaceflinger
    sp<SurfaceFlinger> flinger = new SurfaceFlinger();
    ...
// publish surface flinger
    sp<IServiceManager> sm(defaultServiceManager());
    sm->addService(String16(SurfaceFlinger::getServiceName()), flinger, false);
    ...
}

 SurfaceFlinger是由BnSurfaceComposer派生出来的也就是它的子类

  路径  frameworks/native/services/surfaceflinger/SurfaceFlinger.cpp

SurfaceFlinger::SurfaceFlinger()
    :   BnSurfaceComposer(),

 BnSurfaceComposer继承于ISurfaceComposer接口

BnSurfaceComposer 路径   frameworks/native/libs/gui/include/gui/ISurfaceComposer.h

class BnSurfaceComposer: public BnInterface<ISurfaceComposer> {

 由此可以看出ISurfaceComposer派生出两个binder通信接口

    BnSurfaceComposer  、 BpSurfaceComposer

    而BnSurfaceComposer则派生出了SurfaceFlinger 

    这样app端就可利用BpSurfaceComposer远程调用到SurfaceFlinger中的函数


sp<SurfaceComposerClient> client = new SurfaceComposerClient();

 sp<SurfaceComposerClient>智能指针在初始化时调用

  路径  frameworks/native/libs/gui/SurfaceComposerClient.cpp 

void SurfaceComposerClient::onFirstRef() {
    //获得servicemanager
    sp<ISurfaceComposer> sm(ComposerService::getComposerService());
    if (sm != 0) {
        auto rootProducer = mParent.promote();
        sp<ISurfaceComposerClient> conn;
        conn = (rootProducer != nullptr) ? 
               sm->createScopedConnection(rootProducer) :
               sm->createConnection();
        if (conn != 0) {
            mClient = conn;
            mStatus = NO_ERROR;
        }
    }
}

   ComposerService::getInstance().connectLocked(); 

/*static*/ sp<ISurfaceComposer> ComposerService::getComposerService() {
    ComposerService& instance = ComposerService::getInstance();
    Mutex::Autolock _l(instance.mLock);
    if (instance.mComposerService == NULL) {
        ComposerService::getInstance().connectLocked();
        assert(instance.mComposerService != NULL);
        ALOGD("ComposerService reconnected");
    }
    return instance.mComposerService;
}
void ComposerService::connectLocked() {
    const String16 name("SurfaceFlinger");
    while (getService(name, &mComposerService) != NO_ERROR) {
        usleep(250000);
    }
...
}

  通过调用 connectLocked() 获取SurfaceFlinger服务保存到mComposerService

  再通过上方ComposerService::getComposerService()函数返回

  所以app端调用SurfaceComposerClient::onFirstRef()时获得的sm就就等于                                 BpSurfaceComposer代理类对象,进而可以用这个对象进行binder通信调用SurfaceFlinger服    务中的函数

  接下来调用createConnection()

sp<ISurfaceComposer> sm(ComposerService::getComposerService());
...
sm->createConnection();

createConnection主要的作用就是利用BpSurfaceComposer代理类对象 构造数据进行binder通信调用CREATE_CONNECTION

这里面就涉及到binder通信机制了这里可以参考这个例子https://blog.csdn.net/we1less/article/details/118346184?spm=1001.2014.3001.5501

这里面可以通过binder驱动调用到SurfaceFlinger中的onTransact函数

路径  frameworks/native/libs/gui/ISurfaceComposer.cpp

class BpSurfaceComposer : public BpInterface<ISurfaceComposer>
{
    ...
    virtual sp<ISurfaceComposerClient> createConnection()
    {
        Parcel data, reply;
        data.writeInterfaceToken(ISurfaceComposer::getInterfaceDescriptor());
        remote()->transact(BnSurfaceComposer::CREATE_CONNECTION, data, &reply);
        return interface_cast<ISurfaceComposerClient>(reply.readStrongBinder());
    }
...
}

这里又调用了父类的onTransact函数

路径  frameworks/native/services/surfaceflinger/SurfaceFlinger.cpp 

status_t SurfaceFlinger::onTransact(
    uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
{
    status_t credentialCheck = CheckTransactCodeCredentials(code);
    if (credentialCheck != OK) {
        return credentialCheck;
    }

    status_t err = BnSurfaceComposer::onTransact(code, data, reply, flags);
...
}

这里又调用到子类SurfaceFlingercreateConnection()并且作为binder对象返回

路径  frameworks/native/libs/gui/ISurfaceComposer.cpp

status_t BnSurfaceComposer::onTransact(
    uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
{
    switch(code) {
        case CREATE_CONNECTION: {
            CHECK_INTERFACE(ISurfaceComposer, data, reply);
            sp<IBinder> b = IInterface::asBinder(createConnection());
            reply->writeStrongBinder(b);
            return NO_ERROR;
        }
 ...
}

 这里面就是创建一个client对象然后把这个对象返回回去

  最后再发给BpSurfaceComposer转换成ISurfaceComposerClient返回

  return interface_cast<ISurfaceComposerClient>(reply.readStrongBinder());

  路径  frameworks/native/services/surfaceflinger/SurfaceFlinger.cpp

sp<ISurfaceComposerClient> SurfaceFlinger::createConnection() {
    return initClient(new Client(this));
}

最后返回的Client

  路径  frameworks/native/services/surfaceflinger/Client.h

class Client : public BnSurfaceComposerClient

 BnSurfaceComposerClient继承自ISurfaceComposerClient

 由此可以看出ISurfaceComposerClient派生出两个binder通信接口

    BnSurfaceComposerClient  、 BpSurfaceComposerClient

    而BnSurfaceComposerClient则派生出了Client 

   最后这个Client通过binder发送回SurfaceComposerClient中的mClient = conn;保存

   这个mClient就指向SurfaceFlinger进程中的Client对象

class BnSurfaceComposerClient : public SafeBnInterface<ISurfaceComposerClient> 

以上是关于显示系统[2] APP创建SurfaceFlinger客户端(client)的过程的主要内容,如果未能解决你的问题,请参考以下文章

django 搭建博客系统之创建博客的数据库模型

Android 进阶——系统启动之SystemServer创建并启动PackageManagerService服务

Android 进阶——系统启动之SystemServer创建并启动PackageManagerService服务

Android 进阶——系统启动之SystemServer创建并启动PackageManagerService服务

Android 进阶——系统启动之SystemServer创建并启动PackageManagerService服务

crm 系统项目 admin 后台操作表格