Android latchbuffer

SurfaceFlinger

                -          handleMessageInvalidate

                -         handlePageFlip (layer->latchBuffer)

       Layer

                     -          latchBuffer (mSurfaceFlingerConsumer->updateTexImage)

             SurfaceFlingerConsumer

                           -         updateTexImage

                           -         GLConsumer::updateAndReleaseLocked

 

[cpp]  view plain  copy
  1. Region Layer::latchBuffer(bool& recomputeVisibleRegions)  
  2. {  
  3.     Region outDirtyRegion;  
  4.     if (mQueuedFrames > 0) {  
  5.   
  6.         // Capture the old state of the layer for comparisons later  
  7.         const bool oldOpacity = isOpaque();  
  8.         sp<GraphicBuffer> oldActiveBuffer = mActiveBuffer;  
  9.   
  10.         Reject r(mDrawingState, getCurrentState(), recomputeVisibleRegions);  
  11.   
  12.         status_t updateResult = mSurfaceFlingerConsumer-><strong><span style="color:#ff0000;">updateTexImage</span></strong>(&r);  
  13.   
  14.         // update the active buffer  
  15.         <span style="color:#ff0000;">mActiveBuffer </span>= mSurfaceFlingerConsumer->getCurrentBuffer();  
  16.         if (mActiveBuffer == NULL) {  
  17.             // this can only happen if the very first buffer was rejected.  
  18.             return outDirtyRegion;  
  19.         }  
  20.   
  21.         // FIXME: postedRegion should be dirty & bounds  
  22.         const Layer::State& s(getDrawingState());  
  23.         Region dirtyRegion(Rect(s.active.w, s.active.h));  
  24.   
  25.         // transform the dirty region to window-manager space  
  26.         outDirtyRegion = (s.transform.transform(dirtyRegion));  
  27.     }  
  28.     return outDirtyRegion;  
  29. }  

 

latchBuffer 的作用可以看到更新 mActiveBuffer。 具体是如何来更新 mActiveBuffer  的, 是通过  updateTexImage 方法。

 

[cpp]  view plain  copy
  1. status_t SurfaceFlingerConsumer::updateTexImage(BufferRejecter* rejecter)  
  2. {  
  3.   
  4.     BufferQueue::BufferItem item;  
  5.   
  6.     // Acquire the next buffer.  
  7.     // In asynchronous mode the list is guaranteed to be one buffer  
  8.     // deep, while in synchronous mode we use the oldest buffer.  
  9.     err = <span style="color:#ff0000;">acquireBufferLocked</span>(&item, computeExpectedPresent());  
  10.   
  11.     // We call the rejecter here, in case the caller has a reason to  
  12.     // not accept this buffer.  This is used by SurfaceFlinger to  
  13.     // reject buffers which have the wrong size  
  14.     int buf = item.mBuf;  
  15.     if (rejecter && rejecter-><span style="color:#ff0000;">reject</span>(mSlots[buf].mGraphicBuffer, item)) {  
  16.         releaseBufferLocked(buf, mSlots[buf].mGraphicBuffer, EGL_NO_SYNC_KHR);  
  17.         return NO_ERROR;  
  18.     }  
  19.   
  20.     // Release the previous buffer.  
  21.     err = <span style="color:#ff0000;">updateAndReleaseLocked</span>(item);  
  22.     if (err != NO_ERROR) {  
  23.         return err;  
  24.     }  
  25.   
  26.     return err;  
  27. }  

updateTexImage 的作用也很明确, 理解BufferQueue的模型, SurfaceFlinger作为Consumer端, 其主要操作为 acquire 和 release,首先通过 acquireBufferLocked 获取到一个 BufferItem, 然后通过rejecter去确认这个 BufferItem 是否合法, 最后通过 updateAndReleaseLocked 去更新刚才获取的 BufferItem, 并释放之前的 BufferItem。

 

