Ady Abraham | b4b1e0a | 2019-11-20 18:25:35 -0800 | [diff] [blame] | 1 | /* |
| 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 Abraham | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame] | 16 | |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 17 | // #define LOG_NDEBUG 0 |
| 18 | #define ATRACE_TAG ATRACE_TAG_GRAPHICS |
| 19 | |
Marin Shalamanov | bed7fd3 | 2020-12-21 20:02:20 +0100 | [diff] [blame] | 20 | // TODO(b/129481165): remove the #pragma below and fix conversion issues |
| 21 | #pragma clang diagnostic push |
| 22 | #pragma clang diagnostic ignored "-Wextra" |
| 23 | |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 24 | #include <chrono> |
| 25 | #include <cmath> |
Dominik Laskowski | f5d0ea5 | 2021-09-26 17:27:01 -0700 | [diff] [blame] | 26 | |
| 27 | #include <android-base/properties.h> |
| 28 | #include <android-base/stringprintf.h> |
| 29 | #include <ftl/enum.h> |
Dominik Laskowski | f8734e0 | 2022-08-26 09:06:59 -0700 | [diff] [blame] | 30 | #include <ftl/fake_guard.h> |
Dominik Laskowski | f5d0ea5 | 2021-09-26 17:27:01 -0700 | [diff] [blame] | 31 | #include <utils/Trace.h> |
| 32 | |
Ady Abraham | 4899ff8 | 2021-01-06 13:53:29 -0800 | [diff] [blame] | 33 | #include "../SurfaceFlingerProperties.h" |
Dominik Laskowski | f5d0ea5 | 2021-09-26 17:27:01 -0700 | [diff] [blame] | 34 | #include "RefreshRateConfigs.h" |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 35 | |
Ady Abraham | 5b8afb5a | 2020-03-06 14:57:26 -0800 | [diff] [blame] | 36 | #undef LOG_TAG |
| 37 | #define LOG_TAG "RefreshRateConfigs" |
| 38 | |
Ady Abraham | b4b1e0a | 2019-11-20 18:25:35 -0800 | [diff] [blame] | 39 | namespace android::scheduler { |
Marin Shalamanov | 53fc11d | 2020-11-20 14:00:13 +0100 | [diff] [blame] | 40 | namespace { |
Dominik Laskowski | f5d0ea5 | 2021-09-26 17:27:01 -0700 | [diff] [blame] | 41 | |
Dominik Laskowski | b0054a2 | 2022-03-03 09:03:06 -0800 | [diff] [blame] | 42 | struct RefreshRateScore { |
| 43 | DisplayModeIterator modeIt; |
Ady Abraham | ae2e3c7 | 2022-08-13 05:12:13 +0000 | [diff] [blame] | 44 | float overallScore; |
| 45 | struct { |
Ady Abraham | 62f51d9 | 2022-08-24 22:20:22 +0000 | [diff] [blame] | 46 | float modeBelowThreshold; |
| 47 | float modeAboveThreshold; |
| 48 | } fixedRateBelowThresholdLayersScore; |
Dominik Laskowski | b0054a2 | 2022-03-03 09:03:06 -0800 | [diff] [blame] | 49 | }; |
| 50 | |
Dominik Laskowski | a8626ec | 2021-12-15 18:13:30 -0800 | [diff] [blame] | 51 | constexpr RefreshRateConfigs::GlobalSignals kNoSignals; |
| 52 | |
Marin Shalamanov | 53fc11d | 2020-11-20 14:00:13 +0100 | [diff] [blame] | 53 | std::string formatLayerInfo(const RefreshRateConfigs::LayerRequirement& layer, float weight) { |
Dominik Laskowski | b0054a2 | 2022-03-03 09:03:06 -0800 | [diff] [blame] | 54 | return base::StringPrintf("%s (type=%s, weight=%.2f, seamlessness=%s) %s", layer.name.c_str(), |
Dominik Laskowski | f5d0ea5 | 2021-09-26 17:27:01 -0700 | [diff] [blame] | 55 | ftl::enum_string(layer.vote).c_str(), weight, |
| 56 | ftl::enum_string(layer.seamlessness).c_str(), |
Marin Shalamanov | e8a663d | 2020-11-24 17:48:00 +0100 | [diff] [blame] | 57 | to_string(layer.desiredRefreshRate).c_str()); |
Marin Shalamanov | 53fc11d | 2020-11-20 14:00:13 +0100 | [diff] [blame] | 58 | } |
Marin Shalamanov | eadf2e7 | 2020-12-10 15:35:28 +0100 | [diff] [blame] | 59 | |
Marin Shalamanov | a7fe304 | 2021-01-29 21:02:08 +0100 | [diff] [blame] | 60 | std::vector<Fps> constructKnownFrameRates(const DisplayModes& modes) { |
Dominik Laskowski | 6eab42d | 2021-09-13 14:34:13 -0700 | [diff] [blame] | 61 | std::vector<Fps> knownFrameRates = {24_Hz, 30_Hz, 45_Hz, 60_Hz, 72_Hz}; |
Marin Shalamanov | a7fe304 | 2021-01-29 21:02:08 +0100 | [diff] [blame] | 62 | knownFrameRates.reserve(knownFrameRates.size() + modes.size()); |
Marin Shalamanov | eadf2e7 | 2020-12-10 15:35:28 +0100 | [diff] [blame] | 63 | |
Dominik Laskowski | 6eab42d | 2021-09-13 14:34:13 -0700 | [diff] [blame] | 64 | // Add all supported refresh rates. |
Dominik Laskowski | b0054a2 | 2022-03-03 09:03:06 -0800 | [diff] [blame] | 65 | for (const auto& [id, mode] : modes) { |
| 66 | knownFrameRates.push_back(mode->getFps()); |
Marin Shalamanov | eadf2e7 | 2020-12-10 15:35:28 +0100 | [diff] [blame] | 67 | } |
| 68 | |
Dominik Laskowski | 6eab42d | 2021-09-13 14:34:13 -0700 | [diff] [blame] | 69 | // Sort and remove duplicates. |
| 70 | std::sort(knownFrameRates.begin(), knownFrameRates.end(), isStrictlyLess); |
Marin Shalamanov | eadf2e7 | 2020-12-10 15:35:28 +0100 | [diff] [blame] | 71 | knownFrameRates.erase(std::unique(knownFrameRates.begin(), knownFrameRates.end(), |
Dominik Laskowski | 6eab42d | 2021-09-13 14:34:13 -0700 | [diff] [blame] | 72 | isApproxEqual), |
Marin Shalamanov | eadf2e7 | 2020-12-10 15:35:28 +0100 | [diff] [blame] | 73 | knownFrameRates.end()); |
| 74 | return knownFrameRates; |
| 75 | } |
| 76 | |
Dominik Laskowski | b0054a2 | 2022-03-03 09:03:06 -0800 | [diff] [blame] | 77 | // The Filter is a `bool(const DisplayMode&)` predicate. |
| 78 | template <typename Filter> |
| 79 | std::vector<DisplayModeIterator> sortByRefreshRate(const DisplayModes& modes, Filter&& filter) { |
| 80 | std::vector<DisplayModeIterator> sortedModes; |
| 81 | sortedModes.reserve(modes.size()); |
Ady Abraham | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame] | 82 | |
Dominik Laskowski | b0054a2 | 2022-03-03 09:03:06 -0800 | [diff] [blame] | 83 | for (auto it = modes.begin(); it != modes.end(); ++it) { |
| 84 | const auto& [id, mode] = *it; |
Ady Abraham | b4b1e0a | 2019-11-20 18:25:35 -0800 | [diff] [blame] | 85 | |
Dominik Laskowski | b0054a2 | 2022-03-03 09:03:06 -0800 | [diff] [blame] | 86 | if (filter(*mode)) { |
| 87 | ALOGV("%s: including mode %d", __func__, id.value()); |
| 88 | sortedModes.push_back(it); |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | std::sort(sortedModes.begin(), sortedModes.end(), [](auto it1, auto it2) { |
| 93 | const auto& mode1 = it1->second; |
| 94 | const auto& mode2 = it2->second; |
| 95 | |
| 96 | if (mode1->getVsyncPeriod() == mode2->getVsyncPeriod()) { |
| 97 | return mode1->getGroup() > mode2->getGroup(); |
| 98 | } |
| 99 | |
| 100 | return mode1->getVsyncPeriod() > mode2->getVsyncPeriod(); |
| 101 | }); |
| 102 | |
| 103 | return sortedModes; |
Marin Shalamanov | 4608442 | 2020-10-13 12:33:42 +0200 | [diff] [blame] | 104 | } |
| 105 | |
Dominik Laskowski | b0054a2 | 2022-03-03 09:03:06 -0800 | [diff] [blame] | 106 | bool canModesSupportFrameRateOverride(const std::vector<DisplayModeIterator>& sortedModes) { |
| 107 | for (const auto it1 : sortedModes) { |
| 108 | const auto& mode1 = it1->second; |
| 109 | for (const auto it2 : sortedModes) { |
| 110 | const auto& mode2 = it2->second; |
| 111 | |
| 112 | if (RefreshRateConfigs::getFrameRateDivisor(mode1->getFps(), mode2->getFps()) >= 2) { |
| 113 | return true; |
| 114 | } |
| 115 | } |
| 116 | } |
| 117 | return false; |
| 118 | } |
| 119 | |
| 120 | } // namespace |
| 121 | |
ramindani | d72ba16 | 2022-09-09 21:33:40 +0000 | [diff] [blame] | 122 | struct RefreshRateConfigs::RefreshRateScoreComparator { |
| 123 | bool operator()(const RefreshRateScore& lhs, const RefreshRateScore& rhs) const { |
| 124 | const auto& [modeIt, overallScore, _] = lhs; |
| 125 | |
| 126 | std::string name = to_string(modeIt->second->getFps()); |
| 127 | ALOGV("%s sorting scores %.2f", name.c_str(), overallScore); |
| 128 | |
| 129 | ATRACE_INT(name.c_str(), static_cast<int>(std::round(overallScore * 100))); |
| 130 | |
| 131 | constexpr float kEpsilon = 0.0001f; |
| 132 | if (std::abs(overallScore - rhs.overallScore) > kEpsilon) { |
| 133 | return overallScore > rhs.overallScore; |
| 134 | } |
| 135 | |
| 136 | // If overallScore tie we will pick the higher refresh rate if |
| 137 | // high refresh rate is the priority else the lower refresh rate. |
| 138 | if (refreshRateOrder == RefreshRateOrder::Descending) { |
| 139 | using fps_approx_ops::operator>; |
| 140 | return modeIt->second->getFps() > rhs.modeIt->second->getFps(); |
| 141 | } else { |
| 142 | using fps_approx_ops::operator<; |
| 143 | return modeIt->second->getFps() < rhs.modeIt->second->getFps(); |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | const RefreshRateOrder refreshRateOrder; |
| 148 | }; |
| 149 | |
Marin Shalamanov | b6674e7 | 2020-11-06 13:05:57 +0100 | [diff] [blame] | 150 | std::string RefreshRateConfigs::Policy::toString() const { |
Dominik Laskowski | 0acc384 | 2022-04-07 11:23:42 -0700 | [diff] [blame] | 151 | return base::StringPrintf("{defaultModeId=%d, allowGroupSwitching=%s" |
| 152 | ", primaryRange=%s, appRequestRange=%s}", |
| 153 | defaultMode.value(), allowGroupSwitching ? "true" : "false", |
Dominik Laskowski | 953b7fd | 2022-01-08 19:34:59 -0800 | [diff] [blame] | 154 | to_string(primaryRange).c_str(), to_string(appRequestRange).c_str()); |
Marin Shalamanov | 30b0b3c | 2020-10-13 19:15:06 +0200 | [diff] [blame] | 155 | } |
| 156 | |
Ady Abraham | 4ccdcb4 | 2020-02-11 17:34:34 -0800 | [diff] [blame] | 157 | std::pair<nsecs_t, nsecs_t> RefreshRateConfigs::getDisplayFrames(nsecs_t layerPeriod, |
| 158 | nsecs_t displayPeriod) const { |
Ady Abraham | 62a0be2 | 2020-12-08 16:54:10 -0800 | [diff] [blame] | 159 | auto [quotient, remainder] = std::div(layerPeriod, displayPeriod); |
| 160 | if (remainder <= MARGIN_FOR_PERIOD_CALCULATION || |
| 161 | std::abs(remainder - displayPeriod) <= MARGIN_FOR_PERIOD_CALCULATION) { |
| 162 | quotient++; |
| 163 | remainder = 0; |
Ady Abraham | 4ccdcb4 | 2020-02-11 17:34:34 -0800 | [diff] [blame] | 164 | } |
| 165 | |
Ady Abraham | 62a0be2 | 2020-12-08 16:54:10 -0800 | [diff] [blame] | 166 | return {quotient, remainder}; |
Ady Abraham | 4ccdcb4 | 2020-02-11 17:34:34 -0800 | [diff] [blame] | 167 | } |
| 168 | |
Dominik Laskowski | b0054a2 | 2022-03-03 09:03:06 -0800 | [diff] [blame] | 169 | float RefreshRateConfigs::calculateNonExactMatchingLayerScoreLocked(const LayerRequirement& layer, |
| 170 | Fps refreshRate) const { |
Marin Shalamanov | 15a0fc6 | 2021-08-16 18:20:21 +0200 | [diff] [blame] | 171 | constexpr float kScoreForFractionalPairs = .8f; |
| 172 | |
Dominik Laskowski | b0054a2 | 2022-03-03 09:03:06 -0800 | [diff] [blame] | 173 | const auto displayPeriod = refreshRate.getPeriodNsecs(); |
Ady Abraham | 62a0be2 | 2020-12-08 16:54:10 -0800 | [diff] [blame] | 174 | const auto layerPeriod = layer.desiredRefreshRate.getPeriodNsecs(); |
| 175 | if (layer.vote == LayerVoteType::ExplicitDefault) { |
| 176 | // Find the actual rate the layer will render, assuming |
Marin Shalamanov | 15a0fc6 | 2021-08-16 18:20:21 +0200 | [diff] [blame] | 177 | // that layerPeriod is the minimal period to render a frame. |
| 178 | // For example if layerPeriod is 20ms and displayPeriod is 16ms, |
| 179 | // then the actualLayerPeriod will be 32ms, because it is the |
| 180 | // smallest multiple of the display period which is >= layerPeriod. |
Ady Abraham | 62a0be2 | 2020-12-08 16:54:10 -0800 | [diff] [blame] | 181 | auto actualLayerPeriod = displayPeriod; |
| 182 | int multiplier = 1; |
| 183 | while (layerPeriod > actualLayerPeriod + MARGIN_FOR_PERIOD_CALCULATION) { |
| 184 | multiplier++; |
| 185 | actualLayerPeriod = displayPeriod * multiplier; |
| 186 | } |
Marin Shalamanov | 15a0fc6 | 2021-08-16 18:20:21 +0200 | [diff] [blame] | 187 | |
| 188 | // Because of the threshold we used above it's possible that score is slightly |
| 189 | // above 1. |
Ady Abraham | 62a0be2 | 2020-12-08 16:54:10 -0800 | [diff] [blame] | 190 | return std::min(1.0f, |
| 191 | static_cast<float>(layerPeriod) / static_cast<float>(actualLayerPeriod)); |
| 192 | } |
| 193 | |
| 194 | if (layer.vote == LayerVoteType::ExplicitExactOrMultiple || |
| 195 | layer.vote == LayerVoteType::Heuristic) { |
Dominik Laskowski | b0054a2 | 2022-03-03 09:03:06 -0800 | [diff] [blame] | 196 | if (isFractionalPairOrMultiple(refreshRate, layer.desiredRefreshRate)) { |
Ady Abraham | 05243be | 2021-09-16 15:58:52 -0700 | [diff] [blame] | 197 | return kScoreForFractionalPairs; |
Marin Shalamanov | 15a0fc6 | 2021-08-16 18:20:21 +0200 | [diff] [blame] | 198 | } |
| 199 | |
Ady Abraham | 62a0be2 | 2020-12-08 16:54:10 -0800 | [diff] [blame] | 200 | // Calculate how many display vsyncs we need to present a single frame for this |
| 201 | // layer |
| 202 | const auto [displayFramesQuotient, displayFramesRemainder] = |
| 203 | getDisplayFrames(layerPeriod, displayPeriod); |
| 204 | static constexpr size_t MAX_FRAMES_TO_FIT = 10; // Stop calculating when score < 0.1 |
| 205 | if (displayFramesRemainder == 0) { |
| 206 | // Layer desired refresh rate matches the display rate. |
Ady Abraham | 05243be | 2021-09-16 15:58:52 -0700 | [diff] [blame] | 207 | return 1.0f; |
Ady Abraham | 62a0be2 | 2020-12-08 16:54:10 -0800 | [diff] [blame] | 208 | } |
| 209 | |
| 210 | if (displayFramesQuotient == 0) { |
| 211 | // Layer desired refresh rate is higher than the display rate. |
| 212 | return (static_cast<float>(layerPeriod) / static_cast<float>(displayPeriod)) * |
| 213 | (1.0f / (MAX_FRAMES_TO_FIT + 1)); |
| 214 | } |
| 215 | |
| 216 | // Layer desired refresh rate is lower than the display rate. Check how well it fits |
| 217 | // the cadence. |
| 218 | auto diff = std::abs(displayFramesRemainder - (displayPeriod - displayFramesRemainder)); |
| 219 | int iter = 2; |
| 220 | while (diff > MARGIN_FOR_PERIOD_CALCULATION && iter < MAX_FRAMES_TO_FIT) { |
| 221 | diff = diff - (displayPeriod - diff); |
| 222 | iter++; |
| 223 | } |
| 224 | |
Ady Abraham | 05243be | 2021-09-16 15:58:52 -0700 | [diff] [blame] | 225 | return (1.0f / iter); |
| 226 | } |
| 227 | |
| 228 | return 0; |
| 229 | } |
| 230 | |
ramindani | d72ba16 | 2022-09-09 21:33:40 +0000 | [diff] [blame] | 231 | float RefreshRateConfigs::calculateRefreshRateScoreForFps(Fps refreshRate) const { |
| 232 | const float ratio = |
| 233 | refreshRate.getValue() / mAppRequestRefreshRates.back()->second->getFps().getValue(); |
| 234 | // Use ratio^2 to get a lower score the more we get further from peak |
| 235 | return ratio * ratio; |
| 236 | } |
| 237 | |
Dominik Laskowski | b0054a2 | 2022-03-03 09:03:06 -0800 | [diff] [blame] | 238 | float RefreshRateConfigs::calculateLayerScoreLocked(const LayerRequirement& layer, Fps refreshRate, |
Ady Abraham | 05243be | 2021-09-16 15:58:52 -0700 | [diff] [blame] | 239 | bool isSeamlessSwitch) const { |
Ady Abraham | 05243be | 2021-09-16 15:58:52 -0700 | [diff] [blame] | 240 | // Slightly prefer seamless switches. |
| 241 | constexpr float kSeamedSwitchPenalty = 0.95f; |
| 242 | const float seamlessness = isSeamlessSwitch ? 1.0f : kSeamedSwitchPenalty; |
| 243 | |
| 244 | // If the layer wants Max, give higher score to the higher refresh rate |
| 245 | if (layer.vote == LayerVoteType::Max) { |
ramindani | d72ba16 | 2022-09-09 21:33:40 +0000 | [diff] [blame] | 246 | return calculateRefreshRateScoreForFps(refreshRate); |
Ady Abraham | 62a0be2 | 2020-12-08 16:54:10 -0800 | [diff] [blame] | 247 | } |
| 248 | |
Ady Abraham | dd5bfa9 | 2021-01-07 17:56:08 -0800 | [diff] [blame] | 249 | if (layer.vote == LayerVoteType::ExplicitExact) { |
Dominik Laskowski | b0054a2 | 2022-03-03 09:03:06 -0800 | [diff] [blame] | 250 | const int divisor = getFrameRateDivisor(refreshRate, layer.desiredRefreshRate); |
Andy Yu | 2ae6b6b | 2021-11-18 14:51:06 -0800 | [diff] [blame] | 251 | if (mSupportsFrameRateOverrideByContent) { |
Ady Abraham | dd5bfa9 | 2021-01-07 17:56:08 -0800 | [diff] [blame] | 252 | // Since we support frame rate override, allow refresh rates which are |
| 253 | // multiples of the layer's request, as those apps would be throttled |
| 254 | // down to run at the desired refresh rate. |
Ady Abraham | cc31549 | 2022-02-17 17:06:39 -0800 | [diff] [blame] | 255 | return divisor > 0; |
Ady Abraham | dd5bfa9 | 2021-01-07 17:56:08 -0800 | [diff] [blame] | 256 | } |
| 257 | |
Ady Abraham | cc31549 | 2022-02-17 17:06:39 -0800 | [diff] [blame] | 258 | return divisor == 1; |
Ady Abraham | dd5bfa9 | 2021-01-07 17:56:08 -0800 | [diff] [blame] | 259 | } |
| 260 | |
Ady Abraham | cc31549 | 2022-02-17 17:06:39 -0800 | [diff] [blame] | 261 | // If the layer frame rate is a divisor of the refresh rate it should score |
Ady Abraham | 05243be | 2021-09-16 15:58:52 -0700 | [diff] [blame] | 262 | // the highest score. |
Dominik Laskowski | b0054a2 | 2022-03-03 09:03:06 -0800 | [diff] [blame] | 263 | if (getFrameRateDivisor(refreshRate, layer.desiredRefreshRate) > 0) { |
Ady Abraham | 05243be | 2021-09-16 15:58:52 -0700 | [diff] [blame] | 264 | return 1.0f * seamlessness; |
| 265 | } |
| 266 | |
Ady Abraham | cc31549 | 2022-02-17 17:06:39 -0800 | [diff] [blame] | 267 | // The layer frame rate is not a divisor of the refresh rate, |
Ady Abraham | 05243be | 2021-09-16 15:58:52 -0700 | [diff] [blame] | 268 | // there is a small penalty attached to the score to favor the frame rates |
| 269 | // the exactly matches the display refresh rate or a multiple. |
Ady Abraham | 1c59550 | 2022-01-13 21:58:32 -0800 | [diff] [blame] | 270 | constexpr float kNonExactMatchingPenalty = 0.95f; |
Ady Abraham | 05243be | 2021-09-16 15:58:52 -0700 | [diff] [blame] | 271 | return calculateNonExactMatchingLayerScoreLocked(layer, refreshRate) * seamlessness * |
| 272 | kNonExactMatchingPenalty; |
Ady Abraham | 62a0be2 | 2020-12-08 16:54:10 -0800 | [diff] [blame] | 273 | } |
| 274 | |
ramindani | d72ba16 | 2022-09-09 21:33:40 +0000 | [diff] [blame] | 275 | auto RefreshRateConfigs::getRankedRefreshRates(const std::vector<LayerRequirement>& layers, |
| 276 | GlobalSignals signals) const |
| 277 | -> std::pair<std::vector<RefreshRateRanking>, GlobalSignals> { |
Marin Shalamanov | 4c7831e | 2021-06-08 20:44:06 +0200 | [diff] [blame] | 278 | std::lock_guard lock(mLock); |
| 279 | |
ramindani | d72ba16 | 2022-09-09 21:33:40 +0000 | [diff] [blame] | 280 | if (mGetRankedRefreshRatesCache && |
| 281 | mGetRankedRefreshRatesCache->arguments == std::make_pair(layers, signals)) { |
| 282 | return mGetRankedRefreshRatesCache->result; |
Marin Shalamanov | 4c7831e | 2021-06-08 20:44:06 +0200 | [diff] [blame] | 283 | } |
| 284 | |
ramindani | d72ba16 | 2022-09-09 21:33:40 +0000 | [diff] [blame] | 285 | const auto result = getRankedRefreshRatesLocked(layers, signals); |
| 286 | mGetRankedRefreshRatesCache = GetRankedRefreshRatesCache{{layers, signals}, result}; |
Marin Shalamanov | 4c7831e | 2021-06-08 20:44:06 +0200 | [diff] [blame] | 287 | return result; |
| 288 | } |
| 289 | |
ramindani | d72ba16 | 2022-09-09 21:33:40 +0000 | [diff] [blame] | 290 | auto RefreshRateConfigs::getRankedRefreshRatesLocked(const std::vector<LayerRequirement>& layers, |
| 291 | GlobalSignals signals) const |
| 292 | -> std::pair<std::vector<RefreshRateRanking>, GlobalSignals> { |
Ady Abraham | ae2e3c7 | 2022-08-13 05:12:13 +0000 | [diff] [blame] | 293 | using namespace fps_approx_ops; |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 294 | ATRACE_CALL(); |
Dominik Laskowski | a8626ec | 2021-12-15 18:13:30 -0800 | [diff] [blame] | 295 | ALOGV("%s: %zu layers", __func__, layers.size()); |
Ady Abraham | dfd6216 | 2020-06-10 16:11:56 -0700 | [diff] [blame] | 296 | |
ramindani | 38c8498 | 2022-08-29 18:02:57 +0000 | [diff] [blame] | 297 | const auto& activeMode = *getActiveModeItLocked()->second; |
| 298 | |
| 299 | // Keep the display at max refresh rate for the duration of powering on the display. |
| 300 | if (signals.powerOnImminent) { |
| 301 | ALOGV("Power On Imminent"); |
ramindani | d72ba16 | 2022-09-09 21:33:40 +0000 | [diff] [blame] | 302 | return {getRefreshRatesByPolicyLocked(activeMode.getGroup(), RefreshRateOrder::Descending), |
| 303 | GlobalSignals{.powerOnImminent = true}}; |
ramindani | 38c8498 | 2022-08-29 18:02:57 +0000 | [diff] [blame] | 304 | } |
| 305 | |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 306 | int noVoteLayers = 0; |
| 307 | int minVoteLayers = 0; |
| 308 | int maxVoteLayers = 0; |
Ady Abraham | 71c437d | 2020-01-31 15:56:57 -0800 | [diff] [blame] | 309 | int explicitDefaultVoteLayers = 0; |
| 310 | int explicitExactOrMultipleVoteLayers = 0; |
Ady Abraham | dd5bfa9 | 2021-01-07 17:56:08 -0800 | [diff] [blame] | 311 | int explicitExact = 0; |
Marin Shalamanov | ae0b535 | 2021-03-24 12:56:08 +0100 | [diff] [blame] | 312 | int seamedFocusedLayers = 0; |
Dominik Laskowski | a8626ec | 2021-12-15 18:13:30 -0800 | [diff] [blame] | 313 | |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 314 | for (const auto& layer : layers) { |
Ady Abraham | dd5bfa9 | 2021-01-07 17:56:08 -0800 | [diff] [blame] | 315 | switch (layer.vote) { |
| 316 | case LayerVoteType::NoVote: |
| 317 | noVoteLayers++; |
| 318 | break; |
| 319 | case LayerVoteType::Min: |
| 320 | minVoteLayers++; |
| 321 | break; |
| 322 | case LayerVoteType::Max: |
| 323 | maxVoteLayers++; |
| 324 | break; |
| 325 | case LayerVoteType::ExplicitDefault: |
| 326 | explicitDefaultVoteLayers++; |
Ady Abraham | dd5bfa9 | 2021-01-07 17:56:08 -0800 | [diff] [blame] | 327 | break; |
| 328 | case LayerVoteType::ExplicitExactOrMultiple: |
| 329 | explicitExactOrMultipleVoteLayers++; |
Ady Abraham | dd5bfa9 | 2021-01-07 17:56:08 -0800 | [diff] [blame] | 330 | break; |
| 331 | case LayerVoteType::ExplicitExact: |
| 332 | explicitExact++; |
Ady Abraham | dd5bfa9 | 2021-01-07 17:56:08 -0800 | [diff] [blame] | 333 | break; |
| 334 | case LayerVoteType::Heuristic: |
| 335 | break; |
Ady Abraham | 6fb599b | 2020-03-05 13:48:22 -0800 | [diff] [blame] | 336 | } |
Marin Shalamanov | 4608442 | 2020-10-13 12:33:42 +0200 | [diff] [blame] | 337 | |
Marin Shalamanov | ae0b535 | 2021-03-24 12:56:08 +0100 | [diff] [blame] | 338 | if (layer.seamlessness == Seamlessness::SeamedAndSeamless && layer.focused) { |
| 339 | seamedFocusedLayers++; |
Marin Shalamanov | 4608442 | 2020-10-13 12:33:42 +0200 | [diff] [blame] | 340 | } |
Ady Abraham | 6fb599b | 2020-03-05 13:48:22 -0800 | [diff] [blame] | 341 | } |
| 342 | |
Ady Abraham | dd5bfa9 | 2021-01-07 17:56:08 -0800 | [diff] [blame] | 343 | const bool hasExplicitVoteLayers = explicitDefaultVoteLayers > 0 || |
| 344 | explicitExactOrMultipleVoteLayers > 0 || explicitExact > 0; |
Alec Mouri | 11232a2 | 2020-05-14 18:06:25 -0700 | [diff] [blame] | 345 | |
Marin Shalamanov | 8cd8a99 | 2021-09-14 23:22:49 +0200 | [diff] [blame] | 346 | const Policy* policy = getCurrentPolicyLocked(); |
Dominik Laskowski | b0054a2 | 2022-03-03 09:03:06 -0800 | [diff] [blame] | 347 | const auto& defaultMode = mDisplayModes.get(policy->defaultMode)->get(); |
Dominik Laskowski | f8734e0 | 2022-08-26 09:06:59 -0700 | [diff] [blame] | 348 | |
Marin Shalamanov | 8cd8a99 | 2021-09-14 23:22:49 +0200 | [diff] [blame] | 349 | // If the default mode group is different from the group of current mode, |
| 350 | // this means a layer requesting a seamed mode switch just disappeared and |
| 351 | // we should switch back to the default group. |
| 352 | // However if a seamed layer is still present we anchor around the group |
| 353 | // of the current mode, in order to prevent unnecessary seamed mode switches |
| 354 | // (e.g. when pausing a video playback). |
Dominik Laskowski | b0054a2 | 2022-03-03 09:03:06 -0800 | [diff] [blame] | 355 | const auto anchorGroup = |
Dominik Laskowski | f8734e0 | 2022-08-26 09:06:59 -0700 | [diff] [blame] | 356 | seamedFocusedLayers > 0 ? activeMode.getGroup() : defaultMode->getGroup(); |
Marin Shalamanov | 8cd8a99 | 2021-09-14 23:22:49 +0200 | [diff] [blame] | 357 | |
Steven Thomas | f734df4 | 2020-04-13 21:09:28 -0700 | [diff] [blame] | 358 | // Consider the touch event if there are no Explicit* layers. Otherwise wait until after we've |
| 359 | // selected a refresh rate to see if we should apply touch boost. |
Dominik Laskowski | a8626ec | 2021-12-15 18:13:30 -0800 | [diff] [blame] | 360 | if (signals.touch && !hasExplicitVoteLayers) { |
ramindani | d72ba16 | 2022-09-09 21:33:40 +0000 | [diff] [blame] | 361 | ALOGV("Touch Boost"); |
| 362 | return {getRefreshRatesByPolicyLocked(anchorGroup, RefreshRateOrder::Descending), |
| 363 | GlobalSignals{.touch = true}}; |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 364 | } |
| 365 | |
Alec Mouri | 11232a2 | 2020-05-14 18:06:25 -0700 | [diff] [blame] | 366 | // If the primary range consists of a single refresh rate then we can only |
| 367 | // move out the of range if layers explicitly request a different refresh |
| 368 | // rate. |
Marin Shalamanov | e8a663d | 2020-11-24 17:48:00 +0100 | [diff] [blame] | 369 | const bool primaryRangeIsSingleRate = |
Dominik Laskowski | 6eab42d | 2021-09-13 14:34:13 -0700 | [diff] [blame] | 370 | isApproxEqual(policy->primaryRange.min, policy->primaryRange.max); |
Alec Mouri | 11232a2 | 2020-05-14 18:06:25 -0700 | [diff] [blame] | 371 | |
Dominik Laskowski | a8626ec | 2021-12-15 18:13:30 -0800 | [diff] [blame] | 372 | if (!signals.touch && signals.idle && !(primaryRangeIsSingleRate && hasExplicitVoteLayers)) { |
ramindani | d72ba16 | 2022-09-09 21:33:40 +0000 | [diff] [blame] | 373 | ALOGV("Idle"); |
| 374 | return {getRefreshRatesByPolicyLocked(activeMode.getGroup(), RefreshRateOrder::Ascending), |
| 375 | GlobalSignals{.idle = true}}; |
Steven Thomas | bb37432 | 2020-04-28 22:47:16 -0700 | [diff] [blame] | 376 | } |
| 377 | |
Steven Thomas | debafed | 2020-05-18 17:30:35 -0700 | [diff] [blame] | 378 | if (layers.empty() || noVoteLayers == layers.size()) { |
ramindani | d72ba16 | 2022-09-09 21:33:40 +0000 | [diff] [blame] | 379 | ALOGV("No layers with votes"); |
| 380 | return {getRefreshRatesByPolicyLocked(anchorGroup, RefreshRateOrder::Descending), |
| 381 | kNoSignals}; |
Steven Thomas | bb37432 | 2020-04-28 22:47:16 -0700 | [diff] [blame] | 382 | } |
| 383 | |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 384 | // Only if all layers want Min we should return Min |
| 385 | if (noVoteLayers + minVoteLayers == layers.size()) { |
ramindani | d72ba16 | 2022-09-09 21:33:40 +0000 | [diff] [blame] | 386 | ALOGV("All layers Min"); |
| 387 | return {getRefreshRatesByPolicyLocked(activeMode.getGroup(), RefreshRateOrder::Ascending), |
| 388 | kNoSignals}; |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 389 | } |
| 390 | |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 391 | // Find the best refresh rate based on score |
Ady Abraham | 62a0be2 | 2020-12-08 16:54:10 -0800 | [diff] [blame] | 392 | std::vector<RefreshRateScore> scores; |
Steven Thomas | f734df4 | 2020-04-13 21:09:28 -0700 | [diff] [blame] | 393 | scores.reserve(mAppRequestRefreshRates.size()); |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 394 | |
Dominik Laskowski | b0054a2 | 2022-03-03 09:03:06 -0800 | [diff] [blame] | 395 | for (const DisplayModeIterator modeIt : mAppRequestRefreshRates) { |
| 396 | scores.emplace_back(RefreshRateScore{modeIt, 0.0f}); |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 397 | } |
| 398 | |
| 399 | for (const auto& layer : layers) { |
rnlee | 3bd61066 | 2021-06-23 16:27:57 -0700 | [diff] [blame] | 400 | ALOGV("Calculating score for %s (%s, weight %.2f, desired %.2f) ", layer.name.c_str(), |
Dominik Laskowski | f5d0ea5 | 2021-09-26 17:27:01 -0700 | [diff] [blame] | 401 | ftl::enum_string(layer.vote).c_str(), layer.weight, |
rnlee | 3bd61066 | 2021-06-23 16:27:57 -0700 | [diff] [blame] | 402 | layer.desiredRefreshRate.getValue()); |
Ady Abraham | 4ccdcb4 | 2020-02-11 17:34:34 -0800 | [diff] [blame] | 403 | if (layer.vote == LayerVoteType::NoVote || layer.vote == LayerVoteType::Min) { |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 404 | continue; |
| 405 | } |
| 406 | |
Dominik Laskowski | b0054a2 | 2022-03-03 09:03:06 -0800 | [diff] [blame] | 407 | const auto weight = layer.weight; |
Ady Abraham | 71c437d | 2020-01-31 15:56:57 -0800 | [diff] [blame] | 408 | |
Ady Abraham | 62f51d9 | 2022-08-24 22:20:22 +0000 | [diff] [blame] | 409 | for (auto& [modeIt, overallScore, fixedRateBelowThresholdLayersScore] : scores) { |
Dominik Laskowski | b0054a2 | 2022-03-03 09:03:06 -0800 | [diff] [blame] | 410 | const auto& [id, mode] = *modeIt; |
Dominik Laskowski | f8734e0 | 2022-08-26 09:06:59 -0700 | [diff] [blame] | 411 | const bool isSeamlessSwitch = mode->getGroup() == activeMode.getGroup(); |
Marin Shalamanov | 4608442 | 2020-10-13 12:33:42 +0200 | [diff] [blame] | 412 | |
Marin Shalamanov | 53fc11d | 2020-11-20 14:00:13 +0100 | [diff] [blame] | 413 | if (layer.seamlessness == Seamlessness::OnlySeamless && !isSeamlessSwitch) { |
Marin Shalamanov | a7fe304 | 2021-01-29 21:02:08 +0100 | [diff] [blame] | 414 | ALOGV("%s ignores %s to avoid non-seamless switch. Current mode = %s", |
Dominik Laskowski | b0054a2 | 2022-03-03 09:03:06 -0800 | [diff] [blame] | 415 | formatLayerInfo(layer, weight).c_str(), to_string(*mode).c_str(), |
Dominik Laskowski | f8734e0 | 2022-08-26 09:06:59 -0700 | [diff] [blame] | 416 | to_string(activeMode).c_str()); |
Marin Shalamanov | 4608442 | 2020-10-13 12:33:42 +0200 | [diff] [blame] | 417 | continue; |
| 418 | } |
| 419 | |
Marin Shalamanov | 53fc11d | 2020-11-20 14:00:13 +0100 | [diff] [blame] | 420 | if (layer.seamlessness == Seamlessness::SeamedAndSeamless && !isSeamlessSwitch && |
| 421 | !layer.focused) { |
| 422 | ALOGV("%s ignores %s because it's not focused and the switch is going to be seamed." |
Marin Shalamanov | a7fe304 | 2021-01-29 21:02:08 +0100 | [diff] [blame] | 423 | " Current mode = %s", |
Dominik Laskowski | b0054a2 | 2022-03-03 09:03:06 -0800 | [diff] [blame] | 424 | formatLayerInfo(layer, weight).c_str(), to_string(*mode).c_str(), |
Dominik Laskowski | f8734e0 | 2022-08-26 09:06:59 -0700 | [diff] [blame] | 425 | to_string(activeMode).c_str()); |
Marin Shalamanov | 53fc11d | 2020-11-20 14:00:13 +0100 | [diff] [blame] | 426 | continue; |
| 427 | } |
| 428 | |
Marin Shalamanov | a7fe304 | 2021-01-29 21:02:08 +0100 | [diff] [blame] | 429 | // Layers with default seamlessness vote for the current mode group if |
Marin Shalamanov | 53fc11d | 2020-11-20 14:00:13 +0100 | [diff] [blame] | 430 | // there are layers with seamlessness=SeamedAndSeamless and for the default |
Marin Shalamanov | a7fe304 | 2021-01-29 21:02:08 +0100 | [diff] [blame] | 431 | // mode group otherwise. In second case, if the current mode group is different |
Marin Shalamanov | 53fc11d | 2020-11-20 14:00:13 +0100 | [diff] [blame] | 432 | // from the default, this means a layer with seamlessness=SeamedAndSeamless has just |
| 433 | // disappeared. |
Dominik Laskowski | b0054a2 | 2022-03-03 09:03:06 -0800 | [diff] [blame] | 434 | const bool isInPolicyForDefault = mode->getGroup() == anchorGroup; |
Marin Shalamanov | ae0b535 | 2021-03-24 12:56:08 +0100 | [diff] [blame] | 435 | if (layer.seamlessness == Seamlessness::Default && !isInPolicyForDefault) { |
Marin Shalamanov | a7fe304 | 2021-01-29 21:02:08 +0100 | [diff] [blame] | 436 | ALOGV("%s ignores %s. Current mode = %s", formatLayerInfo(layer, weight).c_str(), |
Dominik Laskowski | f8734e0 | 2022-08-26 09:06:59 -0700 | [diff] [blame] | 437 | to_string(*mode).c_str(), to_string(activeMode).c_str()); |
Marin Shalamanov | 4608442 | 2020-10-13 12:33:42 +0200 | [diff] [blame] | 438 | continue; |
| 439 | } |
| 440 | |
Dominik Laskowski | b0054a2 | 2022-03-03 09:03:06 -0800 | [diff] [blame] | 441 | const bool inPrimaryRange = policy->primaryRange.includes(mode->getFps()); |
Alec Mouri | 11232a2 | 2020-05-14 18:06:25 -0700 | [diff] [blame] | 442 | if ((primaryRangeIsSingleRate || !inPrimaryRange) && |
Ady Abraham | dd5bfa9 | 2021-01-07 17:56:08 -0800 | [diff] [blame] | 443 | !(layer.focused && |
| 444 | (layer.vote == LayerVoteType::ExplicitDefault || |
| 445 | layer.vote == LayerVoteType::ExplicitExact))) { |
Ady Abraham | 20c029c | 2020-07-06 12:58:05 -0700 | [diff] [blame] | 446 | // Only focused layers with ExplicitDefault frame rate settings are allowed to score |
Ady Abraham | aae5ed5 | 2020-06-26 09:32:43 -0700 | [diff] [blame] | 447 | // refresh rates outside the primary range. |
Steven Thomas | f734df4 | 2020-04-13 21:09:28 -0700 | [diff] [blame] | 448 | continue; |
| 449 | } |
| 450 | |
Ady Abraham | ae2e3c7 | 2022-08-13 05:12:13 +0000 | [diff] [blame] | 451 | const float layerScore = |
Dominik Laskowski | b0054a2 | 2022-03-03 09:03:06 -0800 | [diff] [blame] | 452 | calculateLayerScoreLocked(layer, mode->getFps(), isSeamlessSwitch); |
Ady Abraham | ae2e3c7 | 2022-08-13 05:12:13 +0000 | [diff] [blame] | 453 | const float weightedLayerScore = weight * layerScore; |
Dominik Laskowski | b0054a2 | 2022-03-03 09:03:06 -0800 | [diff] [blame] | 454 | |
Ady Abraham | 13cfb36 | 2022-08-13 05:12:13 +0000 | [diff] [blame] | 455 | // Layer with fixed source has a special consideration which depends on the |
Ady Abraham | ae2e3c7 | 2022-08-13 05:12:13 +0000 | [diff] [blame] | 456 | // mConfig.frameRateMultipleThreshold. We don't want these layers to score |
| 457 | // refresh rates above the threshold, but we also don't want to favor the lower |
| 458 | // ones by having a greater number of layers scoring them. Instead, we calculate |
| 459 | // the score independently for these layers and later decide which |
| 460 | // refresh rates to add it. For example, desired 24 fps with 120 Hz threshold should not |
| 461 | // score 120 Hz, but desired 60 fps should contribute to the score. |
| 462 | const bool fixedSourceLayer = [](LayerVoteType vote) { |
| 463 | switch (vote) { |
| 464 | case LayerVoteType::ExplicitExactOrMultiple: |
| 465 | case LayerVoteType::Heuristic: |
| 466 | return true; |
| 467 | case LayerVoteType::NoVote: |
| 468 | case LayerVoteType::Min: |
| 469 | case LayerVoteType::Max: |
| 470 | case LayerVoteType::ExplicitDefault: |
| 471 | case LayerVoteType::ExplicitExact: |
| 472 | return false; |
| 473 | } |
| 474 | }(layer.vote); |
Ady Abraham | 62f51d9 | 2022-08-24 22:20:22 +0000 | [diff] [blame] | 475 | const bool layerBelowThreshold = mConfig.frameRateMultipleThreshold != 0 && |
Ady Abraham | ae2e3c7 | 2022-08-13 05:12:13 +0000 | [diff] [blame] | 476 | layer.desiredRefreshRate < |
| 477 | Fps::fromValue(mConfig.frameRateMultipleThreshold / 2); |
Ady Abraham | 62f51d9 | 2022-08-24 22:20:22 +0000 | [diff] [blame] | 478 | if (fixedSourceLayer && layerBelowThreshold) { |
Ady Abraham | 13cfb36 | 2022-08-13 05:12:13 +0000 | [diff] [blame] | 479 | const bool modeAboveThreshold = |
| 480 | mode->getFps() >= Fps::fromValue(mConfig.frameRateMultipleThreshold); |
Ady Abraham | 62f51d9 | 2022-08-24 22:20:22 +0000 | [diff] [blame] | 481 | if (modeAboveThreshold) { |
Ady Abraham | ae2e3c7 | 2022-08-13 05:12:13 +0000 | [diff] [blame] | 482 | ALOGV("%s gives %s fixed source (above threshold) score of %.4f", |
| 483 | formatLayerInfo(layer, weight).c_str(), to_string(mode->getFps()).c_str(), |
| 484 | layerScore); |
Ady Abraham | 62f51d9 | 2022-08-24 22:20:22 +0000 | [diff] [blame] | 485 | fixedRateBelowThresholdLayersScore.modeAboveThreshold += weightedLayerScore; |
Ady Abraham | ae2e3c7 | 2022-08-13 05:12:13 +0000 | [diff] [blame] | 486 | } else { |
| 487 | ALOGV("%s gives %s fixed source (below threshold) score of %.4f", |
| 488 | formatLayerInfo(layer, weight).c_str(), to_string(mode->getFps()).c_str(), |
| 489 | layerScore); |
Ady Abraham | 62f51d9 | 2022-08-24 22:20:22 +0000 | [diff] [blame] | 490 | fixedRateBelowThresholdLayersScore.modeBelowThreshold += weightedLayerScore; |
Ady Abraham | ae2e3c7 | 2022-08-13 05:12:13 +0000 | [diff] [blame] | 491 | } |
| 492 | } else { |
| 493 | ALOGV("%s gives %s score of %.4f", formatLayerInfo(layer, weight).c_str(), |
| 494 | to_string(mode->getFps()).c_str(), layerScore); |
| 495 | overallScore += weightedLayerScore; |
| 496 | } |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 497 | } |
| 498 | } |
| 499 | |
Ady Abraham | ae2e3c7 | 2022-08-13 05:12:13 +0000 | [diff] [blame] | 500 | // We want to find the best refresh rate without the fixed source layers, |
Ady Abraham | 62f51d9 | 2022-08-24 22:20:22 +0000 | [diff] [blame] | 501 | // so we could know whether we should add the modeAboveThreshold scores or not. |
Ady Abraham | ae2e3c7 | 2022-08-13 05:12:13 +0000 | [diff] [blame] | 502 | // If the best refresh rate is already above the threshold, it means that |
| 503 | // some non-fixed source layers already scored it, so we can just add the score |
| 504 | // for all fixed source layers, even the ones that are above the threshold. |
| 505 | const bool maxScoreAboveThreshold = [&] { |
| 506 | if (mConfig.frameRateMultipleThreshold == 0 || scores.empty()) { |
| 507 | return false; |
| 508 | } |
| 509 | |
| 510 | const auto maxScoreIt = |
| 511 | std::max_element(scores.begin(), scores.end(), |
| 512 | [](RefreshRateScore max, RefreshRateScore current) { |
| 513 | const auto& [modeIt, overallScore, _] = current; |
| 514 | return overallScore > max.overallScore; |
| 515 | }); |
| 516 | ALOGV("%s is the best refresh rate without fixed source layers. It is %s the threshold for " |
| 517 | "refresh rate multiples", |
| 518 | to_string(maxScoreIt->modeIt->second->getFps()).c_str(), |
| 519 | maxScoreAboveThreshold ? "above" : "below"); |
| 520 | return maxScoreIt->modeIt->second->getFps() >= |
| 521 | Fps::fromValue(mConfig.frameRateMultipleThreshold); |
| 522 | }(); |
| 523 | |
| 524 | // Now we can add the fixed rate layers score |
Ady Abraham | 62f51d9 | 2022-08-24 22:20:22 +0000 | [diff] [blame] | 525 | for (auto& [modeIt, overallScore, fixedRateBelowThresholdLayersScore] : scores) { |
| 526 | overallScore += fixedRateBelowThresholdLayersScore.modeBelowThreshold; |
Ady Abraham | ae2e3c7 | 2022-08-13 05:12:13 +0000 | [diff] [blame] | 527 | if (maxScoreAboveThreshold) { |
Ady Abraham | 62f51d9 | 2022-08-24 22:20:22 +0000 | [diff] [blame] | 528 | overallScore += fixedRateBelowThresholdLayersScore.modeAboveThreshold; |
Ady Abraham | ae2e3c7 | 2022-08-13 05:12:13 +0000 | [diff] [blame] | 529 | } |
| 530 | ALOGV("%s adjusted overallScore is %.4f", to_string(modeIt->second->getFps()).c_str(), |
| 531 | overallScore); |
| 532 | } |
| 533 | |
| 534 | // Now that we scored all the refresh rates we need to pick the one that got the highest |
ramindani | d72ba16 | 2022-09-09 21:33:40 +0000 | [diff] [blame] | 535 | // overallScore. Sort the scores based on their overallScore in descending order of priority. |
| 536 | const RefreshRateOrder refreshRateOrder = |
| 537 | maxVoteLayers > 0 ? RefreshRateOrder::Descending : RefreshRateOrder::Ascending; |
| 538 | std::sort(scores.begin(), scores.end(), |
| 539 | RefreshRateScoreComparator{.refreshRateOrder = refreshRateOrder}); |
| 540 | std::vector<RefreshRateRanking> rankedRefreshRates; |
| 541 | rankedRefreshRates.reserve(scores.size()); |
| 542 | |
| 543 | std::transform(scores.begin(), scores.end(), back_inserter(rankedRefreshRates), |
| 544 | [](const RefreshRateScore& score) { |
| 545 | return RefreshRateRanking{score.modeIt->second, score.overallScore}; |
| 546 | }); |
Ady Abraham | 3470210 | 2020-02-10 14:12:05 -0800 | [diff] [blame] | 547 | |
Alec Mouri | 11232a2 | 2020-05-14 18:06:25 -0700 | [diff] [blame] | 548 | if (primaryRangeIsSingleRate) { |
| 549 | // If we never scored any layers, then choose the rate from the primary |
| 550 | // range instead of picking a random score from the app range. |
| 551 | if (std::all_of(scores.begin(), scores.end(), |
Ady Abraham | ae2e3c7 | 2022-08-13 05:12:13 +0000 | [diff] [blame] | 552 | [](RefreshRateScore score) { return score.overallScore == 0; })) { |
ramindani | d72ba16 | 2022-09-09 21:33:40 +0000 | [diff] [blame] | 553 | ALOGV("Layers not scored"); |
| 554 | return {getRefreshRatesByPolicyLocked(anchorGroup, RefreshRateOrder::Descending), |
| 555 | kNoSignals}; |
Alec Mouri | 11232a2 | 2020-05-14 18:06:25 -0700 | [diff] [blame] | 556 | } else { |
ramindani | d72ba16 | 2022-09-09 21:33:40 +0000 | [diff] [blame] | 557 | return {rankedRefreshRates, kNoSignals}; |
Alec Mouri | 11232a2 | 2020-05-14 18:06:25 -0700 | [diff] [blame] | 558 | } |
| 559 | } |
| 560 | |
Steven Thomas | f734df4 | 2020-04-13 21:09:28 -0700 | [diff] [blame] | 561 | // Consider the touch event if there are no ExplicitDefault layers. ExplicitDefault are mostly |
| 562 | // interactive (as opposed to ExplicitExactOrMultiple) and therefore if those posted an explicit |
| 563 | // vote we should not change it if we get a touch event. Only apply touch boost if it will |
| 564 | // actually increase the refresh rate over the normal selection. |
Ady Abraham | 5e4e983 | 2021-06-14 13:40:56 -0700 | [diff] [blame] | 565 | const bool touchBoostForExplicitExact = [&] { |
Andy Yu | 2ae6b6b | 2021-11-18 14:51:06 -0800 | [diff] [blame] | 566 | if (mSupportsFrameRateOverrideByContent) { |
Ady Abraham | 5e4e983 | 2021-06-14 13:40:56 -0700 | [diff] [blame] | 567 | // Enable touch boost if there are other layers besides exact |
| 568 | return explicitExact + noVoteLayers != layers.size(); |
| 569 | } else { |
| 570 | // Enable touch boost if there are no exact layers |
| 571 | return explicitExact == 0; |
| 572 | } |
| 573 | }(); |
Dominik Laskowski | 6eab42d | 2021-09-13 14:34:13 -0700 | [diff] [blame] | 574 | |
ramindani | d72ba16 | 2022-09-09 21:33:40 +0000 | [diff] [blame] | 575 | const auto& touchRefreshRates = |
| 576 | getRefreshRatesByPolicyLocked(anchorGroup, RefreshRateOrder::Descending); |
Dominik Laskowski | 6eab42d | 2021-09-13 14:34:13 -0700 | [diff] [blame] | 577 | using fps_approx_ops::operator<; |
| 578 | |
Dominik Laskowski | a8626ec | 2021-12-15 18:13:30 -0800 | [diff] [blame] | 579 | if (signals.touch && explicitDefaultVoteLayers == 0 && touchBoostForExplicitExact && |
ramindani | d72ba16 | 2022-09-09 21:33:40 +0000 | [diff] [blame] | 580 | scores.front().modeIt->second->getFps() < |
| 581 | touchRefreshRates.front().displayModePtr->getFps()) { |
| 582 | ALOGV("Touch Boost"); |
| 583 | return {touchRefreshRates, GlobalSignals{.touch = true}}; |
Steven Thomas | f734df4 | 2020-04-13 21:09:28 -0700 | [diff] [blame] | 584 | } |
| 585 | |
ramindani | d72ba16 | 2022-09-09 21:33:40 +0000 | [diff] [blame] | 586 | return {rankedRefreshRates, kNoSignals}; |
Ady Abraham | 3470210 | 2020-02-10 14:12:05 -0800 | [diff] [blame] | 587 | } |
| 588 | |
Ady Abraham | 62a0be2 | 2020-12-08 16:54:10 -0800 | [diff] [blame] | 589 | std::unordered_map<uid_t, std::vector<const RefreshRateConfigs::LayerRequirement*>> |
| 590 | groupLayersByUid(const std::vector<RefreshRateConfigs::LayerRequirement>& layers) { |
| 591 | std::unordered_map<uid_t, std::vector<const RefreshRateConfigs::LayerRequirement*>> layersByUid; |
| 592 | for (const auto& layer : layers) { |
| 593 | auto iter = layersByUid.emplace(layer.ownerUid, |
| 594 | std::vector<const RefreshRateConfigs::LayerRequirement*>()); |
| 595 | auto& layersWithSameUid = iter.first->second; |
| 596 | layersWithSameUid.push_back(&layer); |
| 597 | } |
| 598 | |
| 599 | // Remove uids that can't have a frame rate override |
| 600 | for (auto iter = layersByUid.begin(); iter != layersByUid.end();) { |
| 601 | const auto& layersWithSameUid = iter->second; |
| 602 | bool skipUid = false; |
| 603 | for (const auto& layer : layersWithSameUid) { |
| 604 | if (layer->vote == RefreshRateConfigs::LayerVoteType::Max || |
| 605 | layer->vote == RefreshRateConfigs::LayerVoteType::Heuristic) { |
| 606 | skipUid = true; |
| 607 | break; |
| 608 | } |
| 609 | } |
| 610 | if (skipUid) { |
| 611 | iter = layersByUid.erase(iter); |
| 612 | } else { |
| 613 | ++iter; |
| 614 | } |
| 615 | } |
| 616 | |
| 617 | return layersByUid; |
| 618 | } |
| 619 | |
Ady Abraham | 62a0be2 | 2020-12-08 16:54:10 -0800 | [diff] [blame] | 620 | RefreshRateConfigs::UidToFrameRateOverride RefreshRateConfigs::getFrameRateOverrides( |
Dominik Laskowski | b0054a2 | 2022-03-03 09:03:06 -0800 | [diff] [blame] | 621 | const std::vector<LayerRequirement>& layers, Fps displayRefreshRate, |
Dominik Laskowski | 6eab42d | 2021-09-13 14:34:13 -0700 | [diff] [blame] | 622 | GlobalSignals globalSignals) const { |
Ady Abraham | 62a0be2 | 2020-12-08 16:54:10 -0800 | [diff] [blame] | 623 | ATRACE_CALL(); |
Ady Abraham | 62a0be2 | 2020-12-08 16:54:10 -0800 | [diff] [blame] | 624 | |
Dominik Laskowski | b0054a2 | 2022-03-03 09:03:06 -0800 | [diff] [blame] | 625 | ALOGV("%s: %zu layers", __func__, layers.size()); |
| 626 | |
Ady Abraham | 62a0be2 | 2020-12-08 16:54:10 -0800 | [diff] [blame] | 627 | std::lock_guard lock(mLock); |
Dominik Laskowski | b0054a2 | 2022-03-03 09:03:06 -0800 | [diff] [blame] | 628 | |
| 629 | std::vector<RefreshRateScore> scores; |
| 630 | scores.reserve(mDisplayModes.size()); |
| 631 | |
| 632 | for (auto it = mDisplayModes.begin(); it != mDisplayModes.end(); ++it) { |
| 633 | scores.emplace_back(RefreshRateScore{it, 0.0f}); |
| 634 | } |
| 635 | |
| 636 | std::sort(scores.begin(), scores.end(), [](const auto& lhs, const auto& rhs) { |
| 637 | const auto& mode1 = lhs.modeIt->second; |
| 638 | const auto& mode2 = rhs.modeIt->second; |
| 639 | return isStrictlyLess(mode1->getFps(), mode2->getFps()); |
| 640 | }); |
| 641 | |
Ady Abraham | 62a0be2 | 2020-12-08 16:54:10 -0800 | [diff] [blame] | 642 | std::unordered_map<uid_t, std::vector<const LayerRequirement*>> layersByUid = |
| 643 | groupLayersByUid(layers); |
| 644 | UidToFrameRateOverride frameRateOverrides; |
| 645 | for (const auto& [uid, layersWithSameUid] : layersByUid) { |
Ady Abraham | dd5bfa9 | 2021-01-07 17:56:08 -0800 | [diff] [blame] | 646 | // Layers with ExplicitExactOrMultiple expect touch boost |
| 647 | const bool hasExplicitExactOrMultiple = |
| 648 | std::any_of(layersWithSameUid.cbegin(), layersWithSameUid.cend(), |
| 649 | [](const auto& layer) { |
| 650 | return layer->vote == LayerVoteType::ExplicitExactOrMultiple; |
| 651 | }); |
| 652 | |
Dominik Laskowski | 6eab42d | 2021-09-13 14:34:13 -0700 | [diff] [blame] | 653 | if (globalSignals.touch && hasExplicitExactOrMultiple) { |
Ady Abraham | dd5bfa9 | 2021-01-07 17:56:08 -0800 | [diff] [blame] | 654 | continue; |
| 655 | } |
| 656 | |
Ady Abraham | ae2e3c7 | 2022-08-13 05:12:13 +0000 | [diff] [blame] | 657 | for (auto& [_, score, _1] : scores) { |
Dominik Laskowski | b0054a2 | 2022-03-03 09:03:06 -0800 | [diff] [blame] | 658 | score = 0; |
Ady Abraham | 62a0be2 | 2020-12-08 16:54:10 -0800 | [diff] [blame] | 659 | } |
| 660 | |
| 661 | for (const auto& layer : layersWithSameUid) { |
| 662 | if (layer->vote == LayerVoteType::NoVote || layer->vote == LayerVoteType::Min) { |
| 663 | continue; |
| 664 | } |
| 665 | |
| 666 | LOG_ALWAYS_FATAL_IF(layer->vote != LayerVoteType::ExplicitDefault && |
Ady Abraham | dd5bfa9 | 2021-01-07 17:56:08 -0800 | [diff] [blame] | 667 | layer->vote != LayerVoteType::ExplicitExactOrMultiple && |
| 668 | layer->vote != LayerVoteType::ExplicitExact); |
Ady Abraham | ae2e3c7 | 2022-08-13 05:12:13 +0000 | [diff] [blame] | 669 | for (auto& [modeIt, score, _] : scores) { |
Dominik Laskowski | b0054a2 | 2022-03-03 09:03:06 -0800 | [diff] [blame] | 670 | constexpr bool isSeamlessSwitch = true; |
| 671 | const auto layerScore = calculateLayerScoreLocked(*layer, modeIt->second->getFps(), |
| 672 | isSeamlessSwitch); |
| 673 | score += layer->weight * layerScore; |
Ady Abraham | 62a0be2 | 2020-12-08 16:54:10 -0800 | [diff] [blame] | 674 | } |
| 675 | } |
| 676 | |
Ady Abraham | cc31549 | 2022-02-17 17:06:39 -0800 | [diff] [blame] | 677 | // We just care about the refresh rates which are a divisor of the |
Ady Abraham | 62a0be2 | 2020-12-08 16:54:10 -0800 | [diff] [blame] | 678 | // display refresh rate |
Dominik Laskowski | b0054a2 | 2022-03-03 09:03:06 -0800 | [diff] [blame] | 679 | const auto it = std::remove_if(scores.begin(), scores.end(), [&](RefreshRateScore score) { |
| 680 | const auto& [id, mode] = *score.modeIt; |
| 681 | return getFrameRateDivisor(displayRefreshRate, mode->getFps()) == 0; |
| 682 | }); |
| 683 | scores.erase(it, scores.end()); |
Ady Abraham | 62a0be2 | 2020-12-08 16:54:10 -0800 | [diff] [blame] | 684 | |
| 685 | // If we never scored any layers, we don't have a preferred frame rate |
| 686 | if (std::all_of(scores.begin(), scores.end(), |
Ady Abraham | ae2e3c7 | 2022-08-13 05:12:13 +0000 | [diff] [blame] | 687 | [](RefreshRateScore score) { return score.overallScore == 0; })) { |
Ady Abraham | 62a0be2 | 2020-12-08 16:54:10 -0800 | [diff] [blame] | 688 | continue; |
| 689 | } |
| 690 | |
ramindani | d72ba16 | 2022-09-09 21:33:40 +0000 | [diff] [blame] | 691 | // Now that we scored all the refresh rates we need to pick the lowest refresh rate |
| 692 | // that got the highest score. |
Dominik Laskowski | b0054a2 | 2022-03-03 09:03:06 -0800 | [diff] [blame] | 693 | const DisplayModePtr& bestRefreshRate = |
ramindani | d72ba16 | 2022-09-09 21:33:40 +0000 | [diff] [blame] | 694 | std::min_element(scores.begin(), scores.end(), |
| 695 | RefreshRateScoreComparator{.refreshRateOrder = |
| 696 | RefreshRateOrder::Ascending}) |
| 697 | ->modeIt->second; |
Ady Abraham | 5cc2e26 | 2021-03-25 13:09:17 -0700 | [diff] [blame] | 698 | frameRateOverrides.emplace(uid, bestRefreshRate->getFps()); |
Ady Abraham | 62a0be2 | 2020-12-08 16:54:10 -0800 | [diff] [blame] | 699 | } |
| 700 | |
| 701 | return frameRateOverrides; |
| 702 | } |
| 703 | |
Marin Shalamanov | eadf2e7 | 2020-12-10 15:35:28 +0100 | [diff] [blame] | 704 | std::optional<Fps> RefreshRateConfigs::onKernelTimerChanged( |
Dominik Laskowski | b0054a2 | 2022-03-03 09:03:06 -0800 | [diff] [blame] | 705 | std::optional<DisplayModeId> desiredActiveModeId, bool timerExpired) const { |
Ady Abraham | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame] | 706 | std::lock_guard lock(mLock); |
Marin Shalamanov | eadf2e7 | 2020-12-10 15:35:28 +0100 | [diff] [blame] | 707 | |
Dominik Laskowski | b0054a2 | 2022-03-03 09:03:06 -0800 | [diff] [blame] | 708 | const DisplayModePtr& current = desiredActiveModeId |
| 709 | ? mDisplayModes.get(*desiredActiveModeId)->get() |
Dominik Laskowski | f8734e0 | 2022-08-26 09:06:59 -0700 | [diff] [blame] | 710 | : getActiveModeItLocked()->second; |
Marin Shalamanov | eadf2e7 | 2020-12-10 15:35:28 +0100 | [diff] [blame] | 711 | |
Dominik Laskowski | b0054a2 | 2022-03-03 09:03:06 -0800 | [diff] [blame] | 712 | const DisplayModePtr& min = mMinRefreshRateModeIt->second; |
| 713 | if (current == min) { |
| 714 | return {}; |
Marin Shalamanov | eadf2e7 | 2020-12-10 15:35:28 +0100 | [diff] [blame] | 715 | } |
| 716 | |
Dominik Laskowski | b0054a2 | 2022-03-03 09:03:06 -0800 | [diff] [blame] | 717 | const auto& mode = timerExpired ? min : current; |
| 718 | return mode->getFps(); |
Steven Thomas | f734df4 | 2020-04-13 21:09:28 -0700 | [diff] [blame] | 719 | } |
| 720 | |
Dominik Laskowski | b0054a2 | 2022-03-03 09:03:06 -0800 | [diff] [blame] | 721 | const DisplayModePtr& RefreshRateConfigs::getMinRefreshRateByPolicyLocked() const { |
Dominik Laskowski | f8734e0 | 2022-08-26 09:06:59 -0700 | [diff] [blame] | 722 | const auto& activeMode = *getActiveModeItLocked()->second; |
| 723 | |
Dominik Laskowski | b0054a2 | 2022-03-03 09:03:06 -0800 | [diff] [blame] | 724 | for (const DisplayModeIterator modeIt : mPrimaryRefreshRates) { |
| 725 | const auto& mode = modeIt->second; |
Dominik Laskowski | f8734e0 | 2022-08-26 09:06:59 -0700 | [diff] [blame] | 726 | if (activeMode.getGroup() == mode->getGroup()) { |
Dominik Laskowski | b0054a2 | 2022-03-03 09:03:06 -0800 | [diff] [blame] | 727 | return mode; |
Marin Shalamanov | 4608442 | 2020-10-13 12:33:42 +0200 | [diff] [blame] | 728 | } |
| 729 | } |
Dominik Laskowski | b0054a2 | 2022-03-03 09:03:06 -0800 | [diff] [blame] | 730 | |
Dominik Laskowski | f8734e0 | 2022-08-26 09:06:59 -0700 | [diff] [blame] | 731 | ALOGE("Can't find min refresh rate by policy with the same mode group as the current mode %s", |
| 732 | to_string(activeMode).c_str()); |
Dominik Laskowski | b0054a2 | 2022-03-03 09:03:06 -0800 | [diff] [blame] | 733 | |
| 734 | // Default to the lowest refresh rate. |
| 735 | return mPrimaryRefreshRates.front()->second; |
Ady Abraham | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame] | 736 | } |
| 737 | |
Dominik Laskowski | b0054a2 | 2022-03-03 09:03:06 -0800 | [diff] [blame] | 738 | const DisplayModePtr& RefreshRateConfigs::getMaxRefreshRateByPolicyLocked(int anchorGroup) const { |
| 739 | for (auto it = mPrimaryRefreshRates.rbegin(); it != mPrimaryRefreshRates.rend(); ++it) { |
| 740 | const auto& mode = (*it)->second; |
| 741 | if (anchorGroup == mode->getGroup()) { |
| 742 | return mode; |
Marin Shalamanov | 4608442 | 2020-10-13 12:33:42 +0200 | [diff] [blame] | 743 | } |
| 744 | } |
Dominik Laskowski | b0054a2 | 2022-03-03 09:03:06 -0800 | [diff] [blame] | 745 | |
ramindani | d72ba16 | 2022-09-09 21:33:40 +0000 | [diff] [blame] | 746 | ALOGE("Can't find max refresh rate by policy with the same group %d", anchorGroup); |
Dominik Laskowski | b0054a2 | 2022-03-03 09:03:06 -0800 | [diff] [blame] | 747 | |
| 748 | // Default to the highest refresh rate. |
| 749 | return mPrimaryRefreshRates.back()->second; |
Ady Abraham | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame] | 750 | } |
| 751 | |
ramindani | d72ba16 | 2022-09-09 21:33:40 +0000 | [diff] [blame] | 752 | std::vector<RefreshRateRanking> RefreshRateConfigs::getRefreshRatesByPolicyLocked( |
| 753 | std::optional<int> anchorGroupOpt, RefreshRateOrder refreshRateOrder) const { |
| 754 | std::vector<RefreshRateRanking> rankings; |
| 755 | const auto makeRanking = [&](const DisplayModeIterator it) REQUIRES(mLock) { |
| 756 | const auto& mode = it->second; |
| 757 | const bool inverseScore = (refreshRateOrder == RefreshRateOrder::Ascending); |
| 758 | const float score = calculateRefreshRateScoreForFps(mode->getFps()); |
| 759 | if (!anchorGroupOpt || mode->getGroup() == anchorGroupOpt) { |
| 760 | rankings.push_back(RefreshRateRanking{mode, inverseScore ? 1.0f / score : score}); |
| 761 | } |
| 762 | }; |
| 763 | |
| 764 | if (refreshRateOrder == RefreshRateOrder::Ascending) { |
| 765 | std::for_each(mPrimaryRefreshRates.begin(), mPrimaryRefreshRates.end(), makeRanking); |
| 766 | } else { |
| 767 | std::for_each(mPrimaryRefreshRates.rbegin(), mPrimaryRefreshRates.rend(), makeRanking); |
| 768 | } |
| 769 | |
| 770 | if (!rankings.empty() || !anchorGroupOpt) { |
| 771 | return rankings; |
| 772 | } |
| 773 | |
| 774 | ALOGW("Can't find %s refresh rate by policy with the same mode group" |
| 775 | " as the mode group %d", |
| 776 | refreshRateOrder == RefreshRateOrder::Ascending ? "min" : "max", anchorGroupOpt.value()); |
| 777 | |
| 778 | return getRefreshRatesByPolicyLocked(/*anchorGroupOpt*/ std::nullopt, refreshRateOrder); |
| 779 | } |
| 780 | |
Dominik Laskowski | f8734e0 | 2022-08-26 09:06:59 -0700 | [diff] [blame] | 781 | DisplayModePtr RefreshRateConfigs::getActiveModePtr() const { |
Ady Abraham | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame] | 782 | std::lock_guard lock(mLock); |
Dominik Laskowski | f8734e0 | 2022-08-26 09:06:59 -0700 | [diff] [blame] | 783 | return getActiveModeItLocked()->second; |
| 784 | } |
| 785 | |
| 786 | const DisplayMode& RefreshRateConfigs::getActiveMode() const { |
| 787 | // Reads from kMainThreadContext do not require mLock. |
| 788 | ftl::FakeGuard guard(mLock); |
| 789 | return *mActiveModeIt->second; |
| 790 | } |
| 791 | |
| 792 | DisplayModeIterator RefreshRateConfigs::getActiveModeItLocked() const { |
| 793 | // Reads under mLock do not require kMainThreadContext. |
| 794 | return FTL_FAKE_GUARD(kMainThreadContext, mActiveModeIt); |
Ady Abraham | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame] | 795 | } |
| 796 | |
Dominik Laskowski | b0054a2 | 2022-03-03 09:03:06 -0800 | [diff] [blame] | 797 | void RefreshRateConfigs::setActiveModeId(DisplayModeId modeId) { |
Ady Abraham | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame] | 798 | std::lock_guard lock(mLock); |
Marin Shalamanov | 4c7831e | 2021-06-08 20:44:06 +0200 | [diff] [blame] | 799 | |
ramindani | d72ba16 | 2022-09-09 21:33:40 +0000 | [diff] [blame] | 800 | // Invalidate the cached invocation to getRankedRefreshRates. This forces |
| 801 | // the refresh rate to be recomputed on the next call to getRankedRefreshRates. |
| 802 | mGetRankedRefreshRatesCache.reset(); |
Marin Shalamanov | 4c7831e | 2021-06-08 20:44:06 +0200 | [diff] [blame] | 803 | |
Dominik Laskowski | b0054a2 | 2022-03-03 09:03:06 -0800 | [diff] [blame] | 804 | mActiveModeIt = mDisplayModes.find(modeId); |
| 805 | LOG_ALWAYS_FATAL_IF(mActiveModeIt == mDisplayModes.end()); |
Ady Abraham | b4b1e0a | 2019-11-20 18:25:35 -0800 | [diff] [blame] | 806 | } |
| 807 | |
Dominik Laskowski | b0054a2 | 2022-03-03 09:03:06 -0800 | [diff] [blame] | 808 | RefreshRateConfigs::RefreshRateConfigs(DisplayModes modes, DisplayModeId activeModeId, |
rnlee | 3bd61066 | 2021-06-23 16:27:57 -0700 | [diff] [blame] | 809 | Config config) |
| 810 | : mKnownFrameRates(constructKnownFrameRates(modes)), mConfig(config) { |
Ady Abraham | 9a2ea34 | 2021-09-03 17:32:34 -0700 | [diff] [blame] | 811 | initializeIdleTimer(); |
Dominik Laskowski | f8734e0 | 2022-08-26 09:06:59 -0700 | [diff] [blame] | 812 | FTL_FAKE_GUARD(kMainThreadContext, updateDisplayModes(std::move(modes), activeModeId)); |
Marin Shalamanov | eadf2e7 | 2020-12-10 15:35:28 +0100 | [diff] [blame] | 813 | } |
| 814 | |
Ady Abraham | 9a2ea34 | 2021-09-03 17:32:34 -0700 | [diff] [blame] | 815 | void RefreshRateConfigs::initializeIdleTimer() { |
ramindani | 32cf060 | 2022-03-02 02:30:29 +0000 | [diff] [blame] | 816 | if (mConfig.idleTimerTimeout > 0ms) { |
Ady Abraham | 9a2ea34 | 2021-09-03 17:32:34 -0700 | [diff] [blame] | 817 | mIdleTimer.emplace( |
ramindani | 32cf060 | 2022-03-02 02:30:29 +0000 | [diff] [blame] | 818 | "IdleTimer", mConfig.idleTimerTimeout, |
Dominik Laskowski | 83bd771 | 2022-01-07 14:30:53 -0800 | [diff] [blame] | 819 | [this] { |
| 820 | std::scoped_lock lock(mIdleTimerCallbacksMutex); |
| 821 | if (const auto callbacks = getIdleTimerCallbacks()) { |
| 822 | callbacks->onReset(); |
| 823 | } |
Ady Abraham | 9a2ea34 | 2021-09-03 17:32:34 -0700 | [diff] [blame] | 824 | }, |
Dominik Laskowski | 83bd771 | 2022-01-07 14:30:53 -0800 | [diff] [blame] | 825 | [this] { |
| 826 | std::scoped_lock lock(mIdleTimerCallbacksMutex); |
| 827 | if (const auto callbacks = getIdleTimerCallbacks()) { |
| 828 | callbacks->onExpired(); |
| 829 | } |
Ady Abraham | 9a2ea34 | 2021-09-03 17:32:34 -0700 | [diff] [blame] | 830 | }); |
Ady Abraham | 9a2ea34 | 2021-09-03 17:32:34 -0700 | [diff] [blame] | 831 | } |
| 832 | } |
| 833 | |
Dominik Laskowski | b0054a2 | 2022-03-03 09:03:06 -0800 | [diff] [blame] | 834 | void RefreshRateConfigs::updateDisplayModes(DisplayModes modes, DisplayModeId activeModeId) { |
Marin Shalamanov | eadf2e7 | 2020-12-10 15:35:28 +0100 | [diff] [blame] | 835 | std::lock_guard lock(mLock); |
Marin Shalamanov | 4c7831e | 2021-06-08 20:44:06 +0200 | [diff] [blame] | 836 | |
ramindani | d72ba16 | 2022-09-09 21:33:40 +0000 | [diff] [blame] | 837 | // Invalidate the cached invocation to getRankedRefreshRates. This forces |
| 838 | // the refresh rate to be recomputed on the next call to getRankedRefreshRates. |
| 839 | mGetRankedRefreshRatesCache.reset(); |
Marin Shalamanov | 4c7831e | 2021-06-08 20:44:06 +0200 | [diff] [blame] | 840 | |
Dominik Laskowski | b0054a2 | 2022-03-03 09:03:06 -0800 | [diff] [blame] | 841 | mDisplayModes = std::move(modes); |
| 842 | mActiveModeIt = mDisplayModes.find(activeModeId); |
| 843 | LOG_ALWAYS_FATAL_IF(mActiveModeIt == mDisplayModes.end()); |
Ady Abraham | abc2760 | 2020-04-08 17:20:29 -0700 | [diff] [blame] | 844 | |
Dominik Laskowski | b0054a2 | 2022-03-03 09:03:06 -0800 | [diff] [blame] | 845 | const auto sortedModes = |
| 846 | sortByRefreshRate(mDisplayModes, [](const DisplayMode&) { return true; }); |
| 847 | mMinRefreshRateModeIt = sortedModes.front(); |
| 848 | mMaxRefreshRateModeIt = sortedModes.back(); |
| 849 | |
Marin Shalamanov | 75f3725 | 2021-02-10 21:43:57 +0100 | [diff] [blame] | 850 | // Reset the policy because the old one may no longer be valid. |
| 851 | mDisplayManagerPolicy = {}; |
Dominik Laskowski | b0054a2 | 2022-03-03 09:03:06 -0800 | [diff] [blame] | 852 | mDisplayManagerPolicy.defaultMode = activeModeId; |
Ady Abraham | 64c2fc0 | 2020-12-29 12:07:50 -0800 | [diff] [blame] | 853 | |
Dominik Laskowski | b0054a2 | 2022-03-03 09:03:06 -0800 | [diff] [blame] | 854 | mSupportsFrameRateOverrideByContent = |
| 855 | mConfig.enableFrameRateOverride && canModesSupportFrameRateOverride(sortedModes); |
Ady Abraham | 4899ff8 | 2021-01-06 13:53:29 -0800 | [diff] [blame] | 856 | |
Ady Abraham | abc2760 | 2020-04-08 17:20:29 -0700 | [diff] [blame] | 857 | constructAvailableRefreshRates(); |
Ady Abraham | b4b1e0a | 2019-11-20 18:25:35 -0800 | [diff] [blame] | 858 | } |
| 859 | |
Marin Shalamanov | eadf2e7 | 2020-12-10 15:35:28 +0100 | [diff] [blame] | 860 | bool RefreshRateConfigs::isPolicyValidLocked(const Policy& policy) const { |
Marin Shalamanov | a7fe304 | 2021-01-29 21:02:08 +0100 | [diff] [blame] | 861 | // defaultMode must be a valid mode, and within the given refresh rate range. |
Dominik Laskowski | b0054a2 | 2022-03-03 09:03:06 -0800 | [diff] [blame] | 862 | if (const auto mode = mDisplayModes.get(policy.defaultMode)) { |
| 863 | if (!policy.primaryRange.includes(mode->get()->getFps())) { |
| 864 | ALOGE("Default mode is not in the primary range."); |
| 865 | return false; |
| 866 | } |
| 867 | } else { |
Marin Shalamanov | a7fe304 | 2021-01-29 21:02:08 +0100 | [diff] [blame] | 868 | ALOGE("Default mode is not found."); |
Steven Thomas | d407190 | 2020-03-24 16:02:53 -0700 | [diff] [blame] | 869 | return false; |
| 870 | } |
Dominik Laskowski | 6eab42d | 2021-09-13 14:34:13 -0700 | [diff] [blame] | 871 | |
| 872 | using namespace fps_approx_ops; |
| 873 | return policy.appRequestRange.min <= policy.primaryRange.min && |
| 874 | policy.appRequestRange.max >= policy.primaryRange.max; |
Steven Thomas | d407190 | 2020-03-24 16:02:53 -0700 | [diff] [blame] | 875 | } |
| 876 | |
| 877 | status_t RefreshRateConfigs::setDisplayManagerPolicy(const Policy& policy) { |
Ady Abraham | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame] | 878 | std::lock_guard lock(mLock); |
Marin Shalamanov | eadf2e7 | 2020-12-10 15:35:28 +0100 | [diff] [blame] | 879 | if (!isPolicyValidLocked(policy)) { |
Marin Shalamanov | b6674e7 | 2020-11-06 13:05:57 +0100 | [diff] [blame] | 880 | ALOGE("Invalid refresh rate policy: %s", policy.toString().c_str()); |
Ana Krulec | ed3a8cc | 2019-11-14 00:55:07 +0100 | [diff] [blame] | 881 | return BAD_VALUE; |
| 882 | } |
ramindani | d72ba16 | 2022-09-09 21:33:40 +0000 | [diff] [blame] | 883 | mGetRankedRefreshRatesCache.reset(); |
Steven Thomas | d407190 | 2020-03-24 16:02:53 -0700 | [diff] [blame] | 884 | Policy previousPolicy = *getCurrentPolicyLocked(); |
| 885 | mDisplayManagerPolicy = policy; |
| 886 | if (*getCurrentPolicyLocked() == previousPolicy) { |
| 887 | return CURRENT_POLICY_UNCHANGED; |
Ana Krulec | ed3a8cc | 2019-11-14 00:55:07 +0100 | [diff] [blame] | 888 | } |
Ady Abraham | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame] | 889 | constructAvailableRefreshRates(); |
Ana Krulec | ed3a8cc | 2019-11-14 00:55:07 +0100 | [diff] [blame] | 890 | return NO_ERROR; |
| 891 | } |
| 892 | |
Steven Thomas | d407190 | 2020-03-24 16:02:53 -0700 | [diff] [blame] | 893 | status_t RefreshRateConfigs::setOverridePolicy(const std::optional<Policy>& policy) { |
Ana Krulec | ed3a8cc | 2019-11-14 00:55:07 +0100 | [diff] [blame] | 894 | std::lock_guard lock(mLock); |
Marin Shalamanov | eadf2e7 | 2020-12-10 15:35:28 +0100 | [diff] [blame] | 895 | if (policy && !isPolicyValidLocked(*policy)) { |
Steven Thomas | d407190 | 2020-03-24 16:02:53 -0700 | [diff] [blame] | 896 | return BAD_VALUE; |
| 897 | } |
ramindani | d72ba16 | 2022-09-09 21:33:40 +0000 | [diff] [blame] | 898 | mGetRankedRefreshRatesCache.reset(); |
Steven Thomas | d407190 | 2020-03-24 16:02:53 -0700 | [diff] [blame] | 899 | Policy previousPolicy = *getCurrentPolicyLocked(); |
| 900 | mOverridePolicy = policy; |
| 901 | if (*getCurrentPolicyLocked() == previousPolicy) { |
| 902 | return CURRENT_POLICY_UNCHANGED; |
| 903 | } |
| 904 | constructAvailableRefreshRates(); |
| 905 | return NO_ERROR; |
| 906 | } |
| 907 | |
| 908 | const RefreshRateConfigs::Policy* RefreshRateConfigs::getCurrentPolicyLocked() const { |
| 909 | return mOverridePolicy ? &mOverridePolicy.value() : &mDisplayManagerPolicy; |
| 910 | } |
| 911 | |
| 912 | RefreshRateConfigs::Policy RefreshRateConfigs::getCurrentPolicy() const { |
| 913 | std::lock_guard lock(mLock); |
| 914 | return *getCurrentPolicyLocked(); |
| 915 | } |
| 916 | |
| 917 | RefreshRateConfigs::Policy RefreshRateConfigs::getDisplayManagerPolicy() const { |
| 918 | std::lock_guard lock(mLock); |
| 919 | return mDisplayManagerPolicy; |
Ana Krulec | ed3a8cc | 2019-11-14 00:55:07 +0100 | [diff] [blame] | 920 | } |
| 921 | |
Marin Shalamanov | a7fe304 | 2021-01-29 21:02:08 +0100 | [diff] [blame] | 922 | bool RefreshRateConfigs::isModeAllowed(DisplayModeId modeId) const { |
Ana Krulec | ed3a8cc | 2019-11-14 00:55:07 +0100 | [diff] [blame] | 923 | std::lock_guard lock(mLock); |
Dominik Laskowski | b0054a2 | 2022-03-03 09:03:06 -0800 | [diff] [blame] | 924 | return std::any_of(mAppRequestRefreshRates.begin(), mAppRequestRefreshRates.end(), |
| 925 | [modeId](DisplayModeIterator modeIt) { |
| 926 | return modeIt->second->getId() == modeId; |
| 927 | }); |
Ady Abraham | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame] | 928 | } |
| 929 | |
| 930 | void RefreshRateConfigs::constructAvailableRefreshRates() { |
Dominik Laskowski | b0054a2 | 2022-03-03 09:03:06 -0800 | [diff] [blame] | 931 | // Filter modes based on current policy and sort on refresh rate. |
Steven Thomas | d407190 | 2020-03-24 16:02:53 -0700 | [diff] [blame] | 932 | const Policy* policy = getCurrentPolicyLocked(); |
Dominik Laskowski | b0054a2 | 2022-03-03 09:03:06 -0800 | [diff] [blame] | 933 | ALOGV("%s: %s ", __func__, policy->toString().c_str()); |
Ady Abraham | abc2760 | 2020-04-08 17:20:29 -0700 | [diff] [blame] | 934 | |
Dominik Laskowski | b0054a2 | 2022-03-03 09:03:06 -0800 | [diff] [blame] | 935 | const auto& defaultMode = mDisplayModes.get(policy->defaultMode)->get(); |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 936 | |
Dominik Laskowski | b0054a2 | 2022-03-03 09:03:06 -0800 | [diff] [blame] | 937 | const auto filterRefreshRates = [&](FpsRange range, const char* rangeName) REQUIRES(mLock) { |
| 938 | const auto filter = [&](const DisplayMode& mode) { |
| 939 | return mode.getResolution() == defaultMode->getResolution() && |
| 940 | mode.getDpi() == defaultMode->getDpi() && |
| 941 | (policy->allowGroupSwitching || mode.getGroup() == defaultMode->getGroup()) && |
| 942 | range.includes(mode.getFps()); |
| 943 | }; |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 944 | |
Dominik Laskowski | b0054a2 | 2022-03-03 09:03:06 -0800 | [diff] [blame] | 945 | const auto modes = sortByRefreshRate(mDisplayModes, filter); |
| 946 | LOG_ALWAYS_FATAL_IF(modes.empty(), "No matching modes for %s range %s", rangeName, |
| 947 | to_string(range).c_str()); |
Dominik Laskowski | 953b7fd | 2022-01-08 19:34:59 -0800 | [diff] [blame] | 948 | |
Dominik Laskowski | b0054a2 | 2022-03-03 09:03:06 -0800 | [diff] [blame] | 949 | const auto stringifyModes = [&] { |
| 950 | std::string str; |
| 951 | for (const auto modeIt : modes) { |
| 952 | str += to_string(modeIt->second->getFps()); |
| 953 | str.push_back(' '); |
| 954 | } |
| 955 | return str; |
| 956 | }; |
| 957 | ALOGV("%s refresh rates: %s", rangeName, stringifyModes().c_str()); |
Steven Thomas | f734df4 | 2020-04-13 21:09:28 -0700 | [diff] [blame] | 958 | |
Dominik Laskowski | b0054a2 | 2022-03-03 09:03:06 -0800 | [diff] [blame] | 959 | return modes; |
| 960 | }; |
| 961 | |
| 962 | mPrimaryRefreshRates = filterRefreshRates(policy->primaryRange, "primary"); |
| 963 | mAppRequestRefreshRates = filterRefreshRates(policy->appRequestRange, "app request"); |
Ady Abraham | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame] | 964 | } |
| 965 | |
Marin Shalamanov | e8a663d | 2020-11-24 17:48:00 +0100 | [diff] [blame] | 966 | Fps RefreshRateConfigs::findClosestKnownFrameRate(Fps frameRate) const { |
Dominik Laskowski | 6eab42d | 2021-09-13 14:34:13 -0700 | [diff] [blame] | 967 | using namespace fps_approx_ops; |
| 968 | |
| 969 | if (frameRate <= mKnownFrameRates.front()) { |
| 970 | return mKnownFrameRates.front(); |
Ady Abraham | b1b9d41 | 2020-06-01 19:53:52 -0700 | [diff] [blame] | 971 | } |
| 972 | |
Dominik Laskowski | 6eab42d | 2021-09-13 14:34:13 -0700 | [diff] [blame] | 973 | if (frameRate >= mKnownFrameRates.back()) { |
| 974 | return mKnownFrameRates.back(); |
Ady Abraham | b1b9d41 | 2020-06-01 19:53:52 -0700 | [diff] [blame] | 975 | } |
| 976 | |
Marin Shalamanov | e8a663d | 2020-11-24 17:48:00 +0100 | [diff] [blame] | 977 | auto lowerBound = std::lower_bound(mKnownFrameRates.begin(), mKnownFrameRates.end(), frameRate, |
Dominik Laskowski | 6eab42d | 2021-09-13 14:34:13 -0700 | [diff] [blame] | 978 | isStrictlyLess); |
Ady Abraham | b1b9d41 | 2020-06-01 19:53:52 -0700 | [diff] [blame] | 979 | |
Dominik Laskowski | 6eab42d | 2021-09-13 14:34:13 -0700 | [diff] [blame] | 980 | const auto distance1 = std::abs(frameRate.getValue() - lowerBound->getValue()); |
| 981 | const auto distance2 = std::abs(frameRate.getValue() - std::prev(lowerBound)->getValue()); |
Ady Abraham | b1b9d41 | 2020-06-01 19:53:52 -0700 | [diff] [blame] | 982 | return distance1 < distance2 ? *lowerBound : *std::prev(lowerBound); |
| 983 | } |
| 984 | |
Ana Krulec | b9afd79 | 2020-06-11 13:16:15 -0700 | [diff] [blame] | 985 | RefreshRateConfigs::KernelIdleTimerAction RefreshRateConfigs::getIdleTimerAction() const { |
| 986 | std::lock_guard lock(mLock); |
Dominik Laskowski | b0054a2 | 2022-03-03 09:03:06 -0800 | [diff] [blame] | 987 | |
| 988 | const Fps deviceMinFps = mMinRefreshRateModeIt->second->getFps(); |
| 989 | const DisplayModePtr& minByPolicy = getMinRefreshRateByPolicyLocked(); |
Ana Krulec | b9afd79 | 2020-06-11 13:16:15 -0700 | [diff] [blame] | 990 | |
| 991 | // Kernel idle timer will set the refresh rate to the device min. If DisplayManager says that |
| 992 | // the min allowed refresh rate is higher than the device min, we do not want to enable the |
| 993 | // timer. |
Dominik Laskowski | b0054a2 | 2022-03-03 09:03:06 -0800 | [diff] [blame] | 994 | if (isStrictlyLess(deviceMinFps, minByPolicy->getFps())) { |
| 995 | return KernelIdleTimerAction::TurnOff; |
Ana Krulec | b9afd79 | 2020-06-11 13:16:15 -0700 | [diff] [blame] | 996 | } |
Dominik Laskowski | b0054a2 | 2022-03-03 09:03:06 -0800 | [diff] [blame] | 997 | |
ramindani | d72ba16 | 2022-09-09 21:33:40 +0000 | [diff] [blame] | 998 | const DisplayModePtr& maxByPolicy = |
| 999 | getMaxRefreshRateByPolicyLocked(getActiveModeItLocked()->second->getGroup()); |
Ana Krulec | b9afd79 | 2020-06-11 13:16:15 -0700 | [diff] [blame] | 1000 | if (minByPolicy == maxByPolicy) { |
Dominik Laskowski | b0054a2 | 2022-03-03 09:03:06 -0800 | [diff] [blame] | 1001 | // Turn on the timer when the min of the primary range is below the device min. |
| 1002 | if (const Policy* currentPolicy = getCurrentPolicyLocked(); |
| 1003 | isApproxLess(currentPolicy->primaryRange.min, deviceMinFps)) { |
| 1004 | return KernelIdleTimerAction::TurnOn; |
Ana Krulec | b9afd79 | 2020-06-11 13:16:15 -0700 | [diff] [blame] | 1005 | } |
Dominik Laskowski | b0054a2 | 2022-03-03 09:03:06 -0800 | [diff] [blame] | 1006 | return KernelIdleTimerAction::TurnOff; |
Ana Krulec | b9afd79 | 2020-06-11 13:16:15 -0700 | [diff] [blame] | 1007 | } |
Dominik Laskowski | b0054a2 | 2022-03-03 09:03:06 -0800 | [diff] [blame] | 1008 | |
Ana Krulec | b9afd79 | 2020-06-11 13:16:15 -0700 | [diff] [blame] | 1009 | // Turn on the timer in all other cases. |
Dominik Laskowski | b0054a2 | 2022-03-03 09:03:06 -0800 | [diff] [blame] | 1010 | return KernelIdleTimerAction::TurnOn; |
Ana Krulec | b9afd79 | 2020-06-11 13:16:15 -0700 | [diff] [blame] | 1011 | } |
| 1012 | |
Dominik Laskowski | b0054a2 | 2022-03-03 09:03:06 -0800 | [diff] [blame] | 1013 | int RefreshRateConfigs::getFrameRateDivisor(Fps displayRefreshRate, Fps layerFrameRate) { |
Ady Abraham | 62f216c | 2020-10-13 19:07:23 -0700 | [diff] [blame] | 1014 | // This calculation needs to be in sync with the java code |
| 1015 | // in DisplayManagerService.getDisplayInfoForFrameRateOverride |
Marin Shalamanov | 15a0fc6 | 2021-08-16 18:20:21 +0200 | [diff] [blame] | 1016 | |
| 1017 | // The threshold must be smaller than 0.001 in order to differentiate |
| 1018 | // between the fractional pairs (e.g. 59.94 and 60). |
| 1019 | constexpr float kThreshold = 0.0009f; |
Dominik Laskowski | b0054a2 | 2022-03-03 09:03:06 -0800 | [diff] [blame] | 1020 | const auto numPeriods = displayRefreshRate.getValue() / layerFrameRate.getValue(); |
Ady Abraham | 0bb6a47 | 2020-10-12 10:22:13 -0700 | [diff] [blame] | 1021 | const auto numPeriodsRounded = std::round(numPeriods); |
| 1022 | if (std::abs(numPeriods - numPeriodsRounded) > kThreshold) { |
Ady Abraham | 62a0be2 | 2020-12-08 16:54:10 -0800 | [diff] [blame] | 1023 | return 0; |
Ady Abraham | 0bb6a47 | 2020-10-12 10:22:13 -0700 | [diff] [blame] | 1024 | } |
| 1025 | |
Ady Abraham | 62f216c | 2020-10-13 19:07:23 -0700 | [diff] [blame] | 1026 | return static_cast<int>(numPeriodsRounded); |
| 1027 | } |
| 1028 | |
Marin Shalamanov | 15a0fc6 | 2021-08-16 18:20:21 +0200 | [diff] [blame] | 1029 | bool RefreshRateConfigs::isFractionalPairOrMultiple(Fps smaller, Fps bigger) { |
Dominik Laskowski | 6eab42d | 2021-09-13 14:34:13 -0700 | [diff] [blame] | 1030 | if (isStrictlyLess(bigger, smaller)) { |
Marin Shalamanov | 15a0fc6 | 2021-08-16 18:20:21 +0200 | [diff] [blame] | 1031 | return isFractionalPairOrMultiple(bigger, smaller); |
| 1032 | } |
| 1033 | |
| 1034 | const auto multiplier = std::round(bigger.getValue() / smaller.getValue()); |
| 1035 | constexpr float kCoef = 1000.f / 1001.f; |
Dominik Laskowski | 6eab42d | 2021-09-13 14:34:13 -0700 | [diff] [blame] | 1036 | return isApproxEqual(bigger, Fps::fromValue(smaller.getValue() * multiplier / kCoef)) || |
| 1037 | isApproxEqual(bigger, Fps::fromValue(smaller.getValue() * multiplier * kCoef)); |
Marin Shalamanov | 15a0fc6 | 2021-08-16 18:20:21 +0200 | [diff] [blame] | 1038 | } |
| 1039 | |
Dominik Laskowski | e70461a | 2022-08-30 14:42:01 -0700 | [diff] [blame^] | 1040 | void RefreshRateConfigs::dump(utils::Dumper& dumper) const { |
| 1041 | using namespace std::string_view_literals; |
Dominik Laskowski | 0acc384 | 2022-04-07 11:23:42 -0700 | [diff] [blame] | 1042 | |
Marin Shalamanov | ba421a8 | 2020-11-10 21:49:26 +0100 | [diff] [blame] | 1043 | std::lock_guard lock(mLock); |
Marin Shalamanov | ba421a8 | 2020-11-10 21:49:26 +0100 | [diff] [blame] | 1044 | |
Dominik Laskowski | f8734e0 | 2022-08-26 09:06:59 -0700 | [diff] [blame] | 1045 | const auto activeModeId = getActiveModeItLocked()->first; |
Dominik Laskowski | e70461a | 2022-08-30 14:42:01 -0700 | [diff] [blame^] | 1046 | dumper.dump("activeModeId"sv, std::to_string(activeModeId.value())); |
Marin Shalamanov | ba421a8 | 2020-11-10 21:49:26 +0100 | [diff] [blame] | 1047 | |
Dominik Laskowski | e70461a | 2022-08-30 14:42:01 -0700 | [diff] [blame^] | 1048 | dumper.dump("displayModes"sv); |
| 1049 | { |
| 1050 | utils::Dumper::Indent indent(dumper); |
| 1051 | for (const auto& [id, mode] : mDisplayModes) { |
| 1052 | dumper.dump({}, to_string(*mode)); |
| 1053 | } |
Marin Shalamanov | ba421a8 | 2020-11-10 21:49:26 +0100 | [diff] [blame] | 1054 | } |
| 1055 | |
Dominik Laskowski | e70461a | 2022-08-30 14:42:01 -0700 | [diff] [blame^] | 1056 | dumper.dump("displayManagerPolicy"sv, mDisplayManagerPolicy.toString()); |
Dominik Laskowski | b0054a2 | 2022-03-03 09:03:06 -0800 | [diff] [blame] | 1057 | |
Dominik Laskowski | 0acc384 | 2022-04-07 11:23:42 -0700 | [diff] [blame] | 1058 | if (const Policy& currentPolicy = *getCurrentPolicyLocked(); |
| 1059 | mOverridePolicy && currentPolicy != mDisplayManagerPolicy) { |
Dominik Laskowski | e70461a | 2022-08-30 14:42:01 -0700 | [diff] [blame^] | 1060 | dumper.dump("overridePolicy"sv, currentPolicy.toString()); |
ramindani | 32cf060 | 2022-03-02 02:30:29 +0000 | [diff] [blame] | 1061 | } |
Dominik Laskowski | b0054a2 | 2022-03-03 09:03:06 -0800 | [diff] [blame] | 1062 | |
Dominik Laskowski | e70461a | 2022-08-30 14:42:01 -0700 | [diff] [blame^] | 1063 | dumper.dump("supportsFrameRateOverrideByContent"sv, mSupportsFrameRateOverrideByContent); |
Dominik Laskowski | 0acc384 | 2022-04-07 11:23:42 -0700 | [diff] [blame] | 1064 | |
Dominik Laskowski | e70461a | 2022-08-30 14:42:01 -0700 | [diff] [blame^] | 1065 | std::string idleTimer; |
Dominik Laskowski | b0054a2 | 2022-03-03 09:03:06 -0800 | [diff] [blame] | 1066 | if (mIdleTimer) { |
Dominik Laskowski | e70461a | 2022-08-30 14:42:01 -0700 | [diff] [blame^] | 1067 | idleTimer = mIdleTimer->dump(); |
Dominik Laskowski | b0054a2 | 2022-03-03 09:03:06 -0800 | [diff] [blame] | 1068 | } else { |
Dominik Laskowski | e70461a | 2022-08-30 14:42:01 -0700 | [diff] [blame^] | 1069 | idleTimer = "off"sv; |
Dominik Laskowski | b0054a2 | 2022-03-03 09:03:06 -0800 | [diff] [blame] | 1070 | } |
| 1071 | |
Dominik Laskowski | 0acc384 | 2022-04-07 11:23:42 -0700 | [diff] [blame] | 1072 | if (const auto controller = mConfig.kernelIdleTimerController) { |
Dominik Laskowski | e70461a | 2022-08-30 14:42:01 -0700 | [diff] [blame^] | 1073 | base::StringAppendF(&idleTimer, " (kernel via %s)", ftl::enum_string(*controller).c_str()); |
Dominik Laskowski | 0acc384 | 2022-04-07 11:23:42 -0700 | [diff] [blame] | 1074 | } else { |
Dominik Laskowski | e70461a | 2022-08-30 14:42:01 -0700 | [diff] [blame^] | 1075 | idleTimer += " (platform)"sv; |
Dominik Laskowski | 0acc384 | 2022-04-07 11:23:42 -0700 | [diff] [blame] | 1076 | } |
| 1077 | |
Dominik Laskowski | e70461a | 2022-08-30 14:42:01 -0700 | [diff] [blame^] | 1078 | dumper.dump("idleTimer"sv, idleTimer); |
Marin Shalamanov | ba421a8 | 2020-11-10 21:49:26 +0100 | [diff] [blame] | 1079 | } |
| 1080 | |
ramindani | 32cf060 | 2022-03-02 02:30:29 +0000 | [diff] [blame] | 1081 | std::chrono::milliseconds RefreshRateConfigs::getIdleTimerTimeout() { |
| 1082 | return mConfig.idleTimerTimeout; |
| 1083 | } |
| 1084 | |
Ady Abraham | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame] | 1085 | } // namespace android::scheduler |
Marin Shalamanov | bed7fd3 | 2020-12-21 20:02:20 +0100 | [diff] [blame] | 1086 | |
| 1087 | // TODO(b/129481165): remove the #pragma below and fix conversion issues |
Ady Abraham | dd5bfa9 | 2021-01-07 17:56:08 -0800 | [diff] [blame] | 1088 | #pragma clang diagnostic pop // ignored "-Wextra" |