The connection process between the application process and SurfaceFlinger

We start to analyze the connection process between the application and SurfaceFlinger from the creation of the SurfaceComposerClient object. Every application that needs SurfaceFlinger to render will create a SurfaceComposerClient object, so, I'm not sure, it needs to be verified.
The declaration of the SurfaceComposerClient class (in the SurfaceComposerClient.h file) is as follows:
class SurfaceComposerClient : public RefBase
The constructor of the SurfaceComposerClient class is defined as follows:
SurfaceComposerClient::SurfaceComposerClient()
    : mStatus(NO_INIT), mComposer(Composer::getInstance())
{
}
For a detailed analysis of the constructor of the SurfaceComposerClient class, please refer to the page2 file.
Because SurfaceComposerClient inherits from RefBase, the onFirstRef function will be called when only the pointer is referenced for the first time. Let's take a look at the implementation of the onFirstRef function of SurfaceComposerClient:
1void SurfaceComposerClient::onFirstRef() {
2 sp<ISurfaceComposer> sm (ComposerService::getComposerService());
3 if (sm != 0) {
4 sp<ISurfaceComposerClient> conn = sm->createConnection();
5 if (conn != 0) {
6 mClient = conn;
7 mStatus = NO_ERROR;
8 }
9 }
10}
Regarding the analysis of the implementation of the onFirstRef function of SurfaceComposerClient, you can Refer to page3 document.

When the onFirstRef function is called, the member variable mClient of the SurfaceComposerClient object is initialized to point to a Binder proxy object of ISurfaceComposerClient, so that the SurfaceComposerClient object can request the services of SurfaceFlinger through the ISurfaceComposerClient interface.

The above is the analysis of the connection process between the application and the SurfaceFlinger service. Through the above analysis, we can draw the following conclusions:
No.1 Every application that needs to render UI will create a SurfaceComposerClient object. Is this correct?
No. 2 Every application that needs SurfaceFlinger service will create a Client object on the SurfaceFlinger side, and get a Binder proxy object of ISurfaceComposerClient type on the application side.

The constructor of the SurfaceComposerClient class is defined as follows:
SurfaceComposerClient::SurfaceComposerClient()
    : mStatus(NO_INIT), mComposer(Composer::getInstance())
{
}
In the SurfaceComposerClient class, a member variable mComposer, whose type is a reference to the Composer class, is defined as follows:
Composer& mComposer
; What is the role? Wait a minute, let's analyze it bit by bit.
Let's take a look at the definition of the
Composer class. The declaration of the Composer class is as follows:
class Composer : public Singleton<Composer>
It can be seen that Composer is a singleton.
The constructor of Composer is defined as follows:
Composer() : Singleton<Composer>(),
        mForceSynchronous(0), mTransactionNestCount(0),
        mAnimation(false)
    {

}

