blob: 80a7a42de3378b65dde4d6b1fa0384e5d066a334 [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 Laskowski530d6bd2022-10-10 16:55:54 -040026#include <deque>
Ady Abraham68636062022-11-16 17:07:25 -080027#include <map>
Dominik Laskowskif5d0ea52021-09-26 17:27:01 -070028
29#include <android-base/properties.h>
30#include <android-base/stringprintf.h>
31#include <ftl/enum.h>
Dominik Laskowskif8734e02022-08-26 09:06:59 -070032#include <ftl/fake_guard.h>
Dominik Laskowski36dced82022-09-02 09:24:00 -070033#include <ftl/match.h>
Ady Abraham8ca643a2022-10-18 18:26:47 -070034#include <ftl/unit.h>
Ady Abrahamccf63862023-01-19 11:44:01 -080035#include <gui/TraceUtils.h>
Ady Abraham68636062022-11-16 17:07:25 -080036#include <scheduler/FrameRateMode.h>
Dominik Laskowskif5d0ea52021-09-26 17:27:01 -070037#include <utils/Trace.h>
38
Ady Abraham4899ff82021-01-06 13:53:29 -080039#include "../SurfaceFlingerProperties.h"
Dominik Laskowskid82e0f02022-10-26 15:23:04 -040040#include "RefreshRateSelector.h"
Ady Abraham8a82ba62020-01-17 12:43:17 -080041
Ady Abraham5b8afb5a2020-03-06 14:57:26 -080042#undef LOG_TAG
Dominik Laskowskid82e0f02022-10-26 15:23:04 -040043#define LOG_TAG "RefreshRateSelector"
Ady Abraham5b8afb5a2020-03-06 14:57:26 -080044
Ady Abrahamb4b1e0a2019-11-20 18:25:35 -080045namespace android::scheduler {
Marin Shalamanov53fc11d2020-11-20 14:00:13 +010046namespace {
Dominik Laskowskif5d0ea52021-09-26 17:27:01 -070047
Dominik Laskowskib0054a22022-03-03 09:03:06 -080048struct RefreshRateScore {
Ady Abraham68636062022-11-16 17:07:25 -080049 FrameRateMode frameRateMode;
Ady Abrahamae2e3c72022-08-13 05:12:13 +000050 float overallScore;
51 struct {
Ady Abraham62f51d92022-08-24 22:20:22 +000052 float modeBelowThreshold;
53 float modeAboveThreshold;
54 } fixedRateBelowThresholdLayersScore;
Dominik Laskowskib0054a22022-03-03 09:03:06 -080055};
56
Dominik Laskowskid82e0f02022-10-26 15:23:04 -040057constexpr RefreshRateSelector::GlobalSignals kNoSignals;
Dominik Laskowskia8626ec2021-12-15 18:13:30 -080058
Dominik Laskowskid82e0f02022-10-26 15:23:04 -040059std::string formatLayerInfo(const RefreshRateSelector::LayerRequirement& layer, float weight) {
Dominik Laskowskib0054a22022-03-03 09:03:06 -080060 return base::StringPrintf("%s (type=%s, weight=%.2f, seamlessness=%s) %s", layer.name.c_str(),
Dominik Laskowskif5d0ea52021-09-26 17:27:01 -070061 ftl::enum_string(layer.vote).c_str(), weight,
62 ftl::enum_string(layer.seamlessness).c_str(),
Marin Shalamanove8a663d2020-11-24 17:48:00 +010063 to_string(layer.desiredRefreshRate).c_str());
Marin Shalamanov53fc11d2020-11-20 14:00:13 +010064}
Marin Shalamanoveadf2e72020-12-10 15:35:28 +010065
Marin Shalamanova7fe3042021-01-29 21:02:08 +010066std::vector<Fps> constructKnownFrameRates(const DisplayModes& modes) {
Dominik Laskowski6eab42d2021-09-13 14:34:13 -070067 std::vector<Fps> knownFrameRates = {24_Hz, 30_Hz, 45_Hz, 60_Hz, 72_Hz};
Marin Shalamanova7fe3042021-01-29 21:02:08 +010068 knownFrameRates.reserve(knownFrameRates.size() + modes.size());
Marin Shalamanoveadf2e72020-12-10 15:35:28 +010069
Dominik Laskowski6eab42d2021-09-13 14:34:13 -070070 // Add all supported refresh rates.
Dominik Laskowskib0054a22022-03-03 09:03:06 -080071 for (const auto& [id, mode] : modes) {
72 knownFrameRates.push_back(mode->getFps());
Marin Shalamanoveadf2e72020-12-10 15:35:28 +010073 }
74
Dominik Laskowski6eab42d2021-09-13 14:34:13 -070075 // Sort and remove duplicates.
76 std::sort(knownFrameRates.begin(), knownFrameRates.end(), isStrictlyLess);
Marin Shalamanoveadf2e72020-12-10 15:35:28 +010077 knownFrameRates.erase(std::unique(knownFrameRates.begin(), knownFrameRates.end(),
Dominik Laskowski6eab42d2021-09-13 14:34:13 -070078 isApproxEqual),
Marin Shalamanoveadf2e72020-12-10 15:35:28 +010079 knownFrameRates.end());
80 return knownFrameRates;
81}
82
Ady Abraham68636062022-11-16 17:07:25 -080083std::vector<DisplayModeIterator> sortByRefreshRate(const DisplayModes& modes) {
Dominik Laskowskib0054a22022-03-03 09:03:06 -080084 std::vector<DisplayModeIterator> sortedModes;
85 sortedModes.reserve(modes.size());
Dominik Laskowskib0054a22022-03-03 09:03:06 -080086 for (auto it = modes.begin(); it != modes.end(); ++it) {
Ady Abraham68636062022-11-16 17:07:25 -080087 sortedModes.push_back(it);
Dominik Laskowskib0054a22022-03-03 09:03:06 -080088 }
89
90 std::sort(sortedModes.begin(), sortedModes.end(), [](auto it1, auto it2) {
91 const auto& mode1 = it1->second;
92 const auto& mode2 = it2->second;
93
94 if (mode1->getVsyncPeriod() == mode2->getVsyncPeriod()) {
95 return mode1->getGroup() > mode2->getGroup();
96 }
97
98 return mode1->getVsyncPeriod() > mode2->getVsyncPeriod();
99 });
100
101 return sortedModes;
Marin Shalamanov46084422020-10-13 12:33:42 +0200102}
103
Ady Abraham68636062022-11-16 17:07:25 -0800104std::pair<unsigned, unsigned> divisorRange(Fps fps, FpsRange range,
105 RefreshRateSelector::Config::FrameRateOverride config) {
106 if (config != RefreshRateSelector::Config::FrameRateOverride::Enabled) {
107 return {1, 1};
108 }
109
110 using fps_approx_ops::operator/;
Ady Abraham08048ce2022-11-30 18:08:00 -0800111 // use signed type as `fps / range.max` might be 0
112 const auto start = std::max(1, static_cast<int>(fps / range.max) - 1);
Ady Abraham68636062022-11-16 17:07:25 -0800113 const auto end = fps /
114 std::max(range.min, RefreshRateSelector::kMinSupportedFrameRate,
115 fps_approx_ops::operator<);
116
117 return {start, end};
118}
119
Ady Abraham8ca643a2022-10-18 18:26:47 -0700120bool shouldEnableFrameRateOverride(const std::vector<DisplayModeIterator>& sortedModes) {
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800121 for (const auto it1 : sortedModes) {
122 const auto& mode1 = it1->second;
123 for (const auto it2 : sortedModes) {
124 const auto& mode2 = it2->second;
125
Dominik Laskowskid82e0f02022-10-26 15:23:04 -0400126 if (RefreshRateSelector::getFrameRateDivisor(mode1->getFps(), mode2->getFps()) >= 2) {
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800127 return true;
128 }
129 }
130 }
131 return false;
132}
133
Dominik Laskowskid82e0f02022-10-26 15:23:04 -0400134std::string toString(const RefreshRateSelector::PolicyVariant& policy) {
Dominik Laskowski36dced82022-09-02 09:24:00 -0700135 using namespace std::string_literals;
136
137 return ftl::match(
138 policy,
Dominik Laskowskid82e0f02022-10-26 15:23:04 -0400139 [](const RefreshRateSelector::DisplayManagerPolicy& policy) {
Dominik Laskowski36dced82022-09-02 09:24:00 -0700140 return "DisplayManagerPolicy"s + policy.toString();
141 },
Dominik Laskowskid82e0f02022-10-26 15:23:04 -0400142 [](const RefreshRateSelector::OverridePolicy& policy) {
Dominik Laskowski36dced82022-09-02 09:24:00 -0700143 return "OverridePolicy"s + policy.toString();
144 },
Dominik Laskowskid82e0f02022-10-26 15:23:04 -0400145 [](RefreshRateSelector::NoOverridePolicy) { return "NoOverridePolicy"s; });
Dominik Laskowski36dced82022-09-02 09:24:00 -0700146}
147
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800148} // namespace
149
Ady Abraham68636062022-11-16 17:07:25 -0800150auto RefreshRateSelector::createFrameRateModes(
Ady Abraham8d0b0a32023-08-16 11:02:00 -0700151 const Policy& policy, std::function<bool(const DisplayMode&)>&& filterModes,
152 const FpsRange& renderRange) const -> std::vector<FrameRateMode> {
Ady Abraham68636062022-11-16 17:07:25 -0800153 struct Key {
154 Fps fps;
155 int32_t group;
156 };
157
158 struct KeyLess {
159 bool operator()(const Key& a, const Key& b) const {
160 using namespace fps_approx_ops;
161 if (a.fps != b.fps) {
162 return a.fps < b.fps;
163 }
164
165 // For the same fps the order doesn't really matter, but we still
166 // want the behaviour of a strictly less operator.
167 // We use the group id as the secondary ordering for that.
168 return a.group < b.group;
169 }
170 };
171
172 std::map<Key, DisplayModeIterator, KeyLess> ratesMap;
173 for (auto it = mDisplayModes.begin(); it != mDisplayModes.end(); ++it) {
174 const auto& [id, mode] = *it;
175
176 if (!filterModes(*mode)) {
177 continue;
178 }
179 const auto [start, end] =
180 divisorRange(mode->getFps(), renderRange, mConfig.enableFrameRateOverride);
181 for (auto divisor = start; divisor <= end; divisor++) {
182 const auto fps = mode->getFps() / divisor;
183 using fps_approx_ops::operator<;
Ady Abrahamdc0b3a72023-01-04 16:58:27 -0800184 if (divisor > 1 && fps < kMinSupportedFrameRate) {
Ady Abraham68636062022-11-16 17:07:25 -0800185 break;
186 }
187
188 if (mConfig.enableFrameRateOverride == Config::FrameRateOverride::Enabled &&
189 !renderRange.includes(fps)) {
190 continue;
191 }
192
193 if (mConfig.enableFrameRateOverride ==
194 Config::FrameRateOverride::AppOverrideNativeRefreshRates &&
195 !isNativeRefreshRate(fps)) {
196 continue;
197 }
198
199 const auto [existingIter, emplaceHappened] =
200 ratesMap.try_emplace(Key{fps, mode->getGroup()}, it);
201 if (emplaceHappened) {
202 ALOGV("%s: including %s (%s)", __func__, to_string(fps).c_str(),
203 to_string(mode->getFps()).c_str());
204 } else {
Ady Abraham8d0b0a32023-08-16 11:02:00 -0700205 // If the primary physical range is a single rate, prefer to stay in that rate
206 // even if there is a lower physical refresh rate available. This would cause more
207 // cases to stay within the primary physical range
208 const Fps existingModeFps = existingIter->second->second->getFps();
209 const bool existingModeIsPrimaryRange = policy.primaryRangeIsSingleRate() &&
210 policy.primaryRanges.physical.includes(existingModeFps);
211 const bool newModeIsPrimaryRange = policy.primaryRangeIsSingleRate() &&
212 policy.primaryRanges.physical.includes(mode->getFps());
213 if (newModeIsPrimaryRange == existingModeIsPrimaryRange) {
214 // We might need to update the map as we found a lower refresh rate
215 if (isStrictlyLess(mode->getFps(), existingModeFps)) {
216 existingIter->second = it;
217 ALOGV("%s: changing %s (%s) as we found a lower physical rate", __func__,
218 to_string(fps).c_str(), to_string(mode->getFps()).c_str());
219 }
220 } else if (newModeIsPrimaryRange) {
Ady Abraham68636062022-11-16 17:07:25 -0800221 existingIter->second = it;
Ady Abraham8d0b0a32023-08-16 11:02:00 -0700222 ALOGV("%s: changing %s (%s) to stay in the primary range", __func__,
223 to_string(fps).c_str(), to_string(mode->getFps()).c_str());
Ady Abraham68636062022-11-16 17:07:25 -0800224 }
225 }
226 }
227 }
228
229 std::vector<FrameRateMode> frameRateModes;
230 frameRateModes.reserve(ratesMap.size());
231 for (const auto& [key, mode] : ratesMap) {
Ady Abrahamace3d052022-11-17 16:25:05 -0800232 frameRateModes.emplace_back(FrameRateMode{key.fps, ftl::as_non_null(mode->second)});
Ady Abraham68636062022-11-16 17:07:25 -0800233 }
234
235 // We always want that the lowest frame rate will be corresponding to the
236 // lowest mode for power saving.
237 const auto lowestRefreshRateIt =
238 std::min_element(frameRateModes.begin(), frameRateModes.end(),
239 [](const FrameRateMode& lhs, const FrameRateMode& rhs) {
240 return isStrictlyLess(lhs.modePtr->getFps(),
241 rhs.modePtr->getFps());
242 });
243 frameRateModes.erase(frameRateModes.begin(), lowestRefreshRateIt);
244
245 return frameRateModes;
246}
247
Dominik Laskowskid82e0f02022-10-26 15:23:04 -0400248struct RefreshRateSelector::RefreshRateScoreComparator {
ramindanid72ba162022-09-09 21:33:40 +0000249 bool operator()(const RefreshRateScore& lhs, const RefreshRateScore& rhs) const {
Ady Abraham68636062022-11-16 17:07:25 -0800250 const auto& [frameRateMode, overallScore, _] = lhs;
ramindanid72ba162022-09-09 21:33:40 +0000251
Ady Abraham68636062022-11-16 17:07:25 -0800252 std::string name = to_string(frameRateMode);
253
ramindanid72ba162022-09-09 21:33:40 +0000254 ALOGV("%s sorting scores %.2f", name.c_str(), overallScore);
ramindanid72ba162022-09-09 21:33:40 +0000255
Ady Abraham68636062022-11-16 17:07:25 -0800256 if (!ScoredFrameRate::scoresEqual(overallScore, rhs.overallScore)) {
ramindanid72ba162022-09-09 21:33:40 +0000257 return overallScore > rhs.overallScore;
258 }
259
ramindanid72ba162022-09-09 21:33:40 +0000260 if (refreshRateOrder == RefreshRateOrder::Descending) {
261 using fps_approx_ops::operator>;
Ady Abraham68636062022-11-16 17:07:25 -0800262 return frameRateMode.fps > rhs.frameRateMode.fps;
ramindanid72ba162022-09-09 21:33:40 +0000263 } else {
264 using fps_approx_ops::operator<;
Ady Abraham68636062022-11-16 17:07:25 -0800265 return frameRateMode.fps < rhs.frameRateMode.fps;
ramindanid72ba162022-09-09 21:33:40 +0000266 }
267 }
268
269 const RefreshRateOrder refreshRateOrder;
270};
271
Dominik Laskowskid82e0f02022-10-26 15:23:04 -0400272std::string RefreshRateSelector::Policy::toString() const {
Dominik Laskowski0acc3842022-04-07 11:23:42 -0700273 return base::StringPrintf("{defaultModeId=%d, allowGroupSwitching=%s"
Ady Abraham285f8c12022-10-11 17:12:14 -0700274 ", primaryRanges=%s, appRequestRanges=%s}",
Dominik Laskowski0acc3842022-04-07 11:23:42 -0700275 defaultMode.value(), allowGroupSwitching ? "true" : "false",
Ady Abraham285f8c12022-10-11 17:12:14 -0700276 to_string(primaryRanges).c_str(),
277 to_string(appRequestRanges).c_str());
Marin Shalamanov30b0b3c2020-10-13 19:15:06 +0200278}
279
Dominik Laskowskid82e0f02022-10-26 15:23:04 -0400280std::pair<nsecs_t, nsecs_t> RefreshRateSelector::getDisplayFrames(nsecs_t layerPeriod,
281 nsecs_t displayPeriod) const {
Ady Abraham62a0be22020-12-08 16:54:10 -0800282 auto [quotient, remainder] = std::div(layerPeriod, displayPeriod);
283 if (remainder <= MARGIN_FOR_PERIOD_CALCULATION ||
284 std::abs(remainder - displayPeriod) <= MARGIN_FOR_PERIOD_CALCULATION) {
285 quotient++;
286 remainder = 0;
Ady Abraham4ccdcb42020-02-11 17:34:34 -0800287 }
288
Ady Abraham62a0be22020-12-08 16:54:10 -0800289 return {quotient, remainder};
Ady Abraham4ccdcb42020-02-11 17:34:34 -0800290}
291
Dominik Laskowskid82e0f02022-10-26 15:23:04 -0400292float RefreshRateSelector::calculateNonExactMatchingLayerScoreLocked(const LayerRequirement& layer,
293 Fps refreshRate) const {
Marin Shalamanov15a0fc62021-08-16 18:20:21 +0200294 constexpr float kScoreForFractionalPairs = .8f;
295
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800296 const auto displayPeriod = refreshRate.getPeriodNsecs();
Ady Abraham62a0be22020-12-08 16:54:10 -0800297 const auto layerPeriod = layer.desiredRefreshRate.getPeriodNsecs();
298 if (layer.vote == LayerVoteType::ExplicitDefault) {
299 // Find the actual rate the layer will render, assuming
Marin Shalamanov15a0fc62021-08-16 18:20:21 +0200300 // that layerPeriod is the minimal period to render a frame.
301 // For example if layerPeriod is 20ms and displayPeriod is 16ms,
302 // then the actualLayerPeriod will be 32ms, because it is the
303 // smallest multiple of the display period which is >= layerPeriod.
Ady Abraham62a0be22020-12-08 16:54:10 -0800304 auto actualLayerPeriod = displayPeriod;
305 int multiplier = 1;
306 while (layerPeriod > actualLayerPeriod + MARGIN_FOR_PERIOD_CALCULATION) {
307 multiplier++;
308 actualLayerPeriod = displayPeriod * multiplier;
309 }
Marin Shalamanov15a0fc62021-08-16 18:20:21 +0200310
311 // Because of the threshold we used above it's possible that score is slightly
312 // above 1.
Ady Abraham62a0be22020-12-08 16:54:10 -0800313 return std::min(1.0f,
314 static_cast<float>(layerPeriod) / static_cast<float>(actualLayerPeriod));
315 }
316
317 if (layer.vote == LayerVoteType::ExplicitExactOrMultiple ||
318 layer.vote == LayerVoteType::Heuristic) {
Ady Abraham68636062022-11-16 17:07:25 -0800319 const float multiplier = refreshRate.getValue() / layer.desiredRefreshRate.getValue();
320
321 // We only want to score this layer as a fractional pair if the content is not
322 // significantly faster than the display rate, at it would cause a significant frame drop.
323 // It is more appropriate to choose a higher display rate even if
324 // a pull-down will be required.
Rachel Lee36426fa2023-03-08 20:13:52 -0800325 constexpr float kMinMultiplier = 0.75f;
Ady Abraham68636062022-11-16 17:07:25 -0800326 if (multiplier >= kMinMultiplier &&
327 isFractionalPairOrMultiple(refreshRate, layer.desiredRefreshRate)) {
Ady Abraham05243be2021-09-16 15:58:52 -0700328 return kScoreForFractionalPairs;
Marin Shalamanov15a0fc62021-08-16 18:20:21 +0200329 }
330
Ady Abraham62a0be22020-12-08 16:54:10 -0800331 // Calculate how many display vsyncs we need to present a single frame for this
332 // layer
333 const auto [displayFramesQuotient, displayFramesRemainder] =
334 getDisplayFrames(layerPeriod, displayPeriod);
335 static constexpr size_t MAX_FRAMES_TO_FIT = 10; // Stop calculating when score < 0.1
336 if (displayFramesRemainder == 0) {
337 // Layer desired refresh rate matches the display rate.
Ady Abraham05243be2021-09-16 15:58:52 -0700338 return 1.0f;
Ady Abraham62a0be22020-12-08 16:54:10 -0800339 }
340
341 if (displayFramesQuotient == 0) {
342 // Layer desired refresh rate is higher than the display rate.
343 return (static_cast<float>(layerPeriod) / static_cast<float>(displayPeriod)) *
344 (1.0f / (MAX_FRAMES_TO_FIT + 1));
345 }
346
347 // Layer desired refresh rate is lower than the display rate. Check how well it fits
348 // the cadence.
349 auto diff = std::abs(displayFramesRemainder - (displayPeriod - displayFramesRemainder));
350 int iter = 2;
351 while (diff > MARGIN_FOR_PERIOD_CALCULATION && iter < MAX_FRAMES_TO_FIT) {
352 diff = diff - (displayPeriod - diff);
353 iter++;
354 }
355
Ady Abraham05243be2021-09-16 15:58:52 -0700356 return (1.0f / iter);
357 }
358
359 return 0;
360}
361
Ady Abraham68636062022-11-16 17:07:25 -0800362float RefreshRateSelector::calculateDistanceScoreFromMax(Fps refreshRate) const {
363 const auto& maxFps = mAppRequestFrameRates.back().fps;
364 const float ratio = refreshRate.getValue() / maxFps.getValue();
ramindanid72ba162022-09-09 21:33:40 +0000365 // Use ratio^2 to get a lower score the more we get further from peak
366 return ratio * ratio;
367}
368
Dominik Laskowskid82e0f02022-10-26 15:23:04 -0400369float RefreshRateSelector::calculateLayerScoreLocked(const LayerRequirement& layer, Fps refreshRate,
370 bool isSeamlessSwitch) const {
Ady Abraham73c3df52023-01-12 18:09:31 -0800371 ATRACE_CALL();
Ady Abraham05243be2021-09-16 15:58:52 -0700372 // Slightly prefer seamless switches.
373 constexpr float kSeamedSwitchPenalty = 0.95f;
374 const float seamlessness = isSeamlessSwitch ? 1.0f : kSeamedSwitchPenalty;
375
376 // If the layer wants Max, give higher score to the higher refresh rate
377 if (layer.vote == LayerVoteType::Max) {
Ady Abraham68636062022-11-16 17:07:25 -0800378 return calculateDistanceScoreFromMax(refreshRate);
Ady Abraham62a0be22020-12-08 16:54:10 -0800379 }
380
Ady Abrahamdd5bfa92021-01-07 17:56:08 -0800381 if (layer.vote == LayerVoteType::ExplicitExact) {
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800382 const int divisor = getFrameRateDivisor(refreshRate, layer.desiredRefreshRate);
Ady Abraham68636062022-11-16 17:07:25 -0800383 if (supportsAppFrameRateOverrideByContent()) {
Ady Abrahamdd5bfa92021-01-07 17:56:08 -0800384 // Since we support frame rate override, allow refresh rates which are
385 // multiples of the layer's request, as those apps would be throttled
386 // down to run at the desired refresh rate.
Ady Abrahamcc315492022-02-17 17:06:39 -0800387 return divisor > 0;
Ady Abrahamdd5bfa92021-01-07 17:56:08 -0800388 }
389
Ady Abrahamcc315492022-02-17 17:06:39 -0800390 return divisor == 1;
Ady Abrahamdd5bfa92021-01-07 17:56:08 -0800391 }
392
Ady Abrahamcc315492022-02-17 17:06:39 -0800393 // If the layer frame rate is a divisor of the refresh rate it should score
Ady Abraham05243be2021-09-16 15:58:52 -0700394 // the highest score.
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800395 if (getFrameRateDivisor(refreshRate, layer.desiredRefreshRate) > 0) {
Ady Abraham05243be2021-09-16 15:58:52 -0700396 return 1.0f * seamlessness;
397 }
398
Ady Abrahamcc315492022-02-17 17:06:39 -0800399 // The layer frame rate is not a divisor of the refresh rate,
Ady Abraham05243be2021-09-16 15:58:52 -0700400 // there is a small penalty attached to the score to favor the frame rates
401 // the exactly matches the display refresh rate or a multiple.
Ady Abraham1c595502022-01-13 21:58:32 -0800402 constexpr float kNonExactMatchingPenalty = 0.95f;
Ady Abraham05243be2021-09-16 15:58:52 -0700403 return calculateNonExactMatchingLayerScoreLocked(layer, refreshRate) * seamlessness *
404 kNonExactMatchingPenalty;
Ady Abraham62a0be22020-12-08 16:54:10 -0800405}
406
Ady Abraham68636062022-11-16 17:07:25 -0800407auto RefreshRateSelector::getRankedFrameRates(const std::vector<LayerRequirement>& layers,
408 GlobalSignals signals) const -> RankedFrameRates {
Marin Shalamanov4c7831e2021-06-08 20:44:06 +0200409 std::lock_guard lock(mLock);
410
Ady Abraham68636062022-11-16 17:07:25 -0800411 if (mGetRankedFrameRatesCache &&
412 mGetRankedFrameRatesCache->arguments == std::make_pair(layers, signals)) {
413 return mGetRankedFrameRatesCache->result;
Marin Shalamanov4c7831e2021-06-08 20:44:06 +0200414 }
415
Ady Abraham68636062022-11-16 17:07:25 -0800416 const auto result = getRankedFrameRatesLocked(layers, signals);
417 mGetRankedFrameRatesCache = GetRankedFrameRatesCache{{layers, signals}, result};
Marin Shalamanov4c7831e2021-06-08 20:44:06 +0200418 return result;
419}
420
Ady Abraham68636062022-11-16 17:07:25 -0800421auto RefreshRateSelector::getRankedFrameRatesLocked(const std::vector<LayerRequirement>& layers,
422 GlobalSignals signals) const
423 -> RankedFrameRates {
Ady Abrahamae2e3c72022-08-13 05:12:13 +0000424 using namespace fps_approx_ops;
Ady Abraham8a82ba62020-01-17 12:43:17 -0800425 ATRACE_CALL();
Dominik Laskowskia8626ec2021-12-15 18:13:30 -0800426 ALOGV("%s: %zu layers", __func__, layers.size());
Ady Abrahamdfd62162020-06-10 16:11:56 -0700427
Ady Abrahamace3d052022-11-17 16:25:05 -0800428 const auto& activeMode = *getActiveModeLocked().modePtr;
ramindani38c84982022-08-29 18:02:57 +0000429
Ady Abraham68636062022-11-16 17:07:25 -0800430 // Keep the display at max frame rate for the duration of powering on the display.
ramindani38c84982022-08-29 18:02:57 +0000431 if (signals.powerOnImminent) {
432 ALOGV("Power On Imminent");
Ady Abrahamccf63862023-01-19 11:44:01 -0800433 const auto ranking = rankFrameRates(activeMode.getGroup(), RefreshRateOrder::Descending);
434 ATRACE_FORMAT_INSTANT("%s (Power On Imminent)",
435 to_string(ranking.front().frameRateMode.fps).c_str());
436 return {ranking, GlobalSignals{.powerOnImminent = true}};
ramindani38c84982022-08-29 18:02:57 +0000437 }
438
Ady Abraham8a82ba62020-01-17 12:43:17 -0800439 int noVoteLayers = 0;
440 int minVoteLayers = 0;
441 int maxVoteLayers = 0;
Ady Abraham71c437d2020-01-31 15:56:57 -0800442 int explicitDefaultVoteLayers = 0;
443 int explicitExactOrMultipleVoteLayers = 0;
Ady Abrahamdd5bfa92021-01-07 17:56:08 -0800444 int explicitExact = 0;
Marin Shalamanovae0b5352021-03-24 12:56:08 +0100445 int seamedFocusedLayers = 0;
Dominik Laskowskia8626ec2021-12-15 18:13:30 -0800446
Ady Abraham8a82ba62020-01-17 12:43:17 -0800447 for (const auto& layer : layers) {
Ady Abrahamdd5bfa92021-01-07 17:56:08 -0800448 switch (layer.vote) {
449 case LayerVoteType::NoVote:
450 noVoteLayers++;
451 break;
452 case LayerVoteType::Min:
453 minVoteLayers++;
454 break;
455 case LayerVoteType::Max:
456 maxVoteLayers++;
457 break;
458 case LayerVoteType::ExplicitDefault:
459 explicitDefaultVoteLayers++;
Ady Abrahamdd5bfa92021-01-07 17:56:08 -0800460 break;
461 case LayerVoteType::ExplicitExactOrMultiple:
462 explicitExactOrMultipleVoteLayers++;
Ady Abrahamdd5bfa92021-01-07 17:56:08 -0800463 break;
464 case LayerVoteType::ExplicitExact:
465 explicitExact++;
Ady Abrahamdd5bfa92021-01-07 17:56:08 -0800466 break;
467 case LayerVoteType::Heuristic:
468 break;
Ady Abraham6fb599b2020-03-05 13:48:22 -0800469 }
Marin Shalamanov46084422020-10-13 12:33:42 +0200470
Marin Shalamanovae0b5352021-03-24 12:56:08 +0100471 if (layer.seamlessness == Seamlessness::SeamedAndSeamless && layer.focused) {
472 seamedFocusedLayers++;
Marin Shalamanov46084422020-10-13 12:33:42 +0200473 }
Ady Abraham6fb599b2020-03-05 13:48:22 -0800474 }
475
Ady Abrahamdd5bfa92021-01-07 17:56:08 -0800476 const bool hasExplicitVoteLayers = explicitDefaultVoteLayers > 0 ||
477 explicitExactOrMultipleVoteLayers > 0 || explicitExact > 0;
Alec Mouri11232a22020-05-14 18:06:25 -0700478
Marin Shalamanov8cd8a992021-09-14 23:22:49 +0200479 const Policy* policy = getCurrentPolicyLocked();
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800480 const auto& defaultMode = mDisplayModes.get(policy->defaultMode)->get();
Dominik Laskowskif8734e02022-08-26 09:06:59 -0700481
Marin Shalamanov8cd8a992021-09-14 23:22:49 +0200482 // If the default mode group is different from the group of current mode,
483 // this means a layer requesting a seamed mode switch just disappeared and
484 // we should switch back to the default group.
485 // However if a seamed layer is still present we anchor around the group
486 // of the current mode, in order to prevent unnecessary seamed mode switches
487 // (e.g. when pausing a video playback).
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800488 const auto anchorGroup =
Dominik Laskowskif8734e02022-08-26 09:06:59 -0700489 seamedFocusedLayers > 0 ? activeMode.getGroup() : defaultMode->getGroup();
Marin Shalamanov8cd8a992021-09-14 23:22:49 +0200490
Steven Thomasf734df42020-04-13 21:09:28 -0700491 // Consider the touch event if there are no Explicit* layers. Otherwise wait until after we've
492 // selected a refresh rate to see if we should apply touch boost.
Dominik Laskowskia8626ec2021-12-15 18:13:30 -0800493 if (signals.touch && !hasExplicitVoteLayers) {
ramindanid72ba162022-09-09 21:33:40 +0000494 ALOGV("Touch Boost");
Ady Abrahamccf63862023-01-19 11:44:01 -0800495 const auto ranking = rankFrameRates(anchorGroup, RefreshRateOrder::Descending);
496 ATRACE_FORMAT_INSTANT("%s (Touch Boost)",
497 to_string(ranking.front().frameRateMode.fps).c_str());
498 return {ranking, GlobalSignals{.touch = true}};
Ady Abraham8a82ba62020-01-17 12:43:17 -0800499 }
500
Alec Mouri11232a22020-05-14 18:06:25 -0700501 // If the primary range consists of a single refresh rate then we can only
502 // move out the of range if layers explicitly request a different refresh
503 // rate.
Ady Abraham8d0b0a32023-08-16 11:02:00 -0700504 if (!signals.touch && signals.idle &&
505 !(policy->primaryRangeIsSingleRate() && hasExplicitVoteLayers)) {
ramindanid72ba162022-09-09 21:33:40 +0000506 ALOGV("Idle");
Ady Abrahamccf63862023-01-19 11:44:01 -0800507 const auto ranking = rankFrameRates(activeMode.getGroup(), RefreshRateOrder::Ascending);
508 ATRACE_FORMAT_INSTANT("%s (Idle)", to_string(ranking.front().frameRateMode.fps).c_str());
509 return {ranking, GlobalSignals{.idle = true}};
Steven Thomasbb374322020-04-28 22:47:16 -0700510 }
511
Steven Thomasdebafed2020-05-18 17:30:35 -0700512 if (layers.empty() || noVoteLayers == layers.size()) {
ramindanid72ba162022-09-09 21:33:40 +0000513 ALOGV("No layers with votes");
Ady Abrahamccf63862023-01-19 11:44:01 -0800514 const auto ranking = rankFrameRates(anchorGroup, RefreshRateOrder::Descending);
515 ATRACE_FORMAT_INSTANT("%s (No layers with votes)",
516 to_string(ranking.front().frameRateMode.fps).c_str());
517 return {ranking, kNoSignals};
Steven Thomasbb374322020-04-28 22:47:16 -0700518 }
519
Ady Abraham8a82ba62020-01-17 12:43:17 -0800520 // Only if all layers want Min we should return Min
521 if (noVoteLayers + minVoteLayers == layers.size()) {
ramindanid72ba162022-09-09 21:33:40 +0000522 ALOGV("All layers Min");
Ady Abrahamccf63862023-01-19 11:44:01 -0800523 const auto ranking = rankFrameRates(activeMode.getGroup(), RefreshRateOrder::Ascending);
524 ATRACE_FORMAT_INSTANT("%s (All layers Min)",
525 to_string(ranking.front().frameRateMode.fps).c_str());
526 return {ranking, kNoSignals};
Ady Abraham8a82ba62020-01-17 12:43:17 -0800527 }
528
Ady Abraham8a82ba62020-01-17 12:43:17 -0800529 // Find the best refresh rate based on score
Ady Abraham62a0be22020-12-08 16:54:10 -0800530 std::vector<RefreshRateScore> scores;
Ady Abraham68636062022-11-16 17:07:25 -0800531 scores.reserve(mAppRequestFrameRates.size());
Ady Abraham8a82ba62020-01-17 12:43:17 -0800532
Ady Abraham68636062022-11-16 17:07:25 -0800533 for (const FrameRateMode& it : mAppRequestFrameRates) {
534 scores.emplace_back(RefreshRateScore{it, 0.0f});
Ady Abraham8a82ba62020-01-17 12:43:17 -0800535 }
536
537 for (const auto& layer : layers) {
rnlee3bd610662021-06-23 16:27:57 -0700538 ALOGV("Calculating score for %s (%s, weight %.2f, desired %.2f) ", layer.name.c_str(),
Dominik Laskowskif5d0ea52021-09-26 17:27:01 -0700539 ftl::enum_string(layer.vote).c_str(), layer.weight,
rnlee3bd610662021-06-23 16:27:57 -0700540 layer.desiredRefreshRate.getValue());
Ady Abraham4ccdcb42020-02-11 17:34:34 -0800541 if (layer.vote == LayerVoteType::NoVote || layer.vote == LayerVoteType::Min) {
Ady Abraham8a82ba62020-01-17 12:43:17 -0800542 continue;
543 }
544
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800545 const auto weight = layer.weight;
Ady Abraham71c437d2020-01-31 15:56:57 -0800546
Ady Abraham68636062022-11-16 17:07:25 -0800547 for (auto& [mode, overallScore, fixedRateBelowThresholdLayersScore] : scores) {
548 const auto& [fps, modePtr] = mode;
549 const bool isSeamlessSwitch = modePtr->getGroup() == activeMode.getGroup();
Marin Shalamanov46084422020-10-13 12:33:42 +0200550
Marin Shalamanov53fc11d2020-11-20 14:00:13 +0100551 if (layer.seamlessness == Seamlessness::OnlySeamless && !isSeamlessSwitch) {
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100552 ALOGV("%s ignores %s to avoid non-seamless switch. Current mode = %s",
Ady Abraham68636062022-11-16 17:07:25 -0800553 formatLayerInfo(layer, weight).c_str(), to_string(*modePtr).c_str(),
Dominik Laskowskif8734e02022-08-26 09:06:59 -0700554 to_string(activeMode).c_str());
Marin Shalamanov46084422020-10-13 12:33:42 +0200555 continue;
556 }
557
Marin Shalamanov53fc11d2020-11-20 14:00:13 +0100558 if (layer.seamlessness == Seamlessness::SeamedAndSeamless && !isSeamlessSwitch &&
559 !layer.focused) {
560 ALOGV("%s ignores %s because it's not focused and the switch is going to be seamed."
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100561 " Current mode = %s",
Ady Abraham68636062022-11-16 17:07:25 -0800562 formatLayerInfo(layer, weight).c_str(), to_string(*modePtr).c_str(),
Dominik Laskowskif8734e02022-08-26 09:06:59 -0700563 to_string(activeMode).c_str());
Marin Shalamanov53fc11d2020-11-20 14:00:13 +0100564 continue;
565 }
566
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100567 // Layers with default seamlessness vote for the current mode group if
Marin Shalamanov53fc11d2020-11-20 14:00:13 +0100568 // there are layers with seamlessness=SeamedAndSeamless and for the default
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100569 // mode group otherwise. In second case, if the current mode group is different
Marin Shalamanov53fc11d2020-11-20 14:00:13 +0100570 // from the default, this means a layer with seamlessness=SeamedAndSeamless has just
571 // disappeared.
Ady Abraham68636062022-11-16 17:07:25 -0800572 const bool isInPolicyForDefault = modePtr->getGroup() == anchorGroup;
Marin Shalamanovae0b5352021-03-24 12:56:08 +0100573 if (layer.seamlessness == Seamlessness::Default && !isInPolicyForDefault) {
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100574 ALOGV("%s ignores %s. Current mode = %s", formatLayerInfo(layer, weight).c_str(),
Ady Abraham68636062022-11-16 17:07:25 -0800575 to_string(*modePtr).c_str(), to_string(activeMode).c_str());
Marin Shalamanov46084422020-10-13 12:33:42 +0200576 continue;
577 }
578
Ady Abraham8d0b0a32023-08-16 11:02:00 -0700579 const bool inPrimaryPhysicalRange =
580 policy->primaryRanges.physical.includes(modePtr->getFps());
581 const bool inPrimaryRenderRange = policy->primaryRanges.render.includes(fps);
582 if (((policy->primaryRangeIsSingleRate() && !inPrimaryPhysicalRange) ||
583 !inPrimaryRenderRange) &&
Ady Abrahamdd5bfa92021-01-07 17:56:08 -0800584 !(layer.focused &&
585 (layer.vote == LayerVoteType::ExplicitDefault ||
586 layer.vote == LayerVoteType::ExplicitExact))) {
Ady Abraham20c029c2020-07-06 12:58:05 -0700587 // Only focused layers with ExplicitDefault frame rate settings are allowed to score
Ady Abrahamaae5ed52020-06-26 09:32:43 -0700588 // refresh rates outside the primary range.
Steven Thomasf734df42020-04-13 21:09:28 -0700589 continue;
590 }
591
Ady Abraham68636062022-11-16 17:07:25 -0800592 const float layerScore = calculateLayerScoreLocked(layer, fps, isSeamlessSwitch);
Ady Abrahamae2e3c72022-08-13 05:12:13 +0000593 const float weightedLayerScore = weight * layerScore;
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800594
Ady Abraham13cfb362022-08-13 05:12:13 +0000595 // Layer with fixed source has a special consideration which depends on the
Ady Abrahamae2e3c72022-08-13 05:12:13 +0000596 // mConfig.frameRateMultipleThreshold. We don't want these layers to score
597 // refresh rates above the threshold, but we also don't want to favor the lower
598 // ones by having a greater number of layers scoring them. Instead, we calculate
599 // the score independently for these layers and later decide which
600 // refresh rates to add it. For example, desired 24 fps with 120 Hz threshold should not
601 // score 120 Hz, but desired 60 fps should contribute to the score.
602 const bool fixedSourceLayer = [](LayerVoteType vote) {
603 switch (vote) {
604 case LayerVoteType::ExplicitExactOrMultiple:
605 case LayerVoteType::Heuristic:
606 return true;
607 case LayerVoteType::NoVote:
608 case LayerVoteType::Min:
609 case LayerVoteType::Max:
610 case LayerVoteType::ExplicitDefault:
611 case LayerVoteType::ExplicitExact:
612 return false;
613 }
614 }(layer.vote);
Ady Abraham62f51d92022-08-24 22:20:22 +0000615 const bool layerBelowThreshold = mConfig.frameRateMultipleThreshold != 0 &&
Ady Abrahamae2e3c72022-08-13 05:12:13 +0000616 layer.desiredRefreshRate <
617 Fps::fromValue(mConfig.frameRateMultipleThreshold / 2);
Ady Abraham62f51d92022-08-24 22:20:22 +0000618 if (fixedSourceLayer && layerBelowThreshold) {
Ady Abraham13cfb362022-08-13 05:12:13 +0000619 const bool modeAboveThreshold =
Ady Abraham68636062022-11-16 17:07:25 -0800620 modePtr->getFps() >= Fps::fromValue(mConfig.frameRateMultipleThreshold);
Ady Abraham62f51d92022-08-24 22:20:22 +0000621 if (modeAboveThreshold) {
Ady Abraham68636062022-11-16 17:07:25 -0800622 ALOGV("%s gives %s (%s) fixed source (above threshold) score of %.4f",
623 formatLayerInfo(layer, weight).c_str(), to_string(fps).c_str(),
624 to_string(modePtr->getFps()).c_str(), layerScore);
Ady Abraham62f51d92022-08-24 22:20:22 +0000625 fixedRateBelowThresholdLayersScore.modeAboveThreshold += weightedLayerScore;
Ady Abrahamae2e3c72022-08-13 05:12:13 +0000626 } else {
Ady Abraham68636062022-11-16 17:07:25 -0800627 ALOGV("%s gives %s (%s) fixed source (below threshold) score of %.4f",
628 formatLayerInfo(layer, weight).c_str(), to_string(fps).c_str(),
629 to_string(modePtr->getFps()).c_str(), layerScore);
Ady Abraham62f51d92022-08-24 22:20:22 +0000630 fixedRateBelowThresholdLayersScore.modeBelowThreshold += weightedLayerScore;
Ady Abrahamae2e3c72022-08-13 05:12:13 +0000631 }
632 } else {
Ady Abraham68636062022-11-16 17:07:25 -0800633 ALOGV("%s gives %s (%s) score of %.4f", formatLayerInfo(layer, weight).c_str(),
634 to_string(fps).c_str(), to_string(modePtr->getFps()).c_str(), layerScore);
Ady Abrahamae2e3c72022-08-13 05:12:13 +0000635 overallScore += weightedLayerScore;
636 }
Ady Abraham8a82ba62020-01-17 12:43:17 -0800637 }
638 }
639
Ady Abrahamae2e3c72022-08-13 05:12:13 +0000640 // We want to find the best refresh rate without the fixed source layers,
Ady Abraham62f51d92022-08-24 22:20:22 +0000641 // so we could know whether we should add the modeAboveThreshold scores or not.
Ady Abrahamae2e3c72022-08-13 05:12:13 +0000642 // If the best refresh rate is already above the threshold, it means that
643 // some non-fixed source layers already scored it, so we can just add the score
644 // for all fixed source layers, even the ones that are above the threshold.
645 const bool maxScoreAboveThreshold = [&] {
646 if (mConfig.frameRateMultipleThreshold == 0 || scores.empty()) {
647 return false;
648 }
649
650 const auto maxScoreIt =
651 std::max_element(scores.begin(), scores.end(),
652 [](RefreshRateScore max, RefreshRateScore current) {
Ady Abraham68636062022-11-16 17:07:25 -0800653 return current.overallScore > max.overallScore;
Ady Abrahamae2e3c72022-08-13 05:12:13 +0000654 });
Ady Abraham68636062022-11-16 17:07:25 -0800655 ALOGV("%s (%s) is the best refresh rate without fixed source layers. It is %s the "
656 "threshold for "
Ady Abrahamae2e3c72022-08-13 05:12:13 +0000657 "refresh rate multiples",
Ady Abraham68636062022-11-16 17:07:25 -0800658 to_string(maxScoreIt->frameRateMode.fps).c_str(),
659 to_string(maxScoreIt->frameRateMode.modePtr->getFps()).c_str(),
Ady Abrahamae2e3c72022-08-13 05:12:13 +0000660 maxScoreAboveThreshold ? "above" : "below");
Ady Abraham68636062022-11-16 17:07:25 -0800661 return maxScoreIt->frameRateMode.modePtr->getFps() >=
Ady Abrahamae2e3c72022-08-13 05:12:13 +0000662 Fps::fromValue(mConfig.frameRateMultipleThreshold);
663 }();
664
665 // Now we can add the fixed rate layers score
Ady Abraham68636062022-11-16 17:07:25 -0800666 for (auto& [frameRateMode, overallScore, fixedRateBelowThresholdLayersScore] : scores) {
Ady Abraham62f51d92022-08-24 22:20:22 +0000667 overallScore += fixedRateBelowThresholdLayersScore.modeBelowThreshold;
Ady Abrahamae2e3c72022-08-13 05:12:13 +0000668 if (maxScoreAboveThreshold) {
Ady Abraham62f51d92022-08-24 22:20:22 +0000669 overallScore += fixedRateBelowThresholdLayersScore.modeAboveThreshold;
Ady Abrahamae2e3c72022-08-13 05:12:13 +0000670 }
Ady Abraham68636062022-11-16 17:07:25 -0800671 ALOGV("%s (%s) adjusted overallScore is %.4f", to_string(frameRateMode.fps).c_str(),
672 to_string(frameRateMode.modePtr->getFps()).c_str(), overallScore);
Ady Abrahamae2e3c72022-08-13 05:12:13 +0000673 }
674
675 // Now that we scored all the refresh rates we need to pick the one that got the highest
ramindanid72ba162022-09-09 21:33:40 +0000676 // overallScore. Sort the scores based on their overallScore in descending order of priority.
677 const RefreshRateOrder refreshRateOrder =
678 maxVoteLayers > 0 ? RefreshRateOrder::Descending : RefreshRateOrder::Ascending;
679 std::sort(scores.begin(), scores.end(),
680 RefreshRateScoreComparator{.refreshRateOrder = refreshRateOrder});
ramindanid72ba162022-09-09 21:33:40 +0000681
Ady Abraham68636062022-11-16 17:07:25 -0800682 FrameRateRanking ranking;
Dominik Laskowski530d6bd2022-10-10 16:55:54 -0400683 ranking.reserve(scores.size());
684
685 std::transform(scores.begin(), scores.end(), back_inserter(ranking),
ramindanid72ba162022-09-09 21:33:40 +0000686 [](const RefreshRateScore& score) {
Ady Abraham68636062022-11-16 17:07:25 -0800687 return ScoredFrameRate{score.frameRateMode, score.overallScore};
ramindanid72ba162022-09-09 21:33:40 +0000688 });
Ady Abraham34702102020-02-10 14:12:05 -0800689
Ady Abraham37d46922022-10-05 13:08:51 -0700690 const bool noLayerScore = std::all_of(scores.begin(), scores.end(), [](RefreshRateScore score) {
691 return score.overallScore == 0;
692 });
693
Ady Abraham8d0b0a32023-08-16 11:02:00 -0700694 if (policy->primaryRangeIsSingleRate()) {
Alec Mouri11232a22020-05-14 18:06:25 -0700695 // If we never scored any layers, then choose the rate from the primary
696 // range instead of picking a random score from the app range.
Ady Abraham37d46922022-10-05 13:08:51 -0700697 if (noLayerScore) {
ramindanid72ba162022-09-09 21:33:40 +0000698 ALOGV("Layers not scored");
Ady Abrahamccf63862023-01-19 11:44:01 -0800699 const auto descending = rankFrameRates(anchorGroup, RefreshRateOrder::Descending);
700 ATRACE_FORMAT_INSTANT("%s (Layers not scored)",
701 to_string(descending.front().frameRateMode.fps).c_str());
702 return {descending, kNoSignals};
Alec Mouri11232a22020-05-14 18:06:25 -0700703 } else {
Ady Abrahamccf63862023-01-19 11:44:01 -0800704 ATRACE_FORMAT_INSTANT("%s (primaryRangeIsSingleRate)",
705 to_string(ranking.front().frameRateMode.fps).c_str());
Dominik Laskowski530d6bd2022-10-10 16:55:54 -0400706 return {ranking, kNoSignals};
Alec Mouri11232a22020-05-14 18:06:25 -0700707 }
708 }
709
Steven Thomasf734df42020-04-13 21:09:28 -0700710 // Consider the touch event if there are no ExplicitDefault layers. ExplicitDefault are mostly
711 // interactive (as opposed to ExplicitExactOrMultiple) and therefore if those posted an explicit
712 // vote we should not change it if we get a touch event. Only apply touch boost if it will
713 // actually increase the refresh rate over the normal selection.
Ady Abraham5e4e9832021-06-14 13:40:56 -0700714 const bool touchBoostForExplicitExact = [&] {
Ady Abraham68636062022-11-16 17:07:25 -0800715 if (supportsAppFrameRateOverrideByContent()) {
Ady Abraham5e4e9832021-06-14 13:40:56 -0700716 // Enable touch boost if there are other layers besides exact
717 return explicitExact + noVoteLayers != layers.size();
718 } else {
719 // Enable touch boost if there are no exact layers
720 return explicitExact == 0;
721 }
722 }();
Dominik Laskowski6eab42d2021-09-13 14:34:13 -0700723
Ady Abraham68636062022-11-16 17:07:25 -0800724 const auto touchRefreshRates = rankFrameRates(anchorGroup, RefreshRateOrder::Descending);
Dominik Laskowski6eab42d2021-09-13 14:34:13 -0700725 using fps_approx_ops::operator<;
726
Dominik Laskowskia8626ec2021-12-15 18:13:30 -0800727 if (signals.touch && explicitDefaultVoteLayers == 0 && touchBoostForExplicitExact &&
Ady Abraham68636062022-11-16 17:07:25 -0800728 scores.front().frameRateMode.fps < touchRefreshRates.front().frameRateMode.fps) {
ramindanid72ba162022-09-09 21:33:40 +0000729 ALOGV("Touch Boost");
Ady Abrahamccf63862023-01-19 11:44:01 -0800730 ATRACE_FORMAT_INSTANT("%s (Touch Boost [late])",
731 to_string(touchRefreshRates.front().frameRateMode.fps).c_str());
ramindanid72ba162022-09-09 21:33:40 +0000732 return {touchRefreshRates, GlobalSignals{.touch = true}};
Steven Thomasf734df42020-04-13 21:09:28 -0700733 }
734
Ady Abraham37d46922022-10-05 13:08:51 -0700735 // If we never scored any layers, and we don't favor high refresh rates, prefer to stay with the
736 // current config
737 if (noLayerScore && refreshRateOrder == RefreshRateOrder::Ascending) {
Ady Abrahamccf63862023-01-19 11:44:01 -0800738 const auto ascendingWithPreferred =
739 rankFrameRates(anchorGroup, RefreshRateOrder::Ascending, activeMode.getId());
740 ATRACE_FORMAT_INSTANT("%s (preferredDisplayMode)",
741 to_string(ascendingWithPreferred.front().frameRateMode.fps).c_str());
742 return {ascendingWithPreferred, kNoSignals};
Ady Abraham37d46922022-10-05 13:08:51 -0700743 }
744
Ady Abrahamccf63862023-01-19 11:44:01 -0800745 ATRACE_FORMAT_INSTANT("%s (scored))", to_string(ranking.front().frameRateMode.fps).c_str());
Dominik Laskowski530d6bd2022-10-10 16:55:54 -0400746 return {ranking, kNoSignals};
Ady Abraham34702102020-02-10 14:12:05 -0800747}
748
Dominik Laskowskid82e0f02022-10-26 15:23:04 -0400749using LayerRequirementPtrs = std::vector<const RefreshRateSelector::LayerRequirement*>;
750using PerUidLayerRequirements = std::unordered_map<uid_t, LayerRequirementPtrs>;
751
752PerUidLayerRequirements groupLayersByUid(
753 const std::vector<RefreshRateSelector::LayerRequirement>& layers) {
754 PerUidLayerRequirements layersByUid;
Ady Abraham62a0be22020-12-08 16:54:10 -0800755 for (const auto& layer : layers) {
Dominik Laskowskid82e0f02022-10-26 15:23:04 -0400756 const auto it = layersByUid.emplace(layer.ownerUid, LayerRequirementPtrs()).first;
757 auto& layersWithSameUid = it->second;
Ady Abraham62a0be22020-12-08 16:54:10 -0800758 layersWithSameUid.push_back(&layer);
759 }
760
761 // Remove uids that can't have a frame rate override
Dominik Laskowskid82e0f02022-10-26 15:23:04 -0400762 for (auto it = layersByUid.begin(); it != layersByUid.end();) {
763 const auto& layersWithSameUid = it->second;
Ady Abraham62a0be22020-12-08 16:54:10 -0800764 bool skipUid = false;
765 for (const auto& layer : layersWithSameUid) {
Dominik Laskowskid82e0f02022-10-26 15:23:04 -0400766 using LayerVoteType = RefreshRateSelector::LayerVoteType;
767
768 if (layer->vote == LayerVoteType::Max || layer->vote == LayerVoteType::Heuristic) {
Ady Abraham62a0be22020-12-08 16:54:10 -0800769 skipUid = true;
770 break;
771 }
772 }
773 if (skipUid) {
Dominik Laskowskid82e0f02022-10-26 15:23:04 -0400774 it = layersByUid.erase(it);
Ady Abraham62a0be22020-12-08 16:54:10 -0800775 } else {
Dominik Laskowskid82e0f02022-10-26 15:23:04 -0400776 ++it;
Ady Abraham62a0be22020-12-08 16:54:10 -0800777 }
778 }
779
780 return layersByUid;
781}
782
Dominik Laskowskid82e0f02022-10-26 15:23:04 -0400783auto RefreshRateSelector::getFrameRateOverrides(const std::vector<LayerRequirement>& layers,
784 Fps displayRefreshRate,
785 GlobalSignals globalSignals) const
786 -> UidToFrameRateOverride {
Ady Abraham62a0be22020-12-08 16:54:10 -0800787 ATRACE_CALL();
Ady Abraham68636062022-11-16 17:07:25 -0800788 if (mConfig.enableFrameRateOverride == Config::FrameRateOverride::Disabled) {
789 return {};
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800790 }
791
Ady Abraham68636062022-11-16 17:07:25 -0800792 ALOGV("%s: %zu layers", __func__, layers.size());
793 std::lock_guard lock(mLock);
794
Ady Abraham8ca643a2022-10-18 18:26:47 -0700795 const auto* policyPtr = getCurrentPolicyLocked();
796 // We don't want to run lower than 30fps
797 const Fps minFrameRate = std::max(policyPtr->appRequestRanges.render.min, 30_Hz, isApproxLess);
798
799 using fps_approx_ops::operator/;
800 const unsigned numMultiples = displayRefreshRate / minFrameRate;
801
802 std::vector<std::pair<Fps, float>> scoredFrameRates;
803 scoredFrameRates.reserve(numMultiples);
804
805 for (unsigned n = numMultiples; n > 0; n--) {
806 const Fps divisor = displayRefreshRate / n;
807 if (mConfig.enableFrameRateOverride ==
Ady Abraham68636062022-11-16 17:07:25 -0800808 Config::FrameRateOverride::AppOverrideNativeRefreshRates &&
809 !isNativeRefreshRate(divisor)) {
Ady Abraham8ca643a2022-10-18 18:26:47 -0700810 continue;
811 }
812
813 if (policyPtr->appRequestRanges.render.includes(divisor)) {
814 ALOGV("%s: adding %s as a potential frame rate", __func__, to_string(divisor).c_str());
815 scoredFrameRates.emplace_back(divisor, 0);
816 }
817 }
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800818
Dominik Laskowskid82e0f02022-10-26 15:23:04 -0400819 const auto layersByUid = groupLayersByUid(layers);
Ady Abraham62a0be22020-12-08 16:54:10 -0800820 UidToFrameRateOverride frameRateOverrides;
821 for (const auto& [uid, layersWithSameUid] : layersByUid) {
Ady Abrahamdd5bfa92021-01-07 17:56:08 -0800822 // Layers with ExplicitExactOrMultiple expect touch boost
823 const bool hasExplicitExactOrMultiple =
824 std::any_of(layersWithSameUid.cbegin(), layersWithSameUid.cend(),
825 [](const auto& layer) {
826 return layer->vote == LayerVoteType::ExplicitExactOrMultiple;
827 });
828
Dominik Laskowski6eab42d2021-09-13 14:34:13 -0700829 if (globalSignals.touch && hasExplicitExactOrMultiple) {
Ady Abrahamdd5bfa92021-01-07 17:56:08 -0800830 continue;
831 }
832
Ady Abraham8ca643a2022-10-18 18:26:47 -0700833 for (auto& [_, score] : scoredFrameRates) {
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800834 score = 0;
Ady Abraham62a0be22020-12-08 16:54:10 -0800835 }
836
837 for (const auto& layer : layersWithSameUid) {
838 if (layer->vote == LayerVoteType::NoVote || layer->vote == LayerVoteType::Min) {
839 continue;
840 }
841
842 LOG_ALWAYS_FATAL_IF(layer->vote != LayerVoteType::ExplicitDefault &&
Ady Abrahamdd5bfa92021-01-07 17:56:08 -0800843 layer->vote != LayerVoteType::ExplicitExactOrMultiple &&
844 layer->vote != LayerVoteType::ExplicitExact);
Ady Abraham8ca643a2022-10-18 18:26:47 -0700845 for (auto& [fps, score] : scoredFrameRates) {
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800846 constexpr bool isSeamlessSwitch = true;
Ady Abraham8ca643a2022-10-18 18:26:47 -0700847 const auto layerScore = calculateLayerScoreLocked(*layer, fps, isSeamlessSwitch);
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800848 score += layer->weight * layerScore;
Ady Abraham62a0be22020-12-08 16:54:10 -0800849 }
850 }
851
Ady Abraham62a0be22020-12-08 16:54:10 -0800852 // If we never scored any layers, we don't have a preferred frame rate
Ady Abraham8ca643a2022-10-18 18:26:47 -0700853 if (std::all_of(scoredFrameRates.begin(), scoredFrameRates.end(),
854 [](const auto& scoredFrameRate) {
855 const auto [_, score] = scoredFrameRate;
856 return score == 0;
857 })) {
Ady Abraham62a0be22020-12-08 16:54:10 -0800858 continue;
859 }
860
ramindanid72ba162022-09-09 21:33:40 +0000861 // Now that we scored all the refresh rates we need to pick the lowest refresh rate
862 // that got the highest score.
Ady Abraham8ca643a2022-10-18 18:26:47 -0700863 const auto [overrideFps, _] =
864 *std::max_element(scoredFrameRates.begin(), scoredFrameRates.end(),
865 [](const auto& lhsPair, const auto& rhsPair) {
866 const float lhs = lhsPair.second;
867 const float rhs = rhsPair.second;
Ady Abraham68636062022-11-16 17:07:25 -0800868 return lhs < rhs && !ScoredFrameRate::scoresEqual(lhs, rhs);
Ady Abraham8ca643a2022-10-18 18:26:47 -0700869 });
870 ALOGV("%s: overriding to %s for uid=%d", __func__, to_string(overrideFps).c_str(), uid);
871 frameRateOverrides.emplace(uid, overrideFps);
Ady Abraham62a0be22020-12-08 16:54:10 -0800872 }
873
874 return frameRateOverrides;
875}
876
Ady Abraham0aa373a2022-11-22 13:56:50 -0800877ftl::Optional<FrameRateMode> RefreshRateSelector::onKernelTimerChanged(
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800878 std::optional<DisplayModeId> desiredActiveModeId, bool timerExpired) const {
Ady Abraham2139f732019-11-13 18:56:40 -0800879 std::lock_guard lock(mLock);
Marin Shalamanoveadf2e72020-12-10 15:35:28 +0100880
Ady Abraham0aa373a2022-11-22 13:56:50 -0800881 const auto current = [&]() REQUIRES(mLock) -> FrameRateMode {
882 if (desiredActiveModeId) {
883 const auto& modePtr = mDisplayModes.get(*desiredActiveModeId)->get();
884 return FrameRateMode{modePtr->getFps(), ftl::as_non_null(modePtr)};
885 }
886
887 return getActiveModeLocked();
888 }();
Marin Shalamanoveadf2e72020-12-10 15:35:28 +0100889
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800890 const DisplayModePtr& min = mMinRefreshRateModeIt->second;
Ady Abraham0aa373a2022-11-22 13:56:50 -0800891 if (current.modePtr->getId() == min->getId()) {
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800892 return {};
Marin Shalamanoveadf2e72020-12-10 15:35:28 +0100893 }
894
Ady Abraham0aa373a2022-11-22 13:56:50 -0800895 return timerExpired ? FrameRateMode{min->getFps(), ftl::as_non_null(min)} : current;
Steven Thomasf734df42020-04-13 21:09:28 -0700896}
897
Dominik Laskowskid82e0f02022-10-26 15:23:04 -0400898const DisplayModePtr& RefreshRateSelector::getMinRefreshRateByPolicyLocked() const {
Ady Abrahamace3d052022-11-17 16:25:05 -0800899 const auto& activeMode = *getActiveModeLocked().modePtr;
Dominik Laskowskif8734e02022-08-26 09:06:59 -0700900
Ady Abraham68636062022-11-16 17:07:25 -0800901 for (const FrameRateMode& mode : mPrimaryFrameRates) {
902 if (activeMode.getGroup() == mode.modePtr->getGroup()) {
Ady Abrahamace3d052022-11-17 16:25:05 -0800903 return mode.modePtr.get();
Marin Shalamanov46084422020-10-13 12:33:42 +0200904 }
905 }
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800906
Dominik Laskowskif8734e02022-08-26 09:06:59 -0700907 ALOGE("Can't find min refresh rate by policy with the same mode group as the current mode %s",
908 to_string(activeMode).c_str());
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800909
910 // Default to the lowest refresh rate.
Ady Abrahamace3d052022-11-17 16:25:05 -0800911 return mPrimaryFrameRates.front().modePtr.get();
Ady Abraham2139f732019-11-13 18:56:40 -0800912}
913
Dominik Laskowskid82e0f02022-10-26 15:23:04 -0400914const DisplayModePtr& RefreshRateSelector::getMaxRefreshRateByPolicyLocked(int anchorGroup) const {
Ady Abrahamace3d052022-11-17 16:25:05 -0800915 const ftl::NonNull<DisplayModePtr>* maxByAnchor = &mPrimaryFrameRates.back().modePtr;
916 const ftl::NonNull<DisplayModePtr>* max = &mPrimaryFrameRates.back().modePtr;
Ady Abraham68636062022-11-16 17:07:25 -0800917
918 bool maxByAnchorFound = false;
919 for (auto it = mPrimaryFrameRates.rbegin(); it != mPrimaryFrameRates.rend(); ++it) {
920 using namespace fps_approx_ops;
921 if (it->modePtr->getFps() > (*max)->getFps()) {
922 max = &it->modePtr;
Marin Shalamanov46084422020-10-13 12:33:42 +0200923 }
Ady Abraham68636062022-11-16 17:07:25 -0800924
925 if (anchorGroup == it->modePtr->getGroup() &&
926 it->modePtr->getFps() >= (*maxByAnchor)->getFps()) {
927 maxByAnchorFound = true;
928 maxByAnchor = &it->modePtr;
929 }
930 }
931
932 if (maxByAnchorFound) {
Ady Abrahamace3d052022-11-17 16:25:05 -0800933 return maxByAnchor->get();
Marin Shalamanov46084422020-10-13 12:33:42 +0200934 }
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800935
ramindanid72ba162022-09-09 21:33:40 +0000936 ALOGE("Can't find max refresh rate by policy with the same group %d", anchorGroup);
Dominik Laskowskib0054a22022-03-03 09:03:06 -0800937
938 // Default to the highest refresh rate.
Ady Abrahamace3d052022-11-17 16:25:05 -0800939 return max->get();
Ady Abraham2139f732019-11-13 18:56:40 -0800940}
941
Ady Abraham68636062022-11-16 17:07:25 -0800942auto RefreshRateSelector::rankFrameRates(std::optional<int> anchorGroupOpt,
943 RefreshRateOrder refreshRateOrder,
944 std::optional<DisplayModeId> preferredDisplayModeOpt) const
945 -> FrameRateRanking {
Ady Abrahama5992df2023-01-27 21:10:57 -0800946 using fps_approx_ops::operator<;
Ady Abraham68636062022-11-16 17:07:25 -0800947 const char* const whence = __func__;
Ady Abrahama5992df2023-01-27 21:10:57 -0800948
949 // find the highest frame rate for each display mode
950 ftl::SmallMap<DisplayModeId, Fps, 8> maxRenderRateForMode;
951 const bool ascending = (refreshRateOrder == RefreshRateOrder::Ascending);
952 if (ascending) {
953 // TODO(b/266481656): Once this bug is fixed, we can remove this workaround and actually
954 // use a lower frame rate when we want Ascending frame rates.
955 for (const auto& frameRateMode : mPrimaryFrameRates) {
956 if (anchorGroupOpt && frameRateMode.modePtr->getGroup() != anchorGroupOpt) {
957 continue;
958 }
959
960 const auto [iter, _] = maxRenderRateForMode.try_emplace(frameRateMode.modePtr->getId(),
961 frameRateMode.fps);
962 if (iter->second < frameRateMode.fps) {
963 iter->second = frameRateMode.fps;
964 }
965 }
966 }
967
Ady Abraham68636062022-11-16 17:07:25 -0800968 std::deque<ScoredFrameRate> ranking;
969 const auto rankFrameRate = [&](const FrameRateMode& frameRateMode) REQUIRES(mLock) {
970 const auto& modePtr = frameRateMode.modePtr;
971 if (anchorGroupOpt && modePtr->getGroup() != anchorGroupOpt) {
Ady Abraham37d46922022-10-05 13:08:51 -0700972 return;
ramindanid72ba162022-09-09 21:33:40 +0000973 }
Ady Abraham37d46922022-10-05 13:08:51 -0700974
Ady Abraham3f965922023-01-23 17:18:29 -0800975 const bool ascending = (refreshRateOrder == RefreshRateOrder::Ascending);
ramindanif7075202023-03-10 00:24:34 +0000976 const auto id = modePtr->getId();
Ady Abrahama5992df2023-01-27 21:10:57 -0800977 if (ascending && frameRateMode.fps < *maxRenderRateForMode.get(id)) {
Ady Abraham3f965922023-01-23 17:18:29 -0800978 // TODO(b/266481656): Once this bug is fixed, we can remove this workaround and actually
979 // use a lower frame rate when we want Ascending frame rates.
980 return;
981 }
982
Ady Abraham68636062022-11-16 17:07:25 -0800983 float score = calculateDistanceScoreFromMax(frameRateMode.fps);
Ady Abraham3f965922023-01-23 17:18:29 -0800984
985 if (ascending) {
Ady Abraham37d46922022-10-05 13:08:51 -0700986 score = 1.0f / score;
987 }
ramindanif7075202023-03-10 00:24:34 +0000988
989 constexpr float kScore = std::numeric_limits<float>::max();
Ady Abraham37d46922022-10-05 13:08:51 -0700990 if (preferredDisplayModeOpt) {
Ady Abraham68636062022-11-16 17:07:25 -0800991 if (*preferredDisplayModeOpt == modePtr->getId()) {
Ady Abraham68636062022-11-16 17:07:25 -0800992 ranking.emplace_front(ScoredFrameRate{frameRateMode, kScore});
Ady Abraham37d46922022-10-05 13:08:51 -0700993 return;
994 }
995 constexpr float kNonPreferredModePenalty = 0.95f;
996 score *= kNonPreferredModePenalty;
ramindanif7075202023-03-10 00:24:34 +0000997 } else if (ascending && id == getMinRefreshRateByPolicyLocked()->getId()) {
998 // TODO(b/266481656): Once this bug is fixed, we can remove this workaround
999 // and actually use a lower frame rate when we want Ascending frame rates.
1000 ranking.emplace_front(ScoredFrameRate{frameRateMode, kScore});
1001 return;
Ady Abraham37d46922022-10-05 13:08:51 -07001002 }
Ady Abraham3f965922023-01-23 17:18:29 -08001003
Ady Abraham68636062022-11-16 17:07:25 -08001004 ALOGV("%s(%s) %s (%s) scored %.2f", whence, ftl::enum_string(refreshRateOrder).c_str(),
1005 to_string(frameRateMode.fps).c_str(), to_string(modePtr->getFps()).c_str(), score);
1006 ranking.emplace_back(ScoredFrameRate{frameRateMode, score});
ramindanid72ba162022-09-09 21:33:40 +00001007 };
1008
1009 if (refreshRateOrder == RefreshRateOrder::Ascending) {
Ady Abraham68636062022-11-16 17:07:25 -08001010 std::for_each(mPrimaryFrameRates.begin(), mPrimaryFrameRates.end(), rankFrameRate);
ramindanid72ba162022-09-09 21:33:40 +00001011 } else {
Ady Abraham68636062022-11-16 17:07:25 -08001012 std::for_each(mPrimaryFrameRates.rbegin(), mPrimaryFrameRates.rend(), rankFrameRate);
ramindanid72ba162022-09-09 21:33:40 +00001013 }
1014
Dominik Laskowski530d6bd2022-10-10 16:55:54 -04001015 if (!ranking.empty() || !anchorGroupOpt) {
1016 return {ranking.begin(), ranking.end()};
ramindanid72ba162022-09-09 21:33:40 +00001017 }
1018
1019 ALOGW("Can't find %s refresh rate by policy with the same mode group"
1020 " as the mode group %d",
1021 refreshRateOrder == RefreshRateOrder::Ascending ? "min" : "max", anchorGroupOpt.value());
1022
Dominik Laskowski530d6bd2022-10-10 16:55:54 -04001023 constexpr std::optional<int> kNoAnchorGroup = std::nullopt;
Ady Abraham68636062022-11-16 17:07:25 -08001024 return rankFrameRates(kNoAnchorGroup, refreshRateOrder, preferredDisplayModeOpt);
ramindanid72ba162022-09-09 21:33:40 +00001025}
1026
Ady Abrahamace3d052022-11-17 16:25:05 -08001027FrameRateMode RefreshRateSelector::getActiveMode() const {
Ady Abraham2139f732019-11-13 18:56:40 -08001028 std::lock_guard lock(mLock);
Ady Abrahamace3d052022-11-17 16:25:05 -08001029 return getActiveModeLocked();
Dominik Laskowskif8734e02022-08-26 09:06:59 -07001030}
1031
Ady Abrahamace3d052022-11-17 16:25:05 -08001032const FrameRateMode& RefreshRateSelector::getActiveModeLocked() const {
1033 return *mActiveModeOpt;
Dominik Laskowskif8734e02022-08-26 09:06:59 -07001034}
1035
Ady Abrahamace3d052022-11-17 16:25:05 -08001036void RefreshRateSelector::setActiveMode(DisplayModeId modeId, Fps renderFrameRate) {
Ady Abraham2139f732019-11-13 18:56:40 -08001037 std::lock_guard lock(mLock);
Marin Shalamanov4c7831e2021-06-08 20:44:06 +02001038
Ady Abraham68636062022-11-16 17:07:25 -08001039 // Invalidate the cached invocation to getRankedFrameRates. This forces
1040 // the refresh rate to be recomputed on the next call to getRankedFrameRates.
1041 mGetRankedFrameRatesCache.reset();
Marin Shalamanov4c7831e2021-06-08 20:44:06 +02001042
Ady Abrahamace3d052022-11-17 16:25:05 -08001043 const auto activeModeOpt = mDisplayModes.get(modeId);
1044 LOG_ALWAYS_FATAL_IF(!activeModeOpt);
1045
1046 mActiveModeOpt.emplace(FrameRateMode{renderFrameRate, ftl::as_non_null(activeModeOpt->get())});
Ady Abrahamb4b1e0a2019-11-20 18:25:35 -08001047}
1048
Dominik Laskowskid82e0f02022-10-26 15:23:04 -04001049RefreshRateSelector::RefreshRateSelector(DisplayModes modes, DisplayModeId activeModeId,
1050 Config config)
rnlee3bd610662021-06-23 16:27:57 -07001051 : mKnownFrameRates(constructKnownFrameRates(modes)), mConfig(config) {
Ady Abraham9a2ea342021-09-03 17:32:34 -07001052 initializeIdleTimer();
Dominik Laskowskif8734e02022-08-26 09:06:59 -07001053 FTL_FAKE_GUARD(kMainThreadContext, updateDisplayModes(std::move(modes), activeModeId));
Marin Shalamanoveadf2e72020-12-10 15:35:28 +01001054}
1055
Dominik Laskowskid82e0f02022-10-26 15:23:04 -04001056void RefreshRateSelector::initializeIdleTimer() {
ramindani32cf0602022-03-02 02:30:29 +00001057 if (mConfig.idleTimerTimeout > 0ms) {
Ady Abraham9a2ea342021-09-03 17:32:34 -07001058 mIdleTimer.emplace(
ramindani32cf0602022-03-02 02:30:29 +00001059 "IdleTimer", mConfig.idleTimerTimeout,
Dominik Laskowski83bd7712022-01-07 14:30:53 -08001060 [this] {
1061 std::scoped_lock lock(mIdleTimerCallbacksMutex);
1062 if (const auto callbacks = getIdleTimerCallbacks()) {
1063 callbacks->onReset();
1064 }
Ady Abraham9a2ea342021-09-03 17:32:34 -07001065 },
Dominik Laskowski83bd7712022-01-07 14:30:53 -08001066 [this] {
1067 std::scoped_lock lock(mIdleTimerCallbacksMutex);
1068 if (const auto callbacks = getIdleTimerCallbacks()) {
1069 callbacks->onExpired();
1070 }
Ady Abraham9a2ea342021-09-03 17:32:34 -07001071 });
Ady Abraham9a2ea342021-09-03 17:32:34 -07001072 }
1073}
1074
Dominik Laskowskid82e0f02022-10-26 15:23:04 -04001075void RefreshRateSelector::updateDisplayModes(DisplayModes modes, DisplayModeId activeModeId) {
Marin Shalamanoveadf2e72020-12-10 15:35:28 +01001076 std::lock_guard lock(mLock);
Marin Shalamanov4c7831e2021-06-08 20:44:06 +02001077
Ady Abraham68636062022-11-16 17:07:25 -08001078 // Invalidate the cached invocation to getRankedFrameRates. This forces
1079 // the refresh rate to be recomputed on the next call to getRankedFrameRates.
1080 mGetRankedFrameRatesCache.reset();
Marin Shalamanov4c7831e2021-06-08 20:44:06 +02001081
Dominik Laskowskib0054a22022-03-03 09:03:06 -08001082 mDisplayModes = std::move(modes);
Ady Abrahamace3d052022-11-17 16:25:05 -08001083 const auto activeModeOpt = mDisplayModes.get(activeModeId);
1084 LOG_ALWAYS_FATAL_IF(!activeModeOpt);
1085 mActiveModeOpt =
1086 FrameRateMode{activeModeOpt->get()->getFps(), ftl::as_non_null(activeModeOpt->get())};
Ady Abrahamabc27602020-04-08 17:20:29 -07001087
Ady Abraham68636062022-11-16 17:07:25 -08001088 const auto sortedModes = sortByRefreshRate(mDisplayModes);
Dominik Laskowskib0054a22022-03-03 09:03:06 -08001089 mMinRefreshRateModeIt = sortedModes.front();
1090 mMaxRefreshRateModeIt = sortedModes.back();
1091
Marin Shalamanov75f37252021-02-10 21:43:57 +01001092 // Reset the policy because the old one may no longer be valid.
1093 mDisplayManagerPolicy = {};
Dominik Laskowskib0054a22022-03-03 09:03:06 -08001094 mDisplayManagerPolicy.defaultMode = activeModeId;
Ady Abraham64c2fc02020-12-29 12:07:50 -08001095
Ady Abraham8ca643a2022-10-18 18:26:47 -07001096 mFrameRateOverrideConfig = [&] {
1097 switch (mConfig.enableFrameRateOverride) {
1098 case Config::FrameRateOverride::Disabled:
Ady Abraham68636062022-11-16 17:07:25 -08001099 case Config::FrameRateOverride::AppOverride:
Ady Abraham8ca643a2022-10-18 18:26:47 -07001100 case Config::FrameRateOverride::Enabled:
1101 return mConfig.enableFrameRateOverride;
Ady Abraham68636062022-11-16 17:07:25 -08001102 case Config::FrameRateOverride::AppOverrideNativeRefreshRates:
Ady Abraham8ca643a2022-10-18 18:26:47 -07001103 return shouldEnableFrameRateOverride(sortedModes)
Ady Abraham68636062022-11-16 17:07:25 -08001104 ? Config::FrameRateOverride::AppOverrideNativeRefreshRates
Ady Abraham8ca643a2022-10-18 18:26:47 -07001105 : Config::FrameRateOverride::Disabled;
1106 }
1107 }();
Ady Abraham4899ff82021-01-06 13:53:29 -08001108
Ady Abraham68636062022-11-16 17:07:25 -08001109 if (mConfig.enableFrameRateOverride ==
1110 Config::FrameRateOverride::AppOverrideNativeRefreshRates) {
1111 for (const auto& [_, mode] : mDisplayModes) {
1112 mAppOverrideNativeRefreshRates.try_emplace(mode->getFps(), ftl::unit);
1113 }
1114 }
1115
Ady Abrahamabc27602020-04-08 17:20:29 -07001116 constructAvailableRefreshRates();
Ady Abrahamb4b1e0a2019-11-20 18:25:35 -08001117}
1118
Dominik Laskowskid82e0f02022-10-26 15:23:04 -04001119bool RefreshRateSelector::isPolicyValidLocked(const Policy& policy) const {
Marin Shalamanova7fe3042021-01-29 21:02:08 +01001120 // defaultMode must be a valid mode, and within the given refresh rate range.
Dominik Laskowskib0054a22022-03-03 09:03:06 -08001121 if (const auto mode = mDisplayModes.get(policy.defaultMode)) {
Ady Abraham285f8c12022-10-11 17:12:14 -07001122 if (!policy.primaryRanges.physical.includes(mode->get()->getFps())) {
Dominik Laskowskib0054a22022-03-03 09:03:06 -08001123 ALOGE("Default mode is not in the primary range.");
1124 return false;
1125 }
1126 } else {
Marin Shalamanova7fe3042021-01-29 21:02:08 +01001127 ALOGE("Default mode is not found.");
Steven Thomasd4071902020-03-24 16:02:53 -07001128 return false;
1129 }
Dominik Laskowski6eab42d2021-09-13 14:34:13 -07001130
Ady Abraham68636062022-11-16 17:07:25 -08001131 const auto& primaryRanges = policy.primaryRanges;
1132 const auto& appRequestRanges = policy.appRequestRanges;
1133 ALOGE_IF(!appRequestRanges.physical.includes(primaryRanges.physical),
Ady Abraham08048ce2022-11-30 18:08:00 -08001134 "Physical range is invalid: primary: %s appRequest: %s",
1135 to_string(primaryRanges.physical).c_str(),
1136 to_string(appRequestRanges.physical).c_str());
1137 ALOGE_IF(!appRequestRanges.render.includes(primaryRanges.render),
1138 "Render range is invalid: primary: %s appRequest: %s",
1139 to_string(primaryRanges.render).c_str(), to_string(appRequestRanges.render).c_str());
Ady Abraham68636062022-11-16 17:07:25 -08001140
1141 return primaryRanges.valid() && appRequestRanges.valid();
Steven Thomasd4071902020-03-24 16:02:53 -07001142}
1143
Dominik Laskowskid82e0f02022-10-26 15:23:04 -04001144auto RefreshRateSelector::setPolicy(const PolicyVariant& policy) -> SetPolicyResult {
Dominik Laskowski36dced82022-09-02 09:24:00 -07001145 Policy oldPolicy;
Ady Abrahamace3d052022-11-17 16:25:05 -08001146 PhysicalDisplayId displayId;
Dominik Laskowski36dced82022-09-02 09:24:00 -07001147 {
1148 std::lock_guard lock(mLock);
1149 oldPolicy = *getCurrentPolicyLocked();
Ana Kruleced3a8cc2019-11-14 00:55:07 +01001150
Dominik Laskowski36dced82022-09-02 09:24:00 -07001151 const bool valid = ftl::match(
1152 policy,
1153 [this](const auto& policy) {
1154 ftl::FakeGuard guard(mLock);
1155 if (!isPolicyValidLocked(policy)) {
1156 ALOGE("Invalid policy: %s", policy.toString().c_str());
1157 return false;
1158 }
1159
1160 using T = std::decay_t<decltype(policy)>;
1161
1162 if constexpr (std::is_same_v<T, DisplayManagerPolicy>) {
1163 mDisplayManagerPolicy = policy;
1164 } else {
1165 static_assert(std::is_same_v<T, OverridePolicy>);
1166 mOverridePolicy = policy;
1167 }
1168 return true;
1169 },
1170 [this](NoOverridePolicy) {
1171 ftl::FakeGuard guard(mLock);
1172 mOverridePolicy.reset();
1173 return true;
1174 });
1175
1176 if (!valid) {
1177 return SetPolicyResult::Invalid;
1178 }
1179
Ady Abraham68636062022-11-16 17:07:25 -08001180 mGetRankedFrameRatesCache.reset();
Dominik Laskowski36dced82022-09-02 09:24:00 -07001181
1182 if (*getCurrentPolicyLocked() == oldPolicy) {
1183 return SetPolicyResult::Unchanged;
1184 }
1185 constructAvailableRefreshRates();
Ady Abrahamace3d052022-11-17 16:25:05 -08001186
1187 displayId = getActiveModeLocked().modePtr->getPhysicalDisplayId();
Steven Thomasd4071902020-03-24 16:02:53 -07001188 }
Dominik Laskowski36dced82022-09-02 09:24:00 -07001189
Dominik Laskowski36dced82022-09-02 09:24:00 -07001190 const unsigned numModeChanges = std::exchange(mNumModeSwitchesInPolicy, 0u);
1191
1192 ALOGI("Display %s policy changed\n"
1193 "Previous: %s\n"
1194 "Current: %s\n"
1195 "%u mode changes were performed under the previous policy",
1196 to_string(displayId).c_str(), oldPolicy.toString().c_str(), toString(policy).c_str(),
1197 numModeChanges);
1198
1199 return SetPolicyResult::Changed;
Steven Thomasd4071902020-03-24 16:02:53 -07001200}
1201
Dominik Laskowskid82e0f02022-10-26 15:23:04 -04001202auto RefreshRateSelector::getCurrentPolicyLocked() const -> const Policy* {
Steven Thomasd4071902020-03-24 16:02:53 -07001203 return mOverridePolicy ? &mOverridePolicy.value() : &mDisplayManagerPolicy;
1204}
1205
Dominik Laskowskid82e0f02022-10-26 15:23:04 -04001206auto RefreshRateSelector::getCurrentPolicy() const -> Policy {
Steven Thomasd4071902020-03-24 16:02:53 -07001207 std::lock_guard lock(mLock);
1208 return *getCurrentPolicyLocked();
1209}
1210
Dominik Laskowskid82e0f02022-10-26 15:23:04 -04001211auto RefreshRateSelector::getDisplayManagerPolicy() const -> Policy {
Steven Thomasd4071902020-03-24 16:02:53 -07001212 std::lock_guard lock(mLock);
1213 return mDisplayManagerPolicy;
Ana Kruleced3a8cc2019-11-14 00:55:07 +01001214}
1215
Ady Abrahamace3d052022-11-17 16:25:05 -08001216bool RefreshRateSelector::isModeAllowed(const FrameRateMode& mode) const {
Ana Kruleced3a8cc2019-11-14 00:55:07 +01001217 std::lock_guard lock(mLock);
Ady Abrahamace3d052022-11-17 16:25:05 -08001218 return std::find(mAppRequestFrameRates.begin(), mAppRequestFrameRates.end(), mode) !=
1219 mAppRequestFrameRates.end();
Ady Abraham2139f732019-11-13 18:56:40 -08001220}
1221
Dominik Laskowskid82e0f02022-10-26 15:23:04 -04001222void RefreshRateSelector::constructAvailableRefreshRates() {
Dominik Laskowskib0054a22022-03-03 09:03:06 -08001223 // Filter modes based on current policy and sort on refresh rate.
Steven Thomasd4071902020-03-24 16:02:53 -07001224 const Policy* policy = getCurrentPolicyLocked();
Dominik Laskowskib0054a22022-03-03 09:03:06 -08001225 ALOGV("%s: %s ", __func__, policy->toString().c_str());
Ady Abrahamabc27602020-04-08 17:20:29 -07001226
Dominik Laskowskib0054a22022-03-03 09:03:06 -08001227 const auto& defaultMode = mDisplayModes.get(policy->defaultMode)->get();
Ady Abraham8a82ba62020-01-17 12:43:17 -08001228
Ady Abraham68636062022-11-16 17:07:25 -08001229 const auto filterRefreshRates = [&](const FpsRanges& ranges,
1230 const char* rangeName) REQUIRES(mLock) {
1231 const auto filterModes = [&](const DisplayMode& mode) {
Dominik Laskowskib0054a22022-03-03 09:03:06 -08001232 return mode.getResolution() == defaultMode->getResolution() &&
1233 mode.getDpi() == defaultMode->getDpi() &&
1234 (policy->allowGroupSwitching || mode.getGroup() == defaultMode->getGroup()) &&
Ady Abraham68636062022-11-16 17:07:25 -08001235 ranges.physical.includes(mode.getFps()) &&
1236 (supportsFrameRateOverride() || ranges.render.includes(mode.getFps()));
Dominik Laskowskib0054a22022-03-03 09:03:06 -08001237 };
Ady Abraham8a82ba62020-01-17 12:43:17 -08001238
Ady Abraham8d0b0a32023-08-16 11:02:00 -07001239 auto frameRateModes = createFrameRateModes(*policy, filterModes, ranges.render);
Ady Abrahamf5c66832023-07-20 10:33:06 -07001240 if (frameRateModes.empty()) {
1241 ALOGW("No matching frame rate modes for %s range. policy: %s", rangeName,
1242 policy->toString().c_str());
1243 // TODO(b/292105422): Ideally DisplayManager should not send render ranges smaller than
1244 // the min supported. See b/292047939.
1245 // For not we just ignore the render ranges.
Ady Abraham8d0b0a32023-08-16 11:02:00 -07001246 frameRateModes = createFrameRateModes(*policy, filterModes, {});
Ady Abrahamf5c66832023-07-20 10:33:06 -07001247 }
Ady Abraham68636062022-11-16 17:07:25 -08001248 LOG_ALWAYS_FATAL_IF(frameRateModes.empty(),
Ady Abrahamf5c66832023-07-20 10:33:06 -07001249 "No matching frame rate modes for %s range even after ignoring the "
1250 "render range. policy: %s",
1251 rangeName, policy->toString().c_str());
Dominik Laskowski953b7fd2022-01-08 19:34:59 -08001252
Dominik Laskowskib0054a22022-03-03 09:03:06 -08001253 const auto stringifyModes = [&] {
1254 std::string str;
Ady Abraham68636062022-11-16 17:07:25 -08001255 for (const auto& frameRateMode : frameRateModes) {
1256 str += to_string(frameRateMode) + " ";
Dominik Laskowskib0054a22022-03-03 09:03:06 -08001257 }
1258 return str;
1259 };
Ady Abraham68636062022-11-16 17:07:25 -08001260 ALOGV("%s render rates: %s", rangeName, stringifyModes().c_str());
Steven Thomasf734df42020-04-13 21:09:28 -07001261
Ady Abraham68636062022-11-16 17:07:25 -08001262 return frameRateModes;
Dominik Laskowskib0054a22022-03-03 09:03:06 -08001263 };
1264
Ady Abraham68636062022-11-16 17:07:25 -08001265 mPrimaryFrameRates = filterRefreshRates(policy->primaryRanges, "primary");
1266 mAppRequestFrameRates = filterRefreshRates(policy->appRequestRanges, "app request");
Ady Abraham2139f732019-11-13 18:56:40 -08001267}
1268
Dominik Laskowskid82e0f02022-10-26 15:23:04 -04001269Fps RefreshRateSelector::findClosestKnownFrameRate(Fps frameRate) const {
Dominik Laskowski6eab42d2021-09-13 14:34:13 -07001270 using namespace fps_approx_ops;
1271
1272 if (frameRate <= mKnownFrameRates.front()) {
1273 return mKnownFrameRates.front();
Ady Abrahamb1b9d412020-06-01 19:53:52 -07001274 }
1275
Dominik Laskowski6eab42d2021-09-13 14:34:13 -07001276 if (frameRate >= mKnownFrameRates.back()) {
1277 return mKnownFrameRates.back();
Ady Abrahamb1b9d412020-06-01 19:53:52 -07001278 }
1279
Marin Shalamanove8a663d2020-11-24 17:48:00 +01001280 auto lowerBound = std::lower_bound(mKnownFrameRates.begin(), mKnownFrameRates.end(), frameRate,
Dominik Laskowski6eab42d2021-09-13 14:34:13 -07001281 isStrictlyLess);
Ady Abrahamb1b9d412020-06-01 19:53:52 -07001282
Dominik Laskowski6eab42d2021-09-13 14:34:13 -07001283 const auto distance1 = std::abs(frameRate.getValue() - lowerBound->getValue());
1284 const auto distance2 = std::abs(frameRate.getValue() - std::prev(lowerBound)->getValue());
Ady Abrahamb1b9d412020-06-01 19:53:52 -07001285 return distance1 < distance2 ? *lowerBound : *std::prev(lowerBound);
1286}
1287
Dominik Laskowskid82e0f02022-10-26 15:23:04 -04001288auto RefreshRateSelector::getIdleTimerAction() const -> KernelIdleTimerAction {
Ana Krulecb9afd792020-06-11 13:16:15 -07001289 std::lock_guard lock(mLock);
Dominik Laskowskib0054a22022-03-03 09:03:06 -08001290
1291 const Fps deviceMinFps = mMinRefreshRateModeIt->second->getFps();
1292 const DisplayModePtr& minByPolicy = getMinRefreshRateByPolicyLocked();
Ana Krulecb9afd792020-06-11 13:16:15 -07001293
1294 // Kernel idle timer will set the refresh rate to the device min. If DisplayManager says that
1295 // the min allowed refresh rate is higher than the device min, we do not want to enable the
1296 // timer.
Dominik Laskowskib0054a22022-03-03 09:03:06 -08001297 if (isStrictlyLess(deviceMinFps, minByPolicy->getFps())) {
1298 return KernelIdleTimerAction::TurnOff;
Ana Krulecb9afd792020-06-11 13:16:15 -07001299 }
Dominik Laskowskib0054a22022-03-03 09:03:06 -08001300
ramindanid72ba162022-09-09 21:33:40 +00001301 const DisplayModePtr& maxByPolicy =
Ady Abrahamace3d052022-11-17 16:25:05 -08001302 getMaxRefreshRateByPolicyLocked(getActiveModeLocked().modePtr->getGroup());
Ana Krulecb9afd792020-06-11 13:16:15 -07001303 if (minByPolicy == maxByPolicy) {
Dominik Laskowskib0054a22022-03-03 09:03:06 -08001304 // Turn on the timer when the min of the primary range is below the device min.
1305 if (const Policy* currentPolicy = getCurrentPolicyLocked();
Ady Abraham285f8c12022-10-11 17:12:14 -07001306 isApproxLess(currentPolicy->primaryRanges.physical.min, deviceMinFps)) {
Dominik Laskowskib0054a22022-03-03 09:03:06 -08001307 return KernelIdleTimerAction::TurnOn;
Ana Krulecb9afd792020-06-11 13:16:15 -07001308 }
Dominik Laskowskib0054a22022-03-03 09:03:06 -08001309 return KernelIdleTimerAction::TurnOff;
Ana Krulecb9afd792020-06-11 13:16:15 -07001310 }
Dominik Laskowskib0054a22022-03-03 09:03:06 -08001311
Ana Krulecb9afd792020-06-11 13:16:15 -07001312 // Turn on the timer in all other cases.
Dominik Laskowskib0054a22022-03-03 09:03:06 -08001313 return KernelIdleTimerAction::TurnOn;
Ana Krulecb9afd792020-06-11 13:16:15 -07001314}
1315
Dominik Laskowskid82e0f02022-10-26 15:23:04 -04001316int RefreshRateSelector::getFrameRateDivisor(Fps displayRefreshRate, Fps layerFrameRate) {
Ady Abraham62f216c2020-10-13 19:07:23 -07001317 // This calculation needs to be in sync with the java code
1318 // in DisplayManagerService.getDisplayInfoForFrameRateOverride
Marin Shalamanov15a0fc62021-08-16 18:20:21 +02001319
1320 // The threshold must be smaller than 0.001 in order to differentiate
1321 // between the fractional pairs (e.g. 59.94 and 60).
1322 constexpr float kThreshold = 0.0009f;
Dominik Laskowskib0054a22022-03-03 09:03:06 -08001323 const auto numPeriods = displayRefreshRate.getValue() / layerFrameRate.getValue();
Ady Abraham0bb6a472020-10-12 10:22:13 -07001324 const auto numPeriodsRounded = std::round(numPeriods);
1325 if (std::abs(numPeriods - numPeriodsRounded) > kThreshold) {
Ady Abraham62a0be22020-12-08 16:54:10 -08001326 return 0;
Ady Abraham0bb6a472020-10-12 10:22:13 -07001327 }
1328
Ady Abraham62f216c2020-10-13 19:07:23 -07001329 return static_cast<int>(numPeriodsRounded);
1330}
1331
Dominik Laskowskid82e0f02022-10-26 15:23:04 -04001332bool RefreshRateSelector::isFractionalPairOrMultiple(Fps smaller, Fps bigger) {
Dominik Laskowski6eab42d2021-09-13 14:34:13 -07001333 if (isStrictlyLess(bigger, smaller)) {
Marin Shalamanov15a0fc62021-08-16 18:20:21 +02001334 return isFractionalPairOrMultiple(bigger, smaller);
1335 }
1336
1337 const auto multiplier = std::round(bigger.getValue() / smaller.getValue());
1338 constexpr float kCoef = 1000.f / 1001.f;
Dominik Laskowski6eab42d2021-09-13 14:34:13 -07001339 return isApproxEqual(bigger, Fps::fromValue(smaller.getValue() * multiplier / kCoef)) ||
1340 isApproxEqual(bigger, Fps::fromValue(smaller.getValue() * multiplier * kCoef));
Marin Shalamanov15a0fc62021-08-16 18:20:21 +02001341}
1342
Dominik Laskowskid82e0f02022-10-26 15:23:04 -04001343void RefreshRateSelector::dump(utils::Dumper& dumper) const {
Dominik Laskowskie70461a2022-08-30 14:42:01 -07001344 using namespace std::string_view_literals;
Dominik Laskowski0acc3842022-04-07 11:23:42 -07001345
Marin Shalamanovba421a82020-11-10 21:49:26 +01001346 std::lock_guard lock(mLock);
Marin Shalamanovba421a82020-11-10 21:49:26 +01001347
Ady Abrahamace3d052022-11-17 16:25:05 -08001348 const auto activeMode = getActiveModeLocked();
1349 dumper.dump("activeMode"sv, to_string(activeMode));
Marin Shalamanovba421a82020-11-10 21:49:26 +01001350
Dominik Laskowskie70461a2022-08-30 14:42:01 -07001351 dumper.dump("displayModes"sv);
1352 {
1353 utils::Dumper::Indent indent(dumper);
1354 for (const auto& [id, mode] : mDisplayModes) {
1355 dumper.dump({}, to_string(*mode));
1356 }
Marin Shalamanovba421a82020-11-10 21:49:26 +01001357 }
1358
Dominik Laskowskie70461a2022-08-30 14:42:01 -07001359 dumper.dump("displayManagerPolicy"sv, mDisplayManagerPolicy.toString());
Dominik Laskowskib0054a22022-03-03 09:03:06 -08001360
Dominik Laskowski0acc3842022-04-07 11:23:42 -07001361 if (const Policy& currentPolicy = *getCurrentPolicyLocked();
1362 mOverridePolicy && currentPolicy != mDisplayManagerPolicy) {
Dominik Laskowskie70461a2022-08-30 14:42:01 -07001363 dumper.dump("overridePolicy"sv, currentPolicy.toString());
ramindani32cf0602022-03-02 02:30:29 +00001364 }
Dominik Laskowskib0054a22022-03-03 09:03:06 -08001365
Ady Abraham8ca643a2022-10-18 18:26:47 -07001366 dumper.dump("frameRateOverrideConfig"sv, *ftl::enum_name(mFrameRateOverrideConfig));
Dominik Laskowski0acc3842022-04-07 11:23:42 -07001367
Dominik Laskowski03cfce82022-11-02 12:13:29 -04001368 dumper.dump("idleTimer"sv);
1369 {
1370 utils::Dumper::Indent indent(dumper);
1371 dumper.dump("interval"sv, mIdleTimer.transform(&OneShotTimer::interval));
1372 dumper.dump("controller"sv,
1373 mConfig.kernelIdleTimerController
1374 .and_then(&ftl::enum_name<KernelIdleTimerController>)
1375 .value_or("Platform"sv));
Dominik Laskowskib0054a22022-03-03 09:03:06 -08001376 }
Marin Shalamanovba421a82020-11-10 21:49:26 +01001377}
1378
Dominik Laskowskid82e0f02022-10-26 15:23:04 -04001379std::chrono::milliseconds RefreshRateSelector::getIdleTimerTimeout() {
ramindani32cf0602022-03-02 02:30:29 +00001380 return mConfig.idleTimerTimeout;
1381}
1382
Ady Abraham2139f732019-11-13 18:56:40 -08001383} // namespace android::scheduler
Marin Shalamanovbed7fd32020-12-21 20:02:20 +01001384
1385// TODO(b/129481165): remove the #pragma below and fix conversion issues
Ady Abrahamdd5bfa92021-01-07 17:56:08 -08001386#pragma clang diagnostic pop // ignored "-Wextra"