显示系统[3] APP申请创建Surface的过程

Posted we1less

tags:

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

sp<SurfaceControl> surfaceControl = client->createSurface(String8("resize"),
            160, 240, PIXEL_FORMAT_RGB_565, 0);

   调用mClient->createSurface

   值得注意的是此处是biner通信过程,实际上是通过BpSurfaceComposerClient的createSurface函数调用Client的onTransact再调用BnSurfaceComposerClientonTransact函数进而调用的

   上文说过SurfaceComposerClient中的mClient对应SurfaceFlinger进程中的Client对象

   注意 sp<IGraphicBufferProducer> gbp;  最后指的是包装后的生产者

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

sp<SurfaceControl> SurfaceComposerClient::createSurface(
        const String8& name,
        uint32_t w,
        uint32_t h,
        PixelFormat format,
        uint32_t flags,
        SurfaceControl* parent,
        uint32_t windowType,
        uint32_t ownerUid)
{
    sp<SurfaceControl> sur;
    if (mStatus == NO_ERROR) {
        sp<IBinder> handle;
        sp<IBinder> parentHandle;
        sp<IGraphicBufferProducer> gbp;

        if (parent != nullptr) {
            parentHandle = parent->getHandle();
        }
        status_t err = mClient->createSurface(name, w, h, format, flags, parentHandle,
                windowType, ownerUid, &handle, &gbp);
        ALOGE_IF(err, "SurfaceComposerClient::createSurface error %s", strerror(-err));
        if (err == NO_ERROR) {
            sur = new SurfaceControl(this, handle, gbp);
        }
    }
    return sur;
}

   这里面创建一个MessageCreateLayer并且postMessageSync出去

   同时gbp也被包装出去

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

status_t Client::createSurface(
        const String8& name,
        uint32_t w, uint32_t h, PixelFormat format, uint32_t flags,
        const sp<IBinder>& parentHandle, uint32_t windowType, uint32_t ownerUid,
        sp<IBinder>* handle,
        sp<IGraphicBufferProducer>* gbp)
{
    ...

    sp<MessageBase> msg = new MessageCreateLayer(mFlinger.get(),
            name, this, w, h, format, flags, handle,
            windowType, ownerUid, gbp, &parent);
    mFlinger->postMessageSync(msg);
    return static_cast<MessageCreateLayer*>( msg.get() )->getResult();
}

这里调用SurfaceFlingercreateLayer

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

virtual bool handler() {
            result = flinger->createLayer(name, client, w, h, format, flags,
                    windowType, ownerUid, handle, gbp, parent);
            return true;
        }

  创建一个普通的Layer

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

status_t SurfaceFlinger::createLayer(
        const String8& name,
        const sp<Client>& client,
        uint32_t w, uint32_t h, PixelFormat format, uint32_t flags,
        uint32_t windowType, uint32_t ownerUid, sp<IBinder>* handle,
        sp<IGraphicBufferProducer>* gbp, sp<Layer>* parent)
{
    ...

    switch (flags & ISurfaceComposerClient::eFXSurfaceMask) {
        case ISurfaceComposerClient::eFXSurfaceNormal:
            result = createNormalLayer(client,
                    uniqueName, w, h, flags, format,
                    handle, gbp, &layer);
            break;
       ...
    }

   ...
}

  创建了Layer

  对于app创建的每一个Surface都会与之对应创建Layer,而Layer中存在BufferQueue,在BufferQueue中则对应生产者和消费者,生产者(APP应用程序)生产数据到buffer消费者从buffer中取数据,所以APP应用程序中肯定会有生产者的代理类指向Layer中的生产者。

  同时在此处也可以看出gbp被赋值为Layer中的getProducer(),这个MonitoredProducer在下文中也进行追溯了,其实就是包装后的生产者。

sp<IGraphicBufferProducer> Layer::getProducer() const {
    return mProducer;
}

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

