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
|
// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "ui/compositor/compositor.h"
#include <stddef.h>
#include <algorithm>
#include <memory>
#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/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/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 {
// Used to hold on to IssueExternalBeginFrame arguments if
// |external_begin_frame_controller_| isn't ready yet.
struct PendingBeginFrameArgs {
PendingBeginFrameArgs(
const viz::BeginFrameArgs& args,
bool force,
base::OnceCallback<void(const viz::BeginFrameAck&)> callback)
: args(args), force(force), callback(std::move(callback)) {}
viz::BeginFrameArgs args;
bool force;
base::OnceCallback<void(const viz::BeginFrameAck&)> callback;
};
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)
: context_factory_(context_factory),
frame_sink_id_(frame_sink_id),
task_runner_(task_runner),
use_external_begin_frame_control_(use_external_begin_frame_control),
force_software_compositor_(force_software_compositor),
layer_animator_collection_(this),
is_pixel_canvas_(enable_pixel_canvas),
lock_manager_(task_runner) {
DCHECK(context_factory_);
auto* host_frame_sink_manager = context_factory_->GetHostFrameSinkManager();
host_frame_sink_manager->RegisterFrameSinkId(
frame_sink_id_, this, viz::ReportFirstSurfaceActivation::kNo);
host_frame_sink_manager->SetFrameSinkDebugLabel(frame_sink_id_, "Compositor");
root_web_layer_ = cc::Layer::Create();
base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
cc::LayerTreeSettings settings;
// This will ensure PictureLayers always can have LCD text, to match the
// previous behaviour with ContentLayers, where LCD-not-allowed notifications
// were ignored.
settings.layers_always_allowed_lcd_text = true;
// Use occlusion to allow more overlapping windows to take less memory.
settings.use_occlusion_for_tile_prioritization = true;
settings.main_frame_before_activation_enabled = false;
// Browser UI generally doesn't get gains from keeping around hidden layers.
// Better to release the resources and save memory.
settings.release_tile_resources_for_hidden_layers = true;
// Disable edge anti-aliasing in order to increase support for HW overlays.
settings.enable_edge_anti_aliasing = false;
// GPU rasterization in the UI compositor is controlled by a feature.
settings.gpu_rasterization_disabled =
!features::IsUiGpuRasterizationEnabled();
if (command_line->HasSwitch(cc::switches::kUIShowCompositedLayerBorders)) {
std::string layer_borders_string = command_line->GetSwitchValueASCII(
cc::switches::kUIShowCompositedLayerBorders);
std::vector<base::StringPiece> entries = base::SplitStringPiece(
layer_borders_string, ",", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL);
if (entries.empty()) {
settings.initial_debug_state.show_debug_borders.set();
} else {
for (const auto& entry : entries) {
const struct {
const char* name;
cc::DebugBorderType type;
} kBorders[] = {{cc::switches::kCompositedRenderPassBorders,
cc::DebugBorderType::RENDERPASS},
{cc::switches::kCompositedSurfaceBorders,
cc::DebugBorderType::SURFACE},
{cc::switches::kCompositedLayerBorders,
cc::DebugBorderType::LAYER}};
for (const auto& border : kBorders) {
if (border.name == entry) {
settings.initial_debug_state.show_debug_borders.set(border.type);
break;
}
}
}
}
}
settings.initial_debug_state.show_fps_counter =
command_line->HasSwitch(cc::switches::kUIShowFPSCounter);
settings.initial_debug_state.show_layer_animation_bounds_rects =
command_line->HasSwitch(cc::switches::kUIShowLayerAnimationBounds);
settings.initial_debug_state.show_paint_rects =
command_line->HasSwitch(switches::kUIShowPaintRects);
settings.initial_debug_state.show_property_changed_rects =
command_line->HasSwitch(cc::switches::kUIShowPropertyChangedRects);
settings.initial_debug_state.show_surface_damage_rects =
command_line->HasSwitch(cc::switches::kUIShowSurfaceDamageRects);
settings.initial_debug_state.show_screen_space_rects =
command_line->HasSwitch(cc::switches::kUIShowScreenSpaceRects);
settings.initial_debug_state.SetRecordRenderingStats(
command_line->HasSwitch(cc::switches::kEnableGpuBenchmarking));
settings.use_zero_copy = IsUIZeroCopyEnabled() && !features::IsUsingRawDraw();
settings.use_layer_lists =
command_line->HasSwitch(cc::switches::kUIEnableLayerLists);
// UI compositor always uses partial raster if not using zero-copy. Zero copy
// doesn't currently support partial raster.
// RawDraw doesn't support partial raster.
settings.use_partial_raster =
!(settings.use_zero_copy || features::IsUsingRawDraw());
settings.use_rgba_4444 =
command_line->HasSwitch(switches::kUIEnableRGBA4444Textures);
#if BUILDFLAG(IS_APPLE)
// Using CoreAnimation to composite requires using GpuMemoryBuffers, which
// require zero copy.
settings.use_gpu_memory_buffer_resources = settings.use_zero_copy;
settings.enable_elastic_overscroll = true;
#endif
#if BUILDFLAG(IS_CHROMEOS_LACROS)
// Rasterized tiles must be overlay candidates to be forwarded.
// This is very similar to the line above for Apple.
settings.use_gpu_memory_buffer_resources =
features::IsDelegatedCompositingEnabled();
#endif
// Set use_gpu_memory_buffer_resources to false to disable delegated
// compositing, if RawDraw is enabled.
if (settings.use_gpu_memory_buffer_resources && features::IsUsingRawDraw()) {
settings.use_gpu_memory_buffer_resources = false;
}
settings.memory_policy.bytes_limit_when_visible =
(memory_limit_when_visible_mb > 0 ? memory_limit_when_visible_mb : 512) *
1024 * 1024;
settings.memory_policy.priority_cutoff_when_visible =
gpu::MemoryAllocation::CUTOFF_ALLOW_NICE_TO_HAVE;
settings.disallow_non_exact_resource_reuse =
command_line->HasSwitch(switches::kDisallowNonExactResourceReuse);
settings.wait_for_all_pipeline_stages_before_draw =
command_line->HasSwitch(switches::kRunAllCompositorStagesBeforeDraw);
if (features::IsPercentBasedScrollingEnabled()) {
settings.percent_based_scrolling = true;
}
settings.enable_compositing_based_throttling =
enable_compositing_based_throttling;
settings.is_layer_tree_for_ui = true;
#if DCHECK_IS_ON()
if (command_line->HasSwitch(cc::switches::kLogOnUIDoubleBackgroundBlur))
settings.log_on_ui_double_background_blur = true;
#endif
settings.enable_shared_image_cache_for_gpu =
base::FeatureList::IsEnabled(features::kUIEnableSharedImageCacheForGpu);
animation_host_ = cc::AnimationHost::CreateMainInstance();
cc::LayerTreeHost::InitParams params;
params.client = this;
params.task_graph_runner = context_factory_->GetTaskGraphRunner();
params.settings = &settings;
params.main_task_runner = task_runner_;
params.mutator_host = animation_host_.get();
host_ = cc::LayerTreeHost::CreateSingleThreaded(this, std::move(params));
const base::WeakPtr<cc::CompositorDelegateForInput>& compositor_delegate =
host_->GetDelegateForInput();
if (base::FeatureList::IsEnabled(features::kUiCompositorScrollWithLayers) &&
compositor_delegate) {
input_handler_weak_ = cc::InputHandler::Create(*compositor_delegate);
scroll_input_handler_ =
std::make_unique<ScrollInputHandler>(input_handler_weak_);
}
animation_timeline_ =
cc::AnimationTimeline::Create(cc::AnimationIdProvider::NextTimelineId());
animation_host_->AddAnimationTimeline(animation_timeline_.get());
host_->SetRootLayer(root_web_layer_);
// This shouldn't be done in the constructor in order to match Widget.
// See: http://crbug.com/956264.
host_->SetVisible(true);
if (base::PowerMonitor::IsInitialized())
base::PowerMonitor::AddPowerSuspendObserver(this);
if (command_line->HasSwitch(switches::kUISlowAnimations)) {
slow_animations_ = std::make_unique<ScopedAnimationDurationScaleMode>(
ScopedAnimationDurationScaleMode::SLOW_DURATION);
}
settings.disable_frame_rate_limit =
command_line->HasSwitch(switches::kDisableFrameRateLimit);
}
Compositor::~Compositor() {
TRACE_EVENT0("shutdown,viz", "Compositor::destructor");
if (base::PowerMonitor::IsInitialized())
base::PowerMonitor::RemovePowerSuspendObserver(this);
for (auto& observer : observer_list_)
observer.OnCompositingShuttingDown(this);
for (auto& observer : animation_observer_list_)
observer.OnCompositingShuttingDown(this);
if (root_layer_)
root_layer_->ResetCompositor();
if (animation_timeline_)
animation_host_->RemoveAnimationTimeline(animation_timeline_.get());
// Stop all outstanding draws before telling the ContextFactory to tear
// down any contexts that the |host_| may rely upon.
host_.reset();
context_factory_->RemoveCompositor(this);
auto* host_frame_sink_manager = context_factory_->GetHostFrameSinkManager();
for (auto& client : child_frame_sinks_) {
DCHECK(client.is_valid());
host_frame_sink_manager->UnregisterFrameSinkHierarchy(frame_sink_id_,
client);
}
host_frame_sink_manager->InvalidateFrameSinkId(frame_sink_id_, this);
}
void Compositor::AddChildFrameSink(const viz::FrameSinkId& frame_sink_id) {
context_factory_->GetHostFrameSinkManager()->RegisterFrameSinkHierarchy(
frame_sink_id_, frame_sink_id);
auto result = child_frame_sinks_.insert(frame_sink_id);
DCHECK(result.second);
}
void Compositor::RemoveChildFrameSink(const viz::FrameSinkId& frame_sink_id) {
auto it = child_frame_sinks_.find(frame_sink_id);
CHECK(it != child_frame_sinks_.end());
DCHECK(it->is_valid());
context_factory_->GetHostFrameSinkManager()->UnregisterFrameSinkHierarchy(
frame_sink_id_, *it);
child_frame_sinks_.erase(it);
}
void Compositor::SetLayerTreeFrameSink(
std::unique_ptr<cc::LayerTreeFrameSink> layer_tree_frame_sink,
mojo::AssociatedRemote<viz::mojom::DisplayPrivate> display_private) {
layer_tree_frame_sink_requested_ = false;
display_private_ = std::move(display_private);
host_->SetLayerTreeFrameSink(std::move(layer_tree_frame_sink));
// Display properties are reset when the output surface is lost, so update it
// to match the Compositor's.
if (display_private_) {
disabled_swap_until_resize_ = false;
display_private_->Resize(size());
display_private_->SetDisplayVisible(host_->IsVisible());
display_private_->SetDisplayColorSpaces(display_color_spaces_);
display_private_->SetDisplayColorMatrix(
gfx::SkM44ToTransform(display_color_matrix_));
display_private_->SetOutputIsSecure(output_is_secure_);
#if BUILDFLAG(IS_MAC)
display_private_->SetVSyncDisplayID(display_id_);
#endif
if (has_vsync_params_) {
display_private_->SetDisplayVSyncParameters(vsync_timebase_,
vsync_interval_);
}
if (max_vrr_interval_.has_value()) {
display_private_->SetMaxVrrInterval(max_vrr_interval_);
}
}
}
void Compositor::SetExternalBeginFrameController(
mojo::AssociatedRemote<viz::mojom::ExternalBeginFrameController>
external_begin_frame_controller) {
DCHECK(use_external_begin_frame_control());
external_begin_frame_controller_ = std::move(external_begin_frame_controller);
if (pending_begin_frame_args_) {
external_begin_frame_controller_->IssueExternalBeginFrame(
pending_begin_frame_args_->args, pending_begin_frame_args_->force,
std::move(pending_begin_frame_args_->callback));
pending_begin_frame_args_.reset();
}
}
void Compositor::OnChildResizing() {
for (auto& observer : observer_list_)
observer.OnCompositingChildResizing(this);
}
void Compositor::ScheduleDraw() {
host_->SetNeedsCommit();
}
void Compositor::SetRootLayer(Layer* root_layer) {
if (root_layer_ == root_layer)
return;
if (root_layer_)
root_layer_->ResetCompositor();
root_layer_ = root_layer;
root_web_layer_->RemoveAllChildren();
if (root_layer_)
root_layer_->SetCompositor(this, root_web_layer_);
}
void Compositor::DisableAnimations() {
DCHECK(animations_are_enabled_);
animations_are_enabled_ = false;
root_layer_->ResetCompositorForAnimatorsInTree(this);
}
void Compositor::EnableAnimations() {
DCHECK(!animations_are_enabled_);
animations_are_enabled_ = true;
root_layer_->SetCompositorForAnimatorsInTree(this);
}
cc::AnimationTimeline* Compositor::GetAnimationTimeline() const {
return animation_timeline_.get();
}
void Compositor::SetDisplayColorMatrix(const SkM44& matrix) {
display_color_matrix_ = matrix;
if (display_private_)
display_private_->SetDisplayColorMatrix(gfx::SkM44ToTransform(matrix));
}
void Compositor::ScheduleFullRedraw() {
// TODO(enne): Some callers (mac) call this function expecting that it
// will also commit. This should probably just redraw the screen
// from damage and not commit. ScheduleDraw/ScheduleRedraw need
// better names.
host_->SetNeedsRedrawRect(host_->device_viewport_rect());
host_->SetNeedsCommit();
}
void Compositor::ScheduleRedrawRect(const gfx::Rect& damage_rect) {
// TODO(enne): Make this not commit. See ScheduleFullRedraw.
host_->SetNeedsRedrawRect(damage_rect);
host_->SetNeedsCommit();
}
#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_) {
// Browser needs to block for Viz to receive and process this message.
// Otherwise when we return from WM_WINDOWPOSCHANGING message handler and
// receive a WM_WINDOWPOSCHANGED the resize is finalized and any swaps of
// wrong size by Viz can cause the swapped content to get scaled.
// TODO(crbug.com/859168): Investigate nonblocking ways for solving.
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) {
DCHECK_GT(scale, 0);
bool device_scale_factor_changed = device_scale_factor_ != scale;
device_scale_factor_ = scale;
// cc requires the size to be non-empty (meaning DCHECKs if size is empty).
if (!size_in_pixel.IsEmpty()) {
bool size_changed = size_ != size_in_pixel;
size_ = size_in_pixel;
host_->SetViewportRectAndScale(gfx::Rect(size_in_pixel), scale,
local_surface_id);
root_web_layer_->SetBounds(size_in_pixel);
if (display_private_ && (size_changed || disabled_swap_until_resize_)) {
display_private_->Resize(size_in_pixel);
disabled_swap_until_resize_ = false;
}
}
if (device_scale_factor_changed) {
if (is_pixel_canvas())
host_->SetRecordingScaleFactor(scale);
if (root_layer_)
root_layer_->OnDeviceScaleFactorChanged(scale);
}
}
void Compositor::SetDisplayColorSpaces(
const gfx::DisplayColorSpaces& display_color_spaces) {
if (display_color_spaces_ == display_color_spaces)
return;
display_color_spaces_ = display_color_spaces;
host_->SetDisplayColorSpaces(display_color_spaces_);
// Always force the ui::Compositor to re-draw all layers, because damage
// tracking bugs result in black flashes.
// https://crbug.com/804430
// TODO(ccameron): Remove this when the above bug is fixed.
host_->SetNeedsDisplayOnAllLayers();
// Color space is reset when the output surface is lost, so this must also be
// updated then.
if (display_private_)
display_private_->SetDisplayColorSpaces(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) {
host_->set_display_transform_hint(hint);
}
void Compositor::SetBackgroundColor(SkColor color) {
// TODO(crbug/1308932): Remove FromColor and make all SkColor4f.
host_->set_background_color(SkColor4f::FromColor(color));
ScheduleDraw();
}
void Compositor::SetVisible(bool visible) {
host_->SetVisible(visible);
// Visibility is reset when the output surface is lost, so this must also be
// updated then.
if (display_private_)
display_private_->SetDisplayVisible(visible);
}
bool Compositor::IsVisible() {
return host_->IsVisible();
}
// TODO(bokan): These calls should be delegated through the
// scroll_input_handler_ so that we don't have to keep a pointer to the
// cc::InputHandler in this class.
bool Compositor::ScrollLayerTo(cc::ElementId element_id,
const gfx::PointF& offset) {
return input_handler_weak_ &&
input_handler_weak_->ScrollLayerTo(element_id, offset);
}
bool Compositor::GetScrollOffsetForLayer(cc::ElementId element_id,
gfx::PointF* offset) const {
return input_handler_weak_ &&
input_handler_weak_->GetScrollOffsetForLayer(element_id, offset);
}
void Compositor::SetDisplayVSyncParameters(base::TimeTicks timebase,
base::TimeDelta interval) {
static bool is_frame_rate_limit_disabled =
base::CommandLine::ForCurrentProcess()->HasSwitch(
switches::kDisableFrameRateLimit);
if (is_frame_rate_limit_disabled)
return;
if (interval.is_zero()) {
// TODO(brianderson): We should not be receiving 0 intervals.
interval = viz::BeginFrameArgs::DefaultInterval();
}
DCHECK_GT(interval.InMillisecondsF(), 0);
// This is called at high frequenty on macOS, so early-out of redundant
// updates here.
if (vsync_timebase_ == timebase && vsync_interval_ == interval)
return;
if (interval != vsync_interval_)
has_vsync_params_ = true;
vsync_timebase_ = timebase;
vsync_interval_ = interval;
if (display_private_)
display_private_->SetDisplayVSyncParameters(timebase, interval);
}
void Compositor::AddVSyncParameterObserver(
mojo::PendingRemote<viz::mojom::VSyncParameterObserver> observer) {
if (display_private_)
display_private_->AddVSyncParameterObserver(std::move(observer));
}
void Compositor::SetMaxVrrInterval(
const absl::optional<base::TimeDelta>& max_vrr_interval) {
max_vrr_interval_ = max_vrr_interval;
if (display_private_) {
display_private_->SetMaxVrrInterval(max_vrr_interval);
}
}
void Compositor::SetAcceleratedWidget(gfx::AcceleratedWidget widget) {
// This function should only get called once.
DCHECK(!widget_valid_);
widget_ = widget;
widget_valid_ = true;
if (layer_tree_frame_sink_requested_) {
context_factory_->CreateLayerTreeFrameSink(
context_creation_weak_ptr_factory_.GetWeakPtr());
}
}
gfx::AcceleratedWidget Compositor::ReleaseAcceleratedWidget() {
DCHECK(!IsVisible());
host_->ReleaseLayerTreeFrameSink();
display_private_.reset();
external_begin_frame_controller_.reset();
context_factory_->RemoveCompositor(this);
context_creation_weak_ptr_factory_.InvalidateWeakPtrs();
widget_valid_ = false;
gfx::AcceleratedWidget widget = widget_;
widget_ = gfx::kNullAcceleratedWidget;
return widget;
}
gfx::AcceleratedWidget Compositor::widget() const {
DCHECK(widget_valid_);
return widget_;
}
void Compositor::AddObserver(CompositorObserver* observer) {
observer_list_.AddObserver(observer);
}
void Compositor::RemoveObserver(CompositorObserver* observer) {
observer_list_.RemoveObserver(observer);
}
bool Compositor::HasObserver(const CompositorObserver* observer) const {
return observer_list_.HasObserver(observer);
}
void Compositor::AddAnimationObserver(CompositorAnimationObserver* observer) {
animation_started_ = true;
if (animation_observer_list_.empty()) {
for (auto& obs : observer_list_)
obs.OnFirstAnimationStarted(this);
}
observer->Start();
animation_observer_list_.AddObserver(observer);
host_->SetNeedsAnimate();
}
void Compositor::RemoveAnimationObserver(
CompositorAnimationObserver* observer) {
if (!animation_observer_list_.HasObserver(observer))
return;
for (auto& aobs : animation_observer_list_)
aobs.Check();
animation_observer_list_.RemoveObserver(observer);
if (animation_observer_list_.empty()) {
// The only way to get here should be through the AddAnimationObserver.
DCHECK(animation_started_);
// Request one more frame so that BeginMainFrame could notify the observers.
host_->SetNeedsAnimate();
}
}
bool Compositor::HasAnimationObserver(
const CompositorAnimationObserver* observer) const {
return animation_observer_list_.HasObserver(observer);
}
void Compositor::IssueExternalBeginFrame(
const viz::BeginFrameArgs& args,
bool force,
base::OnceCallback<void(const viz::BeginFrameAck&)> callback) {
if (!external_begin_frame_controller_) {
// IssueExternalBeginFrame() shouldn't be called again before the previous
// begin frame is acknowledged.
DCHECK(!pending_begin_frame_args_);
pending_begin_frame_args_ = std::make_unique<PendingBeginFrameArgs>(
args, force, std::move(callback));
return;
}
external_begin_frame_controller_->IssueExternalBeginFrame(
args, force, std::move(callback));
}
ThroughputTracker Compositor::RequestNewThroughputTracker() {
return ThroughputTracker(next_throughput_tracker_id_++,
weak_ptr_factory_.GetWeakPtr());
}
double Compositor::GetPercentDroppedFrames() const {
return host_->GetPercentDroppedFrames();
}
std::unique_ptr<cc::EventsMetricsManager::ScopedMonitor>
Compositor::GetScopedEventMetricsMonitor(
cc::EventsMetricsManager::ScopedMonitor::DoneCallback done_callback) {
return host_->GetScopedEventMetricsMonitor(std::move(done_callback));
}
void Compositor::DidBeginMainFrame() {
for (auto& obs : observer_list_)
obs.OnDidBeginMainFrame(this);
}
void Compositor::DidUpdateLayers() {
// Dump property trees and layers if run with:
// --vmodule=*ui/compositor*=3
VLOG(3) << "After updating layers:\n"
<< "property trees:\n"
<< host_->property_trees()->ToString() << "\n"
<< "cc::Layers:\n"
<< host_->LayersAsString();
}
void Compositor::BeginMainFrame(const viz::BeginFrameArgs& args) {
DCHECK(!IsLocked());
for (auto& observer : animation_observer_list_)
observer.OnAnimationStep(args.frame_time);
if (!animation_observer_list_.empty()) {
host_->SetNeedsAnimate();
} else if (animation_started_) {
// When |animation_started_| is true but there are no animations observers
// notify the compositor observers.
animation_started_ = false;
for (auto& obs : observer_list_)
obs.OnFirstNonAnimatedFrameStarted(this);
}
}
void Compositor::BeginMainFrameNotExpectedSoon() {}
void Compositor::BeginMainFrameNotExpectedUntil(base::TimeTicks time) {}
// static
void Compositor::SendDamagedRectsRecursive(Layer* layer) {
layer->SendDamagedRects();
// Iterate using the size for the case of mutation during sending damaged
// regions. https://crbug.com/1242257.
base::AutoReset<bool> setter(&(layer->sending_damaged_rects_for_descendants_),
true);
for (size_t i = 0; i < layer->children().size(); ++i)
SendDamagedRectsRecursive(layer->children()[i]);
}
void Compositor::UpdateLayerTreeHost() {
if (!root_layer())
return;
SendDamagedRectsRecursive(root_layer());
}
void Compositor::RequestNewLayerTreeFrameSink() {
DCHECK(!layer_tree_frame_sink_requested_);
layer_tree_frame_sink_requested_ = true;
if (widget_valid_) {
context_factory_->CreateLayerTreeFrameSink(
context_creation_weak_ptr_factory_.GetWeakPtr());
}
}
void Compositor::DidFailToInitializeLayerTreeFrameSink() {
task_runner_->PostTask(
FROM_HERE,
base::BindOnce(&Compositor::RequestNewLayerTreeFrameSink,
context_creation_weak_ptr_factory_.GetWeakPtr()));
}
void Compositor::DidCommit(base::TimeTicks, base::TimeTicks) {
DCHECK(!IsLocked());
for (auto& observer : observer_list_)
observer.OnCompositingDidCommit(this);
}
std::unique_ptr<cc::BeginMainFrameMetrics>
Compositor::GetBeginMainFrameMetrics() {
#if BUILDFLAG(IS_CHROMEOS)
auto metrics_data = std::make_unique<cc::BeginMainFrameMetrics>();
metrics_data->should_measure_smoothness = true;
return metrics_data;
#else
return nullptr;
#endif
}
std::unique_ptr<cc::WebVitalMetrics> Compositor::GetWebVitalMetrics() {
return nullptr;
}
void Compositor::NotifyThroughputTrackerResults(
cc::CustomTrackerResults results) {
for (auto& pair : results)
ReportMetricsForTracker(pair.first, std::move(pair.second));
}
void Compositor::DidReceiveCompositorFrameAck() {
++activated_frame_count_;
for (auto& observer : observer_list_)
observer.OnCompositingEnded(this);
}
void Compositor::DidPresentCompositorFrame(
uint32_t frame_token,
const gfx::PresentationFeedback& feedback) {
TRACE_EVENT_MARK_WITH_TIMESTAMP1("cc,benchmark", "FramePresented",
feedback.timestamp, "environment",
"browser");
for (auto& observer : observer_list_)
observer.OnDidPresentCompositorFrame(frame_token, feedback);
}
void Compositor::DidSubmitCompositorFrame() {
base::TimeTicks start_time = base::TimeTicks::Now();
for (auto& observer : observer_list_)
observer.OnCompositingStarted(this, start_time);
}
void Compositor::FrameIntervalUpdated(base::TimeDelta interval) {
refresh_rate_ = interval.ToHz();
}
void Compositor::FrameSinksToThrottleUpdated(
const base::flat_set<viz::FrameSinkId>& ids) {
for (auto& observer : observer_list_) {
observer.OnFrameSinksToThrottleUpdated(ids);
}
}
void Compositor::OnFirstSurfaceActivation(
const viz::SurfaceInfo& surface_info) {
NOTREACHED();
}
void Compositor::OnFrameTokenChanged(uint32_t frame_token,
base::TimeTicks activation_time) {
// TODO(yiyix, fsamuel): Implement frame token propagation for Compositor.
NOTREACHED();
}
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) {
DCHECK(!base::Contains(throughput_tracker_map_, tracker_id));
auto& tracker_state = throughput_tracker_map_[tracker_id];
tracker_state.report_callback = std::move(callback);
animation_host_->StartThroughputTracking(tracker_id);
}
bool Compositor::StopThroughtputTracker(TrackerId tracker_id) {
auto it = throughput_tracker_map_.find(tracker_id);
DCHECK(it != throughput_tracker_map_.end());
// Clean up if report has happened since StopThroughputTracking would
// not trigger report in this case.
if (it->second.report_attempted) {
throughput_tracker_map_.erase(it);
return false;
}
it->second.should_report = true;
animation_host_->StopThroughputTracking(tracker_id);
return true;
}
void Compositor::CancelThroughtputTracker(TrackerId tracker_id) {
auto it = throughput_tracker_map_.find(tracker_id);
DCHECK(it != throughput_tracker_map_.end());
const bool should_stop = !it->second.report_attempted;
throughput_tracker_map_.erase(it);
if (should_stop)
animation_host_->StopThroughputTracking(tracker_id);
}
void Compositor::OnResume() {
// Restart the time upon resume.
for (auto& obs : animation_observer_list_)
obs.ResetIfActive();
}
#if BUILDFLAG(IS_OZONE)
#if BUILDFLAG(OZONE_PLATFORM_X11)
void Compositor::OnCompleteSwapWithNewSize(const gfx::Size& size) {
for (auto& observer : observer_list_)
observer.OnCompositingCompleteSwapWithNewSize(this, size);
}
#endif // BUILDFLAG(OZONE_PLATFORM_X11)
#endif // BUILFFLAG(IS_OZONE)
void Compositor::SetOutputIsSecure(bool output_is_secure) {
output_is_secure_ = output_is_secure;
if (display_private_)
display_private_->SetOutputIsSecure(output_is_secure);
}
const cc::LayerTreeDebugState& Compositor::GetLayerTreeDebugState() const {
return host_->GetDebugState();
}
void Compositor::SetLayerTreeDebugState(
const cc::LayerTreeDebugState& debug_state) {
host_->SetDebugState(debug_state);
}
void Compositor::RequestPresentationTimeForNextFrame(
PresentationTimeCallback callback) {
host_->RequestPresentationTimeForNextFrame(std::move(callback));
}
void Compositor::RequestSuccessfulPresentationTimeForNextFrame(
SuccessfulPresentationTimeCallback callback) {
host_->RequestSuccessfulPresentationTimeForNextFrame(std::move(callback));
}
void Compositor::ReportMetricsForTracker(
int tracker_id,
const cc::FrameSequenceMetrics::CustomReportData& data) {
auto it = throughput_tracker_map_.find(tracker_id);
if (it == throughput_tracker_map_.end())
return;
// Set `report_attempted` but not reporting if relevant ThroughputTrackers
// are not stopped and waiting for reports.
if (!it->second.should_report) {
it->second.report_attempted = true;
return;
}
// Callback may modify `throughput_tracker_map_` so update the map first.
// See https://crbug.com/1193382.
auto callback = std::move(it->second.report_callback);
throughput_tracker_map_.erase(it);
std::move(callback).Run(data);
}
void Compositor::SetDelegatedInkPointRenderer(
mojo::PendingReceiver<gfx::mojom::DelegatedInkPointRenderer> receiver) {
if (display_private_)
display_private_->SetDelegatedInkPointRenderer(std::move(receiver));
}
const cc::LayerTreeSettings& Compositor::GetLayerTreeSettings() const {
return host_->GetSettings();
}
} // namespace ui
|