Add screen space opacity to opacity tree
[chromium-blink-merge.git] / cc / layers / layer.cc
blob0e87ff758e6de2738c80820bc170975f477208d3
1 // Copyright 2010 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "cc/layers/layer.h"
7 #include <algorithm>
9 #include "base/atomic_sequence_num.h"
10 #include "base/location.h"
11 #include "base/metrics/histogram.h"
12 #include "base/single_thread_task_runner.h"
13 #include "base/time/time.h"
14 #include "base/trace_event/trace_event.h"
15 #include "cc/animation/animation.h"
16 #include "cc/animation/animation_events.h"
17 #include "cc/animation/animation_registrar.h"
18 #include "cc/animation/keyframed_animation_curve.h"
19 #include "cc/animation/layer_animation_controller.h"
20 #include "cc/base/simple_enclosed_region.h"
21 #include "cc/debug/frame_viewer_instrumentation.h"
22 #include "cc/layers/layer_client.h"
23 #include "cc/layers/layer_impl.h"
24 #include "cc/layers/scrollbar_layer_interface.h"
25 #include "cc/output/copy_output_request.h"
26 #include "cc/output/copy_output_result.h"
27 #include "cc/trees/layer_tree_host.h"
28 #include "cc/trees/layer_tree_impl.h"
29 #include "third_party/skia/include/core/SkImageFilter.h"
30 #include "ui/gfx/geometry/rect_conversions.h"
31 #include "ui/gfx/geometry/vector2d_conversions.h"
33 namespace cc {
35 base::StaticAtomicSequenceNumber g_next_layer_id;
37 scoped_refptr<Layer> Layer::Create(const LayerSettings& settings) {
38 return make_scoped_refptr(new Layer(settings));
41 Layer::Layer(const LayerSettings& settings)
42 : needs_push_properties_(false),
43 num_dependents_need_push_properties_(0),
44 stacking_order_changed_(false),
45 // Layer IDs start from 1.
46 layer_id_(g_next_layer_id.GetNext() + 1),
47 ignore_set_needs_commit_(false),
48 sorting_context_id_(0),
49 parent_(nullptr),
50 layer_tree_host_(nullptr),
51 scroll_clip_layer_id_(INVALID_ID),
52 num_descendants_that_draw_content_(0),
53 transform_tree_index_(-1),
54 opacity_tree_index_(-1),
55 clip_tree_index_(-1),
56 property_tree_sequence_number_(-1),
57 num_layer_or_descendants_with_copy_request_(0),
58 num_layer_or_descendants_with_input_handler_(0),
59 num_children_with_scroll_parent_(0),
60 should_flatten_transform_from_property_tree_(false),
61 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 draw_checkerboard_for_missing_tiles_(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 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 raster_scale_(0.f),
91 client_(nullptr),
92 frame_timing_requests_dirty_(false) {
93 if (!settings.use_compositor_animation_timelines) {
94 layer_animation_controller_ = LayerAnimationController::Create(layer_id_);
95 layer_animation_controller_->AddValueObserver(this);
96 layer_animation_controller_->set_value_provider(this);
100 Layer::~Layer() {
101 // Our parent should be holding a reference to us so there should be no
102 // way for us to be destroyed while we still have a parent.
103 DCHECK(!parent());
104 // Similarly we shouldn't have a layer tree host since it also keeps a
105 // reference to us.
106 DCHECK(!layer_tree_host());
108 if (layer_animation_controller_) {
109 layer_animation_controller_->RemoveValueObserver(this);
110 layer_animation_controller_->remove_value_provider(this);
113 RemoveFromScrollTree();
114 RemoveFromClipTree();
116 // Remove the parent reference from all children and dependents.
117 RemoveAllChildren();
118 if (mask_layer_.get()) {
119 DCHECK_EQ(this, mask_layer_->parent());
120 mask_layer_->RemoveFromParent();
122 if (replica_layer_.get()) {
123 DCHECK_EQ(this, replica_layer_->parent());
124 replica_layer_->RemoveFromParent();
128 void Layer::SetLayerTreeHost(LayerTreeHost* host) {
129 if (layer_tree_host_ == host)
130 return;
132 if (layer_tree_host_)
133 layer_tree_host_->property_trees()->needs_rebuild = true;
135 if (host)
136 host->property_trees()->needs_rebuild = true;
138 InvalidatePropertyTreesIndices();
140 layer_tree_host_ = host;
142 // When changing hosts, the layer needs to commit its properties to the impl
143 // side for the new host.
144 SetNeedsPushProperties();
146 for (size_t i = 0; i < children_.size(); ++i)
147 children_[i]->SetLayerTreeHost(host);
149 if (mask_layer_.get())
150 mask_layer_->SetLayerTreeHost(host);
151 if (replica_layer_.get())
152 replica_layer_->SetLayerTreeHost(host);
154 if (host) {
155 RegisterForAnimations(host->animation_registrar());
156 if (host->settings().layer_transforms_should_scale_layer_contents)
157 reset_raster_scale_to_unknown();
160 if (host && layer_animation_controller_->has_any_animation())
161 host->SetNeedsCommit();
164 void Layer::SetNeedsUpdate() {
165 if (layer_tree_host_ && !ignore_set_needs_commit_)
166 layer_tree_host_->SetNeedsUpdateLayers();
169 void Layer::SetNeedsCommit() {
170 if (!layer_tree_host_)
171 return;
173 SetNeedsPushProperties();
174 layer_tree_host_->property_trees()->needs_rebuild = true;
176 if (ignore_set_needs_commit_)
177 return;
179 layer_tree_host_->SetNeedsCommit();
182 void Layer::SetNeedsCommitNoRebuild() {
183 if (!layer_tree_host_)
184 return;
186 SetNeedsPushProperties();
188 if (ignore_set_needs_commit_)
189 return;
191 layer_tree_host_->SetNeedsCommit();
194 void Layer::SetNeedsFullTreeSync() {
195 if (!layer_tree_host_)
196 return;
198 layer_tree_host_->SetNeedsFullTreeSync();
201 void Layer::SetNextCommitWaitsForActivation() {
202 if (!layer_tree_host_)
203 return;
205 layer_tree_host_->SetNextCommitWaitsForActivation();
208 void Layer::SetNeedsPushProperties() {
209 if (needs_push_properties_)
210 return;
211 if (!parent_should_know_need_push_properties() && parent_)
212 parent_->AddDependentNeedsPushProperties();
213 needs_push_properties_ = true;
216 void Layer::AddDependentNeedsPushProperties() {
217 DCHECK_GE(num_dependents_need_push_properties_, 0);
219 if (!parent_should_know_need_push_properties() && parent_)
220 parent_->AddDependentNeedsPushProperties();
222 num_dependents_need_push_properties_++;
225 void Layer::RemoveDependentNeedsPushProperties() {
226 num_dependents_need_push_properties_--;
227 DCHECK_GE(num_dependents_need_push_properties_, 0);
229 if (!parent_should_know_need_push_properties() && parent_)
230 parent_->RemoveDependentNeedsPushProperties();
233 bool Layer::IsPropertyChangeAllowed() const {
234 if (!layer_tree_host_)
235 return true;
237 if (!layer_tree_host_->settings().strict_layer_property_change_checking)
238 return true;
240 return !layer_tree_host_->in_paint_layer_contents();
243 skia::RefPtr<SkPicture> Layer::GetPicture() const {
244 return skia::RefPtr<SkPicture>();
247 void Layer::SetParent(Layer* layer) {
248 DCHECK(!layer || !layer->HasAncestor(this));
250 if (parent_should_know_need_push_properties()) {
251 if (parent_)
252 parent_->RemoveDependentNeedsPushProperties();
253 if (layer)
254 layer->AddDependentNeedsPushProperties();
257 parent_ = layer;
258 SetLayerTreeHost(parent_ ? parent_->layer_tree_host() : nullptr);
260 if (!layer_tree_host_)
261 return;
263 layer_tree_host_->property_trees()->needs_rebuild = true;
265 const LayerTreeSettings& settings = layer_tree_host_->settings();
266 if (!settings.layer_transforms_should_scale_layer_contents)
267 return;
269 reset_raster_scale_to_unknown();
270 if (mask_layer_.get())
271 mask_layer_->reset_raster_scale_to_unknown();
272 if (replica_layer_.get() && replica_layer_->mask_layer_.get())
273 replica_layer_->mask_layer_->reset_raster_scale_to_unknown();
276 void Layer::AddChild(scoped_refptr<Layer> child) {
277 InsertChild(child, children_.size());
280 void Layer::InsertChild(scoped_refptr<Layer> child, size_t index) {
281 DCHECK(IsPropertyChangeAllowed());
282 child->RemoveFromParent();
283 AddDrawableDescendants(child->NumDescendantsThatDrawContent() +
284 (child->DrawsContent() ? 1 : 0));
285 child->SetParent(this);
286 child->stacking_order_changed_ = true;
288 index = std::min(index, children_.size());
289 children_.insert(children_.begin() + index, child);
290 SetNeedsFullTreeSync();
293 void Layer::RemoveFromParent() {
294 DCHECK(IsPropertyChangeAllowed());
295 if (parent_)
296 parent_->RemoveChildOrDependent(this);
299 void Layer::RemoveChildOrDependent(Layer* child) {
300 if (mask_layer_.get() == child) {
301 mask_layer_->SetParent(nullptr);
302 mask_layer_ = nullptr;
303 SetNeedsFullTreeSync();
304 return;
306 if (replica_layer_.get() == child) {
307 replica_layer_->SetParent(nullptr);
308 replica_layer_ = nullptr;
309 SetNeedsFullTreeSync();
310 return;
313 for (LayerList::iterator iter = children_.begin();
314 iter != children_.end();
315 ++iter) {
316 if (iter->get() != child)
317 continue;
319 child->SetParent(nullptr);
320 AddDrawableDescendants(-child->NumDescendantsThatDrawContent() -
321 (child->DrawsContent() ? 1 : 0));
322 children_.erase(iter);
323 SetNeedsFullTreeSync();
324 return;
328 void Layer::ReplaceChild(Layer* reference, scoped_refptr<Layer> new_layer) {
329 DCHECK(reference);
330 DCHECK_EQ(reference->parent(), this);
331 DCHECK(IsPropertyChangeAllowed());
333 if (reference == new_layer.get())
334 return;
336 // Find the index of |reference| in |children_|.
337 auto reference_it =
338 std::find_if(children_.begin(), children_.end(),
339 [reference](const scoped_refptr<Layer>& layer) {
340 return layer.get() == reference;
342 DCHECK(reference_it != children_.end());
343 size_t reference_index = reference_it - children_.begin();
344 reference->RemoveFromParent();
346 if (new_layer.get()) {
347 new_layer->RemoveFromParent();
348 InsertChild(new_layer, reference_index);
352 void Layer::SetBounds(const gfx::Size& size) {
353 DCHECK(IsPropertyChangeAllowed());
354 if (bounds() == size)
355 return;
356 bounds_ = size;
358 if (!layer_tree_host_)
359 return;
361 if (ClipNode* clip_node = layer_tree_host_->property_trees()->clip_tree.Node(
362 clip_tree_index())) {
363 if (clip_node->owner_id == id()) {
364 clip_node->data.clip.set_size(size);
365 layer_tree_host_->property_trees()->clip_tree.set_needs_update(true);
369 SetNeedsCommitNoRebuild();
372 Layer* Layer::RootLayer() {
373 Layer* layer = this;
374 while (layer->parent())
375 layer = layer->parent();
376 return layer;
379 void Layer::RemoveAllChildren() {
380 DCHECK(IsPropertyChangeAllowed());
381 while (children_.size()) {
382 Layer* layer = children_[0].get();
383 DCHECK_EQ(this, layer->parent());
384 layer->RemoveFromParent();
388 void Layer::SetChildren(const LayerList& children) {
389 DCHECK(IsPropertyChangeAllowed());
390 if (children == children_)
391 return;
393 RemoveAllChildren();
394 for (size_t i = 0; i < children.size(); ++i)
395 AddChild(children[i]);
398 bool Layer::HasAncestor(const Layer* ancestor) const {
399 for (const Layer* layer = parent(); layer; layer = layer->parent()) {
400 if (layer == ancestor)
401 return true;
403 return false;
406 void Layer::RequestCopyOfOutput(
407 scoped_ptr<CopyOutputRequest> request) {
408 DCHECK(IsPropertyChangeAllowed());
409 bool had_no_copy_requests = copy_requests_.empty();
410 if (void* source = request->source()) {
411 auto it = std::find_if(
412 copy_requests_.begin(), copy_requests_.end(),
413 [source](const CopyOutputRequest* x) { return x->source() == source; });
414 if (it != copy_requests_.end())
415 copy_requests_.erase(it);
417 if (request->IsEmpty())
418 return;
419 copy_requests_.push_back(request.Pass());
420 if (had_no_copy_requests) {
421 bool copy_request_added = true;
422 UpdateNumCopyRequestsForSubtree(copy_request_added);
424 SetNeedsCommit();
427 void Layer::UpdateNumCopyRequestsForSubtree(bool add) {
428 int change = add ? 1 : -1;
429 for (Layer* layer = this; layer; layer = layer->parent()) {
430 layer->num_layer_or_descendants_with_copy_request_ += change;
431 layer->draw_properties().layer_or_descendant_has_copy_request =
432 (layer->num_layer_or_descendants_with_copy_request_ != 0);
433 DCHECK_GE(layer->num_layer_or_descendants_with_copy_request_, 0);
437 void Layer::SetBackgroundColor(SkColor background_color) {
438 DCHECK(IsPropertyChangeAllowed());
439 if (background_color_ == background_color)
440 return;
441 background_color_ = background_color;
442 SetNeedsCommit();
445 SkColor Layer::SafeOpaqueBackgroundColor() const {
446 SkColor color = background_color();
447 if (SkColorGetA(color) == 255 && !contents_opaque()) {
448 color = SK_ColorTRANSPARENT;
449 } else if (SkColorGetA(color) != 255 && contents_opaque()) {
450 for (const Layer* layer = parent(); layer;
451 layer = layer->parent()) {
452 color = layer->background_color();
453 if (SkColorGetA(color) == 255)
454 break;
456 if (SkColorGetA(color) != 255)
457 color = layer_tree_host_->background_color();
458 if (SkColorGetA(color) != 255)
459 color = SkColorSetA(color, 255);
461 return color;
464 void Layer::SetMasksToBounds(bool masks_to_bounds) {
465 DCHECK(IsPropertyChangeAllowed());
466 if (masks_to_bounds_ == masks_to_bounds)
467 return;
468 masks_to_bounds_ = masks_to_bounds;
469 SetNeedsCommit();
472 void Layer::SetMaskLayer(Layer* mask_layer) {
473 DCHECK(IsPropertyChangeAllowed());
474 if (mask_layer_.get() == mask_layer)
475 return;
476 if (mask_layer_.get()) {
477 DCHECK_EQ(this, mask_layer_->parent());
478 mask_layer_->RemoveFromParent();
480 mask_layer_ = mask_layer;
481 if (mask_layer_.get()) {
482 DCHECK(!mask_layer_->parent());
483 mask_layer_->RemoveFromParent();
484 mask_layer_->SetParent(this);
485 mask_layer_->SetIsMask(true);
487 SetNeedsFullTreeSync();
490 void Layer::SetReplicaLayer(Layer* layer) {
491 DCHECK(IsPropertyChangeAllowed());
492 if (replica_layer_.get() == layer)
493 return;
494 if (replica_layer_.get()) {
495 DCHECK_EQ(this, replica_layer_->parent());
496 replica_layer_->RemoveFromParent();
498 replica_layer_ = layer;
499 if (replica_layer_.get()) {
500 DCHECK(!replica_layer_->parent());
501 replica_layer_->RemoveFromParent();
502 replica_layer_->SetParent(this);
504 SetNeedsFullTreeSync();
507 void Layer::SetFilters(const FilterOperations& filters) {
508 DCHECK(IsPropertyChangeAllowed());
509 if (filters_ == filters)
510 return;
511 filters_ = filters;
512 SetNeedsCommit();
515 bool Layer::FilterIsAnimating() const {
516 return layer_animation_controller_->IsAnimatingProperty(Animation::FILTER);
519 void Layer::SetBackgroundFilters(const FilterOperations& filters) {
520 DCHECK(IsPropertyChangeAllowed());
521 if (background_filters_ == filters)
522 return;
523 background_filters_ = filters;
524 SetNeedsCommit();
527 void Layer::SetOpacity(float opacity) {
528 DCHECK(IsPropertyChangeAllowed());
529 if (opacity_ == opacity)
530 return;
531 opacity_ = opacity;
532 SetNeedsCommit();
535 bool Layer::OpacityIsAnimating() const {
536 return layer_animation_controller_->IsAnimatingProperty(Animation::OPACITY);
539 bool Layer::OpacityCanAnimateOnImplThread() const {
540 return false;
543 void Layer::SetBlendMode(SkXfermode::Mode blend_mode) {
544 DCHECK(IsPropertyChangeAllowed());
545 if (blend_mode_ == blend_mode)
546 return;
548 // Allowing only blend modes that are defined in the CSS Compositing standard:
549 // http://dev.w3.org/fxtf/compositing-1/#blending
550 switch (blend_mode) {
551 case SkXfermode::kSrcOver_Mode:
552 case SkXfermode::kScreen_Mode:
553 case SkXfermode::kOverlay_Mode:
554 case SkXfermode::kDarken_Mode:
555 case SkXfermode::kLighten_Mode:
556 case SkXfermode::kColorDodge_Mode:
557 case SkXfermode::kColorBurn_Mode:
558 case SkXfermode::kHardLight_Mode:
559 case SkXfermode::kSoftLight_Mode:
560 case SkXfermode::kDifference_Mode:
561 case SkXfermode::kExclusion_Mode:
562 case SkXfermode::kMultiply_Mode:
563 case SkXfermode::kHue_Mode:
564 case SkXfermode::kSaturation_Mode:
565 case SkXfermode::kColor_Mode:
566 case SkXfermode::kLuminosity_Mode:
567 // supported blend modes
568 break;
569 case SkXfermode::kClear_Mode:
570 case SkXfermode::kSrc_Mode:
571 case SkXfermode::kDst_Mode:
572 case SkXfermode::kDstOver_Mode:
573 case SkXfermode::kSrcIn_Mode:
574 case SkXfermode::kDstIn_Mode:
575 case SkXfermode::kSrcOut_Mode:
576 case SkXfermode::kDstOut_Mode:
577 case SkXfermode::kSrcATop_Mode:
578 case SkXfermode::kDstATop_Mode:
579 case SkXfermode::kXor_Mode:
580 case SkXfermode::kPlus_Mode:
581 case SkXfermode::kModulate_Mode:
582 // Porter Duff Compositing Operators are not yet supported
583 // http://dev.w3.org/fxtf/compositing-1/#porterduffcompositingoperators
584 NOTREACHED();
585 return;
588 blend_mode_ = blend_mode;
589 SetNeedsCommit();
592 void Layer::SetIsRootForIsolatedGroup(bool root) {
593 DCHECK(IsPropertyChangeAllowed());
594 if (is_root_for_isolated_group_ == root)
595 return;
596 is_root_for_isolated_group_ = root;
597 SetNeedsCommit();
600 void Layer::SetContentsOpaque(bool opaque) {
601 DCHECK(IsPropertyChangeAllowed());
602 if (contents_opaque_ == opaque)
603 return;
604 contents_opaque_ = opaque;
605 SetNeedsCommit();
608 void Layer::SetPosition(const gfx::PointF& position) {
609 DCHECK(IsPropertyChangeAllowed());
610 if (position_ == position)
611 return;
612 position_ = position;
614 if (!layer_tree_host_)
615 return;
617 if (TransformNode* transform_node =
618 layer_tree_host_->property_trees()->transform_tree.Node(
619 transform_tree_index())) {
620 if (transform_node->owner_id == id()) {
621 transform_node->data.update_post_local_transform(position,
622 transform_origin());
623 transform_node->data.needs_local_transform_update = true;
624 layer_tree_host_->property_trees()->transform_tree.set_needs_update(true);
625 SetNeedsCommitNoRebuild();
626 return;
630 SetNeedsCommit();
633 bool Layer::IsContainerForFixedPositionLayers() const {
634 if (!transform_.IsIdentityOrTranslation())
635 return true;
636 if (parent_ && !parent_->transform_.IsIdentityOrTranslation())
637 return true;
638 return is_container_for_fixed_position_layers_;
641 bool Are2dAxisAligned(const gfx::Transform& a,
642 const gfx::Transform& b,
643 bool* is_invertible) {
644 if (a.IsScaleOrTranslation() && b.IsScaleOrTranslation()) {
645 *is_invertible = b.IsInvertible();
646 return true;
649 gfx::Transform inverse(gfx::Transform::kSkipInitialization);
650 *is_invertible = b.GetInverse(&inverse);
652 inverse *= a;
653 return inverse.Preserves2dAxisAlignment();
656 void Layer::SetTransform(const gfx::Transform& transform) {
657 DCHECK(IsPropertyChangeAllowed());
658 if (transform_ == transform)
659 return;
661 if (layer_tree_host_) {
662 if (TransformNode* transform_node =
663 layer_tree_host_->property_trees()->transform_tree.Node(
664 transform_tree_index())) {
665 if (transform_node->owner_id == id()) {
666 // We need to trigger a rebuild if we could have affected 2d axis
667 // alignment. We'll check to see if transform and transform_ are axis
668 // align with respect to one another.
669 bool invertible = false;
670 bool preserves_2d_axis_alignment =
671 Are2dAxisAligned(transform_, transform, &invertible);
672 transform_node->data.local = transform;
673 transform_node->data.needs_local_transform_update = true;
674 layer_tree_host_->property_trees()->transform_tree.set_needs_update(
675 true);
676 if (preserves_2d_axis_alignment)
677 SetNeedsCommitNoRebuild();
678 else
679 SetNeedsCommit();
680 transform_ = transform;
681 transform_is_invertible_ = invertible;
682 return;
687 transform_ = transform;
688 transform_is_invertible_ = transform.IsInvertible();
690 SetNeedsCommit();
693 void Layer::SetTransformOrigin(const gfx::Point3F& transform_origin) {
694 DCHECK(IsPropertyChangeAllowed());
695 if (transform_origin_ == transform_origin)
696 return;
697 transform_origin_ = transform_origin;
699 if (!layer_tree_host_)
700 return;
702 if (TransformNode* transform_node =
703 layer_tree_host_->property_trees()->transform_tree.Node(
704 transform_tree_index())) {
705 if (transform_node->owner_id == id()) {
706 transform_node->data.update_pre_local_transform(transform_origin);
707 transform_node->data.update_post_local_transform(position(),
708 transform_origin);
709 transform_node->data.needs_local_transform_update = true;
710 layer_tree_host_->property_trees()->transform_tree.set_needs_update(true);
711 SetNeedsCommitNoRebuild();
712 return;
716 SetNeedsCommit();
719 bool Layer::AnimationsPreserveAxisAlignment() const {
720 return layer_animation_controller_->AnimationsPreserveAxisAlignment();
723 bool Layer::TransformIsAnimating() const {
724 return layer_animation_controller_->IsAnimatingProperty(Animation::TRANSFORM);
727 void Layer::SetScrollParent(Layer* parent) {
728 DCHECK(IsPropertyChangeAllowed());
729 if (scroll_parent_ == parent)
730 return;
732 if (scroll_parent_)
733 scroll_parent_->RemoveScrollChild(this);
735 scroll_parent_ = parent;
737 if (scroll_parent_)
738 scroll_parent_->AddScrollChild(this);
740 SetNeedsCommit();
743 void Layer::AddScrollChild(Layer* child) {
744 if (!scroll_children_)
745 scroll_children_.reset(new std::set<Layer*>);
746 scroll_children_->insert(child);
747 if (layer_tree_host_ && !layer_tree_host_->needs_meta_info_recomputation()) {
748 num_children_with_scroll_parent_++;
749 draw_properties().has_child_with_a_scroll_parent = true;
751 SetNeedsCommit();
754 void Layer::RemoveScrollChild(Layer* child) {
755 scroll_children_->erase(child);
756 if (scroll_children_->empty())
757 scroll_children_ = nullptr;
758 if (layer_tree_host_ && !layer_tree_host_->needs_meta_info_recomputation()) {
759 num_children_with_scroll_parent_--;
760 DCHECK_GE(num_children_with_scroll_parent_, 0);
761 draw_properties().has_child_with_a_scroll_parent =
762 (num_children_with_scroll_parent_ != 0);
764 SetNeedsCommit();
767 void Layer::SetClipParent(Layer* ancestor) {
768 DCHECK(IsPropertyChangeAllowed());
769 if (clip_parent_ == ancestor)
770 return;
772 if (clip_parent_)
773 clip_parent_->RemoveClipChild(this);
775 clip_parent_ = ancestor;
777 if (clip_parent_)
778 clip_parent_->AddClipChild(this);
780 SetNeedsCommit();
781 if (layer_tree_host_)
782 layer_tree_host_->SetNeedsMetaInfoRecomputation(true);
785 void Layer::AddClipChild(Layer* child) {
786 if (!clip_children_)
787 clip_children_.reset(new std::set<Layer*>);
788 clip_children_->insert(child);
789 SetNeedsCommit();
792 void Layer::RemoveClipChild(Layer* child) {
793 clip_children_->erase(child);
794 if (clip_children_->empty())
795 clip_children_ = nullptr;
796 SetNeedsCommit();
799 void Layer::SetScrollOffset(const gfx::ScrollOffset& scroll_offset) {
800 DCHECK(IsPropertyChangeAllowed());
802 if (scroll_offset_ == scroll_offset)
803 return;
804 scroll_offset_ = scroll_offset;
806 if (!layer_tree_host_)
807 return;
809 if (TransformNode* transform_node =
810 layer_tree_host_->property_trees()->transform_tree.Node(
811 transform_tree_index())) {
812 if (transform_node->owner_id == id()) {
813 transform_node->data.scroll_offset = CurrentScrollOffset();
814 transform_node->data.needs_local_transform_update = true;
815 layer_tree_host_->property_trees()->transform_tree.set_needs_update(true);
816 SetNeedsCommitNoRebuild();
817 return;
821 SetNeedsCommit();
824 void Layer::SetScrollCompensationAdjustment(
825 const gfx::Vector2dF& scroll_compensation_adjustment) {
826 if (scroll_compensation_adjustment_ == scroll_compensation_adjustment)
827 return;
828 scroll_compensation_adjustment_ = scroll_compensation_adjustment;
829 SetNeedsCommit();
832 gfx::Vector2dF Layer::ScrollCompensationAdjustment() const {
833 return scroll_compensation_adjustment_;
836 void Layer::SetScrollOffsetFromImplSide(
837 const gfx::ScrollOffset& scroll_offset) {
838 DCHECK(IsPropertyChangeAllowed());
839 // This function only gets called during a BeginMainFrame, so there
840 // is no need to call SetNeedsUpdate here.
841 DCHECK(layer_tree_host_ && layer_tree_host_->CommitRequested());
842 if (scroll_offset_ == scroll_offset)
843 return;
844 scroll_offset_ = scroll_offset;
845 SetNeedsPushProperties();
847 bool needs_rebuild = true;
848 if (TransformNode* transform_node =
849 layer_tree_host_->property_trees()->transform_tree.Node(
850 transform_tree_index())) {
851 if (transform_node->owner_id == id()) {
852 transform_node->data.scroll_offset = CurrentScrollOffset();
853 transform_node->data.needs_local_transform_update = true;
854 layer_tree_host_->property_trees()->transform_tree.set_needs_update(true);
855 needs_rebuild = false;
859 if (needs_rebuild)
860 layer_tree_host_->property_trees()->needs_rebuild = true;
862 if (!did_scroll_callback_.is_null())
863 did_scroll_callback_.Run();
864 // The callback could potentially change the layer structure:
865 // "this" may have been destroyed during the process.
868 void Layer::SetScrollClipLayerId(int clip_layer_id) {
869 DCHECK(IsPropertyChangeAllowed());
870 if (scroll_clip_layer_id_ == clip_layer_id)
871 return;
872 scroll_clip_layer_id_ = clip_layer_id;
873 SetNeedsCommit();
876 void Layer::SetUserScrollable(bool horizontal, bool vertical) {
877 DCHECK(IsPropertyChangeAllowed());
878 if (user_scrollable_horizontal_ == horizontal &&
879 user_scrollable_vertical_ == vertical)
880 return;
881 user_scrollable_horizontal_ = horizontal;
882 user_scrollable_vertical_ = vertical;
883 SetNeedsCommit();
886 void Layer::SetShouldScrollOnMainThread(bool should_scroll_on_main_thread) {
887 DCHECK(IsPropertyChangeAllowed());
888 if (should_scroll_on_main_thread_ == should_scroll_on_main_thread)
889 return;
890 should_scroll_on_main_thread_ = should_scroll_on_main_thread;
891 SetNeedsCommit();
894 void Layer::SetHaveWheelEventHandlers(bool have_wheel_event_handlers) {
895 DCHECK(IsPropertyChangeAllowed());
896 if (have_wheel_event_handlers_ == have_wheel_event_handlers)
897 return;
898 if (touch_event_handler_region_.IsEmpty() && layer_tree_host_ &&
899 !layer_tree_host_->needs_meta_info_recomputation())
900 UpdateNumInputHandlersForSubtree(have_wheel_event_handlers);
902 have_wheel_event_handlers_ = have_wheel_event_handlers;
903 SetNeedsCommit();
906 void Layer::UpdateNumInputHandlersForSubtree(bool add) {
907 int change = add ? 1 : -1;
908 for (Layer* layer = this; layer; layer = layer->parent()) {
909 layer->num_layer_or_descendants_with_input_handler_ += change;
910 layer->draw_properties().layer_or_descendant_has_input_handler =
911 (layer->num_layer_or_descendants_with_input_handler_ != 0);
912 DCHECK_GE(layer->num_layer_or_descendants_with_input_handler_, 0);
916 void Layer::SetHaveScrollEventHandlers(bool have_scroll_event_handlers) {
917 DCHECK(IsPropertyChangeAllowed());
918 if (have_scroll_event_handlers_ == have_scroll_event_handlers)
919 return;
920 have_scroll_event_handlers_ = have_scroll_event_handlers;
921 SetNeedsCommit();
924 void Layer::SetNonFastScrollableRegion(const Region& region) {
925 DCHECK(IsPropertyChangeAllowed());
926 if (non_fast_scrollable_region_ == region)
927 return;
928 non_fast_scrollable_region_ = region;
929 SetNeedsCommit();
932 void Layer::SetTouchEventHandlerRegion(const Region& region) {
933 DCHECK(IsPropertyChangeAllowed());
934 if (touch_event_handler_region_ == region)
935 return;
936 if (!have_wheel_event_handlers_ && layer_tree_host_ &&
937 !layer_tree_host_->needs_meta_info_recomputation())
938 UpdateNumInputHandlersForSubtree(!region.IsEmpty());
940 touch_event_handler_region_ = region;
941 SetNeedsCommit();
944 void Layer::SetScrollBlocksOn(ScrollBlocksOn scroll_blocks_on) {
945 DCHECK(IsPropertyChangeAllowed());
946 if (scroll_blocks_on_ == scroll_blocks_on)
947 return;
948 scroll_blocks_on_ = scroll_blocks_on;
949 SetNeedsCommit();
952 void Layer::SetDrawCheckerboardForMissingTiles(bool checkerboard) {
953 DCHECK(IsPropertyChangeAllowed());
954 if (draw_checkerboard_for_missing_tiles_ == checkerboard)
955 return;
956 draw_checkerboard_for_missing_tiles_ = checkerboard;
957 SetNeedsCommit();
960 void Layer::SetForceRenderSurface(bool force) {
961 DCHECK(IsPropertyChangeAllowed());
962 if (force_render_surface_ == force)
963 return;
964 force_render_surface_ = force;
965 SetNeedsCommit();
968 void Layer::SetDoubleSided(bool double_sided) {
969 DCHECK(IsPropertyChangeAllowed());
970 if (double_sided_ == double_sided)
971 return;
972 double_sided_ = double_sided;
973 SetNeedsCommit();
976 void Layer::Set3dSortingContextId(int id) {
977 DCHECK(IsPropertyChangeAllowed());
978 if (id == sorting_context_id_)
979 return;
980 sorting_context_id_ = id;
981 SetNeedsCommit();
984 void Layer::SetTransformTreeIndex(int index) {
985 DCHECK(IsPropertyChangeAllowed());
986 if (transform_tree_index_ == index)
987 return;
988 transform_tree_index_ = index;
989 SetNeedsPushProperties();
992 int Layer::transform_tree_index() const {
993 if (!layer_tree_host_ ||
994 layer_tree_host_->property_trees()->sequence_number !=
995 property_tree_sequence_number_) {
996 return -1;
998 return transform_tree_index_;
1001 void Layer::SetClipTreeIndex(int index) {
1002 DCHECK(IsPropertyChangeAllowed());
1003 if (clip_tree_index_ == index)
1004 return;
1005 clip_tree_index_ = index;
1006 SetNeedsPushProperties();
1009 int Layer::clip_tree_index() const {
1010 if (!layer_tree_host_ ||
1011 layer_tree_host_->property_trees()->sequence_number !=
1012 property_tree_sequence_number_) {
1013 return -1;
1015 return clip_tree_index_;
1018 void Layer::SetOpacityTreeIndex(int index) {
1019 DCHECK(IsPropertyChangeAllowed());
1020 if (opacity_tree_index_ == index)
1021 return;
1022 opacity_tree_index_ = index;
1023 SetNeedsPushProperties();
1026 int Layer::opacity_tree_index() const {
1027 if (!layer_tree_host_ ||
1028 layer_tree_host_->property_trees()->sequence_number !=
1029 property_tree_sequence_number_) {
1030 return -1;
1032 return opacity_tree_index_;
1035 void Layer::InvalidatePropertyTreesIndices() {
1036 int invalid_property_tree_index = -1;
1037 SetTransformTreeIndex(invalid_property_tree_index);
1038 SetClipTreeIndex(invalid_property_tree_index);
1039 SetOpacityTreeIndex(invalid_property_tree_index);
1042 void Layer::SetShouldFlattenTransform(bool should_flatten) {
1043 DCHECK(IsPropertyChangeAllowed());
1044 if (should_flatten_transform_ == should_flatten)
1045 return;
1046 should_flatten_transform_ = should_flatten;
1047 SetNeedsCommit();
1050 void Layer::SetIsDrawable(bool is_drawable) {
1051 DCHECK(IsPropertyChangeAllowed());
1052 if (is_drawable_ == is_drawable)
1053 return;
1055 is_drawable_ = is_drawable;
1056 UpdateDrawsContent(HasDrawableContent());
1059 void Layer::SetHideLayerAndSubtree(bool hide) {
1060 DCHECK(IsPropertyChangeAllowed());
1061 if (hide_layer_and_subtree_ == hide)
1062 return;
1064 hide_layer_and_subtree_ = hide;
1065 SetNeedsCommit();
1068 void Layer::SetNeedsDisplayRect(const gfx::Rect& dirty_rect) {
1069 if (dirty_rect.IsEmpty())
1070 return;
1072 SetNeedsPushProperties();
1073 update_rect_.Union(dirty_rect);
1075 if (DrawsContent())
1076 SetNeedsUpdate();
1079 bool Layer::DescendantIsFixedToContainerLayer() const {
1080 for (size_t i = 0; i < children_.size(); ++i) {
1081 if (children_[i]->position_constraint_.is_fixed_position() ||
1082 children_[i]->DescendantIsFixedToContainerLayer())
1083 return true;
1085 return false;
1088 void Layer::SetIsContainerForFixedPositionLayers(bool container) {
1089 if (is_container_for_fixed_position_layers_ == container)
1090 return;
1091 is_container_for_fixed_position_layers_ = container;
1093 if (layer_tree_host_ && layer_tree_host_->CommitRequested())
1094 return;
1096 // Only request a commit if we have a fixed positioned descendant.
1097 if (DescendantIsFixedToContainerLayer())
1098 SetNeedsCommit();
1101 void Layer::SetPositionConstraint(const LayerPositionConstraint& constraint) {
1102 DCHECK(IsPropertyChangeAllowed());
1103 if (position_constraint_ == constraint)
1104 return;
1105 position_constraint_ = constraint;
1106 SetNeedsCommit();
1109 static void RunCopyCallbackOnMainThread(scoped_ptr<CopyOutputRequest> request,
1110 scoped_ptr<CopyOutputResult> result) {
1111 request->SendResult(result.Pass());
1114 static void PostCopyCallbackToMainThread(
1115 scoped_refptr<base::SingleThreadTaskRunner> main_thread_task_runner,
1116 scoped_ptr<CopyOutputRequest> request,
1117 scoped_ptr<CopyOutputResult> result) {
1118 main_thread_task_runner->PostTask(FROM_HERE,
1119 base::Bind(&RunCopyCallbackOnMainThread,
1120 base::Passed(&request),
1121 base::Passed(&result)));
1124 void Layer::PushPropertiesTo(LayerImpl* layer) {
1125 DCHECK(layer_tree_host_);
1127 // If we did not SavePaintProperties() for the layer this frame, then push the
1128 // real property values, not the paint property values.
1129 bool use_paint_properties = paint_properties_.source_frame_number ==
1130 layer_tree_host_->source_frame_number();
1132 layer->SetTransformOrigin(transform_origin_);
1133 layer->SetBackgroundColor(background_color_);
1134 layer->SetBounds(use_paint_properties ? paint_properties_.bounds
1135 : bounds_);
1137 if (frame_viewer_instrumentation::IsTracingLayerTreeSnapshots())
1138 layer->SetDebugInfo(TakeDebugInfo());
1140 layer->SetTransformTreeIndex(transform_tree_index());
1141 layer->SetOpacityTreeIndex(opacity_tree_index());
1142 layer->SetClipTreeIndex(clip_tree_index());
1143 layer->set_offset_to_transform_parent(offset_to_transform_parent_);
1144 layer->SetDoubleSided(double_sided_);
1145 layer->SetDrawCheckerboardForMissingTiles(
1146 draw_checkerboard_for_missing_tiles_);
1147 layer->SetDrawsContent(DrawsContent());
1148 layer->SetHideLayerAndSubtree(hide_layer_and_subtree_);
1149 layer->SetHasRenderSurface(has_render_surface_);
1150 if (!layer->FilterIsAnimatingOnImplOnly() && !FilterIsAnimating())
1151 layer->SetFilters(filters_);
1152 DCHECK(!(FilterIsAnimating() && layer->FilterIsAnimatingOnImplOnly()));
1153 layer->SetBackgroundFilters(background_filters());
1154 layer->SetMasksToBounds(masks_to_bounds_);
1155 layer->SetShouldScrollOnMainThread(should_scroll_on_main_thread_);
1156 layer->SetHaveWheelEventHandlers(have_wheel_event_handlers_);
1157 layer->SetHaveScrollEventHandlers(have_scroll_event_handlers_);
1158 layer->SetNonFastScrollableRegion(non_fast_scrollable_region_);
1159 layer->SetTouchEventHandlerRegion(touch_event_handler_region_);
1160 layer->SetScrollBlocksOn(scroll_blocks_on_);
1161 layer->SetContentsOpaque(contents_opaque_);
1162 if (!layer->OpacityIsAnimatingOnImplOnly() && !OpacityIsAnimating()) {
1163 layer->SetOpacity(opacity_);
1164 } else {
1165 // The just-pushed opacity tree will contain |opacity_|. Since we didn't
1166 // push this value to |layer|, it might have a different opacity value. To
1167 // ensure consistency, we need to update the opacity tree.
1168 layer->UpdatePropertyTreeOpacity();
1170 DCHECK(!(OpacityIsAnimating() && layer->OpacityIsAnimatingOnImplOnly()));
1171 layer->SetBlendMode(blend_mode_);
1172 layer->SetIsRootForIsolatedGroup(is_root_for_isolated_group_);
1173 layer->SetPosition(position_);
1174 layer->SetIsContainerForFixedPositionLayers(
1175 IsContainerForFixedPositionLayers());
1176 layer->SetPositionConstraint(position_constraint_);
1177 layer->SetShouldFlattenTransform(should_flatten_transform_);
1178 layer->set_should_flatten_transform_from_property_tree(
1179 should_flatten_transform_from_property_tree_);
1180 layer->SetUseParentBackfaceVisibility(use_parent_backface_visibility_);
1181 if (!layer->TransformIsAnimatingOnImplOnly() && !TransformIsAnimating()) {
1182 layer->SetTransformAndInvertibility(transform_, transform_is_invertible_);
1183 } else {
1184 // The just-pushed transform tree will contain |transform_|. Since we
1185 // didn't push this value to |layer|, it might have a different transform
1186 // value. To ensure consistency, we need to update the transform tree.
1187 layer->UpdatePropertyTreeTransform();
1189 DCHECK(!(TransformIsAnimating() && layer->TransformIsAnimatingOnImplOnly()));
1190 layer->Set3dSortingContextId(sorting_context_id_);
1191 layer->SetNumDescendantsThatDrawContent(num_descendants_that_draw_content_);
1193 layer->SetScrollClipLayer(scroll_clip_layer_id_);
1194 layer->set_user_scrollable_horizontal(user_scrollable_horizontal_);
1195 layer->set_user_scrollable_vertical(user_scrollable_vertical_);
1197 LayerImpl* scroll_parent = nullptr;
1198 if (scroll_parent_) {
1199 scroll_parent = layer->layer_tree_impl()->LayerById(scroll_parent_->id());
1200 DCHECK(scroll_parent);
1203 layer->SetScrollParent(scroll_parent);
1204 if (scroll_children_) {
1205 std::set<LayerImpl*>* scroll_children = new std::set<LayerImpl*>;
1206 for (std::set<Layer*>::iterator it = scroll_children_->begin();
1207 it != scroll_children_->end();
1208 ++it) {
1209 DCHECK_EQ((*it)->scroll_parent(), this);
1210 LayerImpl* scroll_child =
1211 layer->layer_tree_impl()->LayerById((*it)->id());
1212 DCHECK(scroll_child);
1213 scroll_children->insert(scroll_child);
1215 layer->SetScrollChildren(scroll_children);
1216 } else {
1217 layer->SetScrollChildren(nullptr);
1220 LayerImpl* clip_parent = nullptr;
1221 if (clip_parent_) {
1222 clip_parent =
1223 layer->layer_tree_impl()->LayerById(clip_parent_->id());
1224 DCHECK(clip_parent);
1227 layer->SetClipParent(clip_parent);
1228 if (clip_children_) {
1229 std::set<LayerImpl*>* clip_children = new std::set<LayerImpl*>;
1230 for (std::set<Layer*>::iterator it = clip_children_->begin();
1231 it != clip_children_->end(); ++it) {
1232 DCHECK_EQ((*it)->clip_parent(), this);
1233 LayerImpl* clip_child = layer->layer_tree_impl()->LayerById((*it)->id());
1234 DCHECK(clip_child);
1235 clip_children->insert(clip_child);
1237 layer->SetClipChildren(clip_children);
1238 } else {
1239 layer->SetClipChildren(nullptr);
1242 // When a scroll offset animation is interrupted the new scroll position on
1243 // the pending tree will clobber any impl-side scrolling occuring on the
1244 // active tree. To do so, avoid scrolling the pending tree along with it
1245 // instead of trying to undo that scrolling later.
1246 if (layer_animation_controller_->scroll_offset_animation_was_interrupted())
1247 layer->PushScrollOffsetFromMainThreadAndClobberActiveValue(scroll_offset_);
1248 else
1249 layer->PushScrollOffsetFromMainThread(scroll_offset_);
1250 layer->SetScrollCompensationAdjustment(ScrollCompensationAdjustment());
1252 // Wrap the copy_requests_ in a PostTask to the main thread.
1253 bool had_copy_requests = !copy_requests_.empty();
1254 ScopedPtrVector<CopyOutputRequest> main_thread_copy_requests;
1255 for (ScopedPtrVector<CopyOutputRequest>::iterator it = copy_requests_.begin();
1256 it != copy_requests_.end();
1257 ++it) {
1258 scoped_refptr<base::SingleThreadTaskRunner> main_thread_task_runner =
1259 layer_tree_host()->proxy()->MainThreadTaskRunner();
1260 scoped_ptr<CopyOutputRequest> original_request = copy_requests_.take(it);
1261 const CopyOutputRequest& original_request_ref = *original_request;
1262 scoped_ptr<CopyOutputRequest> main_thread_request =
1263 CopyOutputRequest::CreateRelayRequest(
1264 original_request_ref,
1265 base::Bind(&PostCopyCallbackToMainThread,
1266 main_thread_task_runner,
1267 base::Passed(&original_request)));
1268 main_thread_copy_requests.push_back(main_thread_request.Pass());
1270 if (!copy_requests_.empty() && layer_tree_host_)
1271 layer_tree_host_->property_trees()->needs_rebuild = true;
1272 if (had_copy_requests)
1273 UpdateNumCopyRequestsForSubtree(false);
1274 copy_requests_.clear();
1275 layer->PassCopyRequests(&main_thread_copy_requests);
1277 // If the main thread commits multiple times before the impl thread actually
1278 // draws, then damage tracking will become incorrect if we simply clobber the
1279 // update_rect here. The LayerImpl's update_rect needs to accumulate (i.e.
1280 // union) any update changes that have occurred on the main thread.
1281 update_rect_.Union(layer->update_rect());
1282 layer->SetUpdateRect(update_rect_);
1284 layer->SetStackingOrderChanged(stacking_order_changed_);
1286 layer_animation_controller_->PushAnimationUpdatesTo(
1287 layer->layer_animation_controller());
1289 if (frame_timing_requests_dirty_) {
1290 layer->SetFrameTimingRequests(frame_timing_requests_);
1291 frame_timing_requests_dirty_ = false;
1294 // Reset any state that should be cleared for the next update.
1295 stacking_order_changed_ = false;
1296 update_rect_ = gfx::Rect();
1298 needs_push_properties_ = false;
1299 num_dependents_need_push_properties_ = 0;
1302 scoped_ptr<LayerImpl> Layer::CreateLayerImpl(LayerTreeImpl* tree_impl) {
1303 return LayerImpl::Create(tree_impl, layer_id_,
1304 new LayerImpl::SyncedScrollOffset);
1307 bool Layer::DrawsContent() const {
1308 return draws_content_;
1311 bool Layer::HasDrawableContent() const {
1312 return is_drawable_;
1315 void Layer::UpdateDrawsContent(bool has_drawable_content) {
1316 bool draws_content = has_drawable_content;
1317 DCHECK(is_drawable_ || !has_drawable_content);
1318 if (draws_content == draws_content_)
1319 return;
1321 if (HasDelegatedContent()) {
1322 // Layers with delegated content need to be treated as if they have as
1323 // many children as the number of layers they own delegated quads for.
1324 // Since we don't know this number right now, we choose one that acts like
1325 // infinity for our purposes.
1326 AddDrawableDescendants(draws_content ? 1000 : -1000);
1329 if (parent())
1330 parent()->AddDrawableDescendants(draws_content ? 1 : -1);
1332 draws_content_ = draws_content;
1333 SetNeedsCommit();
1336 int Layer::NumDescendantsThatDrawContent() const {
1337 return num_descendants_that_draw_content_;
1340 void Layer::SavePaintProperties() {
1341 DCHECK(layer_tree_host_);
1343 // TODO(reveman): Save all layer properties that we depend on not
1344 // changing until PushProperties() has been called. crbug.com/231016
1345 paint_properties_.bounds = bounds_;
1346 paint_properties_.source_frame_number =
1347 layer_tree_host_->source_frame_number();
1350 bool Layer::Update() {
1351 DCHECK(layer_tree_host_);
1352 DCHECK_EQ(layer_tree_host_->source_frame_number(),
1353 paint_properties_.source_frame_number) <<
1354 "SavePaintProperties must be called for any layer that is painted.";
1355 return false;
1358 bool Layer::NeedMoreUpdates() {
1359 return false;
1362 bool Layer::IsSuitableForGpuRasterization() const {
1363 return true;
1366 scoped_refptr<base::trace_event::ConvertableToTraceFormat>
1367 Layer::TakeDebugInfo() {
1368 if (client_)
1369 return client_->TakeDebugInfo();
1370 else
1371 return nullptr;
1374 void Layer::SetHasRenderSurface(bool has_render_surface) {
1375 if (has_render_surface_ == has_render_surface)
1376 return;
1377 has_render_surface_ = has_render_surface;
1378 // We do not need SetNeedsCommit here, since this is only ever called
1379 // during a commit, from CalculateDrawProperties.
1380 SetNeedsPushProperties();
1383 void Layer::CreateRenderSurface() {
1384 DCHECK(!render_surface_);
1385 render_surface_ = make_scoped_ptr(new RenderSurface(this));
1388 void Layer::ClearRenderSurface() {
1389 render_surface_ = nullptr;
1392 void Layer::ClearRenderSurfaceLayerList() {
1393 if (render_surface_)
1394 render_surface_->ClearLayerLists();
1397 gfx::ScrollOffset Layer::ScrollOffsetForAnimation() const {
1398 return CurrentScrollOffset();
1401 // On<Property>Animated is called due to an ongoing accelerated animation.
1402 // Since this animation is also being run on the compositor thread, there
1403 // is no need to request a commit to push this value over, so the value is
1404 // set directly rather than by calling Set<Property>.
1405 void Layer::OnFilterAnimated(const FilterOperations& filters) {
1406 filters_ = filters;
1409 void Layer::OnOpacityAnimated(float opacity) {
1410 opacity_ = opacity;
1411 if (layer_tree_host_) {
1412 if (OpacityNode* node =
1413 layer_tree_host_->property_trees()->opacity_tree.Node(
1414 opacity_tree_index())) {
1415 if (node->owner_id == id()) {
1416 node->data.opacity = opacity;
1417 layer_tree_host_->property_trees()->opacity_tree.set_needs_update(true);
1423 void Layer::OnTransformAnimated(const gfx::Transform& transform) {
1424 if (transform_ == transform)
1425 return;
1426 transform_ = transform;
1427 transform_is_invertible_ = transform.IsInvertible();
1428 if (layer_tree_host_) {
1429 if (TransformNode* node =
1430 layer_tree_host_->property_trees()->transform_tree.Node(
1431 transform_tree_index())) {
1432 if (node->owner_id == id()) {
1433 node->data.local = transform;
1434 node->data.needs_local_transform_update = true;
1435 node->data.is_animated = true;
1436 layer_tree_host_->property_trees()->transform_tree.set_needs_update(
1437 true);
1443 void Layer::OnScrollOffsetAnimated(const gfx::ScrollOffset& scroll_offset) {
1444 // Do nothing. Scroll deltas will be sent from the compositor thread back
1445 // to the main thread in the same manner as during non-animated
1446 // compositor-driven scrolling.
1449 void Layer::OnAnimationWaitingForDeletion() {
1450 // Animations are only deleted during PushProperties.
1451 SetNeedsPushProperties();
1454 bool Layer::IsActive() const {
1455 return true;
1458 bool Layer::AddAnimation(scoped_ptr <Animation> animation) {
1459 DCHECK(layer_animation_controller_);
1460 if (!layer_animation_controller_->animation_registrar())
1461 return false;
1463 if (animation->target_property() == Animation::SCROLL_OFFSET &&
1464 !layer_animation_controller_->animation_registrar()
1465 ->supports_scroll_animations())
1466 return false;
1468 UMA_HISTOGRAM_BOOLEAN("Renderer.AnimationAddedToOrphanLayer",
1469 !layer_tree_host_);
1470 layer_animation_controller_->AddAnimation(animation.Pass());
1471 SetNeedsCommit();
1472 return true;
1475 void Layer::PauseAnimation(int animation_id, double time_offset) {
1476 DCHECK(layer_animation_controller_);
1477 layer_animation_controller_->PauseAnimation(
1478 animation_id, base::TimeDelta::FromSecondsD(time_offset));
1479 SetNeedsCommit();
1482 void Layer::RemoveAnimation(int animation_id) {
1483 DCHECK(layer_animation_controller_);
1484 layer_animation_controller_->RemoveAnimation(animation_id);
1485 SetNeedsCommit();
1488 void Layer::RemoveAnimation(int animation_id,
1489 Animation::TargetProperty property) {
1490 DCHECK(layer_animation_controller_);
1491 layer_animation_controller_->RemoveAnimation(animation_id, property);
1492 SetNeedsCommit();
1495 void Layer::SetLayerAnimationControllerForTest(
1496 scoped_refptr<LayerAnimationController> controller) {
1497 layer_animation_controller_->RemoveValueObserver(this);
1498 layer_animation_controller_ = controller;
1499 layer_animation_controller_->AddValueObserver(this);
1500 SetNeedsCommit();
1503 bool Layer::HasActiveAnimation() const {
1504 DCHECK(layer_animation_controller_);
1505 return layer_animation_controller_->HasActiveAnimation();
1508 void Layer::RegisterForAnimations(AnimationRegistrar* registrar) {
1509 if (layer_animation_controller_)
1510 layer_animation_controller_->SetAnimationRegistrar(registrar);
1513 void Layer::AddLayerAnimationEventObserver(
1514 LayerAnimationEventObserver* animation_observer) {
1515 DCHECK(layer_animation_controller_);
1516 layer_animation_controller_->AddEventObserver(animation_observer);
1519 void Layer::RemoveLayerAnimationEventObserver(
1520 LayerAnimationEventObserver* animation_observer) {
1521 DCHECK(layer_animation_controller_);
1522 layer_animation_controller_->RemoveEventObserver(animation_observer);
1525 ScrollbarLayerInterface* Layer::ToScrollbarLayer() {
1526 return nullptr;
1529 RenderingStatsInstrumentation* Layer::rendering_stats_instrumentation() const {
1530 return layer_tree_host_->rendering_stats_instrumentation();
1533 void Layer::RemoveFromScrollTree() {
1534 if (scroll_children_.get()) {
1535 std::set<Layer*> copy = *scroll_children_;
1536 for (std::set<Layer*>::iterator it = copy.begin(); it != copy.end(); ++it)
1537 (*it)->SetScrollParent(nullptr);
1540 DCHECK(!scroll_children_);
1541 SetScrollParent(nullptr);
1544 void Layer::RemoveFromClipTree() {
1545 if (clip_children_.get()) {
1546 std::set<Layer*> copy = *clip_children_;
1547 for (std::set<Layer*>::iterator it = copy.begin(); it != copy.end(); ++it)
1548 (*it)->SetClipParent(nullptr);
1551 DCHECK(!clip_children_);
1552 SetClipParent(nullptr);
1555 void Layer::AddDrawableDescendants(int num) {
1556 DCHECK_GE(num_descendants_that_draw_content_, 0);
1557 DCHECK_GE(num_descendants_that_draw_content_ + num, 0);
1558 if (num == 0)
1559 return;
1560 num_descendants_that_draw_content_ += num;
1561 SetNeedsCommit();
1562 if (parent())
1563 parent()->AddDrawableDescendants(num);
1566 void Layer::RunMicroBenchmark(MicroBenchmark* benchmark) {
1567 benchmark->RunOnLayer(this);
1570 bool Layer::HasDelegatedContent() const {
1571 return false;
1574 void Layer::SetFrameTimingRequests(
1575 const std::vector<FrameTimingRequest>& requests) {
1576 // TODO(vmpstr): Early out if there are no changes earlier in the call stack.
1577 if (requests == frame_timing_requests_)
1578 return;
1579 frame_timing_requests_ = requests;
1580 frame_timing_requests_dirty_ = true;
1581 SetNeedsCommit();
1584 void Layer::DidBeginTracing() {
1585 // We'll be dumping layer trees as part of trace, so make sure
1586 // PushPropertiesTo() propagates layer debug info to the impl
1587 // side -- otherwise this won't happen for the the layers that
1588 // remain unchanged since tracing started.
1589 SetNeedsPushProperties();
1592 void Layer::set_visited(bool visited) {
1593 visited_tracker_ =
1594 visited ? layer_tree_host()->meta_information_sequence_number() : 0;
1597 bool Layer::visited() {
1598 return visited_tracker_ ==
1599 layer_tree_host()->meta_information_sequence_number();
1602 void Layer::set_layer_or_descendant_is_drawn(
1603 bool layer_or_descendant_is_drawn) {
1604 layer_or_descendant_is_drawn_tracker_ =
1605 layer_or_descendant_is_drawn
1606 ? layer_tree_host()->meta_information_sequence_number()
1607 : 0;
1610 bool Layer::layer_or_descendant_is_drawn() {
1611 return layer_or_descendant_is_drawn_tracker_ ==
1612 layer_tree_host()->meta_information_sequence_number();
1615 void Layer::set_sorted_for_recursion(bool sorted_for_recursion) {
1616 sorted_for_recursion_tracker_ =
1617 sorted_for_recursion
1618 ? layer_tree_host()->meta_information_sequence_number()
1619 : 0;
1622 bool Layer::sorted_for_recursion() {
1623 return sorted_for_recursion_tracker_ ==
1624 layer_tree_host()->meta_information_sequence_number();
1627 } // namespace cc