File: root_compositor_frame_sink_impl.cc

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 (1008 lines) | stat: -rw-r--r-- 38,635 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
// Copyright 2017 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/root_compositor_frame_sink_impl.h"

#include <algorithm>
#include <utility>
#include <variant>
#include <vector>

#include "base/containers/flat_set.h"
#include "base/logging.h"
#include "base/memory/ptr_util.h"
#include "base/memory/raw_ptr.h"
#include "base/metrics/histogram_functions.h"
#include "base/notimplemented.h"
#include "base/task/single_thread_task_runner.h"
#include "base/threading/platform_thread.h"
#include "base/time/time.h"
#include "base/tracing/protos/chrome_track_event.pbzero.h"
#include "build/build_config.h"
#include "components/viz/common/features.h"
#include "components/viz/common/frame_sinks/begin_frame_source.h"
#include "components/viz/service/display/display.h"
#include "components/viz/service/display/output_surface.h"
#include "components/viz/service/display_embedder/output_surface_provider.h"
#include "components/viz/service/display_embedder/vsync_parameter_listener.h"
#include "components/viz/service/frame_sinks/frame_sink_manager_impl.h"
#include "components/viz/service/hit_test/hit_test_aggregator.h"
#include "services/viz/public/mojom/compositing/layer_context.mojom.h"
#include "third_party/abseil-cpp/absl/functional/overload.h"
#include "ui/base/ozone_buildflags.h"
#include "ui/gfx/geometry/skia_conversions.h"

#if BUILDFLAG(IS_ANDROID)
#include "components/viz/service/frame_sinks/external_begin_frame_source_android.h"
#endif

#if BUILDFLAG(IS_IOS)
#include "components/viz/common/frame_sinks/external_begin_frame_source_ios.h"
#include "components/viz/service/frame_sinks/external_begin_frame_source_mojo_ios.h"
#else
#include "components/viz/service/frame_sinks/external_begin_frame_source_mojo.h"
#endif

#if BUILDFLAG(IS_MAC)
#include "base/feature_list.h"
#include "components/viz/service/frame_sinks/external_begin_frame_source_mac.h"
#endif

#if BUILDFLAG(IS_WIN)
#include "components/viz/service/frame_sinks/external_begin_frame_source_win.h"
#endif

namespace viz {

namespace {
#if BUILDFLAG(IS_ANDROID)
gfx::SurfaceControlFrameRateCompatibility IntervalTypeToCompat(
    FrameIntervalMatcher::ResultIntervalType interval_type) {
  switch (interval_type) {
    case FrameIntervalMatcher::ResultIntervalType::kExact:
      return gfx::SurfaceControlFrameRateCompatibility::kFixedSource;
    case FrameIntervalMatcher::ResultIntervalType::kAtLeast:
      return gfx::SurfaceControlFrameRateCompatibility::kAtLeast;
  }
  NOTREACHED();
}
#endif
}  // namespace

class RootCompositorFrameSinkImpl::StandaloneBeginFrameObserver
    : public BeginFrameObserverBase {
 public:
  StandaloneBeginFrameObserver(
      mojo::PendingRemote<mojom::BeginFrameObserver> observer,
      BeginFrameSource* begin_frame_source)
      : remote_observer_(std::move(observer)),
        begin_frame_source_(begin_frame_source) {
    remote_observer_.set_disconnect_handler(base::BindOnce(
        &StandaloneBeginFrameObserver::StopObserving, base::Unretained(this)));
    begin_frame_source_->AddObserver(this);
  }

  ~StandaloneBeginFrameObserver() override { StopObserving(); }

  bool OnBeginFrameDerivedImpl(const BeginFrameArgs& args) override {
    TRACE_EVENT(
        "graphics.pipeline", "Graphics.Pipeline",
        [&](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_STANDALONE_BEGIN_FRAME_MOJO_MESSAGE);
        });
    remote_observer_->OnStandaloneBeginFrame(args);
    return true;
  }

  void OnBeginFrameSourcePausedChanged(bool paused) override {}
  bool IsRoot() const override { return true; }

 private:
  void StopObserving() {
    if (!begin_frame_source_)
      return;
    begin_frame_source_->RemoveObserver(this);
    begin_frame_source_ = nullptr;
  }

  mojo::Remote<mojom::BeginFrameObserver> remote_observer_;
  raw_ptr<BeginFrameSource> begin_frame_source_;
};

