Android Surfac eFlinger process 流程分析

深入分析SurfaceFlinger作为显示服务器的内部运作,包括线程处理、Binder通信、与底层硬件接口交互。重点探讨SurfaceFlinger如何管理多个surface和layer,实现显示数据的合成与显示过程,以及关键处理函数如readToRun、ThreadLoop、createSurface、setClientState等的作用。同时,详细介绍surface和layer的相关属性,包括ID、尺寸、位置、状态等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

原文链接: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

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

  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. // Initialize OpenGL|ES  
  43.    
  44.     glActiveTexture(GL_TEXTURE0);  
  45.    
  46.     glBindTexture(GL_TEXTURE_2D, 0);  
  47.    
  48.     glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);  
  49.    
  50.     glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);  
  51.    
  52.     glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);  
  53.    
  54. …  
  55.    
  56. …  
  57.    
  58. }  

2)、ThreadLoop

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

  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相关参数会反馈给上层

  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进行处理

  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.                     flags |= eTransactionNeeded;  
  41.    
  42.                     continue;  
  43.    
  44.                 }  
  45.    
  46.             }  
  47.    
  48.             if (what & ePositionChanged) //显示位置变化  
  49.    
  50.                  {  
  51.    
  52.                 if (layer->setPosition(s.x, s.y))  
  53.    
  54.                     flags |= eTraversalNeeded;  
  55.    
  56.             }  
  57.    
  58.             if (what & eLayerChanged) //Layer改变  
  59.    
  60.                  {  
  61.    
  62.                 if (layer->setLayer(s.z))  
  63.    
  64.                   {  
  65.    
  66.                     mCurrentState.layersSortedByZ.reorder(  
  67.    
  68.                             layer, &Layer::compareCurrentStateZ);  
  69.    
  70.                     flags |= eTransactionNeeded|eTraversalNeeded;  
  71.    
  72.                 }  
  73.    
  74.             }  
  75.    
  76.             if (what & eSizeChanged)  
  77.    
  78.                   {  
  79.    
  80.                 if (layer->setSize(s.w, s.h))//设置宽高变化  
  81.    
  82.                     flags |= eTraversalNeeded;  
  83.    
  84.             }  
  85.    
  86.             if (what & eAlphaChanged) {//设置Alpha效果  
  87.    
  88.                 if (layer->setAlpha(uint8_t(255.0f*s.alpha+0.5f)))  
  89.    
  90.                   flags |= eTraversalNeeded;  
  91.    
  92.             }  
  93.    
  94.             if (what & eMatrixChanged) {//矩阵参数变化  
  95.    
  96.                 if (layer->setMatrix(s.matrix))  
  97.    
  98.                     flags |= eTraversalNeeded;  
  99.    
  100.             }  
  101.    
  102.             if (what & eTransparentRegionChanged) {//显示区域变化  
  103.    
  104.                 if (layer->setTransparentRegionHint(s.transparentRegion))  
  105.    
  106.                     flags |= eTraversalNeeded;  
  107.    
  108.             }  
  109.    
  110.             if (what & eVisibilityChanged) {//是否显示  
  111.    
  112.                 if (layer->setFlags(s.flags, s.mask))  
  113.    
  114.                     flags |= eTraversalNeeded;  
  115.    
  116.             }  
  117.    
  118.         }  
  119.    
  120.     }  
  121.    
  122.     if (flags)  
  123.    
  124.     {  
  125.    
  126.         setTransactionFlags(flags);//通过signal通知ThreadLoop  
  127.    
  128.     }  
  129.    
  130.     return NO_ERROR;  
  131.    
  132. }  

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值