Android SurfaceFlinger process 流程分析

原文链接:http://hi.baidu.com/leowenj/blog/item/7abbe33a309367ff3b87ce6f.html

------------------------------------------------------------------------------------------------------------------------


根据前面的介绍,surfaceflinger作为一个server process,上层的应用程序(作为client)通过Binder方式与其进行通信。Surfaceflinger作为一个thread,这里把它分为3个部分,如下:

1、 Thread本身处理部分,包括初始化以及thread loop。

2、 Binder部分,负责接收上层应用的各个设置和命令,并反馈状态标志给上层。

3、 与底层的交互,负责调用底层接口(HAL)。

结构图如下:

a、 Binder接收到应用程序的命令(如创建surface、设置参数等),传递给flinger。

b、 Flinger完成对应命令后将相关结果状态反馈给上层。

c、 在处理上层命令过程中,根据需要设置event(主要和显示有关),通知Thread Loop进行处理。

d、 Flinger根据上层命令通知底层进行处理(主要是设置一些参数,Layer、position等)

e、 Thread Loop中进行surface的合成并通知底层进行显示(Post buffer)。

f、 DisplayHardware层根据flinger命令调用HAL进行HW的操作。

下面来具体分析一些SurfaceFlinger中重要的处理函数以及surface、Layer的属性

1)、readToRun

thread的初始化函数,主要任务是分配内存和设置底层接口(EGL&HAL)。

status_t SurfaceFlinger::readyToRun()
 
{
 
…
 
…
 
mServerHeap = new MemoryDealer(4096, MemoryDealer::READ_ONLY);//为IPC分配共享内存
 
…
 
mSurfaceHeapManager = new SurfaceHeapManager(this, 8 << 20);//为flinger分配heap,大小为8M,存放具体的显示数据
 
    {
 
       // initialize the main display
 
        GraphicPlane& plane(graphicPlane(dpy));
 
        DisplayHardware* const hw = new DisplayHardware(this, dpy);
 
        plane.setDisplayHardware(hw);//保存显示接口
 
}
 
//获取显示相关参数
 
    const GraphicPlane& plane(graphicPlane(dpy));
 
    const DisplayHardware& hw = plane.displayHardware();
 
    const uint32_t w = hw.getWidth();
 
    const uint32_t h = hw.getHeight();
 
    const uint32_t f = hw.getFormat();
 
…
 
…
// Initialize OpenGL|ES
 
    glActiveTexture(GL_TEXTURE0);
 
    glBindTexture(GL_TEXTURE_2D, 0);
 
    glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
 
    glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
 
    glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
 
…
 
…
 
}

2)、ThreadLoop

Surfaceflinger的loop函数,主要是等待其他接口发送的event,进行显示数据的合成以及显示

bool SurfaceFlinger::threadLoop()
 
{
 
    waitForEvent();//等待其他接口的signal event
 
…
 
…
 
    // post surfaces (if needed)
 
    handlePageFlip();//处理翻页机制
 
    const DisplayHardware& hw(graphicPlane(0).displayHardware());
 
    if (LIKELY(hw.canDraw()))
 
   {
 
        // repaint the framebuffer (if needed)
 
        handleRepaint();//合并所有layer并填充到buffer中去
 
…
 
…
 
        postFramebuffer();//互换front buffer和back buffer,调用EGL接口进行显示
 
    }
 
…
 
…
 
}

3)、createSurface

提供给应用程序的主要接口,该接口可以创建一个surface,底层会根据参数创建layer以及分配内存,surface相关参数会反馈给上层

sp<ISurface> SurfaceFlinger::createSurface(ClientID clientId, int pid,
 
        ISurfaceFlingerClient::surface_data_t* params,
 
        DisplayID d, uint32_t w, uint32_t h, PixelFormat format,
 
        uint32_t flags)
 
