blob: 348e2b9c72b747ef09bfb47cb75d71d8eb419a7f [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>
Dominik Laskowskif5d0ea52021-09-26 17:27:01 -070031#include <ftl/enum.h>
Ady Abraham73c3df52023-01-12 18:09:31 -080032#include <gui/TraceUtils.h>
Ady Abraham0ccd79b2020-06-10 10:11:17 -070033
Ady Abraham8a82ba62020-01-17 12:43:17 -080034#undef LOG_TAG
Marin Shalamanov1bc43ee2020-11-20 16:56:52 +010035#define LOG_TAG "LayerInfo"
Ady Abraham8a82ba62020-01-17 12:43:17 -080036
37namespace android::scheduler {
38
Marin Shalamanov1bc43ee2020-11-20 16:56:52 +010039bool LayerInfo::sTraceEnabled = false;
Ady Abrahamb1b9d412020-06-01 19:53:52 -070040
Ady Abrahambdda8f02021-04-01 16:06:11 -070041LayerInfo::LayerInfo(const std::string& name, uid_t ownerUid,
42 LayerHistory::LayerVoteType defaultVote)
Ady Abrahama6b676e2020-05-27 14:29:09 -070043 : mName(name),
Ady Abrahambdda8f02021-04-01 16:06:11 -070044 mOwnerUid(ownerUid),
Ady Abraham8a82ba62020-01-17 12:43:17 -080045 mDefaultVote(defaultVote),
Dominik Laskowski6eab42d2021-09-13 14:34:13 -070046 mLayerVote({defaultVote, Fps()}),
Vishnu Nairef68d6d2023-02-28 06:18:27 +000047 mLayerProps(std::make_unique<LayerProps>()),
48 mRefreshRateHistory(name) {
49 ;
50}
Ady Abraham8a82ba62020-01-17 12:43:17 -080051
Marin Shalamanov1bc43ee2020-11-20 16:56:52 +010052void LayerInfo::setLastPresentTime(nsecs_t lastPresentTime, nsecs_t now, LayerUpdateType updateType,
Vishnu Nairef68d6d2023-02-28 06:18:27 +000053 bool pendingModeChange, const LayerProps& props) {
Ady Abraham8a82ba62020-01-17 12:43:17 -080054 lastPresentTime = std::max(lastPresentTime, static_cast<nsecs_t>(0));
55
56 mLastUpdatedTime = std::max(lastPresentTime, now);
Vishnu Nairef68d6d2023-02-28 06:18:27 +000057 *mLayerProps = props;
Ady Abraham5def7332020-05-29 16:13:47 -070058 switch (updateType) {
59 case LayerUpdateType::AnimationTX:
60 mLastAnimationTime = std::max(lastPresentTime, now);
61 break;
62 case LayerUpdateType::SetFrameRate:
63 case LayerUpdateType::Buffer:
Marin Shalamanov2045d5b2020-12-28 18:11:41 +010064 FrameTimeData frameTime = {.presentTime = lastPresentTime,
Ady Abraham5def7332020-05-29 16:13:47 -070065 .queueTime = mLastUpdatedTime,
Arthur Hung0cc8a382023-06-02 01:35:52 +000066 .pendingModeChange = pendingModeChange,
67 .isSmallDirty = props.isSmallDirty};
Ady Abraham5def7332020-05-29 16:13:47 -070068 mFrameTimes.push_back(frameTime);
69 if (mFrameTimes.size() > HISTORY_SIZE) {
70 mFrameTimes.pop_front();
71 }
72 break;
Ady Abraham8a82ba62020-01-17 12:43:17 -080073 }
74}
75
Marin Shalamanov1bc43ee2020-11-20 16:56:52 +010076bool LayerInfo::isFrameTimeValid(const FrameTimeData& frameTime) const {
Ady Abrahamdfb63ba2020-05-27 20:05:05 +000077 return frameTime.queueTime >= std::chrono::duration_cast<std::chrono::nanoseconds>(
78 mFrameTimeValidSince.time_since_epoch())
79 .count();
80}
Ady Abraham1adbb722020-05-15 11:51:48 -070081
ramindani63db24e2023-04-03 10:56:05 -070082LayerInfo::Frequent LayerInfo::isFrequent(nsecs_t now) const {
Ady Abraham86ac5c52023-01-11 15:24:03 -080083 // If we know nothing about this layer (e.g. after touch event),
84 // we consider it as frequent as it might be the start of an animation.
Marin Shalamanov2045d5b2020-12-28 18:11:41 +010085 if (mFrameTimes.size() < kFrequentLayerWindowSize) {
ramindani63db24e2023-04-03 10:56:05 -070086 return {/* isFrequent */ true, /* clearHistory */ false, /* isConclusive */ true};
Ady Abrahamdfb63ba2020-05-27 20:05:05 +000087 }
Ady Abraham86ac5c52023-01-11 15:24:03 -080088
89 // Non-active layers are also infrequent
90 if (mLastUpdatedTime < getActiveLayerThreshold(now)) {
ramindani63db24e2023-04-03 10:56:05 -070091 return {/* isFrequent */ false, /* clearHistory */ false, /* isConclusive */ true};
Ady Abraham86ac5c52023-01-11 15:24:03 -080092 }
93
94 // We check whether we can classify this layer as frequent or infrequent:
95 // - frequent: a layer posted kFrequentLayerWindowSize within
96 // kMaxPeriodForFrequentLayerNs of each other.
97 // - infrequent: a layer posted kFrequentLayerWindowSize with longer
98 // gaps than kFrequentLayerWindowSize.
99 // If we can't determine the layer classification yet, we return the last
100 // classification.
101 bool isFrequent = true;
102 bool isInfrequent = true;
Arthur Hung0cc8a382023-06-02 01:35:52 +0000103 int32_t smallDirtyCount = 0;
Ady Abraham86ac5c52023-01-11 15:24:03 -0800104 const auto n = mFrameTimes.size() - 1;
105 for (size_t i = 0; i < kFrequentLayerWindowSize - 1; i++) {
106 if (mFrameTimes[n - i].queueTime - mFrameTimes[n - i - 1].queueTime <
107 kMaxPeriodForFrequentLayerNs.count()) {
108 isInfrequent = false;
Arthur Hung0cc8a382023-06-02 01:35:52 +0000109 if (mFrameTimes[n - i].presentTime == 0 && mFrameTimes[n - i].isSmallDirty) {
110 smallDirtyCount++;
111 }
Ady Abraham86ac5c52023-01-11 15:24:03 -0800112 } else {
113 isFrequent = false;
114 }
115 }
116
117 if (isFrequent || isInfrequent) {
ramindani63db24e2023-04-03 10:56:05 -0700118 // If the layer was previously inconclusive, we clear
119 // the history as indeterminate layers changed to frequent,
120 // and we should not look at the stale data.
Arthur Hung0cc8a382023-06-02 01:35:52 +0000121 return {isFrequent, isFrequent && !mIsFrequencyConclusive, /* isConclusive */ true,
122 /* isSmallDirty */ smallDirtyCount >= kNumSmallDirtyThreshold};
Ady Abraham86ac5c52023-01-11 15:24:03 -0800123 }
124
125 // If we can't determine whether the layer is frequent or not, we return
ramindani63db24e2023-04-03 10:56:05 -0700126 // the last known classification and mark the layer frequency as inconclusive.
127 isFrequent = !mLastRefreshRate.infrequent;
128
129 // If the layer was previously tagged as animating, we clear
130 // the history as it is likely the layer just changed its behavior,
131 // and we should not look at stale data.
132 return {isFrequent, isFrequent && mLastRefreshRate.animating, /* isConclusive */ false};
Nathaniel Nifong1303d912021-10-06 09:41:24 -0400133}
Ady Abrahamdfb63ba2020-05-27 20:05:05 +0000134
Nathaniel Nifong1303d912021-10-06 09:41:24 -0400135Fps LayerInfo::getFps(nsecs_t now) const {
Ady Abrahamdfb63ba2020-05-27 20:05:05 +0000136 // Find the first active frame
Ady Abraham983e5682020-05-28 16:49:18 -0700137 auto it = mFrameTimes.begin();
Ady Abrahamdfb63ba2020-05-27 20:05:05 +0000138 for (; it != mFrameTimes.end(); ++it) {
139 if (it->queueTime >= getActiveLayerThreshold(now)) {
140 break;
141 }
142 }
143
144 const auto numFrames = std::distance(it, mFrameTimes.end());
Marin Shalamanov2045d5b2020-12-28 18:11:41 +0100145 if (numFrames < kFrequentLayerWindowSize) {
Nathaniel Nifong1303d912021-10-06 09:41:24 -0400146 return Fps();
Ady Abrahamdfb63ba2020-05-27 20:05:05 +0000147 }
148
149 // Layer is considered frequent if the average frame rate is higher than the threshold
150 const auto totalTime = mFrameTimes.back().queueTime - it->queueTime;
Nathaniel Nifong1303d912021-10-06 09:41:24 -0400151 return Fps::fromPeriodNsecs(totalTime / (numFrames - 1));
Ady Abraham8a82ba62020-01-17 12:43:17 -0800152}
153
Marin Shalamanov1bc43ee2020-11-20 16:56:52 +0100154bool LayerInfo::isAnimating(nsecs_t now) const {
Ady Abraham5def7332020-05-29 16:13:47 -0700155 return mLastAnimationTime >= getActiveLayerThreshold(now);
156}
157
Marin Shalamanov1bc43ee2020-11-20 16:56:52 +0100158bool LayerInfo::hasEnoughDataForHeuristic() const {
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700159 // The layer had to publish at least HISTORY_SIZE or HISTORY_DURATION of updates
Ady Abrahama61edcb2020-01-30 18:32:03 -0800160 if (mFrameTimes.size() < 2) {
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700161 ALOGV("fewer than 2 frames recorded: %zu", mFrameTimes.size());
Ady Abrahama61edcb2020-01-30 18:32:03 -0800162 return false;
163 }
164
Ady Abrahamdfb63ba2020-05-27 20:05:05 +0000165 if (!isFrameTimeValid(mFrameTimes.front())) {
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700166 ALOGV("stale frames still captured");
Ady Abrahamdfb63ba2020-05-27 20:05:05 +0000167 return false;
168 }
169
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700170 const auto totalDuration = mFrameTimes.back().queueTime - mFrameTimes.front().queueTime;
171 if (mFrameTimes.size() < HISTORY_SIZE && totalDuration < HISTORY_DURATION.count()) {
172 ALOGV("not enough frames captured: %zu | %.2f seconds", mFrameTimes.size(),
173 totalDuration / 1e9f);
Ady Abraham8a82ba62020-01-17 12:43:17 -0800174 return false;
175 }
176
177 return true;
178}
179
Marin Shalamanov1bc43ee2020-11-20 16:56:52 +0100180std::optional<nsecs_t> LayerInfo::calculateAverageFrameTime() const {
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100181 // Ignore frames captured during a mode change
182 const bool isDuringModeChange =
Marin Shalamanov2045d5b2020-12-28 18:11:41 +0100183 std::any_of(mFrameTimes.begin(), mFrameTimes.end(),
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100184 [](const auto& frame) { return frame.pendingModeChange; });
185 if (isDuringModeChange) {
Marin Shalamanov2045d5b2020-12-28 18:11:41 +0100186 return std::nullopt;
187 }
Ady Abraham32efd542020-05-19 17:49:26 -0700188
Marin Shalamanov2045d5b2020-12-28 18:11:41 +0100189 const bool isMissingPresentTime =
190 std::any_of(mFrameTimes.begin(), mFrameTimes.end(),
191 [](auto frame) { return frame.presentTime == 0; });
192 if (isMissingPresentTime && !mLastRefreshRate.reported.isValid()) {
193 // If there are no presentation timestamps and we haven't calculated
194 // one in the past then we can't calculate the refresh rate
195 return std::nullopt;
Ady Abraham8a82ba62020-01-17 12:43:17 -0800196 }
Ady Abrahamc9664832020-05-12 14:16:56 -0700197
Ady Abrahamc9664832020-05-12 14:16:56 -0700198 // Calculate the average frame time based on presentation timestamps. If those
199 // doesn't exist, we look at the time the buffer was queued only. We can do that only if
200 // we calculated a refresh rate based on presentation timestamps in the past. The reason
201 // we look at the queue time is to handle cases where hwui attaches presentation timestamps
202 // when implementing render ahead for specific refresh rates. When hwui no longer provides
203 // presentation timestamps we look at the queue time to see if the current refresh rate still
204 // matches the content.
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700205
Marin Shalamanov2045d5b2020-12-28 18:11:41 +0100206 auto getFrameTime = isMissingPresentTime ? [](FrameTimeData data) { return data.queueTime; }
207 : [](FrameTimeData data) { return data.presentTime; };
208
209 nsecs_t totalDeltas = 0;
210 int numDeltas = 0;
Arthur Hung0cc8a382023-06-02 01:35:52 +0000211 int32_t smallDirtyCount = 0;
Marin Shalamanov2045d5b2020-12-28 18:11:41 +0100212 auto prevFrame = mFrameTimes.begin();
213 for (auto it = mFrameTimes.begin() + 1; it != mFrameTimes.end(); ++it) {
214 const auto currDelta = getFrameTime(*it) - getFrameTime(*prevFrame);
215 if (currDelta < kMinPeriodBetweenFrames) {
216 // Skip this frame, but count the delta into the next frame
217 continue;
218 }
219
Arthur Hung0cc8a382023-06-02 01:35:52 +0000220 // If this is a small area update, we don't want to consider it for calculating the average
221 // frame time. Instead, we let the bigger frame updates to drive the calculation.
222 if (it->isSmallDirty && currDelta < kMinPeriodBetweenSmallDirtyFrames) {
223 smallDirtyCount++;
224 continue;
225 }
226
Marin Shalamanov2045d5b2020-12-28 18:11:41 +0100227 prevFrame = it;
228
229 if (currDelta > kMaxPeriodBetweenFrames) {
230 // Skip this frame and the current delta.
231 continue;
232 }
233
234 totalDeltas += currDelta;
235 numDeltas++;
236 }
237
Arthur Hung0cc8a382023-06-02 01:35:52 +0000238 if (smallDirtyCount > 0) {
239 ATRACE_FORMAT_INSTANT("small dirty = %" PRIu32, smallDirtyCount);
240 }
241
Marin Shalamanov2045d5b2020-12-28 18:11:41 +0100242 if (numDeltas == 0) {
243 return std::nullopt;
244 }
245
246 const auto averageFrameTime = static_cast<double>(totalDeltas) / static_cast<double>(numDeltas);
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700247 return static_cast<nsecs_t>(averageFrameTime);
Ady Abraham32efd542020-05-19 17:49:26 -0700248}
Ady Abraham8a82ba62020-01-17 12:43:17 -0800249
Dominik Laskowskid82e0f02022-10-26 15:23:04 -0400250std::optional<Fps> LayerInfo::calculateRefreshRateIfPossible(const RefreshRateSelector& selector,
251 nsecs_t now) {
Ady Abraham73c3df52023-01-12 18:09:31 -0800252 ATRACE_CALL();
Ady Abraham32efd542020-05-19 17:49:26 -0700253 static constexpr float MARGIN = 1.0f; // 1Hz
Ady Abraham32efd542020-05-19 17:49:26 -0700254 if (!hasEnoughDataForHeuristic()) {
255 ALOGV("Not enough data");
256 return std::nullopt;
257 }
258
Dominik Laskowski6eab42d2021-09-13 14:34:13 -0700259 if (const auto averageFrameTime = calculateAverageFrameTime()) {
Marin Shalamanove8a663d2020-11-24 17:48:00 +0100260 const auto refreshRate = Fps::fromPeriodNsecs(*averageFrameTime);
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700261 const bool refreshRateConsistent = mRefreshRateHistory.add(refreshRate, now);
262 if (refreshRateConsistent) {
Dominik Laskowskid82e0f02022-10-26 15:23:04 -0400263 const auto knownRefreshRate = selector.findClosestKnownFrameRate(refreshRate);
Dominik Laskowski6eab42d2021-09-13 14:34:13 -0700264 using fps_approx_ops::operator!=;
265
266 // To avoid oscillation, use the last calculated refresh rate if it is close enough.
Marin Shalamanove8a663d2020-11-24 17:48:00 +0100267 if (std::abs(mLastRefreshRate.calculated.getValue() - refreshRate.getValue()) >
268 MARGIN &&
Dominik Laskowski6eab42d2021-09-13 14:34:13 -0700269 mLastRefreshRate.reported != knownRefreshRate) {
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700270 mLastRefreshRate.calculated = refreshRate;
271 mLastRefreshRate.reported = knownRefreshRate;
272 }
273
Marin Shalamanove8a663d2020-11-24 17:48:00 +0100274 ALOGV("%s %s rounded to nearest known frame rate %s", mName.c_str(),
275 to_string(refreshRate).c_str(), to_string(mLastRefreshRate.reported).c_str());
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700276 } else {
Marin Shalamanove8a663d2020-11-24 17:48:00 +0100277 ALOGV("%s Not stable (%s) returning last known frame rate %s", mName.c_str(),
278 to_string(refreshRate).c_str(), to_string(mLastRefreshRate.reported).c_str());
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700279 }
Ady Abraham32efd542020-05-19 17:49:26 -0700280 }
281
Marin Shalamanove8a663d2020-11-24 17:48:00 +0100282 return mLastRefreshRate.reported.isValid() ? std::make_optional(mLastRefreshRate.reported)
283 : std::nullopt;
Ady Abraham8a82ba62020-01-17 12:43:17 -0800284}
285
Dominik Laskowskid82e0f02022-10-26 15:23:04 -0400286LayerInfo::LayerVote LayerInfo::getRefreshRateVote(const RefreshRateSelector& selector,
Ady Abraham3efa3942021-06-24 19:01:25 -0700287 nsecs_t now) {
Ady Abraham73c3df52023-01-12 18:09:31 -0800288 ATRACE_CALL();
Ady Abraham8a82ba62020-01-17 12:43:17 -0800289 if (mLayerVote.type != LayerHistory::LayerVoteType::Heuristic) {
Ady Abrahama6b676e2020-05-27 14:29:09 -0700290 ALOGV("%s voted %d ", mName.c_str(), static_cast<int>(mLayerVote.type));
Marin Shalamanov46084422020-10-13 12:33:42 +0200291 return mLayerVote;
Ady Abraham8a82ba62020-01-17 12:43:17 -0800292 }
293
Ady Abraham5def7332020-05-29 16:13:47 -0700294 if (isAnimating(now)) {
Ady Abraham73c3df52023-01-12 18:09:31 -0800295 ATRACE_FORMAT_INSTANT("animating");
Ady Abraham5def7332020-05-29 16:13:47 -0700296 ALOGV("%s is animating", mName.c_str());
Ady Abraham86ac5c52023-01-11 15:24:03 -0800297 mLastRefreshRate.animating = true;
Dominik Laskowski6eab42d2021-09-13 14:34:13 -0700298 return {LayerHistory::LayerVoteType::Max, Fps()};
Ady Abraham5def7332020-05-29 16:13:47 -0700299 }
300
ramindani63db24e2023-04-03 10:56:05 -0700301 const LayerInfo::Frequent frequent = isFrequent(now);
302 mIsFrequencyConclusive = frequent.isConclusive;
303 if (!frequent.isFrequent) {
Ady Abraham73c3df52023-01-12 18:09:31 -0800304 ATRACE_FORMAT_INSTANT("infrequent");
Ady Abrahama6b676e2020-05-27 14:29:09 -0700305 ALOGV("%s is infrequent", mName.c_str());
Ady Abraham86ac5c52023-01-11 15:24:03 -0800306 mLastRefreshRate.infrequent = true;
ramindani63db24e2023-04-03 10:56:05 -0700307 // Infrequent layers vote for minimal refresh rate for
Marin Shalamanov29e25402021-04-07 21:09:58 +0200308 // battery saving purposes and also to prevent b/135718869.
Dominik Laskowski6eab42d2021-09-13 14:34:13 -0700309 return {LayerHistory::LayerVoteType::Min, Fps()};
Ady Abraham8a82ba62020-01-17 12:43:17 -0800310 }
311
ramindani63db24e2023-04-03 10:56:05 -0700312 if (frequent.clearHistory) {
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700313 clearHistory(now);
314 }
315
Arthur Hung0cc8a382023-06-02 01:35:52 +0000316 // Return no vote if the latest frames are small dirty.
317 if (frequent.isSmallDirty && !mLastRefreshRate.reported.isValid()) {
318 ATRACE_FORMAT_INSTANT("NoVote (small dirty)");
319 ALOGV("%s is small dirty", mName.c_str());
320 return {LayerHistory::LayerVoteType::NoVote, Fps()};
321 }
322
Dominik Laskowskid82e0f02022-10-26 15:23:04 -0400323 auto refreshRate = calculateRefreshRateIfPossible(selector, now);
Ady Abraham8a82ba62020-01-17 12:43:17 -0800324 if (refreshRate.has_value()) {
Marin Shalamanove8a663d2020-11-24 17:48:00 +0100325 ALOGV("%s calculated refresh rate: %s", mName.c_str(), to_string(*refreshRate).c_str());
Ady Abraham8a82ba62020-01-17 12:43:17 -0800326 return {LayerHistory::LayerVoteType::Heuristic, refreshRate.value()};
327 }
328
Ady Abrahama6b676e2020-05-27 14:29:09 -0700329 ALOGV("%s Max (can't resolve refresh rate)", mName.c_str());
Dominik Laskowski6eab42d2021-09-13 14:34:13 -0700330 return {LayerHistory::LayerVoteType::Max, Fps()};
Ady Abraham8a82ba62020-01-17 12:43:17 -0800331}
332
Dominik Laskowskif5d0ea52021-09-26 17:27:01 -0700333const char* LayerInfo::getTraceTag(LayerHistory::LayerVoteType type) const {
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700334 if (mTraceTags.count(type) == 0) {
Dominik Laskowskif5d0ea52021-09-26 17:27:01 -0700335 auto tag = "LFPS " + mName + " " + ftl::enum_string(type);
336 mTraceTags.emplace(type, std::move(tag));
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700337 }
338
339 return mTraceTags.at(type).c_str();
340}
341
Vishnu Nairef68d6d2023-02-28 06:18:27 +0000342LayerInfo::FrameRate LayerInfo::getSetFrameRateVote() const {
343 return mLayerProps->setFrameRateVote;
344}
345
346bool LayerInfo::isVisible() const {
347 return mLayerProps->visible;
348}
349
350int32_t LayerInfo::getFrameRateSelectionPriority() const {
351 return mLayerProps->frameRateSelectionPriority;
352}
353
354FloatRect LayerInfo::getBounds() const {
355 return mLayerProps->bounds;
356}
357
358ui::Transform LayerInfo::getTransform() const {
359 return mLayerProps->transform;
360}
361
Marin Shalamanov1bc43ee2020-11-20 16:56:52 +0100362LayerInfo::RefreshRateHistory::HeuristicTraceTagData
363LayerInfo::RefreshRateHistory::makeHeuristicTraceTagData() const {
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700364 const std::string prefix = "LFPS ";
365 const std::string suffix = "Heuristic ";
366 return {.min = prefix + mName + suffix + "min",
367 .max = prefix + mName + suffix + "max",
368 .consistent = prefix + mName + suffix + "consistent",
369 .average = prefix + mName + suffix + "average"};
370}
371
Marin Shalamanov1bc43ee2020-11-20 16:56:52 +0100372void LayerInfo::RefreshRateHistory::clear() {
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700373 mRefreshRates.clear();
374}
375
Marin Shalamanove8a663d2020-11-24 17:48:00 +0100376bool LayerInfo::RefreshRateHistory::add(Fps refreshRate, nsecs_t now) {
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700377 mRefreshRates.push_back({refreshRate, now});
378 while (mRefreshRates.size() >= HISTORY_SIZE ||
379 now - mRefreshRates.front().timestamp > HISTORY_DURATION.count()) {
380 mRefreshRates.pop_front();
381 }
382
383 if (CC_UNLIKELY(sTraceEnabled)) {
384 if (!mHeuristicTraceTagData.has_value()) {
385 mHeuristicTraceTagData = makeHeuristicTraceTagData();
386 }
387
Marin Shalamanove8a663d2020-11-24 17:48:00 +0100388 ATRACE_INT(mHeuristicTraceTagData->average.c_str(), refreshRate.getIntValue());
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700389 }
390
391 return isConsistent();
392}
393
Marin Shalamanov1bc43ee2020-11-20 16:56:52 +0100394bool LayerInfo::RefreshRateHistory::isConsistent() const {
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700395 if (mRefreshRates.empty()) return true;
396
Dominik Laskowski6eab42d2021-09-13 14:34:13 -0700397 const auto [min, max] =
398 std::minmax_element(mRefreshRates.begin(), mRefreshRates.end(),
399 [](const auto& lhs, const auto& rhs) {
400 return isStrictlyLess(lhs.refreshRate, rhs.refreshRate);
401 });
402
403 const bool consistent =
Marin Shalamanove8a663d2020-11-24 17:48:00 +0100404 max->refreshRate.getValue() - min->refreshRate.getValue() < MARGIN_CONSISTENT_FPS;
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700405
406 if (CC_UNLIKELY(sTraceEnabled)) {
407 if (!mHeuristicTraceTagData.has_value()) {
408 mHeuristicTraceTagData = makeHeuristicTraceTagData();
409 }
410
Marin Shalamanove8a663d2020-11-24 17:48:00 +0100411 ATRACE_INT(mHeuristicTraceTagData->max.c_str(), max->refreshRate.getIntValue());
412 ATRACE_INT(mHeuristicTraceTagData->min.c_str(), min->refreshRate.getIntValue());
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700413 ATRACE_INT(mHeuristicTraceTagData->consistent.c_str(), consistent);
414 }
415
416 return consistent;
417}
418
Ady Abraham8a82ba62020-01-17 12:43:17 -0800419} // namespace android::scheduler
Marin Shalamanovbed7fd32020-12-21 20:02:20 +0100420
421// TODO(b/129481165): remove the #pragma below and fix conversion issues
Dominik Laskowski6eab42d2021-09-13 14:34:13 -0700422#pragma clang diagnostic pop // ignored "-Wextra"