COPYBARA_IMPORT=Project import generated by Copybara

GitOrigin-RevId: 7a96f855bbba53cb25dcf1b15f07cdb5831a72ea
Change-Id: If6c4a4523a67cf3bd66f46ee5349a2dccc9669ed
diff --git a/protos/third_party/chromium/chrome_track_event.proto b/protos/third_party/chromium/chrome_track_event.proto
index 0a8842f..8b766cb 100644
--- a/protos/third_party/chromium/chrome_track_event.proto
+++ b/protos/third_party/chromium/chrome_track_event.proto
@@ -39,6 +39,7 @@
     TASK_SCOPE_SCHEDULER_POST_TASK = 6;
     TASK_SCOPE_REQUEST_IDLE_CALLBACK = 7;
     TASK_SCOPE_XML_HTTP_REQUEST = 8;
+    TASK_SCOPE_SOFT_NAVIGATION = 9;
   }
   optional TaskScopeType type = 1;
   optional int64 scope_task_id = 2;
diff --git a/src/trace_processor/perfetto_sql/stdlib/chrome/scroll_jank/scroll_jank_v3.sql b/src/trace_processor/perfetto_sql/stdlib/chrome/scroll_jank/scroll_jank_v3.sql
index 4bc9513..589adec 100644
--- a/src/trace_processor/perfetto_sql/stdlib/chrome/scroll_jank/scroll_jank_v3.sql
+++ b/src/trace_processor/perfetto_sql/stdlib/chrome/scroll_jank/scroll_jank_v3.sql
@@ -8,22 +8,7 @@
 -- in BTP.
 INCLUDE PERFETTO MODULE chrome.metadata;
 INCLUDE PERFETTO MODULE chrome.scroll_jank.scroll_jank_v3_cause;
-
--- Checks if slice has a descendant with provided name.
-CREATE PERFETTO FUNCTION _has_descendant_slice_with_name(
-  -- Id of the slice to check descendants of.
-  id INT,
-  -- Name of potential descendant slice.
-  descendant_name STRING
-)
--- Whether `descendant_name` is a name of an descendant slice.
-RETURNS BOOL AS
-SELECT EXISTS(
-  SELECT 1
-  FROM descendant_slice($id)
-  WHERE name = $descendant_name
-  LIMIT 1
-);
+INCLUDE PERFETTO MODULE chrome.scroll_jank.utils;
 
 -- Finds the end timestamp for a given slice's descendant with a given name.
 -- If there are multiple descendants with a given name, the function will return the
@@ -58,9 +43,8 @@
 FROM slice
 WHERE $id = id;
 
-
 -- Grabs all gesture updates with respective scroll ids and start/end
--- timestamps, regardless of being coalesced.
+-- timestamps, regardless of being presented.
 CREATE PERFETTO TABLE chrome_gesture_scroll_updates(
   -- The start timestamp of the scroll.
   ts INT,
@@ -72,82 +56,100 @@
   scroll_update_id INT,
   -- The id of the scroll.
   scroll_id INT,
-  -- Whether this input event was coalesced.
-  is_coalesced BOOL
+  -- Whether this input event was presented.
+  is_presented BOOL,
+  -- Frame presentation timestamp aka the timestamp of the
+  -- SwapEndToPresentationCompositorFrame substage.
+  presentation_timestamp INT,
+  -- EventLatency event type.
+  event_type INT
 ) AS
 SELECT
-  ts,
-  dur,
-  id,
-  -- TODO(b/250089570) Add trace_id to EventLatency and update this script to use it.
-  EXTRACT_ARG(arg_set_id, 'chrome_latency_info.trace_id') AS scroll_update_id,
-  EXTRACT_ARG(arg_set_id, 'chrome_latency_info.gesture_scroll_id') AS scroll_id,
-  EXTRACT_ARG(arg_set_id, 'chrome_latency_info.is_coalesced') AS is_coalesced
-FROM slice
-WHERE name = "InputLatency::GestureScrollUpdate" AND dur != -1;
+  slice.ts,
+  slice.dur,
+  slice.id,
+  EXTRACT_arg(arg_set_id, "event_latency.event_latency_id") AS scroll_update_id,
+  chrome_get_most_recent_scroll_begin_id(slice.ts) AS scroll_id,
+  has_descendant_slice_with_name(slice.id, "SubmitCompositorFrameToPresentationCompositorFrame")
+  AS is_presented,
+  _descendant_slice_end(slice.id, "SwapEndToPresentationCompositorFrame") AS presentation_timestamp,
+  EXTRACT_ARG(arg_set_id, 'event_latency.event_type') AS event_type
+FROM slice JOIN args USING(arg_set_id)
+WHERE name = "EventLatency"
+AND args.string_value GLOB "*GESTURE_SCROLL_UPDATE";
 
