Android display架构分析七-Surfaceflinger process流程分析

来源:互联网 发布:光脉冲星龙淘宝 编辑:程序博客网 时间:2024/06/03 09:24

根据前面的介绍,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

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

[html] view plaincopyprint?
  1. status_t SurfaceFlinger::readyToRun()  
  2.    
  3. {  
  4.    
  5. …  
  6.    
  7. …  
  8.    
  9. mServerHeap = new MemoryDealer(4096, MemoryDealer::READ_ONLY);//为IPC分配共享内存  
  10.    
  11. …  
  12.    
  13. mSurfaceHeapManager = new SurfaceHeapManager(this, 8 << 20);//为flinger分配heap,大小为8M,存放具体的显示数据  
  14.    
  15.     {  
  16.    
  17.        // initialize the main display  
  18.    
  19.         GraphicPlane& plane(graphicPlane(dpy));  
  20.    
  21.         DisplayHardware* const hw = new DisplayHardware(this, dpy);  
  22.    
  23.         plane.setDisplayHardware(hw);//保存显示接口  
  24.    
  25. }  
  26.    
  27. //获取显示相关参数  
  28.    
  29.     const GraphicPlane& plane(graphicPlane(dpy));  
  30.    
  31.     const DisplayHardware& hw = plane.displayHardware();  
  32.    
  33.     const uint32_t w = hw.getWidth();  
  34.    
  35.     const uint32_t h = hw.getHeight();  
  36.    
  37.     const uint32_t f = hw.getFormat();  
  38.    
  39. …  
  40.    
  41. …  
  42.    
  43.     // Initialize OpenGL|ES  
  44.    
  45.     glActiveTexture(GL_TEXTURE0);  
  46.    
  47.     glBindTexture(GL_TEXTURE_2D, 0);  
  48.    
  49.     glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);  
  50.    
  51.     glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);  
  52.    
  53.     glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);  
  54.    
  55. …  
  56.    
  57. …  
  58.    
  59. }  


2)、ThreadLoop

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

[html] view plaincopyprint?
  1. bool SurfaceFlinger::threadLoop()  
  2.    
  3. {  
  4.    
  5.     waitForEvent();//等待其他接口的signal event  
  6.    
  7. …  
  8.    
  9. …  
  10.    
  11.     // post surfaces (if needed)  
  12.    
  13.     handlePageFlip();//处理翻页机制  
  14.    
  15.     const DisplayHardware& hw(graphicPlane(0).displayHardware());  
  16.    
  17.     if (LIKELY(hw.canDraw()))  
  18.    
  19.    {  
  20.    
  21.         // repaint the framebuffer (if needed)  
  22.    
  23.         handleRepaint();//合并所有layer并填充到buffer中去  
  24.    
  25. …  
  26.    
  27. …  
  28.    
  29.         postFramebuffer();//互换front buffer和back buffer,调用EGL接口进行显示  
  30.    
  31.     }  
  32.    
  33. …  
  34.    
  35. …  
  36.    
  37. }  


3)、createSurface

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

[html] view plaincopyprint?
  1. sp<ISurface> SurfaceFlinger::createSurface(ClientID clientId, int pid,  
  2.    
  3.         ISurfaceFlingerClient::surface_data_t* params,  
  4.    
  5.         DisplayID d, uint32_t w, uint32_t h, PixelFormat format,  
  6.    
  7.         uint32_t flags)  
  8.    
  9. {  
  10.    
  11. …  
  12.    
  13. …  
  14.    
  15.     int32_t id = c->generateId(pid);  
  16.    
  17.     if (uint32_t(id) >= NUM_LAYERS_MAX) //NUM_LAYERS_MAX=31  
  18.    
  19.     {  
  20.    
  21.         LOGE("createSurface() failed, generateId = %d", id);  
  22.    
  23.         return  
  24.    
  25.     }  
  26.    
  27. …  
  28.    
  29. layer = createNormalSurfaceLocked(c, d, id, w, h, format, flags);//创建layer,根据参数(宽高格式)分配内存(共2个buffer:front/back buffer)  
  30.    
  31.     if (layer)  
  32.    
  33.     {  
  34.    
  35.         setTransactionFlags(eTransactionNeeded);  
  36.    
  37.         surfaceHandle = layer->getSurface();//创建surface  
  38.    
  39.         if (surfaceHandle != 0)  
  40.    
  41.             surfaceHandle->getSurfaceData(params);//创建的surface参数反馈给应用层  
  42.    
  43.     }  
  44.    
  45. }  


