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/rect_conversions.h"
30 #include "ui/gfx/geometry/vector2d_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_(nullptr),
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
),
73 scroll_parent_(nullptr),
74 clip_parent_(nullptr),
75 replica_layer_(nullptr),
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::Rect
& layer_rect
) const {
206 gfx::Rect content_rect
= gfx::ScaleToEnclosingRect(
207 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()));
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() : nullptr);
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(nullptr);
270 mask_layer_
= nullptr;
271 SetNeedsFullTreeSync();
274 if (replica_layer_
.get() == child
) {
275 replica_layer_
->SetParent(nullptr);
276 replica_layer_
= nullptr;
277 SetNeedsFullTreeSync();
281 for (LayerList::iterator iter
= children_
.begin();
282 iter
!= children_
.end();
284 if (iter
->get() != child
)
287 child
->SetParent(nullptr);
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_
= nullptr;
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_
= nullptr;
657 void Layer::SetScrollOffset(const gfx::ScrollOffset
& scroll_offset
) {
658 DCHECK(IsPropertyChangeAllowed());
660 if (scroll_offset_
== scroll_offset
)
662 scroll_offset_
= scroll_offset
;
666 void Layer::SetScrollOffsetFromImplSide(
667 const gfx::ScrollOffset
& scroll_offset
) {
668 DCHECK(IsPropertyChangeAllowed());
669 // This function only gets called during a BeginMainFrame, so there
670 // is no need to call SetNeedsUpdate here.
671 DCHECK(layer_tree_host_
&& layer_tree_host_
->CommitRequested());
672 if (scroll_offset_
== scroll_offset
)
674 scroll_offset_
= scroll_offset
;
675 SetNeedsPushProperties();
676 if (!did_scroll_callback_
.is_null())
677 did_scroll_callback_
.Run();
678 // The callback could potentially change the layer structure:
679 // "this" may have been destroyed during the process.
682 void Layer::SetScrollClipLayerId(int clip_layer_id
) {
683 DCHECK(IsPropertyChangeAllowed());
684 if (scroll_clip_layer_id_
== clip_layer_id
)
686 scroll_clip_layer_id_
= clip_layer_id
;
690 void Layer::SetUserScrollable(bool horizontal
, bool vertical
) {
691 DCHECK(IsPropertyChangeAllowed());
692 if (user_scrollable_horizontal_
== horizontal
&&
693 user_scrollable_vertical_
== vertical
)
695 user_scrollable_horizontal_
= horizontal
;
696 user_scrollable_vertical_
= vertical
;
700 void Layer::SetShouldScrollOnMainThread(bool should_scroll_on_main_thread
) {
701 DCHECK(IsPropertyChangeAllowed());
702 if (should_scroll_on_main_thread_
== should_scroll_on_main_thread
)
704 should_scroll_on_main_thread_
= should_scroll_on_main_thread
;
708 void Layer::SetHaveWheelEventHandlers(bool have_wheel_event_handlers
) {
709 DCHECK(IsPropertyChangeAllowed());
710 if (have_wheel_event_handlers_
== have_wheel_event_handlers
)
712 have_wheel_event_handlers_
= have_wheel_event_handlers
;
716 void Layer::SetHaveScrollEventHandlers(bool have_scroll_event_handlers
) {
717 DCHECK(IsPropertyChangeAllowed());
718 if (have_scroll_event_handlers_
== have_scroll_event_handlers
)
720 have_scroll_event_handlers_
= have_scroll_event_handlers
;
724 void Layer::SetNonFastScrollableRegion(const Region
& region
) {
725 DCHECK(IsPropertyChangeAllowed());
726 if (non_fast_scrollable_region_
== region
)
728 non_fast_scrollable_region_
= region
;
732 void Layer::SetTouchEventHandlerRegion(const Region
& region
) {
733 DCHECK(IsPropertyChangeAllowed());
734 if (touch_event_handler_region_
== region
)
736 touch_event_handler_region_
= region
;
740 void Layer::SetDrawCheckerboardForMissingTiles(bool checkerboard
) {
741 DCHECK(IsPropertyChangeAllowed());
742 if (draw_checkerboard_for_missing_tiles_
== checkerboard
)
744 draw_checkerboard_for_missing_tiles_
= checkerboard
;
748 void Layer::SetForceRenderSurface(bool force
) {
749 DCHECK(IsPropertyChangeAllowed());
750 if (force_render_surface_
== force
)
752 force_render_surface_
= force
;
756 void Layer::SetDoubleSided(bool double_sided
) {
757 DCHECK(IsPropertyChangeAllowed());
758 if (double_sided_
== double_sided
)
760 double_sided_
= double_sided
;
764 void Layer::Set3dSortingContextId(int id
) {
765 DCHECK(IsPropertyChangeAllowed());
766 if (id
== sorting_context_id_
)
768 sorting_context_id_
= id
;
772 void Layer::SetShouldFlattenTransform(bool should_flatten
) {
773 DCHECK(IsPropertyChangeAllowed());
774 if (should_flatten_transform_
== should_flatten
)
776 should_flatten_transform_
= should_flatten
;
780 void Layer::SetIsDrawable(bool is_drawable
) {
781 DCHECK(IsPropertyChangeAllowed());
782 if (is_drawable_
== is_drawable
)
785 is_drawable_
= is_drawable
;
786 UpdateDrawsContent(HasDrawableContent());
789 void Layer::SetHideLayerAndSubtree(bool hide
) {
790 DCHECK(IsPropertyChangeAllowed());
791 if (hide_layer_and_subtree_
== hide
)
794 hide_layer_and_subtree_
= hide
;
798 void Layer::SetNeedsDisplayRect(const gfx::Rect
& dirty_rect
) {
799 if (dirty_rect
.IsEmpty())
802 SetNeedsPushProperties();
803 update_rect_
.Union(dirty_rect
);
809 bool Layer::DescendantIsFixedToContainerLayer() const {
810 for (size_t i
= 0; i
< children_
.size(); ++i
) {
811 if (children_
[i
]->position_constraint_
.is_fixed_position() ||
812 children_
[i
]->DescendantIsFixedToContainerLayer())
818 void Layer::SetIsContainerForFixedPositionLayers(bool container
) {
819 if (is_container_for_fixed_position_layers_
== container
)
821 is_container_for_fixed_position_layers_
= container
;
823 if (layer_tree_host_
&& layer_tree_host_
->CommitRequested())
826 // Only request a commit if we have a fixed positioned descendant.
827 if (DescendantIsFixedToContainerLayer())
831 void Layer::SetPositionConstraint(const LayerPositionConstraint
& constraint
) {
832 DCHECK(IsPropertyChangeAllowed());
833 if (position_constraint_
== constraint
)
835 position_constraint_
= constraint
;
839 static void RunCopyCallbackOnMainThread(scoped_ptr
<CopyOutputRequest
> request
,
840 scoped_ptr
<CopyOutputResult
> result
) {
841 request
->SendResult(result
.Pass());
844 static void PostCopyCallbackToMainThread(
845 scoped_refptr
<base::SingleThreadTaskRunner
> main_thread_task_runner
,
846 scoped_ptr
<CopyOutputRequest
> request
,
847 scoped_ptr
<CopyOutputResult
> result
) {
848 main_thread_task_runner
->PostTask(FROM_HERE
,
849 base::Bind(&RunCopyCallbackOnMainThread
,
850 base::Passed(&request
),
851 base::Passed(&result
)));
854 void Layer::PushPropertiesTo(LayerImpl
* layer
) {
855 DCHECK(layer_tree_host_
);
857 // If we did not SavePaintProperties() for the layer this frame, then push the
858 // real property values, not the paint property values.
859 bool use_paint_properties
= paint_properties_
.source_frame_number
==
860 layer_tree_host_
->source_frame_number();
862 layer
->SetTransformOrigin(transform_origin_
);
863 layer
->SetBackgroundColor(background_color_
);
864 layer
->SetBounds(use_paint_properties
? paint_properties_
.bounds
866 layer
->SetContentBounds(content_bounds());
867 layer
->SetContentsScale(contents_scale_x(), contents_scale_y());
870 TRACE_EVENT_CATEGORY_GROUP_ENABLED(
871 TRACE_DISABLED_BY_DEFAULT("cc.debug") "," TRACE_DISABLED_BY_DEFAULT(
872 "devtools.timeline.layers"),
875 layer
->SetDebugInfo(TakeDebugInfo());
877 layer
->SetDoubleSided(double_sided_
);
878 layer
->SetDrawCheckerboardForMissingTiles(
879 draw_checkerboard_for_missing_tiles_
);
880 layer
->SetForceRenderSurface(force_render_surface_
);
881 layer
->SetDrawsContent(DrawsContent());
882 layer
->SetHideLayerAndSubtree(hide_layer_and_subtree_
);
883 if (!layer
->FilterIsAnimatingOnImplOnly() && !FilterIsAnimating())
884 layer
->SetFilters(filters_
);
885 DCHECK(!(FilterIsAnimating() && layer
->FilterIsAnimatingOnImplOnly()));
886 layer
->SetBackgroundFilters(background_filters());
887 layer
->SetMasksToBounds(masks_to_bounds_
);
888 layer
->SetShouldScrollOnMainThread(should_scroll_on_main_thread_
);
889 layer
->SetHaveWheelEventHandlers(have_wheel_event_handlers_
);
890 layer
->SetHaveScrollEventHandlers(have_scroll_event_handlers_
);
891 layer
->SetNonFastScrollableRegion(non_fast_scrollable_region_
);
892 layer
->SetTouchEventHandlerRegion(touch_event_handler_region_
);
893 layer
->SetContentsOpaque(contents_opaque_
);
894 if (!layer
->OpacityIsAnimatingOnImplOnly() && !OpacityIsAnimating())
895 layer
->SetOpacity(opacity_
);
896 DCHECK(!(OpacityIsAnimating() && layer
->OpacityIsAnimatingOnImplOnly()));
897 layer
->SetBlendMode(blend_mode_
);
898 layer
->SetIsRootForIsolatedGroup(is_root_for_isolated_group_
);
899 layer
->SetPosition(position_
);
900 layer
->SetIsContainerForFixedPositionLayers(
901 IsContainerForFixedPositionLayers());
902 layer
->SetPositionConstraint(position_constraint_
);
903 layer
->SetShouldFlattenTransform(should_flatten_transform_
);
904 layer
->SetUseParentBackfaceVisibility(use_parent_backface_visibility_
);
905 if (!layer
->TransformIsAnimatingOnImplOnly() && !TransformIsAnimating())
906 layer
->SetTransformAndInvertibility(transform_
, transform_is_invertible_
);
907 DCHECK(!(TransformIsAnimating() && layer
->TransformIsAnimatingOnImplOnly()));
908 layer
->Set3dSortingContextId(sorting_context_id_
);
909 layer
->SetNumDescendantsThatDrawContent(num_descendants_that_draw_content_
);
911 layer
->SetScrollClipLayer(scroll_clip_layer_id_
);
912 layer
->set_user_scrollable_horizontal(user_scrollable_horizontal_
);
913 layer
->set_user_scrollable_vertical(user_scrollable_vertical_
);
915 LayerImpl
* scroll_parent
= nullptr;
916 if (scroll_parent_
) {
917 scroll_parent
= layer
->layer_tree_impl()->LayerById(scroll_parent_
->id());
918 DCHECK(scroll_parent
);
921 layer
->SetScrollParent(scroll_parent
);
922 if (scroll_children_
) {
923 std::set
<LayerImpl
*>* scroll_children
= new std::set
<LayerImpl
*>;
924 for (std::set
<Layer
*>::iterator it
= scroll_children_
->begin();
925 it
!= scroll_children_
->end();
927 DCHECK_EQ((*it
)->scroll_parent(), this);
928 LayerImpl
* scroll_child
=
929 layer
->layer_tree_impl()->LayerById((*it
)->id());
930 DCHECK(scroll_child
);
931 scroll_children
->insert(scroll_child
);
933 layer
->SetScrollChildren(scroll_children
);
935 layer
->SetScrollChildren(nullptr);
938 LayerImpl
* clip_parent
= nullptr;
941 layer
->layer_tree_impl()->LayerById(clip_parent_
->id());
945 layer
->SetClipParent(clip_parent
);
946 if (clip_children_
) {
947 std::set
<LayerImpl
*>* clip_children
= new std::set
<LayerImpl
*>;
948 for (std::set
<Layer
*>::iterator it
= clip_children_
->begin();
949 it
!= clip_children_
->end(); ++it
) {
950 DCHECK_EQ((*it
)->clip_parent(), this);
951 LayerImpl
* clip_child
= layer
->layer_tree_impl()->LayerById((*it
)->id());
953 clip_children
->insert(clip_child
);
955 layer
->SetClipChildren(clip_children
);
957 layer
->SetClipChildren(nullptr);
960 // Adjust the scroll delta to be just the scrolls that have happened since
961 // the BeginMainFrame was sent. This happens for impl-side painting
962 // in LayerImpl::ApplyScrollDeltasSinceBeginMainFrame in a separate tree walk.
963 if (layer
->layer_tree_impl()->settings().impl_side_painting
) {
964 layer
->SetScrollOffset(scroll_offset_
);
966 layer
->SetScrollOffsetAndDelta(
968 layer
->ScrollDelta() - layer
->sent_scroll_delta());
969 layer
->SetSentScrollDelta(gfx::Vector2dF());
972 // Wrap the copy_requests_ in a PostTask to the main thread.
973 ScopedPtrVector
<CopyOutputRequest
> main_thread_copy_requests
;
974 for (ScopedPtrVector
<CopyOutputRequest
>::iterator it
= copy_requests_
.begin();
975 it
!= copy_requests_
.end();
977 scoped_refptr
<base::SingleThreadTaskRunner
> main_thread_task_runner
=
978 layer_tree_host()->proxy()->MainThreadTaskRunner();
979 scoped_ptr
<CopyOutputRequest
> original_request
= copy_requests_
.take(it
);
980 const CopyOutputRequest
& original_request_ref
= *original_request
;
981 scoped_ptr
<CopyOutputRequest
> main_thread_request
=
982 CopyOutputRequest::CreateRelayRequest(
983 original_request_ref
,
984 base::Bind(&PostCopyCallbackToMainThread
,
985 main_thread_task_runner
,
986 base::Passed(&original_request
)));
987 main_thread_copy_requests
.push_back(main_thread_request
.Pass());
989 copy_requests_
.clear();
990 layer
->PassCopyRequests(&main_thread_copy_requests
);
992 // If the main thread commits multiple times before the impl thread actually
993 // draws, then damage tracking will become incorrect if we simply clobber the
994 // update_rect here. The LayerImpl's update_rect needs to accumulate (i.e.
995 // union) any update changes that have occurred on the main thread.
996 update_rect_
.Union(layer
->update_rect());
997 layer
->SetUpdateRect(update_rect_
);
999 layer
->SetStackingOrderChanged(stacking_order_changed_
);
1001 layer_animation_controller_
->PushAnimationUpdatesTo(
1002 layer
->layer_animation_controller());
1004 // Reset any state that should be cleared for the next update.
1005 stacking_order_changed_
= false;
1006 update_rect_
= gfx::Rect();
1008 needs_push_properties_
= false;
1009 num_dependents_need_push_properties_
= 0;
1012 scoped_ptr
<LayerImpl
> Layer::CreateLayerImpl(LayerTreeImpl
* tree_impl
) {
1013 return LayerImpl::Create(tree_impl
, layer_id_
);
1016 bool Layer::DrawsContent() const {
1017 return draws_content_
;
1020 bool Layer::HasDrawableContent() const {
1021 return is_drawable_
;
1024 void Layer::UpdateDrawsContent(bool has_drawable_content
) {
1025 bool draws_content
= has_drawable_content
;
1026 DCHECK(is_drawable_
|| !has_drawable_content
);
1027 if (draws_content
== draws_content_
)
1030 if (HasDelegatedContent()) {
1031 // Layers with delegated content need to be treated as if they have as
1032 // many children as the number of layers they own delegated quads for.
1033 // Since we don't know this number right now, we choose one that acts like
1034 // infinity for our purposes.
1035 AddDrawableDescendants(draws_content
? 1000 : -1000);
1039 parent()->AddDrawableDescendants(draws_content
? 1 : -1);
1041 draws_content_
= draws_content
;
1045 int Layer::NumDescendantsThatDrawContent() const {
1046 return num_descendants_that_draw_content_
;
1049 void Layer::SavePaintProperties() {
1050 DCHECK(layer_tree_host_
);
1052 // TODO(reveman): Save all layer properties that we depend on not
1053 // changing until PushProperties() has been called. crbug.com/231016
1054 paint_properties_
.bounds
= bounds_
;
1055 paint_properties_
.source_frame_number
=
1056 layer_tree_host_
->source_frame_number();
1059 bool Layer::Update(ResourceUpdateQueue
* queue
,
1060 const OcclusionTracker
<Layer
>* occlusion
) {
1061 DCHECK(layer_tree_host_
);
1062 DCHECK_EQ(layer_tree_host_
->source_frame_number(),
1063 paint_properties_
.source_frame_number
) <<
1064 "SavePaintProperties must be called for any layer that is painted.";
1068 bool Layer::NeedMoreUpdates() {
1072 bool Layer::IsSuitableForGpuRasterization() const {
1076 scoped_refptr
<base::debug::ConvertableToTraceFormat
> Layer::TakeDebugInfo() {
1078 return client_
->TakeDebugInfo();
1083 void Layer::CreateRenderSurface() {
1084 DCHECK(!draw_properties_
.render_surface
);
1085 draw_properties_
.render_surface
= make_scoped_ptr(new RenderSurface(this));
1086 draw_properties_
.render_target
= this;
1089 void Layer::ClearRenderSurface() {
1090 draw_properties_
.render_surface
= nullptr;
1093 void Layer::ClearRenderSurfaceLayerList() {
1094 if (draw_properties_
.render_surface
)
1095 draw_properties_
.render_surface
->layer_list().clear();
1098 gfx::ScrollOffset
Layer::ScrollOffsetForAnimation() const {
1099 return TotalScrollOffset();
1102 // On<Property>Animated is called due to an ongoing accelerated animation.
1103 // Since this animation is also being run on the compositor thread, there
1104 // is no need to request a commit to push this value over, so the value is
1105 // set directly rather than by calling Set<Property>.
1106 void Layer::OnFilterAnimated(const FilterOperations
& filters
) {
1110 void Layer::OnOpacityAnimated(float opacity
) {
1114 void Layer::OnTransformAnimated(const gfx::Transform
& transform
) {
1115 if (transform_
== transform
)
1117 transform_
= transform
;
1118 transform_is_invertible_
= transform
.IsInvertible();
1121 void Layer::OnScrollOffsetAnimated(const gfx::ScrollOffset
& scroll_offset
) {
1122 // Do nothing. Scroll deltas will be sent from the compositor thread back
1123 // to the main thread in the same manner as during non-animated
1124 // compositor-driven scrolling.
1127 void Layer::OnAnimationWaitingForDeletion() {
1128 // Animations are only deleted during PushProperties.
1129 SetNeedsPushProperties();
1132 bool Layer::IsActive() const {
1136 bool Layer::AddAnimation(scoped_ptr
<Animation
> animation
) {
1137 if (!layer_animation_controller_
->animation_registrar())
1140 if (animation
->target_property() == Animation::ScrollOffset
&&
1141 !layer_animation_controller_
->animation_registrar()
1142 ->supports_scroll_animations())
1145 UMA_HISTOGRAM_BOOLEAN("Renderer.AnimationAddedToOrphanLayer",
1147 layer_animation_controller_
->AddAnimation(animation
.Pass());
1152 void Layer::PauseAnimation(int animation_id
, double time_offset
) {
1153 layer_animation_controller_
->PauseAnimation(
1154 animation_id
, base::TimeDelta::FromSecondsD(time_offset
));
1158 void Layer::RemoveAnimation(int animation_id
) {
1159 layer_animation_controller_
->RemoveAnimation(animation_id
);
1163 void Layer::SetLayerAnimationControllerForTest(
1164 scoped_refptr
<LayerAnimationController
> controller
) {
1165 layer_animation_controller_
->RemoveValueObserver(this);
1166 layer_animation_controller_
= controller
;
1167 layer_animation_controller_
->AddValueObserver(this);
1171 bool Layer::HasActiveAnimation() const {
1172 return layer_animation_controller_
->HasActiveAnimation();
1175 void Layer::AddLayerAnimationEventObserver(
1176 LayerAnimationEventObserver
* animation_observer
) {
1177 layer_animation_controller_
->AddEventObserver(animation_observer
);
1180 void Layer::RemoveLayerAnimationEventObserver(
1181 LayerAnimationEventObserver
* animation_observer
) {
1182 layer_animation_controller_
->RemoveEventObserver(animation_observer
);
1185 SimpleEnclosedRegion
Layer::VisibleContentOpaqueRegion() const {
1186 if (contents_opaque())
1187 return SimpleEnclosedRegion(visible_content_rect());
1188 return SimpleEnclosedRegion();
1191 ScrollbarLayerInterface
* Layer::ToScrollbarLayer() {
1195 RenderingStatsInstrumentation
* Layer::rendering_stats_instrumentation() const {
1196 return layer_tree_host_
->rendering_stats_instrumentation();
1199 bool Layer::SupportsLCDText() const {
1203 void Layer::RemoveFromScrollTree() {
1204 if (scroll_children_
.get()) {
1205 std::set
<Layer
*> copy
= *scroll_children_
;
1206 for (std::set
<Layer
*>::iterator it
= copy
.begin(); it
!= copy
.end(); ++it
)
1207 (*it
)->SetScrollParent(nullptr);
1210 DCHECK(!scroll_children_
);
1211 SetScrollParent(nullptr);
1214 void Layer::RemoveFromClipTree() {
1215 if (clip_children_
.get()) {
1216 std::set
<Layer
*> copy
= *clip_children_
;
1217 for (std::set
<Layer
*>::iterator it
= copy
.begin(); it
!= copy
.end(); ++it
)
1218 (*it
)->SetClipParent(nullptr);
1221 DCHECK(!clip_children_
);
1222 SetClipParent(nullptr);
1225 void Layer::AddDrawableDescendants(int num
) {
1226 DCHECK_GE(num_descendants_that_draw_content_
, 0);
1227 DCHECK_GE(num_descendants_that_draw_content_
+ num
, 0);
1230 num_descendants_that_draw_content_
+= num
;
1233 parent()->AddDrawableDescendants(num
);
1236 void Layer::RunMicroBenchmark(MicroBenchmark
* benchmark
) {
1237 benchmark
->RunOnLayer(this);
1240 bool Layer::HasDelegatedContent() const {