blob: f32fb3a5c7201b1764b7f67b97360f171bb78242 [file] [log] [blame]
Marin Shalamanov3ea1d602020-12-16 19:59:39 +01001/*
2 * Copyright 2020 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#pragma once
18
Dominik Laskowskif6b4ba62021-11-09 12:46:10 -080019#include <cstddef>
20#include <memory>
Marin Shalamanov3ea1d602020-12-16 19:59:39 +010021
Marin Shalamanov5801c942020-12-17 17:00:13 +010022#include <android-base/stringprintf.h>
Marin Shalamanov3ea1d602020-12-16 19:59:39 +010023#include <android/configuration.h>
Dominik Laskowskib0054a22022-03-03 09:03:06 -080024#include <ftl/small_map.h>
Ady Abraham5e7ee862021-06-23 17:43:41 -070025#include <ui/DisplayId.h>
Marin Shalamanov228f46b2021-01-28 21:11:45 +010026#include <ui/DisplayMode.h>
Marin Shalamanov045b7002021-01-07 16:56:24 +010027#include <ui/Size.h>
Marin Shalamanov3ea1d602020-12-16 19:59:39 +010028#include <utils/Timers.h>
29
Dominik Laskowskif6b4ba62021-11-09 12:46:10 -080030#include <scheduler/Fps.h>
31
32#include "DisplayHardware/Hal.h"
Ady Abrahamd6d80162023-10-23 12:57:41 -070033#include "FlagManager.h"
Dominik Laskowskif6b4ba62021-11-09 12:46:10 -080034#include "Scheduler/StrongTyping.h"
ramindania04b8a52023-08-07 18:49:47 -070035
Marin Shalamanov3ea1d602020-12-16 19:59:39 +010036namespace android {
37
38namespace hal = android::hardware::graphics::composer::hal;
39
40class DisplayMode;
41using DisplayModePtr = std::shared_ptr<const DisplayMode>;
Dominik Laskowskib0054a22022-03-03 09:03:06 -080042
43// Prevent confusion with fps_approx_ops on the underlying Fps.
44bool operator<(const DisplayModePtr&, const DisplayModePtr&) = delete;
45bool operator>(const DisplayModePtr&, const DisplayModePtr&) = delete;
46bool operator<=(const DisplayModePtr&, const DisplayModePtr&) = delete;
47bool operator>=(const DisplayModePtr&, const DisplayModePtr&) = delete;
48
49using DisplayModeId = StrongTyping<ui::DisplayModeId, struct DisplayModeIdTag, Compare>;
50
51using DisplayModes = ftl::SmallMap<DisplayModeId, DisplayModePtr, 3>;
52using DisplayModeIterator = DisplayModes::const_iterator;
Marin Shalamanov3ea1d602020-12-16 19:59:39 +010053
54class DisplayMode {
55public:
56 class Builder {
57 public:
58 explicit Builder(hal::HWConfigId id) : mDisplayMode(new DisplayMode(id)) {}
59
60 DisplayModePtr build() {
61 return std::const_pointer_cast<const DisplayMode>(std::move(mDisplayMode));
62 }
63
Marin Shalamanov23c44202020-12-22 19:09:20 +010064 Builder& setId(DisplayModeId id) {
Marin Shalamanov3ea1d602020-12-16 19:59:39 +010065 mDisplayMode->mId = id;
66 return *this;
67 }
68
Ady Abraham5e7ee862021-06-23 17:43:41 -070069 Builder& setPhysicalDisplayId(PhysicalDisplayId id) {
70 mDisplayMode->mPhysicalDisplayId = id;
71 return *this;
72 }
73
Dominik Laskowskib0054a22022-03-03 09:03:06 -080074 Builder& setResolution(ui::Size resolution) {
75 mDisplayMode->mResolution = resolution;
Marin Shalamanov3ea1d602020-12-16 19:59:39 +010076 return *this;
77 }
78
Dominik Laskowskib0054a22022-03-03 09:03:06 -080079 Builder& setVsyncPeriod(nsecs_t vsyncPeriod) {
ramindania04b8a52023-08-07 18:49:47 -070080 mDisplayMode->mVsyncRate = Fps::fromPeriodNsecs(vsyncPeriod);
81 return *this;
82 }
83
84 Builder& setVrrConfig(std::optional<hal::VrrConfig> vrrConfig) {
85 mDisplayMode->mVrrConfig = std::move(vrrConfig);
Marin Shalamanov3ea1d602020-12-16 19:59:39 +010086 return *this;
87 }
88
ramindani0cd1d8d2023-06-13 13:43:23 -070089 Builder& setDpiX(float dpiX) {
90 if (dpiX == -1.f) {
Dominik Laskowskib0054a22022-03-03 09:03:06 -080091 mDisplayMode->mDpi.x = getDefaultDensity();
Marin Shalamanov3ea1d602020-12-16 19:59:39 +010092 } else {
ramindani0cd1d8d2023-06-13 13:43:23 -070093 mDisplayMode->mDpi.x = dpiX;
Marin Shalamanov3ea1d602020-12-16 19:59:39 +010094 }
95 return *this;
96 }
97
ramindani0cd1d8d2023-06-13 13:43:23 -070098 Builder& setDpiY(float dpiY) {
99 if (dpiY == -1.f) {
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800100 mDisplayMode->mDpi.y = getDefaultDensity();
Marin Shalamanov3ea1d602020-12-16 19:59:39 +0100101 } else {
ramindani0cd1d8d2023-06-13 13:43:23 -0700102 mDisplayMode->mDpi.y = dpiY;
Marin Shalamanov3ea1d602020-12-16 19:59:39 +0100103 }
104 return *this;
105 }
106
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100107 Builder& setGroup(int32_t group) {
108 mDisplayMode->mGroup = group;
Marin Shalamanov3ea1d602020-12-16 19:59:39 +0100109 return *this;
110 }
111
112 private:
113 float getDefaultDensity() {
114 // Default density is based on TVs: 1080p displays get XHIGH density, lower-
115 // resolution displays get TV density. Maybe eventually we'll need to update
116 // it for 4k displays, though hopefully those will just report accurate DPI
117 // information to begin with. This is also used for virtual displays and
118 // older HWC implementations, so be careful about orientation.
119
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800120 if (std::max(mDisplayMode->getWidth(), mDisplayMode->getHeight()) >= 1080) {
Marin Shalamanov3ea1d602020-12-16 19:59:39 +0100121 return ACONFIGURATION_DENSITY_XHIGH;
122 } else {
123 return ACONFIGURATION_DENSITY_TV;
124 }
125 }
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800126
Marin Shalamanov3ea1d602020-12-16 19:59:39 +0100127 std::shared_ptr<DisplayMode> mDisplayMode;
128 };
129
Marin Shalamanov23c44202020-12-22 19:09:20 +0100130 DisplayModeId getId() const { return mId; }
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800131
Marin Shalamanov3ea1d602020-12-16 19:59:39 +0100132 hal::HWConfigId getHwcId() const { return mHwcId; }
Ady Abraham5e7ee862021-06-23 17:43:41 -0700133 PhysicalDisplayId getPhysicalDisplayId() const { return mPhysicalDisplayId; }
Marin Shalamanov3ea1d602020-12-16 19:59:39 +0100134
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800135 ui::Size getResolution() const { return mResolution; }
136 int32_t getWidth() const { return mResolution.getWidth(); }
137 int32_t getHeight() const { return mResolution.getHeight(); }
138
ramindania04b8a52023-08-07 18:49:47 -0700139 // Peak refresh rate represents the highest refresh rate that can be used
140 // for the presentation.
141 Fps getPeakFps() const {
Ady Abrahamd6d80162023-10-23 12:57:41 -0700142 return FlagManager::getInstance().vrr_config() && mVrrConfig
ramindania04b8a52023-08-07 18:49:47 -0700143 ? Fps::fromPeriodNsecs(mVrrConfig->minFrameIntervalNs)
144 : mVsyncRate;
145 }
146
147 Fps getVsyncRate() const { return mVsyncRate; }
148
149 std::optional<hal::VrrConfig> getVrrConfig() const { return mVrrConfig; }
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800150
151 struct Dpi {
152 float x = -1;
153 float y = -1;
154
155 bool operator==(Dpi other) const { return x == other.x && y == other.y; }
156 };
157
158 Dpi getDpi() const { return mDpi; }
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100159
160 // Switches between modes in the same group are seamless, i.e.
161 // without visual interruptions such as a black screen.
162 int32_t getGroup() const { return mGroup; }
Marin Shalamanov3ea1d602020-12-16 19:59:39 +0100163
164private:
165 explicit DisplayMode(hal::HWConfigId id) : mHwcId(id) {}
166
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800167 const hal::HWConfigId mHwcId;
Marin Shalamanov23c44202020-12-22 19:09:20 +0100168 DisplayModeId mId;
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800169
Ady Abraham5e7ee862021-06-23 17:43:41 -0700170 PhysicalDisplayId mPhysicalDisplayId;
Marin Shalamanov3ea1d602020-12-16 19:59:39 +0100171
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800172 ui::Size mResolution;
ramindania04b8a52023-08-07 18:49:47 -0700173 Fps mVsyncRate;
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800174 Dpi mDpi;
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100175 int32_t mGroup = -1;
ramindania04b8a52023-08-07 18:49:47 -0700176 std::optional<hal::VrrConfig> mVrrConfig;
Marin Shalamanov3ea1d602020-12-16 19:59:39 +0100177};
178
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800179inline bool equalsExceptDisplayModeId(const DisplayMode& lhs, const DisplayMode& rhs) {
180 return lhs.getHwcId() == rhs.getHwcId() && lhs.getResolution() == rhs.getResolution() &&
ramindania04b8a52023-08-07 18:49:47 -0700181 lhs.getVsyncRate().getPeriodNsecs() == rhs.getVsyncRate().getPeriodNsecs() &&
182 lhs.getDpi() == rhs.getDpi() && lhs.getGroup() == rhs.getGroup();
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800183}
184
Marin Shalamanov5801c942020-12-17 17:00:13 +0100185inline std::string to_string(const DisplayMode& mode) {
ramindania04b8a52023-08-07 18:49:47 -0700186 return base::StringPrintf("{id=%d, hwcId=%d, resolution=%dx%d, vsyncRate=%s, "
187 "dpi=%.2fx%.2f, group=%d, vrrConfig=%s}",
Marin Shalamanov5801c942020-12-17 17:00:13 +0100188 mode.getId().value(), mode.getHwcId(), mode.getWidth(),
ramindania04b8a52023-08-07 18:49:47 -0700189 mode.getHeight(), to_string(mode.getVsyncRate()).c_str(),
190 mode.getDpi().x, mode.getDpi().y, mode.getGroup(),
191 to_string(mode.getVrrConfig()).c_str());
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800192}
193
194template <typename... DisplayModePtrs>
195inline DisplayModes makeModes(const DisplayModePtrs&... modePtrs) {
196 DisplayModes modes;
197 // Note: The omission of std::move(modePtrs) is intentional, because order of evaluation for
198 // arguments is unspecified.
199 (modes.try_emplace(modePtrs->getId(), modePtrs), ...);
200 return modes;
Marin Shalamanov5801c942020-12-17 17:00:13 +0100201}
202
Dominik Laskowskif6b4ba62021-11-09 12:46:10 -0800203} // namespace android