From 27d018d4982e4457002843c0d61d6c9674eef5a2 Mon Sep 17 00:00:00 2001 From: Jason Simmons Date: Tue, 27 Jul 2021 14:34:02 -0700 Subject: [PATCH] Log all Skia trace events if the --trace-skia flag is passed (flutter/engine#27680) --- .../ci/licenses_golden/licenses_flutter | 1 + engine/src/flutter/common/settings.h | 2 +- engine/src/flutter/shell/common/BUILD.gn | 1 + .../shell/common/skia_event_tracer_impl.cc | 21 +++++--- .../shell/common/skia_event_tracer_impl.h | 6 ++- engine/src/flutter/shell/common/switches.cc | 17 ++++--- .../shell/common/switches_unittests.cc | 50 +++++++++++++++++++ 7 files changed, 81 insertions(+), 17 deletions(-) create mode 100644 engine/src/flutter/shell/common/switches_unittests.cc diff --git a/engine/src/flutter/ci/licenses_golden/licenses_flutter b/engine/src/flutter/ci/licenses_golden/licenses_flutter index 7c768aa0dc..22347dc532 100755 --- a/engine/src/flutter/ci/licenses_golden/licenses_flutter +++ b/engine/src/flutter/ci/licenses_golden/licenses_flutter @@ -731,6 +731,7 @@ FILE: ../../../flutter/shell/common/skp_shader_warmup_unittests.cc FILE: ../../../flutter/shell/common/snapshot_surface_producer.h FILE: ../../../flutter/shell/common/switches.cc FILE: ../../../flutter/shell/common/switches.h +FILE: ../../../flutter/shell/common/switches_unittests.cc FILE: ../../../flutter/shell/common/thread_host.cc FILE: ../../../flutter/shell/common/thread_host.h FILE: ../../../flutter/shell/common/vsync_waiter.cc diff --git a/engine/src/flutter/common/settings.h b/engine/src/flutter/common/settings.h index 89310ed07d..c6289d92e2 100644 --- a/engine/src/flutter/common/settings.h +++ b/engine/src/flutter/common/settings.h @@ -111,7 +111,7 @@ struct Settings { bool start_paused = false; bool trace_skia = false; std::vector trace_allowlist; - std::vector trace_skia_allowlist; + std::optional> trace_skia_allowlist; bool trace_startup = false; bool trace_systrace = false; bool dump_skp_on_shader_compilation = false; diff --git a/engine/src/flutter/shell/common/BUILD.gn b/engine/src/flutter/shell/common/BUILD.gn index dcb28e800b..071a9ed29e 100644 --- a/engine/src/flutter/shell/common/BUILD.gn +++ b/engine/src/flutter/shell/common/BUILD.gn @@ -259,6 +259,7 @@ if (enable_unittests) { "rasterizer_unittests.cc", "shell_unittests.cc", "skp_shader_warmup_unittests.cc", + "switches_unittests.cc", ] deps = [ diff --git a/engine/src/flutter/shell/common/skia_event_tracer_impl.cc b/engine/src/flutter/shell/common/skia_event_tracer_impl.cc index 7b70442883..e3120db25a 100644 --- a/engine/src/flutter/shell/common/skia_event_tracer_impl.cc +++ b/engine/src/flutter/shell/common/skia_event_tracer_impl.cc @@ -79,10 +79,14 @@ class FlutterEventTracer : public SkEventTracer { static constexpr uint8_t kYes = 1; static constexpr uint8_t kNo = 0; - FlutterEventTracer(bool enabled, const std::vector& allowlist) + FlutterEventTracer(bool enabled, + const std::optional>& allowlist) : enabled_(enabled ? kYes : kNo), shaders_category_flag_(nullptr) { - for (const std::string& category : allowlist) { - allowlist_.insert(std::string(kTraceCategoryPrefix) + category); + if (allowlist.has_value()) { + allowlist_.emplace(); + for (const std::string& category : *allowlist) { + allowlist_->insert(std::string(kTraceCategoryPrefix) + category); + } } }; @@ -274,8 +278,8 @@ class FlutterEventTracer : public SkEventTracer { if (flag_it == category_flag_map_.end()) { bool allowed; if (enabled_) { - allowed = - allowlist_.empty() || allowlist_.find(name) != allowlist_.end(); + allowed = !allowlist_.has_value() || + allowlist_->find(name) != allowlist_->end(); } else { allowed = false; } @@ -301,15 +305,16 @@ class FlutterEventTracer : public SkEventTracer { private: uint8_t enabled_; - std::set allowlist_; + std::optional> allowlist_; std::map category_flag_map_; std::map reverse_flag_map_; const uint8_t* shaders_category_flag_; FML_DISALLOW_COPY_AND_ASSIGN(FlutterEventTracer); }; -void InitSkiaEventTracer(bool enabled, - const std::vector& allowlist) { +void InitSkiaEventTracer( + bool enabled, + const std::optional>& allowlist) { auto tracer = new FlutterEventTracer(enabled, allowlist); // Initialize the binding to Skia's tracing events. Skia will // take ownership of and clean up the memory allocated here. diff --git a/engine/src/flutter/shell/common/skia_event_tracer_impl.h b/engine/src/flutter/shell/common/skia_event_tracer_impl.h index cdd66f095d..9154f1fd37 100644 --- a/engine/src/flutter/shell/common/skia_event_tracer_impl.h +++ b/engine/src/flutter/shell/common/skia_event_tracer_impl.h @@ -5,13 +5,15 @@ #ifndef FLUTTER_SHELL_COMMON_SKIA_EVENT_TRACER_IMPL_H_ #define FLUTTER_SHELL_COMMON_SKIA_EVENT_TRACER_IMPL_H_ +#include #include #include namespace flutter { -void InitSkiaEventTracer(bool enabled, - const std::vector& allowlist); +void InitSkiaEventTracer( + bool enabled, + const std::optional>& allowlist); } // namespace flutter diff --git a/engine/src/flutter/shell/common/switches.cc b/engine/src/flutter/shell/common/switches.cc index d49f99bda1..5c767fc20a 100644 --- a/engine/src/flutter/shell/common/switches.cc +++ b/engine/src/flutter/shell/common/switches.cc @@ -299,13 +299,18 @@ Settings SettingsFromCommandLine(const fml::CommandLine& command_line) { #if !FLUTTER_RELEASE settings.trace_skia = true; - std::string trace_skia_allowlist; - command_line.GetOptionValue(FlagForSwitch(Switch::TraceSkiaAllowlist), - &trace_skia_allowlist); - if (trace_skia_allowlist.size()) { - settings.trace_skia_allowlist = ParseCommaDelimited(trace_skia_allowlist); + if (command_line.HasOption(FlagForSwitch(Switch::TraceSkia))) { + // If --trace-skia is specified, then log all Skia events. + settings.trace_skia_allowlist.reset(); } else { - settings.trace_skia_allowlist = {"skia.shaders"}; + std::string trace_skia_allowlist; + command_line.GetOptionValue(FlagForSwitch(Switch::TraceSkiaAllowlist), + &trace_skia_allowlist); + if (trace_skia_allowlist.size()) { + settings.trace_skia_allowlist = ParseCommaDelimited(trace_skia_allowlist); + } else { + settings.trace_skia_allowlist = {"skia.shaders"}; + } } #endif // !FLUTTER_RELEASE diff --git a/engine/src/flutter/shell/common/switches_unittests.cc b/engine/src/flutter/shell/common/switches_unittests.cc new file mode 100644 index 0000000000..686a159606 --- /dev/null +++ b/engine/src/flutter/shell/common/switches_unittests.cc @@ -0,0 +1,50 @@ +// Copyright 2013 The Flutter Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "flutter/common/settings.h" +#include "flutter/fml/command_line.h" +#include "flutter/shell/common/switches.h" + +#include "gtest/gtest.h" + +namespace flutter { +namespace testing { + +TEST(SwitchesTest, SkiaTraceAllowlistFlag) { + fml::CommandLine command_line = + fml::CommandLineFromInitializerList({"command"}); + Settings settings = SettingsFromCommandLine(command_line); +#if !FLUTTER_RELEASE + EXPECT_TRUE(settings.trace_skia); + EXPECT_TRUE(settings.trace_skia_allowlist.has_value()); + EXPECT_EQ(settings.trace_skia_allowlist->size(), 1ul); +#else + EXPECT_FALSE(settings.trace_skia); +#endif + + command_line = + fml::CommandLineFromInitializerList({"command", "--trace-skia"}); + settings = SettingsFromCommandLine(command_line); +#if !FLUTTER_RELEASE + EXPECT_TRUE(settings.trace_skia); + EXPECT_FALSE(settings.trace_skia_allowlist.has_value()); +#else + EXPECT_FALSE(settings.trace_skia); +#endif + + command_line = fml::CommandLineFromInitializerList( + {"command", "--trace-skia-allowlist=aaa,bbb,ccc"}); + settings = SettingsFromCommandLine(command_line); +#if !FLUTTER_RELEASE + EXPECT_TRUE(settings.trace_skia); + EXPECT_TRUE(settings.trace_skia_allowlist.has_value()); + EXPECT_EQ(settings.trace_skia_allowlist->size(), 3ul); + EXPECT_EQ(settings.trace_skia_allowlist->back(), "ccc"); +#else + EXPECT_FALSE(settings.trace_skia); +#endif +} + +} // namespace testing +} // namespace flutter