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