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"
9 #include "base/debug/trace_event.h"
10 #include "base/location.h"
11 #include "base/metrics/histogram.h"
12 #include "base/single_thread_task_runner.h"
13 #include "cc/animation/animation.h"
14 #include "cc/animation/animation_events.h"
15 #include "cc/animation/keyframed_animation_curve.h"
16 #include "cc/animation/layer_animation_controller.h"
17 #include "cc/layers/layer_client.h"
18 #include "cc/layers/layer_impl.h"
19 #include "cc/layers/scrollbar_layer_interface.h"
20 #include "cc/output/copy_output_request.h"
21 #include "cc/output/copy_output_result.h"
22 #include "cc/trees/layer_tree_host.h"
23 #include "cc/trees/layer_tree_impl.h"
24 #include "third_party/skia/include/core/SkImageFilter.h"
25 #include "ui/gfx/geometry/vector2d_conversions.h"
26 #include "ui/gfx/rect_conversions.h"
30 static int s_next_layer_id = 1;
32 scoped_refptr<Layer> Layer::Create() {
33 return make_scoped_refptr(new Layer());
37 : needs_push_properties_(false),
38 num_dependents_need_push_properties_(false),
39 stacking_order_changed_(false),
40 layer_id_(s_next_layer_id++),
41 ignore_set_needs_commit_(false),
43 layer_tree_host_(NULL),
44 scroll_clip_layer_id_(INVALID_ID),
45 should_scroll_on_main_thread_(false),
46 have_wheel_event_handlers_(false),
47 have_scroll_event_handlers_(false),
48 user_scrollable_horizontal_(true),
49 user_scrollable_vertical_(true),
50 is_root_for_isolated_group_(false),
51 is_container_for_fixed_position_layers_(false),
53 hide_layer_and_subtree_(false),
54 masks_to_bounds_(false),
55 contents_opaque_(false),
57 should_flatten_transform_(true),
58 use_parent_backface_visibility_(false),
59 draw_checkerboard_for_missing_tiles_(false),
60 force_render_surface_(false),
62 transform_is_invertible_(true),
63 anchor_point_(0.5f, 0.5f),
66 blend_mode_(SkXfermode::kSrcOver_Mode),
73 if (layer_id_ == INT_MAX) {
77 layer_animation_controller_ = LayerAnimationController::Create(layer_id_);
78 layer_animation_controller_->AddValueObserver(this);
79 layer_animation_controller_->set_value_provider(this);
83 // Our parent should be holding a reference to us so there should be no
84 // way for us to be destroyed while we still have a parent.
86 // Similarly we shouldn't have a layer tree host since it also keeps a
88 DCHECK(!layer_tree_host());
90 layer_animation_controller_->RemoveValueObserver(this);
91 layer_animation_controller_->remove_value_provider(this);
93 // Remove the parent reference from all children and dependents.
95 if (mask_layer_.get()) {
96 DCHECK_EQ(this, mask_layer_->parent());
97 mask_layer_->RemoveFromParent();
99 if (replica_layer_.get()) {
100 DCHECK_EQ(this, replica_layer_->parent());
101 replica_layer_->RemoveFromParent();
104 RemoveFromScrollTree();
105 RemoveFromClipTree();
108 void Layer::SetLayerTreeHost(LayerTreeHost* host) {
109 if (layer_tree_host_ == host)
112 layer_tree_host_ = host;
114 // When changing hosts, the layer needs to commit its properties to the impl
115 // side for the new host.
116 SetNeedsPushProperties();
118 for (size_t i = 0; i < children_.size(); ++i)
119 children_[i]->SetLayerTreeHost(host);
121 if (mask_layer_.get())
122 mask_layer_->SetLayerTreeHost(host);
123 if (replica_layer_.get())
124 replica_layer_->SetLayerTreeHost(host);
127 layer_animation_controller_->SetAnimationRegistrar(
128 host->animation_registrar());
130 if (host->settings().layer_transforms_should_scale_layer_contents)
131 reset_raster_scale_to_unknown();
134 if (host && layer_animation_controller_->has_any_animation())
135 host->SetNeedsCommit();
138 void Layer::SetNeedsUpdate() {
139 if (layer_tree_host_ && !ignore_set_needs_commit_)
140 layer_tree_host_->SetNeedsUpdateLayers();
143 void Layer::SetNeedsCommit() {
144 if (!layer_tree_host_)
147 SetNeedsPushProperties();
149 if (ignore_set_needs_commit_)
152 layer_tree_host_->SetNeedsCommit();
155 void Layer::SetNeedsFullTreeSync() {
156 if (!layer_tree_host_)
159 layer_tree_host_->SetNeedsFullTreeSync();
162 void Layer::SetNextCommitWaitsForActivation() {
163 if (!layer_tree_host_)
166 layer_tree_host_->SetNextCommitWaitsForActivation();
169 void Layer::SetNeedsPushProperties() {
170 if (needs_push_properties_)
172 if (!parent_should_know_need_push_properties() && parent_)
173 parent_->AddDependentNeedsPushProperties();
174 needs_push_properties_ = true;
177 void Layer::AddDependentNeedsPushProperties() {
178 DCHECK_GE(num_dependents_need_push_properties_, 0);
180 if (!parent_should_know_need_push_properties() && parent_)
181 parent_->AddDependentNeedsPushProperties();
183 num_dependents_need_push_properties_++;
186 void Layer::RemoveDependentNeedsPushProperties() {
187 num_dependents_need_push_properties_--;
188 DCHECK_GE(num_dependents_need_push_properties_, 0);
190 if (!parent_should_know_need_push_properties() && parent_)
191 parent_->RemoveDependentNeedsPushProperties();
194 bool Layer::IsPropertyChangeAllowed() const {
195 if (!layer_tree_host_)
198 if (!layer_tree_host_->settings().strict_layer_property_change_checking)
201 return !layer_tree_host_->in_paint_layer_contents();
204 gfx::Rect Layer::LayerRectToContentRect(const gfx::RectF& layer_rect) const {
205 gfx::RectF content_rect =
206 gfx::ScaleRect(layer_rect, contents_scale_x(), contents_scale_y());
207 // Intersect with content rect to avoid the extra pixel because for some
208 // values x and y, ceil((x / y) * y) may be x + 1.
209 content_rect.Intersect(gfx::Rect(content_bounds()));
210 return gfx::ToEnclosingRect(content_rect);
213 skia::RefPtr<SkPicture> Layer::GetPicture() const {
214 return skia::RefPtr<SkPicture>();
217 void Layer::SetParent(Layer* layer) {
218 DCHECK(!layer || !layer->HasAncestor(this));
220 if (parent_should_know_need_push_properties()) {
222 parent_->RemoveDependentNeedsPushProperties();
224 layer->AddDependentNeedsPushProperties();
228 SetLayerTreeHost(parent_ ? parent_->layer_tree_host() : NULL);
230 if (!layer_tree_host_)
232 const LayerTreeSettings& settings = layer_tree_host_->settings();
233 if (!settings.layer_transforms_should_scale_layer_contents)
236 reset_raster_scale_to_unknown();
237 if (mask_layer_.get())
238 mask_layer_->reset_raster_scale_to_unknown();
239 if (replica_layer_.get() && replica_layer_->mask_layer_.get())
240 replica_layer_->mask_layer_->reset_raster_scale_to_unknown();
243 void Layer::AddChild(scoped_refptr<Layer> child) {
244 InsertChild(child, children_.size());
247 void Layer::InsertChild(scoped_refptr<Layer> child, size_t index) {
248 DCHECK(IsPropertyChangeAllowed());
249 child->RemoveFromParent();
250 child->SetParent(this);
251 child->stacking_order_changed_ = true;
253 index = std::min(index, children_.size());
254 children_.insert(children_.begin() + index, child);
255 SetNeedsFullTreeSync();
258 void Layer::RemoveFromParent() {
259 DCHECK(IsPropertyChangeAllowed());
261 parent_->RemoveChildOrDependent(this);
264 void Layer::RemoveChildOrDependent(Layer* child) {
265 if (mask_layer_.get() == child) {
266 mask_layer_->SetParent(NULL);
268 SetNeedsFullTreeSync();
271 if (replica_layer_.get() == child) {
272 replica_layer_->SetParent(NULL);
273 replica_layer_ = NULL;
274 SetNeedsFullTreeSync();
278 for (LayerList::iterator iter = children_.begin();
279 iter != children_.end();
281 if (iter->get() != child)
284 child->SetParent(NULL);
285 children_.erase(iter);
286 SetNeedsFullTreeSync();
291 void Layer::ReplaceChild(Layer* reference, scoped_refptr<Layer> new_layer) {
293 DCHECK_EQ(reference->parent(), this);
294 DCHECK(IsPropertyChangeAllowed());
296 if (reference == new_layer.get())
299 int reference_index = IndexOfChild(reference);
300 if (reference_index == -1) {
305 reference->RemoveFromParent();
307 if (new_layer.get()) {
308 new_layer->RemoveFromParent();
309 InsertChild(new_layer, reference_index);
313 int Layer::IndexOfChild(const Layer* reference) {
314 for (size_t i = 0; i < children_.size(); ++i) {
315 if (children_[i].get() == reference)
321 void Layer::SetBounds(const gfx::Size& size) {
322 DCHECK(IsPropertyChangeAllowed());
323 if (bounds() == size)
330 Layer* Layer::RootLayer() {
332 while (layer->parent())
333 layer = layer->parent();
337 void Layer::RemoveAllChildren() {
338 DCHECK(IsPropertyChangeAllowed());
339 while (children_.size()) {
340 Layer* layer = children_[0].get();
341 DCHECK_EQ(this, layer->parent());
342 layer->RemoveFromParent();
346 void Layer::SetChildren(const LayerList& children) {
347 DCHECK(IsPropertyChangeAllowed());
348 if (children == children_)
352 for (size_t i = 0; i < children.size(); ++i)
353 AddChild(children[i]);
356 bool Layer::HasAncestor(const Layer* ancestor) const {
357 for (const Layer* layer = parent(); layer; layer = layer->parent()) {
358 if (layer == ancestor)
364 void Layer::RequestCopyOfOutput(
365 scoped_ptr<CopyOutputRequest> request) {
366 DCHECK(IsPropertyChangeAllowed());
367 if (request->IsEmpty())
369 copy_requests_.push_back(request.Pass());
373 void Layer::SetAnchorPoint(const gfx::PointF& anchor_point) {
374 DCHECK(IsPropertyChangeAllowed());
375 if (anchor_point_ == anchor_point)
377 anchor_point_ = anchor_point;
381 void Layer::SetAnchorPointZ(float anchor_point_z) {
382 DCHECK(IsPropertyChangeAllowed());
383 if (anchor_point_z_ == anchor_point_z)
385 anchor_point_z_ = anchor_point_z;
389 void Layer::SetBackgroundColor(SkColor background_color) {
390 DCHECK(IsPropertyChangeAllowed());
391 if (background_color_ == background_color)
393 background_color_ = background_color;
397 SkColor Layer::SafeOpaqueBackgroundColor() const {
398 SkColor color = background_color();
399 if (SkColorGetA(color) == 255 && !contents_opaque()) {
400 color = SK_ColorTRANSPARENT;
401 } else if (SkColorGetA(color) != 255 && contents_opaque()) {
402 for (const Layer* layer = parent(); layer;
403 layer = layer->parent()) {
404 color = layer->background_color();
405 if (SkColorGetA(color) == 255)
408 if (SkColorGetA(color) != 255)
409 color = layer_tree_host_->background_color();
410 if (SkColorGetA(color) != 255)
411 color = SkColorSetA(color, 255);
416 void Layer::CalculateContentsScale(float ideal_contents_scale,
417 float device_scale_factor,
418 float page_scale_factor,
419 float maximum_animation_contents_scale,
420 bool animating_transform_to_screen,
421 float* contents_scale_x,
422 float* contents_scale_y,
423 gfx::Size* content_bounds) {
424 DCHECK(layer_tree_host_);
426 *contents_scale_x = 1;
427 *contents_scale_y = 1;
428 *content_bounds = bounds();
431 void Layer::SetMasksToBounds(bool masks_to_bounds) {
432 DCHECK(IsPropertyChangeAllowed());
433 if (masks_to_bounds_ == masks_to_bounds)
435 masks_to_bounds_ = masks_to_bounds;
439 void Layer::SetMaskLayer(Layer* mask_layer) {
440 DCHECK(IsPropertyChangeAllowed());
441 if (mask_layer_.get() == mask_layer)
443 if (mask_layer_.get()) {
444 DCHECK_EQ(this, mask_layer_->parent());
445 mask_layer_->RemoveFromParent();
447 mask_layer_ = mask_layer;
448 if (mask_layer_.get()) {
449 DCHECK(!mask_layer_->parent());
450 mask_layer_->RemoveFromParent();
451 mask_layer_->SetParent(this);
452 mask_layer_->SetIsMask(true);
454 SetNeedsFullTreeSync();
457 void Layer::SetReplicaLayer(Layer* layer) {
458 DCHECK(IsPropertyChangeAllowed());
459 if (replica_layer_.get() == layer)
461 if (replica_layer_.get()) {
462 DCHECK_EQ(this, replica_layer_->parent());
463 replica_layer_->RemoveFromParent();
465 replica_layer_ = layer;
466 if (replica_layer_.get()) {
467 DCHECK(!replica_layer_->parent());
468 replica_layer_->RemoveFromParent();
469 replica_layer_->SetParent(this);
471 SetNeedsFullTreeSync();
474 void Layer::SetFilters(const FilterOperations& filters) {
475 DCHECK(IsPropertyChangeAllowed());
476 if (filters_ == filters)
482 bool Layer::FilterIsAnimating() const {
483 return layer_animation_controller_->IsAnimatingProperty(Animation::Filter);
486 void Layer::SetBackgroundFilters(const FilterOperations& filters) {
487 DCHECK(IsPropertyChangeAllowed());
488 if (background_filters_ == filters)
490 background_filters_ = filters;
494 void Layer::SetOpacity(float opacity) {
495 DCHECK(IsPropertyChangeAllowed());
496 if (opacity_ == opacity)
502 bool Layer::OpacityIsAnimating() const {
503 return layer_animation_controller_->IsAnimatingProperty(Animation::Opacity);
506 bool Layer::OpacityCanAnimateOnImplThread() const {
510 void Layer::SetBlendMode(SkXfermode::Mode blend_mode) {
511 DCHECK(IsPropertyChangeAllowed());
512 if (blend_mode_ == blend_mode)
515 // Allowing only blend modes that are defined in the CSS Compositing standard:
516 // http://dev.w3.org/fxtf/compositing-1/#blending
517 switch (blend_mode) {
518 case SkXfermode::kSrcOver_Mode:
519 case SkXfermode::kScreen_Mode:
520 case SkXfermode::kOverlay_Mode:
521 case SkXfermode::kDarken_Mode:
522 case SkXfermode::kLighten_Mode:
523 case SkXfermode::kColorDodge_Mode:
524 case SkXfermode::kColorBurn_Mode:
525 case SkXfermode::kHardLight_Mode:
526 case SkXfermode::kSoftLight_Mode:
527 case SkXfermode::kDifference_Mode:
528 case SkXfermode::kExclusion_Mode:
529 case SkXfermode::kMultiply_Mode:
530 case SkXfermode::kHue_Mode:
531 case SkXfermode::kSaturation_Mode:
532 case SkXfermode::kColor_Mode:
533 case SkXfermode::kLuminosity_Mode:
534 // supported blend modes
536 case SkXfermode::kClear_Mode:
537 case SkXfermode::kSrc_Mode:
538 case SkXfermode::kDst_Mode:
539 case SkXfermode::kDstOver_Mode:
540 case SkXfermode::kSrcIn_Mode:
541 case SkXfermode::kDstIn_Mode:
542 case SkXfermode::kSrcOut_Mode:
543 case SkXfermode::kDstOut_Mode:
544 case SkXfermode::kSrcATop_Mode:
545 case SkXfermode::kDstATop_Mode:
546 case SkXfermode::kXor_Mode:
547 case SkXfermode::kPlus_Mode:
548 case SkXfermode::kModulate_Mode:
549 // Porter Duff Compositing Operators are not yet supported
550 // http://dev.w3.org/fxtf/compositing-1/#porterduffcompositingoperators
555 blend_mode_ = blend_mode;
559 void Layer::SetIsRootForIsolatedGroup(bool root) {
560 DCHECK(IsPropertyChangeAllowed());
561 if (is_root_for_isolated_group_ == root)
563 is_root_for_isolated_group_ = root;
567 void Layer::SetContentsOpaque(bool opaque) {
568 DCHECK(IsPropertyChangeAllowed());
569 if (contents_opaque_ == opaque)
571 contents_opaque_ = opaque;
575 void Layer::SetPosition(const gfx::PointF& position) {
576 DCHECK(IsPropertyChangeAllowed());
577 if (position_ == position)
579 position_ = position;
583 bool Layer::IsContainerForFixedPositionLayers() const {
584 if (!transform_.IsIdentityOrTranslation())
586 if (parent_ && !parent_->transform_.IsIdentityOrTranslation())
588 return is_container_for_fixed_position_layers_;
591 void Layer::SetTransform(const gfx::Transform& transform) {
592 DCHECK(IsPropertyChangeAllowed());
593 if (transform_ == transform)
595 transform_ = transform;
596 transform_is_invertible_ = transform.IsInvertible();
600 bool Layer::TransformIsAnimating() const {
601 return layer_animation_controller_->IsAnimatingProperty(Animation::Transform);
604 void Layer::SetScrollParent(Layer* parent) {
605 DCHECK(IsPropertyChangeAllowed());
606 if (scroll_parent_ == parent)
610 scroll_parent_->RemoveScrollChild(this);
612 scroll_parent_ = parent;
615 scroll_parent_->AddScrollChild(this);
620 void Layer::AddScrollChild(Layer* child) {
621 if (!scroll_children_)
622 scroll_children_.reset(new std::set<Layer*>);
623 scroll_children_->insert(child);
627 void Layer::RemoveScrollChild(Layer* child) {
628 scroll_children_->erase(child);
629 if (scroll_children_->empty())
630 scroll_children_.reset();
634 void Layer::SetClipParent(Layer* ancestor) {
635 DCHECK(IsPropertyChangeAllowed());
636 if (clip_parent_ == ancestor)
640 clip_parent_->RemoveClipChild(this);
642 clip_parent_ = ancestor;
645 clip_parent_->AddClipChild(this);
650 void Layer::AddClipChild(Layer* child) {
652 clip_children_.reset(new std::set<Layer*>);
653 clip_children_->insert(child);
657 void Layer::RemoveClipChild(Layer* child) {
658 clip_children_->erase(child);
659 if (clip_children_->empty())
660 clip_children_.reset();
664 void Layer::SetScrollOffset(gfx::Vector2d scroll_offset) {
665 DCHECK(IsPropertyChangeAllowed());
667 if (scroll_offset_ == scroll_offset)
669 scroll_offset_ = scroll_offset;
673 void Layer::SetScrollOffsetFromImplSide(const gfx::Vector2d& scroll_offset) {
674 DCHECK(IsPropertyChangeAllowed());
675 // This function only gets called during a BeginMainFrame, so there
676 // is no need to call SetNeedsUpdate here.
677 DCHECK(layer_tree_host_ && layer_tree_host_->CommitRequested());
678 if (scroll_offset_ == scroll_offset)
680 scroll_offset_ = scroll_offset;
681 SetNeedsPushProperties();
682 if (!did_scroll_callback_.is_null())
683 did_scroll_callback_.Run();
684 // The callback could potentially change the layer structure:
685 // "this" may have been destroyed during the process.
688 void Layer::SetScrollClipLayerId(int clip_layer_id) {
689 DCHECK(IsPropertyChangeAllowed());
690 if (scroll_clip_layer_id_ == clip_layer_id)
692 scroll_clip_layer_id_ = clip_layer_id;
696 void Layer::SetUserScrollable(bool horizontal, bool vertical) {
697 DCHECK(IsPropertyChangeAllowed());
698 if (user_scrollable_horizontal_ == horizontal &&
699 user_scrollable_vertical_ == vertical)
701 user_scrollable_horizontal_ = horizontal;
702 user_scrollable_vertical_ = vertical;
706 void Layer::SetShouldScrollOnMainThread(bool should_scroll_on_main_thread) {
707 DCHECK(IsPropertyChangeAllowed());
708 if (should_scroll_on_main_thread_ == should_scroll_on_main_thread)
710 should_scroll_on_main_thread_ = should_scroll_on_main_thread;
714 void Layer::SetHaveWheelEventHandlers(bool have_wheel_event_handlers) {
715 DCHECK(IsPropertyChangeAllowed());
716 if (have_wheel_event_handlers_ == have_wheel_event_handlers)
718 have_wheel_event_handlers_ = have_wheel_event_handlers;
722 void Layer::SetHaveScrollEventHandlers(bool have_scroll_event_handlers) {
723 DCHECK(IsPropertyChangeAllowed());
724 if (have_scroll_event_handlers_ == have_scroll_event_handlers)
726 have_scroll_event_handlers_ = have_scroll_event_handlers;
730 void Layer::SetNonFastScrollableRegion(const Region& region) {
731 DCHECK(IsPropertyChangeAllowed());
732 if (non_fast_scrollable_region_ == region)
734 non_fast_scrollable_region_ = region;
738 void Layer::SetTouchEventHandlerRegion(const Region& region) {
739 DCHECK(IsPropertyChangeAllowed());
740 if (touch_event_handler_region_ == region)
742 touch_event_handler_region_ = region;
746 void Layer::SetDrawCheckerboardForMissingTiles(bool checkerboard) {
747 DCHECK(IsPropertyChangeAllowed());
748 if (draw_checkerboard_for_missing_tiles_ == checkerboard)
750 draw_checkerboard_for_missing_tiles_ = checkerboard;
754 void Layer::SetForceRenderSurface(bool force) {
755 DCHECK(IsPropertyChangeAllowed());
756 if (force_render_surface_ == force)
758 force_render_surface_ = force;
762 void Layer::SetDoubleSided(bool double_sided) {
763 DCHECK(IsPropertyChangeAllowed());
764 if (double_sided_ == double_sided)
766 double_sided_ = double_sided;
770 void Layer::SetIs3dSorted(bool sorted) {
771 DCHECK(IsPropertyChangeAllowed());
772 if (is_3d_sorted_ == sorted)
774 is_3d_sorted_ = sorted;
778 void Layer::SetShouldFlattenTransform(bool should_flatten) {
779 DCHECK(IsPropertyChangeAllowed());
780 if (should_flatten_transform_ == should_flatten)
782 should_flatten_transform_ = should_flatten;
786 void Layer::SetIsDrawable(bool is_drawable) {
787 DCHECK(IsPropertyChangeAllowed());
788 if (is_drawable_ == is_drawable)
791 is_drawable_ = is_drawable;
795 void Layer::SetHideLayerAndSubtree(bool hide) {
796 DCHECK(IsPropertyChangeAllowed());
797 if (hide_layer_and_subtree_ == hide)
800 hide_layer_and_subtree_ = hide;
804 void Layer::SetNeedsDisplayRect(const gfx::RectF& dirty_rect) {
805 if (dirty_rect.IsEmpty())
808 SetNeedsPushProperties();
809 update_rect_.Union(dirty_rect);
815 bool Layer::DescendantIsFixedToContainerLayer() const {
816 for (size_t i = 0; i < children_.size(); ++i) {
817 if (children_[i]->position_constraint_.is_fixed_position() ||
818 children_[i]->DescendantIsFixedToContainerLayer())
824 void Layer::SetIsContainerForFixedPositionLayers(bool container) {
825 if (is_container_for_fixed_position_layers_ == container)
827 is_container_for_fixed_position_layers_ = container;
829 if (layer_tree_host_ && layer_tree_host_->CommitRequested())
832 // Only request a commit if we have a fixed positioned descendant.
833 if (DescendantIsFixedToContainerLayer())
837 void Layer::SetPositionConstraint(const LayerPositionConstraint& constraint) {
838 DCHECK(IsPropertyChangeAllowed());
839 if (position_constraint_ == constraint)
841 position_constraint_ = constraint;
845 static void RunCopyCallbackOnMainThread(scoped_ptr<CopyOutputRequest> request,
846 scoped_ptr<CopyOutputResult> result) {
847 request->SendResult(result.Pass());
850 static void PostCopyCallbackToMainThread(
851 scoped_refptr<base::SingleThreadTaskRunner> main_thread_task_runner,
852 scoped_ptr<CopyOutputRequest> request,
853 scoped_ptr<CopyOutputResult> result) {
854 main_thread_task_runner->PostTask(FROM_HERE,
855 base::Bind(&RunCopyCallbackOnMainThread,
856 base::Passed(&request),
857 base::Passed(&result)));
860 void Layer::PushPropertiesTo(LayerImpl* layer) {
861 DCHECK(layer_tree_host_);
863 // If we did not SavePaintProperties() for the layer this frame, then push the
864 // real property values, not the paint property values.
865 bool use_paint_properties = paint_properties_.source_frame_number ==
866 layer_tree_host_->source_frame_number();
868 layer->SetAnchorPoint(anchor_point_);
869 layer->SetAnchorPointZ(anchor_point_z_);
870 layer->SetBackgroundColor(background_color_);
871 layer->SetBounds(use_paint_properties ? paint_properties_.bounds
873 layer->SetContentBounds(content_bounds());
874 layer->SetContentsScale(contents_scale_x(), contents_scale_y());
877 TRACE_EVENT_CATEGORY_GROUP_ENABLED(TRACE_DISABLED_BY_DEFAULT("cc.debug"),
880 layer->SetDebugInfo(TakeDebugInfo());
882 layer->SetDoubleSided(double_sided_);
883 layer->SetDrawCheckerboardForMissingTiles(
884 draw_checkerboard_for_missing_tiles_);
885 layer->SetForceRenderSurface(force_render_surface_);
886 layer->SetDrawsContent(DrawsContent());
887 layer->SetHideLayerAndSubtree(hide_layer_and_subtree_);
888 if (!layer->FilterIsAnimatingOnImplOnly() && !FilterIsAnimating())
889 layer->SetFilters(filters_);
890 DCHECK(!(FilterIsAnimating() && layer->FilterIsAnimatingOnImplOnly()));
891 layer->SetBackgroundFilters(background_filters());
892 layer->SetMasksToBounds(masks_to_bounds_);
893 layer->SetShouldScrollOnMainThread(should_scroll_on_main_thread_);
894 layer->SetHaveWheelEventHandlers(have_wheel_event_handlers_);
895 layer->SetHaveScrollEventHandlers(have_scroll_event_handlers_);
896 layer->SetNonFastScrollableRegion(non_fast_scrollable_region_);
897 layer->SetTouchEventHandlerRegion(touch_event_handler_region_);
898 layer->SetContentsOpaque(contents_opaque_);
899 if (!layer->OpacityIsAnimatingOnImplOnly() && !OpacityIsAnimating())
900 layer->SetOpacity(opacity_);
901 DCHECK(!(OpacityIsAnimating() && layer->OpacityIsAnimatingOnImplOnly()));
902 layer->SetBlendMode(blend_mode_);
903 layer->SetIsRootForIsolatedGroup(is_root_for_isolated_group_);
904 layer->SetPosition(position_);
905 layer->SetIsContainerForFixedPositionLayers(
906 IsContainerForFixedPositionLayers());
907 layer->SetPositionConstraint(position_constraint_);
908 layer->SetShouldFlattenTransform(should_flatten_transform_);
909 layer->SetIs3dSorted(is_3d_sorted_);
910 layer->SetUseParentBackfaceVisibility(use_parent_backface_visibility_);
911 if (!layer->TransformIsAnimatingOnImplOnly() && !TransformIsAnimating())
912 layer->SetTransformAndInvertibility(transform_, transform_is_invertible_);
913 DCHECK(!(TransformIsAnimating() && layer->TransformIsAnimatingOnImplOnly()));
915 layer->SetScrollClipLayer(scroll_clip_layer_id_);
916 layer->set_user_scrollable_horizontal(user_scrollable_horizontal_);
917 layer->set_user_scrollable_vertical(user_scrollable_vertical_);
919 LayerImpl* scroll_parent = NULL;
920 if (scroll_parent_) {
921 scroll_parent = layer->layer_tree_impl()->LayerById(scroll_parent_->id());
922 DCHECK(scroll_parent);
925 layer->SetScrollParent(scroll_parent);
926 if (scroll_children_) {
927 std::set<LayerImpl*>* scroll_children = new std::set<LayerImpl*>;
928 for (std::set<Layer*>::iterator it = scroll_children_->begin();
929 it != scroll_children_->end();
931 DCHECK_EQ((*it)->scroll_parent(), this);
932 LayerImpl* scroll_child =
933 layer->layer_tree_impl()->LayerById((*it)->id());
934 DCHECK(scroll_child);
935 scroll_children->insert(scroll_child);
937 layer->SetScrollChildren(scroll_children);
939 layer->SetScrollChildren(NULL);
942 LayerImpl* clip_parent = NULL;
945 layer->layer_tree_impl()->LayerById(clip_parent_->id());
949 layer->SetClipParent(clip_parent);
950 if (clip_children_) {
951 std::set<LayerImpl*>* clip_children = new std::set<LayerImpl*>;
952 for (std::set<Layer*>::iterator it = clip_children_->begin();
953 it != clip_children_->end(); ++it) {
954 DCHECK_EQ((*it)->clip_parent(), this);
955 LayerImpl* clip_child = layer->layer_tree_impl()->LayerById((*it)->id());
957 clip_children->insert(clip_child);
959 layer->SetClipChildren(clip_children);
961 layer->SetClipChildren(NULL);
964 // Adjust the scroll delta to be just the scrolls that have happened since
965 // the BeginMainFrame was sent. This happens for impl-side painting
966 // in LayerImpl::ApplyScrollDeltasSinceBeginMainFrame in a separate tree walk.
967 if (layer->layer_tree_impl()->settings().impl_side_painting) {
968 layer->SetScrollOffset(scroll_offset_);
970 layer->SetScrollOffsetAndDelta(
971 scroll_offset_, layer->ScrollDelta() - layer->sent_scroll_delta());
972 layer->SetSentScrollDelta(gfx::Vector2d());
975 // Wrap the copy_requests_ in a PostTask to the main thread.
976 ScopedPtrVector<CopyOutputRequest> main_thread_copy_requests;
977 for (ScopedPtrVector<CopyOutputRequest>::iterator it = copy_requests_.begin();
978 it != copy_requests_.end();
980 scoped_refptr<base::SingleThreadTaskRunner> main_thread_task_runner =
981 layer_tree_host()->proxy()->MainThreadTaskRunner();
982 scoped_ptr<CopyOutputRequest> original_request = copy_requests_.take(it);
983 const CopyOutputRequest& original_request_ref = *original_request;
984 scoped_ptr<CopyOutputRequest> main_thread_request =
985 CopyOutputRequest::CreateRelayRequest(
986 original_request_ref,
987 base::Bind(&PostCopyCallbackToMainThread,
988 main_thread_task_runner,
989 base::Passed(&original_request)));
990 main_thread_copy_requests.push_back(main_thread_request.Pass());
992 copy_requests_.clear();
993 layer->PassCopyRequests(&main_thread_copy_requests);
995 // If the main thread commits multiple times before the impl thread actually
996 // draws, then damage tracking will become incorrect if we simply clobber the
997 // update_rect here. The LayerImpl's update_rect needs to accumulate (i.e.
998 // union) any update changes that have occurred on the main thread.
999 update_rect_.Union(layer->update_rect());
1000 layer->SetUpdateRect(update_rect_);
1002 layer->SetStackingOrderChanged(stacking_order_changed_);
1004 layer_animation_controller_->PushAnimationUpdatesTo(
1005 layer->layer_animation_controller());
1007 // Reset any state that should be cleared for the next update.
1008 stacking_order_changed_ = false;
1009 update_rect_ = gfx::RectF();
1011 needs_push_properties_ = false;
1012 num_dependents_need_push_properties_ = 0;
1015 scoped_ptr<LayerImpl> Layer::CreateLayerImpl(LayerTreeImpl* tree_impl) {
1016 return LayerImpl::Create(tree_impl, layer_id_);
1019 bool Layer::DrawsContent() const {
1020 return is_drawable_;
1023 void Layer::SavePaintProperties() {
1024 DCHECK(layer_tree_host_);
1026 // TODO(reveman): Save all layer properties that we depend on not
1027 // changing until PushProperties() has been called. crbug.com/231016
1028 paint_properties_.bounds = bounds_;
1029 paint_properties_.source_frame_number =
1030 layer_tree_host_->source_frame_number();
1033 bool Layer::Update(ResourceUpdateQueue* queue,
1034 const OcclusionTracker<Layer>* occlusion) {
1035 DCHECK(layer_tree_host_);
1036 DCHECK_EQ(layer_tree_host_->source_frame_number(),
1037 paint_properties_.source_frame_number) <<
1038 "SavePaintProperties must be called for any layer that is painted.";
1042 bool Layer::NeedMoreUpdates() {
1046 bool Layer::IsSuitableForGpuRasterization() const {
1050 scoped_refptr<base::debug::ConvertableToTraceFormat> Layer::TakeDebugInfo() {
1052 return client_->TakeDebugInfo();
1057 void Layer::CreateRenderSurface() {
1058 DCHECK(!draw_properties_.render_surface);
1059 draw_properties_.render_surface = make_scoped_ptr(new RenderSurface(this));
1060 draw_properties_.render_target = this;
1063 void Layer::ClearRenderSurface() {
1064 draw_properties_.render_surface.reset();
1067 void Layer::ClearRenderSurfaceLayerList() {
1068 if (draw_properties_.render_surface)
1069 draw_properties_.render_surface->layer_list().clear();
1072 gfx::Vector2dF Layer::ScrollOffsetForAnimation() const {
1073 return TotalScrollOffset();
1076 // On<Property>Animated is called due to an ongoing accelerated animation.
1077 // Since this animation is also being run on the compositor thread, there
1078 // is no need to request a commit to push this value over, so the value is
1079 // set directly rather than by calling Set<Property>.
1080 void Layer::OnFilterAnimated(const FilterOperations& filters) {
1084 void Layer::OnOpacityAnimated(float opacity) {
1088 void Layer::OnTransformAnimated(const gfx::Transform& transform) {
1089 if (transform_ == transform)
1091 transform_ = transform;
1092 transform_is_invertible_ = transform.IsInvertible();
1095 void Layer::OnScrollOffsetAnimated(const gfx::Vector2dF& scroll_offset) {
1096 // Do nothing. Scroll deltas will be sent from the compositor thread back
1097 // to the main thread in the same manner as during non-animated
1098 // compositor-driven scrolling.
1101 void Layer::OnAnimationWaitingForDeletion() {
1102 // Animations are only deleted during PushProperties.
1103 SetNeedsPushProperties();
1106 bool Layer::IsActive() const {
1110 bool Layer::AddAnimation(scoped_ptr <Animation> animation) {
1111 if (!layer_animation_controller_->animation_registrar())
1114 UMA_HISTOGRAM_BOOLEAN("Renderer.AnimationAddedToOrphanLayer",
1116 layer_animation_controller_->AddAnimation(animation.Pass());
1121 void Layer::PauseAnimation(int animation_id, double time_offset) {
1122 layer_animation_controller_->PauseAnimation(animation_id, time_offset);
1126 void Layer::RemoveAnimation(int animation_id) {
1127 layer_animation_controller_->RemoveAnimation(animation_id);
1131 void Layer::SetLayerAnimationControllerForTest(
1132 scoped_refptr<LayerAnimationController> controller) {
1133 layer_animation_controller_->RemoveValueObserver(this);
1134 layer_animation_controller_ = controller;
1135 layer_animation_controller_->AddValueObserver(this);
1139 bool Layer::HasActiveAnimation() const {
1140 return layer_animation_controller_->HasActiveAnimation();
1143 void Layer::AddLayerAnimationEventObserver(
1144 LayerAnimationEventObserver* animation_observer) {
1145 layer_animation_controller_->AddEventObserver(animation_observer);
1148 void Layer::RemoveLayerAnimationEventObserver(
1149 LayerAnimationEventObserver* animation_observer) {
1150 layer_animation_controller_->RemoveEventObserver(animation_observer);
1153 Region Layer::VisibleContentOpaqueRegion() const {
1154 if (contents_opaque())
1155 return visible_content_rect();
1159 ScrollbarLayerInterface* Layer::ToScrollbarLayer() {
1163 RenderingStatsInstrumentation* Layer::rendering_stats_instrumentation() const {
1164 return layer_tree_host_->rendering_stats_instrumentation();
1167 bool Layer::SupportsLCDText() const {
1171 void Layer::RemoveFromScrollTree() {
1172 if (scroll_children_.get()) {
1173 for (std::set<Layer*>::iterator it = scroll_children_->begin();
1174 it != scroll_children_->end(); ++it)
1175 (*it)->scroll_parent_ = NULL;
1179 scroll_parent_->RemoveScrollChild(this);
1181 scroll_parent_ = NULL;
1184 void Layer::RemoveFromClipTree() {
1185 if (clip_children_.get()) {
1186 for (std::set<Layer*>::iterator it = clip_children_->begin();
1187 it != clip_children_->end(); ++it)
1188 (*it)->clip_parent_ = NULL;
1192 clip_parent_->RemoveClipChild(this);
1194 clip_parent_ = NULL;
1197 void Layer::RunMicroBenchmark(MicroBenchmark* benchmark) {
1198 benchmark->RunOnLayer(this);