blob: 92251edca621c8f7cf18f5db13de764e2bb6c546 [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.");
57 static constexpr nsecs_t TIMESTAMP_PENDING =
58 std::numeric_limits<nsecs_t>::max();
59
60 static inline bool isValidTimestamp(nsecs_t time) {
61 return time != TIMESTAMP_PENDING;
62 }
63
Brian Anderson3890c392016-07-25 12:48:08 -070064 bool hasPostedInfo() const;
65 bool hasRequestedPresentInfo() const;
66 bool hasLatchInfo() const;
67 bool hasFirstRefreshStartInfo() const;
68 bool hasLastRefreshStartInfo() const;
69 bool hasAcquireInfo() const;
70 bool hasGpuCompositionDoneInfo() const;
71 bool hasDisplayPresentInfo() const;
Brian Anderson3890c392016-07-25 12:48:08 -070072 bool hasReleaseInfo() const;
Brian Andersonf6386862016-10-31 16:34:13 -070073 bool hasDequeueReadyInfo() const;
Brian Anderson3890c392016-07-25 12:48:08 -070074
Brian Andersond6927fb2016-07-23 23:37:30 -070075 void checkFencesForCompletion();
76 void dump(String8& outString) const;
77
78 bool valid{false};
Brian Anderson5ea5e592016-12-01 16:54:33 -080079 int connectId{0};
Brian Andersond6927fb2016-07-23 23:37:30 -070080 uint64_t frameNumber{0};
81
82 // Whether or not certain points in the frame's life cycle have been
83 // encountered help us determine if timestamps aren't available because
84 // a) we'll just never get them or b) they're not ready yet.
85 bool addPostCompositeCalled{false};
Brian Anderson3890c392016-07-25 12:48:08 -070086 bool addReleaseCalled{false};
Brian Andersond6927fb2016-07-23 23:37:30 -070087
Brian Andersoned816e62016-10-26 16:12:21 -070088 nsecs_t postedTime{TIMESTAMP_PENDING};
89 nsecs_t requestedPresentTime{TIMESTAMP_PENDING};
90 nsecs_t latchTime{TIMESTAMP_PENDING};
91 nsecs_t firstRefreshStartTime{TIMESTAMP_PENDING};
92 nsecs_t lastRefreshStartTime{TIMESTAMP_PENDING};
93 nsecs_t dequeueReadyTime{TIMESTAMP_PENDING};
Brian Andersond6927fb2016-07-23 23:37:30 -070094
Brian Anderson3d4039d2016-09-23 16:31:30 -070095 std::shared_ptr<FenceTime> acquireFence{FenceTime::NO_FENCE};
96 std::shared_ptr<FenceTime> gpuCompositionDoneFence{FenceTime::NO_FENCE};
97 std::shared_ptr<FenceTime> displayPresentFence{FenceTime::NO_FENCE};
Brian Anderson3d4039d2016-09-23 16:31:30 -070098 std::shared_ptr<FenceTime> releaseFence{FenceTime::NO_FENCE};
Brian Andersond6927fb2016-07-23 23:37:30 -070099};
100
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800101struct CompositorTiming {
102 nsecs_t deadline{0};
103 nsecs_t interval{16666667};
Brian Andersond0010582017-03-07 13:20:31 -0800104 nsecs_t presentLatency{16666667};
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800105};
Brian Andersond6927fb2016-07-23 23:37:30 -0700106
Brian Anderson3890c392016-07-25 12:48:08 -0700107// A short history of frames that are synchronized between the consumer and
108// producer via deltas.
109class FrameEventHistory {
110public:
111 virtual ~FrameEventHistory();
112
113 FrameEvents* getFrame(uint64_t frameNumber);
114 FrameEvents* getFrame(uint64_t frameNumber, size_t* iHint);
115 void checkFencesForCompletion();
116 void dump(String8& outString) const;
117
118 static constexpr size_t MAX_FRAME_HISTORY = 8;
119
120protected:
121 std::array<FrameEvents, MAX_FRAME_HISTORY> mFrames;
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800122
123 CompositorTiming mCompositorTiming;
Brian Anderson3890c392016-07-25 12:48:08 -0700124};
125
126
127// The producer's interface to FrameEventHistory
128class ProducerFrameEventHistory : public FrameEventHistory {
129public:
130 ~ProducerFrameEventHistory() override;
131
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800132 // Public for testing.
133 static nsecs_t snapToNextTick(
134 nsecs_t timestamp, nsecs_t tickPhase, nsecs_t tickInterval);
135
136 nsecs_t getNextCompositeDeadline(const nsecs_t now) const;
137 nsecs_t getCompositeInterval() const { return mCompositorTiming.interval; }
138 nsecs_t getCompositeToPresentLatency() const {
139 return mCompositorTiming.presentLatency;
140 }
141
Brian Anderson3da8d272016-07-28 16:20:47 -0700142 // virtual for testing.
143 virtual void updateAcquireFence(
Brian Anderson3d4039d2016-09-23 16:31:30 -0700144 uint64_t frameNumber, std::shared_ptr<FenceTime>&& acquire);
Brian Anderson3890c392016-07-25 12:48:08 -0700145 void applyDelta(const FrameEventHistoryDelta& delta);
146
Brian Anderson3d4039d2016-09-23 16:31:30 -0700147 void updateSignalTimes();
148
Brian Anderson3da8d272016-07-28 16:20:47 -0700149protected:
150 void applyFenceDelta(FenceTimeline* timeline,
151 std::shared_ptr<FenceTime>* dst,
152 const FenceTime::Snapshot& src) const;
153
154 // virtual for testing.
155 virtual std::shared_ptr<FenceTime> createFenceTime(
156 const sp<Fence>& fence) const;
157
Brian Anderson3890c392016-07-25 12:48:08 -0700158 size_t mAcquireOffset{0};
Brian Anderson3d4039d2016-09-23 16:31:30 -0700159
160 // The consumer updates it's timelines in Layer and SurfaceFlinger since
161 // they can coordinate shared timelines better. The producer doesn't have
162 // shared timelines though, so just let it own and update all of them.
163 FenceTimeline mAcquireTimeline;
164 FenceTimeline mGpuCompositionDoneTimeline;
165 FenceTimeline mPresentTimeline;
Brian Anderson3d4039d2016-09-23 16:31:30 -0700166 FenceTimeline mReleaseTimeline;
Brian Anderson3890c392016-07-25 12:48:08 -0700167};
168
169
170// Used by the consumer to create a new frame event record that is
171// partially complete.
Brian Andersond6927fb2016-07-23 23:37:30 -0700172struct NewFrameEventsEntry {
173 uint64_t frameNumber{0};
174 nsecs_t postedTime{0};
175 nsecs_t requestedPresentTime{0};
Brian Anderson3d4039d2016-09-23 16:31:30 -0700176 std::shared_ptr<FenceTime> acquireFence{FenceTime::NO_FENCE};
Brian Andersond6927fb2016-07-23 23:37:30 -0700177};
178
179
Brian Anderson3890c392016-07-25 12:48:08 -0700180// Used by the consumer to keep track of which fields it already sent to
181// the producer.
182class FrameEventDirtyFields {
Brian Andersond6927fb2016-07-23 23:37:30 -0700183public:
Brian Anderson3890c392016-07-25 12:48:08 -0700184 inline void reset() { mBitset.reset(); }
185 inline bool anyDirty() const { return mBitset.any(); }
Brian Andersond6927fb2016-07-23 23:37:30 -0700186
Brian Anderson3890c392016-07-25 12:48:08 -0700187 template <FrameEvent event>
188 inline void setDirty() {
189 constexpr size_t eventIndex = static_cast<size_t>(event);
190 static_assert(eventIndex < FrameEvents::EVENT_COUNT, "Bad index.");
191 mBitset.set(eventIndex);
192 }
193
194 template <FrameEvent event>
195 inline bool isDirty() const {
196 constexpr size_t eventIndex = static_cast<size_t>(event);
197 static_assert(eventIndex < FrameEvents::EVENT_COUNT, "Bad index.");
198 return mBitset[eventIndex];
199 }
200
201private:
202 std::bitset<FrameEvents::EVENT_COUNT> mBitset;
203};
204
205
206// The consumer's interface to FrameEventHistory
207class ConsumerFrameEventHistory : public FrameEventHistory {
208public:
209 ~ConsumerFrameEventHistory() override;
210
Brian Anderson5ea5e592016-12-01 16:54:33 -0800211 void onDisconnect();
212
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800213 void initializeCompositorTiming(const CompositorTiming& compositorTiming);
214
Brian Anderson3890c392016-07-25 12:48:08 -0700215 void addQueue(const NewFrameEventsEntry& newEntry);
Brian Andersond6927fb2016-07-23 23:37:30 -0700216 void addLatch(uint64_t frameNumber, nsecs_t latchTime);
217 void addPreComposition(uint64_t frameNumber, nsecs_t refreshStartTime);
218 void addPostComposition(uint64_t frameNumber,
Brian Anderson3d4039d2016-09-23 16:31:30 -0700219 const std::shared_ptr<FenceTime>& gpuCompositionDone,
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800220 const std::shared_ptr<FenceTime>& displayPresent,
221 const CompositorTiming& compositorTiming);
Brian Andersonf6386862016-10-31 16:34:13 -0700222 void addRelease(uint64_t frameNumber, nsecs_t dequeueReadyTime,
Brian Anderson3d4039d2016-09-23 16:31:30 -0700223 std::shared_ptr<FenceTime>&& release);
Brian Andersond6927fb2016-07-23 23:37:30 -0700224
Brian Anderson3890c392016-07-25 12:48:08 -0700225 void getAndResetDelta(FrameEventHistoryDelta* delta);
226
Brian Andersond6927fb2016-07-23 23:37:30 -0700227private:
Brian Anderson3d4039d2016-09-23 16:31:30 -0700228 void getFrameDelta(FrameEventHistoryDelta* delta,
229 const std::array<FrameEvents, MAX_FRAME_HISTORY>::iterator& frame);
230
Brian Anderson3890c392016-07-25 12:48:08 -0700231 std::array<FrameEventDirtyFields, MAX_FRAME_HISTORY> mFramesDirty;
Brian Anderson5ea5e592016-12-01 16:54:33 -0800232
Brian Andersond6927fb2016-07-23 23:37:30 -0700233 size_t mQueueOffset{0};
234 size_t mCompositionOffset{0};
Brian Andersond6927fb2016-07-23 23:37:30 -0700235 size_t mReleaseOffset{0};
Brian Anderson4565daa2016-12-13 15:41:28 -0800236
Brian Anderson5ea5e592016-12-01 16:54:33 -0800237 int mCurrentConnectId{0};
Brian Anderson4565daa2016-12-13 15:41:28 -0800238 bool mProducerWantsEvents{false};
Brian Andersond6927fb2016-07-23 23:37:30 -0700239};
240
Brian Anderson3890c392016-07-25 12:48:08 -0700241
242// A single frame update from the consumer to producer that can be sent
243// through Binder.
244// Although this may be sent multiple times for the same frame as new
245// timestamps are set, Fences only need to be sent once.
246class FrameEventsDelta : public Flattenable<FrameEventsDelta> {
247friend class ProducerFrameEventHistory;
248public:
249 FrameEventsDelta() = default;
250 FrameEventsDelta(size_t index,
251 const FrameEvents& frameTimestamps,
252 const FrameEventDirtyFields& dirtyFields);
253
Brian Anderson3d4039d2016-09-23 16:31:30 -0700254 // Movable.
255 FrameEventsDelta(FrameEventsDelta&& src) = default;
256 FrameEventsDelta& operator=(FrameEventsDelta&& src) = default;
257 // Not copyable.
258 FrameEventsDelta(const FrameEventsDelta& src) = delete;
259 FrameEventsDelta& operator=(const FrameEventsDelta& src) = delete;
260
Brian Anderson3890c392016-07-25 12:48:08 -0700261 // Flattenable implementation
262 size_t getFlattenedSize() const;
263 size_t getFdCount() const;
264 status_t flatten(void*& buffer, size_t& size, int*& fds,
265 size_t& count) const;
266 status_t unflatten(void const*& buffer, size_t& size, int const*& fds,
267 size_t& count);
268
269private:
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800270 static constexpr size_t minFlattenedSize();
Brian Anderson3890c392016-07-25 12:48:08 -0700271
272 size_t mIndex{0};
273 uint64_t mFrameNumber{0};
274
275 bool mAddPostCompositeCalled{0};
Brian Anderson3890c392016-07-25 12:48:08 -0700276 bool mAddReleaseCalled{0};
277
Brian Andersoned816e62016-10-26 16:12:21 -0700278 nsecs_t mPostedTime{FrameEvents::TIMESTAMP_PENDING};
279 nsecs_t mRequestedPresentTime{FrameEvents::TIMESTAMP_PENDING};
280 nsecs_t mLatchTime{FrameEvents::TIMESTAMP_PENDING};
281 nsecs_t mFirstRefreshStartTime{FrameEvents::TIMESTAMP_PENDING};
282 nsecs_t mLastRefreshStartTime{FrameEvents::TIMESTAMP_PENDING};
283 nsecs_t mDequeueReadyTime{FrameEvents::TIMESTAMP_PENDING};
Brian Anderson3890c392016-07-25 12:48:08 -0700284
Brian Anderson3d4039d2016-09-23 16:31:30 -0700285 FenceTime::Snapshot mGpuCompositionDoneFence;
286 FenceTime::Snapshot mDisplayPresentFence;
Brian Anderson3d4039d2016-09-23 16:31:30 -0700287 FenceTime::Snapshot mReleaseFence;
Brian Anderson3890c392016-07-25 12:48:08 -0700288
289 // This is a static method with an auto return value so we can call
290 // it without needing const and non-const versions.
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700291 template <typename ThisT>
292 static inline auto allFences(ThisT fed) ->
Brian Anderson4e606e32017-03-16 15:34:57 -0700293 std::array<decltype(&fed->mReleaseFence), 3> {
Brian Anderson3890c392016-07-25 12:48:08 -0700294 return {{
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700295 &fed->mGpuCompositionDoneFence, &fed->mDisplayPresentFence,
Brian Anderson4e606e32017-03-16 15:34:57 -0700296 &fed->mReleaseFence
Brian Anderson3890c392016-07-25 12:48:08 -0700297 }};
298 }
299};
300
301
302// A collection of updates from consumer to producer that can be sent
303// through Binder.
304class FrameEventHistoryDelta
305 : public Flattenable<FrameEventHistoryDelta> {
306
307friend class ConsumerFrameEventHistory;
308friend class ProducerFrameEventHistory;
309
310public:
Brian Anderson3d4039d2016-09-23 16:31:30 -0700311 FrameEventHistoryDelta() = default;
312
313 // Movable.
314 FrameEventHistoryDelta(FrameEventHistoryDelta&& src) = default;
315 FrameEventHistoryDelta& operator=(FrameEventHistoryDelta&& src);
316 // Not copyable.
317 FrameEventHistoryDelta(const FrameEventHistoryDelta& src) = delete;
318 FrameEventHistoryDelta& operator=(
319 const FrameEventHistoryDelta& src) = delete;
320
Brian Anderson3890c392016-07-25 12:48:08 -0700321 // Flattenable implementation.
322 size_t getFlattenedSize() const;
323 size_t getFdCount() const;
324 status_t flatten(void*& buffer, size_t& size, int*& fds,
325 size_t& count) const;
326 status_t unflatten(void const*& buffer, size_t& size, int const*& fds,
327 size_t& count);
328
329private:
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800330 static constexpr size_t minFlattenedSize();
Brian Anderson3890c392016-07-25 12:48:08 -0700331
332 std::vector<FrameEventsDelta> mDeltas;
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800333 CompositorTiming mCompositorTiming;
Brian Anderson3890c392016-07-25 12:48:08 -0700334};
335
336
Pablo Ceballosce796e72016-02-04 19:10:51 -0800337} // namespace android
338#endif