blob: bda3c5cf948d8b0a867a664f877c595dde2ef25b [file] [log] [blame]
Pablo Ceballosce796e72016-02-04 19:10:51 -08001/*
2 * Copyright 2016 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#ifndef ANDROID_GUI_FRAMETIMESTAMPS_H
18#define ANDROID_GUI_FRAMETIMESTAMPS_H
19
Brian Anderson3d4039d2016-09-23 16:31:30 -070020#include <ui/FenceTime.h>
Pablo Ceballosce796e72016-02-04 19:10:51 -080021#include <utils/Flattenable.h>
Brian Andersond6927fb2016-07-23 23:37:30 -070022#include <utils/StrongPointer.h>
23#include <utils/Timers.h>
24
25#include <array>
Brian Anderson3890c392016-07-25 12:48:08 -070026#include <bitset>
27#include <vector>
Pablo Ceballosce796e72016-02-04 19:10:51 -080028
29namespace android {
30
Brian Andersond6927fb2016-07-23 23:37:30 -070031struct FrameEvents;
Brian Anderson3890c392016-07-25 12:48:08 -070032class FrameEventHistoryDelta;
Brian Andersond6927fb2016-07-23 23:37:30 -070033class String8;
34
35
Brian Anderson3d4039d2016-09-23 16:31:30 -070036// Identifiers for all the events that may be recorded or reported.
Brian Anderson3890c392016-07-25 12:48:08 -070037enum class FrameEvent {
38 POSTED,
Brian Anderson069b3652016-07-22 10:32:47 -070039 REQUESTED_PRESENT,
Brian Anderson3890c392016-07-25 12:48:08 -070040 LATCH,
Brian Anderson069b3652016-07-22 10:32:47 -070041 ACQUIRE,
Brian Anderson3890c392016-07-25 12:48:08 -070042 FIRST_REFRESH_START,
43 LAST_REFRESH_START,
Brian Andersonb04c6f02016-10-21 12:57:46 -070044 GPU_COMPOSITION_DONE,
Brian Anderson3890c392016-07-25 12:48:08 -070045 DISPLAY_PRESENT,
46 DISPLAY_RETIRE,
Brian Andersonf6386862016-10-31 16:34:13 -070047 DEQUEUE_READY,
Brian Anderson3890c392016-07-25 12:48:08 -070048 RELEASE,
49 EVENT_COUNT, // Not an actual event.
Pablo Ceballosce796e72016-02-04 19:10:51 -080050};
51
Brian Andersond6927fb2016-07-23 23:37:30 -070052
53// A collection of timestamps corresponding to a single frame.
54struct FrameEvents {
Brian Andersoned816e62016-10-26 16:12:21 -070055 static constexpr auto EVENT_COUNT =
56 static_cast<size_t>(FrameEvent::EVENT_COUNT);
57 static_assert(EVENT_COUNT <= 32, "Event count sanity check failed.");
58 static constexpr nsecs_t TIMESTAMP_PENDING =
59 std::numeric_limits<nsecs_t>::max();
60
61 static inline bool isValidTimestamp(nsecs_t time) {
62 return time != TIMESTAMP_PENDING;
63 }
64
Brian Anderson3890c392016-07-25 12:48:08 -070065 bool hasPostedInfo() const;
66 bool hasRequestedPresentInfo() const;
67 bool hasLatchInfo() const;
68 bool hasFirstRefreshStartInfo() const;
69 bool hasLastRefreshStartInfo() const;
70 bool hasAcquireInfo() const;
71 bool hasGpuCompositionDoneInfo() const;
72 bool hasDisplayPresentInfo() const;
73 bool hasDisplayRetireInfo() const;
74 bool hasReleaseInfo() const;
Brian Andersonf6386862016-10-31 16:34:13 -070075 bool hasDequeueReadyInfo() const;
Brian Anderson3890c392016-07-25 12:48:08 -070076
Brian Andersond6927fb2016-07-23 23:37:30 -070077 void checkFencesForCompletion();
78 void dump(String8& outString) const;
79
80 bool valid{false};
Brian Anderson5ea5e592016-12-01 16:54:33 -080081 int connectId{0};
Brian Andersond6927fb2016-07-23 23:37:30 -070082 uint64_t frameNumber{0};
83
84 // Whether or not certain points in the frame's life cycle have been
85 // encountered help us determine if timestamps aren't available because
86 // a) we'll just never get them or b) they're not ready yet.
87 bool addPostCompositeCalled{false};
88 bool addRetireCalled{false};
Brian Anderson3890c392016-07-25 12:48:08 -070089 bool addReleaseCalled{false};
Brian Andersond6927fb2016-07-23 23:37:30 -070090
Brian Andersoned816e62016-10-26 16:12:21 -070091 nsecs_t postedTime{TIMESTAMP_PENDING};
92 nsecs_t requestedPresentTime{TIMESTAMP_PENDING};
93 nsecs_t latchTime{TIMESTAMP_PENDING};
94 nsecs_t firstRefreshStartTime{TIMESTAMP_PENDING};
95 nsecs_t lastRefreshStartTime{TIMESTAMP_PENDING};
96 nsecs_t dequeueReadyTime{TIMESTAMP_PENDING};
Brian Andersond6927fb2016-07-23 23:37:30 -070097
Brian Anderson3d4039d2016-09-23 16:31:30 -070098 std::shared_ptr<FenceTime> acquireFence{FenceTime::NO_FENCE};
99 std::shared_ptr<FenceTime> gpuCompositionDoneFence{FenceTime::NO_FENCE};
100 std::shared_ptr<FenceTime> displayPresentFence{FenceTime::NO_FENCE};
101 std::shared_ptr<FenceTime> displayRetireFence{FenceTime::NO_FENCE};
102 std::shared_ptr<FenceTime> releaseFence{FenceTime::NO_FENCE};
Brian Andersond6927fb2016-07-23 23:37:30 -0700103};
104
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800105struct CompositorTiming {
106 nsecs_t deadline{0};
107 nsecs_t interval{16666667};
108 nsecs_t presentLatency{0};
109};
Brian Andersond6927fb2016-07-23 23:37:30 -0700110
Brian Anderson3890c392016-07-25 12:48:08 -0700111// A short history of frames that are synchronized between the consumer and
112// producer via deltas.
113class FrameEventHistory {
114public:
115 virtual ~FrameEventHistory();
116
117 FrameEvents* getFrame(uint64_t frameNumber);
118 FrameEvents* getFrame(uint64_t frameNumber, size_t* iHint);
119 void checkFencesForCompletion();
120 void dump(String8& outString) const;
121
122 static constexpr size_t MAX_FRAME_HISTORY = 8;
123
124protected:
125 std::array<FrameEvents, MAX_FRAME_HISTORY> mFrames;
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800126
127 CompositorTiming mCompositorTiming;
Brian Anderson3890c392016-07-25 12:48:08 -0700128};
129
130
131// The producer's interface to FrameEventHistory
132class ProducerFrameEventHistory : public FrameEventHistory {
133public:
134 ~ProducerFrameEventHistory() override;
135
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800136 // Public for testing.
137 static nsecs_t snapToNextTick(
138 nsecs_t timestamp, nsecs_t tickPhase, nsecs_t tickInterval);
139
140 nsecs_t getNextCompositeDeadline(const nsecs_t now) const;
141 nsecs_t getCompositeInterval() const { return mCompositorTiming.interval; }
142 nsecs_t getCompositeToPresentLatency() const {
143 return mCompositorTiming.presentLatency;
144 }
145
Brian Anderson3da8d272016-07-28 16:20:47 -0700146 // virtual for testing.
147 virtual void updateAcquireFence(
Brian Anderson3d4039d2016-09-23 16:31:30 -0700148 uint64_t frameNumber, std::shared_ptr<FenceTime>&& acquire);
Brian Anderson3890c392016-07-25 12:48:08 -0700149 void applyDelta(const FrameEventHistoryDelta& delta);
150
Brian Anderson3d4039d2016-09-23 16:31:30 -0700151 void updateSignalTimes();
152
Brian Anderson3da8d272016-07-28 16:20:47 -0700153protected:
154 void applyFenceDelta(FenceTimeline* timeline,
155 std::shared_ptr<FenceTime>* dst,
156 const FenceTime::Snapshot& src) const;
157
158 // virtual for testing.
159 virtual std::shared_ptr<FenceTime> createFenceTime(
160 const sp<Fence>& fence) const;
161
Brian Anderson3890c392016-07-25 12:48:08 -0700162 size_t mAcquireOffset{0};
Brian Anderson3d4039d2016-09-23 16:31:30 -0700163
164 // The consumer updates it's timelines in Layer and SurfaceFlinger since
165 // they can coordinate shared timelines better. The producer doesn't have
166 // shared timelines though, so just let it own and update all of them.
167 FenceTimeline mAcquireTimeline;
168 FenceTimeline mGpuCompositionDoneTimeline;
169 FenceTimeline mPresentTimeline;
170 FenceTimeline mRetireTimeline;
171 FenceTimeline mReleaseTimeline;
Brian Anderson3890c392016-07-25 12:48:08 -0700172};
173
174
175// Used by the consumer to create a new frame event record that is
176// partially complete.
Brian Andersond6927fb2016-07-23 23:37:30 -0700177struct NewFrameEventsEntry {
178 uint64_t frameNumber{0};
179 nsecs_t postedTime{0};
180 nsecs_t requestedPresentTime{0};
Brian Anderson3d4039d2016-09-23 16:31:30 -0700181 std::shared_ptr<FenceTime> acquireFence{FenceTime::NO_FENCE};
Brian Andersond6927fb2016-07-23 23:37:30 -0700182};
183
184
Brian Anderson3890c392016-07-25 12:48:08 -0700185// Used by the consumer to keep track of which fields it already sent to
186// the producer.
187class FrameEventDirtyFields {
Brian Andersond6927fb2016-07-23 23:37:30 -0700188public:
Brian Anderson3890c392016-07-25 12:48:08 -0700189 inline void reset() { mBitset.reset(); }
190 inline bool anyDirty() const { return mBitset.any(); }
Brian Andersond6927fb2016-07-23 23:37:30 -0700191
Brian Anderson3890c392016-07-25 12:48:08 -0700192 template <FrameEvent event>
193 inline void setDirty() {
194 constexpr size_t eventIndex = static_cast<size_t>(event);
195 static_assert(eventIndex < FrameEvents::EVENT_COUNT, "Bad index.");
196 mBitset.set(eventIndex);
197 }
198
199 template <FrameEvent event>
200 inline bool isDirty() const {
201 constexpr size_t eventIndex = static_cast<size_t>(event);
202 static_assert(eventIndex < FrameEvents::EVENT_COUNT, "Bad index.");
203 return mBitset[eventIndex];
204 }
205
206private:
207 std::bitset<FrameEvents::EVENT_COUNT> mBitset;
208};
209
210
211// The consumer's interface to FrameEventHistory
212class ConsumerFrameEventHistory : public FrameEventHistory {
213public:
214 ~ConsumerFrameEventHistory() override;
215
Brian Anderson5ea5e592016-12-01 16:54:33 -0800216 void onDisconnect();
217
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800218 void initializeCompositorTiming(const CompositorTiming& compositorTiming);
219
Brian Anderson3890c392016-07-25 12:48:08 -0700220 void addQueue(const NewFrameEventsEntry& newEntry);
Brian Andersond6927fb2016-07-23 23:37:30 -0700221 void addLatch(uint64_t frameNumber, nsecs_t latchTime);
222 void addPreComposition(uint64_t frameNumber, nsecs_t refreshStartTime);
223 void addPostComposition(uint64_t frameNumber,
Brian Anderson3d4039d2016-09-23 16:31:30 -0700224 const std::shared_ptr<FenceTime>& gpuCompositionDone,
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800225 const std::shared_ptr<FenceTime>& displayPresent,
226 const CompositorTiming& compositorTiming);
Brian Anderson3d4039d2016-09-23 16:31:30 -0700227 void addRetire(uint64_t frameNumber,
228 const std::shared_ptr<FenceTime>& displayRetire);
Brian Andersonf6386862016-10-31 16:34:13 -0700229 void addRelease(uint64_t frameNumber, nsecs_t dequeueReadyTime,
Brian Anderson3d4039d2016-09-23 16:31:30 -0700230 std::shared_ptr<FenceTime>&& release);
Brian Andersond6927fb2016-07-23 23:37:30 -0700231
Brian Anderson3890c392016-07-25 12:48:08 -0700232 void getAndResetDelta(FrameEventHistoryDelta* delta);
233
Brian Andersond6927fb2016-07-23 23:37:30 -0700234private:
Brian Anderson3d4039d2016-09-23 16:31:30 -0700235 void getFrameDelta(FrameEventHistoryDelta* delta,
236 const std::array<FrameEvents, MAX_FRAME_HISTORY>::iterator& frame);
237
Brian Anderson3890c392016-07-25 12:48:08 -0700238 std::array<FrameEventDirtyFields, MAX_FRAME_HISTORY> mFramesDirty;
Brian Anderson5ea5e592016-12-01 16:54:33 -0800239
Brian Andersond6927fb2016-07-23 23:37:30 -0700240 size_t mQueueOffset{0};
241 size_t mCompositionOffset{0};
242 size_t mRetireOffset{0};
243 size_t mReleaseOffset{0};
Brian Anderson4565daa2016-12-13 15:41:28 -0800244
Brian Anderson5ea5e592016-12-01 16:54:33 -0800245 int mCurrentConnectId{0};
Brian Anderson4565daa2016-12-13 15:41:28 -0800246 bool mProducerWantsEvents{false};
Brian Andersond6927fb2016-07-23 23:37:30 -0700247};
248
Brian Anderson3890c392016-07-25 12:48:08 -0700249
250// A single frame update from the consumer to producer that can be sent
251// through Binder.
252// Although this may be sent multiple times for the same frame as new
253// timestamps are set, Fences only need to be sent once.
254class FrameEventsDelta : public Flattenable<FrameEventsDelta> {
255friend class ProducerFrameEventHistory;
256public:
257 FrameEventsDelta() = default;
258 FrameEventsDelta(size_t index,
259 const FrameEvents& frameTimestamps,
260 const FrameEventDirtyFields& dirtyFields);
261
Brian Anderson3d4039d2016-09-23 16:31:30 -0700262 // Movable.
263 FrameEventsDelta(FrameEventsDelta&& src) = default;
264 FrameEventsDelta& operator=(FrameEventsDelta&& src) = default;
265 // Not copyable.
266 FrameEventsDelta(const FrameEventsDelta& src) = delete;
267 FrameEventsDelta& operator=(const FrameEventsDelta& src) = delete;
268
Brian Anderson3890c392016-07-25 12:48:08 -0700269 // Flattenable implementation
270 size_t getFlattenedSize() const;
271 size_t getFdCount() const;
272 status_t flatten(void*& buffer, size_t& size, int*& fds,
273 size_t& count) const;
274 status_t unflatten(void const*& buffer, size_t& size, int const*& fds,
275 size_t& count);
276
277private:
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800278 static constexpr size_t minFlattenedSize();
Brian Anderson3890c392016-07-25 12:48:08 -0700279
280 size_t mIndex{0};
281 uint64_t mFrameNumber{0};
282
283 bool mAddPostCompositeCalled{0};
284 bool mAddRetireCalled{0};
285 bool mAddReleaseCalled{0};
286
Brian Andersoned816e62016-10-26 16:12:21 -0700287 nsecs_t mPostedTime{FrameEvents::TIMESTAMP_PENDING};
288 nsecs_t mRequestedPresentTime{FrameEvents::TIMESTAMP_PENDING};
289 nsecs_t mLatchTime{FrameEvents::TIMESTAMP_PENDING};
290 nsecs_t mFirstRefreshStartTime{FrameEvents::TIMESTAMP_PENDING};
291 nsecs_t mLastRefreshStartTime{FrameEvents::TIMESTAMP_PENDING};
292 nsecs_t mDequeueReadyTime{FrameEvents::TIMESTAMP_PENDING};
Brian Anderson3890c392016-07-25 12:48:08 -0700293
Brian Anderson3d4039d2016-09-23 16:31:30 -0700294 FenceTime::Snapshot mGpuCompositionDoneFence;
295 FenceTime::Snapshot mDisplayPresentFence;
296 FenceTime::Snapshot mDisplayRetireFence;
297 FenceTime::Snapshot mReleaseFence;
Brian Anderson3890c392016-07-25 12:48:08 -0700298
299 // This is a static method with an auto return value so we can call
300 // it without needing const and non-const versions.
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700301 template <typename ThisT>
302 static inline auto allFences(ThisT fed) ->
303 std::array<decltype(&fed->mReleaseFence), 4> {
Brian Anderson3890c392016-07-25 12:48:08 -0700304 return {{
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700305 &fed->mGpuCompositionDoneFence, &fed->mDisplayPresentFence,
306 &fed->mDisplayRetireFence, &fed->mReleaseFence
Brian Anderson3890c392016-07-25 12:48:08 -0700307 }};
308 }
309};
310
311
312// A collection of updates from consumer to producer that can be sent
313// through Binder.
314class FrameEventHistoryDelta
315 : public Flattenable<FrameEventHistoryDelta> {
316
317friend class ConsumerFrameEventHistory;
318friend class ProducerFrameEventHistory;
319
320public:
Brian Anderson3d4039d2016-09-23 16:31:30 -0700321 FrameEventHistoryDelta() = default;
322
323 // Movable.
324 FrameEventHistoryDelta(FrameEventHistoryDelta&& src) = default;
325 FrameEventHistoryDelta& operator=(FrameEventHistoryDelta&& src);
326 // Not copyable.
327 FrameEventHistoryDelta(const FrameEventHistoryDelta& src) = delete;
328 FrameEventHistoryDelta& operator=(
329 const FrameEventHistoryDelta& src) = delete;
330
Brian Anderson3890c392016-07-25 12:48:08 -0700331 // Flattenable implementation.
332 size_t getFlattenedSize() const;
333 size_t getFdCount() const;
334 status_t flatten(void*& buffer, size_t& size, int*& fds,
335 size_t& count) const;
336 status_t unflatten(void const*& buffer, size_t& size, int const*& fds,
337 size_t& count);
338
339private:
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800340 static constexpr size_t minFlattenedSize();
Brian Anderson3890c392016-07-25 12:48:08 -0700341
342 std::vector<FrameEventsDelta> mDeltas;
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800343 CompositorTiming mCompositorTiming;
Brian Anderson3890c392016-07-25 12:48:08 -0700344};
345
346
Pablo Ceballosce796e72016-02-04 19:10:51 -0800347} // namespace android
348#endif