| John Reck | ba6adf6 | 2015-02-19 14:36:50 -0800 | [diff] [blame] | 1 | /* | 
|  | 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 |  | 
| John Reck | ba6adf6 | 2015-02-19 14:36:50 -0800 | [diff] [blame] | 19 | #include <cutils/compiler.h> | 
| Ady Abraham | e088dcd | 2023-08-10 11:45:58 -0700 | [diff] [blame] | 20 | #include <memory.h> | 
| John Reck | ba6adf6 | 2015-02-19 14:36:50 -0800 | [diff] [blame] | 21 | #include <utils/Timers.h> | 
|  | 22 |  | 
| Siarhei Vishniakou | efed166 | 2020-11-30 10:30:52 -1000 | [diff] [blame] | 23 | #include <array> | 
| Ady Abraham | e088dcd | 2023-08-10 11:45:58 -0700 | [diff] [blame] | 24 | #include <optional> | 
| John Reck | 4db3d17 | 2015-06-02 15:58:43 -0700 | [diff] [blame] | 25 | #include <string> | 
| John Reck | ba6adf6 | 2015-02-19 14:36:50 -0800 | [diff] [blame] | 26 |  | 
| Ady Abraham | e088dcd | 2023-08-10 11:45:58 -0700 | [diff] [blame] | 27 | #include "SkippedFrameInfo.h" | 
|  | 28 | #include "utils/Macros.h" | 
|  | 29 |  | 
| John Reck | ba6adf6 | 2015-02-19 14:36:50 -0800 | [diff] [blame] | 30 | namespace android { | 
|  | 31 | namespace uirenderer { | 
|  | 32 |  | 
| Jorim Jaggi | 10f328c | 2021-01-19 00:08:02 +0100 | [diff] [blame] | 33 | static constexpr size_t UI_THREAD_FRAME_INFO_SIZE = 12; | 
| John Reck | ba6adf6 | 2015-02-19 14:36:50 -0800 | [diff] [blame] | 34 |  | 
| John Reck | c87be99 | 2015-02-20 10:57:22 -0800 | [diff] [blame] | 35 | enum class FrameInfoIndex { | 
| Chris Craik | 1b54fb2 | 2015-06-02 17:40:58 -0700 | [diff] [blame] | 36 | Flags = 0, | 
| Steven Thomas | 6fabb5a | 2020-08-21 16:56:08 -0700 | [diff] [blame] | 37 | FrameTimelineVsyncId, | 
| Chris Craik | 1b54fb2 | 2015-06-02 17:40:58 -0700 | [diff] [blame] | 38 | IntendedVsync, | 
|  | 39 | Vsync, | 
| Siarhei Vishniakou | 4bcbffd | 2021-02-17 06:19:36 +0000 | [diff] [blame] | 40 | InputEventId, | 
| Chris Craik | 1b54fb2 | 2015-06-02 17:40:58 -0700 | [diff] [blame] | 41 | HandleInputStart, | 
|  | 42 | AnimationStart, | 
|  | 43 | PerformTraversalsStart, | 
|  | 44 | DrawStart, | 
| Ady Abraham | dfb1398 | 2020-10-05 17:59:09 -0700 | [diff] [blame] | 45 | FrameDeadline, | 
| Bo Liu | 027b218 | 2021-03-18 16:50:38 -0400 | [diff] [blame] | 46 | FrameStartTime, | 
| Jorim Jaggi | 10f328c | 2021-01-19 00:08:02 +0100 | [diff] [blame] | 47 | FrameInterval, | 
| John Reck | ba6adf6 | 2015-02-19 14:36:50 -0800 | [diff] [blame] | 48 | // End of UI frame info | 
|  | 49 |  | 
| John Reck | be3fba0 | 2015-07-06 13:49:58 -0700 | [diff] [blame] | 50 | SyncQueued, | 
|  | 51 |  | 
| Chris Craik | 1b54fb2 | 2015-06-02 17:40:58 -0700 | [diff] [blame] | 52 | SyncStart, | 
|  | 53 | IssueDrawCommandsStart, | 
|  | 54 | SwapBuffers, | 
|  | 55 | FrameCompleted, | 
| John Reck | ba6adf6 | 2015-02-19 14:36:50 -0800 | [diff] [blame] | 56 |  | 
| John Reck | 2d5b8d7 | 2016-07-28 15:36:11 -0700 | [diff] [blame] | 57 | DequeueBufferDuration, | 
|  | 58 | QueueBufferDuration, | 
|  | 59 |  | 
| Stan Iliev | 7203e1f | 2019-07-25 13:12:02 -0400 | [diff] [blame] | 60 | GpuCompleted, | 
| Jorim Jaggi | 71db889 | 2021-02-03 23:19:29 +0100 | [diff] [blame] | 61 | SwapBuffersCompleted, | 
| Siarhei Vishniakou | 4bcbffd | 2021-02-17 06:19:36 +0000 | [diff] [blame] | 62 | DisplayPresentTime, | 
| Alec Mouri | 3afb397 | 2022-05-27 22:03:11 +0000 | [diff] [blame] | 63 | CommandSubmissionCompleted, | 
| Stan Iliev | 7203e1f | 2019-07-25 13:12:02 -0400 | [diff] [blame] | 64 |  | 
| John Reck | ba6adf6 | 2015-02-19 14:36:50 -0800 | [diff] [blame] | 65 | // Must be the last value! | 
| John Reck | 65ddb15 | 2016-08-02 09:38:26 -0700 | [diff] [blame] | 66 | // Also must be kept in sync with FrameMetrics.java#FRAME_STATS_COUNT | 
| Chris Craik | 1b54fb2 | 2015-06-02 17:40:58 -0700 | [diff] [blame] | 67 | NumIndexes | 
| John Reck | c87be99 | 2015-02-20 10:57:22 -0800 | [diff] [blame] | 68 | }; | 
| John Reck | ba6adf6 | 2015-02-19 14:36:50 -0800 | [diff] [blame] | 69 |  | 
| Siarhei Vishniakou | 4bcbffd | 2021-02-17 06:19:36 +0000 | [diff] [blame] | 70 | extern const std::array<const char*, static_cast<int>(FrameInfoIndex::NumIndexes)> FrameInfoNames; | 
| John Reck | 4db3d17 | 2015-06-02 15:58:43 -0700 | [diff] [blame] | 71 |  | 
| Chris Craik | 1b54fb2 | 2015-06-02 17:40:58 -0700 | [diff] [blame] | 72 | namespace FrameInfoFlags { | 
| John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 73 | enum { | 
| Jorim Jaggi | 9516605 | 2021-05-03 15:49:25 +0200 | [diff] [blame] | 74 | WindowVisibilityChanged = 1 << 0, | 
| John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 75 | RTAnimation = 1 << 1, | 
|  | 76 | SurfaceCanvas = 1 << 2, | 
|  | 77 | SkippedFrame = 1 << 3, | 
|  | 78 | }; | 
| John Reck | c87be99 | 2015-02-20 10:57:22 -0800 | [diff] [blame] | 79 | }; | 
| John Reck | ba6adf6 | 2015-02-19 14:36:50 -0800 | [diff] [blame] | 80 |  | 
| Derek Sollenberger | 3fedf5a | 2020-02-21 13:07:28 -0500 | [diff] [blame] | 81 | class UiFrameInfoBuilder { | 
| John Reck | ba6adf6 | 2015-02-19 14:36:50 -0800 | [diff] [blame] | 82 | public: | 
| Steven Thomas | 6fabb5a | 2020-08-21 16:56:08 -0700 | [diff] [blame] | 83 | static constexpr int64_t INVALID_VSYNC_ID = -1; | 
| Jorim Jaggi | 10f328c | 2021-01-19 00:08:02 +0100 | [diff] [blame] | 84 | static constexpr int64_t UNKNOWN_DEADLINE = std::numeric_limits<int64_t>::max(); | 
|  | 85 | static constexpr int64_t UNKNOWN_FRAME_INTERVAL = -1; | 
|  | 86 |  | 
| Steven Thomas | 6fabb5a | 2020-08-21 16:56:08 -0700 | [diff] [blame] | 87 |  | 
| Chih-Hung Hsieh | faecb78 | 2016-07-21 11:23:06 -0700 | [diff] [blame] | 88 | explicit UiFrameInfoBuilder(int64_t* buffer) : mBuffer(buffer) { | 
| John Reck | ba6adf6 | 2015-02-19 14:36:50 -0800 | [diff] [blame] | 89 | memset(mBuffer, 0, UI_THREAD_FRAME_INFO_SIZE * sizeof(int64_t)); | 
| Steven Thomas | 6fabb5a | 2020-08-21 16:56:08 -0700 | [diff] [blame] | 90 | set(FrameInfoIndex::FrameTimelineVsyncId) = INVALID_VSYNC_ID; | 
| Siarhei Vishniakou | dca1946 | 2021-02-27 08:38:31 +0000 | [diff] [blame] | 91 | // The struct is zeroed by memset above. That also sets FrameInfoIndex::InputEventId to | 
|  | 92 | // equal android::os::IInputConstants::INVALID_INPUT_EVENT_ID == 0. | 
|  | 93 | // Therefore, we can skip setting the value for InputEventId here. If the value for | 
|  | 94 | // INVALID_INPUT_EVENT_ID changes, this code would have to be updated, as well. | 
| Ady Abraham | dfb1398 | 2020-10-05 17:59:09 -0700 | [diff] [blame] | 95 | set(FrameInfoIndex::FrameDeadline) = std::numeric_limits<int64_t>::max(); | 
| John Reck | ba6adf6 | 2015-02-19 14:36:50 -0800 | [diff] [blame] | 96 | } | 
|  | 97 |  | 
| Ady Abraham | dfb1398 | 2020-10-05 17:59:09 -0700 | [diff] [blame] | 98 | UiFrameInfoBuilder& setVsync(nsecs_t vsyncTime, nsecs_t intendedVsync, | 
| Jorim Jaggi | 10f328c | 2021-01-19 00:08:02 +0100 | [diff] [blame] | 99 | int64_t vsyncId, int64_t frameDeadline, nsecs_t frameInterval) { | 
| Steven Thomas | 6fabb5a | 2020-08-21 16:56:08 -0700 | [diff] [blame] | 100 | set(FrameInfoIndex::FrameTimelineVsyncId) = vsyncId; | 
| Chris Craik | 1b54fb2 | 2015-06-02 17:40:58 -0700 | [diff] [blame] | 101 | set(FrameInfoIndex::Vsync) = vsyncTime; | 
|  | 102 | set(FrameInfoIndex::IntendedVsync) = intendedVsync; | 
| John Reck | bf3c602 | 2015-06-02 15:55:00 -0700 | [diff] [blame] | 103 | // Pretend the other fields are all at vsync, too, so that naive | 
|  | 104 | // duration calculations end up being 0 instead of very large | 
| Chris Craik | 1b54fb2 | 2015-06-02 17:40:58 -0700 | [diff] [blame] | 105 | set(FrameInfoIndex::HandleInputStart) = vsyncTime; | 
|  | 106 | set(FrameInfoIndex::AnimationStart) = vsyncTime; | 
|  | 107 | set(FrameInfoIndex::PerformTraversalsStart) = vsyncTime; | 
|  | 108 | set(FrameInfoIndex::DrawStart) = vsyncTime; | 
| Matt Buckley | e9023cf | 2022-11-23 22:39:25 +0000 | [diff] [blame] | 109 | set(FrameInfoIndex::FrameStartTime) = vsyncTime; | 
| Ady Abraham | dfb1398 | 2020-10-05 17:59:09 -0700 | [diff] [blame] | 110 | set(FrameInfoIndex::FrameDeadline) = frameDeadline; | 
| Jorim Jaggi | 10f328c | 2021-01-19 00:08:02 +0100 | [diff] [blame] | 111 | set(FrameInfoIndex::FrameInterval) = frameInterval; | 
| John Reck | ba6adf6 | 2015-02-19 14:36:50 -0800 | [diff] [blame] | 112 | return *this; | 
|  | 113 | } | 
|  | 114 |  | 
| Chris Craik | 1b54fb2 | 2015-06-02 17:40:58 -0700 | [diff] [blame] | 115 | UiFrameInfoBuilder& addFlag(int frameInfoFlag) { | 
|  | 116 | set(FrameInfoIndex::Flags) |= static_cast<uint64_t>(frameInfoFlag); | 
| John Reck | ba6adf6 | 2015-02-19 14:36:50 -0800 | [diff] [blame] | 117 | return *this; | 
|  | 118 | } | 
|  | 119 |  | 
|  | 120 | private: | 
| John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 121 | inline int64_t& set(FrameInfoIndex index) { return mBuffer[static_cast<int>(index)]; } | 
| John Reck | c87be99 | 2015-02-20 10:57:22 -0800 | [diff] [blame] | 122 |  | 
| John Reck | ba6adf6 | 2015-02-19 14:36:50 -0800 | [diff] [blame] | 123 | int64_t* mBuffer; | 
|  | 124 | }; | 
|  | 125 |  | 
|  | 126 | class FrameInfo { | 
|  | 127 | public: | 
|  | 128 | void importUiThreadInfo(int64_t* info); | 
|  | 129 |  | 
| Jerome Gaillard | e218c69 | 2019-06-14 12:58:57 +0100 | [diff] [blame] | 130 | void markSyncStart() { set(FrameInfoIndex::SyncStart) = systemTime(SYSTEM_TIME_MONOTONIC); } | 
| John Reck | ba6adf6 | 2015-02-19 14:36:50 -0800 | [diff] [blame] | 131 |  | 
|  | 132 | void markIssueDrawCommandsStart() { | 
| Jerome Gaillard | e218c69 | 2019-06-14 12:58:57 +0100 | [diff] [blame] | 133 | set(FrameInfoIndex::IssueDrawCommandsStart) = systemTime(SYSTEM_TIME_MONOTONIC); | 
| John Reck | ba6adf6 | 2015-02-19 14:36:50 -0800 | [diff] [blame] | 134 | } | 
|  | 135 |  | 
| Jerome Gaillard | e218c69 | 2019-06-14 12:58:57 +0100 | [diff] [blame] | 136 | void markSwapBuffers() { set(FrameInfoIndex::SwapBuffers) = systemTime(SYSTEM_TIME_MONOTONIC); } | 
| John Reck | ba6adf6 | 2015-02-19 14:36:50 -0800 | [diff] [blame] | 137 |  | 
| Jorim Jaggi | 71db889 | 2021-02-03 23:19:29 +0100 | [diff] [blame] | 138 | void markSwapBuffersCompleted() { | 
|  | 139 | set(FrameInfoIndex::SwapBuffersCompleted) = systemTime(SYSTEM_TIME_MONOTONIC); | 
|  | 140 | } | 
|  | 141 |  | 
| Jerome Gaillard | e218c69 | 2019-06-14 12:58:57 +0100 | [diff] [blame] | 142 | void markFrameCompleted() { set(FrameInfoIndex::FrameCompleted) = systemTime(SYSTEM_TIME_MONOTONIC); } | 
| John Reck | ba6adf6 | 2015-02-19 14:36:50 -0800 | [diff] [blame] | 143 |  | 
| Chris Craik | 1b54fb2 | 2015-06-02 17:40:58 -0700 | [diff] [blame] | 144 | void addFlag(int frameInfoFlag) { | 
|  | 145 | set(FrameInfoIndex::Flags) |= static_cast<uint64_t>(frameInfoFlag); | 
| John Reck | 240ff62 | 2015-04-28 13:50:00 -0700 | [diff] [blame] | 146 | } | 
|  | 147 |  | 
| John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 148 | const int64_t* data() const { return mFrameInfo; } | 
| Andres Morales | 06f5bc7 | 2015-12-15 15:21:31 -0800 | [diff] [blame] | 149 |  | 
| John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 150 | inline int64_t operator[](FrameInfoIndex index) const { return get(index); } | 
| John Reck | ba6adf6 | 2015-02-19 14:36:50 -0800 | [diff] [blame] | 151 |  | 
| John Reck | 4130027 | 2015-06-03 14:42:34 -0700 | [diff] [blame] | 152 | inline int64_t operator[](int index) const { | 
| Chris Craik | 1b54fb2 | 2015-06-02 17:40:58 -0700 | [diff] [blame] | 153 | if (index < 0 || index >= static_cast<int>(FrameInfoIndex::NumIndexes)) return 0; | 
| John Reck | c87be99 | 2015-02-20 10:57:22 -0800 | [diff] [blame] | 154 | return mFrameInfo[index]; | 
| John Reck | ba6adf6 | 2015-02-19 14:36:50 -0800 | [diff] [blame] | 155 | } | 
|  | 156 |  | 
| John Reck | 4130027 | 2015-06-03 14:42:34 -0700 | [diff] [blame] | 157 | inline int64_t duration(FrameInfoIndex start, FrameInfoIndex end) const { | 
| John Reck | be3fba0 | 2015-07-06 13:49:58 -0700 | [diff] [blame] | 158 | int64_t endtime = get(end); | 
|  | 159 | int64_t starttime = get(start); | 
| John Reck | 4130027 | 2015-06-03 14:42:34 -0700 | [diff] [blame] | 160 | int64_t gap = endtime - starttime; | 
|  | 161 | gap = starttime > 0 ? gap : 0; | 
| John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 162 | if (end > FrameInfoIndex::SyncQueued && start < FrameInfoIndex::SyncQueued) { | 
| John Reck | be3fba0 | 2015-07-06 13:49:58 -0700 | [diff] [blame] | 163 | // Need to subtract out the time spent in a stalled state | 
|  | 164 | // as this will be captured by the previous frame's info | 
| John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 165 | int64_t offset = get(FrameInfoIndex::SyncStart) - get(FrameInfoIndex::SyncQueued); | 
| John Reck | be3fba0 | 2015-07-06 13:49:58 -0700 | [diff] [blame] | 166 | if (offset > 0) { | 
|  | 167 | gap -= offset; | 
|  | 168 | } | 
|  | 169 | } | 
| John Reck | 4130027 | 2015-06-03 14:42:34 -0700 | [diff] [blame] | 170 | return gap > 0 ? gap : 0; | 
|  | 171 | } | 
|  | 172 |  | 
|  | 173 | inline int64_t totalDuration() const { | 
|  | 174 | return duration(FrameInfoIndex::IntendedVsync, FrameInfoIndex::FrameCompleted); | 
|  | 175 | } | 
|  | 176 |  | 
| Stan Iliev | 7203e1f | 2019-07-25 13:12:02 -0400 | [diff] [blame] | 177 | inline int64_t gpuDrawTime() const { | 
|  | 178 | // GPU start time is approximated to the moment before swapBuffer is invoked. | 
|  | 179 | // We could add an EGLSyncKHR fence at the beginning of the frame, but that is an overhead. | 
|  | 180 | int64_t endTime = get(FrameInfoIndex::GpuCompleted); | 
| Boleyn Su | a9ce662 | 2020-11-18 03:15:29 +0000 | [diff] [blame] | 181 | return endTime > 0 ? endTime - get(FrameInfoIndex::SwapBuffers) : -1; | 
| Stan Iliev | 7203e1f | 2019-07-25 13:12:02 -0400 | [diff] [blame] | 182 | } | 
|  | 183 |  | 
| John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 184 | inline int64_t& set(FrameInfoIndex index) { return mFrameInfo[static_cast<int>(index)]; } | 
| John Reck | c87be99 | 2015-02-20 10:57:22 -0800 | [diff] [blame] | 185 |  | 
| John Reck | be3fba0 | 2015-07-06 13:49:58 -0700 | [diff] [blame] | 186 | inline int64_t get(FrameInfoIndex index) const { | 
|  | 187 | if (index == FrameInfoIndex::NumIndexes) return 0; | 
|  | 188 | return mFrameInfo[static_cast<int>(index)]; | 
|  | 189 | } | 
|  | 190 |  | 
| Ady Abraham | e088dcd | 2023-08-10 11:45:58 -0700 | [diff] [blame] | 191 | void setSkippedFrameReason(SkippedFrameReason reason); | 
|  | 192 | inline std::optional<SkippedFrameReason> getSkippedFrameReason() const { | 
|  | 193 | return mSkippedFrameReason; | 
|  | 194 | } | 
|  | 195 |  | 
| John Reck | be3fba0 | 2015-07-06 13:49:58 -0700 | [diff] [blame] | 196 | private: | 
| Chris Craik | 1b54fb2 | 2015-06-02 17:40:58 -0700 | [diff] [blame] | 197 | int64_t mFrameInfo[static_cast<int>(FrameInfoIndex::NumIndexes)]; | 
| Ady Abraham | e088dcd | 2023-08-10 11:45:58 -0700 | [diff] [blame] | 198 | std::optional<SkippedFrameReason> mSkippedFrameReason; | 
| John Reck | ba6adf6 | 2015-02-19 14:36:50 -0800 | [diff] [blame] | 199 | }; | 
|  | 200 |  | 
|  | 201 | } /* namespace uirenderer */ | 
|  | 202 | } /* namespace android */ | 
|  | 203 |  | 
|  | 204 | #endif /* FRAMEINFO_H_ */ |