processor: Extract graphics event parsing into an importer module

Moves GraphicsEventParser into importers/proto/ and proxies calls to it
via a new GraphicsEventModule, so it can optionally be disabled at
compile time.

Bug: 141459049
Change-Id: If3ea515d0869026b73570f1865073fa3f821ab2d
diff --git a/Android.bp b/Android.bp
index e7d12ee..4f8e2df 100644
--- a/Android.bp
+++ b/Android.bp
@@ -4546,7 +4546,6 @@
     "src/trace_processor/filtered_row_index.cc",
     "src/trace_processor/forwarding_trace_parser.cc",
     "src/trace_processor/ftrace_utils.cc",
-    "src/trace_processor/graphics_event_parser.cc",
     "src/trace_processor/gzip_trace_parser.cc",
     "src/trace_processor/heap_graph_tracker.cc",
     "src/trace_processor/heap_profile_allocation_table.cc",
@@ -4559,6 +4558,7 @@
     "src/trace_processor/importers/fuchsia/fuchsia_trace_parser.cc",
     "src/trace_processor/importers/fuchsia/fuchsia_trace_tokenizer.cc",
     "src/trace_processor/importers/fuchsia/fuchsia_trace_utils.cc",
+    "src/trace_processor/importers/proto/graphics_event_parser.cc",
     "src/trace_processor/importers/proto/track_event_parser.cc",
     "src/trace_processor/importers/proto/track_event_tokenizer.cc",
     "src/trace_processor/importers/systrace/systrace_parser.cc",
diff --git a/BUILD b/BUILD
index 0965d30..d646d2c 100644
--- a/BUILD
+++ b/BUILD
@@ -644,8 +644,6 @@
         "src/trace_processor/forwarding_trace_parser.h",
         "src/trace_processor/ftrace_utils.cc",
         "src/trace_processor/ftrace_utils.h",
-        "src/trace_processor/graphics_event_parser.cc",
-        "src/trace_processor/graphics_event_parser.h",
         "src/trace_processor/gzip_trace_parser.cc",
         "src/trace_processor/gzip_trace_parser.h",
         "src/trace_processor/heap_graph_tracker.cc",
@@ -677,6 +675,9 @@
         "src/trace_processor/importers/json/json_trace_tokenizer.h",
         "src/trace_processor/importers/json/json_trace_utils.cc",
         "src/trace_processor/importers/json/json_trace_utils.h",
+        "src/trace_processor/importers/proto/graphics_event_module.h",
+        "src/trace_processor/importers/proto/graphics_event_parser.cc",
+        "src/trace_processor/importers/proto/graphics_event_parser.h",
         "src/trace_processor/importers/proto/packet_sequence_state.h",
         "src/trace_processor/importers/proto/proto_importer_module.h",
         "src/trace_processor/importers/proto/proto_incremental_state.h",
diff --git a/gn/BUILD.gn b/gn/BUILD.gn
index 5340b14..0fe11a9 100644
--- a/gn/BUILD.gn
+++ b/gn/BUILD.gn
@@ -74,6 +74,7 @@
     "PERFETTO_TP_LINENOISE=$enable_perfetto_trace_processor_linenoise",
     "PERFETTO_TP_METRICS=$enable_perfetto_trace_processor_metrics",
     "PERFETTO_TP_FTRACE=$enable_perfetto_trace_processor_ftrace",
+    "PERFETTO_TP_GRAPHICS=$enable_perfetto_trace_processor_graphics",
     "PERFETTO_TP_JSON=$enable_perfetto_trace_processor_json",
     "PERFETTO_TP_FUCHSIA=$enable_perfetto_trace_processor_fuchsia",
     "PERFETTO_LOCAL_SYMBOLIZER=$perfetto_local_symbolizer",
diff --git a/gn/perfetto.gni b/gn/perfetto.gni
index c4fa398..3d1b5d1 100644
--- a/gn/perfetto.gni
+++ b/gn/perfetto.gni
@@ -214,6 +214,10 @@
   enable_perfetto_trace_processor_ftrace =
       enable_perfetto_trace_processor && !build_with_chromium
 
+  # Enables graphics event support in the trace processor.
+  enable_perfetto_trace_processor_graphics =
+      enable_perfetto_trace_processor && !build_with_chromium
+
   # Enables JSON support in the trace processor.
   enable_perfetto_trace_processor_json =
       enable_perfetto_trace_processor && !perfetto_build_with_android
