blob: ae4c3e5fd9fc86e79e1bcd0836bb932a6188fa7c [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 Abrahamb4b1e0a2019-11-20 18:25:35 -080020#include "RefreshRateConfigs.h"
Ady Abraham8a82ba62020-01-17 12:43:17 -080021#include <android-base/stringprintf.h>
22#include <utils/Trace.h>
23#include <chrono>
24#include <cmath>
25
Ady Abrahamb4b1e0a2019-11-20 18:25:35 -080026namespace android::scheduler {
Ady Abraham2139f732019-11-13 18:56:40 -080027
28using AllRefreshRatesMapType = RefreshRateConfigs::AllRefreshRatesMapType;
Ady Abrahamb4b1e0a2019-11-20 18:25:35 -080029using RefreshRate = RefreshRateConfigs::RefreshRate;
Ady Abrahamb4b1e0a2019-11-20 18:25:35 -080030
Ady Abraham8a82ba62020-01-17 12:43:17 -080031const RefreshRate& RefreshRateConfigs::getRefreshRateForContent(
32 const std::vector<LayerRequirement>& layers) const {
Ady Abraham2139f732019-11-13 18:56:40 -080033 std::lock_guard lock(mLock);
Ady Abrahamdec1a412020-01-24 10:23:50 -080034 int contentFramerate = 0;
35 int explicitContentFramerate = 0;
Ady Abraham8a82ba62020-01-17 12:43:17 -080036 for (const auto& layer : layers) {
Ady Abrahamdec1a412020-01-24 10:23:50 -080037 const auto desiredRefreshRateRound = round<int>(layer.desiredRefreshRate);
Ady Abraham71c437d2020-01-31 15:56:57 -080038 if (layer.vote == LayerVoteType::ExplicitDefault ||
39 layer.vote == LayerVoteType::ExplicitExactOrMultiple) {
Ady Abrahamdec1a412020-01-24 10:23:50 -080040 if (desiredRefreshRateRound > explicitContentFramerate) {
41 explicitContentFramerate = desiredRefreshRateRound;
Ady Abraham8a82ba62020-01-17 12:43:17 -080042 }
43 } else {
Ady Abrahamdec1a412020-01-24 10:23:50 -080044 if (desiredRefreshRateRound > contentFramerate) {
45 contentFramerate = desiredRefreshRateRound;
Ady Abraham8a82ba62020-01-17 12:43:17 -080046 }
47 }
48 }
49
Ady Abrahamdec1a412020-01-24 10:23:50 -080050 if (explicitContentFramerate != 0) {
Ady Abraham8a82ba62020-01-17 12:43:17 -080051 contentFramerate = explicitContentFramerate;
Ady Abrahamdec1a412020-01-24 10:23:50 -080052 } else if (contentFramerate == 0) {
53 contentFramerate = round<int>(mMaxSupportedRefreshRate->fps);
Ady Abraham8a82ba62020-01-17 12:43:17 -080054 }
Ady Abraham8a82ba62020-01-17 12:43:17 -080055 ATRACE_INT("ContentFPS", contentFramerate);
56
Ady Abraham2139f732019-11-13 18:56:40 -080057 // Find the appropriate refresh rate with minimal error
58 auto iter = min_element(mAvailableRefreshRates.cbegin(), mAvailableRefreshRates.cend(),
59 [contentFramerate](const auto& lhs, const auto& rhs) -> bool {
60 return std::abs(lhs->fps - contentFramerate) <
61 std::abs(rhs->fps - contentFramerate);
62 });
Ady Abrahamb4b1e0a2019-11-20 18:25:35 -080063
Ady Abraham2139f732019-11-13 18:56:40 -080064 // Some content aligns better on higher refresh rate. For example for 45fps we should choose
65 // 90Hz config. However we should still prefer a lower refresh rate if the content doesn't
66 // align well with both
67 const RefreshRate* bestSoFar = *iter;
68 constexpr float MARGIN = 0.05f;
69 float ratio = (*iter)->fps / contentFramerate;
70 if (std::abs(std::round(ratio) - ratio) > MARGIN) {
71 while (iter != mAvailableRefreshRates.cend()) {
72 ratio = (*iter)->fps / contentFramerate;
Ady Abrahamb4b1e0a2019-11-20 18:25:35 -080073
Ady Abraham2139f732019-11-13 18:56:40 -080074 if (std::abs(std::round(ratio) - ratio) <= MARGIN) {
75 bestSoFar = *iter;
76 break;
Ady Abrahamb4b1e0a2019-11-20 18:25:35 -080077 }
Ady Abraham2139f732019-11-13 18:56:40 -080078 ++iter;
Ady Abrahamb4b1e0a2019-11-20 18:25:35 -080079 }
80 }
81
Ady Abraham2139f732019-11-13 18:56:40 -080082 return *bestSoFar;
Ady Abrahamb4b1e0a2019-11-20 18:25:35 -080083}
84
Ady Abraham4ccdcb42020-02-11 17:34:34 -080085std::pair<nsecs_t, nsecs_t> RefreshRateConfigs::getDisplayFrames(nsecs_t layerPeriod,
86 nsecs_t displayPeriod) const {
87 auto [displayFramesQuot, displayFramesRem] = std::div(layerPeriod, displayPeriod);
88 if (displayFramesRem <= MARGIN_FOR_PERIOD_CALCULATION ||
89 std::abs(displayFramesRem - displayPeriod) <= MARGIN_FOR_PERIOD_CALCULATION) {
90 displayFramesQuot++;
91 displayFramesRem = 0;
92 }
93
94 return {displayFramesQuot, displayFramesRem};
95}
96
Ady Abraham8a82ba62020-01-17 12:43:17 -080097const RefreshRate& RefreshRateConfigs::getRefreshRateForContentV2(
Ady Abraham6fb599b2020-03-05 13:48:22 -080098 const std::vector<LayerRequirement>& layers, bool touchActive,
99 bool* touchConsidered) const {
Ady Abraham8a82ba62020-01-17 12:43:17 -0800100 ATRACE_CALL();
101 ALOGV("getRefreshRateForContent %zu layers", layers.size());
102
Ady Abraham6fb599b2020-03-05 13:48:22 -0800103 *touchConsidered = false;
Ady Abraham8a82ba62020-01-17 12:43:17 -0800104 std::lock_guard lock(mLock);
105
Ana Krulec3d367c82020-02-25 15:02:01 -0800106 // If there are not layers, there is not content detection, so return the current
107 // refresh rate.
108 if (layers.empty()) {
Ady Abraham6fb599b2020-03-05 13:48:22 -0800109 *touchConsidered = touchActive;
110 return touchActive ? *mAvailableRefreshRates.back() : getCurrentRefreshRateByPolicyLocked();
Ana Krulec3d367c82020-02-25 15:02:01 -0800111 }
112
Ady Abraham8a82ba62020-01-17 12:43:17 -0800113 int noVoteLayers = 0;
114 int minVoteLayers = 0;
115 int maxVoteLayers = 0;
Ady Abraham71c437d2020-01-31 15:56:57 -0800116 int explicitDefaultVoteLayers = 0;
117 int explicitExactOrMultipleVoteLayers = 0;
Ady Abraham6fb599b2020-03-05 13:48:22 -0800118 float maxExplicitWeight = 0;
Ady Abraham8a82ba62020-01-17 12:43:17 -0800119 for (const auto& layer : layers) {
Ady Abraham6fb599b2020-03-05 13:48:22 -0800120 if (layer.vote == LayerVoteType::NoVote) {
Ady Abraham8a82ba62020-01-17 12:43:17 -0800121 noVoteLayers++;
Ady Abraham6fb599b2020-03-05 13:48:22 -0800122 } else if (layer.vote == LayerVoteType::Min) {
Ady Abraham8a82ba62020-01-17 12:43:17 -0800123 minVoteLayers++;
Ady Abraham6fb599b2020-03-05 13:48:22 -0800124 } else if (layer.vote == LayerVoteType::Max) {
Ady Abraham8a82ba62020-01-17 12:43:17 -0800125 maxVoteLayers++;
Ady Abraham6fb599b2020-03-05 13:48:22 -0800126 } else if (layer.vote == LayerVoteType::ExplicitDefault) {
Ady Abraham71c437d2020-01-31 15:56:57 -0800127 explicitDefaultVoteLayers++;
Ady Abraham6fb599b2020-03-05 13:48:22 -0800128 maxExplicitWeight = std::max(maxExplicitWeight, layer.weight);
129 } else if (layer.vote == LayerVoteType::ExplicitExactOrMultiple) {
Ady Abraham71c437d2020-01-31 15:56:57 -0800130 explicitExactOrMultipleVoteLayers++;
Ady Abraham6fb599b2020-03-05 13:48:22 -0800131 maxExplicitWeight = std::max(maxExplicitWeight, layer.weight);
132 }
133 }
134
135 // Consider the touch event if there are no ExplicitDefault layers.
136 // ExplicitDefault are mostly interactive (as opposed to ExplicitExactOrMultiple)
137 // and therefore if those posted an explicit vote we should not change it
138 // if get get a touch event.
139 if (touchActive && explicitDefaultVoteLayers == 0) {
140 *touchConsidered = true;
141 return *mAvailableRefreshRates.back();
Ady Abraham8a82ba62020-01-17 12:43:17 -0800142 }
143
144 // Only if all layers want Min we should return Min
145 if (noVoteLayers + minVoteLayers == layers.size()) {
146 return *mAvailableRefreshRates.front();
147 }
148
Ady Abraham8a82ba62020-01-17 12:43:17 -0800149 // Find the best refresh rate based on score
150 std::vector<std::pair<const RefreshRate*, float>> scores;
151 scores.reserve(mAvailableRefreshRates.size());
152
153 for (const auto refreshRate : mAvailableRefreshRates) {
154 scores.emplace_back(refreshRate, 0.0f);
155 }
156
157 for (const auto& layer : layers) {
Ady Abrahamf6b77072020-01-30 14:22:54 -0800158 ALOGV("Calculating score for %s (type: %d)", layer.name.c_str(), layer.vote);
Ady Abraham4ccdcb42020-02-11 17:34:34 -0800159 if (layer.vote == LayerVoteType::NoVote || layer.vote == LayerVoteType::Min) {
Ady Abraham8a82ba62020-01-17 12:43:17 -0800160 continue;
161 }
162
Ady Abraham71c437d2020-01-31 15:56:57 -0800163 auto weight = layer.weight;
Ady Abraham71c437d2020-01-31 15:56:57 -0800164
Ady Abraham4ccdcb42020-02-11 17:34:34 -0800165 for (auto i = 0u; i < scores.size(); i++) {
166 // If the layer wants Max, give higher score to the higher refresh rate
167 if (layer.vote == LayerVoteType::Max) {
168 const auto ratio = scores[i].first->fps / scores.back().first->fps;
169 // use ratio^2 to get a lower score the more we get further from peak
170 const auto layerScore = ratio * ratio;
171 ALOGV("%s (Max, weight %.2f) gives %s score of %.2f", layer.name.c_str(), weight,
172 scores[i].first->name.c_str(), layerScore);
173 scores[i].second += weight * layerScore;
174 continue;
Ady Abraham71c437d2020-01-31 15:56:57 -0800175 }
Ady Abraham8a82ba62020-01-17 12:43:17 -0800176
Ady Abraham4ccdcb42020-02-11 17:34:34 -0800177 const auto displayPeriod = scores[i].first->vsyncPeriod;
Ady Abrahamdec1a412020-01-24 10:23:50 -0800178 const auto layerPeriod = round<nsecs_t>(1e9f / layer.desiredRefreshRate);
Ady Abraham4ccdcb42020-02-11 17:34:34 -0800179 if (layer.vote == LayerVoteType::ExplicitDefault) {
180 const auto layerScore = [&]() {
181 const auto [displayFramesQuot, displayFramesRem] =
182 getDisplayFrames(layerPeriod, displayPeriod);
183 if (displayFramesQuot == 0) {
184 // Layer desired refresh rate is higher the display rate.
185 return static_cast<float>(layerPeriod) / static_cast<float>(displayPeriod);
186 }
Ady Abraham8a82ba62020-01-17 12:43:17 -0800187
Ady Abraham4ccdcb42020-02-11 17:34:34 -0800188 return 1.0f -
189 (static_cast<float>(displayFramesRem) /
190 static_cast<float>(layerPeriod));
191 }();
192
193 ALOGV("%s (ExplicitDefault, weight %.2f) %.2fHz gives %s score of %.2f",
194 layer.name.c_str(), weight, 1e9f / layerPeriod, scores[i].first->name.c_str(),
195 layerScore);
196 scores[i].second += weight * layerScore;
197 continue;
Ady Abraham8a82ba62020-01-17 12:43:17 -0800198 }
199
Ady Abraham4ccdcb42020-02-11 17:34:34 -0800200 if (layer.vote == LayerVoteType::ExplicitExactOrMultiple ||
201 layer.vote == LayerVoteType::Heuristic) {
202 const auto layerScore = [&]() {
203 // Calculate how many display vsyncs we need to present a single frame for this
204 // layer
205 const auto [displayFramesQuot, displayFramesRem] =
206 getDisplayFrames(layerPeriod, displayPeriod);
207 static constexpr size_t MAX_FRAMES_TO_FIT =
208 10; // Stop calculating when score < 0.1
209 if (displayFramesRem == 0) {
210 // Layer desired refresh rate matches the display rate.
211 return 1.0f;
212 }
Ady Abraham8a82ba62020-01-17 12:43:17 -0800213
Ady Abraham4ccdcb42020-02-11 17:34:34 -0800214 if (displayFramesQuot == 0) {
215 // Layer desired refresh rate is higher the display rate.
216 return (static_cast<float>(layerPeriod) /
217 static_cast<float>(displayPeriod)) *
218 (1.0f / (MAX_FRAMES_TO_FIT + 1));
219 }
220
221 // Layer desired refresh rate is lower the display rate. Check how well it fits
222 // the cadence
223 auto diff = std::abs(displayFramesRem - (displayPeriod - displayFramesRem));
224 int iter = 2;
225 while (diff > MARGIN_FOR_PERIOD_CALCULATION && iter < MAX_FRAMES_TO_FIT) {
226 diff = diff - (displayPeriod - diff);
227 iter++;
228 }
229
230 return 1.0f / iter;
231 }();
232 ALOGV("%s (ExplicitExactOrMultiple, weight %.2f) %.2fHz gives %s score of %.2f",
233 layer.name.c_str(), weight, 1e9f / layerPeriod, scores[i].first->name.c_str(),
234 layerScore);
235 scores[i].second += weight * layerScore;
236 continue;
Ady Abraham8a82ba62020-01-17 12:43:17 -0800237 }
Ady Abraham8a82ba62020-01-17 12:43:17 -0800238 }
239 }
240
Ady Abraham34702102020-02-10 14:12:05 -0800241 // Now that we scored all the refresh rates we need to pick the one that got the highest score.
242 // In case of a tie we will pick the higher refresh rate if any of the layers wanted Max,
243 // or the lower otherwise.
244 const RefreshRate* bestRefreshRate = maxVoteLayers > 0
245 ? getBestRefreshRate(scores.rbegin(), scores.rend())
246 : getBestRefreshRate(scores.begin(), scores.end());
247
Ady Abrahamde7156e2020-02-28 17:29:39 -0800248 return *bestRefreshRate;
Ady Abraham34702102020-02-10 14:12:05 -0800249}
250
251template <typename Iter>
252const RefreshRate* RefreshRateConfigs::getBestRefreshRate(Iter begin, Iter end) const {
Ady Abrahamde7156e2020-02-28 17:29:39 -0800253 const RefreshRate* bestRefreshRate = begin->first;
254 float max = begin->second;
Ady Abraham34702102020-02-10 14:12:05 -0800255 for (auto i = begin; i != end; ++i) {
256 const auto [refreshRate, score] = *i;
Ady Abraham8a82ba62020-01-17 12:43:17 -0800257 ALOGV("%s scores %.2f", refreshRate->name.c_str(), score);
258
Ady Abrahamdec1a412020-01-24 10:23:50 -0800259 ATRACE_INT(refreshRate->name.c_str(), round<int>(score * 100));
Ady Abraham8a82ba62020-01-17 12:43:17 -0800260
261 if (score > max) {
262 max = score;
263 bestRefreshRate = refreshRate;
264 }
265 }
266
Ady Abraham34702102020-02-10 14:12:05 -0800267 return bestRefreshRate;
Ady Abraham8a82ba62020-01-17 12:43:17 -0800268}
269
Ady Abraham2139f732019-11-13 18:56:40 -0800270const AllRefreshRatesMapType& RefreshRateConfigs::getAllRefreshRates() const {
271 return mRefreshRates;
272}
273
274const RefreshRate& RefreshRateConfigs::getMinRefreshRateByPolicy() const {
275 std::lock_guard lock(mLock);
Ana Krulec3f6a2062020-01-23 15:48:01 -0800276 return *mAvailableRefreshRates.front();
Ady Abraham2139f732019-11-13 18:56:40 -0800277}
278
279const RefreshRate& RefreshRateConfigs::getMaxRefreshRateByPolicy() const {
280 std::lock_guard lock(mLock);
Ady Abraham2139f732019-11-13 18:56:40 -0800281 return *mAvailableRefreshRates.back();
Ady Abraham2139f732019-11-13 18:56:40 -0800282}
283
284const RefreshRate& RefreshRateConfigs::getCurrentRefreshRate() const {
285 std::lock_guard lock(mLock);
286 return *mCurrentRefreshRate;
287}
288
Ana Krulec5d477912020-02-07 12:02:38 -0800289const RefreshRate& RefreshRateConfigs::getCurrentRefreshRateByPolicy() const {
290 std::lock_guard lock(mLock);
Ana Krulec3d367c82020-02-25 15:02:01 -0800291 return getCurrentRefreshRateByPolicyLocked();
292}
293
294const RefreshRate& RefreshRateConfigs::getCurrentRefreshRateByPolicyLocked() const {
Ana Krulec5d477912020-02-07 12:02:38 -0800295 if (std::find(mAvailableRefreshRates.begin(), mAvailableRefreshRates.end(),
296 mCurrentRefreshRate) != mAvailableRefreshRates.end()) {
297 return *mCurrentRefreshRate;
298 }
299 return mRefreshRates.at(mDefaultConfig);
300}
301
Ady Abraham2139f732019-11-13 18:56:40 -0800302void RefreshRateConfigs::setCurrentConfigId(HwcConfigIndexType configId) {
303 std::lock_guard lock(mLock);
304 mCurrentRefreshRate = &mRefreshRates.at(configId);
Ady Abrahamb4b1e0a2019-11-20 18:25:35 -0800305}
306
Ana Krulec3f6a2062020-01-23 15:48:01 -0800307RefreshRateConfigs::RefreshRateConfigs(const std::vector<InputConfig>& configs,
308 HwcConfigIndexType currentHwcConfig) {
Ady Abraham2139f732019-11-13 18:56:40 -0800309 init(configs, currentHwcConfig);
Ady Abrahamb4b1e0a2019-11-20 18:25:35 -0800310}
311
312RefreshRateConfigs::RefreshRateConfigs(
Ady Abrahamb4b1e0a2019-11-20 18:25:35 -0800313 const std::vector<std::shared_ptr<const HWC2::Display::Config>>& configs,
Ana Krulec3f6a2062020-01-23 15:48:01 -0800314 HwcConfigIndexType currentConfigId) {
Ady Abrahamb4b1e0a2019-11-20 18:25:35 -0800315 std::vector<InputConfig> inputConfigs;
Ady Abrahamdec1a412020-01-24 10:23:50 -0800316 for (size_t configId = 0; configId < configs.size(); ++configId) {
317 auto configGroup = HwcConfigGroupType(configs[configId]->getConfigGroup());
318 inputConfigs.push_back({HwcConfigIndexType(static_cast<int>(configId)), configGroup,
319 configs[configId]->getVsyncPeriod()});
Ady Abrahamb4b1e0a2019-11-20 18:25:35 -0800320 }
Ady Abraham2139f732019-11-13 18:56:40 -0800321 init(inputConfigs, currentConfigId);
Ady Abrahamb4b1e0a2019-11-20 18:25:35 -0800322}
323
Ana Kruleced3a8cc2019-11-14 00:55:07 +0100324status_t RefreshRateConfigs::setPolicy(HwcConfigIndexType defaultConfigId, float minRefreshRate,
325 float maxRefreshRate, bool* outPolicyChanged) {
Ady Abraham2139f732019-11-13 18:56:40 -0800326 std::lock_guard lock(mLock);
Ana Kruleced3a8cc2019-11-14 00:55:07 +0100327 bool policyChanged = defaultConfigId != mDefaultConfig ||
328 minRefreshRate != mMinRefreshRateFps || maxRefreshRate != mMaxRefreshRateFps;
329 if (outPolicyChanged) {
330 *outPolicyChanged = policyChanged;
331 }
332 if (!policyChanged) {
333 return NO_ERROR;
334 }
335 // defaultConfigId must be a valid config ID, and within the given refresh rate range.
336 if (mRefreshRates.count(defaultConfigId) == 0) {
337 return BAD_VALUE;
338 }
339 const RefreshRate& refreshRate = mRefreshRates.at(defaultConfigId);
Ana Krulec72f0d6e2020-01-06 15:24:47 -0800340 if (!refreshRate.inPolicy(minRefreshRate, maxRefreshRate)) {
Ana Kruleced3a8cc2019-11-14 00:55:07 +0100341 return BAD_VALUE;
342 }
343 mDefaultConfig = defaultConfigId;
Ady Abraham2139f732019-11-13 18:56:40 -0800344 mMinRefreshRateFps = minRefreshRate;
345 mMaxRefreshRateFps = maxRefreshRate;
346 constructAvailableRefreshRates();
Ana Kruleced3a8cc2019-11-14 00:55:07 +0100347 return NO_ERROR;
348}
349
350void RefreshRateConfigs::getPolicy(HwcConfigIndexType* defaultConfigId, float* minRefreshRate,
351 float* maxRefreshRate) const {
352 std::lock_guard lock(mLock);
353 *defaultConfigId = mDefaultConfig;
354 *minRefreshRate = mMinRefreshRateFps;
355 *maxRefreshRate = mMaxRefreshRateFps;
356}
357
358bool RefreshRateConfigs::isConfigAllowed(HwcConfigIndexType config) const {
359 std::lock_guard lock(mLock);
360 for (const RefreshRate* refreshRate : mAvailableRefreshRates) {
361 if (refreshRate->configId == config) {
362 return true;
363 }
364 }
365 return false;
Ady Abraham2139f732019-11-13 18:56:40 -0800366}
367
368void RefreshRateConfigs::getSortedRefreshRateList(
369 const std::function<bool(const RefreshRate&)>& shouldAddRefreshRate,
370 std::vector<const RefreshRate*>* outRefreshRates) {
371 outRefreshRates->clear();
372 outRefreshRates->reserve(mRefreshRates.size());
373 for (const auto& [type, refreshRate] : mRefreshRates) {
374 if (shouldAddRefreshRate(refreshRate)) {
375 ALOGV("getSortedRefreshRateList: config %d added to list policy",
376 refreshRate.configId.value());
377 outRefreshRates->push_back(&refreshRate);
378 }
379 }
380
381 std::sort(outRefreshRates->begin(), outRefreshRates->end(),
382 [](const auto refreshRate1, const auto refreshRate2) {
383 return refreshRate1->vsyncPeriod > refreshRate2->vsyncPeriod;
384 });
385}
386
387void RefreshRateConfigs::constructAvailableRefreshRates() {
388 // Filter configs based on current policy and sort based on vsync period
Ana Kruleced3a8cc2019-11-14 00:55:07 +0100389 HwcConfigGroupType group = mRefreshRates.at(mDefaultConfig).configGroup;
Ady Abraham8a82ba62020-01-17 12:43:17 -0800390 ALOGV("constructAvailableRefreshRates: default %d group %d min %.2f max %.2f",
391 mDefaultConfig.value(), group.value(), mMinRefreshRateFps, mMaxRefreshRateFps);
Ady Abraham2139f732019-11-13 18:56:40 -0800392 getSortedRefreshRateList(
Ana Kruleced3a8cc2019-11-14 00:55:07 +0100393 [&](const RefreshRate& refreshRate) REQUIRES(mLock) {
Ana Krulec72f0d6e2020-01-06 15:24:47 -0800394 return refreshRate.configGroup == group &&
395 refreshRate.inPolicy(mMinRefreshRateFps, mMaxRefreshRateFps);
Ady Abraham2139f732019-11-13 18:56:40 -0800396 },
397 &mAvailableRefreshRates);
Ady Abraham8a82ba62020-01-17 12:43:17 -0800398
399 std::string availableRefreshRates;
400 for (const auto& refreshRate : mAvailableRefreshRates) {
401 base::StringAppendF(&availableRefreshRates, "%s ", refreshRate->name.c_str());
402 }
403
404 ALOGV("Available refresh rates: %s", availableRefreshRates.c_str());
Ana Kruleced3a8cc2019-11-14 00:55:07 +0100405 LOG_ALWAYS_FATAL_IF(mAvailableRefreshRates.empty(),
406 "No compatible display configs for default=%d min=%.0f max=%.0f",
407 mDefaultConfig.value(), mMinRefreshRateFps, mMaxRefreshRateFps);
Ady Abraham2139f732019-11-13 18:56:40 -0800408}
409
410// NO_THREAD_SAFETY_ANALYSIS since this is called from the constructor
411void RefreshRateConfigs::init(const std::vector<InputConfig>& configs,
412 HwcConfigIndexType currentHwcConfig) NO_THREAD_SAFETY_ANALYSIS {
Ady Abrahamb4b1e0a2019-11-20 18:25:35 -0800413 LOG_ALWAYS_FATAL_IF(configs.empty());
Ady Abraham2139f732019-11-13 18:56:40 -0800414 LOG_ALWAYS_FATAL_IF(currentHwcConfig.value() >= configs.size());
Ady Abrahamb4b1e0a2019-11-20 18:25:35 -0800415
Ady Abraham2139f732019-11-13 18:56:40 -0800416 auto buildRefreshRate = [&](InputConfig config) -> RefreshRate {
417 const float fps = 1e9f / config.vsyncPeriod;
418 return RefreshRate(config.configId, config.vsyncPeriod, config.configGroup,
419 base::StringPrintf("%2.ffps", fps), fps);
Ady Abrahamb4b1e0a2019-11-20 18:25:35 -0800420 };
421
Ady Abraham2139f732019-11-13 18:56:40 -0800422 for (const auto& config : configs) {
423 mRefreshRates.emplace(config.configId, buildRefreshRate(config));
424 if (config.configId == currentHwcConfig) {
425 mCurrentRefreshRate = &mRefreshRates.at(config.configId);
Ady Abraham2139f732019-11-13 18:56:40 -0800426 }
Ady Abrahamb4b1e0a2019-11-20 18:25:35 -0800427 }
428
Ady Abraham2139f732019-11-13 18:56:40 -0800429 std::vector<const RefreshRate*> sortedConfigs;
430 getSortedRefreshRateList([](const RefreshRate&) { return true; }, &sortedConfigs);
Ana Kruleced3a8cc2019-11-14 00:55:07 +0100431 mDefaultConfig = currentHwcConfig;
Ady Abraham2139f732019-11-13 18:56:40 -0800432 mMinSupportedRefreshRate = sortedConfigs.front();
433 mMaxSupportedRefreshRate = sortedConfigs.back();
434 constructAvailableRefreshRates();
Ady Abrahamb4b1e0a2019-11-20 18:25:35 -0800435}
436
Ady Abraham2139f732019-11-13 18:56:40 -0800437} // namespace android::scheduler