blob: 46ca2c2ec023bc49f10675c69a96b552854f5692 [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,
44 GL_COMPOSITION_DONE,
45 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
98
Brian Anderson3890c392016-07-25 12:48:08 -070099// A short history of frames that are synchronized between the consumer and
100// producer via deltas.
101class FrameEventHistory {
102public:
103 virtual ~FrameEventHistory();
104
105 FrameEvents* getFrame(uint64_t frameNumber);
106 FrameEvents* getFrame(uint64_t frameNumber, size_t* iHint);
107 void checkFencesForCompletion();
108 void dump(String8& outString) const;
109
110 static constexpr size_t MAX_FRAME_HISTORY = 8;
111
112protected:
113 std::array<FrameEvents, MAX_FRAME_HISTORY> mFrames;
114};
115
116
117// The producer's interface to FrameEventHistory
118class ProducerFrameEventHistory : public FrameEventHistory {
119public:
120 ~ProducerFrameEventHistory() override;
121
Brian Anderson3da8d272016-07-28 16:20:47 -0700122 // virtual for testing.
123 virtual void updateAcquireFence(
Brian Anderson3d4039d2016-09-23 16:31:30 -0700124 uint64_t frameNumber, std::shared_ptr<FenceTime>&& acquire);
Brian Anderson3890c392016-07-25 12:48:08 -0700125 void applyDelta(const FrameEventHistoryDelta& delta);
126
Brian Anderson3d4039d2016-09-23 16:31:30 -0700127 void updateSignalTimes();
128
Brian Anderson3da8d272016-07-28 16:20:47 -0700129protected:
130 void applyFenceDelta(FenceTimeline* timeline,
131 std::shared_ptr<FenceTime>* dst,
132 const FenceTime::Snapshot& src) const;
133
134 // virtual for testing.
135 virtual std::shared_ptr<FenceTime> createFenceTime(
136 const sp<Fence>& fence) const;
137
Brian Anderson3890c392016-07-25 12:48:08 -0700138 size_t mAcquireOffset{0};
Brian Anderson3d4039d2016-09-23 16:31:30 -0700139
140 // The consumer updates it's timelines in Layer and SurfaceFlinger since
141 // they can coordinate shared timelines better. The producer doesn't have
142 // shared timelines though, so just let it own and update all of them.
143 FenceTimeline mAcquireTimeline;
144 FenceTimeline mGpuCompositionDoneTimeline;
145 FenceTimeline mPresentTimeline;
146 FenceTimeline mRetireTimeline;
147 FenceTimeline mReleaseTimeline;
Brian Anderson3890c392016-07-25 12:48:08 -0700148};
149
150
151// Used by the consumer to create a new frame event record that is
152// partially complete.
Brian Andersond6927fb2016-07-23 23:37:30 -0700153struct NewFrameEventsEntry {
154 uint64_t frameNumber{0};
155 nsecs_t postedTime{0};
156 nsecs_t requestedPresentTime{0};
Brian Anderson3d4039d2016-09-23 16:31:30 -0700157 std::shared_ptr<FenceTime> acquireFence{FenceTime::NO_FENCE};
Brian Andersond6927fb2016-07-23 23:37:30 -0700158};
159
160
Brian Anderson3890c392016-07-25 12:48:08 -0700161// Used by the consumer to keep track of which fields it already sent to
162// the producer.
163class FrameEventDirtyFields {
Brian Andersond6927fb2016-07-23 23:37:30 -0700164public:
Brian Anderson3890c392016-07-25 12:48:08 -0700165 inline void reset() { mBitset.reset(); }
166 inline bool anyDirty() const { return mBitset.any(); }
Brian Andersond6927fb2016-07-23 23:37:30 -0700167
Brian Anderson3890c392016-07-25 12:48:08 -0700168 template <FrameEvent event>
169 inline void setDirty() {
170 constexpr size_t eventIndex = static_cast<size_t>(event);
171 static_assert(eventIndex < FrameEvents::EVENT_COUNT, "Bad index.");
172 mBitset.set(eventIndex);
173 }
174
175 template <FrameEvent event>
176 inline bool isDirty() const {
177 constexpr size_t eventIndex = static_cast<size_t>(event);
178 static_assert(eventIndex < FrameEvents::EVENT_COUNT, "Bad index.");
179 return mBitset[eventIndex];
180 }
181
182private:
183 std::bitset<FrameEvents::EVENT_COUNT> mBitset;
184};
185
186
187// The consumer's interface to FrameEventHistory
188class ConsumerFrameEventHistory : public FrameEventHistory {
189public:
190 ~ConsumerFrameEventHistory() override;
191
192 void addQueue(const NewFrameEventsEntry& newEntry);
Brian Andersond6927fb2016-07-23 23:37:30 -0700193 void addLatch(uint64_t frameNumber, nsecs_t latchTime);
194 void addPreComposition(uint64_t frameNumber, nsecs_t refreshStartTime);
195 void addPostComposition(uint64_t frameNumber,
Brian Anderson3d4039d2016-09-23 16:31:30 -0700196 const std::shared_ptr<FenceTime>& gpuCompositionDone,
197 const std::shared_ptr<FenceTime>& displayPresent);
198 void addRetire(uint64_t frameNumber,
199 const std::shared_ptr<FenceTime>& displayRetire);
Brian Andersonf6386862016-10-31 16:34:13 -0700200 void addRelease(uint64_t frameNumber, nsecs_t dequeueReadyTime,
Brian Anderson3d4039d2016-09-23 16:31:30 -0700201 std::shared_ptr<FenceTime>&& release);
Brian Andersond6927fb2016-07-23 23:37:30 -0700202
Brian Anderson3890c392016-07-25 12:48:08 -0700203 void getAndResetDelta(FrameEventHistoryDelta* delta);
204
Brian Andersond6927fb2016-07-23 23:37:30 -0700205private:
Brian Anderson3d4039d2016-09-23 16:31:30 -0700206 void getFrameDelta(FrameEventHistoryDelta* delta,
207 const std::array<FrameEvents, MAX_FRAME_HISTORY>::iterator& frame);
208
Brian Anderson3890c392016-07-25 12:48:08 -0700209 std::array<FrameEventDirtyFields, MAX_FRAME_HISTORY> mFramesDirty;
Brian Andersond6927fb2016-07-23 23:37:30 -0700210 size_t mQueueOffset{0};
211 size_t mCompositionOffset{0};
212 size_t mRetireOffset{0};
213 size_t mReleaseOffset{0};
Brian Anderson4565daa2016-12-13 15:41:28 -0800214
215 bool mProducerWantsEvents{false};
Brian Andersond6927fb2016-07-23 23:37:30 -0700216};
217
Brian Anderson3890c392016-07-25 12:48:08 -0700218
219// A single frame update from the consumer to producer that can be sent
220// through Binder.
221// Although this may be sent multiple times for the same frame as new
222// timestamps are set, Fences only need to be sent once.
223class FrameEventsDelta : public Flattenable<FrameEventsDelta> {
224friend class ProducerFrameEventHistory;
225public:
226 FrameEventsDelta() = default;
227 FrameEventsDelta(size_t index,
228 const FrameEvents& frameTimestamps,
229 const FrameEventDirtyFields& dirtyFields);
230
Brian Anderson3d4039d2016-09-23 16:31:30 -0700231 // Movable.
232 FrameEventsDelta(FrameEventsDelta&& src) = default;
233 FrameEventsDelta& operator=(FrameEventsDelta&& src) = default;
234 // Not copyable.
235 FrameEventsDelta(const FrameEventsDelta& src) = delete;
236 FrameEventsDelta& operator=(const FrameEventsDelta& src) = delete;
237
Brian Anderson3890c392016-07-25 12:48:08 -0700238 // Flattenable implementation
239 size_t getFlattenedSize() const;
240 size_t getFdCount() const;
241 status_t flatten(void*& buffer, size_t& size, int*& fds,
242 size_t& count) const;
243 status_t unflatten(void const*& buffer, size_t& size, int const*& fds,
244 size_t& count);
245
246private:
247 static size_t minFlattenedSize();
248
249 size_t mIndex{0};
250 uint64_t mFrameNumber{0};
251
252 bool mAddPostCompositeCalled{0};
253 bool mAddRetireCalled{0};
254 bool mAddReleaseCalled{0};
255
256 nsecs_t mPostedTime{0};
257 nsecs_t mRequestedPresentTime{0};
258 nsecs_t mLatchTime{0};
259 nsecs_t mFirstRefreshStartTime{0};
260 nsecs_t mLastRefreshStartTime{0};
Brian Andersonf6386862016-10-31 16:34:13 -0700261 nsecs_t mDequeueReadyTime{0};
Brian Anderson3890c392016-07-25 12:48:08 -0700262
Brian Anderson3d4039d2016-09-23 16:31:30 -0700263 FenceTime::Snapshot mGpuCompositionDoneFence;
264 FenceTime::Snapshot mDisplayPresentFence;
265 FenceTime::Snapshot mDisplayRetireFence;
266 FenceTime::Snapshot mReleaseFence;
Brian Anderson3890c392016-07-25 12:48:08 -0700267
268 // This is a static method with an auto return value so we can call
269 // it without needing const and non-const versions.
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700270 template <typename ThisT>
271 static inline auto allFences(ThisT fed) ->
272 std::array<decltype(&fed->mReleaseFence), 4> {
Brian Anderson3890c392016-07-25 12:48:08 -0700273 return {{
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700274 &fed->mGpuCompositionDoneFence, &fed->mDisplayPresentFence,
275 &fed->mDisplayRetireFence, &fed->mReleaseFence
Brian Anderson3890c392016-07-25 12:48:08 -0700276 }};
277 }
278};
279
280
281// A collection of updates from consumer to producer that can be sent
282// through Binder.
283class FrameEventHistoryDelta
284 : public Flattenable<FrameEventHistoryDelta> {
285
286friend class ConsumerFrameEventHistory;
287friend class ProducerFrameEventHistory;
288
289public:
Brian Anderson3d4039d2016-09-23 16:31:30 -0700290 FrameEventHistoryDelta() = default;
291
292 // Movable.
293 FrameEventHistoryDelta(FrameEventHistoryDelta&& src) = default;
294 FrameEventHistoryDelta& operator=(FrameEventHistoryDelta&& src);
295 // Not copyable.
296 FrameEventHistoryDelta(const FrameEventHistoryDelta& src) = delete;
297 FrameEventHistoryDelta& operator=(
298 const FrameEventHistoryDelta& src) = delete;
299
Brian Anderson3890c392016-07-25 12:48:08 -0700300 // Flattenable implementation.
301 size_t getFlattenedSize() const;
302 size_t getFdCount() const;
303 status_t flatten(void*& buffer, size_t& size, int*& fds,
304 size_t& count) const;
305 status_t unflatten(void const*& buffer, size_t& size, int const*& fds,
306 size_t& count);
307
308private:
309 static size_t minFlattenedSize();
310
311 std::vector<FrameEventsDelta> mDeltas;
312};
313
314
Pablo Ceballosce796e72016-02-04 19:10:51 -0800315} // namespace android
316#endif