Flutter Engine
The Flutter Engine
Classes | Public Types | Public Member Functions | List of all members
StatsLayer Class Reference

#include <StatsLayer.h>

Inheritance diagram for StatsLayer:
sk_app::Window::Layer

Public Types

typedef int Timer
 

Public Member Functions

 StatsLayer ()
 
void resetMeasurements ()
 
Timer addTimer (const char *label, SkColor color, SkColor labelColor=0)
 
void beginTiming (Timer)
 
void endTiming (Timer)
 
void onPrePaint () override
 
void onPaint (SkSurface *) override
 
void setDisplayScale (float scale)
 
- Public Member Functions inherited from sk_app::Window::Layer
 Layer ()
 
virtual ~Layer ()=default
 
bool getActive ()
 
void setActive (bool active)
 
virtual void onBackendCreated ()
 
virtual void onAttach (Window *window)
 
virtual bool onChar (SkUnichar c, skui::ModifierKey)
 
virtual bool onKey (skui::Key, skui::InputState, skui::ModifierKey)
 
virtual bool onMouse (int x, int y, skui::InputState, skui::ModifierKey)
 
virtual bool onMouseWheel (float delta, int x, int y, skui::ModifierKey)
 
virtual bool onTouch (intptr_t owner, skui::InputState, float x, float y)
 
virtual bool onFling (skui::InputState state)
 
virtual bool onPinch (skui::InputState state, float scale, float x, float y)
 
virtual void onUIStateChanged (const SkString &stateName, const SkString &stateValue)
 
virtual void onPrePaint ()
 
virtual void onPaint (SkSurface *)
 
virtual void onResize (int width, int height)
 

Detailed Description

Definition at line 18 of file StatsLayer.h.

Member Typedef Documentation

◆ Timer

Definition at line 23 of file StatsLayer.h.

Constructor & Destructor Documentation

◆ StatsLayer()

StatsLayer::StatsLayer ( )

Definition at line 29 of file StatsLayer.cpp.

30 : fCurrentMeasurement(-1)
31 , fLastTotalBegin(0)
32 , fCumulativeMeasurementTime(0)
33 , fCumulativeMeasurementCount(0)
34 , fDisplayScale(1.0f) {
35 memset(fTotalTimes, 0, sizeof(fTotalTimes));
36}

Member Function Documentation

◆ addTimer()

StatsLayer::Timer StatsLayer::addTimer ( const char *  label,
SkColor  color,
SkColor  labelColor = 0 
)

Definition at line 49 of file StatsLayer.cpp.

49 {
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}
int size() const
Definition: SkTArray.h:421
DlColor color

◆ beginTiming()

void StatsLayer::beginTiming ( Timer  timer)

Definition at line 59 of file StatsLayer.cpp.

59 {
60 if (fCurrentMeasurement >= 0) {
61 fTimers[timer].fTimes[fCurrentMeasurement] -= SkTime::GetMSecs();
62 }
63}
double GetMSecs()
Definition: SkTime.h:17

◆ endTiming()

void StatsLayer::endTiming ( Timer  timer)

Definition at line 65 of file StatsLayer.cpp.

65 {
66 if (fCurrentMeasurement >= 0) {
67 fTimers[timer].fTimes[fCurrentMeasurement] += SkTime::GetMSecs();
68 }
69}

◆ onPaint()

void StatsLayer::onPaint ( SkSurface surface)
overridevirtual

Reimplemented from sk_app::Window::Layer.

Definition at line 82 of file StatsLayer.cpp.

82 {
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
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
Definition: FontMgrTest.cpp:50
constexpr SkColor SK_ColorLTGRAY
Definition: SkColor.h:118
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 SkStringPrintf(const char *format,...) SK_PRINTF_LIKE(1
Creates a new string and writes into it using a printf()-style format.
Definition: SkFont.h:35
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
const Paint & paint
Definition: color_source.cc:38
VkSurfaceKHR surface
Definition: main.cc:49
float SkScalar
Definition: extension.cpp:12
constexpr SkScalar kTextHeight
Definition: glyph_pos.cpp:27
static float max(float r, float g, float b)
Definition: hsl.cpp:49
double x
sk_sp< SkBlender > blender SkRect rect
Definition: SkRecords.h:350
sk_sp< SkTypeface > CreatePortableTypeface(const char *name, SkFontStyle style)
font
Font Metadata and Metrics.
static double time(int loops, Benchmark *bench, Target *target)
Definition: nanobench.cpp:394
int32_t height
Definition: SkSize.h:16
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

◆ onPrePaint()

void StatsLayer::onPrePaint ( )
overridevirtual

Reimplemented from sk_app::Window::Layer.

Definition at line 71 of file StatsLayer.cpp.

71 {
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}
#define SkASSERT(cond)
Definition: SkAssert.h:116

◆ resetMeasurements()

void StatsLayer::resetMeasurements ( )

Definition at line 38 of file StatsLayer.cpp.

38 {
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}

◆ setDisplayScale()

void StatsLayer::setDisplayScale ( float  scale)
inline

Definition at line 32 of file StatsLayer.h.

32{ fDisplayScale = scale; }
const Scalar scale

The documentation for this class was generated from the following files: