blob: 5052e6e257907cb91d7f334bbd44bcfb25c3695a [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>
Dominik Laskowski98041832019-08-01 18:35:59 -070021#include <type_traits>
Dominik Laskowskia8626ec2021-12-15 18:13:30 -080022#include <utility>
Dominik Laskowski36dced82022-09-02 09:24:00 -070023#include <variant>
Ana Krulecb43429d2019-01-09 14:28:51 -080024
Dominik Laskowski530d6bd2022-10-10 16:55:54 -040025#include <ftl/concat.h>
Dominik Laskowski03cfce82022-11-02 12:13:29 -040026#include <ftl/optional.h>
Ady Abraham68636062022-11-16 17:07:25 -080027#include <ftl/unit.h>
Dominik Laskowskif6b4ba62021-11-09 12:46:10 -080028#include <gui/DisplayEventReceiver.h>
29
30#include <scheduler/Fps.h>
Ady Abraham68636062022-11-16 17:07:25 -080031#include <scheduler/FrameRateMode.h>
Dominik Laskowskif6b4ba62021-11-09 12:46:10 -080032#include <scheduler/Seamlessness.h>
33
Marin Shalamanov3ea1d602020-12-16 19:59:39 +010034#include "DisplayHardware/DisplayMode.h"
Ady Abraham9a2ea342021-09-03 17:32:34 -070035#include "Scheduler/OneShotTimer.h"
Ady Abraham2139f732019-11-13 18:56:40 -080036#include "Scheduler/StrongTyping.h"
Dominik Laskowskif8734e02022-08-26 09:06:59 -070037#include "ThreadContext.h"
Dominik Laskowskie70461a2022-08-30 14:42:01 -070038#include "Utils/Dumper.h"
Ana Krulec4593b692019-01-11 22:07:25 -080039
Dominik Laskowski98041832019-08-01 18:35:59 -070040namespace android::scheduler {
Ady Abrahamabc27602020-04-08 17:20:29 -070041
Ady Abraham4ccdcb42020-02-11 17:34:34 -080042using namespace std::chrono_literals;
Dominik Laskowski98041832019-08-01 18:35:59 -070043
Dominik Laskowski068173d2021-08-11 17:22:59 -070044enum class DisplayModeEvent : unsigned { None = 0b0, Changed = 0b1 };
Dominik Laskowski98041832019-08-01 18:35:59 -070045
Dominik Laskowski068173d2021-08-11 17:22:59 -070046inline DisplayModeEvent operator|(DisplayModeEvent lhs, DisplayModeEvent rhs) {
47 using T = std::underlying_type_t<DisplayModeEvent>;
48 return static_cast<DisplayModeEvent>(static_cast<T>(lhs) | static_cast<T>(rhs));
Dominik Laskowski98041832019-08-01 18:35:59 -070049}
Ana Krulecb43429d2019-01-09 14:28:51 -080050
Ady Abraham62f216c2020-10-13 19:07:23 -070051using FrameRateOverride = DisplayEventReceiver::Event::FrameRateOverride;
52
Dominik Laskowskid82e0f02022-10-26 15:23:04 -040053// Selects the refresh rate of a display by ranking its `DisplayModes` in accordance with
54// the DisplayManager (or override) `Policy`, the `LayerRequirement` of each active layer,
55// and `GlobalSignals`.
56class RefreshRateSelector {
Ana Krulecb43429d2019-01-09 14:28:51 -080057public:
Ady Abraham4ccdcb42020-02-11 17:34:34 -080058 // Margin used when matching refresh rates to the content desired ones.
59 static constexpr nsecs_t MARGIN_FOR_PERIOD_CALCULATION =
rnlee3bd610662021-06-23 16:27:57 -070060 std::chrono::nanoseconds(800us).count();
Ady Abraham4ccdcb42020-02-11 17:34:34 -080061
Ady Abraham68636062022-11-16 17:07:25 -080062 // The lowest Render Frame Rate that will ever be selected
Ady Abraham1d173042023-01-04 23:24:47 +000063 static constexpr Fps kMinSupportedFrameRate = 20_Hz;
Ady Abraham68636062022-11-16 17:07:25 -080064
Dominik Laskowski36dced82022-09-02 09:24:00 -070065 class Policy {
Marin Shalamanov30b0b3c2020-10-13 19:15:06 +020066 static constexpr int kAllowGroupSwitchingDefault = false;
67
68 public:
Marin Shalamanova7fe3042021-01-29 21:02:08 +010069 // The default mode, used to ensure we only initiate display mode switches within the
70 // same mode group as defaultMode's group.
71 DisplayModeId defaultMode;
72 // Whether or not we switch mode groups to get the best frame rate.
Marin Shalamanov30b0b3c2020-10-13 19:15:06 +020073 bool allowGroupSwitching = kAllowGroupSwitchingDefault;
Ady Abraham285f8c12022-10-11 17:12:14 -070074 // The primary refresh rate ranges. @see DisplayModeSpecs.aidl for details.
75 // TODO(b/257072060): use the render range when selecting SF render rate
76 // or the app override frame rate
77 FpsRanges primaryRanges;
78 // The app request refresh rate ranges. @see DisplayModeSpecs.aidl for details.
79 FpsRanges appRequestRanges;
Steven Thomasd4071902020-03-24 16:02:53 -070080
Steven Thomasf734df42020-04-13 21:09:28 -070081 Policy() = default;
Marin Shalamanov30b0b3c2020-10-13 19:15:06 +020082
Ady Abraham285f8c12022-10-11 17:12:14 -070083 Policy(DisplayModeId defaultMode, FpsRange range,
84 bool allowGroupSwitching = kAllowGroupSwitchingDefault)
85 : Policy(defaultMode, FpsRanges{range, range}, FpsRanges{range, range},
86 allowGroupSwitching) {}
Marin Shalamanov30b0b3c2020-10-13 19:15:06 +020087
Ady Abraham285f8c12022-10-11 17:12:14 -070088 Policy(DisplayModeId defaultMode, FpsRanges primaryRanges, FpsRanges appRequestRanges,
89 bool allowGroupSwitching = kAllowGroupSwitchingDefault)
Marin Shalamanova7fe3042021-01-29 21:02:08 +010090 : defaultMode(defaultMode),
Marin Shalamanov30b0b3c2020-10-13 19:15:06 +020091 allowGroupSwitching(allowGroupSwitching),
Ady Abraham285f8c12022-10-11 17:12:14 -070092 primaryRanges(primaryRanges),
93 appRequestRanges(appRequestRanges) {}
Steven Thomasf734df42020-04-13 21:09:28 -070094
Steven Thomasd4071902020-03-24 16:02:53 -070095 bool operator==(const Policy& other) const {
Dominik Laskowski953b7fd2022-01-08 19:34:59 -080096 using namespace fps_approx_ops;
Ady Abraham285f8c12022-10-11 17:12:14 -070097 return defaultMode == other.defaultMode && primaryRanges == other.primaryRanges &&
98 appRequestRanges == other.appRequestRanges &&
Steven Thomasd4071902020-03-24 16:02:53 -070099 allowGroupSwitching == other.allowGroupSwitching;
100 }
101
102 bool operator!=(const Policy& other) const { return !(*this == other); }
Marin Shalamanovb6674e72020-11-06 13:05:57 +0100103 std::string toString() const;
Steven Thomasd4071902020-03-24 16:02:53 -0700104 };
105
Dominik Laskowski36dced82022-09-02 09:24:00 -0700106 enum class SetPolicyResult { Invalid, Unchanged, Changed };
Steven Thomasd4071902020-03-24 16:02:53 -0700107
108 // We maintain the display manager policy and the override policy separately. The override
109 // policy is used by CTS tests to get a consistent device state for testing. While the override
110 // policy is set, it takes precedence over the display manager policy. Once the override policy
111 // is cleared, we revert to using the display manager policy.
Dominik Laskowski36dced82022-09-02 09:24:00 -0700112 struct DisplayManagerPolicy : Policy {
113 using Policy::Policy;
114 };
Steven Thomasd4071902020-03-24 16:02:53 -0700115
Dominik Laskowski36dced82022-09-02 09:24:00 -0700116 struct OverridePolicy : Policy {
117 using Policy::Policy;
118 };
119
120 struct NoOverridePolicy {};
121
122 using PolicyVariant = std::variant<DisplayManagerPolicy, OverridePolicy, NoOverridePolicy>;
123
124 SetPolicyResult setPolicy(const PolicyVariant&) EXCLUDES(mLock) REQUIRES(kMainThreadContext);
125
126 void onModeChangeInitiated() REQUIRES(kMainThreadContext) { mNumModeSwitchesInPolicy++; }
127
Steven Thomasd4071902020-03-24 16:02:53 -0700128 // Gets the current policy, which will be the override policy if active, and the display manager
129 // policy otherwise.
130 Policy getCurrentPolicy() const EXCLUDES(mLock);
131 // Gets the display manager policy, regardless of whether an override policy is active.
132 Policy getDisplayManagerPolicy() const EXCLUDES(mLock);
Ana Kruleced3a8cc2019-11-14 00:55:07 +0100133
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100134 // Returns true if mode is allowed by the current policy.
Ady Abrahamace3d052022-11-17 16:25:05 -0800135 bool isModeAllowed(const FrameRateMode&) const EXCLUDES(mLock);
Ady Abraham2139f732019-11-13 18:56:40 -0800136
Ady Abraham8a82ba62020-01-17 12:43:17 -0800137 // Describes the different options the layer voted for refresh rate
138 enum class LayerVoteType {
Ady Abraham71c437d2020-01-31 15:56:57 -0800139 NoVote, // Doesn't care about the refresh rate
140 Min, // Minimal refresh rate available
141 Max, // Maximal refresh rate available
142 Heuristic, // Specific refresh rate that was calculated by platform using a heuristic
143 ExplicitDefault, // Specific refresh rate that was provided by the app with Default
144 // compatibility
Ady Abrahamdd5bfa92021-01-07 17:56:08 -0800145 ExplicitExactOrMultiple, // Specific refresh rate that was provided by the app with
146 // ExactOrMultiple compatibility
147 ExplicitExact, // Specific refresh rate that was provided by the app with
148 // Exact compatibility
149
Dominik Laskowskif5d0ea52021-09-26 17:27:01 -0700150 ftl_last = ExplicitExact
Ady Abraham8a82ba62020-01-17 12:43:17 -0800151 };
152
153 // Captures the layer requirements for a refresh rate. This will be used to determine the
154 // display refresh rate.
155 struct LayerRequirement {
Ady Abrahamaae5ed52020-06-26 09:32:43 -0700156 // Layer's name. Used for debugging purposes.
157 std::string name;
Ady Abraham62a0be22020-12-08 16:54:10 -0800158 // Layer's owner uid
159 uid_t ownerUid = static_cast<uid_t>(-1);
Ady Abrahamaae5ed52020-06-26 09:32:43 -0700160 // Layer vote type.
161 LayerVoteType vote = LayerVoteType::NoVote;
162 // Layer's desired refresh rate, if applicable.
Dominik Laskowski6eab42d2021-09-13 14:34:13 -0700163 Fps desiredRefreshRate;
Marin Shalamanov46084422020-10-13 12:33:42 +0200164 // If a seamless mode switch is required.
Marin Shalamanov53fc11d2020-11-20 14:00:13 +0100165 Seamlessness seamlessness = Seamlessness::Default;
Ady Abrahamaae5ed52020-06-26 09:32:43 -0700166 // Layer's weight in the range of [0, 1]. The higher the weight the more impact this layer
167 // would have on choosing the refresh rate.
168 float weight = 0.0f;
169 // Whether layer is in focus or not based on WindowManager's state
170 bool focused = false;
Ady Abraham8a82ba62020-01-17 12:43:17 -0800171
172 bool operator==(const LayerRequirement& other) const {
173 return name == other.name && vote == other.vote &&
Dominik Laskowski6eab42d2021-09-13 14:34:13 -0700174 isApproxEqual(desiredRefreshRate, other.desiredRefreshRate) &&
Marin Shalamanovbe97cfa2020-12-01 16:02:07 +0100175 seamlessness == other.seamlessness && weight == other.weight &&
Ady Abrahamaae5ed52020-06-26 09:32:43 -0700176 focused == other.focused;
Ady Abraham8a82ba62020-01-17 12:43:17 -0800177 }
178
179 bool operator!=(const LayerRequirement& other) const { return !(*this == other); }
180 };
181
Ady Abrahamdfd62162020-06-10 16:11:56 -0700182 // Global state describing signals that affect refresh rate choice.
183 struct GlobalSignals {
184 // Whether the user touched the screen recently. Used to apply touch boost.
185 bool touch = false;
186 // True if the system hasn't seen any buffers posted to layers recently.
187 bool idle = false;
ramindani38c84982022-08-29 18:02:57 +0000188 // Whether the display is about to be powered on, or has been in PowerMode::ON
189 // within the timeout of DisplayPowerTimer.
190 bool powerOnImminent = false;
Marin Shalamanov4c7831e2021-06-08 20:44:06 +0200191
Dominik Laskowski6eab42d2021-09-13 14:34:13 -0700192 bool operator==(GlobalSignals other) const {
ramindani38c84982022-08-29 18:02:57 +0000193 return touch == other.touch && idle == other.idle &&
194 powerOnImminent == other.powerOnImminent;
Marin Shalamanov4c7831e2021-06-08 20:44:06 +0200195 }
Dominik Laskowski530d6bd2022-10-10 16:55:54 -0400196
197 auto toString() const {
198 return ftl::Concat("{touch=", touch, ", idle=", idle,
199 ", powerOnImminent=", powerOnImminent, '}');
200 }
Ady Abrahamdfd62162020-06-10 16:11:56 -0700201 };
202
Ady Abraham68636062022-11-16 17:07:25 -0800203 struct ScoredFrameRate {
204 FrameRateMode frameRateMode;
Dominik Laskowski530d6bd2022-10-10 16:55:54 -0400205 float score = 0.0f;
206
Ady Abraham68636062022-11-16 17:07:25 -0800207 bool operator==(const ScoredFrameRate& other) const {
208 return frameRateMode == other.frameRateMode && score == other.score;
Dominik Laskowski530d6bd2022-10-10 16:55:54 -0400209 }
210
211 static bool scoresEqual(float lhs, float rhs) {
212 constexpr float kEpsilon = 0.0001f;
213 return std::abs(lhs - rhs) <= kEpsilon;
214 }
215
216 struct DescendingScore {
Ady Abraham68636062022-11-16 17:07:25 -0800217 bool operator()(const ScoredFrameRate& lhs, const ScoredFrameRate& rhs) const {
Dominik Laskowski530d6bd2022-10-10 16:55:54 -0400218 return lhs.score > rhs.score && !scoresEqual(lhs.score, rhs.score);
219 }
220 };
221 };
222
Ady Abraham68636062022-11-16 17:07:25 -0800223 using FrameRateRanking = std::vector<ScoredFrameRate>;
Dominik Laskowski530d6bd2022-10-10 16:55:54 -0400224
Ady Abraham68636062022-11-16 17:07:25 -0800225 struct RankedFrameRates {
226 FrameRateRanking ranking; // Ordered by descending score.
Dominik Laskowski530d6bd2022-10-10 16:55:54 -0400227 GlobalSignals consideredSignals;
228
Ady Abraham68636062022-11-16 17:07:25 -0800229 bool operator==(const RankedFrameRates& other) const {
Dominik Laskowski530d6bd2022-10-10 16:55:54 -0400230 return ranking == other.ranking && consideredSignals == other.consideredSignals;
231 }
232 };
233
Ady Abraham68636062022-11-16 17:07:25 -0800234 RankedFrameRates getRankedFrameRates(const std::vector<LayerRequirement>&, GlobalSignals) const
235 EXCLUDES(mLock);
Ana Krulecb43429d2019-01-09 14:28:51 -0800236
Marin Shalamanoveadf2e72020-12-10 15:35:28 +0100237 FpsRange getSupportedRefreshRateRange() const EXCLUDES(mLock) {
238 std::lock_guard lock(mLock);
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800239 return {mMinRefreshRateModeIt->second->getFps(), mMaxRefreshRateModeIt->second->getFps()};
Marin Shalamanoveadf2e72020-12-10 15:35:28 +0100240 }
Steven Thomas2bbaabe2019-08-28 16:08:35 -0700241
Ady Abraham0aa373a2022-11-22 13:56:50 -0800242 ftl::Optional<FrameRateMode> onKernelTimerChanged(
243 std::optional<DisplayModeId> desiredActiveModeId, bool timerExpired) const
244 EXCLUDES(mLock);
Steven Thomas2bbaabe2019-08-28 16:08:35 -0700245
Ady Abrahamace3d052022-11-17 16:25:05 -0800246 void setActiveMode(DisplayModeId, Fps renderFrameRate) EXCLUDES(mLock);
Dominik Laskowskif8734e02022-08-26 09:06:59 -0700247
Ady Abrahamace3d052022-11-17 16:25:05 -0800248 // See mActiveModeOpt for thread safety.
249 FrameRateMode getActiveMode() const EXCLUDES(mLock);
Dominik Laskowski22488f62019-03-28 09:53:04 -0700250
Ady Abrahamb1b9d412020-06-01 19:53:52 -0700251 // Returns a known frame rate that is the closest to frameRate
Marin Shalamanove8a663d2020-11-24 17:48:00 +0100252 Fps findClosestKnownFrameRate(Fps frameRate) const;
Ady Abrahamb1b9d412020-06-01 19:53:52 -0700253
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800254 enum class KernelIdleTimerController { Sysprop, HwcApi, ftl_last = HwcApi };
ramindani32cf0602022-03-02 02:30:29 +0000255
rnlee3bd610662021-06-23 16:27:57 -0700256 // Configuration flags.
257 struct Config {
Ady Abraham8ca643a2022-10-18 18:26:47 -0700258 enum class FrameRateOverride {
259 // Do not override the frame rate for an app
260 Disabled,
261
262 // Override the frame rate for an app to a value which is also
263 // a display refresh rate
Ady Abraham68636062022-11-16 17:07:25 -0800264 AppOverrideNativeRefreshRates,
Ady Abraham8ca643a2022-10-18 18:26:47 -0700265
266 // Override the frame rate for an app to any value
Ady Abraham68636062022-11-16 17:07:25 -0800267 AppOverride,
268
269 // Override the frame rate for all apps and all values.
Ady Abraham8ca643a2022-10-18 18:26:47 -0700270 Enabled,
271
272 ftl_last = Enabled
273 };
274 FrameRateOverride enableFrameRateOverride = FrameRateOverride::Disabled;
rnlee3bd610662021-06-23 16:27:57 -0700275
276 // Specifies the upper refresh rate threshold (inclusive) for layer vote types of multiple
277 // or heuristic, such that refresh rates higher than this value will not be voted for. 0 if
278 // no threshold is set.
279 int frameRateMultipleThreshold = 0;
Ady Abraham9a2ea342021-09-03 17:32:34 -0700280
Ady Abraham6d885932021-09-03 18:05:48 -0700281 // The Idle Timer timeout. 0 timeout means no idle timer.
ramindani32cf0602022-03-02 02:30:29 +0000282 std::chrono::milliseconds idleTimerTimeout = 0ms;
Ady Abraham6d885932021-09-03 18:05:48 -0700283
ramindani32cf0602022-03-02 02:30:29 +0000284 // The controller representing how the kernel idle timer will be configured
285 // either on the HWC api or sysprop.
Dominik Laskowski03cfce82022-11-02 12:13:29 -0400286 ftl::Optional<KernelIdleTimerController> kernelIdleTimerController;
rnlee3bd610662021-06-23 16:27:57 -0700287 };
288
Ady Abraham8ca643a2022-10-18 18:26:47 -0700289 RefreshRateSelector(
290 DisplayModes, DisplayModeId activeModeId,
291 Config config = {.enableFrameRateOverride = Config::FrameRateOverride::Disabled,
292 .frameRateMultipleThreshold = 0,
293 .idleTimerTimeout = 0ms,
294 .kernelIdleTimerController = {}});
Ana Krulec4593b692019-01-11 22:07:25 -0800295
Dominik Laskowskid82e0f02022-10-26 15:23:04 -0400296 RefreshRateSelector(const RefreshRateSelector&) = delete;
297 RefreshRateSelector& operator=(const RefreshRateSelector&) = delete;
Dominik Laskowski0c252702021-12-20 20:32:09 -0800298
Dominik Laskowski2b1037b2023-02-11 16:45:36 -0500299 const DisplayModes& displayModes() const { return mDisplayModes; }
300
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100301 // Returns whether switching modes (refresh rate or resolution) is possible.
302 // TODO(b/158780872): Consider HAL support, and skip frame rate detection if the modes only
Ady Abraham68636062022-11-16 17:07:25 -0800303 // differ in resolution. Once Config::FrameRateOverride::Enabled becomes the default,
304 // we can probably remove canSwitch altogether since all devices will be able
305 // to switch to a frame rate divisor.
Marin Shalamanoveadf2e72020-12-10 15:35:28 +0100306 bool canSwitch() const EXCLUDES(mLock) {
307 std::lock_guard lock(mLock);
Ady Abraham68636062022-11-16 17:07:25 -0800308 return mDisplayModes.size() > 1 ||
309 mFrameRateOverrideConfig == Config::FrameRateOverride::Enabled;
Marin Shalamanoveadf2e72020-12-10 15:35:28 +0100310 }
Dominik Laskowski983f2b52020-06-25 16:54:06 -0700311
TreeHugger Robot758ab612021-06-22 19:17:29 +0000312 // Class to enumerate options around toggling the kernel timer on and off.
Ana Krulecb9afd792020-06-11 13:16:15 -0700313 enum class KernelIdleTimerAction {
Dominik Laskowskid82e0f02022-10-26 15:23:04 -0400314 TurnOff, // Turn off the idle timer.
315 TurnOn // Turn on the idle timer.
Ana Krulecb9afd792020-06-11 13:16:15 -0700316 };
ramindani32cf0602022-03-02 02:30:29 +0000317
Ana Krulecb9afd792020-06-11 13:16:15 -0700318 // Checks whether kernel idle timer should be active depending the policy decisions around
319 // refresh rates.
320 KernelIdleTimerAction getIdleTimerAction() const;
321
Ady Abraham68636062022-11-16 17:07:25 -0800322 bool supportsAppFrameRateOverrideByContent() const {
Ady Abraham8ca643a2022-10-18 18:26:47 -0700323 return mFrameRateOverrideConfig != Config::FrameRateOverride::Disabled;
324 }
Ady Abraham64c2fc02020-12-29 12:07:50 -0800325
Ady Abraham68636062022-11-16 17:07:25 -0800326 bool supportsFrameRateOverride() const {
327 return mFrameRateOverrideConfig == Config::FrameRateOverride::Enabled;
328 }
329
Ady Abrahamcc315492022-02-17 17:06:39 -0800330 // Return the display refresh rate divisor to match the layer
Ady Abraham5cc2e262021-03-25 13:09:17 -0700331 // frame rate, or 0 if the display refresh rate is not a multiple of the
332 // layer refresh rate.
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800333 static int getFrameRateDivisor(Fps displayRefreshRate, Fps layerFrameRate);
Ady Abraham62a0be22020-12-08 16:54:10 -0800334
Marin Shalamanov15a0fc62021-08-16 18:20:21 +0200335 // Returns if the provided frame rates have a ratio t*1000/1001 or t*1001/1000
336 // for an integer t.
337 static bool isFractionalPairOrMultiple(Fps, Fps);
338
Ady Abraham62a0be22020-12-08 16:54:10 -0800339 using UidToFrameRateOverride = std::map<uid_t, Fps>;
Dominik Laskowski6eab42d2021-09-13 14:34:13 -0700340
Ady Abrahamdd5bfa92021-01-07 17:56:08 -0800341 // Returns the frame rate override for each uid.
Dominik Laskowski6eab42d2021-09-13 14:34:13 -0700342 UidToFrameRateOverride getFrameRateOverrides(const std::vector<LayerRequirement>&,
343 Fps displayFrameRate, GlobalSignals) const
Ady Abrahamdd5bfa92021-01-07 17:56:08 -0800344 EXCLUDES(mLock);
Ady Abraham0bb6a472020-10-12 10:22:13 -0700345
ramindani32cf0602022-03-02 02:30:29 +0000346 std::optional<KernelIdleTimerController> kernelIdleTimerController() {
347 return mConfig.kernelIdleTimerController;
348 }
Ady Abraham9a2ea342021-09-03 17:32:34 -0700349
Dominik Laskowski83bd7712022-01-07 14:30:53 -0800350 struct IdleTimerCallbacks {
351 struct Callbacks {
352 std::function<void()> onReset;
353 std::function<void()> onExpired;
354 };
355
356 Callbacks platform;
357 Callbacks kernel;
358 };
359
360 void setIdleTimerCallbacks(IdleTimerCallbacks callbacks) EXCLUDES(mIdleTimerCallbacksMutex) {
Ady Abraham9a2ea342021-09-03 17:32:34 -0700361 std::scoped_lock lock(mIdleTimerCallbacksMutex);
Dominik Laskowski83bd7712022-01-07 14:30:53 -0800362 mIdleTimerCallbacks = std::move(callbacks);
363 }
364
365 void clearIdleTimerCallbacks() EXCLUDES(mIdleTimerCallbacksMutex) {
366 std::scoped_lock lock(mIdleTimerCallbacksMutex);
367 mIdleTimerCallbacks.reset();
Ady Abraham11663402021-11-10 19:46:09 -0800368 }
369
370 void startIdleTimer() {
371 if (mIdleTimer) {
372 mIdleTimer->start();
373 }
374 }
375
376 void stopIdleTimer() {
377 if (mIdleTimer) {
378 mIdleTimer->stop();
379 }
Ady Abraham9a2ea342021-09-03 17:32:34 -0700380 }
381
Dominik Laskowski596a2562022-10-28 11:26:12 -0400382 void resetKernelIdleTimer() {
383 if (mIdleTimer && mConfig.kernelIdleTimerController) {
384 mIdleTimer->reset();
Ady Abraham9a2ea342021-09-03 17:32:34 -0700385 }
Dominik Laskowski596a2562022-10-28 11:26:12 -0400386 }
387
388 void resetIdleTimer() {
389 if (mIdleTimer) {
390 mIdleTimer->reset();
Ady Abraham9a2ea342021-09-03 17:32:34 -0700391 }
Dominik Laskowski83bd7712022-01-07 14:30:53 -0800392 }
Ady Abraham9a2ea342021-09-03 17:32:34 -0700393
Dominik Laskowskie70461a2022-08-30 14:42:01 -0700394 void dump(utils::Dumper&) const EXCLUDES(mLock);
Marin Shalamanovba421a82020-11-10 21:49:26 +0100395
ramindani32cf0602022-03-02 02:30:29 +0000396 std::chrono::milliseconds getIdleTimerTimeout();
397
Dominik Laskowski22488f62019-03-28 09:53:04 -0700398private:
Dominik Laskowskid82e0f02022-10-26 15:23:04 -0400399 friend struct TestableRefreshRateSelector;
Ady Abrahamb1b9d412020-06-01 19:53:52 -0700400
Ady Abraham2139f732019-11-13 18:56:40 -0800401 void constructAvailableRefreshRates() REQUIRES(mLock);
402
Ady Abrahamace3d052022-11-17 16:25:05 -0800403 // See mActiveModeOpt for thread safety.
404 const FrameRateMode& getActiveModeLocked() const REQUIRES(mLock);
Dominik Laskowskif8734e02022-08-26 09:06:59 -0700405
Ady Abraham68636062022-11-16 17:07:25 -0800406 RankedFrameRates getRankedFrameRatesLocked(const std::vector<LayerRequirement>& layers,
407 GlobalSignals signals) const REQUIRES(mLock);
Marin Shalamanov4c7831e2021-06-08 20:44:06 +0200408
Ady Abraham4ccdcb42020-02-11 17:34:34 -0800409 // Returns number of display frames and remainder when dividing the layer refresh period by
410 // display refresh period.
411 std::pair<nsecs_t, nsecs_t> getDisplayFrames(nsecs_t layerPeriod, nsecs_t displayPeriod) const;
412
Steven Thomasf734df42020-04-13 21:09:28 -0700413 // Returns the lowest refresh rate according to the current policy. May change at runtime. Only
414 // uses the primary range, not the app request range.
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800415 const DisplayModePtr& getMinRefreshRateByPolicyLocked() const REQUIRES(mLock);
Steven Thomasf734df42020-04-13 21:09:28 -0700416
417 // Returns the highest refresh rate according to the current policy. May change at runtime. Only
418 // uses the primary range, not the app request range.
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800419 const DisplayModePtr& getMaxRefreshRateByPolicyLocked(int anchorGroup) const REQUIRES(mLock);
Marin Shalamanov8cd8a992021-09-14 23:22:49 +0200420
ramindanid72ba162022-09-09 21:33:40 +0000421 struct RefreshRateScoreComparator;
422
Ady Abraham68636062022-11-16 17:07:25 -0800423 enum class RefreshRateOrder {
424 Ascending,
425 Descending,
426
427 ftl_last = Descending
428 };
ramindanid72ba162022-09-09 21:33:40 +0000429
ramindanid72ba162022-09-09 21:33:40 +0000430 // Only uses the primary range, not the app request range.
Ady Abraham68636062022-11-16 17:07:25 -0800431 FrameRateRanking rankFrameRates(
432 std::optional<int> anchorGroupOpt, RefreshRateOrder refreshRateOrder,
433 std::optional<DisplayModeId> preferredDisplayModeOpt = std::nullopt) const
434 REQUIRES(mLock);
ramindanid72ba162022-09-09 21:33:40 +0000435
Steven Thomasd4071902020-03-24 16:02:53 -0700436 const Policy* getCurrentPolicyLocked() const REQUIRES(mLock);
Marin Shalamanoveadf2e72020-12-10 15:35:28 +0100437 bool isPolicyValidLocked(const Policy& policy) const REQUIRES(mLock);
Steven Thomasd4071902020-03-24 16:02:53 -0700438
ramindanid72ba162022-09-09 21:33:40 +0000439 // Returns the refresh rate score as a ratio to max refresh rate, which has a score of 1.
Ady Abraham68636062022-11-16 17:07:25 -0800440 float calculateDistanceScoreFromMax(Fps refreshRate) const REQUIRES(mLock);
Ady Abraham62a0be22020-12-08 16:54:10 -0800441 // calculates a score for a layer. Used to determine the display refresh rate
442 // and the frame rate override for certains applications.
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800443 float calculateLayerScoreLocked(const LayerRequirement&, Fps refreshRate,
Ady Abraham62a0be22020-12-08 16:54:10 -0800444 bool isSeamlessSwitch) const REQUIRES(mLock);
445
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800446 float calculateNonExactMatchingLayerScoreLocked(const LayerRequirement&, Fps refreshRate) const
447 REQUIRES(mLock);
Ady Abraham05243be2021-09-16 15:58:52 -0700448
Dominik Laskowskif8734e02022-08-26 09:06:59 -0700449 void updateDisplayModes(DisplayModes, DisplayModeId activeModeId) EXCLUDES(mLock)
450 REQUIRES(kMainThreadContext);
Ady Abraham3efa3942021-06-24 19:01:25 -0700451
Ady Abraham9a2ea342021-09-03 17:32:34 -0700452 void initializeIdleTimer();
453
Dominik Laskowski83bd7712022-01-07 14:30:53 -0800454 std::optional<IdleTimerCallbacks::Callbacks> getIdleTimerCallbacks() const
455 REQUIRES(mIdleTimerCallbacksMutex) {
456 if (!mIdleTimerCallbacks) return {};
ramindani32cf0602022-03-02 02:30:29 +0000457 return mConfig.kernelIdleTimerController.has_value() ? mIdleTimerCallbacks->kernel
458 : mIdleTimerCallbacks->platform;
Dominik Laskowski83bd7712022-01-07 14:30:53 -0800459 }
460
Ady Abraham68636062022-11-16 17:07:25 -0800461 bool isNativeRefreshRate(Fps fps) const REQUIRES(mLock) {
462 LOG_ALWAYS_FATAL_IF(mConfig.enableFrameRateOverride !=
463 Config::FrameRateOverride::AppOverrideNativeRefreshRates,
464 "should only be called when "
465 "Config::FrameRateOverride::AppOverrideNativeRefreshRates is used");
466 return mAppOverrideNativeRefreshRates.contains(fps);
467 }
468
469 std::vector<FrameRateMode> createFrameRateModes(
470 std::function<bool(const DisplayMode&)>&& filterModes, const FpsRange&) const
471 REQUIRES(mLock);
472
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800473 // The display modes of the active display. The DisplayModeIterators below are pointers into
474 // this container, so must be invalidated whenever the DisplayModes change. The Policy below
475 // is also dependent, so must be reset as well.
476 DisplayModes mDisplayModes GUARDED_BY(mLock);
Ady Abraham2139f732019-11-13 18:56:40 -0800477
Ady Abraham68636062022-11-16 17:07:25 -0800478 // Set of supported display refresh rates for easy lookup
479 // when FrameRateOverride::AppOverrideNativeRefreshRates is in use.
480 ftl::SmallMap<Fps, ftl::Unit, 8, FpsApproxEqual> mAppOverrideNativeRefreshRates;
481
Ady Abrahamace3d052022-11-17 16:25:05 -0800482 ftl::Optional<FrameRateMode> mActiveModeOpt GUARDED_BY(mLock);
Dominik Laskowskif8734e02022-08-26 09:06:59 -0700483
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800484 DisplayModeIterator mMinRefreshRateModeIt GUARDED_BY(mLock);
485 DisplayModeIterator mMaxRefreshRateModeIt GUARDED_BY(mLock);
Steven Thomasf734df42020-04-13 21:09:28 -0700486
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800487 // Display modes that satisfy the Policy's ranges, filtered and sorted by refresh rate.
Ady Abraham68636062022-11-16 17:07:25 -0800488 std::vector<FrameRateMode> mPrimaryFrameRates GUARDED_BY(mLock);
489 std::vector<FrameRateMode> mAppRequestFrameRates GUARDED_BY(mLock);
Ady Abraham2139f732019-11-13 18:56:40 -0800490
Steven Thomasd4071902020-03-24 16:02:53 -0700491 Policy mDisplayManagerPolicy GUARDED_BY(mLock);
492 std::optional<Policy> mOverridePolicy GUARDED_BY(mLock);
Ady Abraham2139f732019-11-13 18:56:40 -0800493
Dominik Laskowski36dced82022-09-02 09:24:00 -0700494 unsigned mNumModeSwitchesInPolicy GUARDED_BY(kMainThreadContext) = 0;
495
Ady Abraham2139f732019-11-13 18:56:40 -0800496 mutable std::mutex mLock;
Ady Abrahamb1b9d412020-06-01 19:53:52 -0700497
498 // A sorted list of known frame rates that a Heuristic layer will choose
499 // from based on the closest value.
Marin Shalamanove8a663d2020-11-24 17:48:00 +0100500 const std::vector<Fps> mKnownFrameRates;
Ady Abraham64c2fc02020-12-29 12:07:50 -0800501
rnlee3bd610662021-06-23 16:27:57 -0700502 const Config mConfig;
Ady Abraham8ca643a2022-10-18 18:26:47 -0700503 Config::FrameRateOverride mFrameRateOverrideConfig;
Marin Shalamanov4c7831e2021-06-08 20:44:06 +0200504
Ady Abraham68636062022-11-16 17:07:25 -0800505 struct GetRankedFrameRatesCache {
Dominik Laskowskia8626ec2021-12-15 18:13:30 -0800506 std::pair<std::vector<LayerRequirement>, GlobalSignals> arguments;
Ady Abraham68636062022-11-16 17:07:25 -0800507 RankedFrameRates result;
Marin Shalamanov4c7831e2021-06-08 20:44:06 +0200508 };
Ady Abraham68636062022-11-16 17:07:25 -0800509 mutable std::optional<GetRankedFrameRatesCache> mGetRankedFrameRatesCache GUARDED_BY(mLock);
Ady Abraham9a2ea342021-09-03 17:32:34 -0700510
Dominik Laskowski83bd7712022-01-07 14:30:53 -0800511 // Declare mIdleTimer last to ensure its thread joins before the mutex/callbacks are destroyed.
Ady Abraham9a2ea342021-09-03 17:32:34 -0700512 std::mutex mIdleTimerCallbacksMutex;
513 std::optional<IdleTimerCallbacks> mIdleTimerCallbacks GUARDED_BY(mIdleTimerCallbacksMutex);
Dominik Laskowski83bd7712022-01-07 14:30:53 -0800514 // Used to detect (lack of) frame activity.
Dominik Laskowski03cfce82022-11-02 12:13:29 -0400515 ftl::Optional<scheduler::OneShotTimer> mIdleTimer;
Ana Krulecb43429d2019-01-09 14:28:51 -0800516};
517
Dominik Laskowski98041832019-08-01 18:35:59 -0700518} // namespace android::scheduler