surfaceflinger process 进程分析

根据前面的介绍,surfaceflinger作为一个server process(其进程入口见main_surfaceflinger.cpp中的main函数),上层的应用程序(作为client)通过Binder方式与其进行通信。Surfaceflinger作为一个进程,这里把它分为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. SurfaceFlinger::readyToRun

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

 

[cpp]  view plain  copy
  1. status_t SurfaceFlinger::readyToRun()  
  2. {  
  3.     LOGI(   "SurfaceFlinger's main thread ready to run. "  
  4.             "Initializing graphics H/W...");  
  5.   
  6.     // we only support one display currently  
  7.     int dpy = 0;  
  8.   
  9.     {  
  10.         // initialize the main display  
  11.         GraphicPlane& plane(graphicPlane(dpy));  
  12.         DisplayHardware* const hw = new DisplayHardware(this, dpy);  
  13.         plane.setDisplayHardware(hw);  
  14.     }  
  15.   
  16.     // create the shared control-block  
  17.     mServerHeap = new MemoryHeapBase(4096,  
  18.             MemoryHeapBase::READ_ONLY, "SurfaceFlinger read-only heap");  
  19.     LOGE_IF(mServerHeap==0, "can't create shared memory dealer");  
  20.   
  21.     mServerCblk = static_cast<surface_flinger_cblk_t*>(mServerHeap->getBase());  
  22.     LOGE_IF(mServerCblk==0, "can't get to shared control block's address");  
  23.   
  24.     new(mServerCblk) surface_flinger_cblk_t;  
  25.   
  26.     // initialize primary screen  
  27.     // (other display should be initialized in the same manner, but  
  28.     // asynchronously, as they could come and go. None of this is supported  
  29.     // yet).  
  30.     const GraphicPlane& plane(graphicPlane(dpy));  
  31.     const DisplayHardware& hw = plane.displayHardware();  
  32.     const uint32_t w = hw.getWidth();  
  33.     const uint32_t h = hw.getHeight();  
  34.     const uint32_t f = hw.getFormat();  
  35.     hw.makeCurrent();  
  36.   
  37.     // initialize the shared control block  
  38.     mServerCblk->connected |= 1<<dpy;  
  39.     display_cblk_t* dcblk = mServerCblk->displays + dpy;  
  40.     memset(dcblk, 0, sizeof(display_cblk_t));  
  41.     dcblk->w            = plane.getWidth();  
  42.     dcblk->h            = plane.getHeight();  
  43.     dcblk->format       = f;  
  44.     dcblk->orientation  = ISurfaceComposer::eOrientationDefault;  
  45.     dcblk->xdpi         = hw.getDpiX();  
  46.     dcblk->ydpi         = hw.getDpiY();  
  47.     dcblk->fps          = hw.getRefreshRate();  
  48.     dcblk->density      = hw.getDensity();  
  49.   
  50.     // Initialize OpenGL|ES  
  51.     glPixelStorei(GL_UNPACK_ALIGNMENT, 4);  
  52.     glPixelStorei(GL_PACK_ALIGNMENT, 4);  
  53.     glEnableClientState(GL_VERTEX_ARRAY);  
  54.     glEnable(GL_SCISSOR_TEST);  
  55.     glShadeModel(GL_FLAT);  
  56.     glDisable(GL_DITHER);  
  57.     glDisable(GL_CULL_FACE);  
  58.   
  59.     const uint16_t g0 = pack565(0x0F,0x1F,0x0F);  
  60.     const uint16_t g1 = pack565(0x17,0x2f,0x17);  
  61.     const uint16_t wormholeTexData[4] = { g0, g1, g1, g0 };  
  62.     glGenTextures(1, &mWormholeTexName);  
  63.     glBindTexture(GL_TEXTURE_2D, mWormholeTexName);  
  64.     glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);  
  65.     glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);  
  66.     glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);  
  67.     glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);  
  68.     glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 2, 2, 0,  
  69.             GL_RGB, GL_UNSIGNED_SHORT_5_6_5, wormholeTexData);  
  70.   
  71.     const uint16_t protTexData[] = { pack565(0x03, 0x03, 0x03) };  
  72.     glGenTextures(1, &mProtectedTexName);  
  73.     glBindTexture(GL_TEXTURE_2D, mProtectedTexName);  
  74.     glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);  
  75.     glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);  
  76.     glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);  
  77.     glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);  
  78.     glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 1, 1, 0,  
  79.             GL_RGB, GL_UNSIGNED_SHORT_5_6_5, protTexData);  
  80.   
  81.     glViewport(0, 0, w, h);  
  82.     glMatrixMode(GL_PROJECTION);  
  83.     glLoadIdentity();  
  84.     // put the origin in the left-bottom corner  
  85.     glOrthof(0, w, 0, h, 0, 1); // l=0, r=w ; b=0, t=h  
  86.   
  87.     mReadyToRunBarrier.open();  
  88.   
  89.     /* 
  90.      *  We're now ready to accept clients... 
  91.      */  
  92.   
  93.     // start boot animation  
  94.     property_set("ctl.start""bootanim");  
  95.   
  96.     return NO_ERROR;  
  97. }  


