继上篇《Android Q SurfaceFlinger合成(一)》中SF对INVALIDATE信息处理,针对Layer属性变化、显示设备变化等情况处理,将mCurrentState提交到mDrawingState。然后遍历mDrawingState的Layer,将新的Buffer内容更新绑定到Layer纹理对象。经过这些流程,决定是否需要SF进行合成刷新,如果需要则调用handleMessageRefresh
开始合成处理。
signalRefresh 在onMessageReceivedINVALIDATE信息处理完成后,如果需要刷新,则会触发刷新:
SF.cpp 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 void SurfaceFlinger::onMessageReceived (int32_t what) NO_THREAD_SAFETY_ANALYSIS { ATRACE_CALL(); switch (what) { case MessageQueue::INVALIDATE: { ...... refreshNeeded |= mRepaintEverything; if (refreshNeeded && CC_LIKELY(mBootStage != BootStage::BOOTLOADER)) { signalRefresh(); } break ; } case MessageQueue::REFRESH: { handleMessageRefresh(); break ; } } } void SurfaceFlinger::signalRefresh () { mRefreshPending = true ; mEventQueue.refresh(); }
需要刷新的情况:
有新的Transaction处理
PageFlip时,有Buffer更新
有重新合成请求时mRepaintEverything,这是响应HWC的请求时触发的。
MessageQueue分发refresh frameworks/native/services/surfaceflinger/Scheduler/MessageQueue.cpp 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 void MessageQueue::refresh () { mHandler->dispatchRefresh(); } void MessageQueue::Handler::dispatchRefresh() { if ((android_atomic_or(eventMaskRefresh, &mEventMask) & eventMaskRefresh) == 0 ) { mQueue.mLooper->sendMessage(this , Message(MessageQueue::REFRESH)); } } void MessageQueue::Handler::handleMessage(const Message& message) { switch (message.what) { case INVALIDATE: android_atomic_and(~eventMaskInvalidate, &mEventMask); mQueue.mFlinger->onMessageReceived(message.what); break ; case REFRESH: android_atomic_and(~eventMaskRefresh, &mEventMask); mQueue.mFlinger->onMessageReceived(message.what); break ;
handleMessageRefresh刷新总流程 handleMessageRefresh函数包含了刷新(合成)一帧显示数据的所有流程。
SF.cpp 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::onMessageReceived (int32_t what) NO_THREAD_SAFETY_ANALYSIS { ATRACE_CALL();d switch (what) { case MessageQueue::INVALIDATE: { ...... case MessageQueue::REFRESH: { handleMessageRefresh(); break ; } } } 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(); }
preComposition合成前预处理 SF.cpp 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 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(); } }
其中onPreComposition
函数的返回值针对不同Layer:
ColorLayer和ContainLayer固定返回false
BufferLayer如下:
frameworks/native/services/surfaceflinger/BufferLayer.cpp 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 bool BufferLayer::onPreComposition (nsecs_t refreshStartTime) { if (mBufferLatched) { Mutex::Autolock lock (mFrameEventHistoryMutex) ; mFrameEventHistory.addPreComposition(mCurrentFrameNumber, refreshStartTime); } mRefreshPending = false ; return hasReadyFrame(); } bool BufferLayer::hasReadyFrame () const { return hasFrameUpdate() || getSidebandStreamChanged() || getAutoRefresh(); } bool BufferQueueLayer::hasFrameUpdate () const { return mQueuedFrames > 0 ; } bool BufferQueueLayer::getSidebandStreamChanged () const { return mSidebandStreamChanged; } bool BufferQueueLayer::getAutoRefresh () const { return mAutoRefresh; }
rebuildLayerStacks重构Layer栈 执行该函数,将完成创建Layer栈。
此时需要进行合成显示的数据已经被更新到每个Display各自的layersSortedByZ
中。
SF.cpp 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 void SurfaceFlinger::invalidateHwcGeometry () { mGeometryInvalid = true ; } void SurfaceFlinger::rebuildLayerStacks () { ATRACE_CALL(); ALOGV("rebuildLayerStacks" ); if (CC_UNLIKELY(mVisibleRegionsDirty)) { ATRACE_NAME("rebuildLayerStacks VR Dirty" ); mVisibleRegionsDirty = false ; invalidateHwcGeometry(); for (const auto & pair : mDisplays) { ...... 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); } } }
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 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(); mDrawingState.traverseInReverseZOrder([&](Layer* layer) { const Layer::State& s(layer->getDrawingState()); if (!display->belongsInOutput(layer->getLayerStack(), layer->getPrimaryDisplayOnly())) { 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; } } } ...... 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; }
calculateWorkingSet 在Android P中是用的setUpHWComposer
函数,Q升级后将其分成几个单独的函数。以下是第一个:
SF.cpp 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::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()) { if (layer->isHdrY410()) { layer->forceClientComposition(displayDevice); } else if ((layer->getDataSpace() == Dataspace::BT2020_PQ || layer->getDataSpace() == Dataspace::BT2020_ITU_PQ) && !profile->hasHDR10Support()) { layer->forceClientComposition(displayDevice); } else if ((layer->getDataSpace() == Dataspace::BT2020_HLG || layer->getDataSpace() == Dataspace::BT2020_ITU_HLG) && !profile->hasHLGSupport()) { layer->forceClientComposition(displayDevice); } if (layer->getRoundedCornerState().radius > 0.0f ) { layer->forceClientComposition(displayDevice); } if (layer->getForceClientComposition(displayDevice)) { ALOGV("[%s] Requesting Client composition" , layer->getName().string ()); layer->setCompositionType(displayDevice, Hwc2::IComposerClient::Composition::CLIENT); continue ; } 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)); } } }
颜色矩阵如下,可以在开发这选项中设置,有模拟颜色空间
选项。其支持的transform主要有:
system/core/libsystem/include/system/graphics-base-v1.0.h 1 2 3 4 5 6 7 8 9 typedef enum { HAL_COLOR_TRANSFORM_IDENTITY = 0 , HAL_COLOR_TRANSFORM_ARBITRARY_MATRIX = 1 , HAL_COLOR_TRANSFORM_VALUE_INVERSE = 2 , HAL_COLOR_TRANSFORM_GRAYSCALE = 3 , HAL_COLOR_TRANSFORM_CORRECT_PROTANOPIA = 4 , HAL_COLOR_TRANSFORM_CORRECT_DEUTERANOPIA = 5 , HAL_COLOR_TRANSFORM_CORRECT_TRITANOPIA = 6 , } android_color_transform_t ;
writeStateToHWC设置Layer几何尺寸 OutputLayer.cpp是Android新分离出来的文件,writeStateToHWC函数也是分离成一个单独的函数,以供SurfaceFlinger的calculateWorkingSet函数调用。
frameworks/native/services/surfaceflinger/CompositionEngine/src/OutputLayer.cpp 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 void OutputLayer::writeStateToHWC (bool includeGeometry) const { if (!mState.hwc) { return ; } auto & hwcLayer = (*mState.hwc).hwcLayer; if (!hwcLayer) { ALOGE("[%s] failed to write composition state to HWC -- no hwcLayer for output %s" , mLayerFE->getDebugName(), mOutput.getName().c_str()); return ; } if (includeGeometry) { if (auto error = hwcLayer->setDisplayFrame(mState.displayFrame); error != HWC2::Error::None) {... } if (auto error = hwcLayer->setSourceCrop(mState.sourceCrop); error != HWC2::Error::None) { ... } if (auto error = hwcLayer->setZOrder(mState.z); error != HWC2::Error::None) { ... } if (auto error = hwcLayer->setTransform(static_cast <HWC2::Transform>(mState.bufferTransform)); error != HWC2::Error::None) { ... } const auto & outputIndependentState = mLayer->getState().frontEnd; if (auto error = hwcLayer->setBlendMode( static_cast <HWC2::BlendMode>(outputIndependentState.blendMode)); error != HWC2::Error::None) { ... } if (auto error = hwcLayer->setPlaneAlpha(outputIndependentState.alpha); error != HWC2::Error::None) { ... } if (auto error = hwcLayer->setInfo(outputIndependentState.type, outputIndependentState.appId); error != HWC2::Error::None) { A... } } }
其中setBlendModes
设置混合模式(两个Layer直接的混合方式),主要有以下几种:
hardware/libhardware/include/hardware/hwcomposer2.h 1 2 3 4 5 6 7 8 9 10 11 12 13 typedef enum { HWC2_BLEND_MODE_INVALID = 0 , HWC2_BLEND_MODE_NONE = 1 , HWC2_BLEND_MODE_PREMULTIPLIED = 2 , HWC2_BLEND_MODE_COVERAGE = 3 , } hwc2_blend_mode_t ;
setPerFrameData设置每一层Layer显示数据
ColorLayer::setPerFrameData
frameworks/native/services/surfaceflinger/ColorLayer.cpp 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 void ColorLayer::setPerFrameData (const sp<const DisplayDevice>& display, const ui::Transform& transform, const Rect& viewport, int32_t , const ui::Dataspace targetDataspace) { ...... auto error = hwcLayer->setVisibleRegion(visible); ... error = hwcLayer->setDataspace(dataspace); ... auto & layerCompositionState = getCompositionLayer()->editState().frontEnd; layerCompositionState.dataspace = mCurrentDataSpace; half4 color = getColor(); error = hwcLayer->setColor({static_cast <uint8_t >(std ::round(255.0f * color.r)), static_cast <uint8_t >(std ::round(255.0f * color.g)), static_cast <uint8_t >(std ::round(255.0f * color.b)), 255 }); ... layerCompositionState.color = {static_cast <uint8_t >(std ::round(255.0f * color.r)), static_cast <uint8_t >(std ::round(255.0f * color.g)), static_cast <uint8_t >(std ::round(255.0f * color.b)), 255 }; error = hwcLayer->setTransform(HWC2::Transform::None); if (error != HWC2::Error::None) { ALOGE("[%s] Failed to clear transform: %s (%d)" , mName.string (), to_string(error).c_str(), static_cast <int32_t >(error)); } ... error = hwcLayer->setColorTransform(getColorTransform()); if (error != HWC2::Error::None) { ALOGE("[%s] Failed to setColorTransform: %s (%d)" , mName.string (), to_string(error).c_str(), static_cast <int32_t >(error)); } layerCompositionState.colorTransform = getColorTransform(); error = hwcLayer->setSurfaceDamage(surfaceDamageRegion); if (error != HWC2::Error::None) { ALOGE("[%s] Failed to set surface damage: %s (%d)" , mName.string (), to_string(error).c_str(), static_cast <int32_t >(error)); surfaceDamageRegion.dump(LOG_TAG); } layerCompositionState.surfaceDamage = surfaceDamageRegion; }
BufferLayer::setPerFrameData
BufferLayer的处理比ColorLayer多,Sideband,Cursor和其他的UI图层都属于BufferLayer,每种类型Layer处理都不同。
frameworks/native/services/surfaceflinger/BufferLayer.cpp 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 void BufferLayer::setPerFrameData (const sp<const DisplayDevice>& displayDevice, const ui::Transform& transform, const Rect& viewport, int32_t supportedPerFrameMetadata, const ui::Dataspace targetDataspace) { RETURN_IF_NO_HWC_LAYER(displayDevice); Region visible = transform.transform(visibleRegion.intersect(viewport)); const auto outputLayer = findOutputLayerForDisplay(displayDevice); LOG_FATAL_IF(!outputLayer || !outputLayer->getState().hwc); auto & hwcLayer = (*outputLayer->getState().hwc).hwcLayer; auto error = hwcLayer->setVisibleRegion(visible); outputLayer->editState().visibleRegion = visible; auto & layerCompositionState = getCompositionLayer()->editState().frontEnd; error = hwcLayer->setSurfaceDamage(surfaceDamageRegion); layerCompositionState.surfaceDamage = surfaceDamageRegion; if (layerCompositionState.sidebandStream.get()) { setCompositionType(displayDevice, Hwc2::IComposerClient::Composition::SIDEBAND); ALOGV("[%s] Requesting Sideband composition" , mName.string ()); error = hwcLayer->setSidebandStream(layerCompositionState.sidebandStream->handle()); layerCompositionState.compositionType = Hwc2::IComposerClient::Composition::SIDEBAND; return ; } if (mPotentialCursor) { ALOGV("[%s] Requesting Cursor composition" , mName.string ()); setCompositionType(displayDevice, Hwc2::IComposerClient::Composition::CURSOR); } else { ALOGV("[%s] Requesting Device composition" , mName.string ()); setCompositionType(displayDevice, Hwc2::IComposerClient::Composition::DEVICE); } ui::Dataspace dataspace = isColorSpaceAgnostic() && targetDataspace != ui::Dataspace::UNKNOWN ? targetDataspace : mCurrentDataSpace; error = hwcLayer->setDataspace(dataspace); const HdrMetadata& metadata = getDrawingHdrMetadata(); error = hwcLayer->setPerFrameMetadata(supportedPerFrameMetadata, metadata); error = hwcLayer->setColorTransform(getColorTransform()); if (error == HWC2::Error::Unsupported) { setCompositionType(displayDevice, Hwc2::IComposerClient::Composition::CLIENT); } else if (error != HWC2::Error::None) { ALOGE("[%s] Failed to setColorTransform: %s (%d)" , mName.string (), to_string(error).c_str(), static_cast <int32_t >(error)); } layerCompositionState.dataspace = mCurrentDataSpace; layerCompositionState.colorTransform = getColorTransform(); layerCompositionState.hdrMetadata = metadata; setHwcLayerBuffer(displayDevice); }
beginFrame SF.cpp 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 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); ...... display->getRenderSurface()->beginFrame(mustRecompose); if (mustRecompose) { display->editState().lastCompositionHadVisibleLayers = !empty; } }
prepareFrame准备数据 SF.cpp 1 2 3 4 5 6 7 8 9 10 11 12 void SurfaceFlinger::prepareFrame (const sp<DisplayDevice>& displayDevice) { auto display = displayDevice->getCompositionDisplay(); const auto & displayState = display->getState(); if (!displayState.isEnabled) { return ; } status_t result = display->getRenderSurface()->prepareFrame(); ALOGE_IF(result != NO_ERROR, "prepareFrame failed for %s: %d (%s)" , displayDevice->getDebugName().c_str(), result, strerror(-result)); }
frameworks/native/services/surfaceflinger/CompositionEngine/src/RenderSurface.cpp 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 status_t RenderSurface::prepareFrame () { auto & hwc = mCompositionEngine.getHwComposer(); const auto id = mDisplay.getId(); if (id) { status_t error = hwc.prepare(*id, mDisplay); if (error != NO_ERROR) { return error; } } DisplaySurface::CompositionType compositionType; const bool hasClient = hwc.hasClientComposition(id); const bool hasDevice = hwc.hasDeviceComposition(id); if (hasClient && hasDevice) { compositionType = DisplaySurface::COMPOSITION_MIXED; } else if (hasClient) { compositionType = DisplaySurface::COMPOSITION_GLES; } else if (hasDevice) { compositionType = DisplaySurface::COMPOSITION_HWC; } else { compositionType = DisplaySurface::COMPOSITION_HWC; } return mDisplaySurface->prepareFrame(compositionType); }
重要函数:
frameworks/native/services/surfaceflinger/DisplayHardware/HWComposer.cpp 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 status_t HWComposer::prepare (DisplayId displayId, const compositionengine::Output& output) { ... displayData.validateWasSkipped = false ; if (!displayData.hasClientComposition) { sp<Fence> outPresentFence; uint32_t state = UINT32_MAX; error = hwcDisplay->presentOrValidate(&numTypes, &numRequests, &outPresentFence , &state); if (error != HWC2::Error::HasChanges) { RETURN_IF_HWC_ERROR_FOR("presentOrValidate" , error, displayId, UNKNOWN_ERROR); } if (state == 1 ) { std ::unordered_map <HWC2::Layer*, sp<Fence>> releaseFences; error = hwcDisplay->getReleaseFences(&releaseFences); displayData.releaseFences = std ::move(releaseFences); displayData.lastPresentFence = outPresentFence; displayData.validateWasSkipped = true ; displayData.presentError = error; return NO_ERROR; } } else { error = hwcDisplay->validate(&numTypes, &numRequests); } ...... std ::unordered_map <HWC2::Layer*, HWC2::Composition> changedTypes; changedTypes.reserve(numTypes); error = hwcDisplay->getChangedCompositionTypes(&changedTypes); RETURN_IF_HWC_ERROR_FOR("getChangedCompositionTypes" , error, displayId, BAD_INDEX); displayData.displayRequests = static_cast <HWC2::DisplayRequest>(0 ); std ::unordered_map <HWC2::Layer*, HWC2::LayerRequest> layerRequests; layerRequests.reserve(numRequests); error = hwcDisplay->getRequests(&displayData.displayRequests, &layerRequests); RETURN_IF_HWC_ERROR_FOR("getRequests" , error, displayId, BAD_INDEX); ..... displayData.hasClientComposition = false ; displayData.hasDeviceComposition = false ; for (auto & outputLayer : output.getOutputLayersOrderedByZ()) { auto & state = outputLayer->editState(); LOG_FATAL_IF(!state.hwc.); auto hwcLayer = (*state.hwc).hwcLayer; if (auto it = changedTypes.find(hwcLayer.get()); it != changedTypes.end()) { auto newCompositionType = it->second; validateChange(static_cast <HWC2::Composition>((*state.hwc).hwcCompositionType), newCompositionType); (*state.hwc).hwcCompositionType = static_cast <Hwc2::IComposerClient::Composition>(newCompositionType); } switch ((*state.hwc).hwcCompositionType) { case Hwc2::IComposerClient::Composition::CLIENT: displayData.hasClientComposition = true ; break ; case Hwc2::IComposerClient::Composition::DEVICE: case Hwc2::IComposerClient::Composition::SOLID_COLOR: case Hwc2::IComposerClient::Composition::CURSOR: case Hwc2::IComposerClient::Composition::SIDEBAND: displayData.hasDeviceComposition = true ; break ; default : break ; } state.clearClientTarget = false ; if (auto it = layerRequests.find(hwcLayer.get()); it != layerRequests.end()) { auto request = it->second; if (request == HWC2::LayerRequest::ClearClientTarget) { state.clearClientTarget = true ; } else { LOG_DISPLAY_ERROR(displayId, ("Unknown layer request " + to_string(request)).c_str()); } } } error = hwcDisplay->acceptChanges(); RETURN_IF_HWC_ERROR_FOR("acceptChanges" , error, displayId, BAD_INDEX); return NO_ERROR; }
validate刷新:
frameworks/native/services/surfaceflinger/DisplayHardware/HWC2.cpp 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 Error Display::validate (uint32_t * outNumTypes, uint32_t * outNumRequests) { uint32_t numTypes = 0 ; uint32_t numRequests = 0 ; auto intError = mComposer.validateDisplay(mId, &numTypes, &numRequests); auto error = static_cast <Error>(intError); if (error != Error::None && error != Error::HasChanges) { return error; } *outNumTypes = numTypes; *outNumRequests = numRequests; return error; }
validateDisplay
是通过CommandWriter写Buffer的方式调用到HWC中的,但是多了一个execute
函数:
frameworks/native/services/surfaceflinger/DisplayHardware/ComposerHal.cpp 1 2 3 4 5 6 7 8 9 10 11 12 13 14 Error Composer::validateDisplay (Display display, uint32_t * outNumTypes, uint32_t * outNumRequests) { mWriter.selectDisplay(display); mWriter.validateDisplay(); Error error = execute(); if (error != Error::NONE) { return error; } mReader.hasChanges(display, outNumTypes, outNumRequests); return Error::NONE; }
doDebugFlashRegions doDebugFlashRegions只是一个debug功能,受mDebugRegion
控制(开发者选项)。
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::doDebugFlashRegions (const sp<DisplayDevice>& displayDevice, bool repaintEverything) { auto display = displayDevice->getCompositionDisplay(); const auto & displayState = display->getState(); if (CC_LIKELY(!mDebugRegion)) return ; if (displayState.isEnabled) { const Region dirtyRegion = display->getDirtyRegion(repaintEverything); if (!dirtyRegion.isEmpty()) { base::unique_fd readyFence; doComposeSurfaces(displayDevice, dirtyRegion, &readyFence); display->getRenderSurface()->queueBuffer(std ::move(readyFence)); } } postFramebuffer(displayDevice); if (mDebugRegion > 1 ) { usleep(mDebugRegion * 1000 ); } prepareFrame(displayDevice); }
doComposition合成处理 SF.cpp 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 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); }
合成方式:
Client合成 Client合成方式是相对与硬件合成来说的,其合成方式是,将各个Layer的内容用GPU渲染到暂存缓冲区中,最后将暂存缓冲区传送到显示硬件。这个暂存缓冲区,我们称为FBTarget,每个Display设备有各自的FBTarget。Client合成,之前称为GLES合成,我们也可以称之为GPU合成。Client合成,采用RenderEngine进行合成。
Device合成 就是用专门的硬件合成器进行合成HWComposer,所以硬件合成的能力就取决于硬件的实现。其合成方式是将各个Layer的数据全部传给显示硬件,并告知它从不同的缓冲区读取屏幕不同部分的数据。HWComposer是Devicehec的抽象。
getDirtyRegion获取脏区域 前面在rebuildLayerStacks
重构Layer的时候,Display的脏区域DirtyRegion已经计算出来。如果重画,则mRepaintEverything为true,此时脏区域就是整个屏幕的大小。
frameworks/native/services/surfaceflinger/CompositionEngine/src/Output.cpp 1 2 3 4 5 6 7 Region Output::getDirtyRegion (bool repaintEverything) const { Region dirty (mState.viewport) ; if (!repaintEverything) { dirty.andSelf(mState.dirtyRegion); } return dirty; }
doDisplayComposition合成 合成方式主要就两种,一种Client客户端用GPU合成;另外一种,Device端HWC硬件合成。doComposeSurfaces
主要是处理Client端合成,通过RenderEngine
用GPU来进行合成。
SF.cpp 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 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)); }
*doComposeSurfaces SF.cpp 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 bool SurfaceFlinger::doComposeSurfaces (const sp<DisplayDevice>& displayDevice, const Region& debugRegion, base::unique_fd* readyFence) { ..... 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" ); if (displayDevice->isPrimary() && supportProtectedContent) { bool needsProtected = false ; for (auto & layer : displayDevice->getVisibleLayersSortedByZ()) { if (layer->isProtected()) { needsProtected = true ; break ; } } if (needsProtected != renderEngine.isProtected()) { renderEngine.useProtectedContext(needsProtected); } if (needsProtected != display->getRenderSurface()->isProtected() && needsProtected == renderEngine.isProtected()) { display->getRenderSurface()->setProtected(needsProtected); } } ... 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 = ... 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)) ; if (!clip.isEmpty()) { switch (layer->getCompositionType(displayDevice)) { case Hwc2::IComposerClient::Composition::CURSOR: case Hwc2::IComposerClient::Composition::DEVICE: case Hwc2::IComposerClient::Composition::SIDEBAND: case Hwc2::IComposerClient::Composition::SOLID_COLOR: { LOG_ALWAYS_FATAL_IF(!displayId); const Layer::State& state (layer->getDrawingState()) ; if (layer->getClearClientTarget(displayDevice) && !firstLayer && layer->isOpaque(state) && (layer->getAlpha() == 1.0f ) && layer->getRoundedCornerState().radius == 0.0f && hasClientComposition) { renderengine::LayerSettings layerSettings; Region dummyRegion; bool prepared = layer->prepareClientLayer(renderArea, clip, dummyRegion, supportProtectedContent, layerSettings); if (prepared) { layerSettings.source.buffer.buffer = nullptr ; layerSettings.source.solidColor = half3(0.0 , 0.0 , 0.0 ); layerSettings.alpha = half(0.0 ); layerSettings.disableBlending = true ; clientCompositionLayers.push_back(layerSettings); } } break ; } 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 ; } ...... } }
postFramebuffer SF.cpp 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 void SurfaceFlinger::postFramebuffer (const sp<DisplayDevice>& displayDevice) { ATRACE_CALL(); ALOGV("postFramebuffer" ); auto display = displayDevice->getCompositionDisplay(); const auto & displayState = display->getState(); const auto displayId = display->getId(); if (displayState.isEnabled) { if (displayId) { getHwComposer().presentAndGetReleaseFences(*displayId); } display->getRenderSurface()->onPresentDisplayCompleted(); for (auto & layer : display->getOutputLayersOrderedByZ()) { sp<Fence> releaseFence = Fence::NO_FENCE; bool usedClientComposition = true ; if (layer->getState().hwc) { const auto & hwcState = *layer->getState().hwc; releaseFence = getHwComposer().getLayerReleaseFence(*displayId, hwcState.hwcLayer.get()); usedClientComposition = hwcState.hwcCompositionType == Hwc2::IComposerClient::Composition::CLIENT; } if (usedClientComposition) { releaseFence = Fence::merge("LayerRelease" , releaseFence, display->getRenderSurface()->getClientTargetAcquireFence()); } layer->getLayerFE().onLayerDisplayed(releaseFence); } if (!displayDevice->getLayersNeedingFences().isEmpty()) { sp<Fence> presentFence = displayId ? getHwComposer().getPresentFence(*displayId) : Fence::NO_FENCE; for (auto & layer : displayDevice->getLayersNeedingFences()) { layer->getCompositionLayer()->getLayerFE()->onLayerDisplayed(presentFence); } } if (displayId) { getHwComposer().clearReleaseFences(*displayId); } } }
postComposition SF.cpp 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 void SurfaceFlinger::postComposition () { ATRACE_CALL(); ALOGV("postComposition" ); nsecs_t dequeueReadyTime = systemTime(); for (auto & layer : mLayersWithQueuedFrames) { layer->releasePendingBuffer(dequeueReadyTime); } ... mDrawingState.traverseInZOrder([&](Layer* layer) { bool frameLatched = layer->onPostComposition(displayDevice->getId(), glCompositionDoneFenceTime, presentFenceTime, compositorTiming); if (frameLatched) { recordBufferingStats(layer->getName().string (), layer->getOccupancyHistory(false )); } } if (presentFenceTime->isValid()) { mScheduler->addPresentFence(presentFenceTime); } if (!hasSyncFramework) { if (displayDevice && getHwComposer().isConnected(*displayDevice->getId()) && displayDevice->isPoweredOn()) { mScheduler->enableHardwareVsync(); } } if (mAnimCompositionPending) { mAnimCompositionPending = false ; if (presentFenceTime->isValid()) { mAnimFrameTracker.setActualPresentFence( std ::move(presentFenceTime)); } else if (displayDevice && getHwComposer().isConnected(*displayDevice->getId())) { const nsecs_t presentTime = getHwComposer().getRefreshTimestamp(*displayDevice->getId()); mAnimFrameTracker.setActualPresentTime(presentTime); } mAnimFrameTracker.advanceFrame(); } ...... }
至此,REFRESH处理完成。之后等到下一个Vsync周期,开始下一次合成。
advanceFrame 其中调用advanceFrame方法,虚显用的VirtualDisplaySurface
,非虚显用的FramebufferSurface
。advanceFrame获取FBTarget的数据:
frameworks/native/services/surfaceflinger/DisplayHardware/FramebufferSurface.cpp 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 status_t FramebufferSurface::advanceFrame () { uint32_t slot = 0 ; sp<GraphicBuffer> buf; sp<Fence> acquireFence (Fence::NO_FENCE) ; Dataspace dataspace = Dataspace::UNKNOWN; status_t result = nextBuffer(slot, buf, acquireFence, dataspace); mDataSpace = dataspace; if (result != NO_ERROR) { ALOGE("error latching next FramebufferSurface buffer: %s (%d)" , strerror(-result), result); } return result; } status_t FramebufferSurface::nextBuffer (uint32_t & outSlot, sp<GraphicBuffer>& outBuffer, sp<Fence>& outFence, Dataspace& outDataspace) { Mutex::Autolock lock (mMutex) ; BufferItem item; status_t err = acquireBufferLocked(&item, 0 ); ... if (mCurrentBufferSlot != BufferQueue::INVALID_BUFFER_SLOT && item.mSlot != mCurrentBufferSlot) { mHasPendingRelease = true ; mPreviousBufferSlot = mCurrentBufferSlot; mPreviousBuffer = mCurrentBuffer; } mCurrentBufferSlot = item.mSlot; mCurrentBuffer = mSlots[mCurrentBufferSlot].mGraphicBuffer; mCurrentFence = item.mFence; outFence = item.mFence; mHwcBufferCache.getHwcBuffer(mCurrentBufferSlot, mCurrentBuffer, &outSlot, &outBuffer); outDataspace = static_cast <Dataspace>(item.mDataSpace); status_t result = mHwc.setClientTarget(mDisplayId, outSlot, outFence, outBuffer, outDataspace); if (result != NO_ERROR) { ALOGE("error posting framebuffer: %d" , result); return result; } return NO_ERROR; }
setClientTarget函数:
frameworks/native/services/surfaceflinger/DisplayHardware/HWComposer.cpp 1 2 3 4 5 6 7 8 9 status_t HWComposer::setClientTarget (DisplayId displayId, uint32_t slot, const sp<Fence>& acquireFence, const sp<GraphicBuffer>& target, ui::Dataspace dataspace) { auto & hwcDisplay = mDisplayData[displayId].hwcDisplay; auto error = hwcDisplay->setClientTarget(slot, target, acquireFence, dataspace); return NO_ERROR; }
HWComposer头文件,hwcDisplay在HWC2命名空间内。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 struct DisplayData { bool isVirtual = false ; bool hasClientComposition = false ; bool hasDeviceComposition = false ; HWC2::Display* hwcDisplay = nullptr ; HWC2::DisplayRequest displayRequests; sp<Fence> lastPresentFence = Fence::NO_FENCE; std ::unordered_map <HWC2::Layer*, sp<Fence>> releaseFences; buffer_handle_t outbufHandle = nullptr ; sp<Fence> outbufAcquireFence = Fence::NO_FENCE; mutable std ::unordered_map <int32_t , std ::shared_ptr <const HWC2::Display::Config>> configMap; bool validateWasSkipped; HWC2::Error presentError; bool vsyncTraceToggle = false ; std ::mutex vsyncEnabledLock; HWC2::Vsync vsyncEnabled GUARDED_BY (vsyncEnabledLock) = HWC2::Vsync::Disable; mutable std ::mutex lastHwVsyncLock; nsecs_t lastHwVsync GUARDED_BY (lastHwVsyncLock) = 0 ; };
GPU合成模块概述 硬件HWC合成是由Vendor实现。而各个厂商在这部分的实现不同。
GPU合成(Client)是Android原生自带的,本质是采用GPU进程合成,SurfaceFlinger模块封装了RenderEngine进行具体的实现。
看一下这个模块的文件目录:
frameworks/native/libs/renderengine 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 ├── Android.bp ├── Description.cpp ├── gl │ ├── GLESRenderEngine.cpp │ ├── GLESRenderEngine.h │ ├── GLExtensions.cpp │ ├── GLExtensions.h │ ├── GLFramebuffer.cpp │ ├── GLFramebuffer.h │ ├── GLImage.cpp │ ├── GLImage.h │ ├── ProgramCache.cpp │ ├── ProgramCache.h │ ├── Program.cpp │ └── Program.h ├── include │ └── renderengine │ ├── DisplaySettings.h │ ├── Framebuffer.h │ ├── Image.h │ ├── LayerSettings.h │ ├── Mesh.h │ ├── mock │ │ ├── Framebuffer.h │ │ ├── Image.h │ │ └── RenderEngine.h │ ├── private │ │ └── Description.h │ ├── RenderEngine.h │ └── Texture.h ├── Mesh.cpp ├── mock │ ├── Framebuffer.cpp │ ├── Image.cpp │ └── RenderEngine.cpp ├── OWNERS ├── RenderEngine.cpp ├── TEST_MAPPING ├── tests │ ├── Android.bp │ └── RenderEngineTest.cpp └── Texture.cpp
创建RenderEngine 在SF.cpp初始化函数init中:
1 2 3 4 5 6 7 8 9 void SurfaceFlinger::init () { ... mCompositionEngine->setRenderEngine( renderengine::RenderEngine::create(static_cast <int32_t >(defaultCompositionPixelFormat), renderEngineFeature, maxFrameBufferAcquiredBuffers)); ... }
create调用RenderEngine.cpp中的对应函数:(在Q版本该模块已经独立出来,该模块是对GPU渲染的封装)
frameworks/native/libs/renderengine/RenderEngine.cpp 1 2 3 4 5 6 7 8 9 10 11 std ::unique_ptr <impl::RenderEngine> RenderEngine::create (int hwcFormat, uint32_t featureFlags, uint32_t imageCacheSize) { char prop[PROPERTY_VALUE_MAX]; property_get(PROPERTY_DEBUG_RENDERENGINE_BACKEND, prop, "gles" ); if (strcmp (prop, "gles" ) == 0 ) { ALOGD("RenderEngine GLES Backend" ); return renderengine::gl::GLESRenderEngine::create(hwcFormat, featureFlags, imageCacheSize); } ALOGE("UNKNOWN BackendType: %s, create GLES RenderEngine." , prop); return renderengine::gl::GLESRenderEngine::create(hwcFormat, featureFlags, imageCacheSize); }
frameworks/native/libs/renderengine/gl/GLESRenderEngine.cpp 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 std ::unique_ptr <GLESRenderEngine> GLESRenderEngine::create (int hwcFormat, uint32_t featureFlags, uint32_t imageCacheSize) { EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY); if (!eglInitialize(display, nullptr , nullptr )) { LOG_ALWAYS_FATAL("failed to initialize EGL" ); } EGLConfig config = EGL_NO_CONFIG; if (!extensions.hasNoConfigContext()) { config = chooseEglConfig(display, hwcFormat, true ); } ... if ((featureFlags & RenderEngine::ENABLE_PROTECTED_CONTEXT) && extensions.hasProtectedContent()) { protectedContext = createEglContext(display, config, nullptr , useContextPriority, Protection::PROTECTED); ALOGE_IF(protectedContext == EGL_NO_CONTEXT, "Can't create protected context" ); } EGLContext ctxt = createEglContext(display, config, protectedContext, useContextPriority, Protection::UNPROTECTED); ... EGLSurface dummy = EGL_NO_SURFACE; if (!extensions.hasSurfacelessContext()) { dummy = createDummyEglPbufferSurface(display, config, hwcFormat, Protection::UNPROTECTED); } ... GlesVersion version = parseGlesVersion(extensions.getVersion()); std ::unique_ptr <GLESRenderEngine> engine; switch (version) { case GLES_VERSION_1_0: case GLES_VERSION_1_1: LOG_ALWAYS_FATAL("SurfaceFlinger requires OpenGL ES 2.0 minimum to run." ); break ; case GLES_VERSION_2_0: case GLES_VERSION_3_0: engine = std ::make_unique<GLESRenderEngine>(featureFlags, display, config, ctxt, dummy, protectedContext, protectedDummy, imageCacheSize); break ; } ...... }
创建Surface FBTarget RenderEngine创建时始化的EGLDisplaym,EGLConfig,EGLContext等,都是所有Display共用的。
而Surface每个Display的是自己的,在创建DisplayDevice时,创建对应的Surface。
从BufferQueue中dequeue Buffer进行渲染,swapBuffer时,也queue到Bufferqueu中。这里的ANativeWindow,本质就是FBTarget。
创建Texture纹理 在BufferLayer创建的构造函数中创建Texture:
BufferLayer.cpp 1 2 3 4 5 6 7 8 9 BufferLayer::BufferLayer(const LayerCreationArgs& args) : Layer(args), mTextureName(args.flinger->getNewTexture()), mCompositionLayer{mFlinger->getCompositionEngine().createLayer( compositionengine::LayerCreationArgs{this })} { ...... }
SF.cpp 1 2 3 4 5 6 7 8 uint32_t SurfaceFlinger::getNewTexture () { ...... uint32_t name = 0 ; postMessageSync(new LambdaMessage([&]() { getRenderEngine().genTextures(1 , &name); })); return name; }
调用genTextures:
frameworks/native/libs/renderengine/gl/GLESRenderEngine.cpp 1 2 3 void GLESRenderEngine::genTextures (size_t count, uint32_t * names) { glGenTextures(count, names); }
绑定Texture纹理 在BufferLayerConsumer::updateTexImage
函数中调用bindTextureImageLocked
绑定新的buffer到GL Texture纹理。
而该函数是在SurfaceFlinger调用latchBuffer从BufferQueue申请获取渲染好的buffer的时候会调用到。
Layer合成 在Q版本之前是调用的onDraw
函数,而在Q上函数名变成prepareClientLayer
。
1 2 3 4 5 6 7 8 9 10 bool BufferLayer::prepareClientLayer (const RenderArea& renderArea, const Region& clip, bool useIdentityTransform, Region& clearRegion, const bool supportProtectedContent, renderengine::LayerSettings& layer) { ... bool blackOutLayer = (isProtected() && !supportProtectedContent) || (isSecure() && !renderArea.isSecure()); ... }
在SurfaceFlinger::doComposeSurfaces
函数中,调用完prepareClientLayer后,末尾最后调用renderEngine.drawLayers
函数。
最终调用到GPU合成模块frameworks/native/libs/renderengine/gl/GLESRenderEngine.cpp
的GLESRenderEngine::drawLayers
函数。
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 status_t GLESRenderEngine::drawLayers (const DisplaySettings& display, const std ::vector <LayerSettings>& layers, ANativeWindowBuffer* const buffer, const bool useFramebufferCache, base::unique_fd&& bufferFence, base::unique_fd* drawFence) { ... renderengine::Mesh::VertexArray<vec2> texCoords (mesh.getTexCoordArray<vec2>()) ; texCoords[0 ] = vec2(0.0 , 0.0 ); texCoords[1 ] = vec2(0.0 , 1.0 ); texCoords[2 ] = vec2(1.0 , 1.0 ); texCoords[3 ] = vec2(1.0 , 0.0 ); ... setupLayerBlending(usePremultipliedAlpha, isOpaque, disableTexture, color, layer.geometry.roundedCornersRadius); } ... if (layer.geometry.roundedCornersRadius > 0.0 && color.a >= 1.0f && isOpaque) { handleRoundedCorners(display, layer, mesh); } else { drawMesh(mesh); }
参考文章