-CREATE PERFETTO TABLE _non_coalesced_gesture_scrolls AS
+CREATE PERFETTO TABLE _presented_gesture_scrolls AS
 SELECT
   id,
   ts,
   dur,
   scroll_update_id,
-  scroll_id
-FROM  chrome_gesture_scroll_updates
-WHERE is_coalesced = false
+  scroll_id,
+  presentation_timestamp,
+  event_type
+FROM chrome_gesture_scroll_updates
+WHERE is_presented = true
 ORDER BY ts ASC;
 
 -- Scroll updates, corresponding to all input events that were converted to a
 -- presented scroll update.
 CREATE PERFETTO TABLE chrome_presented_gesture_scrolls(
-  -- Minimum slice id for input presented in this frame, the non coalesced input.
+  -- Minimum slice id for input presented in this frame, the non-presented input.
   id INT,
   -- The start timestamp for producing the frame.
   ts INT,
   -- The duration between producing and presenting the frame.
   dur INT,
-  -- The timestamp of the last input that arrived and got coalesced into the frame.
-  last_coalesced_input_ts INT,
+  -- The timestamp of the last input that arrived and got presented in the frame.
+  last_presented_input_ts INT,
   -- The id of the scroll update event, a unique identifier to the gesture.
   scroll_update_id INT,
   -- The id of the ongoing scroll.
-  scroll_id INT
+  scroll_id INT,
+  -- Frame presentation timestamp.
+  presentation_timestamp INT,
+  -- EventLatency event type.
+  event_type INT
 ) AS
 WITH
-scroll_updates_with_coalesce_info as MATERIALIZED (
+scroll_updates_with_presentation_info as MATERIALIZED (
   SELECT
     id,
     ts,
-    -- For each scroll update, find the latest non-coalesced update which
-    -- happened before it. For coalesced scroll updates, this will be the
-    -- presented scroll update they have been coalesced into.
+    -- For each scroll update, find the latest presented update which
+    -- started before it.
     (
       SELECT id
-      FROM _non_coalesced_gesture_scrolls non_coalesced
-      WHERE non_coalesced.ts <= scroll_update.ts
+      FROM _presented_gesture_scrolls _presented
+      WHERE _presented.ts <= scroll_update.ts
       ORDER BY ts DESC
       LIMIT 1
-     ) as coalesced_to_scroll_update_slice_id
+     ) as presented_to_scroll_update_slice_id
   FROM chrome_gesture_scroll_updates scroll_update
-  ORDER BY coalesced_to_scroll_update_slice_id, ts
+  ORDER BY presented_to_scroll_update_slice_id, ts
 )
 SELECT
   id,
   ts,
   dur,
-  -- Find the latest input that was coalesced into this scroll update.
+  -- Find the latest input that was presented in this scroll update.
   (
-    SELECT coalesce_info.ts
-    FROM scroll_updates_with_coalesce_info coalesce_info
+    SELECT presentation_info.ts
+    FROM scroll_updates_with_presentation_info presentation_info
     WHERE
-      coalesce_info.coalesced_to_scroll_update_slice_id =
-        _non_coalesced_gesture_scrolls.id
+      presentation_info.presented_to_scroll_update_slice_id =
+        _presented_gesture_scrolls.id
     ORDER BY ts DESC
     LIMIT 1
-  ) as last_coalesced_input_ts,
+  ) as last_presented_input_ts,
   scroll_update_id,
-  scroll_id
-FROM _non_coalesced_gesture_scrolls;
+  scroll_id,
+  presentation_timestamp,
+  event_type
+FROM _presented_gesture_scrolls
+-- TODO(b/247542163): remove this condition when all stages
+-- of EventLatency are recorded correctly.
+WHERE presentation_timestamp IS NOT NULL;
 
 -- Associate every trace_id with it's perceived delta_y on the screen after
 -- prediction.
@@ -163,50 +165,15 @@
 FROM slice
 WHERE name = "InputHandlerProxy::HandleGestureScrollUpdate_Result";
 
