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