Disable flaky AnimatedContentSamplerParameterizedTest.FrameTimestampsConvergeTowardsE...
[chromium-blink-merge.git] / cc / layers / layer.cc
blob60145c9eb5dda88011259d3a55759ecce6528fa2
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() {
38 return make_scoped_refptr(new Layer());
41 Layer::Layer()
42 : needs_push_properties_(false),
43 num_dependents_need_push_properties_(false),
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 should_flatten_transform_from_property_tree_(false),
60 should_scroll_on_main_thread_(false),
61 have_wheel_event_handlers_(false),
62 have_scroll_event_handlers_(false),
63 user_scrollable_horizontal_(true),
64 user_scrollable_vertical_(true),
65 is_root_for_isolated_group_(false),
66 is_container_for_fixed_position_layers_(false),
67 is_drawable_(false),
68 draws_content_(false),
69 hide_layer_and_subtree_(false),
70 masks_to_bounds_(false),
71 contents_opaque_(false),
72 double_sided_(true),
73 should_flatten_transform_(true),
74 use_parent_backface_visibility_(false),
75 draw_checkerboard_for_missing_tiles_(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 scroll_parent_(nullptr),
84 clip_parent_(nullptr),
85 replica_layer_(nullptr),
86 raster_scale_(0.f),
87 client_(nullptr),
88 frame_timing_requests_dirty_(false) {
89 layer_animation_controller_ = LayerAnimationController::Create(layer_id_);
90 layer_animation_controller_->AddValueObserver(this);
91 layer_animation_controller_->set_value_provider(this);
94 Layer::~Layer() {
95 // Our parent should be holding a reference to us so there should be no
96 // way for us to be destroyed while we still have a parent.
97 DCHECK(!parent());
98 // Similarly we shouldn't have a layer tree host since it also keeps a
99 // reference to us.
100 DCHECK(!layer_tree_host());
102 layer_animation_controller_->RemoveValueObserver(this);
103 layer_animation_controller_->remove_value_provider(this);
105 RemoveFromScrollTree();
106 RemoveFromClipTree();
108 // Remove the parent reference from all children and dependents.
109 RemoveAllChildren();
110 if (mask_layer_.get()) {
111 DCHECK_EQ(this, mask_layer_->parent());
112 mask_layer_->RemoveFromParent();
114 if (replica_layer_.get()) {
115 DCHECK_EQ(this, replica_layer_->parent());
116 replica_layer_->RemoveFromParent();
120 void Layer::SetLayerTreeHost(LayerTreeHost* host) {
121 if (layer_tree_host_ == host)
122 return;
124 if (layer_tree_host_)
125 layer_tree_host_->property_trees()->needs_rebuild = true;
127 layer_tree_host_ = host;
129 // When changing hosts, the layer needs to commit its properties to the impl
130 // side for the new host.
131 SetNeedsPushProperties();
133 for (size_t i = 0; i < children_.size(); ++i)
134 children_[i]->SetLayerTreeHost(host);
136 if (mask_layer_.get())
137 mask_layer_->SetLayerTreeHost(host);
138 if (replica_layer_.get())
139 replica_layer_->SetLayerTreeHost(host);
141 if (host) {
142 layer_animation_controller_->SetAnimationRegistrar(
143 host->animation_registrar());
145 if (host->settings().layer_transforms_should_scale_layer_contents)
146 reset_raster_scale_to_unknown();
149 if (host && layer_animation_controller_->has_any_animation())
150 host->SetNeedsCommit();
153 void Layer::SetNeedsUpdate() {
154 if (layer_tree_host_ && !ignore_set_needs_commit_)
155 layer_tree_host_->SetNeedsUpdateLayers();
158 void Layer::SetNeedsCommit() {
159 if (!layer_tree_host_)
160 return;
162 SetNeedsPushProperties();
163 layer_tree_host_->property_trees()->needs_rebuild = true;
165 if (ignore_set_needs_commit_)
166 return;
168 layer_tree_host_->SetNeedsCommit();
171 void Layer::SetNeedsCommitNoRebuild() {
172 if (!layer_tree_host_)
173 return;
175 SetNeedsPushProperties();
177 if (ignore_set_needs_commit_)
178 return;
180 layer_tree_host_->SetNeedsCommit();
183 void Layer::SetNeedsFullTreeSync() {
184 if (!layer_tree_host_)
185 return;
187 layer_tree_host_->SetNeedsFullTreeSync();
190 void Layer::SetNextCommitWaitsForActivation() {
191 if (!layer_tree_host_)
192 return;
194 layer_tree_host_->SetNextCommitWaitsForActivation();
197 void Layer::SetNeedsPushProperties() {
198 if (needs_push_properties_)
199 return;
200 if (!parent_should_know_need_push_properties() && parent_)
201 parent_->AddDependentNeedsPushProperties();
202 needs_push_properties_ = true;
205 void Layer::AddDependentNeedsPushProperties() {
206 DCHECK_GE(num_dependents_need_push_properties_, 0);
208 if (!parent_should_know_need_push_properties() && parent_)
209 parent_->AddDependentNeedsPushProperties();
211 num_dependents_need_push_properties_++;
214 void Layer::RemoveDependentNeedsPushProperties() {
215 num_dependents_need_push_properties_--;
216 DCHECK_GE(num_dependents_need_push_properties_, 0);
218 if (!parent_should_know_need_push_properties() && parent_)
219 parent_->RemoveDependentNeedsPushProperties();
222 bool Layer::IsPropertyChangeAllowed() const {
223 if (!layer_tree_host_)
224 return true;
226 if (!layer_tree_host_->settings().strict_layer_property_change_checking)
227 return true;
229 return !layer_tree_host_->in_paint_layer_contents();
232 gfx::Rect Layer::LayerRectToContentRect(const gfx::Rect& layer_rect) const {
233 gfx::Rect content_rect = gfx::ScaleToEnclosingRect(
234 layer_rect, contents_scale_x(), contents_scale_y());
235 // Intersect with content rect to avoid the extra pixel because for some
236 // values x and y, ceil((x / y) * y) may be x + 1.
237 content_rect.Intersect(gfx::Rect(content_bounds()));
238 return content_rect;
241 skia::RefPtr<SkPicture> Layer::GetPicture() const {
242 return skia::RefPtr<SkPicture>();
245 void Layer::SetParent(Layer* layer) {
246 DCHECK(!layer || !layer->HasAncestor(this));
248 if (parent_should_know_need_push_properties()) {
249 if (parent_)
250 parent_->RemoveDependentNeedsPushProperties();
251 if (layer)
252 layer->AddDependentNeedsPushProperties();
255 parent_ = layer;
256 SetLayerTreeHost(parent_ ? parent_->layer_tree_host() : nullptr);
258 if (!layer_tree_host_)
259 return;
261 layer_tree_host_->property_trees()->needs_rebuild = true;
263 const LayerTreeSettings& settings = layer_tree_host_->settings();
264 if (!settings.layer_transforms_should_scale_layer_contents)
265 return;
267 reset_raster_scale_to_unknown();
268 if (mask_layer_.get())
269 mask_layer_->reset_raster_scale_to_unknown();
270 if (replica_layer_.get() && replica_layer_->mask_layer_.get())
271 replica_layer_->mask_layer_->reset_raster_scale_to_unknown();
274 void Layer::AddChild(scoped_refptr<Layer> child) {
275 InsertChild(child, children_.size());
278 void Layer::InsertChild(scoped_refptr<Layer> child, size_t index) {
279 DCHECK(IsPropertyChangeAllowed());
280 child->RemoveFromParent();
281 AddDrawableDescendants(child->NumDescendantsThatDrawContent() +
282 (child->DrawsContent() ? 1 : 0));
283 child->SetParent(this);
284 child->stacking_order_changed_ = true;
286 index = std::min(index, children_.size());
287 children_.insert(children_.begin() + index, child);
288 SetNeedsFullTreeSync();
291 void Layer::RemoveFromParent() {
292 DCHECK(IsPropertyChangeAllowed());
293 if (parent_)
294 parent_->RemoveChildOrDependent(this);
297 void Layer::RemoveChildOrDependent(Layer* child) {
298 if (mask_layer_.get() == child) {
299 mask_layer_->SetParent(nullptr);
300 mask_layer_ = nullptr;
301 SetNeedsFullTreeSync();
302 return;
304 if (replica_layer_.get() == child) {
305 replica_layer_->SetParent(nullptr);
306 replica_layer_ = nullptr;
307 SetNeedsFullTreeSync();
308 return;
311 for (LayerList::iterator iter = children_.begin();
312 iter != children_.end();
313 ++iter) {
314 if (iter->get() != child)
315 continue;
317 child->SetParent(nullptr);
318 AddDrawableDescendants(-child->NumDescendantsThatDrawContent() -
319 (child->DrawsContent() ? 1 : 0));
320 children_.erase(iter);
321 SetNeedsFullTreeSync();
322 return;
326 void Layer::ReplaceChild(Layer* reference, scoped_refptr<Layer> new_layer) {
327 DCHECK(reference);
328 DCHECK_EQ(reference->parent(), this);
329 DCHECK(IsPropertyChangeAllowed());
331 if (reference == new_layer.get())
332 return;
334 int reference_index = IndexOfChild(reference);
335 if (reference_index == -1) {
336 NOTREACHED();
337 return;
340 reference->RemoveFromParent();
342 if (new_layer.get()) {
343 new_layer->RemoveFromParent();
344 InsertChild(new_layer, reference_index);
348 int Layer::IndexOfChild(const Layer* reference) {
349 for (size_t i = 0; i < children_.size(); ++i) {
350 if (children_[i].get() == reference)
351 return i;
353 return -1;
356 void Layer::SetBounds(const gfx::Size& size) {
357 DCHECK(IsPropertyChangeAllowed());
358 if (bounds() == size)
359 return;
360 bounds_ = size;
362 if (!layer_tree_host_)
363 return;
365 if (ClipNode* clip_node = layer_tree_host_->property_trees()->clip_tree.Node(
366 clip_tree_index())) {
367 if (clip_node->owner_id == id()) {
368 clip_node->data.clip.set_size(size);
369 layer_tree_host_->property_trees()->clip_tree.set_needs_update(true);
373 SetNeedsCommitNoRebuild();
376 Layer* Layer::RootLayer() {
377 Layer* layer = this;
378 while (layer->parent())
379 layer = layer->parent();
380 return layer;
383 void Layer::RemoveAllChildren() {
384 DCHECK(IsPropertyChangeAllowed());
385 while (children_.size()) {
386 Layer* layer = children_[0].get();
387 DCHECK_EQ(this, layer->parent());
388 layer->RemoveFromParent();
392 void Layer::SetChildren(const LayerList& children) {
393 DCHECK(IsPropertyChangeAllowed());
394 if (children == children_)
395 return;
397 RemoveAllChildren();
398 for (size_t i = 0; i < children.size(); ++i)
399 AddChild(children[i]);
402 bool Layer::HasAncestor(const Layer* ancestor) const {
403 for (const Layer* layer = parent(); layer; layer = layer->parent()) {
404 if (layer == ancestor)
405 return true;
407 return false;
410 void Layer::RequestCopyOfOutput(
411 scoped_ptr<CopyOutputRequest> request) {
412 DCHECK(IsPropertyChangeAllowed());
413 int size = copy_requests_.size();
414 if (void* source = request->source()) {
415 auto it = std::find_if(
416 copy_requests_.begin(), copy_requests_.end(),
417 [source](const CopyOutputRequest* x) { return x->source() == source; });
418 if (it != copy_requests_.end())
419 copy_requests_.erase(it);
421 if (request->IsEmpty())
422 return;
423 copy_requests_.push_back(request.Pass());
424 if (size == 0) {
425 bool copy_request_added = true;
426 UpdateNumCopyRequestsForSubtree(copy_request_added);
428 SetNeedsCommit();
431 void Layer::UpdateNumCopyRequestsForSubtree(bool add) {
432 int change = add ? 1 : -1;
433 for (Layer* layer = this; layer; layer = layer->parent()) {
434 layer->num_layer_or_descendants_with_copy_request_ += change;
435 layer->draw_properties().layer_or_descendant_has_copy_request =
436 (layer->num_layer_or_descendants_with_copy_request_ != 0);
437 DCHECK_GE(layer->num_layer_or_descendants_with_copy_request_, 0);
441 void Layer::SetBackgroundColor(SkColor background_color) {
442 DCHECK(IsPropertyChangeAllowed());
443 if (background_color_ == background_color)
444 return;
445 background_color_ = background_color;
446 SetNeedsCommit();
449 SkColor Layer::SafeOpaqueBackgroundColor() const {
450 SkColor color = background_color();
451 if (SkColorGetA(color) == 255 && !contents_opaque()) {
452 color = SK_ColorTRANSPARENT;
453 } else if (SkColorGetA(color) != 255 && contents_opaque()) {
454 for (const Layer* layer = parent(); layer;
455 layer = layer->parent()) {
456 color = layer->background_color();
457 if (SkColorGetA(color) == 255)
458 break;
460 if (SkColorGetA(color) != 255)
461 color = layer_tree_host_->background_color();
462 if (SkColorGetA(color) != 255)
463 color = SkColorSetA(color, 255);
465 return color;
468 void Layer::CalculateContentsScale(float ideal_contents_scale,
469 float* contents_scale_x,
470 float* contents_scale_y,
471 gfx::Size* content_bounds) {
472 DCHECK(layer_tree_host_);
474 *contents_scale_x = 1;
475 *contents_scale_y = 1;
476 *content_bounds = bounds();
479 void Layer::SetMasksToBounds(bool masks_to_bounds) {
480 DCHECK(IsPropertyChangeAllowed());
481 if (masks_to_bounds_ == masks_to_bounds)
482 return;
483 masks_to_bounds_ = masks_to_bounds;
484 SetNeedsCommit();
487 void Layer::SetMaskLayer(Layer* mask_layer) {
488 DCHECK(IsPropertyChangeAllowed());
489 if (mask_layer_.get() == mask_layer)
490 return;
491 if (mask_layer_.get()) {
492 DCHECK_EQ(this, mask_layer_->parent());
493 mask_layer_->RemoveFromParent();
495 mask_layer_ = mask_layer;
496 if (mask_layer_.get()) {
497 DCHECK(!mask_layer_->parent());
498 mask_layer_->RemoveFromParent();
499 mask_layer_->SetParent(this);
500 mask_layer_->SetIsMask(true);
502 SetNeedsFullTreeSync();
505 void Layer::SetReplicaLayer(Layer* layer) {
506 DCHECK(IsPropertyChangeAllowed());
507 if (replica_layer_.get() == layer)
508 return;
509 if (replica_layer_.get()) {
510 DCHECK_EQ(this, replica_layer_->parent());
511 replica_layer_->RemoveFromParent();
513 replica_layer_ = layer;
514 if (replica_layer_.get()) {
515 DCHECK(!replica_layer_->parent());
516 replica_layer_->RemoveFromParent();
517 replica_layer_->SetParent(this);
519 SetNeedsFullTreeSync();
522 void Layer::SetFilters(const FilterOperations& filters) {
523 DCHECK(IsPropertyChangeAllowed());
524 if (filters_ == filters)
525 return;
526 filters_ = filters;
527 SetNeedsCommit();
530 bool Layer::FilterIsAnimating() const {
531 return layer_animation_controller_->IsAnimatingProperty(Animation::FILTER);
534 void Layer::SetBackgroundFilters(const FilterOperations& filters) {
535 DCHECK(IsPropertyChangeAllowed());
536 if (background_filters_ == filters)
537 return;
538 background_filters_ = filters;
539 SetNeedsCommit();
542 void Layer::SetOpacity(float opacity) {
543 DCHECK(IsPropertyChangeAllowed());
544 if (opacity_ == opacity)
545 return;
546 opacity_ = opacity;
547 SetNeedsCommit();
550 bool Layer::OpacityIsAnimating() const {
551 return layer_animation_controller_->IsAnimatingProperty(Animation::OPACITY);
554 bool Layer::OpacityCanAnimateOnImplThread() const {
555 return false;
558 void Layer::SetBlendMode(SkXfermode::Mode blend_mode) {
559 DCHECK(IsPropertyChangeAllowed());
560 if (blend_mode_ == blend_mode)
561 return;
563 // Allowing only blend modes that are defined in the CSS Compositing standard:
564 // http://dev.w3.org/fxtf/compositing-1/#blending
565 switch (blend_mode) {
566 case SkXfermode::kSrcOver_Mode:
567 case SkXfermode::kScreen_Mode:
568 case SkXfermode::kOverlay_Mode:
569 case SkXfermode::kDarken_Mode:
570 case SkXfermode::kLighten_Mode:
571 case SkXfermode::kColorDodge_Mode:
572 case SkXfermode::kColorBurn_Mode:
573 case SkXfermode::kHardLight_Mode:
574 case SkXfermode::kSoftLight_Mode:
575 case SkXfermode::kDifference_Mode:
576 case SkXfermode::kExclusion_Mode:
577 case SkXfermode::kMultiply_Mode:
578 case SkXfermode::kHue_Mode:
579 case SkXfermode::kSaturation_Mode:
580 case SkXfermode::kColor_Mode:
581 case SkXfermode::kLuminosity_Mode:
582 // supported blend modes
583 break;
584 case SkXfermode::kClear_Mode:
585 case SkXfermode::kSrc_Mode:
586 case SkXfermode::kDst_Mode:
587 case SkXfermode::kDstOver_Mode:
588 case SkXfermode::kSrcIn_Mode:
589 case SkXfermode::kDstIn_Mode:
590 case SkXfermode::kSrcOut_Mode:
591 case SkXfermode::kDstOut_Mode:
592 case SkXfermode::kSrcATop_Mode:
593 case SkXfermode::kDstATop_Mode:
594 case SkXfermode::kXor_Mode:
595 case SkXfermode::kPlus_Mode:
596 case SkXfermode::kModulate_Mode:
597 // Porter Duff Compositing Operators are not yet supported
598 // http://dev.w3.org/fxtf/compositing-1/#porterduffcompositingoperators
599 NOTREACHED();
600 return;
603 blend_mode_ = blend_mode;
604 SetNeedsCommit();
607 void Layer::SetIsRootForIsolatedGroup(bool root) {
608 DCHECK(IsPropertyChangeAllowed());
609 if (is_root_for_isolated_group_ == root)
610 return;
611 is_root_for_isolated_group_ = root;
612 SetNeedsCommit();
615 void Layer::SetContentsOpaque(bool opaque) {
616 DCHECK(IsPropertyChangeAllowed());
617 if (contents_opaque_ == opaque)
618 return;
619 contents_opaque_ = opaque;
620 SetNeedsCommit();
623 void Layer::SetPosition(const gfx::PointF& position) {
624 DCHECK(IsPropertyChangeAllowed());
625 if (position_ == position)
626 return;
627 position_ = position;
629 if (!layer_tree_host_)
630 return;
632 if (TransformNode* transform_node =
633 layer_tree_host_->property_trees()->transform_tree.Node(
634 transform_tree_index())) {
635 if (transform_node->owner_id == id()) {
636 transform_node->data.update_post_local_transform(position,
637 transform_origin());
638 transform_node->data.needs_local_transform_update = true;
639 layer_tree_host_->property_trees()->transform_tree.set_needs_update(true);
640 SetNeedsCommitNoRebuild();
641 return;
645 SetNeedsCommit();
648 bool Layer::IsContainerForFixedPositionLayers() const {
649 if (!transform_.IsIdentityOrTranslation())
650 return true;
651 if (parent_ && !parent_->transform_.IsIdentityOrTranslation())
652 return true;
653 return is_container_for_fixed_position_layers_;
656 bool Are2dAxisAligned(const gfx::Transform& a,
657 const gfx::Transform& b,
658 bool* is_invertible) {
659 if (a.IsScaleOrTranslation() && b.IsScaleOrTranslation()) {
660 *is_invertible = b.IsInvertible();
661 return true;
664 gfx::Transform inverse(gfx::Transform::kSkipInitialization);
665 *is_invertible = b.GetInverse(&inverse);
667 inverse *= a;
668 return inverse.Preserves2dAxisAlignment();
671 void Layer::SetTransform(const gfx::Transform& transform) {
672 DCHECK(IsPropertyChangeAllowed());
673 if (transform_ == transform)
674 return;
676 if (layer_tree_host_) {
677 if (TransformNode* transform_node =
678 layer_tree_host_->property_trees()->transform_tree.Node(
679 transform_tree_index())) {
680 if (transform_node->owner_id == id()) {
681 // We need to trigger a rebuild if we could have affected 2d axis
682 // alignment. We'll check to see if transform and transform_ are axis
683 // align with respect to one another.
684 bool invertible = false;
685 bool preserves_2d_axis_alignment =
686 Are2dAxisAligned(transform_, transform, &invertible);
687 transform_node->data.local = transform;
688 transform_node->data.needs_local_transform_update = true;
689 layer_tree_host_->property_trees()->transform_tree.set_needs_update(
690 true);
691 if (preserves_2d_axis_alignment)
692 SetNeedsCommitNoRebuild();
693 else
694 SetNeedsCommit();
695 transform_ = transform;
696 transform_is_invertible_ = invertible;
697 return;
702 transform_ = transform;
703 transform_is_invertible_ = transform.IsInvertible();
705 SetNeedsCommit();
708 void Layer::SetTransformOrigin(const gfx::Point3F& transform_origin) {
709 DCHECK(IsPropertyChangeAllowed());
710 if (transform_origin_ == transform_origin)
711 return;
712 transform_origin_ = transform_origin;
714 if (!layer_tree_host_)
715 return;
717 if (TransformNode* transform_node =
718 layer_tree_host_->property_trees()->transform_tree.Node(
719 transform_tree_index())) {
720 if (transform_node->owner_id == id()) {
721 transform_node->data.update_pre_local_transform(transform_origin);
722 transform_node->data.update_post_local_transform(position(),
723 transform_origin);
724 transform_node->data.needs_local_transform_update = true;
725 layer_tree_host_->property_trees()->transform_tree.set_needs_update(true);
726 SetNeedsCommitNoRebuild();
727 return;
731 SetNeedsCommit();
734 bool Layer::AnimationsPreserveAxisAlignment() const {
735 return layer_animation_controller_->AnimationsPreserveAxisAlignment();
738 bool Layer::TransformIsAnimating() const {
739 return layer_animation_controller_->IsAnimatingProperty(Animation::TRANSFORM);
742 void Layer::SetScrollParent(Layer* parent) {
743 DCHECK(IsPropertyChangeAllowed());
744 if (scroll_parent_ == parent)
745 return;
747 if (scroll_parent_)
748 scroll_parent_->RemoveScrollChild(this);
750 scroll_parent_ = parent;
752 if (scroll_parent_)
753 scroll_parent_->AddScrollChild(this);
755 SetNeedsCommit();
758 void Layer::AddScrollChild(Layer* child) {
759 if (!scroll_children_)
760 scroll_children_.reset(new std::set<Layer*>);
761 scroll_children_->insert(child);
762 SetNeedsCommit();
765 void Layer::RemoveScrollChild(Layer* child) {
766 scroll_children_->erase(child);
767 if (scroll_children_->empty())
768 scroll_children_ = nullptr;
769 SetNeedsCommit();
772 void Layer::SetClipParent(Layer* ancestor) {
773 DCHECK(IsPropertyChangeAllowed());
774 if (clip_parent_ == ancestor)
775 return;
777 if (clip_parent_)
778 clip_parent_->RemoveClipChild(this);
780 clip_parent_ = ancestor;
782 if (clip_parent_)
783 clip_parent_->AddClipChild(this);
785 SetNeedsCommit();
786 if (layer_tree_host_)
787 layer_tree_host_->SetNeedsMetaInfoRecomputation(true);
790 void Layer::AddClipChild(Layer* child) {
791 if (!clip_children_)
792 clip_children_.reset(new std::set<Layer*>);
793 clip_children_->insert(child);
794 SetNeedsCommit();
797 void Layer::RemoveClipChild(Layer* child) {
798 clip_children_->erase(child);
799 if (clip_children_->empty())
800 clip_children_ = nullptr;
801 SetNeedsCommit();
804 void Layer::SetScrollOffset(const gfx::ScrollOffset& scroll_offset) {
805 DCHECK(IsPropertyChangeAllowed());
807 if (scroll_offset_ == scroll_offset)
808 return;
809 scroll_offset_ = scroll_offset;
811 if (!layer_tree_host_)
812 return;
814 if (TransformNode* transform_node =
815 layer_tree_host_->property_trees()->transform_tree.Node(
816 transform_tree_index())) {
817 if (transform_node->owner_id == id()) {
818 transform_node->data.scroll_offset = CurrentScrollOffset();
819 transform_node->data.needs_local_transform_update = true;
820 layer_tree_host_->property_trees()->transform_tree.set_needs_update(true);
821 SetNeedsCommitNoRebuild();
822 return;
826 SetNeedsCommit();
829 void Layer::SetScrollCompensationAdjustment(
830 const gfx::Vector2dF& scroll_compensation_adjustment) {
831 if (scroll_compensation_adjustment_ == scroll_compensation_adjustment)
832 return;
833 scroll_compensation_adjustment_ = scroll_compensation_adjustment;
834 SetNeedsCommit();
837 gfx::Vector2dF Layer::ScrollCompensationAdjustment() const {
838 return scroll_compensation_adjustment_;
841 void Layer::SetScrollOffsetFromImplSide(
842 const gfx::ScrollOffset& scroll_offset) {
843 DCHECK(IsPropertyChangeAllowed());
844 // This function only gets called during a BeginMainFrame, so there
845 // is no need to call SetNeedsUpdate here.
846 DCHECK(layer_tree_host_ && layer_tree_host_->CommitRequested());
847 if (scroll_offset_ == scroll_offset)
848 return;
849 scroll_offset_ = scroll_offset;
850 SetNeedsPushProperties();
852 bool needs_rebuild = true;
853 if (TransformNode* transform_node =
854 layer_tree_host_->property_trees()->transform_tree.Node(
855 transform_tree_index())) {
856 if (transform_node->owner_id == id()) {
857 transform_node->data.scroll_offset = CurrentScrollOffset();
858 transform_node->data.needs_local_transform_update = true;
859 layer_tree_host_->property_trees()->transform_tree.set_needs_update(true);
860 needs_rebuild = false;
864 if (needs_rebuild)
865 layer_tree_host_->property_trees()->needs_rebuild = true;
867 if (!did_scroll_callback_.is_null())
868 did_scroll_callback_.Run();
869 // The callback could potentially change the layer structure:
870 // "this" may have been destroyed during the process.
873 void Layer::SetScrollClipLayerId(int clip_layer_id) {
874 DCHECK(IsPropertyChangeAllowed());
875 if (scroll_clip_layer_id_ == clip_layer_id)
876 return;
877 scroll_clip_layer_id_ = clip_layer_id;
878 SetNeedsCommit();
881 void Layer::SetUserScrollable(bool horizontal, bool vertical) {
882 DCHECK(IsPropertyChangeAllowed());
883 if (user_scrollable_horizontal_ == horizontal &&
884 user_scrollable_vertical_ == vertical)
885 return;
886 user_scrollable_horizontal_ = horizontal;
887 user_scrollable_vertical_ = vertical;
888 SetNeedsCommit();
891 void Layer::SetShouldScrollOnMainThread(bool should_scroll_on_main_thread) {
892 DCHECK(IsPropertyChangeAllowed());
893 if (should_scroll_on_main_thread_ == should_scroll_on_main_thread)
894 return;
895 should_scroll_on_main_thread_ = should_scroll_on_main_thread;
896 SetNeedsCommit();
899 void Layer::SetHaveWheelEventHandlers(bool have_wheel_event_handlers) {
900 DCHECK(IsPropertyChangeAllowed());
901 if (have_wheel_event_handlers_ == have_wheel_event_handlers)
902 return;
903 if (touch_event_handler_region_.IsEmpty() && layer_tree_host_ &&
904 !layer_tree_host_->needs_meta_info_recomputation())
905 UpdateNumInputHandlersForSubtree(have_wheel_event_handlers);
907 have_wheel_event_handlers_ = have_wheel_event_handlers;
908 SetNeedsCommit();
911 void Layer::UpdateNumInputHandlersForSubtree(bool add) {
912 int change = add ? 1 : -1;
913 for (Layer* layer = this; layer; layer = layer->parent()) {
914 layer->num_layer_or_descendants_with_input_handler_ += change;
915 layer->draw_properties().layer_or_descendant_has_input_handler =
916 (layer->num_layer_or_descendants_with_input_handler_ != 0);
917 DCHECK_GE(layer->num_layer_or_descendants_with_input_handler_, 0);
921 void Layer::SetHaveScrollEventHandlers(bool have_scroll_event_handlers) {
922 DCHECK(IsPropertyChangeAllowed());
923 if (have_scroll_event_handlers_ == have_scroll_event_handlers)
924 return;
925 have_scroll_event_handlers_ = have_scroll_event_handlers;
926 SetNeedsCommit();
929 void Layer::SetNonFastScrollableRegion(const Region& region) {
930 DCHECK(IsPropertyChangeAllowed());
931 if (non_fast_scrollable_region_ == region)
932 return;
933 non_fast_scrollable_region_ = region;
934 SetNeedsCommit();
937 void Layer::SetTouchEventHandlerRegion(const Region& region) {
938 DCHECK(IsPropertyChangeAllowed());
939 if (touch_event_handler_region_ == region)
940 return;
941 if (!have_wheel_event_handlers_ && layer_tree_host_ &&
942 !layer_tree_host_->needs_meta_info_recomputation())
943 UpdateNumInputHandlersForSubtree(!region.IsEmpty());
945 touch_event_handler_region_ = region;
946 SetNeedsCommit();
949 void Layer::SetScrollBlocksOn(ScrollBlocksOn scroll_blocks_on) {
950 DCHECK(IsPropertyChangeAllowed());
951 if (scroll_blocks_on_ == scroll_blocks_on)
952 return;
953 scroll_blocks_on_ = scroll_blocks_on;
954 SetNeedsCommit();
957 void Layer::SetDrawCheckerboardForMissingTiles(bool checkerboard) {
958 DCHECK(IsPropertyChangeAllowed());
959 if (draw_checkerboard_for_missing_tiles_ == checkerboard)
960 return;
961 draw_checkerboard_for_missing_tiles_ = checkerboard;
962 SetNeedsCommit();
965 void Layer::SetForceRenderSurface(bool force) {
966 DCHECK(IsPropertyChangeAllowed());
967 if (force_render_surface_ == force)
968 return;
969 force_render_surface_ = force;
970 SetNeedsCommit();
973 void Layer::SetDoubleSided(bool double_sided) {
974 DCHECK(IsPropertyChangeAllowed());
975 if (double_sided_ == double_sided)
976 return;
977 double_sided_ = double_sided;
978 SetNeedsCommit();
981 void Layer::Set3dSortingContextId(int id) {
982 DCHECK(IsPropertyChangeAllowed());
983 if (id == sorting_context_id_)
984 return;
985 sorting_context_id_ = id;
986 SetNeedsCommit();
989 void Layer::SetTransformTreeIndex(int index) {
990 DCHECK(IsPropertyChangeAllowed());
991 if (transform_tree_index_ == index)
992 return;
993 transform_tree_index_ = index;
994 SetNeedsPushProperties();
997 int Layer::transform_tree_index() const {
998 if (!layer_tree_host_ ||
999 layer_tree_host_->property_trees()->sequence_number !=
1000 property_tree_sequence_number_) {
1001 return -1;
1003 return transform_tree_index_;
1006 void Layer::SetClipTreeIndex(int index) {
1007 DCHECK(IsPropertyChangeAllowed());
1008 if (clip_tree_index_ == index)
1009 return;
1010 clip_tree_index_ = index;
1011 SetNeedsPushProperties();
1014 int Layer::clip_tree_index() const {
1015 if (!layer_tree_host_ ||
1016 layer_tree_host_->property_trees()->sequence_number !=
1017 property_tree_sequence_number_) {
1018 return -1;
1020 return clip_tree_index_;
1023 void Layer::SetOpacityTreeIndex(int index) {
1024 DCHECK(IsPropertyChangeAllowed());
1025 if (opacity_tree_index_ == index)
1026 return;
1027 opacity_tree_index_ = index;
1028 SetNeedsPushProperties();
1031 int Layer::opacity_tree_index() const {
1032 if (!layer_tree_host_ ||
1033 layer_tree_host_->property_trees()->sequence_number !=
1034 property_tree_sequence_number_) {
1035 return -1;
1037 return opacity_tree_index_;
1040 void Layer::SetShouldFlattenTransform(bool should_flatten) {
1041 DCHECK(IsPropertyChangeAllowed());
1042 if (should_flatten_transform_ == should_flatten)
1043 return;
1044 should_flatten_transform_ = should_flatten;
1045 SetNeedsCommit();
1048 void Layer::SetIsDrawable(bool is_drawable) {
1049 DCHECK(IsPropertyChangeAllowed());
1050 if (is_drawable_ == is_drawable)
1051 return;
1053 is_drawable_ = is_drawable;
1054 UpdateDrawsContent(HasDrawableContent());
1057 void Layer::SetHideLayerAndSubtree(bool hide) {
1058 DCHECK(IsPropertyChangeAllowed());
1059 if (hide_layer_and_subtree_ == hide)
1060 return;
1062 hide_layer_and_subtree_ = hide;
1063 SetNeedsCommit();
1066 void Layer::SetNeedsDisplayRect(const gfx::Rect& dirty_rect) {
1067 if (dirty_rect.IsEmpty())
1068 return;
1070 SetNeedsPushProperties();
1071 update_rect_.Union(dirty_rect);
1073 if (DrawsContent())
1074 SetNeedsUpdate();
1077 bool Layer::DescendantIsFixedToContainerLayer() const {
1078 for (size_t i = 0; i < children_.size(); ++i) {
1079 if (children_[i]->position_constraint_.is_fixed_position() ||
1080 children_[i]->DescendantIsFixedToContainerLayer())
1081 return true;
1083 return false;
1086 void Layer::SetIsContainerForFixedPositionLayers(bool container) {
1087 if (is_container_for_fixed_position_layers_ == container)
1088 return;
1089 is_container_for_fixed_position_layers_ = container;
1091 if (layer_tree_host_ && layer_tree_host_->CommitRequested())
1092 return;
1094 // Only request a commit if we have a fixed positioned descendant.
1095 if (DescendantIsFixedToContainerLayer())
1096 SetNeedsCommit();
1099 void Layer::SetPositionConstraint(const LayerPositionConstraint& constraint) {
1100 DCHECK(IsPropertyChangeAllowed());
1101 if (position_constraint_ == constraint)
1102 return;
1103 position_constraint_ = constraint;
1104 SetNeedsCommit();
1107 static void RunCopyCallbackOnMainThread(scoped_ptr<CopyOutputRequest> request,
1108 scoped_ptr<CopyOutputResult> result) {
1109 request->SendResult(result.Pass());
1112 static void PostCopyCallbackToMainThread(
1113 scoped_refptr<base::SingleThreadTaskRunner> main_thread_task_runner,
1114 scoped_ptr<CopyOutputRequest> request,
1115 scoped_ptr<CopyOutputResult> result) {
1116 main_thread_task_runner->PostTask(FROM_HERE,
1117 base::Bind(&RunCopyCallbackOnMainThread,
1118 base::Passed(&request),
1119 base::Passed(&result)));
1122 void Layer::PushPropertiesTo(LayerImpl* layer) {
1123 DCHECK(layer_tree_host_);
1125 // If we did not SavePaintProperties() for the layer this frame, then push the
1126 // real property values, not the paint property values.
1127 bool use_paint_properties = paint_properties_.source_frame_number ==
1128 layer_tree_host_->source_frame_number();
1130 layer->SetTransformOrigin(transform_origin_);
1131 layer->SetBackgroundColor(background_color_);
1132 layer->SetBounds(use_paint_properties ? paint_properties_.bounds
1133 : bounds_);
1135 // TODO(enne): This is needed because CDP does this. Once main thread CDP
1136 // goes away, content scale / bounds can be removed.
1137 if (layer_tree_host()->settings().impl_side_painting) {
1138 layer->SetContentsScale(1.f, 1.f);
1139 layer->SetContentBounds(bounds());
1140 } else {
1141 layer->SetContentBounds(content_bounds());
1142 layer->SetContentsScale(contents_scale_x(), contents_scale_y());
1145 if (frame_viewer_instrumentation::IsTracingLayerTreeSnapshots())
1146 layer->SetDebugInfo(TakeDebugInfo());
1148 layer->SetDoubleSided(double_sided_);
1149 layer->SetDrawCheckerboardForMissingTiles(
1150 draw_checkerboard_for_missing_tiles_);
1151 layer->SetDrawsContent(DrawsContent());
1152 layer->SetHideLayerAndSubtree(hide_layer_and_subtree_);
1153 layer->SetHasRenderSurface(has_render_surface_ || layer->HasCopyRequest());
1154 if (!layer->FilterIsAnimatingOnImplOnly() && !FilterIsAnimating())
1155 layer->SetFilters(filters_);
1156 DCHECK(!(FilterIsAnimating() && layer->FilterIsAnimatingOnImplOnly()));
1157 layer->SetBackgroundFilters(background_filters());
1158 layer->SetMasksToBounds(masks_to_bounds_);
1159 layer->SetShouldScrollOnMainThread(should_scroll_on_main_thread_);
1160 layer->SetHaveWheelEventHandlers(have_wheel_event_handlers_);
1161 layer->SetHaveScrollEventHandlers(have_scroll_event_handlers_);
1162 layer->SetNonFastScrollableRegion(non_fast_scrollable_region_);
1163 layer->SetTouchEventHandlerRegion(touch_event_handler_region_);
1164 layer->SetScrollBlocksOn(scroll_blocks_on_);
1165 layer->SetContentsOpaque(contents_opaque_);
1166 if (!layer->OpacityIsAnimatingOnImplOnly() && !OpacityIsAnimating())
1167 layer->SetOpacity(opacity_);
1168 DCHECK(!(OpacityIsAnimating() && layer->OpacityIsAnimatingOnImplOnly()));
1169 layer->SetBlendMode(blend_mode_);
1170 layer->SetIsRootForIsolatedGroup(is_root_for_isolated_group_);
1171 layer->SetPosition(position_);
1172 layer->SetIsContainerForFixedPositionLayers(
1173 IsContainerForFixedPositionLayers());
1174 layer->SetPositionConstraint(position_constraint_);
1175 layer->SetShouldFlattenTransform(should_flatten_transform_);
1176 layer->set_should_flatten_transform_from_property_tree(
1177 should_flatten_transform_from_property_tree_);
1178 layer->SetUseParentBackfaceVisibility(use_parent_backface_visibility_);
1179 if (!layer->TransformIsAnimatingOnImplOnly() && !TransformIsAnimating())
1180 layer->SetTransformAndInvertibility(transform_, transform_is_invertible_);
1181 DCHECK(!(TransformIsAnimating() && layer->TransformIsAnimatingOnImplOnly()));
1182 layer->Set3dSortingContextId(sorting_context_id_);
1183 layer->SetNumDescendantsThatDrawContent(num_descendants_that_draw_content_);
1184 layer->SetTransformTreeIndex(transform_tree_index());
1185 layer->SetOpacityTreeIndex(opacity_tree_index());
1186 layer->SetClipTreeIndex(clip_tree_index());
1187 layer->set_offset_to_transform_parent(offset_to_transform_parent_);
1189 layer->SetScrollClipLayer(scroll_clip_layer_id_);
1190 layer->set_user_scrollable_horizontal(user_scrollable_horizontal_);
1191 layer->set_user_scrollable_vertical(user_scrollable_vertical_);
1193 LayerImpl* scroll_parent = nullptr;
1194 if (scroll_parent_) {
1195 scroll_parent = layer->layer_tree_impl()->LayerById(scroll_parent_->id());
1196 DCHECK(scroll_parent);
1199 layer->SetScrollParent(scroll_parent);
1200 if (scroll_children_) {
1201 std::set<LayerImpl*>* scroll_children = new std::set<LayerImpl*>;
1202 for (std::set<Layer*>::iterator it = scroll_children_->begin();
1203 it != scroll_children_->end();
1204 ++it) {
1205 DCHECK_EQ((*it)->scroll_parent(), this);
1206 LayerImpl* scroll_child =
1207 layer->layer_tree_impl()->LayerById((*it)->id());
1208 DCHECK(scroll_child);
1209 scroll_children->insert(scroll_child);
1211 layer->SetScrollChildren(scroll_children);
1212 } else {
1213 layer->SetScrollChildren(nullptr);
1216 LayerImpl* clip_parent = nullptr;
1217 if (clip_parent_) {
1218 clip_parent =
1219 layer->layer_tree_impl()->LayerById(clip_parent_->id());
1220 DCHECK(clip_parent);
1223 layer->SetClipParent(clip_parent);
1224 if (clip_children_) {
1225 std::set<LayerImpl*>* clip_children = new std::set<LayerImpl*>;
1226 for (std::set<Layer*>::iterator it = clip_children_->begin();
1227 it != clip_children_->end(); ++it) {
1228 DCHECK_EQ((*it)->clip_parent(), this);
1229 LayerImpl* clip_child = layer->layer_tree_impl()->LayerById((*it)->id());
1230 DCHECK(clip_child);
1231 clip_children->insert(clip_child);
1233 layer->SetClipChildren(clip_children);
1234 } else {
1235 layer->SetClipChildren(nullptr);
1238 // When a scroll offset animation is interrupted the new scroll position on
1239 // the pending tree will clobber any impl-side scrolling occuring on the
1240 // active tree. To do so, avoid scrolling the pending tree along with it
1241 // instead of trying to undo that scrolling later.
1242 if (layer_animation_controller_->scroll_offset_animation_was_interrupted())
1243 layer->PushScrollOffsetFromMainThreadAndClobberActiveValue(scroll_offset_);
1244 else
1245 layer->PushScrollOffsetFromMainThread(scroll_offset_);
1246 layer->SetScrollCompensationAdjustment(ScrollCompensationAdjustment());
1248 // Wrap the copy_requests_ in a PostTask to the main thread.
1249 int size = copy_requests_.size();
1250 ScopedPtrVector<CopyOutputRequest> main_thread_copy_requests;
1251 for (ScopedPtrVector<CopyOutputRequest>::iterator it = copy_requests_.begin();
1252 it != copy_requests_.end();
1253 ++it) {
1254 scoped_refptr<base::SingleThreadTaskRunner> main_thread_task_runner =
1255 layer_tree_host()->proxy()->MainThreadTaskRunner();
1256 scoped_ptr<CopyOutputRequest> original_request = copy_requests_.take(it);
1257 const CopyOutputRequest& original_request_ref = *original_request;
1258 scoped_ptr<CopyOutputRequest> main_thread_request =
1259 CopyOutputRequest::CreateRelayRequest(
1260 original_request_ref,
1261 base::Bind(&PostCopyCallbackToMainThread,
1262 main_thread_task_runner,
1263 base::Passed(&original_request)));
1264 main_thread_copy_requests.push_back(main_thread_request.Pass());
1266 if (!copy_requests_.empty() && layer_tree_host_)
1267 layer_tree_host_->property_trees()->needs_rebuild = true;
1268 if (size != 0)
1269 UpdateNumCopyRequestsForSubtree(false);
1270 copy_requests_.clear();
1271 layer->PassCopyRequests(&main_thread_copy_requests);
1273 // If the main thread commits multiple times before the impl thread actually
1274 // draws, then damage tracking will become incorrect if we simply clobber the
1275 // update_rect here. The LayerImpl's update_rect needs to accumulate (i.e.
1276 // union) any update changes that have occurred on the main thread.
1277 update_rect_.Union(layer->update_rect());
1278 layer->SetUpdateRect(update_rect_);
1280 layer->SetStackingOrderChanged(stacking_order_changed_);
1282 layer_animation_controller_->PushAnimationUpdatesTo(
1283 layer->layer_animation_controller());
1285 if (frame_timing_requests_dirty_) {
1286 layer->PassFrameTimingRequests(&frame_timing_requests_);
1287 frame_timing_requests_dirty_ = false;
1290 // Reset any state that should be cleared for the next update.
1291 stacking_order_changed_ = false;
1292 update_rect_ = gfx::Rect();
1294 needs_push_properties_ = false;
1295 num_dependents_need_push_properties_ = 0;
1298 scoped_ptr<LayerImpl> Layer::CreateLayerImpl(LayerTreeImpl* tree_impl) {
1299 return LayerImpl::Create(tree_impl, layer_id_,
1300 new LayerImpl::SyncedScrollOffset);
1303 bool Layer::DrawsContent() const {
1304 return draws_content_;
1307 bool Layer::HasDrawableContent() const {
1308 return is_drawable_;
1311 void Layer::UpdateDrawsContent(bool has_drawable_content) {
1312 bool draws_content = has_drawable_content;
1313 DCHECK(is_drawable_ || !has_drawable_content);
1314 if (draws_content == draws_content_)
1315 return;
1317 if (HasDelegatedContent()) {
1318 // Layers with delegated content need to be treated as if they have as
1319 // many children as the number of layers they own delegated quads for.
1320 // Since we don't know this number right now, we choose one that acts like
1321 // infinity for our purposes.
1322 AddDrawableDescendants(draws_content ? 1000 : -1000);
1325 if (parent())
1326 parent()->AddDrawableDescendants(draws_content ? 1 : -1);
1328 draws_content_ = draws_content;
1329 SetNeedsCommit();
1332 int Layer::NumDescendantsThatDrawContent() const {
1333 return num_descendants_that_draw_content_;
1336 void Layer::SavePaintProperties() {
1337 DCHECK(layer_tree_host_);
1339 // TODO(reveman): Save all layer properties that we depend on not
1340 // changing until PushProperties() has been called. crbug.com/231016
1341 paint_properties_.bounds = bounds_;
1342 paint_properties_.source_frame_number =
1343 layer_tree_host_->source_frame_number();
1346 bool Layer::Update(ResourceUpdateQueue* queue,
1347 const OcclusionTracker<Layer>* occlusion) {
1348 DCHECK(layer_tree_host_);
1349 DCHECK_EQ(layer_tree_host_->source_frame_number(),
1350 paint_properties_.source_frame_number) <<
1351 "SavePaintProperties must be called for any layer that is painted.";
1352 return false;
1355 bool Layer::NeedMoreUpdates() {
1356 return false;
1359 bool Layer::IsSuitableForGpuRasterization() const {
1360 return true;
1363 scoped_refptr<base::trace_event::ConvertableToTraceFormat>
1364 Layer::TakeDebugInfo() {
1365 if (client_)
1366 return client_->TakeDebugInfo();
1367 else
1368 return nullptr;
1371 void Layer::SetHasRenderSurface(bool has_render_surface) {
1372 if (has_render_surface_ == has_render_surface)
1373 return;
1374 has_render_surface_ = has_render_surface;
1375 // We do not need SetNeedsCommit here, since this is only ever called
1376 // during a commit, from CalculateDrawProperties.
1377 SetNeedsPushProperties();
1380 void Layer::CreateRenderSurface() {
1381 DCHECK(!render_surface_);
1382 render_surface_ = make_scoped_ptr(new RenderSurface(this));
1385 void Layer::ClearRenderSurface() {
1386 render_surface_ = nullptr;
1389 void Layer::ClearRenderSurfaceLayerList() {
1390 if (render_surface_)
1391 render_surface_->ClearLayerLists();
1394 gfx::ScrollOffset Layer::ScrollOffsetForAnimation() const {
1395 return CurrentScrollOffset();
1398 // On<Property>Animated is called due to an ongoing accelerated animation.
1399 // Since this animation is also being run on the compositor thread, there
1400 // is no need to request a commit to push this value over, so the value is
1401 // set directly rather than by calling Set<Property>.
1402 void Layer::OnFilterAnimated(const FilterOperations& filters) {
1403 filters_ = filters;
1406 void Layer::OnOpacityAnimated(float opacity) {
1407 opacity_ = opacity;
1408 if (layer_tree_host_) {
1409 if (OpacityNode* node =
1410 layer_tree_host_->property_trees()->opacity_tree.Node(
1411 opacity_tree_index())) {
1412 if (node->owner_id == id())
1413 node->data = opacity;
1418 void Layer::OnTransformAnimated(const gfx::Transform& transform) {
1419 if (transform_ == transform)
1420 return;
1421 transform_ = transform;
1422 transform_is_invertible_ = transform.IsInvertible();
1423 if (layer_tree_host_) {
1424 if (TransformNode* node =
1425 layer_tree_host_->property_trees()->transform_tree.Node(
1426 transform_tree_index())) {
1427 if (node->owner_id == id()) {
1428 node->data.local = transform;
1429 node->data.needs_local_transform_update = true;
1430 node->data.is_animated = true;
1431 layer_tree_host_->property_trees()->transform_tree.set_needs_update(
1432 true);
1438 void Layer::OnScrollOffsetAnimated(const gfx::ScrollOffset& scroll_offset) {
1439 // Do nothing. Scroll deltas will be sent from the compositor thread back
1440 // to the main thread in the same manner as during non-animated
1441 // compositor-driven scrolling.
1444 void Layer::OnAnimationWaitingForDeletion() {
1445 // Animations are only deleted during PushProperties.
1446 SetNeedsPushProperties();
1449 bool Layer::IsActive() const {
1450 return true;
1453 bool Layer::AddAnimation(scoped_ptr <Animation> animation) {
1454 if (!layer_animation_controller_->animation_registrar())
1455 return false;
1457 if (animation->target_property() == Animation::SCROLL_OFFSET &&
1458 !layer_animation_controller_->animation_registrar()
1459 ->supports_scroll_animations())
1460 return false;
1462 UMA_HISTOGRAM_BOOLEAN("Renderer.AnimationAddedToOrphanLayer",
1463 !layer_tree_host_);
1464 layer_animation_controller_->AddAnimation(animation.Pass());
1465 SetNeedsCommit();
1466 return true;
1469 void Layer::PauseAnimation(int animation_id, double time_offset) {
1470 layer_animation_controller_->PauseAnimation(
1471 animation_id, base::TimeDelta::FromSecondsD(time_offset));
1472 SetNeedsCommit();
1475 void Layer::RemoveAnimation(int animation_id) {
1476 layer_animation_controller_->RemoveAnimation(animation_id);
1477 SetNeedsCommit();
1480 void Layer::RemoveAnimation(int animation_id,
1481 Animation::TargetProperty property) {
1482 layer_animation_controller_->RemoveAnimation(animation_id, property);
1483 SetNeedsCommit();
1486 void Layer::SetLayerAnimationControllerForTest(
1487 scoped_refptr<LayerAnimationController> controller) {
1488 layer_animation_controller_->RemoveValueObserver(this);
1489 layer_animation_controller_ = controller;
1490 layer_animation_controller_->AddValueObserver(this);
1491 SetNeedsCommit();
1494 bool Layer::HasActiveAnimation() const {
1495 return layer_animation_controller_->HasActiveAnimation();
1498 void Layer::AddLayerAnimationEventObserver(
1499 LayerAnimationEventObserver* animation_observer) {
1500 layer_animation_controller_->AddEventObserver(animation_observer);
1503 void Layer::RemoveLayerAnimationEventObserver(
1504 LayerAnimationEventObserver* animation_observer) {
1505 layer_animation_controller_->RemoveEventObserver(animation_observer);
1508 SimpleEnclosedRegion Layer::VisibleContentOpaqueRegion() const {
1509 if (contents_opaque())
1510 return SimpleEnclosedRegion(visible_content_rect());
1511 return SimpleEnclosedRegion();
1514 ScrollbarLayerInterface* Layer::ToScrollbarLayer() {
1515 return nullptr;
1518 RenderingStatsInstrumentation* Layer::rendering_stats_instrumentation() const {
1519 return layer_tree_host_->rendering_stats_instrumentation();
1522 void Layer::RemoveFromScrollTree() {
1523 if (scroll_children_.get()) {
1524 std::set<Layer*> copy = *scroll_children_;
1525 for (std::set<Layer*>::iterator it = copy.begin(); it != copy.end(); ++it)
1526 (*it)->SetScrollParent(nullptr);
1529 DCHECK(!scroll_children_);
1530 SetScrollParent(nullptr);
1533 void Layer::RemoveFromClipTree() {
1534 if (clip_children_.get()) {
1535 std::set<Layer*> copy = *clip_children_;
1536 for (std::set<Layer*>::iterator it = copy.begin(); it != copy.end(); ++it)
1537 (*it)->SetClipParent(nullptr);
1540 DCHECK(!clip_children_);
1541 SetClipParent(nullptr);
1544 void Layer::AddDrawableDescendants(int num) {
1545 DCHECK_GE(num_descendants_that_draw_content_, 0);
1546 DCHECK_GE(num_descendants_that_draw_content_ + num, 0);
1547 if (num == 0)
1548 return;
1549 num_descendants_that_draw_content_ += num;
1550 SetNeedsCommit();
1551 if (parent())
1552 parent()->AddDrawableDescendants(num);
1555 void Layer::RunMicroBenchmark(MicroBenchmark* benchmark) {
1556 benchmark->RunOnLayer(this);
1559 bool Layer::HasDelegatedContent() const {
1560 return false;
1563 void Layer::SetFrameTimingRequests(
1564 const std::vector<FrameTimingRequest>& requests) {
1565 frame_timing_requests_ = requests;
1566 frame_timing_requests_dirty_ = true;
1567 SetNeedsCommit();
1570 void Layer::DidBeginTracing() {
1571 // We'll be dumping layer trees as part of trace, so make sure
1572 // PushPropertiesTo() propagates layer debug info to the impl
1573 // side -- otherwise this won't happen for the the layers that
1574 // remain unchanged since tracing started.
1575 SetNeedsPushProperties();
1578 } // namespace cc