blob: 912d04c5d87d1e0db8946371464e5c9161386b5e [file] [log] [blame]
John Reckba6adf62015-02-19 14:36:50 -08001/*
2 * Copyright (C) 2015 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#ifndef FRAMEINFO_H_
17#define FRAMEINFO_H_
18
19#include "utils/Macros.h"
20
21#include <cutils/compiler.h>
22#include <utils/Timers.h>
23
Siarhei Vishniakouefed1662020-11-30 10:30:52 -100024#include <array>
John Reckba6adf62015-02-19 14:36:50 -080025#include <memory.h>
John Reck4db3d172015-06-02 15:58:43 -070026#include <string>
John Reckba6adf62015-02-19 14:36:50 -080027
28namespace android {
29namespace uirenderer {
30
Siarhei Vishniakou4bcbffd2021-02-17 06:19:36 +000031static constexpr size_t UI_THREAD_FRAME_INFO_SIZE = 10;
John Reckba6adf62015-02-19 14:36:50 -080032
John Reckc87be992015-02-20 10:57:22 -080033enum class FrameInfoIndex {
Chris Craik1b54fb22015-06-02 17:40:58 -070034 Flags = 0,
Steven Thomas6fabb5a2020-08-21 16:56:08 -070035 FrameTimelineVsyncId,
Chris Craik1b54fb22015-06-02 17:40:58 -070036 IntendedVsync,
37 Vsync,
Siarhei Vishniakou4bcbffd2021-02-17 06:19:36 +000038 InputEventId,
Chris Craik1b54fb22015-06-02 17:40:58 -070039 HandleInputStart,
40 AnimationStart,
41 PerformTraversalsStart,
42 DrawStart,
Ady Abrahamdfb13982020-10-05 17:59:09 -070043 FrameDeadline,
John Reckba6adf62015-02-19 14:36:50 -080044 // End of UI frame info
45
John Reckbe3fba02015-07-06 13:49:58 -070046 SyncQueued,
47
Chris Craik1b54fb22015-06-02 17:40:58 -070048 SyncStart,
49 IssueDrawCommandsStart,
50 SwapBuffers,
51 FrameCompleted,
John Reckba6adf62015-02-19 14:36:50 -080052
John Reck2d5b8d72016-07-28 15:36:11 -070053 DequeueBufferDuration,
54 QueueBufferDuration,
55
Stan Iliev7203e1f2019-07-25 13:12:02 -040056 GpuCompleted,
Jorim Jaggi71db8892021-02-03 23:19:29 +010057 SwapBuffersCompleted,
Siarhei Vishniakou4bcbffd2021-02-17 06:19:36 +000058 DisplayPresentTime,
Stan Iliev7203e1f2019-07-25 13:12:02 -040059
John Reckba6adf62015-02-19 14:36:50 -080060 // Must be the last value!
John Reck65ddb152016-08-02 09:38:26 -070061 // Also must be kept in sync with FrameMetrics.java#FRAME_STATS_COUNT
Chris Craik1b54fb22015-06-02 17:40:58 -070062 NumIndexes
John Reckc87be992015-02-20 10:57:22 -080063};
John Reckba6adf62015-02-19 14:36:50 -080064
Siarhei Vishniakou4bcbffd2021-02-17 06:19:36 +000065extern const std::array<const char*, static_cast<int>(FrameInfoIndex::NumIndexes)> FrameInfoNames;
John Reck4db3d172015-06-02 15:58:43 -070066
Chris Craik1b54fb22015-06-02 17:40:58 -070067namespace FrameInfoFlags {
John Reck1bcacfd2017-11-03 10:12:19 -070068enum {
69 WindowLayoutChanged = 1 << 0,
70 RTAnimation = 1 << 1,
71 SurfaceCanvas = 1 << 2,
72 SkippedFrame = 1 << 3,
73};
John Reckc87be992015-02-20 10:57:22 -080074};
John Reckba6adf62015-02-19 14:36:50 -080075
Derek Sollenberger3fedf5a2020-02-21 13:07:28 -050076class UiFrameInfoBuilder {
John Reckba6adf62015-02-19 14:36:50 -080077public:
Steven Thomas6fabb5a2020-08-21 16:56:08 -070078 static constexpr int64_t INVALID_VSYNC_ID = -1;
79
Chih-Hung Hsiehfaecb782016-07-21 11:23:06 -070080 explicit UiFrameInfoBuilder(int64_t* buffer) : mBuffer(buffer) {
John Reckba6adf62015-02-19 14:36:50 -080081 memset(mBuffer, 0, UI_THREAD_FRAME_INFO_SIZE * sizeof(int64_t));
Steven Thomas6fabb5a2020-08-21 16:56:08 -070082 set(FrameInfoIndex::FrameTimelineVsyncId) = INVALID_VSYNC_ID;
Ady Abrahamdfb13982020-10-05 17:59:09 -070083 set(FrameInfoIndex::FrameDeadline) = std::numeric_limits<int64_t>::max();
John Reckba6adf62015-02-19 14:36:50 -080084 }
85
Ady Abrahamdfb13982020-10-05 17:59:09 -070086 UiFrameInfoBuilder& setVsync(nsecs_t vsyncTime, nsecs_t intendedVsync,
87 int64_t vsyncId, int64_t frameDeadline) {
Steven Thomas6fabb5a2020-08-21 16:56:08 -070088 set(FrameInfoIndex::FrameTimelineVsyncId) = vsyncId;
Chris Craik1b54fb22015-06-02 17:40:58 -070089 set(FrameInfoIndex::Vsync) = vsyncTime;
90 set(FrameInfoIndex::IntendedVsync) = intendedVsync;
John Reckbf3c6022015-06-02 15:55:00 -070091 // Pretend the other fields are all at vsync, too, so that naive
92 // duration calculations end up being 0 instead of very large
Chris Craik1b54fb22015-06-02 17:40:58 -070093 set(FrameInfoIndex::HandleInputStart) = vsyncTime;
94 set(FrameInfoIndex::AnimationStart) = vsyncTime;
95 set(FrameInfoIndex::PerformTraversalsStart) = vsyncTime;
96 set(FrameInfoIndex::DrawStart) = vsyncTime;
Ady Abrahamdfb13982020-10-05 17:59:09 -070097 set(FrameInfoIndex::FrameDeadline) = frameDeadline;
John Reckba6adf62015-02-19 14:36:50 -080098 return *this;
99 }
100
Chris Craik1b54fb22015-06-02 17:40:58 -0700101 UiFrameInfoBuilder& addFlag(int frameInfoFlag) {
102 set(FrameInfoIndex::Flags) |= static_cast<uint64_t>(frameInfoFlag);
John Reckba6adf62015-02-19 14:36:50 -0800103 return *this;
104 }
105
106private:
John Reck1bcacfd2017-11-03 10:12:19 -0700107 inline int64_t& set(FrameInfoIndex index) { return mBuffer[static_cast<int>(index)]; }
John Reckc87be992015-02-20 10:57:22 -0800108
John Reckba6adf62015-02-19 14:36:50 -0800109 int64_t* mBuffer;
110};
111
112class FrameInfo {
113public:
114 void importUiThreadInfo(int64_t* info);
115
Jerome Gaillarde218c692019-06-14 12:58:57 +0100116 void markSyncStart() { set(FrameInfoIndex::SyncStart) = systemTime(SYSTEM_TIME_MONOTONIC); }
John Reckba6adf62015-02-19 14:36:50 -0800117
118 void markIssueDrawCommandsStart() {
Jerome Gaillarde218c692019-06-14 12:58:57 +0100119 set(FrameInfoIndex::IssueDrawCommandsStart) = systemTime(SYSTEM_TIME_MONOTONIC);
John Reckba6adf62015-02-19 14:36:50 -0800120 }
121
Jerome Gaillarde218c692019-06-14 12:58:57 +0100122 void markSwapBuffers() { set(FrameInfoIndex::SwapBuffers) = systemTime(SYSTEM_TIME_MONOTONIC); }
John Reckba6adf62015-02-19 14:36:50 -0800123
Jorim Jaggi71db8892021-02-03 23:19:29 +0100124 void markSwapBuffersCompleted() {
125 set(FrameInfoIndex::SwapBuffersCompleted) = systemTime(SYSTEM_TIME_MONOTONIC);
126 }
127
Jerome Gaillarde218c692019-06-14 12:58:57 +0100128 void markFrameCompleted() { set(FrameInfoIndex::FrameCompleted) = systemTime(SYSTEM_TIME_MONOTONIC); }
John Reckba6adf62015-02-19 14:36:50 -0800129
Chris Craik1b54fb22015-06-02 17:40:58 -0700130 void addFlag(int frameInfoFlag) {
131 set(FrameInfoIndex::Flags) |= static_cast<uint64_t>(frameInfoFlag);
John Reck240ff622015-04-28 13:50:00 -0700132 }
133
John Reck1bcacfd2017-11-03 10:12:19 -0700134 const int64_t* data() const { return mFrameInfo; }
Andres Morales06f5bc72015-12-15 15:21:31 -0800135
John Reck1bcacfd2017-11-03 10:12:19 -0700136 inline int64_t operator[](FrameInfoIndex index) const { return get(index); }
John Reckba6adf62015-02-19 14:36:50 -0800137
John Reck41300272015-06-03 14:42:34 -0700138 inline int64_t operator[](int index) const {
Chris Craik1b54fb22015-06-02 17:40:58 -0700139 if (index < 0 || index >= static_cast<int>(FrameInfoIndex::NumIndexes)) return 0;
John Reckc87be992015-02-20 10:57:22 -0800140 return mFrameInfo[index];
John Reckba6adf62015-02-19 14:36:50 -0800141 }
142
John Reck41300272015-06-03 14:42:34 -0700143 inline int64_t duration(FrameInfoIndex start, FrameInfoIndex end) const {
John Reckbe3fba02015-07-06 13:49:58 -0700144 int64_t endtime = get(end);
145 int64_t starttime = get(start);
John Reck41300272015-06-03 14:42:34 -0700146 int64_t gap = endtime - starttime;
147 gap = starttime > 0 ? gap : 0;
John Reck1bcacfd2017-11-03 10:12:19 -0700148 if (end > FrameInfoIndex::SyncQueued && start < FrameInfoIndex::SyncQueued) {
John Reckbe3fba02015-07-06 13:49:58 -0700149 // Need to subtract out the time spent in a stalled state
150 // as this will be captured by the previous frame's info
John Reck1bcacfd2017-11-03 10:12:19 -0700151 int64_t offset = get(FrameInfoIndex::SyncStart) - get(FrameInfoIndex::SyncQueued);
John Reckbe3fba02015-07-06 13:49:58 -0700152 if (offset > 0) {
153 gap -= offset;
154 }
155 }
John Reck41300272015-06-03 14:42:34 -0700156 return gap > 0 ? gap : 0;
157 }
158
159 inline int64_t totalDuration() const {
160 return duration(FrameInfoIndex::IntendedVsync, FrameInfoIndex::FrameCompleted);
161 }
162
Stan Iliev7203e1f2019-07-25 13:12:02 -0400163 inline int64_t gpuDrawTime() const {
164 // GPU start time is approximated to the moment before swapBuffer is invoked.
165 // We could add an EGLSyncKHR fence at the beginning of the frame, but that is an overhead.
166 int64_t endTime = get(FrameInfoIndex::GpuCompleted);
Boleyn Sua9ce6622020-11-18 03:15:29 +0000167 return endTime > 0 ? endTime - get(FrameInfoIndex::SwapBuffers) : -1;
Stan Iliev7203e1f2019-07-25 13:12:02 -0400168 }
169
John Reck1bcacfd2017-11-03 10:12:19 -0700170 inline int64_t& set(FrameInfoIndex index) { return mFrameInfo[static_cast<int>(index)]; }
John Reckc87be992015-02-20 10:57:22 -0800171
John Reckbe3fba02015-07-06 13:49:58 -0700172 inline int64_t get(FrameInfoIndex index) const {
173 if (index == FrameInfoIndex::NumIndexes) return 0;
174 return mFrameInfo[static_cast<int>(index)];
175 }
176
177private:
Chris Craik1b54fb22015-06-02 17:40:58 -0700178 int64_t mFrameInfo[static_cast<int>(FrameInfoIndex::NumIndexes)];
John Reckba6adf62015-02-19 14:36:50 -0800179};
180
181} /* namespace uirenderer */
182} /* namespace android */
183
184#endif /* FRAMEINFO_H_ */