blob: 3d1be4d2ebc20918209b1cc3d38a10708ea5d39b [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
Huihong Luo0a81aa32022-02-22 16:02:36 -080020#include <android/gui/FrameEvent.h>
21
Dominik Laskowski5a5e01e2022-07-08 07:52:44 -070022#include <gui/CompositorTiming.h>
Brian Anderson3d4039d2016-09-23 16:31:30 -070023#include <ui/FenceTime.h>
Pablo Ceballosce796e72016-02-04 19:10:51 -080024#include <utils/Flattenable.h>
Brian Andersond6927fb2016-07-23 23:37:30 -070025#include <utils/StrongPointer.h>
26#include <utils/Timers.h>
27
28#include <array>
Brian Anderson3890c392016-07-25 12:48:08 -070029#include <bitset>
30#include <vector>
Pablo Ceballosce796e72016-02-04 19:10:51 -080031
32namespace android {
33
Brian Andersond6927fb2016-07-23 23:37:30 -070034struct FrameEvents;
Brian Anderson3890c392016-07-25 12:48:08 -070035class FrameEventHistoryDelta;
Brian Andersond6927fb2016-07-23 23:37:30 -070036
Dominik Laskowski5a5e01e2022-07-08 07:52:44 -070037using gui::CompositorTiming;
Huihong Luo0a81aa32022-02-22 16:02:36 -080038using gui::FrameEvent;
Brian Andersond6927fb2016-07-23 23:37:30 -070039
40// A collection of timestamps corresponding to a single frame.
41struct FrameEvents {
Brian Andersoned816e62016-10-26 16:12:21 -070042 static constexpr auto EVENT_COUNT =
43 static_cast<size_t>(FrameEvent::EVENT_COUNT);
44 static_assert(EVENT_COUNT <= 32, "Event count sanity check failed.");
Brian Andersondc96fdf2017-03-20 16:54:25 -070045 static constexpr nsecs_t TIMESTAMP_PENDING = -2;
Brian Andersoned816e62016-10-26 16:12:21 -070046
47 static inline bool isValidTimestamp(nsecs_t time) {
48 return time != TIMESTAMP_PENDING;
49 }
50
Brian Anderson3890c392016-07-25 12:48:08 -070051 bool hasPostedInfo() const;
52 bool hasRequestedPresentInfo() const;
53 bool hasLatchInfo() const;
54 bool hasFirstRefreshStartInfo() const;
55 bool hasLastRefreshStartInfo() const;
56 bool hasAcquireInfo() const;
57 bool hasGpuCompositionDoneInfo() const;
58 bool hasDisplayPresentInfo() const;
Brian Anderson3890c392016-07-25 12:48:08 -070059 bool hasReleaseInfo() const;
Brian Andersonf6386862016-10-31 16:34:13 -070060 bool hasDequeueReadyInfo() const;
Brian Anderson3890c392016-07-25 12:48:08 -070061
Brian Andersond6927fb2016-07-23 23:37:30 -070062 void checkFencesForCompletion();
Yiwei Zhang5434a782018-12-05 18:06:32 -080063 void dump(std::string& outString) const;
Brian Andersond6927fb2016-07-23 23:37:30 -070064
65 bool valid{false};
Brian Anderson5ea5e592016-12-01 16:54:33 -080066 int connectId{0};
Brian Andersond6927fb2016-07-23 23:37:30 -070067 uint64_t frameNumber{0};
68
69 // Whether or not certain points in the frame's life cycle have been
70 // encountered help us determine if timestamps aren't available because
71 // a) we'll just never get them or b) they're not ready yet.
72 bool addPostCompositeCalled{false};
Brian Anderson3890c392016-07-25 12:48:08 -070073 bool addReleaseCalled{false};
Brian Andersond6927fb2016-07-23 23:37:30 -070074
Brian Andersoned816e62016-10-26 16:12:21 -070075 nsecs_t postedTime{TIMESTAMP_PENDING};
76 nsecs_t requestedPresentTime{TIMESTAMP_PENDING};
77 nsecs_t latchTime{TIMESTAMP_PENDING};
78 nsecs_t firstRefreshStartTime{TIMESTAMP_PENDING};
79 nsecs_t lastRefreshStartTime{TIMESTAMP_PENDING};
80 nsecs_t dequeueReadyTime{TIMESTAMP_PENDING};
Brian Andersond6927fb2016-07-23 23:37:30 -070081
Brian Anderson3d4039d2016-09-23 16:31:30 -070082 std::shared_ptr<FenceTime> acquireFence{FenceTime::NO_FENCE};
83 std::shared_ptr<FenceTime> gpuCompositionDoneFence{FenceTime::NO_FENCE};
84 std::shared_ptr<FenceTime> displayPresentFence{FenceTime::NO_FENCE};
Brian Anderson3d4039d2016-09-23 16:31:30 -070085 std::shared_ptr<FenceTime> releaseFence{FenceTime::NO_FENCE};
Brian Andersond6927fb2016-07-23 23:37:30 -070086};
87
Brian Anderson3890c392016-07-25 12:48:08 -070088// A short history of frames that are synchronized between the consumer and
89// producer via deltas.
90class FrameEventHistory {
91public:
David Stevens7347f0b2020-01-15 20:19:22 +090092 FrameEventHistory();
Brian Anderson3890c392016-07-25 12:48:08 -070093 virtual ~FrameEventHistory();
94
95 FrameEvents* getFrame(uint64_t frameNumber);
96 FrameEvents* getFrame(uint64_t frameNumber, size_t* iHint);
97 void checkFencesForCompletion();
Yiwei Zhang5434a782018-12-05 18:06:32 -080098 void dump(std::string& outString) const;
Brian Anderson3890c392016-07-25 12:48:08 -070099
Brian Lindahl957985b2023-01-31 15:42:47 -0700100 static const size_t INITIAL_MAX_FRAME_HISTORY;
Brian Anderson3890c392016-07-25 12:48:08 -0700101
102protected:
Brian Lindahl957985b2023-01-31 15:42:47 -0700103 void resize(size_t newSize);
104
David Stevens7347f0b2020-01-15 20:19:22 +0900105 std::vector<FrameEvents> mFrames;
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800106
107 CompositorTiming mCompositorTiming;
Brian Anderson3890c392016-07-25 12:48:08 -0700108};
109
110
111// The producer's interface to FrameEventHistory
112class ProducerFrameEventHistory : public FrameEventHistory {
113public:
114 ~ProducerFrameEventHistory() override;
115
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800116 // Public for testing.
117 static nsecs_t snapToNextTick(
118 nsecs_t timestamp, nsecs_t tickPhase, nsecs_t tickInterval);
Vishnu Nair9a69a042021-06-18 13:19:49 -0700119 nsecs_t getReportedCompositeDeadline() const { return mCompositorTiming.deadline; };
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800120
121 nsecs_t getNextCompositeDeadline(const nsecs_t now) const;
122 nsecs_t getCompositeInterval() const { return mCompositorTiming.interval; }
123 nsecs_t getCompositeToPresentLatency() const {
124 return mCompositorTiming.presentLatency;
125 }
126
Brian Anderson3da8d272016-07-28 16:20:47 -0700127 // virtual for testing.
128 virtual void updateAcquireFence(
Brian Anderson3d4039d2016-09-23 16:31:30 -0700129 uint64_t frameNumber, std::shared_ptr<FenceTime>&& acquire);
Brian Anderson3890c392016-07-25 12:48:08 -0700130 void applyDelta(const FrameEventHistoryDelta& delta);
131
Brian Anderson3d4039d2016-09-23 16:31:30 -0700132 void updateSignalTimes();
133
Brian Anderson3da8d272016-07-28 16:20:47 -0700134protected:
135 void applyFenceDelta(FenceTimeline* timeline,
136 std::shared_ptr<FenceTime>* dst,
137 const FenceTime::Snapshot& src) const;
138
139 // virtual for testing.
140 virtual std::shared_ptr<FenceTime> createFenceTime(
141 const sp<Fence>& fence) const;
142
Brian Lindahl957985b2023-01-31 15:42:47 -0700143 void resize(size_t newSize);
144
Brian Anderson3890c392016-07-25 12:48:08 -0700145 size_t mAcquireOffset{0};
Brian Anderson3d4039d2016-09-23 16:31:30 -0700146
147 // The consumer updates it's timelines in Layer and SurfaceFlinger since
148 // they can coordinate shared timelines better. The producer doesn't have
149 // shared timelines though, so just let it own and update all of them.
150 FenceTimeline mAcquireTimeline;
151 FenceTimeline mGpuCompositionDoneTimeline;
152 FenceTimeline mPresentTimeline;
Brian Anderson3d4039d2016-09-23 16:31:30 -0700153 FenceTimeline mReleaseTimeline;
Brian Anderson3890c392016-07-25 12:48:08 -0700154};
155
156
157// Used by the consumer to create a new frame event record that is
158// partially complete.
Brian Andersond6927fb2016-07-23 23:37:30 -0700159struct NewFrameEventsEntry {
160 uint64_t frameNumber{0};
161 nsecs_t postedTime{0};
162 nsecs_t requestedPresentTime{0};
Brian Anderson3d4039d2016-09-23 16:31:30 -0700163 std::shared_ptr<FenceTime> acquireFence{FenceTime::NO_FENCE};
Brian Andersond6927fb2016-07-23 23:37:30 -0700164};
165
Brian Anderson3890c392016-07-25 12:48:08 -0700166// Used by the consumer to keep track of which fields it already sent to
167// the producer.
168class FrameEventDirtyFields {
Brian Andersond6927fb2016-07-23 23:37:30 -0700169public:
Brian Anderson3890c392016-07-25 12:48:08 -0700170 inline void reset() { mBitset.reset(); }
171 inline bool anyDirty() const { return mBitset.any(); }
Brian Andersond6927fb2016-07-23 23:37:30 -0700172
Brian Anderson3890c392016-07-25 12:48:08 -0700173 template <FrameEvent event>
174 inline void setDirty() {
175 constexpr size_t eventIndex = static_cast<size_t>(event);
176 static_assert(eventIndex < FrameEvents::EVENT_COUNT, "Bad index.");
177 mBitset.set(eventIndex);
178 }
179
180 template <FrameEvent event>
181 inline bool isDirty() const {
182 constexpr size_t eventIndex = static_cast<size_t>(event);
183 static_assert(eventIndex < FrameEvents::EVENT_COUNT, "Bad index.");
184 return mBitset[eventIndex];
185 }
186
187private:
188 std::bitset<FrameEvents::EVENT_COUNT> mBitset;
189};
190
191
192// The consumer's interface to FrameEventHistory
193class ConsumerFrameEventHistory : public FrameEventHistory {
194public:
David Stevens7347f0b2020-01-15 20:19:22 +0900195 ConsumerFrameEventHistory();
Brian Anderson3890c392016-07-25 12:48:08 -0700196 ~ConsumerFrameEventHistory() override;
197
Brian Anderson5ea5e592016-12-01 16:54:33 -0800198 void onDisconnect();
Valerie Hau0ca3f992020-01-30 16:07:35 -0800199 void setProducerWantsEvents();
Brian Anderson5ea5e592016-12-01 16:54:33 -0800200
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800201 void initializeCompositorTiming(const CompositorTiming& compositorTiming);
202
Brian Anderson3890c392016-07-25 12:48:08 -0700203 void addQueue(const NewFrameEventsEntry& newEntry);
Brian Andersond6927fb2016-07-23 23:37:30 -0700204 void addLatch(uint64_t frameNumber, nsecs_t latchTime);
205 void addPreComposition(uint64_t frameNumber, nsecs_t refreshStartTime);
206 void addPostComposition(uint64_t frameNumber,
Brian Anderson3d4039d2016-09-23 16:31:30 -0700207 const std::shared_ptr<FenceTime>& gpuCompositionDone,
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800208 const std::shared_ptr<FenceTime>& displayPresent,
209 const CompositorTiming& compositorTiming);
Brian Andersonf6386862016-10-31 16:34:13 -0700210 void addRelease(uint64_t frameNumber, nsecs_t dequeueReadyTime,
Brian Anderson3d4039d2016-09-23 16:31:30 -0700211 std::shared_ptr<FenceTime>&& release);
Brian Andersond6927fb2016-07-23 23:37:30 -0700212
Brian Anderson3890c392016-07-25 12:48:08 -0700213 void getAndResetDelta(FrameEventHistoryDelta* delta);
214
Brian Lindahl957985b2023-01-31 15:42:47 -0700215 void resize(size_t newSize);
216
Brian Andersond6927fb2016-07-23 23:37:30 -0700217private:
Brian Anderson3d4039d2016-09-23 16:31:30 -0700218 void getFrameDelta(FrameEventHistoryDelta* delta,
David Stevens7347f0b2020-01-15 20:19:22 +0900219 const std::vector<FrameEvents>::iterator& frame);
Brian Anderson3d4039d2016-09-23 16:31:30 -0700220
David Stevens7347f0b2020-01-15 20:19:22 +0900221 std::vector<FrameEventDirtyFields> mFramesDirty;
Brian Anderson5ea5e592016-12-01 16:54:33 -0800222
Brian Andersond6927fb2016-07-23 23:37:30 -0700223 size_t mQueueOffset{0};
224 size_t mCompositionOffset{0};
Brian Andersond6927fb2016-07-23 23:37:30 -0700225 size_t mReleaseOffset{0};
Brian Anderson4565daa2016-12-13 15:41:28 -0800226
Brian Anderson5ea5e592016-12-01 16:54:33 -0800227 int mCurrentConnectId{0};
Brian Anderson4565daa2016-12-13 15:41:28 -0800228 bool mProducerWantsEvents{false};
Brian Andersond6927fb2016-07-23 23:37:30 -0700229};
230
Brian Anderson3890c392016-07-25 12:48:08 -0700231
232// A single frame update from the consumer to producer that can be sent
233// through Binder.
234// Although this may be sent multiple times for the same frame as new
235// timestamps are set, Fences only need to be sent once.
236class FrameEventsDelta : public Flattenable<FrameEventsDelta> {
237friend class ProducerFrameEventHistory;
238public:
239 FrameEventsDelta() = default;
240 FrameEventsDelta(size_t index,
241 const FrameEvents& frameTimestamps,
242 const FrameEventDirtyFields& dirtyFields);
243
Brian Anderson3d4039d2016-09-23 16:31:30 -0700244 // Movable.
245 FrameEventsDelta(FrameEventsDelta&& src) = default;
246 FrameEventsDelta& operator=(FrameEventsDelta&& src) = default;
247 // Not copyable.
248 FrameEventsDelta(const FrameEventsDelta& src) = delete;
249 FrameEventsDelta& operator=(const FrameEventsDelta& src) = delete;
250
Brian Anderson3890c392016-07-25 12:48:08 -0700251 // Flattenable implementation
252 size_t getFlattenedSize() const;
253 size_t getFdCount() const;
254 status_t flatten(void*& buffer, size_t& size, int*& fds,
255 size_t& count) const;
256 status_t unflatten(void const*& buffer, size_t& size, int const*& fds,
257 size_t& count);
258
Brian Lindahl957985b2023-01-31 15:42:47 -0700259 uint64_t getFrameNumber() const;
260 bool getLatchTime(nsecs_t* latchTime) const;
261 bool getDisplayPresentFence(sp<Fence>* fence) const;
262
Brian Anderson3890c392016-07-25 12:48:08 -0700263private:
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800264 static constexpr size_t minFlattenedSize();
Brian Anderson3890c392016-07-25 12:48:08 -0700265
266 size_t mIndex{0};
267 uint64_t mFrameNumber{0};
268
269 bool mAddPostCompositeCalled{0};
Brian Anderson3890c392016-07-25 12:48:08 -0700270 bool mAddReleaseCalled{0};
271
Brian Andersoned816e62016-10-26 16:12:21 -0700272 nsecs_t mPostedTime{FrameEvents::TIMESTAMP_PENDING};
273 nsecs_t mRequestedPresentTime{FrameEvents::TIMESTAMP_PENDING};
274 nsecs_t mLatchTime{FrameEvents::TIMESTAMP_PENDING};
275 nsecs_t mFirstRefreshStartTime{FrameEvents::TIMESTAMP_PENDING};
276 nsecs_t mLastRefreshStartTime{FrameEvents::TIMESTAMP_PENDING};
277 nsecs_t mDequeueReadyTime{FrameEvents::TIMESTAMP_PENDING};
Brian Anderson3890c392016-07-25 12:48:08 -0700278
Brian Anderson3d4039d2016-09-23 16:31:30 -0700279 FenceTime::Snapshot mGpuCompositionDoneFence;
280 FenceTime::Snapshot mDisplayPresentFence;
Brian Anderson3d4039d2016-09-23 16:31:30 -0700281 FenceTime::Snapshot mReleaseFence;
Brian Anderson3890c392016-07-25 12:48:08 -0700282
283 // This is a static method with an auto return value so we can call
284 // it without needing const and non-const versions.
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700285 template <typename ThisT>
286 static inline auto allFences(ThisT fed) ->
Brian Anderson4e606e32017-03-16 15:34:57 -0700287 std::array<decltype(&fed->mReleaseFence), 3> {
Brian Anderson3890c392016-07-25 12:48:08 -0700288 return {{
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700289 &fed->mGpuCompositionDoneFence, &fed->mDisplayPresentFence,
Brian Anderson4e606e32017-03-16 15:34:57 -0700290 &fed->mReleaseFence
Brian Anderson3890c392016-07-25 12:48:08 -0700291 }};
292 }
293};
294
295
296// A collection of updates from consumer to producer that can be sent
297// through Binder.
298class FrameEventHistoryDelta
299 : public Flattenable<FrameEventHistoryDelta> {
300
301friend class ConsumerFrameEventHistory;
302friend class ProducerFrameEventHistory;
303
304public:
Brian Anderson3d4039d2016-09-23 16:31:30 -0700305 FrameEventHistoryDelta() = default;
306
307 // Movable.
308 FrameEventHistoryDelta(FrameEventHistoryDelta&& src) = default;
Chih-Hung Hsieh5bc849f2018-09-25 14:21:50 -0700309 FrameEventHistoryDelta& operator=(FrameEventHistoryDelta&& src) noexcept;
Brian Anderson3d4039d2016-09-23 16:31:30 -0700310 // Not copyable.
311 FrameEventHistoryDelta(const FrameEventHistoryDelta& src) = delete;
312 FrameEventHistoryDelta& operator=(
313 const FrameEventHistoryDelta& src) = delete;
314
Brian Anderson3890c392016-07-25 12:48:08 -0700315 // Flattenable implementation.
316 size_t getFlattenedSize() const;
317 size_t getFdCount() const;
318 status_t flatten(void*& buffer, size_t& size, int*& fds,
319 size_t& count) const;
320 status_t unflatten(void const*& buffer, size_t& size, int const*& fds,
321 size_t& count);
322
Brian Lindahl957985b2023-01-31 15:42:47 -0700323 std::vector<FrameEventsDelta>::const_iterator begin() const;
324 std::vector<FrameEventsDelta>::const_iterator end() const;
325
Brian Anderson3890c392016-07-25 12:48:08 -0700326private:
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800327 static constexpr size_t minFlattenedSize();
Brian Anderson3890c392016-07-25 12:48:08 -0700328
329 std::vector<FrameEventsDelta> mDeltas;
Brian Anderson0a61b0c2016-12-07 14:55:56 -0800330 CompositorTiming mCompositorTiming;
Brian Anderson3890c392016-07-25 12:48:08 -0700331};
332
333
Pablo Ceballosce796e72016-02-04 19:10:51 -0800334} // namespace android
335#endif