#include "ui/compositor/compositor.h"
#include <stddef.h>
#include <algorithm>
#include <memory>
#include <string_view>
#include <utility>
#include <vector>
#include "base/command_line.h"
#include "base/containers/contains.h"
#include "base/feature_list.h"
#include "base/functional/bind.h"
#include "base/metrics/histogram_macros.h"
#include "base/not_fatal_until.h"
#include "base/observer_list.h"
#include "base/power_monitor/power_monitor.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/string_split.h"
#include "base/strings/string_util.h"
#include "base/system/sys_info.h"
#include "base/task/single_thread_task_runner.h"
#include "base/trace_event/trace_event.h"
#include "build/build_config.h"
#include "build/chromeos_buildflags.h"
#include "cc/animation/animation_host.h"
#include "cc/animation/animation_id_provider.h"
#include "cc/animation/animation_timeline.h"
#include "cc/base/features.h"
#include "cc/base/switches.h"
#include "cc/input/input_handler.h"
#include "cc/layers/layer.h"
#include "cc/metrics/begin_main_frame_metrics.h"
#include "cc/metrics/custom_metrics_recorder.h"
#include "cc/metrics/frame_sequence_tracker.h"
#include "cc/metrics/web_vital_metrics.h"
#include "cc/trees/layer_tree_host.h"
#include "cc/trees/layer_tree_settings.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/gpu/context_provider.h"
#include "components/viz/common/switches.h"
#include "components/viz/host/host_frame_sink_manager.h"
#include "components/viz/host/renderer_settings_creation.h"
#include "services/viz/privileged/mojom/compositing/display_private.mojom.h"
#include "services/viz/privileged/mojom/compositing/external_begin_frame_controller.mojom.h"
#include "services/viz/privileged/mojom/compositing/vsync_parameter_observer.mojom.h"
#include "third_party/skia/include/core/SkBitmap.h"
#include "ui/base/ozone_buildflags.h"
#include "ui/base/ui_base_features.h"
#include "ui/base/ui_base_switches.h"
#include "ui/compositor/compositor_observer.h"
#include "ui/compositor/compositor_switches.h"
#include "ui/compositor/layer.h"
#include "ui/compositor/layer_animator_collection.h"
#include "ui/compositor/overscroll/scroll_input_handler.h"
#include "ui/compositor/scoped_animation_duration_scale_mode.h"
#include "ui/display/display_switches.h"
#include "ui/gfx/geometry/skia_conversions.h"
#include "ui/gfx/icc_profile.h"
#include "ui/gfx/presentation_feedback.h"
#include "ui/gfx/switches.h"
#include "ui/gl/gl_switches.h"
#if BUILDFLAG(IS_WIN)
#include "mojo/public/cpp/bindings/sync_call_restrictions.h"
#endif
namespace ui {
struct PendingBeginFrameArgs { … };
Compositor::Compositor(const viz::FrameSinkId& frame_sink_id,
ui::ContextFactory* context_factory,
scoped_refptr<base::SingleThreadTaskRunner> task_runner,
bool enable_pixel_canvas,
bool use_external_begin_frame_control,
bool force_software_compositor,
bool enable_compositing_based_throttling,
size_t memory_limit_when_visible_mb)
: … { … }
Compositor::~Compositor() { … }
void Compositor::AddChildFrameSink(const viz::FrameSinkId& frame_sink_id) { … }
void Compositor::RemoveChildFrameSink(const viz::FrameSinkId& frame_sink_id) { … }
void Compositor::SetLayerTreeFrameSink(
std::unique_ptr<cc::LayerTreeFrameSink> layer_tree_frame_sink,
mojo::AssociatedRemote<viz::mojom::DisplayPrivate> display_private) { … }
void Compositor::SetExternalBeginFrameController(
mojo::AssociatedRemote<viz::mojom::ExternalBeginFrameController>
external_begin_frame_controller) { … }
void Compositor::OnChildResizing() { … }
void Compositor::ScheduleDraw() { … }
void Compositor::SetRootLayer(Layer* root_layer) { … }
void Compositor::DisableAnimations() { … }
void Compositor::EnableAnimations() { … }
cc::AnimationTimeline* Compositor::GetAnimationTimeline() const { … }
void Compositor::SetDisplayColorMatrix(const SkM44& matrix) { … }
void Compositor::ScheduleFullRedraw() { … }
void Compositor::ScheduleRedrawRect(const gfx::Rect& damage_rect) { … }
#if BUILDFLAG(IS_WIN)
void Compositor::SetShouldDisableSwapUntilResize(bool should) {
should_disable_swap_until_resize_ = should;
}
void Compositor::DisableSwapUntilResize() {
if (should_disable_swap_until_resize_ && display_private_) {
TRACE_EVENT0("viz", "Blocked UI for DisableSwapUntilResize");
mojo::SyncCallRestrictions::ScopedAllowSyncCall scoped_allow_sync_call;
display_private_->DisableSwapUntilResize();
disabled_swap_until_resize_ = true;
}
}
void Compositor::ReenableSwap() {
if (should_disable_swap_until_resize_ && display_private_)
display_private_->Resize(size_);
}
#endif
void Compositor::SetScaleAndSize(float scale,
const gfx::Size& size_in_pixel,
const viz::LocalSurfaceId& local_surface_id) { … }
void Compositor::SetDisplayColorSpaces(
const gfx::DisplayColorSpaces& display_color_spaces) { … }
#if BUILDFLAG(IS_MAC)
void Compositor::SetVSyncDisplayID(const int64_t display_id) {
if (display_id_ == display_id) {
return;
}
display_id_ = display_id;
if (display_private_) {
display_private_->SetVSyncDisplayID(display_id);
}
}
int64_t Compositor::display_id() const {
return display_id_;
}
#endif
void Compositor::SetDisplayTransformHint(gfx::OverlayTransform hint) { … }
void Compositor::SetBackgroundColor(SkColor color) { … }
void Compositor::SetVisible(bool visible) { … }
bool Compositor::IsVisible() { … }
bool Compositor::ScrollLayerTo(cc::ElementId element_id,
const gfx::PointF& offset) { … }
bool Compositor::GetScrollOffsetForLayer(cc::ElementId element_id,
gfx::PointF* offset) const { … }
void Compositor::SetDisplayVSyncParameters(base::TimeTicks timebase,
base::TimeDelta interval) { … }
void Compositor::AddVSyncParameterObserver(
mojo::PendingRemote<viz::mojom::VSyncParameterObserver> observer) { … }
void Compositor::SetMaxVSyncAndVrr(
const std::optional<base::TimeDelta>& max_vsync_interval,
display::VariableRefreshRateState vrr_state) { … }
void Compositor::SetAcceleratedWidget(gfx::AcceleratedWidget widget) { … }
gfx::AcceleratedWidget Compositor::ReleaseAcceleratedWidget() { … }
gfx::AcceleratedWidget Compositor::widget() const { … }
void Compositor::AddObserver(CompositorObserver* observer) { … }
void Compositor::RemoveObserver(CompositorObserver* observer) { … }
bool Compositor::HasObserver(const CompositorObserver* observer) const { … }
void Compositor::AddAnimationObserver(CompositorAnimationObserver* observer) { … }
void Compositor::RemoveAnimationObserver(
CompositorAnimationObserver* observer) { … }
bool Compositor::HasAnimationObserver(
const CompositorAnimationObserver* observer) const { … }
void Compositor::IssueExternalBeginFrame(
const viz::BeginFrameArgs& args,
bool force,
base::OnceCallback<void(const viz::BeginFrameAck&)> callback) { … }
ThroughputTracker Compositor::RequestNewThroughputTracker() { … }
double Compositor::GetPercentDroppedFrames() const { … }
std::unique_ptr<cc::EventsMetricsManager::ScopedMonitor>
Compositor::GetScopedEventMetricsMonitor(
cc::EventsMetricsManager::ScopedMonitor::DoneCallback done_callback) { … }
void Compositor::DidBeginMainFrame() { … }
void Compositor::DidUpdateLayers() { … }
void Compositor::BeginMainFrame(const viz::BeginFrameArgs& args) { … }
void Compositor::BeginMainFrameNotExpectedSoon() { … }
void Compositor::BeginMainFrameNotExpectedUntil(base::TimeTicks time) { … }
void Compositor::SendDamagedRectsRecursive(Layer* layer) { … }
void Compositor::UpdateLayerTreeHost() { … }
void Compositor::RequestNewLayerTreeFrameSink() { … }
void Compositor::DidFailToInitializeLayerTreeFrameSink() { … }
void Compositor::DidCommit(int source_frame_number,
base::TimeTicks,
base::TimeTicks) { … }
std::unique_ptr<cc::BeginMainFrameMetrics>
Compositor::GetBeginMainFrameMetrics() { … }
std::unique_ptr<cc::WebVitalMetrics> Compositor::GetWebVitalMetrics() { … }
void Compositor::NotifyThroughputTrackerResults(
cc::CustomTrackerResults results) { … }
void Compositor::DidReceiveCompositorFrameAckDeprecatedForCompositor() { … }
void Compositor::DidPresentCompositorFrame(
uint32_t frame_token,
const viz::FrameTimingDetails& frame_timing_details) { … }
void Compositor::DidSubmitCompositorFrame() { … }
void Compositor::FrameIntervalUpdated(base::TimeDelta interval) { … }
void Compositor::FrameSinksToThrottleUpdated(
const base::flat_set<viz::FrameSinkId>& ids) { … }
void Compositor::OnFirstSurfaceActivation(
const viz::SurfaceInfo& surface_info) { … }
void Compositor::OnFrameTokenChanged(uint32_t frame_token,
base::TimeTicks activation_time) { … }
Compositor::TrackerState::TrackerState() = default;
Compositor::TrackerState::TrackerState(TrackerState&&) = default;
Compositor::TrackerState& Compositor::TrackerState::operator=(TrackerState&&) =
default;
Compositor::TrackerState::~TrackerState() = default;
void Compositor::StartThroughputTracker(
TrackerId tracker_id,
ThroughputTrackerHost::ReportCallback callback) { … }
bool Compositor::StopThroughputTracker(TrackerId tracker_id) { … }
void Compositor::CancelThroughputTracker(TrackerId tracker_id) { … }
void Compositor::OnResume() { … }
#if BUILDFLAG(IS_LINUX) && BUILDFLAG(IS_OZONE_X11)
void Compositor::OnCompleteSwapWithNewSize(const gfx::Size& size) { … }
#endif
void Compositor::SetOutputIsSecure(bool output_is_secure) { … }
const cc::LayerTreeDebugState& Compositor::GetLayerTreeDebugState() const { … }
void Compositor::SetLayerTreeDebugState(
const cc::LayerTreeDebugState& debug_state) { … }
void Compositor::RequestPresentationTimeForNextFrame(
PresentationTimeCallback callback) { … }
void Compositor::RequestSuccessfulPresentationTimeForNextFrame(
SuccessfulPresentationTimeCallback callback) { … }
void Compositor::ReportMetricsForTracker(
int tracker_id,
const cc::FrameSequenceMetrics::CustomReportData& data) { … }
void Compositor::SetDelegatedInkPointRenderer(
mojo::PendingReceiver<gfx::mojom::DelegatedInkPointRenderer> receiver) { … }
const cc::LayerTreeSettings& Compositor::GetLayerTreeSettings() const { … }
void Compositor::AddSimpleBeginFrameObserver(
ui::HostBeginFrameObserver::SimpleBeginFrameObserver* obs) { … }
void Compositor::RemoveSimpleBeginFrameObserver(
ui::HostBeginFrameObserver::SimpleBeginFrameObserver* obs) { … }
void Compositor::MaybeUpdateObserveBeginFrame() { … }
#if BUILDFLAG(IS_CHROMEOS_ASH)
void Compositor::SetSeamlessRefreshRates(
const std::vector<float>& seamless_refresh_rates) {
seamless_refresh_rates_ = seamless_refresh_rates;
if (display_private_) {
display_private_->SetSupportedRefreshRates(seamless_refresh_rates);
}
}
void Compositor::OnSetPreferredRefreshRate(float refresh_rate) {
for (auto& observer : observer_list_) {
observer.OnSetPreferredRefreshRate(this, refresh_rate);
}
}
#endif
}