Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / cc / layers / layer.cc
blobc9e53697b1fbf83080f7ea10e59eda8ab12c4309
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 should_flatten_transform_from_property_tree_(false),
60 is_clipped_(false),
61 should_scroll_on_main_thread_(false),
62 have_wheel_event_handlers_(false),
63 have_scroll_event_handlers_(false),
64 user_scrollable_horizontal_(true),
65 user_scrollable_vertical_(true),
66 is_root_for_isolated_group_(false),
67 is_container_for_fixed_position_layers_(false),
68 is_drawable_(false),
69 draws_content_(false),
70 hide_layer_and_subtree_(false),
71 masks_to_bounds_(false),
72 contents_opaque_(false),
73 double_sided_(true),
74 should_flatten_transform_(true),
75 use_parent_backface_visibility_(false),
76 force_render_surface_(false),
77 transform_is_invertible_(true),
78 has_render_surface_(false),
79 scroll_blocks_on_(SCROLL_BLOCKS_ON_NONE),
80 background_color_(0),
81 opacity_(1.f),
82 blend_mode_(SkXfermode::kSrcOver_Mode),
83 draw_blend_mode_(SkXfermode::kSrcOver_Mode),
84 scroll_parent_(nullptr),
85 layer_or_descendant_is_drawn_tracker_(0),
86 sorted_for_recursion_tracker_(0),
87 visited_tracker_(0),
88 clip_parent_(nullptr),
89 replica_layer_(nullptr),
90 client_(nullptr),
91 num_unclipped_descendants_(0),
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 DCHECK_GE(layer->num_layer_or_descendants_with_copy_request_, 0);
431 void Layer::SetBackgroundColor(SkColor background_color) {
432 DCHECK(IsPropertyChangeAllowed());
433 if (background_color_ == background_color)
434 return;
435 background_color_ = background_color;
436 SetNeedsCommit();
439 SkColor Layer::SafeOpaqueBackgroundColor() const {
440 SkColor color = background_color();
441 if (SkColorGetA(color) == 255 && !contents_opaque()) {
442 color = SK_ColorTRANSPARENT;
443 } else if (SkColorGetA(color) != 255 && contents_opaque()) {
444 for (const Layer* layer = parent(); layer;
445 layer = layer->parent()) {
446 color = layer->background_color();
447 if (SkColorGetA(color) == 255)
448 break;
450 if (SkColorGetA(color) != 255)
451 color = layer_tree_host_->background_color();
452 if (SkColorGetA(color) != 255)
453 color = SkColorSetA(color, 255);
455 return color;
458 void Layer::SetMasksToBounds(bool masks_to_bounds) {
459 DCHECK(IsPropertyChangeAllowed());
460 if (masks_to_bounds_ == masks_to_bounds)
461 return;
462 masks_to_bounds_ = masks_to_bounds;
463 SetNeedsCommit();
466 void Layer::SetMaskLayer(Layer* mask_layer) {
467 DCHECK(IsPropertyChangeAllowed());
468 if (mask_layer_.get() == mask_layer)
469 return;
470 if (mask_layer_.get()) {
471 DCHECK_EQ(this, mask_layer_->parent());
472 mask_layer_->RemoveFromParent();
474 mask_layer_ = mask_layer;
475 if (mask_layer_.get()) {
476 DCHECK(!mask_layer_->parent());
477 mask_layer_->RemoveFromParent();
478 mask_layer_->SetParent(this);
479 mask_layer_->SetIsMask(true);
481 SetNeedsFullTreeSync();
484 void Layer::SetReplicaLayer(Layer* layer) {
485 DCHECK(IsPropertyChangeAllowed());
486 if (replica_layer_.get() == layer)
487 return;
488 if (replica_layer_.get()) {
489 DCHECK_EQ(this, replica_layer_->parent());
490 replica_layer_->RemoveFromParent();
492 replica_layer_ = layer;
493 if (replica_layer_.get()) {
494 DCHECK(!replica_layer_->parent());
495 replica_layer_->RemoveFromParent();
496 replica_layer_->SetParent(this);
498 SetNeedsFullTreeSync();
501 void Layer::SetFilters(const FilterOperations& filters) {
502 DCHECK(IsPropertyChangeAllowed());
503 if (filters_ == filters)
504 return;
505 filters_ = filters;
506 SetNeedsCommit();
509 bool Layer::FilterIsAnimating() const {
510 DCHECK(layer_tree_host_);
511 return layer_animation_controller_
512 ? layer_animation_controller_->IsCurrentlyAnimatingProperty(
513 Animation::FILTER,
514 LayerAnimationController::ObserverType::ACTIVE)
515 : layer_tree_host_->IsAnimatingFilterProperty(this);
518 bool Layer::HasPotentiallyRunningFilterAnimation() const {
519 if (layer_animation_controller_) {
520 return layer_animation_controller_->IsPotentiallyAnimatingProperty(
521 Animation::FILTER, LayerAnimationController::ObserverType::ACTIVE);
523 return layer_tree_host_->HasPotentiallyRunningFilterAnimation(this);
526 void Layer::SetBackgroundFilters(const FilterOperations& filters) {
527 DCHECK(IsPropertyChangeAllowed());
528 if (background_filters_ == filters)
529 return;
530 background_filters_ = filters;
531 SetNeedsCommit();
534 void Layer::SetOpacity(float opacity) {
535 DCHECK(IsPropertyChangeAllowed());
536 if (opacity_ == opacity)
537 return;
538 opacity_ = opacity;
539 SetNeedsCommit();
542 bool Layer::OpacityIsAnimating() const {
543 DCHECK(layer_tree_host_);
544 return layer_animation_controller_
545 ? layer_animation_controller_->IsCurrentlyAnimatingProperty(
546 Animation::OPACITY,
547 LayerAnimationController::ObserverType::ACTIVE)
548 : layer_tree_host_->IsAnimatingOpacityProperty(this);
551 bool Layer::HasPotentiallyRunningOpacityAnimation() const {
552 if (layer_animation_controller_) {
553 return layer_animation_controller_->IsPotentiallyAnimatingProperty(
554 Animation::OPACITY, LayerAnimationController::ObserverType::ACTIVE);
556 return layer_tree_host_->HasPotentiallyRunningOpacityAnimation(this);
559 bool Layer::OpacityCanAnimateOnImplThread() const {
560 return false;
563 void Layer::SetBlendMode(SkXfermode::Mode blend_mode) {
564 DCHECK(IsPropertyChangeAllowed());
565 if (blend_mode_ == blend_mode)
566 return;
568 // Allowing only blend modes that are defined in the CSS Compositing standard:
569 // http://dev.w3.org/fxtf/compositing-1/#blending
570 switch (blend_mode) {
571 case SkXfermode::kSrcOver_Mode:
572 case SkXfermode::kScreen_Mode:
573 case SkXfermode::kOverlay_Mode:
574 case SkXfermode::kDarken_Mode:
575 case SkXfermode::kLighten_Mode:
576 case SkXfermode::kColorDodge_Mode:
577 case SkXfermode::kColorBurn_Mode:
578 case SkXfermode::kHardLight_Mode:
579 case SkXfermode::kSoftLight_Mode:
580 case SkXfermode::kDifference_Mode:
581 case SkXfermode::kExclusion_Mode:
582 case SkXfermode::kMultiply_Mode:
583 case SkXfermode::kHue_Mode:
584 case SkXfermode::kSaturation_Mode:
585 case SkXfermode::kColor_Mode:
586 case SkXfermode::kLuminosity_Mode:
587 // supported blend modes
588 break;
589 case SkXfermode::kClear_Mode:
590 case SkXfermode::kSrc_Mode:
591 case SkXfermode::kDst_Mode:
592 case SkXfermode::kDstOver_Mode:
593 case SkXfermode::kSrcIn_Mode:
594 case SkXfermode::kDstIn_Mode:
595 case SkXfermode::kSrcOut_Mode:
596 case SkXfermode::kDstOut_Mode:
597 case SkXfermode::kSrcATop_Mode:
598 case SkXfermode::kDstATop_Mode:
599 case SkXfermode::kXor_Mode:
600 case SkXfermode::kPlus_Mode:
601 case SkXfermode::kModulate_Mode:
602 // Porter Duff Compositing Operators are not yet supported
603 // http://dev.w3.org/fxtf/compositing-1/#porterduffcompositingoperators
604 NOTREACHED();
605 return;
608 blend_mode_ = blend_mode;
609 SetNeedsCommit();
612 void Layer::SetIsRootForIsolatedGroup(bool root) {
613 DCHECK(IsPropertyChangeAllowed());
614 if (is_root_for_isolated_group_ == root)
615 return;
616 is_root_for_isolated_group_ = root;
617 SetNeedsCommit();
620 void Layer::SetContentsOpaque(bool opaque) {
621 DCHECK(IsPropertyChangeAllowed());
622 if (contents_opaque_ == opaque)
623 return;
624 contents_opaque_ = opaque;
625 SetNeedsCommit();
628 void Layer::SetPosition(const gfx::PointF& position) {
629 DCHECK(IsPropertyChangeAllowed());
630 if (position_ == position)
631 return;
632 position_ = position;
634 if (!layer_tree_host_)
635 return;
637 if (TransformNode* transform_node =
638 layer_tree_host_->property_trees()->transform_tree.Node(
639 transform_tree_index())) {
640 if (transform_node->owner_id == id()) {
641 transform_node->data.update_post_local_transform(position,
642 transform_origin());
643 transform_node->data.needs_local_transform_update = true;
644 layer_tree_host_->property_trees()->transform_tree.set_needs_update(true);
645 SetNeedsCommitNoRebuild();
646 return;
650 SetNeedsCommit();
653 bool Layer::IsContainerForFixedPositionLayers() const {
654 if (!transform_.IsIdentityOrTranslation())
655 return true;
656 if (parent_ && !parent_->transform_.IsIdentityOrTranslation())
657 return true;
658 return is_container_for_fixed_position_layers_;
661 bool Are2dAxisAligned(const gfx::Transform& a,
662 const gfx::Transform& b,
663 bool* is_invertible) {
664 if (a.IsScaleOrTranslation() && b.IsScaleOrTranslation()) {
665 *is_invertible = b.IsInvertible();
666 return true;
669 gfx::Transform inverse(gfx::Transform::kSkipInitialization);
670 *is_invertible = b.GetInverse(&inverse);
672 inverse *= a;
673 return inverse.Preserves2dAxisAlignment();
676 void Layer::SetTransform(const gfx::Transform& transform) {
677 DCHECK(IsPropertyChangeAllowed());
678 if (transform_ == transform)
679 return;
681 if (layer_tree_host_) {
682 if (TransformNode* transform_node =
683 layer_tree_host_->property_trees()->transform_tree.Node(
684 transform_tree_index())) {
685 if (transform_node->owner_id == id()) {
686 // We need to trigger a rebuild if we could have affected 2d axis
687 // alignment. We'll check to see if transform and transform_ are axis
688 // align with respect to one another.
689 bool invertible = false;
690 bool preserves_2d_axis_alignment =
691 Are2dAxisAligned(transform_, transform, &invertible);
692 transform_node->data.local = transform;
693 transform_node->data.needs_local_transform_update = true;
694 layer_tree_host_->property_trees()->transform_tree.set_needs_update(
695 true);
696 if (preserves_2d_axis_alignment)
697 SetNeedsCommitNoRebuild();
698 else
699 SetNeedsCommit();
700 transform_ = transform;
701 transform_is_invertible_ = invertible;
702 return;
707 transform_ = transform;
708 transform_is_invertible_ = transform.IsInvertible();
710 SetNeedsCommit();
713 void Layer::SetTransformOrigin(const gfx::Point3F& transform_origin) {
714 DCHECK(IsPropertyChangeAllowed());
715 if (transform_origin_ == transform_origin)
716 return;
717 transform_origin_ = transform_origin;
719 if (!layer_tree_host_)
720 return;
722 if (TransformNode* transform_node =
723 layer_tree_host_->property_trees()->transform_tree.Node(
724 transform_tree_index())) {
725 if (transform_node->owner_id == id()) {
726 transform_node->data.update_pre_local_transform(transform_origin);
727 transform_node->data.update_post_local_transform(position(),
728 transform_origin);
729 transform_node->data.needs_local_transform_update = true;
730 layer_tree_host_->property_trees()->transform_tree.set_needs_update(true);
731 SetNeedsCommitNoRebuild();
732 return;
736 SetNeedsCommit();
739 bool Layer::AnimationsPreserveAxisAlignment() const {
740 DCHECK(layer_tree_host_);
741 return layer_animation_controller_
742 ? layer_animation_controller_->AnimationsPreserveAxisAlignment()
743 : layer_tree_host_->AnimationsPreserveAxisAlignment(this);
746 bool Layer::TransformIsAnimating() const {
747 DCHECK(layer_tree_host_);
748 return layer_animation_controller_
749 ? layer_animation_controller_->IsCurrentlyAnimatingProperty(
750 Animation::TRANSFORM,
751 LayerAnimationController::ObserverType::ACTIVE)
752 : layer_tree_host_->IsAnimatingTransformProperty(this);
755 bool Layer::HasPotentiallyRunningTransformAnimation() const {
756 if (layer_animation_controller_) {
757 return layer_animation_controller_->IsPotentiallyAnimatingProperty(
758 Animation::TRANSFORM, LayerAnimationController::ObserverType::ACTIVE);
760 return layer_tree_host_->HasPotentiallyRunningTransformAnimation(this);
763 bool Layer::HasOnlyTranslationTransforms() const {
764 if (layer_animation_controller_) {
765 return layer_animation_controller_->HasOnlyTranslationTransforms(
766 LayerAnimationController::ObserverType::ACTIVE);
768 return layer_tree_host_->HasOnlyTranslationTransforms(this);
771 bool Layer::MaximumTargetScale(float* max_scale) const {
772 if (layer_animation_controller_) {
773 return layer_animation_controller_->MaximumTargetScale(
774 LayerAnimationController::ObserverType::ACTIVE, max_scale);
776 return layer_tree_host_->MaximumTargetScale(this, max_scale);
779 bool Layer::AnimationStartScale(float* start_scale) const {
780 if (layer_animation_controller_) {
781 return layer_animation_controller_->AnimationStartScale(
782 LayerAnimationController::ObserverType::ACTIVE, start_scale);
784 return layer_tree_host_->AnimationStartScale(this, start_scale);
787 bool Layer::HasAnyAnimationTargetingProperty(
788 Animation::TargetProperty property) const {
789 if (layer_animation_controller_)
790 return !!layer_animation_controller_->GetAnimation(property);
792 return layer_tree_host_->HasAnyAnimationTargetingProperty(this, property);
795 bool Layer::ScrollOffsetAnimationWasInterrupted() const {
796 DCHECK(layer_tree_host_);
797 return layer_animation_controller_
798 ? layer_animation_controller_
799 ->scroll_offset_animation_was_interrupted()
800 : layer_tree_host_->ScrollOffsetAnimationWasInterrupted(this);
803 void Layer::SetScrollParent(Layer* parent) {
804 DCHECK(IsPropertyChangeAllowed());
805 if (scroll_parent_ == parent)
806 return;
808 if (scroll_parent_)
809 scroll_parent_->RemoveScrollChild(this);
811 scroll_parent_ = parent;
813 if (scroll_parent_)
814 scroll_parent_->AddScrollChild(this);
816 SetNeedsCommit();
819 void Layer::AddScrollChild(Layer* child) {
820 if (!scroll_children_)
821 scroll_children_.reset(new std::set<Layer*>);
822 scroll_children_->insert(child);
823 SetNeedsCommit();
826 void Layer::RemoveScrollChild(Layer* child) {
827 scroll_children_->erase(child);
828 if (scroll_children_->empty())
829 scroll_children_ = nullptr;
830 SetNeedsCommit();
833 void Layer::SetClipParent(Layer* ancestor) {
834 DCHECK(IsPropertyChangeAllowed());
835 if (clip_parent_ == ancestor)
836 return;
838 if (clip_parent_)
839 clip_parent_->RemoveClipChild(this);
841 clip_parent_ = ancestor;
843 if (clip_parent_)
844 clip_parent_->AddClipChild(this);
846 SetNeedsCommit();
847 if (layer_tree_host_)
848 layer_tree_host_->SetNeedsMetaInfoRecomputation(true);
851 void Layer::AddClipChild(Layer* child) {
852 if (!clip_children_)
853 clip_children_.reset(new std::set<Layer*>);
854 clip_children_->insert(child);
855 SetNeedsCommit();
858 void Layer::RemoveClipChild(Layer* child) {
859 clip_children_->erase(child);
860 if (clip_children_->empty())
861 clip_children_ = nullptr;
862 SetNeedsCommit();
865 void Layer::SetScrollOffset(const gfx::ScrollOffset& scroll_offset) {
866 DCHECK(IsPropertyChangeAllowed());
868 if (scroll_offset_ == scroll_offset)
869 return;
870 scroll_offset_ = scroll_offset;
872 if (!layer_tree_host_)
873 return;
875 if (TransformNode* transform_node =
876 layer_tree_host_->property_trees()->transform_tree.Node(
877 transform_tree_index())) {
878 if (transform_node->owner_id == id()) {
879 transform_node->data.scroll_offset = CurrentScrollOffset();
880 transform_node->data.needs_local_transform_update = true;
881 layer_tree_host_->property_trees()->transform_tree.set_needs_update(true);
882 SetNeedsCommitNoRebuild();
883 return;
887 SetNeedsCommit();
890 void Layer::SetScrollCompensationAdjustment(
891 const gfx::Vector2dF& scroll_compensation_adjustment) {
892 if (scroll_compensation_adjustment_ == scroll_compensation_adjustment)
893 return;
894 scroll_compensation_adjustment_ = scroll_compensation_adjustment;
895 SetNeedsCommit();
898 gfx::Vector2dF Layer::ScrollCompensationAdjustment() const {
899 return scroll_compensation_adjustment_;
902 void Layer::SetScrollOffsetFromImplSide(
903 const gfx::ScrollOffset& scroll_offset) {
904 DCHECK(IsPropertyChangeAllowed());
905 // This function only gets called during a BeginMainFrame, so there
906 // is no need to call SetNeedsUpdate here.
907 DCHECK(layer_tree_host_ && layer_tree_host_->CommitRequested());
908 if (scroll_offset_ == scroll_offset)
909 return;
910 scroll_offset_ = scroll_offset;
911 SetNeedsPushProperties();
913 bool needs_rebuild = true;
914 if (TransformNode* transform_node =
915 layer_tree_host_->property_trees()->transform_tree.Node(
916 transform_tree_index())) {
917 if (transform_node->owner_id == id()) {
918 transform_node->data.scroll_offset = CurrentScrollOffset();
919 transform_node->data.needs_local_transform_update = true;
920 layer_tree_host_->property_trees()->transform_tree.set_needs_update(true);
921 needs_rebuild = false;
925 if (needs_rebuild)
926 layer_tree_host_->property_trees()->needs_rebuild = true;
928 if (!did_scroll_callback_.is_null())
929 did_scroll_callback_.Run();
930 // The callback could potentially change the layer structure:
931 // "this" may have been destroyed during the process.
934 void Layer::SetScrollClipLayerId(int clip_layer_id) {
935 DCHECK(IsPropertyChangeAllowed());
936 if (scroll_clip_layer_id_ == clip_layer_id)
937 return;
938 scroll_clip_layer_id_ = clip_layer_id;
939 SetNeedsCommit();
942 void Layer::SetUserScrollable(bool horizontal, bool vertical) {
943 DCHECK(IsPropertyChangeAllowed());
944 if (user_scrollable_horizontal_ == horizontal &&
945 user_scrollable_vertical_ == vertical)
946 return;
947 user_scrollable_horizontal_ = horizontal;
948 user_scrollable_vertical_ = vertical;
949 SetNeedsCommit();
952 void Layer::SetShouldScrollOnMainThread(bool should_scroll_on_main_thread) {
953 DCHECK(IsPropertyChangeAllowed());
954 if (should_scroll_on_main_thread_ == should_scroll_on_main_thread)
955 return;
956 should_scroll_on_main_thread_ = should_scroll_on_main_thread;
957 SetNeedsCommit();
960 void Layer::SetHaveWheelEventHandlers(bool have_wheel_event_handlers) {
961 DCHECK(IsPropertyChangeAllowed());
962 if (have_wheel_event_handlers_ == have_wheel_event_handlers)
963 return;
965 have_wheel_event_handlers_ = have_wheel_event_handlers;
966 SetNeedsCommit();
969 void Layer::SetHaveScrollEventHandlers(bool have_scroll_event_handlers) {
970 DCHECK(IsPropertyChangeAllowed());
971 if (have_scroll_event_handlers_ == have_scroll_event_handlers)
972 return;
973 have_scroll_event_handlers_ = have_scroll_event_handlers;
974 SetNeedsCommit();
977 void Layer::SetNonFastScrollableRegion(const Region& region) {
978 DCHECK(IsPropertyChangeAllowed());
979 if (non_fast_scrollable_region_ == region)
980 return;
981 non_fast_scrollable_region_ = region;
982 SetNeedsCommit();
985 void Layer::SetTouchEventHandlerRegion(const Region& region) {
986 DCHECK(IsPropertyChangeAllowed());
987 if (touch_event_handler_region_ == region)
988 return;
990 touch_event_handler_region_ = region;
991 SetNeedsCommit();
994 void Layer::SetScrollBlocksOn(ScrollBlocksOn scroll_blocks_on) {
995 DCHECK(IsPropertyChangeAllowed());
996 if (scroll_blocks_on_ == scroll_blocks_on)
997 return;
998 scroll_blocks_on_ = scroll_blocks_on;
999 SetNeedsCommit();
1002 void Layer::SetForceRenderSurface(bool force) {
1003 DCHECK(IsPropertyChangeAllowed());
1004 if (force_render_surface_ == force)
1005 return;
1006 force_render_surface_ = force;
1007 SetNeedsCommit();
1010 void Layer::SetDoubleSided(bool double_sided) {
1011 DCHECK(IsPropertyChangeAllowed());
1012 if (double_sided_ == double_sided)
1013 return;
1014 double_sided_ = double_sided;
1015 SetNeedsCommit();
1018 void Layer::Set3dSortingContextId(int id) {
1019 DCHECK(IsPropertyChangeAllowed());
1020 if (id == sorting_context_id_)
1021 return;
1022 sorting_context_id_ = id;
1023 SetNeedsCommit();
1026 void Layer::SetTransformTreeIndex(int index) {
1027 DCHECK(IsPropertyChangeAllowed());
1028 if (transform_tree_index_ == index)
1029 return;
1030 transform_tree_index_ = index;
1031 SetNeedsPushProperties();
1034 int Layer::transform_tree_index() const {
1035 if (!layer_tree_host_ ||
1036 layer_tree_host_->property_trees()->sequence_number !=
1037 property_tree_sequence_number_) {
1038 return -1;
1040 return transform_tree_index_;
1043 void Layer::SetClipTreeIndex(int index) {
1044 DCHECK(IsPropertyChangeAllowed());
1045 if (clip_tree_index_ == index)
1046 return;
1047 clip_tree_index_ = index;
1048 SetNeedsPushProperties();
1051 int Layer::clip_tree_index() const {
1052 if (!layer_tree_host_ ||
1053 layer_tree_host_->property_trees()->sequence_number !=
1054 property_tree_sequence_number_) {
1055 return -1;
1057 return clip_tree_index_;
1060 void Layer::SetEffectTreeIndex(int index) {
1061 DCHECK(IsPropertyChangeAllowed());
1062 if (effect_tree_index_ == index)
1063 return;
1064 effect_tree_index_ = index;
1065 SetNeedsPushProperties();
1068 int Layer::effect_tree_index() const {
1069 if (!layer_tree_host_ ||
1070 layer_tree_host_->property_trees()->sequence_number !=
1071 property_tree_sequence_number_) {
1072 return -1;
1074 return effect_tree_index_;
1077 void Layer::InvalidatePropertyTreesIndices() {
1078 int invalid_property_tree_index = -1;
1079 SetTransformTreeIndex(invalid_property_tree_index);
1080 SetClipTreeIndex(invalid_property_tree_index);
1081 SetEffectTreeIndex(invalid_property_tree_index);
1084 void Layer::SetShouldFlattenTransform(bool should_flatten) {
1085 DCHECK(IsPropertyChangeAllowed());
1086 if (should_flatten_transform_ == should_flatten)
1087 return;
1088 should_flatten_transform_ = should_flatten;
1089 SetNeedsCommit();
1092 void Layer::SetIsDrawable(bool is_drawable) {
1093 DCHECK(IsPropertyChangeAllowed());
1094 if (is_drawable_ == is_drawable)
1095 return;
1097 is_drawable_ = is_drawable;
1098 UpdateDrawsContent(HasDrawableContent());
1101 void Layer::SetHideLayerAndSubtree(bool hide) {
1102 DCHECK(IsPropertyChangeAllowed());
1103 if (hide_layer_and_subtree_ == hide)
1104 return;
1106 hide_layer_and_subtree_ = hide;
1107 SetNeedsCommit();
1110 void Layer::SetNeedsDisplayRect(const gfx::Rect& dirty_rect) {
1111 if (dirty_rect.IsEmpty())
1112 return;
1114 SetNeedsPushProperties();
1115 update_rect_.Union(dirty_rect);
1117 if (DrawsContent())
1118 SetNeedsUpdate();
1121 bool Layer::DescendantIsFixedToContainerLayer() const {
1122 for (size_t i = 0; i < children_.size(); ++i) {
1123 if (children_[i]->position_constraint_.is_fixed_position() ||
1124 children_[i]->DescendantIsFixedToContainerLayer())
1125 return true;
1127 return false;
1130 void Layer::SetIsContainerForFixedPositionLayers(bool container) {
1131 if (is_container_for_fixed_position_layers_ == container)
1132 return;
1133 is_container_for_fixed_position_layers_ = container;
1135 if (layer_tree_host_ && layer_tree_host_->CommitRequested())
1136 return;
1138 // Only request a commit if we have a fixed positioned descendant.
1139 if (DescendantIsFixedToContainerLayer())
1140 SetNeedsCommit();
1143 void Layer::SetPositionConstraint(const LayerPositionConstraint& constraint) {
1144 DCHECK(IsPropertyChangeAllowed());
1145 if (position_constraint_ == constraint)
1146 return;
1147 position_constraint_ = constraint;
1148 SetNeedsCommit();
1151 static void RunCopyCallbackOnMainThread(scoped_ptr<CopyOutputRequest> request,
1152 scoped_ptr<CopyOutputResult> result) {
1153 request->SendResult(result.Pass());
1156 static void PostCopyCallbackToMainThread(
1157 scoped_refptr<base::SingleThreadTaskRunner> main_thread_task_runner,
1158 scoped_ptr<CopyOutputRequest> request,
1159 scoped_ptr<CopyOutputResult> result) {
1160 main_thread_task_runner->PostTask(FROM_HERE,
1161 base::Bind(&RunCopyCallbackOnMainThread,
1162 base::Passed(&request),
1163 base::Passed(&result)));
1166 void Layer::PushPropertiesTo(LayerImpl* layer) {
1167 DCHECK(layer_tree_host_);
1169 // If we did not SavePaintProperties() for the layer this frame, then push the
1170 // real property values, not the paint property values.
1171 bool use_paint_properties = paint_properties_.source_frame_number ==
1172 layer_tree_host_->source_frame_number();
1174 layer->SetTransformOrigin(transform_origin_);
1175 layer->SetBackgroundColor(background_color_);
1176 layer->SetBounds(use_paint_properties ? paint_properties_.bounds
1177 : bounds_);
1179 if (frame_viewer_instrumentation::IsTracingLayerTreeSnapshots())
1180 layer->SetDebugInfo(TakeDebugInfo());
1182 layer->SetTransformTreeIndex(transform_tree_index());
1183 layer->SetEffectTreeIndex(effect_tree_index());
1184 layer->SetClipTreeIndex(clip_tree_index());
1185 layer->set_offset_to_transform_parent(offset_to_transform_parent_);
1186 layer->SetDoubleSided(double_sided_);
1187 layer->SetDrawsContent(DrawsContent());
1188 layer->SetHideLayerAndSubtree(hide_layer_and_subtree_);
1189 layer->SetHasRenderSurface(has_render_surface_);
1190 if (!layer->FilterIsAnimatingOnImplOnly() && !FilterIsAnimating())
1191 layer->SetFilters(filters_);
1192 DCHECK(!(FilterIsAnimating() && layer->FilterIsAnimatingOnImplOnly()));
1193 layer->SetBackgroundFilters(background_filters());
1194 layer->SetMasksToBounds(masks_to_bounds_);
1195 layer->SetShouldScrollOnMainThread(should_scroll_on_main_thread_);
1196 layer->SetHaveWheelEventHandlers(have_wheel_event_handlers_);
1197 layer->SetHaveScrollEventHandlers(have_scroll_event_handlers_);
1198 layer->SetNonFastScrollableRegion(non_fast_scrollable_region_);
1199 layer->SetTouchEventHandlerRegion(touch_event_handler_region_);
1200 layer->SetScrollBlocksOn(scroll_blocks_on_);
1201 layer->SetContentsOpaque(contents_opaque_);
1202 if (!layer->OpacityIsAnimatingOnImplOnly() && !OpacityIsAnimating())
1203 layer->SetOpacity(opacity_);
1204 DCHECK(!(OpacityIsAnimating() && layer->OpacityIsAnimatingOnImplOnly()));
1205 layer->SetBlendMode(blend_mode_);
1206 layer->SetIsRootForIsolatedGroup(is_root_for_isolated_group_);
1207 layer->SetPosition(position_);
1208 layer->SetIsContainerForFixedPositionLayers(
1209 IsContainerForFixedPositionLayers());
1210 layer->SetPositionConstraint(position_constraint_);
1211 layer->SetShouldFlattenTransform(should_flatten_transform_);
1212 layer->set_should_flatten_transform_from_property_tree(
1213 should_flatten_transform_from_property_tree_);
1214 layer->set_num_layer_or_descendant_with_copy_request(
1215 num_layer_or_descendants_with_copy_request_);
1216 layer->set_is_clipped(is_clipped_);
1217 layer->set_draw_blend_mode(draw_blend_mode_);
1218 layer->SetUseParentBackfaceVisibility(use_parent_backface_visibility_);
1219 if (!layer->TransformIsAnimatingOnImplOnly() && !TransformIsAnimating())
1220 layer->SetTransformAndInvertibility(transform_, transform_is_invertible_);
1221 DCHECK(!(TransformIsAnimating() && layer->TransformIsAnimatingOnImplOnly()));
1222 layer->Set3dSortingContextId(sorting_context_id_);
1223 layer->SetNumDescendantsThatDrawContent(num_descendants_that_draw_content_);
1225 layer->SetScrollClipLayer(scroll_clip_layer_id_);
1226 layer->set_user_scrollable_horizontal(user_scrollable_horizontal_);
1227 layer->set_user_scrollable_vertical(user_scrollable_vertical_);
1229 LayerImpl* scroll_parent = nullptr;
1230 if (scroll_parent_) {
1231 scroll_parent = layer->layer_tree_impl()->LayerById(scroll_parent_->id());
1232 DCHECK(scroll_parent);
1235 layer->SetScrollParent(scroll_parent);
1236 if (scroll_children_) {
1237 std::set<LayerImpl*>* scroll_children = new std::set<LayerImpl*>;
1238 for (std::set<Layer*>::iterator it = scroll_children_->begin();
1239 it != scroll_children_->end();
1240 ++it) {
1241 DCHECK_EQ((*it)->scroll_parent(), this);
1242 LayerImpl* scroll_child =
1243 layer->layer_tree_impl()->LayerById((*it)->id());
1244 DCHECK(scroll_child);
1245 scroll_children->insert(scroll_child);
1247 layer->SetScrollChildren(scroll_children);
1248 } else {
1249 layer->SetScrollChildren(nullptr);
1252 LayerImpl* clip_parent = nullptr;
1253 if (clip_parent_) {
1254 clip_parent =
1255 layer->layer_tree_impl()->LayerById(clip_parent_->id());
1256 DCHECK(clip_parent);
1259 layer->SetClipParent(clip_parent);
1260 if (clip_children_) {
1261 std::set<LayerImpl*>* clip_children = new std::set<LayerImpl*>;
1262 for (std::set<Layer*>::iterator it = clip_children_->begin();
1263 it != clip_children_->end(); ++it) {
1264 DCHECK_EQ((*it)->clip_parent(), this);
1265 LayerImpl* clip_child = layer->layer_tree_impl()->LayerById((*it)->id());
1266 DCHECK(clip_child);
1267 clip_children->insert(clip_child);
1269 layer->SetClipChildren(clip_children);
1270 } else {
1271 layer->SetClipChildren(nullptr);
1274 // When a scroll offset animation is interrupted the new scroll position on
1275 // the pending tree will clobber any impl-side scrolling occuring on the
1276 // active tree. To do so, avoid scrolling the pending tree along with it
1277 // instead of trying to undo that scrolling later.
1278 if (ScrollOffsetAnimationWasInterrupted())
1279 layer->PushScrollOffsetFromMainThreadAndClobberActiveValue(scroll_offset_);
1280 else
1281 layer->PushScrollOffsetFromMainThread(scroll_offset_);
1282 layer->SetScrollCompensationAdjustment(ScrollCompensationAdjustment());
1284 // Wrap the copy_requests_ in a PostTask to the main thread.
1285 bool had_copy_requests = !copy_requests_.empty();
1286 ScopedPtrVector<CopyOutputRequest> main_thread_copy_requests;
1287 for (ScopedPtrVector<CopyOutputRequest>::iterator it = copy_requests_.begin();
1288 it != copy_requests_.end();
1289 ++it) {
1290 scoped_refptr<base::SingleThreadTaskRunner> main_thread_task_runner =
1291 layer_tree_host()->proxy()->MainThreadTaskRunner();
1292 scoped_ptr<CopyOutputRequest> original_request = copy_requests_.take(it);
1293 const CopyOutputRequest& original_request_ref = *original_request;
1294 scoped_ptr<CopyOutputRequest> main_thread_request =
1295 CopyOutputRequest::CreateRelayRequest(
1296 original_request_ref,
1297 base::Bind(&PostCopyCallbackToMainThread,
1298 main_thread_task_runner,
1299 base::Passed(&original_request)));
1300 main_thread_copy_requests.push_back(main_thread_request.Pass());
1302 if (!copy_requests_.empty() && layer_tree_host_)
1303 layer_tree_host_->property_trees()->needs_rebuild = true;
1304 if (had_copy_requests)
1305 UpdateNumCopyRequestsForSubtree(false);
1306 copy_requests_.clear();
1307 layer->PassCopyRequests(&main_thread_copy_requests);
1309 // If the main thread commits multiple times before the impl thread actually
1310 // draws, then damage tracking will become incorrect if we simply clobber the
1311 // update_rect here. The LayerImpl's update_rect needs to accumulate (i.e.
1312 // union) any update changes that have occurred on the main thread.
1313 update_rect_.Union(layer->update_rect());
1314 layer->SetUpdateRect(update_rect_);
1316 layer->SetStackingOrderChanged(stacking_order_changed_);
1318 if (layer->layer_animation_controller() && layer_animation_controller_)
1319 layer_animation_controller_->PushAnimationUpdatesTo(
1320 layer->layer_animation_controller());
1322 if (frame_timing_requests_dirty_) {
1323 layer->SetFrameTimingRequests(frame_timing_requests_);
1324 frame_timing_requests_dirty_ = false;
1327 bool is_page_scale_layer = this == layer_tree_host()->page_scale_layer();
1328 bool parent_affected =
1329 layer->parent() && layer->parent()->IsAffectedByPageScale();
1330 layer->SetIsAffectedByPageScale(is_page_scale_layer || parent_affected);
1332 // Reset any state that should be cleared for the next update.
1333 stacking_order_changed_ = false;
1334 update_rect_ = gfx::Rect();
1336 needs_push_properties_ = false;
1337 num_dependents_need_push_properties_ = 0;
1340 scoped_ptr<LayerImpl> Layer::CreateLayerImpl(LayerTreeImpl* tree_impl) {
1341 return LayerImpl::Create(tree_impl, layer_id_,
1342 new LayerImpl::SyncedScrollOffset);
1345 bool Layer::DrawsContent() const {
1346 return draws_content_;
1349 bool Layer::HasDrawableContent() const {
1350 return is_drawable_;
1353 void Layer::UpdateDrawsContent(bool has_drawable_content) {
1354 bool draws_content = has_drawable_content;
1355 DCHECK(is_drawable_ || !has_drawable_content);
1356 if (draws_content == draws_content_)
1357 return;
1359 if (HasDelegatedContent()) {
1360 // Layers with delegated content need to be treated as if they have as
1361 // many children as the number of layers they own delegated quads for.
1362 // Since we don't know this number right now, we choose one that acts like
1363 // infinity for our purposes.
1364 AddDrawableDescendants(draws_content ? 1000 : -1000);
1367 if (parent())
1368 parent()->AddDrawableDescendants(draws_content ? 1 : -1);
1370 draws_content_ = draws_content;
1371 SetNeedsCommit();
1374 int Layer::NumDescendantsThatDrawContent() const {
1375 return num_descendants_that_draw_content_;
1378 void Layer::SavePaintProperties() {
1379 DCHECK(layer_tree_host_);
1381 // TODO(reveman): Save all layer properties that we depend on not
1382 // changing until PushProperties() has been called. crbug.com/231016
1383 paint_properties_.bounds = bounds_;
1384 paint_properties_.source_frame_number =
1385 layer_tree_host_->source_frame_number();
1388 bool Layer::Update() {
1389 DCHECK(layer_tree_host_);
1390 DCHECK_EQ(layer_tree_host_->source_frame_number(),
1391 paint_properties_.source_frame_number) <<
1392 "SavePaintProperties must be called for any layer that is painted.";
1393 return false;
1396 bool Layer::IsSuitableForGpuRasterization() const {
1397 return true;
1400 scoped_refptr<base::trace_event::ConvertableToTraceFormat>
1401 Layer::TakeDebugInfo() {
1402 if (client_)
1403 return client_->TakeDebugInfo();
1404 else
1405 return nullptr;
1408 void Layer::SetHasRenderSurface(bool has_render_surface) {
1409 if (has_render_surface_ == has_render_surface)
1410 return;
1411 has_render_surface_ = has_render_surface;
1412 // We do not need SetNeedsCommit here, since this is only ever called
1413 // during a commit, from CalculateDrawProperties.
1414 SetNeedsPushProperties();
1415 layer_tree_host_->property_trees()->needs_rebuild = true;
1418 gfx::ScrollOffset Layer::ScrollOffsetForAnimation() const {
1419 return CurrentScrollOffset();
1422 // On<Property>Animated is called due to an ongoing accelerated animation.
1423 // Since this animation is also being run on the compositor thread, there
1424 // is no need to request a commit to push this value over, so the value is
1425 // set directly rather than by calling Set<Property>.
1426 void Layer::OnFilterAnimated(const FilterOperations& filters) {
1427 filters_ = filters;
1430 void Layer::OnOpacityAnimated(float opacity) {
1431 opacity_ = opacity;
1432 if (layer_tree_host_) {
1433 if (EffectNode* node = layer_tree_host_->property_trees()->effect_tree.Node(
1434 effect_tree_index())) {
1435 if (node->owner_id == id()) {
1436 node->data.opacity = opacity;
1437 layer_tree_host_->property_trees()->effect_tree.set_needs_update(true);
1443 void Layer::OnTransformAnimated(const gfx::Transform& transform) {
1444 if (transform_ == transform)
1445 return;
1446 transform_ = transform;
1447 transform_is_invertible_ = transform.IsInvertible();
1448 if (layer_tree_host_) {
1449 if (TransformNode* node =
1450 layer_tree_host_->property_trees()->transform_tree.Node(
1451 transform_tree_index())) {
1452 if (node->owner_id == id()) {
1453 node->data.local = transform;
1454 node->data.needs_local_transform_update = true;
1455 node->data.is_animated = true;
1456 layer_tree_host_->property_trees()->transform_tree.set_needs_update(
1457 true);
1463 void Layer::OnScrollOffsetAnimated(const gfx::ScrollOffset& scroll_offset) {
1464 // Do nothing. Scroll deltas will be sent from the compositor thread back
1465 // to the main thread in the same manner as during non-animated
1466 // compositor-driven scrolling.
1469 void Layer::OnAnimationWaitingForDeletion() {
1470 // Animations are only deleted during PushProperties.
1471 SetNeedsPushProperties();
1474 void Layer::OnTransformIsPotentiallyAnimatingChanged(bool is_animating) {
1475 if (!layer_tree_host_)
1476 return;
1477 TransformTree& transform_tree =
1478 layer_tree_host_->property_trees()->transform_tree;
1479 TransformNode* node = transform_tree.Node(transform_tree_index());
1480 if (!node)
1481 return;
1483 if (node->owner_id == id()) {
1484 node->data.is_animated = is_animating;
1485 if (is_animating) {
1486 float maximum_target_scale = 0.f;
1487 node->data.local_maximum_animation_target_scale =
1488 MaximumTargetScale(&maximum_target_scale) ? maximum_target_scale
1489 : 0.f;
1491 float animation_start_scale = 0.f;
1492 node->data.local_starting_animation_scale =
1493 AnimationStartScale(&animation_start_scale) ? animation_start_scale
1494 : 0.f;
1496 node->data.has_only_translation_animations =
1497 HasOnlyTranslationTransforms();
1499 } else {
1500 node->data.local_maximum_animation_target_scale = 0.f;
1501 node->data.local_starting_animation_scale = 0.f;
1502 node->data.has_only_translation_animations = true;
1504 transform_tree.set_needs_update(true);
1508 bool Layer::IsActive() const {
1509 return true;
1512 bool Layer::AddAnimation(scoped_ptr <Animation> animation) {
1513 DCHECK(layer_animation_controller_);
1514 if (!layer_animation_controller_->animation_registrar())
1515 return false;
1517 if (animation->target_property() == Animation::SCROLL_OFFSET &&
1518 !layer_animation_controller_->animation_registrar()
1519 ->supports_scroll_animations())
1520 return false;
1522 UMA_HISTOGRAM_BOOLEAN("Renderer.AnimationAddedToOrphanLayer",
1523 !layer_tree_host_);
1524 layer_animation_controller_->AddAnimation(animation.Pass());
1525 SetNeedsCommit();
1526 return true;
1529 void Layer::PauseAnimation(int animation_id, double time_offset) {
1530 DCHECK(layer_animation_controller_);
1531 layer_animation_controller_->PauseAnimation(
1532 animation_id, base::TimeDelta::FromSecondsD(time_offset));
1533 SetNeedsCommit();
1536 void Layer::RemoveAnimation(int animation_id) {
1537 DCHECK(layer_animation_controller_);
1538 layer_animation_controller_->RemoveAnimation(animation_id);
1539 SetNeedsCommit();
1542 void Layer::RemoveAnimation(int animation_id,
1543 Animation::TargetProperty property) {
1544 DCHECK(layer_animation_controller_);
1545 layer_animation_controller_->RemoveAnimation(animation_id, property);
1546 SetNeedsCommit();
1549 void Layer::SetLayerAnimationControllerForTest(
1550 scoped_refptr<LayerAnimationController> controller) {
1551 DCHECK(layer_animation_controller_);
1552 layer_animation_controller_->RemoveValueObserver(this);
1553 layer_animation_controller_ = controller;
1554 layer_animation_controller_->AddValueObserver(this);
1555 SetNeedsCommit();
1558 bool Layer::HasActiveAnimation() const {
1559 DCHECK(layer_tree_host_);
1560 return layer_animation_controller_
1561 ? layer_animation_controller_->HasActiveAnimation()
1562 : layer_tree_host_->HasActiveAnimation(this);
1565 void Layer::RegisterForAnimations(AnimationRegistrar* registrar) {
1566 if (layer_animation_controller_)
1567 layer_animation_controller_->SetAnimationRegistrar(registrar);
1570 void Layer::AddLayerAnimationEventObserver(
1571 LayerAnimationEventObserver* animation_observer) {
1572 DCHECK(layer_animation_controller_);
1573 layer_animation_controller_->AddEventObserver(animation_observer);
1576 void Layer::RemoveLayerAnimationEventObserver(
1577 LayerAnimationEventObserver* animation_observer) {
1578 DCHECK(layer_animation_controller_);
1579 layer_animation_controller_->RemoveEventObserver(animation_observer);
1582 ScrollbarLayerInterface* Layer::ToScrollbarLayer() {
1583 return nullptr;
1586 RenderingStatsInstrumentation* Layer::rendering_stats_instrumentation() const {
1587 return layer_tree_host_->rendering_stats_instrumentation();
1590 void Layer::RemoveFromScrollTree() {
1591 if (scroll_children_.get()) {
1592 std::set<Layer*> copy = *scroll_children_;
1593 for (std::set<Layer*>::iterator it = copy.begin(); it != copy.end(); ++it)
1594 (*it)->SetScrollParent(nullptr);
1597 DCHECK(!scroll_children_);
1598 SetScrollParent(nullptr);
1601 void Layer::RemoveFromClipTree() {
1602 if (clip_children_.get()) {
1603 std::set<Layer*> copy = *clip_children_;
1604 for (std::set<Layer*>::iterator it = copy.begin(); it != copy.end(); ++it)
1605 (*it)->SetClipParent(nullptr);
1608 DCHECK(!clip_children_);
1609 SetClipParent(nullptr);
1612 void Layer::AddDrawableDescendants(int num) {
1613 DCHECK_GE(num_descendants_that_draw_content_, 0);
1614 DCHECK_GE(num_descendants_that_draw_content_ + num, 0);
1615 if (num == 0)
1616 return;
1617 num_descendants_that_draw_content_ += num;
1618 SetNeedsCommit();
1619 if (parent())
1620 parent()->AddDrawableDescendants(num);
1623 void Layer::RunMicroBenchmark(MicroBenchmark* benchmark) {
1624 benchmark->RunOnLayer(this);
1627 bool Layer::HasDelegatedContent() const {
1628 return false;
1631 void Layer::SetFrameTimingRequests(
1632 const std::vector<FrameTimingRequest>& requests) {
1633 // TODO(vmpstr): Early out if there are no changes earlier in the call stack.
1634 if (requests == frame_timing_requests_)
1635 return;
1636 frame_timing_requests_ = requests;
1637 frame_timing_requests_dirty_ = true;
1638 SetNeedsCommit();
1641 void Layer::DidBeginTracing() {
1642 // We'll be dumping layer trees as part of trace, so make sure
1643 // PushPropertiesTo() propagates layer debug info to the impl
1644 // side -- otherwise this won't happen for the the layers that
1645 // remain unchanged since tracing started.
1646 SetNeedsPushProperties();
1649 void Layer::set_visited(bool visited) {
1650 visited_tracker_ =
1651 visited ? layer_tree_host()->meta_information_sequence_number() : 0;
1654 bool Layer::visited() {
1655 return visited_tracker_ ==
1656 layer_tree_host()->meta_information_sequence_number();
1659 void Layer::set_layer_or_descendant_is_drawn(
1660 bool layer_or_descendant_is_drawn) {
1661 layer_or_descendant_is_drawn_tracker_ =
1662 layer_or_descendant_is_drawn
1663 ? layer_tree_host()->meta_information_sequence_number()
1664 : 0;
1667 bool Layer::layer_or_descendant_is_drawn() {
1668 return layer_or_descendant_is_drawn_tracker_ ==
1669 layer_tree_host()->meta_information_sequence_number();
1672 void Layer::set_sorted_for_recursion(bool sorted_for_recursion) {
1673 sorted_for_recursion_tracker_ =
1674 sorted_for_recursion
1675 ? layer_tree_host()->meta_information_sequence_number()
1676 : 0;
1679 bool Layer::sorted_for_recursion() {
1680 return sorted_for_recursion_tracker_ ==
1681 layer_tree_host()->meta_information_sequence_number();
1684 gfx::Transform Layer::draw_transform() const {
1685 DCHECK_NE(transform_tree_index_, -1);
1686 return DrawTransformFromPropertyTrees(
1687 this, layer_tree_host_->property_trees()->transform_tree);
1690 gfx::Transform Layer::screen_space_transform() const {
1691 DCHECK_NE(transform_tree_index_, -1);
1692 return ScreenSpaceTransformFromPropertyTrees(
1693 this, layer_tree_host_->property_trees()->transform_tree);
1696 } // namespace cc