File: chrome_scrolls.sql

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (1259 lines) | stat: -rw-r--r-- 64,136 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
-- Copyright 2023 The Chromium Authors
-- Use of this source code is governed by a BSD-style license that can be
-- found in the LICENSE file.

INCLUDE PERFETTO MODULE chrome.event_latency;

INCLUDE PERFETTO MODULE chrome.graphics_pipeline;

INCLUDE PERFETTO MODULE chrome.input;

INCLUDE PERFETTO MODULE chrome.scroll_jank.scroll_offsets;

INCLUDE PERFETTO MODULE chrome.scroll_jank.utils;

-- Ties together input (`LatencyInfo.Flow`) and frame (`Graphics.Pipeline`)
-- trace events. Only covers input events of the `GESTURE_SCROLL_UPDATE_EVENT`
-- type.
CREATE PERFETTO TABLE chrome_scroll_update_refs (
  -- Id of the Chrome input pipeline (`LatencyInfo.Flow`).
  scroll_update_latency_id LONG,
  -- Id of the touch move input corresponding to this scroll update.
  touch_move_latency_id LONG,
  -- Id of the `EventLatency` of the frame that the input was presented in.
  presentation_latency_id LONG,
  -- Id of the frame pipeline (`Graphics.Pipeline`), pre-surface aggregation.
  surface_frame_id LONG,
  -- Id of the frame pipeline (`Graphics.Pipeline`), post-surface aggregation.
  display_trace_id LONG
) AS
SELECT
  scroll_update.latency_id AS scroll_update_latency_id,
  chrome_touch_move_to_scroll_update.touch_move_latency_id,
  coalesce(chrome_coalesced_inputs.presented_latency_id, scroll_update.latency_id) AS presentation_latency_id,
  chrome_graphics_pipeline_inputs_to_surface_frames.surface_frame_trace_id AS surface_frame_id,
  chrome_surface_frame_id_to_first_display_id.display_trace_id
FROM chrome_inputs AS scroll_update
LEFT JOIN chrome_graphics_pipeline_inputs_to_surface_frames
  USING (latency_id)
LEFT JOIN chrome_surface_frame_id_to_first_display_id
  ON chrome_surface_frame_id_to_first_display_id.surface_frame_trace_id = chrome_graphics_pipeline_inputs_to_surface_frames.surface_frame_trace_id
LEFT JOIN chrome_touch_move_to_scroll_update
  ON chrome_touch_move_to_scroll_update.scroll_update_latency_id = scroll_update.latency_id
LEFT JOIN chrome_coalesced_inputs
  ON chrome_coalesced_inputs.coalesced_latency_id = scroll_update.latency_id
WHERE
  scroll_update.input_type = 'GESTURE_SCROLL_UPDATE_EVENT';

-- Timestamps and other related information for events during the
-- input-associated (before inputs are coalesced into a frame) stages of a
-- scroll.
CREATE PERFETTO TABLE _scroll_update_input_timestamps_and_metadata AS
SELECT
  refs.scroll_update_latency_id AS id,
  refs.presentation_latency_id AS presented_in_frame_id,
  -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
  chrome_event_latency.scroll_id,
  chrome_event_latency.is_presented,
  chrome_event_latency.is_janky,
  chrome_event_latency.is_janky_v3,
  chrome_event_latency.event_type = 'INERTIAL_GESTURE_SCROLL_UPDATE' AS is_inertial,
  chrome_event_latency.event_type = 'FIRST_GESTURE_SCROLL_UPDATE' AS is_first_scroll_update_in_scroll,
  chrome_event_latency.ts AS generation_ts,
  chrome_android_input.input_reader_processing_end_ts,
  chrome_android_input.input_dispatcher_processing_end_ts,
  -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
  touch_move_received_step.slice_id AS touch_move_received_slice_id,
  touch_move_received_step.ts AS touch_move_received_ts,
  -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
  touch_move_processed_step.slice_id AS touch_move_processed_slice_id,
  touch_move_processed_step.ts AS touch_move_processed_ts,
  -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
  scroll_update_created_step.slice_id AS scroll_update_created_slice_id,
  scroll_update_created_step.utid AS browser_utid,
  scroll_update_created_step.ts AS scroll_update_created_ts,
  scroll_update_created_step.ts + scroll_update_created_step.dur AS scroll_update_created_end_ts,
  -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
  compositor_dispatch_step.slice_id AS compositor_dispatch_slice_id,
  compositor_dispatch_step.task_start_time_ts AS compositor_dispatch_task_ts,
  compositor_dispatch_step.ts AS compositor_dispatch_ts,
  compositor_dispatch_step.ts + compositor_dispatch_step.dur AS compositor_dispatch_end_ts,
  compositor_dispatch_step.utid AS compositor_utid,
  -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
  compositor_coalesced_input_handled_step.slice_id AS compositor_coalesced_input_handled_slice_id,
  compositor_coalesced_input_handled_step.ts AS compositor_coalesced_input_handled_ts,
  compositor_coalesced_input_handled_step.ts + compositor_coalesced_input_handled_step.dur AS compositor_coalesced_input_handled_end_ts
FROM chrome_scroll_update_refs AS refs
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
LEFT JOIN chrome_gesture_scroll_updates AS chrome_event_latency
  ON chrome_event_latency.scroll_update_id = refs.scroll_update_latency_id
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
LEFT JOIN chrome_dispatch_android_input_event_to_touch_move
  ON refs.touch_move_latency_id = chrome_dispatch_android_input_event_to_touch_move.touch_move_latency_id
LEFT JOIN chrome_android_input
  ON chrome_android_input.android_input_id = chrome_dispatch_android_input_event_to_touch_move.android_input_id
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
LEFT JOIN chrome_input_pipeline_steps AS touch_move_received_step
  ON refs.touch_move_latency_id = touch_move_received_step.latency_id
  AND touch_move_received_step.step = 'STEP_SEND_INPUT_EVENT_UI'
  AND touch_move_received_step.input_type = 'TOUCH_MOVE_EVENT'
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
LEFT JOIN chrome_input_pipeline_steps AS touch_move_processed_step
  ON touch_move_processed_step.latency_id = refs.touch_move_latency_id
  AND touch_move_processed_step.step = 'STEP_TOUCH_EVENT_HANDLED'
  AND touch_move_processed_step.input_type = 'TOUCH_MOVE_EVENT'
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
LEFT JOIN chrome_input_pipeline_steps AS scroll_update_created_step
  ON scroll_update_created_step.latency_id = refs.scroll_update_latency_id
  AND scroll_update_created_step.step = 'STEP_SEND_INPUT_EVENT_UI'
  AND scroll_update_created_step.input_type = 'GESTURE_SCROLL_UPDATE_EVENT'
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
LEFT JOIN chrome_input_pipeline_steps AS compositor_dispatch_step
  ON compositor_dispatch_step.latency_id = refs.scroll_update_latency_id
  AND compositor_dispatch_step.step = 'STEP_HANDLE_INPUT_EVENT_IMPL'
  AND compositor_dispatch_step.input_type = 'GESTURE_SCROLL_UPDATE_EVENT'
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
LEFT JOIN chrome_input_pipeline_steps AS compositor_coalesced_input_handled_step
  ON compositor_coalesced_input_handled_step.latency_id = refs.scroll_update_latency_id
  AND compositor_coalesced_input_handled_step.step = 'STEP_DID_HANDLE_INPUT_AND_OVERSCROLL'
  AND compositor_coalesced_input_handled_step.input_type = 'GESTURE_SCROLL_UPDATE_EVENT';

