John Reck | fe5e7b7 | 2014-05-23 17:42:28 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2014 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 | */ |
John Reck | 4c9e59d | 2015-05-12 07:17:50 -0700 | [diff] [blame] | 16 | #include "FrameInfoVisualizer.h" |
John Reck | fe5e7b7 | 2014-05-23 17:42:28 -0700 | [diff] [blame] | 17 | |
| 18 | #include "OpenGLRenderer.h" |
John Reck | fe5e7b7 | 2014-05-23 17:42:28 -0700 | [diff] [blame] | 19 | |
John Reck | 4c9e59d | 2015-05-12 07:17:50 -0700 | [diff] [blame] | 20 | #include <cutils/compiler.h> |
John Reck | bf3c602 | 2015-06-02 15:55:00 -0700 | [diff] [blame] | 21 | #include <array> |
John Reck | fe5e7b7 | 2014-05-23 17:42:28 -0700 | [diff] [blame] | 22 | |
Chris Craik | 2507c34 | 2015-05-04 14:36:49 -0700 | [diff] [blame] | 23 | #define RETURN_IF_PROFILING_DISABLED() if (CC_LIKELY(mType == ProfileType::None)) return |
| 24 | #define RETURN_IF_DISABLED() if (CC_LIKELY(mType == ProfileType::None && !mShowDirtyRegions)) return |
John Reck | fe5e7b7 | 2014-05-23 17:42:28 -0700 | [diff] [blame] | 25 | |
John Reck | fe5e7b7 | 2014-05-23 17:42:28 -0700 | [diff] [blame] | 26 | #define PROFILE_DRAW_WIDTH 3 |
| 27 | #define PROFILE_DRAW_THRESHOLD_STROKE_WIDTH 2 |
| 28 | #define PROFILE_DRAW_DP_PER_MS 7 |
| 29 | |
John Reck | fe5e7b7 | 2014-05-23 17:42:28 -0700 | [diff] [blame] | 30 | // Must be NUM_ELEMENTS in size |
John Reck | fe5e7b7 | 2014-05-23 17:42:28 -0700 | [diff] [blame] | 31 | static const SkColor THRESHOLD_COLOR = 0xff5faa4d; |
John Reck | 4130027 | 2015-06-03 14:42:34 -0700 | [diff] [blame] | 32 | static const SkColor BAR_FAST_ALPHA = 0x8F000000; |
| 33 | static const SkColor BAR_JANKY_ALPHA = 0xDF000000; |
John Reck | fe5e7b7 | 2014-05-23 17:42:28 -0700 | [diff] [blame] | 34 | |
| 35 | // We could get this from TimeLord and use the actual frame interval, but |
| 36 | // this is good enough |
| 37 | #define FRAME_THRESHOLD 16 |
John Reck | 4130027 | 2015-06-03 14:42:34 -0700 | [diff] [blame] | 38 | #define FRAME_THRESHOLD_NS 16000000 |
John Reck | fe5e7b7 | 2014-05-23 17:42:28 -0700 | [diff] [blame] | 39 | |
| 40 | namespace android { |
| 41 | namespace uirenderer { |
| 42 | |
John Reck | bf3c602 | 2015-06-02 15:55:00 -0700 | [diff] [blame] | 43 | struct BarSegment { |
| 44 | FrameInfoIndex start; |
| 45 | FrameInfoIndex end; |
| 46 | SkColor color; |
| 47 | }; |
| 48 | |
John Reck | 4130027 | 2015-06-03 14:42:34 -0700 | [diff] [blame] | 49 | static const std::array<BarSegment,7> Bar {{ |
| 50 | { FrameInfoIndex::IntendedVsync, FrameInfoIndex::HandleInputStart, 0x00796B }, |
| 51 | { FrameInfoIndex::HandleInputStart, FrameInfoIndex::PerformTraversalsStart, 0x388E3C }, |
| 52 | { FrameInfoIndex::PerformTraversalsStart, FrameInfoIndex::DrawStart, 0x689F38}, |
Chris Craik | 1b54fb2 | 2015-06-02 17:40:58 -0700 | [diff] [blame] | 53 | { FrameInfoIndex::DrawStart, FrameInfoIndex::SyncStart, 0x2196F3}, |
| 54 | { FrameInfoIndex::SyncStart, FrameInfoIndex::IssueDrawCommandsStart, 0x4FC3F7}, |
| 55 | { FrameInfoIndex::IssueDrawCommandsStart, FrameInfoIndex::SwapBuffers, 0xF44336}, |
| 56 | { FrameInfoIndex::SwapBuffers, FrameInfoIndex::FrameCompleted, 0xFF9800}, |
John Reck | bf3c602 | 2015-06-02 15:55:00 -0700 | [diff] [blame] | 57 | }}; |
| 58 | |
John Reck | fe5e7b7 | 2014-05-23 17:42:28 -0700 | [diff] [blame] | 59 | static int dpToPx(int dp, float density) { |
| 60 | return (int) (dp * density + 0.5f); |
| 61 | } |
| 62 | |
John Reck | 4c9e59d | 2015-05-12 07:17:50 -0700 | [diff] [blame] | 63 | FrameInfoVisualizer::FrameInfoVisualizer(FrameInfoSource& source) |
| 64 | : mFrameSource(source) { |
John Reck | fe5e7b7 | 2014-05-23 17:42:28 -0700 | [diff] [blame] | 65 | setDensity(1); |
| 66 | } |
| 67 | |
John Reck | 4c9e59d | 2015-05-12 07:17:50 -0700 | [diff] [blame] | 68 | FrameInfoVisualizer::~FrameInfoVisualizer() { |
John Reck | fe5e7b7 | 2014-05-23 17:42:28 -0700 | [diff] [blame] | 69 | destroyData(); |
| 70 | } |
| 71 | |
John Reck | 4c9e59d | 2015-05-12 07:17:50 -0700 | [diff] [blame] | 72 | void FrameInfoVisualizer::setDensity(float density) { |
John Reck | fe5e7b7 | 2014-05-23 17:42:28 -0700 | [diff] [blame] | 73 | if (CC_UNLIKELY(mDensity != density)) { |
| 74 | mDensity = density; |
| 75 | mVerticalUnit = dpToPx(PROFILE_DRAW_DP_PER_MS, density); |
John Reck | fe5e7b7 | 2014-05-23 17:42:28 -0700 | [diff] [blame] | 76 | mThresholdStroke = dpToPx(PROFILE_DRAW_THRESHOLD_STROKE_WIDTH, density); |
| 77 | } |
| 78 | } |
| 79 | |
John Reck | 4c9e59d | 2015-05-12 07:17:50 -0700 | [diff] [blame] | 80 | void FrameInfoVisualizer::unionDirty(SkRect* dirty) { |
John Reck | fe5e7b7 | 2014-05-23 17:42:28 -0700 | [diff] [blame] | 81 | RETURN_IF_DISABLED(); |
| 82 | // Not worth worrying about minimizing the dirty region for debugging, so just |
| 83 | // dirty the entire viewport. |
| 84 | if (dirty) { |
John Reck | 23d307c | 2014-10-27 12:38:48 -0700 | [diff] [blame] | 85 | mDirtyRegion = *dirty; |
John Reck | fe5e7b7 | 2014-05-23 17:42:28 -0700 | [diff] [blame] | 86 | dirty->setEmpty(); |
| 87 | } |
| 88 | } |
| 89 | |
John Reck | 4c9e59d | 2015-05-12 07:17:50 -0700 | [diff] [blame] | 90 | void FrameInfoVisualizer::draw(OpenGLRenderer* canvas) { |
John Reck | 23d307c | 2014-10-27 12:38:48 -0700 | [diff] [blame] | 91 | RETURN_IF_DISABLED(); |
| 92 | |
| 93 | if (mShowDirtyRegions) { |
| 94 | mFlashToggle = !mFlashToggle; |
| 95 | if (mFlashToggle) { |
| 96 | SkPaint paint; |
| 97 | paint.setColor(0x7fff0000); |
| 98 | canvas->drawRect(mDirtyRegion.fLeft, mDirtyRegion.fTop, |
| 99 | mDirtyRegion.fRight, mDirtyRegion.fBottom, &paint); |
| 100 | } |
John Reck | fe5e7b7 | 2014-05-23 17:42:28 -0700 | [diff] [blame] | 101 | } |
| 102 | |
Chris Craik | 2507c34 | 2015-05-04 14:36:49 -0700 | [diff] [blame] | 103 | if (mType == ProfileType::Bars) { |
John Reck | 4130027 | 2015-06-03 14:42:34 -0700 | [diff] [blame] | 104 | // Patch up the current frame to pretend we ended here. CanvasContext |
| 105 | // will overwrite these values with the real ones after we return. |
| 106 | // This is a bit nicer looking than the vague green bar, as we have |
| 107 | // valid data for almost all the stages and a very good idea of what |
| 108 | // the issue stage will look like, too |
| 109 | FrameInfo& info = mFrameSource.back(); |
| 110 | info.markSwapBuffers(); |
| 111 | info.markFrameCompleted(); |
| 112 | |
| 113 | initializeRects(canvas->getViewportHeight(), canvas->getViewportWidth()); |
John Reck | 23d307c | 2014-10-27 12:38:48 -0700 | [diff] [blame] | 114 | drawGraph(canvas); |
John Reck | 23d307c | 2014-10-27 12:38:48 -0700 | [diff] [blame] | 115 | drawThreshold(canvas); |
| 116 | } |
John Reck | fe5e7b7 | 2014-05-23 17:42:28 -0700 | [diff] [blame] | 117 | } |
| 118 | |
John Reck | 4c9e59d | 2015-05-12 07:17:50 -0700 | [diff] [blame] | 119 | void FrameInfoVisualizer::createData() { |
John Reck | 4130027 | 2015-06-03 14:42:34 -0700 | [diff] [blame] | 120 | if (mFastRects.get()) return; |
John Reck | fe5e7b7 | 2014-05-23 17:42:28 -0700 | [diff] [blame] | 121 | |
John Reck | 4130027 | 2015-06-03 14:42:34 -0700 | [diff] [blame] | 122 | mFastRects.reset(new float[mFrameSource.capacity() * 4]); |
| 123 | mJankyRects.reset(new float[mFrameSource.capacity() * 4]); |
John Reck | fe5e7b7 | 2014-05-23 17:42:28 -0700 | [diff] [blame] | 124 | } |
| 125 | |
John Reck | 4c9e59d | 2015-05-12 07:17:50 -0700 | [diff] [blame] | 126 | void FrameInfoVisualizer::destroyData() { |
John Reck | 4130027 | 2015-06-03 14:42:34 -0700 | [diff] [blame] | 127 | mFastRects.reset(nullptr); |
| 128 | mJankyRects.reset(nullptr); |
John Reck | fe5e7b7 | 2014-05-23 17:42:28 -0700 | [diff] [blame] | 129 | } |
| 130 | |
John Reck | 4130027 | 2015-06-03 14:42:34 -0700 | [diff] [blame] | 131 | void FrameInfoVisualizer::initializeRects(const int baseline, const int width) { |
| 132 | // Target the 95% mark for the current frame |
| 133 | float right = width * .95; |
| 134 | float baseLineWidth = right / mFrameSource.capacity(); |
| 135 | mNumFastRects = 0; |
| 136 | mNumJankyRects = 0; |
| 137 | int fast_i = 0, janky_i = 0; |
John Reck | bf3c602 | 2015-06-02 15:55:00 -0700 | [diff] [blame] | 138 | // Set the bottom of all the shapes to the baseline |
John Reck | 4130027 | 2015-06-03 14:42:34 -0700 | [diff] [blame] | 139 | for (int fi = mFrameSource.size() - 1; fi >= 0; fi--) { |
| 140 | if (mFrameSource[fi][FrameInfoIndex::Flags] & FrameInfoFlags::SkippedFrame) { |
| 141 | continue; |
| 142 | } |
| 143 | float lineWidth = baseLineWidth; |
| 144 | float* rect; |
| 145 | int ri; |
John Reck | bf3c602 | 2015-06-02 15:55:00 -0700 | [diff] [blame] | 146 | // Rects are LTRB |
John Reck | 4130027 | 2015-06-03 14:42:34 -0700 | [diff] [blame] | 147 | if (mFrameSource[fi].totalDuration() <= FRAME_THRESHOLD_NS) { |
| 148 | rect = mFastRects.get(); |
| 149 | ri = fast_i; |
| 150 | fast_i += 4; |
| 151 | mNumFastRects++; |
| 152 | } else { |
| 153 | rect = mJankyRects.get(); |
| 154 | ri = janky_i; |
| 155 | janky_i += 4; |
| 156 | mNumJankyRects++; |
| 157 | lineWidth *= 2; |
| 158 | } |
| 159 | |
| 160 | rect[ri + 0] = right - lineWidth; |
| 161 | rect[ri + 1] = baseline; |
| 162 | rect[ri + 2] = right; |
| 163 | rect[ri + 3] = baseline; |
| 164 | right -= lineWidth; |
John Reck | bf3c602 | 2015-06-02 15:55:00 -0700 | [diff] [blame] | 165 | } |
John Reck | fe5e7b7 | 2014-05-23 17:42:28 -0700 | [diff] [blame] | 166 | } |
| 167 | |
John Reck | bf3c602 | 2015-06-02 15:55:00 -0700 | [diff] [blame] | 168 | void FrameInfoVisualizer::nextBarSegment(FrameInfoIndex start, FrameInfoIndex end) { |
John Reck | 4130027 | 2015-06-03 14:42:34 -0700 | [diff] [blame] | 169 | int fast_i = (mNumFastRects - 1) * 4; |
| 170 | int janky_i = (mNumJankyRects - 1) * 4;; |
| 171 | for (size_t fi = 0; fi < mFrameSource.size(); fi++) { |
Chris Craik | 1b54fb2 | 2015-06-02 17:40:58 -0700 | [diff] [blame] | 172 | if (mFrameSource[fi][FrameInfoIndex::Flags] & FrameInfoFlags::SkippedFrame) { |
John Reck | bf3c602 | 2015-06-02 15:55:00 -0700 | [diff] [blame] | 173 | continue; |
| 174 | } |
| 175 | |
John Reck | 4130027 | 2015-06-03 14:42:34 -0700 | [diff] [blame] | 176 | float* rect; |
| 177 | int ri; |
| 178 | // Rects are LTRB |
| 179 | if (mFrameSource[fi].totalDuration() <= FRAME_THRESHOLD_NS) { |
| 180 | rect = mFastRects.get(); |
| 181 | ri = fast_i; |
| 182 | fast_i -= 4; |
| 183 | } else { |
| 184 | rect = mJankyRects.get(); |
| 185 | ri = janky_i; |
| 186 | janky_i -= 4; |
| 187 | } |
| 188 | |
John Reck | bf3c602 | 2015-06-02 15:55:00 -0700 | [diff] [blame] | 189 | // Set the bottom to the old top (build upwards) |
John Reck | 4130027 | 2015-06-03 14:42:34 -0700 | [diff] [blame] | 190 | rect[ri + 3] = rect[ri + 1]; |
John Reck | bf3c602 | 2015-06-02 15:55:00 -0700 | [diff] [blame] | 191 | // Move the top up by the duration |
John Reck | 4130027 | 2015-06-03 14:42:34 -0700 | [diff] [blame] | 192 | rect[ri + 1] -= mVerticalUnit * duration(fi, start, end); |
John Reck | fe5e7b7 | 2014-05-23 17:42:28 -0700 | [diff] [blame] | 193 | } |
| 194 | } |
| 195 | |
John Reck | 4c9e59d | 2015-05-12 07:17:50 -0700 | [diff] [blame] | 196 | void FrameInfoVisualizer::drawGraph(OpenGLRenderer* canvas) { |
John Reck | fe5e7b7 | 2014-05-23 17:42:28 -0700 | [diff] [blame] | 197 | SkPaint paint; |
John Reck | bf3c602 | 2015-06-02 15:55:00 -0700 | [diff] [blame] | 198 | for (size_t i = 0; i < Bar.size(); i++) { |
John Reck | bf3c602 | 2015-06-02 15:55:00 -0700 | [diff] [blame] | 199 | nextBarSegment(Bar[i].start, Bar[i].end); |
John Reck | 4130027 | 2015-06-03 14:42:34 -0700 | [diff] [blame] | 200 | paint.setColor(Bar[i].color | BAR_FAST_ALPHA); |
| 201 | canvas->drawRects(mFastRects.get(), mNumFastRects * 4, &paint); |
| 202 | paint.setColor(Bar[i].color | BAR_JANKY_ALPHA); |
| 203 | canvas->drawRects(mJankyRects.get(), mNumJankyRects * 4, &paint); |
John Reck | fe5e7b7 | 2014-05-23 17:42:28 -0700 | [diff] [blame] | 204 | } |
| 205 | } |
| 206 | |
John Reck | 4c9e59d | 2015-05-12 07:17:50 -0700 | [diff] [blame] | 207 | void FrameInfoVisualizer::drawThreshold(OpenGLRenderer* canvas) { |
John Reck | fe5e7b7 | 2014-05-23 17:42:28 -0700 | [diff] [blame] | 208 | SkPaint paint; |
| 209 | paint.setColor(THRESHOLD_COLOR); |
| 210 | paint.setStrokeWidth(mThresholdStroke); |
| 211 | |
| 212 | float pts[4]; |
| 213 | pts[0] = 0.0f; |
| 214 | pts[1] = pts[3] = canvas->getViewportHeight() - (FRAME_THRESHOLD * mVerticalUnit); |
| 215 | pts[2] = canvas->getViewportWidth(); |
| 216 | canvas->drawLines(pts, 4, &paint); |
| 217 | } |
| 218 | |
John Reck | 4c9e59d | 2015-05-12 07:17:50 -0700 | [diff] [blame] | 219 | bool FrameInfoVisualizer::consumeProperties() { |
John Reck | 23d307c | 2014-10-27 12:38:48 -0700 | [diff] [blame] | 220 | bool changed = false; |
Chris Craik | 2507c34 | 2015-05-04 14:36:49 -0700 | [diff] [blame] | 221 | ProfileType newType = Properties::getProfileType(); |
John Reck | fe5e7b7 | 2014-05-23 17:42:28 -0700 | [diff] [blame] | 222 | if (newType != mType) { |
| 223 | mType = newType; |
Chris Craik | 2507c34 | 2015-05-04 14:36:49 -0700 | [diff] [blame] | 224 | if (mType == ProfileType::None) { |
John Reck | fe5e7b7 | 2014-05-23 17:42:28 -0700 | [diff] [blame] | 225 | destroyData(); |
| 226 | } else { |
| 227 | createData(); |
| 228 | } |
John Reck | 23d307c | 2014-10-27 12:38:48 -0700 | [diff] [blame] | 229 | changed = true; |
John Reck | fe5e7b7 | 2014-05-23 17:42:28 -0700 | [diff] [blame] | 230 | } |
Chris Craik | 2507c34 | 2015-05-04 14:36:49 -0700 | [diff] [blame] | 231 | |
| 232 | bool showDirty = Properties::showDirtyRegions; |
John Reck | 23d307c | 2014-10-27 12:38:48 -0700 | [diff] [blame] | 233 | if (showDirty != mShowDirtyRegions) { |
| 234 | mShowDirtyRegions = showDirty; |
| 235 | changed = true; |
| 236 | } |
| 237 | return changed; |
John Reck | fe5e7b7 | 2014-05-23 17:42:28 -0700 | [diff] [blame] | 238 | } |
| 239 | |
John Reck | 4c9e59d | 2015-05-12 07:17:50 -0700 | [diff] [blame] | 240 | void FrameInfoVisualizer::dumpData(int fd) { |
John Reck | 23d307c | 2014-10-27 12:38:48 -0700 | [diff] [blame] | 241 | RETURN_IF_PROFILING_DISABLED(); |
John Reck | fe5e7b7 | 2014-05-23 17:42:28 -0700 | [diff] [blame] | 242 | |
| 243 | // This method logs the last N frames (where N is <= mDataSize) since the |
| 244 | // last call to dumpData(). In other words if there's a dumpData(), draw frame, |
| 245 | // dumpData(), the last dumpData() should only log 1 frame. |
| 246 | |
John Reck | fe5e7b7 | 2014-05-23 17:42:28 -0700 | [diff] [blame] | 247 | FILE *file = fdopen(fd, "a"); |
| 248 | fprintf(file, "\n\tDraw\tPrepare\tProcess\tExecute\n"); |
| 249 | |
John Reck | 4c9e59d | 2015-05-12 07:17:50 -0700 | [diff] [blame] | 250 | for (size_t i = 0; i < mFrameSource.size(); i++) { |
Chris Craik | 1b54fb2 | 2015-06-02 17:40:58 -0700 | [diff] [blame] | 251 | if (mFrameSource[i][FrameInfoIndex::IntendedVsync] <= mLastFrameLogged) { |
John Reck | fe5e7b7 | 2014-05-23 17:42:28 -0700 | [diff] [blame] | 252 | continue; |
| 253 | } |
Chris Craik | 1b54fb2 | 2015-06-02 17:40:58 -0700 | [diff] [blame] | 254 | mLastFrameLogged = mFrameSource[i][FrameInfoIndex::IntendedVsync]; |
John Reck | fe5e7b7 | 2014-05-23 17:42:28 -0700 | [diff] [blame] | 255 | fprintf(file, "\t%3.2f\t%3.2f\t%3.2f\t%3.2f\n", |
Chris Craik | 1b54fb2 | 2015-06-02 17:40:58 -0700 | [diff] [blame] | 256 | duration(i, FrameInfoIndex::IntendedVsync, FrameInfoIndex::SyncStart), |
| 257 | duration(i, FrameInfoIndex::SyncStart, FrameInfoIndex::IssueDrawCommandsStart), |
| 258 | duration(i, FrameInfoIndex::IssueDrawCommandsStart, FrameInfoIndex::SwapBuffers), |
| 259 | duration(i, FrameInfoIndex::SwapBuffers, FrameInfoIndex::FrameCompleted)); |
John Reck | fe5e7b7 | 2014-05-23 17:42:28 -0700 | [diff] [blame] | 260 | } |
John Reck | fe5e7b7 | 2014-05-23 17:42:28 -0700 | [diff] [blame] | 261 | |
| 262 | fflush(file); |
| 263 | } |
| 264 | |
| 265 | } /* namespace uirenderer */ |
| 266 | } /* namespace android */ |