Surfaceflinger中图像绘制流程

App产生图像数据是由Surfaceflinger消费的,下面走一下图像的消费过程。
activity中的surface对应到服务端就是layer,从layer创建过程开始


1.创建过程
SurfaceFlinger.cpp
SurfaceFlinger.createLayer() {
     //普通类型的layer,支持多个类型
      SurfaceFlinger.createNormalLayer()
}

SurfaceFlinger::createNormalLayer(){
    //创建跟app对应的layer, app的surface中主要就是操作 (*outLayer)->getBufferQueue()了
    *outLayer = new Layer(this, client, name, w, h, flags);
    status_t err = (*outLayer)->setBuffers(w, h, format, flags);
    *handle = (*outLayer)->getHandle();
   //app端就是用的这个BufferQueue
     *gbp = (*outLayer)->getBufferQueue();
}

layer.cpp
当new Layer()后第一次sp<>,会调用onFirstRef()
void Layer::onFirstRef() {
    //创建了bufferqueue   
    //class SurfaceTextureLayer : public BufferQueue   继承了BufferQueue

    mBufferQueue = new SurfaceTextureLayer(mFlinger);

   //BufferQueue的消费者,mTextureName:是纹理id
    mSurfaceFlingerConsumer = new SurfaceFlingerConsumer(mBufferQueue, mTextureName);
    //自己监听了是否数据构造好了
    mSurfaceFlingerConsumer->setFrameAvailableListener(this);
   }

SurfaceFlingerConsumer.cpp
 class SurfaceFlingerConsumer : public GLConsumer
    //构造函数 把参数传递给基类
    //TEXTURE_EXTERNAL = GL_TEXTURE_EXTERNAL_OES  特定类型的纹理
    SurfaceFlingerConsumer(const sp<BufferQueue>& bq, uint32_t tex)
        : GLConsumer(bq, tex, GLConsumer::TEXTURE_EXTERNAL, false)
    {}

