Android 渲染机制小结

surface

源码定义:

c++ 复制代码
class Surface
    : public ANativeObjectBase<ANativeWindow, Surface, RefBase>
{
public:

    /*
     * creates a Surface from the given IGraphicBufferProducer (which concrete
     * implementation is a BufferQueue).
     *
     * Surface is mainly state-less while it's disconnected, it can be
     * viewed as a glorified IGraphicBufferProducer holder. It's therefore
     * safe to create other Surfaces from the same IGraphicBufferProducer.
     *
     * However, once a Surface is connected, it'll prevent other Surfaces
     * referring to the same IGraphicBufferProducer to become connected and
     * therefore prevent them to be used as actual producers of buffers.
     *
     * the controlledByApp flag indicates that this Surface (producer) is
     * controlled by the application. This flag is used at connect time.
     */
    //sp 智能引用
    explicit Surface(const sp<IGraphicBufferProducer>& bufferProducer,
            bool controlledByApp = false);
            ...

** IGraphicBufferProducer**

  • dequeueBuffer : 返回一个新的buffer 供客户端调用
  • queueBuffer: 客户端完成buffer 填充后进行提交
SurfaceControl 和 Layer 创建绑定
  • ViewRootImp:
    • performTraversals -> relayoutWindow -> mWindowSession.relayout
  • WindowManagerService:
    • relayoutWindow -> createSurfaceControl -> winAnimator.createSurfaceLocked
  • WindowStateAnimator
    • createSurfaceLocked -> mSurfaceController = new WindowSurfaceController(xxxx)
  • WindowSurfaceController:
    • win.makeSurface() .... .build
  • SurfaceControl
    • init(xxx) -> nativeCreate(xxxxx)
  • android_view_SurfaceControl.cpp
    • sp client;
    • client->createSurfaceChecked(xxx)
  • SurfaceComposerClient.cpp
    • sp SurfaceComposerClient::connection() const {\ return IInterface::asBinder(mClient);\ }
    • createSurfaceChecked-> mClient.createSurface(xxx) // binder 调用 ,服务端在 SurfaceFlinger 进程
    • *outSurface = new SurfaceControl(this, handle, gbp, transformHint);
  • Client.cpp // surfacefinger进程
    • class BnSurfaceComposerClient : public SafeBnInterface
    • class Client : public BnSurfaceComposerClient
    • sp mFlinger;
    • createSurface -> mFlinger->createLayer(xxx)/
  • SurfaceFlinger.cpp
    • sp layer;
    • createBufferStateLayer(xxx) // switch (ISurfaceComposerClient::eFXSurfaceBufferState) -> getFactory().createBufferStateLayer(args)
c++ 复制代码
status_t SurfaceFlinger::createBufferStateLayer(const sp<Client>& client, std::string name,
                                                uint32_t w, uint32_t h, uint32_t flags,
                                                LayerMetadata metadata, sp<IBinder>* handle,
                                                sp<Layer>* outLayer) {
    LayerCreationArgs args(this, client, std::move(name), w, h, flags, std::move(metadata));
    args.textureName = getNewTexture();
    sp<BufferStateLayer> layer = getFactory().createBufferStateLayer(args);
    // 绑定 handler 与 layer
    *handle = layer->getHandle();
    *outLayer = layer;

    return NO_ERROR;
}
  • SurfaceFlingerDefaultFactory.cpp
    • new BufferStateLayer(args);
      小结
cpp 复制代码
// SurfaceComposerClient.cpp
status_t SurfaceComposerClient::createSurfaceChecked(const String8& name, uint32_t w, uint32_t h,
                                                     PixelFormat format,
                                                     sp<SurfaceControl>* outSurface, uint32_t flags,
                                                     SurfaceControl* parent, LayerMetadata metadata,
                                                     uint32_t* outTransformHint) {
    sp<SurfaceControl> sur;
    status_t err = mStatus;

    if (mStatus == NO_ERROR) {
        sp<IBinder> handle;
        sp<IBinder> parentHandle;
        sp<IGraphicBufferProducer> gbp;

        if (parent != nullptr) {
            parentHandle = parent->getHandle();
        }

        uint32_t transformHint = 0;
        //  最终会在 SurfaceFlinger 这边创建一块 Layer 对象
        // handle 作为参数一路传递下去 完成与layer的绑定()
        err = mClient->createSurface(name, w, h, format, flags, parentHandle, std::move(metadata),
                                     &handle, &gbp, &transformHint);
        if (outTransformHint) {
            *outTransformHint = transformHint;
        }
        ALOGE_IF(err, "SurfaceComposerClient::createSurface error %s", strerror(-err));
        if (err == NO_ERROR) {
            // 最终会作为 SurfaceControl 构造函数的参数传递给 SurfaceControl
            *outSurface = new SurfaceControl(this, handle, gbp, transformHint);
        }
    }
    return err;
}
BLASTBufferQueue
css 复制代码
- ViewRootImpl
    - relayoutWindow - final Surface blastSurface = getOrCreateBLASTSurface(mSurfaceSize.x,
                    mSurfaceSize.y); -> mBlastBufferQueue = new BLASTBufferQueue()
                    -> mBlastBufferQueue.getSurface(); //获取Surface
- BLASTBufferQueue
    - init -> nativeCreate(sc.mNativeObject, width, height, tripleBufferingEnabled);
    - getSurface -> nativeGetSurface(mNativeObject);
- android_graphics_BLASTBufferQueue.cpp
    - sp<BLASTBufferQueue> queue = new BLASTBufferQueue(
            reinterpret_cast<SurfaceControl*>(surfaceControl), width, height, enableTripleBuffering);
    - nativeGetSurface -> android_view_Surface_createFromIGraphicBufferProducer(env, queue->getIGraphicBufferProducer()); -> sp<Surface> surface(new Surface(bufferProducer, true)); //android_view_Surface.cpp
- BLASTBufferQueue.cpp
    - sp<IGraphicBufferConsumer> mConsumer;
    - sp<IGraphicBufferProducer> mProducer;
    -  init -> BufferQueue::createBufferQueue(&mProducer, &mConsumer); // 创建 BufferQueueCore,mProducer 和 mConsumer 
    -  init -> mBufferItemConsumer =
        new BLASTBufferItemConsumer(mConsumer, GraphicBuffer::USAGE_HW_COMPOSER, 1, true); // 创建 mBufferItemConsumer
    - mBufferItemConsumer->setFrameAvailableListener(this); //把 FrameAvailableListener 的回调设置成自己, 也就是 BLASTBufferQueue
- BufferQueueCore.h
    -  BufferQueueDefs::SlotsType mSlots; //struct BufferSlot{...sp<GraphicBuffer> mGraphicBuffer; BufferState mBufferState;...}
    -  Fifo mQueue; // typedef Vector<BufferItem> Fifo; //class BufferItem{...sp<GraphicBuffer> mGraphicBuffer;...}
    
cpp 复制代码
void BufferQueue::createBufferQueue(sp<IGraphicBufferProducer>* outProducer,
        sp<IGraphicBufferConsumer>* outConsumer,
        bool consumerIsSurfaceFlinger) {
    LOG_ALWAYS_FATAL_IF(outProducer == nullptr,
            "BufferQueue: outProducer must not be NULL");
    LOG_ALWAYS_FATAL_IF(outConsumer == nullptr,
            "BufferQueue: outConsumer must not be NULL");
    // 
    sp<BufferQueueCore> core(new BufferQueueCore());
    LOG_ALWAYS_FATAL_IF(core == nullptr,
            "BufferQueue: failed to create BufferQueueCore");

    sp<IGraphicBufferProducer> producer(new BufferQueueProducer(core, consumerIsSurfaceFlinger));
    LOG_ALWAYS_FATAL_IF(producer == nullptr,
            "BufferQueue: failed to create BufferQueueProducer");

    sp<IGraphicBufferConsumer> consumer(new BufferQueueConsumer(core));
    LOG_ALWAYS_FATAL_IF(consumer == nullptr,
            "BufferQueue: failed to create BufferQueueConsumer");
    
    *outProducer = producer;
    *outConsumer = consumer;
}

流程

  • Producer 从 mSlots 中拿到一块 BufferSlot // queueBuffer
  • BufferSlot 转换成 BufferItem 插入 mQueue
  • Comsumer 从 mQueue 中取出 BufferItem // acquireBuffer ,releaseBuffer
  • BufferItem 转换成 BufferSlot 提交给 SurfaceFlinger
  • mQueue 的主要作用是 "控制 Buffer 的出队顺序"

BufferQueueProducer

APP 侧
  • SurfaceFlinger 收到 Vsync 分发给 Choreographer
  • Choreographer -> doFrame ->
  • ViewRootImpl.doTarversal -> ViewRootImpl.performTraversals
    • performTraversals ->
      ->performMeasure
      ->performLayout
      ->perforDraw
      -> Draw -> drawSoftware ->

draw

  • canvas = mSurface.lockCanvas(dirty); //初始化 Canvas,从 BLASTBufferQueue 中申请一块 GraphicBuffer
  • ...->Surface.cpp -> lock -> dequeueBuffer(&out, &fenceFd); -> mGraphicBufferProducer->dequeueBuffer(....)
  • mView.draw(canvas); //绘制 ,使用绘制引擎 api 进行绘制 Skia / OpenGL
  • surface.unlockCanvasAndPost(canvas); //绘制完成,提交到 BLASTBufferQueue
  • ...->Surface.cpp ->unlockAndPost-> queueBuffer(mLockedBuffer.get(), fd); -> mGraphicBufferProducer->queueBuffer(i, input, &output);

GraphicBuffer

cpp 复制代码
//GraphicBuffer.h
class GraphicBuffer
    : public ANativeObjectBase<ANativeWindowBuffer, GraphicBuffer, RefBase>,
      public Flattenable<GraphicBuffer> //支持序列化, 支持Binder 跨进程传递
cpp 复制代码
//ANativeObjectBase.h
// 模版类
template <typename NATIVE_TYPE, typename TYPE, typename REF,
        typename NATIVE_BASE = android_native_base_t>
class ANativeObjectBase : public NATIVE_TYPE, public REF
cpp 复制代码
//nativebase.h
typedef struct ANativeWindowBuffer{
    ...
    //底层 Buffer 内存 句柄
    const native_handle_t* handle;
}
typedef struct ANativeWindowBuffer ANativeWindowBuffer;

创建

BufferQueueProducer.cpp

  • dequeueBuffer -> 查找可用buffer,复用 ,没有的话 创建 GraphicBuffer()
    GraphicBuffer.cpp
  • init->initWithSize -> allocator.allocate(inWidth, inHeight, inFormat, inLayerCount,
    inUsage, &handle, &outStride, mId,
    std::move(requestorName)); // 通过 Binder Server 分配一块内存,并且返回这块内存的地址由 handle 持有
    GraphicBufferAllocator.cpp
  • allocate -> allocateHelper -> mAllocator->allocate(....)
相关推荐
踢球的打工仔6 小时前
PHP面向对象(7)
android·开发语言·php
安卓理事人6 小时前
安卓socket
android
安卓理事人12 小时前
安卓LinkedBlockingQueue消息队列
android
万能的小裴同学13 小时前
Android M3U8视频播放器
android·音视频
q***577414 小时前
MySql的慢查询(慢日志)
android·mysql·adb
JavaNoober14 小时前
Android 前台服务 "Bad Notification" 崩溃机制分析文档
android
城东米粉儿15 小时前
关于ObjectAnimator
android
zhangphil16 小时前
Android渲染线程Render Thread的RenderNode与DisplayList,引用Bitmap及Open GL纹理上传GPU
android
火柴就是我17 小时前
从头写一个自己的app
android·前端·flutter
lichong95118 小时前
XLog debug 开启打印日志,release 关闭打印日志
android·java·前端