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/box_f.h"
30 #include "ui/gfx/geometry/vector2d_conversions.h"
31 #include "ui/gfx/point_conversions.h"
32 #include "ui/gfx/quad_f.h"
33 #include "ui/gfx/rect_conversions.h"
34 #include "ui/gfx/size_conversions.h"
37 LayerImpl::LayerImpl(LayerTreeImpl
* tree_impl
, int id
)
42 replica_layer_id_(-1),
44 layer_tree_impl_(tree_impl
),
45 scroll_offset_delegate_(NULL
),
46 scroll_clip_layer_(NULL
),
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 force_render_surface_(false),
64 transform_is_invertible_(true),
65 is_container_for_fixed_position_layers_(false),
68 blend_mode_(SkXfermode::kSrcOver_Mode
),
69 num_descendants_that_draw_content_(0),
71 needs_push_properties_(false),
72 num_dependents_need_push_properties_(0),
73 sorting_context_id_(0),
74 current_draw_mode_(DRAW_MODE_NONE
) {
75 DCHECK_GT(layer_id_
, 0);
76 DCHECK(layer_tree_impl_
);
77 layer_tree_impl_
->RegisterLayer(this);
78 AnimationRegistrar
* registrar
= layer_tree_impl_
->animationRegistrar();
79 layer_animation_controller_
=
80 registrar
->GetAnimationControllerForId(layer_id_
);
81 layer_animation_controller_
->AddValueObserver(this);
83 layer_animation_controller_
->set_value_provider(this);
84 layer_animation_controller_
->set_layer_animation_delegate(this);
86 SetNeedsPushProperties();
89 LayerImpl::~LayerImpl() {
90 DCHECK_EQ(DRAW_MODE_NONE
, current_draw_mode_
);
92 layer_animation_controller_
->RemoveValueObserver(this);
93 layer_animation_controller_
->remove_value_provider(this);
94 layer_animation_controller_
->remove_layer_animation_delegate(this);
96 if (!copy_requests_
.empty() && layer_tree_impl_
->IsActiveTree())
97 layer_tree_impl()->RemoveLayerWithCopyOutputRequest(this);
98 layer_tree_impl_
->UnregisterLayer(this);
100 TRACE_EVENT_OBJECT_DELETED_WITH_ID(
101 TRACE_DISABLED_BY_DEFAULT("cc.debug"), "cc::LayerImpl", this);
104 void LayerImpl::AddChild(scoped_ptr
<LayerImpl
> child
) {
105 child
->SetParent(this);
106 DCHECK_EQ(layer_tree_impl(), child
->layer_tree_impl());
107 children_
.push_back(child
.Pass());
108 layer_tree_impl()->set_needs_update_draw_properties();
111 scoped_ptr
<LayerImpl
> LayerImpl::RemoveChild(LayerImpl
* child
) {
112 for (OwnedLayerImplList::iterator it
= children_
.begin();
113 it
!= children_
.end();
116 scoped_ptr
<LayerImpl
> ret
= children_
.take(it
);
118 layer_tree_impl()->set_needs_update_draw_properties();
125 void LayerImpl::SetParent(LayerImpl
* parent
) {
126 if (parent_should_know_need_push_properties()) {
128 parent_
->RemoveDependentNeedsPushProperties();
130 parent
->AddDependentNeedsPushProperties();
135 void LayerImpl::ClearChildList() {
136 if (children_
.empty())
140 layer_tree_impl()->set_needs_update_draw_properties();
143 bool LayerImpl::HasAncestor(const LayerImpl
* ancestor
) const {
147 for (const LayerImpl
* layer
= this; layer
; layer
= layer
->parent()) {
148 if (layer
== ancestor
)
155 void LayerImpl::SetScrollParent(LayerImpl
* parent
) {
156 if (scroll_parent_
== parent
)
159 // Having both a scroll parent and a scroll offset delegate is unsupported.
160 DCHECK(!scroll_offset_delegate_
);
163 DCHECK_EQ(layer_tree_impl()->LayerById(parent
->id()), parent
);
165 scroll_parent_
= parent
;
166 SetNeedsPushProperties();
169 void LayerImpl::SetDebugInfo(
170 scoped_refptr
<base::debug::ConvertableToTraceFormat
> other
) {
172 SetNeedsPushProperties();
175 void LayerImpl::SetScrollChildren(std::set
<LayerImpl
*>* children
) {
176 if (scroll_children_
.get() == children
)
178 scroll_children_
.reset(children
);
179 SetNeedsPushProperties();
182 void LayerImpl::SetNumDescendantsThatDrawContent(int num_descendants
) {
183 if (num_descendants_that_draw_content_
== num_descendants
)
185 num_descendants_that_draw_content_
= num_descendants
;
186 SetNeedsPushProperties();
189 void LayerImpl::SetClipParent(LayerImpl
* ancestor
) {
190 if (clip_parent_
== ancestor
)
193 clip_parent_
= ancestor
;
194 SetNeedsPushProperties();
197 void LayerImpl::SetClipChildren(std::set
<LayerImpl
*>* children
) {
198 if (clip_children_
.get() == children
)
200 clip_children_
.reset(children
);
201 SetNeedsPushProperties();
204 void LayerImpl::PassCopyRequests(ScopedPtrVector
<CopyOutputRequest
>* requests
) {
205 if (requests
->empty())
208 bool was_empty
= copy_requests_
.empty();
209 copy_requests_
.insert_and_take(copy_requests_
.end(), requests
);
212 if (was_empty
&& layer_tree_impl()->IsActiveTree())
213 layer_tree_impl()->AddLayerWithCopyOutputRequest(this);
214 NoteLayerPropertyChangedForSubtree();
217 void LayerImpl::TakeCopyRequestsAndTransformToTarget(
218 ScopedPtrVector
<CopyOutputRequest
>* requests
) {
219 DCHECK(!copy_requests_
.empty());
220 DCHECK(layer_tree_impl()->IsActiveTree());
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::CreateRenderSurface() {
242 DCHECK(!draw_properties_
.render_surface
);
243 draw_properties_
.render_surface
=
244 make_scoped_ptr(new RenderSurfaceImpl(this));
245 draw_properties_
.render_target
= this;
248 void LayerImpl::ClearRenderSurface() {
249 draw_properties_
.render_surface
= nullptr;
252 void LayerImpl::ClearRenderSurfaceLayerList() {
253 if (draw_properties_
.render_surface
)
254 draw_properties_
.render_surface
->layer_list().clear();
257 void LayerImpl::PopulateSharedQuadState(SharedQuadState
* state
) const {
258 state
->SetAll(draw_properties_
.target_space_transform
,
259 draw_properties_
.content_bounds
,
260 draw_properties_
.visible_content_rect
,
261 draw_properties_
.clip_rect
,
262 draw_properties_
.is_clipped
,
263 draw_properties_
.opacity
,
265 sorting_context_id_
);
268 bool LayerImpl::WillDraw(DrawMode draw_mode
,
269 ResourceProvider
* resource_provider
) {
270 // WillDraw/DidDraw must be matched.
271 DCHECK_NE(DRAW_MODE_NONE
, draw_mode
);
272 DCHECK_EQ(DRAW_MODE_NONE
, current_draw_mode_
);
273 current_draw_mode_
= draw_mode
;
277 void LayerImpl::DidDraw(ResourceProvider
* resource_provider
) {
278 DCHECK_NE(DRAW_MODE_NONE
, current_draw_mode_
);
279 current_draw_mode_
= DRAW_MODE_NONE
;
282 bool LayerImpl::ShowDebugBorders() const {
283 return layer_tree_impl()->debug_state().show_debug_borders
;
286 void LayerImpl::GetDebugBorderProperties(SkColor
* color
, float* width
) const {
287 if (draws_content_
) {
288 *color
= DebugColors::ContentLayerBorderColor();
289 *width
= DebugColors::ContentLayerBorderWidth(layer_tree_impl());
293 if (masks_to_bounds_
) {
294 *color
= DebugColors::MaskingLayerBorderColor();
295 *width
= DebugColors::MaskingLayerBorderWidth(layer_tree_impl());
299 *color
= DebugColors::ContainerLayerBorderColor();
300 *width
= DebugColors::ContainerLayerBorderWidth(layer_tree_impl());
303 void LayerImpl::AppendDebugBorderQuad(
304 RenderPass
* render_pass
,
305 const gfx::Size
& content_bounds
,
306 const SharedQuadState
* shared_quad_state
,
307 AppendQuadsData
* append_quads_data
) const {
310 GetDebugBorderProperties(&color
, &width
);
311 AppendDebugBorderQuad(render_pass
,
319 void LayerImpl::AppendDebugBorderQuad(RenderPass
* render_pass
,
320 const gfx::Size
& content_bounds
,
321 const SharedQuadState
* shared_quad_state
,
322 AppendQuadsData
* append_quads_data
,
325 if (!ShowDebugBorders())
328 gfx::Rect
quad_rect(content_bounds
);
329 gfx::Rect
visible_quad_rect(quad_rect
);
330 DebugBorderDrawQuad
* debug_border_quad
=
331 render_pass
->CreateAndAppendDrawQuad
<DebugBorderDrawQuad
>();
332 debug_border_quad
->SetNew(
333 shared_quad_state
, quad_rect
, visible_quad_rect
, color
, width
);
336 bool LayerImpl::HasDelegatedContent() const {
340 bool LayerImpl::HasContributingDelegatedRenderPasses() const {
344 RenderPassId
LayerImpl::FirstContributingRenderPassId() const {
345 return RenderPassId(0, 0);
348 RenderPassId
LayerImpl::NextContributingRenderPassId(RenderPassId id
) const {
349 return RenderPassId(0, 0);
352 ResourceProvider::ResourceId
LayerImpl::ContentsResourceId() const {
357 void LayerImpl::SetSentScrollDelta(const gfx::Vector2dF
& sent_scroll_delta
) {
358 // Pending tree never has sent scroll deltas
359 DCHECK(layer_tree_impl()->IsActiveTree());
361 if (sent_scroll_delta_
== sent_scroll_delta
)
364 sent_scroll_delta_
= sent_scroll_delta
;
367 gfx::Vector2dF
LayerImpl::ScrollBy(const gfx::Vector2dF
& scroll
) {
368 DCHECK(scrollable());
369 gfx::Vector2dF min_delta
= -ScrollOffsetToVector2dF(scroll_offset_
);
370 gfx::Vector2dF max_delta
= MaxScrollOffset().DeltaFrom(scroll_offset_
);
371 // Clamp new_delta so that position + delta stays within scroll bounds.
372 gfx::Vector2dF new_delta
= (ScrollDelta() + scroll
);
373 new_delta
.SetToMax(min_delta
);
374 new_delta
.SetToMin(max_delta
);
375 gfx::Vector2dF unscrolled
=
376 ScrollDelta() + scroll
- new_delta
;
377 SetScrollDelta(new_delta
);
382 void LayerImpl::SetScrollClipLayer(int scroll_clip_layer_id
) {
383 scroll_clip_layer_
= layer_tree_impl()->LayerById(scroll_clip_layer_id
);
386 void LayerImpl::ApplySentScrollDeltasFromAbortedCommit() {
387 // Pending tree never has sent scroll deltas
388 DCHECK(layer_tree_impl()->IsActiveTree());
390 // Apply sent scroll deltas to scroll position / scroll delta as if the
391 // main thread had applied them and then committed those values.
393 // This function should not change the total scroll offset; it just shifts
394 // some of the scroll delta to the scroll offset. Therefore, adjust these
395 // variables directly rather than calling the scroll offset delegate to
396 // avoid sending it multiple spurious calls.
398 // Because of the way scroll delta is calculated with a delegate, this will
399 // leave the total scroll offset unchanged on this layer regardless of
400 // whether a delegate is being used.
401 scroll_offset_
+= gfx::ScrollOffset(sent_scroll_delta_
);
402 scroll_delta_
-= sent_scroll_delta_
;
403 sent_scroll_delta_
= gfx::Vector2dF();
406 void LayerImpl::ApplyScrollDeltasSinceBeginMainFrame() {
407 // Only the pending tree can have missing scrolls.
408 DCHECK(layer_tree_impl()->IsPendingTree());
412 // Pending tree should never have sent scroll deltas.
413 DCHECK(sent_scroll_delta().IsZero());
415 LayerImpl
* active_twin
= layer_tree_impl()->FindActiveTreeLayerById(id());
417 // Scrolls that happens after begin frame (where the sent scroll delta
418 // comes from) and commit need to be applied to the pending tree
419 // so that it is up to date with the total scroll.
420 SetScrollDelta(active_twin
->ScrollDelta() -
421 active_twin
->sent_scroll_delta());
425 InputHandler::ScrollStatus
LayerImpl::TryScroll(
426 const gfx::PointF
& screen_space_point
,
427 InputHandler::ScrollInputType type
) const {
428 if (should_scroll_on_main_thread()) {
429 TRACE_EVENT0("cc", "LayerImpl::TryScroll: Failed ShouldScrollOnMainThread");
430 return InputHandler::ScrollOnMainThread
;
433 if (!screen_space_transform().IsInvertible()) {
434 TRACE_EVENT0("cc", "LayerImpl::TryScroll: Ignored NonInvertibleTransform");
435 return InputHandler::ScrollIgnored
;
438 if (!non_fast_scrollable_region().IsEmpty()) {
439 bool clipped
= false;
440 gfx::Transform
inverse_screen_space_transform(
441 gfx::Transform::kSkipInitialization
);
442 if (!screen_space_transform().GetInverse(&inverse_screen_space_transform
)) {
443 // TODO(shawnsingh): We shouldn't be applying a projection if screen space
444 // transform is uninvertible here. Perhaps we should be returning
445 // ScrollOnMainThread in this case?
448 gfx::PointF hit_test_point_in_content_space
=
449 MathUtil::ProjectPoint(inverse_screen_space_transform
,
452 gfx::PointF hit_test_point_in_layer_space
=
453 gfx::ScalePoint(hit_test_point_in_content_space
,
454 1.f
/ contents_scale_x(),
455 1.f
/ contents_scale_y());
457 non_fast_scrollable_region().Contains(
458 gfx::ToRoundedPoint(hit_test_point_in_layer_space
))) {
460 "LayerImpl::tryScroll: Failed NonFastScrollableRegion");
461 return InputHandler::ScrollOnMainThread
;
465 if (type
== InputHandler::Wheel
&& have_wheel_event_handlers()) {
466 TRACE_EVENT0("cc", "LayerImpl::tryScroll: Failed WheelEventHandlers");
467 return InputHandler::ScrollOnMainThread
;
471 TRACE_EVENT0("cc", "LayerImpl::tryScroll: Ignored not scrollable");
472 return InputHandler::ScrollIgnored
;
475 gfx::ScrollOffset max_scroll_offset
= MaxScrollOffset();
476 if (max_scroll_offset
.x() <= 0 && max_scroll_offset
.y() <= 0) {
478 "LayerImpl::tryScroll: Ignored. Technically scrollable,"
479 " but has no affordance in either direction.");
480 return InputHandler::ScrollIgnored
;
483 return InputHandler::ScrollStarted
;
486 gfx::Rect
LayerImpl::LayerRectToContentRect(
487 const gfx::RectF
& layer_rect
) const {
488 gfx::RectF content_rect
=
489 gfx::ScaleRect(layer_rect
, contents_scale_x(), contents_scale_y());
490 // Intersect with content rect to avoid the extra pixel because for some
491 // values x and y, ceil((x / y) * y) may be x + 1.
492 content_rect
.Intersect(gfx::Rect(content_bounds()));
493 return gfx::ToEnclosingRect(content_rect
);
496 skia::RefPtr
<SkPicture
> LayerImpl::GetPicture() {
497 return skia::RefPtr
<SkPicture
>();
500 scoped_ptr
<LayerImpl
> LayerImpl::CreateLayerImpl(LayerTreeImpl
* tree_impl
) {
501 return LayerImpl::Create(tree_impl
, layer_id_
);
504 void LayerImpl::PushPropertiesTo(LayerImpl
* layer
) {
505 layer
->SetTransformOrigin(transform_origin_
);
506 layer
->SetBackgroundColor(background_color_
);
507 layer
->SetBounds(bounds_
);
508 layer
->SetContentBounds(content_bounds());
509 layer
->SetContentsScale(contents_scale_x(), contents_scale_y());
510 layer
->SetDoubleSided(double_sided_
);
511 layer
->SetDrawCheckerboardForMissingTiles(
512 draw_checkerboard_for_missing_tiles_
);
513 layer
->SetForceRenderSurface(force_render_surface_
);
514 layer
->SetDrawsContent(DrawsContent());
515 layer
->SetHideLayerAndSubtree(hide_layer_and_subtree_
);
516 layer
->SetFilters(filters());
517 layer
->SetBackgroundFilters(background_filters());
518 layer
->SetMasksToBounds(masks_to_bounds_
);
519 layer
->SetShouldScrollOnMainThread(should_scroll_on_main_thread_
);
520 layer
->SetHaveWheelEventHandlers(have_wheel_event_handlers_
);
521 layer
->SetHaveScrollEventHandlers(have_scroll_event_handlers_
);
522 layer
->SetNonFastScrollableRegion(non_fast_scrollable_region_
);
523 layer
->SetTouchEventHandlerRegion(touch_event_handler_region_
);
524 layer
->SetContentsOpaque(contents_opaque_
);
525 layer
->SetOpacity(opacity_
);
526 layer
->SetBlendMode(blend_mode_
);
527 layer
->SetIsRootForIsolatedGroup(is_root_for_isolated_group_
);
528 layer
->SetPosition(position_
);
529 layer
->SetIsContainerForFixedPositionLayers(
530 is_container_for_fixed_position_layers_
);
531 layer
->SetPositionConstraint(position_constraint_
);
532 layer
->SetShouldFlattenTransform(should_flatten_transform_
);
533 layer
->SetUseParentBackfaceVisibility(use_parent_backface_visibility_
);
534 layer
->SetTransformAndInvertibility(transform_
, transform_is_invertible_
);
536 layer
->SetScrollClipLayer(scroll_clip_layer_
? scroll_clip_layer_
->id()
537 : Layer::INVALID_ID
);
538 layer
->set_user_scrollable_horizontal(user_scrollable_horizontal_
);
539 layer
->set_user_scrollable_vertical(user_scrollable_vertical_
);
540 layer
->SetScrollOffsetAndDelta(
542 layer
->ScrollDelta() - layer
->sent_scroll_delta());
543 layer
->SetSentScrollDelta(gfx::Vector2dF());
544 layer
->Set3dSortingContextId(sorting_context_id_
);
545 layer
->SetNumDescendantsThatDrawContent(num_descendants_that_draw_content_
);
547 LayerImpl
* scroll_parent
= NULL
;
548 if (scroll_parent_
) {
549 scroll_parent
= layer
->layer_tree_impl()->LayerById(scroll_parent_
->id());
550 DCHECK(scroll_parent
);
553 layer
->SetScrollParent(scroll_parent
);
554 if (scroll_children_
) {
555 std::set
<LayerImpl
*>* scroll_children
= new std::set
<LayerImpl
*>;
556 for (std::set
<LayerImpl
*>::iterator it
= scroll_children_
->begin();
557 it
!= scroll_children_
->end();
559 DCHECK_EQ((*it
)->scroll_parent(), this);
560 LayerImpl
* scroll_child
=
561 layer
->layer_tree_impl()->LayerById((*it
)->id());
562 DCHECK(scroll_child
);
563 scroll_children
->insert(scroll_child
);
565 layer
->SetScrollChildren(scroll_children
);
567 layer
->SetScrollChildren(NULL
);
570 LayerImpl
* clip_parent
= NULL
;
572 clip_parent
= layer
->layer_tree_impl()->LayerById(
577 layer
->SetClipParent(clip_parent
);
578 if (clip_children_
) {
579 std::set
<LayerImpl
*>* clip_children
= new std::set
<LayerImpl
*>;
580 for (std::set
<LayerImpl
*>::iterator it
= clip_children_
->begin();
581 it
!= clip_children_
->end(); ++it
)
582 clip_children
->insert(layer
->layer_tree_impl()->LayerById((*it
)->id()));
583 layer
->SetClipChildren(clip_children
);
585 layer
->SetClipChildren(NULL
);
588 layer
->PassCopyRequests(©_requests_
);
590 // If the main thread commits multiple times before the impl thread actually
591 // draws, then damage tracking will become incorrect if we simply clobber the
592 // update_rect here. The LayerImpl's update_rect needs to accumulate (i.e.
593 // union) any update changes that have occurred on the main thread.
594 update_rect_
.Union(layer
->update_rect());
595 layer
->SetUpdateRect(update_rect_
);
597 layer
->SetStackingOrderChanged(stacking_order_changed_
);
598 layer
->SetDebugInfo(debug_info_
);
600 // Reset any state that should be cleared for the next update.
601 stacking_order_changed_
= false;
602 update_rect_
= gfx::RectF();
603 needs_push_properties_
= false;
604 num_dependents_need_push_properties_
= 0;
607 gfx::Vector2dF
LayerImpl::FixedContainerSizeDelta() const {
608 if (!scroll_clip_layer_
)
609 return gfx::Vector2dF();
611 float scale_delta
= layer_tree_impl()->page_scale_delta();
612 float scale
= layer_tree_impl()->page_scale_factor();
614 gfx::Vector2dF delta_from_scroll
= scroll_clip_layer_
->bounds_delta();
615 delta_from_scroll
.Scale(1.f
/ scale
);
617 // The delta-from-pinch component requires some explanation: A viewport of
618 // size (w,h) will appear to be size (w/s,h/s) under scale s in the content
619 // space. If s -> s' on the impl thread, where s' = s * ds, then the apparent
620 // viewport size change in the content space due to ds is:
622 // (w/s',h/s') - (w/s,h/s) = (w,h)(1/s' - 1/s) = (w,h)(1 - ds)/(s ds)
624 gfx::Vector2dF delta_from_pinch
=
625 gfx::RectF(scroll_clip_layer_
->bounds()).bottom_right() - gfx::PointF();
626 delta_from_pinch
.Scale((1.f
- scale_delta
) / (scale
* scale_delta
));
628 return delta_from_scroll
+ delta_from_pinch
;
631 base::DictionaryValue
* LayerImpl::LayerTreeAsJson() const {
632 base::DictionaryValue
* result
= new base::DictionaryValue
;
633 result
->SetString("LayerType", LayerTypeAsString());
635 base::ListValue
* list
= new base::ListValue
;
636 list
->AppendDouble(bounds().width());
637 list
->AppendDouble(bounds().height());
638 result
->Set("Bounds", list
);
640 list
= new base::ListValue
;
641 list
->AppendDouble(position_
.x());
642 list
->AppendDouble(position_
.y());
643 result
->Set("Position", list
);
645 const gfx::Transform
& gfx_transform
= draw_properties_
.target_space_transform
;
646 double transform
[16];
647 gfx_transform
.matrix().asColMajord(transform
);
648 list
= new base::ListValue
;
649 for (int i
= 0; i
< 16; ++i
)
650 list
->AppendDouble(transform
[i
]);
651 result
->Set("DrawTransform", list
);
653 result
->SetBoolean("DrawsContent", draws_content_
);
654 result
->SetBoolean("Is3dSorted", Is3dSorted());
655 result
->SetDouble("Opacity", opacity());
656 result
->SetBoolean("ContentsOpaque", contents_opaque_
);
659 result
->SetBoolean("Scrollable", true);
661 if (have_wheel_event_handlers_
)
662 result
->SetBoolean("WheelHandler", have_wheel_event_handlers_
);
663 if (have_scroll_event_handlers_
)
664 result
->SetBoolean("ScrollHandler", have_scroll_event_handlers_
);
665 if (!touch_event_handler_region_
.IsEmpty()) {
666 scoped_ptr
<base::Value
> region
= touch_event_handler_region_
.AsValue();
667 result
->Set("TouchRegion", region
.release());
670 list
= new base::ListValue
;
671 for (size_t i
= 0; i
< children_
.size(); ++i
)
672 list
->Append(children_
[i
]->LayerTreeAsJson());
673 result
->Set("Children", list
);
678 void LayerImpl::SetStackingOrderChanged(bool stacking_order_changed
) {
679 if (stacking_order_changed
) {
680 stacking_order_changed_
= true;
681 NoteLayerPropertyChangedForSubtree();
685 void LayerImpl::NoteLayerPropertyChanged() {
686 layer_property_changed_
= true;
687 layer_tree_impl()->set_needs_update_draw_properties();
688 SetNeedsPushProperties();
691 void LayerImpl::NoteLayerPropertyChangedForSubtree() {
692 layer_property_changed_
= true;
693 layer_tree_impl()->set_needs_update_draw_properties();
694 for (size_t i
= 0; i
< children_
.size(); ++i
)
695 children_
[i
]->NoteLayerPropertyChangedForDescendantsInternal();
696 SetNeedsPushProperties();
699 void LayerImpl::NoteLayerPropertyChangedForDescendantsInternal() {
700 layer_property_changed_
= true;
701 for (size_t i
= 0; i
< children_
.size(); ++i
)
702 children_
[i
]->NoteLayerPropertyChangedForDescendantsInternal();
705 void LayerImpl::NoteLayerPropertyChangedForDescendants() {
706 layer_tree_impl()->set_needs_update_draw_properties();
707 for (size_t i
= 0; i
< children_
.size(); ++i
)
708 children_
[i
]->NoteLayerPropertyChangedForDescendantsInternal();
709 SetNeedsPushProperties();
712 const char* LayerImpl::LayerTypeAsString() const {
713 return "cc::LayerImpl";
716 void LayerImpl::ResetAllChangeTrackingForSubtree() {
717 layer_property_changed_
= false;
719 update_rect_
= gfx::RectF();
720 damage_rect_
= gfx::RectF();
722 if (draw_properties_
.render_surface
)
723 draw_properties_
.render_surface
->ResetPropertyChangedFlag();
726 mask_layer_
->ResetAllChangeTrackingForSubtree();
728 if (replica_layer_
) {
729 // This also resets the replica mask, if it exists.
730 replica_layer_
->ResetAllChangeTrackingForSubtree();
733 for (size_t i
= 0; i
< children_
.size(); ++i
)
734 children_
[i
]->ResetAllChangeTrackingForSubtree();
736 needs_push_properties_
= false;
737 num_dependents_need_push_properties_
= 0;
740 gfx::ScrollOffset
LayerImpl::ScrollOffsetForAnimation() const {
741 return TotalScrollOffset();
744 void LayerImpl::OnFilterAnimated(const FilterOperations
& filters
) {
748 void LayerImpl::OnOpacityAnimated(float opacity
) {
752 void LayerImpl::OnTransformAnimated(const gfx::Transform
& transform
) {
753 SetTransform(transform
);
756 void LayerImpl::OnScrollOffsetAnimated(const gfx::ScrollOffset
& scroll_offset
) {
757 // Only layers in the active tree should need to do anything here, since
758 // layers in the pending tree will find out about these changes as a
759 // result of the call to SetScrollDelta.
763 SetScrollDelta(scroll_offset
.DeltaFrom(scroll_offset_
));
765 layer_tree_impl_
->DidAnimateScrollOffset();
768 void LayerImpl::OnAnimationWaitingForDeletion() {}
770 bool LayerImpl::IsActive() const {
771 return layer_tree_impl_
->IsActiveTree();
774 gfx::SizeF
LayerImpl::bounds() const {
775 return gfx::ToCeiledSize(gfx::SizeF(bounds_
.width() + bounds_delta_
.x(),
776 bounds_
.height() + bounds_delta_
.y()));
779 void LayerImpl::SetBounds(const gfx::Size
& bounds
) {
780 if (bounds_
== bounds
)
785 ScrollbarParametersDidChange(true);
786 if (masks_to_bounds())
787 NoteLayerPropertyChangedForSubtree();
789 NoteLayerPropertyChanged();
792 void LayerImpl::SetBoundsDelta(const gfx::Vector2dF
& bounds_delta
) {
793 if (bounds_delta_
== bounds_delta
)
796 bounds_delta_
= bounds_delta
;
798 ScrollbarParametersDidChange(false);
799 if (masks_to_bounds())
800 NoteLayerPropertyChangedForSubtree();
802 NoteLayerPropertyChanged();
805 void LayerImpl::SetMaskLayer(scoped_ptr
<LayerImpl
> mask_layer
) {
806 int new_layer_id
= mask_layer
? mask_layer
->id() : -1;
809 DCHECK_EQ(layer_tree_impl(), mask_layer
->layer_tree_impl());
810 DCHECK_NE(new_layer_id
, mask_layer_id_
);
811 } else if (new_layer_id
== mask_layer_id_
) {
815 mask_layer_
= mask_layer
.Pass();
816 mask_layer_id_
= new_layer_id
;
818 mask_layer_
->SetParent(this);
819 NoteLayerPropertyChangedForSubtree();
822 scoped_ptr
<LayerImpl
> LayerImpl::TakeMaskLayer() {
824 return mask_layer_
.Pass();
827 void LayerImpl::SetReplicaLayer(scoped_ptr
<LayerImpl
> replica_layer
) {
828 int new_layer_id
= replica_layer
? replica_layer
->id() : -1;
831 DCHECK_EQ(layer_tree_impl(), replica_layer
->layer_tree_impl());
832 DCHECK_NE(new_layer_id
, replica_layer_id_
);
833 } else if (new_layer_id
== replica_layer_id_
) {
837 replica_layer_
= replica_layer
.Pass();
838 replica_layer_id_
= new_layer_id
;
840 replica_layer_
->SetParent(this);
841 NoteLayerPropertyChangedForSubtree();
844 scoped_ptr
<LayerImpl
> LayerImpl::TakeReplicaLayer() {
845 replica_layer_id_
= -1;
846 return replica_layer_
.Pass();
849 ScrollbarLayerImplBase
* LayerImpl::ToScrollbarLayer() {
853 void LayerImpl::SetDrawsContent(bool draws_content
) {
854 if (draws_content_
== draws_content
)
857 draws_content_
= draws_content
;
858 NoteLayerPropertyChanged();
861 void LayerImpl::SetHideLayerAndSubtree(bool hide
) {
862 if (hide_layer_and_subtree_
== hide
)
865 hide_layer_and_subtree_
= hide
;
866 NoteLayerPropertyChangedForSubtree();
869 void LayerImpl::SetTransformOrigin(const gfx::Point3F
& transform_origin
) {
870 if (transform_origin_
== transform_origin
)
872 transform_origin_
= transform_origin
;
873 NoteLayerPropertyChangedForSubtree();
876 void LayerImpl::SetBackgroundColor(SkColor background_color
) {
877 if (background_color_
== background_color
)
880 background_color_
= background_color
;
881 NoteLayerPropertyChanged();
884 SkColor
LayerImpl::SafeOpaqueBackgroundColor() const {
885 SkColor color
= background_color();
886 if (SkColorGetA(color
) == 255 && !contents_opaque()) {
887 color
= SK_ColorTRANSPARENT
;
888 } else if (SkColorGetA(color
) != 255 && contents_opaque()) {
889 for (const LayerImpl
* layer
= parent(); layer
;
890 layer
= layer
->parent()) {
891 color
= layer
->background_color();
892 if (SkColorGetA(color
) == 255)
895 if (SkColorGetA(color
) != 255)
896 color
= layer_tree_impl()->background_color();
897 if (SkColorGetA(color
) != 255)
898 color
= SkColorSetA(color
, 255);
903 void LayerImpl::SetFilters(const FilterOperations
& filters
) {
904 if (filters_
== filters
)
908 NoteLayerPropertyChangedForSubtree();
911 bool LayerImpl::FilterIsAnimating() const {
912 return layer_animation_controller_
->IsAnimatingProperty(Animation::Filter
);
915 bool LayerImpl::FilterIsAnimatingOnImplOnly() const {
916 Animation
* filter_animation
=
917 layer_animation_controller_
->GetAnimation(Animation::Filter
);
918 return filter_animation
&& filter_animation
->is_impl_only();
921 void LayerImpl::SetBackgroundFilters(
922 const FilterOperations
& filters
) {
923 if (background_filters_
== filters
)
926 background_filters_
= filters
;
927 NoteLayerPropertyChanged();
930 void LayerImpl::SetMasksToBounds(bool masks_to_bounds
) {
931 if (masks_to_bounds_
== masks_to_bounds
)
934 masks_to_bounds_
= masks_to_bounds
;
935 NoteLayerPropertyChangedForSubtree();
938 void LayerImpl::SetContentsOpaque(bool opaque
) {
939 if (contents_opaque_
== opaque
)
942 contents_opaque_
= opaque
;
943 NoteLayerPropertyChangedForSubtree();
946 void LayerImpl::SetOpacity(float opacity
) {
947 if (opacity_
== opacity
)
951 NoteLayerPropertyChangedForSubtree();
954 bool LayerImpl::OpacityIsAnimating() const {
955 return layer_animation_controller_
->IsAnimatingProperty(Animation::Opacity
);
958 bool LayerImpl::OpacityIsAnimatingOnImplOnly() const {
959 Animation
* opacity_animation
=
960 layer_animation_controller_
->GetAnimation(Animation::Opacity
);
961 return opacity_animation
&& opacity_animation
->is_impl_only();
964 void LayerImpl::SetBlendMode(SkXfermode::Mode blend_mode
) {
965 if (blend_mode_
== blend_mode
)
968 blend_mode_
= blend_mode
;
969 NoteLayerPropertyChangedForSubtree();
972 void LayerImpl::SetIsRootForIsolatedGroup(bool root
) {
973 if (is_root_for_isolated_group_
== root
)
976 is_root_for_isolated_group_
= root
;
977 SetNeedsPushProperties();
980 void LayerImpl::SetPosition(const gfx::PointF
& position
) {
981 if (position_
== position
)
984 position_
= position
;
985 NoteLayerPropertyChangedForSubtree();
988 void LayerImpl::SetShouldFlattenTransform(bool flatten
) {
989 if (should_flatten_transform_
== flatten
)
992 should_flatten_transform_
= flatten
;
993 NoteLayerPropertyChangedForSubtree();
996 void LayerImpl::Set3dSortingContextId(int id
) {
997 if (id
== sorting_context_id_
)
999 sorting_context_id_
= id
;
1000 NoteLayerPropertyChangedForSubtree();
1003 void LayerImpl::SetTransform(const gfx::Transform
& transform
) {
1004 if (transform_
== transform
)
1007 transform_
= transform
;
1008 transform_is_invertible_
= transform_
.IsInvertible();
1009 NoteLayerPropertyChangedForSubtree();
1012 void LayerImpl::SetTransformAndInvertibility(const gfx::Transform
& transform
,
1013 bool transform_is_invertible
) {
1014 if (transform_
== transform
) {
1015 DCHECK(transform_is_invertible_
== transform_is_invertible
)
1016 << "Can't change invertibility if transform is unchanged";
1019 transform_
= transform
;
1020 transform_is_invertible_
= transform_is_invertible
;
1021 NoteLayerPropertyChangedForSubtree();
1024 bool LayerImpl::TransformIsAnimating() const {
1025 return layer_animation_controller_
->IsAnimatingProperty(Animation::Transform
);
1028 bool LayerImpl::TransformIsAnimatingOnImplOnly() const {
1029 Animation
* transform_animation
=
1030 layer_animation_controller_
->GetAnimation(Animation::Transform
);
1031 return transform_animation
&& transform_animation
->is_impl_only();
1034 void LayerImpl::SetUpdateRect(const gfx::RectF
& update_rect
) {
1035 update_rect_
= update_rect
;
1036 SetNeedsPushProperties();
1039 void LayerImpl::AddDamageRect(const gfx::RectF
& damage_rect
) {
1040 damage_rect_
= gfx::UnionRects(damage_rect_
, damage_rect
);
1043 void LayerImpl::SetContentBounds(const gfx::Size
& content_bounds
) {
1044 if (this->content_bounds() == content_bounds
)
1047 draw_properties_
.content_bounds
= content_bounds
;
1048 NoteLayerPropertyChanged();
1051 void LayerImpl::SetContentsScale(float contents_scale_x
,
1052 float contents_scale_y
) {
1053 if (this->contents_scale_x() == contents_scale_x
&&
1054 this->contents_scale_y() == contents_scale_y
)
1057 draw_properties_
.contents_scale_x
= contents_scale_x
;
1058 draw_properties_
.contents_scale_y
= contents_scale_y
;
1059 NoteLayerPropertyChanged();
1062 void LayerImpl::SetScrollOffsetDelegate(
1063 ScrollOffsetDelegate
* scroll_offset_delegate
) {
1064 // Having both a scroll parent and a scroll offset delegate is unsupported.
1065 DCHECK(!scroll_parent_
);
1066 if (!scroll_offset_delegate
&& scroll_offset_delegate_
) {
1067 scroll_delta_
= scroll_offset_delegate_
->GetTotalScrollOffset().DeltaFrom(
1070 gfx::ScrollOffset total_offset
= TotalScrollOffset();
1071 scroll_offset_delegate_
= scroll_offset_delegate
;
1072 if (scroll_offset_delegate_
)
1073 scroll_offset_delegate_
->SetTotalScrollOffset(total_offset
);
1076 bool LayerImpl::IsExternalFlingActive() const {
1077 return scroll_offset_delegate_
&&
1078 scroll_offset_delegate_
->IsExternalFlingActive();
1081 void LayerImpl::SetScrollOffset(const gfx::ScrollOffset
& scroll_offset
) {
1082 SetScrollOffsetAndDelta(scroll_offset
, ScrollDelta());
1085 void LayerImpl::SetScrollOffsetAndDelta(const gfx::ScrollOffset
& scroll_offset
,
1086 const gfx::Vector2dF
& scroll_delta
) {
1087 bool changed
= false;
1089 last_scroll_offset_
= scroll_offset
;
1091 if (scroll_offset_
!= scroll_offset
) {
1093 scroll_offset_
= scroll_offset
;
1095 if (scroll_offset_delegate_
)
1096 scroll_offset_delegate_
->SetTotalScrollOffset(TotalScrollOffset());
1099 if (ScrollDelta() != scroll_delta
) {
1101 if (layer_tree_impl()->IsActiveTree()) {
1102 LayerImpl
* pending_twin
=
1103 layer_tree_impl()->FindPendingTreeLayerById(id());
1105 // The pending twin can't mirror the scroll delta of the active
1106 // layer. Although the delta - sent scroll delta difference is
1107 // identical for both twins, the sent scroll delta for the pending
1108 // layer is zero, as anything that has been sent has been baked
1109 // into the layer's position/scroll offset as a part of commit.
1110 DCHECK(pending_twin
->sent_scroll_delta().IsZero());
1111 pending_twin
->SetScrollDelta(scroll_delta
- sent_scroll_delta());
1115 if (scroll_offset_delegate_
) {
1116 scroll_offset_delegate_
->SetTotalScrollOffset(
1117 ScrollOffsetWithDelta(scroll_offset_
, scroll_delta
));
1119 scroll_delta_
= scroll_delta
;
1124 NoteLayerPropertyChangedForSubtree();
1125 ScrollbarParametersDidChange(false);
1129 gfx::Vector2dF
LayerImpl::ScrollDelta() const {
1130 if (scroll_offset_delegate_
) {
1131 return scroll_offset_delegate_
->GetTotalScrollOffset().DeltaFrom(
1134 return scroll_delta_
;
1137 void LayerImpl::SetScrollDelta(const gfx::Vector2dF
& scroll_delta
) {
1138 SetScrollOffsetAndDelta(scroll_offset_
, scroll_delta
);
1141 gfx::ScrollOffset
LayerImpl::TotalScrollOffset() const {
1142 return ScrollOffsetWithDelta(scroll_offset_
, ScrollDelta());
1145 void LayerImpl::SetDoubleSided(bool double_sided
) {
1146 if (double_sided_
== double_sided
)
1149 double_sided_
= double_sided
;
1150 NoteLayerPropertyChangedForSubtree();
1153 SimpleEnclosedRegion
LayerImpl::VisibleContentOpaqueRegion() const {
1154 if (contents_opaque())
1155 return SimpleEnclosedRegion(visible_content_rect());
1156 return SimpleEnclosedRegion();
1159 void LayerImpl::DidBeginTracing() {}
1161 void LayerImpl::ReleaseResources() {}
1163 gfx::ScrollOffset
LayerImpl::MaxScrollOffset() const {
1164 if (!scroll_clip_layer_
|| bounds().IsEmpty())
1165 return gfx::ScrollOffset();
1167 LayerImpl
const* page_scale_layer
= layer_tree_impl()->page_scale_layer();
1168 DCHECK(this != page_scale_layer
);
1169 DCHECK(this != layer_tree_impl()->InnerViewportScrollLayer() ||
1170 IsContainerForFixedPositionLayers());
1172 gfx::SizeF
scaled_scroll_bounds(bounds());
1174 float scale_factor
= 1.f
;
1175 for (LayerImpl
const* current_layer
= this;
1176 current_layer
!= scroll_clip_layer_
;
1177 current_layer
= current_layer
->parent()) {
1178 DCHECK(current_layer
);
1179 float current_layer_scale
= 1.f
;
1181 const gfx::Transform
& layer_transform
= current_layer
->transform();
1182 if (current_layer
== page_scale_layer
) {
1183 DCHECK(layer_transform
.IsIdentity());
1184 current_layer_scale
= layer_tree_impl()->total_page_scale_factor();
1186 // TODO(wjmaclean) Should we allow for translation too?
1187 DCHECK(layer_transform
.IsScale2d());
1188 gfx::Vector2dF layer_scale
= layer_transform
.Scale2d();
1189 // TODO(wjmaclean) Allow for non-isotropic scales.
1190 DCHECK(layer_scale
.x() == layer_scale
.y());
1191 current_layer_scale
= layer_scale
.x();
1194 scale_factor
*= current_layer_scale
;
1196 // TODO(wjmaclean) Once we move to a model where the two-viewport model is
1197 // turned on in all builds, remove the next two lines. For now however, the
1198 // page scale layer may coincide with the clip layer, and so this is
1200 if (page_scale_layer
== scroll_clip_layer_
)
1201 scale_factor
*= layer_tree_impl()->total_page_scale_factor();
1203 scaled_scroll_bounds
.SetSize(scale_factor
* scaled_scroll_bounds
.width(),
1204 scale_factor
* scaled_scroll_bounds
.height());
1205 scaled_scroll_bounds
= gfx::ToFlooredSize(scaled_scroll_bounds
);
1207 gfx::ScrollOffset
max_offset(
1208 scaled_scroll_bounds
.width() - scroll_clip_layer_
->bounds().width(),
1209 scaled_scroll_bounds
.height() - scroll_clip_layer_
->bounds().height());
1210 // We need the final scroll offset to be in CSS coords.
1211 max_offset
.Scale(1 / scale_factor
);
1212 max_offset
.SetToMax(gfx::ScrollOffset());
1216 gfx::Vector2dF
LayerImpl::ClampScrollToMaxScrollOffset() {
1217 gfx::ScrollOffset max_offset
= MaxScrollOffset();
1218 gfx::ScrollOffset old_offset
= TotalScrollOffset();
1219 gfx::ScrollOffset clamped_offset
= old_offset
;
1221 clamped_offset
.SetToMin(max_offset
);
1222 clamped_offset
.SetToMax(gfx::ScrollOffset());
1223 gfx::Vector2dF delta
= clamped_offset
.DeltaFrom(old_offset
);
1224 if (!delta
.IsZero())
1230 void LayerImpl::SetScrollbarPosition(ScrollbarLayerImplBase
* scrollbar_layer
,
1231 LayerImpl
* scrollbar_clip_layer
,
1232 bool on_resize
) const {
1233 DCHECK(scrollbar_layer
);
1234 LayerImpl
* page_scale_layer
= layer_tree_impl()->page_scale_layer();
1236 DCHECK(this != page_scale_layer
);
1237 DCHECK(scrollbar_clip_layer
);
1238 DCHECK(this != layer_tree_impl()->InnerViewportScrollLayer() ||
1239 IsContainerForFixedPositionLayers());
1240 gfx::RectF
clip_rect(gfx::PointF(), scrollbar_clip_layer
->bounds());
1242 // See comment in MaxScrollOffset() regarding the use of the content layer
1244 gfx::RectF
scroll_rect(gfx::PointF(), bounds());
1246 if (scroll_rect
.size().IsEmpty())
1249 // TODO(wjmaclean) This computation is nearly identical to the one in
1250 // MaxScrollOffset. Find some way to combine these.
1251 gfx::ScrollOffset current_offset
;
1252 for (LayerImpl
const* current_layer
= this;
1253 current_layer
!= scrollbar_clip_layer
;
1254 current_layer
= current_layer
->parent()) {
1255 DCHECK(current_layer
);
1256 const gfx::Transform
& layer_transform
= current_layer
->transform();
1257 if (current_layer
== page_scale_layer
) {
1258 DCHECK(layer_transform
.IsIdentity());
1259 float scale_factor
= layer_tree_impl()->total_page_scale_factor();
1260 current_offset
.Scale(scale_factor
);
1261 scroll_rect
.Scale(scale_factor
);
1263 DCHECK(layer_transform
.IsScale2d());
1264 gfx::Vector2dF layer_scale
= layer_transform
.Scale2d();
1265 DCHECK(layer_scale
.x() == layer_scale
.y());
1266 gfx::ScrollOffset new_offset
= ScrollOffsetWithDelta(
1267 current_layer
->scroll_offset(), current_layer
->ScrollDelta());
1268 new_offset
.Scale(layer_scale
.x(), layer_scale
.y());
1269 current_offset
+= new_offset
;
1272 // TODO(wjmaclean) Once we move to a model where the two-viewport model is
1273 // turned on in all builds, remove the next two lines. For now however, the
1274 // page scale layer may coincide with the clip layer, and so this is
1276 if (page_scale_layer
== scrollbar_clip_layer
) {
1277 scroll_rect
.Scale(layer_tree_impl()->total_page_scale_factor());
1278 current_offset
.Scale(layer_tree_impl()->total_page_scale_factor());
1281 bool scrollbar_needs_animation
= false;
1282 scrollbar_needs_animation
|= scrollbar_layer
->SetVerticalAdjust(
1283 scrollbar_clip_layer
->bounds_delta().y());
1284 if (scrollbar_layer
->orientation() == HORIZONTAL
) {
1285 float visible_ratio
= clip_rect
.width() / scroll_rect
.width();
1286 scrollbar_needs_animation
|=
1287 scrollbar_layer
->SetCurrentPos(current_offset
.x());
1288 scrollbar_needs_animation
|=
1289 scrollbar_layer
->SetMaximum(scroll_rect
.width() - clip_rect
.width());
1290 scrollbar_needs_animation
|=
1291 scrollbar_layer
->SetVisibleToTotalLengthRatio(visible_ratio
);
1293 float visible_ratio
= clip_rect
.height() / scroll_rect
.height();
1294 scrollbar_needs_animation
|=
1295 scrollbar_layer
->SetCurrentPos(current_offset
.y());
1296 scrollbar_needs_animation
|=
1297 scrollbar_layer
->SetMaximum(scroll_rect
.height() - clip_rect
.height());
1298 scrollbar_needs_animation
|=
1299 scrollbar_layer
->SetVisibleToTotalLengthRatio(visible_ratio
);
1301 if (scrollbar_needs_animation
) {
1302 layer_tree_impl()->set_needs_update_draw_properties();
1303 // TODO(wjmaclean) The scrollbar animator for the pinch-zoom scrollbars
1304 // should activate for every scroll on the main frame, not just the
1305 // scrolls that move the pinch virtual viewport (i.e. trigger from
1306 // either inner or outer viewport).
1307 if (scrollbar_animation_controller_
) {
1308 // When both non-overlay and overlay scrollbars are both present, don't
1309 // animate the overlay scrollbars when page scale factor is at the min.
1310 // Non-overlay scrollbars also shouldn't trigger animations.
1311 bool is_animatable_scrollbar
=
1312 scrollbar_layer
->is_overlay_scrollbar() &&
1313 ((layer_tree_impl()->total_page_scale_factor() >
1314 layer_tree_impl()->min_page_scale_factor()) ||
1315 !layer_tree_impl()->settings().use_pinch_zoom_scrollbars
);
1316 if (is_animatable_scrollbar
)
1317 scrollbar_animation_controller_
->DidScrollUpdate(on_resize
);
1322 void LayerImpl::DidBecomeActive() {
1323 if (layer_tree_impl_
->settings().scrollbar_animator
==
1324 LayerTreeSettings::NoAnimator
) {
1328 bool need_scrollbar_animation_controller
= scrollable() && scrollbars_
;
1329 if (!need_scrollbar_animation_controller
) {
1330 scrollbar_animation_controller_
= nullptr;
1334 if (scrollbar_animation_controller_
)
1337 scrollbar_animation_controller_
=
1338 layer_tree_impl_
->CreateScrollbarAnimationController(this);
1341 void LayerImpl::ClearScrollbars() {
1345 scrollbars_
.reset(NULL
);
1348 void LayerImpl::AddScrollbar(ScrollbarLayerImplBase
* layer
) {
1350 DCHECK(!scrollbars_
|| scrollbars_
->find(layer
) == scrollbars_
->end());
1352 scrollbars_
.reset(new ScrollbarSet());
1354 scrollbars_
->insert(layer
);
1357 void LayerImpl::RemoveScrollbar(ScrollbarLayerImplBase
* layer
) {
1358 DCHECK(scrollbars_
);
1360 DCHECK(scrollbars_
->find(layer
) != scrollbars_
->end());
1362 scrollbars_
->erase(layer
);
1363 if (scrollbars_
->empty())
1364 scrollbars_
= nullptr;
1367 bool LayerImpl::HasScrollbar(ScrollbarOrientation orientation
) const {
1371 for (ScrollbarSet::iterator it
= scrollbars_
->begin();
1372 it
!= scrollbars_
->end();
1374 if ((*it
)->orientation() == orientation
)
1380 void LayerImpl::ScrollbarParametersDidChange(bool on_resize
) {
1384 for (ScrollbarSet::iterator it
= scrollbars_
->begin();
1385 it
!= scrollbars_
->end();
1387 (*it
)->ScrollbarParametersDidChange(on_resize
);
1390 void LayerImpl::SetNeedsPushProperties() {
1391 if (needs_push_properties_
)
1393 if (!parent_should_know_need_push_properties() && parent_
)
1394 parent_
->AddDependentNeedsPushProperties();
1395 needs_push_properties_
= true;
1398 void LayerImpl::AddDependentNeedsPushProperties() {
1399 DCHECK_GE(num_dependents_need_push_properties_
, 0);
1401 if (!parent_should_know_need_push_properties() && parent_
)
1402 parent_
->AddDependentNeedsPushProperties();
1404 num_dependents_need_push_properties_
++;
1407 void LayerImpl::RemoveDependentNeedsPushProperties() {
1408 num_dependents_need_push_properties_
--;
1409 DCHECK_GE(num_dependents_need_push_properties_
, 0);
1411 if (!parent_should_know_need_push_properties() && parent_
)
1412 parent_
->RemoveDependentNeedsPushProperties();
1415 void LayerImpl::GetAllTilesForTracing(std::set
<const Tile
*>* tiles
) const {
1418 void LayerImpl::AsValueInto(base::debug::TracedValue
* state
) const {
1419 TracedValue::MakeDictIntoImplicitSnapshotWithCategory(
1420 TRACE_DISABLED_BY_DEFAULT("cc.debug"),
1423 LayerTypeAsString(),
1425 state
->SetInteger("layer_id", id());
1426 state
->BeginDictionary("bounds");
1427 MathUtil::AddToTracedValue(bounds_
, state
);
1428 state
->EndDictionary();
1430 state
->SetDouble("opacity", opacity());
1432 state
->BeginArray("position");
1433 MathUtil::AddToTracedValue(position_
, state
);
1436 state
->SetInteger("draws_content", DrawsContent());
1437 state
->SetInteger("gpu_memory_usage", GPUMemoryUsageInBytes());
1439 state
->BeginArray("scroll_offset");
1440 MathUtil::AddToTracedValue(scroll_offset_
, state
);
1443 state
->BeginArray("transform_origin");
1444 MathUtil::AddToTracedValue(transform_origin_
, state
);
1448 gfx::QuadF layer_quad
= MathUtil::MapQuad(
1449 screen_space_transform(),
1450 gfx::QuadF(gfx::Rect(content_bounds())),
1452 state
->BeginArray("layer_quad");
1453 MathUtil::AddToTracedValue(layer_quad
, state
);
1455 if (!touch_event_handler_region_
.IsEmpty()) {
1456 state
->BeginArray("touch_event_handler_region");
1457 touch_event_handler_region_
.AsValueInto(state
);
1460 if (have_wheel_event_handlers_
) {
1461 gfx::Rect
wheel_rect(content_bounds());
1462 Region
wheel_region(wheel_rect
);
1463 state
->BeginArray("wheel_event_handler_region");
1464 wheel_region
.AsValueInto(state
);
1467 if (have_scroll_event_handlers_
) {
1468 gfx::Rect
scroll_rect(content_bounds());
1469 Region
scroll_region(scroll_rect
);
1470 state
->BeginArray("scroll_event_handler_region");
1471 scroll_region
.AsValueInto(state
);
1474 if (!non_fast_scrollable_region_
.IsEmpty()) {
1475 state
->BeginArray("non_fast_scrollable_region");
1476 non_fast_scrollable_region_
.AsValueInto(state
);
1480 state
->BeginArray("children");
1481 for (size_t i
= 0; i
< children_
.size(); ++i
) {
1482 state
->BeginDictionary();
1483 children_
[i
]->AsValueInto(state
);
1484 state
->EndDictionary();
1488 state
->BeginDictionary("mask_layer");
1489 mask_layer_
->AsValueInto(state
);
1490 state
->EndDictionary();
1492 if (replica_layer_
) {
1493 state
->BeginDictionary("replica_layer");
1494 replica_layer_
->AsValueInto(state
);
1495 state
->EndDictionary();
1499 state
->SetInteger("scroll_parent", scroll_parent_
->id());
1502 state
->SetInteger("clip_parent", clip_parent_
->id());
1504 state
->SetBoolean("can_use_lcd_text", can_use_lcd_text());
1505 state
->SetBoolean("contents_opaque", contents_opaque());
1508 "has_animation_bounds",
1509 layer_animation_controller()->HasAnimationThatInflatesBounds());
1512 if (LayerUtils::GetAnimationBounds(*this, &box
)) {
1513 state
->BeginArray("animation_bounds");
1514 MathUtil::AddToTracedValue(box
, state
);
1518 if (debug_info_
.get()) {
1520 debug_info_
->AppendAsTraceFormat(&str
);
1521 base::JSONReader json_reader
;
1522 scoped_ptr
<base::Value
> debug_info_value(json_reader
.ReadToValue(str
));
1524 if (debug_info_value
->IsType(base::Value::TYPE_DICTIONARY
)) {
1525 base::DictionaryValue
* dictionary_value
= NULL
;
1526 bool converted_to_dictionary
=
1527 debug_info_value
->GetAsDictionary(&dictionary_value
);
1528 DCHECK(converted_to_dictionary
);
1529 for (base::DictionaryValue::Iterator
it(*dictionary_value
); !it
.IsAtEnd();
1531 state
->SetValue(it
.key().data(), it
.value().DeepCopy());
1539 bool LayerImpl::IsDrawnRenderSurfaceLayerListMember() const {
1540 return draw_properties_
.last_drawn_render_surface_layer_list_id
==
1541 layer_tree_impl_
->current_render_surface_list_id();
1544 size_t LayerImpl::GPUMemoryUsageInBytes() const { return 0; }
1546 void LayerImpl::RunMicroBenchmark(MicroBenchmarkImpl
* benchmark
) {
1547 benchmark
->RunOnLayer(this);
1550 int LayerImpl::NumDescendantsThatDrawContent() const {
1551 return num_descendants_that_draw_content_
;
1554 void LayerImpl::NotifyAnimationFinished(
1555 base::TimeTicks monotonic_time
,
1556 Animation::TargetProperty target_property
) {
1557 if (target_property
== Animation::ScrollOffset
)
1558 layer_tree_impl_
->InputScrollAnimationFinished();