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 | |
| 19 | #include "utils/Macros.h" |
| 20 | |
| 21 | #include <cutils/compiler.h> |
| 22 | #include <utils/Timers.h> |
| 23 | |
| 24 | #include <memory.h> |
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 | |
| 27 | namespace android { |
| 28 | namespace uirenderer { |
| 29 | |
Steven Thomas | 6fabb5a | 2020-08-21 16:56:08 -0700 | [diff] [blame^] | 30 | #define UI_THREAD_FRAME_INFO_SIZE 10 |
John Reck | ba6adf6 | 2015-02-19 14:36:50 -0800 | [diff] [blame] | 31 | |
John Reck | c87be99 | 2015-02-20 10:57:22 -0800 | [diff] [blame] | 32 | enum class FrameInfoIndex { |
Chris Craik | 1b54fb2 | 2015-06-02 17:40:58 -0700 | [diff] [blame] | 33 | Flags = 0, |
Steven Thomas | 6fabb5a | 2020-08-21 16:56:08 -0700 | [diff] [blame^] | 34 | FrameTimelineVsyncId, |
Chris Craik | 1b54fb2 | 2015-06-02 17:40:58 -0700 | [diff] [blame] | 35 | IntendedVsync, |
| 36 | Vsync, |
| 37 | OldestInputEvent, |
| 38 | NewestInputEvent, |
| 39 | HandleInputStart, |
| 40 | AnimationStart, |
| 41 | PerformTraversalsStart, |
| 42 | DrawStart, |
John Reck | ba6adf6 | 2015-02-19 14:36:50 -0800 | [diff] [blame] | 43 | // End of UI frame info |
| 44 | |
John Reck | be3fba0 | 2015-07-06 13:49:58 -0700 | [diff] [blame] | 45 | SyncQueued, |
| 46 | |
Chris Craik | 1b54fb2 | 2015-06-02 17:40:58 -0700 | [diff] [blame] | 47 | SyncStart, |
| 48 | IssueDrawCommandsStart, |
| 49 | SwapBuffers, |
| 50 | FrameCompleted, |
John Reck | ba6adf6 | 2015-02-19 14:36:50 -0800 | [diff] [blame] | 51 | |
John Reck | 2d5b8d7 | 2016-07-28 15:36:11 -0700 | [diff] [blame] | 52 | DequeueBufferDuration, |
| 53 | QueueBufferDuration, |
| 54 | |
Stan Iliev | 7203e1f | 2019-07-25 13:12:02 -0400 | [diff] [blame] | 55 | GpuCompleted, |
| 56 | |
John Reck | ba6adf6 | 2015-02-19 14:36:50 -0800 | [diff] [blame] | 57 | // Must be the last value! |
John Reck | 65ddb15 | 2016-08-02 09:38:26 -0700 | [diff] [blame] | 58 | // Also must be kept in sync with FrameMetrics.java#FRAME_STATS_COUNT |
Chris Craik | 1b54fb2 | 2015-06-02 17:40:58 -0700 | [diff] [blame] | 59 | NumIndexes |
John Reck | c87be99 | 2015-02-20 10:57:22 -0800 | [diff] [blame] | 60 | }; |
John Reck | ba6adf6 | 2015-02-19 14:36:50 -0800 | [diff] [blame] | 61 | |
John Reck | 2a8bb05 | 2015-06-03 09:52:01 -0700 | [diff] [blame] | 62 | extern const std::string FrameInfoNames[]; |
John Reck | 4db3d17 | 2015-06-02 15:58:43 -0700 | [diff] [blame] | 63 | |
Chris Craik | 1b54fb2 | 2015-06-02 17:40:58 -0700 | [diff] [blame] | 64 | namespace FrameInfoFlags { |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 65 | enum { |
| 66 | WindowLayoutChanged = 1 << 0, |
| 67 | RTAnimation = 1 << 1, |
| 68 | SurfaceCanvas = 1 << 2, |
| 69 | SkippedFrame = 1 << 3, |
| 70 | }; |
John Reck | c87be99 | 2015-02-20 10:57:22 -0800 | [diff] [blame] | 71 | }; |
John Reck | ba6adf6 | 2015-02-19 14:36:50 -0800 | [diff] [blame] | 72 | |
Derek Sollenberger | 3fedf5a | 2020-02-21 13:07:28 -0500 | [diff] [blame] | 73 | class UiFrameInfoBuilder { |
John Reck | ba6adf6 | 2015-02-19 14:36:50 -0800 | [diff] [blame] | 74 | public: |
Steven Thomas | 6fabb5a | 2020-08-21 16:56:08 -0700 | [diff] [blame^] | 75 | static constexpr int64_t INVALID_VSYNC_ID = -1; |
| 76 | |
Chih-Hung Hsieh | faecb78 | 2016-07-21 11:23:06 -0700 | [diff] [blame] | 77 | explicit UiFrameInfoBuilder(int64_t* buffer) : mBuffer(buffer) { |
John Reck | ba6adf6 | 2015-02-19 14:36:50 -0800 | [diff] [blame] | 78 | memset(mBuffer, 0, UI_THREAD_FRAME_INFO_SIZE * sizeof(int64_t)); |
Steven Thomas | 6fabb5a | 2020-08-21 16:56:08 -0700 | [diff] [blame^] | 79 | set(FrameInfoIndex::FrameTimelineVsyncId) = INVALID_VSYNC_ID; |
John Reck | ba6adf6 | 2015-02-19 14:36:50 -0800 | [diff] [blame] | 80 | } |
| 81 | |
Steven Thomas | 6fabb5a | 2020-08-21 16:56:08 -0700 | [diff] [blame^] | 82 | UiFrameInfoBuilder& setVsync(nsecs_t vsyncTime, nsecs_t intendedVsync, int64_t vsyncId) { |
| 83 | set(FrameInfoIndex::FrameTimelineVsyncId) = vsyncId; |
Chris Craik | 1b54fb2 | 2015-06-02 17:40:58 -0700 | [diff] [blame] | 84 | set(FrameInfoIndex::Vsync) = vsyncTime; |
| 85 | set(FrameInfoIndex::IntendedVsync) = intendedVsync; |
John Reck | bf3c602 | 2015-06-02 15:55:00 -0700 | [diff] [blame] | 86 | // Pretend the other fields are all at vsync, too, so that naive |
| 87 | // duration calculations end up being 0 instead of very large |
Chris Craik | 1b54fb2 | 2015-06-02 17:40:58 -0700 | [diff] [blame] | 88 | set(FrameInfoIndex::HandleInputStart) = vsyncTime; |
| 89 | set(FrameInfoIndex::AnimationStart) = vsyncTime; |
| 90 | set(FrameInfoIndex::PerformTraversalsStart) = vsyncTime; |
| 91 | set(FrameInfoIndex::DrawStart) = vsyncTime; |
John Reck | ba6adf6 | 2015-02-19 14:36:50 -0800 | [diff] [blame] | 92 | return *this; |
| 93 | } |
| 94 | |
Chris Craik | 1b54fb2 | 2015-06-02 17:40:58 -0700 | [diff] [blame] | 95 | UiFrameInfoBuilder& addFlag(int frameInfoFlag) { |
| 96 | set(FrameInfoIndex::Flags) |= static_cast<uint64_t>(frameInfoFlag); |
John Reck | ba6adf6 | 2015-02-19 14:36:50 -0800 | [diff] [blame] | 97 | return *this; |
| 98 | } |
| 99 | |
| 100 | private: |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 101 | inline int64_t& set(FrameInfoIndex index) { return mBuffer[static_cast<int>(index)]; } |
John Reck | c87be99 | 2015-02-20 10:57:22 -0800 | [diff] [blame] | 102 | |
John Reck | ba6adf6 | 2015-02-19 14:36:50 -0800 | [diff] [blame] | 103 | int64_t* mBuffer; |
| 104 | }; |
| 105 | |
| 106 | class FrameInfo { |
| 107 | public: |
| 108 | void importUiThreadInfo(int64_t* info); |
| 109 | |
Jerome Gaillard | e218c69 | 2019-06-14 12:58:57 +0100 | [diff] [blame] | 110 | void markSyncStart() { set(FrameInfoIndex::SyncStart) = systemTime(SYSTEM_TIME_MONOTONIC); } |
John Reck | ba6adf6 | 2015-02-19 14:36:50 -0800 | [diff] [blame] | 111 | |
| 112 | void markIssueDrawCommandsStart() { |
Jerome Gaillard | e218c69 | 2019-06-14 12:58:57 +0100 | [diff] [blame] | 113 | set(FrameInfoIndex::IssueDrawCommandsStart) = systemTime(SYSTEM_TIME_MONOTONIC); |
John Reck | ba6adf6 | 2015-02-19 14:36:50 -0800 | [diff] [blame] | 114 | } |
| 115 | |
Jerome Gaillard | e218c69 | 2019-06-14 12:58:57 +0100 | [diff] [blame] | 116 | void markSwapBuffers() { set(FrameInfoIndex::SwapBuffers) = systemTime(SYSTEM_TIME_MONOTONIC); } |
John Reck | ba6adf6 | 2015-02-19 14:36:50 -0800 | [diff] [blame] | 117 | |
Jerome Gaillard | e218c69 | 2019-06-14 12:58:57 +0100 | [diff] [blame] | 118 | void markFrameCompleted() { set(FrameInfoIndex::FrameCompleted) = systemTime(SYSTEM_TIME_MONOTONIC); } |
John Reck | ba6adf6 | 2015-02-19 14:36:50 -0800 | [diff] [blame] | 119 | |
Chris Craik | 1b54fb2 | 2015-06-02 17:40:58 -0700 | [diff] [blame] | 120 | void addFlag(int frameInfoFlag) { |
| 121 | set(FrameInfoIndex::Flags) |= static_cast<uint64_t>(frameInfoFlag); |
John Reck | 240ff62 | 2015-04-28 13:50:00 -0700 | [diff] [blame] | 122 | } |
| 123 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 124 | const int64_t* data() const { return mFrameInfo; } |
Andres Morales | 06f5bc7 | 2015-12-15 15:21:31 -0800 | [diff] [blame] | 125 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 126 | inline int64_t operator[](FrameInfoIndex index) const { return get(index); } |
John Reck | ba6adf6 | 2015-02-19 14:36:50 -0800 | [diff] [blame] | 127 | |
John Reck | 4130027 | 2015-06-03 14:42:34 -0700 | [diff] [blame] | 128 | inline int64_t operator[](int index) const { |
Chris Craik | 1b54fb2 | 2015-06-02 17:40:58 -0700 | [diff] [blame] | 129 | if (index < 0 || index >= static_cast<int>(FrameInfoIndex::NumIndexes)) return 0; |
John Reck | c87be99 | 2015-02-20 10:57:22 -0800 | [diff] [blame] | 130 | return mFrameInfo[index]; |
John Reck | ba6adf6 | 2015-02-19 14:36:50 -0800 | [diff] [blame] | 131 | } |
| 132 | |
John Reck | 4130027 | 2015-06-03 14:42:34 -0700 | [diff] [blame] | 133 | inline int64_t duration(FrameInfoIndex start, FrameInfoIndex end) const { |
John Reck | be3fba0 | 2015-07-06 13:49:58 -0700 | [diff] [blame] | 134 | int64_t endtime = get(end); |
| 135 | int64_t starttime = get(start); |
John Reck | 4130027 | 2015-06-03 14:42:34 -0700 | [diff] [blame] | 136 | int64_t gap = endtime - starttime; |
| 137 | gap = starttime > 0 ? gap : 0; |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 138 | if (end > FrameInfoIndex::SyncQueued && start < FrameInfoIndex::SyncQueued) { |
John Reck | be3fba0 | 2015-07-06 13:49:58 -0700 | [diff] [blame] | 139 | // Need to subtract out the time spent in a stalled state |
| 140 | // as this will be captured by the previous frame's info |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 141 | int64_t offset = get(FrameInfoIndex::SyncStart) - get(FrameInfoIndex::SyncQueued); |
John Reck | be3fba0 | 2015-07-06 13:49:58 -0700 | [diff] [blame] | 142 | if (offset > 0) { |
| 143 | gap -= offset; |
| 144 | } |
| 145 | } |
John Reck | 4130027 | 2015-06-03 14:42:34 -0700 | [diff] [blame] | 146 | return gap > 0 ? gap : 0; |
| 147 | } |
| 148 | |
| 149 | inline int64_t totalDuration() const { |
| 150 | return duration(FrameInfoIndex::IntendedVsync, FrameInfoIndex::FrameCompleted); |
| 151 | } |
| 152 | |
Stan Iliev | 7203e1f | 2019-07-25 13:12:02 -0400 | [diff] [blame] | 153 | inline int64_t gpuDrawTime() const { |
| 154 | // GPU start time is approximated to the moment before swapBuffer is invoked. |
| 155 | // We could add an EGLSyncKHR fence at the beginning of the frame, but that is an overhead. |
| 156 | int64_t endTime = get(FrameInfoIndex::GpuCompleted); |
| 157 | return endTime > 0 ? endTime - get(FrameInfoIndex::SwapBuffers) : -1; |
| 158 | } |
| 159 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 160 | inline int64_t& set(FrameInfoIndex index) { return mFrameInfo[static_cast<int>(index)]; } |
John Reck | c87be99 | 2015-02-20 10:57:22 -0800 | [diff] [blame] | 161 | |
John Reck | be3fba0 | 2015-07-06 13:49:58 -0700 | [diff] [blame] | 162 | inline int64_t get(FrameInfoIndex index) const { |
| 163 | if (index == FrameInfoIndex::NumIndexes) return 0; |
| 164 | return mFrameInfo[static_cast<int>(index)]; |
| 165 | } |
| 166 | |
| 167 | private: |
Chris Craik | 1b54fb2 | 2015-06-02 17:40:58 -0700 | [diff] [blame] | 168 | int64_t mFrameInfo[static_cast<int>(FrameInfoIndex::NumIndexes)]; |
John Reck | ba6adf6 | 2015-02-19 14:36:50 -0800 | [diff] [blame] | 169 | }; |
| 170 | |
| 171 | } /* namespace uirenderer */ |
| 172 | } /* namespace android */ |
| 173 | |
| 174 | #endif /* FRAMEINFO_H_ */ |