Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2020 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 | */ |
| 16 | |
Marin Shalamanov | bed7fd3 | 2020-12-21 20:02:20 +0100 | [diff] [blame] | 17 | // TODO(b/129481165): remove the #pragma below and fix conversion issues |
| 18 | #pragma clang diagnostic push |
| 19 | #pragma clang diagnostic ignored "-Wextra" |
| 20 | |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 21 | // #define LOG_NDEBUG 0 |
Ady Abraham | 0ccd79b | 2020-06-10 10:11:17 -0700 | [diff] [blame] | 22 | #define ATRACE_TAG ATRACE_TAG_GRAPHICS |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 23 | |
Marin Shalamanov | 1bc43ee | 2020-11-20 16:56:52 +0100 | [diff] [blame] | 24 | #include "LayerInfo.h" |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 25 | |
| 26 | #include <algorithm> |
| 27 | #include <utility> |
| 28 | |
Ady Abraham | 0ccd79b | 2020-06-10 10:11:17 -0700 | [diff] [blame] | 29 | #include <cutils/compiler.h> |
| 30 | #include <cutils/trace.h> |
Dominik Laskowski | f5d0ea5 | 2021-09-26 17:27:01 -0700 | [diff] [blame] | 31 | #include <ftl/enum.h> |
Ady Abraham | 73c3df5 | 2023-01-12 18:09:31 -0800 | [diff] [blame] | 32 | #include <gui/TraceUtils.h> |
Ady Abraham | 0ccd79b | 2020-06-10 10:11:17 -0700 | [diff] [blame] | 33 | |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 34 | #undef LOG_TAG |
Marin Shalamanov | 1bc43ee | 2020-11-20 16:56:52 +0100 | [diff] [blame] | 35 | #define LOG_TAG "LayerInfo" |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 36 | |
| 37 | namespace android::scheduler { |
| 38 | |
Marin Shalamanov | 1bc43ee | 2020-11-20 16:56:52 +0100 | [diff] [blame] | 39 | bool LayerInfo::sTraceEnabled = false; |
Ady Abraham | b1b9d41 | 2020-06-01 19:53:52 -0700 | [diff] [blame] | 40 | |
Ady Abraham | bdda8f0 | 2021-04-01 16:06:11 -0700 | [diff] [blame] | 41 | LayerInfo::LayerInfo(const std::string& name, uid_t ownerUid, |
| 42 | LayerHistory::LayerVoteType defaultVote) |
Ady Abraham | a6b676e | 2020-05-27 14:29:09 -0700 | [diff] [blame] | 43 | : mName(name), |
Ady Abraham | bdda8f0 | 2021-04-01 16:06:11 -0700 | [diff] [blame] | 44 | mOwnerUid(ownerUid), |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 45 | mDefaultVote(defaultVote), |
Dominik Laskowski | 6eab42d | 2021-09-13 14:34:13 -0700 | [diff] [blame] | 46 | mLayerVote({defaultVote, Fps()}), |
Vishnu Nair | ef68d6d | 2023-02-28 06:18:27 +0000 | [diff] [blame] | 47 | mLayerProps(std::make_unique<LayerProps>()), |
| 48 | mRefreshRateHistory(name) { |
| 49 | ; |
| 50 | } |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 51 | |
Marin Shalamanov | 1bc43ee | 2020-11-20 16:56:52 +0100 | [diff] [blame] | 52 | void LayerInfo::setLastPresentTime(nsecs_t lastPresentTime, nsecs_t now, LayerUpdateType updateType, |
Vishnu Nair | ef68d6d | 2023-02-28 06:18:27 +0000 | [diff] [blame] | 53 | bool pendingModeChange, const LayerProps& props) { |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 54 | lastPresentTime = std::max(lastPresentTime, static_cast<nsecs_t>(0)); |
| 55 | |
| 56 | mLastUpdatedTime = std::max(lastPresentTime, now); |
Vishnu Nair | ef68d6d | 2023-02-28 06:18:27 +0000 | [diff] [blame] | 57 | *mLayerProps = props; |
Ady Abraham | 5def733 | 2020-05-29 16:13:47 -0700 | [diff] [blame] | 58 | switch (updateType) { |
| 59 | case LayerUpdateType::AnimationTX: |
| 60 | mLastAnimationTime = std::max(lastPresentTime, now); |
| 61 | break; |
| 62 | case LayerUpdateType::SetFrameRate: |
| 63 | case LayerUpdateType::Buffer: |
Marin Shalamanov | 2045d5b | 2020-12-28 18:11:41 +0100 | [diff] [blame] | 64 | FrameTimeData frameTime = {.presentTime = lastPresentTime, |
Ady Abraham | 5def733 | 2020-05-29 16:13:47 -0700 | [diff] [blame] | 65 | .queueTime = mLastUpdatedTime, |
Arthur Hung | 0cc8a38 | 2023-06-02 01:35:52 +0000 | [diff] [blame] | 66 | .pendingModeChange = pendingModeChange, |
| 67 | .isSmallDirty = props.isSmallDirty}; |
Ady Abraham | 5def733 | 2020-05-29 16:13:47 -0700 | [diff] [blame] | 68 | mFrameTimes.push_back(frameTime); |
| 69 | if (mFrameTimes.size() > HISTORY_SIZE) { |
| 70 | mFrameTimes.pop_front(); |
| 71 | } |
| 72 | break; |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 73 | } |
| 74 | } |
| 75 | |
Marin Shalamanov | 1bc43ee | 2020-11-20 16:56:52 +0100 | [diff] [blame] | 76 | bool LayerInfo::isFrameTimeValid(const FrameTimeData& frameTime) const { |
Ady Abraham | dfb63ba | 2020-05-27 20:05:05 +0000 | [diff] [blame] | 77 | return frameTime.queueTime >= std::chrono::duration_cast<std::chrono::nanoseconds>( |
| 78 | mFrameTimeValidSince.time_since_epoch()) |
| 79 | .count(); |
| 80 | } |
Ady Abraham | 1adbb72 | 2020-05-15 11:51:48 -0700 | [diff] [blame] | 81 | |
ramindani | 63db24e | 2023-04-03 10:56:05 -0700 | [diff] [blame] | 82 | LayerInfo::Frequent LayerInfo::isFrequent(nsecs_t now) const { |
Ady Abraham | 86ac5c5 | 2023-01-11 15:24:03 -0800 | [diff] [blame] | 83 | // If we know nothing about this layer (e.g. after touch event), |
| 84 | // we consider it as frequent as it might be the start of an animation. |
Marin Shalamanov | 2045d5b | 2020-12-28 18:11:41 +0100 | [diff] [blame] | 85 | if (mFrameTimes.size() < kFrequentLayerWindowSize) { |
ramindani | 63db24e | 2023-04-03 10:56:05 -0700 | [diff] [blame] | 86 | return {/* isFrequent */ true, /* clearHistory */ false, /* isConclusive */ true}; |
Ady Abraham | dfb63ba | 2020-05-27 20:05:05 +0000 | [diff] [blame] | 87 | } |
Ady Abraham | 86ac5c5 | 2023-01-11 15:24:03 -0800 | [diff] [blame] | 88 | |
| 89 | // Non-active layers are also infrequent |
| 90 | if (mLastUpdatedTime < getActiveLayerThreshold(now)) { |
ramindani | 63db24e | 2023-04-03 10:56:05 -0700 | [diff] [blame] | 91 | return {/* isFrequent */ false, /* clearHistory */ false, /* isConclusive */ true}; |
Ady Abraham | 86ac5c5 | 2023-01-11 15:24:03 -0800 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | // We check whether we can classify this layer as frequent or infrequent: |
| 95 | // - frequent: a layer posted kFrequentLayerWindowSize within |
| 96 | // kMaxPeriodForFrequentLayerNs of each other. |
| 97 | // - infrequent: a layer posted kFrequentLayerWindowSize with longer |
| 98 | // gaps than kFrequentLayerWindowSize. |
| 99 | // If we can't determine the layer classification yet, we return the last |
| 100 | // classification. |
| 101 | bool isFrequent = true; |
| 102 | bool isInfrequent = true; |
Arthur Hung | 0cc8a38 | 2023-06-02 01:35:52 +0000 | [diff] [blame] | 103 | int32_t smallDirtyCount = 0; |
Ady Abraham | 86ac5c5 | 2023-01-11 15:24:03 -0800 | [diff] [blame] | 104 | const auto n = mFrameTimes.size() - 1; |
| 105 | for (size_t i = 0; i < kFrequentLayerWindowSize - 1; i++) { |
| 106 | if (mFrameTimes[n - i].queueTime - mFrameTimes[n - i - 1].queueTime < |
| 107 | kMaxPeriodForFrequentLayerNs.count()) { |
| 108 | isInfrequent = false; |
Arthur Hung | 0cc8a38 | 2023-06-02 01:35:52 +0000 | [diff] [blame] | 109 | if (mFrameTimes[n - i].presentTime == 0 && mFrameTimes[n - i].isSmallDirty) { |
| 110 | smallDirtyCount++; |
| 111 | } |
Ady Abraham | 86ac5c5 | 2023-01-11 15:24:03 -0800 | [diff] [blame] | 112 | } else { |
| 113 | isFrequent = false; |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | if (isFrequent || isInfrequent) { |
ramindani | 63db24e | 2023-04-03 10:56:05 -0700 | [diff] [blame] | 118 | // If the layer was previously inconclusive, we clear |
| 119 | // the history as indeterminate layers changed to frequent, |
| 120 | // and we should not look at the stale data. |
Arthur Hung | 0cc8a38 | 2023-06-02 01:35:52 +0000 | [diff] [blame] | 121 | return {isFrequent, isFrequent && !mIsFrequencyConclusive, /* isConclusive */ true, |
| 122 | /* isSmallDirty */ smallDirtyCount >= kNumSmallDirtyThreshold}; |
Ady Abraham | 86ac5c5 | 2023-01-11 15:24:03 -0800 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | // If we can't determine whether the layer is frequent or not, we return |
ramindani | 63db24e | 2023-04-03 10:56:05 -0700 | [diff] [blame] | 126 | // the last known classification and mark the layer frequency as inconclusive. |
| 127 | isFrequent = !mLastRefreshRate.infrequent; |
| 128 | |
| 129 | // If the layer was previously tagged as animating, we clear |
| 130 | // the history as it is likely the layer just changed its behavior, |
| 131 | // and we should not look at stale data. |
| 132 | return {isFrequent, isFrequent && mLastRefreshRate.animating, /* isConclusive */ false}; |
Nathaniel Nifong | 1303d91 | 2021-10-06 09:41:24 -0400 | [diff] [blame] | 133 | } |
Ady Abraham | dfb63ba | 2020-05-27 20:05:05 +0000 | [diff] [blame] | 134 | |
Nathaniel Nifong | 1303d91 | 2021-10-06 09:41:24 -0400 | [diff] [blame] | 135 | Fps LayerInfo::getFps(nsecs_t now) const { |
Ady Abraham | dfb63ba | 2020-05-27 20:05:05 +0000 | [diff] [blame] | 136 | // Find the first active frame |
Ady Abraham | 983e568 | 2020-05-28 16:49:18 -0700 | [diff] [blame] | 137 | auto it = mFrameTimes.begin(); |
Ady Abraham | dfb63ba | 2020-05-27 20:05:05 +0000 | [diff] [blame] | 138 | for (; it != mFrameTimes.end(); ++it) { |
| 139 | if (it->queueTime >= getActiveLayerThreshold(now)) { |
| 140 | break; |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | const auto numFrames = std::distance(it, mFrameTimes.end()); |
Marin Shalamanov | 2045d5b | 2020-12-28 18:11:41 +0100 | [diff] [blame] | 145 | if (numFrames < kFrequentLayerWindowSize) { |
Nathaniel Nifong | 1303d91 | 2021-10-06 09:41:24 -0400 | [diff] [blame] | 146 | return Fps(); |
Ady Abraham | dfb63ba | 2020-05-27 20:05:05 +0000 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | // Layer is considered frequent if the average frame rate is higher than the threshold |
| 150 | const auto totalTime = mFrameTimes.back().queueTime - it->queueTime; |
Nathaniel Nifong | 1303d91 | 2021-10-06 09:41:24 -0400 | [diff] [blame] | 151 | return Fps::fromPeriodNsecs(totalTime / (numFrames - 1)); |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 152 | } |
| 153 | |
Marin Shalamanov | 1bc43ee | 2020-11-20 16:56:52 +0100 | [diff] [blame] | 154 | bool LayerInfo::isAnimating(nsecs_t now) const { |
Ady Abraham | 5def733 | 2020-05-29 16:13:47 -0700 | [diff] [blame] | 155 | return mLastAnimationTime >= getActiveLayerThreshold(now); |
| 156 | } |
| 157 | |
Marin Shalamanov | 1bc43ee | 2020-11-20 16:56:52 +0100 | [diff] [blame] | 158 | bool LayerInfo::hasEnoughDataForHeuristic() const { |
Ady Abraham | 0ccd79b | 2020-06-10 10:11:17 -0700 | [diff] [blame] | 159 | // The layer had to publish at least HISTORY_SIZE or HISTORY_DURATION of updates |
Ady Abraham | a61edcb | 2020-01-30 18:32:03 -0800 | [diff] [blame] | 160 | if (mFrameTimes.size() < 2) { |
Ady Abraham | 0ccd79b | 2020-06-10 10:11:17 -0700 | [diff] [blame] | 161 | ALOGV("fewer than 2 frames recorded: %zu", mFrameTimes.size()); |
Ady Abraham | a61edcb | 2020-01-30 18:32:03 -0800 | [diff] [blame] | 162 | return false; |
| 163 | } |
| 164 | |
Ady Abraham | dfb63ba | 2020-05-27 20:05:05 +0000 | [diff] [blame] | 165 | if (!isFrameTimeValid(mFrameTimes.front())) { |
Ady Abraham | 0ccd79b | 2020-06-10 10:11:17 -0700 | [diff] [blame] | 166 | ALOGV("stale frames still captured"); |
Ady Abraham | dfb63ba | 2020-05-27 20:05:05 +0000 | [diff] [blame] | 167 | return false; |
| 168 | } |
| 169 | |
Ady Abraham | 0ccd79b | 2020-06-10 10:11:17 -0700 | [diff] [blame] | 170 | const auto totalDuration = mFrameTimes.back().queueTime - mFrameTimes.front().queueTime; |
| 171 | if (mFrameTimes.size() < HISTORY_SIZE && totalDuration < HISTORY_DURATION.count()) { |
| 172 | ALOGV("not enough frames captured: %zu | %.2f seconds", mFrameTimes.size(), |
| 173 | totalDuration / 1e9f); |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 174 | return false; |
| 175 | } |
| 176 | |
| 177 | return true; |
| 178 | } |
| 179 | |
Marin Shalamanov | 1bc43ee | 2020-11-20 16:56:52 +0100 | [diff] [blame] | 180 | std::optional<nsecs_t> LayerInfo::calculateAverageFrameTime() const { |
Marin Shalamanov | a7fe304 | 2021-01-29 21:02:08 +0100 | [diff] [blame] | 181 | // Ignore frames captured during a mode change |
| 182 | const bool isDuringModeChange = |
Marin Shalamanov | 2045d5b | 2020-12-28 18:11:41 +0100 | [diff] [blame] | 183 | std::any_of(mFrameTimes.begin(), mFrameTimes.end(), |
Marin Shalamanov | a7fe304 | 2021-01-29 21:02:08 +0100 | [diff] [blame] | 184 | [](const auto& frame) { return frame.pendingModeChange; }); |
| 185 | if (isDuringModeChange) { |
Marin Shalamanov | 2045d5b | 2020-12-28 18:11:41 +0100 | [diff] [blame] | 186 | return std::nullopt; |
| 187 | } |
Ady Abraham | 32efd54 | 2020-05-19 17:49:26 -0700 | [diff] [blame] | 188 | |
Marin Shalamanov | 2045d5b | 2020-12-28 18:11:41 +0100 | [diff] [blame] | 189 | const bool isMissingPresentTime = |
| 190 | std::any_of(mFrameTimes.begin(), mFrameTimes.end(), |
| 191 | [](auto frame) { return frame.presentTime == 0; }); |
| 192 | if (isMissingPresentTime && !mLastRefreshRate.reported.isValid()) { |
| 193 | // If there are no presentation timestamps and we haven't calculated |
| 194 | // one in the past then we can't calculate the refresh rate |
| 195 | return std::nullopt; |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 196 | } |
Ady Abraham | c966483 | 2020-05-12 14:16:56 -0700 | [diff] [blame] | 197 | |
Ady Abraham | c966483 | 2020-05-12 14:16:56 -0700 | [diff] [blame] | 198 | // Calculate the average frame time based on presentation timestamps. If those |
| 199 | // doesn't exist, we look at the time the buffer was queued only. We can do that only if |
| 200 | // we calculated a refresh rate based on presentation timestamps in the past. The reason |
| 201 | // we look at the queue time is to handle cases where hwui attaches presentation timestamps |
| 202 | // when implementing render ahead for specific refresh rates. When hwui no longer provides |
| 203 | // presentation timestamps we look at the queue time to see if the current refresh rate still |
| 204 | // matches the content. |
Ady Abraham | 0ccd79b | 2020-06-10 10:11:17 -0700 | [diff] [blame] | 205 | |
Marin Shalamanov | 2045d5b | 2020-12-28 18:11:41 +0100 | [diff] [blame] | 206 | auto getFrameTime = isMissingPresentTime ? [](FrameTimeData data) { return data.queueTime; } |
| 207 | : [](FrameTimeData data) { return data.presentTime; }; |
| 208 | |
| 209 | nsecs_t totalDeltas = 0; |
| 210 | int numDeltas = 0; |
Arthur Hung | 0cc8a38 | 2023-06-02 01:35:52 +0000 | [diff] [blame] | 211 | int32_t smallDirtyCount = 0; |
Marin Shalamanov | 2045d5b | 2020-12-28 18:11:41 +0100 | [diff] [blame] | 212 | auto prevFrame = mFrameTimes.begin(); |
| 213 | for (auto it = mFrameTimes.begin() + 1; it != mFrameTimes.end(); ++it) { |
| 214 | const auto currDelta = getFrameTime(*it) - getFrameTime(*prevFrame); |
| 215 | if (currDelta < kMinPeriodBetweenFrames) { |
| 216 | // Skip this frame, but count the delta into the next frame |
| 217 | continue; |
| 218 | } |
| 219 | |
Arthur Hung | 0cc8a38 | 2023-06-02 01:35:52 +0000 | [diff] [blame] | 220 | // If this is a small area update, we don't want to consider it for calculating the average |
| 221 | // frame time. Instead, we let the bigger frame updates to drive the calculation. |
| 222 | if (it->isSmallDirty && currDelta < kMinPeriodBetweenSmallDirtyFrames) { |
| 223 | smallDirtyCount++; |
| 224 | continue; |
| 225 | } |
| 226 | |
Marin Shalamanov | 2045d5b | 2020-12-28 18:11:41 +0100 | [diff] [blame] | 227 | prevFrame = it; |
| 228 | |
| 229 | if (currDelta > kMaxPeriodBetweenFrames) { |
| 230 | // Skip this frame and the current delta. |
| 231 | continue; |
| 232 | } |
| 233 | |
| 234 | totalDeltas += currDelta; |
| 235 | numDeltas++; |
| 236 | } |
| 237 | |
Arthur Hung | 0cc8a38 | 2023-06-02 01:35:52 +0000 | [diff] [blame] | 238 | if (smallDirtyCount > 0) { |
| 239 | ATRACE_FORMAT_INSTANT("small dirty = %" PRIu32, smallDirtyCount); |
| 240 | } |
| 241 | |
Marin Shalamanov | 2045d5b | 2020-12-28 18:11:41 +0100 | [diff] [blame] | 242 | if (numDeltas == 0) { |
| 243 | return std::nullopt; |
| 244 | } |
| 245 | |
| 246 | const auto averageFrameTime = static_cast<double>(totalDeltas) / static_cast<double>(numDeltas); |
Ady Abraham | 0ccd79b | 2020-06-10 10:11:17 -0700 | [diff] [blame] | 247 | return static_cast<nsecs_t>(averageFrameTime); |
Ady Abraham | 32efd54 | 2020-05-19 17:49:26 -0700 | [diff] [blame] | 248 | } |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 249 | |
Dominik Laskowski | d82e0f0 | 2022-10-26 15:23:04 -0400 | [diff] [blame] | 250 | std::optional<Fps> LayerInfo::calculateRefreshRateIfPossible(const RefreshRateSelector& selector, |
| 251 | nsecs_t now) { |
Ady Abraham | 73c3df5 | 2023-01-12 18:09:31 -0800 | [diff] [blame] | 252 | ATRACE_CALL(); |
Ady Abraham | 32efd54 | 2020-05-19 17:49:26 -0700 | [diff] [blame] | 253 | static constexpr float MARGIN = 1.0f; // 1Hz |
Ady Abraham | 32efd54 | 2020-05-19 17:49:26 -0700 | [diff] [blame] | 254 | if (!hasEnoughDataForHeuristic()) { |
| 255 | ALOGV("Not enough data"); |
| 256 | return std::nullopt; |
| 257 | } |
| 258 | |
Dominik Laskowski | 6eab42d | 2021-09-13 14:34:13 -0700 | [diff] [blame] | 259 | if (const auto averageFrameTime = calculateAverageFrameTime()) { |
Marin Shalamanov | e8a663d | 2020-11-24 17:48:00 +0100 | [diff] [blame] | 260 | const auto refreshRate = Fps::fromPeriodNsecs(*averageFrameTime); |
Ady Abraham | 0ccd79b | 2020-06-10 10:11:17 -0700 | [diff] [blame] | 261 | const bool refreshRateConsistent = mRefreshRateHistory.add(refreshRate, now); |
| 262 | if (refreshRateConsistent) { |
Dominik Laskowski | d82e0f0 | 2022-10-26 15:23:04 -0400 | [diff] [blame] | 263 | const auto knownRefreshRate = selector.findClosestKnownFrameRate(refreshRate); |
Dominik Laskowski | 6eab42d | 2021-09-13 14:34:13 -0700 | [diff] [blame] | 264 | using fps_approx_ops::operator!=; |
| 265 | |
| 266 | // To avoid oscillation, use the last calculated refresh rate if it is close enough. |
Marin Shalamanov | e8a663d | 2020-11-24 17:48:00 +0100 | [diff] [blame] | 267 | if (std::abs(mLastRefreshRate.calculated.getValue() - refreshRate.getValue()) > |
| 268 | MARGIN && |
Dominik Laskowski | 6eab42d | 2021-09-13 14:34:13 -0700 | [diff] [blame] | 269 | mLastRefreshRate.reported != knownRefreshRate) { |
Ady Abraham | 0ccd79b | 2020-06-10 10:11:17 -0700 | [diff] [blame] | 270 | mLastRefreshRate.calculated = refreshRate; |
| 271 | mLastRefreshRate.reported = knownRefreshRate; |
| 272 | } |
| 273 | |
Marin Shalamanov | e8a663d | 2020-11-24 17:48:00 +0100 | [diff] [blame] | 274 | ALOGV("%s %s rounded to nearest known frame rate %s", mName.c_str(), |
| 275 | to_string(refreshRate).c_str(), to_string(mLastRefreshRate.reported).c_str()); |
Ady Abraham | 0ccd79b | 2020-06-10 10:11:17 -0700 | [diff] [blame] | 276 | } else { |
Marin Shalamanov | e8a663d | 2020-11-24 17:48:00 +0100 | [diff] [blame] | 277 | ALOGV("%s Not stable (%s) returning last known frame rate %s", mName.c_str(), |
| 278 | to_string(refreshRate).c_str(), to_string(mLastRefreshRate.reported).c_str()); |
Ady Abraham | 0ccd79b | 2020-06-10 10:11:17 -0700 | [diff] [blame] | 279 | } |
Ady Abraham | 32efd54 | 2020-05-19 17:49:26 -0700 | [diff] [blame] | 280 | } |
| 281 | |
Marin Shalamanov | e8a663d | 2020-11-24 17:48:00 +0100 | [diff] [blame] | 282 | return mLastRefreshRate.reported.isValid() ? std::make_optional(mLastRefreshRate.reported) |
| 283 | : std::nullopt; |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 284 | } |
| 285 | |
Dominik Laskowski | d82e0f0 | 2022-10-26 15:23:04 -0400 | [diff] [blame] | 286 | LayerInfo::LayerVote LayerInfo::getRefreshRateVote(const RefreshRateSelector& selector, |
Ady Abraham | 3efa394 | 2021-06-24 19:01:25 -0700 | [diff] [blame] | 287 | nsecs_t now) { |
Ady Abraham | 73c3df5 | 2023-01-12 18:09:31 -0800 | [diff] [blame] | 288 | ATRACE_CALL(); |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 289 | if (mLayerVote.type != LayerHistory::LayerVoteType::Heuristic) { |
Ady Abraham | a6b676e | 2020-05-27 14:29:09 -0700 | [diff] [blame] | 290 | ALOGV("%s voted %d ", mName.c_str(), static_cast<int>(mLayerVote.type)); |
Marin Shalamanov | 4608442 | 2020-10-13 12:33:42 +0200 | [diff] [blame] | 291 | return mLayerVote; |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 292 | } |
| 293 | |
Ady Abraham | 5def733 | 2020-05-29 16:13:47 -0700 | [diff] [blame] | 294 | if (isAnimating(now)) { |
Ady Abraham | 73c3df5 | 2023-01-12 18:09:31 -0800 | [diff] [blame] | 295 | ATRACE_FORMAT_INSTANT("animating"); |
Ady Abraham | 5def733 | 2020-05-29 16:13:47 -0700 | [diff] [blame] | 296 | ALOGV("%s is animating", mName.c_str()); |
Ady Abraham | 86ac5c5 | 2023-01-11 15:24:03 -0800 | [diff] [blame] | 297 | mLastRefreshRate.animating = true; |
Dominik Laskowski | 6eab42d | 2021-09-13 14:34:13 -0700 | [diff] [blame] | 298 | return {LayerHistory::LayerVoteType::Max, Fps()}; |
Ady Abraham | 5def733 | 2020-05-29 16:13:47 -0700 | [diff] [blame] | 299 | } |
| 300 | |
ramindani | 63db24e | 2023-04-03 10:56:05 -0700 | [diff] [blame] | 301 | const LayerInfo::Frequent frequent = isFrequent(now); |
| 302 | mIsFrequencyConclusive = frequent.isConclusive; |
| 303 | if (!frequent.isFrequent) { |
Ady Abraham | 73c3df5 | 2023-01-12 18:09:31 -0800 | [diff] [blame] | 304 | ATRACE_FORMAT_INSTANT("infrequent"); |
Ady Abraham | a6b676e | 2020-05-27 14:29:09 -0700 | [diff] [blame] | 305 | ALOGV("%s is infrequent", mName.c_str()); |
Ady Abraham | 86ac5c5 | 2023-01-11 15:24:03 -0800 | [diff] [blame] | 306 | mLastRefreshRate.infrequent = true; |
ramindani | 63db24e | 2023-04-03 10:56:05 -0700 | [diff] [blame] | 307 | // Infrequent layers vote for minimal refresh rate for |
Marin Shalamanov | 29e2540 | 2021-04-07 21:09:58 +0200 | [diff] [blame] | 308 | // battery saving purposes and also to prevent b/135718869. |
Dominik Laskowski | 6eab42d | 2021-09-13 14:34:13 -0700 | [diff] [blame] | 309 | return {LayerHistory::LayerVoteType::Min, Fps()}; |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 310 | } |
| 311 | |
ramindani | 63db24e | 2023-04-03 10:56:05 -0700 | [diff] [blame] | 312 | if (frequent.clearHistory) { |
Ady Abraham | 0ccd79b | 2020-06-10 10:11:17 -0700 | [diff] [blame] | 313 | clearHistory(now); |
| 314 | } |
| 315 | |
Arthur Hung | 0cc8a38 | 2023-06-02 01:35:52 +0000 | [diff] [blame] | 316 | // Return no vote if the latest frames are small dirty. |
| 317 | if (frequent.isSmallDirty && !mLastRefreshRate.reported.isValid()) { |
| 318 | ATRACE_FORMAT_INSTANT("NoVote (small dirty)"); |
| 319 | ALOGV("%s is small dirty", mName.c_str()); |
| 320 | return {LayerHistory::LayerVoteType::NoVote, Fps()}; |
| 321 | } |
| 322 | |
Dominik Laskowski | d82e0f0 | 2022-10-26 15:23:04 -0400 | [diff] [blame] | 323 | auto refreshRate = calculateRefreshRateIfPossible(selector, now); |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 324 | if (refreshRate.has_value()) { |
Marin Shalamanov | e8a663d | 2020-11-24 17:48:00 +0100 | [diff] [blame] | 325 | ALOGV("%s calculated refresh rate: %s", mName.c_str(), to_string(*refreshRate).c_str()); |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 326 | return {LayerHistory::LayerVoteType::Heuristic, refreshRate.value()}; |
| 327 | } |
| 328 | |
Ady Abraham | a6b676e | 2020-05-27 14:29:09 -0700 | [diff] [blame] | 329 | ALOGV("%s Max (can't resolve refresh rate)", mName.c_str()); |
Dominik Laskowski | 6eab42d | 2021-09-13 14:34:13 -0700 | [diff] [blame] | 330 | return {LayerHistory::LayerVoteType::Max, Fps()}; |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 331 | } |
| 332 | |
Dominik Laskowski | f5d0ea5 | 2021-09-26 17:27:01 -0700 | [diff] [blame] | 333 | const char* LayerInfo::getTraceTag(LayerHistory::LayerVoteType type) const { |
Ady Abraham | 0ccd79b | 2020-06-10 10:11:17 -0700 | [diff] [blame] | 334 | if (mTraceTags.count(type) == 0) { |
Dominik Laskowski | f5d0ea5 | 2021-09-26 17:27:01 -0700 | [diff] [blame] | 335 | auto tag = "LFPS " + mName + " " + ftl::enum_string(type); |
| 336 | mTraceTags.emplace(type, std::move(tag)); |
Ady Abraham | 0ccd79b | 2020-06-10 10:11:17 -0700 | [diff] [blame] | 337 | } |
| 338 | |
| 339 | return mTraceTags.at(type).c_str(); |
| 340 | } |
| 341 | |
Vishnu Nair | ef68d6d | 2023-02-28 06:18:27 +0000 | [diff] [blame] | 342 | LayerInfo::FrameRate LayerInfo::getSetFrameRateVote() const { |
| 343 | return mLayerProps->setFrameRateVote; |
| 344 | } |
| 345 | |
| 346 | bool LayerInfo::isVisible() const { |
| 347 | return mLayerProps->visible; |
| 348 | } |
| 349 | |
| 350 | int32_t LayerInfo::getFrameRateSelectionPriority() const { |
| 351 | return mLayerProps->frameRateSelectionPriority; |
| 352 | } |
| 353 | |
| 354 | FloatRect LayerInfo::getBounds() const { |
| 355 | return mLayerProps->bounds; |
| 356 | } |
| 357 | |
| 358 | ui::Transform LayerInfo::getTransform() const { |
| 359 | return mLayerProps->transform; |
| 360 | } |
| 361 | |
Marin Shalamanov | 1bc43ee | 2020-11-20 16:56:52 +0100 | [diff] [blame] | 362 | LayerInfo::RefreshRateHistory::HeuristicTraceTagData |
| 363 | LayerInfo::RefreshRateHistory::makeHeuristicTraceTagData() const { |
Ady Abraham | 0ccd79b | 2020-06-10 10:11:17 -0700 | [diff] [blame] | 364 | const std::string prefix = "LFPS "; |
| 365 | const std::string suffix = "Heuristic "; |
| 366 | return {.min = prefix + mName + suffix + "min", |
| 367 | .max = prefix + mName + suffix + "max", |
| 368 | .consistent = prefix + mName + suffix + "consistent", |
| 369 | .average = prefix + mName + suffix + "average"}; |
| 370 | } |
| 371 | |
Marin Shalamanov | 1bc43ee | 2020-11-20 16:56:52 +0100 | [diff] [blame] | 372 | void LayerInfo::RefreshRateHistory::clear() { |
Ady Abraham | 0ccd79b | 2020-06-10 10:11:17 -0700 | [diff] [blame] | 373 | mRefreshRates.clear(); |
| 374 | } |
| 375 | |
Marin Shalamanov | e8a663d | 2020-11-24 17:48:00 +0100 | [diff] [blame] | 376 | bool LayerInfo::RefreshRateHistory::add(Fps refreshRate, nsecs_t now) { |
Ady Abraham | 0ccd79b | 2020-06-10 10:11:17 -0700 | [diff] [blame] | 377 | mRefreshRates.push_back({refreshRate, now}); |
| 378 | while (mRefreshRates.size() >= HISTORY_SIZE || |
| 379 | now - mRefreshRates.front().timestamp > HISTORY_DURATION.count()) { |
| 380 | mRefreshRates.pop_front(); |
| 381 | } |
| 382 | |
| 383 | if (CC_UNLIKELY(sTraceEnabled)) { |
| 384 | if (!mHeuristicTraceTagData.has_value()) { |
| 385 | mHeuristicTraceTagData = makeHeuristicTraceTagData(); |
| 386 | } |
| 387 | |
Marin Shalamanov | e8a663d | 2020-11-24 17:48:00 +0100 | [diff] [blame] | 388 | ATRACE_INT(mHeuristicTraceTagData->average.c_str(), refreshRate.getIntValue()); |
Ady Abraham | 0ccd79b | 2020-06-10 10:11:17 -0700 | [diff] [blame] | 389 | } |
| 390 | |
| 391 | return isConsistent(); |
| 392 | } |
| 393 | |
Marin Shalamanov | 1bc43ee | 2020-11-20 16:56:52 +0100 | [diff] [blame] | 394 | bool LayerInfo::RefreshRateHistory::isConsistent() const { |
Ady Abraham | 0ccd79b | 2020-06-10 10:11:17 -0700 | [diff] [blame] | 395 | if (mRefreshRates.empty()) return true; |
| 396 | |
Dominik Laskowski | 6eab42d | 2021-09-13 14:34:13 -0700 | [diff] [blame] | 397 | const auto [min, max] = |
| 398 | std::minmax_element(mRefreshRates.begin(), mRefreshRates.end(), |
| 399 | [](const auto& lhs, const auto& rhs) { |
| 400 | return isStrictlyLess(lhs.refreshRate, rhs.refreshRate); |
| 401 | }); |
| 402 | |
| 403 | const bool consistent = |
Marin Shalamanov | e8a663d | 2020-11-24 17:48:00 +0100 | [diff] [blame] | 404 | max->refreshRate.getValue() - min->refreshRate.getValue() < MARGIN_CONSISTENT_FPS; |
Ady Abraham | 0ccd79b | 2020-06-10 10:11:17 -0700 | [diff] [blame] | 405 | |
| 406 | if (CC_UNLIKELY(sTraceEnabled)) { |
| 407 | if (!mHeuristicTraceTagData.has_value()) { |
| 408 | mHeuristicTraceTagData = makeHeuristicTraceTagData(); |
| 409 | } |
| 410 | |
Marin Shalamanov | e8a663d | 2020-11-24 17:48:00 +0100 | [diff] [blame] | 411 | ATRACE_INT(mHeuristicTraceTagData->max.c_str(), max->refreshRate.getIntValue()); |
| 412 | ATRACE_INT(mHeuristicTraceTagData->min.c_str(), min->refreshRate.getIntValue()); |
Ady Abraham | 0ccd79b | 2020-06-10 10:11:17 -0700 | [diff] [blame] | 413 | ATRACE_INT(mHeuristicTraceTagData->consistent.c_str(), consistent); |
| 414 | } |
| 415 | |
| 416 | return consistent; |
| 417 | } |
| 418 | |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 419 | } // namespace android::scheduler |
Marin Shalamanov | bed7fd3 | 2020-12-21 20:02:20 +0100 | [diff] [blame] | 420 | |
| 421 | // TODO(b/129481165): remove the #pragma below and fix conversion issues |
Dominik Laskowski | 6eab42d | 2021-09-13 14:34:13 -0700 | [diff] [blame] | 422 | #pragma clang diagnostic pop // ignored "-Wextra" |