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/atomic_sequence_num.h"
10 #include "base/debug/trace_event.h"
11 #include "base/location.h"
12 #include "base/metrics/histogram.h"
13 #include "base/single_thread_task_runner.h"
14 #include "base/time/time.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/layers/layer_client.h"
21 #include "cc/layers/layer_impl.h"
22 #include "cc/layers/scrollbar_layer_interface.h"
23 #include "cc/output/copy_output_request.h"
24 #include "cc/output/copy_output_result.h"
25 #include "cc/trees/layer_tree_host.h"
26 #include "cc/trees/layer_tree_impl.h"
27 #include "third_party/skia/include/core/SkImageFilter.h"
28 #include "ui/gfx/geometry/vector2d_conversions.h"
29 #include "ui/gfx/rect_conversions.h"
33 base::StaticAtomicSequenceNumber g_next_layer_id;
35 scoped_refptr<Layer> Layer::Create() {
36 return make_scoped_refptr(new Layer());
40 : needs_push_properties_(false),
41 num_dependents_need_push_properties_(false),
42 stacking_order_changed_(false),
43 // Layer IDs start from 1.
44 layer_id_(g_next_layer_id.GetNext() + 1),
45 ignore_set_needs_commit_(false),
46 sorting_context_id_(0),
48 layer_tree_host_(NULL),
49 scroll_clip_layer_id_(INVALID_ID),
50 num_descendants_that_draw_content_(0),
51 should_scroll_on_main_thread_(false),
52 have_wheel_event_handlers_(false),
53 have_scroll_event_handlers_(false),
54 user_scrollable_horizontal_(true),
55 user_scrollable_vertical_(true),
56 is_root_for_isolated_group_(false),
57 is_container_for_fixed_position_layers_(false),
59 draws_content_(false),
60 hide_layer_and_subtree_(false),
61 masks_to_bounds_(false),
62 contents_opaque_(false),
64 should_flatten_transform_(true),
65 use_parent_backface_visibility_(false),
66 draw_checkerboard_for_missing_tiles_(false),
67 force_render_surface_(false),
68 transform_is_invertible_(true),
71 blend_mode_(SkXfermode::kSrcOver_Mode),
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 RemoveFromScrollTree();
96 // Remove the parent reference from all children and dependents.
98 if (mask_layer_.get()) {
99 DCHECK_EQ(this, mask_layer_->parent());
100 mask_layer_->RemoveFromParent();
102 if (replica_layer_.get()) {
103 DCHECK_EQ(this, replica_layer_->parent());
104 replica_layer_->RemoveFromParent();
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 AddDrawableDescendants(child->NumDescendantsThatDrawContent() +
251 (child->DrawsContent() ? 1 : 0));
252 child->SetParent(this);
253 child->stacking_order_changed_ = true;
255 index = std::min(index, children_.size());
256 children_.insert(children_.begin() + index, child);
257 SetNeedsFullTreeSync();
260 void Layer::RemoveFromParent() {
261 DCHECK(IsPropertyChangeAllowed());
263 parent_->RemoveChildOrDependent(this);
266 void Layer::RemoveChildOrDependent(Layer* child) {
267 if (mask_layer_.get() == child) {
268 mask_layer_->SetParent(NULL);
270 SetNeedsFullTreeSync();
273 if (replica_layer_.get() == child) {
274 replica_layer_->SetParent(NULL);
275 replica_layer_ = NULL;
276 SetNeedsFullTreeSync();
280 for (LayerList::iterator iter = children_.begin();
281 iter != children_.end();
283 if (iter->get() != child)
286 child->SetParent(NULL);
287 AddDrawableDescendants(-child->NumDescendantsThatDrawContent() -
288 (child->DrawsContent() ? 1 : 0));
289 children_.erase(iter);
290 SetNeedsFullTreeSync();
295 void Layer::ReplaceChild(Layer* reference, scoped_refptr<Layer> new_layer) {
297 DCHECK_EQ(reference->parent(), this);
298 DCHECK(IsPropertyChangeAllowed());
300 if (reference == new_layer.get())
303 int reference_index = IndexOfChild(reference);
304 if (reference_index == -1) {
309 reference->RemoveFromParent();
311 if (new_layer.get()) {
312 new_layer->RemoveFromParent();
313 InsertChild(new_layer, reference_index);
317 int Layer::IndexOfChild(const Layer* reference) {
318 for (size_t i = 0; i < children_.size(); ++i) {
319 if (children_[i].get() == reference)
325 void Layer::SetBounds(const gfx::Size& size) {
326 DCHECK(IsPropertyChangeAllowed());
327 if (bounds() == size)
334 Layer* Layer::RootLayer() {
336 while (layer->parent())
337 layer = layer->parent();
341 void Layer::RemoveAllChildren() {
342 DCHECK(IsPropertyChangeAllowed());
343 while (children_.size()) {
344 Layer* layer = children_[0].get();
345 DCHECK_EQ(this, layer->parent());
346 layer->RemoveFromParent();
350 void Layer::SetChildren(const LayerList& children) {
351 DCHECK(IsPropertyChangeAllowed());
352 if (children == children_)
356 for (size_t i = 0; i < children.size(); ++i)
357 AddChild(children[i]);
360 bool Layer::HasAncestor(const Layer* ancestor) const {
361 for (const Layer* layer = parent(); layer; layer = layer->parent()) {
362 if (layer == ancestor)
368 void Layer::RequestCopyOfOutput(
369 scoped_ptr<CopyOutputRequest> request) {
370 DCHECK(IsPropertyChangeAllowed());
371 if (request->IsEmpty())
373 copy_requests_.push_back(request.Pass());
377 void Layer::SetBackgroundColor(SkColor background_color) {
378 DCHECK(IsPropertyChangeAllowed());
379 if (background_color_ == background_color)
381 background_color_ = background_color;
385 SkColor Layer::SafeOpaqueBackgroundColor() const {
386 SkColor color = background_color();
387 if (SkColorGetA(color) == 255 && !contents_opaque()) {
388 color = SK_ColorTRANSPARENT;
389 } else if (SkColorGetA(color) != 255 && contents_opaque()) {
390 for (const Layer* layer = parent(); layer;
391 layer = layer->parent()) {
392 color = layer->background_color();
393 if (SkColorGetA(color) == 255)
396 if (SkColorGetA(color) != 255)
397 color = layer_tree_host_->background_color();
398 if (SkColorGetA(color) != 255)
399 color = SkColorSetA(color, 255);
404 void Layer::CalculateContentsScale(float ideal_contents_scale,
405 float* contents_scale_x,
406 float* contents_scale_y,
407 gfx::Size* content_bounds) {
408 DCHECK(layer_tree_host_);
410 *contents_scale_x = 1;
411 *contents_scale_y = 1;
412 *content_bounds = bounds();
415 void Layer::SetMasksToBounds(bool masks_to_bounds) {
416 DCHECK(IsPropertyChangeAllowed());
417 if (masks_to_bounds_ == masks_to_bounds)
419 masks_to_bounds_ = masks_to_bounds;
423 void Layer::SetMaskLayer(Layer* mask_layer) {
424 DCHECK(IsPropertyChangeAllowed());
425 if (mask_layer_.get() == mask_layer)
427 if (mask_layer_.get()) {
428 DCHECK_EQ(this, mask_layer_->parent());
429 mask_layer_->RemoveFromParent();
431 mask_layer_ = mask_layer;
432 if (mask_layer_.get()) {
433 DCHECK(!mask_layer_->parent());
434 mask_layer_->RemoveFromParent();
435 mask_layer_->SetParent(this);
436 mask_layer_->SetIsMask(true);
438 SetNeedsFullTreeSync();
441 void Layer::SetReplicaLayer(Layer* layer) {
442 DCHECK(IsPropertyChangeAllowed());
443 if (replica_layer_.get() == layer)
445 if (replica_layer_.get()) {
446 DCHECK_EQ(this, replica_layer_->parent());
447 replica_layer_->RemoveFromParent();
449 replica_layer_ = layer;
450 if (replica_layer_.get()) {
451 DCHECK(!replica_layer_->parent());
452 replica_layer_->RemoveFromParent();
453 replica_layer_->SetParent(this);
455 SetNeedsFullTreeSync();
458 void Layer::SetFilters(const FilterOperations& filters) {
459 DCHECK(IsPropertyChangeAllowed());
460 if (filters_ == filters)
466 bool Layer::FilterIsAnimating() const {
467 return layer_animation_controller_->IsAnimatingProperty(Animation::Filter);
470 void Layer::SetBackgroundFilters(const FilterOperations& filters) {
471 DCHECK(IsPropertyChangeAllowed());
472 if (background_filters_ == filters)
474 background_filters_ = filters;
478 void Layer::SetOpacity(float opacity) {
479 DCHECK(IsPropertyChangeAllowed());
480 if (opacity_ == opacity)
486 bool Layer::OpacityIsAnimating() const {
487 return layer_animation_controller_->IsAnimatingProperty(Animation::Opacity);
490 bool Layer::OpacityCanAnimateOnImplThread() const {
494 void Layer::SetBlendMode(SkXfermode::Mode blend_mode) {
495 DCHECK(IsPropertyChangeAllowed());
496 if (blend_mode_ == blend_mode)
499 // Allowing only blend modes that are defined in the CSS Compositing standard:
500 // http://dev.w3.org/fxtf/compositing-1/#blending
501 switch (blend_mode) {
502 case SkXfermode::kSrcOver_Mode:
503 case SkXfermode::kScreen_Mode:
504 case SkXfermode::kOverlay_Mode:
505 case SkXfermode::kDarken_Mode:
506 case SkXfermode::kLighten_Mode:
507 case SkXfermode::kColorDodge_Mode:
508 case SkXfermode::kColorBurn_Mode:
509 case SkXfermode::kHardLight_Mode:
510 case SkXfermode::kSoftLight_Mode:
511 case SkXfermode::kDifference_Mode:
512 case SkXfermode::kExclusion_Mode:
513 case SkXfermode::kMultiply_Mode:
514 case SkXfermode::kHue_Mode:
515 case SkXfermode::kSaturation_Mode:
516 case SkXfermode::kColor_Mode:
517 case SkXfermode::kLuminosity_Mode:
518 // supported blend modes
520 case SkXfermode::kClear_Mode:
521 case SkXfermode::kSrc_Mode:
522 case SkXfermode::kDst_Mode:
523 case SkXfermode::kDstOver_Mode:
524 case SkXfermode::kSrcIn_Mode:
525 case SkXfermode::kDstIn_Mode:
526 case SkXfermode::kSrcOut_Mode:
527 case SkXfermode::kDstOut_Mode:
528 case SkXfermode::kSrcATop_Mode:
529 case SkXfermode::kDstATop_Mode:
530 case SkXfermode::kXor_Mode:
531 case SkXfermode::kPlus_Mode:
532 case SkXfermode::kModulate_Mode:
533 // Porter Duff Compositing Operators are not yet supported
534 // http://dev.w3.org/fxtf/compositing-1/#porterduffcompositingoperators
539 blend_mode_ = blend_mode;
543 void Layer::SetIsRootForIsolatedGroup(bool root) {
544 DCHECK(IsPropertyChangeAllowed());
545 if (is_root_for_isolated_group_ == root)
547 is_root_for_isolated_group_ = root;
551 void Layer::SetContentsOpaque(bool opaque) {
552 DCHECK(IsPropertyChangeAllowed());
553 if (contents_opaque_ == opaque)
555 contents_opaque_ = opaque;
559 void Layer::SetPosition(const gfx::PointF& position) {
560 DCHECK(IsPropertyChangeAllowed());
561 if (position_ == position)
563 position_ = position;
567 bool Layer::IsContainerForFixedPositionLayers() const {
568 if (!transform_.IsIdentityOrTranslation())
570 if (parent_ && !parent_->transform_.IsIdentityOrTranslation())
572 return is_container_for_fixed_position_layers_;
575 void Layer::SetTransform(const gfx::Transform& transform) {
576 DCHECK(IsPropertyChangeAllowed());
577 if (transform_ == transform)
579 transform_ = transform;
580 transform_is_invertible_ = transform.IsInvertible();
584 void Layer::SetTransformOrigin(const gfx::Point3F& transform_origin) {
585 DCHECK(IsPropertyChangeAllowed());
586 if (transform_origin_ == transform_origin)
588 transform_origin_ = transform_origin;
592 bool Layer::TransformIsAnimating() const {
593 return layer_animation_controller_->IsAnimatingProperty(Animation::Transform);
596 void Layer::SetScrollParent(Layer* parent) {
597 DCHECK(IsPropertyChangeAllowed());
598 if (scroll_parent_ == parent)
602 scroll_parent_->RemoveScrollChild(this);
604 scroll_parent_ = parent;
607 scroll_parent_->AddScrollChild(this);
612 void Layer::AddScrollChild(Layer* child) {
613 if (!scroll_children_)
614 scroll_children_.reset(new std::set<Layer*>);
615 scroll_children_->insert(child);
619 void Layer::RemoveScrollChild(Layer* child) {
620 scroll_children_->erase(child);
621 if (scroll_children_->empty())
622 scroll_children_.reset();
626 void Layer::SetClipParent(Layer* ancestor) {
627 DCHECK(IsPropertyChangeAllowed());
628 if (clip_parent_ == ancestor)
632 clip_parent_->RemoveClipChild(this);
634 clip_parent_ = ancestor;
637 clip_parent_->AddClipChild(this);
642 void Layer::AddClipChild(Layer* child) {
644 clip_children_.reset(new std::set<Layer*>);
645 clip_children_->insert(child);
649 void Layer::RemoveClipChild(Layer* child) {
650 clip_children_->erase(child);
651 if (clip_children_->empty())
652 clip_children_.reset();
656 void Layer::SetScrollOffset(gfx::Vector2d scroll_offset) {
657 DCHECK(IsPropertyChangeAllowed());
659 if (scroll_offset_ == scroll_offset)
661 scroll_offset_ = scroll_offset;
665 void Layer::SetScrollOffsetFromImplSide(const gfx::Vector2d& scroll_offset) {
666 DCHECK(IsPropertyChangeAllowed());
667 // This function only gets called during a BeginMainFrame, so there
668 // is no need to call SetNeedsUpdate here.
669 DCHECK(layer_tree_host_ && layer_tree_host_->CommitRequested());
670 if (scroll_offset_ == scroll_offset)
672 scroll_offset_ = scroll_offset;
673 SetNeedsPushProperties();
674 if (!did_scroll_callback_.is_null())
675 did_scroll_callback_.Run();
676 // The callback could potentially change the layer structure:
677 // "this" may have been destroyed during the process.
680 void Layer::SetScrollClipLayerId(int clip_layer_id) {
681 DCHECK(IsPropertyChangeAllowed());
682 if (scroll_clip_layer_id_ == clip_layer_id)
684 scroll_clip_layer_id_ = clip_layer_id;
688 void Layer::SetUserScrollable(bool horizontal, bool vertical) {
689 DCHECK(IsPropertyChangeAllowed());
690 if (user_scrollable_horizontal_ == horizontal &&
691 user_scrollable_vertical_ == vertical)
693 user_scrollable_horizontal_ = horizontal;
694 user_scrollable_vertical_ = vertical;
698 void Layer::SetShouldScrollOnMainThread(bool should_scroll_on_main_thread) {
699 DCHECK(IsPropertyChangeAllowed());
700 if (should_scroll_on_main_thread_ == should_scroll_on_main_thread)
702 should_scroll_on_main_thread_ = should_scroll_on_main_thread;
706 void Layer::SetHaveWheelEventHandlers(bool have_wheel_event_handlers) {
707 DCHECK(IsPropertyChangeAllowed());
708 if (have_wheel_event_handlers_ == have_wheel_event_handlers)
710 have_wheel_event_handlers_ = have_wheel_event_handlers;
714 void Layer::SetHaveScrollEventHandlers(bool have_scroll_event_handlers) {
715 DCHECK(IsPropertyChangeAllowed());
716 if (have_scroll_event_handlers_ == have_scroll_event_handlers)
718 have_scroll_event_handlers_ = have_scroll_event_handlers;
722 void Layer::SetNonFastScrollableRegion(const Region& region) {
723 DCHECK(IsPropertyChangeAllowed());
724 if (non_fast_scrollable_region_ == region)
726 non_fast_scrollable_region_ = region;
730 void Layer::SetTouchEventHandlerRegion(const Region& region) {
731 DCHECK(IsPropertyChangeAllowed());
732 if (touch_event_handler_region_ == region)
734 touch_event_handler_region_ = region;
738 void Layer::SetDrawCheckerboardForMissingTiles(bool checkerboard) {
739 DCHECK(IsPropertyChangeAllowed());
740 if (draw_checkerboard_for_missing_tiles_ == checkerboard)
742 draw_checkerboard_for_missing_tiles_ = checkerboard;
746 void Layer::SetForceRenderSurface(bool force) {
747 DCHECK(IsPropertyChangeAllowed());
748 if (force_render_surface_ == force)
750 force_render_surface_ = force;
754 void Layer::SetDoubleSided(bool double_sided) {
755 DCHECK(IsPropertyChangeAllowed());
756 if (double_sided_ == double_sided)
758 double_sided_ = double_sided;
762 void Layer::Set3dSortingContextId(int id) {
763 DCHECK(IsPropertyChangeAllowed());
764 if (id == sorting_context_id_)
766 sorting_context_id_ = id;
770 void Layer::SetShouldFlattenTransform(bool should_flatten) {
771 DCHECK(IsPropertyChangeAllowed());
772 if (should_flatten_transform_ == should_flatten)
774 should_flatten_transform_ = should_flatten;
778 void Layer::SetIsDrawable(bool is_drawable) {
779 DCHECK(IsPropertyChangeAllowed());
780 if (is_drawable_ == is_drawable)
783 is_drawable_ = is_drawable;
784 UpdateDrawsContent(HasDrawableContent());
787 void Layer::SetHideLayerAndSubtree(bool hide) {
788 DCHECK(IsPropertyChangeAllowed());
789 if (hide_layer_and_subtree_ == hide)
792 hide_layer_and_subtree_ = hide;
796 void Layer::SetNeedsDisplayRect(const gfx::RectF& dirty_rect) {
797 if (dirty_rect.IsEmpty())
800 SetNeedsPushProperties();
801 update_rect_.Union(dirty_rect);
807 bool Layer::DescendantIsFixedToContainerLayer() const {
808 for (size_t i = 0; i < children_.size(); ++i) {
809 if (children_[i]->position_constraint_.is_fixed_position() ||
810 children_[i]->DescendantIsFixedToContainerLayer())
816 void Layer::SetIsContainerForFixedPositionLayers(bool container) {
817 if (is_container_for_fixed_position_layers_ == container)
819 is_container_for_fixed_position_layers_ = container;
821 if (layer_tree_host_ && layer_tree_host_->CommitRequested())
824 // Only request a commit if we have a fixed positioned descendant.
825 if (DescendantIsFixedToContainerLayer())
829 void Layer::SetPositionConstraint(const LayerPositionConstraint& constraint) {
830 DCHECK(IsPropertyChangeAllowed());
831 if (position_constraint_ == constraint)
833 position_constraint_ = constraint;
837 static void RunCopyCallbackOnMainThread(scoped_ptr<CopyOutputRequest> request,
838 scoped_ptr<CopyOutputResult> result) {
839 request->SendResult(result.Pass());
842 static void PostCopyCallbackToMainThread(
843 scoped_refptr<base::SingleThreadTaskRunner> main_thread_task_runner,
844 scoped_ptr<CopyOutputRequest> request,
845 scoped_ptr<CopyOutputResult> result) {
846 main_thread_task_runner->PostTask(FROM_HERE,
847 base::Bind(&RunCopyCallbackOnMainThread,
848 base::Passed(&request),
849 base::Passed(&result)));
852 void Layer::PushPropertiesTo(LayerImpl* layer) {
853 DCHECK(layer_tree_host_);
855 // If we did not SavePaintProperties() for the layer this frame, then push the
856 // real property values, not the paint property values.
857 bool use_paint_properties = paint_properties_.source_frame_number ==
858 layer_tree_host_->source_frame_number();
860 layer->SetTransformOrigin(transform_origin_);
861 layer->SetBackgroundColor(background_color_);
862 layer->SetBounds(use_paint_properties ? paint_properties_.bounds
864 layer->SetContentBounds(content_bounds());
865 layer->SetContentsScale(contents_scale_x(), contents_scale_y());
868 TRACE_EVENT_CATEGORY_GROUP_ENABLED(
869 TRACE_DISABLED_BY_DEFAULT("cc.debug") "," TRACE_DISABLED_BY_DEFAULT(
870 "devtools.timeline.layers"),
873 layer->SetDebugInfo(TakeDebugInfo());
875 layer->SetDoubleSided(double_sided_);
876 layer->SetDrawCheckerboardForMissingTiles(
877 draw_checkerboard_for_missing_tiles_);
878 layer->SetForceRenderSurface(force_render_surface_);
879 layer->SetDrawsContent(DrawsContent());
880 layer->SetHideLayerAndSubtree(hide_layer_and_subtree_);
881 if (!layer->FilterIsAnimatingOnImplOnly() && !FilterIsAnimating())
882 layer->SetFilters(filters_);
883 DCHECK(!(FilterIsAnimating() && layer->FilterIsAnimatingOnImplOnly()));
884 layer->SetBackgroundFilters(background_filters());
885 layer->SetMasksToBounds(masks_to_bounds_);
886 layer->SetShouldScrollOnMainThread(should_scroll_on_main_thread_);
887 layer->SetHaveWheelEventHandlers(have_wheel_event_handlers_);
888 layer->SetHaveScrollEventHandlers(have_scroll_event_handlers_);
889 layer->SetNonFastScrollableRegion(non_fast_scrollable_region_);
890 layer->SetTouchEventHandlerRegion(touch_event_handler_region_);
891 layer->SetContentsOpaque(contents_opaque_);
892 if (!layer->OpacityIsAnimatingOnImplOnly() && !OpacityIsAnimating())
893 layer->SetOpacity(opacity_);
894 DCHECK(!(OpacityIsAnimating() && layer->OpacityIsAnimatingOnImplOnly()));
895 layer->SetBlendMode(blend_mode_);
896 layer->SetIsRootForIsolatedGroup(is_root_for_isolated_group_);
897 layer->SetPosition(position_);
898 layer->SetIsContainerForFixedPositionLayers(
899 IsContainerForFixedPositionLayers());
900 layer->SetPositionConstraint(position_constraint_);
901 layer->SetShouldFlattenTransform(should_flatten_transform_);
902 layer->SetUseParentBackfaceVisibility(use_parent_backface_visibility_);
903 if (!layer->TransformIsAnimatingOnImplOnly() && !TransformIsAnimating())
904 layer->SetTransformAndInvertibility(transform_, transform_is_invertible_);
905 DCHECK(!(TransformIsAnimating() && layer->TransformIsAnimatingOnImplOnly()));
906 layer->Set3dSortingContextId(sorting_context_id_);
907 layer->SetNumDescendantsThatDrawContent(num_descendants_that_draw_content_);
909 layer->SetScrollClipLayer(scroll_clip_layer_id_);
910 layer->set_user_scrollable_horizontal(user_scrollable_horizontal_);
911 layer->set_user_scrollable_vertical(user_scrollable_vertical_);
913 LayerImpl* scroll_parent = NULL;
914 if (scroll_parent_) {
915 scroll_parent = layer->layer_tree_impl()->LayerById(scroll_parent_->id());
916 DCHECK(scroll_parent);
919 layer->SetScrollParent(scroll_parent);
920 if (scroll_children_) {
921 std::set<LayerImpl*>* scroll_children = new std::set<LayerImpl*>;
922 for (std::set<Layer*>::iterator it = scroll_children_->begin();
923 it != scroll_children_->end();
925 DCHECK_EQ((*it)->scroll_parent(), this);
926 LayerImpl* scroll_child =
927 layer->layer_tree_impl()->LayerById((*it)->id());
928 DCHECK(scroll_child);
929 scroll_children->insert(scroll_child);
931 layer->SetScrollChildren(scroll_children);
933 layer->SetScrollChildren(NULL);
936 LayerImpl* clip_parent = NULL;
939 layer->layer_tree_impl()->LayerById(clip_parent_->id());
943 layer->SetClipParent(clip_parent);
944 if (clip_children_) {
945 std::set<LayerImpl*>* clip_children = new std::set<LayerImpl*>;
946 for (std::set<Layer*>::iterator it = clip_children_->begin();
947 it != clip_children_->end(); ++it) {
948 DCHECK_EQ((*it)->clip_parent(), this);
949 LayerImpl* clip_child = layer->layer_tree_impl()->LayerById((*it)->id());
951 clip_children->insert(clip_child);
953 layer->SetClipChildren(clip_children);
955 layer->SetClipChildren(NULL);
958 // Adjust the scroll delta to be just the scrolls that have happened since
959 // the BeginMainFrame was sent. This happens for impl-side painting
960 // in LayerImpl::ApplyScrollDeltasSinceBeginMainFrame in a separate tree walk.
961 if (layer->layer_tree_impl()->settings().impl_side_painting) {
962 layer->SetScrollOffset(scroll_offset_);
964 layer->SetScrollOffsetAndDelta(
965 scroll_offset_, layer->ScrollDelta() - layer->sent_scroll_delta());
966 layer->SetSentScrollDelta(gfx::Vector2d());
969 // Wrap the copy_requests_ in a PostTask to the main thread.
970 ScopedPtrVector<CopyOutputRequest> main_thread_copy_requests;
971 for (ScopedPtrVector<CopyOutputRequest>::iterator it = copy_requests_.begin();
972 it != copy_requests_.end();
974 scoped_refptr<base::SingleThreadTaskRunner> main_thread_task_runner =
975 layer_tree_host()->proxy()->MainThreadTaskRunner();
976 scoped_ptr<CopyOutputRequest> original_request = copy_requests_.take(it);
977 const CopyOutputRequest& original_request_ref = *original_request;
978 scoped_ptr<CopyOutputRequest> main_thread_request =
979 CopyOutputRequest::CreateRelayRequest(
980 original_request_ref,
981 base::Bind(&PostCopyCallbackToMainThread,
982 main_thread_task_runner,
983 base::Passed(&original_request)));
984 main_thread_copy_requests.push_back(main_thread_request.Pass());
986 copy_requests_.clear();
987 layer->PassCopyRequests(&main_thread_copy_requests);
989 // If the main thread commits multiple times before the impl thread actually
990 // draws, then damage tracking will become incorrect if we simply clobber the
991 // update_rect here. The LayerImpl's update_rect needs to accumulate (i.e.
992 // union) any update changes that have occurred on the main thread.
993 update_rect_.Union(layer->update_rect());
994 layer->SetUpdateRect(update_rect_);
996 layer->SetStackingOrderChanged(stacking_order_changed_);
998 layer_animation_controller_->PushAnimationUpdatesTo(
999 layer->layer_animation_controller());
1001 // Reset any state that should be cleared for the next update.
1002 stacking_order_changed_ = false;
1003 update_rect_ = gfx::RectF();
1005 needs_push_properties_ = false;
1006 num_dependents_need_push_properties_ = 0;
1009 scoped_ptr<LayerImpl> Layer::CreateLayerImpl(LayerTreeImpl* tree_impl) {
1010 return LayerImpl::Create(tree_impl, layer_id_);
1013 bool Layer::DrawsContent() const {
1014 return draws_content_;
1017 bool Layer::HasDrawableContent() const {
1018 return is_drawable_;
1021 void Layer::UpdateDrawsContent(bool has_drawable_content) {
1022 bool draws_content = has_drawable_content;
1023 DCHECK(is_drawable_ || !has_drawable_content);
1024 if (draws_content == draws_content_)
1027 if (HasDelegatedContent()) {
1028 // Layers with delegated content need to be treated as if they have as
1029 // many children as the number of layers they own delegated quads for.
1030 // Since we don't know this number right now, we choose one that acts like
1031 // infinity for our purposes.
1032 AddDrawableDescendants(draws_content ? 1000 : -1000);
1036 parent()->AddDrawableDescendants(draws_content ? 1 : -1);
1038 draws_content_ = draws_content;
1042 int Layer::NumDescendantsThatDrawContent() const {
1043 return num_descendants_that_draw_content_;
1046 void Layer::SavePaintProperties() {
1047 DCHECK(layer_tree_host_);
1049 // TODO(reveman): Save all layer properties that we depend on not
1050 // changing until PushProperties() has been called. crbug.com/231016
1051 paint_properties_.bounds = bounds_;
1052 paint_properties_.source_frame_number =
1053 layer_tree_host_->source_frame_number();
1056 bool Layer::Update(ResourceUpdateQueue* queue,
1057 const OcclusionTracker<Layer>* occlusion) {
1058 DCHECK(layer_tree_host_);
1059 DCHECK_EQ(layer_tree_host_->source_frame_number(),
1060 paint_properties_.source_frame_number) <<
1061 "SavePaintProperties must be called for any layer that is painted.";
1065 bool Layer::NeedMoreUpdates() {
1069 bool Layer::IsSuitableForGpuRasterization() const {
1073 scoped_refptr<base::debug::ConvertableToTraceFormat> Layer::TakeDebugInfo() {
1075 return client_->TakeDebugInfo();
1080 void Layer::CreateRenderSurface() {
1081 DCHECK(!draw_properties_.render_surface);
1082 draw_properties_.render_surface = make_scoped_ptr(new RenderSurface(this));
1083 draw_properties_.render_target = this;
1086 void Layer::ClearRenderSurface() {
1087 draw_properties_.render_surface.reset();
1090 void Layer::ClearRenderSurfaceLayerList() {
1091 if (draw_properties_.render_surface)
1092 draw_properties_.render_surface->layer_list().clear();
1095 gfx::Vector2dF Layer::ScrollOffsetForAnimation() const {
1096 return TotalScrollOffset();
1099 // On<Property>Animated is called due to an ongoing accelerated animation.
1100 // Since this animation is also being run on the compositor thread, there
1101 // is no need to request a commit to push this value over, so the value is
1102 // set directly rather than by calling Set<Property>.
1103 void Layer::OnFilterAnimated(const FilterOperations& filters) {
1107 void Layer::OnOpacityAnimated(float opacity) {
1111 void Layer::OnTransformAnimated(const gfx::Transform& transform) {
1112 if (transform_ == transform)
1114 transform_ = transform;
1115 transform_is_invertible_ = transform.IsInvertible();
1118 void Layer::OnScrollOffsetAnimated(const gfx::Vector2dF& scroll_offset) {
1119 // Do nothing. Scroll deltas will be sent from the compositor thread back
1120 // to the main thread in the same manner as during non-animated
1121 // compositor-driven scrolling.
1124 void Layer::OnAnimationWaitingForDeletion() {
1125 // Animations are only deleted during PushProperties.
1126 SetNeedsPushProperties();
1129 bool Layer::IsActive() const {
1133 bool Layer::AddAnimation(scoped_ptr <Animation> animation) {
1134 if (!layer_animation_controller_->animation_registrar())
1137 if (animation->target_property() == Animation::ScrollOffset &&
1138 !layer_animation_controller_->animation_registrar()
1139 ->supports_scroll_animations())
1142 UMA_HISTOGRAM_BOOLEAN("Renderer.AnimationAddedToOrphanLayer",
1144 layer_animation_controller_->AddAnimation(animation.Pass());
1149 void Layer::PauseAnimation(int animation_id, double time_offset) {
1150 layer_animation_controller_->PauseAnimation(
1151 animation_id, base::TimeDelta::FromSecondsD(time_offset));
1155 void Layer::RemoveAnimation(int animation_id) {
1156 layer_animation_controller_->RemoveAnimation(animation_id);
1160 void Layer::SetLayerAnimationControllerForTest(
1161 scoped_refptr<LayerAnimationController> controller) {
1162 layer_animation_controller_->RemoveValueObserver(this);
1163 layer_animation_controller_ = controller;
1164 layer_animation_controller_->AddValueObserver(this);
1168 bool Layer::HasActiveAnimation() const {
1169 return layer_animation_controller_->HasActiveAnimation();
1172 void Layer::AddLayerAnimationEventObserver(
1173 LayerAnimationEventObserver* animation_observer) {
1174 layer_animation_controller_->AddEventObserver(animation_observer);
1177 void Layer::RemoveLayerAnimationEventObserver(
1178 LayerAnimationEventObserver* animation_observer) {
1179 layer_animation_controller_->RemoveEventObserver(animation_observer);
1182 Region Layer::VisibleContentOpaqueRegion() const {
1183 if (contents_opaque())
1184 return visible_content_rect();
1188 ScrollbarLayerInterface* Layer::ToScrollbarLayer() {
1192 RenderingStatsInstrumentation* Layer::rendering_stats_instrumentation() const {
1193 return layer_tree_host_->rendering_stats_instrumentation();
1196 bool Layer::SupportsLCDText() const {
1200 void Layer::RemoveFromScrollTree() {
1201 if (scroll_children_.get()) {
1202 std::set<Layer*> copy = *scroll_children_;
1203 for (std::set<Layer*>::iterator it = copy.begin(); it != copy.end(); ++it)
1204 (*it)->SetScrollParent(NULL);
1207 DCHECK(!scroll_children_);
1208 SetScrollParent(NULL);
1211 void Layer::RemoveFromClipTree() {
1212 if (clip_children_.get()) {
1213 std::set<Layer*> copy = *clip_children_;
1214 for (std::set<Layer*>::iterator it = copy.begin(); it != copy.end(); ++it)
1215 (*it)->SetClipParent(NULL);
1218 DCHECK(!clip_children_);
1219 SetClipParent(NULL);
1222 void Layer::AddDrawableDescendants(int num) {
1223 DCHECK_GE(num_descendants_that_draw_content_, 0);
1224 DCHECK_GE(num_descendants_that_draw_content_ + num, 0);
1227 num_descendants_that_draw_content_ += num;
1230 parent()->AddDrawableDescendants(num);
1233 void Layer::RunMicroBenchmark(MicroBenchmark* benchmark) {
1234 benchmark->RunOnLayer(this);
1237 bool Layer::HasDelegatedContent() const {