blob: ade1787c97ca57b1bc77d1cac05d2d01c6b32c1d [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 <android-base/stringprintf.h>
26#include <gui/DisplayEventReceiver.h>
27
28#include <scheduler/Fps.h>
29#include <scheduler/Seamlessness.h>
30
Marin Shalamanov3ea1d602020-12-16 19:59:39 +010031#include "DisplayHardware/DisplayMode.h"
Ana Krulec4593b692019-01-11 22:07:25 -080032#include "DisplayHardware/HWComposer.h"
Ady Abraham9a2ea342021-09-03 17:32:34 -070033#include "Scheduler/OneShotTimer.h"
Ady Abraham2139f732019-11-13 18:56:40 -080034#include "Scheduler/StrongTyping.h"
Ana Krulec4593b692019-01-11 22:07:25 -080035
Dominik Laskowski98041832019-08-01 18:35:59 -070036namespace android::scheduler {
Ady Abrahamabc27602020-04-08 17:20:29 -070037
Ady Abraham4ccdcb42020-02-11 17:34:34 -080038using namespace std::chrono_literals;
Dominik Laskowski98041832019-08-01 18:35:59 -070039
Dominik Laskowski068173d2021-08-11 17:22:59 -070040enum class DisplayModeEvent : unsigned { None = 0b0, Changed = 0b1 };
Dominik Laskowski98041832019-08-01 18:35:59 -070041
Dominik Laskowski068173d2021-08-11 17:22:59 -070042inline DisplayModeEvent operator|(DisplayModeEvent lhs, DisplayModeEvent rhs) {
43 using T = std::underlying_type_t<DisplayModeEvent>;
44 return static_cast<DisplayModeEvent>(static_cast<T>(lhs) | static_cast<T>(rhs));
Dominik Laskowski98041832019-08-01 18:35:59 -070045}
Ana Krulecb43429d2019-01-09 14:28:51 -080046
Ady Abraham62f216c2020-10-13 19:07:23 -070047using FrameRateOverride = DisplayEventReceiver::Event::FrameRateOverride;
48
Ana Krulecb43429d2019-01-09 14:28:51 -080049/**
Ady Abraham1902d072019-03-01 17:18:59 -080050 * This class is used to encapsulate configuration for refresh rates. It holds information
Ana Krulecb43429d2019-01-09 14:28:51 -080051 * about available refresh rates on the device, and the mapping between the numbers and human
52 * readable names.
53 */
54class RefreshRateConfigs {
55public:
Ady Abraham4ccdcb42020-02-11 17:34:34 -080056 // Margin used when matching refresh rates to the content desired ones.
57 static constexpr nsecs_t MARGIN_FOR_PERIOD_CALCULATION =
rnlee3bd610662021-06-23 16:27:57 -070058 std::chrono::nanoseconds(800us).count();
Ady Abraham4ccdcb42020-02-11 17:34:34 -080059
Ady Abrahamabc27602020-04-08 17:20:29 -070060 class RefreshRate {
61 private:
62 // Effectively making the constructor private while allowing
63 // std::make_unique to create the object
64 struct ConstructorTag {
65 explicit ConstructorTag(int) {}
66 };
Ana Krulec72f0d6e2020-01-06 15:24:47 -080067
Ady Abrahamabc27602020-04-08 17:20:29 -070068 public:
Ady Abraham6b7ad652021-06-23 17:34:57 -070069 RefreshRate(DisplayModePtr mode, ConstructorTag) : mode(mode) {}
Ady Abraham2e1dd892020-03-05 13:48:36 -080070
Ady Abraham6b7ad652021-06-23 17:34:57 -070071 DisplayModeId getModeId() const { return mode->getId(); }
Marin Shalamanova7fe3042021-01-29 21:02:08 +010072 nsecs_t getVsyncPeriod() const { return mode->getVsyncPeriod(); }
73 int32_t getModeGroup() const { return mode->getGroup(); }
Ady Abraham6b7ad652021-06-23 17:34:57 -070074 std::string getName() const { return to_string(getFps()); }
75 Fps getFps() const { return mode->getFps(); }
Ady Abraham690f4612021-07-01 23:24:03 -070076 DisplayModePtr getMode() const { return mode; }
Ady Abraham2139f732019-11-13 18:56:40 -080077
Ana Krulec72f0d6e2020-01-06 15:24:47 -080078 // Checks whether the fps of this RefreshRate struct is within a given min and max refresh
Marin Shalamanove8a663d2020-11-24 17:48:00 +010079 // rate passed in. Margin of error is applied to the boundaries for approximation.
Dominik Laskowski6eab42d2021-09-13 14:34:13 -070080 bool inPolicy(Fps minRefreshRate, Fps maxRefreshRate) const;
Ana Krulec72f0d6e2020-01-06 15:24:47 -080081
Dominik Laskowski6eab42d2021-09-13 14:34:13 -070082 bool operator==(const RefreshRate& other) const { return mode == other.mode; }
83 bool operator!=(const RefreshRate& other) const { return !operator==(other); }
Ady Abraham2139f732019-11-13 18:56:40 -080084
Marin Shalamanove8a663d2020-11-24 17:48:00 +010085 bool operator<(const RefreshRate& other) const {
Dominik Laskowski6eab42d2021-09-13 14:34:13 -070086 return isStrictlyLess(getFps(), other.getFps());
Marin Shalamanove8a663d2020-11-24 17:48:00 +010087 }
Ana Krulecb9afd792020-06-11 13:16:15 -070088
Marin Shalamanov46084422020-10-13 12:33:42 +020089 std::string toString() const;
Marin Shalamanov65ce5512021-01-22 20:57:13 +010090 friend std::ostream& operator<<(std::ostream& os, const RefreshRate& refreshRate) {
91 return os << refreshRate.toString();
92 }
Marin Shalamanov46084422020-10-13 12:33:42 +020093
Ady Abrahamabc27602020-04-08 17:20:29 -070094 private:
95 friend RefreshRateConfigs;
Ady Abrahamb1b9d412020-06-01 19:53:52 -070096 friend class RefreshRateConfigsTest;
Ady Abrahamabc27602020-04-08 17:20:29 -070097
Marin Shalamanova7fe3042021-01-29 21:02:08 +010098 DisplayModePtr mode;
Ana Krulecb43429d2019-01-09 14:28:51 -080099 };
100
Ady Abraham2e1dd892020-03-05 13:48:36 -0800101 using AllRefreshRatesMapType =
Marin Shalamanov23c44202020-12-22 19:09:20 +0100102 std::unordered_map<DisplayModeId, std::unique_ptr<const RefreshRate>>;
Ady Abraham2139f732019-11-13 18:56:40 -0800103
Marin Shalamanoveadf2e72020-12-10 15:35:28 +0100104 struct FpsRange {
Dominik Laskowski6eab42d2021-09-13 14:34:13 -0700105 Fps min = Fps::fromValue(0.f);
106 Fps max = Fps::fromValue(std::numeric_limits<float>::max());
Marin Shalamanoveadf2e72020-12-10 15:35:28 +0100107
108 bool operator==(const FpsRange& other) const {
Dominik Laskowski6eab42d2021-09-13 14:34:13 -0700109 return isApproxEqual(min, other.min) && isApproxEqual(max, other.max);
Marin Shalamanoveadf2e72020-12-10 15:35:28 +0100110 }
111
112 bool operator!=(const FpsRange& other) const { return !(*this == other); }
113
114 std::string toString() const {
115 return base::StringPrintf("[%s %s]", to_string(min).c_str(), to_string(max).c_str());
116 }
117 };
118
Steven Thomasd4071902020-03-24 16:02:53 -0700119 struct Policy {
Marin Shalamanov30b0b3c2020-10-13 19:15:06 +0200120 private:
121 static constexpr int kAllowGroupSwitchingDefault = false;
122
123 public:
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100124 // The default mode, used to ensure we only initiate display mode switches within the
125 // same mode group as defaultMode's group.
126 DisplayModeId defaultMode;
127 // Whether or not we switch mode groups to get the best frame rate.
Marin Shalamanov30b0b3c2020-10-13 19:15:06 +0200128 bool allowGroupSwitching = kAllowGroupSwitchingDefault;
Steven Thomasf734df42020-04-13 21:09:28 -0700129 // The primary refresh rate range represents display manager's general guidance on the
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100130 // display modes we'll consider when switching refresh rates. Unless we get an explicit
Steven Thomasf734df42020-04-13 21:09:28 -0700131 // signal from an app, we should stay within this range.
Marin Shalamanoveadf2e72020-12-10 15:35:28 +0100132 FpsRange primaryRange;
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100133 // The app request refresh rate range allows us to consider more display modes when
Steven Thomasf734df42020-04-13 21:09:28 -0700134 // switching refresh rates. Although we should generally stay within the primary range,
135 // specific considerations, such as layer frame rate settings specified via the
136 // setFrameRate() api, may cause us to go outside the primary range. We never go outside the
137 // app request range. The app request range will be greater than or equal to the primary
138 // refresh rate range, never smaller.
Marin Shalamanoveadf2e72020-12-10 15:35:28 +0100139 FpsRange appRequestRange;
Steven Thomasd4071902020-03-24 16:02:53 -0700140
Steven Thomasf734df42020-04-13 21:09:28 -0700141 Policy() = default;
Marin Shalamanov30b0b3c2020-10-13 19:15:06 +0200142
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100143 Policy(DisplayModeId defaultMode, const FpsRange& range)
144 : Policy(defaultMode, kAllowGroupSwitchingDefault, range, range) {}
Marin Shalamanov30b0b3c2020-10-13 19:15:06 +0200145
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100146 Policy(DisplayModeId defaultMode, bool allowGroupSwitching, const FpsRange& range)
147 : Policy(defaultMode, allowGroupSwitching, range, range) {}
Marin Shalamanov30b0b3c2020-10-13 19:15:06 +0200148
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100149 Policy(DisplayModeId defaultMode, const FpsRange& primaryRange,
Marin Shalamanoveadf2e72020-12-10 15:35:28 +0100150 const FpsRange& appRequestRange)
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100151 : Policy(defaultMode, kAllowGroupSwitchingDefault, primaryRange, appRequestRange) {}
Marin Shalamanov30b0b3c2020-10-13 19:15:06 +0200152
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100153 Policy(DisplayModeId defaultMode, bool allowGroupSwitching, const FpsRange& primaryRange,
Marin Shalamanov23c44202020-12-22 19:09:20 +0100154 const FpsRange& appRequestRange)
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100155 : defaultMode(defaultMode),
Marin Shalamanov30b0b3c2020-10-13 19:15:06 +0200156 allowGroupSwitching(allowGroupSwitching),
Steven Thomasf734df42020-04-13 21:09:28 -0700157 primaryRange(primaryRange),
158 appRequestRange(appRequestRange) {}
159
Steven Thomasd4071902020-03-24 16:02:53 -0700160 bool operator==(const Policy& other) const {
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100161 return defaultMode == other.defaultMode && primaryRange == other.primaryRange &&
Steven Thomasf734df42020-04-13 21:09:28 -0700162 appRequestRange == other.appRequestRange &&
Steven Thomasd4071902020-03-24 16:02:53 -0700163 allowGroupSwitching == other.allowGroupSwitching;
164 }
165
166 bool operator!=(const Policy& other) const { return !(*this == other); }
Marin Shalamanovb6674e72020-11-06 13:05:57 +0100167 std::string toString() const;
Steven Thomasd4071902020-03-24 16:02:53 -0700168 };
169
170 // Return code set*Policy() to indicate the current policy is unchanged.
171 static constexpr int CURRENT_POLICY_UNCHANGED = 1;
172
173 // We maintain the display manager policy and the override policy separately. The override
174 // policy is used by CTS tests to get a consistent device state for testing. While the override
175 // policy is set, it takes precedence over the display manager policy. Once the override policy
176 // is cleared, we revert to using the display manager policy.
177
178 // Sets the display manager policy to choose refresh rates. The return value will be:
179 // - A negative value if the policy is invalid or another error occurred.
180 // - NO_ERROR if the policy was successfully updated, and the current policy is different from
181 // what it was before the call.
182 // - CURRENT_POLICY_UNCHANGED if the policy was successfully updated, but the current policy
183 // is the same as it was before the call.
184 status_t setDisplayManagerPolicy(const Policy& policy) EXCLUDES(mLock);
185 // Sets the override policy. See setDisplayManagerPolicy() for the meaning of the return value.
186 status_t setOverridePolicy(const std::optional<Policy>& policy) EXCLUDES(mLock);
187 // Gets the current policy, which will be the override policy if active, and the display manager
188 // policy otherwise.
189 Policy getCurrentPolicy() const EXCLUDES(mLock);
190 // Gets the display manager policy, regardless of whether an override policy is active.
191 Policy getDisplayManagerPolicy() const EXCLUDES(mLock);
Ana Kruleced3a8cc2019-11-14 00:55:07 +0100192
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100193 // Returns true if mode is allowed by the current policy.
194 bool isModeAllowed(DisplayModeId) const EXCLUDES(mLock);
Ady Abraham2139f732019-11-13 18:56:40 -0800195
Ady Abraham8a82ba62020-01-17 12:43:17 -0800196 // Describes the different options the layer voted for refresh rate
197 enum class LayerVoteType {
Ady Abraham71c437d2020-01-31 15:56:57 -0800198 NoVote, // Doesn't care about the refresh rate
199 Min, // Minimal refresh rate available
200 Max, // Maximal refresh rate available
201 Heuristic, // Specific refresh rate that was calculated by platform using a heuristic
202 ExplicitDefault, // Specific refresh rate that was provided by the app with Default
203 // compatibility
Ady Abrahamdd5bfa92021-01-07 17:56:08 -0800204 ExplicitExactOrMultiple, // Specific refresh rate that was provided by the app with
205 // ExactOrMultiple compatibility
206 ExplicitExact, // Specific refresh rate that was provided by the app with
207 // Exact compatibility
208
Dominik Laskowskif5d0ea52021-09-26 17:27:01 -0700209 ftl_last = ExplicitExact
Ady Abraham8a82ba62020-01-17 12:43:17 -0800210 };
211
212 // Captures the layer requirements for a refresh rate. This will be used to determine the
213 // display refresh rate.
214 struct LayerRequirement {
Ady Abrahamaae5ed52020-06-26 09:32:43 -0700215 // Layer's name. Used for debugging purposes.
216 std::string name;
Ady Abraham62a0be22020-12-08 16:54:10 -0800217 // Layer's owner uid
218 uid_t ownerUid = static_cast<uid_t>(-1);
Ady Abrahamaae5ed52020-06-26 09:32:43 -0700219 // Layer vote type.
220 LayerVoteType vote = LayerVoteType::NoVote;
221 // Layer's desired refresh rate, if applicable.
Dominik Laskowski6eab42d2021-09-13 14:34:13 -0700222 Fps desiredRefreshRate;
Marin Shalamanov46084422020-10-13 12:33:42 +0200223 // If a seamless mode switch is required.
Marin Shalamanov53fc11d2020-11-20 14:00:13 +0100224 Seamlessness seamlessness = Seamlessness::Default;
Ady Abrahamaae5ed52020-06-26 09:32:43 -0700225 // Layer's weight in the range of [0, 1]. The higher the weight the more impact this layer
226 // would have on choosing the refresh rate.
227 float weight = 0.0f;
228 // Whether layer is in focus or not based on WindowManager's state
229 bool focused = false;
Ady Abraham8a82ba62020-01-17 12:43:17 -0800230
231 bool operator==(const LayerRequirement& other) const {
232 return name == other.name && vote == other.vote &&
Dominik Laskowski6eab42d2021-09-13 14:34:13 -0700233 isApproxEqual(desiredRefreshRate, other.desiredRefreshRate) &&
Marin Shalamanovbe97cfa2020-12-01 16:02:07 +0100234 seamlessness == other.seamlessness && weight == other.weight &&
Ady Abrahamaae5ed52020-06-26 09:32:43 -0700235 focused == other.focused;
Ady Abraham8a82ba62020-01-17 12:43:17 -0800236 }
237
238 bool operator!=(const LayerRequirement& other) const { return !(*this == other); }
239 };
240
Ady Abrahamdfd62162020-06-10 16:11:56 -0700241 // Global state describing signals that affect refresh rate choice.
242 struct GlobalSignals {
243 // Whether the user touched the screen recently. Used to apply touch boost.
244 bool touch = false;
245 // True if the system hasn't seen any buffers posted to layers recently.
246 bool idle = false;
Marin Shalamanov4c7831e2021-06-08 20:44:06 +0200247
Dominik Laskowski6eab42d2021-09-13 14:34:13 -0700248 bool operator==(GlobalSignals other) const {
Marin Shalamanov4c7831e2021-06-08 20:44:06 +0200249 return touch == other.touch && idle == other.idle;
250 }
Ady Abrahamdfd62162020-06-10 16:11:56 -0700251 };
252
Dominik Laskowskia8626ec2021-12-15 18:13:30 -0800253 // Returns the refresh rate that best fits the given layers, and whether the refresh rate was
254 // chosen based on touch boost and/or idle timer.
255 std::pair<RefreshRate, GlobalSignals> getBestRefreshRate(const std::vector<LayerRequirement>&,
256 GlobalSignals) const EXCLUDES(mLock);
Ana Krulecb43429d2019-01-09 14:28:51 -0800257
Marin Shalamanoveadf2e72020-12-10 15:35:28 +0100258 FpsRange getSupportedRefreshRateRange() const EXCLUDES(mLock) {
259 std::lock_guard lock(mLock);
260 return {mMinSupportedRefreshRate->getFps(), mMaxSupportedRefreshRate->getFps()};
261 }
Steven Thomas2bbaabe2019-08-28 16:08:35 -0700262
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100263 std::optional<Fps> onKernelTimerChanged(std::optional<DisplayModeId> desiredActiveModeId,
Marin Shalamanoveadf2e72020-12-10 15:35:28 +0100264 bool timerExpired) const EXCLUDES(mLock);
Steven Thomas2bbaabe2019-08-28 16:08:35 -0700265
Steven Thomasf734df42020-04-13 21:09:28 -0700266 // Returns the highest refresh rate according to the current policy. May change at runtime. Only
267 // uses the primary range, not the app request range.
Marin Shalamanoveadf2e72020-12-10 15:35:28 +0100268 RefreshRate getMaxRefreshRateByPolicy() const EXCLUDES(mLock);
Ady Abraham2139f732019-11-13 18:56:40 -0800269
270 // Returns the current refresh rate
Marin Shalamanoveadf2e72020-12-10 15:35:28 +0100271 RefreshRate getCurrentRefreshRate() const EXCLUDES(mLock);
Ady Abraham2139f732019-11-13 18:56:40 -0800272
Ana Krulec5d477912020-02-07 12:02:38 -0800273 // Returns the current refresh rate, if allowed. Otherwise the default that is allowed by
274 // the policy.
Marin Shalamanoveadf2e72020-12-10 15:35:28 +0100275 RefreshRate getCurrentRefreshRateByPolicy() const;
Ana Krulec5d477912020-02-07 12:02:38 -0800276
Marin Shalamanov23c44202020-12-22 19:09:20 +0100277 // Returns the refresh rate that corresponds to a DisplayModeId. This may change at
Ady Abraham2139f732019-11-13 18:56:40 -0800278 // runtime.
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100279 // TODO(b/159590486) An invalid mode id may be given here if the dipslay modes have changed.
280 RefreshRate getRefreshRateFromModeId(DisplayModeId modeId) const EXCLUDES(mLock) {
Marin Shalamanoveadf2e72020-12-10 15:35:28 +0100281 std::lock_guard lock(mLock);
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100282 return *mRefreshRates.at(modeId);
Ady Abraham2139f732019-11-13 18:56:40 -0800283 };
284
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100285 // Stores the current modeId the device operates at
286 void setCurrentModeId(DisplayModeId) EXCLUDES(mLock);
Dominik Laskowski22488f62019-03-28 09:53:04 -0700287
Ady Abrahamb1b9d412020-06-01 19:53:52 -0700288 // Returns a known frame rate that is the closest to frameRate
Marin Shalamanove8a663d2020-11-24 17:48:00 +0100289 Fps findClosestKnownFrameRate(Fps frameRate) const;
Ady Abrahamb1b9d412020-06-01 19:53:52 -0700290
rnlee3bd610662021-06-23 16:27:57 -0700291 // Configuration flags.
292 struct Config {
293 bool enableFrameRateOverride = false;
294
295 // Specifies the upper refresh rate threshold (inclusive) for layer vote types of multiple
296 // or heuristic, such that refresh rates higher than this value will not be voted for. 0 if
297 // no threshold is set.
298 int frameRateMultipleThreshold = 0;
Ady Abraham9a2ea342021-09-03 17:32:34 -0700299
Ady Abraham6d885932021-09-03 18:05:48 -0700300 // The Idle Timer timeout. 0 timeout means no idle timer.
301 int32_t idleTimerTimeoutMs = 0;
302
Ady Abraham9a2ea342021-09-03 17:32:34 -0700303 // Whether to use idle timer callbacks that support the kernel timer.
Ady Abraham6d885932021-09-03 18:05:48 -0700304 bool supportKernelIdleTimer = false;
rnlee3bd610662021-06-23 16:27:57 -0700305 };
306
Ady Abraham6d885932021-09-03 18:05:48 -0700307 RefreshRateConfigs(const DisplayModes&, DisplayModeId,
rnlee3bd610662021-06-23 16:27:57 -0700308 Config config = {.enableFrameRateOverride = false,
Ady Abraham9a2ea342021-09-03 17:32:34 -0700309 .frameRateMultipleThreshold = 0,
Ady Abraham6d885932021-09-03 18:05:48 -0700310 .idleTimerTimeoutMs = 0,
311 .supportKernelIdleTimer = false});
Ana Krulec4593b692019-01-11 22:07:25 -0800312
Dominik Laskowski0c252702021-12-20 20:32:09 -0800313 RefreshRateConfigs(const RefreshRateConfigs&) = delete;
314 RefreshRateConfigs& operator=(const RefreshRateConfigs&) = delete;
315
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100316 // Returns whether switching modes (refresh rate or resolution) is possible.
317 // TODO(b/158780872): Consider HAL support, and skip frame rate detection if the modes only
Dominik Laskowski983f2b52020-06-25 16:54:06 -0700318 // differ in resolution.
Marin Shalamanoveadf2e72020-12-10 15:35:28 +0100319 bool canSwitch() const EXCLUDES(mLock) {
320 std::lock_guard lock(mLock);
321 return mRefreshRates.size() > 1;
322 }
Dominik Laskowski983f2b52020-06-25 16:54:06 -0700323
TreeHugger Robot758ab612021-06-22 19:17:29 +0000324 // Class to enumerate options around toggling the kernel timer on and off.
Ana Krulecb9afd792020-06-11 13:16:15 -0700325 enum class KernelIdleTimerAction {
Ana Krulecb9afd792020-06-11 13:16:15 -0700326 TurnOff, // Turn off the idle timer.
327 TurnOn // Turn on the idle timer.
328 };
329 // Checks whether kernel idle timer should be active depending the policy decisions around
330 // refresh rates.
331 KernelIdleTimerAction getIdleTimerAction() const;
332
Andy Yu2ae6b6b2021-11-18 14:51:06 -0800333 bool supportsFrameRateOverrideByContent() const { return mSupportsFrameRateOverrideByContent; }
Ady Abraham64c2fc02020-12-29 12:07:50 -0800334
Ady Abraham5cc2e262021-03-25 13:09:17 -0700335 // Return the display refresh rate divider to match the layer
336 // frame rate, or 0 if the display refresh rate is not a multiple of the
337 // layer refresh rate.
338 static int getFrameRateDivider(Fps displayFrameRate, Fps layerFrameRate);
Ady Abraham62a0be22020-12-08 16:54:10 -0800339
Marin Shalamanov15a0fc62021-08-16 18:20:21 +0200340 // Returns if the provided frame rates have a ratio t*1000/1001 or t*1001/1000
341 // for an integer t.
342 static bool isFractionalPairOrMultiple(Fps, Fps);
343
Ady Abraham62a0be22020-12-08 16:54:10 -0800344 using UidToFrameRateOverride = std::map<uid_t, Fps>;
Dominik Laskowski6eab42d2021-09-13 14:34:13 -0700345
Ady Abrahamdd5bfa92021-01-07 17:56:08 -0800346 // Returns the frame rate override for each uid.
Dominik Laskowski6eab42d2021-09-13 14:34:13 -0700347 UidToFrameRateOverride getFrameRateOverrides(const std::vector<LayerRequirement>&,
348 Fps displayFrameRate, GlobalSignals) const
Ady Abrahamdd5bfa92021-01-07 17:56:08 -0800349 EXCLUDES(mLock);
Ady Abraham0bb6a472020-10-12 10:22:13 -0700350
Ady Abraham6d885932021-09-03 18:05:48 -0700351 bool supportsKernelIdleTimer() const { return mConfig.supportKernelIdleTimer; }
Ady Abraham9a2ea342021-09-03 17:32:34 -0700352
Dominik Laskowski83bd7712022-01-07 14:30:53 -0800353 struct IdleTimerCallbacks {
354 struct Callbacks {
355 std::function<void()> onReset;
356 std::function<void()> onExpired;
357 };
358
359 Callbacks platform;
360 Callbacks kernel;
361 };
362
363 void setIdleTimerCallbacks(IdleTimerCallbacks callbacks) EXCLUDES(mIdleTimerCallbacksMutex) {
Ady Abraham9a2ea342021-09-03 17:32:34 -0700364 std::scoped_lock lock(mIdleTimerCallbacksMutex);
Dominik Laskowski83bd7712022-01-07 14:30:53 -0800365 mIdleTimerCallbacks = std::move(callbacks);
366 }
367
368 void clearIdleTimerCallbacks() EXCLUDES(mIdleTimerCallbacksMutex) {
369 std::scoped_lock lock(mIdleTimerCallbacksMutex);
370 mIdleTimerCallbacks.reset();
Ady Abraham11663402021-11-10 19:46:09 -0800371 }
372
373 void startIdleTimer() {
374 if (mIdleTimer) {
375 mIdleTimer->start();
376 }
377 }
378
379 void stopIdleTimer() {
380 if (mIdleTimer) {
381 mIdleTimer->stop();
382 }
Ady Abraham9a2ea342021-09-03 17:32:34 -0700383 }
384
385 void resetIdleTimer(bool kernelOnly) {
386 if (!mIdleTimer) {
387 return;
388 }
Ady Abraham6d885932021-09-03 18:05:48 -0700389 if (kernelOnly && !mConfig.supportKernelIdleTimer) {
Ady Abraham9a2ea342021-09-03 17:32:34 -0700390 return;
391 }
392 mIdleTimer->reset();
Dominik Laskowski83bd7712022-01-07 14:30:53 -0800393 }
Ady Abraham9a2ea342021-09-03 17:32:34 -0700394
Marin Shalamanovba421a82020-11-10 21:49:26 +0100395 void dump(std::string& result) const EXCLUDES(mLock);
396
Dominik Laskowski22488f62019-03-28 09:53:04 -0700397private:
Dominik Laskowski0c252702021-12-20 20:32:09 -0800398 friend struct TestableRefreshRateConfigs;
Ady Abrahamb1b9d412020-06-01 19:53:52 -0700399
Ady Abraham2139f732019-11-13 18:56:40 -0800400 void constructAvailableRefreshRates() REQUIRES(mLock);
401
Marin Shalamanoveadf2e72020-12-10 15:35:28 +0100402 void getSortedRefreshRateListLocked(
Ady Abraham2139f732019-11-13 18:56:40 -0800403 const std::function<bool(const RefreshRate&)>& shouldAddRefreshRate,
Marin Shalamanoveadf2e72020-12-10 15:35:28 +0100404 std::vector<const RefreshRate*>* outRefreshRates) REQUIRES(mLock);
Ady Abraham2139f732019-11-13 18:56:40 -0800405
Dominik Laskowskia8626ec2021-12-15 18:13:30 -0800406 std::pair<RefreshRate, GlobalSignals> getBestRefreshRateLocked(
407 const std::vector<LayerRequirement>&, GlobalSignals) const REQUIRES(mLock);
Marin Shalamanov4c7831e2021-06-08 20:44:06 +0200408
Ady Abraham34702102020-02-10 14:12:05 -0800409 // Returns the refresh rate with the highest score in the collection specified from begin
410 // to end. If there are more than one with the same highest refresh rate, the first one is
411 // returned.
412 template <typename Iter>
413 const RefreshRate* getBestRefreshRate(Iter begin, Iter end) const;
414
Ady Abraham4ccdcb42020-02-11 17:34:34 -0800415 // Returns number of display frames and remainder when dividing the layer refresh period by
416 // display refresh period.
417 std::pair<nsecs_t, nsecs_t> getDisplayFrames(nsecs_t layerPeriod, nsecs_t displayPeriod) const;
418
Steven Thomasf734df42020-04-13 21:09:28 -0700419 // Returns the lowest refresh rate according to the current policy. May change at runtime. Only
420 // uses the primary range, not the app request range.
421 const RefreshRate& getMinRefreshRateByPolicyLocked() const REQUIRES(mLock);
422
423 // Returns the highest refresh rate according to the current policy. May change at runtime. Only
424 // uses the primary range, not the app request range.
Marin Shalamanov8cd8a992021-09-14 23:22:49 +0200425 const RefreshRate& getMaxRefreshRateByPolicyLocked() const REQUIRES(mLock) {
426 return getMaxRefreshRateByPolicyLocked(mCurrentRefreshRate->getModeGroup());
427 }
428
429 const RefreshRate& getMaxRefreshRateByPolicyLocked(int anchorGroup) const REQUIRES(mLock);
Steven Thomasf734df42020-04-13 21:09:28 -0700430
Ana Krulec3d367c82020-02-25 15:02:01 -0800431 // Returns the current refresh rate, if allowed. Otherwise the default that is allowed by
432 // the policy.
433 const RefreshRate& getCurrentRefreshRateByPolicyLocked() const REQUIRES(mLock);
434
Steven Thomasd4071902020-03-24 16:02:53 -0700435 const Policy* getCurrentPolicyLocked() const REQUIRES(mLock);
Marin Shalamanoveadf2e72020-12-10 15:35:28 +0100436 bool isPolicyValidLocked(const Policy& policy) const REQUIRES(mLock);
Steven Thomasd4071902020-03-24 16:02:53 -0700437
rnlee3bd610662021-06-23 16:27:57 -0700438 // Returns whether the layer is allowed to vote for the given refresh rate.
439 bool isVoteAllowed(const LayerRequirement&, const RefreshRate&) const;
440
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.
443 float calculateLayerScoreLocked(const LayerRequirement&, const RefreshRate&,
444 bool isSeamlessSwitch) const REQUIRES(mLock);
445
Ady Abraham05243be2021-09-16 15:58:52 -0700446 float calculateNonExactMatchingLayerScoreLocked(const LayerRequirement&,
447 const RefreshRate&) const REQUIRES(mLock);
448
Ady Abraham3efa3942021-06-24 19:01:25 -0700449 void updateDisplayModes(const DisplayModes& mode, DisplayModeId currentModeId) EXCLUDES(mLock);
450
Ady Abraham9a2ea342021-09-03 17:32:34 -0700451 void initializeIdleTimer();
452
Dominik Laskowski83bd7712022-01-07 14:30:53 -0800453 std::optional<IdleTimerCallbacks::Callbacks> getIdleTimerCallbacks() const
454 REQUIRES(mIdleTimerCallbacksMutex) {
455 if (!mIdleTimerCallbacks) return {};
456 return mConfig.supportKernelIdleTimer ? mIdleTimerCallbacks->kernel
457 : mIdleTimerCallbacks->platform;
458 }
459
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100460 // The list of refresh rates, indexed by display modes ID. This may change after this
Steven Thomas2bbaabe2019-08-28 16:08:35 -0700461 // object is initialized.
Marin Shalamanoveadf2e72020-12-10 15:35:28 +0100462 AllRefreshRatesMapType mRefreshRates GUARDED_BY(mLock);
Ady Abraham2139f732019-11-13 18:56:40 -0800463
Steven Thomasf734df42020-04-13 21:09:28 -0700464 // The list of refresh rates in the primary range of the current policy, ordered by vsyncPeriod
465 // (the first element is the lowest refresh rate).
466 std::vector<const RefreshRate*> mPrimaryRefreshRates GUARDED_BY(mLock);
467
468 // The list of refresh rates in the app request range of the current policy, ordered by
469 // vsyncPeriod (the first element is the lowest refresh rate).
470 std::vector<const RefreshRate*> mAppRequestRefreshRates GUARDED_BY(mLock);
Ady Abraham2139f732019-11-13 18:56:40 -0800471
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100472 // The current display mode. This will change at runtime. This is set by SurfaceFlinger on
Ady Abraham2139f732019-11-13 18:56:40 -0800473 // the main thread, and read by the Scheduler (and other objects) on other threads.
474 const RefreshRate* mCurrentRefreshRate GUARDED_BY(mLock);
475
Steven Thomasd4071902020-03-24 16:02:53 -0700476 // The policy values will change at runtime. They're set by SurfaceFlinger on the main thread,
477 // and read by the Scheduler (and other objects) on other threads.
478 Policy mDisplayManagerPolicy GUARDED_BY(mLock);
479 std::optional<Policy> mOverridePolicy GUARDED_BY(mLock);
Ady Abraham2139f732019-11-13 18:56:40 -0800480
481 // The min and max refresh rates supported by the device.
Marin Shalamanoveadf2e72020-12-10 15:35:28 +0100482 // This may change at runtime.
483 const RefreshRate* mMinSupportedRefreshRate GUARDED_BY(mLock);
484 const RefreshRate* mMaxSupportedRefreshRate GUARDED_BY(mLock);
Ady Abraham2139f732019-11-13 18:56:40 -0800485
Ady Abraham2139f732019-11-13 18:56:40 -0800486 mutable std::mutex mLock;
Ady Abrahamb1b9d412020-06-01 19:53:52 -0700487
488 // A sorted list of known frame rates that a Heuristic layer will choose
489 // from based on the closest value.
Marin Shalamanove8a663d2020-11-24 17:48:00 +0100490 const std::vector<Fps> mKnownFrameRates;
Ady Abraham64c2fc02020-12-29 12:07:50 -0800491
rnlee3bd610662021-06-23 16:27:57 -0700492 const Config mConfig;
Andy Yu2ae6b6b2021-11-18 14:51:06 -0800493 bool mSupportsFrameRateOverrideByContent;
Marin Shalamanov4c7831e2021-06-08 20:44:06 +0200494
Dominik Laskowskia8626ec2021-12-15 18:13:30 -0800495 struct GetBestRefreshRateCache {
496 std::pair<std::vector<LayerRequirement>, GlobalSignals> arguments;
497 std::pair<RefreshRate, GlobalSignals> result;
Marin Shalamanov4c7831e2021-06-08 20:44:06 +0200498 };
Dominik Laskowskia8626ec2021-12-15 18:13:30 -0800499 mutable std::optional<GetBestRefreshRateCache> mGetBestRefreshRateCache GUARDED_BY(mLock);
Ady Abraham9a2ea342021-09-03 17:32:34 -0700500
Dominik Laskowski83bd7712022-01-07 14:30:53 -0800501 // Declare mIdleTimer last to ensure its thread joins before the mutex/callbacks are destroyed.
Ady Abraham9a2ea342021-09-03 17:32:34 -0700502 std::mutex mIdleTimerCallbacksMutex;
503 std::optional<IdleTimerCallbacks> mIdleTimerCallbacks GUARDED_BY(mIdleTimerCallbacksMutex);
Dominik Laskowski83bd7712022-01-07 14:30:53 -0800504 // Used to detect (lack of) frame activity.
505 std::optional<scheduler::OneShotTimer> mIdleTimer;
Ana Krulecb43429d2019-01-09 14:28:51 -0800506};
507
Dominik Laskowski98041832019-08-01 18:35:59 -0700508} // namespace android::scheduler