blob: cfe81704b3d2b2236cf620fe1ee298f821db0402 [file] [log] [blame]
Adithya Srinivasanf279e042020-08-17 14:56:27 -07001/*
2 * Copyright 2020 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#pragma once
18
19#include <deque>
20#include <mutex>
21
Ady Abraham22c7b5c2020-09-22 19:33:40 -070022#include <gui/ISurfaceComposer.h>
Adithya Srinivasanf279e042020-08-17 14:56:27 -070023#include <ui/FenceTime.h>
24#include <utils/RefBase.h>
25#include <utils/Timers.h>
26
27namespace android::frametimeline {
28
29class FrameTimelineTest;
30
31/*
32 * Collection of timestamps that can be used for both predictions and actual times.
33 */
34struct TimelineItem {
35 TimelineItem(const nsecs_t startTime = 0, const nsecs_t endTime = 0,
36 const nsecs_t presentTime = 0)
37 : startTime(startTime), endTime(endTime), presentTime(presentTime) {}
38
39 nsecs_t startTime;
40 nsecs_t endTime;
41 nsecs_t presentTime;
42};
43
44/*
45 * TokenManager generates a running number token for a set of predictions made by VsyncPredictor. It
46 * saves these predictions for a short period of time and returns the predictions for a given token,
47 * if it hasn't expired.
48 */
49class TokenManager {
50public:
51 virtual ~TokenManager() = default;
52
53 // Generates a token for the given set of predictions. Stores the predictions for 120ms and
54 // destroys it later.
Adithya Srinivasan5f683cf2020-09-15 14:21:04 -070055 virtual int64_t generateTokenForPredictions(TimelineItem&& prediction) = 0;
Adithya Srinivasanf279e042020-08-17 14:56:27 -070056};
57
58enum class PredictionState {
59 Valid, // Predictions obtained successfully from the TokenManager
60 Expired, // TokenManager no longer has the predictions
61 None, // Predictions are either not present or didn't come from TokenManager
62};
63
64/*
65 * Stores a set of predictions and the corresponding actual timestamps pertaining to a single frame
66 * from the app
67 */
68class SurfaceFrame {
69public:
70 enum class PresentState {
71 Presented, // Buffer was latched and presented by SurfaceFlinger
72 Dropped, // Buffer was dropped by SurfaceFlinger
73 Unknown, // Initial state, SurfaceFlinger hasn't seen this buffer yet
74 };
75
76 virtual ~SurfaceFrame() = default;
77
78 virtual TimelineItem getPredictions() = 0;
79 virtual TimelineItem getActuals() = 0;
Adithya Srinivasan5f683cf2020-09-15 14:21:04 -070080 virtual nsecs_t getActualQueueTime() = 0;
Adithya Srinivasanf279e042020-08-17 14:56:27 -070081 virtual PresentState getPresentState() = 0;
82 virtual PredictionState getPredictionState() = 0;
83
84 virtual void setPresentState(PresentState state) = 0;
Adithya Srinivasanf279e042020-08-17 14:56:27 -070085
Adithya Srinivasan5f683cf2020-09-15 14:21:04 -070086 // Actual timestamps of the app are set individually at different functions.
87 // Start time (if the app provides) and Queue time are accessible after queueing the frame,
Ady Abraham7f8a1e62020-09-28 16:09:35 -070088 // whereas Acquire Fence time is available only during latch.
Adithya Srinivasan5f683cf2020-09-15 14:21:04 -070089 virtual void setActualStartTime(nsecs_t actualStartTime) = 0;
Adithya Srinivasanf279e042020-08-17 14:56:27 -070090 virtual void setActualQueueTime(nsecs_t actualQueueTime) = 0;
Ady Abraham7f8a1e62020-09-28 16:09:35 -070091 virtual void setAcquireFenceTime(nsecs_t acquireFenceTime) = 0;
Adithya Srinivasanf279e042020-08-17 14:56:27 -070092};
93
94/*
95 * Maintains a history of SurfaceFrames grouped together by the vsync time in which they were
96 * presented
97 */
98class FrameTimeline {
99public:
100 virtual ~FrameTimeline() = default;
Adithya Srinivasan5f683cf2020-09-15 14:21:04 -0700101 virtual TokenManager* getTokenManager() = 0;
Adithya Srinivasanf279e042020-08-17 14:56:27 -0700102
103 // Create a new surface frame, set the predictions based on a token and return it to the caller.
104 // Sets the PredictionState of SurfaceFrame.
105 virtual std::unique_ptr<SurfaceFrame> createSurfaceFrameForToken(
106 const std::string& layerName, std::optional<int64_t> token) = 0;
107
108 // Adds a new SurfaceFrame to the current DisplayFrame. Frames from multiple layers can be
109 // composited into one display frame.
110 virtual void addSurfaceFrame(std::unique_ptr<SurfaceFrame> surfaceFrame,
111 SurfaceFrame::PresentState state) = 0;
112
113 // The first function called by SF for the current DisplayFrame. Fetches SF predictions based on
114 // the token and sets the actualSfWakeTime for the current DisplayFrame.
115 virtual void setSfWakeUp(int64_t token, nsecs_t wakeupTime) = 0;
116
117 // Sets the sfPresentTime and finalizes the current DisplayFrame. Tracks the given present fence
118 // until it's signaled, and updates the present timestamps of all presented SurfaceFrames in
119 // that vsync.
120 virtual void setSfPresent(nsecs_t sfPresentTime,
121 const std::shared_ptr<FenceTime>& presentFence) = 0;
Adithya Srinivasan5f683cf2020-09-15 14:21:04 -0700122
123 virtual void dump(std::string& result) = 0;
Adithya Srinivasanf279e042020-08-17 14:56:27 -0700124};
125
126namespace impl {
127
128using namespace std::chrono_literals;
129
130class TokenManager : public android::frametimeline::TokenManager {
131public:
Ady Abraham22c7b5c2020-09-22 19:33:40 -0700132 TokenManager() : mCurrentToken(ISurfaceComposer::INVALID_VSYNC_ID + 1) {}
Adithya Srinivasanf279e042020-08-17 14:56:27 -0700133 ~TokenManager() = default;
134
135 int64_t generateTokenForPredictions(TimelineItem&& predictions) override;
136 std::optional<TimelineItem> getPredictionsForToken(int64_t token);
137
138private:
139 // Friend class for testing
140 friend class android::frametimeline::FrameTimelineTest;
141
142 void flushTokens(nsecs_t flushTime) REQUIRES(mMutex);
143
144 std::unordered_map<int64_t, TimelineItem> mPredictions GUARDED_BY(mMutex);
145 std::vector<std::pair<int64_t, nsecs_t>> mTokens GUARDED_BY(mMutex);
146 int64_t mCurrentToken GUARDED_BY(mMutex);
147 std::mutex mMutex;
148 static constexpr nsecs_t kMaxRetentionTime =
149 std::chrono::duration_cast<std::chrono::nanoseconds>(120ms).count();
150};
151
152class SurfaceFrame : public android::frametimeline::SurfaceFrame {
153public:
154 SurfaceFrame(const std::string& layerName, PredictionState predictionState,
155 TimelineItem&& predictions);
156 ~SurfaceFrame() = default;
157
158 TimelineItem getPredictions() override { return mPredictions; };
159 TimelineItem getActuals() override;
Adithya Srinivasan5f683cf2020-09-15 14:21:04 -0700160 nsecs_t getActualQueueTime() override;
Adithya Srinivasanf279e042020-08-17 14:56:27 -0700161 PresentState getPresentState() override;
162 PredictionState getPredictionState() override;
Adithya Srinivasan5f683cf2020-09-15 14:21:04 -0700163
164 void setActualStartTime(nsecs_t actualStartTime) override;
165 void setActualQueueTime(nsecs_t actualQueueTime) override;
Ady Abraham7f8a1e62020-09-28 16:09:35 -0700166 void setAcquireFenceTime(nsecs_t acquireFenceTime) override;
Adithya Srinivasanf279e042020-08-17 14:56:27 -0700167 void setPresentState(PresentState state) override;
Adithya Srinivasan5f683cf2020-09-15 14:21:04 -0700168 void setActualPresentTime(nsecs_t presentTime);
Adithya Srinivasanf279e042020-08-17 14:56:27 -0700169 void dump(std::string& result);
170
171private:
172 const std::string mLayerName;
173 PresentState mPresentState GUARDED_BY(mMutex);
174 PredictionState mPredictionState GUARDED_BY(mMutex);
175 const TimelineItem mPredictions;
176 TimelineItem mActuals GUARDED_BY(mMutex);
177 nsecs_t mActualQueueTime;
178 std::mutex mMutex;
179};
180
181class FrameTimeline : public android::frametimeline::FrameTimeline {
182public:
183 FrameTimeline();
184 ~FrameTimeline() = default;
185
Adithya Srinivasan5f683cf2020-09-15 14:21:04 -0700186 frametimeline::TokenManager* getTokenManager() override { return &mTokenManager; }
Adithya Srinivasanf279e042020-08-17 14:56:27 -0700187 std::unique_ptr<frametimeline::SurfaceFrame> createSurfaceFrameForToken(
188 const std::string& layerName, std::optional<int64_t> token) override;
189 void addSurfaceFrame(std::unique_ptr<frametimeline::SurfaceFrame> surfaceFrame,
190 SurfaceFrame::PresentState state) override;
191 void setSfWakeUp(int64_t token, nsecs_t wakeupTime) override;
192 void setSfPresent(nsecs_t sfPresentTime,
193 const std::shared_ptr<FenceTime>& presentFence) override;
Adithya Srinivasan5f683cf2020-09-15 14:21:04 -0700194 void dump(std::string& result) override;
Adithya Srinivasanf279e042020-08-17 14:56:27 -0700195
196private:
197 // Friend class for testing
198 friend class android::frametimeline::FrameTimelineTest;
199
200 /*
201 * DisplayFrame should be used only internally within FrameTimeline.
202 */
203 struct DisplayFrame {
204 DisplayFrame();
205
206 /* Usage of TimelineItem w.r.t SurfaceFlinger
207 * startTime Time when SurfaceFlinger wakes up to handle transactions and buffer updates
208 * endTime Time when SurfaceFlinger sends a composited frame to Display
209 * presentTime Time when the composited frame was presented on screen
210 */
211 TimelineItem surfaceFlingerPredictions;
212 TimelineItem surfaceFlingerActuals;
213
214 // Collection of predictions and actual values sent over by Layers
215 std::vector<std::unique_ptr<SurfaceFrame>> surfaceFrames;
216
217 PredictionState predictionState;
218 };
219
220 void flushPendingPresentFences() REQUIRES(mMutex);
221 void finalizeCurrentDisplayFrame() REQUIRES(mMutex);
222
223 // Sliding window of display frames. TODO(b/168072834): compare perf with fixed size array
224 std::deque<std::shared_ptr<DisplayFrame>> mDisplayFrames GUARDED_BY(mMutex);
225 std::vector<std::pair<std::shared_ptr<FenceTime>, std::shared_ptr<DisplayFrame>>>
226 mPendingPresentFences GUARDED_BY(mMutex);
227 std::shared_ptr<DisplayFrame> mCurrentDisplayFrame GUARDED_BY(mMutex);
228 TokenManager mTokenManager;
229 std::mutex mMutex;
230 static constexpr uint32_t kMaxDisplayFrames = 64;
231 // The initial container size for the vector<SurfaceFrames> inside display frame. Although this
232 // number doesn't represent any bounds on the number of surface frames that can go in a display
233 // frame, this is a good starting size for the vector so that we can avoid the internal vector
234 // resizing that happens with push_back.
235 static constexpr uint32_t kNumSurfaceFramesInitial = 10;
236};
237
238} // namespace impl
239} // namespace android::frametimeline