blob: c16387aa9d5f905c3829c6cf96f21d17617b4a67 [file] [log] [blame]
Ady Abraham8a82ba62020-01-17 12:43:17 -08001/*
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
28namespace android::scheduler {
29
Ady Abraham1adbb722020-05-15 11:51:48 -070030LayerInfoV2::LayerInfoV2(const std::string& name, nsecs_t highRefreshRatePeriod,
31 LayerHistory::LayerVoteType defaultVote)
32 : mName(name),
33 mHighRefreshRatePeriod(highRefreshRatePeriod),
Ady Abraham8a82ba62020-01-17 12:43:17 -080034 mDefaultVote(defaultVote),
35 mLayerVote({defaultVote, 0.0f}) {}
36
Ady Abraham32efd542020-05-19 17:49:26 -070037void LayerInfoV2::setLastPresentTime(nsecs_t lastPresentTime, nsecs_t now,
38 bool pendingConfigChange) {
Ady Abraham8a82ba62020-01-17 12:43:17 -080039 lastPresentTime = std::max(lastPresentTime, static_cast<nsecs_t>(0));
40
41 mLastUpdatedTime = std::max(lastPresentTime, now);
42
Ady Abraham32efd542020-05-19 17:49:26 -070043 FrameTimeData frameTime = {.presetTime = lastPresentTime,
44 .queueTime = mLastUpdatedTime,
45 .pendingConfigChange = pendingConfigChange};
Ady Abraham8a82ba62020-01-17 12:43:17 -080046
47 mFrameTimes.push_back(frameTime);
48 if (mFrameTimes.size() > HISTORY_SIZE) {
49 mFrameTimes.pop_front();
50 }
51}
52
Ady Abraham8a82ba62020-01-17 12:43:17 -080053bool LayerInfoV2::isFrequent(nsecs_t now) const {
Ady Abraham1adbb722020-05-15 11:51:48 -070054 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 Abraham4ccdcb42020-02-11 17:34:34 -080065 }
66 }
67
Ady Abraham1adbb722020-05-15 11:51:48 -070068 ALOGV("%s infrequent (not enough frames %zu)", mName.c_str(), mFrameTimes.size());
69 return false;
Ady Abraham8a82ba62020-01-17 12:43:17 -080070}
71
72bool LayerInfoV2::hasEnoughDataForHeuristic() const {
73 // The layer had to publish at least HISTORY_SIZE or HISTORY_TIME of updates
Ady Abrahama61edcb2020-01-30 18:32:03 -080074 if (mFrameTimes.size() < 2) {
75 return false;
76 }
77
Ady Abraham8a82ba62020-01-17 12:43:17 -080078 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 Abraham32efd542020-05-19 17:49:26 -070086std::pair<nsecs_t, bool> LayerInfoV2::calculateAverageFrameTime() const {
Ady Abraham8a82ba62020-01-17 12:43:17 -080087 nsecs_t totalPresentTimeDeltas = 0;
Ady Abrahamc9664832020-05-12 14:16:56 -070088 nsecs_t totalQueueTimeDeltas = 0;
Ady Abraham32efd542020-05-19 17:49:26 -070089 bool missingPresentTime = false;
90 int numFrames = 0;
Ady Abraham8a82ba62020-01-17 12:43:17 -080091 for (auto it = mFrameTimes.begin(); it != mFrameTimes.end() - 1; ++it) {
Ady Abraham32efd542020-05-19 17:49:26 -070092 // Ignore frames captured during a config change
93 if (it->pendingConfigChange || (it + 1)->pendingConfigChange) {
94 continue;
95 }
96
Ady Abrahamc9664832020-05-12 14:16:56 -070097 totalQueueTimeDeltas +=
98 std::max(((it + 1)->queueTime - it->queueTime), mHighRefreshRatePeriod);
Ady Abraham32efd542020-05-19 17:49:26 -070099 numFrames++;
Ady Abrahamc9664832020-05-12 14:16:56 -0700100
Ady Abraham8a82ba62020-01-17 12:43:17 -0800101 if (it->presetTime == 0 || (it + 1)->presetTime == 0) {
Ady Abrahamc9664832020-05-12 14:16:56 -0700102 missingPresentTime = true;
103 continue;
Ady Abraham8a82ba62020-01-17 12:43:17 -0800104 }
105
106 totalPresentTimeDeltas +=
107 std::max(((it + 1)->presetTime - it->presetTime), mHighRefreshRatePeriod);
108 }
Ady Abrahamc9664832020-05-12 14:16:56 -0700109
Ady Abrahamc9664832020-05-12 14:16:56 -0700110 // 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 Abraham32efd542020-05-19 17:49:26 -0700117 const auto averageFrameTime =
Ady Abrahamc9664832020-05-12 14:16:56 -0700118 static_cast<float>(missingPresentTime ? totalQueueTimeDeltas : totalPresentTimeDeltas) /
Ady Abraham32efd542020-05-19 17:49:26 -0700119 numFrames;
120 return {static_cast<nsecs_t>(averageFrameTime), missingPresentTime};
121}
Ady Abraham8a82ba62020-01-17 12:43:17 -0800122
Ady Abraham32efd542020-05-19 17:49:26 -0700123bool LayerInfoV2::isRefreshRateStable(nsecs_t averageFrameTime, bool missingPresentTime) const {
Ady Abraham8a82ba62020-01-17 12:43:17 -0800124 for (auto it = mFrameTimes.begin(); it != mFrameTimes.end() - 1; ++it) {
Ady Abraham32efd542020-05-19 17:49:26 -0700125 // Ignore frames captured during a config change
126 if (it->pendingConfigChange || (it + 1)->pendingConfigChange) {
127 continue;
128 }
Ady Abrahamc9664832020-05-12 14:16:56 -0700129 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 Abraham5f489bd2020-05-12 21:22:02 +0000135 if (std::abs(presentTimeDeltas - averageFrameTime) > 2 * averageFrameTime) {
Ady Abraham32efd542020-05-19 17:49:26 -0700136 return false;
Ady Abraham8a82ba62020-01-17 12:43:17 -0800137 }
138 }
139
Ady Abraham32efd542020-05-19 17:49:26 -0700140 return true;
141}
142
143std::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 Abraham8a82ba62020-01-17 12:43:17 -0800162 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
171std::pair<LayerHistory::LayerVoteType, float> LayerInfoV2::getRefreshRate(nsecs_t now) {
172 if (mLayerVote.type != LayerHistory::LayerVoteType::Heuristic) {
Ady Abraham1adbb722020-05-15 11:51:48 -0700173 ALOGV("%s voted %d ", mName.c_str(), static_cast<int>(mLayerVote.type));
Ady Abraham8a82ba62020-01-17 12:43:17 -0800174 return {mLayerVote.type, mLayerVote.fps};
175 }
176
177 if (!isFrequent(now)) {
Ady Abraham1adbb722020-05-15 11:51:48 -0700178 ALOGV("%s is infrequent", mName.c_str());
Ady Abraham8a82ba62020-01-17 12:43:17 -0800179 return {LayerHistory::LayerVoteType::Min, 0};
180 }
181
182 auto refreshRate = calculateRefreshRateIfPossible();
183 if (refreshRate.has_value()) {
Ady Abraham1adbb722020-05-15 11:51:48 -0700184 ALOGV("%s calculated refresh rate: %.2f", mName.c_str(), refreshRate.value());
Ady Abraham8a82ba62020-01-17 12:43:17 -0800185 return {LayerHistory::LayerVoteType::Heuristic, refreshRate.value()};
186 }
187
Ady Abraham1adbb722020-05-15 11:51:48 -0700188 ALOGV("%s Max (can't resolve refresh rate", mName.c_str());
Ady Abraham8a82ba62020-01-17 12:43:17 -0800189 return {LayerHistory::LayerVoteType::Max, 0};
190}
191
192} // namespace android::scheduler