Convert decoder TRACE_EVENT to flows

This is the first new style trace event so this CL adds and updates
WebRTCs Perfetto configuration.

* Changes all #includes to target "third_party/perfetto". Added this
to DEPS.
* Expose the Perfetto public config in the "tracing" group using
an all_dependent_configs statement. This means the config is included
in all parts that include the "//:tracing" group. However, direct
perfetto includes are banned per DEPS.
* In order to expose Perfetto types (ie Flow/TerminatingFlow) the
perfetto headers are a dependancy on all targets. This should not
affect binary size as these are not used when perfetto is not enabled
and will not be linked.

Bug: b/42226290
Change-Id: I5711d81dae95ee907cbcd41bf1ee9b55d2ec595c
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/349161
Reviewed-by: Harald Alvestrand <hta@webrtc.org>
Reviewed-by: Mirko Bonadei <mbonadei@webrtc.org>
Commit-Queue: Evan Shrubsole <eshr@google.com>
Cr-Commit-Position: refs/heads/main@{#42197}
diff --git a/BUILD.gn b/BUILD.gn
index eb9ad83..f090f16 100644
--- a/BUILD.gn
+++ b/BUILD.gn
@@ -272,26 +272,29 @@
 }
 
 group("tracing") {
+  all_dependent_configs = [ "//third_party/perfetto/gn:public_config" ]
   if (rtc_use_perfetto) {
     if (build_with_chromium) {
       public_deps =  # no-presubmit-check TODO(webrtc:8603)
           [ "//third_party/perfetto:libperfetto" ]
     } else {
-      public_deps =  # no-presubmit-check TODO(webrtc:8603)
-          [ ":webrtc_libperfetto" ]
+      public_deps = [  # no-presubmit-check TODO(webrtc:8603)
+        ":webrtc_libperfetto",
+        "//third_party/perfetto/include/perfetto/tracing",
+      ]
     }
+  } else {
+    public_deps =  # no-presubmit-check TODO(webrtc:8603)
+        [ "//third_party/perfetto/include/perfetto/tracing" ]
   }
 }
 
 if (rtc_use_perfetto) {
   rtc_library("webrtc_libperfetto") {
-    public_configs = [ "//third_party/perfetto/gn:public_config" ]
     deps = [
       "//third_party/perfetto/src/tracing:client_api_without_backends",
       "//third_party/perfetto/src/tracing:platform_impl",
     ]
-    public_deps +=  # no-presubmit-check TODO(webrtc:8603)
-        [ "//third_party/perfetto/include/perfetto/tracing" ]
   }
 }
 
diff --git a/DEPS b/DEPS
index 18145ea..b8e5b51 100644
--- a/DEPS
+++ b/DEPS
@@ -2669,9 +2669,10 @@
   # Abseil flags are allowed in tests and tools.
   "+absl/flags",
 
-  # Perfetto
-  '+third_party/perfetto/include/perfetto/tracing',
-  '+third_party/perfetto/include/perfetto/test',
+  # Perfetto should be used through rtc_base/trace_event.h
+  '-third_party/perfetto',
+  '-perfetto',
+  '-protos/perfetto',
 ]
 
 specific_include_rules = {
diff --git a/modules/video_coding/generic_decoder.cc b/modules/video_coding/generic_decoder.cc
index 0a9920a..0bfc628 100644
--- a/modules/video_coding/generic_decoder.cc
+++ b/modules/video_coding/generic_decoder.cc
@@ -103,9 +103,9 @@
                                       absl::optional<int32_t> decode_time_ms,
                                       absl::optional<uint8_t> qp) {
   RTC_DCHECK(_receiveCallback) << "Callback must not be null at this point";
-  TRACE_EVENT_INSTANT1("webrtc", "VCMDecodedFrameCallback::Decoded",
-                       TRACE_EVENT_SCOPE_GLOBAL, "timestamp",
-                       decodedImage.rtp_timestamp());
+  TRACE_EVENT(
+      "webrtc", "VCMDecodedFrameCallback::Decoded",
+      perfetto::TerminatingFlow::ProcessScoped(decodedImage.rtp_timestamp()));
   // TODO(holmer): We should improve this so that we can handle multiple
   // callbacks from one call to Decode().
   absl::optional<FrameInfo> frame_info;
@@ -293,8 +293,8 @@
 int32_t VCMGenericDecoder::Decode(const EncodedImage& frame,
                                   Timestamp now,
                                   int64_t render_time_ms) {
-  TRACE_EVENT1("webrtc", "VCMGenericDecoder::Decode", "timestamp",
-               frame.RtpTimestamp());
+  TRACE_EVENT("webrtc", "VCMGenericDecoder::Decode",
+              perfetto::Flow::ProcessScoped(frame.RtpTimestamp()));
   FrameInfo frame_info;
   frame_info.rtp_timestamp = frame.RtpTimestamp();
   frame_info.decode_start = now;
diff --git a/rtc_base/BUILD.gn b/rtc_base/BUILD.gn
index f761c36..b6e1697 100644
--- a/rtc_base/BUILD.gn
+++ b/rtc_base/BUILD.gn
@@ -210,6 +210,7 @@
     ":platform_thread_types",
     ":rtc_event",
     ":timeutils",
+    "..:tracing",
     "../api:sequence_checker",
     "synchronization:mutex",
     "system:rtc_export",
@@ -220,7 +221,6 @@
       "trace_categories.cc",
       "trace_categories.h",
     ]
-    deps += [ "..:tracing" ]
   }
 }
 