diff --git a/include/perfetto/base/build_configs/android_tree/perfetto_build_flags.h b/include/perfetto/base/build_configs/android_tree/perfetto_build_flags.h
index 2fa7fa2..f3407cd 100644
--- a/include/perfetto/base/build_configs/android_tree/perfetto_build_flags.h
+++ b/include/perfetto/base/build_configs/android_tree/perfetto_build_flags.h
@@ -19,6 +19,7 @@
 #define PERFETTO_BUILDFLAG_DEFINE_PERFETTO_TP_LINENOISE() (0)
 #define PERFETTO_BUILDFLAG_DEFINE_PERFETTO_TP_METRICS() (1)
 #define PERFETTO_BUILDFLAG_DEFINE_PERFETTO_TP_FTRACE() (1)
+#define PERFETTO_BUILDFLAG_DEFINE_PERFETTO_TP_GRAPHICS() (1)
 #define PERFETTO_BUILDFLAG_DEFINE_PERFETTO_TP_JSON() (0)
 #define PERFETTO_BUILDFLAG_DEFINE_PERFETTO_TP_FUCHSIA() (1)
 #define PERFETTO_BUILDFLAG_DEFINE_PERFETTO_LOCAL_SYMBOLIZER() (PERFETTO_BUILDFLAG_DEFINE_PERFETTO_OS_LINUX())
diff --git a/include/perfetto/base/build_configs/bazel/perfetto_build_flags.h b/include/perfetto/base/build_configs/bazel/perfetto_build_flags.h
index 12fdb96..8c22b95 100644
--- a/include/perfetto/base/build_configs/bazel/perfetto_build_flags.h
+++ b/include/perfetto/base/build_configs/bazel/perfetto_build_flags.h
@@ -19,6 +19,7 @@
 #define PERFETTO_BUILDFLAG_DEFINE_PERFETTO_TP_LINENOISE() (1)
 #define PERFETTO_BUILDFLAG_DEFINE_PERFETTO_TP_METRICS() (1)
 #define PERFETTO_BUILDFLAG_DEFINE_PERFETTO_TP_FTRACE() (1)
+#define PERFETTO_BUILDFLAG_DEFINE_PERFETTO_TP_GRAPHICS() (1)
 #define PERFETTO_BUILDFLAG_DEFINE_PERFETTO_TP_JSON() (1)
 #define PERFETTO_BUILDFLAG_DEFINE_PERFETTO_TP_FUCHSIA() (1)
 #define PERFETTO_BUILDFLAG_DEFINE_PERFETTO_LOCAL_SYMBOLIZER() (PERFETTO_BUILDFLAG_DEFINE_PERFETTO_OS_LINUX())
diff --git a/src/trace_processor/BUILD.gn b/src/trace_processor/BUILD.gn
index b1905e9..8f39e85 100644
--- a/src/trace_processor/BUILD.gn
+++ b/src/trace_processor/BUILD.gn
@@ -72,8 +72,6 @@
     "forwarding_trace_parser.cc",
     "forwarding_trace_parser.h",
     "ftrace_utils.h",
-    "graphics_event_parser.cc",
-    "graphics_event_parser.h",
     "gzip_trace_parser.cc",
     "gzip_trace_parser.h",
     "heap_graph_tracker.cc",
@@ -88,6 +86,8 @@
     "importers/ftrace/ftrace_tokenizer.h",
     "importers/ftrace/sched_event_tracker.h",
     "importers/fuchsia/fuchsia_provider_view.h",
+    "importers/proto/graphics_event_module.h",
+    "importers/proto/graphics_event_parser.h",
     "importers/proto/packet_sequence_state.h",
     "importers/proto/proto_importer_module.h",
     "importers/proto/proto_incremental_state.h",
@@ -229,6 +229,9 @@
       enable_perfetto_trace_processor_fuchsia) {
     sources += [ "ftrace_utils.cc" ]
   }