-- Timestamps and durations for the input-associated (before coalescing inputs
-- into a frame) stages of a scroll.
CREATE PERFETTO TABLE chrome_scroll_update_input_pipeline (
  -- Id of the `LatencyInfo.Flow` slices corresponding to this scroll event.
  id LONG,
  -- Id of the scroll this scroll update belongs to.
  scroll_id LONG,
  -- Id of the frame that this input was presented in. Can be joined with
  -- `chrome_scroll_update_frame_pipeline.id`.
  presented_in_frame_id LONG,
  -- Whether this input event was presented.
  is_presented BOOL,
  -- Whether the corresponding frame is janky based on the
  -- Event.ScrollJank.DelayedFramesPercentage.FixedWindow metric. This comes
  -- directly from `perfetto.protos.EventLatency.is_janky_scrolled_frame`.
  is_janky BOOL,
  -- Whether the corresponding frame is janky based on the
  -- Event.ScrollJank.DelayedFramesPercentage.FixedWindow3 metric. This comes
  -- directly from `perfetto.protos.EventLatency.is_janky_scrolled_frame_v3`.
  is_janky_v3 BOOL,
  -- Whether the corresponding scroll is inertial (fling).
  -- If this is `true`, "generation" and "touch_move" related timestamps and
  -- durations will be null.
  is_inertial BOOL,
  -- Whether this is the first update in a scroll.
  -- First scroll update can never be janky.
  is_first_scroll_update_in_scroll BOOL,
  -- Whether this is the first input that was presented in frame
  -- `presented_in_frame_id`.
  is_first_scroll_update_in_frame BOOL,
  -- Input generation timestamp (from the Android system).
  generation_ts TIMESTAMP,
  -- End timestamp for the InputReader step (see android_input.sql).
  -- Only populated when atrace 'input' category is enabled.
  input_reader_processing_end_ts TIMESTAMP,
  -- End timestamp for the InputDispatcher step (see android_input.sql).
  -- Only populated when atrace 'input' category is enabled.
  input_dispatcher_processing_end_ts TIMESTAMP,
  -- Duration from input generation to when the browser received the input.
  generation_to_browser_main_dur DURATION,
  -- Utid for the browser main thread.
  browser_utid LONG,
  -- Slice id for the `STEP_SEND_INPUT_EVENT_UI` slice for the touch move.
  touch_move_received_slice_id LONG,
  -- Timestamp for the `STEP_SEND_INPUT_EVENT_UI` slice for the touch move.
  touch_move_received_ts TIMESTAMP,
  -- Duration for processing  a `TouchMove` event.
  touch_move_processing_dur DURATION,
  -- Slice id for the `STEP_SEND_INPUT_EVENT_UI` slice for the gesture scroll.
  scroll_update_created_slice_id LONG,
  -- Timestamp for the `STEP_SEND_INPUT_EVENT_UI` slice for the gesture scroll.
  scroll_update_created_ts TIMESTAMP,
  -- Duration for creating a `GestureScrollUpdate` from a `TouchMove` event.
  scroll_update_processing_dur DURATION,
  -- End timestamp for the `STEP_SEND_INPUT_EVENT_UI` slice for the above.
  scroll_update_created_end_ts TIMESTAMP,
  -- Duration between the browser and compositor dispatch.
  browser_to_compositor_delay_dur DURATION,
  -- Utid for the renderer compositor thread.
  compositor_utid LONG,
  -- Slice id for the `STEP_HANDLE_INPUT_EVENT_IMPL` slice.
  compositor_dispatch_slice_id LONG,
  -- Timestamp for the `STEP_HANDLE_INPUT_EVENT_IMPL` slice or the
  -- containing task (if available).
  compositor_dispatch_ts TIMESTAMP,
  -- Duration for the compositor dispatch itself.
  compositor_dispatch_dur DURATION,
  -- End timestamp for the `STEP_HANDLE_INPUT_EVENT_IMPL` slice.
  compositor_dispatch_end_ts TIMESTAMP,
  -- Duration between compositor dispatch and coalescing input.
  compositor_dispatch_to_coalesced_input_handled_dur DURATION,
  -- Slice id for the `STEP_DID_HANDLE_INPUT_AND_OVERSCROLL` slice.
  compositor_coalesced_input_handled_slice_id LONG,
  -- Timestamp for the `STEP_DID_HANDLE_INPUT_AND_OVERSCROLL` slice.
  compositor_coalesced_input_handled_ts TIMESTAMP,
  -- Duration for the `STEP_DID_HANDLE_INPUT_AND_OVERSCROLL` slice.
  compositor_coalesced_input_handled_dur DURATION,
  -- End timestamp for the `STEP_DID_HANDLE_INPUT_AND_OVERSCROLL` slice.
  compositor_coalesced_input_handled_end_ts TIMESTAMP
) AS
WITH
  processed_timestamps_and_metadata AS (
    SELECT
      id,
      scroll_id,
      presented_in_frame_id,
      is_presented,
      is_janky,
      is_janky_v3,
      is_inertial,
      is_first_scroll_update_in_scroll,
      row_number() OVER (PARTITION BY presented_in_frame_id ORDER BY generation_ts ASC) = 1 AS is_first_scroll_update_in_frame,
      -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
      -- Ids
      browser_utid,
      touch_move_received_slice_id,
      -- Timestamps
      generation_ts,
      input_reader_processing_end_ts,
      input_dispatcher_processing_end_ts,
      touch_move_received_ts,
      -- TODO(b:385160424): this is a workaround for cases when
      -- generation time is later than the input time.
      max(
        iif(
          is_inertial AND touch_move_received_ts IS NULL,
          scroll_update_created_ts,
          touch_move_received_ts
        ),
        generation_ts
      ) AS browser_main_received_ts,
      -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
      -- Ids
      scroll_update_created_slice_id,
      -- Timestamps
      scroll_update_created_ts,
      scroll_update_created_end_ts,
      -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
      -- Ids
      compositor_utid,
      compositor_dispatch_slice_id,
      -- Timestamps
      coalesce(compositor_dispatch_task_ts, compositor_dispatch_ts) AS compositor_dispatch_ts,
      compositor_dispatch_end_ts,
      -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
      -- Ids
      compositor_coalesced_input_handled_slice_id,
      -- Timestamps
      compositor_coalesced_input_handled_ts,
      compositor_coalesced_input_handled_end_ts
    FROM _scroll_update_input_timestamps_and_metadata
  )
SELECT
  id,
  scroll_id,
  presented_in_frame_id,
  is_presented,
  is_janky,
  is_janky_v3,
  is_inertial,
  is_first_scroll_update_in_scroll,
  is_first_scroll_update_in_frame,
  -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
  -- No applicable utid (duration between two threads).
  -- No applicable slice id (duration between two threads).
  generation_ts,
  input_reader_processing_end_ts,
  input_dispatcher_processing_end_ts,
  -- Flings don't have a touch move event so make GenerationToBrowserMain span
  -- all the way to the creation of the gesture scroll update.
  browser_main_received_ts - generation_ts AS generation_to_browser_main_dur,
  -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
  browser_utid,
  touch_move_received_slice_id,
  touch_move_received_ts,
  scroll_update_created_ts - max(touch_move_received_ts, generation_ts) AS touch_move_processing_dur,
  -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
  -- On `browser_utid`.
  scroll_update_created_slice_id,
  scroll_update_created_ts,
  scroll_update_created_end_ts - scroll_update_created_ts AS scroll_update_processing_dur,
  -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
  -- No applicable utid (duration between two threads).
  -- No applicable slice id (duration between two threads).
  scroll_update_created_end_ts,
  -- TODO(b:385161677): use the start
  -- of the STEP_SEND_DISPATCH_EVENT_MOJO_MESSAGE step
  -- instead of scroll_update_created_end_ts.
  max(compositor_dispatch_ts, scroll_update_created_end_ts) - scroll_update_created_end_ts AS browser_to_compositor_delay_dur,
  -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
  compositor_utid,
  compositor_dispatch_slice_id,
  compositor_dispatch_ts,
  compositor_dispatch_end_ts - compositor_dispatch_ts AS compositor_dispatch_dur,
  -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
  -- On `compositor_utid`.
  -- No applicable slice id (duration between two slices).
  compositor_dispatch_end_ts,
  -- TODO(b:380868337): This is sometimes negative; check/fix this.
  compositor_coalesced_input_handled_ts - compositor_dispatch_end_ts AS compositor_dispatch_to_coalesced_input_handled_dur,
  -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
  -- On `compositor_utid`.
  compositor_coalesced_input_handled_slice_id,
  compositor_coalesced_input_handled_ts,
  compositor_coalesced_input_handled_end_ts - compositor_coalesced_input_handled_ts AS compositor_coalesced_input_handled_dur,
  -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
  compositor_coalesced_input_handled_end_ts
FROM processed_timestamps_and_metadata;

