Android显示系统 SurfaceFlinger内部机制 2 AP如何创建SurfaceFlinger Client

韦东山 Android Framework 笔记

 

4-2 APP创建SurfaceFlinger client的过程

 

Surface_test的第一句就是:

sp<SurfaceComposerClient> client = new SurfaceComposerClient();

看看它的过程,做了时序图001_create_client

 

通过上面的学习知道,每个使用SF的程序,在SF端都有一个client对应AP,client的创建是AP端发起的,

sp<SurfaceComposerClient> client = new SurfaceComposerClient();

这个client是一个smart point,会导致类的onFirstRef被调用,

它的构造函数什么都没做,重要的在onFirstRef里面

//framework/native/libs/gui/SurfaceComposerClient.cpp
void SurfaceComposerClient::onFirstRef() {
    sp<ISurfaceComposer> sm(ComposerService::getComposerSerivce());
    if(sm != 0) {
        sp<ISurfaceComposerClient> conn = sm->createConnection();
        if(conn != 0) {
            mClient = conn;
            mStatus = NO_ERROR;
        }
    }
}

 

想象一下怎么创建client,

1,获得SurfaceFlinger服务,

2,使用SurfaceFlinger的服务,binder远程调用它的函数来创建client,

 

SurfaceFlinger服务对应 的应用程序是main_surfaceflinger.cpp

//framework/native/services/surfaceflinger/main_surafceflinger.cpp
int main(int, char**){
    //limit number of binder threads to 4;
    ProcessState::self()->setThreadPoolMaxThreadCount(4);
    //start thread pool
    sp<ProcessState> ps(ProcessState::self());
    ps->startThreadPool();
    
    //instantiate示例 surfaceflinger
    //看看这个构造函数
    sp<SUrfaceFlinger> flinger = new SurfaceFlinger();
    
    //initialize before clients can connect
    flinger->init();
    
    //public surfaceflinger
    sp<IServiceManager> sm(defaultServiceManager());
    sm->addService(String16(SurfaceFLinger::getServiceName()),flinger, false);
    
    //run surface flinger in this thread
    flinger->run();
    return 0;
}

看看SurfaceFlinger类
//framework/native/services/surfaceflinger/SurfaceFlinger.h
class SurfaceFlinger : public BnSurfaceComposer, //继承自BnSurfaceComposer
    private IBinder::DeathRecipient,
    private HWComposer::EventHandler
{
    
}    

看看BnSurfaceComposer,他们遵循ISurfaceComposer接口
//framework/native/include/gui/ISurfaceComposer.h
class BnSurfaceComposer : public BnInterface<ISurfaceComposer> {
    
}

 

 

看类图:

ISurfaceComposer 是一个接口:

一定会有native实现BnSurfaceComposer,BnSurfaceComposer派生了SurfaceFlinger

getService的时候一定会得到一个代理类:BpSurfacecomposer,

 

以前分析binder驱动的时候已经有了这些根深蒂固的概念:接口,本地实现,代理类

回头看看SurfaceComposerClient::onFirstRef

//framework/native/libs/gui/SurfaceComposerClient.cpp
void SurfaceComposerClient::onFirstRef() {
    //1,获得sm,就是ServiceManager?通过它拿到SF的句柄
    //getComposerService得到的肯定是BpSurfaceComposer的实例化对象,看看这个函数
    sp<ISurfaceComposer> sm(ComposerService::getComposerSerivce());
    if(sm != 0) {
        //2,createConnection,通过它创建client,这是一个跨进程的函数,看看它做了什么
        sp<ISurfaceComposerClient> conn = sm->createConnection();
        if(conn != 0) {
            mClient = conn;
            mStatus = NO_ERROR;
        }
    }
}

//看看getComposerService如何通过getService拿到SF服务的
//framework/native/libs/gui/SuraceComposerClient.cpp
sp<ISurfaceComposer> ComposerSerivce::getComposerService() {
    ComposerService& instance = ComposerService::getInstance();
    if(instance.mComposerSerive == NULL) {
        //看看这个connectLocked
        ComposerSerice::getInstance().connectLocked();
    }
    return instance.mComposerService;
}

