File: compositor_frame_sink_support.cc

package info (click to toggle)
chromium 139.0.7258.127-2
  • links: PTS, VCS
  • area: main
  • in suites: forky
  • size: 6,122,156 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 (1769 lines) | stat: -rw-r--r-- 69,280 bytes parent folder | download | duplicates (3)
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
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
// Copyright 2016 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "components/viz/service/frame_sinks/compositor_frame_sink_support.h"

#include <algorithm>
#include <string>
#include <utility>
#include <variant>

#include "base/check.h"
#include "base/containers/contains.h"
#include "base/functional/bind.h"
#include "base/functional/callback_helpers.h"
#include "base/memory/raw_ptr.h"
#include "base/metrics/histogram_macros.h"
#include "base/system/sys_info.h"
#include "base/task/sequenced_task_runner.h"
#include "base/time/time.h"
#include "base/trace_event/trace_id_helper.h"
#include "base/trace_event/typed_macros.h"
#include "build/build_config.h"
#include "cc/base/features.h"
#include "components/input/utils.h"
#include "components/viz/common/constants.h"
#include "components/viz/common/features.h"
#include "components/viz/common/frame_sinks/begin_frame_args.h"
#include "components/viz/common/frame_sinks/begin_frame_source.h"
#include "components/viz/common/quads/compositor_frame.h"
#include "components/viz/common/quads/compositor_frame_metadata.h"
#include "components/viz/common/quads/compositor_render_pass.h"
#include "components/viz/common/surfaces/surface_info.h"
#include "components/viz/common/surfaces/video_capture_target.h"
#include "components/viz/common/viz_utils.h"
#include "components/viz/service/display/display.h"
#include "components/viz/service/frame_sinks/frame_counter.h"
#include "components/viz/service/frame_sinks/frame_sink_bundle_impl.h"
#include "components/viz/service/frame_sinks/frame_sink_manager_impl.h"
#include "components/viz/service/layers/layer_context_impl.h"
#include "components/viz/service/surfaces/surface.h"
#include "components/viz/service/surfaces/surface_reference.h"
#include "components/viz/service/transitions/surface_animation_manager.h"
#include "media/filters/video_cadence_estimator.h"
#include "mojo/public/cpp/system/platform_handle.h"

// This determines whether the provided time since last interval corresponds
// to a cadence frame that needs to be rendered.
bool HasElapsedCadenceInterval(
    base::TimeDelta render_interval,
    base::TimeDelta frame_duration,
    base::TimeDelta time_since_last_render_interval) {
  base::TimeDelta time_at_next_interval =
      time_since_last_render_interval + render_interval;
  base::TimeDelta tolerance = render_interval * 0.5;
  return time_at_next_interval > (frame_duration + tolerance);
}

