Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
Public Member Functions | List of all members
flutter::SkStopwatchVisualizer Class Reference

#include <stopwatch_sk.h>

Inheritance diagram for flutter::SkStopwatchVisualizer:
flutter::StopwatchVisualizer

Public Member Functions

 SkStopwatchVisualizer (const Stopwatch &stopwatch)
 
void Visualize (DlCanvas *canvas, const SkRect &rect) const override
 Renders the stopwatch as a graph.
 
- Public Member Functions inherited from flutter::StopwatchVisualizer
 StopwatchVisualizer (const Stopwatch &stopwatch)
 
virtual ~StopwatchVisualizer ()=default
 
 FML_DISALLOW_COPY_AND_ASSIGN (StopwatchVisualizer)
 

Additional Inherited Members

- Protected Member Functions inherited from flutter::StopwatchVisualizer
double UnitFrameInterval (double time_ms) const
 Converts a raster time to a unit interval.
 
double UnitHeight (double time_ms, double max_height) const
 Converts a raster time to a unit height.
 
fml::Milliseconds GetFrameBudget () const
 
- Protected Attributes inherited from flutter::StopwatchVisualizer
const Stopwatchstopwatch_
 
fml::Milliseconds frame_budget_
 

Detailed Description

A stopwatch visualizer that uses Skia (|SkCanvas|) to draw the stopwatch.

See also
DlStopwatchVisualizer for the newer non-backend specific version.

Definition at line 17 of file stopwatch_sk.h.

Constructor & Destructor Documentation

◆ SkStopwatchVisualizer()

flutter::SkStopwatchVisualizer::SkStopwatchVisualizer ( const Stopwatch stopwatch)
inlineexplicit

Definition at line 19 of file stopwatch_sk.h.

20 : StopwatchVisualizer(stopwatch) {}
StopwatchVisualizer(const Stopwatch &stopwatch)
Definition stopwatch.h:95

Member Function Documentation

◆ Visualize()

void flutter::SkStopwatchVisualizer::Visualize ( DlCanvas canvas,
const SkRect rect 
) const
overridevirtual

Renders the stopwatch as a graph.

Parameters
canvasThe canvas to draw on.
[in]rectThe rectangle to draw in.

Implements flutter::StopwatchVisualizer.

Definition at line 88 of file stopwatch_sk.cc.

