blob: a79002e959b123011497736cab45d25e2951f35d [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>
Ana Krulecb43429d2019-01-09 14:28:51 -080024
Dominik Laskowskif6b4ba62021-11-09 12:46:10 -080025#include <gui/DisplayEventReceiver.h>
26
27#include <scheduler/Fps.h>
28#include <scheduler/Seamlessness.h>
29
Marin Shalamanov3ea1d602020-12-16 19:59:39 +010030#include "DisplayHardware/DisplayMode.h"
Ana Krulec4593b692019-01-11 22:07:25 -080031#include "DisplayHardware/HWComposer.h"
Ady Abraham9a2ea342021-09-03 17:32:34 -070032#include "Scheduler/OneShotTimer.h"
Ady Abraham2139f732019-11-13 18:56:40 -080033#include "Scheduler/StrongTyping.h"
Ana Krulec4593b692019-01-11 22:07:25 -080034
Dominik Laskowski98041832019-08-01 18:35:59 -070035namespace android::scheduler {
Ady Abrahamabc27602020-04-08 17:20:29 -070036
Ady Abraham4ccdcb42020-02-11 17:34:34 -080037using namespace std::chrono_literals;
Dominik Laskowski98041832019-08-01 18:35:59 -070038
Dominik Laskowski068173d2021-08-11 17:22:59 -070039enum class DisplayModeEvent : unsigned { None = 0b0, Changed = 0b1 };
Dominik Laskowski98041832019-08-01 18:35:59 -070040
Dominik Laskowski068173d2021-08-11 17:22:59 -070041inline DisplayModeEvent operator|(DisplayModeEvent lhs, DisplayModeEvent rhs) {
42 using T = std::underlying_type_t<DisplayModeEvent>;
43 return static_cast<DisplayModeEvent>(static_cast<T>(lhs) | static_cast<T>(rhs));
Dominik Laskowski98041832019-08-01 18:35:59 -070044}
Ana Krulecb43429d2019-01-09 14:28:51 -080045
Ady Abraham62f216c2020-10-13 19:07:23 -070046using FrameRateOverride = DisplayEventReceiver::Event::FrameRateOverride;
47
Ana Krulecb43429d2019-01-09 14:28:51 -080048/**
Ady Abraham1902d072019-03-01 17:18:59 -080049 * This class is used to encapsulate configuration for refresh rates. It holds information
Ana Krulecb43429d2019-01-09 14:28:51 -080050 * about available refresh rates on the device, and the mapping between the numbers and human
51 * readable names.
52 */
53class RefreshRateConfigs {
54public:
Ady Abraham4ccdcb42020-02-11 17:34:34 -080055 // Margin used when matching refresh rates to the content desired ones.
56 static constexpr nsecs_t MARGIN_FOR_PERIOD_CALCULATION =
rnlee3bd610662021-06-23 16:27:57 -070057 std::chrono::nanoseconds(800us).count();
Ady Abraham4ccdcb42020-02-11 17:34:34 -080058
Steven Thomasd4071902020-03-24 16:02:53 -070059 struct Policy {
Marin Shalamanov30b0b3c2020-10-13 19:15:06 +020060 private:
61 static constexpr int kAllowGroupSwitchingDefault = false;
62
63 public:
Marin Shalamanova7fe3042021-01-29 21:02:08 +010064 // The default mode, used to ensure we only initiate display mode switches within the
65 // same mode group as defaultMode's group.
66 DisplayModeId defaultMode;
67 // Whether or not we switch mode groups to get the best frame rate.
Marin Shalamanov30b0b3c2020-10-13 19:15:06 +020068 bool allowGroupSwitching = kAllowGroupSwitchingDefault;
Steven Thomasf734df42020-04-13 21:09:28 -070069 // The primary refresh rate range represents display manager's general guidance on the
Marin Shalamanova7fe3042021-01-29 21:02:08 +010070 // display modes we'll consider when switching refresh rates. Unless we get an explicit
Steven Thomasf734df42020-04-13 21:09:28 -070071 // signal from an app, we should stay within this range.
Marin Shalamanoveadf2e72020-12-10 15:35:28 +010072 FpsRange primaryRange;
Marin Shalamanova7fe3042021-01-29 21:02:08 +010073 // The app request refresh rate range allows us to consider more display modes when
Steven Thomasf734df42020-04-13 21:09:28 -070074 // switching refresh rates. Although we should generally stay within the primary range,
75 // specific considerations, such as layer frame rate settings specified via the
76 // setFrameRate() api, may cause us to go outside the primary range. We never go outside the
77 // app request range. The app request range will be greater than or equal to the primary
78 // refresh rate range, never smaller.
Marin Shalamanoveadf2e72020-12-10 15:35:28 +010079 FpsRange appRequestRange;
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
Dominik Laskowski953b7fd2022-01-08 19:34:59 -080083 Policy(DisplayModeId defaultMode, FpsRange range)
Marin Shalamanova7fe3042021-01-29 21:02:08 +010084 : Policy(defaultMode, kAllowGroupSwitchingDefault, range, range) {}
Marin Shalamanov30b0b3c2020-10-13 19:15:06 +020085
Dominik Laskowski953b7fd2022-01-08 19:34:59 -080086 Policy(DisplayModeId defaultMode, bool allowGroupSwitching, FpsRange range)
Marin Shalamanova7fe3042021-01-29 21:02:08 +010087 : Policy(defaultMode, allowGroupSwitching, range, range) {}
Marin Shalamanov30b0b3c2020-10-13 19:15:06 +020088
Dominik Laskowski953b7fd2022-01-08 19:34:59 -080089 Policy(DisplayModeId defaultMode, FpsRange primaryRange, FpsRange appRequestRange)
Marin Shalamanova7fe3042021-01-29 21:02:08 +010090 : Policy(defaultMode, kAllowGroupSwitchingDefault, primaryRange, appRequestRange) {}
Marin Shalamanov30b0b3c2020-10-13 19:15:06 +020091
Dominik Laskowski953b7fd2022-01-08 19:34:59 -080092 Policy(DisplayModeId defaultMode, bool allowGroupSwitching, FpsRange primaryRange,
93 FpsRange appRequestRange)
Marin Shalamanova7fe3042021-01-29 21:02:08 +010094 : defaultMode(defaultMode),
Marin Shalamanov30b0b3c2020-10-13 19:15:06 +020095 allowGroupSwitching(allowGroupSwitching),
Steven Thomasf734df42020-04-13 21:09:28 -070096 primaryRange(primaryRange),
97 appRequestRange(appRequestRange) {}
98
Steven Thomasd4071902020-03-24 16:02:53 -070099 bool operator==(const Policy& other) const {
Dominik Laskowski953b7fd2022-01-08 19:34:59 -0800100 using namespace fps_approx_ops;
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100101 return defaultMode == other.defaultMode && primaryRange == other.primaryRange &&
Steven Thomasf734df42020-04-13 21:09:28 -0700102 appRequestRange == other.appRequestRange &&
Steven Thomasd4071902020-03-24 16:02:53 -0700103 allowGroupSwitching == other.allowGroupSwitching;
104 }
105
106 bool operator!=(const Policy& other) const { return !(*this == other); }
Marin Shalamanovb6674e72020-11-06 13:05:57 +0100107 std::string toString() const;
Steven Thomasd4071902020-03-24 16:02:53 -0700108 };
109
110 // Return code set*Policy() to indicate the current policy is unchanged.
111 static constexpr int CURRENT_POLICY_UNCHANGED = 1;
112
113 // We maintain the display manager policy and the override policy separately. The override
114 // policy is used by CTS tests to get a consistent device state for testing. While the override
115 // policy is set, it takes precedence over the display manager policy. Once the override policy
116 // is cleared, we revert to using the display manager policy.
117
118 // Sets the display manager policy to choose refresh rates. The return value will be:
119 // - A negative value if the policy is invalid or another error occurred.
120 // - NO_ERROR if the policy was successfully updated, and the current policy is different from
121 // what it was before the call.
122 // - CURRENT_POLICY_UNCHANGED if the policy was successfully updated, but the current policy
123 // is the same as it was before the call.
124 status_t setDisplayManagerPolicy(const Policy& policy) EXCLUDES(mLock);
125 // Sets the override policy. See setDisplayManagerPolicy() for the meaning of the return value.
126 status_t setOverridePolicy(const std::optional<Policy>& policy) EXCLUDES(mLock);
127 // Gets the current policy, which will be the override policy if active, and the display manager
128 // policy otherwise.
129 Policy getCurrentPolicy() const EXCLUDES(mLock);
130 // Gets the display manager policy, regardless of whether an override policy is active.
131 Policy getDisplayManagerPolicy() const EXCLUDES(mLock);
Ana Kruleced3a8cc2019-11-14 00:55:07 +0100132
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100133 // Returns true if mode is allowed by the current policy.
134 bool isModeAllowed(DisplayModeId) const EXCLUDES(mLock);
Ady Abraham2139f732019-11-13 18:56:40 -0800135
Ady Abraham8a82ba62020-01-17 12:43:17 -0800136 // Describes the different options the layer voted for refresh rate
137 enum class LayerVoteType {
Ady Abraham71c437d2020-01-31 15:56:57 -0800138 NoVote, // Doesn't care about the refresh rate
139 Min, // Minimal refresh rate available
140 Max, // Maximal refresh rate available
141 Heuristic, // Specific refresh rate that was calculated by platform using a heuristic
142 ExplicitDefault, // Specific refresh rate that was provided by the app with Default
143 // compatibility
Ady Abrahamdd5bfa92021-01-07 17:56:08 -0800144 ExplicitExactOrMultiple, // Specific refresh rate that was provided by the app with
145 // ExactOrMultiple compatibility
146 ExplicitExact, // Specific refresh rate that was provided by the app with
147 // Exact compatibility
148
Dominik Laskowskif5d0ea52021-09-26 17:27:01 -0700149 ftl_last = ExplicitExact
Ady Abraham8a82ba62020-01-17 12:43:17 -0800150 };
151
152 // Captures the layer requirements for a refresh rate. This will be used to determine the
153 // display refresh rate.
154 struct LayerRequirement {
Ady Abrahamaae5ed52020-06-26 09:32:43 -0700155 // Layer's name. Used for debugging purposes.
156 std::string name;
Ady Abraham62a0be22020-12-08 16:54:10 -0800157 // Layer's owner uid
158 uid_t ownerUid = static_cast<uid_t>(-1);
Ady Abrahamaae5ed52020-06-26 09:32:43 -0700159 // Layer vote type.
160 LayerVoteType vote = LayerVoteType::NoVote;
161 // Layer's desired refresh rate, if applicable.
Dominik Laskowski6eab42d2021-09-13 14:34:13 -0700162 Fps desiredRefreshRate;
Marin Shalamanov46084422020-10-13 12:33:42 +0200163 // If a seamless mode switch is required.
Marin Shalamanov53fc11d2020-11-20 14:00:13 +0100164 Seamlessness seamlessness = Seamlessness::Default;
Ady Abrahamaae5ed52020-06-26 09:32:43 -0700165 // Layer's weight in the range of [0, 1]. The higher the weight the more impact this layer
166 // would have on choosing the refresh rate.
167 float weight = 0.0f;
168 // Whether layer is in focus or not based on WindowManager's state
169 bool focused = false;
Ady Abraham8a82ba62020-01-17 12:43:17 -0800170
171 bool operator==(const LayerRequirement& other) const {
172 return name == other.name && vote == other.vote &&
Dominik Laskowski6eab42d2021-09-13 14:34:13 -0700173 isApproxEqual(desiredRefreshRate, other.desiredRefreshRate) &&
Marin Shalamanovbe97cfa2020-12-01 16:02:07 +0100174 seamlessness == other.seamlessness && weight == other.weight &&
Ady Abrahamaae5ed52020-06-26 09:32:43 -0700175 focused == other.focused;
Ady Abraham8a82ba62020-01-17 12:43:17 -0800176 }
177
178 bool operator!=(const LayerRequirement& other) const { return !(*this == other); }
179 };
180
Ady Abrahamdfd62162020-06-10 16:11:56 -0700181 // Global state describing signals that affect refresh rate choice.
182 struct GlobalSignals {
183 // Whether the user touched the screen recently. Used to apply touch boost.
184 bool touch = false;
185 // True if the system hasn't seen any buffers posted to layers recently.
186 bool idle = false;
Marin Shalamanov4c7831e2021-06-08 20:44:06 +0200187
Dominik Laskowski6eab42d2021-09-13 14:34:13 -0700188 bool operator==(GlobalSignals other) const {
Marin Shalamanov4c7831e2021-06-08 20:44:06 +0200189 return touch == other.touch && idle == other.idle;
190 }
Ady Abrahamdfd62162020-06-10 16:11:56 -0700191 };
192
Dominik Laskowskia8626ec2021-12-15 18:13:30 -0800193 // Returns the refresh rate that best fits the given layers, and whether the refresh rate was
194 // chosen based on touch boost and/or idle timer.
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800195 std::pair<DisplayModePtr, GlobalSignals> getBestRefreshRate(
196 const std::vector<LayerRequirement>&, GlobalSignals) const EXCLUDES(mLock);
Ana Krulecb43429d2019-01-09 14:28:51 -0800197
Marin Shalamanoveadf2e72020-12-10 15:35:28 +0100198 FpsRange getSupportedRefreshRateRange() const EXCLUDES(mLock) {
199 std::lock_guard lock(mLock);
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800200 return {mMinRefreshRateModeIt->second->getFps(), mMaxRefreshRateModeIt->second->getFps()};
Marin Shalamanoveadf2e72020-12-10 15:35:28 +0100201 }
Steven Thomas2bbaabe2019-08-28 16:08:35 -0700202
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100203 std::optional<Fps> onKernelTimerChanged(std::optional<DisplayModeId> desiredActiveModeId,
Marin Shalamanoveadf2e72020-12-10 15:35:28 +0100204 bool timerExpired) const EXCLUDES(mLock);
Steven Thomas2bbaabe2019-08-28 16:08:35 -0700205
Steven Thomasf734df42020-04-13 21:09:28 -0700206 // Returns the highest refresh rate according to the current policy. May change at runtime. Only
207 // uses the primary range, not the app request range.
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800208 DisplayModePtr getMaxRefreshRateByPolicy() const EXCLUDES(mLock);
Ady Abraham2139f732019-11-13 18:56:40 -0800209
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800210 void setActiveModeId(DisplayModeId) EXCLUDES(mLock);
211 DisplayModePtr getActiveMode() const EXCLUDES(mLock);
Dominik Laskowski22488f62019-03-28 09:53:04 -0700212
Ady Abrahamb1b9d412020-06-01 19:53:52 -0700213 // Returns a known frame rate that is the closest to frameRate
Marin Shalamanove8a663d2020-11-24 17:48:00 +0100214 Fps findClosestKnownFrameRate(Fps frameRate) const;
Ady Abrahamb1b9d412020-06-01 19:53:52 -0700215
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800216 enum class KernelIdleTimerController { Sysprop, HwcApi, ftl_last = HwcApi };
ramindani32cf0602022-03-02 02:30:29 +0000217
rnlee3bd610662021-06-23 16:27:57 -0700218 // Configuration flags.
219 struct Config {
220 bool enableFrameRateOverride = false;
221
222 // Specifies the upper refresh rate threshold (inclusive) for layer vote types of multiple
223 // or heuristic, such that refresh rates higher than this value will not be voted for. 0 if
224 // no threshold is set.
225 int frameRateMultipleThreshold = 0;
Ady Abraham9a2ea342021-09-03 17:32:34 -0700226
Ady Abraham6d885932021-09-03 18:05:48 -0700227 // The Idle Timer timeout. 0 timeout means no idle timer.
ramindani32cf0602022-03-02 02:30:29 +0000228 std::chrono::milliseconds idleTimerTimeout = 0ms;
Ady Abraham6d885932021-09-03 18:05:48 -0700229
ramindani32cf0602022-03-02 02:30:29 +0000230 // The controller representing how the kernel idle timer will be configured
231 // either on the HWC api or sysprop.
232 std::optional<KernelIdleTimerController> kernelIdleTimerController;
rnlee3bd610662021-06-23 16:27:57 -0700233 };
234
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800235 RefreshRateConfigs(DisplayModes, DisplayModeId activeModeId,
rnlee3bd610662021-06-23 16:27:57 -0700236 Config config = {.enableFrameRateOverride = false,
Ady Abraham9a2ea342021-09-03 17:32:34 -0700237 .frameRateMultipleThreshold = 0,
ramindani32cf0602022-03-02 02:30:29 +0000238 .idleTimerTimeout = 0ms,
239 .kernelIdleTimerController = {}});
Ana Krulec4593b692019-01-11 22:07:25 -0800240
Dominik Laskowski0c252702021-12-20 20:32:09 -0800241 RefreshRateConfigs(const RefreshRateConfigs&) = delete;
242 RefreshRateConfigs& operator=(const RefreshRateConfigs&) = delete;
243
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100244 // Returns whether switching modes (refresh rate or resolution) is possible.
245 // TODO(b/158780872): Consider HAL support, and skip frame rate detection if the modes only
Dominik Laskowski983f2b52020-06-25 16:54:06 -0700246 // differ in resolution.
Marin Shalamanoveadf2e72020-12-10 15:35:28 +0100247 bool canSwitch() const EXCLUDES(mLock) {
248 std::lock_guard lock(mLock);
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800249 return mDisplayModes.size() > 1;
Marin Shalamanoveadf2e72020-12-10 15:35:28 +0100250 }
Dominik Laskowski983f2b52020-06-25 16:54:06 -0700251
TreeHugger Robot758ab612021-06-22 19:17:29 +0000252 // Class to enumerate options around toggling the kernel timer on and off.
Ana Krulecb9afd792020-06-11 13:16:15 -0700253 enum class KernelIdleTimerAction {
Ana Krulecb9afd792020-06-11 13:16:15 -0700254 TurnOff, // Turn off the idle timer.
255 TurnOn // Turn on the idle timer.
256 };
ramindani32cf0602022-03-02 02:30:29 +0000257
Ana Krulecb9afd792020-06-11 13:16:15 -0700258 // Checks whether kernel idle timer should be active depending the policy decisions around
259 // refresh rates.
260 KernelIdleTimerAction getIdleTimerAction() const;
261
Andy Yu2ae6b6b2021-11-18 14:51:06 -0800262 bool supportsFrameRateOverrideByContent() const { return mSupportsFrameRateOverrideByContent; }
Ady Abraham64c2fc02020-12-29 12:07:50 -0800263
Ady Abrahamcc315492022-02-17 17:06:39 -0800264 // Return the display refresh rate divisor to match the layer
Ady Abraham5cc2e262021-03-25 13:09:17 -0700265 // frame rate, or 0 if the display refresh rate is not a multiple of the
266 // layer refresh rate.
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800267 static int getFrameRateDivisor(Fps displayRefreshRate, Fps layerFrameRate);
Ady Abraham62a0be22020-12-08 16:54:10 -0800268
Marin Shalamanov15a0fc62021-08-16 18:20:21 +0200269 // Returns if the provided frame rates have a ratio t*1000/1001 or t*1001/1000
270 // for an integer t.
271 static bool isFractionalPairOrMultiple(Fps, Fps);
272
Ady Abraham62a0be22020-12-08 16:54:10 -0800273 using UidToFrameRateOverride = std::map<uid_t, Fps>;
Dominik Laskowski6eab42d2021-09-13 14:34:13 -0700274
Ady Abrahamdd5bfa92021-01-07 17:56:08 -0800275 // Returns the frame rate override for each uid.
Dominik Laskowski6eab42d2021-09-13 14:34:13 -0700276 UidToFrameRateOverride getFrameRateOverrides(const std::vector<LayerRequirement>&,
277 Fps displayFrameRate, GlobalSignals) const
Ady Abrahamdd5bfa92021-01-07 17:56:08 -0800278 EXCLUDES(mLock);
Ady Abraham0bb6a472020-10-12 10:22:13 -0700279
ramindani32cf0602022-03-02 02:30:29 +0000280 std::optional<KernelIdleTimerController> kernelIdleTimerController() {
281 return mConfig.kernelIdleTimerController;
282 }
Ady Abraham9a2ea342021-09-03 17:32:34 -0700283
Dominik Laskowski83bd7712022-01-07 14:30:53 -0800284 struct IdleTimerCallbacks {
285 struct Callbacks {
286 std::function<void()> onReset;
287 std::function<void()> onExpired;
288 };
289
290 Callbacks platform;
291 Callbacks kernel;
292 };
293
294 void setIdleTimerCallbacks(IdleTimerCallbacks callbacks) EXCLUDES(mIdleTimerCallbacksMutex) {
Ady Abraham9a2ea342021-09-03 17:32:34 -0700295 std::scoped_lock lock(mIdleTimerCallbacksMutex);
Dominik Laskowski83bd7712022-01-07 14:30:53 -0800296 mIdleTimerCallbacks = std::move(callbacks);
297 }
298
299 void clearIdleTimerCallbacks() EXCLUDES(mIdleTimerCallbacksMutex) {
300 std::scoped_lock lock(mIdleTimerCallbacksMutex);
301 mIdleTimerCallbacks.reset();
Ady Abraham11663402021-11-10 19:46:09 -0800302 }
303
304 void startIdleTimer() {
305 if (mIdleTimer) {
306 mIdleTimer->start();
307 }
308 }
309
310 void stopIdleTimer() {
311 if (mIdleTimer) {
312 mIdleTimer->stop();
313 }
Ady Abraham9a2ea342021-09-03 17:32:34 -0700314 }
315
316 void resetIdleTimer(bool kernelOnly) {
317 if (!mIdleTimer) {
318 return;
319 }
ramindani32cf0602022-03-02 02:30:29 +0000320 if (kernelOnly && !mConfig.kernelIdleTimerController.has_value()) {
Ady Abraham9a2ea342021-09-03 17:32:34 -0700321 return;
322 }
323 mIdleTimer->reset();
Dominik Laskowski83bd7712022-01-07 14:30:53 -0800324 }
Ady Abraham9a2ea342021-09-03 17:32:34 -0700325
Marin Shalamanovba421a82020-11-10 21:49:26 +0100326 void dump(std::string& result) const EXCLUDES(mLock);
327
ramindani32cf0602022-03-02 02:30:29 +0000328 std::chrono::milliseconds getIdleTimerTimeout();
329
Dominik Laskowski22488f62019-03-28 09:53:04 -0700330private:
Dominik Laskowski0c252702021-12-20 20:32:09 -0800331 friend struct TestableRefreshRateConfigs;
Ady Abrahamb1b9d412020-06-01 19:53:52 -0700332
Ady Abraham2139f732019-11-13 18:56:40 -0800333 void constructAvailableRefreshRates() REQUIRES(mLock);
334
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800335 std::pair<DisplayModePtr, GlobalSignals> getBestRefreshRateLocked(
Dominik Laskowskia8626ec2021-12-15 18:13:30 -0800336 const std::vector<LayerRequirement>&, GlobalSignals) const REQUIRES(mLock);
Marin Shalamanov4c7831e2021-06-08 20:44:06 +0200337
Ady Abraham4ccdcb42020-02-11 17:34:34 -0800338 // Returns number of display frames and remainder when dividing the layer refresh period by
339 // display refresh period.
340 std::pair<nsecs_t, nsecs_t> getDisplayFrames(nsecs_t layerPeriod, nsecs_t displayPeriod) const;
341
Steven Thomasf734df42020-04-13 21:09:28 -0700342 // Returns the lowest refresh rate according to the current policy. May change at runtime. Only
343 // uses the primary range, not the app request range.
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800344 const DisplayModePtr& getMinRefreshRateByPolicyLocked() const REQUIRES(mLock);
Steven Thomasf734df42020-04-13 21:09:28 -0700345
346 // Returns the highest refresh rate according to the current policy. May change at runtime. Only
347 // uses the primary range, not the app request range.
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800348 const DisplayModePtr& getMaxRefreshRateByPolicyLocked(int anchorGroup) const REQUIRES(mLock);
349 const DisplayModePtr& getMaxRefreshRateByPolicyLocked() const REQUIRES(mLock) {
350 return getMaxRefreshRateByPolicyLocked(mActiveModeIt->second->getGroup());
Marin Shalamanov8cd8a992021-09-14 23:22:49 +0200351 }
352
Steven Thomasd4071902020-03-24 16:02:53 -0700353 const Policy* getCurrentPolicyLocked() const REQUIRES(mLock);
Marin Shalamanoveadf2e72020-12-10 15:35:28 +0100354 bool isPolicyValidLocked(const Policy& policy) const REQUIRES(mLock);
Steven Thomasd4071902020-03-24 16:02:53 -0700355
Ady Abraham62a0be22020-12-08 16:54:10 -0800356 // calculates a score for a layer. Used to determine the display refresh rate
357 // and the frame rate override for certains applications.
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800358 float calculateLayerScoreLocked(const LayerRequirement&, Fps refreshRate,
Ady Abraham62a0be22020-12-08 16:54:10 -0800359 bool isSeamlessSwitch) const REQUIRES(mLock);
360
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800361 float calculateNonExactMatchingLayerScoreLocked(const LayerRequirement&, Fps refreshRate) const
362 REQUIRES(mLock);
Ady Abraham05243be2021-09-16 15:58:52 -0700363
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800364 void updateDisplayModes(DisplayModes, DisplayModeId activeModeId) EXCLUDES(mLock);
Ady Abraham3efa3942021-06-24 19:01:25 -0700365
Ady Abraham9a2ea342021-09-03 17:32:34 -0700366 void initializeIdleTimer();
367
Dominik Laskowski83bd7712022-01-07 14:30:53 -0800368 std::optional<IdleTimerCallbacks::Callbacks> getIdleTimerCallbacks() const
369 REQUIRES(mIdleTimerCallbacksMutex) {
370 if (!mIdleTimerCallbacks) return {};
ramindani32cf0602022-03-02 02:30:29 +0000371 return mConfig.kernelIdleTimerController.has_value() ? mIdleTimerCallbacks->kernel
372 : mIdleTimerCallbacks->platform;
Dominik Laskowski83bd7712022-01-07 14:30:53 -0800373 }
374
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800375 // The display modes of the active display. The DisplayModeIterators below are pointers into
376 // this container, so must be invalidated whenever the DisplayModes change. The Policy below
377 // is also dependent, so must be reset as well.
378 DisplayModes mDisplayModes GUARDED_BY(mLock);
Ady Abraham2139f732019-11-13 18:56:40 -0800379
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800380 DisplayModeIterator mActiveModeIt GUARDED_BY(mLock);
381 DisplayModeIterator mMinRefreshRateModeIt GUARDED_BY(mLock);
382 DisplayModeIterator mMaxRefreshRateModeIt GUARDED_BY(mLock);
Steven Thomasf734df42020-04-13 21:09:28 -0700383
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800384 // Display modes that satisfy the Policy's ranges, filtered and sorted by refresh rate.
385 std::vector<DisplayModeIterator> mPrimaryRefreshRates GUARDED_BY(mLock);
386 std::vector<DisplayModeIterator> mAppRequestRefreshRates GUARDED_BY(mLock);
Ady Abraham2139f732019-11-13 18:56:40 -0800387
Steven Thomasd4071902020-03-24 16:02:53 -0700388 Policy mDisplayManagerPolicy GUARDED_BY(mLock);
389 std::optional<Policy> mOverridePolicy GUARDED_BY(mLock);
Ady Abraham2139f732019-11-13 18:56:40 -0800390
Ady Abraham2139f732019-11-13 18:56:40 -0800391 mutable std::mutex mLock;
Ady Abrahamb1b9d412020-06-01 19:53:52 -0700392
393 // A sorted list of known frame rates that a Heuristic layer will choose
394 // from based on the closest value.
Marin Shalamanove8a663d2020-11-24 17:48:00 +0100395 const std::vector<Fps> mKnownFrameRates;
Ady Abraham64c2fc02020-12-29 12:07:50 -0800396
rnlee3bd610662021-06-23 16:27:57 -0700397 const Config mConfig;
Andy Yu2ae6b6b2021-11-18 14:51:06 -0800398 bool mSupportsFrameRateOverrideByContent;
Marin Shalamanov4c7831e2021-06-08 20:44:06 +0200399
Dominik Laskowskia8626ec2021-12-15 18:13:30 -0800400 struct GetBestRefreshRateCache {
401 std::pair<std::vector<LayerRequirement>, GlobalSignals> arguments;
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800402 std::pair<DisplayModePtr, GlobalSignals> result;
Marin Shalamanov4c7831e2021-06-08 20:44:06 +0200403 };
Dominik Laskowskia8626ec2021-12-15 18:13:30 -0800404 mutable std::optional<GetBestRefreshRateCache> mGetBestRefreshRateCache GUARDED_BY(mLock);
Ady Abraham9a2ea342021-09-03 17:32:34 -0700405
Dominik Laskowski83bd7712022-01-07 14:30:53 -0800406 // Declare mIdleTimer last to ensure its thread joins before the mutex/callbacks are destroyed.
Ady Abraham9a2ea342021-09-03 17:32:34 -0700407 std::mutex mIdleTimerCallbacksMutex;
408 std::optional<IdleTimerCallbacks> mIdleTimerCallbacks GUARDED_BY(mIdleTimerCallbacksMutex);
Dominik Laskowski83bd7712022-01-07 14:30:53 -0800409 // Used to detect (lack of) frame activity.
410 std::optional<scheduler::OneShotTimer> mIdleTimer;
Ana Krulecb43429d2019-01-09 14:28:51 -0800411};
412
Dominik Laskowski98041832019-08-01 18:35:59 -0700413} // namespace android::scheduler