4.3_APP申请创建Surface的过程
先创建surfaceControl再创建surface
Client
在测试程序SurfaceTest.cpp 中如下:
创建client,surface大小。
sp<SurfaceComposerClient> client = new SurfaceComposerClient();
sp<SurfaceControl> surfaceControl = client->createSurface(String8("resize"),
surfaceControl
向BnSurfaceComposerClient发送CREATE_SURFACE,最终得到 handle与gbp(BpGraphicBufferProducer)
sp<SurfaceComposerClient> client = new SurfaceComposerClient();
sp<SurfaceControl> surfaceControl = client->createSurface(String8("resize"),
sp<IBinder> handle;//创建变量handle
sp<IGraphicBufferProducer> gbp;//创建变量gdb
mClient->createSurface(&handle, &gbp
mClient//mClient在 onFirstRef 方法中创建ISurfaceComposerClient.cpp创建
virtual status_t createSurface(sp<IBinder>* handle,sp<IGraphicBufferProducer>* gbp// bp***中构造、发送数据 ISurfaceComposerClient.cpp
//构造数据
remote()->transact(CREATE_SURFACE //发送数据
//接收数据并构建handle与gbp
*handle = reply.readStrongBinder();
*gbp = interface_cast<IGraphicBufferProducer>(reply.readStrongBinder());//根据BpBinder创建BpGraphicBufferProducer
new BpBinder(handle);
sur = new SurfaceControl(this, handle, gbp);
return sur;//根据从Binder获取到的数据创建SurfaceControl
根据得到的数据创建 new SurfaceControl, 对其构造参数赋值如下:
new SurfaceControl(this, handle, gbp);
SurfaceControl::SurfaceControl(
const sp<SurfaceComposerClient>& client,
const sp<IBinder>& handle,
const sp<IGraphicBufferProducer>& gbp)
: mClient(client), mHandle(handle), mGraphicBufferProducer(gbp)
server
BnSurfaceComposerClient收到CREATE_SURFACE后解析可知是需要创建一个Layer ,这里也创建了handle与gbp。
status_t BnSurfaceComposerClient::onTransact//ISurfaceComposerClient.cpp
case CREATE_SURFACE:
sp<IBinder> handle;
sp<IGraphicBufferProducer> gbp;
status_t result = createSurface(...&handle, &gbp);
createSurface //Client.cpp
sp<MessageBase> msg = new MessageCreateLayer(... handle, gbp,//创建
mFlinger->postMessageSync(msg);//提交
handler() {//导致message中的handler函数被调用
flinger->createLayer(handle, gbp ... //SurfaceFlinger.cpp
createNormalLayer(...,gbp, &layer
outLayer= new Layer(this,//outLayer从哪里来
status_t err = (*outLayer)->setBuffers(w, h, format, flags);
*handle = (*outLayer)->getHandle();
*gbp = (*outLayer)->getProducer();
handle其中就是sufaceFlinger
sp<IBinder> Layer::getHandle() {
return new Handle(mFlinger, this);
}
gpd就是mProducer生产者;
sp<IGraphicBufferProducer> Layer::getProducer() const {
return mProducer;
}
new Layer 创建了buff,并指向了生产者与消费者。
new Layer(this,//创建new Layer的过程 Client.cpp
void Layer::onFirstRef()//第一次引用时调用Layer.cpp
BufferQueue::createBufferQueue(&producer, &consumer);
sp<BufferQueueCore> core(new BufferQueueCore(allocator));
BufferQueueCore类成员mSlots(),其中类型为typedef BufferSlot SlotsType[NUM_BUFFER_SLOTS];
mAllocator = composer->createGraphicBufferAlloc();//BufferQueueCore.cpp
sp<IGraphicBufferProducer> producer(new BufferQueueProducer(core));
mCore(core),
mSlots(core->mSlots),为那一块数组
sp<IGraphicBufferConsumer> consumer(new BufferQueueConsumer(core));
mCore(core),
mSlots(core->mSlots),为那一块数组
这个producer与consumer都指向了同一块内存。
mProducer = new MonitoredProducer(producer, mFlinger);//创建生产者给到了mFlinger
mSurfaceFlingerConsumer = new SurfaceFlingerConsumer(consumer, mTextureName);//创建消费者给到
SurfaceFlingerConsumer: GLConsumer(consumer, tex, GLConsumer::TEXTURE_//构造函数
GLConsumer::GLConsumer(c//调用其父类的构造函数 GLConsumer.cpp
ConsumerBase(bq, isControlledByApp)//再调用更高父类的构造函数
mConsumer(bufferQueue)成员mConsumer指向consumer
surface
紧接着创建surface让其受surfaceControl管理。
sp<Surface> surface = surfaceControl->getSurface();
mSurfaceData = new Surface(mGraphicBufferProducer, false);