blob: 54e8efbc92dcd3aaf3cf94f3751459a782d3e56e [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
Alec Mouri9a29e672020-09-14 12:39:14 -070019#include <../TimeStats/TimeStats.h>
Ady Abraham22c7b5c2020-09-22 19:33:40 -070020#include <gui/ISurfaceComposer.h>
Jorim Jaggi5814ab82020-12-03 20:45:58 +010021#include <gui/JankInfo.h>
Adithya Srinivasan01189672020-10-20 14:23:05 -070022#include <perfetto/trace/android/frame_timeline_event.pbzero.h>
23#include <perfetto/tracing.h>
Adithya Srinivasanf279e042020-08-17 14:56:27 -070024#include <ui/FenceTime.h>
25#include <utils/RefBase.h>
Adithya Srinivasan8fc601d2020-09-25 13:51:09 -070026#include <utils/String16.h>
Adithya Srinivasanf279e042020-08-17 14:56:27 -070027#include <utils/Timers.h>
Adithya Srinivasan8fc601d2020-09-25 13:51:09 -070028#include <utils/Vector.h>
Adithya Srinivasanf279e042020-08-17 14:56:27 -070029
Alec Mouri9a29e672020-09-14 12:39:14 -070030#include <deque>
31#include <mutex>
Adithya Srinivasanf279e042020-08-17 14:56:27 -070032
Alec Mouri9a29e672020-09-14 12:39:14 -070033namespace android::frametimeline {
Adithya Srinivasan8fc601d2020-09-25 13:51:09 -070034
Adithya Srinivasan9b2ca3e2020-11-10 10:14:17 -080035class FrameTimelineTest;
36
37using namespace std::chrono_literals;
38
39// Metadata indicating how the frame was presented w.r.t expected present time.
40enum class FramePresentMetadata : int8_t {
41 // Frame was presented on time
42 OnTimePresent,
43 // Frame was presented late
44 LatePresent,
45 // Frame was presented early
46 EarlyPresent,
47 // Unknown/initial state
48 UnknownPresent,
Adithya Srinivasan8fc601d2020-09-25 13:51:09 -070049};
50
Adithya Srinivasan9b2ca3e2020-11-10 10:14:17 -080051// Metadata comparing the frame's actual finish time to the expected deadline.
52enum class FrameReadyMetadata : int8_t {
53 // App/SF finished on time. Early finish is treated as on time since the goal of any component
54 // is to finish before the deadline.
55 OnTimeFinish,
56 // App/SF finished work later than expected
57 LateFinish,
58 // Unknown/initial state
59 UnknownFinish,
60};
61
62// Metadata comparing the frame's actual start time to the expected start time.
63enum class FrameStartMetadata : int8_t {
64 // App/SF started on time
65 OnTimeStart,
66 // App/SF started later than expected
67 LateStart,
68 // App/SF started earlier than expected
69 EarlyStart,
70 // Unknown/initial state
71 UnknownStart,
72};
Adithya Srinivasanf279e042020-08-17 14:56:27 -070073
74/*
75 * Collection of timestamps that can be used for both predictions and actual times.
76 */
77struct TimelineItem {
78 TimelineItem(const nsecs_t startTime = 0, const nsecs_t endTime = 0,
79 const nsecs_t presentTime = 0)
80 : startTime(startTime), endTime(endTime), presentTime(presentTime) {}
81
82 nsecs_t startTime;
83 nsecs_t endTime;
84 nsecs_t presentTime;
Ady Abraham55fa7272020-09-30 19:19:27 -070085
86 bool operator==(const TimelineItem& other) const {
87 return startTime == other.startTime && endTime == other.endTime &&
88 presentTime == other.presentTime;
89 }
90
91 bool operator!=(const TimelineItem& other) const { return !(*this == other); }
Adithya Srinivasanf279e042020-08-17 14:56:27 -070092};
93
Adithya Srinivasan9b2ca3e2020-11-10 10:14:17 -080094struct TokenManagerPrediction {
95 nsecs_t timestamp = 0;
96 TimelineItem predictions;
97};
98
99struct JankClassificationThresholds {
100 // The various thresholds for App and SF. If the actual timestamp falls within the threshold
101 // compared to prediction, we treat it as on time.
102 nsecs_t presentThreshold = std::chrono::duration_cast<std::chrono::nanoseconds>(2ms).count();
103 nsecs_t deadlineThreshold = std::chrono::duration_cast<std::chrono::nanoseconds>(2ms).count();
104 nsecs_t startThreshold = std::chrono::duration_cast<std::chrono::nanoseconds>(2ms).count();
105};
106
Adithya Srinivasanf279e042020-08-17 14:56:27 -0700107/*
108 * TokenManager generates a running number token for a set of predictions made by VsyncPredictor. It
109 * saves these predictions for a short period of time and returns the predictions for a given token,
110 * if it hasn't expired.
111 */
112class TokenManager {
113public:
114 virtual ~TokenManager() = default;
115
116 // Generates a token for the given set of predictions. Stores the predictions for 120ms and
117 // destroys it later.
Adithya Srinivasan5f683cf2020-09-15 14:21:04 -0700118 virtual int64_t generateTokenForPredictions(TimelineItem&& prediction) = 0;
Adithya Srinivasan9b2ca3e2020-11-10 10:14:17 -0800119
120 // Returns the stored predictions for a given token, if the predictions haven't expired.
121 virtual std::optional<TimelineItem> getPredictionsForToken(int64_t token) const = 0;
Adithya Srinivasanf279e042020-08-17 14:56:27 -0700122};
123
124enum class PredictionState {
125 Valid, // Predictions obtained successfully from the TokenManager
126 Expired, // TokenManager no longer has the predictions
127 None, // Predictions are either not present or didn't come from TokenManager
128};
129
Adithya Srinivasan05bd2d12021-01-11 18:49:58 +0000130/*
131 * Trace cookie is used to send start and end timestamps of <Surface/Display>Frames separately
132 * without needing to resend all the other information. We send all info to perfetto, along with a
133 * new cookie, in the start of a frame. For the corresponding end, we just send the same cookie.
134 * This helps in reducing the amount of data emitted by the producer.
135 */
136class TraceCookieCounter {
137public:
138 int64_t getCookieForTracing();
139
140private:
141 // Friend class for testing
142 friend class android::frametimeline::FrameTimelineTest;
143
144 std::atomic<int64_t> mTraceCookie = 0;
145};
146
Adithya Srinivasanf279e042020-08-17 14:56:27 -0700147class SurfaceFrame {
148public:
149 enum class PresentState {
150 Presented, // Buffer was latched and presented by SurfaceFlinger
151 Dropped, // Buffer was dropped by SurfaceFlinger
152 Unknown, // Initial state, SurfaceFlinger hasn't seen this buffer yet
153 };
154
Adithya Srinivasan9b2ca3e2020-11-10 10:14:17 -0800155 // Only FrameTimeline can construct a SurfaceFrame as it provides Predictions(through
156 // TokenManager), Thresholds and TimeStats pointer.
Siarhei Vishniakoufc434ac2021-01-13 10:28:00 -1000157 SurfaceFrame(const FrameTimelineInfo& frameTimelineInfo, pid_t ownerPid, uid_t ownerUid,
158 std::string layerName, std::string debugName, PredictionState predictionState,
159 TimelineItem&& predictions, std::shared_ptr<TimeStats> timeStats,
160 JankClassificationThresholds thresholds, TraceCookieCounter* traceCookieCounter);
Adithya Srinivasan9b2ca3e2020-11-10 10:14:17 -0800161 ~SurfaceFrame() = default;
Adithya Srinivasanf279e042020-08-17 14:56:27 -0700162
Adithya Srinivasan9b2ca3e2020-11-10 10:14:17 -0800163 // Returns std::nullopt if the frame hasn't been classified yet.
164 // Used by both SF and FrameTimeline.
165 std::optional<int32_t> getJankType() const;
Adithya Srinivasanf279e042020-08-17 14:56:27 -0700166
Adithya Srinivasan9b2ca3e2020-11-10 10:14:17 -0800167 // Functions called by SF
168 int64_t getToken() const { return mToken; };
Siarhei Vishniakoufc434ac2021-01-13 10:28:00 -1000169 int32_t getInputEventId() const { return mInputEventId; };
Adithya Srinivasan9b2ca3e2020-11-10 10:14:17 -0800170 TimelineItem getPredictions() const { return mPredictions; };
Adithya Srinivasan5f683cf2020-09-15 14:21:04 -0700171 // Actual timestamps of the app are set individually at different functions.
172 // Start time (if the app provides) and Queue time are accessible after queueing the frame,
Ady Abraham7f8a1e62020-09-28 16:09:35 -0700173 // whereas Acquire Fence time is available only during latch.
Adithya Srinivasan9b2ca3e2020-11-10 10:14:17 -0800174 void setActualStartTime(nsecs_t actualStartTime);
175 void setActualQueueTime(nsecs_t actualQueueTime);
176 void setAcquireFenceTime(nsecs_t acquireFenceTime);
177 void setPresentState(PresentState presentState, nsecs_t lastLatchTime = 0);
Jorim Jaggi9c03b502020-11-24 23:51:31 +0100178
Adithya Srinivasan9b2ca3e2020-11-10 10:14:17 -0800179 // Functions called by FrameTimeline
180 // BaseTime is the smallest timestamp in this SurfaceFrame.
181 // Used for dumping all timestamps relative to the oldest, making it easy to read.
182 nsecs_t getBaseTime() const;
183 // Sets the actual present time, appropriate metadata and classifies the jank.
184 void onPresent(nsecs_t presentTime, int32_t displayFrameJankType, nsecs_t vsyncPeriod);
185 // All the timestamps are dumped relative to the baseTime
186 void dump(std::string& result, const std::string& indent, nsecs_t baseTime) const;
187 // Emits a packet for perfetto tracing. The function body will be executed only if tracing is
188 // enabled. The displayFrameToken is needed to link the SurfaceFrame to the corresponding
189 // DisplayFrame at the trace processor side.
190 void trace(int64_t displayFrameToken);
Jorim Jaggi9c03b502020-11-24 23:51:31 +0100191
Adithya Srinivasan9b2ca3e2020-11-10 10:14:17 -0800192 // Getter functions used only by FrameTimelineTests
193 TimelineItem getActuals() const;
194 pid_t getOwnerPid() const { return mOwnerPid; };
195 PredictionState getPredictionState() const { return mPredictionState; };
196 PresentState getPresentState() const;
197 FrameReadyMetadata getFrameReadyMetadata() const;
198 FramePresentMetadata getFramePresentMetadata() const;
199
200private:
201 const int64_t mToken;
Siarhei Vishniakoufc434ac2021-01-13 10:28:00 -1000202 const int32_t mInputEventId;
Adithya Srinivasan9b2ca3e2020-11-10 10:14:17 -0800203 const pid_t mOwnerPid;
204 const uid_t mOwnerUid;
205 const std::string mLayerName;
206 const std::string mDebugName;
207 PresentState mPresentState GUARDED_BY(mMutex);
208 const PredictionState mPredictionState;
209 const TimelineItem mPredictions;
210 TimelineItem mActuals GUARDED_BY(mMutex);
211 std::shared_ptr<TimeStats> mTimeStats;
212 const JankClassificationThresholds mJankClassificationThresholds;
213 nsecs_t mActualQueueTime GUARDED_BY(mMutex) = 0;
214 mutable std::mutex mMutex;
215 // Bitmask for the type of jank
216 int32_t mJankType GUARDED_BY(mMutex) = JankType::None;
217 // Indicates if this frame was composited by the GPU or not
218 bool mGpuComposition GUARDED_BY(mMutex) = false;
219 // Enum for the type of present
220 FramePresentMetadata mFramePresentMetadata GUARDED_BY(mMutex) =
221 FramePresentMetadata::UnknownPresent;
222 // Enum for the type of finish
223 FrameReadyMetadata mFrameReadyMetadata GUARDED_BY(mMutex) = FrameReadyMetadata::UnknownFinish;
224 // Time when the previous buffer from the same layer was latched by SF. This is used in checking
225 // for BufferStuffing where the current buffer is expected to be ready but the previous buffer
226 // was latched instead.
227 nsecs_t mLastLatchTime GUARDED_BY(mMutex) = 0;
Adithya Srinivasan05bd2d12021-01-11 18:49:58 +0000228 // TraceCookieCounter is used to obtain the cookie for sendig trace packets to perfetto. Using a
229 // reference here because the counter is owned by FrameTimeline, which outlives SurfaceFrame.
230 TraceCookieCounter& mTraceCookieCounter;
Adithya Srinivasanf279e042020-08-17 14:56:27 -0700231};
232
233/*
234 * Maintains a history of SurfaceFrames grouped together by the vsync time in which they were
235 * presented
236 */
237class FrameTimeline {
238public:
239 virtual ~FrameTimeline() = default;
Adithya Srinivasan5f683cf2020-09-15 14:21:04 -0700240 virtual TokenManager* getTokenManager() = 0;
Adithya Srinivasanf279e042020-08-17 14:56:27 -0700241
Adithya Srinivasan01189672020-10-20 14:23:05 -0700242 // Initializes the Perfetto DataSource that emits DisplayFrame and SurfaceFrame events. Test
243 // classes can avoid double registration by mocking this function.
244 virtual void onBootFinished() = 0;
245
Adithya Srinivasanf279e042020-08-17 14:56:27 -0700246 // Create a new surface frame, set the predictions based on a token and return it to the caller.
Alec Mouri9a29e672020-09-14 12:39:14 -0700247 // Debug name is the human-readable debugging string for dumpsys.
Siarhei Vishniakoufc434ac2021-01-13 10:28:00 -1000248 virtual std::shared_ptr<SurfaceFrame> createSurfaceFrameForToken(
249 const FrameTimelineInfo& frameTimelineInfo, pid_t ownerPid, uid_t ownerUid,
250 std::string layerName, std::string debugName) = 0;
Adithya Srinivasanf279e042020-08-17 14:56:27 -0700251
252 // Adds a new SurfaceFrame to the current DisplayFrame. Frames from multiple layers can be
253 // composited into one display frame.
Adithya Srinivasan9b2ca3e2020-11-10 10:14:17 -0800254 virtual void addSurfaceFrame(std::shared_ptr<SurfaceFrame> surfaceFrame) = 0;
Adithya Srinivasanf279e042020-08-17 14:56:27 -0700255
256 // The first function called by SF for the current DisplayFrame. Fetches SF predictions based on
257 // the token and sets the actualSfWakeTime for the current DisplayFrame.
Adithya Srinivasan9b2ca3e2020-11-10 10:14:17 -0800258 virtual void setSfWakeUp(int64_t token, nsecs_t wakeupTime, nsecs_t vsyncPeriod) = 0;
Adithya Srinivasanf279e042020-08-17 14:56:27 -0700259
260 // Sets the sfPresentTime and finalizes the current DisplayFrame. Tracks the given present fence
261 // until it's signaled, and updates the present timestamps of all presented SurfaceFrames in
262 // that vsync.
263 virtual void setSfPresent(nsecs_t sfPresentTime,
264 const std::shared_ptr<FenceTime>& presentFence) = 0;
Adithya Srinivasan5f683cf2020-09-15 14:21:04 -0700265
Adithya Srinivasan8fc601d2020-09-25 13:51:09 -0700266 // Args:
267 // -jank : Dumps only the Display Frames that are either janky themselves
268 // or contain janky Surface Frames.
269 // -all : Dumps the entire list of DisplayFrames and the SurfaceFrames contained within
270 virtual void parseArgs(const Vector<String16>& args, std::string& result) = 0;
Adithya Srinivasan2d736322020-10-01 16:53:48 -0700271
272 // Sets the max number of display frames that can be stored. Called by SF backdoor.
273 virtual void setMaxDisplayFrames(uint32_t size);
274
275 // Restores the max number of display frames to default. Called by SF backdoor.
276 virtual void reset() = 0;
Adithya Srinivasanf279e042020-08-17 14:56:27 -0700277};
278
279namespace impl {
280
Adithya Srinivasanf279e042020-08-17 14:56:27 -0700281class TokenManager : public android::frametimeline::TokenManager {
282public:
Siarhei Vishniakoufc434ac2021-01-13 10:28:00 -1000283 TokenManager() : mCurrentToken(FrameTimelineInfo::INVALID_VSYNC_ID + 1) {}
Adithya Srinivasanf279e042020-08-17 14:56:27 -0700284 ~TokenManager() = default;
285
286 int64_t generateTokenForPredictions(TimelineItem&& predictions) override;
Adithya Srinivasan9b2ca3e2020-11-10 10:14:17 -0800287 std::optional<TimelineItem> getPredictionsForToken(int64_t token) const override;
Adithya Srinivasanf279e042020-08-17 14:56:27 -0700288
289private:
290 // Friend class for testing
291 friend class android::frametimeline::FrameTimelineTest;
292
293 void flushTokens(nsecs_t flushTime) REQUIRES(mMutex);
294
Adithya Srinivasan9b2ca3e2020-11-10 10:14:17 -0800295 std::map<int64_t, TokenManagerPrediction> mPredictions GUARDED_BY(mMutex);
Adithya Srinivasanf279e042020-08-17 14:56:27 -0700296 int64_t mCurrentToken GUARDED_BY(mMutex);
Adithya Srinivasan9b2ca3e2020-11-10 10:14:17 -0800297 mutable std::mutex mMutex;
Adithya Srinivasanf279e042020-08-17 14:56:27 -0700298 static constexpr nsecs_t kMaxRetentionTime =
299 std::chrono::duration_cast<std::chrono::nanoseconds>(120ms).count();
300};
301
Adithya Srinivasanf279e042020-08-17 14:56:27 -0700302class FrameTimeline : public android::frametimeline::FrameTimeline {
303public:
Adithya Srinivasan01189672020-10-20 14:23:05 -0700304 class FrameTimelineDataSource : public perfetto::DataSource<FrameTimelineDataSource> {
305 void OnSetup(const SetupArgs&) override{};
306 void OnStart(const StartArgs&) override{};
307 void OnStop(const StopArgs&) override{};
308 };
309
Adithya Srinivasan9b2ca3e2020-11-10 10:14:17 -0800310 /*
311 * DisplayFrame should be used only internally within FrameTimeline. All members and methods are
312 * guarded by FrameTimeline's mMutex.
313 */
314 class DisplayFrame {
315 public:
Adithya Srinivasan05bd2d12021-01-11 18:49:58 +0000316 DisplayFrame(std::shared_ptr<TimeStats> timeStats, JankClassificationThresholds thresholds,
317 TraceCookieCounter* traceCookieCounter);
Adithya Srinivasan9b2ca3e2020-11-10 10:14:17 -0800318 virtual ~DisplayFrame() = default;
319 // Dumpsys interface - dumps only if the DisplayFrame itself is janky or is at least one
320 // SurfaceFrame is janky.
321 void dumpJank(std::string& result, nsecs_t baseTime, int displayFrameCount) const;
322 // Dumpsys interface - dumps all data irrespective of jank
323 void dumpAll(std::string& result, nsecs_t baseTime) const;
324 // Emits a packet for perfetto tracing. The function body will be executed only if tracing
325 // is enabled.
326 void trace(pid_t surfaceFlingerPid) const;
327 // Sets the token, vsyncPeriod, predictions and SF start time.
328 void onSfWakeUp(int64_t token, nsecs_t vsyncPeriod, std::optional<TimelineItem> predictions,
329 nsecs_t wakeUpTime);
330 // Sets the appropriate metadata, classifies the jank and returns the classified jankType.
331 void onPresent(nsecs_t signalTime);
332 // Adds the provided SurfaceFrame to the current display frame.
333 void addSurfaceFrame(std::shared_ptr<SurfaceFrame> surfaceFrame);
334
335 void setTokenAndVsyncPeriod(int64_t token, nsecs_t vsyncPeriod);
336 void setPredictions(PredictionState predictionState, TimelineItem predictions);
337 void setActualStartTime(nsecs_t actualStartTime);
338 void setActualEndTime(nsecs_t actualEndTime);
339
340 // BaseTime is the smallest timestamp in a DisplayFrame.
341 // Used for dumping all timestamps relative to the oldest, making it easy to read.
342 nsecs_t getBaseTime() const;
343
344 // Functions to be used only in testing.
345 TimelineItem getActuals() const { return mSurfaceFlingerActuals; };
346 TimelineItem getPredictions() const { return mSurfaceFlingerPredictions; };
347 FramePresentMetadata getFramePresentMetadata() const { return mFramePresentMetadata; };
348 FrameReadyMetadata getFrameReadyMetadata() const { return mFrameReadyMetadata; };
349 int32_t getJankType() const { return mJankType; }
350 const std::vector<std::shared_ptr<SurfaceFrame>>& getSurfaceFrames() const {
351 return mSurfaceFrames;
352 }
353
354 private:
355 void dump(std::string& result, nsecs_t baseTime) const;
356
Siarhei Vishniakoufc434ac2021-01-13 10:28:00 -1000357 int64_t mToken = FrameTimelineInfo::INVALID_VSYNC_ID;
Adithya Srinivasan9b2ca3e2020-11-10 10:14:17 -0800358
359 /* Usage of TimelineItem w.r.t SurfaceFlinger
360 * startTime Time when SurfaceFlinger wakes up to handle transactions and buffer updates
361 * endTime Time when SurfaceFlinger sends a composited frame to Display
362 * presentTime Time when the composited frame was presented on screen
363 */
364 TimelineItem mSurfaceFlingerPredictions;
365 TimelineItem mSurfaceFlingerActuals;
366 std::shared_ptr<TimeStats> mTimeStats;
367 const JankClassificationThresholds mJankClassificationThresholds;
368
369 // Collection of predictions and actual values sent over by Layers
370 std::vector<std::shared_ptr<SurfaceFrame>> mSurfaceFrames;
371
372 PredictionState mPredictionState = PredictionState::None;
373 // Bitmask for the type of jank
374 int32_t mJankType = JankType::None;
375 // Indicates if this frame was composited by the GPU or not
376 bool mGpuComposition = false;
377 // Enum for the type of present
378 FramePresentMetadata mFramePresentMetadata = FramePresentMetadata::UnknownPresent;
379 // Enum for the type of finish
380 FrameReadyMetadata mFrameReadyMetadata = FrameReadyMetadata::UnknownFinish;
381 // Enum for the type of start
382 FrameStartMetadata mFrameStartMetadata = FrameStartMetadata::UnknownStart;
383 // The refresh rate (vsync period) in nanoseconds as seen by SF during this DisplayFrame's
384 // timeline
385 nsecs_t mVsyncPeriod = 0;
Adithya Srinivasan05bd2d12021-01-11 18:49:58 +0000386 // TraceCookieCounter is used to obtain the cookie for sendig trace packets to perfetto.
387 // Using a reference here because the counter is owned by FrameTimeline, which outlives
388 // DisplayFrame.
389 TraceCookieCounter& mTraceCookieCounter;
Adithya Srinivasan9b2ca3e2020-11-10 10:14:17 -0800390 };
391
392 FrameTimeline(std::shared_ptr<TimeStats> timeStats, pid_t surfaceFlingerPid,
393 JankClassificationThresholds thresholds = {});
Adithya Srinivasanf279e042020-08-17 14:56:27 -0700394 ~FrameTimeline() = default;
395
Adithya Srinivasan5f683cf2020-09-15 14:21:04 -0700396 frametimeline::TokenManager* getTokenManager() override { return &mTokenManager; }
Siarhei Vishniakoufc434ac2021-01-13 10:28:00 -1000397 std::shared_ptr<SurfaceFrame> createSurfaceFrameForToken(
398 const FrameTimelineInfo& frameTimelineInfo, pid_t ownerPid, uid_t ownerUid,
399 std::string layerName, std::string debugName) override;
Adithya Srinivasan9b2ca3e2020-11-10 10:14:17 -0800400 void addSurfaceFrame(std::shared_ptr<frametimeline::SurfaceFrame> surfaceFrame) override;
401 void setSfWakeUp(int64_t token, nsecs_t wakeupTime, nsecs_t vsyncPeriod) override;
Adithya Srinivasanf279e042020-08-17 14:56:27 -0700402 void setSfPresent(nsecs_t sfPresentTime,
403 const std::shared_ptr<FenceTime>& presentFence) override;
Adithya Srinivasan8fc601d2020-09-25 13:51:09 -0700404 void parseArgs(const Vector<String16>& args, std::string& result) override;
Adithya Srinivasan2d736322020-10-01 16:53:48 -0700405 void setMaxDisplayFrames(uint32_t size) override;
406 void reset() override;
Adithya Srinivasanf279e042020-08-17 14:56:27 -0700407
Adithya Srinivasan01189672020-10-20 14:23:05 -0700408 // Sets up the perfetto tracing backend and data source.
409 void onBootFinished() override;
410 // Registers the data source with the perfetto backend. Called as part of onBootFinished()
411 // and should not be called manually outside of tests.
412 void registerDataSource();
413
414 static constexpr char kFrameTimelineDataSource[] = "android.surfaceflinger.frametimeline";
415
Adithya Srinivasanf279e042020-08-17 14:56:27 -0700416private:
417 // Friend class for testing
418 friend class android::frametimeline::FrameTimelineTest;
419
Adithya Srinivasanf279e042020-08-17 14:56:27 -0700420 void flushPendingPresentFences() REQUIRES(mMutex);
421 void finalizeCurrentDisplayFrame() REQUIRES(mMutex);
Adithya Srinivasan8fc601d2020-09-25 13:51:09 -0700422 void dumpAll(std::string& result);
423 void dumpJank(std::string& result);
Adithya Srinivasanf279e042020-08-17 14:56:27 -0700424
425 // Sliding window of display frames. TODO(b/168072834): compare perf with fixed size array
426 std::deque<std::shared_ptr<DisplayFrame>> mDisplayFrames GUARDED_BY(mMutex);
427 std::vector<std::pair<std::shared_ptr<FenceTime>, std::shared_ptr<DisplayFrame>>>
428 mPendingPresentFences GUARDED_BY(mMutex);
429 std::shared_ptr<DisplayFrame> mCurrentDisplayFrame GUARDED_BY(mMutex);
430 TokenManager mTokenManager;
Adithya Srinivasan05bd2d12021-01-11 18:49:58 +0000431 TraceCookieCounter mTraceCookieCounter;
Adithya Srinivasan9b2ca3e2020-11-10 10:14:17 -0800432 mutable std::mutex mMutex;
Adithya Srinivasan2d736322020-10-01 16:53:48 -0700433 uint32_t mMaxDisplayFrames;
Alec Mouri9a29e672020-09-14 12:39:14 -0700434 std::shared_ptr<TimeStats> mTimeStats;
Adithya Srinivasan9b2ca3e2020-11-10 10:14:17 -0800435 const pid_t mSurfaceFlingerPid;
436 const JankClassificationThresholds mJankClassificationThresholds;
Adithya Srinivasan2d736322020-10-01 16:53:48 -0700437 static constexpr uint32_t kDefaultMaxDisplayFrames = 64;
Adithya Srinivasan01189672020-10-20 14:23:05 -0700438 // The initial container size for the vector<SurfaceFrames> inside display frame. Although
439 // this number doesn't represent any bounds on the number of surface frames that can go in a
440 // display frame, this is a good starting size for the vector so that we can avoid the
441 // internal vector resizing that happens with push_back.
Adithya Srinivasanf279e042020-08-17 14:56:27 -0700442 static constexpr uint32_t kNumSurfaceFramesInitial = 10;
Adithya Srinivasanf279e042020-08-17 14:56:27 -0700443};
444
445} // namespace impl
446} // namespace android::frametimeline