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/strings/stringprintf.h"
9 #include "base/trace_event/trace_event.h"
10 #include "base/trace_event/trace_event_argument.h"
11 #include "cc/animation/animation_registrar.h"
12 #include "cc/animation/scrollbar_animation_controller.h"
13 #include "cc/base/math_util.h"
14 #include "cc/base/simple_enclosed_region.h"
15 #include "cc/debug/debug_colors.h"
16 #include "cc/debug/layer_tree_debug_state.h"
17 #include "cc/debug/micro_benchmark_impl.h"
18 #include "cc/debug/traced_value.h"
19 #include "cc/layers/layer_utils.h"
20 #include "cc/layers/painted_scrollbar_layer_impl.h"
21 #include "cc/output/copy_output_request.h"
22 #include "cc/quads/debug_border_draw_quad.h"
23 #include "cc/quads/render_pass.h"
24 #include "cc/trees/layer_tree_host_common.h"
25 #include "cc/trees/layer_tree_impl.h"
26 #include "cc/trees/layer_tree_settings.h"
27 #include "cc/trees/proxy.h"
28 #include "ui/gfx/geometry/box_f.h"
29 #include "ui/gfx/geometry/point_conversions.h"
30 #include "ui/gfx/geometry/quad_f.h"
31 #include "ui/gfx/geometry/rect_conversions.h"
32 #include "ui/gfx/geometry/size_conversions.h"
33 #include "ui/gfx/geometry/vector2d_conversions.h"
36 LayerImpl::LayerImpl(LayerTreeImpl
* layer_impl
, int id
)
37 : LayerImpl(layer_impl
, id
, new LayerImpl::SyncedScrollOffset
) {
40 LayerImpl::LayerImpl(LayerTreeImpl
* tree_impl
,
42 scoped_refptr
<SyncedScrollOffset
> scroll_offset
)
44 scroll_parent_(nullptr),
45 clip_parent_(nullptr),
47 replica_layer_id_(-1),
49 layer_tree_impl_(tree_impl
),
50 scroll_offset_(scroll_offset
),
51 scroll_clip_layer_(nullptr),
52 should_scroll_on_main_thread_(false),
53 have_wheel_event_handlers_(false),
54 have_scroll_event_handlers_(false),
55 scroll_blocks_on_(SCROLL_BLOCKS_ON_NONE
),
56 user_scrollable_horizontal_(true),
57 user_scrollable_vertical_(true),
58 stacking_order_changed_(false),
60 should_flatten_transform_(true),
61 should_flatten_transform_from_property_tree_(false),
62 layer_property_changed_(false),
63 masks_to_bounds_(false),
64 contents_opaque_(false),
65 is_root_for_isolated_group_(false),
66 use_parent_backface_visibility_(false),
67 draw_checkerboard_for_missing_tiles_(false),
68 draws_content_(false),
69 hide_layer_and_subtree_(false),
70 transform_is_invertible_(true),
71 is_container_for_fixed_position_layers_(false),
74 blend_mode_(SkXfermode::kSrcOver_Mode
),
75 num_descendants_that_draw_content_(0),
76 transform_tree_index_(-1),
77 opacity_tree_index_(-1),
80 needs_push_properties_(false),
81 num_dependents_need_push_properties_(0),
82 sorting_context_id_(0),
83 current_draw_mode_(DRAW_MODE_NONE
),
84 frame_timing_requests_dirty_(false),
86 layer_or_descendant_is_drawn_(false),
87 sorted_for_recursion_(false) {
88 DCHECK_GT(layer_id_
, 0);
89 DCHECK(layer_tree_impl_
);
90 layer_tree_impl_
->RegisterLayer(this);
91 AnimationRegistrar
* registrar
= layer_tree_impl_
->GetAnimationRegistrar();
92 layer_animation_controller_
=
93 registrar
->GetAnimationControllerForId(layer_id_
);
94 layer_animation_controller_
->AddValueObserver(this);
96 layer_animation_controller_
->set_value_provider(this);
97 layer_animation_controller_
->set_layer_animation_delegate(this);
99 SetNeedsPushProperties();
102 LayerImpl::~LayerImpl() {
103 DCHECK_EQ(DRAW_MODE_NONE
, current_draw_mode_
);
105 layer_animation_controller_
->RemoveValueObserver(this);
106 layer_animation_controller_
->remove_value_provider(this);
107 layer_animation_controller_
->remove_layer_animation_delegate(this);
109 if (!copy_requests_
.empty() && layer_tree_impl_
->IsActiveTree())
110 layer_tree_impl()->RemoveLayerWithCopyOutputRequest(this);
111 layer_tree_impl_
->UnregisterLayer(this);
113 TRACE_EVENT_OBJECT_DELETED_WITH_ID(
114 TRACE_DISABLED_BY_DEFAULT("cc.debug"), "cc::LayerImpl", this);
117 void LayerImpl::AddChild(scoped_ptr
<LayerImpl
> child
) {
118 child
->SetParent(this);
119 DCHECK_EQ(layer_tree_impl(), child
->layer_tree_impl());
120 children_
.push_back(child
.Pass());
121 layer_tree_impl()->set_needs_update_draw_properties();
124 scoped_ptr
<LayerImpl
> LayerImpl::RemoveChild(LayerImpl
* child
) {
125 for (OwnedLayerImplList::iterator it
= children_
.begin();
126 it
!= children_
.end();
129 scoped_ptr
<LayerImpl
> ret
= children_
.take(it
);
131 layer_tree_impl()->set_needs_update_draw_properties();
138 void LayerImpl::SetParent(LayerImpl
* parent
) {
139 if (parent_should_know_need_push_properties()) {
141 parent_
->RemoveDependentNeedsPushProperties();
143 parent
->AddDependentNeedsPushProperties();
148 void LayerImpl::ClearChildList() {
149 if (children_
.empty())
153 layer_tree_impl()->set_needs_update_draw_properties();
156 bool LayerImpl::HasAncestor(const LayerImpl
* ancestor
) const {
160 for (const LayerImpl
* layer
= this; layer
; layer
= layer
->parent()) {
161 if (layer
== ancestor
)
168 void LayerImpl::SetScrollParent(LayerImpl
* parent
) {
169 if (scroll_parent_
== parent
)
173 DCHECK_EQ(layer_tree_impl()->LayerById(parent
->id()), parent
);
175 scroll_parent_
= parent
;
176 SetNeedsPushProperties();
179 void LayerImpl::SetDebugInfo(
180 scoped_refptr
<base::trace_event::ConvertableToTraceFormat
> other
) {
182 SetNeedsPushProperties();
185 void LayerImpl::SetScrollChildren(std::set
<LayerImpl
*>* children
) {
186 if (scroll_children_
.get() == children
)
188 scroll_children_
.reset(children
);
189 SetNeedsPushProperties();
192 void LayerImpl::SetNumDescendantsThatDrawContent(int num_descendants
) {
193 if (num_descendants_that_draw_content_
== num_descendants
)
195 num_descendants_that_draw_content_
= num_descendants
;
196 SetNeedsPushProperties();
199 void LayerImpl::SetClipParent(LayerImpl
* ancestor
) {
200 if (clip_parent_
== ancestor
)
203 clip_parent_
= ancestor
;
204 SetNeedsPushProperties();
207 void LayerImpl::SetClipChildren(std::set
<LayerImpl
*>* children
) {
208 if (clip_children_
.get() == children
)
210 clip_children_
.reset(children
);
211 SetNeedsPushProperties();
214 void LayerImpl::SetTransformTreeIndex(int index
) {
215 transform_tree_index_
= index
;
216 SetNeedsPushProperties();
219 void LayerImpl::SetClipTreeIndex(int index
) {
220 clip_tree_index_
= index
;
221 SetNeedsPushProperties();
224 void LayerImpl::SetOpacityTreeIndex(int index
) {
225 opacity_tree_index_
= index
;
226 SetNeedsPushProperties();
229 void LayerImpl::PassCopyRequests(ScopedPtrVector
<CopyOutputRequest
>* requests
) {
230 if (requests
->empty())
232 DCHECK(render_surface());
233 bool was_empty
= copy_requests_
.empty();
234 copy_requests_
.insert_and_take(copy_requests_
.end(), requests
);
237 if (was_empty
&& layer_tree_impl()->IsActiveTree())
238 layer_tree_impl()->AddLayerWithCopyOutputRequest(this);
239 NoteLayerPropertyChangedForSubtree();
242 void LayerImpl::TakeCopyRequestsAndTransformToTarget(
243 ScopedPtrVector
<CopyOutputRequest
>* requests
) {
244 DCHECK(!copy_requests_
.empty());
245 DCHECK(layer_tree_impl()->IsActiveTree());
246 DCHECK_EQ(render_target(), this);
248 size_t first_inserted_request
= requests
->size();
249 requests
->insert_and_take(requests
->end(), ©_requests_
);
250 copy_requests_
.clear();
252 for (size_t i
= first_inserted_request
; i
< requests
->size(); ++i
) {
253 CopyOutputRequest
* request
= requests
->at(i
);
254 if (!request
->has_area())
257 gfx::Rect request_in_layer_space
= request
->area();
258 gfx::Rect request_in_content_space
=
259 LayerRectToContentRect(request_in_layer_space
);
260 request
->set_area(MathUtil::MapEnclosingClippedRect(
261 draw_properties_
.target_space_transform
, request_in_content_space
));
264 layer_tree_impl()->RemoveLayerWithCopyOutputRequest(this);
265 layer_tree_impl()->set_needs_update_draw_properties();
268 void LayerImpl::ClearRenderSurfaceLayerList() {
270 render_surface_
->ClearLayerLists();
273 void LayerImpl::PopulateSharedQuadState(SharedQuadState
* state
) const {
275 draw_properties_
.target_space_transform
, draw_properties_
.content_bounds
,
276 draw_properties_
.visible_content_rect
, draw_properties_
.clip_rect
,
277 draw_properties_
.is_clipped
, draw_properties_
.opacity
,
278 draw_properties_
.blend_mode
, sorting_context_id_
);
281 void LayerImpl::PopulateScaledSharedQuadState(SharedQuadState
* state
,
283 gfx::Transform scaled_draw_transform
=
284 draw_properties_
.target_space_transform
;
285 scaled_draw_transform
.Scale(SK_MScalar1
/ scale
, SK_MScalar1
/ scale
);
286 gfx::Size scaled_content_bounds
=
287 gfx::ToCeiledSize(gfx::ScaleSize(bounds(), scale
));
288 gfx::Rect scaled_visible_content_rect
=
289 gfx::ScaleToEnclosingRect(visible_content_rect(), scale
);
290 scaled_visible_content_rect
.Intersect(gfx::Rect(scaled_content_bounds
));
292 state
->SetAll(scaled_draw_transform
, scaled_content_bounds
,
293 scaled_visible_content_rect
, draw_properties().clip_rect
,
294 draw_properties().is_clipped
, draw_properties().opacity
,
295 draw_properties().blend_mode
, sorting_context_id_
);
298 bool LayerImpl::WillDraw(DrawMode draw_mode
,
299 ResourceProvider
* resource_provider
) {
300 // WillDraw/DidDraw must be matched.
301 DCHECK_NE(DRAW_MODE_NONE
, draw_mode
);
302 DCHECK_EQ(DRAW_MODE_NONE
, current_draw_mode_
);
303 current_draw_mode_
= draw_mode
;
307 void LayerImpl::DidDraw(ResourceProvider
* resource_provider
) {
308 DCHECK_NE(DRAW_MODE_NONE
, current_draw_mode_
);
309 current_draw_mode_
= DRAW_MODE_NONE
;
312 bool LayerImpl::ShowDebugBorders() const {
313 return layer_tree_impl()->debug_state().show_debug_borders
;
316 void LayerImpl::GetDebugBorderProperties(SkColor
* color
, float* width
) const {
317 if (draws_content_
) {
318 *color
= DebugColors::ContentLayerBorderColor();
319 *width
= DebugColors::ContentLayerBorderWidth(layer_tree_impl());
323 if (masks_to_bounds_
) {
324 *color
= DebugColors::MaskingLayerBorderColor();
325 *width
= DebugColors::MaskingLayerBorderWidth(layer_tree_impl());
329 *color
= DebugColors::ContainerLayerBorderColor();
330 *width
= DebugColors::ContainerLayerBorderWidth(layer_tree_impl());
333 void LayerImpl::AppendDebugBorderQuad(
334 RenderPass
* render_pass
,
335 const gfx::Size
& content_bounds
,
336 const SharedQuadState
* shared_quad_state
,
337 AppendQuadsData
* append_quads_data
) const {
340 GetDebugBorderProperties(&color
, &width
);
341 AppendDebugBorderQuad(render_pass
,
349 void LayerImpl::AppendDebugBorderQuad(RenderPass
* render_pass
,
350 const gfx::Size
& content_bounds
,
351 const SharedQuadState
* shared_quad_state
,
352 AppendQuadsData
* append_quads_data
,
355 if (!ShowDebugBorders())
358 gfx::Rect
quad_rect(content_bounds
);
359 gfx::Rect
visible_quad_rect(quad_rect
);
360 DebugBorderDrawQuad
* debug_border_quad
=
361 render_pass
->CreateAndAppendDrawQuad
<DebugBorderDrawQuad
>();
362 debug_border_quad
->SetNew(
363 shared_quad_state
, quad_rect
, visible_quad_rect
, color
, width
);
364 if (contents_opaque()) {
365 // When opaque, draw a second inner border that is thicker than the outer
366 // border, but more transparent.
367 static const float kFillOpacity
= 0.3f
;
368 SkColor fill_color
= SkColorSetA(
369 color
, static_cast<uint8_t>(SkColorGetA(color
) * kFillOpacity
));
370 float fill_width
= width
* 3;
371 gfx::Rect fill_rect
= quad_rect
;
372 fill_rect
.Inset(fill_width
/ 2.f
, fill_width
/ 2.f
);
373 if (fill_rect
.IsEmpty())
375 gfx::Rect visible_fill_rect
=
376 gfx::IntersectRects(visible_quad_rect
, fill_rect
);
377 DebugBorderDrawQuad
* fill_quad
=
378 render_pass
->CreateAndAppendDrawQuad
<DebugBorderDrawQuad
>();
379 fill_quad
->SetNew(shared_quad_state
, fill_rect
, visible_fill_rect
,
380 fill_color
, fill_width
);
384 bool LayerImpl::HasDelegatedContent() const {
388 bool LayerImpl::HasContributingDelegatedRenderPasses() const {
392 RenderPassId
LayerImpl::FirstContributingRenderPassId() const {
393 return RenderPassId(0, 0);
396 RenderPassId
LayerImpl::NextContributingRenderPassId(RenderPassId id
) const {
397 return RenderPassId(0, 0);
400 void LayerImpl::GetContentsResourceId(ResourceId
* resource_id
,
401 gfx::Size
* resource_size
) const {
406 gfx::Vector2dF
LayerImpl::ScrollBy(const gfx::Vector2dF
& scroll
) {
407 gfx::ScrollOffset
adjusted_scroll(scroll
);
408 if (!user_scrollable_horizontal_
)
409 adjusted_scroll
.set_x(0);
410 if (!user_scrollable_vertical_
)
411 adjusted_scroll
.set_y(0);
412 DCHECK(scrollable());
413 gfx::ScrollOffset old_offset
= CurrentScrollOffset();
414 gfx::ScrollOffset new_offset
=
415 ClampScrollOffsetToLimits(old_offset
+ adjusted_scroll
);
416 SetCurrentScrollOffset(new_offset
);
418 gfx::ScrollOffset unscrolled
=
419 old_offset
+ gfx::ScrollOffset(scroll
) - new_offset
;
420 return gfx::Vector2dF(unscrolled
.x(), unscrolled
.y());
423 void LayerImpl::SetScrollClipLayer(int scroll_clip_layer_id
) {
424 scroll_clip_layer_
= layer_tree_impl()->LayerById(scroll_clip_layer_id
);
427 bool LayerImpl::user_scrollable(ScrollbarOrientation orientation
) const {
428 return (orientation
== HORIZONTAL
) ? user_scrollable_horizontal_
429 : user_scrollable_vertical_
;
432 void LayerImpl::ApplySentScrollDeltasFromAbortedCommit() {
433 DCHECK(layer_tree_impl()->IsActiveTree());
434 scroll_offset_
->AbortCommit();
437 InputHandler::ScrollStatus
LayerImpl::TryScroll(
438 const gfx::PointF
& screen_space_point
,
439 InputHandler::ScrollInputType type
,
440 ScrollBlocksOn effective_block_mode
) const {
441 if (should_scroll_on_main_thread()) {
442 TRACE_EVENT0("cc", "LayerImpl::TryScroll: Failed ShouldScrollOnMainThread");
443 return InputHandler::SCROLL_ON_MAIN_THREAD
;
446 if (!screen_space_transform().IsInvertible()) {
447 TRACE_EVENT0("cc", "LayerImpl::TryScroll: Ignored NonInvertibleTransform");
448 return InputHandler::SCROLL_IGNORED
;
451 if (!non_fast_scrollable_region().IsEmpty()) {
452 bool clipped
= false;
453 gfx::Transform
inverse_screen_space_transform(
454 gfx::Transform::kSkipInitialization
);
455 if (!screen_space_transform().GetInverse(&inverse_screen_space_transform
)) {
456 // TODO(shawnsingh): We shouldn't be applying a projection if screen space
457 // transform is uninvertible here. Perhaps we should be returning
458 // SCROLL_ON_MAIN_THREAD in this case?
461 gfx::PointF hit_test_point_in_content_space
=
462 MathUtil::ProjectPoint(inverse_screen_space_transform
,
465 gfx::PointF hit_test_point_in_layer_space
=
466 gfx::ScalePoint(hit_test_point_in_content_space
,
467 1.f
/ contents_scale_x(),
468 1.f
/ contents_scale_y());
470 non_fast_scrollable_region().Contains(
471 gfx::ToRoundedPoint(hit_test_point_in_layer_space
))) {
473 "LayerImpl::tryScroll: Failed NonFastScrollableRegion");
474 return InputHandler::SCROLL_ON_MAIN_THREAD
;
478 if (have_scroll_event_handlers() &&
479 effective_block_mode
& SCROLL_BLOCKS_ON_SCROLL_EVENT
) {
480 TRACE_EVENT0("cc", "LayerImpl::tryScroll: Failed ScrollEventHandlers");
481 return InputHandler::SCROLL_ON_MAIN_THREAD
;
484 if (type
== InputHandler::WHEEL
&& have_wheel_event_handlers() &&
485 effective_block_mode
& SCROLL_BLOCKS_ON_WHEEL_EVENT
) {
486 TRACE_EVENT0("cc", "LayerImpl::tryScroll: Failed WheelEventHandlers");
487 return InputHandler::SCROLL_ON_MAIN_THREAD
;
491 TRACE_EVENT0("cc", "LayerImpl::tryScroll: Ignored not scrollable");
492 return InputHandler::SCROLL_IGNORED
;
495 gfx::ScrollOffset max_scroll_offset
= MaxScrollOffset();
496 if (max_scroll_offset
.x() <= 0 && max_scroll_offset
.y() <= 0) {
498 "LayerImpl::tryScroll: Ignored. Technically scrollable,"
499 " but has no affordance in either direction.");
500 return InputHandler::SCROLL_IGNORED
;
503 return InputHandler::SCROLL_STARTED
;
506 gfx::Rect
LayerImpl::LayerRectToContentRect(
507 const gfx::RectF
& layer_rect
) const {
508 gfx::RectF content_rect
=
509 gfx::ScaleRect(layer_rect
, contents_scale_x(), contents_scale_y());
510 // Intersect with content rect to avoid the extra pixel because for some
511 // values x and y, ceil((x / y) * y) may be x + 1.
512 content_rect
.Intersect(gfx::Rect(content_bounds()));
513 return gfx::ToEnclosingRect(content_rect
);
516 skia::RefPtr
<SkPicture
> LayerImpl::GetPicture() {
517 return skia::RefPtr
<SkPicture
>();
520 scoped_ptr
<LayerImpl
> LayerImpl::CreateLayerImpl(LayerTreeImpl
* tree_impl
) {
521 return LayerImpl::Create(tree_impl
, layer_id_
, scroll_offset_
);
524 void LayerImpl::PushPropertiesTo(LayerImpl
* layer
) {
525 layer
->SetTransformOrigin(transform_origin_
);
526 layer
->SetBackgroundColor(background_color_
);
527 layer
->SetBounds(bounds_
);
528 layer
->SetContentBounds(content_bounds());
529 layer
->SetContentsScale(contents_scale_x(), contents_scale_y());
530 layer
->SetDoubleSided(double_sided_
);
531 layer
->SetDrawCheckerboardForMissingTiles(
532 draw_checkerboard_for_missing_tiles_
);
533 layer
->SetDrawsContent(DrawsContent());
534 layer
->SetHideLayerAndSubtree(hide_layer_and_subtree_
);
535 layer
->SetHasRenderSurface(!!render_surface() || layer
->HasCopyRequest());
536 layer
->SetFilters(filters());
537 layer
->SetBackgroundFilters(background_filters());
538 layer
->SetMasksToBounds(masks_to_bounds_
);
539 layer
->SetShouldScrollOnMainThread(should_scroll_on_main_thread_
);
540 layer
->SetHaveWheelEventHandlers(have_wheel_event_handlers_
);
541 layer
->SetHaveScrollEventHandlers(have_scroll_event_handlers_
);
542 layer
->SetScrollBlocksOn(scroll_blocks_on_
);
543 layer
->SetNonFastScrollableRegion(non_fast_scrollable_region_
);
544 layer
->SetTouchEventHandlerRegion(touch_event_handler_region_
);
545 layer
->SetContentsOpaque(contents_opaque_
);
546 layer
->SetOpacity(opacity_
);
547 layer
->SetBlendMode(blend_mode_
);
548 layer
->SetIsRootForIsolatedGroup(is_root_for_isolated_group_
);
549 layer
->SetPosition(position_
);
550 layer
->SetIsContainerForFixedPositionLayers(
551 is_container_for_fixed_position_layers_
);
552 layer
->SetPositionConstraint(position_constraint_
);
553 layer
->SetShouldFlattenTransform(should_flatten_transform_
);
554 layer
->set_should_flatten_transform_from_property_tree(
555 should_flatten_transform_from_property_tree_
);
556 layer
->SetUseParentBackfaceVisibility(use_parent_backface_visibility_
);
557 layer
->SetTransformAndInvertibility(transform_
, transform_is_invertible_
);
559 layer
->SetScrollClipLayer(scroll_clip_layer_
? scroll_clip_layer_
->id()
560 : Layer::INVALID_ID
);
561 layer
->set_user_scrollable_horizontal(user_scrollable_horizontal_
);
562 layer
->set_user_scrollable_vertical(user_scrollable_vertical_
);
564 layer
->SetScrollCompensationAdjustment(scroll_compensation_adjustment_
);
566 layer
->PushScrollOffset(nullptr);
568 layer
->Set3dSortingContextId(sorting_context_id_
);
569 layer
->SetNumDescendantsThatDrawContent(num_descendants_that_draw_content_
);
571 layer
->SetTransformTreeIndex(transform_tree_index_
);
572 layer
->SetClipTreeIndex(clip_tree_index_
);
573 layer
->SetOpacityTreeIndex(opacity_tree_index_
);
574 layer
->set_offset_to_transform_parent(offset_to_transform_parent_
);
576 LayerImpl
* scroll_parent
= nullptr;
577 if (scroll_parent_
) {
578 scroll_parent
= layer
->layer_tree_impl()->LayerById(scroll_parent_
->id());
579 DCHECK(scroll_parent
);
582 layer
->SetScrollParent(scroll_parent
);
583 if (scroll_children_
) {
584 std::set
<LayerImpl
*>* scroll_children
= new std::set
<LayerImpl
*>;
585 for (std::set
<LayerImpl
*>::iterator it
= scroll_children_
->begin();
586 it
!= scroll_children_
->end();
588 DCHECK_EQ((*it
)->scroll_parent(), this);
589 LayerImpl
* scroll_child
=
590 layer
->layer_tree_impl()->LayerById((*it
)->id());
591 DCHECK(scroll_child
);
592 scroll_children
->insert(scroll_child
);
594 layer
->SetScrollChildren(scroll_children
);
596 layer
->SetScrollChildren(nullptr);
599 LayerImpl
* clip_parent
= nullptr;
601 clip_parent
= layer
->layer_tree_impl()->LayerById(
606 layer
->SetClipParent(clip_parent
);
607 if (clip_children_
) {
608 std::set
<LayerImpl
*>* clip_children
= new std::set
<LayerImpl
*>;
609 for (std::set
<LayerImpl
*>::iterator it
= clip_children_
->begin();
610 it
!= clip_children_
->end(); ++it
)
611 clip_children
->insert(layer
->layer_tree_impl()->LayerById((*it
)->id()));
612 layer
->SetClipChildren(clip_children
);
614 layer
->SetClipChildren(nullptr);
617 layer
->PassCopyRequests(©_requests_
);
619 // If the main thread commits multiple times before the impl thread actually
620 // draws, then damage tracking will become incorrect if we simply clobber the
621 // update_rect here. The LayerImpl's update_rect needs to accumulate (i.e.
622 // union) any update changes that have occurred on the main thread.
623 update_rect_
.Union(layer
->update_rect());
624 layer
->SetUpdateRect(update_rect_
);
626 layer
->SetStackingOrderChanged(stacking_order_changed_
);
627 layer
->SetDebugInfo(debug_info_
);
629 if (frame_timing_requests_dirty_
) {
630 layer
->SetFrameTimingRequests(frame_timing_requests_
);
631 frame_timing_requests_dirty_
= false;
634 // Reset any state that should be cleared for the next update.
635 stacking_order_changed_
= false;
636 update_rect_
= gfx::Rect();
637 needs_push_properties_
= false;
638 num_dependents_need_push_properties_
= 0;
641 gfx::Vector2dF
LayerImpl::FixedContainerSizeDelta() const {
642 if (!scroll_clip_layer_
)
643 return gfx::Vector2dF();
645 gfx::Vector2dF delta_from_scroll
= scroll_clip_layer_
->bounds_delta();
647 // In virtual-viewport mode, we don't need to compensate for pinch zoom or
648 // scale since the fixed container is the outer viewport, which sits below
650 return delta_from_scroll
;
653 base::DictionaryValue
* LayerImpl::LayerTreeAsJson() const {
654 base::DictionaryValue
* result
= new base::DictionaryValue
;
655 result
->SetInteger("LayerId", id());
656 result
->SetString("LayerType", LayerTypeAsString());
658 base::ListValue
* list
= new base::ListValue
;
659 list
->AppendInteger(bounds().width());
660 list
->AppendInteger(bounds().height());
661 result
->Set("Bounds", list
);
663 list
= new base::ListValue
;
664 list
->AppendDouble(position_
.x());
665 list
->AppendDouble(position_
.y());
666 result
->Set("Position", list
);
668 const gfx::Transform
& gfx_transform
= draw_properties_
.target_space_transform
;
669 double transform
[16];
670 gfx_transform
.matrix().asColMajord(transform
);
671 list
= new base::ListValue
;
672 for (int i
= 0; i
< 16; ++i
)
673 list
->AppendDouble(transform
[i
]);
674 result
->Set("DrawTransform", list
);
676 result
->SetBoolean("DrawsContent", draws_content_
);
677 result
->SetBoolean("Is3dSorted", Is3dSorted());
678 result
->SetDouble("OPACITY", opacity());
679 result
->SetBoolean("ContentsOpaque", contents_opaque_
);
682 result
->SetBoolean("Scrollable", true);
684 if (have_wheel_event_handlers_
)
685 result
->SetBoolean("WheelHandler", have_wheel_event_handlers_
);
686 if (have_scroll_event_handlers_
)
687 result
->SetBoolean("ScrollHandler", have_scroll_event_handlers_
);
688 if (!touch_event_handler_region_
.IsEmpty()) {
689 scoped_ptr
<base::Value
> region
= touch_event_handler_region_
.AsValue();
690 result
->Set("TouchRegion", region
.release());
693 if (scroll_blocks_on_
) {
694 list
= new base::ListValue
;
695 if (scroll_blocks_on_
& SCROLL_BLOCKS_ON_START_TOUCH
)
696 list
->AppendString("StartTouch");
697 if (scroll_blocks_on_
& SCROLL_BLOCKS_ON_WHEEL_EVENT
)
698 list
->AppendString("WheelEvent");
699 if (scroll_blocks_on_
& SCROLL_BLOCKS_ON_SCROLL_EVENT
)
700 list
->AppendString("ScrollEvent");
701 result
->Set("ScrollBlocksOn", list
);
704 list
= new base::ListValue
;
705 for (size_t i
= 0; i
< children_
.size(); ++i
)
706 list
->Append(children_
[i
]->LayerTreeAsJson());
707 result
->Set("Children", list
);
712 void LayerImpl::SetStackingOrderChanged(bool stacking_order_changed
) {
713 if (stacking_order_changed
) {
714 stacking_order_changed_
= true;
715 NoteLayerPropertyChangedForSubtree();
719 void LayerImpl::NoteLayerPropertyChanged() {
720 layer_property_changed_
= true;
721 layer_tree_impl()->set_needs_update_draw_properties();
722 SetNeedsPushProperties();
725 void LayerImpl::NoteLayerPropertyChangedForSubtree() {
726 layer_property_changed_
= true;
727 layer_tree_impl()->set_needs_update_draw_properties();
728 for (size_t i
= 0; i
< children_
.size(); ++i
)
729 children_
[i
]->NoteLayerPropertyChangedForDescendantsInternal();
730 SetNeedsPushProperties();
733 void LayerImpl::NoteLayerPropertyChangedForDescendantsInternal() {
734 layer_property_changed_
= true;
735 for (size_t i
= 0; i
< children_
.size(); ++i
)
736 children_
[i
]->NoteLayerPropertyChangedForDescendantsInternal();
739 void LayerImpl::NoteLayerPropertyChangedForDescendants() {
740 layer_tree_impl()->set_needs_update_draw_properties();
741 for (size_t i
= 0; i
< children_
.size(); ++i
)
742 children_
[i
]->NoteLayerPropertyChangedForDescendantsInternal();
743 SetNeedsPushProperties();
747 // Verify that the resource id is valid.
748 static ResourceId
ValidateResource(const ResourceProvider
* provider
,
750 provider
->ValidateResource(id
);
755 void LayerImpl::ValidateQuadResourcesInternal(DrawQuad
* quad
) const {
757 quad
->IterateResources(
758 base::Bind(&ValidateResource
, layer_tree_impl_
->resource_provider()));
762 const char* LayerImpl::LayerTypeAsString() const {
763 return "cc::LayerImpl";
766 void LayerImpl::ResetAllChangeTrackingForSubtree() {
767 layer_property_changed_
= false;
769 update_rect_
= gfx::Rect();
770 damage_rect_
= gfx::RectF();
773 render_surface_
->ResetPropertyChangedFlag();
776 mask_layer_
->ResetAllChangeTrackingForSubtree();
778 if (replica_layer_
) {
779 // This also resets the replica mask, if it exists.
780 replica_layer_
->ResetAllChangeTrackingForSubtree();
783 for (size_t i
= 0; i
< children_
.size(); ++i
)
784 children_
[i
]->ResetAllChangeTrackingForSubtree();
786 needs_push_properties_
= false;
787 num_dependents_need_push_properties_
= 0;
790 void LayerImpl::UpdatePropertyTreeTransform() {
791 if (transform_tree_index_
!= -1) {
792 TransformTree
& transform_tree
=
793 layer_tree_impl()->property_trees()->transform_tree
;
794 TransformNode
* node
= transform_tree
.Node(transform_tree_index_
);
795 if (node
->data
.local
!= transform_
) {
796 node
->data
.local
= transform_
;
797 node
->data
.needs_local_transform_update
= true;
798 transform_tree
.set_needs_update(true);
799 // TODO(ajuma): The current criteria for creating clip nodes means that
800 // property trees may need to be rebuilt when the new transform isn't
801 // axis-aligned wrt the old transform (see Layer::SetTransform). Since
802 // rebuilding property trees every frame of a transform animation is
803 // something we should try to avoid, change property tree-building so that
804 // it doesn't depend on axis aliginment.
809 void LayerImpl::UpdatePropertyTreeOpacity() {
810 if (opacity_tree_index_
!= -1) {
811 OpacityTree
& opacity_tree
=
812 layer_tree_impl()->property_trees()->opacity_tree
;
813 OpacityNode
* node
= opacity_tree
.Node(opacity_tree_index_
);
814 node
->data
= opacity_
;
818 gfx::ScrollOffset
LayerImpl::ScrollOffsetForAnimation() const {
819 return CurrentScrollOffset();
822 void LayerImpl::OnFilterAnimated(const FilterOperations
& filters
) {
826 void LayerImpl::OnOpacityAnimated(float opacity
) {
828 UpdatePropertyTreeOpacity();
831 void LayerImpl::OnTransformAnimated(const gfx::Transform
& transform
) {
832 SetTransform(transform
);
833 UpdatePropertyTreeTransform();
836 void LayerImpl::OnScrollOffsetAnimated(const gfx::ScrollOffset
& scroll_offset
) {
837 // Only layers in the active tree should need to do anything here, since
838 // layers in the pending tree will find out about these changes as a
839 // result of the shared SyncedProperty.
843 SetCurrentScrollOffset(scroll_offset
);
845 layer_tree_impl_
->DidAnimateScrollOffset();
848 void LayerImpl::OnAnimationWaitingForDeletion() {}
850 bool LayerImpl::IsActive() const {
851 return layer_tree_impl_
->IsActiveTree();
854 gfx::Size
LayerImpl::bounds() const {
855 gfx::Vector2d delta
= gfx::ToCeiledVector2d(bounds_delta_
);
856 return gfx::Size(bounds_
.width() + delta
.x(),
857 bounds_
.height() + delta
.y());
860 gfx::SizeF
LayerImpl::BoundsForScrolling() const {
861 return gfx::SizeF(bounds_
.width() + bounds_delta_
.x(),
862 bounds_
.height() + bounds_delta_
.y());
865 void LayerImpl::SetBounds(const gfx::Size
& bounds
) {
866 if (bounds_
== bounds
)
871 ScrollbarParametersDidChange(true);
872 if (masks_to_bounds())
873 NoteLayerPropertyChangedForSubtree();
875 NoteLayerPropertyChanged();
878 void LayerImpl::SetBoundsDelta(const gfx::Vector2dF
& bounds_delta
) {
879 if (bounds_delta_
== bounds_delta
)
882 bounds_delta_
= bounds_delta
;
884 ScrollbarParametersDidChange(true);
885 if (masks_to_bounds())
886 NoteLayerPropertyChangedForSubtree();
888 NoteLayerPropertyChanged();
891 void LayerImpl::SetMaskLayer(scoped_ptr
<LayerImpl
> mask_layer
) {
892 int new_layer_id
= mask_layer
? mask_layer
->id() : -1;
895 DCHECK_EQ(layer_tree_impl(), mask_layer
->layer_tree_impl());
896 DCHECK_NE(new_layer_id
, mask_layer_id_
);
897 } else if (new_layer_id
== mask_layer_id_
) {
901 mask_layer_
= mask_layer
.Pass();
902 mask_layer_id_
= new_layer_id
;
904 mask_layer_
->SetParent(this);
905 NoteLayerPropertyChangedForSubtree();
908 scoped_ptr
<LayerImpl
> LayerImpl::TakeMaskLayer() {
910 return mask_layer_
.Pass();
913 void LayerImpl::SetReplicaLayer(scoped_ptr
<LayerImpl
> replica_layer
) {
914 int new_layer_id
= replica_layer
? replica_layer
->id() : -1;
917 DCHECK_EQ(layer_tree_impl(), replica_layer
->layer_tree_impl());
918 DCHECK_NE(new_layer_id
, replica_layer_id_
);
919 } else if (new_layer_id
== replica_layer_id_
) {
923 replica_layer_
= replica_layer
.Pass();
924 replica_layer_id_
= new_layer_id
;
926 replica_layer_
->SetParent(this);
927 NoteLayerPropertyChangedForSubtree();
930 scoped_ptr
<LayerImpl
> LayerImpl::TakeReplicaLayer() {
931 replica_layer_id_
= -1;
932 return replica_layer_
.Pass();
935 ScrollbarLayerImplBase
* LayerImpl::ToScrollbarLayer() {
939 void LayerImpl::SetDrawsContent(bool draws_content
) {
940 if (draws_content_
== draws_content
)
943 draws_content_
= draws_content
;
944 NoteLayerPropertyChanged();
947 void LayerImpl::SetHideLayerAndSubtree(bool hide
) {
948 if (hide_layer_and_subtree_
== hide
)
951 hide_layer_and_subtree_
= hide
;
952 NoteLayerPropertyChangedForSubtree();
955 void LayerImpl::SetTransformOrigin(const gfx::Point3F
& transform_origin
) {
956 if (transform_origin_
== transform_origin
)
958 transform_origin_
= transform_origin
;
959 NoteLayerPropertyChangedForSubtree();
962 void LayerImpl::SetBackgroundColor(SkColor background_color
) {
963 if (background_color_
== background_color
)
966 background_color_
= background_color
;
967 NoteLayerPropertyChanged();
970 SkColor
LayerImpl::SafeOpaqueBackgroundColor() const {
971 SkColor color
= background_color();
972 if (SkColorGetA(color
) == 255 && !contents_opaque()) {
973 color
= SK_ColorTRANSPARENT
;
974 } else if (SkColorGetA(color
) != 255 && contents_opaque()) {
975 for (const LayerImpl
* layer
= parent(); layer
;
976 layer
= layer
->parent()) {
977 color
= layer
->background_color();
978 if (SkColorGetA(color
) == 255)
981 if (SkColorGetA(color
) != 255)
982 color
= layer_tree_impl()->background_color();
983 if (SkColorGetA(color
) != 255)
984 color
= SkColorSetA(color
, 255);
989 void LayerImpl::SetFilters(const FilterOperations
& filters
) {
990 if (filters_
== filters
)
994 NoteLayerPropertyChangedForSubtree();
997 bool LayerImpl::FilterIsAnimating() const {
998 return layer_animation_controller_
->IsAnimatingProperty(Animation::FILTER
);
1001 bool LayerImpl::FilterIsAnimatingOnImplOnly() const {
1002 Animation
* filter_animation
=
1003 layer_animation_controller_
->GetAnimation(Animation::FILTER
);
1004 return filter_animation
&& filter_animation
->is_impl_only();
1007 void LayerImpl::SetBackgroundFilters(
1008 const FilterOperations
& filters
) {
1009 if (background_filters_
== filters
)
1012 background_filters_
= filters
;
1013 NoteLayerPropertyChanged();
1016 void LayerImpl::SetMasksToBounds(bool masks_to_bounds
) {
1017 if (masks_to_bounds_
== masks_to_bounds
)
1020 masks_to_bounds_
= masks_to_bounds
;
1021 NoteLayerPropertyChangedForSubtree();
1024 void LayerImpl::SetContentsOpaque(bool opaque
) {
1025 if (contents_opaque_
== opaque
)
1028 contents_opaque_
= opaque
;
1029 NoteLayerPropertyChangedForSubtree();
1032 void LayerImpl::SetOpacity(float opacity
) {
1033 if (opacity_
== opacity
)
1037 NoteLayerPropertyChangedForSubtree();
1040 bool LayerImpl::OpacityIsAnimating() const {
1041 return layer_animation_controller_
->IsAnimatingProperty(Animation::OPACITY
);
1044 bool LayerImpl::OpacityIsAnimatingOnImplOnly() const {
1045 Animation
* opacity_animation
=
1046 layer_animation_controller_
->GetAnimation(Animation::OPACITY
);
1047 return opacity_animation
&& opacity_animation
->is_impl_only();
1050 void LayerImpl::SetBlendMode(SkXfermode::Mode blend_mode
) {
1051 if (blend_mode_
== blend_mode
)
1054 blend_mode_
= blend_mode
;
1055 NoteLayerPropertyChangedForSubtree();
1058 void LayerImpl::SetIsRootForIsolatedGroup(bool root
) {
1059 if (is_root_for_isolated_group_
== root
)
1062 is_root_for_isolated_group_
= root
;
1063 SetNeedsPushProperties();
1066 void LayerImpl::SetPosition(const gfx::PointF
& position
) {
1067 if (position_
== position
)
1070 position_
= position
;
1071 NoteLayerPropertyChangedForSubtree();
1074 void LayerImpl::SetShouldFlattenTransform(bool flatten
) {
1075 if (should_flatten_transform_
== flatten
)
1078 should_flatten_transform_
= flatten
;
1079 NoteLayerPropertyChangedForSubtree();
1082 void LayerImpl::Set3dSortingContextId(int id
) {
1083 if (id
== sorting_context_id_
)
1085 sorting_context_id_
= id
;
1086 NoteLayerPropertyChangedForSubtree();
1089 void LayerImpl::SetFrameTimingRequests(
1090 const std::vector
<FrameTimingRequest
>& requests
) {
1091 frame_timing_requests_
= requests
;
1092 frame_timing_requests_dirty_
= true;
1093 SetNeedsPushProperties();
1096 void LayerImpl::GatherFrameTimingRequestIds(std::vector
<int64_t>* request_ids
) {
1097 for (const auto& request
: frame_timing_requests_
)
1098 request_ids
->push_back(request
.id());
1101 void LayerImpl::SetTransform(const gfx::Transform
& transform
) {
1102 if (transform_
== transform
)
1105 transform_
= transform
;
1106 transform_is_invertible_
= transform_
.IsInvertible();
1107 NoteLayerPropertyChangedForSubtree();
1110 void LayerImpl::SetTransformAndInvertibility(const gfx::Transform
& transform
,
1111 bool transform_is_invertible
) {
1112 if (transform_
== transform
) {
1113 DCHECK(transform_is_invertible_
== transform_is_invertible
)
1114 << "Can't change invertibility if transform is unchanged";
1117 transform_
= transform
;
1118 transform_is_invertible_
= transform_is_invertible
;
1119 NoteLayerPropertyChangedForSubtree();
1122 bool LayerImpl::TransformIsAnimating() const {
1123 return layer_animation_controller_
->IsAnimatingProperty(Animation::TRANSFORM
);
1126 bool LayerImpl::TransformIsAnimatingOnImplOnly() const {
1127 Animation
* transform_animation
=
1128 layer_animation_controller_
->GetAnimation(Animation::TRANSFORM
);
1129 return transform_animation
&& transform_animation
->is_impl_only();
1132 void LayerImpl::SetUpdateRect(const gfx::Rect
& update_rect
) {
1133 update_rect_
= update_rect
;
1134 SetNeedsPushProperties();
1137 void LayerImpl::AddDamageRect(const gfx::RectF
& damage_rect
) {
1138 damage_rect_
= gfx::UnionRects(damage_rect_
, damage_rect
);
1141 void LayerImpl::SetContentBounds(const gfx::Size
& content_bounds
) {
1142 if (this->content_bounds() == content_bounds
)
1145 draw_properties_
.content_bounds
= content_bounds
;
1146 NoteLayerPropertyChanged();
1149 void LayerImpl::SetContentsScale(float contents_scale_x
,
1150 float contents_scale_y
) {
1151 if (this->contents_scale_x() == contents_scale_x
&&
1152 this->contents_scale_y() == contents_scale_y
)
1155 draw_properties_
.contents_scale_x
= contents_scale_x
;
1156 draw_properties_
.contents_scale_y
= contents_scale_y
;
1157 NoteLayerPropertyChanged();
1160 bool LayerImpl::IsExternalFlingActive() const {
1161 return layer_tree_impl_
->IsExternalFlingActive();
1164 void LayerImpl::SetCurrentScrollOffset(const gfx::ScrollOffset
& scroll_offset
) {
1166 if (scroll_offset_
->SetCurrent(scroll_offset
))
1167 DidUpdateScrollOffset(false);
1170 void LayerImpl::SetCurrentScrollOffsetFromDelegate(
1171 const gfx::ScrollOffset
& scroll_offset
) {
1173 if (scroll_offset_
->SetCurrent(scroll_offset
))
1174 DidUpdateScrollOffset(true);
1177 void LayerImpl::PushScrollOffsetFromMainThread(
1178 const gfx::ScrollOffset
& scroll_offset
) {
1179 PushScrollOffset(&scroll_offset
);
1182 void LayerImpl::PushScrollOffsetFromMainThreadAndClobberActiveValue(
1183 const gfx::ScrollOffset
& scroll_offset
) {
1184 scroll_offset_
->set_clobber_active_value();
1185 PushScrollOffset(&scroll_offset
);
1188 gfx::ScrollOffset
LayerImpl::PullDeltaForMainThread() {
1189 // TODO(miletus): Remove all this temporary flooring machinery when
1190 // Blink fully supports fractional scrolls.
1191 gfx::ScrollOffset current_offset
= CurrentScrollOffset();
1192 gfx::Vector2dF current_delta
= ScrollDelta();
1193 gfx::Vector2dF
floored_delta(floor(current_delta
.x()),
1194 floor(current_delta
.y()));
1195 gfx::Vector2dF diff_delta
= floored_delta
- current_delta
;
1196 gfx::ScrollOffset tmp_offset
= ScrollOffsetWithDelta(current_offset
,
1198 scroll_offset_
->SetCurrent(tmp_offset
);
1199 gfx::ScrollOffset delta
= scroll_offset_
->PullDeltaForMainThread();
1200 scroll_offset_
->SetCurrent(current_offset
);
1204 gfx::ScrollOffset
LayerImpl::CurrentScrollOffset() const {
1205 return scroll_offset_
->Current(IsActive());
1208 gfx::Vector2dF
LayerImpl::ScrollDelta() const {
1210 return gfx::Vector2dF(scroll_offset_
->Delta().x(),
1211 scroll_offset_
->Delta().y());
1213 return gfx::Vector2dF(scroll_offset_
->PendingDelta().get().x(),
1214 scroll_offset_
->PendingDelta().get().y());
1217 void LayerImpl::SetScrollDelta(const gfx::Vector2dF
& delta
) {
1219 SetCurrentScrollOffset(scroll_offset_
->ActiveBase() +
1220 gfx::ScrollOffset(delta
));
1223 gfx::ScrollOffset
LayerImpl::BaseScrollOffset() const {
1225 return scroll_offset_
->ActiveBase();
1227 return scroll_offset_
->PendingBase();
1230 void LayerImpl::PushScrollOffset(const gfx::ScrollOffset
* scroll_offset
) {
1231 DCHECK(scroll_offset
|| IsActive());
1232 bool changed
= false;
1233 if (scroll_offset
) {
1234 DCHECK(!IsActive() || !layer_tree_impl_
->FindPendingTreeLayerById(id()));
1235 changed
|= scroll_offset_
->PushFromMainThread(*scroll_offset
);
1238 changed
|= scroll_offset_
->PushPendingToActive();
1242 DidUpdateScrollOffset(false);
1245 void LayerImpl::DidUpdateScrollOffset(bool is_from_root_delegate
) {
1246 DCHECK(scroll_offset_
);
1248 if (!is_from_root_delegate
)
1249 layer_tree_impl()->DidUpdateScrollOffset(id());
1250 NoteLayerPropertyChangedForSubtree();
1251 ScrollbarParametersDidChange(false);
1253 // TODO(enne): in the future, scrolling should update the scroll tree
1254 // directly instead of going through layers.
1255 if (transform_tree_index_
!= -1) {
1256 TransformTree
& transform_tree
=
1257 layer_tree_impl()->property_trees()->transform_tree
;
1258 TransformNode
* node
= transform_tree
.Node(transform_tree_index_
);
1259 node
->data
.scroll_offset
= scroll_offset_
->Current(IsActive());
1260 node
->data
.needs_local_transform_update
= true;
1261 transform_tree
.set_needs_update(true);
1264 // Inform the pending twin that a property changed.
1265 if (layer_tree_impl()->IsActiveTree()) {
1266 LayerImpl
* pending_twin
= layer_tree_impl()->FindPendingTreeLayerById(id());
1268 pending_twin
->DidUpdateScrollOffset(is_from_root_delegate
);
1272 void LayerImpl::SetDoubleSided(bool double_sided
) {
1273 if (double_sided_
== double_sided
)
1276 double_sided_
= double_sided
;
1277 NoteLayerPropertyChangedForSubtree();
1280 SimpleEnclosedRegion
LayerImpl::VisibleContentOpaqueRegion() const {
1281 if (contents_opaque())
1282 return SimpleEnclosedRegion(visible_content_rect());
1283 return SimpleEnclosedRegion();
1286 void LayerImpl::DidBeginTracing() {}
1288 void LayerImpl::ReleaseResources() {}
1290 void LayerImpl::RecreateResources() {
1293 gfx::ScrollOffset
LayerImpl::MaxScrollOffset() const {
1294 if (!scroll_clip_layer_
|| bounds().IsEmpty())
1295 return gfx::ScrollOffset();
1297 LayerImpl
const* page_scale_layer
= layer_tree_impl()->page_scale_layer();
1298 DCHECK(this != page_scale_layer
);
1299 DCHECK(this != layer_tree_impl()->InnerViewportScrollLayer() ||
1300 IsContainerForFixedPositionLayers());
1302 float scale_factor
= 1.f
;
1303 for (LayerImpl
const* current_layer
= this;
1304 current_layer
!= scroll_clip_layer_
->parent();
1305 current_layer
= current_layer
->parent()) {
1306 if (current_layer
== page_scale_layer
)
1307 scale_factor
= layer_tree_impl()->current_page_scale_factor();
1310 gfx::SizeF scaled_scroll_bounds
=
1311 gfx::ToFlooredSize(gfx::ScaleSize(BoundsForScrolling(), scale_factor
));
1312 scaled_scroll_bounds
= gfx::ToFlooredSize(scaled_scroll_bounds
);
1314 gfx::ScrollOffset
max_offset(
1315 scaled_scroll_bounds
.width() - scroll_clip_layer_
->bounds().width(),
1316 scaled_scroll_bounds
.height() - scroll_clip_layer_
->bounds().height());
1317 // We need the final scroll offset to be in CSS coords.
1318 max_offset
.Scale(1 / scale_factor
);
1319 max_offset
.SetToMax(gfx::ScrollOffset());
1323 gfx::ScrollOffset
LayerImpl::ClampScrollOffsetToLimits(
1324 gfx::ScrollOffset offset
) const {
1325 offset
.SetToMin(MaxScrollOffset());
1326 offset
.SetToMax(gfx::ScrollOffset());
1330 gfx::Vector2dF
LayerImpl::ClampScrollToMaxScrollOffset() {
1331 gfx::ScrollOffset old_offset
= CurrentScrollOffset();
1332 gfx::ScrollOffset clamped_offset
= ClampScrollOffsetToLimits(old_offset
);
1333 gfx::Vector2dF delta
= clamped_offset
.DeltaFrom(old_offset
);
1334 if (!delta
.IsZero())
1339 void LayerImpl::SetScrollbarPosition(ScrollbarLayerImplBase
* scrollbar_layer
,
1340 LayerImpl
* scrollbar_clip_layer
,
1341 bool on_resize
) const {
1342 DCHECK(scrollbar_layer
);
1343 LayerImpl
* page_scale_layer
= layer_tree_impl()->page_scale_layer();
1345 DCHECK(this != page_scale_layer
);
1346 DCHECK(scrollbar_clip_layer
);
1347 gfx::RectF
clip_rect(gfx::PointF(),
1348 scrollbar_clip_layer
->BoundsForScrolling());
1350 // See comment in MaxScrollOffset() regarding the use of the content layer
1352 gfx::RectF
scroll_rect(gfx::PointF(), BoundsForScrolling());
1354 if (scroll_rect
.size().IsEmpty())
1357 gfx::ScrollOffset current_offset
;
1358 for (LayerImpl
const* current_layer
= this;
1359 current_layer
!= scrollbar_clip_layer
->parent();
1360 current_layer
= current_layer
->parent()) {
1361 current_offset
+= current_layer
->CurrentScrollOffset();
1362 if (current_layer
== page_scale_layer
) {
1363 float scale_factor
= layer_tree_impl()->current_page_scale_factor();
1364 current_offset
.Scale(scale_factor
);
1365 scroll_rect
.Scale(scale_factor
);
1369 bool scrollbar_needs_animation
= false;
1370 scrollbar_needs_animation
|= scrollbar_layer
->SetVerticalAdjust(
1371 scrollbar_clip_layer
->bounds_delta().y());
1372 if (scrollbar_layer
->orientation() == HORIZONTAL
) {
1373 float visible_ratio
= clip_rect
.width() / scroll_rect
.width();
1374 scrollbar_needs_animation
|=
1375 scrollbar_layer
->SetCurrentPos(current_offset
.x());
1376 scrollbar_needs_animation
|=
1377 scrollbar_layer
->SetMaximum(scroll_rect
.width() - clip_rect
.width());
1378 scrollbar_needs_animation
|=
1379 scrollbar_layer
->SetVisibleToTotalLengthRatio(visible_ratio
);
1381 float visible_ratio
= clip_rect
.height() / scroll_rect
.height();
1382 bool y_offset_did_change
=
1383 scrollbar_layer
->SetCurrentPos(current_offset
.y());
1384 scrollbar_needs_animation
|= y_offset_did_change
;
1385 scrollbar_needs_animation
|=
1386 scrollbar_layer
->SetMaximum(scroll_rect
.height() - clip_rect
.height());
1387 scrollbar_needs_animation
|=
1388 scrollbar_layer
->SetVisibleToTotalLengthRatio(visible_ratio
);
1389 if (y_offset_did_change
&& layer_tree_impl()->IsActiveTree() &&
1390 this == layer_tree_impl()->InnerViewportScrollLayer()) {
1391 TRACE_COUNTER_ID1("cc", "scroll_offset_y", this->id(),
1392 current_offset
.y());
1395 if (scrollbar_needs_animation
) {
1396 layer_tree_impl()->set_needs_update_draw_properties();
1397 // TODO(wjmaclean) The scrollbar animator for the pinch-zoom scrollbars
1398 // should activate for every scroll on the main frame, not just the
1399 // scrolls that move the pinch virtual viewport (i.e. trigger from
1400 // either inner or outer viewport).
1401 if (scrollbar_animation_controller_
) {
1402 // Non-overlay scrollbars shouldn't trigger animations.
1403 if (scrollbar_layer
->is_overlay_scrollbar())
1404 scrollbar_animation_controller_
->DidScrollUpdate(on_resize
);
1409 void LayerImpl::DidBecomeActive() {
1410 if (layer_tree_impl_
->settings().scrollbar_animator
==
1411 LayerTreeSettings::NO_ANIMATOR
) {
1415 bool need_scrollbar_animation_controller
= scrollable() && scrollbars_
;
1416 if (!need_scrollbar_animation_controller
) {
1417 scrollbar_animation_controller_
= nullptr;
1421 if (scrollbar_animation_controller_
)
1424 scrollbar_animation_controller_
=
1425 layer_tree_impl_
->CreateScrollbarAnimationController(this);
1428 void LayerImpl::ClearScrollbars() {
1432 scrollbars_
.reset(nullptr);
1435 void LayerImpl::AddScrollbar(ScrollbarLayerImplBase
* layer
) {
1437 DCHECK(!scrollbars_
|| scrollbars_
->find(layer
) == scrollbars_
->end());
1439 scrollbars_
.reset(new ScrollbarSet());
1441 scrollbars_
->insert(layer
);
1444 void LayerImpl::RemoveScrollbar(ScrollbarLayerImplBase
* layer
) {
1445 DCHECK(scrollbars_
);
1447 DCHECK(scrollbars_
->find(layer
) != scrollbars_
->end());
1449 scrollbars_
->erase(layer
);
1450 if (scrollbars_
->empty())
1451 scrollbars_
= nullptr;
1454 bool LayerImpl::HasScrollbar(ScrollbarOrientation orientation
) const {
1458 for (ScrollbarSet::iterator it
= scrollbars_
->begin();
1459 it
!= scrollbars_
->end();
1461 if ((*it
)->orientation() == orientation
)
1467 void LayerImpl::ScrollbarParametersDidChange(bool on_resize
) {
1471 for (ScrollbarSet::iterator it
= scrollbars_
->begin();
1472 it
!= scrollbars_
->end();
1474 bool is_scroll_layer
= (*it
)->ScrollLayerId() == layer_id_
;
1475 bool scroll_layer_resized
= is_scroll_layer
&& on_resize
;
1476 (*it
)->ScrollbarParametersDidChange(scroll_layer_resized
);
1480 void LayerImpl::SetNeedsPushProperties() {
1481 if (needs_push_properties_
)
1483 if (!parent_should_know_need_push_properties() && parent_
)
1484 parent_
->AddDependentNeedsPushProperties();
1485 needs_push_properties_
= true;
1488 void LayerImpl::AddDependentNeedsPushProperties() {
1489 DCHECK_GE(num_dependents_need_push_properties_
, 0);
1491 if (!parent_should_know_need_push_properties() && parent_
)
1492 parent_
->AddDependentNeedsPushProperties();
1494 num_dependents_need_push_properties_
++;
1497 void LayerImpl::RemoveDependentNeedsPushProperties() {
1498 num_dependents_need_push_properties_
--;
1499 DCHECK_GE(num_dependents_need_push_properties_
, 0);
1501 if (!parent_should_know_need_push_properties() && parent_
)
1502 parent_
->RemoveDependentNeedsPushProperties();
1505 void LayerImpl::GetAllPrioritizedTilesForTracing(
1506 std::vector
<PrioritizedTile
>* prioritized_tiles
) const {
1509 void LayerImpl::AsValueInto(base::trace_event::TracedValue
* state
) const {
1510 TracedValue::MakeDictIntoImplicitSnapshotWithCategory(
1511 TRACE_DISABLED_BY_DEFAULT("cc.debug"),
1514 LayerTypeAsString(),
1516 state
->SetInteger("layer_id", id());
1517 MathUtil::AddToTracedValue("bounds", bounds_
, state
);
1519 state
->SetDouble("opacity", opacity());
1521 MathUtil::AddToTracedValue("position", position_
, state
);
1523 state
->SetInteger("draws_content", DrawsContent());
1524 state
->SetInteger("gpu_memory_usage", GPUMemoryUsageInBytes());
1526 MathUtil::AddToTracedValue(
1527 "scroll_offset", scroll_offset_
? scroll_offset_
->Current(IsActive())
1528 : gfx::ScrollOffset(),
1531 MathUtil::AddToTracedValue("transform_origin", transform_origin_
, state
);
1534 gfx::QuadF layer_quad
= MathUtil::MapQuad(
1535 screen_space_transform(),
1536 gfx::QuadF(gfx::Rect(content_bounds())),
1538 MathUtil::AddToTracedValue("layer_quad", layer_quad
, state
);
1539 if (!touch_event_handler_region_
.IsEmpty()) {
1540 state
->BeginArray("touch_event_handler_region");
1541 touch_event_handler_region_
.AsValueInto(state
);
1544 if (have_wheel_event_handlers_
) {
1545 gfx::Rect
wheel_rect(content_bounds());
1546 Region
wheel_region(wheel_rect
);
1547 state
->BeginArray("wheel_event_handler_region");
1548 wheel_region
.AsValueInto(state
);
1551 if (have_scroll_event_handlers_
) {
1552 gfx::Rect
scroll_rect(content_bounds());
1553 Region
scroll_region(scroll_rect
);
1554 state
->BeginArray("scroll_event_handler_region");
1555 scroll_region
.AsValueInto(state
);
1558 if (!non_fast_scrollable_region_
.IsEmpty()) {
1559 state
->BeginArray("non_fast_scrollable_region");
1560 non_fast_scrollable_region_
.AsValueInto(state
);
1563 if (scroll_blocks_on_
) {
1564 state
->SetInteger("scroll_blocks_on", scroll_blocks_on_
);
1567 state
->BeginArray("children");
1568 for (size_t i
= 0; i
< children_
.size(); ++i
) {
1569 state
->BeginDictionary();
1570 children_
[i
]->AsValueInto(state
);
1571 state
->EndDictionary();
1575 state
->BeginDictionary("mask_layer");
1576 mask_layer_
->AsValueInto(state
);
1577 state
->EndDictionary();
1579 if (replica_layer_
) {
1580 state
->BeginDictionary("replica_layer");
1581 replica_layer_
->AsValueInto(state
);
1582 state
->EndDictionary();
1586 state
->SetInteger("scroll_parent", scroll_parent_
->id());
1589 state
->SetInteger("clip_parent", clip_parent_
->id());
1591 state
->SetBoolean("can_use_lcd_text", can_use_lcd_text());
1592 state
->SetBoolean("contents_opaque", contents_opaque());
1595 "has_animation_bounds",
1596 layer_animation_controller()->HasAnimationThatInflatesBounds());
1599 if (LayerUtils::GetAnimationBounds(*this, &box
))
1600 MathUtil::AddToTracedValue("animation_bounds", box
, state
);
1602 if (debug_info_
.get()) {
1604 debug_info_
->AppendAsTraceFormat(&str
);
1605 base::JSONReader json_reader
;
1606 scoped_ptr
<base::Value
> debug_info_value(json_reader
.ReadToValue(str
));
1608 if (debug_info_value
->IsType(base::Value::TYPE_DICTIONARY
)) {
1609 base::DictionaryValue
* dictionary_value
= nullptr;
1610 bool converted_to_dictionary
=
1611 debug_info_value
->GetAsDictionary(&dictionary_value
);
1612 DCHECK(converted_to_dictionary
);
1613 for (base::DictionaryValue::Iterator
it(*dictionary_value
); !it
.IsAtEnd();
1615 state
->SetValue(it
.key().data(), it
.value().CreateDeepCopy());
1622 if (!frame_timing_requests_
.empty()) {
1623 state
->BeginArray("frame_timing_requests");
1624 for (const auto& request
: frame_timing_requests_
) {
1625 state
->BeginDictionary();
1626 state
->SetInteger("request_id", request
.id());
1627 MathUtil::AddToTracedValue("request_rect", request
.rect(), state
);
1628 state
->EndDictionary();
1634 bool LayerImpl::IsDrawnRenderSurfaceLayerListMember() const {
1635 return draw_properties_
.last_drawn_render_surface_layer_list_id
==
1636 layer_tree_impl_
->current_render_surface_list_id();
1639 size_t LayerImpl::GPUMemoryUsageInBytes() const { return 0; }
1641 void LayerImpl::RunMicroBenchmark(MicroBenchmarkImpl
* benchmark
) {
1642 benchmark
->RunOnLayer(this);
1645 int LayerImpl::NumDescendantsThatDrawContent() const {
1646 return num_descendants_that_draw_content_
;
1649 void LayerImpl::NotifyAnimationFinished(
1650 base::TimeTicks monotonic_time
,
1651 Animation::TargetProperty target_property
,
1653 if (target_property
== Animation::SCROLL_OFFSET
)
1654 layer_tree_impl_
->InputScrollAnimationFinished();
1657 void LayerImpl::SetHasRenderSurface(bool should_have_render_surface
) {
1658 if (!!render_surface() == should_have_render_surface
)
1661 SetNeedsPushProperties();
1662 layer_tree_impl()->set_needs_update_draw_properties();
1663 if (should_have_render_surface
) {
1664 render_surface_
= make_scoped_ptr(new RenderSurfaceImpl(this));
1667 render_surface_
.reset();
1670 Region
LayerImpl::GetInvalidationRegion() {
1671 return Region(update_rect_
);
1674 gfx::Rect
LayerImpl::GetEnclosingRectInTargetSpace() const {
1675 return MathUtil::MapEnclosingClippedRect(
1676 draw_properties_
.target_space_transform
,
1677 gfx::Rect(draw_properties_
.content_bounds
));
1680 gfx::Rect
LayerImpl::GetScaledEnclosingRectInTargetSpace(float scale
) const {
1681 gfx::Transform scaled_draw_transform
=
1682 draw_properties_
.target_space_transform
;
1683 scaled_draw_transform
.Scale(SK_MScalar1
/ scale
, SK_MScalar1
/ scale
);
1684 gfx::Size scaled_content_bounds
=
1685 gfx::ToCeiledSize(gfx::ScaleSize(content_bounds(), scale
));
1686 return MathUtil::MapEnclosingClippedRect(scaled_draw_transform
,
1687 gfx::Rect(scaled_content_bounds
));