Add event scope to all TRACE_EVENT_INSTANTs

These are required by the Perfetto API and the missing argument prevents
the use of Perfetto.

Bug: webrtc:15917
Change-Id: Ie40c0344dc9d8cd40f7c751b133d150b975a33c7
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/347702
Reviewed-by: Mirko Bonadei <mbonadei@webrtc.org>
Reviewed-by: Tomas Gunnarsson <tommi@webrtc.org>
Commit-Queue: Evan Shrubsole <eshr@google.com>
Cr-Commit-Position: refs/heads/main@{#42147}
diff --git a/modules/desktop_capture/mac/screen_capturer_mac.mm b/modules/desktop_capture/mac/screen_capturer_mac.mm
index 60089fd..e674657 100644
--- a/modules/desktop_capture/mac/screen_capturer_mac.mm
+++ b/modules/desktop_capture/mac/screen_capturer_mac.mm
@@ -178,8 +178,11 @@
   RTC_DCHECK(thread_checker_.IsCurrent());
   RTC_DCHECK(!callback_);
   RTC_DCHECK(callback);
-  TRACE_EVENT_INSTANT1(
-      "webrtc", "ScreenCapturermac::Start", "target display id ", current_display_);
+  TRACE_EVENT_INSTANT1("webrtc",
+                       "ScreenCapturermac::Start",
+                       TRACE_EVENT_SCOPE_GLOBAL,
+                       "target display id ",
+                       current_display_);
 
   callback_ = callback;
   // Start and operate CGDisplayStream handler all from capture thread.
diff --git a/modules/video_coding/generic_decoder.cc b/modules/video_coding/generic_decoder.cc
index 0aafd1a..0a9920a 100644
--- a/modules/video_coding/generic_decoder.cc
+++ b/modules/video_coding/generic_decoder.cc
@@ -104,7 +104,8 @@
                                       absl::optional<uint8_t> qp) {
   RTC_DCHECK(_receiveCallback) << "Callback must not be null at this point";
   TRACE_EVENT_INSTANT1("webrtc", "VCMDecodedFrameCallback::Decoded",
-                       "timestamp", decodedImage.rtp_timestamp());
+                       TRACE_EVENT_SCOPE_GLOBAL, "timestamp",
+                       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;
diff --git a/pc/rtc_stats_collector.cc b/pc/rtc_stats_collector.cc
index b7126ce..6253b7c 100644
--- a/pc/rtc_stats_collector.cc
+++ b/pc/rtc_stats_collector.cc
@@ -1336,8 +1336,8 @@
   // Trace WebRTC Stats when getStats is called on Javascript.
   // This allows access to WebRTC stats from trace logs. To enable them,
   // select the "webrtc_stats" category when recording traces.
-  TRACE_EVENT_INSTANT1("webrtc_stats", "webrtc_stats", "report",
-                       cached_report_->ToJson());
+  TRACE_EVENT_INSTANT1("webrtc_stats", "webrtc_stats", TRACE_EVENT_SCOPE_GLOBAL,
+                       "report", cached_report_->ToJson());
 
   // Deliver report and clear `requests_`.
   std::vector<RequestInfo> requests;
diff --git a/rtc_base/event_tracer.cc b/rtc_base/event_tracer.cc
index 992a2b5..6ad8330 100644
--- a/rtc_base/event_tracer.cc
+++ b/rtc_base/event_tracer.cc
@@ -206,12 +206,14 @@
     // Finally start, everything should be set up now.
     logging_thread_ =
         PlatformThread::SpawnJoinable([this] { Log(); }, "EventTracingThread");
-    TRACE_EVENT_INSTANT0("webrtc", "EventLogger::Start");
+    TRACE_EVENT_INSTANT0("webrtc", "EventLogger::Start",
+                         TRACE_EVENT_SCOPE_GLOBAL);
   }
 
   void Stop() {
     RTC_DCHECK(thread_checker_.IsCurrent());
-    TRACE_EVENT_INSTANT0("webrtc", "EventLogger::Stop");
+    TRACE_EVENT_INSTANT0("webrtc", "EventLogger::Stop",
+                         TRACE_EVENT_SCOPE_GLOBAL);
     // Try to stop. Abort if we're not currently logging.
     int one = 1;
     if (g_event_logging_active.compare_exchange_strong(one, 0))
diff --git a/rtc_base/trace_event.h b/rtc_base/trace_event.h
index a086064..8499983 100644
--- a/rtc_base/trace_event.h
+++ b/rtc_base/trace_event.h
@@ -170,21 +170,27 @@
   INTERNAL_TRACE_EVENT_ADD_SCOPED(category, name, arg1_name, arg1_val,         \
                                   arg2_name, arg2_val)
 
+// Enum reflecting the scope of an INSTANT event. Must fit within
+// TRACE_EVENT_FLAG_SCOPE_MASK.
+static constexpr uint8_t TRACE_EVENT_SCOPE_GLOBAL = 0u << 2;
+static constexpr uint8_t TRACE_EVENT_SCOPE_PROCESS = 1u << 2;
+static constexpr uint8_t TRACE_EVENT_SCOPE_THREAD = 2u << 2;
+
 // Records a single event called "name" immediately, with 0, 1 or 2
 // associated arguments. If the category is not enabled, then this
 // does nothing.
 // - category and name strings must have application lifetime (statics or
 //   literals). They may not include " chars.
-#define TRACE_EVENT_INSTANT0(category, name)                          \
+#define TRACE_EVENT_INSTANT0(category, name, scope)                   \
   INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_INSTANT, category, name, \
                            TRACE_EVENT_FLAG_NONE)
