blob: 0fa71f131d52e434a6914971acbacc605c889c74 [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
Marin Shalamanovbed7fd32020-12-21 20:02:20 +010017// TODO(b/129481165): remove the #pragma below and fix conversion issues
18#pragma clang diagnostic push
19#pragma clang diagnostic ignored "-Wextra"
20
Ady Abraham8a82ba62020-01-17 12:43:17 -080021// #define LOG_NDEBUG 0
Ady Abraham0ccd79b2020-06-10 10:11:17 -070022#define ATRACE_TAG ATRACE_TAG_GRAPHICS
Ady Abraham8a82ba62020-01-17 12:43:17 -080023
Marin Shalamanov1bc43ee2020-11-20 16:56:52 +010024#include "LayerInfo.h"
Ady Abraham8a82ba62020-01-17 12:43:17 -080025
26#include <algorithm>
27#include <utility>
28
Ady Abraham0ccd79b2020-06-10 10:11:17 -070029#include <cutils/compiler.h>
30#include <cutils/trace.h>
31
Ady Abraham8a82ba62020-01-17 12:43:17 -080032#undef LOG_TAG
Marin Shalamanov1bc43ee2020-11-20 16:56:52 +010033#define LOG_TAG "LayerInfo"
Ady Abraham8a82ba62020-01-17 12:43:17 -080034
35namespace android::scheduler {
36
Marin Shalamanov1bc43ee2020-11-20 16:56:52 +010037const RefreshRateConfigs* LayerInfo::sRefreshRateConfigs = nullptr;
38bool LayerInfo::sTraceEnabled = false;
Ady Abrahamb1b9d412020-06-01 19:53:52 -070039
Marin Shalamanov4ad8b302020-12-11 15:50:08 +010040LayerInfo::LayerInfo(const std::string& name, LayerHistory::LayerVoteType defaultVote)
Ady Abrahama6b676e2020-05-27 14:29:09 -070041 : mName(name),
Ady Abraham8a82ba62020-01-17 12:43:17 -080042 mDefaultVote(defaultVote),
Marin Shalamanove8a663d2020-11-24 17:48:00 +010043 mLayerVote({defaultVote, Fps(0.0f)}),
Ady Abraham0ccd79b2020-06-10 10:11:17 -070044 mRefreshRateHistory(name) {}
Ady Abraham8a82ba62020-01-17 12:43:17 -080045
Marin Shalamanov1bc43ee2020-11-20 16:56:52 +010046void LayerInfo::setLastPresentTime(nsecs_t lastPresentTime, nsecs_t now, LayerUpdateType updateType,
47 bool pendingConfigChange) {
Ady Abraham8a82ba62020-01-17 12:43:17 -080048 lastPresentTime = std::max(lastPresentTime, static_cast<nsecs_t>(0));
49
50 mLastUpdatedTime = std::max(lastPresentTime, now);
Ady Abraham5def7332020-05-29 16:13:47 -070051 switch (updateType) {
52 case LayerUpdateType::AnimationTX:
53 mLastAnimationTime = std::max(lastPresentTime, now);
54 break;
55 case LayerUpdateType::SetFrameRate:
56 case LayerUpdateType::Buffer:
57 FrameTimeData frameTime = {.presetTime = lastPresentTime,
58 .queueTime = mLastUpdatedTime,
59 .pendingConfigChange = pendingConfigChange};
60 mFrameTimes.push_back(frameTime);
61 if (mFrameTimes.size() > HISTORY_SIZE) {
62 mFrameTimes.pop_front();
63 }
64 break;
Ady Abraham8a82ba62020-01-17 12:43:17 -080065 }
66}
67
Marin Shalamanov1bc43ee2020-11-20 16:56:52 +010068bool LayerInfo::isFrameTimeValid(const FrameTimeData& frameTime) const {
Ady Abrahamdfb63ba2020-05-27 20:05:05 +000069 return frameTime.queueTime >= std::chrono::duration_cast<std::chrono::nanoseconds>(
70 mFrameTimeValidSince.time_since_epoch())
71 .count();
72}
Ady Abraham1adbb722020-05-15 11:51:48 -070073
Marin Shalamanov1bc43ee2020-11-20 16:56:52 +010074bool LayerInfo::isFrequent(nsecs_t now) const {
Ady Abrahamdfb63ba2020-05-27 20:05:05 +000075 // If we know nothing about this layer we consider it as frequent as it might be the start
76 // of an animation.
Ady Abraham983e5682020-05-28 16:49:18 -070077 if (mFrameTimes.size() < FREQUENT_LAYER_WINDOW_SIZE) {
Ady Abrahamdfb63ba2020-05-27 20:05:05 +000078 return true;
79 }
80
81 // Find the first active frame
Ady Abraham983e5682020-05-28 16:49:18 -070082 auto it = mFrameTimes.begin();
Ady Abrahamdfb63ba2020-05-27 20:05:05 +000083 for (; it != mFrameTimes.end(); ++it) {
84 if (it->queueTime >= getActiveLayerThreshold(now)) {
85 break;
86 }
87 }
88
89 const auto numFrames = std::distance(it, mFrameTimes.end());
90 if (numFrames < FREQUENT_LAYER_WINDOW_SIZE) {
91 return false;
92 }
93
94 // Layer is considered frequent if the average frame rate is higher than the threshold
95 const auto totalTime = mFrameTimes.back().queueTime - it->queueTime;
Marin Shalamanove8a663d2020-11-24 17:48:00 +010096 return Fps::fromPeriodNsecs(totalTime / (numFrames - 1))
97 .greaterThanOrEqualWithMargin(MIN_FPS_FOR_FREQUENT_LAYER);
Ady Abraham8a82ba62020-01-17 12:43:17 -080098}
99
Marin Shalamanov1bc43ee2020-11-20 16:56:52 +0100100bool LayerInfo::isAnimating(nsecs_t now) const {
Ady Abraham5def7332020-05-29 16:13:47 -0700101 return mLastAnimationTime >= getActiveLayerThreshold(now);
102}
103
Marin Shalamanov1bc43ee2020-11-20 16:56:52 +0100104bool LayerInfo::hasEnoughDataForHeuristic() const {
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700105 // The layer had to publish at least HISTORY_SIZE or HISTORY_DURATION of updates
Ady Abrahama61edcb2020-01-30 18:32:03 -0800106 if (mFrameTimes.size() < 2) {
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700107 ALOGV("fewer than 2 frames recorded: %zu", mFrameTimes.size());
Ady Abrahama61edcb2020-01-30 18:32:03 -0800108 return false;
109 }
110
Ady Abrahamdfb63ba2020-05-27 20:05:05 +0000111 if (!isFrameTimeValid(mFrameTimes.front())) {
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700112 ALOGV("stale frames still captured");
Ady Abrahamdfb63ba2020-05-27 20:05:05 +0000113 return false;
114 }
115
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700116 const auto totalDuration = mFrameTimes.back().queueTime - mFrameTimes.front().queueTime;
117 if (mFrameTimes.size() < HISTORY_SIZE && totalDuration < HISTORY_DURATION.count()) {
118 ALOGV("not enough frames captured: %zu | %.2f seconds", mFrameTimes.size(),
119 totalDuration / 1e9f);
Ady Abraham8a82ba62020-01-17 12:43:17 -0800120 return false;
121 }
122
123 return true;
124}
125
Marin Shalamanov1bc43ee2020-11-20 16:56:52 +0100126std::optional<nsecs_t> LayerInfo::calculateAverageFrameTime() const {
Ady Abraham8a82ba62020-01-17 12:43:17 -0800127 nsecs_t totalPresentTimeDeltas = 0;
Ady Abrahamc9664832020-05-12 14:16:56 -0700128 nsecs_t totalQueueTimeDeltas = 0;
Ady Abraham32efd542020-05-19 17:49:26 -0700129 bool missingPresentTime = false;
130 int numFrames = 0;
Ady Abraham8a82ba62020-01-17 12:43:17 -0800131 for (auto it = mFrameTimes.begin(); it != mFrameTimes.end() - 1; ++it) {
Ady Abraham32efd542020-05-19 17:49:26 -0700132 // Ignore frames captured during a config change
133 if (it->pendingConfigChange || (it + 1)->pendingConfigChange) {
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700134 return std::nullopt;
Ady Abraham32efd542020-05-19 17:49:26 -0700135 }
136
Ady Abrahamc9664832020-05-12 14:16:56 -0700137 totalQueueTimeDeltas +=
Marin Shalamanov4ad8b302020-12-11 15:50:08 +0100138 std::max(((it + 1)->queueTime - it->queueTime), kMinPeriodBetweenFrames);
Ady Abraham32efd542020-05-19 17:49:26 -0700139 numFrames++;
Ady Abrahamc9664832020-05-12 14:16:56 -0700140
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700141 if (!missingPresentTime && (it->presetTime == 0 || (it + 1)->presetTime == 0)) {
Ady Abrahamc9664832020-05-12 14:16:56 -0700142 missingPresentTime = true;
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700143 // If there are no presentation timestamps and we haven't calculated
144 // one in the past then we can't calculate the refresh rate
Marin Shalamanove8a663d2020-11-24 17:48:00 +0100145 if (!mLastRefreshRate.reported.isValid()) {
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700146 return std::nullopt;
147 }
Ady Abrahamc9664832020-05-12 14:16:56 -0700148 continue;
Ady Abraham8a82ba62020-01-17 12:43:17 -0800149 }
150
151 totalPresentTimeDeltas +=
Marin Shalamanov4ad8b302020-12-11 15:50:08 +0100152 std::max(((it + 1)->presetTime - it->presetTime), kMinPeriodBetweenFrames);
Ady Abraham8a82ba62020-01-17 12:43:17 -0800153 }
Ady Abrahamc9664832020-05-12 14:16:56 -0700154
Ady Abrahamc9664832020-05-12 14:16:56 -0700155 // Calculate the average frame time based on presentation timestamps. If those
156 // doesn't exist, we look at the time the buffer was queued only. We can do that only if
157 // we calculated a refresh rate based on presentation timestamps in the past. The reason
158 // we look at the queue time is to handle cases where hwui attaches presentation timestamps
159 // when implementing render ahead for specific refresh rates. When hwui no longer provides
160 // presentation timestamps we look at the queue time to see if the current refresh rate still
161 // matches the content.
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700162
Ady Abraham32efd542020-05-19 17:49:26 -0700163 const auto averageFrameTime =
Ady Abrahamc9664832020-05-12 14:16:56 -0700164 static_cast<float>(missingPresentTime ? totalQueueTimeDeltas : totalPresentTimeDeltas) /
Ady Abraham32efd542020-05-19 17:49:26 -0700165 numFrames;
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700166 return static_cast<nsecs_t>(averageFrameTime);
Ady Abraham32efd542020-05-19 17:49:26 -0700167}
Ady Abraham8a82ba62020-01-17 12:43:17 -0800168
Marin Shalamanove8a663d2020-11-24 17:48:00 +0100169std::optional<Fps> LayerInfo::calculateRefreshRateIfPossible(nsecs_t now) {
Ady Abraham32efd542020-05-19 17:49:26 -0700170 static constexpr float MARGIN = 1.0f; // 1Hz
Ady Abraham32efd542020-05-19 17:49:26 -0700171 if (!hasEnoughDataForHeuristic()) {
172 ALOGV("Not enough data");
173 return std::nullopt;
174 }
175
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700176 const auto averageFrameTime = calculateAverageFrameTime();
177 if (averageFrameTime.has_value()) {
Marin Shalamanove8a663d2020-11-24 17:48:00 +0100178 const auto refreshRate = Fps::fromPeriodNsecs(*averageFrameTime);
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700179 const bool refreshRateConsistent = mRefreshRateHistory.add(refreshRate, now);
180 if (refreshRateConsistent) {
181 const auto knownRefreshRate =
182 sRefreshRateConfigs->findClosestKnownFrameRate(refreshRate);
Ady Abraham32efd542020-05-19 17:49:26 -0700183
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700184 // To avoid oscillation, use the last calculated refresh rate if it is
185 // close enough
Marin Shalamanove8a663d2020-11-24 17:48:00 +0100186 if (std::abs(mLastRefreshRate.calculated.getValue() - refreshRate.getValue()) >
187 MARGIN &&
188 !mLastRefreshRate.reported.equalsWithMargin(knownRefreshRate)) {
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700189 mLastRefreshRate.calculated = refreshRate;
190 mLastRefreshRate.reported = knownRefreshRate;
191 }
192
Marin Shalamanove8a663d2020-11-24 17:48:00 +0100193 ALOGV("%s %s rounded to nearest known frame rate %s", mName.c_str(),
194 to_string(refreshRate).c_str(), to_string(mLastRefreshRate.reported).c_str());
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700195 } else {
Marin Shalamanove8a663d2020-11-24 17:48:00 +0100196 ALOGV("%s Not stable (%s) returning last known frame rate %s", mName.c_str(),
197 to_string(refreshRate).c_str(), to_string(mLastRefreshRate.reported).c_str());
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700198 }
Ady Abraham32efd542020-05-19 17:49:26 -0700199 }
200
Marin Shalamanove8a663d2020-11-24 17:48:00 +0100201 return mLastRefreshRate.reported.isValid() ? std::make_optional(mLastRefreshRate.reported)
202 : std::nullopt;
Ady Abraham8a82ba62020-01-17 12:43:17 -0800203}
204
Marin Shalamanov1bc43ee2020-11-20 16:56:52 +0100205LayerInfo::LayerVote LayerInfo::getRefreshRateVote(nsecs_t now) {
Ady Abraham8a82ba62020-01-17 12:43:17 -0800206 if (mLayerVote.type != LayerHistory::LayerVoteType::Heuristic) {
Ady Abrahama6b676e2020-05-27 14:29:09 -0700207 ALOGV("%s voted %d ", mName.c_str(), static_cast<int>(mLayerVote.type));
Marin Shalamanov46084422020-10-13 12:33:42 +0200208 return mLayerVote;
Ady Abraham8a82ba62020-01-17 12:43:17 -0800209 }
210
Ady Abraham5def7332020-05-29 16:13:47 -0700211 if (isAnimating(now)) {
212 ALOGV("%s is animating", mName.c_str());
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700213 mLastRefreshRate.animatingOrInfrequent = true;
Marin Shalamanove8a663d2020-11-24 17:48:00 +0100214 return {LayerHistory::LayerVoteType::Max, Fps(0.0f)};
Ady Abraham5def7332020-05-29 16:13:47 -0700215 }
216
Ady Abraham8a82ba62020-01-17 12:43:17 -0800217 if (!isFrequent(now)) {
Ady Abrahama6b676e2020-05-27 14:29:09 -0700218 ALOGV("%s is infrequent", mName.c_str());
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700219 mLastRefreshRate.animatingOrInfrequent = true;
Marin Shalamanove8a663d2020-11-24 17:48:00 +0100220 return {LayerHistory::LayerVoteType::Min, Fps(0.0f)};
Ady Abraham8a82ba62020-01-17 12:43:17 -0800221 }
222
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700223 // If the layer was previously tagged as animating or infrequent, we clear
224 // the history as it is likely the layer just changed its behavior
225 // and we should not look at stale data
226 if (mLastRefreshRate.animatingOrInfrequent) {
227 clearHistory(now);
228 }
229
230 auto refreshRate = calculateRefreshRateIfPossible(now);
Ady Abraham8a82ba62020-01-17 12:43:17 -0800231 if (refreshRate.has_value()) {
Marin Shalamanove8a663d2020-11-24 17:48:00 +0100232 ALOGV("%s calculated refresh rate: %s", mName.c_str(), to_string(*refreshRate).c_str());
Ady Abraham8a82ba62020-01-17 12:43:17 -0800233 return {LayerHistory::LayerVoteType::Heuristic, refreshRate.value()};
234 }
235
Ady Abrahama6b676e2020-05-27 14:29:09 -0700236 ALOGV("%s Max (can't resolve refresh rate)", mName.c_str());
Marin Shalamanove8a663d2020-11-24 17:48:00 +0100237 return {LayerHistory::LayerVoteType::Max, Fps(0.0f)};
Ady Abraham8a82ba62020-01-17 12:43:17 -0800238}
239
Marin Shalamanov1bc43ee2020-11-20 16:56:52 +0100240const char* LayerInfo::getTraceTag(android::scheduler::LayerHistory::LayerVoteType type) const {
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700241 if (mTraceTags.count(type) == 0) {
242 const auto tag = "LFPS " + mName + " " + RefreshRateConfigs::layerVoteTypeString(type);
243 mTraceTags.emplace(type, tag);
244 }
245
246 return mTraceTags.at(type).c_str();
247}
248
Marin Shalamanov1bc43ee2020-11-20 16:56:52 +0100249LayerInfo::RefreshRateHistory::HeuristicTraceTagData
250LayerInfo::RefreshRateHistory::makeHeuristicTraceTagData() const {
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700251 const std::string prefix = "LFPS ";
252 const std::string suffix = "Heuristic ";
253 return {.min = prefix + mName + suffix + "min",
254 .max = prefix + mName + suffix + "max",
255 .consistent = prefix + mName + suffix + "consistent",
256 .average = prefix + mName + suffix + "average"};
257}
258
Marin Shalamanov1bc43ee2020-11-20 16:56:52 +0100259void LayerInfo::RefreshRateHistory::clear() {
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700260 mRefreshRates.clear();
261}
262
Marin Shalamanove8a663d2020-11-24 17:48:00 +0100263bool LayerInfo::RefreshRateHistory::add(Fps refreshRate, nsecs_t now) {
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700264 mRefreshRates.push_back({refreshRate, now});
265 while (mRefreshRates.size() >= HISTORY_SIZE ||
266 now - mRefreshRates.front().timestamp > HISTORY_DURATION.count()) {
267 mRefreshRates.pop_front();
268 }
269
270 if (CC_UNLIKELY(sTraceEnabled)) {
271 if (!mHeuristicTraceTagData.has_value()) {
272 mHeuristicTraceTagData = makeHeuristicTraceTagData();
273 }
274
Marin Shalamanove8a663d2020-11-24 17:48:00 +0100275 ATRACE_INT(mHeuristicTraceTagData->average.c_str(), refreshRate.getIntValue());
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700276 }
277
278 return isConsistent();
279}
280
Marin Shalamanov1bc43ee2020-11-20 16:56:52 +0100281bool LayerInfo::RefreshRateHistory::isConsistent() const {
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700282 if (mRefreshRates.empty()) return true;
283
284 const auto max = std::max_element(mRefreshRates.begin(), mRefreshRates.end());
285 const auto min = std::min_element(mRefreshRates.begin(), mRefreshRates.end());
Marin Shalamanove8a663d2020-11-24 17:48:00 +0100286 const auto consistent =
287 max->refreshRate.getValue() - min->refreshRate.getValue() < MARGIN_CONSISTENT_FPS;
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700288
289 if (CC_UNLIKELY(sTraceEnabled)) {
290 if (!mHeuristicTraceTagData.has_value()) {
291 mHeuristicTraceTagData = makeHeuristicTraceTagData();
292 }
293
Marin Shalamanove8a663d2020-11-24 17:48:00 +0100294 ATRACE_INT(mHeuristicTraceTagData->max.c_str(), max->refreshRate.getIntValue());
295 ATRACE_INT(mHeuristicTraceTagData->min.c_str(), min->refreshRate.getIntValue());
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700296 ATRACE_INT(mHeuristicTraceTagData->consistent.c_str(), consistent);
297 }
298
299 return consistent;
300}
301
Ady Abraham8a82ba62020-01-17 12:43:17 -0800302} // namespace android::scheduler
Marin Shalamanovbed7fd32020-12-21 20:02:20 +0100303
304// TODO(b/129481165): remove the #pragma below and fix conversion issues
305#pragma clang diagnostic pop // ignored "-Wextra"