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 | |
| 19 | #include <algorithm> |
| 20 | #include <numeric> |
Steven Thomas | d407190 | 2020-03-24 16:02:53 -0700 | [diff] [blame] | 21 | #include <optional> |
Dominik Laskowski | 9804183 | 2019-08-01 18:35:59 -0700 | [diff] [blame] | 22 | #include <type_traits> |
Dominik Laskowski | a8626ec | 2021-12-15 18:13:30 -0800 | [diff] [blame] | 23 | #include <utility> |
Ana Krulec | b43429d | 2019-01-09 14:28:51 -0800 | [diff] [blame] | 24 | |
Dominik Laskowski | f6b4ba6 | 2021-11-09 12:46:10 -0800 | [diff] [blame] | 25 | #include <gui/DisplayEventReceiver.h> |
| 26 | |
| 27 | #include <scheduler/Fps.h> |
| 28 | #include <scheduler/Seamlessness.h> |
| 29 | |
Marin Shalamanov | 3ea1d60 | 2020-12-16 19:59:39 +0100 | [diff] [blame] | 30 | #include "DisplayHardware/DisplayMode.h" |
Ana Krulec | 4593b69 | 2019-01-11 22:07:25 -0800 | [diff] [blame] | 31 | #include "DisplayHardware/HWComposer.h" |
Ady Abraham | 9a2ea34 | 2021-09-03 17:32:34 -0700 | [diff] [blame] | 32 | #include "Scheduler/OneShotTimer.h" |
Ady Abraham | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame] | 33 | #include "Scheduler/StrongTyping.h" |
Ana Krulec | 4593b69 | 2019-01-11 22:07:25 -0800 | [diff] [blame] | 34 | |
Dominik Laskowski | 9804183 | 2019-08-01 18:35:59 -0700 | [diff] [blame] | 35 | namespace android::scheduler { |
Ady Abraham | abc2760 | 2020-04-08 17:20:29 -0700 | [diff] [blame] | 36 | |
Ady Abraham | 4ccdcb4 | 2020-02-11 17:34:34 -0800 | [diff] [blame] | 37 | using namespace std::chrono_literals; |
Dominik Laskowski | 9804183 | 2019-08-01 18:35:59 -0700 | [diff] [blame] | 38 | |
Dominik Laskowski | 068173d | 2021-08-11 17:22:59 -0700 | [diff] [blame] | 39 | enum class DisplayModeEvent : unsigned { None = 0b0, Changed = 0b1 }; |
Dominik Laskowski | 9804183 | 2019-08-01 18:35:59 -0700 | [diff] [blame] | 40 | |
Dominik Laskowski | 068173d | 2021-08-11 17:22:59 -0700 | [diff] [blame] | 41 | inline DisplayModeEvent operator|(DisplayModeEvent lhs, DisplayModeEvent rhs) { |
| 42 | using T = std::underlying_type_t<DisplayModeEvent>; |
| 43 | return static_cast<DisplayModeEvent>(static_cast<T>(lhs) | static_cast<T>(rhs)); |
Dominik Laskowski | 9804183 | 2019-08-01 18:35:59 -0700 | [diff] [blame] | 44 | } |
Ana Krulec | b43429d | 2019-01-09 14:28:51 -0800 | [diff] [blame] | 45 | |
Ady Abraham | 62f216c | 2020-10-13 19:07:23 -0700 | [diff] [blame] | 46 | using FrameRateOverride = DisplayEventReceiver::Event::FrameRateOverride; |
| 47 | |
Ana Krulec | b43429d | 2019-01-09 14:28:51 -0800 | [diff] [blame] | 48 | /** |
Ady Abraham | 1902d07 | 2019-03-01 17:18:59 -0800 | [diff] [blame] | 49 | * 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] | 50 | * about available refresh rates on the device, and the mapping between the numbers and human |
| 51 | * readable names. |
| 52 | */ |
| 53 | class RefreshRateConfigs { |
| 54 | public: |
Ady Abraham | 4ccdcb4 | 2020-02-11 17:34:34 -0800 | [diff] [blame] | 55 | // Margin used when matching refresh rates to the content desired ones. |
| 56 | static constexpr nsecs_t MARGIN_FOR_PERIOD_CALCULATION = |
rnlee | 3bd61066 | 2021-06-23 16:27:57 -0700 | [diff] [blame] | 57 | std::chrono::nanoseconds(800us).count(); |
Ady Abraham | 4ccdcb4 | 2020-02-11 17:34:34 -0800 | [diff] [blame] | 58 | |
Steven Thomas | d407190 | 2020-03-24 16:02:53 -0700 | [diff] [blame] | 59 | struct Policy { |
Marin Shalamanov | 30b0b3c | 2020-10-13 19:15:06 +0200 | [diff] [blame] | 60 | private: |
| 61 | static constexpr int kAllowGroupSwitchingDefault = false; |
| 62 | |
| 63 | public: |
Marin Shalamanov | a7fe304 | 2021-01-29 21:02:08 +0100 | [diff] [blame] | 64 | // The default mode, used to ensure we only initiate display mode switches within the |
| 65 | // same mode group as defaultMode's group. |
| 66 | DisplayModeId defaultMode; |
| 67 | // 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] | 68 | bool allowGroupSwitching = kAllowGroupSwitchingDefault; |
Steven Thomas | f734df4 | 2020-04-13 21:09:28 -0700 | [diff] [blame] | 69 | // 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] | 70 | // 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] | 71 | // signal from an app, we should stay within this range. |
Marin Shalamanov | eadf2e7 | 2020-12-10 15:35:28 +0100 | [diff] [blame] | 72 | FpsRange primaryRange; |
Marin Shalamanov | a7fe304 | 2021-01-29 21:02:08 +0100 | [diff] [blame] | 73 | // 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] | 74 | // switching refresh rates. Although we should generally stay within the primary range, |
| 75 | // specific considerations, such as layer frame rate settings specified via the |
| 76 | // setFrameRate() api, may cause us to go outside the primary range. We never go outside the |
| 77 | // app request range. The app request range will be greater than or equal to the primary |
| 78 | // refresh rate range, never smaller. |
Marin Shalamanov | eadf2e7 | 2020-12-10 15:35:28 +0100 | [diff] [blame] | 79 | FpsRange appRequestRange; |
Steven Thomas | d407190 | 2020-03-24 16:02:53 -0700 | [diff] [blame] | 80 | |
Steven Thomas | f734df4 | 2020-04-13 21:09:28 -0700 | [diff] [blame] | 81 | Policy() = default; |
Marin Shalamanov | 30b0b3c | 2020-10-13 19:15:06 +0200 | [diff] [blame] | 82 | |
Dominik Laskowski | 953b7fd | 2022-01-08 19:34:59 -0800 | [diff] [blame] | 83 | Policy(DisplayModeId defaultMode, FpsRange range) |
Marin Shalamanov | a7fe304 | 2021-01-29 21:02:08 +0100 | [diff] [blame] | 84 | : Policy(defaultMode, kAllowGroupSwitchingDefault, range, range) {} |
Marin Shalamanov | 30b0b3c | 2020-10-13 19:15:06 +0200 | [diff] [blame] | 85 | |
Dominik Laskowski | 953b7fd | 2022-01-08 19:34:59 -0800 | [diff] [blame] | 86 | Policy(DisplayModeId defaultMode, bool allowGroupSwitching, FpsRange range) |
Marin Shalamanov | a7fe304 | 2021-01-29 21:02:08 +0100 | [diff] [blame] | 87 | : Policy(defaultMode, allowGroupSwitching, range, range) {} |
Marin Shalamanov | 30b0b3c | 2020-10-13 19:15:06 +0200 | [diff] [blame] | 88 | |
Dominik Laskowski | 953b7fd | 2022-01-08 19:34:59 -0800 | [diff] [blame] | 89 | Policy(DisplayModeId defaultMode, FpsRange primaryRange, FpsRange appRequestRange) |
Marin Shalamanov | a7fe304 | 2021-01-29 21:02:08 +0100 | [diff] [blame] | 90 | : Policy(defaultMode, kAllowGroupSwitchingDefault, primaryRange, appRequestRange) {} |
Marin Shalamanov | 30b0b3c | 2020-10-13 19:15:06 +0200 | [diff] [blame] | 91 | |
Dominik Laskowski | 953b7fd | 2022-01-08 19:34:59 -0800 | [diff] [blame] | 92 | Policy(DisplayModeId defaultMode, bool allowGroupSwitching, FpsRange primaryRange, |
| 93 | FpsRange appRequestRange) |
Marin Shalamanov | a7fe304 | 2021-01-29 21:02:08 +0100 | [diff] [blame] | 94 | : defaultMode(defaultMode), |
Marin Shalamanov | 30b0b3c | 2020-10-13 19:15:06 +0200 | [diff] [blame] | 95 | allowGroupSwitching(allowGroupSwitching), |
Steven Thomas | f734df4 | 2020-04-13 21:09:28 -0700 | [diff] [blame] | 96 | primaryRange(primaryRange), |
| 97 | appRequestRange(appRequestRange) {} |
| 98 | |
Steven Thomas | d407190 | 2020-03-24 16:02:53 -0700 | [diff] [blame] | 99 | bool operator==(const Policy& other) const { |
Dominik Laskowski | 953b7fd | 2022-01-08 19:34:59 -0800 | [diff] [blame] | 100 | using namespace fps_approx_ops; |
Marin Shalamanov | a7fe304 | 2021-01-29 21:02:08 +0100 | [diff] [blame] | 101 | return defaultMode == other.defaultMode && primaryRange == other.primaryRange && |
Steven Thomas | f734df4 | 2020-04-13 21:09:28 -0700 | [diff] [blame] | 102 | appRequestRange == other.appRequestRange && |
Steven Thomas | d407190 | 2020-03-24 16:02:53 -0700 | [diff] [blame] | 103 | allowGroupSwitching == other.allowGroupSwitching; |
| 104 | } |
| 105 | |
| 106 | bool operator!=(const Policy& other) const { return !(*this == other); } |
Marin Shalamanov | b6674e7 | 2020-11-06 13:05:57 +0100 | [diff] [blame] | 107 | std::string toString() const; |
Steven Thomas | d407190 | 2020-03-24 16:02:53 -0700 | [diff] [blame] | 108 | }; |
| 109 | |
| 110 | // Return code set*Policy() to indicate the current policy is unchanged. |
| 111 | static constexpr int CURRENT_POLICY_UNCHANGED = 1; |
| 112 | |
| 113 | // We maintain the display manager policy and the override policy separately. The override |
| 114 | // policy is used by CTS tests to get a consistent device state for testing. While the override |
| 115 | // policy is set, it takes precedence over the display manager policy. Once the override policy |
| 116 | // is cleared, we revert to using the display manager policy. |
| 117 | |
| 118 | // Sets the display manager policy to choose refresh rates. The return value will be: |
| 119 | // - A negative value if the policy is invalid or another error occurred. |
| 120 | // - NO_ERROR if the policy was successfully updated, and the current policy is different from |
| 121 | // what it was before the call. |
| 122 | // - CURRENT_POLICY_UNCHANGED if the policy was successfully updated, but the current policy |
| 123 | // is the same as it was before the call. |
| 124 | status_t setDisplayManagerPolicy(const Policy& policy) EXCLUDES(mLock); |
| 125 | // Sets the override policy. See setDisplayManagerPolicy() for the meaning of the return value. |
| 126 | status_t setOverridePolicy(const std::optional<Policy>& policy) EXCLUDES(mLock); |
| 127 | // Gets the current policy, which will be the override policy if active, and the display manager |
| 128 | // policy otherwise. |
| 129 | Policy getCurrentPolicy() const EXCLUDES(mLock); |
| 130 | // Gets the display manager policy, regardless of whether an override policy is active. |
| 131 | Policy getDisplayManagerPolicy() const EXCLUDES(mLock); |
Ana Krulec | ed3a8cc | 2019-11-14 00:55:07 +0100 | [diff] [blame] | 132 | |
Marin Shalamanov | a7fe304 | 2021-01-29 21:02:08 +0100 | [diff] [blame] | 133 | // Returns true if mode is allowed by the current policy. |
| 134 | bool isModeAllowed(DisplayModeId) const EXCLUDES(mLock); |
Ady Abraham | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame] | 135 | |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 136 | // Describes the different options the layer voted for refresh rate |
| 137 | enum class LayerVoteType { |
Ady Abraham | 71c437d | 2020-01-31 15:56:57 -0800 | [diff] [blame] | 138 | NoVote, // Doesn't care about the refresh rate |
| 139 | Min, // Minimal refresh rate available |
| 140 | Max, // Maximal refresh rate available |
| 141 | Heuristic, // Specific refresh rate that was calculated by platform using a heuristic |
| 142 | ExplicitDefault, // Specific refresh rate that was provided by the app with Default |
| 143 | // compatibility |
Ady Abraham | dd5bfa9 | 2021-01-07 17:56:08 -0800 | [diff] [blame] | 144 | ExplicitExactOrMultiple, // Specific refresh rate that was provided by the app with |
| 145 | // ExactOrMultiple compatibility |
| 146 | ExplicitExact, // Specific refresh rate that was provided by the app with |
| 147 | // Exact compatibility |
| 148 | |
Dominik Laskowski | f5d0ea5 | 2021-09-26 17:27:01 -0700 | [diff] [blame] | 149 | ftl_last = ExplicitExact |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 150 | }; |
| 151 | |
| 152 | // Captures the layer requirements for a refresh rate. This will be used to determine the |
| 153 | // display refresh rate. |
| 154 | struct LayerRequirement { |
Ady Abraham | aae5ed5 | 2020-06-26 09:32:43 -0700 | [diff] [blame] | 155 | // Layer's name. Used for debugging purposes. |
| 156 | std::string name; |
Ady Abraham | 62a0be2 | 2020-12-08 16:54:10 -0800 | [diff] [blame] | 157 | // Layer's owner uid |
| 158 | uid_t ownerUid = static_cast<uid_t>(-1); |
Ady Abraham | aae5ed5 | 2020-06-26 09:32:43 -0700 | [diff] [blame] | 159 | // Layer vote type. |
| 160 | LayerVoteType vote = LayerVoteType::NoVote; |
| 161 | // Layer's desired refresh rate, if applicable. |
Dominik Laskowski | 6eab42d | 2021-09-13 14:34:13 -0700 | [diff] [blame] | 162 | Fps desiredRefreshRate; |
Marin Shalamanov | 4608442 | 2020-10-13 12:33:42 +0200 | [diff] [blame] | 163 | // If a seamless mode switch is required. |
Marin Shalamanov | 53fc11d | 2020-11-20 14:00:13 +0100 | [diff] [blame] | 164 | Seamlessness seamlessness = Seamlessness::Default; |
Ady Abraham | aae5ed5 | 2020-06-26 09:32:43 -0700 | [diff] [blame] | 165 | // Layer's weight in the range of [0, 1]. The higher the weight the more impact this layer |
| 166 | // would have on choosing the refresh rate. |
| 167 | float weight = 0.0f; |
| 168 | // Whether layer is in focus or not based on WindowManager's state |
| 169 | bool focused = false; |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 170 | |
| 171 | bool operator==(const LayerRequirement& other) const { |
| 172 | return name == other.name && vote == other.vote && |
Dominik Laskowski | 6eab42d | 2021-09-13 14:34:13 -0700 | [diff] [blame] | 173 | isApproxEqual(desiredRefreshRate, other.desiredRefreshRate) && |
Marin Shalamanov | be97cfa | 2020-12-01 16:02:07 +0100 | [diff] [blame] | 174 | seamlessness == other.seamlessness && weight == other.weight && |
Ady Abraham | aae5ed5 | 2020-06-26 09:32:43 -0700 | [diff] [blame] | 175 | focused == other.focused; |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 176 | } |
| 177 | |
| 178 | bool operator!=(const LayerRequirement& other) const { return !(*this == other); } |
| 179 | }; |
| 180 | |
Ady Abraham | dfd6216 | 2020-06-10 16:11:56 -0700 | [diff] [blame] | 181 | // Global state describing signals that affect refresh rate choice. |
| 182 | struct GlobalSignals { |
| 183 | // Whether the user touched the screen recently. Used to apply touch boost. |
| 184 | bool touch = false; |
| 185 | // True if the system hasn't seen any buffers posted to layers recently. |
| 186 | bool idle = false; |
Marin Shalamanov | 4c7831e | 2021-06-08 20:44:06 +0200 | [diff] [blame] | 187 | |
Dominik Laskowski | 6eab42d | 2021-09-13 14:34:13 -0700 | [diff] [blame] | 188 | bool operator==(GlobalSignals other) const { |
Marin Shalamanov | 4c7831e | 2021-06-08 20:44:06 +0200 | [diff] [blame] | 189 | return touch == other.touch && idle == other.idle; |
| 190 | } |
Ady Abraham | dfd6216 | 2020-06-10 16:11:56 -0700 | [diff] [blame] | 191 | }; |
| 192 | |
Dominik Laskowski | a8626ec | 2021-12-15 18:13:30 -0800 | [diff] [blame] | 193 | // Returns the refresh rate that best fits the given layers, and whether the refresh rate was |
| 194 | // chosen based on touch boost and/or idle timer. |
Dominik Laskowski | b0054a2 | 2022-03-03 09:03:06 -0800 | [diff] [blame] | 195 | std::pair<DisplayModePtr, GlobalSignals> getBestRefreshRate( |
| 196 | const std::vector<LayerRequirement>&, GlobalSignals) const EXCLUDES(mLock); |
Ana Krulec | b43429d | 2019-01-09 14:28:51 -0800 | [diff] [blame] | 197 | |
Marin Shalamanov | eadf2e7 | 2020-12-10 15:35:28 +0100 | [diff] [blame] | 198 | FpsRange getSupportedRefreshRateRange() const EXCLUDES(mLock) { |
| 199 | std::lock_guard lock(mLock); |
Dominik Laskowski | b0054a2 | 2022-03-03 09:03:06 -0800 | [diff] [blame] | 200 | return {mMinRefreshRateModeIt->second->getFps(), mMaxRefreshRateModeIt->second->getFps()}; |
Marin Shalamanov | eadf2e7 | 2020-12-10 15:35:28 +0100 | [diff] [blame] | 201 | } |
Steven Thomas | 2bbaabe | 2019-08-28 16:08:35 -0700 | [diff] [blame] | 202 | |
Marin Shalamanov | a7fe304 | 2021-01-29 21:02:08 +0100 | [diff] [blame] | 203 | std::optional<Fps> onKernelTimerChanged(std::optional<DisplayModeId> desiredActiveModeId, |
Marin Shalamanov | eadf2e7 | 2020-12-10 15:35:28 +0100 | [diff] [blame] | 204 | bool timerExpired) const EXCLUDES(mLock); |
Steven Thomas | 2bbaabe | 2019-08-28 16:08:35 -0700 | [diff] [blame] | 205 | |
Steven Thomas | f734df4 | 2020-04-13 21:09:28 -0700 | [diff] [blame] | 206 | // Returns the highest refresh rate according to the current policy. May change at runtime. Only |
| 207 | // uses the primary range, not the app request range. |
Dominik Laskowski | b0054a2 | 2022-03-03 09:03:06 -0800 | [diff] [blame] | 208 | DisplayModePtr getMaxRefreshRateByPolicy() const EXCLUDES(mLock); |
Ady Abraham | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame] | 209 | |
Dominik Laskowski | b0054a2 | 2022-03-03 09:03:06 -0800 | [diff] [blame] | 210 | void setActiveModeId(DisplayModeId) EXCLUDES(mLock); |
| 211 | DisplayModePtr getActiveMode() const EXCLUDES(mLock); |
Dominik Laskowski | 22488f6 | 2019-03-28 09:53:04 -0700 | [diff] [blame] | 212 | |
Ady Abraham | b1b9d41 | 2020-06-01 19:53:52 -0700 | [diff] [blame] | 213 | // Returns a known frame rate that is the closest to frameRate |
Marin Shalamanov | e8a663d | 2020-11-24 17:48:00 +0100 | [diff] [blame] | 214 | Fps findClosestKnownFrameRate(Fps frameRate) const; |
Ady Abraham | b1b9d41 | 2020-06-01 19:53:52 -0700 | [diff] [blame] | 215 | |
Dominik Laskowski | b0054a2 | 2022-03-03 09:03:06 -0800 | [diff] [blame] | 216 | enum class KernelIdleTimerController { Sysprop, HwcApi, ftl_last = HwcApi }; |
ramindani | 32cf060 | 2022-03-02 02:30:29 +0000 | [diff] [blame] | 217 | |
rnlee | 3bd61066 | 2021-06-23 16:27:57 -0700 | [diff] [blame] | 218 | // Configuration flags. |
| 219 | struct Config { |
| 220 | bool enableFrameRateOverride = false; |
| 221 | |
| 222 | // Specifies the upper refresh rate threshold (inclusive) for layer vote types of multiple |
| 223 | // or heuristic, such that refresh rates higher than this value will not be voted for. 0 if |
| 224 | // no threshold is set. |
| 225 | int frameRateMultipleThreshold = 0; |
Ady Abraham | 9a2ea34 | 2021-09-03 17:32:34 -0700 | [diff] [blame] | 226 | |
Ady Abraham | 6d88593 | 2021-09-03 18:05:48 -0700 | [diff] [blame] | 227 | // The Idle Timer timeout. 0 timeout means no idle timer. |
ramindani | 32cf060 | 2022-03-02 02:30:29 +0000 | [diff] [blame] | 228 | std::chrono::milliseconds idleTimerTimeout = 0ms; |
Ady Abraham | 6d88593 | 2021-09-03 18:05:48 -0700 | [diff] [blame] | 229 | |
ramindani | 32cf060 | 2022-03-02 02:30:29 +0000 | [diff] [blame] | 230 | // The controller representing how the kernel idle timer will be configured |
| 231 | // either on the HWC api or sysprop. |
| 232 | std::optional<KernelIdleTimerController> kernelIdleTimerController; |
rnlee | 3bd61066 | 2021-06-23 16:27:57 -0700 | [diff] [blame] | 233 | }; |
| 234 | |
Dominik Laskowski | b0054a2 | 2022-03-03 09:03:06 -0800 | [diff] [blame] | 235 | RefreshRateConfigs(DisplayModes, DisplayModeId activeModeId, |
rnlee | 3bd61066 | 2021-06-23 16:27:57 -0700 | [diff] [blame] | 236 | Config config = {.enableFrameRateOverride = false, |
Ady Abraham | 9a2ea34 | 2021-09-03 17:32:34 -0700 | [diff] [blame] | 237 | .frameRateMultipleThreshold = 0, |
ramindani | 32cf060 | 2022-03-02 02:30:29 +0000 | [diff] [blame] | 238 | .idleTimerTimeout = 0ms, |
| 239 | .kernelIdleTimerController = {}}); |
Ana Krulec | 4593b69 | 2019-01-11 22:07:25 -0800 | [diff] [blame] | 240 | |
Dominik Laskowski | 0c25270 | 2021-12-20 20:32:09 -0800 | [diff] [blame] | 241 | RefreshRateConfigs(const RefreshRateConfigs&) = delete; |
| 242 | RefreshRateConfigs& operator=(const RefreshRateConfigs&) = delete; |
| 243 | |
Marin Shalamanov | a7fe304 | 2021-01-29 21:02:08 +0100 | [diff] [blame] | 244 | // Returns whether switching modes (refresh rate or resolution) is possible. |
| 245 | // 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] | 246 | // differ in resolution. |
Marin Shalamanov | eadf2e7 | 2020-12-10 15:35:28 +0100 | [diff] [blame] | 247 | bool canSwitch() const EXCLUDES(mLock) { |
| 248 | std::lock_guard lock(mLock); |
Dominik Laskowski | b0054a2 | 2022-03-03 09:03:06 -0800 | [diff] [blame] | 249 | return mDisplayModes.size() > 1; |
Marin Shalamanov | eadf2e7 | 2020-12-10 15:35:28 +0100 | [diff] [blame] | 250 | } |
Dominik Laskowski | 983f2b5 | 2020-06-25 16:54:06 -0700 | [diff] [blame] | 251 | |
TreeHugger Robot | 758ab61 | 2021-06-22 19:17:29 +0000 | [diff] [blame] | 252 | // Class to enumerate options around toggling the kernel timer on and off. |
Ana Krulec | b9afd79 | 2020-06-11 13:16:15 -0700 | [diff] [blame] | 253 | enum class KernelIdleTimerAction { |
Ana Krulec | b9afd79 | 2020-06-11 13:16:15 -0700 | [diff] [blame] | 254 | TurnOff, // Turn off the idle timer. |
| 255 | TurnOn // Turn on the idle timer. |
| 256 | }; |
ramindani | 32cf060 | 2022-03-02 02:30:29 +0000 | [diff] [blame] | 257 | |
Ana Krulec | b9afd79 | 2020-06-11 13:16:15 -0700 | [diff] [blame] | 258 | // Checks whether kernel idle timer should be active depending the policy decisions around |
| 259 | // refresh rates. |
| 260 | KernelIdleTimerAction getIdleTimerAction() const; |
| 261 | |
Andy Yu | 2ae6b6b | 2021-11-18 14:51:06 -0800 | [diff] [blame] | 262 | bool supportsFrameRateOverrideByContent() const { return mSupportsFrameRateOverrideByContent; } |
Ady Abraham | 64c2fc0 | 2020-12-29 12:07:50 -0800 | [diff] [blame] | 263 | |
Ady Abraham | cc31549 | 2022-02-17 17:06:39 -0800 | [diff] [blame] | 264 | // Return the display refresh rate divisor to match the layer |
Ady Abraham | 5cc2e26 | 2021-03-25 13:09:17 -0700 | [diff] [blame] | 265 | // frame rate, or 0 if the display refresh rate is not a multiple of the |
| 266 | // layer refresh rate. |
Dominik Laskowski | b0054a2 | 2022-03-03 09:03:06 -0800 | [diff] [blame] | 267 | static int getFrameRateDivisor(Fps displayRefreshRate, Fps layerFrameRate); |
Ady Abraham | 62a0be2 | 2020-12-08 16:54:10 -0800 | [diff] [blame] | 268 | |
Marin Shalamanov | 15a0fc6 | 2021-08-16 18:20:21 +0200 | [diff] [blame] | 269 | // Returns if the provided frame rates have a ratio t*1000/1001 or t*1001/1000 |
| 270 | // for an integer t. |
| 271 | static bool isFractionalPairOrMultiple(Fps, Fps); |
| 272 | |
Ady Abraham | 62a0be2 | 2020-12-08 16:54:10 -0800 | [diff] [blame] | 273 | using UidToFrameRateOverride = std::map<uid_t, Fps>; |
Dominik Laskowski | 6eab42d | 2021-09-13 14:34:13 -0700 | [diff] [blame] | 274 | |
Ady Abraham | dd5bfa9 | 2021-01-07 17:56:08 -0800 | [diff] [blame] | 275 | // Returns the frame rate override for each uid. |
Dominik Laskowski | 6eab42d | 2021-09-13 14:34:13 -0700 | [diff] [blame] | 276 | UidToFrameRateOverride getFrameRateOverrides(const std::vector<LayerRequirement>&, |
| 277 | Fps displayFrameRate, GlobalSignals) const |
Ady Abraham | dd5bfa9 | 2021-01-07 17:56:08 -0800 | [diff] [blame] | 278 | EXCLUDES(mLock); |
Ady Abraham | 0bb6a47 | 2020-10-12 10:22:13 -0700 | [diff] [blame] | 279 | |
ramindani | 32cf060 | 2022-03-02 02:30:29 +0000 | [diff] [blame] | 280 | std::optional<KernelIdleTimerController> kernelIdleTimerController() { |
| 281 | return mConfig.kernelIdleTimerController; |
| 282 | } |
Ady Abraham | 9a2ea34 | 2021-09-03 17:32:34 -0700 | [diff] [blame] | 283 | |
Dominik Laskowski | 83bd771 | 2022-01-07 14:30:53 -0800 | [diff] [blame] | 284 | struct IdleTimerCallbacks { |
| 285 | struct Callbacks { |
| 286 | std::function<void()> onReset; |
| 287 | std::function<void()> onExpired; |
| 288 | }; |
| 289 | |
| 290 | Callbacks platform; |
| 291 | Callbacks kernel; |
| 292 | }; |
| 293 | |
| 294 | void setIdleTimerCallbacks(IdleTimerCallbacks callbacks) EXCLUDES(mIdleTimerCallbacksMutex) { |
Ady Abraham | 9a2ea34 | 2021-09-03 17:32:34 -0700 | [diff] [blame] | 295 | std::scoped_lock lock(mIdleTimerCallbacksMutex); |
Dominik Laskowski | 83bd771 | 2022-01-07 14:30:53 -0800 | [diff] [blame] | 296 | mIdleTimerCallbacks = std::move(callbacks); |
| 297 | } |
| 298 | |
| 299 | void clearIdleTimerCallbacks() EXCLUDES(mIdleTimerCallbacksMutex) { |
| 300 | std::scoped_lock lock(mIdleTimerCallbacksMutex); |
| 301 | mIdleTimerCallbacks.reset(); |
Ady Abraham | 1166340 | 2021-11-10 19:46:09 -0800 | [diff] [blame] | 302 | } |
| 303 | |
| 304 | void startIdleTimer() { |
| 305 | if (mIdleTimer) { |
| 306 | mIdleTimer->start(); |
| 307 | } |
| 308 | } |
| 309 | |
| 310 | void stopIdleTimer() { |
| 311 | if (mIdleTimer) { |
| 312 | mIdleTimer->stop(); |
| 313 | } |
Ady Abraham | 9a2ea34 | 2021-09-03 17:32:34 -0700 | [diff] [blame] | 314 | } |
| 315 | |
| 316 | void resetIdleTimer(bool kernelOnly) { |
| 317 | if (!mIdleTimer) { |
| 318 | return; |
| 319 | } |
ramindani | 32cf060 | 2022-03-02 02:30:29 +0000 | [diff] [blame] | 320 | if (kernelOnly && !mConfig.kernelIdleTimerController.has_value()) { |
Ady Abraham | 9a2ea34 | 2021-09-03 17:32:34 -0700 | [diff] [blame] | 321 | return; |
| 322 | } |
| 323 | mIdleTimer->reset(); |
Dominik Laskowski | 83bd771 | 2022-01-07 14:30:53 -0800 | [diff] [blame] | 324 | } |
Ady Abraham | 9a2ea34 | 2021-09-03 17:32:34 -0700 | [diff] [blame] | 325 | |
Marin Shalamanov | ba421a8 | 2020-11-10 21:49:26 +0100 | [diff] [blame] | 326 | void dump(std::string& result) const EXCLUDES(mLock); |
| 327 | |
ramindani | 32cf060 | 2022-03-02 02:30:29 +0000 | [diff] [blame] | 328 | std::chrono::milliseconds getIdleTimerTimeout(); |
| 329 | |
Dominik Laskowski | 22488f6 | 2019-03-28 09:53:04 -0700 | [diff] [blame] | 330 | private: |
Dominik Laskowski | 0c25270 | 2021-12-20 20:32:09 -0800 | [diff] [blame] | 331 | friend struct TestableRefreshRateConfigs; |
Ady Abraham | b1b9d41 | 2020-06-01 19:53:52 -0700 | [diff] [blame] | 332 | |
Ady Abraham | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame] | 333 | void constructAvailableRefreshRates() REQUIRES(mLock); |
| 334 | |
Dominik Laskowski | b0054a2 | 2022-03-03 09:03:06 -0800 | [diff] [blame] | 335 | std::pair<DisplayModePtr, GlobalSignals> getBestRefreshRateLocked( |
Dominik Laskowski | a8626ec | 2021-12-15 18:13:30 -0800 | [diff] [blame] | 336 | const std::vector<LayerRequirement>&, GlobalSignals) const REQUIRES(mLock); |
Marin Shalamanov | 4c7831e | 2021-06-08 20:44:06 +0200 | [diff] [blame] | 337 | |
Ady Abraham | 4ccdcb4 | 2020-02-11 17:34:34 -0800 | [diff] [blame] | 338 | // Returns number of display frames and remainder when dividing the layer refresh period by |
| 339 | // display refresh period. |
| 340 | std::pair<nsecs_t, nsecs_t> getDisplayFrames(nsecs_t layerPeriod, nsecs_t displayPeriod) const; |
| 341 | |
Steven Thomas | f734df4 | 2020-04-13 21:09:28 -0700 | [diff] [blame] | 342 | // Returns the lowest refresh rate according to the current policy. May change at runtime. Only |
| 343 | // uses the primary range, not the app request range. |
Dominik Laskowski | b0054a2 | 2022-03-03 09:03:06 -0800 | [diff] [blame] | 344 | const DisplayModePtr& getMinRefreshRateByPolicyLocked() const REQUIRES(mLock); |
Steven Thomas | f734df4 | 2020-04-13 21:09:28 -0700 | [diff] [blame] | 345 | |
| 346 | // Returns the highest refresh rate according to the current policy. May change at runtime. Only |
| 347 | // uses the primary range, not the app request range. |
Dominik Laskowski | b0054a2 | 2022-03-03 09:03:06 -0800 | [diff] [blame] | 348 | const DisplayModePtr& getMaxRefreshRateByPolicyLocked(int anchorGroup) const REQUIRES(mLock); |
| 349 | const DisplayModePtr& getMaxRefreshRateByPolicyLocked() const REQUIRES(mLock) { |
| 350 | return getMaxRefreshRateByPolicyLocked(mActiveModeIt->second->getGroup()); |
Marin Shalamanov | 8cd8a99 | 2021-09-14 23:22:49 +0200 | [diff] [blame] | 351 | } |
| 352 | |
Steven Thomas | d407190 | 2020-03-24 16:02:53 -0700 | [diff] [blame] | 353 | const Policy* getCurrentPolicyLocked() const REQUIRES(mLock); |
Marin Shalamanov | eadf2e7 | 2020-12-10 15:35:28 +0100 | [diff] [blame] | 354 | bool isPolicyValidLocked(const Policy& policy) const REQUIRES(mLock); |
Steven Thomas | d407190 | 2020-03-24 16:02:53 -0700 | [diff] [blame] | 355 | |
Ady Abraham | 62a0be2 | 2020-12-08 16:54:10 -0800 | [diff] [blame] | 356 | // calculates a score for a layer. Used to determine the display refresh rate |
| 357 | // and the frame rate override for certains applications. |
Dominik Laskowski | b0054a2 | 2022-03-03 09:03:06 -0800 | [diff] [blame] | 358 | float calculateLayerScoreLocked(const LayerRequirement&, Fps refreshRate, |
Ady Abraham | 62a0be2 | 2020-12-08 16:54:10 -0800 | [diff] [blame] | 359 | bool isSeamlessSwitch) const REQUIRES(mLock); |
| 360 | |
Dominik Laskowski | b0054a2 | 2022-03-03 09:03:06 -0800 | [diff] [blame] | 361 | float calculateNonExactMatchingLayerScoreLocked(const LayerRequirement&, Fps refreshRate) const |
| 362 | REQUIRES(mLock); |
Ady Abraham | 05243be | 2021-09-16 15:58:52 -0700 | [diff] [blame] | 363 | |
Dominik Laskowski | b0054a2 | 2022-03-03 09:03:06 -0800 | [diff] [blame] | 364 | void updateDisplayModes(DisplayModes, DisplayModeId activeModeId) EXCLUDES(mLock); |
Ady Abraham | 3efa394 | 2021-06-24 19:01:25 -0700 | [diff] [blame] | 365 | |
Ady Abraham | 9a2ea34 | 2021-09-03 17:32:34 -0700 | [diff] [blame] | 366 | void initializeIdleTimer(); |
| 367 | |
Dominik Laskowski | 83bd771 | 2022-01-07 14:30:53 -0800 | [diff] [blame] | 368 | std::optional<IdleTimerCallbacks::Callbacks> getIdleTimerCallbacks() const |
| 369 | REQUIRES(mIdleTimerCallbacksMutex) { |
| 370 | if (!mIdleTimerCallbacks) return {}; |
ramindani | 32cf060 | 2022-03-02 02:30:29 +0000 | [diff] [blame] | 371 | return mConfig.kernelIdleTimerController.has_value() ? mIdleTimerCallbacks->kernel |
| 372 | : mIdleTimerCallbacks->platform; |
Dominik Laskowski | 83bd771 | 2022-01-07 14:30:53 -0800 | [diff] [blame] | 373 | } |
| 374 | |
Dominik Laskowski | b0054a2 | 2022-03-03 09:03:06 -0800 | [diff] [blame] | 375 | // The display modes of the active display. The DisplayModeIterators below are pointers into |
| 376 | // this container, so must be invalidated whenever the DisplayModes change. The Policy below |
| 377 | // is also dependent, so must be reset as well. |
| 378 | DisplayModes mDisplayModes GUARDED_BY(mLock); |
Ady Abraham | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame] | 379 | |
Dominik Laskowski | b0054a2 | 2022-03-03 09:03:06 -0800 | [diff] [blame] | 380 | DisplayModeIterator mActiveModeIt GUARDED_BY(mLock); |
| 381 | DisplayModeIterator mMinRefreshRateModeIt GUARDED_BY(mLock); |
| 382 | DisplayModeIterator mMaxRefreshRateModeIt GUARDED_BY(mLock); |
Steven Thomas | f734df4 | 2020-04-13 21:09:28 -0700 | [diff] [blame] | 383 | |
Dominik Laskowski | b0054a2 | 2022-03-03 09:03:06 -0800 | [diff] [blame] | 384 | // Display modes that satisfy the Policy's ranges, filtered and sorted by refresh rate. |
| 385 | std::vector<DisplayModeIterator> mPrimaryRefreshRates GUARDED_BY(mLock); |
| 386 | std::vector<DisplayModeIterator> mAppRequestRefreshRates GUARDED_BY(mLock); |
Ady Abraham | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame] | 387 | |
Steven Thomas | d407190 | 2020-03-24 16:02:53 -0700 | [diff] [blame] | 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 | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame] | 391 | mutable std::mutex mLock; |
Ady Abraham | b1b9d41 | 2020-06-01 19:53:52 -0700 | [diff] [blame] | 392 | |
| 393 | // A sorted list of known frame rates that a Heuristic layer will choose |
| 394 | // from based on the closest value. |
Marin Shalamanov | e8a663d | 2020-11-24 17:48:00 +0100 | [diff] [blame] | 395 | const std::vector<Fps> mKnownFrameRates; |
Ady Abraham | 64c2fc0 | 2020-12-29 12:07:50 -0800 | [diff] [blame] | 396 | |
rnlee | 3bd61066 | 2021-06-23 16:27:57 -0700 | [diff] [blame] | 397 | const Config mConfig; |
Andy Yu | 2ae6b6b | 2021-11-18 14:51:06 -0800 | [diff] [blame] | 398 | bool mSupportsFrameRateOverrideByContent; |
Marin Shalamanov | 4c7831e | 2021-06-08 20:44:06 +0200 | [diff] [blame] | 399 | |
Dominik Laskowski | a8626ec | 2021-12-15 18:13:30 -0800 | [diff] [blame] | 400 | struct GetBestRefreshRateCache { |
| 401 | std::pair<std::vector<LayerRequirement>, GlobalSignals> arguments; |
Dominik Laskowski | b0054a2 | 2022-03-03 09:03:06 -0800 | [diff] [blame] | 402 | std::pair<DisplayModePtr, GlobalSignals> result; |
Marin Shalamanov | 4c7831e | 2021-06-08 20:44:06 +0200 | [diff] [blame] | 403 | }; |
Dominik Laskowski | a8626ec | 2021-12-15 18:13:30 -0800 | [diff] [blame] | 404 | mutable std::optional<GetBestRefreshRateCache> mGetBestRefreshRateCache GUARDED_BY(mLock); |
Ady Abraham | 9a2ea34 | 2021-09-03 17:32:34 -0700 | [diff] [blame] | 405 | |
Dominik Laskowski | 83bd771 | 2022-01-07 14:30:53 -0800 | [diff] [blame] | 406 | // Declare mIdleTimer last to ensure its thread joins before the mutex/callbacks are destroyed. |
Ady Abraham | 9a2ea34 | 2021-09-03 17:32:34 -0700 | [diff] [blame] | 407 | std::mutex mIdleTimerCallbacksMutex; |
| 408 | std::optional<IdleTimerCallbacks> mIdleTimerCallbacks GUARDED_BY(mIdleTimerCallbacksMutex); |
Dominik Laskowski | 83bd771 | 2022-01-07 14:30:53 -0800 | [diff] [blame] | 409 | // Used to detect (lack of) frame activity. |
| 410 | std::optional<scheduler::OneShotTimer> mIdleTimer; |
Ana Krulec | b43429d | 2019-01-09 14:28:51 -0800 | [diff] [blame] | 411 | }; |
| 412 | |
Dominik Laskowski | 9804183 | 2019-08-01 18:35:59 -0700 | [diff] [blame] | 413 | } // namespace android::scheduler |