上篇分析到SurfaceFlinger.signalRefresh()里会调用到SurfaceFlinger里的handleMessageRefresh(), 现在这里面开始涉及到图层合成了,这里面涉及的流程。
handleMessageRefresh()
void SurfaceFlinger::handleMessageRefresh() {
mRefreshPending = false;
nsecs_t refreshStartTime = systemTime(SYSTEM_TIME_MONOTONIC);
//合成图像前的准备工作,设置各种状态
preComposition(refreshStartTime);
//重建可见区域的所有图层
rebuildLayerStacks();
//启动硬件图像合成
setUpHWComposer();
//调试信息
doDebugFlashRegions();
doTracing("handleRefresh");
logLayerStats();
//合成处理
doComposition();
//更新状态与下次合成时间
postComposition(refreshStartTime);
}
上面重点的就是两块,一个setupHWComposer() 启动硬件图像合成, 另一个是doComposition()合成处理。
setUpHWComposer()
void SurfaceFlinger::setUpHWComposer() {
...
if (CC_UNLIKELY(mGeometryInvalid)) {
mGeometryInvalid = false;
//遍历所有的显示设备
for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
sp<const DisplayDevice> displayDevice(mDisplays[dpy]);
const auto hwcId = displayDevice->getHwcDisplayId();
if (hwcId >= 0) {
const Vector<sp<Layer>>& currentLayers(
displayDevice->getVisibleLayersSortedByZ());
//遍历当前显示设备的所有图层
for (size_t i = 0; i < currentLayers.size(); i++) {
const auto& layer = currentLayers[i];
if (!layer->hasHwcLayer(hwcId)) {
//如果没有硬件图层,则创建一个硬件图层
if (!layer->createHwcLayer(getBE().mHwc.get(), hwcId)) {
layer->forceClientComposition(hwcId);
continue;
}
}
//设置图层大小,坐标
layer->setGeometry(displayDevice, i);
if (mDebugDisableHWC || mDebugRegion) {
layer->forceClientComposition(hwcId);
}
}
}
}
// Set the per-frame data
//遍历所有显示屏
for (size_t displayId = 0; displayId < mDisplays.size(); ++displayId) {
auto& displayDevice = mDisplays[displayId];
const auto hwcId = displayDevice->getHwcDisplayId();
if (hwcId < 0) {
continue;
}
//更新颜色变化
if (mDrawingState.colorMatrixChanged) {
displayDevice->setColorTransform(mDrawingState.colorMatrix);
status_t result = getBE().mHwc->setColorTransform(hwcId, mDrawingState.colorMatrix);
ALOGE_IF(result != NO_ERROR, "Failed to set color transform on "
"display %zd: %d", displayId, result);
}
//遍历当前显示屏的所有图层
for (auto& layer : displayDevice->getVisibleLayersSortedByZ()) {
//设置数据存储格式
if (layer->isHdrY410()) {
layer->forceClientComposition(hwcId);
} else if (