// static
std::unique_ptr<RootCompositorFrameSinkImpl>
RootCompositorFrameSinkImpl::Create(
    mojom::RootCompositorFrameSinkParamsPtr params,
    FrameSinkManagerImpl* frame_sink_manager,
    OutputSurfaceProvider* output_surface_provider,
    uint32_t restart_id,
    bool run_all_compositor_stages_before_draw,
    const DebugRendererSettings* debug_settings,
    HintSessionFactory* hint_session_factory) {
  // First create an output surface.
  mojo::Remote<mojom::DisplayClient> display_client(
      std::move(params->display_client));
  auto display_controller = output_surface_provider->CreateGpuDependency(
      params->gpu_compositing, params->widget);
  auto output_surface = output_surface_provider->CreateOutputSurface(
      params->widget, params->gpu_compositing, display_client.get(),
      display_controller.get(), params->renderer_settings, debug_settings);

  // Creating output surface failed. The host can send a new request, possibly
  // with a different compositing mode.
  if (!output_surface)
    return nullptr;

  // If we need swap size notifications tell the output surface now.
  output_surface->SetNeedsSwapSizeNotifications(
      params->send_swap_size_notifications);

#if BUILDFLAG(IS_LINUX) && BUILDFLAG(IS_OZONE_X11)
  // For X11, we need notify client about swap completion after resizing, so the
  // client can use it for synchronize with X11 WM.
  output_surface->SetNeedsSwapSizeNotifications(true);
#endif  // BUILDFLAG(IS_LINUX) && BUILDFLAG(IS_OZONE_X11)

  // Create some sort of a BeginFrameSource, depending on the platform and
  // |params|.
  std::unique_ptr<ExternalBeginFrameSource> external_begin_frame_source;
  std::unique_ptr<SyntheticBeginFrameSource> synthetic_begin_frame_source;
#if !BUILDFLAG(IS_IOS)
  ExternalBeginFrameSourceMojo* external_begin_frame_source_mojo = nullptr;
#endif
  bool hw_support_for_multiple_refresh_rates = false;
#if BUILDFLAG(IS_MAC)
  bool created_external_begin_frame_source_mac = false;
#endif
#if !BUILDFLAG(IS_APPLE)
  bool wants_vsync_updates = false;
#endif

  if (params->external_begin_frame_controller) {
#if BUILDFLAG(IS_IOS)
    hw_support_for_multiple_refresh_rates = true;
    external_begin_frame_source =
        std::make_unique<ExternalBeginFrameSourceMojoIOS>(
            std::move(params->external_begin_frame_controller),
            std::move(params->external_begin_frame_controller_client),
            restart_id);
#else
    external_begin_frame_source =
        std::make_unique<ExternalBeginFrameSourceMojo>(
            frame_sink_manager,
            std::move(params->external_begin_frame_controller),
            std::move(params->external_begin_frame_controller_client),
            restart_id);
    external_begin_frame_source_mojo =
        static_cast<ExternalBeginFrameSourceMojo*>(
            external_begin_frame_source.get());
#endif
  } else {
#if BUILDFLAG(IS_ANDROID)
    hw_support_for_multiple_refresh_rates = true;
    external_begin_frame_source =
        std::make_unique<ExternalBeginFrameSourceAndroid>(
            restart_id, params->refresh_rate,
            /*requires_align_with_java=*/false);
#elif BUILDFLAG(IS_IOS)
    hw_support_for_multiple_refresh_rates = true;
    external_begin_frame_source =
        std::make_unique<ExternalBeginFrameSourceIOS>(restart_id);
#else
#if BUILDFLAG(IS_CHROMEOS)
    hw_support_for_multiple_refresh_rates =
        features::IsCrosContentAdjustedRefreshRateEnabled();
#endif
    if (params->disable_frame_rate_limit) {
      synthetic_begin_frame_source =
          std::make_unique<BackToBackBeginFrameSource>(
              std::make_unique<DelayBasedTimeSource>(
                  base::SingleThreadTaskRunner::GetCurrentDefault().get()));
    } else {
#if BUILDFLAG(IS_WIN)
      // ExternalBeginFrameSourceWin also uses the D3D11 device used by dcomp.
      if (output_surface->capabilities().dc_support_level !=
          OutputSurface::DCSupportLevel::kNone) {
        // Vsync updates are required to update the FrameIntervalDecider with
        // supported refresh rates.
        wants_vsync_updates = true;
        external_begin_frame_source =
            std::make_unique<ExternalBeginFrameSourceWin>(
                restart_id, base::SingleThreadTaskRunner::GetCurrentDefault());
      }
#elif BUILDFLAG(IS_MAC)
        external_begin_frame_source =
            std::make_unique<ExternalBeginFrameSourceMac>(
                restart_id, params->renderer_settings.display_id,
                output_surface.get());
        created_external_begin_frame_source_mac = true;
#endif
      if (!external_begin_frame_source && !synthetic_begin_frame_source) {
        auto time_source = std::make_unique<DelayBasedTimeSource>(
            base::SingleThreadTaskRunner::GetCurrentDefault().get());
        synthetic_begin_frame_source =
            std::make_unique<DelayBasedBeginFrameSource>(std::move(time_source),
                                                         restart_id);
      }
    }
#endif  // BUILDFLAG(IS_ANDROID)
  }

  BeginFrameSource* begin_frame_source = synthetic_begin_frame_source.get();
  if (external_begin_frame_source)
    begin_frame_source = external_begin_frame_source.get();
  DCHECK(begin_frame_source);

  auto task_runner = base::SingleThreadTaskRunner::GetCurrentDefault();

  const auto& capabilities = output_surface->capabilities();
  DCHECK_GT(capabilities.pending_swap_params.max_pending_swaps, 0);
  auto scheduler = std::make_unique<DisplayScheduler>(
      begin_frame_source, task_runner.get(), capabilities.pending_swap_params,
      hint_session_factory, run_all_compositor_stages_before_draw);

#if !BUILDFLAG(IS_APPLE)
  auto* output_surface_ptr = output_surface.get();
#endif

  auto overlay_processor = OverlayProcessorInterface::CreateOverlayProcessor(
      output_surface.get(), output_surface->GetSurfaceHandle(),
      output_surface->capabilities(), display_controller.get(),
      output_surface_provider->GetSharedImageManager(),
      params->renderer_settings, debug_settings);

  auto display = std::make_unique<Display>(
      output_surface_provider->GetSharedImageManager(),
      output_surface_provider->GetGpuScheduler(), params->renderer_settings,
      debug_settings, params->frame_sink_id, std::move(display_controller),
      std::move(output_surface), std::move(overlay_processor),
      std::move(scheduler), std::move(task_runner));

#if !BUILDFLAG(IS_IOS)
  if (external_begin_frame_source_mojo) {
    external_begin_frame_source_mojo->SetDisplay(display.get());
  }
#endif

  // base::WrapUnique instead of std::make_unique because the ctor is private.
  auto impl = base::WrapUnique(new RootCompositorFrameSinkImpl(
      frame_sink_manager, params->frame_sink_id,
      std::move(params->compositor_frame_sink),
      std::move(params->compositor_frame_sink_client),
      std::move(params->display_private), std::move(display_client),
      std::move(synthetic_begin_frame_source),
      std::move(external_begin_frame_source), std::move(display),
      hw_support_for_multiple_refresh_rates));

  // Set up the callback for updating VSyncParameters.
#if !BUILDFLAG(IS_APPLE)
  // On Mac vsync parameter updates does not come from OutputSurface.
  if (wants_vsync_updates || impl->synthetic_begin_frame_source_) {
    // |impl| owns and outlives display, and display owns the output surface so
    // unretained is safe.
    output_surface_ptr->SetUpdateVSyncParametersCallback(base::BindRepeating(
        &RootCompositorFrameSinkImpl::SetDisplayVSyncParameters,
        base::Unretained(impl.get())));
  }
#elif BUILDFLAG(IS_MAC)
  if (impl->external_begin_frame_source_) {
    impl->external_begin_frame_source()->SetUpdateVSyncParametersCallback(
        base::BindRepeating(
            &RootCompositorFrameSinkImpl::SetDisplayVSyncParameters,
            base::Unretained(impl.get())));

    if (created_external_begin_frame_source_mac) {
      static_cast<ExternalBeginFrameSourceMac*>(
          impl->external_begin_frame_source())
          ->SetMultipleHWRefreshRatesCallback(base::BindRepeating(
              &RootCompositorFrameSinkImpl::SetHwSupportForMultipleRefreshRates,
              base::Unretained(impl.get())));
    }
  } else if (impl->synthetic_begin_frame_source_) {
    impl->synthetic_begin_frame_source_->SetUpdateVSyncParametersCallback(
        base::BindRepeating(
            &RootCompositorFrameSinkImpl::SetDisplayVSyncParameters,
            base::Unretained(impl.get())));
  }
#endif

  return impl;
}