-- Timestamps and other related information for events during the
-- frame-associated (after inputs are coalesced into a frame) stages of a
-- scroll.
CREATE PERFETTO TABLE _scroll_update_frame_timestamps_and_metadata AS
SELECT
  refs.scroll_update_latency_id AS id,
  refs.display_trace_id,
  -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
  chrome_event_latency.vsync_interval_ms AS vsync_interval_ms,
  -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
  compositor_resample_step.slice_id AS compositor_resample_slice_id,
  compositor_resample_step.task_start_time_ts AS compositor_resample_task_ts,
  compositor_resample_step.ts AS compositor_resample_ts,
  -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
  compositor_receive_begin_frame_step.id AS compositor_receive_begin_frame_slice_id,
  compositor_receive_begin_frame_step.task_start_time_ts AS compositor_receive_begin_frame_task_ts,
  compositor_receive_begin_frame_step.ts AS compositor_receive_begin_frame_ts,
  --
  compositor_generate_compositor_frame_step.id AS compositor_generate_compositor_frame_slice_id,
  compositor_generate_compositor_frame_step.task_start_time_ts AS compositor_generate_compositor_frame_task_ts,
  compositor_generate_compositor_frame_step.ts AS compositor_generate_compositor_frame_ts,
  -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
  compositor_submit_compositor_frame_step.id AS compositor_submit_compositor_frame_slice_id,
  compositor_submit_compositor_frame_step.ts AS compositor_submit_compositor_frame_ts,
  compositor_submit_compositor_frame_step.ts + compositor_submit_compositor_frame_step.dur AS compositor_submit_compositor_frame_end_ts,
  -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
  viz_receive_compositor_frame_step.id AS viz_receive_compositor_frame_slice_id,
  viz_receive_compositor_frame_step.task_start_time_ts AS viz_receive_compositor_frame_task_ts,
  viz_receive_compositor_frame_step.ts AS viz_receive_compositor_frame_ts,
  viz_receive_compositor_frame_step.ts + viz_receive_compositor_frame_step.dur AS viz_receive_compositor_frame_end_ts,
  viz_receive_compositor_frame_step.utid AS viz_compositor_utid,
  -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
  viz_draw_and_swap_step.id AS viz_draw_and_swap_slice_id,
  viz_draw_and_swap_step.task_start_time_ts AS viz_draw_and_swap_task_ts,
  viz_draw_and_swap_step.ts AS viz_draw_and_swap_ts,
  -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
  viz_send_buffer_swap_step.id AS viz_send_buffer_swap_slice_id,
  viz_send_buffer_swap_step.ts + viz_send_buffer_swap_step.dur AS viz_send_buffer_swap_end_ts,
  -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
  viz_swap_buffers_step.id AS viz_swap_buffers_slice_id,
  viz_swap_buffers_step.task_start_time_ts AS viz_swap_buffers_task_ts,
  viz_swap_buffers_step.ts AS viz_swap_buffers_ts,
  viz_swap_buffers_step.ts + viz_swap_buffers_step.dur AS viz_swap_buffers_end_ts,
  viz_swap_buffers_step.utid AS viz_gpu_thread_utid,
  -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
  chrome_event_latency.buffer_available_timestamp,
  chrome_event_latency.buffer_ready_timestamp,
  chrome_event_latency.latch_timestamp,
  chrome_event_latency.presentation_timestamp
FROM chrome_scroll_update_refs AS refs
LEFT JOIN chrome_event_latencies AS chrome_event_latency
  ON chrome_event_latency.scroll_update_id = refs.presentation_latency_id
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
LEFT JOIN chrome_input_pipeline_steps AS compositor_resample_step
  ON compositor_resample_step.latency_id = refs.presentation_latency_id
  AND compositor_resample_step.step = 'STEP_RESAMPLE_SCROLL_EVENTS'
  AND compositor_resample_step.input_type = 'GESTURE_SCROLL_UPDATE_EVENT'
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
LEFT JOIN chrome_graphics_pipeline_surface_frame_steps AS compositor_receive_begin_frame_step
  ON compositor_receive_begin_frame_step.surface_frame_trace_id = refs.surface_frame_id
  AND compositor_receive_begin_frame_step.step = 'STEP_RECEIVE_BEGIN_FRAME'
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
LEFT JOIN chrome_graphics_pipeline_surface_frame_steps AS compositor_generate_compositor_frame_step
  ON compositor_generate_compositor_frame_step.surface_frame_trace_id = refs.surface_frame_id
  AND compositor_generate_compositor_frame_step.step = 'STEP_GENERATE_COMPOSITOR_FRAME'
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
LEFT JOIN chrome_graphics_pipeline_surface_frame_steps AS compositor_submit_compositor_frame_step
  ON compositor_submit_compositor_frame_step.surface_frame_trace_id = refs.surface_frame_id
  AND compositor_submit_compositor_frame_step.step = 'STEP_SUBMIT_COMPOSITOR_FRAME'
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
LEFT JOIN chrome_graphics_pipeline_surface_frame_steps AS viz_receive_compositor_frame_step
  ON viz_receive_compositor_frame_step.surface_frame_trace_id = refs.surface_frame_id
  AND viz_receive_compositor_frame_step.step = 'STEP_RECEIVE_COMPOSITOR_FRAME'
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
LEFT JOIN chrome_graphics_pipeline_display_frame_steps AS viz_draw_and_swap_step
  ON viz_draw_and_swap_step.display_trace_id = refs.display_trace_id
  AND viz_draw_and_swap_step.step = 'STEP_DRAW_AND_SWAP'
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
LEFT JOIN chrome_graphics_pipeline_display_frame_steps AS viz_send_buffer_swap_step
  ON viz_send_buffer_swap_step.display_trace_id = refs.display_trace_id
  AND viz_send_buffer_swap_step.step = 'STEP_SEND_BUFFER_SWAP'
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
LEFT JOIN chrome_graphics_pipeline_display_frame_steps AS viz_swap_buffers_step
  ON viz_swap_buffers_step.display_trace_id = refs.display_trace_id
  AND viz_swap_buffers_step.step = 'STEP_BUFFER_SWAP_POST_SUBMIT'
-- Filter out inputs which were coalesced into a different frame (so that rows
-- of this table correspond to frames).
WHERE
  refs.scroll_update_latency_id = refs.presentation_latency_id;