GLConsumer.cpp
    class GLConsumer : public ConsumerBase {
    //看名称就是用opengl了,保存了纹理名称,BufferQueue又给了基类
   //mTexName 绘制的纹理,app端的图像
    GLConsumer::GLConsumer(const sp<IGraphicBufferConsumer>& bq, uint32_t tex,
        uint32_t texTarget, bool useFenceSync, bool isControlledByApp) :
        ConsumerBase(bq, isControlledByApp), mTexName(tex),

Consumernase.cpp
    class ConsumerBase : public virtual RefBase,
        protected ConsumerListener

   ConsumerBase::ConsumerBase(const sp<IGraphicBufferConsumer>& bufferQueue, bool controlledByApp) :
        mAbandoned(false),
        mConsumer(bufferQueue) {
      wp<ConsumerListener> listener = static_cast<ConsumerListener*>(this);
      sp<IConsumerListener> proxy = new BufferQueue::ProxyConsumerListener(listener);

      //自己监听数据改变了,proxy还是一个BnBinder,看样是可以跨进程了
      status_t err = mConsumer->consumerConnect(proxy, controlledByApp);
   }

   void ConsumerBase::onFrameAvailable() {
        //数据填充好了以后,再一次转发出去
        mFrameAvailableListener>onFrameAvailable();
    }

    //需要外部设置了,记得Layer的第一次引用调用他,所以layer监听自己的数据
    void ConsumerBase::setFrameAvailableListener(  const wp<FrameAvailableListener>& listener) {
        mFrameAvailableListener = listener;
    }


2.app绘制好数据:

Layer.cpp   
   void Layer::onFrameAvailable() {//激发了flinger的update
       android_atomic_inc(&mQueuedFrames);
       mFlinger->signalLayerUpdate();
    }

SurfaceFlinger.cpp
    void SurfaceFlinger::signalLayerUpdate() {//调用了MessageQueue的函数
         mEventQueue.invalidate();
    }

    void SurfaceFlinger::init() {
        //vsync信号的,这是通过VSYNC信号驱动绘制的  这次不走这个逻辑
        sp<VSyncSource> sfVsyncSrc = new DispSyncSource(&mPrimaryDispSync,
            sfVsyncPhaseOffsetNs, false);
        mSFEventThread = new EventThread(sfVsyncSrc);
        mEventQueue.setEventThread(mSFEventThread);
    }

MessageQueue.cpp
    void MessageQueue::invalidate() {
       #if INVALIDATE_ON_VSYNC//这应用VSYNC信号的
          mEvents->requestNextVsync();
       #else  //这就是handler触发
          mHandler->dispatchInvalidate();  (MessageQueue::Handler)
       #endif
       这两个是一样的,最总都会触发更新  ,
       我们分析下面这种方式了,这个相对简单,不涉及VSYNC信号等
    }

    //handler转发
    void MessageQueue::Handler::dispatchInvalidate() {
       if ((android_atomic_or(eventMaskInvalidate, &mEventMask) & eventMaskInvalidate) == 0) {
          mQueue.mLooper->sendMessage(this, Message(MessageQueue::INVALIDATE));
       }
    }

    void MessageQueue::Handler::handleMessage(const Message& message) {
    switch (message.what) {
        case INVALIDATE://又转到flinger中了
            android_atomic_and(~eventMaskInvalidate, &mEventMask);
            mQueue.mFlinger->onMessageReceived(message.what);
          break;       
      }
    }

SurfaceFlinger.cpp
    void SurfaceFlinger::onMessageReceived(int32_t what) {
    switch (what) {
    case MessageQueue::INVALIDATE:
        handleMessageTransaction();
        handleMessageInvalidate();
        signalRefresh();
        break;
    case MessageQueue::REFRESH:
        handleMessageRefresh();
        break;
    }
}

//转几次后最总都到这了
void SurfaceFlinger::handleMessageRefresh() {
    preComposition();
    rebuildLayerStacks();
    setUpHWComposer();
    doDebugFlashRegions();
    doComposition();
    postComposition();
}

//中间跳过去,直接看layer的绘制
SurfaceFlinger:: doComposition(){
    //可能多屏
    for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {     
        const sp<DisplayDevice>& hw(mDisplays[dpy]);
    //绘制这个屏幕的,数据
        doDisplayComposition(hw, dirtyRegion);
    }

    postFramebuffer();
}

SurfaceFlinger:: doDisplayComposition(){
   doComposeSurfaces(hw, dirtyRegion);
}

void SurfaceFlinger::doComposeSurfaces(const sp<const DisplayDevice>& hw, const Region& dirty)
{
    RenderEngine& engine(getRenderEngine());
    const int32_t id = hw->getHwcDisplayId();


    if (hasGlesComposition) {//是否gles合成
        hw->makeCurrent(mEGLDisplay, mEGLContext);
    }

     //看到绘制layer了吧
     for (size_t i=0 ; i<count ; ++i) {
        const sp<Layer>& layer(layers[i]);
        layer->draw(hw, clip);
     }
}

Layer.cpp
void Layer::draw(const sp<const DisplayDevice>& hw, const Region& clip) const {
    onDraw(hw, clip);
}

void Layer::onDraw(const sp<const DisplayDevice>& hw, const Region& clip) const
{
    //consumer把准备好的数据,绑定到mTexture中
    status_t err = mSurfaceFlingerConsumer->bindTextureImage();
    RenderEngine& engine(mFlinger->getRenderEngine());
    engine.setupLayerTexturing(mTexture);
    //下面的绘制就用上面的纹理了,其实就是app绘制的图
    drawWithOpenGL(hw, clip);
    engine.disableTexturing();
}


void Layer::drawWithOpenGL(
        const sp<const DisplayDevice>& hw, const Region& clip) const {
    const Rect win(computeBounds());
    //计算一下纹理坐标
    float left   = float(win.left)   / float(s.active.w);
    float top    = float(win.top)    / float(s.active.h);
    float right  = float(win.right)  / float(s.active.w);
    float bottom = float(win.bottom) / float(s.active.h);

    Mesh::VertexArray<vec2> texCoords(mMesh.getTexCoordArray<vec2>());
    texCoords[0] = vec2(left, 1.0f - top);
    texCoords[1] = vec2(left, 1.0f - bottom);
    texCoords[2] = vec2(right, 1.0f - bottom);
    texCoords[3] = vec2(right, 1.0f - top);

    RenderEngine& engine(mFlinger->getRenderEngine());
    engine.setupLayerBlending(mPremultipliedAlpha, isOpaque(), s.alpha);
    //绘制
    engine.drawMesh(mMesh);
    engine.disableBlending();
}

3.layer把图像绘制到哪里去了

SurfaceFlinger.cpp
SurfaceFlinger ::doComposeSurfaces(){
    const sp<DisplayDevice>& hw(mDisplays[dpy]);
    if (hasGlesComposition) {//是否gles合成
        hw->makeCurrent(mEGLDisplay, mEGLContext);
    }
}

DisplayDevice.cpp
EGLBoolean DisplayDevice::makeCurrent(EGLDisplay dpy, EGLContext ctx) const {
    EGLBoolean result = EGL_TRUE;
    EGLSurface sur = eglGetCurrentSurface(EGL_DRAW);
    //mSurface那构造的
    if (sur != mSurface) {
        result = eglMakeCurrent(dpy, mSurface, mSurface, ctx);
    }
}

DisplayDevice::DisplayDevice(const sp<IGraphicBufferProducer>& producer,){
    //producer(BufferQueue)构造出Surface, 也是一个本地窗口
    mNativeWindow = new Surface(producer, false);
    ANativeWindow* const window = mNativeWindow.get();

    //本地窗口创建的EGLSurface,当通过gl绘制的时候,会触发本地窗口的queueBuffer等调用
    //也就成为了BufferQueue的生产者
    EGLSurface surface;
    EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
    surface = eglCreateWindowSurface(display, config, window, NULL); 
    mSurface = surface;
}

SurfaceFlinger.cpp
void SurfaceFlinger::init() {
     sp<BufferQueue> bq = new BufferQueue(new GraphicBufferAlloc());
      //bq的消费者 
     sp<FramebufferSurface> fbs = new FramebufferSurface(*mHwc, i, bq);
     //bq的生产者  app的界面会经过这里
     sp<DisplayDevice> hw = new DisplayDevice(this,
                    type, allocateHwcDisplayId(type), isSecure, token,
                    fbs, bq,
                    mEGLConfig);  
}


FramebufferSurface.cpp
class FramebufferSurface : public ConsumerBase,
                           public DisplaySurface {

//ConsumerBase继承来的,数据自己监听了,上面看到过这个ConsumerBase
FramebufferSurface::FramebufferSurface(HWComposer& hwc, int disp,
        const sp<IGraphicBufferConsumer>& consumer) :
    ConsumerBase(consumer),{
    }


void FramebufferSurface::onFrameAvailable() {
    sp<GraphicBuffer> buf;
    sp<Fence> acquireFence;
    status_t err = nextBuffer(buf, acquireFence);
    //数据取出来给mHwc了,   
    //HWComposer& mHwc;这东西硬件抽象层的合成模块
    //就是通过硬件把图像输出到屏幕,这里面逻辑挺复杂,先这样想,以后还会分析这个的
    err = mHwc.fbPost(mDisplayType, acquireFence, buf);
}


status_t FramebufferSurface::nextBuffer(sp<GraphicBuffer>& outBuffer, sp<Fence>& outFence) {
    BufferQueue::BufferItem item;
    //BufferQueue中取数据
    status_t err = acquireBufferLocked(&item, 0);
}


总结:
数据生产消费流程  app surface  ----> layer  --->  HWComposer  ---> fb屏幕

app生产的数据 给layer消费 ,layer把数据加工一下再生产给 HWComposer ,
HWcomposer有数据后通过fb渲染到屏幕。
发布了13 篇原创文章 · 获赞 0 · 访问量 3万+
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览