blob: 5aea04d94cbea99ae9ebb7ad8b6b657b9b0a056e [file] [log] [blame]
John Reckfe5e7b72014-05-23 17:42:28 -07001/*
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 Reck4c9e59d2015-05-12 07:17:50 -070016#include "FrameInfoVisualizer.h"
John Reckfe5e7b72014-05-23 17:42:28 -070017
Chris Craik1dfa0702016-03-04 15:59:24 -080018#include "BakedOpRenderer.h"
Matt Sarettde973072016-10-25 11:07:40 -040019#include "IProfileRenderer.h"
Chris Craik54fa17f2015-11-25 14:14:53 -080020#include "utils/Color.h"
John Reckfe5e7b72014-05-23 17:42:28 -070021
John Reck4c9e59d2015-05-12 07:17:50 -070022#include <cutils/compiler.h>
John Reckbf3c6022015-06-02 15:55:00 -070023#include <array>
John Reckfe5e7b72014-05-23 17:42:28 -070024
John Reck1bcacfd2017-11-03 10:12:19 -070025#define RETURN_IF_PROFILING_DISABLED() \
26 if (CC_LIKELY(mType == ProfileType::None)) return
27#define RETURN_IF_DISABLED() \
28 if (CC_LIKELY(mType == ProfileType::None && !mShowDirtyRegions)) return
John Reckfe5e7b72014-05-23 17:42:28 -070029
John Reckfe5e7b72014-05-23 17:42:28 -070030#define PROFILE_DRAW_WIDTH 3
31#define PROFILE_DRAW_THRESHOLD_STROKE_WIDTH 2
32#define PROFILE_DRAW_DP_PER_MS 7
33
Chris Craik54fa17f2015-11-25 14:14:53 -080034namespace android {
35namespace uirenderer {
36
John Reckfe5e7b72014-05-23 17:42:28 -070037// Must be NUM_ELEMENTS in size
Chris Craik54fa17f2015-11-25 14:14:53 -080038static const SkColor THRESHOLD_COLOR = Color::Green_500;
39static const SkColor BAR_FAST_MASK = 0x8FFFFFFF;
40static const SkColor BAR_JANKY_MASK = 0xDFFFFFFF;
John Reckfe5e7b72014-05-23 17:42:28 -070041
42// We could get this from TimeLord and use the actual frame interval, but
43// this is good enough
44#define FRAME_THRESHOLD 16
John Reck41300272015-06-03 14:42:34 -070045#define FRAME_THRESHOLD_NS 16000000
John Reckfe5e7b72014-05-23 17:42:28 -070046
John Reckbf3c6022015-06-02 15:55:00 -070047struct BarSegment {
48 FrameInfoIndex start;
49 FrameInfoIndex end;
50 SkColor color;
51};
52
John Reck1bcacfd2017-11-03 10:12:19 -070053static const std::array<BarSegment, 7> Bar{{
54 {FrameInfoIndex::IntendedVsync, FrameInfoIndex::HandleInputStart, Color::Teal_700},
55 {FrameInfoIndex::HandleInputStart, FrameInfoIndex::PerformTraversalsStart,
56 Color::Green_700},
57 {FrameInfoIndex::PerformTraversalsStart, FrameInfoIndex::DrawStart, Color::LightGreen_700},
58 {FrameInfoIndex::DrawStart, FrameInfoIndex::SyncStart, Color::Blue_500},
59 {FrameInfoIndex::SyncStart, FrameInfoIndex::IssueDrawCommandsStart, Color::LightBlue_300},
60 {FrameInfoIndex::IssueDrawCommandsStart, FrameInfoIndex::SwapBuffers, Color::Red_500},
61 {FrameInfoIndex::SwapBuffers, FrameInfoIndex::FrameCompleted, Color::Orange_500},
John Reckbf3c6022015-06-02 15:55:00 -070062}};
63
John Reckfe5e7b72014-05-23 17:42:28 -070064static int dpToPx(int dp, float density) {
John Reck1bcacfd2017-11-03 10:12:19 -070065 return (int)(dp * density + 0.5f);
John Reckfe5e7b72014-05-23 17:42:28 -070066}
67
John Reck1bcacfd2017-11-03 10:12:19 -070068FrameInfoVisualizer::FrameInfoVisualizer(FrameInfoSource& source) : mFrameSource(source) {
John Reckfe5e7b72014-05-23 17:42:28 -070069 setDensity(1);
70}
71
John Reck4c9e59d2015-05-12 07:17:50 -070072FrameInfoVisualizer::~FrameInfoVisualizer() {
John Reckfe5e7b72014-05-23 17:42:28 -070073 destroyData();
74}
75
John Reck4c9e59d2015-05-12 07:17:50 -070076void FrameInfoVisualizer::setDensity(float density) {
John Reckfe5e7b72014-05-23 17:42:28 -070077 if (CC_UNLIKELY(mDensity != density)) {
78 mDensity = density;
79 mVerticalUnit = dpToPx(PROFILE_DRAW_DP_PER_MS, density);
John Reckfe5e7b72014-05-23 17:42:28 -070080 mThresholdStroke = dpToPx(PROFILE_DRAW_THRESHOLD_STROKE_WIDTH, density);
81 }
82}
83
John Reck4c9e59d2015-05-12 07:17:50 -070084void FrameInfoVisualizer::unionDirty(SkRect* dirty) {
John Reckfe5e7b72014-05-23 17:42:28 -070085 RETURN_IF_DISABLED();
86 // Not worth worrying about minimizing the dirty region for debugging, so just
87 // dirty the entire viewport.
88 if (dirty) {
John Reck23d307c2014-10-27 12:38:48 -070089 mDirtyRegion = *dirty;
John Reckfe5e7b72014-05-23 17:42:28 -070090 dirty->setEmpty();
91 }
92}
93
Matt Sarettde973072016-10-25 11:07:40 -040094void FrameInfoVisualizer::draw(IProfileRenderer& renderer) {
John Reck23d307c2014-10-27 12:38:48 -070095 RETURN_IF_DISABLED();
96
97 if (mShowDirtyRegions) {
98 mFlashToggle = !mFlashToggle;
99 if (mFlashToggle) {
100 SkPaint paint;
101 paint.setColor(0x7fff0000);
John Reck1bcacfd2017-11-03 10:12:19 -0700102 renderer.drawRect(mDirtyRegion.fLeft, mDirtyRegion.fTop, mDirtyRegion.fRight,
103 mDirtyRegion.fBottom, paint);
John Reck23d307c2014-10-27 12:38:48 -0700104 }
John Reckfe5e7b72014-05-23 17:42:28 -0700105 }
106
Chris Craik2507c342015-05-04 14:36:49 -0700107 if (mType == ProfileType::Bars) {
John Reck41300272015-06-03 14:42:34 -0700108 // Patch up the current frame to pretend we ended here. CanvasContext
109 // will overwrite these values with the real ones after we return.
110 // This is a bit nicer looking than the vague green bar, as we have
111 // valid data for almost all the stages and a very good idea of what
112 // the issue stage will look like, too
113 FrameInfo& info = mFrameSource.back();
114 info.markSwapBuffers();
115 info.markFrameCompleted();
116
Matt Sarettde973072016-10-25 11:07:40 -0400117 initializeRects(renderer.getViewportHeight(), renderer.getViewportWidth());
Chris Craik1dfa0702016-03-04 15:59:24 -0800118 drawGraph(renderer);
119 drawThreshold(renderer);
John Reck23d307c2014-10-27 12:38:48 -0700120 }
John Reckfe5e7b72014-05-23 17:42:28 -0700121}
122
John Reck4c9e59d2015-05-12 07:17:50 -0700123void FrameInfoVisualizer::createData() {
John Reck41300272015-06-03 14:42:34 -0700124 if (mFastRects.get()) return;
John Reckfe5e7b72014-05-23 17:42:28 -0700125
John Reck41300272015-06-03 14:42:34 -0700126 mFastRects.reset(new float[mFrameSource.capacity() * 4]);
127 mJankyRects.reset(new float[mFrameSource.capacity() * 4]);
John Reckfe5e7b72014-05-23 17:42:28 -0700128}
129
John Reck4c9e59d2015-05-12 07:17:50 -0700130void FrameInfoVisualizer::destroyData() {
John Reck41300272015-06-03 14:42:34 -0700131 mFastRects.reset(nullptr);
132 mJankyRects.reset(nullptr);
John Reckfe5e7b72014-05-23 17:42:28 -0700133}
134
John Reck41300272015-06-03 14:42:34 -0700135void FrameInfoVisualizer::initializeRects(const int baseline, const int width) {
136 // Target the 95% mark for the current frame
137 float right = width * .95;
138 float baseLineWidth = right / mFrameSource.capacity();
139 mNumFastRects = 0;
140 mNumJankyRects = 0;
141 int fast_i = 0, janky_i = 0;
John Reckbf3c6022015-06-02 15:55:00 -0700142 // Set the bottom of all the shapes to the baseline
John Reck41300272015-06-03 14:42:34 -0700143 for (int fi = mFrameSource.size() - 1; fi >= 0; fi--) {
144 if (mFrameSource[fi][FrameInfoIndex::Flags] & FrameInfoFlags::SkippedFrame) {
145 continue;
146 }
147 float lineWidth = baseLineWidth;
148 float* rect;
149 int ri;
John Reckbf3c6022015-06-02 15:55:00 -0700150 // Rects are LTRB
John Reck41300272015-06-03 14:42:34 -0700151 if (mFrameSource[fi].totalDuration() <= FRAME_THRESHOLD_NS) {
152 rect = mFastRects.get();
153 ri = fast_i;
154 fast_i += 4;
155 mNumFastRects++;
156 } else {
157 rect = mJankyRects.get();
158 ri = janky_i;
159 janky_i += 4;
160 mNumJankyRects++;
161 lineWidth *= 2;
162 }
163
164 rect[ri + 0] = right - lineWidth;
165 rect[ri + 1] = baseline;
166 rect[ri + 2] = right;
167 rect[ri + 3] = baseline;
168 right -= lineWidth;
John Reckbf3c6022015-06-02 15:55:00 -0700169 }
John Reckfe5e7b72014-05-23 17:42:28 -0700170}
171
John Reckbf3c6022015-06-02 15:55:00 -0700172void FrameInfoVisualizer::nextBarSegment(FrameInfoIndex start, FrameInfoIndex end) {
John Reck41300272015-06-03 14:42:34 -0700173 int fast_i = (mNumFastRects - 1) * 4;
John Reck1bcacfd2017-11-03 10:12:19 -0700174 int janky_i = (mNumJankyRects - 1) * 4;
175 ;
John Reck41300272015-06-03 14:42:34 -0700176 for (size_t fi = 0; fi < mFrameSource.size(); fi++) {
Chris Craik1b54fb22015-06-02 17:40:58 -0700177 if (mFrameSource[fi][FrameInfoIndex::Flags] & FrameInfoFlags::SkippedFrame) {
John Reckbf3c6022015-06-02 15:55:00 -0700178 continue;
179 }
180
John Reck41300272015-06-03 14:42:34 -0700181 float* rect;
182 int ri;
183 // Rects are LTRB
184 if (mFrameSource[fi].totalDuration() <= FRAME_THRESHOLD_NS) {
185 rect = mFastRects.get();
186 ri = fast_i;
187 fast_i -= 4;
188 } else {
189 rect = mJankyRects.get();
190 ri = janky_i;
191 janky_i -= 4;
192 }
193
John Reckbf3c6022015-06-02 15:55:00 -0700194 // Set the bottom to the old top (build upwards)
John Reck41300272015-06-03 14:42:34 -0700195 rect[ri + 3] = rect[ri + 1];
John Reckbf3c6022015-06-02 15:55:00 -0700196 // Move the top up by the duration
John Reckbe3fba02015-07-06 13:49:58 -0700197 rect[ri + 1] -= mVerticalUnit * durationMS(fi, start, end);
John Reckfe5e7b72014-05-23 17:42:28 -0700198 }
199}
200
Matt Sarettde973072016-10-25 11:07:40 -0400201void FrameInfoVisualizer::drawGraph(IProfileRenderer& renderer) {
John Reckfe5e7b72014-05-23 17:42:28 -0700202 SkPaint paint;
John Reckbf3c6022015-06-02 15:55:00 -0700203 for (size_t i = 0; i < Bar.size(); i++) {
John Reckbf3c6022015-06-02 15:55:00 -0700204 nextBarSegment(Bar[i].start, Bar[i].end);
Chris Craik54fa17f2015-11-25 14:14:53 -0800205 paint.setColor(Bar[i].color & BAR_FAST_MASK);
Matt Sarettde973072016-10-25 11:07:40 -0400206 renderer.drawRects(mFastRects.get(), mNumFastRects * 4, paint);
Chris Craik54fa17f2015-11-25 14:14:53 -0800207 paint.setColor(Bar[i].color & BAR_JANKY_MASK);
Matt Sarettde973072016-10-25 11:07:40 -0400208 renderer.drawRects(mJankyRects.get(), mNumJankyRects * 4, paint);
John Reckfe5e7b72014-05-23 17:42:28 -0700209 }
210}
211
Matt Sarettde973072016-10-25 11:07:40 -0400212void FrameInfoVisualizer::drawThreshold(IProfileRenderer& renderer) {
John Reckfe5e7b72014-05-23 17:42:28 -0700213 SkPaint paint;
214 paint.setColor(THRESHOLD_COLOR);
Matt Sarettde973072016-10-25 11:07:40 -0400215 float yLocation = renderer.getViewportHeight() - (FRAME_THRESHOLD * mVerticalUnit);
John Reck1bcacfd2017-11-03 10:12:19 -0700216 renderer.drawRect(0.0f, yLocation - mThresholdStroke / 2, renderer.getViewportWidth(),
217 yLocation + mThresholdStroke / 2, paint);
John Reckfe5e7b72014-05-23 17:42:28 -0700218}
219
John Reck4c9e59d2015-05-12 07:17:50 -0700220bool FrameInfoVisualizer::consumeProperties() {
John Reck23d307c2014-10-27 12:38:48 -0700221 bool changed = false;
Chris Craik2507c342015-05-04 14:36:49 -0700222 ProfileType newType = Properties::getProfileType();
John Reckfe5e7b72014-05-23 17:42:28 -0700223 if (newType != mType) {
224 mType = newType;
Chris Craik2507c342015-05-04 14:36:49 -0700225 if (mType == ProfileType::None) {
John Reckfe5e7b72014-05-23 17:42:28 -0700226 destroyData();
227 } else {
228 createData();
229 }
John Reck23d307c2014-10-27 12:38:48 -0700230 changed = true;
John Reckfe5e7b72014-05-23 17:42:28 -0700231 }
Chris Craik2507c342015-05-04 14:36:49 -0700232
233 bool showDirty = Properties::showDirtyRegions;
John Reck23d307c2014-10-27 12:38:48 -0700234 if (showDirty != mShowDirtyRegions) {
235 mShowDirtyRegions = showDirty;
236 changed = true;
237 }
238 return changed;
John Reckfe5e7b72014-05-23 17:42:28 -0700239}
240
John Reck4c9e59d2015-05-12 07:17:50 -0700241void FrameInfoVisualizer::dumpData(int fd) {
John Reck23d307c2014-10-27 12:38:48 -0700242 RETURN_IF_PROFILING_DISABLED();
John Reckfe5e7b72014-05-23 17:42:28 -0700243
244 // This method logs the last N frames (where N is <= mDataSize) since the
245 // last call to dumpData(). In other words if there's a dumpData(), draw frame,
246 // dumpData(), the last dumpData() should only log 1 frame.
247
John Reck47f5c3a2017-11-13 11:32:39 -0800248 dprintf(fd, "\n\tDraw\tPrepare\tProcess\tExecute\n");
John Reckfe5e7b72014-05-23 17:42:28 -0700249
John Reck4c9e59d2015-05-12 07:17:50 -0700250 for (size_t i = 0; i < mFrameSource.size(); i++) {
Chris Craik1b54fb22015-06-02 17:40:58 -0700251 if (mFrameSource[i][FrameInfoIndex::IntendedVsync] <= mLastFrameLogged) {
John Reckfe5e7b72014-05-23 17:42:28 -0700252 continue;
253 }
Chris Craik1b54fb22015-06-02 17:40:58 -0700254 mLastFrameLogged = mFrameSource[i][FrameInfoIndex::IntendedVsync];
John Reck47f5c3a2017-11-13 11:32:39 -0800255 dprintf(fd, "\t%3.2f\t%3.2f\t%3.2f\t%3.2f\n",
John Reckbe3fba02015-07-06 13:49:58 -0700256 durationMS(i, FrameInfoIndex::IntendedVsync, FrameInfoIndex::SyncStart),
257 durationMS(i, FrameInfoIndex::SyncStart, FrameInfoIndex::IssueDrawCommandsStart),
258 durationMS(i, FrameInfoIndex::IssueDrawCommandsStart, FrameInfoIndex::SwapBuffers),
259 durationMS(i, FrameInfoIndex::SwapBuffers, FrameInfoIndex::FrameCompleted));
John Reckfe5e7b72014-05-23 17:42:28 -0700260 }
John Reckfe5e7b72014-05-23 17:42:28 -0700261}
262
263} /* namespace uirenderer */
264} /* namespace android */