Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
TracingTest.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
10#include "include/core/SkRect.h"
14#include "tests/Test.h"
16
17#include <cstdint>
18
19static DEFINE_bool(slowTracingTest, false,
20 "Artificially slow down tracing test to produce nicer JSON");
21
22namespace {
23
24/**
25 * Helper types for demonstrating usage of TRACE_EVENT_OBJECT_XXX macros.
26 */
27struct TracingShape {
28 TracingShape() {
29 TRACE_EVENT_OBJECT_CREATED_WITH_ID("skia.objects", this->typeName(), this);
30 }
31 virtual ~TracingShape() {
32 TRACE_EVENT_OBJECT_DELETED_WITH_ID("skia.objects", this->typeName(), this);
33 }
34 void traceSnapshot() {
35 // The state of an object can be specified at any point with the OBJECT_SNAPSHOT macro.
36 // This takes the "name" (actually the type name), the ID of the object (typically a
37 // pointer), and a single (unnnamed) argument, which is the "snapshot" of that object.
38 //
39 // Tracing viewer requires that all object macros use the same name and id for creation,
40 // deletion, and snapshots. However: It's convenient to put creation and deletion in the
41 // base-class constructor/destructor where the actual type name isn't known yet. That's
42 // what we're doing here. The JSON for snapshots can therefore include the actual type
43 // name, and a special tag that refers to the type name originally used at creation time.
44 // Skia's JSON tracer handles this automatically, so SNAPSHOT macros can simply use the
45 // derived type name, and the JSON will be formatted correctly to link the events.
46 TRACE_EVENT_OBJECT_SNAPSHOT_WITH_ID("skia.objects", this->typeName(), this,
47 TRACE_STR_COPY(this->toString().c_str()));
48 }
49
50 virtual const char* typeName() { return "TracingShape"; }
51 virtual SkString toString() { return SkString("Shape()"); }
52};
53
54struct TracingCircle : public TracingShape {
55 TracingCircle(SkPoint center, SkScalar radius) : fCenter(center), fRadius(radius) {}
56 const char* typeName() override { return "TracingCircle"; }
57 SkString toString() override {
58 return SkStringPrintf("Circle(%f, %f, %f)", fCenter.fX, fCenter.fY, fRadius);
59 }
60#if defined(SK_ANDROID_FRAMEWORK_USE_PERFETTO)
61 void WriteIntoTrace(::perfetto::TracedValue context) {
62 std::move(context).WriteString(toString().c_str());
63 }
64#endif
65
66 SkPoint fCenter;
67 SkScalar fRadius;
68};
69
70struct TracingRect : public TracingShape {
71 TracingRect(SkRect rect) : fRect(rect) {}
72 const char* typeName() override { return "TracingRect"; }
73 SkString toString() override {
74 return SkStringPrintf("Rect(%f, %f, %f, %f)",
76 }
77
79};
80
81} // namespace
82
84
85static void do_work(int howMuchWork) {
86 // Do busy work so the trace marker durations are large enough to be readable in trace viewer
87 if (FLAGS_slowTracingTest) {
88 for (int i = 0; i < howMuchWork * 100; ++i) {
90 }
91 }
92}
93
94static void test_trace_simple() {
95 // Simple event that lasts until the end of the current scope. TRACE_FUNC is an easy way
96 // to insert the current function name.
97 TRACE_EVENT0("skia", TRACE_FUNC);
98
99 {
100 // There are versions of the macro that take 1 or 2 named arguments. The arguments
101 // can be any simple type. Strings need to be static/literal - we just copy pointers.
102 // Argument names & values are shown when the event is selected in the viewer.
103 TRACE_EVENT1("skia", "Nested work",
104 "isBGRA", kN32_SkColorType == kBGRA_8888_SkColorType);
105 do_work(500);
106 }
107
108 {
109 // If you must copy a string as an argument value, use the TRACE_STR_COPY macro.
110 // This will instruct the tracing system (if one is active) to make a copy.
111 SkString message = SkStringPrintf("%s %s", "Hello", "World");
112 TRACE_EVENT1("skia", "Dynamic String", "message", TRACE_STR_COPY(message.c_str()));
113 do_work(500);
114 }
115}
116
117static void test_trace_counters() {
118 TRACE_EVENT0("skia", TRACE_FUNC);
119
120 {
121 TRACE_EVENT0("skia", "Single Counter");
122
123 // Counter macros allow recording a named value (which must be a 32-bit integer).
124 // The value will be graphed in the viewer.
125 for (int i = 0; i < 180; ++i) {
127 TRACE_COUNTER1("skia", "sin", SkScalarSin(rad) * 1000.0f + 1000.0f);
128 do_work(10);
129 }
130 }
131
132 {
133 TRACE_EVENT0("skia", "Independent Counters");
134
135 // Recording multiple counters with separate COUNTER1 macros will make separate graphs.
136 for (int i = 0; i < 180; ++i) {
138 TRACE_COUNTER1("skia", "sin", SkScalarSin(rad) * 1000.0f + 1000.0f);
139 TRACE_COUNTER1("skia", "cos", SkScalarCos(rad) * 1000.0f + 1000.0f);
140 do_work(10);
141 }
142 }
143
144 {
145 TRACE_EVENT0("skia", "Stacked Counters");
146
147 // Two counters can be recorded together with COUNTER2. They will be graphed together,
148 // as a stacked bar graph. The combined graph needs a name, as does each data series.
149 for (int i = 0; i < 180; ++i) {
151 TRACE_COUNTER2("skia", "trig",
152 "sin", SkScalarSin(rad) * 1000.0f + 1000.0f,
153 "cos", SkScalarCos(rad) * 1000.0f + 1000.0f);
154 do_work(10);
155 }
156 }
157}
158
159static void test_trace_objects() {
160 TRACE_EVENT0("skia", TRACE_FUNC);
161
162 // Objects can be tracked through time with the TRACE_EVENT_OBJECT_ macros.
163 // The macros in use (and their idiosyncracies) are commented in the TracingShape class above.
164
165 TracingCircle* circle = new TracingCircle(SkPoint::Make(20, 20), 15);
166 circle->traceSnapshot();
167 do_work(100);
168
169 // Make another object. Objects with the same base type are shown in the same row in the viewer.
170 TracingRect* rect = new TracingRect(SkRect::MakeWH(100, 50));
171 rect->traceSnapshot();
172 do_work(100);
173
174 // We can create multiple snapshots of objects to reflect their state over time.
175 circle->fCenter.offset(10, 10);
176 circle->traceSnapshot();
177
178 {
179 // Other events (duration or instant) can refer directly to objects. For Skia's JSON
180 // tracer, having an argument whose name starts with '#' will trigger the creation of JSON
181 // that links the event to the object (with a direct link to the most recent snapshot).
182 TRACE_EVENT1("skia", "Processing Shape", "#shape", circle);
183 do_work(100);
184 }
185
186 delete circle;
187 delete rect;
188}
189
#define DEFINE_bool(name, defaultValue, helpString)
SkRect fRect
reporter
@ kBGRA_8888_SkColorType
pixel with 8 bits for blue, green, red, alpha; in 32-bit word
Definition SkColorType.h:26
#define SkDegreesToRadians(degrees)
Definition SkScalar.h:77
#define SkScalarSin(radians)
Definition SkScalar.h:45
#define SkIntToScalar(x)
Definition SkScalar.h:57
#define SkScalarCos(radians)
Definition SkScalar.h:46
SK_API SkString static SkString SkStringPrintf()
Definition SkString.h:287
#define TRACE_COUNTER1(category_group, name, value)
#define TRACE_EVENT_OBJECT_DELETED_WITH_ID(category_group, name, id)
#define TRACE_EVENT_OBJECT_CREATED_WITH_ID(category_group, name, id)
#define TRACE_COUNTER2(category_group, name, value1_name, value1_val, value2_name, value2_val)
#define TRACE_EVENT_OBJECT_SNAPSHOT_WITH_ID(category_group, name, id, snapshot)
#define TRACE_STR_COPY(str)
#define TRACE_FUNC
#define DEF_TEST(name, reporter)
Definition Test.h:312
static SkScalar center(float pos0, float pos1)
static void do_work(int howMuchWork)
static void test_trace_objects()
static void test_trace_counters()
static void test_trace_simple()
static SkScalar gTracingTestWorkSink
float SkScalar
Definition extension.cpp:12
Win32Message message
sk_sp< SkBlender > blender SkRect rect
Definition SkRecords.h:350
float fX
x-axis value
static constexpr SkPoint Make(float x, float y)
float fY
y-axis value
SkScalar fBottom
larger y-axis bounds
Definition extension.cpp:17
SkScalar fLeft
smaller x-axis bounds
Definition extension.cpp:14
SkScalar fRight
larger x-axis bounds
Definition extension.cpp:16
static constexpr SkRect MakeWH(float w, float h)
Definition SkRect.h:609
SkScalar fTop
smaller y-axis bounds
Definition extension.cpp:15
#define TRACE_EVENT0(category_group, name)
#define TRACE_EVENT1(category_group, name, arg1_name, arg1_val)