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 | fe5e7b7 | 2014-05-23 17:42:28 -0700 | [diff] [blame] | 21 | |
Chris Craik | 2507c34 | 2015-05-04 14:36:49 -0700 | [diff] [blame] | 22 | #define RETURN_IF_PROFILING_DISABLED() if (CC_LIKELY(mType == ProfileType::None)) return |
| 23 | #define RETURN_IF_DISABLED() if (CC_LIKELY(mType == ProfileType::None && !mShowDirtyRegions)) return |
John Reck | fe5e7b7 | 2014-05-23 17:42:28 -0700 | [diff] [blame] | 24 | |
John Reck | fe5e7b7 | 2014-05-23 17:42:28 -0700 | [diff] [blame] | 25 | #define PROFILE_DRAW_WIDTH 3 |
| 26 | #define PROFILE_DRAW_THRESHOLD_STROKE_WIDTH 2 |
| 27 | #define PROFILE_DRAW_DP_PER_MS 7 |
| 28 | |
| 29 | // Number of floats we want to display from FrameTimingData |
| 30 | // If this is changed make sure to update the indexes below |
| 31 | #define NUM_ELEMENTS 4 |
| 32 | |
| 33 | #define RECORD_INDEX 0 |
| 34 | #define PREPARE_INDEX 1 |
| 35 | #define PLAYBACK_INDEX 2 |
| 36 | #define SWAPBUFFERS_INDEX 3 |
| 37 | |
| 38 | // Must be NUM_ELEMENTS in size |
| 39 | static const SkColor ELEMENT_COLORS[] = { 0xcf3e66cc, 0xcf8f00ff, 0xcfdc3912, 0xcfe69800 }; |
| 40 | static const SkColor CURRENT_FRAME_COLOR = 0xcf5faa4d; |
| 41 | static const SkColor THRESHOLD_COLOR = 0xff5faa4d; |
| 42 | |
| 43 | // We could get this from TimeLord and use the actual frame interval, but |
| 44 | // this is good enough |
| 45 | #define FRAME_THRESHOLD 16 |
| 46 | |
| 47 | namespace android { |
| 48 | namespace uirenderer { |
| 49 | |
| 50 | static int dpToPx(int dp, float density) { |
| 51 | return (int) (dp * density + 0.5f); |
| 52 | } |
| 53 | |
John Reck | 4c9e59d | 2015-05-12 07:17:50 -0700 | [diff] [blame] | 54 | FrameInfoVisualizer::FrameInfoVisualizer(FrameInfoSource& source) |
| 55 | : mFrameSource(source) { |
John Reck | fe5e7b7 | 2014-05-23 17:42:28 -0700 | [diff] [blame] | 56 | setDensity(1); |
| 57 | } |
| 58 | |
John Reck | 4c9e59d | 2015-05-12 07:17:50 -0700 | [diff] [blame] | 59 | FrameInfoVisualizer::~FrameInfoVisualizer() { |
John Reck | fe5e7b7 | 2014-05-23 17:42:28 -0700 | [diff] [blame] | 60 | destroyData(); |
| 61 | } |
| 62 | |
John Reck | 4c9e59d | 2015-05-12 07:17:50 -0700 | [diff] [blame] | 63 | void FrameInfoVisualizer::setDensity(float density) { |
John Reck | fe5e7b7 | 2014-05-23 17:42:28 -0700 | [diff] [blame] | 64 | if (CC_UNLIKELY(mDensity != density)) { |
| 65 | mDensity = density; |
| 66 | mVerticalUnit = dpToPx(PROFILE_DRAW_DP_PER_MS, density); |
| 67 | mHorizontalUnit = dpToPx(PROFILE_DRAW_WIDTH, density); |
| 68 | mThresholdStroke = dpToPx(PROFILE_DRAW_THRESHOLD_STROKE_WIDTH, density); |
| 69 | } |
| 70 | } |
| 71 | |
John Reck | 4c9e59d | 2015-05-12 07:17:50 -0700 | [diff] [blame] | 72 | void FrameInfoVisualizer::unionDirty(SkRect* dirty) { |
John Reck | fe5e7b7 | 2014-05-23 17:42:28 -0700 | [diff] [blame] | 73 | RETURN_IF_DISABLED(); |
| 74 | // Not worth worrying about minimizing the dirty region for debugging, so just |
| 75 | // dirty the entire viewport. |
| 76 | if (dirty) { |
John Reck | 23d307c | 2014-10-27 12:38:48 -0700 | [diff] [blame] | 77 | mDirtyRegion = *dirty; |
John Reck | fe5e7b7 | 2014-05-23 17:42:28 -0700 | [diff] [blame] | 78 | dirty->setEmpty(); |
| 79 | } |
| 80 | } |
| 81 | |
John Reck | 4c9e59d | 2015-05-12 07:17:50 -0700 | [diff] [blame] | 82 | void FrameInfoVisualizer::draw(OpenGLRenderer* canvas) { |
John Reck | 23d307c | 2014-10-27 12:38:48 -0700 | [diff] [blame] | 83 | RETURN_IF_DISABLED(); |
| 84 | |
| 85 | if (mShowDirtyRegions) { |
| 86 | mFlashToggle = !mFlashToggle; |
| 87 | if (mFlashToggle) { |
| 88 | SkPaint paint; |
| 89 | paint.setColor(0x7fff0000); |
| 90 | canvas->drawRect(mDirtyRegion.fLeft, mDirtyRegion.fTop, |
| 91 | mDirtyRegion.fRight, mDirtyRegion.fBottom, &paint); |
| 92 | } |
John Reck | fe5e7b7 | 2014-05-23 17:42:28 -0700 | [diff] [blame] | 93 | } |
| 94 | |
Chris Craik | 2507c34 | 2015-05-04 14:36:49 -0700 | [diff] [blame] | 95 | if (mType == ProfileType::Bars) { |
John Reck | 23d307c | 2014-10-27 12:38:48 -0700 | [diff] [blame] | 96 | prepareShapes(canvas->getViewportHeight()); |
| 97 | drawGraph(canvas); |
| 98 | drawCurrentFrame(canvas); |
| 99 | drawThreshold(canvas); |
| 100 | } |
John Reck | fe5e7b7 | 2014-05-23 17:42:28 -0700 | [diff] [blame] | 101 | } |
| 102 | |
John Reck | 4c9e59d | 2015-05-12 07:17:50 -0700 | [diff] [blame] | 103 | void FrameInfoVisualizer::createData() { |
| 104 | if (mRects.get()) return; |
John Reck | fe5e7b7 | 2014-05-23 17:42:28 -0700 | [diff] [blame] | 105 | |
John Reck | 4c9e59d | 2015-05-12 07:17:50 -0700 | [diff] [blame] | 106 | mRects.reset(new float*[mFrameSource.capacity()]); |
John Reck | fe5e7b7 | 2014-05-23 17:42:28 -0700 | [diff] [blame] | 107 | for (int i = 0; i < NUM_ELEMENTS; i++) { |
| 108 | // 4 floats per rect |
John Reck | 4c9e59d | 2015-05-12 07:17:50 -0700 | [diff] [blame] | 109 | mRects.get()[i] = (float*) calloc(mFrameSource.capacity(), 4 * sizeof(float)); |
John Reck | fe5e7b7 | 2014-05-23 17:42:28 -0700 | [diff] [blame] | 110 | } |
John Reck | fe5e7b7 | 2014-05-23 17:42:28 -0700 | [diff] [blame] | 111 | } |
| 112 | |
John Reck | 4c9e59d | 2015-05-12 07:17:50 -0700 | [diff] [blame] | 113 | void FrameInfoVisualizer::destroyData() { |
| 114 | mRects.reset(nullptr); |
John Reck | fe5e7b7 | 2014-05-23 17:42:28 -0700 | [diff] [blame] | 115 | } |
| 116 | |
John Reck | 4c9e59d | 2015-05-12 07:17:50 -0700 | [diff] [blame] | 117 | void FrameInfoVisualizer::addRect(Rect& r, float data, float* shapeOutput) { |
John Reck | fe5e7b7 | 2014-05-23 17:42:28 -0700 | [diff] [blame] | 118 | r.top = r.bottom - (data * mVerticalUnit); |
| 119 | shapeOutput[0] = r.left; |
| 120 | shapeOutput[1] = r.top; |
| 121 | shapeOutput[2] = r.right; |
| 122 | shapeOutput[3] = r.bottom; |
| 123 | r.bottom = r.top; |
| 124 | } |
| 125 | |
John Reck | 4c9e59d | 2015-05-12 07:17:50 -0700 | [diff] [blame] | 126 | void FrameInfoVisualizer::prepareShapes(const int baseline) { |
John Reck | fe5e7b7 | 2014-05-23 17:42:28 -0700 | [diff] [blame] | 127 | Rect r; |
| 128 | r.right = mHorizontalUnit; |
John Reck | 4c9e59d | 2015-05-12 07:17:50 -0700 | [diff] [blame] | 129 | for (size_t i = 0; i < mFrameSource.size(); i++) { |
John Reck | fe5e7b7 | 2014-05-23 17:42:28 -0700 | [diff] [blame] | 130 | const int shapeIndex = i * 4; |
| 131 | r.bottom = baseline; |
John Reck | 4c9e59d | 2015-05-12 07:17:50 -0700 | [diff] [blame] | 132 | addRect(r, recordDuration(i), mRects.get()[RECORD_INDEX] + shapeIndex); |
| 133 | addRect(r, prepareDuration(i), mRects.get()[PREPARE_INDEX] + shapeIndex); |
| 134 | addRect(r, issueDrawDuration(i), mRects.get()[PLAYBACK_INDEX] + shapeIndex); |
| 135 | addRect(r, swapBuffersDuration(i), mRects.get()[SWAPBUFFERS_INDEX] + shapeIndex); |
John Reck | fe5e7b7 | 2014-05-23 17:42:28 -0700 | [diff] [blame] | 136 | r.translate(mHorizontalUnit, 0); |
| 137 | } |
| 138 | } |
| 139 | |
John Reck | 4c9e59d | 2015-05-12 07:17:50 -0700 | [diff] [blame] | 140 | void FrameInfoVisualizer::drawGraph(OpenGLRenderer* canvas) { |
John Reck | fe5e7b7 | 2014-05-23 17:42:28 -0700 | [diff] [blame] | 141 | SkPaint paint; |
| 142 | for (int i = 0; i < NUM_ELEMENTS; i++) { |
| 143 | paint.setColor(ELEMENT_COLORS[i]); |
John Reck | 4c9e59d | 2015-05-12 07:17:50 -0700 | [diff] [blame] | 144 | canvas->drawRects(mRects.get()[i], mFrameSource.capacity() * 4, &paint); |
John Reck | fe5e7b7 | 2014-05-23 17:42:28 -0700 | [diff] [blame] | 145 | } |
| 146 | } |
| 147 | |
John Reck | 4c9e59d | 2015-05-12 07:17:50 -0700 | [diff] [blame] | 148 | void FrameInfoVisualizer::drawCurrentFrame(OpenGLRenderer* canvas) { |
John Reck | fe5e7b7 | 2014-05-23 17:42:28 -0700 | [diff] [blame] | 149 | // This draws a solid rect over the entirety of the current frame's shape |
| 150 | // To do so we use the bottom of mRects[0] and the top of mRects[NUM_ELEMENTS-1] |
| 151 | // which will therefore fully overlap the previously drawn rects |
| 152 | SkPaint paint; |
| 153 | paint.setColor(CURRENT_FRAME_COLOR); |
John Reck | 4c9e59d | 2015-05-12 07:17:50 -0700 | [diff] [blame] | 154 | const int i = (mFrameSource.size() - 1) * 4; |
| 155 | canvas->drawRect(mRects.get()[0][i], mRects.get()[NUM_ELEMENTS-1][i+1], |
| 156 | mRects.get()[0][i+2], mRects.get()[0][i+3], &paint); |
John Reck | fe5e7b7 | 2014-05-23 17:42:28 -0700 | [diff] [blame] | 157 | } |
| 158 | |
John Reck | 4c9e59d | 2015-05-12 07:17:50 -0700 | [diff] [blame] | 159 | void FrameInfoVisualizer::drawThreshold(OpenGLRenderer* canvas) { |
John Reck | fe5e7b7 | 2014-05-23 17:42:28 -0700 | [diff] [blame] | 160 | SkPaint paint; |
| 161 | paint.setColor(THRESHOLD_COLOR); |
| 162 | paint.setStrokeWidth(mThresholdStroke); |
| 163 | |
| 164 | float pts[4]; |
| 165 | pts[0] = 0.0f; |
| 166 | pts[1] = pts[3] = canvas->getViewportHeight() - (FRAME_THRESHOLD * mVerticalUnit); |
| 167 | pts[2] = canvas->getViewportWidth(); |
| 168 | canvas->drawLines(pts, 4, &paint); |
| 169 | } |
| 170 | |
John Reck | 4c9e59d | 2015-05-12 07:17:50 -0700 | [diff] [blame] | 171 | bool FrameInfoVisualizer::consumeProperties() { |
John Reck | 23d307c | 2014-10-27 12:38:48 -0700 | [diff] [blame] | 172 | bool changed = false; |
Chris Craik | 2507c34 | 2015-05-04 14:36:49 -0700 | [diff] [blame] | 173 | ProfileType newType = Properties::getProfileType(); |
John Reck | fe5e7b7 | 2014-05-23 17:42:28 -0700 | [diff] [blame] | 174 | if (newType != mType) { |
| 175 | mType = newType; |
Chris Craik | 2507c34 | 2015-05-04 14:36:49 -0700 | [diff] [blame] | 176 | if (mType == ProfileType::None) { |
John Reck | fe5e7b7 | 2014-05-23 17:42:28 -0700 | [diff] [blame] | 177 | destroyData(); |
| 178 | } else { |
| 179 | createData(); |
| 180 | } |
John Reck | 23d307c | 2014-10-27 12:38:48 -0700 | [diff] [blame] | 181 | changed = true; |
John Reck | fe5e7b7 | 2014-05-23 17:42:28 -0700 | [diff] [blame] | 182 | } |
Chris Craik | 2507c34 | 2015-05-04 14:36:49 -0700 | [diff] [blame] | 183 | |
| 184 | bool showDirty = Properties::showDirtyRegions; |
John Reck | 23d307c | 2014-10-27 12:38:48 -0700 | [diff] [blame] | 185 | if (showDirty != mShowDirtyRegions) { |
| 186 | mShowDirtyRegions = showDirty; |
| 187 | changed = true; |
| 188 | } |
| 189 | return changed; |
John Reck | fe5e7b7 | 2014-05-23 17:42:28 -0700 | [diff] [blame] | 190 | } |
| 191 | |
John Reck | 4c9e59d | 2015-05-12 07:17:50 -0700 | [diff] [blame] | 192 | void FrameInfoVisualizer::dumpData(int fd) { |
John Reck | 23d307c | 2014-10-27 12:38:48 -0700 | [diff] [blame] | 193 | RETURN_IF_PROFILING_DISABLED(); |
John Reck | fe5e7b7 | 2014-05-23 17:42:28 -0700 | [diff] [blame] | 194 | |
| 195 | // This method logs the last N frames (where N is <= mDataSize) since the |
| 196 | // last call to dumpData(). In other words if there's a dumpData(), draw frame, |
| 197 | // dumpData(), the last dumpData() should only log 1 frame. |
| 198 | |
John Reck | fe5e7b7 | 2014-05-23 17:42:28 -0700 | [diff] [blame] | 199 | FILE *file = fdopen(fd, "a"); |
| 200 | fprintf(file, "\n\tDraw\tPrepare\tProcess\tExecute\n"); |
| 201 | |
John Reck | 4c9e59d | 2015-05-12 07:17:50 -0700 | [diff] [blame] | 202 | for (size_t i = 0; i < mFrameSource.size(); i++) { |
| 203 | if (mFrameSource[i][FrameInfoIndex::kIntendedVsync] <= mLastFrameLogged) { |
John Reck | fe5e7b7 | 2014-05-23 17:42:28 -0700 | [diff] [blame] | 204 | continue; |
| 205 | } |
John Reck | 4c9e59d | 2015-05-12 07:17:50 -0700 | [diff] [blame] | 206 | mLastFrameLogged = mFrameSource[i][FrameInfoIndex::kIntendedVsync]; |
John Reck | fe5e7b7 | 2014-05-23 17:42:28 -0700 | [diff] [blame] | 207 | fprintf(file, "\t%3.2f\t%3.2f\t%3.2f\t%3.2f\n", |
John Reck | 4c9e59d | 2015-05-12 07:17:50 -0700 | [diff] [blame] | 208 | recordDuration(i), prepareDuration(i), |
| 209 | issueDrawDuration(i), swapBuffersDuration(i)); |
John Reck | fe5e7b7 | 2014-05-23 17:42:28 -0700 | [diff] [blame] | 210 | } |
John Reck | fe5e7b7 | 2014-05-23 17:42:28 -0700 | [diff] [blame] | 211 | |
| 212 | fflush(file); |
| 213 | } |
| 214 | |
| 215 | } /* namespace uirenderer */ |
| 216 | } /* namespace android */ |