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 | |
Ady Abraham | b4b1e0a | 2019-11-20 18:25:35 -0800 | [diff] [blame] | 20 | #include "RefreshRateConfigs.h" |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 21 | #include <android-base/stringprintf.h> |
| 22 | #include <utils/Trace.h> |
| 23 | #include <chrono> |
| 24 | #include <cmath> |
| 25 | |
Ady Abraham | 5b8afb5a | 2020-03-06 14:57:26 -0800 | [diff] [blame] | 26 | #undef LOG_TAG |
| 27 | #define LOG_TAG "RefreshRateConfigs" |
| 28 | |
Ady Abraham | b4b1e0a | 2019-11-20 18:25:35 -0800 | [diff] [blame] | 29 | namespace android::scheduler { |
Marin Shalamanov | 53fc11d | 2020-11-20 14:00:13 +0100 | [diff] [blame] | 30 | namespace { |
| 31 | std::string formatLayerInfo(const RefreshRateConfigs::LayerRequirement& layer, float weight) { |
Marin Shalamanov | e8a663d | 2020-11-24 17:48:00 +0100 | [diff] [blame] | 32 | return base::StringPrintf("%s (type=%s, weight=%.2f seamlessness=%s) %s", layer.name.c_str(), |
Marin Shalamanov | 53fc11d | 2020-11-20 14:00:13 +0100 | [diff] [blame] | 33 | RefreshRateConfigs::layerVoteTypeString(layer.vote).c_str(), weight, |
Marin Shalamanov | e8a663d | 2020-11-24 17:48:00 +0100 | [diff] [blame] | 34 | toString(layer.seamlessness).c_str(), |
| 35 | to_string(layer.desiredRefreshRate).c_str()); |
Marin Shalamanov | 53fc11d | 2020-11-20 14:00:13 +0100 | [diff] [blame] | 36 | } |
| 37 | } // namespace |
Ady Abraham | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame] | 38 | |
| 39 | using AllRefreshRatesMapType = RefreshRateConfigs::AllRefreshRatesMapType; |
Ady Abraham | b4b1e0a | 2019-11-20 18:25:35 -0800 | [diff] [blame] | 40 | using RefreshRate = RefreshRateConfigs::RefreshRate; |
Ady Abraham | b4b1e0a | 2019-11-20 18:25:35 -0800 | [diff] [blame] | 41 | |
Marin Shalamanov | 4608442 | 2020-10-13 12:33:42 +0200 | [diff] [blame] | 42 | std::string RefreshRate::toString() const { |
| 43 | return base::StringPrintf("{id=%d, hwcId=%d, fps=%.2f, width=%d, height=%d group=%d}", |
Marin Shalamanov | e8a663d | 2020-11-24 17:48:00 +0100 | [diff] [blame] | 44 | getConfigId().value(), hwcConfig->getId(), getFps().getValue(), |
Marin Shalamanov | 4608442 | 2020-10-13 12:33:42 +0200 | [diff] [blame] | 45 | hwcConfig->getWidth(), hwcConfig->getHeight(), getConfigGroup()); |
| 46 | } |
| 47 | |
Ady Abraham | a6b676e | 2020-05-27 14:29:09 -0700 | [diff] [blame] | 48 | std::string RefreshRateConfigs::layerVoteTypeString(LayerVoteType vote) { |
| 49 | switch (vote) { |
| 50 | case LayerVoteType::NoVote: |
| 51 | return "NoVote"; |
| 52 | case LayerVoteType::Min: |
| 53 | return "Min"; |
| 54 | case LayerVoteType::Max: |
| 55 | return "Max"; |
| 56 | case LayerVoteType::Heuristic: |
| 57 | return "Heuristic"; |
| 58 | case LayerVoteType::ExplicitDefault: |
| 59 | return "ExplicitDefault"; |
| 60 | case LayerVoteType::ExplicitExactOrMultiple: |
| 61 | return "ExplicitExactOrMultiple"; |
| 62 | } |
| 63 | } |
| 64 | |
Marin Shalamanov | b6674e7 | 2020-11-06 13:05:57 +0100 | [diff] [blame] | 65 | std::string RefreshRateConfigs::Policy::toString() const { |
Marin Shalamanov | 30b0b3c | 2020-10-13 19:15:06 +0200 | [diff] [blame] | 66 | return base::StringPrintf("default config ID: %d, allowGroupSwitching = %d" |
Marin Shalamanov | e8a663d | 2020-11-24 17:48:00 +0100 | [diff] [blame] | 67 | ", primary range: %s, app request range: %s", |
| 68 | defaultConfig.value(), allowGroupSwitching, |
| 69 | primaryRange.toString().c_str(), appRequestRange.toString().c_str()); |
Marin Shalamanov | 30b0b3c | 2020-10-13 19:15:06 +0200 | [diff] [blame] | 70 | } |
| 71 | |
Ady Abraham | 4ccdcb4 | 2020-02-11 17:34:34 -0800 | [diff] [blame] | 72 | std::pair<nsecs_t, nsecs_t> RefreshRateConfigs::getDisplayFrames(nsecs_t layerPeriod, |
| 73 | nsecs_t displayPeriod) const { |
Ady Abraham | 2c6716b | 2020-12-08 16:54:10 -0800 | [diff] [blame] | 74 | auto [quotient, remainder] = std::div(layerPeriod, displayPeriod); |
| 75 | if (remainder <= MARGIN_FOR_PERIOD_CALCULATION || |
| 76 | std::abs(remainder - displayPeriod) <= MARGIN_FOR_PERIOD_CALCULATION) { |
| 77 | quotient++; |
| 78 | remainder = 0; |
Ady Abraham | 4ccdcb4 | 2020-02-11 17:34:34 -0800 | [diff] [blame] | 79 | } |
| 80 | |
Ady Abraham | 2c6716b | 2020-12-08 16:54:10 -0800 | [diff] [blame] | 81 | return {quotient, remainder}; |
Ady Abraham | 4ccdcb4 | 2020-02-11 17:34:34 -0800 | [diff] [blame] | 82 | } |
| 83 | |
Ady Abraham | 2c6716b | 2020-12-08 16:54:10 -0800 | [diff] [blame] | 84 | float RefreshRateConfigs::calculateLayerScoreLocked(const LayerRequirement& layer, |
| 85 | const RefreshRate& refreshRate, |
| 86 | bool isSeamlessSwitch) const { |
| 87 | // Slightly prefer seamless switches. |
| 88 | constexpr float kSeamedSwitchPenalty = 0.95f; |
| 89 | const float seamlessness = isSeamlessSwitch ? 1.0f : kSeamedSwitchPenalty; |
| 90 | |
| 91 | // If the layer wants Max, give higher score to the higher refresh rate |
| 92 | if (layer.vote == LayerVoteType::Max) { |
| 93 | const auto ratio = |
| 94 | refreshRate.fps.getValue() / mAppRequestRefreshRates.back()->fps.getValue(); |
| 95 | // use ratio^2 to get a lower score the more we get further from peak |
| 96 | return ratio * ratio; |
| 97 | } |
| 98 | |
| 99 | const auto displayPeriod = refreshRate.getVsyncPeriod(); |
| 100 | const auto layerPeriod = layer.desiredRefreshRate.getPeriodNsecs(); |
| 101 | if (layer.vote == LayerVoteType::ExplicitDefault) { |
| 102 | // Find the actual rate the layer will render, assuming |
| 103 | // that layerPeriod is the minimal time to render a frame |
| 104 | auto actualLayerPeriod = displayPeriod; |
| 105 | int multiplier = 1; |
| 106 | while (layerPeriod > actualLayerPeriod + MARGIN_FOR_PERIOD_CALCULATION) { |
| 107 | multiplier++; |
| 108 | actualLayerPeriod = displayPeriod * multiplier; |
| 109 | } |
| 110 | return std::min(1.0f, |
| 111 | static_cast<float>(layerPeriod) / static_cast<float>(actualLayerPeriod)); |
| 112 | } |
| 113 | |
| 114 | if (layer.vote == LayerVoteType::ExplicitExactOrMultiple || |
| 115 | layer.vote == LayerVoteType::Heuristic) { |
| 116 | // Calculate how many display vsyncs we need to present a single frame for this |
| 117 | // layer |
| 118 | const auto [displayFramesQuotient, displayFramesRemainder] = |
| 119 | getDisplayFrames(layerPeriod, displayPeriod); |
| 120 | static constexpr size_t MAX_FRAMES_TO_FIT = 10; // Stop calculating when score < 0.1 |
| 121 | if (displayFramesRemainder == 0) { |
| 122 | // Layer desired refresh rate matches the display rate. |
| 123 | return 1.0f * seamlessness; |
| 124 | } |
| 125 | |
| 126 | if (displayFramesQuotient == 0) { |
| 127 | // Layer desired refresh rate is higher than the display rate. |
| 128 | return (static_cast<float>(layerPeriod) / static_cast<float>(displayPeriod)) * |
| 129 | (1.0f / (MAX_FRAMES_TO_FIT + 1)); |
| 130 | } |
| 131 | |
| 132 | // Layer desired refresh rate is lower than the display rate. Check how well it fits |
| 133 | // the cadence. |
| 134 | auto diff = std::abs(displayFramesRemainder - (displayPeriod - displayFramesRemainder)); |
| 135 | int iter = 2; |
| 136 | while (diff > MARGIN_FOR_PERIOD_CALCULATION && iter < MAX_FRAMES_TO_FIT) { |
| 137 | diff = diff - (displayPeriod - diff); |
| 138 | iter++; |
| 139 | } |
| 140 | |
| 141 | return (1.0f / iter) * seamlessness; |
| 142 | } |
| 143 | |
| 144 | return 0; |
| 145 | } |
| 146 | |
| 147 | struct RefreshRateScore { |
| 148 | const RefreshRate* refreshRate; |
| 149 | float score; |
| 150 | }; |
| 151 | |
Steven Thomas | bb37432 | 2020-04-28 22:47:16 -0700 | [diff] [blame] | 152 | const RefreshRate& RefreshRateConfigs::getBestRefreshRate( |
Ady Abraham | dfd6216 | 2020-06-10 16:11:56 -0700 | [diff] [blame] | 153 | const std::vector<LayerRequirement>& layers, const GlobalSignals& globalSignals, |
| 154 | GlobalSignals* outSignalsConsidered) const { |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 155 | ATRACE_CALL(); |
Marin Shalamanov | 4608442 | 2020-10-13 12:33:42 +0200 | [diff] [blame] | 156 | ALOGV("getBestRefreshRate %zu layers", layers.size()); |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 157 | |
Ady Abraham | dfd6216 | 2020-06-10 16:11:56 -0700 | [diff] [blame] | 158 | if (outSignalsConsidered) *outSignalsConsidered = {}; |
| 159 | const auto setTouchConsidered = [&] { |
| 160 | if (outSignalsConsidered) { |
| 161 | outSignalsConsidered->touch = true; |
| 162 | } |
| 163 | }; |
| 164 | |
| 165 | const auto setIdleConsidered = [&] { |
| 166 | if (outSignalsConsidered) { |
| 167 | outSignalsConsidered->idle = true; |
| 168 | } |
| 169 | }; |
| 170 | |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 171 | std::lock_guard lock(mLock); |
| 172 | |
| 173 | int noVoteLayers = 0; |
| 174 | int minVoteLayers = 0; |
| 175 | int maxVoteLayers = 0; |
Ady Abraham | 71c437d | 2020-01-31 15:56:57 -0800 | [diff] [blame] | 176 | int explicitDefaultVoteLayers = 0; |
| 177 | int explicitExactOrMultipleVoteLayers = 0; |
Ady Abraham | 6fb599b | 2020-03-05 13:48:22 -0800 | [diff] [blame] | 178 | float maxExplicitWeight = 0; |
Marin Shalamanov | 4608442 | 2020-10-13 12:33:42 +0200 | [diff] [blame] | 179 | int seamedLayers = 0; |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 180 | for (const auto& layer : layers) { |
Ady Abraham | 6fb599b | 2020-03-05 13:48:22 -0800 | [diff] [blame] | 181 | if (layer.vote == LayerVoteType::NoVote) { |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 182 | noVoteLayers++; |
Ady Abraham | 6fb599b | 2020-03-05 13:48:22 -0800 | [diff] [blame] | 183 | } else if (layer.vote == LayerVoteType::Min) { |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 184 | minVoteLayers++; |
Ady Abraham | 6fb599b | 2020-03-05 13:48:22 -0800 | [diff] [blame] | 185 | } else if (layer.vote == LayerVoteType::Max) { |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 186 | maxVoteLayers++; |
Ady Abraham | 6fb599b | 2020-03-05 13:48:22 -0800 | [diff] [blame] | 187 | } else if (layer.vote == LayerVoteType::ExplicitDefault) { |
Ady Abraham | 71c437d | 2020-01-31 15:56:57 -0800 | [diff] [blame] | 188 | explicitDefaultVoteLayers++; |
Ady Abraham | 6fb599b | 2020-03-05 13:48:22 -0800 | [diff] [blame] | 189 | maxExplicitWeight = std::max(maxExplicitWeight, layer.weight); |
| 190 | } else if (layer.vote == LayerVoteType::ExplicitExactOrMultiple) { |
Ady Abraham | 71c437d | 2020-01-31 15:56:57 -0800 | [diff] [blame] | 191 | explicitExactOrMultipleVoteLayers++; |
Ady Abraham | 6fb599b | 2020-03-05 13:48:22 -0800 | [diff] [blame] | 192 | maxExplicitWeight = std::max(maxExplicitWeight, layer.weight); |
| 193 | } |
Marin Shalamanov | 4608442 | 2020-10-13 12:33:42 +0200 | [diff] [blame] | 194 | |
Marin Shalamanov | 53fc11d | 2020-11-20 14:00:13 +0100 | [diff] [blame] | 195 | if (layer.seamlessness == Seamlessness::SeamedAndSeamless) { |
Marin Shalamanov | 4608442 | 2020-10-13 12:33:42 +0200 | [diff] [blame] | 196 | seamedLayers++; |
| 197 | } |
Ady Abraham | 6fb599b | 2020-03-05 13:48:22 -0800 | [diff] [blame] | 198 | } |
| 199 | |
Alec Mouri | 11232a2 | 2020-05-14 18:06:25 -0700 | [diff] [blame] | 200 | const bool hasExplicitVoteLayers = |
| 201 | explicitDefaultVoteLayers > 0 || explicitExactOrMultipleVoteLayers > 0; |
| 202 | |
Steven Thomas | f734df4 | 2020-04-13 21:09:28 -0700 | [diff] [blame] | 203 | // Consider the touch event if there are no Explicit* layers. Otherwise wait until after we've |
| 204 | // selected a refresh rate to see if we should apply touch boost. |
Ady Abraham | dfd6216 | 2020-06-10 16:11:56 -0700 | [diff] [blame] | 205 | if (globalSignals.touch && !hasExplicitVoteLayers) { |
Ady Abraham | a6b676e | 2020-05-27 14:29:09 -0700 | [diff] [blame] | 206 | ALOGV("TouchBoost - choose %s", getMaxRefreshRateByPolicyLocked().getName().c_str()); |
Ady Abraham | dfd6216 | 2020-06-10 16:11:56 -0700 | [diff] [blame] | 207 | setTouchConsidered(); |
Steven Thomas | f734df4 | 2020-04-13 21:09:28 -0700 | [diff] [blame] | 208 | return getMaxRefreshRateByPolicyLocked(); |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 209 | } |
| 210 | |
Alec Mouri | 11232a2 | 2020-05-14 18:06:25 -0700 | [diff] [blame] | 211 | // If the primary range consists of a single refresh rate then we can only |
| 212 | // move out the of range if layers explicitly request a different refresh |
| 213 | // rate. |
| 214 | const Policy* policy = getCurrentPolicyLocked(); |
Marin Shalamanov | e8a663d | 2020-11-24 17:48:00 +0100 | [diff] [blame] | 215 | const bool primaryRangeIsSingleRate = |
| 216 | policy->primaryRange.min.equalsWithMargin(policy->primaryRange.max); |
Alec Mouri | 11232a2 | 2020-05-14 18:06:25 -0700 | [diff] [blame] | 217 | |
Ady Abraham | dfd6216 | 2020-06-10 16:11:56 -0700 | [diff] [blame] | 218 | if (!globalSignals.touch && globalSignals.idle && |
| 219 | !(primaryRangeIsSingleRate && hasExplicitVoteLayers)) { |
Ady Abraham | a6b676e | 2020-05-27 14:29:09 -0700 | [diff] [blame] | 220 | ALOGV("Idle - choose %s", getMinRefreshRateByPolicyLocked().getName().c_str()); |
Ady Abraham | dfd6216 | 2020-06-10 16:11:56 -0700 | [diff] [blame] | 221 | setIdleConsidered(); |
Steven Thomas | bb37432 | 2020-04-28 22:47:16 -0700 | [diff] [blame] | 222 | return getMinRefreshRateByPolicyLocked(); |
| 223 | } |
| 224 | |
Steven Thomas | debafed | 2020-05-18 17:30:35 -0700 | [diff] [blame] | 225 | if (layers.empty() || noVoteLayers == layers.size()) { |
| 226 | return getMaxRefreshRateByPolicyLocked(); |
Steven Thomas | bb37432 | 2020-04-28 22:47:16 -0700 | [diff] [blame] | 227 | } |
| 228 | |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 229 | // Only if all layers want Min we should return Min |
| 230 | if (noVoteLayers + minVoteLayers == layers.size()) { |
Ady Abraham | a6b676e | 2020-05-27 14:29:09 -0700 | [diff] [blame] | 231 | ALOGV("all layers Min - choose %s", getMinRefreshRateByPolicyLocked().getName().c_str()); |
Steven Thomas | f734df4 | 2020-04-13 21:09:28 -0700 | [diff] [blame] | 232 | return getMinRefreshRateByPolicyLocked(); |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 233 | } |
| 234 | |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 235 | // Find the best refresh rate based on score |
Ady Abraham | 2c6716b | 2020-12-08 16:54:10 -0800 | [diff] [blame] | 236 | std::vector<RefreshRateScore> scores; |
Steven Thomas | f734df4 | 2020-04-13 21:09:28 -0700 | [diff] [blame] | 237 | scores.reserve(mAppRequestRefreshRates.size()); |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 238 | |
Steven Thomas | f734df4 | 2020-04-13 21:09:28 -0700 | [diff] [blame] | 239 | for (const auto refreshRate : mAppRequestRefreshRates) { |
Ady Abraham | 2c6716b | 2020-12-08 16:54:10 -0800 | [diff] [blame] | 240 | scores.emplace_back(RefreshRateScore{refreshRate, 0.0f}); |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 241 | } |
| 242 | |
Marin Shalamanov | 4608442 | 2020-10-13 12:33:42 +0200 | [diff] [blame] | 243 | const auto& defaultConfig = mRefreshRates.at(policy->defaultConfig); |
| 244 | |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 245 | for (const auto& layer : layers) { |
Ady Abraham | a6b676e | 2020-05-27 14:29:09 -0700 | [diff] [blame] | 246 | ALOGV("Calculating score for %s (%s, weight %.2f)", layer.name.c_str(), |
| 247 | layerVoteTypeString(layer.vote).c_str(), layer.weight); |
Ady Abraham | 4ccdcb4 | 2020-02-11 17:34:34 -0800 | [diff] [blame] | 248 | if (layer.vote == LayerVoteType::NoVote || layer.vote == LayerVoteType::Min) { |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 249 | continue; |
| 250 | } |
| 251 | |
Ady Abraham | 71c437d | 2020-01-31 15:56:57 -0800 | [diff] [blame] | 252 | auto weight = layer.weight; |
Ady Abraham | 71c437d | 2020-01-31 15:56:57 -0800 | [diff] [blame] | 253 | |
Ady Abraham | 4ccdcb4 | 2020-02-11 17:34:34 -0800 | [diff] [blame] | 254 | for (auto i = 0u; i < scores.size(); i++) { |
Ady Abraham | 2c6716b | 2020-12-08 16:54:10 -0800 | [diff] [blame] | 255 | const bool isSeamlessSwitch = scores[i].refreshRate->getConfigGroup() == |
| 256 | mCurrentRefreshRate->getConfigGroup(); |
Marin Shalamanov | 4608442 | 2020-10-13 12:33:42 +0200 | [diff] [blame] | 257 | |
Marin Shalamanov | 53fc11d | 2020-11-20 14:00:13 +0100 | [diff] [blame] | 258 | if (layer.seamlessness == Seamlessness::OnlySeamless && !isSeamlessSwitch) { |
| 259 | ALOGV("%s ignores %s to avoid non-seamless switch. Current config = %s", |
Ady Abraham | 2c6716b | 2020-12-08 16:54:10 -0800 | [diff] [blame] | 260 | formatLayerInfo(layer, weight).c_str(), |
| 261 | scores[i].refreshRate->toString().c_str(), |
Marin Shalamanov | 53fc11d | 2020-11-20 14:00:13 +0100 | [diff] [blame] | 262 | mCurrentRefreshRate->toString().c_str()); |
Marin Shalamanov | 4608442 | 2020-10-13 12:33:42 +0200 | [diff] [blame] | 263 | continue; |
| 264 | } |
| 265 | |
Marin Shalamanov | 53fc11d | 2020-11-20 14:00:13 +0100 | [diff] [blame] | 266 | if (layer.seamlessness == Seamlessness::SeamedAndSeamless && !isSeamlessSwitch && |
| 267 | !layer.focused) { |
| 268 | ALOGV("%s ignores %s because it's not focused and the switch is going to be seamed." |
| 269 | " Current config = %s", |
Ady Abraham | 2c6716b | 2020-12-08 16:54:10 -0800 | [diff] [blame] | 270 | formatLayerInfo(layer, weight).c_str(), |
| 271 | scores[i].refreshRate->toString().c_str(), |
Marin Shalamanov | 53fc11d | 2020-11-20 14:00:13 +0100 | [diff] [blame] | 272 | mCurrentRefreshRate->toString().c_str()); |
| 273 | continue; |
| 274 | } |
| 275 | |
| 276 | // Layers with default seamlessness vote for the current config group if |
| 277 | // there are layers with seamlessness=SeamedAndSeamless and for the default |
| 278 | // config group otherwise. In second case, if the current config group is different |
| 279 | // from the default, this means a layer with seamlessness=SeamedAndSeamless has just |
| 280 | // disappeared. |
| 281 | const bool isInPolicyForDefault = seamedLayers > 0 |
Ady Abraham | 2c6716b | 2020-12-08 16:54:10 -0800 | [diff] [blame] | 282 | ? scores[i].refreshRate->getConfigGroup() == |
| 283 | mCurrentRefreshRate->getConfigGroup() |
| 284 | : scores[i].refreshRate->getConfigGroup() == defaultConfig->getConfigGroup(); |
Marin Shalamanov | 53fc11d | 2020-11-20 14:00:13 +0100 | [diff] [blame] | 285 | |
| 286 | if (layer.seamlessness == Seamlessness::Default && !isInPolicyForDefault && |
| 287 | !layer.focused) { |
| 288 | ALOGV("%s ignores %s. Current config = %s", formatLayerInfo(layer, weight).c_str(), |
Ady Abraham | 2c6716b | 2020-12-08 16:54:10 -0800 | [diff] [blame] | 289 | scores[i].refreshRate->toString().c_str(), |
| 290 | mCurrentRefreshRate->toString().c_str()); |
Marin Shalamanov | 4608442 | 2020-10-13 12:33:42 +0200 | [diff] [blame] | 291 | continue; |
| 292 | } |
| 293 | |
Ady Abraham | 2c6716b | 2020-12-08 16:54:10 -0800 | [diff] [blame] | 294 | bool inPrimaryRange = scores[i].refreshRate->inPolicy(policy->primaryRange.min, |
| 295 | policy->primaryRange.max); |
Alec Mouri | 11232a2 | 2020-05-14 18:06:25 -0700 | [diff] [blame] | 296 | if ((primaryRangeIsSingleRate || !inPrimaryRange) && |
Ady Abraham | 20c029c | 2020-07-06 12:58:05 -0700 | [diff] [blame] | 297 | !(layer.focused && layer.vote == LayerVoteType::ExplicitDefault)) { |
| 298 | // Only focused layers with ExplicitDefault frame rate settings are allowed to score |
Ady Abraham | aae5ed5 | 2020-06-26 09:32:43 -0700 | [diff] [blame] | 299 | // refresh rates outside the primary range. |
Steven Thomas | f734df4 | 2020-04-13 21:09:28 -0700 | [diff] [blame] | 300 | continue; |
| 301 | } |
| 302 | |
Ady Abraham | 2c6716b | 2020-12-08 16:54:10 -0800 | [diff] [blame] | 303 | const auto layerScore = |
| 304 | calculateLayerScoreLocked(layer, *scores[i].refreshRate, isSeamlessSwitch); |
| 305 | ALOGV("%s gives %s score of %.2f", formatLayerInfo(layer, weight).c_str(), |
| 306 | scores[i].refreshRate->getName().c_str(), layerScore); |
| 307 | scores[i].score += weight * layerScore; |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 308 | } |
| 309 | } |
| 310 | |
Ady Abraham | 3470210 | 2020-02-10 14:12:05 -0800 | [diff] [blame] | 311 | // Now that we scored all the refresh rates we need to pick the one that got the highest score. |
| 312 | // In case of a tie we will pick the higher refresh rate if any of the layers wanted Max, |
| 313 | // or the lower otherwise. |
| 314 | const RefreshRate* bestRefreshRate = maxVoteLayers > 0 |
| 315 | ? getBestRefreshRate(scores.rbegin(), scores.rend()) |
| 316 | : getBestRefreshRate(scores.begin(), scores.end()); |
| 317 | |
Alec Mouri | 11232a2 | 2020-05-14 18:06:25 -0700 | [diff] [blame] | 318 | if (primaryRangeIsSingleRate) { |
| 319 | // If we never scored any layers, then choose the rate from the primary |
| 320 | // range instead of picking a random score from the app range. |
| 321 | if (std::all_of(scores.begin(), scores.end(), |
Ady Abraham | 2c6716b | 2020-12-08 16:54:10 -0800 | [diff] [blame] | 322 | [](RefreshRateScore score) { return score.score == 0; })) { |
Ady Abraham | a6b676e | 2020-05-27 14:29:09 -0700 | [diff] [blame] | 323 | ALOGV("layers not scored - choose %s", |
| 324 | getMaxRefreshRateByPolicyLocked().getName().c_str()); |
Alec Mouri | 11232a2 | 2020-05-14 18:06:25 -0700 | [diff] [blame] | 325 | return getMaxRefreshRateByPolicyLocked(); |
| 326 | } else { |
| 327 | return *bestRefreshRate; |
| 328 | } |
| 329 | } |
| 330 | |
Steven Thomas | f734df4 | 2020-04-13 21:09:28 -0700 | [diff] [blame] | 331 | // Consider the touch event if there are no ExplicitDefault layers. ExplicitDefault are mostly |
| 332 | // interactive (as opposed to ExplicitExactOrMultiple) and therefore if those posted an explicit |
| 333 | // vote we should not change it if we get a touch event. Only apply touch boost if it will |
| 334 | // actually increase the refresh rate over the normal selection. |
| 335 | const RefreshRate& touchRefreshRate = getMaxRefreshRateByPolicyLocked(); |
Alec Mouri | 11232a2 | 2020-05-14 18:06:25 -0700 | [diff] [blame] | 336 | |
Ady Abraham | dfd6216 | 2020-06-10 16:11:56 -0700 | [diff] [blame] | 337 | if (globalSignals.touch && explicitDefaultVoteLayers == 0 && |
Marin Shalamanov | e8a663d | 2020-11-24 17:48:00 +0100 | [diff] [blame] | 338 | bestRefreshRate->fps.lessThanWithMargin(touchRefreshRate.fps)) { |
Ady Abraham | dfd6216 | 2020-06-10 16:11:56 -0700 | [diff] [blame] | 339 | setTouchConsidered(); |
Ady Abraham | a6b676e | 2020-05-27 14:29:09 -0700 | [diff] [blame] | 340 | ALOGV("TouchBoost - choose %s", touchRefreshRate.getName().c_str()); |
Steven Thomas | f734df4 | 2020-04-13 21:09:28 -0700 | [diff] [blame] | 341 | return touchRefreshRate; |
| 342 | } |
| 343 | |
Ady Abraham | de7156e | 2020-02-28 17:29:39 -0800 | [diff] [blame] | 344 | return *bestRefreshRate; |
Ady Abraham | 3470210 | 2020-02-10 14:12:05 -0800 | [diff] [blame] | 345 | } |
| 346 | |
Ady Abraham | 2c6716b | 2020-12-08 16:54:10 -0800 | [diff] [blame] | 347 | std::unordered_map<uid_t, std::vector<const RefreshRateConfigs::LayerRequirement*>> |
| 348 | groupLayersByUid(const std::vector<RefreshRateConfigs::LayerRequirement>& layers) { |
| 349 | std::unordered_map<uid_t, std::vector<const RefreshRateConfigs::LayerRequirement*>> layersByUid; |
| 350 | for (const auto& layer : layers) { |
| 351 | auto iter = layersByUid.emplace(layer.ownerUid, |
| 352 | std::vector<const RefreshRateConfigs::LayerRequirement*>()); |
| 353 | auto& layersWithSameUid = iter.first->second; |
| 354 | layersWithSameUid.push_back(&layer); |
| 355 | } |
| 356 | |
| 357 | // Remove uids that can't have a frame rate override |
| 358 | for (auto iter = layersByUid.begin(); iter != layersByUid.end();) { |
| 359 | const auto& layersWithSameUid = iter->second; |
| 360 | bool skipUid = false; |
| 361 | for (const auto& layer : layersWithSameUid) { |
| 362 | if (layer->vote == RefreshRateConfigs::LayerVoteType::Max || |
| 363 | layer->vote == RefreshRateConfigs::LayerVoteType::Heuristic) { |
| 364 | skipUid = true; |
| 365 | break; |
| 366 | } |
| 367 | } |
| 368 | if (skipUid) { |
| 369 | iter = layersByUid.erase(iter); |
| 370 | } else { |
| 371 | ++iter; |
| 372 | } |
| 373 | } |
| 374 | |
| 375 | return layersByUid; |
| 376 | } |
| 377 | |
| 378 | std::vector<RefreshRateScore> initializeScoresForAllRefreshRates( |
| 379 | const AllRefreshRatesMapType& refreshRates) { |
| 380 | std::vector<RefreshRateScore> scores; |
| 381 | scores.reserve(refreshRates.size()); |
| 382 | for (const auto& [ignored, refreshRate] : refreshRates) { |
| 383 | scores.emplace_back(RefreshRateScore{refreshRate.get(), 0.0f}); |
| 384 | } |
| 385 | std::sort(scores.begin(), scores.end(), |
| 386 | [](const auto& a, const auto& b) { return *a.refreshRate < *b.refreshRate; }); |
| 387 | return scores; |
| 388 | } |
| 389 | |
| 390 | RefreshRateConfigs::UidToFrameRateOverride RefreshRateConfigs::getFrameRateOverrides( |
| 391 | const std::vector<LayerRequirement>& layers, Fps displayFrameRate) const { |
| 392 | ATRACE_CALL(); |
| 393 | ALOGV("getFrameRateOverrides %zu layers", layers.size()); |
| 394 | |
| 395 | std::lock_guard lock(mLock); |
| 396 | std::vector<RefreshRateScore> scores = initializeScoresForAllRefreshRates(mRefreshRates); |
| 397 | std::unordered_map<uid_t, std::vector<const LayerRequirement*>> layersByUid = |
| 398 | groupLayersByUid(layers); |
| 399 | UidToFrameRateOverride frameRateOverrides; |
| 400 | for (const auto& [uid, layersWithSameUid] : layersByUid) { |
| 401 | for (auto& score : scores) { |
| 402 | score.score = 0; |
| 403 | } |
| 404 | |
| 405 | for (const auto& layer : layersWithSameUid) { |
| 406 | if (layer->vote == LayerVoteType::NoVote || layer->vote == LayerVoteType::Min) { |
| 407 | continue; |
| 408 | } |
| 409 | |
| 410 | LOG_ALWAYS_FATAL_IF(layer->vote != LayerVoteType::ExplicitDefault && |
| 411 | layer->vote != LayerVoteType::ExplicitExactOrMultiple); |
| 412 | for (RefreshRateScore& score : scores) { |
| 413 | const auto layerScore = calculateLayerScoreLocked(*layer, *score.refreshRate, |
| 414 | /*isSeamlessSwitch*/ true); |
| 415 | score.score += layer->weight * layerScore; |
| 416 | } |
| 417 | } |
| 418 | |
| 419 | // We just care about the refresh rates which are a divider of the |
| 420 | // display refresh rate |
| 421 | auto iter = |
| 422 | std::remove_if(scores.begin(), scores.end(), [&](const RefreshRateScore& score) { |
| 423 | return getFrameRateDivider(displayFrameRate, score.refreshRate->getFps()) == 0; |
| 424 | }); |
| 425 | scores.erase(iter, scores.end()); |
| 426 | |
| 427 | // If we never scored any layers, we don't have a preferred frame rate |
| 428 | if (std::all_of(scores.begin(), scores.end(), |
| 429 | [](const RefreshRateScore& score) { return score.score == 0; })) { |
| 430 | continue; |
| 431 | } |
| 432 | |
| 433 | // Now that we scored all the refresh rates we need to pick the one that got the highest |
| 434 | // score. |
| 435 | const RefreshRate* bestRefreshRate = getBestRefreshRate(scores.begin(), scores.end()); |
| 436 | |
| 437 | // If the nest refresh rate is the current one, we don't have an override |
| 438 | if (!bestRefreshRate->getFps().equalsWithMargin(displayFrameRate)) { |
| 439 | frameRateOverrides.emplace(uid, bestRefreshRate->getFps()); |
| 440 | } |
| 441 | } |
| 442 | |
| 443 | return frameRateOverrides; |
| 444 | } |
| 445 | |
Ady Abraham | 3470210 | 2020-02-10 14:12:05 -0800 | [diff] [blame] | 446 | template <typename Iter> |
| 447 | const RefreshRate* RefreshRateConfigs::getBestRefreshRate(Iter begin, Iter end) const { |
Ady Abraham | 5b8afb5a | 2020-03-06 14:57:26 -0800 | [diff] [blame] | 448 | constexpr auto EPSILON = 0.001f; |
Ady Abraham | 2c6716b | 2020-12-08 16:54:10 -0800 | [diff] [blame] | 449 | const RefreshRate* bestRefreshRate = begin->refreshRate; |
| 450 | float max = begin->score; |
Ady Abraham | 3470210 | 2020-02-10 14:12:05 -0800 | [diff] [blame] | 451 | for (auto i = begin; i != end; ++i) { |
| 452 | const auto [refreshRate, score] = *i; |
Marin Shalamanov | e8a663d | 2020-11-24 17:48:00 +0100 | [diff] [blame] | 453 | ALOGV("%s scores %.2f", refreshRate->getName().c_str(), score); |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 454 | |
Marin Shalamanov | e8a663d | 2020-11-24 17:48:00 +0100 | [diff] [blame] | 455 | ATRACE_INT(refreshRate->getName().c_str(), round<int>(score * 100)); |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 456 | |
Ady Abraham | 5b8afb5a | 2020-03-06 14:57:26 -0800 | [diff] [blame] | 457 | if (score > max * (1 + EPSILON)) { |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 458 | max = score; |
| 459 | bestRefreshRate = refreshRate; |
| 460 | } |
| 461 | } |
| 462 | |
Ady Abraham | 3470210 | 2020-02-10 14:12:05 -0800 | [diff] [blame] | 463 | return bestRefreshRate; |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 464 | } |
| 465 | |
Ady Abraham | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame] | 466 | const AllRefreshRatesMapType& RefreshRateConfigs::getAllRefreshRates() const { |
| 467 | return mRefreshRates; |
| 468 | } |
| 469 | |
| 470 | const RefreshRate& RefreshRateConfigs::getMinRefreshRateByPolicy() const { |
| 471 | std::lock_guard lock(mLock); |
Steven Thomas | f734df4 | 2020-04-13 21:09:28 -0700 | [diff] [blame] | 472 | return getMinRefreshRateByPolicyLocked(); |
| 473 | } |
| 474 | |
| 475 | const RefreshRate& RefreshRateConfigs::getMinRefreshRateByPolicyLocked() const { |
Marin Shalamanov | 4608442 | 2020-10-13 12:33:42 +0200 | [diff] [blame] | 476 | for (auto refreshRate : mPrimaryRefreshRates) { |
| 477 | if (mCurrentRefreshRate->getConfigGroup() == refreshRate->getConfigGroup()) { |
| 478 | return *refreshRate; |
| 479 | } |
| 480 | } |
| 481 | ALOGE("Can't find min refresh rate by policy with the same config group" |
| 482 | " as the current config %s", |
| 483 | mCurrentRefreshRate->toString().c_str()); |
| 484 | // Defaulting to the lowest refresh rate |
Steven Thomas | f734df4 | 2020-04-13 21:09:28 -0700 | [diff] [blame] | 485 | return *mPrimaryRefreshRates.front(); |
Ady Abraham | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame] | 486 | } |
| 487 | |
| 488 | const RefreshRate& RefreshRateConfigs::getMaxRefreshRateByPolicy() const { |
| 489 | std::lock_guard lock(mLock); |
Steven Thomas | f734df4 | 2020-04-13 21:09:28 -0700 | [diff] [blame] | 490 | return getMaxRefreshRateByPolicyLocked(); |
| 491 | } |
| 492 | |
| 493 | const RefreshRate& RefreshRateConfigs::getMaxRefreshRateByPolicyLocked() const { |
Marin Shalamanov | 4608442 | 2020-10-13 12:33:42 +0200 | [diff] [blame] | 494 | for (auto it = mPrimaryRefreshRates.rbegin(); it != mPrimaryRefreshRates.rend(); it++) { |
| 495 | const auto& refreshRate = (**it); |
| 496 | if (mCurrentRefreshRate->getConfigGroup() == refreshRate.getConfigGroup()) { |
| 497 | return refreshRate; |
| 498 | } |
| 499 | } |
| 500 | ALOGE("Can't find max refresh rate by policy with the same config group" |
| 501 | " as the current config %s", |
| 502 | mCurrentRefreshRate->toString().c_str()); |
| 503 | // Defaulting to the highest refresh rate |
Steven Thomas | f734df4 | 2020-04-13 21:09:28 -0700 | [diff] [blame] | 504 | return *mPrimaryRefreshRates.back(); |
Ady Abraham | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame] | 505 | } |
| 506 | |
| 507 | const RefreshRate& RefreshRateConfigs::getCurrentRefreshRate() const { |
| 508 | std::lock_guard lock(mLock); |
| 509 | return *mCurrentRefreshRate; |
| 510 | } |
| 511 | |
Ana Krulec | 5d47791 | 2020-02-07 12:02:38 -0800 | [diff] [blame] | 512 | const RefreshRate& RefreshRateConfigs::getCurrentRefreshRateByPolicy() const { |
| 513 | std::lock_guard lock(mLock); |
Ana Krulec | 3d367c8 | 2020-02-25 15:02:01 -0800 | [diff] [blame] | 514 | return getCurrentRefreshRateByPolicyLocked(); |
| 515 | } |
| 516 | |
| 517 | const RefreshRate& RefreshRateConfigs::getCurrentRefreshRateByPolicyLocked() const { |
Steven Thomas | f734df4 | 2020-04-13 21:09:28 -0700 | [diff] [blame] | 518 | if (std::find(mAppRequestRefreshRates.begin(), mAppRequestRefreshRates.end(), |
| 519 | mCurrentRefreshRate) != mAppRequestRefreshRates.end()) { |
Ana Krulec | 5d47791 | 2020-02-07 12:02:38 -0800 | [diff] [blame] | 520 | return *mCurrentRefreshRate; |
| 521 | } |
Steven Thomas | d407190 | 2020-03-24 16:02:53 -0700 | [diff] [blame] | 522 | return *mRefreshRates.at(getCurrentPolicyLocked()->defaultConfig); |
Ana Krulec | 5d47791 | 2020-02-07 12:02:38 -0800 | [diff] [blame] | 523 | } |
| 524 | |
Ady Abraham | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame] | 525 | void RefreshRateConfigs::setCurrentConfigId(HwcConfigIndexType configId) { |
| 526 | std::lock_guard lock(mLock); |
Ady Abraham | 2e1dd89 | 2020-03-05 13:48:36 -0800 | [diff] [blame] | 527 | mCurrentRefreshRate = mRefreshRates.at(configId).get(); |
Ady Abraham | b4b1e0a | 2019-11-20 18:25:35 -0800 | [diff] [blame] | 528 | } |
| 529 | |
Ady Abraham | b4b1e0a | 2019-11-20 18:25:35 -0800 | [diff] [blame] | 530 | RefreshRateConfigs::RefreshRateConfigs( |
Ady Abraham | b4b1e0a | 2019-11-20 18:25:35 -0800 | [diff] [blame] | 531 | const std::vector<std::shared_ptr<const HWC2::Display::Config>>& configs, |
Ady Abraham | b1b9d41 | 2020-06-01 19:53:52 -0700 | [diff] [blame] | 532 | HwcConfigIndexType currentConfigId) |
| 533 | : mKnownFrameRates(constructKnownFrameRates(configs)) { |
Ady Abraham | abc2760 | 2020-04-08 17:20:29 -0700 | [diff] [blame] | 534 | LOG_ALWAYS_FATAL_IF(configs.empty()); |
| 535 | LOG_ALWAYS_FATAL_IF(currentConfigId.value() >= configs.size()); |
| 536 | |
| 537 | for (auto configId = HwcConfigIndexType(0); configId.value() < configs.size(); configId++) { |
| 538 | const auto& config = configs.at(static_cast<size_t>(configId.value())); |
Ady Abraham | abc2760 | 2020-04-08 17:20:29 -0700 | [diff] [blame] | 539 | mRefreshRates.emplace(configId, |
| 540 | std::make_unique<RefreshRate>(configId, config, |
Marin Shalamanov | e8a663d | 2020-11-24 17:48:00 +0100 | [diff] [blame] | 541 | Fps::fromPeriodNsecs( |
| 542 | config->getVsyncPeriod()), |
Ady Abraham | abc2760 | 2020-04-08 17:20:29 -0700 | [diff] [blame] | 543 | RefreshRate::ConstructorTag(0))); |
| 544 | if (configId == currentConfigId) { |
| 545 | mCurrentRefreshRate = mRefreshRates.at(configId).get(); |
| 546 | } |
Ady Abraham | b4b1e0a | 2019-11-20 18:25:35 -0800 | [diff] [blame] | 547 | } |
Ady Abraham | abc2760 | 2020-04-08 17:20:29 -0700 | [diff] [blame] | 548 | |
| 549 | std::vector<const RefreshRate*> sortedConfigs; |
| 550 | getSortedRefreshRateList([](const RefreshRate&) { return true; }, &sortedConfigs); |
| 551 | mDisplayManagerPolicy.defaultConfig = currentConfigId; |
| 552 | mMinSupportedRefreshRate = sortedConfigs.front(); |
| 553 | mMaxSupportedRefreshRate = sortedConfigs.back(); |
| 554 | constructAvailableRefreshRates(); |
Ady Abraham | b4b1e0a | 2019-11-20 18:25:35 -0800 | [diff] [blame] | 555 | } |
| 556 | |
Steven Thomas | d407190 | 2020-03-24 16:02:53 -0700 | [diff] [blame] | 557 | bool RefreshRateConfigs::isPolicyValid(const Policy& policy) { |
| 558 | // defaultConfig must be a valid config, and within the given refresh rate range. |
| 559 | auto iter = mRefreshRates.find(policy.defaultConfig); |
| 560 | if (iter == mRefreshRates.end()) { |
Marin Shalamanov | b6674e7 | 2020-11-06 13:05:57 +0100 | [diff] [blame] | 561 | ALOGE("Default config is not found."); |
Steven Thomas | d407190 | 2020-03-24 16:02:53 -0700 | [diff] [blame] | 562 | return false; |
| 563 | } |
| 564 | const RefreshRate& refreshRate = *iter->second; |
Steven Thomas | f734df4 | 2020-04-13 21:09:28 -0700 | [diff] [blame] | 565 | if (!refreshRate.inPolicy(policy.primaryRange.min, policy.primaryRange.max)) { |
Marin Shalamanov | b6674e7 | 2020-11-06 13:05:57 +0100 | [diff] [blame] | 566 | ALOGE("Default config is not in the primary range."); |
Steven Thomas | d407190 | 2020-03-24 16:02:53 -0700 | [diff] [blame] | 567 | return false; |
| 568 | } |
Marin Shalamanov | e8a663d | 2020-11-24 17:48:00 +0100 | [diff] [blame] | 569 | return policy.appRequestRange.min.lessThanOrEqualWithMargin(policy.primaryRange.min) && |
| 570 | policy.appRequestRange.max.greaterThanOrEqualWithMargin(policy.primaryRange.max); |
Steven Thomas | d407190 | 2020-03-24 16:02:53 -0700 | [diff] [blame] | 571 | } |
| 572 | |
| 573 | status_t RefreshRateConfigs::setDisplayManagerPolicy(const Policy& policy) { |
Ady Abraham | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame] | 574 | std::lock_guard lock(mLock); |
Steven Thomas | d407190 | 2020-03-24 16:02:53 -0700 | [diff] [blame] | 575 | if (!isPolicyValid(policy)) { |
Marin Shalamanov | b6674e7 | 2020-11-06 13:05:57 +0100 | [diff] [blame] | 576 | ALOGE("Invalid refresh rate policy: %s", policy.toString().c_str()); |
Ana Krulec | ed3a8cc | 2019-11-14 00:55:07 +0100 | [diff] [blame] | 577 | return BAD_VALUE; |
| 578 | } |
Steven Thomas | d407190 | 2020-03-24 16:02:53 -0700 | [diff] [blame] | 579 | Policy previousPolicy = *getCurrentPolicyLocked(); |
| 580 | mDisplayManagerPolicy = policy; |
| 581 | if (*getCurrentPolicyLocked() == previousPolicy) { |
| 582 | return CURRENT_POLICY_UNCHANGED; |
Ana Krulec | ed3a8cc | 2019-11-14 00:55:07 +0100 | [diff] [blame] | 583 | } |
Ady Abraham | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame] | 584 | constructAvailableRefreshRates(); |
Ana Krulec | ed3a8cc | 2019-11-14 00:55:07 +0100 | [diff] [blame] | 585 | return NO_ERROR; |
| 586 | } |
| 587 | |
Steven Thomas | d407190 | 2020-03-24 16:02:53 -0700 | [diff] [blame] | 588 | status_t RefreshRateConfigs::setOverridePolicy(const std::optional<Policy>& policy) { |
Ana Krulec | ed3a8cc | 2019-11-14 00:55:07 +0100 | [diff] [blame] | 589 | std::lock_guard lock(mLock); |
Steven Thomas | d407190 | 2020-03-24 16:02:53 -0700 | [diff] [blame] | 590 | if (policy && !isPolicyValid(*policy)) { |
| 591 | return BAD_VALUE; |
| 592 | } |
| 593 | Policy previousPolicy = *getCurrentPolicyLocked(); |
| 594 | mOverridePolicy = policy; |
| 595 | if (*getCurrentPolicyLocked() == previousPolicy) { |
| 596 | return CURRENT_POLICY_UNCHANGED; |
| 597 | } |
| 598 | constructAvailableRefreshRates(); |
| 599 | return NO_ERROR; |
| 600 | } |
| 601 | |
| 602 | const RefreshRateConfigs::Policy* RefreshRateConfigs::getCurrentPolicyLocked() const { |
| 603 | return mOverridePolicy ? &mOverridePolicy.value() : &mDisplayManagerPolicy; |
| 604 | } |
| 605 | |
| 606 | RefreshRateConfigs::Policy RefreshRateConfigs::getCurrentPolicy() const { |
| 607 | std::lock_guard lock(mLock); |
| 608 | return *getCurrentPolicyLocked(); |
| 609 | } |
| 610 | |
| 611 | RefreshRateConfigs::Policy RefreshRateConfigs::getDisplayManagerPolicy() const { |
| 612 | std::lock_guard lock(mLock); |
| 613 | return mDisplayManagerPolicy; |
Ana Krulec | ed3a8cc | 2019-11-14 00:55:07 +0100 | [diff] [blame] | 614 | } |
| 615 | |
| 616 | bool RefreshRateConfigs::isConfigAllowed(HwcConfigIndexType config) const { |
| 617 | std::lock_guard lock(mLock); |
Steven Thomas | f734df4 | 2020-04-13 21:09:28 -0700 | [diff] [blame] | 618 | for (const RefreshRate* refreshRate : mAppRequestRefreshRates) { |
Ana Krulec | ed3a8cc | 2019-11-14 00:55:07 +0100 | [diff] [blame] | 619 | if (refreshRate->configId == config) { |
| 620 | return true; |
| 621 | } |
| 622 | } |
| 623 | return false; |
Ady Abraham | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame] | 624 | } |
| 625 | |
| 626 | void RefreshRateConfigs::getSortedRefreshRateList( |
| 627 | const std::function<bool(const RefreshRate&)>& shouldAddRefreshRate, |
| 628 | std::vector<const RefreshRate*>* outRefreshRates) { |
| 629 | outRefreshRates->clear(); |
| 630 | outRefreshRates->reserve(mRefreshRates.size()); |
| 631 | for (const auto& [type, refreshRate] : mRefreshRates) { |
Ady Abraham | 2e1dd89 | 2020-03-05 13:48:36 -0800 | [diff] [blame] | 632 | if (shouldAddRefreshRate(*refreshRate)) { |
Ady Abraham | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame] | 633 | ALOGV("getSortedRefreshRateList: config %d added to list policy", |
Ady Abraham | 2e1dd89 | 2020-03-05 13:48:36 -0800 | [diff] [blame] | 634 | refreshRate->configId.value()); |
| 635 | outRefreshRates->push_back(refreshRate.get()); |
Ady Abraham | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame] | 636 | } |
| 637 | } |
| 638 | |
| 639 | std::sort(outRefreshRates->begin(), outRefreshRates->end(), |
| 640 | [](const auto refreshRate1, const auto refreshRate2) { |
Ady Abraham | abc2760 | 2020-04-08 17:20:29 -0700 | [diff] [blame] | 641 | if (refreshRate1->hwcConfig->getVsyncPeriod() != |
| 642 | refreshRate2->hwcConfig->getVsyncPeriod()) { |
| 643 | return refreshRate1->hwcConfig->getVsyncPeriod() > |
| 644 | refreshRate2->hwcConfig->getVsyncPeriod(); |
Steven Thomas | d407190 | 2020-03-24 16:02:53 -0700 | [diff] [blame] | 645 | } else { |
Ady Abraham | abc2760 | 2020-04-08 17:20:29 -0700 | [diff] [blame] | 646 | return refreshRate1->hwcConfig->getConfigGroup() > |
| 647 | refreshRate2->hwcConfig->getConfigGroup(); |
Steven Thomas | d407190 | 2020-03-24 16:02:53 -0700 | [diff] [blame] | 648 | } |
Ady Abraham | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame] | 649 | }); |
| 650 | } |
| 651 | |
| 652 | void RefreshRateConfigs::constructAvailableRefreshRates() { |
| 653 | // Filter configs based on current policy and sort based on vsync period |
Steven Thomas | d407190 | 2020-03-24 16:02:53 -0700 | [diff] [blame] | 654 | const Policy* policy = getCurrentPolicyLocked(); |
Ady Abraham | abc2760 | 2020-04-08 17:20:29 -0700 | [diff] [blame] | 655 | const auto& defaultConfig = mRefreshRates.at(policy->defaultConfig)->hwcConfig; |
Marin Shalamanov | e8a663d | 2020-11-24 17:48:00 +0100 | [diff] [blame] | 656 | ALOGV("constructAvailableRefreshRates: %s ", policy->toString().c_str()); |
Ady Abraham | abc2760 | 2020-04-08 17:20:29 -0700 | [diff] [blame] | 657 | |
Marin Shalamanov | e8a663d | 2020-11-24 17:48:00 +0100 | [diff] [blame] | 658 | auto filterRefreshRates = [&](Fps min, Fps max, const char* listName, |
Steven Thomas | f734df4 | 2020-04-13 21:09:28 -0700 | [diff] [blame] | 659 | std::vector<const RefreshRate*>* outRefreshRates) { |
| 660 | getSortedRefreshRateList( |
| 661 | [&](const RefreshRate& refreshRate) REQUIRES(mLock) { |
| 662 | const auto& hwcConfig = refreshRate.hwcConfig; |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 663 | |
Steven Thomas | f734df4 | 2020-04-13 21:09:28 -0700 | [diff] [blame] | 664 | return hwcConfig->getHeight() == defaultConfig->getHeight() && |
| 665 | hwcConfig->getWidth() == defaultConfig->getWidth() && |
| 666 | hwcConfig->getDpiX() == defaultConfig->getDpiX() && |
| 667 | hwcConfig->getDpiY() == defaultConfig->getDpiY() && |
| 668 | (policy->allowGroupSwitching || |
| 669 | hwcConfig->getConfigGroup() == defaultConfig->getConfigGroup()) && |
| 670 | refreshRate.inPolicy(min, max); |
| 671 | }, |
| 672 | outRefreshRates); |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 673 | |
Steven Thomas | f734df4 | 2020-04-13 21:09:28 -0700 | [diff] [blame] | 674 | LOG_ALWAYS_FATAL_IF(outRefreshRates->empty(), |
Marin Shalamanov | e8a663d | 2020-11-24 17:48:00 +0100 | [diff] [blame] | 675 | "No matching configs for %s range: min=%s max=%s", listName, |
| 676 | to_string(min).c_str(), to_string(max).c_str()); |
Steven Thomas | f734df4 | 2020-04-13 21:09:28 -0700 | [diff] [blame] | 677 | auto stringifyRefreshRates = [&]() -> std::string { |
| 678 | std::string str; |
| 679 | for (auto refreshRate : *outRefreshRates) { |
Marin Shalamanov | e8a663d | 2020-11-24 17:48:00 +0100 | [diff] [blame] | 680 | base::StringAppendF(&str, "%s ", refreshRate->getName().c_str()); |
Steven Thomas | f734df4 | 2020-04-13 21:09:28 -0700 | [diff] [blame] | 681 | } |
| 682 | return str; |
| 683 | }; |
| 684 | ALOGV("%s refresh rates: %s", listName, stringifyRefreshRates().c_str()); |
| 685 | }; |
| 686 | |
| 687 | filterRefreshRates(policy->primaryRange.min, policy->primaryRange.max, "primary", |
| 688 | &mPrimaryRefreshRates); |
| 689 | filterRefreshRates(policy->appRequestRange.min, policy->appRequestRange.max, "app request", |
| 690 | &mAppRequestRefreshRates); |
Ady Abraham | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame] | 691 | } |
| 692 | |
Marin Shalamanov | e8a663d | 2020-11-24 17:48:00 +0100 | [diff] [blame] | 693 | std::vector<Fps> RefreshRateConfigs::constructKnownFrameRates( |
Ady Abraham | b1b9d41 | 2020-06-01 19:53:52 -0700 | [diff] [blame] | 694 | const std::vector<std::shared_ptr<const HWC2::Display::Config>>& configs) { |
Marin Shalamanov | e8a663d | 2020-11-24 17:48:00 +0100 | [diff] [blame] | 695 | std::vector<Fps> knownFrameRates = {Fps(24.0f), Fps(30.0f), Fps(45.0f), Fps(60.0f), Fps(72.0f)}; |
Ady Abraham | b1b9d41 | 2020-06-01 19:53:52 -0700 | [diff] [blame] | 696 | knownFrameRates.reserve(knownFrameRates.size() + configs.size()); |
| 697 | |
| 698 | // Add all supported refresh rates to the set |
| 699 | for (const auto& config : configs) { |
Marin Shalamanov | e8a663d | 2020-11-24 17:48:00 +0100 | [diff] [blame] | 700 | const auto refreshRate = Fps::fromPeriodNsecs(config->getVsyncPeriod()); |
Ady Abraham | b1b9d41 | 2020-06-01 19:53:52 -0700 | [diff] [blame] | 701 | knownFrameRates.emplace_back(refreshRate); |
| 702 | } |
| 703 | |
| 704 | // Sort and remove duplicates |
Marin Shalamanov | e8a663d | 2020-11-24 17:48:00 +0100 | [diff] [blame] | 705 | std::sort(knownFrameRates.begin(), knownFrameRates.end(), Fps::comparesLess); |
Ady Abraham | b1b9d41 | 2020-06-01 19:53:52 -0700 | [diff] [blame] | 706 | knownFrameRates.erase(std::unique(knownFrameRates.begin(), knownFrameRates.end(), |
Marin Shalamanov | e8a663d | 2020-11-24 17:48:00 +0100 | [diff] [blame] | 707 | Fps::EqualsWithMargin()), |
Ady Abraham | b1b9d41 | 2020-06-01 19:53:52 -0700 | [diff] [blame] | 708 | knownFrameRates.end()); |
| 709 | return knownFrameRates; |
| 710 | } |
| 711 | |
Marin Shalamanov | e8a663d | 2020-11-24 17:48:00 +0100 | [diff] [blame] | 712 | Fps RefreshRateConfigs::findClosestKnownFrameRate(Fps frameRate) const { |
| 713 | if (frameRate.lessThanOrEqualWithMargin(*mKnownFrameRates.begin())) { |
Ady Abraham | b1b9d41 | 2020-06-01 19:53:52 -0700 | [diff] [blame] | 714 | return *mKnownFrameRates.begin(); |
| 715 | } |
| 716 | |
Marin Shalamanov | e8a663d | 2020-11-24 17:48:00 +0100 | [diff] [blame] | 717 | if (frameRate.greaterThanOrEqualWithMargin(*std::prev(mKnownFrameRates.end()))) { |
Ady Abraham | b1b9d41 | 2020-06-01 19:53:52 -0700 | [diff] [blame] | 718 | return *std::prev(mKnownFrameRates.end()); |
| 719 | } |
| 720 | |
Marin Shalamanov | e8a663d | 2020-11-24 17:48:00 +0100 | [diff] [blame] | 721 | auto lowerBound = std::lower_bound(mKnownFrameRates.begin(), mKnownFrameRates.end(), frameRate, |
| 722 | Fps::comparesLess); |
Ady Abraham | b1b9d41 | 2020-06-01 19:53:52 -0700 | [diff] [blame] | 723 | |
Marin Shalamanov | e8a663d | 2020-11-24 17:48:00 +0100 | [diff] [blame] | 724 | const auto distance1 = std::abs((frameRate.getValue() - lowerBound->getValue())); |
| 725 | const auto distance2 = std::abs((frameRate.getValue() - std::prev(lowerBound)->getValue())); |
Ady Abraham | b1b9d41 | 2020-06-01 19:53:52 -0700 | [diff] [blame] | 726 | return distance1 < distance2 ? *lowerBound : *std::prev(lowerBound); |
| 727 | } |
| 728 | |
Ana Krulec | b9afd79 | 2020-06-11 13:16:15 -0700 | [diff] [blame] | 729 | RefreshRateConfigs::KernelIdleTimerAction RefreshRateConfigs::getIdleTimerAction() const { |
| 730 | std::lock_guard lock(mLock); |
| 731 | const auto& deviceMin = getMinRefreshRate(); |
| 732 | const auto& minByPolicy = getMinRefreshRateByPolicyLocked(); |
| 733 | const auto& maxByPolicy = getMaxRefreshRateByPolicyLocked(); |
| 734 | |
| 735 | // Kernel idle timer will set the refresh rate to the device min. If DisplayManager says that |
| 736 | // the min allowed refresh rate is higher than the device min, we do not want to enable the |
| 737 | // timer. |
| 738 | if (deviceMin < minByPolicy) { |
| 739 | return RefreshRateConfigs::KernelIdleTimerAction::TurnOff; |
| 740 | } |
| 741 | if (minByPolicy == maxByPolicy) { |
| 742 | // Do not sent the call to toggle off kernel idle timer if the device min and policy min and |
| 743 | // max are all the same. This saves us extra unnecessary calls to sysprop. |
| 744 | if (deviceMin == minByPolicy) { |
| 745 | return RefreshRateConfigs::KernelIdleTimerAction::NoChange; |
| 746 | } |
| 747 | return RefreshRateConfigs::KernelIdleTimerAction::TurnOff; |
| 748 | } |
| 749 | // Turn on the timer in all other cases. |
| 750 | return RefreshRateConfigs::KernelIdleTimerAction::TurnOn; |
| 751 | } |
| 752 | |
Ady Abraham | 2c6716b | 2020-12-08 16:54:10 -0800 | [diff] [blame] | 753 | int RefreshRateConfigs::getFrameRateDivider(Fps displayFrameRate, Fps layerFrameRate) { |
Ady Abraham | 62f216c | 2020-10-13 19:07:23 -0700 | [diff] [blame] | 754 | // This calculation needs to be in sync with the java code |
| 755 | // in DisplayManagerService.getDisplayInfoForFrameRateOverride |
| 756 | constexpr float kThreshold = 0.1f; |
Ady Abraham | 2c6716b | 2020-12-08 16:54:10 -0800 | [diff] [blame] | 757 | const auto numPeriods = displayFrameRate.getValue() / layerFrameRate.getValue(); |
Ady Abraham | 0bb6a47 | 2020-10-12 10:22:13 -0700 | [diff] [blame] | 758 | const auto numPeriodsRounded = std::round(numPeriods); |
| 759 | if (std::abs(numPeriods - numPeriodsRounded) > kThreshold) { |
Ady Abraham | 2c6716b | 2020-12-08 16:54:10 -0800 | [diff] [blame] | 760 | return 0; |
Ady Abraham | 0bb6a47 | 2020-10-12 10:22:13 -0700 | [diff] [blame] | 761 | } |
| 762 | |
Ady Abraham | 62f216c | 2020-10-13 19:07:23 -0700 | [diff] [blame] | 763 | return static_cast<int>(numPeriodsRounded); |
| 764 | } |
| 765 | |
Ady Abraham | 2c6716b | 2020-12-08 16:54:10 -0800 | [diff] [blame] | 766 | int RefreshRateConfigs::getRefreshRateDivider(Fps frameRate) const { |
Ady Abraham | 62f216c | 2020-10-13 19:07:23 -0700 | [diff] [blame] | 767 | std::lock_guard lock(mLock); |
Ady Abraham | 2c6716b | 2020-12-08 16:54:10 -0800 | [diff] [blame] | 768 | return getFrameRateDivider(mCurrentRefreshRate->getFps(), frameRate); |
Ady Abraham | 0bb6a47 | 2020-10-12 10:22:13 -0700 | [diff] [blame] | 769 | } |
| 770 | |
Marin Shalamanov | ba421a8 | 2020-11-10 21:49:26 +0100 | [diff] [blame] | 771 | void RefreshRateConfigs::dump(std::string& result) const { |
| 772 | std::lock_guard lock(mLock); |
| 773 | base::StringAppendF(&result, "DesiredDisplayConfigSpecs (DisplayManager): %s\n\n", |
| 774 | mDisplayManagerPolicy.toString().c_str()); |
| 775 | scheduler::RefreshRateConfigs::Policy currentPolicy = *getCurrentPolicyLocked(); |
| 776 | if (mOverridePolicy && currentPolicy != mDisplayManagerPolicy) { |
| 777 | base::StringAppendF(&result, "DesiredDisplayConfigSpecs (Override): %s\n\n", |
| 778 | currentPolicy.toString().c_str()); |
| 779 | } |
| 780 | |
| 781 | auto config = mCurrentRefreshRate->hwcConfig; |
| 782 | base::StringAppendF(&result, "Current config: %s\n", mCurrentRefreshRate->toString().c_str()); |
| 783 | |
| 784 | result.append("Refresh rates:\n"); |
| 785 | for (const auto& [id, refreshRate] : mRefreshRates) { |
| 786 | config = refreshRate->hwcConfig; |
| 787 | base::StringAppendF(&result, "\t%s\n", refreshRate->toString().c_str()); |
| 788 | } |
| 789 | |
| 790 | result.append("\n"); |
| 791 | } |
| 792 | |
Ady Abraham | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame] | 793 | } // namespace android::scheduler |