RootCompositorFrameSinkImpl::~RootCompositorFrameSinkImpl() {
  support_->frame_sink_manager()->UnregisterBeginFrameSource(
      begin_frame_source());
}

void RootCompositorFrameSinkImpl::DidEvictSurface(const SurfaceId& surface_id) {
  const SurfaceId& current_surface_id = display_->CurrentSurfaceId();
  if (!current_surface_id.is_valid()) {
    return;
  }
  DCHECK_EQ(surface_id.frame_sink_id(), current_surface_id.frame_sink_id());

  // This matches CompositorFrameSinkSupport's eviction logic, which will
  // evict `surface_id` or matching but older ones. Avoid overwriting the
  // contents of `current_surface_id` if it's newer here by doing the same
  // check.
  if (surface_id.local_surface_id().parent_sequence_number() >=
      current_surface_id.local_surface_id().parent_sequence_number()) {
    display_->InvalidateCurrentSurfaceId();
  }
}

const SurfaceId& RootCompositorFrameSinkImpl::CurrentSurfaceId() const {
  return display_->CurrentSurfaceId();
}

void RootCompositorFrameSinkImpl::SetDisplayVisible(bool visible) {
  display_->SetVisible(visible);
}

#if BUILDFLAG(IS_WIN)
void RootCompositorFrameSinkImpl::DisableSwapUntilResize(
    DisableSwapUntilResizeCallback callback) {
  display_->DisableSwapUntilResize(std::move(callback));
}
#endif

void RootCompositorFrameSinkImpl::Resize(const gfx::Size& size) {
  if (!display_->resize_based_on_root_surface())
    display_->Resize(size);
}

void RootCompositorFrameSinkImpl::SetDisplayColorMatrix(
    const gfx::Transform& color_matrix) {
  display_->SetColorMatrix(gfx::TransformToSkM44(color_matrix));
}

void RootCompositorFrameSinkImpl::SetDisplayColorSpaces(
    const gfx::DisplayColorSpaces& display_color_spaces) {
  display_->SetDisplayColorSpaces(display_color_spaces);
}

#if BUILDFLAG(IS_MAC)
void RootCompositorFrameSinkImpl::SetVSyncDisplayID(int64_t display_id) {
  begin_frame_source()->SetVSyncDisplayID(display_id);
}
#endif

void RootCompositorFrameSinkImpl::SetOutputIsSecure(bool secure) {
  display_->SetOutputIsSecure(secure);
}

