Delete chrome.mediaGalleriesPrivate because the functionality unique to it has since...
[chromium-blink-merge.git] / cc / layers / layer_impl.cc
blob5bef67d23c99a6dac082b50896964399e9f22df0
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_animation_controller_->scroll_offset_animation_was_interrupted()
548 ? gfx::Vector2dF()
549 : layer->ScrollDelta() - layer->sent_scroll_delta();
551 layer->SetSentScrollDelta(gfx::Vector2dF());
552 layer->SetScrollOffsetAndDelta(scroll_offset_, remaining_delta);
554 layer->Set3dSortingContextId(sorting_context_id_);
555 layer->SetNumDescendantsThatDrawContent(num_descendants_that_draw_content_);
557 LayerImpl* scroll_parent = nullptr;
558 if (scroll_parent_) {
559 scroll_parent = layer->layer_tree_impl()->LayerById(scroll_parent_->id());
560 DCHECK(scroll_parent);
563 layer->SetScrollParent(scroll_parent);
564 if (scroll_children_) {
565 std::set<LayerImpl*>* scroll_children = new std::set<LayerImpl*>;
566 for (std::set<LayerImpl*>::iterator it = scroll_children_->begin();
567 it != scroll_children_->end();
568 ++it) {
569 DCHECK_EQ((*it)->scroll_parent(), this);
570 LayerImpl* scroll_child =
571 layer->layer_tree_impl()->LayerById((*it)->id());
572 DCHECK(scroll_child);
573 scroll_children->insert(scroll_child);
575 layer->SetScrollChildren(scroll_children);
576 } else {
577 layer->SetScrollChildren(nullptr);
580 LayerImpl* clip_parent = nullptr;
581 if (clip_parent_) {
582 clip_parent = layer->layer_tree_impl()->LayerById(
583 clip_parent_->id());
584 DCHECK(clip_parent);
587 layer->SetClipParent(clip_parent);
588 if (clip_children_) {
589 std::set<LayerImpl*>* clip_children = new std::set<LayerImpl*>;
590 for (std::set<LayerImpl*>::iterator it = clip_children_->begin();
591 it != clip_children_->end(); ++it)
592 clip_children->insert(layer->layer_tree_impl()->LayerById((*it)->id()));
593 layer->SetClipChildren(clip_children);
594 } else {
595 layer->SetClipChildren(nullptr);
598 layer->PassCopyRequests(&copy_requests_);
600 // If the main thread commits multiple times before the impl thread actually
601 // draws, then damage tracking will become incorrect if we simply clobber the
602 // update_rect here. The LayerImpl's update_rect needs to accumulate (i.e.
603 // union) any update changes that have occurred on the main thread.
604 update_rect_.Union(layer->update_rect());
605 layer->SetUpdateRect(update_rect_);
607 layer->SetStackingOrderChanged(stacking_order_changed_);
608 layer->SetDebugInfo(debug_info_);
610 // Reset any state that should be cleared for the next update.
611 stacking_order_changed_ = false;
612 update_rect_ = gfx::Rect();
613 needs_push_properties_ = false;
614 num_dependents_need_push_properties_ = 0;
617 gfx::Vector2dF LayerImpl::FixedContainerSizeDelta() const {
618 if (!scroll_clip_layer_)
619 return gfx::Vector2dF();
621 gfx::Vector2dF delta_from_scroll = scroll_clip_layer_->bounds_delta();
623 // In virtual-viewport mode, we don't need to compensate for pinch zoom or
624 // scale since the fixed container is the outer viewport, which sits below
625 // the page scale.
626 if (layer_tree_impl()->settings().use_pinch_virtual_viewport)
627 return delta_from_scroll;
629 float scale_delta = layer_tree_impl()->page_scale_delta();
630 float scale = layer_tree_impl()->current_page_scale_factor() /
631 layer_tree_impl()->page_scale_delta();
633 delta_from_scroll.Scale(1.f / scale);
635 // The delta-from-pinch component requires some explanation: A viewport of
636 // size (w,h) will appear to be size (w/s,h/s) under scale s in the content
637 // space. If s -> s' on the impl thread, where s' = s * ds, then the apparent
638 // viewport size change in the content space due to ds is:
640 // (w/s',h/s') - (w/s,h/s) = (w,h)(1/s' - 1/s) = (w,h)(1 - ds)/(s ds)
642 gfx::Vector2dF delta_from_pinch =
643 gfx::Rect(scroll_clip_layer_->bounds()).bottom_right() - gfx::PointF();
644 delta_from_pinch.Scale((1.f - scale_delta) / (scale * scale_delta));
646 return delta_from_scroll + delta_from_pinch;
649 base::DictionaryValue* LayerImpl::LayerTreeAsJson() const {
650 base::DictionaryValue* result = new base::DictionaryValue;
651 result->SetString("LayerType", LayerTypeAsString());
653 base::ListValue* list = new base::ListValue;
654 list->AppendInteger(bounds().width());
655 list->AppendInteger(bounds().height());
656 result->Set("Bounds", list);
658 list = new base::ListValue;
659 list->AppendDouble(position_.x());
660 list->AppendDouble(position_.y());
661 result->Set("Position", list);
663 const gfx::Transform& gfx_transform = draw_properties_.target_space_transform;
664 double transform[16];
665 gfx_transform.matrix().asColMajord(transform);
666 list = new base::ListValue;
667 for (int i = 0; i < 16; ++i)
668 list->AppendDouble(transform[i]);
669 result->Set("DrawTransform", list);
671 result->SetBoolean("DrawsContent", draws_content_);
672 result->SetBoolean("Is3dSorted", Is3dSorted());
673 result->SetDouble("Opacity", opacity());
674 result->SetBoolean("ContentsOpaque", contents_opaque_);
676 if (scrollable())
677 result->SetBoolean("Scrollable", true);
679 if (have_wheel_event_handlers_)
680 result->SetBoolean("WheelHandler", have_wheel_event_handlers_);
681 if (have_scroll_event_handlers_)
682 result->SetBoolean("ScrollHandler", have_scroll_event_handlers_);
683 if (!touch_event_handler_region_.IsEmpty()) {
684 scoped_ptr<base::Value> region = touch_event_handler_region_.AsValue();
685 result->Set("TouchRegion", region.release());
688 list = new base::ListValue;
689 for (size_t i = 0; i < children_.size(); ++i)
690 list->Append(children_[i]->LayerTreeAsJson());
691 result->Set("Children", list);
693 return result;
696 void LayerImpl::SetStackingOrderChanged(bool stacking_order_changed) {
697 if (stacking_order_changed) {
698 stacking_order_changed_ = true;
699 NoteLayerPropertyChangedForSubtree();
703 void LayerImpl::NoteLayerPropertyChanged() {
704 layer_property_changed_ = true;
705 layer_tree_impl()->set_needs_update_draw_properties();
706 SetNeedsPushProperties();
709 void LayerImpl::NoteLayerPropertyChangedForSubtree() {
710 layer_property_changed_ = true;
711 layer_tree_impl()->set_needs_update_draw_properties();
712 for (size_t i = 0; i < children_.size(); ++i)
713 children_[i]->NoteLayerPropertyChangedForDescendantsInternal();
714 SetNeedsPushProperties();
717 void LayerImpl::NoteLayerPropertyChangedForDescendantsInternal() {
718 layer_property_changed_ = true;
719 for (size_t i = 0; i < children_.size(); ++i)
720 children_[i]->NoteLayerPropertyChangedForDescendantsInternal();
723 void LayerImpl::NoteLayerPropertyChangedForDescendants() {
724 layer_tree_impl()->set_needs_update_draw_properties();
725 for (size_t i = 0; i < children_.size(); ++i)
726 children_[i]->NoteLayerPropertyChangedForDescendantsInternal();
727 SetNeedsPushProperties();
730 const char* LayerImpl::LayerTypeAsString() const {
731 return "cc::LayerImpl";
734 void LayerImpl::ResetAllChangeTrackingForSubtree() {
735 layer_property_changed_ = false;
737 update_rect_ = gfx::Rect();
738 damage_rect_ = gfx::RectF();
740 if (render_surface_)
741 render_surface_->ResetPropertyChangedFlag();
743 if (mask_layer_)
744 mask_layer_->ResetAllChangeTrackingForSubtree();
746 if (replica_layer_) {
747 // This also resets the replica mask, if it exists.
748 replica_layer_->ResetAllChangeTrackingForSubtree();
751 for (size_t i = 0; i < children_.size(); ++i)
752 children_[i]->ResetAllChangeTrackingForSubtree();
754 needs_push_properties_ = false;
755 num_dependents_need_push_properties_ = 0;
758 gfx::ScrollOffset LayerImpl::ScrollOffsetForAnimation() const {
759 return TotalScrollOffset();
762 void LayerImpl::OnFilterAnimated(const FilterOperations& filters) {
763 SetFilters(filters);
766 void LayerImpl::OnOpacityAnimated(float opacity) {
767 SetOpacity(opacity);
770 void LayerImpl::OnTransformAnimated(const gfx::Transform& transform) {
771 SetTransform(transform);
774 void LayerImpl::OnScrollOffsetAnimated(const gfx::ScrollOffset& scroll_offset) {
775 // Only layers in the active tree should need to do anything here, since
776 // layers in the pending tree will find out about these changes as a
777 // result of the call to SetScrollDelta.
778 if (!IsActive())
779 return;
781 SetScrollDelta(scroll_offset.DeltaFrom(scroll_offset_));
783 layer_tree_impl_->DidAnimateScrollOffset();
786 void LayerImpl::OnAnimationWaitingForDeletion() {}
788 bool LayerImpl::IsActive() const {
789 return layer_tree_impl_->IsActiveTree();
792 gfx::Size LayerImpl::bounds() const {
793 gfx::Vector2d delta = gfx::ToCeiledVector2d(bounds_delta_);
794 return gfx::Size(bounds_.width() + delta.x(),
795 bounds_.height() + delta.y());
798 gfx::SizeF LayerImpl::BoundsForScrolling() const {
799 return gfx::SizeF(bounds_.width() + bounds_delta_.x(),
800 bounds_.height() + bounds_delta_.y());
803 void LayerImpl::SetBounds(const gfx::Size& bounds) {
804 if (bounds_ == bounds)
805 return;
807 bounds_ = bounds;
809 ScrollbarParametersDidChange(true);
810 if (masks_to_bounds())
811 NoteLayerPropertyChangedForSubtree();
812 else
813 NoteLayerPropertyChanged();
816 void LayerImpl::SetBoundsDelta(const gfx::Vector2dF& bounds_delta) {
817 if (bounds_delta_ == bounds_delta)
818 return;
820 bounds_delta_ = bounds_delta;
822 ScrollbarParametersDidChange(true);
823 if (masks_to_bounds())
824 NoteLayerPropertyChangedForSubtree();
825 else
826 NoteLayerPropertyChanged();
829 void LayerImpl::SetMaskLayer(scoped_ptr<LayerImpl> mask_layer) {
830 int new_layer_id = mask_layer ? mask_layer->id() : -1;
832 if (mask_layer) {
833 DCHECK_EQ(layer_tree_impl(), mask_layer->layer_tree_impl());
834 DCHECK_NE(new_layer_id, mask_layer_id_);
835 } else if (new_layer_id == mask_layer_id_) {
836 return;
839 mask_layer_ = mask_layer.Pass();
840 mask_layer_id_ = new_layer_id;
841 if (mask_layer_)
842 mask_layer_->SetParent(this);
843 NoteLayerPropertyChangedForSubtree();
846 scoped_ptr<LayerImpl> LayerImpl::TakeMaskLayer() {
847 mask_layer_id_ = -1;
848 return mask_layer_.Pass();
851 void LayerImpl::SetReplicaLayer(scoped_ptr<LayerImpl> replica_layer) {
852 int new_layer_id = replica_layer ? replica_layer->id() : -1;
854 if (replica_layer) {
855 DCHECK_EQ(layer_tree_impl(), replica_layer->layer_tree_impl());
856 DCHECK_NE(new_layer_id, replica_layer_id_);
857 } else if (new_layer_id == replica_layer_id_) {
858 return;
861 replica_layer_ = replica_layer.Pass();
862 replica_layer_id_ = new_layer_id;
863 if (replica_layer_)
864 replica_layer_->SetParent(this);
865 NoteLayerPropertyChangedForSubtree();
868 scoped_ptr<LayerImpl> LayerImpl::TakeReplicaLayer() {
869 replica_layer_id_ = -1;
870 return replica_layer_.Pass();
873 ScrollbarLayerImplBase* LayerImpl::ToScrollbarLayer() {
874 return nullptr;
877 void LayerImpl::SetDrawsContent(bool draws_content) {
878 if (draws_content_ == draws_content)
879 return;
881 draws_content_ = draws_content;
882 NoteLayerPropertyChanged();
885 void LayerImpl::SetHideLayerAndSubtree(bool hide) {
886 if (hide_layer_and_subtree_ == hide)
887 return;
889 hide_layer_and_subtree_ = hide;
890 NoteLayerPropertyChangedForSubtree();
893 void LayerImpl::SetTransformOrigin(const gfx::Point3F& transform_origin) {
894 if (transform_origin_ == transform_origin)
895 return;
896 transform_origin_ = transform_origin;
897 NoteLayerPropertyChangedForSubtree();
900 void LayerImpl::SetBackgroundColor(SkColor background_color) {
901 if (background_color_ == background_color)
902 return;
904 background_color_ = background_color;
905 NoteLayerPropertyChanged();
908 SkColor LayerImpl::SafeOpaqueBackgroundColor() const {
909 SkColor color = background_color();
910 if (SkColorGetA(color) == 255 && !contents_opaque()) {
911 color = SK_ColorTRANSPARENT;
912 } else if (SkColorGetA(color) != 255 && contents_opaque()) {
913 for (const LayerImpl* layer = parent(); layer;
914 layer = layer->parent()) {
915 color = layer->background_color();
916 if (SkColorGetA(color) == 255)
917 break;
919 if (SkColorGetA(color) != 255)
920 color = layer_tree_impl()->background_color();
921 if (SkColorGetA(color) != 255)
922 color = SkColorSetA(color, 255);
924 return color;
927 void LayerImpl::SetFilters(const FilterOperations& filters) {
928 if (filters_ == filters)
929 return;
931 filters_ = filters;
932 NoteLayerPropertyChangedForSubtree();
935 bool LayerImpl::FilterIsAnimating() const {
936 return layer_animation_controller_->IsAnimatingProperty(Animation::Filter);
939 bool LayerImpl::FilterIsAnimatingOnImplOnly() const {
940 Animation* filter_animation =
941 layer_animation_controller_->GetAnimation(Animation::Filter);
942 return filter_animation && filter_animation->is_impl_only();
945 void LayerImpl::SetBackgroundFilters(
946 const FilterOperations& filters) {
947 if (background_filters_ == filters)
948 return;
950 background_filters_ = filters;
951 NoteLayerPropertyChanged();
954 void LayerImpl::SetMasksToBounds(bool masks_to_bounds) {
955 if (masks_to_bounds_ == masks_to_bounds)
956 return;
958 masks_to_bounds_ = masks_to_bounds;
959 NoteLayerPropertyChangedForSubtree();
962 void LayerImpl::SetContentsOpaque(bool opaque) {
963 if (contents_opaque_ == opaque)
964 return;
966 contents_opaque_ = opaque;
967 NoteLayerPropertyChangedForSubtree();
970 void LayerImpl::SetOpacity(float opacity) {
971 if (opacity_ == opacity)
972 return;
974 opacity_ = opacity;
975 NoteLayerPropertyChangedForSubtree();
978 bool LayerImpl::OpacityIsAnimating() const {
979 return layer_animation_controller_->IsAnimatingProperty(Animation::Opacity);
982 bool LayerImpl::OpacityIsAnimatingOnImplOnly() const {
983 Animation* opacity_animation =
984 layer_animation_controller_->GetAnimation(Animation::Opacity);
985 return opacity_animation && opacity_animation->is_impl_only();
988 void LayerImpl::SetBlendMode(SkXfermode::Mode blend_mode) {
989 if (blend_mode_ == blend_mode)
990 return;
992 blend_mode_ = blend_mode;
993 NoteLayerPropertyChangedForSubtree();
996 void LayerImpl::SetIsRootForIsolatedGroup(bool root) {
997 if (is_root_for_isolated_group_ == root)
998 return;
1000 is_root_for_isolated_group_ = root;
1001 SetNeedsPushProperties();
1004 void LayerImpl::SetPosition(const gfx::PointF& position) {
1005 if (position_ == position)
1006 return;
1008 position_ = position;
1009 NoteLayerPropertyChangedForSubtree();
1012 void LayerImpl::SetShouldFlattenTransform(bool flatten) {
1013 if (should_flatten_transform_ == flatten)
1014 return;
1016 should_flatten_transform_ = flatten;
1017 NoteLayerPropertyChangedForSubtree();
1020 void LayerImpl::Set3dSortingContextId(int id) {
1021 if (id == sorting_context_id_)
1022 return;
1023 sorting_context_id_ = id;
1024 NoteLayerPropertyChangedForSubtree();
1027 void LayerImpl::SetTransform(const gfx::Transform& transform) {
1028 if (transform_ == transform)
1029 return;
1031 transform_ = transform;
1032 transform_is_invertible_ = transform_.IsInvertible();
1033 NoteLayerPropertyChangedForSubtree();
1036 void LayerImpl::SetTransformAndInvertibility(const gfx::Transform& transform,
1037 bool transform_is_invertible) {
1038 if (transform_ == transform) {
1039 DCHECK(transform_is_invertible_ == transform_is_invertible)
1040 << "Can't change invertibility if transform is unchanged";
1041 return;
1043 transform_ = transform;
1044 transform_is_invertible_ = transform_is_invertible;
1045 NoteLayerPropertyChangedForSubtree();
1048 bool LayerImpl::TransformIsAnimating() const {
1049 return layer_animation_controller_->IsAnimatingProperty(Animation::Transform);
1052 bool LayerImpl::TransformIsAnimatingOnImplOnly() const {
1053 Animation* transform_animation =
1054 layer_animation_controller_->GetAnimation(Animation::Transform);
1055 return transform_animation && transform_animation->is_impl_only();
1058 void LayerImpl::SetUpdateRect(const gfx::Rect& update_rect) {
1059 update_rect_ = update_rect;
1060 SetNeedsPushProperties();
1063 void LayerImpl::AddDamageRect(const gfx::RectF& damage_rect) {
1064 damage_rect_ = gfx::UnionRects(damage_rect_, damage_rect);
1067 void LayerImpl::SetContentBounds(const gfx::Size& content_bounds) {
1068 if (this->content_bounds() == content_bounds)
1069 return;
1071 draw_properties_.content_bounds = content_bounds;
1072 NoteLayerPropertyChanged();
1075 void LayerImpl::SetContentsScale(float contents_scale_x,
1076 float contents_scale_y) {
1077 if (this->contents_scale_x() == contents_scale_x &&
1078 this->contents_scale_y() == contents_scale_y)
1079 return;
1081 draw_properties_.contents_scale_x = contents_scale_x;
1082 draw_properties_.contents_scale_y = contents_scale_y;
1083 NoteLayerPropertyChanged();
1086 void LayerImpl::SetScrollOffsetDelegate(
1087 ScrollOffsetDelegate* scroll_offset_delegate) {
1088 // Having both a scroll parent and a scroll offset delegate is unsupported.
1089 DCHECK(!scroll_parent_);
1090 if (!scroll_offset_delegate && scroll_offset_delegate_) {
1091 scroll_delta_ = scroll_offset_delegate_->GetTotalScrollOffset().DeltaFrom(
1092 scroll_offset_);
1094 gfx::ScrollOffset total_offset = TotalScrollOffset();
1095 scroll_offset_delegate_ = scroll_offset_delegate;
1096 if (scroll_offset_delegate_)
1097 scroll_offset_delegate_->SetTotalScrollOffset(total_offset);
1100 bool LayerImpl::IsExternalFlingActive() const {
1101 return scroll_offset_delegate_ &&
1102 scroll_offset_delegate_->IsExternalFlingActive();
1105 void LayerImpl::DidScroll() {
1106 NoteLayerPropertyChangedForSubtree();
1107 ScrollbarParametersDidChange(false);
1110 void LayerImpl::SetScrollOffset(const gfx::ScrollOffset& scroll_offset) {
1111 SetScrollOffsetAndDelta(scroll_offset, ScrollDelta());
1114 void LayerImpl::SetScrollOffsetAndDelta(const gfx::ScrollOffset& scroll_offset,
1115 const gfx::Vector2dF& scroll_delta) {
1116 bool changed = false;
1118 last_scroll_offset_ = scroll_offset;
1120 if (scroll_offset_ != scroll_offset) {
1121 changed = true;
1122 scroll_offset_ = scroll_offset;
1124 if (scroll_offset_delegate_)
1125 scroll_offset_delegate_->SetTotalScrollOffset(TotalScrollOffset());
1128 if (ScrollDelta() != scroll_delta) {
1129 changed = true;
1130 if (layer_tree_impl()->IsActiveTree()) {
1131 LayerImpl* pending_twin =
1132 layer_tree_impl()->FindPendingTreeLayerById(id());
1133 if (pending_twin) {
1134 // The pending twin can't mirror the scroll delta of the active
1135 // layer. Although the delta - sent scroll delta difference is
1136 // identical for both twins, the sent scroll delta for the pending
1137 // layer is zero, as anything that has been sent has been baked
1138 // into the layer's position/scroll offset as a part of commit.
1139 DCHECK(pending_twin->sent_scroll_delta().IsZero());
1140 pending_twin->SetScrollDelta(scroll_delta - sent_scroll_delta());
1144 if (scroll_offset_delegate_) {
1145 scroll_offset_delegate_->SetTotalScrollOffset(
1146 ScrollOffsetWithDelta(scroll_offset_, scroll_delta));
1147 } else {
1148 scroll_delta_ = scroll_delta;
1152 if (changed) {
1153 if (scroll_offset_delegate_)
1154 scroll_offset_delegate_->Update();
1155 DidScroll();
1159 gfx::Vector2dF LayerImpl::ScrollDelta() const {
1160 if (scroll_offset_delegate_) {
1161 return scroll_offset_delegate_->GetTotalScrollOffset().DeltaFrom(
1162 scroll_offset_);
1164 return scroll_delta_;
1167 void LayerImpl::SetScrollDelta(const gfx::Vector2dF& scroll_delta) {
1168 SetScrollOffsetAndDelta(scroll_offset_, scroll_delta);
1171 gfx::ScrollOffset LayerImpl::TotalScrollOffset() const {
1172 return ScrollOffsetWithDelta(scroll_offset_, ScrollDelta());
1175 void LayerImpl::SetDoubleSided(bool double_sided) {
1176 if (double_sided_ == double_sided)
1177 return;
1179 double_sided_ = double_sided;
1180 NoteLayerPropertyChangedForSubtree();
1183 SimpleEnclosedRegion LayerImpl::VisibleContentOpaqueRegion() const {
1184 if (contents_opaque())
1185 return SimpleEnclosedRegion(visible_content_rect());
1186 return SimpleEnclosedRegion();
1189 void LayerImpl::DidBeginTracing() {}
1191 void LayerImpl::ReleaseResources() {}
1193 gfx::ScrollOffset LayerImpl::MaxScrollOffset() const {
1194 if (!scroll_clip_layer_ || bounds().IsEmpty())
1195 return gfx::ScrollOffset();
1197 LayerImpl const* page_scale_layer = layer_tree_impl()->page_scale_layer();
1198 DCHECK(this != page_scale_layer);
1199 DCHECK(this != layer_tree_impl()->InnerViewportScrollLayer() ||
1200 IsContainerForFixedPositionLayers());
1202 gfx::SizeF scaled_scroll_bounds(BoundsForScrolling());
1204 float scale_factor = 1.f;
1205 for (LayerImpl const* current_layer = this;
1206 current_layer != scroll_clip_layer_;
1207 current_layer = current_layer->parent()) {
1208 DCHECK(current_layer);
1209 float current_layer_scale = 1.f;
1211 const gfx::Transform& layer_transform = current_layer->transform();
1212 if (current_layer == page_scale_layer) {
1213 DCHECK(layer_transform.IsIdentity());
1214 current_layer_scale = layer_tree_impl()->current_page_scale_factor();
1215 } else {
1216 // TODO(wjmaclean) Should we allow for translation too?
1217 DCHECK(layer_transform.IsScale2d());
1218 gfx::Vector2dF layer_scale = layer_transform.Scale2d();
1219 // TODO(wjmaclean) Allow for non-isotropic scales.
1220 DCHECK(layer_scale.x() == layer_scale.y());
1221 current_layer_scale = layer_scale.x();
1224 scale_factor *= current_layer_scale;
1226 // TODO(wjmaclean) Once we move to a model where the two-viewport model is
1227 // turned on in all builds, remove the next two lines. For now however, the
1228 // page scale layer may coincide with the clip layer, and so this is
1229 // necessary.
1230 if (page_scale_layer == scroll_clip_layer_)
1231 scale_factor *= layer_tree_impl()->current_page_scale_factor();
1233 scaled_scroll_bounds.SetSize(scale_factor * scaled_scroll_bounds.width(),
1234 scale_factor * scaled_scroll_bounds.height());
1235 scaled_scroll_bounds = gfx::ToFlooredSize(scaled_scroll_bounds);
1237 gfx::ScrollOffset max_offset(
1238 scaled_scroll_bounds.width() - scroll_clip_layer_->bounds().width(),
1239 scaled_scroll_bounds.height() - scroll_clip_layer_->bounds().height());
1240 // We need the final scroll offset to be in CSS coords.
1241 max_offset.Scale(1 / scale_factor);
1242 max_offset.SetToMax(gfx::ScrollOffset());
1243 return max_offset;
1246 gfx::Vector2dF LayerImpl::ClampScrollToMaxScrollOffset() {
1247 gfx::ScrollOffset max_offset = MaxScrollOffset();
1248 gfx::ScrollOffset old_offset = TotalScrollOffset();
1249 gfx::ScrollOffset clamped_offset = old_offset;
1251 clamped_offset.SetToMin(max_offset);
1252 clamped_offset.SetToMax(gfx::ScrollOffset());
1253 gfx::Vector2dF delta = clamped_offset.DeltaFrom(old_offset);
1254 if (!delta.IsZero())
1255 ScrollBy(delta);
1257 return delta;
1260 void LayerImpl::SetScrollbarPosition(ScrollbarLayerImplBase* scrollbar_layer,
1261 LayerImpl* scrollbar_clip_layer,
1262 bool on_resize) const {
1263 DCHECK(scrollbar_layer);
1264 LayerImpl* page_scale_layer = layer_tree_impl()->page_scale_layer();
1266 DCHECK(this != page_scale_layer);
1267 DCHECK(scrollbar_clip_layer);
1268 gfx::RectF clip_rect(gfx::PointF(),
1269 scrollbar_clip_layer->BoundsForScrolling());
1271 // See comment in MaxScrollOffset() regarding the use of the content layer
1272 // bounds here.
1273 gfx::RectF scroll_rect(gfx::PointF(), BoundsForScrolling());
1275 if (scroll_rect.size().IsEmpty())
1276 return;
1278 // TODO(wjmaclean) This computation is nearly identical to the one in
1279 // MaxScrollOffset. Find some way to combine these.
1280 gfx::ScrollOffset current_offset;
1281 for (LayerImpl const* current_layer = this;
1282 current_layer != scrollbar_clip_layer;
1283 current_layer = current_layer->parent()) {
1284 DCHECK(current_layer);
1285 const gfx::Transform& layer_transform = current_layer->transform();
1286 if (current_layer == page_scale_layer) {
1287 DCHECK(layer_transform.IsIdentity());
1288 float scale_factor = layer_tree_impl()->current_page_scale_factor();
1289 current_offset.Scale(scale_factor);
1290 scroll_rect.Scale(scale_factor);
1291 } else {
1292 DCHECK(layer_transform.IsScale2d());
1293 gfx::Vector2dF layer_scale = layer_transform.Scale2d();
1294 DCHECK(layer_scale.x() == layer_scale.y());
1295 gfx::ScrollOffset new_offset = ScrollOffsetWithDelta(
1296 current_layer->scroll_offset(), current_layer->ScrollDelta());
1297 new_offset.Scale(layer_scale.x(), layer_scale.y());
1298 current_offset += new_offset;
1301 // TODO(wjmaclean) Once we move to a model where the two-viewport model is
1302 // turned on in all builds, remove the next two lines. For now however, the
1303 // page scale layer may coincide with the clip layer, and so this is
1304 // necessary.
1305 if (page_scale_layer == scrollbar_clip_layer) {
1306 scroll_rect.Scale(layer_tree_impl()->current_page_scale_factor());
1307 current_offset.Scale(layer_tree_impl()->current_page_scale_factor());
1310 bool scrollbar_needs_animation = false;
1311 scrollbar_needs_animation |= scrollbar_layer->SetVerticalAdjust(
1312 scrollbar_clip_layer->bounds_delta().y());
1313 if (scrollbar_layer->orientation() == HORIZONTAL) {
1314 float visible_ratio = clip_rect.width() / scroll_rect.width();
1315 scrollbar_needs_animation |=
1316 scrollbar_layer->SetCurrentPos(current_offset.x());
1317 scrollbar_needs_animation |=
1318 scrollbar_layer->SetMaximum(scroll_rect.width() - clip_rect.width());
1319 scrollbar_needs_animation |=
1320 scrollbar_layer->SetVisibleToTotalLengthRatio(visible_ratio);
1321 } else {
1322 float visible_ratio = clip_rect.height() / scroll_rect.height();
1323 scrollbar_needs_animation |=
1324 scrollbar_layer->SetCurrentPos(current_offset.y());
1325 scrollbar_needs_animation |=
1326 scrollbar_layer->SetMaximum(scroll_rect.height() - clip_rect.height());
1327 scrollbar_needs_animation |=
1328 scrollbar_layer->SetVisibleToTotalLengthRatio(visible_ratio);
1330 if (scrollbar_needs_animation) {
1331 layer_tree_impl()->set_needs_update_draw_properties();
1332 // TODO(wjmaclean) The scrollbar animator for the pinch-zoom scrollbars
1333 // should activate for every scroll on the main frame, not just the
1334 // scrolls that move the pinch virtual viewport (i.e. trigger from
1335 // either inner or outer viewport).
1336 if (scrollbar_animation_controller_) {
1337 // When both non-overlay and overlay scrollbars are both present, don't
1338 // animate the overlay scrollbars when page scale factor is at the min.
1339 // Non-overlay scrollbars also shouldn't trigger animations.
1340 bool is_animatable_scrollbar =
1341 scrollbar_layer->is_overlay_scrollbar() &&
1342 ((layer_tree_impl()->current_page_scale_factor() >
1343 layer_tree_impl()->min_page_scale_factor()) ||
1344 !layer_tree_impl()->settings().use_pinch_zoom_scrollbars);
1345 if (is_animatable_scrollbar)
1346 scrollbar_animation_controller_->DidScrollUpdate(on_resize);
1351 void LayerImpl::DidBecomeActive() {
1352 if (layer_tree_impl_->settings().scrollbar_animator ==
1353 LayerTreeSettings::NoAnimator) {
1354 return;
1357 bool need_scrollbar_animation_controller = scrollable() && scrollbars_;
1358 if (!need_scrollbar_animation_controller) {
1359 scrollbar_animation_controller_ = nullptr;
1360 return;
1363 if (scrollbar_animation_controller_)
1364 return;
1366 scrollbar_animation_controller_ =
1367 layer_tree_impl_->CreateScrollbarAnimationController(this);
1370 void LayerImpl::ClearScrollbars() {
1371 if (!scrollbars_)
1372 return;
1374 scrollbars_.reset(nullptr);
1377 void LayerImpl::AddScrollbar(ScrollbarLayerImplBase* layer) {
1378 DCHECK(layer);
1379 DCHECK(!scrollbars_ || scrollbars_->find(layer) == scrollbars_->end());
1380 if (!scrollbars_)
1381 scrollbars_.reset(new ScrollbarSet());
1383 scrollbars_->insert(layer);
1386 void LayerImpl::RemoveScrollbar(ScrollbarLayerImplBase* layer) {
1387 DCHECK(scrollbars_);
1388 DCHECK(layer);
1389 DCHECK(scrollbars_->find(layer) != scrollbars_->end());
1391 scrollbars_->erase(layer);
1392 if (scrollbars_->empty())
1393 scrollbars_ = nullptr;
1396 bool LayerImpl::HasScrollbar(ScrollbarOrientation orientation) const {
1397 if (!scrollbars_)
1398 return false;
1400 for (ScrollbarSet::iterator it = scrollbars_->begin();
1401 it != scrollbars_->end();
1402 ++it)
1403 if ((*it)->orientation() == orientation)
1404 return true;
1406 return false;
1409 void LayerImpl::ScrollbarParametersDidChange(bool on_resize) {
1410 if (!scrollbars_)
1411 return;
1413 for (ScrollbarSet::iterator it = scrollbars_->begin();
1414 it != scrollbars_->end();
1415 ++it) {
1416 bool is_scroll_layer = (*it)->ScrollLayerId() == layer_id_;
1417 bool scroll_layer_resized = is_scroll_layer && on_resize;
1418 (*it)->ScrollbarParametersDidChange(scroll_layer_resized);
1422 void LayerImpl::SetNeedsPushProperties() {
1423 if (needs_push_properties_)
1424 return;
1425 if (!parent_should_know_need_push_properties() && parent_)
1426 parent_->AddDependentNeedsPushProperties();
1427 needs_push_properties_ = true;
1430 void LayerImpl::AddDependentNeedsPushProperties() {
1431 DCHECK_GE(num_dependents_need_push_properties_, 0);
1433 if (!parent_should_know_need_push_properties() && parent_)
1434 parent_->AddDependentNeedsPushProperties();
1436 num_dependents_need_push_properties_++;
1439 void LayerImpl::RemoveDependentNeedsPushProperties() {
1440 num_dependents_need_push_properties_--;
1441 DCHECK_GE(num_dependents_need_push_properties_, 0);
1443 if (!parent_should_know_need_push_properties() && parent_)
1444 parent_->RemoveDependentNeedsPushProperties();
1447 void LayerImpl::GetAllTilesForTracing(std::set<const Tile*>* tiles) const {
1450 void LayerImpl::AsValueInto(base::debug::TracedValue* state) const {
1451 TracedValue::MakeDictIntoImplicitSnapshotWithCategory(
1452 TRACE_DISABLED_BY_DEFAULT("cc.debug"),
1453 state,
1454 "cc::LayerImpl",
1455 LayerTypeAsString(),
1456 this);
1457 state->SetInteger("layer_id", id());
1458 MathUtil::AddToTracedValue("bounds", bounds_, state);
1460 state->SetDouble("opacity", opacity());
1462 MathUtil::AddToTracedValue("position", position_, state);
1464 state->SetInteger("draws_content", DrawsContent());
1465 state->SetInteger("gpu_memory_usage", GPUMemoryUsageInBytes());
1467 MathUtil::AddToTracedValue("scroll_offset", scroll_offset_, state);
1468 MathUtil::AddToTracedValue("transform_origin", transform_origin_, state);
1470 bool clipped;
1471 gfx::QuadF layer_quad = MathUtil::MapQuad(
1472 screen_space_transform(),
1473 gfx::QuadF(gfx::Rect(content_bounds())),
1474 &clipped);
1475 MathUtil::AddToTracedValue("layer_quad", layer_quad, state);
1476 if (!touch_event_handler_region_.IsEmpty()) {
1477 state->BeginArray("touch_event_handler_region");
1478 touch_event_handler_region_.AsValueInto(state);
1479 state->EndArray();
1481 if (have_wheel_event_handlers_) {
1482 gfx::Rect wheel_rect(content_bounds());
1483 Region wheel_region(wheel_rect);
1484 state->BeginArray("wheel_event_handler_region");
1485 wheel_region.AsValueInto(state);
1486 state->EndArray();
1488 if (have_scroll_event_handlers_) {
1489 gfx::Rect scroll_rect(content_bounds());
1490 Region scroll_region(scroll_rect);
1491 state->BeginArray("scroll_event_handler_region");
1492 scroll_region.AsValueInto(state);
1493 state->EndArray();
1495 if (!non_fast_scrollable_region_.IsEmpty()) {
1496 state->BeginArray("non_fast_scrollable_region");
1497 non_fast_scrollable_region_.AsValueInto(state);
1498 state->EndArray();
1501 state->BeginArray("children");
1502 for (size_t i = 0; i < children_.size(); ++i) {
1503 state->BeginDictionary();
1504 children_[i]->AsValueInto(state);
1505 state->EndDictionary();
1507 state->EndArray();
1508 if (mask_layer_) {
1509 state->BeginDictionary("mask_layer");
1510 mask_layer_->AsValueInto(state);
1511 state->EndDictionary();
1513 if (replica_layer_) {
1514 state->BeginDictionary("replica_layer");
1515 replica_layer_->AsValueInto(state);
1516 state->EndDictionary();
1519 if (scroll_parent_)
1520 state->SetInteger("scroll_parent", scroll_parent_->id());
1522 if (clip_parent_)
1523 state->SetInteger("clip_parent", clip_parent_->id());
1525 state->SetBoolean("can_use_lcd_text", can_use_lcd_text());
1526 state->SetBoolean("contents_opaque", contents_opaque());
1528 state->SetBoolean(
1529 "has_animation_bounds",
1530 layer_animation_controller()->HasAnimationThatInflatesBounds());
1532 gfx::BoxF box;
1533 if (LayerUtils::GetAnimationBounds(*this, &box))
1534 MathUtil::AddToTracedValue("animation_bounds", box, state);
1536 if (debug_info_.get()) {
1537 std::string str;
1538 debug_info_->AppendAsTraceFormat(&str);
1539 base::JSONReader json_reader;
1540 scoped_ptr<base::Value> debug_info_value(json_reader.ReadToValue(str));
1542 if (debug_info_value->IsType(base::Value::TYPE_DICTIONARY)) {
1543 base::DictionaryValue* dictionary_value = nullptr;
1544 bool converted_to_dictionary =
1545 debug_info_value->GetAsDictionary(&dictionary_value);
1546 DCHECK(converted_to_dictionary);
1547 for (base::DictionaryValue::Iterator it(*dictionary_value); !it.IsAtEnd();
1548 it.Advance()) {
1549 state->SetValue(it.key().data(), it.value().DeepCopy());
1551 } else {
1552 NOTREACHED();
1557 bool LayerImpl::IsDrawnRenderSurfaceLayerListMember() const {
1558 return draw_properties_.last_drawn_render_surface_layer_list_id ==
1559 layer_tree_impl_->current_render_surface_list_id();
1562 size_t LayerImpl::GPUMemoryUsageInBytes() const { return 0; }
1564 void LayerImpl::RunMicroBenchmark(MicroBenchmarkImpl* benchmark) {
1565 benchmark->RunOnLayer(this);
1568 int LayerImpl::NumDescendantsThatDrawContent() const {
1569 return num_descendants_that_draw_content_;
1572 void LayerImpl::NotifyAnimationFinished(
1573 base::TimeTicks monotonic_time,
1574 Animation::TargetProperty target_property,
1575 int group) {
1576 if (target_property == Animation::ScrollOffset)
1577 layer_tree_impl_->InputScrollAnimationFinished();
1580 void LayerImpl::SetHasRenderSurface(bool should_have_render_surface) {
1581 if (!!render_surface() == should_have_render_surface)
1582 return;
1584 SetNeedsPushProperties();
1585 layer_tree_impl()->set_needs_update_draw_properties();
1586 if (should_have_render_surface) {
1587 render_surface_ = make_scoped_ptr(new RenderSurfaceImpl(this));
1588 return;
1590 render_surface_.reset();
1593 } // namespace cc