1 /*
2 * Copyright 2017 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8 #include "tools/viewer/StatsLayer.h"
9
10 #include "include/core/SkCanvas.h"
11 #include "include/core/SkFont.h"
12 #include "include/core/SkString.h"
13 #include "include/core/SkSurface.h"
14 #include "include/core/SkTime.h"
15
StatsLayer()16 StatsLayer::StatsLayer()
17 : fCurrentMeasurement(-1)
18 , fLastTotalBegin(0)
19 , fCumulativeMeasurementTime(0)
20 , fCumulativeMeasurementCount(0)
21 , fDisplayScale(1.0f) {
22 memset(fTotalTimes, 0, sizeof(fTotalTimes));
23 }
24
resetMeasurements()25 void StatsLayer::resetMeasurements() {
26 for (int i = 0; i < fTimers.count(); ++i) {
27 memset(fTimers[i].fTimes, 0, sizeof(fTimers[i].fTimes));
28 }
29 memset(fTotalTimes, 0, sizeof(fTotalTimes));
30 fCurrentMeasurement = -1;
31 fLastTotalBegin = 0;
32 fCumulativeMeasurementTime = 0;
33 fCumulativeMeasurementCount = 0;
34 }
35
addTimer(const char * label,SkColor color,SkColor labelColor)36 StatsLayer::Timer StatsLayer::addTimer(const char* label, SkColor color, SkColor labelColor) {
37 Timer newTimer = fTimers.count();
38 TimerData& newData = fTimers.push_back();
39 memset(newData.fTimes, 0, sizeof(newData.fTimes));
40 newData.fLabel = label;
41 newData.fColor = color;
42 newData.fLabelColor = labelColor ? labelColor : color;
43 return newTimer;
44 }
45
beginTiming(Timer timer)46 void StatsLayer::beginTiming(Timer timer) {
47 if (fCurrentMeasurement >= 0) {
48 fTimers[timer].fTimes[fCurrentMeasurement] -= SkTime::GetMSecs();
49 }
50 }
51
endTiming(Timer timer)52 void StatsLayer::endTiming(Timer timer) {
53 if (fCurrentMeasurement >= 0) {
54 fTimers[timer].fTimes[fCurrentMeasurement] += SkTime::GetMSecs();
55 }
56 }
57
onPrePaint()58 void StatsLayer::onPrePaint() {
59 if (fCurrentMeasurement >= 0) {
60 fTotalTimes[fCurrentMeasurement] = SkTime::GetMSecs() - fLastTotalBegin;
61 fCumulativeMeasurementTime += fTotalTimes[fCurrentMeasurement];
62 fCumulativeMeasurementCount++;
63 }
64 fCurrentMeasurement = (fCurrentMeasurement + 1) & (kMeasurementCount - 1);
65 SkASSERT(fCurrentMeasurement >= 0 && fCurrentMeasurement < kMeasurementCount);
66 fLastTotalBegin = SkTime::GetMSecs();
67 }
68
onPaint(SkSurface * surface)69 void StatsLayer::onPaint(SkSurface* surface) {
70 int nextMeasurement = (fCurrentMeasurement + 1) & (kMeasurementCount - 1);
71 for (int i = 0; i < fTimers.count(); ++i) {
72 fTimers[i].fTimes[nextMeasurement] = 0;
73 }
74
75 #ifdef SK_BUILD_FOR_ANDROID
76 // Scale up the stats overlay on Android devices
77 static constexpr SkScalar kScale = 1.5;
78 #else
79 SkScalar kScale = fDisplayScale;
80 #endif
81
82 // Now draw everything
83 static const float kPixelPerMS = 2.0f;
84 static const int kDisplayWidth = 192;
85 static const int kGraphHeight = 100;
86 static const int kTextHeight = 60;
87 static const int kDisplayHeight = kGraphHeight + kTextHeight;
88 static const int kDisplayPadding = 10;
89 static const int kGraphPadding = 3;
90 static const SkScalar kBaseMS = 1000.f / 60.f; // ms/frame to hit 60 fps
91
92 auto canvas = surface->getCanvas();
93 SkISize canvasSize = canvas->getBaseLayerSize();
94 SkRect rect = SkRect::MakeXYWH(SkIntToScalar(canvasSize.fWidth-kDisplayWidth-kDisplayPadding),
95 SkIntToScalar(kDisplayPadding),
96 SkIntToScalar(kDisplayWidth), SkIntToScalar(kDisplayHeight));
97 SkPaint paint;
98 canvas->save();
99
100 // Scale the canvas while keeping the right edge in place.
101 canvas->concat(SkMatrix::RectToRect(SkRect::Make(canvasSize),
102 SkRect::MakeXYWH(canvasSize.width() * (1 - kScale),
103 0,
104 canvasSize.width() * kScale,
105 canvasSize.height() * kScale)));
106
107 paint.setColor(SK_ColorBLACK);
108 canvas->drawRect(rect, paint);
109 // draw the 16ms line
110 paint.setColor(SK_ColorLTGRAY);
111 canvas->drawLine(rect.fLeft, rect.fBottom - kBaseMS*kPixelPerMS,
112 rect.fRight, rect.fBottom - kBaseMS*kPixelPerMS, paint);
113 paint.setColor(SK_ColorRED);
114 paint.setStyle(SkPaint::kStroke_Style);
115 canvas->drawRect(rect, paint);
116 paint.setStyle(SkPaint::kFill_Style);
117
118 int x = SkScalarTruncToInt(rect.fLeft) + kGraphPadding;
119 const int xStep = 3;
120 int i = nextMeasurement;
121 SkTDArray<double> sumTimes;
122 sumTimes.setCount(fTimers.count());
123 memset(sumTimes.begin(), 0, sumTimes.count() * sizeof(double));
124 int count = 0;
125 double totalTime = 0;
126 int totalCount = 0;
127 do {
128 int startY = SkScalarTruncToInt(rect.fBottom);
129 double inc = 0;
130 for (int timer = 0; timer < fTimers.count(); ++timer) {
131 int height = (int)(fTimers[timer].fTimes[i] * kPixelPerMS + 0.5);
132 int endY = std::max(startY - height, kDisplayPadding + kTextHeight);
133 paint.setColor(fTimers[timer].fColor);
134 canvas->drawLine(SkIntToScalar(x), SkIntToScalar(startY),
135 SkIntToScalar(x), SkIntToScalar(endY), paint);
136 startY = endY;
137 inc += fTimers[timer].fTimes[i];
138 sumTimes[timer] += fTimers[timer].fTimes[i];
139 }
140
141 int height = (int)(fTotalTimes[i] * kPixelPerMS + 0.5);
142 height = std::max(0, height - (SkScalarTruncToInt(rect.fBottom) - startY));
143 int endY = std::max(startY - height, kDisplayPadding + kTextHeight);
144 paint.setColor(SK_ColorWHITE);
145 canvas->drawLine(SkIntToScalar(x), SkIntToScalar(startY),
146 SkIntToScalar(x), SkIntToScalar(endY), paint);
147 totalTime += fTotalTimes[i];
148 if (fTotalTimes[i] > 0) {
149 ++totalCount;
150 }
151
152 if (inc > 0) {
153 ++count;
154 }
155
156 i++;
157 i &= (kMeasurementCount - 1); // fast mod
158 x += xStep;
159 } while (i != nextMeasurement);
160
161 SkFont font(nullptr, 16);
162 paint.setColor(SK_ColorWHITE);
163 double time = totalTime / std::max(1, totalCount);
164 double measure = fCumulativeMeasurementTime / std::max(1, fCumulativeMeasurementCount);
165 canvas->drawString(SkStringPrintf("%4.3f ms -> %4.3f ms", time, measure),
166 rect.fLeft + 3, rect.fTop + 14, font, paint);
167
168 for (int timer = 0; timer < fTimers.count(); ++timer) {
169 paint.setColor(fTimers[timer].fLabelColor);
170 canvas->drawString(SkStringPrintf("%s: %4.3f ms", fTimers[timer].fLabel.c_str(),
171 sumTimes[timer] / std::max(1, count)),
172 rect.fLeft + 3, rect.fTop + 28 + (14 * timer), font, paint);
173 }
174
175 canvas->restore();
176 }
177