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