blob: 1132a8c58c90d71e48cef9822a192ed0a755971e [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 {
31
32enum class RefreshRateConfigEvent : unsigned { None = 0b0, Changed = 0b1 };
33
34inline RefreshRateConfigEvent operator|(RefreshRateConfigEvent lhs, RefreshRateConfigEvent rhs) {
35 using T = std::underlying_type_t<RefreshRateConfigEvent>;
36 return static_cast<RefreshRateConfigEvent>(static_cast<T>(lhs) | static_cast<T>(rhs));
37}
Ana Krulecb43429d2019-01-09 14:28:51 -080038
39/**
Ady Abraham1902d072019-03-01 17:18:59 -080040 * This class is used to encapsulate configuration for refresh rates. It holds information
Ana Krulecb43429d2019-01-09 14:28:51 -080041 * about available refresh rates on the device, and the mapping between the numbers and human
42 * readable names.
43 */
44class RefreshRateConfigs {
45public:
Ana Krulecb43429d2019-01-09 14:28:51 -080046 struct RefreshRate {
Ana Krulec72f0d6e2020-01-06 15:24:47 -080047 // The tolerance within which we consider FPS approximately equals.
48 static constexpr float FPS_EPSILON = 0.001f;
49
Ady Abraham2139f732019-11-13 18:56:40 -080050 RefreshRate(HwcConfigIndexType configId, nsecs_t vsyncPeriod,
51 HwcConfigGroupType configGroup, std::string name, float fps)
52 : configId(configId),
53 vsyncPeriod(vsyncPeriod),
54 configGroup(configGroup),
55 name(std::move(name)),
56 fps(fps) {}
Ana Krulecb43429d2019-01-09 14:28:51 -080057 // This config ID corresponds to the position of the config in the vector that is stored
58 // on the device.
Ady Abraham2139f732019-11-13 18:56:40 -080059 const HwcConfigIndexType configId;
Steven Thomas2bbaabe2019-08-28 16:08:35 -070060 // Vsync period in nanoseconds.
Ady Abraham2139f732019-11-13 18:56:40 -080061 const nsecs_t vsyncPeriod;
62 // This configGroup for the config.
63 const HwcConfigGroupType configGroup;
64 // Human readable name of the refresh rate.
65 const std::string name;
66 // Refresh rate in frames per second
67 const float fps = 0;
68
Ana Krulec72f0d6e2020-01-06 15:24:47 -080069 // Checks whether the fps of this RefreshRate struct is within a given min and max refresh
70 // rate passed in. FPS_EPSILON is applied to the boundaries for approximation.
71 bool inPolicy(float minRefreshRate, float maxRefreshRate) const {
72 return (fps >= (minRefreshRate - FPS_EPSILON) && fps <= (maxRefreshRate + FPS_EPSILON));
73 }
74
Ady Abraham2139f732019-11-13 18:56:40 -080075 bool operator!=(const RefreshRate& other) const {
76 return configId != other.configId || vsyncPeriod != other.vsyncPeriod ||
77 configGroup != other.configGroup;
78 }
79
80 bool operator==(const RefreshRate& other) const { return !(*this != other); }
Ana Krulecb43429d2019-01-09 14:28:51 -080081 };
82
Ady Abraham2139f732019-11-13 18:56:40 -080083 using AllRefreshRatesMapType = std::unordered_map<HwcConfigIndexType, const RefreshRate>;
84
Ana Kruleced3a8cc2019-11-14 00:55:07 +010085 // Sets the current policy to choose refresh rates. Returns NO_ERROR if the requested policy is
86 // valid, or a negative error value otherwise. policyChanged, if non-null, will be set to true
87 // if the new policy is different from the old policy.
88 status_t setPolicy(HwcConfigIndexType defaultConfigId, float minRefreshRate,
89 float maxRefreshRate, bool* policyChanged) EXCLUDES(mLock);
90 // Gets the current policy.
91 void getPolicy(HwcConfigIndexType* defaultConfigId, float* minRefreshRate,
92 float* maxRefreshRate) const EXCLUDES(mLock);
93
94 // Returns true if config is allowed by the current policy.
95 bool isConfigAllowed(HwcConfigIndexType config) const EXCLUDES(mLock);
Ady Abraham2139f732019-11-13 18:56:40 -080096
Ady Abraham8a82ba62020-01-17 12:43:17 -080097 // Describes the different options the layer voted for refresh rate
98 enum class LayerVoteType {
Ady Abraham71c437d2020-01-31 15:56:57 -080099 NoVote, // Doesn't care about the refresh rate
100 Min, // Minimal refresh rate available
101 Max, // Maximal refresh rate available
102 Heuristic, // Specific refresh rate that was calculated by platform using a heuristic
103 ExplicitDefault, // Specific refresh rate that was provided by the app with Default
104 // compatibility
105 ExplicitExactOrMultiple // Specific refresh rate that was provided by the app with
106 // ExactOrMultiple compatibility
Ady Abraham8a82ba62020-01-17 12:43:17 -0800107 };
108
109 // Captures the layer requirements for a refresh rate. This will be used to determine the
110 // display refresh rate.
111 struct LayerRequirement {
112 std::string name; // Layer's name. Used for debugging purposes.
113 LayerVoteType vote; // Layer vote type.
114 float desiredRefreshRate; // Layer's desired refresh rate, if applicable.
115 float weight; // Layer's weight in the range of [0, 1]. The higher the weight the more
116 // impact this layer would have on choosing the refresh rate.
117
118 bool operator==(const LayerRequirement& other) const {
119 return name == other.name && vote == other.vote &&
120 desiredRefreshRate == other.desiredRefreshRate && weight == other.weight;
121 }
122
123 bool operator!=(const LayerRequirement& other) const { return !(*this == other); }
124 };
125
Ady Abraham2139f732019-11-13 18:56:40 -0800126 // Returns all available refresh rates according to the current policy.
Ady Abraham8a82ba62020-01-17 12:43:17 -0800127 const RefreshRate& getRefreshRateForContent(const std::vector<LayerRequirement>& layers) const
128 EXCLUDES(mLock);
129
130 // Returns all available refresh rates according to the current policy.
131 const RefreshRate& getRefreshRateForContentV2(const std::vector<LayerRequirement>& layers) const
132 EXCLUDES(mLock);
Ana Krulecb43429d2019-01-09 14:28:51 -0800133
Ady Abraham2139f732019-11-13 18:56:40 -0800134 // Returns all the refresh rates supported by the device. This won't change at runtime.
135 const AllRefreshRatesMapType& getAllRefreshRates() const EXCLUDES(mLock);
Steven Thomas2bbaabe2019-08-28 16:08:35 -0700136
Ady Abraham2139f732019-11-13 18:56:40 -0800137 // Returns the lowest refresh rate supported by the device. This won't change at runtime.
138 const RefreshRate& getMinRefreshRate() const { return *mMinSupportedRefreshRate; }
Steven Thomas2bbaabe2019-08-28 16:08:35 -0700139
Ady Abraham2139f732019-11-13 18:56:40 -0800140 // Returns the lowest refresh rate according to the current policy. May change in runtime.
141 const RefreshRate& getMinRefreshRateByPolicy() const EXCLUDES(mLock);
Steven Thomas2bbaabe2019-08-28 16:08:35 -0700142
Ady Abraham2139f732019-11-13 18:56:40 -0800143 // Returns the highest refresh rate supported by the device. This won't change at runtime.
144 const RefreshRate& getMaxRefreshRate() const { return *mMaxSupportedRefreshRate; }
Steven Thomas2bbaabe2019-08-28 16:08:35 -0700145
Ady Abraham2139f732019-11-13 18:56:40 -0800146 // Returns the highest refresh rate according to the current policy. May change in runtime.
147 const RefreshRate& getMaxRefreshRateByPolicy() const EXCLUDES(mLock);
148
149 // Returns the current refresh rate
150 const RefreshRate& getCurrentRefreshRate() const EXCLUDES(mLock);
151
Ana Krulec5d477912020-02-07 12:02:38 -0800152 // Returns the current refresh rate, if allowed. Otherwise the default that is allowed by
153 // the policy.
154 const RefreshRate& getCurrentRefreshRateByPolicy() const;
155
Ady Abraham2139f732019-11-13 18:56:40 -0800156 // Returns the refresh rate that corresponds to a HwcConfigIndexType. This won't change at
157 // runtime.
158 const RefreshRate& getRefreshRateFromConfigId(HwcConfigIndexType configId) const {
159 return mRefreshRates.at(configId);
160 };
161
162 // Stores the current configId the device operates at
163 void setCurrentConfigId(HwcConfigIndexType configId) EXCLUDES(mLock);
Dominik Laskowski22488f62019-03-28 09:53:04 -0700164
Steven Thomas2bbaabe2019-08-28 16:08:35 -0700165 struct InputConfig {
Ady Abraham2139f732019-11-13 18:56:40 -0800166 HwcConfigIndexType configId = HwcConfigIndexType(0);
167 HwcConfigGroupType configGroup = HwcConfigGroupType(0);
Steven Thomas2bbaabe2019-08-28 16:08:35 -0700168 nsecs_t vsyncPeriod = 0;
169 };
Ana Krulec4593b692019-01-11 22:07:25 -0800170
Ana Krulec3f6a2062020-01-23 15:48:01 -0800171 RefreshRateConfigs(const std::vector<InputConfig>& configs,
Ady Abraham2139f732019-11-13 18:56:40 -0800172 HwcConfigIndexType currentHwcConfig);
Ana Krulec3f6a2062020-01-23 15:48:01 -0800173 RefreshRateConfigs(const std::vector<std::shared_ptr<const HWC2::Display::Config>>& configs,
Ady Abraham2139f732019-11-13 18:56:40 -0800174 HwcConfigIndexType currentConfigId);
Ana Krulec4593b692019-01-11 22:07:25 -0800175
Dominik Laskowski22488f62019-03-28 09:53:04 -0700176private:
Ady Abraham2139f732019-11-13 18:56:40 -0800177 void init(const std::vector<InputConfig>& configs, HwcConfigIndexType currentHwcConfig);
178
179 void constructAvailableRefreshRates() REQUIRES(mLock);
180
181 void getSortedRefreshRateList(
182 const std::function<bool(const RefreshRate&)>& shouldAddRefreshRate,
183 std::vector<const RefreshRate*>* outRefreshRates);
184
Ady Abraham34702102020-02-10 14:12:05 -0800185 // Returns the refresh rate with the highest score in the collection specified from begin
186 // to end. If there are more than one with the same highest refresh rate, the first one is
187 // returned.
188 template <typename Iter>
189 const RefreshRate* getBestRefreshRate(Iter begin, Iter end) const;
190
Steven Thomas2bbaabe2019-08-28 16:08:35 -0700191 // The list of refresh rates, indexed by display config ID. This must not change after this
192 // object is initialized.
Ady Abraham2139f732019-11-13 18:56:40 -0800193 AllRefreshRatesMapType mRefreshRates;
194
195 // The list of refresh rates which are available in the current policy, ordered by vsyncPeriod
196 // (the first element is the lowest refresh rate)
197 std::vector<const RefreshRate*> mAvailableRefreshRates GUARDED_BY(mLock);
198
199 // The current config. This will change at runtime. This is set by SurfaceFlinger on
200 // the main thread, and read by the Scheduler (and other objects) on other threads.
201 const RefreshRate* mCurrentRefreshRate GUARDED_BY(mLock);
202
Ana Kruleced3a8cc2019-11-14 00:55:07 +0100203 // The default config. This will change at runtime. This is set by SurfaceFlinger on
Ady Abraham2139f732019-11-13 18:56:40 -0800204 // the main thread, and read by the Scheduler (and other objects) on other threads.
Ana Kruleced3a8cc2019-11-14 00:55:07 +0100205 HwcConfigIndexType mDefaultConfig GUARDED_BY(mLock);
Ady Abraham2139f732019-11-13 18:56:40 -0800206
207 // The min and max FPS allowed by the policy. This will change at runtime and set by
208 // SurfaceFlinger on the main thread.
209 float mMinRefreshRateFps GUARDED_BY(mLock) = 0;
210 float mMaxRefreshRateFps GUARDED_BY(mLock) = std::numeric_limits<float>::max();
211
212 // The min and max refresh rates supported by the device.
213 // This will not change at runtime.
214 const RefreshRate* mMinSupportedRefreshRate;
215 const RefreshRate* mMaxSupportedRefreshRate;
216
Ady Abraham2139f732019-11-13 18:56:40 -0800217 mutable std::mutex mLock;
Ana Krulecb43429d2019-01-09 14:28:51 -0800218};
219
Dominik Laskowski98041832019-08-01 18:35:59 -0700220} // namespace android::scheduler