Andorid Q
接着上文,当我们接收到来自App RenderThread线程渲染后的Surface之后,会在SurfaceFlinger收到下一次Vsync时做合成。 前面我们也稍微分析了一下,直接看handleMessageRefresh方法:
从上面trace上也可以看出收到Vsync后,sf首先调用handleMessageInvalidate检查时候需要进行合成。 如果需要就会调用方法handleMessageRefresh去做合成,最后将合成后的图像送入屏幕显示。
这里重点分析handleMessageRefresh.
一. handleMessageRefresh 1.1 SurfaceFlinger:handleMessageRefresh 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 void SurfaceFlinger::handleMessageRefresh () { ATRACE_CALL (); mRefreshPending = false ; const bool repaintEverything = mRepaintEverything.exchange (false ); preComposition (); rebuildLayerStacks (); calculateWorkingSet (); for (const auto & [token, display] : mDisplays) { beginFrame (display); prepareFrame (display); doDebugFlashRegions (display, repaintEverything); doComposition (display, repaintEverything); } logLayerStats (); postFrame (); postComposition (); mHadClientComposition = false ; mHadDeviceComposition = false ; for (const auto & [token, displayDevice] : mDisplays) { auto display = displayDevice->getCompositionDisplay (); const auto displayId = display->getId (); mHadClientComposition = mHadClientComposition || getHwComposer ().hasClientComposition (displayId); mHadDeviceComposition = mHadDeviceComposition || getHwComposer ().hasDeviceComposition (displayId); } mVsyncModulator.onRefreshed (mHadClientComposition); mLayersWithQueuedFrames.clear (); }
1.2 SurfaceFlinger:perComposition 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 void SurfaceFlinger::preComposition () { ATRACE_CALL (); ALOGV ("preComposition" ); mRefreshStartTime = systemTime (SYSTEM_TIME_MONOTONIC); bool needExtraInvalidate = false ; mDrawingState.traverseInZOrder ([&](Layer* layer) { if (layer->onPreComposition (mRefreshStartTime)) { needExtraInvalidate = true ; } }); if (needExtraInvalidate) { signalLayerUpdate (); } } void SurfaceFlinger::State::traverseInZOrder (const LayerVector::Visitor& visitor) const { layersSortedByZ.traverseInZOrder (stateSet, visitor); } State mDrawingState{LayerVector::StateSet::Drawing};
layersSortedByZ中存储的layer都是SurfaceFlinger.addClientLayer过程中添加的。
1.2.1 LayerVector:traverseInZOrder 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 void LayerVector::traverseInZOrder (StateSet stateSet, const Visitor& visitor) const { for (size_t i = 0 ; i < size (); i++) { const auto & layer = (*this )[i]; auto & state = (stateSet == StateSet::Current) ? layer->getCurrentState () : layer->getDrawingState (); if (state.zOrderRelativeOf != nullptr ) { continue ; } layer->traverseInZOrder (stateSet, visitor); } }
1.2.2 Layer:traverseInZOrder 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 void Layer::traverseInZOrder (LayerVector::StateSet stateSet, const LayerVector::Visitor& visitor) { bool skipRelativeZUsers = false ; const LayerVector list = makeTraversalList (stateSet, &skipRelativeZUsers); size_t i = 0 ; for (; i < list.size (); i++) { const auto & relative = list[i]; if (skipRelativeZUsers && relative->usingRelativeZ (stateSet)) { continue ; } if (relative->getZ () >= 0 ) { break ; } relative->traverseInZOrder (stateSet, visitor); } visitor (this ); for (; i < list.size (); i++) { const auto & relative = list[i]; if (skipRelativeZUsers && relative->usingRelativeZ (stateSet)) { continue ; } relative->traverseInZOrder (stateSet, visitor); } }
visitor这个就是lambda表达式:
1 2 3 4 5 6 7 mDrawingState.traverseInZOrder ([&](Layer* layer) { if (layer->onPreComposition (mRefreshStartTime)) { needExtraInvalidate = true ; } });
总的来说就是按顺序依次调用layer的onPreComposition方法,标记其mRefreshPending为false。
1.2.2.1 Layer:makeTraversalList 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 __attribute__((no_sanitize ("unsigned-integer-overflow" ))) LayerVector Layer::makeTraversalList ( LayerVector::StateSet stateSet, bool * outSkipRelativeZUsers) { LOG_ALWAYS_FATAL_IF (stateSet == LayerVector::StateSet::Invalid, "makeTraversalList received invalid stateSet" ); const bool useDrawing = stateSet == LayerVector::StateSet::Drawing; const LayerVector& children = useDrawing ? mDrawingChildren : mCurrentChildren; const State& state = useDrawing ? mDrawingState : mCurrentState; if (state.zOrderRelatives.size () == 0 ) { *outSkipRelativeZUsers = true ; return children; } LayerVector traverse (stateSet) ; for (const wp<Layer>& weakRelative : state.zOrderRelatives) { sp<Layer> strongRelative = weakRelative.promote (); if (strongRelative != nullptr ) { traverse.add (strongRelative); } } for (const sp<Layer>& child : children) { const State& childState = useDrawing ? child->mDrawingState : child->mCurrentState; if (childState.zOrderRelativeOf != nullptr ) { continue ; } traverse.add (child); } return traverse; }
回到1.2.2中。
1.2.3 Layer:BufferLayer::onPreComposition 1 2 3 4 5 6 7 8 bool BufferLayer::onPreComposition (nsecs_t refreshStartTime) { if (mBufferLatched) { Mutex::Autolock lock (mFrameEventHistoryMutex) ; mFrameEventHistory.addPreComposition (mCurrentFrameNumber, refreshStartTime); } mRefreshPending = false ; return hasReadyFrame (); }
记录开始刷新的时间,并返回该Layer是否具有可被合成的条件。
1.2.3.1 Layer:BufferLayer::hasReadyFrame 1 2 3 bool BufferLayer::hasReadyFrame () const { return hasFrameUpdate () || getSidebandStreamChanged () || getAutoRefresh (); }
1.2.3.2 Layer:BufferLayer:BufferQueueLayer 1 2 3 4 5 6 7 8 9 10 11 12 bool BufferQueueLayer::hasFrameUpdate () const { return mQueuedFrames > 0 ; } bool BufferQueueLayer::getAutoRefresh () const { return mAutoRefresh; } bool BufferQueueLayer::getSidebandStreamChanged () const { return mSidebandStreamChanged; }
1.3 SurfaceFlinger:rebuildLayerStacks 这个方法比较长,慢慢看。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 void SurfaceFlinger::rebuildLayerStacks () { ATRACE_CALL (); ALOGV ("rebuildLayerStacks" ); Mutex::Autolock lock (mDolphinStateLock) ; if (CC_UNLIKELY (mVisibleRegionsDirty)) { ATRACE_NAME ("rebuildLayerStacks VR Dirty" ); mVisibleRegionsDirty = false ; invalidateHwcGeometry (); for (const auto & pair : mDisplays) { const auto & displayDevice = pair.second; auto display = displayDevice->getCompositionDisplay (); const auto & displayState = display->getState (); Region opaqueRegion; Region dirtyRegion; compositionengine::Output::OutputLayers layersSortedByZ; Vector<sp<Layer>> deprecated_layersSortedByZ; Vector<sp<Layer>> layersNeedingFences; const ui::Transform& tr = displayState.transform; const Rect bounds = displayState.bounds; if (displayState.isEnabled) { computeVisibleRegions (displayDevice, dirtyRegion, opaqueRegion); mDrawingState.traverseInZOrder ([&](Layer* layer) { auto compositionLayer = layer->getCompositionLayer (); if (compositionLayer == nullptr ) { return ; } const auto displayId = displayDevice->getId (); sp<compositionengine::LayerFE> layerFE = compositionLayer->getLayerFE (); LOG_ALWAYS_FATAL_IF (layerFE.get () == nullptr ); bool needsOutputLayer = false ; if (display->belongsInOutput (layer->getLayerStack (), layer->getPrimaryDisplayOnly ())) { Region drawRegion (tr.transform ( layer->visibleNonTransparentRegion)); drawRegion.andSelf (bounds); if (!drawRegion.isEmpty ()) { needsOutputLayer = true ; } } if (needsOutputLayer) { layersSortedByZ.emplace_back ( display->getOrCreateOutputLayer (displayId, compositionLayer, layerFE)); deprecated_layersSortedByZ.add (layer); auto & outputLayerState = layersSortedByZ.back ()->editState (); outputLayerState.visibleRegion = tr.transform (layer->visibleRegion.intersect (displayState.viewport)); } else if (displayId) { bool hasExistingOutputLayer = display->getOutputLayerForLayer (compositionLayer.get ()) != nullptr ; bool hasQueuedFrames = std::find (mLayersWithQueuedFrames.cbegin (), mLayersWithQueuedFrames.cend (), layer) != mLayersWithQueuedFrames.cend (); if (hasExistingOutputLayer && hasQueuedFrames) { layersNeedingFences.add (layer); } } }); } display->setOutputLayersOrderedByZ (std::move (layersSortedByZ)); displayDevice->setVisibleLayersSortedByZ (deprecated_layersSortedByZ); displayDevice->setLayersNeedingFences (layersNeedingFences); Region undefinedRegion{bounds}; undefinedRegion.subtractSelf (tr.transform (opaqueRegion)); display->editState ().undefinedRegion = undefinedRegion; display->editState ().dirtyRegion.orSelf (dirtyRegion); } } }
总的来说,rebuildLayerStacks就是反向遍历Z轴计算各个Layer的可视区域,之后 顺序遍历Z轴Layer将相关信息更新到对应Diplay中。
1.4 SurfaceFlinger::calculateWorkingSet 再看这个方法之前先瞄一眼各个Layer之前的关系:
上图中标红的appId就是在此方法内的latchCompositionState方法中赋值的。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 void SurfaceFlinger::calculateWorkingSet () { ATRACE_CALL (); ALOGV (__FUNCTION__); if (CC_UNLIKELY (mGeometryInvalid)) { mGeometryInvalid = false ; for (const auto & [token, displayDevice] : mDisplays) { auto display = displayDevice->getCompositionDisplay (); uint32_t zOrder = 0 ; for (auto & layer : display->getOutputLayersOrderedByZ ()) { auto & compositionState = layer->editState (); compositionState.forceClientComposition = false ; if (!compositionState.hwc || mDebugDisableHWC || mDebugRegion) { compositionState.forceClientComposition = true ; } compositionState.z = zOrder++; layer->getLayerFE ().latchCompositionState (layer->getLayer ().editState ().frontEnd, true ); layer->updateCompositionState (true ); layer->writeStateToHWC (true ); } } } for (const auto & [token, displayDevice] : mDisplays) { auto display = displayDevice->getCompositionDisplay (); const auto displayId = display->getId (); if (!displayId) { continue ; } auto * profile = display->getDisplayColorProfile (); if (mDrawingState.colorMatrixChanged) { display->setColorTransform (mDrawingState.colorMatrix); } Dataspace targetDataspace = Dataspace::UNKNOWN; if (useColorManagement) { ColorMode colorMode; RenderIntent renderIntent; pickColorMode (displayDevice, &colorMode, &targetDataspace, &renderIntent); display->setColorMode (colorMode, targetDataspace, renderIntent); } for (auto & layer : displayDevice->getVisibleLayersSortedByZ ()) { ...... const auto & displayState = display->getState (); layer->setPerFrameData (displayDevice, displayState.transform, displayState.viewport, displayDevice->getSupportedPerFrameMetadata (), isHdrColorMode (displayState.colorMode) ? Dataspace::UNKNOWN : targetDataspace); } } mDrawingState.colorMatrixChanged = false ; for (const auto & [token, displayDevice] : mDisplays) { auto display = displayDevice->getCompositionDisplay (); for (auto & layer : displayDevice->getVisibleLayersSortedByZ ()) { auto & layerState = layer->getCompositionLayer ()->editState ().frontEnd; layerState.compositionType = static_cast <Hwc2::IComposerClient::Composition>( layer->getCompositionType (displayDevice)); } } }
这里建立HWC中的Layer列表:
按照Z轴的顺序,依次给可见OutputLayer在HWC中建立映射也就是将Layer的状态信息放在CompositionState中,并重新计算OutputLayer的几何状态,写入HWC
将Layer的mActiveBuffer设置到HWComposer中
1.5 SurfaceFlinger::beginFrame 开始合成前的准备。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 void SurfaceFlinger::beginFrame (const sp<DisplayDevice>& displayDevice) { auto display = displayDevice->getCompositionDisplay (); const auto & displayState = display->getState (); bool dirty = !display->getDirtyRegion (false ).isEmpty (); bool empty = displayDevice->getVisibleLayersSortedByZ ().size () == 0 ; bool wasEmpty = !displayState.lastCompositionHadVisibleLayers; bool mustRecompose = dirty && !(empty && wasEmpty); const char flagPrefix[] = {'-' , '+' }; static_cast <void >(flagPrefix); ALOGV_IF (displayDevice->isVirtual (), "%s: %s composition for %s (%cdirty %cempty %cwasEmpty)" , __FUNCTION__, mustRecompose ? "doing" : "skipping" , displayDevice->getDebugName ().c_str (), flagPrefix[dirty], flagPrefix[empty], flagPrefix[wasEmpty]); display->getRenderSurface ()->beginFrame (mustRecompose); if (mustRecompose) { display->editState ().lastCompositionHadVisibleLayers = !empty; } }
这个DiplayDevice是怎么初始化的呢,后续再看。
1.6 SurfaceFlinger::doComposition 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 void SurfaceFlinger::doComposition (const sp<DisplayDevice>& displayDevice, bool repaintEverything) { ATRACE_CALL (); ALOGV ("doComposition" ); auto display = displayDevice->getCompositionDisplay (); const auto & displayState = display->getState (); if (displayState.isEnabled) { const Region dirtyRegion = display->getDirtyRegion (repaintEverything); doDisplayComposition (displayDevice, dirtyRegion); display->editState ().dirtyRegion.clear (); display->getRenderSurface ()->flip (); } postFramebuffer (displayDevice); }
1.6.1 SurfaceFlinger::doDisplayComposition 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 void SurfaceFlinger::doDisplayComposition (const sp<DisplayDevice>& displayDevice, const Region& inDirtyRegion) { auto display = displayDevice->getCompositionDisplay (); if (!displayDevice->getId () && inDirtyRegion.isEmpty ()) { ALOGV ("Skipping display composition" ); return ; } ALOGV ("doDisplayComposition" ); base::unique_fd readyFence; if (!doComposeSurfaces (displayDevice, Region::INVALID_REGION, &readyFence)) return ; display->getRenderSurface ()->queueBuffer (std::move (readyFence)); }
1.6.2 SurfaceFlinger::doComposeSurfaces 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 bool SurfaceFlinger::doComposeSurfaces (const sp<DisplayDevice>& displayDevice, const Region& debugRegion, base::unique_fd* readyFence) { ATRACE_CALL (); ALOGV ("doComposeSurfaces" ); auto display = displayDevice->getCompositionDisplay (); const auto & displayState = display->getState (); const auto displayId = display->getId (); auto & renderEngine = getRenderEngine (); const bool supportProtectedContent = renderEngine.supportsProtectedContent (); const Region bounds (displayState.bounds) ; const DisplayRenderArea renderArea (displayDevice) ; const bool hasClientComposition = getHwComposer ().hasClientComposition (displayId); ATRACE_INT ("hasClientComposition" , hasClientComposition); bool applyColorMatrix = false ; renderengine::DisplaySettings clientCompositionDisplay; std::vector<renderengine::LayerSettings> clientCompositionLayers; sp<GraphicBuffer> buf; base::unique_fd fd; if (hasClientComposition) { ALOGV ("hasClientComposition" ); ...... buf = display->getRenderSurface ()->dequeueBuffer (&fd); if (buf == nullptr ) { ALOGW ("Dequeuing buffer for display [%s] failed, bailing out of " "client composition for this frame" , displayDevice->getDisplayName ().c_str ()); return false ; } clientCompositionDisplay.physicalDisplay = displayState.scissor; clientCompositionDisplay.clip = displayState.scissor; const ui::Transform& displayTransform = displayState.transform; clientCompositionDisplay.globalTransform = displayTransform.asMatrix4 (); clientCompositionDisplay.orientation = displayState.orientation; const auto * profile = display->getDisplayColorProfile (); Dataspace outputDataspace = Dataspace::UNKNOWN; if (profile->hasWideColorGamut ()) { outputDataspace = displayState.dataspace; } clientCompositionDisplay.outputDataspace = outputDataspace; clientCompositionDisplay.maxLuminance = profile->getHdrCapabilities ().getDesiredMaxLuminance (); const bool hasDeviceComposition = getHwComposer ().hasDeviceComposition (displayId); const bool skipClientColorTransform = getHwComposer () .hasDisplayCapability (displayId, HWC2::DisplayCapability::SkipClientColorTransform); applyColorMatrix = !hasDeviceComposition && !skipClientColorTransform; if (applyColorMatrix) { clientCompositionDisplay.colorTransform = displayState.colorTransformMat; } } ALOGV ("Rendering client layers" ); bool firstLayer = true ; Region clearRegion = Region::INVALID_REGION; for (auto & layer : displayDevice->getVisibleLayersSortedByZ ()) { const Region viewportRegion (displayState.viewport) ; const Region clip (viewportRegion.intersect(layer->visibleRegion)) ; ALOGV ("Layer: %s" , layer->getName ().string ()); ALOGV (" Composition type: %s" , toString (layer->getCompositionType (displayDevice)).c_str ()); if (!clip.isEmpty ()) { switch (layer->getCompositionType (displayDevice)) { ...... case Hwc2::IComposerClient::Composition::CLIENT: { renderengine::LayerSettings layerSettings; bool prepared = layer->prepareClientLayer (renderArea, clip, clearRegion, supportProtectedContent, layerSettings); if (prepared) { clientCompositionLayers.push_back (layerSettings); } break ; } default : break ; } } else { ALOGV (" Skipping for empty clip" ); } firstLayer = false ; } if (hasClientComposition) { clientCompositionDisplay.clearRegion = clearRegion; const bool expensiveRenderingExpected = clientCompositionDisplay.outputDataspace == Dataspace::DISPLAY_P3; if (expensiveRenderingExpected && displayId) { mPowerAdvisor.setExpensiveRenderingExpected (*displayId, true ); } ....... renderEngine.drawLayers (clientCompositionDisplay, clientCompositionLayers, buf->getNativeBuffer (), true , std::move (fd), readyFence); } else if (displayId) { mPowerAdvisor.setExpensiveRenderingExpected (*displayId, false ); } return true ; }
先请求GraphicBuffer,然后通过GLESRenderEngine合成所有Layer.
二. 计算Layer可视区域 一般来讲,我们手机App Layer如下:
计算可视区域的时候,从Z轴大的开始遍历计算。这样做的好处是,如果计算到某一层Layer时,完全不透明的可视化区域已经占满整个屏幕,那么这之下的Layer可视化区域就可以不用计算了。
在开始阅读代码之前,还是有必要理清楚:
可见区域(Visible Region)
透明区域(Transparent Region)
半透明区域(Translucent Region)
完全不透明区域(Opaque Region)
被覆盖区域(Covered Region)
如下图:
如果A1为W2中半透明区域,A2在W1中全透明,W2中不透明,A3为W1中完全不透明区域。 则对于Surface W2而言,可见区域为W2-A1-A3.
2.1 SurfaceFlinger:computeVisibleRegions 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 void SurfaceFlinger::computeVisibleRegions (const sp<const DisplayDevice>& displayDevice, Region& outDirtyRegion, Region& outOpaqueRegion) { ATRACE_CALL (); ALOGV ("computeVisibleRegions" ); auto display = displayDevice->getCompositionDisplay (); Region aboveOpaqueLayers; Region aboveCoveredLayers; Region dirty; outDirtyRegion.clear (); Layer* layerOfInterest = NULL ; bool bIgnoreLayer = false ; mDrawingState.traverseInReverseZOrder ([&](Layer* layer) { if (layer->isSecureDisplay ()) { bIgnoreLayer = true ; if (displayDevice->isPrimary ()) { layerOfInterest = layer; } return ; } }); mDrawingState.traverseInReverseZOrder ([&](Layer* layer) { const Layer::State& s (layer->getDrawingState ()); if (!display->belongsInOutput (layer->getLayerStack (), layer->getPrimaryDisplayOnly ())) { return ; } if (bIgnoreLayer && layerOfInterest != layer) { Region visibleNonTransRegion; visibleNonTransRegion.set (Rect (0 , 0 )); layer->setVisibleNonTransparentRegion (visibleNonTransRegion); return ; } Region opaqueRegion; Region visibleRegion; Region coveredRegion; Region transparentRegion; if (CC_LIKELY (layer->isVisible ())) { const bool translucent = !layer->isOpaque (s); Rect bounds (layer->getScreenBounds ()); visibleRegion.set (bounds); ui::Transform tr = layer->getTransform (); if (!visibleRegion.isEmpty ()) { if (translucent) { if (tr.preserveRects ()) { transparentRegion = tr.transform (layer->getActiveTransparentRegion (s)); } else { transparentRegion.clear (); } } const int32_t layerOrientation = tr.getOrientation (); if (layer->getAlpha () == 1.0f && !translucent && layer->getRoundedCornerState ().radius == 0.0f && ((layerOrientation & ui::Transform::ROT_INVALID) == false )) { opaqueRegion = visibleRegion; } } } if (visibleRegion.isEmpty ()) { layer->clearVisibilityRegions (); return ; } coveredRegion = aboveCoveredLayers.intersect (visibleRegion); aboveCoveredLayers.orSelf (visibleRegion); visibleRegion.subtractSelf (aboveOpaqueLayers); if (layer->contentDirty) { dirty = visibleRegion; dirty.orSelf (layer->visibleRegion); layer->contentDirty = false ; } else { const Region newExposed = visibleRegion - coveredRegion; const Region oldVisibleRegion = layer->visibleRegion; const Region oldCoveredRegion = layer->coveredRegion; const Region oldExposed = oldVisibleRegion - oldCoveredRegion; dirty = (visibleRegion&oldCoveredRegion) | (newExposed-oldExposed); } dirty.subtractSelf (aboveOpaqueLayers); outDirtyRegion.orSelf (dirty); aboveOpaqueLayers.orSelf (opaqueRegion); layer->setVisibleRegion (visibleRegion); layer->setCoveredRegion (coveredRegion); layer->setVisibleNonTransparentRegion ( visibleRegion.subtract (transparentRegion)); }); outOpaqueRegion = aboveOpaqueLayers; }
三. 小结 handleMessageRefresh – SF合成所有Layer大概步骤如下:
preComposition 合成前遍历所有layer, 处理处于Drawing状态的Layer是否被更新了
rebuildLayerStacks 反向遍历Z轴计算各个Layer的可视区域,之后 顺序遍历Z轴Layer将相关信息更新到对应Diplay中。
calculateWorkingSet 这里建立HWC中的Layer列表:
按照Z轴的顺序,依次给可见OutputLayer在HWC中建立映射也就是将Layer的状态信息放在CompositionState中,并重新计算OutputLayer的几何状态,写入HWC
将Layer的mActiveBuffer设置到HWComposer中
doComposition 正式的合成处理,使用渲染引擎合成所有layer,然后就是申请GraphicBuffer,向其中填充帧数据, 最终给到硬件帧缓冲区
postComposition && clear mLayersWithQueuedFrames 回调每个layer的onPostComposition并清空mLayersWithQueuedFrames,下一次vsync来到时,会在handlePageFlip中重新添加
大概流程搞清楚了,接下来细细分析比如GraphicBuffer和Fence机制的工作原理, 等等,HWC好像还没有了解,先看看这个是怎么工作的。
参考资料
[Android Synchronization Fences – An Introduction]http://netaz.blogspot.com/2013/10/android-fences-introduction-in-any.html
[Android 4.0.3 显示系统深入理解]https://www.linuxidc.com/Linux/2012-03/55898p4.htm
[Clang 10 documentation ATTRIBUTES IN CLANG]https://clang.llvm.org/docs/AttributeReference.html
[「Android」SurfaceFlinger分析]https://www.cnblogs.com/1996swg/p/9790209.html
[显示系统:第005课_Vsync机制:第007节_rebuildLayerStacks源码分析]http://www.pianshen.com/article/8541345041/
[Android系统Surface机制的SurfaceFlinger服务渲染应用程序UI的过程分析]https://blog.csdn.net/luoshengyang/article/details/8079456
[Android Region代码分析]https://blog.csdn.net/fuyajun01/article/details/25551717?_t=t