2. SurfaceFlinger::threadLoop

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

 

[cpp]  view plain  copy
  1. bool SurfaceFlinger::threadLoop()  
  2. {  
  3.     waitForEvent();  //等待其他接口的signal event  
  4.   
  5.     // post surfaces (if needed)  
  6.     handlePageFlip();   //处理翻页机制  
  7.   
  8.     if (UNLIKELY(mHwWorkListDirty)) {  
  9.         // build the h/w work list  
  10.         handleWorkList();  
  11.     }  
  12.   
  13.     const DisplayHardware& hw(graphicPlane(0).displayHardware());  
  14.     if (LIKELY(hw.canDraw())) {  
  15.         // repaint the framebuffer (if needed)  
  16.   
  17.         const int index = hw.getCurrentBufferIndex();  
  18.         GraphicLog& logger(GraphicLog::getInstance());  
  19.   
  20.         logger.log(GraphicLog::SF_REPAINT, index);  
  21.         handleRepaint(); //合并所有layer并填充到buffer中去  
  22.   
  23.         // inform the h/w that we're done compositing  
  24.         logger.log(GraphicLog::SF_COMPOSITION_COMPLETE, index);  
  25.         hw.compositionComplete();  
  26.   
  27.         logger.log(GraphicLog::SF_SWAP_BUFFERS, index);  
  28.         postFramebuffer();  //互换front buffer和back buffer,调用EGL接口进行显示  
  29.   
  30.         logger.log(GraphicLog::SF_REPAINT_DONE, index);  
  31.     } else {  
  32.         // pretend we did the post  
  33.         hw.compositionComplete();  
  34.         usleep(16667); // 60 fps period,每秒刷新60次  
  35.     }  
  36.     return true;  
  37. }  


3. SurfaceFlinger::createSurface 

      提供给应用程序的主要接口,该接口可以创建一个surface,底层会根据参数创建layer以及分配内存(共2个buffer:front/back buffer),surface相关参数会反馈给上层。 

