blob: 99f81aa75e9eb8cc987cb66266aa6fab1db16b1a [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 Laskowski530d6bd2022-10-10 16:55:54 -040026#include <ftl/concat.h>
Dominik Laskowskif6b4ba62021-11-09 12:46:10 -080027#include <gui/DisplayEventReceiver.h>
28
29#include <scheduler/Fps.h>
30#include <scheduler/Seamlessness.h>
31
Marin Shalamanov3ea1d602020-12-16 19:59:39 +010032#include "DisplayHardware/DisplayMode.h"
Ana Krulec4593b692019-01-11 22:07:25 -080033#include "DisplayHardware/HWComposer.h"
Ady Abraham9a2ea342021-09-03 17:32:34 -070034#include "Scheduler/OneShotTimer.h"
Ady Abraham2139f732019-11-13 18:56:40 -080035#include "Scheduler/StrongTyping.h"
Dominik Laskowskif8734e02022-08-26 09:06:59 -070036#include "ThreadContext.h"
Dominik Laskowskie70461a2022-08-30 14:42:01 -070037#include "Utils/Dumper.h"
Ana Krulec4593b692019-01-11 22:07:25 -080038
Dominik Laskowski98041832019-08-01 18:35:59 -070039namespace android::scheduler {
Ady Abrahamabc27602020-04-08 17:20:29 -070040
Ady Abraham4ccdcb42020-02-11 17:34:34 -080041using namespace std::chrono_literals;
Dominik Laskowski98041832019-08-01 18:35:59 -070042
Dominik Laskowski068173d2021-08-11 17:22:59 -070043enum class DisplayModeEvent : unsigned { None = 0b0, Changed = 0b1 };
Dominik Laskowski98041832019-08-01 18:35:59 -070044
Dominik Laskowski068173d2021-08-11 17:22:59 -070045inline DisplayModeEvent operator|(DisplayModeEvent lhs, DisplayModeEvent rhs) {
46 using T = std::underlying_type_t<DisplayModeEvent>;
47 return static_cast<DisplayModeEvent>(static_cast<T>(lhs) | static_cast<T>(rhs));
Dominik Laskowski98041832019-08-01 18:35:59 -070048}
Ana Krulecb43429d2019-01-09 14:28:51 -080049
Ady Abraham62f216c2020-10-13 19:07:23 -070050using FrameRateOverride = DisplayEventReceiver::Event::FrameRateOverride;
51
Ana Krulecb43429d2019-01-09 14:28:51 -080052/**
Ady Abraham1902d072019-03-01 17:18:59 -080053 * This class is used to encapsulate configuration for refresh rates. It holds information
Ana Krulecb43429d2019-01-09 14:28:51 -080054 * about available refresh rates on the device, and the mapping between the numbers and human
55 * readable names.
56 */
57class RefreshRateConfigs {
58public:
Ady Abraham4ccdcb42020-02-11 17:34:34 -080059 // Margin used when matching refresh rates to the content desired ones.
60 static constexpr nsecs_t MARGIN_FOR_PERIOD_CALCULATION =
rnlee3bd610662021-06-23 16:27:57 -070061 std::chrono::nanoseconds(800us).count();
Ady Abraham4ccdcb42020-02-11 17:34:34 -080062
Dominik Laskowski36dced82022-09-02 09:24:00 -070063 class Policy {
Marin Shalamanov30b0b3c2020-10-13 19:15:06 +020064 static constexpr int kAllowGroupSwitchingDefault = false;
65
66 public:
Marin Shalamanova7fe3042021-01-29 21:02:08 +010067 // The default mode, used to ensure we only initiate display mode switches within the
68 // same mode group as defaultMode's group.
69 DisplayModeId defaultMode;
70 // Whether or not we switch mode groups to get the best frame rate.
Marin Shalamanov30b0b3c2020-10-13 19:15:06 +020071 bool allowGroupSwitching = kAllowGroupSwitchingDefault;
Steven Thomasf734df42020-04-13 21:09:28 -070072 // The primary refresh rate range represents display manager's general guidance on the
Marin Shalamanova7fe3042021-01-29 21:02:08 +010073 // display modes we'll consider when switching refresh rates. Unless we get an explicit
Steven Thomasf734df42020-04-13 21:09:28 -070074 // signal from an app, we should stay within this range.
Marin Shalamanoveadf2e72020-12-10 15:35:28 +010075 FpsRange primaryRange;
Marin Shalamanova7fe3042021-01-29 21:02:08 +010076 // The app request refresh rate range allows us to consider more display modes when
Steven Thomasf734df42020-04-13 21:09:28 -070077 // switching refresh rates. Although we should generally stay within the primary range,
78 // specific considerations, such as layer frame rate settings specified via the
79 // setFrameRate() api, may cause us to go outside the primary range. We never go outside the
80 // app request range. The app request range will be greater than or equal to the primary
81 // refresh rate range, never smaller.
Marin Shalamanoveadf2e72020-12-10 15:35:28 +010082 FpsRange appRequestRange;
Steven Thomasd4071902020-03-24 16:02:53 -070083
Steven Thomasf734df42020-04-13 21:09:28 -070084 Policy() = default;
Marin Shalamanov30b0b3c2020-10-13 19:15:06 +020085
Dominik Laskowski953b7fd2022-01-08 19:34:59 -080086 Policy(DisplayModeId defaultMode, FpsRange range)
Marin Shalamanova7fe3042021-01-29 21:02:08 +010087 : Policy(defaultMode, kAllowGroupSwitchingDefault, range, range) {}
Marin Shalamanov30b0b3c2020-10-13 19:15:06 +020088
Dominik Laskowski953b7fd2022-01-08 19:34:59 -080089 Policy(DisplayModeId defaultMode, bool allowGroupSwitching, FpsRange range)
Marin Shalamanova7fe3042021-01-29 21:02:08 +010090 : Policy(defaultMode, allowGroupSwitching, range, range) {}
Marin Shalamanov30b0b3c2020-10-13 19:15:06 +020091
Dominik Laskowski953b7fd2022-01-08 19:34:59 -080092 Policy(DisplayModeId defaultMode, FpsRange primaryRange, FpsRange appRequestRange)
Marin Shalamanova7fe3042021-01-29 21:02:08 +010093 : Policy(defaultMode, kAllowGroupSwitchingDefault, primaryRange, appRequestRange) {}
Marin Shalamanov30b0b3c2020-10-13 19:15:06 +020094
Dominik Laskowski953b7fd2022-01-08 19:34:59 -080095 Policy(DisplayModeId defaultMode, bool allowGroupSwitching, FpsRange primaryRange,
96 FpsRange appRequestRange)
Marin Shalamanova7fe3042021-01-29 21:02:08 +010097 : defaultMode(defaultMode),
Marin Shalamanov30b0b3c2020-10-13 19:15:06 +020098 allowGroupSwitching(allowGroupSwitching),
Steven Thomasf734df42020-04-13 21:09:28 -070099 primaryRange(primaryRange),
100 appRequestRange(appRequestRange) {}
101
Steven Thomasd4071902020-03-24 16:02:53 -0700102 bool operator==(const Policy& other) const {
Dominik Laskowski953b7fd2022-01-08 19:34:59 -0800103 using namespace fps_approx_ops;
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100104 return defaultMode == other.defaultMode && primaryRange == other.primaryRange &&
Steven Thomasf734df42020-04-13 21:09:28 -0700105 appRequestRange == other.appRequestRange &&
Steven Thomasd4071902020-03-24 16:02:53 -0700106 allowGroupSwitching == other.allowGroupSwitching;
107 }
108
109 bool operator!=(const Policy& other) const { return !(*this == other); }
Marin Shalamanovb6674e72020-11-06 13:05:57 +0100110 std::string toString() const;
Steven Thomasd4071902020-03-24 16:02:53 -0700111 };
112
Dominik Laskowski36dced82022-09-02 09:24:00 -0700113 enum class SetPolicyResult { Invalid, Unchanged, Changed };
Steven Thomasd4071902020-03-24 16:02:53 -0700114
115 // We maintain the display manager policy and the override policy separately. The override
116 // policy is used by CTS tests to get a consistent device state for testing. While the override
117 // policy is set, it takes precedence over the display manager policy. Once the override policy
118 // is cleared, we revert to using the display manager policy.
Dominik Laskowski36dced82022-09-02 09:24:00 -0700119 struct DisplayManagerPolicy : Policy {
120 using Policy::Policy;
121 };
Steven Thomasd4071902020-03-24 16:02:53 -0700122
Dominik Laskowski36dced82022-09-02 09:24:00 -0700123 struct OverridePolicy : Policy {
124 using Policy::Policy;
125 };
126
127 struct NoOverridePolicy {};
128
129 using PolicyVariant = std::variant<DisplayManagerPolicy, OverridePolicy, NoOverridePolicy>;
130
131 SetPolicyResult setPolicy(const PolicyVariant&) EXCLUDES(mLock) REQUIRES(kMainThreadContext);
132
133 void onModeChangeInitiated() REQUIRES(kMainThreadContext) { mNumModeSwitchesInPolicy++; }
134
Steven Thomasd4071902020-03-24 16:02:53 -0700135 // Gets the current policy, which will be the override policy if active, and the display manager
136 // policy otherwise.
137 Policy getCurrentPolicy() const EXCLUDES(mLock);
138 // Gets the display manager policy, regardless of whether an override policy is active.
139 Policy getDisplayManagerPolicy() const EXCLUDES(mLock);
Ana Kruleced3a8cc2019-11-14 00:55:07 +0100140
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100141 // Returns true if mode is allowed by the current policy.
142 bool isModeAllowed(DisplayModeId) const EXCLUDES(mLock);
Ady Abraham2139f732019-11-13 18:56:40 -0800143
Ady Abraham8a82ba62020-01-17 12:43:17 -0800144 // Describes the different options the layer voted for refresh rate
145 enum class LayerVoteType {
Ady Abraham71c437d2020-01-31 15:56:57 -0800146 NoVote, // Doesn't care about the refresh rate
147 Min, // Minimal refresh rate available
148 Max, // Maximal refresh rate available
149 Heuristic, // Specific refresh rate that was calculated by platform using a heuristic
150 ExplicitDefault, // Specific refresh rate that was provided by the app with Default
151 // compatibility
Ady Abrahamdd5bfa92021-01-07 17:56:08 -0800152 ExplicitExactOrMultiple, // Specific refresh rate that was provided by the app with
153 // ExactOrMultiple compatibility
154 ExplicitExact, // Specific refresh rate that was provided by the app with
155 // Exact compatibility
156
Dominik Laskowskif5d0ea52021-09-26 17:27:01 -0700157 ftl_last = ExplicitExact
Ady Abraham8a82ba62020-01-17 12:43:17 -0800158 };
159
160 // Captures the layer requirements for a refresh rate. This will be used to determine the
161 // display refresh rate.
162 struct LayerRequirement {
Ady Abrahamaae5ed52020-06-26 09:32:43 -0700163 // Layer's name. Used for debugging purposes.
164 std::string name;
Ady Abraham62a0be22020-12-08 16:54:10 -0800165 // Layer's owner uid
166 uid_t ownerUid = static_cast<uid_t>(-1);
Ady Abrahamaae5ed52020-06-26 09:32:43 -0700167 // Layer vote type.
168 LayerVoteType vote = LayerVoteType::NoVote;
169 // Layer's desired refresh rate, if applicable.
Dominik Laskowski6eab42d2021-09-13 14:34:13 -0700170 Fps desiredRefreshRate;
Marin Shalamanov46084422020-10-13 12:33:42 +0200171 // If a seamless mode switch is required.
Marin Shalamanov53fc11d2020-11-20 14:00:13 +0100172 Seamlessness seamlessness = Seamlessness::Default;
Ady Abrahamaae5ed52020-06-26 09:32:43 -0700173 // Layer's weight in the range of [0, 1]. The higher the weight the more impact this layer
174 // would have on choosing the refresh rate.
175 float weight = 0.0f;
176 // Whether layer is in focus or not based on WindowManager's state
177 bool focused = false;
Ady Abraham8a82ba62020-01-17 12:43:17 -0800178
179 bool operator==(const LayerRequirement& other) const {
180 return name == other.name && vote == other.vote &&
Dominik Laskowski6eab42d2021-09-13 14:34:13 -0700181 isApproxEqual(desiredRefreshRate, other.desiredRefreshRate) &&
Marin Shalamanovbe97cfa2020-12-01 16:02:07 +0100182 seamlessness == other.seamlessness && weight == other.weight &&
Ady Abrahamaae5ed52020-06-26 09:32:43 -0700183 focused == other.focused;
Ady Abraham8a82ba62020-01-17 12:43:17 -0800184 }
185
186 bool operator!=(const LayerRequirement& other) const { return !(*this == other); }
187 };
188
Ady Abrahamdfd62162020-06-10 16:11:56 -0700189 // Global state describing signals that affect refresh rate choice.
190 struct GlobalSignals {
191 // Whether the user touched the screen recently. Used to apply touch boost.
192 bool touch = false;
193 // True if the system hasn't seen any buffers posted to layers recently.
194 bool idle = false;
ramindani38c84982022-08-29 18:02:57 +0000195 // Whether the display is about to be powered on, or has been in PowerMode::ON
196 // within the timeout of DisplayPowerTimer.
197 bool powerOnImminent = false;
Marin Shalamanov4c7831e2021-06-08 20:44:06 +0200198
Dominik Laskowski6eab42d2021-09-13 14:34:13 -0700199 bool operator==(GlobalSignals other) const {
ramindani38c84982022-08-29 18:02:57 +0000200 return touch == other.touch && idle == other.idle &&
201 powerOnImminent == other.powerOnImminent;
Marin Shalamanov4c7831e2021-06-08 20:44:06 +0200202 }
Dominik Laskowski530d6bd2022-10-10 16:55:54 -0400203
204 auto toString() const {
205 return ftl::Concat("{touch=", touch, ", idle=", idle,
206 ", powerOnImminent=", powerOnImminent, '}');
207 }
Ady Abrahamdfd62162020-06-10 16:11:56 -0700208 };
209
Dominik Laskowski530d6bd2022-10-10 16:55:54 -0400210 struct ScoredRefreshRate {
211 DisplayModePtr modePtr;
212 float score = 0.0f;
213
214 bool operator==(const ScoredRefreshRate& other) const {
215 return modePtr == other.modePtr && score == other.score;
216 }
217
218 static bool scoresEqual(float lhs, float rhs) {
219 constexpr float kEpsilon = 0.0001f;
220 return std::abs(lhs - rhs) <= kEpsilon;
221 }
222
223 struct DescendingScore {
224 bool operator()(const ScoredRefreshRate& lhs, const ScoredRefreshRate& rhs) const {
225 return lhs.score > rhs.score && !scoresEqual(lhs.score, rhs.score);
226 }
227 };
228 };
229
230 using RefreshRateRanking = std::vector<ScoredRefreshRate>;
231
232 struct RankedRefreshRates {
233 RefreshRateRanking ranking; // Ordered by descending score.
234 GlobalSignals consideredSignals;
235
236 bool operator==(const RankedRefreshRates& other) const {
237 return ranking == other.ranking && consideredSignals == other.consideredSignals;
238 }
239 };
240
241 RankedRefreshRates getRankedRefreshRates(const std::vector<LayerRequirement>&,
242 GlobalSignals) const EXCLUDES(mLock);
Ana Krulecb43429d2019-01-09 14:28:51 -0800243
Marin Shalamanoveadf2e72020-12-10 15:35:28 +0100244 FpsRange getSupportedRefreshRateRange() const EXCLUDES(mLock) {
245 std::lock_guard lock(mLock);
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800246 return {mMinRefreshRateModeIt->second->getFps(), mMaxRefreshRateModeIt->second->getFps()};
Marin Shalamanoveadf2e72020-12-10 15:35:28 +0100247 }
Steven Thomas2bbaabe2019-08-28 16:08:35 -0700248
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100249 std::optional<Fps> onKernelTimerChanged(std::optional<DisplayModeId> desiredActiveModeId,
Marin Shalamanoveadf2e72020-12-10 15:35:28 +0100250 bool timerExpired) const EXCLUDES(mLock);
Steven Thomas2bbaabe2019-08-28 16:08:35 -0700251
Dominik Laskowskif8734e02022-08-26 09:06:59 -0700252 void setActiveModeId(DisplayModeId) EXCLUDES(mLock) REQUIRES(kMainThreadContext);
253
254 // See mActiveModeIt for thread safety.
255 DisplayModePtr getActiveModePtr() const EXCLUDES(mLock);
256 const DisplayMode& getActiveMode() const REQUIRES(kMainThreadContext);
Dominik Laskowski22488f62019-03-28 09:53:04 -0700257
Ady Abrahamb1b9d412020-06-01 19:53:52 -0700258 // Returns a known frame rate that is the closest to frameRate
Marin Shalamanove8a663d2020-11-24 17:48:00 +0100259 Fps findClosestKnownFrameRate(Fps frameRate) const;
Ady Abrahamb1b9d412020-06-01 19:53:52 -0700260
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800261 enum class KernelIdleTimerController { Sysprop, HwcApi, ftl_last = HwcApi };
ramindani32cf0602022-03-02 02:30:29 +0000262
rnlee3bd610662021-06-23 16:27:57 -0700263 // Configuration flags.
264 struct Config {
265 bool enableFrameRateOverride = false;
266
267 // Specifies the upper refresh rate threshold (inclusive) for layer vote types of multiple
268 // or heuristic, such that refresh rates higher than this value will not be voted for. 0 if
269 // no threshold is set.
270 int frameRateMultipleThreshold = 0;
Ady Abraham9a2ea342021-09-03 17:32:34 -0700271
Ady Abraham6d885932021-09-03 18:05:48 -0700272 // The Idle Timer timeout. 0 timeout means no idle timer.
ramindani32cf0602022-03-02 02:30:29 +0000273 std::chrono::milliseconds idleTimerTimeout = 0ms;
Ady Abraham6d885932021-09-03 18:05:48 -0700274
ramindani32cf0602022-03-02 02:30:29 +0000275 // The controller representing how the kernel idle timer will be configured
276 // either on the HWC api or sysprop.
277 std::optional<KernelIdleTimerController> kernelIdleTimerController;
rnlee3bd610662021-06-23 16:27:57 -0700278 };
279
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800280 RefreshRateConfigs(DisplayModes, DisplayModeId activeModeId,
rnlee3bd610662021-06-23 16:27:57 -0700281 Config config = {.enableFrameRateOverride = false,
Ady Abraham9a2ea342021-09-03 17:32:34 -0700282 .frameRateMultipleThreshold = 0,
ramindani32cf0602022-03-02 02:30:29 +0000283 .idleTimerTimeout = 0ms,
284 .kernelIdleTimerController = {}});
Ana Krulec4593b692019-01-11 22:07:25 -0800285
Dominik Laskowski0c252702021-12-20 20:32:09 -0800286 RefreshRateConfigs(const RefreshRateConfigs&) = delete;
287 RefreshRateConfigs& operator=(const RefreshRateConfigs&) = delete;
288
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100289 // Returns whether switching modes (refresh rate or resolution) is possible.
290 // TODO(b/158780872): Consider HAL support, and skip frame rate detection if the modes only
Dominik Laskowski983f2b52020-06-25 16:54:06 -0700291 // differ in resolution.
Marin Shalamanoveadf2e72020-12-10 15:35:28 +0100292 bool canSwitch() const EXCLUDES(mLock) {
293 std::lock_guard lock(mLock);
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800294 return mDisplayModes.size() > 1;
Marin Shalamanoveadf2e72020-12-10 15:35:28 +0100295 }
Dominik Laskowski983f2b52020-06-25 16:54:06 -0700296
TreeHugger Robot758ab612021-06-22 19:17:29 +0000297 // Class to enumerate options around toggling the kernel timer on and off.
Ana Krulecb9afd792020-06-11 13:16:15 -0700298 enum class KernelIdleTimerAction {
Ana Krulecb9afd792020-06-11 13:16:15 -0700299 TurnOff, // Turn off the idle timer.
300 TurnOn // Turn on the idle timer.
301 };
ramindani32cf0602022-03-02 02:30:29 +0000302
Ana Krulecb9afd792020-06-11 13:16:15 -0700303 // Checks whether kernel idle timer should be active depending the policy decisions around
304 // refresh rates.
305 KernelIdleTimerAction getIdleTimerAction() const;
306
Andy Yu2ae6b6b2021-11-18 14:51:06 -0800307 bool supportsFrameRateOverrideByContent() const { return mSupportsFrameRateOverrideByContent; }
Ady Abraham64c2fc02020-12-29 12:07:50 -0800308
Ady Abrahamcc315492022-02-17 17:06:39 -0800309 // Return the display refresh rate divisor to match the layer
Ady Abraham5cc2e262021-03-25 13:09:17 -0700310 // frame rate, or 0 if the display refresh rate is not a multiple of the
311 // layer refresh rate.
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800312 static int getFrameRateDivisor(Fps displayRefreshRate, Fps layerFrameRate);
Ady Abraham62a0be22020-12-08 16:54:10 -0800313
Marin Shalamanov15a0fc62021-08-16 18:20:21 +0200314 // Returns if the provided frame rates have a ratio t*1000/1001 or t*1001/1000
315 // for an integer t.
316 static bool isFractionalPairOrMultiple(Fps, Fps);
317
Ady Abraham62a0be22020-12-08 16:54:10 -0800318 using UidToFrameRateOverride = std::map<uid_t, Fps>;
Dominik Laskowski6eab42d2021-09-13 14:34:13 -0700319
Ady Abrahamdd5bfa92021-01-07 17:56:08 -0800320 // Returns the frame rate override for each uid.
Dominik Laskowski6eab42d2021-09-13 14:34:13 -0700321 UidToFrameRateOverride getFrameRateOverrides(const std::vector<LayerRequirement>&,
322 Fps displayFrameRate, GlobalSignals) const
Ady Abrahamdd5bfa92021-01-07 17:56:08 -0800323 EXCLUDES(mLock);
Ady Abraham0bb6a472020-10-12 10:22:13 -0700324
ramindani32cf0602022-03-02 02:30:29 +0000325 std::optional<KernelIdleTimerController> kernelIdleTimerController() {
326 return mConfig.kernelIdleTimerController;
327 }
Ady Abraham9a2ea342021-09-03 17:32:34 -0700328
Dominik Laskowski83bd7712022-01-07 14:30:53 -0800329 struct IdleTimerCallbacks {
330 struct Callbacks {
331 std::function<void()> onReset;
332 std::function<void()> onExpired;
333 };
334
335 Callbacks platform;
336 Callbacks kernel;
337 };
338
339 void setIdleTimerCallbacks(IdleTimerCallbacks callbacks) EXCLUDES(mIdleTimerCallbacksMutex) {
Ady Abraham9a2ea342021-09-03 17:32:34 -0700340 std::scoped_lock lock(mIdleTimerCallbacksMutex);
Dominik Laskowski83bd7712022-01-07 14:30:53 -0800341 mIdleTimerCallbacks = std::move(callbacks);
342 }
343
344 void clearIdleTimerCallbacks() EXCLUDES(mIdleTimerCallbacksMutex) {
345 std::scoped_lock lock(mIdleTimerCallbacksMutex);
346 mIdleTimerCallbacks.reset();
Ady Abraham11663402021-11-10 19:46:09 -0800347 }
348
349 void startIdleTimer() {
350 if (mIdleTimer) {
351 mIdleTimer->start();
352 }
353 }
354
355 void stopIdleTimer() {
356 if (mIdleTimer) {
357 mIdleTimer->stop();
358 }
Ady Abraham9a2ea342021-09-03 17:32:34 -0700359 }
360
361 void resetIdleTimer(bool kernelOnly) {
362 if (!mIdleTimer) {
363 return;
364 }
ramindani32cf0602022-03-02 02:30:29 +0000365 if (kernelOnly && !mConfig.kernelIdleTimerController.has_value()) {
Ady Abraham9a2ea342021-09-03 17:32:34 -0700366 return;
367 }
368 mIdleTimer->reset();
Dominik Laskowski83bd7712022-01-07 14:30:53 -0800369 }
Ady Abraham9a2ea342021-09-03 17:32:34 -0700370
Dominik Laskowskie70461a2022-08-30 14:42:01 -0700371 void dump(utils::Dumper&) const EXCLUDES(mLock);
Marin Shalamanovba421a82020-11-10 21:49:26 +0100372
ramindani32cf0602022-03-02 02:30:29 +0000373 std::chrono::milliseconds getIdleTimerTimeout();
374
Dominik Laskowski22488f62019-03-28 09:53:04 -0700375private:
Dominik Laskowski0c252702021-12-20 20:32:09 -0800376 friend struct TestableRefreshRateConfigs;
Ady Abrahamb1b9d412020-06-01 19:53:52 -0700377
Ady Abraham2139f732019-11-13 18:56:40 -0800378 void constructAvailableRefreshRates() REQUIRES(mLock);
379
Dominik Laskowskif8734e02022-08-26 09:06:59 -0700380 // See mActiveModeIt for thread safety.
381 DisplayModeIterator getActiveModeItLocked() const REQUIRES(mLock);
382
Dominik Laskowski530d6bd2022-10-10 16:55:54 -0400383 RankedRefreshRates getRankedRefreshRatesLocked(const std::vector<LayerRequirement>&,
384 GlobalSignals) const REQUIRES(mLock);
Marin Shalamanov4c7831e2021-06-08 20:44:06 +0200385
Ady Abraham4ccdcb42020-02-11 17:34:34 -0800386 // Returns number of display frames and remainder when dividing the layer refresh period by
387 // display refresh period.
388 std::pair<nsecs_t, nsecs_t> getDisplayFrames(nsecs_t layerPeriod, nsecs_t displayPeriod) const;
389
Steven Thomasf734df42020-04-13 21:09:28 -0700390 // Returns the lowest refresh rate according to the current policy. May change at runtime. Only
391 // uses the primary range, not the app request range.
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800392 const DisplayModePtr& getMinRefreshRateByPolicyLocked() const REQUIRES(mLock);
Steven Thomasf734df42020-04-13 21:09:28 -0700393
394 // Returns the highest refresh rate according to the current policy. May change at runtime. Only
395 // uses the primary range, not the app request range.
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800396 const DisplayModePtr& getMaxRefreshRateByPolicyLocked(int anchorGroup) const REQUIRES(mLock);
Marin Shalamanov8cd8a992021-09-14 23:22:49 +0200397
ramindanid72ba162022-09-09 21:33:40 +0000398 struct RefreshRateScoreComparator;
399
400 enum class RefreshRateOrder { Ascending, Descending };
401
ramindanid72ba162022-09-09 21:33:40 +0000402 // Only uses the primary range, not the app request range.
Dominik Laskowski530d6bd2022-10-10 16:55:54 -0400403 RefreshRateRanking rankRefreshRates(std::optional<int> anchorGroupOpt, RefreshRateOrder,
404 std::optional<DisplayModeId> preferredDisplayModeOpt =
405 std::nullopt) const REQUIRES(mLock);
ramindanid72ba162022-09-09 21:33:40 +0000406
Steven Thomasd4071902020-03-24 16:02:53 -0700407 const Policy* getCurrentPolicyLocked() const REQUIRES(mLock);
Marin Shalamanoveadf2e72020-12-10 15:35:28 +0100408 bool isPolicyValidLocked(const Policy& policy) const REQUIRES(mLock);
Steven Thomasd4071902020-03-24 16:02:53 -0700409
ramindanid72ba162022-09-09 21:33:40 +0000410 // Returns the refresh rate score as a ratio to max refresh rate, which has a score of 1.
411 float calculateRefreshRateScoreForFps(Fps refreshRate) const REQUIRES(mLock);
Ady Abraham62a0be22020-12-08 16:54:10 -0800412 // calculates a score for a layer. Used to determine the display refresh rate
413 // and the frame rate override for certains applications.
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800414 float calculateLayerScoreLocked(const LayerRequirement&, Fps refreshRate,
Ady Abraham62a0be22020-12-08 16:54:10 -0800415 bool isSeamlessSwitch) const REQUIRES(mLock);
416
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800417 float calculateNonExactMatchingLayerScoreLocked(const LayerRequirement&, Fps refreshRate) const
418 REQUIRES(mLock);
Ady Abraham05243be2021-09-16 15:58:52 -0700419
Dominik Laskowskif8734e02022-08-26 09:06:59 -0700420 void updateDisplayModes(DisplayModes, DisplayModeId activeModeId) EXCLUDES(mLock)
421 REQUIRES(kMainThreadContext);
Ady Abraham3efa3942021-06-24 19:01:25 -0700422
Ady Abraham9a2ea342021-09-03 17:32:34 -0700423 void initializeIdleTimer();
424
Dominik Laskowski83bd7712022-01-07 14:30:53 -0800425 std::optional<IdleTimerCallbacks::Callbacks> getIdleTimerCallbacks() const
426 REQUIRES(mIdleTimerCallbacksMutex) {
427 if (!mIdleTimerCallbacks) return {};
ramindani32cf0602022-03-02 02:30:29 +0000428 return mConfig.kernelIdleTimerController.has_value() ? mIdleTimerCallbacks->kernel
429 : mIdleTimerCallbacks->platform;
Dominik Laskowski83bd7712022-01-07 14:30:53 -0800430 }
431
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800432 // The display modes of the active display. The DisplayModeIterators below are pointers into
433 // this container, so must be invalidated whenever the DisplayModes change. The Policy below
434 // is also dependent, so must be reset as well.
435 DisplayModes mDisplayModes GUARDED_BY(mLock);
Ady Abraham2139f732019-11-13 18:56:40 -0800436
Dominik Laskowskif8734e02022-08-26 09:06:59 -0700437 // Written under mLock exclusively from kMainThreadContext, so reads from kMainThreadContext
438 // need not be under mLock.
439 DisplayModeIterator mActiveModeIt GUARDED_BY(mLock) GUARDED_BY(kMainThreadContext);
440
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800441 DisplayModeIterator mMinRefreshRateModeIt GUARDED_BY(mLock);
442 DisplayModeIterator mMaxRefreshRateModeIt GUARDED_BY(mLock);
Steven Thomasf734df42020-04-13 21:09:28 -0700443
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800444 // Display modes that satisfy the Policy's ranges, filtered and sorted by refresh rate.
445 std::vector<DisplayModeIterator> mPrimaryRefreshRates GUARDED_BY(mLock);
446 std::vector<DisplayModeIterator> mAppRequestRefreshRates GUARDED_BY(mLock);
Ady Abraham2139f732019-11-13 18:56:40 -0800447
Steven Thomasd4071902020-03-24 16:02:53 -0700448 Policy mDisplayManagerPolicy GUARDED_BY(mLock);
449 std::optional<Policy> mOverridePolicy GUARDED_BY(mLock);
Ady Abraham2139f732019-11-13 18:56:40 -0800450
Dominik Laskowski36dced82022-09-02 09:24:00 -0700451 unsigned mNumModeSwitchesInPolicy GUARDED_BY(kMainThreadContext) = 0;
452
Ady Abraham2139f732019-11-13 18:56:40 -0800453 mutable std::mutex mLock;
Ady Abrahamb1b9d412020-06-01 19:53:52 -0700454
455 // A sorted list of known frame rates that a Heuristic layer will choose
456 // from based on the closest value.
Marin Shalamanove8a663d2020-11-24 17:48:00 +0100457 const std::vector<Fps> mKnownFrameRates;
Ady Abraham64c2fc02020-12-29 12:07:50 -0800458
rnlee3bd610662021-06-23 16:27:57 -0700459 const Config mConfig;
Andy Yu2ae6b6b2021-11-18 14:51:06 -0800460 bool mSupportsFrameRateOverrideByContent;
Marin Shalamanov4c7831e2021-06-08 20:44:06 +0200461
ramindanid72ba162022-09-09 21:33:40 +0000462 struct GetRankedRefreshRatesCache {
Dominik Laskowskia8626ec2021-12-15 18:13:30 -0800463 std::pair<std::vector<LayerRequirement>, GlobalSignals> arguments;
Dominik Laskowski530d6bd2022-10-10 16:55:54 -0400464 RankedRefreshRates result;
Marin Shalamanov4c7831e2021-06-08 20:44:06 +0200465 };
ramindanid72ba162022-09-09 21:33:40 +0000466 mutable std::optional<GetRankedRefreshRatesCache> mGetRankedRefreshRatesCache GUARDED_BY(mLock);
Ady Abraham9a2ea342021-09-03 17:32:34 -0700467
Dominik Laskowski83bd7712022-01-07 14:30:53 -0800468 // Declare mIdleTimer last to ensure its thread joins before the mutex/callbacks are destroyed.
Ady Abraham9a2ea342021-09-03 17:32:34 -0700469 std::mutex mIdleTimerCallbacksMutex;
470 std::optional<IdleTimerCallbacks> mIdleTimerCallbacks GUARDED_BY(mIdleTimerCallbacksMutex);
Dominik Laskowski83bd7712022-01-07 14:30:53 -0800471 // Used to detect (lack of) frame activity.
472 std::optional<scheduler::OneShotTimer> mIdleTimer;
Ana Krulecb43429d2019-01-09 14:28:51 -0800473};
474
Dominik Laskowski98041832019-08-01 18:35:59 -0700475} // namespace android::scheduler