Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
StatsLayer.cpp
Go to the documentation of this file.
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
9
11#include "include/core/SkFont.h"
14#include "include/core/SkRect.h"
17#include "include/core/SkSize.h"
23#include "src/base/SkTime.h"
25
26#include <algorithm>
27#include <string>
28
30 : fCurrentMeasurement(-1)
31 , fLastTotalBegin(0)
32 , fCumulativeMeasurementTime(0)
33 , fCumulativeMeasurementCount(0)
34 , fDisplayScale(1.0f) {
35 memset(fTotalTimes, 0, sizeof(fTotalTimes));
36}
37
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
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
60 if (fCurrentMeasurement >= 0) {
61 fTimers[timer].fTimes[fCurrentMeasurement] -= SkTime::GetMSecs();
62 }
63}
64
66 if (fCurrentMeasurement >= 0) {
67 fTimers[timer].fTimes[fCurrentMeasurement] += SkTime::GetMSecs();
68 }
69}
70
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
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));
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);
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),
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),
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}
int count
SkColor4f color
#define SkASSERT(cond)
Definition SkAssert.h:116
constexpr SkColor SK_ColorLTGRAY
Definition SkColor.h:118
uint32_t SkColor
Definition SkColor.h:37
constexpr SkColor SK_ColorRED
Definition SkColor.h:126
constexpr SkColor SK_ColorBLACK
Definition SkColor.h:103
constexpr SkColor SK_ColorWHITE
Definition SkColor.h:122
#define SkScalarTruncToInt(x)
Definition SkScalar.h:59
#define SkIntToScalar(x)
Definition SkScalar.h:57
SK_API SkString static SkString SkStringPrintf()
Definition SkString.h:287
Type::kYUV Type::kRGBA() int(0.7 *637)
static SkMatrix RectToRect(const SkRect &src, const SkRect &dst, ScaleToFit mode=kFill_ScaleToFit)
Definition SkMatrix.h:157
@ kStroke_Style
set to stroke geometry
Definition SkPaint.h:194
@ kFill_Style
set to fill geometry
Definition SkPaint.h:193
int size() const
Definition SkTDArray.h:138
T * begin()
Definition SkTDArray.h:150
void resize(int count)
Definition SkTDArray.h:183
Timer addTimer(const char *label, SkColor color, SkColor labelColor=0)
void onPrePaint() override
void resetMeasurements()
void beginTiming(Timer)
void onPaint(SkSurface *) override
void endTiming(Timer)
int size() const
Definition SkTArray.h:416
const Paint & paint
VkSurfaceKHR surface
Definition main.cc:49
float SkScalar
Definition extension.cpp:12
constexpr SkScalar kTextHeight
Definition glyph_pos.cpp:27
double x
double GetMSecs()
Definition SkTime.h:17
sk_sp< SkTypeface > CreatePortableTypeface(const char *name, SkFontStyle style)
int32_t height
int32_t fWidth
Definition SkSize.h:17
constexpr int32_t width() const
Definition SkSize.h:36
constexpr int32_t height() const
Definition SkSize.h:37
static SkRect Make(const SkISize &size)
Definition SkRect.h:669
static constexpr SkRect MakeXYWH(float x, float y, float w, float h)
Definition SkRect.h:659
static constexpr int kScale