Address some additional cleanup work needed for the component flash updates on Linux.
[chromium-blink-merge.git] / cc / layers / layer.cc
blob4f7eeba5b12c50bda73fa46f2069d80071f2828a
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.
5 #include "cc/layers/layer.h"
7 #include <algorithm>
9 #include "base/atomic_sequence_num.h"
10 #include "base/location.h"
11 #include "base/metrics/histogram.h"
12 #include "base/single_thread_task_runner.h"
13 #include "base/time/time.h"
14 #include "base/trace_event/trace_event.h"
15 #include "cc/animation/animation.h"
16 #include "cc/animation/animation_events.h"
17 #include "cc/animation/animation_registrar.h"
18 #include "cc/animation/keyframed_animation_curve.h"
19 #include "cc/animation/layer_animation_controller.h"
20 #include "cc/base/simple_enclosed_region.h"
21 #include "cc/debug/frame_viewer_instrumentation.h"
22 #include "cc/layers/layer_client.h"
23 #include "cc/layers/layer_impl.h"
24 #include "cc/layers/scrollbar_layer_interface.h"
25 #include "cc/output/copy_output_request.h"
26 #include "cc/output/copy_output_result.h"
27 #include "cc/trees/layer_tree_host.h"
28 #include "cc/trees/layer_tree_impl.h"
29 #include "third_party/skia/include/core/SkImageFilter.h"
30 #include "ui/gfx/geometry/rect_conversions.h"
31 #include "ui/gfx/geometry/vector2d_conversions.h"
33 namespace cc {
35 base::StaticAtomicSequenceNumber g_next_layer_id;
37 scoped_refptr<Layer> Layer::Create(const LayerSettings& settings) {
38 return make_scoped_refptr(new Layer(settings));
41 Layer::Layer(const LayerSettings& settings)
42 : needs_push_properties_(false),
43 num_dependents_need_push_properties_(0),
44 stacking_order_changed_(false),
45 // Layer IDs start from 1.
46 layer_id_(g_next_layer_id.GetNext() + 1),
47 ignore_set_needs_commit_(false),
48 sorting_context_id_(0),
49 parent_(nullptr),
50 layer_tree_host_(nullptr),
51 scroll_clip_layer_id_(INVALID_ID),
52 num_descendants_that_draw_content_(0),
53 transform_tree_index_(-1),
54 effect_tree_index_(-1),
55 clip_tree_index_(-1),
56 property_tree_sequence_number_(-1),
57 num_layer_or_descendants_with_copy_request_(0),
58 num_layer_or_descendants_with_input_handler_(0),
59 num_children_with_scroll_parent_(0),
60 should_flatten_transform_from_property_tree_(false),
61 is_clipped_(false),
62 should_scroll_on_main_thread_(false),
63 have_wheel_event_handlers_(false),
64 have_scroll_event_handlers_(false),
65 user_scrollable_horizontal_(true),
66 user_scrollable_vertical_(true),
67 is_root_for_isolated_group_(false),
68 is_container_for_fixed_position_layers_(false),
69 is_drawable_(false),
70 draws_content_(false),
71 hide_layer_and_subtree_(false),
72 masks_to_bounds_(false),
73 contents_opaque_(false),
74 double_sided_(true),
75 should_flatten_transform_(true),
76 use_parent_backface_visibility_(false),
77 force_render_surface_(false),
78 transform_is_invertible_(true),
79 has_render_surface_(false),
80 scroll_blocks_on_(SCROLL_BLOCKS_ON_NONE),
81 background_color_(0),
82 opacity_(1.f),
83 blend_mode_(SkXfermode::kSrcOver_Mode),
84 draw_blend_mode_(SkXfermode::kSrcOver_Mode),
85 scroll_parent_(nullptr),
86 layer_or_descendant_is_drawn_tracker_(0),
87 sorted_for_recursion_tracker_(0),
88 visited_tracker_(0),
89 clip_parent_(nullptr),
90 replica_layer_(nullptr),
91 client_(nullptr),
92 frame_timing_requests_dirty_(false) {
93 if (!settings.use_compositor_animation_timelines) {
94 layer_animation_controller_ = LayerAnimationController::Create(layer_id_);
95 layer_animation_controller_->AddValueObserver(this);
96 layer_animation_controller_->set_value_provider(this);
100 Layer::~Layer() {
101 // Our parent should be holding a reference to us so there should be no
102 // way for us to be destroyed while we still have a parent.
103 DCHECK(!parent());
104 // Similarly we shouldn't have a layer tree host since it also keeps a
105 // reference to us.
106 DCHECK(!layer_tree_host());
108 if (layer_animation_controller_) {
109 layer_animation_controller_->RemoveValueObserver(this);
110 layer_animation_controller_->remove_value_provider(this);
113 RemoveFromScrollTree();
114 RemoveFromClipTree();
116 // Remove the parent reference from all children and dependents.
117 RemoveAllChildren();
118 if (mask_layer_.get()) {
119 DCHECK_EQ(this, mask_layer_->parent());
120 mask_layer_->RemoveFromParent();
122 if (replica_layer_.get()) {
123 DCHECK_EQ(this, replica_layer_->parent());
124 replica_layer_->RemoveFromParent();
128 void Layer::SetLayerTreeHost(LayerTreeHost* host) {
129 if (layer_tree_host_ == host)
130 return;
132 if (layer_tree_host_) {
133 layer_tree_host_->property_trees()->needs_rebuild = true;
134 layer_tree_host_->UnregisterLayer(this);
136 if (host) {
137 host->property_trees()->needs_rebuild = true;
138 host->RegisterLayer(this);
141 InvalidatePropertyTreesIndices();
143 layer_tree_host_ = host;
145 // When changing hosts, the layer needs to commit its properties to the impl
146 // side for the new host.
147 SetNeedsPushProperties();
149 for (size_t i = 0; i < children_.size(); ++i)
150 children_[i]->SetLayerTreeHost(host);
152 if (mask_layer_.get())
153 mask_layer_->SetLayerTreeHost(host);
154 if (replica_layer_.get())
155 replica_layer_->SetLayerTreeHost(host);
157 if (host)
158 RegisterForAnimations(host->animation_registrar());
160 bool has_any_animation = false;
161 if (layer_animation_controller_)
162 has_any_animation = layer_animation_controller_->has_any_animation();
163 else if (layer_tree_host_)
164 has_any_animation = layer_tree_host_->HasAnyAnimation(this);
166 if (host && has_any_animation)
167 host->SetNeedsCommit();
170 void Layer::SetNeedsUpdate() {
171 if (layer_tree_host_ && !ignore_set_needs_commit_)
172 layer_tree_host_->SetNeedsUpdateLayers();
175 void Layer::SetNeedsCommit() {
176 if (!layer_tree_host_)
177 return;
179 SetNeedsPushProperties();
180 layer_tree_host_->property_trees()->needs_rebuild = true;
182 if (ignore_set_needs_commit_)
183 return;
185 layer_tree_host_->SetNeedsCommit();
188 void Layer::SetNeedsCommitNoRebuild() {
189 if (!layer_tree_host_)
190 return;
192 SetNeedsPushProperties();
194 if (ignore_set_needs_commit_)
195 return;
197 layer_tree_host_->SetNeedsCommit();
200 void Layer::SetNeedsFullTreeSync() {
201 if (!layer_tree_host_)
202 return;
204 layer_tree_host_->SetNeedsFullTreeSync();
207 void Layer::SetNextCommitWaitsForActivation() {
208 if (!layer_tree_host_)
209 return;
211 layer_tree_host_->SetNextCommitWaitsForActivation();
214 void Layer::SetNeedsPushProperties() {
215 if (needs_push_properties_)
216 return;
217 if (!parent_should_know_need_push_properties() && parent_)
218 parent_->AddDependentNeedsPushProperties();
219 needs_push_properties_ = true;
222 void Layer::AddDependentNeedsPushProperties() {
223 DCHECK_GE(num_dependents_need_push_properties_, 0);
225 if (!parent_should_know_need_push_properties() && parent_)
226 parent_->AddDependentNeedsPushProperties();
228 num_dependents_need_push_properties_++;
231 void Layer::RemoveDependentNeedsPushProperties() {
232 num_dependents_need_push_properties_--;
233 DCHECK_GE(num_dependents_need_push_properties_, 0);
235 if (!parent_should_know_need_push_properties() && parent_)
236 parent_->RemoveDependentNeedsPushProperties();
239 bool Layer::IsPropertyChangeAllowed() const {
240 if (!layer_tree_host_)
241 return true;
243 if (!layer_tree_host_->settings().strict_layer_property_change_checking)
244 return true;
246 return !layer_tree_host_->in_paint_layer_contents();
249 skia::RefPtr<SkPicture> Layer::GetPicture() const {
250 return skia::RefPtr<SkPicture>();
253 void Layer::SetParent(Layer* layer) {
254 DCHECK(!layer || !layer->HasAncestor(this));
256 if (parent_should_know_need_push_properties()) {
257 if (parent_)
258 parent_->RemoveDependentNeedsPushProperties();
259 if (layer)
260 layer->AddDependentNeedsPushProperties();
263 parent_ = layer;
264 SetLayerTreeHost(parent_ ? parent_->layer_tree_host() : nullptr);
266 if (!layer_tree_host_)
267 return;
269 layer_tree_host_->property_trees()->needs_rebuild = true;
272 void Layer::AddChild(scoped_refptr<Layer> child) {
273 InsertChild(child, children_.size());
276 void Layer::InsertChild(scoped_refptr<Layer> child, size_t index) {
277 DCHECK(IsPropertyChangeAllowed());
278 child->RemoveFromParent();
279 AddDrawableDescendants(child->NumDescendantsThatDrawContent() +
280 (child->DrawsContent() ? 1 : 0));
281 child->SetParent(this);
282 child->stacking_order_changed_ = true;
284 index = std::min(index, children_.size());
285 children_.insert(children_.begin() + index, child);
286 SetNeedsFullTreeSync();
289 void Layer::RemoveFromParent() {
290 DCHECK(IsPropertyChangeAllowed());
291 if (parent_)
292 parent_->RemoveChildOrDependent(this);
295 void Layer::RemoveChildOrDependent(Layer* child) {
296 if (mask_layer_.get() == child) {
297 mask_layer_->SetParent(nullptr);
298 mask_layer_ = nullptr;
299 SetNeedsFullTreeSync();
300 return;
302 if (replica_layer_.get() == child) {
303 replica_layer_->SetParent(nullptr);
304 replica_layer_ = nullptr;
305 SetNeedsFullTreeSync();
306 return;
309 for (LayerList::iterator iter = children_.begin();
310 iter != children_.end();
311 ++iter) {
312 if (iter->get() != child)
313 continue;
315 child->SetParent(nullptr);
316 AddDrawableDescendants(-child->NumDescendantsThatDrawContent() -
317 (child->DrawsContent() ? 1 : 0));
318 children_.erase(iter);
319 SetNeedsFullTreeSync();
320 return;
324 void Layer::ReplaceChild(Layer* reference, scoped_refptr<Layer> new_layer) {
325 DCHECK(reference);
326 DCHECK_EQ(reference->parent(), this);
327 DCHECK(IsPropertyChangeAllowed());
329 if (reference == new_layer.get())
330 return;
332 // Find the index of |reference| in |children_|.
333 auto reference_it =
334 std::find_if(children_.begin(), children_.end(),
335 [reference](const scoped_refptr<Layer>& layer) {
336 return layer.get() == reference;
338 DCHECK(reference_it != children_.end());
339 size_t reference_index = reference_it - children_.begin();
340 reference->RemoveFromParent();
342 if (new_layer.get()) {
343 new_layer->RemoveFromParent();
344 InsertChild(new_layer, reference_index);
348 void Layer::SetBounds(const gfx::Size& size) {
349 DCHECK(IsPropertyChangeAllowed());
350 if (bounds() == size)
351 return;
352 bounds_ = size;
354 if (!layer_tree_host_)
355 return;
357 if (ClipNode* clip_node = layer_tree_host_->property_trees()->clip_tree.Node(
358 clip_tree_index())) {
359 if (clip_node->owner_id == id()) {
360 clip_node->data.clip.set_size(size);
361 layer_tree_host_->property_trees()->clip_tree.set_needs_update(true);
365 SetNeedsCommitNoRebuild();
368 Layer* Layer::RootLayer() {
369 Layer* layer = this;
370 while (layer->parent())
371 layer = layer->parent();
372 return layer;
375 void Layer::RemoveAllChildren() {
376 DCHECK(IsPropertyChangeAllowed());
377 while (children_.size()) {
378 Layer* layer = children_[0].get();
379 DCHECK_EQ(this, layer->parent());
380 layer->RemoveFromParent();
384 void Layer::SetChildren(const LayerList& children) {
385 DCHECK(IsPropertyChangeAllowed());
386 if (children == children_)
387 return;
389 RemoveAllChildren();
390 for (size_t i = 0; i < children.size(); ++i)
391 AddChild(children[i]);
394 bool Layer::HasAncestor(const Layer* ancestor) const {
395 for (const Layer* layer = parent(); layer; layer = layer->parent()) {
396 if (layer == ancestor)
397 return true;
399 return false;
402 void Layer::RequestCopyOfOutput(
403 scoped_ptr<CopyOutputRequest> request) {
404 DCHECK(IsPropertyChangeAllowed());
405 bool had_no_copy_requests = copy_requests_.empty();
406 if (void* source = request->source()) {
407 auto it = std::find_if(
408 copy_requests_.begin(), copy_requests_.end(),
409 [source](const CopyOutputRequest* x) { return x->source() == source; });
410 if (it != copy_requests_.end())
411 copy_requests_.erase(it);
413 if (request->IsEmpty())
414 return;
415 copy_requests_.push_back(request.Pass());
416 if (had_no_copy_requests) {
417 bool copy_request_added = true;
418 UpdateNumCopyRequestsForSubtree(copy_request_added);
420 SetNeedsCommit();
423 void Layer::UpdateNumCopyRequestsForSubtree(bool add) {
424 int change = add ? 1 : -1;
425 for (Layer* layer = this; layer; layer = layer->parent()) {
426 layer->num_layer_or_descendants_with_copy_request_ += change;
427 layer->draw_properties().layer_or_descendant_has_copy_request =
428 (layer->num_layer_or_descendants_with_copy_request_ != 0);
429 DCHECK_GE(layer->num_layer_or_descendants_with_copy_request_, 0);
433 void Layer::SetBackgroundColor(SkColor background_color) {
434 DCHECK(IsPropertyChangeAllowed());
435 if (background_color_ == background_color)
436 return;
437 background_color_ = background_color;
438 SetNeedsCommit();
441 SkColor Layer::SafeOpaqueBackgroundColor() const {
442 SkColor color = background_color();
443 if (SkColorGetA(color) == 255 && !contents_opaque()) {
444 color = SK_ColorTRANSPARENT;
445 } else if (SkColorGetA(color) != 255 && contents_opaque()) {
446 for (const Layer* layer = parent(); layer;
447 layer = layer->parent()) {
448 color = layer->background_color();
449 if (SkColorGetA(color) == 255)
450 break;
452 if (SkColorGetA(color) != 255)
453 color = layer_tree_host_->background_color();
454 if (SkColorGetA(color) != 255)
455 color = SkColorSetA(color, 255);
457 return color;
460 void Layer::SetMasksToBounds(bool masks_to_bounds) {
461 DCHECK(IsPropertyChangeAllowed());
462 if (masks_to_bounds_ == masks_to_bounds)
463 return;
464 masks_to_bounds_ = masks_to_bounds;
465 SetNeedsCommit();
468 void Layer::SetMaskLayer(Layer* mask_layer) {
469 DCHECK(IsPropertyChangeAllowed());
470 if (mask_layer_.get() == mask_layer)
471 return;
472 if (mask_layer_.get()) {
473 DCHECK_EQ(this, mask_layer_->parent());
474 mask_layer_->RemoveFromParent();
476 mask_layer_ = mask_layer;
477 if (mask_layer_.get()) {
478 DCHECK(!mask_layer_->parent());
479 mask_layer_->RemoveFromParent();
480 mask_layer_->SetParent(this);
481 mask_layer_->SetIsMask(true);
483 SetNeedsFullTreeSync();
486 void Layer::SetReplicaLayer(Layer* layer) {
487 DCHECK(IsPropertyChangeAllowed());
488 if (replica_layer_.get() == layer)
489 return;
490 if (replica_layer_.get()) {
491 DCHECK_EQ(this, replica_layer_->parent());
492 replica_layer_->RemoveFromParent();
494 replica_layer_ = layer;
495 if (replica_layer_.get()) {
496 DCHECK(!replica_layer_->parent());
497 replica_layer_->RemoveFromParent();
498 replica_layer_->SetParent(this);
500 SetNeedsFullTreeSync();
503 void Layer::SetFilters(const FilterOperations& filters) {
504 DCHECK(IsPropertyChangeAllowed());
505 if (filters_ == filters)
506 return;
507 filters_ = filters;
508 SetNeedsCommit();
511 bool Layer::FilterIsAnimating() const {
512 DCHECK(layer_tree_host_);
513 return layer_animation_controller_
514 ? layer_animation_controller_->IsCurrentlyAnimatingProperty(
515 Animation::FILTER,
516 LayerAnimationController::ObserverType::ACTIVE)
517 : layer_tree_host_->IsAnimatingFilterProperty(this);
520 bool Layer::HasPotentiallyRunningFilterAnimation() const {
521 if (layer_animation_controller_) {
522 return layer_animation_controller_->IsPotentiallyAnimatingProperty(
523 Animation::FILTER, LayerAnimationController::ObserverType::ACTIVE);
525 return layer_tree_host_->HasPotentiallyRunningFilterAnimation(this);
528 void Layer::SetBackgroundFilters(const FilterOperations& filters) {
529 DCHECK(IsPropertyChangeAllowed());
530 if (background_filters_ == filters)
531 return;
532 background_filters_ = filters;
533 SetNeedsCommit();
536 void Layer::SetOpacity(float opacity) {
537 DCHECK(IsPropertyChangeAllowed());
538 if (opacity_ == opacity)
539 return;
540 opacity_ = opacity;
541 SetNeedsCommit();
544 bool Layer::OpacityIsAnimating() const {
545 DCHECK(layer_tree_host_);
546 return layer_animation_controller_
547 ? layer_animation_controller_->IsCurrentlyAnimatingProperty(
548 Animation::OPACITY,
549 LayerAnimationController::ObserverType::ACTIVE)
550 : layer_tree_host_->IsAnimatingOpacityProperty(this);
553 bool Layer::HasPotentiallyRunningOpacityAnimation() const {
554 if (layer_animation_controller_) {
555 return layer_animation_controller_->IsPotentiallyAnimatingProperty(
556 Animation::OPACITY, LayerAnimationController::ObserverType::ACTIVE);
558 return layer_tree_host_->HasPotentiallyRunningOpacityAnimation(this);
561 bool Layer::OpacityCanAnimateOnImplThread() const {
562 return false;
565 void Layer::SetBlendMode(SkXfermode::Mode blend_mode) {
566 DCHECK(IsPropertyChangeAllowed());
567 if (blend_mode_ == blend_mode)
568 return;
570 // Allowing only blend modes that are defined in the CSS Compositing standard:
571 // http://dev.w3.org/fxtf/compositing-1/#blending
572 switch (blend_mode) {
573 case SkXfermode::kSrcOver_Mode:
574 case SkXfermode::kScreen_Mode:
575 case SkXfermode::kOverlay_Mode:
576 case SkXfermode::kDarken_Mode:
577 case SkXfermode::kLighten_Mode:
578 case SkXfermode::kColorDodge_Mode:
579 case SkXfermode::kColorBurn_Mode:
580 case SkXfermode::kHardLight_Mode:
581 case SkXfermode::kSoftLight_Mode:
582 case SkXfermode::kDifference_Mode:
583 case SkXfermode::kExclusion_Mode:
584 case SkXfermode::kMultiply_Mode:
585 case SkXfermode::kHue_Mode:
586 case SkXfermode::kSaturation_Mode:
587 case SkXfermode::kColor_Mode:
588 case SkXfermode::kLuminosity_Mode:
589 // supported blend modes
590 break;
591 case SkXfermode::kClear_Mode:
592 case SkXfermode::kSrc_Mode:
593 case SkXfermode::kDst_Mode:
594 case SkXfermode::kDstOver_Mode:
595 case SkXfermode::kSrcIn_Mode:
596 case SkXfermode::kDstIn_Mode:
597 case SkXfermode::kSrcOut_Mode:
598 case SkXfermode::kDstOut_Mode:
599 case SkXfermode::kSrcATop_Mode:
600 case SkXfermode::kDstATop_Mode:
601 case SkXfermode::kXor_Mode:
602 case SkXfermode::kPlus_Mode:
603 case SkXfermode::kModulate_Mode:
604 // Porter Duff Compositing Operators are not yet supported
605 // http://dev.w3.org/fxtf/compositing-1/#porterduffcompositingoperators
606 NOTREACHED();
607 return;
610 blend_mode_ = blend_mode;
611 SetNeedsCommit();
614 void Layer::SetIsRootForIsolatedGroup(bool root) {
615 DCHECK(IsPropertyChangeAllowed());
616 if (is_root_for_isolated_group_ == root)
617 return;
618 is_root_for_isolated_group_ = root;
619 SetNeedsCommit();
622 void Layer::SetContentsOpaque(bool opaque) {
623 DCHECK(IsPropertyChangeAllowed());
624 if (contents_opaque_ == opaque)
625 return;
626 contents_opaque_ = opaque;
627 SetNeedsCommit();
630 void Layer::SetPosition(const gfx::PointF& position) {
631 DCHECK(IsPropertyChangeAllowed());
632 if (position_ == position)
633 return;
634 position_ = position;
636 if (!layer_tree_host_)
637 return;
639 if (TransformNode* transform_node =
640 layer_tree_host_->property_trees()->transform_tree.Node(
641 transform_tree_index())) {
642 if (transform_node->owner_id == id()) {
643 transform_node->data.update_post_local_transform(position,
644 transform_origin());
645 transform_node->data.needs_local_transform_update = true;
646 layer_tree_host_->property_trees()->transform_tree.set_needs_update(true);
647 SetNeedsCommitNoRebuild();
648 return;
652 SetNeedsCommit();
655 bool Layer::IsContainerForFixedPositionLayers() const {
656 if (!transform_.IsIdentityOrTranslation())
657 return true;
658 if (parent_ && !parent_->transform_.IsIdentityOrTranslation())
659 return true;
660 return is_container_for_fixed_position_layers_;
663 bool Are2dAxisAligned(const gfx::Transform& a,
664 const gfx::Transform& b,
665 bool* is_invertible) {
666 if (a.IsScaleOrTranslation() && b.IsScaleOrTranslation()) {
667 *is_invertible = b.IsInvertible();
668 return true;
671 gfx::Transform inverse(gfx::Transform::kSkipInitialization);
672 *is_invertible = b.GetInverse(&inverse);
674 inverse *= a;
675 return inverse.Preserves2dAxisAlignment();
678 void Layer::SetTransform(const gfx::Transform& transform) {
679 DCHECK(IsPropertyChangeAllowed());
680 if (transform_ == transform)
681 return;
683 if (layer_tree_host_) {
684 if (TransformNode* transform_node =
685 layer_tree_host_->property_trees()->transform_tree.Node(
686 transform_tree_index())) {
687 if (transform_node->owner_id == id()) {
688 // We need to trigger a rebuild if we could have affected 2d axis
689 // alignment. We'll check to see if transform and transform_ are axis
690 // align with respect to one another.
691 bool invertible = false;
692 bool preserves_2d_axis_alignment =
693 Are2dAxisAligned(transform_, transform, &invertible);
694 transform_node->data.local = transform;
695 transform_node->data.needs_local_transform_update = true;
696 layer_tree_host_->property_trees()->transform_tree.set_needs_update(
697 true);
698 if (preserves_2d_axis_alignment)
699 SetNeedsCommitNoRebuild();
700 else
701 SetNeedsCommit();
702 transform_ = transform;
703 transform_is_invertible_ = invertible;
704 return;
709 transform_ = transform;
710 transform_is_invertible_ = transform.IsInvertible();
712 SetNeedsCommit();
715 void Layer::SetTransformOrigin(const gfx::Point3F& transform_origin) {
716 DCHECK(IsPropertyChangeAllowed());
717 if (transform_origin_ == transform_origin)
718 return;
719 transform_origin_ = transform_origin;
721 if (!layer_tree_host_)
722 return;
724 if (TransformNode* transform_node =
725 layer_tree_host_->property_trees()->transform_tree.Node(
726 transform_tree_index())) {
727 if (transform_node->owner_id == id()) {
728 transform_node->data.update_pre_local_transform(transform_origin);
729 transform_node->data.update_post_local_transform(position(),
730 transform_origin);
731 transform_node->data.needs_local_transform_update = true;
732 layer_tree_host_->property_trees()->transform_tree.set_needs_update(true);
733 SetNeedsCommitNoRebuild();
734 return;
738 SetNeedsCommit();
741 bool Layer::AnimationsPreserveAxisAlignment() const {
742 DCHECK(layer_tree_host_);
743 return layer_animation_controller_
744 ? layer_animation_controller_->AnimationsPreserveAxisAlignment()
745 : layer_tree_host_->AnimationsPreserveAxisAlignment(this);
748 bool Layer::TransformIsAnimating() const {
749 DCHECK(layer_tree_host_);
750 return layer_animation_controller_
751 ? layer_animation_controller_->IsCurrentlyAnimatingProperty(
752 Animation::TRANSFORM,
753 LayerAnimationController::ObserverType::ACTIVE)
754 : layer_tree_host_->IsAnimatingTransformProperty(this);
757 bool Layer::HasPotentiallyRunningTransformAnimation() const {
758 if (layer_animation_controller_) {
759 return layer_animation_controller_->IsPotentiallyAnimatingProperty(
760 Animation::TRANSFORM, LayerAnimationController::ObserverType::ACTIVE);
762 return layer_tree_host_->HasPotentiallyRunningTransformAnimation(this);
765 bool Layer::HasAnyAnimationTargetingProperty(
766 Animation::TargetProperty property) const {
767 if (layer_animation_controller_)
768 return !!layer_animation_controller_->GetAnimation(property);
770 return layer_tree_host_->HasAnyAnimationTargetingProperty(this, property);
773 bool Layer::ScrollOffsetAnimationWasInterrupted() const {
774 DCHECK(layer_tree_host_);
775 return layer_animation_controller_
776 ? layer_animation_controller_
777 ->scroll_offset_animation_was_interrupted()
778 : layer_tree_host_->ScrollOffsetAnimationWasInterrupted(this);
781 void Layer::SetScrollParent(Layer* parent) {
782 DCHECK(IsPropertyChangeAllowed());
783 if (scroll_parent_ == parent)
784 return;
786 if (scroll_parent_)
787 scroll_parent_->RemoveScrollChild(this);
789 scroll_parent_ = parent;
791 if (scroll_parent_)
792 scroll_parent_->AddScrollChild(this);
794 SetNeedsCommit();
797 void Layer::AddScrollChild(Layer* child) {
798 if (!scroll_children_)
799 scroll_children_.reset(new std::set<Layer*>);
800 scroll_children_->insert(child);
801 if (layer_tree_host_ && !layer_tree_host_->needs_meta_info_recomputation()) {
802 num_children_with_scroll_parent_++;
803 draw_properties().has_child_with_a_scroll_parent = true;
805 SetNeedsCommit();
808 void Layer::RemoveScrollChild(Layer* child) {
809 scroll_children_->erase(child);
810 if (scroll_children_->empty())
811 scroll_children_ = nullptr;
812 if (layer_tree_host_ && !layer_tree_host_->needs_meta_info_recomputation()) {
813 num_children_with_scroll_parent_--;
814 DCHECK_GE(num_children_with_scroll_parent_, 0);
815 draw_properties().has_child_with_a_scroll_parent =
816 (num_children_with_scroll_parent_ != 0);
818 SetNeedsCommit();
821 void Layer::SetClipParent(Layer* ancestor) {
822 DCHECK(IsPropertyChangeAllowed());
823 if (clip_parent_ == ancestor)
824 return;
826 if (clip_parent_)
827 clip_parent_->RemoveClipChild(this);
829 clip_parent_ = ancestor;
831 if (clip_parent_)
832 clip_parent_->AddClipChild(this);
834 SetNeedsCommit();
835 if (layer_tree_host_)
836 layer_tree_host_->SetNeedsMetaInfoRecomputation(true);
839 void Layer::AddClipChild(Layer* child) {
840 if (!clip_children_)
841 clip_children_.reset(new std::set<Layer*>);
842 clip_children_->insert(child);
843 SetNeedsCommit();
846 void Layer::RemoveClipChild(Layer* child) {
847 clip_children_->erase(child);
848 if (clip_children_->empty())
849 clip_children_ = nullptr;
850 SetNeedsCommit();
853 void Layer::SetScrollOffset(const gfx::ScrollOffset& scroll_offset) {
854 DCHECK(IsPropertyChangeAllowed());
856 if (scroll_offset_ == scroll_offset)
857 return;
858 scroll_offset_ = scroll_offset;
860 if (!layer_tree_host_)
861 return;
863 if (TransformNode* transform_node =
864 layer_tree_host_->property_trees()->transform_tree.Node(
865 transform_tree_index())) {
866 if (transform_node->owner_id == id()) {
867 transform_node->data.scroll_offset = CurrentScrollOffset();
868 transform_node->data.needs_local_transform_update = true;
869 layer_tree_host_->property_trees()->transform_tree.set_needs_update(true);
870 SetNeedsCommitNoRebuild();
871 return;
875 SetNeedsCommit();
878 void Layer::SetScrollCompensationAdjustment(
879 const gfx::Vector2dF& scroll_compensation_adjustment) {
880 if (scroll_compensation_adjustment_ == scroll_compensation_adjustment)
881 return;
882 scroll_compensation_adjustment_ = scroll_compensation_adjustment;
883 SetNeedsCommit();
886 gfx::Vector2dF Layer::ScrollCompensationAdjustment() const {
887 return scroll_compensation_adjustment_;
890 void Layer::SetScrollOffsetFromImplSide(
891 const gfx::ScrollOffset& scroll_offset) {
892 DCHECK(IsPropertyChangeAllowed());
893 // This function only gets called during a BeginMainFrame, so there
894 // is no need to call SetNeedsUpdate here.
895 DCHECK(layer_tree_host_ && layer_tree_host_->CommitRequested());
896 if (scroll_offset_ == scroll_offset)
897 return;
898 scroll_offset_ = scroll_offset;
899 SetNeedsPushProperties();
901 bool needs_rebuild = true;
902 if (TransformNode* transform_node =
903 layer_tree_host_->property_trees()->transform_tree.Node(
904 transform_tree_index())) {
905 if (transform_node->owner_id == id()) {
906 transform_node->data.scroll_offset = CurrentScrollOffset();
907 transform_node->data.needs_local_transform_update = true;
908 layer_tree_host_->property_trees()->transform_tree.set_needs_update(true);
909 needs_rebuild = false;
913 if (needs_rebuild)
914 layer_tree_host_->property_trees()->needs_rebuild = true;
916 if (!did_scroll_callback_.is_null())
917 did_scroll_callback_.Run();
918 // The callback could potentially change the layer structure:
919 // "this" may have been destroyed during the process.
922 void Layer::SetScrollClipLayerId(int clip_layer_id) {
923 DCHECK(IsPropertyChangeAllowed());
924 if (scroll_clip_layer_id_ == clip_layer_id)
925 return;
926 scroll_clip_layer_id_ = clip_layer_id;
927 SetNeedsCommit();
930 void Layer::SetUserScrollable(bool horizontal, bool vertical) {
931 DCHECK(IsPropertyChangeAllowed());
932 if (user_scrollable_horizontal_ == horizontal &&
933 user_scrollable_vertical_ == vertical)
934 return;
935 user_scrollable_horizontal_ = horizontal;
936 user_scrollable_vertical_ = vertical;
937 SetNeedsCommit();
940 void Layer::SetShouldScrollOnMainThread(bool should_scroll_on_main_thread) {
941 DCHECK(IsPropertyChangeAllowed());
942 if (should_scroll_on_main_thread_ == should_scroll_on_main_thread)
943 return;
944 should_scroll_on_main_thread_ = should_scroll_on_main_thread;
945 SetNeedsCommit();
948 void Layer::SetHaveWheelEventHandlers(bool have_wheel_event_handlers) {
949 DCHECK(IsPropertyChangeAllowed());
950 if (have_wheel_event_handlers_ == have_wheel_event_handlers)
951 return;
952 if (touch_event_handler_region_.IsEmpty() && layer_tree_host_ &&
953 !layer_tree_host_->needs_meta_info_recomputation())
954 UpdateNumInputHandlersForSubtree(have_wheel_event_handlers);
956 have_wheel_event_handlers_ = have_wheel_event_handlers;
957 SetNeedsCommit();
960 void Layer::UpdateNumInputHandlersForSubtree(bool add) {
961 int change = add ? 1 : -1;
962 for (Layer* layer = this; layer; layer = layer->parent()) {
963 layer->num_layer_or_descendants_with_input_handler_ += change;
964 layer->draw_properties().layer_or_descendant_has_input_handler =
965 (layer->num_layer_or_descendants_with_input_handler_ != 0);
966 DCHECK_GE(layer->num_layer_or_descendants_with_input_handler_, 0);
970 void Layer::SetHaveScrollEventHandlers(bool have_scroll_event_handlers) {
971 DCHECK(IsPropertyChangeAllowed());
972 if (have_scroll_event_handlers_ == have_scroll_event_handlers)
973 return;
974 have_scroll_event_handlers_ = have_scroll_event_handlers;
975 SetNeedsCommit();
978 void Layer::SetNonFastScrollableRegion(const Region& region) {
979 DCHECK(IsPropertyChangeAllowed());
980 if (non_fast_scrollable_region_ == region)
981 return;
982 non_fast_scrollable_region_ = region;
983 SetNeedsCommit();
986 void Layer::SetTouchEventHandlerRegion(const Region& region) {
987 DCHECK(IsPropertyChangeAllowed());
988 if (touch_event_handler_region_ == region)
989 return;
990 if (!have_wheel_event_handlers_ && layer_tree_host_ &&
991 !layer_tree_host_->needs_meta_info_recomputation())
992 UpdateNumInputHandlersForSubtree(!region.IsEmpty());
994 touch_event_handler_region_ = region;
995 SetNeedsCommit();
998 void Layer::SetScrollBlocksOn(ScrollBlocksOn scroll_blocks_on) {
999 DCHECK(IsPropertyChangeAllowed());
1000 if (scroll_blocks_on_ == scroll_blocks_on)
1001 return;
1002 scroll_blocks_on_ = scroll_blocks_on;
1003 SetNeedsCommit();
1006 void Layer::SetForceRenderSurface(bool force) {
1007 DCHECK(IsPropertyChangeAllowed());
1008 if (force_render_surface_ == force)
1009 return;
1010 force_render_surface_ = force;
1011 SetNeedsCommit();
1014 void Layer::SetDoubleSided(bool double_sided) {
1015 DCHECK(IsPropertyChangeAllowed());
1016 if (double_sided_ == double_sided)
1017 return;
1018 double_sided_ = double_sided;
1019 SetNeedsCommit();
1022 void Layer::Set3dSortingContextId(int id) {
1023 DCHECK(IsPropertyChangeAllowed());
1024 if (id == sorting_context_id_)
1025 return;
1026 sorting_context_id_ = id;
1027 SetNeedsCommit();
1030 void Layer::SetTransformTreeIndex(int index) {
1031 DCHECK(IsPropertyChangeAllowed());
1032 if (transform_tree_index_ == index)
1033 return;
1034 transform_tree_index_ = index;
1035 SetNeedsPushProperties();
1038 int Layer::transform_tree_index() const {
1039 if (!layer_tree_host_ ||
1040 layer_tree_host_->property_trees()->sequence_number !=
1041 property_tree_sequence_number_) {
1042 return -1;
1044 return transform_tree_index_;
1047 void Layer::SetClipTreeIndex(int index) {
1048 DCHECK(IsPropertyChangeAllowed());
1049 if (clip_tree_index_ == index)
1050 return;
1051 clip_tree_index_ = index;
1052 SetNeedsPushProperties();
1055 int Layer::clip_tree_index() const {
1056 if (!layer_tree_host_ ||
1057 layer_tree_host_->property_trees()->sequence_number !=
1058 property_tree_sequence_number_) {
1059 return -1;
1061 return clip_tree_index_;
1064 void Layer::SetEffectTreeIndex(int index) {
1065 DCHECK(IsPropertyChangeAllowed());
1066 if (effect_tree_index_ == index)
1067 return;
1068 effect_tree_index_ = index;
1069 SetNeedsPushProperties();
1072 int Layer::effect_tree_index() const {
1073 if (!layer_tree_host_ ||
1074 layer_tree_host_->property_trees()->sequence_number !=
1075 property_tree_sequence_number_) {
1076 return -1;
1078 return effect_tree_index_;
1081 void Layer::InvalidatePropertyTreesIndices() {
1082 int invalid_property_tree_index = -1;
1083 SetTransformTreeIndex(invalid_property_tree_index);
1084 SetClipTreeIndex(invalid_property_tree_index);
1085 SetEffectTreeIndex(invalid_property_tree_index);
1088 void Layer::SetShouldFlattenTransform(bool should_flatten) {
1089 DCHECK(IsPropertyChangeAllowed());
1090 if (should_flatten_transform_ == should_flatten)
1091 return;
1092 should_flatten_transform_ = should_flatten;
1093 SetNeedsCommit();
1096 void Layer::SetIsDrawable(bool is_drawable) {
1097 DCHECK(IsPropertyChangeAllowed());
1098 if (is_drawable_ == is_drawable)
1099 return;
1101 is_drawable_ = is_drawable;
1102 UpdateDrawsContent(HasDrawableContent());
1105 void Layer::SetHideLayerAndSubtree(bool hide) {
1106 DCHECK(IsPropertyChangeAllowed());
1107 if (hide_layer_and_subtree_ == hide)
1108 return;
1110 hide_layer_and_subtree_ = hide;
1111 SetNeedsCommit();
1114 void Layer::SetNeedsDisplayRect(const gfx::Rect& dirty_rect) {
1115 if (dirty_rect.IsEmpty())
1116 return;
1118 SetNeedsPushProperties();
1119 update_rect_.Union(dirty_rect);
1121 if (DrawsContent())
1122 SetNeedsUpdate();
1125 bool Layer::DescendantIsFixedToContainerLayer() const {
1126 for (size_t i = 0; i < children_.size(); ++i) {
1127 if (children_[i]->position_constraint_.is_fixed_position() ||
1128 children_[i]->DescendantIsFixedToContainerLayer())
1129 return true;
1131 return false;
1134 void Layer::SetIsContainerForFixedPositionLayers(bool container) {
1135 if (is_container_for_fixed_position_layers_ == container)
1136 return;
1137 is_container_for_fixed_position_layers_ = container;
1139 if (layer_tree_host_ && layer_tree_host_->CommitRequested())
1140 return;
1142 // Only request a commit if we have a fixed positioned descendant.
1143 if (DescendantIsFixedToContainerLayer())
1144 SetNeedsCommit();
1147 void Layer::SetPositionConstraint(const LayerPositionConstraint& constraint) {
1148 DCHECK(IsPropertyChangeAllowed());
1149 if (position_constraint_ == constraint)
1150 return;
1151 position_constraint_ = constraint;
1152 SetNeedsCommit();
1155 static void RunCopyCallbackOnMainThread(scoped_ptr<CopyOutputRequest> request,
1156 scoped_ptr<CopyOutputResult> result) {
1157 request->SendResult(result.Pass());
1160 static void PostCopyCallbackToMainThread(
1161 scoped_refptr<base::SingleThreadTaskRunner> main_thread_task_runner,
1162 scoped_ptr<CopyOutputRequest> request,
1163 scoped_ptr<CopyOutputResult> result) {
1164 main_thread_task_runner->PostTask(FROM_HERE,
1165 base::Bind(&RunCopyCallbackOnMainThread,
1166 base::Passed(&request),
1167 base::Passed(&result)));
1170 void Layer::PushPropertiesTo(LayerImpl* layer) {
1171 DCHECK(layer_tree_host_);
1173 // If we did not SavePaintProperties() for the layer this frame, then push the
1174 // real property values, not the paint property values.
1175 bool use_paint_properties = paint_properties_.source_frame_number ==
1176 layer_tree_host_->source_frame_number();
1178 layer->SetTransformOrigin(transform_origin_);
1179 layer->SetBackgroundColor(background_color_);
1180 layer->SetBounds(use_paint_properties ? paint_properties_.bounds
1181 : bounds_);
1183 if (frame_viewer_instrumentation::IsTracingLayerTreeSnapshots())
1184 layer->SetDebugInfo(TakeDebugInfo());
1186 layer->SetTransformTreeIndex(transform_tree_index());
1187 layer->SetEffectTreeIndex(effect_tree_index());
1188 layer->SetClipTreeIndex(clip_tree_index());
1189 layer->set_offset_to_transform_parent(offset_to_transform_parent_);
1190 layer->SetDoubleSided(double_sided_);
1191 layer->SetDrawsContent(DrawsContent());
1192 layer->SetHideLayerAndSubtree(hide_layer_and_subtree_);
1193 layer->SetHasRenderSurface(has_render_surface_);
1194 if (!layer->FilterIsAnimatingOnImplOnly() && !FilterIsAnimating())
1195 layer->SetFilters(filters_);
1196 DCHECK(!(FilterIsAnimating() && layer->FilterIsAnimatingOnImplOnly()));
1197 layer->SetBackgroundFilters(background_filters());
1198 layer->SetMasksToBounds(masks_to_bounds_);
1199 layer->SetShouldScrollOnMainThread(should_scroll_on_main_thread_);
1200 layer->SetHaveWheelEventHandlers(have_wheel_event_handlers_);
1201 layer->SetHaveScrollEventHandlers(have_scroll_event_handlers_);
1202 layer->SetNonFastScrollableRegion(non_fast_scrollable_region_);
1203 layer->SetTouchEventHandlerRegion(touch_event_handler_region_);
1204 layer->SetScrollBlocksOn(scroll_blocks_on_);
1205 layer->SetContentsOpaque(contents_opaque_);
1206 if (!layer->OpacityIsAnimatingOnImplOnly() && !OpacityIsAnimating())
1207 layer->SetOpacity(opacity_);
1208 DCHECK(!(OpacityIsAnimating() && layer->OpacityIsAnimatingOnImplOnly()));
1209 layer->SetBlendMode(blend_mode_);
1210 layer->SetIsRootForIsolatedGroup(is_root_for_isolated_group_);
1211 layer->SetPosition(position_);
1212 layer->SetIsContainerForFixedPositionLayers(
1213 IsContainerForFixedPositionLayers());
1214 layer->SetPositionConstraint(position_constraint_);
1215 layer->SetShouldFlattenTransform(should_flatten_transform_);
1216 layer->set_should_flatten_transform_from_property_tree(
1217 should_flatten_transform_from_property_tree_);
1218 layer->set_is_clipped(is_clipped_);
1219 layer->set_draw_blend_mode(draw_blend_mode_);
1220 layer->SetUseParentBackfaceVisibility(use_parent_backface_visibility_);
1221 if (!layer->TransformIsAnimatingOnImplOnly() && !TransformIsAnimating())
1222 layer->SetTransformAndInvertibility(transform_, transform_is_invertible_);
1223 DCHECK(!(TransformIsAnimating() && layer->TransformIsAnimatingOnImplOnly()));
1224 layer->Set3dSortingContextId(sorting_context_id_);
1225 layer->SetNumDescendantsThatDrawContent(num_descendants_that_draw_content_);
1227 layer->SetScrollClipLayer(scroll_clip_layer_id_);
1228 layer->set_user_scrollable_horizontal(user_scrollable_horizontal_);
1229 layer->set_user_scrollable_vertical(user_scrollable_vertical_);
1231 LayerImpl* scroll_parent = nullptr;
1232 if (scroll_parent_) {
1233 scroll_parent = layer->layer_tree_impl()->LayerById(scroll_parent_->id());
1234 DCHECK(scroll_parent);
1237 layer->SetScrollParent(scroll_parent);
1238 if (scroll_children_) {
1239 std::set<LayerImpl*>* scroll_children = new std::set<LayerImpl*>;
1240 for (std::set<Layer*>::iterator it = scroll_children_->begin();
1241 it != scroll_children_->end();
1242 ++it) {
1243 DCHECK_EQ((*it)->scroll_parent(), this);
1244 LayerImpl* scroll_child =
1245 layer->layer_tree_impl()->LayerById((*it)->id());
1246 DCHECK(scroll_child);
1247 scroll_children->insert(scroll_child);
1249 layer->SetScrollChildren(scroll_children);
1250 } else {
1251 layer->SetScrollChildren(nullptr);
1254 LayerImpl* clip_parent = nullptr;
1255 if (clip_parent_) {
1256 clip_parent =
1257 layer->layer_tree_impl()->LayerById(clip_parent_->id());
1258 DCHECK(clip_parent);
1261 layer->SetClipParent(clip_parent);
1262 if (clip_children_) {
1263 std::set<LayerImpl*>* clip_children = new std::set<LayerImpl*>;
1264 for (std::set<Layer*>::iterator it = clip_children_->begin();
1265 it != clip_children_->end(); ++it) {
1266 DCHECK_EQ((*it)->clip_parent(), this);
1267 LayerImpl* clip_child = layer->layer_tree_impl()->LayerById((*it)->id());
1268 DCHECK(clip_child);
1269 clip_children->insert(clip_child);
1271 layer->SetClipChildren(clip_children);
1272 } else {
1273 layer->SetClipChildren(nullptr);
1276 // When a scroll offset animation is interrupted the new scroll position on
1277 // the pending tree will clobber any impl-side scrolling occuring on the
1278 // active tree. To do so, avoid scrolling the pending tree along with it
1279 // instead of trying to undo that scrolling later.
1280 if (ScrollOffsetAnimationWasInterrupted())
1281 layer->PushScrollOffsetFromMainThreadAndClobberActiveValue(scroll_offset_);
1282 else
1283 layer->PushScrollOffsetFromMainThread(scroll_offset_);
1284 layer->SetScrollCompensationAdjustment(ScrollCompensationAdjustment());
1286 // Wrap the copy_requests_ in a PostTask to the main thread.
1287 bool had_copy_requests = !copy_requests_.empty();
1288 ScopedPtrVector<CopyOutputRequest> main_thread_copy_requests;
1289 for (ScopedPtrVector<CopyOutputRequest>::iterator it = copy_requests_.begin();
1290 it != copy_requests_.end();
1291 ++it) {
1292 scoped_refptr<base::SingleThreadTaskRunner> main_thread_task_runner =
1293 layer_tree_host()->proxy()->MainThreadTaskRunner();
1294 scoped_ptr<CopyOutputRequest> original_request = copy_requests_.take(it);
1295 const CopyOutputRequest& original_request_ref = *original_request;
1296 scoped_ptr<CopyOutputRequest> main_thread_request =
1297 CopyOutputRequest::CreateRelayRequest(
1298 original_request_ref,
1299 base::Bind(&PostCopyCallbackToMainThread,
1300 main_thread_task_runner,
1301 base::Passed(&original_request)));
1302 main_thread_copy_requests.push_back(main_thread_request.Pass());
1304 if (!copy_requests_.empty() && layer_tree_host_)
1305 layer_tree_host_->property_trees()->needs_rebuild = true;
1306 if (had_copy_requests)
1307 UpdateNumCopyRequestsForSubtree(false);
1308 copy_requests_.clear();
1309 layer->PassCopyRequests(&main_thread_copy_requests);
1311 // If the main thread commits multiple times before the impl thread actually
1312 // draws, then damage tracking will become incorrect if we simply clobber the
1313 // update_rect here. The LayerImpl's update_rect needs to accumulate (i.e.
1314 // union) any update changes that have occurred on the main thread.
1315 update_rect_.Union(layer->update_rect());
1316 layer->SetUpdateRect(update_rect_);
1318 layer->SetStackingOrderChanged(stacking_order_changed_);
1320 if (layer->layer_animation_controller() && layer_animation_controller_)
1321 layer_animation_controller_->PushAnimationUpdatesTo(
1322 layer->layer_animation_controller());
1324 if (frame_timing_requests_dirty_) {
1325 layer->SetFrameTimingRequests(frame_timing_requests_);
1326 frame_timing_requests_dirty_ = false;
1329 bool is_page_scale_layer = this == layer_tree_host()->page_scale_layer();
1330 bool parent_affected =
1331 layer->parent() && layer->parent()->IsAffectedByPageScale();
1332 layer->SetIsAffectedByPageScale(is_page_scale_layer || parent_affected);
1334 // Reset any state that should be cleared for the next update.
1335 stacking_order_changed_ = false;
1336 update_rect_ = gfx::Rect();
1338 needs_push_properties_ = false;
1339 num_dependents_need_push_properties_ = 0;
1342 scoped_ptr<LayerImpl> Layer::CreateLayerImpl(LayerTreeImpl* tree_impl) {
1343 return LayerImpl::Create(tree_impl, layer_id_,
1344 new LayerImpl::SyncedScrollOffset);
1347 bool Layer::DrawsContent() const {
1348 return draws_content_;
1351 bool Layer::HasDrawableContent() const {
1352 return is_drawable_;
1355 void Layer::UpdateDrawsContent(bool has_drawable_content) {
1356 bool draws_content = has_drawable_content;
1357 DCHECK(is_drawable_ || !has_drawable_content);
1358 if (draws_content == draws_content_)
1359 return;
1361 if (HasDelegatedContent()) {
1362 // Layers with delegated content need to be treated as if they have as
1363 // many children as the number of layers they own delegated quads for.
1364 // Since we don't know this number right now, we choose one that acts like
1365 // infinity for our purposes.
1366 AddDrawableDescendants(draws_content ? 1000 : -1000);
1369 if (parent())
1370 parent()->AddDrawableDescendants(draws_content ? 1 : -1);
1372 draws_content_ = draws_content;
1373 SetNeedsCommit();
1376 int Layer::NumDescendantsThatDrawContent() const {
1377 return num_descendants_that_draw_content_;
1380 void Layer::SavePaintProperties() {
1381 DCHECK(layer_tree_host_);
1383 // TODO(reveman): Save all layer properties that we depend on not
1384 // changing until PushProperties() has been called. crbug.com/231016
1385 paint_properties_.bounds = bounds_;
1386 paint_properties_.source_frame_number =
1387 layer_tree_host_->source_frame_number();
1390 bool Layer::Update() {
1391 DCHECK(layer_tree_host_);
1392 DCHECK_EQ(layer_tree_host_->source_frame_number(),
1393 paint_properties_.source_frame_number) <<
1394 "SavePaintProperties must be called for any layer that is painted.";
1395 return false;
1398 bool Layer::NeedMoreUpdates() {
1399 return false;
1402 bool Layer::IsSuitableForGpuRasterization() const {
1403 return true;
1406 scoped_refptr<base::trace_event::ConvertableToTraceFormat>
1407 Layer::TakeDebugInfo() {
1408 if (client_)
1409 return client_->TakeDebugInfo();
1410 else
1411 return nullptr;
1414 void Layer::SetHasRenderSurface(bool has_render_surface) {
1415 if (has_render_surface_ == has_render_surface)
1416 return;
1417 has_render_surface_ = has_render_surface;
1418 // We do not need SetNeedsCommit here, since this is only ever called
1419 // during a commit, from CalculateDrawProperties.
1420 SetNeedsPushProperties();
1421 layer_tree_host_->property_trees()->needs_rebuild = true;
1424 void Layer::CreateRenderSurface() {
1425 DCHECK(!render_surface_);
1426 render_surface_ = make_scoped_ptr(new RenderSurface(this));
1429 void Layer::ClearRenderSurface() {
1430 render_surface_ = nullptr;
1433 void Layer::ClearRenderSurfaceLayerList() {
1434 if (render_surface_)
1435 render_surface_->ClearLayerLists();
1438 gfx::ScrollOffset Layer::ScrollOffsetForAnimation() const {
1439 return CurrentScrollOffset();
1442 // On<Property>Animated is called due to an ongoing accelerated animation.
1443 // Since this animation is also being run on the compositor thread, there
1444 // is no need to request a commit to push this value over, so the value is
1445 // set directly rather than by calling Set<Property>.
1446 void Layer::OnFilterAnimated(const FilterOperations& filters) {
1447 filters_ = filters;
1450 void Layer::OnOpacityAnimated(float opacity) {
1451 opacity_ = opacity;
1452 if (layer_tree_host_) {
1453 if (EffectNode* node = layer_tree_host_->property_trees()->effect_tree.Node(
1454 effect_tree_index())) {
1455 if (node->owner_id == id()) {
1456 node->data.opacity = opacity;
1457 layer_tree_host_->property_trees()->effect_tree.set_needs_update(true);
1463 void Layer::OnTransformAnimated(const gfx::Transform& transform) {
1464 if (transform_ == transform)
1465 return;
1466 transform_ = transform;
1467 transform_is_invertible_ = transform.IsInvertible();
1468 if (layer_tree_host_) {
1469 if (TransformNode* node =
1470 layer_tree_host_->property_trees()->transform_tree.Node(
1471 transform_tree_index())) {
1472 if (node->owner_id == id()) {
1473 node->data.local = transform;
1474 node->data.needs_local_transform_update = true;
1475 node->data.is_animated = true;
1476 layer_tree_host_->property_trees()->transform_tree.set_needs_update(
1477 true);
1483 void Layer::OnScrollOffsetAnimated(const gfx::ScrollOffset& scroll_offset) {
1484 // Do nothing. Scroll deltas will be sent from the compositor thread back
1485 // to the main thread in the same manner as during non-animated
1486 // compositor-driven scrolling.
1489 void Layer::OnAnimationWaitingForDeletion() {
1490 // Animations are only deleted during PushProperties.
1491 SetNeedsPushProperties();
1494 void Layer::OnTransformIsPotentiallyAnimatingChanged(bool is_animating) {
1495 if (!layer_tree_host_)
1496 return;
1497 TransformTree& transform_tree =
1498 layer_tree_host_->property_trees()->transform_tree;
1499 TransformNode* node = transform_tree.Node(transform_tree_index());
1500 if (!node)
1501 return;
1503 if (node->owner_id == id()) {
1504 node->data.is_animated = is_animating;
1505 transform_tree.set_needs_update(true);
1509 bool Layer::IsActive() const {
1510 return true;
1513 bool Layer::AddAnimation(scoped_ptr <Animation> animation) {
1514 DCHECK(layer_animation_controller_);
1515 if (!layer_animation_controller_->animation_registrar())
1516 return false;
1518 if (animation->target_property() == Animation::SCROLL_OFFSET &&
1519 !layer_animation_controller_->animation_registrar()
1520 ->supports_scroll_animations())
1521 return false;
1523 UMA_HISTOGRAM_BOOLEAN("Renderer.AnimationAddedToOrphanLayer",
1524 !layer_tree_host_);
1525 layer_animation_controller_->AddAnimation(animation.Pass());
1526 SetNeedsCommit();
1527 return true;
1530 void Layer::PauseAnimation(int animation_id, double time_offset) {
1531 DCHECK(layer_animation_controller_);
1532 layer_animation_controller_->PauseAnimation(
1533 animation_id, base::TimeDelta::FromSecondsD(time_offset));
1534 SetNeedsCommit();
1537 void Layer::RemoveAnimation(int animation_id) {
1538 DCHECK(layer_animation_controller_);
1539 layer_animation_controller_->RemoveAnimation(animation_id);
1540 SetNeedsCommit();
1543 void Layer::RemoveAnimation(int animation_id,
1544 Animation::TargetProperty property) {
1545 DCHECK(layer_animation_controller_);
1546 layer_animation_controller_->RemoveAnimation(animation_id, property);
1547 SetNeedsCommit();
1550 void Layer::SetLayerAnimationControllerForTest(
1551 scoped_refptr<LayerAnimationController> controller) {
1552 DCHECK(layer_animation_controller_);
1553 layer_animation_controller_->RemoveValueObserver(this);
1554 layer_animation_controller_ = controller;
1555 layer_animation_controller_->AddValueObserver(this);
1556 SetNeedsCommit();
1559 bool Layer::HasActiveAnimation() const {
1560 DCHECK(layer_tree_host_);
1561 return layer_animation_controller_
1562 ? layer_animation_controller_->HasActiveAnimation()
1563 : layer_tree_host_->HasActiveAnimation(this);
1566 void Layer::RegisterForAnimations(AnimationRegistrar* registrar) {
1567 if (layer_animation_controller_)
1568 layer_animation_controller_->SetAnimationRegistrar(registrar);
1571 void Layer::AddLayerAnimationEventObserver(
1572 LayerAnimationEventObserver* animation_observer) {
1573 DCHECK(layer_animation_controller_);
1574 layer_animation_controller_->AddEventObserver(animation_observer);
1577 void Layer::RemoveLayerAnimationEventObserver(
1578 LayerAnimationEventObserver* animation_observer) {
1579 DCHECK(layer_animation_controller_);
1580 layer_animation_controller_->RemoveEventObserver(animation_observer);
1583 ScrollbarLayerInterface* Layer::ToScrollbarLayer() {
1584 return nullptr;
1587 RenderingStatsInstrumentation* Layer::rendering_stats_instrumentation() const {
1588 return layer_tree_host_->rendering_stats_instrumentation();
1591 void Layer::RemoveFromScrollTree() {
1592 if (scroll_children_.get()) {
1593 std::set<Layer*> copy = *scroll_children_;
1594 for (std::set<Layer*>::iterator it = copy.begin(); it != copy.end(); ++it)
1595 (*it)->SetScrollParent(nullptr);
1598 DCHECK(!scroll_children_);
1599 SetScrollParent(nullptr);
1602 void Layer::RemoveFromClipTree() {
1603 if (clip_children_.get()) {
1604 std::set<Layer*> copy = *clip_children_;
1605 for (std::set<Layer*>::iterator it = copy.begin(); it != copy.end(); ++it)
1606 (*it)->SetClipParent(nullptr);
1609 DCHECK(!clip_children_);
1610 SetClipParent(nullptr);
1613 void Layer::AddDrawableDescendants(int num) {
1614 DCHECK_GE(num_descendants_that_draw_content_, 0);
1615 DCHECK_GE(num_descendants_that_draw_content_ + num, 0);
1616 if (num == 0)
1617 return;
1618 num_descendants_that_draw_content_ += num;
1619 SetNeedsCommit();
1620 if (parent())
1621 parent()->AddDrawableDescendants(num);
1624 void Layer::RunMicroBenchmark(MicroBenchmark* benchmark) {
1625 benchmark->RunOnLayer(this);
1628 bool Layer::HasDelegatedContent() const {
1629 return false;
1632 void Layer::SetFrameTimingRequests(
1633 const std::vector<FrameTimingRequest>& requests) {
1634 // TODO(vmpstr): Early out if there are no changes earlier in the call stack.
1635 if (requests == frame_timing_requests_)
1636 return;
1637 frame_timing_requests_ = requests;
1638 frame_timing_requests_dirty_ = true;
1639 SetNeedsCommit();
1642 void Layer::DidBeginTracing() {
1643 // We'll be dumping layer trees as part of trace, so make sure
1644 // PushPropertiesTo() propagates layer debug info to the impl
1645 // side -- otherwise this won't happen for the the layers that
1646 // remain unchanged since tracing started.
1647 SetNeedsPushProperties();
1650 void Layer::set_visited(bool visited) {
1651 visited_tracker_ =
1652 visited ? layer_tree_host()->meta_information_sequence_number() : 0;
1655 bool Layer::visited() {
1656 return visited_tracker_ ==
1657 layer_tree_host()->meta_information_sequence_number();
1660 void Layer::set_layer_or_descendant_is_drawn(
1661 bool layer_or_descendant_is_drawn) {
1662 layer_or_descendant_is_drawn_tracker_ =
1663 layer_or_descendant_is_drawn
1664 ? layer_tree_host()->meta_information_sequence_number()
1665 : 0;
1668 bool Layer::layer_or_descendant_is_drawn() {
1669 return layer_or_descendant_is_drawn_tracker_ ==
1670 layer_tree_host()->meta_information_sequence_number();
1673 void Layer::set_sorted_for_recursion(bool sorted_for_recursion) {
1674 sorted_for_recursion_tracker_ =
1675 sorted_for_recursion
1676 ? layer_tree_host()->meta_information_sequence_number()
1677 : 0;
1680 bool Layer::sorted_for_recursion() {
1681 return sorted_for_recursion_tracker_ ==
1682 layer_tree_host()->meta_information_sequence_number();
1685 } // namespace cc