void RootCompositorFrameSinkImpl::SetDisplayVSyncParameters(
    base::TimeTicks timebase,
    base::TimeDelta interval) {
  // If |use_preferred_interval_| is true, we should decide whether
  // to update the |supported_intervals_| and timebase here.
  // Otherwise, just update the display parameters (timebase & interval)
  if (use_preferred_interval_) {
    if (display_frame_interval_ != interval) {
      display_frame_interval_ = interval;
      UpdateFrameIntervalDeciderSettings();
    }

    // If there is a meaningful |preferred_frame_interval_|, firstly
    // determine the delta of next tick time using the current timebase
    // and incoming timebase.
    if (!preferred_frame_interval_.is_zero()) {
      auto time = base::TimeTicks();
      base::TimeDelta timebase_delta =
          (time.SnappedToNextTick(timebase, display_frame_interval_) -
           time.SnappedToNextTick(display_frame_timebase_,
                                  display_frame_interval_))
              .magnitude();
      timebase_delta %= display_frame_interval_;
      timebase_delta =
          std::min(timebase_delta, display_frame_interval_ - timebase_delta);

      // If delta is more than |kMaxTimebaseDelta| of the display interval,
      // we update the timebase.
      constexpr float kMaxTimebaseDelta = 0.05;
      if (timebase_delta > display_frame_interval_ * kMaxTimebaseDelta)
        display_frame_timebase_ = timebase;
    } else {
      // |display_frame_timebase_| should be still updated as normal in
      // preferred interval mode without a meaningful
      // |preferred_frame_interval_|
      display_frame_timebase_ = timebase;
    }
  } else {
    display_frame_timebase_ = timebase;
    display_frame_interval_ = interval;
  }

  UpdateVSyncParameters();
}

base::flat_set<base::TimeDelta>
RootCompositorFrameSinkImpl::GetSupportedFrameIntervals() {
  if (!exact_supported_refresh_rates_.empty()) {
    base::flat_set<base::TimeDelta> supported_frame_intervals;
    for (auto& [supported_interval, rate] : exact_supported_refresh_rates_) {
      supported_frame_intervals.insert(supported_interval);
    }
    return supported_frame_intervals;
  }
  if (external_begin_frame_source_) {
    return external_begin_frame_source_->GetSupportedFrameIntervals(
        display_frame_interval_);
  }

  return {display_frame_interval_, display_frame_interval_ * 2};
}

void RootCompositorFrameSinkImpl::UpdateVSyncParameters() {
  base::TimeTicks timebase = display_frame_timebase_;

  // Overwrite the interval with a meaningful one here if
  // |use_preferred_interval_|
  base::TimeDelta interval =
      use_preferred_interval_ && !preferred_frame_interval_.is_zero()
          ? preferred_frame_interval_
          : display_frame_interval_;

  if (synthetic_begin_frame_source_) {
    synthetic_begin_frame_source_->OnUpdateVSyncParameters(timebase, interval);
    if (vsync_listener_)
      vsync_listener_->OnVSyncParametersUpdated(timebase, interval);
  }
  if (external_begin_frame_source_)
    external_begin_frame_source_->SetPreferredInterval(interval);
}

void RootCompositorFrameSinkImpl::ForceImmediateDrawAndSwapIfPossible() {
  display_->ForceImmediateDrawAndSwapIfPossible();
}

#if BUILDFLAG(IS_ANDROID)
void RootCompositorFrameSinkImpl::UpdateRefreshRate(float refresh_rate) {
  if (external_begin_frame_source_)
    external_begin_frame_source_->UpdateRefreshRate(refresh_rate);
}

void RootCompositorFrameSinkImpl::SetAdaptiveRefreshRateInfo(
    bool has_support,
    float suggested_high,
    float device_scale_factor) {
  supports_adaptive_refresh_rate_ =
      has_support && base::FeatureList::IsEnabled(
                         features::kUseFrameIntervalDeciderAdaptiveFrameRate);
  suggested_frame_interval_high_ = base::Hertz(suggested_high);
  device_scale_factor_ = device_scale_factor;
  UpdateFrameIntervalDeciderSettings();
}

void RootCompositorFrameSinkImpl::PreserveChildSurfaceControls() {
  display_->PreserveChildSurfaceControls();
}

void RootCompositorFrameSinkImpl::SetSwapCompletionCallbackEnabled(
    bool enable) {
  enable_swap_completion_callback_ = enable;
}
#endif  // BUILDFLAG(IS_ANDROID)

#if BUILDFLAG(IS_ANDROID) || BUILDFLAG(IS_CHROMEOS)
void RootCompositorFrameSinkImpl::SetSupportedRefreshRates(
    const std::vector<float>& supported_refresh_rates) {
#if BUILDFLAG(IS_CHROMEOS)
  CHECK_NE(use_preferred_interval_,
           features::IsCrosContentAdjustedRefreshRateEnabled());
  if (use_preferred_interval_) {
    return;
  }
#endif  // BUILDFLAG(IS_CHROMEOS)

  exact_supported_refresh_rates_.clear();
  for (float rate : supported_refresh_rates) {
    const base::TimeDelta interval = base::Hertz(rate);
    exact_supported_refresh_rates_[interval] = rate;
  }

  UpdateFrameIntervalDeciderSettings();
}
#endif  // BUILDFLAG(IS_ANDROID) || BUILDFLAG(IS_CHROMEOS)

