blob: e90b5b70cbbdeaf5e9c5661d54c25fa464c34798 [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 Laskowski43baf902023-11-17 18:13:11 -050024#include <ftl/mixins.h>
Dominik Laskowskib0054a22022-03-03 09:03:06 -080025#include <ftl/small_map.h>
Ady Abraham5e7ee862021-06-23 17:43:41 -070026#include <ui/DisplayId.h>
Marin Shalamanov228f46b2021-01-28 21:11:45 +010027#include <ui/DisplayMode.h>
Marin Shalamanov045b7002021-01-07 16:56:24 +010028#include <ui/Size.h>
Marin Shalamanov3ea1d602020-12-16 19:59:39 +010029#include <utils/Timers.h>
30
Dominik Laskowski43baf902023-11-17 18:13:11 -050031#include <common/FlagManager.h>
Dominik Laskowskif6b4ba62021-11-09 12:46:10 -080032#include <scheduler/Fps.h>
33
Sasha McIntoshf08ed642024-10-24 15:43:07 -040034#include "Hal.h"
ramindania04b8a52023-08-07 18:49:47 -070035
Marin Shalamanov3ea1d602020-12-16 19:59:39 +010036namespace android {
37
Sasha McIntoshf08ed642024-10-24 15:43:07 -040038using aidl::android::hardware::graphics::composer3::OutputType;
Marin Shalamanov3ea1d602020-12-16 19:59:39 +010039namespace hal = android::hardware::graphics::composer::hal;
40
41class DisplayMode;
42using DisplayModePtr = std::shared_ptr<const DisplayMode>;
Dominik Laskowskib0054a22022-03-03 09:03:06 -080043
44// Prevent confusion with fps_approx_ops on the underlying Fps.
45bool operator<(const DisplayModePtr&, const DisplayModePtr&) = delete;
46bool operator>(const DisplayModePtr&, const DisplayModePtr&) = delete;
47bool operator<=(const DisplayModePtr&, const DisplayModePtr&) = delete;
48bool operator>=(const DisplayModePtr&, const DisplayModePtr&) = delete;
49
Dominik Laskowski43baf902023-11-17 18:13:11 -050050struct DisplayModeId : ftl::DefaultConstructible<DisplayModeId, ui::DisplayModeId>,
51 ftl::Incrementable<DisplayModeId>,
52 ftl::Equatable<DisplayModeId>,
53 ftl::Orderable<DisplayModeId> {
54 using DefaultConstructible::DefaultConstructible;
55};
Dominik Laskowskib0054a22022-03-03 09:03:06 -080056
57using DisplayModes = ftl::SmallMap<DisplayModeId, DisplayModePtr, 3>;
58using DisplayModeIterator = DisplayModes::const_iterator;
Marin Shalamanov3ea1d602020-12-16 19:59:39 +010059
60class DisplayMode {
61public:
62 class Builder {
63 public:
64 explicit Builder(hal::HWConfigId id) : mDisplayMode(new DisplayMode(id)) {}
65
66 DisplayModePtr build() {
67 return std::const_pointer_cast<const DisplayMode>(std::move(mDisplayMode));
68 }
69
Marin Shalamanov23c44202020-12-22 19:09:20 +010070 Builder& setId(DisplayModeId id) {
Marin Shalamanov3ea1d602020-12-16 19:59:39 +010071 mDisplayMode->mId = id;
72 return *this;
73 }
74
Ady Abraham5e7ee862021-06-23 17:43:41 -070075 Builder& setPhysicalDisplayId(PhysicalDisplayId id) {
76 mDisplayMode->mPhysicalDisplayId = id;
77 return *this;
78 }
79
Dominik Laskowskib0054a22022-03-03 09:03:06 -080080 Builder& setResolution(ui::Size resolution) {
81 mDisplayMode->mResolution = resolution;
Marin Shalamanov3ea1d602020-12-16 19:59:39 +010082 return *this;
83 }
84
Dominik Laskowskib0054a22022-03-03 09:03:06 -080085 Builder& setVsyncPeriod(nsecs_t vsyncPeriod) {
ramindania04b8a52023-08-07 18:49:47 -070086 mDisplayMode->mVsyncRate = Fps::fromPeriodNsecs(vsyncPeriod);
87 return *this;
88 }
89
90 Builder& setVrrConfig(std::optional<hal::VrrConfig> vrrConfig) {
91 mDisplayMode->mVrrConfig = std::move(vrrConfig);
Marin Shalamanov3ea1d602020-12-16 19:59:39 +010092 return *this;
93 }
94
ramindani0cd1d8d2023-06-13 13:43:23 -070095 Builder& setDpiX(float dpiX) {
96 if (dpiX == -1.f) {
Dominik Laskowskib0054a22022-03-03 09:03:06 -080097 mDisplayMode->mDpi.x = getDefaultDensity();
Marin Shalamanov3ea1d602020-12-16 19:59:39 +010098 } else {
ramindani0cd1d8d2023-06-13 13:43:23 -070099 mDisplayMode->mDpi.x = dpiX;
Marin Shalamanov3ea1d602020-12-16 19:59:39 +0100100 }
101 return *this;
102 }
103
ramindani0cd1d8d2023-06-13 13:43:23 -0700104 Builder& setDpiY(float dpiY) {
105 if (dpiY == -1.f) {
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800106 mDisplayMode->mDpi.y = getDefaultDensity();
Marin Shalamanov3ea1d602020-12-16 19:59:39 +0100107 } else {
ramindani0cd1d8d2023-06-13 13:43:23 -0700108 mDisplayMode->mDpi.y = dpiY;
Marin Shalamanov3ea1d602020-12-16 19:59:39 +0100109 }
110 return *this;
111 }
112
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100113 Builder& setGroup(int32_t group) {
114 mDisplayMode->mGroup = group;
Marin Shalamanov3ea1d602020-12-16 19:59:39 +0100115 return *this;
116 }
117
Sasha McIntoshf08ed642024-10-24 15:43:07 -0400118 Builder& setHdrOutputType(OutputType type) {
119 mDisplayMode->mHdrOutputType = type;
120 return *this;
121 }
122
Marin Shalamanov3ea1d602020-12-16 19:59:39 +0100123 private:
124 float getDefaultDensity() {
125 // Default density is based on TVs: 1080p displays get XHIGH density, lower-
126 // resolution displays get TV density. Maybe eventually we'll need to update
127 // it for 4k displays, though hopefully those will just report accurate DPI
128 // information to begin with. This is also used for virtual displays and
129 // older HWC implementations, so be careful about orientation.
130
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800131 if (std::max(mDisplayMode->getWidth(), mDisplayMode->getHeight()) >= 1080) {
Marin Shalamanov3ea1d602020-12-16 19:59:39 +0100132 return ACONFIGURATION_DENSITY_XHIGH;
133 } else {
134 return ACONFIGURATION_DENSITY_TV;
135 }
136 }
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800137
Marin Shalamanov3ea1d602020-12-16 19:59:39 +0100138 std::shared_ptr<DisplayMode> mDisplayMode;
139 };
140
Marin Shalamanov23c44202020-12-22 19:09:20 +0100141 DisplayModeId getId() const { return mId; }
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800142
Marin Shalamanov3ea1d602020-12-16 19:59:39 +0100143 hal::HWConfigId getHwcId() const { return mHwcId; }
Ady Abraham5e7ee862021-06-23 17:43:41 -0700144 PhysicalDisplayId getPhysicalDisplayId() const { return mPhysicalDisplayId; }
Marin Shalamanov3ea1d602020-12-16 19:59:39 +0100145
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800146 ui::Size getResolution() const { return mResolution; }
147 int32_t getWidth() const { return mResolution.getWidth(); }
148 int32_t getHeight() const { return mResolution.getHeight(); }
149
ramindania04b8a52023-08-07 18:49:47 -0700150 // Peak refresh rate represents the highest refresh rate that can be used
151 // for the presentation.
152 Fps getPeakFps() const {
Ady Abrahamd6d80162023-10-23 12:57:41 -0700153 return FlagManager::getInstance().vrr_config() && mVrrConfig
ramindania04b8a52023-08-07 18:49:47 -0700154 ? Fps::fromPeriodNsecs(mVrrConfig->minFrameIntervalNs)
155 : mVsyncRate;
156 }
157
158 Fps getVsyncRate() const { return mVsyncRate; }
159
160 std::optional<hal::VrrConfig> getVrrConfig() const { return mVrrConfig; }
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800161
162 struct Dpi {
163 float x = -1;
164 float y = -1;
165
166 bool operator==(Dpi other) const { return x == other.x && y == other.y; }
167 };
168
169 Dpi getDpi() const { return mDpi; }
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100170
171 // Switches between modes in the same group are seamless, i.e.
172 // without visual interruptions such as a black screen.
173 int32_t getGroup() const { return mGroup; }
Marin Shalamanov3ea1d602020-12-16 19:59:39 +0100174
Sasha McIntoshf08ed642024-10-24 15:43:07 -0400175 OutputType getHdrOutputType() const { return mHdrOutputType; }
176
Marin Shalamanov3ea1d602020-12-16 19:59:39 +0100177private:
178 explicit DisplayMode(hal::HWConfigId id) : mHwcId(id) {}
179
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800180 const hal::HWConfigId mHwcId;
Marin Shalamanov23c44202020-12-22 19:09:20 +0100181 DisplayModeId mId;
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800182
Ady Abraham5e7ee862021-06-23 17:43:41 -0700183 PhysicalDisplayId mPhysicalDisplayId;
Marin Shalamanov3ea1d602020-12-16 19:59:39 +0100184
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800185 ui::Size mResolution;
ramindania04b8a52023-08-07 18:49:47 -0700186 Fps mVsyncRate;
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800187 Dpi mDpi;
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100188 int32_t mGroup = -1;
ramindania04b8a52023-08-07 18:49:47 -0700189 std::optional<hal::VrrConfig> mVrrConfig;
Sasha McIntoshf08ed642024-10-24 15:43:07 -0400190 OutputType mHdrOutputType;
Marin Shalamanov3ea1d602020-12-16 19:59:39 +0100191};
192
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800193inline bool equalsExceptDisplayModeId(const DisplayMode& lhs, const DisplayMode& rhs) {
194 return lhs.getHwcId() == rhs.getHwcId() && lhs.getResolution() == rhs.getResolution() &&
ramindania04b8a52023-08-07 18:49:47 -0700195 lhs.getVsyncRate().getPeriodNsecs() == rhs.getVsyncRate().getPeriodNsecs() &&
Sasha McIntoshf08ed642024-10-24 15:43:07 -0400196 lhs.getDpi() == rhs.getDpi() && lhs.getGroup() == rhs.getGroup() &&
197 lhs.getVrrConfig() == rhs.getVrrConfig() &&
198 lhs.getHdrOutputType() == rhs.getHdrOutputType();
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800199}
200
Marin Shalamanov5801c942020-12-17 17:00:13 +0100201inline std::string to_string(const DisplayMode& mode) {
ramindania04b8a52023-08-07 18:49:47 -0700202 return base::StringPrintf("{id=%d, hwcId=%d, resolution=%dx%d, vsyncRate=%s, "
Sasha McIntoshf08ed642024-10-24 15:43:07 -0400203 "dpi=%.2fx%.2f, group=%d, vrrConfig=%s, supportedHdrTypes=%s}",
Dominik Laskowski43baf902023-11-17 18:13:11 -0500204 ftl::to_underlying(mode.getId()), mode.getHwcId(), mode.getWidth(),
ramindania04b8a52023-08-07 18:49:47 -0700205 mode.getHeight(), to_string(mode.getVsyncRate()).c_str(),
206 mode.getDpi().x, mode.getDpi().y, mode.getGroup(),
Sasha McIntoshf08ed642024-10-24 15:43:07 -0400207 to_string(mode.getVrrConfig()).c_str(),
208 toString(mode.getHdrOutputType()).c_str());
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800209}
210
211template <typename... DisplayModePtrs>
212inline DisplayModes makeModes(const DisplayModePtrs&... modePtrs) {
213 DisplayModes modes;
214 // Note: The omission of std::move(modePtrs) is intentional, because order of evaluation for
215 // arguments is unspecified.
216 (modes.try_emplace(modePtrs->getId(), modePtrs), ...);
217 return modes;
Marin Shalamanov5801c942020-12-17 17:00:13 +0100218}
219
Dominik Laskowskif6b4ba62021-11-09 12:46:10 -0800220} // namespace android