blob: bae37395011d6c861a54e6b288605ece6b6e7067 [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,
Marin Shalamanova7fe3042021-01-29 21:02:08 +010066 .pendingModeChange = pendingModeChange};
Ady Abraham5def7332020-05-29 16:13:47 -070067 mFrameTimes.push_back(frameTime);
68 if (mFrameTimes.size() > HISTORY_SIZE) {
69 mFrameTimes.pop_front();
70 }
71 break;
Ady Abraham8a82ba62020-01-17 12:43:17 -080072 }
73}
74
Marin Shalamanov1bc43ee2020-11-20 16:56:52 +010075bool LayerInfo::isFrameTimeValid(const FrameTimeData& frameTime) const {
Ady Abrahamdfb63ba2020-05-27 20:05:05 +000076 return frameTime.queueTime >= std::chrono::duration_cast<std::chrono::nanoseconds>(
77 mFrameTimeValidSince.time_since_epoch())
78 .count();
79}
Ady Abraham1adbb722020-05-15 11:51:48 -070080
ramindani63db24e2023-04-03 10:56:05 -070081LayerInfo::Frequent LayerInfo::isFrequent(nsecs_t now) const {
Ady Abraham86ac5c52023-01-11 15:24:03 -080082 // If we know nothing about this layer (e.g. after touch event),
83 // we consider it as frequent as it might be the start of an animation.
Marin Shalamanov2045d5b2020-12-28 18:11:41 +010084 if (mFrameTimes.size() < kFrequentLayerWindowSize) {
ramindani63db24e2023-04-03 10:56:05 -070085 return {/* isFrequent */ true, /* clearHistory */ false, /* isConclusive */ true};
Ady Abrahamdfb63ba2020-05-27 20:05:05 +000086 }
Ady Abraham86ac5c52023-01-11 15:24:03 -080087
88 // Non-active layers are also infrequent
89 if (mLastUpdatedTime < getActiveLayerThreshold(now)) {
ramindani63db24e2023-04-03 10:56:05 -070090 return {/* isFrequent */ false, /* clearHistory */ false, /* isConclusive */ true};
Ady Abraham86ac5c52023-01-11 15:24:03 -080091 }
92
93 // We check whether we can classify this layer as frequent or infrequent:
94 // - frequent: a layer posted kFrequentLayerWindowSize within
95 // kMaxPeriodForFrequentLayerNs of each other.
96 // - infrequent: a layer posted kFrequentLayerWindowSize with longer
97 // gaps than kFrequentLayerWindowSize.
98 // If we can't determine the layer classification yet, we return the last
99 // classification.
100 bool isFrequent = true;
101 bool isInfrequent = true;
102 const auto n = mFrameTimes.size() - 1;
103 for (size_t i = 0; i < kFrequentLayerWindowSize - 1; i++) {
104 if (mFrameTimes[n - i].queueTime - mFrameTimes[n - i - 1].queueTime <
105 kMaxPeriodForFrequentLayerNs.count()) {
106 isInfrequent = false;
107 } else {
108 isFrequent = false;
109 }
110 }
111
112 if (isFrequent || isInfrequent) {
ramindani63db24e2023-04-03 10:56:05 -0700113 // If the layer was previously inconclusive, we clear
114 // the history as indeterminate layers changed to frequent,
115 // and we should not look at the stale data.
116 return {isFrequent, isFrequent && !mIsFrequencyConclusive, /* isConclusive */ true};
Ady Abraham86ac5c52023-01-11 15:24:03 -0800117 }
118
119 // If we can't determine whether the layer is frequent or not, we return
ramindani63db24e2023-04-03 10:56:05 -0700120 // the last known classification and mark the layer frequency as inconclusive.
121 isFrequent = !mLastRefreshRate.infrequent;
122
123 // If the layer was previously tagged as animating, we clear
124 // the history as it is likely the layer just changed its behavior,
125 // and we should not look at stale data.
126 return {isFrequent, isFrequent && mLastRefreshRate.animating, /* isConclusive */ false};
Nathaniel Nifong1303d912021-10-06 09:41:24 -0400127}
Ady Abrahamdfb63ba2020-05-27 20:05:05 +0000128
Nathaniel Nifong1303d912021-10-06 09:41:24 -0400129Fps LayerInfo::getFps(nsecs_t now) const {
Ady Abrahamdfb63ba2020-05-27 20:05:05 +0000130 // Find the first active frame
Ady Abraham983e5682020-05-28 16:49:18 -0700131 auto it = mFrameTimes.begin();
Ady Abrahamdfb63ba2020-05-27 20:05:05 +0000132 for (; it != mFrameTimes.end(); ++it) {
133 if (it->queueTime >= getActiveLayerThreshold(now)) {
134 break;
135 }
136 }
137
138 const auto numFrames = std::distance(it, mFrameTimes.end());
Marin Shalamanov2045d5b2020-12-28 18:11:41 +0100139 if (numFrames < kFrequentLayerWindowSize) {
Nathaniel Nifong1303d912021-10-06 09:41:24 -0400140 return Fps();
Ady Abrahamdfb63ba2020-05-27 20:05:05 +0000141 }
142
143 // Layer is considered frequent if the average frame rate is higher than the threshold
144 const auto totalTime = mFrameTimes.back().queueTime - it->queueTime;
Nathaniel Nifong1303d912021-10-06 09:41:24 -0400145 return Fps::fromPeriodNsecs(totalTime / (numFrames - 1));
Ady Abraham8a82ba62020-01-17 12:43:17 -0800146}
147
Marin Shalamanov1bc43ee2020-11-20 16:56:52 +0100148bool LayerInfo::isAnimating(nsecs_t now) const {
Ady Abraham5def7332020-05-29 16:13:47 -0700149 return mLastAnimationTime >= getActiveLayerThreshold(now);
150}
151
Marin Shalamanov1bc43ee2020-11-20 16:56:52 +0100152bool LayerInfo::hasEnoughDataForHeuristic() const {
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700153 // The layer had to publish at least HISTORY_SIZE or HISTORY_DURATION of updates
Ady Abrahama61edcb2020-01-30 18:32:03 -0800154 if (mFrameTimes.size() < 2) {
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700155 ALOGV("fewer than 2 frames recorded: %zu", mFrameTimes.size());
Ady Abrahama61edcb2020-01-30 18:32:03 -0800156 return false;
157 }
158
Ady Abrahamdfb63ba2020-05-27 20:05:05 +0000159 if (!isFrameTimeValid(mFrameTimes.front())) {
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700160 ALOGV("stale frames still captured");
Ady Abrahamdfb63ba2020-05-27 20:05:05 +0000161 return false;
162 }
163
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700164 const auto totalDuration = mFrameTimes.back().queueTime - mFrameTimes.front().queueTime;
165 if (mFrameTimes.size() < HISTORY_SIZE && totalDuration < HISTORY_DURATION.count()) {
166 ALOGV("not enough frames captured: %zu | %.2f seconds", mFrameTimes.size(),
167 totalDuration / 1e9f);
Ady Abraham8a82ba62020-01-17 12:43:17 -0800168 return false;
169 }
170
171 return true;
172}
173
Marin Shalamanov1bc43ee2020-11-20 16:56:52 +0100174std::optional<nsecs_t> LayerInfo::calculateAverageFrameTime() const {
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100175 // Ignore frames captured during a mode change
176 const bool isDuringModeChange =
Marin Shalamanov2045d5b2020-12-28 18:11:41 +0100177 std::any_of(mFrameTimes.begin(), mFrameTimes.end(),
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100178 [](const auto& frame) { return frame.pendingModeChange; });
179 if (isDuringModeChange) {
Marin Shalamanov2045d5b2020-12-28 18:11:41 +0100180 return std::nullopt;
181 }
Ady Abraham32efd542020-05-19 17:49:26 -0700182
Marin Shalamanov2045d5b2020-12-28 18:11:41 +0100183 const bool isMissingPresentTime =
184 std::any_of(mFrameTimes.begin(), mFrameTimes.end(),
185 [](auto frame) { return frame.presentTime == 0; });
186 if (isMissingPresentTime && !mLastRefreshRate.reported.isValid()) {
187 // If there are no presentation timestamps and we haven't calculated
188 // one in the past then we can't calculate the refresh rate
189 return std::nullopt;
Ady Abraham8a82ba62020-01-17 12:43:17 -0800190 }
Ady Abrahamc9664832020-05-12 14:16:56 -0700191
Ady Abrahamc9664832020-05-12 14:16:56 -0700192 // Calculate the average frame time based on presentation timestamps. If those
193 // doesn't exist, we look at the time the buffer was queued only. We can do that only if
194 // we calculated a refresh rate based on presentation timestamps in the past. The reason
195 // we look at the queue time is to handle cases where hwui attaches presentation timestamps
196 // when implementing render ahead for specific refresh rates. When hwui no longer provides
197 // presentation timestamps we look at the queue time to see if the current refresh rate still
198 // matches the content.
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700199
Marin Shalamanov2045d5b2020-12-28 18:11:41 +0100200 auto getFrameTime = isMissingPresentTime ? [](FrameTimeData data) { return data.queueTime; }
201 : [](FrameTimeData data) { return data.presentTime; };
202
203 nsecs_t totalDeltas = 0;
204 int numDeltas = 0;
205 auto prevFrame = mFrameTimes.begin();
206 for (auto it = mFrameTimes.begin() + 1; it != mFrameTimes.end(); ++it) {
207 const auto currDelta = getFrameTime(*it) - getFrameTime(*prevFrame);
208 if (currDelta < kMinPeriodBetweenFrames) {
209 // Skip this frame, but count the delta into the next frame
210 continue;
211 }
212
213 prevFrame = it;
214
215 if (currDelta > kMaxPeriodBetweenFrames) {
216 // Skip this frame and the current delta.
217 continue;
218 }
219
220 totalDeltas += currDelta;
221 numDeltas++;
222 }
223
224 if (numDeltas == 0) {
225 return std::nullopt;
226 }
227
228 const auto averageFrameTime = static_cast<double>(totalDeltas) / static_cast<double>(numDeltas);
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700229 return static_cast<nsecs_t>(averageFrameTime);
Ady Abraham32efd542020-05-19 17:49:26 -0700230}
Ady Abraham8a82ba62020-01-17 12:43:17 -0800231
Dominik Laskowskid82e0f02022-10-26 15:23:04 -0400232std::optional<Fps> LayerInfo::calculateRefreshRateIfPossible(const RefreshRateSelector& selector,
233 nsecs_t now) {
Ady Abraham73c3df52023-01-12 18:09:31 -0800234 ATRACE_CALL();
Ady Abraham32efd542020-05-19 17:49:26 -0700235 static constexpr float MARGIN = 1.0f; // 1Hz
Ady Abraham32efd542020-05-19 17:49:26 -0700236 if (!hasEnoughDataForHeuristic()) {
237 ALOGV("Not enough data");
238 return std::nullopt;
239 }
240
Dominik Laskowski6eab42d2021-09-13 14:34:13 -0700241 if (const auto averageFrameTime = calculateAverageFrameTime()) {
Marin Shalamanove8a663d2020-11-24 17:48:00 +0100242 const auto refreshRate = Fps::fromPeriodNsecs(*averageFrameTime);
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700243 const bool refreshRateConsistent = mRefreshRateHistory.add(refreshRate, now);
244 if (refreshRateConsistent) {
Dominik Laskowskid82e0f02022-10-26 15:23:04 -0400245 const auto knownRefreshRate = selector.findClosestKnownFrameRate(refreshRate);
Dominik Laskowski6eab42d2021-09-13 14:34:13 -0700246 using fps_approx_ops::operator!=;
247
248 // To avoid oscillation, use the last calculated refresh rate if it is close enough.
Marin Shalamanove8a663d2020-11-24 17:48:00 +0100249 if (std::abs(mLastRefreshRate.calculated.getValue() - refreshRate.getValue()) >
250 MARGIN &&
Dominik Laskowski6eab42d2021-09-13 14:34:13 -0700251 mLastRefreshRate.reported != knownRefreshRate) {
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700252 mLastRefreshRate.calculated = refreshRate;
253 mLastRefreshRate.reported = knownRefreshRate;
254 }
255
Marin Shalamanove8a663d2020-11-24 17:48:00 +0100256 ALOGV("%s %s rounded to nearest known frame rate %s", mName.c_str(),
257 to_string(refreshRate).c_str(), to_string(mLastRefreshRate.reported).c_str());
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700258 } else {
Marin Shalamanove8a663d2020-11-24 17:48:00 +0100259 ALOGV("%s Not stable (%s) returning last known frame rate %s", mName.c_str(),
260 to_string(refreshRate).c_str(), to_string(mLastRefreshRate.reported).c_str());
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700261 }
Ady Abraham32efd542020-05-19 17:49:26 -0700262 }
263
Marin Shalamanove8a663d2020-11-24 17:48:00 +0100264 return mLastRefreshRate.reported.isValid() ? std::make_optional(mLastRefreshRate.reported)
265 : std::nullopt;
Ady Abraham8a82ba62020-01-17 12:43:17 -0800266}
267
Dominik Laskowskid82e0f02022-10-26 15:23:04 -0400268LayerInfo::LayerVote LayerInfo::getRefreshRateVote(const RefreshRateSelector& selector,
Ady Abraham3efa3942021-06-24 19:01:25 -0700269 nsecs_t now) {
Ady Abraham73c3df52023-01-12 18:09:31 -0800270 ATRACE_CALL();
Ady Abraham8a82ba62020-01-17 12:43:17 -0800271 if (mLayerVote.type != LayerHistory::LayerVoteType::Heuristic) {
Ady Abrahama6b676e2020-05-27 14:29:09 -0700272 ALOGV("%s voted %d ", mName.c_str(), static_cast<int>(mLayerVote.type));
Marin Shalamanov46084422020-10-13 12:33:42 +0200273 return mLayerVote;
Ady Abraham8a82ba62020-01-17 12:43:17 -0800274 }
275
Ady Abraham5def7332020-05-29 16:13:47 -0700276 if (isAnimating(now)) {
Ady Abraham73c3df52023-01-12 18:09:31 -0800277 ATRACE_FORMAT_INSTANT("animating");
Ady Abraham5def7332020-05-29 16:13:47 -0700278 ALOGV("%s is animating", mName.c_str());
Ady Abraham86ac5c52023-01-11 15:24:03 -0800279 mLastRefreshRate.animating = true;
Dominik Laskowski6eab42d2021-09-13 14:34:13 -0700280 return {LayerHistory::LayerVoteType::Max, Fps()};
Ady Abraham5def7332020-05-29 16:13:47 -0700281 }
282
ramindani63db24e2023-04-03 10:56:05 -0700283 const LayerInfo::Frequent frequent = isFrequent(now);
284 mIsFrequencyConclusive = frequent.isConclusive;
285 if (!frequent.isFrequent) {
Ady Abraham73c3df52023-01-12 18:09:31 -0800286 ATRACE_FORMAT_INSTANT("infrequent");
Ady Abrahama6b676e2020-05-27 14:29:09 -0700287 ALOGV("%s is infrequent", mName.c_str());
Ady Abraham86ac5c52023-01-11 15:24:03 -0800288 mLastRefreshRate.infrequent = true;
ramindani63db24e2023-04-03 10:56:05 -0700289 // Infrequent layers vote for minimal refresh rate for
Marin Shalamanov29e25402021-04-07 21:09:58 +0200290 // battery saving purposes and also to prevent b/135718869.
Dominik Laskowski6eab42d2021-09-13 14:34:13 -0700291 return {LayerHistory::LayerVoteType::Min, Fps()};
Ady Abraham8a82ba62020-01-17 12:43:17 -0800292 }
293
ramindani63db24e2023-04-03 10:56:05 -0700294 if (frequent.clearHistory) {
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700295 clearHistory(now);
296 }
297
Dominik Laskowskid82e0f02022-10-26 15:23:04 -0400298 auto refreshRate = calculateRefreshRateIfPossible(selector, now);
Ady Abraham8a82ba62020-01-17 12:43:17 -0800299 if (refreshRate.has_value()) {
Marin Shalamanove8a663d2020-11-24 17:48:00 +0100300 ALOGV("%s calculated refresh rate: %s", mName.c_str(), to_string(*refreshRate).c_str());
Ady Abraham8a82ba62020-01-17 12:43:17 -0800301 return {LayerHistory::LayerVoteType::Heuristic, refreshRate.value()};
302 }
303
Ady Abrahama6b676e2020-05-27 14:29:09 -0700304 ALOGV("%s Max (can't resolve refresh rate)", mName.c_str());
Dominik Laskowski6eab42d2021-09-13 14:34:13 -0700305 return {LayerHistory::LayerVoteType::Max, Fps()};
Ady Abraham8a82ba62020-01-17 12:43:17 -0800306}
307
Dominik Laskowskif5d0ea52021-09-26 17:27:01 -0700308const char* LayerInfo::getTraceTag(LayerHistory::LayerVoteType type) const {
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700309 if (mTraceTags.count(type) == 0) {
Dominik Laskowskif5d0ea52021-09-26 17:27:01 -0700310 auto tag = "LFPS " + mName + " " + ftl::enum_string(type);
311 mTraceTags.emplace(type, std::move(tag));
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700312 }
313
314 return mTraceTags.at(type).c_str();
315}
316
Vishnu Nairef68d6d2023-02-28 06:18:27 +0000317LayerInfo::FrameRate LayerInfo::getSetFrameRateVote() const {
318 return mLayerProps->setFrameRateVote;
319}
320
321bool LayerInfo::isVisible() const {
322 return mLayerProps->visible;
323}
324
325int32_t LayerInfo::getFrameRateSelectionPriority() const {
326 return mLayerProps->frameRateSelectionPriority;
327}
328
329FloatRect LayerInfo::getBounds() const {
330 return mLayerProps->bounds;
331}
332
333ui::Transform LayerInfo::getTransform() const {
334 return mLayerProps->transform;
335}
336
Marin Shalamanov1bc43ee2020-11-20 16:56:52 +0100337LayerInfo::RefreshRateHistory::HeuristicTraceTagData
338LayerInfo::RefreshRateHistory::makeHeuristicTraceTagData() const {
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700339 const std::string prefix = "LFPS ";
340 const std::string suffix = "Heuristic ";
341 return {.min = prefix + mName + suffix + "min",
342 .max = prefix + mName + suffix + "max",
343 .consistent = prefix + mName + suffix + "consistent",
344 .average = prefix + mName + suffix + "average"};
345}
346
Marin Shalamanov1bc43ee2020-11-20 16:56:52 +0100347void LayerInfo::RefreshRateHistory::clear() {
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700348 mRefreshRates.clear();
349}
350
Marin Shalamanove8a663d2020-11-24 17:48:00 +0100351bool LayerInfo::RefreshRateHistory::add(Fps refreshRate, nsecs_t now) {
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700352 mRefreshRates.push_back({refreshRate, now});
353 while (mRefreshRates.size() >= HISTORY_SIZE ||
354 now - mRefreshRates.front().timestamp > HISTORY_DURATION.count()) {
355 mRefreshRates.pop_front();
356 }
357
358 if (CC_UNLIKELY(sTraceEnabled)) {
359 if (!mHeuristicTraceTagData.has_value()) {
360 mHeuristicTraceTagData = makeHeuristicTraceTagData();
361 }
362
Marin Shalamanove8a663d2020-11-24 17:48:00 +0100363 ATRACE_INT(mHeuristicTraceTagData->average.c_str(), refreshRate.getIntValue());
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700364 }
365
366 return isConsistent();
367}
368
Marin Shalamanov1bc43ee2020-11-20 16:56:52 +0100369bool LayerInfo::RefreshRateHistory::isConsistent() const {
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700370 if (mRefreshRates.empty()) return true;
371
Dominik Laskowski6eab42d2021-09-13 14:34:13 -0700372 const auto [min, max] =
373 std::minmax_element(mRefreshRates.begin(), mRefreshRates.end(),
374 [](const auto& lhs, const auto& rhs) {
375 return isStrictlyLess(lhs.refreshRate, rhs.refreshRate);
376 });
377
378 const bool consistent =
Marin Shalamanove8a663d2020-11-24 17:48:00 +0100379 max->refreshRate.getValue() - min->refreshRate.getValue() < MARGIN_CONSISTENT_FPS;
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700380
381 if (CC_UNLIKELY(sTraceEnabled)) {
382 if (!mHeuristicTraceTagData.has_value()) {
383 mHeuristicTraceTagData = makeHeuristicTraceTagData();
384 }
385
Marin Shalamanove8a663d2020-11-24 17:48:00 +0100386 ATRACE_INT(mHeuristicTraceTagData->max.c_str(), max->refreshRate.getIntValue());
387 ATRACE_INT(mHeuristicTraceTagData->min.c_str(), min->refreshRate.getIntValue());
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700388 ATRACE_INT(mHeuristicTraceTagData->consistent.c_str(), consistent);
389 }
390
391 return consistent;
392}
393
Ady Abraham8a82ba62020-01-17 12:43:17 -0800394} // namespace android::scheduler
Marin Shalamanovbed7fd32020-12-21 20:02:20 +0100395
396// TODO(b/129481165): remove the #pragma below and fix conversion issues
Dominik Laskowski6eab42d2021-09-13 14:34:13 -0700397#pragma clang diagnostic pop // ignored "-Wextra"