Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit 0fc1bea

Browse files
Add an allowlist flag for Skia trace event categories (#25985)
1 parent 75f705b commit 0fc1bea

File tree

6 files changed

+71
-56
lines changed

6 files changed

+71
-56
lines changed

common/settings.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,8 @@ struct Settings {
104104
bool enable_checked_mode = false;
105105
bool start_paused = false;
106106
bool trace_skia = false;
107-
std::string trace_allowlist;
107+
std::vector<std::string> trace_allowlist;
108+
std::vector<std::string> trace_skia_allowlist;
108109
bool trace_startup = false;
109110
bool trace_systrace = false;
110111
bool dump_skp_on_shader_compilation = false;

shell/common/shell.cc

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -68,16 +68,6 @@ std::unique_ptr<Engine> CreateEngine(
6868
volatile_path_tracker);
6969
}
7070

71-
void Tokenize(const std::string& input,
72-
std::vector<std::string>* results,
73-
char delimiter) {
74-
std::istringstream ss(input);
75-
std::string token;
76-
while (std::getline(ss, token, delimiter)) {
77-
results->push_back(token);
78-
}
79-
}
80-
8171
// Though there can be multiple shells, some settings apply to all components in
8272
// the process. These have to be set up before the shell or any of its
8373
// sub-components can be initialized. In a perfect world, this would be empty.
@@ -103,13 +93,11 @@ void PerformInitializationTasks(Settings& settings) {
10393
[](const char* message) { FML_LOG(ERROR) << message; });
10494

10595
if (settings.trace_skia) {
106-
InitSkiaEventTracer(settings.trace_skia);
96+
InitSkiaEventTracer(settings.trace_skia, settings.trace_skia_allowlist);
10797
}
10898

10999
if (!settings.trace_allowlist.empty()) {
110-
std::vector<std::string> prefixes;
111-
Tokenize(settings.trace_allowlist, &prefixes, ',');
112-
fml::tracing::TraceSetAllowlist(prefixes);
100+
fml::tracing::TraceSetAllowlist(settings.trace_allowlist);
113101
}
114102

115103
if (!settings.skia_deterministic_rendering_on_cpu) {

shell/common/skia_event_tracer_impl.cc

Lines changed: 38 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
#include "flutter/shell/common/skia_event_tracer_impl.h"
66

77
#define TRACE_EVENT_HIDE_MACROS
8+
#include <map>
9+
#include <set>
810
#include <vector>
911

1012
#include "flutter/fml/logging.h"
@@ -42,6 +44,10 @@ namespace flutter {
4244

4345
namespace {
4446

47+
// Skia prepends this string to the category names of its trace events.
48+
// Defined in Skia's src/core/SkTraceEvent.h.
49+
constexpr std::string_view kTraceCategoryPrefix = "disabled-by-default-";
50+
4551
#if defined(OS_FUCHSIA)
4652
template <class T, class U>
4753
inline T BitCast(const U& u) {
@@ -61,7 +67,12 @@ class FlutterEventTracer : public SkEventTracer {
6167
static constexpr uint8_t kYes = 1;
6268
static constexpr uint8_t kNo = 0;
6369

64-
FlutterEventTracer(bool enabled) : enabled_(enabled ? kYes : kNo){};
70+
FlutterEventTracer(bool enabled, const std::vector<std::string>& allowlist)
71+
: enabled_(enabled ? kYes : kNo) {
72+
for (const std::string& category : allowlist) {
73+
allowlist_.insert(std::string(kTraceCategoryPrefix) + category);
74+
}
75+
};
6576

6677
SkEventTracer::Handle addTraceEvent(char phase,
6778
const uint8_t* category_enabled_flag,
@@ -221,39 +232,43 @@ class FlutterEventTracer : public SkEventTracer {
221232
}
222233

223234
const uint8_t* getCategoryGroupEnabled(const char* name) override {
224-
return &enabled_;
235+
// Skia will only use long-lived string literals as event names.
236+
auto flag_it = category_flag_map_.find(name);
237+
if (flag_it == category_flag_map_.end()) {
238+
bool allowed;
239+
if (enabled_) {
240+
allowed =
241+
allowlist_.empty() || allowlist_.find(name) != allowlist_.end();
242+
} else {
243+
allowed = false;
244+
}
245+
flag_it = category_flag_map_.insert(std::make_pair(name, allowed)).first;
246+
reverse_flag_map_.insert(std::make_pair(&flag_it->second, name));
247+
}
248+
return &flag_it->second;
225249
}
226250

227251
const char* getCategoryGroupName(
228252
const uint8_t* category_enabled_flag) override {
229-
return kSkiaTag;
253+
auto reverse_it = reverse_flag_map_.find(category_enabled_flag);
254+
if (reverse_it != reverse_flag_map_.end()) {
255+
return reverse_it->second;
256+
} else {
257+
return kSkiaTag;
258+
}
230259
}
231260

232-
void enable() { enabled_ = kYes; }
233-
234261
private:
235262
uint8_t enabled_;
263+
std::set<std::string> allowlist_;
264+
std::map<const char*, uint8_t> category_flag_map_;
265+
std::map<const uint8_t*, const char*> reverse_flag_map_;
236266
FML_DISALLOW_COPY_AND_ASSIGN(FlutterEventTracer);
237267
};
238268

239-
bool enableSkiaTracingCallback(const char* method,
240-
const char** param_keys,
241-
const char** param_values,
242-
intptr_t num_params,
243-
void* user_data,
244-
const char** json_object) {
245-
FlutterEventTracer* tracer = static_cast<FlutterEventTracer*>(user_data);
246-
tracer->enable();
247-
*json_object = fml::strdup("{\"type\":\"Success\"}");
248-
return true;
249-
}
250-
251-
void InitSkiaEventTracer(bool enabled) {
252-
// TODO(chinmaygarde): Leaked https://github.com/flutter/flutter/issues/30808.
253-
auto tracer = new FlutterEventTracer(enabled);
254-
Dart_RegisterRootServiceRequestCallback("_flutter.enableSkiaTracing",
255-
enableSkiaTracingCallback,
256-
static_cast<void*>(tracer));
269+
void InitSkiaEventTracer(bool enabled,
270+
const std::vector<std::string>& allowlist) {
271+
auto tracer = new FlutterEventTracer(enabled, allowlist);
257272
// Initialize the binding to Skia's tracing events. Skia will
258273
// take ownership of and clean up the memory allocated here.
259274
SkEventTracer::SetInstance(tracer);

shell/common/skia_event_tracer_impl.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,13 @@
55
#ifndef FLUTTER_SHELL_COMMON_SKIA_EVENT_TRACER_IMPL_H_
66
#define FLUTTER_SHELL_COMMON_SKIA_EVENT_TRACER_IMPL_H_
77

8+
#include <string>
9+
#include <vector>
10+
811
namespace flutter {
912

10-
void InitSkiaEventTracer(bool enabled);
13+
void InitSkiaEventTracer(bool enabled,
14+
const std::vector<std::string>& allowlist);
1115

1216
} // namespace flutter
1317

shell/common/switches.cc

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,16 @@ const std::string_view FlagForSwitch(Switch swtch) {
155155
return std::string_view();
156156
}
157157

158+
static std::vector<std::string> ParseCommaDelimited(const std::string& input) {
159+
std::istringstream ss(input);
160+
std::vector<std::string> result;
161+
std::string token;
162+
while (std::getline(ss, token, ',')) {
163+
result.push_back(token);
164+
}
165+
return result;
166+
}
167+
158168
static bool IsAllowedDartVMFlag(const std::string& flag) {
159169
for (uint32_t i = 0; i < fml::size(gAllowedDartFlags); ++i) {
160170
const std::string& allowed = gAllowedDartFlags[i];
@@ -289,17 +299,15 @@ Settings SettingsFromCommandLine(const fml::CommandLine& command_line) {
289299
settings.trace_skia =
290300
command_line.HasOption(FlagForSwitch(Switch::TraceSkia));
291301

292-
command_line.GetOptionValue(FlagForSwitch(Switch::TraceAllowlist),
293-
&settings.trace_allowlist);
302+
std::string trace_skia_allowlist;
303+
command_line.GetOptionValue(FlagForSwitch(Switch::TraceSkiaAllowlist),
304+
&trace_skia_allowlist);
305+
settings.trace_skia_allowlist = ParseCommaDelimited(trace_skia_allowlist);
294306

295-
if (settings.trace_allowlist.empty()) {
296-
command_line.GetOptionValue(FlagForSwitch(Switch::TraceWhitelist),
297-
&settings.trace_allowlist);
298-
if (!settings.trace_allowlist.empty()) {
299-
FML_LOG(INFO)
300-
<< "--trace-whitelist is deprecated. Use --trace-allowlist instead.";
301-
}
302-
}
307+
std::string trace_allowlist;
308+
command_line.GetOptionValue(FlagForSwitch(Switch::TraceAllowlist),
309+
&trace_allowlist);
310+
settings.trace_allowlist = ParseCommaDelimited(trace_allowlist);
303311

304312
settings.trace_systrace =
305313
command_line.HasOption(FlagForSwitch(Switch::TraceSystrace));
@@ -384,11 +392,9 @@ Settings SettingsFromCommandLine(const fml::CommandLine& command_line) {
384392
std::string all_dart_flags;
385393
if (command_line.GetOptionValue(FlagForSwitch(Switch::DartFlags),
386394
&all_dart_flags)) {
387-
std::stringstream stream(all_dart_flags);
388-
std::string flag;
389-
390395
// Assume that individual flags are comma separated.
391-
while (std::getline(stream, flag, ',')) {
396+
std::vector<std::string> flags = ParseCommaDelimited(all_dart_flags);
397+
for (auto flag : flags) {
392398
if (!IsAllowedDartVMFlag(flag)) {
393399
FML_LOG(FATAL) << "Encountered disallowed Dart VM flag: " << flag;
394400
}

shell/common/switches.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -132,9 +132,10 @@ DEF_SWITCH(TraceSkia,
132132
"Trace Skia calls. This is useful when debugging the GPU threed."
133133
"By default, Skia tracing is not enabled to reduce the number of "
134134
"traced events")
135-
DEF_SWITCH(TraceWhitelist,
136-
"trace-whitelist",
137-
"(deprecated) Use --trace-allowlist instead.")
135+
DEF_SWITCH(TraceSkiaAllowlist,
136+
"trace-skia-allowlist",
137+
"Filters out all Skia trace event categories except those that are "
138+
"specified in this comma separated list.")
138139
DEF_SWITCH(
139140
TraceAllowlist,
140141
"trace-allowlist",

0 commit comments

Comments
 (0)