blob: 7d627061c01bacb2bd2e5b786e15240b9355be04 [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 Anderson3da8d272016-07-28 16:20:47 -0700119 // virtual for testing.
120 virtual void updateAcquireFence(
Brian Anderson3d4039d2016-09-23 16:31:30 -0700121 uint64_t frameNumber, std::shared_ptr<FenceTime>&& acquire);
Brian Anderson3890c392016-07-25 12:48:08 -0700122 void applyDelta(const FrameEventHistoryDelta& delta);
123
Brian Anderson3d4039d2016-09-23 16:31:30 -0700124 void updateSignalTimes();
125
Brian Anderson3da8d272016-07-28 16:20:47 -0700126protected:
127 void applyFenceDelta(FenceTimeline* timeline,
128 std::shared_ptr<FenceTime>* dst,
129 const FenceTime::Snapshot& src) const;
130
131 // virtual for testing.
132 virtual std::shared_ptr<FenceTime> createFenceTime(
133 const sp<Fence>& fence) const;
134
Brian Anderson3890c392016-07-25 12:48:08 -0700135 size_t mAcquireOffset{0};
Brian Anderson3d4039d2016-09-23 16:31:30 -0700136
137 // The consumer updates it's timelines in Layer and SurfaceFlinger since
138 // they can coordinate shared timelines better. The producer doesn't have
139 // shared timelines though, so just let it own and update all of them.
140 FenceTimeline mAcquireTimeline;
141 FenceTimeline mGpuCompositionDoneTimeline;
142 FenceTimeline mPresentTimeline;
143 FenceTimeline mRetireTimeline;
144 FenceTimeline mReleaseTimeline;
Brian Anderson3890c392016-07-25 12:48:08 -0700145};
146
147
148// Used by the consumer to create a new frame event record that is
149// partially complete.
Brian Andersond6927fb2016-07-23 23:37:30 -0700150struct NewFrameEventsEntry {
151 uint64_t frameNumber{0};
152 nsecs_t postedTime{0};
153 nsecs_t requestedPresentTime{0};
Brian Anderson3d4039d2016-09-23 16:31:30 -0700154 std::shared_ptr<FenceTime> acquireFence{FenceTime::NO_FENCE};
Brian Andersond6927fb2016-07-23 23:37:30 -0700155};
156
157
Brian Anderson3890c392016-07-25 12:48:08 -0700158// Used by the consumer to keep track of which fields it already sent to
159// the producer.
160class FrameEventDirtyFields {
Brian Andersond6927fb2016-07-23 23:37:30 -0700161public:
Brian Anderson3890c392016-07-25 12:48:08 -0700162 inline void reset() { mBitset.reset(); }
163 inline bool anyDirty() const { return mBitset.any(); }
Brian Andersond6927fb2016-07-23 23:37:30 -0700164
Brian Anderson3890c392016-07-25 12:48:08 -0700165 template <FrameEvent event>
166 inline void setDirty() {
167 constexpr size_t eventIndex = static_cast<size_t>(event);
168 static_assert(eventIndex < FrameEvents::EVENT_COUNT, "Bad index.");
169 mBitset.set(eventIndex);
170 }
171
172 template <FrameEvent event>
173 inline bool isDirty() const {
174 constexpr size_t eventIndex = static_cast<size_t>(event);
175 static_assert(eventIndex < FrameEvents::EVENT_COUNT, "Bad index.");
176 return mBitset[eventIndex];
177 }
178
179private:
180 std::bitset<FrameEvents::EVENT_COUNT> mBitset;
181};
182
183
184// The consumer's interface to FrameEventHistory
185class ConsumerFrameEventHistory : public FrameEventHistory {
186public:
187 ~ConsumerFrameEventHistory() override;
188
189 void addQueue(const NewFrameEventsEntry& newEntry);
Brian Andersond6927fb2016-07-23 23:37:30 -0700190 void addLatch(uint64_t frameNumber, nsecs_t latchTime);
191 void addPreComposition(uint64_t frameNumber, nsecs_t refreshStartTime);
192 void addPostComposition(uint64_t frameNumber,
Brian Anderson3d4039d2016-09-23 16:31:30 -0700193 const std::shared_ptr<FenceTime>& gpuCompositionDone,
194 const std::shared_ptr<FenceTime>& displayPresent);
195 void addRetire(uint64_t frameNumber,
196 const std::shared_ptr<FenceTime>& displayRetire);
197 void addRelease(uint64_t frameNumber,
198 std::shared_ptr<FenceTime>&& release);
Brian Andersond6927fb2016-07-23 23:37:30 -0700199
Brian Anderson3890c392016-07-25 12:48:08 -0700200 void getAndResetDelta(FrameEventHistoryDelta* delta);
201
Brian Andersond6927fb2016-07-23 23:37:30 -0700202private:
Brian Anderson3d4039d2016-09-23 16:31:30 -0700203 void getFrameDelta(FrameEventHistoryDelta* delta,
204 const std::array<FrameEvents, MAX_FRAME_HISTORY>::iterator& frame);
205
Brian Anderson3890c392016-07-25 12:48:08 -0700206 std::array<FrameEventDirtyFields, MAX_FRAME_HISTORY> mFramesDirty;
Brian Andersond6927fb2016-07-23 23:37:30 -0700207 size_t mQueueOffset{0};
208 size_t mCompositionOffset{0};
209 size_t mRetireOffset{0};
210 size_t mReleaseOffset{0};
Brian Anderson4565daa2016-12-13 15:41:28 -0800211
212 bool mProducerWantsEvents{false};
Brian Andersond6927fb2016-07-23 23:37:30 -0700213};
214
Brian Anderson3890c392016-07-25 12:48:08 -0700215
216// A single frame update from the consumer to producer that can be sent
217// through Binder.
218// Although this may be sent multiple times for the same frame as new
219// timestamps are set, Fences only need to be sent once.
220class FrameEventsDelta : public Flattenable<FrameEventsDelta> {
221friend class ProducerFrameEventHistory;
222public:
223 FrameEventsDelta() = default;
224 FrameEventsDelta(size_t index,
225 const FrameEvents& frameTimestamps,
226 const FrameEventDirtyFields& dirtyFields);
227
Brian Anderson3d4039d2016-09-23 16:31:30 -0700228 // Movable.
229 FrameEventsDelta(FrameEventsDelta&& src) = default;
230 FrameEventsDelta& operator=(FrameEventsDelta&& src) = default;
231 // Not copyable.
232 FrameEventsDelta(const FrameEventsDelta& src) = delete;
233 FrameEventsDelta& operator=(const FrameEventsDelta& src) = delete;
234
Brian Anderson3890c392016-07-25 12:48:08 -0700235 // Flattenable implementation
236 size_t getFlattenedSize() const;
237 size_t getFdCount() const;
238 status_t flatten(void*& buffer, size_t& size, int*& fds,
239 size_t& count) const;
240 status_t unflatten(void const*& buffer, size_t& size, int const*& fds,
241 size_t& count);
242
243private:
244 static size_t minFlattenedSize();
245
246 size_t mIndex{0};
247 uint64_t mFrameNumber{0};
248
249 bool mAddPostCompositeCalled{0};
250 bool mAddRetireCalled{0};
251 bool mAddReleaseCalled{0};
252
253 nsecs_t mPostedTime{0};
254 nsecs_t mRequestedPresentTime{0};
255 nsecs_t mLatchTime{0};
256 nsecs_t mFirstRefreshStartTime{0};
257 nsecs_t mLastRefreshStartTime{0};
258
Brian Anderson3d4039d2016-09-23 16:31:30 -0700259 FenceTime::Snapshot mGpuCompositionDoneFence;
260 FenceTime::Snapshot mDisplayPresentFence;
261 FenceTime::Snapshot mDisplayRetireFence;
262 FenceTime::Snapshot mReleaseFence;
Brian Anderson3890c392016-07-25 12:48:08 -0700263
264 // This is a static method with an auto return value so we can call
265 // it without needing const and non-const versions.
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700266 template <typename ThisT>
267 static inline auto allFences(ThisT fed) ->
268 std::array<decltype(&fed->mReleaseFence), 4> {
Brian Anderson3890c392016-07-25 12:48:08 -0700269 return {{
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700270 &fed->mGpuCompositionDoneFence, &fed->mDisplayPresentFence,
271 &fed->mDisplayRetireFence, &fed->mReleaseFence
Brian Anderson3890c392016-07-25 12:48:08 -0700272 }};
273 }
274};
275
276
277// A collection of updates from consumer to producer that can be sent
278// through Binder.
279class FrameEventHistoryDelta
280 : public Flattenable<FrameEventHistoryDelta> {
281
282friend class ConsumerFrameEventHistory;
283friend class ProducerFrameEventHistory;
284
285public:
Brian Anderson3d4039d2016-09-23 16:31:30 -0700286 FrameEventHistoryDelta() = default;
287
288 // Movable.
289 FrameEventHistoryDelta(FrameEventHistoryDelta&& src) = default;
290 FrameEventHistoryDelta& operator=(FrameEventHistoryDelta&& src);
291 // Not copyable.
292 FrameEventHistoryDelta(const FrameEventHistoryDelta& src) = delete;
293 FrameEventHistoryDelta& operator=(
294 const FrameEventHistoryDelta& src) = delete;
295
Brian Anderson3890c392016-07-25 12:48:08 -0700296 // Flattenable implementation.
297 size_t getFlattenedSize() const;
298 size_t getFdCount() const;
299 status_t flatten(void*& buffer, size_t& size, int*& fds,
300 size_t& count) const;
301 status_t unflatten(void const*& buffer, size_t& size, int const*& fds,
302 size_t& count);
303
304private:
305 static size_t minFlattenedSize();
306
307 std::vector<FrameEventsDelta> mDeltas;
308};
309
310
Pablo Ceballosce796e72016-02-04 19:10:51 -0800311} // namespace android
312#endif