cc: Remove LayerImpl::SetContentBounds()
[chromium-blink-merge.git] / cc / layers / layer_impl.cc
blob04ad44ffac56a2d884f5c66f8405f8164252f5c1
1 // Copyright 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "cc/layers/layer_impl.h"
7 #include "base/json/json_reader.h"
8 #include "base/numerics/safe_conversions.h"
9 #include "base/strings/stringprintf.h"
10 #include "base/trace_event/trace_event.h"
11 #include "base/trace_event/trace_event_argument.h"
12 #include "cc/animation/animation_registrar.h"
13 #include "cc/animation/scrollbar_animation_controller.h"
14 #include "cc/base/math_util.h"
15 #include "cc/base/simple_enclosed_region.h"
16 #include "cc/debug/debug_colors.h"
17 #include "cc/debug/layer_tree_debug_state.h"
18 #include "cc/debug/micro_benchmark_impl.h"
19 #include "cc/debug/traced_value.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* layer_impl, int id)
38 : LayerImpl(layer_impl, id, new LayerImpl::SyncedScrollOffset) {
41 LayerImpl::LayerImpl(LayerTreeImpl* tree_impl,
42 int id,
43 scoped_refptr<SyncedScrollOffset> scroll_offset)
44 : parent_(nullptr),
45 scroll_parent_(nullptr),
46 clip_parent_(nullptr),
47 mask_layer_id_(-1),
48 replica_layer_id_(-1),
49 layer_id_(id),
50 layer_tree_impl_(tree_impl),
51 scroll_offset_(scroll_offset),
52 scroll_clip_layer_(nullptr),
53 should_scroll_on_main_thread_(false),
54 have_wheel_event_handlers_(false),
55 have_scroll_event_handlers_(false),
56 scroll_blocks_on_(SCROLL_BLOCKS_ON_NONE),
57 user_scrollable_horizontal_(true),
58 user_scrollable_vertical_(true),
59 stacking_order_changed_(false),
60 double_sided_(true),
61 should_flatten_transform_(true),
62 should_flatten_transform_from_property_tree_(false),
63 layer_property_changed_(false),
64 masks_to_bounds_(false),
65 contents_opaque_(false),
66 is_root_for_isolated_group_(false),
67 use_parent_backface_visibility_(false),
68 draw_checkerboard_for_missing_tiles_(false),
69 draws_content_(false),
70 hide_layer_and_subtree_(false),
71 transform_is_invertible_(true),
72 is_container_for_fixed_position_layers_(false),
73 background_color_(0),
74 opacity_(1.0),
75 blend_mode_(SkXfermode::kSrcOver_Mode),
76 num_descendants_that_draw_content_(0),
77 transform_tree_index_(-1),
78 opacity_tree_index_(-1),
79 clip_tree_index_(-1),
80 draw_depth_(0.f),
81 needs_push_properties_(false),
82 num_dependents_need_push_properties_(0),
83 sorting_context_id_(0),
84 current_draw_mode_(DRAW_MODE_NONE),
85 frame_timing_requests_dirty_(false),
86 visited_(false),
87 layer_or_descendant_is_drawn_(false),
88 sorted_for_recursion_(false) {
89 DCHECK_GT(layer_id_, 0);
90 DCHECK(layer_tree_impl_);
91 layer_tree_impl_->RegisterLayer(this);
92 AnimationRegistrar* registrar = layer_tree_impl_->GetAnimationRegistrar();
93 layer_animation_controller_ =
94 registrar->GetAnimationControllerForId(layer_id_);
95 layer_animation_controller_->AddValueObserver(this);
96 if (IsActive()) {
97 layer_animation_controller_->set_value_provider(this);
98 layer_animation_controller_->set_layer_animation_delegate(this);
100 SetNeedsPushProperties();
103 LayerImpl::~LayerImpl() {
104 DCHECK_EQ(DRAW_MODE_NONE, current_draw_mode_);
106 layer_animation_controller_->RemoveValueObserver(this);
107 layer_animation_controller_->remove_value_provider(this);
108 layer_animation_controller_->remove_layer_animation_delegate(this);
110 if (!copy_requests_.empty() && layer_tree_impl_->IsActiveTree())
111 layer_tree_impl()->RemoveLayerWithCopyOutputRequest(this);
112 layer_tree_impl_->UnregisterLayer(this);
114 TRACE_EVENT_OBJECT_DELETED_WITH_ID(
115 TRACE_DISABLED_BY_DEFAULT("cc.debug"), "cc::LayerImpl", this);
118 void LayerImpl::AddChild(scoped_ptr<LayerImpl> child) {
119 child->SetParent(this);
120 DCHECK_EQ(layer_tree_impl(), child->layer_tree_impl());
121 children_.push_back(child.Pass());
122 layer_tree_impl()->set_needs_update_draw_properties();
125 scoped_ptr<LayerImpl> LayerImpl::RemoveChild(LayerImpl* child) {
126 for (OwnedLayerImplList::iterator it = children_.begin();
127 it != children_.end();
128 ++it) {
129 if (*it == child) {
130 scoped_ptr<LayerImpl> ret = children_.take(it);
131 children_.erase(it);
132 layer_tree_impl()->set_needs_update_draw_properties();
133 return ret.Pass();
136 return nullptr;
139 void LayerImpl::SetParent(LayerImpl* parent) {
140 if (parent_should_know_need_push_properties()) {
141 if (parent_)
142 parent_->RemoveDependentNeedsPushProperties();
143 if (parent)
144 parent->AddDependentNeedsPushProperties();
146 parent_ = parent;
149 void LayerImpl::ClearChildList() {
150 if (children_.empty())
151 return;
153 children_.clear();
154 layer_tree_impl()->set_needs_update_draw_properties();
157 bool LayerImpl::HasAncestor(const LayerImpl* ancestor) const {
158 if (!ancestor)
159 return false;
161 for (const LayerImpl* layer = this; layer; layer = layer->parent()) {
162 if (layer == ancestor)
163 return true;
166 return false;
169 void LayerImpl::SetScrollParent(LayerImpl* parent) {
170 if (scroll_parent_ == parent)
171 return;
173 if (parent)
174 DCHECK_EQ(layer_tree_impl()->LayerById(parent->id()), parent);
176 scroll_parent_ = parent;
177 SetNeedsPushProperties();
180 void LayerImpl::SetDebugInfo(
181 scoped_refptr<base::trace_event::ConvertableToTraceFormat> other) {
182 debug_info_ = other;
183 SetNeedsPushProperties();
186 void LayerImpl::SetScrollChildren(std::set<LayerImpl*>* children) {
187 if (scroll_children_.get() == children)
188 return;
189 scroll_children_.reset(children);
190 SetNeedsPushProperties();
193 void LayerImpl::SetNumDescendantsThatDrawContent(int num_descendants) {
194 if (num_descendants_that_draw_content_ == num_descendants)
195 return;
196 num_descendants_that_draw_content_ = num_descendants;
197 SetNeedsPushProperties();
200 void LayerImpl::SetClipParent(LayerImpl* ancestor) {
201 if (clip_parent_ == ancestor)
202 return;
204 clip_parent_ = ancestor;
205 SetNeedsPushProperties();
208 void LayerImpl::SetClipChildren(std::set<LayerImpl*>* children) {
209 if (clip_children_.get() == children)
210 return;
211 clip_children_.reset(children);
212 SetNeedsPushProperties();
215 void LayerImpl::SetTransformTreeIndex(int index) {
216 transform_tree_index_ = index;
217 SetNeedsPushProperties();
220 void LayerImpl::SetClipTreeIndex(int index) {
221 clip_tree_index_ = index;
222 SetNeedsPushProperties();
225 void LayerImpl::SetOpacityTreeIndex(int index) {
226 opacity_tree_index_ = index;
227 SetNeedsPushProperties();
230 void LayerImpl::PassCopyRequests(ScopedPtrVector<CopyOutputRequest>* requests) {
231 // In the case that a layer still has a copy request, this means that there's
232 // a commit to the active tree without a draw. This only happens in some
233 // edge cases during lost context or visibility changes, so don't try to
234 // handle preserving these output requests (and their surface).
235 if (!copy_requests_.empty()) {
236 layer_tree_impl()->RemoveLayerWithCopyOutputRequest(this);
237 // Destroying these will abort them.
238 copy_requests_.clear();
241 if (requests->empty())
242 return;
244 DCHECK(render_surface());
245 bool was_empty = copy_requests_.empty();
246 copy_requests_.insert_and_take(copy_requests_.end(), requests);
247 requests->clear();
249 if (was_empty && layer_tree_impl()->IsActiveTree())
250 layer_tree_impl()->AddLayerWithCopyOutputRequest(this);
251 NoteLayerPropertyChangedForSubtree();
254 void LayerImpl::TakeCopyRequestsAndTransformToTarget(
255 ScopedPtrVector<CopyOutputRequest>* requests) {
256 DCHECK(!copy_requests_.empty());
257 DCHECK(layer_tree_impl()->IsActiveTree());
258 DCHECK_EQ(render_target(), this);
260 size_t first_inserted_request = requests->size();
261 requests->insert_and_take(requests->end(), &copy_requests_);
262 copy_requests_.clear();
264 for (size_t i = first_inserted_request; i < requests->size(); ++i) {
265 CopyOutputRequest* request = requests->at(i);
266 if (!request->has_area())
267 continue;
269 gfx::Rect request_in_layer_space = request->area();
270 gfx::Rect request_in_content_space =
271 LayerRectToContentRect(request_in_layer_space);
272 request->set_area(MathUtil::MapEnclosingClippedRect(
273 draw_properties_.target_space_transform, request_in_content_space));
276 layer_tree_impl()->RemoveLayerWithCopyOutputRequest(this);
277 layer_tree_impl()->set_needs_update_draw_properties();
280 void LayerImpl::ClearRenderSurfaceLayerList() {
281 if (render_surface_)
282 render_surface_->ClearLayerLists();
285 void LayerImpl::PopulateSharedQuadState(SharedQuadState* state) const {
286 state->SetAll(draw_properties_.target_space_transform, bounds(),
287 draw_properties_.visible_content_rect,
288 draw_properties_.clip_rect, draw_properties_.is_clipped,
289 draw_properties_.opacity, draw_properties_.blend_mode,
290 sorting_context_id_);
293 void LayerImpl::PopulateScaledSharedQuadState(SharedQuadState* state,
294 float scale) const {
295 gfx::Transform scaled_draw_transform =
296 draw_properties_.target_space_transform;
297 scaled_draw_transform.Scale(SK_MScalar1 / scale, SK_MScalar1 / scale);
298 gfx::Size scaled_bounds = gfx::ToCeiledSize(gfx::ScaleSize(bounds(), scale));
299 gfx::Rect scaled_visible_content_rect =
300 gfx::ScaleToEnclosingRect(visible_content_rect(), scale);
301 scaled_visible_content_rect.Intersect(gfx::Rect(scaled_bounds));
303 state->SetAll(scaled_draw_transform, scaled_bounds,
304 scaled_visible_content_rect, draw_properties().clip_rect,
305 draw_properties().is_clipped, draw_properties().opacity,
306 draw_properties().blend_mode, sorting_context_id_);
309 bool LayerImpl::WillDraw(DrawMode draw_mode,
310 ResourceProvider* resource_provider) {
311 // WillDraw/DidDraw must be matched.
312 DCHECK_NE(DRAW_MODE_NONE, draw_mode);
313 DCHECK_EQ(DRAW_MODE_NONE, current_draw_mode_);
314 current_draw_mode_ = draw_mode;
315 return true;
318 void LayerImpl::DidDraw(ResourceProvider* resource_provider) {
319 DCHECK_NE(DRAW_MODE_NONE, current_draw_mode_);
320 current_draw_mode_ = DRAW_MODE_NONE;
323 bool LayerImpl::ShowDebugBorders() const {
324 return layer_tree_impl()->debug_state().show_debug_borders;
327 void LayerImpl::GetDebugBorderProperties(SkColor* color, float* width) const {
328 if (draws_content_) {
329 *color = DebugColors::ContentLayerBorderColor();
330 *width = DebugColors::ContentLayerBorderWidth(layer_tree_impl());
331 return;
334 if (masks_to_bounds_) {
335 *color = DebugColors::MaskingLayerBorderColor();
336 *width = DebugColors::MaskingLayerBorderWidth(layer_tree_impl());
337 return;
340 *color = DebugColors::ContainerLayerBorderColor();
341 *width = DebugColors::ContainerLayerBorderWidth(layer_tree_impl());
344 void LayerImpl::AppendDebugBorderQuad(
345 RenderPass* render_pass,
346 const gfx::Size& bounds,
347 const SharedQuadState* shared_quad_state,
348 AppendQuadsData* append_quads_data) const {
349 SkColor color;
350 float width;
351 GetDebugBorderProperties(&color, &width);
352 AppendDebugBorderQuad(render_pass, bounds, shared_quad_state,
353 append_quads_data, color, width);
356 void LayerImpl::AppendDebugBorderQuad(RenderPass* render_pass,
357 const gfx::Size& bounds,
358 const SharedQuadState* shared_quad_state,
359 AppendQuadsData* append_quads_data,
360 SkColor color,
361 float width) const {
362 if (!ShowDebugBorders())
363 return;
365 gfx::Rect quad_rect(bounds);
366 gfx::Rect visible_quad_rect(quad_rect);
367 DebugBorderDrawQuad* debug_border_quad =
368 render_pass->CreateAndAppendDrawQuad<DebugBorderDrawQuad>();
369 debug_border_quad->SetNew(
370 shared_quad_state, quad_rect, visible_quad_rect, color, width);
371 if (contents_opaque()) {
372 // When opaque, draw a second inner border that is thicker than the outer
373 // border, but more transparent.
374 static const float kFillOpacity = 0.3f;
375 SkColor fill_color = SkColorSetA(
376 color, static_cast<uint8_t>(SkColorGetA(color) * kFillOpacity));
377 float fill_width = width * 3;
378 gfx::Rect fill_rect = quad_rect;
379 fill_rect.Inset(fill_width / 2.f, fill_width / 2.f);
380 if (fill_rect.IsEmpty())
381 return;
382 gfx::Rect visible_fill_rect =
383 gfx::IntersectRects(visible_quad_rect, fill_rect);
384 DebugBorderDrawQuad* fill_quad =
385 render_pass->CreateAndAppendDrawQuad<DebugBorderDrawQuad>();
386 fill_quad->SetNew(shared_quad_state, fill_rect, visible_fill_rect,
387 fill_color, fill_width);
391 bool LayerImpl::HasDelegatedContent() const {
392 return false;
395 bool LayerImpl::HasContributingDelegatedRenderPasses() const {
396 return false;
399 RenderPassId LayerImpl::FirstContributingRenderPassId() const {
400 return RenderPassId(0, 0);
403 RenderPassId LayerImpl::NextContributingRenderPassId(RenderPassId id) const {
404 return RenderPassId(0, 0);
407 void LayerImpl::GetContentsResourceId(ResourceId* resource_id,
408 gfx::Size* resource_size) const {
409 NOTREACHED();
410 *resource_id = 0;
413 gfx::Vector2dF LayerImpl::ScrollBy(const gfx::Vector2dF& scroll) {
414 gfx::ScrollOffset adjusted_scroll(scroll);
415 if (!user_scrollable_horizontal_)
416 adjusted_scroll.set_x(0);
417 if (!user_scrollable_vertical_)
418 adjusted_scroll.set_y(0);
419 DCHECK(scrollable());
420 gfx::ScrollOffset old_offset = CurrentScrollOffset();
421 gfx::ScrollOffset new_offset =
422 ClampScrollOffsetToLimits(old_offset + adjusted_scroll);
423 SetCurrentScrollOffset(new_offset);
425 gfx::ScrollOffset unscrolled =
426 old_offset + gfx::ScrollOffset(scroll) - new_offset;
427 return gfx::Vector2dF(unscrolled.x(), unscrolled.y());
430 void LayerImpl::SetScrollClipLayer(int scroll_clip_layer_id) {
431 scroll_clip_layer_ = layer_tree_impl()->LayerById(scroll_clip_layer_id);
434 bool LayerImpl::user_scrollable(ScrollbarOrientation orientation) const {
435 return (orientation == HORIZONTAL) ? user_scrollable_horizontal_
436 : user_scrollable_vertical_;
439 void LayerImpl::ApplySentScrollDeltasFromAbortedCommit() {
440 DCHECK(layer_tree_impl()->IsActiveTree());
441 scroll_offset_->AbortCommit();
444 InputHandler::ScrollStatus LayerImpl::TryScroll(
445 const gfx::PointF& screen_space_point,
446 InputHandler::ScrollInputType type,
447 ScrollBlocksOn effective_block_mode) const {
448 if (should_scroll_on_main_thread()) {
449 TRACE_EVENT0("cc", "LayerImpl::TryScroll: Failed ShouldScrollOnMainThread");
450 return InputHandler::SCROLL_ON_MAIN_THREAD;
453 if (!screen_space_transform().IsInvertible()) {
454 TRACE_EVENT0("cc", "LayerImpl::TryScroll: Ignored NonInvertibleTransform");
455 return InputHandler::SCROLL_IGNORED;
458 if (!non_fast_scrollable_region().IsEmpty()) {
459 bool clipped = false;
460 gfx::Transform inverse_screen_space_transform(
461 gfx::Transform::kSkipInitialization);
462 if (!screen_space_transform().GetInverse(&inverse_screen_space_transform)) {
463 // TODO(shawnsingh): We shouldn't be applying a projection if screen space
464 // transform is uninvertible here. Perhaps we should be returning
465 // SCROLL_ON_MAIN_THREAD in this case?
468 gfx::PointF hit_test_point_in_content_space =
469 MathUtil::ProjectPoint(inverse_screen_space_transform,
470 screen_space_point,
471 &clipped);
472 gfx::PointF hit_test_point_in_layer_space =
473 gfx::ScalePoint(hit_test_point_in_content_space,
474 1.f / contents_scale_x(),
475 1.f / contents_scale_y());
476 if (!clipped &&
477 non_fast_scrollable_region().Contains(
478 gfx::ToRoundedPoint(hit_test_point_in_layer_space))) {
479 TRACE_EVENT0("cc",
480 "LayerImpl::tryScroll: Failed NonFastScrollableRegion");
481 return InputHandler::SCROLL_ON_MAIN_THREAD;
485 if (have_scroll_event_handlers() &&
486 effective_block_mode & SCROLL_BLOCKS_ON_SCROLL_EVENT) {
487 TRACE_EVENT0("cc", "LayerImpl::tryScroll: Failed ScrollEventHandlers");
488 return InputHandler::SCROLL_ON_MAIN_THREAD;
491 if (type == InputHandler::WHEEL && have_wheel_event_handlers() &&
492 effective_block_mode & SCROLL_BLOCKS_ON_WHEEL_EVENT) {
493 TRACE_EVENT0("cc", "LayerImpl::tryScroll: Failed WheelEventHandlers");
494 return InputHandler::SCROLL_ON_MAIN_THREAD;
497 if (!scrollable()) {
498 TRACE_EVENT0("cc", "LayerImpl::tryScroll: Ignored not scrollable");
499 return InputHandler::SCROLL_IGNORED;
502 gfx::ScrollOffset max_scroll_offset = MaxScrollOffset();
503 if (max_scroll_offset.x() <= 0 && max_scroll_offset.y() <= 0) {
504 TRACE_EVENT0("cc",
505 "LayerImpl::tryScroll: Ignored. Technically scrollable,"
506 " but has no affordance in either direction.");
507 return InputHandler::SCROLL_IGNORED;
510 return InputHandler::SCROLL_STARTED;
513 // TODO(danakj): Remove this after impl_side_painting.
514 gfx::Rect LayerImpl::LayerRectToContentRect(
515 const gfx::RectF& layer_rect) const {
516 gfx::RectF content_rect = layer_rect;
517 content_rect.Intersect(gfx::Rect(bounds()));
518 return gfx::ToEnclosingRect(content_rect);
521 skia::RefPtr<SkPicture> LayerImpl::GetPicture() {
522 return skia::RefPtr<SkPicture>();
525 scoped_ptr<LayerImpl> LayerImpl::CreateLayerImpl(LayerTreeImpl* tree_impl) {
526 return LayerImpl::Create(tree_impl, layer_id_, scroll_offset_);
529 void LayerImpl::PushPropertiesTo(LayerImpl* layer) {
530 layer->SetTransformOrigin(transform_origin_);
531 layer->SetBackgroundColor(background_color_);
532 layer->SetBounds(bounds_);
533 layer->SetContentsScale(contents_scale_x(), contents_scale_y());
534 layer->SetDoubleSided(double_sided_);
535 layer->SetDrawCheckerboardForMissingTiles(
536 draw_checkerboard_for_missing_tiles_);
537 layer->SetDrawsContent(DrawsContent());
538 layer->SetHideLayerAndSubtree(hide_layer_and_subtree_);
539 layer->SetHasRenderSurface(!!render_surface());
540 layer->SetFilters(filters());
541 layer->SetBackgroundFilters(background_filters());
542 layer->SetMasksToBounds(masks_to_bounds_);
543 layer->SetShouldScrollOnMainThread(should_scroll_on_main_thread_);
544 layer->SetHaveWheelEventHandlers(have_wheel_event_handlers_);
545 layer->SetHaveScrollEventHandlers(have_scroll_event_handlers_);
546 layer->SetScrollBlocksOn(scroll_blocks_on_);
547 layer->SetNonFastScrollableRegion(non_fast_scrollable_region_);
548 layer->SetTouchEventHandlerRegion(touch_event_handler_region_);
549 layer->SetContentsOpaque(contents_opaque_);
550 layer->SetOpacity(opacity_);
551 layer->SetBlendMode(blend_mode_);
552 layer->SetIsRootForIsolatedGroup(is_root_for_isolated_group_);
553 layer->SetPosition(position_);
554 layer->SetIsContainerForFixedPositionLayers(
555 is_container_for_fixed_position_layers_);
556 layer->SetPositionConstraint(position_constraint_);
557 layer->SetShouldFlattenTransform(should_flatten_transform_);
558 layer->set_should_flatten_transform_from_property_tree(
559 should_flatten_transform_from_property_tree_);
560 layer->SetUseParentBackfaceVisibility(use_parent_backface_visibility_);
561 layer->SetTransformAndInvertibility(transform_, transform_is_invertible_);
563 layer->SetScrollClipLayer(scroll_clip_layer_ ? scroll_clip_layer_->id()
564 : Layer::INVALID_ID);
565 layer->set_user_scrollable_horizontal(user_scrollable_horizontal_);
566 layer->set_user_scrollable_vertical(user_scrollable_vertical_);
568 layer->SetScrollCompensationAdjustment(scroll_compensation_adjustment_);
570 layer->PushScrollOffset(nullptr);
572 layer->Set3dSortingContextId(sorting_context_id_);
573 layer->SetNumDescendantsThatDrawContent(num_descendants_that_draw_content_);
575 layer->SetTransformTreeIndex(transform_tree_index_);
576 layer->SetClipTreeIndex(clip_tree_index_);
577 layer->SetOpacityTreeIndex(opacity_tree_index_);
578 layer->set_offset_to_transform_parent(offset_to_transform_parent_);
580 LayerImpl* scroll_parent = nullptr;
581 if (scroll_parent_) {
582 scroll_parent = layer->layer_tree_impl()->LayerById(scroll_parent_->id());
583 DCHECK(scroll_parent);
586 layer->SetScrollParent(scroll_parent);
587 if (scroll_children_) {
588 std::set<LayerImpl*>* scroll_children = new std::set<LayerImpl*>;
589 for (std::set<LayerImpl*>::iterator it = scroll_children_->begin();
590 it != scroll_children_->end();
591 ++it) {
592 DCHECK_EQ((*it)->scroll_parent(), this);
593 LayerImpl* scroll_child =
594 layer->layer_tree_impl()->LayerById((*it)->id());
595 DCHECK(scroll_child);
596 scroll_children->insert(scroll_child);
598 layer->SetScrollChildren(scroll_children);
599 } else {
600 layer->SetScrollChildren(nullptr);
603 LayerImpl* clip_parent = nullptr;
604 if (clip_parent_) {
605 clip_parent = layer->layer_tree_impl()->LayerById(
606 clip_parent_->id());
607 DCHECK(clip_parent);
610 layer->SetClipParent(clip_parent);
611 if (clip_children_) {
612 std::set<LayerImpl*>* clip_children = new std::set<LayerImpl*>;
613 for (std::set<LayerImpl*>::iterator it = clip_children_->begin();
614 it != clip_children_->end(); ++it)
615 clip_children->insert(layer->layer_tree_impl()->LayerById((*it)->id()));
616 layer->SetClipChildren(clip_children);
617 } else {
618 layer->SetClipChildren(nullptr);
621 layer->PassCopyRequests(&copy_requests_);
623 // If the main thread commits multiple times before the impl thread actually
624 // draws, then damage tracking will become incorrect if we simply clobber the
625 // update_rect here. The LayerImpl's update_rect needs to accumulate (i.e.
626 // union) any update changes that have occurred on the main thread.
627 update_rect_.Union(layer->update_rect());
628 layer->SetUpdateRect(update_rect_);
630 layer->SetStackingOrderChanged(stacking_order_changed_);
631 layer->SetDebugInfo(debug_info_);
633 if (frame_timing_requests_dirty_) {
634 layer->SetFrameTimingRequests(frame_timing_requests_);
635 frame_timing_requests_dirty_ = false;
638 // Reset any state that should be cleared for the next update.
639 stacking_order_changed_ = false;
640 update_rect_ = gfx::Rect();
641 needs_push_properties_ = false;
642 num_dependents_need_push_properties_ = 0;
645 gfx::Vector2dF LayerImpl::FixedContainerSizeDelta() const {
646 if (!scroll_clip_layer_)
647 return gfx::Vector2dF();
649 gfx::Vector2dF delta_from_scroll = scroll_clip_layer_->bounds_delta();
651 // In virtual-viewport mode, we don't need to compensate for pinch zoom or
652 // scale since the fixed container is the outer viewport, which sits below
653 // the page scale.
654 return delta_from_scroll;
657 base::DictionaryValue* LayerImpl::LayerTreeAsJson() const {
658 base::DictionaryValue* result = new base::DictionaryValue;
659 result->SetInteger("LayerId", id());
660 result->SetString("LayerType", LayerTypeAsString());
662 base::ListValue* list = new base::ListValue;
663 list->AppendInteger(bounds().width());
664 list->AppendInteger(bounds().height());
665 result->Set("Bounds", list);
667 list = new base::ListValue;
668 list->AppendDouble(position_.x());
669 list->AppendDouble(position_.y());
670 result->Set("Position", list);
672 const gfx::Transform& gfx_transform = draw_properties_.target_space_transform;
673 double transform[16];
674 gfx_transform.matrix().asColMajord(transform);
675 list = new base::ListValue;
676 for (int i = 0; i < 16; ++i)
677 list->AppendDouble(transform[i]);
678 result->Set("DrawTransform", list);
680 result->SetBoolean("DrawsContent", draws_content_);
681 result->SetBoolean("Is3dSorted", Is3dSorted());
682 result->SetDouble("OPACITY", opacity());
683 result->SetBoolean("ContentsOpaque", contents_opaque_);
685 if (scrollable())
686 result->SetBoolean("Scrollable", true);
688 if (have_wheel_event_handlers_)
689 result->SetBoolean("WheelHandler", have_wheel_event_handlers_);
690 if (have_scroll_event_handlers_)
691 result->SetBoolean("ScrollHandler", have_scroll_event_handlers_);
692 if (!touch_event_handler_region_.IsEmpty()) {
693 scoped_ptr<base::Value> region = touch_event_handler_region_.AsValue();
694 result->Set("TouchRegion", region.release());
697 if (scroll_blocks_on_) {
698 list = new base::ListValue;
699 if (scroll_blocks_on_ & SCROLL_BLOCKS_ON_START_TOUCH)
700 list->AppendString("StartTouch");
701 if (scroll_blocks_on_ & SCROLL_BLOCKS_ON_WHEEL_EVENT)
702 list->AppendString("WheelEvent");
703 if (scroll_blocks_on_ & SCROLL_BLOCKS_ON_SCROLL_EVENT)
704 list->AppendString("ScrollEvent");
705 result->Set("ScrollBlocksOn", list);
708 list = new base::ListValue;
709 for (size_t i = 0; i < children_.size(); ++i)
710 list->Append(children_[i]->LayerTreeAsJson());
711 result->Set("Children", list);
713 return result;
716 void LayerImpl::SetStackingOrderChanged(bool stacking_order_changed) {
717 if (stacking_order_changed) {
718 stacking_order_changed_ = true;
719 NoteLayerPropertyChangedForSubtree();
723 void LayerImpl::NoteLayerPropertyChanged() {
724 layer_property_changed_ = true;
725 layer_tree_impl()->set_needs_update_draw_properties();
726 SetNeedsPushProperties();
729 void LayerImpl::NoteLayerPropertyChangedForSubtree() {
730 layer_property_changed_ = true;
731 layer_tree_impl()->set_needs_update_draw_properties();
732 for (size_t i = 0; i < children_.size(); ++i)
733 children_[i]->NoteLayerPropertyChangedForDescendantsInternal();
734 SetNeedsPushProperties();
737 void LayerImpl::NoteLayerPropertyChangedForDescendantsInternal() {
738 layer_property_changed_ = true;
739 for (size_t i = 0; i < children_.size(); ++i)
740 children_[i]->NoteLayerPropertyChangedForDescendantsInternal();
743 void LayerImpl::NoteLayerPropertyChangedForDescendants() {
744 layer_tree_impl()->set_needs_update_draw_properties();
745 for (size_t i = 0; i < children_.size(); ++i)
746 children_[i]->NoteLayerPropertyChangedForDescendantsInternal();
747 SetNeedsPushProperties();
750 void LayerImpl::ValidateQuadResourcesInternal(DrawQuad* quad) const {
751 #if DCHECK_IS_ON()
752 const ResourceProvider* resource_provider =
753 layer_tree_impl_->resource_provider();
754 for (ResourceId resource_id : quad->resources)
755 resource_provider->ValidateResource(resource_id);
756 #endif
759 const char* LayerImpl::LayerTypeAsString() const {
760 return "cc::LayerImpl";
763 void LayerImpl::ResetAllChangeTrackingForSubtree() {
764 layer_property_changed_ = false;
766 update_rect_ = gfx::Rect();
767 damage_rect_ = gfx::RectF();
769 if (render_surface_)
770 render_surface_->ResetPropertyChangedFlag();
772 if (mask_layer_)
773 mask_layer_->ResetAllChangeTrackingForSubtree();
775 if (replica_layer_) {
776 // This also resets the replica mask, if it exists.
777 replica_layer_->ResetAllChangeTrackingForSubtree();
780 for (size_t i = 0; i < children_.size(); ++i)
781 children_[i]->ResetAllChangeTrackingForSubtree();
783 needs_push_properties_ = false;
784 num_dependents_need_push_properties_ = 0;
787 void LayerImpl::UpdatePropertyTreeTransform() {
788 if (transform_tree_index_ != -1) {
789 TransformTree& transform_tree =
790 layer_tree_impl()->property_trees()->transform_tree;
791 TransformNode* node = transform_tree.Node(transform_tree_index_);
792 if (node->data.local != transform_) {
793 node->data.local = transform_;
794 node->data.needs_local_transform_update = true;
795 transform_tree.set_needs_update(true);
796 // TODO(ajuma): The current criteria for creating clip nodes means that
797 // property trees may need to be rebuilt when the new transform isn't
798 // axis-aligned wrt the old transform (see Layer::SetTransform). Since
799 // rebuilding property trees every frame of a transform animation is
800 // something we should try to avoid, change property tree-building so that
801 // it doesn't depend on axis aliginment.
806 void LayerImpl::UpdatePropertyTreeOpacity() {
807 if (opacity_tree_index_ != -1) {
808 OpacityTree& opacity_tree =
809 layer_tree_impl()->property_trees()->opacity_tree;
810 OpacityNode* node = opacity_tree.Node(opacity_tree_index_);
811 node->data = opacity_;
815 gfx::ScrollOffset LayerImpl::ScrollOffsetForAnimation() const {
816 return CurrentScrollOffset();
819 void LayerImpl::OnFilterAnimated(const FilterOperations& filters) {
820 SetFilters(filters);
823 void LayerImpl::OnOpacityAnimated(float opacity) {
824 SetOpacity(opacity);
825 UpdatePropertyTreeOpacity();
828 void LayerImpl::OnTransformAnimated(const gfx::Transform& transform) {
829 SetTransform(transform);
830 UpdatePropertyTreeTransform();
833 void LayerImpl::OnScrollOffsetAnimated(const gfx::ScrollOffset& scroll_offset) {
834 // Only layers in the active tree should need to do anything here, since
835 // layers in the pending tree will find out about these changes as a
836 // result of the shared SyncedProperty.
837 if (!IsActive())
838 return;
840 SetCurrentScrollOffset(scroll_offset);
842 layer_tree_impl_->DidAnimateScrollOffset();
845 void LayerImpl::OnAnimationWaitingForDeletion() {}
847 bool LayerImpl::IsActive() const {
848 return layer_tree_impl_->IsActiveTree();
851 gfx::Size LayerImpl::bounds() const {
852 gfx::Vector2d delta = gfx::ToCeiledVector2d(bounds_delta_);
853 return gfx::Size(bounds_.width() + delta.x(),
854 bounds_.height() + delta.y());
857 gfx::SizeF LayerImpl::BoundsForScrolling() const {
858 return gfx::SizeF(bounds_.width() + bounds_delta_.x(),
859 bounds_.height() + bounds_delta_.y());
862 void LayerImpl::SetBounds(const gfx::Size& bounds) {
863 if (bounds_ == bounds)
864 return;
866 bounds_ = bounds;
868 ScrollbarParametersDidChange(true);
869 if (masks_to_bounds())
870 NoteLayerPropertyChangedForSubtree();
871 else
872 NoteLayerPropertyChanged();
875 void LayerImpl::SetBoundsDelta(const gfx::Vector2dF& bounds_delta) {
876 if (bounds_delta_ == bounds_delta)
877 return;
879 bounds_delta_ = bounds_delta;
881 ScrollbarParametersDidChange(true);
883 if (masks_to_bounds()) {
884 // If layer is clipping, then update the clip node using the new bounds.
885 ClipNode* clip_node =
886 layer_tree_impl()->property_trees()->clip_tree.Node(clip_tree_index());
887 if (clip_node) {
888 DCHECK(id() == clip_node->owner_id);
889 clip_node->data.clip =
890 gfx::RectF(gfx::PointF() + offset_to_transform_parent(), bounds());
891 layer_tree_impl()->property_trees()->clip_tree.set_needs_update(true);
894 NoteLayerPropertyChangedForSubtree();
895 } else {
896 NoteLayerPropertyChanged();
900 void LayerImpl::SetMaskLayer(scoped_ptr<LayerImpl> mask_layer) {
901 int new_layer_id = mask_layer ? mask_layer->id() : -1;
903 if (mask_layer) {
904 DCHECK_EQ(layer_tree_impl(), mask_layer->layer_tree_impl());
905 DCHECK_NE(new_layer_id, mask_layer_id_);
906 } else if (new_layer_id == mask_layer_id_) {
907 return;
910 mask_layer_ = mask_layer.Pass();
911 mask_layer_id_ = new_layer_id;
912 if (mask_layer_)
913 mask_layer_->SetParent(this);
914 NoteLayerPropertyChangedForSubtree();
917 scoped_ptr<LayerImpl> LayerImpl::TakeMaskLayer() {
918 mask_layer_id_ = -1;
919 return mask_layer_.Pass();
922 void LayerImpl::SetReplicaLayer(scoped_ptr<LayerImpl> replica_layer) {
923 int new_layer_id = replica_layer ? replica_layer->id() : -1;
925 if (replica_layer) {
926 DCHECK_EQ(layer_tree_impl(), replica_layer->layer_tree_impl());
927 DCHECK_NE(new_layer_id, replica_layer_id_);
928 } else if (new_layer_id == replica_layer_id_) {
929 return;
932 replica_layer_ = replica_layer.Pass();
933 replica_layer_id_ = new_layer_id;
934 if (replica_layer_)
935 replica_layer_->SetParent(this);
936 NoteLayerPropertyChangedForSubtree();
939 scoped_ptr<LayerImpl> LayerImpl::TakeReplicaLayer() {
940 replica_layer_id_ = -1;
941 return replica_layer_.Pass();
944 ScrollbarLayerImplBase* LayerImpl::ToScrollbarLayer() {
945 return nullptr;
948 void LayerImpl::SetDrawsContent(bool draws_content) {
949 if (draws_content_ == draws_content)
950 return;
952 draws_content_ = draws_content;
953 NoteLayerPropertyChanged();
956 void LayerImpl::SetHideLayerAndSubtree(bool hide) {
957 if (hide_layer_and_subtree_ == hide)
958 return;
960 hide_layer_and_subtree_ = hide;
961 NoteLayerPropertyChangedForSubtree();
964 void LayerImpl::SetTransformOrigin(const gfx::Point3F& transform_origin) {
965 if (transform_origin_ == transform_origin)
966 return;
967 transform_origin_ = transform_origin;
968 NoteLayerPropertyChangedForSubtree();
971 void LayerImpl::SetBackgroundColor(SkColor background_color) {
972 if (background_color_ == background_color)
973 return;
975 background_color_ = background_color;
976 NoteLayerPropertyChanged();
979 SkColor LayerImpl::SafeOpaqueBackgroundColor() const {
980 SkColor color = background_color();
981 if (SkColorGetA(color) == 255 && !contents_opaque()) {
982 color = SK_ColorTRANSPARENT;
983 } else if (SkColorGetA(color) != 255 && contents_opaque()) {
984 for (const LayerImpl* layer = parent(); layer;
985 layer = layer->parent()) {
986 color = layer->background_color();
987 if (SkColorGetA(color) == 255)
988 break;
990 if (SkColorGetA(color) != 255)
991 color = layer_tree_impl()->background_color();
992 if (SkColorGetA(color) != 255)
993 color = SkColorSetA(color, 255);
995 return color;
998 void LayerImpl::SetFilters(const FilterOperations& filters) {
999 if (filters_ == filters)
1000 return;
1002 filters_ = filters;
1003 NoteLayerPropertyChangedForSubtree();
1006 bool LayerImpl::FilterIsAnimating() const {
1007 return layer_animation_controller_->IsAnimatingProperty(Animation::FILTER);
1010 bool LayerImpl::FilterIsAnimatingOnImplOnly() const {
1011 Animation* filter_animation =
1012 layer_animation_controller_->GetAnimation(Animation::FILTER);
1013 return filter_animation && filter_animation->is_impl_only();
1016 void LayerImpl::SetBackgroundFilters(
1017 const FilterOperations& filters) {
1018 if (background_filters_ == filters)
1019 return;
1021 background_filters_ = filters;
1022 NoteLayerPropertyChanged();
1025 void LayerImpl::SetMasksToBounds(bool masks_to_bounds) {
1026 if (masks_to_bounds_ == masks_to_bounds)
1027 return;
1029 masks_to_bounds_ = masks_to_bounds;
1030 NoteLayerPropertyChangedForSubtree();
1033 void LayerImpl::SetContentsOpaque(bool opaque) {
1034 if (contents_opaque_ == opaque)
1035 return;
1037 contents_opaque_ = opaque;
1038 NoteLayerPropertyChangedForSubtree();
1041 void LayerImpl::SetOpacity(float opacity) {
1042 if (opacity_ == opacity)
1043 return;
1045 opacity_ = opacity;
1046 NoteLayerPropertyChangedForSubtree();
1049 bool LayerImpl::OpacityIsAnimating() const {
1050 return layer_animation_controller_->IsAnimatingProperty(Animation::OPACITY);
1053 bool LayerImpl::OpacityIsAnimatingOnImplOnly() const {
1054 Animation* opacity_animation =
1055 layer_animation_controller_->GetAnimation(Animation::OPACITY);
1056 return opacity_animation && opacity_animation->is_impl_only();
1059 void LayerImpl::SetBlendMode(SkXfermode::Mode blend_mode) {
1060 if (blend_mode_ == blend_mode)
1061 return;
1063 blend_mode_ = blend_mode;
1064 NoteLayerPropertyChangedForSubtree();
1067 void LayerImpl::SetIsRootForIsolatedGroup(bool root) {
1068 if (is_root_for_isolated_group_ == root)
1069 return;
1071 is_root_for_isolated_group_ = root;
1072 SetNeedsPushProperties();
1075 void LayerImpl::SetPosition(const gfx::PointF& position) {
1076 if (position_ == position)
1077 return;
1079 position_ = position;
1080 NoteLayerPropertyChangedForSubtree();
1083 void LayerImpl::SetShouldFlattenTransform(bool flatten) {
1084 if (should_flatten_transform_ == flatten)
1085 return;
1087 should_flatten_transform_ = flatten;
1088 NoteLayerPropertyChangedForSubtree();
1091 void LayerImpl::Set3dSortingContextId(int id) {
1092 if (id == sorting_context_id_)
1093 return;
1094 sorting_context_id_ = id;
1095 NoteLayerPropertyChangedForSubtree();
1098 void LayerImpl::SetFrameTimingRequests(
1099 const std::vector<FrameTimingRequest>& requests) {
1100 frame_timing_requests_ = requests;
1101 frame_timing_requests_dirty_ = true;
1102 SetNeedsPushProperties();
1105 void LayerImpl::GatherFrameTimingRequestIds(std::vector<int64_t>* request_ids) {
1106 for (const auto& request : frame_timing_requests_)
1107 request_ids->push_back(request.id());
1110 void LayerImpl::SetTransform(const gfx::Transform& transform) {
1111 if (transform_ == transform)
1112 return;
1114 transform_ = transform;
1115 transform_is_invertible_ = transform_.IsInvertible();
1116 NoteLayerPropertyChangedForSubtree();
1119 void LayerImpl::SetTransformAndInvertibility(const gfx::Transform& transform,
1120 bool transform_is_invertible) {
1121 if (transform_ == transform) {
1122 DCHECK(transform_is_invertible_ == transform_is_invertible)
1123 << "Can't change invertibility if transform is unchanged";
1124 return;
1126 transform_ = transform;
1127 transform_is_invertible_ = transform_is_invertible;
1128 NoteLayerPropertyChangedForSubtree();
1131 bool LayerImpl::TransformIsAnimating() const {
1132 return layer_animation_controller_->IsAnimatingProperty(Animation::TRANSFORM);
1135 bool LayerImpl::TransformIsAnimatingOnImplOnly() const {
1136 Animation* transform_animation =
1137 layer_animation_controller_->GetAnimation(Animation::TRANSFORM);
1138 return transform_animation && transform_animation->is_impl_only();
1141 void LayerImpl::SetUpdateRect(const gfx::Rect& update_rect) {
1142 update_rect_ = update_rect;
1143 SetNeedsPushProperties();
1146 void LayerImpl::AddDamageRect(const gfx::RectF& damage_rect) {
1147 damage_rect_ = gfx::UnionRects(damage_rect_, damage_rect);
1150 void LayerImpl::SetContentsScale(float contents_scale_x,
1151 float contents_scale_y) {
1152 if (this->contents_scale_x() == contents_scale_x &&
1153 this->contents_scale_y() == contents_scale_y)
1154 return;
1156 draw_properties_.contents_scale_x = contents_scale_x;
1157 draw_properties_.contents_scale_y = contents_scale_y;
1158 NoteLayerPropertyChanged();
1161 bool LayerImpl::IsExternalScrollActive() const {
1162 return layer_tree_impl_->IsExternalScrollActive();
1165 void LayerImpl::SetCurrentScrollOffset(const gfx::ScrollOffset& scroll_offset) {
1166 DCHECK(IsActive());
1167 if (scroll_offset_->SetCurrent(scroll_offset))
1168 DidUpdateScrollOffset(false);
1171 void LayerImpl::SetCurrentScrollOffsetFromDelegate(
1172 const gfx::ScrollOffset& scroll_offset) {
1173 DCHECK(IsActive());
1174 if (scroll_offset_->SetCurrent(scroll_offset))
1175 DidUpdateScrollOffset(true);
1178 void LayerImpl::PushScrollOffsetFromMainThread(
1179 const gfx::ScrollOffset& scroll_offset) {
1180 PushScrollOffset(&scroll_offset);
1183 void LayerImpl::PushScrollOffsetFromMainThreadAndClobberActiveValue(
1184 const gfx::ScrollOffset& scroll_offset) {
1185 scroll_offset_->set_clobber_active_value();
1186 PushScrollOffset(&scroll_offset);
1189 gfx::ScrollOffset LayerImpl::PullDeltaForMainThread() {
1190 // TODO(miletus): Remove all this temporary flooring machinery when
1191 // Blink fully supports fractional scrolls.
1192 gfx::ScrollOffset current_offset = CurrentScrollOffset();
1193 gfx::Vector2dF current_delta = ScrollDelta();
1194 gfx::Vector2dF floored_delta(floor(current_delta.x()),
1195 floor(current_delta.y()));
1196 gfx::Vector2dF diff_delta = floored_delta - current_delta;
1197 gfx::ScrollOffset tmp_offset = ScrollOffsetWithDelta(current_offset,
1198 diff_delta);
1199 scroll_offset_->SetCurrent(tmp_offset);
1200 gfx::ScrollOffset delta = scroll_offset_->PullDeltaForMainThread();
1201 scroll_offset_->SetCurrent(current_offset);
1202 return delta;
1205 gfx::ScrollOffset LayerImpl::CurrentScrollOffset() const {
1206 return scroll_offset_->Current(IsActive());
1209 gfx::Vector2dF LayerImpl::ScrollDelta() const {
1210 if (IsActive())
1211 return gfx::Vector2dF(scroll_offset_->Delta().x(),
1212 scroll_offset_->Delta().y());
1213 else
1214 return gfx::Vector2dF(scroll_offset_->PendingDelta().get().x(),
1215 scroll_offset_->PendingDelta().get().y());
1218 void LayerImpl::SetScrollDelta(const gfx::Vector2dF& delta) {
1219 DCHECK(IsActive());
1220 DCHECK(scrollable() || delta.IsZero());
1221 SetCurrentScrollOffset(scroll_offset_->ActiveBase() +
1222 gfx::ScrollOffset(delta));
1225 gfx::ScrollOffset LayerImpl::BaseScrollOffset() const {
1226 if (IsActive())
1227 return scroll_offset_->ActiveBase();
1228 else
1229 return scroll_offset_->PendingBase();
1232 void LayerImpl::PushScrollOffset(const gfx::ScrollOffset* scroll_offset) {
1233 DCHECK(scroll_offset || IsActive());
1234 bool changed = false;
1235 if (scroll_offset) {
1236 DCHECK(!IsActive() || !layer_tree_impl_->FindPendingTreeLayerById(id()));
1237 changed |= scroll_offset_->PushFromMainThread(*scroll_offset);
1239 if (IsActive()) {
1240 changed |= scroll_offset_->PushPendingToActive();
1243 if (changed)
1244 DidUpdateScrollOffset(false);
1247 void LayerImpl::UpdatePropertyTreeForScrollingIfNeeded() {
1248 // TODO(enne): in the future, scrolling should update the scroll tree
1249 // directly instead of going through layers.
1250 if (transform_tree_index_ != -1) {
1251 TransformTree& transform_tree =
1252 layer_tree_impl()->property_trees()->transform_tree;
1253 TransformNode* node = transform_tree.Node(transform_tree_index_);
1254 gfx::ScrollOffset current_offset = scroll_offset_->Current(IsActive());
1255 if (node->data.scroll_offset != current_offset) {
1256 node->data.scroll_offset = current_offset;
1257 node->data.needs_local_transform_update = true;
1258 transform_tree.set_needs_update(true);
1263 void LayerImpl::DidUpdateScrollOffset(bool is_from_root_delegate) {
1264 DCHECK(scroll_offset_);
1266 if (!is_from_root_delegate)
1267 layer_tree_impl()->DidUpdateScrollOffset(id());
1268 NoteLayerPropertyChangedForSubtree();
1269 ScrollbarParametersDidChange(false);
1271 UpdatePropertyTreeForScrollingIfNeeded();
1273 // Inform the pending twin that a property changed.
1274 if (layer_tree_impl()->IsActiveTree()) {
1275 LayerImpl* pending_twin = layer_tree_impl()->FindPendingTreeLayerById(id());
1276 if (pending_twin)
1277 pending_twin->DidUpdateScrollOffset(is_from_root_delegate);
1281 void LayerImpl::SetDoubleSided(bool double_sided) {
1282 if (double_sided_ == double_sided)
1283 return;
1285 double_sided_ = double_sided;
1286 NoteLayerPropertyChangedForSubtree();
1289 SimpleEnclosedRegion LayerImpl::VisibleContentOpaqueRegion() const {
1290 if (contents_opaque())
1291 return SimpleEnclosedRegion(visible_content_rect());
1292 return SimpleEnclosedRegion();
1295 void LayerImpl::DidBeginTracing() {}
1297 void LayerImpl::ReleaseResources() {}
1299 void LayerImpl::RecreateResources() {
1302 gfx::ScrollOffset LayerImpl::MaxScrollOffset() const {
1303 if (!scroll_clip_layer_ || bounds().IsEmpty())
1304 return gfx::ScrollOffset();
1306 LayerImpl const* page_scale_layer = layer_tree_impl()->page_scale_layer();
1307 DCHECK(this != page_scale_layer);
1308 DCHECK(this != layer_tree_impl()->InnerViewportScrollLayer() ||
1309 IsContainerForFixedPositionLayers());
1311 float scale_factor = 1.f;
1312 for (LayerImpl const* current_layer = this;
1313 current_layer != scroll_clip_layer_->parent();
1314 current_layer = current_layer->parent()) {
1315 if (current_layer == page_scale_layer)
1316 scale_factor = layer_tree_impl()->current_page_scale_factor();
1319 gfx::SizeF scaled_scroll_bounds =
1320 gfx::ToFlooredSize(gfx::ScaleSize(BoundsForScrolling(), scale_factor));
1321 scaled_scroll_bounds = gfx::ToFlooredSize(scaled_scroll_bounds);
1323 gfx::ScrollOffset max_offset(
1324 scaled_scroll_bounds.width() - scroll_clip_layer_->bounds().width(),
1325 scaled_scroll_bounds.height() - scroll_clip_layer_->bounds().height());
1326 // We need the final scroll offset to be in CSS coords.
1327 max_offset.Scale(1 / scale_factor);
1328 max_offset.SetToMax(gfx::ScrollOffset());
1329 return max_offset;
1332 gfx::ScrollOffset LayerImpl::ClampScrollOffsetToLimits(
1333 gfx::ScrollOffset offset) const {
1334 offset.SetToMin(MaxScrollOffset());
1335 offset.SetToMax(gfx::ScrollOffset());
1336 return offset;
1339 gfx::Vector2dF LayerImpl::ClampScrollToMaxScrollOffset() {
1340 gfx::ScrollOffset old_offset = CurrentScrollOffset();
1341 gfx::ScrollOffset clamped_offset = ClampScrollOffsetToLimits(old_offset);
1342 gfx::Vector2dF delta = clamped_offset.DeltaFrom(old_offset);
1343 if (!delta.IsZero())
1344 ScrollBy(delta);
1345 return delta;
1348 void LayerImpl::SetScrollbarPosition(ScrollbarLayerImplBase* scrollbar_layer,
1349 LayerImpl* scrollbar_clip_layer,
1350 bool on_resize) const {
1351 DCHECK(scrollbar_layer);
1352 LayerImpl* page_scale_layer = layer_tree_impl()->page_scale_layer();
1354 DCHECK(this != page_scale_layer);
1355 DCHECK(scrollbar_clip_layer);
1356 gfx::RectF clip_rect(gfx::PointF(),
1357 scrollbar_clip_layer->BoundsForScrolling());
1359 // See comment in MaxScrollOffset() regarding the use of the content layer
1360 // bounds here.
1361 gfx::RectF scroll_rect(gfx::PointF(), BoundsForScrolling());
1363 if (scroll_rect.size().IsEmpty())
1364 return;
1366 gfx::ScrollOffset current_offset;
1367 for (LayerImpl const* current_layer = this;
1368 current_layer != scrollbar_clip_layer->parent();
1369 current_layer = current_layer->parent()) {
1370 current_offset += current_layer->CurrentScrollOffset();
1371 if (current_layer == page_scale_layer) {
1372 float scale_factor = layer_tree_impl()->current_page_scale_factor();
1373 current_offset.Scale(scale_factor);
1374 scroll_rect.Scale(scale_factor);
1378 bool scrollbar_needs_animation = false;
1379 scrollbar_needs_animation |= scrollbar_layer->SetVerticalAdjust(
1380 scrollbar_clip_layer->bounds_delta().y());
1381 if (scrollbar_layer->orientation() == HORIZONTAL) {
1382 float visible_ratio = clip_rect.width() / scroll_rect.width();
1383 scrollbar_needs_animation |=
1384 scrollbar_layer->SetCurrentPos(current_offset.x());
1385 scrollbar_needs_animation |=
1386 scrollbar_layer->SetMaximum(scroll_rect.width() - clip_rect.width());
1387 scrollbar_needs_animation |=
1388 scrollbar_layer->SetVisibleToTotalLengthRatio(visible_ratio);
1389 } else {
1390 float visible_ratio = clip_rect.height() / scroll_rect.height();
1391 bool y_offset_did_change =
1392 scrollbar_layer->SetCurrentPos(current_offset.y());
1393 scrollbar_needs_animation |= y_offset_did_change;
1394 scrollbar_needs_animation |=
1395 scrollbar_layer->SetMaximum(scroll_rect.height() - clip_rect.height());
1396 scrollbar_needs_animation |=
1397 scrollbar_layer->SetVisibleToTotalLengthRatio(visible_ratio);
1398 if (y_offset_did_change && layer_tree_impl()->IsActiveTree() &&
1399 this == layer_tree_impl()->InnerViewportScrollLayer()) {
1400 TRACE_COUNTER_ID1("cc", "scroll_offset_y", this->id(),
1401 current_offset.y());
1404 if (scrollbar_needs_animation) {
1405 layer_tree_impl()->set_needs_update_draw_properties();
1406 // TODO(wjmaclean) The scrollbar animator for the pinch-zoom scrollbars
1407 // should activate for every scroll on the main frame, not just the
1408 // scrolls that move the pinch virtual viewport (i.e. trigger from
1409 // either inner or outer viewport).
1410 if (scrollbar_animation_controller_) {
1411 // Non-overlay scrollbars shouldn't trigger animations.
1412 if (scrollbar_layer->is_overlay_scrollbar())
1413 scrollbar_animation_controller_->DidScrollUpdate(on_resize);
1418 void LayerImpl::DidBecomeActive() {
1419 if (layer_tree_impl_->settings().scrollbar_animator ==
1420 LayerTreeSettings::NO_ANIMATOR) {
1421 return;
1424 bool need_scrollbar_animation_controller = scrollable() && scrollbars_;
1425 if (!need_scrollbar_animation_controller) {
1426 scrollbar_animation_controller_ = nullptr;
1427 return;
1430 if (scrollbar_animation_controller_)
1431 return;
1433 scrollbar_animation_controller_ =
1434 layer_tree_impl_->CreateScrollbarAnimationController(this);
1437 void LayerImpl::ClearScrollbars() {
1438 if (!scrollbars_)
1439 return;
1441 scrollbars_.reset(nullptr);
1444 void LayerImpl::AddScrollbar(ScrollbarLayerImplBase* layer) {
1445 DCHECK(layer);
1446 DCHECK(!scrollbars_ || scrollbars_->find(layer) == scrollbars_->end());
1447 if (!scrollbars_)
1448 scrollbars_.reset(new ScrollbarSet());
1450 scrollbars_->insert(layer);
1453 void LayerImpl::RemoveScrollbar(ScrollbarLayerImplBase* layer) {
1454 DCHECK(scrollbars_);
1455 DCHECK(layer);
1456 DCHECK(scrollbars_->find(layer) != scrollbars_->end());
1458 scrollbars_->erase(layer);
1459 if (scrollbars_->empty())
1460 scrollbars_ = nullptr;
1463 bool LayerImpl::HasScrollbar(ScrollbarOrientation orientation) const {
1464 if (!scrollbars_)
1465 return false;
1467 for (ScrollbarSet::iterator it = scrollbars_->begin();
1468 it != scrollbars_->end();
1469 ++it)
1470 if ((*it)->orientation() == orientation)
1471 return true;
1473 return false;
1476 void LayerImpl::ScrollbarParametersDidChange(bool on_resize) {
1477 if (!scrollbars_)
1478 return;
1480 for (ScrollbarSet::iterator it = scrollbars_->begin();
1481 it != scrollbars_->end();
1482 ++it) {
1483 bool is_scroll_layer = (*it)->ScrollLayerId() == layer_id_;
1484 bool scroll_layer_resized = is_scroll_layer && on_resize;
1485 (*it)->ScrollbarParametersDidChange(scroll_layer_resized);
1489 void LayerImpl::SetNeedsPushProperties() {
1490 if (needs_push_properties_)
1491 return;
1492 if (!parent_should_know_need_push_properties() && parent_)
1493 parent_->AddDependentNeedsPushProperties();
1494 needs_push_properties_ = true;
1497 void LayerImpl::AddDependentNeedsPushProperties() {
1498 DCHECK_GE(num_dependents_need_push_properties_, 0);
1500 if (!parent_should_know_need_push_properties() && parent_)
1501 parent_->AddDependentNeedsPushProperties();
1503 num_dependents_need_push_properties_++;
1506 void LayerImpl::RemoveDependentNeedsPushProperties() {
1507 num_dependents_need_push_properties_--;
1508 DCHECK_GE(num_dependents_need_push_properties_, 0);
1510 if (!parent_should_know_need_push_properties() && parent_)
1511 parent_->RemoveDependentNeedsPushProperties();
1514 void LayerImpl::GetAllPrioritizedTilesForTracing(
1515 std::vector<PrioritizedTile>* prioritized_tiles) const {
1518 void LayerImpl::AsValueInto(base::trace_event::TracedValue* state) const {
1519 TracedValue::MakeDictIntoImplicitSnapshotWithCategory(
1520 TRACE_DISABLED_BY_DEFAULT("cc.debug"),
1521 state,
1522 "cc::LayerImpl",
1523 LayerTypeAsString(),
1524 this);
1525 state->SetInteger("layer_id", id());
1526 MathUtil::AddToTracedValue("bounds", bounds_, state);
1528 state->SetDouble("opacity", opacity());
1530 MathUtil::AddToTracedValue("position", position_, state);
1532 state->SetInteger("draws_content", DrawsContent());
1533 state->SetInteger("gpu_memory_usage",
1534 base::saturated_cast<int>(GPUMemoryUsageInBytes()));
1536 MathUtil::AddToTracedValue(
1537 "scroll_offset", scroll_offset_ ? scroll_offset_->Current(IsActive())
1538 : gfx::ScrollOffset(),
1539 state);
1541 MathUtil::AddToTracedValue("transform_origin", transform_origin_, state);
1543 bool clipped;
1544 gfx::QuadF layer_quad = MathUtil::MapQuad(
1545 screen_space_transform(), gfx::QuadF(gfx::Rect(bounds())), &clipped);
1546 MathUtil::AddToTracedValue("layer_quad", layer_quad, state);
1547 if (!touch_event_handler_region_.IsEmpty()) {
1548 state->BeginArray("touch_event_handler_region");
1549 touch_event_handler_region_.AsValueInto(state);
1550 state->EndArray();
1552 if (have_wheel_event_handlers_) {
1553 gfx::Rect wheel_rect(bounds());
1554 Region wheel_region(wheel_rect);
1555 state->BeginArray("wheel_event_handler_region");
1556 wheel_region.AsValueInto(state);
1557 state->EndArray();
1559 if (have_scroll_event_handlers_) {
1560 gfx::Rect scroll_rect(bounds());
1561 Region scroll_region(scroll_rect);
1562 state->BeginArray("scroll_event_handler_region");
1563 scroll_region.AsValueInto(state);
1564 state->EndArray();
1566 if (!non_fast_scrollable_region_.IsEmpty()) {
1567 state->BeginArray("non_fast_scrollable_region");
1568 non_fast_scrollable_region_.AsValueInto(state);
1569 state->EndArray();
1571 if (scroll_blocks_on_) {
1572 state->SetInteger("scroll_blocks_on", scroll_blocks_on_);
1575 state->BeginArray("children");
1576 for (size_t i = 0; i < children_.size(); ++i) {
1577 state->BeginDictionary();
1578 children_[i]->AsValueInto(state);
1579 state->EndDictionary();
1581 state->EndArray();
1582 if (mask_layer_) {
1583 state->BeginDictionary("mask_layer");
1584 mask_layer_->AsValueInto(state);
1585 state->EndDictionary();
1587 if (replica_layer_) {
1588 state->BeginDictionary("replica_layer");
1589 replica_layer_->AsValueInto(state);
1590 state->EndDictionary();
1593 if (scroll_parent_)
1594 state->SetInteger("scroll_parent", scroll_parent_->id());
1596 if (clip_parent_)
1597 state->SetInteger("clip_parent", clip_parent_->id());
1599 state->SetBoolean("can_use_lcd_text", can_use_lcd_text());
1600 state->SetBoolean("contents_opaque", contents_opaque());
1602 state->SetBoolean(
1603 "has_animation_bounds",
1604 layer_animation_controller()->HasAnimationThatInflatesBounds());
1606 gfx::BoxF box;
1607 if (LayerUtils::GetAnimationBounds(*this, &box))
1608 MathUtil::AddToTracedValue("animation_bounds", box, state);
1610 if (debug_info_.get()) {
1611 std::string str;
1612 debug_info_->AppendAsTraceFormat(&str);
1613 base::JSONReader json_reader;
1614 scoped_ptr<base::Value> debug_info_value(json_reader.ReadToValue(str));
1616 if (debug_info_value->IsType(base::Value::TYPE_DICTIONARY)) {
1617 base::DictionaryValue* dictionary_value = nullptr;
1618 bool converted_to_dictionary =
1619 debug_info_value->GetAsDictionary(&dictionary_value);
1620 DCHECK(converted_to_dictionary);
1621 for (base::DictionaryValue::Iterator it(*dictionary_value); !it.IsAtEnd();
1622 it.Advance()) {
1623 state->SetValue(it.key().data(), it.value().CreateDeepCopy());
1625 } else {
1626 NOTREACHED();
1630 if (!frame_timing_requests_.empty()) {
1631 state->BeginArray("frame_timing_requests");
1632 for (const auto& request : frame_timing_requests_) {
1633 state->BeginDictionary();
1634 state->SetInteger("request_id", request.id());
1635 MathUtil::AddToTracedValue("request_rect", request.rect(), state);
1636 state->EndDictionary();
1638 state->EndArray();
1642 bool LayerImpl::IsDrawnRenderSurfaceLayerListMember() const {
1643 return draw_properties_.last_drawn_render_surface_layer_list_id ==
1644 layer_tree_impl_->current_render_surface_list_id();
1647 size_t LayerImpl::GPUMemoryUsageInBytes() const { return 0; }
1649 void LayerImpl::RunMicroBenchmark(MicroBenchmarkImpl* benchmark) {
1650 benchmark->RunOnLayer(this);
1653 int LayerImpl::NumDescendantsThatDrawContent() const {
1654 return num_descendants_that_draw_content_;
1657 void LayerImpl::NotifyAnimationFinished(
1658 base::TimeTicks monotonic_time,
1659 Animation::TargetProperty target_property,
1660 int group) {
1661 if (target_property == Animation::SCROLL_OFFSET)
1662 layer_tree_impl_->InputScrollAnimationFinished();
1665 void LayerImpl::SetHasRenderSurface(bool should_have_render_surface) {
1666 if (!!render_surface() == should_have_render_surface)
1667 return;
1669 SetNeedsPushProperties();
1670 layer_tree_impl()->set_needs_update_draw_properties();
1671 if (should_have_render_surface) {
1672 render_surface_ = make_scoped_ptr(new RenderSurfaceImpl(this));
1673 return;
1675 render_surface_.reset();
1678 Region LayerImpl::GetInvalidationRegion() {
1679 return Region(update_rect_);
1682 gfx::Rect LayerImpl::GetEnclosingRectInTargetSpace() const {
1683 return MathUtil::MapEnclosingClippedRect(
1684 draw_properties_.target_space_transform, gfx::Rect(bounds()));
1687 gfx::Rect LayerImpl::GetScaledEnclosingRectInTargetSpace(float scale) const {
1688 gfx::Transform scaled_draw_transform =
1689 draw_properties_.target_space_transform;
1690 scaled_draw_transform.Scale(SK_MScalar1 / scale, SK_MScalar1 / scale);
1691 gfx::Size scaled_bounds = gfx::ToCeiledSize(gfx::ScaleSize(bounds(), scale));
1692 return MathUtil::MapEnclosingClippedRect(scaled_draw_transform,
1693 gfx::Rect(scaled_bounds));
1696 } // namespace cc