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/debug/trace_event.h"
8 #include "base/debug/trace_event_argument.h"
9 #include "base/json/json_reader.h"
10 #include "base/strings/stringprintf.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/input/layer_scroll_offset_delegate.h"
20 #include "cc/layers/layer_utils.h"
21 #include "cc/layers/painted_scrollbar_layer_impl.h"
22 #include "cc/output/copy_output_request.h"
23 #include "cc/quads/debug_border_draw_quad.h"
24 #include "cc/quads/render_pass.h"
25 #include "cc/trees/layer_tree_host_common.h"
26 #include "cc/trees/layer_tree_impl.h"
27 #include "cc/trees/layer_tree_settings.h"
28 #include "cc/trees/proxy.h"
29 #include "ui/gfx/geometry/box_f.h"
30 #include "ui/gfx/geometry/point_conversions.h"
31 #include "ui/gfx/geometry/quad_f.h"
32 #include "ui/gfx/geometry/rect_conversions.h"
33 #include "ui/gfx/geometry/size_conversions.h"
34 #include "ui/gfx/geometry/vector2d_conversions.h"
37 LayerImpl::LayerImpl(LayerTreeImpl
* tree_impl
, int id
)
39 scroll_parent_(nullptr),
40 clip_parent_(nullptr),
42 replica_layer_id_(-1),
44 layer_tree_impl_(tree_impl
),
45 scroll_offset_delegate_(nullptr),
46 scroll_clip_layer_(nullptr),
47 should_scroll_on_main_thread_(false),
48 have_wheel_event_handlers_(false),
49 have_scroll_event_handlers_(false),
50 user_scrollable_horizontal_(true),
51 user_scrollable_vertical_(true),
52 stacking_order_changed_(false),
54 should_flatten_transform_(true),
55 layer_property_changed_(false),
56 masks_to_bounds_(false),
57 contents_opaque_(false),
58 is_root_for_isolated_group_(false),
59 use_parent_backface_visibility_(false),
60 draw_checkerboard_for_missing_tiles_(false),
61 draws_content_(false),
62 hide_layer_and_subtree_(false),
63 transform_is_invertible_(true),
64 is_container_for_fixed_position_layers_(false),
67 blend_mode_(SkXfermode::kSrcOver_Mode
),
68 num_descendants_that_draw_content_(0),
70 needs_push_properties_(false),
71 num_dependents_need_push_properties_(0),
72 sorting_context_id_(0),
73 current_draw_mode_(DRAW_MODE_NONE
) {
74 DCHECK_GT(layer_id_
, 0);
75 DCHECK(layer_tree_impl_
);
76 layer_tree_impl_
->RegisterLayer(this);
77 AnimationRegistrar
* registrar
= layer_tree_impl_
->animationRegistrar();
78 layer_animation_controller_
=
79 registrar
->GetAnimationControllerForId(layer_id_
);
80 layer_animation_controller_
->AddValueObserver(this);
82 layer_animation_controller_
->set_value_provider(this);
83 layer_animation_controller_
->set_layer_animation_delegate(this);
85 SetNeedsPushProperties();
88 LayerImpl::~LayerImpl() {
89 DCHECK_EQ(DRAW_MODE_NONE
, current_draw_mode_
);
91 layer_animation_controller_
->RemoveValueObserver(this);
92 layer_animation_controller_
->remove_value_provider(this);
93 layer_animation_controller_
->remove_layer_animation_delegate(this);
95 if (!copy_requests_
.empty() && layer_tree_impl_
->IsActiveTree())
96 layer_tree_impl()->RemoveLayerWithCopyOutputRequest(this);
97 layer_tree_impl_
->UnregisterLayer(this);
99 TRACE_EVENT_OBJECT_DELETED_WITH_ID(
100 TRACE_DISABLED_BY_DEFAULT("cc.debug"), "cc::LayerImpl", this);
103 void LayerImpl::AddChild(scoped_ptr
<LayerImpl
> child
) {
104 child
->SetParent(this);
105 DCHECK_EQ(layer_tree_impl(), child
->layer_tree_impl());
106 children_
.push_back(child
.Pass());
107 layer_tree_impl()->set_needs_update_draw_properties();
110 scoped_ptr
<LayerImpl
> LayerImpl::RemoveChild(LayerImpl
* child
) {
111 for (OwnedLayerImplList::iterator it
= children_
.begin();
112 it
!= children_
.end();
115 scoped_ptr
<LayerImpl
> ret
= children_
.take(it
);
117 layer_tree_impl()->set_needs_update_draw_properties();
124 void LayerImpl::SetParent(LayerImpl
* parent
) {
125 if (parent_should_know_need_push_properties()) {
127 parent_
->RemoveDependentNeedsPushProperties();
129 parent
->AddDependentNeedsPushProperties();
134 void LayerImpl::ClearChildList() {
135 if (children_
.empty())
139 layer_tree_impl()->set_needs_update_draw_properties();
142 bool LayerImpl::HasAncestor(const LayerImpl
* ancestor
) const {
146 for (const LayerImpl
* layer
= this; layer
; layer
= layer
->parent()) {
147 if (layer
== ancestor
)
154 void LayerImpl::SetScrollParent(LayerImpl
* parent
) {
155 if (scroll_parent_
== parent
)
158 // Having both a scroll parent and a scroll offset delegate is unsupported.
159 DCHECK(!scroll_offset_delegate_
);
162 DCHECK_EQ(layer_tree_impl()->LayerById(parent
->id()), parent
);
164 scroll_parent_
= parent
;
165 SetNeedsPushProperties();
168 void LayerImpl::SetDebugInfo(
169 scoped_refptr
<base::debug::ConvertableToTraceFormat
> other
) {
171 SetNeedsPushProperties();
174 void LayerImpl::SetScrollChildren(std::set
<LayerImpl
*>* children
) {
175 if (scroll_children_
.get() == children
)
177 scroll_children_
.reset(children
);
178 SetNeedsPushProperties();
181 void LayerImpl::SetNumDescendantsThatDrawContent(int num_descendants
) {
182 if (num_descendants_that_draw_content_
== num_descendants
)
184 num_descendants_that_draw_content_
= num_descendants
;
185 SetNeedsPushProperties();
188 void LayerImpl::SetClipParent(LayerImpl
* ancestor
) {
189 if (clip_parent_
== ancestor
)
192 clip_parent_
= ancestor
;
193 SetNeedsPushProperties();
196 void LayerImpl::SetClipChildren(std::set
<LayerImpl
*>* children
) {
197 if (clip_children_
.get() == children
)
199 clip_children_
.reset(children
);
200 SetNeedsPushProperties();
203 void LayerImpl::PassCopyRequests(ScopedPtrVector
<CopyOutputRequest
>* requests
) {
204 if (requests
->empty())
206 DCHECK(render_surface());
207 bool was_empty
= copy_requests_
.empty();
208 copy_requests_
.insert_and_take(copy_requests_
.end(), requests
);
211 if (was_empty
&& layer_tree_impl()->IsActiveTree())
212 layer_tree_impl()->AddLayerWithCopyOutputRequest(this);
213 NoteLayerPropertyChangedForSubtree();
216 void LayerImpl::TakeCopyRequestsAndTransformToTarget(
217 ScopedPtrVector
<CopyOutputRequest
>* requests
) {
218 DCHECK(!copy_requests_
.empty());
219 DCHECK(layer_tree_impl()->IsActiveTree());
220 DCHECK_EQ(render_target(), this);
222 size_t first_inserted_request
= requests
->size();
223 requests
->insert_and_take(requests
->end(), ©_requests_
);
224 copy_requests_
.clear();
226 for (size_t i
= first_inserted_request
; i
< requests
->size(); ++i
) {
227 CopyOutputRequest
* request
= requests
->at(i
);
228 if (!request
->has_area())
231 gfx::Rect request_in_layer_space
= request
->area();
232 gfx::Rect request_in_content_space
=
233 LayerRectToContentRect(request_in_layer_space
);
234 request
->set_area(MathUtil::MapEnclosingClippedRect(
235 draw_properties_
.target_space_transform
, request_in_content_space
));
238 layer_tree_impl()->RemoveLayerWithCopyOutputRequest(this);
241 void LayerImpl::ClearRenderSurfaceLayerList() {
243 render_surface_
->ClearLayerLists();
246 void LayerImpl::PopulateSharedQuadState(SharedQuadState
* state
) const {
248 draw_properties_
.target_space_transform
, draw_properties_
.content_bounds
,
249 draw_properties_
.visible_content_rect
, draw_properties_
.clip_rect
,
250 draw_properties_
.is_clipped
, draw_properties_
.opacity
,
251 draw_properties_
.blend_mode
, sorting_context_id_
);
254 bool LayerImpl::WillDraw(DrawMode draw_mode
,
255 ResourceProvider
* resource_provider
) {
256 // WillDraw/DidDraw must be matched.
257 DCHECK_NE(DRAW_MODE_NONE
, draw_mode
);
258 DCHECK_EQ(DRAW_MODE_NONE
, current_draw_mode_
);
259 current_draw_mode_
= draw_mode
;
263 void LayerImpl::DidDraw(ResourceProvider
* resource_provider
) {
264 DCHECK_NE(DRAW_MODE_NONE
, current_draw_mode_
);
265 current_draw_mode_
= DRAW_MODE_NONE
;
268 bool LayerImpl::ShowDebugBorders() const {
269 return layer_tree_impl()->debug_state().show_debug_borders
;
272 void LayerImpl::GetDebugBorderProperties(SkColor
* color
, float* width
) const {
273 if (draws_content_
) {
274 *color
= DebugColors::ContentLayerBorderColor();
275 *width
= DebugColors::ContentLayerBorderWidth(layer_tree_impl());
279 if (masks_to_bounds_
) {
280 *color
= DebugColors::MaskingLayerBorderColor();
281 *width
= DebugColors::MaskingLayerBorderWidth(layer_tree_impl());
285 *color
= DebugColors::ContainerLayerBorderColor();
286 *width
= DebugColors::ContainerLayerBorderWidth(layer_tree_impl());
289 void LayerImpl::AppendDebugBorderQuad(
290 RenderPass
* render_pass
,
291 const gfx::Size
& content_bounds
,
292 const SharedQuadState
* shared_quad_state
,
293 AppendQuadsData
* append_quads_data
) const {
296 GetDebugBorderProperties(&color
, &width
);
297 AppendDebugBorderQuad(render_pass
,
305 void LayerImpl::AppendDebugBorderQuad(RenderPass
* render_pass
,
306 const gfx::Size
& content_bounds
,
307 const SharedQuadState
* shared_quad_state
,
308 AppendQuadsData
* append_quads_data
,
311 if (!ShowDebugBorders())
314 gfx::Rect
quad_rect(content_bounds
);
315 gfx::Rect
visible_quad_rect(quad_rect
);
316 DebugBorderDrawQuad
* debug_border_quad
=
317 render_pass
->CreateAndAppendDrawQuad
<DebugBorderDrawQuad
>();
318 debug_border_quad
->SetNew(
319 shared_quad_state
, quad_rect
, visible_quad_rect
, color
, width
);
322 bool LayerImpl::HasDelegatedContent() const {
326 bool LayerImpl::HasContributingDelegatedRenderPasses() const {
330 RenderPassId
LayerImpl::FirstContributingRenderPassId() const {
331 return RenderPassId(0, 0);
334 RenderPassId
LayerImpl::NextContributingRenderPassId(RenderPassId id
) const {
335 return RenderPassId(0, 0);
338 bool LayerImpl::UpdateTiles(const Occlusion
& occlusion_in_layer_space
,
339 bool resourceless_software_draw
) {
343 void LayerImpl::GetContentsResourceId(ResourceProvider::ResourceId
* resource_id
,
344 gfx::Size
* resource_size
) const {
349 void LayerImpl::SetSentScrollDelta(const gfx::Vector2dF
& sent_scroll_delta
) {
350 // Pending tree never has sent scroll deltas
351 DCHECK(layer_tree_impl()->IsActiveTree());
353 if (sent_scroll_delta_
== sent_scroll_delta
)
356 sent_scroll_delta_
= sent_scroll_delta
;
359 gfx::Vector2dF
LayerImpl::ScrollBy(const gfx::Vector2dF
& scroll
) {
360 gfx::Vector2dF adjusted_scroll
= scroll
;
361 if (layer_tree_impl()->settings().use_pinch_virtual_viewport
) {
362 if (!user_scrollable_horizontal_
)
363 adjusted_scroll
.set_x(0);
364 if (!user_scrollable_vertical_
)
365 adjusted_scroll
.set_y(0);
367 DCHECK(scrollable());
368 gfx::Vector2dF min_delta
= -ScrollOffsetToVector2dF(scroll_offset_
);
369 gfx::Vector2dF max_delta
= MaxScrollOffset().DeltaFrom(scroll_offset_
);
370 // Clamp new_delta so that position + delta stays within scroll bounds.
371 gfx::Vector2dF new_delta
= (ScrollDelta() + adjusted_scroll
);
372 new_delta
.SetToMax(min_delta
);
373 new_delta
.SetToMin(max_delta
);
374 gfx::Vector2dF unscrolled
=
375 ScrollDelta() + scroll
- new_delta
;
376 SetScrollDelta(new_delta
);
381 void LayerImpl::SetScrollClipLayer(int scroll_clip_layer_id
) {
382 scroll_clip_layer_
= layer_tree_impl()->LayerById(scroll_clip_layer_id
);
385 bool LayerImpl::user_scrollable(ScrollbarOrientation orientation
) const {
386 return (orientation
== HORIZONTAL
) ? user_scrollable_horizontal_
387 : user_scrollable_vertical_
;
390 void LayerImpl::ApplySentScrollDeltasFromAbortedCommit() {
391 if (sent_scroll_delta_
.IsZero())
394 // Pending tree never has sent scroll deltas
395 DCHECK(layer_tree_impl()->IsActiveTree());
397 // The combination of pending tree and aborted commits with impl scrolls
398 // shouldn't happen; we don't know how to update its deltas correctly.
399 DCHECK(!layer_tree_impl()->FindPendingTreeLayerById(id()));
401 // Apply sent scroll deltas to scroll position / scroll delta as if the
402 // main thread had applied them and then committed those values.
403 SetScrollOffsetAndDelta(
404 scroll_offset_
+ gfx::ScrollOffset(sent_scroll_delta_
),
405 ScrollDelta() - sent_scroll_delta_
);
406 SetSentScrollDelta(gfx::Vector2dF());
409 void LayerImpl::ApplyScrollDeltasSinceBeginMainFrame() {
410 // Only the pending tree can have missing scrolls.
411 DCHECK(layer_tree_impl()->IsPendingTree());
415 // Pending tree should never have sent scroll deltas.
416 DCHECK(sent_scroll_delta().IsZero());
418 LayerImpl
* active_twin
= layer_tree_impl()->FindActiveTreeLayerById(id());
420 // Scrolls that happens after begin frame (where the sent scroll delta
421 // comes from) and commit need to be applied to the pending tree
422 // so that it is up to date with the total scroll.
423 SetScrollDelta(active_twin
->ScrollDelta() -
424 active_twin
->sent_scroll_delta());
428 InputHandler::ScrollStatus
LayerImpl::TryScroll(
429 const gfx::PointF
& screen_space_point
,
430 InputHandler::ScrollInputType type
) const {
431 if (should_scroll_on_main_thread()) {
432 TRACE_EVENT0("cc", "LayerImpl::TryScroll: Failed ShouldScrollOnMainThread");
433 return InputHandler::ScrollOnMainThread
;
436 if (!screen_space_transform().IsInvertible()) {
437 TRACE_EVENT0("cc", "LayerImpl::TryScroll: Ignored NonInvertibleTransform");
438 return InputHandler::ScrollIgnored
;
441 if (!non_fast_scrollable_region().IsEmpty()) {
442 bool clipped
= false;
443 gfx::Transform
inverse_screen_space_transform(
444 gfx::Transform::kSkipInitialization
);
445 if (!screen_space_transform().GetInverse(&inverse_screen_space_transform
)) {
446 // TODO(shawnsingh): We shouldn't be applying a projection if screen space
447 // transform is uninvertible here. Perhaps we should be returning
448 // ScrollOnMainThread in this case?
451 gfx::PointF hit_test_point_in_content_space
=
452 MathUtil::ProjectPoint(inverse_screen_space_transform
,
455 gfx::PointF hit_test_point_in_layer_space
=
456 gfx::ScalePoint(hit_test_point_in_content_space
,
457 1.f
/ contents_scale_x(),
458 1.f
/ contents_scale_y());
460 non_fast_scrollable_region().Contains(
461 gfx::ToRoundedPoint(hit_test_point_in_layer_space
))) {
463 "LayerImpl::tryScroll: Failed NonFastScrollableRegion");
464 return InputHandler::ScrollOnMainThread
;
468 if (type
== InputHandler::Wheel
&& have_wheel_event_handlers()) {
469 TRACE_EVENT0("cc", "LayerImpl::tryScroll: Failed WheelEventHandlers");
470 return InputHandler::ScrollOnMainThread
;
474 TRACE_EVENT0("cc", "LayerImpl::tryScroll: Ignored not scrollable");
475 return InputHandler::ScrollIgnored
;
478 gfx::ScrollOffset max_scroll_offset
= MaxScrollOffset();
479 if (max_scroll_offset
.x() <= 0 && max_scroll_offset
.y() <= 0) {
481 "LayerImpl::tryScroll: Ignored. Technically scrollable,"
482 " but has no affordance in either direction.");
483 return InputHandler::ScrollIgnored
;
486 return InputHandler::ScrollStarted
;
489 gfx::Rect
LayerImpl::LayerRectToContentRect(
490 const gfx::RectF
& layer_rect
) const {
491 gfx::RectF content_rect
=
492 gfx::ScaleRect(layer_rect
, contents_scale_x(), contents_scale_y());
493 // Intersect with content rect to avoid the extra pixel because for some
494 // values x and y, ceil((x / y) * y) may be x + 1.
495 content_rect
.Intersect(gfx::Rect(content_bounds()));
496 return gfx::ToEnclosingRect(content_rect
);
499 skia::RefPtr
<SkPicture
> LayerImpl::GetPicture() {
500 return skia::RefPtr
<SkPicture
>();
503 scoped_ptr
<LayerImpl
> LayerImpl::CreateLayerImpl(LayerTreeImpl
* tree_impl
) {
504 return LayerImpl::Create(tree_impl
, layer_id_
);
507 void LayerImpl::PushPropertiesTo(LayerImpl
* layer
) {
508 layer
->SetTransformOrigin(transform_origin_
);
509 layer
->SetBackgroundColor(background_color_
);
510 layer
->SetBounds(bounds_
);
511 layer
->SetContentBounds(content_bounds());
512 layer
->SetContentsScale(contents_scale_x(), contents_scale_y());
513 layer
->SetDoubleSided(double_sided_
);
514 layer
->SetDrawCheckerboardForMissingTiles(
515 draw_checkerboard_for_missing_tiles_
);
516 layer
->SetDrawsContent(DrawsContent());
517 layer
->SetHideLayerAndSubtree(hide_layer_and_subtree_
);
518 layer
->SetHasRenderSurface(!!render_surface());
519 layer
->SetFilters(filters());
520 layer
->SetBackgroundFilters(background_filters());
521 layer
->SetMasksToBounds(masks_to_bounds_
);
522 layer
->SetShouldScrollOnMainThread(should_scroll_on_main_thread_
);
523 layer
->SetHaveWheelEventHandlers(have_wheel_event_handlers_
);
524 layer
->SetHaveScrollEventHandlers(have_scroll_event_handlers_
);
525 layer
->SetNonFastScrollableRegion(non_fast_scrollable_region_
);
526 layer
->SetTouchEventHandlerRegion(touch_event_handler_region_
);
527 layer
->SetContentsOpaque(contents_opaque_
);
528 layer
->SetOpacity(opacity_
);
529 layer
->SetBlendMode(blend_mode_
);
530 layer
->SetIsRootForIsolatedGroup(is_root_for_isolated_group_
);
531 layer
->SetPosition(position_
);
532 layer
->SetIsContainerForFixedPositionLayers(
533 is_container_for_fixed_position_layers_
);
534 layer
->SetPositionConstraint(position_constraint_
);
535 layer
->SetShouldFlattenTransform(should_flatten_transform_
);
536 layer
->SetUseParentBackfaceVisibility(use_parent_backface_visibility_
);
537 layer
->SetTransformAndInvertibility(transform_
, transform_is_invertible_
);
539 layer
->SetScrollClipLayer(scroll_clip_layer_
? scroll_clip_layer_
->id()
540 : Layer::INVALID_ID
);
541 layer
->set_user_scrollable_horizontal(user_scrollable_horizontal_
);
542 layer
->set_user_scrollable_vertical(user_scrollable_vertical_
);
544 // Save the difference but clear the sent delta so that we don't subtract
545 // it again in SetScrollOffsetAndDelta's pending twin mirroring logic.
546 gfx::Vector2dF remaining_delta
=
547 layer
->ScrollDelta() - layer
->sent_scroll_delta();
548 layer
->SetSentScrollDelta(gfx::Vector2dF());
549 layer
->SetScrollOffsetAndDelta(scroll_offset_
, remaining_delta
);
551 layer
->Set3dSortingContextId(sorting_context_id_
);
552 layer
->SetNumDescendantsThatDrawContent(num_descendants_that_draw_content_
);
554 LayerImpl
* scroll_parent
= nullptr;
555 if (scroll_parent_
) {
556 scroll_parent
= layer
->layer_tree_impl()->LayerById(scroll_parent_
->id());
557 DCHECK(scroll_parent
);
560 layer
->SetScrollParent(scroll_parent
);
561 if (scroll_children_
) {
562 std::set
<LayerImpl
*>* scroll_children
= new std::set
<LayerImpl
*>;
563 for (std::set
<LayerImpl
*>::iterator it
= scroll_children_
->begin();
564 it
!= scroll_children_
->end();
566 DCHECK_EQ((*it
)->scroll_parent(), this);
567 LayerImpl
* scroll_child
=
568 layer
->layer_tree_impl()->LayerById((*it
)->id());
569 DCHECK(scroll_child
);
570 scroll_children
->insert(scroll_child
);
572 layer
->SetScrollChildren(scroll_children
);
574 layer
->SetScrollChildren(nullptr);
577 LayerImpl
* clip_parent
= nullptr;
579 clip_parent
= layer
->layer_tree_impl()->LayerById(
584 layer
->SetClipParent(clip_parent
);
585 if (clip_children_
) {
586 std::set
<LayerImpl
*>* clip_children
= new std::set
<LayerImpl
*>;
587 for (std::set
<LayerImpl
*>::iterator it
= clip_children_
->begin();
588 it
!= clip_children_
->end(); ++it
)
589 clip_children
->insert(layer
->layer_tree_impl()->LayerById((*it
)->id()));
590 layer
->SetClipChildren(clip_children
);
592 layer
->SetClipChildren(nullptr);
595 layer
->PassCopyRequests(©_requests_
);
597 // If the main thread commits multiple times before the impl thread actually
598 // draws, then damage tracking will become incorrect if we simply clobber the
599 // update_rect here. The LayerImpl's update_rect needs to accumulate (i.e.
600 // union) any update changes that have occurred on the main thread.
601 update_rect_
.Union(layer
->update_rect());
602 layer
->SetUpdateRect(update_rect_
);
604 layer
->SetStackingOrderChanged(stacking_order_changed_
);
605 layer
->SetDebugInfo(debug_info_
);
607 // Reset any state that should be cleared for the next update.
608 stacking_order_changed_
= false;
609 update_rect_
= gfx::Rect();
610 needs_push_properties_
= false;
611 num_dependents_need_push_properties_
= 0;
614 gfx::Vector2dF
LayerImpl::FixedContainerSizeDelta() const {
615 if (!scroll_clip_layer_
)
616 return gfx::Vector2dF();
618 gfx::Vector2dF delta_from_scroll
= scroll_clip_layer_
->bounds_delta();
620 // In virtual-viewport mode, we don't need to compensate for pinch zoom or
621 // scale since the fixed container is the outer viewport, which sits below
623 if (layer_tree_impl()->settings().use_pinch_virtual_viewport
)
624 return delta_from_scroll
;
626 float scale_delta
= layer_tree_impl()->page_scale_delta();
627 float scale
= layer_tree_impl()->current_page_scale_factor() /
628 layer_tree_impl()->page_scale_delta();
630 delta_from_scroll
.Scale(1.f
/ scale
);
632 // The delta-from-pinch component requires some explanation: A viewport of
633 // size (w,h) will appear to be size (w/s,h/s) under scale s in the content
634 // space. If s -> s' on the impl thread, where s' = s * ds, then the apparent
635 // viewport size change in the content space due to ds is:
637 // (w/s',h/s') - (w/s,h/s) = (w,h)(1/s' - 1/s) = (w,h)(1 - ds)/(s ds)
639 gfx::Vector2dF delta_from_pinch
=
640 gfx::Rect(scroll_clip_layer_
->bounds()).bottom_right() - gfx::PointF();
641 delta_from_pinch
.Scale((1.f
- scale_delta
) / (scale
* scale_delta
));
643 return delta_from_scroll
+ delta_from_pinch
;
646 base::DictionaryValue
* LayerImpl::LayerTreeAsJson() const {
647 base::DictionaryValue
* result
= new base::DictionaryValue
;
648 result
->SetString("LayerType", LayerTypeAsString());
650 base::ListValue
* list
= new base::ListValue
;
651 list
->AppendInteger(bounds().width());
652 list
->AppendInteger(bounds().height());
653 result
->Set("Bounds", list
);
655 list
= new base::ListValue
;
656 list
->AppendDouble(position_
.x());
657 list
->AppendDouble(position_
.y());
658 result
->Set("Position", list
);
660 const gfx::Transform
& gfx_transform
= draw_properties_
.target_space_transform
;
661 double transform
[16];
662 gfx_transform
.matrix().asColMajord(transform
);
663 list
= new base::ListValue
;
664 for (int i
= 0; i
< 16; ++i
)
665 list
->AppendDouble(transform
[i
]);
666 result
->Set("DrawTransform", list
);
668 result
->SetBoolean("DrawsContent", draws_content_
);
669 result
->SetBoolean("Is3dSorted", Is3dSorted());
670 result
->SetDouble("Opacity", opacity());
671 result
->SetBoolean("ContentsOpaque", contents_opaque_
);
674 result
->SetBoolean("Scrollable", true);
676 if (have_wheel_event_handlers_
)
677 result
->SetBoolean("WheelHandler", have_wheel_event_handlers_
);
678 if (have_scroll_event_handlers_
)
679 result
->SetBoolean("ScrollHandler", have_scroll_event_handlers_
);
680 if (!touch_event_handler_region_
.IsEmpty()) {
681 scoped_ptr
<base::Value
> region
= touch_event_handler_region_
.AsValue();
682 result
->Set("TouchRegion", region
.release());
685 list
= new base::ListValue
;
686 for (size_t i
= 0; i
< children_
.size(); ++i
)
687 list
->Append(children_
[i
]->LayerTreeAsJson());
688 result
->Set("Children", list
);
693 void LayerImpl::SetStackingOrderChanged(bool stacking_order_changed
) {
694 if (stacking_order_changed
) {
695 stacking_order_changed_
= true;
696 NoteLayerPropertyChangedForSubtree();
700 void LayerImpl::NoteLayerPropertyChanged() {
701 layer_property_changed_
= true;
702 layer_tree_impl()->set_needs_update_draw_properties();
703 SetNeedsPushProperties();
706 void LayerImpl::NoteLayerPropertyChangedForSubtree() {
707 layer_property_changed_
= true;
708 layer_tree_impl()->set_needs_update_draw_properties();
709 for (size_t i
= 0; i
< children_
.size(); ++i
)
710 children_
[i
]->NoteLayerPropertyChangedForDescendantsInternal();
711 SetNeedsPushProperties();
714 void LayerImpl::NoteLayerPropertyChangedForDescendantsInternal() {
715 layer_property_changed_
= true;
716 for (size_t i
= 0; i
< children_
.size(); ++i
)
717 children_
[i
]->NoteLayerPropertyChangedForDescendantsInternal();
720 void LayerImpl::NoteLayerPropertyChangedForDescendants() {
721 layer_tree_impl()->set_needs_update_draw_properties();
722 for (size_t i
= 0; i
< children_
.size(); ++i
)
723 children_
[i
]->NoteLayerPropertyChangedForDescendantsInternal();
724 SetNeedsPushProperties();
727 const char* LayerImpl::LayerTypeAsString() const {
728 return "cc::LayerImpl";
731 void LayerImpl::ResetAllChangeTrackingForSubtree() {
732 layer_property_changed_
= false;
734 update_rect_
= gfx::Rect();
735 damage_rect_
= gfx::RectF();
738 render_surface_
->ResetPropertyChangedFlag();
741 mask_layer_
->ResetAllChangeTrackingForSubtree();
743 if (replica_layer_
) {
744 // This also resets the replica mask, if it exists.
745 replica_layer_
->ResetAllChangeTrackingForSubtree();
748 for (size_t i
= 0; i
< children_
.size(); ++i
)
749 children_
[i
]->ResetAllChangeTrackingForSubtree();
751 needs_push_properties_
= false;
752 num_dependents_need_push_properties_
= 0;
755 gfx::ScrollOffset
LayerImpl::ScrollOffsetForAnimation() const {
756 return TotalScrollOffset();
759 void LayerImpl::OnFilterAnimated(const FilterOperations
& filters
) {
763 void LayerImpl::OnOpacityAnimated(float opacity
) {
767 void LayerImpl::OnTransformAnimated(const gfx::Transform
& transform
) {
768 SetTransform(transform
);
771 void LayerImpl::OnScrollOffsetAnimated(const gfx::ScrollOffset
& scroll_offset
) {
772 // Only layers in the active tree should need to do anything here, since
773 // layers in the pending tree will find out about these changes as a
774 // result of the call to SetScrollDelta.
778 SetScrollDelta(scroll_offset
.DeltaFrom(scroll_offset_
));
780 layer_tree_impl_
->DidAnimateScrollOffset();
783 void LayerImpl::OnAnimationWaitingForDeletion() {}
785 bool LayerImpl::IsActive() const {
786 return layer_tree_impl_
->IsActiveTree();
789 gfx::Size
LayerImpl::bounds() const {
790 gfx::Vector2d delta
= gfx::ToCeiledVector2d(bounds_delta_
);
791 return gfx::Size(bounds_
.width() + delta
.x(),
792 bounds_
.height() + delta
.y());
795 gfx::SizeF
LayerImpl::BoundsForScrolling() const {
796 return gfx::SizeF(bounds_
.width() + bounds_delta_
.x(),
797 bounds_
.height() + bounds_delta_
.y());
800 void LayerImpl::SetBounds(const gfx::Size
& bounds
) {
801 if (bounds_
== bounds
)
806 ScrollbarParametersDidChange(true);
807 if (masks_to_bounds())
808 NoteLayerPropertyChangedForSubtree();
810 NoteLayerPropertyChanged();
813 void LayerImpl::SetBoundsDelta(const gfx::Vector2dF
& bounds_delta
) {
814 if (bounds_delta_
== bounds_delta
)
817 bounds_delta_
= bounds_delta
;
819 ScrollbarParametersDidChange(true);
820 if (masks_to_bounds())
821 NoteLayerPropertyChangedForSubtree();
823 NoteLayerPropertyChanged();
826 void LayerImpl::SetMaskLayer(scoped_ptr
<LayerImpl
> mask_layer
) {
827 int new_layer_id
= mask_layer
? mask_layer
->id() : -1;
830 DCHECK_EQ(layer_tree_impl(), mask_layer
->layer_tree_impl());
831 DCHECK_NE(new_layer_id
, mask_layer_id_
);
832 } else if (new_layer_id
== mask_layer_id_
) {
836 mask_layer_
= mask_layer
.Pass();
837 mask_layer_id_
= new_layer_id
;
839 mask_layer_
->SetParent(this);
840 NoteLayerPropertyChangedForSubtree();
843 scoped_ptr
<LayerImpl
> LayerImpl::TakeMaskLayer() {
845 return mask_layer_
.Pass();
848 void LayerImpl::SetReplicaLayer(scoped_ptr
<LayerImpl
> replica_layer
) {
849 int new_layer_id
= replica_layer
? replica_layer
->id() : -1;
852 DCHECK_EQ(layer_tree_impl(), replica_layer
->layer_tree_impl());
853 DCHECK_NE(new_layer_id
, replica_layer_id_
);
854 } else if (new_layer_id
== replica_layer_id_
) {
858 replica_layer_
= replica_layer
.Pass();
859 replica_layer_id_
= new_layer_id
;
861 replica_layer_
->SetParent(this);
862 NoteLayerPropertyChangedForSubtree();
865 scoped_ptr
<LayerImpl
> LayerImpl::TakeReplicaLayer() {
866 replica_layer_id_
= -1;
867 return replica_layer_
.Pass();
870 ScrollbarLayerImplBase
* LayerImpl::ToScrollbarLayer() {
874 void LayerImpl::SetDrawsContent(bool draws_content
) {
875 if (draws_content_
== draws_content
)
878 draws_content_
= draws_content
;
879 NoteLayerPropertyChanged();
882 void LayerImpl::SetHideLayerAndSubtree(bool hide
) {
883 if (hide_layer_and_subtree_
== hide
)
886 hide_layer_and_subtree_
= hide
;
887 NoteLayerPropertyChangedForSubtree();
890 void LayerImpl::SetTransformOrigin(const gfx::Point3F
& transform_origin
) {
891 if (transform_origin_
== transform_origin
)
893 transform_origin_
= transform_origin
;
894 NoteLayerPropertyChangedForSubtree();
897 void LayerImpl::SetBackgroundColor(SkColor background_color
) {
898 if (background_color_
== background_color
)
901 background_color_
= background_color
;
902 NoteLayerPropertyChanged();
905 SkColor
LayerImpl::SafeOpaqueBackgroundColor() const {
906 SkColor color
= background_color();
907 if (SkColorGetA(color
) == 255 && !contents_opaque()) {
908 color
= SK_ColorTRANSPARENT
;
909 } else if (SkColorGetA(color
) != 255 && contents_opaque()) {
910 for (const LayerImpl
* layer
= parent(); layer
;
911 layer
= layer
->parent()) {
912 color
= layer
->background_color();
913 if (SkColorGetA(color
) == 255)
916 if (SkColorGetA(color
) != 255)
917 color
= layer_tree_impl()->background_color();
918 if (SkColorGetA(color
) != 255)
919 color
= SkColorSetA(color
, 255);
924 void LayerImpl::SetFilters(const FilterOperations
& filters
) {
925 if (filters_
== filters
)
929 NoteLayerPropertyChangedForSubtree();
932 bool LayerImpl::FilterIsAnimating() const {
933 return layer_animation_controller_
->IsAnimatingProperty(Animation::Filter
);
936 bool LayerImpl::FilterIsAnimatingOnImplOnly() const {
937 Animation
* filter_animation
=
938 layer_animation_controller_
->GetAnimation(Animation::Filter
);
939 return filter_animation
&& filter_animation
->is_impl_only();
942 void LayerImpl::SetBackgroundFilters(
943 const FilterOperations
& filters
) {
944 if (background_filters_
== filters
)
947 background_filters_
= filters
;
948 NoteLayerPropertyChanged();
951 void LayerImpl::SetMasksToBounds(bool masks_to_bounds
) {
952 if (masks_to_bounds_
== masks_to_bounds
)
955 masks_to_bounds_
= masks_to_bounds
;
956 NoteLayerPropertyChangedForSubtree();
959 void LayerImpl::SetContentsOpaque(bool opaque
) {
960 if (contents_opaque_
== opaque
)
963 contents_opaque_
= opaque
;
964 NoteLayerPropertyChangedForSubtree();
967 void LayerImpl::SetOpacity(float opacity
) {
968 if (opacity_
== opacity
)
972 NoteLayerPropertyChangedForSubtree();
975 bool LayerImpl::OpacityIsAnimating() const {
976 return layer_animation_controller_
->IsAnimatingProperty(Animation::Opacity
);
979 bool LayerImpl::OpacityIsAnimatingOnImplOnly() const {
980 Animation
* opacity_animation
=
981 layer_animation_controller_
->GetAnimation(Animation::Opacity
);
982 return opacity_animation
&& opacity_animation
->is_impl_only();
985 void LayerImpl::SetBlendMode(SkXfermode::Mode blend_mode
) {
986 if (blend_mode_
== blend_mode
)
989 blend_mode_
= blend_mode
;
990 NoteLayerPropertyChangedForSubtree();
993 void LayerImpl::SetIsRootForIsolatedGroup(bool root
) {
994 if (is_root_for_isolated_group_
== root
)
997 is_root_for_isolated_group_
= root
;
998 SetNeedsPushProperties();
1001 void LayerImpl::SetPosition(const gfx::PointF
& position
) {
1002 if (position_
== position
)
1005 position_
= position
;
1006 NoteLayerPropertyChangedForSubtree();
1009 void LayerImpl::SetShouldFlattenTransform(bool flatten
) {
1010 if (should_flatten_transform_
== flatten
)
1013 should_flatten_transform_
= flatten
;
1014 NoteLayerPropertyChangedForSubtree();
1017 void LayerImpl::Set3dSortingContextId(int id
) {
1018 if (id
== sorting_context_id_
)
1020 sorting_context_id_
= id
;
1021 NoteLayerPropertyChangedForSubtree();
1024 void LayerImpl::SetTransform(const gfx::Transform
& transform
) {
1025 if (transform_
== transform
)
1028 transform_
= transform
;
1029 transform_is_invertible_
= transform_
.IsInvertible();
1030 NoteLayerPropertyChangedForSubtree();
1033 void LayerImpl::SetTransformAndInvertibility(const gfx::Transform
& transform
,
1034 bool transform_is_invertible
) {
1035 if (transform_
== transform
) {
1036 DCHECK(transform_is_invertible_
== transform_is_invertible
)
1037 << "Can't change invertibility if transform is unchanged";
1040 transform_
= transform
;
1041 transform_is_invertible_
= transform_is_invertible
;
1042 NoteLayerPropertyChangedForSubtree();
1045 bool LayerImpl::TransformIsAnimating() const {
1046 return layer_animation_controller_
->IsAnimatingProperty(Animation::Transform
);
1049 bool LayerImpl::TransformIsAnimatingOnImplOnly() const {
1050 Animation
* transform_animation
=
1051 layer_animation_controller_
->GetAnimation(Animation::Transform
);
1052 return transform_animation
&& transform_animation
->is_impl_only();
1055 void LayerImpl::SetUpdateRect(const gfx::Rect
& update_rect
) {
1056 update_rect_
= update_rect
;
1057 SetNeedsPushProperties();
1060 void LayerImpl::AddDamageRect(const gfx::RectF
& damage_rect
) {
1061 damage_rect_
= gfx::UnionRects(damage_rect_
, damage_rect
);
1064 void LayerImpl::SetContentBounds(const gfx::Size
& content_bounds
) {
1065 if (this->content_bounds() == content_bounds
)
1068 draw_properties_
.content_bounds
= content_bounds
;
1069 NoteLayerPropertyChanged();
1072 void LayerImpl::SetContentsScale(float contents_scale_x
,
1073 float contents_scale_y
) {
1074 if (this->contents_scale_x() == contents_scale_x
&&
1075 this->contents_scale_y() == contents_scale_y
)
1078 draw_properties_
.contents_scale_x
= contents_scale_x
;
1079 draw_properties_
.contents_scale_y
= contents_scale_y
;
1080 NoteLayerPropertyChanged();
1083 void LayerImpl::SetScrollOffsetDelegate(
1084 ScrollOffsetDelegate
* scroll_offset_delegate
) {
1085 // Having both a scroll parent and a scroll offset delegate is unsupported.
1086 DCHECK(!scroll_parent_
);
1087 if (!scroll_offset_delegate
&& scroll_offset_delegate_
) {
1088 scroll_delta_
= scroll_offset_delegate_
->GetTotalScrollOffset().DeltaFrom(
1091 gfx::ScrollOffset total_offset
= TotalScrollOffset();
1092 scroll_offset_delegate_
= scroll_offset_delegate
;
1093 if (scroll_offset_delegate_
)
1094 scroll_offset_delegate_
->SetTotalScrollOffset(total_offset
);
1097 bool LayerImpl::IsExternalFlingActive() const {
1098 return scroll_offset_delegate_
&&
1099 scroll_offset_delegate_
->IsExternalFlingActive();
1102 void LayerImpl::DidScroll() {
1103 NoteLayerPropertyChangedForSubtree();
1104 ScrollbarParametersDidChange(false);
1107 void LayerImpl::SetScrollOffset(const gfx::ScrollOffset
& scroll_offset
) {
1108 SetScrollOffsetAndDelta(scroll_offset
, ScrollDelta());
1111 void LayerImpl::SetScrollOffsetAndDelta(const gfx::ScrollOffset
& scroll_offset
,
1112 const gfx::Vector2dF
& scroll_delta
) {
1113 bool changed
= false;
1115 last_scroll_offset_
= scroll_offset
;
1117 if (scroll_offset_
!= scroll_offset
) {
1119 scroll_offset_
= scroll_offset
;
1121 if (scroll_offset_delegate_
)
1122 scroll_offset_delegate_
->SetTotalScrollOffset(TotalScrollOffset());
1125 if (ScrollDelta() != scroll_delta
) {
1127 if (layer_tree_impl()->IsActiveTree()) {
1128 LayerImpl
* pending_twin
=
1129 layer_tree_impl()->FindPendingTreeLayerById(id());
1131 // The pending twin can't mirror the scroll delta of the active
1132 // layer. Although the delta - sent scroll delta difference is
1133 // identical for both twins, the sent scroll delta for the pending
1134 // layer is zero, as anything that has been sent has been baked
1135 // into the layer's position/scroll offset as a part of commit.
1136 DCHECK(pending_twin
->sent_scroll_delta().IsZero());
1137 pending_twin
->SetScrollDelta(scroll_delta
- sent_scroll_delta());
1141 if (scroll_offset_delegate_
) {
1142 scroll_offset_delegate_
->SetTotalScrollOffset(
1143 ScrollOffsetWithDelta(scroll_offset_
, scroll_delta
));
1145 scroll_delta_
= scroll_delta
;
1150 if (scroll_offset_delegate_
)
1151 scroll_offset_delegate_
->Update();
1156 gfx::Vector2dF
LayerImpl::ScrollDelta() const {
1157 if (scroll_offset_delegate_
) {
1158 return scroll_offset_delegate_
->GetTotalScrollOffset().DeltaFrom(
1161 return scroll_delta_
;
1164 void LayerImpl::SetScrollDelta(const gfx::Vector2dF
& scroll_delta
) {
1165 SetScrollOffsetAndDelta(scroll_offset_
, scroll_delta
);
1168 gfx::ScrollOffset
LayerImpl::TotalScrollOffset() const {
1169 return ScrollOffsetWithDelta(scroll_offset_
, ScrollDelta());
1172 void LayerImpl::SetDoubleSided(bool double_sided
) {
1173 if (double_sided_
== double_sided
)
1176 double_sided_
= double_sided
;
1177 NoteLayerPropertyChangedForSubtree();
1180 SimpleEnclosedRegion
LayerImpl::VisibleContentOpaqueRegion() const {
1181 if (contents_opaque())
1182 return SimpleEnclosedRegion(visible_content_rect());
1183 return SimpleEnclosedRegion();
1186 void LayerImpl::DidBeginTracing() {}
1188 void LayerImpl::ReleaseResources() {}
1190 gfx::ScrollOffset
LayerImpl::MaxScrollOffset() const {
1191 if (!scroll_clip_layer_
|| bounds().IsEmpty())
1192 return gfx::ScrollOffset();
1194 LayerImpl
const* page_scale_layer
= layer_tree_impl()->page_scale_layer();
1195 DCHECK(this != page_scale_layer
);
1196 DCHECK(this != layer_tree_impl()->InnerViewportScrollLayer() ||
1197 IsContainerForFixedPositionLayers());
1199 gfx::SizeF
scaled_scroll_bounds(BoundsForScrolling());
1201 float scale_factor
= 1.f
;
1202 for (LayerImpl
const* current_layer
= this;
1203 current_layer
!= scroll_clip_layer_
;
1204 current_layer
= current_layer
->parent()) {
1205 DCHECK(current_layer
);
1206 float current_layer_scale
= 1.f
;
1208 const gfx::Transform
& layer_transform
= current_layer
->transform();
1209 if (current_layer
== page_scale_layer
) {
1210 DCHECK(layer_transform
.IsIdentity());
1211 current_layer_scale
= layer_tree_impl()->current_page_scale_factor();
1213 // TODO(wjmaclean) Should we allow for translation too?
1214 DCHECK(layer_transform
.IsScale2d());
1215 gfx::Vector2dF layer_scale
= layer_transform
.Scale2d();
1216 // TODO(wjmaclean) Allow for non-isotropic scales.
1217 DCHECK(layer_scale
.x() == layer_scale
.y());
1218 current_layer_scale
= layer_scale
.x();
1221 scale_factor
*= current_layer_scale
;
1223 // TODO(wjmaclean) Once we move to a model where the two-viewport model is
1224 // turned on in all builds, remove the next two lines. For now however, the
1225 // page scale layer may coincide with the clip layer, and so this is
1227 if (page_scale_layer
== scroll_clip_layer_
)
1228 scale_factor
*= layer_tree_impl()->current_page_scale_factor();
1230 scaled_scroll_bounds
.SetSize(scale_factor
* scaled_scroll_bounds
.width(),
1231 scale_factor
* scaled_scroll_bounds
.height());
1232 scaled_scroll_bounds
= gfx::ToFlooredSize(scaled_scroll_bounds
);
1234 gfx::ScrollOffset
max_offset(
1235 scaled_scroll_bounds
.width() - scroll_clip_layer_
->bounds().width(),
1236 scaled_scroll_bounds
.height() - scroll_clip_layer_
->bounds().height());
1237 // We need the final scroll offset to be in CSS coords.
1238 max_offset
.Scale(1 / scale_factor
);
1239 max_offset
.SetToMax(gfx::ScrollOffset());
1243 gfx::Vector2dF
LayerImpl::ClampScrollToMaxScrollOffset() {
1244 gfx::ScrollOffset max_offset
= MaxScrollOffset();
1245 gfx::ScrollOffset old_offset
= TotalScrollOffset();
1246 gfx::ScrollOffset clamped_offset
= old_offset
;
1248 clamped_offset
.SetToMin(max_offset
);
1249 clamped_offset
.SetToMax(gfx::ScrollOffset());
1250 gfx::Vector2dF delta
= clamped_offset
.DeltaFrom(old_offset
);
1251 if (!delta
.IsZero())
1257 void LayerImpl::SetScrollbarPosition(ScrollbarLayerImplBase
* scrollbar_layer
,
1258 LayerImpl
* scrollbar_clip_layer
,
1259 bool on_resize
) const {
1260 DCHECK(scrollbar_layer
);
1261 LayerImpl
* page_scale_layer
= layer_tree_impl()->page_scale_layer();
1263 DCHECK(this != page_scale_layer
);
1264 DCHECK(scrollbar_clip_layer
);
1265 gfx::RectF
clip_rect(gfx::PointF(),
1266 scrollbar_clip_layer
->BoundsForScrolling());
1268 // See comment in MaxScrollOffset() regarding the use of the content layer
1270 gfx::RectF
scroll_rect(gfx::PointF(), BoundsForScrolling());
1272 if (scroll_rect
.size().IsEmpty())
1275 // TODO(wjmaclean) This computation is nearly identical to the one in
1276 // MaxScrollOffset. Find some way to combine these.
1277 gfx::ScrollOffset current_offset
;
1278 for (LayerImpl
const* current_layer
= this;
1279 current_layer
!= scrollbar_clip_layer
;
1280 current_layer
= current_layer
->parent()) {
1281 DCHECK(current_layer
);
1282 const gfx::Transform
& layer_transform
= current_layer
->transform();
1283 if (current_layer
== page_scale_layer
) {
1284 DCHECK(layer_transform
.IsIdentity());
1285 float scale_factor
= layer_tree_impl()->current_page_scale_factor();
1286 current_offset
.Scale(scale_factor
);
1287 scroll_rect
.Scale(scale_factor
);
1289 DCHECK(layer_transform
.IsScale2d());
1290 gfx::Vector2dF layer_scale
= layer_transform
.Scale2d();
1291 DCHECK(layer_scale
.x() == layer_scale
.y());
1292 gfx::ScrollOffset new_offset
= ScrollOffsetWithDelta(
1293 current_layer
->scroll_offset(), current_layer
->ScrollDelta());
1294 new_offset
.Scale(layer_scale
.x(), layer_scale
.y());
1295 current_offset
+= new_offset
;
1298 // TODO(wjmaclean) Once we move to a model where the two-viewport model is
1299 // turned on in all builds, remove the next two lines. For now however, the
1300 // page scale layer may coincide with the clip layer, and so this is
1302 if (page_scale_layer
== scrollbar_clip_layer
) {
1303 scroll_rect
.Scale(layer_tree_impl()->current_page_scale_factor());
1304 current_offset
.Scale(layer_tree_impl()->current_page_scale_factor());
1307 bool scrollbar_needs_animation
= false;
1308 scrollbar_needs_animation
|= scrollbar_layer
->SetVerticalAdjust(
1309 scrollbar_clip_layer
->bounds_delta().y());
1310 if (scrollbar_layer
->orientation() == HORIZONTAL
) {
1311 float visible_ratio
= clip_rect
.width() / scroll_rect
.width();
1312 scrollbar_needs_animation
|=
1313 scrollbar_layer
->SetCurrentPos(current_offset
.x());
1314 scrollbar_needs_animation
|=
1315 scrollbar_layer
->SetMaximum(scroll_rect
.width() - clip_rect
.width());
1316 scrollbar_needs_animation
|=
1317 scrollbar_layer
->SetVisibleToTotalLengthRatio(visible_ratio
);
1319 float visible_ratio
= clip_rect
.height() / scroll_rect
.height();
1320 scrollbar_needs_animation
|=
1321 scrollbar_layer
->SetCurrentPos(current_offset
.y());
1322 scrollbar_needs_animation
|=
1323 scrollbar_layer
->SetMaximum(scroll_rect
.height() - clip_rect
.height());
1324 scrollbar_needs_animation
|=
1325 scrollbar_layer
->SetVisibleToTotalLengthRatio(visible_ratio
);
1327 if (scrollbar_needs_animation
) {
1328 layer_tree_impl()->set_needs_update_draw_properties();
1329 // TODO(wjmaclean) The scrollbar animator for the pinch-zoom scrollbars
1330 // should activate for every scroll on the main frame, not just the
1331 // scrolls that move the pinch virtual viewport (i.e. trigger from
1332 // either inner or outer viewport).
1333 if (scrollbar_animation_controller_
) {
1334 // When both non-overlay and overlay scrollbars are both present, don't
1335 // animate the overlay scrollbars when page scale factor is at the min.
1336 // Non-overlay scrollbars also shouldn't trigger animations.
1337 bool is_animatable_scrollbar
=
1338 scrollbar_layer
->is_overlay_scrollbar() &&
1339 ((layer_tree_impl()->current_page_scale_factor() >
1340 layer_tree_impl()->min_page_scale_factor()) ||
1341 !layer_tree_impl()->settings().use_pinch_zoom_scrollbars
);
1342 if (is_animatable_scrollbar
)
1343 scrollbar_animation_controller_
->DidScrollUpdate(on_resize
);
1348 void LayerImpl::DidBecomeActive() {
1349 if (layer_tree_impl_
->settings().scrollbar_animator
==
1350 LayerTreeSettings::NoAnimator
) {
1354 bool need_scrollbar_animation_controller
= scrollable() && scrollbars_
;
1355 if (!need_scrollbar_animation_controller
) {
1356 scrollbar_animation_controller_
= nullptr;
1360 if (scrollbar_animation_controller_
)
1363 scrollbar_animation_controller_
=
1364 layer_tree_impl_
->CreateScrollbarAnimationController(this);
1367 void LayerImpl::ClearScrollbars() {
1371 scrollbars_
.reset(nullptr);
1374 void LayerImpl::AddScrollbar(ScrollbarLayerImplBase
* layer
) {
1376 DCHECK(!scrollbars_
|| scrollbars_
->find(layer
) == scrollbars_
->end());
1378 scrollbars_
.reset(new ScrollbarSet());
1380 scrollbars_
->insert(layer
);
1383 void LayerImpl::RemoveScrollbar(ScrollbarLayerImplBase
* layer
) {
1384 DCHECK(scrollbars_
);
1386 DCHECK(scrollbars_
->find(layer
) != scrollbars_
->end());
1388 scrollbars_
->erase(layer
);
1389 if (scrollbars_
->empty())
1390 scrollbars_
= nullptr;
1393 bool LayerImpl::HasScrollbar(ScrollbarOrientation orientation
) const {
1397 for (ScrollbarSet::iterator it
= scrollbars_
->begin();
1398 it
!= scrollbars_
->end();
1400 if ((*it
)->orientation() == orientation
)
1406 void LayerImpl::ScrollbarParametersDidChange(bool on_resize
) {
1410 for (ScrollbarSet::iterator it
= scrollbars_
->begin();
1411 it
!= scrollbars_
->end();
1413 bool is_scroll_layer
= (*it
)->ScrollLayerId() == layer_id_
;
1414 bool scroll_layer_resized
= is_scroll_layer
&& on_resize
;
1415 (*it
)->ScrollbarParametersDidChange(scroll_layer_resized
);
1419 void LayerImpl::SetNeedsPushProperties() {
1420 if (needs_push_properties_
)
1422 if (!parent_should_know_need_push_properties() && parent_
)
1423 parent_
->AddDependentNeedsPushProperties();
1424 needs_push_properties_
= true;
1427 void LayerImpl::AddDependentNeedsPushProperties() {
1428 DCHECK_GE(num_dependents_need_push_properties_
, 0);
1430 if (!parent_should_know_need_push_properties() && parent_
)
1431 parent_
->AddDependentNeedsPushProperties();
1433 num_dependents_need_push_properties_
++;
1436 void LayerImpl::RemoveDependentNeedsPushProperties() {
1437 num_dependents_need_push_properties_
--;
1438 DCHECK_GE(num_dependents_need_push_properties_
, 0);
1440 if (!parent_should_know_need_push_properties() && parent_
)
1441 parent_
->RemoveDependentNeedsPushProperties();
1444 void LayerImpl::GetAllTilesForTracing(std::set
<const Tile
*>* tiles
) const {
1447 void LayerImpl::AsValueInto(base::debug::TracedValue
* state
) const {
1448 TracedValue::MakeDictIntoImplicitSnapshotWithCategory(
1449 TRACE_DISABLED_BY_DEFAULT("cc.debug"),
1452 LayerTypeAsString(),
1454 state
->SetInteger("layer_id", id());
1455 state
->BeginDictionary("bounds");
1456 MathUtil::AddToTracedValue(bounds_
, state
);
1457 state
->EndDictionary();
1459 state
->SetDouble("opacity", opacity());
1461 state
->BeginArray("position");
1462 MathUtil::AddToTracedValue(position_
, state
);
1465 state
->SetInteger("draws_content", DrawsContent());
1466 state
->SetInteger("gpu_memory_usage", GPUMemoryUsageInBytes());
1468 state
->BeginArray("scroll_offset");
1469 MathUtil::AddToTracedValue(scroll_offset_
, state
);
1472 state
->BeginArray("transform_origin");
1473 MathUtil::AddToTracedValue(transform_origin_
, state
);
1477 gfx::QuadF layer_quad
= MathUtil::MapQuad(
1478 screen_space_transform(),
1479 gfx::QuadF(gfx::Rect(content_bounds())),
1481 state
->BeginArray("layer_quad");
1482 MathUtil::AddToTracedValue(layer_quad
, state
);
1484 if (!touch_event_handler_region_
.IsEmpty()) {
1485 state
->BeginArray("touch_event_handler_region");
1486 touch_event_handler_region_
.AsValueInto(state
);
1489 if (have_wheel_event_handlers_
) {
1490 gfx::Rect
wheel_rect(content_bounds());
1491 Region
wheel_region(wheel_rect
);
1492 state
->BeginArray("wheel_event_handler_region");
1493 wheel_region
.AsValueInto(state
);
1496 if (have_scroll_event_handlers_
) {
1497 gfx::Rect
scroll_rect(content_bounds());
1498 Region
scroll_region(scroll_rect
);
1499 state
->BeginArray("scroll_event_handler_region");
1500 scroll_region
.AsValueInto(state
);
1503 if (!non_fast_scrollable_region_
.IsEmpty()) {
1504 state
->BeginArray("non_fast_scrollable_region");
1505 non_fast_scrollable_region_
.AsValueInto(state
);
1509 state
->BeginArray("children");
1510 for (size_t i
= 0; i
< children_
.size(); ++i
) {
1511 state
->BeginDictionary();
1512 children_
[i
]->AsValueInto(state
);
1513 state
->EndDictionary();
1517 state
->BeginDictionary("mask_layer");
1518 mask_layer_
->AsValueInto(state
);
1519 state
->EndDictionary();
1521 if (replica_layer_
) {
1522 state
->BeginDictionary("replica_layer");
1523 replica_layer_
->AsValueInto(state
);
1524 state
->EndDictionary();
1528 state
->SetInteger("scroll_parent", scroll_parent_
->id());
1531 state
->SetInteger("clip_parent", clip_parent_
->id());
1533 state
->SetBoolean("can_use_lcd_text", can_use_lcd_text());
1534 state
->SetBoolean("contents_opaque", contents_opaque());
1537 "has_animation_bounds",
1538 layer_animation_controller()->HasAnimationThatInflatesBounds());
1541 if (LayerUtils::GetAnimationBounds(*this, &box
)) {
1542 state
->BeginArray("animation_bounds");
1543 MathUtil::AddToTracedValue(box
, state
);
1547 if (debug_info_
.get()) {
1549 debug_info_
->AppendAsTraceFormat(&str
);
1550 base::JSONReader json_reader
;
1551 scoped_ptr
<base::Value
> debug_info_value(json_reader
.ReadToValue(str
));
1553 if (debug_info_value
->IsType(base::Value::TYPE_DICTIONARY
)) {
1554 base::DictionaryValue
* dictionary_value
= nullptr;
1555 bool converted_to_dictionary
=
1556 debug_info_value
->GetAsDictionary(&dictionary_value
);
1557 DCHECK(converted_to_dictionary
);
1558 for (base::DictionaryValue::Iterator
it(*dictionary_value
); !it
.IsAtEnd();
1560 state
->SetValue(it
.key().data(), it
.value().DeepCopy());
1568 bool LayerImpl::IsDrawnRenderSurfaceLayerListMember() const {
1569 return draw_properties_
.last_drawn_render_surface_layer_list_id
==
1570 layer_tree_impl_
->current_render_surface_list_id();
1573 size_t LayerImpl::GPUMemoryUsageInBytes() const { return 0; }
1575 void LayerImpl::RunMicroBenchmark(MicroBenchmarkImpl
* benchmark
) {
1576 benchmark
->RunOnLayer(this);
1579 int LayerImpl::NumDescendantsThatDrawContent() const {
1580 return num_descendants_that_draw_content_
;
1583 void LayerImpl::NotifyAnimationFinished(
1584 base::TimeTicks monotonic_time
,
1585 Animation::TargetProperty target_property
,
1587 if (target_property
== Animation::ScrollOffset
)
1588 layer_tree_impl_
->InputScrollAnimationFinished();
1591 void LayerImpl::SetHasRenderSurface(bool should_have_render_surface
) {
1592 if (!!render_surface() == should_have_render_surface
)
1595 SetNeedsPushProperties();
1596 layer_tree_impl()->set_needs_update_draw_properties();
1597 if (should_have_render_surface
) {
1598 render_surface_
= make_scoped_ptr(new RenderSurfaceImpl(this));
1601 render_surface_
.reset();