blob: cd03c180c0909dec6c4534c5ce05846310b21e4b [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 {
Marin Shalamanov53fc11d2020-11-20 14:00:13 +010030namespace {
31std::string formatLayerInfo(const RefreshRateConfigs::LayerRequirement& layer, float weight) {
Marin Shalamanove8a663d2020-11-24 17:48:00 +010032 return base::StringPrintf("%s (type=%s, weight=%.2f seamlessness=%s) %s", layer.name.c_str(),
Marin Shalamanov53fc11d2020-11-20 14:00:13 +010033 RefreshRateConfigs::layerVoteTypeString(layer.vote).c_str(), weight,
Marin Shalamanove8a663d2020-11-24 17:48:00 +010034 toString(layer.seamlessness).c_str(),
35 to_string(layer.desiredRefreshRate).c_str());
Marin Shalamanov53fc11d2020-11-20 14:00:13 +010036}
37} // namespace
Ady Abraham2139f732019-11-13 18:56:40 -080038
39using AllRefreshRatesMapType = RefreshRateConfigs::AllRefreshRatesMapType;
Ady Abrahamb4b1e0a2019-11-20 18:25:35 -080040using RefreshRate = RefreshRateConfigs::RefreshRate;
Ady Abrahamb4b1e0a2019-11-20 18:25:35 -080041
Marin Shalamanov46084422020-10-13 12:33:42 +020042std::string RefreshRate::toString() const {
43 return base::StringPrintf("{id=%d, hwcId=%d, fps=%.2f, width=%d, height=%d group=%d}",
Marin Shalamanove8a663d2020-11-24 17:48:00 +010044 getConfigId().value(), hwcConfig->getId(), getFps().getValue(),
Marin Shalamanov46084422020-10-13 12:33:42 +020045 hwcConfig->getWidth(), hwcConfig->getHeight(), getConfigGroup());
46}
47
Ady Abrahama6b676e2020-05-27 14:29:09 -070048std::string RefreshRateConfigs::layerVoteTypeString(LayerVoteType vote) {
49 switch (vote) {
50 case LayerVoteType::NoVote:
51 return "NoVote";
52 case LayerVoteType::Min:
53 return "Min";
54 case LayerVoteType::Max:
55 return "Max";
56 case LayerVoteType::Heuristic:
57 return "Heuristic";
58 case LayerVoteType::ExplicitDefault:
59 return "ExplicitDefault";
60 case LayerVoteType::ExplicitExactOrMultiple:
61 return "ExplicitExactOrMultiple";
62 }
63}
64
Marin Shalamanovb6674e72020-11-06 13:05:57 +010065std::string RefreshRateConfigs::Policy::toString() const {
Marin Shalamanov30b0b3c2020-10-13 19:15:06 +020066 return base::StringPrintf("default config ID: %d, allowGroupSwitching = %d"
Marin Shalamanove8a663d2020-11-24 17:48:00 +010067 ", primary range: %s, app request range: %s",
68 defaultConfig.value(), allowGroupSwitching,
69 primaryRange.toString().c_str(), appRequestRange.toString().c_str());
Marin Shalamanov30b0b3c2020-10-13 19:15:06 +020070}
71
Ady Abraham4ccdcb42020-02-11 17:34:34 -080072std::pair<nsecs_t, nsecs_t> RefreshRateConfigs::getDisplayFrames(nsecs_t layerPeriod,
73 nsecs_t displayPeriod) const {
Ady Abraham2c6716b2020-12-08 16:54:10 -080074 auto [quotient, remainder] = std::div(layerPeriod, displayPeriod);
75 if (remainder <= MARGIN_FOR_PERIOD_CALCULATION ||
76 std::abs(remainder - displayPeriod) <= MARGIN_FOR_PERIOD_CALCULATION) {
77 quotient++;
78 remainder = 0;
Ady Abraham4ccdcb42020-02-11 17:34:34 -080079 }
80
Ady Abraham2c6716b2020-12-08 16:54:10 -080081 return {quotient, remainder};
Ady Abraham4ccdcb42020-02-11 17:34:34 -080082}
83
Ady Abraham2c6716b2020-12-08 16:54:10 -080084float RefreshRateConfigs::calculateLayerScoreLocked(const LayerRequirement& layer,
85 const RefreshRate& refreshRate,
86 bool isSeamlessSwitch) const {
87 // Slightly prefer seamless switches.
88 constexpr float kSeamedSwitchPenalty = 0.95f;
89 const float seamlessness = isSeamlessSwitch ? 1.0f : kSeamedSwitchPenalty;
90
91 // If the layer wants Max, give higher score to the higher refresh rate
92 if (layer.vote == LayerVoteType::Max) {
93 const auto ratio =
94 refreshRate.fps.getValue() / mAppRequestRefreshRates.back()->fps.getValue();
95 // use ratio^2 to get a lower score the more we get further from peak
96 return ratio * ratio;
97 }
98
99 const auto displayPeriod = refreshRate.getVsyncPeriod();
100 const auto layerPeriod = layer.desiredRefreshRate.getPeriodNsecs();
101 if (layer.vote == LayerVoteType::ExplicitDefault) {
102 // Find the actual rate the layer will render, assuming
103 // that layerPeriod is the minimal time to render a frame
104 auto actualLayerPeriod = displayPeriod;
105 int multiplier = 1;
106 while (layerPeriod > actualLayerPeriod + MARGIN_FOR_PERIOD_CALCULATION) {
107 multiplier++;
108 actualLayerPeriod = displayPeriod * multiplier;
109 }
110 return std::min(1.0f,
111 static_cast<float>(layerPeriod) / static_cast<float>(actualLayerPeriod));
112 }
113
114 if (layer.vote == LayerVoteType::ExplicitExactOrMultiple ||
115 layer.vote == LayerVoteType::Heuristic) {
116 // Calculate how many display vsyncs we need to present a single frame for this
117 // layer
118 const auto [displayFramesQuotient, displayFramesRemainder] =
119 getDisplayFrames(layerPeriod, displayPeriod);
120 static constexpr size_t MAX_FRAMES_TO_FIT = 10; // Stop calculating when score < 0.1
121 if (displayFramesRemainder == 0) {
122 // Layer desired refresh rate matches the display rate.
123 return 1.0f * seamlessness;
124 }
125
126 if (displayFramesQuotient == 0) {
127 // Layer desired refresh rate is higher than the display rate.
128 return (static_cast<float>(layerPeriod) / static_cast<float>(displayPeriod)) *
129 (1.0f / (MAX_FRAMES_TO_FIT + 1));
130 }
131
132 // Layer desired refresh rate is lower than the display rate. Check how well it fits
133 // the cadence.
134 auto diff = std::abs(displayFramesRemainder - (displayPeriod - displayFramesRemainder));
135 int iter = 2;
136 while (diff > MARGIN_FOR_PERIOD_CALCULATION && iter < MAX_FRAMES_TO_FIT) {
137 diff = diff - (displayPeriod - diff);
138 iter++;
139 }
140
141 return (1.0f / iter) * seamlessness;
142 }
143
144 return 0;
145}
146
147struct RefreshRateScore {
148 const RefreshRate* refreshRate;
149 float score;
150};
151
Steven Thomasbb374322020-04-28 22:47:16 -0700152const RefreshRate& RefreshRateConfigs::getBestRefreshRate(
Ady Abrahamdfd62162020-06-10 16:11:56 -0700153 const std::vector<LayerRequirement>& layers, const GlobalSignals& globalSignals,
154 GlobalSignals* outSignalsConsidered) const {
Ady Abraham8a82ba62020-01-17 12:43:17 -0800155 ATRACE_CALL();
Marin Shalamanov46084422020-10-13 12:33:42 +0200156 ALOGV("getBestRefreshRate %zu layers", layers.size());
Ady Abraham8a82ba62020-01-17 12:43:17 -0800157
Ady Abrahamdfd62162020-06-10 16:11:56 -0700158 if (outSignalsConsidered) *outSignalsConsidered = {};
159 const auto setTouchConsidered = [&] {
160 if (outSignalsConsidered) {
161 outSignalsConsidered->touch = true;
162 }
163 };
164
165 const auto setIdleConsidered = [&] {
166 if (outSignalsConsidered) {
167 outSignalsConsidered->idle = true;
168 }
169 };
170
Ady Abraham8a82ba62020-01-17 12:43:17 -0800171 std::lock_guard lock(mLock);
172
173 int noVoteLayers = 0;
174 int minVoteLayers = 0;
175 int maxVoteLayers = 0;
Ady Abraham71c437d2020-01-31 15:56:57 -0800176 int explicitDefaultVoteLayers = 0;
177 int explicitExactOrMultipleVoteLayers = 0;
Ady Abraham6fb599b2020-03-05 13:48:22 -0800178 float maxExplicitWeight = 0;
Marin Shalamanov46084422020-10-13 12:33:42 +0200179 int seamedLayers = 0;
Ady Abraham8a82ba62020-01-17 12:43:17 -0800180 for (const auto& layer : layers) {
Ady Abraham6fb599b2020-03-05 13:48:22 -0800181 if (layer.vote == LayerVoteType::NoVote) {
Ady Abraham8a82ba62020-01-17 12:43:17 -0800182 noVoteLayers++;
Ady Abraham6fb599b2020-03-05 13:48:22 -0800183 } else if (layer.vote == LayerVoteType::Min) {
Ady Abraham8a82ba62020-01-17 12:43:17 -0800184 minVoteLayers++;
Ady Abraham6fb599b2020-03-05 13:48:22 -0800185 } else if (layer.vote == LayerVoteType::Max) {
Ady Abraham8a82ba62020-01-17 12:43:17 -0800186 maxVoteLayers++;
Ady Abraham6fb599b2020-03-05 13:48:22 -0800187 } else if (layer.vote == LayerVoteType::ExplicitDefault) {
Ady Abraham71c437d2020-01-31 15:56:57 -0800188 explicitDefaultVoteLayers++;
Ady Abraham6fb599b2020-03-05 13:48:22 -0800189 maxExplicitWeight = std::max(maxExplicitWeight, layer.weight);
190 } else if (layer.vote == LayerVoteType::ExplicitExactOrMultiple) {
Ady Abraham71c437d2020-01-31 15:56:57 -0800191 explicitExactOrMultipleVoteLayers++;
Ady Abraham6fb599b2020-03-05 13:48:22 -0800192 maxExplicitWeight = std::max(maxExplicitWeight, layer.weight);
193 }
Marin Shalamanov46084422020-10-13 12:33:42 +0200194
Marin Shalamanov53fc11d2020-11-20 14:00:13 +0100195 if (layer.seamlessness == Seamlessness::SeamedAndSeamless) {
Marin Shalamanov46084422020-10-13 12:33:42 +0200196 seamedLayers++;
197 }
Ady Abraham6fb599b2020-03-05 13:48:22 -0800198 }
199
Alec Mouri11232a22020-05-14 18:06:25 -0700200 const bool hasExplicitVoteLayers =
201 explicitDefaultVoteLayers > 0 || explicitExactOrMultipleVoteLayers > 0;
202
Steven Thomasf734df42020-04-13 21:09:28 -0700203 // Consider the touch event if there are no Explicit* layers. Otherwise wait until after we've
204 // selected a refresh rate to see if we should apply touch boost.
Ady Abrahamdfd62162020-06-10 16:11:56 -0700205 if (globalSignals.touch && !hasExplicitVoteLayers) {
Ady Abrahama6b676e2020-05-27 14:29:09 -0700206 ALOGV("TouchBoost - choose %s", getMaxRefreshRateByPolicyLocked().getName().c_str());
Ady Abrahamdfd62162020-06-10 16:11:56 -0700207 setTouchConsidered();
Steven Thomasf734df42020-04-13 21:09:28 -0700208 return getMaxRefreshRateByPolicyLocked();
Ady Abraham8a82ba62020-01-17 12:43:17 -0800209 }
210
Alec Mouri11232a22020-05-14 18:06:25 -0700211 // If the primary range consists of a single refresh rate then we can only
212 // move out the of range if layers explicitly request a different refresh
213 // rate.
214 const Policy* policy = getCurrentPolicyLocked();
Marin Shalamanove8a663d2020-11-24 17:48:00 +0100215 const bool primaryRangeIsSingleRate =
216 policy->primaryRange.min.equalsWithMargin(policy->primaryRange.max);
Alec Mouri11232a22020-05-14 18:06:25 -0700217
Ady Abrahamdfd62162020-06-10 16:11:56 -0700218 if (!globalSignals.touch && globalSignals.idle &&
219 !(primaryRangeIsSingleRate && hasExplicitVoteLayers)) {
Ady Abrahama6b676e2020-05-27 14:29:09 -0700220 ALOGV("Idle - choose %s", getMinRefreshRateByPolicyLocked().getName().c_str());
Ady Abrahamdfd62162020-06-10 16:11:56 -0700221 setIdleConsidered();
Steven Thomasbb374322020-04-28 22:47:16 -0700222 return getMinRefreshRateByPolicyLocked();
223 }
224
Steven Thomasdebafed2020-05-18 17:30:35 -0700225 if (layers.empty() || noVoteLayers == layers.size()) {
226 return getMaxRefreshRateByPolicyLocked();
Steven Thomasbb374322020-04-28 22:47:16 -0700227 }
228
Ady Abraham8a82ba62020-01-17 12:43:17 -0800229 // Only if all layers want Min we should return Min
230 if (noVoteLayers + minVoteLayers == layers.size()) {
Ady Abrahama6b676e2020-05-27 14:29:09 -0700231 ALOGV("all layers Min - choose %s", getMinRefreshRateByPolicyLocked().getName().c_str());
Steven Thomasf734df42020-04-13 21:09:28 -0700232 return getMinRefreshRateByPolicyLocked();
Ady Abraham8a82ba62020-01-17 12:43:17 -0800233 }
234
Ady Abraham8a82ba62020-01-17 12:43:17 -0800235 // Find the best refresh rate based on score
Ady Abraham2c6716b2020-12-08 16:54:10 -0800236 std::vector<RefreshRateScore> scores;
Steven Thomasf734df42020-04-13 21:09:28 -0700237 scores.reserve(mAppRequestRefreshRates.size());
Ady Abraham8a82ba62020-01-17 12:43:17 -0800238
Steven Thomasf734df42020-04-13 21:09:28 -0700239 for (const auto refreshRate : mAppRequestRefreshRates) {
Ady Abraham2c6716b2020-12-08 16:54:10 -0800240 scores.emplace_back(RefreshRateScore{refreshRate, 0.0f});
Ady Abraham8a82ba62020-01-17 12:43:17 -0800241 }
242
Marin Shalamanov46084422020-10-13 12:33:42 +0200243 const auto& defaultConfig = mRefreshRates.at(policy->defaultConfig);
244
Ady Abraham8a82ba62020-01-17 12:43:17 -0800245 for (const auto& layer : layers) {
Ady Abrahama6b676e2020-05-27 14:29:09 -0700246 ALOGV("Calculating score for %s (%s, weight %.2f)", layer.name.c_str(),
247 layerVoteTypeString(layer.vote).c_str(), layer.weight);
Ady Abraham4ccdcb42020-02-11 17:34:34 -0800248 if (layer.vote == LayerVoteType::NoVote || layer.vote == LayerVoteType::Min) {
Ady Abraham8a82ba62020-01-17 12:43:17 -0800249 continue;
250 }
251
Ady Abraham71c437d2020-01-31 15:56:57 -0800252 auto weight = layer.weight;
Ady Abraham71c437d2020-01-31 15:56:57 -0800253
Ady Abraham4ccdcb42020-02-11 17:34:34 -0800254 for (auto i = 0u; i < scores.size(); i++) {
Ady Abraham2c6716b2020-12-08 16:54:10 -0800255 const bool isSeamlessSwitch = scores[i].refreshRate->getConfigGroup() ==
256 mCurrentRefreshRate->getConfigGroup();
Marin Shalamanov46084422020-10-13 12:33:42 +0200257
Marin Shalamanov53fc11d2020-11-20 14:00:13 +0100258 if (layer.seamlessness == Seamlessness::OnlySeamless && !isSeamlessSwitch) {
259 ALOGV("%s ignores %s to avoid non-seamless switch. Current config = %s",
Ady Abraham2c6716b2020-12-08 16:54:10 -0800260 formatLayerInfo(layer, weight).c_str(),
261 scores[i].refreshRate->toString().c_str(),
Marin Shalamanov53fc11d2020-11-20 14:00:13 +0100262 mCurrentRefreshRate->toString().c_str());
Marin Shalamanov46084422020-10-13 12:33:42 +0200263 continue;
264 }
265
Marin Shalamanov53fc11d2020-11-20 14:00:13 +0100266 if (layer.seamlessness == Seamlessness::SeamedAndSeamless && !isSeamlessSwitch &&
267 !layer.focused) {
268 ALOGV("%s ignores %s because it's not focused and the switch is going to be seamed."
269 " Current config = %s",
Ady Abraham2c6716b2020-12-08 16:54:10 -0800270 formatLayerInfo(layer, weight).c_str(),
271 scores[i].refreshRate->toString().c_str(),
Marin Shalamanov53fc11d2020-11-20 14:00:13 +0100272 mCurrentRefreshRate->toString().c_str());
273 continue;
274 }
275
276 // Layers with default seamlessness vote for the current config group if
277 // there are layers with seamlessness=SeamedAndSeamless and for the default
278 // config group otherwise. In second case, if the current config group is different
279 // from the default, this means a layer with seamlessness=SeamedAndSeamless has just
280 // disappeared.
281 const bool isInPolicyForDefault = seamedLayers > 0
Ady Abraham2c6716b2020-12-08 16:54:10 -0800282 ? scores[i].refreshRate->getConfigGroup() ==
283 mCurrentRefreshRate->getConfigGroup()
284 : scores[i].refreshRate->getConfigGroup() == defaultConfig->getConfigGroup();
Marin Shalamanov53fc11d2020-11-20 14:00:13 +0100285
286 if (layer.seamlessness == Seamlessness::Default && !isInPolicyForDefault &&
287 !layer.focused) {
288 ALOGV("%s ignores %s. Current config = %s", formatLayerInfo(layer, weight).c_str(),
Ady Abraham2c6716b2020-12-08 16:54:10 -0800289 scores[i].refreshRate->toString().c_str(),
290 mCurrentRefreshRate->toString().c_str());
Marin Shalamanov46084422020-10-13 12:33:42 +0200291 continue;
292 }
293
Ady Abraham2c6716b2020-12-08 16:54:10 -0800294 bool inPrimaryRange = scores[i].refreshRate->inPolicy(policy->primaryRange.min,
295 policy->primaryRange.max);
Alec Mouri11232a22020-05-14 18:06:25 -0700296 if ((primaryRangeIsSingleRate || !inPrimaryRange) &&
Ady Abraham20c029c2020-07-06 12:58:05 -0700297 !(layer.focused && layer.vote == LayerVoteType::ExplicitDefault)) {
298 // Only focused layers with ExplicitDefault frame rate settings are allowed to score
Ady Abrahamaae5ed52020-06-26 09:32:43 -0700299 // refresh rates outside the primary range.
Steven Thomasf734df42020-04-13 21:09:28 -0700300 continue;
301 }
302
Ady Abraham2c6716b2020-12-08 16:54:10 -0800303 const auto layerScore =
304 calculateLayerScoreLocked(layer, *scores[i].refreshRate, isSeamlessSwitch);
305 ALOGV("%s gives %s score of %.2f", formatLayerInfo(layer, weight).c_str(),
306 scores[i].refreshRate->getName().c_str(), layerScore);
307 scores[i].score += weight * layerScore;
Ady Abraham8a82ba62020-01-17 12:43:17 -0800308 }
309 }
310
Ady Abraham34702102020-02-10 14:12:05 -0800311 // Now that we scored all the refresh rates we need to pick the one that got the highest score.
312 // In case of a tie we will pick the higher refresh rate if any of the layers wanted Max,
313 // or the lower otherwise.
314 const RefreshRate* bestRefreshRate = maxVoteLayers > 0
315 ? getBestRefreshRate(scores.rbegin(), scores.rend())
316 : getBestRefreshRate(scores.begin(), scores.end());
317
Alec Mouri11232a22020-05-14 18:06:25 -0700318 if (primaryRangeIsSingleRate) {
319 // If we never scored any layers, then choose the rate from the primary
320 // range instead of picking a random score from the app range.
321 if (std::all_of(scores.begin(), scores.end(),
Ady Abraham2c6716b2020-12-08 16:54:10 -0800322 [](RefreshRateScore score) { return score.score == 0; })) {
Ady Abrahama6b676e2020-05-27 14:29:09 -0700323 ALOGV("layers not scored - choose %s",
324 getMaxRefreshRateByPolicyLocked().getName().c_str());
Alec Mouri11232a22020-05-14 18:06:25 -0700325 return getMaxRefreshRateByPolicyLocked();
326 } else {
327 return *bestRefreshRate;
328 }
329 }
330
Steven Thomasf734df42020-04-13 21:09:28 -0700331 // Consider the touch event if there are no ExplicitDefault layers. ExplicitDefault are mostly
332 // interactive (as opposed to ExplicitExactOrMultiple) and therefore if those posted an explicit
333 // vote we should not change it if we get a touch event. Only apply touch boost if it will
334 // actually increase the refresh rate over the normal selection.
335 const RefreshRate& touchRefreshRate = getMaxRefreshRateByPolicyLocked();
Alec Mouri11232a22020-05-14 18:06:25 -0700336
Ady Abrahamdfd62162020-06-10 16:11:56 -0700337 if (globalSignals.touch && explicitDefaultVoteLayers == 0 &&
Marin Shalamanove8a663d2020-11-24 17:48:00 +0100338 bestRefreshRate->fps.lessThanWithMargin(touchRefreshRate.fps)) {
Ady Abrahamdfd62162020-06-10 16:11:56 -0700339 setTouchConsidered();
Ady Abrahama6b676e2020-05-27 14:29:09 -0700340 ALOGV("TouchBoost - choose %s", touchRefreshRate.getName().c_str());
Steven Thomasf734df42020-04-13 21:09:28 -0700341 return touchRefreshRate;
342 }
343
Ady Abrahamde7156e2020-02-28 17:29:39 -0800344 return *bestRefreshRate;
Ady Abraham34702102020-02-10 14:12:05 -0800345}
346
Ady Abraham2c6716b2020-12-08 16:54:10 -0800347std::unordered_map<uid_t, std::vector<const RefreshRateConfigs::LayerRequirement*>>
348groupLayersByUid(const std::vector<RefreshRateConfigs::LayerRequirement>& layers) {
349 std::unordered_map<uid_t, std::vector<const RefreshRateConfigs::LayerRequirement*>> layersByUid;
350 for (const auto& layer : layers) {
351 auto iter = layersByUid.emplace(layer.ownerUid,
352 std::vector<const RefreshRateConfigs::LayerRequirement*>());
353 auto& layersWithSameUid = iter.first->second;
354 layersWithSameUid.push_back(&layer);
355 }
356
357 // Remove uids that can't have a frame rate override
358 for (auto iter = layersByUid.begin(); iter != layersByUid.end();) {
359 const auto& layersWithSameUid = iter->second;
360 bool skipUid = false;
361 for (const auto& layer : layersWithSameUid) {
362 if (layer->vote == RefreshRateConfigs::LayerVoteType::Max ||
363 layer->vote == RefreshRateConfigs::LayerVoteType::Heuristic) {
364 skipUid = true;
365 break;
366 }
367 }
368 if (skipUid) {
369 iter = layersByUid.erase(iter);
370 } else {
371 ++iter;
372 }
373 }
374
375 return layersByUid;
376}
377
378std::vector<RefreshRateScore> initializeScoresForAllRefreshRates(
379 const AllRefreshRatesMapType& refreshRates) {
380 std::vector<RefreshRateScore> scores;
381 scores.reserve(refreshRates.size());
382 for (const auto& [ignored, refreshRate] : refreshRates) {
383 scores.emplace_back(RefreshRateScore{refreshRate.get(), 0.0f});
384 }
385 std::sort(scores.begin(), scores.end(),
386 [](const auto& a, const auto& b) { return *a.refreshRate < *b.refreshRate; });
387 return scores;
388}
389
390RefreshRateConfigs::UidToFrameRateOverride RefreshRateConfigs::getFrameRateOverrides(
391 const std::vector<LayerRequirement>& layers, Fps displayFrameRate) const {
392 ATRACE_CALL();
393 ALOGV("getFrameRateOverrides %zu layers", layers.size());
394
395 std::lock_guard lock(mLock);
396 std::vector<RefreshRateScore> scores = initializeScoresForAllRefreshRates(mRefreshRates);
397 std::unordered_map<uid_t, std::vector<const LayerRequirement*>> layersByUid =
398 groupLayersByUid(layers);
399 UidToFrameRateOverride frameRateOverrides;
400 for (const auto& [uid, layersWithSameUid] : layersByUid) {
401 for (auto& score : scores) {
402 score.score = 0;
403 }
404
405 for (const auto& layer : layersWithSameUid) {
406 if (layer->vote == LayerVoteType::NoVote || layer->vote == LayerVoteType::Min) {
407 continue;
408 }
409
410 LOG_ALWAYS_FATAL_IF(layer->vote != LayerVoteType::ExplicitDefault &&
411 layer->vote != LayerVoteType::ExplicitExactOrMultiple);
412 for (RefreshRateScore& score : scores) {
413 const auto layerScore = calculateLayerScoreLocked(*layer, *score.refreshRate,
414 /*isSeamlessSwitch*/ true);
415 score.score += layer->weight * layerScore;
416 }
417 }
418
419 // We just care about the refresh rates which are a divider of the
420 // display refresh rate
421 auto iter =
422 std::remove_if(scores.begin(), scores.end(), [&](const RefreshRateScore& score) {
423 return getFrameRateDivider(displayFrameRate, score.refreshRate->getFps()) == 0;
424 });
425 scores.erase(iter, scores.end());
426
427 // If we never scored any layers, we don't have a preferred frame rate
428 if (std::all_of(scores.begin(), scores.end(),
429 [](const RefreshRateScore& score) { return score.score == 0; })) {
430 continue;
431 }
432
433 // Now that we scored all the refresh rates we need to pick the one that got the highest
434 // score.
435 const RefreshRate* bestRefreshRate = getBestRefreshRate(scores.begin(), scores.end());
436
437 // If the nest refresh rate is the current one, we don't have an override
438 if (!bestRefreshRate->getFps().equalsWithMargin(displayFrameRate)) {
439 frameRateOverrides.emplace(uid, bestRefreshRate->getFps());
440 }
441 }
442
443 return frameRateOverrides;
444}
445
Ady Abraham34702102020-02-10 14:12:05 -0800446template <typename Iter>
447const RefreshRate* RefreshRateConfigs::getBestRefreshRate(Iter begin, Iter end) const {
Ady Abraham5b8afb5a2020-03-06 14:57:26 -0800448 constexpr auto EPSILON = 0.001f;
Ady Abraham2c6716b2020-12-08 16:54:10 -0800449 const RefreshRate* bestRefreshRate = begin->refreshRate;
450 float max = begin->score;
Ady Abraham34702102020-02-10 14:12:05 -0800451 for (auto i = begin; i != end; ++i) {
452 const auto [refreshRate, score] = *i;
Marin Shalamanove8a663d2020-11-24 17:48:00 +0100453 ALOGV("%s scores %.2f", refreshRate->getName().c_str(), score);
Ady Abraham8a82ba62020-01-17 12:43:17 -0800454
Marin Shalamanove8a663d2020-11-24 17:48:00 +0100455 ATRACE_INT(refreshRate->getName().c_str(), round<int>(score * 100));
Ady Abraham8a82ba62020-01-17 12:43:17 -0800456
Ady Abraham5b8afb5a2020-03-06 14:57:26 -0800457 if (score > max * (1 + EPSILON)) {
Ady Abraham8a82ba62020-01-17 12:43:17 -0800458 max = score;
459 bestRefreshRate = refreshRate;
460 }
461 }
462
Ady Abraham34702102020-02-10 14:12:05 -0800463 return bestRefreshRate;
Ady Abraham8a82ba62020-01-17 12:43:17 -0800464}
465
Ady Abraham2139f732019-11-13 18:56:40 -0800466const AllRefreshRatesMapType& RefreshRateConfigs::getAllRefreshRates() const {
467 return mRefreshRates;
468}
469
470const RefreshRate& RefreshRateConfigs::getMinRefreshRateByPolicy() const {
471 std::lock_guard lock(mLock);
Steven Thomasf734df42020-04-13 21:09:28 -0700472 return getMinRefreshRateByPolicyLocked();
473}
474
475const RefreshRate& RefreshRateConfigs::getMinRefreshRateByPolicyLocked() const {
Marin Shalamanov46084422020-10-13 12:33:42 +0200476 for (auto refreshRate : mPrimaryRefreshRates) {
477 if (mCurrentRefreshRate->getConfigGroup() == refreshRate->getConfigGroup()) {
478 return *refreshRate;
479 }
480 }
481 ALOGE("Can't find min refresh rate by policy with the same config group"
482 " as the current config %s",
483 mCurrentRefreshRate->toString().c_str());
484 // Defaulting to the lowest refresh rate
Steven Thomasf734df42020-04-13 21:09:28 -0700485 return *mPrimaryRefreshRates.front();
Ady Abraham2139f732019-11-13 18:56:40 -0800486}
487
488const RefreshRate& RefreshRateConfigs::getMaxRefreshRateByPolicy() const {
489 std::lock_guard lock(mLock);
Steven Thomasf734df42020-04-13 21:09:28 -0700490 return getMaxRefreshRateByPolicyLocked();
491}
492
493const RefreshRate& RefreshRateConfigs::getMaxRefreshRateByPolicyLocked() const {
Marin Shalamanov46084422020-10-13 12:33:42 +0200494 for (auto it = mPrimaryRefreshRates.rbegin(); it != mPrimaryRefreshRates.rend(); it++) {
495 const auto& refreshRate = (**it);
496 if (mCurrentRefreshRate->getConfigGroup() == refreshRate.getConfigGroup()) {
497 return refreshRate;
498 }
499 }
500 ALOGE("Can't find max refresh rate by policy with the same config group"
501 " as the current config %s",
502 mCurrentRefreshRate->toString().c_str());
503 // Defaulting to the highest refresh rate
Steven Thomasf734df42020-04-13 21:09:28 -0700504 return *mPrimaryRefreshRates.back();
Ady Abraham2139f732019-11-13 18:56:40 -0800505}
506
507const RefreshRate& RefreshRateConfigs::getCurrentRefreshRate() const {
508 std::lock_guard lock(mLock);
509 return *mCurrentRefreshRate;
510}
511
Ana Krulec5d477912020-02-07 12:02:38 -0800512const RefreshRate& RefreshRateConfigs::getCurrentRefreshRateByPolicy() const {
513 std::lock_guard lock(mLock);
Ana Krulec3d367c82020-02-25 15:02:01 -0800514 return getCurrentRefreshRateByPolicyLocked();
515}
516
517const RefreshRate& RefreshRateConfigs::getCurrentRefreshRateByPolicyLocked() const {
Steven Thomasf734df42020-04-13 21:09:28 -0700518 if (std::find(mAppRequestRefreshRates.begin(), mAppRequestRefreshRates.end(),
519 mCurrentRefreshRate) != mAppRequestRefreshRates.end()) {
Ana Krulec5d477912020-02-07 12:02:38 -0800520 return *mCurrentRefreshRate;
521 }
Steven Thomasd4071902020-03-24 16:02:53 -0700522 return *mRefreshRates.at(getCurrentPolicyLocked()->defaultConfig);
Ana Krulec5d477912020-02-07 12:02:38 -0800523}
524
Ady Abraham2139f732019-11-13 18:56:40 -0800525void RefreshRateConfigs::setCurrentConfigId(HwcConfigIndexType configId) {
526 std::lock_guard lock(mLock);
Ady Abraham2e1dd892020-03-05 13:48:36 -0800527 mCurrentRefreshRate = mRefreshRates.at(configId).get();
Ady Abrahamb4b1e0a2019-11-20 18:25:35 -0800528}
529
Ady Abrahamb4b1e0a2019-11-20 18:25:35 -0800530RefreshRateConfigs::RefreshRateConfigs(
Ady Abrahamb4b1e0a2019-11-20 18:25:35 -0800531 const std::vector<std::shared_ptr<const HWC2::Display::Config>>& configs,
Ady Abrahamb1b9d412020-06-01 19:53:52 -0700532 HwcConfigIndexType currentConfigId)
533 : mKnownFrameRates(constructKnownFrameRates(configs)) {
Ady Abrahamabc27602020-04-08 17:20:29 -0700534 LOG_ALWAYS_FATAL_IF(configs.empty());
535 LOG_ALWAYS_FATAL_IF(currentConfigId.value() >= configs.size());
536
537 for (auto configId = HwcConfigIndexType(0); configId.value() < configs.size(); configId++) {
538 const auto& config = configs.at(static_cast<size_t>(configId.value()));
Ady Abrahamabc27602020-04-08 17:20:29 -0700539 mRefreshRates.emplace(configId,
540 std::make_unique<RefreshRate>(configId, config,
Marin Shalamanove8a663d2020-11-24 17:48:00 +0100541 Fps::fromPeriodNsecs(
542 config->getVsyncPeriod()),
Ady Abrahamabc27602020-04-08 17:20:29 -0700543 RefreshRate::ConstructorTag(0)));
544 if (configId == currentConfigId) {
545 mCurrentRefreshRate = mRefreshRates.at(configId).get();
546 }
Ady Abrahamb4b1e0a2019-11-20 18:25:35 -0800547 }
Ady Abrahamabc27602020-04-08 17:20:29 -0700548
549 std::vector<const RefreshRate*> sortedConfigs;
550 getSortedRefreshRateList([](const RefreshRate&) { return true; }, &sortedConfigs);
551 mDisplayManagerPolicy.defaultConfig = currentConfigId;
552 mMinSupportedRefreshRate = sortedConfigs.front();
553 mMaxSupportedRefreshRate = sortedConfigs.back();
554 constructAvailableRefreshRates();
Ady Abrahamb4b1e0a2019-11-20 18:25:35 -0800555}
556
Steven Thomasd4071902020-03-24 16:02:53 -0700557bool RefreshRateConfigs::isPolicyValid(const Policy& policy) {
558 // defaultConfig must be a valid config, and within the given refresh rate range.
559 auto iter = mRefreshRates.find(policy.defaultConfig);
560 if (iter == mRefreshRates.end()) {
Marin Shalamanovb6674e72020-11-06 13:05:57 +0100561 ALOGE("Default config is not found.");
Steven Thomasd4071902020-03-24 16:02:53 -0700562 return false;
563 }
564 const RefreshRate& refreshRate = *iter->second;
Steven Thomasf734df42020-04-13 21:09:28 -0700565 if (!refreshRate.inPolicy(policy.primaryRange.min, policy.primaryRange.max)) {
Marin Shalamanovb6674e72020-11-06 13:05:57 +0100566 ALOGE("Default config is not in the primary range.");
Steven Thomasd4071902020-03-24 16:02:53 -0700567 return false;
568 }
Marin Shalamanove8a663d2020-11-24 17:48:00 +0100569 return policy.appRequestRange.min.lessThanOrEqualWithMargin(policy.primaryRange.min) &&
570 policy.appRequestRange.max.greaterThanOrEqualWithMargin(policy.primaryRange.max);
Steven Thomasd4071902020-03-24 16:02:53 -0700571}
572
573status_t RefreshRateConfigs::setDisplayManagerPolicy(const Policy& policy) {
Ady Abraham2139f732019-11-13 18:56:40 -0800574 std::lock_guard lock(mLock);
Steven Thomasd4071902020-03-24 16:02:53 -0700575 if (!isPolicyValid(policy)) {
Marin Shalamanovb6674e72020-11-06 13:05:57 +0100576 ALOGE("Invalid refresh rate policy: %s", policy.toString().c_str());
Ana Kruleced3a8cc2019-11-14 00:55:07 +0100577 return BAD_VALUE;
578 }
Steven Thomasd4071902020-03-24 16:02:53 -0700579 Policy previousPolicy = *getCurrentPolicyLocked();
580 mDisplayManagerPolicy = policy;
581 if (*getCurrentPolicyLocked() == previousPolicy) {
582 return CURRENT_POLICY_UNCHANGED;
Ana Kruleced3a8cc2019-11-14 00:55:07 +0100583 }
Ady Abraham2139f732019-11-13 18:56:40 -0800584 constructAvailableRefreshRates();
Ana Kruleced3a8cc2019-11-14 00:55:07 +0100585 return NO_ERROR;
586}
587
Steven Thomasd4071902020-03-24 16:02:53 -0700588status_t RefreshRateConfigs::setOverridePolicy(const std::optional<Policy>& policy) {
Ana Kruleced3a8cc2019-11-14 00:55:07 +0100589 std::lock_guard lock(mLock);
Steven Thomasd4071902020-03-24 16:02:53 -0700590 if (policy && !isPolicyValid(*policy)) {
591 return BAD_VALUE;
592 }
593 Policy previousPolicy = *getCurrentPolicyLocked();
594 mOverridePolicy = policy;
595 if (*getCurrentPolicyLocked() == previousPolicy) {
596 return CURRENT_POLICY_UNCHANGED;
597 }
598 constructAvailableRefreshRates();
599 return NO_ERROR;
600}
601
602const RefreshRateConfigs::Policy* RefreshRateConfigs::getCurrentPolicyLocked() const {
603 return mOverridePolicy ? &mOverridePolicy.value() : &mDisplayManagerPolicy;
604}
605
606RefreshRateConfigs::Policy RefreshRateConfigs::getCurrentPolicy() const {
607 std::lock_guard lock(mLock);
608 return *getCurrentPolicyLocked();
609}
610
611RefreshRateConfigs::Policy RefreshRateConfigs::getDisplayManagerPolicy() const {
612 std::lock_guard lock(mLock);
613 return mDisplayManagerPolicy;
Ana Kruleced3a8cc2019-11-14 00:55:07 +0100614}
615
616bool RefreshRateConfigs::isConfigAllowed(HwcConfigIndexType config) const {
617 std::lock_guard lock(mLock);
Steven Thomasf734df42020-04-13 21:09:28 -0700618 for (const RefreshRate* refreshRate : mAppRequestRefreshRates) {
Ana Kruleced3a8cc2019-11-14 00:55:07 +0100619 if (refreshRate->configId == config) {
620 return true;
621 }
622 }
623 return false;
Ady Abraham2139f732019-11-13 18:56:40 -0800624}
625
626void RefreshRateConfigs::getSortedRefreshRateList(
627 const std::function<bool(const RefreshRate&)>& shouldAddRefreshRate,
628 std::vector<const RefreshRate*>* outRefreshRates) {
629 outRefreshRates->clear();
630 outRefreshRates->reserve(mRefreshRates.size());
631 for (const auto& [type, refreshRate] : mRefreshRates) {
Ady Abraham2e1dd892020-03-05 13:48:36 -0800632 if (shouldAddRefreshRate(*refreshRate)) {
Ady Abraham2139f732019-11-13 18:56:40 -0800633 ALOGV("getSortedRefreshRateList: config %d added to list policy",
Ady Abraham2e1dd892020-03-05 13:48:36 -0800634 refreshRate->configId.value());
635 outRefreshRates->push_back(refreshRate.get());
Ady Abraham2139f732019-11-13 18:56:40 -0800636 }
637 }
638
639 std::sort(outRefreshRates->begin(), outRefreshRates->end(),
640 [](const auto refreshRate1, const auto refreshRate2) {
Ady Abrahamabc27602020-04-08 17:20:29 -0700641 if (refreshRate1->hwcConfig->getVsyncPeriod() !=
642 refreshRate2->hwcConfig->getVsyncPeriod()) {
643 return refreshRate1->hwcConfig->getVsyncPeriod() >
644 refreshRate2->hwcConfig->getVsyncPeriod();
Steven Thomasd4071902020-03-24 16:02:53 -0700645 } else {
Ady Abrahamabc27602020-04-08 17:20:29 -0700646 return refreshRate1->hwcConfig->getConfigGroup() >
647 refreshRate2->hwcConfig->getConfigGroup();
Steven Thomasd4071902020-03-24 16:02:53 -0700648 }
Ady Abraham2139f732019-11-13 18:56:40 -0800649 });
650}
651
652void RefreshRateConfigs::constructAvailableRefreshRates() {
653 // Filter configs based on current policy and sort based on vsync period
Steven Thomasd4071902020-03-24 16:02:53 -0700654 const Policy* policy = getCurrentPolicyLocked();
Ady Abrahamabc27602020-04-08 17:20:29 -0700655 const auto& defaultConfig = mRefreshRates.at(policy->defaultConfig)->hwcConfig;
Marin Shalamanove8a663d2020-11-24 17:48:00 +0100656 ALOGV("constructAvailableRefreshRates: %s ", policy->toString().c_str());
Ady Abrahamabc27602020-04-08 17:20:29 -0700657
Marin Shalamanove8a663d2020-11-24 17:48:00 +0100658 auto filterRefreshRates = [&](Fps min, Fps max, const char* listName,
Steven Thomasf734df42020-04-13 21:09:28 -0700659 std::vector<const RefreshRate*>* outRefreshRates) {
660 getSortedRefreshRateList(
661 [&](const RefreshRate& refreshRate) REQUIRES(mLock) {
662 const auto& hwcConfig = refreshRate.hwcConfig;
Ady Abraham8a82ba62020-01-17 12:43:17 -0800663
Steven Thomasf734df42020-04-13 21:09:28 -0700664 return hwcConfig->getHeight() == defaultConfig->getHeight() &&
665 hwcConfig->getWidth() == defaultConfig->getWidth() &&
666 hwcConfig->getDpiX() == defaultConfig->getDpiX() &&
667 hwcConfig->getDpiY() == defaultConfig->getDpiY() &&
668 (policy->allowGroupSwitching ||
669 hwcConfig->getConfigGroup() == defaultConfig->getConfigGroup()) &&
670 refreshRate.inPolicy(min, max);
671 },
672 outRefreshRates);
Ady Abraham8a82ba62020-01-17 12:43:17 -0800673
Steven Thomasf734df42020-04-13 21:09:28 -0700674 LOG_ALWAYS_FATAL_IF(outRefreshRates->empty(),
Marin Shalamanove8a663d2020-11-24 17:48:00 +0100675 "No matching configs for %s range: min=%s max=%s", listName,
676 to_string(min).c_str(), to_string(max).c_str());
Steven Thomasf734df42020-04-13 21:09:28 -0700677 auto stringifyRefreshRates = [&]() -> std::string {
678 std::string str;
679 for (auto refreshRate : *outRefreshRates) {
Marin Shalamanove8a663d2020-11-24 17:48:00 +0100680 base::StringAppendF(&str, "%s ", refreshRate->getName().c_str());
Steven Thomasf734df42020-04-13 21:09:28 -0700681 }
682 return str;
683 };
684 ALOGV("%s refresh rates: %s", listName, stringifyRefreshRates().c_str());
685 };
686
687 filterRefreshRates(policy->primaryRange.min, policy->primaryRange.max, "primary",
688 &mPrimaryRefreshRates);
689 filterRefreshRates(policy->appRequestRange.min, policy->appRequestRange.max, "app request",
690 &mAppRequestRefreshRates);
Ady Abraham2139f732019-11-13 18:56:40 -0800691}
692
Marin Shalamanove8a663d2020-11-24 17:48:00 +0100693std::vector<Fps> RefreshRateConfigs::constructKnownFrameRates(
Ady Abrahamb1b9d412020-06-01 19:53:52 -0700694 const std::vector<std::shared_ptr<const HWC2::Display::Config>>& configs) {
Marin Shalamanove8a663d2020-11-24 17:48:00 +0100695 std::vector<Fps> knownFrameRates = {Fps(24.0f), Fps(30.0f), Fps(45.0f), Fps(60.0f), Fps(72.0f)};
Ady Abrahamb1b9d412020-06-01 19:53:52 -0700696 knownFrameRates.reserve(knownFrameRates.size() + configs.size());
697
698 // Add all supported refresh rates to the set
699 for (const auto& config : configs) {
Marin Shalamanove8a663d2020-11-24 17:48:00 +0100700 const auto refreshRate = Fps::fromPeriodNsecs(config->getVsyncPeriod());
Ady Abrahamb1b9d412020-06-01 19:53:52 -0700701 knownFrameRates.emplace_back(refreshRate);
702 }
703
704 // Sort and remove duplicates
Marin Shalamanove8a663d2020-11-24 17:48:00 +0100705 std::sort(knownFrameRates.begin(), knownFrameRates.end(), Fps::comparesLess);
Ady Abrahamb1b9d412020-06-01 19:53:52 -0700706 knownFrameRates.erase(std::unique(knownFrameRates.begin(), knownFrameRates.end(),
Marin Shalamanove8a663d2020-11-24 17:48:00 +0100707 Fps::EqualsWithMargin()),
Ady Abrahamb1b9d412020-06-01 19:53:52 -0700708 knownFrameRates.end());
709 return knownFrameRates;
710}
711
Marin Shalamanove8a663d2020-11-24 17:48:00 +0100712Fps RefreshRateConfigs::findClosestKnownFrameRate(Fps frameRate) const {
713 if (frameRate.lessThanOrEqualWithMargin(*mKnownFrameRates.begin())) {
Ady Abrahamb1b9d412020-06-01 19:53:52 -0700714 return *mKnownFrameRates.begin();
715 }
716
Marin Shalamanove8a663d2020-11-24 17:48:00 +0100717 if (frameRate.greaterThanOrEqualWithMargin(*std::prev(mKnownFrameRates.end()))) {
Ady Abrahamb1b9d412020-06-01 19:53:52 -0700718 return *std::prev(mKnownFrameRates.end());
719 }
720
Marin Shalamanove8a663d2020-11-24 17:48:00 +0100721 auto lowerBound = std::lower_bound(mKnownFrameRates.begin(), mKnownFrameRates.end(), frameRate,
722 Fps::comparesLess);
Ady Abrahamb1b9d412020-06-01 19:53:52 -0700723
Marin Shalamanove8a663d2020-11-24 17:48:00 +0100724 const auto distance1 = std::abs((frameRate.getValue() - lowerBound->getValue()));
725 const auto distance2 = std::abs((frameRate.getValue() - std::prev(lowerBound)->getValue()));
Ady Abrahamb1b9d412020-06-01 19:53:52 -0700726 return distance1 < distance2 ? *lowerBound : *std::prev(lowerBound);
727}
728
Ana Krulecb9afd792020-06-11 13:16:15 -0700729RefreshRateConfigs::KernelIdleTimerAction RefreshRateConfigs::getIdleTimerAction() const {
730 std::lock_guard lock(mLock);
731 const auto& deviceMin = getMinRefreshRate();
732 const auto& minByPolicy = getMinRefreshRateByPolicyLocked();
733 const auto& maxByPolicy = getMaxRefreshRateByPolicyLocked();
734
735 // Kernel idle timer will set the refresh rate to the device min. If DisplayManager says that
736 // the min allowed refresh rate is higher than the device min, we do not want to enable the
737 // timer.
738 if (deviceMin < minByPolicy) {
739 return RefreshRateConfigs::KernelIdleTimerAction::TurnOff;
740 }
741 if (minByPolicy == maxByPolicy) {
742 // Do not sent the call to toggle off kernel idle timer if the device min and policy min and
743 // max are all the same. This saves us extra unnecessary calls to sysprop.
744 if (deviceMin == minByPolicy) {
745 return RefreshRateConfigs::KernelIdleTimerAction::NoChange;
746 }
747 return RefreshRateConfigs::KernelIdleTimerAction::TurnOff;
748 }
749 // Turn on the timer in all other cases.
750 return RefreshRateConfigs::KernelIdleTimerAction::TurnOn;
751}
752
Ady Abraham2c6716b2020-12-08 16:54:10 -0800753int RefreshRateConfigs::getFrameRateDivider(Fps displayFrameRate, Fps layerFrameRate) {
Ady Abraham62f216c2020-10-13 19:07:23 -0700754 // This calculation needs to be in sync with the java code
755 // in DisplayManagerService.getDisplayInfoForFrameRateOverride
756 constexpr float kThreshold = 0.1f;
Ady Abraham2c6716b2020-12-08 16:54:10 -0800757 const auto numPeriods = displayFrameRate.getValue() / layerFrameRate.getValue();
Ady Abraham0bb6a472020-10-12 10:22:13 -0700758 const auto numPeriodsRounded = std::round(numPeriods);
759 if (std::abs(numPeriods - numPeriodsRounded) > kThreshold) {
Ady Abraham2c6716b2020-12-08 16:54:10 -0800760 return 0;
Ady Abraham0bb6a472020-10-12 10:22:13 -0700761 }
762
Ady Abraham62f216c2020-10-13 19:07:23 -0700763 return static_cast<int>(numPeriodsRounded);
764}
765
Ady Abraham2c6716b2020-12-08 16:54:10 -0800766int RefreshRateConfigs::getRefreshRateDivider(Fps frameRate) const {
Ady Abraham62f216c2020-10-13 19:07:23 -0700767 std::lock_guard lock(mLock);
Ady Abraham2c6716b2020-12-08 16:54:10 -0800768 return getFrameRateDivider(mCurrentRefreshRate->getFps(), frameRate);
Ady Abraham0bb6a472020-10-12 10:22:13 -0700769}
770
Marin Shalamanovba421a82020-11-10 21:49:26 +0100771void RefreshRateConfigs::dump(std::string& result) const {
772 std::lock_guard lock(mLock);
773 base::StringAppendF(&result, "DesiredDisplayConfigSpecs (DisplayManager): %s\n\n",
774 mDisplayManagerPolicy.toString().c_str());
775 scheduler::RefreshRateConfigs::Policy currentPolicy = *getCurrentPolicyLocked();
776 if (mOverridePolicy && currentPolicy != mDisplayManagerPolicy) {
777 base::StringAppendF(&result, "DesiredDisplayConfigSpecs (Override): %s\n\n",
778 currentPolicy.toString().c_str());
779 }
780
781 auto config = mCurrentRefreshRate->hwcConfig;
782 base::StringAppendF(&result, "Current config: %s\n", mCurrentRefreshRate->toString().c_str());
783
784 result.append("Refresh rates:\n");
785 for (const auto& [id, refreshRate] : mRefreshRates) {
786 config = refreshRate->hwcConfig;
787 base::StringAppendF(&result, "\t%s\n", refreshRate->toString().c_str());
788 }
789
790 result.append("\n");
791}
792
Ady Abraham2139f732019-11-13 18:56:40 -0800793} // namespace android::scheduler