void RootCompositorFrameSinkImpl::AddVSyncParameterObserver(
    mojo::PendingRemote<mojom::VSyncParameterObserver> observer) {
  vsync_listener_ =
      std::make_unique<VSyncParameterListener>(std::move(observer));
}

void RootCompositorFrameSinkImpl::SetDelegatedInkPointRenderer(
    mojo::PendingReceiver<gfx::mojom::DelegatedInkPointRenderer> receiver) {
  display_->InitDelegatedInkPointRendererReceiver(std::move(receiver));
}

void RootCompositorFrameSinkImpl::SetStandaloneBeginFrameObserver(
    mojo::PendingRemote<mojom::BeginFrameObserver> observer) {
  standalone_begin_frame_observer_ =
      std::make_unique<StandaloneBeginFrameObserver>(std::move(observer),
                                                     begin_frame_source());
}

void RootCompositorFrameSinkImpl::SetNeedsBeginFrame(bool needs_begin_frame) {
  support_->SetNeedsBeginFrame(needs_begin_frame);
}

void RootCompositorFrameSinkImpl::SetWantsAnimateOnlyBeginFrames() {
  support_->SetWantsAnimateOnlyBeginFrames();
}

void RootCompositorFrameSinkImpl::SetAutoNeedsBeginFrame() {
  support_->SetAutoNeedsBeginFrame();
}

void RootCompositorFrameSinkImpl::SubmitCompositorFrame(
    const LocalSurfaceId& local_surface_id,
    CompositorFrame frame,
    std::optional<HitTestRegionList> hit_test_region_list,
    uint64_t submit_time) {
  if (support_->last_activated_local_surface_id() != local_surface_id &&
      !support_->IsEvicted(local_surface_id)) {
    display_->SetLocalSurfaceId(local_surface_id, frame.device_scale_factor());
    // Resize the |display_| to the root compositor frame |output_rect| so that
    // we won't show root surface gutters.
    if (display_->resize_based_on_root_surface())
      display_->Resize(frame.render_pass_list.back()->output_rect.size());
  }

  const auto result = support_->MaybeSubmitCompositorFrame(
      local_surface_id, std::move(frame), std::move(hit_test_region_list),
      submit_time, SubmitCompositorFrameSyncCallback());
  if (result == SubmitResult::ACCEPTED)
    return;

  const char* reason =
      CompositorFrameSinkSupport::GetSubmitResultAsString(result);
  DLOG(ERROR) << "SubmitCompositorFrame failed for " << local_surface_id
              << " because " << reason;
  compositor_frame_sink_receiver_.ResetWithReason(static_cast<uint32_t>(result),
                                                  reason);
}

void RootCompositorFrameSinkImpl::SubmitCompositorFrameSync(
    const LocalSurfaceId& local_surface_id,
    CompositorFrame frame,
    std::optional<HitTestRegionList> hit_test_region_list,
    uint64_t submit_time,
    SubmitCompositorFrameSyncCallback callback) {
  NOTIMPLEMENTED();
}

void RootCompositorFrameSinkImpl::NotifyNewLocalSurfaceIdExpectedWhilePaused() {
  support_->NotifyNewLocalSurfaceIdExpectedWhilePaused();
}

void RootCompositorFrameSinkImpl::DidNotProduceFrame(
    const BeginFrameAck& begin_frame_ack) {
  support_->DidNotProduceFrame(begin_frame_ack);
}

void RootCompositorFrameSinkImpl::BindLayerContext(
    mojom::PendingLayerContextPtr context,
    bool draw_mode_is_gpu) {
  support_->BindLayerContext(*context, draw_mode_is_gpu);
}

#if BUILDFLAG(IS_ANDROID)
void RootCompositorFrameSinkImpl::SetThreads(
    const std::vector<Thread>& threads) {
  support_->SetThreads(/*from_untrusted_client=*/false, threads);
}
#endif

RootCompositorFrameSinkImpl::RootCompositorFrameSinkImpl(
    FrameSinkManagerImpl* frame_sink_manager,
    const FrameSinkId& frame_sink_id,
    mojo::PendingAssociatedReceiver<mojom::CompositorFrameSink>
        frame_sink_receiver,
    mojo::PendingRemote<mojom::CompositorFrameSinkClient> frame_sink_client,
    mojo::PendingAssociatedReceiver<mojom::DisplayPrivate> display_receiver,
    mojo::Remote<mojom::DisplayClient> display_client,
    std::unique_ptr<SyntheticBeginFrameSource> synthetic_begin_frame_source,
    std::unique_ptr<ExternalBeginFrameSource> external_begin_frame_source,
    std::unique_ptr<Display> display,
    bool hw_support_for_multiple_refresh_rates)
    : compositor_frame_sink_client_(std::move(frame_sink_client)),
      compositor_frame_sink_receiver_(this, std::move(frame_sink_receiver)),
      display_client_(std::move(display_client)),
      display_private_receiver_(this, std::move(display_receiver)),
      support_(std::make_unique<CompositorFrameSinkSupport>(
          compositor_frame_sink_client_.get(),
          frame_sink_manager,
          frame_sink_id,
          /*is_root=*/true)),
      synthetic_begin_frame_source_(std::move(synthetic_begin_frame_source)),
      external_begin_frame_source_(std::move(external_begin_frame_source)),
      display_(std::move(display)) {
  DCHECK(display_);
  DCHECK(begin_frame_source());
  frame_sink_manager->RegisterBeginFrameSource(begin_frame_source(),
                                               support_->frame_sink_id());
  display_->Initialize(this, support_->frame_sink_manager()->surface_manager());
  support_->SetUpHitTest(display_.get());
#if BUILDFLAG(IS_IOS)
  // iOS supports preferred refresh rate interval set as a hint how often a
  // client wants to refresh the content. It works two ways - a client setting a
  // preferred refresh rate and the system throttling the refresh rate in case
  // of battery saving or any other events.
  DCHECK(hw_support_for_multiple_refresh_rates);
  use_preferred_interval_ = true;
#else
  if (!hw_support_for_multiple_refresh_rates) {
    use_preferred_interval_ = true;
  }
#endif

  if (external_begin_frame_source_) {
    // Start with the maximum supported refresh rate by setting
    // |display_frame_interval_| to the minimum frame interval.
    display_frame_interval_ =
        external_begin_frame_source_->GetMinimumFrameInterval();
  }

#if BUILDFLAG(IS_ANDROID)
  interval_decider_use_fixed_intervals_ =
      !display_->OutputSurfaceSupportsSetFrameRate();
#elif BUILDFLAG(IS_IOS)
  interval_decider_use_fixed_intervals_ = false;
#endif
  UpdateFrameIntervalDeciderSettings();
}