+  if (enable_perfetto_trace_processor_graphics) {
+    sources += [ "importers/proto/graphics_event_parser.cc" ]
+  }
   if (enable_perfetto_trace_processor_metrics) {
     deps += [
       "../../protos/perfetto/metrics:zero",
diff --git a/src/trace_processor/importers/proto/graphics_event_module.h b/src/trace_processor/importers/proto/graphics_event_module.h
new file mode 100644
index 0000000..b336398
--- /dev/null
+++ b/src/trace_processor/importers/proto/graphics_event_module.h
@@ -0,0 +1,73 @@
+/*
+ * Copyright (C) 2019 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef SRC_TRACE_PROCESSOR_IMPORTERS_PROTO_GRAPHICS_EVENT_MODULE_H_
+#define SRC_TRACE_PROCESSOR_IMPORTERS_PROTO_GRAPHICS_EVENT_MODULE_H_
+
+#include "perfetto/base/build_config.h"
+#include "src/trace_processor/importers/proto/graphics_event_parser.h"
+#include "src/trace_processor/importers/proto/proto_importer_module.h"
+#include "src/trace_processor/timestamped_trace_piece.h"
+
+namespace perfetto {
+namespace trace_processor {
+
+class GraphicsEventModule
+    : public ProtoImporterModuleBase<PERFETTO_BUILDFLAG(PERFETTO_TP_GRAPHICS)> {
+ public:
+  explicit GraphicsEventModule(TraceProcessorContext* context)
+      : ProtoImporterModuleBase(context), parser_(context) {}
+
+  ModuleResult ParsePacket(const protos::pbzero::TracePacket::Decoder& decoder,
+                           const TimestampedTracePiece& ttp) {
+    if (decoder.has_gpu_counter_event()) {
+      parser_.ParseGpuCounterEvent(ttp.timestamp, decoder.gpu_counter_event());
+      return ModuleResult::Handled();
+    }
+
+    if (decoder.has_gpu_render_stage_event()) {
+      parser_.ParseGpuRenderStageEvent(ttp.timestamp,
+                                       decoder.gpu_render_stage_event());
+      return ModuleResult::Handled();
+    }
+
+    if (decoder.has_gpu_log()) {
+      parser_.ParseGpuLog(ttp.timestamp, decoder.gpu_log());
+      return ModuleResult::Handled();
+    }
+
+    if (decoder.has_graphics_frame_event()) {
+      parser_.ParseGraphicsFrameEvent(ttp.timestamp,
+                                      decoder.graphics_frame_event());
+      return ModuleResult::Handled();
+    }
+
+    if (decoder.has_vulkan_memory_event()) {
+      parser_.ParseVulkanMemoryEvent(decoder.vulkan_memory_event());
+      return ModuleResult::Handled();
+    }
+
+    return ModuleResult::Ignored();
+  }
+
+ private:
+  GraphicsEventParser parser_;
+};
+
+}  // namespace trace_processor
+}  // namespace perfetto
+
+#endif  // SRC_TRACE_PROCESSOR_IMPORTERS_PROTO_GRAPHICS_EVENT_MODULE_H_
diff --git a/src/trace_processor/graphics_event_parser.cc b/src/trace_processor/importers/proto/graphics_event_parser.cc
similarity index 99%
rename from src/trace_processor/graphics_event_parser.cc
rename to src/trace_processor/importers/proto/graphics_event_parser.cc
index 3ab5dee..17909bd 100644
--- a/src/trace_processor/graphics_event_parser.cc
+++ b/src/trace_processor/importers/proto/graphics_event_parser.cc
@@ -14,7 +14,7 @@
  * limitations under the License.
  */
 
-#include "src/trace_processor/graphics_event_parser.h"
+#include "src/trace_processor/importers/proto/graphics_event_parser.h"
 
 #include "perfetto/protozero/field.h"
 #include "src/trace_processor/args_tracker.h"
@@ -35,8 +35,6 @@
 namespace perfetto {
 namespace trace_processor {
 
-GraphicsEventParser::~GraphicsEventParser() = default;
-
 GraphicsEventParser::GraphicsEventParser(TraceProcessorContext* context)
     : context_(context),
       gpu_render_stage_scope_id_(
diff --git a/src/trace_processor/graphics_event_parser.h b/src/trace_processor/importers/proto/graphics_event_parser.h
similarity index 92%
rename from src/trace_processor/graphics_event_parser.h
rename to src/trace_processor/importers/proto/graphics_event_parser.h
index 8cac922..25ab411 100644
--- a/src/trace_processor/graphics_event_parser.h
+++ b/src/trace_processor/importers/proto/graphics_event_parser.h
@@ -14,8 +14,8 @@
  * limitations under the License.
  */
 
-#ifndef SRC_TRACE_PROCESSOR_GRAPHICS_EVENT_PARSER_H_
-#define SRC_TRACE_PROCESSOR_GRAPHICS_EVENT_PARSER_H_
+#ifndef SRC_TRACE_PROCESSOR_IMPORTERS_PROTO_GRAPHICS_EVENT_PARSER_H_
+#define SRC_TRACE_PROCESSOR_IMPORTERS_PROTO_GRAPHICS_EVENT_PARSER_H_
 
 #include <vector>
 
@@ -32,7 +32,6 @@
  public:
   using ConstBytes = protozero::ConstBytes;
   explicit GraphicsEventParser(TraceProcessorContext*);
-  ~GraphicsEventParser();
 
   void ParseGpuCounterEvent(int64_t ts, ConstBytes);
   void ParseGpuRenderStageEvent(int64_t ts, ConstBytes);
@@ -81,4 +80,4 @@
 }  // namespace trace_processor
 }  // namespace perfetto
 
-#endif  // SRC_TRACE_PROCESSOR_GRAPHICS_EVENT_PARSER_H_
+#endif  // SRC_TRACE_PROCESSOR_IMPORTERS_PROTO_GRAPHICS_EVENT_PARSER_H_
diff --git a/src/trace_processor/proto_trace_parser.cc b/src/trace_processor/proto_trace_parser.cc
index 98ea5f5..8035336 100644
--- a/src/trace_processor/proto_trace_parser.cc
+++ b/src/trace_processor/proto_trace_parser.cc
@@ -36,6 +36,7 @@
 #include "src/trace_processor/heap_graph_tracker.h"
 #include "src/trace_processor/heap_profile_tracker.h"
 #include "src/trace_processor/importers/ftrace/ftrace_module.h"
+#include "src/trace_processor/importers/proto/graphics_event_module.h"
 #include "src/trace_processor/importers/proto/packet_sequence_state.h"
 #include "src/trace_processor/importers/proto/track_event_module.h"
 #include "src/trace_processor/importers/systrace/systrace_parser.h"
@@ -223,7 +224,6 @@
 
 ProtoTraceParser::ProtoTraceParser(TraceProcessorContext* context)
     : context_(context),
-      graphics_event_parser_(new GraphicsEventParser(context_)),
       utid_name_id_(context->storage->InternString("utid")),
       num_forks_name_id_(context->storage->InternString("num_forks")),
       num_irq_total_name_id_(context->storage->InternString("num_irq_total")),
@@ -318,6 +318,9 @@
   if (!context_->track_event_module->ParsePacket(packet, ttp).ignored())
     return;
 
+  if (!context_->graphics_event_module->ParsePacket(packet, ttp).ignored())
+    return;
+
   if (packet.has_process_tree())
     ParseProcessTree(packet.process_tree());
 
@@ -366,33 +369,14 @@
     ParseMetatraceEvent(ts, packet.perfetto_metatrace());
   }
 
-  if (packet.has_gpu_counter_event()) {
-    graphics_event_parser_->ParseGpuCounterEvent(ts,
-                                                 packet.gpu_counter_event());
-  }
-
-  if (packet.has_gpu_render_stage_event()) {
-    graphics_event_parser_->ParseGpuRenderStageEvent(
-        ts, packet.gpu_render_stage_event());
-  }
-
   if (packet.has_trace_config()) {
     ParseTraceConfig(packet.trace_config());
   }
 
-  if (packet.has_gpu_log()) {
-    graphics_event_parser_->ParseGpuLog(ts, packet.gpu_log());
-  }
-
   if (packet.has_packages_list()) {
     ParseAndroidPackagesList(packet.packages_list());
   }
 
-  if (packet.has_graphics_frame_event()) {
-    graphics_event_parser_->ParseGraphicsFrameEvent(
-        ts, packet.graphics_frame_event());
-  }
-
   if (packet.has_module_symbols()) {
     ParseModuleSymbols(packet.module_symbols());
   }
@@ -400,11 +384,6 @@
   if (packet.has_heap_graph()) {
     ParseHeapGraph(ts, packet.heap_graph());
   }
-
-  if (packet.has_vulkan_memory_event()) {
-    graphics_event_parser_->ParseVulkanMemoryEvent(
-        packet.vulkan_memory_event());
-  }
 }
 
 void ProtoTraceParser::ParseSysStats(int64_t ts, ConstBytes blob) {
diff --git a/src/trace_processor/proto_trace_parser.h b/src/trace_processor/proto_trace_parser.h
index 8e52c8a..4413980 100644
--- a/src/trace_processor/proto_trace_parser.h
+++ b/src/trace_processor/proto_trace_parser.h
@@ -25,7 +25,6 @@
 #include "perfetto/ext/base/optional.h"
 #include "perfetto/ext/base/string_view.h"
 #include "perfetto/protozero/field.h"
-#include "src/trace_processor/graphics_event_parser.h"
 #include "src/trace_processor/timestamped_trace_piece.h"
 #include "src/trace_processor/trace_blob_view.h"
 #include "src/trace_processor/trace_parser.h"
@@ -84,7 +83,6 @@
 
  private:
   TraceProcessorContext* context_;
-  std::unique_ptr<GraphicsEventParser> graphics_event_parser_;
 
   const StringId utid_name_id_;
   const StringId num_forks_name_id_;
diff --git a/src/trace_processor/proto_trace_parser_unittest.cc b/src/trace_processor/proto_trace_parser_unittest.cc
index 387fb38..1cfe4f0 100644
--- a/src/trace_processor/proto_trace_parser_unittest.cc
+++ b/src/trace_processor/proto_trace_parser_unittest.cc
@@ -24,6 +24,7 @@
 #include "src/trace_processor/event_tracker.h"
 #include "src/trace_processor/importers/ftrace/ftrace_module.h"
 #include "src/trace_processor/importers/ftrace/sched_event_tracker.h"
+#include "src/trace_processor/importers/proto/graphics_event_module.h"
 #include "src/trace_processor/importers/proto/proto_importer_module.h"
 #include "src/trace_processor/importers/proto/track_event_module.h"
 #include "src/trace_processor/importers/systrace/systrace_parser.h"
@@ -255,6 +256,8 @@
         new ProtoImporterModule<FtraceModule>(&context_));
     context_.track_event_module.reset(
         new ProtoImporterModule<TrackEventModule>(&context_));
+    context_.graphics_event_module.reset(
+        new ProtoImporterModule<GraphicsEventModule>(&context_));
   }
 
   void ResetTraceBuffers() {
diff --git a/src/trace_processor/trace_processor_context.cc b/src/trace_processor/trace_processor_context.cc
index e5bd1ca..b40285e 100644
--- a/src/trace_processor/trace_processor_context.cc
+++ b/src/trace_processor/trace_processor_context.cc
@@ -25,6 +25,7 @@
 #include "src/trace_processor/importers/ftrace/ftrace_module.h"
 #include "src/trace_processor/importers/ftrace/sched_event_tracker.h"
 #include "src/trace_processor/importers/json/json_trace_parser.h"
+#include "src/trace_processor/importers/proto/graphics_event_module.h"
 #include "src/trace_processor/importers/proto/track_event_module.h"
 #include "src/trace_processor/importers/systrace/systrace_parser.h"
 #include "src/trace_processor/process_tracker.h"
diff --git a/src/trace_processor/trace_processor_context.h b/src/trace_processor/trace_processor_context.h
index 2f6c9db..4cd8f42 100644
--- a/src/trace_processor/trace_processor_context.h
+++ b/src/trace_processor/trace_processor_context.h
@@ -30,9 +30,9 @@
 class ClockTracker;
 class EventTracker;
 class FtraceModule;
+class GraphicsEventModule;
 class HeapGraphTracker;
 class HeapProfileTracker;
-class VulkanMemoryTracker;
 class ProcessTracker;
 class SchedEventTracker;
 class SliceTracker;
@@ -43,6 +43,7 @@
 class TraceSorter;
 class TrackEventModule;
 class TrackTracker;
+class VulkanMemoryTracker;
 
 class TraceProcessorContext {
  public:
@@ -70,6 +71,8 @@
 
   std::unique_ptr<ProtoImporterModule<FtraceModule>> ftrace_module;
   std::unique_ptr<ProtoImporterModule<TrackEventModule>> track_event_module;
+  std::unique_ptr<ProtoImporterModule<GraphicsEventModule>>
+      graphics_event_module;
 };
 
 }  // namespace trace_processor
