blob: 4739106fdfe236f4334c7c934d411bc6624889df [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 Mouri7d436ec2021-01-27 20:40:50 -080019#include <../Fps.h>
Alec Mouri9a29e672020-09-14 12:39:14 -070020#include <../TimeStats/TimeStats.h>
Ady Abraham22c7b5c2020-09-22 19:33:40 -070021#include <gui/ISurfaceComposer.h>
Jorim Jaggi5814ab82020-12-03 20:45:58 +010022#include <gui/JankInfo.h>
Adithya Srinivasan01189672020-10-20 14:23:05 -070023#include <perfetto/trace/android/frame_timeline_event.pbzero.h>
24#include <perfetto/tracing.h>
Adithya Srinivasanf279e042020-08-17 14:56:27 -070025#include <ui/FenceTime.h>
26#include <utils/RefBase.h>
Adithya Srinivasan8fc601d2020-09-25 13:51:09 -070027#include <utils/String16.h>
Adithya Srinivasanf279e042020-08-17 14:56:27 -070028#include <utils/Timers.h>
Adithya Srinivasan8fc601d2020-09-25 13:51:09 -070029#include <utils/Vector.h>
Adithya Srinivasanf279e042020-08-17 14:56:27 -070030
Alec Mouri9a29e672020-09-14 12:39:14 -070031#include <deque>
32#include <mutex>
Adithya Srinivasanf279e042020-08-17 14:56:27 -070033
Alec Mouri9a29e672020-09-14 12:39:14 -070034namespace android::frametimeline {
Adithya Srinivasan8fc601d2020-09-25 13:51:09 -070035
Adithya Srinivasan9b2ca3e2020-11-10 10:14:17 -080036class FrameTimelineTest;
37
38using namespace std::chrono_literals;
39
40// Metadata indicating how the frame was presented w.r.t expected present time.
41enum class FramePresentMetadata : int8_t {
42 // Frame was presented on time
43 OnTimePresent,
44 // Frame was presented late
45 LatePresent,
46 // Frame was presented early
47 EarlyPresent,
48 // Unknown/initial state
49 UnknownPresent,
Adithya Srinivasan8fc601d2020-09-25 13:51:09 -070050};
51
Adithya Srinivasan9b2ca3e2020-11-10 10:14:17 -080052// Metadata comparing the frame's actual finish time to the expected deadline.
53enum class FrameReadyMetadata : int8_t {
54 // App/SF finished on time. Early finish is treated as on time since the goal of any component
55 // is to finish before the deadline.
56 OnTimeFinish,
57 // App/SF finished work later than expected
58 LateFinish,
59 // Unknown/initial state
60 UnknownFinish,
61};
62
63// Metadata comparing the frame's actual start time to the expected start time.
64enum class FrameStartMetadata : int8_t {
65 // App/SF started on time
66 OnTimeStart,
67 // App/SF started later than expected
68 LateStart,
69 // App/SF started earlier than expected
70 EarlyStart,
71 // Unknown/initial state
72 UnknownStart,
73};
Adithya Srinivasanf279e042020-08-17 14:56:27 -070074
75/*
76 * Collection of timestamps that can be used for both predictions and actual times.
77 */
78struct TimelineItem {
79 TimelineItem(const nsecs_t startTime = 0, const nsecs_t endTime = 0,
80 const nsecs_t presentTime = 0)
81 : startTime(startTime), endTime(endTime), presentTime(presentTime) {}
82
83 nsecs_t startTime;
84 nsecs_t endTime;
85 nsecs_t presentTime;
Ady Abraham55fa7272020-09-30 19:19:27 -070086
87 bool operator==(const TimelineItem& other) const {
88 return startTime == other.startTime && endTime == other.endTime &&
89 presentTime == other.presentTime;
90 }
91
92 bool operator!=(const TimelineItem& other) const { return !(*this == other); }
Adithya Srinivasanf279e042020-08-17 14:56:27 -070093};
94
Adithya Srinivasan9b2ca3e2020-11-10 10:14:17 -080095struct TokenManagerPrediction {
96 nsecs_t timestamp = 0;
97 TimelineItem predictions;
98};
99
100struct JankClassificationThresholds {
101 // The various thresholds for App and SF. If the actual timestamp falls within the threshold
102 // compared to prediction, we treat it as on time.
103 nsecs_t presentThreshold = std::chrono::duration_cast<std::chrono::nanoseconds>(2ms).count();
104 nsecs_t deadlineThreshold = std::chrono::duration_cast<std::chrono::nanoseconds>(2ms).count();
105 nsecs_t startThreshold = std::chrono::duration_cast<std::chrono::nanoseconds>(2ms).count();
106};
107
Adithya Srinivasanf279e042020-08-17 14:56:27 -0700108/*
109 * TokenManager generates a running number token for a set of predictions made by VsyncPredictor. It
110 * saves these predictions for a short period of time and returns the predictions for a given token,
111 * if it hasn't expired.
112 */
113class TokenManager {
114public:
115 virtual ~TokenManager() = default;
116
117 // Generates a token for the given set of predictions. Stores the predictions for 120ms and
118 // destroys it later.
Adithya Srinivasan5f683cf2020-09-15 14:21:04 -0700119 virtual int64_t generateTokenForPredictions(TimelineItem&& prediction) = 0;
Adithya Srinivasan9b2ca3e2020-11-10 10:14:17 -0800120
121 // Returns the stored predictions for a given token, if the predictions haven't expired.
122 virtual std::optional<TimelineItem> getPredictionsForToken(int64_t token) const = 0;
Adithya Srinivasanf279e042020-08-17 14:56:27 -0700123};
124
125enum class PredictionState {
126 Valid, // Predictions obtained successfully from the TokenManager
127 Expired, // TokenManager no longer has the predictions
128 None, // Predictions are either not present or didn't come from TokenManager
129};
130
Adithya Srinivasan05bd2d12021-01-11 18:49:58 +0000131/*
132 * Trace cookie is used to send start and end timestamps of <Surface/Display>Frames separately
133 * without needing to resend all the other information. We send all info to perfetto, along with a
134 * new cookie, in the start of a frame. For the corresponding end, we just send the same cookie.
135 * This helps in reducing the amount of data emitted by the producer.
136 */
137class TraceCookieCounter {
138public:
139 int64_t getCookieForTracing();
140
141private:
142 // Friend class for testing
143 friend class android::frametimeline::FrameTimelineTest;
144
145 std::atomic<int64_t> mTraceCookie = 0;
146};
147
Adithya Srinivasanf279e042020-08-17 14:56:27 -0700148class SurfaceFrame {
149public:
150 enum class PresentState {
151 Presented, // Buffer was latched and presented by SurfaceFlinger
152 Dropped, // Buffer was dropped by SurfaceFlinger
153 Unknown, // Initial state, SurfaceFlinger hasn't seen this buffer yet
154 };
155
Adithya Srinivasan9b2ca3e2020-11-10 10:14:17 -0800156 // Only FrameTimeline can construct a SurfaceFrame as it provides Predictions(through
157 // TokenManager), Thresholds and TimeStats pointer.
Siarhei Vishniakoufc434ac2021-01-13 10:28:00 -1000158 SurfaceFrame(const FrameTimelineInfo& frameTimelineInfo, pid_t ownerPid, uid_t ownerUid,
159 std::string layerName, std::string debugName, PredictionState predictionState,
160 TimelineItem&& predictions, std::shared_ptr<TimeStats> timeStats,
161 JankClassificationThresholds thresholds, TraceCookieCounter* traceCookieCounter);
Adithya Srinivasan9b2ca3e2020-11-10 10:14:17 -0800162 ~SurfaceFrame() = default;
Adithya Srinivasanf279e042020-08-17 14:56:27 -0700163
Adithya Srinivasan9b2ca3e2020-11-10 10:14:17 -0800164 // Returns std::nullopt if the frame hasn't been classified yet.
165 // Used by both SF and FrameTimeline.
166 std::optional<int32_t> getJankType() const;
Adithya Srinivasanf279e042020-08-17 14:56:27 -0700167
Adithya Srinivasan9b2ca3e2020-11-10 10:14:17 -0800168 // Functions called by SF
169 int64_t getToken() const { return mToken; };
Siarhei Vishniakoufc434ac2021-01-13 10:28:00 -1000170 int32_t getInputEventId() const { return mInputEventId; };
Adithya Srinivasan9b2ca3e2020-11-10 10:14:17 -0800171 TimelineItem getPredictions() const { return mPredictions; };
Adithya Srinivasan5f683cf2020-09-15 14:21:04 -0700172 // Actual timestamps of the app are set individually at different functions.
173 // Start time (if the app provides) and Queue time are accessible after queueing the frame,
Ady Abraham7f8a1e62020-09-28 16:09:35 -0700174 // whereas Acquire Fence time is available only during latch.
Adithya Srinivasan9b2ca3e2020-11-10 10:14:17 -0800175 void setActualStartTime(nsecs_t actualStartTime);
176 void setActualQueueTime(nsecs_t actualQueueTime);
177 void setAcquireFenceTime(nsecs_t acquireFenceTime);
178 void setPresentState(PresentState presentState, nsecs_t lastLatchTime = 0);
Alec Mouri7d436ec2021-01-27 20:40:50 -0800179 void setRenderRate(Fps renderRate);
Jorim Jaggi9c03b502020-11-24 23:51:31 +0100180
Adithya Srinivasan9b2ca3e2020-11-10 10:14:17 -0800181 // Functions called by FrameTimeline
182 // BaseTime is the smallest timestamp in this SurfaceFrame.
183 // Used for dumping all timestamps relative to the oldest, making it easy to read.
184 nsecs_t getBaseTime() const;
185 // Sets the actual present time, appropriate metadata and classifies the jank.
Alec Mouri363faf02021-01-29 16:34:55 -0800186 // displayRefreshRate, displayDeadlineDelta, and displayPresentDelta are propagated from the
187 // display frame.
188 void onPresent(nsecs_t presentTime, int32_t displayFrameJankType, Fps refreshRate,
189 nsecs_t displayDeadlineDelta, nsecs_t displayPresentDelta);
Adithya Srinivasan9b2ca3e2020-11-10 10:14:17 -0800190 // All the timestamps are dumped relative to the baseTime
191 void dump(std::string& result, const std::string& indent, nsecs_t baseTime) const;
192 // Emits a packet for perfetto tracing. The function body will be executed only if tracing is
193 // enabled. The displayFrameToken is needed to link the SurfaceFrame to the corresponding
194 // DisplayFrame at the trace processor side.
195 void trace(int64_t displayFrameToken);
Jorim Jaggi9c03b502020-11-24 23:51:31 +0100196
Adithya Srinivasan9b2ca3e2020-11-10 10:14:17 -0800197 // Getter functions used only by FrameTimelineTests
198 TimelineItem getActuals() const;
199 pid_t getOwnerPid() const { return mOwnerPid; };
200 PredictionState getPredictionState() const { return mPredictionState; };
201 PresentState getPresentState() const;
202 FrameReadyMetadata getFrameReadyMetadata() const;
203 FramePresentMetadata getFramePresentMetadata() const;
204
205private:
206 const int64_t mToken;
Siarhei Vishniakoufc434ac2021-01-13 10:28:00 -1000207 const int32_t mInputEventId;
Adithya Srinivasan9b2ca3e2020-11-10 10:14:17 -0800208 const pid_t mOwnerPid;
209 const uid_t mOwnerUid;
210 const std::string mLayerName;
211 const std::string mDebugName;
212 PresentState mPresentState GUARDED_BY(mMutex);
213 const PredictionState mPredictionState;
214 const TimelineItem mPredictions;
215 TimelineItem mActuals GUARDED_BY(mMutex);
216 std::shared_ptr<TimeStats> mTimeStats;
217 const JankClassificationThresholds mJankClassificationThresholds;
218 nsecs_t mActualQueueTime GUARDED_BY(mMutex) = 0;
219 mutable std::mutex mMutex;
220 // Bitmask for the type of jank
221 int32_t mJankType GUARDED_BY(mMutex) = JankType::None;
222 // Indicates if this frame was composited by the GPU or not
223 bool mGpuComposition GUARDED_BY(mMutex) = false;
Alec Mouri7d436ec2021-01-27 20:40:50 -0800224 // Rendering rate for this frame.
225 std::optional<Fps> mRenderRate GUARDED_BY(mMutex);
Adithya Srinivasan9b2ca3e2020-11-10 10:14:17 -0800226 // Enum for the type of present
227 FramePresentMetadata mFramePresentMetadata GUARDED_BY(mMutex) =
228 FramePresentMetadata::UnknownPresent;
229 // Enum for the type of finish
230 FrameReadyMetadata mFrameReadyMetadata GUARDED_BY(mMutex) = FrameReadyMetadata::UnknownFinish;
231 // Time when the previous buffer from the same layer was latched by SF. This is used in checking
232 // for BufferStuffing where the current buffer is expected to be ready but the previous buffer
233 // was latched instead.
234 nsecs_t mLastLatchTime GUARDED_BY(mMutex) = 0;
Adithya Srinivasan05bd2d12021-01-11 18:49:58 +0000235 // TraceCookieCounter is used to obtain the cookie for sendig trace packets to perfetto. Using a
236 // reference here because the counter is owned by FrameTimeline, which outlives SurfaceFrame.
237 TraceCookieCounter& mTraceCookieCounter;
Adithya Srinivasanf279e042020-08-17 14:56:27 -0700238};
239
240/*
241 * Maintains a history of SurfaceFrames grouped together by the vsync time in which they were
242 * presented
243 */
244class FrameTimeline {
245public:
246 virtual ~FrameTimeline() = default;
Adithya Srinivasan5f683cf2020-09-15 14:21:04 -0700247 virtual TokenManager* getTokenManager() = 0;
Adithya Srinivasanf279e042020-08-17 14:56:27 -0700248
Adithya Srinivasan01189672020-10-20 14:23:05 -0700249 // Initializes the Perfetto DataSource that emits DisplayFrame and SurfaceFrame events. Test
250 // classes can avoid double registration by mocking this function.
251 virtual void onBootFinished() = 0;
252
Adithya Srinivasanf279e042020-08-17 14:56:27 -0700253 // 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 -0700254 // Debug name is the human-readable debugging string for dumpsys.
Siarhei Vishniakoufc434ac2021-01-13 10:28:00 -1000255 virtual std::shared_ptr<SurfaceFrame> createSurfaceFrameForToken(
256 const FrameTimelineInfo& frameTimelineInfo, pid_t ownerPid, uid_t ownerUid,
257 std::string layerName, std::string debugName) = 0;
Adithya Srinivasanf279e042020-08-17 14:56:27 -0700258
259 // Adds a new SurfaceFrame to the current DisplayFrame. Frames from multiple layers can be
260 // composited into one display frame.
Adithya Srinivasan9b2ca3e2020-11-10 10:14:17 -0800261 virtual void addSurfaceFrame(std::shared_ptr<SurfaceFrame> surfaceFrame) = 0;
Adithya Srinivasanf279e042020-08-17 14:56:27 -0700262
263 // The first function called by SF for the current DisplayFrame. Fetches SF predictions based on
264 // the token and sets the actualSfWakeTime for the current DisplayFrame.
Alec Mouri7d436ec2021-01-27 20:40:50 -0800265 virtual void setSfWakeUp(int64_t token, nsecs_t wakeupTime, Fps refreshRate) = 0;
Adithya Srinivasanf279e042020-08-17 14:56:27 -0700266
267 // Sets the sfPresentTime and finalizes the current DisplayFrame. Tracks the given present fence
268 // until it's signaled, and updates the present timestamps of all presented SurfaceFrames in
269 // that vsync.
270 virtual void setSfPresent(nsecs_t sfPresentTime,
271 const std::shared_ptr<FenceTime>& presentFence) = 0;
Adithya Srinivasan5f683cf2020-09-15 14:21:04 -0700272
Adithya Srinivasan8fc601d2020-09-25 13:51:09 -0700273 // Args:
274 // -jank : Dumps only the Display Frames that are either janky themselves
275 // or contain janky Surface Frames.
276 // -all : Dumps the entire list of DisplayFrames and the SurfaceFrames contained within
277 virtual void parseArgs(const Vector<String16>& args, std::string& result) = 0;
Adithya Srinivasan2d736322020-10-01 16:53:48 -0700278
279 // Sets the max number of display frames that can be stored. Called by SF backdoor.
280 virtual void setMaxDisplayFrames(uint32_t size);
281
282 // Restores the max number of display frames to default. Called by SF backdoor.
283 virtual void reset() = 0;
Adithya Srinivasanf279e042020-08-17 14:56:27 -0700284};
285
286namespace impl {
287
Adithya Srinivasanf279e042020-08-17 14:56:27 -0700288class TokenManager : public android::frametimeline::TokenManager {
289public:
Siarhei Vishniakoufc434ac2021-01-13 10:28:00 -1000290 TokenManager() : mCurrentToken(FrameTimelineInfo::INVALID_VSYNC_ID + 1) {}
Adithya Srinivasanf279e042020-08-17 14:56:27 -0700291 ~TokenManager() = default;
292
293 int64_t generateTokenForPredictions(TimelineItem&& predictions) override;
Adithya Srinivasan9b2ca3e2020-11-10 10:14:17 -0800294 std::optional<TimelineItem> getPredictionsForToken(int64_t token) const override;
Adithya Srinivasanf279e042020-08-17 14:56:27 -0700295
296private:
297 // Friend class for testing
298 friend class android::frametimeline::FrameTimelineTest;
299
300 void flushTokens(nsecs_t flushTime) REQUIRES(mMutex);
301
Adithya Srinivasan9b2ca3e2020-11-10 10:14:17 -0800302 std::map<int64_t, TokenManagerPrediction> mPredictions GUARDED_BY(mMutex);
Adithya Srinivasanf279e042020-08-17 14:56:27 -0700303 int64_t mCurrentToken GUARDED_BY(mMutex);
Adithya Srinivasan9b2ca3e2020-11-10 10:14:17 -0800304 mutable std::mutex mMutex;
Adithya Srinivasanf279e042020-08-17 14:56:27 -0700305 static constexpr nsecs_t kMaxRetentionTime =
306 std::chrono::duration_cast<std::chrono::nanoseconds>(120ms).count();
307};
308
Adithya Srinivasanf279e042020-08-17 14:56:27 -0700309class FrameTimeline : public android::frametimeline::FrameTimeline {
310public:
Adithya Srinivasan01189672020-10-20 14:23:05 -0700311 class FrameTimelineDataSource : public perfetto::DataSource<FrameTimelineDataSource> {
312 void OnSetup(const SetupArgs&) override{};
313 void OnStart(const StartArgs&) override{};
314 void OnStop(const StopArgs&) override{};
315 };
316
Adithya Srinivasan9b2ca3e2020-11-10 10:14:17 -0800317 /*
318 * DisplayFrame should be used only internally within FrameTimeline. All members and methods are
319 * guarded by FrameTimeline's mMutex.
320 */
321 class DisplayFrame {
322 public:
Adithya Srinivasan05bd2d12021-01-11 18:49:58 +0000323 DisplayFrame(std::shared_ptr<TimeStats> timeStats, JankClassificationThresholds thresholds,
324 TraceCookieCounter* traceCookieCounter);
Adithya Srinivasan9b2ca3e2020-11-10 10:14:17 -0800325 virtual ~DisplayFrame() = default;
326 // Dumpsys interface - dumps only if the DisplayFrame itself is janky or is at least one
327 // SurfaceFrame is janky.
328 void dumpJank(std::string& result, nsecs_t baseTime, int displayFrameCount) const;
329 // Dumpsys interface - dumps all data irrespective of jank
330 void dumpAll(std::string& result, nsecs_t baseTime) const;
331 // Emits a packet for perfetto tracing. The function body will be executed only if tracing
332 // is enabled.
333 void trace(pid_t surfaceFlingerPid) const;
334 // Sets the token, vsyncPeriod, predictions and SF start time.
Alec Mouri7d436ec2021-01-27 20:40:50 -0800335 void onSfWakeUp(int64_t token, Fps refreshRate, std::optional<TimelineItem> predictions,
Adithya Srinivasan9b2ca3e2020-11-10 10:14:17 -0800336 nsecs_t wakeUpTime);
337 // Sets the appropriate metadata, classifies the jank and returns the classified jankType.
338 void onPresent(nsecs_t signalTime);
339 // Adds the provided SurfaceFrame to the current display frame.
340 void addSurfaceFrame(std::shared_ptr<SurfaceFrame> surfaceFrame);
341
Adithya Srinivasan9b2ca3e2020-11-10 10:14:17 -0800342 void setPredictions(PredictionState predictionState, TimelineItem predictions);
343 void setActualStartTime(nsecs_t actualStartTime);
344 void setActualEndTime(nsecs_t actualEndTime);
345
346 // BaseTime is the smallest timestamp in a DisplayFrame.
347 // Used for dumping all timestamps relative to the oldest, making it easy to read.
348 nsecs_t getBaseTime() const;
349
350 // Functions to be used only in testing.
351 TimelineItem getActuals() const { return mSurfaceFlingerActuals; };
352 TimelineItem getPredictions() const { return mSurfaceFlingerPredictions; };
353 FramePresentMetadata getFramePresentMetadata() const { return mFramePresentMetadata; };
354 FrameReadyMetadata getFrameReadyMetadata() const { return mFrameReadyMetadata; };
355 int32_t getJankType() const { return mJankType; }
356 const std::vector<std::shared_ptr<SurfaceFrame>>& getSurfaceFrames() const {
357 return mSurfaceFrames;
358 }
359
360 private:
361 void dump(std::string& result, nsecs_t baseTime) const;
362
Siarhei Vishniakoufc434ac2021-01-13 10:28:00 -1000363 int64_t mToken = FrameTimelineInfo::INVALID_VSYNC_ID;
Adithya Srinivasan9b2ca3e2020-11-10 10:14:17 -0800364
365 /* Usage of TimelineItem w.r.t SurfaceFlinger
366 * startTime Time when SurfaceFlinger wakes up to handle transactions and buffer updates
367 * endTime Time when SurfaceFlinger sends a composited frame to Display
368 * presentTime Time when the composited frame was presented on screen
369 */
370 TimelineItem mSurfaceFlingerPredictions;
371 TimelineItem mSurfaceFlingerActuals;
372 std::shared_ptr<TimeStats> mTimeStats;
373 const JankClassificationThresholds mJankClassificationThresholds;
374
375 // Collection of predictions and actual values sent over by Layers
376 std::vector<std::shared_ptr<SurfaceFrame>> mSurfaceFrames;
377
378 PredictionState mPredictionState = PredictionState::None;
379 // Bitmask for the type of jank
380 int32_t mJankType = JankType::None;
381 // Indicates if this frame was composited by the GPU or not
382 bool mGpuComposition = false;
383 // Enum for the type of present
384 FramePresentMetadata mFramePresentMetadata = FramePresentMetadata::UnknownPresent;
385 // Enum for the type of finish
386 FrameReadyMetadata mFrameReadyMetadata = FrameReadyMetadata::UnknownFinish;
387 // Enum for the type of start
388 FrameStartMetadata mFrameStartMetadata = FrameStartMetadata::UnknownStart;
389 // The refresh rate (vsync period) in nanoseconds as seen by SF during this DisplayFrame's
390 // timeline
Alec Mouri7d436ec2021-01-27 20:40:50 -0800391 Fps mRefreshRate;
Adithya Srinivasan05bd2d12021-01-11 18:49:58 +0000392 // TraceCookieCounter is used to obtain the cookie for sendig trace packets to perfetto.
393 // Using a reference here because the counter is owned by FrameTimeline, which outlives
394 // DisplayFrame.
395 TraceCookieCounter& mTraceCookieCounter;
Adithya Srinivasan9b2ca3e2020-11-10 10:14:17 -0800396 };
397
398 FrameTimeline(std::shared_ptr<TimeStats> timeStats, pid_t surfaceFlingerPid,
399 JankClassificationThresholds thresholds = {});
Adithya Srinivasanf279e042020-08-17 14:56:27 -0700400 ~FrameTimeline() = default;
401
Adithya Srinivasan5f683cf2020-09-15 14:21:04 -0700402 frametimeline::TokenManager* getTokenManager() override { return &mTokenManager; }
Siarhei Vishniakoufc434ac2021-01-13 10:28:00 -1000403 std::shared_ptr<SurfaceFrame> createSurfaceFrameForToken(
404 const FrameTimelineInfo& frameTimelineInfo, pid_t ownerPid, uid_t ownerUid,
405 std::string layerName, std::string debugName) override;
Adithya Srinivasan9b2ca3e2020-11-10 10:14:17 -0800406 void addSurfaceFrame(std::shared_ptr<frametimeline::SurfaceFrame> surfaceFrame) override;
Alec Mouri7d436ec2021-01-27 20:40:50 -0800407 void setSfWakeUp(int64_t token, nsecs_t wakeupTime, Fps refreshRate) override;
Adithya Srinivasanf279e042020-08-17 14:56:27 -0700408 void setSfPresent(nsecs_t sfPresentTime,
409 const std::shared_ptr<FenceTime>& presentFence) override;
Adithya Srinivasan8fc601d2020-09-25 13:51:09 -0700410 void parseArgs(const Vector<String16>& args, std::string& result) override;
Adithya Srinivasan2d736322020-10-01 16:53:48 -0700411 void setMaxDisplayFrames(uint32_t size) override;
412 void reset() override;
Adithya Srinivasanf279e042020-08-17 14:56:27 -0700413
Adithya Srinivasan01189672020-10-20 14:23:05 -0700414 // Sets up the perfetto tracing backend and data source.
415 void onBootFinished() override;
416 // Registers the data source with the perfetto backend. Called as part of onBootFinished()
417 // and should not be called manually outside of tests.
418 void registerDataSource();
419
420 static constexpr char kFrameTimelineDataSource[] = "android.surfaceflinger.frametimeline";
421
Adithya Srinivasanf279e042020-08-17 14:56:27 -0700422private:
423 // Friend class for testing
424 friend class android::frametimeline::FrameTimelineTest;
425
Adithya Srinivasanf279e042020-08-17 14:56:27 -0700426 void flushPendingPresentFences() REQUIRES(mMutex);
427 void finalizeCurrentDisplayFrame() REQUIRES(mMutex);
Adithya Srinivasan8fc601d2020-09-25 13:51:09 -0700428 void dumpAll(std::string& result);
429 void dumpJank(std::string& result);
Adithya Srinivasanf279e042020-08-17 14:56:27 -0700430
431 // Sliding window of display frames. TODO(b/168072834): compare perf with fixed size array
432 std::deque<std::shared_ptr<DisplayFrame>> mDisplayFrames GUARDED_BY(mMutex);
433 std::vector<std::pair<std::shared_ptr<FenceTime>, std::shared_ptr<DisplayFrame>>>
434 mPendingPresentFences GUARDED_BY(mMutex);
435 std::shared_ptr<DisplayFrame> mCurrentDisplayFrame GUARDED_BY(mMutex);
436 TokenManager mTokenManager;
Adithya Srinivasan05bd2d12021-01-11 18:49:58 +0000437 TraceCookieCounter mTraceCookieCounter;
Adithya Srinivasan9b2ca3e2020-11-10 10:14:17 -0800438 mutable std::mutex mMutex;
Adithya Srinivasan2d736322020-10-01 16:53:48 -0700439 uint32_t mMaxDisplayFrames;
Alec Mouri9a29e672020-09-14 12:39:14 -0700440 std::shared_ptr<TimeStats> mTimeStats;
Adithya Srinivasan9b2ca3e2020-11-10 10:14:17 -0800441 const pid_t mSurfaceFlingerPid;
442 const JankClassificationThresholds mJankClassificationThresholds;
Adithya Srinivasan2d736322020-10-01 16:53:48 -0700443 static constexpr uint32_t kDefaultMaxDisplayFrames = 64;
Adithya Srinivasan01189672020-10-20 14:23:05 -0700444 // The initial container size for the vector<SurfaceFrames> inside display frame. Although
445 // this number doesn't represent any bounds on the number of surface frames that can go in a
446 // display frame, this is a good starting size for the vector so that we can avoid the
447 // internal vector resizing that happens with push_back.
Adithya Srinivasanf279e042020-08-17 14:56:27 -0700448 static constexpr uint32_t kNumSurfaceFramesInitial = 10;
Adithya Srinivasanf279e042020-08-17 14:56:27 -0700449};
450
451} // namespace impl
452} // namespace android::frametimeline