blob: a8f8e0e2457175d0c04f8d1d58e2c8b5862801b0 [file] [log] [blame]
Ady Abrahamb4b1e0a2019-11-20 18:25:35 -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 */
Ady Abraham2139f732019-11-13 18:56:40 -080016
Ady Abraham8a82ba62020-01-17 12:43:17 -080017// #define LOG_NDEBUG 0
18#define ATRACE_TAG ATRACE_TAG_GRAPHICS
19
Ady Abrahamb0dbdaa2020-01-06 16:19:42 -080020// TODO(b/129481165): remove the #pragma below and fix conversion issues
21#pragma clang diagnostic push
22#pragma clang diagnostic ignored "-Wconversion"
23
Ady Abrahamb4b1e0a2019-11-20 18:25:35 -080024#include "RefreshRateConfigs.h"
Ady Abraham8a82ba62020-01-17 12:43:17 -080025#include <android-base/stringprintf.h>
26#include <utils/Trace.h>
27#include <chrono>
28#include <cmath>
29
30using namespace std::chrono_literals;
Ady Abrahamb4b1e0a2019-11-20 18:25:35 -080031
32namespace android::scheduler {
Ady Abraham2139f732019-11-13 18:56:40 -080033
34using AllRefreshRatesMapType = RefreshRateConfigs::AllRefreshRatesMapType;
Ady Abrahamb4b1e0a2019-11-20 18:25:35 -080035using RefreshRate = RefreshRateConfigs::RefreshRate;
Ady Abrahamb4b1e0a2019-11-20 18:25:35 -080036
Ady Abraham8a82ba62020-01-17 12:43:17 -080037const RefreshRate& RefreshRateConfigs::getRefreshRateForContent(
38 const std::vector<LayerRequirement>& layers) const {
Ady Abraham2139f732019-11-13 18:56:40 -080039 std::lock_guard lock(mLock);
Ady Abraham8a82ba62020-01-17 12:43:17 -080040 float contentFramerate = 0.0f;
41 float explicitContentFramerate = 0.0f;
42 for (const auto& layer : layers) {
43 if (layer.vote == LayerVoteType::Explicit) {
44 if (layer.desiredRefreshRate > explicitContentFramerate) {
45 explicitContentFramerate = layer.desiredRefreshRate;
46 }
47 } else {
48 if (layer.desiredRefreshRate > contentFramerate) {
49 contentFramerate = layer.desiredRefreshRate;
50 }
51 }
52 }
53
54 if (explicitContentFramerate != 0.0f) {
55 contentFramerate = explicitContentFramerate;
56 } else if (contentFramerate == 0.0f) {
57 contentFramerate = mMaxSupportedRefreshRate->fps;
58 }
59 contentFramerate = std::round(contentFramerate);
60 ATRACE_INT("ContentFPS", contentFramerate);
61
Ady Abraham2139f732019-11-13 18:56:40 -080062 // Find the appropriate refresh rate with minimal error
63 auto iter = min_element(mAvailableRefreshRates.cbegin(), mAvailableRefreshRates.cend(),
64 [contentFramerate](const auto& lhs, const auto& rhs) -> bool {
65 return std::abs(lhs->fps - contentFramerate) <
66 std::abs(rhs->fps - contentFramerate);
67 });
Ady Abrahamb4b1e0a2019-11-20 18:25:35 -080068
Ady Abraham2139f732019-11-13 18:56:40 -080069 // Some content aligns better on higher refresh rate. For example for 45fps we should choose
70 // 90Hz config. However we should still prefer a lower refresh rate if the content doesn't
71 // align well with both
72 const RefreshRate* bestSoFar = *iter;
73 constexpr float MARGIN = 0.05f;
74 float ratio = (*iter)->fps / contentFramerate;
75 if (std::abs(std::round(ratio) - ratio) > MARGIN) {
76 while (iter != mAvailableRefreshRates.cend()) {
77 ratio = (*iter)->fps / contentFramerate;
Ady Abrahamb4b1e0a2019-11-20 18:25:35 -080078
Ady Abraham2139f732019-11-13 18:56:40 -080079 if (std::abs(std::round(ratio) - ratio) <= MARGIN) {
80 bestSoFar = *iter;
81 break;
Ady Abrahamb4b1e0a2019-11-20 18:25:35 -080082 }
Ady Abraham2139f732019-11-13 18:56:40 -080083 ++iter;
Ady Abrahamb4b1e0a2019-11-20 18:25:35 -080084 }
85 }
86
Ady Abraham2139f732019-11-13 18:56:40 -080087 return *bestSoFar;
Ady Abrahamb4b1e0a2019-11-20 18:25:35 -080088}
89
Ady Abraham8a82ba62020-01-17 12:43:17 -080090const RefreshRate& RefreshRateConfigs::getRefreshRateForContentV2(
91 const std::vector<LayerRequirement>& layers) const {
92 constexpr nsecs_t MARGIN = std::chrono::nanoseconds(800us).count();
93 ATRACE_CALL();
94 ALOGV("getRefreshRateForContent %zu layers", layers.size());
95
96 std::lock_guard lock(mLock);
97
98 int noVoteLayers = 0;
99 int minVoteLayers = 0;
100 int maxVoteLayers = 0;
101 int explicitVoteLayers = 0;
102 for (const auto& layer : layers) {
103 if (layer.vote == LayerVoteType::NoVote)
104 noVoteLayers++;
105 else if (layer.vote == LayerVoteType::Min)
106 minVoteLayers++;
107 else if (layer.vote == LayerVoteType::Max)
108 maxVoteLayers++;
109 else if (layer.vote == LayerVoteType::Explicit)
110 explicitVoteLayers++;
111 }
112
113 // Only if all layers want Min we should return Min
114 if (noVoteLayers + minVoteLayers == layers.size()) {
115 return *mAvailableRefreshRates.front();
116 }
117
118 // If we have some Max layers and no Explicit we should return Max
119 if (maxVoteLayers > 0 && explicitVoteLayers == 0) {
120 return *mAvailableRefreshRates.back();
121 }
122
123 // Find the best refresh rate based on score
124 std::vector<std::pair<const RefreshRate*, float>> scores;
125 scores.reserve(mAvailableRefreshRates.size());
126
127 for (const auto refreshRate : mAvailableRefreshRates) {
128 scores.emplace_back(refreshRate, 0.0f);
129 }
130
131 for (const auto& layer : layers) {
132 if (layer.vote == LayerVoteType::NoVote || layer.vote == LayerVoteType::Min ||
133 layer.vote == LayerVoteType::Max) {
134 continue;
135 }
136
137 // If we have Explicit layers, ignore the Huristic ones
138 if (explicitVoteLayers > 0 && layer.vote == LayerVoteType::Heuristic) {
139 continue;
140 }
141
142 for (auto& [refreshRate, overallScore] : scores) {
143 const auto displayPeriod = refreshRate->vsyncPeriod;
144 const auto layerPeriod = 1e9f / layer.desiredRefreshRate;
145
146 // Calculate how many display vsyncs we need to present a single frame for this layer
147 auto [displayFramesQuot, displayFramesRem] = std::div(layerPeriod, displayPeriod);
148 if (displayFramesRem <= MARGIN ||
149 std::abs(displayFramesRem - displayPeriod) <= MARGIN) {
150 displayFramesQuot++;
151 displayFramesRem = 0;
152 }
153
154 float layerScore;
155 if (displayFramesRem == 0) {
156 // Layer desired refresh rate matches the display rate.
157 layerScore = layer.weight * 1.0f;
158 } else if (displayFramesQuot == 0) {
159 // Layer desired refresh rate is higher the display rate.
160 layerScore = layer.weight * layerPeriod / displayPeriod;
161 } else {
162 // Layer desired refresh rate is lower the display rate. Check how well it fits the
163 // cadence
164 auto diff = std::abs(displayFramesRem - (displayPeriod - displayFramesRem));
165 int iter = 2;
166 static constexpr size_t MAX_ITERATOR = 10; // Stop calculating when score < 0.1
167 while (diff > MARGIN && iter < MAX_ITERATOR) {
168 diff = diff - (displayPeriod - diff);
169 iter++;
170 }
171
172 layerScore = layer.weight * 1.0f / iter;
173 }
174
175 ALOGV("%s (weight %.2f) %.2fHz gives %s score of %.2f", layer.name.c_str(),
176 layer.weight, 1e9f / layerPeriod, refreshRate->name.c_str(), layerScore);
177 overallScore += layerScore;
178 }
179 }
180
181 float max = 0;
182 const RefreshRate* bestRefreshRate = nullptr;
183 for (const auto [refreshRate, score] : scores) {
184 ALOGV("%s scores %.2f", refreshRate->name.c_str(), score);
185
186 ATRACE_INT(refreshRate->name.c_str(), std::round(score * 100));
187
188 if (score > max) {
189 max = score;
190 bestRefreshRate = refreshRate;
191 }
192 }
193
194 return bestRefreshRate == nullptr ? *mCurrentRefreshRate : *bestRefreshRate;
195}
196
Ady Abraham2139f732019-11-13 18:56:40 -0800197const AllRefreshRatesMapType& RefreshRateConfigs::getAllRefreshRates() const {
198 return mRefreshRates;
199}
200
201const RefreshRate& RefreshRateConfigs::getMinRefreshRateByPolicy() const {
202 std::lock_guard lock(mLock);
203 if (!mRefreshRateSwitching) {
204 return *mCurrentRefreshRate;
205 } else {
206 return *mAvailableRefreshRates.front();
207 }
208}
209
210const RefreshRate& RefreshRateConfigs::getMaxRefreshRateByPolicy() const {
211 std::lock_guard lock(mLock);
212 if (!mRefreshRateSwitching) {
213 return *mCurrentRefreshRate;
214 } else {
215 return *mAvailableRefreshRates.back();
216 }
217}
218
219const RefreshRate& RefreshRateConfigs::getCurrentRefreshRate() const {
220 std::lock_guard lock(mLock);
221 return *mCurrentRefreshRate;
222}
223
224void RefreshRateConfigs::setCurrentConfigId(HwcConfigIndexType configId) {
225 std::lock_guard lock(mLock);
226 mCurrentRefreshRate = &mRefreshRates.at(configId);
Ady Abrahamb4b1e0a2019-11-20 18:25:35 -0800227}
228
229RefreshRateConfigs::RefreshRateConfigs(bool refreshRateSwitching,
Ady Abraham2139f732019-11-13 18:56:40 -0800230 const std::vector<InputConfig>& configs,
231 HwcConfigIndexType currentHwcConfig)
232 : mRefreshRateSwitching(refreshRateSwitching) {
233 init(configs, currentHwcConfig);
Ady Abrahamb4b1e0a2019-11-20 18:25:35 -0800234}
235
236RefreshRateConfigs::RefreshRateConfigs(
237 bool refreshRateSwitching,
238 const std::vector<std::shared_ptr<const HWC2::Display::Config>>& configs,
Ady Abraham2139f732019-11-13 18:56:40 -0800239 HwcConfigIndexType currentConfigId)
240 : mRefreshRateSwitching(refreshRateSwitching) {
Ady Abrahamb4b1e0a2019-11-20 18:25:35 -0800241 std::vector<InputConfig> inputConfigs;
Ady Abraham2139f732019-11-13 18:56:40 -0800242 for (auto configId = HwcConfigIndexType(0); configId < HwcConfigIndexType(configs.size());
243 ++configId) {
244 auto configGroup = HwcConfigGroupType(configs[configId.value()]->getConfigGroup());
245 inputConfigs.push_back(
246 {configId, configGroup, configs[configId.value()]->getVsyncPeriod()});
Ady Abrahamb4b1e0a2019-11-20 18:25:35 -0800247 }
Ady Abraham2139f732019-11-13 18:56:40 -0800248 init(inputConfigs, currentConfigId);
Ady Abrahamb4b1e0a2019-11-20 18:25:35 -0800249}
250
Ana Kruleced3a8cc2019-11-14 00:55:07 +0100251status_t RefreshRateConfigs::setPolicy(HwcConfigIndexType defaultConfigId, float minRefreshRate,
252 float maxRefreshRate, bool* outPolicyChanged) {
Ady Abraham2139f732019-11-13 18:56:40 -0800253 std::lock_guard lock(mLock);
Ana Kruleced3a8cc2019-11-14 00:55:07 +0100254 bool policyChanged = defaultConfigId != mDefaultConfig ||
255 minRefreshRate != mMinRefreshRateFps || maxRefreshRate != mMaxRefreshRateFps;
256 if (outPolicyChanged) {
257 *outPolicyChanged = policyChanged;
258 }
259 if (!policyChanged) {
260 return NO_ERROR;
261 }
262 // defaultConfigId must be a valid config ID, and within the given refresh rate range.
263 if (mRefreshRates.count(defaultConfigId) == 0) {
264 return BAD_VALUE;
265 }
266 const RefreshRate& refreshRate = mRefreshRates.at(defaultConfigId);
Ana Krulec72f0d6e2020-01-06 15:24:47 -0800267 if (!refreshRate.inPolicy(minRefreshRate, maxRefreshRate)) {
Ana Kruleced3a8cc2019-11-14 00:55:07 +0100268 return BAD_VALUE;
269 }
270 mDefaultConfig = defaultConfigId;
Ady Abraham2139f732019-11-13 18:56:40 -0800271 mMinRefreshRateFps = minRefreshRate;
272 mMaxRefreshRateFps = maxRefreshRate;
273 constructAvailableRefreshRates();
Ana Kruleced3a8cc2019-11-14 00:55:07 +0100274 return NO_ERROR;
275}
276
277void RefreshRateConfigs::getPolicy(HwcConfigIndexType* defaultConfigId, float* minRefreshRate,
278 float* maxRefreshRate) const {
279 std::lock_guard lock(mLock);
280 *defaultConfigId = mDefaultConfig;
281 *minRefreshRate = mMinRefreshRateFps;
282 *maxRefreshRate = mMaxRefreshRateFps;
283}
284
285bool RefreshRateConfigs::isConfigAllowed(HwcConfigIndexType config) const {
286 std::lock_guard lock(mLock);
287 for (const RefreshRate* refreshRate : mAvailableRefreshRates) {
288 if (refreshRate->configId == config) {
289 return true;
290 }
291 }
292 return false;
Ady Abraham2139f732019-11-13 18:56:40 -0800293}
294
295void RefreshRateConfigs::getSortedRefreshRateList(
296 const std::function<bool(const RefreshRate&)>& shouldAddRefreshRate,
297 std::vector<const RefreshRate*>* outRefreshRates) {
298 outRefreshRates->clear();
299 outRefreshRates->reserve(mRefreshRates.size());
300 for (const auto& [type, refreshRate] : mRefreshRates) {
301 if (shouldAddRefreshRate(refreshRate)) {
302 ALOGV("getSortedRefreshRateList: config %d added to list policy",
303 refreshRate.configId.value());
304 outRefreshRates->push_back(&refreshRate);
305 }
306 }
307
308 std::sort(outRefreshRates->begin(), outRefreshRates->end(),
309 [](const auto refreshRate1, const auto refreshRate2) {
310 return refreshRate1->vsyncPeriod > refreshRate2->vsyncPeriod;
311 });
312}
313
314void RefreshRateConfigs::constructAvailableRefreshRates() {
315 // Filter configs based on current policy and sort based on vsync period
Ana Kruleced3a8cc2019-11-14 00:55:07 +0100316 HwcConfigGroupType group = mRefreshRates.at(mDefaultConfig).configGroup;
Ady Abraham8a82ba62020-01-17 12:43:17 -0800317 ALOGV("constructAvailableRefreshRates: default %d group %d min %.2f max %.2f",
318 mDefaultConfig.value(), group.value(), mMinRefreshRateFps, mMaxRefreshRateFps);
Ady Abraham2139f732019-11-13 18:56:40 -0800319 getSortedRefreshRateList(
Ana Kruleced3a8cc2019-11-14 00:55:07 +0100320 [&](const RefreshRate& refreshRate) REQUIRES(mLock) {
Ana Krulec72f0d6e2020-01-06 15:24:47 -0800321 return refreshRate.configGroup == group &&
322 refreshRate.inPolicy(mMinRefreshRateFps, mMaxRefreshRateFps);
Ady Abraham2139f732019-11-13 18:56:40 -0800323 },
324 &mAvailableRefreshRates);
Ady Abraham8a82ba62020-01-17 12:43:17 -0800325
326 std::string availableRefreshRates;
327 for (const auto& refreshRate : mAvailableRefreshRates) {
328 base::StringAppendF(&availableRefreshRates, "%s ", refreshRate->name.c_str());
329 }
330
331 ALOGV("Available refresh rates: %s", availableRefreshRates.c_str());
Ana Kruleced3a8cc2019-11-14 00:55:07 +0100332 LOG_ALWAYS_FATAL_IF(mAvailableRefreshRates.empty(),
333 "No compatible display configs for default=%d min=%.0f max=%.0f",
334 mDefaultConfig.value(), mMinRefreshRateFps, mMaxRefreshRateFps);
Ady Abraham2139f732019-11-13 18:56:40 -0800335}
336
337// NO_THREAD_SAFETY_ANALYSIS since this is called from the constructor
338void RefreshRateConfigs::init(const std::vector<InputConfig>& configs,
339 HwcConfigIndexType currentHwcConfig) NO_THREAD_SAFETY_ANALYSIS {
Ady Abrahamb4b1e0a2019-11-20 18:25:35 -0800340 LOG_ALWAYS_FATAL_IF(configs.empty());
Ady Abraham2139f732019-11-13 18:56:40 -0800341 LOG_ALWAYS_FATAL_IF(currentHwcConfig.value() >= configs.size());
Ady Abrahamb4b1e0a2019-11-20 18:25:35 -0800342
Ady Abraham2139f732019-11-13 18:56:40 -0800343 auto buildRefreshRate = [&](InputConfig config) -> RefreshRate {
344 const float fps = 1e9f / config.vsyncPeriod;
345 return RefreshRate(config.configId, config.vsyncPeriod, config.configGroup,
346 base::StringPrintf("%2.ffps", fps), fps);
Ady Abrahamb4b1e0a2019-11-20 18:25:35 -0800347 };
348
Ady Abraham2139f732019-11-13 18:56:40 -0800349 for (const auto& config : configs) {
350 mRefreshRates.emplace(config.configId, buildRefreshRate(config));
351 if (config.configId == currentHwcConfig) {
352 mCurrentRefreshRate = &mRefreshRates.at(config.configId);
Ady Abraham2139f732019-11-13 18:56:40 -0800353 }
Ady Abrahamb4b1e0a2019-11-20 18:25:35 -0800354 }
355
Ady Abraham2139f732019-11-13 18:56:40 -0800356 std::vector<const RefreshRate*> sortedConfigs;
357 getSortedRefreshRateList([](const RefreshRate&) { return true; }, &sortedConfigs);
Ana Kruleced3a8cc2019-11-14 00:55:07 +0100358 mDefaultConfig = currentHwcConfig;
Ady Abraham2139f732019-11-13 18:56:40 -0800359 mMinSupportedRefreshRate = sortedConfigs.front();
360 mMaxSupportedRefreshRate = sortedConfigs.back();
361 constructAvailableRefreshRates();
Ady Abrahamb4b1e0a2019-11-20 18:25:35 -0800362}
363
Ady Abraham2139f732019-11-13 18:56:40 -0800364} // namespace android::scheduler
Ady Abrahamb0dbdaa2020-01-06 16:19:42 -0800365// TODO(b/129481165): remove the #pragma below and fix conversion issues
366#pragma clang diagnostic pop // ignored "-Wconversion"