diff --git a/rtc_base/DEPS b/rtc_base/DEPS
index 4ba8c92..bb985b9 100644
--- a/rtc_base/DEPS
+++ b/rtc_base/DEPS
@@ -13,9 +13,12 @@
     "+testing/base/public/gunit.h"
   ],
   "trace_categories\.h": [
-    "+perfetto",
+    "+third_party/perfetto",
   ],
   "event_tracer\.cc": [
-    "+perfetto",
+    "+third_party/perfetto",
+  ],
+  "trace_event\.h": [
+    "+third_party/perfetto",
   ]
 }
diff --git a/rtc_base/event_tracer.cc b/rtc_base/event_tracer.cc
index 677b95b..7e9ecba 100644
--- a/rtc_base/event_tracer.cc
+++ b/rtc_base/event_tracer.cc
@@ -15,8 +15,8 @@
 #include "rtc_base/trace_event.h"
 
 #if defined(RTC_USE_PERFETTO)
-#include "perfetto/tracing/tracing.h"
 #include "rtc_base/trace_categories.h"
+#include "third_party/perfetto/include/perfetto/tracing/tracing.h"
 #else
 #include <inttypes.h>
 #include <stdint.h>
diff --git a/rtc_base/trace_categories.h b/rtc_base/trace_categories.h
index c22ce91..e355bbb 100644
--- a/rtc_base/trace_categories.h
+++ b/rtc_base/trace_categories.h
@@ -13,10 +13,10 @@
 
 #define PERFETTO_ENABLE_LEGACY_TRACE_EVENTS 1
 
-#include "perfetto/tracing/track_event.h"  // IWYU pragma: export
-#include "perfetto/tracing/track_event_category_registry.h"
-#include "perfetto/tracing/track_event_legacy.h"  // IWYU pragma: export
 #include "rtc_base/system/rtc_export.h"
+#include "third_party/perfetto/include/perfetto/tracing/track_event.h"  // IWYU pragma: export
+#include "third_party/perfetto/include/perfetto/tracing/track_event_category_registry.h"
+#include "third_party/perfetto/include/perfetto/tracing/track_event_legacy.h"  // IWYU pragma: export
 
 PERFETTO_DEFINE_TEST_CATEGORY_PREFIXES("webrtc-test");
 
diff --git a/rtc_base/trace_event.h b/rtc_base/trace_event.h
index ab62802..4a2d234 100644
--- a/rtc_base/trace_event.h
+++ b/rtc_base/trace_event.h
@@ -17,9 +17,16 @@
 #define RTC_TRACE_EVENTS_ENABLED 1
 #endif
 
+// IWYU pragma: begin_exports
 #if defined(RTC_USE_PERFETTO)
+#include "rtc_base/trace_categories.h"
+#endif
+#include "third_party/perfetto/include/perfetto/tracing/event_context.h"
+#include "third_party/perfetto/include/perfetto/tracing/track.h"
+#include "third_party/perfetto/include/perfetto/tracing/track_event_args.h"
+// IWYU pragma: end_exports
 
-#include "rtc_base/trace_categories.h"  // IWYU pragma: export
+#if defined(RTC_USE_PERFETTO)
 
 // TODO(webrtc:15917): Replace these events.
 #define TRACE_EVENT_ASYNC_STEP0(category_group, name, id, step) \
@@ -34,6 +41,21 @@
 #include <string>
 
 #include "rtc_base/event_tracer.h"
