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