1 // Copyright 2012 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_impl.h"
7 #include "base/json/json_reader.h"
8 #include "base/numerics/safe_conversions.h"
9 #include "base/strings/stringprintf.h"
10 #include "base/trace_event/trace_event.h"
11 #include "base/trace_event/trace_event_argument.h"
12 #include "cc/animation/animation_registrar.h"
13 #include "cc/animation/scrollbar_animation_controller.h"
14 #include "cc/base/math_util.h"
15 #include "cc/base/simple_enclosed_region.h"
16 #include "cc/debug/debug_colors.h"
17 #include "cc/debug/layer_tree_debug_state.h"
18 #include "cc/debug/micro_benchmark_impl.h"
19 #include "cc/debug/traced_value.h"
20 #include "cc/input/scroll_state.h"
21 #include "cc/layers/layer_utils.h"
22 #include "cc/layers/painted_scrollbar_layer_impl.h"
23 #include "cc/output/copy_output_request.h"
24 #include "cc/quads/debug_border_draw_quad.h"
25 #include "cc/quads/render_pass.h"
26 #include "cc/trees/layer_tree_host_common.h"
27 #include "cc/trees/layer_tree_impl.h"
28 #include "cc/trees/layer_tree_settings.h"
29 #include "cc/trees/proxy.h"
30 #include "ui/gfx/geometry/box_f.h"
31 #include "ui/gfx/geometry/point_conversions.h"
32 #include "ui/gfx/geometry/quad_f.h"
33 #include "ui/gfx/geometry/rect_conversions.h"
34 #include "ui/gfx/geometry/size_conversions.h"
35 #include "ui/gfx/geometry/vector2d_conversions.h"
38 LayerImpl::LayerImpl(LayerTreeImpl
* layer_impl
, int id
)
39 : LayerImpl(layer_impl
, id
, new LayerImpl::SyncedScrollOffset
) {
42 LayerImpl::LayerImpl(LayerTreeImpl
* tree_impl
,
44 scoped_refptr
<SyncedScrollOffset
> scroll_offset
)
46 scroll_parent_(nullptr),
47 clip_parent_(nullptr),
49 replica_layer_id_(-1),
51 layer_tree_impl_(tree_impl
),
52 scroll_offset_(scroll_offset
),
53 scroll_clip_layer_(nullptr),
54 should_scroll_on_main_thread_(false),
55 have_wheel_event_handlers_(false),
56 have_scroll_event_handlers_(false),
57 scroll_blocks_on_(SCROLL_BLOCKS_ON_NONE
),
58 user_scrollable_horizontal_(true),
59 user_scrollable_vertical_(true),
60 stacking_order_changed_(false),
62 should_flatten_transform_(true),
63 should_flatten_transform_from_property_tree_(false),
65 layer_property_changed_(false),
66 masks_to_bounds_(false),
67 contents_opaque_(false),
68 is_root_for_isolated_group_(false),
69 use_parent_backface_visibility_(false),
70 draw_checkerboard_for_missing_tiles_(false),
71 draws_content_(false),
72 hide_layer_and_subtree_(false),
73 transform_is_invertible_(true),
74 is_container_for_fixed_position_layers_(false),
75 is_affected_by_page_scale_(true),
78 blend_mode_(SkXfermode::kSrcOver_Mode
),
79 num_descendants_that_draw_content_(0),
80 transform_tree_index_(-1),
81 opacity_tree_index_(-1),
84 needs_push_properties_(false),
85 num_dependents_need_push_properties_(0),
86 sorting_context_id_(0),
87 current_draw_mode_(DRAW_MODE_NONE
),
88 frame_timing_requests_dirty_(false),
90 layer_or_descendant_is_drawn_(false),
91 sorted_for_recursion_(false) {
92 DCHECK_GT(layer_id_
, 0);
93 DCHECK(layer_tree_impl_
);
94 layer_tree_impl_
->RegisterLayer(this);
96 if (!layer_tree_impl_
->settings().use_compositor_animation_timelines
) {
97 AnimationRegistrar
* registrar
= layer_tree_impl_
->GetAnimationRegistrar();
98 layer_animation_controller_
=
99 registrar
->GetAnimationControllerForId(layer_id_
);
100 layer_animation_controller_
->AddValueObserver(this);
102 layer_animation_controller_
->set_value_provider(this);
103 layer_animation_controller_
->set_layer_animation_delegate(this);
106 SetNeedsPushProperties();
109 LayerImpl::~LayerImpl() {
110 DCHECK_EQ(DRAW_MODE_NONE
, current_draw_mode_
);
112 if (layer_animation_controller_
) {
113 layer_animation_controller_
->RemoveValueObserver(this);
114 layer_animation_controller_
->remove_value_provider(this);
115 layer_animation_controller_
->remove_layer_animation_delegate(this);
118 if (!copy_requests_
.empty() && layer_tree_impl_
->IsActiveTree())
119 layer_tree_impl()->RemoveLayerWithCopyOutputRequest(this);
120 layer_tree_impl_
->UnregisterLayer(this);
122 TRACE_EVENT_OBJECT_DELETED_WITH_ID(
123 TRACE_DISABLED_BY_DEFAULT("cc.debug"), "cc::LayerImpl", this);
126 void LayerImpl::AddChild(scoped_ptr
<LayerImpl
> child
) {
127 child
->SetParent(this);
128 DCHECK_EQ(layer_tree_impl(), child
->layer_tree_impl());
129 children_
.push_back(child
.Pass());
130 layer_tree_impl()->set_needs_update_draw_properties();
133 scoped_ptr
<LayerImpl
> LayerImpl::RemoveChild(LayerImpl
* child
) {
134 for (OwnedLayerImplList::iterator it
= children_
.begin();
135 it
!= children_
.end();
138 scoped_ptr
<LayerImpl
> ret
= children_
.take(it
);
140 layer_tree_impl()->set_needs_update_draw_properties();
147 void LayerImpl::SetParent(LayerImpl
* parent
) {
148 if (parent_should_know_need_push_properties()) {
150 parent_
->RemoveDependentNeedsPushProperties();
152 parent
->AddDependentNeedsPushProperties();
157 void LayerImpl::ClearChildList() {
158 if (children_
.empty())
162 layer_tree_impl()->set_needs_update_draw_properties();
165 bool LayerImpl::HasAncestor(const LayerImpl
* ancestor
) const {
169 for (const LayerImpl
* layer
= this; layer
; layer
= layer
->parent()) {
170 if (layer
== ancestor
)
177 void LayerImpl::SetScrollParent(LayerImpl
* parent
) {
178 if (scroll_parent_
== parent
)
182 DCHECK_EQ(layer_tree_impl()->LayerById(parent
->id()), parent
);
184 scroll_parent_
= parent
;
185 SetNeedsPushProperties();
188 void LayerImpl::SetDebugInfo(
189 scoped_refptr
<base::trace_event::ConvertableToTraceFormat
> other
) {
191 SetNeedsPushProperties();
194 void LayerImpl::SetScrollChildren(std::set
<LayerImpl
*>* children
) {
195 if (scroll_children_
.get() == children
)
197 scroll_children_
.reset(children
);
198 SetNeedsPushProperties();
201 void LayerImpl::DistributeScroll(ScrollState
* scroll_state
) {
202 DCHECK(scroll_state
);
203 if (scroll_state
->FullyConsumed())
206 scroll_state
->DistributeToScrollChainDescendant();
208 // If the scroll doesn't propagate, and we're currently scrolling
209 // a layer other than this one, prevent the scroll from
210 // propagating to this layer.
211 if (!scroll_state
->should_propagate() &&
212 scroll_state
->delta_consumed_for_scroll_sequence() &&
213 scroll_state
->current_native_scrolling_layer() != this) {
217 ApplyScroll(scroll_state
);
220 void LayerImpl::ApplyScroll(ScrollState
* scroll_state
) {
221 DCHECK(scroll_state
);
222 layer_tree_impl()->ApplyScroll(this, scroll_state
);
225 void LayerImpl::SetNumDescendantsThatDrawContent(int num_descendants
) {
226 if (num_descendants_that_draw_content_
== num_descendants
)
228 num_descendants_that_draw_content_
= num_descendants
;
229 SetNeedsPushProperties();
232 void LayerImpl::SetClipParent(LayerImpl
* ancestor
) {
233 if (clip_parent_
== ancestor
)
236 clip_parent_
= ancestor
;
237 SetNeedsPushProperties();
240 void LayerImpl::SetClipChildren(std::set
<LayerImpl
*>* children
) {
241 if (clip_children_
.get() == children
)
243 clip_children_
.reset(children
);
244 SetNeedsPushProperties();
247 void LayerImpl::SetTransformTreeIndex(int index
) {
248 transform_tree_index_
= index
;
249 SetNeedsPushProperties();
252 void LayerImpl::SetClipTreeIndex(int index
) {
253 clip_tree_index_
= index
;
254 SetNeedsPushProperties();
257 void LayerImpl::SetOpacityTreeIndex(int index
) {
258 opacity_tree_index_
= index
;
259 SetNeedsPushProperties();
262 void LayerImpl::PassCopyRequests(ScopedPtrVector
<CopyOutputRequest
>* requests
) {
263 // In the case that a layer still has a copy request, this means that there's
264 // a commit to the active tree without a draw. This only happens in some
265 // edge cases during lost context or visibility changes, so don't try to
266 // handle preserving these output requests (and their surface).
267 if (!copy_requests_
.empty()) {
268 layer_tree_impl()->RemoveLayerWithCopyOutputRequest(this);
269 // Destroying these will abort them.
270 copy_requests_
.clear();
273 if (requests
->empty())
276 DCHECK(render_surface());
277 bool was_empty
= copy_requests_
.empty();
278 copy_requests_
.insert_and_take(copy_requests_
.end(), requests
);
281 if (was_empty
&& layer_tree_impl()->IsActiveTree())
282 layer_tree_impl()->AddLayerWithCopyOutputRequest(this);
283 NoteLayerPropertyChangedForSubtree();
286 void LayerImpl::TakeCopyRequestsAndTransformToTarget(
287 ScopedPtrVector
<CopyOutputRequest
>* requests
) {
288 DCHECK(!copy_requests_
.empty());
289 DCHECK(layer_tree_impl()->IsActiveTree());
290 DCHECK_EQ(render_target(), this);
292 size_t first_inserted_request
= requests
->size();
293 requests
->insert_and_take(requests
->end(), ©_requests_
);
294 copy_requests_
.clear();
296 for (size_t i
= first_inserted_request
; i
< requests
->size(); ++i
) {
297 CopyOutputRequest
* request
= requests
->at(i
);
298 if (!request
->has_area())
301 gfx::Rect request_in_layer_space
= request
->area();
302 request_in_layer_space
.Intersect(gfx::Rect(bounds()));
303 request
->set_area(MathUtil::MapEnclosingClippedRect(
304 draw_properties_
.target_space_transform
, request_in_layer_space
));
307 layer_tree_impl()->RemoveLayerWithCopyOutputRequest(this);
308 layer_tree_impl()->set_needs_update_draw_properties();
311 void LayerImpl::ClearRenderSurfaceLayerList() {
313 render_surface_
->ClearLayerLists();
316 void LayerImpl::PopulateSharedQuadState(SharedQuadState
* state
) const {
317 state
->SetAll(draw_properties_
.target_space_transform
, bounds(),
318 draw_properties_
.visible_layer_rect
, draw_properties_
.clip_rect
,
319 is_clipped_
, draw_properties_
.opacity
,
320 draw_properties_
.blend_mode
, sorting_context_id_
);
323 void LayerImpl::PopulateScaledSharedQuadState(SharedQuadState
* state
,
325 gfx::Transform scaled_draw_transform
=
326 draw_properties_
.target_space_transform
;
327 scaled_draw_transform
.Scale(SK_MScalar1
/ scale
, SK_MScalar1
/ scale
);
328 gfx::Size scaled_bounds
= gfx::ToCeiledSize(gfx::ScaleSize(bounds(), scale
));
329 gfx::Rect scaled_visible_layer_rect
=
330 gfx::ScaleToEnclosingRect(visible_layer_rect(), scale
);
331 scaled_visible_layer_rect
.Intersect(gfx::Rect(scaled_bounds
));
333 state
->SetAll(scaled_draw_transform
, scaled_bounds
, scaled_visible_layer_rect
,
334 draw_properties().clip_rect
, is_clipped_
,
335 draw_properties().opacity
, draw_properties().blend_mode
,
336 sorting_context_id_
);
339 bool LayerImpl::WillDraw(DrawMode draw_mode
,
340 ResourceProvider
* resource_provider
) {
341 // WillDraw/DidDraw must be matched.
342 DCHECK_NE(DRAW_MODE_NONE
, draw_mode
);
343 DCHECK_EQ(DRAW_MODE_NONE
, current_draw_mode_
);
344 current_draw_mode_
= draw_mode
;
348 void LayerImpl::DidDraw(ResourceProvider
* resource_provider
) {
349 DCHECK_NE(DRAW_MODE_NONE
, current_draw_mode_
);
350 current_draw_mode_
= DRAW_MODE_NONE
;
353 bool LayerImpl::ShowDebugBorders() const {
354 return layer_tree_impl()->debug_state().show_debug_borders
;
357 void LayerImpl::GetDebugBorderProperties(SkColor
* color
, float* width
) const {
358 if (draws_content_
) {
359 *color
= DebugColors::ContentLayerBorderColor();
360 *width
= DebugColors::ContentLayerBorderWidth(layer_tree_impl());
364 if (masks_to_bounds_
) {
365 *color
= DebugColors::MaskingLayerBorderColor();
366 *width
= DebugColors::MaskingLayerBorderWidth(layer_tree_impl());
370 *color
= DebugColors::ContainerLayerBorderColor();
371 *width
= DebugColors::ContainerLayerBorderWidth(layer_tree_impl());
374 void LayerImpl::AppendDebugBorderQuad(
375 RenderPass
* render_pass
,
376 const gfx::Size
& bounds
,
377 const SharedQuadState
* shared_quad_state
,
378 AppendQuadsData
* append_quads_data
) const {
381 GetDebugBorderProperties(&color
, &width
);
382 AppendDebugBorderQuad(render_pass
, bounds
, shared_quad_state
,
383 append_quads_data
, color
, width
);
386 void LayerImpl::AppendDebugBorderQuad(RenderPass
* render_pass
,
387 const gfx::Size
& bounds
,
388 const SharedQuadState
* shared_quad_state
,
389 AppendQuadsData
* append_quads_data
,
392 if (!ShowDebugBorders())
395 gfx::Rect
quad_rect(bounds
);
396 gfx::Rect
visible_quad_rect(quad_rect
);
397 DebugBorderDrawQuad
* debug_border_quad
=
398 render_pass
->CreateAndAppendDrawQuad
<DebugBorderDrawQuad
>();
399 debug_border_quad
->SetNew(
400 shared_quad_state
, quad_rect
, visible_quad_rect
, color
, width
);
401 if (contents_opaque()) {
402 // When opaque, draw a second inner border that is thicker than the outer
403 // border, but more transparent.
404 static const float kFillOpacity
= 0.3f
;
405 SkColor fill_color
= SkColorSetA(
406 color
, static_cast<uint8_t>(SkColorGetA(color
) * kFillOpacity
));
407 float fill_width
= width
* 3;
408 gfx::Rect fill_rect
= quad_rect
;
409 fill_rect
.Inset(fill_width
/ 2.f
, fill_width
/ 2.f
);
410 if (fill_rect
.IsEmpty())
412 gfx::Rect visible_fill_rect
=
413 gfx::IntersectRects(visible_quad_rect
, fill_rect
);
414 DebugBorderDrawQuad
* fill_quad
=
415 render_pass
->CreateAndAppendDrawQuad
<DebugBorderDrawQuad
>();
416 fill_quad
->SetNew(shared_quad_state
, fill_rect
, visible_fill_rect
,
417 fill_color
, fill_width
);
421 bool LayerImpl::HasDelegatedContent() const {
425 bool LayerImpl::HasContributingDelegatedRenderPasses() const {
429 RenderPassId
LayerImpl::FirstContributingRenderPassId() const {
430 return RenderPassId(0, 0);
433 RenderPassId
LayerImpl::NextContributingRenderPassId(RenderPassId id
) const {
434 return RenderPassId(0, 0);
437 void LayerImpl::GetContentsResourceId(ResourceId
* resource_id
,
438 gfx::Size
* resource_size
) const {
443 gfx::Vector2dF
LayerImpl::ScrollBy(const gfx::Vector2dF
& scroll
) {
444 gfx::ScrollOffset
adjusted_scroll(scroll
);
445 if (!user_scrollable_horizontal_
)
446 adjusted_scroll
.set_x(0);
447 if (!user_scrollable_vertical_
)
448 adjusted_scroll
.set_y(0);
449 DCHECK(scrollable());
450 gfx::ScrollOffset old_offset
= CurrentScrollOffset();
451 gfx::ScrollOffset new_offset
=
452 ClampScrollOffsetToLimits(old_offset
+ adjusted_scroll
);
453 SetCurrentScrollOffset(new_offset
);
455 gfx::ScrollOffset unscrolled
=
456 old_offset
+ gfx::ScrollOffset(scroll
) - new_offset
;
457 return gfx::Vector2dF(unscrolled
.x(), unscrolled
.y());
460 void LayerImpl::SetScrollClipLayer(int scroll_clip_layer_id
) {
461 scroll_clip_layer_
= layer_tree_impl()->LayerById(scroll_clip_layer_id
);
464 bool LayerImpl::user_scrollable(ScrollbarOrientation orientation
) const {
465 return (orientation
== HORIZONTAL
) ? user_scrollable_horizontal_
466 : user_scrollable_vertical_
;
469 void LayerImpl::ApplySentScrollDeltasFromAbortedCommit() {
470 DCHECK(layer_tree_impl()->IsActiveTree());
471 scroll_offset_
->AbortCommit();
474 InputHandler::ScrollStatus
LayerImpl::TryScroll(
475 const gfx::PointF
& screen_space_point
,
476 InputHandler::ScrollInputType type
,
477 ScrollBlocksOn effective_block_mode
) const {
478 if (should_scroll_on_main_thread()) {
479 TRACE_EVENT0("cc", "LayerImpl::TryScroll: Failed ShouldScrollOnMainThread");
480 return InputHandler::SCROLL_ON_MAIN_THREAD
;
483 if (!screen_space_transform().IsInvertible()) {
484 TRACE_EVENT0("cc", "LayerImpl::TryScroll: Ignored NonInvertibleTransform");
485 return InputHandler::SCROLL_IGNORED
;
488 if (!non_fast_scrollable_region().IsEmpty()) {
489 bool clipped
= false;
490 gfx::Transform
inverse_screen_space_transform(
491 gfx::Transform::kSkipInitialization
);
492 if (!screen_space_transform().GetInverse(&inverse_screen_space_transform
)) {
493 // TODO(shawnsingh): We shouldn't be applying a projection if screen space
494 // transform is uninvertible here. Perhaps we should be returning
495 // SCROLL_ON_MAIN_THREAD in this case?
498 gfx::PointF hit_test_point_in_layer_space
= MathUtil::ProjectPoint(
499 inverse_screen_space_transform
, screen_space_point
, &clipped
);
501 non_fast_scrollable_region().Contains(
502 gfx::ToRoundedPoint(hit_test_point_in_layer_space
))) {
504 "LayerImpl::tryScroll: Failed NonFastScrollableRegion");
505 return InputHandler::SCROLL_ON_MAIN_THREAD
;
509 if (have_scroll_event_handlers() &&
510 effective_block_mode
& SCROLL_BLOCKS_ON_SCROLL_EVENT
) {
511 TRACE_EVENT0("cc", "LayerImpl::tryScroll: Failed ScrollEventHandlers");
512 return InputHandler::SCROLL_ON_MAIN_THREAD
;
515 if (type
== InputHandler::WHEEL
&& have_wheel_event_handlers() &&
516 effective_block_mode
& SCROLL_BLOCKS_ON_WHEEL_EVENT
) {
517 TRACE_EVENT0("cc", "LayerImpl::tryScroll: Failed WheelEventHandlers");
518 return InputHandler::SCROLL_ON_MAIN_THREAD
;
522 TRACE_EVENT0("cc", "LayerImpl::tryScroll: Ignored not scrollable");
523 return InputHandler::SCROLL_IGNORED
;
526 gfx::ScrollOffset max_scroll_offset
= MaxScrollOffset();
527 if (max_scroll_offset
.x() <= 0 && max_scroll_offset
.y() <= 0) {
529 "LayerImpl::tryScroll: Ignored. Technically scrollable,"
530 " but has no affordance in either direction.");
531 return InputHandler::SCROLL_IGNORED
;
534 return InputHandler::SCROLL_STARTED
;
537 skia::RefPtr
<SkPicture
> LayerImpl::GetPicture() {
538 return skia::RefPtr
<SkPicture
>();
541 scoped_ptr
<LayerImpl
> LayerImpl::CreateLayerImpl(LayerTreeImpl
* tree_impl
) {
542 return LayerImpl::Create(tree_impl
, layer_id_
, scroll_offset_
);
545 void LayerImpl::PushPropertiesTo(LayerImpl
* layer
) {
546 layer
->SetTransformOrigin(transform_origin_
);
547 layer
->SetBackgroundColor(background_color_
);
548 layer
->SetBounds(bounds_
);
549 layer
->SetDoubleSided(double_sided_
);
550 layer
->SetDrawCheckerboardForMissingTiles(
551 draw_checkerboard_for_missing_tiles_
);
552 layer
->SetDrawsContent(DrawsContent());
553 layer
->SetHideLayerAndSubtree(hide_layer_and_subtree_
);
554 layer
->SetHasRenderSurface(!!render_surface());
555 layer
->SetFilters(filters());
556 layer
->SetBackgroundFilters(background_filters());
557 layer
->SetMasksToBounds(masks_to_bounds_
);
558 layer
->SetShouldScrollOnMainThread(should_scroll_on_main_thread_
);
559 layer
->SetHaveWheelEventHandlers(have_wheel_event_handlers_
);
560 layer
->SetHaveScrollEventHandlers(have_scroll_event_handlers_
);
561 layer
->SetScrollBlocksOn(scroll_blocks_on_
);
562 layer
->SetNonFastScrollableRegion(non_fast_scrollable_region_
);
563 layer
->SetTouchEventHandlerRegion(touch_event_handler_region_
);
564 layer
->SetContentsOpaque(contents_opaque_
);
565 layer
->SetOpacity(opacity_
);
566 layer
->SetBlendMode(blend_mode_
);
567 layer
->SetIsRootForIsolatedGroup(is_root_for_isolated_group_
);
568 layer
->SetPosition(position_
);
569 layer
->SetIsContainerForFixedPositionLayers(
570 is_container_for_fixed_position_layers_
);
571 layer
->SetPositionConstraint(position_constraint_
);
572 layer
->SetShouldFlattenTransform(should_flatten_transform_
);
573 layer
->set_should_flatten_transform_from_property_tree(
574 should_flatten_transform_from_property_tree_
);
575 layer
->set_is_clipped(is_clipped_
);
576 layer
->SetUseParentBackfaceVisibility(use_parent_backface_visibility_
);
577 layer
->SetTransformAndInvertibility(transform_
, transform_is_invertible_
);
579 layer
->SetScrollClipLayer(scroll_clip_layer_
? scroll_clip_layer_
->id()
580 : Layer::INVALID_ID
);
581 layer
->set_user_scrollable_horizontal(user_scrollable_horizontal_
);
582 layer
->set_user_scrollable_vertical(user_scrollable_vertical_
);
584 layer
->SetScrollCompensationAdjustment(scroll_compensation_adjustment_
);
586 layer
->PushScrollOffset(nullptr);
588 layer
->Set3dSortingContextId(sorting_context_id_
);
589 layer
->SetNumDescendantsThatDrawContent(num_descendants_that_draw_content_
);
591 layer
->SetTransformTreeIndex(transform_tree_index_
);
592 layer
->SetClipTreeIndex(clip_tree_index_
);
593 layer
->SetOpacityTreeIndex(opacity_tree_index_
);
594 layer
->set_offset_to_transform_parent(offset_to_transform_parent_
);
596 LayerImpl
* scroll_parent
= nullptr;
597 if (scroll_parent_
) {
598 scroll_parent
= layer
->layer_tree_impl()->LayerById(scroll_parent_
->id());
599 DCHECK(scroll_parent
);
602 layer
->SetScrollParent(scroll_parent
);
603 if (scroll_children_
) {
604 std::set
<LayerImpl
*>* scroll_children
= new std::set
<LayerImpl
*>;
605 for (std::set
<LayerImpl
*>::iterator it
= scroll_children_
->begin();
606 it
!= scroll_children_
->end();
608 DCHECK_EQ((*it
)->scroll_parent(), this);
609 LayerImpl
* scroll_child
=
610 layer
->layer_tree_impl()->LayerById((*it
)->id());
611 DCHECK(scroll_child
);
612 scroll_children
->insert(scroll_child
);
614 layer
->SetScrollChildren(scroll_children
);
616 layer
->SetScrollChildren(nullptr);
619 LayerImpl
* clip_parent
= nullptr;
621 clip_parent
= layer
->layer_tree_impl()->LayerById(
626 layer
->SetClipParent(clip_parent
);
627 if (clip_children_
) {
628 std::set
<LayerImpl
*>* clip_children
= new std::set
<LayerImpl
*>;
629 for (std::set
<LayerImpl
*>::iterator it
= clip_children_
->begin();
630 it
!= clip_children_
->end(); ++it
)
631 clip_children
->insert(layer
->layer_tree_impl()->LayerById((*it
)->id()));
632 layer
->SetClipChildren(clip_children
);
634 layer
->SetClipChildren(nullptr);
637 layer
->PassCopyRequests(©_requests_
);
639 // If the main thread commits multiple times before the impl thread actually
640 // draws, then damage tracking will become incorrect if we simply clobber the
641 // update_rect here. The LayerImpl's update_rect needs to accumulate (i.e.
642 // union) any update changes that have occurred on the main thread.
643 update_rect_
.Union(layer
->update_rect());
644 layer
->SetUpdateRect(update_rect_
);
646 layer
->SetStackingOrderChanged(stacking_order_changed_
);
647 layer
->SetDebugInfo(debug_info_
);
649 if (frame_timing_requests_dirty_
) {
650 layer
->SetFrameTimingRequests(frame_timing_requests_
);
651 frame_timing_requests_dirty_
= false;
654 // Reset any state that should be cleared for the next update.
655 stacking_order_changed_
= false;
656 update_rect_
= gfx::Rect();
657 needs_push_properties_
= false;
658 num_dependents_need_push_properties_
= 0;
661 gfx::Vector2dF
LayerImpl::FixedContainerSizeDelta() const {
662 if (!scroll_clip_layer_
)
663 return gfx::Vector2dF();
665 gfx::Vector2dF delta_from_scroll
= scroll_clip_layer_
->bounds_delta();
667 // In virtual-viewport mode, we don't need to compensate for pinch zoom or
668 // scale since the fixed container is the outer viewport, which sits below
670 return delta_from_scroll
;
673 base::DictionaryValue
* LayerImpl::LayerTreeAsJson() const {
674 base::DictionaryValue
* result
= new base::DictionaryValue
;
675 result
->SetInteger("LayerId", id());
676 result
->SetString("LayerType", LayerTypeAsString());
678 base::ListValue
* list
= new base::ListValue
;
679 list
->AppendInteger(bounds().width());
680 list
->AppendInteger(bounds().height());
681 result
->Set("Bounds", list
);
683 list
= new base::ListValue
;
684 list
->AppendDouble(position_
.x());
685 list
->AppendDouble(position_
.y());
686 result
->Set("Position", list
);
688 const gfx::Transform
& gfx_transform
= draw_properties_
.target_space_transform
;
689 double transform
[16];
690 gfx_transform
.matrix().asColMajord(transform
);
691 list
= new base::ListValue
;
692 for (int i
= 0; i
< 16; ++i
)
693 list
->AppendDouble(transform
[i
]);
694 result
->Set("DrawTransform", list
);
696 result
->SetBoolean("DrawsContent", draws_content_
);
697 result
->SetBoolean("Is3dSorted", Is3dSorted());
698 result
->SetDouble("OPACITY", opacity());
699 result
->SetBoolean("ContentsOpaque", contents_opaque_
);
702 result
->SetBoolean("Scrollable", true);
704 if (have_wheel_event_handlers_
)
705 result
->SetBoolean("WheelHandler", have_wheel_event_handlers_
);
706 if (have_scroll_event_handlers_
)
707 result
->SetBoolean("ScrollHandler", have_scroll_event_handlers_
);
708 if (!touch_event_handler_region_
.IsEmpty()) {
709 scoped_ptr
<base::Value
> region
= touch_event_handler_region_
.AsValue();
710 result
->Set("TouchRegion", region
.release());
713 if (scroll_blocks_on_
) {
714 list
= new base::ListValue
;
715 if (scroll_blocks_on_
& SCROLL_BLOCKS_ON_START_TOUCH
)
716 list
->AppendString("StartTouch");
717 if (scroll_blocks_on_
& SCROLL_BLOCKS_ON_WHEEL_EVENT
)
718 list
->AppendString("WheelEvent");
719 if (scroll_blocks_on_
& SCROLL_BLOCKS_ON_SCROLL_EVENT
)
720 list
->AppendString("ScrollEvent");
721 result
->Set("ScrollBlocksOn", list
);
724 list
= new base::ListValue
;
725 for (size_t i
= 0; i
< children_
.size(); ++i
)
726 list
->Append(children_
[i
]->LayerTreeAsJson());
727 result
->Set("Children", list
);
732 void LayerImpl::SetStackingOrderChanged(bool stacking_order_changed
) {
733 if (stacking_order_changed
) {
734 stacking_order_changed_
= true;
735 NoteLayerPropertyChangedForSubtree();
739 void LayerImpl::NoteLayerPropertyChanged() {
740 layer_property_changed_
= true;
741 layer_tree_impl()->set_needs_update_draw_properties();
742 SetNeedsPushProperties();
745 void LayerImpl::NoteLayerPropertyChangedForSubtree() {
746 layer_property_changed_
= true;
747 layer_tree_impl()->set_needs_update_draw_properties();
748 for (size_t i
= 0; i
< children_
.size(); ++i
)
749 children_
[i
]->NoteLayerPropertyChangedForDescendantsInternal();
750 SetNeedsPushProperties();
753 void LayerImpl::NoteLayerPropertyChangedForDescendantsInternal() {
754 layer_property_changed_
= true;
755 for (size_t i
= 0; i
< children_
.size(); ++i
)
756 children_
[i
]->NoteLayerPropertyChangedForDescendantsInternal();
759 void LayerImpl::NoteLayerPropertyChangedForDescendants() {
760 layer_tree_impl()->set_needs_update_draw_properties();
761 for (size_t i
= 0; i
< children_
.size(); ++i
)
762 children_
[i
]->NoteLayerPropertyChangedForDescendantsInternal();
763 SetNeedsPushProperties();
766 void LayerImpl::ValidateQuadResourcesInternal(DrawQuad
* quad
) const {
768 const ResourceProvider
* resource_provider
=
769 layer_tree_impl_
->resource_provider();
770 for (ResourceId resource_id
: quad
->resources
)
771 resource_provider
->ValidateResource(resource_id
);
775 const char* LayerImpl::LayerTypeAsString() const {
776 return "cc::LayerImpl";
779 void LayerImpl::ResetAllChangeTrackingForSubtree() {
780 layer_property_changed_
= false;
782 update_rect_
= gfx::Rect();
783 damage_rect_
= gfx::RectF();
786 render_surface_
->ResetPropertyChangedFlag();
789 mask_layer_
->ResetAllChangeTrackingForSubtree();
791 if (replica_layer_
) {
792 // This also resets the replica mask, if it exists.
793 replica_layer_
->ResetAllChangeTrackingForSubtree();
796 for (size_t i
= 0; i
< children_
.size(); ++i
)
797 children_
[i
]->ResetAllChangeTrackingForSubtree();
799 needs_push_properties_
= false;
800 num_dependents_need_push_properties_
= 0;
803 void LayerImpl::UpdatePropertyTreeTransform() {
804 if (transform_tree_index_
!= -1) {
805 TransformTree
& transform_tree
=
806 layer_tree_impl()->property_trees()->transform_tree
;
807 TransformNode
* node
= transform_tree
.Node(transform_tree_index_
);
808 // A LayerImpl's own current state is insufficient for determining whether
809 // it owns a TransformNode, since this depends on the state of the
810 // corresponding Layer at the time of the last commit. For example, a
811 // transform animation might have been in progress at the time the last
812 // commit started, but might have finished since then on the compositor
814 if (node
->owner_id
!= id())
816 if (node
->data
.local
!= transform_
) {
817 node
->data
.local
= transform_
;
818 node
->data
.needs_local_transform_update
= true;
819 transform_tree
.set_needs_update(true);
820 // TODO(ajuma): The current criteria for creating clip nodes means that
821 // property trees may need to be rebuilt when the new transform isn't
822 // axis-aligned wrt the old transform (see Layer::SetTransform). Since
823 // rebuilding property trees every frame of a transform animation is
824 // something we should try to avoid, change property tree-building so that
825 // it doesn't depend on axis aliginment.
830 void LayerImpl::UpdatePropertyTreeOpacity() {
831 if (opacity_tree_index_
!= -1) {
832 OpacityTree
& opacity_tree
=
833 layer_tree_impl()->property_trees()->opacity_tree
;
834 OpacityNode
* node
= opacity_tree
.Node(opacity_tree_index_
);
835 // A LayerImpl's own current state is insufficient for determining whether
836 // it owns an OpacityNode, since this depends on the state of the
837 // corresponding Layer at the time of the last commit. For example, an
838 // opacity animation might have been in progress at the time the last commit
839 // started, but might have finished since then on the compositor thread.
840 if (node
->owner_id
!= id())
842 node
->data
.opacity
= opacity_
;
843 opacity_tree
.set_needs_update(true);
847 void LayerImpl::UpdatePropertyTreeForScrollingAndAnimationIfNeeded() {
849 UpdatePropertyTreeScrollOffset();
851 if (HasAnyAnimationTargetingProperty(Animation::OPACITY
))
852 UpdatePropertyTreeOpacity();
854 if (HasAnyAnimationTargetingProperty(Animation::TRANSFORM
))
855 UpdatePropertyTreeTransform();
858 gfx::ScrollOffset
LayerImpl::ScrollOffsetForAnimation() const {
859 return CurrentScrollOffset();
862 void LayerImpl::OnFilterAnimated(const FilterOperations
& filters
) {
866 void LayerImpl::OnOpacityAnimated(float opacity
) {
868 UpdatePropertyTreeOpacity();
871 void LayerImpl::OnTransformAnimated(const gfx::Transform
& transform
) {
872 SetTransform(transform
);
873 UpdatePropertyTreeTransform();
876 void LayerImpl::OnScrollOffsetAnimated(const gfx::ScrollOffset
& scroll_offset
) {
877 // Only layers in the active tree should need to do anything here, since
878 // layers in the pending tree will find out about these changes as a
879 // result of the shared SyncedProperty.
883 SetCurrentScrollOffset(scroll_offset
);
885 layer_tree_impl_
->DidAnimateScrollOffset();
888 void LayerImpl::OnAnimationWaitingForDeletion() {}
890 bool LayerImpl::IsActive() const {
891 return layer_tree_impl_
->IsActiveTree();
894 gfx::Size
LayerImpl::bounds() const {
895 gfx::Vector2d delta
= gfx::ToCeiledVector2d(bounds_delta_
);
896 return gfx::Size(bounds_
.width() + delta
.x(),
897 bounds_
.height() + delta
.y());
900 gfx::SizeF
LayerImpl::BoundsForScrolling() const {
901 return gfx::SizeF(bounds_
.width() + bounds_delta_
.x(),
902 bounds_
.height() + bounds_delta_
.y());
905 void LayerImpl::SetBounds(const gfx::Size
& bounds
) {
906 if (bounds_
== bounds
)
911 ScrollbarParametersDidChange(true);
912 if (masks_to_bounds())
913 NoteLayerPropertyChangedForSubtree();
915 NoteLayerPropertyChanged();
918 void LayerImpl::SetBoundsDelta(const gfx::Vector2dF
& bounds_delta
) {
920 if (bounds_delta_
== bounds_delta
)
923 bounds_delta_
= bounds_delta
;
925 TransformTree
& transform_tree
=
926 layer_tree_impl()->property_trees()->transform_tree
;
927 if (this == layer_tree_impl()->InnerViewportContainerLayer())
928 transform_tree
.SetInnerViewportBoundsDelta(bounds_delta
);
929 else if (this == layer_tree_impl()->OuterViewportContainerLayer())
930 transform_tree
.SetOuterViewportBoundsDelta(bounds_delta
);
932 ScrollbarParametersDidChange(true);
934 if (masks_to_bounds()) {
935 // If layer is clipping, then update the clip node using the new bounds.
936 ClipNode
* clip_node
=
937 layer_tree_impl()->property_trees()->clip_tree
.Node(clip_tree_index());
939 DCHECK(id() == clip_node
->owner_id
);
940 clip_node
->data
.clip
=
941 gfx::RectF(gfx::PointF() + offset_to_transform_parent(), bounds());
942 layer_tree_impl()->property_trees()->clip_tree
.set_needs_update(true);
945 NoteLayerPropertyChangedForSubtree();
947 NoteLayerPropertyChanged();
951 void LayerImpl::SetMaskLayer(scoped_ptr
<LayerImpl
> mask_layer
) {
952 int new_layer_id
= mask_layer
? mask_layer
->id() : -1;
955 DCHECK_EQ(layer_tree_impl(), mask_layer
->layer_tree_impl());
956 DCHECK_NE(new_layer_id
, mask_layer_id_
);
957 } else if (new_layer_id
== mask_layer_id_
) {
961 mask_layer_
= mask_layer
.Pass();
962 mask_layer_id_
= new_layer_id
;
964 mask_layer_
->SetParent(this);
965 NoteLayerPropertyChangedForSubtree();
968 scoped_ptr
<LayerImpl
> LayerImpl::TakeMaskLayer() {
970 return mask_layer_
.Pass();
973 void LayerImpl::SetReplicaLayer(scoped_ptr
<LayerImpl
> replica_layer
) {
974 int new_layer_id
= replica_layer
? replica_layer
->id() : -1;
977 DCHECK_EQ(layer_tree_impl(), replica_layer
->layer_tree_impl());
978 DCHECK_NE(new_layer_id
, replica_layer_id_
);
979 } else if (new_layer_id
== replica_layer_id_
) {
983 replica_layer_
= replica_layer
.Pass();
984 replica_layer_id_
= new_layer_id
;
986 replica_layer_
->SetParent(this);
987 NoteLayerPropertyChangedForSubtree();
990 scoped_ptr
<LayerImpl
> LayerImpl::TakeReplicaLayer() {
991 replica_layer_id_
= -1;
992 return replica_layer_
.Pass();
995 ScrollbarLayerImplBase
* LayerImpl::ToScrollbarLayer() {
999 void LayerImpl::SetDrawsContent(bool draws_content
) {
1000 if (draws_content_
== draws_content
)
1003 draws_content_
= draws_content
;
1004 NoteLayerPropertyChanged();
1007 void LayerImpl::SetHideLayerAndSubtree(bool hide
) {
1008 if (hide_layer_and_subtree_
== hide
)
1011 hide_layer_and_subtree_
= hide
;
1012 NoteLayerPropertyChangedForSubtree();
1015 void LayerImpl::SetTransformOrigin(const gfx::Point3F
& transform_origin
) {
1016 if (transform_origin_
== transform_origin
)
1018 transform_origin_
= transform_origin
;
1019 NoteLayerPropertyChangedForSubtree();
1022 void LayerImpl::SetBackgroundColor(SkColor background_color
) {
1023 if (background_color_
== background_color
)
1026 background_color_
= background_color
;
1027 NoteLayerPropertyChanged();
1030 SkColor
LayerImpl::SafeOpaqueBackgroundColor() const {
1031 SkColor color
= background_color();
1032 if (SkColorGetA(color
) == 255 && !contents_opaque()) {
1033 color
= SK_ColorTRANSPARENT
;
1034 } else if (SkColorGetA(color
) != 255 && contents_opaque()) {
1035 for (const LayerImpl
* layer
= parent(); layer
;
1036 layer
= layer
->parent()) {
1037 color
= layer
->background_color();
1038 if (SkColorGetA(color
) == 255)
1041 if (SkColorGetA(color
) != 255)
1042 color
= layer_tree_impl()->background_color();
1043 if (SkColorGetA(color
) != 255)
1044 color
= SkColorSetA(color
, 255);
1049 void LayerImpl::SetFilters(const FilterOperations
& filters
) {
1050 if (filters_
== filters
)
1054 NoteLayerPropertyChangedForSubtree();
1057 bool LayerImpl::FilterIsAnimating() const {
1058 LayerAnimationController::ObserverType observer_type
=
1059 IsActive() ? LayerAnimationController::ObserverType::ACTIVE
1060 : LayerAnimationController::ObserverType::PENDING
;
1061 return layer_animation_controller_
1062 ? layer_animation_controller_
->IsCurrentlyAnimatingProperty(
1063 Animation::FILTER
, observer_type
)
1064 : layer_tree_impl_
->IsAnimatingFilterProperty(this);
1067 bool LayerImpl::HasPotentiallyRunningFilterAnimation() const {
1068 LayerAnimationController::ObserverType observer_type
=
1069 IsActive() ? LayerAnimationController::ObserverType::ACTIVE
1070 : LayerAnimationController::ObserverType::PENDING
;
1071 return layer_animation_controller_
1072 ? layer_animation_controller_
->IsPotentiallyAnimatingProperty(
1073 Animation::FILTER
, observer_type
)
1074 : layer_tree_impl_
->HasPotentiallyRunningFilterAnimation(this);
1077 bool LayerImpl::FilterIsAnimatingOnImplOnly() const {
1078 if (!layer_animation_controller_
)
1079 return layer_tree_impl_
->FilterIsAnimatingOnImplOnly(this);
1081 Animation
* filter_animation
=
1082 layer_animation_controller_
->GetAnimation(Animation::FILTER
);
1083 return filter_animation
&& filter_animation
->is_impl_only();
1086 void LayerImpl::SetBackgroundFilters(
1087 const FilterOperations
& filters
) {
1088 if (background_filters_
== filters
)
1091 background_filters_
= filters
;
1092 NoteLayerPropertyChanged();
1095 void LayerImpl::SetMasksToBounds(bool masks_to_bounds
) {
1096 if (masks_to_bounds_
== masks_to_bounds
)
1099 masks_to_bounds_
= masks_to_bounds
;
1100 NoteLayerPropertyChangedForSubtree();
1103 void LayerImpl::SetContentsOpaque(bool opaque
) {
1104 if (contents_opaque_
== opaque
)
1107 contents_opaque_
= opaque
;
1108 NoteLayerPropertyChangedForSubtree();
1111 void LayerImpl::SetOpacity(float opacity
) {
1112 if (opacity_
== opacity
)
1116 NoteLayerPropertyChangedForSubtree();
1119 bool LayerImpl::OpacityIsAnimating() const {
1120 LayerAnimationController::ObserverType observer_type
=
1121 IsActive() ? LayerAnimationController::ObserverType::ACTIVE
1122 : LayerAnimationController::ObserverType::PENDING
;
1123 return layer_animation_controller_
1124 ? layer_animation_controller_
->IsCurrentlyAnimatingProperty(
1125 Animation::OPACITY
, observer_type
)
1126 : layer_tree_impl_
->IsAnimatingOpacityProperty(this);
1129 bool LayerImpl::HasPotentiallyRunningOpacityAnimation() const {
1130 LayerAnimationController::ObserverType observer_type
=
1131 IsActive() ? LayerAnimationController::ObserverType::ACTIVE
1132 : LayerAnimationController::ObserverType::PENDING
;
1133 return layer_animation_controller_
1134 ? layer_animation_controller_
->IsPotentiallyAnimatingProperty(
1135 Animation::OPACITY
, observer_type
)
1136 : layer_tree_impl_
->HasPotentiallyRunningOpacityAnimation(this);
1139 bool LayerImpl::OpacityIsAnimatingOnImplOnly() const {
1140 if (!layer_animation_controller_
)
1141 return layer_tree_impl_
->OpacityIsAnimatingOnImplOnly(this);
1143 Animation
* opacity_animation
=
1144 layer_animation_controller_
->GetAnimation(Animation::OPACITY
);
1145 return opacity_animation
&& opacity_animation
->is_impl_only();
1148 void LayerImpl::SetBlendMode(SkXfermode::Mode blend_mode
) {
1149 if (blend_mode_
== blend_mode
)
1152 blend_mode_
= blend_mode
;
1153 NoteLayerPropertyChangedForSubtree();
1156 void LayerImpl::SetIsRootForIsolatedGroup(bool root
) {
1157 if (is_root_for_isolated_group_
== root
)
1160 is_root_for_isolated_group_
= root
;
1161 SetNeedsPushProperties();
1164 void LayerImpl::SetPosition(const gfx::PointF
& position
) {
1165 if (position_
== position
)
1168 position_
= position
;
1169 NoteLayerPropertyChangedForSubtree();
1172 void LayerImpl::SetShouldFlattenTransform(bool flatten
) {
1173 if (should_flatten_transform_
== flatten
)
1176 should_flatten_transform_
= flatten
;
1177 NoteLayerPropertyChangedForSubtree();
1180 void LayerImpl::Set3dSortingContextId(int id
) {
1181 if (id
== sorting_context_id_
)
1183 sorting_context_id_
= id
;
1184 NoteLayerPropertyChangedForSubtree();
1187 void LayerImpl::SetFrameTimingRequests(
1188 const std::vector
<FrameTimingRequest
>& requests
) {
1189 frame_timing_requests_
= requests
;
1190 frame_timing_requests_dirty_
= true;
1191 SetNeedsPushProperties();
1194 void LayerImpl::GatherFrameTimingRequestIds(std::vector
<int64_t>* request_ids
) {
1195 for (const auto& request
: frame_timing_requests_
)
1196 request_ids
->push_back(request
.id());
1199 void LayerImpl::SetTransform(const gfx::Transform
& transform
) {
1200 if (transform_
== transform
)
1203 transform_
= transform
;
1204 transform_is_invertible_
= transform_
.IsInvertible();
1205 NoteLayerPropertyChangedForSubtree();
1208 void LayerImpl::SetTransformAndInvertibility(const gfx::Transform
& transform
,
1209 bool transform_is_invertible
) {
1210 if (transform_
== transform
) {
1211 DCHECK(transform_is_invertible_
== transform_is_invertible
)
1212 << "Can't change invertibility if transform is unchanged";
1215 transform_
= transform
;
1216 transform_is_invertible_
= transform_is_invertible
;
1217 NoteLayerPropertyChangedForSubtree();
1220 bool LayerImpl::TransformIsAnimating() const {
1221 LayerAnimationController::ObserverType observer_type
=
1222 IsActive() ? LayerAnimationController::ObserverType::ACTIVE
1223 : LayerAnimationController::ObserverType::PENDING
;
1224 return layer_animation_controller_
1225 ? layer_animation_controller_
->IsCurrentlyAnimatingProperty(
1226 Animation::TRANSFORM
, observer_type
)
1227 : layer_tree_impl_
->IsAnimatingTransformProperty(this);
1230 bool LayerImpl::HasPotentiallyRunningTransformAnimation() const {
1231 LayerAnimationController::ObserverType observer_type
=
1232 IsActive() ? LayerAnimationController::ObserverType::ACTIVE
1233 : LayerAnimationController::ObserverType::PENDING
;
1234 return layer_animation_controller_
1235 ? layer_animation_controller_
->IsPotentiallyAnimatingProperty(
1236 Animation::TRANSFORM
, observer_type
)
1237 : layer_tree_impl_
->HasPotentiallyRunningTransformAnimation(this);
1240 bool LayerImpl::TransformIsAnimatingOnImplOnly() const {
1241 if (!layer_animation_controller_
)
1242 return layer_tree_impl_
->TransformIsAnimatingOnImplOnly(this);
1244 Animation
* transform_animation
=
1245 layer_animation_controller_
->GetAnimation(Animation::TRANSFORM
);
1246 return transform_animation
&& transform_animation
->is_impl_only();
1249 bool LayerImpl::HasOnlyTranslationTransforms() const {
1250 if (!layer_animation_controller_
)
1251 return layer_tree_impl_
->HasOnlyTranslationTransforms(this);
1253 return layer_animation_controller_
->HasOnlyTranslationTransforms();
1256 bool LayerImpl::MaximumTargetScale(float* max_scale
) const {
1257 if (!layer_animation_controller_
)
1258 return layer_tree_impl_
->MaximumTargetScale(this, max_scale
);
1260 return layer_animation_controller_
->MaximumTargetScale(max_scale
);
1263 bool LayerImpl::AnimationStartScale(float* start_scale
) const {
1264 if (!layer_animation_controller_
)
1265 return layer_tree_impl_
->AnimationStartScale(this, start_scale
);
1267 return layer_animation_controller_
->AnimationStartScale(start_scale
);
1270 bool LayerImpl::HasAnyAnimationTargetingProperty(
1271 Animation::TargetProperty property
) const {
1272 if (!layer_animation_controller_
)
1273 return layer_tree_impl_
->HasAnyAnimationTargetingProperty(this, property
);
1275 return !!layer_animation_controller_
->GetAnimation(property
);
1278 bool LayerImpl::HasFilterAnimationThatInflatesBounds() const {
1279 if (!layer_animation_controller_
)
1280 return layer_tree_impl_
->HasFilterAnimationThatInflatesBounds(this);
1282 return layer_animation_controller_
->HasFilterAnimationThatInflatesBounds();
1285 bool LayerImpl::HasTransformAnimationThatInflatesBounds() const {
1286 if (!layer_animation_controller_
)
1287 return layer_tree_impl_
->HasTransformAnimationThatInflatesBounds(this);
1289 return layer_animation_controller_
->HasTransformAnimationThatInflatesBounds();
1292 bool LayerImpl::HasAnimationThatInflatesBounds() const {
1293 if (!layer_animation_controller_
)
1294 return layer_tree_impl_
->HasAnimationThatInflatesBounds(this);
1296 return layer_animation_controller_
->HasAnimationThatInflatesBounds();
1299 bool LayerImpl::FilterAnimationBoundsForBox(const gfx::BoxF
& box
,
1300 gfx::BoxF
* bounds
) const {
1301 if (!layer_animation_controller_
)
1302 return layer_tree_impl_
->FilterAnimationBoundsForBox(this, box
, bounds
);
1304 return layer_animation_controller_
->FilterAnimationBoundsForBox(box
, bounds
);
1307 bool LayerImpl::TransformAnimationBoundsForBox(const gfx::BoxF
& box
,
1308 gfx::BoxF
* bounds
) const {
1309 if (!layer_animation_controller_
)
1310 return layer_tree_impl_
->TransformAnimationBoundsForBox(this, box
, bounds
);
1312 return layer_animation_controller_
->TransformAnimationBoundsForBox(box
,
1316 void LayerImpl::SetUpdateRect(const gfx::Rect
& update_rect
) {
1317 update_rect_
= update_rect
;
1318 SetNeedsPushProperties();
1321 void LayerImpl::AddDamageRect(const gfx::RectF
& damage_rect
) {
1322 damage_rect_
= gfx::UnionRects(damage_rect_
, damage_rect
);
1325 bool LayerImpl::IsExternalScrollActive() const {
1326 return layer_tree_impl_
->IsExternalScrollActive();
1329 void LayerImpl::SetCurrentScrollOffset(const gfx::ScrollOffset
& scroll_offset
) {
1331 if (scroll_offset_
->SetCurrent(scroll_offset
))
1332 DidUpdateScrollOffset(false);
1335 void LayerImpl::SetCurrentScrollOffsetFromDelegate(
1336 const gfx::ScrollOffset
& scroll_offset
) {
1338 if (scroll_offset_
->SetCurrent(scroll_offset
))
1339 DidUpdateScrollOffset(true);
1342 void LayerImpl::PushScrollOffsetFromMainThread(
1343 const gfx::ScrollOffset
& scroll_offset
) {
1344 PushScrollOffset(&scroll_offset
);
1347 void LayerImpl::PushScrollOffsetFromMainThreadAndClobberActiveValue(
1348 const gfx::ScrollOffset
& scroll_offset
) {
1349 scroll_offset_
->set_clobber_active_value();
1350 PushScrollOffset(&scroll_offset
);
1353 gfx::ScrollOffset
LayerImpl::PullDeltaForMainThread() {
1354 // TODO(miletus): Remove all this temporary flooring machinery when
1355 // Blink fully supports fractional scrolls.
1356 gfx::ScrollOffset current_offset
= CurrentScrollOffset();
1357 gfx::ScrollOffset current_delta
= IsActive()
1358 ? scroll_offset_
->Delta()
1359 : scroll_offset_
->PendingDelta().get();
1360 gfx::ScrollOffset
floored_delta(floor(current_delta
.x()),
1361 floor(current_delta
.y()));
1362 gfx::ScrollOffset diff_delta
= floored_delta
- current_delta
;
1363 gfx::ScrollOffset tmp_offset
= current_offset
+ diff_delta
;
1364 scroll_offset_
->SetCurrent(tmp_offset
);
1365 gfx::ScrollOffset delta
= scroll_offset_
->PullDeltaForMainThread();
1366 scroll_offset_
->SetCurrent(current_offset
);
1370 gfx::ScrollOffset
LayerImpl::CurrentScrollOffset() const {
1371 return scroll_offset_
->Current(IsActive());
1374 gfx::Vector2dF
LayerImpl::ScrollDelta() const {
1376 return gfx::Vector2dF(scroll_offset_
->Delta().x(),
1377 scroll_offset_
->Delta().y());
1379 return gfx::Vector2dF(scroll_offset_
->PendingDelta().get().x(),
1380 scroll_offset_
->PendingDelta().get().y());
1383 void LayerImpl::SetScrollDelta(const gfx::Vector2dF
& delta
) {
1385 DCHECK(scrollable() || delta
.IsZero());
1386 SetCurrentScrollOffset(scroll_offset_
->ActiveBase() +
1387 gfx::ScrollOffset(delta
));
1390 gfx::ScrollOffset
LayerImpl::BaseScrollOffset() const {
1392 return scroll_offset_
->ActiveBase();
1394 return scroll_offset_
->PendingBase();
1397 void LayerImpl::PushScrollOffset(const gfx::ScrollOffset
* scroll_offset
) {
1398 DCHECK(scroll_offset
|| IsActive());
1399 bool changed
= false;
1400 if (scroll_offset
) {
1401 DCHECK(!IsActive() || !layer_tree_impl_
->FindPendingTreeLayerById(id()));
1402 changed
|= scroll_offset_
->PushFromMainThread(*scroll_offset
);
1405 changed
|= scroll_offset_
->PushPendingToActive();
1409 DidUpdateScrollOffset(false);
1412 void LayerImpl::UpdatePropertyTreeScrollOffset() {
1413 // TODO(enne): in the future, scrolling should update the scroll tree
1414 // directly instead of going through layers.
1415 if (transform_tree_index_
!= -1) {
1416 TransformTree
& transform_tree
=
1417 layer_tree_impl()->property_trees()->transform_tree
;
1418 TransformNode
* node
= transform_tree
.Node(transform_tree_index_
);
1419 gfx::ScrollOffset current_offset
= scroll_offset_
->Current(IsActive());
1420 if (node
->data
.scroll_offset
!= current_offset
) {
1421 node
->data
.scroll_offset
= current_offset
;
1422 node
->data
.needs_local_transform_update
= true;
1423 transform_tree
.set_needs_update(true);
1428 void LayerImpl::DidUpdateScrollOffset(bool is_from_root_delegate
) {
1429 DCHECK(scroll_offset_
);
1431 if (!is_from_root_delegate
)
1432 layer_tree_impl()->DidUpdateScrollOffset(id());
1433 NoteLayerPropertyChangedForSubtree();
1434 ScrollbarParametersDidChange(false);
1436 UpdatePropertyTreeScrollOffset();
1438 // Inform the pending twin that a property changed.
1439 if (layer_tree_impl()->IsActiveTree()) {
1440 LayerImpl
* pending_twin
= layer_tree_impl()->FindPendingTreeLayerById(id());
1442 pending_twin
->DidUpdateScrollOffset(is_from_root_delegate
);
1446 void LayerImpl::SetDoubleSided(bool double_sided
) {
1447 if (double_sided_
== double_sided
)
1450 double_sided_
= double_sided
;
1451 NoteLayerPropertyChangedForSubtree();
1454 SimpleEnclosedRegion
LayerImpl::VisibleOpaqueRegion() const {
1455 if (contents_opaque())
1456 return SimpleEnclosedRegion(visible_layer_rect());
1457 return SimpleEnclosedRegion();
1460 void LayerImpl::DidBeginTracing() {}
1462 void LayerImpl::ReleaseResources() {}
1464 void LayerImpl::RecreateResources() {
1467 gfx::ScrollOffset
LayerImpl::MaxScrollOffset() const {
1468 if (!scroll_clip_layer_
|| bounds().IsEmpty())
1469 return gfx::ScrollOffset();
1471 LayerImpl
const* page_scale_layer
= layer_tree_impl()->PageScaleLayer();
1472 DCHECK(this != page_scale_layer
);
1473 DCHECK(this != layer_tree_impl()->InnerViewportScrollLayer() ||
1474 IsContainerForFixedPositionLayers());
1476 float scale_factor
= 1.f
;
1477 for (LayerImpl
const* current_layer
= this;
1478 current_layer
!= scroll_clip_layer_
->parent();
1479 current_layer
= current_layer
->parent()) {
1480 if (current_layer
== page_scale_layer
)
1481 scale_factor
= layer_tree_impl()->current_page_scale_factor();
1484 gfx::SizeF scaled_scroll_bounds
=
1485 gfx::ToFlooredSize(gfx::ScaleSize(BoundsForScrolling(), scale_factor
));
1486 scaled_scroll_bounds
= gfx::ToFlooredSize(scaled_scroll_bounds
);
1488 gfx::ScrollOffset
max_offset(
1489 scaled_scroll_bounds
.width() - scroll_clip_layer_
->bounds().width(),
1490 scaled_scroll_bounds
.height() - scroll_clip_layer_
->bounds().height());
1491 // We need the final scroll offset to be in CSS coords.
1492 max_offset
.Scale(1 / scale_factor
);
1493 max_offset
.SetToMax(gfx::ScrollOffset());
1497 gfx::ScrollOffset
LayerImpl::ClampScrollOffsetToLimits(
1498 gfx::ScrollOffset offset
) const {
1499 offset
.SetToMin(MaxScrollOffset());
1500 offset
.SetToMax(gfx::ScrollOffset());
1504 gfx::Vector2dF
LayerImpl::ClampScrollToMaxScrollOffset() {
1505 gfx::ScrollOffset old_offset
= CurrentScrollOffset();
1506 gfx::ScrollOffset clamped_offset
= ClampScrollOffsetToLimits(old_offset
);
1507 gfx::Vector2dF delta
= clamped_offset
.DeltaFrom(old_offset
);
1508 if (!delta
.IsZero())
1513 void LayerImpl::SetScrollbarPosition(ScrollbarLayerImplBase
* scrollbar_layer
,
1514 LayerImpl
* scrollbar_clip_layer
,
1515 bool on_resize
) const {
1516 DCHECK(scrollbar_layer
);
1517 LayerImpl
* page_scale_layer
= layer_tree_impl()->PageScaleLayer();
1519 DCHECK(this != page_scale_layer
);
1520 DCHECK(scrollbar_clip_layer
);
1521 gfx::RectF
clip_rect(gfx::PointF(),
1522 scrollbar_clip_layer
->BoundsForScrolling());
1524 // See comment in MaxScrollOffset() regarding the use of the content layer
1526 gfx::RectF
scroll_rect(gfx::PointF(), BoundsForScrolling());
1528 if (scroll_rect
.size().IsEmpty())
1531 gfx::ScrollOffset current_offset
;
1532 for (LayerImpl
const* current_layer
= this;
1533 current_layer
!= scrollbar_clip_layer
->parent();
1534 current_layer
= current_layer
->parent()) {
1535 current_offset
+= current_layer
->CurrentScrollOffset();
1536 if (current_layer
== page_scale_layer
) {
1537 float scale_factor
= layer_tree_impl()->current_page_scale_factor();
1538 current_offset
.Scale(scale_factor
);
1539 scroll_rect
.Scale(scale_factor
);
1543 bool scrollbar_needs_animation
= false;
1544 scrollbar_needs_animation
|= scrollbar_layer
->SetVerticalAdjust(
1545 scrollbar_clip_layer
->bounds_delta().y());
1546 if (scrollbar_layer
->orientation() == HORIZONTAL
) {
1547 float visible_ratio
= clip_rect
.width() / scroll_rect
.width();
1548 scrollbar_needs_animation
|=
1549 scrollbar_layer
->SetCurrentPos(current_offset
.x());
1550 scrollbar_needs_animation
|=
1551 scrollbar_layer
->SetMaximum(scroll_rect
.width() - clip_rect
.width());
1552 scrollbar_needs_animation
|=
1553 scrollbar_layer
->SetVisibleToTotalLengthRatio(visible_ratio
);
1555 float visible_ratio
= clip_rect
.height() / scroll_rect
.height();
1556 bool y_offset_did_change
=
1557 scrollbar_layer
->SetCurrentPos(current_offset
.y());
1558 scrollbar_needs_animation
|= y_offset_did_change
;
1559 scrollbar_needs_animation
|=
1560 scrollbar_layer
->SetMaximum(scroll_rect
.height() - clip_rect
.height());
1561 scrollbar_needs_animation
|=
1562 scrollbar_layer
->SetVisibleToTotalLengthRatio(visible_ratio
);
1563 if (y_offset_did_change
&& layer_tree_impl()->IsActiveTree() &&
1564 this == layer_tree_impl()->OuterViewportScrollLayer()) {
1565 TRACE_COUNTER_ID1("cc", "scroll_offset_y", this->id(),
1566 current_offset
.y());
1569 if (scrollbar_needs_animation
) {
1570 layer_tree_impl()->set_needs_update_draw_properties();
1571 // TODO(wjmaclean) The scrollbar animator for the pinch-zoom scrollbars
1572 // should activate for every scroll on the main frame, not just the
1573 // scrolls that move the pinch virtual viewport (i.e. trigger from
1574 // either inner or outer viewport).
1575 if (scrollbar_animation_controller_
) {
1576 // Non-overlay scrollbars shouldn't trigger animations.
1577 if (scrollbar_layer
->is_overlay_scrollbar())
1578 scrollbar_animation_controller_
->DidScrollUpdate(on_resize
);
1583 void LayerImpl::DidBecomeActive() {
1584 if (layer_tree_impl_
->settings().scrollbar_animator
==
1585 LayerTreeSettings::NO_ANIMATOR
) {
1589 bool need_scrollbar_animation_controller
= scrollable() && scrollbars_
;
1590 if (!need_scrollbar_animation_controller
) {
1591 scrollbar_animation_controller_
= nullptr;
1595 if (scrollbar_animation_controller_
)
1598 scrollbar_animation_controller_
=
1599 layer_tree_impl_
->CreateScrollbarAnimationController(this);
1602 void LayerImpl::ClearScrollbars() {
1606 scrollbars_
.reset(nullptr);
1609 void LayerImpl::AddScrollbar(ScrollbarLayerImplBase
* layer
) {
1611 DCHECK(!scrollbars_
|| scrollbars_
->find(layer
) == scrollbars_
->end());
1613 scrollbars_
.reset(new ScrollbarSet());
1615 scrollbars_
->insert(layer
);
1618 void LayerImpl::RemoveScrollbar(ScrollbarLayerImplBase
* layer
) {
1619 DCHECK(scrollbars_
);
1621 DCHECK(scrollbars_
->find(layer
) != scrollbars_
->end());
1623 scrollbars_
->erase(layer
);
1624 if (scrollbars_
->empty())
1625 scrollbars_
= nullptr;
1628 bool LayerImpl::HasScrollbar(ScrollbarOrientation orientation
) const {
1632 for (ScrollbarSet::iterator it
= scrollbars_
->begin();
1633 it
!= scrollbars_
->end();
1635 if ((*it
)->orientation() == orientation
)
1641 void LayerImpl::ScrollbarParametersDidChange(bool on_resize
) {
1645 for (ScrollbarSet::iterator it
= scrollbars_
->begin();
1646 it
!= scrollbars_
->end();
1648 bool is_scroll_layer
= (*it
)->ScrollLayerId() == layer_id_
;
1649 bool scroll_layer_resized
= is_scroll_layer
&& on_resize
;
1650 (*it
)->ScrollbarParametersDidChange(scroll_layer_resized
);
1654 void LayerImpl::SetNeedsPushProperties() {
1655 if (needs_push_properties_
)
1657 if (!parent_should_know_need_push_properties() && parent_
)
1658 parent_
->AddDependentNeedsPushProperties();
1659 needs_push_properties_
= true;
1662 void LayerImpl::AddDependentNeedsPushProperties() {
1663 DCHECK_GE(num_dependents_need_push_properties_
, 0);
1665 if (!parent_should_know_need_push_properties() && parent_
)
1666 parent_
->AddDependentNeedsPushProperties();
1668 num_dependents_need_push_properties_
++;
1671 void LayerImpl::RemoveDependentNeedsPushProperties() {
1672 num_dependents_need_push_properties_
--;
1673 DCHECK_GE(num_dependents_need_push_properties_
, 0);
1675 if (!parent_should_know_need_push_properties() && parent_
)
1676 parent_
->RemoveDependentNeedsPushProperties();
1679 void LayerImpl::GetAllPrioritizedTilesForTracing(
1680 std::vector
<PrioritizedTile
>* prioritized_tiles
) const {
1683 void LayerImpl::AsValueInto(base::trace_event::TracedValue
* state
) const {
1684 TracedValue::MakeDictIntoImplicitSnapshotWithCategory(
1685 TRACE_DISABLED_BY_DEFAULT("cc.debug"),
1688 LayerTypeAsString(),
1690 state
->SetInteger("layer_id", id());
1691 MathUtil::AddToTracedValue("bounds", bounds_
, state
);
1693 state
->SetDouble("opacity", opacity());
1695 MathUtil::AddToTracedValue("position", position_
, state
);
1697 state
->SetInteger("draws_content", DrawsContent());
1698 state
->SetInteger("gpu_memory_usage",
1699 base::saturated_cast
<int>(GPUMemoryUsageInBytes()));
1701 MathUtil::AddToTracedValue(
1702 "scroll_offset", scroll_offset_
? scroll_offset_
->Current(IsActive())
1703 : gfx::ScrollOffset(),
1706 MathUtil::AddToTracedValue("transform_origin", transform_origin_
, state
);
1709 gfx::QuadF layer_quad
= MathUtil::MapQuad(
1710 screen_space_transform(), gfx::QuadF(gfx::Rect(bounds())), &clipped
);
1711 MathUtil::AddToTracedValue("layer_quad", layer_quad
, state
);
1712 if (!touch_event_handler_region_
.IsEmpty()) {
1713 state
->BeginArray("touch_event_handler_region");
1714 touch_event_handler_region_
.AsValueInto(state
);
1717 if (have_wheel_event_handlers_
) {
1718 gfx::Rect
wheel_rect(bounds());
1719 Region
wheel_region(wheel_rect
);
1720 state
->BeginArray("wheel_event_handler_region");
1721 wheel_region
.AsValueInto(state
);
1724 if (have_scroll_event_handlers_
) {
1725 gfx::Rect
scroll_rect(bounds());
1726 Region
scroll_region(scroll_rect
);
1727 state
->BeginArray("scroll_event_handler_region");
1728 scroll_region
.AsValueInto(state
);
1731 if (!non_fast_scrollable_region_
.IsEmpty()) {
1732 state
->BeginArray("non_fast_scrollable_region");
1733 non_fast_scrollable_region_
.AsValueInto(state
);
1736 if (scroll_blocks_on_
) {
1737 state
->SetInteger("scroll_blocks_on", scroll_blocks_on_
);
1740 state
->BeginArray("children");
1741 for (size_t i
= 0; i
< children_
.size(); ++i
) {
1742 state
->BeginDictionary();
1743 children_
[i
]->AsValueInto(state
);
1744 state
->EndDictionary();
1748 state
->BeginDictionary("mask_layer");
1749 mask_layer_
->AsValueInto(state
);
1750 state
->EndDictionary();
1752 if (replica_layer_
) {
1753 state
->BeginDictionary("replica_layer");
1754 replica_layer_
->AsValueInto(state
);
1755 state
->EndDictionary();
1759 state
->SetInteger("scroll_parent", scroll_parent_
->id());
1762 state
->SetInteger("clip_parent", clip_parent_
->id());
1764 state
->SetBoolean("can_use_lcd_text", can_use_lcd_text());
1765 state
->SetBoolean("contents_opaque", contents_opaque());
1768 "has_animation_bounds",
1769 layer_animation_controller_
1770 ? layer_animation_controller_
->HasAnimationThatInflatesBounds()
1771 : layer_tree_impl_
->HasAnimationThatInflatesBounds(this));
1774 if (LayerUtils::GetAnimationBounds(*this, &box
))
1775 MathUtil::AddToTracedValue("animation_bounds", box
, state
);
1777 if (debug_info_
.get()) {
1779 debug_info_
->AppendAsTraceFormat(&str
);
1780 base::JSONReader json_reader
;
1781 scoped_ptr
<base::Value
> debug_info_value(json_reader
.ReadToValue(str
));
1783 if (debug_info_value
->IsType(base::Value::TYPE_DICTIONARY
)) {
1784 base::DictionaryValue
* dictionary_value
= nullptr;
1785 bool converted_to_dictionary
=
1786 debug_info_value
->GetAsDictionary(&dictionary_value
);
1787 DCHECK(converted_to_dictionary
);
1788 for (base::DictionaryValue::Iterator
it(*dictionary_value
); !it
.IsAtEnd();
1790 state
->SetValue(it
.key().data(), it
.value().CreateDeepCopy());
1797 if (!frame_timing_requests_
.empty()) {
1798 state
->BeginArray("frame_timing_requests");
1799 for (const auto& request
: frame_timing_requests_
) {
1800 state
->BeginDictionary();
1801 state
->SetInteger("request_id", request
.id());
1802 MathUtil::AddToTracedValue("request_rect", request
.rect(), state
);
1803 state
->EndDictionary();
1809 bool LayerImpl::IsDrawnRenderSurfaceLayerListMember() const {
1810 return draw_properties_
.last_drawn_render_surface_layer_list_id
==
1811 layer_tree_impl_
->current_render_surface_list_id();
1814 size_t LayerImpl::GPUMemoryUsageInBytes() const { return 0; }
1816 void LayerImpl::RunMicroBenchmark(MicroBenchmarkImpl
* benchmark
) {
1817 benchmark
->RunOnLayer(this);
1820 int LayerImpl::NumDescendantsThatDrawContent() const {
1821 return num_descendants_that_draw_content_
;
1824 void LayerImpl::NotifyAnimationFinished(
1825 base::TimeTicks monotonic_time
,
1826 Animation::TargetProperty target_property
,
1828 if (target_property
== Animation::SCROLL_OFFSET
)
1829 layer_tree_impl_
->InputScrollAnimationFinished();
1832 void LayerImpl::SetHasRenderSurface(bool should_have_render_surface
) {
1833 if (!!render_surface() == should_have_render_surface
)
1836 SetNeedsPushProperties();
1837 layer_tree_impl()->set_needs_update_draw_properties();
1838 if (should_have_render_surface
) {
1839 render_surface_
= make_scoped_ptr(new RenderSurfaceImpl(this));
1842 render_surface_
.reset();
1845 Region
LayerImpl::GetInvalidationRegion() {
1846 return Region(update_rect_
);
1849 gfx::Rect
LayerImpl::GetEnclosingRectInTargetSpace() const {
1850 return MathUtil::MapEnclosingClippedRect(
1851 draw_properties_
.target_space_transform
, gfx::Rect(bounds()));
1854 gfx::Rect
LayerImpl::GetScaledEnclosingRectInTargetSpace(float scale
) const {
1855 gfx::Transform scaled_draw_transform
=
1856 draw_properties_
.target_space_transform
;
1857 scaled_draw_transform
.Scale(SK_MScalar1
/ scale
, SK_MScalar1
/ scale
);
1858 gfx::Size scaled_bounds
= gfx::ToCeiledSize(gfx::ScaleSize(bounds(), scale
));
1859 return MathUtil::MapEnclosingClippedRect(scaled_draw_transform
,
1860 gfx::Rect(scaled_bounds
));
1863 float LayerImpl::GetIdealContentsScale() const {
1864 float page_scale
= IsAffectedByPageScale()
1865 ? layer_tree_impl()->current_page_scale_factor()
1867 float device_scale
= layer_tree_impl()->device_scale_factor();
1869 float default_scale
= page_scale
* device_scale
;
1870 if (!layer_tree_impl()
1872 .layer_transforms_should_scale_layer_contents
) {
1873 return default_scale
;
1876 // TODO(enne): the transform needs to come from property trees instead of
1878 gfx::Vector2dF transform_scales
= MathUtil::ComputeTransform2dScaleComponents(
1879 draw_properties().target_space_transform
, default_scale
);
1880 return std::max(transform_scales
.x(), transform_scales
.y());