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 | |
| 17 | // #define LOG_NDEBUG 0 |
| 18 | |
| 19 | #include "LayerInfoV2.h" |
| 20 | |
| 21 | #include <algorithm> |
| 22 | #include <utility> |
| 23 | |
| 24 | #undef LOG_TAG |
| 25 | #define LOG_TAG "LayerInfoV2" |
| 26 | #define ATRACE_TAG ATRACE_TAG_GRAPHICS |
| 27 | |
| 28 | namespace android::scheduler { |
| 29 | |
Ady Abraham | 1adbb72 | 2020-05-15 11:51:48 -0700 | [diff] [blame] | 30 | LayerInfoV2::LayerInfoV2(const std::string& name, nsecs_t highRefreshRatePeriod, |
| 31 | LayerHistory::LayerVoteType defaultVote) |
| 32 | : mName(name), |
| 33 | mHighRefreshRatePeriod(highRefreshRatePeriod), |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 34 | mDefaultVote(defaultVote), |
| 35 | mLayerVote({defaultVote, 0.0f}) {} |
| 36 | |
Ady Abraham | 32efd54 | 2020-05-19 17:49:26 -0700 | [diff] [blame^] | 37 | void LayerInfoV2::setLastPresentTime(nsecs_t lastPresentTime, nsecs_t now, |
| 38 | bool pendingConfigChange) { |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 39 | lastPresentTime = std::max(lastPresentTime, static_cast<nsecs_t>(0)); |
| 40 | |
| 41 | mLastUpdatedTime = std::max(lastPresentTime, now); |
| 42 | |
Ady Abraham | 32efd54 | 2020-05-19 17:49:26 -0700 | [diff] [blame^] | 43 | FrameTimeData frameTime = {.presetTime = lastPresentTime, |
| 44 | .queueTime = mLastUpdatedTime, |
| 45 | .pendingConfigChange = pendingConfigChange}; |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 46 | |
| 47 | mFrameTimes.push_back(frameTime); |
| 48 | if (mFrameTimes.size() > HISTORY_SIZE) { |
| 49 | mFrameTimes.pop_front(); |
| 50 | } |
| 51 | } |
| 52 | |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 53 | bool LayerInfoV2::isFrequent(nsecs_t now) const { |
Ady Abraham | 1adbb72 | 2020-05-15 11:51:48 -0700 | [diff] [blame] | 54 | for (auto it = mFrameTimes.crbegin(); it != mFrameTimes.crend(); ++it) { |
| 55 | if (now - it->queueTime >= MAX_FREQUENT_LAYER_PERIOD_NS.count()) { |
| 56 | ALOGV("%s infrequent (last frame is %.2fms ago", mName.c_str(), |
| 57 | (now - mFrameTimes.back().queueTime) / 1e6f); |
| 58 | return false; |
| 59 | } |
| 60 | |
| 61 | const auto numFrames = std::distance(mFrameTimes.crbegin(), it + 1); |
| 62 | if (numFrames >= FREQUENT_LAYER_WINDOW_SIZE) { |
| 63 | ALOGV("%s frequent (burst of %zu frames", mName.c_str(), numFrames); |
| 64 | return true; |
Ady Abraham | 4ccdcb4 | 2020-02-11 17:34:34 -0800 | [diff] [blame] | 65 | } |
| 66 | } |
| 67 | |
Ady Abraham | 1adbb72 | 2020-05-15 11:51:48 -0700 | [diff] [blame] | 68 | ALOGV("%s infrequent (not enough frames %zu)", mName.c_str(), mFrameTimes.size()); |
| 69 | return false; |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | bool LayerInfoV2::hasEnoughDataForHeuristic() const { |
| 73 | // The layer had to publish at least HISTORY_SIZE or HISTORY_TIME of updates |
Ady Abraham | a61edcb | 2020-01-30 18:32:03 -0800 | [diff] [blame] | 74 | if (mFrameTimes.size() < 2) { |
| 75 | return false; |
| 76 | } |
| 77 | |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 78 | if (mFrameTimes.size() < HISTORY_SIZE && |
| 79 | mFrameTimes.back().queueTime - mFrameTimes.front().queueTime < HISTORY_TIME.count()) { |
| 80 | return false; |
| 81 | } |
| 82 | |
| 83 | return true; |
| 84 | } |
| 85 | |
Ady Abraham | 32efd54 | 2020-05-19 17:49:26 -0700 | [diff] [blame^] | 86 | std::pair<nsecs_t, bool> LayerInfoV2::calculateAverageFrameTime() const { |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 87 | nsecs_t totalPresentTimeDeltas = 0; |
Ady Abraham | c966483 | 2020-05-12 14:16:56 -0700 | [diff] [blame] | 88 | nsecs_t totalQueueTimeDeltas = 0; |
Ady Abraham | 32efd54 | 2020-05-19 17:49:26 -0700 | [diff] [blame^] | 89 | bool missingPresentTime = false; |
| 90 | int numFrames = 0; |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 91 | for (auto it = mFrameTimes.begin(); it != mFrameTimes.end() - 1; ++it) { |
Ady Abraham | 32efd54 | 2020-05-19 17:49:26 -0700 | [diff] [blame^] | 92 | // Ignore frames captured during a config change |
| 93 | if (it->pendingConfigChange || (it + 1)->pendingConfigChange) { |
| 94 | continue; |
| 95 | } |
| 96 | |
Ady Abraham | c966483 | 2020-05-12 14:16:56 -0700 | [diff] [blame] | 97 | totalQueueTimeDeltas += |
| 98 | std::max(((it + 1)->queueTime - it->queueTime), mHighRefreshRatePeriod); |
Ady Abraham | 32efd54 | 2020-05-19 17:49:26 -0700 | [diff] [blame^] | 99 | numFrames++; |
Ady Abraham | c966483 | 2020-05-12 14:16:56 -0700 | [diff] [blame] | 100 | |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 101 | if (it->presetTime == 0 || (it + 1)->presetTime == 0) { |
Ady Abraham | c966483 | 2020-05-12 14:16:56 -0700 | [diff] [blame] | 102 | missingPresentTime = true; |
| 103 | continue; |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | totalPresentTimeDeltas += |
| 107 | std::max(((it + 1)->presetTime - it->presetTime), mHighRefreshRatePeriod); |
| 108 | } |
Ady Abraham | c966483 | 2020-05-12 14:16:56 -0700 | [diff] [blame] | 109 | |
Ady Abraham | c966483 | 2020-05-12 14:16:56 -0700 | [diff] [blame] | 110 | // Calculate the average frame time based on presentation timestamps. If those |
| 111 | // doesn't exist, we look at the time the buffer was queued only. We can do that only if |
| 112 | // we calculated a refresh rate based on presentation timestamps in the past. The reason |
| 113 | // we look at the queue time is to handle cases where hwui attaches presentation timestamps |
| 114 | // when implementing render ahead for specific refresh rates. When hwui no longer provides |
| 115 | // presentation timestamps we look at the queue time to see if the current refresh rate still |
| 116 | // matches the content. |
Ady Abraham | 32efd54 | 2020-05-19 17:49:26 -0700 | [diff] [blame^] | 117 | const auto averageFrameTime = |
Ady Abraham | c966483 | 2020-05-12 14:16:56 -0700 | [diff] [blame] | 118 | static_cast<float>(missingPresentTime ? totalQueueTimeDeltas : totalPresentTimeDeltas) / |
Ady Abraham | 32efd54 | 2020-05-19 17:49:26 -0700 | [diff] [blame^] | 119 | numFrames; |
| 120 | return {static_cast<nsecs_t>(averageFrameTime), missingPresentTime}; |
| 121 | } |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 122 | |
Ady Abraham | 32efd54 | 2020-05-19 17:49:26 -0700 | [diff] [blame^] | 123 | bool LayerInfoV2::isRefreshRateStable(nsecs_t averageFrameTime, bool missingPresentTime) const { |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 124 | for (auto it = mFrameTimes.begin(); it != mFrameTimes.end() - 1; ++it) { |
Ady Abraham | 32efd54 | 2020-05-19 17:49:26 -0700 | [diff] [blame^] | 125 | // Ignore frames captured during a config change |
| 126 | if (it->pendingConfigChange || (it + 1)->pendingConfigChange) { |
| 127 | continue; |
| 128 | } |
Ady Abraham | c966483 | 2020-05-12 14:16:56 -0700 | [diff] [blame] | 129 | const auto presentTimeDeltas = [&] { |
| 130 | const auto delta = missingPresentTime ? (it + 1)->queueTime - it->queueTime |
| 131 | : (it + 1)->presetTime - it->presetTime; |
| 132 | return std::max(delta, mHighRefreshRatePeriod); |
| 133 | }(); |
| 134 | |
Ady Abraham | 5f489bd | 2020-05-12 21:22:02 +0000 | [diff] [blame] | 135 | if (std::abs(presentTimeDeltas - averageFrameTime) > 2 * averageFrameTime) { |
Ady Abraham | 32efd54 | 2020-05-19 17:49:26 -0700 | [diff] [blame^] | 136 | return false; |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 137 | } |
| 138 | } |
| 139 | |
Ady Abraham | 32efd54 | 2020-05-19 17:49:26 -0700 | [diff] [blame^] | 140 | return true; |
| 141 | } |
| 142 | |
| 143 | std::optional<float> LayerInfoV2::calculateRefreshRateIfPossible() { |
| 144 | static constexpr float MARGIN = 1.0f; // 1Hz |
| 145 | |
| 146 | if (!hasEnoughDataForHeuristic()) { |
| 147 | ALOGV("Not enough data"); |
| 148 | return std::nullopt; |
| 149 | } |
| 150 | |
| 151 | const auto [averageFrameTime, missingPresentTime] = calculateAverageFrameTime(); |
| 152 | |
| 153 | // If there are no presentation timestamps provided we can't calculate the refresh rate |
| 154 | if (missingPresentTime && mLastReportedRefreshRate == 0) { |
| 155 | return std::nullopt; |
| 156 | } |
| 157 | |
| 158 | if (!isRefreshRateStable(averageFrameTime, missingPresentTime)) { |
| 159 | return std::nullopt; |
| 160 | } |
| 161 | |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 162 | const auto refreshRate = 1e9f / averageFrameTime; |
| 163 | if (std::abs(refreshRate - mLastReportedRefreshRate) > MARGIN) { |
| 164 | mLastReportedRefreshRate = refreshRate; |
| 165 | } |
| 166 | |
| 167 | ALOGV("Refresh rate: %.2f", mLastReportedRefreshRate); |
| 168 | return mLastReportedRefreshRate; |
| 169 | } |
| 170 | |
| 171 | std::pair<LayerHistory::LayerVoteType, float> LayerInfoV2::getRefreshRate(nsecs_t now) { |
| 172 | if (mLayerVote.type != LayerHistory::LayerVoteType::Heuristic) { |
Ady Abraham | 1adbb72 | 2020-05-15 11:51:48 -0700 | [diff] [blame] | 173 | ALOGV("%s voted %d ", mName.c_str(), static_cast<int>(mLayerVote.type)); |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 174 | return {mLayerVote.type, mLayerVote.fps}; |
| 175 | } |
| 176 | |
| 177 | if (!isFrequent(now)) { |
Ady Abraham | 1adbb72 | 2020-05-15 11:51:48 -0700 | [diff] [blame] | 178 | ALOGV("%s is infrequent", mName.c_str()); |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 179 | return {LayerHistory::LayerVoteType::Min, 0}; |
| 180 | } |
| 181 | |
| 182 | auto refreshRate = calculateRefreshRateIfPossible(); |
| 183 | if (refreshRate.has_value()) { |
Ady Abraham | 1adbb72 | 2020-05-15 11:51:48 -0700 | [diff] [blame] | 184 | ALOGV("%s calculated refresh rate: %.2f", mName.c_str(), refreshRate.value()); |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 185 | return {LayerHistory::LayerVoteType::Heuristic, refreshRate.value()}; |
| 186 | } |
| 187 | |
Ady Abraham | 1adbb72 | 2020-05-15 11:51:48 -0700 | [diff] [blame] | 188 | ALOGV("%s Max (can't resolve refresh rate", mName.c_str()); |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 189 | return {LayerHistory::LayerVoteType::Max, 0}; |
| 190 | } |
| 191 | |
| 192 | } // namespace android::scheduler |