blob: 0f0fe22a888cbefdcb265a87ff9c9bcf0b10f444 [file] [log] [blame]
Ana Krulecb43429d2019-01-09 14:28:51 -08001/*
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 Laskowski98041832019-08-01 18:35:59 -070019#include <android-base/stringprintf.h>
Ady Abraham62f216c2020-10-13 19:07:23 -070020#include <gui/DisplayEventReceiver.h>
Dominik Laskowski98041832019-08-01 18:35:59 -070021
Ana Krulecb43429d2019-01-09 14:28:51 -080022#include <algorithm>
23#include <numeric>
Steven Thomasd4071902020-03-24 16:02:53 -070024#include <optional>
Dominik Laskowski98041832019-08-01 18:35:59 -070025#include <type_traits>
Ana Krulecb43429d2019-01-09 14:28:51 -080026
Marin Shalamanov3ea1d602020-12-16 19:59:39 +010027#include "DisplayHardware/DisplayMode.h"
Ana Krulec4593b692019-01-11 22:07:25 -080028#include "DisplayHardware/HWComposer.h"
Marin Shalamanove8a663d2020-11-24 17:48:00 +010029#include "Fps.h"
Ady Abraham9a2ea342021-09-03 17:32:34 -070030#include "Scheduler/OneShotTimer.h"
Ana Krulec4593b692019-01-11 22:07:25 -080031#include "Scheduler/SchedulerUtils.h"
Marin Shalamanov53fc11d2020-11-20 14:00:13 +010032#include "Scheduler/Seamlessness.h"
Ady Abraham2139f732019-11-13 18:56:40 -080033#include "Scheduler/StrongTyping.h"
Ana Krulec4593b692019-01-11 22:07:25 -080034
Dominik Laskowski98041832019-08-01 18:35:59 -070035namespace android::scheduler {
Ady Abrahamabc27602020-04-08 17:20:29 -070036
Ady Abraham4ccdcb42020-02-11 17:34:34 -080037using namespace std::chrono_literals;
Dominik Laskowski98041832019-08-01 18:35:59 -070038
39enum class RefreshRateConfigEvent : unsigned { None = 0b0, Changed = 0b1 };
40
41inline RefreshRateConfigEvent operator|(RefreshRateConfigEvent lhs, RefreshRateConfigEvent rhs) {
42 using T = std::underlying_type_t<RefreshRateConfigEvent>;
43 return static_cast<RefreshRateConfigEvent>(static_cast<T>(lhs) | static_cast<T>(rhs));
44}
Ana Krulecb43429d2019-01-09 14:28:51 -080045
Ady Abraham62f216c2020-10-13 19:07:23 -070046using FrameRateOverride = DisplayEventReceiver::Event::FrameRateOverride;
47
Ana Krulecb43429d2019-01-09 14:28:51 -080048/**
Ady Abraham1902d072019-03-01 17:18:59 -080049 * This class is used to encapsulate configuration for refresh rates. It holds information
Ana Krulecb43429d2019-01-09 14:28:51 -080050 * about available refresh rates on the device, and the mapping between the numbers and human
51 * readable names.
52 */
53class RefreshRateConfigs {
54public:
Ady Abraham4ccdcb42020-02-11 17:34:34 -080055 // Margin used when matching refresh rates to the content desired ones.
56 static constexpr nsecs_t MARGIN_FOR_PERIOD_CALCULATION =
rnlee3bd610662021-06-23 16:27:57 -070057 std::chrono::nanoseconds(800us).count();
Ady Abraham4ccdcb42020-02-11 17:34:34 -080058
Ady Abrahamabc27602020-04-08 17:20:29 -070059 class RefreshRate {
60 private:
61 // Effectively making the constructor private while allowing
62 // std::make_unique to create the object
63 struct ConstructorTag {
64 explicit ConstructorTag(int) {}
65 };
Ana Krulec72f0d6e2020-01-06 15:24:47 -080066
Ady Abrahamabc27602020-04-08 17:20:29 -070067 public:
Ady Abraham6b7ad652021-06-23 17:34:57 -070068 RefreshRate(DisplayModePtr mode, ConstructorTag) : mode(mode) {}
Ady Abraham2e1dd892020-03-05 13:48:36 -080069
Ady Abraham6b7ad652021-06-23 17:34:57 -070070 DisplayModeId getModeId() const { return mode->getId(); }
Marin Shalamanova7fe3042021-01-29 21:02:08 +010071 nsecs_t getVsyncPeriod() const { return mode->getVsyncPeriod(); }
72 int32_t getModeGroup() const { return mode->getGroup(); }
Ady Abraham6b7ad652021-06-23 17:34:57 -070073 std::string getName() const { return to_string(getFps()); }
74 Fps getFps() const { return mode->getFps(); }
Ady Abrahamb4937392021-06-23 18:05:03 -070075 DisplayModePtr getMode() const { return mode; }
Ady Abraham2139f732019-11-13 18:56:40 -080076
Ana Krulec72f0d6e2020-01-06 15:24:47 -080077 // Checks whether the fps of this RefreshRate struct is within a given min and max refresh
Marin Shalamanove8a663d2020-11-24 17:48:00 +010078 // rate passed in. Margin of error is applied to the boundaries for approximation.
79 bool inPolicy(Fps minRefreshRate, Fps maxRefreshRate) const {
Ady Abraham6b7ad652021-06-23 17:34:57 -070080 return minRefreshRate.lessThanOrEqualWithMargin(getFps()) &&
81 getFps().lessThanOrEqualWithMargin(maxRefreshRate);
Ana Krulec72f0d6e2020-01-06 15:24:47 -080082 }
83
Ady Abraham6b7ad652021-06-23 17:34:57 -070084 bool operator!=(const RefreshRate& other) const { return mode != other.mode; }
Ady Abraham2139f732019-11-13 18:56:40 -080085
Marin Shalamanove8a663d2020-11-24 17:48:00 +010086 bool operator<(const RefreshRate& other) const {
87 return getFps().getValue() < other.getFps().getValue();
88 }
Ana Krulecb9afd792020-06-11 13:16:15 -070089
Ady Abraham2139f732019-11-13 18:56:40 -080090 bool operator==(const RefreshRate& other) const { return !(*this != other); }
Ady Abrahamabc27602020-04-08 17:20:29 -070091
Marin Shalamanov46084422020-10-13 12:33:42 +020092 std::string toString() const;
Marin Shalamanov65ce5512021-01-22 20:57:13 +010093 friend std::ostream& operator<<(std::ostream& os, const RefreshRate& refreshRate) {
94 return os << refreshRate.toString();
95 }
Marin Shalamanov46084422020-10-13 12:33:42 +020096
Ady Abrahamabc27602020-04-08 17:20:29 -070097 private:
98 friend RefreshRateConfigs;
Ady Abrahamb1b9d412020-06-01 19:53:52 -070099 friend class RefreshRateConfigsTest;
Ady Abrahamabc27602020-04-08 17:20:29 -0700100
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100101 DisplayModePtr mode;
Ana Krulecb43429d2019-01-09 14:28:51 -0800102 };
103
Ady Abraham2e1dd892020-03-05 13:48:36 -0800104 using AllRefreshRatesMapType =
Marin Shalamanov23c44202020-12-22 19:09:20 +0100105 std::unordered_map<DisplayModeId, std::unique_ptr<const RefreshRate>>;
Ady Abraham2139f732019-11-13 18:56:40 -0800106
Marin Shalamanoveadf2e72020-12-10 15:35:28 +0100107 struct FpsRange {
108 Fps min{0.0f};
109 Fps max{std::numeric_limits<float>::max()};
110
111 bool operator==(const FpsRange& other) const {
112 return min.equalsWithMargin(other.min) && max.equalsWithMargin(other.max);
113 }
114
115 bool operator!=(const FpsRange& other) const { return !(*this == other); }
116
117 std::string toString() const {
118 return base::StringPrintf("[%s %s]", to_string(min).c_str(), to_string(max).c_str());
119 }
120 };
121
Steven Thomasd4071902020-03-24 16:02:53 -0700122 struct Policy {
Marin Shalamanov30b0b3c2020-10-13 19:15:06 +0200123 private:
124 static constexpr int kAllowGroupSwitchingDefault = false;
125
126 public:
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100127 // The default mode, used to ensure we only initiate display mode switches within the
128 // same mode group as defaultMode's group.
129 DisplayModeId defaultMode;
130 // Whether or not we switch mode groups to get the best frame rate.
Marin Shalamanov30b0b3c2020-10-13 19:15:06 +0200131 bool allowGroupSwitching = kAllowGroupSwitchingDefault;
Steven Thomasf734df42020-04-13 21:09:28 -0700132 // The primary refresh rate range represents display manager's general guidance on the
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100133 // display modes we'll consider when switching refresh rates. Unless we get an explicit
Steven Thomasf734df42020-04-13 21:09:28 -0700134 // signal from an app, we should stay within this range.
Marin Shalamanoveadf2e72020-12-10 15:35:28 +0100135 FpsRange primaryRange;
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100136 // The app request refresh rate range allows us to consider more display modes when
Steven Thomasf734df42020-04-13 21:09:28 -0700137 // switching refresh rates. Although we should generally stay within the primary range,
138 // specific considerations, such as layer frame rate settings specified via the
139 // setFrameRate() api, may cause us to go outside the primary range. We never go outside the
140 // app request range. The app request range will be greater than or equal to the primary
141 // refresh rate range, never smaller.
Marin Shalamanoveadf2e72020-12-10 15:35:28 +0100142 FpsRange appRequestRange;
Steven Thomasd4071902020-03-24 16:02:53 -0700143
Steven Thomasf734df42020-04-13 21:09:28 -0700144 Policy() = default;
Marin Shalamanov30b0b3c2020-10-13 19:15:06 +0200145
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100146 Policy(DisplayModeId defaultMode, const FpsRange& range)
147 : Policy(defaultMode, kAllowGroupSwitchingDefault, range, range) {}
Marin Shalamanov30b0b3c2020-10-13 19:15:06 +0200148
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100149 Policy(DisplayModeId defaultMode, bool allowGroupSwitching, const FpsRange& range)
150 : Policy(defaultMode, allowGroupSwitching, range, range) {}
Marin Shalamanov30b0b3c2020-10-13 19:15:06 +0200151
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100152 Policy(DisplayModeId defaultMode, const FpsRange& primaryRange,
Marin Shalamanoveadf2e72020-12-10 15:35:28 +0100153 const FpsRange& appRequestRange)
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100154 : Policy(defaultMode, kAllowGroupSwitchingDefault, primaryRange, appRequestRange) {}
Marin Shalamanov30b0b3c2020-10-13 19:15:06 +0200155
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100156 Policy(DisplayModeId defaultMode, bool allowGroupSwitching, const FpsRange& primaryRange,
Marin Shalamanov23c44202020-12-22 19:09:20 +0100157 const FpsRange& appRequestRange)
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100158 : defaultMode(defaultMode),
Marin Shalamanov30b0b3c2020-10-13 19:15:06 +0200159 allowGroupSwitching(allowGroupSwitching),
Steven Thomasf734df42020-04-13 21:09:28 -0700160 primaryRange(primaryRange),
161 appRequestRange(appRequestRange) {}
162
Steven Thomasd4071902020-03-24 16:02:53 -0700163 bool operator==(const Policy& other) const {
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100164 return defaultMode == other.defaultMode && primaryRange == other.primaryRange &&
Steven Thomasf734df42020-04-13 21:09:28 -0700165 appRequestRange == other.appRequestRange &&
Steven Thomasd4071902020-03-24 16:02:53 -0700166 allowGroupSwitching == other.allowGroupSwitching;
167 }
168
169 bool operator!=(const Policy& other) const { return !(*this == other); }
Marin Shalamanovb6674e72020-11-06 13:05:57 +0100170 std::string toString() const;
Steven Thomasd4071902020-03-24 16:02:53 -0700171 };
172
173 // Return code set*Policy() to indicate the current policy is unchanged.
174 static constexpr int CURRENT_POLICY_UNCHANGED = 1;
175
176 // We maintain the display manager policy and the override policy separately. The override
177 // policy is used by CTS tests to get a consistent device state for testing. While the override
178 // policy is set, it takes precedence over the display manager policy. Once the override policy
179 // is cleared, we revert to using the display manager policy.
180
181 // Sets the display manager policy to choose refresh rates. The return value will be:
182 // - A negative value if the policy is invalid or another error occurred.
183 // - NO_ERROR if the policy was successfully updated, and the current policy is different from
184 // what it was before the call.
185 // - CURRENT_POLICY_UNCHANGED if the policy was successfully updated, but the current policy
186 // is the same as it was before the call.
187 status_t setDisplayManagerPolicy(const Policy& policy) EXCLUDES(mLock);
188 // Sets the override policy. See setDisplayManagerPolicy() for the meaning of the return value.
189 status_t setOverridePolicy(const std::optional<Policy>& policy) EXCLUDES(mLock);
190 // Gets the current policy, which will be the override policy if active, and the display manager
191 // policy otherwise.
192 Policy getCurrentPolicy() const EXCLUDES(mLock);
193 // Gets the display manager policy, regardless of whether an override policy is active.
194 Policy getDisplayManagerPolicy() const EXCLUDES(mLock);
Ana Kruleced3a8cc2019-11-14 00:55:07 +0100195
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100196 // Returns true if mode is allowed by the current policy.
197 bool isModeAllowed(DisplayModeId) const EXCLUDES(mLock);
Ady Abraham2139f732019-11-13 18:56:40 -0800198
Ady Abraham8a82ba62020-01-17 12:43:17 -0800199 // Describes the different options the layer voted for refresh rate
200 enum class LayerVoteType {
Ady Abraham71c437d2020-01-31 15:56:57 -0800201 NoVote, // Doesn't care about the refresh rate
202 Min, // Minimal refresh rate available
203 Max, // Maximal refresh rate available
204 Heuristic, // Specific refresh rate that was calculated by platform using a heuristic
205 ExplicitDefault, // Specific refresh rate that was provided by the app with Default
206 // compatibility
Ady Abrahamdd5bfa92021-01-07 17:56:08 -0800207 ExplicitExactOrMultiple, // Specific refresh rate that was provided by the app with
208 // ExactOrMultiple compatibility
209 ExplicitExact, // Specific refresh rate that was provided by the app with
210 // Exact compatibility
211
Ady Abraham8a82ba62020-01-17 12:43:17 -0800212 };
213
214 // Captures the layer requirements for a refresh rate. This will be used to determine the
215 // display refresh rate.
216 struct LayerRequirement {
Ady Abrahamaae5ed52020-06-26 09:32:43 -0700217 // Layer's name. Used for debugging purposes.
218 std::string name;
Ady Abraham62a0be22020-12-08 16:54:10 -0800219 // Layer's owner uid
220 uid_t ownerUid = static_cast<uid_t>(-1);
Ady Abrahamaae5ed52020-06-26 09:32:43 -0700221 // Layer vote type.
222 LayerVoteType vote = LayerVoteType::NoVote;
223 // Layer's desired refresh rate, if applicable.
Marin Shalamanove8a663d2020-11-24 17:48:00 +0100224 Fps desiredRefreshRate{0.0f};
Marin Shalamanov46084422020-10-13 12:33:42 +0200225 // If a seamless mode switch is required.
Marin Shalamanov53fc11d2020-11-20 14:00:13 +0100226 Seamlessness seamlessness = Seamlessness::Default;
Ady Abrahamaae5ed52020-06-26 09:32:43 -0700227 // Layer's weight in the range of [0, 1]. The higher the weight the more impact this layer
228 // would have on choosing the refresh rate.
229 float weight = 0.0f;
230 // Whether layer is in focus or not based on WindowManager's state
231 bool focused = false;
Ady Abraham8a82ba62020-01-17 12:43:17 -0800232
233 bool operator==(const LayerRequirement& other) const {
234 return name == other.name && vote == other.vote &&
Marin Shalamanove8a663d2020-11-24 17:48:00 +0100235 desiredRefreshRate.equalsWithMargin(other.desiredRefreshRate) &&
Marin Shalamanovbe97cfa2020-12-01 16:02:07 +0100236 seamlessness == other.seamlessness && weight == other.weight &&
Ady Abrahamaae5ed52020-06-26 09:32:43 -0700237 focused == other.focused;
Ady Abraham8a82ba62020-01-17 12:43:17 -0800238 }
239
240 bool operator!=(const LayerRequirement& other) const { return !(*this == other); }
241 };
242
Ady Abrahamdfd62162020-06-10 16:11:56 -0700243 // Global state describing signals that affect refresh rate choice.
244 struct GlobalSignals {
245 // Whether the user touched the screen recently. Used to apply touch boost.
246 bool touch = false;
247 // True if the system hasn't seen any buffers posted to layers recently.
248 bool idle = false;
Marin Shalamanov4c7831e2021-06-08 20:44:06 +0200249
250 bool operator==(const GlobalSignals& other) const {
251 return touch == other.touch && idle == other.idle;
252 }
Ady Abrahamdfd62162020-06-10 16:11:56 -0700253 };
254
Steven Thomasbb374322020-04-28 22:47:16 -0700255 // Returns the refresh rate that fits best to the given layers.
256 // layers - The layer requirements to consider.
Ady Abrahamdfd62162020-06-10 16:11:56 -0700257 // globalSignals - global state of touch and idle
258 // outSignalsConsidered - An output param that tells the caller whether the refresh rate was
259 // chosen based on touch boost and/or idle timer.
Marin Shalamanoveadf2e72020-12-10 15:35:28 +0100260 RefreshRate getBestRefreshRate(const std::vector<LayerRequirement>& layers,
261 const GlobalSignals& globalSignals,
262 GlobalSignals* outSignalsConsidered = nullptr) const
Ady Abraham6fb599b2020-03-05 13:48:22 -0800263 EXCLUDES(mLock);
Ana Krulecb43429d2019-01-09 14:28:51 -0800264
Marin Shalamanoveadf2e72020-12-10 15:35:28 +0100265 FpsRange getSupportedRefreshRateRange() const EXCLUDES(mLock) {
266 std::lock_guard lock(mLock);
267 return {mMinSupportedRefreshRate->getFps(), mMaxSupportedRefreshRate->getFps()};
268 }
Steven Thomas2bbaabe2019-08-28 16:08:35 -0700269
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100270 std::optional<Fps> onKernelTimerChanged(std::optional<DisplayModeId> desiredActiveModeId,
Marin Shalamanoveadf2e72020-12-10 15:35:28 +0100271 bool timerExpired) const EXCLUDES(mLock);
Steven Thomas2bbaabe2019-08-28 16:08:35 -0700272
Steven Thomasf734df42020-04-13 21:09:28 -0700273 // Returns the highest refresh rate according to the current policy. May change at runtime. Only
274 // uses the primary range, not the app request range.
Marin Shalamanoveadf2e72020-12-10 15:35:28 +0100275 RefreshRate getMaxRefreshRateByPolicy() const EXCLUDES(mLock);
Ady Abraham2139f732019-11-13 18:56:40 -0800276
277 // Returns the current refresh rate
Marin Shalamanoveadf2e72020-12-10 15:35:28 +0100278 RefreshRate getCurrentRefreshRate() const EXCLUDES(mLock);
Ady Abraham2139f732019-11-13 18:56:40 -0800279
Ana Krulec5d477912020-02-07 12:02:38 -0800280 // Returns the current refresh rate, if allowed. Otherwise the default that is allowed by
281 // the policy.
Marin Shalamanoveadf2e72020-12-10 15:35:28 +0100282 RefreshRate getCurrentRefreshRateByPolicy() const;
Ana Krulec5d477912020-02-07 12:02:38 -0800283
Marin Shalamanov23c44202020-12-22 19:09:20 +0100284 // Returns the refresh rate that corresponds to a DisplayModeId. This may change at
Ady Abraham2139f732019-11-13 18:56:40 -0800285 // runtime.
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100286 // TODO(b/159590486) An invalid mode id may be given here if the dipslay modes have changed.
287 RefreshRate getRefreshRateFromModeId(DisplayModeId modeId) const EXCLUDES(mLock) {
Marin Shalamanoveadf2e72020-12-10 15:35:28 +0100288 std::lock_guard lock(mLock);
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100289 return *mRefreshRates.at(modeId);
Ady Abraham2139f732019-11-13 18:56:40 -0800290 };
291
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100292 // Stores the current modeId the device operates at
293 void setCurrentModeId(DisplayModeId) EXCLUDES(mLock);
Dominik Laskowski22488f62019-03-28 09:53:04 -0700294
Ady Abrahama6b676e2020-05-27 14:29:09 -0700295 // Returns a string that represents the layer vote type
296 static std::string layerVoteTypeString(LayerVoteType vote);
297
Ady Abrahamb1b9d412020-06-01 19:53:52 -0700298 // Returns a known frame rate that is the closest to frameRate
Marin Shalamanove8a663d2020-11-24 17:48:00 +0100299 Fps findClosestKnownFrameRate(Fps frameRate) const;
Ady Abrahamb1b9d412020-06-01 19:53:52 -0700300
rnlee3bd610662021-06-23 16:27:57 -0700301 // Configuration flags.
302 struct Config {
303 bool enableFrameRateOverride = false;
304
305 // Specifies the upper refresh rate threshold (inclusive) for layer vote types of multiple
306 // or heuristic, such that refresh rates higher than this value will not be voted for. 0 if
307 // no threshold is set.
308 int frameRateMultipleThreshold = 0;
Ady Abraham9a2ea342021-09-03 17:32:34 -0700309
310 // Whether to use idle timer callbacks that support the kernel timer.
311 bool supportKernelTimer = false;
rnlee3bd610662021-06-23 16:27:57 -0700312 };
313
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100314 RefreshRateConfigs(const DisplayModes& modes, DisplayModeId currentModeId,
rnlee3bd610662021-06-23 16:27:57 -0700315 Config config = {.enableFrameRateOverride = false,
Ady Abraham9a2ea342021-09-03 17:32:34 -0700316 .frameRateMultipleThreshold = 0,
317 .supportKernelTimer = false});
Ana Krulec4593b692019-01-11 22:07:25 -0800318
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100319 // Returns whether switching modes (refresh rate or resolution) is possible.
320 // TODO(b/158780872): Consider HAL support, and skip frame rate detection if the modes only
Dominik Laskowski983f2b52020-06-25 16:54:06 -0700321 // differ in resolution.
Marin Shalamanoveadf2e72020-12-10 15:35:28 +0100322 bool canSwitch() const EXCLUDES(mLock) {
323 std::lock_guard lock(mLock);
324 return mRefreshRates.size() > 1;
325 }
Dominik Laskowski983f2b52020-06-25 16:54:06 -0700326
TreeHugger Robot758ab612021-06-22 19:17:29 +0000327 // Class to enumerate options around toggling the kernel timer on and off.
Ana Krulecb9afd792020-06-11 13:16:15 -0700328 enum class KernelIdleTimerAction {
Ana Krulecb9afd792020-06-11 13:16:15 -0700329 TurnOff, // Turn off the idle timer.
330 TurnOn // Turn on the idle timer.
331 };
332 // Checks whether kernel idle timer should be active depending the policy decisions around
333 // refresh rates.
334 KernelIdleTimerAction getIdleTimerAction() const;
335
Ady Abraham64c2fc02020-12-29 12:07:50 -0800336 bool supportsFrameRateOverride() const { return mSupportsFrameRateOverride; }
337
Ady Abraham5cc2e262021-03-25 13:09:17 -0700338 // Return the display refresh rate divider to match the layer
339 // frame rate, or 0 if the display refresh rate is not a multiple of the
340 // layer refresh rate.
341 static int getFrameRateDivider(Fps displayFrameRate, Fps layerFrameRate);
Ady Abraham62a0be22020-12-08 16:54:10 -0800342
Ady Abraham62a0be22020-12-08 16:54:10 -0800343 using UidToFrameRateOverride = std::map<uid_t, Fps>;
Ady Abrahamdd5bfa92021-01-07 17:56:08 -0800344 // Returns the frame rate override for each uid.
345 //
346 // @param layers list of visible layers
347 // @param displayFrameRate the display frame rate
348 // @param touch whether touch timer is active (i.e. user touched the screen recently)
Ady Abraham62a0be22020-12-08 16:54:10 -0800349 UidToFrameRateOverride getFrameRateOverrides(const std::vector<LayerRequirement>& layers,
Ady Abrahamdd5bfa92021-01-07 17:56:08 -0800350 Fps displayFrameRate, bool touch) const
351 EXCLUDES(mLock);
Ady Abraham0bb6a472020-10-12 10:22:13 -0700352
Ady Abraham9a2ea342021-09-03 17:32:34 -0700353 bool supportsKernelIdleTimer() const { return mConfig.supportKernelTimer; }
354
355 void setIdleTimerCallbacks(std::function<void()> platformTimerReset,
356 std::function<void()> platformTimerExpired,
357 std::function<void()> kernelTimerReset,
358 std::function<void()> kernelTimerExpired) {
359 std::scoped_lock lock(mIdleTimerCallbacksMutex);
360 mIdleTimerCallbacks.emplace();
361 mIdleTimerCallbacks->platform.onReset = platformTimerReset;
362 mIdleTimerCallbacks->platform.onExpired = platformTimerExpired;
363 mIdleTimerCallbacks->kernel.onReset = kernelTimerReset;
364 mIdleTimerCallbacks->kernel.onExpired = kernelTimerExpired;
365 }
366
367 void resetIdleTimer(bool kernelOnly) {
368 if (!mIdleTimer) {
369 return;
370 }
371 if (kernelOnly && !mConfig.supportKernelTimer) {
372 return;
373 }
374 mIdleTimer->reset();
375 };
376
Marin Shalamanovba421a82020-11-10 21:49:26 +0100377 void dump(std::string& result) const EXCLUDES(mLock);
378
Ady Abraham3efa3942021-06-24 19:01:25 -0700379 RefreshRateConfigs(const RefreshRateConfigs&) = delete;
380 void operator=(const RefreshRateConfigs&) = delete;
381
Dominik Laskowski22488f62019-03-28 09:53:04 -0700382private:
Ady Abrahamb1b9d412020-06-01 19:53:52 -0700383 friend class RefreshRateConfigsTest;
384
Ady Abraham2139f732019-11-13 18:56:40 -0800385 void constructAvailableRefreshRates() REQUIRES(mLock);
386
Marin Shalamanoveadf2e72020-12-10 15:35:28 +0100387 void getSortedRefreshRateListLocked(
Ady Abraham2139f732019-11-13 18:56:40 -0800388 const std::function<bool(const RefreshRate&)>& shouldAddRefreshRate,
Marin Shalamanoveadf2e72020-12-10 15:35:28 +0100389 std::vector<const RefreshRate*>* outRefreshRates) REQUIRES(mLock);
Ady Abraham2139f732019-11-13 18:56:40 -0800390
Marin Shalamanov4c7831e2021-06-08 20:44:06 +0200391 std::optional<RefreshRate> getCachedBestRefreshRate(const std::vector<LayerRequirement>& layers,
392 const GlobalSignals& globalSignals,
393 GlobalSignals* outSignalsConsidered) const
394 REQUIRES(mLock);
395
396 RefreshRate getBestRefreshRateLocked(const std::vector<LayerRequirement>& layers,
397 const GlobalSignals& globalSignals,
398 GlobalSignals* outSignalsConsidered) const REQUIRES(mLock);
399
Ady Abraham34702102020-02-10 14:12:05 -0800400 // Returns the refresh rate with the highest score in the collection specified from begin
401 // to end. If there are more than one with the same highest refresh rate, the first one is
402 // returned.
403 template <typename Iter>
404 const RefreshRate* getBestRefreshRate(Iter begin, Iter end) const;
405
Ady Abraham4ccdcb42020-02-11 17:34:34 -0800406 // Returns number of display frames and remainder when dividing the layer refresh period by
407 // display refresh period.
408 std::pair<nsecs_t, nsecs_t> getDisplayFrames(nsecs_t layerPeriod, nsecs_t displayPeriod) const;
409
Steven Thomasf734df42020-04-13 21:09:28 -0700410 // Returns the lowest refresh rate according to the current policy. May change at runtime. Only
411 // uses the primary range, not the app request range.
412 const RefreshRate& getMinRefreshRateByPolicyLocked() const REQUIRES(mLock);
413
414 // Returns the highest refresh rate according to the current policy. May change at runtime. Only
415 // uses the primary range, not the app request range.
416 const RefreshRate& getMaxRefreshRateByPolicyLocked() const REQUIRES(mLock);
417
Ana Krulec3d367c82020-02-25 15:02:01 -0800418 // Returns the current refresh rate, if allowed. Otherwise the default that is allowed by
419 // the policy.
420 const RefreshRate& getCurrentRefreshRateByPolicyLocked() const REQUIRES(mLock);
421
Steven Thomasd4071902020-03-24 16:02:53 -0700422 const Policy* getCurrentPolicyLocked() const REQUIRES(mLock);
Marin Shalamanoveadf2e72020-12-10 15:35:28 +0100423 bool isPolicyValidLocked(const Policy& policy) const REQUIRES(mLock);
Steven Thomasd4071902020-03-24 16:02:53 -0700424
rnlee3bd610662021-06-23 16:27:57 -0700425 // Returns whether the layer is allowed to vote for the given refresh rate.
426 bool isVoteAllowed(const LayerRequirement&, const RefreshRate&) const;
427
Ady Abraham62a0be22020-12-08 16:54:10 -0800428 // calculates a score for a layer. Used to determine the display refresh rate
429 // and the frame rate override for certains applications.
430 float calculateLayerScoreLocked(const LayerRequirement&, const RefreshRate&,
431 bool isSeamlessSwitch) const REQUIRES(mLock);
432
Ady Abraham3efa3942021-06-24 19:01:25 -0700433 void updateDisplayModes(const DisplayModes& mode, DisplayModeId currentModeId) EXCLUDES(mLock);
434
Ady Abraham9a2ea342021-09-03 17:32:34 -0700435 void initializeIdleTimer();
436
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100437 // The list of refresh rates, indexed by display modes ID. This may change after this
Steven Thomas2bbaabe2019-08-28 16:08:35 -0700438 // object is initialized.
Marin Shalamanoveadf2e72020-12-10 15:35:28 +0100439 AllRefreshRatesMapType mRefreshRates GUARDED_BY(mLock);
Ady Abraham2139f732019-11-13 18:56:40 -0800440
Steven Thomasf734df42020-04-13 21:09:28 -0700441 // The list of refresh rates in the primary range of the current policy, ordered by vsyncPeriod
442 // (the first element is the lowest refresh rate).
443 std::vector<const RefreshRate*> mPrimaryRefreshRates GUARDED_BY(mLock);
444
445 // The list of refresh rates in the app request range of the current policy, ordered by
446 // vsyncPeriod (the first element is the lowest refresh rate).
447 std::vector<const RefreshRate*> mAppRequestRefreshRates GUARDED_BY(mLock);
Ady Abraham2139f732019-11-13 18:56:40 -0800448
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100449 // The current display mode. This will change at runtime. This is set by SurfaceFlinger on
Ady Abraham2139f732019-11-13 18:56:40 -0800450 // the main thread, and read by the Scheduler (and other objects) on other threads.
451 const RefreshRate* mCurrentRefreshRate GUARDED_BY(mLock);
452
Steven Thomasd4071902020-03-24 16:02:53 -0700453 // The policy values will change at runtime. They're set by SurfaceFlinger on the main thread,
454 // and read by the Scheduler (and other objects) on other threads.
455 Policy mDisplayManagerPolicy GUARDED_BY(mLock);
456 std::optional<Policy> mOverridePolicy GUARDED_BY(mLock);
Ady Abraham2139f732019-11-13 18:56:40 -0800457
458 // The min and max refresh rates supported by the device.
Marin Shalamanoveadf2e72020-12-10 15:35:28 +0100459 // This may change at runtime.
460 const RefreshRate* mMinSupportedRefreshRate GUARDED_BY(mLock);
461 const RefreshRate* mMaxSupportedRefreshRate GUARDED_BY(mLock);
Ady Abraham2139f732019-11-13 18:56:40 -0800462
Ady Abraham2139f732019-11-13 18:56:40 -0800463 mutable std::mutex mLock;
Ady Abrahamb1b9d412020-06-01 19:53:52 -0700464
465 // A sorted list of known frame rates that a Heuristic layer will choose
466 // from based on the closest value.
Marin Shalamanove8a663d2020-11-24 17:48:00 +0100467 const std::vector<Fps> mKnownFrameRates;
Ady Abraham64c2fc02020-12-29 12:07:50 -0800468
rnlee3bd610662021-06-23 16:27:57 -0700469 const Config mConfig;
Ady Abraham64c2fc02020-12-29 12:07:50 -0800470 bool mSupportsFrameRateOverride;
Marin Shalamanov4c7831e2021-06-08 20:44:06 +0200471
472 struct GetBestRefreshRateInvocation {
473 std::vector<LayerRequirement> layerRequirements;
474 GlobalSignals globalSignals;
475 GlobalSignals outSignalsConsidered;
476 RefreshRate resultingBestRefreshRate;
477 };
478 mutable std::optional<GetBestRefreshRateInvocation> lastBestRefreshRateInvocation
479 GUARDED_BY(mLock);
Ady Abraham9a2ea342021-09-03 17:32:34 -0700480
481 // Timer that records time between requests for next vsync.
482 std::optional<scheduler::OneShotTimer> mIdleTimer;
483
484 struct IdleTimerCallbacks {
485 struct Callbacks {
486 std::function<void()> onReset;
487 std::function<void()> onExpired;
488 };
489
490 Callbacks platform;
491 Callbacks kernel;
492 };
493
494 std::mutex mIdleTimerCallbacksMutex;
495 std::optional<IdleTimerCallbacks> mIdleTimerCallbacks GUARDED_BY(mIdleTimerCallbacksMutex);
Ana Krulecb43429d2019-01-09 14:28:51 -0800496};
497
Dominik Laskowski98041832019-08-01 18:35:59 -0700498} // namespace android::scheduler