1 // Copyright 2010 The Chromium Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #include "cc/layers/layer.h" 6 7 #include <algorithm> 8 9 #include "base/atomic_sequence_num.h" 10 #include "base/debug/trace_event.h" 11 #include "base/location.h" 12 #include "base/metrics/histogram.h" 13 #include "base/single_thread_task_runner.h" 14 #include "base/time/time.h" 15 #include "cc/animation/animation.h" 16 #include "cc/animation/animation_events.h" 17 #include "cc/animation/keyframed_animation_curve.h" 18 #include "cc/animation/layer_animation_controller.h" 19 #include "cc/layers/layer_client.h" 20 #include "cc/layers/layer_impl.h" 21 #include "cc/layers/scrollbar_layer_interface.h" 22 #include "cc/output/copy_output_request.h" 23 #include "cc/output/copy_output_result.h" 24 #include "cc/trees/layer_tree_host.h" 25 #include "cc/trees/layer_tree_impl.h" 26 #include "third_party/skia/include/core/SkImageFilter.h" 27 #include "ui/gfx/geometry/vector2d_conversions.h" 28 #include "ui/gfx/rect_conversions.h" 29 30 namespace cc { 31 32 base::StaticAtomicSequenceNumber g_next_layer_id; 33 34 scoped_refptr<Layer> Layer::Create() { 35 return make_scoped_refptr(new Layer()); 36 } 37 38 Layer::Layer() 39 : needs_push_properties_(false), 40 num_dependents_need_push_properties_(false), 41 stacking_order_changed_(false), 42 // Layer IDs start from 1. 43 layer_id_(g_next_layer_id.GetNext() + 1), 44 ignore_set_needs_commit_(false), 45 sorting_context_id_(0), 46 parent_(NULL), 47 layer_tree_host_(NULL), 48 scroll_clip_layer_id_(INVALID_ID), 49 should_scroll_on_main_thread_(false), 50 have_wheel_event_handlers_(false), 51 have_scroll_event_handlers_(false), 52 user_scrollable_horizontal_(true), 53 user_scrollable_vertical_(true), 54 is_root_for_isolated_group_(false), 55 is_container_for_fixed_position_layers_(false), 56 is_drawable_(false), 57 hide_layer_and_subtree_(false), 58 masks_to_bounds_(false), 59 contents_opaque_(false), 60 double_sided_(true), 61 should_flatten_transform_(true), 62 use_parent_backface_visibility_(false), 63 draw_checkerboard_for_missing_tiles_(false), 64 force_render_surface_(false), 65 transform_is_invertible_(true), 66 background_color_(0), 67 opacity_(1.f), 68 blend_mode_(SkXfermode::kSrcOver_Mode), 69 scroll_parent_(NULL), 70 clip_parent_(NULL), 71 replica_layer_(NULL), 72 raster_scale_(0.f), 73 client_(NULL) { 74 layer_animation_controller_ = LayerAnimationController::Create(layer_id_); 75 layer_animation_controller_->AddValueObserver(this); 76 layer_animation_controller_->set_value_provider(this); 77 } 78 79 Layer::~Layer() { 80 // Our parent should be holding a reference to us so there should be no 81 // way for us to be destroyed while we still have a parent. 82 DCHECK(!parent()); 83 // Similarly we shouldn't have a layer tree host since it also keeps a 84 // reference to us. 85 DCHECK(!layer_tree_host()); 86 87 layer_animation_controller_->RemoveValueObserver(this); 88 layer_animation_controller_->remove_value_provider(this); 89 90 RemoveFromScrollTree(); 91 RemoveFromClipTree(); 92 93 // Remove the parent reference from all children and dependents. 94 RemoveAllChildren(); 95 if (mask_layer_.get()) { 96 DCHECK_EQ(this, mask_layer_->parent()); 97 mask_layer_->RemoveFromParent(); 98 } 99 if (replica_layer_.get()) { 100 DCHECK_EQ(this, replica_layer_->parent()); 101 replica_layer_->RemoveFromParent(); 102 } 103 } 104 105 void Layer::SetLayerTreeHost(LayerTreeHost* host) { 106 if (layer_tree_host_ == host) 107 return; 108 109 layer_tree_host_ = host; 110 111 // When changing hosts, the layer needs to commit its properties to the impl 112 // side for the new host. 113 SetNeedsPushProperties(); 114 115 for (size_t i = 0; i < children_.size(); ++i) 116 children_[i]->SetLayerTreeHost(host); 117 118 if (mask_layer_.get()) 119 mask_layer_->SetLayerTreeHost(host); 120 if (replica_layer_.get()) 121 replica_layer_->SetLayerTreeHost(host); 122 123 if (host) { 124 layer_animation_controller_->SetAnimationRegistrar( 125 host->animation_registrar()); 126 127 if (host->settings().layer_transforms_should_scale_layer_contents) 128 reset_raster_scale_to_unknown(); 129 } 130 131 if (host && layer_animation_controller_->has_any_animation()) 132 host->SetNeedsCommit(); 133 } 134 135 void Layer::SetNeedsUpdate() { 136 if (layer_tree_host_ && !ignore_set_needs_commit_) 137 layer_tree_host_->SetNeedsUpdateLayers(); 138 } 139 140 void Layer::SetNeedsCommit() { 141 if (!layer_tree_host_) 142 return; 143 144 SetNeedsPushProperties(); 145 146 if (ignore_set_needs_commit_) 147 return; 148 149 layer_tree_host_->SetNeedsCommit(); 150 } 151 152 void Layer::SetNeedsFullTreeSync() { 153 if (!layer_tree_host_) 154 return; 155 156 layer_tree_host_->SetNeedsFullTreeSync(); 157 } 158 159 void Layer::SetNextCommitWaitsForActivation() { 160 if (!layer_tree_host_) 161 return; 162 163 layer_tree_host_->SetNextCommitWaitsForActivation(); 164 } 165 166 void Layer::SetNeedsPushProperties() { 167 if (needs_push_properties_) 168 return; 169 if (!parent_should_know_need_push_properties() && parent_) 170 parent_->AddDependentNeedsPushProperties(); 171 needs_push_properties_ = true; 172 } 173 174 void Layer::AddDependentNeedsPushProperties() { 175 DCHECK_GE(num_dependents_need_push_properties_, 0); 176 177 if (!parent_should_know_need_push_properties() && parent_) 178 parent_->AddDependentNeedsPushProperties(); 179 180 num_dependents_need_push_properties_++; 181 } 182 183 void Layer::RemoveDependentNeedsPushProperties() { 184 num_dependents_need_push_properties_--; 185 DCHECK_GE(num_dependents_need_push_properties_, 0); 186 187 if (!parent_should_know_need_push_properties() && parent_) 188 parent_->RemoveDependentNeedsPushProperties(); 189 } 190 191 bool Layer::IsPropertyChangeAllowed() const { 192 if (!layer_tree_host_) 193 return true; 194 195 if (!layer_tree_host_->settings().strict_layer_property_change_checking) 196 return true; 197 198 return !layer_tree_host_->in_paint_layer_contents(); 199 } 200 201 gfx::Rect Layer::LayerRectToContentRect(const gfx::RectF& layer_rect) const { 202 gfx::RectF content_rect = 203 gfx::ScaleRect(layer_rect, contents_scale_x(), contents_scale_y()); 204 // Intersect with content rect to avoid the extra pixel because for some 205 // values x and y, ceil((x / y) * y) may be x + 1. 206 content_rect.Intersect(gfx::Rect(content_bounds())); 207 return gfx::ToEnclosingRect(content_rect); 208 } 209 210 skia::RefPtr<SkPicture> Layer::GetPicture() const { 211 return skia::RefPtr<SkPicture>(); 212 } 213 214 void Layer::SetParent(Layer* layer) { 215 DCHECK(!layer || !layer->HasAncestor(this)); 216 217 if (parent_should_know_need_push_properties()) { 218 if (parent_) 219 parent_->RemoveDependentNeedsPushProperties(); 220 if (layer) 221 layer->AddDependentNeedsPushProperties(); 222 } 223 224 parent_ = layer; 225 SetLayerTreeHost(parent_ ? parent_->layer_tree_host() : NULL); 226 227 if (!layer_tree_host_) 228 return; 229 const LayerTreeSettings& settings = layer_tree_host_->settings(); 230 if (!settings.layer_transforms_should_scale_layer_contents) 231 return; 232 233 reset_raster_scale_to_unknown(); 234 if (mask_layer_.get()) 235 mask_layer_->reset_raster_scale_to_unknown(); 236 if (replica_layer_.get() && replica_layer_->mask_layer_.get()) 237 replica_layer_->mask_layer_->reset_raster_scale_to_unknown(); 238 } 239 240 void Layer::AddChild(scoped_refptr<Layer> child) { 241 InsertChild(child, children_.size()); 242 } 243 244 void Layer::InsertChild(scoped_refptr<Layer> child, size_t index) { 245 DCHECK(IsPropertyChangeAllowed()); 246 child->RemoveFromParent(); 247 child->SetParent(this); 248 child->stacking_order_changed_ = true; 249 250 index = std::min(index, children_.size()); 251 children_.insert(children_.begin() + index, child); 252 SetNeedsFullTreeSync(); 253 } 254 255 void Layer::RemoveFromParent() { 256 DCHECK(IsPropertyChangeAllowed()); 257 if (parent_) 258 parent_->RemoveChildOrDependent(this); 259 } 260 261 void Layer::RemoveChildOrDependent(Layer* child) { 262 if (mask_layer_.get() == child) { 263 mask_layer_->SetParent(NULL); 264 mask_layer_ = NULL; 265 SetNeedsFullTreeSync(); 266 return; 267 } 268 if (replica_layer_.get() == child) { 269 replica_layer_->SetParent(NULL); 270 replica_layer_ = NULL; 271 SetNeedsFullTreeSync(); 272 return; 273 } 274 275 for (LayerList::iterator iter = children_.begin(); 276 iter != children_.end(); 277 ++iter) { 278 if (iter->get() != child) 279 continue; 280 281 child->SetParent(NULL); 282 children_.erase(iter); 283 SetNeedsFullTreeSync(); 284 return; 285 } 286 } 287 288 void Layer::ReplaceChild(Layer* reference, scoped_refptr<Layer> new_layer) { 289 DCHECK(reference); 290 DCHECK_EQ(reference->parent(), this); 291 DCHECK(IsPropertyChangeAllowed()); 292 293 if (reference == new_layer.get()) 294 return; 295 296 int reference_index = IndexOfChild(reference); 297 if (reference_index == -1) { 298 NOTREACHED(); 299 return; 300 } 301 302 reference->RemoveFromParent(); 303 304 if (new_layer.get()) { 305 new_layer->RemoveFromParent(); 306 InsertChild(new_layer, reference_index); 307 } 308 } 309 310 int Layer::IndexOfChild(const Layer* reference) { 311 for (size_t i = 0; i < children_.size(); ++i) { 312 if (children_[i].get() == reference) 313 return i; 314 } 315 return -1; 316 } 317 318 void Layer::SetBounds(const gfx::Size& size) { 319 DCHECK(IsPropertyChangeAllowed()); 320 if (bounds() == size) 321 return; 322 323 bounds_ = size; 324 SetNeedsCommit(); 325 } 326 327 Layer* Layer::RootLayer() { 328 Layer* layer = this; 329 while (layer->parent()) 330 layer = layer->parent(); 331 return layer; 332 } 333 334 void Layer::RemoveAllChildren() { 335 DCHECK(IsPropertyChangeAllowed()); 336 while (children_.size()) { 337 Layer* layer = children_[0].get(); 338 DCHECK_EQ(this, layer->parent()); 339 layer->RemoveFromParent(); 340 } 341 } 342 343 void Layer::SetChildren(const LayerList& children) { 344 DCHECK(IsPropertyChangeAllowed()); 345 if (children == children_) 346 return; 347 348 RemoveAllChildren(); 349 for (size_t i = 0; i < children.size(); ++i) 350 AddChild(children[i]); 351 } 352 353 bool Layer::HasAncestor(const Layer* ancestor) const { 354 for (const Layer* layer = parent(); layer; layer = layer->parent()) { 355 if (layer == ancestor) 356 return true; 357 } 358 return false; 359 } 360 361 void Layer::RequestCopyOfOutput( 362 scoped_ptr<CopyOutputRequest> request) { 363 DCHECK(IsPropertyChangeAllowed()); 364 if (request->IsEmpty()) 365 return; 366 copy_requests_.push_back(request.Pass()); 367 SetNeedsCommit(); 368 } 369 370 void Layer::SetBackgroundColor(SkColor background_color) { 371 DCHECK(IsPropertyChangeAllowed()); 372 if (background_color_ == background_color) 373 return; 374 background_color_ = background_color; 375 SetNeedsCommit(); 376 } 377 378 SkColor Layer::SafeOpaqueBackgroundColor() const { 379 SkColor color = background_color(); 380 if (SkColorGetA(color) == 255 && !contents_opaque()) { 381 color = SK_ColorTRANSPARENT; 382 } else if (SkColorGetA(color) != 255 && contents_opaque()) { 383 for (const Layer* layer = parent(); layer; 384 layer = layer->parent()) { 385 color = layer->background_color(); 386 if (SkColorGetA(color) == 255) 387 break; 388 } 389 if (SkColorGetA(color) != 255) 390 color = layer_tree_host_->background_color(); 391 if (SkColorGetA(color) != 255) 392 color = SkColorSetA(color, 255); 393 } 394 return color; 395 } 396 397 void Layer::CalculateContentsScale(float ideal_contents_scale, 398 float device_scale_factor, 399 float page_scale_factor, 400 float maximum_animation_contents_scale, 401 bool animating_transform_to_screen, 402 float* contents_scale_x, 403 float* contents_scale_y, 404 gfx::Size* content_bounds) { 405 DCHECK(layer_tree_host_); 406 407 *contents_scale_x = 1; 408 *contents_scale_y = 1; 409 *content_bounds = bounds(); 410 } 411 412 void Layer::SetMasksToBounds(bool masks_to_bounds) { 413 DCHECK(IsPropertyChangeAllowed()); 414 if (masks_to_bounds_ == masks_to_bounds) 415 return; 416 masks_to_bounds_ = masks_to_bounds; 417 SetNeedsCommit(); 418 } 419 420 void Layer::SetMaskLayer(Layer* mask_layer) { 421 DCHECK(IsPropertyChangeAllowed()); 422 if (mask_layer_.get() == mask_layer) 423 return; 424 if (mask_layer_.get()) { 425 DCHECK_EQ(this, mask_layer_->parent()); 426 mask_layer_->RemoveFromParent(); 427 } 428 mask_layer_ = mask_layer; 429 if (mask_layer_.get()) { 430 DCHECK(!mask_layer_->parent()); 431 mask_layer_->RemoveFromParent(); 432 mask_layer_->SetParent(this); 433 mask_layer_->SetIsMask(true); 434 } 435 SetNeedsFullTreeSync(); 436 } 437 438 void Layer::SetReplicaLayer(Layer* layer) { 439 DCHECK(IsPropertyChangeAllowed()); 440 if (replica_layer_.get() == layer) 441 return; 442 if (replica_layer_.get()) { 443 DCHECK_EQ(this, replica_layer_->parent()); 444 replica_layer_->RemoveFromParent(); 445 } 446 replica_layer_ = layer; 447 if (replica_layer_.get()) { 448 DCHECK(!replica_layer_->parent()); 449 replica_layer_->RemoveFromParent(); 450 replica_layer_->SetParent(this); 451 } 452 SetNeedsFullTreeSync(); 453 } 454 455 void Layer::SetFilters(const FilterOperations& filters) { 456 DCHECK(IsPropertyChangeAllowed()); 457 if (filters_ == filters) 458 return; 459 filters_ = filters; 460 SetNeedsCommit(); 461 } 462 463 bool Layer::FilterIsAnimating() const { 464 return layer_animation_controller_->IsAnimatingProperty(Animation::Filter); 465 } 466 467 void Layer::SetBackgroundFilters(const FilterOperations& filters) { 468 DCHECK(IsPropertyChangeAllowed()); 469 if (background_filters_ == filters) 470 return; 471 background_filters_ = filters; 472 SetNeedsCommit(); 473 } 474 475 void Layer::SetOpacity(float opacity) { 476 DCHECK(IsPropertyChangeAllowed()); 477 if (opacity_ == opacity) 478 return; 479 opacity_ = opacity; 480 SetNeedsCommit(); 481 } 482 483 bool Layer::OpacityIsAnimating() const { 484 return layer_animation_controller_->IsAnimatingProperty(Animation::Opacity); 485 } 486 487 bool Layer::OpacityCanAnimateOnImplThread() const { 488 return false; 489 } 490 491 void Layer::SetBlendMode(SkXfermode::Mode blend_mode) { 492 DCHECK(IsPropertyChangeAllowed()); 493 if (blend_mode_ == blend_mode) 494 return; 495 496 // Allowing only blend modes that are defined in the CSS Compositing standard: 497 // http://dev.w3.org/fxtf/compositing-1/#blending 498 switch (blend_mode) { 499 case SkXfermode::kSrcOver_Mode: 500 case SkXfermode::kScreen_Mode: 501 case SkXfermode::kOverlay_Mode: 502 case SkXfermode::kDarken_Mode: 503 case SkXfermode::kLighten_Mode: 504 case SkXfermode::kColorDodge_Mode: 505 case SkXfermode::kColorBurn_Mode: 506 case SkXfermode::kHardLight_Mode: 507 case SkXfermode::kSoftLight_Mode: 508 case SkXfermode::kDifference_Mode: 509 case SkXfermode::kExclusion_Mode: 510 case SkXfermode::kMultiply_Mode: 511 case SkXfermode::kHue_Mode: 512 case SkXfermode::kSaturation_Mode: 513 case SkXfermode::kColor_Mode: 514 case SkXfermode::kLuminosity_Mode: 515 // supported blend modes 516 break; 517 case SkXfermode::kClear_Mode: 518 case SkXfermode::kSrc_Mode: 519 case SkXfermode::kDst_Mode: 520 case SkXfermode::kDstOver_Mode: 521 case SkXfermode::kSrcIn_Mode: 522 case SkXfermode::kDstIn_Mode: 523 case SkXfermode::kSrcOut_Mode: 524 case SkXfermode::kDstOut_Mode: 525 case SkXfermode::kSrcATop_Mode: 526 case SkXfermode::kDstATop_Mode: 527 case SkXfermode::kXor_Mode: 528 case SkXfermode::kPlus_Mode: 529 case SkXfermode::kModulate_Mode: 530 // Porter Duff Compositing Operators are not yet supported 531 // http://dev.w3.org/fxtf/compositing-1/#porterduffcompositingoperators 532 NOTREACHED(); 533 return; 534 } 535 536 blend_mode_ = blend_mode; 537 SetNeedsCommit(); 538 } 539 540 void Layer::SetIsRootForIsolatedGroup(bool root) { 541 DCHECK(IsPropertyChangeAllowed()); 542 if (is_root_for_isolated_group_ == root) 543 return; 544 is_root_for_isolated_group_ = root; 545 SetNeedsCommit(); 546 } 547 548 void Layer::SetContentsOpaque(bool opaque) { 549 DCHECK(IsPropertyChangeAllowed()); 550 if (contents_opaque_ == opaque) 551 return; 552 contents_opaque_ = opaque; 553 SetNeedsCommit(); 554 } 555 556 void Layer::SetPosition(const gfx::PointF& position) { 557 DCHECK(IsPropertyChangeAllowed()); 558 if (position_ == position) 559 return; 560 position_ = position; 561 SetNeedsCommit(); 562 } 563 564 bool Layer::IsContainerForFixedPositionLayers() const { 565 if (!transform_.IsIdentityOrTranslation()) 566 return true; 567 if (parent_ && !parent_->transform_.IsIdentityOrTranslation()) 568 return true; 569 return is_container_for_fixed_position_layers_; 570 } 571 572 void Layer::SetTransform(const gfx::Transform& transform) { 573 DCHECK(IsPropertyChangeAllowed()); 574 if (transform_ == transform) 575 return; 576 transform_ = transform; 577 transform_is_invertible_ = transform.IsInvertible(); 578 SetNeedsCommit(); 579 } 580 581 void Layer::SetTransformOrigin(const gfx::Point3F& transform_origin) { 582 DCHECK(IsPropertyChangeAllowed()); 583 if (transform_origin_ == transform_origin) 584 return; 585 transform_origin_ = transform_origin; 586 SetNeedsCommit(); 587 } 588 589 bool Layer::TransformIsAnimating() const { 590 return layer_animation_controller_->IsAnimatingProperty(Animation::Transform); 591 } 592 593 void Layer::SetScrollParent(Layer* parent) { 594 DCHECK(IsPropertyChangeAllowed()); 595 if (scroll_parent_ == parent) 596 return; 597 598 if (scroll_parent_) 599 scroll_parent_->RemoveScrollChild(this); 600 601 scroll_parent_ = parent; 602 603 if (scroll_parent_) 604 scroll_parent_->AddScrollChild(this); 605 606 SetNeedsCommit(); 607 } 608 609 void Layer::AddScrollChild(Layer* child) { 610 if (!scroll_children_) 611 scroll_children_.reset(new std::set<Layer*>); 612 scroll_children_->insert(child); 613 SetNeedsCommit(); 614 } 615 616 void Layer::RemoveScrollChild(Layer* child) { 617 scroll_children_->erase(child); 618 if (scroll_children_->empty()) 619 scroll_children_.reset(); 620 SetNeedsCommit(); 621 } 622 623 void Layer::SetClipParent(Layer* ancestor) { 624 DCHECK(IsPropertyChangeAllowed()); 625 if (clip_parent_ == ancestor) 626 return; 627 628 if (clip_parent_) 629 clip_parent_->RemoveClipChild(this); 630 631 clip_parent_ = ancestor; 632 633 if (clip_parent_) 634 clip_parent_->AddClipChild(this); 635 636 SetNeedsCommit(); 637 } 638 639 void Layer::AddClipChild(Layer* child) { 640 if (!clip_children_) 641 clip_children_.reset(new std::set<Layer*>); 642 clip_children_->insert(child); 643 SetNeedsCommit(); 644 } 645 646 void Layer::RemoveClipChild(Layer* child) { 647 clip_children_->erase(child); 648 if (clip_children_->empty()) 649 clip_children_.reset(); 650 SetNeedsCommit(); 651 } 652 653 void Layer::SetScrollOffset(gfx::Vector2d scroll_offset) { 654 DCHECK(IsPropertyChangeAllowed()); 655 656 if (scroll_offset_ == scroll_offset) 657 return; 658 scroll_offset_ = scroll_offset; 659 SetNeedsCommit(); 660 } 661 662 void Layer::SetScrollOffsetFromImplSide(const gfx::Vector2d& scroll_offset) { 663 DCHECK(IsPropertyChangeAllowed()); 664 // This function only gets called during a BeginMainFrame, so there 665 // is no need to call SetNeedsUpdate here. 666 DCHECK(layer_tree_host_ && layer_tree_host_->CommitRequested()); 667 if (scroll_offset_ == scroll_offset) 668 return; 669 scroll_offset_ = scroll_offset; 670 SetNeedsPushProperties(); 671 if (!did_scroll_callback_.is_null()) 672 did_scroll_callback_.Run(); 673 // The callback could potentially change the layer structure: 674 // "this" may have been destroyed during the process. 675 } 676 677 void Layer::SetScrollClipLayerId(int clip_layer_id) { 678 DCHECK(IsPropertyChangeAllowed()); 679 if (scroll_clip_layer_id_ == clip_layer_id) 680 return; 681 scroll_clip_layer_id_ = clip_layer_id; 682 SetNeedsCommit(); 683 } 684 685 void Layer::SetUserScrollable(bool horizontal, bool vertical) { 686 DCHECK(IsPropertyChangeAllowed()); 687 if (user_scrollable_horizontal_ == horizontal && 688 user_scrollable_vertical_ == vertical) 689 return; 690 user_scrollable_horizontal_ = horizontal; 691 user_scrollable_vertical_ = vertical; 692 SetNeedsCommit(); 693 } 694 695 void Layer::SetShouldScrollOnMainThread(bool should_scroll_on_main_thread) { 696 DCHECK(IsPropertyChangeAllowed()); 697 if (should_scroll_on_main_thread_ == should_scroll_on_main_thread) 698 return; 699 should_scroll_on_main_thread_ = should_scroll_on_main_thread; 700 SetNeedsCommit(); 701 } 702 703 void Layer::SetHaveWheelEventHandlers(bool have_wheel_event_handlers) { 704 DCHECK(IsPropertyChangeAllowed()); 705 if (have_wheel_event_handlers_ == have_wheel_event_handlers) 706 return; 707 have_wheel_event_handlers_ = have_wheel_event_handlers; 708 SetNeedsCommit(); 709 } 710 711 void Layer::SetHaveScrollEventHandlers(bool have_scroll_event_handlers) { 712 DCHECK(IsPropertyChangeAllowed()); 713 if (have_scroll_event_handlers_ == have_scroll_event_handlers) 714 return; 715 have_scroll_event_handlers_ = have_scroll_event_handlers; 716 SetNeedsCommit(); 717 } 718 719 void Layer::SetNonFastScrollableRegion(const Region& region) { 720 DCHECK(IsPropertyChangeAllowed()); 721 if (non_fast_scrollable_region_ == region) 722 return; 723 non_fast_scrollable_region_ = region; 724 SetNeedsCommit(); 725 } 726 727 void Layer::SetTouchEventHandlerRegion(const Region& region) { 728 DCHECK(IsPropertyChangeAllowed()); 729 if (touch_event_handler_region_ == region) 730 return; 731 touch_event_handler_region_ = region; 732 SetNeedsCommit(); 733 } 734 735 void Layer::SetDrawCheckerboardForMissingTiles(bool checkerboard) { 736 DCHECK(IsPropertyChangeAllowed()); 737 if (draw_checkerboard_for_missing_tiles_ == checkerboard) 738 return; 739 draw_checkerboard_for_missing_tiles_ = checkerboard; 740 SetNeedsCommit(); 741 } 742 743 void Layer::SetForceRenderSurface(bool force) { 744 DCHECK(IsPropertyChangeAllowed()); 745 if (force_render_surface_ == force) 746 return; 747 force_render_surface_ = force; 748 SetNeedsCommit(); 749 } 750 751 void Layer::SetDoubleSided(bool double_sided) { 752 DCHECK(IsPropertyChangeAllowed()); 753 if (double_sided_ == double_sided) 754 return; 755 double_sided_ = double_sided; 756 SetNeedsCommit(); 757 } 758 759 void Layer::Set3dSortingContextId(int id) { 760 DCHECK(IsPropertyChangeAllowed()); 761 if (id == sorting_context_id_) 762 return; 763 sorting_context_id_ = id; 764 SetNeedsCommit(); 765 } 766 767 void Layer::SetShouldFlattenTransform(bool should_flatten) { 768 DCHECK(IsPropertyChangeAllowed()); 769 if (should_flatten_transform_ == should_flatten) 770 return; 771 should_flatten_transform_ = should_flatten; 772 SetNeedsCommit(); 773 } 774 775 void Layer::SetIsDrawable(bool is_drawable) { 776 DCHECK(IsPropertyChangeAllowed()); 777 if (is_drawable_ == is_drawable) 778 return; 779 780 is_drawable_ = is_drawable; 781 SetNeedsCommit(); 782 } 783 784 void Layer::SetHideLayerAndSubtree(bool hide) { 785 DCHECK(IsPropertyChangeAllowed()); 786 if (hide_layer_and_subtree_ == hide) 787 return; 788 789 hide_layer_and_subtree_ = hide; 790 SetNeedsCommit(); 791 } 792 793 void Layer::SetNeedsDisplayRect(const gfx::RectF& dirty_rect) { 794 if (dirty_rect.IsEmpty()) 795 return; 796 797 SetNeedsPushProperties(); 798 update_rect_.Union(dirty_rect); 799 800 if (DrawsContent()) 801 SetNeedsUpdate(); 802 } 803 804 bool Layer::DescendantIsFixedToContainerLayer() const { 805 for (size_t i = 0; i < children_.size(); ++i) { 806 if (children_[i]->position_constraint_.is_fixed_position() || 807 children_[i]->DescendantIsFixedToContainerLayer()) 808 return true; 809 } 810 return false; 811 } 812 813 void Layer::SetIsContainerForFixedPositionLayers(bool container) { 814 if (is_container_for_fixed_position_layers_ == container) 815 return; 816 is_container_for_fixed_position_layers_ = container; 817 818 if (layer_tree_host_ && layer_tree_host_->CommitRequested()) 819 return; 820 821 // Only request a commit if we have a fixed positioned descendant. 822 if (DescendantIsFixedToContainerLayer()) 823 SetNeedsCommit(); 824 } 825 826 void Layer::SetPositionConstraint(const LayerPositionConstraint& constraint) { 827 DCHECK(IsPropertyChangeAllowed()); 828 if (position_constraint_ == constraint) 829 return; 830 position_constraint_ = constraint; 831 SetNeedsCommit(); 832 } 833 834 static void RunCopyCallbackOnMainThread(scoped_ptr<CopyOutputRequest> request, 835 scoped_ptr<CopyOutputResult> result) { 836 request->SendResult(result.Pass()); 837 } 838 839 static void PostCopyCallbackToMainThread( 840 scoped_refptr<base::SingleThreadTaskRunner> main_thread_task_runner, 841 scoped_ptr<CopyOutputRequest> request, 842 scoped_ptr<CopyOutputResult> result) { 843 main_thread_task_runner->PostTask(FROM_HERE, 844 base::Bind(&RunCopyCallbackOnMainThread, 845 base::Passed(&request), 846 base::Passed(&result))); 847 } 848 849 void Layer::PushPropertiesTo(LayerImpl* layer) { 850 DCHECK(layer_tree_host_); 851 852 // If we did not SavePaintProperties() for the layer this frame, then push the 853 // real property values, not the paint property values. 854 bool use_paint_properties = paint_properties_.source_frame_number == 855 layer_tree_host_->source_frame_number(); 856 857 layer->SetTransformOrigin(transform_origin_); 858 layer->SetBackgroundColor(background_color_); 859 layer->SetBounds(use_paint_properties ? paint_properties_.bounds 860 : bounds_); 861 layer->SetContentBounds(content_bounds()); 862 layer->SetContentsScale(contents_scale_x(), contents_scale_y()); 863 864 bool is_tracing; 865 TRACE_EVENT_CATEGORY_GROUP_ENABLED(TRACE_DISABLED_BY_DEFAULT("cc.debug"), 866 &is_tracing); 867 if (is_tracing) 868 layer->SetDebugInfo(TakeDebugInfo()); 869 870 layer->SetDoubleSided(double_sided_); 871 layer->SetDrawCheckerboardForMissingTiles( 872 draw_checkerboard_for_missing_tiles_); 873 layer->SetForceRenderSurface(force_render_surface_); 874 layer->SetDrawsContent(DrawsContent()); 875 layer->SetHideLayerAndSubtree(hide_layer_and_subtree_); 876 if (!layer->FilterIsAnimatingOnImplOnly() && !FilterIsAnimating()) 877 layer->SetFilters(filters_); 878 DCHECK(!(FilterIsAnimating() && layer->FilterIsAnimatingOnImplOnly())); 879 layer->SetBackgroundFilters(background_filters()); 880 layer->SetMasksToBounds(masks_to_bounds_); 881 layer->SetShouldScrollOnMainThread(should_scroll_on_main_thread_); 882 layer->SetHaveWheelEventHandlers(have_wheel_event_handlers_); 883 layer->SetHaveScrollEventHandlers(have_scroll_event_handlers_); 884 layer->SetNonFastScrollableRegion(non_fast_scrollable_region_); 885 layer->SetTouchEventHandlerRegion(touch_event_handler_region_); 886 layer->SetContentsOpaque(contents_opaque_); 887 if (!layer->OpacityIsAnimatingOnImplOnly() && !OpacityIsAnimating()) 888 layer->SetOpacity(opacity_); 889 DCHECK(!(OpacityIsAnimating() && layer->OpacityIsAnimatingOnImplOnly())); 890 layer->SetBlendMode(blend_mode_); 891 layer->SetIsRootForIsolatedGroup(is_root_for_isolated_group_); 892 layer->SetPosition(position_); 893 layer->SetIsContainerForFixedPositionLayers( 894 IsContainerForFixedPositionLayers()); 895 layer->SetPositionConstraint(position_constraint_); 896 layer->SetShouldFlattenTransform(should_flatten_transform_); 897 layer->SetUseParentBackfaceVisibility(use_parent_backface_visibility_); 898 if (!layer->TransformIsAnimatingOnImplOnly() && !TransformIsAnimating()) 899 layer->SetTransformAndInvertibility(transform_, transform_is_invertible_); 900 DCHECK(!(TransformIsAnimating() && layer->TransformIsAnimatingOnImplOnly())); 901 layer->Set3dSortingContextId(sorting_context_id_); 902 903 layer->SetScrollClipLayer(scroll_clip_layer_id_); 904 layer->set_user_scrollable_horizontal(user_scrollable_horizontal_); 905 layer->set_user_scrollable_vertical(user_scrollable_vertical_); 906 907 LayerImpl* scroll_parent = NULL; 908 if (scroll_parent_) { 909 scroll_parent = layer->layer_tree_impl()->LayerById(scroll_parent_->id()); 910 DCHECK(scroll_parent); 911 } 912 913 layer->SetScrollParent(scroll_parent); 914 if (scroll_children_) { 915 std::set<LayerImpl*>* scroll_children = new std::set<LayerImpl*>; 916 for (std::set<Layer*>::iterator it = scroll_children_->begin(); 917 it != scroll_children_->end(); 918 ++it) { 919 DCHECK_EQ((*it)->scroll_parent(), this); 920 LayerImpl* scroll_child = 921 layer->layer_tree_impl()->LayerById((*it)->id()); 922 DCHECK(scroll_child); 923 scroll_children->insert(scroll_child); 924 } 925 layer->SetScrollChildren(scroll_children); 926 } else { 927 layer->SetScrollChildren(NULL); 928 } 929 930 LayerImpl* clip_parent = NULL; 931 if (clip_parent_) { 932 clip_parent = 933 layer->layer_tree_impl()->LayerById(clip_parent_->id()); 934 DCHECK(clip_parent); 935 } 936 937 layer->SetClipParent(clip_parent); 938 if (clip_children_) { 939 std::set<LayerImpl*>* clip_children = new std::set<LayerImpl*>; 940 for (std::set<Layer*>::iterator it = clip_children_->begin(); 941 it != clip_children_->end(); ++it) { 942 DCHECK_EQ((*it)->clip_parent(), this); 943 LayerImpl* clip_child = layer->layer_tree_impl()->LayerById((*it)->id()); 944 DCHECK(clip_child); 945 clip_children->insert(clip_child); 946 } 947 layer->SetClipChildren(clip_children); 948 } else { 949 layer->SetClipChildren(NULL); 950 } 951 952 // Adjust the scroll delta to be just the scrolls that have happened since 953 // the BeginMainFrame was sent. This happens for impl-side painting 954 // in LayerImpl::ApplyScrollDeltasSinceBeginMainFrame in a separate tree walk. 955 if (layer->layer_tree_impl()->settings().impl_side_painting) { 956 layer->SetScrollOffset(scroll_offset_); 957 } else { 958 layer->SetScrollOffsetAndDelta( 959 scroll_offset_, layer->ScrollDelta() - layer->sent_scroll_delta()); 960 layer->SetSentScrollDelta(gfx::Vector2d()); 961 } 962 963 // Wrap the copy_requests_ in a PostTask to the main thread. 964 ScopedPtrVector<CopyOutputRequest> main_thread_copy_requests; 965 for (ScopedPtrVector<CopyOutputRequest>::iterator it = copy_requests_.begin(); 966 it != copy_requests_.end(); 967 ++it) { 968 scoped_refptr<base::SingleThreadTaskRunner> main_thread_task_runner = 969 layer_tree_host()->proxy()->MainThreadTaskRunner(); 970 scoped_ptr<CopyOutputRequest> original_request = copy_requests_.take(it); 971 const CopyOutputRequest& original_request_ref = *original_request; 972 scoped_ptr<CopyOutputRequest> main_thread_request = 973 CopyOutputRequest::CreateRelayRequest( 974 original_request_ref, 975 base::Bind(&PostCopyCallbackToMainThread, 976 main_thread_task_runner, 977 base::Passed(&original_request))); 978 main_thread_copy_requests.push_back(main_thread_request.Pass()); 979 } 980 copy_requests_.clear(); 981 layer->PassCopyRequests(&main_thread_copy_requests); 982 983 // If the main thread commits multiple times before the impl thread actually 984 // draws, then damage tracking will become incorrect if we simply clobber the 985 // update_rect here. The LayerImpl's update_rect needs to accumulate (i.e. 986 // union) any update changes that have occurred on the main thread. 987 update_rect_.Union(layer->update_rect()); 988 layer->SetUpdateRect(update_rect_); 989 990 layer->SetStackingOrderChanged(stacking_order_changed_); 991 992 layer_animation_controller_->PushAnimationUpdatesTo( 993 layer->layer_animation_controller()); 994 995 // Reset any state that should be cleared for the next update. 996 stacking_order_changed_ = false; 997 update_rect_ = gfx::RectF(); 998 999 needs_push_properties_ = false; 1000 num_dependents_need_push_properties_ = 0; 1001 } 1002 1003 scoped_ptr<LayerImpl> Layer::CreateLayerImpl(LayerTreeImpl* tree_impl) { 1004 return LayerImpl::Create(tree_impl, layer_id_); 1005 } 1006 1007 bool Layer::DrawsContent() const { 1008 return is_drawable_; 1009 } 1010 1011 void Layer::SavePaintProperties() { 1012 DCHECK(layer_tree_host_); 1013 1014 // TODO(reveman): Save all layer properties that we depend on not 1015 // changing until PushProperties() has been called. crbug.com/231016 1016 paint_properties_.bounds = bounds_; 1017 paint_properties_.source_frame_number = 1018 layer_tree_host_->source_frame_number(); 1019 } 1020 1021 bool Layer::Update(ResourceUpdateQueue* queue, 1022 const OcclusionTracker<Layer>* occlusion) { 1023 DCHECK(layer_tree_host_); 1024 DCHECK_EQ(layer_tree_host_->source_frame_number(), 1025 paint_properties_.source_frame_number) << 1026 "SavePaintProperties must be called for any layer that is painted."; 1027 return false; 1028 } 1029 1030 bool Layer::NeedMoreUpdates() { 1031 return false; 1032 } 1033 1034 bool Layer::IsSuitableForGpuRasterization() const { 1035 return true; 1036 } 1037 1038 scoped_refptr<base::debug::ConvertableToTraceFormat> Layer::TakeDebugInfo() { 1039 if (client_) 1040 return client_->TakeDebugInfo(); 1041 else 1042 return NULL; 1043 } 1044 1045 void Layer::CreateRenderSurface() { 1046 DCHECK(!draw_properties_.render_surface); 1047 draw_properties_.render_surface = make_scoped_ptr(new RenderSurface(this)); 1048 draw_properties_.render_target = this; 1049 } 1050 1051 void Layer::ClearRenderSurface() { 1052 draw_properties_.render_surface.reset(); 1053 } 1054 1055 void Layer::ClearRenderSurfaceLayerList() { 1056 if (draw_properties_.render_surface) 1057 draw_properties_.render_surface->layer_list().clear(); 1058 } 1059 1060 gfx::Vector2dF Layer::ScrollOffsetForAnimation() const { 1061 return TotalScrollOffset(); 1062 } 1063 1064 // On<Property>Animated is called due to an ongoing accelerated animation. 1065 // Since this animation is also being run on the compositor thread, there 1066 // is no need to request a commit to push this value over, so the value is 1067 // set directly rather than by calling Set<Property>. 1068 void Layer::OnFilterAnimated(const FilterOperations& filters) { 1069 filters_ = filters; 1070 } 1071 1072 void Layer::OnOpacityAnimated(float opacity) { 1073 opacity_ = opacity; 1074 } 1075 1076 void Layer::OnTransformAnimated(const gfx::Transform& transform) { 1077 if (transform_ == transform) 1078 return; 1079 transform_ = transform; 1080 transform_is_invertible_ = transform.IsInvertible(); 1081 } 1082 1083 void Layer::OnScrollOffsetAnimated(const gfx::Vector2dF& scroll_offset) { 1084 // Do nothing. Scroll deltas will be sent from the compositor thread back 1085 // to the main thread in the same manner as during non-animated 1086 // compositor-driven scrolling. 1087 } 1088 1089 void Layer::OnAnimationWaitingForDeletion() { 1090 // Animations are only deleted during PushProperties. 1091 SetNeedsPushProperties(); 1092 } 1093 1094 bool Layer::IsActive() const { 1095 return true; 1096 } 1097 1098 bool Layer::AddAnimation(scoped_ptr <Animation> animation) { 1099 if (!layer_animation_controller_->animation_registrar()) 1100 return false; 1101 1102 UMA_HISTOGRAM_BOOLEAN("Renderer.AnimationAddedToOrphanLayer", 1103 !layer_tree_host_); 1104 layer_animation_controller_->AddAnimation(animation.Pass()); 1105 SetNeedsCommit(); 1106 return true; 1107 } 1108 1109 void Layer::PauseAnimation(int animation_id, double time_offset) { 1110 layer_animation_controller_->PauseAnimation( 1111 animation_id, base::TimeDelta::FromSecondsD(time_offset)); 1112 SetNeedsCommit(); 1113 } 1114 1115 void Layer::RemoveAnimation(int animation_id) { 1116 layer_animation_controller_->RemoveAnimation(animation_id); 1117 SetNeedsCommit(); 1118 } 1119 1120 void Layer::SetLayerAnimationControllerForTest( 1121 scoped_refptr<LayerAnimationController> controller) { 1122 layer_animation_controller_->RemoveValueObserver(this); 1123 layer_animation_controller_ = controller; 1124 layer_animation_controller_->AddValueObserver(this); 1125 SetNeedsCommit(); 1126 } 1127 1128 bool Layer::HasActiveAnimation() const { 1129 return layer_animation_controller_->HasActiveAnimation(); 1130 } 1131 1132 void Layer::AddLayerAnimationEventObserver( 1133 LayerAnimationEventObserver* animation_observer) { 1134 layer_animation_controller_->AddEventObserver(animation_observer); 1135 } 1136 1137 void Layer::RemoveLayerAnimationEventObserver( 1138 LayerAnimationEventObserver* animation_observer) { 1139 layer_animation_controller_->RemoveEventObserver(animation_observer); 1140 } 1141 1142 Region Layer::VisibleContentOpaqueRegion() const { 1143 if (contents_opaque()) 1144 return visible_content_rect(); 1145 return Region(); 1146 } 1147 1148 ScrollbarLayerInterface* Layer::ToScrollbarLayer() { 1149 return NULL; 1150 } 1151 1152 RenderingStatsInstrumentation* Layer::rendering_stats_instrumentation() const { 1153 return layer_tree_host_->rendering_stats_instrumentation(); 1154 } 1155 1156 bool Layer::SupportsLCDText() const { 1157 return false; 1158 } 1159 1160 void Layer::RemoveFromScrollTree() { 1161 if (scroll_children_.get()) { 1162 std::set<Layer*> copy = *scroll_children_; 1163 for (std::set<Layer*>::iterator it = copy.begin(); it != copy.end(); ++it) 1164 (*it)->SetScrollParent(NULL); 1165 } 1166 1167 DCHECK(!scroll_children_); 1168 SetScrollParent(NULL); 1169 } 1170 1171 void Layer::RemoveFromClipTree() { 1172 if (clip_children_.get()) { 1173 std::set<Layer*> copy = *clip_children_; 1174 for (std::set<Layer*>::iterator it = copy.begin(); it != copy.end(); ++it) 1175 (*it)->SetClipParent(NULL); 1176 } 1177 1178 DCHECK(!clip_children_); 1179 SetClipParent(NULL); 1180 } 1181 1182 void Layer::RunMicroBenchmark(MicroBenchmark* benchmark) { 1183 benchmark->RunOnLayer(this); 1184 } 1185 } // namespace cc 1186