- 公众号:阿豪讲Framework
- 系统教程:ahaoframework.tech/
- 进 Android Framework 技术交流群加微信 zzh0838,备注进群
本文超过了掘金的支持的最大字符数,原文请查看 ahaoframework.tech/
1. 引子
1.1 图层的概念
(图片来自 www.jianshu.com/p/b0ef7c044…
在很多的图形相关的软件中都有图层的概念,那什么是图层呢?
简单的说,可以将每个图层理解为一张透明的纸,将图像的各部分绘制在不同的透明纸(图层)上。透过这层纸,可以看到纸后面的东西,而且每层纸都是独立的,无论在这层纸上如何涂画,都不会影响到其他图层中的图像。也就是说,每个图层可以独立编辑或修改,最后将透明纸叠加起来,从上向下俯瞰,即可得到并实时改变最终的合成效果。
1.2 什么是图层合成
以 Android 原生版本的 Launcher 为例,这个场景下有四个图层,状态栏、导航栏由 SystemUI 绘制,壁纸由壁纸服务提供,图标由 Launcher 应用绘制,图层合成就是把这四个图层按既定的显示区域,展现到显示屏上。
图片来自:blog.csdn.net/jxt1234and2…
每一个图层对应一块 buffer,所谓合成,就是把多个 buffer 合并成一个,然后送显。
具体怎么合成是一个复杂的工作,现代嵌入式平台通常有两类合成方法:
- Client 合成,使用软件图形库合成,opengles skia 等,这些图形库通常会操作 GPU 来完成合成工作。
- Device 合成,使用特定的硬件合成图层。比如高通平台的 mdp。
Device 合成效率更高,但是能力有限,不支持复杂图形的合成。Client 合成消耗资源更多,但是能力强大,支持各种复杂效果。
2. 合成过程的发起
在 Vsync 章节我们说到,当 sf 收到 Vsync 信号后,会回调到 Scheduler::onFrameSignal 函数:
void Scheduler::onFrameSignal(ICompositor& compositor, VsyncId vsyncId,
TimePoint expectedVsyncTime) {
const TimePoint frameTime = SchedulerClock::now();
// 关注点1
if (!compositor.commit(frameTime, vsyncId, expectedVsyncTime)) {
return;
}
// 关注点2
compositor.composite(frameTime, vsyncId);
compositor.sample();
}
这里的 compositor 实际是 SurfaceFlinger,SurfaceFlinger 实现了 ICompositor 接口。
该函数就是 sf 合成
的发起点。
其中:
- 关注点 1 commit,对所有 layer 的变化信息做一个预处理,
- 关注点 2 composite,发起 layer 的合成操作
3. 合成过程之前的准备工作
在具体分析 commit 和 composite 两个函数实现之前,我们先来看看 SurfaceFlinger 为了完成合成工作做了哪些准备。
合成过程会涉及到以下几个对象:
- RenderEngine 负责 Client 合成
- HWComposer,HWComposer HAL 的客户端包装类,负责 Device 合成
- BufferQueue 负责提供 buffer,buffer 用于保存合成后的结果,注意这里是图层合成阶段的 BufferQueue,不是 App 绘制阶段的 BufferQueue,不要混淆了
接下来我们来看看这几个对象的初始化过程。
在 SurfaceFlinger 的启动过程中会执行到 SurfaceFlinger::init()
函数:
// /frameworks/native/services/surfaceflinger/SurfaceFlinger.cpp
void SurfaceFlinger::init() FTL_FAKE_GUARD(kMainThreadContext) {
// .....
// 关注点 1
// 构建 RenderEngine,用于 Client 合成
auto builder = renderengine::RenderEngineCreationArgs::Builder()
.setPixelFormat(static_cast<int32_t>(defaultCompositionPixelFormat))
.setImageCacheSize(maxFrameBufferAcquiredBuffers)
.setUseColorManagerment(useColorManagement)
.setEnableProtectedContext(enable_protected_contents(false))
.setPrecacheToneMapperShaderOnly(false)
.setSupportsBackgroundBlur(mSupportsBlur)
.setContextPriority(
useContextPriority
? renderengine::RenderEngine::ContextPriority::REALTIME
: renderengine::RenderEngine::ContextPriority::MEDIUM);
if (auto type = chooseRenderEngineTypeViaSysProp()) {
builder.setRenderEngineType(type.value());
}
mRenderEngine = renderengine::RenderEngine::create(builder.build());
mCompositionEngine->setRenderEngine(mRenderEngine.get());
// ......
// 关注点2
// 创建 HWComposer 对象并传入一个 name 属性,再通过 mCompositionEngine->setHwComposer 设置对象属性。
mCompositionEngine->setHwComposer(getFactory().createHWComposer(mHwcServiceName));
// 这里的 this 就是 SurfaceFlinger 对象本身,因为它实现了 HWC2::ComposerCallback 回调接口
mCompositionEngine->getHwComposer().setCallback(*this);
// ......
// Commit primary display.
sp<const DisplayDevice> display;
if (const auto indexOpt = mCurrentState.getDisplayIndex(getPrimaryDisplayIdLocked())) {
const auto& displays = mCurrentState.displays;
const auto& token = displays.keyAt(*indexOpt);
const auto& state = displays.valueAt(*indexOpt);
// 关注点3
processDisplayAdded(token, state);
mDrawingState.displays.add(token, state);
//
display = getDefaultDisplayDeviceLocked();
}
//......
initScheduler(display);
// ......
}
- 关注点 1,构建 RenderEngine 对象,该对象用于 GPU 合成,也叫 Client 合成
- 关注点 2,构建 HWComposer 对象,该对象是 HWC HAL 的客户端包装类,方便客户端使用 HWC HAL
- 关注点 3,初始化 Display 相关对象,我们主要关注 BufferQueue 及其相关类
3.1 RenderEngine 的初始化
关注点 1,初始化了一个 renderengine::RenderEngineCreationArgs::Builder
对象,接着将 builder 对象传入 create 函数构建一个 RenderEngine 对象,create 函数的实现如下:
std::unique_ptr<RenderEngine> RenderEngine::create(const RenderEngineCreationArgs& args) {
// 构建 builder 时,没有配置 renderEngineType 参数
// 使用默认值 RenderEngine::RenderEngineType::SKIA_GL_THREADED
switch (args.renderEngineType) {
case RenderEngineType::THREADED:
ALOGD("Threaded RenderEngine with GLES Backend");
return renderengine::threaded::RenderEngineThreaded::create(
[args]() { return android::renderengine::gl::GLESRenderEngine::create(args); },
args.renderEngineType);
case RenderEngineType::SKIA_GL:
ALOGD("RenderEngine with SkiaGL Backend");
return renderengine::skia::SkiaGLRenderEngine::create(args);
case RenderEngineType::SKIA_VK:
ALOGD("RenderEngine with SkiaVK Backend");
return renderengine::skia::SkiaVkRenderEngine::create(args);
case RenderEngineType::SKIA_GL_THREADED: { // 走这个分支
ALOGD("Threaded RenderEngine with SkiaGL Backend");
return renderengine::threaded::RenderEngineThreaded::create(
[args]() {
return android::renderengine::skia::SkiaGLRenderEngine::create(args);
},
args.renderEngineType);
}
case RenderEngineType::SKIA_VK_THREADED:
ALOGD("Threaded RenderEngine with SkiaVK Backend");
return renderengine::threaded::RenderEngineThreaded::create(
[args]() {
return android::renderengine::skia::SkiaVkRenderEngine::create(args);
},
args.renderEngineType);
case RenderEngineType::GLES:
default:
ALOGD("RenderEngine with GLES Backend");
return renderengine::gl::GLESRenderEngine::create(args);
}
}
create 函数会走到 SKIA_GL_THREADED 这个 case, create 一个 RenderEngineThreaded 对象返回:
std::unique_ptr<RenderEngineThreaded> RenderEngineThreaded::create(CreateInstanceFactory factory,
RenderEngineType type) {
return std::make_unique<RenderEngineThreaded>(std::move(factory), type);
}
实际就是 new 一个 RenderEngineThreaded,其构造函数如下:
RenderEngineThreaded::RenderEngineThreaded(CreateInstanceFactory factory, RenderEngineType type)
: RenderEngine(type) {
ATRACE_CALL();
std::lock_guard lockThread(mThreadMutex);
mThread = std::thread(&RenderEngineThreaded::threadMain, this, factory);
}
在构造函数中会启动一个线程,线程的主函数是 RenderEngineThreaded::threadMain:
void RenderEngineThreaded::threadMain(CreateInstanceFactory factory) NO_THREAD_SAFETY_ANALYSIS {
ATRACE_CALL();
if (!SetTaskProfiles(0, {"SFRenderEnginePolicy"})) {
ALOGW("Failed to set render-engine task profile!");
}
if (setSchedFifo(true) != NO_ERROR) {
ALOGW("Couldn't set SCHED_FIFO");
}
// SkiaGLRenderEngine 类型
mRenderEngine = factory();
pthread_setname_np(pthread_self(), mThreadName);
{
std::scoped_lock lock(mInitializedMutex);
mIsInitialized = true;
}
mInitializedCondition.notify_all();
while (mRunning) {
const auto getNextTask = [this]() -> std::optional<Work> {
std::scoped_lock lock(mThreadMutex);
if (!mFunctionCalls.empty()) {
Work task = mFunctionCalls.front();
mFunctionCalls.pop();
return std::make_optional<Work>(task);
}
return std::nullopt;
};
const auto task = getNextTask();
if (task) {
(*task)(*mRenderEngine);
}
std::unique_lock<std::mutex> lock(mThreadMutex);
mCondition.wait(lock, [this]() REQUIRES(mThreadMutex) {
return !mRunning || !mFunctionCalls.empty();
});
}
// we must release the RenderEngine on the thread that created it
mRenderEngine.reset();
}
完成基本的初始化,主要是使用传入的 factory(create 的时候传入的 lamda 表达式) 构建一个 SkiaGLRenderEngine 对象,然后进入一个无线循环,取 task,执行 task,然后 lock 到锁对象上,等待唤醒。
容易猜到,这里的 Task 应该就是图层的 GPU 合成工作。
最后会调用到 mCompositionEngine->setRenderEngine(mRenderEngine.get());
把构建好的 RenderEngine 对象给到 CompositionEngine 的 mRenderEngine 成员.
// /frameworks/native/services/surfaceflinger/CompositionEngine/src/CompositionEngine.cpp
void CompositionEngine::setRenderEngine(renderengine::RenderEngine* renderEngine) {
mRenderEngine = renderEngine;
}
相关类的关系如下图所示:
3.2 HWComposer 的初始化
接着回到 init 函数中,我们来看看关注点 2 处:
// 关注点2
// 创建 HWComposer 对象并传入一个 name 属性,再通过 mCompositionEngine->setHwComposer 设置对象属性。
mCompositionEngine->setHwComposer(getFactory().createHWComposer(mHwcServiceName));
// 这里的 this 就是 SurfaceFlinger 对象本身,因为它实现了 HWC2::ComposerCallback 回调接口
mCompositionEngine->getHwComposer().setCallback(*this);
首先,明确一点,HardWare Composor hal 是以 Binder 服务的形式存在于系统中的,这里通过 DefaultFactory 构建一个 HwComposer 对象,根据我们以往的分析源码的经验,HwComposer 大概率是一个 Binder 代理端类的包装类。
接下来我们来分析 HwComposer 的初始化过程。
// /frameworks/native/services/surfaceflinger/SurfaceFlingerDefaultFactory.cpp
std::unique_ptr<HWComposer> DefaultFactory::createHWComposer(const std::string& serviceName) {
return std::make_unique<android::impl::HWComposer>(serviceName);
}
直接 new 一个 android::impl::HWComposer,其构造函数实现如下:
// /frameworks/native/services/surfaceflinger/DisplayHardware/HWComposer.cpp
HWComposer::HWComposer(const std::string& composerServiceName)
: HWComposer(Hwc2::Composer::create(composerServiceName)) {}
HWComposer::HWComposer(std::unique_ptr<Hwc2::Composer> composer)
: mComposer(std::move(composer)),
mMaxVirtualDisplayDimension(static_cast<size_t>(sysprop::max_virtual_display_dimension(0))),
mUpdateDeviceProductInfoOnHotplugReconnect(
sysprop::update_device_product_info_on_hotplug_reconnect(false)) {}
接着会调用 create 函数构造一个 AidlComposer 对象,然后调用另一个构造函数重载:
std::unique_ptr<Composer> Composer::create(const std::string& serviceName) {
if (AidlComposer::isDeclared(serviceName)) { // 新版本都会走该分支,使用 aidl hal
return std::make_unique<AidlComposer>(serviceName);
}
return std::make_unique<HidlComposer>(serviceName);
}
AidlComposer 是 IComposer 的包装类,IComposer 实际就是 HWComposer Hal Binder 服务的代理端对象,用于发起 Binder 远程过程调用。构造函数实现如下:
// /frameworks/native/services/surfaceflinger/DisplayHardware/AidlComposerHal.h
std::shared_ptr<AidlIComposer> mAidlComposer;
std::shared_ptr<AidlIComposerClient> mAidlComposerClient;
// /frameworks/native/services/surfaceflinger/DisplayHardware/AidlComposerHal.cpp
AidlComposer::AidlComposer(const std::string& serviceName) {
// This only waits if the service is actually declared
// 拿到代理端对象
mAidlComposer = AidlIComposer::fromBinder(
ndk::SpAIBinder(AServiceManager_waitForService(instance(serviceName).c_str())));
if (!mAidlComposer) {
LOG_ALWAYS_FATAL("Failed to get AIDL composer service");
return;
}
// 拿到匿名 Binder 服务的代理端对象 mAidlComposerClient
if (!mAidlComposer->createClient(&mAidlComposerClient).isOk()) {
LOG_ALWAYS_FATAL("Can't create AidlComposerClient, fallback to HIDL");
return;
}
addReader(translate<Display>(kSingleReaderKey));
// If unable to read interface version, then become backwards compatible.
int32_t version = 1;
const auto status = mAidlComposerClient->getInterfaceVersion(&version);
if (!status.isOk()) {
ALOGE("getInterfaceVersion for AidlComposer constructor failed %s",
status.getDescription().c_str());
}
mSupportsBufferSlotsToClear = version > 1;
if (!mSupportsBufferSlotsToClear) {
if (sysprop::clear_slots_with_set_layer_buffer(false)) {
mClearSlotBuffer = sp<GraphicBuffer>::make(1, 1, PIXEL_FORMAT_RGBX_8888,
GraphicBuffer::USAGE_HW_COMPOSER |
GraphicBuffer::USAGE_SW_READ_OFTEN |
GraphicBuffer::USAGE_SW_WRITE_OFTEN,
"AidlComposer");
if (!mClearSlotBuffer || mClearSlotBuffer->initCheck() != ::android::OK) {
LOG_ALWAYS_FATAL("Failed to allocate a buffer for clearing layer buffer slots");
return;
}
}
}
ALOGI("Loaded AIDL composer3 HAL service");
}
在构造函数中,首先通过 ServiceManager 获取到 HWComposer Hal 的代理端对象 mAidlComposer,接着通过 mAidlComposer 发起远程调用获取到匿名 Binder 服务的代理端对象 mAidlComposerClient。都是 Binder 的基本操作了。不在细说了。总而言之,AidlComposer 类包装了与 HWC Hal 交互的接口,让使用方更为方便地操作 HWC Hal。
到这里呢,我们就梳理出了 CompositionEngine 相关类的关系图:
总之,CompositionEngine 是 SurfaceFlinger 与 RenderEngine 和 HWComposer 沟通的桥梁,RenderEngine 负责图层的 GPU 合成,HWComposer 负责图层的硬件合成。
HWComposer 中还有一个重要成员 mDisplayData,mDisplayData 中有一个重要成员 Display,他们都是什么初始化的呢?
本节开头的代码中:
mCompositionEngine->getHwComposer().setCallback(*this);
这里向 HwComposer 注册了一个回调,回调类型是 HWC2::ComposerCallback ,回调对象就是 SurfaceFlinger,SurfaceFlinger 实现了 HWC2::ComposerCallback 接口。
HWC2::ComposerCallback 接口的定义如下:
struct ComposerCallback {
virtual void onComposerHalHotplug(hal::HWDisplayId, hal::Connection) = 0;
virtual void onComposerHalRefresh(hal::HWDisplayId) = 0;
virtual void onComposerHalVsync(hal::HWDisplayId, nsecs_t timestamp,
std::optional<hal::VsyncPeriodNanos>) = 0;
virtual void onComposerHalVsyncPeriodTimingChanged(hal::HWDisplayId,
const hal::VsyncPeriodChangeTimeline&) = 0;
virtual void onComposerHalSeamlessPossible(hal::HWDisplayId) = 0;
virtual void onComposerHalVsyncIdle(hal::HWDisplayId) = 0;
virtual void onRefreshRateChangedDebug(const RefreshRateChangedDebugData&) = 0;
protected:
~ComposerCallback() = default;
};
当手机开机时或者有新的显示设备加入时,就会回调到这里的 onComposerHalHotplug 函数。SurfaceFlinger 中,该函数的实现如下:
void SurfaceFlinger::onComposerHalHotplug(hal::HWDisplayId hwcDisplayId,
hal::Connection connection) {
{
std::lock_guard<std::mutex> lock(mHotplugMutex);
mPendingHotplugEvents.push_back(HotplugEvent{hwcDisplayId, connection});
}
if (mScheduler) {
mScheduler->scheduleConfigure();
}
}
接着调用 mScheduler->scheduleConfigure();
:
void MessageQueue::scheduleConfigure() {
struct ConfigureHandler : MessageHandler {
explicit ConfigureHandler(ICompositor& compositor) : compositor(compositor) {}
void handleMessage(const Message&) override { compositor.configure(); }
ICompositor& compositor;
};
// TODO(b/241285876): Batch configure tasks that happen within some duration.
postMessage(sp<ConfigureHandler>::make(mCompositor));
}
这里会 postMessage,对应的 Handler 回调函数 handleMessage 中会接着调用 compositor.configure()
,这里的 compositor 实际类型是 SurfaceFligner。
void SurfaceFlinger::configure() FTL_FAKE_GUARD(kMainThreadContext) {
Mutex::Autolock lock(mStateLock);
if (configureLocked()) {
setTransactionFlags(eDisplayTransactionNeeded);
}
}
接着调用 configureLocked
:
bool SurfaceFlinger::configureLocked() {
std::vector<HotplugEvent> events;
{
std::lock_guard<std::mutex> lock(mHotplugMutex);
events = std::move(mPendingHotplugEvents);
}
for (const auto [hwcDisplayId, connection] : events) {
if (auto info = getHwComposer().onHotplug(hwcDisplayId, connection)) {
const auto displayId = info->id;
const bool connected = connection == hal::Connection::CONNECTED;
if (const char* const log =
processHotplug(displayId, hwcDisplayId, connected, std::move(*info))) {
ALOGI("%s display %s (HAL ID %" PRIu64 ")", log, to_string(displayId).c_str(),
hwcDisplayId);
}
}
}
return !events.empty();
}
这里会接着调用到 getHwComposer().onHotplug
:
std::optional<DisplayIdentificationInfo> HWComposer::onHotplug(hal::HWDisplayId hwcDisplayId,
hal::Connection connection) {
switch (connection) {
case hal::Connection::CONNECTED:
return onHotplugConnect(hwcDisplayId);
case hal::Connection::DISCONNECTED:
return onHotplugDisconnect(hwcDisplayId);
case hal::Connection::INVALID:
return {};
}
}
接着调用 onHotplugConnect
:
std::optional<DisplayIdentificationInfo> HWComposer::onHotplugConnect(
hal::HWDisplayId hwcDisplayId) {
// ......
if (!isConnected(info->id)) {
allocatePhysicalDisplay(hwcDisplayId, info->id);
}
return info;
}
这里会调用到 allocatePhysicalDisplay
:
void HWComposer::allocatePhysicalDisplay(hal::HWDisplayId hwcDisplayId,
PhysicalDisplayId displayId) {
mPhysicalDisplayIdMap[hwcDisplayId] = displayId;
if (!mPrimaryHwcDisplayId) {
mPrimaryHwcDisplayId = hwcDisplayId;
}
// unordered_map 如果当前容器中没有以 key 为键的键值对,则其会使用该键向当前容器中插入一个新键值对。
auto& displayData = mDisplayData[displayId];
auto newDisplay =
std::make_unique<HWC2::impl::Display>(*mComposer.get(), mCapabilities, hwcDisplayId,
hal::DisplayType::PHYSICAL);
newDisplay->setConnected(true);
displayData.hwcDisplay = std::move(newDisplay);
}
这里会向 mDisplayData 插入一组新的数据,同时初始化一个 HWC2::impl::Display
对象保存在 displayData.hwcDisplay
中。
HWC2::impl::Display
继承自 HWC2::Display
,是显示设备的抽象:
class Display : public HWC2::Display {
//......
using Layers = std::unordered_map<hal::HWLayerId, std::weak_ptr<HWC2::impl::Layer>>;
Layers mLayers;
//......
}
有一个重要成员 mLayers,用于保存 HWC2::impl::Layer
Display 的构造函数实现如下:
Display::Display(android::Hwc2::Composer& composer,
const std::unordered_set<AidlCapability>& capabilities, HWDisplayId id,
DisplayType type)
: mComposer(composer), mCapabilities(capabilities), mId(id), mType(type) {
ALOGV("Created display %" PRIu64, id);
}
就是几个成员的赋值。
3.3 BufferQueue 的初始化
关注点 3,完成合成阶段 BufferQueue 及其相关类的初始化。
// /frameworks/native/services/surfaceflinger/SurfaceFlinger.cpp
void SurfaceFlinger::init() FTL_FAKE_GUARD(kMainThreadContext) {
// ......
processDisplayAdded(token, state);
// ......
}
void SurfaceFlinger::processDisplayAdded(const wp<IBinder>& displayToken,
const DisplayDeviceState& state) {
// ......
// 关注点 1,创建 Display 对象
auto compositionDisplay = getCompositionEngine().createDisplay(builder.build());
compositionDisplay->setLayerCachingEnabled(mLayerCachingEnabled);
sp<compositionengine::DisplaySurface> displaySurface;
// 关注点 2 创建 BufferQueue 获取到生产者和消费者
sp<IGraphicBufferProducer> producer;
sp<IGraphicBufferProducer> bqProducer;
sp<IGraphicBufferConsumer> bqConsumer;
getFactory().createBufferQueue(&bqProducer, &bqConsumer, /*consumerIsSurfaceFlinger =*/false);
if (state.isVirtual()) { // 虚拟屏,不管
// ......
} else {
ALOGE_IF(state.surface != nullptr,
"adding a supported display, but rendering "
"surface is provided (%p), ignoring it",
state.surface.get());
const auto displayId = PhysicalDisplayId::tryCast(compositionDisplay->getId());
LOG_FATAL_IF(!displayId);
// 关注点 3
// 创建了 FramebufferSurface 对象,FramebufferSurface 继承自 compositionengine::DisplaySurface
// FramebufferSurface 是作为消费者的角色工作的,消费 SF GPU 合成后的图形数据
displaySurface =
sp<FramebufferSurface>::make(getHwComposer(), *displayId, bqConsumer,
state.physical->activeMode->getResolution(),
ui::Size(maxGraphicsWidth, maxGraphicsHeight));
producer = bqProducer;
}
LOG_FATAL_IF(!displaySurface);
// 关注点 4 创建 DisplayDevice,同时会创建 RenderSurface,作为生产者角色工作
auto display = setupNewDisplayDeviceInternal(displayToken, std::move(compositionDisplay), state,
displaySurface, producer);
if (mScheduler && !display->isVirtual()) {
const auto displayId = display->getPhysicalId();
{
// TODO(b/241285876): Annotate `processDisplayAdded` instead.
ftl::FakeGuard guard(kMainThreadContext);
// For hotplug reconnect, renew the registration since display modes have been reloaded.
mScheduler->registerDisplay(displayId, display->holdRefreshRateSelector());
}
dispatchDisplayHotplugEvent(displayId, true);
}
if (display->isVirtual()) {
display->adjustRefreshRate(mScheduler->getPacesetterRefreshRate());
}
mDisplays.try_emplace(displayToken, std::move(display));
}
关注点1,初始化 Display 对象过程,Display 对象是显示设备的抽象。
std::shared_ptr<compositionengine::Display> CompositionEngine::createDisplay(
const DisplayCreationArgs& args) {
return compositionengine::impl::createDisplay(*this, args);
}
namespace android::compositionengine::impl {
std::shared_ptr<Display> createDisplay(
const compositionengine::CompositionEngine& compositionEngine,
const compositionengine::DisplayCreationArgs& args) {
return createDisplayTemplated<Display>(compositionEngine, args);
}
//......
}
进一步调用到 createDisplayTemplated 函数:
// BaseDisplay -> android::compositionengine::impl::Display
// CompositionEngine -> compositionengine::CompositionEngine
template <typename BaseDisplay, typename CompositionEngine>
std::shared_ptr<BaseDisplay> createDisplayTemplated(
const CompositionEngine& compositionEngine,
const compositionengine::DisplayCreationArgs& args) {
auto display = createOutputTemplated<BaseDisplay>(compositionEngine);
display->setConfiguration(args);
return display;
}
接着调用模版函数:
// BaseOutput -> BaseDisplay -> android::compositionengine::impl::Display
// CompositionEngine -> compositionengine::CompositionEngine
template <typename BaseOutput, typename CompositionEngine, typename... Args>
std::shared_ptr<BaseOutput> createOutputTemplated(const CompositionEngine& compositionEngine,
Args... args) {
// 定义一个内部类
// 实际继承自 android::compositionengine::impl::Display
class Output final : public BaseOutput {
public:
// Clang incorrectly complains that these are unused.
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wunused-local-typedef"
// compositionengine::impl::OutputCompositionState
using OutputCompositionState = std::remove_const_t<
std::remove_reference_t<decltype(std::declval<BaseOutput>().getState())>>;
using OutputLayer = std::remove_pointer_t<decltype(
std::declval<BaseOutput>().getOutputLayerOrderedByZByIndex(0))>;
#pragma clang diagnostic pop
explicit Output(const CompositionEngine& compositionEngine, Args... args)
: BaseOutput(std::forward<Args>(args)...), mCompositionEngine(compositionEngine) {}
~Output() override = default;
private:
// compositionengine::Output overrides
const OutputCompositionState& getState() const override { return mState; }
OutputCompositionState& editState() override { return mState; }
size_t getOutputLayerCount() const override {
return mCurrentOutputLayersOrderedByZ.size();
}
OutputLayer* getOutputLayerOrderedByZByIndex(size_t index) const override {
if (index >= mCurrentOutputLayersOrderedByZ.size()) {
return nullptr;
}
return mCurrentOutputLayersOrderedByZ[index].get();
}
// compositionengine::impl::Output overrides
const CompositionEngine& getCompositionEngine() const override {
return mCompositionEngine;
};
OutputLayer* ensureOutputLayer(std::optional<size_t> prevIndex,
const sp<LayerFE>& layerFE) {
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;
}
void finalizePendingOutputLayers() override {
// The pending layers are added in reverse order. Reverse them to
// get the back-to-front ordered list of layers.
std::reverse(mPendingOutputLayersOrderedByZ.begin(),
mPendingOutputLayersOrderedByZ.end());
mCurrentOutputLayersOrderedByZ = std::move(mPendingOutputLayersOrderedByZ);
}
void dumpState(std::string& out) const override { mState.dump(out); }
OutputLayer* injectOutputLayerForTest(const sp<LayerFE>& layerFE) override {
auto outputLayer = BaseOutput::createOutputLayer(layerFE);
auto result = outputLayer.get();
mCurrentOutputLayersOrderedByZ.emplace_back(std::move(outputLayer));
return result;
}
// Note: This is declared as a private virtual non-override so it can be
// an override implementation in the unit tests, but otherwise is not an
// accessible override for the normal implementation.
virtual void injectOutputLayerForTest(std::unique_ptr<OutputLayer> outputLayer) {
mCurrentOutputLayersOrderedByZ.emplace_back(std::move(outputLayer));
}
void clearOutputLayers() override {
mCurrentOutputLayersOrderedByZ.clear();
mPendingOutputLayersOrderedByZ.clear();
}
const CompositionEngine& mCompositionEngine;
OutputCompositionState mState;
std::vector<std::unique_ptr<OutputLayer>> mCurrentOutputLayersOrderedByZ;
std::vector<std::unique_ptr<OutputLayer>> mPendingOutputLayersOrderedByZ;
};
// 构建一个内部类的对象
return std::make_shared<Output>(compositionEngine, std::forward<Args>(args)...);
}
定义了一个 Output 函数内部类,然后 new 一个对象返回。
Output 是一个模版类,当前场景下 Output 相关类类图如下:
Outout 中的两个成员很重要:
std::vector<std::unique_ptr<OutputLayer>> mCurrentOutputLayersOrderedByZ;
std::vector<std::unique_ptr<OutputLayer>> mPendingOutputLayersOrderedByZ;
后续合成过程中就会构建新的 OutputLayer 对象并插入这两个 vector。
Display 对象到这里就初始化完成了。
关注点2 创建 BufferQueue:
// /frameworks/native/services/surfaceflinger/SurfaceFlingerDefaultFactory.cpp
void DefaultFactory::createBufferQueue(sp<IGraphicBufferProducer>* outProducer,
sp<IGraphicBufferConsumer>* outConsumer,
bool consumerIsSurfaceFlinger) {
BufferQueue::createBufferQueue(outProducer, outConsumer, consumerIsSurfaceFlinger);
}
// /frameworks/native/libs/gui/BufferQueue.cpp
void BufferQueue::createBufferQueue(sp<IGraphicBufferProducer>* outProducer,
sp<IGraphicBufferConsumer>* outConsumer,
bool consumerIsSurfaceFlinger) {
LOG_ALWAYS_FATAL_IF(outProducer == nullptr,
"BufferQueue: outProducer must not be NULL");
LOG_ALWAYS_FATAL_IF(outConsumer == nullptr,
"BufferQueue: outConsumer must not be NULL");
sp<BufferQueueCore> core(new BufferQueueCore());
LOG_ALWAYS_FATAL_IF(core == nullptr,
"BufferQueue: failed to create BufferQueueCore");
sp<IGraphicBufferProducer> producer(new BufferQueueProducer(core, consumerIsSurfaceFlinger));
LOG_ALWAYS_FATAL_IF(producer == nullptr,
"BufferQueue: failed to create BufferQueueProducer");
sp<IGraphicBufferConsumer> consumer(new BufferQueueConsumer(core));
LOG_ALWAYS_FATAL_IF(consumer == nullptr,
"BufferQueue: failed to create BufferQueueConsumer");
*outProducer = producer;
*outConsumer = consumer;
}
初始化一个 BufferQueueCore,然后通过 BufferQueueCore 初始化好 Producer,Consumer。Producer 不断的 dequeueBuffer & queueBuffer ; Consumer 不断的 acquireBuffer & releaseBuffer,这样 Buffer 就流转起来了。
不论是 Producer,还是 Consumer,都是和具体的类结合起来使用:
- RenderSurface 对象间接持有 producer,作为生产者
- FramebufferSurface 对象会持有 consumer,作为消费者
关注点 3,初始化一个 FramebufferSurface 对象,持有 consumer,是具体的消费者,其构造函数实现如下:
FramebufferSurface::FramebufferSurface(HWComposer& hwc, PhysicalDisplayId displayId,
const sp<IGraphicBufferConsumer>& consumer,
const ui::Size& size, const ui::Size& maxSize)
: ConsumerBase(consumer), // 持有 consumer
mDisplayId(displayId),
mMaxSize(maxSize),
mCurrentBufferSlot(-1),
mCurrentBuffer(),
mCurrentFence(Fence::NO_FENCE),
mHwc(hwc),
mHasPendingRelease(false),
mPreviousBufferSlot(BufferQueue::INVALID_BUFFER_SLOT),
mPreviousBuffer() {
ALOGV("Creating for display %s", to_string(displayId).c_str());
mName = "FramebufferSurface";
mConsumer->setConsumerName(mName);
mConsumer->setConsumerUsageBits(GRALLOC_USAGE_HW_FB |
GRALLOC_USAGE_HW_RENDER |
GRALLOC_USAGE_HW_COMPOSER);
const auto limitedSize = limitSize(size);
mConsumer->setDefaultBufferSize(limitedSize.width, limitedSize.height);
mConsumer->setMaxAcquiredBufferCount(
SurfaceFlinger::maxFrameBufferAcquiredBuffers - 1);
for (size_t i = 0; i < sizeof(mHwcBufferIds) / sizeof(mHwcBufferIds[0]); ++i) {
mHwcBufferIds[i] = UINT64_MAX;
}
}
主要是初始化和一些配置,FramebufferSurface 内部持有一个 consumer,是 GPU 合成后 Buffer 的消费端。
接下来看关注点 4,主要任务是初始化一个 DisplayDevice 对象,同时会构建生产者对象 RenderSurface
这里会调用 setupNewDisplayDeviceInternal
函数:
sp<DisplayDevice> SurfaceFlinger::setupNewDisplayDeviceInternal(
const wp<IBinder>& displayToken,
std::shared_ptr<compositionengine::Display> compositionDisplay,
const DisplayDeviceState& state,
const sp<compositionengine::DisplaySurface>& displaySurface,
const sp<IGraphicBufferProducer>& producer) {
// 构建 DisplayDevice 的参数
DisplayDeviceCreationArgs creationArgs(sp<SurfaceFlinger>::fromExisting(this), getHwComposer(),
displayToken, compositionDisplay);
creationArgs.sequenceId = state.sequenceId;
creationArgs.isSecure = state.isSecure;
creationArgs.displaySurface = displaySurface;
creationArgs.hasWideColorGamut = false;
creationArgs.supportedPerFrameMetadata = 0;
if (const auto& physical = state.physical) {
creationArgs.activeModeId = physical->activeMode->getId();
const auto [kernelIdleTimerController, idleTimerTimeoutMs] =
getKernelIdleTimerProperties(compositionDisplay->getId());
using Config = scheduler::RefreshRateSelector::Config;
const auto enableFrameRateOverride = sysprop::enable_frame_rate_override(true)
? Config::FrameRateOverride::Enabled
: Config::FrameRateOverride::Disabled;
Config config =
{.enableFrameRateOverride = enableFrameRateOverride,
.frameRateMultipleThreshold =
base::GetIntProperty("debug.sf.frame_rate_multiple_threshold", 0),
.idleTimerTimeout = idleTimerTimeoutMs,
.kernelIdleTimerController = kernelIdleTimerController};
creationArgs.refreshRateSelector =
mPhysicalDisplays.get(physical->id)
.transform(&PhysicalDisplay::snapshotRef)
.transform([&](const display::DisplaySnapshot& snapshot) {
return std::make_shared<
scheduler::RefreshRateSelector>(snapshot.displayModes(),
creationArgs.activeModeId,
config);
})
.value_or(nullptr);
creationArgs.isPrimary = physical->id == getPrimaryDisplayIdLocked();
if (useColorManagement) {
mPhysicalDisplays.get(physical->id)
.transform(&PhysicalDisplay::snapshotRef)
.transform(ftl::unit_fn([&](const display::DisplaySnapshot& snapshot) {
for (const auto mode : snapshot.colorModes()) {
creationArgs.hasWideColorGamut |= ui::isWideColorMode(mode);
creationArgs.hwcColorModes
.emplace(mode,
getHwComposer().getRenderIntents(physical->id, mode));
}
}));
}
}
if (const auto id = HalDisplayId::tryCast(compositionDisplay->getId())) {
getHwComposer().getHdrCapabilities(*id, &creationArgs.hdrCapabilities);
creationArgs.supportedPerFrameMetadata = getHwComposer().getSupportedPerFrameMetadata(*id);
}
// 关注点 1,构建 NativeWindowSurface 对象
auto nativeWindowSurface = getFactory().createNativeWindowSurface(producer);
auto nativeWindow = nativeWindowSurface->getNativeWindow();
creationArgs.nativeWindow = nativeWindow;
// Make sure that composition can never be stalled by a virtual display
// consumer that isn't processing buffers fast enough. We have to do this
// here, in case the display is composed entirely by HWC.
if (state.isVirtual()) {
nativeWindow->setSwapInterval(nativeWindow.get(), 0);
}
creationArgs.physicalOrientation =
getPhysicalDisplayOrientation(compositionDisplay->getId(), creationArgs.isPrimary);
ALOGV("Display Orientation: %s", toCString(creationArgs.physicalOrientation));
// virtual displays are always considered enabled
creationArgs.initialPowerMode =
state.isVirtual() ? std::make_optional(hal::PowerMode::ON) : std::nullopt;
creationArgs.requestedRefreshRate = state.requestedRefreshRate;
// 前面的代码都是准备 creationArgs
// 关注点2
// Factory 类型是 DefaultFactory,实际就是 new 一个 DisplayDevice
sp<DisplayDevice> display = getFactory().createDisplayDevice(creationArgs);
nativeWindowSurface->preallocateBuffers();
ui::ColorMode defaultColorMode = ui::ColorMode::NATIVE;
Dataspace defaultDataSpace = Dataspace::UNKNOWN;
if (display->hasWideColorGamut()) {
defaultColorMode = ui::ColorMode::SRGB;
defaultDataSpace = Dataspace::V0_SRGB;
}
display->getCompositionDisplay()->setColorProfile(
compositionengine::Output::ColorProfile{defaultColorMode, defaultDataSpace,
RenderIntent::COLORIMETRIC,
Dataspace::UNKNOWN});
if (const auto& physical = state.physical) {
mPhysicalDisplays.get(physical->id)
.transform(&PhysicalDisplay::snapshotRef)
.transform(ftl::unit_fn([&](const display::DisplaySnapshot& snapshot) {
FTL_FAKE_GUARD(kMainThreadContext,
display->setActiveMode(physical->activeMode->getId(),
physical->activeMode->getFps(),
physical->activeMode->getFps()));
}));
}
display->setLayerFilter(makeLayerFilterForDisplay(display->getId(), state.layerStack));
display->setProjection(state.orientation, state.layerStackSpaceRect,
state.orientedDisplaySpaceRect);
display->setDisplayName(state.displayName);
display->setFlags(state.flags);
return display;
}
关注点1:
开始的一大堆代码都是用来准备 DisplayDeviceCreationArgs 参数,我们主要关注一下其中的 nativeWindow,它通过 createNativeWindowSurface 函数初始化:
// /frameworks/native/services/surfaceflinger/SurfaceFlingerDefaultFactory.cpp
std::unique_ptr<surfaceflinger::NativeWindowSurface> DefaultFactory::createNativeWindowSurface(
const sp<IGraphicBufferProducer>& producer) {
return surfaceflinger::impl::createNativeWindowSurface(producer);
}
接着调用 impl::createNativeWindowSurface
:
// /frameworks/native/services/surfaceflinger/NativeWindowSurface.cpp
std::unique_ptr<surfaceflinger::NativeWindoANativeWindowwSurface> createNativeWindowSurface(
const sp<IGraphicBufferProducer>& producer) {
class NativeWindowSurface final : public surfaceflinger::NativeWindowSurface {
public:
explicit NativeWindowSurface(const sp<IGraphicBufferProducer>& producer)
: mSurface(sp<Surface>::make(producer, /* controlledByApp */ false)) {}
~NativeWindowSurface() override = default;
sp<ANativeWindow> getNativeWindow() const override { return mSurface; }
void preallocateBuffers() override { mSurface->allocateBuffers(); }
private:
sp<Surface> mSurface;
};
return std::make_unique<NativeWindowSurface>(producer);
}
NativeWindowSurface 是一个函数内部类(自己写代码就没用过),继承自 surfaceflinger::NativeWindowSurface。有一个重要成员 mSurface,Surface 是 Producer 的具体持有者,这里会初始化一个 Surface 对象然后赋值给 mSurface 成员,其构造函数实现如下:
Surface::Surface(const sp<IGraphicBufferProducer>& bufferProducer, bool controlledByApp,
const sp<IBinder>& surfaceControlHandle)
: mGraphicBufferProducer(bufferProducer),
mCrop(Rect::EMPTY_RECT),
mBufferAge(0),
mGenerationNumber(0),
mSharedBufferMode(false),
mAutoRefresh(false),
mAutoPrerotation(false),
mSharedBufferSlot(BufferItem::INVALID_BUFFER_SLOT),
mSharedBufferHasBeenQueued(false),
mQueriedSupportedTimestamps(false),
mFrameTimestampsSupportsPresent(false),
mEnableFrameTimestamps(false),
mFrameEventHistory(std::make_unique<ProducerFrameEventHistory>()) {
// Initialize the ANativeWindow function pointers.
ANativeWindow::setSwapInterval = hook_setSwapInterval;
ANativeWindow::dequeueBuffer = hook_dequeueBuffer;
ANativeWindow::cancelBuffer = hook_cancelBuffer;
ANativeWindow::queueBuffer = hook_queueBuffer;
ANativeWindow::query = hook_query;
ANativeWindow::perform = hook_perform;
ANativeWindow::dequeueBuffer_DEPRECATED = hook_dequeueBuffer_DEPRECATED;
ANativeWindow::cancelBuffer_DEPRECATED = hook_cancelBuffer_DEPRECATED;
ANativeWindow::lockBuffer_DEPRECATED = hook_lockBuffer_DEPRECATED;
ANativeWindow::queueBuffer_DEPRECATED = hook_queueBuffer_DEPRECATED;
const_cast<int&>(ANativeWindow::minSwapInterval) = 0;
const_cast<int&>(ANativeWindow::maxSwapInterval) = 1;
mReqWidth = 0;
mReqHeight = 0;
mReqFormat = 0;
mReqUsage = 0;
mTimestamp = NATIVE_WINDOW_TIMESTAMP_AUTO;
mDataSpace = Dataspace::UNKNOWN;
mScalingMode = NATIVE_WINDOW_SCALING_MODE_FREEZE;
mTransform = 0;
mStickyTransform = 0;
mDefaultWidth = 0;
mDefaultHeight = 0;
mUserWidth = 0;
mUserHeight = 0;
mTransformHint = 0;
mConsumerRunningBehind = false;
mConnectedToCpu = false;
mProducerControlledByApp = controlledByApp;
mSwapIntervalZero = false;
mMaxBufferCount = NUM_BUFFER_SLOTS;
mSurfaceControlHandle = surfaceControlHandle;
}
Surface 的构造函数就是一堆赋值。
关注点2,会通过工厂模式构建一个 DisplayDevice 对象:
sp<DisplayDevice> DefaultFactory::createDisplayDevice(DisplayDeviceCreationArgs& creationArgs) {
return sp<DisplayDevice>::make(creationArgs);
}
实际就是 new 一个 DisplayDevice 对象:
DisplayDevice::DisplayDevice(DisplayDeviceCreationArgs& args)
: mFlinger(args.flinger),
mHwComposer(args.hwComposer),
mDisplayToken(args.displayToken),
mSequenceId(args.sequenceId),
mCompositionDisplay{args.compositionDisplay}, // Display 成员
mActiveModeFPSTrace("ActiveModeFPS -" + to_string(getId())),
mActiveModeFPSHwcTrace("ActiveModeFPS_HWC -" + to_string(getId())),
mRenderFrameRateFPSTrace("RenderRateFPS -" + to_string(getId())),
mPhysicalOrientation(args.physicalOrientation),
mIsPrimary(args.isPrimary),
mRequestedRefreshRate(args.requestedRefreshRate),
mRefreshRateSelector(std::move(args.refreshRateSelector)) {
mCompositionDisplay->editState().isSecure = args.isSecure;
// 初始化 Display 成员的 mRenderSurface 成员
mCompositionDisplay->createRenderSurface(
compositionengine::RenderSurfaceCreationArgsBuilder()
.setDisplayWidth(ANativeWindow_getWidth(args.nativeWindow.get()))
.setDisplayHeight(ANativeWindow_getHeight(args.nativeWindow.get()))
.setNativeWindow(std::move(args.nativeWindow))
.setDisplaySurface(std::move(args.displaySurface))
.setMaxTextureCacheSize(
static_cast<size_t>(SurfaceFlinger::maxFrameBufferAcquiredBuffers))
.build());
if (!mFlinger->mDisableClientCompositionCache &&
SurfaceFlinger::maxFrameBufferAcquiredBuffers > 0) {
mCompositionDisplay->createClientCompositionCache(
static_cast<uint32_t>(SurfaceFlinger::maxFrameBufferAcquiredBuffers));
}
mCompositionDisplay->setPredictCompositionStrategy(mFlinger->mPredictCompositionStrategy);
mCompositionDisplay->setTreat170mAsSrgb(mFlinger->mTreat170mAsSrgb);
mCompositionDisplay->createDisplayColorProfile(
compositionengine::DisplayColorProfileCreationArgsBuilder()
.setHasWideColorGamut(args.hasWideColorGamut)
.setHdrCapabilities(std::move(args.hdrCapabilities))
.setSupportedPerFrameMetadata(args.supportedPerFrameMetadata)
.setHwcColorModes(std::move(args.hwcColorModes))
.Build());
if (!mCompositionDisplay->isValid()) {
ALOGE("Composition Display did not validate!");
}
mCompositionDisplay->getRenderSurface()->initialize();
if (const auto powerModeOpt = args.initialPowerMode) {
setPowerMode(*powerModeOpt);
}
// initialize the display orientation transform.
setProjection(ui::ROTATION_0, Rect::INVALID_RECT, Rect::INVALID_RECT);
}
构造函数中会调用 createRenderSurface 去构造一个 RenderSurface 对象,调用方是前面构造的 Display 对象:
void Display::createRenderSurface(const RenderSurfaceCreationArgs& args) {
setRenderSurface(
compositionengine::impl::createRenderSurface(getCompositionEngine(), *this, args));
}
std::unique_ptr<compositionengine::RenderSurface> createRenderSurface(
const compositionengine::CompositionEngine& compositionEngine,
compositionengine::Display& display,
const compositionengine::RenderSurfaceCreationArgs& args) {
return std::make_unique<RenderSurface>(compositionEngine, display, args);
}
void Output::setRenderSurface(std::unique_ptr<compositionengine::RenderSurface> surface) {
mRenderSurface = std::move(surface);
const auto size = mRenderSurface->getSize();
editState().framebufferSpace.setBounds(size);
if (mPlanner) {
mPlanner->setDisplaySize(size);
}
dirtyEntireOutput();
}
RenderSurface 构造函数如下:
RenderSurface::RenderSurface(const CompositionEngine& compositionEngine, Display& display,
const RenderSurfaceCreationArgs& args)
: mCompositionEngine(compositionEngine),
mDisplay(display),
mNativeWindow(args.nativeWindow), // 这里是前面初始化好的 NativeWindowSurface
mDisplaySurface(args.displaySurface),
mSize(args.displayWidth, args.displayHeight),
mMaxTextureCacheSize(args.maxTextureCacheSize) {
LOG_ALWAYS_FATAL_IF(!mNativeWindow);
}
RenderSurface 会持有前面初始化好的 NativeWindowSurface 对象,NativeWindowSurface 持有 Surface 对象,Surface 对象持有 Producer 对象。所以 RenderSurface 间接持有了 Producer.
client 合成时(先了解一下,后面有分析):
- RenderSurface 间接持有 producer 对象,作为生产者的角色工作,申请 buffer,然后 client 合成 layer,然后把合成结果存在 buffer 中,最后提交 buffer,
- FramebufferSurface 持有 comsumer,作为消费者,acquireBuffer 拿到合成好的 buffer,然后通过 setClientTarget 把这个 buffer 传递给 HWC 做处理显示。
4. commit 合成数据预处理
本节将对 commit 阶段预处理数据的过程进行详细解析。
4.1 回顾:预处理的数据都来自哪里?
commit 预处理合成相关的数据,需要预处理的数据主要来自三个方面:
- App 调用 createSurface,创建图层,SurfaceFlinger 进程构建 Layer 对象
- App 发起远程事务,配置图层,Transaction
- App 通过 Transaction 提交渲染好的 buffer
接下来,我们逐一回顾这些数据的构建过程:
4.1.1 App 调用 createSurface
App 端的实现:
tring8 name("displaydemo");
sp<SurfaceControl> surfaceControl =
surfaceComposerClient->createSurface(name, resolution.getWidth(),
resolution.getHeight(), PIXEL_FORMAT_RGBA_8888,
ISurfaceComposerClient::eFXSurfaceBufferState,/*parent*/ nullptr);
createSurface 的实现如下:
// /frameworks/native/libs/gui/SurfaceComposerClient.cpp
sp<SurfaceControl> SurfaceComposerClient::createSurface(const String8& name, uint32_t w, uint32_t h,
PixelFormat format, int32_t flags,
const sp<IBinder>& parentHandle,
LayerMetadata metadata,
uint32_t* outTransformHint) {
sp<SurfaceControl> s;
createSurfaceChecked(name, w, h, format, &s, flags, parentHandle, std::move(metadata),
outTransformHint);
return s;
}
接着调用 createSurfaceChecked:
// /frameworks/native/libs/gui/SurfaceComposerClient.cpp
status_t SurfaceComposerClient::createSurfaceChecked(const String8& name, uint32_t w, uint32_t h,
PixelFormat format,
sp<SurfaceControl>* outSurface, int32_t flags,
const sp<IBinder>& parentHandle,
LayerMetadata metadata,
uint32_t* outTransformHint) {
sp<SurfaceControl> sur;
status_t err = mStatus;
if (mStatus == NO_ERROR) {
gui::CreateSurfaceResult result;
// 发起远程调用
// 创建 Layer
// Layer 相关的信息通过参数 result 返回
// 重点关注 result 中的 handle 成员
binder::Status status = mClient->createSurface(std::string(name.string()), flags,
parentHandle, std::move(metadata), &result);
err = statusTFromBinderStatus(status);
if (outTransformHint) {
*outTransformHint = result.transformHint;
}
ALOGE_IF(err, "SurfaceComposerClient::createSurface error %s", strerror(-err));
if (err == NO_ERROR) {
// 通过 Result 的参数 new 一个 SurfaceControl
// 重点是 handle
*outSurface = new SurfaceControl(this, result.handle, result.layerId,
toString(result.layerName), w, h, format,
result.transformHint, flags);
}
}
return err;
}
mClient 是匿名 Binder 服务 Client 的代理端对象,这里远程调用到 SurfaceFlinger 进程中的 createSurface 函数,最终会调用到服务端 Client 类的 createSurface 函数中。
// /frameworks/native/services/surfaceflinger/Client.h
sp<SurfaceFlinger> mFlinger;
// /frameworks/native/services/surfaceflinger/Client.cpp
binder::Status Client::createSurface(const std::string& name, int32_t flags,
const sp<IBinder>& parent, const gui::LayerMetadata& metadata,
gui::CreateSurfaceResult* outResult) {
// We rely on createLayer to check permissions.
sp<IBinder> handle;
LayerCreationArgs args(mFlinger.get(), sp<Client>::fromExisting(this), name.c_str(),
static_cast<uint32_t>(flags), std::move(metadata));
// handle 放到了 LayerCreationArgs 中,这个 handle 是我们即将要创建的 Layer 的父 Layer 的索引
// 需要注意的是 CreateSurfaceResult 中也有一个 handle,这个是我们要创建的 Layer 的索引
args.parentHandle = parent;
// 接着调用 SurfaceFlinger 的 createLayer 函数
const status_t status = mFlinger->createLayer(args, *outResult);
return binderStatusFromStatusT(status);
}
SurfaceFlinger 的 createLayer 函数实现如下:
// /frameworks/native/services/surfaceflinger/SurfaceFlinger.cpp
status_t SurfaceFlinger::createLayer(LayerCreationArgs& args, gui::CreateSurfaceResult& outResult) {
status_t result = NO_ERROR;
sp<Layer> layer;
// flags 的值为 ISurfaceComposerClient::eFXSurfaceBufferState
switch (args.flags & ISurfaceComposerClient::eFXSurfaceMask) {
case ISurfaceComposerClient::eFXSurfaceBufferQueue:
case ISurfaceComposerClient::eFXSurfaceContainer:
case ISurfaceComposerClient::eFXSurfaceBufferState: // 走这个 case
args.flags |= ISurfaceComposerClient::eNoColorFill;
FMT_FALLTHROUGH;
case ISurfaceComposerClient::eFXSurfaceEffect: { // 上面没有 break,这个 case 也会走
// 关注点 1
// 创建 Layer 对象
// args 中有个 handle
// 第二个参数也是一个 handle
result = createBufferStateLayer(args, &outResult.handle, &layer);
std::atomic<int32_t>* pendingBufferCounter = layer->getPendingBufferCounter();
if (pendingBufferCounter) {
std::string counterName = layer->getPendingBufferCounterName();
mBufferCountTracker.add(outResult.handle->localBinder(), counterName,
pendingBufferCounter);
}
} break;
default:
result = BAD_VALUE;
break;
}
if (result != NO_ERROR) {
return result;
}
args.addToRoot = args.addToRoot && callingThreadHasUnscopedSurfaceFlingerAccess();
// We can safely promote the parent layer in binder thread because we have a strong reference
// to the layer's handle inside this scope.
// 使用 args.parentHandle 索引找到 Layer 的父节点
sp<Layer> parent = LayerHandle::getLayer(args.parentHandle.promote());
if (args.parentHandle != nullptr && parent == nullptr) {
ALOGE("Invalid parent handle %p", args.parentHandle.promote().get());
args.addToRoot = false;
}
uint32_t outTransformHint;
// add a layer to SurfaceFlinger
// 关注点 2
// 把 Layer 对象添加到 SurfaceFlinger 中去
result = addClientLayer(args, outResult.handle, layer, parent, &outTransformHint);
if (result != NO_ERROR) {
return result;
}
outResult.transformHint = static_cast<int32_t>(outTransformHint);
outResult.layerId = layer->sequence;
outResult.layerName = String16(layer->getDebugName());
return result;
}
关注点 1 处,通过工厂模式初始化一个 Layer 对象,实际跟进代码就是 new 一个 Layer 对象。
关注点 2 处,把刚 new 的 Layer 对象添加到 SurfaceFlinger 中,具体实现如下:
// /frameworks/native/services/surfaceflinger/SurfaceFlinger.h
std::vector<LayerCreatedState> mCreatedLayers GUARDED_BY(mCreatedLayersLock);
std::vector<std::unique_ptr<frontend::RequestedLayerState>> mNewLayers;
std::vector<LayerCreationArgs> mNewLayerArgs;
// /frameworks/native/services/surfaceflinger/SurfaceFlinger.cpp
status_t SurfaceFlinger::addClientLayer(LayerCreationArgs& args, const sp<IBinder>& handle,
const sp<Layer>& layer, const wp<Layer>& parent,
uint32_t* outTransformHint) {
if (mNumLayers >= MAX_LAYERS) { // Layer 过多的处理
// ......
}
layer->updateTransformHint(mActiveDisplayTransformHint);
if (outTransformHint) {
*outTransformHint = mActiveDisplayTransformHint;
}
args.parentId = LayerHandle::getLayerId(args.parentHandle.promote());
args.layerIdToMirror = LayerHandle::getLayerId(args.mirrorLayerHandle.promote());
{
std::scoped_lock<std::mutex> lock(mCreatedLayersLock);
// layer 相关信息放到这里,下次 VSync 信号到来,读取这些数据
mCreatedLayers.emplace_back(layer, parent, args.addToRoot);
// 创建 Layer 的参数
mNewLayers.emplace_back(std::make_unique<frontend::RequestedLayerState>(args));
args.mirrorLayerHandle.clear();
args.parentHandle.clear();
mNewLayerArgs.emplace_back(std::move(args));
}
setTransactionFlags(eTransactionNeeded); // 0x01
return NO_ERROR;
}
核心就是把创建的 Layer 以及创建过程中的参数保存到 SurfaceFlinger 的 mCreatedLayers mNewLayers mNewLayerArgs 三个成员中。
后续合成过程会使用到这三个参数来构建 Layer 树。
4.1.2 App 发起远程事务
App 示例代码中开启了一个事务:
// 构建事务对象并提交
SurfaceComposerClient::Transaction{}
.setLayer(surfaceControl, std::numeric_limits<int32_t>::max())
.show(surfaceControl)
.setBackgroundColor(surfaceControl, half3{0, 0, 0}, 1.0f, ui::Dataspace::UNKNOWN) // black background
.setAlpha(surfaceControl, 1.0f)
.setLayerStack(surfaceControl, ui::LayerStack::fromValue(mLayerStack))
.apply();
这里会先 new 一个 SurfaceComposerClient::Transaction 对象,Transaction 有一个重要的成员 mComposerStates:
class Transaction : public Parcelable {
// .....
std::unordered_map<sp<IBinder>, ComposerState, IBinderHash> mComposerStates;
std::unordered_map<sp<ITransactionCompletedListener>, CallbackInfo, TCLHash>
mListenerCallbacks;
// ......
}
mComposerStates 是一个 map,key 是 Layer 的 token, value 是 ComposerState,用于保存 Layer 相关的信息:
class ComposerState {
public:
layer_state_t state;
status_t write(Parcel& output) const;
status_t read(const Parcel& input);
};
layer_state_t 是一个结构体,用于在 SurfaceFlinger 与 app 之间交换 layer 相关的数据。
struct layer_state_t {
// ......
sp<IBinder> surface;
int32_t layerId;
uint64_t what;
float x;
float y;
int32_t z;
ui::LayerStack layerStack = ui::DEFAULT_LAYER_STACK;
uint32_t flags;
uint32_t mask;
uint8_t reserved;
matrix22_t matrix;
float cornerRadius;
uint32_t backgroundBlurRadius;
sp<SurfaceControl> relativeLayerSurfaceControl;
sp<SurfaceControl> parentSurfaceControlForChild;
half4 color;
// non POD must be last. see write/read
Region transparentRegion;
uint32_t bufferTransform;
bool transformToDisplayInverse;
Rect crop;
std::shared_ptr<BufferData> bufferData = nullptr;
ui::Dataspace dataspace;
HdrMetadata hdrMetadata;
Region surfaceDamageRegion;
int32_t api;
sp<NativeHandle> sidebandStream;
mat4 colorTransform;
std::vector<BlurRegion> blurRegions;
sp<gui::WindowInfoHandle> windowInfoHandle = sp<gui::WindowInfoHandle>::make();
LayerMetadata metadata;
// The following refer to the alpha, and dataspace, respectively of
// the background color layer
half4 bgColor;
ui::Dataspace bgColorDataspace;
// .....
}
先看 setLayer 的实现:
// /frameworks/native/libs/gui/SurfaceComposerClient.cpp
SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setLayer(
const sp<SurfaceControl>& sc, int32_t z) {
layer_state_t* s = getLayerState(sc);
if (!s) {
mStatus = BAD_INDEX;
return *this;
}
s->what |= layer_state_t::eLayerChanged;
s->what &= ~layer_state_t::eRelativeLayerChanged;
s->z = z;
registerSurfaceControlForCallback(sc);
return *this;
}
std::unordered_map<sp<IBinder>, ComposerState, IBinderHash> mComposerStates;
layer_state_t* SurfaceComposerClient::Transaction::getLayerState(const sp<SurfaceControl>& sc) {
// Layer 的 token
auto handle = sc->getLayerStateHandle();
if (mComposerStates.count(handle) == 0) {
// we don't have it, add an initialized layer_state to our list
ComposerState s;
s.state.surface = handle;
s.state.layerId = sc->getLayerId();
mComposerStates[handle] = s;
}
return &(mComposerStates[handle].state);
}
实际就是把配置信息写到 mComposerStates 的 layer_state_t state;
成员中。
setPosition 的实现:
SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setPosition(
const sp<SurfaceControl>& sc, float x, float y) {
layer_state_t* s = getLayerState(sc);
s->what |= layer_state_t::ePositionChanged;
s->x = x;
s->y = y;
return *this;
}
别的设置属性的实现基本都差不多,都是把数据存储到 layer_state_t 中,不再一一阐述。数据的关系如下图所示:
设置完一堆参数后,调用 apply 发起远程调用:
// 两个参数默认是 false
status_t SurfaceComposerClient::Transaction::apply(bool synchronous, bool oneWay) {
// ......
// 关键
// 主要数据在 composerStates 中
sf->setTransactionState(mFrameTimelineInfo, composerStates, displayStates, flags, applyToken,
mInputWindowCommands, mDesiredPresentTime, mIsAutoTimestamp,
mUncacheBuffers, hasListenerCallbacks, listenerCallbacks, mId,
// .....
return NO_ERROR;
}
这里 sf->setTransactionState 发起远程调用,最终会 Binder 远程调用到 SurfaceFlinger 的 setTransactionState:
// /frameworks/native/services/surfaceflinger/SurfaceFlinger.cpp
status_t SurfaceFlinger::setTransactionState(
const FrameTimelineInfo& frameTimelineInfo, Vector<ComposerState>& states,
const Vector<DisplayState>& displays, uint32_t flags, const sp<IBinder>& applyToken,
InputWindowCommands inputWindowCommands, int64_t desiredPresentTime, bool isAutoTimestamp,
const std::vector<client_cache_t>& uncacheBuffers, bool hasListenerCallbacks,
const std::vector<ListenerCallbacks>& listenerCallbacks, uint64_t transactionId,
const std::vector<uint64_t>& mergedTransactionIds) {
// ......
// 关注点1
// ComposerState 转换为 ResolvedComposerState
std::vector<ResolvedComposerState> resolvedStates;
resolvedStates.reserve(states.size());
for (auto& state : states) {
resolvedStates.emplace_back(std::move(state));
auto& resolvedState = resolvedStates.back();
if (resolvedState.state.hasBufferChanges() && resolvedState.state.hasValidBuffer() &&
resolvedState.state.surface) {
sp<Layer> layer = LayerHandle::getLayer(resolvedState.state.surface);
std::string layerName = (layer) ?
layer->getDebugName() : std::to_string(resolvedState.state.layerId);
resolvedState.externalTexture =
getExternalTextureFromBufferData(*resolvedState.state.bufferData,
layerName.c_str(), transactionId);
mBufferCountTracker.increment(resolvedState.state.surface->localBinder());
}
resolvedState.layerId = LayerHandle::getLayerId(resolvedState.state.surface);
if (resolvedState.state.what & layer_state_t::eReparent) {
resolvedState.parentId =
getLayerIdFromSurfaceControl(resolvedState.state.parentSurfaceControlForChild);
}
if (resolvedState.state.what & layer_state_t::eRelativeLayerChanged) {
resolvedState.relativeParentId =
getLayerIdFromSurfaceControl(resolvedState.state.relativeLayerSurfaceControl);
}
if (resolvedState.state.what & layer_state_t::eInputInfoChanged) {
wp<IBinder>& touchableRegionCropHandle =
resolvedState.state.windowInfoHandle->editInfo()->touchableRegionCropHandle;
resolvedState.touchCropId =
LayerHandle::getLayerId(touchableRegionCropHandle.promote());
}
}
// 关注点 2
// 构建一个 TransactionState
TransactionState state{frameTimelineInfo,
resolvedStates,
displays,
flags,
applyToken,
std::move(inputWindowCommands),
desiredPresentTime,
isAutoTimestamp,
std::move(uncacheBufferIds),
postTime,
hasListenerCallbacks,
listenerCallbacks,
originPid,
originUid,
transactionId,
mergedTransactionIds};
if (mTransactionTracing) {
mTransactionTracing->addQueuedTransaction(state);
}
const auto schedule = [](uint32_t flags) {
if (flags & eEarlyWakeupEnd) return TransactionSchedule::EarlyEnd;
if (flags & eEarlyWakeupStart) return TransactionSchedule::EarlyStart;
return TransactionSchedule::Late;
}(state.flags);
// 关注点3
const auto frameHint = state.isFrameActive() ? FrameHint::kActive : FrameHint::kNone;
mTransactionHandler.queueTransaction(std::move(state));
setTransactionFlags(eTransactionFlushNeeded, schedule, applyToken, frameHint);
return NO_ERROR;
}
关注点 1,将参数传入的 ComposerState 转换为 ResolvedComposerState 关注点 2,通过传入的参数构建一个 TransactionState 对象。 关注点 3,将 TransactionState 对象保存在 mTransactionHandler 的 mLocklessTransactionQueue 成员中。
看下 关注点3 的实现:
// /frameworks/native/services/surfaceflinger/FrontEnd/TransactionHandler.h
LocklessQueue<TransactionState> mLocklessTransactionQueue;
// /frameworks/native/services/surfaceflinger/FrontEnd/TransactionHandler.cpp
void TransactionHandler::queueTransaction(TransactionState&& state) {
mLocklessTransactionQueue.push(std::move(state));
mPendingTransactionCount.fetch_add(1);
ATRACE_INT("TransactionQueue", static_cast<int>(mPendingTransactionCount.load()));
}
小结:事务对象的成员经过简单的处理,构建为 TransactionState 对象并保存在 mLocklessTransactionQueue 中。
4.1.3 App 提交渲染好的 buffer
前面我们分析过,App 端渲染好 buffer 以后,会通过层层的回调调用到 acquireNextBufferLocked 函数:
status_t BLASTBufferQueue::acquireNextBufferLocked(
const std::optional<SurfaceComposerClient::Transaction*> transaction) {
//......
// 准备事务对象 Transaction
SurfaceComposerClient::Transaction localTransaction;
bool applyTransaction = true;
SurfaceComposerClient::Transaction* t = &localTransaction;
if (transaction) {
t = *transaction;
applyTransaction = false;
}
// 调用 acquireBuffer 函数 从队列中获取到一个 BufferItem 对象
BufferItem bufferItem;
// 关注点1
status_t status =
mBufferItemConsumer->acquireBuffer(&bufferItem, 0 /* expectedPresent */, false);
//......
auto buffer = bufferItem.mGraphicBuffer;
mNumFrameAvailable--;
// ......
mNumAcquired++;
mLastAcquiredFrameNumber = bufferItem.mFrameNumber;
ReleaseCallbackId releaseCallbackId(buffer->getId(), mLastAcquiredFrameNumber);
mSubmitted[releaseCallbackId] = bufferItem;
bool needsDisconnect = false;
mBufferItemConsumer->getConnectionEvents(bufferItem.mFrameNumber, &needsDisconnect);
// if producer disconnected before, notify SurfaceFlinger
if (needsDisconnect) {
t->notifyProducerDisconnect(mSurfaceControl);
}
// Ensure BLASTBufferQueue stays alive until we receive the transaction complete callback.
incStrong((void*)transactionCallbackThunk);
// Only update mSize for destination bounds if the incoming buffer matches the requested size.
// Otherwise, it could cause stretching since the destination bounds will update before the
// buffer with the new size is acquired.
if (mRequestedSize == getBufferSize(bufferItem) ||
bufferItem.mScalingMode != NATIVE_WINDOW_SCALING_MODE_FREEZE) {
mSize = mRequestedSize;
}
Rect crop = computeCrop(bufferItem);
mLastBufferInfo.update(true /* hasBuffer */, bufferItem.mGraphicBuffer->getWidth(),
bufferItem.mGraphicBuffer->getHeight(), bufferItem.mTransform,
bufferItem.mScalingMode, crop);
// 构造一个回调对象
auto releaseBufferCallback =
std::bind(releaseBufferCallbackThunk, wp<BLASTBufferQueue>(this) /* callbackContext */,
std::placeholders::_1, std::placeholders::_2, std::placeholders::_3);
// 这里会同时拿到 bufferItem 中的 fence 对象
sp<Fence> fence = bufferItem.mFence ? new Fence(bufferItem.mFence->dup()) : Fence::NO_FENCE;
// t 是准备提交给 SurfaceFlinger 的事务对象
// 根据 bufferItem 来配置
// 关注点2
// 注意这里传入了回调对象
t->setBuffer(mSurfaceControl, buffer, fence, bufferItem.mFrameNumber, mProducerId,
releaseBufferCallback);
t->setDataspace(mSurfaceControl, static_cast<ui::Dataspace>(bufferItem.mDataSpace));
t->setHdrMetadata(mSurfaceControl, bufferItem.mHdrMetadata);
t->setSurfaceDamageRegion(mSurfaceControl, bufferItem.mSurfaceDamage);
t->addTransactionCompletedCallback(transactionCallbackThunk, static_cast<void*>(this));
mSurfaceControlsWithPendingCallback.push(mSurfaceControl);
if (mUpdateDestinationFrame) {
t->setDestinationFrame(mSurfaceControl, Rect(mSize));
} else {
const bool ignoreDestinationFrame =
bufferItem.mScalingMode == NATIVE_WINDOW_SCALING_MODE_FREEZE;
t->setFlags(mSurfaceControl,
ignoreDestinationFrame ? layer_state_t::eIgnoreDestinationFrame : 0,
layer_state_t::eIgnoreDestinationFrame);
}
t->setBufferCrop(mSurfaceControl, crop);
t->setTransform(mSurfaceControl, bufferItem.mTransform);
t->setTransformToDisplayInverse(mSurfaceControl, bufferItem.mTransformToDisplayInverse);
t->setAutoRefresh(mSurfaceControl, bufferItem.mAutoRefresh);
if (!bufferItem.mIsAutoTimestamp) {
t->setDesiredPresentTime(bufferItem.mTimestamp);
}
// Drop stale frame timeline infos
while (!mPendingFrameTimelines.empty() &&
mPendingFrameTimelines.front().first < bufferItem.mFrameNumber) {
ATRACE_FORMAT_INSTANT("dropping stale frameNumber: %" PRIu64 " vsyncId: %" PRId64,
mPendingFrameTimelines.front().first,
mPendingFrameTimelines.front().second.vsyncId);
mPendingFrameTimelines.pop();
}
if (!mPendingFrameTimelines.empty() &&
mPendingFrameTimelines.front().first == bufferItem.mFrameNumber) {
ATRACE_FORMAT_INSTANT("Transaction::setFrameTimelineInfo frameNumber: %" PRIu64
" vsyncId: %" PRId64,
bufferItem.mFrameNumber,
mPendingFrameTimelines.front().second.vsyncId);
t->setFrameTimelineInfo(mPendingFrameTimelines.front().second);
mPendingFrameTimelines.pop();
}
{
std::lock_guard _lock{mTimestampMutex};
auto dequeueTime = mDequeueTimestamps.find(buffer->getId());
if (dequeueTime != mDequeueTimestamps.end()) {
Parcel p;
p.writeInt64(dequeueTime->second);
t->setMetadata(mSurfaceControl, gui::METADATA_DEQUEUE_TIME, p);
mDequeueTimestamps.erase(dequeueTime);
}
}
mergePendingTransactions(t, bufferItem.mFrameNumber);
if (applyTransaction) { // 进入分支
// All transactions on our apply token are one-way. See comment on mAppliedLastTransaction
// 关注点 3
// 提交事务
t->setApplyToken(mApplyToken).apply(false, true);
mAppliedLastTransaction = true;
mLastAppliedFrameNumber = bufferItem.mFrameNumber;
} else {
// 设置 barrier
t->setBufferHasBarrier(mSurfaceControl, mLastAppliedFrameNumber);
mAppliedLastTransaction = false;
}
BQA_LOGV("acquireNextBufferLocked size=%dx%d mFrameNumber=%" PRIu64
" applyTransaction=%s mTimestamp=%" PRId64 "%s mPendingTransactions.size=%d"
" graphicBufferId=%" PRIu64 "%s transform=%d",
mSize.width, mSize.height, bufferItem.mFrameNumber, boolToString(applyTransaction),
bufferItem.mTimestamp, bufferItem.mIsAutoTimestamp ? "(auto)" : "",
static_cast<uint32_t>(mPendingTransactions.size()), bufferItem.mGraphicBuffer->getId(),
bufferItem.mAutoRefresh ? " mAutoRefresh" : "", bufferItem.mTransform);
return OK;
}
关注点 1,调用 acquireBuffer 获取到 BufferItem 关注点 2,构建事务对象 Transaction 关注点 3,apply 提交事务对象
apply 部分和上一小节一样,提交的事务对象最终会转换为 TransactionState 对象并保存在 mLocklessTransactionQueue 中。流程都是一样的,就不再分析了。差异主要是在构建的 Transaction 对象上。
4.2 commit 处理数据过程分析
commit 执行之前,以下的数据们都准备好了:
SurfaceFlinger 对象中的 LayerCreatedState 成员:
std::vector<LayerCreatedState> mCreatedLayers
SurfaceFlinger 中有一个成员 TransactionHandler,它有一个 mLocklessTransactionQueue 成员:
LocklessQueue<TransactionState> mLocklessTransactionQueue;
提交的配置事务,渲染好的 buffer 都保存在里面。
接下来我们来看 commit 的具体实现:
bool SurfaceFlinger::commit(TimePoint frameTime, VsyncId vsyncId, TimePoint expectedVsyncTime)
FTL_FAKE_GUARD(kMainThreadContext) {
// ......
bool mustComposite = mMustComposite.exchange(false);
{
mFrameTimeline->setSfWakeUp(vsyncId.value, frameTime.ns(),
Fps::fromPeriodNsecs(vsyncPeriod.ns()));
const bool flushTransactions = clearTransactionFlags(eTransactionFlushNeeded);
// 预处理后的信息放到一个结构体 Update 中
frontend::Update updates;
if (flushTransactions) {
// 关注点1
updates = flushLifecycleUpdates();
//tracing
}
bool transactionsAreEmpty;
// mLegacyFrontEndEnabled和mLayerLifecycleManagerEnabled只有一个为ture,
// 应该是两套算法逻辑,会根据一个系统属性来决定,都是更新layer中的Snapshot的
// 因为我看我的手机的属性应该是走的mLegacyFrontEndEnabled true的逻辑,我们这里就看这条线
if (mLegacyFrontEndEnabled) {
// 关注点 2
mustComposite |= updateLayerSnapshotsLegacy(vsyncId, updates, flushTransactions,
transactionsAreEmpty);
}
if (mLayerLifecycleManagerEnabled) {
mustComposite |=
updateLayerSnapshots(vsyncId, updates, flushTransactions, transactionsAreEmpty);
}
if (transactionFlushNeeded()) {
setTransactionFlags(eTransactionFlushNeeded);
}
// 回调通知,在releasePendingBuffer时候,会addCallbackHandles,然后这里会回调client侧,然后client进行BufferRelease
if (transactionsAreEmpty) {
mTransactionCallbackInvoker.sendCallbacks(false /* onCommitOnly */);
} else {
mTransactionCallbackInvoker.sendCallbacks(true /* onCommitOnly */);
}
}
// 处理刷新率更新
{
Mutex::Autolock lock(mStateLock);
mScheduler->chooseRefreshRateForContent();
setActiveModeInHwcIfNeeded();
}
updateCursorAsync();
// 更新inputFlinger focused窗口
updateInputFlinger(vsyncId, frameTime);
// 。。。
persistDisplayBrightness(mustComposite);
// 返回是否需要合成
return mustComposite && CC_LIKELY(mBootStage != BootStage::BOOTLOADER);
}
代码非常多,我们主要关注两点:
- 关注点 1, flushLifecycleUpdates:Layer 相关数据收集
- 关注点2,updateLayerSnapshotsLegacy:Layer 相关数据基本处理
4.2.1 flushLifecycleUpdates 数据处理过程分析
flushLifecycleUpdates 的实现如下:
struct Update {
std::vector<TransactionState> transactions;
std::vector<LayerCreatedState> layerCreatedStates;
std::vector<std::unique_ptr<frontend::RequestedLayerState>> newLayers;
std::vector<LayerCreationArgs> layerCreationArgs;
std::vector<uint32_t> destroyedHandles;
};
frontend::Update SurfaceFlinger::flushLifecycleUpdates() {
frontend::Update update;
// 处理之前 setTransactionState 放入队列的任务
// 会把所有 Ready 状态的 TransactionState 返回回来,然后存在 update.transactions
update.transactions = mTransactionHandler.flushTransactions();
{
// 当client侧通过binder请求SurfaceFlinger,SurfaceFlinger会将参数放到mNewLayers,mCreatedLayers里
// 这里会把参数都提取出来,全部都放到Update里面
std::scoped_lock<std::mutex> lock(mCreatedLayersLock);
update.layerCreatedStates = std::move(mCreatedLayers);
mCreatedLayers.clear();
update.newLayers = std::move(mNewLayers);
mNewLayers.clear();
update.layerCreationArgs = std::move(mNewLayerArgs);
mNewLayerArgs.clear();
update.destroyedHandles = std::move(mDestroyedHandles);
mDestroyedHandles.clear();
}
return update;
}
- flushTransactions 用于过滤 mLocklessTransactionQueue 中的数据
- 接着把 mCreatedLayers mNewLayers mNewLayerArgs mDestroyedHandles 这些成员保存到 Update 对象中,这些数据主要来自 App 端,Surface 的添加或者删除
接下来来看 flushTransactions() 如何过滤数据:
LocklessQueue<TransactionState> mLocklessTransactionQueue;
std::unordered_map<sp<IBinder>, std::queue<TransactionState>, IListenerHash> mPendingTransactionQueues;
std::vector<TransactionState> TransactionHandler::flushTransactions() {
// 关注点1
// 第一次筛选 mLocklessTransactionQueue -> mPendingTransactionQueues
while (!mLocklessTransactionQueue.isEmpty()) {
// 之前讲 Transaction 到章节最后我们提到,Transaction 里面的内容被封装成 TransactionState,通过 queueTransaction 放到队列中
// 就是放在了 mLocklessTransactionQueue 中
// 这里取出来的就是 TransactionState
auto maybeTransaction = mLocklessTransactionQueue.pop();
// 过滤异常的 State
if (!maybeTransaction.has_value()) {
break;
}
auto transaction = maybeTransaction.value();
// 把有效的 TransactionState 放到 mPendingTransactionQueues 中。
mPendingTransactionQueues[transaction.applyToken].emplace(std::move(transaction));
}
std::vector<TransactionState> transactions;
TransactionFlushState flushState;
flushState.queueProcessTime = systemTime();
// 循环执行 flushPendingTransactionQueues,这里需要循环的原因上面已经说了,主要是处理有 barrier 的 Transaction。
int lastTransactionsPendingBarrier = 0;
int transactionsPendingBarrier = 0;
// 关注点2
// 第二次筛选 mPendingTransactionQueues -> transactions
// 直到两次 flushPendingTransactionQueues 的返回值相同才跳出循环
do {
// 这里比较两次flushPendingTransactionQueues后由于barrier未就绪的Transaction个数,如果一样说明不需要再继续执行flushPendingTransactionQueues
lastTransactionsPendingBarrier = transactionsPendingBarrier;
// 会把所有ready状态的TransactionState存储到transactions
transactionsPendingBarrier = flushPendingTransactionQueues(transactions, flushState);
} while (lastTransactionsPendingBarrier != transactionsPendingBarrier);
applyUnsignaledBufferTransaction(transactions, flushState);
mPendingTransactionCount.fetch_sub(transactions.size());
ATRACE_INT("TransactionQueue", static_cast<int>(mPendingTransactionCount.load()));
return transactions;
}
- 关注点 1,遍历 mLocklessTransactionQueue 中保存的 TransactionState,剔除没有数据的,剩余的数据保存到 mPendingTransactionQueues 中
- 关注点 2,调用 flushPendingTransactionQueues,进一步筛选 TransactionState,筛选后符合要求的数据会被保存在参数 transactions 中。
关注点 2 处比较特别的是,循环调用了 flushPendingTransactionQueues,flushPendingTransactionQueues 本身就会遍历所有的 TransactionState,这里之所以还要加一个循环是因为有屏障逻辑。是因为在 acquireNextBufferLocked 的时候如果没有立刻将 Transaction 传给 SurfaceFlinger 的情况,就会设置一个 barrier,表示新的这个 Transaction 的处理必须要依赖上一个 Transaction 的完成。
status_t BLASTBufferQueue::acquireNextBufferLocked(
const std::optional<SurfaceComposerClient::Transaction*> transaction) {
//......
// 准备事务对象 Transaction
SurfaceComposerClient::Transaction localTransaction;
bool applyTransaction = true;
SurfaceComposerClient::Transaction* t = &localTransaction;
if (transaction) {
t = *transaction;
applyTransaction = false;
}
// ......
if (applyTransaction) {
t->setApplyToken(mApplyToken).apply(false, true);
mAppliedLastTransaction = true;
mLastAppliedFrameNumber = bufferItem.mFrameNumber;
} else { // 参数 transaction 不为空
// 关注点:设置 barrier,且没有 apply
t->setBufferHasBarrier(mSurfaceControl, mLastAppliedFrameNumber);
mAppliedLastTransaction = false;
}
BQA_LOGV("acquireNextBufferLocked size=%dx%d mFrameNumber=%" PRIu64
" applyTransaction=%s mTimestamp=%" PRId64 "%s mPendingTransactions.size=%d"
" graphicBufferId=%" PRIu64 "%s transform=%d",
mSize.width, mSize.height, bufferItem.mFrameNumber, boolToString(applyTransaction),
bufferItem.mTimestamp, bufferItem.mIsAutoTimestamp ? "(auto)" : "",
static_cast<uint32_t>(mPendingTransactions.size()), bufferItem.mGraphicBuffer->getId(),
bufferItem.mAutoRefresh ? " mAutoRefresh" : "", bufferItem.mTransform);
return OK;
}
接着我们来看 flushPendingTransactionQueues 的实现:
int TransactionHandler::flushPendingTransactionQueues(std::vector<TransactionState>& transactions, TransactionFlushState& flushState) {
int transactionsPendingBarrier = 0;
// 遍历所有 TransactionState
auto it = mPendingTransactionQueues.begin();
while (it != mPendingTransactionQueues.end()) {
auto& [applyToken, queue] = *it;
while (!queue.empty()) {
auto& transaction = queue.front();
flushState.transaction = &transaction;
// 判断Transaction当前是否ready,比如有barrier的,依赖的前置Transaction是否已经执行了
// 或者是期望的时间还没有到
auto ready = applyFilters(flushState);
if (ready == TransactionReadiness::NotReadyBarrier) {
// 统计barrier没有就绪的个数,如果两次循环执行flushPendingTransactionQueues这个数量未改变,
// 就不用再继续循环了,需要等下一帧再处理
transactionsPendingBarrier++;
break;
} else if (ready == TransactionReadiness::NotReady) {
break;
} else if (ready == TransactionReadiness::NotReadyUnsignaled) {
flushState.queueWithUnsignaledBuffer = applyToken;
break;
}
// 走到这的Transaction就是已经处于ready状态了
popTransactionFromPending(transactions, flushState, queue);
}
if (queue.empty()) {
it = mPendingTransactionQueues.erase(it);
} else {
it = std::next(it, 1);
}
}
// 返回由于barrier未就绪导致未处理的Transcaion个数
return transactionsPendingBarrier;
}
通过 applyFilters 逐个判断 TrasactionState 是否 ready,主要是判断两个点:
1.时间,通过 SurfaceFlinger::transactionReadyTimelineCheck 判断 2.barrier 是否就绪,通过 SurfaceFlinger::transactionReadyBufferCheck 判断
applyFilters:
TransactionHandler::TransactionReadiness TransactionHandler::applyFilters(
TransactionFlushState& flushState) {
auto ready = TransactionReadiness::Ready;
for (auto& filter : mTransactionReadyFilters) {
auto perFilterReady = filter(flushState);
switch (perFilterReady) {
case TransactionReadiness::NotReady:
case TransactionReadiness::NotReadyBarrier:
return perFilterReady;
case TransactionReadiness::NotReadyUnsignaled:
// If one of the filters allows latching an unsignaled buffer, latch this ready
// state.
ready = perFilterReady;
break;
case TransactionReadiness::Ready:
continue;
}
}
return ready;
}
mTransactionReadyFilters 什么时候初始化?
// /frameworks/native/services/surfaceflinger/SurfaceFlinger.cpp
void SurfaceFlinger::init() FTL_FAKE_GUARD(kMainThreadContext) {
ALOGI( "SurfaceFlinger's main thread ready to run. "
"Initializing graphics H/W...");
addTransactionReadyFilters();
Mutex::Autolock lock(mStateLock);
//......
}
void SurfaceFlinger::addTransactionReadyFilters() {
mTransactionHandler.addTransactionReadyFilter(
std::bind(&SurfaceFlinger::transactionReadyTimelineCheck, this, std::placeholders::_1));
mTransactionHandler.addTransactionReadyFilter(
std::bind(&SurfaceFlinger::transactionReadyBufferCheck, this, std::placeholders::_1));
}
// /frameworks/native/services/surfaceflinger/FrontEnd/TransactionHandler.cpp
void TransactionHandler::addTransactionReadyFilter(TransactionFilter&& filter) {
mTransactionReadyFilters.emplace_back(std::move(filter));
}
所以这里有两个回调是 SurfaceFlinger::transactionReadyTimelineCheck
和 SurfaceFlinger::transactionReadyBufferCheck
。
经过两个回调的处理,TransactionState 就符合要求了,接着调用 popTransactionFromPending 将 ready 的 TransactionState 存到 transactions 集合中。
void TransactionHandler::popTransactionFromPending(std::vector<TransactionState>& transactions,
TransactionFlushState& flushState,
std::queue<TransactionState>& queue) {
auto& transaction = queue.front();
// Transaction is ready move it from the pending queue.
flushState.firstTransaction = false;
removeFromStalledTransactions(transaction.id);
// 将TransactionState加到transactions队列里。
transactions.emplace_back(std::move(transaction));
queue.pop();
auto& readyToApplyTransaction = transactions.back();
readyToApplyTransaction.traverseStatesWithBuffers([&](const layer_state_t& state) {
const bool frameNumberChanged =
state.bufferData->flags.test(BufferData::BufferDataChange::frameNumberChanged);
// bufferLayersReadyToPresent是一个map,key是窗口的handle,value是帧号。
if (frameNumberChanged) {
flushState.bufferLayersReadyToPresent.emplace_or_replace(state.surface.get(),
state.bufferData->frameNumber);
} else {
flushState.bufferLayersReadyToPresent
.emplace_or_replace(state.surface.get(), std::numeric_limits<uint64_t>::max());
}
});
}
层层返回以后,最后符合要求的 TransactionState 都保存在了 update.transactions 中。