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::MakeRectToRect(SkRect::Make(canvasSize),
102 SkRect::MakeXYWH(canvasSize.width() * (1 - kScale),
103 0,
104 canvasSize.width() * kScale,
105 canvasSize.height() * kScale),
106 SkMatrix::kFill_ScaleToFit));
107
108 paint.setColor(SK_ColorBLACK);
109 canvas->drawRect(rect, paint);
110 // draw the 16ms line
111 paint.setColor(SK_ColorLTGRAY);
112 canvas->drawLine(rect.fLeft, rect.fBottom - kBaseMS*kPixelPerMS,
113 rect.fRight, rect.fBottom - kBaseMS*kPixelPerMS, paint);
114 paint.setColor(SK_ColorRED);
115 paint.setStyle(SkPaint::kStroke_Style);
116 canvas->drawRect(rect, paint);
117 paint.setStyle(SkPaint::kFill_Style);
118
119 int x = SkScalarTruncToInt(rect.fLeft) + kGraphPadding;
120 const int xStep = 3;
121 int i = nextMeasurement;
122 SkTDArray<double> sumTimes;
123 sumTimes.setCount(fTimers.count());
124 memset(sumTimes.begin(), 0, sumTimes.count() * sizeof(double));
125 int count = 0;
126 double totalTime = 0;
127 int totalCount = 0;
128 do {
129 int startY = SkScalarTruncToInt(rect.fBottom);
130 double inc = 0;
131 for (int timer = 0; timer < fTimers.count(); ++timer) {
132 int height = (int)(fTimers[timer].fTimes[i] * kPixelPerMS + 0.5);
133 int endY = SkTMax(startY - height, kDisplayPadding + kTextHeight);
134 paint.setColor(fTimers[timer].fColor);
135 canvas->drawLine(SkIntToScalar(x), SkIntToScalar(startY),
136 SkIntToScalar(x), SkIntToScalar(endY), paint);
137 startY = endY;
138 inc += fTimers[timer].fTimes[i];
139 sumTimes[timer] += fTimers[timer].fTimes[i];
140 }
141
142 int height = (int)(fTotalTimes[i] * kPixelPerMS + 0.5);
143 height = SkTMax(0, height - (SkScalarTruncToInt(rect.fBottom) - startY));
144 int endY = SkTMax(startY - height, kDisplayPadding + kTextHeight);
145 paint.setColor(SK_ColorWHITE);
146 canvas->drawLine(SkIntToScalar(x), SkIntToScalar(startY),
147 SkIntToScalar(x), SkIntToScalar(endY), paint);
148 totalTime += fTotalTimes[i];
149 if (fTotalTimes[i] > 0) {
150 ++totalCount;
151 }
152
153 if (inc > 0) {
154 ++count;
155 }
156
157 i++;
158 i &= (kMeasurementCount - 1); // fast mod
159 x += xStep;
160 } while (i != nextMeasurement);
161
162 SkFont font(nullptr, 16);
163 paint.setColor(SK_ColorWHITE);
164 double time = totalTime / SkTMax(1, totalCount);
165 double measure = fCumulativeMeasurementTime / SkTMax(1, fCumulativeMeasurementCount);
166 canvas->drawString(SkStringPrintf("%4.3f ms -> %4.3f ms", time, measure),
167 rect.fLeft + 3, rect.fTop + 14, font, paint);
168
169 for (int timer = 0; timer < fTimers.count(); ++timer) {
170 paint.setColor(fTimers[timer].fLabelColor);
171 canvas->drawString(SkStringPrintf("%s: %4.3f ms", fTimers[timer].fLabel.c_str(),
172 sumTimes[timer] / SkTMax(1, count)),
173 rect.fLeft + 3, rect.fTop + 28 + (14 * timer), font, paint);
174 }
175
176 canvas->restore();
177 }
178