blob: c5a60573f53e328c00e30e1709dc93dc3e495206 [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
Dominik Laskowskif6b4ba62021-11-09 12:46:10 -080019#include <chrono>
20#include <deque>
21#include <optional>
22#include <string>
23#include <unordered_map>
24
Ady Abrahambdda8f02021-04-01 16:06:11 -070025#include <ui/Transform.h>
Ady Abraham8a82ba62020-01-17 12:43:17 -080026#include <utils/Timers.h>
27
Dominik Laskowskif6b4ba62021-11-09 12:46:10 -080028#include <scheduler/Seamlessness.h>
Ady Abraham8a82ba62020-01-17 12:43:17 -080029
30#include "LayerHistory.h"
Dominik Laskowskid82e0f02022-10-26 15:23:04 -040031#include "RefreshRateSelector.h"
Ady Abraham8a82ba62020-01-17 12:43:17 -080032
33namespace android {
34
35class Layer;
36
37namespace scheduler {
38
39using namespace std::chrono_literals;
Vishnu Nairef68d6d2023-02-28 06:18:27 +000040struct LayerProps;
Ady Abraham8a82ba62020-01-17 12:43:17 -080041// Maximum period between presents for a layer to be considered active.
42constexpr std::chrono::nanoseconds MAX_ACTIVE_LAYER_PERIOD_NS = 1200ms;
43
44// Earliest present time for a layer to be considered active.
45constexpr nsecs_t getActiveLayerThreshold(nsecs_t now) {
46 return now - MAX_ACTIVE_LAYER_PERIOD_NS.count();
47}
48
49// Stores history of present times and refresh rates for a layer.
Marin Shalamanov1bc43ee2020-11-20 16:56:52 +010050class LayerInfo {
Ady Abraham5def7332020-05-29 16:13:47 -070051 using LayerUpdateType = LayerHistory::LayerUpdateType;
52
Ady Abraham8a82ba62020-01-17 12:43:17 -080053 // Layer is considered frequent if the earliest value in the window of most recent present times
54 // is within a threshold. If a layer is infrequent, its average refresh rate is disregarded in
55 // favor of a low refresh rate.
Ady Abraham86ac5c52023-01-11 15:24:03 -080056 static constexpr size_t kFrequentLayerWindowSize = 4;
Dominik Laskowski6eab42d2021-09-13 14:34:13 -070057 static constexpr Fps kMinFpsForFrequentLayer = 10_Hz;
Marin Shalamanov2045d5b2020-12-28 18:11:41 +010058 static constexpr auto kMaxPeriodForFrequentLayerNs =
59 std::chrono::nanoseconds(kMinFpsForFrequentLayer.getPeriodNsecs()) + 1ms;
Ady Abraham8a82ba62020-01-17 12:43:17 -080060
Marin Shalamanov1bc43ee2020-11-20 16:56:52 +010061 friend class LayerHistoryTest;
Marin Shalamanov2045d5b2020-12-28 18:11:41 +010062 friend class LayerInfoTest;
Ady Abraham8a82ba62020-01-17 12:43:17 -080063
64public:
Marin Shalamanov46084422020-10-13 12:33:42 +020065 // Holds information about the layer vote
66 struct LayerVote {
67 LayerHistory::LayerVoteType type = LayerHistory::LayerVoteType::Heuristic;
Dominik Laskowski6eab42d2021-09-13 14:34:13 -070068 Fps fps;
Marin Shalamanov53fc11d2020-11-20 14:00:13 +010069 Seamlessness seamlessness = Seamlessness::Default;
Marin Shalamanov46084422020-10-13 12:33:42 +020070 };
71
Ady Abrahambdda8f02021-04-01 16:06:11 -070072 // FrameRateCompatibility specifies how we should interpret the frame rate associated with
73 // the layer.
74 enum class FrameRateCompatibility {
75 Default, // Layer didn't specify any specific handling strategy
76
Andy Labrada096227e2022-06-15 16:58:11 +000077 Min, // Layer needs the minimum frame rate.
78
Ady Abrahambdda8f02021-04-01 16:06:11 -070079 Exact, // Layer needs the exact frame rate.
80
81 ExactOrMultiple, // Layer needs the exact frame rate (or a multiple of it) to present the
82 // content properly. Any other value will result in a pull down.
83
84 NoVote, // Layer doesn't have any requirements for the refresh rate and
85 // should not be considered when the display refresh rate is determined.
Dominik Laskowskif5d0ea52021-09-26 17:27:01 -070086
87 ftl_last = NoVote
Ady Abrahambdda8f02021-04-01 16:06:11 -070088 };
89
90 // Encapsulates the frame rate and compatibility of the layer. This information will be used
91 // when the display refresh rate is determined.
92 struct FrameRate {
93 using Seamlessness = scheduler::Seamlessness;
94
95 Fps rate;
Dominik Laskowski6eab42d2021-09-13 14:34:13 -070096 FrameRateCompatibility type = FrameRateCompatibility::Default;
97 Seamlessness seamlessness = Seamlessness::Default;
Ady Abrahambdda8f02021-04-01 16:06:11 -070098
Dominik Laskowski6eab42d2021-09-13 14:34:13 -070099 FrameRate() = default;
100
Ady Abrahambdda8f02021-04-01 16:06:11 -0700101 FrameRate(Fps rate, FrameRateCompatibility type,
102 Seamlessness seamlessness = Seamlessness::OnlySeamless)
103 : rate(rate), type(type), seamlessness(getSeamlessness(rate, seamlessness)) {}
104
105 bool operator==(const FrameRate& other) const {
Dominik Laskowski6eab42d2021-09-13 14:34:13 -0700106 return isApproxEqual(rate, other.rate) && type == other.type &&
Ady Abrahambdda8f02021-04-01 16:06:11 -0700107 seamlessness == other.seamlessness;
108 }
109
110 bool operator!=(const FrameRate& other) const { return !(*this == other); }
111
112 // Convert an ANATIVEWINDOW_FRAME_RATE_COMPATIBILITY_* value to a
113 // Layer::FrameRateCompatibility. Logs fatal if the compatibility value is invalid.
114 static FrameRateCompatibility convertCompatibility(int8_t compatibility);
115 static scheduler::Seamlessness convertChangeFrameRateStrategy(int8_t strategy);
116
117 private:
118 static Seamlessness getSeamlessness(Fps rate, Seamlessness seamlessness) {
119 if (!rate.isValid()) {
120 // Refresh rate of 0 is a special value which should reset the vote to
121 // its default value.
122 return Seamlessness::Default;
123 }
124 return seamlessness;
125 }
126 };
127
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700128 static void setTraceEnabled(bool enabled) { sTraceEnabled = enabled; }
129
Ady Abrahambdda8f02021-04-01 16:06:11 -0700130 LayerInfo(const std::string& name, uid_t ownerUid, LayerHistory::LayerVoteType defaultVote);
Ady Abraham8a82ba62020-01-17 12:43:17 -0800131
Marin Shalamanov1bc43ee2020-11-20 16:56:52 +0100132 LayerInfo(const LayerInfo&) = delete;
133 LayerInfo& operator=(const LayerInfo&) = delete;
Ady Abraham8a82ba62020-01-17 12:43:17 -0800134
135 // Records the last requested present time. It also stores information about when
136 // the layer was last updated. If the present time is farther in the future than the
137 // updated time, the updated time is the present time.
Ady Abraham5def7332020-05-29 16:13:47 -0700138 void setLastPresentTime(nsecs_t lastPresentTime, nsecs_t now, LayerUpdateType updateType,
Vishnu Nairef68d6d2023-02-28 06:18:27 +0000139 bool pendingModeChange, const LayerProps& props);
Ady Abraham8a82ba62020-01-17 12:43:17 -0800140
Ady Abraham8a82ba62020-01-17 12:43:17 -0800141 // Sets an explicit layer vote. This usually comes directly from the application via
142 // ANativeWindow_setFrameRate API
Marin Shalamanov46084422020-10-13 12:33:42 +0200143 void setLayerVote(LayerVote vote) { mLayerVote = vote; }
Ady Abraham8a82ba62020-01-17 12:43:17 -0800144
145 // Sets the default layer vote. This will be the layer vote after calling to resetLayerVote().
146 // This is used for layers that called to setLayerVote() and then removed the vote, so that the
147 // layer can go back to whatever vote it had before the app voted for it.
148 void setDefaultLayerVote(LayerHistory::LayerVoteType type) { mDefaultVote = type; }
149
150 // Resets the layer vote to its default.
Dominik Laskowski6eab42d2021-09-13 14:34:13 -0700151 void resetLayerVote() { mLayerVote = {mDefaultVote, Fps(), Seamlessness::Default}; }
Ady Abraham8a82ba62020-01-17 12:43:17 -0800152
Ady Abrahambdda8f02021-04-01 16:06:11 -0700153 std::string getName() const { return mName; }
154
155 uid_t getOwnerUid() const { return mOwnerUid; }
156
Dominik Laskowskid82e0f02022-10-26 15:23:04 -0400157 LayerVote getRefreshRateVote(const RefreshRateSelector&, nsecs_t now);
Ady Abraham8a82ba62020-01-17 12:43:17 -0800158
159 // Return the last updated time. If the present time is farther in the future than the
160 // updated time, the updated time is the present time.
161 nsecs_t getLastUpdatedTime() const { return mLastUpdatedTime; }
162
Vishnu Nairef68d6d2023-02-28 06:18:27 +0000163 FrameRate getSetFrameRateVote() const;
164 bool isVisible() const;
165 int32_t getFrameRateSelectionPriority() const;
166 FloatRect getBounds() const;
167 ui::Transform getTransform() const;
Ady Abrahambdda8f02021-04-01 16:06:11 -0700168
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700169 // Returns a C string for tracing a vote
170 const char* getTraceTag(LayerHistory::LayerVoteType type) const;
171
Nathaniel Nifong1303d912021-10-06 09:41:24 -0400172 // Return the framerate of this layer.
173 Fps getFps(nsecs_t now) const;
174
Ady Abraham983e5682020-05-28 16:49:18 -0700175 void onLayerInactive(nsecs_t now) {
Ady Abrahamdfb63ba2020-05-27 20:05:05 +0000176 // Mark mFrameTimeValidSince to now to ignore all previous frame times.
177 // We are not deleting the old frame to keep track of whether we should treat the first
178 // buffer as Max as we don't know anything about this layer or Min as this layer is
179 // posting infrequent updates.
Ady Abraham983e5682020-05-28 16:49:18 -0700180 const auto timePoint = std::chrono::nanoseconds(now);
181 mFrameTimeValidSince = std::chrono::time_point<std::chrono::steady_clock>(timePoint);
Ady Abrahamb1b9d412020-06-01 19:53:52 -0700182 mLastRefreshRate = {};
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700183 mRefreshRateHistory.clear();
ramindani63db24e2023-04-03 10:56:05 -0700184 mIsFrequencyConclusive = true;
Ady Abrahama61edcb2020-01-30 18:32:03 -0800185 }
Ady Abraham8a82ba62020-01-17 12:43:17 -0800186
Ady Abraham983e5682020-05-28 16:49:18 -0700187 void clearHistory(nsecs_t now) {
188 onLayerInactive(now);
189 mFrameTimes.clear();
190 }
191
Ady Abraham8a82ba62020-01-17 12:43:17 -0800192private:
Ady Abrahama61edcb2020-01-30 18:32:03 -0800193 // Used to store the layer timestamps
194 struct FrameTimeData {
Marin Shalamanov2045d5b2020-12-28 18:11:41 +0100195 nsecs_t presentTime; // desiredPresentTime, if provided
Ady Abrahama61edcb2020-01-30 18:32:03 -0800196 nsecs_t queueTime; // buffer queue time
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100197 bool pendingModeChange;
Ady Abrahama61edcb2020-01-30 18:32:03 -0800198 };
199
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700200 // Holds information about the calculated and reported refresh rate
201 struct RefreshRateHeuristicData {
202 // Rate calculated on the layer
Dominik Laskowski6eab42d2021-09-13 14:34:13 -0700203 Fps calculated;
Marin Shalamanov1bc43ee2020-11-20 16:56:52 +0100204 // Last reported rate for LayerInfo::getRefreshRate()
Dominik Laskowski6eab42d2021-09-13 14:34:13 -0700205 Fps reported;
Marin Shalamanov1bc43ee2020-11-20 16:56:52 +0100206 // Whether the last reported rate for LayerInfo::getRefreshRate()
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700207 // was due to animation or infrequent updates
Ady Abraham86ac5c52023-01-11 15:24:03 -0800208 bool animating = false;
209 // Whether the last reported rate for LayerInfo::getRefreshRate()
210 // was due to infrequent updates
211 bool infrequent = false;
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700212 };
213
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700214 // Class to store past calculated refresh rate and determine whether
215 // the refresh rate calculated is consistent with past values
216 class RefreshRateHistory {
217 public:
218 static constexpr auto HISTORY_SIZE = 90;
219 static constexpr std::chrono::nanoseconds HISTORY_DURATION = 2s;
220
221 RefreshRateHistory(const std::string& name) : mName(name) {}
222
223 // Clears History
224 void clear();
225
226 // Adds a new refresh rate and returns true if it is consistent
Marin Shalamanove8a663d2020-11-24 17:48:00 +0100227 bool add(Fps refreshRate, nsecs_t now);
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700228
229 private:
Marin Shalamanov1bc43ee2020-11-20 16:56:52 +0100230 friend class LayerHistoryTest;
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700231
232 // Holds the refresh rate when it was calculated
233 struct RefreshRateData {
Dominik Laskowski6eab42d2021-09-13 14:34:13 -0700234 Fps refreshRate;
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700235 nsecs_t timestamp = 0;
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700236 };
237
238 // Holds tracing strings
239 struct HeuristicTraceTagData {
240 std::string min;
241 std::string max;
242 std::string consistent;
243 std::string average;
244 };
245
246 bool isConsistent() const;
247 HeuristicTraceTagData makeHeuristicTraceTagData() const;
248
249 const std::string mName;
250 mutable std::optional<HeuristicTraceTagData> mHeuristicTraceTagData;
251 std::deque<RefreshRateData> mRefreshRates;
Marin Shalamanove8a663d2020-11-24 17:48:00 +0100252 static constexpr float MARGIN_CONSISTENT_FPS = 1.0;
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700253 };
254
ramindani63db24e2023-04-03 10:56:05 -0700255 // Represents whether we were able to determine either layer is frequent or infrequent
256 bool mIsFrequencyConclusive = true;
257 struct Frequent {
258 bool isFrequent;
259 bool clearHistory;
260 // Represents whether we were able to determine isFrequent conclusively
261 bool isConclusive;
262 };
263 Frequent 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;
Dominik Laskowskid82e0f02022-10-26 15:23:04 -0400266 std::optional<Fps> calculateRefreshRateIfPossible(const RefreshRateSelector&, 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.
Dominik Laskowski6eab42d2021-09-13 14:34:13 -0700275 static constexpr nsecs_t kMinPeriodBetweenFrames = (240_Hz).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
Vishnu Nairef68d6d2023-02-28 06:18:27 +0000296 std::unique_ptr<LayerProps> mLayerProps;
Ady Abrahambdda8f02021-04-01 16:06:11 -0700297
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
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700303 static bool sTraceEnabled;
Ady Abraham8a82ba62020-01-17 12:43:17 -0800304};
305
Vishnu Nairef68d6d2023-02-28 06:18:27 +0000306struct LayerProps {
307 bool visible = false;
308 FloatRect bounds;
309 ui::Transform transform;
310 LayerInfo::FrameRate setFrameRateVote;
311 int32_t frameRateSelectionPriority = -1;
312};
313
Ady Abraham8a82ba62020-01-17 12:43:17 -0800314} // namespace scheduler
315} // namespace android