blob: a42c32c1c1b8ab444a5d9c1ff156e41cf58c159b [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.
Adithya Srinivasan5f683cf2020-09-15 14:21:04 -070054 virtual int64_t generateTokenForPredictions(TimelineItem&& prediction) = 0;
Adithya Srinivasanf279e042020-08-17 14:56:27 -070055};
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;
Adithya Srinivasan5f683cf2020-09-15 14:21:04 -070079 virtual nsecs_t getActualQueueTime() = 0;
Adithya Srinivasanf279e042020-08-17 14:56:27 -070080 virtual PresentState getPresentState() = 0;
81 virtual PredictionState getPredictionState() = 0;
82
83 virtual void setPresentState(PresentState state) = 0;
Adithya Srinivasanf279e042020-08-17 14:56:27 -070084
Adithya Srinivasan5f683cf2020-09-15 14:21:04 -070085 // Actual timestamps of the app are set individually at different functions.
86 // Start time (if the app provides) and Queue time are accessible after queueing the frame,
87 // whereas End time is available only during latch.
88 virtual void setActualStartTime(nsecs_t actualStartTime) = 0;
Adithya Srinivasanf279e042020-08-17 14:56:27 -070089 virtual void setActualQueueTime(nsecs_t actualQueueTime) = 0;
Adithya Srinivasan5f683cf2020-09-15 14:21:04 -070090 virtual void setActualEndTime(nsecs_t actualEndTime) = 0;
Adithya Srinivasanf279e042020-08-17 14:56:27 -070091};
92
93/*
94 * Maintains a history of SurfaceFrames grouped together by the vsync time in which they were
95 * presented
96 */
97class FrameTimeline {
98public:
99 virtual ~FrameTimeline() = default;
Adithya Srinivasan5f683cf2020-09-15 14:21:04 -0700100 virtual TokenManager* getTokenManager() = 0;
Adithya Srinivasanf279e042020-08-17 14:56:27 -0700101
102 // Create a new surface frame, set the predictions based on a token and return it to the caller.
103 // Sets the PredictionState of SurfaceFrame.
104 virtual std::unique_ptr<SurfaceFrame> createSurfaceFrameForToken(
105 const std::string& layerName, std::optional<int64_t> token) = 0;
106
107 // Adds a new SurfaceFrame to the current DisplayFrame. Frames from multiple layers can be
108 // composited into one display frame.
109 virtual void addSurfaceFrame(std::unique_ptr<SurfaceFrame> surfaceFrame,
110 SurfaceFrame::PresentState state) = 0;
111
112 // The first function called by SF for the current DisplayFrame. Fetches SF predictions based on
113 // the token and sets the actualSfWakeTime for the current DisplayFrame.
114 virtual void setSfWakeUp(int64_t token, nsecs_t wakeupTime) = 0;
115
116 // Sets the sfPresentTime and finalizes the current DisplayFrame. Tracks the given present fence
117 // until it's signaled, and updates the present timestamps of all presented SurfaceFrames in
118 // that vsync.
119 virtual void setSfPresent(nsecs_t sfPresentTime,
120 const std::shared_ptr<FenceTime>& presentFence) = 0;
Adithya Srinivasan5f683cf2020-09-15 14:21:04 -0700121
122 virtual void dump(std::string& result) = 0;
Adithya Srinivasanf279e042020-08-17 14:56:27 -0700123};
124
125namespace impl {
126
127using namespace std::chrono_literals;
128
129class TokenManager : public android::frametimeline::TokenManager {
130public:
131 TokenManager() : mCurrentToken(0) {}
132 ~TokenManager() = default;
133
134 int64_t generateTokenForPredictions(TimelineItem&& predictions) override;
135 std::optional<TimelineItem> getPredictionsForToken(int64_t token);
136
137private:
138 // Friend class for testing
139 friend class android::frametimeline::FrameTimelineTest;
140
141 void flushTokens(nsecs_t flushTime) REQUIRES(mMutex);
142
143 std::unordered_map<int64_t, TimelineItem> mPredictions GUARDED_BY(mMutex);
144 std::vector<std::pair<int64_t, nsecs_t>> mTokens GUARDED_BY(mMutex);
145 int64_t mCurrentToken GUARDED_BY(mMutex);
146 std::mutex mMutex;
147 static constexpr nsecs_t kMaxRetentionTime =
148 std::chrono::duration_cast<std::chrono::nanoseconds>(120ms).count();
149};
150
151class SurfaceFrame : public android::frametimeline::SurfaceFrame {
152public:
153 SurfaceFrame(const std::string& layerName, PredictionState predictionState,
154 TimelineItem&& predictions);
155 ~SurfaceFrame() = default;
156
157 TimelineItem getPredictions() override { return mPredictions; };
158 TimelineItem getActuals() override;
Adithya Srinivasan5f683cf2020-09-15 14:21:04 -0700159 nsecs_t getActualQueueTime() override;
Adithya Srinivasanf279e042020-08-17 14:56:27 -0700160 PresentState getPresentState() override;
161 PredictionState getPredictionState() override;
Adithya Srinivasan5f683cf2020-09-15 14:21:04 -0700162
163 void setActualStartTime(nsecs_t actualStartTime) override;
164 void setActualQueueTime(nsecs_t actualQueueTime) override;
165 void setActualEndTime(nsecs_t actualEndTime) override;
Adithya Srinivasanf279e042020-08-17 14:56:27 -0700166 void setPresentState(PresentState state) override;
Adithya Srinivasan5f683cf2020-09-15 14:21:04 -0700167 void setActualPresentTime(nsecs_t presentTime);
Adithya Srinivasanf279e042020-08-17 14:56:27 -0700168 void dump(std::string& result);
169
170private:
171 const std::string mLayerName;
172 PresentState mPresentState GUARDED_BY(mMutex);
173 PredictionState mPredictionState GUARDED_BY(mMutex);
174 const TimelineItem mPredictions;
175 TimelineItem mActuals GUARDED_BY(mMutex);
176 nsecs_t mActualQueueTime;
177 std::mutex mMutex;
178};
179
180class FrameTimeline : public android::frametimeline::FrameTimeline {
181public:
182 FrameTimeline();
183 ~FrameTimeline() = default;
184
Adithya Srinivasan5f683cf2020-09-15 14:21:04 -0700185 frametimeline::TokenManager* getTokenManager() override { return &mTokenManager; }
Adithya Srinivasanf279e042020-08-17 14:56:27 -0700186 std::unique_ptr<frametimeline::SurfaceFrame> createSurfaceFrameForToken(
187 const std::string& layerName, std::optional<int64_t> token) override;
188 void addSurfaceFrame(std::unique_ptr<frametimeline::SurfaceFrame> surfaceFrame,
189 SurfaceFrame::PresentState state) override;
190 void setSfWakeUp(int64_t token, nsecs_t wakeupTime) override;
191 void setSfPresent(nsecs_t sfPresentTime,
192 const std::shared_ptr<FenceTime>& presentFence) override;
Adithya Srinivasan5f683cf2020-09-15 14:21:04 -0700193 void dump(std::string& result) override;
Adithya Srinivasanf279e042020-08-17 14:56:27 -0700194
195private:
196 // Friend class for testing
197 friend class android::frametimeline::FrameTimelineTest;
198
199 /*
200 * DisplayFrame should be used only internally within FrameTimeline.
201 */
202 struct DisplayFrame {
203 DisplayFrame();
204
205 /* Usage of TimelineItem w.r.t SurfaceFlinger
206 * startTime Time when SurfaceFlinger wakes up to handle transactions and buffer updates
207 * endTime Time when SurfaceFlinger sends a composited frame to Display
208 * presentTime Time when the composited frame was presented on screen
209 */
210 TimelineItem surfaceFlingerPredictions;
211 TimelineItem surfaceFlingerActuals;
212
213 // Collection of predictions and actual values sent over by Layers
214 std::vector<std::unique_ptr<SurfaceFrame>> surfaceFrames;
215
216 PredictionState predictionState;
217 };
218
219 void flushPendingPresentFences() REQUIRES(mMutex);
220 void finalizeCurrentDisplayFrame() REQUIRES(mMutex);
221
222 // Sliding window of display frames. TODO(b/168072834): compare perf with fixed size array
223 std::deque<std::shared_ptr<DisplayFrame>> mDisplayFrames GUARDED_BY(mMutex);
224 std::vector<std::pair<std::shared_ptr<FenceTime>, std::shared_ptr<DisplayFrame>>>
225 mPendingPresentFences GUARDED_BY(mMutex);
226 std::shared_ptr<DisplayFrame> mCurrentDisplayFrame GUARDED_BY(mMutex);
227 TokenManager mTokenManager;
228 std::mutex mMutex;
229 static constexpr uint32_t kMaxDisplayFrames = 64;
230 // The initial container size for the vector<SurfaceFrames> inside display frame. Although this
231 // number doesn't represent any bounds on the number of surface frames that can go in a display
232 // frame, this is a good starting size for the vector so that we can avoid the internal vector
233 // resizing that happens with push_back.
234 static constexpr uint32_t kNumSurfaceFramesInitial = 10;
235};
236
237} // namespace impl
238} // namespace android::frametimeline