blob: 9716be4bfd186b8d63be8a899f33ba13243ffea5 [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,
Brian Andersonf6386862016-10-31 16:34:13 -070046 DEQUEUE_READY,
Brian Anderson3890c392016-07-25 12:48:08 -070047 RELEASE,
48 EVENT_COUNT, // Not an actual event.
Pablo Ceballosce796e72016-02-04 19:10:51 -080049};
50
Brian Andersond6927fb2016-07-23 23:37:30 -070051
52// A collection of timestamps corresponding to a single frame.
53struct FrameEvents {
Brian Andersoned816e62016-10-26 16:12:21 -070054 static constexpr auto EVENT_COUNT =
55 static_cast<size_t>(FrameEvent::EVENT_COUNT);
56 static_assert(EVENT_COUNT <= 32, "Event count sanity check failed.");
Brian Andersondc96fdf2017-03-20 16:54:25 -070057 static constexpr nsecs_t TIMESTAMP_PENDING = -2;
Brian Andersoned816e62016-10-26 16:12:21 -070058
59 static inline bool isValidTimestamp(nsecs_t time) {
60 return time != TIMESTAMP_PENDING;
61 }
62
Brian Anderson3890c392016-07-25 12:48:08 -070063 bool hasPostedInfo() const;
64 bool hasRequestedPresentInfo() const;
65 bool hasLatchInfo() const;
66 bool hasFirstRefreshStartInfo() const;
67 bool hasLastRefreshStartInfo() const;
68 bool hasAcquireInfo() const;
69 bool hasGpuCompositionDoneInfo() const;
70 bool hasDisplayPresentInfo() const;
Brian Anderson3890c392016-07-25 12:48:08 -070071 bool hasReleaseInfo() const;
Brian Andersonf6386862016-10-31 16:34:13 -070072 bool hasDequeueReadyInfo() const;
Brian Anderson3890c392016-07-25 12:48:08 -070073
Brian Andersond6927fb2016-07-23 23:37:30 -070074 void checkFencesForCompletion();
75 void dump(String8& outString) const;
76
77 bool valid{false};
Brian Anderson5ea5e592016-12-01 16:54:33 -080078 int connectId{0};
Brian Andersond6927fb2016-07-23 23:37:30 -070079 uint64_t frameNumber{0};
80
81 // Whether or not certain points in the frame's life cycle have been
82 // encountered help us determine if timestamps aren't available because
83 // a) we'll just never get them or b) they're not ready yet.
84 bool addPostCompositeCalled{false};
Brian Anderson3890c392016-07-25 12:48:08 -070085 bool addReleaseCalled{false};
Brian Andersond6927fb2016-07-23 23:37:30 -070086
Brian Andersoned816e62016-10-26 16:12:21 -070087 nsecs_t postedTime{TIMESTAMP_PENDING};
88 nsecs_t requestedPresentTime{TIMESTAMP_PENDING};
89 nsecs_t latchTime{TIMESTAMP_PENDING};
90 nsecs_t firstRefreshStartTime{TIMESTAMP_PENDING};
91 nsecs_t lastRefreshStartTime{TIMESTAMP_PENDING};
92 nsecs_t dequeueReadyTime{TIMESTAMP_PENDING};
Brian Andersond6927fb2016-07-23 23:37:30 -070093
Brian Anderson3d4039d2016-09-23 16:31:30 -070094 std::shared_ptr<FenceTime> acquireFence{FenceTime::NO_FENCE};
95 std::shared_ptr<FenceTime> gpuCompositionDoneFence{FenceTime::NO_FENCE};
96 std::shared_ptr<FenceTime> displayPresentFence{FenceTime::NO_FENCE};
Brian Anderson3d4039d2016-09-23 16:31:30 -070097 std::shared_ptr<FenceTime> releaseFence{FenceTime::NO_FENCE};
Brian Andersond6927fb2016-07-23 23:37:30 -070098};
99
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800100struct CompositorTiming {
101 nsecs_t deadline{0};
102 nsecs_t interval{16666667};
Brian Andersond0010582017-03-07 13:20:31 -0800103 nsecs_t presentLatency{16666667};
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800104};
Brian Andersond6927fb2016-07-23 23:37:30 -0700105
Brian Anderson3890c392016-07-25 12:48:08 -0700106// A short history of frames that are synchronized between the consumer and
107// producer via deltas.
108class FrameEventHistory {
109public:
110 virtual ~FrameEventHistory();
111
112 FrameEvents* getFrame(uint64_t frameNumber);
113 FrameEvents* getFrame(uint64_t frameNumber, size_t* iHint);
114 void checkFencesForCompletion();
115 void dump(String8& outString) const;
116
117 static constexpr size_t MAX_FRAME_HISTORY = 8;
118
119protected:
120 std::array<FrameEvents, MAX_FRAME_HISTORY> mFrames;
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800121
122 CompositorTiming mCompositorTiming;
Brian Anderson3890c392016-07-25 12:48:08 -0700123};
124
125
126// The producer's interface to FrameEventHistory
127class ProducerFrameEventHistory : public FrameEventHistory {
128public:
129 ~ProducerFrameEventHistory() override;
130
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800131 // Public for testing.
132 static nsecs_t snapToNextTick(
133 nsecs_t timestamp, nsecs_t tickPhase, nsecs_t tickInterval);
134
135 nsecs_t getNextCompositeDeadline(const nsecs_t now) const;
136 nsecs_t getCompositeInterval() const { return mCompositorTiming.interval; }
137 nsecs_t getCompositeToPresentLatency() const {
138 return mCompositorTiming.presentLatency;
139 }
140
Brian Anderson3da8d272016-07-28 16:20:47 -0700141 // virtual for testing.
142 virtual void updateAcquireFence(
Brian Anderson3d4039d2016-09-23 16:31:30 -0700143 uint64_t frameNumber, std::shared_ptr<FenceTime>&& acquire);
Brian Anderson3890c392016-07-25 12:48:08 -0700144 void applyDelta(const FrameEventHistoryDelta& delta);
145
Brian Anderson3d4039d2016-09-23 16:31:30 -0700146 void updateSignalTimes();
147
Brian Anderson3da8d272016-07-28 16:20:47 -0700148protected:
149 void applyFenceDelta(FenceTimeline* timeline,
150 std::shared_ptr<FenceTime>* dst,
151 const FenceTime::Snapshot& src) const;
152
153 // virtual for testing.
154 virtual std::shared_ptr<FenceTime> createFenceTime(
155 const sp<Fence>& fence) const;
156
Brian Anderson3890c392016-07-25 12:48:08 -0700157 size_t mAcquireOffset{0};
Brian Anderson3d4039d2016-09-23 16:31:30 -0700158
159 // The consumer updates it's timelines in Layer and SurfaceFlinger since
160 // they can coordinate shared timelines better. The producer doesn't have
161 // shared timelines though, so just let it own and update all of them.
162 FenceTimeline mAcquireTimeline;
163 FenceTimeline mGpuCompositionDoneTimeline;
164 FenceTimeline mPresentTimeline;
Brian Anderson3d4039d2016-09-23 16:31:30 -0700165 FenceTimeline mReleaseTimeline;
Brian Anderson3890c392016-07-25 12:48:08 -0700166};
167
168
169// Used by the consumer to create a new frame event record that is
170// partially complete.
Brian Andersond6927fb2016-07-23 23:37:30 -0700171struct NewFrameEventsEntry {
172 uint64_t frameNumber{0};
173 nsecs_t postedTime{0};
174 nsecs_t requestedPresentTime{0};
Brian Anderson3d4039d2016-09-23 16:31:30 -0700175 std::shared_ptr<FenceTime> acquireFence{FenceTime::NO_FENCE};
Brian Andersond6927fb2016-07-23 23:37:30 -0700176};
177
178
Brian Anderson3890c392016-07-25 12:48:08 -0700179// Used by the consumer to keep track of which fields it already sent to
180// the producer.
181class FrameEventDirtyFields {
Brian Andersond6927fb2016-07-23 23:37:30 -0700182public:
Brian Anderson3890c392016-07-25 12:48:08 -0700183 inline void reset() { mBitset.reset(); }
184 inline bool anyDirty() const { return mBitset.any(); }
Brian Andersond6927fb2016-07-23 23:37:30 -0700185
Brian Anderson3890c392016-07-25 12:48:08 -0700186 template <FrameEvent event>
187 inline void setDirty() {
188 constexpr size_t eventIndex = static_cast<size_t>(event);
189 static_assert(eventIndex < FrameEvents::EVENT_COUNT, "Bad index.");
190 mBitset.set(eventIndex);
191 }
192
193 template <FrameEvent event>
194 inline bool isDirty() const {
195 constexpr size_t eventIndex = static_cast<size_t>(event);
196 static_assert(eventIndex < FrameEvents::EVENT_COUNT, "Bad index.");
197 return mBitset[eventIndex];
198 }
199
200private:
201 std::bitset<FrameEvents::EVENT_COUNT> mBitset;
202};
203
204
205// The consumer's interface to FrameEventHistory
206class ConsumerFrameEventHistory : public FrameEventHistory {
207public:
208 ~ConsumerFrameEventHistory() override;
209
Brian Anderson5ea5e592016-12-01 16:54:33 -0800210 void onDisconnect();
211
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800212 void initializeCompositorTiming(const CompositorTiming& compositorTiming);
213
Brian Anderson3890c392016-07-25 12:48:08 -0700214 void addQueue(const NewFrameEventsEntry& newEntry);
Brian Andersond6927fb2016-07-23 23:37:30 -0700215 void addLatch(uint64_t frameNumber, nsecs_t latchTime);
216 void addPreComposition(uint64_t frameNumber, nsecs_t refreshStartTime);
217 void addPostComposition(uint64_t frameNumber,
Brian Anderson3d4039d2016-09-23 16:31:30 -0700218 const std::shared_ptr<FenceTime>& gpuCompositionDone,
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800219 const std::shared_ptr<FenceTime>& displayPresent,
220 const CompositorTiming& compositorTiming);
Brian Andersonf6386862016-10-31 16:34:13 -0700221 void addRelease(uint64_t frameNumber, nsecs_t dequeueReadyTime,
Brian Anderson3d4039d2016-09-23 16:31:30 -0700222 std::shared_ptr<FenceTime>&& release);
Brian Andersond6927fb2016-07-23 23:37:30 -0700223
Brian Anderson3890c392016-07-25 12:48:08 -0700224 void getAndResetDelta(FrameEventHistoryDelta* delta);
225
Brian Andersond6927fb2016-07-23 23:37:30 -0700226private:
Brian Anderson3d4039d2016-09-23 16:31:30 -0700227 void getFrameDelta(FrameEventHistoryDelta* delta,
228 const std::array<FrameEvents, MAX_FRAME_HISTORY>::iterator& frame);
229
Brian Anderson3890c392016-07-25 12:48:08 -0700230 std::array<FrameEventDirtyFields, MAX_FRAME_HISTORY> mFramesDirty;
Brian Anderson5ea5e592016-12-01 16:54:33 -0800231
Brian Andersond6927fb2016-07-23 23:37:30 -0700232 size_t mQueueOffset{0};
233 size_t mCompositionOffset{0};
Brian Andersond6927fb2016-07-23 23:37:30 -0700234 size_t mReleaseOffset{0};
Brian Anderson4565daa2016-12-13 15:41:28 -0800235
Brian Anderson5ea5e592016-12-01 16:54:33 -0800236 int mCurrentConnectId{0};
Brian Anderson4565daa2016-12-13 15:41:28 -0800237 bool mProducerWantsEvents{false};
Brian Andersond6927fb2016-07-23 23:37:30 -0700238};
239
Brian Anderson3890c392016-07-25 12:48:08 -0700240
241// A single frame update from the consumer to producer that can be sent
242// through Binder.
243// Although this may be sent multiple times for the same frame as new
244// timestamps are set, Fences only need to be sent once.
245class FrameEventsDelta : public Flattenable<FrameEventsDelta> {
246friend class ProducerFrameEventHistory;
247public:
248 FrameEventsDelta() = default;
249 FrameEventsDelta(size_t index,
250 const FrameEvents& frameTimestamps,
251 const FrameEventDirtyFields& dirtyFields);
252
Brian Anderson3d4039d2016-09-23 16:31:30 -0700253 // Movable.
254 FrameEventsDelta(FrameEventsDelta&& src) = default;
255 FrameEventsDelta& operator=(FrameEventsDelta&& src) = default;
256 // Not copyable.
257 FrameEventsDelta(const FrameEventsDelta& src) = delete;
258 FrameEventsDelta& operator=(const FrameEventsDelta& src) = delete;
259
Brian Anderson3890c392016-07-25 12:48:08 -0700260 // Flattenable implementation
261 size_t getFlattenedSize() const;
262 size_t getFdCount() const;
263 status_t flatten(void*& buffer, size_t& size, int*& fds,
264 size_t& count) const;
265 status_t unflatten(void const*& buffer, size_t& size, int const*& fds,
266 size_t& count);
267
268private:
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800269 static constexpr size_t minFlattenedSize();
Brian Anderson3890c392016-07-25 12:48:08 -0700270
271 size_t mIndex{0};
272 uint64_t mFrameNumber{0};
273
274 bool mAddPostCompositeCalled{0};
Brian Anderson3890c392016-07-25 12:48:08 -0700275 bool mAddReleaseCalled{0};
276
Brian Andersoned816e62016-10-26 16:12:21 -0700277 nsecs_t mPostedTime{FrameEvents::TIMESTAMP_PENDING};
278 nsecs_t mRequestedPresentTime{FrameEvents::TIMESTAMP_PENDING};
279 nsecs_t mLatchTime{FrameEvents::TIMESTAMP_PENDING};
280 nsecs_t mFirstRefreshStartTime{FrameEvents::TIMESTAMP_PENDING};
281 nsecs_t mLastRefreshStartTime{FrameEvents::TIMESTAMP_PENDING};
282 nsecs_t mDequeueReadyTime{FrameEvents::TIMESTAMP_PENDING};
Brian Anderson3890c392016-07-25 12:48:08 -0700283
Brian Anderson3d4039d2016-09-23 16:31:30 -0700284 FenceTime::Snapshot mGpuCompositionDoneFence;
285 FenceTime::Snapshot mDisplayPresentFence;
Brian Anderson3d4039d2016-09-23 16:31:30 -0700286 FenceTime::Snapshot mReleaseFence;
Brian Anderson3890c392016-07-25 12:48:08 -0700287
288 // This is a static method with an auto return value so we can call
289 // it without needing const and non-const versions.
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700290 template <typename ThisT>
291 static inline auto allFences(ThisT fed) ->
Brian Anderson4e606e32017-03-16 15:34:57 -0700292 std::array<decltype(&fed->mReleaseFence), 3> {
Brian Anderson3890c392016-07-25 12:48:08 -0700293 return {{
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700294 &fed->mGpuCompositionDoneFence, &fed->mDisplayPresentFence,
Brian Anderson4e606e32017-03-16 15:34:57 -0700295 &fed->mReleaseFence
Brian Anderson3890c392016-07-25 12:48:08 -0700296 }};
297 }
298};
299
300
301// A collection of updates from consumer to producer that can be sent
302// through Binder.
303class FrameEventHistoryDelta
304 : public Flattenable<FrameEventHistoryDelta> {
305
306friend class ConsumerFrameEventHistory;
307friend class ProducerFrameEventHistory;
308
309public:
Brian Anderson3d4039d2016-09-23 16:31:30 -0700310 FrameEventHistoryDelta() = default;
311
312 // Movable.
313 FrameEventHistoryDelta(FrameEventHistoryDelta&& src) = default;
314 FrameEventHistoryDelta& operator=(FrameEventHistoryDelta&& src);
315 // Not copyable.
316 FrameEventHistoryDelta(const FrameEventHistoryDelta& src) = delete;
317 FrameEventHistoryDelta& operator=(
318 const FrameEventHistoryDelta& src) = delete;
319
Brian Anderson3890c392016-07-25 12:48:08 -0700320 // Flattenable implementation.
321 size_t getFlattenedSize() const;
322 size_t getFdCount() const;
323 status_t flatten(void*& buffer, size_t& size, int*& fds,
324 size_t& count) const;
325 status_t unflatten(void const*& buffer, size_t& size, int const*& fds,
326 size_t& count);
327
328private:
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800329 static constexpr size_t minFlattenedSize();
Brian Anderson3890c392016-07-25 12:48:08 -0700330
331 std::vector<FrameEventsDelta> mDeltas;
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800332 CompositorTiming mCompositorTiming;
Brian Anderson3890c392016-07-25 12:48:08 -0700333};
334
335
Pablo Ceballosce796e72016-02-04 19:10:51 -0800336} // namespace android
337#endif