Supervised user whitelists: Cleanup
[chromium-blink-merge.git] / cc / layers / layer_impl.cc
blob5655596d0eb4e79c5ea4b95c4789c2fcb9fd7c78
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/strings/stringprintf.h"
9 #include "base/trace_event/trace_event.h"
10 #include "base/trace_event/trace_event_argument.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/layers/layer_utils.h"
20 #include "cc/layers/painted_scrollbar_layer_impl.h"
21 #include "cc/output/copy_output_request.h"
22 #include "cc/quads/debug_border_draw_quad.h"
23 #include "cc/quads/render_pass.h"
24 #include "cc/trees/layer_tree_host_common.h"
25 #include "cc/trees/layer_tree_impl.h"
26 #include "cc/trees/layer_tree_settings.h"
27 #include "cc/trees/proxy.h"
28 #include "ui/gfx/geometry/box_f.h"
29 #include "ui/gfx/geometry/point_conversions.h"
30 #include "ui/gfx/geometry/quad_f.h"
31 #include "ui/gfx/geometry/rect_conversions.h"
32 #include "ui/gfx/geometry/size_conversions.h"
33 #include "ui/gfx/geometry/vector2d_conversions.h"
35 namespace cc {
36 LayerImpl::LayerImpl(LayerTreeImpl* layer_impl, int id)
37 : LayerImpl(layer_impl, id, new LayerImpl::SyncedScrollOffset) {
40 LayerImpl::LayerImpl(LayerTreeImpl* tree_impl,
41 int id,
42 scoped_refptr<SyncedScrollOffset> scroll_offset)
43 : parent_(nullptr),
44 scroll_parent_(nullptr),
45 clip_parent_(nullptr),
46 mask_layer_id_(-1),
47 replica_layer_id_(-1),
48 layer_id_(id),
49 layer_tree_impl_(tree_impl),
50 scroll_offset_(scroll_offset),
51 scroll_clip_layer_(nullptr),
52 should_scroll_on_main_thread_(false),
53 have_wheel_event_handlers_(false),
54 have_scroll_event_handlers_(false),
55 scroll_blocks_on_(SCROLL_BLOCKS_ON_NONE),
56 user_scrollable_horizontal_(true),
57 user_scrollable_vertical_(true),
58 stacking_order_changed_(false),
59 double_sided_(true),
60 should_flatten_transform_(true),
61 should_flatten_transform_from_property_tree_(false),
62 layer_property_changed_(false),
63 masks_to_bounds_(false),
64 contents_opaque_(false),
65 is_root_for_isolated_group_(false),
66 use_parent_backface_visibility_(false),
67 draw_checkerboard_for_missing_tiles_(false),
68 draws_content_(false),
69 hide_layer_and_subtree_(false),
70 transform_is_invertible_(true),
71 is_container_for_fixed_position_layers_(false),
72 background_color_(0),
73 opacity_(1.0),
74 blend_mode_(SkXfermode::kSrcOver_Mode),
75 num_descendants_that_draw_content_(0),
76 transform_tree_index_(-1),
77 opacity_tree_index_(-1),
78 clip_tree_index_(-1),
79 draw_depth_(0.f),
80 needs_push_properties_(false),
81 num_dependents_need_push_properties_(0),
82 sorting_context_id_(0),
83 current_draw_mode_(DRAW_MODE_NONE),
84 frame_timing_requests_dirty_(false) {
85 DCHECK_GT(layer_id_, 0);
86 DCHECK(layer_tree_impl_);
87 layer_tree_impl_->RegisterLayer(this);
88 AnimationRegistrar* registrar = layer_tree_impl_->GetAnimationRegistrar();
89 layer_animation_controller_ =
90 registrar->GetAnimationControllerForId(layer_id_);
91 layer_animation_controller_->AddValueObserver(this);
92 if (IsActive()) {
93 layer_animation_controller_->set_value_provider(this);
94 layer_animation_controller_->set_layer_animation_delegate(this);
96 SetNeedsPushProperties();
99 LayerImpl::~LayerImpl() {
100 DCHECK_EQ(DRAW_MODE_NONE, current_draw_mode_);
102 layer_animation_controller_->RemoveValueObserver(this);
103 layer_animation_controller_->remove_value_provider(this);
104 layer_animation_controller_->remove_layer_animation_delegate(this);
106 if (!copy_requests_.empty() && layer_tree_impl_->IsActiveTree())
107 layer_tree_impl()->RemoveLayerWithCopyOutputRequest(this);
108 layer_tree_impl_->UnregisterLayer(this);
110 TRACE_EVENT_OBJECT_DELETED_WITH_ID(
111 TRACE_DISABLED_BY_DEFAULT("cc.debug"), "cc::LayerImpl", this);
114 void LayerImpl::AddChild(scoped_ptr<LayerImpl> child) {
115 child->SetParent(this);
116 DCHECK_EQ(layer_tree_impl(), child->layer_tree_impl());
117 children_.push_back(child.Pass());
118 layer_tree_impl()->set_needs_update_draw_properties();
121 scoped_ptr<LayerImpl> LayerImpl::RemoveChild(LayerImpl* child) {
122 for (OwnedLayerImplList::iterator it = children_.begin();
123 it != children_.end();
124 ++it) {
125 if (*it == child) {
126 scoped_ptr<LayerImpl> ret = children_.take(it);
127 children_.erase(it);
128 layer_tree_impl()->set_needs_update_draw_properties();
129 return ret.Pass();
132 return nullptr;
135 void LayerImpl::SetParent(LayerImpl* parent) {
136 if (parent_should_know_need_push_properties()) {
137 if (parent_)
138 parent_->RemoveDependentNeedsPushProperties();
139 if (parent)
140 parent->AddDependentNeedsPushProperties();
142 parent_ = parent;
145 void LayerImpl::ClearChildList() {
146 if (children_.empty())
147 return;
149 children_.clear();
150 layer_tree_impl()->set_needs_update_draw_properties();
153 bool LayerImpl::HasAncestor(const LayerImpl* ancestor) const {
154 if (!ancestor)
155 return false;
157 for (const LayerImpl* layer = this; layer; layer = layer->parent()) {
158 if (layer == ancestor)
159 return true;
162 return false;
165 void LayerImpl::SetScrollParent(LayerImpl* parent) {
166 if (scroll_parent_ == parent)
167 return;
169 if (parent)
170 DCHECK_EQ(layer_tree_impl()->LayerById(parent->id()), parent);
172 scroll_parent_ = parent;
173 SetNeedsPushProperties();
176 void LayerImpl::SetDebugInfo(
177 scoped_refptr<base::trace_event::ConvertableToTraceFormat> other) {
178 debug_info_ = other;
179 SetNeedsPushProperties();
182 void LayerImpl::SetScrollChildren(std::set<LayerImpl*>* children) {
183 if (scroll_children_.get() == children)
184 return;
185 scroll_children_.reset(children);
186 SetNeedsPushProperties();
189 void LayerImpl::SetNumDescendantsThatDrawContent(int num_descendants) {
190 if (num_descendants_that_draw_content_ == num_descendants)
191 return;
192 num_descendants_that_draw_content_ = num_descendants;
193 SetNeedsPushProperties();
196 void LayerImpl::SetClipParent(LayerImpl* ancestor) {
197 if (clip_parent_ == ancestor)
198 return;
200 clip_parent_ = ancestor;
201 SetNeedsPushProperties();
204 void LayerImpl::SetClipChildren(std::set<LayerImpl*>* children) {
205 if (clip_children_.get() == children)
206 return;
207 clip_children_.reset(children);
208 SetNeedsPushProperties();
211 void LayerImpl::PassCopyRequests(ScopedPtrVector<CopyOutputRequest>* requests) {
212 if (requests->empty())
213 return;
214 DCHECK(render_surface());
215 bool was_empty = copy_requests_.empty();
216 copy_requests_.insert_and_take(copy_requests_.end(), requests);
217 requests->clear();
219 if (was_empty && layer_tree_impl()->IsActiveTree())
220 layer_tree_impl()->AddLayerWithCopyOutputRequest(this);
221 NoteLayerPropertyChangedForSubtree();
224 void LayerImpl::TakeCopyRequestsAndTransformToTarget(
225 ScopedPtrVector<CopyOutputRequest>* requests) {
226 DCHECK(!copy_requests_.empty());
227 DCHECK(layer_tree_impl()->IsActiveTree());
228 DCHECK_EQ(render_target(), this);
230 size_t first_inserted_request = requests->size();
231 requests->insert_and_take(requests->end(), &copy_requests_);
232 copy_requests_.clear();
234 for (size_t i = first_inserted_request; i < requests->size(); ++i) {
235 CopyOutputRequest* request = requests->at(i);
236 if (!request->has_area())
237 continue;
239 gfx::Rect request_in_layer_space = request->area();
240 gfx::Rect request_in_content_space =
241 LayerRectToContentRect(request_in_layer_space);
242 request->set_area(MathUtil::MapEnclosingClippedRect(
243 draw_properties_.target_space_transform, request_in_content_space));
246 layer_tree_impl()->RemoveLayerWithCopyOutputRequest(this);
247 layer_tree_impl()->set_needs_update_draw_properties();
250 void LayerImpl::ClearRenderSurfaceLayerList() {
251 if (render_surface_)
252 render_surface_->ClearLayerLists();
255 void LayerImpl::PopulateSharedQuadState(SharedQuadState* state) const {
256 state->SetAll(
257 draw_properties_.target_space_transform, draw_properties_.content_bounds,
258 draw_properties_.visible_content_rect, draw_properties_.clip_rect,
259 draw_properties_.is_clipped, draw_properties_.opacity,
260 draw_properties_.blend_mode, sorting_context_id_);
263 void LayerImpl::PopulateScaledSharedQuadState(SharedQuadState* state,
264 float scale) const {
265 gfx::Transform scaled_draw_transform =
266 draw_properties_.target_space_transform;
267 scaled_draw_transform.Scale(SK_MScalar1 / scale, SK_MScalar1 / scale);
268 gfx::Size scaled_content_bounds =
269 gfx::ToCeiledSize(gfx::ScaleSize(bounds(), scale));
270 gfx::Rect scaled_visible_content_rect =
271 gfx::ScaleToEnclosingRect(visible_content_rect(), scale);
272 scaled_visible_content_rect.Intersect(gfx::Rect(scaled_content_bounds));
274 state->SetAll(scaled_draw_transform, scaled_content_bounds,
275 scaled_visible_content_rect, draw_properties().clip_rect,
276 draw_properties().is_clipped, draw_properties().opacity,
277 draw_properties().blend_mode, sorting_context_id_);
280 bool LayerImpl::WillDraw(DrawMode draw_mode,
281 ResourceProvider* resource_provider) {
282 // WillDraw/DidDraw must be matched.
283 DCHECK_NE(DRAW_MODE_NONE, draw_mode);
284 DCHECK_EQ(DRAW_MODE_NONE, current_draw_mode_);
285 current_draw_mode_ = draw_mode;
286 return true;
289 void LayerImpl::DidDraw(ResourceProvider* resource_provider) {
290 DCHECK_NE(DRAW_MODE_NONE, current_draw_mode_);
291 current_draw_mode_ = DRAW_MODE_NONE;
294 bool LayerImpl::ShowDebugBorders() const {
295 return layer_tree_impl()->debug_state().show_debug_borders;
298 void LayerImpl::GetDebugBorderProperties(SkColor* color, float* width) const {
299 if (draws_content_) {
300 *color = DebugColors::ContentLayerBorderColor();
301 *width = DebugColors::ContentLayerBorderWidth(layer_tree_impl());
302 return;
305 if (masks_to_bounds_) {
306 *color = DebugColors::MaskingLayerBorderColor();
307 *width = DebugColors::MaskingLayerBorderWidth(layer_tree_impl());
308 return;
311 *color = DebugColors::ContainerLayerBorderColor();
312 *width = DebugColors::ContainerLayerBorderWidth(layer_tree_impl());
315 void LayerImpl::AppendDebugBorderQuad(
316 RenderPass* render_pass,
317 const gfx::Size& content_bounds,
318 const SharedQuadState* shared_quad_state,
319 AppendQuadsData* append_quads_data) const {
320 SkColor color;
321 float width;
322 GetDebugBorderProperties(&color, &width);
323 AppendDebugBorderQuad(render_pass,
324 content_bounds,
325 shared_quad_state,
326 append_quads_data,
327 color,
328 width);
331 void LayerImpl::AppendDebugBorderQuad(RenderPass* render_pass,
332 const gfx::Size& content_bounds,
333 const SharedQuadState* shared_quad_state,
334 AppendQuadsData* append_quads_data,
335 SkColor color,
336 float width) const {
337 if (!ShowDebugBorders())
338 return;
340 gfx::Rect quad_rect(content_bounds);
341 gfx::Rect visible_quad_rect(quad_rect);
342 DebugBorderDrawQuad* debug_border_quad =
343 render_pass->CreateAndAppendDrawQuad<DebugBorderDrawQuad>();
344 debug_border_quad->SetNew(
345 shared_quad_state, quad_rect, visible_quad_rect, color, width);
346 if (contents_opaque()) {
347 // When opaque, draw a second inner border that is thicker than the outer
348 // border, but more transparent.
349 static const float kFillOpacity = 0.3f;
350 SkColor fill_color = SkColorSetA(
351 color, static_cast<uint8_t>(SkColorGetA(color) * kFillOpacity));
352 float fill_width = width * 3;
353 gfx::Rect fill_rect = quad_rect;
354 fill_rect.Inset(fill_width / 2.f, fill_width / 2.f);
355 if (fill_rect.IsEmpty())
356 return;
357 gfx::Rect visible_fill_rect =
358 gfx::IntersectRects(visible_quad_rect, fill_rect);
359 DebugBorderDrawQuad* fill_quad =
360 render_pass->CreateAndAppendDrawQuad<DebugBorderDrawQuad>();
361 fill_quad->SetNew(shared_quad_state, fill_rect, visible_fill_rect,
362 fill_color, fill_width);
366 bool LayerImpl::HasDelegatedContent() const {
367 return false;
370 bool LayerImpl::HasContributingDelegatedRenderPasses() const {
371 return false;
374 RenderPassId LayerImpl::FirstContributingRenderPassId() const {
375 return RenderPassId(0, 0);
378 RenderPassId LayerImpl::NextContributingRenderPassId(RenderPassId id) const {
379 return RenderPassId(0, 0);
382 void LayerImpl::GetContentsResourceId(ResourceProvider::ResourceId* resource_id,
383 gfx::Size* resource_size) const {
384 NOTREACHED();
385 *resource_id = 0;
388 gfx::Vector2dF LayerImpl::ScrollBy(const gfx::Vector2dF& scroll) {
389 gfx::ScrollOffset adjusted_scroll(scroll);
390 if (layer_tree_impl()->settings().use_pinch_virtual_viewport) {
391 if (!user_scrollable_horizontal_)
392 adjusted_scroll.set_x(0);
393 if (!user_scrollable_vertical_)
394 adjusted_scroll.set_y(0);
396 DCHECK(scrollable());
397 gfx::ScrollOffset old_offset = CurrentScrollOffset();
398 gfx::ScrollOffset new_offset =
399 ClampScrollOffsetToLimits(old_offset + adjusted_scroll);
400 SetCurrentScrollOffset(new_offset);
402 gfx::ScrollOffset unscrolled =
403 old_offset + gfx::ScrollOffset(scroll) - new_offset;
404 return gfx::Vector2dF(unscrolled.x(), unscrolled.y());
407 void LayerImpl::SetScrollClipLayer(int scroll_clip_layer_id) {
408 scroll_clip_layer_ = layer_tree_impl()->LayerById(scroll_clip_layer_id);
411 bool LayerImpl::user_scrollable(ScrollbarOrientation orientation) const {
412 return (orientation == HORIZONTAL) ? user_scrollable_horizontal_
413 : user_scrollable_vertical_;
416 void LayerImpl::ApplySentScrollDeltasFromAbortedCommit() {
417 DCHECK(layer_tree_impl()->IsActiveTree());
418 scroll_offset_->AbortCommit();
421 InputHandler::ScrollStatus LayerImpl::TryScroll(
422 const gfx::PointF& screen_space_point,
423 InputHandler::ScrollInputType type,
424 ScrollBlocksOn effective_block_mode) const {
425 if (should_scroll_on_main_thread()) {
426 TRACE_EVENT0("cc", "LayerImpl::TryScroll: Failed ShouldScrollOnMainThread");
427 return InputHandler::SCROLL_ON_MAIN_THREAD;
430 if (!screen_space_transform().IsInvertible()) {
431 TRACE_EVENT0("cc", "LayerImpl::TryScroll: Ignored NonInvertibleTransform");
432 return InputHandler::SCROLL_IGNORED;
435 if (!non_fast_scrollable_region().IsEmpty()) {
436 bool clipped = false;
437 gfx::Transform inverse_screen_space_transform(
438 gfx::Transform::kSkipInitialization);
439 if (!screen_space_transform().GetInverse(&inverse_screen_space_transform)) {
440 // TODO(shawnsingh): We shouldn't be applying a projection if screen space
441 // transform is uninvertible here. Perhaps we should be returning
442 // SCROLL_ON_MAIN_THREAD in this case?
445 gfx::PointF hit_test_point_in_content_space =
446 MathUtil::ProjectPoint(inverse_screen_space_transform,
447 screen_space_point,
448 &clipped);
449 gfx::PointF hit_test_point_in_layer_space =
450 gfx::ScalePoint(hit_test_point_in_content_space,
451 1.f / contents_scale_x(),
452 1.f / contents_scale_y());
453 if (!clipped &&
454 non_fast_scrollable_region().Contains(
455 gfx::ToRoundedPoint(hit_test_point_in_layer_space))) {
456 TRACE_EVENT0("cc",
457 "LayerImpl::tryScroll: Failed NonFastScrollableRegion");
458 return InputHandler::SCROLL_ON_MAIN_THREAD;
462 if (have_scroll_event_handlers() &&
463 effective_block_mode & SCROLL_BLOCKS_ON_SCROLL_EVENT) {
464 TRACE_EVENT0("cc", "LayerImpl::tryScroll: Failed ScrollEventHandlers");
465 return InputHandler::SCROLL_ON_MAIN_THREAD;
468 if (type == InputHandler::WHEEL && have_wheel_event_handlers() &&
469 effective_block_mode & SCROLL_BLOCKS_ON_WHEEL_EVENT) {
470 TRACE_EVENT0("cc", "LayerImpl::tryScroll: Failed WheelEventHandlers");
471 return InputHandler::SCROLL_ON_MAIN_THREAD;
474 if (!scrollable()) {
475 TRACE_EVENT0("cc", "LayerImpl::tryScroll: Ignored not scrollable");
476 return InputHandler::SCROLL_IGNORED;
479 gfx::ScrollOffset max_scroll_offset = MaxScrollOffset();
480 if (max_scroll_offset.x() <= 0 && max_scroll_offset.y() <= 0) {
481 TRACE_EVENT0("cc",
482 "LayerImpl::tryScroll: Ignored. Technically scrollable,"
483 " but has no affordance in either direction.");
484 return InputHandler::SCROLL_IGNORED;
487 return InputHandler::SCROLL_STARTED;
490 gfx::Rect LayerImpl::LayerRectToContentRect(
491 const gfx::RectF& layer_rect) const {
492 gfx::RectF content_rect =
493 gfx::ScaleRect(layer_rect, contents_scale_x(), contents_scale_y());
494 // Intersect with content rect to avoid the extra pixel because for some
495 // values x and y, ceil((x / y) * y) may be x + 1.
496 content_rect.Intersect(gfx::Rect(content_bounds()));
497 return gfx::ToEnclosingRect(content_rect);
500 skia::RefPtr<SkPicture> LayerImpl::GetPicture() {
501 return skia::RefPtr<SkPicture>();
504 scoped_ptr<LayerImpl> LayerImpl::CreateLayerImpl(LayerTreeImpl* tree_impl) {
505 return LayerImpl::Create(tree_impl, layer_id_, scroll_offset_);
508 void LayerImpl::PushPropertiesTo(LayerImpl* layer) {
509 layer->SetTransformOrigin(transform_origin_);
510 layer->SetBackgroundColor(background_color_);
511 layer->SetBounds(bounds_);
512 layer->SetContentBounds(content_bounds());
513 layer->SetContentsScale(contents_scale_x(), contents_scale_y());
514 layer->SetDoubleSided(double_sided_);
515 layer->SetDrawCheckerboardForMissingTiles(
516 draw_checkerboard_for_missing_tiles_);
517 layer->SetDrawsContent(DrawsContent());
518 layer->SetHideLayerAndSubtree(hide_layer_and_subtree_);
519 layer->SetHasRenderSurface(!!render_surface() || layer->HasCopyRequest());
520 layer->SetFilters(filters());
521 layer->SetBackgroundFilters(background_filters());
522 layer->SetMasksToBounds(masks_to_bounds_);
523 layer->SetShouldScrollOnMainThread(should_scroll_on_main_thread_);
524 layer->SetHaveWheelEventHandlers(have_wheel_event_handlers_);
525 layer->SetHaveScrollEventHandlers(have_scroll_event_handlers_);
526 layer->SetScrollBlocksOn(scroll_blocks_on_);
527 layer->SetNonFastScrollableRegion(non_fast_scrollable_region_);
528 layer->SetTouchEventHandlerRegion(touch_event_handler_region_);
529 layer->SetContentsOpaque(contents_opaque_);
530 layer->SetOpacity(opacity_);
531 layer->SetBlendMode(blend_mode_);
532 layer->SetIsRootForIsolatedGroup(is_root_for_isolated_group_);
533 layer->SetPosition(position_);
534 layer->SetIsContainerForFixedPositionLayers(
535 is_container_for_fixed_position_layers_);
536 layer->SetPositionConstraint(position_constraint_);
537 layer->SetShouldFlattenTransform(should_flatten_transform_);
538 layer->set_should_flatten_transform_from_property_tree(
539 should_flatten_transform_from_property_tree_);
540 layer->SetUseParentBackfaceVisibility(use_parent_backface_visibility_);
541 layer->SetTransformAndInvertibility(transform_, transform_is_invertible_);
543 layer->SetScrollClipLayer(scroll_clip_layer_ ? scroll_clip_layer_->id()
544 : Layer::INVALID_ID);
545 layer->set_user_scrollable_horizontal(user_scrollable_horizontal_);
546 layer->set_user_scrollable_vertical(user_scrollable_vertical_);
548 layer->SetScrollCompensationAdjustment(scroll_compensation_adjustment_);
550 layer->PushScrollOffset(nullptr);
552 layer->Set3dSortingContextId(sorting_context_id_);
553 layer->SetNumDescendantsThatDrawContent(num_descendants_that_draw_content_);
555 layer->set_transform_tree_index(transform_tree_index_);
556 layer->set_opacity_tree_index(opacity_tree_index_);
557 layer->set_clip_tree_index(clip_tree_index_);
558 layer->set_offset_to_transform_parent(offset_to_transform_parent_);
560 LayerImpl* scroll_parent = nullptr;
561 if (scroll_parent_) {
562 scroll_parent = layer->layer_tree_impl()->LayerById(scroll_parent_->id());
563 DCHECK(scroll_parent);
566 layer->SetScrollParent(scroll_parent);
567 if (scroll_children_) {
568 std::set<LayerImpl*>* scroll_children = new std::set<LayerImpl*>;
569 for (std::set<LayerImpl*>::iterator it = scroll_children_->begin();
570 it != scroll_children_->end();
571 ++it) {
572 DCHECK_EQ((*it)->scroll_parent(), this);
573 LayerImpl* scroll_child =
574 layer->layer_tree_impl()->LayerById((*it)->id());
575 DCHECK(scroll_child);
576 scroll_children->insert(scroll_child);
578 layer->SetScrollChildren(scroll_children);
579 } else {
580 layer->SetScrollChildren(nullptr);
583 LayerImpl* clip_parent = nullptr;
584 if (clip_parent_) {
585 clip_parent = layer->layer_tree_impl()->LayerById(
586 clip_parent_->id());
587 DCHECK(clip_parent);
590 layer->SetClipParent(clip_parent);
591 if (clip_children_) {
592 std::set<LayerImpl*>* clip_children = new std::set<LayerImpl*>;
593 for (std::set<LayerImpl*>::iterator it = clip_children_->begin();
594 it != clip_children_->end(); ++it)
595 clip_children->insert(layer->layer_tree_impl()->LayerById((*it)->id()));
596 layer->SetClipChildren(clip_children);
597 } else {
598 layer->SetClipChildren(nullptr);
601 layer->PassCopyRequests(&copy_requests_);
603 // If the main thread commits multiple times before the impl thread actually
604 // draws, then damage tracking will become incorrect if we simply clobber the
605 // update_rect here. The LayerImpl's update_rect needs to accumulate (i.e.
606 // union) any update changes that have occurred on the main thread.
607 update_rect_.Union(layer->update_rect());
608 layer->SetUpdateRect(update_rect_);
610 layer->SetStackingOrderChanged(stacking_order_changed_);
611 layer->SetDebugInfo(debug_info_);
613 if (frame_timing_requests_dirty_) {
614 layer->PassFrameTimingRequests(&frame_timing_requests_);
615 frame_timing_requests_dirty_ = false;
618 // Reset any state that should be cleared for the next update.
619 stacking_order_changed_ = false;
620 update_rect_ = gfx::Rect();
621 needs_push_properties_ = false;
622 num_dependents_need_push_properties_ = 0;
625 gfx::Vector2dF LayerImpl::FixedContainerSizeDelta() const {
626 if (!scroll_clip_layer_)
627 return gfx::Vector2dF();
629 gfx::Vector2dF delta_from_scroll = scroll_clip_layer_->bounds_delta();
631 // In virtual-viewport mode, we don't need to compensate for pinch zoom or
632 // scale since the fixed container is the outer viewport, which sits below
633 // the page scale.
634 if (layer_tree_impl()->settings().use_pinch_virtual_viewport)
635 return delta_from_scroll;
637 float scale_delta = layer_tree_impl()->page_scale_delta();
638 float scale = layer_tree_impl()->current_page_scale_factor() /
639 layer_tree_impl()->page_scale_delta();
641 delta_from_scroll.Scale(1.f / scale);
643 // The delta-from-pinch component requires some explanation: A viewport of
644 // size (w,h) will appear to be size (w/s,h/s) under scale s in the content
645 // space. If s -> s' on the impl thread, where s' = s * ds, then the apparent
646 // viewport size change in the content space due to ds is:
648 // (w/s',h/s') - (w/s,h/s) = (w,h)(1/s' - 1/s) = (w,h)(1 - ds)/(s ds)
650 gfx::Vector2dF delta_from_pinch =
651 gfx::Rect(scroll_clip_layer_->bounds()).bottom_right() - gfx::PointF();
652 delta_from_pinch.Scale((1.f - scale_delta) / (scale * scale_delta));
654 return delta_from_scroll + delta_from_pinch;
657 base::DictionaryValue* LayerImpl::LayerTreeAsJson() const {
658 base::DictionaryValue* result = new base::DictionaryValue;
659 result->SetString("LayerType", LayerTypeAsString());
661 base::ListValue* list = new base::ListValue;
662 list->AppendInteger(bounds().width());
663 list->AppendInteger(bounds().height());
664 result->Set("Bounds", list);
666 list = new base::ListValue;
667 list->AppendDouble(position_.x());
668 list->AppendDouble(position_.y());
669 result->Set("Position", list);
671 const gfx::Transform& gfx_transform = draw_properties_.target_space_transform;
672 double transform[16];
673 gfx_transform.matrix().asColMajord(transform);
674 list = new base::ListValue;
675 for (int i = 0; i < 16; ++i)
676 list->AppendDouble(transform[i]);
677 result->Set("DrawTransform", list);
679 result->SetBoolean("DrawsContent", draws_content_);
680 result->SetBoolean("Is3dSorted", Is3dSorted());
681 result->SetDouble("OPACITY", opacity());
682 result->SetBoolean("ContentsOpaque", contents_opaque_);
684 if (scrollable())
685 result->SetBoolean("Scrollable", true);
687 if (have_wheel_event_handlers_)
688 result->SetBoolean("WheelHandler", have_wheel_event_handlers_);
689 if (have_scroll_event_handlers_)
690 result->SetBoolean("ScrollHandler", have_scroll_event_handlers_);
691 if (!touch_event_handler_region_.IsEmpty()) {
692 scoped_ptr<base::Value> region = touch_event_handler_region_.AsValue();
693 result->Set("TouchRegion", region.release());
696 if (scroll_blocks_on_) {
697 list = new base::ListValue;
698 if (scroll_blocks_on_ & SCROLL_BLOCKS_ON_START_TOUCH)
699 list->AppendString("StartTouch");
700 if (scroll_blocks_on_ & SCROLL_BLOCKS_ON_WHEEL_EVENT)
701 list->AppendString("WheelEvent");
702 if (scroll_blocks_on_ & SCROLL_BLOCKS_ON_SCROLL_EVENT)
703 list->AppendString("ScrollEvent");
704 result->Set("ScrollBlocksOn", list);
707 list = new base::ListValue;
708 for (size_t i = 0; i < children_.size(); ++i)
709 list->Append(children_[i]->LayerTreeAsJson());
710 result->Set("Children", list);
712 return result;
715 void LayerImpl::SetStackingOrderChanged(bool stacking_order_changed) {
716 if (stacking_order_changed) {
717 stacking_order_changed_ = true;
718 NoteLayerPropertyChangedForSubtree();
722 void LayerImpl::NoteLayerPropertyChanged() {
723 layer_property_changed_ = true;
724 layer_tree_impl()->set_needs_update_draw_properties();
725 SetNeedsPushProperties();
728 void LayerImpl::NoteLayerPropertyChangedForSubtree() {
729 layer_property_changed_ = true;
730 layer_tree_impl()->set_needs_update_draw_properties();
731 for (size_t i = 0; i < children_.size(); ++i)
732 children_[i]->NoteLayerPropertyChangedForDescendantsInternal();
733 SetNeedsPushProperties();
736 void LayerImpl::NoteLayerPropertyChangedForDescendantsInternal() {
737 layer_property_changed_ = true;
738 for (size_t i = 0; i < children_.size(); ++i)
739 children_[i]->NoteLayerPropertyChangedForDescendantsInternal();
742 void LayerImpl::NoteLayerPropertyChangedForDescendants() {
743 layer_tree_impl()->set_needs_update_draw_properties();
744 for (size_t i = 0; i < children_.size(); ++i)
745 children_[i]->NoteLayerPropertyChangedForDescendantsInternal();
746 SetNeedsPushProperties();
749 const char* LayerImpl::LayerTypeAsString() const {
750 return "cc::LayerImpl";
753 void LayerImpl::ResetAllChangeTrackingForSubtree() {
754 layer_property_changed_ = false;
756 update_rect_ = gfx::Rect();
757 damage_rect_ = gfx::RectF();
759 if (render_surface_)
760 render_surface_->ResetPropertyChangedFlag();
762 if (mask_layer_)
763 mask_layer_->ResetAllChangeTrackingForSubtree();
765 if (replica_layer_) {
766 // This also resets the replica mask, if it exists.
767 replica_layer_->ResetAllChangeTrackingForSubtree();
770 for (size_t i = 0; i < children_.size(); ++i)
771 children_[i]->ResetAllChangeTrackingForSubtree();
773 needs_push_properties_ = false;
774 num_dependents_need_push_properties_ = 0;
777 gfx::ScrollOffset LayerImpl::ScrollOffsetForAnimation() const {
778 return CurrentScrollOffset();
781 void LayerImpl::OnFilterAnimated(const FilterOperations& filters) {
782 SetFilters(filters);
785 void LayerImpl::OnOpacityAnimated(float opacity) {
786 SetOpacity(opacity);
789 void LayerImpl::OnTransformAnimated(const gfx::Transform& transform) {
790 SetTransform(transform);
793 void LayerImpl::OnScrollOffsetAnimated(const gfx::ScrollOffset& scroll_offset) {
794 // Only layers in the active tree should need to do anything here, since
795 // layers in the pending tree will find out about these changes as a
796 // result of the shared SyncedProperty.
797 if (!IsActive())
798 return;
800 SetCurrentScrollOffset(scroll_offset);
802 layer_tree_impl_->DidAnimateScrollOffset();
805 void LayerImpl::OnAnimationWaitingForDeletion() {}
807 bool LayerImpl::IsActive() const {
808 return layer_tree_impl_->IsActiveTree();
811 gfx::Size LayerImpl::bounds() const {
812 gfx::Vector2d delta = gfx::ToCeiledVector2d(bounds_delta_);
813 return gfx::Size(bounds_.width() + delta.x(),
814 bounds_.height() + delta.y());
817 gfx::SizeF LayerImpl::BoundsForScrolling() const {
818 return gfx::SizeF(bounds_.width() + bounds_delta_.x(),
819 bounds_.height() + bounds_delta_.y());
822 void LayerImpl::SetBounds(const gfx::Size& bounds) {
823 if (bounds_ == bounds)
824 return;
826 bounds_ = bounds;
828 ScrollbarParametersDidChange(true);
829 if (masks_to_bounds())
830 NoteLayerPropertyChangedForSubtree();
831 else
832 NoteLayerPropertyChanged();
835 void LayerImpl::SetBoundsDelta(const gfx::Vector2dF& bounds_delta) {
836 if (bounds_delta_ == bounds_delta)
837 return;
839 bounds_delta_ = bounds_delta;
841 ScrollbarParametersDidChange(true);
842 if (masks_to_bounds())
843 NoteLayerPropertyChangedForSubtree();
844 else
845 NoteLayerPropertyChanged();
848 void LayerImpl::SetMaskLayer(scoped_ptr<LayerImpl> mask_layer) {
849 int new_layer_id = mask_layer ? mask_layer->id() : -1;
851 if (mask_layer) {
852 DCHECK_EQ(layer_tree_impl(), mask_layer->layer_tree_impl());
853 DCHECK_NE(new_layer_id, mask_layer_id_);
854 } else if (new_layer_id == mask_layer_id_) {
855 return;
858 mask_layer_ = mask_layer.Pass();
859 mask_layer_id_ = new_layer_id;
860 if (mask_layer_)
861 mask_layer_->SetParent(this);
862 NoteLayerPropertyChangedForSubtree();
865 scoped_ptr<LayerImpl> LayerImpl::TakeMaskLayer() {
866 mask_layer_id_ = -1;
867 return mask_layer_.Pass();
870 void LayerImpl::SetReplicaLayer(scoped_ptr<LayerImpl> replica_layer) {
871 int new_layer_id = replica_layer ? replica_layer->id() : -1;
873 if (replica_layer) {
874 DCHECK_EQ(layer_tree_impl(), replica_layer->layer_tree_impl());
875 DCHECK_NE(new_layer_id, replica_layer_id_);
876 } else if (new_layer_id == replica_layer_id_) {
877 return;
880 replica_layer_ = replica_layer.Pass();
881 replica_layer_id_ = new_layer_id;
882 if (replica_layer_)
883 replica_layer_->SetParent(this);
884 NoteLayerPropertyChangedForSubtree();
887 scoped_ptr<LayerImpl> LayerImpl::TakeReplicaLayer() {
888 replica_layer_id_ = -1;
889 return replica_layer_.Pass();
892 ScrollbarLayerImplBase* LayerImpl::ToScrollbarLayer() {
893 return nullptr;
896 void LayerImpl::SetDrawsContent(bool draws_content) {
897 if (draws_content_ == draws_content)
898 return;
900 draws_content_ = draws_content;
901 NoteLayerPropertyChanged();
904 void LayerImpl::SetHideLayerAndSubtree(bool hide) {
905 if (hide_layer_and_subtree_ == hide)
906 return;
908 hide_layer_and_subtree_ = hide;
909 NoteLayerPropertyChangedForSubtree();
912 void LayerImpl::SetTransformOrigin(const gfx::Point3F& transform_origin) {
913 if (transform_origin_ == transform_origin)
914 return;
915 transform_origin_ = transform_origin;
916 NoteLayerPropertyChangedForSubtree();
919 void LayerImpl::SetBackgroundColor(SkColor background_color) {
920 if (background_color_ == background_color)
921 return;
923 background_color_ = background_color;
924 NoteLayerPropertyChanged();
927 SkColor LayerImpl::SafeOpaqueBackgroundColor() const {
928 SkColor color = background_color();
929 if (SkColorGetA(color) == 255 && !contents_opaque()) {
930 color = SK_ColorTRANSPARENT;
931 } else if (SkColorGetA(color) != 255 && contents_opaque()) {
932 for (const LayerImpl* layer = parent(); layer;
933 layer = layer->parent()) {
934 color = layer->background_color();
935 if (SkColorGetA(color) == 255)
936 break;
938 if (SkColorGetA(color) != 255)
939 color = layer_tree_impl()->background_color();
940 if (SkColorGetA(color) != 255)
941 color = SkColorSetA(color, 255);
943 return color;
946 void LayerImpl::SetFilters(const FilterOperations& filters) {
947 if (filters_ == filters)
948 return;
950 filters_ = filters;
951 NoteLayerPropertyChangedForSubtree();
954 bool LayerImpl::FilterIsAnimating() const {
955 return layer_animation_controller_->IsAnimatingProperty(Animation::FILTER);
958 bool LayerImpl::FilterIsAnimatingOnImplOnly() const {
959 Animation* filter_animation =
960 layer_animation_controller_->GetAnimation(Animation::FILTER);
961 return filter_animation && filter_animation->is_impl_only();
964 void LayerImpl::SetBackgroundFilters(
965 const FilterOperations& filters) {
966 if (background_filters_ == filters)
967 return;
969 background_filters_ = filters;
970 NoteLayerPropertyChanged();
973 void LayerImpl::SetMasksToBounds(bool masks_to_bounds) {
974 if (masks_to_bounds_ == masks_to_bounds)
975 return;
977 masks_to_bounds_ = masks_to_bounds;
978 NoteLayerPropertyChangedForSubtree();
981 void LayerImpl::SetContentsOpaque(bool opaque) {
982 if (contents_opaque_ == opaque)
983 return;
985 contents_opaque_ = opaque;
986 NoteLayerPropertyChangedForSubtree();
989 void LayerImpl::SetOpacity(float opacity) {
990 if (opacity_ == opacity)
991 return;
993 opacity_ = opacity;
994 NoteLayerPropertyChangedForSubtree();
997 bool LayerImpl::OpacityIsAnimating() const {
998 return layer_animation_controller_->IsAnimatingProperty(Animation::OPACITY);
1001 bool LayerImpl::OpacityIsAnimatingOnImplOnly() const {
1002 Animation* opacity_animation =
1003 layer_animation_controller_->GetAnimation(Animation::OPACITY);
1004 return opacity_animation && opacity_animation->is_impl_only();
1007 void LayerImpl::SetBlendMode(SkXfermode::Mode blend_mode) {
1008 if (blend_mode_ == blend_mode)
1009 return;
1011 blend_mode_ = blend_mode;
1012 NoteLayerPropertyChangedForSubtree();
1015 void LayerImpl::SetIsRootForIsolatedGroup(bool root) {
1016 if (is_root_for_isolated_group_ == root)
1017 return;
1019 is_root_for_isolated_group_ = root;
1020 SetNeedsPushProperties();
1023 void LayerImpl::SetPosition(const gfx::PointF& position) {
1024 if (position_ == position)
1025 return;
1027 position_ = position;
1028 NoteLayerPropertyChangedForSubtree();
1031 void LayerImpl::SetShouldFlattenTransform(bool flatten) {
1032 if (should_flatten_transform_ == flatten)
1033 return;
1035 should_flatten_transform_ = flatten;
1036 NoteLayerPropertyChangedForSubtree();
1039 void LayerImpl::Set3dSortingContextId(int id) {
1040 if (id == sorting_context_id_)
1041 return;
1042 sorting_context_id_ = id;
1043 NoteLayerPropertyChangedForSubtree();
1046 void LayerImpl::PassFrameTimingRequests(
1047 std::vector<FrameTimingRequest>* requests) {
1048 frame_timing_requests_.swap(*requests);
1049 frame_timing_requests_dirty_ = true;
1050 SetNeedsPushProperties();
1053 void LayerImpl::GatherFrameTimingRequestIds(std::vector<int64_t>* request_ids) {
1054 for (const auto& request : frame_timing_requests_)
1055 request_ids->push_back(request.id());
1058 void LayerImpl::SetTransform(const gfx::Transform& transform) {
1059 if (transform_ == transform)
1060 return;
1062 transform_ = transform;
1063 transform_is_invertible_ = transform_.IsInvertible();
1064 NoteLayerPropertyChangedForSubtree();
1067 void LayerImpl::SetTransformAndInvertibility(const gfx::Transform& transform,
1068 bool transform_is_invertible) {
1069 if (transform_ == transform) {
1070 DCHECK(transform_is_invertible_ == transform_is_invertible)
1071 << "Can't change invertibility if transform is unchanged";
1072 return;
1074 transform_ = transform;
1075 transform_is_invertible_ = transform_is_invertible;
1076 NoteLayerPropertyChangedForSubtree();
1079 bool LayerImpl::TransformIsAnimating() const {
1080 return layer_animation_controller_->IsAnimatingProperty(Animation::TRANSFORM);
1083 bool LayerImpl::TransformIsAnimatingOnImplOnly() const {
1084 Animation* transform_animation =
1085 layer_animation_controller_->GetAnimation(Animation::TRANSFORM);
1086 return transform_animation && transform_animation->is_impl_only();
1089 void LayerImpl::SetUpdateRect(const gfx::Rect& update_rect) {
1090 update_rect_ = update_rect;
1091 SetNeedsPushProperties();
1094 void LayerImpl::AddDamageRect(const gfx::RectF& damage_rect) {
1095 damage_rect_ = gfx::UnionRects(damage_rect_, damage_rect);
1098 void LayerImpl::SetContentBounds(const gfx::Size& content_bounds) {
1099 if (this->content_bounds() == content_bounds)
1100 return;
1102 draw_properties_.content_bounds = content_bounds;
1103 NoteLayerPropertyChanged();
1106 void LayerImpl::SetContentsScale(float contents_scale_x,
1107 float contents_scale_y) {
1108 if (this->contents_scale_x() == contents_scale_x &&
1109 this->contents_scale_y() == contents_scale_y)
1110 return;
1112 draw_properties_.contents_scale_x = contents_scale_x;
1113 draw_properties_.contents_scale_y = contents_scale_y;
1114 NoteLayerPropertyChanged();
1117 bool LayerImpl::IsExternalFlingActive() const {
1118 return layer_tree_impl_->IsExternalFlingActive();
1121 void LayerImpl::SetCurrentScrollOffset(const gfx::ScrollOffset& scroll_offset) {
1122 DCHECK(IsActive());
1123 if (scroll_offset_->SetCurrent(scroll_offset))
1124 DidUpdateScrollOffset(false);
1127 void LayerImpl::SetCurrentScrollOffsetFromDelegate(
1128 const gfx::ScrollOffset& scroll_offset) {
1129 DCHECK(IsActive());
1130 if (scroll_offset_->SetCurrent(scroll_offset))
1131 DidUpdateScrollOffset(true);
1134 void LayerImpl::PushScrollOffsetFromMainThread(
1135 const gfx::ScrollOffset& scroll_offset) {
1136 PushScrollOffset(&scroll_offset);
1139 void LayerImpl::PushScrollOffsetFromMainThreadAndClobberActiveValue(
1140 const gfx::ScrollOffset& scroll_offset) {
1141 scroll_offset_->set_clobber_active_value();
1142 PushScrollOffset(&scroll_offset);
1145 gfx::ScrollOffset LayerImpl::PullDeltaForMainThread() {
1146 // TODO(miletus): Remove all this temporary flooring machinery when
1147 // Blink fully supports fractional scrolls.
1148 gfx::ScrollOffset current_offset = CurrentScrollOffset();
1149 gfx::Vector2dF current_delta = ScrollDelta();
1150 gfx::Vector2dF floored_delta(floor(current_delta.x()),
1151 floor(current_delta.y()));
1152 gfx::Vector2dF diff_delta = floored_delta - current_delta;
1153 gfx::ScrollOffset tmp_offset = ScrollOffsetWithDelta(current_offset,
1154 diff_delta);
1155 scroll_offset_->SetCurrent(tmp_offset);
1156 gfx::ScrollOffset delta = scroll_offset_->PullDeltaForMainThread();
1157 scroll_offset_->SetCurrent(current_offset);
1158 return delta;
1161 gfx::ScrollOffset LayerImpl::CurrentScrollOffset() const {
1162 return scroll_offset_->Current(IsActive());
1165 gfx::Vector2dF LayerImpl::ScrollDelta() const {
1166 if (IsActive())
1167 return gfx::Vector2dF(scroll_offset_->Delta().x(),
1168 scroll_offset_->Delta().y());
1169 else
1170 return gfx::Vector2dF(scroll_offset_->PendingDelta().get().x(),
1171 scroll_offset_->PendingDelta().get().y());
1174 void LayerImpl::SetScrollDelta(const gfx::Vector2dF& delta) {
1175 DCHECK(IsActive());
1176 SetCurrentScrollOffset(scroll_offset_->ActiveBase() +
1177 gfx::ScrollOffset(delta));
1180 gfx::ScrollOffset LayerImpl::BaseScrollOffset() const {
1181 if (IsActive())
1182 return scroll_offset_->ActiveBase();
1183 else
1184 return scroll_offset_->PendingBase();
1187 void LayerImpl::PushScrollOffset(const gfx::ScrollOffset* scroll_offset) {
1188 DCHECK(scroll_offset || IsActive());
1189 bool changed = false;
1190 if (scroll_offset) {
1191 DCHECK(!IsActive() || !layer_tree_impl_->FindPendingTreeLayerById(id()));
1192 changed |= scroll_offset_->PushFromMainThread(*scroll_offset);
1194 if (IsActive()) {
1195 changed |= scroll_offset_->PushPendingToActive();
1198 if (changed)
1199 DidUpdateScrollOffset(false);
1202 void LayerImpl::DidUpdateScrollOffset(bool is_from_root_delegate) {
1203 if (!is_from_root_delegate)
1204 layer_tree_impl()->DidUpdateScrollOffset(id());
1205 NoteLayerPropertyChangedForSubtree();
1206 ScrollbarParametersDidChange(false);
1209 void LayerImpl::SetDoubleSided(bool double_sided) {
1210 if (double_sided_ == double_sided)
1211 return;
1213 double_sided_ = double_sided;
1214 NoteLayerPropertyChangedForSubtree();
1217 SimpleEnclosedRegion LayerImpl::VisibleContentOpaqueRegion() const {
1218 if (contents_opaque())
1219 return SimpleEnclosedRegion(visible_content_rect());
1220 return SimpleEnclosedRegion();
1223 void LayerImpl::DidBeginTracing() {}
1225 void LayerImpl::ReleaseResources() {}
1227 void LayerImpl::RecreateResources() {
1230 gfx::ScrollOffset LayerImpl::MaxScrollOffset() const {
1231 if (!scroll_clip_layer_ || bounds().IsEmpty())
1232 return gfx::ScrollOffset();
1234 LayerImpl const* page_scale_layer = layer_tree_impl()->page_scale_layer();
1235 DCHECK(this != page_scale_layer);
1236 DCHECK(this != layer_tree_impl()->InnerViewportScrollLayer() ||
1237 IsContainerForFixedPositionLayers());
1239 float scale_factor = 1.f;
1240 for (LayerImpl const* current_layer = this;
1241 current_layer != scroll_clip_layer_->parent();
1242 current_layer = current_layer->parent()) {
1243 if (current_layer == page_scale_layer)
1244 scale_factor = layer_tree_impl()->current_page_scale_factor();
1247 gfx::SizeF scaled_scroll_bounds =
1248 gfx::ToFlooredSize(gfx::ScaleSize(BoundsForScrolling(), scale_factor));
1249 scaled_scroll_bounds = gfx::ToFlooredSize(scaled_scroll_bounds);
1251 gfx::ScrollOffset max_offset(
1252 scaled_scroll_bounds.width() - scroll_clip_layer_->bounds().width(),
1253 scaled_scroll_bounds.height() - scroll_clip_layer_->bounds().height());
1254 // We need the final scroll offset to be in CSS coords.
1255 max_offset.Scale(1 / scale_factor);
1256 max_offset.SetToMax(gfx::ScrollOffset());
1257 return max_offset;
1260 gfx::ScrollOffset LayerImpl::ClampScrollOffsetToLimits(
1261 gfx::ScrollOffset offset) const {
1262 offset.SetToMin(MaxScrollOffset());
1263 offset.SetToMax(gfx::ScrollOffset());
1264 return offset;
1267 gfx::Vector2dF LayerImpl::ClampScrollToMaxScrollOffset() {
1268 gfx::ScrollOffset old_offset = CurrentScrollOffset();
1269 gfx::ScrollOffset clamped_offset = ClampScrollOffsetToLimits(old_offset);
1270 gfx::Vector2dF delta = clamped_offset.DeltaFrom(old_offset);
1271 if (!delta.IsZero())
1272 ScrollBy(delta);
1273 return delta;
1276 void LayerImpl::SetScrollbarPosition(ScrollbarLayerImplBase* scrollbar_layer,
1277 LayerImpl* scrollbar_clip_layer,
1278 bool on_resize) const {
1279 DCHECK(scrollbar_layer);
1280 LayerImpl* page_scale_layer = layer_tree_impl()->page_scale_layer();
1282 DCHECK(this != page_scale_layer);
1283 DCHECK(scrollbar_clip_layer);
1284 gfx::RectF clip_rect(gfx::PointF(),
1285 scrollbar_clip_layer->BoundsForScrolling());
1287 // See comment in MaxScrollOffset() regarding the use of the content layer
1288 // bounds here.
1289 gfx::RectF scroll_rect(gfx::PointF(), BoundsForScrolling());
1291 if (scroll_rect.size().IsEmpty())
1292 return;
1294 gfx::ScrollOffset current_offset;
1295 for (LayerImpl const* current_layer = this;
1296 current_layer != scrollbar_clip_layer->parent();
1297 current_layer = current_layer->parent()) {
1298 current_offset += current_layer->CurrentScrollOffset();
1299 if (current_layer == page_scale_layer) {
1300 float scale_factor = layer_tree_impl()->current_page_scale_factor();
1301 current_offset.Scale(scale_factor);
1302 scroll_rect.Scale(scale_factor);
1306 bool scrollbar_needs_animation = false;
1307 scrollbar_needs_animation |= scrollbar_layer->SetVerticalAdjust(
1308 scrollbar_clip_layer->bounds_delta().y());
1309 if (scrollbar_layer->orientation() == HORIZONTAL) {
1310 float visible_ratio = clip_rect.width() / scroll_rect.width();
1311 scrollbar_needs_animation |=
1312 scrollbar_layer->SetCurrentPos(current_offset.x());
1313 scrollbar_needs_animation |=
1314 scrollbar_layer->SetMaximum(scroll_rect.width() - clip_rect.width());
1315 scrollbar_needs_animation |=
1316 scrollbar_layer->SetVisibleToTotalLengthRatio(visible_ratio);
1317 } else {
1318 float visible_ratio = clip_rect.height() / scroll_rect.height();
1319 bool y_offset_did_change =
1320 scrollbar_layer->SetCurrentPos(current_offset.y());
1321 scrollbar_needs_animation |= y_offset_did_change;
1322 scrollbar_needs_animation |=
1323 scrollbar_layer->SetMaximum(scroll_rect.height() - clip_rect.height());
1324 scrollbar_needs_animation |=
1325 scrollbar_layer->SetVisibleToTotalLengthRatio(visible_ratio);
1326 if (y_offset_did_change && layer_tree_impl()->IsActiveTree() &&
1327 this == layer_tree_impl()->InnerViewportScrollLayer()) {
1328 TRACE_COUNTER_ID1("cc", "scroll_offset_y", this->id(),
1329 current_offset.y());
1332 if (scrollbar_needs_animation) {
1333 layer_tree_impl()->set_needs_update_draw_properties();
1334 // TODO(wjmaclean) The scrollbar animator for the pinch-zoom scrollbars
1335 // should activate for every scroll on the main frame, not just the
1336 // scrolls that move the pinch virtual viewport (i.e. trigger from
1337 // either inner or outer viewport).
1338 if (scrollbar_animation_controller_) {
1339 // Non-overlay scrollbars shouldn't trigger animations.
1340 if (scrollbar_layer->is_overlay_scrollbar())
1341 scrollbar_animation_controller_->DidScrollUpdate(on_resize);
1346 void LayerImpl::DidBecomeActive() {
1347 if (layer_tree_impl_->settings().scrollbar_animator ==
1348 LayerTreeSettings::NO_ANIMATOR) {
1349 return;
1352 bool need_scrollbar_animation_controller = scrollable() && scrollbars_;
1353 if (!need_scrollbar_animation_controller) {
1354 scrollbar_animation_controller_ = nullptr;
1355 return;
1358 if (scrollbar_animation_controller_)
1359 return;
1361 scrollbar_animation_controller_ =
1362 layer_tree_impl_->CreateScrollbarAnimationController(this);
1365 void LayerImpl::ClearScrollbars() {
1366 if (!scrollbars_)
1367 return;
1369 scrollbars_.reset(nullptr);
1372 void LayerImpl::AddScrollbar(ScrollbarLayerImplBase* layer) {
1373 DCHECK(layer);
1374 DCHECK(!scrollbars_ || scrollbars_->find(layer) == scrollbars_->end());
1375 if (!scrollbars_)
1376 scrollbars_.reset(new ScrollbarSet());
1378 scrollbars_->insert(layer);
1381 void LayerImpl::RemoveScrollbar(ScrollbarLayerImplBase* layer) {
1382 DCHECK(scrollbars_);
1383 DCHECK(layer);
1384 DCHECK(scrollbars_->find(layer) != scrollbars_->end());
1386 scrollbars_->erase(layer);
1387 if (scrollbars_->empty())
1388 scrollbars_ = nullptr;
1391 bool LayerImpl::HasScrollbar(ScrollbarOrientation orientation) const {
1392 if (!scrollbars_)
1393 return false;
1395 for (ScrollbarSet::iterator it = scrollbars_->begin();
1396 it != scrollbars_->end();
1397 ++it)
1398 if ((*it)->orientation() == orientation)
1399 return true;
1401 return false;
1404 void LayerImpl::ScrollbarParametersDidChange(bool on_resize) {
1405 if (!scrollbars_)
1406 return;
1408 for (ScrollbarSet::iterator it = scrollbars_->begin();
1409 it != scrollbars_->end();
1410 ++it) {
1411 bool is_scroll_layer = (*it)->ScrollLayerId() == layer_id_;
1412 bool scroll_layer_resized = is_scroll_layer && on_resize;
1413 (*it)->ScrollbarParametersDidChange(scroll_layer_resized);
1417 void LayerImpl::SetNeedsPushProperties() {
1418 if (needs_push_properties_)
1419 return;
1420 if (!parent_should_know_need_push_properties() && parent_)
1421 parent_->AddDependentNeedsPushProperties();
1422 needs_push_properties_ = true;
1425 void LayerImpl::AddDependentNeedsPushProperties() {
1426 DCHECK_GE(num_dependents_need_push_properties_, 0);
1428 if (!parent_should_know_need_push_properties() && parent_)
1429 parent_->AddDependentNeedsPushProperties();
1431 num_dependents_need_push_properties_++;
1434 void LayerImpl::RemoveDependentNeedsPushProperties() {
1435 num_dependents_need_push_properties_--;
1436 DCHECK_GE(num_dependents_need_push_properties_, 0);
1438 if (!parent_should_know_need_push_properties() && parent_)
1439 parent_->RemoveDependentNeedsPushProperties();
1442 void LayerImpl::GetAllTilesAndPrioritiesForTracing(
1443 std::map<const Tile*, TilePriority>* tile_map) const {
1446 void LayerImpl::AsValueInto(base::trace_event::TracedValue* state) const {
1447 TracedValue::MakeDictIntoImplicitSnapshotWithCategory(
1448 TRACE_DISABLED_BY_DEFAULT("cc.debug"),
1449 state,
1450 "cc::LayerImpl",
1451 LayerTypeAsString(),
1452 this);
1453 state->SetInteger("layer_id", id());
1454 MathUtil::AddToTracedValue("bounds", bounds_, state);
1456 state->SetDouble("opacity", opacity());
1458 MathUtil::AddToTracedValue("position", position_, state);
1460 state->SetInteger("draws_content", DrawsContent());
1461 state->SetInteger("gpu_memory_usage", GPUMemoryUsageInBytes());
1463 MathUtil::AddToTracedValue(
1464 "scroll_offset", scroll_offset_ ? scroll_offset_->Current(IsActive())
1465 : gfx::ScrollOffset(),
1466 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();
1500 if (scroll_blocks_on_) {
1501 state->SetInteger("scroll_blocks_on", scroll_blocks_on_);
1504 state->BeginArray("children");
1505 for (size_t i = 0; i < children_.size(); ++i) {
1506 state->BeginDictionary();
1507 children_[i]->AsValueInto(state);
1508 state->EndDictionary();
1510 state->EndArray();
1511 if (mask_layer_) {
1512 state->BeginDictionary("mask_layer");
1513 mask_layer_->AsValueInto(state);
1514 state->EndDictionary();
1516 if (replica_layer_) {
1517 state->BeginDictionary("replica_layer");
1518 replica_layer_->AsValueInto(state);
1519 state->EndDictionary();
1522 if (scroll_parent_)
1523 state->SetInteger("scroll_parent", scroll_parent_->id());
1525 if (clip_parent_)
1526 state->SetInteger("clip_parent", clip_parent_->id());
1528 state->SetBoolean("can_use_lcd_text", can_use_lcd_text());
1529 state->SetBoolean("contents_opaque", contents_opaque());
1531 state->SetBoolean(
1532 "has_animation_bounds",
1533 layer_animation_controller()->HasAnimationThatInflatesBounds());
1535 gfx::BoxF box;
1536 if (LayerUtils::GetAnimationBounds(*this, &box))
1537 MathUtil::AddToTracedValue("animation_bounds", box, state);
1539 if (debug_info_.get()) {
1540 std::string str;
1541 debug_info_->AppendAsTraceFormat(&str);
1542 base::JSONReader json_reader;
1543 scoped_ptr<base::Value> debug_info_value(json_reader.ReadToValue(str));
1545 if (debug_info_value->IsType(base::Value::TYPE_DICTIONARY)) {
1546 base::DictionaryValue* dictionary_value = nullptr;
1547 bool converted_to_dictionary =
1548 debug_info_value->GetAsDictionary(&dictionary_value);
1549 DCHECK(converted_to_dictionary);
1550 for (base::DictionaryValue::Iterator it(*dictionary_value); !it.IsAtEnd();
1551 it.Advance()) {
1552 state->SetValue(it.key().data(), it.value().DeepCopy());
1554 } else {
1555 NOTREACHED();
1559 if (!frame_timing_requests_.empty()) {
1560 state->BeginArray("frame_timing_requests");
1561 for (const auto& request : frame_timing_requests_) {
1562 state->BeginDictionary();
1563 state->SetInteger("request_id", request.id());
1564 MathUtil::AddToTracedValue("request_rect", request.rect(), state);
1565 state->EndDictionary();
1567 state->EndArray();
1571 bool LayerImpl::IsDrawnRenderSurfaceLayerListMember() const {
1572 return draw_properties_.last_drawn_render_surface_layer_list_id ==
1573 layer_tree_impl_->current_render_surface_list_id();
1576 size_t LayerImpl::GPUMemoryUsageInBytes() const { return 0; }
1578 void LayerImpl::RunMicroBenchmark(MicroBenchmarkImpl* benchmark) {
1579 benchmark->RunOnLayer(this);
1582 int LayerImpl::NumDescendantsThatDrawContent() const {
1583 return num_descendants_that_draw_content_;
1586 void LayerImpl::NotifyAnimationFinished(
1587 base::TimeTicks monotonic_time,
1588 Animation::TargetProperty target_property,
1589 int group) {
1590 if (target_property == Animation::SCROLL_OFFSET)
1591 layer_tree_impl_->InputScrollAnimationFinished();
1594 void LayerImpl::SetHasRenderSurface(bool should_have_render_surface) {
1595 if (!!render_surface() == should_have_render_surface)
1596 return;
1598 SetNeedsPushProperties();
1599 layer_tree_impl()->set_needs_update_draw_properties();
1600 if (should_have_render_surface) {
1601 render_surface_ = make_scoped_ptr(new RenderSurfaceImpl(this));
1602 return;
1604 render_surface_.reset();
1607 Region LayerImpl::GetInvalidationRegion() {
1608 return Region(update_rect_);
1611 gfx::Rect LayerImpl::GetEnclosingRectInTargetSpace() const {
1612 return MathUtil::MapEnclosingClippedRect(
1613 draw_properties_.target_space_transform,
1614 gfx::Rect(draw_properties_.content_bounds));
1617 gfx::Rect LayerImpl::GetScaledEnclosingRectInTargetSpace(float scale) const {
1618 gfx::Transform scaled_draw_transform =
1619 draw_properties_.target_space_transform;
1620 scaled_draw_transform.Scale(SK_MScalar1 / scale, SK_MScalar1 / scale);
1621 gfx::Size scaled_content_bounds =
1622 gfx::ToCeiledSize(gfx::ScaleSize(content_bounds(), scale));
1623 return MathUtil::MapEnclosingClippedRect(scaled_draw_transform,
1624 gfx::Rect(scaled_content_bounds));
1627 } // namespace cc