[cpp]  view plain  copy
  1. sp<ISurface> SurfaceFlinger::createSurface(  
  2.         ISurfaceComposerClient::surface_data_t* params,  
  3.         const String8& name,  
  4.         const sp<Client>& client,  
  5.         DisplayID d, uint32_t w, uint32_t h, PixelFormat format,  
  6.         uint32_t flags)  
  7. {  
  8.     sp<LayerBaseClient> layer;  
  9.     sp<ISurface> surfaceHandle;  
  10.   
  11.     if (int32_t(w|h) < 0) {  
  12.         LOGE("createSurface() failed, w or h is negative (w=%d, h=%d)",  
  13.                 int(w), int(h));  
  14.         return surfaceHandle;  
  15.     }  
  16.   
  17.     //LOGD("createSurface for pid %d (%d x %d)", pid, w, h);  
  18.     sp<Layer> normalLayer;  
  19.     //创建layer,根据参数(宽高格式)分配内存(共2个buffer:front/back buffer)  
  20.     switch (flags & eFXSurfaceMask) {  
  21.         case eFXSurfaceNormal:  
  22.             normalLayer = createNormalSurface(client, d, w, h, flags, format);  
  23.             layer = normalLayer;  
  24.             break;  
  25.         case eFXSurfaceBlur:  
  26.             // for now we treat Blur as Dim, until we can implement it  
  27.             // efficiently.  
  28.         case eFXSurfaceDim:  
  29.             layer = createDimSurface(client, d, w, h, flags);  
  30.             break;  
  31.         case eFXSurfaceScreenshot:  
  32.             layer = createScreenshotSurface(client, d, w, h, flags);  
  33.             break;  
  34.     }  
  35.   
  36.     if (layer != 0) {  
  37.         layer->initStates(w, h, flags);  
  38.         layer->setName(name);  
  39.         ssize_t token = addClientLayer(client, layer);  
  40.           
  41.         //创建surface  
  42.         surfaceHandle = layer->getSurface();  
  43.         if (surfaceHandle != 0) {  
  44.             params->token = token;  
  45.             params->identity = layer->getIdentity();  
  46.             if (normalLayer != 0) {  
  47.                 Mutex::Autolock _l(mStateLock);  
  48.                 mLayerMap.add(layer->getSurfaceBinder(), normalLayer);  
  49.             }  
  50.         }  
  51.   
  52.         setTransactionFlags(eTransactionNeeded);  
  53.     }  
  54.   
  55.     return surfaceHandle;  
  56. }  


 4. SurfaceFlinger::setTransactionState

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

[cpp]  view plain  copy
  1. void SurfaceFlinger::setTransactionState(const Vector<ComposerState>& state,  
  2.         int orientation) {  
  3.     Mutex::Autolock _l(mStateLock);  
  4.   
  5.     uint32_t flags = 0;  
  6.     if (mCurrentState.orientation != orientation) {  
  7.         if (uint32_t(orientation)<=eOrientation270 || orientation==42) {  
  8.             mCurrentState.orientation = orientation;  
  9.             flags |= eTransactionNeeded;  
  10.             mResizeTransationPending = true;  
  11.         } else if (orientation != eOrientationUnchanged) {  
  12.             LOGW("setTransactionState: ignoring unrecognized orientation: %d",  
  13.                     orientation);  
  14.         }  
  15.     }  
  16.   
  17.     const size_t count = state.size();  
  18.     for (size_t i=0 ; i<count ; i++) {  
  19.         const ComposerState& s(state[i]);  
  20.         sp<Client> client( static_cast<Client *>(s.client.get()) );  
  21.         flags |= setClientStateLocked(client, s.state);  
  22.     }  
  23.     if (flags) {  
  24.         setTransactionFlags(flags);  
  25.     }  
  26.   
  27.     signalEvent();  
  28.   
  29.     // if there is a transaction with a resize, wait for it to  
  30.     // take effect before returning.  
  31.     while (mResizeTransationPending) {  
  32.         status_t err = mTransactionCV.waitRelative(mStateLock, s2ns(5));  
  33.         if (CC_UNLIKELY(err != NO_ERROR)) {  
  34.             // just in case something goes wrong in SF, return to the  
  35.             // called after a few seconds.  
  36.             LOGW_IF(err == TIMED_OUT, "closeGlobalTransaction timed out!");  
  37.             mResizeTransationPending = false;  
  38.             break;  
  39.         }  
  40.     }  
  41. }  


5. SurfaceFlinger::composeSurfaces

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

6. SurfaceFlinger::postFramebuffer

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

 7. surface与layer

     从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、付费专栏及课程。

余额充值