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