| 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 |  | 
| Marin Shalamanov | 3ea1d60 | 2020-12-16 19:59:39 +0100 | [diff] [blame] | 27 | #include "DisplayHardware/DisplayMode.h" | 
| Ana Krulec | 4593b69 | 2019-01-11 22:07:25 -0800 | [diff] [blame] | 28 | #include "DisplayHardware/HWComposer.h" | 
| Marin Shalamanov | e8a663d | 2020-11-24 17:48:00 +0100 | [diff] [blame] | 29 | #include "Fps.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 = | 
| rnlee | 3bd61066 | 2021-06-23 16:27:57 -0700 | [diff] [blame] | 56 | std::chrono::nanoseconds(800us).count(); | 
| Ady Abraham | 4ccdcb4 | 2020-02-11 17:34:34 -0800 | [diff] [blame] | 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: | 
| Marin Shalamanov | a7fe304 | 2021-01-29 21:02:08 +0100 | [diff] [blame] | 67 | RefreshRate(DisplayModeId modeId, DisplayModePtr mode, Fps fps, ConstructorTag) | 
|  | 68 | : modeId(modeId), mode(mode), fps(std::move(fps)) {} | 
| Ady Abraham | 2e1dd89 | 2020-03-05 13:48:36 -0800 | [diff] [blame] | 69 |  | 
| Marin Shalamanov | a7fe304 | 2021-01-29 21:02:08 +0100 | [diff] [blame] | 70 | DisplayModeId getModeId() const { return modeId; } | 
|  | 71 | nsecs_t getVsyncPeriod() const { return mode->getVsyncPeriod(); } | 
|  | 72 | int32_t getModeGroup() const { return mode->getGroup(); } | 
| Marin Shalamanov | e8a663d | 2020-11-24 17:48:00 +0100 | [diff] [blame] | 73 | std::string getName() const { return to_string(fps); } | 
|  | 74 | Fps getFps() const { return fps; } | 
| Ady Abraham | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame] | 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 | 
| Marin Shalamanov | e8a663d | 2020-11-24 17:48:00 +0100 | [diff] [blame] | 77 | // rate passed in. Margin of error is applied to the boundaries for approximation. | 
|  | 78 | bool inPolicy(Fps minRefreshRate, Fps maxRefreshRate) const { | 
|  | 79 | return minRefreshRate.lessThanOrEqualWithMargin(fps) && | 
|  | 80 | fps.lessThanOrEqualWithMargin(maxRefreshRate); | 
| Ana Krulec | 72f0d6e | 2020-01-06 15:24:47 -0800 | [diff] [blame] | 81 | } | 
|  | 82 |  | 
| Ady Abraham | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame] | 83 | bool operator!=(const RefreshRate& other) const { | 
| Marin Shalamanov | a7fe304 | 2021-01-29 21:02:08 +0100 | [diff] [blame] | 84 | return modeId != other.modeId || mode != other.mode; | 
| Ady Abraham | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame] | 85 | } | 
|  | 86 |  | 
| Marin Shalamanov | e8a663d | 2020-11-24 17:48:00 +0100 | [diff] [blame] | 87 | bool operator<(const RefreshRate& other) const { | 
|  | 88 | return getFps().getValue() < other.getFps().getValue(); | 
|  | 89 | } | 
| Ana Krulec | b9afd79 | 2020-06-11 13:16:15 -0700 | [diff] [blame] | 90 |  | 
| Ady Abraham | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame] | 91 | bool operator==(const RefreshRate& other) const { return !(*this != other); } | 
| Ady Abraham | abc2760 | 2020-04-08 17:20:29 -0700 | [diff] [blame] | 92 |  | 
| Marin Shalamanov | 4608442 | 2020-10-13 12:33:42 +0200 | [diff] [blame] | 93 | std::string toString() const; | 
| Marin Shalamanov | 65ce551 | 2021-01-22 20:57:13 +0100 | [diff] [blame] | 94 | friend std::ostream& operator<<(std::ostream& os, const RefreshRate& refreshRate) { | 
|  | 95 | return os << refreshRate.toString(); | 
|  | 96 | } | 
| Marin Shalamanov | 4608442 | 2020-10-13 12:33:42 +0200 | [diff] [blame] | 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 |  | 
| Marin Shalamanov | a7fe304 | 2021-01-29 21:02:08 +0100 | [diff] [blame] | 102 | const DisplayModeId modeId; | 
|  | 103 | DisplayModePtr mode; | 
| Ady Abraham | abc2760 | 2020-04-08 17:20:29 -0700 | [diff] [blame] | 104 | // Refresh rate in frames per second | 
| Marin Shalamanov | e8a663d | 2020-11-24 17:48:00 +0100 | [diff] [blame] | 105 | const Fps fps{0.0f}; | 
| Ana Krulec | b43429d | 2019-01-09 14:28:51 -0800 | [diff] [blame] | 106 | }; | 
|  | 107 |  | 
| Ady Abraham | 2e1dd89 | 2020-03-05 13:48:36 -0800 | [diff] [blame] | 108 | using AllRefreshRatesMapType = | 
| Marin Shalamanov | 23c4420 | 2020-12-22 19:09:20 +0100 | [diff] [blame] | 109 | std::unordered_map<DisplayModeId, std::unique_ptr<const RefreshRate>>; | 
| Ady Abraham | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame] | 110 |  | 
| Marin Shalamanov | eadf2e7 | 2020-12-10 15:35:28 +0100 | [diff] [blame] | 111 | struct FpsRange { | 
|  | 112 | Fps min{0.0f}; | 
|  | 113 | Fps max{std::numeric_limits<float>::max()}; | 
|  | 114 |  | 
|  | 115 | bool operator==(const FpsRange& other) const { | 
|  | 116 | return min.equalsWithMargin(other.min) && max.equalsWithMargin(other.max); | 
|  | 117 | } | 
|  | 118 |  | 
|  | 119 | bool operator!=(const FpsRange& other) const { return !(*this == other); } | 
|  | 120 |  | 
|  | 121 | std::string toString() const { | 
|  | 122 | return base::StringPrintf("[%s %s]", to_string(min).c_str(), to_string(max).c_str()); | 
|  | 123 | } | 
|  | 124 | }; | 
|  | 125 |  | 
| Steven Thomas | d407190 | 2020-03-24 16:02:53 -0700 | [diff] [blame] | 126 | struct Policy { | 
| Marin Shalamanov | 30b0b3c | 2020-10-13 19:15:06 +0200 | [diff] [blame] | 127 | private: | 
|  | 128 | static constexpr int kAllowGroupSwitchingDefault = false; | 
|  | 129 |  | 
|  | 130 | public: | 
| Marin Shalamanov | a7fe304 | 2021-01-29 21:02:08 +0100 | [diff] [blame] | 131 | // The default mode, used to ensure we only initiate display mode switches within the | 
|  | 132 | // same mode group as defaultMode's group. | 
|  | 133 | DisplayModeId defaultMode; | 
|  | 134 | // Whether or not we switch mode groups to get the best frame rate. | 
| Marin Shalamanov | 30b0b3c | 2020-10-13 19:15:06 +0200 | [diff] [blame] | 135 | bool allowGroupSwitching = kAllowGroupSwitchingDefault; | 
| Steven Thomas | f734df4 | 2020-04-13 21:09:28 -0700 | [diff] [blame] | 136 | // The primary refresh rate range represents display manager's general guidance on the | 
| Marin Shalamanov | a7fe304 | 2021-01-29 21:02:08 +0100 | [diff] [blame] | 137 | // display modes we'll consider when switching refresh rates. Unless we get an explicit | 
| Steven Thomas | f734df4 | 2020-04-13 21:09:28 -0700 | [diff] [blame] | 138 | // signal from an app, we should stay within this range. | 
| Marin Shalamanov | eadf2e7 | 2020-12-10 15:35:28 +0100 | [diff] [blame] | 139 | FpsRange primaryRange; | 
| Marin Shalamanov | a7fe304 | 2021-01-29 21:02:08 +0100 | [diff] [blame] | 140 | // The app request refresh rate range allows us to consider more display modes when | 
| Steven Thomas | f734df4 | 2020-04-13 21:09:28 -0700 | [diff] [blame] | 141 | // switching refresh rates. Although we should generally stay within the primary range, | 
|  | 142 | // specific considerations, such as layer frame rate settings specified via the | 
|  | 143 | // setFrameRate() api, may cause us to go outside the primary range. We never go outside the | 
|  | 144 | // app request range. The app request range will be greater than or equal to the primary | 
|  | 145 | // refresh rate range, never smaller. | 
| Marin Shalamanov | eadf2e7 | 2020-12-10 15:35:28 +0100 | [diff] [blame] | 146 | FpsRange appRequestRange; | 
| Steven Thomas | d407190 | 2020-03-24 16:02:53 -0700 | [diff] [blame] | 147 |  | 
| Steven Thomas | f734df4 | 2020-04-13 21:09:28 -0700 | [diff] [blame] | 148 | Policy() = default; | 
| Marin Shalamanov | 30b0b3c | 2020-10-13 19:15:06 +0200 | [diff] [blame] | 149 |  | 
| Marin Shalamanov | a7fe304 | 2021-01-29 21:02:08 +0100 | [diff] [blame] | 150 | Policy(DisplayModeId defaultMode, const FpsRange& range) | 
|  | 151 | : Policy(defaultMode, kAllowGroupSwitchingDefault, range, range) {} | 
| Marin Shalamanov | 30b0b3c | 2020-10-13 19:15:06 +0200 | [diff] [blame] | 152 |  | 
| Marin Shalamanov | a7fe304 | 2021-01-29 21:02:08 +0100 | [diff] [blame] | 153 | Policy(DisplayModeId defaultMode, bool allowGroupSwitching, const FpsRange& range) | 
|  | 154 | : Policy(defaultMode, allowGroupSwitching, range, range) {} | 
| Marin Shalamanov | 30b0b3c | 2020-10-13 19:15:06 +0200 | [diff] [blame] | 155 |  | 
| Marin Shalamanov | a7fe304 | 2021-01-29 21:02:08 +0100 | [diff] [blame] | 156 | Policy(DisplayModeId defaultMode, const FpsRange& primaryRange, | 
| Marin Shalamanov | eadf2e7 | 2020-12-10 15:35:28 +0100 | [diff] [blame] | 157 | const FpsRange& appRequestRange) | 
| Marin Shalamanov | a7fe304 | 2021-01-29 21:02:08 +0100 | [diff] [blame] | 158 | : Policy(defaultMode, kAllowGroupSwitchingDefault, primaryRange, appRequestRange) {} | 
| Marin Shalamanov | 30b0b3c | 2020-10-13 19:15:06 +0200 | [diff] [blame] | 159 |  | 
| Marin Shalamanov | a7fe304 | 2021-01-29 21:02:08 +0100 | [diff] [blame] | 160 | Policy(DisplayModeId defaultMode, bool allowGroupSwitching, const FpsRange& primaryRange, | 
| Marin Shalamanov | 23c4420 | 2020-12-22 19:09:20 +0100 | [diff] [blame] | 161 | const FpsRange& appRequestRange) | 
| Marin Shalamanov | a7fe304 | 2021-01-29 21:02:08 +0100 | [diff] [blame] | 162 | : defaultMode(defaultMode), | 
| Marin Shalamanov | 30b0b3c | 2020-10-13 19:15:06 +0200 | [diff] [blame] | 163 | allowGroupSwitching(allowGroupSwitching), | 
| Steven Thomas | f734df4 | 2020-04-13 21:09:28 -0700 | [diff] [blame] | 164 | primaryRange(primaryRange), | 
|  | 165 | appRequestRange(appRequestRange) {} | 
|  | 166 |  | 
| Steven Thomas | d407190 | 2020-03-24 16:02:53 -0700 | [diff] [blame] | 167 | bool operator==(const Policy& other) const { | 
| Marin Shalamanov | a7fe304 | 2021-01-29 21:02:08 +0100 | [diff] [blame] | 168 | return defaultMode == other.defaultMode && primaryRange == other.primaryRange && | 
| Steven Thomas | f734df4 | 2020-04-13 21:09:28 -0700 | [diff] [blame] | 169 | appRequestRange == other.appRequestRange && | 
| Steven Thomas | d407190 | 2020-03-24 16:02:53 -0700 | [diff] [blame] | 170 | allowGroupSwitching == other.allowGroupSwitching; | 
|  | 171 | } | 
|  | 172 |  | 
|  | 173 | bool operator!=(const Policy& other) const { return !(*this == other); } | 
| Marin Shalamanov | b6674e7 | 2020-11-06 13:05:57 +0100 | [diff] [blame] | 174 | std::string toString() const; | 
| Steven Thomas | d407190 | 2020-03-24 16:02:53 -0700 | [diff] [blame] | 175 | }; | 
|  | 176 |  | 
|  | 177 | // Return code set*Policy() to indicate the current policy is unchanged. | 
|  | 178 | static constexpr int CURRENT_POLICY_UNCHANGED = 1; | 
|  | 179 |  | 
|  | 180 | // We maintain the display manager policy and the override policy separately. The override | 
|  | 181 | // policy is used by CTS tests to get a consistent device state for testing. While the override | 
|  | 182 | // policy is set, it takes precedence over the display manager policy. Once the override policy | 
|  | 183 | // is cleared, we revert to using the display manager policy. | 
|  | 184 |  | 
|  | 185 | // Sets the display manager policy to choose refresh rates. The return value will be: | 
|  | 186 | //   - A negative value if the policy is invalid or another error occurred. | 
|  | 187 | //   - NO_ERROR if the policy was successfully updated, and the current policy is different from | 
|  | 188 | //     what it was before the call. | 
|  | 189 | //   - CURRENT_POLICY_UNCHANGED if the policy was successfully updated, but the current policy | 
|  | 190 | //     is the same as it was before the call. | 
|  | 191 | status_t setDisplayManagerPolicy(const Policy& policy) EXCLUDES(mLock); | 
|  | 192 | // Sets the override policy. See setDisplayManagerPolicy() for the meaning of the return value. | 
|  | 193 | status_t setOverridePolicy(const std::optional<Policy>& policy) EXCLUDES(mLock); | 
|  | 194 | // Gets the current policy, which will be the override policy if active, and the display manager | 
|  | 195 | // policy otherwise. | 
|  | 196 | Policy getCurrentPolicy() const EXCLUDES(mLock); | 
|  | 197 | // Gets the display manager policy, regardless of whether an override policy is active. | 
|  | 198 | Policy getDisplayManagerPolicy() const EXCLUDES(mLock); | 
| Ana Krulec | ed3a8cc | 2019-11-14 00:55:07 +0100 | [diff] [blame] | 199 |  | 
| Marin Shalamanov | a7fe304 | 2021-01-29 21:02:08 +0100 | [diff] [blame] | 200 | // Returns true if mode is allowed by the current policy. | 
|  | 201 | bool isModeAllowed(DisplayModeId) const EXCLUDES(mLock); | 
| Ady Abraham | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame] | 202 |  | 
| Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 203 | // Describes the different options the layer voted for refresh rate | 
|  | 204 | enum class LayerVoteType { | 
| Ady Abraham | 71c437d | 2020-01-31 15:56:57 -0800 | [diff] [blame] | 205 | NoVote,          // Doesn't care about the refresh rate | 
|  | 206 | Min,             // Minimal refresh rate available | 
|  | 207 | Max,             // Maximal refresh rate available | 
|  | 208 | Heuristic,       // Specific refresh rate that was calculated by platform using a heuristic | 
|  | 209 | ExplicitDefault, // Specific refresh rate that was provided by the app with Default | 
|  | 210 | // compatibility | 
| Ady Abraham | dd5bfa9 | 2021-01-07 17:56:08 -0800 | [diff] [blame] | 211 | ExplicitExactOrMultiple, // Specific refresh rate that was provided by the app with | 
|  | 212 | // ExactOrMultiple compatibility | 
|  | 213 | ExplicitExact,           // Specific refresh rate that was provided by the app with | 
|  | 214 | // Exact compatibility | 
|  | 215 |  | 
| Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 216 | }; | 
|  | 217 |  | 
|  | 218 | // Captures the layer requirements for a refresh rate. This will be used to determine the | 
|  | 219 | // display refresh rate. | 
|  | 220 | struct LayerRequirement { | 
| Ady Abraham | aae5ed5 | 2020-06-26 09:32:43 -0700 | [diff] [blame] | 221 | // Layer's name. Used for debugging purposes. | 
|  | 222 | std::string name; | 
| Ady Abraham | 62a0be2 | 2020-12-08 16:54:10 -0800 | [diff] [blame] | 223 | // Layer's owner uid | 
|  | 224 | uid_t ownerUid = static_cast<uid_t>(-1); | 
| Ady Abraham | aae5ed5 | 2020-06-26 09:32:43 -0700 | [diff] [blame] | 225 | // Layer vote type. | 
|  | 226 | LayerVoteType vote = LayerVoteType::NoVote; | 
|  | 227 | // Layer's desired refresh rate, if applicable. | 
| Marin Shalamanov | e8a663d | 2020-11-24 17:48:00 +0100 | [diff] [blame] | 228 | Fps desiredRefreshRate{0.0f}; | 
| Marin Shalamanov | 4608442 | 2020-10-13 12:33:42 +0200 | [diff] [blame] | 229 | // If a seamless mode switch is required. | 
| Marin Shalamanov | 53fc11d | 2020-11-20 14:00:13 +0100 | [diff] [blame] | 230 | Seamlessness seamlessness = Seamlessness::Default; | 
| Ady Abraham | aae5ed5 | 2020-06-26 09:32:43 -0700 | [diff] [blame] | 231 | // Layer's weight in the range of [0, 1]. The higher the weight the more impact this layer | 
|  | 232 | // would have on choosing the refresh rate. | 
|  | 233 | float weight = 0.0f; | 
|  | 234 | // Whether layer is in focus or not based on WindowManager's state | 
|  | 235 | bool focused = false; | 
| Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 236 |  | 
|  | 237 | bool operator==(const LayerRequirement& other) const { | 
|  | 238 | return name == other.name && vote == other.vote && | 
| Marin Shalamanov | e8a663d | 2020-11-24 17:48:00 +0100 | [diff] [blame] | 239 | desiredRefreshRate.equalsWithMargin(other.desiredRefreshRate) && | 
| Marin Shalamanov | be97cfa | 2020-12-01 16:02:07 +0100 | [diff] [blame] | 240 | seamlessness == other.seamlessness && weight == other.weight && | 
| Ady Abraham | aae5ed5 | 2020-06-26 09:32:43 -0700 | [diff] [blame] | 241 | focused == other.focused; | 
| Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 242 | } | 
|  | 243 |  | 
|  | 244 | bool operator!=(const LayerRequirement& other) const { return !(*this == other); } | 
|  | 245 | }; | 
|  | 246 |  | 
| Ady Abraham | dfd6216 | 2020-06-10 16:11:56 -0700 | [diff] [blame] | 247 | // Global state describing signals that affect refresh rate choice. | 
|  | 248 | struct GlobalSignals { | 
|  | 249 | // Whether the user touched the screen recently. Used to apply touch boost. | 
|  | 250 | bool touch = false; | 
|  | 251 | // True if the system hasn't seen any buffers posted to layers recently. | 
|  | 252 | bool idle = false; | 
| Marin Shalamanov | 4c7831e | 2021-06-08 20:44:06 +0200 | [diff] [blame] | 253 |  | 
|  | 254 | bool operator==(const GlobalSignals& other) const { | 
|  | 255 | return touch == other.touch && idle == other.idle; | 
|  | 256 | } | 
| Ady Abraham | dfd6216 | 2020-06-10 16:11:56 -0700 | [diff] [blame] | 257 | }; | 
|  | 258 |  | 
| Steven Thomas | bb37432 | 2020-04-28 22:47:16 -0700 | [diff] [blame] | 259 | // Returns the refresh rate that fits best to the given layers. | 
|  | 260 | //   layers - The layer requirements to consider. | 
| Ady Abraham | dfd6216 | 2020-06-10 16:11:56 -0700 | [diff] [blame] | 261 | //   globalSignals - global state of touch and idle | 
|  | 262 | //   outSignalsConsidered - An output param that tells the caller whether the refresh rate was | 
|  | 263 | //                          chosen based on touch boost and/or idle timer. | 
| Marin Shalamanov | eadf2e7 | 2020-12-10 15:35:28 +0100 | [diff] [blame] | 264 | RefreshRate getBestRefreshRate(const std::vector<LayerRequirement>& layers, | 
|  | 265 | const GlobalSignals& globalSignals, | 
|  | 266 | GlobalSignals* outSignalsConsidered = nullptr) const | 
| Ady Abraham | 6fb599b | 2020-03-05 13:48:22 -0800 | [diff] [blame] | 267 | EXCLUDES(mLock); | 
| Ana Krulec | b43429d | 2019-01-09 14:28:51 -0800 | [diff] [blame] | 268 |  | 
| Marin Shalamanov | eadf2e7 | 2020-12-10 15:35:28 +0100 | [diff] [blame] | 269 | FpsRange getSupportedRefreshRateRange() const EXCLUDES(mLock) { | 
|  | 270 | std::lock_guard lock(mLock); | 
|  | 271 | return {mMinSupportedRefreshRate->getFps(), mMaxSupportedRefreshRate->getFps()}; | 
|  | 272 | } | 
| Steven Thomas | 2bbaabe | 2019-08-28 16:08:35 -0700 | [diff] [blame] | 273 |  | 
| Marin Shalamanov | a7fe304 | 2021-01-29 21:02:08 +0100 | [diff] [blame] | 274 | std::optional<Fps> onKernelTimerChanged(std::optional<DisplayModeId> desiredActiveModeId, | 
| Marin Shalamanov | eadf2e7 | 2020-12-10 15:35:28 +0100 | [diff] [blame] | 275 | bool timerExpired) const EXCLUDES(mLock); | 
| 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. | 
| Marin Shalamanov | eadf2e7 | 2020-12-10 15:35:28 +0100 | [diff] [blame] | 279 | RefreshRate getMaxRefreshRateByPolicy() const EXCLUDES(mLock); | 
| Ady Abraham | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame] | 280 |  | 
|  | 281 | // Returns the current refresh rate | 
| Marin Shalamanov | eadf2e7 | 2020-12-10 15:35:28 +0100 | [diff] [blame] | 282 | RefreshRate getCurrentRefreshRate() const EXCLUDES(mLock); | 
| Ady Abraham | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame] | 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. | 
| Marin Shalamanov | eadf2e7 | 2020-12-10 15:35:28 +0100 | [diff] [blame] | 286 | RefreshRate getCurrentRefreshRateByPolicy() const; | 
| Ana Krulec | 5d47791 | 2020-02-07 12:02:38 -0800 | [diff] [blame] | 287 |  | 
| Marin Shalamanov | 23c4420 | 2020-12-22 19:09:20 +0100 | [diff] [blame] | 288 | // Returns the refresh rate that corresponds to a DisplayModeId. This may change at | 
| Ady Abraham | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame] | 289 | // runtime. | 
| Marin Shalamanov | a7fe304 | 2021-01-29 21:02:08 +0100 | [diff] [blame] | 290 | // TODO(b/159590486) An invalid mode id may be given here if the dipslay modes have changed. | 
|  | 291 | RefreshRate getRefreshRateFromModeId(DisplayModeId modeId) const EXCLUDES(mLock) { | 
| Marin Shalamanov | eadf2e7 | 2020-12-10 15:35:28 +0100 | [diff] [blame] | 292 | std::lock_guard lock(mLock); | 
| Marin Shalamanov | a7fe304 | 2021-01-29 21:02:08 +0100 | [diff] [blame] | 293 | return *mRefreshRates.at(modeId); | 
| Ady Abraham | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame] | 294 | }; | 
|  | 295 |  | 
| Marin Shalamanov | a7fe304 | 2021-01-29 21:02:08 +0100 | [diff] [blame] | 296 | // Stores the current modeId the device operates at | 
|  | 297 | void setCurrentModeId(DisplayModeId) EXCLUDES(mLock); | 
| Dominik Laskowski | 22488f6 | 2019-03-28 09:53:04 -0700 | [diff] [blame] | 298 |  | 
| Ady Abraham | a6b676e | 2020-05-27 14:29:09 -0700 | [diff] [blame] | 299 | // Returns a string that represents the layer vote type | 
|  | 300 | static std::string layerVoteTypeString(LayerVoteType vote); | 
|  | 301 |  | 
| Ady Abraham | b1b9d41 | 2020-06-01 19:53:52 -0700 | [diff] [blame] | 302 | // Returns a known frame rate that is the closest to frameRate | 
| Marin Shalamanov | e8a663d | 2020-11-24 17:48:00 +0100 | [diff] [blame] | 303 | Fps findClosestKnownFrameRate(Fps frameRate) const; | 
| Ady Abraham | b1b9d41 | 2020-06-01 19:53:52 -0700 | [diff] [blame] | 304 |  | 
| rnlee | 3bd61066 | 2021-06-23 16:27:57 -0700 | [diff] [blame] | 305 | // Configuration flags. | 
|  | 306 | struct Config { | 
|  | 307 | bool enableFrameRateOverride = false; | 
|  | 308 |  | 
|  | 309 | // Specifies the upper refresh rate threshold (inclusive) for layer vote types of multiple | 
|  | 310 | // or heuristic, such that refresh rates higher than this value will not be voted for. 0 if | 
|  | 311 | // no threshold is set. | 
|  | 312 | int frameRateMultipleThreshold = 0; | 
|  | 313 | }; | 
|  | 314 |  | 
| Marin Shalamanov | a7fe304 | 2021-01-29 21:02:08 +0100 | [diff] [blame] | 315 | RefreshRateConfigs(const DisplayModes& modes, DisplayModeId currentModeId, | 
| rnlee | 3bd61066 | 2021-06-23 16:27:57 -0700 | [diff] [blame] | 316 | Config config = {.enableFrameRateOverride = false, | 
|  | 317 | .frameRateMultipleThreshold = 0}); | 
| Ana Krulec | 4593b69 | 2019-01-11 22:07:25 -0800 | [diff] [blame] | 318 |  | 
| Marin Shalamanov | a7fe304 | 2021-01-29 21:02:08 +0100 | [diff] [blame] | 319 | void updateDisplayModes(const DisplayModes& mode, DisplayModeId currentModeId) EXCLUDES(mLock); | 
| Marin Shalamanov | eadf2e7 | 2020-12-10 15:35:28 +0100 | [diff] [blame] | 320 |  | 
| Marin Shalamanov | a7fe304 | 2021-01-29 21:02:08 +0100 | [diff] [blame] | 321 | // Returns whether switching modes (refresh rate or resolution) is possible. | 
|  | 322 | // TODO(b/158780872): Consider HAL support, and skip frame rate detection if the modes only | 
| Dominik Laskowski | 983f2b5 | 2020-06-25 16:54:06 -0700 | [diff] [blame] | 323 | // differ in resolution. | 
| Marin Shalamanov | eadf2e7 | 2020-12-10 15:35:28 +0100 | [diff] [blame] | 324 | bool canSwitch() const EXCLUDES(mLock) { | 
|  | 325 | std::lock_guard lock(mLock); | 
|  | 326 | return mRefreshRates.size() > 1; | 
|  | 327 | } | 
| Dominik Laskowski | 983f2b5 | 2020-06-25 16:54:06 -0700 | [diff] [blame] | 328 |  | 
| TreeHugger Robot | 758ab61 | 2021-06-22 19:17:29 +0000 | [diff] [blame] | 329 | // Class to enumerate options around toggling the kernel timer on and off. | 
| Ana Krulec | b9afd79 | 2020-06-11 13:16:15 -0700 | [diff] [blame] | 330 | enum class KernelIdleTimerAction { | 
| Ana Krulec | b9afd79 | 2020-06-11 13:16:15 -0700 | [diff] [blame] | 331 | TurnOff,  // Turn off the idle timer. | 
|  | 332 | TurnOn    // Turn on the idle timer. | 
|  | 333 | }; | 
|  | 334 | // Checks whether kernel idle timer should be active depending the policy decisions around | 
|  | 335 | // refresh rates. | 
|  | 336 | KernelIdleTimerAction getIdleTimerAction() const; | 
|  | 337 |  | 
| Ady Abraham | 64c2fc0 | 2020-12-29 12:07:50 -0800 | [diff] [blame] | 338 | bool supportsFrameRateOverride() const { return mSupportsFrameRateOverride; } | 
|  | 339 |  | 
| Ady Abraham | 5cc2e26 | 2021-03-25 13:09:17 -0700 | [diff] [blame] | 340 | // Return the display refresh rate divider to match the layer | 
|  | 341 | // frame rate, or 0 if the display refresh rate is not a multiple of the | 
|  | 342 | // layer refresh rate. | 
|  | 343 | static int getFrameRateDivider(Fps displayFrameRate, Fps layerFrameRate); | 
| Ady Abraham | 62a0be2 | 2020-12-08 16:54:10 -0800 | [diff] [blame] | 344 |  | 
| Ady Abraham | 62a0be2 | 2020-12-08 16:54:10 -0800 | [diff] [blame] | 345 | using UidToFrameRateOverride = std::map<uid_t, Fps>; | 
| Ady Abraham | dd5bfa9 | 2021-01-07 17:56:08 -0800 | [diff] [blame] | 346 | // Returns the frame rate override for each uid. | 
|  | 347 | // | 
|  | 348 | // @param layers list of visible layers | 
|  | 349 | // @param displayFrameRate the display frame rate | 
|  | 350 | // @param touch whether touch timer is active (i.e. user touched the screen recently) | 
| Ady Abraham | 62a0be2 | 2020-12-08 16:54:10 -0800 | [diff] [blame] | 351 | UidToFrameRateOverride getFrameRateOverrides(const std::vector<LayerRequirement>& layers, | 
| Ady Abraham | dd5bfa9 | 2021-01-07 17:56:08 -0800 | [diff] [blame] | 352 | Fps displayFrameRate, bool touch) const | 
|  | 353 | EXCLUDES(mLock); | 
| Ady Abraham | 0bb6a47 | 2020-10-12 10:22:13 -0700 | [diff] [blame] | 354 |  | 
| Marin Shalamanov | ba421a8 | 2020-11-10 21:49:26 +0100 | [diff] [blame] | 355 | void dump(std::string& result) const EXCLUDES(mLock); | 
|  | 356 |  | 
| Dominik Laskowski | 22488f6 | 2019-03-28 09:53:04 -0700 | [diff] [blame] | 357 | private: | 
| Ady Abraham | b1b9d41 | 2020-06-01 19:53:52 -0700 | [diff] [blame] | 358 | friend class RefreshRateConfigsTest; | 
|  | 359 |  | 
| Ady Abraham | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame] | 360 | void constructAvailableRefreshRates() REQUIRES(mLock); | 
|  | 361 |  | 
| Marin Shalamanov | eadf2e7 | 2020-12-10 15:35:28 +0100 | [diff] [blame] | 362 | void getSortedRefreshRateListLocked( | 
| Ady Abraham | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame] | 363 | const std::function<bool(const RefreshRate&)>& shouldAddRefreshRate, | 
| Marin Shalamanov | eadf2e7 | 2020-12-10 15:35:28 +0100 | [diff] [blame] | 364 | std::vector<const RefreshRate*>* outRefreshRates) REQUIRES(mLock); | 
| Ady Abraham | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame] | 365 |  | 
| Marin Shalamanov | 4c7831e | 2021-06-08 20:44:06 +0200 | [diff] [blame] | 366 | std::optional<RefreshRate> getCachedBestRefreshRate(const std::vector<LayerRequirement>& layers, | 
|  | 367 | const GlobalSignals& globalSignals, | 
|  | 368 | GlobalSignals* outSignalsConsidered) const | 
|  | 369 | REQUIRES(mLock); | 
|  | 370 |  | 
|  | 371 | RefreshRate getBestRefreshRateLocked(const std::vector<LayerRequirement>& layers, | 
|  | 372 | const GlobalSignals& globalSignals, | 
|  | 373 | GlobalSignals* outSignalsConsidered) const REQUIRES(mLock); | 
|  | 374 |  | 
| Ady Abraham | 3470210 | 2020-02-10 14:12:05 -0800 | [diff] [blame] | 375 | // Returns the refresh rate with the highest score in the collection specified from begin | 
|  | 376 | // to end. If there are more than one with the same highest refresh rate, the first one is | 
|  | 377 | // returned. | 
|  | 378 | template <typename Iter> | 
|  | 379 | const RefreshRate* getBestRefreshRate(Iter begin, Iter end) const; | 
|  | 380 |  | 
| Ady Abraham | 4ccdcb4 | 2020-02-11 17:34:34 -0800 | [diff] [blame] | 381 | // Returns number of display frames and remainder when dividing the layer refresh period by | 
|  | 382 | // display refresh period. | 
|  | 383 | std::pair<nsecs_t, nsecs_t> getDisplayFrames(nsecs_t layerPeriod, nsecs_t displayPeriod) const; | 
|  | 384 |  | 
| Steven Thomas | f734df4 | 2020-04-13 21:09:28 -0700 | [diff] [blame] | 385 | // Returns the lowest refresh rate according to the current policy. May change at runtime. Only | 
|  | 386 | // uses the primary range, not the app request range. | 
|  | 387 | const RefreshRate& getMinRefreshRateByPolicyLocked() const REQUIRES(mLock); | 
|  | 388 |  | 
|  | 389 | // Returns the highest refresh rate according to the current policy. May change at runtime. Only | 
|  | 390 | // uses the primary range, not the app request range. | 
|  | 391 | const RefreshRate& getMaxRefreshRateByPolicyLocked() const REQUIRES(mLock); | 
|  | 392 |  | 
| Ana Krulec | 3d367c8 | 2020-02-25 15:02:01 -0800 | [diff] [blame] | 393 | // Returns the current refresh rate, if allowed. Otherwise the default that is allowed by | 
|  | 394 | // the policy. | 
|  | 395 | const RefreshRate& getCurrentRefreshRateByPolicyLocked() const REQUIRES(mLock); | 
|  | 396 |  | 
| Steven Thomas | d407190 | 2020-03-24 16:02:53 -0700 | [diff] [blame] | 397 | const Policy* getCurrentPolicyLocked() const REQUIRES(mLock); | 
| Marin Shalamanov | eadf2e7 | 2020-12-10 15:35:28 +0100 | [diff] [blame] | 398 | bool isPolicyValidLocked(const Policy& policy) const REQUIRES(mLock); | 
| Steven Thomas | d407190 | 2020-03-24 16:02:53 -0700 | [diff] [blame] | 399 |  | 
| rnlee | 3bd61066 | 2021-06-23 16:27:57 -0700 | [diff] [blame] | 400 | // Returns whether the layer is allowed to vote for the given refresh rate. | 
|  | 401 | bool isVoteAllowed(const LayerRequirement&, const RefreshRate&) const; | 
|  | 402 |  | 
| Ady Abraham | 62a0be2 | 2020-12-08 16:54:10 -0800 | [diff] [blame] | 403 | // calculates a score for a layer. Used to determine the display refresh rate | 
|  | 404 | // and the frame rate override for certains applications. | 
|  | 405 | float calculateLayerScoreLocked(const LayerRequirement&, const RefreshRate&, | 
|  | 406 | bool isSeamlessSwitch) const REQUIRES(mLock); | 
|  | 407 |  | 
| Marin Shalamanov | a7fe304 | 2021-01-29 21:02:08 +0100 | [diff] [blame] | 408 | // The list of refresh rates, indexed by display modes ID. This may change after this | 
| Steven Thomas | 2bbaabe | 2019-08-28 16:08:35 -0700 | [diff] [blame] | 409 | // object is initialized. | 
| Marin Shalamanov | eadf2e7 | 2020-12-10 15:35:28 +0100 | [diff] [blame] | 410 | AllRefreshRatesMapType mRefreshRates GUARDED_BY(mLock); | 
| Ady Abraham | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame] | 411 |  | 
| Steven Thomas | f734df4 | 2020-04-13 21:09:28 -0700 | [diff] [blame] | 412 | // The list of refresh rates in the primary range of the current policy, ordered by vsyncPeriod | 
|  | 413 | // (the first element is the lowest refresh rate). | 
|  | 414 | std::vector<const RefreshRate*> mPrimaryRefreshRates GUARDED_BY(mLock); | 
|  | 415 |  | 
|  | 416 | // The list of refresh rates in the app request range of the current policy, ordered by | 
|  | 417 | // vsyncPeriod (the first element is the lowest refresh rate). | 
|  | 418 | std::vector<const RefreshRate*> mAppRequestRefreshRates GUARDED_BY(mLock); | 
| Ady Abraham | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame] | 419 |  | 
| Marin Shalamanov | a7fe304 | 2021-01-29 21:02:08 +0100 | [diff] [blame] | 420 | // The current display mode. This will change at runtime. This is set by SurfaceFlinger on | 
| Ady Abraham | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame] | 421 | // the main thread, and read by the Scheduler (and other objects) on other threads. | 
|  | 422 | const RefreshRate* mCurrentRefreshRate GUARDED_BY(mLock); | 
|  | 423 |  | 
| Steven Thomas | d407190 | 2020-03-24 16:02:53 -0700 | [diff] [blame] | 424 | // The policy values will change at runtime. They're set by SurfaceFlinger on the main thread, | 
|  | 425 | // and read by the Scheduler (and other objects) on other threads. | 
|  | 426 | Policy mDisplayManagerPolicy GUARDED_BY(mLock); | 
|  | 427 | std::optional<Policy> mOverridePolicy GUARDED_BY(mLock); | 
| Ady Abraham | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame] | 428 |  | 
|  | 429 | // The min and max refresh rates supported by the device. | 
| Marin Shalamanov | eadf2e7 | 2020-12-10 15:35:28 +0100 | [diff] [blame] | 430 | // This may change at runtime. | 
|  | 431 | const RefreshRate* mMinSupportedRefreshRate GUARDED_BY(mLock); | 
|  | 432 | const RefreshRate* mMaxSupportedRefreshRate GUARDED_BY(mLock); | 
| Ady Abraham | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame] | 433 |  | 
| Ady Abraham | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame] | 434 | mutable std::mutex mLock; | 
| Ady Abraham | b1b9d41 | 2020-06-01 19:53:52 -0700 | [diff] [blame] | 435 |  | 
|  | 436 | // A sorted list of known frame rates that a Heuristic layer will choose | 
|  | 437 | // from based on the closest value. | 
| Marin Shalamanov | e8a663d | 2020-11-24 17:48:00 +0100 | [diff] [blame] | 438 | const std::vector<Fps> mKnownFrameRates; | 
| Ady Abraham | 64c2fc0 | 2020-12-29 12:07:50 -0800 | [diff] [blame] | 439 |  | 
| rnlee | 3bd61066 | 2021-06-23 16:27:57 -0700 | [diff] [blame] | 440 | const Config mConfig; | 
| Ady Abraham | 64c2fc0 | 2020-12-29 12:07:50 -0800 | [diff] [blame] | 441 | bool mSupportsFrameRateOverride; | 
| Marin Shalamanov | 4c7831e | 2021-06-08 20:44:06 +0200 | [diff] [blame] | 442 |  | 
|  | 443 | struct GetBestRefreshRateInvocation { | 
|  | 444 | std::vector<LayerRequirement> layerRequirements; | 
|  | 445 | GlobalSignals globalSignals; | 
|  | 446 | GlobalSignals outSignalsConsidered; | 
|  | 447 | RefreshRate resultingBestRefreshRate; | 
|  | 448 | }; | 
|  | 449 | mutable std::optional<GetBestRefreshRateInvocation> lastBestRefreshRateInvocation | 
|  | 450 | GUARDED_BY(mLock); | 
| Ana Krulec | b43429d | 2019-01-09 14:28:51 -0800 | [diff] [blame] | 451 | }; | 
|  | 452 |  | 
| Dominik Laskowski | 9804183 | 2019-08-01 18:35:59 -0700 | [diff] [blame] | 453 | } // namespace android::scheduler |