android应用中申请的Surface在SurfaceFlinger服务中都有对应有图层Layer与之对应。将这些图层合并且输出到显示外设是SurfaceFlinger的工作重点。本文来分析下合成的过程。合成工作在接收到VSync消息(MessageQueue::REFRESH)后开始。
void SurfaceFlinger::onMessageReceived(int32_t what) {
ATRACE_CALL();
switch (what) {
......
case MessageQueue::REFRESH: {
handleMessageRefresh();
break;
}
}
}
- 调用消息处理函数handleMessageRefresh来进行显示刷新工作,图像合成与输出的核心就在这个处理函数里
void SurfaceFlinger::handleMessageRefresh() {
ATRACE_CALL();
......
preComposition();
rebuildLayerStacks();
setUpHWComposer();
doDebugFlashRegions();
doComposition();
postComposition();
......
previousExpectedPresent = mPrimaryDispSync.computeNextRefresh(0);
}
- preComposition、rebuildLayerStacks、setUpHWComposer图层合成准备工作
- setUpHWComposer会去设置图层属性compositionType(HWC_OVERLAY、HWC_FRAMEBUFFER等),这个属性将决定该图层在合成采用什么方式(硬件合成或软件合成)
- doComposition图层合成的主要工作,合成后的图像也在这里进行输出显示,重点关注
- postComposition主要做一些收尾工作,不是关注的重点
void SurfaceFlinger::doComposition() {
ATRACE_CALL();
const bool repaintEverything = android_atomic_and(0, &mRepaintEverything);
for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
const sp<DisplayDevice>& hw(mDisplays[dpy]);
if (hw->isDisplayOn()) {
// transform the dirty region into this screen's coordinate space
const Region dirtyRegion(hw->getDirtyRegion(repaintEverything));
// repaint the framebuffer (if needed)
doDisplayComposition(hw, dirtyRegion);
hw->dirtyRegion.clear();
hw->flip(hw->swapRegion);
hw->swapRegion.clear();
}
// inform the h/w that we're done compositing
hw->compositionComplete();
}
postFramebuffer();
}
- doDisplayComposition函数对第个DisplayDevice的图层进行合成,在没有硬件合成模块的情况下,还进行输出显示
- postFramebuffer在有硬件合成模块时,提交图层到HWComposer
void SurfaceFlinger::doDisplayComposition(const sp<const DisplayDevice>& hw,
const Region& inDirtyRegion)
{
......
if (CC_LIKELY(!mDaltonize && !mHasColorMatrix)) {
if (!doComposeSurfaces(hw, dirtyRegion)) return;
} else {
RenderEngine& engine(getRenderEngine());
mat4 colorMatrix = mColorMatrix;
if (mDaltonize) {
colorMatrix = colorMatrix * mDaltonizer();
}
mat4 oldMatrix = engine.setupColorTransform(colorMatrix);
doComposeSurfaces(hw, dirtyRegion);
engine.setupColorTransform(oldMatrix);
}
// update the swap region and clear the dirty region
hw->swapRegion.orSelf(dirtyRegion);
// swap buffers (presentation)
hw->swapBuffers(getHwComposer());
}
- 调用doComposeSurfaces函数进行图层的合成,图像合成的真正执行者
- 调用hw->swapBuffers将软件合成的结果提交到图像缓冲区,待消费者去处理(后文分析)
bool SurfaceFlinger::doComposeSurfaces(const sp<const DisplayDevice>& hw, const Region& dirty)
{
......
/*
* and then, render the layers targeted at the framebuffer
*/
const Vector< sp<Layer> >& layers(hw->getVisibleLayersSortedByZ());
const size_t count = layers.size();
const Transform& tr = hw->getTransform();
if (cur != end) {
// we're using h/w composer
for (size_t i=0 ; i<count && cur!=end ; ++i, ++cur) {
const sp<Layer>& layer(layers[i]);
const Region clip(dirty.intersect(tr.transform(layer->visibleRegion)));
if (!clip.isEmpty()) {
switch (cur->getCompositionType()) {
case HWC_CURSOR_OVERLAY:
case HWC_OVERLAY: {
const Layer::State& state(layer->getDrawingState());
if ((cur->getHints() & HWC_HINT_CLEAR_FB)
&& i
&& layer->isOpaque(state) && (state.alpha == 0xFF)
&& hasGlesComposition) {
// never clear the very first layer since we're
// guaranteed the FB is already cleared
layer->clearWithOpenGL(hw, clip);
}
break;
}
case HWC_FRAMEBUFFER: {
layer->draw(hw, clip);
break;
}
case HWC_FRAMEBUFFER_TARGET: {
// this should not happen as the iterator shouldn't
// let us get there.
ALOGW("HWC_FRAMEBUFFER_TARGET found in hwc list (index=%zu)", i);
break;
}
}
}
layer->setAcquireFence(hw, *cur);
}
} else {
// we're not using h/w composer
for (size_t i=0 ; i<count ; ++i) {
const sp<Layer>& layer(layers[i]);
const Region clip(dirty.intersect(
tr.transform(layer->visibleRegion)));
if (!clip.isEmpty()) {
layer->draw(hw, clip);
}
}
}
// disable scissor at the end of the frame
engine.disableScissor();
return true;
}
- 当图层的compositionType值为HWC_CURSOR_OVERLAY或HWC_OVERLAY,采用硬件合成。既将图层提交给HWComposer去处理
- 当图层的compositionType值为HWC_FRAMEBUFFER,采用软件gl合成。既调用Layer的draw方法绘制
至此,图层合成流程大体走完,图像输出的过程将在下文分析。