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