89 {
90 // Initialize visualize cache if it has not yet been initialized.
91 InitVisualizeSurface(SkISize::Make(rect.width(), rect.height()));
92
93 SkCanvas* cache_canvas = visualize_cache_surface_->getCanvas();
95
96 // Establish the graph position.
97 const SkScalar x = 0;
98 const SkScalar y = 0;
99 const SkScalar width = visualize_cache_surface_->width();
100 const SkScalar height = visualize_cache_surface_->height();
101
102 // Scale the graph to show frame times up to those that are 3 times the frame
103 // time.
104 const double one_frame_ms = GetFrameBudget().count();
105 const double max_interval = one_frame_ms * 3.0;
106 const double max_unit_interval = UnitFrameInterval(max_interval);
107
108 const double sample_unit_width = (1.0 / kMaxSamples);
109
110 // Draw vertical replacement bar to erase old/stale pixels.
111 paint.setColor(0x99FFFFFF);
113 paint.setBlendMode(SkBlendMode::kSrc);
114 double sample_x =
115 x + width * (static_cast<double>(prev_drawn_sample_index_) / kMaxSamples);
116 const auto eraser_rect = SkRect::MakeLTRB(
117 sample_x, y, sample_x + width * sample_unit_width, height);
118 cache_canvas->drawRect(eraser_rect, paint);
119
120 // Draws blue timing bar for new data.
121 paint.setColor(0xAA0000FF);
122 paint.setBlendMode(SkBlendMode::kSrcOver);
123 const auto bar_rect = SkRect::MakeLTRB(
124 sample_x,
125 y + height *
126 (1.0 -
128 .GetLap(stopwatch_.GetCurrentSample() == 0
129 ? kMaxSamples - 1
130 : stopwatch_.GetCurrentSample() - 1)
131 .ToMillisecondsF(),
132 max_unit_interval)),
133 sample_x + width * sample_unit_width, height);
134 cache_canvas->drawRect(bar_rect, paint);
135
136 // Draw horizontal frame markers.
137 paint.setStrokeWidth(0); // hairline
139 paint.setColor(0xCC000000);
140
141 if (max_interval > one_frame_ms) {
142 // Paint the horizontal markers
143 size_t frame_marker_count =
144 static_cast<size_t>(max_interval / one_frame_ms);
145
146 // Limit the number of markers displayed. After a certain point, the graph
147 // becomes crowded
148 if (frame_marker_count > kMaxFrameMarkers) {
149 frame_marker_count = 1;
150 }
151
152 for (size_t frame_index = 0; frame_index < frame_marker_count;
153 frame_index++) {
154 const double frame_height =
155 height * (1.0 - (UnitFrameInterval((frame_index + 1) * one_frame_ms) /
156 max_unit_interval));
157 cache_canvas->drawLine(x, y + frame_height, width, y + frame_height,
158 paint);
159 }
160 }
161
162 // Paint the vertical marker for the current frame.
163 // We paint it over the current frame, not after it, because when we
164 // paint this we don't yet have all the times for the current frame.
166 paint.setBlendMode(SkBlendMode::kSrcOver);
168 // budget exceeded
169 paint.setColor(SK_ColorRED);
170 } else {
171 // within budget
172 paint.setColor(SK_ColorGREEN);
173 }
174 sample_x = x + width * (static_cast<double>(stopwatch_.GetCurrentSample()) /
176 const auto marker_rect = SkRect::MakeLTRB(
177 sample_x, y, sample_x + width * sample_unit_width, height);
178 cache_canvas->drawRect(marker_rect, paint);
179 prev_drawn_sample_index_ = stopwatch_.GetCurrentSample();
180
181 // Draw the cached surface onto the output canvas.
182 auto image = DlImage::Make(visualize_cache_surface_->makeImageSnapshot());
183 canvas->DrawImage(image, {rect.x(), rect.y()},
185}
@ kSrcOver
r = s + (1-sa)*d
constexpr SkColor SK_ColorRED
Definition SkColor.h:126
constexpr SkColor SK_ColorGREEN
Definition SkColor.h:131
void drawRect(const SkRect &rect, const SkPaint &paint)
void drawLine(SkScalar x0, SkScalar y0, SkScalar x1, SkScalar y1, const SkPaint &paint)
@ kStroke_Style
set to stroke geometry
Definition SkPaint.h:194
@ kFill_Style
set to fill geometry
Definition SkPaint.h:193
SkCanvas * getCanvas()
Definition SkSurface.cpp:82
int width() const
Definition SkSurface.h:178
sk_sp< SkImage > makeImageSnapshot()
Definition SkSurface.cpp:90
int height() const
Definition SkSurface.h:184
static sk_sp< DlImage > Make(const SkImage *image)
Definition dl_image.cc:11
double UnitHeight(double time_ms, double max_height) const
Converts a raster time to a unit height.
Definition stopwatch.cc:61
const Stopwatch & stopwatch_
Definition stopwatch.h:122
fml::Milliseconds GetFrameBudget() const
Definition stopwatch.h:120
double UnitFrameInterval(double time_ms) const
Converts a raster time to a unit interval.
Definition stopwatch.cc:57
const fml::TimeDelta & LastLap() const
Definition stopwatch.cc:41
size_t GetCurrentSample() const
Definition stopwatch.cc:53
constexpr double ToMillisecondsF() const
Definition time_delta.h:68
const Paint & paint
sk_sp< SkImage > image
Definition examples.cpp:29
float SkScalar
Definition extension.cpp:12
double y
double x
sk_sp< SkBlender > blender SkRect rect
Definition SkRecords.h:350
static const size_t kMaxSamples
Definition stopwatch.cc:9
static const size_t kMaxFrameMarkers
int32_t height
int32_t width
static constexpr SkISize Make(int32_t w, int32_t h)
Definition SkSize.h:20
static constexpr SkRect MakeLTRB(float l, float t, float r, float b)
Definition SkRect.h:646

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