blob: ba03c89a1d55bb8e28edc8acd30bdc25c0d77b6c [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#pragma once
18
Ady Abrahambdda8f02021-04-01 16:06:11 -070019#include <ui/Transform.h>
Ady Abraham8a82ba62020-01-17 12:43:17 -080020#include <utils/Timers.h>
21
22#include <chrono>
23#include <deque>
24
25#include "LayerHistory.h"
26#include "RefreshRateConfigs.h"
Marin Shalamanov53fc11d2020-11-20 14:00:13 +010027#include "Scheduler/Seamlessness.h"
Ady Abraham8a82ba62020-01-17 12:43:17 -080028#include "SchedulerUtils.h"
29
30namespace android {
31
32class Layer;
33
34namespace scheduler {
35
36using namespace std::chrono_literals;
37
38// Maximum period between presents for a layer to be considered active.
39constexpr std::chrono::nanoseconds MAX_ACTIVE_LAYER_PERIOD_NS = 1200ms;
40
41// Earliest present time for a layer to be considered active.
42constexpr nsecs_t getActiveLayerThreshold(nsecs_t now) {
43 return now - MAX_ACTIVE_LAYER_PERIOD_NS.count();
44}
45
46// Stores history of present times and refresh rates for a layer.
Marin Shalamanov1bc43ee2020-11-20 16:56:52 +010047class LayerInfo {
Ady Abraham5def7332020-05-29 16:13:47 -070048 using LayerUpdateType = LayerHistory::LayerUpdateType;
49
Ady Abraham8a82ba62020-01-17 12:43:17 -080050 // Layer is considered frequent if the earliest value in the window of most recent present times
51 // is within a threshold. If a layer is infrequent, its average refresh rate is disregarded in
52 // favor of a low refresh rate.
Marin Shalamanov2045d5b2020-12-28 18:11:41 +010053 static constexpr size_t kFrequentLayerWindowSize = 3;
54 static constexpr Fps kMinFpsForFrequentLayer{10.0f};
55 static constexpr auto kMaxPeriodForFrequentLayerNs =
56 std::chrono::nanoseconds(kMinFpsForFrequentLayer.getPeriodNsecs()) + 1ms;
Ady Abraham8a82ba62020-01-17 12:43:17 -080057
Marin Shalamanov1bc43ee2020-11-20 16:56:52 +010058 friend class LayerHistoryTest;
Marin Shalamanov2045d5b2020-12-28 18:11:41 +010059 friend class LayerInfoTest;
Ady Abraham8a82ba62020-01-17 12:43:17 -080060
61public:
Marin Shalamanov46084422020-10-13 12:33:42 +020062 // Holds information about the layer vote
63 struct LayerVote {
64 LayerHistory::LayerVoteType type = LayerHistory::LayerVoteType::Heuristic;
Marin Shalamanove8a663d2020-11-24 17:48:00 +010065 Fps fps{0.0f};
Marin Shalamanov53fc11d2020-11-20 14:00:13 +010066 Seamlessness seamlessness = Seamlessness::Default;
Marin Shalamanov46084422020-10-13 12:33:42 +020067 };
68
Ady Abrahambdda8f02021-04-01 16:06:11 -070069 // FrameRateCompatibility specifies how we should interpret the frame rate associated with
70 // the layer.
71 enum class FrameRateCompatibility {
72 Default, // Layer didn't specify any specific handling strategy
73
74 Exact, // Layer needs the exact frame rate.
75
76 ExactOrMultiple, // Layer needs the exact frame rate (or a multiple of it) to present the
77 // content properly. Any other value will result in a pull down.
78
79 NoVote, // Layer doesn't have any requirements for the refresh rate and
80 // should not be considered when the display refresh rate is determined.
81 };
82
83 // Encapsulates the frame rate and compatibility of the layer. This information will be used
84 // when the display refresh rate is determined.
85 struct FrameRate {
86 using Seamlessness = scheduler::Seamlessness;
87
88 Fps rate;
89 FrameRateCompatibility type;
90 Seamlessness seamlessness;
91
92 FrameRate()
93 : rate(0),
94 type(FrameRateCompatibility::Default),
95 seamlessness(Seamlessness::Default) {}
96 FrameRate(Fps rate, FrameRateCompatibility type,
97 Seamlessness seamlessness = Seamlessness::OnlySeamless)
98 : rate(rate), type(type), seamlessness(getSeamlessness(rate, seamlessness)) {}
99
100 bool operator==(const FrameRate& other) const {
101 return rate.equalsWithMargin(other.rate) && type == other.type &&
102 seamlessness == other.seamlessness;
103 }
104
105 bool operator!=(const FrameRate& other) const { return !(*this == other); }
106
107 // Convert an ANATIVEWINDOW_FRAME_RATE_COMPATIBILITY_* value to a
108 // Layer::FrameRateCompatibility. Logs fatal if the compatibility value is invalid.
109 static FrameRateCompatibility convertCompatibility(int8_t compatibility);
110 static scheduler::Seamlessness convertChangeFrameRateStrategy(int8_t strategy);
111
112 private:
113 static Seamlessness getSeamlessness(Fps rate, Seamlessness seamlessness) {
114 if (!rate.isValid()) {
115 // Refresh rate of 0 is a special value which should reset the vote to
116 // its default value.
117 return Seamlessness::Default;
118 }
119 return seamlessness;
120 }
121 };
122
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700123 static void setTraceEnabled(bool enabled) { sTraceEnabled = enabled; }
124
Ady Abrahamb1b9d412020-06-01 19:53:52 -0700125 static void setRefreshRateConfigs(const RefreshRateConfigs& refreshRateConfigs) {
126 sRefreshRateConfigs = &refreshRateConfigs;
127 }
128
Ady Abrahambdda8f02021-04-01 16:06:11 -0700129 LayerInfo(const std::string& name, uid_t ownerUid, LayerHistory::LayerVoteType defaultVote);
Ady Abraham8a82ba62020-01-17 12:43:17 -0800130
Marin Shalamanov1bc43ee2020-11-20 16:56:52 +0100131 LayerInfo(const LayerInfo&) = delete;
132 LayerInfo& operator=(const LayerInfo&) = delete;
Ady Abraham8a82ba62020-01-17 12:43:17 -0800133
Ady Abrahambdda8f02021-04-01 16:06:11 -0700134 struct LayerProps {
135 bool visible = false;
136 FloatRect bounds;
137 ui::Transform transform;
138 FrameRate setFrameRateVote;
139 int32_t frameRateSelectionPriority = -1;
140 };
141
Ady Abraham8a82ba62020-01-17 12:43:17 -0800142 // Records the last requested present time. It also stores information about when
143 // the layer was last updated. If the present time is farther in the future than the
144 // updated time, the updated time is the present time.
Ady Abraham5def7332020-05-29 16:13:47 -0700145 void setLastPresentTime(nsecs_t lastPresentTime, nsecs_t now, LayerUpdateType updateType,
Ady Abrahambdda8f02021-04-01 16:06:11 -0700146 bool pendingModeChange, LayerProps props);
Ady Abraham8a82ba62020-01-17 12:43:17 -0800147
Ady Abraham8a82ba62020-01-17 12:43:17 -0800148 // Sets an explicit layer vote. This usually comes directly from the application via
149 // ANativeWindow_setFrameRate API
Marin Shalamanov46084422020-10-13 12:33:42 +0200150 void setLayerVote(LayerVote vote) { mLayerVote = vote; }
Ady Abraham8a82ba62020-01-17 12:43:17 -0800151
152 // Sets the default layer vote. This will be the layer vote after calling to resetLayerVote().
153 // This is used for layers that called to setLayerVote() and then removed the vote, so that the
154 // layer can go back to whatever vote it had before the app voted for it.
155 void setDefaultLayerVote(LayerHistory::LayerVoteType type) { mDefaultVote = type; }
156
157 // Resets the layer vote to its default.
Marin Shalamanove8a663d2020-11-24 17:48:00 +0100158 void resetLayerVote() { mLayerVote = {mDefaultVote, Fps(0.0f), Seamlessness::Default}; }
Ady Abraham8a82ba62020-01-17 12:43:17 -0800159
Ady Abrahambdda8f02021-04-01 16:06:11 -0700160 std::string getName() const { return mName; }
161
162 uid_t getOwnerUid() const { return mOwnerUid; }
163
Marin Shalamanov46084422020-10-13 12:33:42 +0200164 LayerVote getRefreshRateVote(nsecs_t now);
Ady Abraham8a82ba62020-01-17 12:43:17 -0800165
166 // Return the last updated time. If the present time is farther in the future than the
167 // updated time, the updated time is the present time.
168 nsecs_t getLastUpdatedTime() const { return mLastUpdatedTime; }
169
Ady Abrahambdda8f02021-04-01 16:06:11 -0700170 FrameRate getSetFrameRateVote() const { return mLayerProps.setFrameRateVote; }
171 bool isVisible() const { return mLayerProps.visible; }
172 int32_t getFrameRateSelectionPriority() const { return mLayerProps.frameRateSelectionPriority; }
173
174 FloatRect getBounds() const { return mLayerProps.bounds; }
175
176 ui::Transform getTransform() const { return mLayerProps.transform; }
177
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700178 // Returns a C string for tracing a vote
179 const char* getTraceTag(LayerHistory::LayerVoteType type) const;
180
Ady Abraham983e5682020-05-28 16:49:18 -0700181 void onLayerInactive(nsecs_t now) {
Ady Abrahamdfb63ba2020-05-27 20:05:05 +0000182 // Mark mFrameTimeValidSince to now to ignore all previous frame times.
183 // We are not deleting the old frame to keep track of whether we should treat the first
184 // buffer as Max as we don't know anything about this layer or Min as this layer is
185 // posting infrequent updates.
Ady Abraham983e5682020-05-28 16:49:18 -0700186 const auto timePoint = std::chrono::nanoseconds(now);
187 mFrameTimeValidSince = std::chrono::time_point<std::chrono::steady_clock>(timePoint);
Ady Abrahamb1b9d412020-06-01 19:53:52 -0700188 mLastRefreshRate = {};
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700189 mRefreshRateHistory.clear();
Ady Abrahama61edcb2020-01-30 18:32:03 -0800190 }
Ady Abraham8a82ba62020-01-17 12:43:17 -0800191
Ady Abraham983e5682020-05-28 16:49:18 -0700192 void clearHistory(nsecs_t now) {
193 onLayerInactive(now);
194 mFrameTimes.clear();
195 }
196
Ady Abraham8a82ba62020-01-17 12:43:17 -0800197private:
Ady Abrahama61edcb2020-01-30 18:32:03 -0800198 // Used to store the layer timestamps
199 struct FrameTimeData {
Marin Shalamanov2045d5b2020-12-28 18:11:41 +0100200 nsecs_t presentTime; // desiredPresentTime, if provided
Ady Abrahama61edcb2020-01-30 18:32:03 -0800201 nsecs_t queueTime; // buffer queue time
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100202 bool pendingModeChange;
Ady Abrahama61edcb2020-01-30 18:32:03 -0800203 };
204
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700205 // Holds information about the calculated and reported refresh rate
206 struct RefreshRateHeuristicData {
207 // Rate calculated on the layer
Marin Shalamanove8a663d2020-11-24 17:48:00 +0100208 Fps calculated{0.0f};
Marin Shalamanov1bc43ee2020-11-20 16:56:52 +0100209 // Last reported rate for LayerInfo::getRefreshRate()
Marin Shalamanove8a663d2020-11-24 17:48:00 +0100210 Fps reported{0.0f};
Marin Shalamanov1bc43ee2020-11-20 16:56:52 +0100211 // Whether the last reported rate for LayerInfo::getRefreshRate()
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700212 // was due to animation or infrequent updates
213 bool animatingOrInfrequent = false;
214 };
215
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700216 // Class to store past calculated refresh rate and determine whether
217 // the refresh rate calculated is consistent with past values
218 class RefreshRateHistory {
219 public:
220 static constexpr auto HISTORY_SIZE = 90;
221 static constexpr std::chrono::nanoseconds HISTORY_DURATION = 2s;
222
223 RefreshRateHistory(const std::string& name) : mName(name) {}
224
225 // Clears History
226 void clear();
227
228 // Adds a new refresh rate and returns true if it is consistent
Marin Shalamanove8a663d2020-11-24 17:48:00 +0100229 bool add(Fps refreshRate, nsecs_t now);
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700230
231 private:
Marin Shalamanov1bc43ee2020-11-20 16:56:52 +0100232 friend class LayerHistoryTest;
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700233
234 // Holds the refresh rate when it was calculated
235 struct RefreshRateData {
Marin Shalamanove8a663d2020-11-24 17:48:00 +0100236 Fps refreshRate{0.0f};
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700237 nsecs_t timestamp = 0;
238
239 bool operator<(const RefreshRateData& other) const {
Marin Shalamanove8a663d2020-11-24 17:48:00 +0100240 // We don't need comparison with margins since we are using
241 // this to find the min and max refresh rates.
242 return refreshRate.getValue() < other.refreshRate.getValue();
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700243 }
244 };
245
246 // Holds tracing strings
247 struct HeuristicTraceTagData {
248 std::string min;
249 std::string max;
250 std::string consistent;
251 std::string average;
252 };
253
254 bool isConsistent() const;
255 HeuristicTraceTagData makeHeuristicTraceTagData() const;
256
257 const std::string mName;
258 mutable std::optional<HeuristicTraceTagData> mHeuristicTraceTagData;
259 std::deque<RefreshRateData> mRefreshRates;
Marin Shalamanove8a663d2020-11-24 17:48:00 +0100260 static constexpr float MARGIN_CONSISTENT_FPS = 1.0;
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700261 };
262
Ady Abrahamaf6d8a42020-05-27 19:56:15 +0000263 bool isFrequent(nsecs_t now) const;
Ady Abraham5def7332020-05-29 16:13:47 -0700264 bool isAnimating(nsecs_t now) const;
Ady Abraham8a82ba62020-01-17 12:43:17 -0800265 bool hasEnoughDataForHeuristic() const;
Marin Shalamanove8a663d2020-11-24 17:48:00 +0100266 std::optional<Fps> calculateRefreshRateIfPossible(nsecs_t now);
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700267 std::optional<nsecs_t> calculateAverageFrameTime() const;
Ady Abrahamdfb63ba2020-05-27 20:05:05 +0000268 bool isFrameTimeValid(const FrameTimeData&) const;
Ady Abraham8a82ba62020-01-17 12:43:17 -0800269
Ady Abrahama6b676e2020-05-27 14:29:09 -0700270 const std::string mName;
Ady Abrahambdda8f02021-04-01 16:06:11 -0700271 const uid_t mOwnerUid;
Ady Abrahama6b676e2020-05-27 14:29:09 -0700272
Marin Shalamanov4ad8b302020-12-11 15:50:08 +0100273 // Used for sanitizing the heuristic data. If two frames are less than
274 // this period apart from each other they'll be considered as duplicates.
275 static constexpr nsecs_t kMinPeriodBetweenFrames = Fps(120.f).getPeriodNsecs();
Marin Shalamanov2045d5b2020-12-28 18:11:41 +0100276 // Used for sanitizing the heuristic data. If two frames are more than
277 // this period apart from each other, the interval between them won't be
278 // taken into account when calculating average frame rate.
279 static constexpr nsecs_t kMaxPeriodBetweenFrames = kMinFpsForFrequentLayer.getPeriodNsecs();
Ady Abraham8a82ba62020-01-17 12:43:17 -0800280 LayerHistory::LayerVoteType mDefaultVote;
281
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700282 LayerVote mLayerVote;
283
Ady Abraham8a82ba62020-01-17 12:43:17 -0800284 nsecs_t mLastUpdatedTime = 0;
285
Ady Abraham5def7332020-05-29 16:13:47 -0700286 nsecs_t mLastAnimationTime = 0;
287
Ady Abrahamb1b9d412020-06-01 19:53:52 -0700288 RefreshRateHeuristicData mLastRefreshRate;
Ady Abraham8a82ba62020-01-17 12:43:17 -0800289
Ady Abraham8a82ba62020-01-17 12:43:17 -0800290 std::deque<FrameTimeData> mFrameTimes;
Ady Abrahamdfb63ba2020-05-27 20:05:05 +0000291 std::chrono::time_point<std::chrono::steady_clock> mFrameTimeValidSince =
292 std::chrono::steady_clock::now();
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700293 static constexpr size_t HISTORY_SIZE = RefreshRateHistory::HISTORY_SIZE;
294 static constexpr std::chrono::nanoseconds HISTORY_DURATION = 1s;
295
Ady Abrahambdda8f02021-04-01 16:06:11 -0700296 LayerProps mLayerProps;
297
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700298 RefreshRateHistory mRefreshRateHistory;
299
300 mutable std::unordered_map<LayerHistory::LayerVoteType, std::string> mTraceTags;
Ady Abrahamb1b9d412020-06-01 19:53:52 -0700301
302 // Shared for all LayerInfo instances
303 static const RefreshRateConfigs* sRefreshRateConfigs;
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700304 static bool sTraceEnabled;
Ady Abraham8a82ba62020-01-17 12:43:17 -0800305};
306
307} // namespace scheduler
308} // namespace android