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 | 43baf90 | 2023-11-17 18:13:11 -0500 | [diff] [blame] | 24 | #include <ftl/mixins.h> |
Dominik Laskowski | b0054a2 | 2022-03-03 09:03:06 -0800 | [diff] [blame] | 25 | #include <ftl/small_map.h> |
Ady Abraham | 5e7ee86 | 2021-06-23 17:43:41 -0700 | [diff] [blame] | 26 | #include <ui/DisplayId.h> |
Marin Shalamanov | 228f46b | 2021-01-28 21:11:45 +0100 | [diff] [blame] | 27 | #include <ui/DisplayMode.h> |
Marin Shalamanov | 045b700 | 2021-01-07 16:56:24 +0100 | [diff] [blame] | 28 | #include <ui/Size.h> |
Marin Shalamanov | 3ea1d60 | 2020-12-16 19:59:39 +0100 | [diff] [blame] | 29 | #include <utils/Timers.h> |
| 30 | |
Dominik Laskowski | 43baf90 | 2023-11-17 18:13:11 -0500 | [diff] [blame] | 31 | #include <common/FlagManager.h> |
Dominik Laskowski | f6b4ba6 | 2021-11-09 12:46:10 -0800 | [diff] [blame] | 32 | #include <scheduler/Fps.h> |
| 33 | |
Sasha McIntosh | f08ed64 | 2024-10-24 15:43:07 -0400 | [diff] [blame] | 34 | #include "Hal.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 | |
Sasha McIntosh | f08ed64 | 2024-10-24 15:43:07 -0400 | [diff] [blame] | 38 | using aidl::android::hardware::graphics::composer3::OutputType; |
Marin Shalamanov | 3ea1d60 | 2020-12-16 19:59:39 +0100 | [diff] [blame] | 39 | namespace hal = android::hardware::graphics::composer::hal; |
| 40 | |
| 41 | class DisplayMode; |
| 42 | using DisplayModePtr = std::shared_ptr<const DisplayMode>; |
Dominik Laskowski | b0054a2 | 2022-03-03 09:03:06 -0800 | [diff] [blame] | 43 | |
| 44 | // Prevent confusion with fps_approx_ops on the underlying Fps. |
| 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 | bool operator>=(const DisplayModePtr&, const DisplayModePtr&) = delete; |
| 49 | |
Dominik Laskowski | 43baf90 | 2023-11-17 18:13:11 -0500 | [diff] [blame] | 50 | struct DisplayModeId : ftl::DefaultConstructible<DisplayModeId, ui::DisplayModeId>, |
| 51 | ftl::Incrementable<DisplayModeId>, |
| 52 | ftl::Equatable<DisplayModeId>, |
| 53 | ftl::Orderable<DisplayModeId> { |
| 54 | using DefaultConstructible::DefaultConstructible; |
| 55 | }; |
Dominik Laskowski | b0054a2 | 2022-03-03 09:03:06 -0800 | [diff] [blame] | 56 | |
| 57 | using DisplayModes = ftl::SmallMap<DisplayModeId, DisplayModePtr, 3>; |
| 58 | using DisplayModeIterator = DisplayModes::const_iterator; |
Marin Shalamanov | 3ea1d60 | 2020-12-16 19:59:39 +0100 | [diff] [blame] | 59 | |
| 60 | class DisplayMode { |
| 61 | public: |
| 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 Shalamanov | 23c4420 | 2020-12-22 19:09:20 +0100 | [diff] [blame] | 70 | Builder& setId(DisplayModeId id) { |
Marin Shalamanov | 3ea1d60 | 2020-12-16 19:59:39 +0100 | [diff] [blame] | 71 | mDisplayMode->mId = id; |
| 72 | return *this; |
| 73 | } |
| 74 | |
Ady Abraham | 5e7ee86 | 2021-06-23 17:43:41 -0700 | [diff] [blame] | 75 | Builder& setPhysicalDisplayId(PhysicalDisplayId id) { |
| 76 | mDisplayMode->mPhysicalDisplayId = id; |
| 77 | return *this; |
| 78 | } |
| 79 | |
Dominik Laskowski | b0054a2 | 2022-03-03 09:03:06 -0800 | [diff] [blame] | 80 | Builder& setResolution(ui::Size resolution) { |
| 81 | mDisplayMode->mResolution = resolution; |
Marin Shalamanov | 3ea1d60 | 2020-12-16 19:59:39 +0100 | [diff] [blame] | 82 | return *this; |
| 83 | } |
| 84 | |
Dominik Laskowski | b0054a2 | 2022-03-03 09:03:06 -0800 | [diff] [blame] | 85 | Builder& setVsyncPeriod(nsecs_t vsyncPeriod) { |
ramindani | a04b8a5 | 2023-08-07 18:49:47 -0700 | [diff] [blame] | 86 | 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 Shalamanov | 3ea1d60 | 2020-12-16 19:59:39 +0100 | [diff] [blame] | 92 | return *this; |
| 93 | } |
| 94 | |
ramindani | 0cd1d8d | 2023-06-13 13:43:23 -0700 | [diff] [blame] | 95 | Builder& setDpiX(float dpiX) { |
| 96 | if (dpiX == -1.f) { |
Dominik Laskowski | b0054a2 | 2022-03-03 09:03:06 -0800 | [diff] [blame] | 97 | mDisplayMode->mDpi.x = getDefaultDensity(); |
Marin Shalamanov | 3ea1d60 | 2020-12-16 19:59:39 +0100 | [diff] [blame] | 98 | } else { |
ramindani | 0cd1d8d | 2023-06-13 13:43:23 -0700 | [diff] [blame] | 99 | mDisplayMode->mDpi.x = dpiX; |
Marin Shalamanov | 3ea1d60 | 2020-12-16 19:59:39 +0100 | [diff] [blame] | 100 | } |
| 101 | return *this; |
| 102 | } |
| 103 | |
ramindani | 0cd1d8d | 2023-06-13 13:43:23 -0700 | [diff] [blame] | 104 | Builder& setDpiY(float dpiY) { |
| 105 | if (dpiY == -1.f) { |
Dominik Laskowski | b0054a2 | 2022-03-03 09:03:06 -0800 | [diff] [blame] | 106 | mDisplayMode->mDpi.y = getDefaultDensity(); |
Marin Shalamanov | 3ea1d60 | 2020-12-16 19:59:39 +0100 | [diff] [blame] | 107 | } else { |
ramindani | 0cd1d8d | 2023-06-13 13:43:23 -0700 | [diff] [blame] | 108 | mDisplayMode->mDpi.y = dpiY; |
Marin Shalamanov | 3ea1d60 | 2020-12-16 19:59:39 +0100 | [diff] [blame] | 109 | } |
| 110 | return *this; |
| 111 | } |
| 112 | |
Marin Shalamanov | a7fe304 | 2021-01-29 21:02:08 +0100 | [diff] [blame] | 113 | Builder& setGroup(int32_t group) { |
| 114 | mDisplayMode->mGroup = group; |
Marin Shalamanov | 3ea1d60 | 2020-12-16 19:59:39 +0100 | [diff] [blame] | 115 | return *this; |
| 116 | } |
| 117 | |
Sasha McIntosh | f08ed64 | 2024-10-24 15:43:07 -0400 | [diff] [blame] | 118 | Builder& setHdrOutputType(OutputType type) { |
| 119 | mDisplayMode->mHdrOutputType = type; |
| 120 | return *this; |
| 121 | } |
| 122 | |
Marin Shalamanov | 3ea1d60 | 2020-12-16 19:59:39 +0100 | [diff] [blame] | 123 | 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 Laskowski | b0054a2 | 2022-03-03 09:03:06 -0800 | [diff] [blame] | 131 | if (std::max(mDisplayMode->getWidth(), mDisplayMode->getHeight()) >= 1080) { |
Marin Shalamanov | 3ea1d60 | 2020-12-16 19:59:39 +0100 | [diff] [blame] | 132 | return ACONFIGURATION_DENSITY_XHIGH; |
| 133 | } else { |
| 134 | return ACONFIGURATION_DENSITY_TV; |
| 135 | } |
| 136 | } |
Dominik Laskowski | b0054a2 | 2022-03-03 09:03:06 -0800 | [diff] [blame] | 137 | |
Marin Shalamanov | 3ea1d60 | 2020-12-16 19:59:39 +0100 | [diff] [blame] | 138 | std::shared_ptr<DisplayMode> mDisplayMode; |
| 139 | }; |
| 140 | |
Marin Shalamanov | 23c4420 | 2020-12-22 19:09:20 +0100 | [diff] [blame] | 141 | DisplayModeId getId() const { return mId; } |
Dominik Laskowski | b0054a2 | 2022-03-03 09:03:06 -0800 | [diff] [blame] | 142 | |
Marin Shalamanov | 3ea1d60 | 2020-12-16 19:59:39 +0100 | [diff] [blame] | 143 | hal::HWConfigId getHwcId() const { return mHwcId; } |
Ady Abraham | 5e7ee86 | 2021-06-23 17:43:41 -0700 | [diff] [blame] | 144 | PhysicalDisplayId getPhysicalDisplayId() const { return mPhysicalDisplayId; } |
Marin Shalamanov | 3ea1d60 | 2020-12-16 19:59:39 +0100 | [diff] [blame] | 145 | |
Dominik Laskowski | b0054a2 | 2022-03-03 09:03:06 -0800 | [diff] [blame] | 146 | ui::Size getResolution() const { return mResolution; } |
| 147 | int32_t getWidth() const { return mResolution.getWidth(); } |
| 148 | int32_t getHeight() const { return mResolution.getHeight(); } |
| 149 | |
ramindani | a04b8a5 | 2023-08-07 18:49:47 -0700 | [diff] [blame] | 150 | // Peak refresh rate represents the highest refresh rate that can be used |
| 151 | // for the presentation. |
| 152 | Fps getPeakFps() const { |
Ady Abraham | d6d8016 | 2023-10-23 12:57:41 -0700 | [diff] [blame] | 153 | return FlagManager::getInstance().vrr_config() && mVrrConfig |
ramindani | a04b8a5 | 2023-08-07 18:49:47 -0700 | [diff] [blame] | 154 | ? 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 Laskowski | b0054a2 | 2022-03-03 09:03:06 -0800 | [diff] [blame] | 161 | |
| 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 Shalamanov | a7fe304 | 2021-01-29 21:02:08 +0100 | [diff] [blame] | 170 | |
| 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 Shalamanov | 3ea1d60 | 2020-12-16 19:59:39 +0100 | [diff] [blame] | 174 | |
Sasha McIntosh | f08ed64 | 2024-10-24 15:43:07 -0400 | [diff] [blame] | 175 | OutputType getHdrOutputType() const { return mHdrOutputType; } |
| 176 | |
Marin Shalamanov | 3ea1d60 | 2020-12-16 19:59:39 +0100 | [diff] [blame] | 177 | private: |
| 178 | explicit DisplayMode(hal::HWConfigId id) : mHwcId(id) {} |
| 179 | |
Dominik Laskowski | b0054a2 | 2022-03-03 09:03:06 -0800 | [diff] [blame] | 180 | const hal::HWConfigId mHwcId; |
Marin Shalamanov | 23c4420 | 2020-12-22 19:09:20 +0100 | [diff] [blame] | 181 | DisplayModeId mId; |
Dominik Laskowski | b0054a2 | 2022-03-03 09:03:06 -0800 | [diff] [blame] | 182 | |
Ady Abraham | 5e7ee86 | 2021-06-23 17:43:41 -0700 | [diff] [blame] | 183 | PhysicalDisplayId mPhysicalDisplayId; |
Marin Shalamanov | 3ea1d60 | 2020-12-16 19:59:39 +0100 | [diff] [blame] | 184 | |
Dominik Laskowski | b0054a2 | 2022-03-03 09:03:06 -0800 | [diff] [blame] | 185 | ui::Size mResolution; |
ramindani | a04b8a5 | 2023-08-07 18:49:47 -0700 | [diff] [blame] | 186 | Fps mVsyncRate; |
Dominik Laskowski | b0054a2 | 2022-03-03 09:03:06 -0800 | [diff] [blame] | 187 | Dpi mDpi; |
Marin Shalamanov | a7fe304 | 2021-01-29 21:02:08 +0100 | [diff] [blame] | 188 | int32_t mGroup = -1; |
ramindani | a04b8a5 | 2023-08-07 18:49:47 -0700 | [diff] [blame] | 189 | std::optional<hal::VrrConfig> mVrrConfig; |
Sasha McIntosh | f08ed64 | 2024-10-24 15:43:07 -0400 | [diff] [blame] | 190 | OutputType mHdrOutputType; |
Marin Shalamanov | 3ea1d60 | 2020-12-16 19:59:39 +0100 | [diff] [blame] | 191 | }; |
| 192 | |
Dominik Laskowski | b0054a2 | 2022-03-03 09:03:06 -0800 | [diff] [blame] | 193 | inline bool equalsExceptDisplayModeId(const DisplayMode& lhs, const DisplayMode& rhs) { |
| 194 | return lhs.getHwcId() == rhs.getHwcId() && lhs.getResolution() == rhs.getResolution() && |
ramindani | a04b8a5 | 2023-08-07 18:49:47 -0700 | [diff] [blame] | 195 | lhs.getVsyncRate().getPeriodNsecs() == rhs.getVsyncRate().getPeriodNsecs() && |
Sasha McIntosh | f08ed64 | 2024-10-24 15:43:07 -0400 | [diff] [blame] | 196 | lhs.getDpi() == rhs.getDpi() && lhs.getGroup() == rhs.getGroup() && |
| 197 | lhs.getVrrConfig() == rhs.getVrrConfig() && |
| 198 | lhs.getHdrOutputType() == rhs.getHdrOutputType(); |
Dominik Laskowski | b0054a2 | 2022-03-03 09:03:06 -0800 | [diff] [blame] | 199 | } |
| 200 | |
Marin Shalamanov | 5801c94 | 2020-12-17 17:00:13 +0100 | [diff] [blame] | 201 | inline std::string to_string(const DisplayMode& mode) { |
ramindani | a04b8a5 | 2023-08-07 18:49:47 -0700 | [diff] [blame] | 202 | return base::StringPrintf("{id=%d, hwcId=%d, resolution=%dx%d, vsyncRate=%s, " |
Sasha McIntosh | f08ed64 | 2024-10-24 15:43:07 -0400 | [diff] [blame] | 203 | "dpi=%.2fx%.2f, group=%d, vrrConfig=%s, supportedHdrTypes=%s}", |
Dominik Laskowski | 43baf90 | 2023-11-17 18:13:11 -0500 | [diff] [blame] | 204 | ftl::to_underlying(mode.getId()), mode.getHwcId(), mode.getWidth(), |
ramindani | a04b8a5 | 2023-08-07 18:49:47 -0700 | [diff] [blame] | 205 | mode.getHeight(), to_string(mode.getVsyncRate()).c_str(), |
| 206 | mode.getDpi().x, mode.getDpi().y, mode.getGroup(), |
Sasha McIntosh | f08ed64 | 2024-10-24 15:43:07 -0400 | [diff] [blame] | 207 | to_string(mode.getVrrConfig()).c_str(), |
| 208 | toString(mode.getHdrOutputType()).c_str()); |
Dominik Laskowski | b0054a2 | 2022-03-03 09:03:06 -0800 | [diff] [blame] | 209 | } |
| 210 | |
| 211 | template <typename... DisplayModePtrs> |
| 212 | inline 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 Shalamanov | 5801c94 | 2020-12-17 17:00:13 +0100 | [diff] [blame] | 218 | } |
| 219 | |
Dominik Laskowski | f6b4ba6 | 2021-11-09 12:46:10 -0800 | [diff] [blame] | 220 | } // namespace android |