blob: 02d0b5359cb8c1e8b3c3fcd1d2f7979da483a734 [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 Abraham5b8afb5a2020-03-06 14:57:26 -080026#undef LOG_TAG
27#define LOG_TAG "RefreshRateConfigs"
28
Ady Abrahamb4b1e0a2019-11-20 18:25:35 -080029namespace android::scheduler {
Ady Abraham2139f732019-11-13 18:56:40 -080030
31using AllRefreshRatesMapType = RefreshRateConfigs::AllRefreshRatesMapType;
Ady Abrahamb4b1e0a2019-11-20 18:25:35 -080032using RefreshRate = RefreshRateConfigs::RefreshRate;
Ady Abrahamb4b1e0a2019-11-20 18:25:35 -080033
Ady Abraham8a82ba62020-01-17 12:43:17 -080034const RefreshRate& RefreshRateConfigs::getRefreshRateForContent(
35 const std::vector<LayerRequirement>& layers) const {
Ady Abraham2139f732019-11-13 18:56:40 -080036 std::lock_guard lock(mLock);
Ady Abrahamdec1a412020-01-24 10:23:50 -080037 int contentFramerate = 0;
38 int explicitContentFramerate = 0;
Ady Abraham8a82ba62020-01-17 12:43:17 -080039 for (const auto& layer : layers) {
Ady Abrahamdec1a412020-01-24 10:23:50 -080040 const auto desiredRefreshRateRound = round<int>(layer.desiredRefreshRate);
Ady Abraham71c437d2020-01-31 15:56:57 -080041 if (layer.vote == LayerVoteType::ExplicitDefault ||
42 layer.vote == LayerVoteType::ExplicitExactOrMultiple) {
Ady Abrahamdec1a412020-01-24 10:23:50 -080043 if (desiredRefreshRateRound > explicitContentFramerate) {
44 explicitContentFramerate = desiredRefreshRateRound;
Ady Abraham8a82ba62020-01-17 12:43:17 -080045 }
46 } else {
Ady Abrahamdec1a412020-01-24 10:23:50 -080047 if (desiredRefreshRateRound > contentFramerate) {
48 contentFramerate = desiredRefreshRateRound;
Ady Abraham8a82ba62020-01-17 12:43:17 -080049 }
50 }
51 }
52
Ady Abrahamdec1a412020-01-24 10:23:50 -080053 if (explicitContentFramerate != 0) {
Ady Abraham8a82ba62020-01-17 12:43:17 -080054 contentFramerate = explicitContentFramerate;
Ady Abrahamdec1a412020-01-24 10:23:50 -080055 } else if (contentFramerate == 0) {
56 contentFramerate = round<int>(mMaxSupportedRefreshRate->fps);
Ady Abraham8a82ba62020-01-17 12:43:17 -080057 }
Ady Abraham8a82ba62020-01-17 12:43:17 -080058 ATRACE_INT("ContentFPS", contentFramerate);
59
Ady Abraham2139f732019-11-13 18:56:40 -080060 // Find the appropriate refresh rate with minimal error
61 auto iter = min_element(mAvailableRefreshRates.cbegin(), mAvailableRefreshRates.cend(),
62 [contentFramerate](const auto& lhs, const auto& rhs) -> bool {
63 return std::abs(lhs->fps - contentFramerate) <
64 std::abs(rhs->fps - contentFramerate);
65 });
Ady Abrahamb4b1e0a2019-11-20 18:25:35 -080066
Ady Abraham2139f732019-11-13 18:56:40 -080067 // Some content aligns better on higher refresh rate. For example for 45fps we should choose
68 // 90Hz config. However we should still prefer a lower refresh rate if the content doesn't
69 // align well with both
70 const RefreshRate* bestSoFar = *iter;
71 constexpr float MARGIN = 0.05f;
72 float ratio = (*iter)->fps / contentFramerate;
73 if (std::abs(std::round(ratio) - ratio) > MARGIN) {
74 while (iter != mAvailableRefreshRates.cend()) {
75 ratio = (*iter)->fps / contentFramerate;
Ady Abrahamb4b1e0a2019-11-20 18:25:35 -080076
Ady Abraham2139f732019-11-13 18:56:40 -080077 if (std::abs(std::round(ratio) - ratio) <= MARGIN) {
78 bestSoFar = *iter;
79 break;
Ady Abrahamb4b1e0a2019-11-20 18:25:35 -080080 }
Ady Abraham2139f732019-11-13 18:56:40 -080081 ++iter;
Ady Abrahamb4b1e0a2019-11-20 18:25:35 -080082 }
83 }
84
Ady Abraham2139f732019-11-13 18:56:40 -080085 return *bestSoFar;
Ady Abrahamb4b1e0a2019-11-20 18:25:35 -080086}
87
Ady Abraham4ccdcb42020-02-11 17:34:34 -080088std::pair<nsecs_t, nsecs_t> RefreshRateConfigs::getDisplayFrames(nsecs_t layerPeriod,
89 nsecs_t displayPeriod) const {
90 auto [displayFramesQuot, displayFramesRem] = std::div(layerPeriod, displayPeriod);
91 if (displayFramesRem <= MARGIN_FOR_PERIOD_CALCULATION ||
92 std::abs(displayFramesRem - displayPeriod) <= MARGIN_FOR_PERIOD_CALCULATION) {
93 displayFramesQuot++;
94 displayFramesRem = 0;
95 }
96
97 return {displayFramesQuot, displayFramesRem};
98}
99
Ady Abraham8a82ba62020-01-17 12:43:17 -0800100const RefreshRate& RefreshRateConfigs::getRefreshRateForContentV2(
Ady Abraham6fb599b2020-03-05 13:48:22 -0800101 const std::vector<LayerRequirement>& layers, bool touchActive,
102 bool* touchConsidered) const {
Ady Abraham8a82ba62020-01-17 12:43:17 -0800103 ATRACE_CALL();
104 ALOGV("getRefreshRateForContent %zu layers", layers.size());
105
Ady Abraham6fb599b2020-03-05 13:48:22 -0800106 *touchConsidered = false;
Ady Abraham8a82ba62020-01-17 12:43:17 -0800107 std::lock_guard lock(mLock);
108
Ana Krulec3d367c82020-02-25 15:02:01 -0800109 // If there are not layers, there is not content detection, so return the current
110 // refresh rate.
111 if (layers.empty()) {
Ady Abraham6fb599b2020-03-05 13:48:22 -0800112 *touchConsidered = touchActive;
113 return touchActive ? *mAvailableRefreshRates.back() : getCurrentRefreshRateByPolicyLocked();
Ana Krulec3d367c82020-02-25 15:02:01 -0800114 }
115
Ady Abraham8a82ba62020-01-17 12:43:17 -0800116 int noVoteLayers = 0;
117 int minVoteLayers = 0;
118 int maxVoteLayers = 0;
Ady Abraham71c437d2020-01-31 15:56:57 -0800119 int explicitDefaultVoteLayers = 0;
120 int explicitExactOrMultipleVoteLayers = 0;
Ady Abraham6fb599b2020-03-05 13:48:22 -0800121 float maxExplicitWeight = 0;
Ady Abraham8a82ba62020-01-17 12:43:17 -0800122 for (const auto& layer : layers) {
Ady Abraham6fb599b2020-03-05 13:48:22 -0800123 if (layer.vote == LayerVoteType::NoVote) {
Ady Abraham8a82ba62020-01-17 12:43:17 -0800124 noVoteLayers++;
Ady Abraham6fb599b2020-03-05 13:48:22 -0800125 } else if (layer.vote == LayerVoteType::Min) {
Ady Abraham8a82ba62020-01-17 12:43:17 -0800126 minVoteLayers++;
Ady Abraham6fb599b2020-03-05 13:48:22 -0800127 } else if (layer.vote == LayerVoteType::Max) {
Ady Abraham8a82ba62020-01-17 12:43:17 -0800128 maxVoteLayers++;
Ady Abraham6fb599b2020-03-05 13:48:22 -0800129 } else if (layer.vote == LayerVoteType::ExplicitDefault) {
Ady Abraham71c437d2020-01-31 15:56:57 -0800130 explicitDefaultVoteLayers++;
Ady Abraham6fb599b2020-03-05 13:48:22 -0800131 maxExplicitWeight = std::max(maxExplicitWeight, layer.weight);
132 } else if (layer.vote == LayerVoteType::ExplicitExactOrMultiple) {
Ady Abraham71c437d2020-01-31 15:56:57 -0800133 explicitExactOrMultipleVoteLayers++;
Ady Abraham6fb599b2020-03-05 13:48:22 -0800134 maxExplicitWeight = std::max(maxExplicitWeight, layer.weight);
135 }
136 }
137
138 // Consider the touch event if there are no ExplicitDefault layers.
139 // ExplicitDefault are mostly interactive (as opposed to ExplicitExactOrMultiple)
140 // and therefore if those posted an explicit vote we should not change it
141 // if get get a touch event.
142 if (touchActive && explicitDefaultVoteLayers == 0) {
143 *touchConsidered = true;
144 return *mAvailableRefreshRates.back();
Ady Abraham8a82ba62020-01-17 12:43:17 -0800145 }
146
147 // Only if all layers want Min we should return Min
148 if (noVoteLayers + minVoteLayers == layers.size()) {
149 return *mAvailableRefreshRates.front();
150 }
151
Ady Abraham8a82ba62020-01-17 12:43:17 -0800152 // Find the best refresh rate based on score
153 std::vector<std::pair<const RefreshRate*, float>> scores;
154 scores.reserve(mAvailableRefreshRates.size());
155
156 for (const auto refreshRate : mAvailableRefreshRates) {
157 scores.emplace_back(refreshRate, 0.0f);
158 }
159
160 for (const auto& layer : layers) {
Ady Abrahamf6b77072020-01-30 14:22:54 -0800161 ALOGV("Calculating score for %s (type: %d)", layer.name.c_str(), layer.vote);
Ady Abraham4ccdcb42020-02-11 17:34:34 -0800162 if (layer.vote == LayerVoteType::NoVote || layer.vote == LayerVoteType::Min) {
Ady Abraham8a82ba62020-01-17 12:43:17 -0800163 continue;
164 }
165
Ady Abraham71c437d2020-01-31 15:56:57 -0800166 auto weight = layer.weight;
Ady Abraham71c437d2020-01-31 15:56:57 -0800167
Ady Abraham4ccdcb42020-02-11 17:34:34 -0800168 for (auto i = 0u; i < scores.size(); i++) {
169 // If the layer wants Max, give higher score to the higher refresh rate
170 if (layer.vote == LayerVoteType::Max) {
171 const auto ratio = scores[i].first->fps / scores.back().first->fps;
172 // use ratio^2 to get a lower score the more we get further from peak
173 const auto layerScore = ratio * ratio;
174 ALOGV("%s (Max, weight %.2f) gives %s score of %.2f", layer.name.c_str(), weight,
175 scores[i].first->name.c_str(), layerScore);
176 scores[i].second += weight * layerScore;
177 continue;
Ady Abraham71c437d2020-01-31 15:56:57 -0800178 }
Ady Abraham8a82ba62020-01-17 12:43:17 -0800179
Ady Abraham4ccdcb42020-02-11 17:34:34 -0800180 const auto displayPeriod = scores[i].first->vsyncPeriod;
Ady Abrahamdec1a412020-01-24 10:23:50 -0800181 const auto layerPeriod = round<nsecs_t>(1e9f / layer.desiredRefreshRate);
Ady Abraham4ccdcb42020-02-11 17:34:34 -0800182 if (layer.vote == LayerVoteType::ExplicitDefault) {
183 const auto layerScore = [&]() {
Ady Abraham5b8afb5a2020-03-06 14:57:26 -0800184 // Find the actual rate the layer will render, assuming
185 // that layerPeriod is the minimal time to render a frame
186 auto actualLayerPeriod = displayPeriod;
187 int multiplier = 1;
188 while (layerPeriod > actualLayerPeriod + MARGIN_FOR_PERIOD_CALCULATION) {
189 multiplier++;
190 actualLayerPeriod = displayPeriod * multiplier;
Ady Abraham4ccdcb42020-02-11 17:34:34 -0800191 }
Ady Abraham5b8afb5a2020-03-06 14:57:26 -0800192 return std::min(1.0f,
193 static_cast<float>(layerPeriod) /
194 static_cast<float>(actualLayerPeriod));
Ady Abraham4ccdcb42020-02-11 17:34:34 -0800195 }();
196
197 ALOGV("%s (ExplicitDefault, weight %.2f) %.2fHz gives %s score of %.2f",
198 layer.name.c_str(), weight, 1e9f / layerPeriod, scores[i].first->name.c_str(),
199 layerScore);
200 scores[i].second += weight * layerScore;
201 continue;
Ady Abraham8a82ba62020-01-17 12:43:17 -0800202 }
203
Ady Abraham4ccdcb42020-02-11 17:34:34 -0800204 if (layer.vote == LayerVoteType::ExplicitExactOrMultiple ||
205 layer.vote == LayerVoteType::Heuristic) {
206 const auto layerScore = [&]() {
207 // Calculate how many display vsyncs we need to present a single frame for this
208 // layer
209 const auto [displayFramesQuot, displayFramesRem] =
210 getDisplayFrames(layerPeriod, displayPeriod);
211 static constexpr size_t MAX_FRAMES_TO_FIT =
212 10; // Stop calculating when score < 0.1
213 if (displayFramesRem == 0) {
214 // Layer desired refresh rate matches the display rate.
215 return 1.0f;
216 }
Ady Abraham8a82ba62020-01-17 12:43:17 -0800217
Ady Abraham4ccdcb42020-02-11 17:34:34 -0800218 if (displayFramesQuot == 0) {
219 // Layer desired refresh rate is higher the display rate.
220 return (static_cast<float>(layerPeriod) /
221 static_cast<float>(displayPeriod)) *
222 (1.0f / (MAX_FRAMES_TO_FIT + 1));
223 }
224
225 // Layer desired refresh rate is lower the display rate. Check how well it fits
226 // the cadence
227 auto diff = std::abs(displayFramesRem - (displayPeriod - displayFramesRem));
228 int iter = 2;
229 while (diff > MARGIN_FOR_PERIOD_CALCULATION && iter < MAX_FRAMES_TO_FIT) {
230 diff = diff - (displayPeriod - diff);
231 iter++;
232 }
233
234 return 1.0f / iter;
235 }();
236 ALOGV("%s (ExplicitExactOrMultiple, weight %.2f) %.2fHz gives %s score of %.2f",
237 layer.name.c_str(), weight, 1e9f / layerPeriod, scores[i].first->name.c_str(),
238 layerScore);
239 scores[i].second += weight * layerScore;
240 continue;
Ady Abraham8a82ba62020-01-17 12:43:17 -0800241 }
Ady Abraham8a82ba62020-01-17 12:43:17 -0800242 }
243 }
244
Ady Abraham34702102020-02-10 14:12:05 -0800245 // Now that we scored all the refresh rates we need to pick the one that got the highest score.
246 // In case of a tie we will pick the higher refresh rate if any of the layers wanted Max,
247 // or the lower otherwise.
248 const RefreshRate* bestRefreshRate = maxVoteLayers > 0
249 ? getBestRefreshRate(scores.rbegin(), scores.rend())
250 : getBestRefreshRate(scores.begin(), scores.end());
251
Ady Abrahamde7156e2020-02-28 17:29:39 -0800252 return *bestRefreshRate;
Ady Abraham34702102020-02-10 14:12:05 -0800253}
254
255template <typename Iter>
256const RefreshRate* RefreshRateConfigs::getBestRefreshRate(Iter begin, Iter end) const {
Ady Abraham5b8afb5a2020-03-06 14:57:26 -0800257 constexpr auto EPSILON = 0.001f;
Ady Abrahamde7156e2020-02-28 17:29:39 -0800258 const RefreshRate* bestRefreshRate = begin->first;
259 float max = begin->second;
Ady Abraham34702102020-02-10 14:12:05 -0800260 for (auto i = begin; i != end; ++i) {
261 const auto [refreshRate, score] = *i;
Ady Abraham8a82ba62020-01-17 12:43:17 -0800262 ALOGV("%s scores %.2f", refreshRate->name.c_str(), score);
263
Ady Abrahamdec1a412020-01-24 10:23:50 -0800264 ATRACE_INT(refreshRate->name.c_str(), round<int>(score * 100));
Ady Abraham8a82ba62020-01-17 12:43:17 -0800265
Ady Abraham5b8afb5a2020-03-06 14:57:26 -0800266 if (score > max * (1 + EPSILON)) {
Ady Abraham8a82ba62020-01-17 12:43:17 -0800267 max = score;
268 bestRefreshRate = refreshRate;
269 }
270 }
271
Ady Abraham34702102020-02-10 14:12:05 -0800272 return bestRefreshRate;
Ady Abraham8a82ba62020-01-17 12:43:17 -0800273}
274
Ady Abraham2139f732019-11-13 18:56:40 -0800275const AllRefreshRatesMapType& RefreshRateConfigs::getAllRefreshRates() const {
276 return mRefreshRates;
277}
278
279const RefreshRate& RefreshRateConfigs::getMinRefreshRateByPolicy() const {
280 std::lock_guard lock(mLock);
Ana Krulec3f6a2062020-01-23 15:48:01 -0800281 return *mAvailableRefreshRates.front();
Ady Abraham2139f732019-11-13 18:56:40 -0800282}
283
284const RefreshRate& RefreshRateConfigs::getMaxRefreshRateByPolicy() const {
285 std::lock_guard lock(mLock);
Ady Abraham2139f732019-11-13 18:56:40 -0800286 return *mAvailableRefreshRates.back();
Ady Abraham2139f732019-11-13 18:56:40 -0800287}
288
289const RefreshRate& RefreshRateConfigs::getCurrentRefreshRate() const {
290 std::lock_guard lock(mLock);
291 return *mCurrentRefreshRate;
292}
293
Ana Krulec5d477912020-02-07 12:02:38 -0800294const RefreshRate& RefreshRateConfigs::getCurrentRefreshRateByPolicy() const {
295 std::lock_guard lock(mLock);
Ana Krulec3d367c82020-02-25 15:02:01 -0800296 return getCurrentRefreshRateByPolicyLocked();
297}
298
299const RefreshRate& RefreshRateConfigs::getCurrentRefreshRateByPolicyLocked() const {
Ana Krulec5d477912020-02-07 12:02:38 -0800300 if (std::find(mAvailableRefreshRates.begin(), mAvailableRefreshRates.end(),
301 mCurrentRefreshRate) != mAvailableRefreshRates.end()) {
302 return *mCurrentRefreshRate;
303 }
Ady Abraham2e1dd892020-03-05 13:48:36 -0800304 return *mRefreshRates.at(mDefaultConfig);
Ana Krulec5d477912020-02-07 12:02:38 -0800305}
306
Ady Abraham2139f732019-11-13 18:56:40 -0800307void RefreshRateConfigs::setCurrentConfigId(HwcConfigIndexType configId) {
308 std::lock_guard lock(mLock);
Ady Abraham2e1dd892020-03-05 13:48:36 -0800309 mCurrentRefreshRate = mRefreshRates.at(configId).get();
Ady Abrahamb4b1e0a2019-11-20 18:25:35 -0800310}
311
Ana Krulec3f6a2062020-01-23 15:48:01 -0800312RefreshRateConfigs::RefreshRateConfigs(const std::vector<InputConfig>& configs,
313 HwcConfigIndexType currentHwcConfig) {
Ady Abraham2139f732019-11-13 18:56:40 -0800314 init(configs, currentHwcConfig);
Ady Abrahamb4b1e0a2019-11-20 18:25:35 -0800315}
316
317RefreshRateConfigs::RefreshRateConfigs(
Ady Abrahamb4b1e0a2019-11-20 18:25:35 -0800318 const std::vector<std::shared_ptr<const HWC2::Display::Config>>& configs,
Ana Krulec3f6a2062020-01-23 15:48:01 -0800319 HwcConfigIndexType currentConfigId) {
Ady Abrahamb4b1e0a2019-11-20 18:25:35 -0800320 std::vector<InputConfig> inputConfigs;
Ady Abrahamdec1a412020-01-24 10:23:50 -0800321 for (size_t configId = 0; configId < configs.size(); ++configId) {
322 auto configGroup = HwcConfigGroupType(configs[configId]->getConfigGroup());
323 inputConfigs.push_back({HwcConfigIndexType(static_cast<int>(configId)), configGroup,
324 configs[configId]->getVsyncPeriod()});
Ady Abrahamb4b1e0a2019-11-20 18:25:35 -0800325 }
Ady Abraham2139f732019-11-13 18:56:40 -0800326 init(inputConfigs, currentConfigId);
Ady Abrahamb4b1e0a2019-11-20 18:25:35 -0800327}
328
Ana Kruleced3a8cc2019-11-14 00:55:07 +0100329status_t RefreshRateConfigs::setPolicy(HwcConfigIndexType defaultConfigId, float minRefreshRate,
330 float maxRefreshRate, bool* outPolicyChanged) {
Ady Abraham2139f732019-11-13 18:56:40 -0800331 std::lock_guard lock(mLock);
Ana Kruleced3a8cc2019-11-14 00:55:07 +0100332 bool policyChanged = defaultConfigId != mDefaultConfig ||
333 minRefreshRate != mMinRefreshRateFps || maxRefreshRate != mMaxRefreshRateFps;
334 if (outPolicyChanged) {
335 *outPolicyChanged = policyChanged;
336 }
337 if (!policyChanged) {
338 return NO_ERROR;
339 }
340 // defaultConfigId must be a valid config ID, and within the given refresh rate range.
341 if (mRefreshRates.count(defaultConfigId) == 0) {
342 return BAD_VALUE;
343 }
Ady Abraham2e1dd892020-03-05 13:48:36 -0800344 const RefreshRate& refreshRate = *mRefreshRates.at(defaultConfigId);
Ana Krulec72f0d6e2020-01-06 15:24:47 -0800345 if (!refreshRate.inPolicy(minRefreshRate, maxRefreshRate)) {
Ana Kruleced3a8cc2019-11-14 00:55:07 +0100346 return BAD_VALUE;
347 }
348 mDefaultConfig = defaultConfigId;
Ady Abraham2139f732019-11-13 18:56:40 -0800349 mMinRefreshRateFps = minRefreshRate;
350 mMaxRefreshRateFps = maxRefreshRate;
351 constructAvailableRefreshRates();
Ana Kruleced3a8cc2019-11-14 00:55:07 +0100352 return NO_ERROR;
353}
354
355void RefreshRateConfigs::getPolicy(HwcConfigIndexType* defaultConfigId, float* minRefreshRate,
356 float* maxRefreshRate) const {
357 std::lock_guard lock(mLock);
358 *defaultConfigId = mDefaultConfig;
359 *minRefreshRate = mMinRefreshRateFps;
360 *maxRefreshRate = mMaxRefreshRateFps;
361}
362
363bool RefreshRateConfigs::isConfigAllowed(HwcConfigIndexType config) const {
364 std::lock_guard lock(mLock);
365 for (const RefreshRate* refreshRate : mAvailableRefreshRates) {
366 if (refreshRate->configId == config) {
367 return true;
368 }
369 }
370 return false;
Ady Abraham2139f732019-11-13 18:56:40 -0800371}
372
373void RefreshRateConfigs::getSortedRefreshRateList(
374 const std::function<bool(const RefreshRate&)>& shouldAddRefreshRate,
375 std::vector<const RefreshRate*>* outRefreshRates) {
376 outRefreshRates->clear();
377 outRefreshRates->reserve(mRefreshRates.size());
378 for (const auto& [type, refreshRate] : mRefreshRates) {
Ady Abraham2e1dd892020-03-05 13:48:36 -0800379 if (shouldAddRefreshRate(*refreshRate)) {
Ady Abraham2139f732019-11-13 18:56:40 -0800380 ALOGV("getSortedRefreshRateList: config %d added to list policy",
Ady Abraham2e1dd892020-03-05 13:48:36 -0800381 refreshRate->configId.value());
382 outRefreshRates->push_back(refreshRate.get());
Ady Abraham2139f732019-11-13 18:56:40 -0800383 }
384 }
385
386 std::sort(outRefreshRates->begin(), outRefreshRates->end(),
387 [](const auto refreshRate1, const auto refreshRate2) {
388 return refreshRate1->vsyncPeriod > refreshRate2->vsyncPeriod;
389 });
390}
391
392void RefreshRateConfigs::constructAvailableRefreshRates() {
393 // Filter configs based on current policy and sort based on vsync period
Ady Abraham2e1dd892020-03-05 13:48:36 -0800394 HwcConfigGroupType group = mRefreshRates.at(mDefaultConfig)->configGroup;
Ady Abraham8a82ba62020-01-17 12:43:17 -0800395 ALOGV("constructAvailableRefreshRates: default %d group %d min %.2f max %.2f",
396 mDefaultConfig.value(), group.value(), mMinRefreshRateFps, mMaxRefreshRateFps);
Ady Abraham2139f732019-11-13 18:56:40 -0800397 getSortedRefreshRateList(
Ana Kruleced3a8cc2019-11-14 00:55:07 +0100398 [&](const RefreshRate& refreshRate) REQUIRES(mLock) {
Ana Krulec72f0d6e2020-01-06 15:24:47 -0800399 return refreshRate.configGroup == group &&
400 refreshRate.inPolicy(mMinRefreshRateFps, mMaxRefreshRateFps);
Ady Abraham2139f732019-11-13 18:56:40 -0800401 },
402 &mAvailableRefreshRates);
Ady Abraham8a82ba62020-01-17 12:43:17 -0800403
404 std::string availableRefreshRates;
405 for (const auto& refreshRate : mAvailableRefreshRates) {
406 base::StringAppendF(&availableRefreshRates, "%s ", refreshRate->name.c_str());
407 }
408
409 ALOGV("Available refresh rates: %s", availableRefreshRates.c_str());
Ana Kruleced3a8cc2019-11-14 00:55:07 +0100410 LOG_ALWAYS_FATAL_IF(mAvailableRefreshRates.empty(),
411 "No compatible display configs for default=%d min=%.0f max=%.0f",
412 mDefaultConfig.value(), mMinRefreshRateFps, mMaxRefreshRateFps);
Ady Abraham2139f732019-11-13 18:56:40 -0800413}
414
415// NO_THREAD_SAFETY_ANALYSIS since this is called from the constructor
416void RefreshRateConfigs::init(const std::vector<InputConfig>& configs,
417 HwcConfigIndexType currentHwcConfig) NO_THREAD_SAFETY_ANALYSIS {
Ady Abrahamb4b1e0a2019-11-20 18:25:35 -0800418 LOG_ALWAYS_FATAL_IF(configs.empty());
Ady Abraham2139f732019-11-13 18:56:40 -0800419 LOG_ALWAYS_FATAL_IF(currentHwcConfig.value() >= configs.size());
Ady Abrahamb4b1e0a2019-11-20 18:25:35 -0800420
Ady Abraham2139f732019-11-13 18:56:40 -0800421 for (const auto& config : configs) {
Ady Abraham2e1dd892020-03-05 13:48:36 -0800422 const float fps = 1e9f / config.vsyncPeriod;
423 mRefreshRates.emplace(config.configId,
424 std::make_unique<RefreshRate>(config.configId, config.vsyncPeriod,
425 config.configGroup,
426 base::StringPrintf("%2.ffps", fps),
427 fps));
Ady Abraham2139f732019-11-13 18:56:40 -0800428 if (config.configId == currentHwcConfig) {
Ady Abraham2e1dd892020-03-05 13:48:36 -0800429 mCurrentRefreshRate = mRefreshRates.at(config.configId).get();
Ady Abraham2139f732019-11-13 18:56:40 -0800430 }
Ady Abrahamb4b1e0a2019-11-20 18:25:35 -0800431 }
432
Ady Abraham2139f732019-11-13 18:56:40 -0800433 std::vector<const RefreshRate*> sortedConfigs;
434 getSortedRefreshRateList([](const RefreshRate&) { return true; }, &sortedConfigs);
Ana Kruleced3a8cc2019-11-14 00:55:07 +0100435 mDefaultConfig = currentHwcConfig;
Ady Abraham2139f732019-11-13 18:56:40 -0800436 mMinSupportedRefreshRate = sortedConfigs.front();
437 mMaxSupportedRefreshRate = sortedConfigs.back();
438 constructAvailableRefreshRates();
Ady Abrahamb4b1e0a2019-11-20 18:25:35 -0800439}
440
Ady Abraham2139f732019-11-13 18:56:40 -0800441} // namespace android::scheduler