blob: 61a9a08a5cd5369fb405f10f155a3cdae029ecd7 [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"
33#include "Scheduler/StrongTyping.h"
Marin Shalamanov3ea1d602020-12-16 19:59:39 +010034
35namespace android {
36
37namespace hal = android::hardware::graphics::composer::hal;
38
39class DisplayMode;
40using DisplayModePtr = std::shared_ptr<const DisplayMode>;
Dominik Laskowskib0054a22022-03-03 09:03:06 -080041
42// Prevent confusion with fps_approx_ops on the underlying Fps.
43bool operator<(const DisplayModePtr&, const DisplayModePtr&) = delete;
44bool operator>(const DisplayModePtr&, const DisplayModePtr&) = delete;
45bool operator<=(const DisplayModePtr&, const DisplayModePtr&) = delete;
46bool operator>=(const DisplayModePtr&, const DisplayModePtr&) = delete;
47
48using DisplayModeId = StrongTyping<ui::DisplayModeId, struct DisplayModeIdTag, Compare>;
49
50using DisplayModes = ftl::SmallMap<DisplayModeId, DisplayModePtr, 3>;
51using DisplayModeIterator = DisplayModes::const_iterator;
Marin Shalamanov3ea1d602020-12-16 19:59:39 +010052
53class DisplayMode {
54public:
55 class Builder {
56 public:
57 explicit Builder(hal::HWConfigId id) : mDisplayMode(new DisplayMode(id)) {}
58
59 DisplayModePtr build() {
60 return std::const_pointer_cast<const DisplayMode>(std::move(mDisplayMode));
61 }
62
Marin Shalamanov23c44202020-12-22 19:09:20 +010063 Builder& setId(DisplayModeId id) {
Marin Shalamanov3ea1d602020-12-16 19:59:39 +010064 mDisplayMode->mId = id;
65 return *this;
66 }
67
Ady Abraham5e7ee862021-06-23 17:43:41 -070068 Builder& setPhysicalDisplayId(PhysicalDisplayId id) {
69 mDisplayMode->mPhysicalDisplayId = id;
70 return *this;
71 }
72
Dominik Laskowskib0054a22022-03-03 09:03:06 -080073 Builder& setResolution(ui::Size resolution) {
74 mDisplayMode->mResolution = resolution;
Marin Shalamanov3ea1d602020-12-16 19:59:39 +010075 return *this;
76 }
77
Dominik Laskowskib0054a22022-03-03 09:03:06 -080078 Builder& setVsyncPeriod(nsecs_t vsyncPeriod) {
Marin Shalamanov5801c942020-12-17 17:00:13 +010079 mDisplayMode->mFps = Fps::fromPeriodNsecs(vsyncPeriod);
Marin Shalamanov3ea1d602020-12-16 19:59:39 +010080 return *this;
81 }
82
83 Builder& setDpiX(int32_t dpiX) {
84 if (dpiX == -1) {
Dominik Laskowskib0054a22022-03-03 09:03:06 -080085 mDisplayMode->mDpi.x = getDefaultDensity();
Marin Shalamanov3ea1d602020-12-16 19:59:39 +010086 } else {
Dominik Laskowskib0054a22022-03-03 09:03:06 -080087 mDisplayMode->mDpi.x = dpiX / 1000.f;
Marin Shalamanov3ea1d602020-12-16 19:59:39 +010088 }
89 return *this;
90 }
91
92 Builder& setDpiY(int32_t dpiY) {
93 if (dpiY == -1) {
Dominik Laskowskib0054a22022-03-03 09:03:06 -080094 mDisplayMode->mDpi.y = getDefaultDensity();
Marin Shalamanov3ea1d602020-12-16 19:59:39 +010095 } else {
Dominik Laskowskib0054a22022-03-03 09:03:06 -080096 mDisplayMode->mDpi.y = dpiY / 1000.f;
Marin Shalamanov3ea1d602020-12-16 19:59:39 +010097 }
98 return *this;
99 }
100
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100101 Builder& setGroup(int32_t group) {
102 mDisplayMode->mGroup = group;
Marin Shalamanov3ea1d602020-12-16 19:59:39 +0100103 return *this;
104 }
105
106 private:
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
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800114 if (std::max(mDisplayMode->getWidth(), mDisplayMode->getHeight()) >= 1080) {
Marin Shalamanov3ea1d602020-12-16 19:59:39 +0100115 return ACONFIGURATION_DENSITY_XHIGH;
116 } else {
117 return ACONFIGURATION_DENSITY_TV;
118 }
119 }
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800120
Marin Shalamanov3ea1d602020-12-16 19:59:39 +0100121 std::shared_ptr<DisplayMode> mDisplayMode;
122 };
123
Marin Shalamanov23c44202020-12-22 19:09:20 +0100124 DisplayModeId getId() const { return mId; }
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800125
Marin Shalamanov3ea1d602020-12-16 19:59:39 +0100126 hal::HWConfigId getHwcId() const { return mHwcId; }
Ady Abraham5e7ee862021-06-23 17:43:41 -0700127 PhysicalDisplayId getPhysicalDisplayId() const { return mPhysicalDisplayId; }
Marin Shalamanov3ea1d602020-12-16 19:59:39 +0100128
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800129 ui::Size getResolution() const { return mResolution; }
130 int32_t getWidth() const { return mResolution.getWidth(); }
131 int32_t getHeight() const { return mResolution.getHeight(); }
132
Marin Shalamanov5801c942020-12-17 17:00:13 +0100133 Fps getFps() const { return mFps; }
134 nsecs_t getVsyncPeriod() const { return mFps.getPeriodNsecs(); }
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800135
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
143 Dpi getDpi() const { return mDpi; }
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100144
145 // Switches between modes in the same group are seamless, i.e.
146 // without visual interruptions such as a black screen.
147 int32_t getGroup() const { return mGroup; }
Marin Shalamanov3ea1d602020-12-16 19:59:39 +0100148
149private:
150 explicit DisplayMode(hal::HWConfigId id) : mHwcId(id) {}
151
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800152 const hal::HWConfigId mHwcId;
Marin Shalamanov23c44202020-12-22 19:09:20 +0100153 DisplayModeId mId;
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800154
Ady Abraham5e7ee862021-06-23 17:43:41 -0700155 PhysicalDisplayId mPhysicalDisplayId;
Marin Shalamanov3ea1d602020-12-16 19:59:39 +0100156
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800157 ui::Size mResolution;
Marin Shalamanov5801c942020-12-17 17:00:13 +0100158 Fps mFps;
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800159 Dpi mDpi;
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100160 int32_t mGroup = -1;
Marin Shalamanov3ea1d602020-12-16 19:59:39 +0100161};
162
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800163inline 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
Marin Shalamanov5801c942020-12-17 17:00:13 +0100169inline std::string to_string(const DisplayMode& mode) {
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800170 return base::StringPrintf("{id=%d, hwcId=%d, resolution=%dx%d, refreshRate=%s, "
171 "dpi=%.2fx%.2f, group=%d}",
Marin Shalamanov5801c942020-12-17 17:00:13 +0100172 mode.getId().value(), mode.getHwcId(), mode.getWidth(),
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800173 mode.getHeight(), to_string(mode.getFps()).c_str(), mode.getDpi().x,
174 mode.getDpi().y, mode.getGroup());
175}
176
177template <typename... DisplayModePtrs>
178inline 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;
Marin Shalamanov5801c942020-12-17 17:00:13 +0100184}
185
Dominik Laskowskif6b4ba62021-11-09 12:46:10 -0800186} // namespace android