-- Timestamps and durations for the frame-associated (after coalescing inputs
-- into a frame) stages of a scroll.
CREATE PERFETTO TABLE chrome_scroll_update_frame_pipeline (
  -- Id of the `LatencyInfo.Flow` slices corresponding to this scroll event.
  id LONG,
  -- Id of the aggregated frame this scroll update was presented in.
  display_trace_id LONG,
  -- Vsync interval (in milliseconds).
  vsync_interval_ms DOUBLE,
  -- Slice id for the `STEP_RESAMPLE_SCROLL_EVENTS` slice.
  compositor_resample_slice_id LONG,
  -- Timestamp for the `STEP_RESAMPLE_SCROLL_EVENTS` slice.
  compositor_resample_ts TIMESTAMP,
  -- Timestamp for the `STEP_RECEIVE_BEGIN_FRAME` slice or the
  -- containing task (if available).
  compositor_receive_begin_frame_ts TIMESTAMP,
  -- Slice id for the `STEP_GENERATE_COMPOSITOR_FRAME` slice.
  compositor_generate_compositor_frame_slice_id LONG,
  -- Timestamp for the `STEP_GENERATE_COMPOSITOR_FRAME` slice or the
  -- containing task (if available).
  compositor_generate_compositor_frame_ts TIMESTAMP,
  -- Duration between generating and submitting the compositor frame.
  compositor_generate_frame_to_submit_frame_dur DURATION,
  -- Slice id for the `STEP_SUBMIT_COMPOSITOR_FRAME` slice.
  compositor_submit_compositor_frame_slice_id LONG,
  -- Timestamp for the `STEP_SUBMIT_COMPOSITOR_FRAME` slice.
  compositor_submit_compositor_frame_ts TIMESTAMP,
  -- Duration for submitting the compositor frame (to viz).
  compositor_submit_frame_dur DURATION,
  -- End timestamp for the `STEP_SUBMIT_COMPOSITOR_FRAME` slice.
  compositor_submit_compositor_frame_end_ts TIMESTAMP,
  -- Delay when a compositor frame is sent from the renderer to viz.
  compositor_to_viz_delay_dur DURATION,
  -- Utid for the viz compositor thread.
  viz_compositor_utid LONG,
  -- Slice id for the `STEP_RECEIVE_COMPOSITOR_FRAME` slice.
  viz_receive_compositor_frame_slice_id LONG,
  -- Timestamp for the `STEP_RECEIVE_COMPOSITOR_FRAME` slice or the
  -- containing task (if available).
  viz_receive_compositor_frame_ts TIMESTAMP,
  -- Duration of the viz work done on receiving the compositor frame.
  viz_receive_compositor_frame_dur DURATION,
  -- End timestamp for the `STEP_RECEIVE_COMPOSITOR_FRAME` slice.
  viz_receive_compositor_frame_end_ts TIMESTAMP,
  -- Duration between viz receiving the compositor frame to frame draw.
  viz_wait_for_draw_dur DURATION,
  -- Slice id for the `STEP_DRAW_AND_SWAP` slice.
  viz_draw_and_swap_slice_id LONG,
  -- Timestamp for the `STEP_DRAW_AND_SWAP` slice or the
  -- containing task (if available).
  viz_draw_and_swap_ts TIMESTAMP,
  -- Duration for the viz drawing/swapping work for this frame.
  viz_draw_and_swap_dur DURATION,
  -- Slice id for the `STEP_SEND_BUFFER_SWAP` slice.
  viz_send_buffer_swap_slice_id LONG,
  -- End timestamp for the `STEP_SEND_BUFFER_SWAP` slice.
  viz_send_buffer_swap_end_ts TIMESTAMP,
  -- Delay between viz work on compositor thread and `CompositorGpuThread`.
  viz_to_gpu_delay_dur DURATION,
  -- Utid for the viz `CompositorGpuThread`.
  viz_gpu_thread_utid LONG,
  -- Slice id for the `STEP_BUFFER_SWAP_POST_SUBMIT` slice.
  viz_swap_buffers_slice_id LONG,
  -- Timestamp for the `STEP_BUFFER_SWAP_POST_SUBMIT` slice or the
  -- containing task (if available).
  viz_swap_buffers_ts TIMESTAMP,
  -- Duration of frame buffer swapping work on viz.
  viz_swap_buffers_dur DURATION,
  -- End timestamp for the `STEP_BUFFER_SWAP_POST_SUBMIT` slice.
  viz_swap_buffers_end_ts TIMESTAMP,
  -- Duration of `EventLatency`'s `BufferReadyToLatch` step.
  viz_swap_buffers_to_latch_dur DURATION,
  -- Timestamp for `EventLatency`'s `LatchToSwapEnd` step.
  latch_timestamp TIMESTAMP,
  -- Duration of either `EventLatency`'s `LatchToSwapEnd` +
  -- `SwapEndToPresentationCompositorFrame` steps or its `LatchToPresentation`
  -- step.
  viz_latch_to_presentation_dur DURATION,
  -- Presentation timestamp for the frame.
  presentation_timestamp TIMESTAMP
) AS
WITH
  processed_timestamps_and_metadata AS (
    SELECT
      id,
      display_trace_id,
      vsync_interval_ms,
      -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
      -- Ids
      compositor_resample_slice_id,
      -- Timestamps
      coalesce(compositor_resample_task_ts, compositor_resample_ts) AS compositor_resample_ts,
      -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
      -- Ids
      compositor_receive_begin_frame_slice_id,
      -- Timestamps
      coalesce(compositor_receive_begin_frame_task_ts, compositor_receive_begin_frame_ts) AS compositor_receive_begin_frame_ts,
      -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
      -- Ids
      compositor_generate_compositor_frame_slice_id,
      -- Timestamps
      coalesce(
        compositor_generate_compositor_frame_task_ts,
        compositor_generate_compositor_frame_ts
      ) AS compositor_generate_compositor_frame_ts,
      -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
      -- Ids
      compositor_submit_compositor_frame_slice_id,
      -- Timestamps
      compositor_submit_compositor_frame_ts,
      compositor_submit_compositor_frame_end_ts,
      -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
      -- Ids
      viz_compositor_utid,
      viz_receive_compositor_frame_slice_id,
      -- Timestamps
      coalesce(viz_receive_compositor_frame_task_ts, viz_receive_compositor_frame_ts) AS viz_receive_compositor_frame_ts,
      viz_receive_compositor_frame_end_ts,
      -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
      -- Ids
      viz_draw_and_swap_slice_id,
      -- Timestamps
      coalesce(viz_draw_and_swap_task_ts, viz_draw_and_swap_ts) AS viz_draw_and_swap_ts,
      -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
      -- Ids
      viz_send_buffer_swap_slice_id,
      -- Timestamps
      viz_send_buffer_swap_end_ts,
      -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
      -- Ids
      viz_gpu_thread_utid,
      viz_swap_buffers_slice_id,
      -- Timestamps
      coalesce(viz_swap_buffers_task_ts, viz_swap_buffers_ts) AS viz_swap_buffers_ts,
      viz_swap_buffers_end_ts,
      -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
      -- Timestamps
      latch_timestamp,
      presentation_timestamp
    FROM _scroll_update_frame_timestamps_and_metadata
  )
SELECT
  id,
  display_trace_id,
  -- TODO(b:381062412): This is sometimes unexpectedly 0; check/fix this.
  vsync_interval_ms,
  -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
  -- On `compositor_utid`.
  compositor_resample_slice_id,
  compositor_resample_ts,
  -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
  -- On `compositor_utid`.
  compositor_receive_begin_frame_ts,
  -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
  -- On `compositor_utid`.
  compositor_generate_compositor_frame_slice_id,
  -- TODO(b:380868337): This is sometimes unexpectedly null; check/fix this.
  compositor_generate_compositor_frame_ts,
  compositor_submit_compositor_frame_ts - compositor_generate_compositor_frame_ts AS compositor_generate_frame_to_submit_frame_dur,
  -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
  -- On `compositor_utid`.
  compositor_submit_compositor_frame_slice_id,
  compositor_submit_compositor_frame_ts,
  compositor_submit_compositor_frame_end_ts - compositor_submit_compositor_frame_ts AS compositor_submit_frame_dur,
  -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
  -- No applicable utid (duration between two threads).
  -- No applicable slice id (duration between two threads).
  compositor_submit_compositor_frame_end_ts,
  -- TODO(b:380868337): This is sometimes negative; check/fix this.
  viz_receive_compositor_frame_ts - compositor_submit_compositor_frame_end_ts AS compositor_to_viz_delay_dur,
  -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
  viz_compositor_utid,
  viz_receive_compositor_frame_slice_id,
  viz_receive_compositor_frame_ts,
  viz_receive_compositor_frame_end_ts - viz_receive_compositor_frame_ts AS viz_receive_compositor_frame_dur,
  -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
  -- On `viz_compositor_utid`.
  -- No applicable slice id (duration between two slices).
  viz_receive_compositor_frame_end_ts,
  viz_draw_and_swap_ts - viz_receive_compositor_frame_end_ts AS viz_wait_for_draw_dur,
  -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
  -- On `viz_compositor_utid`.
  viz_draw_and_swap_slice_id,
  viz_draw_and_swap_ts,
  viz_send_buffer_swap_end_ts - viz_draw_and_swap_ts AS viz_draw_and_swap_dur,
  -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
  -- No applicable utid (duration between two threads).
  viz_send_buffer_swap_slice_id,
  viz_send_buffer_swap_end_ts,
  viz_swap_buffers_ts - viz_send_buffer_swap_end_ts AS viz_to_gpu_delay_dur,
  -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
  viz_gpu_thread_utid,
  viz_swap_buffers_slice_id,
  viz_swap_buffers_ts,
  viz_swap_buffers_end_ts - viz_swap_buffers_ts AS viz_swap_buffers_dur,
  -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
  viz_swap_buffers_end_ts,
  latch_timestamp - viz_swap_buffers_end_ts AS viz_swap_buffers_to_latch_dur,
  -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
  latch_timestamp,
  presentation_timestamp - latch_timestamp AS viz_latch_to_presentation_dur,
  -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
  presentation_timestamp
FROM processed_timestamps_and_metadata;

