xref: /aosp_15_r20/frameworks/native/services/surfaceflinger/FrameTimeline/FrameTimeline.h (revision 38e8c45f13ce32b0dcecb25141ffecaf386fa17f)
1*38e8c45fSAndroid Build Coastguard Worker /*
2*38e8c45fSAndroid Build Coastguard Worker  * Copyright 2020 The Android Open Source Project
3*38e8c45fSAndroid Build Coastguard Worker  *
4*38e8c45fSAndroid Build Coastguard Worker  * Licensed under the Apache License, Version 2.0 (the "License");
5*38e8c45fSAndroid Build Coastguard Worker  * you may not use this file except in compliance with the License.
6*38e8c45fSAndroid Build Coastguard Worker  * You may obtain a copy of the License at
7*38e8c45fSAndroid Build Coastguard Worker  *
8*38e8c45fSAndroid Build Coastguard Worker  *      http://www.apache.org/licenses/LICENSE-2.0
9*38e8c45fSAndroid Build Coastguard Worker  *
10*38e8c45fSAndroid Build Coastguard Worker  * Unless required by applicable law or agreed to in writing, software
11*38e8c45fSAndroid Build Coastguard Worker  * distributed under the License is distributed on an "AS IS" BASIS,
12*38e8c45fSAndroid Build Coastguard Worker  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*38e8c45fSAndroid Build Coastguard Worker  * See the License for the specific language governing permissions and
14*38e8c45fSAndroid Build Coastguard Worker  * limitations under the License.
15*38e8c45fSAndroid Build Coastguard Worker  */
16*38e8c45fSAndroid Build Coastguard Worker 
17*38e8c45fSAndroid Build Coastguard Worker #pragma once
18*38e8c45fSAndroid Build Coastguard Worker 
19*38e8c45fSAndroid Build Coastguard Worker #include <atomic>
20*38e8c45fSAndroid Build Coastguard Worker #include <chrono>
21*38e8c45fSAndroid Build Coastguard Worker #include <deque>
22*38e8c45fSAndroid Build Coastguard Worker #include <memory>
23*38e8c45fSAndroid Build Coastguard Worker #include <mutex>
24*38e8c45fSAndroid Build Coastguard Worker #include <optional>
25*38e8c45fSAndroid Build Coastguard Worker #include <string>
26*38e8c45fSAndroid Build Coastguard Worker 
27*38e8c45fSAndroid Build Coastguard Worker #include <gui/ISurfaceComposer.h>
28*38e8c45fSAndroid Build Coastguard Worker #include <gui/JankInfo.h>
29*38e8c45fSAndroid Build Coastguard Worker #include <gui/LayerMetadata.h>
30*38e8c45fSAndroid Build Coastguard Worker #include <perfetto/trace/android/frame_timeline_event.pbzero.h>
31*38e8c45fSAndroid Build Coastguard Worker #include <perfetto/tracing.h>
32*38e8c45fSAndroid Build Coastguard Worker #include <ui/FenceTime.h>
33*38e8c45fSAndroid Build Coastguard Worker #include <utils/RefBase.h>
34*38e8c45fSAndroid Build Coastguard Worker #include <utils/String16.h>
35*38e8c45fSAndroid Build Coastguard Worker #include <utils/Timers.h>
36*38e8c45fSAndroid Build Coastguard Worker #include <utils/Vector.h>
37*38e8c45fSAndroid Build Coastguard Worker 
38*38e8c45fSAndroid Build Coastguard Worker #include <scheduler/Fps.h>
39*38e8c45fSAndroid Build Coastguard Worker 
40*38e8c45fSAndroid Build Coastguard Worker #include "../TimeStats/TimeStats.h"
41*38e8c45fSAndroid Build Coastguard Worker 
42*38e8c45fSAndroid Build Coastguard Worker namespace android::frametimeline {
43*38e8c45fSAndroid Build Coastguard Worker 
44*38e8c45fSAndroid Build Coastguard Worker class FrameTimelineTest;
45*38e8c45fSAndroid Build Coastguard Worker 
46*38e8c45fSAndroid Build Coastguard Worker using namespace std::chrono_literals;
47*38e8c45fSAndroid Build Coastguard Worker 
48*38e8c45fSAndroid Build Coastguard Worker // Metadata indicating how the frame was presented w.r.t expected present time.
49*38e8c45fSAndroid Build Coastguard Worker enum class FramePresentMetadata : int8_t {
50*38e8c45fSAndroid Build Coastguard Worker     // Frame was presented on time
51*38e8c45fSAndroid Build Coastguard Worker     OnTimePresent,
52*38e8c45fSAndroid Build Coastguard Worker     // Frame was presented late
53*38e8c45fSAndroid Build Coastguard Worker     LatePresent,
54*38e8c45fSAndroid Build Coastguard Worker     // Frame was presented early
55*38e8c45fSAndroid Build Coastguard Worker     EarlyPresent,
56*38e8c45fSAndroid Build Coastguard Worker     // Unknown/initial state
57*38e8c45fSAndroid Build Coastguard Worker     UnknownPresent,
58*38e8c45fSAndroid Build Coastguard Worker };
59*38e8c45fSAndroid Build Coastguard Worker 
60*38e8c45fSAndroid Build Coastguard Worker // Metadata comparing the frame's actual finish time to the expected deadline.
61*38e8c45fSAndroid Build Coastguard Worker enum class FrameReadyMetadata : int8_t {
62*38e8c45fSAndroid Build Coastguard Worker     // App/SF finished on time. Early finish is treated as on time since the goal of any component
63*38e8c45fSAndroid Build Coastguard Worker     // is to finish before the deadline.
64*38e8c45fSAndroid Build Coastguard Worker     OnTimeFinish,
65*38e8c45fSAndroid Build Coastguard Worker     // App/SF finished work later than expected
66*38e8c45fSAndroid Build Coastguard Worker     LateFinish,
67*38e8c45fSAndroid Build Coastguard Worker     // Unknown/initial state
68*38e8c45fSAndroid Build Coastguard Worker     UnknownFinish,
69*38e8c45fSAndroid Build Coastguard Worker };
70*38e8c45fSAndroid Build Coastguard Worker 
71*38e8c45fSAndroid Build Coastguard Worker // Metadata comparing the frame's actual start time to the expected start time.
72*38e8c45fSAndroid Build Coastguard Worker enum class FrameStartMetadata : int8_t {
73*38e8c45fSAndroid Build Coastguard Worker     // App/SF started on time
74*38e8c45fSAndroid Build Coastguard Worker     OnTimeStart,
75*38e8c45fSAndroid Build Coastguard Worker     // App/SF started later than expected
76*38e8c45fSAndroid Build Coastguard Worker     LateStart,
77*38e8c45fSAndroid Build Coastguard Worker     // App/SF started earlier than expected
78*38e8c45fSAndroid Build Coastguard Worker     EarlyStart,
79*38e8c45fSAndroid Build Coastguard Worker     // Unknown/initial state
80*38e8c45fSAndroid Build Coastguard Worker     UnknownStart,
81*38e8c45fSAndroid Build Coastguard Worker };
82*38e8c45fSAndroid Build Coastguard Worker 
83*38e8c45fSAndroid Build Coastguard Worker /*
84*38e8c45fSAndroid Build Coastguard Worker  * Collection of timestamps that can be used for both predictions and actual times.
85*38e8c45fSAndroid Build Coastguard Worker  */
86*38e8c45fSAndroid Build Coastguard Worker struct TimelineItem {
87*38e8c45fSAndroid Build Coastguard Worker     TimelineItem(const nsecs_t startTime = 0, const nsecs_t endTime = 0,
88*38e8c45fSAndroid Build Coastguard Worker                  const nsecs_t presentTime = 0, const nsecs_t desiredPresentTime = 0)
startTimeTimelineItem89*38e8c45fSAndroid Build Coastguard Worker           : startTime(startTime),
90*38e8c45fSAndroid Build Coastguard Worker             endTime(endTime),
91*38e8c45fSAndroid Build Coastguard Worker             presentTime(presentTime),
92*38e8c45fSAndroid Build Coastguard Worker             desiredPresentTime(desiredPresentTime) {}
93*38e8c45fSAndroid Build Coastguard Worker 
94*38e8c45fSAndroid Build Coastguard Worker     nsecs_t startTime;
95*38e8c45fSAndroid Build Coastguard Worker     nsecs_t endTime;
96*38e8c45fSAndroid Build Coastguard Worker     nsecs_t presentTime;
97*38e8c45fSAndroid Build Coastguard Worker     nsecs_t desiredPresentTime;
98*38e8c45fSAndroid Build Coastguard Worker 
99*38e8c45fSAndroid Build Coastguard Worker     bool operator==(const TimelineItem& other) const {
100*38e8c45fSAndroid Build Coastguard Worker         return startTime == other.startTime && endTime == other.endTime &&
101*38e8c45fSAndroid Build Coastguard Worker                 presentTime == other.presentTime && desiredPresentTime != other.desiredPresentTime;
102*38e8c45fSAndroid Build Coastguard Worker     }
103*38e8c45fSAndroid Build Coastguard Worker 
104*38e8c45fSAndroid Build Coastguard Worker     bool operator!=(const TimelineItem& other) const { return !(*this == other); }
105*38e8c45fSAndroid Build Coastguard Worker };
106*38e8c45fSAndroid Build Coastguard Worker 
107*38e8c45fSAndroid Build Coastguard Worker struct JankClassificationThresholds {
108*38e8c45fSAndroid Build Coastguard Worker     // The various thresholds for App and SF. If the actual timestamp falls within the threshold
109*38e8c45fSAndroid Build Coastguard Worker     // compared to prediction, we treat it as on time.
110*38e8c45fSAndroid Build Coastguard Worker     nsecs_t presentThreshold = std::chrono::duration_cast<std::chrono::nanoseconds>(2ms).count();
111*38e8c45fSAndroid Build Coastguard Worker     nsecs_t deadlineThreshold = std::chrono::duration_cast<std::chrono::nanoseconds>(0ms).count();
112*38e8c45fSAndroid Build Coastguard Worker     nsecs_t startThreshold = std::chrono::duration_cast<std::chrono::nanoseconds>(2ms).count();
113*38e8c45fSAndroid Build Coastguard Worker };
114*38e8c45fSAndroid Build Coastguard Worker 
115*38e8c45fSAndroid Build Coastguard Worker /*
116*38e8c45fSAndroid Build Coastguard Worker  * TokenManager generates a running number token for a set of predictions made by VsyncPredictor. It
117*38e8c45fSAndroid Build Coastguard Worker  * saves these predictions for a short period of time and returns the predictions for a given token,
118*38e8c45fSAndroid Build Coastguard Worker  * if it hasn't expired.
119*38e8c45fSAndroid Build Coastguard Worker  */
120*38e8c45fSAndroid Build Coastguard Worker class TokenManager {
121*38e8c45fSAndroid Build Coastguard Worker public:
122*38e8c45fSAndroid Build Coastguard Worker     virtual ~TokenManager() = default;
123*38e8c45fSAndroid Build Coastguard Worker 
124*38e8c45fSAndroid Build Coastguard Worker     // Generates a token for the given set of predictions. Stores the predictions for 120ms and
125*38e8c45fSAndroid Build Coastguard Worker     // destroys it later.
126*38e8c45fSAndroid Build Coastguard Worker     virtual int64_t generateTokenForPredictions(TimelineItem&& prediction) = 0;
127*38e8c45fSAndroid Build Coastguard Worker 
128*38e8c45fSAndroid Build Coastguard Worker     // Returns the stored predictions for a given token, if the predictions haven't expired.
129*38e8c45fSAndroid Build Coastguard Worker     virtual std::optional<TimelineItem> getPredictionsForToken(int64_t token) const = 0;
130*38e8c45fSAndroid Build Coastguard Worker };
131*38e8c45fSAndroid Build Coastguard Worker 
132*38e8c45fSAndroid Build Coastguard Worker enum class PredictionState {
133*38e8c45fSAndroid Build Coastguard Worker     Valid,   // Predictions obtained successfully from the TokenManager
134*38e8c45fSAndroid Build Coastguard Worker     Expired, // TokenManager no longer has the predictions
135*38e8c45fSAndroid Build Coastguard Worker     None,    // Predictions are either not present or didn't come from TokenManager
136*38e8c45fSAndroid Build Coastguard Worker };
137*38e8c45fSAndroid Build Coastguard Worker 
138*38e8c45fSAndroid Build Coastguard Worker /*
139*38e8c45fSAndroid Build Coastguard Worker  * Trace cookie is used to send start and end timestamps of <Surface/Display>Frames separately
140*38e8c45fSAndroid Build Coastguard Worker  * without needing to resend all the other information. We send all info to perfetto, along with a
141*38e8c45fSAndroid Build Coastguard Worker  * new cookie, in the start of a frame. For the corresponding end, we just send the same cookie.
142*38e8c45fSAndroid Build Coastguard Worker  * This helps in reducing the amount of data emitted by the producer.
143*38e8c45fSAndroid Build Coastguard Worker  */
144*38e8c45fSAndroid Build Coastguard Worker class TraceCookieCounter {
145*38e8c45fSAndroid Build Coastguard Worker public:
146*38e8c45fSAndroid Build Coastguard Worker     int64_t getCookieForTracing();
147*38e8c45fSAndroid Build Coastguard Worker 
148*38e8c45fSAndroid Build Coastguard Worker private:
149*38e8c45fSAndroid Build Coastguard Worker     // Friend class for testing
150*38e8c45fSAndroid Build Coastguard Worker     friend class android::frametimeline::FrameTimelineTest;
151*38e8c45fSAndroid Build Coastguard Worker 
152*38e8c45fSAndroid Build Coastguard Worker     std::atomic<int64_t> mTraceCookie = 0;
153*38e8c45fSAndroid Build Coastguard Worker };
154*38e8c45fSAndroid Build Coastguard Worker 
155*38e8c45fSAndroid Build Coastguard Worker class SurfaceFrame {
156*38e8c45fSAndroid Build Coastguard Worker public:
157*38e8c45fSAndroid Build Coastguard Worker     enum class PresentState {
158*38e8c45fSAndroid Build Coastguard Worker         Presented, // Buffer was latched and presented by SurfaceFlinger
159*38e8c45fSAndroid Build Coastguard Worker         Dropped,   // Buffer was dropped by SurfaceFlinger
160*38e8c45fSAndroid Build Coastguard Worker         Unknown,   // Initial state, SurfaceFlinger hasn't seen this buffer yet
161*38e8c45fSAndroid Build Coastguard Worker     };
162*38e8c45fSAndroid Build Coastguard Worker 
163*38e8c45fSAndroid Build Coastguard Worker     // Only FrameTimeline can construct a SurfaceFrame as it provides Predictions(through
164*38e8c45fSAndroid Build Coastguard Worker     // TokenManager), Thresholds and TimeStats pointer.
165*38e8c45fSAndroid Build Coastguard Worker     SurfaceFrame(const FrameTimelineInfo& frameTimelineInfo, pid_t ownerPid, uid_t ownerUid,
166*38e8c45fSAndroid Build Coastguard Worker                  int32_t layerId, std::string layerName, std::string debugName,
167*38e8c45fSAndroid Build Coastguard Worker                  PredictionState predictionState, TimelineItem&& predictions,
168*38e8c45fSAndroid Build Coastguard Worker                  std::shared_ptr<TimeStats> timeStats, JankClassificationThresholds thresholds,
169*38e8c45fSAndroid Build Coastguard Worker                  TraceCookieCounter* traceCookieCounter, bool isBuffer, GameMode);
170*38e8c45fSAndroid Build Coastguard Worker     ~SurfaceFrame() = default;
171*38e8c45fSAndroid Build Coastguard Worker 
172*38e8c45fSAndroid Build Coastguard Worker     bool isSelfJanky() const;
173*38e8c45fSAndroid Build Coastguard Worker 
174*38e8c45fSAndroid Build Coastguard Worker     // Returns std::nullopt if the frame hasn't been classified yet.
175*38e8c45fSAndroid Build Coastguard Worker     // Used by both SF and FrameTimeline.
176*38e8c45fSAndroid Build Coastguard Worker     std::optional<int32_t> getJankType() const;
177*38e8c45fSAndroid Build Coastguard Worker     std::optional<JankSeverityType> getJankSeverityType() const;
178*38e8c45fSAndroid Build Coastguard Worker 
179*38e8c45fSAndroid Build Coastguard Worker     // Functions called by SF
getToken()180*38e8c45fSAndroid Build Coastguard Worker     int64_t getToken() const { return mToken; };
getInputEventId()181*38e8c45fSAndroid Build Coastguard Worker     int32_t getInputEventId() const { return mInputEventId; };
getPredictions()182*38e8c45fSAndroid Build Coastguard Worker     TimelineItem getPredictions() const { return mPredictions; };
183*38e8c45fSAndroid Build Coastguard Worker     // Actual timestamps of the app are set individually at different functions.
184*38e8c45fSAndroid Build Coastguard Worker     // Start time (if the app provides) and Queue time are accessible after queueing the frame,
185*38e8c45fSAndroid Build Coastguard Worker     // whereas Acquire Fence time is available only during latch. Drop time is available at the time
186*38e8c45fSAndroid Build Coastguard Worker     // the buffer was dropped.
187*38e8c45fSAndroid Build Coastguard Worker     void setActualStartTime(nsecs_t actualStartTime);
188*38e8c45fSAndroid Build Coastguard Worker     void setActualQueueTime(nsecs_t actualQueueTime);
189*38e8c45fSAndroid Build Coastguard Worker     void setAcquireFenceTime(nsecs_t acquireFenceTime);
190*38e8c45fSAndroid Build Coastguard Worker     void setDesiredPresentTime(nsecs_t desiredPresentTime);
191*38e8c45fSAndroid Build Coastguard Worker     void setDropTime(nsecs_t dropTime);
192*38e8c45fSAndroid Build Coastguard Worker     void setPresentState(PresentState presentState, nsecs_t lastLatchTime = 0);
193*38e8c45fSAndroid Build Coastguard Worker     void setRenderRate(Fps renderRate);
194*38e8c45fSAndroid Build Coastguard Worker     // Return the render rate if it exists, otherwise returns the DisplayFrame's render rate.
195*38e8c45fSAndroid Build Coastguard Worker     Fps getRenderRate() const;
196*38e8c45fSAndroid Build Coastguard Worker     void setGpuComposition();
197*38e8c45fSAndroid Build Coastguard Worker 
198*38e8c45fSAndroid Build Coastguard Worker     // When a bufferless SurfaceFrame is promoted to a buffer SurfaceFrame, we also have to update
199*38e8c45fSAndroid Build Coastguard Worker     // isBuffer.
200*38e8c45fSAndroid Build Coastguard Worker     void promoteToBuffer();
201*38e8c45fSAndroid Build Coastguard Worker 
202*38e8c45fSAndroid Build Coastguard Worker     // Functions called by FrameTimeline
203*38e8c45fSAndroid Build Coastguard Worker     // BaseTime is the smallest timestamp in this SurfaceFrame.
204*38e8c45fSAndroid Build Coastguard Worker     // Used for dumping all timestamps relative to the oldest, making it easy to read.
205*38e8c45fSAndroid Build Coastguard Worker     nsecs_t getBaseTime() const;
206*38e8c45fSAndroid Build Coastguard Worker     // Sets the actual present time, appropriate metadata and classifies the jank.
207*38e8c45fSAndroid Build Coastguard Worker     // displayRefreshRate, displayDeadlineDelta, and displayPresentDelta are propagated from the
208*38e8c45fSAndroid Build Coastguard Worker     // display frame.
209*38e8c45fSAndroid Build Coastguard Worker     void onPresent(nsecs_t presentTime, int32_t displayFrameJankType, Fps refreshRate,
210*38e8c45fSAndroid Build Coastguard Worker                    Fps displayFrameRenderRate, nsecs_t displayDeadlineDelta,
211*38e8c45fSAndroid Build Coastguard Worker                    nsecs_t displayPresentDelta);
212*38e8c45fSAndroid Build Coastguard Worker     // Sets the frame as none janky as there was no real display frame.
213*38e8c45fSAndroid Build Coastguard Worker     void onCommitNotComposited(Fps refreshRate, Fps displayFrameRenderRate);
214*38e8c45fSAndroid Build Coastguard Worker     // All the timestamps are dumped relative to the baseTime
215*38e8c45fSAndroid Build Coastguard Worker     void dump(std::string& result, const std::string& indent, nsecs_t baseTime) const;
216*38e8c45fSAndroid Build Coastguard Worker     // Dumps only the layer, token, is buffer, jank metadata, prediction and present states.
217*38e8c45fSAndroid Build Coastguard Worker     std::string miniDump() const;
218*38e8c45fSAndroid Build Coastguard Worker     // Emits a packet for perfetto tracing. The function body will be executed only if tracing is
219*38e8c45fSAndroid Build Coastguard Worker     // enabled. The displayFrameToken is needed to link the SurfaceFrame to the corresponding
220*38e8c45fSAndroid Build Coastguard Worker     // DisplayFrame at the trace processor side. monoBootOffset is the difference
221*38e8c45fSAndroid Build Coastguard Worker     // between SYSTEM_TIME_BOOTTIME and SYSTEM_TIME_MONOTONIC.
222*38e8c45fSAndroid Build Coastguard Worker     void trace(int64_t displayFrameToken, nsecs_t monoBootOffset,
223*38e8c45fSAndroid Build Coastguard Worker                bool filterFramesBeforeTraceStarts) const;
224*38e8c45fSAndroid Build Coastguard Worker 
225*38e8c45fSAndroid Build Coastguard Worker     // Getter functions used only by FrameTimelineTests and SurfaceFrame internally
226*38e8c45fSAndroid Build Coastguard Worker     TimelineItem getActuals() const;
getOwnerPid()227*38e8c45fSAndroid Build Coastguard Worker     pid_t getOwnerPid() const { return mOwnerPid; };
getLayerId()228*38e8c45fSAndroid Build Coastguard Worker     int32_t getLayerId() const { return mLayerId; };
229*38e8c45fSAndroid Build Coastguard Worker     PredictionState getPredictionState() const;
230*38e8c45fSAndroid Build Coastguard Worker     PresentState getPresentState() const;
231*38e8c45fSAndroid Build Coastguard Worker     FrameReadyMetadata getFrameReadyMetadata() const;
232*38e8c45fSAndroid Build Coastguard Worker     FramePresentMetadata getFramePresentMetadata() const;
233*38e8c45fSAndroid Build Coastguard Worker     nsecs_t getDropTime() const;
234*38e8c45fSAndroid Build Coastguard Worker     bool getIsBuffer() const;
235*38e8c45fSAndroid Build Coastguard Worker 
236*38e8c45fSAndroid Build Coastguard Worker     // For prediction expired frames, this delta is subtracted from the actual end time to get a
237*38e8c45fSAndroid Build Coastguard Worker     // start time decent enough to see in traces.
238*38e8c45fSAndroid Build Coastguard Worker     // TODO(b/172587309): Remove this when we have actual start times.
239*38e8c45fSAndroid Build Coastguard Worker     static constexpr nsecs_t kPredictionExpiredStartTimeDelta =
240*38e8c45fSAndroid Build Coastguard Worker             std::chrono::duration_cast<std::chrono::nanoseconds>(2ms).count();
241*38e8c45fSAndroid Build Coastguard Worker 
242*38e8c45fSAndroid Build Coastguard Worker private:
243*38e8c45fSAndroid Build Coastguard Worker     void tracePredictions(int64_t displayFrameToken, nsecs_t monoBootOffset,
244*38e8c45fSAndroid Build Coastguard Worker                           bool filterFramesBeforeTraceStarts) const;
245*38e8c45fSAndroid Build Coastguard Worker     void traceActuals(int64_t displayFrameToken, nsecs_t monoBootOffset,
246*38e8c45fSAndroid Build Coastguard Worker                       bool filterFramesBeforeTraceStarts) const;
247*38e8c45fSAndroid Build Coastguard Worker     void classifyJankLocked(int32_t displayFrameJankType, const Fps& refreshRate,
248*38e8c45fSAndroid Build Coastguard Worker                             Fps displayFrameRenderRate, nsecs_t* outDeadlineDelta) REQUIRES(mMutex);
249*38e8c45fSAndroid Build Coastguard Worker 
250*38e8c45fSAndroid Build Coastguard Worker     const int64_t mToken;
251*38e8c45fSAndroid Build Coastguard Worker     const int32_t mInputEventId;
252*38e8c45fSAndroid Build Coastguard Worker     const pid_t mOwnerPid;
253*38e8c45fSAndroid Build Coastguard Worker     const uid_t mOwnerUid;
254*38e8c45fSAndroid Build Coastguard Worker     const std::string mLayerName;
255*38e8c45fSAndroid Build Coastguard Worker     const std::string mDebugName;
256*38e8c45fSAndroid Build Coastguard Worker     const int32_t mLayerId;
257*38e8c45fSAndroid Build Coastguard Worker     PresentState mPresentState GUARDED_BY(mMutex);
258*38e8c45fSAndroid Build Coastguard Worker     const PredictionState mPredictionState;
259*38e8c45fSAndroid Build Coastguard Worker     const TimelineItem mPredictions;
260*38e8c45fSAndroid Build Coastguard Worker     TimelineItem mActuals GUARDED_BY(mMutex);
261*38e8c45fSAndroid Build Coastguard Worker     std::shared_ptr<TimeStats> mTimeStats;
262*38e8c45fSAndroid Build Coastguard Worker     const JankClassificationThresholds mJankClassificationThresholds;
263*38e8c45fSAndroid Build Coastguard Worker     nsecs_t mActualQueueTime GUARDED_BY(mMutex) = 0;
264*38e8c45fSAndroid Build Coastguard Worker     nsecs_t mDropTime GUARDED_BY(mMutex) = 0;
265*38e8c45fSAndroid Build Coastguard Worker     mutable std::mutex mMutex;
266*38e8c45fSAndroid Build Coastguard Worker     // Bitmask for the type of jank
267*38e8c45fSAndroid Build Coastguard Worker     int32_t mJankType GUARDED_BY(mMutex) = JankType::None;
268*38e8c45fSAndroid Build Coastguard Worker     // Enum for the severity of jank
269*38e8c45fSAndroid Build Coastguard Worker     JankSeverityType mJankSeverityType GUARDED_BY(mMutex) = JankSeverityType::None;
270*38e8c45fSAndroid Build Coastguard Worker     // Indicates if this frame was composited by the GPU or not
271*38e8c45fSAndroid Build Coastguard Worker     bool mGpuComposition GUARDED_BY(mMutex) = false;
272*38e8c45fSAndroid Build Coastguard Worker     // Refresh rate for this frame.
273*38e8c45fSAndroid Build Coastguard Worker     Fps mDisplayFrameRenderRate GUARDED_BY(mMutex);
274*38e8c45fSAndroid Build Coastguard Worker     // Rendering rate for this frame.
275*38e8c45fSAndroid Build Coastguard Worker     std::optional<Fps> mRenderRate GUARDED_BY(mMutex);
276*38e8c45fSAndroid Build Coastguard Worker     // Enum for the type of present
277*38e8c45fSAndroid Build Coastguard Worker     FramePresentMetadata mFramePresentMetadata GUARDED_BY(mMutex) =
278*38e8c45fSAndroid Build Coastguard Worker             FramePresentMetadata::UnknownPresent;
279*38e8c45fSAndroid Build Coastguard Worker     // Enum for the type of finish
280*38e8c45fSAndroid Build Coastguard Worker     FrameReadyMetadata mFrameReadyMetadata GUARDED_BY(mMutex) = FrameReadyMetadata::UnknownFinish;
281*38e8c45fSAndroid Build Coastguard Worker     // Time when the previous buffer from the same layer was latched by SF. This is used in checking
282*38e8c45fSAndroid Build Coastguard Worker     // for BufferStuffing where the current buffer is expected to be ready but the previous buffer
283*38e8c45fSAndroid Build Coastguard Worker     // was latched instead.
284*38e8c45fSAndroid Build Coastguard Worker     nsecs_t mLastLatchTime GUARDED_BY(mMutex) = 0;
285*38e8c45fSAndroid Build Coastguard Worker     // TraceCookieCounter is used to obtain the cookie for sendig trace packets to perfetto. Using a
286*38e8c45fSAndroid Build Coastguard Worker     // reference here because the counter is owned by FrameTimeline, which outlives SurfaceFrame.
287*38e8c45fSAndroid Build Coastguard Worker     TraceCookieCounter& mTraceCookieCounter;
288*38e8c45fSAndroid Build Coastguard Worker     // Tells if the SurfaceFrame is representing a buffer or a transaction without a
289*38e8c45fSAndroid Build Coastguard Worker     // buffer(animations)
290*38e8c45fSAndroid Build Coastguard Worker     bool mIsBuffer;
291*38e8c45fSAndroid Build Coastguard Worker     // GameMode from the layer. Used in metrics.
292*38e8c45fSAndroid Build Coastguard Worker     GameMode mGameMode = GameMode::Unsupported;
293*38e8c45fSAndroid Build Coastguard Worker };
294*38e8c45fSAndroid Build Coastguard Worker 
295*38e8c45fSAndroid Build Coastguard Worker /*
296*38e8c45fSAndroid Build Coastguard Worker  * Maintains a history of SurfaceFrames grouped together by the vsync time in which they were
297*38e8c45fSAndroid Build Coastguard Worker  * presented
298*38e8c45fSAndroid Build Coastguard Worker  */
299*38e8c45fSAndroid Build Coastguard Worker class FrameTimeline {
300*38e8c45fSAndroid Build Coastguard Worker public:
301*38e8c45fSAndroid Build Coastguard Worker     virtual ~FrameTimeline() = default;
302*38e8c45fSAndroid Build Coastguard Worker     virtual TokenManager* getTokenManager() = 0;
303*38e8c45fSAndroid Build Coastguard Worker 
304*38e8c45fSAndroid Build Coastguard Worker     // Initializes the Perfetto DataSource that emits DisplayFrame and SurfaceFrame events. Test
305*38e8c45fSAndroid Build Coastguard Worker     // classes can avoid double registration by mocking this function.
306*38e8c45fSAndroid Build Coastguard Worker     virtual void onBootFinished() = 0;
307*38e8c45fSAndroid Build Coastguard Worker 
308*38e8c45fSAndroid Build Coastguard Worker     // Create a new surface frame, set the predictions based on a token and return it to the caller.
309*38e8c45fSAndroid Build Coastguard Worker     // Debug name is the human-readable debugging string for dumpsys.
310*38e8c45fSAndroid Build Coastguard Worker     virtual std::shared_ptr<SurfaceFrame> createSurfaceFrameForToken(
311*38e8c45fSAndroid Build Coastguard Worker             const FrameTimelineInfo& frameTimelineInfo, pid_t ownerPid, uid_t ownerUid,
312*38e8c45fSAndroid Build Coastguard Worker             int32_t layerId, std::string layerName, std::string debugName, bool isBuffer,
313*38e8c45fSAndroid Build Coastguard Worker             GameMode) = 0;
314*38e8c45fSAndroid Build Coastguard Worker 
315*38e8c45fSAndroid Build Coastguard Worker     // Adds a new SurfaceFrame to the current DisplayFrame. Frames from multiple layers can be
316*38e8c45fSAndroid Build Coastguard Worker     // composited into one display frame.
317*38e8c45fSAndroid Build Coastguard Worker     virtual void addSurfaceFrame(std::shared_ptr<SurfaceFrame> surfaceFrame) = 0;
318*38e8c45fSAndroid Build Coastguard Worker 
319*38e8c45fSAndroid Build Coastguard Worker     // The first function called by SF for the current DisplayFrame. Fetches SF predictions based on
320*38e8c45fSAndroid Build Coastguard Worker     // the token and sets the actualSfWakeTime for the current DisplayFrame.
321*38e8c45fSAndroid Build Coastguard Worker     virtual void setSfWakeUp(int64_t token, nsecs_t wakeupTime, Fps refreshRate,
322*38e8c45fSAndroid Build Coastguard Worker                              Fps renderRate) = 0;
323*38e8c45fSAndroid Build Coastguard Worker 
324*38e8c45fSAndroid Build Coastguard Worker     // Sets the sfPresentTime and finalizes the current DisplayFrame. Tracks the
325*38e8c45fSAndroid Build Coastguard Worker     // given present fence until it's signaled, and updates the present timestamps of all presented
326*38e8c45fSAndroid Build Coastguard Worker     // SurfaceFrames in that vsync. If a gpuFence was also provided, its tracked in the
327*38e8c45fSAndroid Build Coastguard Worker     // corresponding DisplayFrame.
328*38e8c45fSAndroid Build Coastguard Worker     virtual void setSfPresent(nsecs_t sfPresentTime, const std::shared_ptr<FenceTime>& presentFence,
329*38e8c45fSAndroid Build Coastguard Worker                               const std::shared_ptr<FenceTime>& gpuFence) = 0;
330*38e8c45fSAndroid Build Coastguard Worker 
331*38e8c45fSAndroid Build Coastguard Worker     // Provides surface frames that have already been jank classified in the most recent
332*38e8c45fSAndroid Build Coastguard Worker     // flush of pending present fences. This allows buffer stuffing detection from SF.
333*38e8c45fSAndroid Build Coastguard Worker     virtual const std::vector<std::shared_ptr<frametimeline::SurfaceFrame>>& getPresentFrames()
334*38e8c45fSAndroid Build Coastguard Worker             const = 0;
335*38e8c45fSAndroid Build Coastguard Worker 
336*38e8c45fSAndroid Build Coastguard Worker     // Tells FrameTimeline that a frame was committed but not composited. This is used to flush
337*38e8c45fSAndroid Build Coastguard Worker     // all the associated surface frames.
338*38e8c45fSAndroid Build Coastguard Worker     virtual void onCommitNotComposited() = 0;
339*38e8c45fSAndroid Build Coastguard Worker 
340*38e8c45fSAndroid Build Coastguard Worker     // Args:
341*38e8c45fSAndroid Build Coastguard Worker     // -jank : Dumps only the Display Frames that are either janky themselves
342*38e8c45fSAndroid Build Coastguard Worker     //         or contain janky Surface Frames.
343*38e8c45fSAndroid Build Coastguard Worker     // -all : Dumps the entire list of DisplayFrames and the SurfaceFrames contained within
344*38e8c45fSAndroid Build Coastguard Worker     virtual void parseArgs(const Vector<String16>& args, std::string& result) = 0;
345*38e8c45fSAndroid Build Coastguard Worker 
346*38e8c45fSAndroid Build Coastguard Worker     // Sets the max number of display frames that can be stored. Called by SF backdoor.
347*38e8c45fSAndroid Build Coastguard Worker     virtual void setMaxDisplayFrames(uint32_t size) = 0;
348*38e8c45fSAndroid Build Coastguard Worker 
349*38e8c45fSAndroid Build Coastguard Worker     // Computes the historical fps for the provided set of layer IDs
350*38e8c45fSAndroid Build Coastguard Worker     // The fps is compted from the linear timeline of present timestamps for DisplayFrames
351*38e8c45fSAndroid Build Coastguard Worker     // containing at least one layer ID.
352*38e8c45fSAndroid Build Coastguard Worker     virtual float computeFps(const std::unordered_set<int32_t>& layerIds) = 0;
353*38e8c45fSAndroid Build Coastguard Worker 
354*38e8c45fSAndroid Build Coastguard Worker     // Supports the legacy FrameStats interface
355*38e8c45fSAndroid Build Coastguard Worker     virtual void generateFrameStats(int32_t layer, size_t count, FrameStats* outStats) const = 0;
356*38e8c45fSAndroid Build Coastguard Worker 
357*38e8c45fSAndroid Build Coastguard Worker     // Restores the max number of display frames to default. Called by SF backdoor.
358*38e8c45fSAndroid Build Coastguard Worker     virtual void reset() = 0;
359*38e8c45fSAndroid Build Coastguard Worker };
360*38e8c45fSAndroid Build Coastguard Worker 
361*38e8c45fSAndroid Build Coastguard Worker namespace impl {
362*38e8c45fSAndroid Build Coastguard Worker 
363*38e8c45fSAndroid Build Coastguard Worker class TokenManager : public android::frametimeline::TokenManager {
364*38e8c45fSAndroid Build Coastguard Worker public:
TokenManager()365*38e8c45fSAndroid Build Coastguard Worker     TokenManager() : mCurrentToken(FrameTimelineInfo::INVALID_VSYNC_ID + 1) {}
366*38e8c45fSAndroid Build Coastguard Worker     ~TokenManager() = default;
367*38e8c45fSAndroid Build Coastguard Worker 
368*38e8c45fSAndroid Build Coastguard Worker     int64_t generateTokenForPredictions(TimelineItem&& predictions) override;
369*38e8c45fSAndroid Build Coastguard Worker     std::optional<TimelineItem> getPredictionsForToken(int64_t token) const override;
370*38e8c45fSAndroid Build Coastguard Worker 
371*38e8c45fSAndroid Build Coastguard Worker private:
372*38e8c45fSAndroid Build Coastguard Worker     // Friend class for testing
373*38e8c45fSAndroid Build Coastguard Worker     friend class android::frametimeline::FrameTimelineTest;
374*38e8c45fSAndroid Build Coastguard Worker 
375*38e8c45fSAndroid Build Coastguard Worker     void flushTokens(nsecs_t flushTime) REQUIRES(mMutex);
376*38e8c45fSAndroid Build Coastguard Worker 
377*38e8c45fSAndroid Build Coastguard Worker     std::map<int64_t, TimelineItem> mPredictions GUARDED_BY(mMutex);
378*38e8c45fSAndroid Build Coastguard Worker     int64_t mCurrentToken GUARDED_BY(mMutex);
379*38e8c45fSAndroid Build Coastguard Worker     mutable std::mutex mMutex;
380*38e8c45fSAndroid Build Coastguard Worker     static constexpr size_t kMaxTokens = 500;
381*38e8c45fSAndroid Build Coastguard Worker };
382*38e8c45fSAndroid Build Coastguard Worker 
383*38e8c45fSAndroid Build Coastguard Worker class FrameTimeline : public android::frametimeline::FrameTimeline {
384*38e8c45fSAndroid Build Coastguard Worker public:
385*38e8c45fSAndroid Build Coastguard Worker     class FrameTimelineDataSource : public perfetto::DataSource<FrameTimelineDataSource> {
386*38e8c45fSAndroid Build Coastguard Worker     public:
getStartTime()387*38e8c45fSAndroid Build Coastguard Worker         nsecs_t getStartTime() const { return mTraceStartTime; }
388*38e8c45fSAndroid Build Coastguard Worker 
389*38e8c45fSAndroid Build Coastguard Worker     private:
OnSetup(const SetupArgs &)390*38e8c45fSAndroid Build Coastguard Worker         void OnSetup(const SetupArgs&) override {};
OnStart(const StartArgs &)391*38e8c45fSAndroid Build Coastguard Worker         void OnStart(const StartArgs&) override { mTraceStartTime = systemTime(); };
OnStop(const StopArgs &)392*38e8c45fSAndroid Build Coastguard Worker         void OnStop(const StopArgs&) override {};
393*38e8c45fSAndroid Build Coastguard Worker 
394*38e8c45fSAndroid Build Coastguard Worker         nsecs_t mTraceStartTime = 0;
395*38e8c45fSAndroid Build Coastguard Worker     };
396*38e8c45fSAndroid Build Coastguard Worker 
397*38e8c45fSAndroid Build Coastguard Worker     /*
398*38e8c45fSAndroid Build Coastguard Worker      * DisplayFrame should be used only internally within FrameTimeline. All members and methods are
399*38e8c45fSAndroid Build Coastguard Worker      * guarded by FrameTimeline's mMutex.
400*38e8c45fSAndroid Build Coastguard Worker      */
401*38e8c45fSAndroid Build Coastguard Worker     class DisplayFrame {
402*38e8c45fSAndroid Build Coastguard Worker     public:
403*38e8c45fSAndroid Build Coastguard Worker         DisplayFrame(std::shared_ptr<TimeStats> timeStats, JankClassificationThresholds thresholds,
404*38e8c45fSAndroid Build Coastguard Worker                      TraceCookieCounter* traceCookieCounter);
405*38e8c45fSAndroid Build Coastguard Worker         virtual ~DisplayFrame() = default;
406*38e8c45fSAndroid Build Coastguard Worker         // Dumpsys interface - dumps only if the DisplayFrame itself is janky or is at least one
407*38e8c45fSAndroid Build Coastguard Worker         // SurfaceFrame is janky.
408*38e8c45fSAndroid Build Coastguard Worker         void dumpJank(std::string& result, nsecs_t baseTime, int displayFrameCount) const;
409*38e8c45fSAndroid Build Coastguard Worker         // Dumpsys interface - dumps all data irrespective of jank
410*38e8c45fSAndroid Build Coastguard Worker         void dumpAll(std::string& result, nsecs_t baseTime) const;
411*38e8c45fSAndroid Build Coastguard Worker         // Emits a packet for perfetto tracing. The function body will be executed only if tracing
412*38e8c45fSAndroid Build Coastguard Worker         // is enabled. monoBootOffset is the difference between SYSTEM_TIME_BOOTTIME
413*38e8c45fSAndroid Build Coastguard Worker         // and SYSTEM_TIME_MONOTONIC.
414*38e8c45fSAndroid Build Coastguard Worker         nsecs_t trace(pid_t surfaceFlingerPid, nsecs_t monoBootOffset,
415*38e8c45fSAndroid Build Coastguard Worker                       nsecs_t previousPredictionPresentTime,
416*38e8c45fSAndroid Build Coastguard Worker                       bool filterFramesBeforeTraceStarts) const;
417*38e8c45fSAndroid Build Coastguard Worker         // Sets the token, vsyncPeriod, predictions and SF start time.
418*38e8c45fSAndroid Build Coastguard Worker         void onSfWakeUp(int64_t token, Fps refreshRate, Fps renderRate,
419*38e8c45fSAndroid Build Coastguard Worker                         std::optional<TimelineItem> predictions, nsecs_t wakeUpTime);
420*38e8c45fSAndroid Build Coastguard Worker         // Sets the appropriate metadata and classifies the jank.
421*38e8c45fSAndroid Build Coastguard Worker         void onPresent(nsecs_t signalTime, nsecs_t previousPresentTime);
422*38e8c45fSAndroid Build Coastguard Worker         // Flushes all the surface frames as those were not generating any actual display frames.
423*38e8c45fSAndroid Build Coastguard Worker         void onCommitNotComposited();
424*38e8c45fSAndroid Build Coastguard Worker         // Adds the provided SurfaceFrame to the current display frame.
425*38e8c45fSAndroid Build Coastguard Worker         void addSurfaceFrame(std::shared_ptr<SurfaceFrame> surfaceFrame);
426*38e8c45fSAndroid Build Coastguard Worker 
427*38e8c45fSAndroid Build Coastguard Worker         void setPredictions(PredictionState predictionState, TimelineItem predictions);
428*38e8c45fSAndroid Build Coastguard Worker         void setActualStartTime(nsecs_t actualStartTime);
429*38e8c45fSAndroid Build Coastguard Worker         void setActualEndTime(nsecs_t actualEndTime);
430*38e8c45fSAndroid Build Coastguard Worker         void setGpuFence(const std::shared_ptr<FenceTime>& gpuFence);
431*38e8c45fSAndroid Build Coastguard Worker 
432*38e8c45fSAndroid Build Coastguard Worker         // BaseTime is the smallest timestamp in a DisplayFrame.
433*38e8c45fSAndroid Build Coastguard Worker         // Used for dumping all timestamps relative to the oldest, making it easy to read.
434*38e8c45fSAndroid Build Coastguard Worker         nsecs_t getBaseTime() const;
435*38e8c45fSAndroid Build Coastguard Worker 
436*38e8c45fSAndroid Build Coastguard Worker         // Functions to be used only in testing.
getActuals()437*38e8c45fSAndroid Build Coastguard Worker         TimelineItem getActuals() const { return mSurfaceFlingerActuals; };
getPredictions()438*38e8c45fSAndroid Build Coastguard Worker         TimelineItem getPredictions() const { return mSurfaceFlingerPredictions; };
getFrameStartMetadata()439*38e8c45fSAndroid Build Coastguard Worker         FrameStartMetadata getFrameStartMetadata() const { return mFrameStartMetadata; };
getFramePresentMetadata()440*38e8c45fSAndroid Build Coastguard Worker         FramePresentMetadata getFramePresentMetadata() const { return mFramePresentMetadata; };
getFrameReadyMetadata()441*38e8c45fSAndroid Build Coastguard Worker         FrameReadyMetadata getFrameReadyMetadata() const { return mFrameReadyMetadata; };
getJankType()442*38e8c45fSAndroid Build Coastguard Worker         int32_t getJankType() const { return mJankType; }
getJankSeverityType()443*38e8c45fSAndroid Build Coastguard Worker         JankSeverityType getJankSeverityType() const { return mJankSeverityType; }
getSurfaceFrames()444*38e8c45fSAndroid Build Coastguard Worker         const std::vector<std::shared_ptr<SurfaceFrame>>& getSurfaceFrames() const {
445*38e8c45fSAndroid Build Coastguard Worker             return mSurfaceFrames;
446*38e8c45fSAndroid Build Coastguard Worker         }
447*38e8c45fSAndroid Build Coastguard Worker 
448*38e8c45fSAndroid Build Coastguard Worker     private:
449*38e8c45fSAndroid Build Coastguard Worker         void dump(std::string& result, nsecs_t baseTime) const;
450*38e8c45fSAndroid Build Coastguard Worker         void tracePredictions(pid_t surfaceFlingerPid, nsecs_t monoBootOffset,
451*38e8c45fSAndroid Build Coastguard Worker                               bool filterFramesBeforeTraceStarts) const;
452*38e8c45fSAndroid Build Coastguard Worker         void traceActuals(pid_t surfaceFlingerPid, nsecs_t monoBootOffset,
453*38e8c45fSAndroid Build Coastguard Worker                           bool filterFramesBeforeTraceStarts) const;
454*38e8c45fSAndroid Build Coastguard Worker         void addSkippedFrame(pid_t surfaceFlingerPid, nsecs_t monoBootOffset,
455*38e8c45fSAndroid Build Coastguard Worker                              nsecs_t previousActualPresentTime,
456*38e8c45fSAndroid Build Coastguard Worker                              bool filterFramesBeforeTraceStarts) const;
457*38e8c45fSAndroid Build Coastguard Worker         void classifyJank(nsecs_t& deadlineDelta, nsecs_t& deltaToVsync,
458*38e8c45fSAndroid Build Coastguard Worker                           nsecs_t previousPresentTime);
459*38e8c45fSAndroid Build Coastguard Worker 
460*38e8c45fSAndroid Build Coastguard Worker         int64_t mToken = FrameTimelineInfo::INVALID_VSYNC_ID;
461*38e8c45fSAndroid Build Coastguard Worker 
462*38e8c45fSAndroid Build Coastguard Worker         /* Usage of TimelineItem w.r.t SurfaceFlinger
463*38e8c45fSAndroid Build Coastguard Worker          * startTime    Time when SurfaceFlinger wakes up to handle transactions and buffer updates
464*38e8c45fSAndroid Build Coastguard Worker          * endTime      Time when SurfaceFlinger sends a composited frame to Display
465*38e8c45fSAndroid Build Coastguard Worker          * presentTime  Time when the composited frame was presented on screen
466*38e8c45fSAndroid Build Coastguard Worker          */
467*38e8c45fSAndroid Build Coastguard Worker         TimelineItem mSurfaceFlingerPredictions;
468*38e8c45fSAndroid Build Coastguard Worker         TimelineItem mSurfaceFlingerActuals;
469*38e8c45fSAndroid Build Coastguard Worker         std::shared_ptr<TimeStats> mTimeStats;
470*38e8c45fSAndroid Build Coastguard Worker         const JankClassificationThresholds mJankClassificationThresholds;
471*38e8c45fSAndroid Build Coastguard Worker 
472*38e8c45fSAndroid Build Coastguard Worker         // Collection of predictions and actual values sent over by Layers
473*38e8c45fSAndroid Build Coastguard Worker         std::vector<std::shared_ptr<SurfaceFrame>> mSurfaceFrames;
474*38e8c45fSAndroid Build Coastguard Worker 
475*38e8c45fSAndroid Build Coastguard Worker         PredictionState mPredictionState = PredictionState::None;
476*38e8c45fSAndroid Build Coastguard Worker         // Bitmask for the type of jank
477*38e8c45fSAndroid Build Coastguard Worker         int32_t mJankType = JankType::None;
478*38e8c45fSAndroid Build Coastguard Worker         // Enum for the severity of jank
479*38e8c45fSAndroid Build Coastguard Worker         JankSeverityType mJankSeverityType = JankSeverityType::None;
480*38e8c45fSAndroid Build Coastguard Worker         // A valid gpu fence indicates that the DisplayFrame was composited by the GPU
481*38e8c45fSAndroid Build Coastguard Worker         std::shared_ptr<FenceTime> mGpuFence = FenceTime::NO_FENCE;
482*38e8c45fSAndroid Build Coastguard Worker         // Enum for the type of present
483*38e8c45fSAndroid Build Coastguard Worker         FramePresentMetadata mFramePresentMetadata = FramePresentMetadata::UnknownPresent;
484*38e8c45fSAndroid Build Coastguard Worker         // Enum for the type of finish
485*38e8c45fSAndroid Build Coastguard Worker         FrameReadyMetadata mFrameReadyMetadata = FrameReadyMetadata::UnknownFinish;
486*38e8c45fSAndroid Build Coastguard Worker         // Enum for the type of start
487*38e8c45fSAndroid Build Coastguard Worker         FrameStartMetadata mFrameStartMetadata = FrameStartMetadata::UnknownStart;
488*38e8c45fSAndroid Build Coastguard Worker         // The refresh rate (vsync period) in nanoseconds as seen by SF during this DisplayFrame's
489*38e8c45fSAndroid Build Coastguard Worker         // timeline
490*38e8c45fSAndroid Build Coastguard Worker         Fps mRefreshRate;
491*38e8c45fSAndroid Build Coastguard Worker         // The current render rate for this DisplayFrame.
492*38e8c45fSAndroid Build Coastguard Worker         Fps mRenderRate;
493*38e8c45fSAndroid Build Coastguard Worker         // TraceCookieCounter is used to obtain the cookie for sendig trace packets to perfetto.
494*38e8c45fSAndroid Build Coastguard Worker         // Using a reference here because the counter is owned by FrameTimeline, which outlives
495*38e8c45fSAndroid Build Coastguard Worker         // DisplayFrame.
496*38e8c45fSAndroid Build Coastguard Worker         TraceCookieCounter& mTraceCookieCounter;
497*38e8c45fSAndroid Build Coastguard Worker     };
498*38e8c45fSAndroid Build Coastguard Worker 
499*38e8c45fSAndroid Build Coastguard Worker     FrameTimeline(std::shared_ptr<TimeStats> timeStats, pid_t surfaceFlingerPid,
500*38e8c45fSAndroid Build Coastguard Worker                   JankClassificationThresholds thresholds = {}, bool useBootTimeClock = true,
501*38e8c45fSAndroid Build Coastguard Worker                   bool filterFramesBeforeTraceStarts = true);
502*38e8c45fSAndroid Build Coastguard Worker     ~FrameTimeline() = default;
503*38e8c45fSAndroid Build Coastguard Worker 
getTokenManager()504*38e8c45fSAndroid Build Coastguard Worker     frametimeline::TokenManager* getTokenManager() override { return &mTokenManager; }
505*38e8c45fSAndroid Build Coastguard Worker     std::shared_ptr<SurfaceFrame> createSurfaceFrameForToken(
506*38e8c45fSAndroid Build Coastguard Worker             const FrameTimelineInfo& frameTimelineInfo, pid_t ownerPid, uid_t ownerUid,
507*38e8c45fSAndroid Build Coastguard Worker             int32_t layerId, std::string layerName, std::string debugName, bool isBuffer,
508*38e8c45fSAndroid Build Coastguard Worker             GameMode) override;
509*38e8c45fSAndroid Build Coastguard Worker     void addSurfaceFrame(std::shared_ptr<frametimeline::SurfaceFrame> surfaceFrame) override;
510*38e8c45fSAndroid Build Coastguard Worker     void setSfWakeUp(int64_t token, nsecs_t wakeupTime, Fps refreshRate, Fps renderRate) override;
511*38e8c45fSAndroid Build Coastguard Worker     void setSfPresent(nsecs_t sfPresentTime, const std::shared_ptr<FenceTime>& presentFence,
512*38e8c45fSAndroid Build Coastguard Worker                       const std::shared_ptr<FenceTime>& gpuFence = FenceTime::NO_FENCE) override;
513*38e8c45fSAndroid Build Coastguard Worker     const std::vector<std::shared_ptr<frametimeline::SurfaceFrame>>& getPresentFrames()
514*38e8c45fSAndroid Build Coastguard Worker             const override;
515*38e8c45fSAndroid Build Coastguard Worker     void onCommitNotComposited() override;
516*38e8c45fSAndroid Build Coastguard Worker     void parseArgs(const Vector<String16>& args, std::string& result) override;
517*38e8c45fSAndroid Build Coastguard Worker     void setMaxDisplayFrames(uint32_t size) override;
518*38e8c45fSAndroid Build Coastguard Worker     float computeFps(const std::unordered_set<int32_t>& layerIds) override;
519*38e8c45fSAndroid Build Coastguard Worker     void generateFrameStats(int32_t layer, size_t count, FrameStats* outStats) const override;
520*38e8c45fSAndroid Build Coastguard Worker     void reset() override;
521*38e8c45fSAndroid Build Coastguard Worker 
522*38e8c45fSAndroid Build Coastguard Worker     // Sets up the perfetto tracing backend and data source.
523*38e8c45fSAndroid Build Coastguard Worker     void onBootFinished() override;
524*38e8c45fSAndroid Build Coastguard Worker     // Registers the data source with the perfetto backend. Called as part of onBootFinished()
525*38e8c45fSAndroid Build Coastguard Worker     // and should not be called manually outside of tests.
526*38e8c45fSAndroid Build Coastguard Worker     void registerDataSource();
527*38e8c45fSAndroid Build Coastguard Worker 
528*38e8c45fSAndroid Build Coastguard Worker     static constexpr char kFrameTimelineDataSource[] = "android.surfaceflinger.frametimeline";
529*38e8c45fSAndroid Build Coastguard Worker 
530*38e8c45fSAndroid Build Coastguard Worker private:
531*38e8c45fSAndroid Build Coastguard Worker     // Friend class for testing
532*38e8c45fSAndroid Build Coastguard Worker     friend class android::frametimeline::FrameTimelineTest;
533*38e8c45fSAndroid Build Coastguard Worker 
534*38e8c45fSAndroid Build Coastguard Worker     void flushPendingPresentFences() REQUIRES(mMutex);
535*38e8c45fSAndroid Build Coastguard Worker     std::optional<size_t> getFirstSignalFenceIndex() const REQUIRES(mMutex);
536*38e8c45fSAndroid Build Coastguard Worker     void finalizeCurrentDisplayFrame() REQUIRES(mMutex);
537*38e8c45fSAndroid Build Coastguard Worker     void dumpAll(std::string& result);
538*38e8c45fSAndroid Build Coastguard Worker     void dumpJank(std::string& result);
539*38e8c45fSAndroid Build Coastguard Worker 
540*38e8c45fSAndroid Build Coastguard Worker     // Sliding window of display frames. TODO(b/168072834): compare perf with fixed size array
541*38e8c45fSAndroid Build Coastguard Worker     std::deque<std::shared_ptr<DisplayFrame>> mDisplayFrames GUARDED_BY(mMutex);
542*38e8c45fSAndroid Build Coastguard Worker     std::vector<std::pair<std::shared_ptr<FenceTime>, std::shared_ptr<DisplayFrame>>>
543*38e8c45fSAndroid Build Coastguard Worker             mPendingPresentFences GUARDED_BY(mMutex);
544*38e8c45fSAndroid Build Coastguard Worker     std::shared_ptr<DisplayFrame> mCurrentDisplayFrame GUARDED_BY(mMutex);
545*38e8c45fSAndroid Build Coastguard Worker     TokenManager mTokenManager;
546*38e8c45fSAndroid Build Coastguard Worker     TraceCookieCounter mTraceCookieCounter;
547*38e8c45fSAndroid Build Coastguard Worker     mutable std::mutex mMutex;
548*38e8c45fSAndroid Build Coastguard Worker     const bool mUseBootTimeClock;
549*38e8c45fSAndroid Build Coastguard Worker     const bool mFilterFramesBeforeTraceStarts;
550*38e8c45fSAndroid Build Coastguard Worker     uint32_t mMaxDisplayFrames;
551*38e8c45fSAndroid Build Coastguard Worker     std::shared_ptr<TimeStats> mTimeStats;
552*38e8c45fSAndroid Build Coastguard Worker     const pid_t mSurfaceFlingerPid;
553*38e8c45fSAndroid Build Coastguard Worker     nsecs_t mPreviousActualPresentTime = 0;
554*38e8c45fSAndroid Build Coastguard Worker     nsecs_t mPreviousPredictionPresentTime = 0;
555*38e8c45fSAndroid Build Coastguard Worker     const JankClassificationThresholds mJankClassificationThresholds;
556*38e8c45fSAndroid Build Coastguard Worker     static constexpr uint32_t kDefaultMaxDisplayFrames = 64;
557*38e8c45fSAndroid Build Coastguard Worker     // The initial container size for the vector<SurfaceFrames> inside display frame. Although
558*38e8c45fSAndroid Build Coastguard Worker     // this number doesn't represent any bounds on the number of surface frames that can go in a
559*38e8c45fSAndroid Build Coastguard Worker     // display frame, this is a good starting size for the vector so that we can avoid the
560*38e8c45fSAndroid Build Coastguard Worker     // internal vector resizing that happens with push_back.
561*38e8c45fSAndroid Build Coastguard Worker     static constexpr uint32_t kNumSurfaceFramesInitial = 10;
562*38e8c45fSAndroid Build Coastguard Worker     // Presented surface frames that have been jank classified and can
563*38e8c45fSAndroid Build Coastguard Worker     // indicate of potential buffer stuffing.
564*38e8c45fSAndroid Build Coastguard Worker     std::vector<std::shared_ptr<frametimeline::SurfaceFrame>> mPresentFrames;
565*38e8c45fSAndroid Build Coastguard Worker };
566*38e8c45fSAndroid Build Coastguard Worker 
567*38e8c45fSAndroid Build Coastguard Worker } // namespace impl
568*38e8c45fSAndroid Build Coastguard Worker } // namespace android::frametimeline
569