//看看这个connectLocked,
//它里面做了getService,服务保存在mComposerService里面
//framework/native/libs/gui/SuraceComposerClient.cpp
void ComposerService::connectLocked() {
    const String16 name("SurfaceFlinger");
    while(getService(name, &mComposerService) != NO_ERROR) {
        usleep(250000);
    }
    assert(mComposerService != NULL);

}

//慕课网分析过getService?
//framework/native/include/binder/IServiceManager.h
status_t getService(const String16& name, sp<INTERFACE>* outService)
{
    const sp<IServiceManager> sm = defaultServiceManager();
    if(sm != NULL) {
        *outService = interface_cast<INTERFACE>(sm->getService(name));
    }
}

 

下面看2,createConnection,跨进程调用

它是sp<ISurfaceComposer>类的函数,所以ISurfaceComposer接口里面,一定有代理类,

代理类做的就是,构造好数据,发起binder远程调用,

//framework/natrive/libs/gui/ISurfaceComposer.cpp
class BpSurfaceComposer : public BpInterface<ISurfaceComposer>
{
public:
    vietual sp<ISurfaceComposerClient> createConnection()
    {
        Parcel data, reply;
        data.writeInterfaceToken(ISurfaceComposer::getInterfaceDescriptor());
        //code是CRAETE_CONNECTION,。
        remote()->transact(BnSurfaceComposer::CREATE_CONNECTION, data, &replay);
        //AP端得到reply,会把reply的binder对象转换成ISurfaceComposerClient对象,这既是一个Bp代理类
        return interface_cast<ISurfaceComposerClient>(reply.readStrongBinder());
    }
}

 

这会导致SF端的native实现被调用

先走到SurfaceFLinger::onTransact

//framework/native/servivces/surfaceflinger/SurfaceFlinger.cpp
status_t SurfaceFlinger::onTransact(
    uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
{
    switch (code) {
        case CREATE_CONNECTION:
    }
    //BnSurfaceComposer是它的父类,这里调用了父类的onTransact
    status_t err = BnSurfaceComposer::onTransact(code, data, reply, flags);
}    

//framework/native/libs./gui/ISurfaceComposer.cpp
status_t BnSurfaceComposer::onTransact(
    uint32)t code, const Parcel& data, Parcel* replay, uint32_t flags)
{
    switch(code) {
        case CREATE_CONNECTION: {
            //SF实现了createConnection,根据多态性,它会调用到子类的对应函数。
            //把ISurfaceComposerClient类型的client赋给了Binder对象,然后reply给AP
            sp<IBinder> b = IInterface::asBinder(createConnection());
            reply->writeStrongBinder(b);
            reutrn NO_ERROR;
        }
    }
}    

//回到子类SurfaceFlinger,看看它的createConnection的实现
sp<ISurfaceComposerClient> SurfaceFlinger::createConnection()
{
    sp<ISurfaceComposerClient> bclinet;
    sp<Client> client(new Client(this));
    bcclinet = client;
    return bclinet;
}

//看看这个client是什么类型的对象,它的父类是BnSurfaceComposerClient
//framework/native/serivces/Surfaceflinger/Client.h
class Client : public BnSurfaceComposerClient
{
    
}

 

//SF实现了createConnection,根据多态性,它会调用到子类的对应函数。

CreateConnection过程:

APP:

BpSurfaceComposer:构造函数,发起binder调用:remote->transact()。

 

这导致SF进程中,SurfaceFlinger类的onTransact被调用,

它进而调用父类BnSurfaceComposer的onTransact,

BnSurfaceComposer调用子类的createConnection,然后把ISurfaceComposerClient的client给binder的reply:replay->writeStrongBinder(b);

 

然后AP端:

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

转换为BpSurfaceComposerClient代理类,

以后AP端调用client里面的相关函数如client->createSurface,

 

回到这个套路:

ISurfaceComposerClient是一个接口,

本地实现是BnSurfaceComposerClient,它派生出Client类,

代理类是BpSurfaceComposerClient,

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值