blob: 887d81566a833d5ac0b24e94e9e8784594d96521 [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
Dominik Laskowskid82e0f02022-10-26 15:23:04 -040052// Selects the refresh rate of a display by ranking its `DisplayModes` in accordance with
53// the DisplayManager (or override) `Policy`, the `LayerRequirement` of each active layer,
54// and `GlobalSignals`.
55class RefreshRateSelector {
Ana Krulecb43429d2019-01-09 14:28:51 -080056public:
Ady Abraham4ccdcb42020-02-11 17:34:34 -080057 // Margin used when matching refresh rates to the content desired ones.
58 static constexpr nsecs_t MARGIN_FOR_PERIOD_CALCULATION =
rnlee3bd610662021-06-23 16:27:57 -070059 std::chrono::nanoseconds(800us).count();
Ady Abraham4ccdcb42020-02-11 17:34:34 -080060
Dominik Laskowski36dced82022-09-02 09:24:00 -070061 class Policy {
Marin Shalamanov30b0b3c2020-10-13 19:15:06 +020062 static constexpr int kAllowGroupSwitchingDefault = false;
63
64 public:
Marin Shalamanova7fe3042021-01-29 21:02:08 +010065 // The default mode, used to ensure we only initiate display mode switches within the
66 // same mode group as defaultMode's group.
67 DisplayModeId defaultMode;
68 // Whether or not we switch mode groups to get the best frame rate.
Marin Shalamanov30b0b3c2020-10-13 19:15:06 +020069 bool allowGroupSwitching = kAllowGroupSwitchingDefault;
Ady Abraham285f8c12022-10-11 17:12:14 -070070 // The primary refresh rate ranges. @see DisplayModeSpecs.aidl for details.
71 // TODO(b/257072060): use the render range when selecting SF render rate
72 // or the app override frame rate
73 FpsRanges primaryRanges;
74 // The app request refresh rate ranges. @see DisplayModeSpecs.aidl for details.
75 FpsRanges appRequestRanges;
Steven Thomasd4071902020-03-24 16:02:53 -070076
Steven Thomasf734df42020-04-13 21:09:28 -070077 Policy() = default;
Marin Shalamanov30b0b3c2020-10-13 19:15:06 +020078
Ady Abraham285f8c12022-10-11 17:12:14 -070079 Policy(DisplayModeId defaultMode, FpsRange range,
80 bool allowGroupSwitching = kAllowGroupSwitchingDefault)
81 : Policy(defaultMode, FpsRanges{range, range}, FpsRanges{range, range},
82 allowGroupSwitching) {}
Marin Shalamanov30b0b3c2020-10-13 19:15:06 +020083
Ady Abraham285f8c12022-10-11 17:12:14 -070084 Policy(DisplayModeId defaultMode, FpsRanges primaryRanges, FpsRanges appRequestRanges,
85 bool allowGroupSwitching = kAllowGroupSwitchingDefault)
Marin Shalamanova7fe3042021-01-29 21:02:08 +010086 : defaultMode(defaultMode),
Marin Shalamanov30b0b3c2020-10-13 19:15:06 +020087 allowGroupSwitching(allowGroupSwitching),
Ady Abraham285f8c12022-10-11 17:12:14 -070088 primaryRanges(primaryRanges),
89 appRequestRanges(appRequestRanges) {}
Steven Thomasf734df42020-04-13 21:09:28 -070090
Steven Thomasd4071902020-03-24 16:02:53 -070091 bool operator==(const Policy& other) const {
Dominik Laskowski953b7fd2022-01-08 19:34:59 -080092 using namespace fps_approx_ops;
Ady Abraham285f8c12022-10-11 17:12:14 -070093 return defaultMode == other.defaultMode && primaryRanges == other.primaryRanges &&
94 appRequestRanges == other.appRequestRanges &&
Steven Thomasd4071902020-03-24 16:02:53 -070095 allowGroupSwitching == other.allowGroupSwitching;
96 }
97
98 bool operator!=(const Policy& other) const { return !(*this == other); }
Marin Shalamanovb6674e72020-11-06 13:05:57 +010099 std::string toString() const;
Steven Thomasd4071902020-03-24 16:02:53 -0700100 };
101
Dominik Laskowski36dced82022-09-02 09:24:00 -0700102 enum class SetPolicyResult { Invalid, Unchanged, Changed };
Steven Thomasd4071902020-03-24 16:02:53 -0700103
104 // We maintain the display manager policy and the override policy separately. The override
105 // policy is used by CTS tests to get a consistent device state for testing. While the override
106 // policy is set, it takes precedence over the display manager policy. Once the override policy
107 // is cleared, we revert to using the display manager policy.
Dominik Laskowski36dced82022-09-02 09:24:00 -0700108 struct DisplayManagerPolicy : Policy {
109 using Policy::Policy;
110 };
Steven Thomasd4071902020-03-24 16:02:53 -0700111
Dominik Laskowski36dced82022-09-02 09:24:00 -0700112 struct OverridePolicy : Policy {
113 using Policy::Policy;
114 };
115
116 struct NoOverridePolicy {};
117
118 using PolicyVariant = std::variant<DisplayManagerPolicy, OverridePolicy, NoOverridePolicy>;
119
120 SetPolicyResult setPolicy(const PolicyVariant&) EXCLUDES(mLock) REQUIRES(kMainThreadContext);
121
122 void onModeChangeInitiated() REQUIRES(kMainThreadContext) { mNumModeSwitchesInPolicy++; }
123
Steven Thomasd4071902020-03-24 16:02:53 -0700124 // Gets the current policy, which will be the override policy if active, and the display manager
125 // policy otherwise.
126 Policy getCurrentPolicy() const EXCLUDES(mLock);
127 // Gets the display manager policy, regardless of whether an override policy is active.
128 Policy getDisplayManagerPolicy() const EXCLUDES(mLock);
Ana Kruleced3a8cc2019-11-14 00:55:07 +0100129
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100130 // Returns true if mode is allowed by the current policy.
131 bool isModeAllowed(DisplayModeId) const EXCLUDES(mLock);
Ady Abraham2139f732019-11-13 18:56:40 -0800132
Ady Abraham8a82ba62020-01-17 12:43:17 -0800133 // Describes the different options the layer voted for refresh rate
134 enum class LayerVoteType {
Ady Abraham71c437d2020-01-31 15:56:57 -0800135 NoVote, // Doesn't care about the refresh rate
136 Min, // Minimal refresh rate available
137 Max, // Maximal refresh rate available
138 Heuristic, // Specific refresh rate that was calculated by platform using a heuristic
139 ExplicitDefault, // Specific refresh rate that was provided by the app with Default
140 // compatibility
Ady Abrahamdd5bfa92021-01-07 17:56:08 -0800141 ExplicitExactOrMultiple, // Specific refresh rate that was provided by the app with
142 // ExactOrMultiple compatibility
143 ExplicitExact, // Specific refresh rate that was provided by the app with
144 // Exact compatibility
145
Dominik Laskowskif5d0ea52021-09-26 17:27:01 -0700146 ftl_last = ExplicitExact
Ady Abraham8a82ba62020-01-17 12:43:17 -0800147 };
148
149 // Captures the layer requirements for a refresh rate. This will be used to determine the
150 // display refresh rate.
151 struct LayerRequirement {
Ady Abrahamaae5ed52020-06-26 09:32:43 -0700152 // Layer's name. Used for debugging purposes.
153 std::string name;
Ady Abraham62a0be22020-12-08 16:54:10 -0800154 // Layer's owner uid
155 uid_t ownerUid = static_cast<uid_t>(-1);
Ady Abrahamaae5ed52020-06-26 09:32:43 -0700156 // Layer vote type.
157 LayerVoteType vote = LayerVoteType::NoVote;
158 // Layer's desired refresh rate, if applicable.
Dominik Laskowski6eab42d2021-09-13 14:34:13 -0700159 Fps desiredRefreshRate;
Marin Shalamanov46084422020-10-13 12:33:42 +0200160 // If a seamless mode switch is required.
Marin Shalamanov53fc11d2020-11-20 14:00:13 +0100161 Seamlessness seamlessness = Seamlessness::Default;
Ady Abrahamaae5ed52020-06-26 09:32:43 -0700162 // Layer's weight in the range of [0, 1]. The higher the weight the more impact this layer
163 // would have on choosing the refresh rate.
164 float weight = 0.0f;
165 // Whether layer is in focus or not based on WindowManager's state
166 bool focused = false;
Ady Abraham8a82ba62020-01-17 12:43:17 -0800167
168 bool operator==(const LayerRequirement& other) const {
169 return name == other.name && vote == other.vote &&
Dominik Laskowski6eab42d2021-09-13 14:34:13 -0700170 isApproxEqual(desiredRefreshRate, other.desiredRefreshRate) &&
Marin Shalamanovbe97cfa2020-12-01 16:02:07 +0100171 seamlessness == other.seamlessness && weight == other.weight &&
Ady Abrahamaae5ed52020-06-26 09:32:43 -0700172 focused == other.focused;
Ady Abraham8a82ba62020-01-17 12:43:17 -0800173 }
174
175 bool operator!=(const LayerRequirement& other) const { return !(*this == other); }
176 };
177
Ady Abrahamdfd62162020-06-10 16:11:56 -0700178 // Global state describing signals that affect refresh rate choice.
179 struct GlobalSignals {
180 // Whether the user touched the screen recently. Used to apply touch boost.
181 bool touch = false;
182 // True if the system hasn't seen any buffers posted to layers recently.
183 bool idle = false;
ramindani38c84982022-08-29 18:02:57 +0000184 // Whether the display is about to be powered on, or has been in PowerMode::ON
185 // within the timeout of DisplayPowerTimer.
186 bool powerOnImminent = false;
Marin Shalamanov4c7831e2021-06-08 20:44:06 +0200187
Dominik Laskowski6eab42d2021-09-13 14:34:13 -0700188 bool operator==(GlobalSignals other) const {
ramindani38c84982022-08-29 18:02:57 +0000189 return touch == other.touch && idle == other.idle &&
190 powerOnImminent == other.powerOnImminent;
Marin Shalamanov4c7831e2021-06-08 20:44:06 +0200191 }
Dominik Laskowski530d6bd2022-10-10 16:55:54 -0400192
193 auto toString() const {
194 return ftl::Concat("{touch=", touch, ", idle=", idle,
195 ", powerOnImminent=", powerOnImminent, '}');
196 }
Ady Abrahamdfd62162020-06-10 16:11:56 -0700197 };
198
Dominik Laskowski530d6bd2022-10-10 16:55:54 -0400199 struct ScoredRefreshRate {
200 DisplayModePtr modePtr;
201 float score = 0.0f;
202
203 bool operator==(const ScoredRefreshRate& other) const {
204 return modePtr == other.modePtr && score == other.score;
205 }
206
207 static bool scoresEqual(float lhs, float rhs) {
208 constexpr float kEpsilon = 0.0001f;
209 return std::abs(lhs - rhs) <= kEpsilon;
210 }
211
212 struct DescendingScore {
213 bool operator()(const ScoredRefreshRate& lhs, const ScoredRefreshRate& rhs) const {
214 return lhs.score > rhs.score && !scoresEqual(lhs.score, rhs.score);
215 }
216 };
217 };
218
219 using RefreshRateRanking = std::vector<ScoredRefreshRate>;
220
221 struct RankedRefreshRates {
222 RefreshRateRanking ranking; // Ordered by descending score.
223 GlobalSignals consideredSignals;
224
225 bool operator==(const RankedRefreshRates& other) const {
226 return ranking == other.ranking && consideredSignals == other.consideredSignals;
227 }
228 };
229
230 RankedRefreshRates getRankedRefreshRates(const std::vector<LayerRequirement>&,
231 GlobalSignals) const EXCLUDES(mLock);
Ana Krulecb43429d2019-01-09 14:28:51 -0800232
Marin Shalamanoveadf2e72020-12-10 15:35:28 +0100233 FpsRange getSupportedRefreshRateRange() const EXCLUDES(mLock) {
234 std::lock_guard lock(mLock);
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800235 return {mMinRefreshRateModeIt->second->getFps(), mMaxRefreshRateModeIt->second->getFps()};
Marin Shalamanoveadf2e72020-12-10 15:35:28 +0100236 }
Steven Thomas2bbaabe2019-08-28 16:08:35 -0700237
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100238 std::optional<Fps> onKernelTimerChanged(std::optional<DisplayModeId> desiredActiveModeId,
Marin Shalamanoveadf2e72020-12-10 15:35:28 +0100239 bool timerExpired) const EXCLUDES(mLock);
Steven Thomas2bbaabe2019-08-28 16:08:35 -0700240
Dominik Laskowskif8734e02022-08-26 09:06:59 -0700241 void setActiveModeId(DisplayModeId) EXCLUDES(mLock) REQUIRES(kMainThreadContext);
242
243 // See mActiveModeIt for thread safety.
244 DisplayModePtr getActiveModePtr() const EXCLUDES(mLock);
245 const DisplayMode& getActiveMode() const REQUIRES(kMainThreadContext);
Dominik Laskowski22488f62019-03-28 09:53:04 -0700246
Ady Abrahamb1b9d412020-06-01 19:53:52 -0700247 // Returns a known frame rate that is the closest to frameRate
Marin Shalamanove8a663d2020-11-24 17:48:00 +0100248 Fps findClosestKnownFrameRate(Fps frameRate) const;
Ady Abrahamb1b9d412020-06-01 19:53:52 -0700249
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800250 enum class KernelIdleTimerController { Sysprop, HwcApi, ftl_last = HwcApi };
ramindani32cf0602022-03-02 02:30:29 +0000251
rnlee3bd610662021-06-23 16:27:57 -0700252 // Configuration flags.
253 struct Config {
254 bool enableFrameRateOverride = false;
255
256 // Specifies the upper refresh rate threshold (inclusive) for layer vote types of multiple
257 // or heuristic, such that refresh rates higher than this value will not be voted for. 0 if
258 // no threshold is set.
259 int frameRateMultipleThreshold = 0;
Ady Abraham9a2ea342021-09-03 17:32:34 -0700260
Ady Abraham6d885932021-09-03 18:05:48 -0700261 // The Idle Timer timeout. 0 timeout means no idle timer.
ramindani32cf0602022-03-02 02:30:29 +0000262 std::chrono::milliseconds idleTimerTimeout = 0ms;
Ady Abraham6d885932021-09-03 18:05:48 -0700263
ramindani32cf0602022-03-02 02:30:29 +0000264 // The controller representing how the kernel idle timer will be configured
265 // either on the HWC api or sysprop.
266 std::optional<KernelIdleTimerController> kernelIdleTimerController;
rnlee3bd610662021-06-23 16:27:57 -0700267 };
268
Dominik Laskowskid82e0f02022-10-26 15:23:04 -0400269 RefreshRateSelector(DisplayModes, DisplayModeId activeModeId,
270 Config config = {.enableFrameRateOverride = false,
271 .frameRateMultipleThreshold = 0,
272 .idleTimerTimeout = 0ms,
273 .kernelIdleTimerController = {}});
Ana Krulec4593b692019-01-11 22:07:25 -0800274
Dominik Laskowskid82e0f02022-10-26 15:23:04 -0400275 RefreshRateSelector(const RefreshRateSelector&) = delete;
276 RefreshRateSelector& operator=(const RefreshRateSelector&) = delete;
Dominik Laskowski0c252702021-12-20 20:32:09 -0800277
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100278 // Returns whether switching modes (refresh rate or resolution) is possible.
279 // TODO(b/158780872): Consider HAL support, and skip frame rate detection if the modes only
Dominik Laskowski983f2b52020-06-25 16:54:06 -0700280 // differ in resolution.
Marin Shalamanoveadf2e72020-12-10 15:35:28 +0100281 bool canSwitch() const EXCLUDES(mLock) {
282 std::lock_guard lock(mLock);
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800283 return mDisplayModes.size() > 1;
Marin Shalamanoveadf2e72020-12-10 15:35:28 +0100284 }
Dominik Laskowski983f2b52020-06-25 16:54:06 -0700285
TreeHugger Robot758ab612021-06-22 19:17:29 +0000286 // Class to enumerate options around toggling the kernel timer on and off.
Ana Krulecb9afd792020-06-11 13:16:15 -0700287 enum class KernelIdleTimerAction {
Dominik Laskowskid82e0f02022-10-26 15:23:04 -0400288 TurnOff, // Turn off the idle timer.
289 TurnOn // Turn on the idle timer.
Ana Krulecb9afd792020-06-11 13:16:15 -0700290 };
ramindani32cf0602022-03-02 02:30:29 +0000291
Ana Krulecb9afd792020-06-11 13:16:15 -0700292 // Checks whether kernel idle timer should be active depending the policy decisions around
293 // refresh rates.
294 KernelIdleTimerAction getIdleTimerAction() const;
295
Andy Yu2ae6b6b2021-11-18 14:51:06 -0800296 bool supportsFrameRateOverrideByContent() const { return mSupportsFrameRateOverrideByContent; }
Ady Abraham64c2fc02020-12-29 12:07:50 -0800297
Ady Abrahamcc315492022-02-17 17:06:39 -0800298 // Return the display refresh rate divisor to match the layer
Ady Abraham5cc2e262021-03-25 13:09:17 -0700299 // frame rate, or 0 if the display refresh rate is not a multiple of the
300 // layer refresh rate.
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800301 static int getFrameRateDivisor(Fps displayRefreshRate, Fps layerFrameRate);
Ady Abraham62a0be22020-12-08 16:54:10 -0800302
Marin Shalamanov15a0fc62021-08-16 18:20:21 +0200303 // Returns if the provided frame rates have a ratio t*1000/1001 or t*1001/1000
304 // for an integer t.
305 static bool isFractionalPairOrMultiple(Fps, Fps);
306
Ady Abraham62a0be22020-12-08 16:54:10 -0800307 using UidToFrameRateOverride = std::map<uid_t, Fps>;
Dominik Laskowski6eab42d2021-09-13 14:34:13 -0700308
Ady Abrahamdd5bfa92021-01-07 17:56:08 -0800309 // Returns the frame rate override for each uid.
Dominik Laskowski6eab42d2021-09-13 14:34:13 -0700310 UidToFrameRateOverride getFrameRateOverrides(const std::vector<LayerRequirement>&,
311 Fps displayFrameRate, GlobalSignals) const
Ady Abrahamdd5bfa92021-01-07 17:56:08 -0800312 EXCLUDES(mLock);
Ady Abraham0bb6a472020-10-12 10:22:13 -0700313
ramindani32cf0602022-03-02 02:30:29 +0000314 std::optional<KernelIdleTimerController> kernelIdleTimerController() {
315 return mConfig.kernelIdleTimerController;
316 }
Ady Abraham9a2ea342021-09-03 17:32:34 -0700317
Dominik Laskowski83bd7712022-01-07 14:30:53 -0800318 struct IdleTimerCallbacks {
319 struct Callbacks {
320 std::function<void()> onReset;
321 std::function<void()> onExpired;
322 };
323
324 Callbacks platform;
325 Callbacks kernel;
326 };
327
328 void setIdleTimerCallbacks(IdleTimerCallbacks callbacks) EXCLUDES(mIdleTimerCallbacksMutex) {
Ady Abraham9a2ea342021-09-03 17:32:34 -0700329 std::scoped_lock lock(mIdleTimerCallbacksMutex);
Dominik Laskowski83bd7712022-01-07 14:30:53 -0800330 mIdleTimerCallbacks = std::move(callbacks);
331 }
332
333 void clearIdleTimerCallbacks() EXCLUDES(mIdleTimerCallbacksMutex) {
334 std::scoped_lock lock(mIdleTimerCallbacksMutex);
335 mIdleTimerCallbacks.reset();
Ady Abraham11663402021-11-10 19:46:09 -0800336 }
337
338 void startIdleTimer() {
339 if (mIdleTimer) {
340 mIdleTimer->start();
341 }
342 }
343
344 void stopIdleTimer() {
345 if (mIdleTimer) {
346 mIdleTimer->stop();
347 }
Ady Abraham9a2ea342021-09-03 17:32:34 -0700348 }
349
350 void resetIdleTimer(bool kernelOnly) {
351 if (!mIdleTimer) {
352 return;
353 }
ramindani32cf0602022-03-02 02:30:29 +0000354 if (kernelOnly && !mConfig.kernelIdleTimerController.has_value()) {
Ady Abraham9a2ea342021-09-03 17:32:34 -0700355 return;
356 }
357 mIdleTimer->reset();
Dominik Laskowski83bd7712022-01-07 14:30:53 -0800358 }
Ady Abraham9a2ea342021-09-03 17:32:34 -0700359
Dominik Laskowskie70461a2022-08-30 14:42:01 -0700360 void dump(utils::Dumper&) const EXCLUDES(mLock);
Marin Shalamanovba421a82020-11-10 21:49:26 +0100361
ramindani32cf0602022-03-02 02:30:29 +0000362 std::chrono::milliseconds getIdleTimerTimeout();
363
Dominik Laskowski22488f62019-03-28 09:53:04 -0700364private:
Dominik Laskowskid82e0f02022-10-26 15:23:04 -0400365 friend struct TestableRefreshRateSelector;
Ady Abrahamb1b9d412020-06-01 19:53:52 -0700366
Ady Abraham2139f732019-11-13 18:56:40 -0800367 void constructAvailableRefreshRates() REQUIRES(mLock);
368
Dominik Laskowskif8734e02022-08-26 09:06:59 -0700369 // See mActiveModeIt for thread safety.
370 DisplayModeIterator getActiveModeItLocked() const REQUIRES(mLock);
371
Dominik Laskowski530d6bd2022-10-10 16:55:54 -0400372 RankedRefreshRates getRankedRefreshRatesLocked(const std::vector<LayerRequirement>&,
373 GlobalSignals) const REQUIRES(mLock);
Marin Shalamanov4c7831e2021-06-08 20:44:06 +0200374
Ady Abraham4ccdcb42020-02-11 17:34:34 -0800375 // Returns number of display frames and remainder when dividing the layer refresh period by
376 // display refresh period.
377 std::pair<nsecs_t, nsecs_t> getDisplayFrames(nsecs_t layerPeriod, nsecs_t displayPeriod) const;
378
Steven Thomasf734df42020-04-13 21:09:28 -0700379 // Returns the lowest refresh rate according to the current policy. May change at runtime. Only
380 // uses the primary range, not the app request range.
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800381 const DisplayModePtr& getMinRefreshRateByPolicyLocked() const REQUIRES(mLock);
Steven Thomasf734df42020-04-13 21:09:28 -0700382
383 // Returns the highest refresh rate according to the current policy. May change at runtime. Only
384 // uses the primary range, not the app request range.
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800385 const DisplayModePtr& getMaxRefreshRateByPolicyLocked(int anchorGroup) const REQUIRES(mLock);
Marin Shalamanov8cd8a992021-09-14 23:22:49 +0200386
ramindanid72ba162022-09-09 21:33:40 +0000387 struct RefreshRateScoreComparator;
388
389 enum class RefreshRateOrder { Ascending, Descending };
390
ramindanid72ba162022-09-09 21:33:40 +0000391 // Only uses the primary range, not the app request range.
Dominik Laskowski530d6bd2022-10-10 16:55:54 -0400392 RefreshRateRanking rankRefreshRates(std::optional<int> anchorGroupOpt, RefreshRateOrder,
393 std::optional<DisplayModeId> preferredDisplayModeOpt =
394 std::nullopt) const REQUIRES(mLock);
ramindanid72ba162022-09-09 21:33:40 +0000395
Steven Thomasd4071902020-03-24 16:02:53 -0700396 const Policy* getCurrentPolicyLocked() const REQUIRES(mLock);
Marin Shalamanoveadf2e72020-12-10 15:35:28 +0100397 bool isPolicyValidLocked(const Policy& policy) const REQUIRES(mLock);
Steven Thomasd4071902020-03-24 16:02:53 -0700398
ramindanid72ba162022-09-09 21:33:40 +0000399 // Returns the refresh rate score as a ratio to max refresh rate, which has a score of 1.
400 float calculateRefreshRateScoreForFps(Fps refreshRate) const REQUIRES(mLock);
Ady Abraham62a0be22020-12-08 16:54:10 -0800401 // calculates a score for a layer. Used to determine the display refresh rate
402 // and the frame rate override for certains applications.
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800403 float calculateLayerScoreLocked(const LayerRequirement&, Fps refreshRate,
Ady Abraham62a0be22020-12-08 16:54:10 -0800404 bool isSeamlessSwitch) const REQUIRES(mLock);
405
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800406 float calculateNonExactMatchingLayerScoreLocked(const LayerRequirement&, Fps refreshRate) const
407 REQUIRES(mLock);
Ady Abraham05243be2021-09-16 15:58:52 -0700408
Dominik Laskowskif8734e02022-08-26 09:06:59 -0700409 void updateDisplayModes(DisplayModes, DisplayModeId activeModeId) EXCLUDES(mLock)
410 REQUIRES(kMainThreadContext);
Ady Abraham3efa3942021-06-24 19:01:25 -0700411
Ady Abraham9a2ea342021-09-03 17:32:34 -0700412 void initializeIdleTimer();
413
Dominik Laskowski83bd7712022-01-07 14:30:53 -0800414 std::optional<IdleTimerCallbacks::Callbacks> getIdleTimerCallbacks() const
415 REQUIRES(mIdleTimerCallbacksMutex) {
416 if (!mIdleTimerCallbacks) return {};
ramindani32cf0602022-03-02 02:30:29 +0000417 return mConfig.kernelIdleTimerController.has_value() ? mIdleTimerCallbacks->kernel
418 : mIdleTimerCallbacks->platform;
Dominik Laskowski83bd7712022-01-07 14:30:53 -0800419 }
420
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800421 // The display modes of the active display. The DisplayModeIterators below are pointers into
422 // this container, so must be invalidated whenever the DisplayModes change. The Policy below
423 // is also dependent, so must be reset as well.
424 DisplayModes mDisplayModes GUARDED_BY(mLock);
Ady Abraham2139f732019-11-13 18:56:40 -0800425
Dominik Laskowskif8734e02022-08-26 09:06:59 -0700426 // Written under mLock exclusively from kMainThreadContext, so reads from kMainThreadContext
427 // need not be under mLock.
428 DisplayModeIterator mActiveModeIt GUARDED_BY(mLock) GUARDED_BY(kMainThreadContext);
429
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800430 DisplayModeIterator mMinRefreshRateModeIt GUARDED_BY(mLock);
431 DisplayModeIterator mMaxRefreshRateModeIt GUARDED_BY(mLock);
Steven Thomasf734df42020-04-13 21:09:28 -0700432
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800433 // Display modes that satisfy the Policy's ranges, filtered and sorted by refresh rate.
434 std::vector<DisplayModeIterator> mPrimaryRefreshRates GUARDED_BY(mLock);
435 std::vector<DisplayModeIterator> mAppRequestRefreshRates GUARDED_BY(mLock);
Ady Abraham2139f732019-11-13 18:56:40 -0800436
Steven Thomasd4071902020-03-24 16:02:53 -0700437 Policy mDisplayManagerPolicy GUARDED_BY(mLock);
438 std::optional<Policy> mOverridePolicy GUARDED_BY(mLock);
Ady Abraham2139f732019-11-13 18:56:40 -0800439
Dominik Laskowski36dced82022-09-02 09:24:00 -0700440 unsigned mNumModeSwitchesInPolicy GUARDED_BY(kMainThreadContext) = 0;
441
Ady Abraham2139f732019-11-13 18:56:40 -0800442 mutable std::mutex mLock;
Ady Abrahamb1b9d412020-06-01 19:53:52 -0700443
444 // A sorted list of known frame rates that a Heuristic layer will choose
445 // from based on the closest value.
Marin Shalamanove8a663d2020-11-24 17:48:00 +0100446 const std::vector<Fps> mKnownFrameRates;
Ady Abraham64c2fc02020-12-29 12:07:50 -0800447
rnlee3bd610662021-06-23 16:27:57 -0700448 const Config mConfig;
Andy Yu2ae6b6b2021-11-18 14:51:06 -0800449 bool mSupportsFrameRateOverrideByContent;
Marin Shalamanov4c7831e2021-06-08 20:44:06 +0200450
ramindanid72ba162022-09-09 21:33:40 +0000451 struct GetRankedRefreshRatesCache {
Dominik Laskowskia8626ec2021-12-15 18:13:30 -0800452 std::pair<std::vector<LayerRequirement>, GlobalSignals> arguments;
Dominik Laskowski530d6bd2022-10-10 16:55:54 -0400453 RankedRefreshRates result;
Marin Shalamanov4c7831e2021-06-08 20:44:06 +0200454 };
ramindanid72ba162022-09-09 21:33:40 +0000455 mutable std::optional<GetRankedRefreshRatesCache> mGetRankedRefreshRatesCache GUARDED_BY(mLock);
Ady Abraham9a2ea342021-09-03 17:32:34 -0700456
Dominik Laskowski83bd7712022-01-07 14:30:53 -0800457 // Declare mIdleTimer last to ensure its thread joins before the mutex/callbacks are destroyed.
Ady Abraham9a2ea342021-09-03 17:32:34 -0700458 std::mutex mIdleTimerCallbacksMutex;
459 std::optional<IdleTimerCallbacks> mIdleTimerCallbacks GUARDED_BY(mIdleTimerCallbacksMutex);
Dominik Laskowski83bd7712022-01-07 14:30:53 -0800460 // Used to detect (lack of) frame activity.
461 std::optional<scheduler::OneShotTimer> mIdleTimer;
Ana Krulecb43429d2019-01-09 14:28:51 -0800462};
463
Dominik Laskowski98041832019-08-01 18:35:59 -0700464} // namespace android::scheduler