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