-#define TRACE_EVENT_INSTANT1(category, name, arg1_name, arg1_val)     \
-  INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_INSTANT, category, name, \
+#define TRACE_EVENT_INSTANT1(category, name, scope, arg1_name, arg1_val) \
+  INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_INSTANT, category, name,    \
                            TRACE_EVENT_FLAG_NONE, arg1_name, arg1_val)
-#define TRACE_EVENT_INSTANT2(category, name, arg1_name, arg1_val, arg2_name, \
-                             arg2_val)                                       \
-  INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_INSTANT, category, name,        \
-                           TRACE_EVENT_FLAG_NONE, arg1_name, arg1_val,       \
+#define TRACE_EVENT_INSTANT2(category, name, scope, arg1_name, arg1_val, \
+                             arg2_name, arg2_val)                        \
+  INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_INSTANT, category, name,    \
+                           TRACE_EVENT_FLAG_NONE, arg1_name, arg1_val,   \
                            arg2_name, arg2_val)
 
 // Records a single BEGIN event called "name" immediately, with 0, 1 or 2
@@ -709,7 +715,6 @@
   do {             \
   } while (0)
 
-
 #define TRACE_DISABLED_BY_DEFAULT(name) "disabled-by-default-" name
 
 #define TRACE_ID_MANGLE(id) 0
@@ -719,11 +724,12 @@
 #define TRACE_EVENT2(category, name, arg1_name, arg1_val, arg2_name, arg2_val) \
   RTC_NOOP()
 
-#define TRACE_EVENT_INSTANT0(category, name) RTC_NOOP()
-#define TRACE_EVENT_INSTANT1(category, name, arg1_name, arg1_val) RTC_NOOP()
+#define TRACE_EVENT_INSTANT0(category, name, scope) RTC_NOOP()
+#define TRACE_EVENT_INSTANT1(category, name, scope, arg1_name, arg1_val) \
+  RTC_NOOP()
 
-#define TRACE_EVENT_INSTANT2(category, name, arg1_name, arg1_val, arg2_name, \
-                             arg2_val)                                       \
+#define TRACE_EVENT_INSTANT2(category, name, scope, arg1_name, arg1_val, \
+                             arg2_name, arg2_val)                        \
   RTC_NOOP()
 
 #define TRACE_EVENT_BEGIN0(category, name) RTC_NOOP()
