blob: bd637df8bc184f13aa3c635bf439907dc877ff4a [file] [log] [blame]
Adithya Srinivasanf279e042020-08-17 14:56:27 -07001/*
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 <deque>
20#include <mutex>
21
Ady Abraham22c7b5c2020-09-22 19:33:40 -070022#include <gui/ISurfaceComposer.h>
Adithya Srinivasanf279e042020-08-17 14:56:27 -070023#include <ui/FenceTime.h>
24#include <utils/RefBase.h>
Adithya Srinivasan8fc601d2020-09-25 13:51:09 -070025#include <utils/String16.h>
Adithya Srinivasanf279e042020-08-17 14:56:27 -070026#include <utils/Timers.h>
Adithya Srinivasan8fc601d2020-09-25 13:51:09 -070027#include <utils/Vector.h>
Adithya Srinivasanf279e042020-08-17 14:56:27 -070028
29namespace android::frametimeline {
30
Adithya Srinivasan8fc601d2020-09-25 13:51:09 -070031/*
32 * The type of jank that is associated with a Display/Surface frame
33 */
34enum class JankType {
35 // No Jank
36 None,
37 // Jank not related to SurfaceFlinger or the App
38 Display,
39 // SF took too long on the CPU
40 SurfaceFlingerDeadlineMissed,
41 // Either App or GPU took too long on the frame
42 AppDeadlineMissed,
43 // Predictions live for 120ms, if prediction is expired for a frame, there is definitely a jank
44 // associated with the App if this is for a SurfaceFrame, and SF for a DisplayFrame.
45 PredictionExpired,
46 // Latching a buffer early might cause an early present of the frame
47 SurfaceFlingerEarlyLatch,
48};
49
50enum JankMetadata {
51 // Frame was presented earlier than expected
52 EarlyPresent = 0x1,
53 // Frame was presented later than expected
54 LatePresent = 0x2,
55 // App/SF started earlier than expected
56 EarlyStart = 0x4,
57 // App/SF started later than expected
58 LateStart = 0x8,
59 // App/SF finished work earlier than the deadline
60 EarlyFinish = 0x10,
61 // App/SF finished work later than the deadline
62 LateFinish = 0x20,
63 // SF was in GPU composition
64 GpuComposition = 0x40,
65};
66
Adithya Srinivasanf279e042020-08-17 14:56:27 -070067class FrameTimelineTest;
68
69/*
70 * Collection of timestamps that can be used for both predictions and actual times.
71 */
72struct TimelineItem {
73 TimelineItem(const nsecs_t startTime = 0, const nsecs_t endTime = 0,
74 const nsecs_t presentTime = 0)
75 : startTime(startTime), endTime(endTime), presentTime(presentTime) {}
76
77 nsecs_t startTime;
78 nsecs_t endTime;
79 nsecs_t presentTime;
Ady Abraham55fa7272020-09-30 19:19:27 -070080
81 bool operator==(const TimelineItem& other) const {
82 return startTime == other.startTime && endTime == other.endTime &&
83 presentTime == other.presentTime;
84 }
85
86 bool operator!=(const TimelineItem& other) const { return !(*this == other); }
Adithya Srinivasanf279e042020-08-17 14:56:27 -070087};
88
89/*
90 * TokenManager generates a running number token for a set of predictions made by VsyncPredictor. It
91 * saves these predictions for a short period of time and returns the predictions for a given token,
92 * if it hasn't expired.
93 */
94class TokenManager {
95public:
96 virtual ~TokenManager() = default;
97
98 // Generates a token for the given set of predictions. Stores the predictions for 120ms and
99 // destroys it later.
Adithya Srinivasan5f683cf2020-09-15 14:21:04 -0700100 virtual int64_t generateTokenForPredictions(TimelineItem&& prediction) = 0;
Adithya Srinivasanf279e042020-08-17 14:56:27 -0700101};
102
103enum class PredictionState {
104 Valid, // Predictions obtained successfully from the TokenManager
105 Expired, // TokenManager no longer has the predictions
106 None, // Predictions are either not present or didn't come from TokenManager
107};
108
109/*
110 * Stores a set of predictions and the corresponding actual timestamps pertaining to a single frame
111 * from the app
112 */
113class SurfaceFrame {
114public:
115 enum class PresentState {
116 Presented, // Buffer was latched and presented by SurfaceFlinger
117 Dropped, // Buffer was dropped by SurfaceFlinger
118 Unknown, // Initial state, SurfaceFlinger hasn't seen this buffer yet
119 };
120
121 virtual ~SurfaceFrame() = default;
122
Adithya Srinivasan8fc601d2020-09-25 13:51:09 -0700123 virtual TimelineItem getPredictions() const = 0;
124 virtual TimelineItem getActuals() const = 0;
125 virtual nsecs_t getActualQueueTime() const = 0;
126 virtual PresentState getPresentState() const = 0;
127 virtual PredictionState getPredictionState() const = 0;
Adithya Srinivasanf279e042020-08-17 14:56:27 -0700128
129 virtual void setPresentState(PresentState state) = 0;
Adithya Srinivasanf279e042020-08-17 14:56:27 -0700130
Adithya Srinivasan5f683cf2020-09-15 14:21:04 -0700131 // Actual timestamps of the app are set individually at different functions.
132 // Start time (if the app provides) and Queue time are accessible after queueing the frame,
Ady Abraham7f8a1e62020-09-28 16:09:35 -0700133 // whereas Acquire Fence time is available only during latch.
Adithya Srinivasan5f683cf2020-09-15 14:21:04 -0700134 virtual void setActualStartTime(nsecs_t actualStartTime) = 0;
Adithya Srinivasanf279e042020-08-17 14:56:27 -0700135 virtual void setActualQueueTime(nsecs_t actualQueueTime) = 0;
Ady Abraham7f8a1e62020-09-28 16:09:35 -0700136 virtual void setAcquireFenceTime(nsecs_t acquireFenceTime) = 0;
Adithya Srinivasanf279e042020-08-17 14:56:27 -0700137};
138
139/*
140 * Maintains a history of SurfaceFrames grouped together by the vsync time in which they were
141 * presented
142 */
143class FrameTimeline {
144public:
145 virtual ~FrameTimeline() = default;
Adithya Srinivasan5f683cf2020-09-15 14:21:04 -0700146 virtual TokenManager* getTokenManager() = 0;
Adithya Srinivasanf279e042020-08-17 14:56:27 -0700147
148 // Create a new surface frame, set the predictions based on a token and return it to the caller.
149 // Sets the PredictionState of SurfaceFrame.
150 virtual std::unique_ptr<SurfaceFrame> createSurfaceFrameForToken(
151 const std::string& layerName, std::optional<int64_t> token) = 0;
152
153 // Adds a new SurfaceFrame to the current DisplayFrame. Frames from multiple layers can be
154 // composited into one display frame.
155 virtual void addSurfaceFrame(std::unique_ptr<SurfaceFrame> surfaceFrame,
156 SurfaceFrame::PresentState state) = 0;
157
158 // The first function called by SF for the current DisplayFrame. Fetches SF predictions based on
159 // the token and sets the actualSfWakeTime for the current DisplayFrame.
160 virtual void setSfWakeUp(int64_t token, nsecs_t wakeupTime) = 0;
161
162 // Sets the sfPresentTime and finalizes the current DisplayFrame. Tracks the given present fence
163 // until it's signaled, and updates the present timestamps of all presented SurfaceFrames in
164 // that vsync.
165 virtual void setSfPresent(nsecs_t sfPresentTime,
166 const std::shared_ptr<FenceTime>& presentFence) = 0;
Adithya Srinivasan5f683cf2020-09-15 14:21:04 -0700167
Adithya Srinivasan8fc601d2020-09-25 13:51:09 -0700168 // Args:
169 // -jank : Dumps only the Display Frames that are either janky themselves
170 // or contain janky Surface Frames.
171 // -all : Dumps the entire list of DisplayFrames and the SurfaceFrames contained within
172 virtual void parseArgs(const Vector<String16>& args, std::string& result) = 0;
Adithya Srinivasan2d736322020-10-01 16:53:48 -0700173
174 // Sets the max number of display frames that can be stored. Called by SF backdoor.
175 virtual void setMaxDisplayFrames(uint32_t size);
176
177 // Restores the max number of display frames to default. Called by SF backdoor.
178 virtual void reset() = 0;
Adithya Srinivasanf279e042020-08-17 14:56:27 -0700179};
180
181namespace impl {
182
183using namespace std::chrono_literals;
184
185class TokenManager : public android::frametimeline::TokenManager {
186public:
Ady Abraham22c7b5c2020-09-22 19:33:40 -0700187 TokenManager() : mCurrentToken(ISurfaceComposer::INVALID_VSYNC_ID + 1) {}
Adithya Srinivasanf279e042020-08-17 14:56:27 -0700188 ~TokenManager() = default;
189
190 int64_t generateTokenForPredictions(TimelineItem&& predictions) override;
191 std::optional<TimelineItem> getPredictionsForToken(int64_t token);
192
193private:
194 // Friend class for testing
195 friend class android::frametimeline::FrameTimelineTest;
196
197 void flushTokens(nsecs_t flushTime) REQUIRES(mMutex);
198
199 std::unordered_map<int64_t, TimelineItem> mPredictions GUARDED_BY(mMutex);
200 std::vector<std::pair<int64_t, nsecs_t>> mTokens GUARDED_BY(mMutex);
201 int64_t mCurrentToken GUARDED_BY(mMutex);
202 std::mutex mMutex;
203 static constexpr nsecs_t kMaxRetentionTime =
204 std::chrono::duration_cast<std::chrono::nanoseconds>(120ms).count();
205};
206
207class SurfaceFrame : public android::frametimeline::SurfaceFrame {
208public:
209 SurfaceFrame(const std::string& layerName, PredictionState predictionState,
210 TimelineItem&& predictions);
211 ~SurfaceFrame() = default;
212
Adithya Srinivasan8fc601d2020-09-25 13:51:09 -0700213 TimelineItem getPredictions() const override { return mPredictions; };
214 TimelineItem getActuals() const override;
215 nsecs_t getActualQueueTime() const override;
216 PresentState getPresentState() const override;
217 PredictionState getPredictionState() const override { return mPredictionState; };
Adithya Srinivasan5f683cf2020-09-15 14:21:04 -0700218
219 void setActualStartTime(nsecs_t actualStartTime) override;
220 void setActualQueueTime(nsecs_t actualQueueTime) override;
Ady Abraham7f8a1e62020-09-28 16:09:35 -0700221 void setAcquireFenceTime(nsecs_t acquireFenceTime) override;
Adithya Srinivasanf279e042020-08-17 14:56:27 -0700222 void setPresentState(PresentState state) override;
Adithya Srinivasan5f683cf2020-09-15 14:21:04 -0700223 void setActualPresentTime(nsecs_t presentTime);
Adithya Srinivasan8fc601d2020-09-25 13:51:09 -0700224 void setJankInfo(JankType jankType, int32_t jankMetadata);
225 JankType getJankType() const;
226 nsecs_t getBaseTime() const;
227 // All the timestamps are dumped relative to the baseTime
228 void dump(std::string& result, const std::string& indent, nsecs_t baseTime);
Adithya Srinivasanf279e042020-08-17 14:56:27 -0700229
230private:
231 const std::string mLayerName;
232 PresentState mPresentState GUARDED_BY(mMutex);
Adithya Srinivasan8fc601d2020-09-25 13:51:09 -0700233 const PredictionState mPredictionState;
Adithya Srinivasanf279e042020-08-17 14:56:27 -0700234 const TimelineItem mPredictions;
235 TimelineItem mActuals GUARDED_BY(mMutex);
Adithya Srinivasan8fc601d2020-09-25 13:51:09 -0700236 nsecs_t mActualQueueTime GUARDED_BY(mMutex);
237 mutable std::mutex mMutex;
238 JankType mJankType GUARDED_BY(mMutex); // Enum for the type of jank
239 int32_t mJankMetadata GUARDED_BY(mMutex); // Additional details about the jank
Adithya Srinivasanf279e042020-08-17 14:56:27 -0700240};
241
242class FrameTimeline : public android::frametimeline::FrameTimeline {
243public:
244 FrameTimeline();
245 ~FrameTimeline() = default;
246
Adithya Srinivasan5f683cf2020-09-15 14:21:04 -0700247 frametimeline::TokenManager* getTokenManager() override { return &mTokenManager; }
Adithya Srinivasanf279e042020-08-17 14:56:27 -0700248 std::unique_ptr<frametimeline::SurfaceFrame> createSurfaceFrameForToken(
249 const std::string& layerName, std::optional<int64_t> token) override;
250 void addSurfaceFrame(std::unique_ptr<frametimeline::SurfaceFrame> surfaceFrame,
251 SurfaceFrame::PresentState state) override;
252 void setSfWakeUp(int64_t token, nsecs_t wakeupTime) override;
253 void setSfPresent(nsecs_t sfPresentTime,
254 const std::shared_ptr<FenceTime>& presentFence) override;
Adithya Srinivasan8fc601d2020-09-25 13:51:09 -0700255 void parseArgs(const Vector<String16>& args, std::string& result) override;
Adithya Srinivasan2d736322020-10-01 16:53:48 -0700256 void setMaxDisplayFrames(uint32_t size) override;
257 void reset() override;
Adithya Srinivasanf279e042020-08-17 14:56:27 -0700258
259private:
260 // Friend class for testing
261 friend class android::frametimeline::FrameTimelineTest;
262
263 /*
264 * DisplayFrame should be used only internally within FrameTimeline.
265 */
266 struct DisplayFrame {
267 DisplayFrame();
268
269 /* Usage of TimelineItem w.r.t SurfaceFlinger
270 * startTime Time when SurfaceFlinger wakes up to handle transactions and buffer updates
271 * endTime Time when SurfaceFlinger sends a composited frame to Display
272 * presentTime Time when the composited frame was presented on screen
273 */
274 TimelineItem surfaceFlingerPredictions;
275 TimelineItem surfaceFlingerActuals;
276
277 // Collection of predictions and actual values sent over by Layers
278 std::vector<std::unique_ptr<SurfaceFrame>> surfaceFrames;
279
280 PredictionState predictionState;
Adithya Srinivasan8fc601d2020-09-25 13:51:09 -0700281 JankType jankType = JankType::None; // Enum for the type of jank
282 int32_t jankMetadata = 0x0; // Additional details about the jank
Adithya Srinivasanf279e042020-08-17 14:56:27 -0700283 };
284
285 void flushPendingPresentFences() REQUIRES(mMutex);
286 void finalizeCurrentDisplayFrame() REQUIRES(mMutex);
Adithya Srinivasan8fc601d2020-09-25 13:51:09 -0700287 // BaseTime is the smallest timestamp in a DisplayFrame.
288 // Used for dumping all timestamps relative to the oldest, making it easy to read.
289 nsecs_t findBaseTime(const std::shared_ptr<DisplayFrame>&) REQUIRES(mMutex);
290 void dumpDisplayFrame(std::string& result, const std::shared_ptr<DisplayFrame>&,
291 nsecs_t baseTime) REQUIRES(mMutex);
292 void dumpAll(std::string& result);
293 void dumpJank(std::string& result);
Adithya Srinivasanf279e042020-08-17 14:56:27 -0700294
295 // Sliding window of display frames. TODO(b/168072834): compare perf with fixed size array
296 std::deque<std::shared_ptr<DisplayFrame>> mDisplayFrames GUARDED_BY(mMutex);
297 std::vector<std::pair<std::shared_ptr<FenceTime>, std::shared_ptr<DisplayFrame>>>
298 mPendingPresentFences GUARDED_BY(mMutex);
299 std::shared_ptr<DisplayFrame> mCurrentDisplayFrame GUARDED_BY(mMutex);
300 TokenManager mTokenManager;
301 std::mutex mMutex;
Adithya Srinivasan2d736322020-10-01 16:53:48 -0700302 uint32_t mMaxDisplayFrames;
303 static constexpr uint32_t kDefaultMaxDisplayFrames = 64;
Adithya Srinivasanf279e042020-08-17 14:56:27 -0700304 // The initial container size for the vector<SurfaceFrames> inside display frame. Although this
305 // number doesn't represent any bounds on the number of surface frames that can go in a display
306 // frame, this is a good starting size for the vector so that we can avoid the internal vector
307 // resizing that happens with push_back.
308 static constexpr uint32_t kNumSurfaceFramesInitial = 10;
Adithya Srinivasan8fc601d2020-09-25 13:51:09 -0700309 // The various thresholds for App and SF. If the actual timestamp falls within the threshold
310 // compared to prediction, we don't treat it as a jank.
311 static constexpr nsecs_t kPresentThreshold =
312 std::chrono::duration_cast<std::chrono::nanoseconds>(2ms).count();
313 static constexpr nsecs_t kDeadlineThreshold =
314 std::chrono::duration_cast<std::chrono::nanoseconds>(2ms).count();
315 static constexpr nsecs_t kSFStartThreshold =
316 std::chrono::duration_cast<std::chrono::nanoseconds>(1ms).count();
Adithya Srinivasanf279e042020-08-17 14:56:27 -0700317};
318
319} // namespace impl
320} // namespace android::frametimeline