status_t SurfaceFlinger::createNormalLayer(const sp<Client>& client,
        const String8& name, uint32_t w, uint32_t h, uint32_t flags, PixelFormat& format,
        sp<IBinder>* handle, sp<IGraphicBufferProducer>* gbp, sp<Layer>* outLayer)
{
    // initialize the surfaces
    ...

    *outLayer = new Layer(this, client, name, w, h, flags);
    status_t err = (*outLayer)->setBuffers(w, h, format, flags);
    if (err == NO_ERROR) {
        *handle = (*outLayer)->getHandle();
        *gbp = (*outLayer)->getProducer();
    }
    ...
}

 代码里做到了创建BufferQueue,在创建BufferQueue的同时将生产者消费者同时创建好,在后面对生产者消费者进行一次封装。

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

void Layer::onFirstRef() {
    // Creates a custom BufferQueue for SurfaceFlingerConsumer to use
    sp<IGraphicBufferProducer> producer;
    sp<IGraphicBufferConsumer> consumer;
        //创建BufferQueue
    BufferQueue::createBufferQueue(&producer, &consumer, true);
        //创建生产者
    mProducer = new MonitoredProducer(producer, mFlinger, this);
        //创建消费者
    mSurfaceFlingerConsumer = new SurfaceFlingerConsumer(consumer, mTextureName, this);
    mSurfaceFlingerConsumer->setConsumerUsageBits(getEffectiveUsage(0));
    mSurfaceFlingerConsumer->setContentsChangedListener(this);
    mSurfaceFlingerConsumer->setName(mName);

    if (mFlinger->isLayerTripleBufferingDisabled()) {
        mProducer->setMaxDequeuedBufferCount(2);
    }

    const sp<const DisplayDevice> hw(mFlinger->getDefaultDisplayDevice());
    updateTransformHint(hw);
}

  创建一个BufferQueueCore  创建生产者消费者返回

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

void BufferQueue::createBufferQueue(sp<IGraphicBufferProducer>* outProducer,
        sp<IGraphicBufferConsumer>* outConsumer,
        bool consumerIsSurfaceFlinger) {
    LOG_ALWAYS_FATAL_IF(outProducer == NULL,
            "BufferQueue: outProducer must not be NULL");
    LOG_ALWAYS_FATAL_IF(outConsumer == NULL,
            "BufferQueue: outConsumer must not be NULL");
    //创建BufferQueueCore
    sp<BufferQueueCore> core(new BufferQueueCore());
    LOG_ALWAYS_FATAL_IF(core == NULL,
            "BufferQueue: failed to create BufferQueueCore");
    //创建生产者
    sp<IGraphicBufferProducer> producer(new BufferQueueProducer(core, consumerIsSurfaceFlinger));
    LOG_ALWAYS_FATAL_IF(producer == NULL,
            "BufferQueue: failed to create BufferQueueProducer");
    //创建消费者
    sp<IGraphicBufferConsumer> consumer(new BufferQueueConsumer(core));
    LOG_ALWAYS_FATAL_IF(consumer == NULL,
            "BufferQueue: failed to create BufferQueueConsumer");

    *outProducer = producer;
    *outConsumer = consumer;
}

其中包含的成员BufferQueueDefs::SlotsType mSlots;是一个数组[64]

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

BufferQueueCore::BufferQueueCore() 

  路径  frameworks/native/libs/gui/include/gui/BufferQueueDefs.h 

namespace BufferQueueDefs {
        typedef BufferSlot SlotsType[NUM_BUFFER_SLOTS];
    } // namespace BufferQueueDefs

  路径  frameworks/native/libs/ui/include/ui/BufferQueueDefs.h 

namespace BufferQueueDefs {
        // BufferQueue will keep track of at most this value of buffers.
        // Attempts at runtime to increase the number of buffers past this
        // will fail.
        static constexpr int NUM_BUFFER_SLOTS = 64;
    } // namespace BufferQueueDefs

