blob: 174c5ea525e568a549d659aae7383c306c53a518 [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 Anderson3890c392016-07-25 12:48:08 -070055 bool hasPostedInfo() const;
56 bool hasRequestedPresentInfo() const;
57 bool hasLatchInfo() const;
58 bool hasFirstRefreshStartInfo() const;
59 bool hasLastRefreshStartInfo() const;
60 bool hasAcquireInfo() const;
61 bool hasGpuCompositionDoneInfo() const;
62 bool hasDisplayPresentInfo() const;
63 bool hasDisplayRetireInfo() const;
64 bool hasReleaseInfo() const;
Brian Andersonf6386862016-10-31 16:34:13 -070065 bool hasDequeueReadyInfo() const;
Brian Anderson3890c392016-07-25 12:48:08 -070066
Brian Andersond6927fb2016-07-23 23:37:30 -070067 void checkFencesForCompletion();
68 void dump(String8& outString) const;
69
Brian Anderson3890c392016-07-25 12:48:08 -070070 static constexpr size_t EVENT_COUNT =
71 static_cast<size_t>(FrameEvent::EVENT_COUNT);
72 static_assert(EVENT_COUNT <= 32, "Event count sanity check failed.");
73
Brian Andersond6927fb2016-07-23 23:37:30 -070074 bool valid{false};
75 uint64_t frameNumber{0};
76
77 // Whether or not certain points in the frame's life cycle have been
78 // encountered help us determine if timestamps aren't available because
79 // a) we'll just never get them or b) they're not ready yet.
80 bool addPostCompositeCalled{false};
81 bool addRetireCalled{false};
Brian Anderson3890c392016-07-25 12:48:08 -070082 bool addReleaseCalled{false};
Brian Andersond6927fb2016-07-23 23:37:30 -070083
Brian Anderson3d4039d2016-09-23 16:31:30 -070084 nsecs_t postedTime{-1};
85 nsecs_t requestedPresentTime{-1};
86 nsecs_t latchTime{-1};
87 nsecs_t firstRefreshStartTime{-1};
88 nsecs_t lastRefreshStartTime{-1};
Brian Andersonf6386862016-10-31 16:34:13 -070089 nsecs_t dequeueReadyTime{-1};
Brian Andersond6927fb2016-07-23 23:37:30 -070090
Brian Anderson3d4039d2016-09-23 16:31:30 -070091 std::shared_ptr<FenceTime> acquireFence{FenceTime::NO_FENCE};
92 std::shared_ptr<FenceTime> gpuCompositionDoneFence{FenceTime::NO_FENCE};
93 std::shared_ptr<FenceTime> displayPresentFence{FenceTime::NO_FENCE};
94 std::shared_ptr<FenceTime> displayRetireFence{FenceTime::NO_FENCE};
95 std::shared_ptr<FenceTime> releaseFence{FenceTime::NO_FENCE};
Brian Andersond6927fb2016-07-23 23:37:30 -070096};
97
Brian Anderson0a61b0c2016-12-07 14:55:56 -080098struct CompositorTiming {
99 nsecs_t deadline{0};
100 nsecs_t interval{16666667};
101 nsecs_t presentLatency{0};
102};
Brian Andersond6927fb2016-07-23 23:37:30 -0700103
Brian Anderson3890c392016-07-25 12:48:08 -0700104// A short history of frames that are synchronized between the consumer and
105// producer via deltas.
106class FrameEventHistory {
107public:
108 virtual ~FrameEventHistory();
109
110 FrameEvents* getFrame(uint64_t frameNumber);
111 FrameEvents* getFrame(uint64_t frameNumber, size_t* iHint);
112 void checkFencesForCompletion();
113 void dump(String8& outString) const;
114
115 static constexpr size_t MAX_FRAME_HISTORY = 8;
116
117protected:
118 std::array<FrameEvents, MAX_FRAME_HISTORY> mFrames;
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800119
120 CompositorTiming mCompositorTiming;
Brian Anderson3890c392016-07-25 12:48:08 -0700121};
122
123
124// The producer's interface to FrameEventHistory
125class ProducerFrameEventHistory : public FrameEventHistory {
126public:
127 ~ProducerFrameEventHistory() override;
128
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800129 // Public for testing.
130 static nsecs_t snapToNextTick(
131 nsecs_t timestamp, nsecs_t tickPhase, nsecs_t tickInterval);
132
133 nsecs_t getNextCompositeDeadline(const nsecs_t now) const;
134 nsecs_t getCompositeInterval() const { return mCompositorTiming.interval; }
135 nsecs_t getCompositeToPresentLatency() const {
136 return mCompositorTiming.presentLatency;
137 }
138
Brian Anderson3da8d272016-07-28 16:20:47 -0700139 // virtual for testing.
140 virtual void updateAcquireFence(
Brian Anderson3d4039d2016-09-23 16:31:30 -0700141 uint64_t frameNumber, std::shared_ptr<FenceTime>&& acquire);
Brian Anderson3890c392016-07-25 12:48:08 -0700142 void applyDelta(const FrameEventHistoryDelta& delta);
143
Brian Anderson3d4039d2016-09-23 16:31:30 -0700144 void updateSignalTimes();
145
Brian Anderson3da8d272016-07-28 16:20:47 -0700146protected:
147 void applyFenceDelta(FenceTimeline* timeline,
148 std::shared_ptr<FenceTime>* dst,
149 const FenceTime::Snapshot& src) const;
150
151 // virtual for testing.
152 virtual std::shared_ptr<FenceTime> createFenceTime(
153 const sp<Fence>& fence) const;
154
Brian Anderson3890c392016-07-25 12:48:08 -0700155 size_t mAcquireOffset{0};
Brian Anderson3d4039d2016-09-23 16:31:30 -0700156
157 // The consumer updates it's timelines in Layer and SurfaceFlinger since
158 // they can coordinate shared timelines better. The producer doesn't have
159 // shared timelines though, so just let it own and update all of them.
160 FenceTimeline mAcquireTimeline;
161 FenceTimeline mGpuCompositionDoneTimeline;
162 FenceTimeline mPresentTimeline;
163 FenceTimeline mRetireTimeline;
164 FenceTimeline mReleaseTimeline;
Brian Anderson3890c392016-07-25 12:48:08 -0700165};
166
167
168// Used by the consumer to create a new frame event record that is
169// partially complete.
Brian Andersond6927fb2016-07-23 23:37:30 -0700170struct NewFrameEventsEntry {
171 uint64_t frameNumber{0};
172 nsecs_t postedTime{0};
173 nsecs_t requestedPresentTime{0};
Brian Anderson3d4039d2016-09-23 16:31:30 -0700174 std::shared_ptr<FenceTime> acquireFence{FenceTime::NO_FENCE};
Brian Andersond6927fb2016-07-23 23:37:30 -0700175};
176
177
Brian Anderson3890c392016-07-25 12:48:08 -0700178// Used by the consumer to keep track of which fields it already sent to
179// the producer.
180class FrameEventDirtyFields {
Brian Andersond6927fb2016-07-23 23:37:30 -0700181public:
Brian Anderson3890c392016-07-25 12:48:08 -0700182 inline void reset() { mBitset.reset(); }
183 inline bool anyDirty() const { return mBitset.any(); }
Brian Andersond6927fb2016-07-23 23:37:30 -0700184
Brian Anderson3890c392016-07-25 12:48:08 -0700185 template <FrameEvent event>
186 inline void setDirty() {
187 constexpr size_t eventIndex = static_cast<size_t>(event);
188 static_assert(eventIndex < FrameEvents::EVENT_COUNT, "Bad index.");
189 mBitset.set(eventIndex);
190 }
191
192 template <FrameEvent event>
193 inline bool isDirty() const {
194 constexpr size_t eventIndex = static_cast<size_t>(event);
195 static_assert(eventIndex < FrameEvents::EVENT_COUNT, "Bad index.");
196 return mBitset[eventIndex];
197 }
198
199private:
200 std::bitset<FrameEvents::EVENT_COUNT> mBitset;
201};
202
203
204// The consumer's interface to FrameEventHistory
205class ConsumerFrameEventHistory : public FrameEventHistory {
206public:
207 ~ConsumerFrameEventHistory() override;
208
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800209 void initializeCompositorTiming(const CompositorTiming& compositorTiming);
210
Brian Anderson3890c392016-07-25 12:48:08 -0700211 void addQueue(const NewFrameEventsEntry& newEntry);
Brian Andersond6927fb2016-07-23 23:37:30 -0700212 void addLatch(uint64_t frameNumber, nsecs_t latchTime);
213 void addPreComposition(uint64_t frameNumber, nsecs_t refreshStartTime);
214 void addPostComposition(uint64_t frameNumber,
Brian Anderson3d4039d2016-09-23 16:31:30 -0700215 const std::shared_ptr<FenceTime>& gpuCompositionDone,
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800216 const std::shared_ptr<FenceTime>& displayPresent,
217 const CompositorTiming& compositorTiming);
Brian Anderson3d4039d2016-09-23 16:31:30 -0700218 void addRetire(uint64_t frameNumber,
219 const std::shared_ptr<FenceTime>& displayRetire);
Brian Andersonf6386862016-10-31 16:34:13 -0700220 void addRelease(uint64_t frameNumber, nsecs_t dequeueReadyTime,
Brian Anderson3d4039d2016-09-23 16:31:30 -0700221 std::shared_ptr<FenceTime>&& release);
Brian Andersond6927fb2016-07-23 23:37:30 -0700222
Brian Anderson3890c392016-07-25 12:48:08 -0700223 void getAndResetDelta(FrameEventHistoryDelta* delta);
224
Brian Andersond6927fb2016-07-23 23:37:30 -0700225private:
Brian Anderson3d4039d2016-09-23 16:31:30 -0700226 void getFrameDelta(FrameEventHistoryDelta* delta,
227 const std::array<FrameEvents, MAX_FRAME_HISTORY>::iterator& frame);
228
Brian Anderson3890c392016-07-25 12:48:08 -0700229 std::array<FrameEventDirtyFields, MAX_FRAME_HISTORY> mFramesDirty;
Brian Andersond6927fb2016-07-23 23:37:30 -0700230 size_t mQueueOffset{0};
231 size_t mCompositionOffset{0};
232 size_t mRetireOffset{0};
233 size_t mReleaseOffset{0};
Brian Anderson4565daa2016-12-13 15:41:28 -0800234
235 bool mProducerWantsEvents{false};
Brian Andersond6927fb2016-07-23 23:37:30 -0700236};
237
Brian Anderson3890c392016-07-25 12:48:08 -0700238
239// A single frame update from the consumer to producer that can be sent
240// through Binder.
241// Although this may be sent multiple times for the same frame as new
242// timestamps are set, Fences only need to be sent once.
243class FrameEventsDelta : public Flattenable<FrameEventsDelta> {
244friend class ProducerFrameEventHistory;
245public:
246 FrameEventsDelta() = default;
247 FrameEventsDelta(size_t index,
248 const FrameEvents& frameTimestamps,
249 const FrameEventDirtyFields& dirtyFields);
250
Brian Anderson3d4039d2016-09-23 16:31:30 -0700251 // Movable.
252 FrameEventsDelta(FrameEventsDelta&& src) = default;
253 FrameEventsDelta& operator=(FrameEventsDelta&& src) = default;
254 // Not copyable.
255 FrameEventsDelta(const FrameEventsDelta& src) = delete;
256 FrameEventsDelta& operator=(const FrameEventsDelta& src) = delete;
257
Brian Anderson3890c392016-07-25 12:48:08 -0700258 // Flattenable implementation
259 size_t getFlattenedSize() const;
260 size_t getFdCount() const;
261 status_t flatten(void*& buffer, size_t& size, int*& fds,
262 size_t& count) const;
263 status_t unflatten(void const*& buffer, size_t& size, int const*& fds,
264 size_t& count);
265
266private:
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800267 static constexpr size_t minFlattenedSize();
Brian Anderson3890c392016-07-25 12:48:08 -0700268
269 size_t mIndex{0};
270 uint64_t mFrameNumber{0};
271
272 bool mAddPostCompositeCalled{0};
273 bool mAddRetireCalled{0};
274 bool mAddReleaseCalled{0};
275
276 nsecs_t mPostedTime{0};
277 nsecs_t mRequestedPresentTime{0};
278 nsecs_t mLatchTime{0};
279 nsecs_t mFirstRefreshStartTime{0};
280 nsecs_t mLastRefreshStartTime{0};
Brian Andersonf6386862016-10-31 16:34:13 -0700281 nsecs_t mDequeueReadyTime{0};
Brian Anderson3890c392016-07-25 12:48:08 -0700282
Brian Anderson3d4039d2016-09-23 16:31:30 -0700283 FenceTime::Snapshot mGpuCompositionDoneFence;
284 FenceTime::Snapshot mDisplayPresentFence;
285 FenceTime::Snapshot mDisplayRetireFence;
286 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) ->
292 std::array<decltype(&fed->mReleaseFence), 4> {
Brian Anderson3890c392016-07-25 12:48:08 -0700293 return {{
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700294 &fed->mGpuCompositionDoneFence, &fed->mDisplayPresentFence,
295 &fed->mDisplayRetireFence, &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