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/layer_animation_controller.h"
16 #include "cc/layers/layer_client.h"
17 #include "cc/layers/layer_impl.h"
18 #include "cc/output/copy_output_request.h"
19 #include "cc/output/copy_output_result.h"
20 #include "cc/trees/layer_tree_host.h"
21 #include "cc/trees/layer_tree_impl.h"
22 #include "third_party/skia/include/core/SkImageFilter.h"
23 #include "ui/gfx/rect_conversions.h"
27 static int s_next_layer_id = 1;
29 scoped_refptr<Layer> Layer::Create() {
30 return make_scoped_refptr(new Layer());
34 : needs_push_properties_(false),
35 num_dependents_need_push_properties_(false),
36 stacking_order_changed_(false),
37 layer_id_(s_next_layer_id++),
38 ignore_set_needs_commit_(false),
40 layer_tree_host_(NULL),
42 should_scroll_on_main_thread_(false),
43 have_wheel_event_handlers_(false),
44 user_scrollable_horizontal_(true),
45 user_scrollable_vertical_(true),
46 anchor_point_(0.5f, 0.5f),
48 compositing_reasons_(kCompositingReasonUnknown),
51 is_container_for_fixed_position_layers_(false),
53 hide_layer_and_subtree_(false),
54 masks_to_bounds_(false),
55 contents_opaque_(false),
58 use_parent_backface_visibility_(false),
59 draw_checkerboard_for_missing_tiles_(false),
60 force_render_surface_(false),
68 layer_id_ = s_next_layer_id++;
71 layer_animation_controller_ = LayerAnimationController::Create(layer_id_);
72 layer_animation_controller_->AddValueObserver(this);
76 // Our parent should be holding a reference to us so there should be no
77 // way for us to be destroyed while we still have a parent.
79 // Similarly we shouldn't have a layer tree host since it also keeps a
81 DCHECK(!layer_tree_host());
83 layer_animation_controller_->RemoveValueObserver(this);
85 // Remove the parent reference from all children and dependents.
87 if (mask_layer_.get()) {
88 DCHECK_EQ(this, mask_layer_->parent());
89 mask_layer_->RemoveFromParent();
91 if (replica_layer_.get()) {
92 DCHECK_EQ(this, replica_layer_->parent());
93 replica_layer_->RemoveFromParent();
96 RemoveFromScrollTree();
100 void Layer::SetLayerTreeHost(LayerTreeHost* host) {
101 if (layer_tree_host_ == host)
104 layer_tree_host_ = host;
106 // When changing hosts, the layer needs to commit its properties to the impl
107 // side for the new host.
108 SetNeedsPushProperties();
110 for (size_t i = 0; i < children_.size(); ++i)
111 children_[i]->SetLayerTreeHost(host);
113 if (mask_layer_.get())
114 mask_layer_->SetLayerTreeHost(host);
115 if (replica_layer_.get())
116 replica_layer_->SetLayerTreeHost(host);
119 layer_animation_controller_->SetAnimationRegistrar(
120 host->animation_registrar());
122 if (host->settings().layer_transforms_should_scale_layer_contents)
123 reset_raster_scale_to_unknown();
126 if (host && layer_animation_controller_->has_any_animation())
127 host->SetNeedsCommit();
128 if (host && (!filters_.IsEmpty() || !background_filters_.IsEmpty()))
129 layer_tree_host_->set_needs_filter_context();
132 void Layer::SetNeedsUpdate() {
133 if (layer_tree_host_ && !ignore_set_needs_commit_)
134 layer_tree_host_->SetNeedsUpdateLayers();
137 void Layer::SetNeedsCommit() {
138 if (!layer_tree_host_)
141 SetNeedsPushProperties();
143 if (ignore_set_needs_commit_)
146 layer_tree_host_->SetNeedsCommit();
149 void Layer::SetNeedsFullTreeSync() {
150 if (!layer_tree_host_)
153 layer_tree_host_->SetNeedsFullTreeSync();
156 void Layer::SetNextCommitWaitsForActivation() {
157 if (!layer_tree_host_)
160 layer_tree_host_->SetNextCommitWaitsForActivation();
163 void Layer::SetNeedsPushProperties() {
164 if (needs_push_properties_)
166 if (!parent_should_know_need_push_properties() && parent_)
167 parent_->AddDependentNeedsPushProperties();
168 needs_push_properties_ = true;
171 void Layer::AddDependentNeedsPushProperties() {
172 DCHECK_GE(num_dependents_need_push_properties_, 0);
174 if (!parent_should_know_need_push_properties() && parent_)
175 parent_->AddDependentNeedsPushProperties();
177 num_dependents_need_push_properties_++;
180 void Layer::RemoveDependentNeedsPushProperties() {
181 num_dependents_need_push_properties_--;
182 DCHECK_GE(num_dependents_need_push_properties_, 0);
184 if (!parent_should_know_need_push_properties() && parent_)
185 parent_->RemoveDependentNeedsPushProperties();
188 bool Layer::IsPropertyChangeAllowed() const {
189 if (!layer_tree_host_)
192 if (!layer_tree_host_->settings().strict_layer_property_change_checking)
195 return !layer_tree_host_->in_paint_layer_contents();
198 gfx::Rect Layer::LayerRectToContentRect(const gfx::RectF& layer_rect) const {
199 gfx::RectF content_rect =
200 gfx::ScaleRect(layer_rect, contents_scale_x(), contents_scale_y());
201 // Intersect with content rect to avoid the extra pixel because for some
202 // values x and y, ceil((x / y) * y) may be x + 1.
203 content_rect.Intersect(gfx::Rect(content_bounds()));
204 return gfx::ToEnclosingRect(content_rect);
207 skia::RefPtr<SkPicture> Layer::GetPicture() const {
208 return skia::RefPtr<SkPicture>();
211 void Layer::SetParent(Layer* layer) {
212 DCHECK(!layer || !layer->HasAncestor(this));
214 if (parent_should_know_need_push_properties()) {
216 parent_->RemoveDependentNeedsPushProperties();
218 layer->AddDependentNeedsPushProperties();
222 SetLayerTreeHost(parent_ ? parent_->layer_tree_host() : NULL);
224 if (!layer_tree_host_)
226 const LayerTreeSettings& settings = layer_tree_host_->settings();
227 if (!settings.layer_transforms_should_scale_layer_contents)
230 reset_raster_scale_to_unknown();
231 if (mask_layer_.get())
232 mask_layer_->reset_raster_scale_to_unknown();
233 if (replica_layer_.get() && replica_layer_->mask_layer_.get())
234 replica_layer_->mask_layer_->reset_raster_scale_to_unknown();
237 void Layer::AddChild(scoped_refptr<Layer> child) {
238 InsertChild(child, children_.size());
241 void Layer::InsertChild(scoped_refptr<Layer> child, size_t index) {
242 DCHECK(IsPropertyChangeAllowed());
243 child->RemoveFromParent();
244 child->SetParent(this);
245 child->stacking_order_changed_ = true;
247 index = std::min(index, children_.size());
248 children_.insert(children_.begin() + index, child);
249 SetNeedsFullTreeSync();
252 void Layer::RemoveFromParent() {
253 DCHECK(IsPropertyChangeAllowed());
255 parent_->RemoveChildOrDependent(this);
258 void Layer::RemoveChildOrDependent(Layer* child) {
259 if (mask_layer_.get() == child) {
260 mask_layer_->SetParent(NULL);
262 SetNeedsFullTreeSync();
265 if (replica_layer_.get() == child) {
266 replica_layer_->SetParent(NULL);
267 replica_layer_ = NULL;
268 SetNeedsFullTreeSync();
272 for (LayerList::iterator iter = children_.begin();
273 iter != children_.end();
275 if (iter->get() != child)
278 child->SetParent(NULL);
279 children_.erase(iter);
280 SetNeedsFullTreeSync();
285 void Layer::ReplaceChild(Layer* reference, scoped_refptr<Layer> new_layer) {
287 DCHECK_EQ(reference->parent(), this);
288 DCHECK(IsPropertyChangeAllowed());
290 if (reference == new_layer.get())
293 int reference_index = IndexOfChild(reference);
294 if (reference_index == -1) {
299 reference->RemoveFromParent();
301 if (new_layer.get()) {
302 new_layer->RemoveFromParent();
303 InsertChild(new_layer, reference_index);
307 int Layer::IndexOfChild(const Layer* reference) {
308 for (size_t i = 0; i < children_.size(); ++i) {
309 if (children_[i].get() == reference)
315 void Layer::SetBounds(gfx::Size size) {
316 DCHECK(IsPropertyChangeAllowed());
317 if (bounds() == size)
324 Layer* Layer::RootLayer() {
326 while (layer->parent())
327 layer = layer->parent();
331 void Layer::RemoveAllChildren() {
332 DCHECK(IsPropertyChangeAllowed());
333 while (children_.size()) {
334 Layer* layer = children_[0].get();
335 DCHECK_EQ(this, layer->parent());
336 layer->RemoveFromParent();
340 void Layer::SetChildren(const LayerList& children) {
341 DCHECK(IsPropertyChangeAllowed());
342 if (children == children_)
346 for (size_t i = 0; i < children.size(); ++i)
347 AddChild(children[i]);
350 bool Layer::HasAncestor(const Layer* ancestor) const {
351 for (const Layer* layer = parent(); layer; layer = layer->parent()) {
352 if (layer == ancestor)
358 void Layer::RequestCopyOfOutput(
359 scoped_ptr<CopyOutputRequest> request) {
360 DCHECK(IsPropertyChangeAllowed());
361 if (request->IsEmpty())
363 copy_requests_.push_back(request.Pass());
367 void Layer::SetAnchorPoint(gfx::PointF anchor_point) {
368 DCHECK(IsPropertyChangeAllowed());
369 if (anchor_point_ == anchor_point)
371 anchor_point_ = anchor_point;
375 void Layer::SetAnchorPointZ(float anchor_point_z) {
376 DCHECK(IsPropertyChangeAllowed());
377 if (anchor_point_z_ == anchor_point_z)
379 anchor_point_z_ = anchor_point_z;
383 void Layer::SetBackgroundColor(SkColor background_color) {
384 DCHECK(IsPropertyChangeAllowed());
385 if (background_color_ == background_color)
387 background_color_ = background_color;
391 SkColor Layer::SafeOpaqueBackgroundColor() const {
392 SkColor color = background_color();
393 if (SkColorGetA(color) == 255 && !contents_opaque()) {
394 color = SK_ColorTRANSPARENT;
395 } else if (SkColorGetA(color) != 255 && contents_opaque()) {
396 for (const Layer* layer = parent(); layer;
397 layer = layer->parent()) {
398 color = layer->background_color();
399 if (SkColorGetA(color) == 255)
402 if (SkColorGetA(color) != 255)
403 color = layer_tree_host_->background_color();
404 if (SkColorGetA(color) != 255)
405 color = SkColorSetA(color, 255);
410 void Layer::CalculateContentsScale(
411 float ideal_contents_scale,
412 float device_scale_factor,
413 float page_scale_factor,
414 bool animating_transform_to_screen,
415 float* contents_scale_x,
416 float* contents_scale_y,
417 gfx::Size* content_bounds) {
418 DCHECK(layer_tree_host_);
420 *contents_scale_x = 1;
421 *contents_scale_y = 1;
422 *content_bounds = bounds();
425 void Layer::SetMasksToBounds(bool masks_to_bounds) {
426 DCHECK(IsPropertyChangeAllowed());
427 if (masks_to_bounds_ == masks_to_bounds)
429 masks_to_bounds_ = masks_to_bounds;
433 void Layer::SetMaskLayer(Layer* mask_layer) {
434 DCHECK(IsPropertyChangeAllowed());
435 if (mask_layer_.get() == mask_layer)
437 if (mask_layer_.get()) {
438 DCHECK_EQ(this, mask_layer_->parent());
439 mask_layer_->RemoveFromParent();
441 mask_layer_ = mask_layer;
442 if (mask_layer_.get()) {
443 DCHECK(!mask_layer_->parent());
444 mask_layer_->RemoveFromParent();
445 mask_layer_->SetParent(this);
446 mask_layer_->SetIsMask(true);
448 SetNeedsFullTreeSync();
451 void Layer::SetReplicaLayer(Layer* layer) {
452 DCHECK(IsPropertyChangeAllowed());
453 if (replica_layer_.get() == layer)
455 if (replica_layer_.get()) {
456 DCHECK_EQ(this, replica_layer_->parent());
457 replica_layer_->RemoveFromParent();
459 replica_layer_ = layer;
460 if (replica_layer_.get()) {
461 DCHECK(!replica_layer_->parent());
462 replica_layer_->RemoveFromParent();
463 replica_layer_->SetParent(this);
465 SetNeedsFullTreeSync();
468 void Layer::SetFilters(const FilterOperations& filters) {
469 DCHECK(IsPropertyChangeAllowed());
470 if (filters_ == filters)
474 if (!filters.IsEmpty() && layer_tree_host_)
475 layer_tree_host_->set_needs_filter_context();
478 bool Layer::FilterIsAnimating() const {
479 return layer_animation_controller_->IsAnimatingProperty(Animation::Filter);
482 void Layer::SetBackgroundFilters(const FilterOperations& filters) {
483 DCHECK(IsPropertyChangeAllowed());
484 if (background_filters_ == filters)
486 background_filters_ = filters;
488 if (!filters.IsEmpty() && layer_tree_host_)
489 layer_tree_host_->set_needs_filter_context();
492 void Layer::SetOpacity(float opacity) {
493 DCHECK(IsPropertyChangeAllowed());
494 if (opacity_ == opacity)
500 bool Layer::OpacityIsAnimating() const {
501 return layer_animation_controller_->IsAnimatingProperty(Animation::Opacity);
504 bool Layer::OpacityCanAnimateOnImplThread() const {
508 void Layer::SetContentsOpaque(bool opaque) {
509 DCHECK(IsPropertyChangeAllowed());
510 if (contents_opaque_ == opaque)
512 contents_opaque_ = opaque;
516 void Layer::SetPosition(gfx::PointF position) {
517 DCHECK(IsPropertyChangeAllowed());
518 if (position_ == position)
520 position_ = position;
524 bool Layer::IsContainerForFixedPositionLayers() const {
525 if (!transform_.IsIdentityOrTranslation())
527 if (parent_ && !parent_->sublayer_transform_.IsIdentityOrTranslation())
529 return is_container_for_fixed_position_layers_;
532 void Layer::SetSublayerTransform(const gfx::Transform& sublayer_transform) {
533 DCHECK(IsPropertyChangeAllowed());
534 if (sublayer_transform_ == sublayer_transform)
536 sublayer_transform_ = sublayer_transform;
540 void Layer::SetTransform(const gfx::Transform& transform) {
541 DCHECK(IsPropertyChangeAllowed());
542 if (transform_ == transform)
544 transform_ = transform;
548 bool Layer::TransformIsAnimating() const {
549 return layer_animation_controller_->IsAnimatingProperty(Animation::Transform);
552 void Layer::SetScrollParent(Layer* parent) {
553 DCHECK(IsPropertyChangeAllowed());
554 if (scroll_parent_ == parent)
558 scroll_parent_->RemoveScrollChild(this);
560 scroll_parent_ = parent;
563 scroll_parent_->AddScrollChild(this);
568 void Layer::AddScrollChild(Layer* child) {
569 if (!scroll_children_)
570 scroll_children_.reset(new std::set<Layer*>);
571 scroll_children_->insert(child);
575 void Layer::RemoveScrollChild(Layer* child) {
576 scroll_children_->erase(child);
577 if (scroll_children_->empty())
578 scroll_children_.reset();
582 void Layer::SetClipParent(Layer* ancestor) {
583 DCHECK(IsPropertyChangeAllowed());
584 if (clip_parent_ == ancestor)
588 clip_parent_->RemoveClipChild(this);
590 clip_parent_ = ancestor;
593 clip_parent_->AddClipChild(this);
598 void Layer::AddClipChild(Layer* child) {
600 clip_children_.reset(new std::set<Layer*>);
601 clip_children_->insert(child);
605 void Layer::RemoveClipChild(Layer* child) {
606 clip_children_->erase(child);
607 if (clip_children_->empty())
608 clip_children_.reset();
612 void Layer::SetScrollOffset(gfx::Vector2d scroll_offset) {
613 DCHECK(IsPropertyChangeAllowed());
614 if (scroll_offset_ == scroll_offset)
616 scroll_offset_ = scroll_offset;
620 void Layer::SetScrollOffsetFromImplSide(gfx::Vector2d scroll_offset) {
621 DCHECK(IsPropertyChangeAllowed());
622 // This function only gets called during a BeginMainFrame, so there
623 // is no need to call SetNeedsUpdate here.
624 DCHECK(layer_tree_host_ && layer_tree_host_->CommitRequested());
625 if (scroll_offset_ == scroll_offset)
627 scroll_offset_ = scroll_offset;
628 SetNeedsPushProperties();
629 if (!did_scroll_callback_.is_null())
630 did_scroll_callback_.Run();
631 // The callback could potentially change the layer structure:
632 // "this" may have been destroyed during the process.
635 void Layer::SetMaxScrollOffset(gfx::Vector2d max_scroll_offset) {
636 DCHECK(IsPropertyChangeAllowed());
637 if (max_scroll_offset_ == max_scroll_offset)
639 max_scroll_offset_ = max_scroll_offset;
643 void Layer::SetScrollable(bool scrollable) {
644 DCHECK(IsPropertyChangeAllowed());
645 if (scrollable_ == scrollable)
647 scrollable_ = scrollable;
651 void Layer::SetUserScrollable(bool horizontal, bool vertical) {
652 DCHECK(IsPropertyChangeAllowed());
653 if (user_scrollable_horizontal_ == horizontal &&
654 user_scrollable_vertical_ == vertical)
656 user_scrollable_horizontal_ = horizontal;
657 user_scrollable_vertical_ = vertical;
661 void Layer::SetShouldScrollOnMainThread(bool should_scroll_on_main_thread) {
662 DCHECK(IsPropertyChangeAllowed());
663 if (should_scroll_on_main_thread_ == should_scroll_on_main_thread)
665 should_scroll_on_main_thread_ = should_scroll_on_main_thread;
669 void Layer::SetHaveWheelEventHandlers(bool have_wheel_event_handlers) {
670 DCHECK(IsPropertyChangeAllowed());
671 if (have_wheel_event_handlers_ == have_wheel_event_handlers)
673 have_wheel_event_handlers_ = have_wheel_event_handlers;
677 void Layer::SetNonFastScrollableRegion(const Region& region) {
678 DCHECK(IsPropertyChangeAllowed());
679 if (non_fast_scrollable_region_ == region)
681 non_fast_scrollable_region_ = region;
685 void Layer::SetTouchEventHandlerRegion(const Region& region) {
686 DCHECK(IsPropertyChangeAllowed());
687 if (touch_event_handler_region_ == region)
689 touch_event_handler_region_ = region;
693 void Layer::SetDrawCheckerboardForMissingTiles(bool checkerboard) {
694 DCHECK(IsPropertyChangeAllowed());
695 if (draw_checkerboard_for_missing_tiles_ == checkerboard)
697 draw_checkerboard_for_missing_tiles_ = checkerboard;
701 void Layer::SetForceRenderSurface(bool force) {
702 DCHECK(IsPropertyChangeAllowed());
703 if (force_render_surface_ == force)
705 force_render_surface_ = force;
709 void Layer::SetDoubleSided(bool double_sided) {
710 DCHECK(IsPropertyChangeAllowed());
711 if (double_sided_ == double_sided)
713 double_sided_ = double_sided;
717 void Layer::SetIsDrawable(bool is_drawable) {
718 DCHECK(IsPropertyChangeAllowed());
719 if (is_drawable_ == is_drawable)
722 is_drawable_ = is_drawable;
726 void Layer::SetHideLayerAndSubtree(bool hide) {
727 DCHECK(IsPropertyChangeAllowed());
728 if (hide_layer_and_subtree_ == hide)
731 hide_layer_and_subtree_ = hide;
735 void Layer::SetNeedsDisplayRect(const gfx::RectF& dirty_rect) {
736 if (dirty_rect.IsEmpty())
739 SetNeedsPushProperties();
740 update_rect_.Union(dirty_rect);
746 bool Layer::DescendantIsFixedToContainerLayer() const {
747 for (size_t i = 0; i < children_.size(); ++i) {
748 if (children_[i]->position_constraint_.is_fixed_position() ||
749 children_[i]->DescendantIsFixedToContainerLayer())
755 void Layer::SetIsContainerForFixedPositionLayers(bool container) {
756 if (is_container_for_fixed_position_layers_ == container)
758 is_container_for_fixed_position_layers_ = container;
760 if (layer_tree_host_ && layer_tree_host_->CommitRequested())
763 // Only request a commit if we have a fixed positioned descendant.
764 if (DescendantIsFixedToContainerLayer())
768 void Layer::SetPositionConstraint(const LayerPositionConstraint& constraint) {
769 DCHECK(IsPropertyChangeAllowed());
770 if (position_constraint_ == constraint)
772 position_constraint_ = constraint;
776 static void RunCopyCallbackOnMainThread(scoped_ptr<CopyOutputRequest> request,
777 scoped_ptr<CopyOutputResult> result) {
778 request->SendResult(result.Pass());
781 static void PostCopyCallbackToMainThread(
782 scoped_refptr<base::SingleThreadTaskRunner> main_thread_task_runner,
783 scoped_ptr<CopyOutputRequest> request,
784 scoped_ptr<CopyOutputResult> result) {
785 main_thread_task_runner->PostTask(FROM_HERE,
786 base::Bind(&RunCopyCallbackOnMainThread,
787 base::Passed(&request),
788 base::Passed(&result)));
791 void Layer::PushPropertiesTo(LayerImpl* layer) {
792 DCHECK(layer_tree_host_);
794 // If we did not SavePaintProperties() for the layer this frame, then push the
795 // real property values, not the paint property values.
796 bool use_paint_properties = paint_properties_.source_frame_number ==
797 layer_tree_host_->source_frame_number();
799 layer->SetAnchorPoint(anchor_point_);
800 layer->SetAnchorPointZ(anchor_point_z_);
801 layer->SetBackgroundColor(background_color_);
802 layer->SetBounds(use_paint_properties ? paint_properties_.bounds
804 layer->SetContentBounds(content_bounds());
805 layer->SetContentsScale(contents_scale_x(), contents_scale_y());
808 TRACE_EVENT_CATEGORY_GROUP_ENABLED(TRACE_DISABLED_BY_DEFAULT("cc.debug"),
811 layer->SetDebugName(DebugName());
813 layer->SetDebugName(std::string());
815 layer->SetCompositingReasons(compositing_reasons_);
816 layer->SetDoubleSided(double_sided_);
817 layer->SetDrawCheckerboardForMissingTiles(
818 draw_checkerboard_for_missing_tiles_);
819 layer->SetForceRenderSurface(force_render_surface_);
820 layer->SetDrawsContent(DrawsContent());
821 layer->SetHideLayerAndSubtree(hide_layer_and_subtree_);
822 if (!layer->FilterIsAnimatingOnImplOnly() && !FilterIsAnimating())
823 layer->SetFilters(filters_);
824 DCHECK(!(FilterIsAnimating() && layer->FilterIsAnimatingOnImplOnly()));
825 layer->SetBackgroundFilters(background_filters());
826 layer->SetMasksToBounds(masks_to_bounds_);
827 layer->SetShouldScrollOnMainThread(should_scroll_on_main_thread_);
828 layer->SetHaveWheelEventHandlers(have_wheel_event_handlers_);
829 layer->SetNonFastScrollableRegion(non_fast_scrollable_region_);
830 layer->SetTouchEventHandlerRegion(touch_event_handler_region_);
831 layer->SetContentsOpaque(contents_opaque_);
832 if (!layer->OpacityIsAnimatingOnImplOnly() && !OpacityIsAnimating())
833 layer->SetOpacity(opacity_);
834 DCHECK(!(OpacityIsAnimating() && layer->OpacityIsAnimatingOnImplOnly()));
835 layer->SetPosition(position_);
836 layer->SetIsContainerForFixedPositionLayers(
837 IsContainerForFixedPositionLayers());
838 layer->SetFixedContainerSizeDelta(gfx::Vector2dF());
839 layer->SetPositionConstraint(position_constraint_);
840 layer->SetPreserves3d(preserves_3d());
841 layer->SetUseParentBackfaceVisibility(use_parent_backface_visibility_);
842 layer->SetSublayerTransform(sublayer_transform_);
843 if (!layer->TransformIsAnimatingOnImplOnly() && !TransformIsAnimating())
844 layer->SetTransform(transform_);
845 DCHECK(!(TransformIsAnimating() && layer->TransformIsAnimatingOnImplOnly()));
847 layer->SetScrollable(scrollable_);
848 layer->set_user_scrollable_horizontal(user_scrollable_horizontal_);
849 layer->set_user_scrollable_vertical(user_scrollable_vertical_);
850 layer->SetMaxScrollOffset(max_scroll_offset_);
852 LayerImpl* scroll_parent = NULL;
854 scroll_parent = layer->layer_tree_impl()->LayerById(scroll_parent_->id());
856 layer->SetScrollParent(scroll_parent);
857 if (scroll_children_) {
858 std::set<LayerImpl*>* scroll_children = new std::set<LayerImpl*>;
859 for (std::set<Layer*>::iterator it = scroll_children_->begin();
860 it != scroll_children_->end(); ++it)
861 scroll_children->insert(layer->layer_tree_impl()->LayerById((*it)->id()));
862 layer->SetScrollChildren(scroll_children);
865 LayerImpl* clip_parent = NULL;
868 layer->layer_tree_impl()->LayerById(clip_parent_->id());
871 layer->SetClipParent(clip_parent);
872 if (clip_children_) {
873 std::set<LayerImpl*>* clip_children = new std::set<LayerImpl*>;
874 for (std::set<Layer*>::iterator it = clip_children_->begin();
875 it != clip_children_->end(); ++it) {
876 LayerImpl* clip_child = layer->layer_tree_impl()->LayerById((*it)->id());
878 clip_children->insert(clip_child);
880 layer->SetClipChildren(clip_children);
883 // Adjust the scroll delta to be just the scrolls that have happened since
884 // the BeginMainFrame was sent. This happens for impl-side painting
885 // in LayerImpl::ApplyScrollDeltasSinceBeginMainFrame in a separate tree walk.
886 if (layer->layer_tree_impl()->settings().impl_side_painting) {
887 layer->SetScrollOffset(scroll_offset_);
889 layer->SetScrollOffsetAndDelta(
890 scroll_offset_, layer->ScrollDelta() - layer->sent_scroll_delta());
891 layer->SetSentScrollDelta(gfx::Vector2d());
894 // Wrap the copy_requests_ in a PostTask to the main thread.
895 ScopedPtrVector<CopyOutputRequest> main_thread_copy_requests;
896 for (ScopedPtrVector<CopyOutputRequest>::iterator it = copy_requests_.begin();
897 it != copy_requests_.end();
899 scoped_refptr<base::SingleThreadTaskRunner> main_thread_task_runner =
900 layer_tree_host()->proxy()->MainThreadTaskRunner();
901 scoped_ptr<CopyOutputRequest> original_request = copy_requests_.take(it);
902 const CopyOutputRequest& original_request_ref = *original_request;
903 scoped_ptr<CopyOutputRequest> main_thread_request =
904 CopyOutputRequest::CreateRelayRequest(
905 original_request_ref,
906 base::Bind(&PostCopyCallbackToMainThread,
907 main_thread_task_runner,
908 base::Passed(&original_request)));
909 main_thread_copy_requests.push_back(main_thread_request.Pass());
911 copy_requests_.clear();
912 layer->PassCopyRequests(&main_thread_copy_requests);
914 // If the main thread commits multiple times before the impl thread actually
915 // draws, then damage tracking will become incorrect if we simply clobber the
916 // update_rect here. The LayerImpl's update_rect needs to accumulate (i.e.
917 // union) any update changes that have occurred on the main thread.
918 update_rect_.Union(layer->update_rect());
919 layer->set_update_rect(update_rect_);
921 layer->SetStackingOrderChanged(stacking_order_changed_);
923 layer_animation_controller_->PushAnimationUpdatesTo(
924 layer->layer_animation_controller());
926 // Reset any state that should be cleared for the next update.
927 stacking_order_changed_ = false;
928 update_rect_ = gfx::RectF();
930 // Animating layers require further push properties to clean up the animation.
932 needs_push_properties_ = layer_animation_controller_->has_any_animation();
933 num_dependents_need_push_properties_ = 0;
936 scoped_ptr<LayerImpl> Layer::CreateLayerImpl(LayerTreeImpl* tree_impl) {
937 return LayerImpl::Create(tree_impl, layer_id_);
940 bool Layer::DrawsContent() const {
944 void Layer::SavePaintProperties() {
945 DCHECK(layer_tree_host_);
947 // TODO(reveman): Save all layer properties that we depend on not
948 // changing until PushProperties() has been called. crbug.com/231016
949 paint_properties_.bounds = bounds_;
950 paint_properties_.source_frame_number =
951 layer_tree_host_->source_frame_number();
954 bool Layer::Update(ResourceUpdateQueue* queue,
955 const OcclusionTracker* occlusion) {
956 DCHECK(layer_tree_host_);
957 DCHECK_EQ(layer_tree_host_->source_frame_number(),
958 paint_properties_.source_frame_number) <<
959 "SavePaintProperties must be called for any layer that is painted.";
963 bool Layer::NeedMoreUpdates() {
967 std::string Layer::DebugName() {
968 return client_ ? client_->DebugName() : std::string();
971 void Layer::SetCompositingReasons(CompositingReasons reasons) {
972 compositing_reasons_ = reasons;
975 void Layer::CreateRenderSurface() {
976 DCHECK(!draw_properties_.render_surface);
977 draw_properties_.render_surface = make_scoped_ptr(new RenderSurface(this));
978 draw_properties_.render_target = this;
981 void Layer::ClearRenderSurface() {
982 draw_properties_.render_surface.reset();
985 // On<Property>Animated is called due to an ongoing accelerated animation.
986 // Since this animation is also being run on the compositor thread, there
987 // is no need to request a commit to push this value over, so the value is
988 // set directly rather than by calling Set<Property>.
989 void Layer::OnFilterAnimated(const FilterOperations& filters) {
993 void Layer::OnOpacityAnimated(float opacity) {
997 void Layer::OnTransformAnimated(const gfx::Transform& transform) {
998 transform_ = transform;
1001 bool Layer::IsActive() const {
1005 bool Layer::AddAnimation(scoped_ptr <Animation> animation) {
1006 if (!layer_animation_controller_->animation_registrar())
1009 UMA_HISTOGRAM_BOOLEAN("Renderer.AnimationAddedToOrphanLayer",
1011 layer_animation_controller_->AddAnimation(animation.Pass());
1016 void Layer::PauseAnimation(int animation_id, double time_offset) {
1017 layer_animation_controller_->PauseAnimation(animation_id, time_offset);
1021 void Layer::RemoveAnimation(int animation_id) {
1022 layer_animation_controller_->RemoveAnimation(animation_id);
1026 void Layer::SetLayerAnimationControllerForTest(
1027 scoped_refptr<LayerAnimationController> controller) {
1028 layer_animation_controller_->RemoveValueObserver(this);
1029 layer_animation_controller_ = controller;
1030 layer_animation_controller_->set_force_sync();
1031 layer_animation_controller_->AddValueObserver(this);
1035 bool Layer::HasActiveAnimation() const {
1036 return layer_animation_controller_->HasActiveAnimation();
1039 void Layer::AddLayerAnimationEventObserver(
1040 LayerAnimationEventObserver* animation_observer) {
1041 layer_animation_controller_->AddEventObserver(animation_observer);
1044 void Layer::RemoveLayerAnimationEventObserver(
1045 LayerAnimationEventObserver* animation_observer) {
1046 layer_animation_controller_->RemoveEventObserver(animation_observer);
1049 Region Layer::VisibleContentOpaqueRegion() const {
1050 if (contents_opaque())
1051 return visible_content_rect();
1055 ScrollbarLayerInterface* Layer::ToScrollbarLayer() {
1059 RenderingStatsInstrumentation* Layer::rendering_stats_instrumentation() const {
1060 return layer_tree_host_->rendering_stats_instrumentation();
1063 bool Layer::SupportsLCDText() const {
1067 void Layer::RemoveFromScrollTree() {
1068 if (scroll_children_.get()) {
1069 for (std::set<Layer*>::iterator it = scroll_children_->begin();
1070 it != scroll_children_->end(); ++it)
1071 (*it)->scroll_parent_ = NULL;
1075 scroll_parent_->RemoveScrollChild(this);
1077 scroll_parent_ = NULL;
1080 void Layer::RemoveFromClipTree() {
1081 if (clip_children_.get()) {
1082 for (std::set<Layer*>::iterator it = clip_children_->begin();
1083 it != clip_children_->end(); ++it)
1084 (*it)->clip_parent_ = NULL;
1088 clip_parent_->RemoveClipChild(this);
1090 clip_parent_ = NULL;
1093 void Layer::RunMicroBenchmark(MicroBenchmark* benchmark) {
1094 benchmark->RunOnLayer(this);