blob: 255eac6efc0a4409209dc58c69d6c110c6ff8746 [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
37void LayerInfoV2::setLastPresentTime(nsecs_t lastPresentTime, nsecs_t now) {
38 lastPresentTime = std::max(lastPresentTime, static_cast<nsecs_t>(0));
39
40 mLastUpdatedTime = std::max(lastPresentTime, now);
41
42 FrameTimeData frameTime = {.presetTime = lastPresentTime, .queueTime = mLastUpdatedTime};
43
44 mFrameTimes.push_back(frameTime);
45 if (mFrameTimes.size() > HISTORY_SIZE) {
46 mFrameTimes.pop_front();
47 }
48}
49
Ady Abraham8a82ba62020-01-17 12:43:17 -080050bool LayerInfoV2::isFrequent(nsecs_t now) const {
Ady Abraham1adbb722020-05-15 11:51:48 -070051 for (auto it = mFrameTimes.crbegin(); it != mFrameTimes.crend(); ++it) {
52 if (now - it->queueTime >= MAX_FREQUENT_LAYER_PERIOD_NS.count()) {
53 ALOGV("%s infrequent (last frame is %.2fms ago", mName.c_str(),
54 (now - mFrameTimes.back().queueTime) / 1e6f);
55 return false;
56 }
57
58 const auto numFrames = std::distance(mFrameTimes.crbegin(), it + 1);
59 if (numFrames >= FREQUENT_LAYER_WINDOW_SIZE) {
60 ALOGV("%s frequent (burst of %zu frames", mName.c_str(), numFrames);
61 return true;
Ady Abraham4ccdcb42020-02-11 17:34:34 -080062 }
63 }
64
Ady Abraham1adbb722020-05-15 11:51:48 -070065 ALOGV("%s infrequent (not enough frames %zu)", mName.c_str(), mFrameTimes.size());
66 return false;
Ady Abraham8a82ba62020-01-17 12:43:17 -080067}
68
69bool LayerInfoV2::hasEnoughDataForHeuristic() const {
70 // The layer had to publish at least HISTORY_SIZE or HISTORY_TIME of updates
Ady Abrahama61edcb2020-01-30 18:32:03 -080071 if (mFrameTimes.size() < 2) {
72 return false;
73 }
74
Ady Abraham8a82ba62020-01-17 12:43:17 -080075 if (mFrameTimes.size() < HISTORY_SIZE &&
76 mFrameTimes.back().queueTime - mFrameTimes.front().queueTime < HISTORY_TIME.count()) {
77 return false;
78 }
79
80 return true;
81}
82
83std::optional<float> LayerInfoV2::calculateRefreshRateIfPossible() {
84 static constexpr float MARGIN = 1.0f; // 1Hz
85
86 if (!hasEnoughDataForHeuristic()) {
87 ALOGV("Not enough data");
88 return std::nullopt;
89 }
90
91 // Calculate the refresh rate by finding the average delta between frames
92 nsecs_t totalPresentTimeDeltas = 0;
Ady Abrahamc9664832020-05-12 14:16:56 -070093 nsecs_t totalQueueTimeDeltas = 0;
94 auto missingPresentTime = false;
Ady Abraham8a82ba62020-01-17 12:43:17 -080095 for (auto it = mFrameTimes.begin(); it != mFrameTimes.end() - 1; ++it) {
Ady Abrahamc9664832020-05-12 14:16:56 -070096 totalQueueTimeDeltas +=
97 std::max(((it + 1)->queueTime - it->queueTime), mHighRefreshRatePeriod);
98
Ady Abraham8a82ba62020-01-17 12:43:17 -080099 if (it->presetTime == 0 || (it + 1)->presetTime == 0) {
Ady Abrahamc9664832020-05-12 14:16:56 -0700100 missingPresentTime = true;
101 continue;
Ady Abraham8a82ba62020-01-17 12:43:17 -0800102 }
103
104 totalPresentTimeDeltas +=
105 std::max(((it + 1)->presetTime - it->presetTime), mHighRefreshRatePeriod);
106 }
Ady Abrahamc9664832020-05-12 14:16:56 -0700107
108 // If there are no presentation timestamps provided we can't calculate the refresh rate
109 if (missingPresentTime && mLastReportedRefreshRate == 0) {
110 return std::nullopt;
111 }
112
113 // Calculate the average frame time based on presentation timestamps. If those
114 // doesn't exist, we look at the time the buffer was queued only. We can do that only if
115 // we calculated a refresh rate based on presentation timestamps in the past. The reason
116 // we look at the queue time is to handle cases where hwui attaches presentation timestamps
117 // when implementing render ahead for specific refresh rates. When hwui no longer provides
118 // presentation timestamps we look at the queue time to see if the current refresh rate still
119 // matches the content.
Ady Abrahamb28c5cc2020-05-12 21:22:32 +0000120 const float averageFrameTime =
Ady Abrahamc9664832020-05-12 14:16:56 -0700121 static_cast<float>(missingPresentTime ? totalQueueTimeDeltas : totalPresentTimeDeltas) /
122 (mFrameTimes.size() - 1);
Ady Abraham8a82ba62020-01-17 12:43:17 -0800123
124 // Now once we calculated the refresh rate we need to make sure that all the frames we captured
Ady Abrahamf6b77072020-01-30 14:22:54 -0800125 // are evenly distributed and we don't calculate the average across some burst of frames.
Ady Abraham8a82ba62020-01-17 12:43:17 -0800126 for (auto it = mFrameTimes.begin(); it != mFrameTimes.end() - 1; ++it) {
Ady Abrahamc9664832020-05-12 14:16:56 -0700127 const auto presentTimeDeltas = [&] {
128 const auto delta = missingPresentTime ? (it + 1)->queueTime - it->queueTime
129 : (it + 1)->presetTime - it->presetTime;
130 return std::max(delta, mHighRefreshRatePeriod);
131 }();
132
Ady Abraham5f489bd2020-05-12 21:22:02 +0000133 if (std::abs(presentTimeDeltas - averageFrameTime) > 2 * averageFrameTime) {
Ady Abraham8a82ba62020-01-17 12:43:17 -0800134 return std::nullopt;
135 }
136 }
137
138 const auto refreshRate = 1e9f / averageFrameTime;
139 if (std::abs(refreshRate - mLastReportedRefreshRate) > MARGIN) {
140 mLastReportedRefreshRate = refreshRate;
141 }
142
143 ALOGV("Refresh rate: %.2f", mLastReportedRefreshRate);
144 return mLastReportedRefreshRate;
145}
146
147std::pair<LayerHistory::LayerVoteType, float> LayerInfoV2::getRefreshRate(nsecs_t now) {
148 if (mLayerVote.type != LayerHistory::LayerVoteType::Heuristic) {
Ady Abraham1adbb722020-05-15 11:51:48 -0700149 ALOGV("%s voted %d ", mName.c_str(), static_cast<int>(mLayerVote.type));
Ady Abraham8a82ba62020-01-17 12:43:17 -0800150 return {mLayerVote.type, mLayerVote.fps};
151 }
152
153 if (!isFrequent(now)) {
Ady Abraham1adbb722020-05-15 11:51:48 -0700154 ALOGV("%s is infrequent", mName.c_str());
Ady Abraham8a82ba62020-01-17 12:43:17 -0800155 return {LayerHistory::LayerVoteType::Min, 0};
156 }
157
158 auto refreshRate = calculateRefreshRateIfPossible();
159 if (refreshRate.has_value()) {
Ady Abraham1adbb722020-05-15 11:51:48 -0700160 ALOGV("%s calculated refresh rate: %.2f", mName.c_str(), refreshRate.value());
Ady Abraham8a82ba62020-01-17 12:43:17 -0800161 return {LayerHistory::LayerVoteType::Heuristic, refreshRate.value()};
162 }
163
Ady Abraham1adbb722020-05-15 11:51:48 -0700164 ALOGV("%s Max (can't resolve refresh rate", mName.c_str());
Ady Abraham8a82ba62020-01-17 12:43:17 -0800165 return {LayerHistory::LayerVoteType::Max, 0};
166}
167
168} // namespace android::scheduler