We still don't know what mComposer does? Analyze it later. ^-^

 
The implementation of the onFirstRef function of SurfaceComposerClient is as follows
1void SurfaceComposerClient::onFirstRef() {
2 sp<ISurfaceComposer> sm(ComposerService::getComposerService());
3 if (sm != 0) {
4 sp<ISurfaceComposerClient> conn = sm->createConnection();
5 if (conn != 0) {
6 mClient = conn;
7 mStatus = NO_ERROR;
8 }
9 }
10}
The first line (SurfaceComposerClient::onFirstRef) first calls ComposerService::getComposerService() to get a Binder proxy object of ISurfaceComposer.
Let's look first Let's take a look at the declaration of the ComposerService class, located in the frameworks/native/include/private/gui/ComposerService.h file:
class ComposerService : public Singleton<ComposerService>
It can be seen that ComposerService is a singleton.
After analyzing the declaration of the ComposerService class, let's take a look at the implementation of the getComposerService function of ComposerService, located in the frameworks/native/libs/gui/SurfaceComposerClient.cpp file, defined as follows:
1sp<ISurfaceComposer> ComposerService::getComposerService() {
2 ComposerService& instance = ComposerService::getInstance();
3 Mutex::Autolock _l(instance.mLock);
4 if (instance.mComposerService == NULL) {
5 ComposerService::getInstance().connectLocked();
6 assert(instance.mComposerService != NULL);
7 ALOGD("ComposerService reconnected");
8 }
9 return instance.mComposerService;
10}
The main logic of the getComposerService function of ComposerService is to get the ComposerService instance and determine whether the mComposerService of the ComposerService instance is null, if it is, it means that the SurfaceFlinger service has not been obtained, then the connectLocked function of the ComposerService instance will be called to get the SurfaceFlinger service, if not null , indicates that the SurfaceFlinger service has been obtained, then return SurfaceFlinger directly.
So let's take a look at the implementation of the connectLocked function of ComposerService, located in the frameworks/native/libs/gui/SurfaceComposerClient.cpp file, defined as follows:
void ComposerService::connectLocked( ) {
    const String16 name("SurfaceFlinger");
    while (getService(name, &mComposerService) != NO_ERROR) {
usleep(250000);
    }
    assert(mComposerService != NULL);

    // Create the death listener.
    class DeathObserver : public IBinder ::DeathRecipient {
ComposerService& mComposerService;
virtual void binderDied(const wp<IBinder>& who) {
    ALOGW("ComposerService remote (surfaceflinger) died [%p]",
          who.unsafe_get());
    mComposerService.composerServiceDied();
}
     public:
DeathObserver(ComposerService& mgr) : mComposerService(mgr) { }
    };

    mDeathObserver = new DeathObserver(*const_cast<ComposerService*>(this));
    IInterface::asBinder(mComposerService)->linkToDeath(mDeathObserver);
}
In the connectLocked function of ComposerService, the main Two things are done: First, the SurfaceFlinger service is continuously obtained from the ServiceManager. Second, a Binder death notification is registered.

Let's go back to the analysis of the SurfaceComposerClient::onFirstRef function. When the SurfaceFlinger service is obtained, lines 4-8 (SurfaceComposerClient::onFirstRef) will call the SurfaceFlinger's createConnection function. The definition of the createConnection function is as follows:
1sp<ISurfaceComposerClient> SurfaceFlinger::createConnection ()
2{
3 sp<ISurfaceComposerClient> bclient;
4 sp<Client> client(new Client(this));
5 status_t err = client->initCheck();
6 if (err == NO_ERROR) {
7 bclient = client;
8 }
9 return bclient;
10}
The detailed analysis of the createConnection function can refer to the page4 file.

Line 6 (SurfaceComposerClient::onFirstRef) saves the Binder interface of the ISurfaceComposerClient returned by the createConnection function, so that the SurfaceFlinger service can be requested through this interface in the future.

The implementation of SurfaceFlinger's createConnection function is as follows:
1sp<ISurfaceComposerClient> SurfaceFlinger::createConnection()
2{
3 sp<ISurfaceComposerClient> bclient;
4 sp<Client> client(new Client(this));
5 status_t err = client->initCheck();
6 if (err == NO_ERROR) {
7 bclient = client;
8 }
9 return bclient;
10}
Line 4 (SurfaceFlinger::createConnection) first creates an object of type Client, and the incoming parameter is the SurfaceFlinger object.
The declaration of the Client class is as follows, located in the frameworks/native/services/surfaceflinger/Client.h file:
class Client : public BnSurfaceComposerClient
It can be seen that Client is a local object of Binder.
The constructor of the Client class is defined as follows, located in frameworks/native/services/surfaceflinger/Client.cpp:
1Client::Client(const sp<SurfaceFlinger>& flinger)
    2: mFlinger(flinger), mNameGenerator(1)
3{
4}
The client's constructor just points the Client's member variable mFlinger to the SurfaceFlinger service.
After the Client object is constructed, the Client's initCheck function will be called on line 5 (SurfaceFlinger::createConnection). The definition of the initCheck function is as follows:
status_t Client::initCheck() const {
return NO_ERROR;
}
You can see that the initCheck function is only Simply returns NO_ERROR.
Finally, the Client object will be returned.
It can be seen that each application connected to SurfaceFlinger will have a Client object on the SurfaceFlinger side, which is the Binder local object of ISurfaceComposerClient.
Finally, the Binder interface of the ISurfaceComposerClient is returned to the application, so that the application can use this Binder interface to request SurfaceFlinger to serve it, so that the application establishes a connection with SurfaceFlinger.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326278982&siteId=291194637