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