標籤:out auto bind tor change ret msu 分析 name
在上一篇文章中,主要分析了Surface的建立過程,對於Layer圖層的建立並沒有深入跟蹤分析。這篇文章將分析Layer圖層的建立,並分析handle gbp這兩個變數的來源。
在SurfaceFlinger中會根據flags的值建立不同的layer。這裡主要以建立普通layer為例進行分析。
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 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; } *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(); } ALOGE_IF(err, "createNormalLayer() failed (%s)", strerror(-err)); return err;}
- 建立一個Layer對象outLayer
- 調用Layer對象的getHandle()方法賦值給handle
- 調用Layer對象的getProducer()方法賦值給gbp
感覺快到源頭了,在來看看Layer類的onFirstRef方法
void Layer::onFirstRef() { // Creates a custom BufferQueue for SurfaceFlingerConsumer to use sp<IGraphicBufferProducer> producer; sp<IGraphicBufferConsumer> consumer; BufferQueue::createBufferQueue(&producer, &consumer); mProducer = new MonitoredProducer(producer, mFlinger); mSurfaceFlingerConsumer = new SurfaceFlingerConsumer(consumer, mTextureName); mSurfaceFlingerConsumer->setConsumerUsageBits(getEffectiveUsage(0)); mSurfaceFlingerConsumer->setContentsChangedListener(this); mSurfaceFlingerConsumer->setName(mName);#ifdef TARGET_DISABLE_TRIPLE_BUFFERING#warning "disabling triple buffering" mSurfaceFlingerConsumer->setDefaultMaxBufferCount(2);#else mSurfaceFlingerConsumer->setDefaultMaxBufferCount(3);#endif const sp<const DisplayDevice> hw(mFlinger->getDefaultDisplayDevice()); updateTransformHint(hw);}
- 調用BufferQueue::createBufferQueue()方法建立映像緩衝區隊列
- 建立產生者對象MonitoredProducer,並賦值給mProducer
- 建立消費者對象SurfaceFlingerConsumer,並賦值給mSurfaceFlingerConsumer
在來看看Layer的getHandle方法
sp<IBinder> Layer::getHandle() { Mutex::Autolock _l(mLock); LOG_ALWAYS_FATAL_IF(mHasSurface, "Layer::getHandle() has already been called"); mHasSurface = true; /* * The layer handle is just a BBinder object passed to the client * (remote process) -- we don‘t keep any reference on our side such that * the dtor is called when the remote side let go of its reference. * * LayerCleaner ensures that mFlinger->onLayerDestroyed() is called for * this layer when the handle is destroyed. */ class Handle : public BBinder, public LayerCleaner { wp<const Layer> mOwner; public: Handle(const sp<SurfaceFlinger>& flinger, const sp<Layer>& layer) : LayerCleaner(flinger, layer), mOwner(layer) { } }; return new Handle(mFlinger, this);}
- 最終返回一個Handle類的binder對象,不知道有什麼用
總結,當用戶端建立Surface的時候,SurfaceFlinger服務端為會之建立對應的圖層Layer。並且把Layer的映像緩衝區的生產者介面賦值給Surface對象的成員變數mGraphicBufferProducer。這樣用戶端就能將畫好的映像提交給SurfaceFlinger服務去處理了。
Android SurfaceFlinger服務(三) ----- 本地圖層Layer建立