android P SurfaceFlinger---图层创建过程

由上节的内容可知,SurfaceComposerClient是应用程序与surfaceflinger之间的通信桥梁,不过SurfaceComposerClient只是一个封装,实质是通过内部的ISurfaceComposerClient来执行的,ISurfaceComposerClient对应的服务端实现是Client。

SurfaceComposerClient.h
sp  mClient;

SurfaceComposerClient.cpp
void SurfaceComposerClient::onFirstRef() {
    sp sf(ComposerService::getComposerService());
    if (sf != nullptr && mStatus == NO_INIT) {
        sp conn;
        conn = sf->createConnection();
        if (conn != nullptr) {
            mClient = conn;
            mStatus = NO_ERROR;
        }
    }
}

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

Client类的继承关系
class Client : public BnSurfaceComposerClient
class BnSurfaceComposerClient : public SafeBnInterface

Client将应用程序创建Surface的请求转交给SurfaceFlinger。

Client.cpp
status_t Client::createSurface(const String8& name, uint32_t w, uint32_t h, PixelFormat format,
                               uint32_t flags, const sp& parentHandle,
                               LayerMetadata metadata, sp* handle,
                               sp* gbp) {
    // We rely on createLayer to check permissions.
    return mFlinger->createLayer(name, this, w, h, format, flags, std::move(metadata), handle, gbp,
                                 parentHandle);
}

SurfaceFlinger.cpp
status_t SurfaceFlinger::createLayer(const String8& name, const sp& client, uint32_t w,
                                     uint32_t h, PixelFormat format, uint32_t flags,
                                     LayerMetadata metadata, sp* handle,
                                     sp* gbp,
                                     const sp& parentHandle,
                                     const sp& parentLayer) {Layer
    ...

    status_t result = NO_ERROR;

    sp layer;

    ...

    switch (flags & ISurfaceComposerClient::eFXSurfaceMask) {
        case ISurfaceComposerClient::eFXSurfaceBufferQueue:
            result = createBufferQueueLayer(client, uniqueName, w, h, flags, std::move(metadata),
                                            format, handle, gbp, &layer);

            break;
        case ISurfaceComposerClient::eFXSurfaceBufferState:
            result = createBufferStateLayer(client, uniqueName, w, h, flags, std::move(metadata),
                                            handle, &layer);
            break;
        case ISurfaceComposerClient::eFXSurfaceColor:
            // check if buffer size is set for color layer.
            if (w > 0 || h > 0) {
                ALOGE("createLayer() failed, w or h cannot be set for color layer (w=%d, h=%d)",
                      int(w), int(h));
                return BAD_VALUE;
            }

            result = createColorLayer(client, uniqueName, w, h, flags, std::move(metadata), handle,
                                      &layer);
            break;
        case ISurfaceComposerClient::eFXSurfaceContainer:
            // check if buffer size is set for container layer.
            if (w > 0 || h > 0) {
                ALOGE("createLayer() failed, w or h cannot be set for container layer (w=%d, h=%d)",
                      int(w), int(h));
                return BAD_VALUE;
            }
            result = createContainerLayer(client, uniqueName, w, h, flags, std::move(metadata),
                                          handle, &layer);
            break;
        default:
            result = BAD_VALUE;
            break;
    }

    ...
    result = addClientLayer(client, *handle, *gbp, layer, parentHandle, parentLayer,
                            addToCurrentState);
    ...
    return result;
}

该函数中根据flag创建不同的Layer,Layer用于标示一个图层。SurfaceFlinger为应用程序创建好Layer后,需要统一管理这些Layer对象,因此通过函数addClientLayer将创建的Layer保存到当前State的Z秩序列表layersSortedByZ中,同时将这个Layer所对应的IGraphicBufferProducer本地Binder对象gbp保存到SurfaceFlinger的成员变量mGraphicBufferProducerList中。除了SurfaceFlinger需要统一管理系统中创建的所有Layer对象外,专门为每个应用程序进程服务的Client也需要统一管理当前应用程序进程所创建的Layer,因此在addClientLayer函数里还会通过Client::attachLayer将创建的Layer和该类对应的handle以键值对的方式保存到Client的成员变量mLayers表中。

SurfaceFlinger.cpp
status_t SurfaceFlinger::addClientLayer(const sp& client, const sp& handle,
                                        const sp& gbc, const sp& lbc,
                                        const sp& parentHandle,
                                        const sp& parentLayer, bool addToCurrentState) {
    // add this layer to the current state list
    {
        ...
        if (parent == nullptr && addToCurrentState) {
            mCurrentState.layersSortedByZ.add(lbc);
        } else if (parent == nullptr) {
            lbc->onRemovedFromCurrentState();
        } else if (parent->isRemovedFromCurrentState()) {
            parent->addChild(lbc);
            lbc->onRemovedFromCurrentState();
        } else {
            parent->addChild(lbc);
        }

        if (gbc != nullptr) {
            mGraphicBufferProducerList.insert(IInterface::asBinder(gbc).get());
            ...
        }
        ...
    }

    // attach this layer to the client
    client->attachLayer(handle, lbc);

    return NO_ERROR;
}