-- Defines slices for all of the individual scrolls in a trace based on the
-- LatencyInfo-based scroll definition.
--
-- NOTE: this view of top level scrolls is based on the LatencyInfo definition
-- of a scroll, which differs subtly from the definition based on
-- EventLatencies.
-- TODO(b/278684408): add support for tracking scrolls across multiple Chrome/
-- WebView instances. Currently gesture_scroll_id unique within an instance, but
-- is not unique across multiple instances. Switching to an EventLatency based
-- definition of scrolls should resolve this.
CREATE PERFETTO TABLE chrome_scrolls (
  -- The unique identifier of the scroll.
  id LONG,
  -- The start timestamp of the scroll.
  ts TIMESTAMP,
  -- The duration of the scroll.
  dur DURATION,
  -- The earliest timestamp of the EventLatency slice of the GESTURE_SCROLL_BEGIN type for the
  -- corresponding scroll id.
  gesture_scroll_begin_ts TIMESTAMP,
  -- The earliest timestamp of the EventLatency slice of the GESTURE_SCROLL_END type /
  -- the latest timestamp of the EventLatency slice of the GESTURE_SCROLL_UPDATE type for the
  -- corresponding scroll id.
  gesture_scroll_end_ts TIMESTAMP
) AS
SELECT
  scroll_id AS id,
  min(ts) AS ts,
  cast_int!(MAX(ts + dur) - MIN(ts)) AS dur,
  -- TODO(b:389055670): Remove this once the UI doesn't rely on it.
  NULL AS gesture_scroll_begin_ts,
  NULL AS gesture_scroll_end_ts
FROM chrome_gesture_scroll_updates
GROUP BY
  scroll_id;

