blob: e749f8fb93910b5efb1e441464234e5f64d05aeb [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 Abraham4ccdcb42020-02-11 17:34:34 -080032using namespace std::chrono_literals;
Dominik Laskowski98041832019-08-01 18:35:59 -070033
34enum class RefreshRateConfigEvent : unsigned { None = 0b0, Changed = 0b1 };
35
36inline RefreshRateConfigEvent operator|(RefreshRateConfigEvent lhs, RefreshRateConfigEvent rhs) {
37 using T = std::underlying_type_t<RefreshRateConfigEvent>;
38 return static_cast<RefreshRateConfigEvent>(static_cast<T>(lhs) | static_cast<T>(rhs));
39}
Ana Krulecb43429d2019-01-09 14:28:51 -080040
41/**
Ady Abraham1902d072019-03-01 17:18:59 -080042 * This class is used to encapsulate configuration for refresh rates. It holds information
Ana Krulecb43429d2019-01-09 14:28:51 -080043 * about available refresh rates on the device, and the mapping between the numbers and human
44 * readable names.
45 */
46class RefreshRateConfigs {
47public:
Ady Abraham4ccdcb42020-02-11 17:34:34 -080048 // Margin used when matching refresh rates to the content desired ones.
49 static constexpr nsecs_t MARGIN_FOR_PERIOD_CALCULATION =
50 std::chrono::nanoseconds(800us).count();
51
Ana Krulecb43429d2019-01-09 14:28:51 -080052 struct RefreshRate {
Ana Krulec72f0d6e2020-01-06 15:24:47 -080053 // The tolerance within which we consider FPS approximately equals.
54 static constexpr float FPS_EPSILON = 0.001f;
55
Ady Abraham2139f732019-11-13 18:56:40 -080056 RefreshRate(HwcConfigIndexType configId, nsecs_t vsyncPeriod,
57 HwcConfigGroupType configGroup, std::string name, float fps)
58 : configId(configId),
59 vsyncPeriod(vsyncPeriod),
60 configGroup(configGroup),
61 name(std::move(name)),
62 fps(fps) {}
Ady Abraham2e1dd892020-03-05 13:48:36 -080063
64 RefreshRate(const RefreshRate&) = delete;
Ana Krulecb43429d2019-01-09 14:28:51 -080065 // This config ID corresponds to the position of the config in the vector that is stored
66 // on the device.
Ady Abraham2139f732019-11-13 18:56:40 -080067 const HwcConfigIndexType configId;
Steven Thomas2bbaabe2019-08-28 16:08:35 -070068 // Vsync period in nanoseconds.
Ady Abraham2139f732019-11-13 18:56:40 -080069 const nsecs_t vsyncPeriod;
70 // This configGroup for the config.
71 const HwcConfigGroupType configGroup;
72 // Human readable name of the refresh rate.
73 const std::string name;
74 // Refresh rate in frames per second
75 const float fps = 0;
76
Ana Krulec72f0d6e2020-01-06 15:24:47 -080077 // Checks whether the fps of this RefreshRate struct is within a given min and max refresh
78 // rate passed in. FPS_EPSILON is applied to the boundaries for approximation.
79 bool inPolicy(float minRefreshRate, float maxRefreshRate) const {
80 return (fps >= (minRefreshRate - FPS_EPSILON) && fps <= (maxRefreshRate + FPS_EPSILON));
81 }
82
Ady Abraham2139f732019-11-13 18:56:40 -080083 bool operator!=(const RefreshRate& other) const {
84 return configId != other.configId || vsyncPeriod != other.vsyncPeriod ||
85 configGroup != other.configGroup;
86 }
87
88 bool operator==(const RefreshRate& other) const { return !(*this != other); }
Ana Krulecb43429d2019-01-09 14:28:51 -080089 };
90
Ady Abraham2e1dd892020-03-05 13:48:36 -080091 using AllRefreshRatesMapType =
92 std::unordered_map<HwcConfigIndexType, std::unique_ptr<const RefreshRate>>;
Ady Abraham2139f732019-11-13 18:56:40 -080093
Steven Thomasd4071902020-03-24 16:02:53 -070094 struct Policy {
95 // The default config, used to ensure we only initiate display config switches within the
96 // same config group as defaultConfigId's group.
97 HwcConfigIndexType defaultConfig;
98 // The min and max FPS allowed by the policy.
99 float minRefreshRate = 0;
100 float maxRefreshRate = std::numeric_limits<float>::max();
101 // Whether or not we switch config groups to get the best frame rate. Only used by tests.
102 bool allowGroupSwitching = false;
103
104 bool operator==(const Policy& other) const {
105 return defaultConfig == other.defaultConfig && minRefreshRate == other.minRefreshRate &&
106 maxRefreshRate == other.maxRefreshRate &&
107 allowGroupSwitching == other.allowGroupSwitching;
108 }
109
110 bool operator!=(const Policy& other) const { return !(*this == other); }
111 };
112
113 // Return code set*Policy() to indicate the current policy is unchanged.
114 static constexpr int CURRENT_POLICY_UNCHANGED = 1;
115
116 // We maintain the display manager policy and the override policy separately. The override
117 // policy is used by CTS tests to get a consistent device state for testing. While the override
118 // policy is set, it takes precedence over the display manager policy. Once the override policy
119 // is cleared, we revert to using the display manager policy.
120
121 // Sets the display manager policy to choose refresh rates. The return value will be:
122 // - A negative value if the policy is invalid or another error occurred.
123 // - NO_ERROR if the policy was successfully updated, and the current policy is different from
124 // what it was before the call.
125 // - CURRENT_POLICY_UNCHANGED if the policy was successfully updated, but the current policy
126 // is the same as it was before the call.
127 status_t setDisplayManagerPolicy(const Policy& policy) EXCLUDES(mLock);
128 // Sets the override policy. See setDisplayManagerPolicy() for the meaning of the return value.
129 status_t setOverridePolicy(const std::optional<Policy>& policy) EXCLUDES(mLock);
130 // Gets the current policy, which will be the override policy if active, and the display manager
131 // policy otherwise.
132 Policy getCurrentPolicy() const EXCLUDES(mLock);
133 // Gets the display manager policy, regardless of whether an override policy is active.
134 Policy getDisplayManagerPolicy() const EXCLUDES(mLock);
Ana Kruleced3a8cc2019-11-14 00:55:07 +0100135
136 // Returns true if config is allowed by the current policy.
137 bool isConfigAllowed(HwcConfigIndexType config) const EXCLUDES(mLock);
Ady Abraham2139f732019-11-13 18:56:40 -0800138
Ady Abraham8a82ba62020-01-17 12:43:17 -0800139 // Describes the different options the layer voted for refresh rate
140 enum class LayerVoteType {
Ady Abraham71c437d2020-01-31 15:56:57 -0800141 NoVote, // Doesn't care about the refresh rate
142 Min, // Minimal refresh rate available
143 Max, // Maximal refresh rate available
144 Heuristic, // Specific refresh rate that was calculated by platform using a heuristic
145 ExplicitDefault, // Specific refresh rate that was provided by the app with Default
146 // compatibility
147 ExplicitExactOrMultiple // Specific refresh rate that was provided by the app with
148 // ExactOrMultiple compatibility
Ady Abraham8a82ba62020-01-17 12:43:17 -0800149 };
150
151 // Captures the layer requirements for a refresh rate. This will be used to determine the
152 // display refresh rate.
153 struct LayerRequirement {
154 std::string name; // Layer's name. Used for debugging purposes.
155 LayerVoteType vote; // Layer vote type.
156 float desiredRefreshRate; // Layer's desired refresh rate, if applicable.
157 float weight; // Layer's weight in the range of [0, 1]. The higher the weight the more
158 // impact this layer would have on choosing the refresh rate.
159
160 bool operator==(const LayerRequirement& other) const {
161 return name == other.name && vote == other.vote &&
162 desiredRefreshRate == other.desiredRefreshRate && weight == other.weight;
163 }
164
165 bool operator!=(const LayerRequirement& other) const { return !(*this == other); }
166 };
167
Ady Abraham4ccdcb42020-02-11 17:34:34 -0800168 // Returns the refresh rate that fits best to the given layers.
Ady Abraham8a82ba62020-01-17 12:43:17 -0800169 const RefreshRate& getRefreshRateForContent(const std::vector<LayerRequirement>& layers) const
170 EXCLUDES(mLock);
171
Ady Abraham4ccdcb42020-02-11 17:34:34 -0800172 // Returns the refresh rate that fits best to the given layers. This function also gets a
173 // boolean flag that indicates whether user touched the screen recently to be factored in when
Ady Abraham6fb599b2020-03-05 13:48:22 -0800174 // choosing the refresh rate and returns whether the refresh rate was chosen as a result of
175 // a touch event.
Ady Abraham4ccdcb42020-02-11 17:34:34 -0800176 const RefreshRate& getRefreshRateForContentV2(const std::vector<LayerRequirement>& layers,
Ady Abraham6fb599b2020-03-05 13:48:22 -0800177 bool touchActive, bool* touchConsidered) const
178 EXCLUDES(mLock);
Ana Krulecb43429d2019-01-09 14:28:51 -0800179
Ady Abraham2139f732019-11-13 18:56:40 -0800180 // Returns all the refresh rates supported by the device. This won't change at runtime.
181 const AllRefreshRatesMapType& getAllRefreshRates() const EXCLUDES(mLock);
Steven Thomas2bbaabe2019-08-28 16:08:35 -0700182
Ady Abraham2139f732019-11-13 18:56:40 -0800183 // Returns the lowest refresh rate supported by the device. This won't change at runtime.
184 const RefreshRate& getMinRefreshRate() const { return *mMinSupportedRefreshRate; }
Steven Thomas2bbaabe2019-08-28 16:08:35 -0700185
Ady Abraham2139f732019-11-13 18:56:40 -0800186 // Returns the lowest refresh rate according to the current policy. May change in runtime.
187 const RefreshRate& getMinRefreshRateByPolicy() const EXCLUDES(mLock);
Steven Thomas2bbaabe2019-08-28 16:08:35 -0700188
Ady Abraham2139f732019-11-13 18:56:40 -0800189 // Returns the highest refresh rate supported by the device. This won't change at runtime.
190 const RefreshRate& getMaxRefreshRate() const { return *mMaxSupportedRefreshRate; }
Steven Thomas2bbaabe2019-08-28 16:08:35 -0700191
Ady Abraham2139f732019-11-13 18:56:40 -0800192 // Returns the highest refresh rate according to the current policy. May change in runtime.
193 const RefreshRate& getMaxRefreshRateByPolicy() const EXCLUDES(mLock);
194
195 // Returns the current refresh rate
196 const RefreshRate& getCurrentRefreshRate() const EXCLUDES(mLock);
197
Ana Krulec5d477912020-02-07 12:02:38 -0800198 // Returns the current refresh rate, if allowed. Otherwise the default that is allowed by
199 // the policy.
200 const RefreshRate& getCurrentRefreshRateByPolicy() const;
201
Ady Abraham2139f732019-11-13 18:56:40 -0800202 // Returns the refresh rate that corresponds to a HwcConfigIndexType. This won't change at
203 // runtime.
204 const RefreshRate& getRefreshRateFromConfigId(HwcConfigIndexType configId) const {
Ady Abraham2e1dd892020-03-05 13:48:36 -0800205 return *mRefreshRates.at(configId);
Ady Abraham2139f732019-11-13 18:56:40 -0800206 };
207
208 // Stores the current configId the device operates at
209 void setCurrentConfigId(HwcConfigIndexType configId) EXCLUDES(mLock);
Dominik Laskowski22488f62019-03-28 09:53:04 -0700210
Steven Thomas2bbaabe2019-08-28 16:08:35 -0700211 struct InputConfig {
Ady Abraham2139f732019-11-13 18:56:40 -0800212 HwcConfigIndexType configId = HwcConfigIndexType(0);
213 HwcConfigGroupType configGroup = HwcConfigGroupType(0);
Steven Thomas2bbaabe2019-08-28 16:08:35 -0700214 nsecs_t vsyncPeriod = 0;
215 };
Ana Krulec4593b692019-01-11 22:07:25 -0800216
Ana Krulec3f6a2062020-01-23 15:48:01 -0800217 RefreshRateConfigs(const std::vector<InputConfig>& configs,
Ady Abraham2139f732019-11-13 18:56:40 -0800218 HwcConfigIndexType currentHwcConfig);
Ana Krulec3f6a2062020-01-23 15:48:01 -0800219 RefreshRateConfigs(const std::vector<std::shared_ptr<const HWC2::Display::Config>>& configs,
Ady Abraham2139f732019-11-13 18:56:40 -0800220 HwcConfigIndexType currentConfigId);
Ana Krulec4593b692019-01-11 22:07:25 -0800221
Dominik Laskowski22488f62019-03-28 09:53:04 -0700222private:
Ady Abraham2139f732019-11-13 18:56:40 -0800223 void init(const std::vector<InputConfig>& configs, HwcConfigIndexType currentHwcConfig);
224
225 void constructAvailableRefreshRates() REQUIRES(mLock);
226
227 void getSortedRefreshRateList(
228 const std::function<bool(const RefreshRate&)>& shouldAddRefreshRate,
229 std::vector<const RefreshRate*>* outRefreshRates);
230
Ady Abraham34702102020-02-10 14:12:05 -0800231 // Returns the refresh rate with the highest score in the collection specified from begin
232 // to end. If there are more than one with the same highest refresh rate, the first one is
233 // returned.
234 template <typename Iter>
235 const RefreshRate* getBestRefreshRate(Iter begin, Iter end) const;
236
Ady Abraham4ccdcb42020-02-11 17:34:34 -0800237 // Returns number of display frames and remainder when dividing the layer refresh period by
238 // display refresh period.
239 std::pair<nsecs_t, nsecs_t> getDisplayFrames(nsecs_t layerPeriod, nsecs_t displayPeriod) const;
240
Ana Krulec3d367c82020-02-25 15:02:01 -0800241 // Returns the current refresh rate, if allowed. Otherwise the default that is allowed by
242 // the policy.
243 const RefreshRate& getCurrentRefreshRateByPolicyLocked() const REQUIRES(mLock);
244
Steven Thomasd4071902020-03-24 16:02:53 -0700245 const Policy* getCurrentPolicyLocked() const REQUIRES(mLock);
246 bool isPolicyValid(const Policy& policy);
247
Steven Thomas2bbaabe2019-08-28 16:08:35 -0700248 // The list of refresh rates, indexed by display config ID. This must not change after this
249 // object is initialized.
Ady Abraham2139f732019-11-13 18:56:40 -0800250 AllRefreshRatesMapType mRefreshRates;
251
252 // The list of refresh rates which are available in the current policy, ordered by vsyncPeriod
253 // (the first element is the lowest refresh rate)
254 std::vector<const RefreshRate*> mAvailableRefreshRates GUARDED_BY(mLock);
255
256 // The current config. This will change at runtime. This is set by SurfaceFlinger on
257 // the main thread, and read by the Scheduler (and other objects) on other threads.
258 const RefreshRate* mCurrentRefreshRate GUARDED_BY(mLock);
259
Steven Thomasd4071902020-03-24 16:02:53 -0700260 // The policy values will change at runtime. They're set by SurfaceFlinger on the main thread,
261 // and read by the Scheduler (and other objects) on other threads.
262 Policy mDisplayManagerPolicy GUARDED_BY(mLock);
263 std::optional<Policy> mOverridePolicy GUARDED_BY(mLock);
Ady Abraham2139f732019-11-13 18:56:40 -0800264
265 // The min and max refresh rates supported by the device.
266 // This will not change at runtime.
267 const RefreshRate* mMinSupportedRefreshRate;
268 const RefreshRate* mMaxSupportedRefreshRate;
269
Ady Abraham2139f732019-11-13 18:56:40 -0800270 mutable std::mutex mLock;
Ana Krulecb43429d2019-01-09 14:28:51 -0800271};
272
Dominik Laskowski98041832019-08-01 18:35:59 -0700273} // namespace android::scheduler