blob: 8a6d52a110e04c331ba5f3860dfa12e852759e88 [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 Abrahamdfb63ba2020-05-27 20:05:05 +000030LayerInfoV2::LayerInfoV2(nsecs_t highRefreshRatePeriod, LayerHistory::LayerVoteType defaultVote)
31 : mHighRefreshRatePeriod(highRefreshRatePeriod),
Ady Abraham8a82ba62020-01-17 12:43:17 -080032 mDefaultVote(defaultVote),
33 mLayerVote({defaultVote, 0.0f}) {}
34
Ady Abraham32efd542020-05-19 17:49:26 -070035void LayerInfoV2::setLastPresentTime(nsecs_t lastPresentTime, nsecs_t now,
36 bool pendingConfigChange) {
Ady Abraham8a82ba62020-01-17 12:43:17 -080037 lastPresentTime = std::max(lastPresentTime, static_cast<nsecs_t>(0));
38
39 mLastUpdatedTime = std::max(lastPresentTime, now);
40
Ady Abraham32efd542020-05-19 17:49:26 -070041 FrameTimeData frameTime = {.presetTime = lastPresentTime,
42 .queueTime = mLastUpdatedTime,
43 .pendingConfigChange = pendingConfigChange};
Ady Abraham8a82ba62020-01-17 12:43:17 -080044
45 mFrameTimes.push_back(frameTime);
46 if (mFrameTimes.size() > HISTORY_SIZE) {
47 mFrameTimes.pop_front();
48 }
49}
50
Ady Abrahamdfb63ba2020-05-27 20:05:05 +000051bool LayerInfoV2::isFrameTimeValid(const FrameTimeData& frameTime) const {
52 return frameTime.queueTime >= std::chrono::duration_cast<std::chrono::nanoseconds>(
53 mFrameTimeValidSince.time_since_epoch())
54 .count();
55}
Ady Abraham1adbb722020-05-15 11:51:48 -070056
Ady Abrahamdfb63ba2020-05-27 20:05:05 +000057bool LayerInfoV2::isFrequent(nsecs_t now) const {
58 // Find the first valid frame time
59 auto it = mFrameTimes.begin();
60 for (; it != mFrameTimes.end(); ++it) {
61 if (isFrameTimeValid(*it)) {
62 break;
Ady Abrahamaf6d8a42020-05-27 19:56:15 +000063 }
64 }
Ady Abraham4ccdcb42020-02-11 17:34:34 -080065
Ady Abrahamdfb63ba2020-05-27 20:05:05 +000066 // If we know nothing about this layer we consider it as frequent as it might be the start
67 // of an animation.
68 if (std::distance(it, mFrameTimes.end()) < FREQUENT_LAYER_WINDOW_SIZE) {
69 return true;
70 }
71
72 // Find the first active frame
73 for (; it != mFrameTimes.end(); ++it) {
74 if (it->queueTime >= getActiveLayerThreshold(now)) {
75 break;
76 }
77 }
78
79 const auto numFrames = std::distance(it, mFrameTimes.end());
80 if (numFrames < FREQUENT_LAYER_WINDOW_SIZE) {
81 return false;
82 }
83
84 // Layer is considered frequent if the average frame rate is higher than the threshold
85 const auto totalTime = mFrameTimes.back().queueTime - it->queueTime;
86 return (1e9f * (numFrames - 1)) / totalTime >= MIN_FPS_FOR_FREQUENT_LAYER;
Ady Abraham8a82ba62020-01-17 12:43:17 -080087}
88
89bool LayerInfoV2::hasEnoughDataForHeuristic() const {
90 // The layer had to publish at least HISTORY_SIZE or HISTORY_TIME of updates
Ady Abrahama61edcb2020-01-30 18:32:03 -080091 if (mFrameTimes.size() < 2) {
92 return false;
93 }
94
Ady Abrahamdfb63ba2020-05-27 20:05:05 +000095 if (!isFrameTimeValid(mFrameTimes.front())) {
96 return false;
97 }
98
Ady Abraham8a82ba62020-01-17 12:43:17 -080099 if (mFrameTimes.size() < HISTORY_SIZE &&
100 mFrameTimes.back().queueTime - mFrameTimes.front().queueTime < HISTORY_TIME.count()) {
101 return false;
102 }
103
104 return true;
105}
106
Ady Abraham32efd542020-05-19 17:49:26 -0700107std::pair<nsecs_t, bool> LayerInfoV2::calculateAverageFrameTime() const {
Ady Abraham8a82ba62020-01-17 12:43:17 -0800108 nsecs_t totalPresentTimeDeltas = 0;
Ady Abrahamc9664832020-05-12 14:16:56 -0700109 nsecs_t totalQueueTimeDeltas = 0;
Ady Abraham32efd542020-05-19 17:49:26 -0700110 bool missingPresentTime = false;
111 int numFrames = 0;
Ady Abraham8a82ba62020-01-17 12:43:17 -0800112 for (auto it = mFrameTimes.begin(); it != mFrameTimes.end() - 1; ++it) {
Ady Abraham32efd542020-05-19 17:49:26 -0700113 // Ignore frames captured during a config change
114 if (it->pendingConfigChange || (it + 1)->pendingConfigChange) {
115 continue;
116 }
117
Ady Abrahamc9664832020-05-12 14:16:56 -0700118 totalQueueTimeDeltas +=
119 std::max(((it + 1)->queueTime - it->queueTime), mHighRefreshRatePeriod);
Ady Abraham32efd542020-05-19 17:49:26 -0700120 numFrames++;
Ady Abrahamc9664832020-05-12 14:16:56 -0700121
Ady Abraham8a82ba62020-01-17 12:43:17 -0800122 if (it->presetTime == 0 || (it + 1)->presetTime == 0) {
Ady Abrahamc9664832020-05-12 14:16:56 -0700123 missingPresentTime = true;
124 continue;
Ady Abraham8a82ba62020-01-17 12:43:17 -0800125 }
126
127 totalPresentTimeDeltas +=
128 std::max(((it + 1)->presetTime - it->presetTime), mHighRefreshRatePeriod);
129 }
Ady Abrahamc9664832020-05-12 14:16:56 -0700130
Ady Abrahamc9664832020-05-12 14:16:56 -0700131 // Calculate the average frame time based on presentation timestamps. If those
132 // doesn't exist, we look at the time the buffer was queued only. We can do that only if
133 // we calculated a refresh rate based on presentation timestamps in the past. The reason
134 // we look at the queue time is to handle cases where hwui attaches presentation timestamps
135 // when implementing render ahead for specific refresh rates. When hwui no longer provides
136 // presentation timestamps we look at the queue time to see if the current refresh rate still
137 // matches the content.
Ady Abraham32efd542020-05-19 17:49:26 -0700138 const auto averageFrameTime =
Ady Abrahamc9664832020-05-12 14:16:56 -0700139 static_cast<float>(missingPresentTime ? totalQueueTimeDeltas : totalPresentTimeDeltas) /
Ady Abraham32efd542020-05-19 17:49:26 -0700140 numFrames;
141 return {static_cast<nsecs_t>(averageFrameTime), missingPresentTime};
142}
Ady Abraham8a82ba62020-01-17 12:43:17 -0800143
Ady Abraham32efd542020-05-19 17:49:26 -0700144bool LayerInfoV2::isRefreshRateStable(nsecs_t averageFrameTime, bool missingPresentTime) const {
Ady Abraham8a82ba62020-01-17 12:43:17 -0800145 for (auto it = mFrameTimes.begin(); it != mFrameTimes.end() - 1; ++it) {
Ady Abraham32efd542020-05-19 17:49:26 -0700146 // Ignore frames captured during a config change
147 if (it->pendingConfigChange || (it + 1)->pendingConfigChange) {
148 continue;
149 }
Ady Abrahamc9664832020-05-12 14:16:56 -0700150 const auto presentTimeDeltas = [&] {
151 const auto delta = missingPresentTime ? (it + 1)->queueTime - it->queueTime
152 : (it + 1)->presetTime - it->presetTime;
153 return std::max(delta, mHighRefreshRatePeriod);
154 }();
155
Ady Abraham5f489bd2020-05-12 21:22:02 +0000156 if (std::abs(presentTimeDeltas - averageFrameTime) > 2 * averageFrameTime) {
Ady Abraham32efd542020-05-19 17:49:26 -0700157 return false;
Ady Abraham8a82ba62020-01-17 12:43:17 -0800158 }
159 }
160
Ady Abraham32efd542020-05-19 17:49:26 -0700161 return true;
162}
163
164std::optional<float> LayerInfoV2::calculateRefreshRateIfPossible() {
165 static constexpr float MARGIN = 1.0f; // 1Hz
166
167 if (!hasEnoughDataForHeuristic()) {
168 ALOGV("Not enough data");
169 return std::nullopt;
170 }
171
172 const auto [averageFrameTime, missingPresentTime] = calculateAverageFrameTime();
173
174 // If there are no presentation timestamps provided we can't calculate the refresh rate
175 if (missingPresentTime && mLastReportedRefreshRate == 0) {
176 return std::nullopt;
177 }
178
179 if (!isRefreshRateStable(averageFrameTime, missingPresentTime)) {
180 return std::nullopt;
181 }
182
Ady Abraham8a82ba62020-01-17 12:43:17 -0800183 const auto refreshRate = 1e9f / averageFrameTime;
184 if (std::abs(refreshRate - mLastReportedRefreshRate) > MARGIN) {
185 mLastReportedRefreshRate = refreshRate;
186 }
187
188 ALOGV("Refresh rate: %.2f", mLastReportedRefreshRate);
189 return mLastReportedRefreshRate;
190}
191
192std::pair<LayerHistory::LayerVoteType, float> LayerInfoV2::getRefreshRate(nsecs_t now) {
193 if (mLayerVote.type != LayerHistory::LayerVoteType::Heuristic) {
194 return {mLayerVote.type, mLayerVote.fps};
195 }
196
197 if (!isFrequent(now)) {
198 return {LayerHistory::LayerVoteType::Min, 0};
199 }
200
201 auto refreshRate = calculateRefreshRateIfPossible();
202 if (refreshRate.has_value()) {
203 return {LayerHistory::LayerVoteType::Heuristic, refreshRate.value()};
204 }
205
Ady Abraham8a82ba62020-01-17 12:43:17 -0800206 return {LayerHistory::LayerVoteType::Max, 0};
207}
208
209} // namespace android::scheduler