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