blob: 0c7dc0515c9d54096dcdb081c98b022ff986efc7 [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
Dominik Laskowski98041832019-08-01 18:35:59 -070019#include <android-base/stringprintf.h>
Ady Abraham62f216c2020-10-13 19:07:23 -070020#include <gui/DisplayEventReceiver.h>
Dominik Laskowski98041832019-08-01 18:35:59 -070021
Ana Krulecb43429d2019-01-09 14:28:51 -080022#include <algorithm>
23#include <numeric>
Steven Thomasd4071902020-03-24 16:02:53 -070024#include <optional>
Dominik Laskowski98041832019-08-01 18:35:59 -070025#include <type_traits>
Ana Krulecb43429d2019-01-09 14:28:51 -080026
Marin Shalamanov3ea1d602020-12-16 19:59:39 +010027#include "DisplayHardware/DisplayMode.h"
Ana Krulec4593b692019-01-11 22:07:25 -080028#include "DisplayHardware/HWComposer.h"
Marin Shalamanove8a663d2020-11-24 17:48:00 +010029#include "Fps.h"
Ana Krulec4593b692019-01-11 22:07:25 -080030#include "Scheduler/SchedulerUtils.h"
Marin Shalamanov53fc11d2020-11-20 14:00:13 +010031#include "Scheduler/Seamlessness.h"
Ady Abraham2139f732019-11-13 18:56:40 -080032#include "Scheduler/StrongTyping.h"
Ana Krulec4593b692019-01-11 22:07:25 -080033
Dominik Laskowski98041832019-08-01 18:35:59 -070034namespace android::scheduler {
Ady Abrahamabc27602020-04-08 17:20:29 -070035
Ady Abraham4ccdcb42020-02-11 17:34:34 -080036using namespace std::chrono_literals;
Dominik Laskowski98041832019-08-01 18:35:59 -070037
38enum class RefreshRateConfigEvent : unsigned { None = 0b0, Changed = 0b1 };
39
40inline RefreshRateConfigEvent operator|(RefreshRateConfigEvent lhs, RefreshRateConfigEvent rhs) {
41 using T = std::underlying_type_t<RefreshRateConfigEvent>;
42 return static_cast<RefreshRateConfigEvent>(static_cast<T>(lhs) | static_cast<T>(rhs));
43}
Ana Krulecb43429d2019-01-09 14:28:51 -080044
Ady Abraham62f216c2020-10-13 19:07:23 -070045using FrameRateOverride = DisplayEventReceiver::Event::FrameRateOverride;
46
Ana Krulecb43429d2019-01-09 14:28:51 -080047/**
Ady Abraham1902d072019-03-01 17:18:59 -080048 * This class is used to encapsulate configuration for refresh rates. It holds information
Ana Krulecb43429d2019-01-09 14:28:51 -080049 * about available refresh rates on the device, and the mapping between the numbers and human
50 * readable names.
51 */
52class RefreshRateConfigs {
53public:
Ady Abraham4ccdcb42020-02-11 17:34:34 -080054 // Margin used when matching refresh rates to the content desired ones.
55 static constexpr nsecs_t MARGIN_FOR_PERIOD_CALCULATION =
56 std::chrono::nanoseconds(800us).count();
57
Ady Abrahamabc27602020-04-08 17:20:29 -070058 class RefreshRate {
59 private:
60 // Effectively making the constructor private while allowing
61 // std::make_unique to create the object
62 struct ConstructorTag {
63 explicit ConstructorTag(int) {}
64 };
Ana Krulec72f0d6e2020-01-06 15:24:47 -080065
Ady Abrahamabc27602020-04-08 17:20:29 -070066 public:
Marin Shalamanov23c44202020-12-22 19:09:20 +010067 RefreshRate(DisplayModeId configId, DisplayModePtr config, Fps fps, ConstructorTag)
Marin Shalamanove8a663d2020-11-24 17:48:00 +010068 : configId(configId), hwcConfig(config), fps(std::move(fps)) {}
Ady Abraham2e1dd892020-03-05 13:48:36 -080069
Marin Shalamanov23c44202020-12-22 19:09:20 +010070 DisplayModeId getConfigId() const { return configId; }
Ady Abrahamabc27602020-04-08 17:20:29 -070071 nsecs_t getVsyncPeriod() const { return hwcConfig->getVsyncPeriod(); }
72 int32_t getConfigGroup() const { return hwcConfig->getConfigGroup(); }
Marin Shalamanove8a663d2020-11-24 17:48:00 +010073 std::string getName() const { return to_string(fps); }
74 Fps getFps() const { return fps; }
Ady Abraham2139f732019-11-13 18:56:40 -080075
Ana Krulec72f0d6e2020-01-06 15:24:47 -080076 // Checks whether the fps of this RefreshRate struct is within a given min and max refresh
Marin Shalamanove8a663d2020-11-24 17:48:00 +010077 // rate passed in. Margin of error is applied to the boundaries for approximation.
78 bool inPolicy(Fps minRefreshRate, Fps maxRefreshRate) const {
79 return minRefreshRate.lessThanOrEqualWithMargin(fps) &&
80 fps.lessThanOrEqualWithMargin(maxRefreshRate);
Ana Krulec72f0d6e2020-01-06 15:24:47 -080081 }
82
Ady Abraham2139f732019-11-13 18:56:40 -080083 bool operator!=(const RefreshRate& other) const {
Ady Abrahamabc27602020-04-08 17:20:29 -070084 return configId != other.configId || hwcConfig != other.hwcConfig;
Ady Abraham2139f732019-11-13 18:56:40 -080085 }
86
Marin Shalamanove8a663d2020-11-24 17:48:00 +010087 bool operator<(const RefreshRate& other) const {
88 return getFps().getValue() < other.getFps().getValue();
89 }
Ana Krulecb9afd792020-06-11 13:16:15 -070090
Ady Abraham2139f732019-11-13 18:56:40 -080091 bool operator==(const RefreshRate& other) const { return !(*this != other); }
Ady Abrahamabc27602020-04-08 17:20:29 -070092
Marin Shalamanov46084422020-10-13 12:33:42 +020093 std::string toString() const;
Marin Shalamanov65ce5512021-01-22 20:57:13 +010094 friend std::ostream& operator<<(std::ostream& os, const RefreshRate& refreshRate) {
95 return os << refreshRate.toString();
96 }
Marin Shalamanov46084422020-10-13 12:33:42 +020097
Ady Abrahamabc27602020-04-08 17:20:29 -070098 private:
99 friend RefreshRateConfigs;
Ady Abrahamb1b9d412020-06-01 19:53:52 -0700100 friend class RefreshRateConfigsTest;
Ady Abrahamabc27602020-04-08 17:20:29 -0700101
Ady Abrahamabc27602020-04-08 17:20:29 -0700102 // This config ID corresponds to the position of the config in the vector that is stored
103 // on the device.
Marin Shalamanov23c44202020-12-22 19:09:20 +0100104 const DisplayModeId configId;
Ady Abrahamabc27602020-04-08 17:20:29 -0700105 // The config itself
Marin Shalamanov3ea1d602020-12-16 19:59:39 +0100106 DisplayModePtr hwcConfig;
Ady Abrahamabc27602020-04-08 17:20:29 -0700107 // Refresh rate in frames per second
Marin Shalamanove8a663d2020-11-24 17:48:00 +0100108 const Fps fps{0.0f};
Ana Krulecb43429d2019-01-09 14:28:51 -0800109 };
110
Ady Abraham2e1dd892020-03-05 13:48:36 -0800111 using AllRefreshRatesMapType =
Marin Shalamanov23c44202020-12-22 19:09:20 +0100112 std::unordered_map<DisplayModeId, std::unique_ptr<const RefreshRate>>;
Ady Abraham2139f732019-11-13 18:56:40 -0800113
Marin Shalamanoveadf2e72020-12-10 15:35:28 +0100114 struct FpsRange {
115 Fps min{0.0f};
116 Fps max{std::numeric_limits<float>::max()};
117
118 bool operator==(const FpsRange& other) const {
119 return min.equalsWithMargin(other.min) && max.equalsWithMargin(other.max);
120 }
121
122 bool operator!=(const FpsRange& other) const { return !(*this == other); }
123
124 std::string toString() const {
125 return base::StringPrintf("[%s %s]", to_string(min).c_str(), to_string(max).c_str());
126 }
127 };
128
Steven Thomasd4071902020-03-24 16:02:53 -0700129 struct Policy {
Marin Shalamanov30b0b3c2020-10-13 19:15:06 +0200130 private:
131 static constexpr int kAllowGroupSwitchingDefault = false;
132
133 public:
Steven Thomasd4071902020-03-24 16:02:53 -0700134 // The default config, used to ensure we only initiate display config switches within the
135 // same config group as defaultConfigId's group.
Marin Shalamanov23c44202020-12-22 19:09:20 +0100136 DisplayModeId defaultConfig;
Marin Shalamanov30b0b3c2020-10-13 19:15:06 +0200137 // Whether or not we switch config groups to get the best frame rate.
138 bool allowGroupSwitching = kAllowGroupSwitchingDefault;
Steven Thomasf734df42020-04-13 21:09:28 -0700139 // The primary refresh rate range represents display manager's general guidance on the
140 // display configs we'll consider when switching refresh rates. Unless we get an explicit
141 // signal from an app, we should stay within this range.
Marin Shalamanoveadf2e72020-12-10 15:35:28 +0100142 FpsRange primaryRange;
Steven Thomasf734df42020-04-13 21:09:28 -0700143 // The app request refresh rate range allows us to consider more display configs when
144 // switching refresh rates. Although we should generally stay within the primary range,
145 // specific considerations, such as layer frame rate settings specified via the
146 // setFrameRate() api, may cause us to go outside the primary range. We never go outside the
147 // app request range. The app request range will be greater than or equal to the primary
148 // refresh rate range, never smaller.
Marin Shalamanoveadf2e72020-12-10 15:35:28 +0100149 FpsRange appRequestRange;
Steven Thomasd4071902020-03-24 16:02:53 -0700150
Steven Thomasf734df42020-04-13 21:09:28 -0700151 Policy() = default;
Marin Shalamanov30b0b3c2020-10-13 19:15:06 +0200152
Marin Shalamanov23c44202020-12-22 19:09:20 +0100153 Policy(DisplayModeId defaultConfig, const FpsRange& range)
Marin Shalamanov30b0b3c2020-10-13 19:15:06 +0200154 : Policy(defaultConfig, kAllowGroupSwitchingDefault, range, range) {}
155
Marin Shalamanov23c44202020-12-22 19:09:20 +0100156 Policy(DisplayModeId defaultConfig, bool allowGroupSwitching, const FpsRange& range)
Marin Shalamanov30b0b3c2020-10-13 19:15:06 +0200157 : Policy(defaultConfig, allowGroupSwitching, range, range) {}
158
Marin Shalamanov23c44202020-12-22 19:09:20 +0100159 Policy(DisplayModeId defaultConfig, const FpsRange& primaryRange,
Marin Shalamanoveadf2e72020-12-10 15:35:28 +0100160 const FpsRange& appRequestRange)
Marin Shalamanov30b0b3c2020-10-13 19:15:06 +0200161 : Policy(defaultConfig, kAllowGroupSwitchingDefault, primaryRange, appRequestRange) {}
162
Marin Shalamanov23c44202020-12-22 19:09:20 +0100163 Policy(DisplayModeId defaultConfig, bool allowGroupSwitching, const FpsRange& primaryRange,
164 const FpsRange& appRequestRange)
Steven Thomasf734df42020-04-13 21:09:28 -0700165 : defaultConfig(defaultConfig),
Marin Shalamanov30b0b3c2020-10-13 19:15:06 +0200166 allowGroupSwitching(allowGroupSwitching),
Steven Thomasf734df42020-04-13 21:09:28 -0700167 primaryRange(primaryRange),
168 appRequestRange(appRequestRange) {}
169
Steven Thomasd4071902020-03-24 16:02:53 -0700170 bool operator==(const Policy& other) const {
Steven Thomasf734df42020-04-13 21:09:28 -0700171 return defaultConfig == other.defaultConfig && primaryRange == other.primaryRange &&
172 appRequestRange == other.appRequestRange &&
Steven Thomasd4071902020-03-24 16:02:53 -0700173 allowGroupSwitching == other.allowGroupSwitching;
174 }
175
176 bool operator!=(const Policy& other) const { return !(*this == other); }
Marin Shalamanovb6674e72020-11-06 13:05:57 +0100177 std::string toString() const;
Steven Thomasd4071902020-03-24 16:02:53 -0700178 };
179
180 // Return code set*Policy() to indicate the current policy is unchanged.
181 static constexpr int CURRENT_POLICY_UNCHANGED = 1;
182
183 // We maintain the display manager policy and the override policy separately. The override
184 // policy is used by CTS tests to get a consistent device state for testing. While the override
185 // policy is set, it takes precedence over the display manager policy. Once the override policy
186 // is cleared, we revert to using the display manager policy.
187
188 // Sets the display manager policy to choose refresh rates. The return value will be:
189 // - A negative value if the policy is invalid or another error occurred.
190 // - NO_ERROR if the policy was successfully updated, and the current policy is different from
191 // what it was before the call.
192 // - CURRENT_POLICY_UNCHANGED if the policy was successfully updated, but the current policy
193 // is the same as it was before the call.
194 status_t setDisplayManagerPolicy(const Policy& policy) EXCLUDES(mLock);
195 // Sets the override policy. See setDisplayManagerPolicy() for the meaning of the return value.
196 status_t setOverridePolicy(const std::optional<Policy>& policy) EXCLUDES(mLock);
197 // Gets the current policy, which will be the override policy if active, and the display manager
198 // policy otherwise.
199 Policy getCurrentPolicy() const EXCLUDES(mLock);
200 // Gets the display manager policy, regardless of whether an override policy is active.
201 Policy getDisplayManagerPolicy() const EXCLUDES(mLock);
Ana Kruleced3a8cc2019-11-14 00:55:07 +0100202
203 // Returns true if config is allowed by the current policy.
Marin Shalamanov23c44202020-12-22 19:09:20 +0100204 bool isConfigAllowed(DisplayModeId config) const EXCLUDES(mLock);
Ady Abraham2139f732019-11-13 18:56:40 -0800205
Ady Abraham8a82ba62020-01-17 12:43:17 -0800206 // Describes the different options the layer voted for refresh rate
207 enum class LayerVoteType {
Ady Abraham71c437d2020-01-31 15:56:57 -0800208 NoVote, // Doesn't care about the refresh rate
209 Min, // Minimal refresh rate available
210 Max, // Maximal refresh rate available
211 Heuristic, // Specific refresh rate that was calculated by platform using a heuristic
212 ExplicitDefault, // Specific refresh rate that was provided by the app with Default
213 // compatibility
Ady Abrahamdd5bfa92021-01-07 17:56:08 -0800214 ExplicitExactOrMultiple, // Specific refresh rate that was provided by the app with
215 // ExactOrMultiple compatibility
216 ExplicitExact, // Specific refresh rate that was provided by the app with
217 // Exact compatibility
218
Ady Abraham8a82ba62020-01-17 12:43:17 -0800219 };
220
221 // Captures the layer requirements for a refresh rate. This will be used to determine the
222 // display refresh rate.
223 struct LayerRequirement {
Ady Abrahamaae5ed52020-06-26 09:32:43 -0700224 // Layer's name. Used for debugging purposes.
225 std::string name;
Ady Abraham62a0be22020-12-08 16:54:10 -0800226 // Layer's owner uid
227 uid_t ownerUid = static_cast<uid_t>(-1);
Ady Abrahamaae5ed52020-06-26 09:32:43 -0700228 // Layer vote type.
229 LayerVoteType vote = LayerVoteType::NoVote;
230 // Layer's desired refresh rate, if applicable.
Marin Shalamanove8a663d2020-11-24 17:48:00 +0100231 Fps desiredRefreshRate{0.0f};
Marin Shalamanov46084422020-10-13 12:33:42 +0200232 // If a seamless mode switch is required.
Marin Shalamanov53fc11d2020-11-20 14:00:13 +0100233 Seamlessness seamlessness = Seamlessness::Default;
Ady Abrahamaae5ed52020-06-26 09:32:43 -0700234 // Layer's weight in the range of [0, 1]. The higher the weight the more impact this layer
235 // would have on choosing the refresh rate.
236 float weight = 0.0f;
237 // Whether layer is in focus or not based on WindowManager's state
238 bool focused = false;
Ady Abraham8a82ba62020-01-17 12:43:17 -0800239
240 bool operator==(const LayerRequirement& other) const {
241 return name == other.name && vote == other.vote &&
Marin Shalamanove8a663d2020-11-24 17:48:00 +0100242 desiredRefreshRate.equalsWithMargin(other.desiredRefreshRate) &&
Marin Shalamanovbe97cfa2020-12-01 16:02:07 +0100243 seamlessness == other.seamlessness && weight == other.weight &&
Ady Abrahamaae5ed52020-06-26 09:32:43 -0700244 focused == other.focused;
Ady Abraham8a82ba62020-01-17 12:43:17 -0800245 }
246
247 bool operator!=(const LayerRequirement& other) const { return !(*this == other); }
248 };
249
Ady Abrahamdfd62162020-06-10 16:11:56 -0700250 // Global state describing signals that affect refresh rate choice.
251 struct GlobalSignals {
252 // Whether the user touched the screen recently. Used to apply touch boost.
253 bool touch = false;
254 // True if the system hasn't seen any buffers posted to layers recently.
255 bool idle = false;
256 };
257
Steven Thomasbb374322020-04-28 22:47:16 -0700258 // Returns the refresh rate that fits best to the given layers.
259 // layers - The layer requirements to consider.
Ady Abrahamdfd62162020-06-10 16:11:56 -0700260 // globalSignals - global state of touch and idle
261 // outSignalsConsidered - An output param that tells the caller whether the refresh rate was
262 // chosen based on touch boost and/or idle timer.
Marin Shalamanoveadf2e72020-12-10 15:35:28 +0100263 RefreshRate getBestRefreshRate(const std::vector<LayerRequirement>& layers,
264 const GlobalSignals& globalSignals,
265 GlobalSignals* outSignalsConsidered = nullptr) const
Ady Abraham6fb599b2020-03-05 13:48:22 -0800266 EXCLUDES(mLock);
Ana Krulecb43429d2019-01-09 14:28:51 -0800267
Marin Shalamanoveadf2e72020-12-10 15:35:28 +0100268 FpsRange getSupportedRefreshRateRange() const EXCLUDES(mLock) {
269 std::lock_guard lock(mLock);
270 return {mMinSupportedRefreshRate->getFps(), mMaxSupportedRefreshRate->getFps()};
271 }
Steven Thomas2bbaabe2019-08-28 16:08:35 -0700272
Marin Shalamanov23c44202020-12-22 19:09:20 +0100273 std::optional<Fps> onKernelTimerChanged(std::optional<DisplayModeId> desiredActiveConfigId,
Marin Shalamanoveadf2e72020-12-10 15:35:28 +0100274 bool timerExpired) const EXCLUDES(mLock);
Steven Thomas2bbaabe2019-08-28 16:08:35 -0700275
Steven Thomasf734df42020-04-13 21:09:28 -0700276 // Returns the highest refresh rate according to the current policy. May change at runtime. Only
277 // uses the primary range, not the app request range.
Marin Shalamanoveadf2e72020-12-10 15:35:28 +0100278 RefreshRate getMaxRefreshRateByPolicy() const EXCLUDES(mLock);
Ady Abraham2139f732019-11-13 18:56:40 -0800279
280 // Returns the current refresh rate
Marin Shalamanoveadf2e72020-12-10 15:35:28 +0100281 RefreshRate getCurrentRefreshRate() const EXCLUDES(mLock);
Ady Abraham2139f732019-11-13 18:56:40 -0800282
Ana Krulec5d477912020-02-07 12:02:38 -0800283 // Returns the current refresh rate, if allowed. Otherwise the default that is allowed by
284 // the policy.
Marin Shalamanoveadf2e72020-12-10 15:35:28 +0100285 RefreshRate getCurrentRefreshRateByPolicy() const;
Ana Krulec5d477912020-02-07 12:02:38 -0800286
Marin Shalamanov23c44202020-12-22 19:09:20 +0100287 // Returns the refresh rate that corresponds to a DisplayModeId. This may change at
Ady Abraham2139f732019-11-13 18:56:40 -0800288 // runtime.
Marin Shalamanoveadf2e72020-12-10 15:35:28 +0100289 // TODO(b/159590486) An invalid config id may be given here if the dipslay configs have changed.
Marin Shalamanov23c44202020-12-22 19:09:20 +0100290 RefreshRate getRefreshRateFromConfigId(DisplayModeId configId) const EXCLUDES(mLock) {
Marin Shalamanoveadf2e72020-12-10 15:35:28 +0100291 std::lock_guard lock(mLock);
Ady Abraham2e1dd892020-03-05 13:48:36 -0800292 return *mRefreshRates.at(configId);
Ady Abraham2139f732019-11-13 18:56:40 -0800293 };
294
295 // Stores the current configId the device operates at
Marin Shalamanov23c44202020-12-22 19:09:20 +0100296 void setCurrentConfigId(DisplayModeId configId) EXCLUDES(mLock);
Dominik Laskowski22488f62019-03-28 09:53:04 -0700297
Ady Abrahama6b676e2020-05-27 14:29:09 -0700298 // Returns a string that represents the layer vote type
299 static std::string layerVoteTypeString(LayerVoteType vote);
300
Ady Abrahamb1b9d412020-06-01 19:53:52 -0700301 // Returns a known frame rate that is the closest to frameRate
Marin Shalamanove8a663d2020-11-24 17:48:00 +0100302 Fps findClosestKnownFrameRate(Fps frameRate) const;
Ady Abrahamb1b9d412020-06-01 19:53:52 -0700303
Ady Abrahamdd5bfa92021-01-07 17:56:08 -0800304 RefreshRateConfigs(const DisplayModes& configs, DisplayModeId currentConfigId,
305 bool enableFrameRateOverride = false);
Ana Krulec4593b692019-01-11 22:07:25 -0800306
Marin Shalamanov23c44202020-12-22 19:09:20 +0100307 void updateDisplayConfigs(const DisplayModes& configs, DisplayModeId currentConfig)
Marin Shalamanov3ea1d602020-12-16 19:59:39 +0100308 EXCLUDES(mLock);
Marin Shalamanoveadf2e72020-12-10 15:35:28 +0100309
Dominik Laskowski983f2b52020-06-25 16:54:06 -0700310 // Returns whether switching configs (refresh rate or resolution) is possible.
311 // TODO(b/158780872): Consider HAL support, and skip frame rate detection if the configs only
312 // differ in resolution.
Marin Shalamanoveadf2e72020-12-10 15:35:28 +0100313 bool canSwitch() const EXCLUDES(mLock) {
314 std::lock_guard lock(mLock);
315 return mRefreshRates.size() > 1;
316 }
Dominik Laskowski983f2b52020-06-25 16:54:06 -0700317
Ana Krulecb9afd792020-06-11 13:16:15 -0700318 // Class to enumerate options around toggling the kernel timer on and off. We have an option
319 // for no change to avoid extra calls to kernel.
320 enum class KernelIdleTimerAction {
321 NoChange, // Do not change the idle timer.
322 TurnOff, // Turn off the idle timer.
323 TurnOn // Turn on the idle timer.
324 };
325 // Checks whether kernel idle timer should be active depending the policy decisions around
326 // refresh rates.
327 KernelIdleTimerAction getIdleTimerAction() const;
328
Ady Abraham64c2fc02020-12-29 12:07:50 -0800329 bool supportsFrameRateOverride() const { return mSupportsFrameRateOverride; }
330
Ady Abrahamdbb6dcf2020-12-28 22:22:12 +0000331 // Returns a divider for the current refresh rate
Ady Abraham62a0be22020-12-08 16:54:10 -0800332 int getRefreshRateDivider(Fps frameRate) const EXCLUDES(mLock);
333
Ady Abraham62a0be22020-12-08 16:54:10 -0800334 using UidToFrameRateOverride = std::map<uid_t, Fps>;
Ady Abrahamdd5bfa92021-01-07 17:56:08 -0800335 // Returns the frame rate override for each uid.
336 //
337 // @param layers list of visible layers
338 // @param displayFrameRate the display frame rate
339 // @param touch whether touch timer is active (i.e. user touched the screen recently)
Ady Abraham62a0be22020-12-08 16:54:10 -0800340 UidToFrameRateOverride getFrameRateOverrides(const std::vector<LayerRequirement>& layers,
Ady Abrahamdd5bfa92021-01-07 17:56:08 -0800341 Fps displayFrameRate, bool touch) const
342 EXCLUDES(mLock);
Ady Abraham0bb6a472020-10-12 10:22:13 -0700343
Marin Shalamanovba421a82020-11-10 21:49:26 +0100344 void dump(std::string& result) const EXCLUDES(mLock);
345
Dominik Laskowski22488f62019-03-28 09:53:04 -0700346private:
Ady Abrahamb1b9d412020-06-01 19:53:52 -0700347 friend class RefreshRateConfigsTest;
348
Ady Abraham2139f732019-11-13 18:56:40 -0800349 void constructAvailableRefreshRates() REQUIRES(mLock);
350
Marin Shalamanoveadf2e72020-12-10 15:35:28 +0100351 void getSortedRefreshRateListLocked(
Ady Abraham2139f732019-11-13 18:56:40 -0800352 const std::function<bool(const RefreshRate&)>& shouldAddRefreshRate,
Marin Shalamanoveadf2e72020-12-10 15:35:28 +0100353 std::vector<const RefreshRate*>* outRefreshRates) REQUIRES(mLock);
Ady Abraham2139f732019-11-13 18:56:40 -0800354
Ady Abraham34702102020-02-10 14:12:05 -0800355 // Returns the refresh rate with the highest score in the collection specified from begin
356 // to end. If there are more than one with the same highest refresh rate, the first one is
357 // returned.
358 template <typename Iter>
359 const RefreshRate* getBestRefreshRate(Iter begin, Iter end) const;
360
Ady Abraham4ccdcb42020-02-11 17:34:34 -0800361 // Returns number of display frames and remainder when dividing the layer refresh period by
362 // display refresh period.
363 std::pair<nsecs_t, nsecs_t> getDisplayFrames(nsecs_t layerPeriod, nsecs_t displayPeriod) const;
364
Steven Thomasf734df42020-04-13 21:09:28 -0700365 // Returns the lowest refresh rate according to the current policy. May change at runtime. Only
366 // uses the primary range, not the app request range.
367 const RefreshRate& getMinRefreshRateByPolicyLocked() const REQUIRES(mLock);
368
369 // Returns the highest refresh rate according to the current policy. May change at runtime. Only
370 // uses the primary range, not the app request range.
371 const RefreshRate& getMaxRefreshRateByPolicyLocked() const REQUIRES(mLock);
372
Ana Krulec3d367c82020-02-25 15:02:01 -0800373 // Returns the current refresh rate, if allowed. Otherwise the default that is allowed by
374 // the policy.
375 const RefreshRate& getCurrentRefreshRateByPolicyLocked() const REQUIRES(mLock);
376
Steven Thomasd4071902020-03-24 16:02:53 -0700377 const Policy* getCurrentPolicyLocked() const REQUIRES(mLock);
Marin Shalamanoveadf2e72020-12-10 15:35:28 +0100378 bool isPolicyValidLocked(const Policy& policy) const REQUIRES(mLock);
Steven Thomasd4071902020-03-24 16:02:53 -0700379
Ady Abraham62a0be22020-12-08 16:54:10 -0800380 // Return the display refresh rate divider to match the layer
381 // frame rate, or 0 if the display refresh rate is not a multiple of the
382 // layer refresh rate.
383 static int getFrameRateDivider(Fps displayFrameRate, Fps layerFrameRate);
384
385 // calculates a score for a layer. Used to determine the display refresh rate
386 // and the frame rate override for certains applications.
387 float calculateLayerScoreLocked(const LayerRequirement&, const RefreshRate&,
388 bool isSeamlessSwitch) const REQUIRES(mLock);
389
Marin Shalamanoveadf2e72020-12-10 15:35:28 +0100390 // The list of refresh rates, indexed by display config ID. This may change after this
Steven Thomas2bbaabe2019-08-28 16:08:35 -0700391 // object is initialized.
Marin Shalamanoveadf2e72020-12-10 15:35:28 +0100392 AllRefreshRatesMapType mRefreshRates GUARDED_BY(mLock);
Ady Abraham2139f732019-11-13 18:56:40 -0800393
Steven Thomasf734df42020-04-13 21:09:28 -0700394 // The list of refresh rates in the primary range of the current policy, ordered by vsyncPeriod
395 // (the first element is the lowest refresh rate).
396 std::vector<const RefreshRate*> mPrimaryRefreshRates GUARDED_BY(mLock);
397
398 // The list of refresh rates in the app request range of the current policy, ordered by
399 // vsyncPeriod (the first element is the lowest refresh rate).
400 std::vector<const RefreshRate*> mAppRequestRefreshRates GUARDED_BY(mLock);
Ady Abraham2139f732019-11-13 18:56:40 -0800401
402 // The current config. This will change at runtime. This is set by SurfaceFlinger on
403 // the main thread, and read by the Scheduler (and other objects) on other threads.
404 const RefreshRate* mCurrentRefreshRate GUARDED_BY(mLock);
405
Steven Thomasd4071902020-03-24 16:02:53 -0700406 // The policy values will change at runtime. They're set by SurfaceFlinger on the main thread,
407 // and read by the Scheduler (and other objects) on other threads.
408 Policy mDisplayManagerPolicy GUARDED_BY(mLock);
409 std::optional<Policy> mOverridePolicy GUARDED_BY(mLock);
Ady Abraham2139f732019-11-13 18:56:40 -0800410
411 // The min and max refresh rates supported by the device.
Marin Shalamanoveadf2e72020-12-10 15:35:28 +0100412 // This may change at runtime.
413 const RefreshRate* mMinSupportedRefreshRate GUARDED_BY(mLock);
414 const RefreshRate* mMaxSupportedRefreshRate GUARDED_BY(mLock);
Ady Abraham2139f732019-11-13 18:56:40 -0800415
Ady Abraham2139f732019-11-13 18:56:40 -0800416 mutable std::mutex mLock;
Ady Abrahamb1b9d412020-06-01 19:53:52 -0700417
418 // A sorted list of known frame rates that a Heuristic layer will choose
419 // from based on the closest value.
Marin Shalamanove8a663d2020-11-24 17:48:00 +0100420 const std::vector<Fps> mKnownFrameRates;
Ady Abraham64c2fc02020-12-29 12:07:50 -0800421
Ady Abrahamdd5bfa92021-01-07 17:56:08 -0800422 const bool mEnableFrameRateOverride;
Ady Abraham64c2fc02020-12-29 12:07:50 -0800423 bool mSupportsFrameRateOverride;
Ana Krulecb43429d2019-01-09 14:28:51 -0800424};
425
Dominik Laskowski98041832019-08-01 18:35:59 -0700426} // namespace android::scheduler