blob: 8a3fa399565534bede5841d14ef1d0a3f41acc63 [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,
47 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 Anderson3890c392016-07-25 12:48:08 -070054 bool hasPostedInfo() const;
55 bool hasRequestedPresentInfo() const;
56 bool hasLatchInfo() const;
57 bool hasFirstRefreshStartInfo() const;
58 bool hasLastRefreshStartInfo() const;
59 bool hasAcquireInfo() const;
60 bool hasGpuCompositionDoneInfo() const;
61 bool hasDisplayPresentInfo() const;
62 bool hasDisplayRetireInfo() const;
63 bool hasReleaseInfo() const;
64
Brian Andersond6927fb2016-07-23 23:37:30 -070065 void checkFencesForCompletion();
66 void dump(String8& outString) const;
67
Brian Anderson3890c392016-07-25 12:48:08 -070068 static constexpr size_t EVENT_COUNT =
69 static_cast<size_t>(FrameEvent::EVENT_COUNT);
70 static_assert(EVENT_COUNT <= 32, "Event count sanity check failed.");
71
Brian Andersond6927fb2016-07-23 23:37:30 -070072 bool valid{false};
73 uint64_t frameNumber{0};
74
75 // Whether or not certain points in the frame's life cycle have been
76 // encountered help us determine if timestamps aren't available because
77 // a) we'll just never get them or b) they're not ready yet.
78 bool addPostCompositeCalled{false};
79 bool addRetireCalled{false};
Brian Anderson3890c392016-07-25 12:48:08 -070080 bool addReleaseCalled{false};
Brian Andersond6927fb2016-07-23 23:37:30 -070081
Brian Anderson3d4039d2016-09-23 16:31:30 -070082 nsecs_t postedTime{-1};
83 nsecs_t requestedPresentTime{-1};
84 nsecs_t latchTime{-1};
85 nsecs_t firstRefreshStartTime{-1};
86 nsecs_t lastRefreshStartTime{-1};
Brian Andersond6927fb2016-07-23 23:37:30 -070087
Brian Anderson3d4039d2016-09-23 16:31:30 -070088 std::shared_ptr<FenceTime> acquireFence{FenceTime::NO_FENCE};
89 std::shared_ptr<FenceTime> gpuCompositionDoneFence{FenceTime::NO_FENCE};
90 std::shared_ptr<FenceTime> displayPresentFence{FenceTime::NO_FENCE};
91 std::shared_ptr<FenceTime> displayRetireFence{FenceTime::NO_FENCE};
92 std::shared_ptr<FenceTime> releaseFence{FenceTime::NO_FENCE};
Brian Andersond6927fb2016-07-23 23:37:30 -070093};
94
95
Brian Anderson3890c392016-07-25 12:48:08 -070096// A short history of frames that are synchronized between the consumer and
97// producer via deltas.
98class FrameEventHistory {
99public:
100 virtual ~FrameEventHistory();
101
102 FrameEvents* getFrame(uint64_t frameNumber);
103 FrameEvents* getFrame(uint64_t frameNumber, size_t* iHint);
104 void checkFencesForCompletion();
105 void dump(String8& outString) const;
106
107 static constexpr size_t MAX_FRAME_HISTORY = 8;
108
109protected:
110 std::array<FrameEvents, MAX_FRAME_HISTORY> mFrames;
111};
112
113
114// The producer's interface to FrameEventHistory
115class ProducerFrameEventHistory : public FrameEventHistory {
116public:
117 ~ProducerFrameEventHistory() override;
118
Brian Anderson3d4039d2016-09-23 16:31:30 -0700119 void updateAcquireFence(
120 uint64_t frameNumber, std::shared_ptr<FenceTime>&& acquire);
Brian Anderson3890c392016-07-25 12:48:08 -0700121 void applyDelta(const FrameEventHistoryDelta& delta);
122
Brian Anderson3d4039d2016-09-23 16:31:30 -0700123 void updateSignalTimes();
124
Brian Anderson3890c392016-07-25 12:48:08 -0700125private:
126 size_t mAcquireOffset{0};
Brian Anderson3d4039d2016-09-23 16:31:30 -0700127
128 // The consumer updates it's timelines in Layer and SurfaceFlinger since
129 // they can coordinate shared timelines better. The producer doesn't have
130 // shared timelines though, so just let it own and update all of them.
131 FenceTimeline mAcquireTimeline;
132 FenceTimeline mGpuCompositionDoneTimeline;
133 FenceTimeline mPresentTimeline;
134 FenceTimeline mRetireTimeline;
135 FenceTimeline mReleaseTimeline;
Brian Anderson3890c392016-07-25 12:48:08 -0700136};
137
138
139// Used by the consumer to create a new frame event record that is
140// partially complete.
Brian Andersond6927fb2016-07-23 23:37:30 -0700141struct NewFrameEventsEntry {
142 uint64_t frameNumber{0};
143 nsecs_t postedTime{0};
144 nsecs_t requestedPresentTime{0};
Brian Anderson3d4039d2016-09-23 16:31:30 -0700145 std::shared_ptr<FenceTime> acquireFence{FenceTime::NO_FENCE};
Brian Andersond6927fb2016-07-23 23:37:30 -0700146};
147
148
Brian Anderson3890c392016-07-25 12:48:08 -0700149// Used by the consumer to keep track of which fields it already sent to
150// the producer.
151class FrameEventDirtyFields {
Brian Andersond6927fb2016-07-23 23:37:30 -0700152public:
Brian Anderson3890c392016-07-25 12:48:08 -0700153 inline void reset() { mBitset.reset(); }
154 inline bool anyDirty() const { return mBitset.any(); }
Brian Andersond6927fb2016-07-23 23:37:30 -0700155
Brian Anderson3890c392016-07-25 12:48:08 -0700156 template <FrameEvent event>
157 inline void setDirty() {
158 constexpr size_t eventIndex = static_cast<size_t>(event);
159 static_assert(eventIndex < FrameEvents::EVENT_COUNT, "Bad index.");
160 mBitset.set(eventIndex);
161 }
162
163 template <FrameEvent event>
164 inline bool isDirty() const {
165 constexpr size_t eventIndex = static_cast<size_t>(event);
166 static_assert(eventIndex < FrameEvents::EVENT_COUNT, "Bad index.");
167 return mBitset[eventIndex];
168 }
169
170private:
171 std::bitset<FrameEvents::EVENT_COUNT> mBitset;
172};
173
174
175// The consumer's interface to FrameEventHistory
176class ConsumerFrameEventHistory : public FrameEventHistory {
177public:
178 ~ConsumerFrameEventHistory() override;
179
180 void addQueue(const NewFrameEventsEntry& newEntry);
Brian Andersond6927fb2016-07-23 23:37:30 -0700181 void addLatch(uint64_t frameNumber, nsecs_t latchTime);
182 void addPreComposition(uint64_t frameNumber, nsecs_t refreshStartTime);
183 void addPostComposition(uint64_t frameNumber,
Brian Anderson3d4039d2016-09-23 16:31:30 -0700184 const std::shared_ptr<FenceTime>& gpuCompositionDone,
185 const std::shared_ptr<FenceTime>& displayPresent);
186 void addRetire(uint64_t frameNumber,
187 const std::shared_ptr<FenceTime>& displayRetire);
188 void addRelease(uint64_t frameNumber,
189 std::shared_ptr<FenceTime>&& release);
Brian Andersond6927fb2016-07-23 23:37:30 -0700190
Brian Anderson3890c392016-07-25 12:48:08 -0700191 void getAndResetDelta(FrameEventHistoryDelta* delta);
192
Brian Andersond6927fb2016-07-23 23:37:30 -0700193private:
Brian Anderson3d4039d2016-09-23 16:31:30 -0700194 void getFrameDelta(FrameEventHistoryDelta* delta,
195 const std::array<FrameEvents, MAX_FRAME_HISTORY>::iterator& frame);
196
Brian Anderson3890c392016-07-25 12:48:08 -0700197 std::array<FrameEventDirtyFields, MAX_FRAME_HISTORY> mFramesDirty;
Brian Andersond6927fb2016-07-23 23:37:30 -0700198 size_t mQueueOffset{0};
199 size_t mCompositionOffset{0};
200 size_t mRetireOffset{0};
201 size_t mReleaseOffset{0};
Brian Anderson4565daa2016-12-13 15:41:28 -0800202
203 bool mProducerWantsEvents{false};
Brian Andersond6927fb2016-07-23 23:37:30 -0700204};
205
Brian Anderson3890c392016-07-25 12:48:08 -0700206
207// A single frame update from the consumer to producer that can be sent
208// through Binder.
209// Although this may be sent multiple times for the same frame as new
210// timestamps are set, Fences only need to be sent once.
211class FrameEventsDelta : public Flattenable<FrameEventsDelta> {
212friend class ProducerFrameEventHistory;
213public:
214 FrameEventsDelta() = default;
215 FrameEventsDelta(size_t index,
216 const FrameEvents& frameTimestamps,
217 const FrameEventDirtyFields& dirtyFields);
218
Brian Anderson3d4039d2016-09-23 16:31:30 -0700219 // Movable.
220 FrameEventsDelta(FrameEventsDelta&& src) = default;
221 FrameEventsDelta& operator=(FrameEventsDelta&& src) = default;
222 // Not copyable.
223 FrameEventsDelta(const FrameEventsDelta& src) = delete;
224 FrameEventsDelta& operator=(const FrameEventsDelta& src) = delete;
225
Brian Anderson3890c392016-07-25 12:48:08 -0700226 // Flattenable implementation
227 size_t getFlattenedSize() const;
228 size_t getFdCount() const;
229 status_t flatten(void*& buffer, size_t& size, int*& fds,
230 size_t& count) const;
231 status_t unflatten(void const*& buffer, size_t& size, int const*& fds,
232 size_t& count);
233
234private:
235 static size_t minFlattenedSize();
236
237 size_t mIndex{0};
238 uint64_t mFrameNumber{0};
239
240 bool mAddPostCompositeCalled{0};
241 bool mAddRetireCalled{0};
242 bool mAddReleaseCalled{0};
243
244 nsecs_t mPostedTime{0};
245 nsecs_t mRequestedPresentTime{0};
246 nsecs_t mLatchTime{0};
247 nsecs_t mFirstRefreshStartTime{0};
248 nsecs_t mLastRefreshStartTime{0};
249
Brian Anderson3d4039d2016-09-23 16:31:30 -0700250 FenceTime::Snapshot mGpuCompositionDoneFence;
251 FenceTime::Snapshot mDisplayPresentFence;
252 FenceTime::Snapshot mDisplayRetireFence;
253 FenceTime::Snapshot mReleaseFence;
Brian Anderson3890c392016-07-25 12:48:08 -0700254
255 // This is a static method with an auto return value so we can call
256 // it without needing const and non-const versions.
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700257 template <typename ThisT>
258 static inline auto allFences(ThisT fed) ->
259 std::array<decltype(&fed->mReleaseFence), 4> {
Brian Anderson3890c392016-07-25 12:48:08 -0700260 return {{
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700261 &fed->mGpuCompositionDoneFence, &fed->mDisplayPresentFence,
262 &fed->mDisplayRetireFence, &fed->mReleaseFence
Brian Anderson3890c392016-07-25 12:48:08 -0700263 }};
264 }
265};
266
267
268// A collection of updates from consumer to producer that can be sent
269// through Binder.
270class FrameEventHistoryDelta
271 : public Flattenable<FrameEventHistoryDelta> {
272
273friend class ConsumerFrameEventHistory;
274friend class ProducerFrameEventHistory;
275
276public:
Brian Anderson3d4039d2016-09-23 16:31:30 -0700277 FrameEventHistoryDelta() = default;
278
279 // Movable.
280 FrameEventHistoryDelta(FrameEventHistoryDelta&& src) = default;
281 FrameEventHistoryDelta& operator=(FrameEventHistoryDelta&& src);
282 // Not copyable.
283 FrameEventHistoryDelta(const FrameEventHistoryDelta& src) = delete;
284 FrameEventHistoryDelta& operator=(
285 const FrameEventHistoryDelta& src) = delete;
286
Brian Anderson3890c392016-07-25 12:48:08 -0700287 // Flattenable implementation.
288 size_t getFlattenedSize() const;
289 size_t getFdCount() const;
290 status_t flatten(void*& buffer, size_t& size, int*& fds,
291 size_t& count) const;
292 status_t unflatten(void const*& buffer, size_t& size, int const*& fds,
293 size_t& count);
294
295private:
296 static size_t minFlattenedSize();
297
298 std::vector<FrameEventsDelta> mDeltas;
299};
300
301
Pablo Ceballosce796e72016-02-04 19:10:51 -0800302} // namespace android
303#endif