blob: c8aec86db3c6d7203b25fb8a7b63b2652c4fb872 [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>
Dominik Laskowski98041832019-08-01 18:35:59 -070023#include <type_traits>
Ana Krulecb43429d2019-01-09 14:28:51 -080024
Ana Krulec4593b692019-01-11 22:07:25 -080025#include "DisplayHardware/HWComposer.h"
Ady Abraham2139f732019-11-13 18:56:40 -080026#include "HwcStrongTypes.h"
Ana Krulec4593b692019-01-11 22:07:25 -080027#include "Scheduler/SchedulerUtils.h"
Ady Abraham2139f732019-11-13 18:56:40 -080028#include "Scheduler/StrongTyping.h"
Ana Krulec4593b692019-01-11 22:07:25 -080029
Dominik Laskowski98041832019-08-01 18:35:59 -070030namespace android::scheduler {
Ady Abraham4ccdcb42020-02-11 17:34:34 -080031using namespace std::chrono_literals;
Dominik Laskowski98041832019-08-01 18:35:59 -070032
33enum class RefreshRateConfigEvent : unsigned { None = 0b0, Changed = 0b1 };
34
35inline RefreshRateConfigEvent operator|(RefreshRateConfigEvent lhs, RefreshRateConfigEvent rhs) {
36 using T = std::underlying_type_t<RefreshRateConfigEvent>;
37 return static_cast<RefreshRateConfigEvent>(static_cast<T>(lhs) | static_cast<T>(rhs));
38}
Ana Krulecb43429d2019-01-09 14:28:51 -080039
40/**
Ady Abraham1902d072019-03-01 17:18:59 -080041 * This class is used to encapsulate configuration for refresh rates. It holds information
Ana Krulecb43429d2019-01-09 14:28:51 -080042 * about available refresh rates on the device, and the mapping between the numbers and human
43 * readable names.
44 */
45class RefreshRateConfigs {
46public:
Ady Abraham4ccdcb42020-02-11 17:34:34 -080047 // Margin used when matching refresh rates to the content desired ones.
48 static constexpr nsecs_t MARGIN_FOR_PERIOD_CALCULATION =
49 std::chrono::nanoseconds(800us).count();
50
Ana Krulecb43429d2019-01-09 14:28:51 -080051 struct RefreshRate {
Ana Krulec72f0d6e2020-01-06 15:24:47 -080052 // The tolerance within which we consider FPS approximately equals.
53 static constexpr float FPS_EPSILON = 0.001f;
54
Ady Abraham2139f732019-11-13 18:56:40 -080055 RefreshRate(HwcConfigIndexType configId, nsecs_t vsyncPeriod,
56 HwcConfigGroupType configGroup, std::string name, float fps)
57 : configId(configId),
58 vsyncPeriod(vsyncPeriod),
59 configGroup(configGroup),
60 name(std::move(name)),
61 fps(fps) {}
Ana Krulecb43429d2019-01-09 14:28:51 -080062 // This config ID corresponds to the position of the config in the vector that is stored
63 // on the device.
Ady Abraham2139f732019-11-13 18:56:40 -080064 const HwcConfigIndexType configId;
Steven Thomas2bbaabe2019-08-28 16:08:35 -070065 // Vsync period in nanoseconds.
Ady Abraham2139f732019-11-13 18:56:40 -080066 const nsecs_t vsyncPeriod;
67 // This configGroup for the config.
68 const HwcConfigGroupType configGroup;
69 // Human readable name of the refresh rate.
70 const std::string name;
71 // Refresh rate in frames per second
72 const float fps = 0;
73
Ana Krulec72f0d6e2020-01-06 15:24:47 -080074 // Checks whether the fps of this RefreshRate struct is within a given min and max refresh
75 // rate passed in. FPS_EPSILON is applied to the boundaries for approximation.
76 bool inPolicy(float minRefreshRate, float maxRefreshRate) const {
77 return (fps >= (minRefreshRate - FPS_EPSILON) && fps <= (maxRefreshRate + FPS_EPSILON));
78 }
79
Ady Abraham2139f732019-11-13 18:56:40 -080080 bool operator!=(const RefreshRate& other) const {
81 return configId != other.configId || vsyncPeriod != other.vsyncPeriod ||
82 configGroup != other.configGroup;
83 }
84
85 bool operator==(const RefreshRate& other) const { return !(*this != other); }
Ana Krulecb43429d2019-01-09 14:28:51 -080086 };
87
Ady Abraham2139f732019-11-13 18:56:40 -080088 using AllRefreshRatesMapType = std::unordered_map<HwcConfigIndexType, const RefreshRate>;
89
Ana Kruleced3a8cc2019-11-14 00:55:07 +010090 // Sets the current policy to choose refresh rates. Returns NO_ERROR if the requested policy is
91 // valid, or a negative error value otherwise. policyChanged, if non-null, will be set to true
92 // if the new policy is different from the old policy.
93 status_t setPolicy(HwcConfigIndexType defaultConfigId, float minRefreshRate,
94 float maxRefreshRate, bool* policyChanged) EXCLUDES(mLock);
95 // Gets the current policy.
96 void getPolicy(HwcConfigIndexType* defaultConfigId, float* minRefreshRate,
97 float* maxRefreshRate) const EXCLUDES(mLock);
98
99 // Returns true if config is allowed by the current policy.
100 bool isConfigAllowed(HwcConfigIndexType config) const EXCLUDES(mLock);
Ady Abraham2139f732019-11-13 18:56:40 -0800101
Ady Abraham8a82ba62020-01-17 12:43:17 -0800102 // Describes the different options the layer voted for refresh rate
103 enum class LayerVoteType {
Ady Abraham71c437d2020-01-31 15:56:57 -0800104 NoVote, // Doesn't care about the refresh rate
105 Min, // Minimal refresh rate available
106 Max, // Maximal refresh rate available
107 Heuristic, // Specific refresh rate that was calculated by platform using a heuristic
108 ExplicitDefault, // Specific refresh rate that was provided by the app with Default
109 // compatibility
110 ExplicitExactOrMultiple // Specific refresh rate that was provided by the app with
111 // ExactOrMultiple compatibility
Ady Abraham8a82ba62020-01-17 12:43:17 -0800112 };
113
114 // Captures the layer requirements for a refresh rate. This will be used to determine the
115 // display refresh rate.
116 struct LayerRequirement {
117 std::string name; // Layer's name. Used for debugging purposes.
118 LayerVoteType vote; // Layer vote type.
119 float desiredRefreshRate; // Layer's desired refresh rate, if applicable.
120 float weight; // Layer's weight in the range of [0, 1]. The higher the weight the more
121 // impact this layer would have on choosing the refresh rate.
122
123 bool operator==(const LayerRequirement& other) const {
124 return name == other.name && vote == other.vote &&
125 desiredRefreshRate == other.desiredRefreshRate && weight == other.weight;
126 }
127
128 bool operator!=(const LayerRequirement& other) const { return !(*this == other); }
129 };
130
Ady Abraham4ccdcb42020-02-11 17:34:34 -0800131 // Returns the refresh rate that fits best to the given layers.
Ady Abraham8a82ba62020-01-17 12:43:17 -0800132 const RefreshRate& getRefreshRateForContent(const std::vector<LayerRequirement>& layers) const
133 EXCLUDES(mLock);
134
Ady Abraham4ccdcb42020-02-11 17:34:34 -0800135 // Returns the refresh rate that fits best to the given layers. This function also gets a
136 // boolean flag that indicates whether user touched the screen recently to be factored in when
137 // choosing the refresh rate.
138 const RefreshRate& getRefreshRateForContentV2(const std::vector<LayerRequirement>& layers,
139 bool touchActive) const EXCLUDES(mLock);
Ana Krulecb43429d2019-01-09 14:28:51 -0800140
Ady Abraham2139f732019-11-13 18:56:40 -0800141 // Returns all the refresh rates supported by the device. This won't change at runtime.
142 const AllRefreshRatesMapType& getAllRefreshRates() const EXCLUDES(mLock);
Steven Thomas2bbaabe2019-08-28 16:08:35 -0700143
Ady Abraham2139f732019-11-13 18:56:40 -0800144 // Returns the lowest refresh rate supported by the device. This won't change at runtime.
145 const RefreshRate& getMinRefreshRate() const { return *mMinSupportedRefreshRate; }
Steven Thomas2bbaabe2019-08-28 16:08:35 -0700146
Ady Abraham2139f732019-11-13 18:56:40 -0800147 // Returns the lowest refresh rate according to the current policy. May change in runtime.
148 const RefreshRate& getMinRefreshRateByPolicy() const EXCLUDES(mLock);
Steven Thomas2bbaabe2019-08-28 16:08:35 -0700149
Ady Abraham2139f732019-11-13 18:56:40 -0800150 // Returns the highest refresh rate supported by the device. This won't change at runtime.
151 const RefreshRate& getMaxRefreshRate() const { return *mMaxSupportedRefreshRate; }
Steven Thomas2bbaabe2019-08-28 16:08:35 -0700152
Ady Abraham2139f732019-11-13 18:56:40 -0800153 // Returns the highest refresh rate according to the current policy. May change in runtime.
154 const RefreshRate& getMaxRefreshRateByPolicy() const EXCLUDES(mLock);
155
156 // Returns the current refresh rate
157 const RefreshRate& getCurrentRefreshRate() const EXCLUDES(mLock);
158
Ana Krulec5d477912020-02-07 12:02:38 -0800159 // Returns the current refresh rate, if allowed. Otherwise the default that is allowed by
160 // the policy.
161 const RefreshRate& getCurrentRefreshRateByPolicy() const;
162
Ady Abraham2139f732019-11-13 18:56:40 -0800163 // Returns the refresh rate that corresponds to a HwcConfigIndexType. This won't change at
164 // runtime.
165 const RefreshRate& getRefreshRateFromConfigId(HwcConfigIndexType configId) const {
166 return mRefreshRates.at(configId);
167 };
168
169 // Stores the current configId the device operates at
170 void setCurrentConfigId(HwcConfigIndexType configId) EXCLUDES(mLock);
Dominik Laskowski22488f62019-03-28 09:53:04 -0700171
Steven Thomas2bbaabe2019-08-28 16:08:35 -0700172 struct InputConfig {
Ady Abraham2139f732019-11-13 18:56:40 -0800173 HwcConfigIndexType configId = HwcConfigIndexType(0);
174 HwcConfigGroupType configGroup = HwcConfigGroupType(0);
Steven Thomas2bbaabe2019-08-28 16:08:35 -0700175 nsecs_t vsyncPeriod = 0;
176 };
Ana Krulec4593b692019-01-11 22:07:25 -0800177
Ana Krulec3f6a2062020-01-23 15:48:01 -0800178 RefreshRateConfigs(const std::vector<InputConfig>& configs,
Ady Abraham2139f732019-11-13 18:56:40 -0800179 HwcConfigIndexType currentHwcConfig);
Ana Krulec3f6a2062020-01-23 15:48:01 -0800180 RefreshRateConfigs(const std::vector<std::shared_ptr<const HWC2::Display::Config>>& configs,
Ady Abraham2139f732019-11-13 18:56:40 -0800181 HwcConfigIndexType currentConfigId);
Ana Krulec4593b692019-01-11 22:07:25 -0800182
Dominik Laskowski22488f62019-03-28 09:53:04 -0700183private:
Ady Abraham2139f732019-11-13 18:56:40 -0800184 void init(const std::vector<InputConfig>& configs, HwcConfigIndexType currentHwcConfig);
185
186 void constructAvailableRefreshRates() REQUIRES(mLock);
187
188 void getSortedRefreshRateList(
189 const std::function<bool(const RefreshRate&)>& shouldAddRefreshRate,
190 std::vector<const RefreshRate*>* outRefreshRates);
191
Ady Abraham34702102020-02-10 14:12:05 -0800192 // Returns the refresh rate with the highest score in the collection specified from begin
193 // to end. If there are more than one with the same highest refresh rate, the first one is
194 // returned.
195 template <typename Iter>
196 const RefreshRate* getBestRefreshRate(Iter begin, Iter end) const;
197
Ady Abraham4ccdcb42020-02-11 17:34:34 -0800198 // Returns number of display frames and remainder when dividing the layer refresh period by
199 // display refresh period.
200 std::pair<nsecs_t, nsecs_t> getDisplayFrames(nsecs_t layerPeriod, nsecs_t displayPeriod) const;
201
Ana Krulec3d367c82020-02-25 15:02:01 -0800202 // Returns the current refresh rate, if allowed. Otherwise the default that is allowed by
203 // the policy.
204 const RefreshRate& getCurrentRefreshRateByPolicyLocked() const REQUIRES(mLock);
205
Steven Thomas2bbaabe2019-08-28 16:08:35 -0700206 // The list of refresh rates, indexed by display config ID. This must not change after this
207 // object is initialized.
Ady Abraham2139f732019-11-13 18:56:40 -0800208 AllRefreshRatesMapType mRefreshRates;
209
210 // The list of refresh rates which are available in the current policy, ordered by vsyncPeriod
211 // (the first element is the lowest refresh rate)
212 std::vector<const RefreshRate*> mAvailableRefreshRates GUARDED_BY(mLock);
213
214 // The current config. This will change at runtime. This is set by SurfaceFlinger on
215 // the main thread, and read by the Scheduler (and other objects) on other threads.
216 const RefreshRate* mCurrentRefreshRate GUARDED_BY(mLock);
217
Ana Kruleced3a8cc2019-11-14 00:55:07 +0100218 // The default config. This will change at runtime. This is set by SurfaceFlinger on
Ady Abraham2139f732019-11-13 18:56:40 -0800219 // the main thread, and read by the Scheduler (and other objects) on other threads.
Ana Kruleced3a8cc2019-11-14 00:55:07 +0100220 HwcConfigIndexType mDefaultConfig GUARDED_BY(mLock);
Ady Abraham2139f732019-11-13 18:56:40 -0800221
222 // The min and max FPS allowed by the policy. This will change at runtime and set by
223 // SurfaceFlinger on the main thread.
224 float mMinRefreshRateFps GUARDED_BY(mLock) = 0;
225 float mMaxRefreshRateFps GUARDED_BY(mLock) = std::numeric_limits<float>::max();
226
227 // The min and max refresh rates supported by the device.
228 // This will not change at runtime.
229 const RefreshRate* mMinSupportedRefreshRate;
230 const RefreshRate* mMaxSupportedRefreshRate;
231
Ady Abraham2139f732019-11-13 18:56:40 -0800232 mutable std::mutex mLock;
Ana Krulecb43429d2019-01-09 14:28:51 -0800233};
234
Dominik Laskowski98041832019-08-01 18:35:59 -0700235} // namespace android::scheduler