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