blob: 4a9a1fd9fcb10e310cd10fd1206a755624f95878 [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"
Ana Krulec4593b692019-01-11 22:07:25 -080030#include "Scheduler/SchedulerUtils.h"
Marin Shalamanov53fc11d2020-11-20 14:00:13 +010031#include "Scheduler/Seamlessness.h"
Ady Abraham2139f732019-11-13 18:56:40 -080032#include "Scheduler/StrongTyping.h"
Ana Krulec4593b692019-01-11 22:07:25 -080033
Dominik Laskowski98041832019-08-01 18:35:59 -070034namespace android::scheduler {
Ady Abrahamabc27602020-04-08 17:20:29 -070035
Ady Abraham4ccdcb42020-02-11 17:34:34 -080036using namespace std::chrono_literals;
Dominik Laskowski98041832019-08-01 18:35:59 -070037
38enum class RefreshRateConfigEvent : unsigned { None = 0b0, Changed = 0b1 };
39
40inline 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 Krulecb43429d2019-01-09 14:28:51 -080044
Ady Abraham62f216c2020-10-13 19:07:23 -070045using FrameRateOverride = DisplayEventReceiver::Event::FrameRateOverride;
46
Ana Krulecb43429d2019-01-09 14:28:51 -080047/**
Ady Abraham1902d072019-03-01 17:18:59 -080048 * This class is used to encapsulate configuration for refresh rates. It holds information
Ana Krulecb43429d2019-01-09 14:28:51 -080049 * about available refresh rates on the device, and the mapping between the numbers and human
50 * readable names.
51 */
52class RefreshRateConfigs {
53public:
Ady Abraham4ccdcb42020-02-11 17:34:34 -080054 // Margin used when matching refresh rates to the content desired ones.
55 static constexpr nsecs_t MARGIN_FOR_PERIOD_CALCULATION =
rnlee3bd610662021-06-23 16:27:57 -070056 std::chrono::nanoseconds(800us).count();
Ady Abraham4ccdcb42020-02-11 17:34:34 -080057
Ady Abrahamabc27602020-04-08 17:20:29 -070058 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 Krulec72f0d6e2020-01-06 15:24:47 -080065
Ady Abrahamabc27602020-04-08 17:20:29 -070066 public:
Ady Abraham6b7ad652021-06-23 17:34:57 -070067 RefreshRate(DisplayModePtr mode, ConstructorTag) : mode(mode) {}
Ady Abraham2e1dd892020-03-05 13:48:36 -080068
Ady Abraham6b7ad652021-06-23 17:34:57 -070069 DisplayModeId getModeId() const { return mode->getId(); }
Marin Shalamanova7fe3042021-01-29 21:02:08 +010070 nsecs_t getVsyncPeriod() const { return mode->getVsyncPeriod(); }
71 int32_t getModeGroup() const { return mode->getGroup(); }
Ady Abraham6b7ad652021-06-23 17:34:57 -070072 std::string getName() const { return to_string(getFps()); }
73 Fps getFps() const { return mode->getFps(); }
Ady Abraham690f4612021-07-01 23:24:03 -070074 DisplayModePtr getMode() const { return mode; }
Ady Abraham2139f732019-11-13 18:56:40 -080075
Ana Krulec72f0d6e2020-01-06 15:24:47 -080076 // Checks whether the fps of this RefreshRate struct is within a given min and max refresh
Marin Shalamanove8a663d2020-11-24 17:48:00 +010077 // rate passed in. Margin of error is applied to the boundaries for approximation.
78 bool inPolicy(Fps minRefreshRate, Fps maxRefreshRate) const {
Ady Abraham6b7ad652021-06-23 17:34:57 -070079 return minRefreshRate.lessThanOrEqualWithMargin(getFps()) &&
80 getFps().lessThanOrEqualWithMargin(maxRefreshRate);
Ana Krulec72f0d6e2020-01-06 15:24:47 -080081 }
82
Ady Abraham6b7ad652021-06-23 17:34:57 -070083 bool operator!=(const RefreshRate& other) const { return mode != other.mode; }
Ady Abraham2139f732019-11-13 18:56:40 -080084
Marin Shalamanove8a663d2020-11-24 17:48:00 +010085 bool operator<(const RefreshRate& other) const {
86 return getFps().getValue() < other.getFps().getValue();
87 }
Ana Krulecb9afd792020-06-11 13:16:15 -070088
Ady Abraham2139f732019-11-13 18:56:40 -080089 bool operator==(const RefreshRate& other) const { return !(*this != other); }
Ady Abrahamabc27602020-04-08 17:20:29 -070090
Marin Shalamanov46084422020-10-13 12:33:42 +020091 std::string toString() const;
Marin Shalamanov65ce5512021-01-22 20:57:13 +010092 friend std::ostream& operator<<(std::ostream& os, const RefreshRate& refreshRate) {
93 return os << refreshRate.toString();
94 }
Marin Shalamanov46084422020-10-13 12:33:42 +020095
Ady Abrahamabc27602020-04-08 17:20:29 -070096 private:
97 friend RefreshRateConfigs;
Ady Abrahamb1b9d412020-06-01 19:53:52 -070098 friend class RefreshRateConfigsTest;
Ady Abrahamabc27602020-04-08 17:20:29 -070099
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100100 DisplayModePtr mode;
Ana Krulecb43429d2019-01-09 14:28:51 -0800101 };
102
Ady Abraham2e1dd892020-03-05 13:48:36 -0800103 using AllRefreshRatesMapType =
Marin Shalamanov23c44202020-12-22 19:09:20 +0100104 std::unordered_map<DisplayModeId, std::unique_ptr<const RefreshRate>>;
Ady Abraham2139f732019-11-13 18:56:40 -0800105
Marin Shalamanoveadf2e72020-12-10 15:35:28 +0100106 struct FpsRange {
107 Fps min{0.0f};
108 Fps max{std::numeric_limits<float>::max()};
109
110 bool operator==(const FpsRange& other) const {
111 return min.equalsWithMargin(other.min) && max.equalsWithMargin(other.max);
112 }
113
114 bool operator!=(const FpsRange& other) const { return !(*this == other); }
115
116 std::string toString() const {
117 return base::StringPrintf("[%s %s]", to_string(min).c_str(), to_string(max).c_str());
118 }
119 };
120
Steven Thomasd4071902020-03-24 16:02:53 -0700121 struct Policy {
Marin Shalamanov30b0b3c2020-10-13 19:15:06 +0200122 private:
123 static constexpr int kAllowGroupSwitchingDefault = false;
124
125 public:
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100126 // The default mode, used to ensure we only initiate display mode switches within the
127 // same mode group as defaultMode's group.
128 DisplayModeId defaultMode;
129 // Whether or not we switch mode groups to get the best frame rate.
Marin Shalamanov30b0b3c2020-10-13 19:15:06 +0200130 bool allowGroupSwitching = kAllowGroupSwitchingDefault;
Steven Thomasf734df42020-04-13 21:09:28 -0700131 // The primary refresh rate range represents display manager's general guidance on the
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100132 // display modes we'll consider when switching refresh rates. Unless we get an explicit
Steven Thomasf734df42020-04-13 21:09:28 -0700133 // signal from an app, we should stay within this range.
Marin Shalamanoveadf2e72020-12-10 15:35:28 +0100134 FpsRange primaryRange;
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100135 // The app request refresh rate range allows us to consider more display modes when
Steven Thomasf734df42020-04-13 21:09:28 -0700136 // switching refresh rates. Although we should generally stay within the primary range,
137 // specific considerations, such as layer frame rate settings specified via the
138 // setFrameRate() api, may cause us to go outside the primary range. We never go outside the
139 // app request range. The app request range will be greater than or equal to the primary
140 // refresh rate range, never smaller.
Marin Shalamanoveadf2e72020-12-10 15:35:28 +0100141 FpsRange appRequestRange;
Steven Thomasd4071902020-03-24 16:02:53 -0700142
Steven Thomasf734df42020-04-13 21:09:28 -0700143 Policy() = default;
Marin Shalamanov30b0b3c2020-10-13 19:15:06 +0200144
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100145 Policy(DisplayModeId defaultMode, const FpsRange& range)
146 : Policy(defaultMode, kAllowGroupSwitchingDefault, range, range) {}
Marin Shalamanov30b0b3c2020-10-13 19:15:06 +0200147
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100148 Policy(DisplayModeId defaultMode, bool allowGroupSwitching, const FpsRange& range)
149 : Policy(defaultMode, allowGroupSwitching, range, range) {}
Marin Shalamanov30b0b3c2020-10-13 19:15:06 +0200150
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100151 Policy(DisplayModeId defaultMode, const FpsRange& primaryRange,
Marin Shalamanoveadf2e72020-12-10 15:35:28 +0100152 const FpsRange& appRequestRange)
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100153 : Policy(defaultMode, kAllowGroupSwitchingDefault, primaryRange, appRequestRange) {}
Marin Shalamanov30b0b3c2020-10-13 19:15:06 +0200154
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100155 Policy(DisplayModeId defaultMode, bool allowGroupSwitching, const FpsRange& primaryRange,
Marin Shalamanov23c44202020-12-22 19:09:20 +0100156 const FpsRange& appRequestRange)
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100157 : defaultMode(defaultMode),
Marin Shalamanov30b0b3c2020-10-13 19:15:06 +0200158 allowGroupSwitching(allowGroupSwitching),
Steven Thomasf734df42020-04-13 21:09:28 -0700159 primaryRange(primaryRange),
160 appRequestRange(appRequestRange) {}
161
Steven Thomasd4071902020-03-24 16:02:53 -0700162 bool operator==(const Policy& other) const {
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100163 return defaultMode == other.defaultMode && primaryRange == other.primaryRange &&
Steven Thomasf734df42020-04-13 21:09:28 -0700164 appRequestRange == other.appRequestRange &&
Steven Thomasd4071902020-03-24 16:02:53 -0700165 allowGroupSwitching == other.allowGroupSwitching;
166 }
167
168 bool operator!=(const Policy& other) const { return !(*this == other); }
Marin Shalamanovb6674e72020-11-06 13:05:57 +0100169 std::string toString() const;
Steven Thomasd4071902020-03-24 16:02:53 -0700170 };
171
172 // Return code set*Policy() to indicate the current policy is unchanged.
173 static constexpr int CURRENT_POLICY_UNCHANGED = 1;
174
175 // We maintain the display manager policy and the override policy separately. The override
176 // policy is used by CTS tests to get a consistent device state for testing. While the override
177 // policy is set, it takes precedence over the display manager policy. Once the override policy
178 // is cleared, we revert to using the display manager policy.
179
180 // Sets the display manager policy to choose refresh rates. The return value will be:
181 // - A negative value if the policy is invalid or another error occurred.
182 // - NO_ERROR if the policy was successfully updated, and the current policy is different from
183 // what it was before the call.
184 // - CURRENT_POLICY_UNCHANGED if the policy was successfully updated, but the current policy
185 // is the same as it was before the call.
186 status_t setDisplayManagerPolicy(const Policy& policy) EXCLUDES(mLock);
187 // Sets the override policy. See setDisplayManagerPolicy() for the meaning of the return value.
188 status_t setOverridePolicy(const std::optional<Policy>& policy) EXCLUDES(mLock);
189 // Gets the current policy, which will be the override policy if active, and the display manager
190 // policy otherwise.
191 Policy getCurrentPolicy() const EXCLUDES(mLock);
192 // Gets the display manager policy, regardless of whether an override policy is active.
193 Policy getDisplayManagerPolicy() const EXCLUDES(mLock);
Ana Kruleced3a8cc2019-11-14 00:55:07 +0100194
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100195 // Returns true if mode is allowed by the current policy.
196 bool isModeAllowed(DisplayModeId) const EXCLUDES(mLock);
Ady Abraham2139f732019-11-13 18:56:40 -0800197
Ady Abraham8a82ba62020-01-17 12:43:17 -0800198 // Describes the different options the layer voted for refresh rate
199 enum class LayerVoteType {
Ady Abraham71c437d2020-01-31 15:56:57 -0800200 NoVote, // Doesn't care about the refresh rate
201 Min, // Minimal refresh rate available
202 Max, // Maximal refresh rate available
203 Heuristic, // Specific refresh rate that was calculated by platform using a heuristic
204 ExplicitDefault, // Specific refresh rate that was provided by the app with Default
205 // compatibility
Ady Abrahamdd5bfa92021-01-07 17:56:08 -0800206 ExplicitExactOrMultiple, // Specific refresh rate that was provided by the app with
207 // ExactOrMultiple compatibility
208 ExplicitExact, // Specific refresh rate that was provided by the app with
209 // Exact compatibility
210
Ady Abraham8a82ba62020-01-17 12:43:17 -0800211 };
212
213 // Captures the layer requirements for a refresh rate. This will be used to determine the
214 // display refresh rate.
215 struct LayerRequirement {
Ady Abrahamaae5ed52020-06-26 09:32:43 -0700216 // Layer's name. Used for debugging purposes.
217 std::string name;
Ady Abraham62a0be22020-12-08 16:54:10 -0800218 // Layer's owner uid
219 uid_t ownerUid = static_cast<uid_t>(-1);
Ady Abrahamaae5ed52020-06-26 09:32:43 -0700220 // Layer vote type.
221 LayerVoteType vote = LayerVoteType::NoVote;
222 // Layer's desired refresh rate, if applicable.
Marin Shalamanove8a663d2020-11-24 17:48:00 +0100223 Fps desiredRefreshRate{0.0f};
Marin Shalamanov46084422020-10-13 12:33:42 +0200224 // If a seamless mode switch is required.
Marin Shalamanov53fc11d2020-11-20 14:00:13 +0100225 Seamlessness seamlessness = Seamlessness::Default;
Ady Abrahamaae5ed52020-06-26 09:32:43 -0700226 // Layer's weight in the range of [0, 1]. The higher the weight the more impact this layer
227 // would have on choosing the refresh rate.
228 float weight = 0.0f;
229 // Whether layer is in focus or not based on WindowManager's state
230 bool focused = false;
Ady Abraham8a82ba62020-01-17 12:43:17 -0800231
232 bool operator==(const LayerRequirement& other) const {
233 return name == other.name && vote == other.vote &&
Marin Shalamanove8a663d2020-11-24 17:48:00 +0100234 desiredRefreshRate.equalsWithMargin(other.desiredRefreshRate) &&
Marin Shalamanovbe97cfa2020-12-01 16:02:07 +0100235 seamlessness == other.seamlessness && weight == other.weight &&
Ady Abrahamaae5ed52020-06-26 09:32:43 -0700236 focused == other.focused;
Ady Abraham8a82ba62020-01-17 12:43:17 -0800237 }
238
239 bool operator!=(const LayerRequirement& other) const { return !(*this == other); }
240 };
241
Ady Abrahamdfd62162020-06-10 16:11:56 -0700242 // Global state describing signals that affect refresh rate choice.
243 struct GlobalSignals {
244 // Whether the user touched the screen recently. Used to apply touch boost.
245 bool touch = false;
246 // True if the system hasn't seen any buffers posted to layers recently.
247 bool idle = false;
Marin Shalamanov4c7831e2021-06-08 20:44:06 +0200248
249 bool operator==(const GlobalSignals& other) const {
250 return touch == other.touch && idle == other.idle;
251 }
Ady Abrahamdfd62162020-06-10 16:11:56 -0700252 };
253
Steven Thomasbb374322020-04-28 22:47:16 -0700254 // Returns the refresh rate that fits best to the given layers.
255 // layers - The layer requirements to consider.
Ady Abrahamdfd62162020-06-10 16:11:56 -0700256 // globalSignals - global state of touch and idle
257 // outSignalsConsidered - An output param that tells the caller whether the refresh rate was
258 // chosen based on touch boost and/or idle timer.
Marin Shalamanoveadf2e72020-12-10 15:35:28 +0100259 RefreshRate getBestRefreshRate(const std::vector<LayerRequirement>& layers,
260 const GlobalSignals& globalSignals,
261 GlobalSignals* outSignalsConsidered = nullptr) const
Ady Abraham6fb599b2020-03-05 13:48:22 -0800262 EXCLUDES(mLock);
Ana Krulecb43429d2019-01-09 14:28:51 -0800263
Marin Shalamanoveadf2e72020-12-10 15:35:28 +0100264 FpsRange getSupportedRefreshRateRange() const EXCLUDES(mLock) {
265 std::lock_guard lock(mLock);
266 return {mMinSupportedRefreshRate->getFps(), mMaxSupportedRefreshRate->getFps()};
267 }
Steven Thomas2bbaabe2019-08-28 16:08:35 -0700268
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100269 std::optional<Fps> onKernelTimerChanged(std::optional<DisplayModeId> desiredActiveModeId,
Marin Shalamanoveadf2e72020-12-10 15:35:28 +0100270 bool timerExpired) const EXCLUDES(mLock);
Steven Thomas2bbaabe2019-08-28 16:08:35 -0700271
Steven Thomasf734df42020-04-13 21:09:28 -0700272 // Returns the highest refresh rate according to the current policy. May change at runtime. Only
273 // uses the primary range, not the app request range.
Marin Shalamanoveadf2e72020-12-10 15:35:28 +0100274 RefreshRate getMaxRefreshRateByPolicy() const EXCLUDES(mLock);
Ady Abraham2139f732019-11-13 18:56:40 -0800275
276 // Returns the current refresh rate
Marin Shalamanoveadf2e72020-12-10 15:35:28 +0100277 RefreshRate getCurrentRefreshRate() const EXCLUDES(mLock);
Ady Abraham2139f732019-11-13 18:56:40 -0800278
Ana Krulec5d477912020-02-07 12:02:38 -0800279 // Returns the current refresh rate, if allowed. Otherwise the default that is allowed by
280 // the policy.
Marin Shalamanoveadf2e72020-12-10 15:35:28 +0100281 RefreshRate getCurrentRefreshRateByPolicy() const;
Ana Krulec5d477912020-02-07 12:02:38 -0800282
Marin Shalamanov23c44202020-12-22 19:09:20 +0100283 // Returns the refresh rate that corresponds to a DisplayModeId. This may change at
Ady Abraham2139f732019-11-13 18:56:40 -0800284 // runtime.
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100285 // TODO(b/159590486) An invalid mode id may be given here if the dipslay modes have changed.
286 RefreshRate getRefreshRateFromModeId(DisplayModeId modeId) const EXCLUDES(mLock) {
Marin Shalamanoveadf2e72020-12-10 15:35:28 +0100287 std::lock_guard lock(mLock);
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100288 return *mRefreshRates.at(modeId);
Ady Abraham2139f732019-11-13 18:56:40 -0800289 };
290
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100291 // Stores the current modeId the device operates at
292 void setCurrentModeId(DisplayModeId) EXCLUDES(mLock);
Dominik Laskowski22488f62019-03-28 09:53:04 -0700293
Ady Abrahama6b676e2020-05-27 14:29:09 -0700294 // Returns a string that represents the layer vote type
295 static std::string layerVoteTypeString(LayerVoteType vote);
296
Ady Abrahamb1b9d412020-06-01 19:53:52 -0700297 // Returns a known frame rate that is the closest to frameRate
Marin Shalamanove8a663d2020-11-24 17:48:00 +0100298 Fps findClosestKnownFrameRate(Fps frameRate) const;
Ady Abrahamb1b9d412020-06-01 19:53:52 -0700299
rnlee3bd610662021-06-23 16:27:57 -0700300 // Configuration flags.
301 struct Config {
302 bool enableFrameRateOverride = false;
303
304 // Specifies the upper refresh rate threshold (inclusive) for layer vote types of multiple
305 // or heuristic, such that refresh rates higher than this value will not be voted for. 0 if
306 // no threshold is set.
307 int frameRateMultipleThreshold = 0;
308 };
309
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100310 RefreshRateConfigs(const DisplayModes& modes, DisplayModeId currentModeId,
rnlee3bd610662021-06-23 16:27:57 -0700311 Config config = {.enableFrameRateOverride = false,
312 .frameRateMultipleThreshold = 0});
Ana Krulec4593b692019-01-11 22:07:25 -0800313
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100314 // Returns whether switching modes (refresh rate or resolution) is possible.
315 // TODO(b/158780872): Consider HAL support, and skip frame rate detection if the modes only
Dominik Laskowski983f2b52020-06-25 16:54:06 -0700316 // differ in resolution.
Marin Shalamanoveadf2e72020-12-10 15:35:28 +0100317 bool canSwitch() const EXCLUDES(mLock) {
318 std::lock_guard lock(mLock);
319 return mRefreshRates.size() > 1;
320 }
Dominik Laskowski983f2b52020-06-25 16:54:06 -0700321
TreeHugger Robot758ab612021-06-22 19:17:29 +0000322 // Class to enumerate options around toggling the kernel timer on and off.
Ana Krulecb9afd792020-06-11 13:16:15 -0700323 enum class KernelIdleTimerAction {
Ana Krulecb9afd792020-06-11 13:16:15 -0700324 TurnOff, // Turn off the idle timer.
325 TurnOn // Turn on the idle timer.
326 };
327 // Checks whether kernel idle timer should be active depending the policy decisions around
328 // refresh rates.
329 KernelIdleTimerAction getIdleTimerAction() const;
330
Ady Abraham64c2fc02020-12-29 12:07:50 -0800331 bool supportsFrameRateOverride() const { return mSupportsFrameRateOverride; }
332
Ady Abraham5cc2e262021-03-25 13:09:17 -0700333 // Return the display refresh rate divider to match the layer
334 // frame rate, or 0 if the display refresh rate is not a multiple of the
335 // layer refresh rate.
336 static int getFrameRateDivider(Fps displayFrameRate, Fps layerFrameRate);
Ady Abraham62a0be22020-12-08 16:54:10 -0800337
Ady Abraham62a0be22020-12-08 16:54:10 -0800338 using UidToFrameRateOverride = std::map<uid_t, Fps>;
Ady Abrahamdd5bfa92021-01-07 17:56:08 -0800339 // Returns the frame rate override for each uid.
340 //
341 // @param layers list of visible layers
342 // @param displayFrameRate the display frame rate
343 // @param touch whether touch timer is active (i.e. user touched the screen recently)
Ady Abraham62a0be22020-12-08 16:54:10 -0800344 UidToFrameRateOverride getFrameRateOverrides(const std::vector<LayerRequirement>& layers,
Ady Abrahamdd5bfa92021-01-07 17:56:08 -0800345 Fps displayFrameRate, bool touch) const
346 EXCLUDES(mLock);
Ady Abraham0bb6a472020-10-12 10:22:13 -0700347
Marin Shalamanovba421a82020-11-10 21:49:26 +0100348 void dump(std::string& result) const EXCLUDES(mLock);
349
Ady Abraham3efa3942021-06-24 19:01:25 -0700350 RefreshRateConfigs(const RefreshRateConfigs&) = delete;
351 void operator=(const RefreshRateConfigs&) = delete;
352
Dominik Laskowski22488f62019-03-28 09:53:04 -0700353private:
Ady Abrahamb1b9d412020-06-01 19:53:52 -0700354 friend class RefreshRateConfigsTest;
355
Ady Abraham2139f732019-11-13 18:56:40 -0800356 void constructAvailableRefreshRates() REQUIRES(mLock);
357
Marin Shalamanoveadf2e72020-12-10 15:35:28 +0100358 void getSortedRefreshRateListLocked(
Ady Abraham2139f732019-11-13 18:56:40 -0800359 const std::function<bool(const RefreshRate&)>& shouldAddRefreshRate,
Marin Shalamanoveadf2e72020-12-10 15:35:28 +0100360 std::vector<const RefreshRate*>* outRefreshRates) REQUIRES(mLock);
Ady Abraham2139f732019-11-13 18:56:40 -0800361
Marin Shalamanov4c7831e2021-06-08 20:44:06 +0200362 std::optional<RefreshRate> getCachedBestRefreshRate(const std::vector<LayerRequirement>& layers,
363 const GlobalSignals& globalSignals,
364 GlobalSignals* outSignalsConsidered) const
365 REQUIRES(mLock);
366
367 RefreshRate getBestRefreshRateLocked(const std::vector<LayerRequirement>& layers,
368 const GlobalSignals& globalSignals,
369 GlobalSignals* outSignalsConsidered) const REQUIRES(mLock);
370
Ady Abraham34702102020-02-10 14:12:05 -0800371 // Returns the refresh rate with the highest score in the collection specified from begin
372 // to end. If there are more than one with the same highest refresh rate, the first one is
373 // returned.
374 template <typename Iter>
375 const RefreshRate* getBestRefreshRate(Iter begin, Iter end) const;
376
Ady Abraham4ccdcb42020-02-11 17:34:34 -0800377 // Returns number of display frames and remainder when dividing the layer refresh period by
378 // display refresh period.
379 std::pair<nsecs_t, nsecs_t> getDisplayFrames(nsecs_t layerPeriod, nsecs_t displayPeriod) const;
380
Steven Thomasf734df42020-04-13 21:09:28 -0700381 // Returns the lowest refresh rate according to the current policy. May change at runtime. Only
382 // uses the primary range, not the app request range.
383 const RefreshRate& getMinRefreshRateByPolicyLocked() const REQUIRES(mLock);
384
385 // Returns the highest 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& getMaxRefreshRateByPolicyLocked() const REQUIRES(mLock);
388
Ana Krulec3d367c82020-02-25 15:02:01 -0800389 // Returns the current refresh rate, if allowed. Otherwise the default that is allowed by
390 // the policy.
391 const RefreshRate& getCurrentRefreshRateByPolicyLocked() const REQUIRES(mLock);
392
Steven Thomasd4071902020-03-24 16:02:53 -0700393 const Policy* getCurrentPolicyLocked() const REQUIRES(mLock);
Marin Shalamanoveadf2e72020-12-10 15:35:28 +0100394 bool isPolicyValidLocked(const Policy& policy) const REQUIRES(mLock);
Steven Thomasd4071902020-03-24 16:02:53 -0700395
rnlee3bd610662021-06-23 16:27:57 -0700396 // Returns whether the layer is allowed to vote for the given refresh rate.
397 bool isVoteAllowed(const LayerRequirement&, const RefreshRate&) const;
398
Ady Abraham62a0be22020-12-08 16:54:10 -0800399 // calculates a score for a layer. Used to determine the display refresh rate
400 // and the frame rate override for certains applications.
401 float calculateLayerScoreLocked(const LayerRequirement&, const RefreshRate&,
402 bool isSeamlessSwitch) const REQUIRES(mLock);
403
Ady Abraham3efa3942021-06-24 19:01:25 -0700404 void updateDisplayModes(const DisplayModes& mode, DisplayModeId currentModeId) EXCLUDES(mLock);
405
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100406 // The list of refresh rates, indexed by display modes ID. This may change after this
Steven Thomas2bbaabe2019-08-28 16:08:35 -0700407 // object is initialized.
Marin Shalamanoveadf2e72020-12-10 15:35:28 +0100408 AllRefreshRatesMapType mRefreshRates GUARDED_BY(mLock);
Ady Abraham2139f732019-11-13 18:56:40 -0800409
Steven Thomasf734df42020-04-13 21:09:28 -0700410 // The list of refresh rates in the primary range of the current policy, ordered by vsyncPeriod
411 // (the first element is the lowest refresh rate).
412 std::vector<const RefreshRate*> mPrimaryRefreshRates GUARDED_BY(mLock);
413
414 // The list of refresh rates in the app request range of the current policy, ordered by
415 // vsyncPeriod (the first element is the lowest refresh rate).
416 std::vector<const RefreshRate*> mAppRequestRefreshRates GUARDED_BY(mLock);
Ady Abraham2139f732019-11-13 18:56:40 -0800417
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100418 // The current display mode. This will change at runtime. This is set by SurfaceFlinger on
Ady Abraham2139f732019-11-13 18:56:40 -0800419 // the main thread, and read by the Scheduler (and other objects) on other threads.
420 const RefreshRate* mCurrentRefreshRate GUARDED_BY(mLock);
421
Steven Thomasd4071902020-03-24 16:02:53 -0700422 // The policy values will change at runtime. They're set by SurfaceFlinger on the main thread,
423 // and read by the Scheduler (and other objects) on other threads.
424 Policy mDisplayManagerPolicy GUARDED_BY(mLock);
425 std::optional<Policy> mOverridePolicy GUARDED_BY(mLock);
Ady Abraham2139f732019-11-13 18:56:40 -0800426
427 // The min and max refresh rates supported by the device.
Marin Shalamanoveadf2e72020-12-10 15:35:28 +0100428 // This may change at runtime.
429 const RefreshRate* mMinSupportedRefreshRate GUARDED_BY(mLock);
430 const RefreshRate* mMaxSupportedRefreshRate GUARDED_BY(mLock);
Ady Abraham2139f732019-11-13 18:56:40 -0800431
Ady Abraham2139f732019-11-13 18:56:40 -0800432 mutable std::mutex mLock;
Ady Abrahamb1b9d412020-06-01 19:53:52 -0700433
434 // A sorted list of known frame rates that a Heuristic layer will choose
435 // from based on the closest value.
Marin Shalamanove8a663d2020-11-24 17:48:00 +0100436 const std::vector<Fps> mKnownFrameRates;
Ady Abraham64c2fc02020-12-29 12:07:50 -0800437
rnlee3bd610662021-06-23 16:27:57 -0700438 const Config mConfig;
Ady Abraham64c2fc02020-12-29 12:07:50 -0800439 bool mSupportsFrameRateOverride;
Marin Shalamanov4c7831e2021-06-08 20:44:06 +0200440
441 struct GetBestRefreshRateInvocation {
442 std::vector<LayerRequirement> layerRequirements;
443 GlobalSignals globalSignals;
444 GlobalSignals outSignalsConsidered;
445 RefreshRate resultingBestRefreshRate;
446 };
447 mutable std::optional<GetBestRefreshRateInvocation> lastBestRefreshRateInvocation
448 GUARDED_BY(mLock);
Ana Krulecb43429d2019-01-09 14:28:51 -0800449};
450
Dominik Laskowski98041832019-08-01 18:35:59 -0700451} // namespace android::scheduler