xref: /aosp_15_r20/external/skia/tests/TracingTest.cpp (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
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 
8 #include "include/core/SkColorType.h"
9 #include "include/core/SkPoint.h"
10 #include "include/core/SkRect.h"
11 #include "include/core/SkScalar.h"
12 #include "include/core/SkString.h"
13 #include "src/core/SkTraceEvent.h"
14 #include "tests/Test.h"
15 #include "tools/flags/CommandLineFlags.h"
16 
17 #include <cstdint>
18 
19 static DEFINE_bool(slowTracingTest, false,
20                    "Artificially slow down tracing test to produce nicer JSON");
21 
22 namespace {
23 
24 /**
25  * Helper types for demonstrating usage of TRACE_EVENT_OBJECT_XXX macros.
26  */
27 struct TracingShape {
TracingShape__anon41afd2390111::TracingShape28     TracingShape() {
29         TRACE_EVENT_OBJECT_CREATED_WITH_ID("skia.objects", this->typeName(), this);
30     }
~TracingShape__anon41afd2390111::TracingShape31     virtual ~TracingShape() {
32         TRACE_EVENT_OBJECT_DELETED_WITH_ID("skia.objects", this->typeName(), this);
33     }
traceSnapshot__anon41afd2390111::TracingShape34     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 
typeName__anon41afd2390111::TracingShape50     virtual const char* typeName() { return "TracingShape"; }
toString__anon41afd2390111::TracingShape51     virtual SkString toString() { return SkString("Shape()"); }
52 };
53 
54 struct TracingCircle : public TracingShape {
TracingCircle__anon41afd2390111::TracingCircle55     TracingCircle(SkPoint center, SkScalar radius) : fCenter(center), fRadius(radius) {}
typeName__anon41afd2390111::TracingCircle56     const char* typeName() override { return "TracingCircle"; }
toString__anon41afd2390111::TracingCircle57     SkString toString() override {
58         return SkStringPrintf("Circle(%f, %f, %f)", fCenter.fX, fCenter.fY, fRadius);
59     }
60 #if defined(SK_ANDROID_FRAMEWORK_USE_PERFETTO)
WriteIntoTrace__anon41afd2390111::TracingCircle61     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 
70 struct TracingRect : public TracingShape {
TracingRect__anon41afd2390111::TracingRect71     TracingRect(SkRect rect) : fRect(rect) {}
typeName__anon41afd2390111::TracingRect72     const char* typeName() override { return "TracingRect"; }
toString__anon41afd2390111::TracingRect73     SkString toString() override {
74         return SkStringPrintf("Rect(%f, %f, %f, %f)",
75                               fRect.fLeft, fRect.fTop, fRect.fRight, fRect.fBottom);
76     }
77 
78     SkRect fRect;
79 };
80 
81 }  // namespace
82 
83 static SkScalar gTracingTestWorkSink = 1.0f;
84 
do_work(int howMuchWork)85 static 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) {
89             gTracingTestWorkSink += SkScalarSin(i);
90         }
91     }
92 }
93 
test_trace_simple()94 static 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 
test_trace_counters()117 static 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) {
126             SkScalar rad = SkDegreesToRadians(SkIntToScalar(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) {
137             SkScalar rad = SkDegreesToRadians(SkIntToScalar(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) {
150             SkScalar rad = SkDegreesToRadians(SkIntToScalar(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 
test_trace_objects()159 static 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 
DEF_TEST(Tracing,reporter)190 DEF_TEST(Tracing, reporter) {
191     test_trace_simple();
192     test_trace_counters();
193     test_trace_objects();
194 }
195