void RootCompositorFrameSinkImpl::UpdateFrameIntervalDeciderSettings() {
  FrameIntervalDecider* decider = display_->frame_interval_decider();

  // Note that matcher order defines precedence.
  std::vector<std::unique_ptr<FrameIntervalMatcher>> matchers;

#if BUILDFLAG(IS_ANDROID)
  if (supports_adaptive_refresh_rate_) {
    matchers.push_back(std::make_unique<UserInputBoostMatcher>());
    matchers.push_back(
        std::make_unique<SlowScrollThrottleMatcher>(device_scale_factor_));
  } else {
    matchers.push_back(std::make_unique<InputBoostMatcher>());
  }
#else
  matchers.push_back(std::make_unique<InputBoostMatcher>());
#endif

#if BUILDFLAG(IS_ANDROID)
  matchers.push_back(std::make_unique<OnlyVideoMatcher>());
  if (supports_adaptive_refresh_rate_) {
    matchers.push_back(std::make_unique<OnlyAnimatingImageMatcher>());
    matchers.push_back(
        std::make_unique<OnlyScrollBarFadeOutAnimationMatcher>());
  }
#elif BUILDFLAG(IS_IOS)
  matchers.push_back(std::make_unique<OnlyVideoMatcher>());
#else
  if (base::FeatureList::IsEnabled(features::kSingleVideoFrameRateThrottling)) {
    matchers.push_back(std::make_unique<OnlyVideoMatcher>());
  }

  // Only desktop platforms get VideoConferenceMatcher.
  matchers.push_back(std::make_unique<VideoConferenceMatcher>());
#endif

  FrameIntervalDecider::Settings settings = decider->settings();
  if (interval_decider_use_fixed_intervals_) {
    FrameIntervalMatcher::FixedIntervalSettings fixed_interval_settings;
    fixed_interval_settings.supported_intervals = GetSupportedFrameIntervals();
#if BUILDFLAG(IS_ANDROID)
    // Android relies on always returning an element from
    // `exact_supported_refresh_rates_`.
    fixed_interval_settings.default_interval =
        *fixed_interval_settings.supported_intervals.begin();
#else
    // Other platforms uses the special unspecified value for default.
    fixed_interval_settings.default_interval = base::TimeDelta();
#endif
    settings.interval_settings = fixed_interval_settings;
  } else if (max_vsync_interval_.has_value()) {
    FrameIntervalMatcher::ContinuousRangeSettings continuous_range_settings;
    continuous_range_settings.min_interval =
        *GetSupportedFrameIntervals().begin();
    continuous_range_settings.max_interval = max_vsync_interval_.value();
    continuous_range_settings.default_interval = base::TimeDelta();
    settings.interval_settings = continuous_range_settings;
  } else {
    settings.interval_settings = {};
  }

  // Unretained is safe since this owns Display which owns FrameIntervalDecider.
  settings.result_callback = base::BindRepeating(
      &RootCompositorFrameSinkImpl::FrameIntervalDeciderResultCallback,
      base::Unretained(this));
  decider->UpdateSettings(std::move(settings), std::move(matchers));
}

