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 "base/time/time.h"
14 #include "cc/animation/animation.h"
15 #include "cc/animation/animation_events.h"
16 #include "cc/animation/keyframed_animation_curve.h"
17 #include "cc/animation/layer_animation_controller.h"
18 #include "cc/layers/layer_client.h"
19 #include "cc/layers/layer_impl.h"
20 #include "cc/layers/scrollbar_layer_interface.h"
21 #include "cc/output/copy_output_request.h"
22 #include "cc/output/copy_output_result.h"
23 #include "cc/trees/layer_tree_host.h"
24 #include "cc/trees/layer_tree_impl.h"
25 #include "third_party/skia/include/core/SkImageFilter.h"
26 #include "ui/gfx/geometry/vector2d_conversions.h"
27 #include "ui/gfx/rect_conversions.h"
31 static int s_next_layer_id
= 1;
33 scoped_refptr
<Layer
> Layer::Create() {
34 return make_scoped_refptr(new Layer());
38 : needs_push_properties_(false),
39 num_dependents_need_push_properties_(false),
40 stacking_order_changed_(false),
41 layer_id_(s_next_layer_id
++),
42 ignore_set_needs_commit_(false),
44 layer_tree_host_(NULL
),
45 scroll_clip_layer_id_(INVALID_ID
),
46 should_scroll_on_main_thread_(false),
47 have_wheel_event_handlers_(false),
48 have_scroll_event_handlers_(false),
49 user_scrollable_horizontal_(true),
50 user_scrollable_vertical_(true),
51 is_root_for_isolated_group_(false),
52 is_container_for_fixed_position_layers_(false),
54 hide_layer_and_subtree_(false),
55 masks_to_bounds_(false),
56 contents_opaque_(false),
58 should_flatten_transform_(true),
59 use_parent_backface_visibility_(false),
60 draw_checkerboard_for_missing_tiles_(false),
61 force_render_surface_(false),
63 transform_is_invertible_(true),
64 anchor_point_(0.5f
, 0.5f
),
67 blend_mode_(SkXfermode::kSrcOver_Mode
),
74 if (layer_id_
== INT_MAX
) {
78 layer_animation_controller_
= LayerAnimationController::Create(layer_id_
);
79 layer_animation_controller_
->AddValueObserver(this);
80 layer_animation_controller_
->set_value_provider(this);
84 // Our parent should be holding a reference to us so there should be no
85 // way for us to be destroyed while we still have a parent.
87 // Similarly we shouldn't have a layer tree host since it also keeps a
89 DCHECK(!layer_tree_host());
91 layer_animation_controller_
->RemoveValueObserver(this);
92 layer_animation_controller_
->remove_value_provider(this);
94 // Remove the parent reference from all children and dependents.
96 if (mask_layer_
.get()) {
97 DCHECK_EQ(this, mask_layer_
->parent());
98 mask_layer_
->RemoveFromParent();
100 if (replica_layer_
.get()) {
101 DCHECK_EQ(this, replica_layer_
->parent());
102 replica_layer_
->RemoveFromParent();
105 RemoveFromScrollTree();
106 RemoveFromClipTree();
109 void Layer::SetLayerTreeHost(LayerTreeHost
* host
) {
110 if (layer_tree_host_
== host
)
113 layer_tree_host_
= host
;
115 // When changing hosts, the layer needs to commit its properties to the impl
116 // side for the new host.
117 SetNeedsPushProperties();
119 for (size_t i
= 0; i
< children_
.size(); ++i
)
120 children_
[i
]->SetLayerTreeHost(host
);
122 if (mask_layer_
.get())
123 mask_layer_
->SetLayerTreeHost(host
);
124 if (replica_layer_
.get())
125 replica_layer_
->SetLayerTreeHost(host
);
128 layer_animation_controller_
->SetAnimationRegistrar(
129 host
->animation_registrar());
131 if (host
->settings().layer_transforms_should_scale_layer_contents
)
132 reset_raster_scale_to_unknown();
135 if (host
&& layer_animation_controller_
->has_any_animation())
136 host
->SetNeedsCommit();
139 void Layer::SetNeedsUpdate() {
140 if (layer_tree_host_
&& !ignore_set_needs_commit_
)
141 layer_tree_host_
->SetNeedsUpdateLayers();
144 void Layer::SetNeedsCommit() {
145 if (!layer_tree_host_
)
148 SetNeedsPushProperties();
150 if (ignore_set_needs_commit_
)
153 layer_tree_host_
->SetNeedsCommit();
156 void Layer::SetNeedsFullTreeSync() {
157 if (!layer_tree_host_
)
160 layer_tree_host_
->SetNeedsFullTreeSync();
163 void Layer::SetNextCommitWaitsForActivation() {
164 if (!layer_tree_host_
)
167 layer_tree_host_
->SetNextCommitWaitsForActivation();
170 void Layer::SetNeedsPushProperties() {
171 if (needs_push_properties_
)
173 if (!parent_should_know_need_push_properties() && parent_
)
174 parent_
->AddDependentNeedsPushProperties();
175 needs_push_properties_
= true;
178 void Layer::AddDependentNeedsPushProperties() {
179 DCHECK_GE(num_dependents_need_push_properties_
, 0);
181 if (!parent_should_know_need_push_properties() && parent_
)
182 parent_
->AddDependentNeedsPushProperties();
184 num_dependents_need_push_properties_
++;
187 void Layer::RemoveDependentNeedsPushProperties() {
188 num_dependents_need_push_properties_
--;
189 DCHECK_GE(num_dependents_need_push_properties_
, 0);
191 if (!parent_should_know_need_push_properties() && parent_
)
192 parent_
->RemoveDependentNeedsPushProperties();
195 bool Layer::IsPropertyChangeAllowed() const {
196 if (!layer_tree_host_
)
199 if (!layer_tree_host_
->settings().strict_layer_property_change_checking
)
202 return !layer_tree_host_
->in_paint_layer_contents();
205 gfx::Rect
Layer::LayerRectToContentRect(const gfx::RectF
& layer_rect
) const {
206 gfx::RectF content_rect
=
207 gfx::ScaleRect(layer_rect
, contents_scale_x(), contents_scale_y());
208 // Intersect with content rect to avoid the extra pixel because for some
209 // values x and y, ceil((x / y) * y) may be x + 1.
210 content_rect
.Intersect(gfx::Rect(content_bounds()));
211 return gfx::ToEnclosingRect(content_rect
);
214 skia::RefPtr
<SkPicture
> Layer::GetPicture() const {
215 return skia::RefPtr
<SkPicture
>();
218 void Layer::SetParent(Layer
* layer
) {
219 DCHECK(!layer
|| !layer
->HasAncestor(this));
221 if (parent_should_know_need_push_properties()) {
223 parent_
->RemoveDependentNeedsPushProperties();
225 layer
->AddDependentNeedsPushProperties();
229 SetLayerTreeHost(parent_
? parent_
->layer_tree_host() : NULL
);
231 if (!layer_tree_host_
)
233 const LayerTreeSettings
& settings
= layer_tree_host_
->settings();
234 if (!settings
.layer_transforms_should_scale_layer_contents
)
237 reset_raster_scale_to_unknown();
238 if (mask_layer_
.get())
239 mask_layer_
->reset_raster_scale_to_unknown();
240 if (replica_layer_
.get() && replica_layer_
->mask_layer_
.get())
241 replica_layer_
->mask_layer_
->reset_raster_scale_to_unknown();
244 void Layer::AddChild(scoped_refptr
<Layer
> child
) {
245 InsertChild(child
, children_
.size());
248 void Layer::InsertChild(scoped_refptr
<Layer
> child
, size_t index
) {
249 DCHECK(IsPropertyChangeAllowed());
250 child
->RemoveFromParent();
251 child
->SetParent(this);
252 child
->stacking_order_changed_
= true;
254 index
= std::min(index
, children_
.size());
255 children_
.insert(children_
.begin() + index
, child
);
256 SetNeedsFullTreeSync();
259 void Layer::RemoveFromParent() {
260 DCHECK(IsPropertyChangeAllowed());
262 parent_
->RemoveChildOrDependent(this);
265 void Layer::RemoveChildOrDependent(Layer
* child
) {
266 if (mask_layer_
.get() == child
) {
267 mask_layer_
->SetParent(NULL
);
269 SetNeedsFullTreeSync();
272 if (replica_layer_
.get() == child
) {
273 replica_layer_
->SetParent(NULL
);
274 replica_layer_
= NULL
;
275 SetNeedsFullTreeSync();
279 for (LayerList::iterator iter
= children_
.begin();
280 iter
!= children_
.end();
282 if (iter
->get() != child
)
285 child
->SetParent(NULL
);
286 children_
.erase(iter
);
287 SetNeedsFullTreeSync();
292 void Layer::ReplaceChild(Layer
* reference
, scoped_refptr
<Layer
> new_layer
) {
294 DCHECK_EQ(reference
->parent(), this);
295 DCHECK(IsPropertyChangeAllowed());
297 if (reference
== new_layer
.get())
300 int reference_index
= IndexOfChild(reference
);
301 if (reference_index
== -1) {
306 reference
->RemoveFromParent();
308 if (new_layer
.get()) {
309 new_layer
->RemoveFromParent();
310 InsertChild(new_layer
, reference_index
);
314 int Layer::IndexOfChild(const Layer
* reference
) {
315 for (size_t i
= 0; i
< children_
.size(); ++i
) {
316 if (children_
[i
].get() == reference
)
322 void Layer::SetBounds(const gfx::Size
& size
) {
323 DCHECK(IsPropertyChangeAllowed());
324 if (bounds() == size
)
331 Layer
* Layer::RootLayer() {
333 while (layer
->parent())
334 layer
= layer
->parent();
338 void Layer::RemoveAllChildren() {
339 DCHECK(IsPropertyChangeAllowed());
340 while (children_
.size()) {
341 Layer
* layer
= children_
[0].get();
342 DCHECK_EQ(this, layer
->parent());
343 layer
->RemoveFromParent();
347 void Layer::SetChildren(const LayerList
& children
) {
348 DCHECK(IsPropertyChangeAllowed());
349 if (children
== children_
)
353 for (size_t i
= 0; i
< children
.size(); ++i
)
354 AddChild(children
[i
]);
357 bool Layer::HasAncestor(const Layer
* ancestor
) const {
358 for (const Layer
* layer
= parent(); layer
; layer
= layer
->parent()) {
359 if (layer
== ancestor
)
365 void Layer::RequestCopyOfOutput(
366 scoped_ptr
<CopyOutputRequest
> request
) {
367 DCHECK(IsPropertyChangeAllowed());
368 if (request
->IsEmpty())
370 copy_requests_
.push_back(request
.Pass());
374 void Layer::SetAnchorPoint(const gfx::PointF
& anchor_point
) {
375 DCHECK(IsPropertyChangeAllowed());
376 if (anchor_point_
== anchor_point
)
378 anchor_point_
= anchor_point
;
382 void Layer::SetAnchorPointZ(float anchor_point_z
) {
383 DCHECK(IsPropertyChangeAllowed());
384 if (anchor_point_z_
== anchor_point_z
)
386 anchor_point_z_
= anchor_point_z
;
390 void Layer::SetBackgroundColor(SkColor background_color
) {
391 DCHECK(IsPropertyChangeAllowed());
392 if (background_color_
== background_color
)
394 background_color_
= background_color
;
398 SkColor
Layer::SafeOpaqueBackgroundColor() const {
399 SkColor color
= background_color();
400 if (SkColorGetA(color
) == 255 && !contents_opaque()) {
401 color
= SK_ColorTRANSPARENT
;
402 } else if (SkColorGetA(color
) != 255 && contents_opaque()) {
403 for (const Layer
* layer
= parent(); layer
;
404 layer
= layer
->parent()) {
405 color
= layer
->background_color();
406 if (SkColorGetA(color
) == 255)
409 if (SkColorGetA(color
) != 255)
410 color
= layer_tree_host_
->background_color();
411 if (SkColorGetA(color
) != 255)
412 color
= SkColorSetA(color
, 255);
417 void Layer::CalculateContentsScale(float ideal_contents_scale
,
418 float device_scale_factor
,
419 float page_scale_factor
,
420 float maximum_animation_contents_scale
,
421 bool animating_transform_to_screen
,
422 float* contents_scale_x
,
423 float* contents_scale_y
,
424 gfx::Size
* content_bounds
) {
425 DCHECK(layer_tree_host_
);
427 *contents_scale_x
= 1;
428 *contents_scale_y
= 1;
429 *content_bounds
= bounds();
432 void Layer::SetMasksToBounds(bool masks_to_bounds
) {
433 DCHECK(IsPropertyChangeAllowed());
434 if (masks_to_bounds_
== masks_to_bounds
)
436 masks_to_bounds_
= masks_to_bounds
;
440 void Layer::SetMaskLayer(Layer
* mask_layer
) {
441 DCHECK(IsPropertyChangeAllowed());
442 if (mask_layer_
.get() == mask_layer
)
444 if (mask_layer_
.get()) {
445 DCHECK_EQ(this, mask_layer_
->parent());
446 mask_layer_
->RemoveFromParent();
448 mask_layer_
= mask_layer
;
449 if (mask_layer_
.get()) {
450 DCHECK(!mask_layer_
->parent());
451 mask_layer_
->RemoveFromParent();
452 mask_layer_
->SetParent(this);
453 mask_layer_
->SetIsMask(true);
455 SetNeedsFullTreeSync();
458 void Layer::SetReplicaLayer(Layer
* layer
) {
459 DCHECK(IsPropertyChangeAllowed());
460 if (replica_layer_
.get() == layer
)
462 if (replica_layer_
.get()) {
463 DCHECK_EQ(this, replica_layer_
->parent());
464 replica_layer_
->RemoveFromParent();
466 replica_layer_
= layer
;
467 if (replica_layer_
.get()) {
468 DCHECK(!replica_layer_
->parent());
469 replica_layer_
->RemoveFromParent();
470 replica_layer_
->SetParent(this);
472 SetNeedsFullTreeSync();
475 void Layer::SetFilters(const FilterOperations
& filters
) {
476 DCHECK(IsPropertyChangeAllowed());
477 if (filters_
== filters
)
483 bool Layer::FilterIsAnimating() const {
484 return layer_animation_controller_
->IsAnimatingProperty(Animation::Filter
);
487 void Layer::SetBackgroundFilters(const FilterOperations
& filters
) {
488 DCHECK(IsPropertyChangeAllowed());
489 if (background_filters_
== filters
)
491 background_filters_
= filters
;
495 void Layer::SetOpacity(float opacity
) {
496 DCHECK(IsPropertyChangeAllowed());
497 if (opacity_
== opacity
)
503 bool Layer::OpacityIsAnimating() const {
504 return layer_animation_controller_
->IsAnimatingProperty(Animation::Opacity
);
507 bool Layer::OpacityCanAnimateOnImplThread() const {
511 void Layer::SetBlendMode(SkXfermode::Mode blend_mode
) {
512 DCHECK(IsPropertyChangeAllowed());
513 if (blend_mode_
== blend_mode
)
516 // Allowing only blend modes that are defined in the CSS Compositing standard:
517 // http://dev.w3.org/fxtf/compositing-1/#blending
518 switch (blend_mode
) {
519 case SkXfermode::kSrcOver_Mode
:
520 case SkXfermode::kScreen_Mode
:
521 case SkXfermode::kOverlay_Mode
:
522 case SkXfermode::kDarken_Mode
:
523 case SkXfermode::kLighten_Mode
:
524 case SkXfermode::kColorDodge_Mode
:
525 case SkXfermode::kColorBurn_Mode
:
526 case SkXfermode::kHardLight_Mode
:
527 case SkXfermode::kSoftLight_Mode
:
528 case SkXfermode::kDifference_Mode
:
529 case SkXfermode::kExclusion_Mode
:
530 case SkXfermode::kMultiply_Mode
:
531 case SkXfermode::kHue_Mode
:
532 case SkXfermode::kSaturation_Mode
:
533 case SkXfermode::kColor_Mode
:
534 case SkXfermode::kLuminosity_Mode
:
535 // supported blend modes
537 case SkXfermode::kClear_Mode
:
538 case SkXfermode::kSrc_Mode
:
539 case SkXfermode::kDst_Mode
:
540 case SkXfermode::kDstOver_Mode
:
541 case SkXfermode::kSrcIn_Mode
:
542 case SkXfermode::kDstIn_Mode
:
543 case SkXfermode::kSrcOut_Mode
:
544 case SkXfermode::kDstOut_Mode
:
545 case SkXfermode::kSrcATop_Mode
:
546 case SkXfermode::kDstATop_Mode
:
547 case SkXfermode::kXor_Mode
:
548 case SkXfermode::kPlus_Mode
:
549 case SkXfermode::kModulate_Mode
:
550 // Porter Duff Compositing Operators are not yet supported
551 // http://dev.w3.org/fxtf/compositing-1/#porterduffcompositingoperators
556 blend_mode_
= blend_mode
;
560 void Layer::SetIsRootForIsolatedGroup(bool root
) {
561 DCHECK(IsPropertyChangeAllowed());
562 if (is_root_for_isolated_group_
== root
)
564 is_root_for_isolated_group_
= root
;
568 void Layer::SetContentsOpaque(bool opaque
) {
569 DCHECK(IsPropertyChangeAllowed());
570 if (contents_opaque_
== opaque
)
572 contents_opaque_
= opaque
;
576 void Layer::SetPosition(const gfx::PointF
& position
) {
577 DCHECK(IsPropertyChangeAllowed());
578 if (position_
== position
)
580 position_
= position
;
584 bool Layer::IsContainerForFixedPositionLayers() const {
585 if (!transform_
.IsIdentityOrTranslation())
587 if (parent_
&& !parent_
->transform_
.IsIdentityOrTranslation())
589 return is_container_for_fixed_position_layers_
;
592 void Layer::SetTransform(const gfx::Transform
& transform
) {
593 DCHECK(IsPropertyChangeAllowed());
594 if (transform_
== transform
)
596 transform_
= transform
;
597 transform_is_invertible_
= transform
.IsInvertible();
601 bool Layer::TransformIsAnimating() const {
602 return layer_animation_controller_
->IsAnimatingProperty(Animation::Transform
);
605 void Layer::SetScrollParent(Layer
* parent
) {
606 DCHECK(IsPropertyChangeAllowed());
607 if (scroll_parent_
== parent
)
611 scroll_parent_
->RemoveScrollChild(this);
613 scroll_parent_
= parent
;
616 scroll_parent_
->AddScrollChild(this);
621 void Layer::AddScrollChild(Layer
* child
) {
622 if (!scroll_children_
)
623 scroll_children_
.reset(new std::set
<Layer
*>);
624 scroll_children_
->insert(child
);
628 void Layer::RemoveScrollChild(Layer
* child
) {
629 scroll_children_
->erase(child
);
630 if (scroll_children_
->empty())
631 scroll_children_
.reset();
635 void Layer::SetClipParent(Layer
* ancestor
) {
636 DCHECK(IsPropertyChangeAllowed());
637 if (clip_parent_
== ancestor
)
641 clip_parent_
->RemoveClipChild(this);
643 clip_parent_
= ancestor
;
646 clip_parent_
->AddClipChild(this);
651 void Layer::AddClipChild(Layer
* child
) {
653 clip_children_
.reset(new std::set
<Layer
*>);
654 clip_children_
->insert(child
);
658 void Layer::RemoveClipChild(Layer
* child
) {
659 clip_children_
->erase(child
);
660 if (clip_children_
->empty())
661 clip_children_
.reset();
665 void Layer::SetScrollOffset(gfx::Vector2d scroll_offset
) {
666 DCHECK(IsPropertyChangeAllowed());
668 if (scroll_offset_
== scroll_offset
)
670 scroll_offset_
= scroll_offset
;
674 void Layer::SetScrollOffsetFromImplSide(const gfx::Vector2d
& scroll_offset
) {
675 DCHECK(IsPropertyChangeAllowed());
676 // This function only gets called during a BeginMainFrame, so there
677 // is no need to call SetNeedsUpdate here.
678 DCHECK(layer_tree_host_
&& layer_tree_host_
->CommitRequested());
679 if (scroll_offset_
== scroll_offset
)
681 scroll_offset_
= scroll_offset
;
682 SetNeedsPushProperties();
683 if (!did_scroll_callback_
.is_null())
684 did_scroll_callback_
.Run();
685 // The callback could potentially change the layer structure:
686 // "this" may have been destroyed during the process.
689 void Layer::SetScrollClipLayerId(int clip_layer_id
) {
690 DCHECK(IsPropertyChangeAllowed());
691 if (scroll_clip_layer_id_
== clip_layer_id
)
693 scroll_clip_layer_id_
= clip_layer_id
;
697 void Layer::SetUserScrollable(bool horizontal
, bool vertical
) {
698 DCHECK(IsPropertyChangeAllowed());
699 if (user_scrollable_horizontal_
== horizontal
&&
700 user_scrollable_vertical_
== vertical
)
702 user_scrollable_horizontal_
= horizontal
;
703 user_scrollable_vertical_
= vertical
;
707 void Layer::SetShouldScrollOnMainThread(bool should_scroll_on_main_thread
) {
708 DCHECK(IsPropertyChangeAllowed());
709 if (should_scroll_on_main_thread_
== should_scroll_on_main_thread
)
711 should_scroll_on_main_thread_
= should_scroll_on_main_thread
;
715 void Layer::SetHaveWheelEventHandlers(bool have_wheel_event_handlers
) {
716 DCHECK(IsPropertyChangeAllowed());
717 if (have_wheel_event_handlers_
== have_wheel_event_handlers
)
719 have_wheel_event_handlers_
= have_wheel_event_handlers
;
723 void Layer::SetHaveScrollEventHandlers(bool have_scroll_event_handlers
) {
724 DCHECK(IsPropertyChangeAllowed());
725 if (have_scroll_event_handlers_
== have_scroll_event_handlers
)
727 have_scroll_event_handlers_
= have_scroll_event_handlers
;
731 void Layer::SetNonFastScrollableRegion(const Region
& region
) {
732 DCHECK(IsPropertyChangeAllowed());
733 if (non_fast_scrollable_region_
== region
)
735 non_fast_scrollable_region_
= region
;
739 void Layer::SetTouchEventHandlerRegion(const Region
& region
) {
740 DCHECK(IsPropertyChangeAllowed());
741 if (touch_event_handler_region_
== region
)
743 touch_event_handler_region_
= region
;
747 void Layer::SetDrawCheckerboardForMissingTiles(bool checkerboard
) {
748 DCHECK(IsPropertyChangeAllowed());
749 if (draw_checkerboard_for_missing_tiles_
== checkerboard
)
751 draw_checkerboard_for_missing_tiles_
= checkerboard
;
755 void Layer::SetForceRenderSurface(bool force
) {
756 DCHECK(IsPropertyChangeAllowed());
757 if (force_render_surface_
== force
)
759 force_render_surface_
= force
;
763 void Layer::SetDoubleSided(bool double_sided
) {
764 DCHECK(IsPropertyChangeAllowed());
765 if (double_sided_
== double_sided
)
767 double_sided_
= double_sided
;
771 void Layer::SetIs3dSorted(bool sorted
) {
772 DCHECK(IsPropertyChangeAllowed());
773 if (is_3d_sorted_
== sorted
)
775 is_3d_sorted_
= sorted
;
779 void Layer::SetShouldFlattenTransform(bool should_flatten
) {
780 DCHECK(IsPropertyChangeAllowed());
781 if (should_flatten_transform_
== should_flatten
)
783 should_flatten_transform_
= should_flatten
;
787 void Layer::SetIsDrawable(bool is_drawable
) {
788 DCHECK(IsPropertyChangeAllowed());
789 if (is_drawable_
== is_drawable
)
792 is_drawable_
= is_drawable
;
796 void Layer::SetHideLayerAndSubtree(bool hide
) {
797 DCHECK(IsPropertyChangeAllowed());
798 if (hide_layer_and_subtree_
== hide
)
801 hide_layer_and_subtree_
= hide
;
805 void Layer::SetNeedsDisplayRect(const gfx::RectF
& dirty_rect
) {
806 if (dirty_rect
.IsEmpty())
809 SetNeedsPushProperties();
810 update_rect_
.Union(dirty_rect
);
816 bool Layer::DescendantIsFixedToContainerLayer() const {
817 for (size_t i
= 0; i
< children_
.size(); ++i
) {
818 if (children_
[i
]->position_constraint_
.is_fixed_position() ||
819 children_
[i
]->DescendantIsFixedToContainerLayer())
825 void Layer::SetIsContainerForFixedPositionLayers(bool container
) {
826 if (is_container_for_fixed_position_layers_
== container
)
828 is_container_for_fixed_position_layers_
= container
;
830 if (layer_tree_host_
&& layer_tree_host_
->CommitRequested())
833 // Only request a commit if we have a fixed positioned descendant.
834 if (DescendantIsFixedToContainerLayer())
838 void Layer::SetPositionConstraint(const LayerPositionConstraint
& constraint
) {
839 DCHECK(IsPropertyChangeAllowed());
840 if (position_constraint_
== constraint
)
842 position_constraint_
= constraint
;
846 static void RunCopyCallbackOnMainThread(scoped_ptr
<CopyOutputRequest
> request
,
847 scoped_ptr
<CopyOutputResult
> result
) {
848 request
->SendResult(result
.Pass());
851 static void PostCopyCallbackToMainThread(
852 scoped_refptr
<base::SingleThreadTaskRunner
> main_thread_task_runner
,
853 scoped_ptr
<CopyOutputRequest
> request
,
854 scoped_ptr
<CopyOutputResult
> result
) {
855 main_thread_task_runner
->PostTask(FROM_HERE
,
856 base::Bind(&RunCopyCallbackOnMainThread
,
857 base::Passed(&request
),
858 base::Passed(&result
)));
861 void Layer::PushPropertiesTo(LayerImpl
* layer
) {
862 DCHECK(layer_tree_host_
);
864 // If we did not SavePaintProperties() for the layer this frame, then push the
865 // real property values, not the paint property values.
866 bool use_paint_properties
= paint_properties_
.source_frame_number
==
867 layer_tree_host_
->source_frame_number();
869 layer
->SetAnchorPoint(anchor_point_
);
870 layer
->SetAnchorPointZ(anchor_point_z_
);
871 layer
->SetBackgroundColor(background_color_
);
872 layer
->SetBounds(use_paint_properties
? paint_properties_
.bounds
874 layer
->SetContentBounds(content_bounds());
875 layer
->SetContentsScale(contents_scale_x(), contents_scale_y());
878 TRACE_EVENT_CATEGORY_GROUP_ENABLED(TRACE_DISABLED_BY_DEFAULT("cc.debug"),
881 layer
->SetDebugInfo(TakeDebugInfo());
883 layer
->SetDoubleSided(double_sided_
);
884 layer
->SetDrawCheckerboardForMissingTiles(
885 draw_checkerboard_for_missing_tiles_
);
886 layer
->SetForceRenderSurface(force_render_surface_
);
887 layer
->SetDrawsContent(DrawsContent());
888 layer
->SetHideLayerAndSubtree(hide_layer_and_subtree_
);
889 if (!layer
->FilterIsAnimatingOnImplOnly() && !FilterIsAnimating())
890 layer
->SetFilters(filters_
);
891 DCHECK(!(FilterIsAnimating() && layer
->FilterIsAnimatingOnImplOnly()));
892 layer
->SetBackgroundFilters(background_filters());
893 layer
->SetMasksToBounds(masks_to_bounds_
);
894 layer
->SetShouldScrollOnMainThread(should_scroll_on_main_thread_
);
895 layer
->SetHaveWheelEventHandlers(have_wheel_event_handlers_
);
896 layer
->SetHaveScrollEventHandlers(have_scroll_event_handlers_
);
897 layer
->SetNonFastScrollableRegion(non_fast_scrollable_region_
);
898 layer
->SetTouchEventHandlerRegion(touch_event_handler_region_
);
899 layer
->SetContentsOpaque(contents_opaque_
);
900 if (!layer
->OpacityIsAnimatingOnImplOnly() && !OpacityIsAnimating())
901 layer
->SetOpacity(opacity_
);
902 DCHECK(!(OpacityIsAnimating() && layer
->OpacityIsAnimatingOnImplOnly()));
903 layer
->SetBlendMode(blend_mode_
);
904 layer
->SetIsRootForIsolatedGroup(is_root_for_isolated_group_
);
905 layer
->SetPosition(position_
);
906 layer
->SetIsContainerForFixedPositionLayers(
907 IsContainerForFixedPositionLayers());
908 layer
->SetPositionConstraint(position_constraint_
);
909 layer
->SetShouldFlattenTransform(should_flatten_transform_
);
910 layer
->SetIs3dSorted(is_3d_sorted_
);
911 layer
->SetUseParentBackfaceVisibility(use_parent_backface_visibility_
);
912 if (!layer
->TransformIsAnimatingOnImplOnly() && !TransformIsAnimating())
913 layer
->SetTransformAndInvertibility(transform_
, transform_is_invertible_
);
914 DCHECK(!(TransformIsAnimating() && layer
->TransformIsAnimatingOnImplOnly()));
916 layer
->SetScrollClipLayer(scroll_clip_layer_id_
);
917 layer
->set_user_scrollable_horizontal(user_scrollable_horizontal_
);
918 layer
->set_user_scrollable_vertical(user_scrollable_vertical_
);
920 LayerImpl
* scroll_parent
= NULL
;
921 if (scroll_parent_
) {
922 scroll_parent
= layer
->layer_tree_impl()->LayerById(scroll_parent_
->id());
923 DCHECK(scroll_parent
);
926 layer
->SetScrollParent(scroll_parent
);
927 if (scroll_children_
) {
928 std::set
<LayerImpl
*>* scroll_children
= new std::set
<LayerImpl
*>;
929 for (std::set
<Layer
*>::iterator it
= scroll_children_
->begin();
930 it
!= scroll_children_
->end();
932 DCHECK_EQ((*it
)->scroll_parent(), this);
933 LayerImpl
* scroll_child
=
934 layer
->layer_tree_impl()->LayerById((*it
)->id());
935 DCHECK(scroll_child
);
936 scroll_children
->insert(scroll_child
);
938 layer
->SetScrollChildren(scroll_children
);
940 layer
->SetScrollChildren(NULL
);
943 LayerImpl
* clip_parent
= NULL
;
946 layer
->layer_tree_impl()->LayerById(clip_parent_
->id());
950 layer
->SetClipParent(clip_parent
);
951 if (clip_children_
) {
952 std::set
<LayerImpl
*>* clip_children
= new std::set
<LayerImpl
*>;
953 for (std::set
<Layer
*>::iterator it
= clip_children_
->begin();
954 it
!= clip_children_
->end(); ++it
) {
955 DCHECK_EQ((*it
)->clip_parent(), this);
956 LayerImpl
* clip_child
= layer
->layer_tree_impl()->LayerById((*it
)->id());
958 clip_children
->insert(clip_child
);
960 layer
->SetClipChildren(clip_children
);
962 layer
->SetClipChildren(NULL
);
965 // Adjust the scroll delta to be just the scrolls that have happened since
966 // the BeginMainFrame was sent. This happens for impl-side painting
967 // in LayerImpl::ApplyScrollDeltasSinceBeginMainFrame in a separate tree walk.
968 if (layer
->layer_tree_impl()->settings().impl_side_painting
) {
969 layer
->SetScrollOffset(scroll_offset_
);
971 layer
->SetScrollOffsetAndDelta(
972 scroll_offset_
, layer
->ScrollDelta() - layer
->sent_scroll_delta());
973 layer
->SetSentScrollDelta(gfx::Vector2d());
976 // Wrap the copy_requests_ in a PostTask to the main thread.
977 ScopedPtrVector
<CopyOutputRequest
> main_thread_copy_requests
;
978 for (ScopedPtrVector
<CopyOutputRequest
>::iterator it
= copy_requests_
.begin();
979 it
!= copy_requests_
.end();
981 scoped_refptr
<base::SingleThreadTaskRunner
> main_thread_task_runner
=
982 layer_tree_host()->proxy()->MainThreadTaskRunner();
983 scoped_ptr
<CopyOutputRequest
> original_request
= copy_requests_
.take(it
);
984 const CopyOutputRequest
& original_request_ref
= *original_request
;
985 scoped_ptr
<CopyOutputRequest
> main_thread_request
=
986 CopyOutputRequest::CreateRelayRequest(
987 original_request_ref
,
988 base::Bind(&PostCopyCallbackToMainThread
,
989 main_thread_task_runner
,
990 base::Passed(&original_request
)));
991 main_thread_copy_requests
.push_back(main_thread_request
.Pass());
993 copy_requests_
.clear();
994 layer
->PassCopyRequests(&main_thread_copy_requests
);
996 // If the main thread commits multiple times before the impl thread actually
997 // draws, then damage tracking will become incorrect if we simply clobber the
998 // update_rect here. The LayerImpl's update_rect needs to accumulate (i.e.
999 // union) any update changes that have occurred on the main thread.
1000 update_rect_
.Union(layer
->update_rect());
1001 layer
->SetUpdateRect(update_rect_
);
1003 layer
->SetStackingOrderChanged(stacking_order_changed_
);
1005 layer_animation_controller_
->PushAnimationUpdatesTo(
1006 layer
->layer_animation_controller());
1008 // Reset any state that should be cleared for the next update.
1009 stacking_order_changed_
= false;
1010 update_rect_
= gfx::RectF();
1012 needs_push_properties_
= false;
1013 num_dependents_need_push_properties_
= 0;
1016 scoped_ptr
<LayerImpl
> Layer::CreateLayerImpl(LayerTreeImpl
* tree_impl
) {
1017 return LayerImpl::Create(tree_impl
, layer_id_
);
1020 bool Layer::DrawsContent() const {
1021 return is_drawable_
;
1024 void Layer::SavePaintProperties() {
1025 DCHECK(layer_tree_host_
);
1027 // TODO(reveman): Save all layer properties that we depend on not
1028 // changing until PushProperties() has been called. crbug.com/231016
1029 paint_properties_
.bounds
= bounds_
;
1030 paint_properties_
.source_frame_number
=
1031 layer_tree_host_
->source_frame_number();
1034 bool Layer::Update(ResourceUpdateQueue
* queue
,
1035 const OcclusionTracker
<Layer
>* occlusion
) {
1036 DCHECK(layer_tree_host_
);
1037 DCHECK_EQ(layer_tree_host_
->source_frame_number(),
1038 paint_properties_
.source_frame_number
) <<
1039 "SavePaintProperties must be called for any layer that is painted.";
1043 bool Layer::NeedMoreUpdates() {
1047 bool Layer::IsSuitableForGpuRasterization() const {
1051 scoped_refptr
<base::debug::ConvertableToTraceFormat
> Layer::TakeDebugInfo() {
1053 return client_
->TakeDebugInfo();
1058 void Layer::CreateRenderSurface() {
1059 DCHECK(!draw_properties_
.render_surface
);
1060 draw_properties_
.render_surface
= make_scoped_ptr(new RenderSurface(this));
1061 draw_properties_
.render_target
= this;
1064 void Layer::ClearRenderSurface() {
1065 draw_properties_
.render_surface
.reset();
1068 void Layer::ClearRenderSurfaceLayerList() {
1069 if (draw_properties_
.render_surface
)
1070 draw_properties_
.render_surface
->layer_list().clear();
1073 gfx::Vector2dF
Layer::ScrollOffsetForAnimation() const {
1074 return TotalScrollOffset();
1077 // On<Property>Animated is called due to an ongoing accelerated animation.
1078 // Since this animation is also being run on the compositor thread, there
1079 // is no need to request a commit to push this value over, so the value is
1080 // set directly rather than by calling Set<Property>.
1081 void Layer::OnFilterAnimated(const FilterOperations
& filters
) {
1085 void Layer::OnOpacityAnimated(float opacity
) {
1089 void Layer::OnTransformAnimated(const gfx::Transform
& transform
) {
1090 if (transform_
== transform
)
1092 transform_
= transform
;
1093 transform_is_invertible_
= transform
.IsInvertible();
1096 void Layer::OnScrollOffsetAnimated(const gfx::Vector2dF
& scroll_offset
) {
1097 // Do nothing. Scroll deltas will be sent from the compositor thread back
1098 // to the main thread in the same manner as during non-animated
1099 // compositor-driven scrolling.
1102 void Layer::OnAnimationWaitingForDeletion() {
1103 // Animations are only deleted during PushProperties.
1104 SetNeedsPushProperties();
1107 bool Layer::IsActive() const {
1111 bool Layer::AddAnimation(scoped_ptr
<Animation
> animation
) {
1112 if (!layer_animation_controller_
->animation_registrar())
1115 UMA_HISTOGRAM_BOOLEAN("Renderer.AnimationAddedToOrphanLayer",
1117 layer_animation_controller_
->AddAnimation(animation
.Pass());
1122 void Layer::PauseAnimation(int animation_id
, double time_offset
) {
1123 layer_animation_controller_
->PauseAnimation(
1124 animation_id
, base::TimeDelta::FromSecondsD(time_offset
));
1128 void Layer::RemoveAnimation(int animation_id
) {
1129 layer_animation_controller_
->RemoveAnimation(animation_id
);
1133 void Layer::SetLayerAnimationControllerForTest(
1134 scoped_refptr
<LayerAnimationController
> controller
) {
1135 layer_animation_controller_
->RemoveValueObserver(this);
1136 layer_animation_controller_
= controller
;
1137 layer_animation_controller_
->AddValueObserver(this);
1141 bool Layer::HasActiveAnimation() const {
1142 return layer_animation_controller_
->HasActiveAnimation();
1145 void Layer::AddLayerAnimationEventObserver(
1146 LayerAnimationEventObserver
* animation_observer
) {
1147 layer_animation_controller_
->AddEventObserver(animation_observer
);
1150 void Layer::RemoveLayerAnimationEventObserver(
1151 LayerAnimationEventObserver
* animation_observer
) {
1152 layer_animation_controller_
->RemoveEventObserver(animation_observer
);
1155 Region
Layer::VisibleContentOpaqueRegion() const {
1156 if (contents_opaque())
1157 return visible_content_rect();
1161 ScrollbarLayerInterface
* Layer::ToScrollbarLayer() {
1165 RenderingStatsInstrumentation
* Layer::rendering_stats_instrumentation() const {
1166 return layer_tree_host_
->rendering_stats_instrumentation();
1169 bool Layer::SupportsLCDText() const {
1173 void Layer::RemoveFromScrollTree() {
1174 if (scroll_children_
.get()) {
1175 for (std::set
<Layer
*>::iterator it
= scroll_children_
->begin();
1176 it
!= scroll_children_
->end(); ++it
)
1177 (*it
)->scroll_parent_
= NULL
;
1181 scroll_parent_
->RemoveScrollChild(this);
1183 scroll_parent_
= NULL
;
1186 void Layer::RemoveFromClipTree() {
1187 if (clip_children_
.get()) {
1188 for (std::set
<Layer
*>::iterator it
= clip_children_
->begin();
1189 it
!= clip_children_
->end(); ++it
)
1190 (*it
)->clip_parent_
= NULL
;
1194 clip_parent_
->RemoveClipChild(this);
1196 clip_parent_
= NULL
;
1199 void Layer::RunMicroBenchmark(MicroBenchmark
* benchmark
) {
1200 benchmark
->RunOnLayer(this);