blob: 92abbae532d52f402ecbdad9b8bed17e2dc69017 [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;
Dominik Laskowski6eab42d2021-09-13 14:34:13 -070054 static constexpr Fps kMinFpsForFrequentLayer = 10_Hz;
Marin Shalamanov2045d5b2020-12-28 18:11:41 +010055 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;
Dominik Laskowski6eab42d2021-09-13 14:34:13 -070065 Fps fps;
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;
Dominik Laskowski6eab42d2021-09-13 14:34:13 -070089 FrameRateCompatibility type = FrameRateCompatibility::Default;
90 Seamlessness seamlessness = Seamlessness::Default;
Ady Abrahambdda8f02021-04-01 16:06:11 -070091
Dominik Laskowski6eab42d2021-09-13 14:34:13 -070092 FrameRate() = default;
93
Ady Abrahambdda8f02021-04-01 16:06:11 -070094 FrameRate(Fps rate, FrameRateCompatibility type,
95 Seamlessness seamlessness = Seamlessness::OnlySeamless)
96 : rate(rate), type(type), seamlessness(getSeamlessness(rate, seamlessness)) {}
97
98 bool operator==(const FrameRate& other) const {
Dominik Laskowski6eab42d2021-09-13 14:34:13 -070099 return isApproxEqual(rate, other.rate) && type == other.type &&
Ady Abrahambdda8f02021-04-01 16:06:11 -0700100 seamlessness == other.seamlessness;
101 }
102
103 bool operator!=(const FrameRate& other) const { return !(*this == other); }
104
105 // Convert an ANATIVEWINDOW_FRAME_RATE_COMPATIBILITY_* value to a
106 // Layer::FrameRateCompatibility. Logs fatal if the compatibility value is invalid.
107 static FrameRateCompatibility convertCompatibility(int8_t compatibility);
108 static scheduler::Seamlessness convertChangeFrameRateStrategy(int8_t strategy);
109
110 private:
111 static Seamlessness getSeamlessness(Fps rate, Seamlessness seamlessness) {
112 if (!rate.isValid()) {
113 // Refresh rate of 0 is a special value which should reset the vote to
114 // its default value.
115 return Seamlessness::Default;
116 }
117 return seamlessness;
118 }
119 };
120
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700121 static void setTraceEnabled(bool enabled) { sTraceEnabled = enabled; }
122
Ady Abrahambdda8f02021-04-01 16:06:11 -0700123 LayerInfo(const std::string& name, uid_t ownerUid, LayerHistory::LayerVoteType defaultVote);
Ady Abraham8a82ba62020-01-17 12:43:17 -0800124
Marin Shalamanov1bc43ee2020-11-20 16:56:52 +0100125 LayerInfo(const LayerInfo&) = delete;
126 LayerInfo& operator=(const LayerInfo&) = delete;
Ady Abraham8a82ba62020-01-17 12:43:17 -0800127
Ady Abrahambdda8f02021-04-01 16:06:11 -0700128 struct LayerProps {
129 bool visible = false;
130 FloatRect bounds;
131 ui::Transform transform;
132 FrameRate setFrameRateVote;
133 int32_t frameRateSelectionPriority = -1;
134 };
135
Ady Abraham8a82ba62020-01-17 12:43:17 -0800136 // Records the last requested present time. It also stores information about when
137 // the layer was last updated. If the present time is farther in the future than the
138 // updated time, the updated time is the present time.
Ady Abraham5def7332020-05-29 16:13:47 -0700139 void setLastPresentTime(nsecs_t lastPresentTime, nsecs_t now, LayerUpdateType updateType,
Ady Abrahambdda8f02021-04-01 16:06:11 -0700140 bool pendingModeChange, LayerProps props);
Ady Abraham8a82ba62020-01-17 12:43:17 -0800141
Ady Abraham8a82ba62020-01-17 12:43:17 -0800142 // Sets an explicit layer vote. This usually comes directly from the application via
143 // ANativeWindow_setFrameRate API
Marin Shalamanov46084422020-10-13 12:33:42 +0200144 void setLayerVote(LayerVote vote) { mLayerVote = vote; }
Ady Abraham8a82ba62020-01-17 12:43:17 -0800145
146 // Sets the default layer vote. This will be the layer vote after calling to resetLayerVote().
147 // This is used for layers that called to setLayerVote() and then removed the vote, so that the
148 // layer can go back to whatever vote it had before the app voted for it.
149 void setDefaultLayerVote(LayerHistory::LayerVoteType type) { mDefaultVote = type; }
150
151 // Resets the layer vote to its default.
Dominik Laskowski6eab42d2021-09-13 14:34:13 -0700152 void resetLayerVote() { mLayerVote = {mDefaultVote, Fps(), Seamlessness::Default}; }
Ady Abraham8a82ba62020-01-17 12:43:17 -0800153
Ady Abrahambdda8f02021-04-01 16:06:11 -0700154 std::string getName() const { return mName; }
155
156 uid_t getOwnerUid() const { return mOwnerUid; }
157
Ady Abraham3efa3942021-06-24 19:01:25 -0700158 LayerVote getRefreshRateVote(const RefreshRateConfigs&, nsecs_t now);
Ady Abraham8a82ba62020-01-17 12:43:17 -0800159
160 // Return the last updated time. If the present time is farther in the future than the
161 // updated time, the updated time is the present time.
162 nsecs_t getLastUpdatedTime() const { return mLastUpdatedTime; }
163
Ady Abrahambdda8f02021-04-01 16:06:11 -0700164 FrameRate getSetFrameRateVote() const { return mLayerProps.setFrameRateVote; }
165 bool isVisible() const { return mLayerProps.visible; }
166 int32_t getFrameRateSelectionPriority() const { return mLayerProps.frameRateSelectionPriority; }
167
168 FloatRect getBounds() const { return mLayerProps.bounds; }
169
170 ui::Transform getTransform() const { return mLayerProps.transform; }
171
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700172 // Returns a C string for tracing a vote
173 const char* getTraceTag(LayerHistory::LayerVoteType type) 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();
Ady Abrahama61edcb2020-01-30 18:32:03 -0800184 }
Ady Abraham8a82ba62020-01-17 12:43:17 -0800185
Ady Abraham983e5682020-05-28 16:49:18 -0700186 void clearHistory(nsecs_t now) {
187 onLayerInactive(now);
188 mFrameTimes.clear();
189 }
190
Ady Abraham8a82ba62020-01-17 12:43:17 -0800191private:
Ady Abrahama61edcb2020-01-30 18:32:03 -0800192 // Used to store the layer timestamps
193 struct FrameTimeData {
Marin Shalamanov2045d5b2020-12-28 18:11:41 +0100194 nsecs_t presentTime; // desiredPresentTime, if provided
Ady Abrahama61edcb2020-01-30 18:32:03 -0800195 nsecs_t queueTime; // buffer queue time
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100196 bool pendingModeChange;
Ady Abrahama61edcb2020-01-30 18:32:03 -0800197 };
198
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700199 // Holds information about the calculated and reported refresh rate
200 struct RefreshRateHeuristicData {
201 // Rate calculated on the layer
Dominik Laskowski6eab42d2021-09-13 14:34:13 -0700202 Fps calculated;
Marin Shalamanov1bc43ee2020-11-20 16:56:52 +0100203 // Last reported rate for LayerInfo::getRefreshRate()
Dominik Laskowski6eab42d2021-09-13 14:34:13 -0700204 Fps reported;
Marin Shalamanov1bc43ee2020-11-20 16:56:52 +0100205 // Whether the last reported rate for LayerInfo::getRefreshRate()
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700206 // was due to animation or infrequent updates
207 bool animatingOrInfrequent = false;
208 };
209
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700210 // Class to store past calculated refresh rate and determine whether
211 // the refresh rate calculated is consistent with past values
212 class RefreshRateHistory {
213 public:
214 static constexpr auto HISTORY_SIZE = 90;
215 static constexpr std::chrono::nanoseconds HISTORY_DURATION = 2s;
216
217 RefreshRateHistory(const std::string& name) : mName(name) {}
218
219 // Clears History
220 void clear();
221
222 // Adds a new refresh rate and returns true if it is consistent
Marin Shalamanove8a663d2020-11-24 17:48:00 +0100223 bool add(Fps refreshRate, nsecs_t now);
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700224
225 private:
Marin Shalamanov1bc43ee2020-11-20 16:56:52 +0100226 friend class LayerHistoryTest;
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700227
228 // Holds the refresh rate when it was calculated
229 struct RefreshRateData {
Dominik Laskowski6eab42d2021-09-13 14:34:13 -0700230 Fps refreshRate;
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700231 nsecs_t timestamp = 0;
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700232 };
233
234 // Holds tracing strings
235 struct HeuristicTraceTagData {
236 std::string min;
237 std::string max;
238 std::string consistent;
239 std::string average;
240 };
241
242 bool isConsistent() const;
243 HeuristicTraceTagData makeHeuristicTraceTagData() const;
244
245 const std::string mName;
246 mutable std::optional<HeuristicTraceTagData> mHeuristicTraceTagData;
247 std::deque<RefreshRateData> mRefreshRates;
Marin Shalamanove8a663d2020-11-24 17:48:00 +0100248 static constexpr float MARGIN_CONSISTENT_FPS = 1.0;
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700249 };
250
Ady Abrahamaf6d8a42020-05-27 19:56:15 +0000251 bool isFrequent(nsecs_t now) const;
Ady Abraham5def7332020-05-29 16:13:47 -0700252 bool isAnimating(nsecs_t now) const;
Ady Abraham8a82ba62020-01-17 12:43:17 -0800253 bool hasEnoughDataForHeuristic() const;
Ady Abraham3efa3942021-06-24 19:01:25 -0700254 std::optional<Fps> calculateRefreshRateIfPossible(const RefreshRateConfigs&, nsecs_t now);
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700255 std::optional<nsecs_t> calculateAverageFrameTime() const;
Ady Abrahamdfb63ba2020-05-27 20:05:05 +0000256 bool isFrameTimeValid(const FrameTimeData&) const;
Ady Abraham8a82ba62020-01-17 12:43:17 -0800257
Ady Abrahama6b676e2020-05-27 14:29:09 -0700258 const std::string mName;
Ady Abrahambdda8f02021-04-01 16:06:11 -0700259 const uid_t mOwnerUid;
Ady Abrahama6b676e2020-05-27 14:29:09 -0700260
Marin Shalamanov4ad8b302020-12-11 15:50:08 +0100261 // Used for sanitizing the heuristic data. If two frames are less than
262 // this period apart from each other they'll be considered as duplicates.
Dominik Laskowski6eab42d2021-09-13 14:34:13 -0700263 static constexpr nsecs_t kMinPeriodBetweenFrames = (240_Hz).getPeriodNsecs();
Marin Shalamanov2045d5b2020-12-28 18:11:41 +0100264 // Used for sanitizing the heuristic data. If two frames are more than
265 // this period apart from each other, the interval between them won't be
266 // taken into account when calculating average frame rate.
267 static constexpr nsecs_t kMaxPeriodBetweenFrames = kMinFpsForFrequentLayer.getPeriodNsecs();
Ady Abraham8a82ba62020-01-17 12:43:17 -0800268 LayerHistory::LayerVoteType mDefaultVote;
269
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700270 LayerVote mLayerVote;
271
Ady Abraham8a82ba62020-01-17 12:43:17 -0800272 nsecs_t mLastUpdatedTime = 0;
273
Ady Abraham5def7332020-05-29 16:13:47 -0700274 nsecs_t mLastAnimationTime = 0;
275
Ady Abrahamb1b9d412020-06-01 19:53:52 -0700276 RefreshRateHeuristicData mLastRefreshRate;
Ady Abraham8a82ba62020-01-17 12:43:17 -0800277
Ady Abraham8a82ba62020-01-17 12:43:17 -0800278 std::deque<FrameTimeData> mFrameTimes;
Ady Abrahamdfb63ba2020-05-27 20:05:05 +0000279 std::chrono::time_point<std::chrono::steady_clock> mFrameTimeValidSince =
280 std::chrono::steady_clock::now();
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700281 static constexpr size_t HISTORY_SIZE = RefreshRateHistory::HISTORY_SIZE;
282 static constexpr std::chrono::nanoseconds HISTORY_DURATION = 1s;
283
Ady Abrahambdda8f02021-04-01 16:06:11 -0700284 LayerProps mLayerProps;
285
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700286 RefreshRateHistory mRefreshRateHistory;
287
288 mutable std::unordered_map<LayerHistory::LayerVoteType, std::string> mTraceTags;
Ady Abrahamb1b9d412020-06-01 19:53:52 -0700289
290 // Shared for all LayerInfo instances
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700291 static bool sTraceEnabled;
Ady Abraham8a82ba62020-01-17 12:43:17 -0800292};
293
294} // namespace scheduler
295} // namespace android