void RootCompositorFrameSinkImpl::FrameIntervalDeciderResultCallback(
    FrameIntervalDecider::Result result,
    FrameIntervalMatcherType matcher_type) {
#if BUILDFLAG(IS_ANDROID)
  base::TimeDelta interval;
  std::pair<base::TimeDelta, gfx::SurfaceControlFrameRateCompatibility>
      interval_and_compat = std::visit(
          absl::Overload(
              [this](FrameIntervalDecider::FrameIntervalClass
                         frame_interval_class) {
                switch (frame_interval_class) {
                  case FrameIntervalDecider::FrameIntervalClass::kBoost:
                    if (supports_adaptive_refresh_rate_) {
                      return std::pair(
                          suggested_frame_interval_high_,
                          gfx::SurfaceControlFrameRateCompatibility::kAtLeast);
                    }
                    return std::pair(base::Milliseconds(0),
                                     gfx::SurfaceControlFrameRateCompatibility::
                                         kFixedSource);
                  case FrameIntervalDecider::FrameIntervalClass::kDefault:
                    // 0 is a special value on Android for no preference.
                    return std::pair(base::Milliseconds(0),
                                     gfx::SurfaceControlFrameRateCompatibility::
                                         kFixedSource);
                }
              },
              [](FrameIntervalDecider::ResultInterval interval) {
                return std::pair(interval.interval,
                                 IntervalTypeToCompat(interval.type));
              }),
          result);
  interval = interval_and_compat.first;
  gfx::SurfaceControlFrameRateCompatibility compat = interval_and_compat.second;

  if (decided_display_interval_ == interval &&
      decided_display_frame_rate_compat_ == compat) {
    return;
  }
  decided_display_interval_ = interval;
  decided_display_frame_rate_compat_ = compat;
#else
  base::TimeDelta interval = std::visit(
      absl::Overload(
          [](FrameIntervalDecider::FrameIntervalClass frame_interval_class) {
            switch (frame_interval_class) {
              case FrameIntervalDecider::FrameIntervalClass::kBoost:
                return base::TimeDelta();
              case FrameIntervalDecider::FrameIntervalClass::kDefault:
                return base::TimeDelta();
            }
          },
          [](FrameIntervalDecider::ResultInterval interval) {
            return interval.interval;
          }),
      result);

  if (decided_display_interval_ == interval) {
    return;
  }
  decided_display_interval_ = interval;
#endif

#if BUILDFLAG(IS_ANDROID)
  if (display_->OutputSurfaceSupportsSetFrameRate()) {
    float interval_s = interval.InSecondsF();
    float frame_rate = interval_s == 0 ? 0 : (1 / interval_s);
    display_->SetFrameIntervalOnOutputSurface(
        {.frame_rate = frame_rate, .compatibility = compat});
    return;
  }
#endif
  SetPreferredFrameInterval(interval);
}

void RootCompositorFrameSinkImpl::DisplayOutputSurfaceLost() {
  // |display_| has encountered an error and needs to be recreated. Reset
  // message pipes from the client, the client will see the connection error and
  // recreate the CompositorFrameSink+Display.
  compositor_frame_sink_receiver_.reset();
  display_private_receiver_.reset();
}

void RootCompositorFrameSinkImpl::DisplayWillDrawAndSwap(
    bool will_draw_and_swap,
    AggregatedRenderPassList* render_passes) {
  DCHECK(support_->GetHitTestAggregator());
  support_->GetHitTestAggregator()->Aggregate(display_->CurrentSurfaceId());

  if (external_begin_frame_source_ &&
      external_begin_frame_source_->last_begin_frame_args().IsValid() &&
      base::ShouldRecordSubsampledMetric(0.001)) {
    const BeginFrameArgs& begin_frame_args =
        external_begin_frame_source_->last_begin_frame_args();
    constexpr base::TimeDelta kEpsilonTimeDelta = base::Milliseconds(0.5);
    if (decided_display_interval_.is_zero()) {
      base::UmaHistogramCustomTimes(
          "Viz.FrameIntervalDecider.ActualIntervalDefault",
          begin_frame_args.interval, base::Milliseconds(0),
          base::Milliseconds(500), 50);
    }
    if ((decided_display_interval_ - base::Hertz(30)).magnitude() <
        kEpsilonTimeDelta) {
      base::UmaHistogramCustomTimes(
          "Viz.FrameIntervalDecider.ActualIntervalFor30hz",
          begin_frame_args.interval, base::Milliseconds(0),
          base::Milliseconds(500), 50);
    }
    if ((decided_display_interval_ - base::Hertz(25)).magnitude() <
        kEpsilonTimeDelta) {
      base::UmaHistogramCustomTimes(
          "Viz.FrameIntervalDecider.ActualIntervalFor25hz",
          begin_frame_args.interval, base::Milliseconds(0),
          base::Milliseconds(500), 50);
    }
    if ((decided_display_interval_ - base::Hertz(24)).magnitude() <
        kEpsilonTimeDelta) {
      base::UmaHistogramCustomTimes(
          "Viz.FrameIntervalDecider.ActualIntervalFor24hz",
          begin_frame_args.interval, base::Milliseconds(0),
          base::Milliseconds(500), 50);
    }
    if ((decided_display_interval_ - base::Hertz(20)).magnitude() <
        kEpsilonTimeDelta) {
      base::UmaHistogramCustomTimes(
          "Viz.FrameIntervalDecider.ActualIntervalFor20hz",
          begin_frame_args.interval, base::Milliseconds(0),
          base::Milliseconds(500), 50);
    }
  }
}

#if BUILDFLAG(IS_ANDROID)
base::ScopedClosureRunner RootCompositorFrameSinkImpl::GetCacheBackBufferCb() {
  return display_->GetCacheBackBufferCb();
}
#endif

void RootCompositorFrameSinkImpl::SetHwSupportForMultipleRefreshRates(
    bool support) {
  interval_decider_use_fixed_intervals_ = !support;
  UpdateFrameIntervalDeciderSettings();
}

void RootCompositorFrameSinkImpl::StartOverdrawTracking(
    int interval_length_in_seconds) {
  display_->StartTrackingOverdraw(interval_length_in_seconds);
}

