| 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> | 
| Ady Abraham | 62f216c | 2020-10-13 19:07:23 -0700 | [diff] [blame] | 20 | #include <gui/DisplayEventReceiver.h> | 
| Dominik Laskowski | 9804183 | 2019-08-01 18:35:59 -0700 | [diff] [blame] | 21 |  | 
| Ana Krulec | b43429d | 2019-01-09 14:28:51 -0800 | [diff] [blame] | 22 | #include <algorithm> | 
|  | 23 | #include <numeric> | 
| Steven Thomas | d407190 | 2020-03-24 16:02:53 -0700 | [diff] [blame] | 24 | #include <optional> | 
| Dominik Laskowski | 9804183 | 2019-08-01 18:35:59 -0700 | [diff] [blame] | 25 | #include <type_traits> | 
| Ana Krulec | b43429d | 2019-01-09 14:28:51 -0800 | [diff] [blame] | 26 |  | 
| Ana Krulec | 4593b69 | 2019-01-11 22:07:25 -0800 | [diff] [blame] | 27 | #include "DisplayHardware/HWComposer.h" | 
| Marin Shalamanov | e8a663d | 2020-11-24 17:48:00 +0100 | [diff] [blame] | 28 | #include "Fps.h" | 
| Ady Abraham | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame] | 29 | #include "HwcStrongTypes.h" | 
| Ana Krulec | 4593b69 | 2019-01-11 22:07:25 -0800 | [diff] [blame] | 30 | #include "Scheduler/SchedulerUtils.h" | 
| Marin Shalamanov | 53fc11d | 2020-11-20 14:00:13 +0100 | [diff] [blame] | 31 | #include "Scheduler/Seamlessness.h" | 
| Ady Abraham | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame] | 32 | #include "Scheduler/StrongTyping.h" | 
| Ana Krulec | 4593b69 | 2019-01-11 22:07:25 -0800 | [diff] [blame] | 33 |  | 
| Dominik Laskowski | 9804183 | 2019-08-01 18:35:59 -0700 | [diff] [blame] | 34 | namespace android::scheduler { | 
| Ady Abraham | abc2760 | 2020-04-08 17:20:29 -0700 | [diff] [blame] | 35 |  | 
| Ady Abraham | 4ccdcb4 | 2020-02-11 17:34:34 -0800 | [diff] [blame] | 36 | using namespace std::chrono_literals; | 
| Dominik Laskowski | 9804183 | 2019-08-01 18:35:59 -0700 | [diff] [blame] | 37 |  | 
|  | 38 | enum class RefreshRateConfigEvent : unsigned { None = 0b0, Changed = 0b1 }; | 
|  | 39 |  | 
|  | 40 | inline RefreshRateConfigEvent operator|(RefreshRateConfigEvent lhs, RefreshRateConfigEvent rhs) { | 
|  | 41 | using T = std::underlying_type_t<RefreshRateConfigEvent>; | 
|  | 42 | return static_cast<RefreshRateConfigEvent>(static_cast<T>(lhs) | static_cast<T>(rhs)); | 
|  | 43 | } | 
| Ana Krulec | b43429d | 2019-01-09 14:28:51 -0800 | [diff] [blame] | 44 |  | 
| Ady Abraham | 62f216c | 2020-10-13 19:07:23 -0700 | [diff] [blame] | 45 | using FrameRateOverride = DisplayEventReceiver::Event::FrameRateOverride; | 
|  | 46 |  | 
| Ana Krulec | b43429d | 2019-01-09 14:28:51 -0800 | [diff] [blame] | 47 | /** | 
| Ady Abraham | 1902d07 | 2019-03-01 17:18:59 -0800 | [diff] [blame] | 48 | * 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] | 49 | * about available refresh rates on the device, and the mapping between the numbers and human | 
|  | 50 | * readable names. | 
|  | 51 | */ | 
|  | 52 | class RefreshRateConfigs { | 
|  | 53 | public: | 
| Ady Abraham | 4ccdcb4 | 2020-02-11 17:34:34 -0800 | [diff] [blame] | 54 | // Margin used when matching refresh rates to the content desired ones. | 
|  | 55 | static constexpr nsecs_t MARGIN_FOR_PERIOD_CALCULATION = | 
|  | 56 | std::chrono::nanoseconds(800us).count(); | 
|  | 57 |  | 
| Ady Abraham | abc2760 | 2020-04-08 17:20:29 -0700 | [diff] [blame] | 58 | class RefreshRate { | 
|  | 59 | private: | 
|  | 60 | // Effectively making the constructor private while allowing | 
|  | 61 | // std::make_unique to create the object | 
|  | 62 | struct ConstructorTag { | 
|  | 63 | explicit ConstructorTag(int) {} | 
|  | 64 | }; | 
| Ana Krulec | 72f0d6e | 2020-01-06 15:24:47 -0800 | [diff] [blame] | 65 |  | 
| Ady Abraham | abc2760 | 2020-04-08 17:20:29 -0700 | [diff] [blame] | 66 | public: | 
|  | 67 | RefreshRate(HwcConfigIndexType configId, | 
| Marin Shalamanov | e8a663d | 2020-11-24 17:48:00 +0100 | [diff] [blame] | 68 | std::shared_ptr<const HWC2::Display::Config> config, Fps fps, ConstructorTag) | 
|  | 69 | : configId(configId), hwcConfig(config), fps(std::move(fps)) {} | 
| Ady Abraham | 2e1dd89 | 2020-03-05 13:48:36 -0800 | [diff] [blame] | 70 |  | 
|  | 71 | RefreshRate(const RefreshRate&) = delete; | 
| Ady Abraham | abc2760 | 2020-04-08 17:20:29 -0700 | [diff] [blame] | 72 |  | 
|  | 73 | HwcConfigIndexType getConfigId() const { return configId; } | 
|  | 74 | nsecs_t getVsyncPeriod() const { return hwcConfig->getVsyncPeriod(); } | 
|  | 75 | int32_t getConfigGroup() const { return hwcConfig->getConfigGroup(); } | 
| Marin Shalamanov | e8a663d | 2020-11-24 17:48:00 +0100 | [diff] [blame] | 76 | std::string getName() const { return to_string(fps); } | 
|  | 77 | Fps getFps() const { return fps; } | 
| Ady Abraham | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame] | 78 |  | 
| Ana Krulec | 72f0d6e | 2020-01-06 15:24:47 -0800 | [diff] [blame] | 79 | // Checks whether the fps of this RefreshRate struct is within a given min and max refresh | 
| Marin Shalamanov | e8a663d | 2020-11-24 17:48:00 +0100 | [diff] [blame] | 80 | // rate passed in. Margin of error is applied to the boundaries for approximation. | 
|  | 81 | bool inPolicy(Fps minRefreshRate, Fps maxRefreshRate) const { | 
|  | 82 | return minRefreshRate.lessThanOrEqualWithMargin(fps) && | 
|  | 83 | fps.lessThanOrEqualWithMargin(maxRefreshRate); | 
| Ana Krulec | 72f0d6e | 2020-01-06 15:24:47 -0800 | [diff] [blame] | 84 | } | 
|  | 85 |  | 
| Ady Abraham | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame] | 86 | bool operator!=(const RefreshRate& other) const { | 
| Ady Abraham | abc2760 | 2020-04-08 17:20:29 -0700 | [diff] [blame] | 87 | return configId != other.configId || hwcConfig != other.hwcConfig; | 
| Ady Abraham | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame] | 88 | } | 
|  | 89 |  | 
| Marin Shalamanov | e8a663d | 2020-11-24 17:48:00 +0100 | [diff] [blame] | 90 | bool operator<(const RefreshRate& other) const { | 
|  | 91 | return getFps().getValue() < other.getFps().getValue(); | 
|  | 92 | } | 
| Ana Krulec | b9afd79 | 2020-06-11 13:16:15 -0700 | [diff] [blame] | 93 |  | 
| Ady Abraham | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame] | 94 | bool operator==(const RefreshRate& other) const { return !(*this != other); } | 
| Ady Abraham | abc2760 | 2020-04-08 17:20:29 -0700 | [diff] [blame] | 95 |  | 
| Marin Shalamanov | 4608442 | 2020-10-13 12:33:42 +0200 | [diff] [blame] | 96 | std::string toString() const; | 
|  | 97 |  | 
| Ady Abraham | abc2760 | 2020-04-08 17:20:29 -0700 | [diff] [blame] | 98 | private: | 
|  | 99 | friend RefreshRateConfigs; | 
| Ady Abraham | b1b9d41 | 2020-06-01 19:53:52 -0700 | [diff] [blame] | 100 | friend class RefreshRateConfigsTest; | 
| Ady Abraham | abc2760 | 2020-04-08 17:20:29 -0700 | [diff] [blame] | 101 |  | 
| Ady Abraham | abc2760 | 2020-04-08 17:20:29 -0700 | [diff] [blame] | 102 | // This config ID corresponds to the position of the config in the vector that is stored | 
|  | 103 | // on the device. | 
|  | 104 | const HwcConfigIndexType configId; | 
|  | 105 | // The config itself | 
|  | 106 | std::shared_ptr<const HWC2::Display::Config> hwcConfig; | 
| Ady Abraham | abc2760 | 2020-04-08 17:20:29 -0700 | [diff] [blame] | 107 | // Refresh rate in frames per second | 
| Marin Shalamanov | e8a663d | 2020-11-24 17:48:00 +0100 | [diff] [blame] | 108 | const Fps fps{0.0f}; | 
| Ana Krulec | b43429d | 2019-01-09 14:28:51 -0800 | [diff] [blame] | 109 | }; | 
|  | 110 |  | 
| Ady Abraham | 2e1dd89 | 2020-03-05 13:48:36 -0800 | [diff] [blame] | 111 | using AllRefreshRatesMapType = | 
|  | 112 | std::unordered_map<HwcConfigIndexType, std::unique_ptr<const RefreshRate>>; | 
| Ady Abraham | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame] | 113 |  | 
| Steven Thomas | d407190 | 2020-03-24 16:02:53 -0700 | [diff] [blame] | 114 | struct Policy { | 
| Marin Shalamanov | 30b0b3c | 2020-10-13 19:15:06 +0200 | [diff] [blame] | 115 | private: | 
|  | 116 | static constexpr int kAllowGroupSwitchingDefault = false; | 
|  | 117 |  | 
|  | 118 | public: | 
| Steven Thomas | f734df4 | 2020-04-13 21:09:28 -0700 | [diff] [blame] | 119 | struct Range { | 
| Marin Shalamanov | e8a663d | 2020-11-24 17:48:00 +0100 | [diff] [blame] | 120 | Fps min{0.0f}; | 
|  | 121 | Fps max{std::numeric_limits<float>::max()}; | 
| Steven Thomas | f734df4 | 2020-04-13 21:09:28 -0700 | [diff] [blame] | 122 |  | 
|  | 123 | bool operator==(const Range& other) const { | 
| Marin Shalamanov | e8a663d | 2020-11-24 17:48:00 +0100 | [diff] [blame] | 124 | return min.equalsWithMargin(other.min) && max.equalsWithMargin(other.max); | 
| Steven Thomas | f734df4 | 2020-04-13 21:09:28 -0700 | [diff] [blame] | 125 | } | 
|  | 126 |  | 
|  | 127 | bool operator!=(const Range& other) const { return !(*this == other); } | 
| Marin Shalamanov | e8a663d | 2020-11-24 17:48:00 +0100 | [diff] [blame] | 128 |  | 
|  | 129 | std::string toString() const { | 
|  | 130 | return base::StringPrintf("[%s %s]", to_string(min).c_str(), | 
|  | 131 | to_string(max).c_str()); | 
|  | 132 | } | 
| Steven Thomas | f734df4 | 2020-04-13 21:09:28 -0700 | [diff] [blame] | 133 | }; | 
|  | 134 |  | 
| Steven Thomas | d407190 | 2020-03-24 16:02:53 -0700 | [diff] [blame] | 135 | // The default config, used to ensure we only initiate display config switches within the | 
|  | 136 | // same config group as defaultConfigId's group. | 
|  | 137 | HwcConfigIndexType defaultConfig; | 
| Marin Shalamanov | 30b0b3c | 2020-10-13 19:15:06 +0200 | [diff] [blame] | 138 | // Whether or not we switch config groups to get the best frame rate. | 
|  | 139 | bool allowGroupSwitching = kAllowGroupSwitchingDefault; | 
| Steven Thomas | f734df4 | 2020-04-13 21:09:28 -0700 | [diff] [blame] | 140 | // The primary refresh rate range represents display manager's general guidance on the | 
|  | 141 | // display configs we'll consider when switching refresh rates. Unless we get an explicit | 
|  | 142 | // signal from an app, we should stay within this range. | 
|  | 143 | Range primaryRange; | 
|  | 144 | // The app request refresh rate range allows us to consider more display configs when | 
|  | 145 | // switching refresh rates. Although we should generally stay within the primary range, | 
|  | 146 | // specific considerations, such as layer frame rate settings specified via the | 
|  | 147 | // setFrameRate() api, may cause us to go outside the primary range. We never go outside the | 
|  | 148 | // app request range. The app request range will be greater than or equal to the primary | 
|  | 149 | // refresh rate range, never smaller. | 
|  | 150 | Range appRequestRange; | 
| Steven Thomas | d407190 | 2020-03-24 16:02:53 -0700 | [diff] [blame] | 151 |  | 
| Steven Thomas | f734df4 | 2020-04-13 21:09:28 -0700 | [diff] [blame] | 152 | Policy() = default; | 
| Marin Shalamanov | 30b0b3c | 2020-10-13 19:15:06 +0200 | [diff] [blame] | 153 |  | 
| Steven Thomas | f734df4 | 2020-04-13 21:09:28 -0700 | [diff] [blame] | 154 | Policy(HwcConfigIndexType defaultConfig, const Range& range) | 
| Marin Shalamanov | 30b0b3c | 2020-10-13 19:15:06 +0200 | [diff] [blame] | 155 | : Policy(defaultConfig, kAllowGroupSwitchingDefault, range, range) {} | 
|  | 156 |  | 
|  | 157 | Policy(HwcConfigIndexType defaultConfig, bool allowGroupSwitching, const Range& range) | 
|  | 158 | : Policy(defaultConfig, allowGroupSwitching, range, range) {} | 
|  | 159 |  | 
| Steven Thomas | f734df4 | 2020-04-13 21:09:28 -0700 | [diff] [blame] | 160 | Policy(HwcConfigIndexType defaultConfig, const Range& primaryRange, | 
|  | 161 | const Range& appRequestRange) | 
| Marin Shalamanov | 30b0b3c | 2020-10-13 19:15:06 +0200 | [diff] [blame] | 162 | : Policy(defaultConfig, kAllowGroupSwitchingDefault, primaryRange, appRequestRange) {} | 
|  | 163 |  | 
|  | 164 | Policy(HwcConfigIndexType defaultConfig, bool allowGroupSwitching, | 
|  | 165 | const Range& primaryRange, const Range& appRequestRange) | 
| Steven Thomas | f734df4 | 2020-04-13 21:09:28 -0700 | [diff] [blame] | 166 | : defaultConfig(defaultConfig), | 
| Marin Shalamanov | 30b0b3c | 2020-10-13 19:15:06 +0200 | [diff] [blame] | 167 | allowGroupSwitching(allowGroupSwitching), | 
| Steven Thomas | f734df4 | 2020-04-13 21:09:28 -0700 | [diff] [blame] | 168 | primaryRange(primaryRange), | 
|  | 169 | appRequestRange(appRequestRange) {} | 
|  | 170 |  | 
| Steven Thomas | d407190 | 2020-03-24 16:02:53 -0700 | [diff] [blame] | 171 | bool operator==(const Policy& other) const { | 
| Steven Thomas | f734df4 | 2020-04-13 21:09:28 -0700 | [diff] [blame] | 172 | return defaultConfig == other.defaultConfig && primaryRange == other.primaryRange && | 
|  | 173 | appRequestRange == other.appRequestRange && | 
| Steven Thomas | d407190 | 2020-03-24 16:02:53 -0700 | [diff] [blame] | 174 | allowGroupSwitching == other.allowGroupSwitching; | 
|  | 175 | } | 
|  | 176 |  | 
|  | 177 | bool operator!=(const Policy& other) const { return !(*this == other); } | 
| Marin Shalamanov | b6674e7 | 2020-11-06 13:05:57 +0100 | [diff] [blame] | 178 | std::string toString() const; | 
| Steven Thomas | d407190 | 2020-03-24 16:02:53 -0700 | [diff] [blame] | 179 | }; | 
|  | 180 |  | 
|  | 181 | // Return code set*Policy() to indicate the current policy is unchanged. | 
|  | 182 | static constexpr int CURRENT_POLICY_UNCHANGED = 1; | 
|  | 183 |  | 
|  | 184 | // We maintain the display manager policy and the override policy separately. The override | 
|  | 185 | // policy is used by CTS tests to get a consistent device state for testing. While the override | 
|  | 186 | // policy is set, it takes precedence over the display manager policy. Once the override policy | 
|  | 187 | // is cleared, we revert to using the display manager policy. | 
|  | 188 |  | 
|  | 189 | // Sets the display manager policy to choose refresh rates. The return value will be: | 
|  | 190 | //   - A negative value if the policy is invalid or another error occurred. | 
|  | 191 | //   - NO_ERROR if the policy was successfully updated, and the current policy is different from | 
|  | 192 | //     what it was before the call. | 
|  | 193 | //   - CURRENT_POLICY_UNCHANGED if the policy was successfully updated, but the current policy | 
|  | 194 | //     is the same as it was before the call. | 
|  | 195 | status_t setDisplayManagerPolicy(const Policy& policy) EXCLUDES(mLock); | 
|  | 196 | // Sets the override policy. See setDisplayManagerPolicy() for the meaning of the return value. | 
|  | 197 | status_t setOverridePolicy(const std::optional<Policy>& policy) EXCLUDES(mLock); | 
|  | 198 | // Gets the current policy, which will be the override policy if active, and the display manager | 
|  | 199 | // policy otherwise. | 
|  | 200 | Policy getCurrentPolicy() const EXCLUDES(mLock); | 
|  | 201 | // Gets the display manager policy, regardless of whether an override policy is active. | 
|  | 202 | Policy getDisplayManagerPolicy() const EXCLUDES(mLock); | 
| Ana Krulec | ed3a8cc | 2019-11-14 00:55:07 +0100 | [diff] [blame] | 203 |  | 
|  | 204 | // Returns true if config is allowed by the current policy. | 
|  | 205 | bool isConfigAllowed(HwcConfigIndexType config) const EXCLUDES(mLock); | 
| Ady Abraham | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame] | 206 |  | 
| Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 207 | // Describes the different options the layer voted for refresh rate | 
|  | 208 | enum class LayerVoteType { | 
| Ady Abraham | 71c437d | 2020-01-31 15:56:57 -0800 | [diff] [blame] | 209 | NoVote,          // Doesn't care about the refresh rate | 
|  | 210 | Min,             // Minimal refresh rate available | 
|  | 211 | Max,             // Maximal refresh rate available | 
|  | 212 | Heuristic,       // Specific refresh rate that was calculated by platform using a heuristic | 
|  | 213 | ExplicitDefault, // Specific refresh rate that was provided by the app with Default | 
|  | 214 | // compatibility | 
|  | 215 | ExplicitExactOrMultiple // Specific refresh rate that was provided by the app with | 
|  | 216 | // ExactOrMultiple compatibility | 
| Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 217 | }; | 
|  | 218 |  | 
|  | 219 | // Captures the layer requirements for a refresh rate. This will be used to determine the | 
|  | 220 | // display refresh rate. | 
|  | 221 | struct LayerRequirement { | 
| Ady Abraham | aae5ed5 | 2020-06-26 09:32:43 -0700 | [diff] [blame] | 222 | // Layer's name. Used for debugging purposes. | 
|  | 223 | std::string name; | 
|  | 224 | // Layer vote type. | 
|  | 225 | LayerVoteType vote = LayerVoteType::NoVote; | 
|  | 226 | // Layer's desired refresh rate, if applicable. | 
| Marin Shalamanov | e8a663d | 2020-11-24 17:48:00 +0100 | [diff] [blame] | 227 | Fps desiredRefreshRate{0.0f}; | 
| Marin Shalamanov | 4608442 | 2020-10-13 12:33:42 +0200 | [diff] [blame] | 228 | // If a seamless mode switch is required. | 
| Marin Shalamanov | 53fc11d | 2020-11-20 14:00:13 +0100 | [diff] [blame] | 229 | Seamlessness seamlessness = Seamlessness::Default; | 
| Ady Abraham | aae5ed5 | 2020-06-26 09:32:43 -0700 | [diff] [blame] | 230 | // Layer's weight in the range of [0, 1]. The higher the weight the more impact this layer | 
|  | 231 | // would have on choosing the refresh rate. | 
|  | 232 | float weight = 0.0f; | 
|  | 233 | // Whether layer is in focus or not based on WindowManager's state | 
|  | 234 | bool focused = false; | 
| Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 235 |  | 
|  | 236 | bool operator==(const LayerRequirement& other) const { | 
|  | 237 | return name == other.name && vote == other.vote && | 
| Marin Shalamanov | e8a663d | 2020-11-24 17:48:00 +0100 | [diff] [blame] | 238 | desiredRefreshRate.equalsWithMargin(other.desiredRefreshRate) && | 
| Marin Shalamanov | be97cfa | 2020-12-01 16:02:07 +0100 | [diff] [blame] | 239 | seamlessness == other.seamlessness && weight == other.weight && | 
| Ady Abraham | aae5ed5 | 2020-06-26 09:32:43 -0700 | [diff] [blame] | 240 | focused == other.focused; | 
| Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 241 | } | 
|  | 242 |  | 
|  | 243 | bool operator!=(const LayerRequirement& other) const { return !(*this == other); } | 
|  | 244 | }; | 
|  | 245 |  | 
| Ady Abraham | dfd6216 | 2020-06-10 16:11:56 -0700 | [diff] [blame] | 246 | // Global state describing signals that affect refresh rate choice. | 
|  | 247 | struct GlobalSignals { | 
|  | 248 | // Whether the user touched the screen recently. Used to apply touch boost. | 
|  | 249 | bool touch = false; | 
|  | 250 | // True if the system hasn't seen any buffers posted to layers recently. | 
|  | 251 | bool idle = false; | 
|  | 252 | }; | 
|  | 253 |  | 
| Steven Thomas | bb37432 | 2020-04-28 22:47:16 -0700 | [diff] [blame] | 254 | // Returns the refresh rate that fits best to the given layers. | 
|  | 255 | //   layers - The layer requirements to consider. | 
| Ady Abraham | dfd6216 | 2020-06-10 16:11:56 -0700 | [diff] [blame] | 256 | //   globalSignals - global state of touch and idle | 
|  | 257 | //   outSignalsConsidered - An output param that tells the caller whether the refresh rate was | 
|  | 258 | //                          chosen based on touch boost and/or idle timer. | 
| Steven Thomas | bb37432 | 2020-04-28 22:47:16 -0700 | [diff] [blame] | 259 | const RefreshRate& getBestRefreshRate(const std::vector<LayerRequirement>& layers, | 
| Ady Abraham | dfd6216 | 2020-06-10 16:11:56 -0700 | [diff] [blame] | 260 | const GlobalSignals& globalSignals, | 
|  | 261 | GlobalSignals* outSignalsConsidered = nullptr) const | 
| Ady Abraham | 6fb599b | 2020-03-05 13:48:22 -0800 | [diff] [blame] | 262 | EXCLUDES(mLock); | 
| Ana Krulec | b43429d | 2019-01-09 14:28:51 -0800 | [diff] [blame] | 263 |  | 
| Ady Abraham | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame] | 264 | // Returns all the refresh rates supported by the device. This won't change at runtime. | 
|  | 265 | const AllRefreshRatesMapType& getAllRefreshRates() const EXCLUDES(mLock); | 
| Steven Thomas | 2bbaabe | 2019-08-28 16:08:35 -0700 | [diff] [blame] | 266 |  | 
| Ady Abraham | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame] | 267 | // Returns the lowest refresh rate supported by the device. This won't change at runtime. | 
|  | 268 | const RefreshRate& getMinRefreshRate() const { return *mMinSupportedRefreshRate; } | 
| Steven Thomas | 2bbaabe | 2019-08-28 16:08:35 -0700 | [diff] [blame] | 269 |  | 
| Steven Thomas | f734df4 | 2020-04-13 21:09:28 -0700 | [diff] [blame] | 270 | // Returns the lowest refresh rate according to the current policy. May change at runtime. Only | 
|  | 271 | // uses the primary range, not the app request range. | 
| Ady Abraham | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame] | 272 | const RefreshRate& getMinRefreshRateByPolicy() const EXCLUDES(mLock); | 
| Steven Thomas | 2bbaabe | 2019-08-28 16:08:35 -0700 | [diff] [blame] | 273 |  | 
| Ady Abraham | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame] | 274 | // Returns the highest refresh rate supported by the device. This won't change at runtime. | 
|  | 275 | const RefreshRate& getMaxRefreshRate() const { return *mMaxSupportedRefreshRate; } | 
| Steven Thomas | 2bbaabe | 2019-08-28 16:08:35 -0700 | [diff] [blame] | 276 |  | 
| Steven Thomas | f734df4 | 2020-04-13 21:09:28 -0700 | [diff] [blame] | 277 | // Returns the highest refresh rate according to the current policy. May change at runtime. Only | 
|  | 278 | // uses the primary range, not the app request range. | 
| Ady Abraham | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame] | 279 | const RefreshRate& getMaxRefreshRateByPolicy() const EXCLUDES(mLock); | 
|  | 280 |  | 
|  | 281 | // Returns the current refresh rate | 
|  | 282 | const RefreshRate& getCurrentRefreshRate() const EXCLUDES(mLock); | 
|  | 283 |  | 
| Ana Krulec | 5d47791 | 2020-02-07 12:02:38 -0800 | [diff] [blame] | 284 | // Returns the current refresh rate, if allowed. Otherwise the default that is allowed by | 
|  | 285 | // the policy. | 
|  | 286 | const RefreshRate& getCurrentRefreshRateByPolicy() const; | 
|  | 287 |  | 
| Ady Abraham | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame] | 288 | // Returns the refresh rate that corresponds to a HwcConfigIndexType. This won't change at | 
|  | 289 | // runtime. | 
|  | 290 | const RefreshRate& getRefreshRateFromConfigId(HwcConfigIndexType configId) const { | 
| Ady Abraham | 2e1dd89 | 2020-03-05 13:48:36 -0800 | [diff] [blame] | 291 | return *mRefreshRates.at(configId); | 
| Ady Abraham | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame] | 292 | }; | 
|  | 293 |  | 
|  | 294 | // Stores the current configId the device operates at | 
|  | 295 | void setCurrentConfigId(HwcConfigIndexType configId) EXCLUDES(mLock); | 
| Dominik Laskowski | 22488f6 | 2019-03-28 09:53:04 -0700 | [diff] [blame] | 296 |  | 
| Ady Abraham | a6b676e | 2020-05-27 14:29:09 -0700 | [diff] [blame] | 297 | // Returns a string that represents the layer vote type | 
|  | 298 | static std::string layerVoteTypeString(LayerVoteType vote); | 
|  | 299 |  | 
| Ady Abraham | b1b9d41 | 2020-06-01 19:53:52 -0700 | [diff] [blame] | 300 | // Returns a known frame rate that is the closest to frameRate | 
| Marin Shalamanov | e8a663d | 2020-11-24 17:48:00 +0100 | [diff] [blame] | 301 | Fps findClosestKnownFrameRate(Fps frameRate) const; | 
| Ady Abraham | b1b9d41 | 2020-06-01 19:53:52 -0700 | [diff] [blame] | 302 |  | 
| Ana Krulec | 3f6a206 | 2020-01-23 15:48:01 -0800 | [diff] [blame] | 303 | RefreshRateConfigs(const std::vector<std::shared_ptr<const HWC2::Display::Config>>& configs, | 
| Ady Abraham | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame] | 304 | HwcConfigIndexType currentConfigId); | 
| Ana Krulec | 4593b69 | 2019-01-11 22:07:25 -0800 | [diff] [blame] | 305 |  | 
| Dominik Laskowski | 983f2b5 | 2020-06-25 16:54:06 -0700 | [diff] [blame] | 306 | // Returns whether switching configs (refresh rate or resolution) is possible. | 
|  | 307 | // TODO(b/158780872): Consider HAL support, and skip frame rate detection if the configs only | 
|  | 308 | // differ in resolution. | 
|  | 309 | bool canSwitch() const { return mRefreshRates.size() > 1; } | 
|  | 310 |  | 
| Ana Krulec | b9afd79 | 2020-06-11 13:16:15 -0700 | [diff] [blame] | 311 | // Class to enumerate options around toggling the kernel timer on and off. We have an option | 
|  | 312 | // for no change to avoid extra calls to kernel. | 
|  | 313 | enum class KernelIdleTimerAction { | 
|  | 314 | NoChange, // Do not change the idle timer. | 
|  | 315 | TurnOff,  // Turn off the idle timer. | 
|  | 316 | TurnOn    // Turn on the idle timer. | 
|  | 317 | }; | 
|  | 318 | // Checks whether kernel idle timer should be active depending the policy decisions around | 
|  | 319 | // refresh rates. | 
|  | 320 | KernelIdleTimerAction getIdleTimerAction() const; | 
|  | 321 |  | 
| Ady Abraham | 0bb6a47 | 2020-10-12 10:22:13 -0700 | [diff] [blame] | 322 | // Stores the preferred refresh rate that an app should run at. | 
| Ady Abraham | 62f216c | 2020-10-13 19:07:23 -0700 | [diff] [blame] | 323 | // FrameRateOverride.refreshRateHz == 0 means no preference. | 
|  | 324 | void setPreferredRefreshRateForUid(FrameRateOverride) EXCLUDES(mLock); | 
| Ady Abraham | 0bb6a47 | 2020-10-12 10:22:13 -0700 | [diff] [blame] | 325 |  | 
|  | 326 | // Returns a divider for the current refresh rate | 
|  | 327 | int getRefreshRateDividerForUid(uid_t) const EXCLUDES(mLock); | 
|  | 328 |  | 
| Marin Shalamanov | ba421a8 | 2020-11-10 21:49:26 +0100 | [diff] [blame] | 329 | void dump(std::string& result) const EXCLUDES(mLock); | 
|  | 330 |  | 
| Ady Abraham | 62f216c | 2020-10-13 19:07:23 -0700 | [diff] [blame] | 331 | // Returns the current frame rate overrides | 
|  | 332 | std::vector<FrameRateOverride> getFrameRateOverrides() EXCLUDES(mLock); | 
|  | 333 |  | 
| Dominik Laskowski | 22488f6 | 2019-03-28 09:53:04 -0700 | [diff] [blame] | 334 | private: | 
| Ady Abraham | b1b9d41 | 2020-06-01 19:53:52 -0700 | [diff] [blame] | 335 | friend class RefreshRateConfigsTest; | 
|  | 336 |  | 
| Ady Abraham | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame] | 337 | void constructAvailableRefreshRates() REQUIRES(mLock); | 
| Marin Shalamanov | e8a663d | 2020-11-24 17:48:00 +0100 | [diff] [blame] | 338 | static std::vector<Fps> constructKnownFrameRates( | 
| Ady Abraham | b1b9d41 | 2020-06-01 19:53:52 -0700 | [diff] [blame] | 339 | const std::vector<std::shared_ptr<const HWC2::Display::Config>>& configs); | 
| Ady Abraham | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame] | 340 |  | 
|  | 341 | void getSortedRefreshRateList( | 
|  | 342 | const std::function<bool(const RefreshRate&)>& shouldAddRefreshRate, | 
|  | 343 | std::vector<const RefreshRate*>* outRefreshRates); | 
|  | 344 |  | 
| Ady Abraham | 3470210 | 2020-02-10 14:12:05 -0800 | [diff] [blame] | 345 | // Returns the refresh rate with the highest score in the collection specified from begin | 
|  | 346 | // to end. If there are more than one with the same highest refresh rate, the first one is | 
|  | 347 | // returned. | 
|  | 348 | template <typename Iter> | 
|  | 349 | const RefreshRate* getBestRefreshRate(Iter begin, Iter end) const; | 
|  | 350 |  | 
| Ady Abraham | 4ccdcb4 | 2020-02-11 17:34:34 -0800 | [diff] [blame] | 351 | // Returns number of display frames and remainder when dividing the layer refresh period by | 
|  | 352 | // display refresh period. | 
|  | 353 | std::pair<nsecs_t, nsecs_t> getDisplayFrames(nsecs_t layerPeriod, nsecs_t displayPeriod) const; | 
|  | 354 |  | 
| Steven Thomas | f734df4 | 2020-04-13 21:09:28 -0700 | [diff] [blame] | 355 | // Returns the lowest refresh rate according to the current policy. May change at runtime. Only | 
|  | 356 | // uses the primary range, not the app request range. | 
|  | 357 | const RefreshRate& getMinRefreshRateByPolicyLocked() const REQUIRES(mLock); | 
|  | 358 |  | 
|  | 359 | // Returns the highest refresh rate according to the current policy. May change at runtime. Only | 
|  | 360 | // uses the primary range, not the app request range. | 
|  | 361 | const RefreshRate& getMaxRefreshRateByPolicyLocked() const REQUIRES(mLock); | 
|  | 362 |  | 
| Ana Krulec | 3d367c8 | 2020-02-25 15:02:01 -0800 | [diff] [blame] | 363 | // Returns the current refresh rate, if allowed. Otherwise the default that is allowed by | 
|  | 364 | // the policy. | 
|  | 365 | const RefreshRate& getCurrentRefreshRateByPolicyLocked() const REQUIRES(mLock); | 
|  | 366 |  | 
| Steven Thomas | d407190 | 2020-03-24 16:02:53 -0700 | [diff] [blame] | 367 | const Policy* getCurrentPolicyLocked() const REQUIRES(mLock); | 
|  | 368 | bool isPolicyValid(const Policy& policy); | 
|  | 369 |  | 
| Steven Thomas | 2bbaabe | 2019-08-28 16:08:35 -0700 | [diff] [blame] | 370 | // The list of refresh rates, indexed by display config ID. This must not change after this | 
|  | 371 | // object is initialized. | 
| Ady Abraham | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame] | 372 | AllRefreshRatesMapType mRefreshRates; | 
|  | 373 |  | 
| Steven Thomas | f734df4 | 2020-04-13 21:09:28 -0700 | [diff] [blame] | 374 | // The list of refresh rates in the primary range of the current policy, ordered by vsyncPeriod | 
|  | 375 | // (the first element is the lowest refresh rate). | 
|  | 376 | std::vector<const RefreshRate*> mPrimaryRefreshRates GUARDED_BY(mLock); | 
|  | 377 |  | 
|  | 378 | // The list of refresh rates in the app request range of the current policy, ordered by | 
|  | 379 | // vsyncPeriod (the first element is the lowest refresh rate). | 
|  | 380 | std::vector<const RefreshRate*> mAppRequestRefreshRates GUARDED_BY(mLock); | 
| Ady Abraham | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame] | 381 |  | 
|  | 382 | // The current config. This will change at runtime. This is set by SurfaceFlinger on | 
|  | 383 | // the main thread, and read by the Scheduler (and other objects) on other threads. | 
|  | 384 | const RefreshRate* mCurrentRefreshRate GUARDED_BY(mLock); | 
|  | 385 |  | 
| Steven Thomas | d407190 | 2020-03-24 16:02:53 -0700 | [diff] [blame] | 386 | // The policy values will change at runtime. They're set by SurfaceFlinger on the main thread, | 
|  | 387 | // and read by the Scheduler (and other objects) on other threads. | 
|  | 388 | Policy mDisplayManagerPolicy GUARDED_BY(mLock); | 
|  | 389 | std::optional<Policy> mOverridePolicy GUARDED_BY(mLock); | 
| Ady Abraham | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame] | 390 |  | 
| Ady Abraham | 62f216c | 2020-10-13 19:07:23 -0700 | [diff] [blame] | 391 | // A mapping between a UID and a preferred refresh rate that this app would | 
|  | 392 | // run at. | 
| Marin Shalamanov | e8a663d | 2020-11-24 17:48:00 +0100 | [diff] [blame] | 393 | std::unordered_map<uid_t, Fps> mPreferredRefreshRateForUid GUARDED_BY(mLock); | 
| Ady Abraham | 0bb6a47 | 2020-10-12 10:22:13 -0700 | [diff] [blame] | 394 |  | 
| Ady Abraham | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame] | 395 | // The min and max refresh rates supported by the device. | 
|  | 396 | // This will not change at runtime. | 
|  | 397 | const RefreshRate* mMinSupportedRefreshRate; | 
|  | 398 | const RefreshRate* mMaxSupportedRefreshRate; | 
|  | 399 |  | 
| Ady Abraham | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame] | 400 | mutable std::mutex mLock; | 
| Ady Abraham | b1b9d41 | 2020-06-01 19:53:52 -0700 | [diff] [blame] | 401 |  | 
|  | 402 | // A sorted list of known frame rates that a Heuristic layer will choose | 
|  | 403 | // from based on the closest value. | 
| Marin Shalamanov | e8a663d | 2020-11-24 17:48:00 +0100 | [diff] [blame] | 404 | const std::vector<Fps> mKnownFrameRates; | 
| Ana Krulec | b43429d | 2019-01-09 14:28:51 -0800 | [diff] [blame] | 405 | }; | 
|  | 406 |  | 
| Dominik Laskowski | 9804183 | 2019-08-01 18:35:59 -0700 | [diff] [blame] | 407 | } // namespace android::scheduler |