blob: 3ddd900145d2d251dc52fa6f79a8a1e8c2f92c39 [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 Mouri7d436ec2021-01-27 20:40:50 -0800186 void onPresent(nsecs_t presentTime, int32_t displayFrameJankType, Fps refreshRate);
Adithya Srinivasan9b2ca3e2020-11-10 10:14:17 -0800187 // All the timestamps are dumped relative to the baseTime
188 void dump(std::string& result, const std::string& indent, nsecs_t baseTime) const;
189 // Emits a packet for perfetto tracing. The function body will be executed only if tracing is
190 // enabled. The displayFrameToken is needed to link the SurfaceFrame to the corresponding
191 // DisplayFrame at the trace processor side.
192 void trace(int64_t displayFrameToken);
Jorim Jaggi9c03b502020-11-24 23:51:31 +0100193
Adithya Srinivasan9b2ca3e2020-11-10 10:14:17 -0800194 // Getter functions used only by FrameTimelineTests
195 TimelineItem getActuals() const;
196 pid_t getOwnerPid() const { return mOwnerPid; };
197 PredictionState getPredictionState() const { return mPredictionState; };
198 PresentState getPresentState() const;
199 FrameReadyMetadata getFrameReadyMetadata() const;
200 FramePresentMetadata getFramePresentMetadata() const;
201
202private:
203 const int64_t mToken;
Siarhei Vishniakoufc434ac2021-01-13 10:28:00 -1000204 const int32_t mInputEventId;
Adithya Srinivasan9b2ca3e2020-11-10 10:14:17 -0800205 const pid_t mOwnerPid;
206 const uid_t mOwnerUid;
207 const std::string mLayerName;
208 const std::string mDebugName;
209 PresentState mPresentState GUARDED_BY(mMutex);
210 const PredictionState mPredictionState;
211 const TimelineItem mPredictions;
212 TimelineItem mActuals GUARDED_BY(mMutex);
213 std::shared_ptr<TimeStats> mTimeStats;
214 const JankClassificationThresholds mJankClassificationThresholds;
215 nsecs_t mActualQueueTime GUARDED_BY(mMutex) = 0;
216 mutable std::mutex mMutex;
217 // Bitmask for the type of jank
218 int32_t mJankType GUARDED_BY(mMutex) = JankType::None;
219 // Indicates if this frame was composited by the GPU or not
220 bool mGpuComposition GUARDED_BY(mMutex) = false;
Alec Mouri7d436ec2021-01-27 20:40:50 -0800221 // Rendering rate for this frame.
222 std::optional<Fps> mRenderRate GUARDED_BY(mMutex);
Adithya Srinivasan9b2ca3e2020-11-10 10:14:17 -0800223 // Enum for the type of present
224 FramePresentMetadata mFramePresentMetadata GUARDED_BY(mMutex) =
225 FramePresentMetadata::UnknownPresent;
226 // Enum for the type of finish
227 FrameReadyMetadata mFrameReadyMetadata GUARDED_BY(mMutex) = FrameReadyMetadata::UnknownFinish;
228 // Time when the previous buffer from the same layer was latched by SF. This is used in checking
229 // for BufferStuffing where the current buffer is expected to be ready but the previous buffer
230 // was latched instead.
231 nsecs_t mLastLatchTime GUARDED_BY(mMutex) = 0;
Adithya Srinivasan05bd2d12021-01-11 18:49:58 +0000232 // TraceCookieCounter is used to obtain the cookie for sendig trace packets to perfetto. Using a
233 // reference here because the counter is owned by FrameTimeline, which outlives SurfaceFrame.
234 TraceCookieCounter& mTraceCookieCounter;
Adithya Srinivasanf279e042020-08-17 14:56:27 -0700235};
236
237/*
238 * Maintains a history of SurfaceFrames grouped together by the vsync time in which they were
239 * presented
240 */
241class FrameTimeline {
242public:
243 virtual ~FrameTimeline() = default;
Adithya Srinivasan5f683cf2020-09-15 14:21:04 -0700244 virtual TokenManager* getTokenManager() = 0;
Adithya Srinivasanf279e042020-08-17 14:56:27 -0700245
Adithya Srinivasan01189672020-10-20 14:23:05 -0700246 // Initializes the Perfetto DataSource that emits DisplayFrame and SurfaceFrame events. Test
247 // classes can avoid double registration by mocking this function.
248 virtual void onBootFinished() = 0;
249
Adithya Srinivasanf279e042020-08-17 14:56:27 -0700250 // 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 -0700251 // Debug name is the human-readable debugging string for dumpsys.
Siarhei Vishniakoufc434ac2021-01-13 10:28:00 -1000252 virtual std::shared_ptr<SurfaceFrame> createSurfaceFrameForToken(
253 const FrameTimelineInfo& frameTimelineInfo, pid_t ownerPid, uid_t ownerUid,
254 std::string layerName, std::string debugName) = 0;
Adithya Srinivasanf279e042020-08-17 14:56:27 -0700255
256 // Adds a new SurfaceFrame to the current DisplayFrame. Frames from multiple layers can be
257 // composited into one display frame.
Adithya Srinivasan9b2ca3e2020-11-10 10:14:17 -0800258 virtual void addSurfaceFrame(std::shared_ptr<SurfaceFrame> surfaceFrame) = 0;
Adithya Srinivasanf279e042020-08-17 14:56:27 -0700259
260 // The first function called by SF for the current DisplayFrame. Fetches SF predictions based on
261 // the token and sets the actualSfWakeTime for the current DisplayFrame.
Alec Mouri7d436ec2021-01-27 20:40:50 -0800262 virtual void setSfWakeUp(int64_t token, nsecs_t wakeupTime, Fps refreshRate) = 0;
Adithya Srinivasanf279e042020-08-17 14:56:27 -0700263
264 // Sets the sfPresentTime and finalizes the current DisplayFrame. Tracks the given present fence
265 // until it's signaled, and updates the present timestamps of all presented SurfaceFrames in
266 // that vsync.
267 virtual void setSfPresent(nsecs_t sfPresentTime,
268 const std::shared_ptr<FenceTime>& presentFence) = 0;
Adithya Srinivasan5f683cf2020-09-15 14:21:04 -0700269
Adithya Srinivasan8fc601d2020-09-25 13:51:09 -0700270 // Args:
271 // -jank : Dumps only the Display Frames that are either janky themselves
272 // or contain janky Surface Frames.
273 // -all : Dumps the entire list of DisplayFrames and the SurfaceFrames contained within
274 virtual void parseArgs(const Vector<String16>& args, std::string& result) = 0;
Adithya Srinivasan2d736322020-10-01 16:53:48 -0700275
276 // Sets the max number of display frames that can be stored. Called by SF backdoor.
277 virtual void setMaxDisplayFrames(uint32_t size);
278
279 // Restores the max number of display frames to default. Called by SF backdoor.
280 virtual void reset() = 0;
Adithya Srinivasanf279e042020-08-17 14:56:27 -0700281};
282
283namespace impl {
284
Adithya Srinivasanf279e042020-08-17 14:56:27 -0700285class TokenManager : public android::frametimeline::TokenManager {
286public:
Siarhei Vishniakoufc434ac2021-01-13 10:28:00 -1000287 TokenManager() : mCurrentToken(FrameTimelineInfo::INVALID_VSYNC_ID + 1) {}
Adithya Srinivasanf279e042020-08-17 14:56:27 -0700288 ~TokenManager() = default;
289
290 int64_t generateTokenForPredictions(TimelineItem&& predictions) override;
Adithya Srinivasan9b2ca3e2020-11-10 10:14:17 -0800291 std::optional<TimelineItem> getPredictionsForToken(int64_t token) const override;
Adithya Srinivasanf279e042020-08-17 14:56:27 -0700292
293private:
294 // Friend class for testing
295 friend class android::frametimeline::FrameTimelineTest;
296
297 void flushTokens(nsecs_t flushTime) REQUIRES(mMutex);
298
Adithya Srinivasan9b2ca3e2020-11-10 10:14:17 -0800299 std::map<int64_t, TokenManagerPrediction> mPredictions GUARDED_BY(mMutex);
Adithya Srinivasanf279e042020-08-17 14:56:27 -0700300 int64_t mCurrentToken GUARDED_BY(mMutex);
Adithya Srinivasan9b2ca3e2020-11-10 10:14:17 -0800301 mutable std::mutex mMutex;
Adithya Srinivasanf279e042020-08-17 14:56:27 -0700302 static constexpr nsecs_t kMaxRetentionTime =
303 std::chrono::duration_cast<std::chrono::nanoseconds>(120ms).count();
304};
305
Adithya Srinivasanf279e042020-08-17 14:56:27 -0700306class FrameTimeline : public android::frametimeline::FrameTimeline {
307public:
Adithya Srinivasan01189672020-10-20 14:23:05 -0700308 class FrameTimelineDataSource : public perfetto::DataSource<FrameTimelineDataSource> {
309 void OnSetup(const SetupArgs&) override{};
310 void OnStart(const StartArgs&) override{};
311 void OnStop(const StopArgs&) override{};
312 };
313
Adithya Srinivasan9b2ca3e2020-11-10 10:14:17 -0800314 /*
315 * DisplayFrame should be used only internally within FrameTimeline. All members and methods are
316 * guarded by FrameTimeline's mMutex.
317 */
318 class DisplayFrame {
319 public:
Adithya Srinivasan05bd2d12021-01-11 18:49:58 +0000320 DisplayFrame(std::shared_ptr<TimeStats> timeStats, JankClassificationThresholds thresholds,
321 TraceCookieCounter* traceCookieCounter);
Adithya Srinivasan9b2ca3e2020-11-10 10:14:17 -0800322 virtual ~DisplayFrame() = default;
323 // Dumpsys interface - dumps only if the DisplayFrame itself is janky or is at least one
324 // SurfaceFrame is janky.
325 void dumpJank(std::string& result, nsecs_t baseTime, int displayFrameCount) const;
326 // Dumpsys interface - dumps all data irrespective of jank
327 void dumpAll(std::string& result, nsecs_t baseTime) const;
328 // Emits a packet for perfetto tracing. The function body will be executed only if tracing
329 // is enabled.
330 void trace(pid_t surfaceFlingerPid) const;
331 // Sets the token, vsyncPeriod, predictions and SF start time.
Alec Mouri7d436ec2021-01-27 20:40:50 -0800332 void onSfWakeUp(int64_t token, Fps refreshRate, std::optional<TimelineItem> predictions,
Adithya Srinivasan9b2ca3e2020-11-10 10:14:17 -0800333 nsecs_t wakeUpTime);
334 // Sets the appropriate metadata, classifies the jank and returns the classified jankType.
335 void onPresent(nsecs_t signalTime);
336 // Adds the provided SurfaceFrame to the current display frame.
337 void addSurfaceFrame(std::shared_ptr<SurfaceFrame> surfaceFrame);
338
Adithya Srinivasan9b2ca3e2020-11-10 10:14:17 -0800339 void setPredictions(PredictionState predictionState, TimelineItem predictions);
340 void setActualStartTime(nsecs_t actualStartTime);
341 void setActualEndTime(nsecs_t actualEndTime);
342
343 // BaseTime is the smallest timestamp in a DisplayFrame.
344 // Used for dumping all timestamps relative to the oldest, making it easy to read.
345 nsecs_t getBaseTime() const;
346
347 // Functions to be used only in testing.
348 TimelineItem getActuals() const { return mSurfaceFlingerActuals; };
349 TimelineItem getPredictions() const { return mSurfaceFlingerPredictions; };
350 FramePresentMetadata getFramePresentMetadata() const { return mFramePresentMetadata; };
351 FrameReadyMetadata getFrameReadyMetadata() const { return mFrameReadyMetadata; };
352 int32_t getJankType() const { return mJankType; }
353 const std::vector<std::shared_ptr<SurfaceFrame>>& getSurfaceFrames() const {
354 return mSurfaceFrames;
355 }
356
357 private:
358 void dump(std::string& result, nsecs_t baseTime) const;
359
Siarhei Vishniakoufc434ac2021-01-13 10:28:00 -1000360 int64_t mToken = FrameTimelineInfo::INVALID_VSYNC_ID;
Adithya Srinivasan9b2ca3e2020-11-10 10:14:17 -0800361
362 /* Usage of TimelineItem w.r.t SurfaceFlinger
363 * startTime Time when SurfaceFlinger wakes up to handle transactions and buffer updates
364 * endTime Time when SurfaceFlinger sends a composited frame to Display
365 * presentTime Time when the composited frame was presented on screen
366 */
367 TimelineItem mSurfaceFlingerPredictions;
368 TimelineItem mSurfaceFlingerActuals;
369 std::shared_ptr<TimeStats> mTimeStats;
370 const JankClassificationThresholds mJankClassificationThresholds;
371
372 // Collection of predictions and actual values sent over by Layers
373 std::vector<std::shared_ptr<SurfaceFrame>> mSurfaceFrames;
374
375 PredictionState mPredictionState = PredictionState::None;
376 // Bitmask for the type of jank
377 int32_t mJankType = JankType::None;
378 // Indicates if this frame was composited by the GPU or not
379 bool mGpuComposition = false;
380 // Enum for the type of present
381 FramePresentMetadata mFramePresentMetadata = FramePresentMetadata::UnknownPresent;
382 // Enum for the type of finish
383 FrameReadyMetadata mFrameReadyMetadata = FrameReadyMetadata::UnknownFinish;
384 // Enum for the type of start
385 FrameStartMetadata mFrameStartMetadata = FrameStartMetadata::UnknownStart;
386 // The refresh rate (vsync period) in nanoseconds as seen by SF during this DisplayFrame's
387 // timeline
Alec Mouri7d436ec2021-01-27 20:40:50 -0800388 Fps mRefreshRate;
Adithya Srinivasan05bd2d12021-01-11 18:49:58 +0000389 // TraceCookieCounter is used to obtain the cookie for sendig trace packets to perfetto.
390 // Using a reference here because the counter is owned by FrameTimeline, which outlives
391 // DisplayFrame.
392 TraceCookieCounter& mTraceCookieCounter;
Adithya Srinivasan9b2ca3e2020-11-10 10:14:17 -0800393 };
394
395 FrameTimeline(std::shared_ptr<TimeStats> timeStats, pid_t surfaceFlingerPid,
396 JankClassificationThresholds thresholds = {});
Adithya Srinivasanf279e042020-08-17 14:56:27 -0700397 ~FrameTimeline() = default;
398
Adithya Srinivasan5f683cf2020-09-15 14:21:04 -0700399 frametimeline::TokenManager* getTokenManager() override { return &mTokenManager; }
Siarhei Vishniakoufc434ac2021-01-13 10:28:00 -1000400 std::shared_ptr<SurfaceFrame> createSurfaceFrameForToken(
401 const FrameTimelineInfo& frameTimelineInfo, pid_t ownerPid, uid_t ownerUid,
402 std::string layerName, std::string debugName) override;
Adithya Srinivasan9b2ca3e2020-11-10 10:14:17 -0800403 void addSurfaceFrame(std::shared_ptr<frametimeline::SurfaceFrame> surfaceFrame) override;
Alec Mouri7d436ec2021-01-27 20:40:50 -0800404 void setSfWakeUp(int64_t token, nsecs_t wakeupTime, Fps refreshRate) override;
Adithya Srinivasanf279e042020-08-17 14:56:27 -0700405 void setSfPresent(nsecs_t sfPresentTime,
406 const std::shared_ptr<FenceTime>& presentFence) override;
Adithya Srinivasan8fc601d2020-09-25 13:51:09 -0700407 void parseArgs(const Vector<String16>& args, std::string& result) override;
Adithya Srinivasan2d736322020-10-01 16:53:48 -0700408 void setMaxDisplayFrames(uint32_t size) override;
409 void reset() override;
Adithya Srinivasanf279e042020-08-17 14:56:27 -0700410
Adithya Srinivasan01189672020-10-20 14:23:05 -0700411 // Sets up the perfetto tracing backend and data source.
412 void onBootFinished() override;
413 // Registers the data source with the perfetto backend. Called as part of onBootFinished()
414 // and should not be called manually outside of tests.
415 void registerDataSource();
416
417 static constexpr char kFrameTimelineDataSource[] = "android.surfaceflinger.frametimeline";
418
Adithya Srinivasanf279e042020-08-17 14:56:27 -0700419private:
420 // Friend class for testing
421 friend class android::frametimeline::FrameTimelineTest;
422
Adithya Srinivasanf279e042020-08-17 14:56:27 -0700423 void flushPendingPresentFences() REQUIRES(mMutex);
424 void finalizeCurrentDisplayFrame() REQUIRES(mMutex);
Adithya Srinivasan8fc601d2020-09-25 13:51:09 -0700425 void dumpAll(std::string& result);
426 void dumpJank(std::string& result);
Adithya Srinivasanf279e042020-08-17 14:56:27 -0700427
428 // Sliding window of display frames. TODO(b/168072834): compare perf with fixed size array
429 std::deque<std::shared_ptr<DisplayFrame>> mDisplayFrames GUARDED_BY(mMutex);
430 std::vector<std::pair<std::shared_ptr<FenceTime>, std::shared_ptr<DisplayFrame>>>
431 mPendingPresentFences GUARDED_BY(mMutex);
432 std::shared_ptr<DisplayFrame> mCurrentDisplayFrame GUARDED_BY(mMutex);
433 TokenManager mTokenManager;
Adithya Srinivasan05bd2d12021-01-11 18:49:58 +0000434 TraceCookieCounter mTraceCookieCounter;
Adithya Srinivasan9b2ca3e2020-11-10 10:14:17 -0800435 mutable std::mutex mMutex;
Adithya Srinivasan2d736322020-10-01 16:53:48 -0700436 uint32_t mMaxDisplayFrames;
Alec Mouri9a29e672020-09-14 12:39:14 -0700437 std::shared_ptr<TimeStats> mTimeStats;
Adithya Srinivasan9b2ca3e2020-11-10 10:14:17 -0800438 const pid_t mSurfaceFlingerPid;
439 const JankClassificationThresholds mJankClassificationThresholds;
Adithya Srinivasan2d736322020-10-01 16:53:48 -0700440 static constexpr uint32_t kDefaultMaxDisplayFrames = 64;
Adithya Srinivasan01189672020-10-20 14:23:05 -0700441 // The initial container size for the vector<SurfaceFrames> inside display frame. Although
442 // this number doesn't represent any bounds on the number of surface frames that can go in a
443 // display frame, this is a good starting size for the vector so that we can avoid the
444 // internal vector resizing that happens with push_back.
Adithya Srinivasanf279e042020-08-17 14:56:27 -0700445 static constexpr uint32_t kNumSurfaceFramesInitial = 10;
Adithya Srinivasanf279e042020-08-17 14:56:27 -0700446};
447
448} // namespace impl
449} // namespace android::frametimeline