blob: d270655f4f5641818e29408191de29aa7b6d189c [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
Ady Abraham8a82ba62020-01-17 12:43:17 -0800283 int noVoteLayers = 0;
284 int minVoteLayers = 0;
285 int maxVoteLayers = 0;
Ady Abraham71c437d2020-01-31 15:56:57 -0800286 int explicitDefaultVoteLayers = 0;
287 int explicitExactOrMultipleVoteLayers = 0;
Ady Abrahamdd5bfa92021-01-07 17:56:08 -0800288 int explicitExact = 0;
Ady Abraham6fb599b2020-03-05 13:48:22 -0800289 float maxExplicitWeight = 0;
Marin Shalamanovae0b5352021-03-24 12:56:08 +0100290 int seamedFocusedLayers = 0;
Dominik Laskowskia8626ec2021-12-15 18:13:30 -0800291
Ady Abraham8a82ba62020-01-17 12:43:17 -0800292 for (const auto& layer : layers) {
Ady Abrahamdd5bfa92021-01-07 17:56:08 -0800293 switch (layer.vote) {
294 case LayerVoteType::NoVote:
295 noVoteLayers++;
296 break;
297 case LayerVoteType::Min:
298 minVoteLayers++;
299 break;
300 case LayerVoteType::Max:
301 maxVoteLayers++;
302 break;
303 case LayerVoteType::ExplicitDefault:
304 explicitDefaultVoteLayers++;
305 maxExplicitWeight = std::max(maxExplicitWeight, layer.weight);
306 break;
307 case LayerVoteType::ExplicitExactOrMultiple:
308 explicitExactOrMultipleVoteLayers++;
309 maxExplicitWeight = std::max(maxExplicitWeight, layer.weight);
310 break;
311 case LayerVoteType::ExplicitExact:
312 explicitExact++;
313 maxExplicitWeight = std::max(maxExplicitWeight, layer.weight);
314 break;
315 case LayerVoteType::Heuristic:
316 break;
Ady Abraham6fb599b2020-03-05 13:48:22 -0800317 }
Marin Shalamanov46084422020-10-13 12:33:42 +0200318
Marin Shalamanovae0b5352021-03-24 12:56:08 +0100319 if (layer.seamlessness == Seamlessness::SeamedAndSeamless && layer.focused) {
320 seamedFocusedLayers++;
Marin Shalamanov46084422020-10-13 12:33:42 +0200321 }
Ady Abraham6fb599b2020-03-05 13:48:22 -0800322 }
323
Ady Abrahamdd5bfa92021-01-07 17:56:08 -0800324 const bool hasExplicitVoteLayers = explicitDefaultVoteLayers > 0 ||
325 explicitExactOrMultipleVoteLayers > 0 || explicitExact > 0;
Alec Mouri11232a22020-05-14 18:06:25 -0700326
Marin Shalamanov8cd8a992021-09-14 23:22:49 +0200327 const Policy* policy = getCurrentPolicyLocked();
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800328 const auto& defaultMode = mDisplayModes.get(policy->defaultMode)->get();
Dominik Laskowskif8734e02022-08-26 09:06:59 -0700329 const auto& activeMode = *getActiveModeItLocked()->second;
330
Marin Shalamanov8cd8a992021-09-14 23:22:49 +0200331 // If the default mode group is different from the group of current mode,
332 // this means a layer requesting a seamed mode switch just disappeared and
333 // we should switch back to the default group.
334 // However if a seamed layer is still present we anchor around the group
335 // of the current mode, in order to prevent unnecessary seamed mode switches
336 // (e.g. when pausing a video playback).
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800337 const auto anchorGroup =
Dominik Laskowskif8734e02022-08-26 09:06:59 -0700338 seamedFocusedLayers > 0 ? activeMode.getGroup() : defaultMode->getGroup();
Marin Shalamanov8cd8a992021-09-14 23:22:49 +0200339
Steven Thomasf734df42020-04-13 21:09:28 -0700340 // Consider the touch event if there are no Explicit* layers. Otherwise wait until after we've
341 // selected a refresh rate to see if we should apply touch boost.
Dominik Laskowskia8626ec2021-12-15 18:13:30 -0800342 if (signals.touch && !hasExplicitVoteLayers) {
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800343 const DisplayModePtr& max = getMaxRefreshRateByPolicyLocked(anchorGroup);
344 ALOGV("TouchBoost - choose %s", to_string(max->getFps()).c_str());
345 return {max, GlobalSignals{.touch = true}};
Ady Abraham8a82ba62020-01-17 12:43:17 -0800346 }
347
Alec Mouri11232a22020-05-14 18:06:25 -0700348 // If the primary range consists of a single refresh rate then we can only
349 // move out the of range if layers explicitly request a different refresh
350 // rate.
Marin Shalamanove8a663d2020-11-24 17:48:00 +0100351 const bool primaryRangeIsSingleRate =
Dominik Laskowski6eab42d2021-09-13 14:34:13 -0700352 isApproxEqual(policy->primaryRange.min, policy->primaryRange.max);
Alec Mouri11232a22020-05-14 18:06:25 -0700353
Dominik Laskowskia8626ec2021-12-15 18:13:30 -0800354 if (!signals.touch && signals.idle && !(primaryRangeIsSingleRate && hasExplicitVoteLayers)) {
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800355 const DisplayModePtr& min = getMinRefreshRateByPolicyLocked();
356 ALOGV("Idle - choose %s", to_string(min->getFps()).c_str());
357 return {min, GlobalSignals{.idle = true}};
Steven Thomasbb374322020-04-28 22:47:16 -0700358 }
359
Steven Thomasdebafed2020-05-18 17:30:35 -0700360 if (layers.empty() || noVoteLayers == layers.size()) {
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800361 const DisplayModePtr& max = getMaxRefreshRateByPolicyLocked(anchorGroup);
362 ALOGV("no layers with votes - choose %s", to_string(max->getFps()).c_str());
363 return {max, kNoSignals};
Steven Thomasbb374322020-04-28 22:47:16 -0700364 }
365
Ady Abraham8a82ba62020-01-17 12:43:17 -0800366 // Only if all layers want Min we should return Min
367 if (noVoteLayers + minVoteLayers == layers.size()) {
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800368 const DisplayModePtr& min = getMinRefreshRateByPolicyLocked();
369 ALOGV("all layers Min - choose %s", to_string(min->getFps()).c_str());
370 return {min, kNoSignals};
Ady Abraham8a82ba62020-01-17 12:43:17 -0800371 }
372
Ady Abraham8a82ba62020-01-17 12:43:17 -0800373 // Find the best refresh rate based on score
Ady Abraham62a0be22020-12-08 16:54:10 -0800374 std::vector<RefreshRateScore> scores;
Steven Thomasf734df42020-04-13 21:09:28 -0700375 scores.reserve(mAppRequestRefreshRates.size());
Ady Abraham8a82ba62020-01-17 12:43:17 -0800376
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800377 for (const DisplayModeIterator modeIt : mAppRequestRefreshRates) {
378 scores.emplace_back(RefreshRateScore{modeIt, 0.0f});
Ady Abraham8a82ba62020-01-17 12:43:17 -0800379 }
380
381 for (const auto& layer : layers) {
rnlee3bd610662021-06-23 16:27:57 -0700382 ALOGV("Calculating score for %s (%s, weight %.2f, desired %.2f) ", layer.name.c_str(),
Dominik Laskowskif5d0ea52021-09-26 17:27:01 -0700383 ftl::enum_string(layer.vote).c_str(), layer.weight,
rnlee3bd610662021-06-23 16:27:57 -0700384 layer.desiredRefreshRate.getValue());
Ady Abraham4ccdcb42020-02-11 17:34:34 -0800385 if (layer.vote == LayerVoteType::NoVote || layer.vote == LayerVoteType::Min) {
Ady Abraham8a82ba62020-01-17 12:43:17 -0800386 continue;
387 }
388
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800389 const auto weight = layer.weight;
Ady Abraham71c437d2020-01-31 15:56:57 -0800390
Ady Abraham62f51d92022-08-24 22:20:22 +0000391 for (auto& [modeIt, overallScore, fixedRateBelowThresholdLayersScore] : scores) {
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800392 const auto& [id, mode] = *modeIt;
Dominik Laskowskif8734e02022-08-26 09:06:59 -0700393 const bool isSeamlessSwitch = mode->getGroup() == activeMode.getGroup();
Marin Shalamanov46084422020-10-13 12:33:42 +0200394
Marin Shalamanov53fc11d2020-11-20 14:00:13 +0100395 if (layer.seamlessness == Seamlessness::OnlySeamless && !isSeamlessSwitch) {
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100396 ALOGV("%s ignores %s to avoid non-seamless switch. Current mode = %s",
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800397 formatLayerInfo(layer, weight).c_str(), to_string(*mode).c_str(),
Dominik Laskowskif8734e02022-08-26 09:06:59 -0700398 to_string(activeMode).c_str());
Marin Shalamanov46084422020-10-13 12:33:42 +0200399 continue;
400 }
401
Marin Shalamanov53fc11d2020-11-20 14:00:13 +0100402 if (layer.seamlessness == Seamlessness::SeamedAndSeamless && !isSeamlessSwitch &&
403 !layer.focused) {
404 ALOGV("%s ignores %s because it's not focused and the switch is going to be seamed."
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100405 " Current mode = %s",
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800406 formatLayerInfo(layer, weight).c_str(), to_string(*mode).c_str(),
Dominik Laskowskif8734e02022-08-26 09:06:59 -0700407 to_string(activeMode).c_str());
Marin Shalamanov53fc11d2020-11-20 14:00:13 +0100408 continue;
409 }
410
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100411 // Layers with default seamlessness vote for the current mode group if
Marin Shalamanov53fc11d2020-11-20 14:00:13 +0100412 // there are layers with seamlessness=SeamedAndSeamless and for the default
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100413 // mode group otherwise. In second case, if the current mode group is different
Marin Shalamanov53fc11d2020-11-20 14:00:13 +0100414 // from the default, this means a layer with seamlessness=SeamedAndSeamless has just
415 // disappeared.
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800416 const bool isInPolicyForDefault = mode->getGroup() == anchorGroup;
Marin Shalamanovae0b5352021-03-24 12:56:08 +0100417 if (layer.seamlessness == Seamlessness::Default && !isInPolicyForDefault) {
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100418 ALOGV("%s ignores %s. Current mode = %s", formatLayerInfo(layer, weight).c_str(),
Dominik Laskowskif8734e02022-08-26 09:06:59 -0700419 to_string(*mode).c_str(), to_string(activeMode).c_str());
Marin Shalamanov46084422020-10-13 12:33:42 +0200420 continue;
421 }
422
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800423 const bool inPrimaryRange = policy->primaryRange.includes(mode->getFps());
Alec Mouri11232a22020-05-14 18:06:25 -0700424 if ((primaryRangeIsSingleRate || !inPrimaryRange) &&
Ady Abrahamdd5bfa92021-01-07 17:56:08 -0800425 !(layer.focused &&
426 (layer.vote == LayerVoteType::ExplicitDefault ||
427 layer.vote == LayerVoteType::ExplicitExact))) {
Ady Abraham20c029c2020-07-06 12:58:05 -0700428 // Only focused layers with ExplicitDefault frame rate settings are allowed to score
Ady Abrahamaae5ed52020-06-26 09:32:43 -0700429 // refresh rates outside the primary range.
Steven Thomasf734df42020-04-13 21:09:28 -0700430 continue;
431 }
432
Ady Abrahamae2e3c72022-08-13 05:12:13 +0000433 const float layerScore =
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800434 calculateLayerScoreLocked(layer, mode->getFps(), isSeamlessSwitch);
Ady Abrahamae2e3c72022-08-13 05:12:13 +0000435 const float weightedLayerScore = weight * layerScore;
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800436
Ady Abraham13cfb362022-08-13 05:12:13 +0000437 // Layer with fixed source has a special consideration which depends on the
Ady Abrahamae2e3c72022-08-13 05:12:13 +0000438 // mConfig.frameRateMultipleThreshold. We don't want these layers to score
439 // refresh rates above the threshold, but we also don't want to favor the lower
440 // ones by having a greater number of layers scoring them. Instead, we calculate
441 // the score independently for these layers and later decide which
442 // refresh rates to add it. For example, desired 24 fps with 120 Hz threshold should not
443 // score 120 Hz, but desired 60 fps should contribute to the score.
444 const bool fixedSourceLayer = [](LayerVoteType vote) {
445 switch (vote) {
446 case LayerVoteType::ExplicitExactOrMultiple:
447 case LayerVoteType::Heuristic:
448 return true;
449 case LayerVoteType::NoVote:
450 case LayerVoteType::Min:
451 case LayerVoteType::Max:
452 case LayerVoteType::ExplicitDefault:
453 case LayerVoteType::ExplicitExact:
454 return false;
455 }
456 }(layer.vote);
Ady Abraham62f51d92022-08-24 22:20:22 +0000457 const bool layerBelowThreshold = mConfig.frameRateMultipleThreshold != 0 &&
Ady Abrahamae2e3c72022-08-13 05:12:13 +0000458 layer.desiredRefreshRate <
459 Fps::fromValue(mConfig.frameRateMultipleThreshold / 2);
Ady Abraham62f51d92022-08-24 22:20:22 +0000460 if (fixedSourceLayer && layerBelowThreshold) {
Ady Abraham13cfb362022-08-13 05:12:13 +0000461 const bool modeAboveThreshold =
462 mode->getFps() >= Fps::fromValue(mConfig.frameRateMultipleThreshold);
Ady Abraham62f51d92022-08-24 22:20:22 +0000463 if (modeAboveThreshold) {
Ady Abrahamae2e3c72022-08-13 05:12:13 +0000464 ALOGV("%s gives %s fixed source (above threshold) score of %.4f",
465 formatLayerInfo(layer, weight).c_str(), to_string(mode->getFps()).c_str(),
466 layerScore);
Ady Abraham62f51d92022-08-24 22:20:22 +0000467 fixedRateBelowThresholdLayersScore.modeAboveThreshold += weightedLayerScore;
Ady Abrahamae2e3c72022-08-13 05:12:13 +0000468 } else {
469 ALOGV("%s gives %s fixed source (below threshold) score of %.4f",
470 formatLayerInfo(layer, weight).c_str(), to_string(mode->getFps()).c_str(),
471 layerScore);
Ady Abraham62f51d92022-08-24 22:20:22 +0000472 fixedRateBelowThresholdLayersScore.modeBelowThreshold += weightedLayerScore;
Ady Abrahamae2e3c72022-08-13 05:12:13 +0000473 }
474 } else {
475 ALOGV("%s gives %s score of %.4f", formatLayerInfo(layer, weight).c_str(),
476 to_string(mode->getFps()).c_str(), layerScore);
477 overallScore += weightedLayerScore;
478 }
Ady Abraham8a82ba62020-01-17 12:43:17 -0800479 }
480 }
481
Ady Abrahamae2e3c72022-08-13 05:12:13 +0000482 // We want to find the best refresh rate without the fixed source layers,
Ady Abraham62f51d92022-08-24 22:20:22 +0000483 // so we could know whether we should add the modeAboveThreshold scores or not.
Ady Abrahamae2e3c72022-08-13 05:12:13 +0000484 // If the best refresh rate is already above the threshold, it means that
485 // some non-fixed source layers already scored it, so we can just add the score
486 // for all fixed source layers, even the ones that are above the threshold.
487 const bool maxScoreAboveThreshold = [&] {
488 if (mConfig.frameRateMultipleThreshold == 0 || scores.empty()) {
489 return false;
490 }
491
492 const auto maxScoreIt =
493 std::max_element(scores.begin(), scores.end(),
494 [](RefreshRateScore max, RefreshRateScore current) {
495 const auto& [modeIt, overallScore, _] = current;
496 return overallScore > max.overallScore;
497 });
498 ALOGV("%s is the best refresh rate without fixed source layers. It is %s the threshold for "
499 "refresh rate multiples",
500 to_string(maxScoreIt->modeIt->second->getFps()).c_str(),
501 maxScoreAboveThreshold ? "above" : "below");
502 return maxScoreIt->modeIt->second->getFps() >=
503 Fps::fromValue(mConfig.frameRateMultipleThreshold);
504 }();
505
506 // Now we can add the fixed rate layers score
Ady Abraham62f51d92022-08-24 22:20:22 +0000507 for (auto& [modeIt, overallScore, fixedRateBelowThresholdLayersScore] : scores) {
508 overallScore += fixedRateBelowThresholdLayersScore.modeBelowThreshold;
Ady Abrahamae2e3c72022-08-13 05:12:13 +0000509 if (maxScoreAboveThreshold) {
Ady Abraham62f51d92022-08-24 22:20:22 +0000510 overallScore += fixedRateBelowThresholdLayersScore.modeAboveThreshold;
Ady Abrahamae2e3c72022-08-13 05:12:13 +0000511 }
512 ALOGV("%s adjusted overallScore is %.4f", to_string(modeIt->second->getFps()).c_str(),
513 overallScore);
514 }
515
516 // Now that we scored all the refresh rates we need to pick the one that got the highest
517 // overallScore. In case of a tie we will pick the higher refresh rate if any of the layers
518 // wanted Max, or the lower otherwise.
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800519 const DisplayModePtr& bestRefreshRate = maxVoteLayers > 0
520 ? getMaxScoreRefreshRate(scores.rbegin(), scores.rend())
521 : getMaxScoreRefreshRate(scores.begin(), scores.end());
Ady Abraham34702102020-02-10 14:12:05 -0800522
Alec Mouri11232a22020-05-14 18:06:25 -0700523 if (primaryRangeIsSingleRate) {
524 // If we never scored any layers, then choose the rate from the primary
525 // range instead of picking a random score from the app range.
526 if (std::all_of(scores.begin(), scores.end(),
Ady Abrahamae2e3c72022-08-13 05:12:13 +0000527 [](RefreshRateScore score) { return score.overallScore == 0; })) {
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800528 const DisplayModePtr& max = getMaxRefreshRateByPolicyLocked(anchorGroup);
529 ALOGV("layers not scored - choose %s", to_string(max->getFps()).c_str());
530 return {max, kNoSignals};
Alec Mouri11232a22020-05-14 18:06:25 -0700531 } else {
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800532 return {bestRefreshRate, kNoSignals};
Alec Mouri11232a22020-05-14 18:06:25 -0700533 }
534 }
535
Steven Thomasf734df42020-04-13 21:09:28 -0700536 // Consider the touch event if there are no ExplicitDefault layers. ExplicitDefault are mostly
537 // interactive (as opposed to ExplicitExactOrMultiple) and therefore if those posted an explicit
538 // vote we should not change it if we get a touch event. Only apply touch boost if it will
539 // actually increase the refresh rate over the normal selection.
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800540 const DisplayModePtr& touchRefreshRate = getMaxRefreshRateByPolicyLocked(anchorGroup);
Alec Mouri11232a22020-05-14 18:06:25 -0700541
Ady Abraham5e4e9832021-06-14 13:40:56 -0700542 const bool touchBoostForExplicitExact = [&] {
Andy Yu2ae6b6b2021-11-18 14:51:06 -0800543 if (mSupportsFrameRateOverrideByContent) {
Ady Abraham5e4e9832021-06-14 13:40:56 -0700544 // Enable touch boost if there are other layers besides exact
545 return explicitExact + noVoteLayers != layers.size();
546 } else {
547 // Enable touch boost if there are no exact layers
548 return explicitExact == 0;
549 }
550 }();
Dominik Laskowski6eab42d2021-09-13 14:34:13 -0700551
552 using fps_approx_ops::operator<;
553
Dominik Laskowskia8626ec2021-12-15 18:13:30 -0800554 if (signals.touch && explicitDefaultVoteLayers == 0 && touchBoostForExplicitExact &&
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800555 bestRefreshRate->getFps() < touchRefreshRate->getFps()) {
556 ALOGV("TouchBoost - choose %s", to_string(touchRefreshRate->getFps()).c_str());
Dominik Laskowskia8626ec2021-12-15 18:13:30 -0800557 return {touchRefreshRate, GlobalSignals{.touch = true}};
Steven Thomasf734df42020-04-13 21:09:28 -0700558 }
559
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800560 return {bestRefreshRate, kNoSignals};
Ady Abraham34702102020-02-10 14:12:05 -0800561}
562
Ady Abraham62a0be22020-12-08 16:54:10 -0800563std::unordered_map<uid_t, std::vector<const RefreshRateConfigs::LayerRequirement*>>
564groupLayersByUid(const std::vector<RefreshRateConfigs::LayerRequirement>& layers) {
565 std::unordered_map<uid_t, std::vector<const RefreshRateConfigs::LayerRequirement*>> layersByUid;
566 for (const auto& layer : layers) {
567 auto iter = layersByUid.emplace(layer.ownerUid,
568 std::vector<const RefreshRateConfigs::LayerRequirement*>());
569 auto& layersWithSameUid = iter.first->second;
570 layersWithSameUid.push_back(&layer);
571 }
572
573 // Remove uids that can't have a frame rate override
574 for (auto iter = layersByUid.begin(); iter != layersByUid.end();) {
575 const auto& layersWithSameUid = iter->second;
576 bool skipUid = false;
577 for (const auto& layer : layersWithSameUid) {
578 if (layer->vote == RefreshRateConfigs::LayerVoteType::Max ||
579 layer->vote == RefreshRateConfigs::LayerVoteType::Heuristic) {
580 skipUid = true;
581 break;
582 }
583 }
584 if (skipUid) {
585 iter = layersByUid.erase(iter);
586 } else {
587 ++iter;
588 }
589 }
590
591 return layersByUid;
592}
593
Ady Abraham62a0be22020-12-08 16:54:10 -0800594RefreshRateConfigs::UidToFrameRateOverride RefreshRateConfigs::getFrameRateOverrides(
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800595 const std::vector<LayerRequirement>& layers, Fps displayRefreshRate,
Dominik Laskowski6eab42d2021-09-13 14:34:13 -0700596 GlobalSignals globalSignals) const {
Ady Abraham62a0be22020-12-08 16:54:10 -0800597 ATRACE_CALL();
Ady Abraham62a0be22020-12-08 16:54:10 -0800598
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800599 ALOGV("%s: %zu layers", __func__, layers.size());
600
Ady Abraham62a0be22020-12-08 16:54:10 -0800601 std::lock_guard lock(mLock);
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800602
603 std::vector<RefreshRateScore> scores;
604 scores.reserve(mDisplayModes.size());
605
606 for (auto it = mDisplayModes.begin(); it != mDisplayModes.end(); ++it) {
607 scores.emplace_back(RefreshRateScore{it, 0.0f});
608 }
609
610 std::sort(scores.begin(), scores.end(), [](const auto& lhs, const auto& rhs) {
611 const auto& mode1 = lhs.modeIt->second;
612 const auto& mode2 = rhs.modeIt->second;
613 return isStrictlyLess(mode1->getFps(), mode2->getFps());
614 });
615
Ady Abraham62a0be22020-12-08 16:54:10 -0800616 std::unordered_map<uid_t, std::vector<const LayerRequirement*>> layersByUid =
617 groupLayersByUid(layers);
618 UidToFrameRateOverride frameRateOverrides;
619 for (const auto& [uid, layersWithSameUid] : layersByUid) {
Ady Abrahamdd5bfa92021-01-07 17:56:08 -0800620 // Layers with ExplicitExactOrMultiple expect touch boost
621 const bool hasExplicitExactOrMultiple =
622 std::any_of(layersWithSameUid.cbegin(), layersWithSameUid.cend(),
623 [](const auto& layer) {
624 return layer->vote == LayerVoteType::ExplicitExactOrMultiple;
625 });
626
Dominik Laskowski6eab42d2021-09-13 14:34:13 -0700627 if (globalSignals.touch && hasExplicitExactOrMultiple) {
Ady Abrahamdd5bfa92021-01-07 17:56:08 -0800628 continue;
629 }
630
Ady Abrahamae2e3c72022-08-13 05:12:13 +0000631 for (auto& [_, score, _1] : scores) {
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800632 score = 0;
Ady Abraham62a0be22020-12-08 16:54:10 -0800633 }
634
635 for (const auto& layer : layersWithSameUid) {
636 if (layer->vote == LayerVoteType::NoVote || layer->vote == LayerVoteType::Min) {
637 continue;
638 }
639
640 LOG_ALWAYS_FATAL_IF(layer->vote != LayerVoteType::ExplicitDefault &&
Ady Abrahamdd5bfa92021-01-07 17:56:08 -0800641 layer->vote != LayerVoteType::ExplicitExactOrMultiple &&
642 layer->vote != LayerVoteType::ExplicitExact);
Ady Abrahamae2e3c72022-08-13 05:12:13 +0000643 for (auto& [modeIt, score, _] : scores) {
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800644 constexpr bool isSeamlessSwitch = true;
645 const auto layerScore = calculateLayerScoreLocked(*layer, modeIt->second->getFps(),
646 isSeamlessSwitch);
647 score += layer->weight * layerScore;
Ady Abraham62a0be22020-12-08 16:54:10 -0800648 }
649 }
650
Ady Abrahamcc315492022-02-17 17:06:39 -0800651 // We just care about the refresh rates which are a divisor of the
Ady Abraham62a0be22020-12-08 16:54:10 -0800652 // display refresh rate
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800653 const auto it = std::remove_if(scores.begin(), scores.end(), [&](RefreshRateScore score) {
654 const auto& [id, mode] = *score.modeIt;
655 return getFrameRateDivisor(displayRefreshRate, mode->getFps()) == 0;
656 });
657 scores.erase(it, scores.end());
Ady Abraham62a0be22020-12-08 16:54:10 -0800658
659 // If we never scored any layers, we don't have a preferred frame rate
660 if (std::all_of(scores.begin(), scores.end(),
Ady Abrahamae2e3c72022-08-13 05:12:13 +0000661 [](RefreshRateScore score) { return score.overallScore == 0; })) {
Ady Abraham62a0be22020-12-08 16:54:10 -0800662 continue;
663 }
664
665 // Now that we scored all the refresh rates we need to pick the one that got the highest
666 // score.
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800667 const DisplayModePtr& bestRefreshRate =
668 getMaxScoreRefreshRate(scores.begin(), scores.end());
669
Ady Abraham5cc2e262021-03-25 13:09:17 -0700670 frameRateOverrides.emplace(uid, bestRefreshRate->getFps());
Ady Abraham62a0be22020-12-08 16:54:10 -0800671 }
672
673 return frameRateOverrides;
674}
675
Marin Shalamanoveadf2e72020-12-10 15:35:28 +0100676std::optional<Fps> RefreshRateConfigs::onKernelTimerChanged(
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800677 std::optional<DisplayModeId> desiredActiveModeId, bool timerExpired) const {
Ady Abraham2139f732019-11-13 18:56:40 -0800678 std::lock_guard lock(mLock);
Marin Shalamanoveadf2e72020-12-10 15:35:28 +0100679
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800680 const DisplayModePtr& current = desiredActiveModeId
681 ? mDisplayModes.get(*desiredActiveModeId)->get()
Dominik Laskowskif8734e02022-08-26 09:06:59 -0700682 : getActiveModeItLocked()->second;
Marin Shalamanoveadf2e72020-12-10 15:35:28 +0100683
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800684 const DisplayModePtr& min = mMinRefreshRateModeIt->second;
685 if (current == min) {
686 return {};
Marin Shalamanoveadf2e72020-12-10 15:35:28 +0100687 }
688
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800689 const auto& mode = timerExpired ? min : current;
690 return mode->getFps();
Steven Thomasf734df42020-04-13 21:09:28 -0700691}
692
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800693const DisplayModePtr& RefreshRateConfigs::getMinRefreshRateByPolicyLocked() const {
Dominik Laskowskif8734e02022-08-26 09:06:59 -0700694 const auto& activeMode = *getActiveModeItLocked()->second;
695
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800696 for (const DisplayModeIterator modeIt : mPrimaryRefreshRates) {
697 const auto& mode = modeIt->second;
Dominik Laskowskif8734e02022-08-26 09:06:59 -0700698 if (activeMode.getGroup() == mode->getGroup()) {
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800699 return mode;
Marin Shalamanov46084422020-10-13 12:33:42 +0200700 }
701 }
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800702
Dominik Laskowskif8734e02022-08-26 09:06:59 -0700703 ALOGE("Can't find min refresh rate by policy with the same mode group as the current mode %s",
704 to_string(activeMode).c_str());
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800705
706 // Default to the lowest refresh rate.
707 return mPrimaryRefreshRates.front()->second;
Ady Abraham2139f732019-11-13 18:56:40 -0800708}
709
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800710DisplayModePtr RefreshRateConfigs::getMaxRefreshRateByPolicy() const {
Ady Abraham2139f732019-11-13 18:56:40 -0800711 std::lock_guard lock(mLock);
Steven Thomasf734df42020-04-13 21:09:28 -0700712 return getMaxRefreshRateByPolicyLocked();
713}
714
Dominik Laskowskif8734e02022-08-26 09:06:59 -0700715const DisplayModePtr& RefreshRateConfigs::getMaxRefreshRateByPolicyLocked() const {
716 const int anchorGroup = getActiveModeItLocked()->second->getGroup();
717 return getMaxRefreshRateByPolicyLocked(anchorGroup);
718}
719
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800720const DisplayModePtr& RefreshRateConfigs::getMaxRefreshRateByPolicyLocked(int anchorGroup) const {
721 for (auto it = mPrimaryRefreshRates.rbegin(); it != mPrimaryRefreshRates.rend(); ++it) {
722 const auto& mode = (*it)->second;
723 if (anchorGroup == mode->getGroup()) {
724 return mode;
Marin Shalamanov46084422020-10-13 12:33:42 +0200725 }
726 }
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800727
Dominik Laskowskif8734e02022-08-26 09:06:59 -0700728 const auto& activeMode = *getActiveModeItLocked()->second;
729 ALOGE("Can't find max refresh rate by policy with the same mode group as the current mode %s",
730 to_string(activeMode).c_str());
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800731
732 // Default to the highest refresh rate.
733 return mPrimaryRefreshRates.back()->second;
Ady Abraham2139f732019-11-13 18:56:40 -0800734}
735
Dominik Laskowskif8734e02022-08-26 09:06:59 -0700736DisplayModePtr RefreshRateConfigs::getActiveModePtr() const {
Ady Abraham2139f732019-11-13 18:56:40 -0800737 std::lock_guard lock(mLock);
Dominik Laskowskif8734e02022-08-26 09:06:59 -0700738 return getActiveModeItLocked()->second;
739}
740
741const DisplayMode& RefreshRateConfigs::getActiveMode() const {
742 // Reads from kMainThreadContext do not require mLock.
743 ftl::FakeGuard guard(mLock);
744 return *mActiveModeIt->second;
745}
746
747DisplayModeIterator RefreshRateConfigs::getActiveModeItLocked() const {
748 // Reads under mLock do not require kMainThreadContext.
749 return FTL_FAKE_GUARD(kMainThreadContext, mActiveModeIt);
Ady Abraham2139f732019-11-13 18:56:40 -0800750}
751
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800752void RefreshRateConfigs::setActiveModeId(DisplayModeId modeId) {
Ady Abraham2139f732019-11-13 18:56:40 -0800753 std::lock_guard lock(mLock);
Marin Shalamanov4c7831e2021-06-08 20:44:06 +0200754
755 // Invalidate the cached invocation to getBestRefreshRate. This forces
756 // the refresh rate to be recomputed on the next call to getBestRefreshRate.
Dominik Laskowskia8626ec2021-12-15 18:13:30 -0800757 mGetBestRefreshRateCache.reset();
Marin Shalamanov4c7831e2021-06-08 20:44:06 +0200758
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800759 mActiveModeIt = mDisplayModes.find(modeId);
760 LOG_ALWAYS_FATAL_IF(mActiveModeIt == mDisplayModes.end());
Ady Abrahamb4b1e0a2019-11-20 18:25:35 -0800761}
762
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800763RefreshRateConfigs::RefreshRateConfigs(DisplayModes modes, DisplayModeId activeModeId,
rnlee3bd610662021-06-23 16:27:57 -0700764 Config config)
765 : mKnownFrameRates(constructKnownFrameRates(modes)), mConfig(config) {
Ady Abraham9a2ea342021-09-03 17:32:34 -0700766 initializeIdleTimer();
Dominik Laskowskif8734e02022-08-26 09:06:59 -0700767 FTL_FAKE_GUARD(kMainThreadContext, updateDisplayModes(std::move(modes), activeModeId));
Marin Shalamanoveadf2e72020-12-10 15:35:28 +0100768}
769
Ady Abraham9a2ea342021-09-03 17:32:34 -0700770void RefreshRateConfigs::initializeIdleTimer() {
ramindani32cf0602022-03-02 02:30:29 +0000771 if (mConfig.idleTimerTimeout > 0ms) {
Ady Abraham9a2ea342021-09-03 17:32:34 -0700772 mIdleTimer.emplace(
ramindani32cf0602022-03-02 02:30:29 +0000773 "IdleTimer", mConfig.idleTimerTimeout,
Dominik Laskowski83bd7712022-01-07 14:30:53 -0800774 [this] {
775 std::scoped_lock lock(mIdleTimerCallbacksMutex);
776 if (const auto callbacks = getIdleTimerCallbacks()) {
777 callbacks->onReset();
778 }
Ady Abraham9a2ea342021-09-03 17:32:34 -0700779 },
Dominik Laskowski83bd7712022-01-07 14:30:53 -0800780 [this] {
781 std::scoped_lock lock(mIdleTimerCallbacksMutex);
782 if (const auto callbacks = getIdleTimerCallbacks()) {
783 callbacks->onExpired();
784 }
Ady Abraham9a2ea342021-09-03 17:32:34 -0700785 });
Ady Abraham9a2ea342021-09-03 17:32:34 -0700786 }
787}
788
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800789void RefreshRateConfigs::updateDisplayModes(DisplayModes modes, DisplayModeId activeModeId) {
Marin Shalamanoveadf2e72020-12-10 15:35:28 +0100790 std::lock_guard lock(mLock);
Marin Shalamanov4c7831e2021-06-08 20:44:06 +0200791
Marin Shalamanov4c7831e2021-06-08 20:44:06 +0200792 // Invalidate the cached invocation to getBestRefreshRate. This forces
793 // the refresh rate to be recomputed on the next call to getBestRefreshRate.
Dominik Laskowskia8626ec2021-12-15 18:13:30 -0800794 mGetBestRefreshRateCache.reset();
Marin Shalamanov4c7831e2021-06-08 20:44:06 +0200795
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800796 mDisplayModes = std::move(modes);
797 mActiveModeIt = mDisplayModes.find(activeModeId);
798 LOG_ALWAYS_FATAL_IF(mActiveModeIt == mDisplayModes.end());
Ady Abrahamabc27602020-04-08 17:20:29 -0700799
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800800 const auto sortedModes =
801 sortByRefreshRate(mDisplayModes, [](const DisplayMode&) { return true; });
802 mMinRefreshRateModeIt = sortedModes.front();
803 mMaxRefreshRateModeIt = sortedModes.back();
804
Marin Shalamanov75f37252021-02-10 21:43:57 +0100805 // Reset the policy because the old one may no longer be valid.
806 mDisplayManagerPolicy = {};
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800807 mDisplayManagerPolicy.defaultMode = activeModeId;
Ady Abraham64c2fc02020-12-29 12:07:50 -0800808
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800809 mSupportsFrameRateOverrideByContent =
810 mConfig.enableFrameRateOverride && canModesSupportFrameRateOverride(sortedModes);
Ady Abraham4899ff82021-01-06 13:53:29 -0800811
Ady Abrahamabc27602020-04-08 17:20:29 -0700812 constructAvailableRefreshRates();
Ady Abrahamb4b1e0a2019-11-20 18:25:35 -0800813}
814
Marin Shalamanoveadf2e72020-12-10 15:35:28 +0100815bool RefreshRateConfigs::isPolicyValidLocked(const Policy& policy) const {
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100816 // defaultMode must be a valid mode, and within the given refresh rate range.
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800817 if (const auto mode = mDisplayModes.get(policy.defaultMode)) {
818 if (!policy.primaryRange.includes(mode->get()->getFps())) {
819 ALOGE("Default mode is not in the primary range.");
820 return false;
821 }
822 } else {
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100823 ALOGE("Default mode is not found.");
Steven Thomasd4071902020-03-24 16:02:53 -0700824 return false;
825 }
Dominik Laskowski6eab42d2021-09-13 14:34:13 -0700826
827 using namespace fps_approx_ops;
828 return policy.appRequestRange.min <= policy.primaryRange.min &&
829 policy.appRequestRange.max >= policy.primaryRange.max;
Steven Thomasd4071902020-03-24 16:02:53 -0700830}
831
832status_t RefreshRateConfigs::setDisplayManagerPolicy(const Policy& policy) {
Ady Abraham2139f732019-11-13 18:56:40 -0800833 std::lock_guard lock(mLock);
Marin Shalamanoveadf2e72020-12-10 15:35:28 +0100834 if (!isPolicyValidLocked(policy)) {
Marin Shalamanovb6674e72020-11-06 13:05:57 +0100835 ALOGE("Invalid refresh rate policy: %s", policy.toString().c_str());
Ana Kruleced3a8cc2019-11-14 00:55:07 +0100836 return BAD_VALUE;
837 }
Dominik Laskowskia8626ec2021-12-15 18:13:30 -0800838 mGetBestRefreshRateCache.reset();
Steven Thomasd4071902020-03-24 16:02:53 -0700839 Policy previousPolicy = *getCurrentPolicyLocked();
840 mDisplayManagerPolicy = policy;
841 if (*getCurrentPolicyLocked() == previousPolicy) {
842 return CURRENT_POLICY_UNCHANGED;
Ana Kruleced3a8cc2019-11-14 00:55:07 +0100843 }
Ady Abraham2139f732019-11-13 18:56:40 -0800844 constructAvailableRefreshRates();
Ana Kruleced3a8cc2019-11-14 00:55:07 +0100845 return NO_ERROR;
846}
847
Steven Thomasd4071902020-03-24 16:02:53 -0700848status_t RefreshRateConfigs::setOverridePolicy(const std::optional<Policy>& policy) {
Ana Kruleced3a8cc2019-11-14 00:55:07 +0100849 std::lock_guard lock(mLock);
Marin Shalamanoveadf2e72020-12-10 15:35:28 +0100850 if (policy && !isPolicyValidLocked(*policy)) {
Steven Thomasd4071902020-03-24 16:02:53 -0700851 return BAD_VALUE;
852 }
Dominik Laskowskia8626ec2021-12-15 18:13:30 -0800853 mGetBestRefreshRateCache.reset();
Steven Thomasd4071902020-03-24 16:02:53 -0700854 Policy previousPolicy = *getCurrentPolicyLocked();
855 mOverridePolicy = policy;
856 if (*getCurrentPolicyLocked() == previousPolicy) {
857 return CURRENT_POLICY_UNCHANGED;
858 }
859 constructAvailableRefreshRates();
860 return NO_ERROR;
861}
862
863const RefreshRateConfigs::Policy* RefreshRateConfigs::getCurrentPolicyLocked() const {
864 return mOverridePolicy ? &mOverridePolicy.value() : &mDisplayManagerPolicy;
865}
866
867RefreshRateConfigs::Policy RefreshRateConfigs::getCurrentPolicy() const {
868 std::lock_guard lock(mLock);
869 return *getCurrentPolicyLocked();
870}
871
872RefreshRateConfigs::Policy RefreshRateConfigs::getDisplayManagerPolicy() const {
873 std::lock_guard lock(mLock);
874 return mDisplayManagerPolicy;
Ana Kruleced3a8cc2019-11-14 00:55:07 +0100875}
876
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100877bool RefreshRateConfigs::isModeAllowed(DisplayModeId modeId) const {
Ana Kruleced3a8cc2019-11-14 00:55:07 +0100878 std::lock_guard lock(mLock);
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800879 return std::any_of(mAppRequestRefreshRates.begin(), mAppRequestRefreshRates.end(),
880 [modeId](DisplayModeIterator modeIt) {
881 return modeIt->second->getId() == modeId;
882 });
Ady Abraham2139f732019-11-13 18:56:40 -0800883}
884
885void RefreshRateConfigs::constructAvailableRefreshRates() {
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800886 // Filter modes based on current policy and sort on refresh rate.
Steven Thomasd4071902020-03-24 16:02:53 -0700887 const Policy* policy = getCurrentPolicyLocked();
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800888 ALOGV("%s: %s ", __func__, policy->toString().c_str());
Ady Abrahamabc27602020-04-08 17:20:29 -0700889
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800890 const auto& defaultMode = mDisplayModes.get(policy->defaultMode)->get();
Ady Abraham8a82ba62020-01-17 12:43:17 -0800891
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800892 const auto filterRefreshRates = [&](FpsRange range, const char* rangeName) REQUIRES(mLock) {
893 const auto filter = [&](const DisplayMode& mode) {
894 return mode.getResolution() == defaultMode->getResolution() &&
895 mode.getDpi() == defaultMode->getDpi() &&
896 (policy->allowGroupSwitching || mode.getGroup() == defaultMode->getGroup()) &&
897 range.includes(mode.getFps());
898 };
Ady Abraham8a82ba62020-01-17 12:43:17 -0800899
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800900 const auto modes = sortByRefreshRate(mDisplayModes, filter);
901 LOG_ALWAYS_FATAL_IF(modes.empty(), "No matching modes for %s range %s", rangeName,
902 to_string(range).c_str());
Dominik Laskowski953b7fd2022-01-08 19:34:59 -0800903
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800904 const auto stringifyModes = [&] {
905 std::string str;
906 for (const auto modeIt : modes) {
907 str += to_string(modeIt->second->getFps());
908 str.push_back(' ');
909 }
910 return str;
911 };
912 ALOGV("%s refresh rates: %s", rangeName, stringifyModes().c_str());
Steven Thomasf734df42020-04-13 21:09:28 -0700913
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800914 return modes;
915 };
916
917 mPrimaryRefreshRates = filterRefreshRates(policy->primaryRange, "primary");
918 mAppRequestRefreshRates = filterRefreshRates(policy->appRequestRange, "app request");
Ady Abraham2139f732019-11-13 18:56:40 -0800919}
920
Marin Shalamanove8a663d2020-11-24 17:48:00 +0100921Fps RefreshRateConfigs::findClosestKnownFrameRate(Fps frameRate) const {
Dominik Laskowski6eab42d2021-09-13 14:34:13 -0700922 using namespace fps_approx_ops;
923
924 if (frameRate <= mKnownFrameRates.front()) {
925 return mKnownFrameRates.front();
Ady Abrahamb1b9d412020-06-01 19:53:52 -0700926 }
927
Dominik Laskowski6eab42d2021-09-13 14:34:13 -0700928 if (frameRate >= mKnownFrameRates.back()) {
929 return mKnownFrameRates.back();
Ady Abrahamb1b9d412020-06-01 19:53:52 -0700930 }
931
Marin Shalamanove8a663d2020-11-24 17:48:00 +0100932 auto lowerBound = std::lower_bound(mKnownFrameRates.begin(), mKnownFrameRates.end(), frameRate,
Dominik Laskowski6eab42d2021-09-13 14:34:13 -0700933 isStrictlyLess);
Ady Abrahamb1b9d412020-06-01 19:53:52 -0700934
Dominik Laskowski6eab42d2021-09-13 14:34:13 -0700935 const auto distance1 = std::abs(frameRate.getValue() - lowerBound->getValue());
936 const auto distance2 = std::abs(frameRate.getValue() - std::prev(lowerBound)->getValue());
Ady Abrahamb1b9d412020-06-01 19:53:52 -0700937 return distance1 < distance2 ? *lowerBound : *std::prev(lowerBound);
938}
939
Ana Krulecb9afd792020-06-11 13:16:15 -0700940RefreshRateConfigs::KernelIdleTimerAction RefreshRateConfigs::getIdleTimerAction() const {
941 std::lock_guard lock(mLock);
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800942
943 const Fps deviceMinFps = mMinRefreshRateModeIt->second->getFps();
944 const DisplayModePtr& minByPolicy = getMinRefreshRateByPolicyLocked();
Ana Krulecb9afd792020-06-11 13:16:15 -0700945
946 // Kernel idle timer will set the refresh rate to the device min. If DisplayManager says that
947 // the min allowed refresh rate is higher than the device min, we do not want to enable the
948 // timer.
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800949 if (isStrictlyLess(deviceMinFps, minByPolicy->getFps())) {
950 return KernelIdleTimerAction::TurnOff;
Ana Krulecb9afd792020-06-11 13:16:15 -0700951 }
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800952
953 const DisplayModePtr& maxByPolicy = getMaxRefreshRateByPolicyLocked();
Ana Krulecb9afd792020-06-11 13:16:15 -0700954 if (minByPolicy == maxByPolicy) {
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800955 // Turn on the timer when the min of the primary range is below the device min.
956 if (const Policy* currentPolicy = getCurrentPolicyLocked();
957 isApproxLess(currentPolicy->primaryRange.min, deviceMinFps)) {
958 return KernelIdleTimerAction::TurnOn;
Ana Krulecb9afd792020-06-11 13:16:15 -0700959 }
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800960 return KernelIdleTimerAction::TurnOff;
Ana Krulecb9afd792020-06-11 13:16:15 -0700961 }
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800962
Ana Krulecb9afd792020-06-11 13:16:15 -0700963 // Turn on the timer in all other cases.
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800964 return KernelIdleTimerAction::TurnOn;
Ana Krulecb9afd792020-06-11 13:16:15 -0700965}
966
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800967int RefreshRateConfigs::getFrameRateDivisor(Fps displayRefreshRate, Fps layerFrameRate) {
Ady Abraham62f216c2020-10-13 19:07:23 -0700968 // This calculation needs to be in sync with the java code
969 // in DisplayManagerService.getDisplayInfoForFrameRateOverride
Marin Shalamanov15a0fc62021-08-16 18:20:21 +0200970
971 // The threshold must be smaller than 0.001 in order to differentiate
972 // between the fractional pairs (e.g. 59.94 and 60).
973 constexpr float kThreshold = 0.0009f;
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800974 const auto numPeriods = displayRefreshRate.getValue() / layerFrameRate.getValue();
Ady Abraham0bb6a472020-10-12 10:22:13 -0700975 const auto numPeriodsRounded = std::round(numPeriods);
976 if (std::abs(numPeriods - numPeriodsRounded) > kThreshold) {
Ady Abraham62a0be22020-12-08 16:54:10 -0800977 return 0;
Ady Abraham0bb6a472020-10-12 10:22:13 -0700978 }
979
Ady Abraham62f216c2020-10-13 19:07:23 -0700980 return static_cast<int>(numPeriodsRounded);
981}
982
Marin Shalamanov15a0fc62021-08-16 18:20:21 +0200983bool RefreshRateConfigs::isFractionalPairOrMultiple(Fps smaller, Fps bigger) {
Dominik Laskowski6eab42d2021-09-13 14:34:13 -0700984 if (isStrictlyLess(bigger, smaller)) {
Marin Shalamanov15a0fc62021-08-16 18:20:21 +0200985 return isFractionalPairOrMultiple(bigger, smaller);
986 }
987
988 const auto multiplier = std::round(bigger.getValue() / smaller.getValue());
989 constexpr float kCoef = 1000.f / 1001.f;
Dominik Laskowski6eab42d2021-09-13 14:34:13 -0700990 return isApproxEqual(bigger, Fps::fromValue(smaller.getValue() * multiplier / kCoef)) ||
991 isApproxEqual(bigger, Fps::fromValue(smaller.getValue() * multiplier * kCoef));
Marin Shalamanov15a0fc62021-08-16 18:20:21 +0200992}
993
Marin Shalamanovba421a82020-11-10 21:49:26 +0100994void RefreshRateConfigs::dump(std::string& result) const {
Dominik Laskowski0acc3842022-04-07 11:23:42 -0700995 using namespace std::string_literals;
996
Marin Shalamanovba421a82020-11-10 21:49:26 +0100997 std::lock_guard lock(mLock);
Marin Shalamanovba421a82020-11-10 21:49:26 +0100998
Dominik Laskowskif8734e02022-08-26 09:06:59 -0700999 const auto activeModeId = getActiveModeItLocked()->first;
Dominik Laskowski0acc3842022-04-07 11:23:42 -07001000 result += " activeModeId="s;
1001 result += std::to_string(activeModeId.value());
Marin Shalamanovba421a82020-11-10 21:49:26 +01001002
Dominik Laskowski0acc3842022-04-07 11:23:42 -07001003 result += "\n displayModes=\n"s;
Dominik Laskowskib0054a22022-03-03 09:03:06 -08001004 for (const auto& [id, mode] : mDisplayModes) {
Dominik Laskowski0acc3842022-04-07 11:23:42 -07001005 result += " "s;
1006 result += to_string(*mode);
1007 result += '\n';
Marin Shalamanovba421a82020-11-10 21:49:26 +01001008 }
1009
Dominik Laskowski0acc3842022-04-07 11:23:42 -07001010 base::StringAppendF(&result, " displayManagerPolicy=%s\n",
1011 mDisplayManagerPolicy.toString().c_str());
Dominik Laskowskib0054a22022-03-03 09:03:06 -08001012
Dominik Laskowski0acc3842022-04-07 11:23:42 -07001013 if (const Policy& currentPolicy = *getCurrentPolicyLocked();
1014 mOverridePolicy && currentPolicy != mDisplayManagerPolicy) {
1015 base::StringAppendF(&result, " overridePolicy=%s\n", currentPolicy.toString().c_str());
ramindani32cf0602022-03-02 02:30:29 +00001016 }
Dominik Laskowskib0054a22022-03-03 09:03:06 -08001017
Dominik Laskowski0acc3842022-04-07 11:23:42 -07001018 base::StringAppendF(&result, " supportsFrameRateOverrideByContent=%s\n",
1019 mSupportsFrameRateOverrideByContent ? "true" : "false");
1020
1021 result += " idleTimer="s;
Dominik Laskowskib0054a22022-03-03 09:03:06 -08001022 if (mIdleTimer) {
Dominik Laskowski0acc3842022-04-07 11:23:42 -07001023 result += mIdleTimer->dump();
Dominik Laskowskib0054a22022-03-03 09:03:06 -08001024 } else {
Dominik Laskowski0acc3842022-04-07 11:23:42 -07001025 result += "off"s;
Dominik Laskowskib0054a22022-03-03 09:03:06 -08001026 }
1027
Dominik Laskowski0acc3842022-04-07 11:23:42 -07001028 if (const auto controller = mConfig.kernelIdleTimerController) {
1029 base::StringAppendF(&result, " (kernel via %s)", ftl::enum_string(*controller).c_str());
1030 } else {
1031 result += " (platform)"s;
1032 }
1033
1034 result += '\n';
Marin Shalamanovba421a82020-11-10 21:49:26 +01001035}
1036
ramindani32cf0602022-03-02 02:30:29 +00001037std::chrono::milliseconds RefreshRateConfigs::getIdleTimerTimeout() {
1038 return mConfig.idleTimerTimeout;
1039}
1040
Ady Abraham2139f732019-11-13 18:56:40 -08001041} // namespace android::scheduler
Marin Shalamanovbed7fd32020-12-21 20:02:20 +01001042
1043// TODO(b/129481165): remove the #pragma below and fix conversion issues
Ady Abrahamdd5bfa92021-01-07 17:56:08 -08001044#pragma clang diagnostic pop // ignored "-Wextra"