-- Timestamps and durations for the critical path stages during scrolling.
-- This table covers both the input-associated (before coalescing inputs into a
-- frame) and frame-associated (after coalescing inputs into a frame) stages of
-- a scroll:
--
--                              ...
--                               |
--                +--------------+--------------+
--                |                             |
--                V                             V
--   +-------------------------+   +-------------------------+
--   | _scroll_update_INPUT_   |   | _scroll_update_FRAME_   |
--   | timestamps_and_metadata |   | timestamps_and_metadata |
--   +------------+------------+   +------------+------------+
--                |                             |
--                V                             V
--    +-----------------------+     +-----------------------+
--    | chrome_scroll_update_ |     | chrome_scroll_update_ |
--    |     INPUT_pipeline    |     |     FRAME_pipeline    |
--    +-----------+-----------+     +-----------+-----------+
--                |                             |
--                +--------------+--------------+
--                               |
--                               V
--                 +---------------------------+
--                 | chrome_scroll_update_info |
--                 +---------------------------+
CREATE PERFETTO TABLE chrome_scroll_update_info (
  -- Id of the `LatencyInfo.Flow` slices corresponding to this scroll event.
  id LONG,
  -- Id of the scroll this scroll update belongs to.
  scroll_id LONG,
  -- Id (`LatencyInfo.ID`) of the previous input in this scroll.
  previous_input_id LONG,
  -- Id (`display_trace_id`) of the aggregated frame which this scroll update
  -- was presented in.
  frame_display_id LONG,
  -- Vsync interval (in milliseconds).
  vsync_interval_ms DOUBLE,
  -- Whether this input event was presented.
  is_presented BOOL,
  -- Whether the corresponding frame is janky based on the
  -- Event.ScrollJank.DelayedFramesPercentage.FixedWindow metric. This comes
  -- directly from `perfetto.protos.EventLatency.is_janky_scrolled_frame`.
  is_janky BOOL,
  -- Whether the corresponding frame is janky based on the
  -- Event.ScrollJank.DelayedFramesPercentage.FixedWindow3 metric. This comes
  -- directly from `perfetto.protos.EventLatency.is_janky_scrolled_frame_v3`.
  is_janky_v3 BOOL,
  -- Whether the corresponding scroll is inertial (fling).
  -- If this is `true`, "generation" and "touch_move" related timestamps and
  -- durations will be null.
  is_inertial BOOL,
  -- Whether this is the first update in a scroll.
  -- First scroll update can never be janky.
  is_first_scroll_update_in_scroll BOOL,
  -- Whether this is the first input that was presented in the frame.
  is_first_scroll_update_in_frame BOOL,
  -- Duration from the start of the browser process to the first
  -- input generation timestamp.
  browser_uptime_dur DURATION,
  -- Input generation timestamp (from the Android system).
  generation_ts TIMESTAMP,
  -- Duration from the generation timestamp to the end of InputReader's work.
  -- Only populated when atrace 'input' category is enabled.
  input_reader_dur DURATION,
  -- Duration of InputDispatcher's work.
  -- Only populated when atrace 'input' category is enabled.
  input_dispatcher_dur DURATION,
  -- Duration from the generation timestamp for the previous input to
  -- this input's generation timestamp.
  since_previous_generation_dur DURATION,
  -- Duration from input generation to when the browser received the input.
  generation_to_browser_main_dur DURATION,
  -- Utid for the browser main thread.
  browser_utid LONG,
  -- Slice id for the `STEP_SEND_INPUT_EVENT_UI` slice for the touch move.
  touch_move_received_slice_id LONG,
  -- Timestamp for the `STEP_SEND_INPUT_EVENT_UI` slice for the touch move.
  touch_move_received_ts TIMESTAMP,
  -- Duration for processing  a `TouchMove` event.
  touch_move_processing_dur DURATION,
  -- Slice id for the `STEP_SEND_INPUT_EVENT_UI` slice for the gesture scroll.
  scroll_update_created_slice_id LONG,
  -- Timestamp for the `STEP_SEND_INPUT_EVENT_UI` slice for the gesture scroll.
  scroll_update_created_ts TIMESTAMP,
  -- Duration for creating a `GestureScrollUpdate` from a `TouchMove` event.
  scroll_update_processing_dur DURATION,
  -- End timestamp for the `STEP_SEND_INPUT_EVENT_UI` slice for the above.
  scroll_update_created_end_ts TIMESTAMP,
  -- Duration between the browser and compositor dispatch.
  browser_to_compositor_delay_dur DURATION,
  -- Utid for the renderer compositor thread.
  compositor_utid LONG,
  -- Slice id for the `STEP_HANDLE_INPUT_EVENT_IMPL` slice.
  compositor_dispatch_slice_id LONG,
  -- Timestamp for the `STEP_HANDLE_INPUT_EVENT_IMPL` slice or the
  -- containing task (if available).
  compositor_dispatch_ts TIMESTAMP,
  -- Duration for the compositor dispatch itself.
  compositor_dispatch_dur DURATION,
  -- End timestamp for the `STEP_HANDLE_INPUT_EVENT_IMPL` slice.
  compositor_dispatch_end_ts TIMESTAMP,
  -- Duration between compositor dispatch and input resampling work.
  compositor_dispatch_to_on_begin_frame_delay_dur DURATION,
  -- Slice id for the `STEP_RESAMPLE_SCROLL_EVENTS` slice.
  compositor_resample_slice_id LONG,
  -- Slice id for the `STEP_DID_HANDLE_INPUT_AND_OVERSCROLL` slice.
  compositor_coalesced_input_handled_slice_id LONG,
  -- Start timestamp for work done on the input during "OnBeginFrame".
  compositor_on_begin_frame_ts TIMESTAMP,
  -- Duration of the "OnBeginFrame" work for this input.
  compositor_on_begin_frame_dur DURATION,
  -- End timestamp for work done on the input during "OnBeginFrame".
  compositor_on_begin_frame_end_ts TIMESTAMP,
  -- Delay until the compositor work for generating the frame begins.
  compositor_on_begin_frame_to_generation_delay_dur DURATION,
  -- Slice id for the `STEP_GENERATE_COMPOSITOR_FRAME` slice.
  compositor_generate_compositor_frame_slice_id LONG,
  -- Timestamp for the `STEP_GENERATE_COMPOSITOR_FRAME` slice or the
  -- containing task (if available).
  compositor_generate_compositor_frame_ts TIMESTAMP,
  -- Duration between generating and submitting the compositor frame.
  compositor_generate_frame_to_submit_frame_dur DURATION,
  -- Slice id for the `STEP_SUBMIT_COMPOSITOR_FRAME` slice.
  compositor_submit_compositor_frame_slice_id LONG,
  -- Timestamp for the `STEP_SUBMIT_COMPOSITOR_FRAME` slice.
  compositor_submit_compositor_frame_ts TIMESTAMP,
  -- Duration for submitting the compositor frame (to viz).
  compositor_submit_frame_dur DURATION,
  -- End timestamp for the `STEP_SUBMIT_COMPOSITOR_FRAME` slice.
  compositor_submit_compositor_frame_end_ts TIMESTAMP,
  -- Delay when a compositor frame is sent from the renderer to viz.
  compositor_to_viz_delay_dur DURATION,
  -- Utid for the viz compositor thread.
  viz_compositor_utid LONG,
  -- Slice id for the `STEP_RECEIVE_COMPOSITOR_FRAME` slice.
  viz_receive_compositor_frame_slice_id LONG,
  -- Timestamp for the `STEP_RECEIVE_COMPOSITOR_FRAME` slice or the
  -- containing task (if available).
  viz_receive_compositor_frame_ts TIMESTAMP,
  -- Duration of the viz work done on receiving the compositor frame.
  viz_receive_compositor_frame_dur DURATION,
  -- End timestamp for the `STEP_RECEIVE_COMPOSITOR_FRAME` slice.
  viz_receive_compositor_frame_end_ts TIMESTAMP,
  -- Duration between viz receiving the compositor frame to frame draw.
  viz_wait_for_draw_dur DURATION,
  -- Slice id for the `STEP_DRAW_AND_SWAP` slice.
  viz_draw_and_swap_slice_id LONG,
  -- Timestamp for the `STEP_DRAW_AND_SWAP` slice or the
  -- containing task (if available).
  viz_draw_and_swap_ts TIMESTAMP,
  -- Duration for the viz drawing/swapping work for this frame.
  viz_draw_and_swap_dur DURATION,
  -- Slice id for the `STEP_SEND_BUFFER_SWAP` slice.
  viz_send_buffer_swap_slice_id LONG,
  -- End timestamp for the `STEP_SEND_BUFFER_SWAP` slice.
  viz_send_buffer_swap_end_ts TIMESTAMP,
  -- Delay between viz work on compositor thread and `CompositorGpuThread`.
  viz_to_gpu_delay_dur DURATION,
  -- Utid for the viz `CompositorGpuThread`.
  viz_gpu_thread_utid LONG,
  -- Slice id for the `STEP_BUFFER_SWAP_POST_SUBMIT` slice.
  viz_swap_buffers_slice_id LONG,
  -- Timestamp for the `STEP_BUFFER_SWAP_POST_SUBMIT` slice or the
  -- containing task (if available).
  viz_swap_buffers_ts TIMESTAMP,
  -- Duration of frame buffer swapping work on viz.
  viz_swap_buffers_dur DURATION,
  -- End timestamp for the `STEP_BUFFER_SWAP_POST_SUBMIT` slice.
  viz_swap_buffers_end_ts TIMESTAMP,
  -- Duration of `EventLatency`'s `BufferReadyToLatch` step.
  viz_swap_buffers_to_latch_dur DURATION,
  -- Timestamp for `EventLatency`'s `LatchToSwapEnd` step.
  latch_timestamp TIMESTAMP,
  -- Duration of either `EventLatency`'s `LatchToSwapEnd` +
  -- `SwapEndToPresentationCompositorFrame` steps or its `LatchToPresentation`
  -- step.
  viz_latch_to_presentation_dur DURATION,
  -- Presentation timestamp for the frame.
  presentation_timestamp TIMESTAMP
) AS
SELECT
  input.id,
  input.scroll_id,
  lag(input.id) OVER (PARTITION BY input.scroll_id ORDER BY input.generation_ts) AS previous_input_id,
  frame.display_trace_id AS frame_display_id,
  -- TODO(b:381062412): This is sometimes unexpectedly 0; check/fix this.
  frame.vsync_interval_ms,
  input.is_presented,
  input.is_janky,
  input.is_janky_v3,
  input.is_inertial,
  input.is_first_scroll_update_in_scroll,
  input.is_first_scroll_update_in_frame,
  generation_ts - browser_process.start_ts AS browser_uptime_dur,
  -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
  -- No applicable utid (duration between two threads).
  -- No applicable slice id (duration between two threads).
  input.generation_ts,
  input.input_reader_processing_end_ts - generation_ts AS input_reader_dur,
  input.input_dispatcher_processing_end_ts - input.input_reader_processing_end_ts AS input_dispatcher_dur,
  input.generation_ts - lag(input.generation_ts) OVER (PARTITION BY input.scroll_id ORDER BY input.generation_ts) AS since_previous_generation_dur,
  input.generation_to_browser_main_dur,
  -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
  input.browser_utid,
  input.touch_move_received_slice_id,
  input.touch_move_received_ts,
  input.touch_move_processing_dur,
  -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
  -- On `browser_utid`.
  input.scroll_update_created_slice_id,
  input.scroll_update_created_ts,
  input.scroll_update_processing_dur,
  -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
  -- No applicable utid (duration between two threads).
  -- No applicable slice id (duration between two threads).
  input.scroll_update_created_end_ts,
  -- TODO(b:380868337): This is sometimes negative; check/fix this.
  input.browser_to_compositor_delay_dur,
  -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
  input.compositor_utid,
  input.compositor_dispatch_slice_id,
  input.compositor_dispatch_ts,
  input.compositor_dispatch_dur,
  -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
  -- On `compositor_utid`.
  -- No applicable slice id (duration between two slices).
  input.compositor_dispatch_end_ts,
  -- TODO(b:380868337): This is sometimes negative; check/fix this.
  -- TODO(b:381273884): use frame.compositor_receive_begin_frame_ts instead of
  -- input.compositor_dispatch_end_ts.
  coalesce(frame.compositor_resample_ts, input.compositor_coalesced_input_handled_ts) - input.compositor_dispatch_end_ts AS compositor_dispatch_to_on_begin_frame_delay_dur,
  -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
  -- On `compositor_utid`.
  -- `compositor_on_begin_frame_dur` can depend on two slices.
  frame.compositor_resample_slice_id,
  input.compositor_coalesced_input_handled_slice_id,
  coalesce(frame.compositor_resample_ts, input.compositor_coalesced_input_handled_ts) AS compositor_on_begin_frame_ts,
  input.compositor_coalesced_input_handled_end_ts - coalesce(frame.compositor_resample_ts, input.compositor_coalesced_input_handled_ts) AS compositor_on_begin_frame_dur,
  -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
  -- On `compositor_utid`.
  -- No applicable slice id (duration between two slices).
  input.compositor_coalesced_input_handled_end_ts AS compositor_on_begin_frame_end_ts,
  frame.compositor_generate_compositor_frame_ts - input.compositor_coalesced_input_handled_end_ts AS compositor_on_begin_frame_to_generation_delay_dur,
  -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
  -- On `compositor_utid`.
  frame.compositor_generate_compositor_frame_slice_id,
  -- TODO(b:380868337): This is sometimes unexpectedly null; check/fix this.
  frame.compositor_generate_compositor_frame_ts,
  frame.compositor_generate_frame_to_submit_frame_dur,
  -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
  -- On `compositor_utid`.
  frame.compositor_submit_compositor_frame_slice_id,
  frame.compositor_submit_compositor_frame_ts,
  frame.compositor_submit_frame_dur,
  -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
  -- No applicable utid (duration between two threads).
  -- No applicable slice id (duration between two threads).
  frame.compositor_submit_compositor_frame_end_ts,
  -- TODO(b:380868337): This is sometimes negative; check/fix this.
  frame.compositor_to_viz_delay_dur,
  -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
  frame.viz_compositor_utid,
  frame.viz_receive_compositor_frame_slice_id,
  frame.viz_receive_compositor_frame_ts,
  frame.viz_receive_compositor_frame_dur,
  -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
  -- On `viz_compositor_utid`.
  -- No applicable slice id (duration between two slices).
  frame.viz_receive_compositor_frame_end_ts,
  frame.viz_wait_for_draw_dur,
  -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
  -- On `viz_compositor_utid`.
  frame.viz_draw_and_swap_slice_id,
  frame.viz_draw_and_swap_ts,
  frame.viz_draw_and_swap_dur,
  -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
  -- No applicable utid (duration between two threads).
  frame.viz_send_buffer_swap_slice_id,
  frame.viz_send_buffer_swap_end_ts,
  frame.viz_to_gpu_delay_dur,
  -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
  frame.viz_gpu_thread_utid,
  frame.viz_swap_buffers_slice_id,
  frame.viz_swap_buffers_ts,
  frame.viz_swap_buffers_dur,
  -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
  frame.viz_swap_buffers_end_ts,
  frame.viz_swap_buffers_to_latch_dur,
  -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
  frame.latch_timestamp,
  frame.viz_latch_to_presentation_dur,
  -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
  frame.presentation_timestamp
FROM chrome_scroll_update_input_pipeline AS input
LEFT JOIN chrome_scroll_update_frame_pipeline AS frame
  ON input.presented_in_frame_id = frame.id
LEFT JOIN thread AS browser_main_thread
  ON browser_utid = browser_main_thread.utid
