blob: 14ef73335e664c3ec86f65144de56e0276dcf585 [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 }
Steven Thomasd4071902020-03-24 16:02:53 -0700304 return *mRefreshRates.at(getCurrentPolicyLocked()->defaultConfig);
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
Steven Thomasd4071902020-03-24 16:02:53 -0700329bool RefreshRateConfigs::isPolicyValid(const Policy& policy) {
330 // defaultConfig must be a valid config, and within the given refresh rate range.
331 auto iter = mRefreshRates.find(policy.defaultConfig);
332 if (iter == mRefreshRates.end()) {
333 return false;
334 }
335 const RefreshRate& refreshRate = *iter->second;
336 if (!refreshRate.inPolicy(policy.minRefreshRate, policy.maxRefreshRate)) {
337 return false;
338 }
339 return true;
340}
341
342status_t RefreshRateConfigs::setDisplayManagerPolicy(const Policy& policy) {
Ady Abraham2139f732019-11-13 18:56:40 -0800343 std::lock_guard lock(mLock);
Steven Thomasd4071902020-03-24 16:02:53 -0700344 if (!isPolicyValid(policy)) {
Ana Kruleced3a8cc2019-11-14 00:55:07 +0100345 return BAD_VALUE;
346 }
Steven Thomasd4071902020-03-24 16:02:53 -0700347 Policy previousPolicy = *getCurrentPolicyLocked();
348 mDisplayManagerPolicy = policy;
349 if (*getCurrentPolicyLocked() == previousPolicy) {
350 return CURRENT_POLICY_UNCHANGED;
Ana Kruleced3a8cc2019-11-14 00:55:07 +0100351 }
Ady Abraham2139f732019-11-13 18:56:40 -0800352 constructAvailableRefreshRates();
Ana Kruleced3a8cc2019-11-14 00:55:07 +0100353 return NO_ERROR;
354}
355
Steven Thomasd4071902020-03-24 16:02:53 -0700356status_t RefreshRateConfigs::setOverridePolicy(const std::optional<Policy>& policy) {
Ana Kruleced3a8cc2019-11-14 00:55:07 +0100357 std::lock_guard lock(mLock);
Steven Thomasd4071902020-03-24 16:02:53 -0700358 if (policy && !isPolicyValid(*policy)) {
359 return BAD_VALUE;
360 }
361 Policy previousPolicy = *getCurrentPolicyLocked();
362 mOverridePolicy = policy;
363 if (*getCurrentPolicyLocked() == previousPolicy) {
364 return CURRENT_POLICY_UNCHANGED;
365 }
366 constructAvailableRefreshRates();
367 return NO_ERROR;
368}
369
370const RefreshRateConfigs::Policy* RefreshRateConfigs::getCurrentPolicyLocked() const {
371 return mOverridePolicy ? &mOverridePolicy.value() : &mDisplayManagerPolicy;
372}
373
374RefreshRateConfigs::Policy RefreshRateConfigs::getCurrentPolicy() const {
375 std::lock_guard lock(mLock);
376 return *getCurrentPolicyLocked();
377}
378
379RefreshRateConfigs::Policy RefreshRateConfigs::getDisplayManagerPolicy() const {
380 std::lock_guard lock(mLock);
381 return mDisplayManagerPolicy;
Ana Kruleced3a8cc2019-11-14 00:55:07 +0100382}
383
384bool RefreshRateConfigs::isConfigAllowed(HwcConfigIndexType config) const {
385 std::lock_guard lock(mLock);
386 for (const RefreshRate* refreshRate : mAvailableRefreshRates) {
387 if (refreshRate->configId == config) {
388 return true;
389 }
390 }
391 return false;
Ady Abraham2139f732019-11-13 18:56:40 -0800392}
393
394void RefreshRateConfigs::getSortedRefreshRateList(
395 const std::function<bool(const RefreshRate&)>& shouldAddRefreshRate,
396 std::vector<const RefreshRate*>* outRefreshRates) {
397 outRefreshRates->clear();
398 outRefreshRates->reserve(mRefreshRates.size());
399 for (const auto& [type, refreshRate] : mRefreshRates) {
Ady Abraham2e1dd892020-03-05 13:48:36 -0800400 if (shouldAddRefreshRate(*refreshRate)) {
Ady Abraham2139f732019-11-13 18:56:40 -0800401 ALOGV("getSortedRefreshRateList: config %d added to list policy",
Ady Abraham2e1dd892020-03-05 13:48:36 -0800402 refreshRate->configId.value());
403 outRefreshRates->push_back(refreshRate.get());
Ady Abraham2139f732019-11-13 18:56:40 -0800404 }
405 }
406
407 std::sort(outRefreshRates->begin(), outRefreshRates->end(),
408 [](const auto refreshRate1, const auto refreshRate2) {
Steven Thomasd4071902020-03-24 16:02:53 -0700409 if (refreshRate1->vsyncPeriod != refreshRate2->vsyncPeriod) {
410 return refreshRate1->vsyncPeriod > refreshRate2->vsyncPeriod;
411 } else {
412 return refreshRate1->configGroup > refreshRate2->configGroup;
413 }
Ady Abraham2139f732019-11-13 18:56:40 -0800414 });
415}
416
417void RefreshRateConfigs::constructAvailableRefreshRates() {
418 // Filter configs based on current policy and sort based on vsync period
Steven Thomasd4071902020-03-24 16:02:53 -0700419 const Policy* policy = getCurrentPolicyLocked();
420 HwcConfigGroupType group = mRefreshRates.at(policy->defaultConfig)->configGroup;
Ady Abraham8a82ba62020-01-17 12:43:17 -0800421 ALOGV("constructAvailableRefreshRates: default %d group %d min %.2f max %.2f",
Steven Thomasd4071902020-03-24 16:02:53 -0700422 policy->defaultConfig.value(), group.value(), policy->minRefreshRate,
423 policy->maxRefreshRate);
Ady Abraham2139f732019-11-13 18:56:40 -0800424 getSortedRefreshRateList(
Ana Kruleced3a8cc2019-11-14 00:55:07 +0100425 [&](const RefreshRate& refreshRate) REQUIRES(mLock) {
Steven Thomasd4071902020-03-24 16:02:53 -0700426 return (policy->allowGroupSwitching || refreshRate.configGroup == group) &&
427 refreshRate.inPolicy(policy->minRefreshRate, policy->maxRefreshRate);
Ady Abraham2139f732019-11-13 18:56:40 -0800428 },
429 &mAvailableRefreshRates);
Ady Abraham8a82ba62020-01-17 12:43:17 -0800430
431 std::string availableRefreshRates;
432 for (const auto& refreshRate : mAvailableRefreshRates) {
433 base::StringAppendF(&availableRefreshRates, "%s ", refreshRate->name.c_str());
434 }
435
436 ALOGV("Available refresh rates: %s", availableRefreshRates.c_str());
Ana Kruleced3a8cc2019-11-14 00:55:07 +0100437 LOG_ALWAYS_FATAL_IF(mAvailableRefreshRates.empty(),
438 "No compatible display configs for default=%d min=%.0f max=%.0f",
Steven Thomasd4071902020-03-24 16:02:53 -0700439 policy->defaultConfig.value(), policy->minRefreshRate,
440 policy->maxRefreshRate);
Ady Abraham2139f732019-11-13 18:56:40 -0800441}
442
443// NO_THREAD_SAFETY_ANALYSIS since this is called from the constructor
444void RefreshRateConfigs::init(const std::vector<InputConfig>& configs,
445 HwcConfigIndexType currentHwcConfig) NO_THREAD_SAFETY_ANALYSIS {
Ady Abrahamb4b1e0a2019-11-20 18:25:35 -0800446 LOG_ALWAYS_FATAL_IF(configs.empty());
Ady Abraham2139f732019-11-13 18:56:40 -0800447 LOG_ALWAYS_FATAL_IF(currentHwcConfig.value() >= configs.size());
Ady Abrahamb4b1e0a2019-11-20 18:25:35 -0800448
Ady Abraham2139f732019-11-13 18:56:40 -0800449 for (const auto& config : configs) {
Ady Abraham2e1dd892020-03-05 13:48:36 -0800450 const float fps = 1e9f / config.vsyncPeriod;
451 mRefreshRates.emplace(config.configId,
452 std::make_unique<RefreshRate>(config.configId, config.vsyncPeriod,
453 config.configGroup,
454 base::StringPrintf("%2.ffps", fps),
455 fps));
Ady Abraham2139f732019-11-13 18:56:40 -0800456 if (config.configId == currentHwcConfig) {
Ady Abraham2e1dd892020-03-05 13:48:36 -0800457 mCurrentRefreshRate = mRefreshRates.at(config.configId).get();
Ady Abraham2139f732019-11-13 18:56:40 -0800458 }
Ady Abrahamb4b1e0a2019-11-20 18:25:35 -0800459 }
460
Ady Abraham2139f732019-11-13 18:56:40 -0800461 std::vector<const RefreshRate*> sortedConfigs;
462 getSortedRefreshRateList([](const RefreshRate&) { return true; }, &sortedConfigs);
Steven Thomasd4071902020-03-24 16:02:53 -0700463 mDisplayManagerPolicy.defaultConfig = currentHwcConfig;
Ady Abraham2139f732019-11-13 18:56:40 -0800464 mMinSupportedRefreshRate = sortedConfigs.front();
465 mMaxSupportedRefreshRate = sortedConfigs.back();
466 constructAvailableRefreshRates();
Ady Abrahamb4b1e0a2019-11-20 18:25:35 -0800467}
468
Ady Abraham2139f732019-11-13 18:56:40 -0800469} // namespace android::scheduler