码迷,mamicode.com
首页 > 移动开发 > 详细

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

时间:2018-04-23 18:44:21      阅读:882      评论:0      收藏:0      [点我收藏+]

标签: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创建

标签:out   auto   bind   tor   change   ret   msu   分析   name   

原文地址:https://www.cnblogs.com/jonnezhang/p/8920375.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!