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