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) {
85 DCHECK_GT(layer_id_
, 0);
86 DCHECK(layer_tree_impl_
);
87 layer_tree_impl_
->RegisterLayer(this);
88 AnimationRegistrar
* registrar
= layer_tree_impl_
->GetAnimationRegistrar();
89 layer_animation_controller_
=
90 registrar
->GetAnimationControllerForId(layer_id_
);
91 layer_animation_controller_
->AddValueObserver(this);
93 layer_animation_controller_
->set_value_provider(this);
94 layer_animation_controller_
->set_layer_animation_delegate(this);
96 SetNeedsPushProperties();
99 LayerImpl::~LayerImpl() {
100 DCHECK_EQ(DRAW_MODE_NONE
, current_draw_mode_
);
102 layer_animation_controller_
->RemoveValueObserver(this);
103 layer_animation_controller_
->remove_value_provider(this);
104 layer_animation_controller_
->remove_layer_animation_delegate(this);
106 if (!copy_requests_
.empty() && layer_tree_impl_
->IsActiveTree())
107 layer_tree_impl()->RemoveLayerWithCopyOutputRequest(this);
108 layer_tree_impl_
->UnregisterLayer(this);
110 TRACE_EVENT_OBJECT_DELETED_WITH_ID(
111 TRACE_DISABLED_BY_DEFAULT("cc.debug"), "cc::LayerImpl", this);
114 void LayerImpl::AddChild(scoped_ptr
<LayerImpl
> child
) {
115 child
->SetParent(this);
116 DCHECK_EQ(layer_tree_impl(), child
->layer_tree_impl());
117 children_
.push_back(child
.Pass());
118 layer_tree_impl()->set_needs_update_draw_properties();
121 scoped_ptr
<LayerImpl
> LayerImpl::RemoveChild(LayerImpl
* child
) {
122 for (OwnedLayerImplList::iterator it
= children_
.begin();
123 it
!= children_
.end();
126 scoped_ptr
<LayerImpl
> ret
= children_
.take(it
);
128 layer_tree_impl()->set_needs_update_draw_properties();
135 void LayerImpl::SetParent(LayerImpl
* parent
) {
136 if (parent_should_know_need_push_properties()) {
138 parent_
->RemoveDependentNeedsPushProperties();
140 parent
->AddDependentNeedsPushProperties();
145 void LayerImpl::ClearChildList() {
146 if (children_
.empty())
150 layer_tree_impl()->set_needs_update_draw_properties();
153 bool LayerImpl::HasAncestor(const LayerImpl
* ancestor
) const {
157 for (const LayerImpl
* layer
= this; layer
; layer
= layer
->parent()) {
158 if (layer
== ancestor
)
165 void LayerImpl::SetScrollParent(LayerImpl
* parent
) {
166 if (scroll_parent_
== parent
)
170 DCHECK_EQ(layer_tree_impl()->LayerById(parent
->id()), parent
);
172 scroll_parent_
= parent
;
173 SetNeedsPushProperties();
176 void LayerImpl::SetDebugInfo(
177 scoped_refptr
<base::trace_event::ConvertableToTraceFormat
> other
) {
179 SetNeedsPushProperties();
182 void LayerImpl::SetScrollChildren(std::set
<LayerImpl
*>* children
) {
183 if (scroll_children_
.get() == children
)
185 scroll_children_
.reset(children
);
186 SetNeedsPushProperties();
189 void LayerImpl::SetNumDescendantsThatDrawContent(int num_descendants
) {
190 if (num_descendants_that_draw_content_
== num_descendants
)
192 num_descendants_that_draw_content_
= num_descendants
;
193 SetNeedsPushProperties();
196 void LayerImpl::SetClipParent(LayerImpl
* ancestor
) {
197 if (clip_parent_
== ancestor
)
200 clip_parent_
= ancestor
;
201 SetNeedsPushProperties();
204 void LayerImpl::SetClipChildren(std::set
<LayerImpl
*>* children
) {
205 if (clip_children_
.get() == children
)
207 clip_children_
.reset(children
);
208 SetNeedsPushProperties();
211 void LayerImpl::PassCopyRequests(ScopedPtrVector
<CopyOutputRequest
>* requests
) {
212 if (requests
->empty())
214 DCHECK(render_surface());
215 bool was_empty
= copy_requests_
.empty();
216 copy_requests_
.insert_and_take(copy_requests_
.end(), requests
);
219 if (was_empty
&& layer_tree_impl()->IsActiveTree())
220 layer_tree_impl()->AddLayerWithCopyOutputRequest(this);
221 NoteLayerPropertyChangedForSubtree();
224 void LayerImpl::TakeCopyRequestsAndTransformToTarget(
225 ScopedPtrVector
<CopyOutputRequest
>* requests
) {
226 DCHECK(!copy_requests_
.empty());
227 DCHECK(layer_tree_impl()->IsActiveTree());
228 DCHECK_EQ(render_target(), this);
230 size_t first_inserted_request
= requests
->size();
231 requests
->insert_and_take(requests
->end(), ©_requests_
);
232 copy_requests_
.clear();
234 for (size_t i
= first_inserted_request
; i
< requests
->size(); ++i
) {
235 CopyOutputRequest
* request
= requests
->at(i
);
236 if (!request
->has_area())
239 gfx::Rect request_in_layer_space
= request
->area();
240 gfx::Rect request_in_content_space
=
241 LayerRectToContentRect(request_in_layer_space
);
242 request
->set_area(MathUtil::MapEnclosingClippedRect(
243 draw_properties_
.target_space_transform
, request_in_content_space
));
246 layer_tree_impl()->RemoveLayerWithCopyOutputRequest(this);
247 layer_tree_impl()->set_needs_update_draw_properties();
250 void LayerImpl::ClearRenderSurfaceLayerList() {
252 render_surface_
->ClearLayerLists();
255 void LayerImpl::PopulateSharedQuadState(SharedQuadState
* state
) const {
257 draw_properties_
.target_space_transform
, draw_properties_
.content_bounds
,
258 draw_properties_
.visible_content_rect
, draw_properties_
.clip_rect
,
259 draw_properties_
.is_clipped
, draw_properties_
.opacity
,
260 draw_properties_
.blend_mode
, sorting_context_id_
);
263 void LayerImpl::PopulateScaledSharedQuadState(SharedQuadState
* state
,
265 gfx::Transform scaled_draw_transform
=
266 draw_properties_
.target_space_transform
;
267 scaled_draw_transform
.Scale(SK_MScalar1
/ scale
, SK_MScalar1
/ scale
);
268 gfx::Size scaled_content_bounds
=
269 gfx::ToCeiledSize(gfx::ScaleSize(bounds(), scale
));
270 gfx::Rect scaled_visible_content_rect
=
271 gfx::ScaleToEnclosingRect(visible_content_rect(), scale
);
272 scaled_visible_content_rect
.Intersect(gfx::Rect(scaled_content_bounds
));
274 state
->SetAll(scaled_draw_transform
, scaled_content_bounds
,
275 scaled_visible_content_rect
, draw_properties().clip_rect
,
276 draw_properties().is_clipped
, draw_properties().opacity
,
277 draw_properties().blend_mode
, sorting_context_id_
);
280 bool LayerImpl::WillDraw(DrawMode draw_mode
,
281 ResourceProvider
* resource_provider
) {
282 // WillDraw/DidDraw must be matched.
283 DCHECK_NE(DRAW_MODE_NONE
, draw_mode
);
284 DCHECK_EQ(DRAW_MODE_NONE
, current_draw_mode_
);
285 current_draw_mode_
= draw_mode
;
289 void LayerImpl::DidDraw(ResourceProvider
* resource_provider
) {
290 DCHECK_NE(DRAW_MODE_NONE
, current_draw_mode_
);
291 current_draw_mode_
= DRAW_MODE_NONE
;
294 bool LayerImpl::ShowDebugBorders() const {
295 return layer_tree_impl()->debug_state().show_debug_borders
;
298 void LayerImpl::GetDebugBorderProperties(SkColor
* color
, float* width
) const {
299 if (draws_content_
) {
300 *color
= DebugColors::ContentLayerBorderColor();
301 *width
= DebugColors::ContentLayerBorderWidth(layer_tree_impl());
305 if (masks_to_bounds_
) {
306 *color
= DebugColors::MaskingLayerBorderColor();
307 *width
= DebugColors::MaskingLayerBorderWidth(layer_tree_impl());
311 *color
= DebugColors::ContainerLayerBorderColor();
312 *width
= DebugColors::ContainerLayerBorderWidth(layer_tree_impl());
315 void LayerImpl::AppendDebugBorderQuad(
316 RenderPass
* render_pass
,
317 const gfx::Size
& content_bounds
,
318 const SharedQuadState
* shared_quad_state
,
319 AppendQuadsData
* append_quads_data
) const {
322 GetDebugBorderProperties(&color
, &width
);
323 AppendDebugBorderQuad(render_pass
,
331 void LayerImpl::AppendDebugBorderQuad(RenderPass
* render_pass
,
332 const gfx::Size
& content_bounds
,
333 const SharedQuadState
* shared_quad_state
,
334 AppendQuadsData
* append_quads_data
,
337 if (!ShowDebugBorders())
340 gfx::Rect
quad_rect(content_bounds
);
341 gfx::Rect
visible_quad_rect(quad_rect
);
342 DebugBorderDrawQuad
* debug_border_quad
=
343 render_pass
->CreateAndAppendDrawQuad
<DebugBorderDrawQuad
>();
344 debug_border_quad
->SetNew(
345 shared_quad_state
, quad_rect
, visible_quad_rect
, color
, width
);
346 if (contents_opaque()) {
347 // When opaque, draw a second inner border that is thicker than the outer
348 // border, but more transparent.
349 static const float kFillOpacity
= 0.3f
;
350 SkColor fill_color
= SkColorSetA(
351 color
, static_cast<uint8_t>(SkColorGetA(color
) * kFillOpacity
));
352 float fill_width
= width
* 3;
353 gfx::Rect fill_rect
= quad_rect
;
354 fill_rect
.Inset(fill_width
/ 2.f
, fill_width
/ 2.f
);
355 if (fill_rect
.IsEmpty())
357 gfx::Rect visible_fill_rect
=
358 gfx::IntersectRects(visible_quad_rect
, fill_rect
);
359 DebugBorderDrawQuad
* fill_quad
=
360 render_pass
->CreateAndAppendDrawQuad
<DebugBorderDrawQuad
>();
361 fill_quad
->SetNew(shared_quad_state
, fill_rect
, visible_fill_rect
,
362 fill_color
, fill_width
);
366 bool LayerImpl::HasDelegatedContent() const {
370 bool LayerImpl::HasContributingDelegatedRenderPasses() const {
374 RenderPassId
LayerImpl::FirstContributingRenderPassId() const {
375 return RenderPassId(0, 0);
378 RenderPassId
LayerImpl::NextContributingRenderPassId(RenderPassId id
) const {
379 return RenderPassId(0, 0);
382 void LayerImpl::GetContentsResourceId(ResourceProvider::ResourceId
* resource_id
,
383 gfx::Size
* resource_size
) const {
388 gfx::Vector2dF
LayerImpl::ScrollBy(const gfx::Vector2dF
& scroll
) {
389 gfx::ScrollOffset
adjusted_scroll(scroll
);
390 if (layer_tree_impl()->settings().use_pinch_virtual_viewport
) {
391 if (!user_scrollable_horizontal_
)
392 adjusted_scroll
.set_x(0);
393 if (!user_scrollable_vertical_
)
394 adjusted_scroll
.set_y(0);
396 DCHECK(scrollable());
397 gfx::ScrollOffset old_offset
= CurrentScrollOffset();
398 gfx::ScrollOffset new_offset
=
399 ClampScrollOffsetToLimits(old_offset
+ adjusted_scroll
);
400 SetCurrentScrollOffset(new_offset
);
402 gfx::ScrollOffset unscrolled
=
403 old_offset
+ gfx::ScrollOffset(scroll
) - new_offset
;
404 return gfx::Vector2dF(unscrolled
.x(), unscrolled
.y());
407 void LayerImpl::SetScrollClipLayer(int scroll_clip_layer_id
) {
408 scroll_clip_layer_
= layer_tree_impl()->LayerById(scroll_clip_layer_id
);
411 bool LayerImpl::user_scrollable(ScrollbarOrientation orientation
) const {
412 return (orientation
== HORIZONTAL
) ? user_scrollable_horizontal_
413 : user_scrollable_vertical_
;
416 void LayerImpl::ApplySentScrollDeltasFromAbortedCommit() {
417 DCHECK(layer_tree_impl()->IsActiveTree());
418 scroll_offset_
->AbortCommit();
421 InputHandler::ScrollStatus
LayerImpl::TryScroll(
422 const gfx::PointF
& screen_space_point
,
423 InputHandler::ScrollInputType type
,
424 ScrollBlocksOn effective_block_mode
) const {
425 if (should_scroll_on_main_thread()) {
426 TRACE_EVENT0("cc", "LayerImpl::TryScroll: Failed ShouldScrollOnMainThread");
427 return InputHandler::SCROLL_ON_MAIN_THREAD
;
430 if (!screen_space_transform().IsInvertible()) {
431 TRACE_EVENT0("cc", "LayerImpl::TryScroll: Ignored NonInvertibleTransform");
432 return InputHandler::SCROLL_IGNORED
;
435 if (!non_fast_scrollable_region().IsEmpty()) {
436 bool clipped
= false;
437 gfx::Transform
inverse_screen_space_transform(
438 gfx::Transform::kSkipInitialization
);
439 if (!screen_space_transform().GetInverse(&inverse_screen_space_transform
)) {
440 // TODO(shawnsingh): We shouldn't be applying a projection if screen space
441 // transform is uninvertible here. Perhaps we should be returning
442 // SCROLL_ON_MAIN_THREAD in this case?
445 gfx::PointF hit_test_point_in_content_space
=
446 MathUtil::ProjectPoint(inverse_screen_space_transform
,
449 gfx::PointF hit_test_point_in_layer_space
=
450 gfx::ScalePoint(hit_test_point_in_content_space
,
451 1.f
/ contents_scale_x(),
452 1.f
/ contents_scale_y());
454 non_fast_scrollable_region().Contains(
455 gfx::ToRoundedPoint(hit_test_point_in_layer_space
))) {
457 "LayerImpl::tryScroll: Failed NonFastScrollableRegion");
458 return InputHandler::SCROLL_ON_MAIN_THREAD
;
462 if (have_scroll_event_handlers() &&
463 effective_block_mode
& SCROLL_BLOCKS_ON_SCROLL_EVENT
) {
464 TRACE_EVENT0("cc", "LayerImpl::tryScroll: Failed ScrollEventHandlers");
465 return InputHandler::SCROLL_ON_MAIN_THREAD
;
468 if (type
== InputHandler::WHEEL
&& have_wheel_event_handlers() &&
469 effective_block_mode
& SCROLL_BLOCKS_ON_WHEEL_EVENT
) {
470 TRACE_EVENT0("cc", "LayerImpl::tryScroll: Failed WheelEventHandlers");
471 return InputHandler::SCROLL_ON_MAIN_THREAD
;
475 TRACE_EVENT0("cc", "LayerImpl::tryScroll: Ignored not scrollable");
476 return InputHandler::SCROLL_IGNORED
;
479 gfx::ScrollOffset max_scroll_offset
= MaxScrollOffset();
480 if (max_scroll_offset
.x() <= 0 && max_scroll_offset
.y() <= 0) {
482 "LayerImpl::tryScroll: Ignored. Technically scrollable,"
483 " but has no affordance in either direction.");
484 return InputHandler::SCROLL_IGNORED
;
487 return InputHandler::SCROLL_STARTED
;
490 gfx::Rect
LayerImpl::LayerRectToContentRect(
491 const gfx::RectF
& layer_rect
) const {
492 gfx::RectF content_rect
=
493 gfx::ScaleRect(layer_rect
, contents_scale_x(), contents_scale_y());
494 // Intersect with content rect to avoid the extra pixel because for some
495 // values x and y, ceil((x / y) * y) may be x + 1.
496 content_rect
.Intersect(gfx::Rect(content_bounds()));
497 return gfx::ToEnclosingRect(content_rect
);
500 skia::RefPtr
<SkPicture
> LayerImpl::GetPicture() {
501 return skia::RefPtr
<SkPicture
>();
504 scoped_ptr
<LayerImpl
> LayerImpl::CreateLayerImpl(LayerTreeImpl
* tree_impl
) {
505 return LayerImpl::Create(tree_impl
, layer_id_
, scroll_offset_
);
508 void LayerImpl::PushPropertiesTo(LayerImpl
* layer
) {
509 layer
->SetTransformOrigin(transform_origin_
);
510 layer
->SetBackgroundColor(background_color_
);
511 layer
->SetBounds(bounds_
);
512 layer
->SetContentBounds(content_bounds());
513 layer
->SetContentsScale(contents_scale_x(), contents_scale_y());
514 layer
->SetDoubleSided(double_sided_
);
515 layer
->SetDrawCheckerboardForMissingTiles(
516 draw_checkerboard_for_missing_tiles_
);
517 layer
->SetDrawsContent(DrawsContent());
518 layer
->SetHideLayerAndSubtree(hide_layer_and_subtree_
);
519 layer
->SetHasRenderSurface(!!render_surface() || layer
->HasCopyRequest());
520 layer
->SetFilters(filters());
521 layer
->SetBackgroundFilters(background_filters());
522 layer
->SetMasksToBounds(masks_to_bounds_
);
523 layer
->SetShouldScrollOnMainThread(should_scroll_on_main_thread_
);
524 layer
->SetHaveWheelEventHandlers(have_wheel_event_handlers_
);
525 layer
->SetHaveScrollEventHandlers(have_scroll_event_handlers_
);
526 layer
->SetScrollBlocksOn(scroll_blocks_on_
);
527 layer
->SetNonFastScrollableRegion(non_fast_scrollable_region_
);
528 layer
->SetTouchEventHandlerRegion(touch_event_handler_region_
);
529 layer
->SetContentsOpaque(contents_opaque_
);
530 layer
->SetOpacity(opacity_
);
531 layer
->SetBlendMode(blend_mode_
);
532 layer
->SetIsRootForIsolatedGroup(is_root_for_isolated_group_
);
533 layer
->SetPosition(position_
);
534 layer
->SetIsContainerForFixedPositionLayers(
535 is_container_for_fixed_position_layers_
);
536 layer
->SetPositionConstraint(position_constraint_
);
537 layer
->SetShouldFlattenTransform(should_flatten_transform_
);
538 layer
->set_should_flatten_transform_from_property_tree(
539 should_flatten_transform_from_property_tree_
);
540 layer
->SetUseParentBackfaceVisibility(use_parent_backface_visibility_
);
541 layer
->SetTransformAndInvertibility(transform_
, transform_is_invertible_
);
543 layer
->SetScrollClipLayer(scroll_clip_layer_
? scroll_clip_layer_
->id()
544 : Layer::INVALID_ID
);
545 layer
->set_user_scrollable_horizontal(user_scrollable_horizontal_
);
546 layer
->set_user_scrollable_vertical(user_scrollable_vertical_
);
548 layer
->SetScrollCompensationAdjustment(scroll_compensation_adjustment_
);
550 layer
->PushScrollOffset(nullptr);
552 layer
->Set3dSortingContextId(sorting_context_id_
);
553 layer
->SetNumDescendantsThatDrawContent(num_descendants_that_draw_content_
);
555 layer
->set_transform_tree_index(transform_tree_index_
);
556 layer
->set_opacity_tree_index(opacity_tree_index_
);
557 layer
->set_clip_tree_index(clip_tree_index_
);
558 layer
->set_offset_to_transform_parent(offset_to_transform_parent_
);
560 LayerImpl
* scroll_parent
= nullptr;
561 if (scroll_parent_
) {
562 scroll_parent
= layer
->layer_tree_impl()->LayerById(scroll_parent_
->id());
563 DCHECK(scroll_parent
);
566 layer
->SetScrollParent(scroll_parent
);
567 if (scroll_children_
) {
568 std::set
<LayerImpl
*>* scroll_children
= new std::set
<LayerImpl
*>;
569 for (std::set
<LayerImpl
*>::iterator it
= scroll_children_
->begin();
570 it
!= scroll_children_
->end();
572 DCHECK_EQ((*it
)->scroll_parent(), this);
573 LayerImpl
* scroll_child
=
574 layer
->layer_tree_impl()->LayerById((*it
)->id());
575 DCHECK(scroll_child
);
576 scroll_children
->insert(scroll_child
);
578 layer
->SetScrollChildren(scroll_children
);
580 layer
->SetScrollChildren(nullptr);
583 LayerImpl
* clip_parent
= nullptr;
585 clip_parent
= layer
->layer_tree_impl()->LayerById(
590 layer
->SetClipParent(clip_parent
);
591 if (clip_children_
) {
592 std::set
<LayerImpl
*>* clip_children
= new std::set
<LayerImpl
*>;
593 for (std::set
<LayerImpl
*>::iterator it
= clip_children_
->begin();
594 it
!= clip_children_
->end(); ++it
)
595 clip_children
->insert(layer
->layer_tree_impl()->LayerById((*it
)->id()));
596 layer
->SetClipChildren(clip_children
);
598 layer
->SetClipChildren(nullptr);
601 layer
->PassCopyRequests(©_requests_
);
603 // If the main thread commits multiple times before the impl thread actually
604 // draws, then damage tracking will become incorrect if we simply clobber the
605 // update_rect here. The LayerImpl's update_rect needs to accumulate (i.e.
606 // union) any update changes that have occurred on the main thread.
607 update_rect_
.Union(layer
->update_rect());
608 layer
->SetUpdateRect(update_rect_
);
610 layer
->SetStackingOrderChanged(stacking_order_changed_
);
611 layer
->SetDebugInfo(debug_info_
);
613 if (frame_timing_requests_dirty_
) {
614 layer
->PassFrameTimingRequests(&frame_timing_requests_
);
615 frame_timing_requests_dirty_
= false;
618 // Reset any state that should be cleared for the next update.
619 stacking_order_changed_
= false;
620 update_rect_
= gfx::Rect();
621 needs_push_properties_
= false;
622 num_dependents_need_push_properties_
= 0;
625 gfx::Vector2dF
LayerImpl::FixedContainerSizeDelta() const {
626 if (!scroll_clip_layer_
)
627 return gfx::Vector2dF();
629 gfx::Vector2dF delta_from_scroll
= scroll_clip_layer_
->bounds_delta();
631 // In virtual-viewport mode, we don't need to compensate for pinch zoom or
632 // scale since the fixed container is the outer viewport, which sits below
634 if (layer_tree_impl()->settings().use_pinch_virtual_viewport
)
635 return delta_from_scroll
;
637 float scale_delta
= layer_tree_impl()->page_scale_delta();
638 float scale
= layer_tree_impl()->current_page_scale_factor() /
639 layer_tree_impl()->page_scale_delta();
641 delta_from_scroll
.Scale(1.f
/ scale
);
643 // The delta-from-pinch component requires some explanation: A viewport of
644 // size (w,h) will appear to be size (w/s,h/s) under scale s in the content
645 // space. If s -> s' on the impl thread, where s' = s * ds, then the apparent
646 // viewport size change in the content space due to ds is:
648 // (w/s',h/s') - (w/s,h/s) = (w,h)(1/s' - 1/s) = (w,h)(1 - ds)/(s ds)
650 gfx::Vector2dF delta_from_pinch
=
651 gfx::Rect(scroll_clip_layer_
->bounds()).bottom_right() - gfx::PointF();
652 delta_from_pinch
.Scale((1.f
- scale_delta
) / (scale
* scale_delta
));
654 return delta_from_scroll
+ delta_from_pinch
;
657 base::DictionaryValue
* LayerImpl::LayerTreeAsJson() const {
658 base::DictionaryValue
* result
= new base::DictionaryValue
;
659 result
->SetString("LayerType", LayerTypeAsString());
661 base::ListValue
* list
= new base::ListValue
;
662 list
->AppendInteger(bounds().width());
663 list
->AppendInteger(bounds().height());
664 result
->Set("Bounds", list
);
666 list
= new base::ListValue
;
667 list
->AppendDouble(position_
.x());
668 list
->AppendDouble(position_
.y());
669 result
->Set("Position", list
);
671 const gfx::Transform
& gfx_transform
= draw_properties_
.target_space_transform
;
672 double transform
[16];
673 gfx_transform
.matrix().asColMajord(transform
);
674 list
= new base::ListValue
;
675 for (int i
= 0; i
< 16; ++i
)
676 list
->AppendDouble(transform
[i
]);
677 result
->Set("DrawTransform", list
);
679 result
->SetBoolean("DrawsContent", draws_content_
);
680 result
->SetBoolean("Is3dSorted", Is3dSorted());
681 result
->SetDouble("OPACITY", opacity());
682 result
->SetBoolean("ContentsOpaque", contents_opaque_
);
685 result
->SetBoolean("Scrollable", true);
687 if (have_wheel_event_handlers_
)
688 result
->SetBoolean("WheelHandler", have_wheel_event_handlers_
);
689 if (have_scroll_event_handlers_
)
690 result
->SetBoolean("ScrollHandler", have_scroll_event_handlers_
);
691 if (!touch_event_handler_region_
.IsEmpty()) {
692 scoped_ptr
<base::Value
> region
= touch_event_handler_region_
.AsValue();
693 result
->Set("TouchRegion", region
.release());
696 if (scroll_blocks_on_
) {
697 list
= new base::ListValue
;
698 if (scroll_blocks_on_
& SCROLL_BLOCKS_ON_START_TOUCH
)
699 list
->AppendString("StartTouch");
700 if (scroll_blocks_on_
& SCROLL_BLOCKS_ON_WHEEL_EVENT
)
701 list
->AppendString("WheelEvent");
702 if (scroll_blocks_on_
& SCROLL_BLOCKS_ON_SCROLL_EVENT
)
703 list
->AppendString("ScrollEvent");
704 result
->Set("ScrollBlocksOn", list
);
707 list
= new base::ListValue
;
708 for (size_t i
= 0; i
< children_
.size(); ++i
)
709 list
->Append(children_
[i
]->LayerTreeAsJson());
710 result
->Set("Children", list
);
715 void LayerImpl::SetStackingOrderChanged(bool stacking_order_changed
) {
716 if (stacking_order_changed
) {
717 stacking_order_changed_
= true;
718 NoteLayerPropertyChangedForSubtree();
722 void LayerImpl::NoteLayerPropertyChanged() {
723 layer_property_changed_
= true;
724 layer_tree_impl()->set_needs_update_draw_properties();
725 SetNeedsPushProperties();
728 void LayerImpl::NoteLayerPropertyChangedForSubtree() {
729 layer_property_changed_
= true;
730 layer_tree_impl()->set_needs_update_draw_properties();
731 for (size_t i
= 0; i
< children_
.size(); ++i
)
732 children_
[i
]->NoteLayerPropertyChangedForDescendantsInternal();
733 SetNeedsPushProperties();
736 void LayerImpl::NoteLayerPropertyChangedForDescendantsInternal() {
737 layer_property_changed_
= true;
738 for (size_t i
= 0; i
< children_
.size(); ++i
)
739 children_
[i
]->NoteLayerPropertyChangedForDescendantsInternal();
742 void LayerImpl::NoteLayerPropertyChangedForDescendants() {
743 layer_tree_impl()->set_needs_update_draw_properties();
744 for (size_t i
= 0; i
< children_
.size(); ++i
)
745 children_
[i
]->NoteLayerPropertyChangedForDescendantsInternal();
746 SetNeedsPushProperties();
750 // Verify that the resource id is valid.
751 static ResourceProvider::ResourceId
ValidateResource(
752 const ResourceProvider
* provider
,
753 ResourceProvider::ResourceId id
) {
754 provider
->ValidateResource(id
);
759 void LayerImpl::ValidateQuadResourcesInternal(DrawQuad
* quad
) const {
761 quad
->IterateResources(
762 base::Bind(&ValidateResource
, layer_tree_impl_
->resource_provider()));
766 const char* LayerImpl::LayerTypeAsString() const {
767 return "cc::LayerImpl";
770 void LayerImpl::ResetAllChangeTrackingForSubtree() {
771 layer_property_changed_
= false;
773 update_rect_
= gfx::Rect();
774 damage_rect_
= gfx::RectF();
777 render_surface_
->ResetPropertyChangedFlag();
780 mask_layer_
->ResetAllChangeTrackingForSubtree();
782 if (replica_layer_
) {
783 // This also resets the replica mask, if it exists.
784 replica_layer_
->ResetAllChangeTrackingForSubtree();
787 for (size_t i
= 0; i
< children_
.size(); ++i
)
788 children_
[i
]->ResetAllChangeTrackingForSubtree();
790 needs_push_properties_
= false;
791 num_dependents_need_push_properties_
= 0;
794 gfx::ScrollOffset
LayerImpl::ScrollOffsetForAnimation() const {
795 return CurrentScrollOffset();
798 void LayerImpl::OnFilterAnimated(const FilterOperations
& filters
) {
802 void LayerImpl::OnOpacityAnimated(float opacity
) {
806 void LayerImpl::OnTransformAnimated(const gfx::Transform
& transform
) {
807 SetTransform(transform
);
810 void LayerImpl::OnScrollOffsetAnimated(const gfx::ScrollOffset
& scroll_offset
) {
811 // Only layers in the active tree should need to do anything here, since
812 // layers in the pending tree will find out about these changes as a
813 // result of the shared SyncedProperty.
817 SetCurrentScrollOffset(scroll_offset
);
819 layer_tree_impl_
->DidAnimateScrollOffset();
822 void LayerImpl::OnAnimationWaitingForDeletion() {}
824 bool LayerImpl::IsActive() const {
825 return layer_tree_impl_
->IsActiveTree();
828 gfx::Size
LayerImpl::bounds() const {
829 gfx::Vector2d delta
= gfx::ToCeiledVector2d(bounds_delta_
);
830 return gfx::Size(bounds_
.width() + delta
.x(),
831 bounds_
.height() + delta
.y());
834 gfx::SizeF
LayerImpl::BoundsForScrolling() const {
835 return gfx::SizeF(bounds_
.width() + bounds_delta_
.x(),
836 bounds_
.height() + bounds_delta_
.y());
839 void LayerImpl::SetBounds(const gfx::Size
& bounds
) {
840 if (bounds_
== bounds
)
845 ScrollbarParametersDidChange(true);
846 if (masks_to_bounds())
847 NoteLayerPropertyChangedForSubtree();
849 NoteLayerPropertyChanged();
852 void LayerImpl::SetBoundsDelta(const gfx::Vector2dF
& bounds_delta
) {
853 if (bounds_delta_
== bounds_delta
)
856 bounds_delta_
= bounds_delta
;
858 ScrollbarParametersDidChange(true);
859 if (masks_to_bounds())
860 NoteLayerPropertyChangedForSubtree();
862 NoteLayerPropertyChanged();
865 void LayerImpl::SetMaskLayer(scoped_ptr
<LayerImpl
> mask_layer
) {
866 int new_layer_id
= mask_layer
? mask_layer
->id() : -1;
869 DCHECK_EQ(layer_tree_impl(), mask_layer
->layer_tree_impl());
870 DCHECK_NE(new_layer_id
, mask_layer_id_
);
871 } else if (new_layer_id
== mask_layer_id_
) {
875 mask_layer_
= mask_layer
.Pass();
876 mask_layer_id_
= new_layer_id
;
878 mask_layer_
->SetParent(this);
879 NoteLayerPropertyChangedForSubtree();
882 scoped_ptr
<LayerImpl
> LayerImpl::TakeMaskLayer() {
884 return mask_layer_
.Pass();
887 void LayerImpl::SetReplicaLayer(scoped_ptr
<LayerImpl
> replica_layer
) {
888 int new_layer_id
= replica_layer
? replica_layer
->id() : -1;
891 DCHECK_EQ(layer_tree_impl(), replica_layer
->layer_tree_impl());
892 DCHECK_NE(new_layer_id
, replica_layer_id_
);
893 } else if (new_layer_id
== replica_layer_id_
) {
897 replica_layer_
= replica_layer
.Pass();
898 replica_layer_id_
= new_layer_id
;
900 replica_layer_
->SetParent(this);
901 NoteLayerPropertyChangedForSubtree();
904 scoped_ptr
<LayerImpl
> LayerImpl::TakeReplicaLayer() {
905 replica_layer_id_
= -1;
906 return replica_layer_
.Pass();
909 ScrollbarLayerImplBase
* LayerImpl::ToScrollbarLayer() {
913 void LayerImpl::SetDrawsContent(bool draws_content
) {
914 if (draws_content_
== draws_content
)
917 draws_content_
= draws_content
;
918 NoteLayerPropertyChanged();
921 void LayerImpl::SetHideLayerAndSubtree(bool hide
) {
922 if (hide_layer_and_subtree_
== hide
)
925 hide_layer_and_subtree_
= hide
;
926 NoteLayerPropertyChangedForSubtree();
929 void LayerImpl::SetTransformOrigin(const gfx::Point3F
& transform_origin
) {
930 if (transform_origin_
== transform_origin
)
932 transform_origin_
= transform_origin
;
933 NoteLayerPropertyChangedForSubtree();
936 void LayerImpl::SetBackgroundColor(SkColor background_color
) {
937 if (background_color_
== background_color
)
940 background_color_
= background_color
;
941 NoteLayerPropertyChanged();
944 SkColor
LayerImpl::SafeOpaqueBackgroundColor() const {
945 SkColor color
= background_color();
946 if (SkColorGetA(color
) == 255 && !contents_opaque()) {
947 color
= SK_ColorTRANSPARENT
;
948 } else if (SkColorGetA(color
) != 255 && contents_opaque()) {
949 for (const LayerImpl
* layer
= parent(); layer
;
950 layer
= layer
->parent()) {
951 color
= layer
->background_color();
952 if (SkColorGetA(color
) == 255)
955 if (SkColorGetA(color
) != 255)
956 color
= layer_tree_impl()->background_color();
957 if (SkColorGetA(color
) != 255)
958 color
= SkColorSetA(color
, 255);
963 void LayerImpl::SetFilters(const FilterOperations
& filters
) {
964 if (filters_
== filters
)
968 NoteLayerPropertyChangedForSubtree();
971 bool LayerImpl::FilterIsAnimating() const {
972 return layer_animation_controller_
->IsAnimatingProperty(Animation::FILTER
);
975 bool LayerImpl::FilterIsAnimatingOnImplOnly() const {
976 Animation
* filter_animation
=
977 layer_animation_controller_
->GetAnimation(Animation::FILTER
);
978 return filter_animation
&& filter_animation
->is_impl_only();
981 void LayerImpl::SetBackgroundFilters(
982 const FilterOperations
& filters
) {
983 if (background_filters_
== filters
)
986 background_filters_
= filters
;
987 NoteLayerPropertyChanged();
990 void LayerImpl::SetMasksToBounds(bool masks_to_bounds
) {
991 if (masks_to_bounds_
== masks_to_bounds
)
994 masks_to_bounds_
= masks_to_bounds
;
995 NoteLayerPropertyChangedForSubtree();
998 void LayerImpl::SetContentsOpaque(bool opaque
) {
999 if (contents_opaque_
== opaque
)
1002 contents_opaque_
= opaque
;
1003 NoteLayerPropertyChangedForSubtree();
1006 void LayerImpl::SetOpacity(float opacity
) {
1007 if (opacity_
== opacity
)
1011 NoteLayerPropertyChangedForSubtree();
1014 bool LayerImpl::OpacityIsAnimating() const {
1015 return layer_animation_controller_
->IsAnimatingProperty(Animation::OPACITY
);
1018 bool LayerImpl::OpacityIsAnimatingOnImplOnly() const {
1019 Animation
* opacity_animation
=
1020 layer_animation_controller_
->GetAnimation(Animation::OPACITY
);
1021 return opacity_animation
&& opacity_animation
->is_impl_only();
1024 void LayerImpl::SetBlendMode(SkXfermode::Mode blend_mode
) {
1025 if (blend_mode_
== blend_mode
)
1028 blend_mode_
= blend_mode
;
1029 NoteLayerPropertyChangedForSubtree();
1032 void LayerImpl::SetIsRootForIsolatedGroup(bool root
) {
1033 if (is_root_for_isolated_group_
== root
)
1036 is_root_for_isolated_group_
= root
;
1037 SetNeedsPushProperties();
1040 void LayerImpl::SetPosition(const gfx::PointF
& position
) {
1041 if (position_
== position
)
1044 position_
= position
;
1045 NoteLayerPropertyChangedForSubtree();
1048 void LayerImpl::SetShouldFlattenTransform(bool flatten
) {
1049 if (should_flatten_transform_
== flatten
)
1052 should_flatten_transform_
= flatten
;
1053 NoteLayerPropertyChangedForSubtree();
1056 void LayerImpl::Set3dSortingContextId(int id
) {
1057 if (id
== sorting_context_id_
)
1059 sorting_context_id_
= id
;
1060 NoteLayerPropertyChangedForSubtree();
1063 void LayerImpl::PassFrameTimingRequests(
1064 std::vector
<FrameTimingRequest
>* requests
) {
1065 frame_timing_requests_
.swap(*requests
);
1066 frame_timing_requests_dirty_
= true;
1067 SetNeedsPushProperties();
1070 void LayerImpl::GatherFrameTimingRequestIds(std::vector
<int64_t>* request_ids
) {
1071 for (const auto& request
: frame_timing_requests_
)
1072 request_ids
->push_back(request
.id());
1075 void LayerImpl::SetTransform(const gfx::Transform
& transform
) {
1076 if (transform_
== transform
)
1079 transform_
= transform
;
1080 transform_is_invertible_
= transform_
.IsInvertible();
1081 NoteLayerPropertyChangedForSubtree();
1084 void LayerImpl::SetTransformAndInvertibility(const gfx::Transform
& transform
,
1085 bool transform_is_invertible
) {
1086 if (transform_
== transform
) {
1087 DCHECK(transform_is_invertible_
== transform_is_invertible
)
1088 << "Can't change invertibility if transform is unchanged";
1091 transform_
= transform
;
1092 transform_is_invertible_
= transform_is_invertible
;
1093 NoteLayerPropertyChangedForSubtree();
1096 bool LayerImpl::TransformIsAnimating() const {
1097 return layer_animation_controller_
->IsAnimatingProperty(Animation::TRANSFORM
);
1100 bool LayerImpl::TransformIsAnimatingOnImplOnly() const {
1101 Animation
* transform_animation
=
1102 layer_animation_controller_
->GetAnimation(Animation::TRANSFORM
);
1103 return transform_animation
&& transform_animation
->is_impl_only();
1106 void LayerImpl::SetUpdateRect(const gfx::Rect
& update_rect
) {
1107 update_rect_
= update_rect
;
1108 SetNeedsPushProperties();
1111 void LayerImpl::AddDamageRect(const gfx::RectF
& damage_rect
) {
1112 damage_rect_
= gfx::UnionRects(damage_rect_
, damage_rect
);
1115 void LayerImpl::SetContentBounds(const gfx::Size
& content_bounds
) {
1116 if (this->content_bounds() == content_bounds
)
1119 draw_properties_
.content_bounds
= content_bounds
;
1120 NoteLayerPropertyChanged();
1123 void LayerImpl::SetContentsScale(float contents_scale_x
,
1124 float contents_scale_y
) {
1125 if (this->contents_scale_x() == contents_scale_x
&&
1126 this->contents_scale_y() == contents_scale_y
)
1129 draw_properties_
.contents_scale_x
= contents_scale_x
;
1130 draw_properties_
.contents_scale_y
= contents_scale_y
;
1131 NoteLayerPropertyChanged();
1134 bool LayerImpl::IsExternalFlingActive() const {
1135 return layer_tree_impl_
->IsExternalFlingActive();
1138 void LayerImpl::SetCurrentScrollOffset(const gfx::ScrollOffset
& scroll_offset
) {
1140 if (scroll_offset_
->SetCurrent(scroll_offset
))
1141 DidUpdateScrollOffset(false);
1144 void LayerImpl::SetCurrentScrollOffsetFromDelegate(
1145 const gfx::ScrollOffset
& scroll_offset
) {
1147 if (scroll_offset_
->SetCurrent(scroll_offset
))
1148 DidUpdateScrollOffset(true);
1151 void LayerImpl::PushScrollOffsetFromMainThread(
1152 const gfx::ScrollOffset
& scroll_offset
) {
1153 PushScrollOffset(&scroll_offset
);
1156 void LayerImpl::PushScrollOffsetFromMainThreadAndClobberActiveValue(
1157 const gfx::ScrollOffset
& scroll_offset
) {
1158 scroll_offset_
->set_clobber_active_value();
1159 PushScrollOffset(&scroll_offset
);
1162 gfx::ScrollOffset
LayerImpl::PullDeltaForMainThread() {
1163 // TODO(miletus): Remove all this temporary flooring machinery when
1164 // Blink fully supports fractional scrolls.
1165 gfx::ScrollOffset current_offset
= CurrentScrollOffset();
1166 gfx::Vector2dF current_delta
= ScrollDelta();
1167 gfx::Vector2dF
floored_delta(floor(current_delta
.x()),
1168 floor(current_delta
.y()));
1169 gfx::Vector2dF diff_delta
= floored_delta
- current_delta
;
1170 gfx::ScrollOffset tmp_offset
= ScrollOffsetWithDelta(current_offset
,
1172 scroll_offset_
->SetCurrent(tmp_offset
);
1173 gfx::ScrollOffset delta
= scroll_offset_
->PullDeltaForMainThread();
1174 scroll_offset_
->SetCurrent(current_offset
);
1178 gfx::ScrollOffset
LayerImpl::CurrentScrollOffset() const {
1179 return scroll_offset_
->Current(IsActive());
1182 gfx::Vector2dF
LayerImpl::ScrollDelta() const {
1184 return gfx::Vector2dF(scroll_offset_
->Delta().x(),
1185 scroll_offset_
->Delta().y());
1187 return gfx::Vector2dF(scroll_offset_
->PendingDelta().get().x(),
1188 scroll_offset_
->PendingDelta().get().y());
1191 void LayerImpl::SetScrollDelta(const gfx::Vector2dF
& delta
) {
1193 SetCurrentScrollOffset(scroll_offset_
->ActiveBase() +
1194 gfx::ScrollOffset(delta
));
1197 gfx::ScrollOffset
LayerImpl::BaseScrollOffset() const {
1199 return scroll_offset_
->ActiveBase();
1201 return scroll_offset_
->PendingBase();
1204 void LayerImpl::PushScrollOffset(const gfx::ScrollOffset
* scroll_offset
) {
1205 DCHECK(scroll_offset
|| IsActive());
1206 bool changed
= false;
1207 if (scroll_offset
) {
1208 DCHECK(!IsActive() || !layer_tree_impl_
->FindPendingTreeLayerById(id()));
1209 changed
|= scroll_offset_
->PushFromMainThread(*scroll_offset
);
1212 changed
|= scroll_offset_
->PushPendingToActive();
1216 DidUpdateScrollOffset(false);
1219 void LayerImpl::DidUpdateScrollOffset(bool is_from_root_delegate
) {
1220 if (!is_from_root_delegate
)
1221 layer_tree_impl()->DidUpdateScrollOffset(id());
1222 NoteLayerPropertyChangedForSubtree();
1223 ScrollbarParametersDidChange(false);
1224 // Inform the pending twin that a property changed.
1225 if (layer_tree_impl()->IsActiveTree()) {
1226 LayerImpl
* pending_twin
= layer_tree_impl()->FindPendingTreeLayerById(id());
1228 pending_twin
->NoteLayerPropertyChangedForSubtree();
1232 void LayerImpl::SetDoubleSided(bool double_sided
) {
1233 if (double_sided_
== double_sided
)
1236 double_sided_
= double_sided
;
1237 NoteLayerPropertyChangedForSubtree();
1240 SimpleEnclosedRegion
LayerImpl::VisibleContentOpaqueRegion() const {
1241 if (contents_opaque())
1242 return SimpleEnclosedRegion(visible_content_rect());
1243 return SimpleEnclosedRegion();
1246 void LayerImpl::DidBeginTracing() {}
1248 void LayerImpl::ReleaseResources() {}
1250 void LayerImpl::RecreateResources() {
1253 gfx::ScrollOffset
LayerImpl::MaxScrollOffset() const {
1254 if (!scroll_clip_layer_
|| bounds().IsEmpty())
1255 return gfx::ScrollOffset();
1257 LayerImpl
const* page_scale_layer
= layer_tree_impl()->page_scale_layer();
1258 DCHECK(this != page_scale_layer
);
1259 DCHECK(this != layer_tree_impl()->InnerViewportScrollLayer() ||
1260 IsContainerForFixedPositionLayers());
1262 float scale_factor
= 1.f
;
1263 for (LayerImpl
const* current_layer
= this;
1264 current_layer
!= scroll_clip_layer_
->parent();
1265 current_layer
= current_layer
->parent()) {
1266 if (current_layer
== page_scale_layer
)
1267 scale_factor
= layer_tree_impl()->current_page_scale_factor();
1270 gfx::SizeF scaled_scroll_bounds
=
1271 gfx::ToFlooredSize(gfx::ScaleSize(BoundsForScrolling(), scale_factor
));
1272 scaled_scroll_bounds
= gfx::ToFlooredSize(scaled_scroll_bounds
);
1274 gfx::ScrollOffset
max_offset(
1275 scaled_scroll_bounds
.width() - scroll_clip_layer_
->bounds().width(),
1276 scaled_scroll_bounds
.height() - scroll_clip_layer_
->bounds().height());
1277 // We need the final scroll offset to be in CSS coords.
1278 max_offset
.Scale(1 / scale_factor
);
1279 max_offset
.SetToMax(gfx::ScrollOffset());
1283 gfx::ScrollOffset
LayerImpl::ClampScrollOffsetToLimits(
1284 gfx::ScrollOffset offset
) const {
1285 offset
.SetToMin(MaxScrollOffset());
1286 offset
.SetToMax(gfx::ScrollOffset());
1290 gfx::Vector2dF
LayerImpl::ClampScrollToMaxScrollOffset() {
1291 gfx::ScrollOffset old_offset
= CurrentScrollOffset();
1292 gfx::ScrollOffset clamped_offset
= ClampScrollOffsetToLimits(old_offset
);
1293 gfx::Vector2dF delta
= clamped_offset
.DeltaFrom(old_offset
);
1294 if (!delta
.IsZero())
1299 void LayerImpl::SetScrollbarPosition(ScrollbarLayerImplBase
* scrollbar_layer
,
1300 LayerImpl
* scrollbar_clip_layer
,
1301 bool on_resize
) const {
1302 DCHECK(scrollbar_layer
);
1303 LayerImpl
* page_scale_layer
= layer_tree_impl()->page_scale_layer();
1305 DCHECK(this != page_scale_layer
);
1306 DCHECK(scrollbar_clip_layer
);
1307 gfx::RectF
clip_rect(gfx::PointF(),
1308 scrollbar_clip_layer
->BoundsForScrolling());
1310 // See comment in MaxScrollOffset() regarding the use of the content layer
1312 gfx::RectF
scroll_rect(gfx::PointF(), BoundsForScrolling());
1314 if (scroll_rect
.size().IsEmpty())
1317 gfx::ScrollOffset current_offset
;
1318 for (LayerImpl
const* current_layer
= this;
1319 current_layer
!= scrollbar_clip_layer
->parent();
1320 current_layer
= current_layer
->parent()) {
1321 current_offset
+= current_layer
->CurrentScrollOffset();
1322 if (current_layer
== page_scale_layer
) {
1323 float scale_factor
= layer_tree_impl()->current_page_scale_factor();
1324 current_offset
.Scale(scale_factor
);
1325 scroll_rect
.Scale(scale_factor
);
1329 bool scrollbar_needs_animation
= false;
1330 scrollbar_needs_animation
|= scrollbar_layer
->SetVerticalAdjust(
1331 scrollbar_clip_layer
->bounds_delta().y());
1332 if (scrollbar_layer
->orientation() == HORIZONTAL
) {
1333 float visible_ratio
= clip_rect
.width() / scroll_rect
.width();
1334 scrollbar_needs_animation
|=
1335 scrollbar_layer
->SetCurrentPos(current_offset
.x());
1336 scrollbar_needs_animation
|=
1337 scrollbar_layer
->SetMaximum(scroll_rect
.width() - clip_rect
.width());
1338 scrollbar_needs_animation
|=
1339 scrollbar_layer
->SetVisibleToTotalLengthRatio(visible_ratio
);
1341 float visible_ratio
= clip_rect
.height() / scroll_rect
.height();
1342 bool y_offset_did_change
=
1343 scrollbar_layer
->SetCurrentPos(current_offset
.y());
1344 scrollbar_needs_animation
|= y_offset_did_change
;
1345 scrollbar_needs_animation
|=
1346 scrollbar_layer
->SetMaximum(scroll_rect
.height() - clip_rect
.height());
1347 scrollbar_needs_animation
|=
1348 scrollbar_layer
->SetVisibleToTotalLengthRatio(visible_ratio
);
1349 if (y_offset_did_change
&& layer_tree_impl()->IsActiveTree() &&
1350 this == layer_tree_impl()->InnerViewportScrollLayer()) {
1351 TRACE_COUNTER_ID1("cc", "scroll_offset_y", this->id(),
1352 current_offset
.y());
1355 if (scrollbar_needs_animation
) {
1356 layer_tree_impl()->set_needs_update_draw_properties();
1357 // TODO(wjmaclean) The scrollbar animator for the pinch-zoom scrollbars
1358 // should activate for every scroll on the main frame, not just the
1359 // scrolls that move the pinch virtual viewport (i.e. trigger from
1360 // either inner or outer viewport).
1361 if (scrollbar_animation_controller_
) {
1362 // Non-overlay scrollbars shouldn't trigger animations.
1363 if (scrollbar_layer
->is_overlay_scrollbar())
1364 scrollbar_animation_controller_
->DidScrollUpdate(on_resize
);
1369 void LayerImpl::DidBecomeActive() {
1370 if (layer_tree_impl_
->settings().scrollbar_animator
==
1371 LayerTreeSettings::NO_ANIMATOR
) {
1375 bool need_scrollbar_animation_controller
= scrollable() && scrollbars_
;
1376 if (!need_scrollbar_animation_controller
) {
1377 scrollbar_animation_controller_
= nullptr;
1381 if (scrollbar_animation_controller_
)
1384 scrollbar_animation_controller_
=
1385 layer_tree_impl_
->CreateScrollbarAnimationController(this);
1388 void LayerImpl::ClearScrollbars() {
1392 scrollbars_
.reset(nullptr);
1395 void LayerImpl::AddScrollbar(ScrollbarLayerImplBase
* layer
) {
1397 DCHECK(!scrollbars_
|| scrollbars_
->find(layer
) == scrollbars_
->end());
1399 scrollbars_
.reset(new ScrollbarSet());
1401 scrollbars_
->insert(layer
);
1404 void LayerImpl::RemoveScrollbar(ScrollbarLayerImplBase
* layer
) {
1405 DCHECK(scrollbars_
);
1407 DCHECK(scrollbars_
->find(layer
) != scrollbars_
->end());
1409 scrollbars_
->erase(layer
);
1410 if (scrollbars_
->empty())
1411 scrollbars_
= nullptr;
1414 bool LayerImpl::HasScrollbar(ScrollbarOrientation orientation
) const {
1418 for (ScrollbarSet::iterator it
= scrollbars_
->begin();
1419 it
!= scrollbars_
->end();
1421 if ((*it
)->orientation() == orientation
)
1427 void LayerImpl::ScrollbarParametersDidChange(bool on_resize
) {
1431 for (ScrollbarSet::iterator it
= scrollbars_
->begin();
1432 it
!= scrollbars_
->end();
1434 bool is_scroll_layer
= (*it
)->ScrollLayerId() == layer_id_
;
1435 bool scroll_layer_resized
= is_scroll_layer
&& on_resize
;
1436 (*it
)->ScrollbarParametersDidChange(scroll_layer_resized
);
1440 void LayerImpl::SetNeedsPushProperties() {
1441 if (needs_push_properties_
)
1443 if (!parent_should_know_need_push_properties() && parent_
)
1444 parent_
->AddDependentNeedsPushProperties();
1445 needs_push_properties_
= true;
1448 void LayerImpl::AddDependentNeedsPushProperties() {
1449 DCHECK_GE(num_dependents_need_push_properties_
, 0);
1451 if (!parent_should_know_need_push_properties() && parent_
)
1452 parent_
->AddDependentNeedsPushProperties();
1454 num_dependents_need_push_properties_
++;
1457 void LayerImpl::RemoveDependentNeedsPushProperties() {
1458 num_dependents_need_push_properties_
--;
1459 DCHECK_GE(num_dependents_need_push_properties_
, 0);
1461 if (!parent_should_know_need_push_properties() && parent_
)
1462 parent_
->RemoveDependentNeedsPushProperties();
1465 void LayerImpl::GetAllPrioritizedTilesForTracing(
1466 std::vector
<PrioritizedTile
>* prioritized_tiles
) const {
1469 void LayerImpl::AsValueInto(base::trace_event::TracedValue
* state
) const {
1470 TracedValue::MakeDictIntoImplicitSnapshotWithCategory(
1471 TRACE_DISABLED_BY_DEFAULT("cc.debug"),
1474 LayerTypeAsString(),
1476 state
->SetInteger("layer_id", id());
1477 MathUtil::AddToTracedValue("bounds", bounds_
, state
);
1479 state
->SetDouble("opacity", opacity());
1481 MathUtil::AddToTracedValue("position", position_
, state
);
1483 state
->SetInteger("draws_content", DrawsContent());
1484 state
->SetInteger("gpu_memory_usage", GPUMemoryUsageInBytes());
1486 MathUtil::AddToTracedValue(
1487 "scroll_offset", scroll_offset_
? scroll_offset_
->Current(IsActive())
1488 : gfx::ScrollOffset(),
1491 MathUtil::AddToTracedValue("transform_origin", transform_origin_
, state
);
1494 gfx::QuadF layer_quad
= MathUtil::MapQuad(
1495 screen_space_transform(),
1496 gfx::QuadF(gfx::Rect(content_bounds())),
1498 MathUtil::AddToTracedValue("layer_quad", layer_quad
, state
);
1499 if (!touch_event_handler_region_
.IsEmpty()) {
1500 state
->BeginArray("touch_event_handler_region");
1501 touch_event_handler_region_
.AsValueInto(state
);
1504 if (have_wheel_event_handlers_
) {
1505 gfx::Rect
wheel_rect(content_bounds());
1506 Region
wheel_region(wheel_rect
);
1507 state
->BeginArray("wheel_event_handler_region");
1508 wheel_region
.AsValueInto(state
);
1511 if (have_scroll_event_handlers_
) {
1512 gfx::Rect
scroll_rect(content_bounds());
1513 Region
scroll_region(scroll_rect
);
1514 state
->BeginArray("scroll_event_handler_region");
1515 scroll_region
.AsValueInto(state
);
1518 if (!non_fast_scrollable_region_
.IsEmpty()) {
1519 state
->BeginArray("non_fast_scrollable_region");
1520 non_fast_scrollable_region_
.AsValueInto(state
);
1523 if (scroll_blocks_on_
) {
1524 state
->SetInteger("scroll_blocks_on", scroll_blocks_on_
);
1527 state
->BeginArray("children");
1528 for (size_t i
= 0; i
< children_
.size(); ++i
) {
1529 state
->BeginDictionary();
1530 children_
[i
]->AsValueInto(state
);
1531 state
->EndDictionary();
1535 state
->BeginDictionary("mask_layer");
1536 mask_layer_
->AsValueInto(state
);
1537 state
->EndDictionary();
1539 if (replica_layer_
) {
1540 state
->BeginDictionary("replica_layer");
1541 replica_layer_
->AsValueInto(state
);
1542 state
->EndDictionary();
1546 state
->SetInteger("scroll_parent", scroll_parent_
->id());
1549 state
->SetInteger("clip_parent", clip_parent_
->id());
1551 state
->SetBoolean("can_use_lcd_text", can_use_lcd_text());
1552 state
->SetBoolean("contents_opaque", contents_opaque());
1555 "has_animation_bounds",
1556 layer_animation_controller()->HasAnimationThatInflatesBounds());
1559 if (LayerUtils::GetAnimationBounds(*this, &box
))
1560 MathUtil::AddToTracedValue("animation_bounds", box
, state
);
1562 if (debug_info_
.get()) {
1564 debug_info_
->AppendAsTraceFormat(&str
);
1565 base::JSONReader json_reader
;
1566 scoped_ptr
<base::Value
> debug_info_value(json_reader
.ReadToValue(str
));
1568 if (debug_info_value
->IsType(base::Value::TYPE_DICTIONARY
)) {
1569 base::DictionaryValue
* dictionary_value
= nullptr;
1570 bool converted_to_dictionary
=
1571 debug_info_value
->GetAsDictionary(&dictionary_value
);
1572 DCHECK(converted_to_dictionary
);
1573 for (base::DictionaryValue::Iterator
it(*dictionary_value
); !it
.IsAtEnd();
1575 state
->SetValue(it
.key().data(), it
.value().CreateDeepCopy());
1582 if (!frame_timing_requests_
.empty()) {
1583 state
->BeginArray("frame_timing_requests");
1584 for (const auto& request
: frame_timing_requests_
) {
1585 state
->BeginDictionary();
1586 state
->SetInteger("request_id", request
.id());
1587 MathUtil::AddToTracedValue("request_rect", request
.rect(), state
);
1588 state
->EndDictionary();
1594 bool LayerImpl::IsDrawnRenderSurfaceLayerListMember() const {
1595 return draw_properties_
.last_drawn_render_surface_layer_list_id
==
1596 layer_tree_impl_
->current_render_surface_list_id();
1599 size_t LayerImpl::GPUMemoryUsageInBytes() const { return 0; }
1601 void LayerImpl::RunMicroBenchmark(MicroBenchmarkImpl
* benchmark
) {
1602 benchmark
->RunOnLayer(this);
1605 int LayerImpl::NumDescendantsThatDrawContent() const {
1606 return num_descendants_that_draw_content_
;
1609 void LayerImpl::NotifyAnimationFinished(
1610 base::TimeTicks monotonic_time
,
1611 Animation::TargetProperty target_property
,
1613 if (target_property
== Animation::SCROLL_OFFSET
)
1614 layer_tree_impl_
->InputScrollAnimationFinished();
1617 void LayerImpl::SetHasRenderSurface(bool should_have_render_surface
) {
1618 if (!!render_surface() == should_have_render_surface
)
1621 SetNeedsPushProperties();
1622 layer_tree_impl()->set_needs_update_draw_properties();
1623 if (should_have_render_surface
) {
1624 render_surface_
= make_scoped_ptr(new RenderSurfaceImpl(this));
1627 render_surface_
.reset();
1630 Region
LayerImpl::GetInvalidationRegion() {
1631 return Region(update_rect_
);
1634 gfx::Rect
LayerImpl::GetEnclosingRectInTargetSpace() const {
1635 return MathUtil::MapEnclosingClippedRect(
1636 draw_properties_
.target_space_transform
,
1637 gfx::Rect(draw_properties_
.content_bounds
));
1640 gfx::Rect
LayerImpl::GetScaledEnclosingRectInTargetSpace(float scale
) const {
1641 gfx::Transform scaled_draw_transform
=
1642 draw_properties_
.target_space_transform
;
1643 scaled_draw_transform
.Scale(SK_MScalar1
/ scale
, SK_MScalar1
/ scale
);
1644 gfx::Size scaled_content_bounds
=
1645 gfx::ToCeiledSize(gfx::ScaleSize(content_bounds(), scale
));
1646 return MathUtil::MapEnclosingClippedRect(scaled_draw_transform
,
1647 gfx::Rect(scaled_content_bounds
));