4)、setClientState

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

[html] view plaincopyprint?
  1. status_t SurfaceFlinger::setClientState(  
  2.    
  3.         ClientID cid,  
  4.    
  5.         int32_t count,  
  6.    
  7.         const layer_state_t* states)  
  8.    
  9. {  
  10.    
  11.     Mutex::Autolock _l(mStateLock);  
  12.    
  13.     uint32_t flags = 0;  
  14.    
  15.     cid <<= 16;  
  16.    
  17.     for (int i=0 ; i<count ; i++) //检测所有存在layer的状态标志  
  18.    
  19.     {  
  20.    
  21.         const layer_state_t& s = states[i];  
  22.    
  23.         LayerBaseClient* layer = getLayerUser_l(s.surface | cid);  
  24.    
  25.         if (layer)  
  26.    
  27.              {  
  28.    
  29.             const uint32_t what = s.what;  
  30.    
  31.             // 检测应用层是否设置各个标志,如果有则通知底层完成对应操作,并通知ThreadLoop做对应的处理  
  32.    
  33.             if (what & eDestroyed) //删除该层Layer  
  34.    
  35.                  {  
  36.    
  37.                 if (removeLayer_l(layer) == NO_ERROR)  
  38.    
  39.                  {  
  40.    
  41.                     flags |= eTransactionNeeded;  
  42.    
  43.                     continue;  
  44.    
  45.                 }  
  46.    
  47.             }  
  48.    
  49.             if (what & ePositionChanged) //显示位置变化  
  50.    
  51.                  {  
  52.    
  53.                 if (layer->setPosition(s.x, s.y))  
  54.    
  55.                     flags |= eTraversalNeeded;  
  56.    
  57.             }  
  58.    
  59.             if (what & eLayerChanged) //Layer改变  
  60.    
  61.                  {  
  62.    
  63.                 if (layer->setLayer(s.z))  
  64.    
  65.                   {  
  66.    
  67.                     mCurrentState.layersSortedByZ.reorder(  
  68.    
  69.                             layer, &Layer::compareCurrentStateZ);  
  70.    
  71.                     flags |= eTransactionNeeded|eTraversalNeeded;  
  72.    
  73.                 }  
  74.    
  75.             }  
  76.    
  77.             if (what & eSizeChanged)  
  78.    
  79.                   {  
  80.    
  81.                 if (layer->setSize(s.w, s.h))//设置宽高变化  
  82.    
  83.                     flags |= eTraversalNeeded;  
  84.    
  85.             }  
  86.    
  87.             if (what & eAlphaChanged) {//设置Alpha效果  
  88.    
  89.                 if (layer->setAlpha(uint8_t(255.0f*s.alpha+0.5f)))  
  90.    
  91.                   flags |= eTraversalNeeded;  
  92.    
  93.             }  
  94.    
  95.             if (what & eMatrixChanged) {//矩阵参数变化  
  96.    
  97.                 if (layer->setMatrix(s.matrix))  
  98.    
  99.                     flags |= eTraversalNeeded;  
  100.    
  101.             }  
  102.    
  103.             if (what & eTransparentRegionChanged) {//显示区域变化  
  104.    
  105.                 if (layer->setTransparentRegionHint(s.transparentRegion))  
  106.    
  107.                     flags |= eTraversalNeeded;  
  108.    
  109.             }  
  110.    
  111.             if (what & eVisibilityChanged) {//是否显示  
  112.    
  113.                 if (layer->setFlags(s.flags, s.mask))  
  114.    
  115.                     flags |= eTraversalNeeded;  
  116.    
  117.             }  
  118.    
  119.         }  
  120.    
  121.     }  
  122.    
  123. if (flags)  
  124.    
  125. {  
  126.    
  127.         setTransactionFlags(flags);//通过signal通知ThreadLoop  
  128.    
  129.     }  
  130.    
  131.     return NO_ERROR;  
  132.    
  133. }  


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等)


原创粉丝点击