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/base/simple_enclosed_region.h"
21 #include "cc/layers/layer_client.h"
22 #include "cc/layers/layer_impl.h"
23 #include "cc/layers/scrollbar_layer_interface.h"
24 #include "cc/output/copy_output_request.h"
25 #include "cc/output/copy_output_result.h"
26 #include "cc/trees/layer_tree_host.h"
27 #include "cc/trees/layer_tree_impl.h"
28 #include "third_party/skia/include/core/SkImageFilter.h"
29 #include "ui/gfx/geometry/vector2d_conversions.h"
30 #include "ui/gfx/rect_conversions.h"
34 base::StaticAtomicSequenceNumber g_next_layer_id
;
36 scoped_refptr
<Layer
> Layer::Create() {
37 return make_scoped_refptr(new Layer());
41 : needs_push_properties_(false),
42 num_dependents_need_push_properties_(false),
43 stacking_order_changed_(false),
44 // Layer IDs start from 1.
45 layer_id_(g_next_layer_id
.GetNext() + 1),
46 ignore_set_needs_commit_(false),
47 sorting_context_id_(0),
49 layer_tree_host_(NULL
),
50 scroll_clip_layer_id_(INVALID_ID
),
51 num_descendants_that_draw_content_(0),
52 should_scroll_on_main_thread_(false),
53 have_wheel_event_handlers_(false),
54 have_scroll_event_handlers_(false),
55 user_scrollable_horizontal_(true),
56 user_scrollable_vertical_(true),
57 is_root_for_isolated_group_(false),
58 is_container_for_fixed_position_layers_(false),
60 draws_content_(false),
61 hide_layer_and_subtree_(false),
62 masks_to_bounds_(false),
63 contents_opaque_(false),
65 should_flatten_transform_(true),
66 use_parent_backface_visibility_(false),
67 draw_checkerboard_for_missing_tiles_(false),
68 force_render_surface_(false),
69 transform_is_invertible_(true),
72 blend_mode_(SkXfermode::kSrcOver_Mode
),
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 RemoveFromScrollTree();
97 // Remove the parent reference from all children and dependents.
99 if (mask_layer_
.get()) {
100 DCHECK_EQ(this, mask_layer_
->parent());
101 mask_layer_
->RemoveFromParent();
103 if (replica_layer_
.get()) {
104 DCHECK_EQ(this, replica_layer_
->parent());
105 replica_layer_
->RemoveFromParent();
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 AddDrawableDescendants(child
->NumDescendantsThatDrawContent() +
252 (child
->DrawsContent() ? 1 : 0));
253 child
->SetParent(this);
254 child
->stacking_order_changed_
= true;
256 index
= std::min(index
, children_
.size());
257 children_
.insert(children_
.begin() + index
, child
);
258 SetNeedsFullTreeSync();
261 void Layer::RemoveFromParent() {
262 DCHECK(IsPropertyChangeAllowed());
264 parent_
->RemoveChildOrDependent(this);
267 void Layer::RemoveChildOrDependent(Layer
* child
) {
268 if (mask_layer_
.get() == child
) {
269 mask_layer_
->SetParent(NULL
);
271 SetNeedsFullTreeSync();
274 if (replica_layer_
.get() == child
) {
275 replica_layer_
->SetParent(NULL
);
276 replica_layer_
= NULL
;
277 SetNeedsFullTreeSync();
281 for (LayerList::iterator iter
= children_
.begin();
282 iter
!= children_
.end();
284 if (iter
->get() != child
)
287 child
->SetParent(NULL
);
288 AddDrawableDescendants(-child
->NumDescendantsThatDrawContent() -
289 (child
->DrawsContent() ? 1 : 0));
290 children_
.erase(iter
);
291 SetNeedsFullTreeSync();
296 void Layer::ReplaceChild(Layer
* reference
, scoped_refptr
<Layer
> new_layer
) {
298 DCHECK_EQ(reference
->parent(), this);
299 DCHECK(IsPropertyChangeAllowed());
301 if (reference
== new_layer
.get())
304 int reference_index
= IndexOfChild(reference
);
305 if (reference_index
== -1) {
310 reference
->RemoveFromParent();
312 if (new_layer
.get()) {
313 new_layer
->RemoveFromParent();
314 InsertChild(new_layer
, reference_index
);
318 int Layer::IndexOfChild(const Layer
* reference
) {
319 for (size_t i
= 0; i
< children_
.size(); ++i
) {
320 if (children_
[i
].get() == reference
)
326 void Layer::SetBounds(const gfx::Size
& size
) {
327 DCHECK(IsPropertyChangeAllowed());
328 if (bounds() == size
)
335 Layer
* Layer::RootLayer() {
337 while (layer
->parent())
338 layer
= layer
->parent();
342 void Layer::RemoveAllChildren() {
343 DCHECK(IsPropertyChangeAllowed());
344 while (children_
.size()) {
345 Layer
* layer
= children_
[0].get();
346 DCHECK_EQ(this, layer
->parent());
347 layer
->RemoveFromParent();
351 void Layer::SetChildren(const LayerList
& children
) {
352 DCHECK(IsPropertyChangeAllowed());
353 if (children
== children_
)
357 for (size_t i
= 0; i
< children
.size(); ++i
)
358 AddChild(children
[i
]);
361 bool Layer::HasAncestor(const Layer
* ancestor
) const {
362 for (const Layer
* layer
= parent(); layer
; layer
= layer
->parent()) {
363 if (layer
== ancestor
)
369 void Layer::RequestCopyOfOutput(
370 scoped_ptr
<CopyOutputRequest
> request
) {
371 DCHECK(IsPropertyChangeAllowed());
372 if (request
->IsEmpty())
374 copy_requests_
.push_back(request
.Pass());
378 void Layer::SetBackgroundColor(SkColor background_color
) {
379 DCHECK(IsPropertyChangeAllowed());
380 if (background_color_
== background_color
)
382 background_color_
= background_color
;
386 SkColor
Layer::SafeOpaqueBackgroundColor() const {
387 SkColor color
= background_color();
388 if (SkColorGetA(color
) == 255 && !contents_opaque()) {
389 color
= SK_ColorTRANSPARENT
;
390 } else if (SkColorGetA(color
) != 255 && contents_opaque()) {
391 for (const Layer
* layer
= parent(); layer
;
392 layer
= layer
->parent()) {
393 color
= layer
->background_color();
394 if (SkColorGetA(color
) == 255)
397 if (SkColorGetA(color
) != 255)
398 color
= layer_tree_host_
->background_color();
399 if (SkColorGetA(color
) != 255)
400 color
= SkColorSetA(color
, 255);
405 void Layer::CalculateContentsScale(float ideal_contents_scale
,
406 float* contents_scale_x
,
407 float* contents_scale_y
,
408 gfx::Size
* content_bounds
) {
409 DCHECK(layer_tree_host_
);
411 *contents_scale_x
= 1;
412 *contents_scale_y
= 1;
413 *content_bounds
= bounds();
416 void Layer::SetMasksToBounds(bool masks_to_bounds
) {
417 DCHECK(IsPropertyChangeAllowed());
418 if (masks_to_bounds_
== masks_to_bounds
)
420 masks_to_bounds_
= masks_to_bounds
;
424 void Layer::SetMaskLayer(Layer
* mask_layer
) {
425 DCHECK(IsPropertyChangeAllowed());
426 if (mask_layer_
.get() == mask_layer
)
428 if (mask_layer_
.get()) {
429 DCHECK_EQ(this, mask_layer_
->parent());
430 mask_layer_
->RemoveFromParent();
432 mask_layer_
= mask_layer
;
433 if (mask_layer_
.get()) {
434 DCHECK(!mask_layer_
->parent());
435 mask_layer_
->RemoveFromParent();
436 mask_layer_
->SetParent(this);
437 mask_layer_
->SetIsMask(true);
439 SetNeedsFullTreeSync();
442 void Layer::SetReplicaLayer(Layer
* layer
) {
443 DCHECK(IsPropertyChangeAllowed());
444 if (replica_layer_
.get() == layer
)
446 if (replica_layer_
.get()) {
447 DCHECK_EQ(this, replica_layer_
->parent());
448 replica_layer_
->RemoveFromParent();
450 replica_layer_
= layer
;
451 if (replica_layer_
.get()) {
452 DCHECK(!replica_layer_
->parent());
453 replica_layer_
->RemoveFromParent();
454 replica_layer_
->SetParent(this);
456 SetNeedsFullTreeSync();
459 void Layer::SetFilters(const FilterOperations
& filters
) {
460 DCHECK(IsPropertyChangeAllowed());
461 if (filters_
== filters
)
467 bool Layer::FilterIsAnimating() const {
468 return layer_animation_controller_
->IsAnimatingProperty(Animation::Filter
);
471 void Layer::SetBackgroundFilters(const FilterOperations
& filters
) {
472 DCHECK(IsPropertyChangeAllowed());
473 if (background_filters_
== filters
)
475 background_filters_
= filters
;
479 void Layer::SetOpacity(float opacity
) {
480 DCHECK(IsPropertyChangeAllowed());
481 if (opacity_
== opacity
)
487 bool Layer::OpacityIsAnimating() const {
488 return layer_animation_controller_
->IsAnimatingProperty(Animation::Opacity
);
491 bool Layer::OpacityCanAnimateOnImplThread() const {
495 void Layer::SetBlendMode(SkXfermode::Mode blend_mode
) {
496 DCHECK(IsPropertyChangeAllowed());
497 if (blend_mode_
== blend_mode
)
500 // Allowing only blend modes that are defined in the CSS Compositing standard:
501 // http://dev.w3.org/fxtf/compositing-1/#blending
502 switch (blend_mode
) {
503 case SkXfermode::kSrcOver_Mode
:
504 case SkXfermode::kScreen_Mode
:
505 case SkXfermode::kOverlay_Mode
:
506 case SkXfermode::kDarken_Mode
:
507 case SkXfermode::kLighten_Mode
:
508 case SkXfermode::kColorDodge_Mode
:
509 case SkXfermode::kColorBurn_Mode
:
510 case SkXfermode::kHardLight_Mode
:
511 case SkXfermode::kSoftLight_Mode
:
512 case SkXfermode::kDifference_Mode
:
513 case SkXfermode::kExclusion_Mode
:
514 case SkXfermode::kMultiply_Mode
:
515 case SkXfermode::kHue_Mode
:
516 case SkXfermode::kSaturation_Mode
:
517 case SkXfermode::kColor_Mode
:
518 case SkXfermode::kLuminosity_Mode
:
519 // supported blend modes
521 case SkXfermode::kClear_Mode
:
522 case SkXfermode::kSrc_Mode
:
523 case SkXfermode::kDst_Mode
:
524 case SkXfermode::kDstOver_Mode
:
525 case SkXfermode::kSrcIn_Mode
:
526 case SkXfermode::kDstIn_Mode
:
527 case SkXfermode::kSrcOut_Mode
:
528 case SkXfermode::kDstOut_Mode
:
529 case SkXfermode::kSrcATop_Mode
:
530 case SkXfermode::kDstATop_Mode
:
531 case SkXfermode::kXor_Mode
:
532 case SkXfermode::kPlus_Mode
:
533 case SkXfermode::kModulate_Mode
:
534 // Porter Duff Compositing Operators are not yet supported
535 // http://dev.w3.org/fxtf/compositing-1/#porterduffcompositingoperators
540 blend_mode_
= blend_mode
;
544 void Layer::SetIsRootForIsolatedGroup(bool root
) {
545 DCHECK(IsPropertyChangeAllowed());
546 if (is_root_for_isolated_group_
== root
)
548 is_root_for_isolated_group_
= root
;
552 void Layer::SetContentsOpaque(bool opaque
) {
553 DCHECK(IsPropertyChangeAllowed());
554 if (contents_opaque_
== opaque
)
556 contents_opaque_
= opaque
;
560 void Layer::SetPosition(const gfx::PointF
& position
) {
561 DCHECK(IsPropertyChangeAllowed());
562 if (position_
== position
)
564 position_
= position
;
568 bool Layer::IsContainerForFixedPositionLayers() const {
569 if (!transform_
.IsIdentityOrTranslation())
571 if (parent_
&& !parent_
->transform_
.IsIdentityOrTranslation())
573 return is_container_for_fixed_position_layers_
;
576 void Layer::SetTransform(const gfx::Transform
& transform
) {
577 DCHECK(IsPropertyChangeAllowed());
578 if (transform_
== transform
)
580 transform_
= transform
;
581 transform_is_invertible_
= transform
.IsInvertible();
585 void Layer::SetTransformOrigin(const gfx::Point3F
& transform_origin
) {
586 DCHECK(IsPropertyChangeAllowed());
587 if (transform_origin_
== transform_origin
)
589 transform_origin_
= transform_origin
;
593 bool Layer::TransformIsAnimating() const {
594 return layer_animation_controller_
->IsAnimatingProperty(Animation::Transform
);
597 void Layer::SetScrollParent(Layer
* parent
) {
598 DCHECK(IsPropertyChangeAllowed());
599 if (scroll_parent_
== parent
)
603 scroll_parent_
->RemoveScrollChild(this);
605 scroll_parent_
= parent
;
608 scroll_parent_
->AddScrollChild(this);
613 void Layer::AddScrollChild(Layer
* child
) {
614 if (!scroll_children_
)
615 scroll_children_
.reset(new std::set
<Layer
*>);
616 scroll_children_
->insert(child
);
620 void Layer::RemoveScrollChild(Layer
* child
) {
621 scroll_children_
->erase(child
);
622 if (scroll_children_
->empty())
623 scroll_children_
.reset();
627 void Layer::SetClipParent(Layer
* ancestor
) {
628 DCHECK(IsPropertyChangeAllowed());
629 if (clip_parent_
== ancestor
)
633 clip_parent_
->RemoveClipChild(this);
635 clip_parent_
= ancestor
;
638 clip_parent_
->AddClipChild(this);
643 void Layer::AddClipChild(Layer
* child
) {
645 clip_children_
.reset(new std::set
<Layer
*>);
646 clip_children_
->insert(child
);
650 void Layer::RemoveClipChild(Layer
* child
) {
651 clip_children_
->erase(child
);
652 if (clip_children_
->empty())
653 clip_children_
.reset();
657 void Layer::SetScrollOffset(gfx::Vector2d scroll_offset
) {
658 DCHECK(IsPropertyChangeAllowed());
660 if (scroll_offset_
== scroll_offset
)
662 scroll_offset_
= scroll_offset
;
666 void Layer::SetScrollOffsetFromImplSide(const gfx::Vector2d
& scroll_offset
) {
667 DCHECK(IsPropertyChangeAllowed());
668 // This function only gets called during a BeginMainFrame, so there
669 // is no need to call SetNeedsUpdate here.
670 DCHECK(layer_tree_host_
&& layer_tree_host_
->CommitRequested());
671 if (scroll_offset_
== scroll_offset
)
673 scroll_offset_
= scroll_offset
;
674 SetNeedsPushProperties();
675 if (!did_scroll_callback_
.is_null())
676 did_scroll_callback_
.Run();
677 // The callback could potentially change the layer structure:
678 // "this" may have been destroyed during the process.
681 void Layer::SetScrollClipLayerId(int clip_layer_id
) {
682 DCHECK(IsPropertyChangeAllowed());
683 if (scroll_clip_layer_id_
== clip_layer_id
)
685 scroll_clip_layer_id_
= clip_layer_id
;
689 void Layer::SetUserScrollable(bool horizontal
, bool vertical
) {
690 DCHECK(IsPropertyChangeAllowed());
691 if (user_scrollable_horizontal_
== horizontal
&&
692 user_scrollable_vertical_
== vertical
)
694 user_scrollable_horizontal_
= horizontal
;
695 user_scrollable_vertical_
= vertical
;
699 void Layer::SetShouldScrollOnMainThread(bool should_scroll_on_main_thread
) {
700 DCHECK(IsPropertyChangeAllowed());
701 if (should_scroll_on_main_thread_
== should_scroll_on_main_thread
)
703 should_scroll_on_main_thread_
= should_scroll_on_main_thread
;
707 void Layer::SetHaveWheelEventHandlers(bool have_wheel_event_handlers
) {
708 DCHECK(IsPropertyChangeAllowed());
709 if (have_wheel_event_handlers_
== have_wheel_event_handlers
)
711 have_wheel_event_handlers_
= have_wheel_event_handlers
;
715 void Layer::SetHaveScrollEventHandlers(bool have_scroll_event_handlers
) {
716 DCHECK(IsPropertyChangeAllowed());
717 if (have_scroll_event_handlers_
== have_scroll_event_handlers
)
719 have_scroll_event_handlers_
= have_scroll_event_handlers
;
723 void Layer::SetNonFastScrollableRegion(const Region
& region
) {
724 DCHECK(IsPropertyChangeAllowed());
725 if (non_fast_scrollable_region_
== region
)
727 non_fast_scrollable_region_
= region
;
731 void Layer::SetTouchEventHandlerRegion(const Region
& region
) {
732 DCHECK(IsPropertyChangeAllowed());
733 if (touch_event_handler_region_
== region
)
735 touch_event_handler_region_
= region
;
739 void Layer::SetDrawCheckerboardForMissingTiles(bool checkerboard
) {
740 DCHECK(IsPropertyChangeAllowed());
741 if (draw_checkerboard_for_missing_tiles_
== checkerboard
)
743 draw_checkerboard_for_missing_tiles_
= checkerboard
;
747 void Layer::SetForceRenderSurface(bool force
) {
748 DCHECK(IsPropertyChangeAllowed());
749 if (force_render_surface_
== force
)
751 force_render_surface_
= force
;
755 void Layer::SetDoubleSided(bool double_sided
) {
756 DCHECK(IsPropertyChangeAllowed());
757 if (double_sided_
== double_sided
)
759 double_sided_
= double_sided
;
763 void Layer::Set3dSortingContextId(int id
) {
764 DCHECK(IsPropertyChangeAllowed());
765 if (id
== sorting_context_id_
)
767 sorting_context_id_
= id
;
771 void Layer::SetShouldFlattenTransform(bool should_flatten
) {
772 DCHECK(IsPropertyChangeAllowed());
773 if (should_flatten_transform_
== should_flatten
)
775 should_flatten_transform_
= should_flatten
;
779 void Layer::SetIsDrawable(bool is_drawable
) {
780 DCHECK(IsPropertyChangeAllowed());
781 if (is_drawable_
== is_drawable
)
784 is_drawable_
= is_drawable
;
785 UpdateDrawsContent(HasDrawableContent());
788 void Layer::SetHideLayerAndSubtree(bool hide
) {
789 DCHECK(IsPropertyChangeAllowed());
790 if (hide_layer_and_subtree_
== hide
)
793 hide_layer_and_subtree_
= hide
;
797 void Layer::SetNeedsDisplayRect(const gfx::RectF
& dirty_rect
) {
798 if (dirty_rect
.IsEmpty())
801 SetNeedsPushProperties();
802 update_rect_
.Union(dirty_rect
);
808 bool Layer::DescendantIsFixedToContainerLayer() const {
809 for (size_t i
= 0; i
< children_
.size(); ++i
) {
810 if (children_
[i
]->position_constraint_
.is_fixed_position() ||
811 children_
[i
]->DescendantIsFixedToContainerLayer())
817 void Layer::SetIsContainerForFixedPositionLayers(bool container
) {
818 if (is_container_for_fixed_position_layers_
== container
)
820 is_container_for_fixed_position_layers_
= container
;
822 if (layer_tree_host_
&& layer_tree_host_
->CommitRequested())
825 // Only request a commit if we have a fixed positioned descendant.
826 if (DescendantIsFixedToContainerLayer())
830 void Layer::SetPositionConstraint(const LayerPositionConstraint
& constraint
) {
831 DCHECK(IsPropertyChangeAllowed());
832 if (position_constraint_
== constraint
)
834 position_constraint_
= constraint
;
838 static void RunCopyCallbackOnMainThread(scoped_ptr
<CopyOutputRequest
> request
,
839 scoped_ptr
<CopyOutputResult
> result
) {
840 request
->SendResult(result
.Pass());
843 static void PostCopyCallbackToMainThread(
844 scoped_refptr
<base::SingleThreadTaskRunner
> main_thread_task_runner
,
845 scoped_ptr
<CopyOutputRequest
> request
,
846 scoped_ptr
<CopyOutputResult
> result
) {
847 main_thread_task_runner
->PostTask(FROM_HERE
,
848 base::Bind(&RunCopyCallbackOnMainThread
,
849 base::Passed(&request
),
850 base::Passed(&result
)));
853 void Layer::PushPropertiesTo(LayerImpl
* layer
) {
854 DCHECK(layer_tree_host_
);
856 // If we did not SavePaintProperties() for the layer this frame, then push the
857 // real property values, not the paint property values.
858 bool use_paint_properties
= paint_properties_
.source_frame_number
==
859 layer_tree_host_
->source_frame_number();
861 layer
->SetTransformOrigin(transform_origin_
);
862 layer
->SetBackgroundColor(background_color_
);
863 layer
->SetBounds(use_paint_properties
? paint_properties_
.bounds
865 layer
->SetContentBounds(content_bounds());
866 layer
->SetContentsScale(contents_scale_x(), contents_scale_y());
869 TRACE_EVENT_CATEGORY_GROUP_ENABLED(
870 TRACE_DISABLED_BY_DEFAULT("cc.debug") "," TRACE_DISABLED_BY_DEFAULT(
871 "devtools.timeline.layers"),
874 layer
->SetDebugInfo(TakeDebugInfo());
876 layer
->SetDoubleSided(double_sided_
);
877 layer
->SetDrawCheckerboardForMissingTiles(
878 draw_checkerboard_for_missing_tiles_
);
879 layer
->SetForceRenderSurface(force_render_surface_
);
880 layer
->SetDrawsContent(DrawsContent());
881 layer
->SetHideLayerAndSubtree(hide_layer_and_subtree_
);
882 if (!layer
->FilterIsAnimatingOnImplOnly() && !FilterIsAnimating())
883 layer
->SetFilters(filters_
);
884 DCHECK(!(FilterIsAnimating() && layer
->FilterIsAnimatingOnImplOnly()));
885 layer
->SetBackgroundFilters(background_filters());
886 layer
->SetMasksToBounds(masks_to_bounds_
);
887 layer
->SetShouldScrollOnMainThread(should_scroll_on_main_thread_
);
888 layer
->SetHaveWheelEventHandlers(have_wheel_event_handlers_
);
889 layer
->SetHaveScrollEventHandlers(have_scroll_event_handlers_
);
890 layer
->SetNonFastScrollableRegion(non_fast_scrollable_region_
);
891 layer
->SetTouchEventHandlerRegion(touch_event_handler_region_
);
892 layer
->SetContentsOpaque(contents_opaque_
);
893 if (!layer
->OpacityIsAnimatingOnImplOnly() && !OpacityIsAnimating())
894 layer
->SetOpacity(opacity_
);
895 DCHECK(!(OpacityIsAnimating() && layer
->OpacityIsAnimatingOnImplOnly()));
896 layer
->SetBlendMode(blend_mode_
);
897 layer
->SetIsRootForIsolatedGroup(is_root_for_isolated_group_
);
898 layer
->SetPosition(position_
);
899 layer
->SetIsContainerForFixedPositionLayers(
900 IsContainerForFixedPositionLayers());
901 layer
->SetPositionConstraint(position_constraint_
);
902 layer
->SetShouldFlattenTransform(should_flatten_transform_
);
903 layer
->SetUseParentBackfaceVisibility(use_parent_backface_visibility_
);
904 if (!layer
->TransformIsAnimatingOnImplOnly() && !TransformIsAnimating())
905 layer
->SetTransformAndInvertibility(transform_
, transform_is_invertible_
);
906 DCHECK(!(TransformIsAnimating() && layer
->TransformIsAnimatingOnImplOnly()));
907 layer
->Set3dSortingContextId(sorting_context_id_
);
908 layer
->SetNumDescendantsThatDrawContent(num_descendants_that_draw_content_
);
910 layer
->SetScrollClipLayer(scroll_clip_layer_id_
);
911 layer
->set_user_scrollable_horizontal(user_scrollable_horizontal_
);
912 layer
->set_user_scrollable_vertical(user_scrollable_vertical_
);
914 LayerImpl
* scroll_parent
= NULL
;
915 if (scroll_parent_
) {
916 scroll_parent
= layer
->layer_tree_impl()->LayerById(scroll_parent_
->id());
917 DCHECK(scroll_parent
);
920 layer
->SetScrollParent(scroll_parent
);
921 if (scroll_children_
) {
922 std::set
<LayerImpl
*>* scroll_children
= new std::set
<LayerImpl
*>;
923 for (std::set
<Layer
*>::iterator it
= scroll_children_
->begin();
924 it
!= scroll_children_
->end();
926 DCHECK_EQ((*it
)->scroll_parent(), this);
927 LayerImpl
* scroll_child
=
928 layer
->layer_tree_impl()->LayerById((*it
)->id());
929 DCHECK(scroll_child
);
930 scroll_children
->insert(scroll_child
);
932 layer
->SetScrollChildren(scroll_children
);
934 layer
->SetScrollChildren(NULL
);
937 LayerImpl
* clip_parent
= NULL
;
940 layer
->layer_tree_impl()->LayerById(clip_parent_
->id());
944 layer
->SetClipParent(clip_parent
);
945 if (clip_children_
) {
946 std::set
<LayerImpl
*>* clip_children
= new std::set
<LayerImpl
*>;
947 for (std::set
<Layer
*>::iterator it
= clip_children_
->begin();
948 it
!= clip_children_
->end(); ++it
) {
949 DCHECK_EQ((*it
)->clip_parent(), this);
950 LayerImpl
* clip_child
= layer
->layer_tree_impl()->LayerById((*it
)->id());
952 clip_children
->insert(clip_child
);
954 layer
->SetClipChildren(clip_children
);
956 layer
->SetClipChildren(NULL
);
959 // Adjust the scroll delta to be just the scrolls that have happened since
960 // the BeginMainFrame was sent. This happens for impl-side painting
961 // in LayerImpl::ApplyScrollDeltasSinceBeginMainFrame in a separate tree walk.
962 if (layer
->layer_tree_impl()->settings().impl_side_painting
) {
963 layer
->SetScrollOffset(scroll_offset_
);
965 layer
->SetScrollOffsetAndDelta(
966 scroll_offset_
, layer
->ScrollDelta() - layer
->sent_scroll_delta());
967 layer
->SetSentScrollDelta(gfx::Vector2d());
970 // Wrap the copy_requests_ in a PostTask to the main thread.
971 ScopedPtrVector
<CopyOutputRequest
> main_thread_copy_requests
;
972 for (ScopedPtrVector
<CopyOutputRequest
>::iterator it
= copy_requests_
.begin();
973 it
!= copy_requests_
.end();
975 scoped_refptr
<base::SingleThreadTaskRunner
> main_thread_task_runner
=
976 layer_tree_host()->proxy()->MainThreadTaskRunner();
977 scoped_ptr
<CopyOutputRequest
> original_request
= copy_requests_
.take(it
);
978 const CopyOutputRequest
& original_request_ref
= *original_request
;
979 scoped_ptr
<CopyOutputRequest
> main_thread_request
=
980 CopyOutputRequest::CreateRelayRequest(
981 original_request_ref
,
982 base::Bind(&PostCopyCallbackToMainThread
,
983 main_thread_task_runner
,
984 base::Passed(&original_request
)));
985 main_thread_copy_requests
.push_back(main_thread_request
.Pass());
987 copy_requests_
.clear();
988 layer
->PassCopyRequests(&main_thread_copy_requests
);
990 // If the main thread commits multiple times before the impl thread actually
991 // draws, then damage tracking will become incorrect if we simply clobber the
992 // update_rect here. The LayerImpl's update_rect needs to accumulate (i.e.
993 // union) any update changes that have occurred on the main thread.
994 update_rect_
.Union(layer
->update_rect());
995 layer
->SetUpdateRect(update_rect_
);
997 layer
->SetStackingOrderChanged(stacking_order_changed_
);
999 layer_animation_controller_
->PushAnimationUpdatesTo(
1000 layer
->layer_animation_controller());
1002 // Reset any state that should be cleared for the next update.
1003 stacking_order_changed_
= false;
1004 update_rect_
= gfx::RectF();
1006 needs_push_properties_
= false;
1007 num_dependents_need_push_properties_
= 0;
1010 scoped_ptr
<LayerImpl
> Layer::CreateLayerImpl(LayerTreeImpl
* tree_impl
) {
1011 return LayerImpl::Create(tree_impl
, layer_id_
);
1014 bool Layer::DrawsContent() const {
1015 return draws_content_
;
1018 bool Layer::HasDrawableContent() const {
1019 return is_drawable_
;
1022 void Layer::UpdateDrawsContent(bool has_drawable_content
) {
1023 bool draws_content
= has_drawable_content
;
1024 DCHECK(is_drawable_
|| !has_drawable_content
);
1025 if (draws_content
== draws_content_
)
1028 if (HasDelegatedContent()) {
1029 // Layers with delegated content need to be treated as if they have as
1030 // many children as the number of layers they own delegated quads for.
1031 // Since we don't know this number right now, we choose one that acts like
1032 // infinity for our purposes.
1033 AddDrawableDescendants(draws_content
? 1000 : -1000);
1037 parent()->AddDrawableDescendants(draws_content
? 1 : -1);
1039 draws_content_
= draws_content
;
1043 int Layer::NumDescendantsThatDrawContent() const {
1044 return num_descendants_that_draw_content_
;
1047 void Layer::SavePaintProperties() {
1048 DCHECK(layer_tree_host_
);
1050 // TODO(reveman): Save all layer properties that we depend on not
1051 // changing until PushProperties() has been called. crbug.com/231016
1052 paint_properties_
.bounds
= bounds_
;
1053 paint_properties_
.source_frame_number
=
1054 layer_tree_host_
->source_frame_number();
1057 bool Layer::Update(ResourceUpdateQueue
* queue
,
1058 const OcclusionTracker
<Layer
>* occlusion
) {
1059 DCHECK(layer_tree_host_
);
1060 DCHECK_EQ(layer_tree_host_
->source_frame_number(),
1061 paint_properties_
.source_frame_number
) <<
1062 "SavePaintProperties must be called for any layer that is painted.";
1066 bool Layer::NeedMoreUpdates() {
1070 bool Layer::IsSuitableForGpuRasterization() const {
1074 scoped_refptr
<base::debug::ConvertableToTraceFormat
> Layer::TakeDebugInfo() {
1076 return client_
->TakeDebugInfo();
1081 void Layer::CreateRenderSurface() {
1082 DCHECK(!draw_properties_
.render_surface
);
1083 draw_properties_
.render_surface
= make_scoped_ptr(new RenderSurface(this));
1084 draw_properties_
.render_target
= this;
1087 void Layer::ClearRenderSurface() {
1088 draw_properties_
.render_surface
.reset();
1091 void Layer::ClearRenderSurfaceLayerList() {
1092 if (draw_properties_
.render_surface
)
1093 draw_properties_
.render_surface
->layer_list().clear();
1096 gfx::Vector2dF
Layer::ScrollOffsetForAnimation() const {
1097 return TotalScrollOffset();
1100 // On<Property>Animated is called due to an ongoing accelerated animation.
1101 // Since this animation is also being run on the compositor thread, there
1102 // is no need to request a commit to push this value over, so the value is
1103 // set directly rather than by calling Set<Property>.
1104 void Layer::OnFilterAnimated(const FilterOperations
& filters
) {
1108 void Layer::OnOpacityAnimated(float opacity
) {
1112 void Layer::OnTransformAnimated(const gfx::Transform
& transform
) {
1113 if (transform_
== transform
)
1115 transform_
= transform
;
1116 transform_is_invertible_
= transform
.IsInvertible();
1119 void Layer::OnScrollOffsetAnimated(const gfx::Vector2dF
& scroll_offset
) {
1120 // Do nothing. Scroll deltas will be sent from the compositor thread back
1121 // to the main thread in the same manner as during non-animated
1122 // compositor-driven scrolling.
1125 void Layer::OnAnimationWaitingForDeletion() {
1126 // Animations are only deleted during PushProperties.
1127 SetNeedsPushProperties();
1130 bool Layer::IsActive() const {
1134 bool Layer::AddAnimation(scoped_ptr
<Animation
> animation
) {
1135 if (!layer_animation_controller_
->animation_registrar())
1138 if (animation
->target_property() == Animation::ScrollOffset
&&
1139 !layer_animation_controller_
->animation_registrar()
1140 ->supports_scroll_animations())
1143 UMA_HISTOGRAM_BOOLEAN("Renderer.AnimationAddedToOrphanLayer",
1145 layer_animation_controller_
->AddAnimation(animation
.Pass());
1150 void Layer::PauseAnimation(int animation_id
, double time_offset
) {
1151 layer_animation_controller_
->PauseAnimation(
1152 animation_id
, base::TimeDelta::FromSecondsD(time_offset
));
1156 void Layer::RemoveAnimation(int animation_id
) {
1157 layer_animation_controller_
->RemoveAnimation(animation_id
);
1161 void Layer::SetLayerAnimationControllerForTest(
1162 scoped_refptr
<LayerAnimationController
> controller
) {
1163 layer_animation_controller_
->RemoveValueObserver(this);
1164 layer_animation_controller_
= controller
;
1165 layer_animation_controller_
->AddValueObserver(this);
1169 bool Layer::HasActiveAnimation() const {
1170 return layer_animation_controller_
->HasActiveAnimation();
1173 void Layer::AddLayerAnimationEventObserver(
1174 LayerAnimationEventObserver
* animation_observer
) {
1175 layer_animation_controller_
->AddEventObserver(animation_observer
);
1178 void Layer::RemoveLayerAnimationEventObserver(
1179 LayerAnimationEventObserver
* animation_observer
) {
1180 layer_animation_controller_
->RemoveEventObserver(animation_observer
);
1183 SimpleEnclosedRegion
Layer::VisibleContentOpaqueRegion() const {
1184 if (contents_opaque())
1185 return SimpleEnclosedRegion(visible_content_rect());
1186 return SimpleEnclosedRegion();
1189 ScrollbarLayerInterface
* Layer::ToScrollbarLayer() {
1193 RenderingStatsInstrumentation
* Layer::rendering_stats_instrumentation() const {
1194 return layer_tree_host_
->rendering_stats_instrumentation();
1197 bool Layer::SupportsLCDText() const {
1201 void Layer::RemoveFromScrollTree() {
1202 if (scroll_children_
.get()) {
1203 std::set
<Layer
*> copy
= *scroll_children_
;
1204 for (std::set
<Layer
*>::iterator it
= copy
.begin(); it
!= copy
.end(); ++it
)
1205 (*it
)->SetScrollParent(NULL
);
1208 DCHECK(!scroll_children_
);
1209 SetScrollParent(NULL
);
1212 void Layer::RemoveFromClipTree() {
1213 if (clip_children_
.get()) {
1214 std::set
<Layer
*> copy
= *clip_children_
;
1215 for (std::set
<Layer
*>::iterator it
= copy
.begin(); it
!= copy
.end(); ++it
)
1216 (*it
)->SetClipParent(NULL
);
1219 DCHECK(!clip_children_
);
1220 SetClipParent(NULL
);
1223 void Layer::AddDrawableDescendants(int num
) {
1224 DCHECK_GE(num_descendants_that_draw_content_
, 0);
1225 DCHECK_GE(num_descendants_that_draw_content_
+ num
, 0);
1228 num_descendants_that_draw_content_
+= num
;
1231 parent()->AddDrawableDescendants(num
);
1234 void Layer::RunMicroBenchmark(MicroBenchmark
* benchmark
) {
1235 benchmark
->RunOnLayer(this);
1238 bool Layer::HasDelegatedContent() const {