blob: 00886f030eeb3cc9e1d867b2e7fa5ecd4ea6a9dc [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
Marin Shalamanovbed7fd32020-12-21 20:02:20 +010020// TODO(b/129481165): remove the #pragma below and fix conversion issues
21#pragma clang diagnostic push
22#pragma clang diagnostic ignored "-Wextra"
23
Ady Abraham8a82ba62020-01-17 12:43:17 -080024#include <chrono>
25#include <cmath>
Dominik Laskowskif5d0ea52021-09-26 17:27:01 -070026
27#include <android-base/properties.h>
28#include <android-base/stringprintf.h>
29#include <ftl/enum.h>
Dominik Laskowskif8734e02022-08-26 09:06:59 -070030#include <ftl/fake_guard.h>
Dominik Laskowskif5d0ea52021-09-26 17:27:01 -070031#include <utils/Trace.h>
32
Ady Abraham4899ff82021-01-06 13:53:29 -080033#include "../SurfaceFlingerProperties.h"
Dominik Laskowskif5d0ea52021-09-26 17:27:01 -070034#include "RefreshRateConfigs.h"
Ady Abraham8a82ba62020-01-17 12:43:17 -080035
Ady Abraham5b8afb5a2020-03-06 14:57:26 -080036#undef LOG_TAG
37#define LOG_TAG "RefreshRateConfigs"
38
Ady Abrahamb4b1e0a2019-11-20 18:25:35 -080039namespace android::scheduler {
Marin Shalamanov53fc11d2020-11-20 14:00:13 +010040namespace {
Dominik Laskowskif5d0ea52021-09-26 17:27:01 -070041
Dominik Laskowskib0054a22022-03-03 09:03:06 -080042struct RefreshRateScore {
43 DisplayModeIterator modeIt;
Ady Abrahamae2e3c72022-08-13 05:12:13 +000044 float overallScore;
45 struct {
Ady Abraham62f51d92022-08-24 22:20:22 +000046 float modeBelowThreshold;
47 float modeAboveThreshold;
48 } fixedRateBelowThresholdLayersScore;
Dominik Laskowskib0054a22022-03-03 09:03:06 -080049};
50
51template <typename Iterator>
52const DisplayModePtr& getMaxScoreRefreshRate(Iterator begin, Iterator end) {
53 const auto it =
54 std::max_element(begin, end, [](RefreshRateScore max, RefreshRateScore current) {
Ady Abrahamae2e3c72022-08-13 05:12:13 +000055 const auto& [modeIt, overallScore, _] = current;
Dominik Laskowskib0054a22022-03-03 09:03:06 -080056
57 std::string name = to_string(modeIt->second->getFps());
Ady Abrahamae2e3c72022-08-13 05:12:13 +000058 ALOGV("%s scores %.2f", name.c_str(), overallScore);
Dominik Laskowskib0054a22022-03-03 09:03:06 -080059
Ady Abrahamae2e3c72022-08-13 05:12:13 +000060 ATRACE_INT(name.c_str(), static_cast<int>(std::round(overallScore * 100)));
Dominik Laskowskib0054a22022-03-03 09:03:06 -080061
62 constexpr float kEpsilon = 0.0001f;
Ady Abrahamae2e3c72022-08-13 05:12:13 +000063 return overallScore > max.overallScore * (1 + kEpsilon);
Dominik Laskowskib0054a22022-03-03 09:03:06 -080064 });
65
66 return it->modeIt->second;
67}
68
Dominik Laskowskia8626ec2021-12-15 18:13:30 -080069constexpr RefreshRateConfigs::GlobalSignals kNoSignals;
70
Marin Shalamanov53fc11d2020-11-20 14:00:13 +010071std::string formatLayerInfo(const RefreshRateConfigs::LayerRequirement& layer, float weight) {
Dominik Laskowskib0054a22022-03-03 09:03:06 -080072 return base::StringPrintf("%s (type=%s, weight=%.2f, seamlessness=%s) %s", layer.name.c_str(),
Dominik Laskowskif5d0ea52021-09-26 17:27:01 -070073 ftl::enum_string(layer.vote).c_str(), weight,
74 ftl::enum_string(layer.seamlessness).c_str(),
Marin Shalamanove8a663d2020-11-24 17:48:00 +010075 to_string(layer.desiredRefreshRate).c_str());
Marin Shalamanov53fc11d2020-11-20 14:00:13 +010076}
Marin Shalamanoveadf2e72020-12-10 15:35:28 +010077
Marin Shalamanova7fe3042021-01-29 21:02:08 +010078std::vector<Fps> constructKnownFrameRates(const DisplayModes& modes) {
Dominik Laskowski6eab42d2021-09-13 14:34:13 -070079 std::vector<Fps> knownFrameRates = {24_Hz, 30_Hz, 45_Hz, 60_Hz, 72_Hz};
Marin Shalamanova7fe3042021-01-29 21:02:08 +010080 knownFrameRates.reserve(knownFrameRates.size() + modes.size());
Marin Shalamanoveadf2e72020-12-10 15:35:28 +010081
Dominik Laskowski6eab42d2021-09-13 14:34:13 -070082 // Add all supported refresh rates.
Dominik Laskowskib0054a22022-03-03 09:03:06 -080083 for (const auto& [id, mode] : modes) {
84 knownFrameRates.push_back(mode->getFps());
Marin Shalamanoveadf2e72020-12-10 15:35:28 +010085 }
86
Dominik Laskowski6eab42d2021-09-13 14:34:13 -070087 // Sort and remove duplicates.
88 std::sort(knownFrameRates.begin(), knownFrameRates.end(), isStrictlyLess);
Marin Shalamanoveadf2e72020-12-10 15:35:28 +010089 knownFrameRates.erase(std::unique(knownFrameRates.begin(), knownFrameRates.end(),
Dominik Laskowski6eab42d2021-09-13 14:34:13 -070090 isApproxEqual),
Marin Shalamanoveadf2e72020-12-10 15:35:28 +010091 knownFrameRates.end());
92 return knownFrameRates;
93}
94
Dominik Laskowskib0054a22022-03-03 09:03:06 -080095// The Filter is a `bool(const DisplayMode&)` predicate.
96template <typename Filter>
97std::vector<DisplayModeIterator> sortByRefreshRate(const DisplayModes& modes, Filter&& filter) {
98 std::vector<DisplayModeIterator> sortedModes;
99 sortedModes.reserve(modes.size());
Ady Abraham2139f732019-11-13 18:56:40 -0800100
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800101 for (auto it = modes.begin(); it != modes.end(); ++it) {
102 const auto& [id, mode] = *it;
Ady Abrahamb4b1e0a2019-11-20 18:25:35 -0800103
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800104 if (filter(*mode)) {
105 ALOGV("%s: including mode %d", __func__, id.value());
106 sortedModes.push_back(it);
107 }
108 }
109
110 std::sort(sortedModes.begin(), sortedModes.end(), [](auto it1, auto it2) {
111 const auto& mode1 = it1->second;
112 const auto& mode2 = it2->second;
113
114 if (mode1->getVsyncPeriod() == mode2->getVsyncPeriod()) {
115 return mode1->getGroup() > mode2->getGroup();
116 }
117
118 return mode1->getVsyncPeriod() > mode2->getVsyncPeriod();
119 });
120
121 return sortedModes;
Marin Shalamanov46084422020-10-13 12:33:42 +0200122}
123
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800124bool canModesSupportFrameRateOverride(const std::vector<DisplayModeIterator>& sortedModes) {
125 for (const auto it1 : sortedModes) {
126 const auto& mode1 = it1->second;
127 for (const auto it2 : sortedModes) {
128 const auto& mode2 = it2->second;
129
130 if (RefreshRateConfigs::getFrameRateDivisor(mode1->getFps(), mode2->getFps()) >= 2) {
131 return true;
132 }
133 }
134 }
135 return false;
136}
137
138} // namespace
139
Marin Shalamanovb6674e72020-11-06 13:05:57 +0100140std::string RefreshRateConfigs::Policy::toString() const {
Dominik Laskowski0acc3842022-04-07 11:23:42 -0700141 return base::StringPrintf("{defaultModeId=%d, allowGroupSwitching=%s"
142 ", primaryRange=%s, appRequestRange=%s}",
143 defaultMode.value(), allowGroupSwitching ? "true" : "false",
Dominik Laskowski953b7fd2022-01-08 19:34:59 -0800144 to_string(primaryRange).c_str(), to_string(appRequestRange).c_str());
Marin Shalamanov30b0b3c2020-10-13 19:15:06 +0200145}
146
Ady Abraham4ccdcb42020-02-11 17:34:34 -0800147std::pair<nsecs_t, nsecs_t> RefreshRateConfigs::getDisplayFrames(nsecs_t layerPeriod,
148 nsecs_t displayPeriod) const {
Ady Abraham62a0be22020-12-08 16:54:10 -0800149 auto [quotient, remainder] = std::div(layerPeriod, displayPeriod);
150 if (remainder <= MARGIN_FOR_PERIOD_CALCULATION ||
151 std::abs(remainder - displayPeriod) <= MARGIN_FOR_PERIOD_CALCULATION) {
152 quotient++;
153 remainder = 0;
Ady Abraham4ccdcb42020-02-11 17:34:34 -0800154 }
155
Ady Abraham62a0be22020-12-08 16:54:10 -0800156 return {quotient, remainder};
Ady Abraham4ccdcb42020-02-11 17:34:34 -0800157}
158
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800159float RefreshRateConfigs::calculateNonExactMatchingLayerScoreLocked(const LayerRequirement& layer,
160 Fps refreshRate) const {
Marin Shalamanov15a0fc62021-08-16 18:20:21 +0200161 constexpr float kScoreForFractionalPairs = .8f;
162
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800163 const auto displayPeriod = refreshRate.getPeriodNsecs();
Ady Abraham62a0be22020-12-08 16:54:10 -0800164 const auto layerPeriod = layer.desiredRefreshRate.getPeriodNsecs();
165 if (layer.vote == LayerVoteType::ExplicitDefault) {
166 // Find the actual rate the layer will render, assuming
Marin Shalamanov15a0fc62021-08-16 18:20:21 +0200167 // that layerPeriod is the minimal period to render a frame.
168 // For example if layerPeriod is 20ms and displayPeriod is 16ms,
169 // then the actualLayerPeriod will be 32ms, because it is the
170 // smallest multiple of the display period which is >= layerPeriod.
Ady Abraham62a0be22020-12-08 16:54:10 -0800171 auto actualLayerPeriod = displayPeriod;
172 int multiplier = 1;
173 while (layerPeriod > actualLayerPeriod + MARGIN_FOR_PERIOD_CALCULATION) {
174 multiplier++;
175 actualLayerPeriod = displayPeriod * multiplier;
176 }
Marin Shalamanov15a0fc62021-08-16 18:20:21 +0200177
178 // Because of the threshold we used above it's possible that score is slightly
179 // above 1.
Ady Abraham62a0be22020-12-08 16:54:10 -0800180 return std::min(1.0f,
181 static_cast<float>(layerPeriod) / static_cast<float>(actualLayerPeriod));
182 }
183
184 if (layer.vote == LayerVoteType::ExplicitExactOrMultiple ||
185 layer.vote == LayerVoteType::Heuristic) {
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800186 if (isFractionalPairOrMultiple(refreshRate, layer.desiredRefreshRate)) {
Ady Abraham05243be2021-09-16 15:58:52 -0700187 return kScoreForFractionalPairs;
Marin Shalamanov15a0fc62021-08-16 18:20:21 +0200188 }
189
Ady Abraham62a0be22020-12-08 16:54:10 -0800190 // Calculate how many display vsyncs we need to present a single frame for this
191 // layer
192 const auto [displayFramesQuotient, displayFramesRemainder] =
193 getDisplayFrames(layerPeriod, displayPeriod);
194 static constexpr size_t MAX_FRAMES_TO_FIT = 10; // Stop calculating when score < 0.1
195 if (displayFramesRemainder == 0) {
196 // Layer desired refresh rate matches the display rate.
Ady Abraham05243be2021-09-16 15:58:52 -0700197 return 1.0f;
Ady Abraham62a0be22020-12-08 16:54:10 -0800198 }
199
200 if (displayFramesQuotient == 0) {
201 // Layer desired refresh rate is higher than the display rate.
202 return (static_cast<float>(layerPeriod) / static_cast<float>(displayPeriod)) *
203 (1.0f / (MAX_FRAMES_TO_FIT + 1));
204 }
205
206 // Layer desired refresh rate is lower than the display rate. Check how well it fits
207 // the cadence.
208 auto diff = std::abs(displayFramesRemainder - (displayPeriod - displayFramesRemainder));
209 int iter = 2;
210 while (diff > MARGIN_FOR_PERIOD_CALCULATION && iter < MAX_FRAMES_TO_FIT) {
211 diff = diff - (displayPeriod - diff);
212 iter++;
213 }
214
Ady Abraham05243be2021-09-16 15:58:52 -0700215 return (1.0f / iter);
216 }
217
218 return 0;
219}
220
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800221float RefreshRateConfigs::calculateLayerScoreLocked(const LayerRequirement& layer, Fps refreshRate,
Ady Abraham05243be2021-09-16 15:58:52 -0700222 bool isSeamlessSwitch) const {
Ady Abraham05243be2021-09-16 15:58:52 -0700223 // Slightly prefer seamless switches.
224 constexpr float kSeamedSwitchPenalty = 0.95f;
225 const float seamlessness = isSeamlessSwitch ? 1.0f : kSeamedSwitchPenalty;
226
227 // If the layer wants Max, give higher score to the higher refresh rate
228 if (layer.vote == LayerVoteType::Max) {
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800229 const auto& maxRefreshRate = mAppRequestRefreshRates.back()->second;
230 const auto ratio = refreshRate.getValue() / maxRefreshRate->getFps().getValue();
Ady Abraham05243be2021-09-16 15:58:52 -0700231 // use ratio^2 to get a lower score the more we get further from peak
232 return ratio * ratio;
Ady Abraham62a0be22020-12-08 16:54:10 -0800233 }
234
Ady Abrahamdd5bfa92021-01-07 17:56:08 -0800235 if (layer.vote == LayerVoteType::ExplicitExact) {
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800236 const int divisor = getFrameRateDivisor(refreshRate, layer.desiredRefreshRate);
Andy Yu2ae6b6b2021-11-18 14:51:06 -0800237 if (mSupportsFrameRateOverrideByContent) {
Ady Abrahamdd5bfa92021-01-07 17:56:08 -0800238 // Since we support frame rate override, allow refresh rates which are
239 // multiples of the layer's request, as those apps would be throttled
240 // down to run at the desired refresh rate.
Ady Abrahamcc315492022-02-17 17:06:39 -0800241 return divisor > 0;
Ady Abrahamdd5bfa92021-01-07 17:56:08 -0800242 }
243
Ady Abrahamcc315492022-02-17 17:06:39 -0800244 return divisor == 1;
Ady Abrahamdd5bfa92021-01-07 17:56:08 -0800245 }
246
Ady Abrahamcc315492022-02-17 17:06:39 -0800247 // If the layer frame rate is a divisor of the refresh rate it should score
Ady Abraham05243be2021-09-16 15:58:52 -0700248 // the highest score.
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800249 if (getFrameRateDivisor(refreshRate, layer.desiredRefreshRate) > 0) {
Ady Abraham05243be2021-09-16 15:58:52 -0700250 return 1.0f * seamlessness;
251 }
252
Ady Abrahamcc315492022-02-17 17:06:39 -0800253 // The layer frame rate is not a divisor of the refresh rate,
Ady Abraham05243be2021-09-16 15:58:52 -0700254 // there is a small penalty attached to the score to favor the frame rates
255 // the exactly matches the display refresh rate or a multiple.
Ady Abraham1c595502022-01-13 21:58:32 -0800256 constexpr float kNonExactMatchingPenalty = 0.95f;
Ady Abraham05243be2021-09-16 15:58:52 -0700257 return calculateNonExactMatchingLayerScoreLocked(layer, refreshRate) * seamlessness *
258 kNonExactMatchingPenalty;
Ady Abraham62a0be22020-12-08 16:54:10 -0800259}
260
Dominik Laskowskia8626ec2021-12-15 18:13:30 -0800261auto RefreshRateConfigs::getBestRefreshRate(const std::vector<LayerRequirement>& layers,
262 GlobalSignals signals) const
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800263 -> std::pair<DisplayModePtr, GlobalSignals> {
Marin Shalamanov4c7831e2021-06-08 20:44:06 +0200264 std::lock_guard lock(mLock);
265
Dominik Laskowskia8626ec2021-12-15 18:13:30 -0800266 if (mGetBestRefreshRateCache &&
267 mGetBestRefreshRateCache->arguments == std::make_pair(layers, signals)) {
268 return mGetBestRefreshRateCache->result;
Marin Shalamanov4c7831e2021-06-08 20:44:06 +0200269 }
270
Dominik Laskowskia8626ec2021-12-15 18:13:30 -0800271 const auto result = getBestRefreshRateLocked(layers, signals);
272 mGetBestRefreshRateCache = GetBestRefreshRateCache{{layers, signals}, result};
Marin Shalamanov4c7831e2021-06-08 20:44:06 +0200273 return result;
274}
275
Dominik Laskowskia8626ec2021-12-15 18:13:30 -0800276auto RefreshRateConfigs::getBestRefreshRateLocked(const std::vector<LayerRequirement>& layers,
277 GlobalSignals signals) const
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800278 -> std::pair<DisplayModePtr, GlobalSignals> {
Ady Abrahamae2e3c72022-08-13 05:12:13 +0000279 using namespace fps_approx_ops;
Ady Abraham8a82ba62020-01-17 12:43:17 -0800280 ATRACE_CALL();
Dominik Laskowskia8626ec2021-12-15 18:13:30 -0800281 ALOGV("%s: %zu layers", __func__, layers.size());
Ady Abrahamdfd62162020-06-10 16:11:56 -0700282
ramindani38c84982022-08-29 18:02:57 +0000283 const auto& activeMode = *getActiveModeItLocked()->second;
284
285 // Keep the display at max refresh rate for the duration of powering on the display.
286 if (signals.powerOnImminent) {
287 ALOGV("Power On Imminent");
288 const auto& max = getMaxRefreshRateByPolicyLocked(activeMode.getGroup());
289 return {max, GlobalSignals{.powerOnImminent = true}};
290 }
291
Ady Abraham8a82ba62020-01-17 12:43:17 -0800292 int noVoteLayers = 0;
293 int minVoteLayers = 0;
294 int maxVoteLayers = 0;
Ady Abraham71c437d2020-01-31 15:56:57 -0800295 int explicitDefaultVoteLayers = 0;
296 int explicitExactOrMultipleVoteLayers = 0;
Ady Abrahamdd5bfa92021-01-07 17:56:08 -0800297 int explicitExact = 0;
Ady Abraham6fb599b2020-03-05 13:48:22 -0800298 float maxExplicitWeight = 0;
Marin Shalamanovae0b5352021-03-24 12:56:08 +0100299 int seamedFocusedLayers = 0;
Dominik Laskowskia8626ec2021-12-15 18:13:30 -0800300
Ady Abraham8a82ba62020-01-17 12:43:17 -0800301 for (const auto& layer : layers) {
Ady Abrahamdd5bfa92021-01-07 17:56:08 -0800302 switch (layer.vote) {
303 case LayerVoteType::NoVote:
304 noVoteLayers++;
305 break;
306 case LayerVoteType::Min:
307 minVoteLayers++;
308 break;
309 case LayerVoteType::Max:
310 maxVoteLayers++;
311 break;
312 case LayerVoteType::ExplicitDefault:
313 explicitDefaultVoteLayers++;
314 maxExplicitWeight = std::max(maxExplicitWeight, layer.weight);
315 break;
316 case LayerVoteType::ExplicitExactOrMultiple:
317 explicitExactOrMultipleVoteLayers++;
318 maxExplicitWeight = std::max(maxExplicitWeight, layer.weight);
319 break;
320 case LayerVoteType::ExplicitExact:
321 explicitExact++;
322 maxExplicitWeight = std::max(maxExplicitWeight, layer.weight);
323 break;
324 case LayerVoteType::Heuristic:
325 break;
Ady Abraham6fb599b2020-03-05 13:48:22 -0800326 }
Marin Shalamanov46084422020-10-13 12:33:42 +0200327
Marin Shalamanovae0b5352021-03-24 12:56:08 +0100328 if (layer.seamlessness == Seamlessness::SeamedAndSeamless && layer.focused) {
329 seamedFocusedLayers++;
Marin Shalamanov46084422020-10-13 12:33:42 +0200330 }
Ady Abraham6fb599b2020-03-05 13:48:22 -0800331 }
332
Ady Abrahamdd5bfa92021-01-07 17:56:08 -0800333 const bool hasExplicitVoteLayers = explicitDefaultVoteLayers > 0 ||
334 explicitExactOrMultipleVoteLayers > 0 || explicitExact > 0;
Alec Mouri11232a22020-05-14 18:06:25 -0700335
Marin Shalamanov8cd8a992021-09-14 23:22:49 +0200336 const Policy* policy = getCurrentPolicyLocked();
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800337 const auto& defaultMode = mDisplayModes.get(policy->defaultMode)->get();
Dominik Laskowskif8734e02022-08-26 09:06:59 -0700338
Marin Shalamanov8cd8a992021-09-14 23:22:49 +0200339 // If the default mode group is different from the group of current mode,
340 // this means a layer requesting a seamed mode switch just disappeared and
341 // we should switch back to the default group.
342 // However if a seamed layer is still present we anchor around the group
343 // of the current mode, in order to prevent unnecessary seamed mode switches
344 // (e.g. when pausing a video playback).
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800345 const auto anchorGroup =
Dominik Laskowskif8734e02022-08-26 09:06:59 -0700346 seamedFocusedLayers > 0 ? activeMode.getGroup() : defaultMode->getGroup();
Marin Shalamanov8cd8a992021-09-14 23:22:49 +0200347
Steven Thomasf734df42020-04-13 21:09:28 -0700348 // Consider the touch event if there are no Explicit* layers. Otherwise wait until after we've
349 // selected a refresh rate to see if we should apply touch boost.
Dominik Laskowskia8626ec2021-12-15 18:13:30 -0800350 if (signals.touch && !hasExplicitVoteLayers) {
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800351 const DisplayModePtr& max = getMaxRefreshRateByPolicyLocked(anchorGroup);
352 ALOGV("TouchBoost - choose %s", to_string(max->getFps()).c_str());
353 return {max, GlobalSignals{.touch = true}};
Ady Abraham8a82ba62020-01-17 12:43:17 -0800354 }
355
Alec Mouri11232a22020-05-14 18:06:25 -0700356 // If the primary range consists of a single refresh rate then we can only
357 // move out the of range if layers explicitly request a different refresh
358 // rate.
Marin Shalamanove8a663d2020-11-24 17:48:00 +0100359 const bool primaryRangeIsSingleRate =
Dominik Laskowski6eab42d2021-09-13 14:34:13 -0700360 isApproxEqual(policy->primaryRange.min, policy->primaryRange.max);
Alec Mouri11232a22020-05-14 18:06:25 -0700361
Dominik Laskowskia8626ec2021-12-15 18:13:30 -0800362 if (!signals.touch && signals.idle && !(primaryRangeIsSingleRate && hasExplicitVoteLayers)) {
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800363 const DisplayModePtr& min = getMinRefreshRateByPolicyLocked();
364 ALOGV("Idle - choose %s", to_string(min->getFps()).c_str());
365 return {min, GlobalSignals{.idle = true}};
Steven Thomasbb374322020-04-28 22:47:16 -0700366 }
367
Steven Thomasdebafed2020-05-18 17:30:35 -0700368 if (layers.empty() || noVoteLayers == layers.size()) {
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800369 const DisplayModePtr& max = getMaxRefreshRateByPolicyLocked(anchorGroup);
370 ALOGV("no layers with votes - choose %s", to_string(max->getFps()).c_str());
371 return {max, kNoSignals};
Steven Thomasbb374322020-04-28 22:47:16 -0700372 }
373
Ady Abraham8a82ba62020-01-17 12:43:17 -0800374 // Only if all layers want Min we should return Min
375 if (noVoteLayers + minVoteLayers == layers.size()) {
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800376 const DisplayModePtr& min = getMinRefreshRateByPolicyLocked();
377 ALOGV("all layers Min - choose %s", to_string(min->getFps()).c_str());
378 return {min, kNoSignals};
Ady Abraham8a82ba62020-01-17 12:43:17 -0800379 }
380
Ady Abraham8a82ba62020-01-17 12:43:17 -0800381 // Find the best refresh rate based on score
Ady Abraham62a0be22020-12-08 16:54:10 -0800382 std::vector<RefreshRateScore> scores;
Steven Thomasf734df42020-04-13 21:09:28 -0700383 scores.reserve(mAppRequestRefreshRates.size());
Ady Abraham8a82ba62020-01-17 12:43:17 -0800384
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800385 for (const DisplayModeIterator modeIt : mAppRequestRefreshRates) {
386 scores.emplace_back(RefreshRateScore{modeIt, 0.0f});
Ady Abraham8a82ba62020-01-17 12:43:17 -0800387 }
388
389 for (const auto& layer : layers) {
rnlee3bd610662021-06-23 16:27:57 -0700390 ALOGV("Calculating score for %s (%s, weight %.2f, desired %.2f) ", layer.name.c_str(),
Dominik Laskowskif5d0ea52021-09-26 17:27:01 -0700391 ftl::enum_string(layer.vote).c_str(), layer.weight,
rnlee3bd610662021-06-23 16:27:57 -0700392 layer.desiredRefreshRate.getValue());
Ady Abraham4ccdcb42020-02-11 17:34:34 -0800393 if (layer.vote == LayerVoteType::NoVote || layer.vote == LayerVoteType::Min) {
Ady Abraham8a82ba62020-01-17 12:43:17 -0800394 continue;
395 }
396
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800397 const auto weight = layer.weight;
Ady Abraham71c437d2020-01-31 15:56:57 -0800398
Ady Abraham62f51d92022-08-24 22:20:22 +0000399 for (auto& [modeIt, overallScore, fixedRateBelowThresholdLayersScore] : scores) {
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800400 const auto& [id, mode] = *modeIt;
Dominik Laskowskif8734e02022-08-26 09:06:59 -0700401 const bool isSeamlessSwitch = mode->getGroup() == activeMode.getGroup();
Marin Shalamanov46084422020-10-13 12:33:42 +0200402
Marin Shalamanov53fc11d2020-11-20 14:00:13 +0100403 if (layer.seamlessness == Seamlessness::OnlySeamless && !isSeamlessSwitch) {
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100404 ALOGV("%s ignores %s to avoid non-seamless switch. Current mode = %s",
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800405 formatLayerInfo(layer, weight).c_str(), to_string(*mode).c_str(),
Dominik Laskowskif8734e02022-08-26 09:06:59 -0700406 to_string(activeMode).c_str());
Marin Shalamanov46084422020-10-13 12:33:42 +0200407 continue;
408 }
409
Marin Shalamanov53fc11d2020-11-20 14:00:13 +0100410 if (layer.seamlessness == Seamlessness::SeamedAndSeamless && !isSeamlessSwitch &&
411 !layer.focused) {
412 ALOGV("%s ignores %s because it's not focused and the switch is going to be seamed."
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100413 " Current mode = %s",
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800414 formatLayerInfo(layer, weight).c_str(), to_string(*mode).c_str(),
Dominik Laskowskif8734e02022-08-26 09:06:59 -0700415 to_string(activeMode).c_str());
Marin Shalamanov53fc11d2020-11-20 14:00:13 +0100416 continue;
417 }
418
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100419 // Layers with default seamlessness vote for the current mode group if
Marin Shalamanov53fc11d2020-11-20 14:00:13 +0100420 // there are layers with seamlessness=SeamedAndSeamless and for the default
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100421 // mode group otherwise. In second case, if the current mode group is different
Marin Shalamanov53fc11d2020-11-20 14:00:13 +0100422 // from the default, this means a layer with seamlessness=SeamedAndSeamless has just
423 // disappeared.
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800424 const bool isInPolicyForDefault = mode->getGroup() == anchorGroup;
Marin Shalamanovae0b5352021-03-24 12:56:08 +0100425 if (layer.seamlessness == Seamlessness::Default && !isInPolicyForDefault) {
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100426 ALOGV("%s ignores %s. Current mode = %s", formatLayerInfo(layer, weight).c_str(),
Dominik Laskowskif8734e02022-08-26 09:06:59 -0700427 to_string(*mode).c_str(), to_string(activeMode).c_str());
Marin Shalamanov46084422020-10-13 12:33:42 +0200428 continue;
429 }
430
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800431 const bool inPrimaryRange = policy->primaryRange.includes(mode->getFps());
Alec Mouri11232a22020-05-14 18:06:25 -0700432 if ((primaryRangeIsSingleRate || !inPrimaryRange) &&
Ady Abrahamdd5bfa92021-01-07 17:56:08 -0800433 !(layer.focused &&
434 (layer.vote == LayerVoteType::ExplicitDefault ||
435 layer.vote == LayerVoteType::ExplicitExact))) {
Ady Abraham20c029c2020-07-06 12:58:05 -0700436 // Only focused layers with ExplicitDefault frame rate settings are allowed to score
Ady Abrahamaae5ed52020-06-26 09:32:43 -0700437 // refresh rates outside the primary range.
Steven Thomasf734df42020-04-13 21:09:28 -0700438 continue;
439 }
440
Ady Abrahamae2e3c72022-08-13 05:12:13 +0000441 const float layerScore =
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800442 calculateLayerScoreLocked(layer, mode->getFps(), isSeamlessSwitch);
Ady Abrahamae2e3c72022-08-13 05:12:13 +0000443 const float weightedLayerScore = weight * layerScore;
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800444
Ady Abraham13cfb362022-08-13 05:12:13 +0000445 // Layer with fixed source has a special consideration which depends on the
Ady Abrahamae2e3c72022-08-13 05:12:13 +0000446 // mConfig.frameRateMultipleThreshold. We don't want these layers to score
447 // refresh rates above the threshold, but we also don't want to favor the lower
448 // ones by having a greater number of layers scoring them. Instead, we calculate
449 // the score independently for these layers and later decide which
450 // refresh rates to add it. For example, desired 24 fps with 120 Hz threshold should not
451 // score 120 Hz, but desired 60 fps should contribute to the score.
452 const bool fixedSourceLayer = [](LayerVoteType vote) {
453 switch (vote) {
454 case LayerVoteType::ExplicitExactOrMultiple:
455 case LayerVoteType::Heuristic:
456 return true;
457 case LayerVoteType::NoVote:
458 case LayerVoteType::Min:
459 case LayerVoteType::Max:
460 case LayerVoteType::ExplicitDefault:
461 case LayerVoteType::ExplicitExact:
462 return false;
463 }
464 }(layer.vote);
Ady Abraham62f51d92022-08-24 22:20:22 +0000465 const bool layerBelowThreshold = mConfig.frameRateMultipleThreshold != 0 &&
Ady Abrahamae2e3c72022-08-13 05:12:13 +0000466 layer.desiredRefreshRate <
467 Fps::fromValue(mConfig.frameRateMultipleThreshold / 2);
Ady Abraham62f51d92022-08-24 22:20:22 +0000468 if (fixedSourceLayer && layerBelowThreshold) {
Ady Abraham13cfb362022-08-13 05:12:13 +0000469 const bool modeAboveThreshold =
470 mode->getFps() >= Fps::fromValue(mConfig.frameRateMultipleThreshold);
Ady Abraham62f51d92022-08-24 22:20:22 +0000471 if (modeAboveThreshold) {
Ady Abrahamae2e3c72022-08-13 05:12:13 +0000472 ALOGV("%s gives %s fixed source (above threshold) score of %.4f",
473 formatLayerInfo(layer, weight).c_str(), to_string(mode->getFps()).c_str(),
474 layerScore);
Ady Abraham62f51d92022-08-24 22:20:22 +0000475 fixedRateBelowThresholdLayersScore.modeAboveThreshold += weightedLayerScore;
Ady Abrahamae2e3c72022-08-13 05:12:13 +0000476 } else {
477 ALOGV("%s gives %s fixed source (below threshold) score of %.4f",
478 formatLayerInfo(layer, weight).c_str(), to_string(mode->getFps()).c_str(),
479 layerScore);
Ady Abraham62f51d92022-08-24 22:20:22 +0000480 fixedRateBelowThresholdLayersScore.modeBelowThreshold += weightedLayerScore;
Ady Abrahamae2e3c72022-08-13 05:12:13 +0000481 }
482 } else {
483 ALOGV("%s gives %s score of %.4f", formatLayerInfo(layer, weight).c_str(),
484 to_string(mode->getFps()).c_str(), layerScore);
485 overallScore += weightedLayerScore;
486 }
Ady Abraham8a82ba62020-01-17 12:43:17 -0800487 }
488 }
489
Ady Abrahamae2e3c72022-08-13 05:12:13 +0000490 // We want to find the best refresh rate without the fixed source layers,
Ady Abraham62f51d92022-08-24 22:20:22 +0000491 // so we could know whether we should add the modeAboveThreshold scores or not.
Ady Abrahamae2e3c72022-08-13 05:12:13 +0000492 // If the best refresh rate is already above the threshold, it means that
493 // some non-fixed source layers already scored it, so we can just add the score
494 // for all fixed source layers, even the ones that are above the threshold.
495 const bool maxScoreAboveThreshold = [&] {
496 if (mConfig.frameRateMultipleThreshold == 0 || scores.empty()) {
497 return false;
498 }
499
500 const auto maxScoreIt =
501 std::max_element(scores.begin(), scores.end(),
502 [](RefreshRateScore max, RefreshRateScore current) {
503 const auto& [modeIt, overallScore, _] = current;
504 return overallScore > max.overallScore;
505 });
506 ALOGV("%s is the best refresh rate without fixed source layers. It is %s the threshold for "
507 "refresh rate multiples",
508 to_string(maxScoreIt->modeIt->second->getFps()).c_str(),
509 maxScoreAboveThreshold ? "above" : "below");
510 return maxScoreIt->modeIt->second->getFps() >=
511 Fps::fromValue(mConfig.frameRateMultipleThreshold);
512 }();
513
514 // Now we can add the fixed rate layers score
Ady Abraham62f51d92022-08-24 22:20:22 +0000515 for (auto& [modeIt, overallScore, fixedRateBelowThresholdLayersScore] : scores) {
516 overallScore += fixedRateBelowThresholdLayersScore.modeBelowThreshold;
Ady Abrahamae2e3c72022-08-13 05:12:13 +0000517 if (maxScoreAboveThreshold) {
Ady Abraham62f51d92022-08-24 22:20:22 +0000518 overallScore += fixedRateBelowThresholdLayersScore.modeAboveThreshold;
Ady Abrahamae2e3c72022-08-13 05:12:13 +0000519 }
520 ALOGV("%s adjusted overallScore is %.4f", to_string(modeIt->second->getFps()).c_str(),
521 overallScore);
522 }
523
524 // Now that we scored all the refresh rates we need to pick the one that got the highest
525 // overallScore. In case of a tie we will pick the higher refresh rate if any of the layers
526 // wanted Max, or the lower otherwise.
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800527 const DisplayModePtr& bestRefreshRate = maxVoteLayers > 0
528 ? getMaxScoreRefreshRate(scores.rbegin(), scores.rend())
529 : getMaxScoreRefreshRate(scores.begin(), scores.end());
Ady Abraham34702102020-02-10 14:12:05 -0800530
Alec Mouri11232a22020-05-14 18:06:25 -0700531 if (primaryRangeIsSingleRate) {
532 // If we never scored any layers, then choose the rate from the primary
533 // range instead of picking a random score from the app range.
534 if (std::all_of(scores.begin(), scores.end(),
Ady Abrahamae2e3c72022-08-13 05:12:13 +0000535 [](RefreshRateScore score) { return score.overallScore == 0; })) {
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800536 const DisplayModePtr& max = getMaxRefreshRateByPolicyLocked(anchorGroup);
537 ALOGV("layers not scored - choose %s", to_string(max->getFps()).c_str());
538 return {max, kNoSignals};
Alec Mouri11232a22020-05-14 18:06:25 -0700539 } else {
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800540 return {bestRefreshRate, kNoSignals};
Alec Mouri11232a22020-05-14 18:06:25 -0700541 }
542 }
543
Steven Thomasf734df42020-04-13 21:09:28 -0700544 // Consider the touch event if there are no ExplicitDefault layers. ExplicitDefault are mostly
545 // interactive (as opposed to ExplicitExactOrMultiple) and therefore if those posted an explicit
546 // vote we should not change it if we get a touch event. Only apply touch boost if it will
547 // actually increase the refresh rate over the normal selection.
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800548 const DisplayModePtr& touchRefreshRate = getMaxRefreshRateByPolicyLocked(anchorGroup);
Alec Mouri11232a22020-05-14 18:06:25 -0700549
Ady Abraham5e4e9832021-06-14 13:40:56 -0700550 const bool touchBoostForExplicitExact = [&] {
Andy Yu2ae6b6b2021-11-18 14:51:06 -0800551 if (mSupportsFrameRateOverrideByContent) {
Ady Abraham5e4e9832021-06-14 13:40:56 -0700552 // Enable touch boost if there are other layers besides exact
553 return explicitExact + noVoteLayers != layers.size();
554 } else {
555 // Enable touch boost if there are no exact layers
556 return explicitExact == 0;
557 }
558 }();
Dominik Laskowski6eab42d2021-09-13 14:34:13 -0700559
560 using fps_approx_ops::operator<;
561
Dominik Laskowskia8626ec2021-12-15 18:13:30 -0800562 if (signals.touch && explicitDefaultVoteLayers == 0 && touchBoostForExplicitExact &&
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800563 bestRefreshRate->getFps() < touchRefreshRate->getFps()) {
564 ALOGV("TouchBoost - choose %s", to_string(touchRefreshRate->getFps()).c_str());
Dominik Laskowskia8626ec2021-12-15 18:13:30 -0800565 return {touchRefreshRate, GlobalSignals{.touch = true}};
Steven Thomasf734df42020-04-13 21:09:28 -0700566 }
567
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800568 return {bestRefreshRate, kNoSignals};
Ady Abraham34702102020-02-10 14:12:05 -0800569}
570
Ady Abraham62a0be22020-12-08 16:54:10 -0800571std::unordered_map<uid_t, std::vector<const RefreshRateConfigs::LayerRequirement*>>
572groupLayersByUid(const std::vector<RefreshRateConfigs::LayerRequirement>& layers) {
573 std::unordered_map<uid_t, std::vector<const RefreshRateConfigs::LayerRequirement*>> layersByUid;
574 for (const auto& layer : layers) {
575 auto iter = layersByUid.emplace(layer.ownerUid,
576 std::vector<const RefreshRateConfigs::LayerRequirement*>());
577 auto& layersWithSameUid = iter.first->second;
578 layersWithSameUid.push_back(&layer);
579 }
580
581 // Remove uids that can't have a frame rate override
582 for (auto iter = layersByUid.begin(); iter != layersByUid.end();) {
583 const auto& layersWithSameUid = iter->second;
584 bool skipUid = false;
585 for (const auto& layer : layersWithSameUid) {
586 if (layer->vote == RefreshRateConfigs::LayerVoteType::Max ||
587 layer->vote == RefreshRateConfigs::LayerVoteType::Heuristic) {
588 skipUid = true;
589 break;
590 }
591 }
592 if (skipUid) {
593 iter = layersByUid.erase(iter);
594 } else {
595 ++iter;
596 }
597 }
598
599 return layersByUid;
600}
601
Ady Abraham62a0be22020-12-08 16:54:10 -0800602RefreshRateConfigs::UidToFrameRateOverride RefreshRateConfigs::getFrameRateOverrides(
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800603 const std::vector<LayerRequirement>& layers, Fps displayRefreshRate,
Dominik Laskowski6eab42d2021-09-13 14:34:13 -0700604 GlobalSignals globalSignals) const {
Ady Abraham62a0be22020-12-08 16:54:10 -0800605 ATRACE_CALL();
Ady Abraham62a0be22020-12-08 16:54:10 -0800606
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800607 ALOGV("%s: %zu layers", __func__, layers.size());
608
Ady Abraham62a0be22020-12-08 16:54:10 -0800609 std::lock_guard lock(mLock);
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800610
611 std::vector<RefreshRateScore> scores;
612 scores.reserve(mDisplayModes.size());
613
614 for (auto it = mDisplayModes.begin(); it != mDisplayModes.end(); ++it) {
615 scores.emplace_back(RefreshRateScore{it, 0.0f});
616 }
617
618 std::sort(scores.begin(), scores.end(), [](const auto& lhs, const auto& rhs) {
619 const auto& mode1 = lhs.modeIt->second;
620 const auto& mode2 = rhs.modeIt->second;
621 return isStrictlyLess(mode1->getFps(), mode2->getFps());
622 });
623
Ady Abraham62a0be22020-12-08 16:54:10 -0800624 std::unordered_map<uid_t, std::vector<const LayerRequirement*>> layersByUid =
625 groupLayersByUid(layers);
626 UidToFrameRateOverride frameRateOverrides;
627 for (const auto& [uid, layersWithSameUid] : layersByUid) {
Ady Abrahamdd5bfa92021-01-07 17:56:08 -0800628 // Layers with ExplicitExactOrMultiple expect touch boost
629 const bool hasExplicitExactOrMultiple =
630 std::any_of(layersWithSameUid.cbegin(), layersWithSameUid.cend(),
631 [](const auto& layer) {
632 return layer->vote == LayerVoteType::ExplicitExactOrMultiple;
633 });
634
Dominik Laskowski6eab42d2021-09-13 14:34:13 -0700635 if (globalSignals.touch && hasExplicitExactOrMultiple) {
Ady Abrahamdd5bfa92021-01-07 17:56:08 -0800636 continue;
637 }
638
Ady Abrahamae2e3c72022-08-13 05:12:13 +0000639 for (auto& [_, score, _1] : scores) {
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800640 score = 0;
Ady Abraham62a0be22020-12-08 16:54:10 -0800641 }
642
643 for (const auto& layer : layersWithSameUid) {
644 if (layer->vote == LayerVoteType::NoVote || layer->vote == LayerVoteType::Min) {
645 continue;
646 }
647
648 LOG_ALWAYS_FATAL_IF(layer->vote != LayerVoteType::ExplicitDefault &&
Ady Abrahamdd5bfa92021-01-07 17:56:08 -0800649 layer->vote != LayerVoteType::ExplicitExactOrMultiple &&
650 layer->vote != LayerVoteType::ExplicitExact);
Ady Abrahamae2e3c72022-08-13 05:12:13 +0000651 for (auto& [modeIt, score, _] : scores) {
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800652 constexpr bool isSeamlessSwitch = true;
653 const auto layerScore = calculateLayerScoreLocked(*layer, modeIt->second->getFps(),
654 isSeamlessSwitch);
655 score += layer->weight * layerScore;
Ady Abraham62a0be22020-12-08 16:54:10 -0800656 }
657 }
658
Ady Abrahamcc315492022-02-17 17:06:39 -0800659 // We just care about the refresh rates which are a divisor of the
Ady Abraham62a0be22020-12-08 16:54:10 -0800660 // display refresh rate
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800661 const auto it = std::remove_if(scores.begin(), scores.end(), [&](RefreshRateScore score) {
662 const auto& [id, mode] = *score.modeIt;
663 return getFrameRateDivisor(displayRefreshRate, mode->getFps()) == 0;
664 });
665 scores.erase(it, scores.end());
Ady Abraham62a0be22020-12-08 16:54:10 -0800666
667 // If we never scored any layers, we don't have a preferred frame rate
668 if (std::all_of(scores.begin(), scores.end(),
Ady Abrahamae2e3c72022-08-13 05:12:13 +0000669 [](RefreshRateScore score) { return score.overallScore == 0; })) {
Ady Abraham62a0be22020-12-08 16:54:10 -0800670 continue;
671 }
672
673 // Now that we scored all the refresh rates we need to pick the one that got the highest
674 // score.
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800675 const DisplayModePtr& bestRefreshRate =
676 getMaxScoreRefreshRate(scores.begin(), scores.end());
677
Ady Abraham5cc2e262021-03-25 13:09:17 -0700678 frameRateOverrides.emplace(uid, bestRefreshRate->getFps());
Ady Abraham62a0be22020-12-08 16:54:10 -0800679 }
680
681 return frameRateOverrides;
682}
683
Marin Shalamanoveadf2e72020-12-10 15:35:28 +0100684std::optional<Fps> RefreshRateConfigs::onKernelTimerChanged(
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800685 std::optional<DisplayModeId> desiredActiveModeId, bool timerExpired) const {
Ady Abraham2139f732019-11-13 18:56:40 -0800686 std::lock_guard lock(mLock);
Marin Shalamanoveadf2e72020-12-10 15:35:28 +0100687
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800688 const DisplayModePtr& current = desiredActiveModeId
689 ? mDisplayModes.get(*desiredActiveModeId)->get()
Dominik Laskowskif8734e02022-08-26 09:06:59 -0700690 : getActiveModeItLocked()->second;
Marin Shalamanoveadf2e72020-12-10 15:35:28 +0100691
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800692 const DisplayModePtr& min = mMinRefreshRateModeIt->second;
693 if (current == min) {
694 return {};
Marin Shalamanoveadf2e72020-12-10 15:35:28 +0100695 }
696
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800697 const auto& mode = timerExpired ? min : current;
698 return mode->getFps();
Steven Thomasf734df42020-04-13 21:09:28 -0700699}
700
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800701const DisplayModePtr& RefreshRateConfigs::getMinRefreshRateByPolicyLocked() const {
Dominik Laskowskif8734e02022-08-26 09:06:59 -0700702 const auto& activeMode = *getActiveModeItLocked()->second;
703
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800704 for (const DisplayModeIterator modeIt : mPrimaryRefreshRates) {
705 const auto& mode = modeIt->second;
Dominik Laskowskif8734e02022-08-26 09:06:59 -0700706 if (activeMode.getGroup() == mode->getGroup()) {
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800707 return mode;
Marin Shalamanov46084422020-10-13 12:33:42 +0200708 }
709 }
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800710
Dominik Laskowskif8734e02022-08-26 09:06:59 -0700711 ALOGE("Can't find min refresh rate by policy with the same mode group as the current mode %s",
712 to_string(activeMode).c_str());
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800713
714 // Default to the lowest refresh rate.
715 return mPrimaryRefreshRates.front()->second;
Ady Abraham2139f732019-11-13 18:56:40 -0800716}
717
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800718DisplayModePtr RefreshRateConfigs::getMaxRefreshRateByPolicy() const {
Ady Abraham2139f732019-11-13 18:56:40 -0800719 std::lock_guard lock(mLock);
Steven Thomasf734df42020-04-13 21:09:28 -0700720 return getMaxRefreshRateByPolicyLocked();
721}
722
Dominik Laskowskif8734e02022-08-26 09:06:59 -0700723const DisplayModePtr& RefreshRateConfigs::getMaxRefreshRateByPolicyLocked() const {
724 const int anchorGroup = getActiveModeItLocked()->second->getGroup();
725 return getMaxRefreshRateByPolicyLocked(anchorGroup);
726}
727
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800728const DisplayModePtr& RefreshRateConfigs::getMaxRefreshRateByPolicyLocked(int anchorGroup) const {
729 for (auto it = mPrimaryRefreshRates.rbegin(); it != mPrimaryRefreshRates.rend(); ++it) {
730 const auto& mode = (*it)->second;
731 if (anchorGroup == mode->getGroup()) {
732 return mode;
Marin Shalamanov46084422020-10-13 12:33:42 +0200733 }
734 }
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800735
Dominik Laskowskif8734e02022-08-26 09:06:59 -0700736 const auto& activeMode = *getActiveModeItLocked()->second;
737 ALOGE("Can't find max refresh rate by policy with the same mode group as the current mode %s",
738 to_string(activeMode).c_str());
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800739
740 // Default to the highest refresh rate.
741 return mPrimaryRefreshRates.back()->second;
Ady Abraham2139f732019-11-13 18:56:40 -0800742}
743
Dominik Laskowskif8734e02022-08-26 09:06:59 -0700744DisplayModePtr RefreshRateConfigs::getActiveModePtr() const {
Ady Abraham2139f732019-11-13 18:56:40 -0800745 std::lock_guard lock(mLock);
Dominik Laskowskif8734e02022-08-26 09:06:59 -0700746 return getActiveModeItLocked()->second;
747}
748
749const DisplayMode& RefreshRateConfigs::getActiveMode() const {
750 // Reads from kMainThreadContext do not require mLock.
751 ftl::FakeGuard guard(mLock);
752 return *mActiveModeIt->second;
753}
754
755DisplayModeIterator RefreshRateConfigs::getActiveModeItLocked() const {
756 // Reads under mLock do not require kMainThreadContext.
757 return FTL_FAKE_GUARD(kMainThreadContext, mActiveModeIt);
Ady Abraham2139f732019-11-13 18:56:40 -0800758}
759
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800760void RefreshRateConfigs::setActiveModeId(DisplayModeId modeId) {
Ady Abraham2139f732019-11-13 18:56:40 -0800761 std::lock_guard lock(mLock);
Marin Shalamanov4c7831e2021-06-08 20:44:06 +0200762
763 // Invalidate the cached invocation to getBestRefreshRate. This forces
764 // the refresh rate to be recomputed on the next call to getBestRefreshRate.
Dominik Laskowskia8626ec2021-12-15 18:13:30 -0800765 mGetBestRefreshRateCache.reset();
Marin Shalamanov4c7831e2021-06-08 20:44:06 +0200766
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800767 mActiveModeIt = mDisplayModes.find(modeId);
768 LOG_ALWAYS_FATAL_IF(mActiveModeIt == mDisplayModes.end());
Ady Abrahamb4b1e0a2019-11-20 18:25:35 -0800769}
770
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800771RefreshRateConfigs::RefreshRateConfigs(DisplayModes modes, DisplayModeId activeModeId,
rnlee3bd610662021-06-23 16:27:57 -0700772 Config config)
773 : mKnownFrameRates(constructKnownFrameRates(modes)), mConfig(config) {
Ady Abraham9a2ea342021-09-03 17:32:34 -0700774 initializeIdleTimer();
Dominik Laskowskif8734e02022-08-26 09:06:59 -0700775 FTL_FAKE_GUARD(kMainThreadContext, updateDisplayModes(std::move(modes), activeModeId));
Marin Shalamanoveadf2e72020-12-10 15:35:28 +0100776}
777
Ady Abraham9a2ea342021-09-03 17:32:34 -0700778void RefreshRateConfigs::initializeIdleTimer() {
ramindani32cf0602022-03-02 02:30:29 +0000779 if (mConfig.idleTimerTimeout > 0ms) {
Ady Abraham9a2ea342021-09-03 17:32:34 -0700780 mIdleTimer.emplace(
ramindani32cf0602022-03-02 02:30:29 +0000781 "IdleTimer", mConfig.idleTimerTimeout,
Dominik Laskowski83bd7712022-01-07 14:30:53 -0800782 [this] {
783 std::scoped_lock lock(mIdleTimerCallbacksMutex);
784 if (const auto callbacks = getIdleTimerCallbacks()) {
785 callbacks->onReset();
786 }
Ady Abraham9a2ea342021-09-03 17:32:34 -0700787 },
Dominik Laskowski83bd7712022-01-07 14:30:53 -0800788 [this] {
789 std::scoped_lock lock(mIdleTimerCallbacksMutex);
790 if (const auto callbacks = getIdleTimerCallbacks()) {
791 callbacks->onExpired();
792 }
Ady Abraham9a2ea342021-09-03 17:32:34 -0700793 });
Ady Abraham9a2ea342021-09-03 17:32:34 -0700794 }
795}
796
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800797void RefreshRateConfigs::updateDisplayModes(DisplayModes modes, DisplayModeId activeModeId) {
Marin Shalamanoveadf2e72020-12-10 15:35:28 +0100798 std::lock_guard lock(mLock);
Marin Shalamanov4c7831e2021-06-08 20:44:06 +0200799
Marin Shalamanov4c7831e2021-06-08 20:44:06 +0200800 // Invalidate the cached invocation to getBestRefreshRate. This forces
801 // the refresh rate to be recomputed on the next call to getBestRefreshRate.
Dominik Laskowskia8626ec2021-12-15 18:13:30 -0800802 mGetBestRefreshRateCache.reset();
Marin Shalamanov4c7831e2021-06-08 20:44:06 +0200803
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800804 mDisplayModes = std::move(modes);
805 mActiveModeIt = mDisplayModes.find(activeModeId);
806 LOG_ALWAYS_FATAL_IF(mActiveModeIt == mDisplayModes.end());
Ady Abrahamabc27602020-04-08 17:20:29 -0700807
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800808 const auto sortedModes =
809 sortByRefreshRate(mDisplayModes, [](const DisplayMode&) { return true; });
810 mMinRefreshRateModeIt = sortedModes.front();
811 mMaxRefreshRateModeIt = sortedModes.back();
812
Marin Shalamanov75f37252021-02-10 21:43:57 +0100813 // Reset the policy because the old one may no longer be valid.
814 mDisplayManagerPolicy = {};
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800815 mDisplayManagerPolicy.defaultMode = activeModeId;
Ady Abraham64c2fc02020-12-29 12:07:50 -0800816
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800817 mSupportsFrameRateOverrideByContent =
818 mConfig.enableFrameRateOverride && canModesSupportFrameRateOverride(sortedModes);
Ady Abraham4899ff82021-01-06 13:53:29 -0800819
Ady Abrahamabc27602020-04-08 17:20:29 -0700820 constructAvailableRefreshRates();
Ady Abrahamb4b1e0a2019-11-20 18:25:35 -0800821}
822
Marin Shalamanoveadf2e72020-12-10 15:35:28 +0100823bool RefreshRateConfigs::isPolicyValidLocked(const Policy& policy) const {
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100824 // defaultMode must be a valid mode, and within the given refresh rate range.
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800825 if (const auto mode = mDisplayModes.get(policy.defaultMode)) {
826 if (!policy.primaryRange.includes(mode->get()->getFps())) {
827 ALOGE("Default mode is not in the primary range.");
828 return false;
829 }
830 } else {
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100831 ALOGE("Default mode is not found.");
Steven Thomasd4071902020-03-24 16:02:53 -0700832 return false;
833 }
Dominik Laskowski6eab42d2021-09-13 14:34:13 -0700834
835 using namespace fps_approx_ops;
836 return policy.appRequestRange.min <= policy.primaryRange.min &&
837 policy.appRequestRange.max >= policy.primaryRange.max;
Steven Thomasd4071902020-03-24 16:02:53 -0700838}
839
840status_t RefreshRateConfigs::setDisplayManagerPolicy(const Policy& policy) {
Ady Abraham2139f732019-11-13 18:56:40 -0800841 std::lock_guard lock(mLock);
Marin Shalamanoveadf2e72020-12-10 15:35:28 +0100842 if (!isPolicyValidLocked(policy)) {
Marin Shalamanovb6674e72020-11-06 13:05:57 +0100843 ALOGE("Invalid refresh rate policy: %s", policy.toString().c_str());
Ana Kruleced3a8cc2019-11-14 00:55:07 +0100844 return BAD_VALUE;
845 }
Dominik Laskowskia8626ec2021-12-15 18:13:30 -0800846 mGetBestRefreshRateCache.reset();
Steven Thomasd4071902020-03-24 16:02:53 -0700847 Policy previousPolicy = *getCurrentPolicyLocked();
848 mDisplayManagerPolicy = policy;
849 if (*getCurrentPolicyLocked() == previousPolicy) {
850 return CURRENT_POLICY_UNCHANGED;
Ana Kruleced3a8cc2019-11-14 00:55:07 +0100851 }
Ady Abraham2139f732019-11-13 18:56:40 -0800852 constructAvailableRefreshRates();
Ana Kruleced3a8cc2019-11-14 00:55:07 +0100853 return NO_ERROR;
854}
855
Steven Thomasd4071902020-03-24 16:02:53 -0700856status_t RefreshRateConfigs::setOverridePolicy(const std::optional<Policy>& policy) {
Ana Kruleced3a8cc2019-11-14 00:55:07 +0100857 std::lock_guard lock(mLock);
Marin Shalamanoveadf2e72020-12-10 15:35:28 +0100858 if (policy && !isPolicyValidLocked(*policy)) {
Steven Thomasd4071902020-03-24 16:02:53 -0700859 return BAD_VALUE;
860 }
Dominik Laskowskia8626ec2021-12-15 18:13:30 -0800861 mGetBestRefreshRateCache.reset();
Steven Thomasd4071902020-03-24 16:02:53 -0700862 Policy previousPolicy = *getCurrentPolicyLocked();
863 mOverridePolicy = policy;
864 if (*getCurrentPolicyLocked() == previousPolicy) {
865 return CURRENT_POLICY_UNCHANGED;
866 }
867 constructAvailableRefreshRates();
868 return NO_ERROR;
869}
870
871const RefreshRateConfigs::Policy* RefreshRateConfigs::getCurrentPolicyLocked() const {
872 return mOverridePolicy ? &mOverridePolicy.value() : &mDisplayManagerPolicy;
873}
874
875RefreshRateConfigs::Policy RefreshRateConfigs::getCurrentPolicy() const {
876 std::lock_guard lock(mLock);
877 return *getCurrentPolicyLocked();
878}
879
880RefreshRateConfigs::Policy RefreshRateConfigs::getDisplayManagerPolicy() const {
881 std::lock_guard lock(mLock);
882 return mDisplayManagerPolicy;
Ana Kruleced3a8cc2019-11-14 00:55:07 +0100883}
884
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100885bool RefreshRateConfigs::isModeAllowed(DisplayModeId modeId) const {
Ana Kruleced3a8cc2019-11-14 00:55:07 +0100886 std::lock_guard lock(mLock);
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800887 return std::any_of(mAppRequestRefreshRates.begin(), mAppRequestRefreshRates.end(),
888 [modeId](DisplayModeIterator modeIt) {
889 return modeIt->second->getId() == modeId;
890 });
Ady Abraham2139f732019-11-13 18:56:40 -0800891}
892
893void RefreshRateConfigs::constructAvailableRefreshRates() {
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800894 // Filter modes based on current policy and sort on refresh rate.
Steven Thomasd4071902020-03-24 16:02:53 -0700895 const Policy* policy = getCurrentPolicyLocked();
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800896 ALOGV("%s: %s ", __func__, policy->toString().c_str());
Ady Abrahamabc27602020-04-08 17:20:29 -0700897
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800898 const auto& defaultMode = mDisplayModes.get(policy->defaultMode)->get();
Ady Abraham8a82ba62020-01-17 12:43:17 -0800899
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800900 const auto filterRefreshRates = [&](FpsRange range, const char* rangeName) REQUIRES(mLock) {
901 const auto filter = [&](const DisplayMode& mode) {
902 return mode.getResolution() == defaultMode->getResolution() &&
903 mode.getDpi() == defaultMode->getDpi() &&
904 (policy->allowGroupSwitching || mode.getGroup() == defaultMode->getGroup()) &&
905 range.includes(mode.getFps());
906 };
Ady Abraham8a82ba62020-01-17 12:43:17 -0800907
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800908 const auto modes = sortByRefreshRate(mDisplayModes, filter);
909 LOG_ALWAYS_FATAL_IF(modes.empty(), "No matching modes for %s range %s", rangeName,
910 to_string(range).c_str());
Dominik Laskowski953b7fd2022-01-08 19:34:59 -0800911
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800912 const auto stringifyModes = [&] {
913 std::string str;
914 for (const auto modeIt : modes) {
915 str += to_string(modeIt->second->getFps());
916 str.push_back(' ');
917 }
918 return str;
919 };
920 ALOGV("%s refresh rates: %s", rangeName, stringifyModes().c_str());
Steven Thomasf734df42020-04-13 21:09:28 -0700921
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800922 return modes;
923 };
924
925 mPrimaryRefreshRates = filterRefreshRates(policy->primaryRange, "primary");
926 mAppRequestRefreshRates = filterRefreshRates(policy->appRequestRange, "app request");
Ady Abraham2139f732019-11-13 18:56:40 -0800927}
928
Marin Shalamanove8a663d2020-11-24 17:48:00 +0100929Fps RefreshRateConfigs::findClosestKnownFrameRate(Fps frameRate) const {
Dominik Laskowski6eab42d2021-09-13 14:34:13 -0700930 using namespace fps_approx_ops;
931
932 if (frameRate <= mKnownFrameRates.front()) {
933 return mKnownFrameRates.front();
Ady Abrahamb1b9d412020-06-01 19:53:52 -0700934 }
935
Dominik Laskowski6eab42d2021-09-13 14:34:13 -0700936 if (frameRate >= mKnownFrameRates.back()) {
937 return mKnownFrameRates.back();
Ady Abrahamb1b9d412020-06-01 19:53:52 -0700938 }
939
Marin Shalamanove8a663d2020-11-24 17:48:00 +0100940 auto lowerBound = std::lower_bound(mKnownFrameRates.begin(), mKnownFrameRates.end(), frameRate,
Dominik Laskowski6eab42d2021-09-13 14:34:13 -0700941 isStrictlyLess);
Ady Abrahamb1b9d412020-06-01 19:53:52 -0700942
Dominik Laskowski6eab42d2021-09-13 14:34:13 -0700943 const auto distance1 = std::abs(frameRate.getValue() - lowerBound->getValue());
944 const auto distance2 = std::abs(frameRate.getValue() - std::prev(lowerBound)->getValue());
Ady Abrahamb1b9d412020-06-01 19:53:52 -0700945 return distance1 < distance2 ? *lowerBound : *std::prev(lowerBound);
946}
947
Ana Krulecb9afd792020-06-11 13:16:15 -0700948RefreshRateConfigs::KernelIdleTimerAction RefreshRateConfigs::getIdleTimerAction() const {
949 std::lock_guard lock(mLock);
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800950
951 const Fps deviceMinFps = mMinRefreshRateModeIt->second->getFps();
952 const DisplayModePtr& minByPolicy = getMinRefreshRateByPolicyLocked();
Ana Krulecb9afd792020-06-11 13:16:15 -0700953
954 // Kernel idle timer will set the refresh rate to the device min. If DisplayManager says that
955 // the min allowed refresh rate is higher than the device min, we do not want to enable the
956 // timer.
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800957 if (isStrictlyLess(deviceMinFps, minByPolicy->getFps())) {
958 return KernelIdleTimerAction::TurnOff;
Ana Krulecb9afd792020-06-11 13:16:15 -0700959 }
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800960
961 const DisplayModePtr& maxByPolicy = getMaxRefreshRateByPolicyLocked();
Ana Krulecb9afd792020-06-11 13:16:15 -0700962 if (minByPolicy == maxByPolicy) {
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800963 // Turn on the timer when the min of the primary range is below the device min.
964 if (const Policy* currentPolicy = getCurrentPolicyLocked();
965 isApproxLess(currentPolicy->primaryRange.min, deviceMinFps)) {
966 return KernelIdleTimerAction::TurnOn;
Ana Krulecb9afd792020-06-11 13:16:15 -0700967 }
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800968 return KernelIdleTimerAction::TurnOff;
Ana Krulecb9afd792020-06-11 13:16:15 -0700969 }
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800970
Ana Krulecb9afd792020-06-11 13:16:15 -0700971 // Turn on the timer in all other cases.
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800972 return KernelIdleTimerAction::TurnOn;
Ana Krulecb9afd792020-06-11 13:16:15 -0700973}
974
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800975int RefreshRateConfigs::getFrameRateDivisor(Fps displayRefreshRate, Fps layerFrameRate) {
Ady Abraham62f216c2020-10-13 19:07:23 -0700976 // This calculation needs to be in sync with the java code
977 // in DisplayManagerService.getDisplayInfoForFrameRateOverride
Marin Shalamanov15a0fc62021-08-16 18:20:21 +0200978
979 // The threshold must be smaller than 0.001 in order to differentiate
980 // between the fractional pairs (e.g. 59.94 and 60).
981 constexpr float kThreshold = 0.0009f;
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800982 const auto numPeriods = displayRefreshRate.getValue() / layerFrameRate.getValue();
Ady Abraham0bb6a472020-10-12 10:22:13 -0700983 const auto numPeriodsRounded = std::round(numPeriods);
984 if (std::abs(numPeriods - numPeriodsRounded) > kThreshold) {
Ady Abraham62a0be22020-12-08 16:54:10 -0800985 return 0;
Ady Abraham0bb6a472020-10-12 10:22:13 -0700986 }
987
Ady Abraham62f216c2020-10-13 19:07:23 -0700988 return static_cast<int>(numPeriodsRounded);
989}
990
Marin Shalamanov15a0fc62021-08-16 18:20:21 +0200991bool RefreshRateConfigs::isFractionalPairOrMultiple(Fps smaller, Fps bigger) {
Dominik Laskowski6eab42d2021-09-13 14:34:13 -0700992 if (isStrictlyLess(bigger, smaller)) {
Marin Shalamanov15a0fc62021-08-16 18:20:21 +0200993 return isFractionalPairOrMultiple(bigger, smaller);
994 }
995
996 const auto multiplier = std::round(bigger.getValue() / smaller.getValue());
997 constexpr float kCoef = 1000.f / 1001.f;
Dominik Laskowski6eab42d2021-09-13 14:34:13 -0700998 return isApproxEqual(bigger, Fps::fromValue(smaller.getValue() * multiplier / kCoef)) ||
999 isApproxEqual(bigger, Fps::fromValue(smaller.getValue() * multiplier * kCoef));
Marin Shalamanov15a0fc62021-08-16 18:20:21 +02001000}
1001
Marin Shalamanovba421a82020-11-10 21:49:26 +01001002void RefreshRateConfigs::dump(std::string& result) const {
Dominik Laskowski0acc3842022-04-07 11:23:42 -07001003 using namespace std::string_literals;
1004
Marin Shalamanovba421a82020-11-10 21:49:26 +01001005 std::lock_guard lock(mLock);
Marin Shalamanovba421a82020-11-10 21:49:26 +01001006
Dominik Laskowskif8734e02022-08-26 09:06:59 -07001007 const auto activeModeId = getActiveModeItLocked()->first;
Dominik Laskowski0acc3842022-04-07 11:23:42 -07001008 result += " activeModeId="s;
1009 result += std::to_string(activeModeId.value());
Marin Shalamanovba421a82020-11-10 21:49:26 +01001010
Dominik Laskowski0acc3842022-04-07 11:23:42 -07001011 result += "\n displayModes=\n"s;
Dominik Laskowskib0054a22022-03-03 09:03:06 -08001012 for (const auto& [id, mode] : mDisplayModes) {
Dominik Laskowski0acc3842022-04-07 11:23:42 -07001013 result += " "s;
1014 result += to_string(*mode);
1015 result += '\n';
Marin Shalamanovba421a82020-11-10 21:49:26 +01001016 }
1017
Dominik Laskowski0acc3842022-04-07 11:23:42 -07001018 base::StringAppendF(&result, " displayManagerPolicy=%s\n",
1019 mDisplayManagerPolicy.toString().c_str());
Dominik Laskowskib0054a22022-03-03 09:03:06 -08001020
Dominik Laskowski0acc3842022-04-07 11:23:42 -07001021 if (const Policy& currentPolicy = *getCurrentPolicyLocked();
1022 mOverridePolicy && currentPolicy != mDisplayManagerPolicy) {
1023 base::StringAppendF(&result, " overridePolicy=%s\n", currentPolicy.toString().c_str());
ramindani32cf0602022-03-02 02:30:29 +00001024 }
Dominik Laskowskib0054a22022-03-03 09:03:06 -08001025
Dominik Laskowski0acc3842022-04-07 11:23:42 -07001026 base::StringAppendF(&result, " supportsFrameRateOverrideByContent=%s\n",
1027 mSupportsFrameRateOverrideByContent ? "true" : "false");
1028
1029 result += " idleTimer="s;
Dominik Laskowskib0054a22022-03-03 09:03:06 -08001030 if (mIdleTimer) {
Dominik Laskowski0acc3842022-04-07 11:23:42 -07001031 result += mIdleTimer->dump();
Dominik Laskowskib0054a22022-03-03 09:03:06 -08001032 } else {
Dominik Laskowski0acc3842022-04-07 11:23:42 -07001033 result += "off"s;
Dominik Laskowskib0054a22022-03-03 09:03:06 -08001034 }
1035
Dominik Laskowski0acc3842022-04-07 11:23:42 -07001036 if (const auto controller = mConfig.kernelIdleTimerController) {
1037 base::StringAppendF(&result, " (kernel via %s)", ftl::enum_string(*controller).c_str());
1038 } else {
1039 result += " (platform)"s;
1040 }
1041
1042 result += '\n';
Marin Shalamanovba421a82020-11-10 21:49:26 +01001043}
1044
ramindani32cf0602022-03-02 02:30:29 +00001045std::chrono::milliseconds RefreshRateConfigs::getIdleTimerTimeout() {
1046 return mConfig.idleTimerTimeout;
1047}
1048
Ady Abraham2139f732019-11-13 18:56:40 -08001049} // namespace android::scheduler
Marin Shalamanovbed7fd32020-12-21 20:02:20 +01001050
1051// TODO(b/129481165): remove the #pragma below and fix conversion issues
Ady Abrahamdd5bfa92021-01-07 17:56:08 -08001052#pragma clang diagnostic pop // ignored "-Wextra"