显示设备初始化
# 深入理解 SurfaceFlinger —— 显示设备初始化
SurfaceFlinger 中从多个方面对显示设备进行了抽象以方便不同场景下对显示设备的管理和操作:
- HWC2::Display,用于与 HWC HAL 进行通信
- PhysicalDisplay 用于记录从 HWC HAL 获取到的显示设备信息,描述了显示设备支持的所有能力
- DisplayDeviceState 用于记录显示设备当前的状态
- DisplayModeController::Display, 显示模式配置过程中显示设备的抽象
- compositionengine::Display(Output),Layer 合成过程,显示设备的抽象
- android::scheduler::Scheduler::Display,用于表示和管理单个物理显示设备的调度相关信息
- DisplayDevice,SurfaceFlinger 中用于表示和管理显示设备的核心类,收集了各方信息,上层显示相关的调用都是以找到对应的 DisplayDevice 对象为起点
- Scheduler::Display,用于表示和管理单个物理显示设备的调度相关信息,包含了显示设备的刷新率、当前刷新率、当前模式等信息,这货过于复杂,下一篇单独说
整体类关系图可以查看: https://boardmix.cn/app/share/CAE.CMLx1wwgASoQUKJdPfT_sYgzSx5tiYHtmjAGQAE/hxtUlY
建议 配合着类图看,不然容易懵
# 1. 显示设备初始化整体流程
Android 系统启动过程中,在 SurfaceFlinger 进程启动过程中,会执行到 SurfaceFlinger::init 函数,在这个函数中会向 HWC HAL 注册一个回调,用于接受显示设备相关事件回调。
// frameworks/native/services/surfaceflinger/SurfaceFlinger.h
std::atomic<PhysicalDisplayId> mActiveDisplayId;
// frameworks/native/services/SurfaceFlinger/SurfaceFlinger.cpp
void SurfaceFlinger::init() FTL_FAKE_GUARD(kMainThreadContext) {
// ......
mCompositionEngine->setHwComposer(getFactory().createHWComposer(mHwcServiceName));
auto& composer = mCompositionEngine->getHwComposer();
// 关注点1,对应第 2 节,HWC2::Display、PhysicalDisplay 对象初始化
// 向 HWC Hal 注册一个回调
// 回调对象是 SurfaceFlinger 对象自己
composer.setCallback(*this);
// ......
// 调用 configureLocked
// Process hotplug for displays connected at boot.
LOG_ALWAYS_FATAL_IF(!configureLocked(),
"Initial display configuration failed: HWC did not hotplug");
// ......
// Commit primary display.
sp<const DisplayDevice> display;
// mCurrentState 中的数据什么时候赋值的?
// mCurrentState 与 mDrawingState 之间的关系
if (const auto indexOpt = mCurrentState.getDisplayIndex(mActiveDisplayId)) {
// 拿到 DisplayDeviceState 对象
const auto& displays = mCurrentState.displays;
const auto& token = displays.keyAt(*indexOpt);
const auto& state = displays.valueAt(*indexOpt);
// 关注点2 对应第 3 节
// compositionengine::Display DisplayDevice 初始化
processDisplayAdded(token, state);
mDrawingState.displays.add(token, state);
display = getDefaultDisplayDeviceLocked();
}
// ......
// mScheduler 初始化,下一节分析
initScheduler(display);
// ......
}
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
- 关注点1,对应第 2 节,注册回调时,会主动触发一次热插拔事件 onComposerHalHotplugEvent() 回调,完成
HWC2::Display、PhysicalDisplay、 DisplayDeviceState、DisplayModeController::Display对象的初始化 - 关注点2,对应第 3 节,SurfaceFlinger::processDisplayAdded 函数依次完成
compositionengine::Display、Scheduler::Display、DisplayDevice对象的初始化
整体的调用栈如下:
SurfaceFlinger::init()
RenderEngine::create()
createHWComposer()
HwComposer::setCallback
SurfaceFlinger::onComposerHalHotplugEvent // 首次注册触发回调,Binder线程,异步过程
SurfaceFlinger::mPendingHotplugEvents.push_back // 插入新的 HotplugEvent
Scheduler::scheduleConfigure // mScheduler 不为 nullptr 的情况
SurfaceFlinger::configure() // post 到主线程调用
SurfaceFlinger::configureLocked() // 第一次调用 configureLocked,消费 mPendingHotplugEvents
SurfaceFlinger::configureLocked() // 第二次调用
SurfaceFlinger::processDisplayAdded
CompositionEngine::createDisplay // 创建 CompositionEngine::Display 对象
getFactory().createBufferQueue // GPU 合成的 bufferqueue 准备
sp<FramebufferSurface>::make
SurfaceFlinger::setupNewDisplayDeviceInternal // 构建 DisplayDevice 对象
2
3
4
5
6
7
8
9
10
11
12
13
14
15
SurfaceFlinger::configureLocked
HwComposer::onHotplug // 初始化 HWC2::Display
HWComposer::onHotplugConnect
HWComposer::allocatePhysicalDisplay
SurfaceFlinger::processHotplugConnect // 初始化 PhysicalDisplay 对象,DisplayDeviceState 对象
DisplayModeController::registerDisplay // 初始化 DisplayModeController::Display 对象
2
3
4
5
6
# 2. HWC 回调过程分析
HwComposer.setCallback 实现如下:
// frameworks/native/services/surfaceflinger/DisplayHardware/HWComposer.h
std::unordered_set<aidl::android::hardware::graphics::composer3::Capability> mCapabilities;
std::unordered_map<std::string, bool> mSupportedLayerGenericMetadata;
aidl::android::hardware::graphics::composer3::OverlayProperties mOverlayProperties;
std::vector<aidl::android::hardware::graphics::common::HdrConversionCapability>
mHdrConversionCapabilities = {};
// frameworks/native/services/surfaceflinger/DisplayHardware/HWComposer.cpp
void HWComposer::setCallback(HWC2::ComposerCallback& callback) {
// 从 HWC 加载 HWC 所支持的能力,结果保存在 HWComposer::mCapabilities 成员中
loadCapabilities();
// 从 HWC 加载 HWC 支持的图层属性和特性,结果保存在 HWComposer::mSupportedLayerGenericMetadata 成员中
loadLayerMetadataSupport();
// 从 HWC 加载 HWC 支持的覆盖层(Overlay)属性和能力,结果保存在 HWComposer::mOverlayProperties 成员中
loadOverlayProperties();
// 从 HWC 加载 HWC 支持的 HDR 转换能力,结果保存在 HWComposer::mHdrConversionCapabilities 成员中
loadHdrConversionCapabilities();
// 只能注册一次
if (mRegisteredCallback) {
ALOGW("Callback already registered. Ignored extra registration attempt.");
return;
}
mRegisteredCallback = true;
// 向 HWC HAL 注册回调
mComposer->registerCallback(callback);
}
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
setCallback 函数传入的参数类型是 ComposerCallback,对应的实现类是 SurfaceFlinger:
// frameworks/native/services/SurfaceFlinger/DisplayHardware/HWC2.h
// 首次注册回调时,会主动触发一次热插拔事件 onComposerHalHotplugEvent () 回调
// Implement this interface to receive hardware composer events.
//
// These callback functions will generally be called on a hwbinder thread, but
// when first registering the callback the onComposerHalHotplugEvent() function
// will immediately be called on the thread calling registerCallback().
struct ComposerCallback {
// 发生热插拔事件时的回调
virtual void onComposerHalHotplugEvent(hal::HWDisplayId, DisplayHotplugEvent) = 0;
// 发生刷新率变化时回调
virtual void onComposerHalRefresh(hal::HWDisplayId) = 0;
// 发生硬件 VSYNC 信号时回调
virtual void onComposerHalVsync(hal::HWDisplayId, nsecs_t timestamp,
std::optional<hal::VsyncPeriodNanos>) = 0;
// 硬件 VSYNC 周期变化时回调
virtual void onComposerHalVsyncPeriodTimingChanged(hal::HWDisplayId,
const hal::VsyncPeriodChangeTimeline&) = 0;
virtual void onComposerHalSeamlessPossible(hal::HWDisplayId) = 0;
// 进入 IDLE 状态时回调
virtual void onComposerHalVsyncIdle(hal::HWDisplayId) = 0;
// 刷新率变化时回调,需要 HWC 支持该功能
virtual void onRefreshRateChangedDebug(const RefreshRateChangedDebugData&) = 0;
virtual void onComposerHalHdcpLevelsChanged(hal::HWDisplayId, const HdcpLevels& levels) = 0;
protected:
~ComposerCallback() = default;
};
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
注释中说明了,首次注册回调时,会主动触发一次热插拔事件 onComposerHalHotplugEvent() 回调,SurfaceFlinger 中将利用这次热插拔事件来完成默认屏的加载
// /frameworks/native/services/surfaceflinger/SurfaceFlinger.h
std::vector<HotplugEvent> mPendingHotplugEvents GUARDED_BY(mHotplugMutex);
// frameworks/native/services/SurfaceFlinger/SurfaceFlinger.cpp
void SurfaceFlinger::onComposerHalHotplugEvent(hal::HWDisplayId hwcDisplayId,
DisplayHotplugEvent event) {
if (event == DisplayHotplugEvent::CONNECTED || event == DisplayHotplugEvent::DISCONNECTED) {
hal::Connection connection = (event == DisplayHotplugEvent::CONNECTED)
? hal::Connection::CONNECTED
: hal::Connection::DISCONNECTED;
{
// mPendingHotplugEvents 队列受 mHotplugMutex 保护
std::lock_guard<std::mutex> lock(mHotplugMutex);
// 将 hotplug 事件保存在 mPendingHotplugEvents 中
mPendingHotplugEvents.push_back(HotplugEvent{hwcDisplayId, connection});
}
// mScheduler 指针在主线程初始化
// onComposerHalHotplugEvent 在 binder 线程执行,和主线程是异步的
// mScheduler 可能已经初始化完成,也可能未初始化完成,为 nullptr
// 但是无论哪种情况都会执行到 SurfaceFlinger::configureLocked()
if (mScheduler) {
mScheduler->scheduleConfigure();
}
// ......
return;
}
// .......
}
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
该回调中,HWC HAL 向 SurfaceFlinger 返回了两个数据:
- hal::HWDisplayId hwcDisplayId:HWC HAL 中的屏幕 ID
- DisplayHotplugEvent event:屏幕的链接状态
其中 DisplayHotplugEvent 的定义如下:
enum class DisplayHotplugEvent : int32_t {
// 屏幕连接
CONNECTED = 0,
// 屏幕断开
DISCONNECTED = 1,
ERROR_UNKNOWN = -1,
ERROR_INCOMPATIBLE_CABLE = -2,
ERROR_TOO_MANY_DISPLAYS = -3,
ERROR_LINK_UNSTABLE = -4,
};
2
3
4
5
6
7
8
9
10
- onComposerHalHotplugEvent 函数中会构建一个 HotplugEvent 结构体,插入到 mPendingHotplugEvents 队列中。
- mScheduler 指针在主线程初始化,onComposerHalHotplugEvent 在 binder 线程执行,和主线程是异步的,mScheduler 可能已经初始化完成,也可能为 nullptr。
情况一,如果 mScheduler 此时已初始化完成,函数都会调用 mScheduler->scheduleConfigure() 来安排配置更新,确保显示系统能及时响应硬件变化。
scheduleConfigure 实现在 Scheduler 的父类 MessageQueue 中:
// /frameworks/native/services/surfaceflinger/Scheduler/MessageQueue.cpp
void MessageQueue::scheduleConfigure() {
struct ConfigureHandler : MessageHandler {
explicit ConfigureHandler(ICompositor& compositor) : compositor(compositor) {}
// Looper 中会调用到这个函数
void handleMessage(const Message&) override { compositor.configure(); }
ICompositor& compositor;
};
// mCompositor 就是 SurfaceFligner 对象
// TODO(b/241285876): Batch configure tasks that happen within some duration.
postMessage(sp<ConfigureHandler>::make(mCompositor));
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
Scheduler 是 SurfaceFlinger 中的调度器,继承自 MessageQueue 类,并封装了 Looper 相关的操作。这里就是简单向 Looper post 一个 Message,Looper 遍历到该 Message 时,会执行到 ConfigureHandler::handleMessage 回调函数。handleMessage 中的 mCompositor 就是 SurfaceFligner 对象,所以这里会调用到 SurfaceFlinger::configure() 函数:
void SurfaceFlinger::configure() {
Mutex::Autolock lock(mStateLock);
if (configureLocked()) {
// eDisplayTransactionNeeded 是一个位标志,值为 0x04 ,当设置 eDisplayTransactionNeeded 标志时,表示显示配置发生了变化,需要在下一个合成周期处理显示相关的事务
setTransactionFlags(eDisplayTransactionNeeded); // eDisplayTransactionNeeded = 0x04
}
}
2
3
4
5
6
7
最终会调用到 configureLocked 来处理显示相关的配置变更。
小结:mScheduler 不为空的情况下,最终会调用到 SurfaceFlinger::configureLocked() 函数来处理新的屏幕热插拔事件,整体的调用栈如下:
SurfaceFlinger::init()
HwComposer.setCallback(this);
SurfaceFlinger::onComposerHalHotplugEvent // Binder 线程异步调用
mPendingHotplugEvents.push_back // 插入新的 HotplugEvent
Scheduler::scheduleConfigure // mScheduler 不为 nullptr 的情况
SurfaceFlinger::configure() // post 到主线程调用
SurfaceFlinger::configureLocked()
SurfaceFlinger::configureLocked() // 后续会二次调用 configureLocked
2
3
4
5
6
7
8
情况二,如果 SurfaceFlinger::onComposerHalHotplugEvent 中,mScheduler 为 nullptr,会发生什么?我们从 SurfaceFlinger::init() 开始再跟踪一次整体流程:
// frameworks/native/services/SurfaceFlinger/SurfaceFlinger.cpp
void SurfaceFlinger::init() FTL_FAKE_GUARD(kMainThreadContext) {
// ......
mCompositionEngine->setHwComposer(getFactory().createHWComposer(mHwcServiceName));
auto& composer = mCompositionEngine->getHwComposer();
// 关注点
// 向 HWC Hal 注册一个回调
// 回调对象是 SurfaceFlinger 对象自己
composer.setCallback(*this);
// ......
// 关注点,执行 configureLocked 函数
// Process hotplug for displays connected at boot.
LOG_ALWAYS_FATAL_IF(!configureLocked(),
"Initial display configuration failed: HWC did not hotplug");
// ....
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
composer.setCallback 首次注册过程,会回调到 SurfaceFlinger::onComposerHalHotplugEvent:
// /frameworks/native/services/surfaceflinger/SurfaceFlinger.h
std::vector<HotplugEvent> mPendingHotplugEvents GUARDED_BY(mHotplugMutex);
// frameworks/native/services/SurfaceFlinger/SurfaceFlinger.cpp
void SurfaceFlinger::onComposerHalHotplugEvent(hal::HWDisplayId hwcDisplayId,
DisplayHotplugEvent event) {
if (event == DisplayHotplugEvent::CONNECTED || event == DisplayHotplugEvent::DISCONNECTED) {
hal::Connection connection = (event == DisplayHotplugEvent::CONNECTED)
? hal::Connection::CONNECTED
: hal::Connection::DISCONNECTED;
{
// mPendingHotplugEvents 队列受 mHotplugMutex 保护
std::lock_guard<std::mutex> lock(mHotplugMutex);
// 将 hotplug 事件保存在 mPendingHotplugEvents 中
mPendingHotplugEvents.push_back(HotplugEvent{hwcDisplayId, connection});
}
if (mScheduler) { // 此时预设 mScheduler 为 null,不执行后续操作
mScheduler->scheduleConfigure();
}
// ......
return;
}
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
此时 mScheduler 为 nullptr,onComposerHalHotplugEvent 中不会执行 scheduleConfigure,但是在 SurfaceFlinger::init() 中,后续会调用一次 configureLocked 函数,整体的调用栈如下:
SurfaceFlinger::init()
HwComposer.setCallback(this);
SurfaceFlinger::onComposerHalHotplugEvent // Binder 线程异步调用
mPendingHotplugEvents.push_back // 插入新的 HotplugEvent
SurfaceFlinger::configureLocked() // 后续仍会调用到 configureLocked
2
3
4
5
可以看出,无论 mScheduler 是否为 nullptr,都会执行到 SurfaceFlinger::configureLocked() 函数,其实现如下:
bool SurfaceFlinger::configureLocked() {
std::vector<HotplugEvent> events;
{
std::lock_guard<std::mutex> lock(mHotplugMutex);
// 将容器中的所有元素移动到局部变量 events 中
// std::move 操作后,原来的 mPendingHotplugEvents 容器会自动变为空状态(moved-from state)
// 所以在 mScheduler 不为空的情况下,会执行两次 configureLocked,但第二次执行时,mPendingHotplugEvents 中的数据已经处理,不会有重复执行的情况
events = std::move(mPendingHotplugEvents);
}
// 遍历热插拔事件
for (const auto [hwcDisplayId, connection] : events) {
// 关注点1 对应 2.1 节, 通过 HWC HAL 进行硬件屏幕标识信息读取,并进行 HWC2::Display 对象的初始化,
if (auto info = getHwComposer().onHotplug(hwcDisplayId, connection)) {
const auto displayId = info->id; // onHotplug 函数中按照一定规则生产的 ID
const ftl::Concat displayString("display ", displayId.value, "(HAL ID ", hwcDisplayId,')');
if (connection == hal::Connection::CONNECTED) {
// 关注点2,对应 2.2 节, 执行 SurfaceFlinger::processHotplug() 方法,
// 进行 PhysicalDisplay 对象的初始化,PhysicalDisplay 用于记录显示设备所具备的能力
// 进行 DisplayDeviceState 对象的初始化,DisplayDeviceState 用于记录当前显示设备的状态
const auto activeModeIdOpt =
processHotplugConnect(displayId, hwcDisplayId, std::move(*info),
displayString.c_str());
if (!activeModeIdOpt) {
mScheduler->dispatchHotplugError(
static_cast<int32_t>(DisplayHotplugEvent::ERROR_UNKNOWN));
getHwComposer().disconnectDisplay(displayId);
continue;
}
// idle 配置
const auto [kernelIdleTimerController, idleTimerTimeoutMs] =
getKernelIdleTimerProperties(displayId);
// DisplayModeController::Display 配置信息
using Config = scheduler::RefreshRateSelector::Config;
const Config config =
{.enableFrameRateOverride = sysprop::enable_frame_rate_override(true)
? Config::FrameRateOverride::Enabled
: Config::FrameRateOverride::Disabled, // 是否支持 FrameRateOverride 特性
.frameRateMultipleThreshold =
base::GetIntProperty("debug.sf.frame_rate_multiple_threshold"s, 0),
.legacyIdleTimerTimeout = idleTimerTimeoutMs,
.kernelIdleTimerController = kernelIdleTimerController};
const auto snapshotOpt =
mPhysicalDisplays.get(displayId).transform(&PhysicalDisplay::snapshotRef);
LOG_ALWAYS_FATAL_IF(!snapshotOpt);
// 关注点3,对应 2.3 节,初始化 DisplayModeController::Display 对象
mDisplayModeController.registerDisplay(*snapshotOpt, *activeModeIdOpt, config);
} else {
// Unregister before destroying the DisplaySnapshot below.
mDisplayModeController.unregisterDisplay(displayId);
processHotplugDisconnect(displayId, displayString.c_str());
}
}
}
return !events.empty();
}
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
相关调用栈如下:
SurfaceFlinger::configureLocked
HwComposer::onHotplug // 2.1节, 初始化 HWC2::Display
HWComposer::onHotplugConnect
HWComposer::allocatePhysicalDisplay
SurfaceFlinger::processHotplugConnect // 2.2 节,初始化 PhysicalDisplay 对象,DisplayDeviceState 对象
DisplayModeController::registerDisplay // 2.3 节,初始化 DisplayModeController::Display 对象
2
3
4
5
6
# 2.1 HWC2::Display 对象的初始化
关注点 1 处,调用 onHotplug 函数,向 HWC HAL 读取硬件屏幕标识信息,并进行 HWC2::Display 对象的初始化
// /frameworks/native/services/surfaceflinger/DisplayHardware/HWComposer.cpp
std::optional<DisplayIdentificationInfo> HWComposer::onHotplug(hal::HWDisplayId hwcDisplayId,
hal::Connection connection) {
switch (connection) {
case hal::Connection::CONNECTED:
// 关注这个 case
return onHotplugConnect(hwcDisplayId);
case hal::Connection::DISCONNECTED:
return onHotplugDisconnect(hwcDisplayId);
case hal::Connection::INVALID:
return {};
}
}
2
3
4
5
6
7
8
9
10
11
12
13
目前只关注屏幕连接的情况,接着看 onHotplugConnect 的实现:
// /frameworks/native/services/surfaceflinger/DisplayHardware/HWComposer.h
std::unordered_map<hal::HWDisplayId, PhysicalDisplayId> mPhysicalDisplayIdMap;
// /frameworks/native/services/surfaceflinger/DisplayHardware/HWComposer.cpp
std::optional<DisplayIdentificationInfo> HWComposer::onHotplugConnect(
hal::HWDisplayId hwcDisplayId) {
// 硬件屏幕标识信息
std::optional<DisplayIdentificationInfo> info;
if (const auto displayId = toPhysicalDisplayId(hwcDisplayId)) {
// 首次加载还没有缓存 id 信息,不走该分支
} else {
uint8_t port;
DisplayIdentificationData data;
// 向 HWC HAL 获取到硬件屏幕标识信息
const bool hasDisplayIdentificationData =
getDisplayIdentificationData(hwcDisplayId, &port, &data);
if (mPhysicalDisplayIdMap.empty()) {
mHasMultiDisplaySupport = hasDisplayIdentificationData;
ALOGI("Switching to %s multi-display mode",
mHasMultiDisplaySupport ? "generalized" : "legacy");
}
if (shouldIgnoreHotplugConnect(hwcDisplayId, hasDisplayIdentificationData)) {
return {};
}
// lamda 表达式主要用于计算出一个 info->id,类型是 PhysicalDisplayId,这个 id 是 sf 中按照一定规则生成的,不是直接从硬件中获取的
info = [this, hwcDisplayId, &port, &data, hasDisplayIdentificationData] {
const bool isPrimary = !mPrimaryHwcDisplayId;
if (mHasMultiDisplaySupport) {
// 格式转换
// parseDisplayIdentificationData 会计算出一个 info.id
if (const auto info = parseDisplayIdentificationData(port, data)) {
return *info;
}
ALOGE("Failed to parse identification data for display %" PRIu64, hwcDisplayId);
} else {
ALOGW_IF(hasDisplayIdentificationData,
"Ignoring identification data for display %" PRIu64, hwcDisplayId);
port = isPrimary ? LEGACY_DISPLAY_TYPE_PRIMARY : LEGACY_DISPLAY_TYPE_EXTERNAL;
}
return DisplayIdentificationInfo{.id = PhysicalDisplayId::fromPort(port),
.name = isPrimary ? "Primary display"
: "Secondary display",
.deviceProductInfo = std::nullopt};
}();
// 通知 hwc hal,sf 这边搞定了
mComposer->onHotplugConnect(hwcDisplayId);
}
if (!isConnected(info->id)) {
// 关注点
// hwcDisplayId:HWC 返回的 ID
// info-id:根据屏幕信息计算出的 ID
// 创建 HWC2::Display 对象
allocatePhysicalDisplay(hwcDisplayId, info->id);
}
return info;
}
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
- 根据从 HWC 获取到的 DisplayIdentificationData port 信息, 调用 parseDisplayIdentificationData 计算出一个 PhysicalDisplayId,赋值给 info->id
- 调用 allocatePhysicalDisplay 创建 HWC2::Display 对象
// /frameworks/native/libs/ui/DisplayIdentification.cpp
std::optional<DisplayIdentificationInfo> parseDisplayIdentificationData(
uint8_t port, const DisplayIdentificationData& data) {
if (data.empty()) {
ALOGI("Display identification data is empty.");
return {};
}
if (!isEdid(data)) {
ALOGE("Display identification data has unknown format.");
return {};
}
// 解析 EDID 信息
const auto edid = parseEdid(data);
if (!edid) {
return {};
}
// 根据屏幕 edid 信息,按照固定规则生成一个 id
// 也就是说 PhysicalDisplayId 是 sf 中按照一定规则生成的,不是直接从硬件中获取的
const auto displayId = PhysicalDisplayId::fromEdid(port, edid->manufacturerId, edid->modelHash);
return DisplayIdentificationInfo{.id = displayId,
.name = std::string(edid->displayName),
.deviceProductInfo = buildDeviceProductInfo(*edid)};
}
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
转换的过程,我们不是特别关心,主要看转换后的结果:
// /frameworks/native/libs/ui/DisplayIdentification.cpp
struct DisplayIdentificationInfo {
PhysicalDisplayId id;
std::string name;
std::optional<DeviceProductInfo> deviceProductInfo;
};
// /frameworks/native/libs/ui/include/ui/DeviceProductInfo.h
// Product-specific information about the display or the directly connected device on the
// display chain. For example, if the display is transitively connected, this field may contain
// product information about the intermediate device.
struct DeviceProductInfo {
struct ModelYear {
uint32_t year;
};
struct ManufactureYear : ModelYear {};
struct ManufactureWeekAndYear : ManufactureYear {
// 1-base week number. Week numbering may not be consistent between manufacturers.
uint8_t week;
};
// Display name.
std::string name;
// Manufacturer Plug and Play ID.
PnpId manufacturerPnpId;
// Manufacturer product ID.
std::string productId;
using ManufactureOrModelDate = std::variant<ModelYear, ManufactureYear, ManufactureWeekAndYear>;
static_assert(std::is_trivially_copyable_v<ManufactureOrModelDate>);
ManufactureOrModelDate manufactureOrModelDate;
// Relative address in the display network. Empty vector indicates that the
// address is unavailable.
// For example, for HDMI connected device this will be the physical address.
std::vector<uint8_t> relativeAddress;
};
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
完成 PhysicalDisplayId 的生成后,接下来,将执行 allocatePhysicalDisplay() 函数,创建 HWC2::Display 对象和 DisplayData 对象:
- HWC2::Display 是硬件屏幕在 SurfaceFlinger 进程内的第一层抽象封装,主要用于与 HWC HAL 通信完成屏幕相关的操作,提供了操作硬件屏幕状态、亮度、VSYNC 信号等函数。
- DisplayData 可以理解为 HWC2::Display 的 "Owner",它持有 HWC2::Display 对象,以及和该 HWC2::Display 相关的 Fence 相关信息。
我们接着看 allocatePhysicalDisplay() 函数的实现:
// frameworks/native/services/surfaceflinger/DisplayHardware/HWComposer.h
std::unordered_map<HalDisplayId, DisplayData> mDisplayData; // 支持多个屏幕
std::unordered_map<hal::HWDisplayId, PhysicalDisplayId> mPhysicalDisplayIdMap;
// 用于保存主屏幕的 HWDisplayId
std::optional<hal::HWDisplayId> mPrimaryHwcDisplayId;
// /frameworks/native/services/surfaceflinger/DisplayHardware/HWComposer.cpp
void HWComposer::allocatePhysicalDisplay(hal::HWDisplayId hwcDisplayId, PhysicalDisplayId displayId,
std::optional<ui::Size> physicalSize) {
// HWDisplayId 与 PhysicalDisplayId 的映射关系保存在 mPhysicalDisplayIdMap 中
mPhysicalDisplayIdMap[hwcDisplayId] = displayId;
if (!mPrimaryHwcDisplayId) {
mPrimaryHwcDisplayId = hwcDisplayId;
}
// 插入一个新的 DisplayData 对象
auto& displayData = mDisplayData[displayId];
// 关注点
// 创建 HWC2::Display 对象
// mCapabilities:在 HWComposer::setCallback 执行时,通过 loadCapabilities 加载了该数据,该数据用于表示 HWC 所支持的能力
auto newDisplay =
std::make_unique<HWC2::impl::Display>(*mComposer.get(), mCapabilities, hwcDisplayId,
hal::DisplayType::PHYSICAL);
newDisplay->setConnected(true);
newDisplay->setPhysicalSizeInMm(physicalSize);
displayData.hwcDisplay = std::move(newDisplay);
}
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
这里初始化了一个 HWC2::impl::Display 对象,HWC2::impl::Display 是 Android SurfaceFlinger 中硬件合成器(Hardware Composer)的显示设备抽象层实现,它封装了与底层硬件显示设备的交互。
HWC2::Display 对象创建完成后,会保存到 DisplayData 对象的 hwcDisplay 成员中。先看下 DisplayData 的整体的结构:
struct DisplayData {
std::unique_ptr<HWC2::Display> hwcDisplay; // HWC2::impl::Display 对象保存在这里
std::optional<uint8_t> port;
sp<Fence> lastPresentFence = Fence::NO_FENCE; // signals when the last set op retires
nsecs_t lastPresentTimestamp = 0;
// Fence
std::unordered_map<HWC2::Layer*, sp<Fence>> releaseFences;
bool validateWasSkipped;
hal::Error presentError;
bool vsyncTraceToggle = false;
std::mutex vsyncEnabledLock;
hal::Vsync vsyncEnabled GUARDED_BY(vsyncEnabledLock) = hal::Vsync::DISABLE;
};
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
接着看 HWC2::impl::Display 的结构与初始化过程:
// frameworks/native/services/surfaceflinger/DisplayHardware/HWC2.h
class Display : public HWC2::Display {
public:
Display(android::Hwc2::Composer&,
const std::unordered_set<aidl::android::hardware::graphics::composer3::Capability>&,
hal::HWDisplayId, hal::DisplayType);
~Display() override;
// 设置屏幕状态
hal::Error setPowerMode(hal::PowerMode) override;
// 设置 VSYNC 开启/关闭
hal::Error setVSYNCEnabled(hal::VSYNC enabled) override;
// 设置屏幕亮度
ftl::Future<hal::Error> setDisplayBrightness(
float brightness, float brightnessNits,
const Hwc2::Composer::DisplayBrightnessOptions& options) override;
// ......
// Member variables
// These are references to data owned by HWComposer, which will outlive
// this HWC2::Display, so these references are guaranteed to be valid for
// the lifetime of this object.
android::Hwc2::Composer& mComposer;
const std::unordered_set<aidl::android::hardware::graphics::composer3::Capability>&
mCapabilities;
const hal::HWDisplayId mId;
hal::DisplayType mType;
// Cached on first call to getConnectionType.
mutable std::optional<ftl::Expected<ui::DisplayConnectionType, hal::Error>> mConnectionType;
bool mIsConnected = false;
using Layers = std::unordered_map<hal::HWLayerId, std::weak_ptr<HWC2::impl::Layer>>;
Layers mLayers;
mutable std::mutex mDisplayCapabilitiesMutex;
std::once_flag mDisplayCapabilityQueryFlag;
std::optional<
std::unordered_set<aidl::android::hardware::graphics::composer3::DisplayCapability>>
mDisplayCapabilities GUARDED_BY(mDisplayCapabilitiesMutex);
// Physical size in mm.
std::optional<ui::Size> mPhysicalSize;
};
// frameworks/native/services/surfaceflinger/DisplayHardware/HWC2.cpp
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);
if (mType == hal::DisplayType::VIRTUAL) {
loadDisplayCapabilities(); // 从 HAL 加载显示能力,结果保存在 mDisplayCapabilities 成员中
}
}
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
HWC2::impl::Display 对象是 Android 显示系统中的核心组件,负责将上层的显示请求转换为底层 HAL 调用,实现了显示硬件的抽象化管理。
# 2.2 初始化 PhysicalDisplay 对象
接着执行 SurfaceFlinger::processHotplugConnect() 方法,进行 PhysicalDisplay 对象的初始化。
using PhysicalDisplays = ui::PhysicalDisplayMap<PhysicalDisplayId, PhysicalDisplay>;
// frameworks/native/services/surfaceflinger/SurfaceFlinger.h
display::PhysicalDisplays mPhysicalDisplays GUARDED_BY(mStateLock);
// frameworks/native/services/SurfaceFlinger/SurfaceFlinger.cpp
std::optional<DisplayModeId> SurfaceFlinger::processHotplugConnect(PhysicalDisplayId displayId,
hal::HWDisplayId hwcDisplayId,
DisplayIdentificationInfo&& info,
const char* displayString) {
// 从 HWC 加载当前屏幕支持的 displaymode 以及现在正在使用的 activeMode
auto [displayModes, activeMode] = loadDisplayModes(displayId);
if (!activeMode) {
ALOGE("Failed to hotplug %s", displayString);
return std::nullopt;
}
const DisplayModeId activeModeId = activeMode->getId();
// 从 HWC 加载当前屏幕支持的 colorMode
ui::ColorModes colorModes = getHwComposer().getColorModes(displayId);
if (const auto displayOpt = mPhysicalDisplays.get(displayId)) { // 第一次执行时为空,不进入
// ......
}
const sp<IBinder> token = sp<BBinder>::make(); // 新建一个 token 标识屏幕
const ui::DisplayConnectionType connectionType =
getHwComposer().getDisplayConnectionType(displayId);
// 关注点,初始化新的 PhysicalDisplay 对象,保存了屏幕支持的 displaymode,colormode 以及屏幕相关信息
mPhysicalDisplays.try_emplace(displayId, token, displayId, connectionType,
std::move(displayModes), std::move(colorModes),
std::move(info.deviceProductInfo));
// 关注点,创建 DisplayDeviceState 对象,用于表示当前显示设备的状态
DisplayDeviceState state;
state.physical = {.id = displayId,
.hwcDisplayId = hwcDisplayId,
.activeMode = std::move(activeMode)};
state.isSecure = connectionType == ui::DisplayConnectionType::Internal;
state.isProtected = true;
state.displayName = std::move(info.name);
// 将 DisplayDeviceState 添加到 mCurrentState.displays 列表中
mCurrentState.displays.add(token, state);
ALOGI("Connecting %s", displayString);
// 返回当前的显示模式
return activeModeId;
}
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
接下来看 PhysicalDisplay 的定义与构造函数:
// frameworks/native/services/surfaceflinger/Display/PhysicalDisplay.h
class PhysicalDisplay {
public:
// 构造函数
template <typename... Args>
PhysicalDisplay(sp<DisplayToken> token, Args&&... args)
: mToken(std::move(token)), mSnapshot(std::forward<Args>(args)...) {}
// ......
private:
// 物理屏标识
// 该物理屏的唯一标识,该值贯穿了整个 SurfaceFlinger 和 system_server 进程屏幕管理机制,DisplayManager 中通过该 Token 指定屏幕进行状态的更新
const sp<DisplayToken> mToken;
// 物理屏配置参数快照
// 是热插拔事件发生后的屏幕快照,保存了物理屏相关属性,如 ColorMode、DisplayModes、连接类型等等。
DisplaySnapshot mSnapshot;
};
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
主要的参数都在 DisplaySnapshot 中:
// frameworks/native/services/surfaceflinger/Display/DisplaySnapshot.h
class DisplaySnapshot {
public:
DisplaySnapshot(PhysicalDisplayId, ui::DisplayConnectionType, DisplayModes&&, ui::ColorModes&&,
std::optional<DeviceProductInfo>&&);
DisplaySnapshot(const DisplaySnapshot&) = delete;
DisplaySnapshot(DisplaySnapshot&&) = default;
PhysicalDisplayId displayId() const { return mDisplayId; }
ui::DisplayConnectionType connectionType() const { return mConnectionType; }
std::optional<DisplayModeId> translateModeId(hal::HWConfigId) const;
const auto& displayModes() const { return mDisplayModes; }
const auto& colorModes() const { return mColorModes; }
const auto& deviceProductInfo() const { return mDeviceProductInfo; }
ui::ColorModes filterColorModes(bool supportsWideColor) const;
void dump(utils::Dumper&) const;
private:
const PhysicalDisplayId mDisplayId;
const ui::DisplayConnectionType mConnectionType;
// Effectively const except in move constructor.
DisplayModes mDisplayModes;
ui::ColorModes mColorModes;
std::optional<DeviceProductInfo> mDeviceProductInfo;
};
// frameworks/native/services/surfaceflinger/Display/DisplaySnapshot.cpp
DisplaySnapshot::DisplaySnapshot(PhysicalDisplayId displayId,
ui::DisplayConnectionType connectionType,
DisplayModes&& displayModes, ui::ColorModes&& colorModes,
std::optional<DeviceProductInfo>&& deviceProductInfo)
: mDisplayId(displayId),
mConnectionType(connectionType),
mDisplayModes(std::move(displayModes)),
mColorModes(std::move(colorModes)),
mDeviceProductInfo(std::move(deviceProductInfo)) {}
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
mPhysicalDisplays 是 SurfaceFlinger 中用于管理物理显示设备的核心容器,其中保存的 PhysicalDisplay 对象具有以下重要作用:
- 物理显示设备的抽象表示 PhysicalDisplay 类封装了物理显示设备的完整信息,包括:
- Display Token : 用于标识显示设备的唯一令牌 ( mToken )
- Display Snapshot : 包含显示设备的不可变状态信息 ( mSnapshot )
- 显示设备的核心属性存储 通过 DisplaySnapshot , PhysicalDisplay 存储了以下关键信息:
- 物理显示ID ( PhysicalDisplayId )
- 端口号 ( port )
- 连接类型 ( DisplayConnectionType ,如内部显示或外部显示)
- 显示模式列表 ( DisplayModes )
- 颜色模式列表 ( ColorModes )
- 设备产品信息 ( DeviceProductInfo )
- 热插拔事件管理 在显示设备的热插拔过程中,mPhysicalDisplays 成员负责:
- 连接事件 : 通过 try_emplace() 添加新的物理显示设备
- 重连事件 : 通过 try_replace() 更新现有显示设备信息
- 断开事件 : 通过 erase() 移除断开的显示设备
- DisplayDevice 创建的数据源 在后续 setupNewDisplayDeviceInternal() 方法中, PhysicalDisplay 提供创建 DisplayDevice 所需的关键信息:
- 颜色模式和宽色域支持判断
- HDR 能力查询
- 显示方向设置
- 刷新率选择器配置
- 显示能力查询接口
PhysicalDisplay 提供了多种查询接口:
- isInternal() : 判断是否为内部显示
- snapshotRef() : 获取显示快照引用
- hasToken() : 根据 token 查找显示设备
- 系统级显示管理
mPhysicalDisplays 容器支持:
- 多显示设备的统一管理
- 主显示设备的识别
- 显示设备状态的实时更新
- 显示配置的动态调整
总的来说,PhysicalDisplay 对象作为物理显示硬件在 SurfaceFlinger 中的软件抽象,负责管理显示设备的生命周期、属性信息和状态变化,是连接底层硬件和上层显示服务的重要桥梁
接着初始化 DisplayDeviceState 结构体,并保存到 SurfaceFlinger.mCurrentState.displays 成员中,DisplayDeviceState 用于表示当前显示设备的状态:
struct DisplayDeviceState {
struct Physical {
PhysicalDisplayId id;
hardware::graphics::composer::hal::HWDisplayId hwcDisplayId;
DisplayModePtr activeMode;
bool operator==(const Physical& other) const {
return id == other.id && hwcDisplayId == other.hwcDisplayId;
}
};
bool isVirtual() const { return !physical; }
int32_t sequenceId = sNextSequenceId++;
std::optional<Physical> physical;
sp<IGraphicBufferProducer> surface;
ui::LayerStack layerStack;
uint32_t flags = 0;
Rect layerStackSpaceRect;
Rect orientedDisplaySpaceRect;
ui::Rotation orientation = ui::ROTATION_0;
uint32_t width = 0;
uint32_t height = 0;
std::string displayName;
std::string uniqueId;
bool isSecure = false;
bool isProtected = false;
// Refer to DisplayDevice::mRequestedRefreshRate, for virtual display only
Fps requestedRefreshRate;
int32_t maxLayerPictureProfiles = 0;
bool hasPictureProcessing = false;
private:
static std::atomic<int32_t> sNextSequenceId;
};
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
看下 SurfaceFlinger::State 的定义:
// frameworks\native\services\surfaceflinger\SurfaceFlinger.h
class State {
public:
explicit State(LayerVector::StateSet set) : stateSet(set) {}
State& operator=(const State& other) {
// We explicitly don't copy stateSet so that, e.g., mDrawingState
// always uses the Drawing StateSet.
displays = other.displays;
colorMatrixChanged = other.colorMatrixChanged;
if (colorMatrixChanged) {
colorMatrix = other.colorMatrix;
}
globalShadowSettings = other.globalShadowSettings;
return *this;
}
const LayerVector::StateSet stateSet = LayerVector::StateSet::Invalid;
// TODO(b/241285876): Replace deprecated DefaultKeyedVector with ftl::SmallMap.
DefaultKeyedVector<wp<IBinder>, DisplayDeviceState> displays;
std::optional<size_t> getDisplayIndex(PhysicalDisplayId displayId) const {
for (size_t i = 0; i < displays.size(); i++) {
const auto& state = displays.valueAt(i);
if (state.physical && state.physical->id == displayId) {
return i;
}
}
return {};
}
bool colorMatrixChanged = true;
mat4 colorMatrix;
ShadowSettings globalShadowSettings;
};
// frameworks/native/services/surfaceflinger/LayerVector.h
enum class StateSet {
Invalid,
Current,
Drawing,
};
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
DisplayDeviceState 用于表示显示设备的当前状态,PhysicalDisplay 用于保存显示设备所支持的所有能力。
SurfaceFlinger 中有两个 State 成员 mCurrentState 和 mDrawingState:
- mCurrentState(当前状态) 作用:
接收新的事务请求 :当客户端通过 Binder 调用提交新的显示或图层变更时,这些变更首先被应用到 mCurrentState
暂存待处理的变更 :作为一个"暂存区",收集所有待处理的状态变更
线程安全访问 :受 mStateLock 保护,支持多线程安全访问 特点:
可能包含尚未生效的变更
在事务提交前可能被多次修改
需要锁保护以确保线程安全
- mDrawingState(绘制状态) 作用:
实际渲染的状态 :代表当前正在用于渲染和合成的稳定状态
合成器使用的状态 :Hardware Composer (HWC) 和 RenderEngine 基于此状态进行实际的屏幕合成
稳定的快照 :在一个 VSync 周期内保持不变,确保渲染的一致性 特点:
只在主线程访问,无需锁保护
状态稳定,在合成过程中不会改变
反映了实际显示的内容
工作流程
- 接收事务 :客户端的变更请求被应用到 mCurrentState
- 累积变更 :多个事务可能在一个 VSync 周期内累积到 mCurrentState
- 事务提交 :在 VSync 信号到来时,调用 commitTransactionsLocked()
- 状态同步 : doCommitTransactions() 将 mCurrentState 复制到 mDrawingState
- 开始合成 :基于稳定的 mDrawingState 进行屏幕合成
设计优势 这种双状态设计提供了以下优势:
- 性能优化 :避免在合成过程中频繁加锁
- 状态一致性 :确保单次合成使用一致的状态快照
- 并发处理 :允许在合成进行时继续接收新的事务请求
- 原子性 :事务变更要么全部生效,要么全部不生效 总结来说, mCurrentState 是"未来状态"的容器,而 mDrawingState 是"当前实际状态"的快照,两者通过定期同步实现了高效的状态管理和渲染流水线。
# 2.3 显示设备的配置
完成 HWC2::impl::Display、PhysicalDisplay 和 DisplayDeviceState 后,接下来就要配置(configure)显示设备了,在配置过程中初始化 DisplayModeController::Display 对象
bool SurfaceFlinger::configureLocked() {
// ......
// 关注点3,对应 2.3 节,显示设备配置
// 配置对象 Config 初始化
// kernelIdleTimerController 有两个值 KernelIdleTimerController::HwcApi KernelIdleTimerController::Sysprop
// 具体哪个值,取决于 hwc 的支持情况,大多情况为 null
// 指定多倍数或启发式图层投票类型的刷新率上限阈值(包含),高于此值的刷新率将不会被投票选择。0 表示未设置阈值。
// enableFrameRateOverride 是否开启 FrameRateOverride feature
// frameRateMultipleThreshold idleTimerTimeoutMs 来自系统属性 debug.sf.set_idle_timer_ms_
const auto [kernelIdleTimerController, idleTimerTimeoutMs] =
getKernelIdleTimerProperties(displayId);
using Config = scheduler::RefreshRateSelector::Config;
const Config config =
{.enableFrameRateOverride = sysprop::enable_frame_rate_override(true)
? Config::FrameRateOverride::Enabled
: Config::FrameRateOverride::Disabled,
.frameRateMultipleThreshold =
base::GetIntProperty("debug.sf.frame_rate_multiple_threshold"s, 0),
.legacyIdleTimerTimeout = idleTimerTimeoutMs,
.kernelIdleTimerController = kernelIdleTimerController};
// 拿到当前 PhysicalDisplay 的 DisplaySnapshot 成员
const auto snapshotOpt =
mPhysicalDisplays.get(displayId).transform(&PhysicalDisplay::snapshotRef);
LOG_ALWAYS_FATAL_IF(!snapshotOpt);
// 配置显示设备
// 初始化 DisplayModeController::Display
mDisplayModeController.registerDisplay(*snapshotOpt, *activeModeIdOpt, config);
// ......
}
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
这里用到了 SurfaceFlinger 的 mDisplayModeController 组件,PhysicalDisplay 中保存了显示设备支持的各种 DisplayMode,不同 DisplayMode 之间的差异点主要在分辨率和刷新率上,mDisplayModeController 就是 SurfaceFlinger 中用于管理和切换 DisplayMode 的组件。
接下来来看 mDisplayModeController 的初始化与配置过程。
# 2.3.1 mDisplayModeController 的初始化过程
mDisplayModeController 是 SurfaceFlinger 的值成员:
// /frameworks/native/services/surfaceflinger/SurfaceFlinger.h
display::DisplayModeController mDisplayModeController;
2
使用默认构造函数进行初始化
// /frameworks/native/services/surfaceflinger/Display/DisplayModeController.h
class DisplayModeController {
public:
// ......
DisplayModeController() = default;
//......
}
2
3
4
5
6
7
在 SurfaceFlinger::init() 过程中,mDisplayModeController 会设置其 HWComposer 引用
// /frameworks/native/services/surfaceflinger/SurfaceFlinger.cpp
void SurfaceFlinger::init() FTL_FAKE_GUARD(kMainThreadContext) {
// ....
mCompositionEngine->setHwComposer(getFactory().createHWComposer(mHwcServiceName));
auto& composer = mCompositionEngine->getHwComposer();
composer.setCallback(*this);
// 设置 HWComposer 引用
mDisplayModeController.setHwComposer(&composer);
// ......
}
2
3
4
5
6
7
8
9
10
11
在 SurfaceFlinger::init() 后续过程中,mDisplayModeController 会设置活动模式监听器 ActiveModeListener mActiveModeListener 成员,该回调会在 modeId 切换时调用。
void SurfaceFlinger::init() FTL_FAKE_GUARD(kMainThreadContext) {
// ....
mDisplayModeController.setActiveModeListener(
display::DisplayModeController::ActiveModeListener::make(
[this](PhysicalDisplayId displayId, Fps vsyncRate, Fps renderRate) {
// 回调处理逻辑
static_cast<void>(mScheduler->schedule([=, this]() FTL_FAKE_GUARD(mStateLock) {
if (const auto display = getDisplayDeviceLocked(displayId)) {
display->updateRefreshRateOverlayRate(vsyncRate, renderRate);
}
}));
}));
2
3
4
5
6
7
8
9
10
11
12
# 2.3.2 registerDisplay 过程分析
DisplayModeController 主要的成员有:
- HWComposer* mComposerPtr :指向硬件合成器的指针
- ActiveModeListener mActiveModeListener :活动模式变化监听器
- ui::PhysicalDisplayMap<PhysicalDisplayId, DisplayPtr> mDisplays :用于保存 DisplayModeController::Display 的 Map 结构
接下来分析 DisplayModeController.registerDisplay 过程:
registerDisplay 传入了 Config 参数:
const Config config =
{.enableFrameRateOverride = sysprop::enable_frame_rate_override(true)
? Config::FrameRateOverride::Enabled
: Config::FrameRateOverride::Disabled,
.frameRateMultipleThreshold =
base::GetIntProperty("debug.sf.frame_rate_multiple_threshold"s, 0),
.legacyIdleTimerTimeout = idleTimerTimeoutMs,
.kernelIdleTimerController = kernelIdleTimerController};
2
3
4
5
6
7
8
- enableFrameRateOverride 用于决定是否开启 FrameRateOverride 功能,其值来自系统属性
ro.surface_flinger.enable_frame_rate_override - frameRateMultipleThreshold 为 multiple 或 heuristic 类型的图层投票设置刷新率上限阈值,高于此值的刷新率将不会被投票选择,其值来自系统属性
debug.sf.frame_rate_multiple_threshold - legacyIdleTimerTimeout 用于设置空闲定时器的超时时长,用于在无活动时降低刷新率以节省电量,当屏幕在此时间内无活动时,系统可能会降低刷新率。
- kernelIdleTimerController 指定如何配置内核空闲定时器
- Sysprop :通过系统属性控制
- HwcApi :通过硬件合成器(HWC)API 控制
- std::nullopt :不支持内核空闲定时器,一般是这个值
registerDisplay 的实现如下:
// frameworks/native/services/surfaceflinger/Display/DisplayModeController.h
ui::PhysicalDisplayMap<PhysicalDisplayId, DisplayPtr> mDisplays GUARDED_BY(mDisplayLock);
// frameworks/native/services/surfaceflinger/Display/DisplayModeController.cpp
void DisplayModeController::registerDisplay(DisplaySnapshotRef snapshotRef,
DisplayModeId activeModeId,
scheduler::RefreshRateSelector::Config config) {
const auto& snapshot = snapshotRef.get();
const auto displayId = snapshot.displayId();
// DisplayModeController::Display 类型初始化
DisplayPtr displayPtr =
std::make_unique<Display>(snapshotRef, snapshot.displayModes(), activeModeId, config);
// TODO: b/349703362 - Remove first condition when HDCP aidl APIs are enforced
displayPtr->setSecure(!supportsHdcp() ||
snapshotRef.get().connectionType() ==
ui::DisplayConnectionType::Internal);
std::lock_guard lock(mDisplayLock);
mDisplays.emplace_or_replace(displayId, std::move(displayPtr));
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
构建一个 DisplayModeController::Display 对象,然后插入 mDisplays 中。
DisplayModeController::Display 的定义如下:
// frameworks/native/services/surfaceflinger/Display/DisplayModeController.h
struct Display {
template <size_t N>
std::string concatId(const char (&)[N]) const;
Display(DisplaySnapshotRef, RefreshRateSelectorPtr);
// 构造函数
Display(DisplaySnapshotRef snapshot, DisplayModes modes, DisplayModeId activeModeId,
scheduler::RefreshRateSelector::Config config)
: Display(snapshot,
std::make_shared<scheduler::RefreshRateSelector>(std::move(modes),
activeModeId, config)) {}
void setSecure(bool secure) {
hdcpState = secure ? HdcpState::Undesired : HdcpState::Desired;
}
const DisplaySnapshotRef snapshot;
const RefreshRateSelectorPtr selectorPtr;
const std::string pendingModeFpsTrace;
const std::string activeModeFpsTrace;
const std::string renderRateFpsTrace;
std::mutex desiredModeLock;
/*
1.设置期望模式 :应用请求 → desiredModeOpt 被设置
2.开始切换 :系统处理请求 → desiredModeOpt 移动到 pendingModeOpt
3.完成切换 :硬件确认 → pendingModeOpt 清空,成为活动模式
*/
// 存储 期望的 显示模式请求
DisplayModeRequestOpt desiredModeOpt GUARDED_BY(desiredModeLock);
TracedOrdinal<bool> hasDesiredModeTrace GUARDED_BY(desiredModeLock);
// 存储 正在进行中的 显示模式请求
DisplayModeRequestOpt pendingModeOpt GUARDED_BY(kMainThreadContext);
bool isModeSetPending GUARDED_BY(kMainThreadContext) = false;
bool isKernelIdleTimerEnabled GUARDED_BY(kMainThreadContext) = false;
// HdcpState 是一个用于管理 HDCP (High-bandwidth Digital Content Protection) 协议状态的枚举类型。HDCP 是一种数字内容保护技术,用于防止高清数字音视频内容在传输过程中被非法复制。
HdcpState hdcpState = HdcpState::Desired;
};
// /frameworks/native/services/surfaceflinger/Display/DisplayModeController.cpp
DisplayModeController::Display::Display(DisplaySnapshotRef snapshot,
RefreshRateSelectorPtr selectorPtr)
: snapshot(snapshot),
selectorPtr(std::move(selectorPtr)),
pendingModeFpsTrace(concatId("PendingModeFps")),
activeModeFpsTrace(concatId("ActiveModeFps")),
renderRateFpsTrace(concatId("RenderRateFps")),
hasDesiredModeTrace(concatId("HasDesiredMode"), false) {}
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
DisplayModeController::Display 构造函数中,先构建一个 RefreshRateSelector 对象,再用这个对象初始化 Display。
RefreshRateSelector 中定义了多个内部类:
- Policy 用于定义刷新率选择策略
class Policy {
static constexpr int kAllowGroupSwitchingDefault = false;
public:
// The default mode, used to ensure we only initiate display mode switches within the
// same mode group as defaultMode's group.
DisplayModeId defaultMode;
// Whether or not we switch mode groups to get the best frame rate.
bool allowGroupSwitching = kAllowGroupSwitchingDefault;
// The primary refresh rate ranges. @see DisplayModeSpecs.aidl for details.
// TODO(b/257072060): use the render range when selecting SF render rate
// or the app override frame rate
FpsRanges primaryRanges;
// The app request refresh rate ranges. @see DisplayModeSpecs.aidl for details.
FpsRanges appRequestRanges;
// The idle timer configuration, if provided.
std::optional<gui::DisplayModeSpecs::IdleScreenRefreshRateConfig> idleScreenConfigOpt;
// .......
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
- defaultMode : 默认显示模式
- allowGroupSwitching : 是否允许模式组切换
- primaryRanges : 主要刷新率范围
- appRequestRanges : 应用请求的刷新率范围
- idleScreenConfigOpt : 空闲屏幕配置
- LayerRequrement 描述单个图层的刷新率需求
// Captures the layer requirements for a refresh rate. This will be used to determine the
// display refresh rate.
struct LayerRequirement {
// Layer's name. Used for debugging purposes.
std::string name;
// Layer's owner uid
uid_t ownerUid = static_cast<uid_t>(-1);
// Layer vote type.
LayerVoteType vote = LayerVoteType::NoVote;
// Layer's desired refresh rate, if applicable.
Fps desiredRefreshRate;
// If a seamless mode switch is required.
Seamlessness seamlessness = Seamlessness::Default;
// Layer frame rate category.
FrameRateCategory frameRateCategory = FrameRateCategory::Default;
// Goes together with frame rate category vote. Allow refresh rate changes only
// if there would be no jank.
bool frameRateCategorySmoothSwitchOnly = false;
// Layer's weight in the range of [0, 1]. The higher the weight the more impact this layer
// would have on choosing the refresh rate.
float weight = 0.0f;
// Whether layer is in focus or not based on WindowManager's state
bool focused = false;
// ......
};
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
- vote : 投票类型(NoVote, Min, Max, Heuristic, ExplicitDefault 等)
- desiredRefreshRate : 期望的刷新率
- weight : 图层权重(0-1)
- focused : 是否为焦点图层
- frameRateCategory : 帧率类别
- LayerVoteType 定义图层投票类型
enum class LayerVoteType {
NoVote, // 不关心刷新率
Min, // 最小刷新率
Max, // 最大刷新率
Heuristic, // 启发式计算的刷新率
ExplicitDefault, // 应用明确指定(默认兼容性)
ExplicitExactOrMultiple, // 应用明确指定(精确或倍数兼容性)
ExplicitExact, // 应用明确指定(精确兼容性)
ExplicitGte, // 大于等于指定帧率
ExplicitCategory // 指定帧率类别
};
2
3
4
5
6
7
8
9
10
11
- GlobalSignals 描述全局信号
// Global state describing signals that affect refresh rate choice.
struct GlobalSignals {
// Whether the user touched the screen recently. Used to apply touch boost.
bool touch = false;
// True if the system hasn't seen any buffers posted to layers recently.
bool idle = false;
// Whether the display is about to be powered on, or has been in PowerMode::ON
// within the timeout of DisplayPowerTimer.
bool powerOnImminent = false;
bool shouldEmitEvent() const { return !idle; }
bool operator==(GlobalSignals other) const {
return touch == other.touch && idle == other.idle &&
powerOnImminent == other.powerOnImminent;
}
auto toString() const {
return ftl::Concat("{touch=", touch, ", idle=", idle,
", powerOnImminent=", powerOnImminent, '}');
}
};
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
- touch : 用户是否最近触摸屏幕(触摸加速)
- idle : 系统是否空闲(无新缓冲区)
- powerOnImminent : 显示器是否即将开启或刚开启
- ScoredFrameRate 描述带评分的刷新率模式
struct ScoredFrameRate {
FrameRateMode frameRateMode;
float score = 0.0f;
bool operator==(const ScoredFrameRate& other) const {
return frameRateMode == other.frameRateMode && score == other.score;
}
static bool scoresEqual(float lhs, float rhs) {
constexpr float kEpsilon = 0.0001f;
return std::abs(lhs - rhs) <= kEpsilon;
}
struct DescendingScore {
bool operator()(const ScoredFrameRate& lhs, const ScoredFrameRate& rhs) const {
return lhs.score > rhs.score && !scoresEqual(lhs.score, rhs.score);
}
};
};
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
- RankedFrameRates 描述排名后的刷新率模式
using FrameRateRanking = std::vector<ScoredFrameRate>;
struct RankedFrameRates {
FrameRateRanking ranking; // Ordered by descending score.
GlobalSignals consideredSignals;
Fps pacesetterFps;
bool operator==(const RankedFrameRates& other) const {
return ranking == other.ranking && consideredSignals == other.consideredSignals &&
isApproxEqual(pacesetterFps, other.pacesetterFps);
}
};
2
3
4
5
6
7
8
9
10
11
12
- Config 配置管理
// Configuration flags.
struct Config {
enum class FrameRateOverride {
// Do not override the frame rate for an app
Disabled,
// Override the frame rate for an app to a value which is also
// a display refresh rate
AppOverrideNativeRefreshRates,
// Override the frame rate for an app to any value
AppOverride,
// Override the frame rate for all apps and all values.
Enabled,
ftl_last = Enabled
};
FrameRateOverride enableFrameRateOverride = FrameRateOverride::Disabled;
// Specifies the upper refresh rate threshold (inclusive) for layer vote types of multiple
// or heuristic, such that refresh rates higher than this value will not be voted for. 0 if
// no threshold is set.
int frameRateMultipleThreshold = 0;
// The Idle Timer timeout. 0 timeout means no idle timer.
std::chrono::milliseconds legacyIdleTimerTimeout = 0ms;
// The controller representing how the kernel idle timer will be configured
// either on the HWC api or sysprop.
ftl::Optional<KernelIdleTimerController> kernelIdleTimerController;
};
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
- enableFrameRateOverride : 帧率覆盖模式
- frameRateMultipleThreshold : 帧率倍数阈值(我们之前分析的参数)
- legacyIdleTimerTimeout : 空闲定时器超时
- kernelIdleTimerController : 内核空闲定时器控制器
RefreshRateSelector 中主要的成员变量:
// 包含所有支持的分辨率和刷新率组合。当显示模式发生变化时,相关的迭代器和策略都需要重新设置.
using DisplayModes = ftl::SmallMap<DisplayModeId, DisplayModePtr, 3>;
DisplayModes mDisplayModes GUARDED_BY(mLock);
// 表示系统当前正在使用的显示模式,包括刷新率和渲染帧率信息
ftl::Optional<FrameRateMode> mActiveModeOpt GUARDED_BY(mLock);
// 指向最低刷新率显示模式的迭代器
DisplayModeIterator mMinRefreshRateModeIt GUARDED_BY(mLock);
// 指向最高刷新率显示模式的迭代器
DisplayModeIterator mMaxRefreshRateModeIt GUARDED_BY(mLock);
// 满足主要策略范围的帧率模式列表,按刷新率过滤和排序,用于主要的刷新率选择逻辑
std::vector<FrameRateMode> mPrimaryFrameRates GUARDED_BY(mLock);
// 满足应用请求范围的帧率模式列表,专门处理应用明确请求的刷新率范围
std::vector<FrameRateMode> mAppRequestFrameRates GUARDED_BY(mLock);
// 所有可用的帧率模式列表,包含所有可能的帧率组合,用于全局搜索和比较
std::vector<FrameRateMode> mAllFrameRates GUARDED_BY(mLock);
// DMS 设置的策略
Policy mDisplayManagerPolicy GUARDED_BY(mLock);
// 用于测试或特殊场景的策略覆盖,优先级高于显示管理器策略
std::optional<Policy> mOverridePolicy GUARDED_BY(mLock);
// 当使用 AppOverrideNativeRefreshRates 模式时,用于快速查找支持的原生刷新率
ftl::SmallMap<Fps, ftl::Unit, 8, FpsApproxEqual> mAppOverrideNativeRefreshRates;
// 帧率排序结果的缓存 缓存最近一次的帧率排序计算结果,避免重复计算相同的输入
mutable std::optional<GetRankedFrameRatesCache> mGetRankedFrameRatesCache GUARDED_BY(mLock);
// idle 定时器
std::optional<IdleTimerCallbacks> mIdleTimerCallbacks GUARDED_BY(mIdleTimerCallbacksMutex);
// Used to detect (lack of) frame activity.
ftl::Optional<scheduler::OneShotTimer> mIdleTimer;
std::atomic<bool> mIdleTimerStarted = false;
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
RefreshRateSelector 中主要的函数有:
getRankedFrameRates: 根据图层需求和全局信号获取排序的帧率列表setPolicy: 设置刷新率策略getCurrentPolicy: 获取当前策略setActiveMode: 设置活动模式getActiveMode: 获取活动模式startIdleTimer: 启动空闲定时器resetIdleTimer: 重置空闲定时器
RefreshRateSelector 工作基本流程如下:
- 收集输入 : 接收图层需求(LayerRequirement)、全局信号(GlobalSignals)和当前策略(Policy)
- 评分计算 : 为每个可用的刷新率模式计算评分,考虑因素包括:
- 图层的期望刷新率和权重
- 图层的投票类型和兼容性要求
- 全局信号(触摸、空闲、电源状态)
- 系统策略限制
- 排序选择 : 按评分对刷新率进行排序,选择最优的刷新率
- 动态调整 : 根据系统状态变化(如应用切换、用户交互、电源状态)动态调整刷新率
RefreshRateSelector 的初始化过程如下:
// /frameworks/native/services/surfaceflinger/Scheduler/RefreshRateSelector.cpp
RefreshRateSelector::RefreshRateSelector(DisplayModes modes, DisplayModeId activeModeId,
Config config)
: mKnownFrameRates(constructKnownFrameRates(modes)), mConfig(config) {
initializeIdleTimer(mConfig.legacyIdleTimerTimeout);
FTL_FAKE_GUARD(kMainThreadContext, updateDisplayModes(std::move(modes), activeModeId));
}
std::vector<Fps> constructKnownFrameRates(const DisplayModes& modes) {
std::vector<Fps> knownFrameRates = {24_Hz, 30_Hz, 45_Hz, 60_Hz, 72_Hz};
knownFrameRates.reserve(knownFrameRates.size() + modes.size());
// Add all supported refresh rates.
for (const auto& [id, mode] : modes) {
knownFrameRates.push_back(mode->getPeakFps());
}
// Sort and remove duplicates.
std::sort(knownFrameRates.begin(), knownFrameRates.end(), isStrictlyLess);
knownFrameRates.erase(std::unique(knownFrameRates.begin(), knownFrameRates.end(),
isApproxEqual),
knownFrameRates.end());
return knownFrameRates;
}
// 初始化 mIdleTimer
void RefreshRateSelector::initializeIdleTimer(std::chrono::milliseconds timeout) {
if (timeout > 0ms) {
mIdleTimer.emplace(
"IdleTimer", timeout,
[this] {
std::scoped_lock lock(mIdleTimerCallbacksMutex);
if (const auto callbacks = getIdleTimerCallbacks()) {
callbacks->onReset();
}
},
[this] {
std::scoped_lock lock(mIdleTimerCallbacksMutex);
if (const auto callbacks = getIdleTimerCallbacks()) {
callbacks->onExpired();
}
});
}
}
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
接着执行 updateDisplayModes:
void RefreshRateSelector::updateDisplayModes(DisplayModes modes, DisplayModeId activeModeId) {
std::lock_guard lock(mLock);
// Invalidate the cached invocation to getRankedFrameRates. This forces
// the refresh rate to be recomputed on the next call to getRankedFrameRates.
mGetRankedFrameRatesCache.reset();
mDisplayModes = std::move(modes);
const auto activeModeOpt = mDisplayModes.get(activeModeId);
LOG_ALWAYS_FATAL_IF(!activeModeOpt);
mActiveModeOpt = FrameRateMode{activeModeOpt->get()->getPeakFps(),
ftl::as_non_null(activeModeOpt->get())};
const auto sortedModes = sortByRefreshRate(mDisplayModes);
mMinRefreshRateModeIt = sortedModes.front();
mMaxRefreshRateModeIt = sortedModes.back();
// Reset the policy because the old one may no longer be valid.
mDisplayManagerPolicy = {};
mDisplayManagerPolicy.defaultMode = activeModeId;
mFrameRateOverrideConfig = [&] {
switch (mConfig.enableFrameRateOverride) {
case Config::FrameRateOverride::Disabled:
case Config::FrameRateOverride::AppOverride:
case Config::FrameRateOverride::Enabled:
return mConfig.enableFrameRateOverride;
case Config::FrameRateOverride::AppOverrideNativeRefreshRates:
return shouldEnableFrameRateOverride(sortedModes)
? Config::FrameRateOverride::AppOverrideNativeRefreshRates
: Config::FrameRateOverride::Disabled;
}
}();
if (mConfig.enableFrameRateOverride ==
Config::FrameRateOverride::AppOverrideNativeRefreshRates) {
for (const auto& [_, mode] : mDisplayModes) {
mAppOverrideNativeRefreshRates.try_emplace(mode->getPeakFps(), ftl::unit);
}
}
constructAvailableRefreshRates();
}
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
void RefreshRateSelector::constructAvailableRefreshRates() {
// Filter modes based on current policy and sort on refresh rate.
// 关键
const Policy* policy = getCurrentPolicyLocked();
ALOGV("%s: %s ", __func__, policy->toString().c_str());
const auto& defaultMode = mDisplayModes.get(policy->defaultMode)->get();
const auto filterRefreshRates = [&](const FpsRanges& ranges,
const char* rangeName) REQUIRES(mLock) {
const auto filterModes = [&](const DisplayMode& mode) {
return mode.getResolution() == defaultMode->getResolution() &&
mode.getDpi() == defaultMode->getDpi() &&
(policy->allowGroupSwitching || mode.getGroup() == defaultMode->getGroup()) &&
ranges.physical.includes(mode.getPeakFps()) &&
(supportsFrameRateOverride() || ranges.render.includes(mode.getPeakFps()));
};
auto frameRateModes = createFrameRateModes(*policy, filterModes, ranges.render);
if (frameRateModes.empty()) {
ALOGW("No matching frame rate modes for %s range. policy: %s", rangeName,
policy->toString().c_str());
// TODO(b/292105422): Ideally DisplayManager should not send render ranges smaller than
// the min supported. See b/292047939.
// For not we just ignore the render ranges.
frameRateModes = createFrameRateModes(*policy, filterModes, {});
}
LOG_ALWAYS_FATAL_IF(frameRateModes.empty(),
"No matching frame rate modes for %s range even after ignoring the "
"render range. policy: %s",
rangeName, policy->toString().c_str());
const auto stringifyModes = [&] {
std::string str;
for (const auto& frameRateMode : frameRateModes) {
str += to_string(frameRateMode) + " ";
}
return str;
};
ALOGV("%s render rates: %s, isVrrDevice? %d", rangeName, stringifyModes().c_str(),
mIsVrrDevice.load());
return frameRateModes;
};
mPrimaryFrameRates = filterRefreshRates(policy->primaryRanges, "primary");
mAppRequestFrameRates = filterRefreshRates(policy->appRequestRanges, "app request");
mAllFrameRates = filterRefreshRates(FpsRanges(getSupportedFrameRateRangeLocked(),
getSupportedFrameRateRangeLocked()),
"full frame rates");
}
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
# 3. processDisplayAdded 过程分析(重要)
本节分析后续的 processDisplayAdded 过程。
void SurfaceFlinger::processDisplayAdded(const wp<IBinder>& displayToken,
const DisplayDeviceState& state) {
ui::Size resolution(0, 0);
ui::PixelFormat pixelFormat = static_cast<ui::PixelFormat>(PIXEL_FORMAT_UNKNOWN);
if (state.physical) {
resolution = state.physical->activeMode->getResolution();
// 物理屏默认格式为 RGBA_8888
pixelFormat = static_cast<ui::PixelFormat>(PIXEL_FORMAT_RGBA_8888);
} else if (state.surface != nullptr) { //虚拟屏的情况
int status = state.surface->query(NATIVE_WINDOW_WIDTH, &resolution.width);
ALOGE_IF(status != NO_ERROR, "Unable to query width (%d)", status);
status = state.surface->query(NATIVE_WINDOW_HEIGHT, &resolution.height);
ALOGE_IF(status != NO_ERROR, "Unable to query height (%d)", status);
int format;
status = state.surface->query(NATIVE_WINDOW_FORMAT, &format);
ALOGE_IF(status != NO_ERROR, "Unable to query format (%d)", status);
pixelFormat = static_cast<ui::PixelFormat>(format);
} else {
// Virtual displays without a surface are dormant:
// they have external state (layer stack, projection,
// etc.) but no internal state (i.e. a DisplayDevice).
return;
}
// compositionengine::Display 构造器对象
compositionengine::DisplayCreationArgsBuilder builder;
if (const auto& physical = state.physical) {
builder.setId(physical->id); // 传入的是 PhysicalDisplayId
} else {
builder.setId(acquireVirtualDisplay(resolution, pixelFormat, state.uniqueId));
}
builder.setPixels(resolution);
builder.setIsSecure(state.isSecure);
builder.setIsProtected(state.isProtected);
builder.setHasPictureProcessing(state.hasPictureProcessing);
builder.setMaxLayerPictureProfiles(state.maxLayerPictureProfiles);
builder.setPowerAdvisor(mPowerAdvisor.get());
builder.setName(state.displayName);
// 关注点1,对应 3.1 节
// 创建 compositionengine::Display 对象
auto compositionDisplay = getCompositionEngine().createDisplay(builder.build());
compositionDisplay->setLayerCachingEnabled(mLayerCachingEnabled);
// 关注点2,对应 3.2 节
// 创建 BufferQueue、BufferProducer 和 BufferComsumer,用于对应屏幕的 GPU 合成
sp<compositionengine::DisplaySurface> displaySurface;
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()); // PhysicalDisplayId
LOG_FATAL_IF(!displayId);
// 创建 FramebufferSurface 对象
// /frameworks/native/libs/gui/libgui_flags.aconfig
// aosp a16 模拟器上查看 adb shell aflags list | grep com.android.graphics.libgui.flags
// com.android.graphics.libgui.flags.wb_consumer_base_owns_bq enabled - default read-only system
#if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(WB_CONSUMER_BASE_OWNS_BQ) // 走这个分支
const auto frameBufferSurface =
sp<FramebufferSurface>::make(getHwComposer(), *displayId, bqProducer, bqConsumer,
state.physical->activeMode->getResolution(),
ui::Size(maxGraphicsWidth, maxGraphicsHeight));
displaySurface = frameBufferSurface;
producer = frameBufferSurface->getSurface()->getIGraphicBufferProducer();
#else
displaySurface =
sp<FramebufferSurface>::make(getHwComposer(), *displayId, bqConsumer,
state.physical->activeMode->getResolution(),
ui::Size(maxGraphicsWidth, maxGraphicsHeight));
producer = bqProducer;
#endif // COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(WB_CONSUMER_BASE_OWNS_BQ)
}
LOG_FATAL_IF(!displaySurface);
// 关注点3, 对应 3.3 节,构建 DisplayDevice 对象
auto display = setupNewDisplayDeviceInternal(displayToken, std::move(compositionDisplay), state,
displaySurface, producer);
// 此时 mScheduler 为 null,不会走这里
if (mScheduler && !display->isVirtual()) {
// TODO(b/241285876): Annotate `processDisplayAdded` instead.
ftl::FakeGuard guard(kMainThreadContext);
// For hotplug reconnect, renew the registration since display modes have been reloaded.
mScheduler->registerDisplay(display->getPhysicalId(), display->holdRefreshRateSelector(),
mActiveDisplayId);
}
if (display->isVirtual()) {
display->adjustRefreshRate(mScheduler->getPacesetterRefreshRate());
}
if (display->isRefreshable()) {
incRefreshableDisplays();
}
mDisplays.try_emplace(displayToken, std::move(display));
// For an external display, loadDisplayModes already attempted to select the same mode
// as DM, but SF still needs to be updated to match.
// TODO (b/318534874): Let DM decide the initial mode.
if (const auto& physical = state.physical;
mScheduler && physical && FlagManager::getInstance().connected_display()) {
const bool isInternalDisplay = mPhysicalDisplays.get(physical->id)
.transform(&PhysicalDisplay::isInternal)
.value_or(false);
if (!isInternalDisplay) {
auto activeModePtr = physical->activeMode;
const auto fps = activeModePtr->getPeakFps();
setDesiredMode(
{.mode = scheduler::FrameRateMode{fps,
ftl::as_non_null(std::move(activeModePtr))},
.emitEvent = false,
.force = true});
}
}
}
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
127
128
# 3.1 compositionengine::Display 初始化
compositionengine::Display 是图层合成过程中显示设备的抽象与描述,其整体的类关系还是很复杂的:

compositionengine::Display 通过 CompositionEngine::createDisplay 函数进行构建与初始化,整体调用栈如下:
SurfaceFlinger::processDisplayAdded()
└── getCompositionEngine().createDisplay(builder.build())
└── CompositionEngine::createDisplay()
└── compositionengine::impl::createDisplay()
└── createDisplayTemplated<Display>()
└── createOutputTemplated<Display>()
└── Display::setConfiguration()
2
3
4
5
6
7
- 参数构建
// SurfaceFlinger::processDisplayAdded() 函数中
compositionengine::DisplayCreationArgsBuilder builder;
// ... 设置各种参数
builder.setPixels(resolution);
builder.setIsSecure(state.isSecure);
builder.setIsProtected(state.isProtected);
builder.setHasPictureProcessing(state.hasPictureProcessing);
builder.setMaxLayerPictureProfiles(state.maxLayerPictureProfiles);
builder.setPowerAdvisor(mPowerAdvisor.get());
builder.setName(state.displayName);
auto compositionDisplay = getCompositionEngine().createDisplay(builder.build());
2
3
4
5
6
7
8
9
10
11
- 调用 CompositionEngine::createDisplay()
// /frameworks/native/services/surfaceflinger/CompositionEngine/src/CompositionEngine.cpp
std::shared_ptr<compositionengine::Display> CompositionEngine::createDisplay(
const DisplayCreationArgs& args) {
return compositionengine::impl::createDisplay(*this, args);
}
2
3
4
5
- 调用 impl::createDisplay()
// /frameworks/native/services/surfaceflinger/CompositionEngine/src/Display.cpp
std::shared_ptr<Display> createDisplay(
const compositionengine::CompositionEngine& compositionEngine,
const compositionengine::DisplayCreationArgs& args) {
return createDisplayTemplated<Display>(compositionEngine, args);
}
2
3
4
5
6
- 模板函数createDisplayTemplated()
// /frameworks/native/services/surfaceflinger/CompositionEngine/include/compositionengine/impl/Display.h
// This template factory function standardizes the implementation details of the
// final class using the types actually required by the implementation. This is
// not possible to do in the base class as those types may not even be visible
// to the base code.
// 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;
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
两个泛型对应的具体类型如下:
- BaseDisplay -> android::compositionengine::impl::Display
- CompositionEngine -> compositionengine::CompositionEngine
- createOutputTemplated() 创建Display对象
// /frameworks/native/services/surfaceflinger/CompositionEngine/include/compositionengine/impl/Output.h
// This template factory function standardizes the implementation details of the
// final class using the types actually required by the implementation. This is
// not possible to do in the base class as those types may not even be visible
// to the base code.
// 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) {
// 内部类
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 t`ests, 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)...);
}
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
两个泛型参数的具体类型:
- BaseOutput -> BaseDisplay -> android::compositionengine::impl::Display
- CompositionEngine -> compositionengine::CompositionEngine
整体过程:
- 创建一个内部 Output 类对象, 内部 Output 类继承自 BaseDisplay(android::compositionengine::impl::Display)
- Output 类构造函数中
- 设置 CompositionEngine 引用 :保存对 CompositionEngine 的引用
- 初始化状态管理 :创建 OutputCompositionState 和 OutputLayer 容器
- 返回 shared_ptr :返回创建的 Output 对象
- 配置新构建的 Output 对象
// frameworks/native/services/surfaceflinger/CompositionEngine/src/Display.cpp
void Display::setConfiguration(const compositionengine::DisplayCreationArgs& args) {
mId = args.id;
mPowerAdvisor = args.powerAdvisor;
mHasPictureProcessing = args.hasPictureProcessing;
mMaxLayerPictureProfiles = args.maxLayerPictureProfiles;
editState().isSecure = args.isSecure;
editState().isProtected = args.isProtected;
editState().displaySpace.setBounds(args.pixels);
setName(args.name);
}
2
3
4
5
6
7
8
9
10
11
# 3.2 FramebufferSurface 初始化
整体过程如下:
// 创建 BufferQueue、BufferProducer 和 BufferComsumer,用于对应屏幕的 GPU 合成
sp<compositionengine::DisplaySurface> displaySurface;
sp<IGraphicBufferProducer> producer;
sp<IGraphicBufferProducer> bqProducer; // producer 与 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);
// 创建 FramebufferSurface 对象
#if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(WB_CONSUMER_BASE_OWNS_BQ) // 走这个分支
const auto frameBufferSurface =
sp<FramebufferSurface>::make(getHwComposer(), *displayId, bqProducer, bqConsumer,
state.physical->activeMode->getResolution(),
ui::Size(maxGraphicsWidth, maxGraphicsHeight));
displaySurface = frameBufferSurface;
// ?
producer = frameBufferSurface->getSurface()->getIGraphicBufferProducer();
#else
displaySurface =
sp<FramebufferSurface>::make(getHwComposer(), *displayId, bqConsumer,
state.physical->activeMode->getResolution(),
ui::Size(maxGraphicsWidth, maxGraphicsHeight));
producer = bqProducer;
#endif // COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(WB_CONSUMER_BASE_OWNS_BQ)
}
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
FramebufferSurface 是 GPU 合成阶段的消费者端,这里完成其构建与初始化,为后续 GPU 合成做准备
- 调用 DefaultFactory::createBufferQueue 初始化 BBQ
// frameworks/native/services/surfaceflinger/SurfaceFlingerDefaultFactory.cpp
void DefaultFactory::createBufferQueue(sp<IGraphicBufferProducer>* outProducer,
sp<IGraphicBufferConsumer>* outConsumer,
bool consumerIsSurfaceFlinger) {
BufferQueue::createBufferQueue(outProducer, outConsumer, consumerIsSurfaceFlinger);
}
2
3
4
5
6
// 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");
// BufferQueueCore
sp<BufferQueueCore> core = sp<BufferQueueCore>::make();
LOG_ALWAYS_FATAL_IF(core == nullptr,
"BufferQueue: failed to create BufferQueueCore");
// BufferQueueProducer
sp<IGraphicBufferProducer> producer =
sp<BufferQueueProducer>::make(core, consumerIsSurfaceFlinger);
LOG_ALWAYS_FATAL_IF(producer == nullptr,
"BufferQueue: failed to create BufferQueueProducer");
// BufferQueueConsumer
sp<IGraphicBufferConsumer> consumer = sp<BufferQueueConsumer>::make(core);
LOG_ALWAYS_FATAL_IF(consumer == nullptr,
"BufferQueue: failed to create BufferQueueConsumer");
*outProducer = producer;
*outConsumer = consumer;
}
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
- 初始化 FramebufferSurface 对象
// frameworks/native/services/surfaceflinger/DisplayHardware/FramebufferSurface.cpp
FramebufferSurface::FramebufferSurface(HWComposer& hwc, PhysicalDisplayId displayId,
const sp<IGraphicBufferProducer>& producer,
const sp<IGraphicBufferConsumer>& consumer,
const ui::Size& size, const ui::Size& maxSize)
: ConsumerBase(producer, consumer),
// frameworks/native/libs/gui/ConsumerBase.cpp
// controlledByApp 传入默认值 false
ConsumerBase::ConsumerBase(const sp<IGraphicBufferProducer>& producer,
const sp<IGraphicBufferConsumer>& consumer, bool controlledByApp)
:
#if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(WB_UNLIMITED_SLOTS) //不走这个
mSlots(BufferQueueDefs::NUM_BUFFER_SLOTS),
#endif
mAbandoned(false),
mConsumer(consumer),
mSurface(sp<Surface>::make(producer, controlledByApp)), // 新建了一个 Surface
mPrevFinalReleaseFence(Fence::NO_FENCE) {
initialize(controlledByApp);
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# 3.3 DisplayDevice 初始化
接下来执行 SurfaceFlinger::setupNewDisplayDeviceInternal 函数,完成 DisplayDevice 的初始化。
// frameworks/native/services/surfaceflinger/SurfaceFlinger.cpp
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.isProtected = state.isProtected;
creationArgs.displaySurface = displaySurface;
creationArgs.hasWideColorGamut = false;
creationArgs.supportedPerFrameMetadata = 0;
if (const auto physicalIdOpt =
compositionDisplay->getDisplayIdVariant().and_then(asPhysicalDisplayId)) {
const auto physicalId = *physicalIdOpt;
creationArgs.isPrimary = physicalId == getPrimaryDisplayIdLocked();
creationArgs.refreshRateSelector =
FTL_FAKE_GUARD(kMainThreadContext,
mDisplayModeController.selectorPtrFor(physicalId));
creationArgs.physicalOrientation =
getPhysicalDisplayOrientation(physicalId, creationArgs.isPrimary);
ALOGV("Display Orientation: %s", toCString(creationArgs.physicalOrientation));
mPhysicalDisplays.get(physicalId)
.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(physicalId, mode));
}
}));
}
if (const auto id = compositionDisplay->getDisplayIdVariant().and_then(
asHalDisplayId<DisplayIdVariant>)) {
getHwComposer().getHdrCapabilities(*id, &creationArgs.hdrCapabilities);
creationArgs.supportedPerFrameMetadata = getHwComposer().getSupportedPerFrameMetadata(*id);
}
// 关注点1
auto nativeWindowSurface = getFactory().createNativeWindowSurface(producer);
auto nativeWindow = nativeWindowSurface->getNativeWindow(); // 返回 mSurface
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);
}
if (FlagManager::getInstance().correct_virtual_display_power_state()) {
creationArgs.initialPowerMode = state.initialPowerMode;
} else {
creationArgs.initialPowerMode =
state.isVirtual() ? hal::PowerMode::ON : hal::PowerMode::OFF;
}
creationArgs.requestedRefreshRate = state.requestedRefreshRate;
// 关注点2
sp<DisplayDevice> display = getFactory().createDisplayDevice(creationArgs);
// 关注点3
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});
if (const auto& physical = state.physical) {
const auto& mode = *physical->activeMode;
mDisplayModeController.setActiveMode(physical->id, mode.getId(), mode.getVsyncRate(),
mode.getPeakFps());
}
display->setLayerFilter(
makeLayerFilterForDisplay(display->getDisplayIdVariant(), state.layerStack));
display->setProjection(state.orientation, state.layerStackSpaceRect,
state.orientedDisplaySpaceRect);
display->setDisplayName(state.displayName);
display->setOptimizationPolicy(state.optimizationPolicy);
display->setFlags(state.flags);
return display;
}
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
关注点1,构建与初始化 NativeWindowSurface 对象
// /frameworks/native/services/surfaceflinger/SurfaceFlingerDefaultFactory.cpp
std::unique_ptr<surfaceflinger::NativeWindowSurface> DefaultFactory::createNativeWindowSurface(
const sp<IGraphicBufferProducer>& producer) {
return surfaceflinger::impl::createNativeWindowSurface(producer);
}
2
3
4
5
接着调用 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);
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
这里初始化了一个 Surface 对象:
class Surface
: public ANativeObjectBase<ANativeWindow, Surface, RefBase>
{
// ......
}
2
3
4
5
Surface 继承自 ANativeWindow,ANativeWindow 是 Android 原生窗口系统的核心结构,它为图形渲染提供了标准化的接口。
struct ANativeWindow {
struct android_native_base_t common; // 基础对象,包含引用计数
const uint32_t flags; // 描述表面属性的标志
const int minSwapInterval; // 最小交换间隔
const int maxSwapInterval; // 最大交换间隔
const float xdpi; // 水平分辨率 (DPI)
const float ydpi; // 垂直分辨率 (DPI)
intptr_t oem[4]; // OEM 厂商保留存储空间
void incStrong(const void* id) const; // 增加强引用计数
void decStrong(const void* id) const; // 减少强引用计数
// 缓冲区管理
// 获取缓冲区用于渲染
int (*dequeueBuffer)(struct ANativeWindow* window,
struct ANativeWindowBuffer** buffer, int* fenceFd);
// 提交渲染完成的缓冲区
int (*queueBuffer)(struct ANativeWindow* window,
struct ANativeWindowBuffer* buffer, int fenceFd);
// 取消已获取的缓冲区
int (*cancelBuffer)(struct ANativeWindow* window,
struct ANativeWindowBuffer* buffer, int fenceFd);
// 配置查询
// 查询窗口属性
/*
通过 query 函数可以获取的属性包括:
- NATIVE_WINDOW_WIDTH/HEIGHT : 窗口尺寸
- NATIVE_WINDOW_FORMAT : 缓冲区格式
- NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS : 最小未出队缓冲区数量
- NATIVE_WINDOW_CONSUMER_USAGE_BITS : 消费者使用标志
- NATIVE_WINDOW_TRANSFORM_HINT : 变换提示
- NATIVE_WINDOW_DEFAULT_DATASPACE : 默认数据空间
- NATIVE_WINDOW_BUFFER_AGE : 缓冲区年龄
- NATIVE_WINDOW_IS_VALID : 窗口是否有效
*/
int (*query)(const struct ANativeWindow* window, int what, int* value);
// 执行各种操作的通用接口
/*
通过 perform 函数可以执行的操作包括:
1.缓冲区配置
- NATIVE_WINDOW_SET_USAGE64 : 设置缓冲区使用标志
- NATIVE_WINDOW_SET_BUFFERS_FORMAT : 设置缓冲区格式
- NATIVE_WINDOW_SET_BUFFERS_DIMENSIONS : 设置缓冲区尺寸
- NATIVE_WINDOW_SET_BUFFERS_DATASPACE : 设置数据空间
- NATIVE_WINDOW_SET_BUFFER_COUNT : 设置缓冲区数量
1. 变换和裁剪
- NATIVE_WINDOW_SET_BUFFERS_TRANSFORM : 设置缓冲区变换
- NATIVE_WINDOW_SET_CROP : 设置裁剪区域
- NATIVE_WINDOW_SET_SCALING_MODE : 设置缩放模式
2. 高级功能
- NATIVE_WINDOW_SET_FRAME_RATE : 设置帧率
- NATIVE_WINDOW_ENABLE_FRAME_TIMESTAMPS : 启用帧时间戳
- NATIVE_WINDOW_SET_AUTO_REFRESH : 设置自动刷新
- NATIVE_WINDOW_SET_SHARED_BUFFER_MODE : 设置共享缓冲区模式
*/
int (*perform)(struct ANativeWindow* window, int operation, ...);
// 设置交换间隔
int (*setSwapInterval)(struct ANativeWindow* window, int interval);
}
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
典型的使用流程:
- 配置阶段 : 使用 perform 函数设置缓冲区属性
- 渲染循环 :
- 调用 dequeueBuffer 获取可用缓冲区
- 在缓冲区中进行渲染操作
- 调用 queueBuffer 提交渲染结果
- 清理阶段 : 必要时调用 cancelBuffer 取消缓冲区
ANativeWindow 是 Android 图形系统的基础抽象层,它为不同的渲染 API(EGL、Vulkan、CPU 渲染等)提供了统一的缓冲区管理接口,是连接应用程序渲染和系统显示的关键桥梁。
Surface 典型渲染流程:
- 连接阶段 : 调用 connect() 连接到 BufferQueue
- 配置阶段 : 设置缓冲区格式、尺寸、使用标志等
- 渲染循环 :
- 调用 dequeueBuffer() 获取缓冲区
- 在缓冲区中进行渲染(OpenGL、CPU 等)
- 调用 queueBuffer() 提交渲染结果
- 断开阶段 : 调用 disconnect() 清理资源
NativeWindowSurface 是一个函数内部类,继承自 surfaceflinger::NativeWindowSurface。
关注点2,构建与初始化 DisplayDevice 对象
// frameworks/native/services/surfaceflinger/SurfaceFlingerDefaultFactory.cpp
sp<DisplayDevice> DefaultFactory::createDisplayDevice(DisplayDeviceCreationArgs& creationArgs) {
return sp<DisplayDevice>::make(creationArgs);
}
2
3
4
// frameworks/native/services/surfaceflinger/DisplayDevice.cpp
DisplayDevice::DisplayDevice(DisplayDeviceCreationArgs& args)
: mFlinger(args.flinger),
mHwComposer(args.hwComposer),
mDisplayToken(args.displayToken),
mSequenceId(args.sequenceId),
mCompositionDisplay{args.compositionDisplay},
mPhysicalOrientation(args.physicalOrientation),
mPowerMode(ftl::Concat("PowerMode ", getId().value).c_str(), args.initialPowerMode),
mIsPrimary(args.isPrimary),
mRequestedRefreshRate(args.requestedRefreshRate),
mRefreshRateSelector(std::move(args.refreshRateSelector)) {
mCompositionDisplay->editState().isSecure = args.isSecure;
mCompositionDisplay->editState().isProtected = args.isProtected;
// 关注点
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();
setPowerMode(args.initialPowerMode);
// initialize the display orientation transform.
setProjection(ui::ROTATION_0, Rect::INVALID_RECT, Rect::INVALID_RECT);
}
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
构造函数中会调用 createRenderSurface 去构造一个 RenderSurface 对象,调用方是前面构造的 CompositionDisplay 对象:
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();
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
RenderSurface 构造函数如下:
RenderSurface::RenderSurface(const CompositionEngine& compositionEngine, Display& display,
const RenderSurfaceCreationArgs& args)
: mCompositionEngine(compositionEngine),
mDisplay(display),
mNativeWindow(args.nativeWindow), // 这里是前面初始化好的 Surface
mDisplaySurface(args.displaySurface),
mSize(args.displayWidth, args.displayHeight),
mMaxTextureCacheSize(args.maxTextureCacheSize) {
LOG_ALWAYS_FATAL_IF(!mNativeWindow);
}
2
3
4
5
6
7
8
9
10
- RenderSurface 会持有前面初始化好的 NativeWindowSurface 对象,NativeWindowSurface 持有 Surface 对象,Surface 对象持有 Producer 对象。所以 RenderSurface 间接持有了 BufferQueueProducer.
- RenderSurface mDisplaySurface 成员是前面初始化好的 FramebufferSurface 对象,FramebufferSurface 持有 BufferQueueConsumer 对象,所以 RenderSurface 间接持有了 BufferQueueConsumer.
关注点3,调用 preallocateBuffers 分配内存:
// /frameworks/native/services/surfaceflinger/NativeWindowSurface.cpp
void preallocateBuffers() override { mSurface->allocateBuffers(); }
// /frameworks/native/libs/gui/Surface.cpp
void Surface::allocateBuffers() {
uint32_t reqWidth = mReqWidth ? mReqWidth : mUserWidth;
uint32_t reqHeight = mReqHeight ? mReqHeight : mUserHeight;
// 通过 Producer 的接口去申请内存
mGraphicBufferProducer->allocateBuffers(reqWidth, reqHeight,
mReqFormat, mReqUsage);
}
2
3
4
5
6
7
8
9
10
11