--- Extract event latency timestamps, to later use it for joining
--- with gesture scroll updates, as event latencies don't have trace
--- ids associated with it.
-CREATE PERFETTO TABLE chrome_gesture_scroll_event_latencies(
-  -- Start timestamp for the EventLatency.
-  ts INT,
-  -- Slice id of the EventLatency.
-  event_latency_id INT,
-  -- Duration of the EventLatency.
-  dur INT,
-  -- End timestamp for input aka the timestamp of the LatchToSwapEnd substage.
-  input_latency_end_ts INT,
-  -- Frame presentation timestamp aka the timestamp of the
-  -- SwapEndToPresentationCompositorFrame substage.
-  presentation_timestamp INT,
-  -- EventLatency event type.
-  event_type INT
-) AS
-SELECT
-  slice.ts,
-  slice.id AS event_latency_id,
-  slice.dur AS dur,
-  _descendant_slice_end(slice.id, "LatchToSwapEnd") AS input_latency_end_ts,
-  _descendant_slice_end(slice.id, "SwapEndToPresentationCompositorFrame") AS presentation_timestamp,
-  EXTRACT_ARG(arg_set_id, 'event_latency.event_type') AS event_type
-FROM slice
-WHERE name = "EventLatency"
-      AND event_type in (
-          "GESTURE_SCROLL_UPDATE",
-          "FIRST_GESTURE_SCROLL_UPDATE",
-          "INERTIAL_GESTURE_SCROLL_UPDATE")
-      AND _has_descendant_slice_with_name(slice.id, "SwapEndToPresentationCompositorFrame");
-
--- Join presented gesture scrolls with their respective event
--- latencies based on |LatchToSwapEnd| timestamp, as it's the
--- end timestamp for both the gesture scroll update slice and
--- the LatchToSwapEnd slice.
+-- Obtain the subset of input events that were fully presented, as indicated
+-- by the presence of SwapEndToPresentationCompositorFrame.
 CREATE PERFETTO TABLE chrome_full_frame_view(
   -- ID of the frame.
   id INT,
   -- Start timestamp of the frame.
   ts INT,
-  -- The timestamp of the last coalesced input.
-  last_coalesced_input_ts INT,
+  -- The timestamp of the last presented input.
+  last_presented_input_ts INT,
   -- ID of the associated scroll.
   scroll_id INT,
   -- ID of the associated scroll update.
@@ -221,16 +188,18 @@
 SELECT
   frames.id,
   frames.ts,
-  frames.last_coalesced_input_ts,
+  frames.last_presented_input_ts,
   frames.scroll_id,
   frames.scroll_update_id,
-  events.event_latency_id,
-  events.dur,
-  events.presentation_timestamp
+  frames.id AS event_latency_id,
+  frames.dur,
+  frames.presentation_timestamp
 FROM chrome_presented_gesture_scrolls frames
-JOIN chrome_gesture_scroll_event_latencies events
-  ON frames.ts = events.ts
-  AND events.input_latency_end_ts = (frames.ts + frames.dur);
+WHERE frames.event_type in (
+          "GESTURE_SCROLL_UPDATE",
+          "FIRST_GESTURE_SCROLL_UPDATE",
+          "INERTIAL_GESTURE_SCROLL_UPDATE")
+    AND has_descendant_slice_with_name(frames.id, "SwapEndToPresentationCompositorFrame");
 
 -- Join deltas with EventLatency data.
 CREATE PERFETTO TABLE chrome_full_frame_delta_view(
@@ -242,8 +211,8 @@
   scroll_id INT,
   -- ID of the associated scroll update.
   scroll_update_id INT,
-  -- The timestamp of the last coalesced input.
-  last_coalesced_input_ts INT,
+  -- The timestamp of the last presented input.
+  last_presented_input_ts INT,
   -- The perceived delta_y on the screen post prediction.
   delta_y INT,
   -- ID of the associated EventLatency.
@@ -258,7 +227,7 @@
   frames.ts,
   frames.scroll_id,
   frames.scroll_update_id,
-  frames.last_coalesced_input_ts,
+  frames.last_presented_input_ts,
   deltas.delta_y,
   frames.event_latency_id,
   frames.dur,
@@ -272,7 +241,7 @@
 CREATE PERFETTO VIEW chrome_merged_frame_view(
   -- ID of the frame.
   id INT,
-  -- The timestamp of the last coalesced input.
+  -- The timestamp of the last presented input.
   max_start_ts INT,
   -- The earliest frame start timestamp.
   min_start_ts INT,
@@ -295,7 +264,7 @@
 ) AS
 SELECT
   id,
-  MAX(last_coalesced_input_ts) AS max_start_ts,
+  MAX(last_presented_input_ts) AS max_start_ts,
   MIN(ts) AS min_start_ts,
   scroll_id,
   scroll_update_id,
@@ -461,7 +430,7 @@
 ) AS
 SELECT DISTINCT
 presentation_timestamp
-FROM chrome_gesture_scroll_event_latencies;
+FROM chrome_presented_gesture_scrolls;
 
 -- Dividing missed frames over total frames to get janky frame percentage.
 -- This represents the v3 scroll jank metrics.