{
 
…
 
…
 
    int32_t id = c->generateId(pid);
 
    if (uint32_t(id) >= NUM_LAYERS_MAX) //NUM_LAYERS_MAX=31
 
    {
 
        LOGE("createSurface() failed, generateId = %d", id);
 
        return
 
    }
 
…
 
layer = createNormalSurfaceLocked(c, d, id, w, h, format, flags);//创建layer,根据参数(宽高格式)分配内存(共2个buffer:front/back buffer)
 
    if (layer)
 
    {
 
        setTransactionFlags(eTransactionNeeded);
 
        surfaceHandle = layer->getSurface();//创建surface
 
        if (surfaceHandle != 0)
 
            surfaceHandle->getSurfaceData(params);//创建的surface参数反馈给应用层
 
    }
 
}

4)、setClientState

处理上层的各个命令,并根据flag设置event通知Threadloop进行处理

status_t SurfaceFlinger::setClientState(
 
        ClientID cid,
 
        int32_t count,
 
        const layer_state_t* states)
 
{
 
    Mutex::Autolock _l(mStateLock);
 
    uint32_t flags = 0;
 
    cid <<= 16;
 
    for (int i=0 ; i<count ; i++) //检测所有存在layer的状态标志
 
    {
 
        const layer_state_t& s = states[i];
 
        LayerBaseClient* layer = getLayerUser_l(s.surface | cid);
 
        if (layer)
 
             {
 
            const uint32_t what = s.what;
 
            // 检测应用层是否设置各个标志,如果有则通知底层完成对应操作,并通知ThreadLoop做对应的处理
 
            if (what & eDestroyed) //删除该层Layer
 
                 {
 
                if (removeLayer_l(layer) == NO_ERROR)
                 {
 
                    flags |= eTransactionNeeded;
 
                    continue;
 
                }
 
            }
 
            if (what & ePositionChanged) //显示位置变化
 
                 {
 
                if (layer->setPosition(s.x, s.y))
 
                    flags |= eTraversalNeeded;
 
            }
 
            if (what & eLayerChanged) //Layer改变
 
                 {
 
                if (layer->setLayer(s.z))
 
                  {
 
                    mCurrentState.layersSortedByZ.reorder(
 
                            layer, &Layer::compareCurrentStateZ);
 
                    flags |= eTransactionNeeded|eTraversalNeeded;
 
                }
 
            }
 
            if (what & eSizeChanged)
 
                  {
 
                if (layer->setSize(s.w, s.h))//设置宽高变化
 
                    flags |= eTraversalNeeded;
 
            }
 
            if (what & eAlphaChanged) {//设置Alpha效果
 
                if (layer->setAlpha(uint8_t(255.0f*s.alpha+0.5f)))
 
                  flags |= eTraversalNeeded;
 
            }
 
            if (what & eMatrixChanged) {//矩阵参数变化
 
                if (layer->setMatrix(s.matrix))
 
                    flags |= eTraversalNeeded;
 
            }
 
            if (what & eTransparentRegionChanged) {//显示区域变化
 
                if (layer->setTransparentRegionHint(s.transparentRegion))
 
                    flags |= eTraversalNeeded;
 
            }
 
            if (what & eVisibilityChanged) {//是否显示
 
                if (layer->setFlags(s.flags, s.mask))
 
                    flags |= eTraversalNeeded;
 
            }
 
        }
 
    }
 
    if (flags)
 
    {
 
        setTransactionFlags(flags);//通过signal通知ThreadLoop
 
    }
 
    return NO_ERROR;
 
}

5)、composeSurfaces

该接口在Threadloop中被调用,负责将所有存在的surface进行合并,OpenGl模块负责这个部分。

6)、postFramebuffer

该接口在Threadloop中被调用,负责将合成好的数据(存于back buffer中)推入在front buffer中,然后调用HAL接口命令底层显示。

7)、从3中可知,上层每创建一个surface的时候,底层都会同时创建一个layer,下面看一下surface及layer的相关属性。

Note:code中相关结构体太大,就不全部罗列出来了

A、Surface相关属性(详细参考文件surface.h)

a1:SurfaceID:根据此ID把相关surface和layer对应起来

a2:SurfaceInfo

包括宽高格式等信息

a3:2个buffer指针、buffer索引等信息

B、Layer相关属性(详细参考文件layer.h/layerbase.h/layerbitmap.h)

包括Layer的ID、宽高、位置、layer、alpha指、前后buffer地址及索引、layer的状态信息(如eFlipRequested、eBusy、eLocked等)


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值