Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
stopwatch_dl.cc
Go to the documentation of this file.
1// Copyright 2013 The Flutter Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "flutter/flow/stopwatch_dl.h"
6#include <memory>
7#include <vector>
13#include "include/core/SkRect.h"
14
15namespace flutter {
16
17static const size_t kMaxSamples = 120;
18static const size_t kMaxFrameMarkers = 8;
19
21 const SkRect& rect) const {
22 auto painter = DlVertexPainter();
24
25 // Establish the graph position.
26 auto const x = rect.x();
27 auto const y = rect.y();
28 auto const width = rect.width();
29 auto const height = rect.height();
30 auto const bottom = rect.bottom();
31
32 // Scale the graph to show time frames up to those that are 3x the frame time.
33 auto const one_frame_ms = GetFrameBudget().count();
34 auto const max_interval = one_frame_ms * 3.0;
35 auto const max_unit_interval = UnitFrameInterval(max_interval);
36 auto const sample_unit_width = (1.0 / kMaxSamples);
37
38 // Provide a semi-transparent background for the graph.
39 painter.DrawRect(rect, DlColor(0x99FFFFFF));
40
41 // Prepare a path for the data; we start at the height of the last point so
42 // it looks like we wrap around.
43 {
44 for (auto i = size_t(0); i < stopwatch_.GetLapsCount(); i++) {
45 auto const sample_unit_height =
47 max_unit_interval));
48
49 auto const bar_width = width * sample_unit_width;
50 auto const bar_height = height * sample_unit_height;
51 auto const bar_left = x + width * sample_unit_width * i;
52
53 painter.DrawRect(SkRect::MakeLTRB(/*l=*/bar_left,
54 /*t=*/y + bar_height,
55 /*r=*/bar_left + bar_width,
56 /*b=*/bottom),
57 DlColor(0xAA0000FF));
58 }
59 }
60
61 // Draw horizontal frame markers.
62 {
63 if (max_interval > one_frame_ms) {
64 // Paint the horizontal markers.
65 auto count = static_cast<size_t>(max_interval / one_frame_ms);
66
67 // Limit the number of markers to a reasonable amount.
68 if (count > kMaxFrameMarkers) {
69 count = 1;
70 }
71
72 for (auto i = size_t(0); i < count; i++) {
73 auto const frame_height =
74 height * (1.0 - (UnitFrameInterval(i + 1) * one_frame_ms) /
75 max_unit_interval);
76
77 // Draw a skinny rectangle (i.e. a line).
78 painter.DrawRect(SkRect::MakeLTRB(/*l=*/x,
79 /*t=*/y + frame_height,
80 /*r=*/width,
81 /*b=*/y + frame_height + 1),
82 DlColor(0xCC000000));
83 }
84 }
85 }
86
87 // Paint the vertical marker for the current frame.
88 {
91 // budget exceeded.
93 }
94 auto const l =
95 x + width * (static_cast<double>(stopwatch_.GetCurrentSample()) /
97 auto const t = y;
98 auto const r = l + width * sample_unit_width;
99 auto const b = rect.bottom();
100 painter.DrawRect(SkRect::MakeLTRB(l, t, r, b), color);
101 }
102
103 // Actually draw.
104 // Use kSrcOver blend mode so that elements under the performance overlay are
105 // partially visible.
106 paint.setBlendMode(DlBlendMode::kSrcOver);
107 // The second blend mode does nothing since the paint has no additional color
108 // sources like a tiled image or gradient.
109 canvas->DrawVertices(painter.IntoVertices(), DlBlendMode::kSrcOver, paint);
110}
111
113 // Draw 6 vertices representing 2 triangles.
114 auto const left = rect.x();
115 auto const top = rect.y();
116 auto const right = rect.right();
117 auto const bottom = rect.bottom();
118
119 auto const vertices = std::array<SkPoint, 6>{
120 SkPoint::Make(left, top), // tl tr
121 SkPoint::Make(right, top), // br
122 SkPoint::Make(right, bottom), //
123 SkPoint::Make(right, bottom), // tl
124 SkPoint::Make(left, bottom), // bl br
125 SkPoint::Make(left, top) //
126 };
127
128 auto const colors = std::array<DlColor, 6>{
129 color, // tl tr
130 color, // br
131 color, //
132 color, // tl
133 color, // bl br
134 color //
135 };
136
137 vertices_.insert(vertices_.end(), vertices.begin(), vertices.end());
138 colors_.insert(colors_.end(), colors.begin(), colors.end());
139}
140
141std::shared_ptr<DlVertices> DlVertexPainter::IntoVertices() {
142 auto const result = DlVertices::Make(
144 /*vertex_count=*/vertices_.size(),
145 /*vertices=*/vertices_.data(),
146 /*texture_coordinates=*/nullptr,
147 /*colors=*/colors_.data());
148 vertices_.clear();
149 colors_.clear();
150 return result;
151}
152
153} // namespace flutter
int count
SkColor4f color
static bool left(const SkPoint &p0, const SkPoint &p1)
static bool right(const SkPoint &p0, const SkPoint &p1)
Developer-facing API for rendering anything within the engine.
Definition dl_canvas.h:37
virtual void DrawVertices(const DlVertices *vertices, DlBlendMode mode, const DlPaint &paint)=0
void Visualize(DlCanvas *canvas, const SkRect &rect) const override
Renders the stopwatch as a graph.
Provides canvas-like painting methods that actually build vertices.
void DrawRect(const SkRect &rect, const DlColor &color)
Draws a rectangle with the given color to a buffer.
std::shared_ptr< DlVertices > IntoVertices()
static std::shared_ptr< DlVertices > Make(DlVertexMode mode, int vertex_count, const SkPoint vertices[], const SkPoint texture_coordinates[], const DlColor colors[], int index_count=0, const uint16_t indices[]=nullptr)
Constructs a DlVector with compact inline storage for all of its required and optional lists of data.
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 & GetLap(size_t index) const
Definition stopwatch.cc:45
const fml::TimeDelta & LastLap() const
Definition stopwatch.cc:41
size_t GetLapsCount() const
Return a reference to all the laps.
Definition stopwatch.cc:49
size_t GetCurrentSample() const
Definition stopwatch.cc:53
constexpr double ToMillisecondsF() const
Definition time_delta.h:68
const Paint & paint
static bool b
GAsyncResult * result
double y
double x
@ kTriangles
The vertices are taken 3 at a time to form a triangle.
static const size_t kMaxSamples
Definition stopwatch.cc:9
static const size_t kMaxFrameMarkers
@ kSrcOver
r = s + (1-sa)*d
int32_t height
int32_t width
static constexpr SkPoint Make(float x, float y)
static constexpr SkRect MakeLTRB(float l, float t, float r, float b)
Definition SkRect.h:646
static constexpr DlColor kRed()
Definition dl_color.h:24
static constexpr DlColor kGreen()
Definition dl_color.h:25