blob: 33dc66fd19c7c2963292ad5bfee13eef77132103 [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
19#include <utils/Timers.h>
20
21#include <chrono>
22#include <deque>
23
24#include "LayerHistory.h"
25#include "RefreshRateConfigs.h"
26#include "SchedulerUtils.h"
27
28namespace android {
29
30class Layer;
31
32namespace scheduler {
33
34using namespace std::chrono_literals;
35
36// Maximum period between presents for a layer to be considered active.
37constexpr std::chrono::nanoseconds MAX_ACTIVE_LAYER_PERIOD_NS = 1200ms;
38
39// Earliest present time for a layer to be considered active.
40constexpr nsecs_t getActiveLayerThreshold(nsecs_t now) {
41 return now - MAX_ACTIVE_LAYER_PERIOD_NS.count();
42}
43
44// Stores history of present times and refresh rates for a layer.
45class LayerInfoV2 {
Ady Abraham5def7332020-05-29 16:13:47 -070046 using LayerUpdateType = LayerHistory::LayerUpdateType;
47
Ady Abraham8a82ba62020-01-17 12:43:17 -080048 // Layer is considered frequent if the earliest value in the window of most recent present times
49 // is within a threshold. If a layer is infrequent, its average refresh rate is disregarded in
50 // favor of a low refresh rate.
51 static constexpr size_t FREQUENT_LAYER_WINDOW_SIZE = 3;
Ady Abrahamf5bc7792020-05-27 20:04:20 +000052 static constexpr float MIN_FPS_FOR_FREQUENT_LAYER = 10.0f;
53 static constexpr auto MAX_FREQUENT_LAYER_PERIOD_NS =
54 std::chrono::nanoseconds(static_cast<nsecs_t>(1e9f / MIN_FPS_FOR_FREQUENT_LAYER)) + 1ms;
Ady Abraham8a82ba62020-01-17 12:43:17 -080055
56 friend class LayerHistoryTestV2;
57
58public:
Ady Abraham0ccd79b2020-06-10 10:11:17 -070059 static void setTraceEnabled(bool enabled) { sTraceEnabled = enabled; }
60
Ady Abrahamb1b9d412020-06-01 19:53:52 -070061 static void setRefreshRateConfigs(const RefreshRateConfigs& refreshRateConfigs) {
62 sRefreshRateConfigs = &refreshRateConfigs;
63 }
64
Ady Abrahama6b676e2020-05-27 14:29:09 -070065 LayerInfoV2(const std::string& name, nsecs_t highRefreshRatePeriod,
66 LayerHistory::LayerVoteType defaultVote);
Ady Abraham8a82ba62020-01-17 12:43:17 -080067
68 LayerInfoV2(const LayerInfo&) = delete;
69 LayerInfoV2& operator=(const LayerInfoV2&) = delete;
70
71 // Records the last requested present time. It also stores information about when
72 // the layer was last updated. If the present time is farther in the future than the
73 // updated time, the updated time is the present time.
Ady Abraham5def7332020-05-29 16:13:47 -070074 void setLastPresentTime(nsecs_t lastPresentTime, nsecs_t now, LayerUpdateType updateType,
75 bool pendingConfigChange);
Ady Abraham8a82ba62020-01-17 12:43:17 -080076
Ady Abraham8a82ba62020-01-17 12:43:17 -080077 // Sets an explicit layer vote. This usually comes directly from the application via
78 // ANativeWindow_setFrameRate API
79 void setLayerVote(LayerHistory::LayerVoteType type, float fps) { mLayerVote = {type, fps}; }
80
81 // Sets the default layer vote. This will be the layer vote after calling to resetLayerVote().
82 // This is used for layers that called to setLayerVote() and then removed the vote, so that the
83 // layer can go back to whatever vote it had before the app voted for it.
84 void setDefaultLayerVote(LayerHistory::LayerVoteType type) { mDefaultVote = type; }
85
86 // Resets the layer vote to its default.
87 void resetLayerVote() { mLayerVote = {mDefaultVote, 0.0f}; }
88
89 std::pair<LayerHistory::LayerVoteType, float> getRefreshRate(nsecs_t now);
90
91 // Return the last updated time. If the present time is farther in the future than the
92 // updated time, the updated time is the present time.
93 nsecs_t getLastUpdatedTime() const { return mLastUpdatedTime; }
94
Ady Abraham0ccd79b2020-06-10 10:11:17 -070095 // Returns a C string for tracing a vote
96 const char* getTraceTag(LayerHistory::LayerVoteType type) const;
97
Ady Abraham983e5682020-05-28 16:49:18 -070098 void onLayerInactive(nsecs_t now) {
Ady Abrahamdfb63ba2020-05-27 20:05:05 +000099 // Mark mFrameTimeValidSince to now to ignore all previous frame times.
100 // We are not deleting the old frame to keep track of whether we should treat the first
101 // buffer as Max as we don't know anything about this layer or Min as this layer is
102 // posting infrequent updates.
Ady Abraham983e5682020-05-28 16:49:18 -0700103 const auto timePoint = std::chrono::nanoseconds(now);
104 mFrameTimeValidSince = std::chrono::time_point<std::chrono::steady_clock>(timePoint);
Ady Abrahamb1b9d412020-06-01 19:53:52 -0700105 mLastRefreshRate = {};
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700106 mRefreshRateHistory.clear();
Ady Abrahama61edcb2020-01-30 18:32:03 -0800107 }
Ady Abraham8a82ba62020-01-17 12:43:17 -0800108
Ady Abraham983e5682020-05-28 16:49:18 -0700109 void clearHistory(nsecs_t now) {
110 onLayerInactive(now);
111 mFrameTimes.clear();
112 }
113
Ady Abraham8a82ba62020-01-17 12:43:17 -0800114private:
Ady Abrahama61edcb2020-01-30 18:32:03 -0800115 // Used to store the layer timestamps
116 struct FrameTimeData {
117 nsecs_t presetTime; // desiredPresentTime, if provided
118 nsecs_t queueTime; // buffer queue time
Ady Abraham32efd542020-05-19 17:49:26 -0700119 bool pendingConfigChange;
Ady Abrahama61edcb2020-01-30 18:32:03 -0800120 };
121
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700122 // Holds information about the calculated and reported refresh rate
123 struct RefreshRateHeuristicData {
124 // Rate calculated on the layer
125 float calculated = 0.0f;
126 // Last reported rate for LayerInfoV2::getRefreshRate()
127 float reported = 0.0f;
128 // Whether the last reported rate for LayerInfoV2::getRefreshRate()
129 // was due to animation or infrequent updates
130 bool animatingOrInfrequent = false;
131 };
132
133 // Holds information about the layer vote
134 struct LayerVote {
135 LayerHistory::LayerVoteType type = LayerHistory::LayerVoteType::Heuristic;
136 float fps = 0.0f;
137 };
138
139 // Class to store past calculated refresh rate and determine whether
140 // the refresh rate calculated is consistent with past values
141 class RefreshRateHistory {
142 public:
143 static constexpr auto HISTORY_SIZE = 90;
144 static constexpr std::chrono::nanoseconds HISTORY_DURATION = 2s;
145
146 RefreshRateHistory(const std::string& name) : mName(name) {}
147
148 // Clears History
149 void clear();
150
151 // Adds a new refresh rate and returns true if it is consistent
152 bool add(float refreshRate, nsecs_t now);
153
154 private:
155 friend class LayerHistoryTestV2;
156
157 // Holds the refresh rate when it was calculated
158 struct RefreshRateData {
159 float refreshRate = 0.0f;
160 nsecs_t timestamp = 0;
161
162 bool operator<(const RefreshRateData& other) const {
163 return refreshRate < other.refreshRate;
164 }
165 };
166
167 // Holds tracing strings
168 struct HeuristicTraceTagData {
169 std::string min;
170 std::string max;
171 std::string consistent;
172 std::string average;
173 };
174
175 bool isConsistent() const;
176 HeuristicTraceTagData makeHeuristicTraceTagData() const;
177
178 const std::string mName;
179 mutable std::optional<HeuristicTraceTagData> mHeuristicTraceTagData;
180 std::deque<RefreshRateData> mRefreshRates;
181 static constexpr float MARGIN_FPS = 1.0;
182 };
183
Ady Abrahamaf6d8a42020-05-27 19:56:15 +0000184 bool isFrequent(nsecs_t now) const;
Ady Abraham5def7332020-05-29 16:13:47 -0700185 bool isAnimating(nsecs_t now) const;
Ady Abraham8a82ba62020-01-17 12:43:17 -0800186 bool hasEnoughDataForHeuristic() const;
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700187 std::optional<float> calculateRefreshRateIfPossible(nsecs_t now);
188 std::optional<nsecs_t> calculateAverageFrameTime() const;
Ady Abrahamdfb63ba2020-05-27 20:05:05 +0000189 bool isFrameTimeValid(const FrameTimeData&) const;
Ady Abraham8a82ba62020-01-17 12:43:17 -0800190
Ady Abrahama6b676e2020-05-27 14:29:09 -0700191 const std::string mName;
192
Ady Abraham8a82ba62020-01-17 12:43:17 -0800193 // Used for sanitizing the heuristic data
194 const nsecs_t mHighRefreshRatePeriod;
195 LayerHistory::LayerVoteType mDefaultVote;
196
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700197 LayerVote mLayerVote;
198
Ady Abraham8a82ba62020-01-17 12:43:17 -0800199 nsecs_t mLastUpdatedTime = 0;
200
Ady Abraham5def7332020-05-29 16:13:47 -0700201 nsecs_t mLastAnimationTime = 0;
202
Ady Abrahamb1b9d412020-06-01 19:53:52 -0700203 RefreshRateHeuristicData mLastRefreshRate;
Ady Abraham8a82ba62020-01-17 12:43:17 -0800204
Ady Abraham8a82ba62020-01-17 12:43:17 -0800205 std::deque<FrameTimeData> mFrameTimes;
Ady Abrahamdfb63ba2020-05-27 20:05:05 +0000206 std::chrono::time_point<std::chrono::steady_clock> mFrameTimeValidSince =
207 std::chrono::steady_clock::now();
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700208 static constexpr size_t HISTORY_SIZE = RefreshRateHistory::HISTORY_SIZE;
209 static constexpr std::chrono::nanoseconds HISTORY_DURATION = 1s;
210
211 RefreshRateHistory mRefreshRateHistory;
212
213 mutable std::unordered_map<LayerHistory::LayerVoteType, std::string> mTraceTags;
Ady Abrahamb1b9d412020-06-01 19:53:52 -0700214
215 // Shared for all LayerInfo instances
216 static const RefreshRateConfigs* sRefreshRateConfigs;
Ady Abraham0ccd79b2020-06-10 10:11:17 -0700217 static bool sTraceEnabled;
Ady Abraham8a82ba62020-01-17 12:43:17 -0800218};
219
220} // namespace scheduler
221} // namespace android