Ana Krulec | b43429d | 2019-01-09 14:28:51 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2019 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 | 9804183 | 2019-08-01 18:35:59 -0700 | [diff] [blame] | 19 | #include <android-base/stringprintf.h> |
| 20 | |
Ana Krulec | b43429d | 2019-01-09 14:28:51 -0800 | [diff] [blame] | 21 | #include <algorithm> |
| 22 | #include <numeric> |
Steven Thomas | d407190 | 2020-03-24 16:02:53 -0700 | [diff] [blame] | 23 | #include <optional> |
Dominik Laskowski | 9804183 | 2019-08-01 18:35:59 -0700 | [diff] [blame] | 24 | #include <type_traits> |
Ana Krulec | b43429d | 2019-01-09 14:28:51 -0800 | [diff] [blame] | 25 | |
Ana Krulec | 4593b69 | 2019-01-11 22:07:25 -0800 | [diff] [blame] | 26 | #include "DisplayHardware/HWComposer.h" |
Ady Abraham | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame] | 27 | #include "HwcStrongTypes.h" |
Ana Krulec | 4593b69 | 2019-01-11 22:07:25 -0800 | [diff] [blame] | 28 | #include "Scheduler/SchedulerUtils.h" |
Ady Abraham | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame] | 29 | #include "Scheduler/StrongTyping.h" |
Ana Krulec | 4593b69 | 2019-01-11 22:07:25 -0800 | [diff] [blame] | 30 | |
Dominik Laskowski | 9804183 | 2019-08-01 18:35:59 -0700 | [diff] [blame] | 31 | namespace android::scheduler { |
Ady Abraham | 4ccdcb4 | 2020-02-11 17:34:34 -0800 | [diff] [blame] | 32 | using namespace std::chrono_literals; |
Dominik Laskowski | 9804183 | 2019-08-01 18:35:59 -0700 | [diff] [blame] | 33 | |
| 34 | enum class RefreshRateConfigEvent : unsigned { None = 0b0, Changed = 0b1 }; |
| 35 | |
| 36 | inline RefreshRateConfigEvent operator|(RefreshRateConfigEvent lhs, RefreshRateConfigEvent rhs) { |
| 37 | using T = std::underlying_type_t<RefreshRateConfigEvent>; |
| 38 | return static_cast<RefreshRateConfigEvent>(static_cast<T>(lhs) | static_cast<T>(rhs)); |
| 39 | } |
Ana Krulec | b43429d | 2019-01-09 14:28:51 -0800 | [diff] [blame] | 40 | |
| 41 | /** |
Ady Abraham | 1902d07 | 2019-03-01 17:18:59 -0800 | [diff] [blame] | 42 | * This class is used to encapsulate configuration for refresh rates. It holds information |
Ana Krulec | b43429d | 2019-01-09 14:28:51 -0800 | [diff] [blame] | 43 | * about available refresh rates on the device, and the mapping between the numbers and human |
| 44 | * readable names. |
| 45 | */ |
| 46 | class RefreshRateConfigs { |
| 47 | public: |
Ady Abraham | 4ccdcb4 | 2020-02-11 17:34:34 -0800 | [diff] [blame] | 48 | // Margin used when matching refresh rates to the content desired ones. |
| 49 | static constexpr nsecs_t MARGIN_FOR_PERIOD_CALCULATION = |
| 50 | std::chrono::nanoseconds(800us).count(); |
| 51 | |
Ana Krulec | b43429d | 2019-01-09 14:28:51 -0800 | [diff] [blame] | 52 | struct RefreshRate { |
Ana Krulec | 72f0d6e | 2020-01-06 15:24:47 -0800 | [diff] [blame] | 53 | // The tolerance within which we consider FPS approximately equals. |
| 54 | static constexpr float FPS_EPSILON = 0.001f; |
| 55 | |
Ady Abraham | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame] | 56 | RefreshRate(HwcConfigIndexType configId, nsecs_t vsyncPeriod, |
| 57 | HwcConfigGroupType configGroup, std::string name, float fps) |
| 58 | : configId(configId), |
| 59 | vsyncPeriod(vsyncPeriod), |
| 60 | configGroup(configGroup), |
| 61 | name(std::move(name)), |
| 62 | fps(fps) {} |
Ady Abraham | 2e1dd89 | 2020-03-05 13:48:36 -0800 | [diff] [blame] | 63 | |
| 64 | RefreshRate(const RefreshRate&) = delete; |
Ana Krulec | b43429d | 2019-01-09 14:28:51 -0800 | [diff] [blame] | 65 | // This config ID corresponds to the position of the config in the vector that is stored |
| 66 | // on the device. |
Ady Abraham | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame] | 67 | const HwcConfigIndexType configId; |
Steven Thomas | 2bbaabe | 2019-08-28 16:08:35 -0700 | [diff] [blame] | 68 | // Vsync period in nanoseconds. |
Ady Abraham | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame] | 69 | const nsecs_t vsyncPeriod; |
| 70 | // This configGroup for the config. |
| 71 | const HwcConfigGroupType configGroup; |
| 72 | // Human readable name of the refresh rate. |
| 73 | const std::string name; |
| 74 | // Refresh rate in frames per second |
| 75 | const float fps = 0; |
| 76 | |
Ana Krulec | 72f0d6e | 2020-01-06 15:24:47 -0800 | [diff] [blame] | 77 | // Checks whether the fps of this RefreshRate struct is within a given min and max refresh |
| 78 | // rate passed in. FPS_EPSILON is applied to the boundaries for approximation. |
| 79 | bool inPolicy(float minRefreshRate, float maxRefreshRate) const { |
| 80 | return (fps >= (minRefreshRate - FPS_EPSILON) && fps <= (maxRefreshRate + FPS_EPSILON)); |
| 81 | } |
| 82 | |
Ady Abraham | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame] | 83 | bool operator!=(const RefreshRate& other) const { |
| 84 | return configId != other.configId || vsyncPeriod != other.vsyncPeriod || |
| 85 | configGroup != other.configGroup; |
| 86 | } |
| 87 | |
| 88 | bool operator==(const RefreshRate& other) const { return !(*this != other); } |
Ana Krulec | b43429d | 2019-01-09 14:28:51 -0800 | [diff] [blame] | 89 | }; |
| 90 | |
Ady Abraham | 2e1dd89 | 2020-03-05 13:48:36 -0800 | [diff] [blame] | 91 | using AllRefreshRatesMapType = |
| 92 | std::unordered_map<HwcConfigIndexType, std::unique_ptr<const RefreshRate>>; |
Ady Abraham | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame] | 93 | |
Steven Thomas | d407190 | 2020-03-24 16:02:53 -0700 | [diff] [blame] | 94 | struct Policy { |
| 95 | // The default config, used to ensure we only initiate display config switches within the |
| 96 | // same config group as defaultConfigId's group. |
| 97 | HwcConfigIndexType defaultConfig; |
| 98 | // The min and max FPS allowed by the policy. |
| 99 | float minRefreshRate = 0; |
| 100 | float maxRefreshRate = std::numeric_limits<float>::max(); |
| 101 | // Whether or not we switch config groups to get the best frame rate. Only used by tests. |
| 102 | bool allowGroupSwitching = false; |
| 103 | |
| 104 | bool operator==(const Policy& other) const { |
| 105 | return defaultConfig == other.defaultConfig && minRefreshRate == other.minRefreshRate && |
| 106 | maxRefreshRate == other.maxRefreshRate && |
| 107 | allowGroupSwitching == other.allowGroupSwitching; |
| 108 | } |
| 109 | |
| 110 | bool operator!=(const Policy& other) const { return !(*this == other); } |
| 111 | }; |
| 112 | |
| 113 | // Return code set*Policy() to indicate the current policy is unchanged. |
| 114 | static constexpr int CURRENT_POLICY_UNCHANGED = 1; |
| 115 | |
| 116 | // We maintain the display manager policy and the override policy separately. The override |
| 117 | // policy is used by CTS tests to get a consistent device state for testing. While the override |
| 118 | // policy is set, it takes precedence over the display manager policy. Once the override policy |
| 119 | // is cleared, we revert to using the display manager policy. |
| 120 | |
| 121 | // Sets the display manager policy to choose refresh rates. The return value will be: |
| 122 | // - A negative value if the policy is invalid or another error occurred. |
| 123 | // - NO_ERROR if the policy was successfully updated, and the current policy is different from |
| 124 | // what it was before the call. |
| 125 | // - CURRENT_POLICY_UNCHANGED if the policy was successfully updated, but the current policy |
| 126 | // is the same as it was before the call. |
| 127 | status_t setDisplayManagerPolicy(const Policy& policy) EXCLUDES(mLock); |
| 128 | // Sets the override policy. See setDisplayManagerPolicy() for the meaning of the return value. |
| 129 | status_t setOverridePolicy(const std::optional<Policy>& policy) EXCLUDES(mLock); |
| 130 | // Gets the current policy, which will be the override policy if active, and the display manager |
| 131 | // policy otherwise. |
| 132 | Policy getCurrentPolicy() const EXCLUDES(mLock); |
| 133 | // Gets the display manager policy, regardless of whether an override policy is active. |
| 134 | Policy getDisplayManagerPolicy() const EXCLUDES(mLock); |
Ana Krulec | ed3a8cc | 2019-11-14 00:55:07 +0100 | [diff] [blame] | 135 | |
| 136 | // Returns true if config is allowed by the current policy. |
| 137 | bool isConfigAllowed(HwcConfigIndexType config) const EXCLUDES(mLock); |
Ady Abraham | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame] | 138 | |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 139 | // Describes the different options the layer voted for refresh rate |
| 140 | enum class LayerVoteType { |
Ady Abraham | 71c437d | 2020-01-31 15:56:57 -0800 | [diff] [blame] | 141 | NoVote, // Doesn't care about the refresh rate |
| 142 | Min, // Minimal refresh rate available |
| 143 | Max, // Maximal refresh rate available |
| 144 | Heuristic, // Specific refresh rate that was calculated by platform using a heuristic |
| 145 | ExplicitDefault, // Specific refresh rate that was provided by the app with Default |
| 146 | // compatibility |
| 147 | ExplicitExactOrMultiple // Specific refresh rate that was provided by the app with |
| 148 | // ExactOrMultiple compatibility |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 149 | }; |
| 150 | |
| 151 | // Captures the layer requirements for a refresh rate. This will be used to determine the |
| 152 | // display refresh rate. |
| 153 | struct LayerRequirement { |
| 154 | std::string name; // Layer's name. Used for debugging purposes. |
| 155 | LayerVoteType vote; // Layer vote type. |
| 156 | float desiredRefreshRate; // Layer's desired refresh rate, if applicable. |
| 157 | float weight; // Layer's weight in the range of [0, 1]. The higher the weight the more |
| 158 | // impact this layer would have on choosing the refresh rate. |
| 159 | |
| 160 | bool operator==(const LayerRequirement& other) const { |
| 161 | return name == other.name && vote == other.vote && |
| 162 | desiredRefreshRate == other.desiredRefreshRate && weight == other.weight; |
| 163 | } |
| 164 | |
| 165 | bool operator!=(const LayerRequirement& other) const { return !(*this == other); } |
| 166 | }; |
| 167 | |
Ady Abraham | 4ccdcb4 | 2020-02-11 17:34:34 -0800 | [diff] [blame] | 168 | // Returns the refresh rate that fits best to the given layers. |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 169 | const RefreshRate& getRefreshRateForContent(const std::vector<LayerRequirement>& layers) const |
| 170 | EXCLUDES(mLock); |
| 171 | |
Ady Abraham | 4ccdcb4 | 2020-02-11 17:34:34 -0800 | [diff] [blame] | 172 | // Returns the refresh rate that fits best to the given layers. This function also gets a |
| 173 | // boolean flag that indicates whether user touched the screen recently to be factored in when |
Ady Abraham | 6fb599b | 2020-03-05 13:48:22 -0800 | [diff] [blame] | 174 | // choosing the refresh rate and returns whether the refresh rate was chosen as a result of |
| 175 | // a touch event. |
Ady Abraham | 4ccdcb4 | 2020-02-11 17:34:34 -0800 | [diff] [blame] | 176 | const RefreshRate& getRefreshRateForContentV2(const std::vector<LayerRequirement>& layers, |
Ady Abraham | 6fb599b | 2020-03-05 13:48:22 -0800 | [diff] [blame] | 177 | bool touchActive, bool* touchConsidered) const |
| 178 | EXCLUDES(mLock); |
Ana Krulec | b43429d | 2019-01-09 14:28:51 -0800 | [diff] [blame] | 179 | |
Ady Abraham | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame] | 180 | // Returns all the refresh rates supported by the device. This won't change at runtime. |
| 181 | const AllRefreshRatesMapType& getAllRefreshRates() const EXCLUDES(mLock); |
Steven Thomas | 2bbaabe | 2019-08-28 16:08:35 -0700 | [diff] [blame] | 182 | |
Ady Abraham | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame] | 183 | // Returns the lowest refresh rate supported by the device. This won't change at runtime. |
| 184 | const RefreshRate& getMinRefreshRate() const { return *mMinSupportedRefreshRate; } |
Steven Thomas | 2bbaabe | 2019-08-28 16:08:35 -0700 | [diff] [blame] | 185 | |
Ady Abraham | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame] | 186 | // Returns the lowest refresh rate according to the current policy. May change in runtime. |
| 187 | const RefreshRate& getMinRefreshRateByPolicy() const EXCLUDES(mLock); |
Steven Thomas | 2bbaabe | 2019-08-28 16:08:35 -0700 | [diff] [blame] | 188 | |
Ady Abraham | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame] | 189 | // Returns the highest refresh rate supported by the device. This won't change at runtime. |
| 190 | const RefreshRate& getMaxRefreshRate() const { return *mMaxSupportedRefreshRate; } |
Steven Thomas | 2bbaabe | 2019-08-28 16:08:35 -0700 | [diff] [blame] | 191 | |
Ady Abraham | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame] | 192 | // Returns the highest refresh rate according to the current policy. May change in runtime. |
| 193 | const RefreshRate& getMaxRefreshRateByPolicy() const EXCLUDES(mLock); |
| 194 | |
| 195 | // Returns the current refresh rate |
| 196 | const RefreshRate& getCurrentRefreshRate() const EXCLUDES(mLock); |
| 197 | |
Ana Krulec | 5d47791 | 2020-02-07 12:02:38 -0800 | [diff] [blame] | 198 | // Returns the current refresh rate, if allowed. Otherwise the default that is allowed by |
| 199 | // the policy. |
| 200 | const RefreshRate& getCurrentRefreshRateByPolicy() const; |
| 201 | |
Ady Abraham | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame] | 202 | // Returns the refresh rate that corresponds to a HwcConfigIndexType. This won't change at |
| 203 | // runtime. |
| 204 | const RefreshRate& getRefreshRateFromConfigId(HwcConfigIndexType configId) const { |
Ady Abraham | 2e1dd89 | 2020-03-05 13:48:36 -0800 | [diff] [blame] | 205 | return *mRefreshRates.at(configId); |
Ady Abraham | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame] | 206 | }; |
| 207 | |
| 208 | // Stores the current configId the device operates at |
| 209 | void setCurrentConfigId(HwcConfigIndexType configId) EXCLUDES(mLock); |
Dominik Laskowski | 22488f6 | 2019-03-28 09:53:04 -0700 | [diff] [blame] | 210 | |
Steven Thomas | 2bbaabe | 2019-08-28 16:08:35 -0700 | [diff] [blame] | 211 | struct InputConfig { |
Ady Abraham | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame] | 212 | HwcConfigIndexType configId = HwcConfigIndexType(0); |
| 213 | HwcConfigGroupType configGroup = HwcConfigGroupType(0); |
Steven Thomas | 2bbaabe | 2019-08-28 16:08:35 -0700 | [diff] [blame] | 214 | nsecs_t vsyncPeriod = 0; |
| 215 | }; |
Ana Krulec | 4593b69 | 2019-01-11 22:07:25 -0800 | [diff] [blame] | 216 | |
Ana Krulec | 3f6a206 | 2020-01-23 15:48:01 -0800 | [diff] [blame] | 217 | RefreshRateConfigs(const std::vector<InputConfig>& configs, |
Ady Abraham | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame] | 218 | HwcConfigIndexType currentHwcConfig); |
Ana Krulec | 3f6a206 | 2020-01-23 15:48:01 -0800 | [diff] [blame] | 219 | RefreshRateConfigs(const std::vector<std::shared_ptr<const HWC2::Display::Config>>& configs, |
Ady Abraham | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame] | 220 | HwcConfigIndexType currentConfigId); |
Ana Krulec | 4593b69 | 2019-01-11 22:07:25 -0800 | [diff] [blame] | 221 | |
Dominik Laskowski | 22488f6 | 2019-03-28 09:53:04 -0700 | [diff] [blame] | 222 | private: |
Ady Abraham | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame] | 223 | void init(const std::vector<InputConfig>& configs, HwcConfigIndexType currentHwcConfig); |
| 224 | |
| 225 | void constructAvailableRefreshRates() REQUIRES(mLock); |
| 226 | |
| 227 | void getSortedRefreshRateList( |
| 228 | const std::function<bool(const RefreshRate&)>& shouldAddRefreshRate, |
| 229 | std::vector<const RefreshRate*>* outRefreshRates); |
| 230 | |
Ady Abraham | 3470210 | 2020-02-10 14:12:05 -0800 | [diff] [blame] | 231 | // Returns the refresh rate with the highest score in the collection specified from begin |
| 232 | // to end. If there are more than one with the same highest refresh rate, the first one is |
| 233 | // returned. |
| 234 | template <typename Iter> |
| 235 | const RefreshRate* getBestRefreshRate(Iter begin, Iter end) const; |
| 236 | |
Ady Abraham | 4ccdcb4 | 2020-02-11 17:34:34 -0800 | [diff] [blame] | 237 | // Returns number of display frames and remainder when dividing the layer refresh period by |
| 238 | // display refresh period. |
| 239 | std::pair<nsecs_t, nsecs_t> getDisplayFrames(nsecs_t layerPeriod, nsecs_t displayPeriod) const; |
| 240 | |
Ana Krulec | 3d367c8 | 2020-02-25 15:02:01 -0800 | [diff] [blame] | 241 | // Returns the current refresh rate, if allowed. Otherwise the default that is allowed by |
| 242 | // the policy. |
| 243 | const RefreshRate& getCurrentRefreshRateByPolicyLocked() const REQUIRES(mLock); |
| 244 | |
Steven Thomas | d407190 | 2020-03-24 16:02:53 -0700 | [diff] [blame] | 245 | const Policy* getCurrentPolicyLocked() const REQUIRES(mLock); |
| 246 | bool isPolicyValid(const Policy& policy); |
| 247 | |
Steven Thomas | 2bbaabe | 2019-08-28 16:08:35 -0700 | [diff] [blame] | 248 | // The list of refresh rates, indexed by display config ID. This must not change after this |
| 249 | // object is initialized. |
Ady Abraham | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame] | 250 | AllRefreshRatesMapType mRefreshRates; |
| 251 | |
| 252 | // The list of refresh rates which are available in the current policy, ordered by vsyncPeriod |
| 253 | // (the first element is the lowest refresh rate) |
| 254 | std::vector<const RefreshRate*> mAvailableRefreshRates GUARDED_BY(mLock); |
| 255 | |
| 256 | // The current config. This will change at runtime. This is set by SurfaceFlinger on |
| 257 | // the main thread, and read by the Scheduler (and other objects) on other threads. |
| 258 | const RefreshRate* mCurrentRefreshRate GUARDED_BY(mLock); |
| 259 | |
Steven Thomas | d407190 | 2020-03-24 16:02:53 -0700 | [diff] [blame] | 260 | // The policy values will change at runtime. They're set by SurfaceFlinger on the main thread, |
| 261 | // and read by the Scheduler (and other objects) on other threads. |
| 262 | Policy mDisplayManagerPolicy GUARDED_BY(mLock); |
| 263 | std::optional<Policy> mOverridePolicy GUARDED_BY(mLock); |
Ady Abraham | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame] | 264 | |
| 265 | // The min and max refresh rates supported by the device. |
| 266 | // This will not change at runtime. |
| 267 | const RefreshRate* mMinSupportedRefreshRate; |
| 268 | const RefreshRate* mMaxSupportedRefreshRate; |
| 269 | |
Ady Abraham | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame] | 270 | mutable std::mutex mLock; |
Ana Krulec | b43429d | 2019-01-09 14:28:51 -0800 | [diff] [blame] | 271 | }; |
| 272 | |
Dominik Laskowski | 9804183 | 2019-08-01 18:35:59 -0700 | [diff] [blame] | 273 | } // namespace android::scheduler |