+#include "third_party/perfetto/include/perfetto/tracing/event_context.h"
+#include "third_party/perfetto/include/perfetto/tracing/track.h"
+#include "third_party/perfetto/include/perfetto/tracing/track_event_args.h"
+
+#define RTC_NOOP() \
+  do {             \
+  } while (0)
+
+// TODO(b/42226290): Add implementation for these events with Perfetto.
+#define TRACE_EVENT_BEGIN(category, name, ...) RTC_NOOP();
+#define TRACE_EVENT_END(category, ...) RTC_NOOP();
+#define TRACE_EVENT(category, name, ...) RTC_NOOP();
+#define TRACE_EVENT_INSTANT(category, name, ...) RTC_NOOP();
+#define TRACE_EVENT_CATEGORY_ENABLED(category) RTC_NOOP();
+#define TRACE_COUNTER(category, track, ...) RTC_NOOP();
 
 // Type values for identifying types in the TraceValue union.
 #define TRACE_VALUE_TYPE_BOOL (static_cast<unsigned char>(1))
@@ -731,10 +753,6 @@
 // This section defines no-op alternatives to the tracing macros when
 // RTC_DISABLE_TRACE_EVENTS is defined.
 
-#define RTC_NOOP() \
-  do {             \
-  } while (0)
-
 #define TRACE_DISABLED_BY_DEFAULT(name) "disabled-by-default-" name
 
 #define TRACE_ID_MANGLE(id) 0
diff --git a/test/BUILD.gn b/test/BUILD.gn
index aeb5146..180297f 100644
--- a/test/BUILD.gn
+++ b/test/BUILD.gn
@@ -616,6 +616,7 @@
         "../rtc_base:threading",
         "../system_wrappers:field_trial",
         "../system_wrappers:metrics",
+        "//third_party/perfetto/include/perfetto/tracing",
       ]
       absl_deps = [
         "//third_party/abseil-cpp/absl/flags:flag",
diff --git a/test/DEPS b/test/DEPS
index f9f47e1..95193e7 100644
--- a/test/DEPS
+++ b/test/DEPS
@@ -50,8 +50,7 @@
     "+p2p/base/stun.h",
   ],
   ".*test_main_lib\.cc": [
-    "+perfetto/tracing",
-    "+protos/perfetto/config",
+    "+third_party/perfetto",
   ],
   ".*test_main\.cc": [
     "+absl/debugging/failure_signal_handler.h",
diff --git a/test/test_main_lib.cc b/test/test_main_lib.cc
index 76b5a16..e35029b 100644
--- a/test/test_main_lib.cc
+++ b/test/test_main_lib.cc
@@ -44,10 +44,10 @@
 #include "test/testsupport/resources_dir_flag.h"
 
 #if defined(RTC_USE_PERFETTO)
-#include "perfetto/tracing/backend_type.h"
-#include "perfetto/tracing/tracing.h"
-#include "protos/perfetto/config/trace_config.gen.h"
 #include "rtc_base/event_tracer.h"
+#include "third_party/perfetto/include/perfetto/tracing/backend_type.h"
+#include "third_party/perfetto/include/perfetto/tracing/tracing.h"
+#include "third_party/perfetto/protos/perfetto/config/trace_config.gen.h"
 #endif
 
 #if defined(WEBRTC_WIN)
diff --git a/tools_webrtc/libs/generate_licenses.py b/tools_webrtc/libs/generate_licenses.py
index 7d34ec6..10d6f67 100755
--- a/tools_webrtc/libs/generate_licenses.py
+++ b/tools_webrtc/libs/generate_licenses.py
@@ -79,6 +79,7 @@
     'kotlin_stdlib': ['third_party/kotlin_stdlib/LICENSE'],
     'jni_zero': ['third_party/jni_zero/LICENSE'],
     'protobuf-javascript': ['third_party/protobuf-javascript/LICENSE'],
+    'perfetto': ['third_party/perfetto/LICENSE'],
     # TODO(bugs.webrtc.org/1110): Remove this hack. This is not a lib.
     # For some reason it is listed as so in _get_third_party_libraries.
     'android_deps': [],
diff --git a/webrtc.gni b/webrtc.gni
index f7a8e73..af341d7 100644
--- a/webrtc.gni
+++ b/webrtc.gni
@@ -394,10 +394,6 @@
   rtc_use_perfetto = true
 }
 
-if (rtc_use_perfetto) {
-  rtc_common_configs += [ "//third_party/perfetto/gn:public_config" ]
-}
-
 # Global public configuration that should be applied to all WebRTC targets. You
 # normally shouldn't need to include this in your target as it's automatically
 # included when using the rtc_* templates. It set the defines, include paths and