OverdrawTracker::OverdrawTimeSeries
RootCompositorFrameSinkImpl::StopOverdrawTracking() {
  return display_->StopTrackingOverdraw();
}

void RootCompositorFrameSinkImpl::DisplayDidReceiveCALayerParams(
    const gfx::CALayerParams& ca_layer_params) {
#if BUILDFLAG(IS_APPLE)
  // If |ca_layer_params| should have content only when there exists a client
  // to send it to.
  DCHECK(ca_layer_params.is_empty || display_client_);
  if (last_ca_layer_params_ == ca_layer_params &&
      base::TimeTicks::Now() < next_forced_ca_layer_params_update_time_) {
    return;
  }
  last_ca_layer_params_ = ca_layer_params;
  // OnDisplayReceivedCALayerParams() is ultimately responsible for triggering
  // updates to vsync. VSync may change dynamically. To ensure the value is
  // updated correctly, OnDisplayReceivedCALayerParams() is periodically called,
  // even if the params haven't changed. The value here matches that of
  // DisplayLinkMac, which is responsible for querying for vsync updates.
  next_forced_ca_layer_params_update_time_ =
      base::TimeTicks::Now() + base::Seconds(10);
  if (display_client_)
    display_client_->OnDisplayReceivedCALayerParams(ca_layer_params);
#else
  NOTREACHED();
#endif
}

void RootCompositorFrameSinkImpl::DisplayDidCompleteSwapWithSize(
    const gfx::Size& pixel_size) {
#if BUILDFLAG(IS_ANDROID)
  if (display_client_ && enable_swap_completion_callback_) {
    display_client_->DidCompleteSwapWithSize(pixel_size);
  }
#elif BUILDFLAG(IS_LINUX) && BUILDFLAG(IS_OZONE_X11)
  if (display_client_ && pixel_size != last_swap_pixel_size_) {
    last_swap_pixel_size_ = pixel_size;
    display_client_->DidCompleteSwapWithNewSize(last_swap_pixel_size_);
  }
#else  // !BUILDFLAG(IS_ANDROID) && !(BUILDFLAG(IS_LINUX) &&
       // BUILDFLAG(IS_OZONE_X11))
  NOTREACHED();
#endif
}

void RootCompositorFrameSinkImpl::DisplayAddChildWindowToBrowser(
    gpu::SurfaceHandle child_window) {
#if BUILDFLAG(IS_WIN)
  if (display_client_) {
    display_client_->AddChildWindowToBrowser(child_window);
  }
#else
  NOTREACHED();
#endif
}

void RootCompositorFrameSinkImpl::SetWideColorEnabled(bool enabled) {
#if BUILDFLAG(IS_ANDROID)
  if (display_client_)
    display_client_->SetWideColorEnabled(enabled);
#endif
}

void RootCompositorFrameSinkImpl::SetPreferredFrameInterval(
    base::TimeDelta interval) {
#if BUILDFLAG(IS_CHROMEOS)
  CHECK_NE(use_preferred_interval_,
           features::IsCrosContentAdjustedRefreshRateEnabled());
  if (use_preferred_interval_) {
    preferred_frame_interval_ = interval;
    UpdateVSyncParameters();
    return;
  }
#endif  // BUILDFLAG(IS_CHROMEOS))

#if BUILDFLAG(IS_ANDROID) || BUILDFLAG(IS_CHROMEOS)
  if (display_client_) {
    float refresh_rate;
    if (interval.is_zero()) {
      refresh_rate = 0;
    } else {
      auto it = exact_supported_refresh_rates_.find(interval);
      if (it != exact_supported_refresh_rates_.end()) {
        refresh_rate = it->second;
      } else {
        refresh_rate = 1 / interval.InSecondsF();
        LOG_IF(WARNING, interval_decider_use_fixed_intervals_)
            << "Requested unsupported preferred frame interval " << interval
            << " (=" << refresh_rate << "Hz)";
      }
    }
    display_client_->SetPreferredRefreshRate(refresh_rate);
  }
#else
  preferred_frame_interval_ = interval;
  UpdateVSyncParameters();
#endif  // BUILDFLAG(IS_ANDROID) || BUILDFLAG(IS_CHROMEOS)
}

void RootCompositorFrameSinkImpl::DisplayDidDrawAndSwap() {}

BeginFrameSource* RootCompositorFrameSinkImpl::begin_frame_source() {
  if (external_begin_frame_source_) {
    return external_begin_frame_source_.get();
  }
  return synthetic_begin_frame_source_.get();
}

void RootCompositorFrameSinkImpl::SetMaxVSyncAndVrr(
    std::optional<base::TimeDelta> max_vsync_interval,
    display::VariableRefreshRateState vrr_state) {
  max_vsync_interval_ = max_vsync_interval;

  if (synthetic_begin_frame_source_) {
    synthetic_begin_frame_source_->SetMaxVrrInterval(
        vrr_state == display::VariableRefreshRateState::kVrrEnabled
            ? max_vsync_interval
            : std::nullopt);
  }

#if BUILDFLAG(IS_CHROMEOS)
  if (!use_preferred_interval_) {
    interval_decider_use_fixed_intervals_ = !max_vsync_interval.has_value();
  }
#endif  // BUILDFLAG(IS_CHROMEOS)
  UpdateFrameIntervalDeciderSettings();
}

}  // namespace viz