Fix flaky PDFExtensionTest
[chromium-blink-merge.git] / cc / layers / layer.cc
blobd795670192770cf273875a4bf29e67db9a660aa0
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_(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 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 layer_tree_host_ = host;
137 // When changing hosts, the layer needs to commit its properties to the impl
138 // side for the new host.
139 SetNeedsPushProperties();
141 for (size_t i = 0; i < children_.size(); ++i)
142 children_[i]->SetLayerTreeHost(host);
144 if (mask_layer_.get())
145 mask_layer_->SetLayerTreeHost(host);
146 if (replica_layer_.get())
147 replica_layer_->SetLayerTreeHost(host);
149 if (host) {
150 RegisterForAnimations(host->animation_registrar());
151 if (host->settings().layer_transforms_should_scale_layer_contents)
152 reset_raster_scale_to_unknown();
155 if (host && layer_animation_controller_->has_any_animation())
156 host->SetNeedsCommit();
159 void Layer::SetNeedsUpdate() {
160 if (layer_tree_host_ && !ignore_set_needs_commit_)
161 layer_tree_host_->SetNeedsUpdateLayers();
164 void Layer::SetNeedsCommit() {
165 if (!layer_tree_host_)
166 return;
168 SetNeedsPushProperties();
169 layer_tree_host_->property_trees()->needs_rebuild = true;
171 if (ignore_set_needs_commit_)
172 return;
174 layer_tree_host_->SetNeedsCommit();
177 void Layer::SetNeedsCommitNoRebuild() {
178 if (!layer_tree_host_)
179 return;
181 SetNeedsPushProperties();
183 if (ignore_set_needs_commit_)
184 return;
186 layer_tree_host_->SetNeedsCommit();
189 void Layer::SetNeedsFullTreeSync() {
190 if (!layer_tree_host_)
191 return;
193 layer_tree_host_->SetNeedsFullTreeSync();
196 void Layer::SetNextCommitWaitsForActivation() {
197 if (!layer_tree_host_)
198 return;
200 layer_tree_host_->SetNextCommitWaitsForActivation();
203 void Layer::SetNeedsPushProperties() {
204 if (needs_push_properties_)
205 return;
206 if (!parent_should_know_need_push_properties() && parent_)
207 parent_->AddDependentNeedsPushProperties();
208 needs_push_properties_ = true;
211 void Layer::AddDependentNeedsPushProperties() {
212 DCHECK_GE(num_dependents_need_push_properties_, 0);
214 if (!parent_should_know_need_push_properties() && parent_)
215 parent_->AddDependentNeedsPushProperties();
217 num_dependents_need_push_properties_++;
220 void Layer::RemoveDependentNeedsPushProperties() {
221 num_dependents_need_push_properties_--;
222 DCHECK_GE(num_dependents_need_push_properties_, 0);
224 if (!parent_should_know_need_push_properties() && parent_)
225 parent_->RemoveDependentNeedsPushProperties();
228 bool Layer::IsPropertyChangeAllowed() const {
229 if (!layer_tree_host_)
230 return true;
232 if (!layer_tree_host_->settings().strict_layer_property_change_checking)
233 return true;
235 return !layer_tree_host_->in_paint_layer_contents();
238 gfx::Rect Layer::LayerRectToContentRect(const gfx::Rect& layer_rect) const {
239 gfx::Rect content_rect = gfx::ScaleToEnclosingRect(
240 layer_rect, contents_scale_x(), contents_scale_y());
241 // Intersect with content rect to avoid the extra pixel because for some
242 // values x and y, ceil((x / y) * y) may be x + 1.
243 content_rect.Intersect(gfx::Rect(content_bounds()));
244 return content_rect;
247 skia::RefPtr<SkPicture> Layer::GetPicture() const {
248 return skia::RefPtr<SkPicture>();
251 void Layer::SetParent(Layer* layer) {
252 DCHECK(!layer || !layer->HasAncestor(this));
254 if (parent_should_know_need_push_properties()) {
255 if (parent_)
256 parent_->RemoveDependentNeedsPushProperties();
257 if (layer)
258 layer->AddDependentNeedsPushProperties();
261 parent_ = layer;
262 SetLayerTreeHost(parent_ ? parent_->layer_tree_host() : nullptr);
264 if (!layer_tree_host_)
265 return;
267 layer_tree_host_->property_trees()->needs_rebuild = true;
269 const LayerTreeSettings& settings = layer_tree_host_->settings();
270 if (!settings.layer_transforms_should_scale_layer_contents)
271 return;
273 reset_raster_scale_to_unknown();
274 if (mask_layer_.get())
275 mask_layer_->reset_raster_scale_to_unknown();
276 if (replica_layer_.get() && replica_layer_->mask_layer_.get())
277 replica_layer_->mask_layer_->reset_raster_scale_to_unknown();
280 void Layer::AddChild(scoped_refptr<Layer> child) {
281 InsertChild(child, children_.size());
284 void Layer::InsertChild(scoped_refptr<Layer> child, size_t index) {
285 DCHECK(IsPropertyChangeAllowed());
286 child->RemoveFromParent();
287 AddDrawableDescendants(child->NumDescendantsThatDrawContent() +
288 (child->DrawsContent() ? 1 : 0));
289 child->SetParent(this);
290 child->stacking_order_changed_ = true;
292 index = std::min(index, children_.size());
293 children_.insert(children_.begin() + index, child);
294 SetNeedsFullTreeSync();
297 void Layer::RemoveFromParent() {
298 DCHECK(IsPropertyChangeAllowed());
299 if (parent_)
300 parent_->RemoveChildOrDependent(this);
303 void Layer::RemoveChildOrDependent(Layer* child) {
304 if (mask_layer_.get() == child) {
305 mask_layer_->SetParent(nullptr);
306 mask_layer_ = nullptr;
307 SetNeedsFullTreeSync();
308 return;
310 if (replica_layer_.get() == child) {
311 replica_layer_->SetParent(nullptr);
312 replica_layer_ = nullptr;
313 SetNeedsFullTreeSync();
314 return;
317 for (LayerList::iterator iter = children_.begin();
318 iter != children_.end();
319 ++iter) {
320 if (iter->get() != child)
321 continue;
323 child->SetParent(nullptr);
324 AddDrawableDescendants(-child->NumDescendantsThatDrawContent() -
325 (child->DrawsContent() ? 1 : 0));
326 children_.erase(iter);
327 SetNeedsFullTreeSync();
328 return;
332 void Layer::ReplaceChild(Layer* reference, scoped_refptr<Layer> new_layer) {
333 DCHECK(reference);
334 DCHECK_EQ(reference->parent(), this);
335 DCHECK(IsPropertyChangeAllowed());
337 if (reference == new_layer.get())
338 return;
340 int reference_index = IndexOfChild(reference);
341 if (reference_index == -1) {
342 NOTREACHED();
343 return;
346 reference->RemoveFromParent();
348 if (new_layer.get()) {
349 new_layer->RemoveFromParent();
350 InsertChild(new_layer, reference_index);
354 int Layer::IndexOfChild(const Layer* reference) {
355 for (size_t i = 0; i < children_.size(); ++i) {
356 if (children_[i].get() == reference)
357 return i;
359 return -1;
362 void Layer::SetBounds(const gfx::Size& size) {
363 DCHECK(IsPropertyChangeAllowed());
364 if (bounds() == size)
365 return;
366 bounds_ = size;
368 if (!layer_tree_host_)
369 return;
371 if (ClipNode* clip_node = layer_tree_host_->property_trees()->clip_tree.Node(
372 clip_tree_index())) {
373 if (clip_node->owner_id == id()) {
374 clip_node->data.clip.set_size(size);
375 layer_tree_host_->property_trees()->clip_tree.set_needs_update(true);
379 SetNeedsCommitNoRebuild();
382 Layer* Layer::RootLayer() {
383 Layer* layer = this;
384 while (layer->parent())
385 layer = layer->parent();
386 return layer;
389 void Layer::RemoveAllChildren() {
390 DCHECK(IsPropertyChangeAllowed());
391 while (children_.size()) {
392 Layer* layer = children_[0].get();
393 DCHECK_EQ(this, layer->parent());
394 layer->RemoveFromParent();
398 void Layer::SetChildren(const LayerList& children) {
399 DCHECK(IsPropertyChangeAllowed());
400 if (children == children_)
401 return;
403 RemoveAllChildren();
404 for (size_t i = 0; i < children.size(); ++i)
405 AddChild(children[i]);
408 bool Layer::HasAncestor(const Layer* ancestor) const {
409 for (const Layer* layer = parent(); layer; layer = layer->parent()) {
410 if (layer == ancestor)
411 return true;
413 return false;
416 void Layer::RequestCopyOfOutput(
417 scoped_ptr<CopyOutputRequest> request) {
418 DCHECK(IsPropertyChangeAllowed());
419 int size = copy_requests_.size();
420 if (void* source = request->source()) {
421 auto it = std::find_if(
422 copy_requests_.begin(), copy_requests_.end(),
423 [source](const CopyOutputRequest* x) { return x->source() == source; });
424 if (it != copy_requests_.end())
425 copy_requests_.erase(it);
427 if (request->IsEmpty())
428 return;
429 copy_requests_.push_back(request.Pass());
430 if (size == 0) {
431 bool copy_request_added = true;
432 UpdateNumCopyRequestsForSubtree(copy_request_added);
434 SetNeedsCommit();
437 void Layer::UpdateNumCopyRequestsForSubtree(bool add) {
438 int change = add ? 1 : -1;
439 for (Layer* layer = this; layer; layer = layer->parent()) {
440 layer->num_layer_or_descendants_with_copy_request_ += change;
441 layer->draw_properties().layer_or_descendant_has_copy_request =
442 (layer->num_layer_or_descendants_with_copy_request_ != 0);
443 DCHECK_GE(layer->num_layer_or_descendants_with_copy_request_, 0);
447 void Layer::SetBackgroundColor(SkColor background_color) {
448 DCHECK(IsPropertyChangeAllowed());
449 if (background_color_ == background_color)
450 return;
451 background_color_ = background_color;
452 SetNeedsCommit();
455 SkColor Layer::SafeOpaqueBackgroundColor() const {
456 SkColor color = background_color();
457 if (SkColorGetA(color) == 255 && !contents_opaque()) {
458 color = SK_ColorTRANSPARENT;
459 } else if (SkColorGetA(color) != 255 && contents_opaque()) {
460 for (const Layer* layer = parent(); layer;
461 layer = layer->parent()) {
462 color = layer->background_color();
463 if (SkColorGetA(color) == 255)
464 break;
466 if (SkColorGetA(color) != 255)
467 color = layer_tree_host_->background_color();
468 if (SkColorGetA(color) != 255)
469 color = SkColorSetA(color, 255);
471 return color;
474 void Layer::CalculateContentsScale(float ideal_contents_scale,
475 float* contents_scale_x,
476 float* contents_scale_y,
477 gfx::Size* content_bounds) {
478 DCHECK(layer_tree_host_);
480 *contents_scale_x = 1;
481 *contents_scale_y = 1;
482 *content_bounds = bounds();
485 void Layer::SetMasksToBounds(bool masks_to_bounds) {
486 DCHECK(IsPropertyChangeAllowed());
487 if (masks_to_bounds_ == masks_to_bounds)
488 return;
489 masks_to_bounds_ = masks_to_bounds;
490 SetNeedsCommit();
493 void Layer::SetMaskLayer(Layer* mask_layer) {
494 DCHECK(IsPropertyChangeAllowed());
495 if (mask_layer_.get() == mask_layer)
496 return;
497 if (mask_layer_.get()) {
498 DCHECK_EQ(this, mask_layer_->parent());
499 mask_layer_->RemoveFromParent();
501 mask_layer_ = mask_layer;
502 if (mask_layer_.get()) {
503 DCHECK(!mask_layer_->parent());
504 mask_layer_->RemoveFromParent();
505 mask_layer_->SetParent(this);
506 mask_layer_->SetIsMask(true);
508 SetNeedsFullTreeSync();
511 void Layer::SetReplicaLayer(Layer* layer) {
512 DCHECK(IsPropertyChangeAllowed());
513 if (replica_layer_.get() == layer)
514 return;
515 if (replica_layer_.get()) {
516 DCHECK_EQ(this, replica_layer_->parent());
517 replica_layer_->RemoveFromParent();
519 replica_layer_ = layer;
520 if (replica_layer_.get()) {
521 DCHECK(!replica_layer_->parent());
522 replica_layer_->RemoveFromParent();
523 replica_layer_->SetParent(this);
525 SetNeedsFullTreeSync();
528 void Layer::SetFilters(const FilterOperations& filters) {
529 DCHECK(IsPropertyChangeAllowed());
530 if (filters_ == filters)
531 return;
532 filters_ = filters;
533 SetNeedsCommit();
536 bool Layer::FilterIsAnimating() const {
537 return layer_animation_controller_->IsAnimatingProperty(Animation::FILTER);
540 void Layer::SetBackgroundFilters(const FilterOperations& filters) {
541 DCHECK(IsPropertyChangeAllowed());
542 if (background_filters_ == filters)
543 return;
544 background_filters_ = filters;
545 SetNeedsCommit();
548 void Layer::SetOpacity(float opacity) {
549 DCHECK(IsPropertyChangeAllowed());
550 if (opacity_ == opacity)
551 return;
552 opacity_ = opacity;
553 SetNeedsCommit();
556 bool Layer::OpacityIsAnimating() const {
557 return layer_animation_controller_->IsAnimatingProperty(Animation::OPACITY);
560 bool Layer::OpacityCanAnimateOnImplThread() const {
561 return false;
564 void Layer::SetBlendMode(SkXfermode::Mode blend_mode) {
565 DCHECK(IsPropertyChangeAllowed());
566 if (blend_mode_ == blend_mode)
567 return;
569 // Allowing only blend modes that are defined in the CSS Compositing standard:
570 // http://dev.w3.org/fxtf/compositing-1/#blending
571 switch (blend_mode) {
572 case SkXfermode::kSrcOver_Mode:
573 case SkXfermode::kScreen_Mode:
574 case SkXfermode::kOverlay_Mode:
575 case SkXfermode::kDarken_Mode:
576 case SkXfermode::kLighten_Mode:
577 case SkXfermode::kColorDodge_Mode:
578 case SkXfermode::kColorBurn_Mode:
579 case SkXfermode::kHardLight_Mode:
580 case SkXfermode::kSoftLight_Mode:
581 case SkXfermode::kDifference_Mode:
582 case SkXfermode::kExclusion_Mode:
583 case SkXfermode::kMultiply_Mode:
584 case SkXfermode::kHue_Mode:
585 case SkXfermode::kSaturation_Mode:
586 case SkXfermode::kColor_Mode:
587 case SkXfermode::kLuminosity_Mode:
588 // supported blend modes
589 break;
590 case SkXfermode::kClear_Mode:
591 case SkXfermode::kSrc_Mode:
592 case SkXfermode::kDst_Mode:
593 case SkXfermode::kDstOver_Mode:
594 case SkXfermode::kSrcIn_Mode:
595 case SkXfermode::kDstIn_Mode:
596 case SkXfermode::kSrcOut_Mode:
597 case SkXfermode::kDstOut_Mode:
598 case SkXfermode::kSrcATop_Mode:
599 case SkXfermode::kDstATop_Mode:
600 case SkXfermode::kXor_Mode:
601 case SkXfermode::kPlus_Mode:
602 case SkXfermode::kModulate_Mode:
603 // Porter Duff Compositing Operators are not yet supported
604 // http://dev.w3.org/fxtf/compositing-1/#porterduffcompositingoperators
605 NOTREACHED();
606 return;
609 blend_mode_ = blend_mode;
610 SetNeedsCommit();
613 void Layer::SetIsRootForIsolatedGroup(bool root) {
614 DCHECK(IsPropertyChangeAllowed());
615 if (is_root_for_isolated_group_ == root)
616 return;
617 is_root_for_isolated_group_ = root;
618 SetNeedsCommit();
621 void Layer::SetContentsOpaque(bool opaque) {
622 DCHECK(IsPropertyChangeAllowed());
623 if (contents_opaque_ == opaque)
624 return;
625 contents_opaque_ = opaque;
626 SetNeedsCommit();
629 void Layer::SetPosition(const gfx::PointF& position) {
630 DCHECK(IsPropertyChangeAllowed());
631 if (position_ == position)
632 return;
633 position_ = position;
635 if (!layer_tree_host_)
636 return;
638 if (TransformNode* transform_node =
639 layer_tree_host_->property_trees()->transform_tree.Node(
640 transform_tree_index())) {
641 if (transform_node->owner_id == id()) {
642 transform_node->data.update_post_local_transform(position,
643 transform_origin());
644 transform_node->data.needs_local_transform_update = true;
645 layer_tree_host_->property_trees()->transform_tree.set_needs_update(true);
646 SetNeedsCommitNoRebuild();
647 return;
651 SetNeedsCommit();
654 bool Layer::IsContainerForFixedPositionLayers() const {
655 if (!transform_.IsIdentityOrTranslation())
656 return true;
657 if (parent_ && !parent_->transform_.IsIdentityOrTranslation())
658 return true;
659 return is_container_for_fixed_position_layers_;
662 bool Are2dAxisAligned(const gfx::Transform& a,
663 const gfx::Transform& b,
664 bool* is_invertible) {
665 if (a.IsScaleOrTranslation() && b.IsScaleOrTranslation()) {
666 *is_invertible = b.IsInvertible();
667 return true;
670 gfx::Transform inverse(gfx::Transform::kSkipInitialization);
671 *is_invertible = b.GetInverse(&inverse);
673 inverse *= a;
674 return inverse.Preserves2dAxisAlignment();
677 void Layer::SetTransform(const gfx::Transform& transform) {
678 DCHECK(IsPropertyChangeAllowed());
679 if (transform_ == transform)
680 return;
682 if (layer_tree_host_) {
683 if (TransformNode* transform_node =
684 layer_tree_host_->property_trees()->transform_tree.Node(
685 transform_tree_index())) {
686 if (transform_node->owner_id == id()) {
687 // We need to trigger a rebuild if we could have affected 2d axis
688 // alignment. We'll check to see if transform and transform_ are axis
689 // align with respect to one another.
690 bool invertible = false;
691 bool preserves_2d_axis_alignment =
692 Are2dAxisAligned(transform_, transform, &invertible);
693 transform_node->data.local = transform;
694 transform_node->data.needs_local_transform_update = true;
695 layer_tree_host_->property_trees()->transform_tree.set_needs_update(
696 true);
697 if (preserves_2d_axis_alignment)
698 SetNeedsCommitNoRebuild();
699 else
700 SetNeedsCommit();
701 transform_ = transform;
702 transform_is_invertible_ = invertible;
703 return;
708 transform_ = transform;
709 transform_is_invertible_ = transform.IsInvertible();
711 SetNeedsCommit();
714 void Layer::SetTransformOrigin(const gfx::Point3F& transform_origin) {
715 DCHECK(IsPropertyChangeAllowed());
716 if (transform_origin_ == transform_origin)
717 return;
718 transform_origin_ = transform_origin;
720 if (!layer_tree_host_)
721 return;
723 if (TransformNode* transform_node =
724 layer_tree_host_->property_trees()->transform_tree.Node(
725 transform_tree_index())) {
726 if (transform_node->owner_id == id()) {
727 transform_node->data.update_pre_local_transform(transform_origin);
728 transform_node->data.update_post_local_transform(position(),
729 transform_origin);
730 transform_node->data.needs_local_transform_update = true;
731 layer_tree_host_->property_trees()->transform_tree.set_needs_update(true);
732 SetNeedsCommitNoRebuild();
733 return;
737 SetNeedsCommit();
740 bool Layer::AnimationsPreserveAxisAlignment() const {
741 return layer_animation_controller_->AnimationsPreserveAxisAlignment();
744 bool Layer::TransformIsAnimating() const {
745 return layer_animation_controller_->IsAnimatingProperty(Animation::TRANSFORM);
748 void Layer::SetScrollParent(Layer* parent) {
749 DCHECK(IsPropertyChangeAllowed());
750 if (scroll_parent_ == parent)
751 return;
753 if (scroll_parent_)
754 scroll_parent_->RemoveScrollChild(this);
756 scroll_parent_ = parent;
758 if (scroll_parent_)
759 scroll_parent_->AddScrollChild(this);
761 SetNeedsCommit();
764 void Layer::AddScrollChild(Layer* child) {
765 if (!scroll_children_)
766 scroll_children_.reset(new std::set<Layer*>);
767 scroll_children_->insert(child);
768 if (layer_tree_host_ && !layer_tree_host_->needs_meta_info_recomputation()) {
769 num_children_with_scroll_parent_++;
770 draw_properties().has_child_with_a_scroll_parent = true;
772 SetNeedsCommit();
775 void Layer::RemoveScrollChild(Layer* child) {
776 scroll_children_->erase(child);
777 if (scroll_children_->empty())
778 scroll_children_ = nullptr;
779 if (layer_tree_host_ && !layer_tree_host_->needs_meta_info_recomputation()) {
780 num_children_with_scroll_parent_--;
781 DCHECK_GE(num_children_with_scroll_parent_, 0);
782 draw_properties().has_child_with_a_scroll_parent =
783 (num_children_with_scroll_parent_ != 0);
785 SetNeedsCommit();
788 void Layer::SetClipParent(Layer* ancestor) {
789 DCHECK(IsPropertyChangeAllowed());
790 if (clip_parent_ == ancestor)
791 return;
793 if (clip_parent_)
794 clip_parent_->RemoveClipChild(this);
796 clip_parent_ = ancestor;
798 if (clip_parent_)
799 clip_parent_->AddClipChild(this);
801 SetNeedsCommit();
802 if (layer_tree_host_)
803 layer_tree_host_->SetNeedsMetaInfoRecomputation(true);
806 void Layer::AddClipChild(Layer* child) {
807 if (!clip_children_)
808 clip_children_.reset(new std::set<Layer*>);
809 clip_children_->insert(child);
810 SetNeedsCommit();
813 void Layer::RemoveClipChild(Layer* child) {
814 clip_children_->erase(child);
815 if (clip_children_->empty())
816 clip_children_ = nullptr;
817 SetNeedsCommit();
820 void Layer::SetScrollOffset(const gfx::ScrollOffset& scroll_offset) {
821 DCHECK(IsPropertyChangeAllowed());
823 if (scroll_offset_ == scroll_offset)
824 return;
825 scroll_offset_ = scroll_offset;
827 if (!layer_tree_host_)
828 return;
830 if (TransformNode* transform_node =
831 layer_tree_host_->property_trees()->transform_tree.Node(
832 transform_tree_index())) {
833 if (transform_node->owner_id == id()) {
834 transform_node->data.scroll_offset = CurrentScrollOffset();
835 transform_node->data.needs_local_transform_update = true;
836 layer_tree_host_->property_trees()->transform_tree.set_needs_update(true);
837 SetNeedsCommitNoRebuild();
838 return;
842 SetNeedsCommit();
845 void Layer::SetScrollCompensationAdjustment(
846 const gfx::Vector2dF& scroll_compensation_adjustment) {
847 if (scroll_compensation_adjustment_ == scroll_compensation_adjustment)
848 return;
849 scroll_compensation_adjustment_ = scroll_compensation_adjustment;
850 SetNeedsCommit();
853 gfx::Vector2dF Layer::ScrollCompensationAdjustment() const {
854 return scroll_compensation_adjustment_;
857 void Layer::SetScrollOffsetFromImplSide(
858 const gfx::ScrollOffset& scroll_offset) {
859 DCHECK(IsPropertyChangeAllowed());
860 // This function only gets called during a BeginMainFrame, so there
861 // is no need to call SetNeedsUpdate here.
862 DCHECK(layer_tree_host_ && layer_tree_host_->CommitRequested());
863 if (scroll_offset_ == scroll_offset)
864 return;
865 scroll_offset_ = scroll_offset;
866 SetNeedsPushProperties();
868 bool needs_rebuild = true;
869 if (TransformNode* transform_node =
870 layer_tree_host_->property_trees()->transform_tree.Node(
871 transform_tree_index())) {
872 if (transform_node->owner_id == id()) {
873 transform_node->data.scroll_offset = CurrentScrollOffset();
874 transform_node->data.needs_local_transform_update = true;
875 layer_tree_host_->property_trees()->transform_tree.set_needs_update(true);
876 needs_rebuild = false;
880 if (needs_rebuild)
881 layer_tree_host_->property_trees()->needs_rebuild = true;
883 if (!did_scroll_callback_.is_null())
884 did_scroll_callback_.Run();
885 // The callback could potentially change the layer structure:
886 // "this" may have been destroyed during the process.
889 void Layer::SetScrollClipLayerId(int clip_layer_id) {
890 DCHECK(IsPropertyChangeAllowed());
891 if (scroll_clip_layer_id_ == clip_layer_id)
892 return;
893 scroll_clip_layer_id_ = clip_layer_id;
894 SetNeedsCommit();
897 void Layer::SetUserScrollable(bool horizontal, bool vertical) {
898 DCHECK(IsPropertyChangeAllowed());
899 if (user_scrollable_horizontal_ == horizontal &&
900 user_scrollable_vertical_ == vertical)
901 return;
902 user_scrollable_horizontal_ = horizontal;
903 user_scrollable_vertical_ = vertical;
904 SetNeedsCommit();
907 void Layer::SetShouldScrollOnMainThread(bool should_scroll_on_main_thread) {
908 DCHECK(IsPropertyChangeAllowed());
909 if (should_scroll_on_main_thread_ == should_scroll_on_main_thread)
910 return;
911 should_scroll_on_main_thread_ = should_scroll_on_main_thread;
912 SetNeedsCommit();
915 void Layer::SetHaveWheelEventHandlers(bool have_wheel_event_handlers) {
916 DCHECK(IsPropertyChangeAllowed());
917 if (have_wheel_event_handlers_ == have_wheel_event_handlers)
918 return;
919 if (touch_event_handler_region_.IsEmpty() && layer_tree_host_ &&
920 !layer_tree_host_->needs_meta_info_recomputation())
921 UpdateNumInputHandlersForSubtree(have_wheel_event_handlers);
923 have_wheel_event_handlers_ = have_wheel_event_handlers;
924 SetNeedsCommit();
927 void Layer::UpdateNumInputHandlersForSubtree(bool add) {
928 int change = add ? 1 : -1;
929 for (Layer* layer = this; layer; layer = layer->parent()) {
930 layer->num_layer_or_descendants_with_input_handler_ += change;
931 layer->draw_properties().layer_or_descendant_has_input_handler =
932 (layer->num_layer_or_descendants_with_input_handler_ != 0);
933 DCHECK_GE(layer->num_layer_or_descendants_with_input_handler_, 0);
937 void Layer::SetHaveScrollEventHandlers(bool have_scroll_event_handlers) {
938 DCHECK(IsPropertyChangeAllowed());
939 if (have_scroll_event_handlers_ == have_scroll_event_handlers)
940 return;
941 have_scroll_event_handlers_ = have_scroll_event_handlers;
942 SetNeedsCommit();
945 void Layer::SetNonFastScrollableRegion(const Region& region) {
946 DCHECK(IsPropertyChangeAllowed());
947 if (non_fast_scrollable_region_ == region)
948 return;
949 non_fast_scrollable_region_ = region;
950 SetNeedsCommit();
953 void Layer::SetTouchEventHandlerRegion(const Region& region) {
954 DCHECK(IsPropertyChangeAllowed());
955 if (touch_event_handler_region_ == region)
956 return;
957 if (!have_wheel_event_handlers_ && layer_tree_host_ &&
958 !layer_tree_host_->needs_meta_info_recomputation())
959 UpdateNumInputHandlersForSubtree(!region.IsEmpty());
961 touch_event_handler_region_ = region;
962 SetNeedsCommit();
965 void Layer::SetScrollBlocksOn(ScrollBlocksOn scroll_blocks_on) {
966 DCHECK(IsPropertyChangeAllowed());
967 if (scroll_blocks_on_ == scroll_blocks_on)
968 return;
969 scroll_blocks_on_ = scroll_blocks_on;
970 SetNeedsCommit();
973 void Layer::SetDrawCheckerboardForMissingTiles(bool checkerboard) {
974 DCHECK(IsPropertyChangeAllowed());
975 if (draw_checkerboard_for_missing_tiles_ == checkerboard)
976 return;
977 draw_checkerboard_for_missing_tiles_ = checkerboard;
978 SetNeedsCommit();
981 void Layer::SetForceRenderSurface(bool force) {
982 DCHECK(IsPropertyChangeAllowed());
983 if (force_render_surface_ == force)
984 return;
985 force_render_surface_ = force;
986 SetNeedsCommit();
989 void Layer::SetDoubleSided(bool double_sided) {
990 DCHECK(IsPropertyChangeAllowed());
991 if (double_sided_ == double_sided)
992 return;
993 double_sided_ = double_sided;
994 SetNeedsCommit();
997 void Layer::Set3dSortingContextId(int id) {
998 DCHECK(IsPropertyChangeAllowed());
999 if (id == sorting_context_id_)
1000 return;
1001 sorting_context_id_ = id;
1002 SetNeedsCommit();
1005 void Layer::SetTransformTreeIndex(int index) {
1006 DCHECK(IsPropertyChangeAllowed());
1007 if (transform_tree_index_ == index)
1008 return;
1009 transform_tree_index_ = index;
1010 SetNeedsPushProperties();
1013 int Layer::transform_tree_index() const {
1014 if (!layer_tree_host_ ||
1015 layer_tree_host_->property_trees()->sequence_number !=
1016 property_tree_sequence_number_) {
1017 return -1;
1019 return transform_tree_index_;
1022 void Layer::SetClipTreeIndex(int index) {
1023 DCHECK(IsPropertyChangeAllowed());
1024 if (clip_tree_index_ == index)
1025 return;
1026 clip_tree_index_ = index;
1027 SetNeedsPushProperties();
1030 int Layer::clip_tree_index() const {
1031 if (!layer_tree_host_ ||
1032 layer_tree_host_->property_trees()->sequence_number !=
1033 property_tree_sequence_number_) {
1034 return -1;
1036 return clip_tree_index_;
1039 void Layer::SetOpacityTreeIndex(int index) {
1040 DCHECK(IsPropertyChangeAllowed());
1041 if (opacity_tree_index_ == index)
1042 return;
1043 opacity_tree_index_ = index;
1044 SetNeedsPushProperties();
1047 int Layer::opacity_tree_index() const {
1048 if (!layer_tree_host_ ||
1049 layer_tree_host_->property_trees()->sequence_number !=
1050 property_tree_sequence_number_) {
1051 return -1;
1053 return opacity_tree_index_;
1056 void Layer::SetShouldFlattenTransform(bool should_flatten) {
1057 DCHECK(IsPropertyChangeAllowed());
1058 if (should_flatten_transform_ == should_flatten)
1059 return;
1060 should_flatten_transform_ = should_flatten;
1061 SetNeedsCommit();
1064 void Layer::SetIsDrawable(bool is_drawable) {
1065 DCHECK(IsPropertyChangeAllowed());
1066 if (is_drawable_ == is_drawable)
1067 return;
1069 is_drawable_ = is_drawable;
1070 UpdateDrawsContent(HasDrawableContent());
1073 void Layer::SetHideLayerAndSubtree(bool hide) {
1074 DCHECK(IsPropertyChangeAllowed());
1075 if (hide_layer_and_subtree_ == hide)
1076 return;
1078 hide_layer_and_subtree_ = hide;
1079 SetNeedsCommit();
1082 void Layer::SetNeedsDisplayRect(const gfx::Rect& dirty_rect) {
1083 if (dirty_rect.IsEmpty())
1084 return;
1086 SetNeedsPushProperties();
1087 update_rect_.Union(dirty_rect);
1089 if (DrawsContent())
1090 SetNeedsUpdate();
1093 bool Layer::DescendantIsFixedToContainerLayer() const {
1094 for (size_t i = 0; i < children_.size(); ++i) {
1095 if (children_[i]->position_constraint_.is_fixed_position() ||
1096 children_[i]->DescendantIsFixedToContainerLayer())
1097 return true;
1099 return false;
1102 void Layer::SetIsContainerForFixedPositionLayers(bool container) {
1103 if (is_container_for_fixed_position_layers_ == container)
1104 return;
1105 is_container_for_fixed_position_layers_ = container;
1107 if (layer_tree_host_ && layer_tree_host_->CommitRequested())
1108 return;
1110 // Only request a commit if we have a fixed positioned descendant.
1111 if (DescendantIsFixedToContainerLayer())
1112 SetNeedsCommit();
1115 void Layer::SetPositionConstraint(const LayerPositionConstraint& constraint) {
1116 DCHECK(IsPropertyChangeAllowed());
1117 if (position_constraint_ == constraint)
1118 return;
1119 position_constraint_ = constraint;
1120 SetNeedsCommit();
1123 static void RunCopyCallbackOnMainThread(scoped_ptr<CopyOutputRequest> request,
1124 scoped_ptr<CopyOutputResult> result) {
1125 request->SendResult(result.Pass());
1128 static void PostCopyCallbackToMainThread(
1129 scoped_refptr<base::SingleThreadTaskRunner> main_thread_task_runner,
1130 scoped_ptr<CopyOutputRequest> request,
1131 scoped_ptr<CopyOutputResult> result) {
1132 main_thread_task_runner->PostTask(FROM_HERE,
1133 base::Bind(&RunCopyCallbackOnMainThread,
1134 base::Passed(&request),
1135 base::Passed(&result)));
1138 void Layer::PushPropertiesTo(LayerImpl* layer) {
1139 DCHECK(layer_tree_host_);
1141 // If we did not SavePaintProperties() for the layer this frame, then push the
1142 // real property values, not the paint property values.
1143 bool use_paint_properties = paint_properties_.source_frame_number ==
1144 layer_tree_host_->source_frame_number();
1146 layer->SetTransformOrigin(transform_origin_);
1147 layer->SetBackgroundColor(background_color_);
1148 layer->SetBounds(use_paint_properties ? paint_properties_.bounds
1149 : bounds_);
1151 // TODO(enne): This is needed because CDP does this. Once main thread CDP
1152 // goes away, content scale / bounds can be removed.
1153 if (layer_tree_host()->settings().impl_side_painting) {
1154 layer->SetContentsScale(1.f, 1.f);
1155 layer->SetContentBounds(bounds());
1156 } else {
1157 layer->SetContentBounds(content_bounds());
1158 layer->SetContentsScale(contents_scale_x(), contents_scale_y());
1161 if (frame_viewer_instrumentation::IsTracingLayerTreeSnapshots())
1162 layer->SetDebugInfo(TakeDebugInfo());
1164 layer->SetTransformTreeIndex(transform_tree_index());
1165 layer->SetOpacityTreeIndex(opacity_tree_index());
1166 layer->SetClipTreeIndex(clip_tree_index());
1167 layer->set_offset_to_transform_parent(offset_to_transform_parent_);
1168 layer->SetDoubleSided(double_sided_);
1169 layer->SetDrawCheckerboardForMissingTiles(
1170 draw_checkerboard_for_missing_tiles_);
1171 layer->SetDrawsContent(DrawsContent());
1172 layer->SetHideLayerAndSubtree(hide_layer_and_subtree_);
1173 layer->SetHasRenderSurface(has_render_surface_ || layer->HasCopyRequest());
1174 if (!layer->FilterIsAnimatingOnImplOnly() && !FilterIsAnimating())
1175 layer->SetFilters(filters_);
1176 DCHECK(!(FilterIsAnimating() && layer->FilterIsAnimatingOnImplOnly()));
1177 layer->SetBackgroundFilters(background_filters());
1178 layer->SetMasksToBounds(masks_to_bounds_);
1179 layer->SetShouldScrollOnMainThread(should_scroll_on_main_thread_);
1180 layer->SetHaveWheelEventHandlers(have_wheel_event_handlers_);
1181 layer->SetHaveScrollEventHandlers(have_scroll_event_handlers_);
1182 layer->SetNonFastScrollableRegion(non_fast_scrollable_region_);
1183 layer->SetTouchEventHandlerRegion(touch_event_handler_region_);
1184 layer->SetScrollBlocksOn(scroll_blocks_on_);
1185 layer->SetContentsOpaque(contents_opaque_);
1186 if (!layer->OpacityIsAnimatingOnImplOnly() && !OpacityIsAnimating()) {
1187 layer->SetOpacity(opacity_);
1188 } else {
1189 // The just-pushed opacity tree will contain |opacity_|. Since we didn't
1190 // push this value to |layer|, it might have a different opacity value. To
1191 // ensure consistency, we need to update the opacity tree.
1192 layer->UpdatePropertyTreeOpacity();
1194 DCHECK(!(OpacityIsAnimating() && layer->OpacityIsAnimatingOnImplOnly()));
1195 layer->SetBlendMode(blend_mode_);
1196 layer->SetIsRootForIsolatedGroup(is_root_for_isolated_group_);
1197 layer->SetPosition(position_);
1198 layer->SetIsContainerForFixedPositionLayers(
1199 IsContainerForFixedPositionLayers());
1200 layer->SetPositionConstraint(position_constraint_);
1201 layer->SetShouldFlattenTransform(should_flatten_transform_);
1202 layer->set_should_flatten_transform_from_property_tree(
1203 should_flatten_transform_from_property_tree_);
1204 layer->SetUseParentBackfaceVisibility(use_parent_backface_visibility_);
1205 if (!layer->TransformIsAnimatingOnImplOnly() && !TransformIsAnimating()) {
1206 layer->SetTransformAndInvertibility(transform_, transform_is_invertible_);
1207 } else {
1208 // The just-pushed transform tree will contain |transform_|. Since we
1209 // didn't push this value to |layer|, it might have a different transform
1210 // value. To ensure consistency, we need to update the transform tree.
1211 layer->UpdatePropertyTreeTransform();
1213 DCHECK(!(TransformIsAnimating() && layer->TransformIsAnimatingOnImplOnly()));
1214 layer->Set3dSortingContextId(sorting_context_id_);
1215 layer->SetNumDescendantsThatDrawContent(num_descendants_that_draw_content_);
1217 layer->SetScrollClipLayer(scroll_clip_layer_id_);
1218 layer->set_user_scrollable_horizontal(user_scrollable_horizontal_);
1219 layer->set_user_scrollable_vertical(user_scrollable_vertical_);
1221 LayerImpl* scroll_parent = nullptr;
1222 if (scroll_parent_) {
1223 scroll_parent = layer->layer_tree_impl()->LayerById(scroll_parent_->id());
1224 DCHECK(scroll_parent);
1227 layer->SetScrollParent(scroll_parent);
1228 if (scroll_children_) {
1229 std::set<LayerImpl*>* scroll_children = new std::set<LayerImpl*>;
1230 for (std::set<Layer*>::iterator it = scroll_children_->begin();
1231 it != scroll_children_->end();
1232 ++it) {
1233 DCHECK_EQ((*it)->scroll_parent(), this);
1234 LayerImpl* scroll_child =
1235 layer->layer_tree_impl()->LayerById((*it)->id());
1236 DCHECK(scroll_child);
1237 scroll_children->insert(scroll_child);
1239 layer->SetScrollChildren(scroll_children);
1240 } else {
1241 layer->SetScrollChildren(nullptr);
1244 LayerImpl* clip_parent = nullptr;
1245 if (clip_parent_) {
1246 clip_parent =
1247 layer->layer_tree_impl()->LayerById(clip_parent_->id());
1248 DCHECK(clip_parent);
1251 layer->SetClipParent(clip_parent);
1252 if (clip_children_) {
1253 std::set<LayerImpl*>* clip_children = new std::set<LayerImpl*>;
1254 for (std::set<Layer*>::iterator it = clip_children_->begin();
1255 it != clip_children_->end(); ++it) {
1256 DCHECK_EQ((*it)->clip_parent(), this);
1257 LayerImpl* clip_child = layer->layer_tree_impl()->LayerById((*it)->id());
1258 DCHECK(clip_child);
1259 clip_children->insert(clip_child);
1261 layer->SetClipChildren(clip_children);
1262 } else {
1263 layer->SetClipChildren(nullptr);
1266 // When a scroll offset animation is interrupted the new scroll position on
1267 // the pending tree will clobber any impl-side scrolling occuring on the
1268 // active tree. To do so, avoid scrolling the pending tree along with it
1269 // instead of trying to undo that scrolling later.
1270 if (layer_animation_controller_->scroll_offset_animation_was_interrupted())
1271 layer->PushScrollOffsetFromMainThreadAndClobberActiveValue(scroll_offset_);
1272 else
1273 layer->PushScrollOffsetFromMainThread(scroll_offset_);
1274 layer->SetScrollCompensationAdjustment(ScrollCompensationAdjustment());
1276 // Wrap the copy_requests_ in a PostTask to the main thread.
1277 int size = copy_requests_.size();
1278 ScopedPtrVector<CopyOutputRequest> main_thread_copy_requests;
1279 for (ScopedPtrVector<CopyOutputRequest>::iterator it = copy_requests_.begin();
1280 it != copy_requests_.end();
1281 ++it) {
1282 scoped_refptr<base::SingleThreadTaskRunner> main_thread_task_runner =
1283 layer_tree_host()->proxy()->MainThreadTaskRunner();
1284 scoped_ptr<CopyOutputRequest> original_request = copy_requests_.take(it);
1285 const CopyOutputRequest& original_request_ref = *original_request;
1286 scoped_ptr<CopyOutputRequest> main_thread_request =
1287 CopyOutputRequest::CreateRelayRequest(
1288 original_request_ref,
1289 base::Bind(&PostCopyCallbackToMainThread,
1290 main_thread_task_runner,
1291 base::Passed(&original_request)));
1292 main_thread_copy_requests.push_back(main_thread_request.Pass());
1294 if (!copy_requests_.empty() && layer_tree_host_)
1295 layer_tree_host_->property_trees()->needs_rebuild = true;
1296 if (size != 0)
1297 UpdateNumCopyRequestsForSubtree(false);
1298 copy_requests_.clear();
1299 layer->PassCopyRequests(&main_thread_copy_requests);
1301 // If the main thread commits multiple times before the impl thread actually
1302 // draws, then damage tracking will become incorrect if we simply clobber the
1303 // update_rect here. The LayerImpl's update_rect needs to accumulate (i.e.
1304 // union) any update changes that have occurred on the main thread.
1305 update_rect_.Union(layer->update_rect());
1306 layer->SetUpdateRect(update_rect_);
1308 layer->SetStackingOrderChanged(stacking_order_changed_);
1310 layer_animation_controller_->PushAnimationUpdatesTo(
1311 layer->layer_animation_controller());
1313 if (frame_timing_requests_dirty_) {
1314 layer->SetFrameTimingRequests(frame_timing_requests_);
1315 frame_timing_requests_dirty_ = false;
1318 // Reset any state that should be cleared for the next update.
1319 stacking_order_changed_ = false;
1320 update_rect_ = gfx::Rect();
1322 needs_push_properties_ = false;
1323 num_dependents_need_push_properties_ = 0;
1326 scoped_ptr<LayerImpl> Layer::CreateLayerImpl(LayerTreeImpl* tree_impl) {
1327 return LayerImpl::Create(tree_impl, layer_id_,
1328 new LayerImpl::SyncedScrollOffset);
1331 bool Layer::DrawsContent() const {
1332 return draws_content_;
1335 bool Layer::HasDrawableContent() const {
1336 return is_drawable_;
1339 void Layer::UpdateDrawsContent(bool has_drawable_content) {
1340 bool draws_content = has_drawable_content;
1341 DCHECK(is_drawable_ || !has_drawable_content);
1342 if (draws_content == draws_content_)
1343 return;
1345 if (HasDelegatedContent()) {
1346 // Layers with delegated content need to be treated as if they have as
1347 // many children as the number of layers they own delegated quads for.
1348 // Since we don't know this number right now, we choose one that acts like
1349 // infinity for our purposes.
1350 AddDrawableDescendants(draws_content ? 1000 : -1000);
1353 if (parent())
1354 parent()->AddDrawableDescendants(draws_content ? 1 : -1);
1356 draws_content_ = draws_content;
1357 SetNeedsCommit();
1360 int Layer::NumDescendantsThatDrawContent() const {
1361 return num_descendants_that_draw_content_;
1364 void Layer::SavePaintProperties() {
1365 DCHECK(layer_tree_host_);
1367 // TODO(reveman): Save all layer properties that we depend on not
1368 // changing until PushProperties() has been called. crbug.com/231016
1369 paint_properties_.bounds = bounds_;
1370 paint_properties_.source_frame_number =
1371 layer_tree_host_->source_frame_number();
1374 bool Layer::Update(ResourceUpdateQueue* queue,
1375 const OcclusionTracker<Layer>* occlusion) {
1376 DCHECK(layer_tree_host_);
1377 DCHECK_EQ(layer_tree_host_->source_frame_number(),
1378 paint_properties_.source_frame_number) <<
1379 "SavePaintProperties must be called for any layer that is painted.";
1380 return false;
1383 bool Layer::NeedMoreUpdates() {
1384 return false;
1387 bool Layer::IsSuitableForGpuRasterization() const {
1388 return true;
1391 scoped_refptr<base::trace_event::ConvertableToTraceFormat>
1392 Layer::TakeDebugInfo() {
1393 if (client_)
1394 return client_->TakeDebugInfo();
1395 else
1396 return nullptr;
1399 void Layer::SetHasRenderSurface(bool has_render_surface) {
1400 if (has_render_surface_ == has_render_surface)
1401 return;
1402 has_render_surface_ = has_render_surface;
1403 // We do not need SetNeedsCommit here, since this is only ever called
1404 // during a commit, from CalculateDrawProperties.
1405 SetNeedsPushProperties();
1408 void Layer::CreateRenderSurface() {
1409 DCHECK(!render_surface_);
1410 render_surface_ = make_scoped_ptr(new RenderSurface(this));
1413 void Layer::ClearRenderSurface() {
1414 render_surface_ = nullptr;
1417 void Layer::ClearRenderSurfaceLayerList() {
1418 if (render_surface_)
1419 render_surface_->ClearLayerLists();
1422 gfx::ScrollOffset Layer::ScrollOffsetForAnimation() const {
1423 return CurrentScrollOffset();
1426 // On<Property>Animated is called due to an ongoing accelerated animation.
1427 // Since this animation is also being run on the compositor thread, there
1428 // is no need to request a commit to push this value over, so the value is
1429 // set directly rather than by calling Set<Property>.
1430 void Layer::OnFilterAnimated(const FilterOperations& filters) {
1431 filters_ = filters;
1434 void Layer::OnOpacityAnimated(float opacity) {
1435 opacity_ = opacity;
1436 if (layer_tree_host_) {
1437 if (OpacityNode* node =
1438 layer_tree_host_->property_trees()->opacity_tree.Node(
1439 opacity_tree_index())) {
1440 if (node->owner_id == id())
1441 node->data = opacity;
1446 void Layer::OnTransformAnimated(const gfx::Transform& transform) {
1447 if (transform_ == transform)
1448 return;
1449 transform_ = transform;
1450 transform_is_invertible_ = transform.IsInvertible();
1451 if (layer_tree_host_) {
1452 if (TransformNode* node =
1453 layer_tree_host_->property_trees()->transform_tree.Node(
1454 transform_tree_index())) {
1455 if (node->owner_id == id()) {
1456 node->data.local = transform;
1457 node->data.needs_local_transform_update = true;
1458 node->data.is_animated = true;
1459 layer_tree_host_->property_trees()->transform_tree.set_needs_update(
1460 true);
1466 void Layer::OnScrollOffsetAnimated(const gfx::ScrollOffset& scroll_offset) {
1467 // Do nothing. Scroll deltas will be sent from the compositor thread back
1468 // to the main thread in the same manner as during non-animated
1469 // compositor-driven scrolling.
1472 void Layer::OnAnimationWaitingForDeletion() {
1473 // Animations are only deleted during PushProperties.
1474 SetNeedsPushProperties();
1477 bool Layer::IsActive() const {
1478 return true;
1481 bool Layer::AddAnimation(scoped_ptr <Animation> animation) {
1482 DCHECK(layer_animation_controller_);
1483 if (!layer_animation_controller_->animation_registrar())
1484 return false;
1486 if (animation->target_property() == Animation::SCROLL_OFFSET &&
1487 !layer_animation_controller_->animation_registrar()
1488 ->supports_scroll_animations())
1489 return false;
1491 UMA_HISTOGRAM_BOOLEAN("Renderer.AnimationAddedToOrphanLayer",
1492 !layer_tree_host_);
1493 layer_animation_controller_->AddAnimation(animation.Pass());
1494 SetNeedsCommit();
1495 return true;
1498 void Layer::PauseAnimation(int animation_id, double time_offset) {
1499 DCHECK(layer_animation_controller_);
1500 layer_animation_controller_->PauseAnimation(
1501 animation_id, base::TimeDelta::FromSecondsD(time_offset));
1502 SetNeedsCommit();
1505 void Layer::RemoveAnimation(int animation_id) {
1506 DCHECK(layer_animation_controller_);
1507 layer_animation_controller_->RemoveAnimation(animation_id);
1508 SetNeedsCommit();
1511 void Layer::RemoveAnimation(int animation_id,
1512 Animation::TargetProperty property) {
1513 DCHECK(layer_animation_controller_);
1514 layer_animation_controller_->RemoveAnimation(animation_id, property);
1515 SetNeedsCommit();
1518 void Layer::SetLayerAnimationControllerForTest(
1519 scoped_refptr<LayerAnimationController> controller) {
1520 layer_animation_controller_->RemoveValueObserver(this);
1521 layer_animation_controller_ = controller;
1522 layer_animation_controller_->AddValueObserver(this);
1523 SetNeedsCommit();
1526 bool Layer::HasActiveAnimation() const {
1527 DCHECK(layer_animation_controller_);
1528 return layer_animation_controller_->HasActiveAnimation();
1531 void Layer::RegisterForAnimations(AnimationRegistrar* registrar) {
1532 if (layer_animation_controller_)
1533 layer_animation_controller_->SetAnimationRegistrar(registrar);
1536 void Layer::AddLayerAnimationEventObserver(
1537 LayerAnimationEventObserver* animation_observer) {
1538 DCHECK(layer_animation_controller_);
1539 layer_animation_controller_->AddEventObserver(animation_observer);
1542 void Layer::RemoveLayerAnimationEventObserver(
1543 LayerAnimationEventObserver* animation_observer) {
1544 DCHECK(layer_animation_controller_);
1545 layer_animation_controller_->RemoveEventObserver(animation_observer);
1548 SimpleEnclosedRegion Layer::VisibleContentOpaqueRegion() const {
1549 if (contents_opaque())
1550 return SimpleEnclosedRegion(visible_content_rect());
1551 return SimpleEnclosedRegion();
1554 ScrollbarLayerInterface* Layer::ToScrollbarLayer() {
1555 return nullptr;
1558 RenderingStatsInstrumentation* Layer::rendering_stats_instrumentation() const {
1559 return layer_tree_host_->rendering_stats_instrumentation();
1562 void Layer::RemoveFromScrollTree() {
1563 if (scroll_children_.get()) {
1564 std::set<Layer*> copy = *scroll_children_;
1565 for (std::set<Layer*>::iterator it = copy.begin(); it != copy.end(); ++it)
1566 (*it)->SetScrollParent(nullptr);
1569 DCHECK(!scroll_children_);
1570 SetScrollParent(nullptr);
1573 void Layer::RemoveFromClipTree() {
1574 if (clip_children_.get()) {
1575 std::set<Layer*> copy = *clip_children_;
1576 for (std::set<Layer*>::iterator it = copy.begin(); it != copy.end(); ++it)
1577 (*it)->SetClipParent(nullptr);
1580 DCHECK(!clip_children_);
1581 SetClipParent(nullptr);
1584 void Layer::AddDrawableDescendants(int num) {
1585 DCHECK_GE(num_descendants_that_draw_content_, 0);
1586 DCHECK_GE(num_descendants_that_draw_content_ + num, 0);
1587 if (num == 0)
1588 return;
1589 num_descendants_that_draw_content_ += num;
1590 SetNeedsCommit();
1591 if (parent())
1592 parent()->AddDrawableDescendants(num);
1595 void Layer::RunMicroBenchmark(MicroBenchmark* benchmark) {
1596 benchmark->RunOnLayer(this);
1599 bool Layer::HasDelegatedContent() const {
1600 return false;
1603 void Layer::SetFrameTimingRequests(
1604 const std::vector<FrameTimingRequest>& requests) {
1605 // TODO(vmpstr): Early out if there are no changes earlier in the call stack.
1606 if (requests == frame_timing_requests_)
1607 return;
1608 frame_timing_requests_ = requests;
1609 frame_timing_requests_dirty_ = true;
1610 SetNeedsCommit();
1613 void Layer::DidBeginTracing() {
1614 // We'll be dumping layer trees as part of trace, so make sure
1615 // PushPropertiesTo() propagates layer debug info to the impl
1616 // side -- otherwise this won't happen for the the layers that
1617 // remain unchanged since tracing started.
1618 SetNeedsPushProperties();
1621 void Layer::set_visited(bool visited) {
1622 visited_tracker_ =
1623 visited ? layer_tree_host()->meta_information_sequence_number() : 0;
1626 bool Layer::visited() {
1627 return visited_tracker_ ==
1628 layer_tree_host()->meta_information_sequence_number();
1631 void Layer::set_layer_or_descendant_is_drawn(
1632 bool layer_or_descendant_is_drawn) {
1633 layer_or_descendant_is_drawn_tracker_ =
1634 layer_or_descendant_is_drawn
1635 ? layer_tree_host()->meta_information_sequence_number()
1636 : 0;
1639 bool Layer::layer_or_descendant_is_drawn() {
1640 return layer_or_descendant_is_drawn_tracker_ ==
1641 layer_tree_host()->meta_information_sequence_number();
1644 void Layer::set_sorted_for_recursion(bool sorted_for_recursion) {
1645 sorted_for_recursion_tracker_ =
1646 sorted_for_recursion
1647 ? layer_tree_host()->meta_information_sequence_number()
1648 : 0;
1651 bool Layer::sorted_for_recursion() {
1652 return sorted_for_recursion_tracker_ ==
1653 layer_tree_host()->meta_information_sequence_number();
1656 } // namespace cc