blob: 2c2e34ac13cd8a752f44dd74dcfc5b74df719c92 [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
19#include <algorithm>
20#include <numeric>
Steven Thomasd4071902020-03-24 16:02:53 -070021#include <optional>
Dominik Laskowski98041832019-08-01 18:35:59 -070022#include <type_traits>
Dominik Laskowskia8626ec2021-12-15 18:13:30 -080023#include <utility>
Dominik Laskowski36dced82022-09-02 09:24:00 -070024#include <variant>
Ana Krulecb43429d2019-01-09 14:28:51 -080025
Dominik Laskowskif6b4ba62021-11-09 12:46:10 -080026#include <gui/DisplayEventReceiver.h>
27
28#include <scheduler/Fps.h>
29#include <scheduler/Seamlessness.h>
30
Marin Shalamanov3ea1d602020-12-16 19:59:39 +010031#include "DisplayHardware/DisplayMode.h"
Ana Krulec4593b692019-01-11 22:07:25 -080032#include "DisplayHardware/HWComposer.h"
Ady Abraham9a2ea342021-09-03 17:32:34 -070033#include "Scheduler/OneShotTimer.h"
Ady Abraham2139f732019-11-13 18:56:40 -080034#include "Scheduler/StrongTyping.h"
Dominik Laskowskif8734e02022-08-26 09:06:59 -070035#include "ThreadContext.h"
Dominik Laskowskie70461a2022-08-30 14:42:01 -070036#include "Utils/Dumper.h"
Ana Krulec4593b692019-01-11 22:07:25 -080037
Dominik Laskowski98041832019-08-01 18:35:59 -070038namespace android::scheduler {
Ady Abrahamabc27602020-04-08 17:20:29 -070039
Ady Abraham4ccdcb42020-02-11 17:34:34 -080040using namespace std::chrono_literals;
Dominik Laskowski98041832019-08-01 18:35:59 -070041
Dominik Laskowski068173d2021-08-11 17:22:59 -070042enum class DisplayModeEvent : unsigned { None = 0b0, Changed = 0b1 };
Dominik Laskowski98041832019-08-01 18:35:59 -070043
Dominik Laskowski068173d2021-08-11 17:22:59 -070044inline DisplayModeEvent operator|(DisplayModeEvent lhs, DisplayModeEvent rhs) {
45 using T = std::underlying_type_t<DisplayModeEvent>;
46 return static_cast<DisplayModeEvent>(static_cast<T>(lhs) | static_cast<T>(rhs));
Dominik Laskowski98041832019-08-01 18:35:59 -070047}
Ana Krulecb43429d2019-01-09 14:28:51 -080048
ramindanid72ba162022-09-09 21:33:40 +000049struct RefreshRateRanking {
50 DisplayModePtr displayModePtr;
51 float score = 0.0f;
52
53 bool operator==(const RefreshRateRanking& ranking) const {
54 return displayModePtr == ranking.displayModePtr && score == ranking.score;
55 }
56};
57
Ady Abraham62f216c2020-10-13 19:07:23 -070058using FrameRateOverride = DisplayEventReceiver::Event::FrameRateOverride;
59
Ana Krulecb43429d2019-01-09 14:28:51 -080060/**
Ady Abraham1902d072019-03-01 17:18:59 -080061 * This class is used to encapsulate configuration for refresh rates. It holds information
Ana Krulecb43429d2019-01-09 14:28:51 -080062 * about available refresh rates on the device, and the mapping between the numbers and human
63 * readable names.
64 */
65class RefreshRateConfigs {
66public:
Ady Abraham4ccdcb42020-02-11 17:34:34 -080067 // Margin used when matching refresh rates to the content desired ones.
68 static constexpr nsecs_t MARGIN_FOR_PERIOD_CALCULATION =
rnlee3bd610662021-06-23 16:27:57 -070069 std::chrono::nanoseconds(800us).count();
Ady Abraham4ccdcb42020-02-11 17:34:34 -080070
Dominik Laskowski36dced82022-09-02 09:24:00 -070071 class Policy {
Marin Shalamanov30b0b3c2020-10-13 19:15:06 +020072 static constexpr int kAllowGroupSwitchingDefault = false;
73
74 public:
Marin Shalamanova7fe3042021-01-29 21:02:08 +010075 // The default mode, used to ensure we only initiate display mode switches within the
76 // same mode group as defaultMode's group.
77 DisplayModeId defaultMode;
78 // Whether or not we switch mode groups to get the best frame rate.
Marin Shalamanov30b0b3c2020-10-13 19:15:06 +020079 bool allowGroupSwitching = kAllowGroupSwitchingDefault;
Steven Thomasf734df42020-04-13 21:09:28 -070080 // The primary refresh rate range represents display manager's general guidance on the
Marin Shalamanova7fe3042021-01-29 21:02:08 +010081 // display modes we'll consider when switching refresh rates. Unless we get an explicit
Steven Thomasf734df42020-04-13 21:09:28 -070082 // signal from an app, we should stay within this range.
Marin Shalamanoveadf2e72020-12-10 15:35:28 +010083 FpsRange primaryRange;
Marin Shalamanova7fe3042021-01-29 21:02:08 +010084 // The app request refresh rate range allows us to consider more display modes when
Steven Thomasf734df42020-04-13 21:09:28 -070085 // switching refresh rates. Although we should generally stay within the primary range,
86 // specific considerations, such as layer frame rate settings specified via the
87 // setFrameRate() api, may cause us to go outside the primary range. We never go outside the
88 // app request range. The app request range will be greater than or equal to the primary
89 // refresh rate range, never smaller.
Marin Shalamanoveadf2e72020-12-10 15:35:28 +010090 FpsRange appRequestRange;
Steven Thomasd4071902020-03-24 16:02:53 -070091
Steven Thomasf734df42020-04-13 21:09:28 -070092 Policy() = default;
Marin Shalamanov30b0b3c2020-10-13 19:15:06 +020093
Dominik Laskowski953b7fd2022-01-08 19:34:59 -080094 Policy(DisplayModeId defaultMode, FpsRange range)
Marin Shalamanova7fe3042021-01-29 21:02:08 +010095 : Policy(defaultMode, kAllowGroupSwitchingDefault, range, range) {}
Marin Shalamanov30b0b3c2020-10-13 19:15:06 +020096
Dominik Laskowski953b7fd2022-01-08 19:34:59 -080097 Policy(DisplayModeId defaultMode, bool allowGroupSwitching, FpsRange range)
Marin Shalamanova7fe3042021-01-29 21:02:08 +010098 : Policy(defaultMode, allowGroupSwitching, range, range) {}
Marin Shalamanov30b0b3c2020-10-13 19:15:06 +020099
Dominik Laskowski953b7fd2022-01-08 19:34:59 -0800100 Policy(DisplayModeId defaultMode, FpsRange primaryRange, FpsRange appRequestRange)
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100101 : Policy(defaultMode, kAllowGroupSwitchingDefault, primaryRange, appRequestRange) {}
Marin Shalamanov30b0b3c2020-10-13 19:15:06 +0200102
Dominik Laskowski953b7fd2022-01-08 19:34:59 -0800103 Policy(DisplayModeId defaultMode, bool allowGroupSwitching, FpsRange primaryRange,
104 FpsRange appRequestRange)
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100105 : defaultMode(defaultMode),
Marin Shalamanov30b0b3c2020-10-13 19:15:06 +0200106 allowGroupSwitching(allowGroupSwitching),
Steven Thomasf734df42020-04-13 21:09:28 -0700107 primaryRange(primaryRange),
108 appRequestRange(appRequestRange) {}
109
Steven Thomasd4071902020-03-24 16:02:53 -0700110 bool operator==(const Policy& other) const {
Dominik Laskowski953b7fd2022-01-08 19:34:59 -0800111 using namespace fps_approx_ops;
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100112 return defaultMode == other.defaultMode && primaryRange == other.primaryRange &&
Steven Thomasf734df42020-04-13 21:09:28 -0700113 appRequestRange == other.appRequestRange &&
Steven Thomasd4071902020-03-24 16:02:53 -0700114 allowGroupSwitching == other.allowGroupSwitching;
115 }
116
117 bool operator!=(const Policy& other) const { return !(*this == other); }
Marin Shalamanovb6674e72020-11-06 13:05:57 +0100118 std::string toString() const;
Steven Thomasd4071902020-03-24 16:02:53 -0700119 };
120
Dominik Laskowski36dced82022-09-02 09:24:00 -0700121 enum class SetPolicyResult { Invalid, Unchanged, Changed };
Steven Thomasd4071902020-03-24 16:02:53 -0700122
123 // We maintain the display manager policy and the override policy separately. The override
124 // policy is used by CTS tests to get a consistent device state for testing. While the override
125 // policy is set, it takes precedence over the display manager policy. Once the override policy
126 // is cleared, we revert to using the display manager policy.
Dominik Laskowski36dced82022-09-02 09:24:00 -0700127 struct DisplayManagerPolicy : Policy {
128 using Policy::Policy;
129 };
Steven Thomasd4071902020-03-24 16:02:53 -0700130
Dominik Laskowski36dced82022-09-02 09:24:00 -0700131 struct OverridePolicy : Policy {
132 using Policy::Policy;
133 };
134
135 struct NoOverridePolicy {};
136
137 using PolicyVariant = std::variant<DisplayManagerPolicy, OverridePolicy, NoOverridePolicy>;
138
139 SetPolicyResult setPolicy(const PolicyVariant&) EXCLUDES(mLock) REQUIRES(kMainThreadContext);
140
141 void onModeChangeInitiated() REQUIRES(kMainThreadContext) { mNumModeSwitchesInPolicy++; }
142
Steven Thomasd4071902020-03-24 16:02:53 -0700143 // Gets the current policy, which will be the override policy if active, and the display manager
144 // policy otherwise.
145 Policy getCurrentPolicy() const EXCLUDES(mLock);
146 // Gets the display manager policy, regardless of whether an override policy is active.
147 Policy getDisplayManagerPolicy() const EXCLUDES(mLock);
Ana Kruleced3a8cc2019-11-14 00:55:07 +0100148
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100149 // Returns true if mode is allowed by the current policy.
150 bool isModeAllowed(DisplayModeId) const EXCLUDES(mLock);
Ady Abraham2139f732019-11-13 18:56:40 -0800151
Ady Abraham8a82ba62020-01-17 12:43:17 -0800152 // Describes the different options the layer voted for refresh rate
153 enum class LayerVoteType {
Ady Abraham71c437d2020-01-31 15:56:57 -0800154 NoVote, // Doesn't care about the refresh rate
155 Min, // Minimal refresh rate available
156 Max, // Maximal refresh rate available
157 Heuristic, // Specific refresh rate that was calculated by platform using a heuristic
158 ExplicitDefault, // Specific refresh rate that was provided by the app with Default
159 // compatibility
Ady Abrahamdd5bfa92021-01-07 17:56:08 -0800160 ExplicitExactOrMultiple, // Specific refresh rate that was provided by the app with
161 // ExactOrMultiple compatibility
162 ExplicitExact, // Specific refresh rate that was provided by the app with
163 // Exact compatibility
164
Dominik Laskowskif5d0ea52021-09-26 17:27:01 -0700165 ftl_last = ExplicitExact
Ady Abraham8a82ba62020-01-17 12:43:17 -0800166 };
167
168 // Captures the layer requirements for a refresh rate. This will be used to determine the
169 // display refresh rate.
170 struct LayerRequirement {
Ady Abrahamaae5ed52020-06-26 09:32:43 -0700171 // Layer's name. Used for debugging purposes.
172 std::string name;
Ady Abraham62a0be22020-12-08 16:54:10 -0800173 // Layer's owner uid
174 uid_t ownerUid = static_cast<uid_t>(-1);
Ady Abrahamaae5ed52020-06-26 09:32:43 -0700175 // Layer vote type.
176 LayerVoteType vote = LayerVoteType::NoVote;
177 // Layer's desired refresh rate, if applicable.
Dominik Laskowski6eab42d2021-09-13 14:34:13 -0700178 Fps desiredRefreshRate;
Marin Shalamanov46084422020-10-13 12:33:42 +0200179 // If a seamless mode switch is required.
Marin Shalamanov53fc11d2020-11-20 14:00:13 +0100180 Seamlessness seamlessness = Seamlessness::Default;
Ady Abrahamaae5ed52020-06-26 09:32:43 -0700181 // Layer's weight in the range of [0, 1]. The higher the weight the more impact this layer
182 // would have on choosing the refresh rate.
183 float weight = 0.0f;
184 // Whether layer is in focus or not based on WindowManager's state
185 bool focused = false;
Ady Abraham8a82ba62020-01-17 12:43:17 -0800186
187 bool operator==(const LayerRequirement& other) const {
188 return name == other.name && vote == other.vote &&
Dominik Laskowski6eab42d2021-09-13 14:34:13 -0700189 isApproxEqual(desiredRefreshRate, other.desiredRefreshRate) &&
Marin Shalamanovbe97cfa2020-12-01 16:02:07 +0100190 seamlessness == other.seamlessness && weight == other.weight &&
Ady Abrahamaae5ed52020-06-26 09:32:43 -0700191 focused == other.focused;
Ady Abraham8a82ba62020-01-17 12:43:17 -0800192 }
193
194 bool operator!=(const LayerRequirement& other) const { return !(*this == other); }
195 };
196
Ady Abrahamdfd62162020-06-10 16:11:56 -0700197 // Global state describing signals that affect refresh rate choice.
198 struct GlobalSignals {
199 // Whether the user touched the screen recently. Used to apply touch boost.
200 bool touch = false;
201 // True if the system hasn't seen any buffers posted to layers recently.
202 bool idle = false;
ramindani38c84982022-08-29 18:02:57 +0000203 // Whether the display is about to be powered on, or has been in PowerMode::ON
204 // within the timeout of DisplayPowerTimer.
205 bool powerOnImminent = false;
Marin Shalamanov4c7831e2021-06-08 20:44:06 +0200206
Dominik Laskowski6eab42d2021-09-13 14:34:13 -0700207 bool operator==(GlobalSignals other) const {
ramindani38c84982022-08-29 18:02:57 +0000208 return touch == other.touch && idle == other.idle &&
209 powerOnImminent == other.powerOnImminent;
Marin Shalamanov4c7831e2021-06-08 20:44:06 +0200210 }
Ady Abrahamdfd62162020-06-10 16:11:56 -0700211 };
212
ramindanid72ba162022-09-09 21:33:40 +0000213 // Returns the list in the descending order of refresh rates desired
214 // based on their overall score, and the GlobalSignals that were considered.
215 std::pair<std::vector<RefreshRateRanking>, GlobalSignals> getRankedRefreshRates(
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800216 const std::vector<LayerRequirement>&, GlobalSignals) const EXCLUDES(mLock);
Ana Krulecb43429d2019-01-09 14:28:51 -0800217
Marin Shalamanoveadf2e72020-12-10 15:35:28 +0100218 FpsRange getSupportedRefreshRateRange() const EXCLUDES(mLock) {
219 std::lock_guard lock(mLock);
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800220 return {mMinRefreshRateModeIt->second->getFps(), mMaxRefreshRateModeIt->second->getFps()};
Marin Shalamanoveadf2e72020-12-10 15:35:28 +0100221 }
Steven Thomas2bbaabe2019-08-28 16:08:35 -0700222
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100223 std::optional<Fps> onKernelTimerChanged(std::optional<DisplayModeId> desiredActiveModeId,
Marin Shalamanoveadf2e72020-12-10 15:35:28 +0100224 bool timerExpired) const EXCLUDES(mLock);
Steven Thomas2bbaabe2019-08-28 16:08:35 -0700225
Dominik Laskowskif8734e02022-08-26 09:06:59 -0700226 void setActiveModeId(DisplayModeId) EXCLUDES(mLock) REQUIRES(kMainThreadContext);
227
228 // See mActiveModeIt for thread safety.
229 DisplayModePtr getActiveModePtr() const EXCLUDES(mLock);
230 const DisplayMode& getActiveMode() const REQUIRES(kMainThreadContext);
Dominik Laskowski22488f62019-03-28 09:53:04 -0700231
Ady Abrahamb1b9d412020-06-01 19:53:52 -0700232 // Returns a known frame rate that is the closest to frameRate
Marin Shalamanove8a663d2020-11-24 17:48:00 +0100233 Fps findClosestKnownFrameRate(Fps frameRate) const;
Ady Abrahamb1b9d412020-06-01 19:53:52 -0700234
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800235 enum class KernelIdleTimerController { Sysprop, HwcApi, ftl_last = HwcApi };
ramindani32cf0602022-03-02 02:30:29 +0000236
rnlee3bd610662021-06-23 16:27:57 -0700237 // Configuration flags.
238 struct Config {
239 bool enableFrameRateOverride = false;
240
241 // Specifies the upper refresh rate threshold (inclusive) for layer vote types of multiple
242 // or heuristic, such that refresh rates higher than this value will not be voted for. 0 if
243 // no threshold is set.
244 int frameRateMultipleThreshold = 0;
Ady Abraham9a2ea342021-09-03 17:32:34 -0700245
Ady Abraham6d885932021-09-03 18:05:48 -0700246 // The Idle Timer timeout. 0 timeout means no idle timer.
ramindani32cf0602022-03-02 02:30:29 +0000247 std::chrono::milliseconds idleTimerTimeout = 0ms;
Ady Abraham6d885932021-09-03 18:05:48 -0700248
ramindani32cf0602022-03-02 02:30:29 +0000249 // The controller representing how the kernel idle timer will be configured
250 // either on the HWC api or sysprop.
251 std::optional<KernelIdleTimerController> kernelIdleTimerController;
rnlee3bd610662021-06-23 16:27:57 -0700252 };
253
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800254 RefreshRateConfigs(DisplayModes, DisplayModeId activeModeId,
rnlee3bd610662021-06-23 16:27:57 -0700255 Config config = {.enableFrameRateOverride = false,
Ady Abraham9a2ea342021-09-03 17:32:34 -0700256 .frameRateMultipleThreshold = 0,
ramindani32cf0602022-03-02 02:30:29 +0000257 .idleTimerTimeout = 0ms,
258 .kernelIdleTimerController = {}});
Ana Krulec4593b692019-01-11 22:07:25 -0800259
Dominik Laskowski0c252702021-12-20 20:32:09 -0800260 RefreshRateConfigs(const RefreshRateConfigs&) = delete;
261 RefreshRateConfigs& operator=(const RefreshRateConfigs&) = delete;
262
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100263 // Returns whether switching modes (refresh rate or resolution) is possible.
264 // TODO(b/158780872): Consider HAL support, and skip frame rate detection if the modes only
Dominik Laskowski983f2b52020-06-25 16:54:06 -0700265 // differ in resolution.
Marin Shalamanoveadf2e72020-12-10 15:35:28 +0100266 bool canSwitch() const EXCLUDES(mLock) {
267 std::lock_guard lock(mLock);
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800268 return mDisplayModes.size() > 1;
Marin Shalamanoveadf2e72020-12-10 15:35:28 +0100269 }
Dominik Laskowski983f2b52020-06-25 16:54:06 -0700270
TreeHugger Robot758ab612021-06-22 19:17:29 +0000271 // Class to enumerate options around toggling the kernel timer on and off.
Ana Krulecb9afd792020-06-11 13:16:15 -0700272 enum class KernelIdleTimerAction {
Ana Krulecb9afd792020-06-11 13:16:15 -0700273 TurnOff, // Turn off the idle timer.
274 TurnOn // Turn on the idle timer.
275 };
ramindani32cf0602022-03-02 02:30:29 +0000276
Ana Krulecb9afd792020-06-11 13:16:15 -0700277 // Checks whether kernel idle timer should be active depending the policy decisions around
278 // refresh rates.
279 KernelIdleTimerAction getIdleTimerAction() const;
280
Andy Yu2ae6b6b2021-11-18 14:51:06 -0800281 bool supportsFrameRateOverrideByContent() const { return mSupportsFrameRateOverrideByContent; }
Ady Abraham64c2fc02020-12-29 12:07:50 -0800282
Ady Abrahamcc315492022-02-17 17:06:39 -0800283 // Return the display refresh rate divisor to match the layer
Ady Abraham5cc2e262021-03-25 13:09:17 -0700284 // frame rate, or 0 if the display refresh rate is not a multiple of the
285 // layer refresh rate.
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800286 static int getFrameRateDivisor(Fps displayRefreshRate, Fps layerFrameRate);
Ady Abraham62a0be22020-12-08 16:54:10 -0800287
Marin Shalamanov15a0fc62021-08-16 18:20:21 +0200288 // Returns if the provided frame rates have a ratio t*1000/1001 or t*1001/1000
289 // for an integer t.
290 static bool isFractionalPairOrMultiple(Fps, Fps);
291
Ady Abraham62a0be22020-12-08 16:54:10 -0800292 using UidToFrameRateOverride = std::map<uid_t, Fps>;
Dominik Laskowski6eab42d2021-09-13 14:34:13 -0700293
Ady Abrahamdd5bfa92021-01-07 17:56:08 -0800294 // Returns the frame rate override for each uid.
Dominik Laskowski6eab42d2021-09-13 14:34:13 -0700295 UidToFrameRateOverride getFrameRateOverrides(const std::vector<LayerRequirement>&,
296 Fps displayFrameRate, GlobalSignals) const
Ady Abrahamdd5bfa92021-01-07 17:56:08 -0800297 EXCLUDES(mLock);
Ady Abraham0bb6a472020-10-12 10:22:13 -0700298
ramindani32cf0602022-03-02 02:30:29 +0000299 std::optional<KernelIdleTimerController> kernelIdleTimerController() {
300 return mConfig.kernelIdleTimerController;
301 }
Ady Abraham9a2ea342021-09-03 17:32:34 -0700302
Dominik Laskowski83bd7712022-01-07 14:30:53 -0800303 struct IdleTimerCallbacks {
304 struct Callbacks {
305 std::function<void()> onReset;
306 std::function<void()> onExpired;
307 };
308
309 Callbacks platform;
310 Callbacks kernel;
311 };
312
313 void setIdleTimerCallbacks(IdleTimerCallbacks callbacks) EXCLUDES(mIdleTimerCallbacksMutex) {
Ady Abraham9a2ea342021-09-03 17:32:34 -0700314 std::scoped_lock lock(mIdleTimerCallbacksMutex);
Dominik Laskowski83bd7712022-01-07 14:30:53 -0800315 mIdleTimerCallbacks = std::move(callbacks);
316 }
317
318 void clearIdleTimerCallbacks() EXCLUDES(mIdleTimerCallbacksMutex) {
319 std::scoped_lock lock(mIdleTimerCallbacksMutex);
320 mIdleTimerCallbacks.reset();
Ady Abraham11663402021-11-10 19:46:09 -0800321 }
322
323 void startIdleTimer() {
324 if (mIdleTimer) {
325 mIdleTimer->start();
326 }
327 }
328
329 void stopIdleTimer() {
330 if (mIdleTimer) {
331 mIdleTimer->stop();
332 }
Ady Abraham9a2ea342021-09-03 17:32:34 -0700333 }
334
335 void resetIdleTimer(bool kernelOnly) {
336 if (!mIdleTimer) {
337 return;
338 }
ramindani32cf0602022-03-02 02:30:29 +0000339 if (kernelOnly && !mConfig.kernelIdleTimerController.has_value()) {
Ady Abraham9a2ea342021-09-03 17:32:34 -0700340 return;
341 }
342 mIdleTimer->reset();
Dominik Laskowski83bd7712022-01-07 14:30:53 -0800343 }
Ady Abraham9a2ea342021-09-03 17:32:34 -0700344
Dominik Laskowskie70461a2022-08-30 14:42:01 -0700345 void dump(utils::Dumper&) const EXCLUDES(mLock);
Marin Shalamanovba421a82020-11-10 21:49:26 +0100346
ramindani32cf0602022-03-02 02:30:29 +0000347 std::chrono::milliseconds getIdleTimerTimeout();
348
Dominik Laskowski22488f62019-03-28 09:53:04 -0700349private:
Dominik Laskowski0c252702021-12-20 20:32:09 -0800350 friend struct TestableRefreshRateConfigs;
Ady Abrahamb1b9d412020-06-01 19:53:52 -0700351
Ady Abraham2139f732019-11-13 18:56:40 -0800352 void constructAvailableRefreshRates() REQUIRES(mLock);
353
Dominik Laskowskif8734e02022-08-26 09:06:59 -0700354 // See mActiveModeIt for thread safety.
355 DisplayModeIterator getActiveModeItLocked() const REQUIRES(mLock);
356
ramindanid72ba162022-09-09 21:33:40 +0000357 std::pair<std::vector<RefreshRateRanking>, GlobalSignals> getRankedRefreshRatesLocked(
Dominik Laskowskia8626ec2021-12-15 18:13:30 -0800358 const std::vector<LayerRequirement>&, GlobalSignals) const REQUIRES(mLock);
Marin Shalamanov4c7831e2021-06-08 20:44:06 +0200359
Ady Abraham4ccdcb42020-02-11 17:34:34 -0800360 // Returns number of display frames and remainder when dividing the layer refresh period by
361 // display refresh period.
362 std::pair<nsecs_t, nsecs_t> getDisplayFrames(nsecs_t layerPeriod, nsecs_t displayPeriod) const;
363
Steven Thomasf734df42020-04-13 21:09:28 -0700364 // Returns the lowest refresh rate according to the current policy. May change at runtime. Only
365 // uses the primary range, not the app request range.
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800366 const DisplayModePtr& getMinRefreshRateByPolicyLocked() const REQUIRES(mLock);
Steven Thomasf734df42020-04-13 21:09:28 -0700367
368 // Returns the highest refresh rate according to the current policy. May change at runtime. Only
369 // uses the primary range, not the app request range.
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800370 const DisplayModePtr& getMaxRefreshRateByPolicyLocked(int anchorGroup) const REQUIRES(mLock);
Marin Shalamanov8cd8a992021-09-14 23:22:49 +0200371
ramindanid72ba162022-09-09 21:33:40 +0000372 struct RefreshRateScoreComparator;
373
374 enum class RefreshRateOrder { Ascending, Descending };
375
376 // Returns the rankings in RefreshRateOrder. May change at runtime.
377 // Only uses the primary range, not the app request range.
378 std::vector<RefreshRateRanking> getRefreshRatesByPolicyLocked(std::optional<int> anchorGroupOpt,
379 RefreshRateOrder) const
380 REQUIRES(mLock);
381
Steven Thomasd4071902020-03-24 16:02:53 -0700382 const Policy* getCurrentPolicyLocked() const REQUIRES(mLock);
Marin Shalamanoveadf2e72020-12-10 15:35:28 +0100383 bool isPolicyValidLocked(const Policy& policy) const REQUIRES(mLock);
Steven Thomasd4071902020-03-24 16:02:53 -0700384
ramindanid72ba162022-09-09 21:33:40 +0000385 // Returns the refresh rate score as a ratio to max refresh rate, which has a score of 1.
386 float calculateRefreshRateScoreForFps(Fps refreshRate) const REQUIRES(mLock);
Ady Abraham62a0be22020-12-08 16:54:10 -0800387 // calculates a score for a layer. Used to determine the display refresh rate
388 // and the frame rate override for certains applications.
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800389 float calculateLayerScoreLocked(const LayerRequirement&, Fps refreshRate,
Ady Abraham62a0be22020-12-08 16:54:10 -0800390 bool isSeamlessSwitch) const REQUIRES(mLock);
391
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800392 float calculateNonExactMatchingLayerScoreLocked(const LayerRequirement&, Fps refreshRate) const
393 REQUIRES(mLock);
Ady Abraham05243be2021-09-16 15:58:52 -0700394
Dominik Laskowskif8734e02022-08-26 09:06:59 -0700395 void updateDisplayModes(DisplayModes, DisplayModeId activeModeId) EXCLUDES(mLock)
396 REQUIRES(kMainThreadContext);
Ady Abraham3efa3942021-06-24 19:01:25 -0700397
Ady Abraham9a2ea342021-09-03 17:32:34 -0700398 void initializeIdleTimer();
399
Dominik Laskowski83bd7712022-01-07 14:30:53 -0800400 std::optional<IdleTimerCallbacks::Callbacks> getIdleTimerCallbacks() const
401 REQUIRES(mIdleTimerCallbacksMutex) {
402 if (!mIdleTimerCallbacks) return {};
ramindani32cf0602022-03-02 02:30:29 +0000403 return mConfig.kernelIdleTimerController.has_value() ? mIdleTimerCallbacks->kernel
404 : mIdleTimerCallbacks->platform;
Dominik Laskowski83bd7712022-01-07 14:30:53 -0800405 }
406
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800407 // The display modes of the active display. The DisplayModeIterators below are pointers into
408 // this container, so must be invalidated whenever the DisplayModes change. The Policy below
409 // is also dependent, so must be reset as well.
410 DisplayModes mDisplayModes GUARDED_BY(mLock);
Ady Abraham2139f732019-11-13 18:56:40 -0800411
Dominik Laskowskif8734e02022-08-26 09:06:59 -0700412 // Written under mLock exclusively from kMainThreadContext, so reads from kMainThreadContext
413 // need not be under mLock.
414 DisplayModeIterator mActiveModeIt GUARDED_BY(mLock) GUARDED_BY(kMainThreadContext);
415
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800416 DisplayModeIterator mMinRefreshRateModeIt GUARDED_BY(mLock);
417 DisplayModeIterator mMaxRefreshRateModeIt GUARDED_BY(mLock);
Steven Thomasf734df42020-04-13 21:09:28 -0700418
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800419 // Display modes that satisfy the Policy's ranges, filtered and sorted by refresh rate.
420 std::vector<DisplayModeIterator> mPrimaryRefreshRates GUARDED_BY(mLock);
421 std::vector<DisplayModeIterator> mAppRequestRefreshRates GUARDED_BY(mLock);
Ady Abraham2139f732019-11-13 18:56:40 -0800422
Steven Thomasd4071902020-03-24 16:02:53 -0700423 Policy mDisplayManagerPolicy GUARDED_BY(mLock);
424 std::optional<Policy> mOverridePolicy GUARDED_BY(mLock);
Ady Abraham2139f732019-11-13 18:56:40 -0800425
Dominik Laskowski36dced82022-09-02 09:24:00 -0700426 unsigned mNumModeSwitchesInPolicy GUARDED_BY(kMainThreadContext) = 0;
427
Ady Abraham2139f732019-11-13 18:56:40 -0800428 mutable std::mutex mLock;
Ady Abrahamb1b9d412020-06-01 19:53:52 -0700429
430 // A sorted list of known frame rates that a Heuristic layer will choose
431 // from based on the closest value.
Marin Shalamanove8a663d2020-11-24 17:48:00 +0100432 const std::vector<Fps> mKnownFrameRates;
Ady Abraham64c2fc02020-12-29 12:07:50 -0800433
rnlee3bd610662021-06-23 16:27:57 -0700434 const Config mConfig;
Andy Yu2ae6b6b2021-11-18 14:51:06 -0800435 bool mSupportsFrameRateOverrideByContent;
Marin Shalamanov4c7831e2021-06-08 20:44:06 +0200436
ramindanid72ba162022-09-09 21:33:40 +0000437 struct GetRankedRefreshRatesCache {
Dominik Laskowskia8626ec2021-12-15 18:13:30 -0800438 std::pair<std::vector<LayerRequirement>, GlobalSignals> arguments;
ramindanid72ba162022-09-09 21:33:40 +0000439 std::pair<std::vector<RefreshRateRanking>, GlobalSignals> result;
Marin Shalamanov4c7831e2021-06-08 20:44:06 +0200440 };
ramindanid72ba162022-09-09 21:33:40 +0000441 mutable std::optional<GetRankedRefreshRatesCache> mGetRankedRefreshRatesCache GUARDED_BY(mLock);
Ady Abraham9a2ea342021-09-03 17:32:34 -0700442
Dominik Laskowski83bd7712022-01-07 14:30:53 -0800443 // Declare mIdleTimer last to ensure its thread joins before the mutex/callbacks are destroyed.
Ady Abraham9a2ea342021-09-03 17:32:34 -0700444 std::mutex mIdleTimerCallbacksMutex;
445 std::optional<IdleTimerCallbacks> mIdleTimerCallbacks GUARDED_BY(mIdleTimerCallbacksMutex);
Dominik Laskowski83bd7712022-01-07 14:30:53 -0800446 // Used to detect (lack of) frame activity.
447 std::optional<scheduler::OneShotTimer> mIdleTimer;
Ana Krulecb43429d2019-01-09 14:28:51 -0800448};
449
Dominik Laskowski98041832019-08-01 18:35:59 -0700450} // namespace android::scheduler