blob: 291e30ebcb7e10e2cf740d02009fd9e60f4778ab [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
22#include <ui/FenceTime.h>
23#include <utils/RefBase.h>
24#include <utils/Timers.h>
25
26namespace android::frametimeline {
27
28class FrameTimelineTest;
29
30/*
31 * Collection of timestamps that can be used for both predictions and actual times.
32 */
33struct TimelineItem {
34 TimelineItem(const nsecs_t startTime = 0, const nsecs_t endTime = 0,
35 const nsecs_t presentTime = 0)
36 : startTime(startTime), endTime(endTime), presentTime(presentTime) {}
37
38 nsecs_t startTime;
39 nsecs_t endTime;
40 nsecs_t presentTime;
41};
42
43/*
44 * TokenManager generates a running number token for a set of predictions made by VsyncPredictor. It
45 * saves these predictions for a short period of time and returns the predictions for a given token,
46 * if it hasn't expired.
47 */
48class TokenManager {
49public:
50 virtual ~TokenManager() = default;
51
52 // Generates a token for the given set of predictions. Stores the predictions for 120ms and
53 // destroys it later.
54 virtual int64_t generateTokenForPredictions(TimelineItem&& prediction);
55};
56
57enum class PredictionState {
58 Valid, // Predictions obtained successfully from the TokenManager
59 Expired, // TokenManager no longer has the predictions
60 None, // Predictions are either not present or didn't come from TokenManager
61};
62
63/*
64 * Stores a set of predictions and the corresponding actual timestamps pertaining to a single frame
65 * from the app
66 */
67class SurfaceFrame {
68public:
69 enum class PresentState {
70 Presented, // Buffer was latched and presented by SurfaceFlinger
71 Dropped, // Buffer was dropped by SurfaceFlinger
72 Unknown, // Initial state, SurfaceFlinger hasn't seen this buffer yet
73 };
74
75 virtual ~SurfaceFrame() = default;
76
77 virtual TimelineItem getPredictions() = 0;
78 virtual TimelineItem getActuals() = 0;
79 virtual PresentState getPresentState() = 0;
80 virtual PredictionState getPredictionState() = 0;
81
82 virtual void setPresentState(PresentState state) = 0;
83 virtual void setActuals(TimelineItem&& actuals) = 0;
84
85 // There is no prediction for Queue time and it is not a part of TimelineItem. Set it
86 // separately.
87 virtual void setActualQueueTime(nsecs_t actualQueueTime) = 0;
88};
89
90/*
91 * Maintains a history of SurfaceFrames grouped together by the vsync time in which they were
92 * presented
93 */
94class FrameTimeline {
95public:
96 virtual ~FrameTimeline() = default;
97 virtual TokenManager& getTokenManager() = 0;
98
99 // Create a new surface frame, set the predictions based on a token and return it to the caller.
100 // Sets the PredictionState of SurfaceFrame.
101 virtual std::unique_ptr<SurfaceFrame> createSurfaceFrameForToken(
102 const std::string& layerName, std::optional<int64_t> token) = 0;
103
104 // Adds a new SurfaceFrame to the current DisplayFrame. Frames from multiple layers can be
105 // composited into one display frame.
106 virtual void addSurfaceFrame(std::unique_ptr<SurfaceFrame> surfaceFrame,
107 SurfaceFrame::PresentState state) = 0;
108
109 // The first function called by SF for the current DisplayFrame. Fetches SF predictions based on
110 // the token and sets the actualSfWakeTime for the current DisplayFrame.
111 virtual void setSfWakeUp(int64_t token, nsecs_t wakeupTime) = 0;
112
113 // Sets the sfPresentTime and finalizes the current DisplayFrame. Tracks the given present fence
114 // until it's signaled, and updates the present timestamps of all presented SurfaceFrames in
115 // that vsync.
116 virtual void setSfPresent(nsecs_t sfPresentTime,
117 const std::shared_ptr<FenceTime>& presentFence) = 0;
118};
119
120namespace impl {
121
122using namespace std::chrono_literals;
123
124class TokenManager : public android::frametimeline::TokenManager {
125public:
126 TokenManager() : mCurrentToken(0) {}
127 ~TokenManager() = default;
128
129 int64_t generateTokenForPredictions(TimelineItem&& predictions) override;
130 std::optional<TimelineItem> getPredictionsForToken(int64_t token);
131
132private:
133 // Friend class for testing
134 friend class android::frametimeline::FrameTimelineTest;
135
136 void flushTokens(nsecs_t flushTime) REQUIRES(mMutex);
137
138 std::unordered_map<int64_t, TimelineItem> mPredictions GUARDED_BY(mMutex);
139 std::vector<std::pair<int64_t, nsecs_t>> mTokens GUARDED_BY(mMutex);
140 int64_t mCurrentToken GUARDED_BY(mMutex);
141 std::mutex mMutex;
142 static constexpr nsecs_t kMaxRetentionTime =
143 std::chrono::duration_cast<std::chrono::nanoseconds>(120ms).count();
144};
145
146class SurfaceFrame : public android::frametimeline::SurfaceFrame {
147public:
148 SurfaceFrame(const std::string& layerName, PredictionState predictionState,
149 TimelineItem&& predictions);
150 ~SurfaceFrame() = default;
151
152 TimelineItem getPredictions() override { return mPredictions; };
153 TimelineItem getActuals() override;
154 PresentState getPresentState() override;
155 PredictionState getPredictionState() override;
156 void setActuals(TimelineItem&& actuals) override;
157 void setActualQueueTime(nsecs_t actualQueueTime) override {
158 mActualQueueTime = actualQueueTime;
159 };
160 void setPresentState(PresentState state) override;
161 void setPresentTime(nsecs_t presentTime);
162 void dump(std::string& result);
163
164private:
165 const std::string mLayerName;
166 PresentState mPresentState GUARDED_BY(mMutex);
167 PredictionState mPredictionState GUARDED_BY(mMutex);
168 const TimelineItem mPredictions;
169 TimelineItem mActuals GUARDED_BY(mMutex);
170 nsecs_t mActualQueueTime;
171 std::mutex mMutex;
172};
173
174class FrameTimeline : public android::frametimeline::FrameTimeline {
175public:
176 FrameTimeline();
177 ~FrameTimeline() = default;
178
179 frametimeline::TokenManager& getTokenManager() override { return mTokenManager; }
180 std::unique_ptr<frametimeline::SurfaceFrame> createSurfaceFrameForToken(
181 const std::string& layerName, std::optional<int64_t> token) override;
182 void addSurfaceFrame(std::unique_ptr<frametimeline::SurfaceFrame> surfaceFrame,
183 SurfaceFrame::PresentState state) override;
184 void setSfWakeUp(int64_t token, nsecs_t wakeupTime) override;
185 void setSfPresent(nsecs_t sfPresentTime,
186 const std::shared_ptr<FenceTime>& presentFence) override;
187 void dump(std::string& result);
188
189private:
190 // Friend class for testing
191 friend class android::frametimeline::FrameTimelineTest;
192
193 /*
194 * DisplayFrame should be used only internally within FrameTimeline.
195 */
196 struct DisplayFrame {
197 DisplayFrame();
198
199 /* Usage of TimelineItem w.r.t SurfaceFlinger
200 * startTime Time when SurfaceFlinger wakes up to handle transactions and buffer updates
201 * endTime Time when SurfaceFlinger sends a composited frame to Display
202 * presentTime Time when the composited frame was presented on screen
203 */
204 TimelineItem surfaceFlingerPredictions;
205 TimelineItem surfaceFlingerActuals;
206
207 // Collection of predictions and actual values sent over by Layers
208 std::vector<std::unique_ptr<SurfaceFrame>> surfaceFrames;
209
210 PredictionState predictionState;
211 };
212
213 void flushPendingPresentFences() REQUIRES(mMutex);
214 void finalizeCurrentDisplayFrame() REQUIRES(mMutex);
215
216 // Sliding window of display frames. TODO(b/168072834): compare perf with fixed size array
217 std::deque<std::shared_ptr<DisplayFrame>> mDisplayFrames GUARDED_BY(mMutex);
218 std::vector<std::pair<std::shared_ptr<FenceTime>, std::shared_ptr<DisplayFrame>>>
219 mPendingPresentFences GUARDED_BY(mMutex);
220 std::shared_ptr<DisplayFrame> mCurrentDisplayFrame GUARDED_BY(mMutex);
221 TokenManager mTokenManager;
222 std::mutex mMutex;
223 static constexpr uint32_t kMaxDisplayFrames = 64;
224 // The initial container size for the vector<SurfaceFrames> inside display frame. Although this
225 // number doesn't represent any bounds on the number of surface frames that can go in a display
226 // frame, this is a good starting size for the vector so that we can avoid the internal vector
227 // resizing that happens with push_back.
228 static constexpr uint32_t kNumSurfaceFramesInitial = 10;
229};
230
231} // namespace impl
232} // namespace android::frametimeline