Add test for clicking bookmark star in presence of ctrl-D keybinding
[chromium-blink-merge.git] / cc / layers / layer_impl.cc
blob281a3ae411a624d9a35a94adde9b4f6fff3dd5e1
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"
36 namespace cc {
37 LayerImpl::LayerImpl(LayerTreeImpl* tree_impl, int id)
38 : parent_(nullptr),
39 scroll_parent_(nullptr),
40 clip_parent_(nullptr),
41 mask_layer_id_(-1),
42 replica_layer_id_(-1),
43 layer_id_(id),
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),
53 double_sided_(true),
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),
65 background_color_(0),
66 opacity_(1.0),
67 blend_mode_(SkXfermode::kSrcOver_Mode),
68 num_descendants_that_draw_content_(0),
69 draw_depth_(0.f),
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);
81 if (IsActive()) {
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();
113 ++it) {
114 if (*it == child) {
115 scoped_ptr<LayerImpl> ret = children_.take(it);
116 children_.erase(it);
117 layer_tree_impl()->set_needs_update_draw_properties();
118 return ret.Pass();
121 return nullptr;
124 void LayerImpl::SetParent(LayerImpl* parent) {
125 if (parent_should_know_need_push_properties()) {
126 if (parent_)
127 parent_->RemoveDependentNeedsPushProperties();
128 if (parent)
129 parent->AddDependentNeedsPushProperties();
131 parent_ = parent;
134 void LayerImpl::ClearChildList() {
135 if (children_.empty())
136 return;
138 children_.clear();
139 layer_tree_impl()->set_needs_update_draw_properties();
142 bool LayerImpl::HasAncestor(const LayerImpl* ancestor) const {
143 if (!ancestor)
144 return false;
146 for (const LayerImpl* layer = this; layer; layer = layer->parent()) {
147 if (layer == ancestor)
148 return true;
151 return false;
154 void LayerImpl::SetScrollParent(LayerImpl* parent) {
155 if (scroll_parent_ == parent)
156 return;
158 // Having both a scroll parent and a scroll offset delegate is unsupported.
159 DCHECK(!scroll_offset_delegate_);
161 if (parent)
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) {
170 debug_info_ = other;
171 SetNeedsPushProperties();
174 void LayerImpl::SetScrollChildren(std::set<LayerImpl*>* children) {
175 if (scroll_children_.get() == children)
176 return;
177 scroll_children_.reset(children);
178 SetNeedsPushProperties();
181 void LayerImpl::SetNumDescendantsThatDrawContent(int num_descendants) {
182 if (num_descendants_that_draw_content_ == num_descendants)
183 return;
184 num_descendants_that_draw_content_ = num_descendants;
185 SetNeedsPushProperties();
188 void LayerImpl::SetClipParent(LayerImpl* ancestor) {
189 if (clip_parent_ == ancestor)
190 return;
192 clip_parent_ = ancestor;
193 SetNeedsPushProperties();
196 void LayerImpl::SetClipChildren(std::set<LayerImpl*>* children) {
197 if (clip_children_.get() == children)
198 return;
199 clip_children_.reset(children);
200 SetNeedsPushProperties();
203 void LayerImpl::PassCopyRequests(ScopedPtrVector<CopyOutputRequest>* requests) {
204 if (requests->empty())
205 return;
206 DCHECK(render_surface());
207 bool was_empty = copy_requests_.empty();
208 copy_requests_.insert_and_take(copy_requests_.end(), requests);
209 requests->clear();
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(), &copy_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())
229 continue;
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() {
242 if (render_surface_)
243 render_surface_->ClearLayerLists();
246 void LayerImpl::PopulateSharedQuadState(SharedQuadState* state) const {
247 state->SetAll(
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;
260 return true;
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());
276 return;
279 if (masks_to_bounds_) {
280 *color = DebugColors::MaskingLayerBorderColor();
281 *width = DebugColors::MaskingLayerBorderWidth(layer_tree_impl());
282 return;
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 {
294 SkColor color;
295 float width;
296 GetDebugBorderProperties(&color, &width);
297 AppendDebugBorderQuad(render_pass,
298 content_bounds,
299 shared_quad_state,
300 append_quads_data,
301 color,
302 width);
305 void LayerImpl::AppendDebugBorderQuad(RenderPass* render_pass,
306 const gfx::Size& content_bounds,
307 const SharedQuadState* shared_quad_state,
308 AppendQuadsData* append_quads_data,
309 SkColor color,
310 float width) const {
311 if (!ShowDebugBorders())
312 return;
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 {
323 return false;
326 bool LayerImpl::HasContributingDelegatedRenderPasses() const {
327 return false;
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) {
340 return false;
343 void LayerImpl::GetContentsResourceId(ResourceProvider::ResourceId* resource_id,
344 gfx::Size* resource_size) const {
345 NOTREACHED();
346 *resource_id = 0;
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)
354 return;
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);
378 return unscrolled;
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())
392 return;
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());
412 if (!scrollable())
413 return;
415 // Pending tree should never have sent scroll deltas.
416 DCHECK(sent_scroll_delta().IsZero());
418 LayerImpl* active_twin = layer_tree_impl()->FindActiveTreeLayerById(id());
419 if (active_twin) {
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,
453 screen_space_point,
454 &clipped);
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());
459 if (!clipped &&
460 non_fast_scrollable_region().Contains(
461 gfx::ToRoundedPoint(hit_test_point_in_layer_space))) {
462 TRACE_EVENT0("cc",
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;
473 if (!scrollable()) {
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) {
480 TRACE_EVENT0("cc",
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();
565 ++it) {
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);
573 } else {
574 layer->SetScrollChildren(nullptr);
577 LayerImpl* clip_parent = nullptr;
578 if (clip_parent_) {
579 clip_parent = layer->layer_tree_impl()->LayerById(
580 clip_parent_->id());
581 DCHECK(clip_parent);
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);
591 } else {
592 layer->SetClipChildren(nullptr);
595 layer->PassCopyRequests(&copy_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
622 // the page scale.
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_);
673 if (scrollable())
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);
690 return result;
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();
737 if (render_surface_)
738 render_surface_->ResetPropertyChangedFlag();
740 if (mask_layer_)
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) {
760 SetFilters(filters);
763 void LayerImpl::OnOpacityAnimated(float opacity) {
764 SetOpacity(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.
775 if (!IsActive())
776 return;
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)
802 return;
804 bounds_ = bounds;
806 ScrollbarParametersDidChange(true);
807 if (masks_to_bounds())
808 NoteLayerPropertyChangedForSubtree();
809 else
810 NoteLayerPropertyChanged();
813 void LayerImpl::SetBoundsDelta(const gfx::Vector2dF& bounds_delta) {
814 if (bounds_delta_ == bounds_delta)
815 return;
817 bounds_delta_ = bounds_delta;
819 ScrollbarParametersDidChange(true);
820 if (masks_to_bounds())
821 NoteLayerPropertyChangedForSubtree();
822 else
823 NoteLayerPropertyChanged();
826 void LayerImpl::SetMaskLayer(scoped_ptr<LayerImpl> mask_layer) {
827 int new_layer_id = mask_layer ? mask_layer->id() : -1;
829 if (mask_layer) {
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_) {
833 return;
836 mask_layer_ = mask_layer.Pass();
837 mask_layer_id_ = new_layer_id;
838 if (mask_layer_)
839 mask_layer_->SetParent(this);
840 NoteLayerPropertyChangedForSubtree();
843 scoped_ptr<LayerImpl> LayerImpl::TakeMaskLayer() {
844 mask_layer_id_ = -1;
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;
851 if (replica_layer) {
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_) {
855 return;
858 replica_layer_ = replica_layer.Pass();
859 replica_layer_id_ = new_layer_id;
860 if (replica_layer_)
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() {
871 return nullptr;
874 void LayerImpl::SetDrawsContent(bool draws_content) {
875 if (draws_content_ == draws_content)
876 return;
878 draws_content_ = draws_content;
879 NoteLayerPropertyChanged();
882 void LayerImpl::SetHideLayerAndSubtree(bool hide) {
883 if (hide_layer_and_subtree_ == hide)
884 return;
886 hide_layer_and_subtree_ = hide;
887 NoteLayerPropertyChangedForSubtree();
890 void LayerImpl::SetTransformOrigin(const gfx::Point3F& transform_origin) {
891 if (transform_origin_ == transform_origin)
892 return;
893 transform_origin_ = transform_origin;
894 NoteLayerPropertyChangedForSubtree();
897 void LayerImpl::SetBackgroundColor(SkColor background_color) {
898 if (background_color_ == background_color)
899 return;
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)
914 break;
916 if (SkColorGetA(color) != 255)
917 color = layer_tree_impl()->background_color();
918 if (SkColorGetA(color) != 255)
919 color = SkColorSetA(color, 255);
921 return color;
924 void LayerImpl::SetFilters(const FilterOperations& filters) {
925 if (filters_ == filters)
926 return;
928 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)
945 return;
947 background_filters_ = filters;
948 NoteLayerPropertyChanged();
951 void LayerImpl::SetMasksToBounds(bool masks_to_bounds) {
952 if (masks_to_bounds_ == masks_to_bounds)
953 return;
955 masks_to_bounds_ = masks_to_bounds;
956 NoteLayerPropertyChangedForSubtree();
959 void LayerImpl::SetContentsOpaque(bool opaque) {
960 if (contents_opaque_ == opaque)
961 return;
963 contents_opaque_ = opaque;
964 NoteLayerPropertyChangedForSubtree();
967 void LayerImpl::SetOpacity(float opacity) {
968 if (opacity_ == opacity)
969 return;
971 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)
987 return;
989 blend_mode_ = blend_mode;
990 NoteLayerPropertyChangedForSubtree();
993 void LayerImpl::SetIsRootForIsolatedGroup(bool root) {
994 if (is_root_for_isolated_group_ == root)
995 return;
997 is_root_for_isolated_group_ = root;
998 SetNeedsPushProperties();
1001 void LayerImpl::SetPosition(const gfx::PointF& position) {
1002 if (position_ == position)
1003 return;
1005 position_ = position;
1006 NoteLayerPropertyChangedForSubtree();
1009 void LayerImpl::SetShouldFlattenTransform(bool flatten) {
1010 if (should_flatten_transform_ == flatten)
1011 return;
1013 should_flatten_transform_ = flatten;
1014 NoteLayerPropertyChangedForSubtree();
1017 void LayerImpl::Set3dSortingContextId(int id) {
1018 if (id == sorting_context_id_)
1019 return;
1020 sorting_context_id_ = id;
1021 NoteLayerPropertyChangedForSubtree();
1024 void LayerImpl::SetTransform(const gfx::Transform& transform) {
1025 if (transform_ == transform)
1026 return;
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";
1038 return;
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)
1066 return;
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)
1076 return;
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(
1089 scroll_offset_);
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) {
1118 changed = true;
1119 scroll_offset_ = scroll_offset;
1121 if (scroll_offset_delegate_)
1122 scroll_offset_delegate_->SetTotalScrollOffset(TotalScrollOffset());
1125 if (ScrollDelta() != scroll_delta) {
1126 changed = true;
1127 if (layer_tree_impl()->IsActiveTree()) {
1128 LayerImpl* pending_twin =
1129 layer_tree_impl()->FindPendingTreeLayerById(id());
1130 if (pending_twin) {
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));
1144 } else {
1145 scroll_delta_ = scroll_delta;
1149 if (changed) {
1150 if (scroll_offset_delegate_)
1151 scroll_offset_delegate_->Update();
1152 DidScroll();
1156 gfx::Vector2dF LayerImpl::ScrollDelta() const {
1157 if (scroll_offset_delegate_) {
1158 return scroll_offset_delegate_->GetTotalScrollOffset().DeltaFrom(
1159 scroll_offset_);
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)
1174 return;
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();
1212 } else {
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
1226 // necessary.
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());
1240 return max_offset;
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())
1252 ScrollBy(delta);
1254 return delta;
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
1269 // bounds here.
1270 gfx::RectF scroll_rect(gfx::PointF(), BoundsForScrolling());
1272 if (scroll_rect.size().IsEmpty())
1273 return;
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);
1288 } else {
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
1301 // necessary.
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);
1318 } else {
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) {
1351 return;
1354 bool need_scrollbar_animation_controller = scrollable() && scrollbars_;
1355 if (!need_scrollbar_animation_controller) {
1356 scrollbar_animation_controller_ = nullptr;
1357 return;
1360 if (scrollbar_animation_controller_)
1361 return;
1363 scrollbar_animation_controller_ =
1364 layer_tree_impl_->CreateScrollbarAnimationController(this);
1367 void LayerImpl::ClearScrollbars() {
1368 if (!scrollbars_)
1369 return;
1371 scrollbars_.reset(nullptr);
1374 void LayerImpl::AddScrollbar(ScrollbarLayerImplBase* layer) {
1375 DCHECK(layer);
1376 DCHECK(!scrollbars_ || scrollbars_->find(layer) == scrollbars_->end());
1377 if (!scrollbars_)
1378 scrollbars_.reset(new ScrollbarSet());
1380 scrollbars_->insert(layer);
1383 void LayerImpl::RemoveScrollbar(ScrollbarLayerImplBase* layer) {
1384 DCHECK(scrollbars_);
1385 DCHECK(layer);
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 {
1394 if (!scrollbars_)
1395 return false;
1397 for (ScrollbarSet::iterator it = scrollbars_->begin();
1398 it != scrollbars_->end();
1399 ++it)
1400 if ((*it)->orientation() == orientation)
1401 return true;
1403 return false;
1406 void LayerImpl::ScrollbarParametersDidChange(bool on_resize) {
1407 if (!scrollbars_)
1408 return;
1410 for (ScrollbarSet::iterator it = scrollbars_->begin();
1411 it != scrollbars_->end();
1412 ++it) {
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_)
1421 return;
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"),
1450 state,
1451 "cc::LayerImpl",
1452 LayerTypeAsString(),
1453 this);
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);
1463 state->EndArray();
1465 state->SetInteger("draws_content", DrawsContent());
1466 state->SetInteger("gpu_memory_usage", GPUMemoryUsageInBytes());
1468 state->BeginArray("scroll_offset");
1469 MathUtil::AddToTracedValue(scroll_offset_, state);
1470 state->EndArray();
1472 state->BeginArray("transform_origin");
1473 MathUtil::AddToTracedValue(transform_origin_, state);
1474 state->EndArray();
1476 bool clipped;
1477 gfx::QuadF layer_quad = MathUtil::MapQuad(
1478 screen_space_transform(),
1479 gfx::QuadF(gfx::Rect(content_bounds())),
1480 &clipped);
1481 state->BeginArray("layer_quad");
1482 MathUtil::AddToTracedValue(layer_quad, state);
1483 state->EndArray();
1484 if (!touch_event_handler_region_.IsEmpty()) {
1485 state->BeginArray("touch_event_handler_region");
1486 touch_event_handler_region_.AsValueInto(state);
1487 state->EndArray();
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);
1494 state->EndArray();
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);
1501 state->EndArray();
1503 if (!non_fast_scrollable_region_.IsEmpty()) {
1504 state->BeginArray("non_fast_scrollable_region");
1505 non_fast_scrollable_region_.AsValueInto(state);
1506 state->EndArray();
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();
1515 state->EndArray();
1516 if (mask_layer_) {
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();
1527 if (scroll_parent_)
1528 state->SetInteger("scroll_parent", scroll_parent_->id());
1530 if (clip_parent_)
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());
1536 state->SetBoolean(
1537 "has_animation_bounds",
1538 layer_animation_controller()->HasAnimationThatInflatesBounds());
1540 gfx::BoxF box;
1541 if (LayerUtils::GetAnimationBounds(*this, &box)) {
1542 state->BeginArray("animation_bounds");
1543 MathUtil::AddToTracedValue(box, state);
1544 state->EndArray();
1547 if (debug_info_.get()) {
1548 std::string str;
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();
1559 it.Advance()) {
1560 state->SetValue(it.key().data(), it.value().DeepCopy());
1562 } else {
1563 NOTREACHED();
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,
1586 int group) {
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)
1593 return;
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));
1599 return;
1601 render_surface_.reset();
1604 } // namespace cc