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