[cpp]  view plain  copy
  1. status_t GLConsumer::updateAndReleaseLocked(const BufferQueue::BufferItem& item)  
  2. {  
  3.     int buf = item.mBuf;  
  4.   
  5.     // If the mEglSlot entry is empty, create an EGLImage for the gralloc  
  6.     // buffer currently in the slot in ConsumerBase.  
  7.     //  
  8.     // We may have to do this even when item.mGraphicBuffer == NULL (which  
  9.     // means the buffer was previously acquired), if we destroyed the  
  10.     // EGLImage when detaching from a context but the buffer has not been  
  11.     // re-allocated.  
  12.     if (mEglSlots[buf].mEglImage == EGL_NO_IMAGE_KHR) {  
  13.         EGLImageKHR image = <span style="color:#ff0000;">createImage</span>(mEglDisplay,  
  14.                 mSlots[buf].mGraphicBuffer, item.mCrop);  
  15.         mEglSlots[buf].mEglImage = image;  
  16.         mEglSlots[buf].mCropRect = item.mCrop;  
  17.     }  
  18.   
  19.     // Do whatever sync ops we need to do before releasing the old slot.  
  20.     err = <span style="color:#ff0000;">syncForReleaseLocked</span>(mEglDisplay);  
  21.   
  22.     // release old buffer  
  23.     if (mCurrentTexture != BufferQueue::INVALID_BUFFER_SLOT) {  
  24.         status_t status = <span style="color:#ff0000;">releaseBufferLocked</span>(  
  25.                 mCurrentTexture, mCurrentTextureBuf, mEglDisplay,  
  26.                 mEglSlots[mCurrentTexture].mEglFence);  
  27.         if (status < NO_ERROR) {  
  28.             ST_LOGE("updateAndRelease: failed to release buffer: %s (%d)",  
  29.                    strerror(-status), status);  
  30.             err = status;  
  31.             // keep going, with error raised [?]  
  32.         }  
  33.     }  
  34.   
  35.     // <span style="color:#ff0000;">Update the GLConsumer state.  
  36. </span>    mCurrentTexture = buf;  
  37.     mCurrentTextureBuf = mSlots[buf].mGraphicBuffer;  
  38.     mCurrentCrop = item.mCrop;  
  39.   
  40.     return err;  
  41. }  


updateAndReleaseLocked:

1 : 通过 createImage 生成一个 EGLImageKHR(通过它来生成 Opengl 纹理) 对象。

2 : 通过 syncForReleaseLocked,来向当前的 GraphicBuffer 添加一个 ReleaseFence。 (GraphicBuffer -- GPU同步

3 : 通过 releaseBufferLocked , 来释放当前的 GraphicBuffer。 (GraphicBuffer -- CPU同步

4 : 最后更新下当前的状态.

最后做一些扫尾更新当前状态的操作。

[cpp]  view plain  copy
  1. status_t GLConsumer::syncForReleaseLocked(EGLDisplay dpy) {  
  2.     if (mCurrentTexture != BufferQueue::INVALID_BUFFER_SLOT) {  
  3.         if (SyncFeatures::getInstance().useNativeFenceSync()) {  
  4.             <span style="color:#000000;">EGLSyncKHR </span>sync = <span style="color:#ff0000;">eglCreateSyncKHR</span>(dpy,  
  5.                     EGL_SYNC_NATIVE_FENCE_ANDROID, NULL);  
  6.             glFlush();  
  7.             int fenceFd = eglDupNativeFenceFDANDROID(dpy, sync);  
  8.             eglDestroySyncKHR(dpy, sync);  
  9.             sp<Fence> <span style="color:#ff0000;">fence</span>(new Fence(fenceFd));  
  10.             status_t err = <span style="color:#ff0000;">addReleaseFenceLocked</span>(mCurrentTexture,  
  11.                     mCurrentTextureBuf, fence);  
  12.         }   
  13.     }  
  14.   
  15.     return OK;  
  16. }  

syncForReleaseLocked 的作用比较特殊: (实际效果是 给当前的 buf 关联一个 fence, 当屏幕被新的 buf 更新的时候, 这个 fence 被触发。 表现的效果是 dequeueBuffer 的时候会检查这个fence是否被触发)

首先通过 eglCreateSyncKHR,生成一个 EGLSyncKHR 对象, 然后再通过 EGLSyncKHR 对象生成一个Fence对象。

最后通过 addReleaseFenceLocked 把 mCurrentTextureBuf 和 fence对象关联起来。


  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值