blob: 0c4af469c4e739070639e123f21404a1a455b150 [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};
81 uint64_t frameNumber{0};
82
83 // Whether or not certain points in the frame's life cycle have been
84 // encountered help us determine if timestamps aren't available because
85 // a) we'll just never get them or b) they're not ready yet.
86 bool addPostCompositeCalled{false};
87 bool addRetireCalled{false};
Brian Anderson3890c392016-07-25 12:48:08 -070088 bool addReleaseCalled{false};
Brian Andersond6927fb2016-07-23 23:37:30 -070089
Brian Andersoned816e62016-10-26 16:12:21 -070090 nsecs_t postedTime{TIMESTAMP_PENDING};
91 nsecs_t requestedPresentTime{TIMESTAMP_PENDING};
92 nsecs_t latchTime{TIMESTAMP_PENDING};
93 nsecs_t firstRefreshStartTime{TIMESTAMP_PENDING};
94 nsecs_t lastRefreshStartTime{TIMESTAMP_PENDING};
95 nsecs_t dequeueReadyTime{TIMESTAMP_PENDING};
Brian Andersond6927fb2016-07-23 23:37:30 -070096
Brian Anderson3d4039d2016-09-23 16:31:30 -070097 std::shared_ptr<FenceTime> acquireFence{FenceTime::NO_FENCE};
98 std::shared_ptr<FenceTime> gpuCompositionDoneFence{FenceTime::NO_FENCE};
99 std::shared_ptr<FenceTime> displayPresentFence{FenceTime::NO_FENCE};
100 std::shared_ptr<FenceTime> displayRetireFence{FenceTime::NO_FENCE};
101 std::shared_ptr<FenceTime> releaseFence{FenceTime::NO_FENCE};
Brian Andersond6927fb2016-07-23 23:37:30 -0700102};
103
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800104struct CompositorTiming {
105 nsecs_t deadline{0};
106 nsecs_t interval{16666667};
107 nsecs_t presentLatency{0};
108};
Brian Andersond6927fb2016-07-23 23:37:30 -0700109
Brian Anderson3890c392016-07-25 12:48:08 -0700110// A short history of frames that are synchronized between the consumer and
111// producer via deltas.
112class FrameEventHistory {
113public:
114 virtual ~FrameEventHistory();
115
116 FrameEvents* getFrame(uint64_t frameNumber);
117 FrameEvents* getFrame(uint64_t frameNumber, size_t* iHint);
118 void checkFencesForCompletion();
119 void dump(String8& outString) const;
120
121 static constexpr size_t MAX_FRAME_HISTORY = 8;
122
123protected:
124 std::array<FrameEvents, MAX_FRAME_HISTORY> mFrames;
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800125
126 CompositorTiming mCompositorTiming;
Brian Anderson3890c392016-07-25 12:48:08 -0700127};
128
129
130// The producer's interface to FrameEventHistory
131class ProducerFrameEventHistory : public FrameEventHistory {
132public:
133 ~ProducerFrameEventHistory() override;
134
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800135 // Public for testing.
136 static nsecs_t snapToNextTick(
137 nsecs_t timestamp, nsecs_t tickPhase, nsecs_t tickInterval);
138
139 nsecs_t getNextCompositeDeadline(const nsecs_t now) const;
140 nsecs_t getCompositeInterval() const { return mCompositorTiming.interval; }
141 nsecs_t getCompositeToPresentLatency() const {
142 return mCompositorTiming.presentLatency;
143 }
144
Brian Anderson3da8d272016-07-28 16:20:47 -0700145 // virtual for testing.
146 virtual void updateAcquireFence(
Brian Anderson3d4039d2016-09-23 16:31:30 -0700147 uint64_t frameNumber, std::shared_ptr<FenceTime>&& acquire);
Brian Anderson3890c392016-07-25 12:48:08 -0700148 void applyDelta(const FrameEventHistoryDelta& delta);
149
Brian Anderson3d4039d2016-09-23 16:31:30 -0700150 void updateSignalTimes();
151
Brian Anderson3da8d272016-07-28 16:20:47 -0700152protected:
153 void applyFenceDelta(FenceTimeline* timeline,
154 std::shared_ptr<FenceTime>* dst,
155 const FenceTime::Snapshot& src) const;
156
157 // virtual for testing.
158 virtual std::shared_ptr<FenceTime> createFenceTime(
159 const sp<Fence>& fence) const;
160
Brian Anderson3890c392016-07-25 12:48:08 -0700161 size_t mAcquireOffset{0};
Brian Anderson3d4039d2016-09-23 16:31:30 -0700162
163 // The consumer updates it's timelines in Layer and SurfaceFlinger since
164 // they can coordinate shared timelines better. The producer doesn't have
165 // shared timelines though, so just let it own and update all of them.
166 FenceTimeline mAcquireTimeline;
167 FenceTimeline mGpuCompositionDoneTimeline;
168 FenceTimeline mPresentTimeline;
169 FenceTimeline mRetireTimeline;
170 FenceTimeline mReleaseTimeline;
Brian Anderson3890c392016-07-25 12:48:08 -0700171};
172
173
174// Used by the consumer to create a new frame event record that is
175// partially complete.
Brian Andersond6927fb2016-07-23 23:37:30 -0700176struct NewFrameEventsEntry {
177 uint64_t frameNumber{0};
178 nsecs_t postedTime{0};
179 nsecs_t requestedPresentTime{0};
Brian Anderson3d4039d2016-09-23 16:31:30 -0700180 std::shared_ptr<FenceTime> acquireFence{FenceTime::NO_FENCE};
Brian Andersond6927fb2016-07-23 23:37:30 -0700181};
182
183
Brian Anderson3890c392016-07-25 12:48:08 -0700184// Used by the consumer to keep track of which fields it already sent to
185// the producer.
186class FrameEventDirtyFields {
Brian Andersond6927fb2016-07-23 23:37:30 -0700187public:
Brian Anderson3890c392016-07-25 12:48:08 -0700188 inline void reset() { mBitset.reset(); }
189 inline bool anyDirty() const { return mBitset.any(); }
Brian Andersond6927fb2016-07-23 23:37:30 -0700190
Brian Anderson3890c392016-07-25 12:48:08 -0700191 template <FrameEvent event>
192 inline void setDirty() {
193 constexpr size_t eventIndex = static_cast<size_t>(event);
194 static_assert(eventIndex < FrameEvents::EVENT_COUNT, "Bad index.");
195 mBitset.set(eventIndex);
196 }
197
198 template <FrameEvent event>
199 inline bool isDirty() const {
200 constexpr size_t eventIndex = static_cast<size_t>(event);
201 static_assert(eventIndex < FrameEvents::EVENT_COUNT, "Bad index.");
202 return mBitset[eventIndex];
203 }
204
205private:
206 std::bitset<FrameEvents::EVENT_COUNT> mBitset;
207};
208
209
210// The consumer's interface to FrameEventHistory
211class ConsumerFrameEventHistory : public FrameEventHistory {
212public:
213 ~ConsumerFrameEventHistory() override;
214
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800215 void initializeCompositorTiming(const CompositorTiming& compositorTiming);
216
Brian Anderson3890c392016-07-25 12:48:08 -0700217 void addQueue(const NewFrameEventsEntry& newEntry);
Brian Andersond6927fb2016-07-23 23:37:30 -0700218 void addLatch(uint64_t frameNumber, nsecs_t latchTime);
219 void addPreComposition(uint64_t frameNumber, nsecs_t refreshStartTime);
220 void addPostComposition(uint64_t frameNumber,
Brian Anderson3d4039d2016-09-23 16:31:30 -0700221 const std::shared_ptr<FenceTime>& gpuCompositionDone,
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800222 const std::shared_ptr<FenceTime>& displayPresent,
223 const CompositorTiming& compositorTiming);
Brian Anderson3d4039d2016-09-23 16:31:30 -0700224 void addRetire(uint64_t frameNumber,
225 const std::shared_ptr<FenceTime>& displayRetire);
Brian Andersonf6386862016-10-31 16:34:13 -0700226 void addRelease(uint64_t frameNumber, nsecs_t dequeueReadyTime,
Brian Anderson3d4039d2016-09-23 16:31:30 -0700227 std::shared_ptr<FenceTime>&& release);
Brian Andersond6927fb2016-07-23 23:37:30 -0700228
Brian Anderson3890c392016-07-25 12:48:08 -0700229 void getAndResetDelta(FrameEventHistoryDelta* delta);
230
Brian Andersond6927fb2016-07-23 23:37:30 -0700231private:
Brian Anderson3d4039d2016-09-23 16:31:30 -0700232 void getFrameDelta(FrameEventHistoryDelta* delta,
233 const std::array<FrameEvents, MAX_FRAME_HISTORY>::iterator& frame);
234
Brian Anderson3890c392016-07-25 12:48:08 -0700235 std::array<FrameEventDirtyFields, MAX_FRAME_HISTORY> mFramesDirty;
Brian Andersond6927fb2016-07-23 23:37:30 -0700236 size_t mQueueOffset{0};
237 size_t mCompositionOffset{0};
238 size_t mRetireOffset{0};
239 size_t mReleaseOffset{0};
Brian Anderson4565daa2016-12-13 15:41:28 -0800240
241 bool mProducerWantsEvents{false};
Brian Andersond6927fb2016-07-23 23:37:30 -0700242};
243
Brian Anderson3890c392016-07-25 12:48:08 -0700244
245// A single frame update from the consumer to producer that can be sent
246// through Binder.
247// Although this may be sent multiple times for the same frame as new
248// timestamps are set, Fences only need to be sent once.
249class FrameEventsDelta : public Flattenable<FrameEventsDelta> {
250friend class ProducerFrameEventHistory;
251public:
252 FrameEventsDelta() = default;
253 FrameEventsDelta(size_t index,
254 const FrameEvents& frameTimestamps,
255 const FrameEventDirtyFields& dirtyFields);
256
Brian Anderson3d4039d2016-09-23 16:31:30 -0700257 // Movable.
258 FrameEventsDelta(FrameEventsDelta&& src) = default;
259 FrameEventsDelta& operator=(FrameEventsDelta&& src) = default;
260 // Not copyable.
261 FrameEventsDelta(const FrameEventsDelta& src) = delete;
262 FrameEventsDelta& operator=(const FrameEventsDelta& src) = delete;
263
Brian Anderson3890c392016-07-25 12:48:08 -0700264 // Flattenable implementation
265 size_t getFlattenedSize() const;
266 size_t getFdCount() const;
267 status_t flatten(void*& buffer, size_t& size, int*& fds,
268 size_t& count) const;
269 status_t unflatten(void const*& buffer, size_t& size, int const*& fds,
270 size_t& count);
271
272private:
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800273 static constexpr size_t minFlattenedSize();
Brian Anderson3890c392016-07-25 12:48:08 -0700274
275 size_t mIndex{0};
276 uint64_t mFrameNumber{0};
277
278 bool mAddPostCompositeCalled{0};
279 bool mAddRetireCalled{0};
280 bool mAddReleaseCalled{0};
281
Brian Andersoned816e62016-10-26 16:12:21 -0700282 nsecs_t mPostedTime{FrameEvents::TIMESTAMP_PENDING};
283 nsecs_t mRequestedPresentTime{FrameEvents::TIMESTAMP_PENDING};
284 nsecs_t mLatchTime{FrameEvents::TIMESTAMP_PENDING};
285 nsecs_t mFirstRefreshStartTime{FrameEvents::TIMESTAMP_PENDING};
286 nsecs_t mLastRefreshStartTime{FrameEvents::TIMESTAMP_PENDING};
287 nsecs_t mDequeueReadyTime{FrameEvents::TIMESTAMP_PENDING};
Brian Anderson3890c392016-07-25 12:48:08 -0700288
Brian Anderson3d4039d2016-09-23 16:31:30 -0700289 FenceTime::Snapshot mGpuCompositionDoneFence;
290 FenceTime::Snapshot mDisplayPresentFence;
291 FenceTime::Snapshot mDisplayRetireFence;
292 FenceTime::Snapshot mReleaseFence;
Brian Anderson3890c392016-07-25 12:48:08 -0700293
294 // This is a static method with an auto return value so we can call
295 // it without needing const and non-const versions.
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700296 template <typename ThisT>
297 static inline auto allFences(ThisT fed) ->
298 std::array<decltype(&fed->mReleaseFence), 4> {
Brian Anderson3890c392016-07-25 12:48:08 -0700299 return {{
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700300 &fed->mGpuCompositionDoneFence, &fed->mDisplayPresentFence,
301 &fed->mDisplayRetireFence, &fed->mReleaseFence
Brian Anderson3890c392016-07-25 12:48:08 -0700302 }};
303 }
304};
305
306
307// A collection of updates from consumer to producer that can be sent
308// through Binder.
309class FrameEventHistoryDelta
310 : public Flattenable<FrameEventHistoryDelta> {
311
312friend class ConsumerFrameEventHistory;
313friend class ProducerFrameEventHistory;
314
315public:
Brian Anderson3d4039d2016-09-23 16:31:30 -0700316 FrameEventHistoryDelta() = default;
317
318 // Movable.
319 FrameEventHistoryDelta(FrameEventHistoryDelta&& src) = default;
320 FrameEventHistoryDelta& operator=(FrameEventHistoryDelta&& src);
321 // Not copyable.
322 FrameEventHistoryDelta(const FrameEventHistoryDelta& src) = delete;
323 FrameEventHistoryDelta& operator=(
324 const FrameEventHistoryDelta& src) = delete;
325
Brian Anderson3890c392016-07-25 12:48:08 -0700326 // Flattenable implementation.
327 size_t getFlattenedSize() const;
328 size_t getFdCount() const;
329 status_t flatten(void*& buffer, size_t& size, int*& fds,
330 size_t& count) const;
331 status_t unflatten(void const*& buffer, size_t& size, int const*& fds,
332 size_t& count);
333
334private:
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800335 static constexpr size_t minFlattenedSize();
Brian Anderson3890c392016-07-25 12:48:08 -0700336
337 std::vector<FrameEventsDelta> mDeltas;
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800338 CompositorTiming mCompositorTiming;
Brian Anderson3890c392016-07-25 12:48:08 -0700339};
340
341
Pablo Ceballosce796e72016-02-04 19:10:51 -0800342} // namespace android
343#endif