Client.cpp
void Client::attachLayer(const sp& handle, const sp& layer)
{
    Mutex::Autolock _l(mLock);
    mLayers.add(handle, layer);
}

以createBufferQueueLayer为例,继续后续过程的分析。

SurfaceFlinger.cpp
status_t SurfaceFlinger::createBufferQueueLayer(const sp& client, const String8& name,
                                                uint32_t w, uint32_t h, uint32_t flags,
                                                LayerMetadata metadata, PixelFormat& format,
                                                sp* handle,
                                                sp* gbp,
                                                sp* outLayer) {
    // initialize the surfaces
    switch (format) {
    case PIXEL_FORMAT_TRANSPARENT:
    case PIXEL_FORMAT_TRANSLUCENT:
        format = PIXEL_FORMAT_RGBA_8888;
        break;
    case PIXEL_FORMAT_OPAQUE:
        format = PIXEL_FORMAT_RGBX_8888;
        break;
    }

    sp layer = getFactory().createBufferQueueLayer(
            LayerCreationArgs(this, client, name, w, h, flags, std::move(metadata)));
    status_t err = layer->setDefaultBufferProperties(w, h, format);
    if (err == NO_ERROR) {
        *handle = layer->getHandle();
        *gbp = layer->getProducer();
        *outLayer = layer;
    }

    ...
    return err;
}

SurfaceFlingerFactory.cpp
sp createBufferQueueLayer(const LayerCreationArgs& args) override {
    return new BufferQueueLayer(args);
}

BufferQueueLayer.cpp
void BufferQueueLayer::onFirstRef() {
    BufferLayer::onFirstRef();

    // Creates a custom BufferQueue for SurfaceFlingerConsumer to use
    sp producer;
    sp consumer;
    BufferQueue::createBufferQueue(&producer, &consumer, true);
    mProducer = new MonitoredProducer(producer, mFlinger, this);
    {
        // Grab the SF state lock during this since it's the only safe way to access RenderEngine
        Mutex::Autolock lock(mFlinger->mStateLock);
        mConsumer =
                new BufferLayerConsumer(consumer, mFlinger->getRenderEngine(), mTextureName, this);
    }
    mConsumer->setConsumerUsageBits(getEffectiveUsage(0));
    mConsumer->setContentsChangedListener(this);
    mConsumer->setName(mName);

    // BufferQueueCore::mMaxDequeuedBufferCount is default to 1
    if (!mFlinger->isLayerTripleBufferingDisabled()) {
        mProducer->setMaxDequeuedBufferCount(2);
    }

    if (const auto display = mFlinger->getDefaultDisplayDevice()) {
        updateTransformHint(display);
    }
}

BufferQueue.cpp
void BufferQueue::createBufferQueue(sp* outProducer,
        sp* outConsumer,
        bool consumerIsSurfaceFlinger) {
    ...
    sp core(new BufferQueueCore());
    ...
    sp producer(new BufferQueueProducer(core, consumerIsSurfaceFlinger));
    ...
    sp consumer(new BufferQueueConsumer(core));
    ...
    *outProducer = producer;
    *outConsumer = consumer;
}

1) BufferQueue

可以认为BufferQueue是一个服务中心,IGraphicBufferProducer和IGraphicBufferConsumer
所需要使用的buffer必须要通过它来管理。比如说当IGraphicBufferProducer想要获取一个buffer时,它不能越过BufferQueue直接与IGraphicBufferConsumer进行联系,反之亦然。这有点像房产中介一样,房主与买方的任何交易都需要经过中介的同意,私自达成的协议都是违反规定的
2) IGraphicBufferProducer

IGraphicBufferProducer就是“填充”buffer空间的人,通常情况下是应用程序。因为应用程序不断地刷新UI,从而将产生的显示数据源源不断地写到buffer中。当IGraphicBufferProducer需要使用一块buffer时,它首先会向中介BufferQueue发起dequeueBuffer申请,然后才能对指定的buffer进行操作。此时buffer就只属于IGraphicBufferProducer一个人的了,它可以对buffer进行任何必要的操作,而IGraphicBufferConsumer此刻绝不能操作这块buffer。当IGraphicBufferProducer认为一块buffer已经写入完成后,它进一步调用queueBuffer函数。从字面上看这个函数是“入列”的意思,形象地表达了buffer此时的操作,把buffer归还到BufferQueue的队列中。一旦queue成功后,buffer的owner也就随之改变为BufferQueue了。
3) IGraphicBufferConsumer

IGraphicBufferConsumer是与IGraphicBufferProducer相对应的,它的操作同样受到BufferQueue的管控。当一块buffer已经就绪后,IGraphicBufferConsumer就可以开始工作了。

 

你可能感兴趣的:(android P SurfaceFlinger---图层创建过程)