1 /*
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
19 #include <cstddef>
20 #include <memory>
21
22 #include <android-base/stringprintf.h>
23 #include <android/configuration.h>
24 #include <ftl/mixins.h>
25 #include <ftl/small_map.h>
26 #include <ui/DisplayId.h>
27 #include <ui/DisplayMode.h>
28 #include <ui/Size.h>
29 #include <utils/Timers.h>
30
31 #include <common/FlagManager.h>
32 #include <scheduler/Fps.h>
33
34 #include "DisplayHardware/Hal.h"
35
36 namespace android {
37
38 namespace hal = android::hardware::graphics::composer::hal;
39
40 class DisplayMode;
41 using DisplayModePtr = std::shared_ptr<const DisplayMode>;
42
43 // Prevent confusion with fps_approx_ops on the underlying Fps.
44 bool operator<(const DisplayModePtr&, const DisplayModePtr&) = delete;
45 bool operator>(const DisplayModePtr&, const DisplayModePtr&) = delete;
46 bool operator<=(const DisplayModePtr&, const DisplayModePtr&) = delete;
47 bool operator>=(const DisplayModePtr&, const DisplayModePtr&) = delete;
48
49 struct DisplayModeId : ftl::DefaultConstructible<DisplayModeId, ui::DisplayModeId>,
50 ftl::Incrementable<DisplayModeId>,
51 ftl::Equatable<DisplayModeId>,
52 ftl::Orderable<DisplayModeId> {
53 using DefaultConstructible::DefaultConstructible;
54 };
55
56 using DisplayModes = ftl::SmallMap<DisplayModeId, DisplayModePtr, 3>;
57 using DisplayModeIterator = DisplayModes::const_iterator;
58
59 class DisplayMode {
60 public:
61 class Builder {
62 public:
Builder(hal::HWConfigId id)63 explicit Builder(hal::HWConfigId id) : mDisplayMode(new DisplayMode(id)) {}
64
build()65 DisplayModePtr build() {
66 return std::const_pointer_cast<const DisplayMode>(std::move(mDisplayMode));
67 }
68
setId(DisplayModeId id)69 Builder& setId(DisplayModeId id) {
70 mDisplayMode->mId = id;
71 return *this;
72 }
73
setPhysicalDisplayId(PhysicalDisplayId id)74 Builder& setPhysicalDisplayId(PhysicalDisplayId id) {
75 mDisplayMode->mPhysicalDisplayId = id;
76 return *this;
77 }
78
setResolution(ui::Size resolution)79 Builder& setResolution(ui::Size resolution) {
80 mDisplayMode->mResolution = resolution;
81 return *this;
82 }
83
setVsyncPeriod(nsecs_t vsyncPeriod)84 Builder& setVsyncPeriod(nsecs_t vsyncPeriod) {
85 mDisplayMode->mVsyncRate = Fps::fromPeriodNsecs(vsyncPeriod);
86 return *this;
87 }
88
setVrrConfig(std::optional<hal::VrrConfig> vrrConfig)89 Builder& setVrrConfig(std::optional<hal::VrrConfig> vrrConfig) {
90 mDisplayMode->mVrrConfig = std::move(vrrConfig);
91 return *this;
92 }
93
setDpiX(float dpiX)94 Builder& setDpiX(float dpiX) {
95 if (dpiX == -1.f) {
96 mDisplayMode->mDpi.x = getDefaultDensity();
97 } else {
98 mDisplayMode->mDpi.x = dpiX;
99 }
100 return *this;
101 }
102
setDpiY(float dpiY)103 Builder& setDpiY(float dpiY) {
104 if (dpiY == -1.f) {
105 mDisplayMode->mDpi.y = getDefaultDensity();
106 } else {
107 mDisplayMode->mDpi.y = dpiY;
108 }
109 return *this;
110 }
111
setGroup(int32_t group)112 Builder& setGroup(int32_t group) {
113 mDisplayMode->mGroup = group;
114 return *this;
115 }
116
117 private:
getDefaultDensity()118 float getDefaultDensity() {
119 // Default density is based on TVs: 1080p displays get XHIGH density, lower-
120 // resolution displays get TV density. Maybe eventually we'll need to update
121 // it for 4k displays, though hopefully those will just report accurate DPI
122 // information to begin with. This is also used for virtual displays and
123 // older HWC implementations, so be careful about orientation.
124
125 if (std::max(mDisplayMode->getWidth(), mDisplayMode->getHeight()) >= 1080) {
126 return ACONFIGURATION_DENSITY_XHIGH;
127 } else {
128 return ACONFIGURATION_DENSITY_TV;
129 }
130 }
131
132 std::shared_ptr<DisplayMode> mDisplayMode;
133 };
134
getId()135 DisplayModeId getId() const { return mId; }
136
getHwcId()137 hal::HWConfigId getHwcId() const { return mHwcId; }
getPhysicalDisplayId()138 PhysicalDisplayId getPhysicalDisplayId() const { return mPhysicalDisplayId; }
139
getResolution()140 ui::Size getResolution() const { return mResolution; }
getWidth()141 int32_t getWidth() const { return mResolution.getWidth(); }
getHeight()142 int32_t getHeight() const { return mResolution.getHeight(); }
143
144 // Peak refresh rate represents the highest refresh rate that can be used
145 // for the presentation.
getPeakFps()146 Fps getPeakFps() const {
147 return FlagManager::getInstance().vrr_config() && mVrrConfig
148 ? Fps::fromPeriodNsecs(mVrrConfig->minFrameIntervalNs)
149 : mVsyncRate;
150 }
151
getVsyncRate()152 Fps getVsyncRate() const { return mVsyncRate; }
153
getVrrConfig()154 std::optional<hal::VrrConfig> getVrrConfig() const { return mVrrConfig; }
155
156 struct Dpi {
157 float x = -1;
158 float y = -1;
159
160 bool operator==(Dpi other) const { return x == other.x && y == other.y; }
161 };
162
getDpi()163 Dpi getDpi() const { return mDpi; }
164
165 // Switches between modes in the same group are seamless, i.e.
166 // without visual interruptions such as a black screen.
getGroup()167 int32_t getGroup() const { return mGroup; }
168
169 private:
DisplayMode(hal::HWConfigId id)170 explicit DisplayMode(hal::HWConfigId id) : mHwcId(id) {}
171
172 const hal::HWConfigId mHwcId;
173 DisplayModeId mId;
174
175 PhysicalDisplayId mPhysicalDisplayId;
176
177 ui::Size mResolution;
178 Fps mVsyncRate;
179 Dpi mDpi;
180 int32_t mGroup = -1;
181 std::optional<hal::VrrConfig> mVrrConfig;
182 };
183
equalsExceptDisplayModeId(const DisplayMode & lhs,const DisplayMode & rhs)184 inline bool equalsExceptDisplayModeId(const DisplayMode& lhs, const DisplayMode& rhs) {
185 return lhs.getHwcId() == rhs.getHwcId() && lhs.getResolution() == rhs.getResolution() &&
186 lhs.getVsyncRate().getPeriodNsecs() == rhs.getVsyncRate().getPeriodNsecs() &&
187 lhs.getDpi() == rhs.getDpi() && lhs.getGroup() == rhs.getGroup();
188 }
189
to_string(const DisplayMode & mode)190 inline std::string to_string(const DisplayMode& mode) {
191 return base::StringPrintf("{id=%d, hwcId=%d, resolution=%dx%d, vsyncRate=%s, "
192 "dpi=%.2fx%.2f, group=%d, vrrConfig=%s}",
193 ftl::to_underlying(mode.getId()), mode.getHwcId(), mode.getWidth(),
194 mode.getHeight(), to_string(mode.getVsyncRate()).c_str(),
195 mode.getDpi().x, mode.getDpi().y, mode.getGroup(),
196 to_string(mode.getVrrConfig()).c_str());
197 }
198
199 template <typename... DisplayModePtrs>
makeModes(const DisplayModePtrs &...modePtrs)200 inline DisplayModes makeModes(const DisplayModePtrs&... modePtrs) {
201 DisplayModes modes;
202 // Note: The omission of std::move(modePtrs) is intentional, because order of evaluation for
203 // arguments is unspecified.
204 (modes.try_emplace(modePtrs->getId(), modePtrs), ...);
205 return modes;
206 }
207
208 } // namespace android
209