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