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/location.h" 10 #include "base/metrics/histogram.h" 11 #include "base/single_thread_task_runner.h" 12 #include "cc/animation/animation.h" 13 #include "cc/animation/animation_events.h" 14 #include "cc/animation/layer_animation_controller.h" 15 #include "cc/layers/layer_impl.h" 16 #include "cc/output/copy_output_request.h" 17 #include "cc/output/copy_output_result.h" 18 #include "cc/trees/layer_tree_host.h" 19 #include "cc/trees/layer_tree_impl.h" 20 #include "third_party/skia/include/core/SkImageFilter.h" 21 #include "ui/gfx/rect_conversions.h" 22 23 namespace cc { 24 25 static int s_next_layer_id = 1; 26 27 scoped_refptr<Layer> Layer::Create() { 28 return make_scoped_refptr(new Layer()); 29 } 30 31 Layer::Layer() 32 : needs_push_properties_(false), 33 num_dependents_need_push_properties_(false), 34 stacking_order_changed_(false), 35 layer_id_(s_next_layer_id++), 36 ignore_set_needs_commit_(false), 37 parent_(NULL), 38 layer_tree_host_(NULL), 39 scrollable_(false), 40 should_scroll_on_main_thread_(false), 41 have_wheel_event_handlers_(false), 42 anchor_point_(0.5f, 0.5f), 43 background_color_(0), 44 compositing_reasons_(kCompositingReasonUnknown), 45 opacity_(1.f), 46 anchor_point_z_(0.f), 47 is_container_for_fixed_position_layers_(false), 48 is_drawable_(false), 49 hide_layer_and_subtree_(false), 50 masks_to_bounds_(false), 51 contents_opaque_(false), 52 double_sided_(true), 53 preserves_3d_(false), 54 use_parent_backface_visibility_(false), 55 draw_checkerboard_for_missing_tiles_(false), 56 force_render_surface_(false), 57 replica_layer_(NULL), 58 raster_scale_(0.f) { 59 if (layer_id_ < 0) { 60 s_next_layer_id = 1; 61 layer_id_ = s_next_layer_id++; 62 } 63 64 layer_animation_controller_ = LayerAnimationController::Create(layer_id_); 65 layer_animation_controller_->AddValueObserver(this); 66 } 67 68 Layer::~Layer() { 69 // Our parent should be holding a reference to us so there should be no 70 // way for us to be destroyed while we still have a parent. 71 DCHECK(!parent()); 72 // Similarly we shouldn't have a layer tree host since it also keeps a 73 // reference to us. 74 DCHECK(!layer_tree_host()); 75 76 layer_animation_controller_->RemoveValueObserver(this); 77 78 // Remove the parent reference from all children and dependents. 79 RemoveAllChildren(); 80 if (mask_layer_.get()) { 81 DCHECK_EQ(this, mask_layer_->parent()); 82 mask_layer_->RemoveFromParent(); 83 } 84 if (replica_layer_.get()) { 85 DCHECK_EQ(this, replica_layer_->parent()); 86 replica_layer_->RemoveFromParent(); 87 } 88 } 89 90 void Layer::SetLayerTreeHost(LayerTreeHost* host) { 91 if (layer_tree_host_ == host) 92 return; 93 94 layer_tree_host_ = host; 95 96 // When changing hosts, the layer needs to commit its properties to the impl 97 // side for the new host. 98 SetNeedsPushProperties(); 99 100 for (size_t i = 0; i < children_.size(); ++i) 101 children_[i]->SetLayerTreeHost(host); 102 103 if (mask_layer_.get()) 104 mask_layer_->SetLayerTreeHost(host); 105 if (replica_layer_.get()) 106 replica_layer_->SetLayerTreeHost(host); 107 108 if (host) { 109 layer_animation_controller_->SetAnimationRegistrar( 110 host->animation_registrar()); 111 112 if (host->settings().layer_transforms_should_scale_layer_contents) 113 reset_raster_scale_to_unknown(); 114 } 115 116 if (host && layer_animation_controller_->has_any_animation()) 117 host->SetNeedsCommit(); 118 if (host && 119 (!filters_.IsEmpty() || !background_filters_.IsEmpty() || filter_)) 120 layer_tree_host_->set_needs_filter_context(); 121 } 122 123 void Layer::SetNeedsUpdate() { 124 if (layer_tree_host_ && !ignore_set_needs_commit_) 125 layer_tree_host_->SetNeedsUpdateLayers(); 126 } 127 128 void Layer::SetNeedsCommit() { 129 if (!layer_tree_host_) 130 return; 131 132 SetNeedsPushProperties(); 133 134 if (ignore_set_needs_commit_) 135 return; 136 137 layer_tree_host_->SetNeedsCommit(); 138 } 139 140 void Layer::SetNeedsFullTreeSync() { 141 if (!layer_tree_host_) 142 return; 143 144 layer_tree_host_->SetNeedsFullTreeSync(); 145 } 146 147 bool Layer::IsPropertyChangeAllowed() const { 148 if (!layer_tree_host_) 149 return true; 150 151 if (!layer_tree_host_->settings().strict_layer_property_change_checking) 152 return true; 153 154 return !layer_tree_host_->in_paint_layer_contents(); 155 } 156 157 void Layer::SetNeedsPushProperties() { 158 if (needs_push_properties_) 159 return; 160 if (!parent_should_know_need_push_properties() && parent_) 161 parent_->AddDependentNeedsPushProperties(); 162 needs_push_properties_ = true; 163 } 164 165 void Layer::AddDependentNeedsPushProperties() { 166 DCHECK_GE(num_dependents_need_push_properties_, 0); 167 168 if (!parent_should_know_need_push_properties() && parent_) 169 parent_->AddDependentNeedsPushProperties(); 170 171 num_dependents_need_push_properties_++; 172 } 173 174 void Layer::RemoveDependentNeedsPushProperties() { 175 num_dependents_need_push_properties_--; 176 DCHECK_GE(num_dependents_need_push_properties_, 0); 177 178 if (!parent_should_know_need_push_properties() && parent_) 179 parent_->RemoveDependentNeedsPushProperties(); 180 } 181 182 gfx::Rect Layer::LayerRectToContentRect(const gfx::RectF& layer_rect) const { 183 gfx::RectF content_rect = 184 gfx::ScaleRect(layer_rect, contents_scale_x(), contents_scale_y()); 185 // Intersect with content rect to avoid the extra pixel because for some 186 // values x and y, ceil((x / y) * y) may be x + 1. 187 content_rect.Intersect(gfx::Rect(content_bounds())); 188 return gfx::ToEnclosingRect(content_rect); 189 } 190 191 bool Layer::BlocksPendingCommit() const { 192 return false; 193 } 194 195 bool Layer::CanClipSelf() const { 196 return false; 197 } 198 199 bool Layer::BlocksPendingCommitRecursive() const { 200 if (BlocksPendingCommit()) 201 return true; 202 if (mask_layer() && mask_layer()->BlocksPendingCommitRecursive()) 203 return true; 204 if (replica_layer() && replica_layer()->BlocksPendingCommitRecursive()) 205 return true; 206 for (size_t i = 0; i < children_.size(); ++i) { 207 if (children_[i]->BlocksPendingCommitRecursive()) 208 return true; 209 } 210 return false; 211 } 212 213 void Layer::SetParent(Layer* layer) { 214 DCHECK(!layer || !layer->HasAncestor(this)); 215 216 if (parent_should_know_need_push_properties()) { 217 if (parent_) 218 parent_->RemoveDependentNeedsPushProperties(); 219 if (layer) 220 layer->AddDependentNeedsPushProperties(); 221 } 222 223 parent_ = layer; 224 SetLayerTreeHost(parent_ ? parent_->layer_tree_host() : NULL); 225 226 if (!layer_tree_host_) 227 return; 228 const LayerTreeSettings& settings = layer_tree_host_->settings(); 229 if (!settings.layer_transforms_should_scale_layer_contents) 230 return; 231 232 reset_raster_scale_to_unknown(); 233 if (mask_layer_.get()) 234 mask_layer_->reset_raster_scale_to_unknown(); 235 if (replica_layer_.get() && replica_layer_->mask_layer_.get()) 236 replica_layer_->mask_layer_->reset_raster_scale_to_unknown(); 237 } 238 239 void Layer::AddChild(scoped_refptr<Layer> child) { 240 InsertChild(child, children_.size()); 241 } 242 243 void Layer::InsertChild(scoped_refptr<Layer> child, size_t index) { 244 DCHECK(IsPropertyChangeAllowed()); 245 child->RemoveFromParent(); 246 child->SetParent(this); 247 child->stacking_order_changed_ = true; 248 249 index = std::min(index, children_.size()); 250 children_.insert(children_.begin() + index, child); 251 SetNeedsFullTreeSync(); 252 } 253 254 void Layer::RemoveFromParent() { 255 DCHECK(IsPropertyChangeAllowed()); 256 if (parent_) 257 parent_->RemoveChildOrDependent(this); 258 } 259 260 void Layer::RemoveChildOrDependent(Layer* child) { 261 if (mask_layer_.get() == child) { 262 mask_layer_->SetParent(NULL); 263 mask_layer_ = NULL; 264 SetNeedsFullTreeSync(); 265 return; 266 } 267 if (replica_layer_.get() == child) { 268 replica_layer_->SetParent(NULL); 269 replica_layer_ = NULL; 270 SetNeedsFullTreeSync(); 271 return; 272 } 273 274 for (LayerList::iterator iter = children_.begin(); 275 iter != children_.end(); 276 ++iter) { 277 if (iter->get() != child) 278 continue; 279 280 child->SetParent(NULL); 281 children_.erase(iter); 282 SetNeedsFullTreeSync(); 283 return; 284 } 285 } 286 287 void Layer::ReplaceChild(Layer* reference, scoped_refptr<Layer> new_layer) { 288 DCHECK(reference); 289 DCHECK_EQ(reference->parent(), this); 290 DCHECK(IsPropertyChangeAllowed()); 291 292 if (reference == new_layer.get()) 293 return; 294 295 int reference_index = IndexOfChild(reference); 296 if (reference_index == -1) { 297 NOTREACHED(); 298 return; 299 } 300 301 reference->RemoveFromParent(); 302 303 if (new_layer.get()) { 304 new_layer->RemoveFromParent(); 305 InsertChild(new_layer, reference_index); 306 } 307 } 308 309 int Layer::IndexOfChild(const Layer* reference) { 310 for (size_t i = 0; i < children_.size(); ++i) { 311 if (children_[i].get() == reference) 312 return i; 313 } 314 return -1; 315 } 316 317 void Layer::SetBounds(gfx::Size size) { 318 DCHECK(IsPropertyChangeAllowed()); 319 if (bounds() == size) 320 return; 321 322 bounds_ = size; 323 SetNeedsCommit(); 324 } 325 326 Layer* Layer::RootLayer() { 327 Layer* layer = this; 328 while (layer->parent()) 329 layer = layer->parent(); 330 return layer; 331 } 332 333 void Layer::RemoveAllChildren() { 334 DCHECK(IsPropertyChangeAllowed()); 335 while (children_.size()) { 336 Layer* layer = children_[0].get(); 337 DCHECK_EQ(this, layer->parent()); 338 layer->RemoveFromParent(); 339 } 340 } 341 342 void Layer::SetChildren(const LayerList& children) { 343 DCHECK(IsPropertyChangeAllowed()); 344 if (children == children_) 345 return; 346 347 RemoveAllChildren(); 348 for (size_t i = 0; i < children.size(); ++i) 349 AddChild(children[i]); 350 } 351 352 bool Layer::HasAncestor(const Layer* ancestor) const { 353 for (const Layer* layer = parent(); layer; layer = layer->parent()) { 354 if (layer == ancestor) 355 return true; 356 } 357 return false; 358 } 359 360 void Layer::RequestCopyOfOutput( 361 scoped_ptr<CopyOutputRequest> request) { 362 DCHECK(IsPropertyChangeAllowed()); 363 if (request->IsEmpty()) 364 return; 365 copy_requests_.push_back(request.Pass()); 366 SetNeedsCommit(); 367 } 368 369 void Layer::SetAnchorPoint(gfx::PointF anchor_point) { 370 DCHECK(IsPropertyChangeAllowed()); 371 if (anchor_point_ == anchor_point) 372 return; 373 anchor_point_ = anchor_point; 374 SetNeedsCommit(); 375 } 376 377 void Layer::SetAnchorPointZ(float anchor_point_z) { 378 DCHECK(IsPropertyChangeAllowed()); 379 if (anchor_point_z_ == anchor_point_z) 380 return; 381 anchor_point_z_ = anchor_point_z; 382 SetNeedsCommit(); 383 } 384 385 void Layer::SetBackgroundColor(SkColor background_color) { 386 DCHECK(IsPropertyChangeAllowed()); 387 if (background_color_ == background_color) 388 return; 389 background_color_ = background_color; 390 SetNeedsCommit(); 391 } 392 393 SkColor Layer::SafeOpaqueBackgroundColor() const { 394 SkColor color = background_color(); 395 if (SkColorGetA(color) == 255 && !contents_opaque()) { 396 color = SK_ColorTRANSPARENT; 397 } else if (SkColorGetA(color) != 255 && contents_opaque()) { 398 for (const Layer* layer = parent(); layer; 399 layer = layer->parent()) { 400 color = layer->background_color(); 401 if (SkColorGetA(color) == 255) 402 break; 403 } 404 if (SkColorGetA(color) != 255) 405 color = layer_tree_host_->background_color(); 406 if (SkColorGetA(color) != 255) 407 color = SkColorSetA(color, 255); 408 } 409 return color; 410 } 411 412 void Layer::CalculateContentsScale( 413 float ideal_contents_scale, 414 float device_scale_factor, 415 float page_scale_factor, 416 bool animating_transform_to_screen, 417 float* contents_scale_x, 418 float* contents_scale_y, 419 gfx::Size* content_bounds) { 420 DCHECK(layer_tree_host_); 421 422 *contents_scale_x = 1; 423 *contents_scale_y = 1; 424 *content_bounds = bounds(); 425 } 426 427 void Layer::SetMasksToBounds(bool masks_to_bounds) { 428 DCHECK(IsPropertyChangeAllowed()); 429 if (masks_to_bounds_ == masks_to_bounds) 430 return; 431 masks_to_bounds_ = masks_to_bounds; 432 SetNeedsCommit(); 433 } 434 435 void Layer::SetMaskLayer(Layer* mask_layer) { 436 DCHECK(IsPropertyChangeAllowed()); 437 if (mask_layer_.get() == mask_layer) 438 return; 439 if (mask_layer_.get()) { 440 DCHECK_EQ(this, mask_layer_->parent()); 441 mask_layer_->RemoveFromParent(); 442 } 443 mask_layer_ = mask_layer; 444 if (mask_layer_.get()) { 445 DCHECK(!mask_layer_->parent()); 446 mask_layer_->RemoveFromParent(); 447 mask_layer_->SetParent(this); 448 mask_layer_->SetIsMask(true); 449 } 450 SetNeedsFullTreeSync(); 451 } 452 453 void Layer::SetReplicaLayer(Layer* layer) { 454 DCHECK(IsPropertyChangeAllowed()); 455 if (replica_layer_.get() == layer) 456 return; 457 if (replica_layer_.get()) { 458 DCHECK_EQ(this, replica_layer_->parent()); 459 replica_layer_->RemoveFromParent(); 460 } 461 replica_layer_ = layer; 462 if (replica_layer_.get()) { 463 DCHECK(!replica_layer_->parent()); 464 replica_layer_->RemoveFromParent(); 465 replica_layer_->SetParent(this); 466 } 467 SetNeedsFullTreeSync(); 468 } 469 470 void Layer::SetFilters(const FilterOperations& filters) { 471 DCHECK(IsPropertyChangeAllowed()); 472 if (filters_ == filters) 473 return; 474 DCHECK(!filter_); 475 filters_ = filters; 476 SetNeedsCommit(); 477 if (!filters.IsEmpty() && layer_tree_host_) 478 layer_tree_host_->set_needs_filter_context(); 479 } 480 481 void Layer::SetFilter(const skia::RefPtr<SkImageFilter>& filter) { 482 DCHECK(IsPropertyChangeAllowed()); 483 if (filter_.get() == filter.get()) 484 return; 485 DCHECK(filters_.IsEmpty()); 486 filter_ = filter; 487 SetNeedsCommit(); 488 if (filter && layer_tree_host_) 489 layer_tree_host_->set_needs_filter_context(); 490 } 491 492 void Layer::SetBackgroundFilters(const FilterOperations& filters) { 493 DCHECK(IsPropertyChangeAllowed()); 494 if (background_filters_ == filters) 495 return; 496 background_filters_ = filters; 497 SetNeedsCommit(); 498 if (!filters.IsEmpty() && layer_tree_host_) 499 layer_tree_host_->set_needs_filter_context(); 500 } 501 502 void Layer::SetOpacity(float opacity) { 503 DCHECK(IsPropertyChangeAllowed()); 504 if (opacity_ == opacity) 505 return; 506 opacity_ = opacity; 507 SetNeedsCommit(); 508 } 509 510 bool Layer::OpacityIsAnimating() const { 511 return layer_animation_controller_->IsAnimatingProperty(Animation::Opacity); 512 } 513 514 bool Layer::OpacityCanAnimateOnImplThread() const { 515 return false; 516 } 517 518 void Layer::SetContentsOpaque(bool opaque) { 519 DCHECK(IsPropertyChangeAllowed()); 520 if (contents_opaque_ == opaque) 521 return; 522 contents_opaque_ = opaque; 523 SetNeedsCommit(); 524 } 525 526 void Layer::SetPosition(gfx::PointF position) { 527 DCHECK(IsPropertyChangeAllowed()); 528 if (position_ == position) 529 return; 530 position_ = position; 531 SetNeedsCommit(); 532 } 533 534 bool Layer::IsContainerForFixedPositionLayers() const { 535 if (!transform_.IsIdentityOrTranslation()) 536 return true; 537 if (parent_ && !parent_->sublayer_transform_.IsIdentityOrTranslation()) 538 return true; 539 return is_container_for_fixed_position_layers_; 540 } 541 542 void Layer::SetSublayerTransform(const gfx::Transform& sublayer_transform) { 543 DCHECK(IsPropertyChangeAllowed()); 544 if (sublayer_transform_ == sublayer_transform) 545 return; 546 sublayer_transform_ = sublayer_transform; 547 SetNeedsCommit(); 548 } 549 550 void Layer::SetTransform(const gfx::Transform& transform) { 551 DCHECK(IsPropertyChangeAllowed()); 552 if (transform_ == transform) 553 return; 554 transform_ = transform; 555 SetNeedsCommit(); 556 } 557 558 bool Layer::TransformIsAnimating() const { 559 return layer_animation_controller_->IsAnimatingProperty(Animation::Transform); 560 } 561 562 void Layer::SetScrollOffset(gfx::Vector2d scroll_offset) { 563 DCHECK(IsPropertyChangeAllowed()); 564 if (scroll_offset_ == scroll_offset) 565 return; 566 scroll_offset_ = scroll_offset; 567 SetNeedsCommit(); 568 } 569 570 void Layer::SetScrollOffsetFromImplSide(gfx::Vector2d scroll_offset) { 571 DCHECK(IsPropertyChangeAllowed()); 572 // This function only gets called during a begin frame, so there 573 // is no need to call SetNeedsUpdate here. 574 DCHECK(layer_tree_host_ && layer_tree_host_->CommitRequested()); 575 if (scroll_offset_ == scroll_offset) 576 return; 577 scroll_offset_ = scroll_offset; 578 SetNeedsPushProperties(); 579 if (!did_scroll_callback_.is_null()) 580 did_scroll_callback_.Run(); 581 // The callback could potentially change the layer structure: 582 // "this" may have been destroyed during the process. 583 } 584 585 void Layer::SetMaxScrollOffset(gfx::Vector2d max_scroll_offset) { 586 DCHECK(IsPropertyChangeAllowed()); 587 if (max_scroll_offset_ == max_scroll_offset) 588 return; 589 max_scroll_offset_ = max_scroll_offset; 590 SetNeedsCommit(); 591 } 592 593 void Layer::SetScrollable(bool scrollable) { 594 DCHECK(IsPropertyChangeAllowed()); 595 if (scrollable_ == scrollable) 596 return; 597 scrollable_ = scrollable; 598 SetNeedsCommit(); 599 } 600 601 void Layer::SetShouldScrollOnMainThread(bool should_scroll_on_main_thread) { 602 DCHECK(IsPropertyChangeAllowed()); 603 if (should_scroll_on_main_thread_ == should_scroll_on_main_thread) 604 return; 605 should_scroll_on_main_thread_ = should_scroll_on_main_thread; 606 SetNeedsCommit(); 607 } 608 609 void Layer::SetHaveWheelEventHandlers(bool have_wheel_event_handlers) { 610 DCHECK(IsPropertyChangeAllowed()); 611 if (have_wheel_event_handlers_ == have_wheel_event_handlers) 612 return; 613 have_wheel_event_handlers_ = have_wheel_event_handlers; 614 SetNeedsCommit(); 615 } 616 617 void Layer::SetNonFastScrollableRegion(const Region& region) { 618 DCHECK(IsPropertyChangeAllowed()); 619 if (non_fast_scrollable_region_ == region) 620 return; 621 non_fast_scrollable_region_ = region; 622 SetNeedsCommit(); 623 } 624 625 void Layer::SetTouchEventHandlerRegion(const Region& region) { 626 DCHECK(IsPropertyChangeAllowed()); 627 if (touch_event_handler_region_ == region) 628 return; 629 touch_event_handler_region_ = region; 630 SetNeedsCommit(); 631 } 632 633 void Layer::SetDrawCheckerboardForMissingTiles(bool checkerboard) { 634 DCHECK(IsPropertyChangeAllowed()); 635 if (draw_checkerboard_for_missing_tiles_ == checkerboard) 636 return; 637 draw_checkerboard_for_missing_tiles_ = checkerboard; 638 SetNeedsCommit(); 639 } 640 641 void Layer::SetForceRenderSurface(bool force) { 642 DCHECK(IsPropertyChangeAllowed()); 643 if (force_render_surface_ == force) 644 return; 645 force_render_surface_ = force; 646 SetNeedsCommit(); 647 } 648 649 void Layer::SetDoubleSided(bool double_sided) { 650 DCHECK(IsPropertyChangeAllowed()); 651 if (double_sided_ == double_sided) 652 return; 653 double_sided_ = double_sided; 654 SetNeedsCommit(); 655 } 656 657 void Layer::SetIsDrawable(bool is_drawable) { 658 DCHECK(IsPropertyChangeAllowed()); 659 if (is_drawable_ == is_drawable) 660 return; 661 662 is_drawable_ = is_drawable; 663 SetNeedsCommit(); 664 } 665 666 void Layer::SetHideLayerAndSubtree(bool hide) { 667 DCHECK(IsPropertyChangeAllowed()); 668 if (hide_layer_and_subtree_ == hide) 669 return; 670 671 hide_layer_and_subtree_ = hide; 672 SetNeedsCommit(); 673 } 674 675 void Layer::SetNeedsDisplayRect(const gfx::RectF& dirty_rect) { 676 if (dirty_rect.IsEmpty()) 677 return; 678 679 SetNeedsPushProperties(); 680 update_rect_.Union(dirty_rect); 681 682 if (DrawsContent()) 683 SetNeedsUpdate(); 684 } 685 686 bool Layer::DescendantIsFixedToContainerLayer() const { 687 for (size_t i = 0; i < children_.size(); ++i) { 688 if (children_[i]->position_constraint_.is_fixed_position() || 689 children_[i]->DescendantIsFixedToContainerLayer()) 690 return true; 691 } 692 return false; 693 } 694 695 void Layer::SetIsContainerForFixedPositionLayers(bool container) { 696 if (is_container_for_fixed_position_layers_ == container) 697 return; 698 is_container_for_fixed_position_layers_ = container; 699 700 if (layer_tree_host_ && layer_tree_host_->CommitRequested()) 701 return; 702 703 // Only request a commit if we have a fixed positioned descendant. 704 if (DescendantIsFixedToContainerLayer()) 705 SetNeedsCommit(); 706 } 707 708 void Layer::SetPositionConstraint(const LayerPositionConstraint& constraint) { 709 DCHECK(IsPropertyChangeAllowed()); 710 if (position_constraint_ == constraint) 711 return; 712 position_constraint_ = constraint; 713 SetNeedsCommit(); 714 } 715 716 static void RunCopyCallbackOnMainThread(scoped_ptr<CopyOutputRequest> request, 717 scoped_ptr<CopyOutputResult> result) { 718 request->SendResult(result.Pass()); 719 } 720 721 static void PostCopyCallbackToMainThread( 722 scoped_refptr<base::SingleThreadTaskRunner> main_thread_task_runner, 723 scoped_ptr<CopyOutputRequest> request, 724 scoped_ptr<CopyOutputResult> result) { 725 main_thread_task_runner->PostTask(FROM_HERE, 726 base::Bind(&RunCopyCallbackOnMainThread, 727 base::Passed(&request), 728 base::Passed(&result))); 729 } 730 731 void Layer::PushPropertiesTo(LayerImpl* layer) { 732 DCHECK(layer_tree_host_); 733 734 // If we did not SavePaintProperties() for the layer this frame, then push the 735 // real property values, not the paint property values. 736 bool use_paint_properties = paint_properties_.source_frame_number == 737 layer_tree_host_->source_frame_number(); 738 739 layer->SetAnchorPoint(anchor_point_); 740 layer->SetAnchorPointZ(anchor_point_z_); 741 layer->SetBackgroundColor(background_color_); 742 layer->SetBounds(use_paint_properties ? paint_properties_.bounds 743 : bounds_); 744 layer->SetContentBounds(content_bounds()); 745 layer->SetContentsScale(contents_scale_x(), contents_scale_y()); 746 layer->SetDebugName(debug_name_); 747 layer->SetCompositingReasons(compositing_reasons_); 748 layer->SetDoubleSided(double_sided_); 749 layer->SetDrawCheckerboardForMissingTiles( 750 draw_checkerboard_for_missing_tiles_); 751 layer->SetForceRenderSurface(force_render_surface_); 752 layer->SetDrawsContent(DrawsContent()); 753 layer->SetHideLayerAndSubtree(hide_layer_and_subtree_); 754 layer->SetFilters(filters()); 755 layer->SetFilter(filter()); 756 layer->SetBackgroundFilters(background_filters()); 757 layer->SetMasksToBounds(masks_to_bounds_); 758 layer->SetShouldScrollOnMainThread(should_scroll_on_main_thread_); 759 layer->SetHaveWheelEventHandlers(have_wheel_event_handlers_); 760 layer->SetNonFastScrollableRegion(non_fast_scrollable_region_); 761 layer->SetTouchEventHandlerRegion(touch_event_handler_region_); 762 layer->SetContentsOpaque(contents_opaque_); 763 if (!layer->OpacityIsAnimatingOnImplOnly() && !OpacityIsAnimating()) 764 layer->SetOpacity(opacity_); 765 DCHECK(!(OpacityIsAnimating() && layer->OpacityIsAnimatingOnImplOnly())); 766 layer->SetPosition(position_); 767 layer->SetIsContainerForFixedPositionLayers( 768 IsContainerForFixedPositionLayers()); 769 layer->SetFixedContainerSizeDelta(gfx::Vector2dF()); 770 layer->SetPositionConstraint(position_constraint_); 771 layer->SetPreserves3d(preserves_3d()); 772 layer->SetUseParentBackfaceVisibility(use_parent_backface_visibility_); 773 layer->SetSublayerTransform(sublayer_transform_); 774 if (!layer->TransformIsAnimatingOnImplOnly() && !TransformIsAnimating()) 775 layer->SetTransform(transform_); 776 DCHECK(!(TransformIsAnimating() && layer->TransformIsAnimatingOnImplOnly())); 777 778 layer->SetScrollable(scrollable_); 779 layer->SetScrollOffset(scroll_offset_); 780 layer->SetMaxScrollOffset(max_scroll_offset_); 781 782 // Wrap the copy_requests_ in a PostTask to the main thread. 783 ScopedPtrVector<CopyOutputRequest> main_thread_copy_requests; 784 for (ScopedPtrVector<CopyOutputRequest>::iterator it = copy_requests_.begin(); 785 it != copy_requests_.end(); 786 ++it) { 787 scoped_refptr<base::SingleThreadTaskRunner> main_thread_task_runner = 788 layer_tree_host()->proxy()->MainThreadTaskRunner(); 789 scoped_ptr<CopyOutputRequest> original_request = copy_requests_.take(it); 790 const CopyOutputRequest& original_request_ref = *original_request; 791 scoped_ptr<CopyOutputRequest> main_thread_request = 792 CopyOutputRequest::CreateRelayRequest( 793 original_request_ref, 794 base::Bind(&PostCopyCallbackToMainThread, 795 main_thread_task_runner, 796 base::Passed(&original_request))); 797 main_thread_copy_requests.push_back(main_thread_request.Pass()); 798 } 799 copy_requests_.clear(); 800 layer->PassCopyRequests(&main_thread_copy_requests); 801 802 // If the main thread commits multiple times before the impl thread actually 803 // draws, then damage tracking will become incorrect if we simply clobber the 804 // update_rect here. The LayerImpl's update_rect needs to accumulate (i.e. 805 // union) any update changes that have occurred on the main thread. 806 update_rect_.Union(layer->update_rect()); 807 layer->set_update_rect(update_rect_); 808 809 if (layer->layer_tree_impl()->settings().impl_side_painting) { 810 DCHECK(layer->layer_tree_impl()->IsPendingTree()); 811 LayerImpl* active_twin = 812 layer->layer_tree_impl()->FindActiveTreeLayerById(id()); 813 // Update the scroll delta from the active layer, which may have 814 // adjusted its scroll delta prior to this pending layer being created. 815 // This code is identical to that in LayerImpl::SetScrollDelta. 816 if (active_twin) { 817 DCHECK(layer->sent_scroll_delta().IsZero()); 818 layer->SetScrollDelta(active_twin->ScrollDelta() - 819 active_twin->sent_scroll_delta()); 820 } 821 } else { 822 layer->SetScrollDelta(layer->ScrollDelta() - layer->sent_scroll_delta()); 823 layer->SetSentScrollDelta(gfx::Vector2d()); 824 } 825 826 layer->SetStackingOrderChanged(stacking_order_changed_); 827 828 layer_animation_controller_->PushAnimationUpdatesTo( 829 layer->layer_animation_controller()); 830 831 // Reset any state that should be cleared for the next update. 832 stacking_order_changed_ = false; 833 update_rect_ = gfx::RectF(); 834 835 // Animating layers require further push properties to clean up the animation. 836 // crbug.com/259088 837 needs_push_properties_ = layer_animation_controller_->has_any_animation(); 838 num_dependents_need_push_properties_ = 0; 839 } 840 841 scoped_ptr<LayerImpl> Layer::CreateLayerImpl(LayerTreeImpl* tree_impl) { 842 return LayerImpl::Create(tree_impl, layer_id_); 843 } 844 845 bool Layer::DrawsContent() const { 846 return is_drawable_; 847 } 848 849 void Layer::SavePaintProperties() { 850 DCHECK(layer_tree_host_); 851 852 // TODO(reveman): Save all layer properties that we depend on not 853 // changing until PushProperties() has been called. crbug.com/231016 854 paint_properties_.bounds = bounds_; 855 paint_properties_.source_frame_number = 856 layer_tree_host_->source_frame_number(); 857 } 858 859 bool Layer::Update(ResourceUpdateQueue* queue, 860 const OcclusionTracker* occlusion) { 861 DCHECK(layer_tree_host_); 862 DCHECK_EQ(layer_tree_host_->source_frame_number(), 863 paint_properties_.source_frame_number) << 864 "SavePaintProperties must be called for any layer that is painted."; 865 return false; 866 } 867 868 bool Layer::NeedMoreUpdates() { 869 return false; 870 } 871 872 void Layer::SetDebugName(const std::string& debug_name) { 873 debug_name_ = debug_name; 874 SetNeedsCommit(); 875 } 876 877 void Layer::SetCompositingReasons(CompositingReasons reasons) { 878 compositing_reasons_ = reasons; 879 } 880 881 void Layer::CreateRenderSurface() { 882 DCHECK(!draw_properties_.render_surface); 883 draw_properties_.render_surface = make_scoped_ptr(new RenderSurface(this)); 884 draw_properties_.render_target = this; 885 } 886 887 void Layer::ClearRenderSurface() { 888 draw_properties_.render_surface.reset(); 889 } 890 891 void Layer::OnOpacityAnimated(float opacity) { 892 // This is called due to an ongoing accelerated animation. Since this 893 // animation is also being run on the impl thread, there is no need to request 894 // a commit to push this value over, so set the value directly rather than 895 // calling SetOpacity. 896 opacity_ = opacity; 897 } 898 899 void Layer::OnTransformAnimated(const gfx::Transform& transform) { 900 // This is called due to an ongoing accelerated animation. Since this 901 // animation is also being run on the impl thread, there is no need to request 902 // a commit to push this value over, so set this value directly rather than 903 // calling SetTransform. 904 transform_ = transform; 905 } 906 907 bool Layer::IsActive() const { 908 return true; 909 } 910 911 bool Layer::AddAnimation(scoped_ptr <Animation> animation) { 912 if (!layer_animation_controller_->animation_registrar()) 913 return false; 914 915 UMA_HISTOGRAM_BOOLEAN("Renderer.AnimationAddedToOrphanLayer", 916 !layer_tree_host_); 917 layer_animation_controller_->AddAnimation(animation.Pass()); 918 SetNeedsCommit(); 919 return true; 920 } 921 922 void Layer::PauseAnimation(int animation_id, double time_offset) { 923 layer_animation_controller_->PauseAnimation(animation_id, time_offset); 924 SetNeedsCommit(); 925 } 926 927 void Layer::RemoveAnimation(int animation_id) { 928 layer_animation_controller_->RemoveAnimation(animation_id); 929 SetNeedsCommit(); 930 } 931 932 void Layer::SuspendAnimations(double monotonic_time) { 933 layer_animation_controller_->SuspendAnimations(monotonic_time); 934 SetNeedsCommit(); 935 } 936 937 void Layer::ResumeAnimations(double monotonic_time) { 938 layer_animation_controller_->ResumeAnimations(monotonic_time); 939 SetNeedsCommit(); 940 } 941 942 void Layer::SetLayerAnimationControllerForTest( 943 scoped_refptr<LayerAnimationController> controller) { 944 layer_animation_controller_->RemoveValueObserver(this); 945 layer_animation_controller_ = controller; 946 layer_animation_controller_->set_force_sync(); 947 layer_animation_controller_->AddValueObserver(this); 948 SetNeedsCommit(); 949 } 950 951 bool Layer::HasActiveAnimation() const { 952 return layer_animation_controller_->HasActiveAnimation(); 953 } 954 955 void Layer::AddLayerAnimationEventObserver( 956 LayerAnimationEventObserver* animation_observer) { 957 layer_animation_controller_->AddEventObserver(animation_observer); 958 } 959 960 void Layer::RemoveLayerAnimationEventObserver( 961 LayerAnimationEventObserver* animation_observer) { 962 layer_animation_controller_->RemoveEventObserver(animation_observer); 963 } 964 965 Region Layer::VisibleContentOpaqueRegion() const { 966 if (contents_opaque()) 967 return visible_content_rect(); 968 return Region(); 969 } 970 971 ScrollbarLayer* Layer::ToScrollbarLayer() { 972 return NULL; 973 } 974 975 RenderingStatsInstrumentation* Layer::rendering_stats_instrumentation() const { 976 return layer_tree_host_->rendering_stats_instrumentation(); 977 } 978 979 bool Layer::SupportsLCDText() const { 980 return false; 981 } 982 983 } // namespace cc 984