diff --git a/video/frame_cadence_adapter.cc b/video/frame_cadence_adapter.cc
index 0becc82..b11c7ba 100644
--- a/video/frame_cadence_adapter.cc
+++ b/video/frame_cadence_adapter.cc
@@ -469,8 +469,8 @@
     bool quality_converged) {
   RTC_DCHECK_RUN_ON(&sequence_checker_);
   TRACE_EVENT_INSTANT2(TRACE_DISABLED_BY_DEFAULT("webrtc"), __func__,
-                       "spatial_index", spatial_index, "converged",
-                       quality_converged);
+                       TRACE_EVENT_SCOPE_GLOBAL, "spatial_index", spatial_index,
+                       "converged", quality_converged);
   if (spatial_index >= layer_trackers_.size())
     return;
   if (layer_trackers_[spatial_index].quality_converged.has_value())
@@ -481,7 +481,8 @@
                                              bool enabled) {
   RTC_DCHECK_RUN_ON(&sequence_checker_);
   TRACE_EVENT_INSTANT2(TRACE_DISABLED_BY_DEFAULT("webrtc"), __func__,
-                       "spatial_index", spatial_index, "enabled", enabled);
+                       TRACE_EVENT_SCOPE_GLOBAL, "spatial_index", spatial_index,
+                       "enabled", enabled);
   if (spatial_index >= layer_trackers_.size())
     return;
   if (enabled) {
@@ -546,7 +547,8 @@
     absl::optional<double> max_frame_rate) {
   RTC_DCHECK_RUN_ON(&sequence_checker_);
   TRACE_EVENT_INSTANT1(TRACE_DISABLED_BY_DEFAULT("webrtc"), __func__,
-                       "max_frame_rate", max_frame_rate.value_or(-1));
+                       TRACE_EVENT_SCOPE_GLOBAL, "max_frame_rate",
+                       max_frame_rate.value_or(-1));
   if (max_frame_rate.value_or(0) > 0) {
     // Set new, validated (> 0) and restricted frame rate.
     restricted_frame_delay_ = TimeDelta::Seconds(1) / *max_frame_rate;
@@ -558,7 +560,7 @@
 
 void ZeroHertzAdapterMode::ProcessKeyFrameRequest() {
   RTC_DCHECK_RUN_ON(&sequence_checker_);
-  TRACE_EVENT_INSTANT0("webrtc", __func__);
+  TRACE_EVENT_INSTANT0("webrtc", __func__, TRACE_EVENT_SCOPE_GLOBAL);
   // If we're new and don't have a frame, there's no need to request refresh
   // frames as this was being triggered for us when zero-hz mode was set up.
   //
diff --git a/video/video_send_stream_impl.cc b/video/video_send_stream_impl.cc
index 52e437a..142827d 100644
--- a/video/video_send_stream_impl.cc
+++ b/video/video_send_stream_impl.cc
@@ -683,7 +683,8 @@
   if (!rtp_video_sender_->IsActive())
     return;
 
-  TRACE_EVENT_INSTANT0("webrtc", "VideoSendStream::Stop");
+  TRACE_EVENT_INSTANT0("webrtc", "VideoSendStream::Stop",
+                       TRACE_EVENT_SCOPE_GLOBAL);
   rtp_video_sender_->SetSending(false);
   if (IsRunning()) {
     StopVideoSendStream();
diff --git a/video/video_stream_encoder.cc b/video/video_stream_encoder.cc
index 14206e4..7f42f73 100644
--- a/video/video_stream_encoder.cc
+++ b/video/video_stream_encoder.cc
@@ -2129,7 +2129,8 @@
     const EncodedImage& encoded_image,
     const CodecSpecificInfo* codec_specific_info) {
   TRACE_EVENT_INSTANT1("webrtc", "VCMEncodedFrameCallback::Encoded",
-                       "timestamp", encoded_image.RtpTimestamp());
+                       TRACE_EVENT_SCOPE_GLOBAL, "timestamp",
+                       encoded_image.RtpTimestamp());
 
   const size_t simulcast_index = encoded_image.SimulcastIndex().value_or(0);
   const VideoCodecType codec_type = codec_specific_info