diff --git a/src/trace_processor/trace_processor_impl.cc b/src/trace_processor/trace_processor_impl.cc
index eeec130..bcc18cf 100644
--- a/src/trace_processor/trace_processor_impl.cc
+++ b/src/trace_processor/trace_processor_impl.cc
@@ -40,6 +40,7 @@
 #include "src/trace_processor/heap_profile_tracker.h"
 #include "src/trace_processor/importers/ftrace/ftrace_module.h"
 #include "src/trace_processor/importers/ftrace/sched_event_tracker.h"
+#include "src/trace_processor/importers/proto/graphics_event_module.h"
 #include "src/trace_processor/importers/proto/proto_importer_module.h"
 #include "src/trace_processor/importers/proto/track_event_module.h"
 #include "src/trace_processor/importers/systrace/systrace_parser.h"
@@ -345,6 +346,8 @@
       new ProtoImporterModule<FtraceModule>(&context_));
   context_.track_event_module.reset(
       new ProtoImporterModule<TrackEventModule>(&context_));
+  context_.graphics_event_module.reset(
+      new ProtoImporterModule<GraphicsEventModule>(&context_));
 
 #if PERFETTO_BUILDFLAG(PERFETTO_TP_JSON)
   CreateJsonExportFunction(this->context_.storage.get(), db);