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