Android系统_Surface创建过程分析
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Android系统_Surface创建过程分析相关的知识,希望对你有一定的参考价值。
参考技术A WMS添加window过程,最后会执行到session.windowAddedLocked创建 SurfaceSession 对象,并将当前 Session 添加到 WMS.mSessions 成员变量
SurfaceSession 的创建会调用 JNI,在 JNI 调用 nativeCreate()
构造了一个SurfaceComposerClient对象。并返回它的指针。这个对象一个应用程序就有一个,它是应用程序与SurfaceFlinger沟通的桥梁
通过SurfaceFlinger创造了一个Client对象,每一个APP都有一个Client对象向对应,通过这个代理对象可以跟SurfaceFlinger通信
构造了一个Client对象,Client实现了ISurfaceComposerClient接口。是一个可以跨进程通信的aidl对象。除此之外它还可以创建Surface,并且维护一个应用程序的所有Layer
一个ViewRootImpl就对应一个Surface
直接看ViewRootImpl的绘制流程
winAnimator.createSurfaceLocked实际上是创建了一个SurfaceControl。即上面是先构造SurfaceControl,然后在构造Surface
通过SurfaceControl的构造函数创建了一个SurfaceControl对象,这个对象的作用其实就是负责维护Surface,Surface其实也是由这个对象负责创建的
创建时传入了一个对象 sp<IGraphicBufferProducer> gbp, 后面会说吗应用所渲染的每一帧,实际上都会添加到IGraphicBufferProducer中,来等待SurfaceFlinger的渲染
Client将应用程序创建Surface的请求转换为异步消息投递到SurfaceFlinger的消息队列中,将创建Surface的任务转交给SurfaceFlinger,因为同一时刻可以有多个应用程序请求SurfaceFlinger为其创建Surface,通过消息队列可以实现请求排队,然后SurfaceFlinger依次为应用程序创建Surface
该函数中根据flag创建不同的Layer,Layer用于标示一个图层。
除了SurfaceFlinger需要统一管理系统中创建的所有Layer对象外,专门为每个应用程序进程服务的Client也需要统一管理当前应用程序进程所创建的Layer,因此在addClientLayer函数里还会通过Client::attachLayer将创建的Layer和该类对应的handle以键值对的方式保存到Client的成员变量mLayers表中
SurfaceFlinger为应用程序创建好Layer后,需要统一管理这些Layer对象,因此通过函数addClientLayer将创建的Layer保存到当前State的Z秩序列表layersSortedByZ中,同时将这个Layer所对应的IGraphicBufferProducer本地Binder对象gbp保存到SurfaceFlinger的成员变量mGraphicBufferProducerList中
上面完成了 surfaceController的创建跟踪,下面分析从surfaceController获取surface过程
JNI构建方法获取到一个指针,
创建一个native层的Surface对象,并将该对象指针返回给Java层的Surface,从而建立Java层的Surface和native层Surface的关联关系
另外Surface和SurfaceControl都持有mGraphicBufferProducer用于操作位于SurfaceFlinger中的图形buffer
推荐阅读: 图形系统总结
显示系统[3] APP申请创建Surface的过程
sp<SurfaceControl> surfaceControl = client->createSurface(String8("resize"),
160, 240, PIXEL_FORMAT_RGB_565, 0);
调用mClient->createSurface
值得注意的是此处是biner通信过程,实际上是通过BpSurfaceComposerClient的createSurface函数调用Client的onTransact再调用BnSurfaceComposerClient的onTransact函数进而调用的
上文说过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();
}
这里调用SurfaceFlinger的createLayer
路径 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而传给Surface的mGraphicBufferProducer就是上文提到的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;
}
以上是关于Android系统_Surface创建过程分析的主要内容,如果未能解决你的问题,请参考以下文章
Android 图形架构 之三—— 创建LayerSurfaceSurfaceControl