blob: ff1eabdfc830d9d078cd2b72ccb54fb609a31a07 [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>
20
Ana Krulecb43429d2019-01-09 14:28:51 -080021#include <algorithm>
22#include <numeric>
Steven Thomasd4071902020-03-24 16:02:53 -070023#include <optional>
Dominik Laskowski98041832019-08-01 18:35:59 -070024#include <type_traits>
Ana Krulecb43429d2019-01-09 14:28:51 -080025
Ana Krulec4593b692019-01-11 22:07:25 -080026#include "DisplayHardware/HWComposer.h"
Ady Abraham2139f732019-11-13 18:56:40 -080027#include "HwcStrongTypes.h"
Ana Krulec4593b692019-01-11 22:07:25 -080028#include "Scheduler/SchedulerUtils.h"
Ady Abraham2139f732019-11-13 18:56:40 -080029#include "Scheduler/StrongTyping.h"
Ana Krulec4593b692019-01-11 22:07:25 -080030
Dominik Laskowski98041832019-08-01 18:35:59 -070031namespace android::scheduler {
Ady Abrahamabc27602020-04-08 17:20:29 -070032class RefreshRateConfigsTest;
33
Ady Abraham4ccdcb42020-02-11 17:34:34 -080034using namespace std::chrono_literals;
Dominik Laskowski98041832019-08-01 18:35:59 -070035
36enum class RefreshRateConfigEvent : unsigned { None = 0b0, Changed = 0b1 };
37
38inline RefreshRateConfigEvent operator|(RefreshRateConfigEvent lhs, RefreshRateConfigEvent rhs) {
39 using T = std::underlying_type_t<RefreshRateConfigEvent>;
40 return static_cast<RefreshRateConfigEvent>(static_cast<T>(lhs) | static_cast<T>(rhs));
41}
Ana Krulecb43429d2019-01-09 14:28:51 -080042
43/**
Ady Abraham1902d072019-03-01 17:18:59 -080044 * This class is used to encapsulate configuration for refresh rates. It holds information
Ana Krulecb43429d2019-01-09 14:28:51 -080045 * about available refresh rates on the device, and the mapping between the numbers and human
46 * readable names.
47 */
48class RefreshRateConfigs {
49public:
Ady Abraham4ccdcb42020-02-11 17:34:34 -080050 // Margin used when matching refresh rates to the content desired ones.
51 static constexpr nsecs_t MARGIN_FOR_PERIOD_CALCULATION =
52 std::chrono::nanoseconds(800us).count();
53
Ady Abrahamabc27602020-04-08 17:20:29 -070054 class RefreshRate {
55 private:
56 // Effectively making the constructor private while allowing
57 // std::make_unique to create the object
58 struct ConstructorTag {
59 explicit ConstructorTag(int) {}
60 };
Ana Krulec72f0d6e2020-01-06 15:24:47 -080061
Ady Abrahamabc27602020-04-08 17:20:29 -070062 public:
63 RefreshRate(HwcConfigIndexType configId,
64 std::shared_ptr<const HWC2::Display::Config> config, std::string name,
65 float fps, ConstructorTag)
66 : configId(configId), hwcConfig(config), name(std::move(name)), fps(fps) {}
Ady Abraham2e1dd892020-03-05 13:48:36 -080067
68 RefreshRate(const RefreshRate&) = delete;
Ady Abrahamabc27602020-04-08 17:20:29 -070069
70 HwcConfigIndexType getConfigId() const { return configId; }
71 nsecs_t getVsyncPeriod() const { return hwcConfig->getVsyncPeriod(); }
72 int32_t getConfigGroup() const { return hwcConfig->getConfigGroup(); }
73 const std::string& getName() const { return name; }
74 float 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
77 // rate passed in. FPS_EPSILON is applied to the boundaries for approximation.
78 bool inPolicy(float minRefreshRate, float maxRefreshRate) const {
79 return (fps >= (minRefreshRate - FPS_EPSILON) && fps <= (maxRefreshRate + FPS_EPSILON));
80 }
81
Ady Abraham2139f732019-11-13 18:56:40 -080082 bool operator!=(const RefreshRate& other) const {
Ady Abrahamabc27602020-04-08 17:20:29 -070083 return configId != other.configId || hwcConfig != other.hwcConfig;
Ady Abraham2139f732019-11-13 18:56:40 -080084 }
85
86 bool operator==(const RefreshRate& other) const { return !(*this != other); }
Ady Abrahamabc27602020-04-08 17:20:29 -070087
88 private:
89 friend RefreshRateConfigs;
90 friend RefreshRateConfigsTest;
91
92 // The tolerance within which we consider FPS approximately equals.
93 static constexpr float FPS_EPSILON = 0.001f;
94
95 // This config ID corresponds to the position of the config in the vector that is stored
96 // on the device.
97 const HwcConfigIndexType configId;
98 // The config itself
99 std::shared_ptr<const HWC2::Display::Config> hwcConfig;
100 // Human readable name of the refresh rate.
101 const std::string name;
102 // Refresh rate in frames per second
103 const float fps = 0;
Ana Krulecb43429d2019-01-09 14:28:51 -0800104 };
105
Ady Abraham2e1dd892020-03-05 13:48:36 -0800106 using AllRefreshRatesMapType =
107 std::unordered_map<HwcConfigIndexType, std::unique_ptr<const RefreshRate>>;
Ady Abraham2139f732019-11-13 18:56:40 -0800108
Steven Thomasd4071902020-03-24 16:02:53 -0700109 struct Policy {
Steven Thomasf734df42020-04-13 21:09:28 -0700110 struct Range {
111 float min = 0;
112 float max = std::numeric_limits<float>::max();
113
114 bool operator==(const Range& other) const {
115 return min == other.min && max == other.max;
116 }
117
118 bool operator!=(const Range& other) const { return !(*this == other); }
119 };
120
Steven Thomasd4071902020-03-24 16:02:53 -0700121 // The default config, used to ensure we only initiate display config switches within the
122 // same config group as defaultConfigId's group.
123 HwcConfigIndexType defaultConfig;
Steven Thomasf734df42020-04-13 21:09:28 -0700124 // The primary refresh rate range represents display manager's general guidance on the
125 // display configs we'll consider when switching refresh rates. Unless we get an explicit
126 // signal from an app, we should stay within this range.
127 Range primaryRange;
128 // The app request refresh rate range allows us to consider more display configs when
129 // switching refresh rates. Although we should generally stay within the primary range,
130 // specific considerations, such as layer frame rate settings specified via the
131 // setFrameRate() api, may cause us to go outside the primary range. We never go outside the
132 // app request range. The app request range will be greater than or equal to the primary
133 // refresh rate range, never smaller.
134 Range appRequestRange;
Steven Thomasd4071902020-03-24 16:02:53 -0700135 // Whether or not we switch config groups to get the best frame rate. Only used by tests.
136 bool allowGroupSwitching = false;
137
Steven Thomasf734df42020-04-13 21:09:28 -0700138 Policy() = default;
139 Policy(HwcConfigIndexType defaultConfig, const Range& range)
140 : Policy(defaultConfig, range, range) {}
141 Policy(HwcConfigIndexType defaultConfig, const Range& primaryRange,
142 const Range& appRequestRange)
143 : defaultConfig(defaultConfig),
144 primaryRange(primaryRange),
145 appRequestRange(appRequestRange) {}
146
Steven Thomasd4071902020-03-24 16:02:53 -0700147 bool operator==(const Policy& other) const {
Steven Thomasf734df42020-04-13 21:09:28 -0700148 return defaultConfig == other.defaultConfig && primaryRange == other.primaryRange &&
149 appRequestRange == other.appRequestRange &&
Steven Thomasd4071902020-03-24 16:02:53 -0700150 allowGroupSwitching == other.allowGroupSwitching;
151 }
152
153 bool operator!=(const Policy& other) const { return !(*this == other); }
154 };
155
156 // Return code set*Policy() to indicate the current policy is unchanged.
157 static constexpr int CURRENT_POLICY_UNCHANGED = 1;
158
159 // We maintain the display manager policy and the override policy separately. The override
160 // policy is used by CTS tests to get a consistent device state for testing. While the override
161 // policy is set, it takes precedence over the display manager policy. Once the override policy
162 // is cleared, we revert to using the display manager policy.
163
164 // Sets the display manager policy to choose refresh rates. The return value will be:
165 // - A negative value if the policy is invalid or another error occurred.
166 // - NO_ERROR if the policy was successfully updated, and the current policy is different from
167 // what it was before the call.
168 // - CURRENT_POLICY_UNCHANGED if the policy was successfully updated, but the current policy
169 // is the same as it was before the call.
170 status_t setDisplayManagerPolicy(const Policy& policy) EXCLUDES(mLock);
171 // Sets the override policy. See setDisplayManagerPolicy() for the meaning of the return value.
172 status_t setOverridePolicy(const std::optional<Policy>& policy) EXCLUDES(mLock);
173 // Gets the current policy, which will be the override policy if active, and the display manager
174 // policy otherwise.
175 Policy getCurrentPolicy() const EXCLUDES(mLock);
176 // Gets the display manager policy, regardless of whether an override policy is active.
177 Policy getDisplayManagerPolicy() const EXCLUDES(mLock);
Ana Kruleced3a8cc2019-11-14 00:55:07 +0100178
179 // Returns true if config is allowed by the current policy.
180 bool isConfigAllowed(HwcConfigIndexType config) const EXCLUDES(mLock);
Ady Abraham2139f732019-11-13 18:56:40 -0800181
Ady Abraham8a82ba62020-01-17 12:43:17 -0800182 // Describes the different options the layer voted for refresh rate
183 enum class LayerVoteType {
Ady Abraham71c437d2020-01-31 15:56:57 -0800184 NoVote, // Doesn't care about the refresh rate
185 Min, // Minimal refresh rate available
186 Max, // Maximal refresh rate available
187 Heuristic, // Specific refresh rate that was calculated by platform using a heuristic
188 ExplicitDefault, // Specific refresh rate that was provided by the app with Default
189 // compatibility
190 ExplicitExactOrMultiple // Specific refresh rate that was provided by the app with
191 // ExactOrMultiple compatibility
Ady Abraham8a82ba62020-01-17 12:43:17 -0800192 };
193
194 // Captures the layer requirements for a refresh rate. This will be used to determine the
195 // display refresh rate.
196 struct LayerRequirement {
197 std::string name; // Layer's name. Used for debugging purposes.
198 LayerVoteType vote; // Layer vote type.
199 float desiredRefreshRate; // Layer's desired refresh rate, if applicable.
200 float weight; // Layer's weight in the range of [0, 1]. The higher the weight the more
201 // impact this layer would have on choosing the refresh rate.
202
203 bool operator==(const LayerRequirement& other) const {
204 return name == other.name && vote == other.vote &&
205 desiredRefreshRate == other.desiredRefreshRate && weight == other.weight;
206 }
207
208 bool operator!=(const LayerRequirement& other) const { return !(*this == other); }
209 };
210
Ady Abraham4ccdcb42020-02-11 17:34:34 -0800211 // Returns the refresh rate that fits best to the given layers.
Ady Abraham8a82ba62020-01-17 12:43:17 -0800212 const RefreshRate& getRefreshRateForContent(const std::vector<LayerRequirement>& layers) const
213 EXCLUDES(mLock);
214
Steven Thomasbb374322020-04-28 22:47:16 -0700215 // Returns the refresh rate that fits best to the given layers.
216 // layers - The layer requirements to consider.
217 // touchActive - Whether the user touched the screen recently. Used to apply touch boost.
218 // idle - True if the system hasn't seen any buffers posted to layers recently.
219 // touchConsidered - An output param that tells the caller whether the refresh rate was chosen
220 // based on touch boost.
221 const RefreshRate& getBestRefreshRate(const std::vector<LayerRequirement>& layers,
222 bool touchActive, bool idle, bool* touchConsidered) const
Ady Abraham6fb599b2020-03-05 13:48:22 -0800223 EXCLUDES(mLock);
Ana Krulecb43429d2019-01-09 14:28:51 -0800224
Ady Abraham2139f732019-11-13 18:56:40 -0800225 // Returns all the refresh rates supported by the device. This won't change at runtime.
226 const AllRefreshRatesMapType& getAllRefreshRates() const EXCLUDES(mLock);
Steven Thomas2bbaabe2019-08-28 16:08:35 -0700227
Ady Abraham2139f732019-11-13 18:56:40 -0800228 // Returns the lowest refresh rate supported by the device. This won't change at runtime.
229 const RefreshRate& getMinRefreshRate() const { return *mMinSupportedRefreshRate; }
Steven Thomas2bbaabe2019-08-28 16:08:35 -0700230
Steven Thomasf734df42020-04-13 21:09:28 -0700231 // Returns the lowest refresh rate according to the current policy. May change at runtime. Only
232 // uses the primary range, not the app request range.
Ady Abraham2139f732019-11-13 18:56:40 -0800233 const RefreshRate& getMinRefreshRateByPolicy() const EXCLUDES(mLock);
Steven Thomas2bbaabe2019-08-28 16:08:35 -0700234
Ady Abraham2139f732019-11-13 18:56:40 -0800235 // Returns the highest refresh rate supported by the device. This won't change at runtime.
236 const RefreshRate& getMaxRefreshRate() const { return *mMaxSupportedRefreshRate; }
Steven Thomas2bbaabe2019-08-28 16:08:35 -0700237
Steven Thomasf734df42020-04-13 21:09:28 -0700238 // Returns the highest refresh rate according to the current policy. May change at runtime. Only
239 // uses the primary range, not the app request range.
Ady Abraham2139f732019-11-13 18:56:40 -0800240 const RefreshRate& getMaxRefreshRateByPolicy() const EXCLUDES(mLock);
241
242 // Returns the current refresh rate
243 const RefreshRate& getCurrentRefreshRate() const EXCLUDES(mLock);
244
Ana Krulec5d477912020-02-07 12:02:38 -0800245 // Returns the current refresh rate, if allowed. Otherwise the default that is allowed by
246 // the policy.
247 const RefreshRate& getCurrentRefreshRateByPolicy() const;
248
Ady Abraham2139f732019-11-13 18:56:40 -0800249 // Returns the refresh rate that corresponds to a HwcConfigIndexType. This won't change at
250 // runtime.
251 const RefreshRate& getRefreshRateFromConfigId(HwcConfigIndexType configId) const {
Ady Abraham2e1dd892020-03-05 13:48:36 -0800252 return *mRefreshRates.at(configId);
Ady Abraham2139f732019-11-13 18:56:40 -0800253 };
254
255 // Stores the current configId the device operates at
256 void setCurrentConfigId(HwcConfigIndexType configId) EXCLUDES(mLock);
Dominik Laskowski22488f62019-03-28 09:53:04 -0700257
Ady Abrahama6b676e2020-05-27 14:29:09 -0700258 // Returns a string that represents the layer vote type
259 static std::string layerVoteTypeString(LayerVoteType vote);
260
Ana Krulec3f6a2062020-01-23 15:48:01 -0800261 RefreshRateConfigs(const std::vector<std::shared_ptr<const HWC2::Display::Config>>& configs,
Ady Abraham2139f732019-11-13 18:56:40 -0800262 HwcConfigIndexType currentConfigId);
Ana Krulec4593b692019-01-11 22:07:25 -0800263
Dominik Laskowski22488f62019-03-28 09:53:04 -0700264private:
Ady Abraham2139f732019-11-13 18:56:40 -0800265 void constructAvailableRefreshRates() REQUIRES(mLock);
266
267 void getSortedRefreshRateList(
268 const std::function<bool(const RefreshRate&)>& shouldAddRefreshRate,
269 std::vector<const RefreshRate*>* outRefreshRates);
270
Ady Abraham34702102020-02-10 14:12:05 -0800271 // Returns the refresh rate with the highest score in the collection specified from begin
272 // to end. If there are more than one with the same highest refresh rate, the first one is
273 // returned.
274 template <typename Iter>
275 const RefreshRate* getBestRefreshRate(Iter begin, Iter end) const;
276
Ady Abraham4ccdcb42020-02-11 17:34:34 -0800277 // Returns number of display frames and remainder when dividing the layer refresh period by
278 // display refresh period.
279 std::pair<nsecs_t, nsecs_t> getDisplayFrames(nsecs_t layerPeriod, nsecs_t displayPeriod) const;
280
Steven Thomasf734df42020-04-13 21:09:28 -0700281 // Returns the lowest refresh rate according to the current policy. May change at runtime. Only
282 // uses the primary range, not the app request range.
283 const RefreshRate& getMinRefreshRateByPolicyLocked() const REQUIRES(mLock);
284
285 // Returns the highest refresh rate according to the current policy. May change at runtime. Only
286 // uses the primary range, not the app request range.
287 const RefreshRate& getMaxRefreshRateByPolicyLocked() const REQUIRES(mLock);
288
Ana Krulec3d367c82020-02-25 15:02:01 -0800289 // Returns the current refresh rate, if allowed. Otherwise the default that is allowed by
290 // the policy.
291 const RefreshRate& getCurrentRefreshRateByPolicyLocked() const REQUIRES(mLock);
292
Steven Thomasd4071902020-03-24 16:02:53 -0700293 const Policy* getCurrentPolicyLocked() const REQUIRES(mLock);
294 bool isPolicyValid(const Policy& policy);
295
Steven Thomas2bbaabe2019-08-28 16:08:35 -0700296 // The list of refresh rates, indexed by display config ID. This must not change after this
297 // object is initialized.
Ady Abraham2139f732019-11-13 18:56:40 -0800298 AllRefreshRatesMapType mRefreshRates;
299
Steven Thomasf734df42020-04-13 21:09:28 -0700300 // The list of refresh rates in the primary range of the current policy, ordered by vsyncPeriod
301 // (the first element is the lowest refresh rate).
302 std::vector<const RefreshRate*> mPrimaryRefreshRates GUARDED_BY(mLock);
303
304 // The list of refresh rates in the app request range of the current policy, ordered by
305 // vsyncPeriod (the first element is the lowest refresh rate).
306 std::vector<const RefreshRate*> mAppRequestRefreshRates GUARDED_BY(mLock);
Ady Abraham2139f732019-11-13 18:56:40 -0800307
308 // The current config. This will change at runtime. This is set by SurfaceFlinger on
309 // the main thread, and read by the Scheduler (and other objects) on other threads.
310 const RefreshRate* mCurrentRefreshRate GUARDED_BY(mLock);
311
Steven Thomasd4071902020-03-24 16:02:53 -0700312 // The policy values will change at runtime. They're set by SurfaceFlinger on the main thread,
313 // and read by the Scheduler (and other objects) on other threads.
314 Policy mDisplayManagerPolicy GUARDED_BY(mLock);
315 std::optional<Policy> mOverridePolicy GUARDED_BY(mLock);
Ady Abraham2139f732019-11-13 18:56:40 -0800316
317 // The min and max refresh rates supported by the device.
318 // This will not change at runtime.
319 const RefreshRate* mMinSupportedRefreshRate;
320 const RefreshRate* mMaxSupportedRefreshRate;
321
Ady Abraham2139f732019-11-13 18:56:40 -0800322 mutable std::mutex mLock;
Ana Krulecb43429d2019-01-09 14:28:51 -0800323};
324
Dominik Laskowski98041832019-08-01 18:35:59 -0700325} // namespace android::scheduler