生产者BufferQueueProducer

   mCore保存了BufferQueueCore

   mSlots(引用)指向了BufferQueueCore中的mSlots

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

BufferQueueProducer::BufferQueueProducer(const sp<BufferQueueCore>& core,
        bool consumerIsSurfaceFlinger) :
    mCore(core),
    mSlots(core->mSlots),
   ...
}

消费者BufferQueueConsumer

   mCore保存了BufferQueueCore

   mSlots(引用)指向了BufferQueueCore中的mSlots

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

BufferQueueConsumer::BufferQueueConsumer(const sp<BufferQueueCore>& core) :
    mCore(core),
    mSlots(core->mSlots),
    mConsumerName() {}

在Layer中创建好了生产者消费者,在后面对生产者消费者进行一次封装

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

void Layer::onFirstRef() {
    BufferQueue::createBufferQueue(&producer, &consumer, true);
    mProducer = new MonitoredProducer(producer, mFlinger, this);
}

成员mProducer指向生产者

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

MonitoredProducer::MonitoredProducer(const sp<IGraphicBufferProducer>& producer,
        const sp<SurfaceFlinger>& flinger,
        const wp<Layer>& layer) :
    mProducer(producer),
    mFlinger(flinger),
    mLayer(layer) {}

调用父类的GLConsumer构造函数

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

SurfaceFlingerConsumer(const sp<IGraphicBufferConsumer>& consumer,
            uint32_t tex, Layer* layer)
        : GLConsumer(consumer, tex, GLConsumer::TEXTURE_EXTERNAL, false, false),
          mTransformToDisplayInverse(false), mSurfaceDamage(), mLayer(layer)
    {}

调用父类的ConsumerBase构造函数 

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

GLConsumer::GLConsumer(const sp<IGraphicBufferConsumer>& bq, uint32_t tex,
        uint32_t texTarget, bool useFenceSync, bool isControlledByApp) :
    ConsumerBase(bq, isControlledByApp),
   ...
}

成员mConsumer指向消费者

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

ConsumerBase::ConsumerBase(const sp<IGraphicBufferConsumer>& bufferQueue, bool controlledByApp) :
        mAbandoned(false),
        mConsumer(bufferQueue),
    ...
}

  回到开始创建 SurfaceComposerClient::createSurface的时候

  这里利用(包装类生产者代理类)gbp创建一个SurfaceControl

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

if (err == NO_ERROR) {
    sur = new SurfaceControl(this, handle, gbp);
}
sp<Surface> surface = surfaceControl->getSurface();

这里创建了Surface而传给SurfacemGraphicBufferProducer就是上文提到的IGraphicBufferProducer(包装类生产者代理类)gbp

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

SurfaceControl::SurfaceControl(
        const sp<SurfaceComposerClient>& client,
        const sp<IBinder>& handle,
        const sp<IGraphicBufferProducer>& gbp)
    : mClient(client), mHandle(handle), mGraphicBufferProducer(gbp)
{
}

sp<Surface> SurfaceControl::getSurface() const
{
    Mutex::Autolock _l(mLock);
    if (mSurfaceData == 0) {
        return generateSurfaceLocked();
    }
    return mSurfaceData;
}

sp<Surface> SurfaceControl::generateSurfaceLocked() const
{
    // This surface is always consumed by SurfaceFlinger, so the
    // producerControlledByApp value doesn't matter; using false.
    mSurfaceData = new Surface(mGraphicBufferProducer, false);

    return mSurfaceData;
}

以上是关于显示系统[3] APP申请创建Surface的过程的主要内容,如果未能解决你的问题,请参考以下文章

Android系统_Surface创建过程分析

「Android」 Surface分析

Android图形系统(六)-app与SurfaceFlinger服务连接过程

Android 图形架构 之三—— 创建LayerSurfaceSurfaceControl

Chromium网页绘图表面(Output Surface)创建过程分析

Android 图形架构 之三—— 创建LayerSurfaceSurfaceControl