namespace viz {
namespace {

bool RecordShouldSendBeginFrame(const std::string& reason, bool should_send) {
  TRACE_EVENT2("viz", "SendBeginFrameDecision", "reason", reason, "should_send",
               should_send);
  return should_send;
}

void AdjustPresentationFeedback(gfx::PresentationFeedback* feedback,
                                base::TimeTicks swap_start) {
  // Swap start to end breakdown is always reported if ready timestamp is
  // available. The other timestamps are adjusted to assume 0 delay in those
  // stages if the breakdown is not available.
  if (feedback->ready_timestamp.is_null())
    return;

  feedback->available_timestamp =
      std::max(feedback->available_timestamp, swap_start);
  feedback->latch_timestamp =
      std::max(feedback->latch_timestamp, feedback->ready_timestamp);
}

// Takes a given `rect` and then transforms it to a given space via
// `transform_to_space` to be interested by an `intersectee_in_space`, then
// transforms it back to its original space. NOTE: `transform_to_space` must be
// invertible.
gfx::Rect IntersectInSpace(const gfx::Rect& rect,
                           const gfx::Transform& transform_to_space,
                           const gfx::Rect& intersectee_in_space) {
  const gfx::Rect rect_in_space = transform_to_space.MapRect(rect);

  const gfx::Rect intersected_in_space =
      gfx::IntersectRects(rect_in_space, intersectee_in_space);

  const std::optional<gfx::Rect> intersected =
      transform_to_space.InverseMapRect(intersected_in_space);

  return intersected.value_or(gfx::Rect{});
}

void RemoveSurfaceReferenceAndDispatchCopyOutputRequestCallback(
    base::WeakPtr<FrameSinkManagerImpl> frame_sink_manager,
    const SurfaceId& holds_ref_surface_id,
    const blink::SameDocNavigationScreenshotDestinationToken& destination_token,
    std::unique_ptr<CopyOutputResult> result) {
  if (!frame_sink_manager) {
    return;
  }
  if (auto* surface_holds_ref =
          frame_sink_manager->surface_manager()->GetSurfaceForId(
              holds_ref_surface_id)) {
    surface_holds_ref->ResetPendingCopySurfaceId();
  }
  // Send the IPC to the browser process even if `result` is empty. The empty
  // result will be handled on the browser side.
  frame_sink_manager->OnScreenshotCaptured(destination_token,
                                           std::move(result));
}

}  // namespace

CompositorFrameSinkSupport::CompositorFrameSinkSupport(
    mojom::CompositorFrameSinkClient* client,
    FrameSinkManagerImpl* frame_sink_manager,
    const FrameSinkId& frame_sink_id,
    bool is_root)
    : client_(client),
      frame_sink_manager_(frame_sink_manager),
      surface_manager_(frame_sink_manager->surface_manager()),
      frame_sink_id_(frame_sink_id),
      surface_resource_holder_(this),
      is_root_(is_root),
      allow_copy_output_requests_(is_root) {
  // This may result in SetBeginFrameSource() being called.
  frame_sink_manager_->RegisterCompositorFrameSinkSupport(frame_sink_id_, this);
}

CompositorFrameSinkSupport::~CompositorFrameSinkSupport() {
  // Shut down the layer context so that it no longer calls into
  // |this|, before doing other tear down.
  layer_context_.reset();

  // Unregister |this| as a BeginFrameObserver so that the
  // BeginFrameSource does not call into |this| after it's deleted.
  callback_received_begin_frame_ = true;
  callback_received_receive_ack_ = true;
  frame_timing_details_.clear();
  SetNeedsBeginFrame(false);

  // For display root surfaces the surface is no longer going to be visible.
  // Make it unreachable from the top-level root.
  if (referenced_local_surface_id_.has_value()) {
    auto reference = MakeTopLevelRootReference(
        SurfaceId(frame_sink_id_, referenced_local_surface_id_.value()));
    surface_manager_->RemoveSurfaceReferences({reference});
  }

  if (last_activated_surface_id_.is_valid())
    EvictLastActiveSurface();
  if (last_created_surface_id_.is_valid())
    surface_manager_->MarkSurfaceForDestruction(last_created_surface_id_);
  frame_sink_manager_->UnregisterCompositorFrameSinkSupport(frame_sink_id_);
  // When we unregister `this` from `frame_sink_manager_` from above,
  // `FrameSinkManagerImpl::DiscardPendingCopyOfOutputRequests` clears out all
  // the outstanding requests.
  // However there are other destruction code paths that don't exercise
  // `DiscardPendingCopyOfOutputRequests` (e.g, in unittests we might not have a
  // `RootCompositorFrameSinkImpl`, such that no `ExternalBeginFrameSourceMojo`
  // is added as an observer of `frame_sink_manager_`). Therefore we explicitly
  // clear the requests here regardless.
  ClearAllPendingCopyOutputRequests();

  // No video capture clients should remain after calling
  // UnregisterCompositorFrameSinkSupport().
  DCHECK(capture_clients_.empty());

  if (begin_frame_source_ && added_frame_observer_)
    begin_frame_source_->RemoveObserver(this);

  if (bundle_id_.has_value()) {
    if (auto* bundle = frame_sink_manager_->GetFrameSinkBundle(*bundle_id_)) {
      bundle->RemoveFrameSink(this);
    }
  }
}

FrameTimingDetailsMap CompositorFrameSinkSupport::TakeFrameTimingDetailsMap() {
  FrameTimingDetailsMap map;
  map.swap(frame_timing_details_);

  // As we're clearing `frame_timing_details_`, we might no longer need
  // BeginFrame if delivering presentation feedback was the only reason.
  UpdateNeedsBeginFramesInternal();
  return map;
}

void CompositorFrameSinkSupport::SetUpHitTest(
    LatestLocalSurfaceIdLookupDelegate* local_surface_id_lookup_delegate) {
  DCHECK(is_root_);
  hit_test_aggregator_ = std::make_unique<HitTestAggregator>(
      frame_sink_manager_->hit_test_manager(), frame_sink_manager_,
      local_surface_id_lookup_delegate, frame_sink_id_);
}

void CompositorFrameSinkSupport::SetAggregatedDamageCallbackForTesting(
    AggregatedDamageCallback callback) {
  aggregated_damage_callback_ = std::move(callback);
}

void CompositorFrameSinkSupport::SetBeginFrameSource(
    BeginFrameSource* begin_frame_source) {
  if (begin_frame_source_ && added_frame_observer_) {
    begin_frame_source_->RemoveObserver(this);
    // When detaching from source, CompositorFrameSinkClient needs to know that
    // there are no more OnBeginFrame. Otherwise, client in renderer could wait
    // OnBeginFrame forever. e.g., crbug.com/1335000.
    // OnBeginFrameSourcePausedChanged(false) is not handled here because it's
    // handled in AddObserver() depending on current status of begin frame
    // source.
    if (!begin_frame_source)
      OnBeginFrameSourcePausedChanged(true);
    added_frame_observer_ = false;
  }

  auto* old_source = begin_frame_source_.get();
  begin_frame_source_ = begin_frame_source;

  FrameSinkBundleImpl* bundle = nullptr;
  if (bundle_id_) {
    bundle = frame_sink_manager_->GetFrameSinkBundle(*bundle_id_);
    if (!bundle) {
      // Our bundle has been destroyed.
      ScheduleSelfDestruction();
      return;
    }
    bundle->UpdateFrameSink(this, old_source);
  }

  UpdateNeedsBeginFramesInternal();
}

void CompositorFrameSinkSupport::SetBundle(const FrameSinkBundleId& bundle_id) {
  auto* bundle = frame_sink_manager_->GetFrameSinkBundle(bundle_id);
  if (!bundle) {
    // The bundle is gone already, so force the client to re-establish their
    // CompositorFrameSink.
    ScheduleSelfDestruction();
    return;
  }

  bundle->AddFrameSink(this);
  bundle_id_ = bundle_id;
  UpdateNeedsBeginFramesInternal();
}

void CompositorFrameSinkSupport::SetLastKnownVsync(
    base::TimeDelta vsync_interval) {
  if (last_known_vsync_interval_ == vsync_interval) {
    return;
  }
  last_known_vsync_interval_ = vsync_interval;
  ThrottleBeginFrame(begin_frame_interval_, /*simple_cadence_only=*/true);
}

bool CompositorFrameSinkSupport::ThrottleBeginFrame(base::TimeDelta interval,
                                                    bool simple_cadence_only) {
  if (!interval.is_positive()) {
    // Remove any existing throttling
    begin_frame_interval_ = base::TimeDelta();
    return true;
  }

  if (!last_known_vsync_interval_.is_positive() || !simple_cadence_only) {
    // No known vsync interval or forcing throttle interval.
    begin_frame_interval_ = interval;
    return true;
  }

  if (media::VideoCadenceEstimator::HasSimpleCadence(
          last_known_vsync_interval_, interval, kMaxTimeUntilNextGlitch)) {
    begin_frame_interval_ = interval;
    return true;
  } else {
    begin_frame_interval_ = base::TimeDelta();
    return false;
  }
}

void CompositorFrameSinkSupport::OnSurfaceCommitted(Surface* surface) {
  if (surface->HasPendingFrame()) {
    // Make sure we periodically check if the frame should activate.
    pending_surfaces_.insert(surface);
    UpdateNeedsBeginFramesInternal();
  }
}

void CompositorFrameSinkSupport::OnSurfaceActivated(Surface* surface) {
  DCHECK(surface);
  DCHECK(surface->HasActiveFrame());
  DCHECK(!surface->HasPendingFrame());

  pending_surfaces_.erase(surface);
  if (pending_surfaces_.empty())
    UpdateNeedsBeginFramesInternal();

  const CompositorFrameMetadata& active_frame_metadata =
      surface->GetActiveFrameMetadata();
  for (const auto& directive : active_frame_metadata.transition_directives) {
    ProcessCompositorFrameTransitionDirective(directive, surface);
  }

  // The directives above generate TransferableResources which are required to
  // replace shared elements with the corresponding cached snapshots. This step
  // must be done after processing directives above.
  SurfaceAnimationManager::ReplaceSharedElementResources(
      surface, view_transition_token_to_animation_manager_);

  if (surface->surface_id() == last_activated_surface_id_)
    return;

  Surface* previous_surface =
      surface_manager_->GetSurfaceForId(last_activated_surface_id_);

  if (!previous_surface) {
    last_activated_surface_id_ = surface->surface_id();
  } else if (previous_surface->GetActiveFrameIndex() <
             surface->GetActiveFrameIndex()) {
    surface_manager_->MarkSurfaceForDestruction(last_activated_surface_id_);
    last_activated_surface_id_ = surface->surface_id();
    // TODO(samans): Why is this not done when creating the surface?
    surface->SetPreviousFrameSurface(previous_surface);
  } else {
    DCHECK_GT(previous_surface->GetActiveFrameIndex(),
              surface->GetActiveFrameIndex());
    // We can get into a situation where a child-initiated synchronization is
    // deferred until after a parent-initiated synchronization happens resulting
    // in activations happening out of order. In that case, we simply discard
    // the stale surface.
    surface_manager_->MarkSurfaceForDestruction(surface->surface_id());
  }

  // Check if this is a display root surface and the SurfaceId is changing.
  if (is_root_ && (!referenced_local_surface_id_ ||
                   *referenced_local_surface_id_ !=
                       last_activated_surface_id_.local_surface_id())) {
    UpdateDisplayRootReference(surface);
  }

  MaybeEvictSurfaces();

  // Update |device_scale_factor_| if it changes with latest activated surface.
  float new_device_scale_factor = active_frame_metadata.device_scale_factor;
  if (device_scale_factor_ != new_device_scale_factor) {
    frame_sink_manager_->OnFrameSinkDeviceScaleFactorChanged(
        surface->surface_id().frame_sink_id(), new_device_scale_factor);
    device_scale_factor_ = new_device_scale_factor;
  }

  if (is_mobile_optimized_ != active_frame_metadata.is_mobile_optimized) {
    is_mobile_optimized_ = active_frame_metadata.is_mobile_optimized;
    frame_sink_manager_->OnFrameSinkMobileOptimizedChanged(
        frame_sink_id_, is_mobile_optimized_);
  }
}

void CompositorFrameSinkSupport::OnSurfaceWillDraw(Surface* surface) {
  if (last_drawn_frame_index_ >= surface->GetActiveFrameIndex())
    return;
  last_drawn_frame_index_ = surface->GetActiveFrameIndex();
}

void CompositorFrameSinkSupport::OnFrameTokenChanged(uint32_t frame_token) {
  frame_sink_manager_->OnFrameTokenChanged(frame_sink_id_, frame_token);
}

void CompositorFrameSinkSupport::SendCompositorFrameAck() {
  DidReceiveCompositorFrameAck();
}

void CompositorFrameSinkSupport::OnSurfaceAggregatedDamage(
    Surface* surface,
    const LocalSurfaceId& local_surface_id,
    const CompositorFrame& frame,
    const gfx::Rect& damage_rect,
    base::TimeTicks expected_display_time) {
  DCHECK(!damage_rect.IsEmpty());

  const gfx::Size& frame_size_in_pixels = frame.size_in_pixels();
  if (aggregated_damage_callback_) {
    aggregated_damage_callback_.Run(local_surface_id, frame_size_in_pixels,
                                    damage_rect, expected_display_time);
  }

  current_capture_bounds_ = frame.metadata.capture_bounds;
  for (CapturableFrameSink::Client* client : capture_clients_) {
    client->OnFrameDamaged(frame_size_in_pixels, damage_rect,
                           expected_display_time, frame.metadata);
  }
}

bool CompositorFrameSinkSupport::IsVideoCaptureStarted() {
  return number_clients_capturing_ > 0;
}

std::vector<Thread> CompositorFrameSinkSupport::GetThreads() {
  return threads_;
}

void CompositorFrameSinkSupport::OnSurfaceDestroyed(Surface* surface) {
  pending_surfaces_.erase(surface);

  if (surface->surface_id() == last_activated_surface_id_)
    last_activated_surface_id_ = SurfaceId();

  if (surface->surface_id() == last_created_surface_id_)
    last_created_surface_id_ = SurfaceId();

  if (!client_ || surface_returned_resources_.empty()) {
    return;
  }
  client_->ReclaimResources(std::move(surface_returned_resources_));
  surface_returned_resources_.clear();
}

void CompositorFrameSinkSupport::OnSurfacePresented(
    uint32_t frame_token,
    base::TimeTicks draw_start_timestamp,
    const gfx::SwapTimings& swap_timings,
    const gfx::PresentationFeedback& feedback) {
  // If the frame was submitted locally (from inside viz), do not tell the
  // client about it, since the client did not send it.
  if (frame_token != kLocalFrameToken) {
    DidPresentCompositorFrame(frame_token, draw_start_timestamp, swap_timings,
                              feedback);
  }
}

void CompositorFrameSinkSupport::RefResources(
    const std::vector<TransferableResource>& resources) {
  ForAllReservedResourceDelegates(
      [&resources](ReservedResourceDelegate& delegate) {
        delegate.RefResources(resources);
      });

  surface_resource_holder_.RefResources(resources);
}

void CompositorFrameSinkSupport::UnrefResources(
    std::vector<ReturnedResource> resources) {
  // `ReservedResourceDelegate` allocates ResourceIds in a different range
  // than the client so it can process returned resources before
  // |surface_resource_holder_|.
  ForAllReservedResourceDelegates(
      [&resources](ReservedResourceDelegate& delegate) {
        delegate.UnrefResources(resources);
      });

  surface_resource_holder_.UnrefResources(std::move(resources));
}

void CompositorFrameSinkSupport::ReturnResources(
    std::vector<ReturnedResource> resources) {
  if (resources.empty())
    return;

  if (layer_context_) {
    // Resource management is delegated to LayerContext when it's in use.
    layer_context_->ReceiveReturnsFromParent(std::move(resources));
    return;
  }

  DoReturnResources(std::move(resources));
}

void CompositorFrameSinkSupport::DoReturnResources(
    std::vector<ReturnedResource> resources) {
  // When we attempt to return resources in DidReceiveCompositorFrameAck.
  // However if there are no pending frames then we don't expect that signal
  // soon. In which case we return the resources to the `client_` now.
  if (pending_frames_.empty() && client_) {
    client_->ReclaimResources(std::move(resources));
    return;
  }

  std::move(resources.begin(), resources.end(),
            std::back_inserter(surface_returned_resources_));
}

void CompositorFrameSinkSupport::ReceiveFromChild(
    const std::vector<TransferableResource>& resources) {
  ForAllReservedResourceDelegates(
      [&resources](ReservedResourceDelegate& delegate) {
        delegate.ReceiveFromChild(resources);
      });

  surface_resource_holder_.ReceiveFromChild(resources);
}

std::vector<PendingCopyOutputRequest>
CompositorFrameSinkSupport::TakeCopyOutputRequests(
    const LocalSurfaceId& latest_local_id) {
  std::vector<PendingCopyOutputRequest> results;
  for (auto it = copy_output_requests_.begin();
       it != copy_output_requests_.end();) {
    // Pick up the requests that require an exact `LocalSurfaceId` match.
    if (it->capture_exact_surface_id) {
      // `ui::DelegatedFrameHostAndroid` won't send a `CopyOutputRequest`
      // without a valid `LocalSurfaceId`. This is guaranteed as we can't
      // serialize/deserialize an empty `LocalSurfaceId`.
      CHECK(it->local_surface_id.is_valid());
      if (it->local_surface_id == latest_local_id) {
        results.push_back(std::move(*it));
        it = copy_output_requests_.erase(it);
      } else {
        ++it;
      }
    }
    // Requests with a non-valid local id should be satisfied as soon as
    // possible.
    else if (!it->local_surface_id.is_valid() ||  // NOLINT
             it->local_surface_id <= latest_local_id) {
      results.push_back(std::move(*it));
      it = copy_output_requests_.erase(it);
    } else {
      ++it;
    }
  }
  return results;
}

void CompositorFrameSinkSupport::EvictSurface(const LocalSurfaceId& id) {
  DCHECK(id.embed_token() != last_evicted_local_surface_id_.embed_token() ||
         id.parent_sequence_number() >=
             last_evicted_local_surface_id_.parent_sequence_number());
  last_evicted_local_surface_id_ = id;
  surface_manager_->DropTemporaryReference(SurfaceId(frame_sink_id_, id));
  MaybeEvictSurfaces();
  if (client_) {
    client_->OnSurfaceEvicted(id);
  }
}

void CompositorFrameSinkSupport::MaybeEvictSurfaces() {
  if (IsEvicted(last_activated_surface_id_.local_surface_id()))
    EvictLastActiveSurface();
  if (IsEvicted(last_created_surface_id_.local_surface_id())) {
    surface_manager_->MarkSurfaceForDestruction(last_created_surface_id_);
    last_created_surface_id_ = SurfaceId();
  }
}

void CompositorFrameSinkSupport::EvictLastActiveSurface() {
  SurfaceId to_destroy_surface_id = last_activated_surface_id_;
  if (last_created_surface_id_ == last_activated_surface_id_)
    last_created_surface_id_ = SurfaceId();
  last_activated_surface_id_ = SurfaceId();
  surface_manager_->MarkSurfaceForDestruction(to_destroy_surface_id);

  // For display root surfaces the surface is no longer going to be visible.
  // Make it unreachable from the top-level root.
  if (referenced_local_surface_id_.has_value()) {
    auto reference = MakeTopLevelRootReference(
        SurfaceId(frame_sink_id_, referenced_local_surface_id_.value()));
    surface_manager_->RemoveSurfaceReferences({reference});
    referenced_local_surface_id_.reset();
  }
}

void CompositorFrameSinkSupport::SetNeedsBeginFrame(bool needs_begin_frame) {
  client_needs_begin_frame_ = needs_begin_frame;
  UpdateNeedsBeginFramesInternal();
}

void CompositorFrameSinkSupport::SetWantsAnimateOnlyBeginFrames() {
  wants_animate_only_begin_frames_ = true;
}

void CompositorFrameSinkSupport::SetAutoNeedsBeginFrame() {
  auto_needs_begin_frame_ = true;
}

bool CompositorFrameSinkSupport::WantsAnimateOnlyBeginFrames() const {
  return wants_animate_only_begin_frames_;
}

void CompositorFrameSinkSupport::BindLayerContext(
    mojom::PendingLayerContext& context,
    bool draw_mode_is_gpu) {
  layer_context_ =
      std::make_unique<LayerContextImpl>(this, context, draw_mode_is_gpu);
}

void CompositorFrameSinkSupport::SetThreads(
    bool from_untrusted_client,
    std::vector<Thread> unverified_threads) {
  if (!from_untrusted_client) {
    threads_ = std::move(unverified_threads);
    return;
  }
  base::flat_set<base::PlatformThreadId> thread_ids;
  for (const auto& thread : unverified_threads) {
    thread_ids.insert(base::PlatformThreadId(thread.id));
  }
  frame_sink_manager_->VerifySandboxedThreadIds(
      thread_ids,
      base::BindOnce(
          &CompositorFrameSinkSupport::UpdateThreadIdsPostVerification,
          weak_factory_.GetWeakPtr(), unverified_threads));
}

void CompositorFrameSinkSupport::UpdateThreadIdsPostVerification(
    std::vector<Thread> threads,
    bool passed_verification) {
  if (passed_verification) {
    threads_ = std::move(threads);
  } else {
    std::vector<base::PlatformThreadId> thread_ids;
    for (const auto& thread : threads) {
      thread_ids.push_back(thread.id);
    }
    TRACE_EVENT_INSTANT("viz,android.adpf",
                        "FailedToUpdateThreadIdsPostVerification", "thread_ids",
                        thread_ids);
  }
}

bool CompositorFrameSinkSupport::IsRoot() const {
  return is_root_;
}

void CompositorFrameSinkSupport::DidNotProduceFrame(const BeginFrameAck& ack) {
  TRACE_EVENT(
      "viz,benchmark,graphics.pipeline", "Graphics.Pipeline",
      perfetto::Flow::Global(ack.trace_id), [&](perfetto::EventContext ctx) {
        base::TaskAnnotator::EmitTaskTimingDetails(ctx);
        auto* event = ctx.event<perfetto::protos::pbzero::ChromeTrackEvent>();
        auto* data = event->set_chrome_graphics_pipeline();
        data->set_step(perfetto::protos::pbzero::ChromeGraphicsPipeline::
                           StepName::STEP_DID_NOT_PRODUCE_FRAME);
        frame_sink_id_.WriteIntoTrace(ctx.Wrap(data->set_frame_sink_id()));
        data->set_surface_frame_trace_id(ack.trace_id);
      });
  DCHECK(ack.frame_id.IsSequenceValid());

  begin_frame_tracker_.ReceivedAck(ack);

  // Override the has_damage flag (ignoring invalid data from clients).
  BeginFrameAck modified_ack(ack);
  modified_ack.has_damage = false;

  // If the client doesn't produce a frame, we assume it's no longer interactive
  // for scheduling.
  if (last_activated_surface_id_.is_valid())
    surface_manager_->SurfaceModified(last_activated_surface_id_, modified_ack,
                                      SurfaceObserver::HandleInteraction::kNo);

  if (begin_frame_source_) {
    begin_frame_source_->DidFinishFrame(this);
    frame_sink_manager_->DidFinishFrame(frame_sink_id_, last_begin_frame_args_);
  }
}

void CompositorFrameSinkSupport::SubmitCompositorFrame(
    const LocalSurfaceId& local_surface_id,
    CompositorFrame frame,
    std::optional<HitTestRegionList> hit_test_region_list,
    uint64_t submit_time) {
  const auto result = MaybeSubmitCompositorFrame(
      local_surface_id, std::move(frame), std::move(hit_test_region_list),
      submit_time,
      mojom::CompositorFrameSink::SubmitCompositorFrameSyncCallback());
  DCHECK_EQ(result, SubmitResult::ACCEPTED);
}

SubmitResult CompositorFrameSinkSupport::MaybeSubmitCompositorFrame(
    const LocalSurfaceId& local_surface_id,
    CompositorFrame frame,
    std::optional<HitTestRegionList> hit_test_region_list,
    uint64_t submit_time,
    mojom::CompositorFrameSink::SubmitCompositorFrameSyncCallback callback) {
  if (!client_needs_begin_frame_ && auto_needs_begin_frame_) {
    // SetNeedsBeginFrame(true) below may cause `last_begin_frame_args_` to be
    // updated.
    BeginFrameId old_frame_id = last_begin_frame_args_.frame_id;

    handling_auto_needs_begin_frame_ = true;
    SetNeedsBeginFrame(true);
    handling_auto_needs_begin_frame_ = false;

    // If the unsolicited frame has manual frame source and a new BeginFrameArgs
    // is received because of this unsolicited frame, update the frame ID to
    // to match the BeginFrameArgs, so that the corresponding surface won't be
    // considered as pending.
    if (frame.metadata.begin_frame_ack.frame_id.source_id ==
            BeginFrameArgs::kManualSourceId &&
        last_begin_frame_args_.frame_id != old_frame_id) {
      CHECK(last_begin_frame_args_.frame_id.IsSequenceValid());

      frame.metadata.begin_frame_ack.frame_id = last_begin_frame_args_.frame_id;
    }
  }

  TRACE_EVENT(
      "viz,benchmark,graphics.pipeline", "Graphics.Pipeline",
      perfetto::Flow::Global((frame.metadata.begin_frame_ack.trace_id)),
      [&](perfetto::EventContext ctx) {
        base::TaskAnnotator::EmitTaskTimingDetails(ctx);
        auto* event = ctx.event<perfetto::protos::pbzero::ChromeTrackEvent>();
        auto* data = event->set_chrome_graphics_pipeline();
        data->set_step(perfetto::protos::pbzero::ChromeGraphicsPipeline::
                           StepName::STEP_RECEIVE_COMPOSITOR_FRAME);
        frame_sink_id_.WriteIntoTrace(ctx.Wrap(data->set_frame_sink_id()));
        data->set_surface_frame_trace_id(
            frame.metadata.begin_frame_ack.trace_id);
      });

  DCHECK(local_surface_id.is_valid());
  DCHECK(!frame.render_pass_list.empty());
  DCHECK(!frame.size_in_pixels().IsEmpty());

  CHECK(callback_received_begin_frame_);
  CHECK(callback_received_receive_ack_);

  begin_frame_tracker_.ReceivedAck(frame.metadata.begin_frame_ack);
  pending_frames_.push_back(FrameData{.local_frame = false});

  compositor_frame_callback_ = std::move(callback);
  if (compositor_frame_callback_) {
    callback_received_begin_frame_ = false;
    callback_received_receive_ack_ = false;
    UpdateNeedsBeginFramesInternal();
  }

  base::TimeTicks now_time = base::TimeTicks::Now();
  pending_received_frame_times_.emplace(
      frame.metadata.frame_token,
      std::make_unique<PendingFrameDetails>(now_time, surface_manager_));

  // Override the has_damage flag (ignoring invalid data from clients).
  frame.metadata.begin_frame_ack.has_damage = true;
  DCHECK(frame.metadata.begin_frame_ack.frame_id.IsSequenceValid());

  if (!ui::LatencyInfo::Verify(
          frame.metadata.latency_info,
          "CompositorFrameSinkSupport::MaybeSubmitCompositorFrame")) {
    for (auto& info : frame.metadata.latency_info) {
      info.Terminate();
    }
    std::vector<ui::LatencyInfo>().swap(frame.metadata.latency_info);
  }
  for (ui::LatencyInfo& latency : frame.metadata.latency_info) {
    if (latency.latency_components().size() > 0) {
      latency.AddLatencyNumberWithTimestamp(
          ui::DISPLAY_COMPOSITOR_RECEIVED_FRAME_COMPONENT, now_time);
    }
  }

  base::ScopedClosureRunner frame_rejected_callback(
      base::BindOnce(&CompositorFrameSinkSupport::DidRejectCompositorFrame,
                     weak_factory_.GetWeakPtr(), frame.metadata.frame_token,
                     frame.resource_list, frame.metadata.latency_info));

  // Ensure no CopyOutputRequests have been submitted if they are banned.
  if (!allow_copy_output_requests_ && frame.HasCopyOutputRequests()) {
    TRACE_EVENT_INSTANT0("viz", "CopyOutputRequests not allowed",
                         TRACE_EVENT_SCOPE_THREAD);
    return SubmitResult::COPY_OUTPUT_REQUESTS_NOT_ALLOWED;
  }

  // TODO(crbug.com/40578019): It should be possible to use
  // |frame.metadata.frame_token| instead of maintaining a |last_frame_index_|.
  uint64_t frame_index = ++last_frame_index_;

  if (features::ShouldOnBeginFrameThrottleVideo()) {
    const auto& interval_info =
        frame.metadata.frame_interval_inputs.content_interval_info;
    auto info_itr =
        std::find_if(interval_info.begin(), interval_info.end(),
                     [](const ContentFrameIntervalInfo& info) {
                       return info.type == ContentFrameIntervalType::kVideo;
                     });
    if (info_itr != interval_info.end()) {
      base::TimeDelta preferred_frame_interval = info_itr->frame_interval;

      // Skip throttling for very small changes in frame interval.
      // A value of 2 ms proved to be enough to not have throttle firing during
      // a constant video playback but can be changed to a higher value if
      // over firing occurs in some edge case while always aiming to keep it
      // lower than a full frame interval.
      if ((last_known_frame_interval_ - preferred_frame_interval).magnitude() >
          base::Milliseconds(2)) {
        TRACE_EVENT_INSTANT2("viz", "Set sink framerate",
                             TRACE_EVENT_SCOPE_THREAD, "interval",
                             preferred_frame_interval, "sourceid",
                             frame.metadata.begin_frame_ack.frame_id.source_id);
        last_known_frame_interval_ = preferred_frame_interval;
        // Only throttle simple cadences.
        ThrottleBeginFrame(preferred_frame_interval,
                           /*simple_cadence_only=*/true);
      }
    }
  }

  Surface* prev_surface =
      surface_manager_->GetSurfaceForId(last_created_surface_id_);
  Surface* current_surface = nullptr;
  if (prev_surface &&
      local_surface_id == last_created_surface_id_.local_surface_id()) {
    current_surface = prev_surface;
  } else {
    TRACE_EVENT_WITH_FLOW2(
        TRACE_DISABLED_BY_DEFAULT("viz.surface_id_flow"),
        "LocalSurfaceId.Submission.Flow",
        TRACE_ID_GLOBAL(local_surface_id.submission_trace_id()),
        TRACE_EVENT_FLAG_FLOW_IN | TRACE_EVENT_FLAG_FLOW_OUT, "step",
        "ReceiveCompositorFrame", "local_surface_id",
        local_surface_id.ToString());

    SurfaceId surface_id(frame_sink_id_, local_surface_id);
    SurfaceInfo surface_info(surface_id, frame.device_scale_factor(),
                             frame.size_in_pixels());

    // LocalSurfaceIds should be monotonically increasing. This ID is used
    // to determine the freshness of a surface at aggregation time.
    const LocalSurfaceId& last_created_local_surface_id =
        last_created_surface_id_.local_surface_id();

    bool child_initiated_synchronization_event =
        last_created_local_surface_id.is_valid() &&
        local_surface_id.child_sequence_number() >
            last_created_local_surface_id.child_sequence_number();

    // Neither sequence numbers of the LocalSurfaceId can decrease and at least
    // one must increase.
    bool monotonically_increasing_id =
        (local_surface_id.parent_sequence_number() >=
             last_created_local_surface_id.parent_sequence_number() &&
         local_surface_id.child_sequence_number() >=
             last_created_local_surface_id.child_sequence_number()) &&
        (local_surface_id.parent_sequence_number() >
             last_created_local_surface_id.parent_sequence_number() ||
         child_initiated_synchronization_event);

    DCHECK(surface_info.is_valid());
    if (local_surface_id.embed_token() ==
            last_created_local_surface_id.embed_token() &&
        !monotonically_increasing_id) {
      TRACE_EVENT_INSTANT0("viz", "LocalSurfaceId decreased",
                           TRACE_EVENT_SCOPE_THREAD);
      return SubmitResult::SURFACE_ID_DECREASED;
    }

    // Don't recreate a surface that was previously evicted. Drop the
    // CompositorFrame and return all its resources.
    if (IsEvicted(local_surface_id)) {
      TRACE_EVENT_INSTANT0("viz", "Submit rejected to evicted surface",
                           TRACE_EVENT_SCOPE_THREAD);
      return SubmitResult::ACCEPTED;
    }

    const bool has_copy_request_against_prev_surface =
        frame.metadata.screenshot_destination.has_value() && prev_surface;

    current_surface = surface_manager_->CreateSurface(
        weak_factory_.GetWeakPtr(), surface_info,
        has_copy_request_against_prev_surface ? last_created_surface_id_
                                              : SurfaceId());

    // The previous surface needs to be valid to generate a screenshot.
    //
    // NOTE: In order for the previous surface to be copied, it needs to be
    // reachable and kept alive. This is achieved by adding a reference from the
    // current surface to the previous surface. Normally this reference is
    // removed when the copy is finished in Viz. However we could run into an
    // edge case where the frame sink is destroyed before the copy is finished.
    // If that happens, we will rely on the GC of the current surface to remove
    // the reference.
    if (has_copy_request_against_prev_surface) {
      auto copy_request = std::make_unique<CopyOutputRequest>(
          CopyOutputRequest::ResultFormat::RGBA,
          CopyOutputRequest::ResultDestination::kSystemMemory,
          base::BindOnce(
              &RemoveSurfaceReferenceAndDispatchCopyOutputRequestCallback,
              frame_sink_manager_->GetWeakPtr(), surface_info.id(),
              frame.metadata.screenshot_destination.value()));
      if (const auto& size_for_testing =
              frame_sink_manager_
                  ->copy_output_request_result_size_for_testing();  // IN-TEST
          !size_for_testing.IsEmpty()) [[unlikely]] {
        SetCopyOutputRequestResultSize(copy_request.get(), gfx::Rect(),
                                       size_for_testing,
                                       prev_surface->size_in_pixels());
      }

      copy_request->set_result_task_runner(
          base::SequencedTaskRunner::GetCurrentDefault());

      RequestCopyOfOutput(
          PendingCopyOutputRequest(last_created_surface_id_.local_surface_id(),
                                   SubtreeCaptureId{}, std::move(copy_request),
                                   /*capture_exact_id=*/true));
    }

    if (!current_surface) {
      TRACE_EVENT_INSTANT0("viz", "Surface belongs to another client",
                           TRACE_EVENT_SCOPE_THREAD);
      return SubmitResult::SURFACE_OWNED_BY_ANOTHER_CLIENT;
    }
    last_created_surface_id_ = SurfaceId(frame_sink_id_, local_surface_id);

    surface_manager_->SurfaceDamageExpected(current_surface->surface_id(),
                                            last_begin_frame_args_);
  }

  // We use `last_created_surface_id_` to use the embedding timestamp of the
  // last Surface which was successfully presented before this point, in case
  // the frame is rejected. Now that we know this frame can be presented, use
  // its correct SurfaceId.
  pending_received_frame_times_[frame.metadata.frame_token]->set_surface_id(
      last_created_surface_id_);

  pending_received_frame_times_[frame.metadata.frame_token]->set_frame_id(
      frame.metadata.begin_frame_ack.frame_id);

  const int64_t trace_id = ~frame.metadata.begin_frame_ack.trace_id;
  TRACE_EVENT_WITH_FLOW1(TRACE_DISABLED_BY_DEFAULT("viz.hit_testing_flow"),
                         "Event.Pipeline", TRACE_ID_GLOBAL(trace_id),
                         TRACE_EVENT_FLAG_FLOW_IN | TRACE_EVENT_FLAG_FLOW_OUT,
                         "step", "ReceiveHitTestData");

  // QueueFrame can fail in unit tests, so SubmitHitTestRegionList has to be
  // called before that.
  frame_sink_manager()->SubmitHitTestRegionList(
      last_created_surface_id_, frame_index, std::move(hit_test_region_list));

  Surface::QueueFrameResult result = current_surface->QueueFrame(
      std::move(frame), frame_index, std::move(frame_rejected_callback));
  switch (result) {
    case Surface::QueueFrameResult::REJECTED:
      TRACE_EVENT_INSTANT0("viz", "QueueFrame failed",
                           TRACE_EVENT_SCOPE_THREAD);
      return SubmitResult::SIZE_MISMATCH;
    case Surface::QueueFrameResult::ACCEPTED_PENDING:
      // Pending frames are processed in OnSurfaceCommitted.
      break;
    case Surface::QueueFrameResult::ACCEPTED_ACTIVE:
      // Nothing to do here.
      break;
  }

  if (begin_frame_source_) {
    begin_frame_source_->DidFinishFrame(this);
    frame_sink_manager_->DidFinishFrame(frame_sink_id_, last_begin_frame_args_);
  }

  return SubmitResult::ACCEPTED;
}

void CompositorFrameSinkSupport::NotifyNewLocalSurfaceIdExpectedWhilePaused() {
  if (!last_activated_surface_id_.is_valid()) {
    return;
  }
  Surface* previous_surface =
      surface_manager_->GetSurfaceForId(last_activated_surface_id_);
  previous_surface->ClearNonRootCopyRequests();
}

SurfaceReference CompositorFrameSinkSupport::MakeTopLevelRootReference(
    const SurfaceId& surface_id) {
  return SurfaceReference(surface_manager_->GetRootSurfaceId(), surface_id);
}

void CompositorFrameSinkSupport::HandleCallback() {
  if (!compositor_frame_callback_ || !callback_received_begin_frame_ ||
      !callback_received_receive_ack_) {
    return;
  }

  std::move(compositor_frame_callback_)
      .Run(std::move(surface_returned_resources_));
  surface_returned_resources_.clear();
}

void CompositorFrameSinkSupport::DidReceiveCompositorFrameAck() {
  DCHECK(!pending_frames_.empty());
  // TODO(https://crbug.com/40902503): Drawing from a layer context is indeed
  // local, but we'll likely want to use a different resource return policy.
  bool was_local_frame = pending_frames_.front().local_frame || layer_context_;
  pending_frames_.pop_front();

  if (!client_)
    return;

  // If this frame came from viz directly and not from the client, don't send
  // the client an ack, since it didn't do anything. Just return the resources.
  if (was_local_frame) {
    client_->ReclaimResources(std::move(surface_returned_resources_));
    surface_returned_resources_.clear();
    return;
  }

  // If we have a callback, we only return the resource on onBeginFrame.
  if (compositor_frame_callback_) {
    callback_received_receive_ack_ = true;
    UpdateNeedsBeginFramesInternal();
    HandleCallback();
    return;
  }

  client_->DidReceiveCompositorFrameAck(std::move(surface_returned_resources_));
  surface_returned_resources_.clear();
}

void CompositorFrameSinkSupport::DidPresentCompositorFrame(
    uint32_t frame_token,
    base::TimeTicks draw_start_timestamp,
    const gfx::SwapTimings& swap_timings,
    const gfx::PresentationFeedback& feedback) {
  CHECK_NE(frame_token, kInvalidFrameToken);
  CHECK_NE(frame_token, kLocalFrameToken);
  DCHECK((feedback.flags & gfx::PresentationFeedback::kFailure) ||
         (!draw_start_timestamp.is_null() && !swap_timings.is_null()));

  DCHECK_LE(pending_received_frame_times_.size(), 25u);
  auto received_frame_timestamp =
      pending_received_frame_times_.find(frame_token);
  CHECK(received_frame_timestamp != pending_received_frame_times_.end());

  FrameTimingDetails details;
  details.received_compositor_frame_timestamp =
      received_frame_timestamp->second->frame_submit_timestamp();
  details.embedded_frame_timestamp =
      is_root_ ? details.received_compositor_frame_timestamp
               : received_frame_timestamp->second->frame_embed_timestamp();
  details.draw_start_timestamp = draw_start_timestamp;
  details.swap_timings = swap_timings;
  details.presentation_feedback = feedback;
  details.frame_id = received_frame_timestamp->second->frame_id();
  AdjustPresentationFeedback(&details.presentation_feedback,
                             swap_timings.swap_start);
  // Override with the throttled interval if one has been set. Otherwise,
  // consumers will assume that the default vsync interval was the target and
  // that the frames are presented too late when in fact, this is intentional.
  if (begin_frame_interval_.is_positive() &&
      details.presentation_feedback.interval.is_positive()) {
    details.presentation_feedback.interval = begin_frame_interval_;
  }
  pending_received_frame_times_.erase(received_frame_timestamp);

  // We should only ever get one PresentationFeedback per frame_token.
  CHECK(!frame_timing_details_.contains(frame_token));
  frame_timing_details_.emplace(frame_token, details);

  if (!feedback.failed() && frame_sink_manager_->frame_counter()) {
    frame_sink_manager_->frame_counter()->AddPresentedFrame(frame_sink_id_,
                                                            feedback.timestamp);
  }

  UpdateNeedsBeginFramesInternal();
}

void CompositorFrameSinkSupport::DidRejectCompositorFrame(
    uint32_t frame_token,
    std::vector<TransferableResource> frame_resource_list,
    std::vector<ui::LatencyInfo> latency_info) {
  TRACE_EVENT_INSTANT0("viz", "DidRejectCompositorFrame",
                       TRACE_EVENT_SCOPE_THREAD);
  // TODO(eseckler): Should these be stored and attached to the next successful
  // frame submission instead?
  for (ui::LatencyInfo& info : latency_info)
    info.Terminate();

  std::vector<ReturnedResource> resources =
      TransferableResource::ReturnResources(frame_resource_list);
  ReturnResources(std::move(resources));
  DidReceiveCompositorFrameAck();
  DidPresentCompositorFrame(frame_token, base::TimeTicks(), gfx::SwapTimings(),
                            gfx::PresentationFeedback::Failure());
}

void CompositorFrameSinkSupport::UpdateDisplayRootReference(
    const Surface* surface) {
  // Make the new SurfaceId reachable from the top-level root.
  surface_manager_->AddSurfaceReferences(
      {MakeTopLevelRootReference(surface->surface_id())});

  // Make the old SurfaceId unreachable from the top-level root if applicable.
  if (referenced_local_surface_id_) {
    surface_manager_->RemoveSurfaceReferences({MakeTopLevelRootReference(
        SurfaceId(frame_sink_id_, *referenced_local_surface_id_))});
  }

  referenced_local_surface_id_ = surface->surface_id().local_surface_id();
}

void CompositorFrameSinkSupport::OnBeginFrame(const BeginFrameArgs& args) {
  int64_t trace_id = base::trace_event::GetNextGlobalTraceId();
  TRACE_EVENT(
      "viz,benchmark,graphics.pipeline", "Graphics.Pipeline",
      perfetto::Flow::Global(trace_id),
      [trace_id, &args](perfetto::EventContext ctx) {
        base::TaskAnnotator::EmitTaskTimingDetails(ctx);
        auto* event = ctx.event<perfetto::protos::pbzero::ChromeTrackEvent>();
        auto* data = event->set_chrome_graphics_pipeline();
        data->set_step(perfetto::protos::pbzero::ChromeGraphicsPipeline::
                           StepName::STEP_ISSUE_BEGIN_FRAME);
        data->set_surface_frame_trace_id(trace_id);
        auto* possible_deadlines = data->set_possible_deadlines();
        possible_deadlines->set_frame_time_us(
            args.frame_time.since_origin().InMicroseconds());
        if (args.possible_deadlines.has_value()) {
          for (const PossibleDeadline& deadline :
               args.possible_deadlines->deadlines) {
            auto* timeline = possible_deadlines->add_frame_timeline();
            timeline->set_vsync_id(deadline.vsync_id);
            timeline->set_latch_delta_us(deadline.latch_delta.InMicroseconds());
            timeline->set_present_delta_us(
                deadline.present_delta.InMicroseconds());
          }
          possible_deadlines->set_preferred_frame_timeline_index(
              args.possible_deadlines->preferred_index);
        }
      });

  if (compositor_frame_callback_) {
    callback_received_begin_frame_ = true;
    UpdateNeedsBeginFramesInternal();
    HandleCallback();
  }

  CheckPendingSurfaces();

  BeginFrameArgs adjusted_args = args;
  adjusted_args.dispatch_time = base::TimeTicks::Now();
  if (begin_frame_interval_.is_positive()) {
    adjusted_args.interval = begin_frame_interval_;
    // Deadline is not necessarily frame_time + interval. For example, it may
    // incorporate an estimate for the frame's draw/swap time, so it's
    // desirable to preserve any offset from the next scheduled frame.
    base::TimeDelta offset_from_next_scheduled_frame =
        args.deadline - (args.frame_time + args.interval);
    adjusted_args.deadline = args.frame_time + begin_frame_interval_ +
                             offset_from_next_scheduled_frame;
  }
  SetLastKnownVsync(args.interval);
  const bool should_send_begin_frame =
      ShouldSendBeginFrame(args.frame_id, adjusted_args.frame_time,
                           args.interval) &&
      (client_ || layer_context_);
  if (should_send_begin_frame) {
    if (last_activated_surface_id_.is_valid())
      surface_manager_->SurfaceDamageExpected(last_activated_surface_id_,
                                              adjusted_args);
    last_begin_frame_args_ = adjusted_args;

    // Force full frame if surface not yet activated to ensure surface creation.
    if (!last_activated_surface_id_.is_valid())
      adjusted_args.animate_only = false;

    adjusted_args.trace_id = trace_id;
    adjusted_args.frames_throttled_since_last = frames_throttled_since_last_;
    frames_throttled_since_last_ = 0;

    last_frame_time_ = adjusted_args.frame_time;

    if (client_ && !handling_auto_needs_begin_frame_) {
      TRACE_EVENT(
          "graphics.pipeline", "Graphics.Pipeline",
          perfetto::Flow::Global(adjusted_args.trace_id),
          [&](perfetto::EventContext ctx) {
            auto* event =
                ctx.event<perfetto::protos::pbzero::ChromeTrackEvent>();
            auto* data = event->set_chrome_graphics_pipeline();
            data->set_step(perfetto::protos::pbzero::ChromeGraphicsPipeline::
                               StepName::STEP_SEND_ON_BEGIN_FRAME_MOJO_MESSAGE);
            data->set_surface_frame_trace_id(adjusted_args.trace_id);
          });
      client_->OnBeginFrame(adjusted_args, frame_timing_details_,
                            std::vector<ReturnedResource>());
      frame_timing_details_.clear();
    }

    begin_frame_tracker_.SentBeginFrame(adjusted_args);
    frame_sink_manager_->DidBeginFrame(frame_sink_id_, adjusted_args);
    UpdateNeedsBeginFramesInternal();

    if (layer_context_) {
      // NOTE: BeginFrame() re-enters `this` to finish the frame.
      layer_context_->BeginFrame(adjusted_args);
      frame_timing_details_.clear();
    }
  } else if (begin_frame_source_) {
    begin_frame_source_->DidFinishFrame(this);
  }
}

const BeginFrameArgs& CompositorFrameSinkSupport::LastUsedBeginFrameArgs()
    const {
  return last_begin_frame_args_;
}

void CompositorFrameSinkSupport::OnBeginFrameSourcePausedChanged(bool paused) {
  if (client_)
    client_->OnBeginFramePausedChanged(paused);
}

void CompositorFrameSinkSupport::UpdateNeedsBeginFramesInternal() {
  if (!begin_frame_source_)
    return;

  // We require a begin frame if there's a callback pending, or if the client
  // requested it, or if the client needs to get some frame timing details, or
  // if the client is waiting for a frame ack, or if there are resources to
  // return.
  needs_begin_frame_ =
      (client_needs_begin_frame_ || !frame_timing_details_.empty() ||
       !pending_surfaces_.empty() || layer_context_wants_begin_frames_ ||
       (compositor_frame_callback_ && !callback_received_begin_frame_));

  if (bundle_id_.has_value()) {
    // When bundled with other sinks, observation of BeginFrame notifications is
    // always delegated to the bundle.
    if (added_frame_observer_) {
      StopObservingBeginFrameSource();
    }
    if (auto* bundle = frame_sink_manager_->GetFrameSinkBundle(*bundle_id_)) {
      bundle->SetSinkNeedsBeginFrame(frame_sink_id_.sink_id(),
                                     needs_begin_frame_);
    }
    return;
  }

  if (needs_begin_frame_ == added_frame_observer_)
    return;

  if (needs_begin_frame_) {
    StartObservingBeginFrameSource();
  } else {
    StopObservingBeginFrameSource();
  }
}

void CompositorFrameSinkSupport::StartObservingBeginFrameSource() {
  added_frame_observer_ = true;
  begin_frame_source_->AddObserver(this);
}

void CompositorFrameSinkSupport::StopObservingBeginFrameSource() {
  added_frame_observer_ = false;
  begin_frame_source_->RemoveObserver(this);
}

const FrameSinkId& CompositorFrameSinkSupport::GetFrameSinkId() const {
  return frame_sink_id_;
}

void CompositorFrameSinkSupport::AttachCaptureClient(
    CapturableFrameSink::Client* client) {
  DCHECK(!base::Contains(capture_clients_, client));
  capture_clients_.push_back(client);
  if (client->IsVideoCaptureStarted())
    OnClientCaptureStarted();
}

void CompositorFrameSinkSupport::DetachCaptureClient(
    CapturableFrameSink::Client* client) {
  const auto it = std::ranges::find(capture_clients_, client);
  if (it != capture_clients_.end())
    capture_clients_.erase(it);
  if (client->IsVideoCaptureStarted())
    OnClientCaptureStopped();
}

void CompositorFrameSinkSupport::OnClientCaptureStarted() {
  if (number_clients_capturing_++ == 0) {
    // First client started capturing.
    frame_sink_manager_->OnCaptureStarted(frame_sink_id_);
  }
}

void CompositorFrameSinkSupport::OnClientCaptureStopped() {
  DCHECK_GT(number_clients_capturing_, 0u);
  if (--number_clients_capturing_ == 0) {
    // The last client has stopped capturing.
    frame_sink_manager_->OnCaptureStopped(frame_sink_id_);
  }
}

std::optional<CapturableFrameSink::RegionProperties>
CompositorFrameSinkSupport::GetRequestRegionProperties(
    const VideoCaptureSubTarget& sub_target) const {
  if (!last_activated_surface_id_.is_valid())
    return {};

  Surface* current_surface =
      surface_manager_->GetSurfaceForId(last_activated_surface_id_);
  DCHECK(current_surface);
  if (!current_surface->HasActiveFrame()) {
    return {};
  }

  const CompositorFrame& frame = current_surface->GetActiveFrame();
  RegionProperties out;
  out.root_render_pass_size = frame.size_in_pixels();
  if (out.root_render_pass_size.IsEmpty()) {
    return {};
  }

  // If we don't have a sub target, capture everything in the frame.
  if (IsEntireTabCapture(sub_target)) {
    out.render_pass_subrect = gfx::Rect(out.root_render_pass_size);
    return out;
  }

  // If we have a region capture crop ID, capture a subsection of the root
  // render pass.
  if (IsRegionCapture(sub_target)) {
    const auto it = current_capture_bounds_.bounds().find(
        std::get<RegionCaptureCropId>(sub_target));
    if (it != current_capture_bounds_.bounds().end() && !it->second.IsEmpty() &&
        gfx::Rect(out.root_render_pass_size).Contains(it->second)) {
      out.render_pass_subrect = it->second;
      return out;
    }

    // Nothing to capture.
    return {};
  }

  // Else, we have a subtree capture ID and should capture a subsection of a
  // child render pass.
  CHECK(IsSubtreeCapture(sub_target));
  const SubtreeCaptureId& id = std::get<SubtreeCaptureId>(sub_target);
  for (const auto& render_pass : frame.render_pass_list) {
    if (render_pass->subtree_capture_id == id) {
      out.transform_to_root = render_pass->transform_to_root_target;

      if (!render_pass->subtree_size.IsEmpty()) {
        out.render_pass_subrect = gfx::Rect(render_pass->subtree_size);
      } else {
        out.render_pass_subrect =
            IntersectInSpace(render_pass->output_rect, out.transform_to_root,
                             gfx::Rect(out.root_render_pass_size));
      }

      if (!out.render_pass_subrect.IsEmpty() &&
          render_pass->output_rect.Contains(out.render_pass_subrect)) {
        return out;
      }
    }
  }

  // No target exists and no CopyOutputRequest will be added.
  return {};
}

void CompositorFrameSinkSupport::RequestCopyOfOutput(
    PendingCopyOutputRequest pending_copy_output_request) {
  copy_output_requests_.push_back(std::move(pending_copy_output_request));
  if (last_activated_surface_id_.is_valid()) {
    BeginFrameAck ack;
    ack.has_damage = true;
    surface_manager_->SurfaceModified(
        last_activated_surface_id_, ack,
        SurfaceObserver::HandleInteraction::kNoChange);
  }
}

const CompositorFrameMetadata*
CompositorFrameSinkSupport::GetLastActivatedFrameMetadata() {
  if (!last_activated_surface_id_.is_valid())
    return nullptr;
  Surface* surface =
      surface_manager_->GetSurfaceForId(last_activated_surface_id_);
  DCHECK(surface);
  return &surface->GetActiveFrameMetadata();
}

HitTestAggregator* CompositorFrameSinkSupport::GetHitTestAggregator() {
  DCHECK(is_root_);
  return hit_test_aggregator_.get();
}

Surface* CompositorFrameSinkSupport::GetLastCreatedSurfaceForTesting() {
  return surface_manager_->GetSurfaceForId(last_created_surface_id_);
}

// static
const char* CompositorFrameSinkSupport::GetSubmitResultAsString(
    SubmitResult result) {
  switch (result) {
    case SubmitResult::ACCEPTED:
      return "Accepted";
    case SubmitResult::COPY_OUTPUT_REQUESTS_NOT_ALLOWED:
      return "CopyOutputRequests not allowed";
    case SubmitResult::SIZE_MISMATCH:
      return "CompositorFrame size doesn't match surface size";
    case SubmitResult::SURFACE_ID_DECREASED:
      return "LocalSurfaceId sequence numbers decreased";
    case SubmitResult::SURFACE_OWNED_BY_ANOTHER_CLIENT:
      return "Surface belongs to another client";
  }
  NOTREACHED();
}

bool CompositorFrameSinkSupport::ShouldSendBeginFrame(
    BeginFrameId frame_id,
    base::TimeTicks frame_time,
    base::TimeDelta vsync_interval) {
  // Don't send the same BeginFrameId to a client twice. This could otherwise
  // happen if the client removes and then immediately re-adds a
  // BeginFrameObserver before the next BeginFrame arrives. See
  // https://crbug.com/40286473.
  if (frame_id == last_begin_frame_args_.frame_id) {
    return RecordShouldSendBeginFrame("DuplicateFrameId", false);
  }

  // We should throttle OnBeginFrame() if it has been less than
  // |begin_frame_interval_| since the last one was sent because clients have
  // requested to update at such rate.
  const bool should_throttle_as_requested =
      ShouldThrottleBeginFrameAsRequested(frame_time, vsync_interval);
  // We might throttle this OnBeginFrame() if it's been less than a second
  // since the last one was sent, either because clients are unresponsive or
  // have submitted too many undrawn frames.
  const bool can_throttle_if_unresponsive_or_excessive =
      frame_time - last_frame_time_ < base::Seconds(1);

  bool should_throttle_undrawn_frames = false;
  if (last_activated_surface_id_.is_valid()) {
    Surface* surface =
        surface_manager_->GetSurfaceForId(last_activated_surface_id_);

    DCHECK(surface);
    DCHECK(surface->HasActiveFrame());
    uint64_t active_frame_index = surface->GetActiveFrameIndex();

    // Since we have an active frame, and frame indexes strictly increase
    // during the lifetime of the CompositorFrameSinkSupport, our active frame
    // index must be at least as large as our last drawn frame index.
    DCHECK_GE(active_frame_index, last_drawn_frame_index_);

    // Throttle clients that have submitted too many undrawn frames, unless the
    // active frame requests that it doesn't.
    uint64_t num_undrawn_frames = active_frame_index - last_drawn_frame_index_;
    should_throttle_undrawn_frames =
        can_throttle_if_unresponsive_or_excessive &&
        num_undrawn_frames > kUndrawnFrameLimit &&
        surface->GetActiveFrameMetadata().may_throttle_if_undrawn_frames;
  }

  bool frame_timing_details_all_failed = true;
  for (const auto& entry : frame_timing_details_) {
    if (!entry.second.presentation_feedback.failed()) {
      frame_timing_details_all_failed = false;
      break;
    }
  }

  // If there are pending timing details from the previous successfully
  // presented frame(s), then the non-throttled client needs to receive the
  // begin-frame.
  if (!frame_timing_details_.empty() && !should_throttle_as_requested &&
      (!should_throttle_undrawn_frames || !frame_timing_details_all_failed)) {
    return RecordShouldSendBeginFrame("SendFrameTiming", true);
  }

  if (!client_needs_begin_frame_ && !layer_context_wants_begin_frames_) {
    return RecordShouldSendBeginFrame("StopNotRequested", false);
  }

  // Stop sending BeginFrames to clients that are totally unresponsive.
  if (begin_frame_tracker_.ShouldStopBeginFrame()) {
    return RecordShouldSendBeginFrame("StopUnresponsiveClient", false);
  }

  // Throttle clients that are unresponsive.
  if (can_throttle_if_unresponsive_or_excessive &&
      begin_frame_tracker_.ShouldThrottleBeginFrame()) {
    return RecordShouldSendBeginFrame("ThrottleUnresponsiveClient", false);
  }

  if (!last_activated_surface_id_.is_valid()) {
    return RecordShouldSendBeginFrame("SendNoActiveSurface", true);
  }

  // We should never throttle BeginFrames if there is another client waiting
  // for this client to submit a frame.
  if (surface_manager_->HasBlockedEmbedder(frame_sink_id_)) {
    return RecordShouldSendBeginFrame("SendBlockedEmbedded", true);
  }

  if (should_throttle_as_requested) {
    ++frames_throttled_since_last_;
    return RecordShouldSendBeginFrame("ThrottleRequested", false);
  }

  if (should_throttle_undrawn_frames) {
    return RecordShouldSendBeginFrame("ThrottleUndrawnFrames", false);
  }

  // No other conditions apply so send the begin frame.
  return RecordShouldSendBeginFrame("SendDefault", true);
}

void CompositorFrameSinkSupport::CheckPendingSurfaces() {
  if (pending_surfaces_.empty())
    return;
  base::flat_set<raw_ptr<Surface, CtnExperimental>> pending_surfaces(
      pending_surfaces_);
  for (Surface* surface : pending_surfaces) {
    surface->ActivateIfDeadlinePassed();
  }
}

bool CompositorFrameSinkSupport::ShouldThrottleBeginFrameAsRequested(
    base::TimeTicks frame_time,
    base::TimeDelta vsync_interval) {
  if (!begin_frame_interval_.is_positive() || !vsync_interval.is_positive()) {
    return false;
  }

  // At this point, throttling is in place and following a simple cadence, we
  // need to know if the current frame has elapsed a full cadence interval to
  // know its time to render.
  base::TimeDelta time_since_last_frame = frame_time - last_frame_time_;
  return !HasElapsedCadenceInterval(vsync_interval, begin_frame_interval_,
                                    time_since_last_frame);
}

void CompositorFrameSinkSupport::ProcessCompositorFrameTransitionDirective(
    const CompositorFrameTransitionDirective& directive,
    Surface* surface) {
  const auto& transition_token = directive.transition_token();

  switch (directive.type()) {
    case CompositorFrameTransitionDirective::Type::kSave:
      // The save directive is used to start a new transition sequence. Ensure
      // we don't have any existing state for this transition.
      if (frame_sink_manager_->ClearSurfaceAnimationManager(transition_token))
          [[unlikely]] {
        view_transition_token_to_animation_manager_.erase(transition_token);
        return;
      }
      if (view_transition_token_to_animation_manager_.erase(transition_token))
          [[unlikely]] {
        return;
      }

      view_transition_token_to_animation_manager_[transition_token] =
          SurfaceAnimationManager::CreateWithSave(
              directive, surface,
              frame_sink_manager_->GetSharedImageInterface(),
              frame_sink_manager_->reserved_resource_id_tracker(),
              base::BindOnce(&CompositorFrameSinkSupport::
                                 OnSaveTransitionDirectiveProcessed,
                             base::Unretained(this)));
      if (surface_animation_manager_callback_) {
        std::move(surface_animation_manager_callback_).Run();
      }
      break;
    case CompositorFrameTransitionDirective::Type::kAnimateRenderer: {
      if (directive.maybe_cross_frame_sink()) {
        // We shouldn't have an existing SurfaceAnimationManager for this
        // token.
        if (view_transition_token_to_animation_manager_.erase(
                transition_token)) {
          return;
        }
        view_transition_token_to_animation_manager_[transition_token] =
            frame_sink_manager_->TakeSurfaceAnimationManager(transition_token);
      }

      auto it =
          view_transition_token_to_animation_manager_.find(transition_token);
      if (it == view_transition_token_to_animation_manager_.end()) {
        return;
      }

      // The save operation must have been completed before the renderer sends
      // an animate directive.
      auto& surface_animation_manager = it->second;
      if (!surface_animation_manager->Animate()) {
        view_transition_token_to_animation_manager_.erase(it);
        return;
      }
    } break;
    case CompositorFrameTransitionDirective::Type::kRelease:
      frame_sink_manager_->ClearSurfaceAnimationManager(
          directive.transition_token());
      view_transition_token_to_animation_manager_.erase(
          directive.transition_token());
      break;
  }
}

void CompositorFrameSinkSupport::OnSaveTransitionDirectiveProcessed(
    const CompositorFrameTransitionDirective& directive) {
  DCHECK_EQ(directive.type(), CompositorFrameTransitionDirective::Type::kSave)
      << "Only save directives need to be ack'd back to the client";

  auto it = view_transition_token_to_animation_manager_.find(
      directive.transition_token());
  if (it == view_transition_token_to_animation_manager_.end()) {
    return;
  }

  CHECK(it->second);

  if (client_) {
    client_->OnCompositorFrameTransitionDirectiveProcessed(
        directive.sequence_id());
  }

  if (directive.maybe_cross_frame_sink()) {
    frame_sink_manager_->CacheSurfaceAnimationManager(
        directive.transition_token(), std::move(it->second));
    view_transition_token_to_animation_manager_.erase(it);
  }
}

bool CompositorFrameSinkSupport::IsEvicted(
    const LocalSurfaceId& local_surface_id) const {
  return local_surface_id.embed_token() ==
             last_evicted_local_surface_id_.embed_token() &&
         local_surface_id.parent_sequence_number() <=
             last_evicted_local_surface_id_.parent_sequence_number();
}

void CompositorFrameSinkSupport::ClearAllPendingCopyOutputRequests() {
  CHECK(surface_manager_);
  for (auto& request : copy_output_requests_) {
    // If the frame sink is getting destroyed while there are still
    // outstanding `CopyOutputRequest`s to capture an associated surface,
    // transfer these requests to the corresponding `Surface`s.
    //
    // Resources reclamation: once frame sink is destroyed, the `Surface`s
    // won't be able to notify the client code (the renderer's
    // `cc::LayerTreeHostImpl`) to reclaim the resources. This is fine,
    // because the destruction of the renderer and its CC (as part of a
    // cross-RenderFrame navigation) will implicitly reclaim all the
    // resources. The `Surface` kept alive will still have a reference to
    // the underlying GPU resources. The GPU resources will finally be
    // released when the `Surface` is destroyed (in this case, after the
    // CopyOutputRequest is fulfilled).
    if (request.capture_exact_surface_id) {
      const SurfaceId target_id(frame_sink_id_, request.local_surface_id);
      auto* target_surface = surface_manager_->GetSurfaceForId(target_id);
      if (target_surface) {
        target_surface->RequestCopyOfOutput(std::move(request));

        BeginFrameAck ack;
        ack.has_damage = true;
        surface_manager_->SurfaceModified(
            target_id, ack, SurfaceObserver::HandleInteraction::kNoChange);
      } else {
        // We might not have a `Surface` if the renderer is crashed, or too busy
        // to even submit a CompositorFrame (e.g., low end Android devices). In
        // either cases we don't want to crash the GPU in production. The
        // WARNING logs will show up in "chrome://gpu".
        LOG(WARNING) << "Surface " << target_id
                     << " is destroyed while there is an outstanding "
                        "CopyOutputRequest specificc for it.";
      }
    } else {
      // TODO(crbug.com/40276723): We should probably transfer all
      // the requests to their corresponding `Surface`s or `RenderPass`es.
    }
  }
  // Upon destruction, the `PendingOutputRequest` will invoke the callback to
  // return an empty bitmap, signalling that the request is never satisfied.
  copy_output_requests_.clear();
}

void CompositorFrameSinkSupport::SetExternalReservedResourceDelegate(
    ReservedResourceDelegate* delegate) {
  external_reserved_resource_delegate_ = delegate;
}

void CompositorFrameSinkSupport::SetLayerContextWantsBeginFrames(
    bool wants_begin_frames) {
  layer_context_wants_begin_frames_ = wants_begin_frames;
  UpdateNeedsBeginFramesInternal();
}

void CompositorFrameSinkSupport::RegisterSurfaceAnimationManagerNotification(
    base::OnceCallback<void()> callback) {
  if (!view_transition_token_to_animation_manager_.empty()) {
    std::move(callback).Run();
  } else {
    CHECK(!surface_animation_manager_callback_);
    surface_animation_manager_callback_ = std::move(callback);
  }
}

void CompositorFrameSinkSupport::DestroySelf() {
  // SUBTLE: We explicitly copy `frame_sink_id_` because
  // DestroyCompositorFrameSink takes the FrameSinkId by reference and may
  // dereference it after destroying `this`.
  FrameSinkId frame_sink_id = frame_sink_id_;
  frame_sink_manager_->DestroyCompositorFrameSink(frame_sink_id,
                                                  base::DoNothing());
}

void CompositorFrameSinkSupport::ScheduleSelfDestruction() {
  base::SequencedTaskRunner::GetCurrentDefault()->PostTask(
      FROM_HERE, base::BindOnce(&CompositorFrameSinkSupport::DestroySelf,
                                weak_factory_.GetWeakPtr()));
}

void CompositorFrameSinkSupport::ForAllReservedResourceDelegates(
    base::FunctionRef<void(ReservedResourceDelegate&)> func) {
  if (external_reserved_resource_delegate_) {
    func(*external_reserved_resource_delegate_);
  }

  for (auto& it : view_transition_token_to_animation_manager_) {
    func(*it.second);
  }
}

CompositorFrameSinkSupport::PendingFrameDetails::PendingFrameDetails(
    base::TimeTicks frame_submit_timestamp,
    SurfaceManager* surface_manager)
    : frame_submit_timestamp_(frame_submit_timestamp),
      // Use the submit timestamp as the default value, so that the metrics
      // won't get skewed in case the surface never gets embedded/the surface
      // ID never gets set.
      frame_embed_timestamp_(frame_submit_timestamp),
      surface_manager_(surface_manager) {}

void CompositorFrameSinkSupport::PendingFrameDetails::
    SetOrObserveFrameEmbedTimeStamp() {
  frame_embed_timestamp_ =
      surface_manager_->GetSurfaceReferencedTimestamp(surface_id_);
  if (frame_embed_timestamp_ == base::TimeTicks()) {
    // The frame hasn't been embedded yet. Observe `OnAddedSurfaceReference()`
    // to be notified when the surface for the frame gets embedded.
    surface_manager_->AddObserver(this);
  }
}

CompositorFrameSinkSupport::PendingFrameDetails::~PendingFrameDetails() {
  surface_manager_->RemoveObserver(this);
}

void CompositorFrameSinkSupport::PendingFrameDetails::set_surface_id(
    SurfaceId surface_id) {
  CHECK(!surface_id_.is_valid());
  surface_id_ = surface_id;
  SetOrObserveFrameEmbedTimeStamp();
}

void CompositorFrameSinkSupport::PendingFrameDetails::OnAddedSurfaceReference(
    const SurfaceId& parent_id,
    const SurfaceId& child_id) {
  CHECK_EQ(frame_embed_timestamp_, base::TimeTicks());
  if (child_id != surface_id_) {
    return;
  }
  frame_embed_timestamp_ = base::TimeTicks::Now();
  surface_manager_->RemoveObserver(this);
}

void CompositorFrameSinkSupport::PendingFrameDetails::set_frame_id(
    BeginFrameId frame_id) {
  frame_id_ = frame_id;
}

}  // namespace viz