LEFT JOIN process AS browser_process
  ON browser_process.upid = browser_main_thread.upid;

-- Helper macro to compute the stage delta.
-- Should be used only as a part of `chrome_scroll_frame_info`.
CREATE PERFETTO MACRO _chrome_scroll_frame_stage_delta(
    name ColumnName
)
RETURNS Expr AS
iif(
  info.is_first_scroll_update_in_scroll,
  NULL,
  $name - lag($name) OVER (ORDER BY generation_ts)
);

-- A list of all presented Chrome frames which contain scroll updates and associated
-- metadata.
CREATE PERFETTO TABLE chrome_scroll_frame_info (
  -- Id (frame's display_trace_id) for the given frame.
  id LONG,
  -- Id of the scroll this scroll update belongs to.
  scroll_id LONG,
  -- Id (LatencyInfo.ID) of the last input before this frame.
  last_input_before_this_frame_id LONG,
  -- Vsync interval (in milliseconds).
  -- TODO(b/394303662): Remove in favour of `vsync_interval_dur`.
  vsync_interval_ms DOUBLE,
  -- Vsync interval (in nanoseconds).
  vsync_interval_dur DURATION,
  -- Whether the corresponding frame is janky based on the
  -- Event.ScrollJank.DelayedFramesPercentage.FixedWindow metric. This comes
  -- directly from `perfetto.protos.EventLatency.is_janky_scrolled_frame`.
  is_janky BOOL,
  -- Whether the corresponding frame is janky based on the
  -- Event.ScrollJank.DelayedFramesPercentage.FixedWindow3 metric. This comes
  -- directly from `perfetto.protos.EventLatency.is_janky_scrolled_frame_v3`.
  is_janky_v3 BOOL,
  -- Whether the corresponding scroll is inertial (fling).
  is_inertial BOOL,
  -- Sum of all input deltas for all scroll updates in this frame.
  -- These values are based on the delta of the OS input events.
  total_input_delta_y DOUBLE,
  -- Presented delta (change in page offset) for the given frame.
  -- This delta is computed by Chrome (based on the input events).
  presented_scrolled_delta_y DOUBLE,
  -- Duration from the start of the browser process to the first
  -- input generation timestamp.
  browser_uptime_dur DURATION,
  -- Input generation timestamp (from the Android system) for the first input.
  first_input_generation_ts TIMESTAMP,
  --  Duration from the generation timestamp to the end of InputReader's work.
  -- Only populated when atrace 'input' category is enabled.
  input_reader_dur DURATION,
  -- Duration of InputDispatcher's work.
  -- Only populated when atrace 'input' category is enabled.
  input_dispatcher_dur DURATION,
  -- Duration from the previous input (last input that wasn't part of this frame)
  -- to the first input in this frame.
  previous_last_input_to_first_input_generation_dur DURATION,
  -- Presentation timestamp for the frame.
  presentation_ts TIMESTAMP,
  -- Utid for the browser main thread.
  browser_utid JOINID(thread.id),
  -- Duration from input generation to when the browser received the first input
  -- in this frame.
  first_input_generation_to_browser_main_dur DURATION,
  -- Difference between `first_input_generation_to_browser_main_dur` for this
  -- frame and the previous frame in the same scroll.
  first_input_generation_to_browser_main_delta_dur DURATION,
  -- Duration for processing  a `TouchMove` event for the first input in this
  -- frame.
  first_input_touch_move_processing_dur DURATION,
  -- Difference between `first_input_touch_move_processing_dur` for this
  -- frame and the previous frame in the same scroll.
  first_input_touch_move_processing_delta_dur DURATION,
  -- Utid for the renderer compositor thread.
  compositor_utid JOINID(thread.id),
  -- Duration between the browser and compositor dispatch for the first input
  -- in this frame.
  first_input_browser_to_compositor_delay_dur DURATION,
  -- Difference between `first_input_browser_to_compositor_delay_dur` for this
  -- frame and the previous frame in the same scroll.
  first_input_browser_to_compositor_delay_delta_dur DURATION,
  -- Duration for the compositor dispatch for the first input in this frame.
  first_input_compositor_dispatch_dur DURATION,
  -- Difference between `first_input_compositor_dispatch_dur` for this frame and
  -- the previous frame in the same scroll.
  first_input_compositor_dispatch_delta_dur DURATION,
  -- Duration between the compositor dispatch and the "OnBeginFrame" work for the
  -- first input in this frame.
  first_input_compositor_dispatch_to_on_begin_frame_delay_dur DURATION,
  -- Difference between `first_input_compositor_dispatch_to_on_begin_frame_delay_dur`
  -- for this frame and the previous frame in the same scroll.
  first_input_compositor_dispatch_to_on_begin_frame_delay_delta_dur DURATION,
  -- Duration of the "OnBeginFrame" work for this frame.
  compositor_on_begin_frame_dur DURATION,
  -- Difference between `compositor_on_begin_frame_dur` for this frame and the
  -- previous frame in the same scroll.
  compositor_on_begin_frame_delta_dur DURATION,
  -- Duration between the "OnBeginFrame" work and the generation of this frame.
  compositor_on_begin_frame_to_generation_delay_dur DURATION,
  -- Difference between `compositor_on_begin_frame_to_generation_delay_dur` for
  -- this frame and the previous frame in the same scroll.
  compositor_on_begin_frame_to_generation_delay_delta_dur DURATION,
  -- Duration between the generation and submission of this frame.
  compositor_generate_frame_to_submit_frame_dur DURATION,
  -- Difference between `compositor_generate_frame_to_submit_frame_dur` for this
  -- frame and the previous frame in the same scroll.
  compositor_generate_frame_to_submit_frame_delta_dur DURATION,
  -- Duration for submitting this frame.
  compositor_submit_frame_dur DURATION,
  -- Difference between `compositor_submit_frame_dur` for this frame and the
  -- previous frame in the same scroll.
  compositor_submit_frame_delta_dur DURATION,
  -- Utid for the viz compositor thread.
  viz_compositor_utid JOINID(thread.id),
  -- Delay when a compositor frame is sent from the renderer to viz.
  compositor_to_viz_delay_dur DURATION,
  -- Difference between `compositor_to_viz_delay_dur` for this frame and the
  -- previous frame in the same scroll.
  compositor_to_viz_delay_delta_dur DURATION,
  -- Duration of the viz work done on receiving the compositor frame.
  viz_receive_compositor_frame_dur DURATION,
  -- Difference between `viz_receive_compositor_frame_dur` for this frame and the
  -- previous frame in the same scroll.
  viz_receive_compositor_frame_delta_dur DURATION,
  -- Duration between viz receiving the compositor frame to frame draw.
  viz_wait_for_draw_dur DURATION,
  -- Difference between `viz_wait_for_draw_dur` for this frame and the previous
  -- frame in the same scroll.
  viz_wait_for_draw_delta_dur DURATION,
  -- Duration of the viz drawing/swapping work for this frame.
  viz_draw_and_swap_dur DURATION,
  -- Difference between `viz_draw_and_swap_dur` for this frame and the previous
  -- frame in the same scroll.
  viz_draw_and_swap_delta_dur DURATION,
  -- Utid for the viz `CompositorGpuThread`.
  viz_gpu_thread_utid JOINID(thread.id),
  -- Delay between viz work on compositor thread and `CompositorGpuThread`.
  viz_to_gpu_delay_dur DURATION,
  -- Difference between `viz_to_gpu_delay_dur` for this frame and the previous
  -- frame in the same scroll.
  viz_to_gpu_delay_delta_dur DURATION,
  -- Duration of frame buffer swapping work on viz.
  viz_swap_buffers_dur DURATION,
  -- Difference between `viz_swap_buffers_dur` for this frame and the previous
  -- frame in the same scroll.
  viz_swap_buffers_delta_dur DURATION,
  -- Time between buffers ready until Choreographer's latch.
  viz_swap_buffers_to_latch_dur DURATION,
  -- Difference between `viz_swap_buffers_to_latch_dur` for this frame and the
  -- previous frame in the same scroll.
  viz_swap_buffers_to_latch_delta_dur DURATION,
  -- Duration between Choreographer's latch and presentation.
  viz_latch_to_presentation_dur DURATION,
  -- Difference between `viz_latch_to_presentation_dur` for this frame and the
  -- previous frame in the same scroll.
  viz_latch_to_presentation_delta_dur DURATION
) AS
SELECT
  frame_display_id AS id,
  info.scroll_id,
  previous_input_id AS last_input_before_this_frame_id,
  vsync_interval_ms,
  cast_int!(vsync_interval_ms * 1e6) AS vsync_interval_dur,
  is_janky,
  is_janky_v3,
  is_inertial,
  (
    SELECT
      sum(delta_y)
    FROM chrome_scroll_input_offsets AS input
    JOIN chrome_scroll_update_info AS update_info
      ON input.scroll_update_id = update_info.id
    WHERE
      update_info.frame_display_id = info.frame_display_id
  ) AS total_input_delta_y,
  delta.delta_y AS presented_scrolled_delta_y,
  browser_uptime_dur,
  info.generation_ts AS first_input_generation_ts,
  input_reader_dur,
  input_dispatcher_dur,
  info.since_previous_generation_dur AS previous_last_input_to_first_input_generation_dur,
  info.browser_utid,
  info.generation_to_browser_main_dur AS first_input_generation_to_browser_main_dur,
  presentation_timestamp AS presentation_ts,
  _chrome_scroll_frame_stage_delta!(generation_to_browser_main_dur) AS first_input_generation_to_browser_main_delta_dur,
  info.touch_move_processing_dur AS first_input_touch_move_processing_dur,
  _chrome_scroll_frame_stage_delta!(touch_move_processing_dur) AS first_input_touch_move_processing_delta_dur,
  info.compositor_utid,
  info.browser_to_compositor_delay_dur AS first_input_browser_to_compositor_delay_dur,
  _chrome_scroll_frame_stage_delta!(browser_to_compositor_delay_dur) AS first_input_browser_to_compositor_delay_delta_dur,
  info.compositor_dispatch_dur AS first_input_compositor_dispatch_dur,
  _chrome_scroll_frame_stage_delta!(compositor_dispatch_dur) AS first_input_compositor_dispatch_delta_dur,
  info.compositor_dispatch_to_on_begin_frame_delay_dur AS first_input_compositor_dispatch_to_on_begin_frame_delay_dur,
  _chrome_scroll_frame_stage_delta!(compositor_dispatch_to_on_begin_frame_delay_dur) AS first_input_compositor_dispatch_to_on_begin_frame_delay_delta_dur,
  info.compositor_on_begin_frame_dur,
  _chrome_scroll_frame_stage_delta!(compositor_on_begin_frame_dur) AS compositor_on_begin_frame_delta_dur,
  info.compositor_on_begin_frame_to_generation_delay_dur,
  _chrome_scroll_frame_stage_delta!(compositor_on_begin_frame_to_generation_delay_dur) AS compositor_on_begin_frame_to_generation_delay_delta_dur,
  info.compositor_generate_frame_to_submit_frame_dur,
  _chrome_scroll_frame_stage_delta!(compositor_generate_frame_to_submit_frame_dur) AS compositor_generate_frame_to_submit_frame_delta_dur,
  info.compositor_submit_frame_dur,
  _chrome_scroll_frame_stage_delta!(compositor_submit_frame_dur) AS compositor_submit_frame_delta_dur,
  viz_compositor_utid,
  info.compositor_to_viz_delay_dur,
  _chrome_scroll_frame_stage_delta!(compositor_to_viz_delay_dur) AS compositor_to_viz_delay_delta_dur,
  info.viz_receive_compositor_frame_dur,
  _chrome_scroll_frame_stage_delta!(viz_receive_compositor_frame_dur) AS viz_receive_compositor_frame_delta_dur,
  info.viz_wait_for_draw_dur,
  _chrome_scroll_frame_stage_delta!(viz_wait_for_draw_dur) AS viz_wait_for_draw_delta_dur,
  info.viz_draw_and_swap_dur,
  _chrome_scroll_frame_stage_delta!(viz_draw_and_swap_dur) AS viz_draw_and_swap_delta_dur,
  viz_gpu_thread_utid,
  info.viz_to_gpu_delay_dur,
  _chrome_scroll_frame_stage_delta!(viz_to_gpu_delay_dur) AS viz_to_gpu_delay_delta_dur,
  info.viz_swap_buffers_dur,
  _chrome_scroll_frame_stage_delta!(viz_swap_buffers_dur) AS viz_swap_buffers_delta_dur,
  info.viz_swap_buffers_to_latch_dur,
  _chrome_scroll_frame_stage_delta!(viz_swap_buffers_to_latch_dur) AS viz_swap_buffers_to_latch_delta_dur,
  info.viz_latch_to_presentation_dur,
  _chrome_scroll_frame_stage_delta!(viz_latch_to_presentation_dur) AS viz_latch_to_presentation_delta_dur
FROM chrome_scroll_update_info AS info
LEFT JOIN chrome_presented_scroll_offsets AS delta
  ON info.id = delta.scroll_update_id
-- TODO(b:380286381, b:393051057): remove the frame_display_id condition when dropped frames are handled.
WHERE
  is_first_scroll_update_in_frame AND info.frame_display_id IS NOT NULL;

-- Source of truth for the definition of the stages of a scroll. Mainly intended
-- for visualization purposes (e.g. in Chrome Scroll Jank plugin).
CREATE PERFETTO TABLE chrome_scroll_update_info_step_templates (
  -- The name of a stage of a scroll.
  step_name STRING,
  -- The name of the column in `chrome_scroll_update_info` which contains the
  -- timestamp of the stage.
  ts_column_name STRING,
  -- The name of the column in `chrome_scroll_update_info` which contains the
  -- duration of the stage. NULL if the stage doesn't have a duration.
  dur_column_name STRING
) AS
WITH
  steps(step_name, ts_column_name, dur_column_name) AS (
    SELECT
      *
    FROM (VALUES
      ('GenerationToBrowserMain', 'generation_ts', 'generation_to_browser_main_dur'),
      ('TouchMoveProcessing', 'touch_move_received_ts', 'touch_move_processing_dur'),
      (
        'ScrollUpdateProcessing',
        'scroll_update_created_ts',
        'scroll_update_processing_dur'
      ),
      (
        'BrowserMainToRendererCompositor',
        'scroll_update_created_end_ts',
        'browser_to_compositor_delay_dur'
      ),
      (
        'RendererCompositorDispatch',
        'compositor_dispatch_ts',
        'compositor_dispatch_dur'
      ),
      (
        'RendererCompositorDispatchToOnBeginFrame',
        'compositor_dispatch_end_ts',
        'compositor_dispatch_to_on_begin_frame_delay_dur'
      ),
      (
        'RendererCompositorBeginFrame',
        'compositor_on_begin_frame_ts',
        'compositor_on_begin_frame_dur'
      ),
      (
        'RendererCompositorBeginToGenerateFrame',
        'compositor_on_begin_frame_end_ts',
        'compositor_on_begin_frame_to_generation_delay_dur'
      ),
      (
        'RendererCompositorGenerateToSubmitFrame',
        'compositor_generate_compositor_frame_ts',
        'compositor_generate_frame_to_submit_frame_dur'
      ),
      (
        'RendererCompositorSubmitFrame',
        'compositor_submit_compositor_frame_ts',
        'compositor_submit_frame_dur'
      ),
      (
        'RendererCompositorToViz',
        'compositor_submit_compositor_frame_end_ts',
        'compositor_to_viz_delay_dur'
      ),
      (
        'VizReceiveFrame',
        'viz_receive_compositor_frame_ts',
        'viz_receive_compositor_frame_dur'
      ),
      (
        'VizReceiveToDrawFrame',
        'viz_receive_compositor_frame_end_ts',
        'viz_wait_for_draw_dur'
      ),
      ('VizDrawToSwapFrame', 'viz_draw_and_swap_ts', 'viz_draw_and_swap_dur'),
      ('VizToGpu', 'viz_send_buffer_swap_end_ts', 'viz_to_gpu_delay_dur'),
      ('VizSwapBuffers', 'viz_swap_buffers_ts', 'viz_swap_buffers_dur'),
      (
        'VizSwapBuffersToLatch',
        'viz_swap_buffers_end_ts',
        'viz_swap_buffers_to_latch_dur'
      ),
      ('VizLatchToPresentation', 'latch_timestamp', 'viz_latch_to_presentation_dur'),
      ('Presentation', 'presentation_timestamp', NULL)) AS _values
  )
SELECT
  step_name,
  ts_column_name,
  dur_column_name
FROM steps;