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