blob: 78f4433185b341b3b3c5d5b1a6e97183eadf651e [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 Abraham39db2c92020-05-21 14:20:33 -070053bool LayerInfoV2::isFrequent(nsecs_t now) {
54 mLastReportedIsFrequent = [&] {
55 for (auto it = mFrameTimes.crbegin(); it != mFrameTimes.crend(); ++it) {
56 if (now - it->queueTime >= MAX_FREQUENT_LAYER_PERIOD_NS.count()) {
57 ALOGV("%s infrequent (last frame is %.2fms ago)", mName.c_str(),
58 (now - mFrameTimes.back().queueTime) / 1e6f);
59 return false;
60 }
61
62 const auto numFrames = std::distance(mFrameTimes.crbegin(), it + 1);
63 if (numFrames >= FREQUENT_LAYER_WINDOW_SIZE) {
64 ALOGV("%s frequent (burst of %zu frames)", mName.c_str(), numFrames);
65 return true;
66 }
Ady Abraham1adbb722020-05-15 11:51:48 -070067 }
68
Ady Abraham39db2c92020-05-21 14:20:33 -070069 ALOGV("%s %sfrequent (not enough frames %zu)", mName.c_str(),
70 mLastReportedIsFrequent ? "" : "in", mFrameTimes.size());
71 return mLastReportedIsFrequent;
72 }();
Ady Abraham4ccdcb42020-02-11 17:34:34 -080073
Ady Abraham39db2c92020-05-21 14:20:33 -070074 return mLastReportedIsFrequent;
Ady Abraham8a82ba62020-01-17 12:43:17 -080075}
76
77bool LayerInfoV2::hasEnoughDataForHeuristic() const {
78 // The layer had to publish at least HISTORY_SIZE or HISTORY_TIME of updates
Ady Abrahama61edcb2020-01-30 18:32:03 -080079 if (mFrameTimes.size() < 2) {
80 return false;
81 }
82
Ady Abraham8a82ba62020-01-17 12:43:17 -080083 if (mFrameTimes.size() < HISTORY_SIZE &&
84 mFrameTimes.back().queueTime - mFrameTimes.front().queueTime < HISTORY_TIME.count()) {
85 return false;
86 }
87
88 return true;
89}
90
Ady Abraham32efd542020-05-19 17:49:26 -070091std::pair<nsecs_t, bool> LayerInfoV2::calculateAverageFrameTime() const {
Ady Abraham8a82ba62020-01-17 12:43:17 -080092 nsecs_t totalPresentTimeDeltas = 0;
Ady Abrahamc9664832020-05-12 14:16:56 -070093 nsecs_t totalQueueTimeDeltas = 0;
Ady Abraham32efd542020-05-19 17:49:26 -070094 bool missingPresentTime = false;
95 int numFrames = 0;
Ady Abraham8a82ba62020-01-17 12:43:17 -080096 for (auto it = mFrameTimes.begin(); it != mFrameTimes.end() - 1; ++it) {
Ady Abraham32efd542020-05-19 17:49:26 -070097 // Ignore frames captured during a config change
98 if (it->pendingConfigChange || (it + 1)->pendingConfigChange) {
99 continue;
100 }
101
Ady Abrahamc9664832020-05-12 14:16:56 -0700102 totalQueueTimeDeltas +=
103 std::max(((it + 1)->queueTime - it->queueTime), mHighRefreshRatePeriod);
Ady Abraham32efd542020-05-19 17:49:26 -0700104 numFrames++;
Ady Abrahamc9664832020-05-12 14:16:56 -0700105
Ady Abraham8a82ba62020-01-17 12:43:17 -0800106 if (it->presetTime == 0 || (it + 1)->presetTime == 0) {
Ady Abrahamc9664832020-05-12 14:16:56 -0700107 missingPresentTime = true;
108 continue;
Ady Abraham8a82ba62020-01-17 12:43:17 -0800109 }
110
111 totalPresentTimeDeltas +=
112 std::max(((it + 1)->presetTime - it->presetTime), mHighRefreshRatePeriod);
113 }
Ady Abrahamc9664832020-05-12 14:16:56 -0700114
Ady Abrahamc9664832020-05-12 14:16:56 -0700115 // Calculate the average frame time based on presentation timestamps. If those
116 // doesn't exist, we look at the time the buffer was queued only. We can do that only if
117 // we calculated a refresh rate based on presentation timestamps in the past. The reason
118 // we look at the queue time is to handle cases where hwui attaches presentation timestamps
119 // when implementing render ahead for specific refresh rates. When hwui no longer provides
120 // presentation timestamps we look at the queue time to see if the current refresh rate still
121 // matches the content.
Ady Abraham32efd542020-05-19 17:49:26 -0700122 const auto averageFrameTime =
Ady Abrahamc9664832020-05-12 14:16:56 -0700123 static_cast<float>(missingPresentTime ? totalQueueTimeDeltas : totalPresentTimeDeltas) /
Ady Abraham32efd542020-05-19 17:49:26 -0700124 numFrames;
125 return {static_cast<nsecs_t>(averageFrameTime), missingPresentTime};
126}
Ady Abraham8a82ba62020-01-17 12:43:17 -0800127
Ady Abraham32efd542020-05-19 17:49:26 -0700128bool LayerInfoV2::isRefreshRateStable(nsecs_t averageFrameTime, bool missingPresentTime) const {
Ady Abraham8a82ba62020-01-17 12:43:17 -0800129 for (auto it = mFrameTimes.begin(); it != mFrameTimes.end() - 1; ++it) {
Ady Abraham32efd542020-05-19 17:49:26 -0700130 // Ignore frames captured during a config change
131 if (it->pendingConfigChange || (it + 1)->pendingConfigChange) {
132 continue;
133 }
Ady Abrahamc9664832020-05-12 14:16:56 -0700134 const auto presentTimeDeltas = [&] {
135 const auto delta = missingPresentTime ? (it + 1)->queueTime - it->queueTime
136 : (it + 1)->presetTime - it->presetTime;
137 return std::max(delta, mHighRefreshRatePeriod);
138 }();
139
Ady Abraham5f489bd2020-05-12 21:22:02 +0000140 if (std::abs(presentTimeDeltas - averageFrameTime) > 2 * averageFrameTime) {
Ady Abraham32efd542020-05-19 17:49:26 -0700141 return false;
Ady Abraham8a82ba62020-01-17 12:43:17 -0800142 }
143 }
144
Ady Abraham32efd542020-05-19 17:49:26 -0700145 return true;
146}
147
148std::optional<float> LayerInfoV2::calculateRefreshRateIfPossible() {
149 static constexpr float MARGIN = 1.0f; // 1Hz
150
151 if (!hasEnoughDataForHeuristic()) {
152 ALOGV("Not enough data");
153 return std::nullopt;
154 }
155
156 const auto [averageFrameTime, missingPresentTime] = calculateAverageFrameTime();
157
158 // If there are no presentation timestamps provided we can't calculate the refresh rate
159 if (missingPresentTime && mLastReportedRefreshRate == 0) {
160 return std::nullopt;
161 }
162
163 if (!isRefreshRateStable(averageFrameTime, missingPresentTime)) {
164 return std::nullopt;
165 }
166
Ady Abraham8a82ba62020-01-17 12:43:17 -0800167 const auto refreshRate = 1e9f / averageFrameTime;
168 if (std::abs(refreshRate - mLastReportedRefreshRate) > MARGIN) {
169 mLastReportedRefreshRate = refreshRate;
170 }
171
172 ALOGV("Refresh rate: %.2f", mLastReportedRefreshRate);
173 return mLastReportedRefreshRate;
174}
175
176std::pair<LayerHistory::LayerVoteType, float> LayerInfoV2::getRefreshRate(nsecs_t now) {
177 if (mLayerVote.type != LayerHistory::LayerVoteType::Heuristic) {
Ady Abraham1adbb722020-05-15 11:51:48 -0700178 ALOGV("%s voted %d ", mName.c_str(), static_cast<int>(mLayerVote.type));
Ady Abraham8a82ba62020-01-17 12:43:17 -0800179 return {mLayerVote.type, mLayerVote.fps};
180 }
181
182 if (!isFrequent(now)) {
Ady Abraham1adbb722020-05-15 11:51:48 -0700183 ALOGV("%s is infrequent", mName.c_str());
Ady Abraham8a82ba62020-01-17 12:43:17 -0800184 return {LayerHistory::LayerVoteType::Min, 0};
185 }
186
187 auto refreshRate = calculateRefreshRateIfPossible();
188 if (refreshRate.has_value()) {
Ady Abraham1adbb722020-05-15 11:51:48 -0700189 ALOGV("%s calculated refresh rate: %.2f", mName.c_str(), refreshRate.value());
Ady Abraham8a82ba62020-01-17 12:43:17 -0800190 return {LayerHistory::LayerVoteType::Heuristic, refreshRate.value()};
191 }
192
Ady Abraham1adbb722020-05-15 11:51:48 -0700193 ALOGV("%s Max (can't resolve refresh rate", mName.c_str());
Ady Abraham8a82ba62020-01-17 12:43:17 -0800194 return {LayerHistory::LayerVoteType::Max, 0};
195}
196
197} // namespace android::scheduler