xref: /aosp_15_r20/external/skia/tools/viewer/StatsLayer.cpp (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
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 
enableGpuTimer(SkColor color)71 void StatsLayer::enableGpuTimer(SkColor color) {
72     fGpuTimer.fColor = color;
73     std::fill_n(fGpuTimer.fTimes, std::size(fGpuTimer.fTimes), 0);
74     fGpuTimerEnabled = true;
75 }
76 
disableGpuTimer()77 void StatsLayer::disableGpuTimer() { fGpuTimerEnabled = false; }
78 
issueGpuTimer()79 std::function<void(uint64_t ns)> StatsLayer::issueGpuTimer() {
80     if (fCurrentMeasurement < 0 || !fGpuTimerEnabled) {
81         return {};
82     }
83     // The -1 indicates to the rendering code that we are still awaiting the result. Unlike the CPU
84     // timers, there may be a multi-frame latency.
85     fGpuTimer.fTimes[fCurrentMeasurement] = -1;
86     return [index = fCurrentMeasurement, layer = this](uint64_t ns) {
87         layer->fGpuTimer.fTimes[index] = static_cast<double>(ns) / 1000000.0;
88     };
89 }
90 
onPrePaint()91 void StatsLayer::onPrePaint() {
92     if (fCurrentMeasurement >= 0) {
93         fTotalTimes[fCurrentMeasurement] = SkTime::GetMSecs() - fLastTotalBegin;
94         fCumulativeMeasurementTime += fTotalTimes[fCurrentMeasurement];
95         fCumulativeMeasurementCount++;
96     }
97     fCurrentMeasurement = (fCurrentMeasurement + 1) & (kMeasurementCount - 1);
98     SkASSERT(fCurrentMeasurement >= 0 && fCurrentMeasurement < kMeasurementCount);
99     fLastTotalBegin = SkTime::GetMSecs();
100 }
101 
onPaint(SkSurface * surface)102 void StatsLayer::onPaint(SkSurface* surface) {
103     int nextMeasurement = (fCurrentMeasurement + 1) & (kMeasurementCount - 1);
104     for (int i = 0; i < fTimers.size(); ++i) {
105         fTimers[i].fTimes[nextMeasurement] = 0;
106     }
107 
108 #ifdef SK_BUILD_FOR_ANDROID
109     // Scale up the stats overlay on Android devices
110     static constexpr SkScalar kScale = 1.5;
111 #else
112     SkScalar kScale = fDisplayScale;
113 #endif
114 
115     // Now draw everything
116 
117     // Vertical height corresponding to 1 ms in the graph
118     static const float kPixelPerMS = 2.0f;
119     // Horizontal spacing between measurements
120     static const int kPixelPerMeasurement = 3;
121     // We add one extra spacing on the left, hence the + 1
122     static const int kDisplayWidth = (kMeasurementCount + 1) * kPixelPerMeasurement;
123     static const int kGraphHeight = 100;
124     static const int kTextHeight = 60;
125     // The GPU graph is only shown if supported by the backend.
126     const int gpuGraphHeight = fGpuTimerEnabled ? kGraphHeight : 0;
127     const int displayHeight = gpuGraphHeight + kGraphHeight + kTextHeight;
128     // Padding between the graph and top/right edges of the canvas.
129     static const int kDisplayPadding = 10;
130     // ms/frame to hit 60 fps
131     static const SkScalar kBaseMS = 1000.f / 60.f;
132 
133     auto canvas = surface->getCanvas();
134     SkISize canvasSize = canvas->getBaseLayerSize();
135     SkRect rect = SkRect::MakeXYWH(SkIntToScalar(canvasSize.fWidth-kDisplayWidth-kDisplayPadding),
136                                    SkIntToScalar(kDisplayPadding),
137                                    SkIntToScalar(kDisplayWidth), SkIntToScalar(displayHeight));
138     SkPaint paint;
139     SkAutoCanvasRestore acr(canvas, /*doSave=*/true);
140 
141     // Scale the canvas while keeping the right edge in place.
142     canvas->concat(SkMatrix::RectToRect(SkRect::Make(canvasSize),
143                                         SkRect::MakeXYWH(canvasSize.width()  * (1 - kScale),
144                                                          0,
145                                                          canvasSize.width()  * kScale,
146                                                          canvasSize.height() * kScale)));
147 
148     paint.setColor(SK_ColorBLACK);
149     canvas->drawRect(rect, paint);
150 
151     float cpuGraphBottom = rect.fBottom - gpuGraphHeight;
152 
153     // draw the 16ms line
154     paint.setColor(SK_ColorLTGRAY);
155     canvas->drawLine(rect.fLeft, cpuGraphBottom - kBaseMS*kPixelPerMS,
156                      rect.fRight, cpuGraphBottom - kBaseMS*kPixelPerMS, paint);
157     paint.setColor(SK_ColorRED);
158     paint.setStyle(SkPaint::kStroke_Style);
159     canvas->drawRect(SkRect::MakeLTRB(rect.fLeft, rect.fTop, rect.fRight, cpuGraphBottom), paint);
160     paint.setStyle(SkPaint::kFill_Style);
161 
162     int x = SkScalarTruncToInt(rect.fLeft) + kPixelPerMeasurement;
163     int i = nextMeasurement;
164     SkTDArray<double> sumTimes;
165     sumTimes.resize(fTimers.size());
166     memset(sumTimes.begin(), 0, sumTimes.size() * sizeof(double));
167     int count = 0;
168     double totalTime = 0;
169     int totalCount = 0;
170     do {
171         int startY = SkScalarTruncToInt(cpuGraphBottom);
172         double inc = 0;
173         for (int timer = 0; timer < fTimers.size(); ++timer) {
174             int height = (int)(fTimers[timer].fTimes[i] * kPixelPerMS + 0.5);
175             int endY = std::max(startY - height, kDisplayPadding + kTextHeight);
176             paint.setColor(fTimers[timer].fColor);
177             canvas->drawLine(SkIntToScalar(x), SkIntToScalar(startY),
178                              SkIntToScalar(x), SkIntToScalar(endY), paint);
179             startY = endY;
180             inc += fTimers[timer].fTimes[i];
181             sumTimes[timer] += fTimers[timer].fTimes[i];
182         }
183 
184         int height = (int)(fTotalTimes[i] * kPixelPerMS + 0.5);
185         height = std::max(0, height - (SkScalarTruncToInt(cpuGraphBottom) - startY));
186         int endY = std::max(startY - height, kDisplayPadding + kTextHeight);
187         paint.setColor(SK_ColorWHITE);
188         canvas->drawLine(SkIntToScalar(x), SkIntToScalar(startY),
189                          SkIntToScalar(x), SkIntToScalar(endY), paint);
190         totalTime += fTotalTimes[i];
191         if (fTotalTimes[i] > 0) {
192             ++totalCount;
193         }
194 
195         if (inc > 0) {
196             ++count;
197         }
198 
199         i++;
200         i &= (kMeasurementCount - 1);  // fast mod
201         x += kPixelPerMeasurement;
202     } while (i != nextMeasurement);
203 
204     SkFont font(ToolUtils::CreatePortableTypeface("sans-serif", SkFontStyle()), 14);
205     paint.setColor(SK_ColorWHITE);
206     double time = totalTime / std::max(1, totalCount);
207     double measure = fCumulativeMeasurementTime / std::max(1, fCumulativeMeasurementCount);
208     canvas->drawString(SkStringPrintf("C: %4.3f ms -> %4.3f ms", time, measure),
209                        rect.fLeft + 3, rect.fTop + 14, font, paint);
210 
211     for (int timer = 0; timer < fTimers.size(); ++timer) {
212         paint.setColor(fTimers[timer].fLabelColor);
213         canvas->drawString(SkStringPrintf("%s: %4.3f ms", fTimers[timer].fLabel.c_str(),
214                                           sumTimes[timer] / std::max(1, count)),
215                            rect.fLeft + 3, rect.fTop + 28 + (14 * timer), font, paint);
216     }
217 
218     if (!fGpuTimerEnabled) {
219         return;
220     }
221 
222     float gpuGraphBottom = rect.bottom();
223     // draw the 16ms line
224     paint.setColor(SK_ColorLTGRAY);
225     canvas->drawLine(rect.fLeft, gpuGraphBottom - kBaseMS*kPixelPerMS,
226                      rect.fRight, gpuGraphBottom - kBaseMS*kPixelPerMS,
227                      paint);
228     paint.setColor(SK_ColorRED);
229     paint.setStyle(SkPaint::kStroke_Style);
230     canvas->drawRect(SkRect::MakeLTRB(rect.fLeft, cpuGraphBottom, rect.fRight, gpuGraphBottom),
231                      paint);
232     paint.setStyle(SkPaint::kFill_Style);
233 
234     x = SkScalarTruncToInt(rect.fLeft) + kPixelPerMeasurement;
235     i = nextMeasurement;
236     totalCount = 0;
237     totalTime = 0;
238     do {
239         int endY;
240         if (fGpuTimer.fTimes[i] < 0) {
241             // Draw a full height line with the color inverted to indicate a measurement
242             // that is still pending.
243             auto alpha = SkColorSetARGB(SkColorGetA(fGpuTimer.fColor), 0, 0, 0);
244             auto inv = (0x00FFFFF & ~fGpuTimer.fColor);
245             paint.setColor(alpha | inv);
246             endY = cpuGraphBottom;
247         } else {
248             paint.setColor(fGpuTimer.fColor);
249             ++totalCount;
250             totalTime += fGpuTimer.fTimes[i];
251             float height = fGpuTimer.fTimes[i] * kPixelPerMS + 0.5f;
252             endY = std::max(gpuGraphBottom - height, cpuGraphBottom);
253         }
254         canvas->drawLine(x, gpuGraphBottom, x, endY, paint);
255 
256         i++;
257         i &= (kMeasurementCount - 1);  // fast mod
258         x += kPixelPerMeasurement;
259     } while (i != nextMeasurement);
260     paint.setColor(SK_ColorWHITE);
261     time = totalTime / std::max(1, totalCount);
262     canvas->drawString(SkStringPrintf("G: %4.3f ms", time),
263                        rect.fLeft + 3,
264                        cpuGraphBottom + 14,
265                        font,
266                        paint);
267 }
268