Update V8 to version 4.7.44.
[chromium-blink-merge.git] / cc / layers / layer.cc
blobc2a754e130d6a5f2c975586b2b7497f3e0903a32
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/draw_property_utils.h"
28 #include "cc/trees/layer_tree_host.h"
29 #include "cc/trees/layer_tree_impl.h"
30 #include "third_party/skia/include/core/SkImageFilter.h"
31 #include "ui/gfx/geometry/rect_conversions.h"
32 #include "ui/gfx/geometry/vector2d_conversions.h"
34 namespace cc {
36 base::StaticAtomicSequenceNumber g_next_layer_id;
38 scoped_refptr<Layer> Layer::Create(const LayerSettings& settings) {
39 return make_scoped_refptr(new Layer(settings));
42 Layer::Layer(const LayerSettings& settings)
43 : needs_push_properties_(false),
44 num_dependents_need_push_properties_(0),
45 stacking_order_changed_(false),
46 // Layer IDs start from 1.
47 layer_id_(g_next_layer_id.GetNext() + 1),
48 ignore_set_needs_commit_(false),
49 sorting_context_id_(0),
50 parent_(nullptr),
51 layer_tree_host_(nullptr),
52 scroll_clip_layer_id_(INVALID_ID),
53 num_descendants_that_draw_content_(0),
54 transform_tree_index_(-1),
55 effect_tree_index_(-1),
56 clip_tree_index_(-1),
57 property_tree_sequence_number_(-1),
58 num_layer_or_descendants_with_copy_request_(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 num_unclipped_descendants_(0),
93 frame_timing_requests_dirty_(false) {
94 if (!settings.use_compositor_animation_timelines) {
95 layer_animation_controller_ = LayerAnimationController::Create(layer_id_);
96 layer_animation_controller_->AddValueObserver(this);
97 layer_animation_controller_->set_value_provider(this);
101 Layer::~Layer() {
102 // Our parent should be holding a reference to us so there should be no
103 // way for us to be destroyed while we still have a parent.
104 DCHECK(!parent());
105 // Similarly we shouldn't have a layer tree host since it also keeps a
106 // reference to us.
107 DCHECK(!layer_tree_host());
109 if (layer_animation_controller_) {
110 layer_animation_controller_->RemoveValueObserver(this);
111 layer_animation_controller_->remove_value_provider(this);
114 RemoveFromScrollTree();
115 RemoveFromClipTree();
117 // Remove the parent reference from all children and dependents.
118 RemoveAllChildren();
119 if (mask_layer_.get()) {
120 DCHECK_EQ(this, mask_layer_->parent());
121 mask_layer_->RemoveFromParent();
123 if (replica_layer_.get()) {
124 DCHECK_EQ(this, replica_layer_->parent());
125 replica_layer_->RemoveFromParent();
129 void Layer::SetLayerTreeHost(LayerTreeHost* host) {
130 if (layer_tree_host_ == host)
131 return;
133 if (layer_tree_host_) {
134 layer_tree_host_->property_trees()->needs_rebuild = true;
135 layer_tree_host_->UnregisterLayer(this);
137 if (host) {
138 host->property_trees()->needs_rebuild = true;
139 host->RegisterLayer(this);
142 InvalidatePropertyTreesIndices();
144 layer_tree_host_ = host;
146 // When changing hosts, the layer needs to commit its properties to the impl
147 // side for the new host.
148 SetNeedsPushProperties();
150 for (size_t i = 0; i < children_.size(); ++i)
151 children_[i]->SetLayerTreeHost(host);
153 if (mask_layer_.get())
154 mask_layer_->SetLayerTreeHost(host);
155 if (replica_layer_.get())
156 replica_layer_->SetLayerTreeHost(host);
158 if (host)
159 RegisterForAnimations(host->animation_registrar());
161 bool has_any_animation = false;
162 if (layer_animation_controller_)
163 has_any_animation = layer_animation_controller_->has_any_animation();
164 else if (layer_tree_host_)
165 has_any_animation = layer_tree_host_->HasAnyAnimation(this);
167 if (host && has_any_animation)
168 host->SetNeedsCommit();
171 void Layer::SetNeedsUpdate() {
172 if (layer_tree_host_ && !ignore_set_needs_commit_)
173 layer_tree_host_->SetNeedsUpdateLayers();
176 void Layer::SetNeedsCommit() {
177 if (!layer_tree_host_)
178 return;
180 SetNeedsPushProperties();
181 layer_tree_host_->property_trees()->needs_rebuild = true;
183 if (ignore_set_needs_commit_)
184 return;
186 layer_tree_host_->SetNeedsCommit();
189 void Layer::SetNeedsCommitNoRebuild() {
190 if (!layer_tree_host_)
191 return;
193 SetNeedsPushProperties();
195 if (ignore_set_needs_commit_)
196 return;
198 layer_tree_host_->SetNeedsCommit();
201 void Layer::SetNeedsFullTreeSync() {
202 if (!layer_tree_host_)
203 return;
205 layer_tree_host_->SetNeedsFullTreeSync();
208 void Layer::SetNextCommitWaitsForActivation() {
209 if (!layer_tree_host_)
210 return;
212 layer_tree_host_->SetNextCommitWaitsForActivation();
215 void Layer::SetNeedsPushProperties() {
216 if (needs_push_properties_)
217 return;
218 if (!parent_should_know_need_push_properties() && parent_)
219 parent_->AddDependentNeedsPushProperties();
220 needs_push_properties_ = true;
223 void Layer::AddDependentNeedsPushProperties() {
224 DCHECK_GE(num_dependents_need_push_properties_, 0);
226 if (!parent_should_know_need_push_properties() && parent_)
227 parent_->AddDependentNeedsPushProperties();
229 num_dependents_need_push_properties_++;
232 void Layer::RemoveDependentNeedsPushProperties() {
233 num_dependents_need_push_properties_--;
234 DCHECK_GE(num_dependents_need_push_properties_, 0);
236 if (!parent_should_know_need_push_properties() && parent_)
237 parent_->RemoveDependentNeedsPushProperties();
240 bool Layer::IsPropertyChangeAllowed() const {
241 if (!layer_tree_host_)
242 return true;
244 if (!layer_tree_host_->settings().strict_layer_property_change_checking)
245 return true;
247 return !layer_tree_host_->in_paint_layer_contents();
250 skia::RefPtr<SkPicture> Layer::GetPicture() const {
251 return skia::RefPtr<SkPicture>();
254 void Layer::SetParent(Layer* layer) {
255 DCHECK(!layer || !layer->HasAncestor(this));
257 if (parent_should_know_need_push_properties()) {
258 if (parent_)
259 parent_->RemoveDependentNeedsPushProperties();
260 if (layer)
261 layer->AddDependentNeedsPushProperties();
264 parent_ = layer;
265 SetLayerTreeHost(parent_ ? parent_->layer_tree_host() : nullptr);
267 if (!layer_tree_host_)
268 return;
270 layer_tree_host_->property_trees()->needs_rebuild = true;
273 void Layer::AddChild(scoped_refptr<Layer> child) {
274 InsertChild(child, children_.size());
277 void Layer::InsertChild(scoped_refptr<Layer> child, size_t index) {
278 DCHECK(IsPropertyChangeAllowed());
279 child->RemoveFromParent();
280 AddDrawableDescendants(child->NumDescendantsThatDrawContent() +
281 (child->DrawsContent() ? 1 : 0));
282 child->SetParent(this);
283 child->stacking_order_changed_ = true;
285 index = std::min(index, children_.size());
286 children_.insert(children_.begin() + index, child);
287 SetNeedsFullTreeSync();
290 void Layer::RemoveFromParent() {
291 DCHECK(IsPropertyChangeAllowed());
292 if (parent_)
293 parent_->RemoveChildOrDependent(this);
296 void Layer::RemoveChildOrDependent(Layer* child) {
297 if (mask_layer_.get() == child) {
298 mask_layer_->SetParent(nullptr);
299 mask_layer_ = nullptr;
300 SetNeedsFullTreeSync();
301 return;
303 if (replica_layer_.get() == child) {
304 replica_layer_->SetParent(nullptr);
305 replica_layer_ = nullptr;
306 SetNeedsFullTreeSync();
307 return;
310 for (LayerList::iterator iter = children_.begin();
311 iter != children_.end();
312 ++iter) {
313 if (iter->get() != child)
314 continue;
316 child->SetParent(nullptr);
317 AddDrawableDescendants(-child->NumDescendantsThatDrawContent() -
318 (child->DrawsContent() ? 1 : 0));
319 children_.erase(iter);
320 SetNeedsFullTreeSync();
321 return;
325 void Layer::ReplaceChild(Layer* reference, scoped_refptr<Layer> new_layer) {
326 DCHECK(reference);
327 DCHECK_EQ(reference->parent(), this);
328 DCHECK(IsPropertyChangeAllowed());
330 if (reference == new_layer.get())
331 return;
333 // Find the index of |reference| in |children_|.
334 auto reference_it =
335 std::find_if(children_.begin(), children_.end(),
336 [reference](const scoped_refptr<Layer>& layer) {
337 return layer.get() == reference;
339 DCHECK(reference_it != children_.end());
340 size_t reference_index = reference_it - children_.begin();
341 reference->RemoveFromParent();
343 if (new_layer.get()) {
344 new_layer->RemoveFromParent();
345 InsertChild(new_layer, reference_index);
349 void Layer::SetBounds(const gfx::Size& size) {
350 DCHECK(IsPropertyChangeAllowed());
351 if (bounds() == size)
352 return;
353 bounds_ = size;
355 if (!layer_tree_host_)
356 return;
358 if (ClipNode* clip_node = layer_tree_host_->property_trees()->clip_tree.Node(
359 clip_tree_index())) {
360 if (clip_node->owner_id == id()) {
361 clip_node->data.clip.set_size(size);
362 layer_tree_host_->property_trees()->clip_tree.set_needs_update(true);
366 SetNeedsCommitNoRebuild();
369 Layer* Layer::RootLayer() {
370 Layer* layer = this;
371 while (layer->parent())
372 layer = layer->parent();
373 return layer;
376 void Layer::RemoveAllChildren() {
377 DCHECK(IsPropertyChangeAllowed());
378 while (children_.size()) {
379 Layer* layer = children_[0].get();
380 DCHECK_EQ(this, layer->parent());
381 layer->RemoveFromParent();
385 void Layer::SetChildren(const LayerList& children) {
386 DCHECK(IsPropertyChangeAllowed());
387 if (children == children_)
388 return;
390 RemoveAllChildren();
391 for (size_t i = 0; i < children.size(); ++i)
392 AddChild(children[i]);
395 bool Layer::HasAncestor(const Layer* ancestor) const {
396 for (const Layer* layer = parent(); layer; layer = layer->parent()) {
397 if (layer == ancestor)
398 return true;
400 return false;
403 void Layer::RequestCopyOfOutput(
404 scoped_ptr<CopyOutputRequest> request) {
405 DCHECK(IsPropertyChangeAllowed());
406 bool had_no_copy_requests = copy_requests_.empty();
407 if (void* source = request->source()) {
408 auto it = std::find_if(
409 copy_requests_.begin(), copy_requests_.end(),
410 [source](const CopyOutputRequest* x) { return x->source() == source; });
411 if (it != copy_requests_.end())
412 copy_requests_.erase(it);
414 if (request->IsEmpty())
415 return;
416 copy_requests_.push_back(request.Pass());
417 if (had_no_copy_requests) {
418 bool copy_request_added = true;
419 UpdateNumCopyRequestsForSubtree(copy_request_added);
421 SetNeedsCommit();
424 void Layer::UpdateNumCopyRequestsForSubtree(bool add) {
425 int change = add ? 1 : -1;
426 for (Layer* layer = this; layer; layer = layer->parent()) {
427 layer->num_layer_or_descendants_with_copy_request_ += change;
428 DCHECK_GE(layer->num_layer_or_descendants_with_copy_request_, 0);
432 void Layer::SetBackgroundColor(SkColor background_color) {
433 DCHECK(IsPropertyChangeAllowed());
434 if (background_color_ == background_color)
435 return;
436 background_color_ = background_color;
437 SetNeedsCommit();
440 SkColor Layer::SafeOpaqueBackgroundColor() const {
441 SkColor color = background_color();
442 if (SkColorGetA(color) == 255 && !contents_opaque()) {
443 color = SK_ColorTRANSPARENT;
444 } else if (SkColorGetA(color) != 255 && contents_opaque()) {
445 for (const Layer* layer = parent(); layer;
446 layer = layer->parent()) {
447 color = layer->background_color();
448 if (SkColorGetA(color) == 255)
449 break;
451 if (SkColorGetA(color) != 255)
452 color = layer_tree_host_->background_color();
453 if (SkColorGetA(color) != 255)
454 color = SkColorSetA(color, 255);
456 return color;
459 void Layer::SetMasksToBounds(bool masks_to_bounds) {
460 DCHECK(IsPropertyChangeAllowed());
461 if (masks_to_bounds_ == masks_to_bounds)
462 return;
463 masks_to_bounds_ = masks_to_bounds;
464 SetNeedsCommit();
467 void Layer::SetMaskLayer(Layer* mask_layer) {
468 DCHECK(IsPropertyChangeAllowed());
469 if (mask_layer_.get() == mask_layer)
470 return;
471 if (mask_layer_.get()) {
472 DCHECK_EQ(this, mask_layer_->parent());
473 mask_layer_->RemoveFromParent();
475 mask_layer_ = mask_layer;
476 if (mask_layer_.get()) {
477 DCHECK(!mask_layer_->parent());
478 mask_layer_->RemoveFromParent();
479 mask_layer_->SetParent(this);
480 mask_layer_->SetIsMask(true);
482 SetNeedsFullTreeSync();
485 void Layer::SetReplicaLayer(Layer* layer) {
486 DCHECK(IsPropertyChangeAllowed());
487 if (replica_layer_.get() == layer)
488 return;
489 if (replica_layer_.get()) {
490 DCHECK_EQ(this, replica_layer_->parent());
491 replica_layer_->RemoveFromParent();
493 replica_layer_ = layer;
494 if (replica_layer_.get()) {
495 DCHECK(!replica_layer_->parent());
496 replica_layer_->RemoveFromParent();
497 replica_layer_->SetParent(this);
499 SetNeedsFullTreeSync();
502 void Layer::SetFilters(const FilterOperations& filters) {
503 DCHECK(IsPropertyChangeAllowed());
504 if (filters_ == filters)
505 return;
506 filters_ = filters;
507 SetNeedsCommit();
510 bool Layer::FilterIsAnimating() const {
511 DCHECK(layer_tree_host_);
512 return layer_animation_controller_
513 ? layer_animation_controller_->IsCurrentlyAnimatingProperty(
514 Animation::FILTER,
515 LayerAnimationController::ObserverType::ACTIVE)
516 : layer_tree_host_->IsAnimatingFilterProperty(this);
519 bool Layer::HasPotentiallyRunningFilterAnimation() const {
520 if (layer_animation_controller_) {
521 return layer_animation_controller_->IsPotentiallyAnimatingProperty(
522 Animation::FILTER, LayerAnimationController::ObserverType::ACTIVE);
524 return layer_tree_host_->HasPotentiallyRunningFilterAnimation(this);
527 void Layer::SetBackgroundFilters(const FilterOperations& filters) {
528 DCHECK(IsPropertyChangeAllowed());
529 if (background_filters_ == filters)
530 return;
531 background_filters_ = filters;
532 SetNeedsCommit();
535 void Layer::SetOpacity(float opacity) {
536 DCHECK(IsPropertyChangeAllowed());
537 if (opacity_ == opacity)
538 return;
539 opacity_ = opacity;
540 SetNeedsCommit();
543 bool Layer::OpacityIsAnimating() const {
544 DCHECK(layer_tree_host_);
545 return layer_animation_controller_
546 ? layer_animation_controller_->IsCurrentlyAnimatingProperty(
547 Animation::OPACITY,
548 LayerAnimationController::ObserverType::ACTIVE)
549 : layer_tree_host_->IsAnimatingOpacityProperty(this);
552 bool Layer::HasPotentiallyRunningOpacityAnimation() const {
553 if (layer_animation_controller_) {
554 return layer_animation_controller_->IsPotentiallyAnimatingProperty(
555 Animation::OPACITY, LayerAnimationController::ObserverType::ACTIVE);
557 return layer_tree_host_->HasPotentiallyRunningOpacityAnimation(this);
560 bool Layer::OpacityCanAnimateOnImplThread() const {
561 return false;
564 void Layer::SetBlendMode(SkXfermode::Mode blend_mode) {
565 DCHECK(IsPropertyChangeAllowed());
566 if (blend_mode_ == blend_mode)
567 return;
569 // Allowing only blend modes that are defined in the CSS Compositing standard:
570 // http://dev.w3.org/fxtf/compositing-1/#blending
571 switch (blend_mode) {
572 case SkXfermode::kSrcOver_Mode:
573 case SkXfermode::kScreen_Mode:
574 case SkXfermode::kOverlay_Mode:
575 case SkXfermode::kDarken_Mode:
576 case SkXfermode::kLighten_Mode:
577 case SkXfermode::kColorDodge_Mode:
578 case SkXfermode::kColorBurn_Mode:
579 case SkXfermode::kHardLight_Mode:
580 case SkXfermode::kSoftLight_Mode:
581 case SkXfermode::kDifference_Mode:
582 case SkXfermode::kExclusion_Mode:
583 case SkXfermode::kMultiply_Mode:
584 case SkXfermode::kHue_Mode:
585 case SkXfermode::kSaturation_Mode:
586 case SkXfermode::kColor_Mode:
587 case SkXfermode::kLuminosity_Mode:
588 // supported blend modes
589 break;
590 case SkXfermode::kClear_Mode:
591 case SkXfermode::kSrc_Mode:
592 case SkXfermode::kDst_Mode:
593 case SkXfermode::kDstOver_Mode:
594 case SkXfermode::kSrcIn_Mode:
595 case SkXfermode::kDstIn_Mode:
596 case SkXfermode::kSrcOut_Mode:
597 case SkXfermode::kDstOut_Mode:
598 case SkXfermode::kSrcATop_Mode:
599 case SkXfermode::kDstATop_Mode:
600 case SkXfermode::kXor_Mode:
601 case SkXfermode::kPlus_Mode:
602 case SkXfermode::kModulate_Mode:
603 // Porter Duff Compositing Operators are not yet supported
604 // http://dev.w3.org/fxtf/compositing-1/#porterduffcompositingoperators
605 NOTREACHED();
606 return;
609 blend_mode_ = blend_mode;
610 SetNeedsCommit();
613 void Layer::SetIsRootForIsolatedGroup(bool root) {
614 DCHECK(IsPropertyChangeAllowed());
615 if (is_root_for_isolated_group_ == root)
616 return;
617 is_root_for_isolated_group_ = root;
618 SetNeedsCommit();
621 void Layer::SetContentsOpaque(bool opaque) {
622 DCHECK(IsPropertyChangeAllowed());
623 if (contents_opaque_ == opaque)
624 return;
625 contents_opaque_ = opaque;
626 SetNeedsCommit();
629 void Layer::SetPosition(const gfx::PointF& position) {
630 DCHECK(IsPropertyChangeAllowed());
631 if (position_ == position)
632 return;
633 position_ = position;
635 if (!layer_tree_host_)
636 return;
638 if (TransformNode* transform_node =
639 layer_tree_host_->property_trees()->transform_tree.Node(
640 transform_tree_index())) {
641 if (transform_node->owner_id == id()) {
642 transform_node->data.update_post_local_transform(position,
643 transform_origin());
644 transform_node->data.needs_local_transform_update = true;
645 layer_tree_host_->property_trees()->transform_tree.set_needs_update(true);
646 SetNeedsCommitNoRebuild();
647 return;
651 SetNeedsCommit();
654 bool Layer::IsContainerForFixedPositionLayers() const {
655 if (!transform_.IsIdentityOrTranslation())
656 return true;
657 if (parent_ && !parent_->transform_.IsIdentityOrTranslation())
658 return true;
659 return is_container_for_fixed_position_layers_;
662 bool Are2dAxisAligned(const gfx::Transform& a,
663 const gfx::Transform& b,
664 bool* is_invertible) {
665 if (a.IsScaleOrTranslation() && b.IsScaleOrTranslation()) {
666 *is_invertible = b.IsInvertible();
667 return true;
670 gfx::Transform inverse(gfx::Transform::kSkipInitialization);
671 *is_invertible = b.GetInverse(&inverse);
673 inverse *= a;
674 return inverse.Preserves2dAxisAlignment();
677 void Layer::SetTransform(const gfx::Transform& transform) {
678 DCHECK(IsPropertyChangeAllowed());
679 if (transform_ == transform)
680 return;
682 if (layer_tree_host_) {
683 if (TransformNode* transform_node =
684 layer_tree_host_->property_trees()->transform_tree.Node(
685 transform_tree_index())) {
686 if (transform_node->owner_id == id()) {
687 // We need to trigger a rebuild if we could have affected 2d axis
688 // alignment. We'll check to see if transform and transform_ are axis
689 // align with respect to one another.
690 bool invertible = false;
691 bool preserves_2d_axis_alignment =
692 Are2dAxisAligned(transform_, transform, &invertible);
693 transform_node->data.local = transform;
694 transform_node->data.needs_local_transform_update = true;
695 layer_tree_host_->property_trees()->transform_tree.set_needs_update(
696 true);
697 if (preserves_2d_axis_alignment)
698 SetNeedsCommitNoRebuild();
699 else
700 SetNeedsCommit();
701 transform_ = transform;
702 transform_is_invertible_ = invertible;
703 return;
708 transform_ = transform;
709 transform_is_invertible_ = transform.IsInvertible();
711 SetNeedsCommit();
714 void Layer::SetTransformOrigin(const gfx::Point3F& transform_origin) {
715 DCHECK(IsPropertyChangeAllowed());
716 if (transform_origin_ == transform_origin)
717 return;
718 transform_origin_ = transform_origin;
720 if (!layer_tree_host_)
721 return;
723 if (TransformNode* transform_node =
724 layer_tree_host_->property_trees()->transform_tree.Node(
725 transform_tree_index())) {
726 if (transform_node->owner_id == id()) {
727 transform_node->data.update_pre_local_transform(transform_origin);
728 transform_node->data.update_post_local_transform(position(),
729 transform_origin);
730 transform_node->data.needs_local_transform_update = true;
731 layer_tree_host_->property_trees()->transform_tree.set_needs_update(true);
732 SetNeedsCommitNoRebuild();
733 return;
737 SetNeedsCommit();
740 bool Layer::AnimationsPreserveAxisAlignment() const {
741 DCHECK(layer_tree_host_);
742 return layer_animation_controller_
743 ? layer_animation_controller_->AnimationsPreserveAxisAlignment()
744 : layer_tree_host_->AnimationsPreserveAxisAlignment(this);
747 bool Layer::TransformIsAnimating() const {
748 DCHECK(layer_tree_host_);
749 return layer_animation_controller_
750 ? layer_animation_controller_->IsCurrentlyAnimatingProperty(
751 Animation::TRANSFORM,
752 LayerAnimationController::ObserverType::ACTIVE)
753 : layer_tree_host_->IsAnimatingTransformProperty(this);
756 bool Layer::HasPotentiallyRunningTransformAnimation() const {
757 if (layer_animation_controller_) {
758 return layer_animation_controller_->IsPotentiallyAnimatingProperty(
759 Animation::TRANSFORM, LayerAnimationController::ObserverType::ACTIVE);
761 return layer_tree_host_->HasPotentiallyRunningTransformAnimation(this);
764 bool Layer::HasOnlyTranslationTransforms() const {
765 if (layer_animation_controller_) {
766 return layer_animation_controller_->HasOnlyTranslationTransforms(
767 LayerAnimationController::ObserverType::ACTIVE);
769 return layer_tree_host_->HasOnlyTranslationTransforms(this);
772 bool Layer::MaximumTargetScale(float* max_scale) const {
773 if (layer_animation_controller_) {
774 return layer_animation_controller_->MaximumTargetScale(
775 LayerAnimationController::ObserverType::ACTIVE, max_scale);
777 return layer_tree_host_->MaximumTargetScale(this, max_scale);
780 bool Layer::AnimationStartScale(float* start_scale) const {
781 if (layer_animation_controller_) {
782 return layer_animation_controller_->AnimationStartScale(
783 LayerAnimationController::ObserverType::ACTIVE, start_scale);
785 return layer_tree_host_->AnimationStartScale(this, start_scale);
788 bool Layer::HasAnyAnimationTargetingProperty(
789 Animation::TargetProperty property) const {
790 if (layer_animation_controller_)
791 return !!layer_animation_controller_->GetAnimation(property);
793 return layer_tree_host_->HasAnyAnimationTargetingProperty(this, property);
796 bool Layer::ScrollOffsetAnimationWasInterrupted() const {
797 DCHECK(layer_tree_host_);
798 return layer_animation_controller_
799 ? layer_animation_controller_
800 ->scroll_offset_animation_was_interrupted()
801 : layer_tree_host_->ScrollOffsetAnimationWasInterrupted(this);
804 void Layer::SetScrollParent(Layer* parent) {
805 DCHECK(IsPropertyChangeAllowed());
806 if (scroll_parent_ == parent)
807 return;
809 if (scroll_parent_)
810 scroll_parent_->RemoveScrollChild(this);
812 scroll_parent_ = parent;
814 if (scroll_parent_)
815 scroll_parent_->AddScrollChild(this);
817 SetNeedsCommit();
820 void Layer::AddScrollChild(Layer* child) {
821 if (!scroll_children_)
822 scroll_children_.reset(new std::set<Layer*>);
823 scroll_children_->insert(child);
824 if (layer_tree_host_ && !layer_tree_host_->needs_meta_info_recomputation()) {
825 num_children_with_scroll_parent_++;
827 SetNeedsCommit();
830 void Layer::RemoveScrollChild(Layer* child) {
831 scroll_children_->erase(child);
832 if (scroll_children_->empty())
833 scroll_children_ = nullptr;
834 if (layer_tree_host_ && !layer_tree_host_->needs_meta_info_recomputation()) {
835 num_children_with_scroll_parent_--;
836 DCHECK_GE(num_children_with_scroll_parent_, 0);
838 SetNeedsCommit();
841 void Layer::SetClipParent(Layer* ancestor) {
842 DCHECK(IsPropertyChangeAllowed());
843 if (clip_parent_ == ancestor)
844 return;
846 if (clip_parent_)
847 clip_parent_->RemoveClipChild(this);
849 clip_parent_ = ancestor;
851 if (clip_parent_)
852 clip_parent_->AddClipChild(this);
854 SetNeedsCommit();
855 if (layer_tree_host_)
856 layer_tree_host_->SetNeedsMetaInfoRecomputation(true);
859 void Layer::AddClipChild(Layer* child) {
860 if (!clip_children_)
861 clip_children_.reset(new std::set<Layer*>);
862 clip_children_->insert(child);
863 SetNeedsCommit();
866 void Layer::RemoveClipChild(Layer* child) {
867 clip_children_->erase(child);
868 if (clip_children_->empty())
869 clip_children_ = nullptr;
870 SetNeedsCommit();
873 void Layer::SetScrollOffset(const gfx::ScrollOffset& scroll_offset) {
874 DCHECK(IsPropertyChangeAllowed());
876 if (scroll_offset_ == scroll_offset)
877 return;
878 scroll_offset_ = scroll_offset;
880 if (!layer_tree_host_)
881 return;
883 if (TransformNode* transform_node =
884 layer_tree_host_->property_trees()->transform_tree.Node(
885 transform_tree_index())) {
886 if (transform_node->owner_id == id()) {
887 transform_node->data.scroll_offset = CurrentScrollOffset();
888 transform_node->data.needs_local_transform_update = true;
889 layer_tree_host_->property_trees()->transform_tree.set_needs_update(true);
890 SetNeedsCommitNoRebuild();
891 return;
895 SetNeedsCommit();
898 void Layer::SetScrollCompensationAdjustment(
899 const gfx::Vector2dF& scroll_compensation_adjustment) {
900 if (scroll_compensation_adjustment_ == scroll_compensation_adjustment)
901 return;
902 scroll_compensation_adjustment_ = scroll_compensation_adjustment;
903 SetNeedsCommit();
906 gfx::Vector2dF Layer::ScrollCompensationAdjustment() const {
907 return scroll_compensation_adjustment_;
910 void Layer::SetScrollOffsetFromImplSide(
911 const gfx::ScrollOffset& scroll_offset) {
912 DCHECK(IsPropertyChangeAllowed());
913 // This function only gets called during a BeginMainFrame, so there
914 // is no need to call SetNeedsUpdate here.
915 DCHECK(layer_tree_host_ && layer_tree_host_->CommitRequested());
916 if (scroll_offset_ == scroll_offset)
917 return;
918 scroll_offset_ = scroll_offset;
919 SetNeedsPushProperties();
921 bool needs_rebuild = true;
922 if (TransformNode* transform_node =
923 layer_tree_host_->property_trees()->transform_tree.Node(
924 transform_tree_index())) {
925 if (transform_node->owner_id == id()) {
926 transform_node->data.scroll_offset = CurrentScrollOffset();
927 transform_node->data.needs_local_transform_update = true;
928 layer_tree_host_->property_trees()->transform_tree.set_needs_update(true);
929 needs_rebuild = false;
933 if (needs_rebuild)
934 layer_tree_host_->property_trees()->needs_rebuild = true;
936 if (!did_scroll_callback_.is_null())
937 did_scroll_callback_.Run();
938 // The callback could potentially change the layer structure:
939 // "this" may have been destroyed during the process.
942 void Layer::SetScrollClipLayerId(int clip_layer_id) {
943 DCHECK(IsPropertyChangeAllowed());
944 if (scroll_clip_layer_id_ == clip_layer_id)
945 return;
946 scroll_clip_layer_id_ = clip_layer_id;
947 SetNeedsCommit();
950 void Layer::SetUserScrollable(bool horizontal, bool vertical) {
951 DCHECK(IsPropertyChangeAllowed());
952 if (user_scrollable_horizontal_ == horizontal &&
953 user_scrollable_vertical_ == vertical)
954 return;
955 user_scrollable_horizontal_ = horizontal;
956 user_scrollable_vertical_ = vertical;
957 SetNeedsCommit();
960 void Layer::SetShouldScrollOnMainThread(bool should_scroll_on_main_thread) {
961 DCHECK(IsPropertyChangeAllowed());
962 if (should_scroll_on_main_thread_ == should_scroll_on_main_thread)
963 return;
964 should_scroll_on_main_thread_ = should_scroll_on_main_thread;
965 SetNeedsCommit();
968 void Layer::SetHaveWheelEventHandlers(bool have_wheel_event_handlers) {
969 DCHECK(IsPropertyChangeAllowed());
970 if (have_wheel_event_handlers_ == have_wheel_event_handlers)
971 return;
973 have_wheel_event_handlers_ = have_wheel_event_handlers;
974 SetNeedsCommit();
977 void Layer::SetHaveScrollEventHandlers(bool have_scroll_event_handlers) {
978 DCHECK(IsPropertyChangeAllowed());
979 if (have_scroll_event_handlers_ == have_scroll_event_handlers)
980 return;
981 have_scroll_event_handlers_ = have_scroll_event_handlers;
982 SetNeedsCommit();
985 void Layer::SetNonFastScrollableRegion(const Region& region) {
986 DCHECK(IsPropertyChangeAllowed());
987 if (non_fast_scrollable_region_ == region)
988 return;
989 non_fast_scrollable_region_ = region;
990 SetNeedsCommit();
993 void Layer::SetTouchEventHandlerRegion(const Region& region) {
994 DCHECK(IsPropertyChangeAllowed());
995 if (touch_event_handler_region_ == region)
996 return;
998 touch_event_handler_region_ = region;
999 SetNeedsCommit();
1002 void Layer::SetScrollBlocksOn(ScrollBlocksOn scroll_blocks_on) {
1003 DCHECK(IsPropertyChangeAllowed());
1004 if (scroll_blocks_on_ == scroll_blocks_on)
1005 return;
1006 scroll_blocks_on_ = scroll_blocks_on;
1007 SetNeedsCommit();
1010 void Layer::SetForceRenderSurface(bool force) {
1011 DCHECK(IsPropertyChangeAllowed());
1012 if (force_render_surface_ == force)
1013 return;
1014 force_render_surface_ = force;
1015 SetNeedsCommit();
1018 void Layer::SetDoubleSided(bool double_sided) {
1019 DCHECK(IsPropertyChangeAllowed());
1020 if (double_sided_ == double_sided)
1021 return;
1022 double_sided_ = double_sided;
1023 SetNeedsCommit();
1026 void Layer::Set3dSortingContextId(int id) {
1027 DCHECK(IsPropertyChangeAllowed());
1028 if (id == sorting_context_id_)
1029 return;
1030 sorting_context_id_ = id;
1031 SetNeedsCommit();
1034 void Layer::SetTransformTreeIndex(int index) {
1035 DCHECK(IsPropertyChangeAllowed());
1036 if (transform_tree_index_ == index)
1037 return;
1038 transform_tree_index_ = index;
1039 SetNeedsPushProperties();
1042 int Layer::transform_tree_index() const {
1043 if (!layer_tree_host_ ||
1044 layer_tree_host_->property_trees()->sequence_number !=
1045 property_tree_sequence_number_) {
1046 return -1;
1048 return transform_tree_index_;
1051 void Layer::SetClipTreeIndex(int index) {
1052 DCHECK(IsPropertyChangeAllowed());
1053 if (clip_tree_index_ == index)
1054 return;
1055 clip_tree_index_ = index;
1056 SetNeedsPushProperties();
1059 int Layer::clip_tree_index() const {
1060 if (!layer_tree_host_ ||
1061 layer_tree_host_->property_trees()->sequence_number !=
1062 property_tree_sequence_number_) {
1063 return -1;
1065 return clip_tree_index_;
1068 void Layer::SetEffectTreeIndex(int index) {
1069 DCHECK(IsPropertyChangeAllowed());
1070 if (effect_tree_index_ == index)
1071 return;
1072 effect_tree_index_ = index;
1073 SetNeedsPushProperties();
1076 int Layer::effect_tree_index() const {
1077 if (!layer_tree_host_ ||
1078 layer_tree_host_->property_trees()->sequence_number !=
1079 property_tree_sequence_number_) {
1080 return -1;
1082 return effect_tree_index_;
1085 void Layer::InvalidatePropertyTreesIndices() {
1086 int invalid_property_tree_index = -1;
1087 SetTransformTreeIndex(invalid_property_tree_index);
1088 SetClipTreeIndex(invalid_property_tree_index);
1089 SetEffectTreeIndex(invalid_property_tree_index);
1092 void Layer::SetShouldFlattenTransform(bool should_flatten) {
1093 DCHECK(IsPropertyChangeAllowed());
1094 if (should_flatten_transform_ == should_flatten)
1095 return;
1096 should_flatten_transform_ = should_flatten;
1097 SetNeedsCommit();
1100 void Layer::SetIsDrawable(bool is_drawable) {
1101 DCHECK(IsPropertyChangeAllowed());
1102 if (is_drawable_ == is_drawable)
1103 return;
1105 is_drawable_ = is_drawable;
1106 UpdateDrawsContent(HasDrawableContent());
1109 void Layer::SetHideLayerAndSubtree(bool hide) {
1110 DCHECK(IsPropertyChangeAllowed());
1111 if (hide_layer_and_subtree_ == hide)
1112 return;
1114 hide_layer_and_subtree_ = hide;
1115 SetNeedsCommit();
1118 void Layer::SetNeedsDisplayRect(const gfx::Rect& dirty_rect) {
1119 if (dirty_rect.IsEmpty())
1120 return;
1122 SetNeedsPushProperties();
1123 update_rect_.Union(dirty_rect);
1125 if (DrawsContent())
1126 SetNeedsUpdate();
1129 bool Layer::DescendantIsFixedToContainerLayer() const {
1130 for (size_t i = 0; i < children_.size(); ++i) {
1131 if (children_[i]->position_constraint_.is_fixed_position() ||
1132 children_[i]->DescendantIsFixedToContainerLayer())
1133 return true;
1135 return false;
1138 void Layer::SetIsContainerForFixedPositionLayers(bool container) {
1139 if (is_container_for_fixed_position_layers_ == container)
1140 return;
1141 is_container_for_fixed_position_layers_ = container;
1143 if (layer_tree_host_ && layer_tree_host_->CommitRequested())
1144 return;
1146 // Only request a commit if we have a fixed positioned descendant.
1147 if (DescendantIsFixedToContainerLayer())
1148 SetNeedsCommit();
1151 void Layer::SetPositionConstraint(const LayerPositionConstraint& constraint) {
1152 DCHECK(IsPropertyChangeAllowed());
1153 if (position_constraint_ == constraint)
1154 return;
1155 position_constraint_ = constraint;
1156 SetNeedsCommit();
1159 static void RunCopyCallbackOnMainThread(scoped_ptr<CopyOutputRequest> request,
1160 scoped_ptr<CopyOutputResult> result) {
1161 request->SendResult(result.Pass());
1164 static void PostCopyCallbackToMainThread(
1165 scoped_refptr<base::SingleThreadTaskRunner> main_thread_task_runner,
1166 scoped_ptr<CopyOutputRequest> request,
1167 scoped_ptr<CopyOutputResult> result) {
1168 main_thread_task_runner->PostTask(FROM_HERE,
1169 base::Bind(&RunCopyCallbackOnMainThread,
1170 base::Passed(&request),
1171 base::Passed(&result)));
1174 void Layer::PushPropertiesTo(LayerImpl* layer) {
1175 DCHECK(layer_tree_host_);
1177 // If we did not SavePaintProperties() for the layer this frame, then push the
1178 // real property values, not the paint property values.
1179 bool use_paint_properties = paint_properties_.source_frame_number ==
1180 layer_tree_host_->source_frame_number();
1182 layer->SetTransformOrigin(transform_origin_);
1183 layer->SetBackgroundColor(background_color_);
1184 layer->SetBounds(use_paint_properties ? paint_properties_.bounds
1185 : bounds_);
1187 if (frame_viewer_instrumentation::IsTracingLayerTreeSnapshots())
1188 layer->SetDebugInfo(TakeDebugInfo());
1190 layer->SetTransformTreeIndex(transform_tree_index());
1191 layer->SetEffectTreeIndex(effect_tree_index());
1192 layer->SetClipTreeIndex(clip_tree_index());
1193 layer->set_offset_to_transform_parent(offset_to_transform_parent_);
1194 layer->SetDoubleSided(double_sided_);
1195 layer->SetDrawsContent(DrawsContent());
1196 layer->SetHideLayerAndSubtree(hide_layer_and_subtree_);
1197 layer->SetHasRenderSurface(has_render_surface_);
1198 if (!layer->FilterIsAnimatingOnImplOnly() && !FilterIsAnimating())
1199 layer->SetFilters(filters_);
1200 DCHECK(!(FilterIsAnimating() && layer->FilterIsAnimatingOnImplOnly()));
1201 layer->SetBackgroundFilters(background_filters());
1202 layer->SetMasksToBounds(masks_to_bounds_);
1203 layer->SetShouldScrollOnMainThread(should_scroll_on_main_thread_);
1204 layer->SetHaveWheelEventHandlers(have_wheel_event_handlers_);
1205 layer->SetHaveScrollEventHandlers(have_scroll_event_handlers_);
1206 layer->SetNonFastScrollableRegion(non_fast_scrollable_region_);
1207 layer->SetTouchEventHandlerRegion(touch_event_handler_region_);
1208 layer->SetScrollBlocksOn(scroll_blocks_on_);
1209 layer->SetContentsOpaque(contents_opaque_);
1210 if (!layer->OpacityIsAnimatingOnImplOnly() && !OpacityIsAnimating())
1211 layer->SetOpacity(opacity_);
1212 DCHECK(!(OpacityIsAnimating() && layer->OpacityIsAnimatingOnImplOnly()));
1213 layer->SetBlendMode(blend_mode_);
1214 layer->SetIsRootForIsolatedGroup(is_root_for_isolated_group_);
1215 layer->SetPosition(position_);
1216 layer->SetIsContainerForFixedPositionLayers(
1217 IsContainerForFixedPositionLayers());
1218 layer->SetPositionConstraint(position_constraint_);
1219 layer->SetShouldFlattenTransform(should_flatten_transform_);
1220 layer->set_should_flatten_transform_from_property_tree(
1221 should_flatten_transform_from_property_tree_);
1222 layer->set_is_clipped(is_clipped_);
1223 layer->set_draw_blend_mode(draw_blend_mode_);
1224 layer->SetUseParentBackfaceVisibility(use_parent_backface_visibility_);
1225 if (!layer->TransformIsAnimatingOnImplOnly() && !TransformIsAnimating())
1226 layer->SetTransformAndInvertibility(transform_, transform_is_invertible_);
1227 DCHECK(!(TransformIsAnimating() && layer->TransformIsAnimatingOnImplOnly()));
1228 layer->Set3dSortingContextId(sorting_context_id_);
1229 layer->SetNumDescendantsThatDrawContent(num_descendants_that_draw_content_);
1231 layer->SetScrollClipLayer(scroll_clip_layer_id_);
1232 layer->set_user_scrollable_horizontal(user_scrollable_horizontal_);
1233 layer->set_user_scrollable_vertical(user_scrollable_vertical_);
1235 LayerImpl* scroll_parent = nullptr;
1236 if (scroll_parent_) {
1237 scroll_parent = layer->layer_tree_impl()->LayerById(scroll_parent_->id());
1238 DCHECK(scroll_parent);
1241 layer->SetScrollParent(scroll_parent);
1242 if (scroll_children_) {
1243 std::set<LayerImpl*>* scroll_children = new std::set<LayerImpl*>;
1244 for (std::set<Layer*>::iterator it = scroll_children_->begin();
1245 it != scroll_children_->end();
1246 ++it) {
1247 DCHECK_EQ((*it)->scroll_parent(), this);
1248 LayerImpl* scroll_child =
1249 layer->layer_tree_impl()->LayerById((*it)->id());
1250 DCHECK(scroll_child);
1251 scroll_children->insert(scroll_child);
1253 layer->SetScrollChildren(scroll_children);
1254 } else {
1255 layer->SetScrollChildren(nullptr);
1258 LayerImpl* clip_parent = nullptr;
1259 if (clip_parent_) {
1260 clip_parent =
1261 layer->layer_tree_impl()->LayerById(clip_parent_->id());
1262 DCHECK(clip_parent);
1265 layer->SetClipParent(clip_parent);
1266 if (clip_children_) {
1267 std::set<LayerImpl*>* clip_children = new std::set<LayerImpl*>;
1268 for (std::set<Layer*>::iterator it = clip_children_->begin();
1269 it != clip_children_->end(); ++it) {
1270 DCHECK_EQ((*it)->clip_parent(), this);
1271 LayerImpl* clip_child = layer->layer_tree_impl()->LayerById((*it)->id());
1272 DCHECK(clip_child);
1273 clip_children->insert(clip_child);
1275 layer->SetClipChildren(clip_children);
1276 } else {
1277 layer->SetClipChildren(nullptr);
1280 // When a scroll offset animation is interrupted the new scroll position on
1281 // the pending tree will clobber any impl-side scrolling occuring on the
1282 // active tree. To do so, avoid scrolling the pending tree along with it
1283 // instead of trying to undo that scrolling later.
1284 if (ScrollOffsetAnimationWasInterrupted())
1285 layer->PushScrollOffsetFromMainThreadAndClobberActiveValue(scroll_offset_);
1286 else
1287 layer->PushScrollOffsetFromMainThread(scroll_offset_);
1288 layer->SetScrollCompensationAdjustment(ScrollCompensationAdjustment());
1290 // Wrap the copy_requests_ in a PostTask to the main thread.
1291 bool had_copy_requests = !copy_requests_.empty();
1292 ScopedPtrVector<CopyOutputRequest> main_thread_copy_requests;
1293 for (ScopedPtrVector<CopyOutputRequest>::iterator it = copy_requests_.begin();
1294 it != copy_requests_.end();
1295 ++it) {
1296 scoped_refptr<base::SingleThreadTaskRunner> main_thread_task_runner =
1297 layer_tree_host()->proxy()->MainThreadTaskRunner();
1298 scoped_ptr<CopyOutputRequest> original_request = copy_requests_.take(it);
1299 const CopyOutputRequest& original_request_ref = *original_request;
1300 scoped_ptr<CopyOutputRequest> main_thread_request =
1301 CopyOutputRequest::CreateRelayRequest(
1302 original_request_ref,
1303 base::Bind(&PostCopyCallbackToMainThread,
1304 main_thread_task_runner,
1305 base::Passed(&original_request)));
1306 main_thread_copy_requests.push_back(main_thread_request.Pass());
1308 if (!copy_requests_.empty() && layer_tree_host_)
1309 layer_tree_host_->property_trees()->needs_rebuild = true;
1310 if (had_copy_requests)
1311 UpdateNumCopyRequestsForSubtree(false);
1312 copy_requests_.clear();
1313 layer->PassCopyRequests(&main_thread_copy_requests);
1315 // If the main thread commits multiple times before the impl thread actually
1316 // draws, then damage tracking will become incorrect if we simply clobber the
1317 // update_rect here. The LayerImpl's update_rect needs to accumulate (i.e.
1318 // union) any update changes that have occurred on the main thread.
1319 update_rect_.Union(layer->update_rect());
1320 layer->SetUpdateRect(update_rect_);
1322 layer->SetStackingOrderChanged(stacking_order_changed_);
1324 if (layer->layer_animation_controller() && layer_animation_controller_)
1325 layer_animation_controller_->PushAnimationUpdatesTo(
1326 layer->layer_animation_controller());
1328 if (frame_timing_requests_dirty_) {
1329 layer->SetFrameTimingRequests(frame_timing_requests_);
1330 frame_timing_requests_dirty_ = false;
1333 bool is_page_scale_layer = this == layer_tree_host()->page_scale_layer();
1334 bool parent_affected =
1335 layer->parent() && layer->parent()->IsAffectedByPageScale();
1336 layer->SetIsAffectedByPageScale(is_page_scale_layer || parent_affected);
1338 // Reset any state that should be cleared for the next update.
1339 stacking_order_changed_ = false;
1340 update_rect_ = gfx::Rect();
1342 needs_push_properties_ = false;
1343 num_dependents_need_push_properties_ = 0;
1346 scoped_ptr<LayerImpl> Layer::CreateLayerImpl(LayerTreeImpl* tree_impl) {
1347 return LayerImpl::Create(tree_impl, layer_id_,
1348 new LayerImpl::SyncedScrollOffset);
1351 bool Layer::DrawsContent() const {
1352 return draws_content_;
1355 bool Layer::HasDrawableContent() const {
1356 return is_drawable_;
1359 void Layer::UpdateDrawsContent(bool has_drawable_content) {
1360 bool draws_content = has_drawable_content;
1361 DCHECK(is_drawable_ || !has_drawable_content);
1362 if (draws_content == draws_content_)
1363 return;
1365 if (HasDelegatedContent()) {
1366 // Layers with delegated content need to be treated as if they have as
1367 // many children as the number of layers they own delegated quads for.
1368 // Since we don't know this number right now, we choose one that acts like
1369 // infinity for our purposes.
1370 AddDrawableDescendants(draws_content ? 1000 : -1000);
1373 if (parent())
1374 parent()->AddDrawableDescendants(draws_content ? 1 : -1);
1376 draws_content_ = draws_content;
1377 SetNeedsCommit();
1380 int Layer::NumDescendantsThatDrawContent() const {
1381 return num_descendants_that_draw_content_;
1384 void Layer::SavePaintProperties() {
1385 DCHECK(layer_tree_host_);
1387 // TODO(reveman): Save all layer properties that we depend on not
1388 // changing until PushProperties() has been called. crbug.com/231016
1389 paint_properties_.bounds = bounds_;
1390 paint_properties_.source_frame_number =
1391 layer_tree_host_->source_frame_number();
1394 bool Layer::Update() {
1395 DCHECK(layer_tree_host_);
1396 DCHECK_EQ(layer_tree_host_->source_frame_number(),
1397 paint_properties_.source_frame_number) <<
1398 "SavePaintProperties must be called for any layer that is painted.";
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 gfx::ScrollOffset Layer::ScrollOffsetForAnimation() const {
1425 return CurrentScrollOffset();
1428 // On<Property>Animated is called due to an ongoing accelerated animation.
1429 // Since this animation is also being run on the compositor thread, there
1430 // is no need to request a commit to push this value over, so the value is
1431 // set directly rather than by calling Set<Property>.
1432 void Layer::OnFilterAnimated(const FilterOperations& filters) {
1433 filters_ = filters;
1436 void Layer::OnOpacityAnimated(float opacity) {
1437 opacity_ = opacity;
1438 if (layer_tree_host_) {
1439 if (EffectNode* node = layer_tree_host_->property_trees()->effect_tree.Node(
1440 effect_tree_index())) {
1441 if (node->owner_id == id()) {
1442 node->data.opacity = opacity;
1443 layer_tree_host_->property_trees()->effect_tree.set_needs_update(true);
1449 void Layer::OnTransformAnimated(const gfx::Transform& transform) {
1450 if (transform_ == transform)
1451 return;
1452 transform_ = transform;
1453 transform_is_invertible_ = transform.IsInvertible();
1454 if (layer_tree_host_) {
1455 if (TransformNode* node =
1456 layer_tree_host_->property_trees()->transform_tree.Node(
1457 transform_tree_index())) {
1458 if (node->owner_id == id()) {
1459 node->data.local = transform;
1460 node->data.needs_local_transform_update = true;
1461 node->data.is_animated = true;
1462 layer_tree_host_->property_trees()->transform_tree.set_needs_update(
1463 true);
1469 void Layer::OnScrollOffsetAnimated(const gfx::ScrollOffset& scroll_offset) {
1470 // Do nothing. Scroll deltas will be sent from the compositor thread back
1471 // to the main thread in the same manner as during non-animated
1472 // compositor-driven scrolling.
1475 void Layer::OnAnimationWaitingForDeletion() {
1476 // Animations are only deleted during PushProperties.
1477 SetNeedsPushProperties();
1480 void Layer::OnTransformIsPotentiallyAnimatingChanged(bool is_animating) {
1481 if (!layer_tree_host_)
1482 return;
1483 TransformTree& transform_tree =
1484 layer_tree_host_->property_trees()->transform_tree;
1485 TransformNode* node = transform_tree.Node(transform_tree_index());
1486 if (!node)
1487 return;
1489 if (node->owner_id == id()) {
1490 node->data.is_animated = is_animating;
1491 if (is_animating) {
1492 float maximum_target_scale = 0.f;
1493 node->data.local_maximum_animation_target_scale =
1494 MaximumTargetScale(&maximum_target_scale) ? maximum_target_scale
1495 : 0.f;
1497 float animation_start_scale = 0.f;
1498 node->data.local_starting_animation_scale =
1499 AnimationStartScale(&animation_start_scale) ? animation_start_scale
1500 : 0.f;
1502 node->data.has_only_translation_animations =
1503 HasOnlyTranslationTransforms();
1505 } else {
1506 node->data.local_maximum_animation_target_scale = 0.f;
1507 node->data.local_starting_animation_scale = 0.f;
1508 node->data.has_only_translation_animations = true;
1510 transform_tree.set_needs_update(true);
1514 bool Layer::IsActive() const {
1515 return true;
1518 bool Layer::AddAnimation(scoped_ptr <Animation> animation) {
1519 DCHECK(layer_animation_controller_);
1520 if (!layer_animation_controller_->animation_registrar())
1521 return false;
1523 if (animation->target_property() == Animation::SCROLL_OFFSET &&
1524 !layer_animation_controller_->animation_registrar()
1525 ->supports_scroll_animations())
1526 return false;
1528 UMA_HISTOGRAM_BOOLEAN("Renderer.AnimationAddedToOrphanLayer",
1529 !layer_tree_host_);
1530 layer_animation_controller_->AddAnimation(animation.Pass());
1531 SetNeedsCommit();
1532 return true;
1535 void Layer::PauseAnimation(int animation_id, double time_offset) {
1536 DCHECK(layer_animation_controller_);
1537 layer_animation_controller_->PauseAnimation(
1538 animation_id, base::TimeDelta::FromSecondsD(time_offset));
1539 SetNeedsCommit();
1542 void Layer::RemoveAnimation(int animation_id) {
1543 DCHECK(layer_animation_controller_);
1544 layer_animation_controller_->RemoveAnimation(animation_id);
1545 SetNeedsCommit();
1548 void Layer::RemoveAnimation(int animation_id,
1549 Animation::TargetProperty property) {
1550 DCHECK(layer_animation_controller_);
1551 layer_animation_controller_->RemoveAnimation(animation_id, property);
1552 SetNeedsCommit();
1555 void Layer::SetLayerAnimationControllerForTest(
1556 scoped_refptr<LayerAnimationController> controller) {
1557 DCHECK(layer_animation_controller_);
1558 layer_animation_controller_->RemoveValueObserver(this);
1559 layer_animation_controller_ = controller;
1560 layer_animation_controller_->AddValueObserver(this);
1561 SetNeedsCommit();
1564 bool Layer::HasActiveAnimation() const {
1565 DCHECK(layer_tree_host_);
1566 return layer_animation_controller_
1567 ? layer_animation_controller_->HasActiveAnimation()
1568 : layer_tree_host_->HasActiveAnimation(this);
1571 void Layer::RegisterForAnimations(AnimationRegistrar* registrar) {
1572 if (layer_animation_controller_)
1573 layer_animation_controller_->SetAnimationRegistrar(registrar);
1576 void Layer::AddLayerAnimationEventObserver(
1577 LayerAnimationEventObserver* animation_observer) {
1578 DCHECK(layer_animation_controller_);
1579 layer_animation_controller_->AddEventObserver(animation_observer);
1582 void Layer::RemoveLayerAnimationEventObserver(
1583 LayerAnimationEventObserver* animation_observer) {
1584 DCHECK(layer_animation_controller_);
1585 layer_animation_controller_->RemoveEventObserver(animation_observer);
1588 ScrollbarLayerInterface* Layer::ToScrollbarLayer() {
1589 return nullptr;
1592 RenderingStatsInstrumentation* Layer::rendering_stats_instrumentation() const {
1593 return layer_tree_host_->rendering_stats_instrumentation();
1596 void Layer::RemoveFromScrollTree() {
1597 if (scroll_children_.get()) {
1598 std::set<Layer*> copy = *scroll_children_;
1599 for (std::set<Layer*>::iterator it = copy.begin(); it != copy.end(); ++it)
1600 (*it)->SetScrollParent(nullptr);
1603 DCHECK(!scroll_children_);
1604 SetScrollParent(nullptr);
1607 void Layer::RemoveFromClipTree() {
1608 if (clip_children_.get()) {
1609 std::set<Layer*> copy = *clip_children_;
1610 for (std::set<Layer*>::iterator it = copy.begin(); it != copy.end(); ++it)
1611 (*it)->SetClipParent(nullptr);
1614 DCHECK(!clip_children_);
1615 SetClipParent(nullptr);
1618 void Layer::AddDrawableDescendants(int num) {
1619 DCHECK_GE(num_descendants_that_draw_content_, 0);
1620 DCHECK_GE(num_descendants_that_draw_content_ + num, 0);
1621 if (num == 0)
1622 return;
1623 num_descendants_that_draw_content_ += num;
1624 SetNeedsCommit();
1625 if (parent())
1626 parent()->AddDrawableDescendants(num);
1629 void Layer::RunMicroBenchmark(MicroBenchmark* benchmark) {
1630 benchmark->RunOnLayer(this);
1633 bool Layer::HasDelegatedContent() const {
1634 return false;
1637 void Layer::SetFrameTimingRequests(
1638 const std::vector<FrameTimingRequest>& requests) {
1639 // TODO(vmpstr): Early out if there are no changes earlier in the call stack.
1640 if (requests == frame_timing_requests_)
1641 return;
1642 frame_timing_requests_ = requests;
1643 frame_timing_requests_dirty_ = true;
1644 SetNeedsCommit();
1647 void Layer::DidBeginTracing() {
1648 // We'll be dumping layer trees as part of trace, so make sure
1649 // PushPropertiesTo() propagates layer debug info to the impl
1650 // side -- otherwise this won't happen for the the layers that
1651 // remain unchanged since tracing started.
1652 SetNeedsPushProperties();
1655 void Layer::set_visited(bool visited) {
1656 visited_tracker_ =
1657 visited ? layer_tree_host()->meta_information_sequence_number() : 0;
1660 bool Layer::visited() {
1661 return visited_tracker_ ==
1662 layer_tree_host()->meta_information_sequence_number();
1665 void Layer::set_layer_or_descendant_is_drawn(
1666 bool layer_or_descendant_is_drawn) {
1667 layer_or_descendant_is_drawn_tracker_ =
1668 layer_or_descendant_is_drawn
1669 ? layer_tree_host()->meta_information_sequence_number()
1670 : 0;
1673 bool Layer::layer_or_descendant_is_drawn() {
1674 return layer_or_descendant_is_drawn_tracker_ ==
1675 layer_tree_host()->meta_information_sequence_number();
1678 void Layer::set_sorted_for_recursion(bool sorted_for_recursion) {
1679 sorted_for_recursion_tracker_ =
1680 sorted_for_recursion
1681 ? layer_tree_host()->meta_information_sequence_number()
1682 : 0;
1685 bool Layer::sorted_for_recursion() {
1686 return sorted_for_recursion_tracker_ ==
1687 layer_tree_host()->meta_information_sequence_number();
1690 gfx::Transform Layer::draw_transform() const {
1691 DCHECK_NE(transform_tree_index_, -1);
1692 return DrawTransformFromPropertyTrees(
1693 this, layer_tree_host_->property_trees()->transform_tree);
1696 gfx::Transform Layer::screen_space_transform() const {
1697 DCHECK_NE(transform_tree_index_, -1);
1698 return ScreenSpaceTransformFromPropertyTrees(
1699 this, layer_tree_host_->property_trees()->transform_tree);
1702 } // namespace cc