当有事务的更新或者有Buffer的更新便会触发后面刷新的流程。
onMessageRefresh
文件:frameworks/native/services/surfaceflinger/Surfaceflinger.cpp
void SurfaceFlinger::onMessageRefresh() {
ATRACE_CALL();
mRefreshPending = false;
compositionengine::CompositionRefreshArgs refreshArgs;
const auto& displays = ON_MAIN_THREAD(mDisplays);
// display 的数量,把当前displaydevice 记录下来
refreshArgs.outputs.reserve(displays.size());
for (const auto& [_, display] : displays) {
refreshArgs.outputs.push_back(display->getCompositionDisplay());
}
// 把当前layer记录下来
mDrawingState.traverseInZOrder([&refreshArgs](Layer* layer) {
if (auto layerFE = layer->getCompositionEngineLayerFE())
refreshArgs.layers.push_back(layerFE);
});
// 把当前有queueframe的layer记录下来, 有Buffer的layer
refreshArgs.layersWithQueuedFrames.reserve(mLayersWithQueuedFrames.size());
for (sp<Layer> layer : mLayersWithQueuedFrames) {
if (auto layerFE = layer->getCompositionEngineLayerFE())
refreshArgs.layersWithQueuedFrames.push_back(layerFE);
}
...
// 主要逻辑在present里面
mCompositionEngine->present(refreshArgs);
}
刷新的主要逻辑在present里面。
文件:frameworks/native/services/surfaceflinger/CompositionEngine/src/CompositionEngine.cpp
void CompositionEngine::present(CompositionRefreshArgs& args) {
ATRACE_CALL();
ALOGV(__FUNCTION__);
// 根据是否还有layer没有消费掉,判断再调起一次刷新
preComposition(args);
{
// latchedLayers is used to track the set of front-end layer state that
// has been latched across all outputs for the prepare step, and is not
// needed for anything else.
LayerFESet latchedLayers;
for (const auto& output : args.outputs) {
// 计算各个区域大小以及创建hwc layer
output->prepare(args, latchedLayers);
}
}
// 更新layerCompositionState
updateLayerStateFromFE(args);
for (const auto& output : args.outputs) {
// 逻辑在output.cpp的present里面
output->present(args);
}
}
prepare主要的作用是计算各个区域大小以及创建hwc layer,主要逻辑在ensureOutputLayerIfVisible 里面。
ensureOutputLayerIfVisible
文件:frameworks/native/services/surfaceflinger/CompositionEngine/src/Output.cpp
void Output::ensureOutputLayerIfVisible(sp<compositionengine::LayerFE>& layerFE,
compositionengine::Output::CoverageState& coverage) {
...
/*
* opaqueRegion: area of a surface that is fully opaque.
*/
Region opaqueRegion;
/*
* visibleRegion: area of a surface that is visible on screen and not fully
* transparent. This is essentially the layer's footprint minus the opaque
* regions above it. Areas covered by a translucent surface are considered
* visible.
*/
Region visibleRegion;
/*
* coveredRegion: area of a surface that is covered by all visible regions
* above it (which includes the translucent areas).
*/
Region coveredRegion;
/*
* transparentRegion: area of a surface that is hinted to be completely
* transparent. This is only used to tell when the layer has no visible non-
* transparent regions and can be removed from the layer list. It does not
* affect the visibleRegion of this layer or any layers beneath it. The hint
* may not be correct if apps don't respect the SurfaceView restrictions
* (which, sadly, some don't).
*/
Region transparentRegion;
/*
* shadowRegion: Region cast by the layer's shadow.
*/
Region shadowRegion;
....
// 创建OutputLayer
auto result = ensureOutputLayer(prevOutputLayerIndex, layerFE);
...
}
各个layer区域的计算结果可以通过adb shell dumpsys SurfaceFlinger看到,通过区域的计算把区域为空的layer过滤掉
ensureOutputLayer将创建Output layer,这些layer最后是要显示到屏幕上的。
文件: frameworks/native/services/surfaceflinger/CompositionEngine/include/compositionengine/impl/Output.h
OutputLayer* ensureOutputLayer(std::optional<size_t> prevIndex,
const sp<LayerFE>& layerFE) {
// 判断当前的outputlayer 集合里面有没有当前的layer,如果没有则新创建一个
auto outputLayer = (prevIndex && *prevIndex <= mCurrentOutputLayersOrderedByZ.size())
? std::move(mCurrentOutputLayersOrderedByZ[*prevIndex])
: BaseOutput::createOutputLayer(layerFE);
auto result = outputLayer.get();
mPendingOutputLayersOrderedByZ.emplace_back(std::move(outputLayer));
return result;
}
文件:frameworks/native/services/surfaceflinger/CompositionEngine/src/Display.cpp
std::unique_ptr<compositionengine::OutputLayer> Display::createOutputLayer(
const sp<compositionengine::LayerFE>& layerFE) const {
auto result = impl::createOutputLayer(*this, layerFE);
if (result && mId) {
auto& hwc = getCompositionEngine().getHwComposer();
auto displayId = *mId;
// 创建hwclayer
auto hwcLayer = std::shared_ptr<HWC2::Layer>(hwc.createLayer(displayId),
[&hwc, displayId](HWC2::Layer* layer) {
hwc.destroyLayer(displayId, layer);
});
ALOGE_IF(!hwcLayer, "Failed to create a HWC layer for a HWC supported display %s",
getName().c_str());
// 更新state.hwc
result->setHwcLayer(std::move(hwcLayer));
}
return result;
}
可以看到prepare主要的作用是遍历layer计算各个区域大小,过滤掉空区域的layer以及创建hwc layer。
updateLayerStateFromFE
更新OutputlayerState的参数,即layer的状态信息。
文件:frameworks/native/services/surfaceflinger/Layer.cpp
void Layer::prepareCompositionState(compositionengine::LayerFE::StateSubset subset) {
using StateSubset = compositionengine::LayerFE::StateSubset;
switch (subset) {
case StateSubset::BasicGeometry:
prepareBasicGeometryCompositionState();
break;
case StateSubset::GeometryAndContent:
prepareBasicGeometryCompositionState();
prepareGeometryCompositionState();
preparePerFrameCompositionState();
break;
case StateSubset::Content:
preparePerFrameCompositionState();
break;
case StateSubset::Cursor:
prepareCursorCompositionState();
break;
}
}
这部分也可以通过adb shell dumpsys SurfaceFlinger看出来有哪些属性。
接下来看下present的逻辑。
文件:frameworks/native/services/surfaceflinger/CompositionEngine/src/Output.cpp
void Output::present(const compositionengine::CompositionRefreshArgs& refreshArgs) {
ATRACE_CALL();
ALOGV(__FUNCTION__);
//设置 Display的dataspace, colormode
updateColorProfile(refreshArgs);
// 设置outputlayer的dispFrame和sourceCrop等以及将outputlayer属性设给hwc
updateAndWriteCompositionState(refreshArgs);
// 设置display的颜色矩阵,对全屏有效
setColorTransform(refreshArgs);
// FrameBuffer里面没做啥操作
beginFrame();
// 选择合成策略,判断是device还是GPU合成,如果是device合成,直接present,如果要走GPU合成则需要validate
prepareFrame();
// 一般不走
devOptRepaintFlash(refreshArgs);
// GPU合成主要逻辑在此,device合成没做啥
finishFrame(refreshArgs);
// device合成设置release fence,GPU合成需要present给hwc
postFramebuffer();
}
先关注device合成,涉及GPU部分单独分析。
updateAndWriteCompositionState
文件:frameworks/native/services/surfaceflinger/CompositionEngine/src/Output.cpp
void Output::updateAndWriteCompositionState(
const compositionengine::CompositionRefreshArgs& refreshArgs) {
ATRACE_CALL();
ALOGV(__FUNCTION__);
if (!getState().isEnabled) {
return;
}
mLayerRequestingBackgroundBlur = findLayerRequestingBackgroundComposition();
// 如果是有背景模糊的layer则强制使用GPU合成
bool forceClientComposition = mLayerRequestingBackgroundBlur != nullptr;
for (auto* layer : getOutputLayersOrderedByZ()) {
// 遍历outputlayer,计算DisplayFrame, SourceCrop 等参数
layer->updateCompositionState(refreshArgs.updatingGeometryThisFrame,
refreshArgs.devOptForceClientComposition ||
forceClientComposition,
refreshArgs.internalDisplayRotationFlags);
if (mLayerRequestingBackgroundBlur == layer) {
forceClientComposition = false;
}
// 将layer属性设给hwc
layer->writeStateToHWC(refreshArgs.updatingGeometryThisFrame);
}
这部分参数也可以通过adb shell dumpsys SurfaceFlinger看到
setColorTransform
文件:frameworks/native/services/surfaceflinger/CompositionEngine/src/Display.cpp
void Display::setColorTransform(const compositionengine::CompositionRefreshArgs& args) {
Output::setColorTransform(args);
if (!mId || CC_LIKELY(!args.colorTransformMatrix)) {
return;
}
auto& hwc = getCompositionEngine().getHwComposer();
// 将上层设的colorTransformMatrix 设给hwc,这个效果作用于全屏,比如护眼模式下,改变的就是这个colorTransformMatrix
status_t result = hwc.setColorTransform(*mId, *args.colorTransformMatrix);
ALOGE_IF(result != NO_ERROR, "Failed to set color transform on display \"%s\": %d",
mId ? to_string(*mId).c_str() : "", result);
}
setColorTransform 设置颜色矩阵给屏幕,作用于屏幕显示,所有的layer都受影响,护眼效果就是改变的这个值。
prepareFrame
文件:frameworks/native/services/surfaceflinger/CompositionEngine/src/Output.cpp
void Output::prepareFrame() {
ATRACE_CALL();
ALOGV(__FUNCTION__);
const auto& outputState = getState();
if (!outputState.isEnabled) {
return;
}
// 选择合成类型,如果是device合成,则跳过validate,直接present送显
chooseCompositionStrategy();
// 把合成类型送到frameBufferSurface,没啥逻辑
mRenderSurface->prepareFrame(outputState.usesClientComposition,
outputState.usesDeviceComposition);
}
void Output::chooseCompositionStrategy() {
// The base output implementation can only do client composition
// 默认使用GPU合成,针对没有hwc的设备
auto& outputState = editState();
outputState.usesClientComposition = true;
outputState.usesDeviceComposition = false;
outputState.reusedClientComposition = false;
}
文件:frameworks/native/services/surfaceflinger/CompositionEngine/src/Display.cpp
void Display::chooseCompositionStrategy() {
ATRACE_CALL();
ALOGV(__FUNCTION__);
// Default to the base settings -- client composition only.
Output::chooseCompositionStrategy();
// If we don't have a HWC display, then we are done
if (!mId) {
return;
}
// Get any composition changes requested by the HWC device, and apply them.
std::optional<android::HWComposer::DeviceRequestedChanges> changes;
auto& hwc = getCompositionEngine().getHwComposer();
// 从HWC device获得合成类型的改变,这个根据hwc能力来选择device还是GPU合成
if (status_t result = hwc.getDeviceCompositionChanges(*mId, anyLayersRequireClientComposition(),
&changes);
result != NO_ERROR) {
ALOGE("chooseCompositionStrategy failed for %s: %d (%s)", getName().c_str(), result,
strerror(-result));
return;
}
//如果有变化则设置给对应的layer
if (changes) {
applyChangedTypesToLayers(changes->changedTypes);
applyDisplayRequests(changes->displayRequests);
applyLayerRequestsToLayers(changes->layerRequests);
applyClientTargetRequests(changes->clientTargetProperty);
}
// Determine what type of composition we are doing from the final state
// 决定最后的合成类型
auto& state = editState();
state.usesClientComposition = anyLayersRequireClientComposition();
state.usesDeviceComposition = !allLayersRequireClientComposition();
}
文件:frameworks/native/services/surfaceflinger/DisplayHardware/HWComposer.cpp
status_t HWComposer::getDeviceCompositionChanges(
DisplayId displayId, bool frameUsesClientComposition,
std::optional<android::HWComposer::DeviceRequestedChanges>* outChanges) {
ATRACE_CALL();
...
if (!frameUsesClientComposition) {
sp<Fence> outPresentFence;
uint32_t state = UINT32_MAX;
// 如果所有的layer都能走device合成,则在hwc里面直接present,若有不支持device合成的情况,则走GPU合成,会走validate逻辑
error = hwcDisplay->presentOrValidate(&numTypes, &numRequests, &outPresentFence , &state);
if (!hasChangesError(error)) {
RETURN_IF_HWC_ERROR_FOR("presentOrValidate", error, displayId, UNKNOWN_ERROR);
}
if (state == 1) { //Present Succeeded.
// present成功,数据直接提交给了hwc
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;
}
// Present failed but Validate ran.
} else {
// 若有GPU合成,则走validate逻辑
error = hwcDisplay->validate(&numTypes, &numRequests);
}
// 接收hwc过来的change,对于device合成不走,GPU合成走的逻辑
...
prepareFrame 的作用是根据hwc的能力选择合成方式,如果是device合成则直接走hwc present上屏,如果是GPU合成则走hwc validate,然后根据hwc过来的变化改变layer的合成方式。
finishFrame
文件: frameworks/native/services/surfaceflinger/CompositionEngine/src/Output.cpp
void Output::finishFrame(const compositionengine::CompositionRefreshArgs& refreshArgs) {
ATRACE_CALL();
ALOGV(__FUNCTION__);
if (!getState().isEnabled) {
return;
}
// Repaint the framebuffer (if needed), getting the optional fence for when
// the composition completes.
// 主要针对GPU合成的逻辑
auto optReadyFence = composeSurfaces(Region::INVALID_REGION, refreshArgs);
// device合成直接return
if (!optReadyFence) {
return;
}
// swap buffers (presentation)
mRenderSurface->queueBuffer(std::move(*optReadyFence));
}
composeSurfaces主要的逻辑是针对GPU合成,对于device合成,直接return了。
postFramebuffer
文件:frameworks/native/services/surfaceflinger/CompositionEngine/src/Output.cpp
void Output::postFramebuffer() {
...
// device合成获取release fence
auto frame = presentAndGetFrameFences();
mRenderSurface->onPresentDisplayCompleted();
for (auto* layer : getOutputLayersOrderedByZ()) {
// The layer buffer from the previous frame (if any) is released
// by HWC only when the release fence from this frame (if any) is
// signaled. Always get the release fence from HWC first.
sp<Fence> releaseFence = Fence::NO_FENCE;
if (auto hwcLayer = layer->getHwcLayer()) {
if (auto f = frame.layerFences.find(hwcLayer); f != frame.layerFences.end()) {
releaseFence = f->second;
}
}
if (outputState.usesClientComposition) {
releaseFence =
Fence::merge("LayerRelease", releaseFence, frame.clientTargetAcquireFence);
}
// 将releasefence放到mslot里面,这个是dequeueBuffer等的fence
layer->getLayerFE().onLayerDisplayed(releaseFence);
}
...
}
文件:frameworks/native/services/surfaceflinger/DisplayHardware/HWComposer.cpp
status_t HWComposer::presentAndGetReleaseFences(DisplayId displayId) {
ATRACE_CALL();
RETURN_IF_INVALID_DISPLAY(displayId, BAD_INDEX);
auto& displayData = mDisplayData[displayId];
auto& hwcDisplay = displayData.hwcDisplay;
// device合成直接return
if (displayData.validateWasSkipped) {
// explicitly flush all pending commands
auto error = static_cast<hal::Error>(mComposer->executeCommands());
RETURN_IF_HWC_ERROR_FOR("executeCommands", error, displayId, UNKNOWN_ERROR);
RETURN_IF_HWC_ERROR_FOR("present", displayData.presentError, displayId, UNKNOWN_ERROR);
return NO_ERROR;
}
//GPU 合成走present
auto error = hwcDisplay->present(&displayData.lastPresentFence);
RETURN_IF_HWC_ERROR_FOR("present", error, displayId, UNKNOWN_ERROR);
std::unordered_map<HWC2::Layer*, sp<Fence>> releaseFences;
error = hwcDisplay->getReleaseFences(&releaseFences);
RETURN_IF_HWC_ERROR_FOR("getReleaseFences", error, displayId, UNKNOWN_ERROR);
displayData.releaseFences = std::move(releaseFences);
return NO_ERROR;
}
postComposition
文件: frameworks/native/services/surfaceflinger/SurfaceFlinger.cpp
void SurfaceFlinger::postComposition()
{
ATRACE_CALL();
ALOGV("postComposition");
nsecs_t dequeueReadyTime = systemTime();
// release Bufferqueuelayer
for (auto& layer : mLayersWithQueuedFrames) {
layer->releasePendingBuffer(dequeueReadyTime);
}
...
// mPreviousPresentFences[0]是这一帧的present fence,mPreviousPresentFences[1]是上一帧的present fence
mPreviousPresentFences[1] = mPreviousPresentFences[0];
mPreviousPresentFences[0] =
display ? getHwComposer().getPresentFence(*display->getId()) : Fence::NO_FENCE;
...
// 这个有关vsync,是校验presentfence释放的时间戳
if (display && display->isPrimary() && display->getPowerMode() == hal::PowerMode::ON &&
presentFenceTime->isValid()) {
mScheduler->addPresentFence(presentFenceTime);
}
....
}
postComposition的作用就是释放上一帧显示的Buffer,然后重新设置当前帧的present fence,以及根据presentfence时间戳判断是否重新打开HW Vsync和软件Vsync进行校准。至此,SurfaceFlinger刷新的主体函数分析完了,可以看出来,如果是走device合成,SurfaceFlinger是直接把layer信息提交给hwc,由hwc去合成,涉及到GPU部分,下篇进行分析。