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