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