Android SurfaceFlinger服务 ----- 本地图层Layer创建

别裁伪体亲风雅,转益多师是汝师。这篇文章主要讲述Android SurfaceFlinger服务 ----- 本地图层Layer创建相关的知识,希望能为你提供帮助。
在上一篇文章中,主要分析了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); #endifconst 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对象,不知道有什么用
【Android SurfaceFlinger服务 ----- 本地图层Layer创建】总结,当客户端创建Surface的时候,SurfaceFlinger服务端为会之创建对应的图层Layer。并且把Layer的图像缓冲区的生产者接口赋值给Surface对象的成员变量mGraphicBufferProducer。这样客户端就能将画好的图像提交给SurfaceFlinger服务去处理了。

    推荐阅读