ServiceWorker: Introduce SWMessageSender in order to mock IPC messaging
[chromium-blink-merge.git] / cc / layers / layer_impl.cc
blob0353a787a3bbcb5ad75f93bcfb800cf23efad9ca
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/input/layer_scroll_offset_delegate.h"
20 #include "cc/layers/layer_utils.h"
21 #include "cc/layers/painted_scrollbar_layer_impl.h"
22 #include "cc/output/copy_output_request.h"
23 #include "cc/quads/debug_border_draw_quad.h"
24 #include "cc/quads/render_pass.h"
25 #include "cc/trees/layer_tree_host_common.h"
26 #include "cc/trees/layer_tree_impl.h"
27 #include "cc/trees/layer_tree_settings.h"
28 #include "cc/trees/proxy.h"
29 #include "ui/gfx/geometry/box_f.h"
30 #include "ui/gfx/geometry/point_conversions.h"
31 #include "ui/gfx/geometry/quad_f.h"
32 #include "ui/gfx/geometry/rect_conversions.h"
33 #include "ui/gfx/geometry/size_conversions.h"
34 #include "ui/gfx/geometry/vector2d_conversions.h"
36 namespace cc {
37 LayerImpl::LayerImpl(LayerTreeImpl* 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_offset_delegate_(nullptr),
53 scroll_clip_layer_(nullptr),
54 should_scroll_on_main_thread_(false),
55 have_wheel_event_handlers_(false),
56 have_scroll_event_handlers_(false),
57 scroll_blocks_on_(SCROLL_BLOCKS_ON_NONE),
58 user_scrollable_horizontal_(true),
59 user_scrollable_vertical_(true),
60 stacking_order_changed_(false),
61 double_sided_(true),
62 should_flatten_transform_(true),
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 draw_depth_(0.f),
78 needs_push_properties_(false),
79 num_dependents_need_push_properties_(0),
80 sorting_context_id_(0),
81 current_draw_mode_(DRAW_MODE_NONE),
82 frame_timing_requests_dirty_(false) {
83 DCHECK_GT(layer_id_, 0);
84 DCHECK(layer_tree_impl_);
85 layer_tree_impl_->RegisterLayer(this);
86 AnimationRegistrar* registrar = layer_tree_impl_->GetAnimationRegistrar();
87 layer_animation_controller_ =
88 registrar->GetAnimationControllerForId(layer_id_);
89 layer_animation_controller_->AddValueObserver(this);
90 if (IsActive()) {
91 layer_animation_controller_->set_value_provider(this);
92 layer_animation_controller_->set_layer_animation_delegate(this);
94 SetNeedsPushProperties();
97 LayerImpl::~LayerImpl() {
98 DCHECK_EQ(DRAW_MODE_NONE, current_draw_mode_);
100 layer_animation_controller_->RemoveValueObserver(this);
101 layer_animation_controller_->remove_value_provider(this);
102 layer_animation_controller_->remove_layer_animation_delegate(this);
104 if (!copy_requests_.empty() && layer_tree_impl_->IsActiveTree())
105 layer_tree_impl()->RemoveLayerWithCopyOutputRequest(this);
106 layer_tree_impl_->UnregisterLayer(this);
108 TRACE_EVENT_OBJECT_DELETED_WITH_ID(
109 TRACE_DISABLED_BY_DEFAULT("cc.debug"), "cc::LayerImpl", this);
112 void LayerImpl::AddChild(scoped_ptr<LayerImpl> child) {
113 child->SetParent(this);
114 DCHECK_EQ(layer_tree_impl(), child->layer_tree_impl());
115 children_.push_back(child.Pass());
116 layer_tree_impl()->set_needs_update_draw_properties();
119 scoped_ptr<LayerImpl> LayerImpl::RemoveChild(LayerImpl* child) {
120 for (OwnedLayerImplList::iterator it = children_.begin();
121 it != children_.end();
122 ++it) {
123 if (*it == child) {
124 scoped_ptr<LayerImpl> ret = children_.take(it);
125 children_.erase(it);
126 layer_tree_impl()->set_needs_update_draw_properties();
127 return ret.Pass();
130 return nullptr;
133 void LayerImpl::SetParent(LayerImpl* parent) {
134 if (parent_should_know_need_push_properties()) {
135 if (parent_)
136 parent_->RemoveDependentNeedsPushProperties();
137 if (parent)
138 parent->AddDependentNeedsPushProperties();
140 parent_ = parent;
143 void LayerImpl::ClearChildList() {
144 if (children_.empty())
145 return;
147 children_.clear();
148 layer_tree_impl()->set_needs_update_draw_properties();
151 bool LayerImpl::HasAncestor(const LayerImpl* ancestor) const {
152 if (!ancestor)
153 return false;
155 for (const LayerImpl* layer = this; layer; layer = layer->parent()) {
156 if (layer == ancestor)
157 return true;
160 return false;
163 void LayerImpl::SetScrollParent(LayerImpl* parent) {
164 if (scroll_parent_ == parent)
165 return;
167 // Having both a scroll parent and a scroll offset delegate is unsupported.
168 DCHECK(!scroll_offset_delegate_);
170 if (parent)
171 DCHECK_EQ(layer_tree_impl()->LayerById(parent->id()), parent);
173 scroll_parent_ = parent;
174 SetNeedsPushProperties();
177 void LayerImpl::SetDebugInfo(
178 scoped_refptr<base::trace_event::ConvertableToTraceFormat> other) {
179 debug_info_ = other;
180 SetNeedsPushProperties();
183 void LayerImpl::SetScrollChildren(std::set<LayerImpl*>* children) {
184 if (scroll_children_.get() == children)
185 return;
186 scroll_children_.reset(children);
187 SetNeedsPushProperties();
190 void LayerImpl::SetNumDescendantsThatDrawContent(int num_descendants) {
191 if (num_descendants_that_draw_content_ == num_descendants)
192 return;
193 num_descendants_that_draw_content_ = num_descendants;
194 SetNeedsPushProperties();
197 void LayerImpl::SetClipParent(LayerImpl* ancestor) {
198 if (clip_parent_ == ancestor)
199 return;
201 clip_parent_ = ancestor;
202 SetNeedsPushProperties();
205 void LayerImpl::SetClipChildren(std::set<LayerImpl*>* children) {
206 if (clip_children_.get() == children)
207 return;
208 clip_children_.reset(children);
209 SetNeedsPushProperties();
212 void LayerImpl::PassCopyRequests(ScopedPtrVector<CopyOutputRequest>* requests) {
213 if (requests->empty())
214 return;
215 DCHECK(render_surface());
216 bool was_empty = copy_requests_.empty();
217 copy_requests_.insert_and_take(copy_requests_.end(), requests);
218 requests->clear();
220 if (was_empty && layer_tree_impl()->IsActiveTree())
221 layer_tree_impl()->AddLayerWithCopyOutputRequest(this);
222 NoteLayerPropertyChangedForSubtree();
225 void LayerImpl::TakeCopyRequestsAndTransformToTarget(
226 ScopedPtrVector<CopyOutputRequest>* requests) {
227 DCHECK(!copy_requests_.empty());
228 DCHECK(layer_tree_impl()->IsActiveTree());
229 DCHECK_EQ(render_target(), this);
231 size_t first_inserted_request = requests->size();
232 requests->insert_and_take(requests->end(), &copy_requests_);
233 copy_requests_.clear();
235 for (size_t i = first_inserted_request; i < requests->size(); ++i) {
236 CopyOutputRequest* request = requests->at(i);
237 if (!request->has_area())
238 continue;
240 gfx::Rect request_in_layer_space = request->area();
241 gfx::Rect request_in_content_space =
242 LayerRectToContentRect(request_in_layer_space);
243 request->set_area(MathUtil::MapEnclosingClippedRect(
244 draw_properties_.target_space_transform, request_in_content_space));
247 layer_tree_impl()->RemoveLayerWithCopyOutputRequest(this);
248 layer_tree_impl()->set_needs_update_draw_properties();
251 void LayerImpl::ClearRenderSurfaceLayerList() {
252 if (render_surface_)
253 render_surface_->ClearLayerLists();
256 void LayerImpl::PopulateSharedQuadState(SharedQuadState* state) const {
257 state->SetAll(
258 draw_properties_.target_space_transform, draw_properties_.content_bounds,
259 draw_properties_.visible_content_rect, draw_properties_.clip_rect,
260 draw_properties_.is_clipped, draw_properties_.opacity,
261 draw_properties_.blend_mode, sorting_context_id_);
264 void LayerImpl::PopulateScaledSharedQuadState(SharedQuadState* state,
265 float scale) const {
266 gfx::Transform scaled_draw_transform =
267 draw_properties_.target_space_transform;
268 scaled_draw_transform.Scale(SK_MScalar1 / scale, SK_MScalar1 / scale);
269 gfx::Size scaled_content_bounds =
270 gfx::ToCeiledSize(gfx::ScaleSize(bounds(), scale));
271 gfx::Rect scaled_visible_content_rect =
272 gfx::ScaleToEnclosingRect(visible_content_rect(), scale);
273 scaled_visible_content_rect.Intersect(gfx::Rect(scaled_content_bounds));
275 state->SetAll(scaled_draw_transform, scaled_content_bounds,
276 scaled_visible_content_rect, draw_properties().clip_rect,
277 draw_properties().is_clipped, draw_properties().opacity,
278 draw_properties().blend_mode, sorting_context_id_);
281 bool LayerImpl::WillDraw(DrawMode draw_mode,
282 ResourceProvider* resource_provider) {
283 // WillDraw/DidDraw must be matched.
284 DCHECK_NE(DRAW_MODE_NONE, draw_mode);
285 DCHECK_EQ(DRAW_MODE_NONE, current_draw_mode_);
286 current_draw_mode_ = draw_mode;
287 return true;
290 void LayerImpl::DidDraw(ResourceProvider* resource_provider) {
291 DCHECK_NE(DRAW_MODE_NONE, current_draw_mode_);
292 current_draw_mode_ = DRAW_MODE_NONE;
295 bool LayerImpl::ShowDebugBorders() const {
296 return layer_tree_impl()->debug_state().show_debug_borders;
299 void LayerImpl::GetDebugBorderProperties(SkColor* color, float* width) const {
300 if (draws_content_) {
301 *color = DebugColors::ContentLayerBorderColor();
302 *width = DebugColors::ContentLayerBorderWidth(layer_tree_impl());
303 return;
306 if (masks_to_bounds_) {
307 *color = DebugColors::MaskingLayerBorderColor();
308 *width = DebugColors::MaskingLayerBorderWidth(layer_tree_impl());
309 return;
312 *color = DebugColors::ContainerLayerBorderColor();
313 *width = DebugColors::ContainerLayerBorderWidth(layer_tree_impl());
316 void LayerImpl::AppendDebugBorderQuad(
317 RenderPass* render_pass,
318 const gfx::Size& content_bounds,
319 const SharedQuadState* shared_quad_state,
320 AppendQuadsData* append_quads_data) const {
321 SkColor color;
322 float width;
323 GetDebugBorderProperties(&color, &width);
324 AppendDebugBorderQuad(render_pass,
325 content_bounds,
326 shared_quad_state,
327 append_quads_data,
328 color,
329 width);
332 void LayerImpl::AppendDebugBorderQuad(RenderPass* render_pass,
333 const gfx::Size& content_bounds,
334 const SharedQuadState* shared_quad_state,
335 AppendQuadsData* append_quads_data,
336 SkColor color,
337 float width) const {
338 if (!ShowDebugBorders())
339 return;
341 gfx::Rect quad_rect(content_bounds);
342 gfx::Rect visible_quad_rect(quad_rect);
343 DebugBorderDrawQuad* debug_border_quad =
344 render_pass->CreateAndAppendDrawQuad<DebugBorderDrawQuad>();
345 debug_border_quad->SetNew(
346 shared_quad_state, quad_rect, visible_quad_rect, color, width);
347 if (contents_opaque()) {
348 // When opaque, draw a second inner border that is thicker than the outer
349 // border, but more transparent.
350 static const float kFillOpacity = 0.3f;
351 SkColor fill_color = SkColorSetA(
352 color, static_cast<uint8_t>(SkColorGetA(color) * kFillOpacity));
353 float fill_width = width * 3;
354 gfx::Rect fill_rect = quad_rect;
355 fill_rect.Inset(fill_width / 2.f, fill_width / 2.f);
356 gfx::Rect visible_fill_rect =
357 gfx::IntersectRects(visible_quad_rect, fill_rect);
358 DebugBorderDrawQuad* fill_quad =
359 render_pass->CreateAndAppendDrawQuad<DebugBorderDrawQuad>();
360 fill_quad->SetNew(shared_quad_state, fill_rect, visible_fill_rect,
361 fill_color, fill_width);
365 bool LayerImpl::HasDelegatedContent() const {
366 return false;
369 bool LayerImpl::HasContributingDelegatedRenderPasses() const {
370 return false;
373 RenderPassId LayerImpl::FirstContributingRenderPassId() const {
374 return RenderPassId(0, 0);
377 RenderPassId LayerImpl::NextContributingRenderPassId(RenderPassId id) const {
378 return RenderPassId(0, 0);
381 void LayerImpl::GetContentsResourceId(ResourceProvider::ResourceId* resource_id,
382 gfx::Size* resource_size) const {
383 NOTREACHED();
384 *resource_id = 0;
387 gfx::Vector2dF LayerImpl::ScrollBy(const gfx::Vector2dF& scroll) {
388 RefreshFromScrollDelegate();
390 gfx::ScrollOffset adjusted_scroll(scroll);
391 if (layer_tree_impl()->settings().use_pinch_virtual_viewport) {
392 if (!user_scrollable_horizontal_)
393 adjusted_scroll.set_x(0);
394 if (!user_scrollable_vertical_)
395 adjusted_scroll.set_y(0);
397 DCHECK(scrollable());
398 gfx::ScrollOffset old_offset = CurrentScrollOffset();
399 gfx::ScrollOffset new_offset =
400 ClampScrollOffsetToLimits(old_offset + adjusted_scroll);
401 SetCurrentScrollOffset(new_offset);
403 gfx::ScrollOffset unscrolled =
404 old_offset + gfx::ScrollOffset(scroll) - new_offset;
405 return gfx::Vector2dF(unscrolled.x(), unscrolled.y());
408 void LayerImpl::SetScrollClipLayer(int scroll_clip_layer_id) {
409 scroll_clip_layer_ = layer_tree_impl()->LayerById(scroll_clip_layer_id);
412 bool LayerImpl::user_scrollable(ScrollbarOrientation orientation) const {
413 return (orientation == HORIZONTAL) ? user_scrollable_horizontal_
414 : user_scrollable_vertical_;
417 void LayerImpl::ApplySentScrollDeltasFromAbortedCommit() {
418 DCHECK(layer_tree_impl()->IsActiveTree());
419 scroll_offset_->AbortCommit();
422 InputHandler::ScrollStatus LayerImpl::TryScroll(
423 const gfx::PointF& screen_space_point,
424 InputHandler::ScrollInputType type,
425 ScrollBlocksOn effective_block_mode) const {
426 if (should_scroll_on_main_thread()) {
427 TRACE_EVENT0("cc", "LayerImpl::TryScroll: Failed ShouldScrollOnMainThread");
428 return InputHandler::SCROLL_ON_MAIN_THREAD;
431 if (!screen_space_transform().IsInvertible()) {
432 TRACE_EVENT0("cc", "LayerImpl::TryScroll: Ignored NonInvertibleTransform");
433 return InputHandler::SCROLL_IGNORED;
436 if (!non_fast_scrollable_region().IsEmpty()) {
437 bool clipped = false;
438 gfx::Transform inverse_screen_space_transform(
439 gfx::Transform::kSkipInitialization);
440 if (!screen_space_transform().GetInverse(&inverse_screen_space_transform)) {
441 // TODO(shawnsingh): We shouldn't be applying a projection if screen space
442 // transform is uninvertible here. Perhaps we should be returning
443 // SCROLL_ON_MAIN_THREAD in this case?
446 gfx::PointF hit_test_point_in_content_space =
447 MathUtil::ProjectPoint(inverse_screen_space_transform,
448 screen_space_point,
449 &clipped);
450 gfx::PointF hit_test_point_in_layer_space =
451 gfx::ScalePoint(hit_test_point_in_content_space,
452 1.f / contents_scale_x(),
453 1.f / contents_scale_y());
454 if (!clipped &&
455 non_fast_scrollable_region().Contains(
456 gfx::ToRoundedPoint(hit_test_point_in_layer_space))) {
457 TRACE_EVENT0("cc",
458 "LayerImpl::tryScroll: Failed NonFastScrollableRegion");
459 return InputHandler::SCROLL_ON_MAIN_THREAD;
463 if (have_scroll_event_handlers() &&
464 effective_block_mode & SCROLL_BLOCKS_ON_SCROLL_EVENT) {
465 TRACE_EVENT0("cc", "LayerImpl::tryScroll: Failed ScrollEventHandlers");
466 return InputHandler::SCROLL_ON_MAIN_THREAD;
469 if (type == InputHandler::WHEEL && have_wheel_event_handlers() &&
470 effective_block_mode & SCROLL_BLOCKS_ON_WHEEL_EVENT) {
471 TRACE_EVENT0("cc", "LayerImpl::tryScroll: Failed WheelEventHandlers");
472 return InputHandler::SCROLL_ON_MAIN_THREAD;
475 if (!scrollable()) {
476 TRACE_EVENT0("cc", "LayerImpl::tryScroll: Ignored not scrollable");
477 return InputHandler::SCROLL_IGNORED;
480 gfx::ScrollOffset max_scroll_offset = MaxScrollOffset();
481 if (max_scroll_offset.x() <= 0 && max_scroll_offset.y() <= 0) {
482 TRACE_EVENT0("cc",
483 "LayerImpl::tryScroll: Ignored. Technically scrollable,"
484 " but has no affordance in either direction.");
485 return InputHandler::SCROLL_IGNORED;
488 return InputHandler::SCROLL_STARTED;
491 gfx::Rect LayerImpl::LayerRectToContentRect(
492 const gfx::RectF& layer_rect) const {
493 gfx::RectF content_rect =
494 gfx::ScaleRect(layer_rect, contents_scale_x(), contents_scale_y());
495 // Intersect with content rect to avoid the extra pixel because for some
496 // values x and y, ceil((x / y) * y) may be x + 1.
497 content_rect.Intersect(gfx::Rect(content_bounds()));
498 return gfx::ToEnclosingRect(content_rect);
501 skia::RefPtr<SkPicture> LayerImpl::GetPicture() {
502 return skia::RefPtr<SkPicture>();
505 scoped_ptr<LayerImpl> LayerImpl::CreateLayerImpl(LayerTreeImpl* tree_impl) {
506 return LayerImpl::Create(tree_impl, layer_id_, scroll_offset_);
509 void LayerImpl::PushPropertiesTo(LayerImpl* layer) {
510 layer->SetTransformOrigin(transform_origin_);
511 layer->SetBackgroundColor(background_color_);
512 layer->SetBounds(bounds_);
513 layer->SetContentBounds(content_bounds());
514 layer->SetContentsScale(contents_scale_x(), contents_scale_y());
515 layer->SetDoubleSided(double_sided_);
516 layer->SetDrawCheckerboardForMissingTiles(
517 draw_checkerboard_for_missing_tiles_);
518 layer->SetDrawsContent(DrawsContent());
519 layer->SetHideLayerAndSubtree(hide_layer_and_subtree_);
520 layer->SetHasRenderSurface(!!render_surface() || layer->HasCopyRequest());
521 layer->SetFilters(filters());
522 layer->SetBackgroundFilters(background_filters());
523 layer->SetMasksToBounds(masks_to_bounds_);
524 layer->SetShouldScrollOnMainThread(should_scroll_on_main_thread_);
525 layer->SetHaveWheelEventHandlers(have_wheel_event_handlers_);
526 layer->SetHaveScrollEventHandlers(have_scroll_event_handlers_);
527 layer->SetScrollBlocksOn(scroll_blocks_on_);
528 layer->SetNonFastScrollableRegion(non_fast_scrollable_region_);
529 layer->SetTouchEventHandlerRegion(touch_event_handler_region_);
530 layer->SetContentsOpaque(contents_opaque_);
531 layer->SetOpacity(opacity_);
532 layer->SetBlendMode(blend_mode_);
533 layer->SetIsRootForIsolatedGroup(is_root_for_isolated_group_);
534 layer->SetPosition(position_);
535 layer->SetIsContainerForFixedPositionLayers(
536 is_container_for_fixed_position_layers_);
537 layer->SetPositionConstraint(position_constraint_);
538 layer->SetShouldFlattenTransform(should_flatten_transform_);
539 layer->SetUseParentBackfaceVisibility(use_parent_backface_visibility_);
540 layer->SetTransformAndInvertibility(transform_, transform_is_invertible_);
542 layer->SetScrollClipLayer(scroll_clip_layer_ ? scroll_clip_layer_->id()
543 : Layer::INVALID_ID);
544 layer->set_user_scrollable_horizontal(user_scrollable_horizontal_);
545 layer->set_user_scrollable_vertical(user_scrollable_vertical_);
547 layer->SetScrollCompensationAdjustment(scroll_compensation_adjustment_);
549 layer->PushScrollOffset(nullptr);
551 layer->Set3dSortingContextId(sorting_context_id_);
552 layer->SetNumDescendantsThatDrawContent(num_descendants_that_draw_content_);
554 LayerImpl* scroll_parent = nullptr;
555 if (scroll_parent_) {
556 scroll_parent = layer->layer_tree_impl()->LayerById(scroll_parent_->id());
557 DCHECK(scroll_parent);
560 layer->SetScrollParent(scroll_parent);
561 if (scroll_children_) {
562 std::set<LayerImpl*>* scroll_children = new std::set<LayerImpl*>;
563 for (std::set<LayerImpl*>::iterator it = scroll_children_->begin();
564 it != scroll_children_->end();
565 ++it) {
566 DCHECK_EQ((*it)->scroll_parent(), this);
567 LayerImpl* scroll_child =
568 layer->layer_tree_impl()->LayerById((*it)->id());
569 DCHECK(scroll_child);
570 scroll_children->insert(scroll_child);
572 layer->SetScrollChildren(scroll_children);
573 } else {
574 layer->SetScrollChildren(nullptr);
577 LayerImpl* clip_parent = nullptr;
578 if (clip_parent_) {
579 clip_parent = layer->layer_tree_impl()->LayerById(
580 clip_parent_->id());
581 DCHECK(clip_parent);
584 layer->SetClipParent(clip_parent);
585 if (clip_children_) {
586 std::set<LayerImpl*>* clip_children = new std::set<LayerImpl*>;
587 for (std::set<LayerImpl*>::iterator it = clip_children_->begin();
588 it != clip_children_->end(); ++it)
589 clip_children->insert(layer->layer_tree_impl()->LayerById((*it)->id()));
590 layer->SetClipChildren(clip_children);
591 } else {
592 layer->SetClipChildren(nullptr);
595 layer->PassCopyRequests(&copy_requests_);
597 // If the main thread commits multiple times before the impl thread actually
598 // draws, then damage tracking will become incorrect if we simply clobber the
599 // update_rect here. The LayerImpl's update_rect needs to accumulate (i.e.
600 // union) any update changes that have occurred on the main thread.
601 update_rect_.Union(layer->update_rect());
602 layer->SetUpdateRect(update_rect_);
604 layer->SetStackingOrderChanged(stacking_order_changed_);
605 layer->SetDebugInfo(debug_info_);
607 if (frame_timing_requests_dirty_) {
608 layer->PassFrameTimingRequests(&frame_timing_requests_);
609 frame_timing_requests_dirty_ = false;
612 // Reset any state that should be cleared for the next update.
613 stacking_order_changed_ = false;
614 update_rect_ = gfx::Rect();
615 needs_push_properties_ = false;
616 num_dependents_need_push_properties_ = 0;
619 gfx::Vector2dF LayerImpl::FixedContainerSizeDelta() const {
620 if (!scroll_clip_layer_)
621 return gfx::Vector2dF();
623 gfx::Vector2dF delta_from_scroll = scroll_clip_layer_->bounds_delta();
625 // In virtual-viewport mode, we don't need to compensate for pinch zoom or
626 // scale since the fixed container is the outer viewport, which sits below
627 // the page scale.
628 if (layer_tree_impl()->settings().use_pinch_virtual_viewport)
629 return delta_from_scroll;
631 float scale_delta = layer_tree_impl()->page_scale_delta();
632 float scale = layer_tree_impl()->current_page_scale_factor() /
633 layer_tree_impl()->page_scale_delta();
635 delta_from_scroll.Scale(1.f / scale);
637 // The delta-from-pinch component requires some explanation: A viewport of
638 // size (w,h) will appear to be size (w/s,h/s) under scale s in the content
639 // space. If s -> s' on the impl thread, where s' = s * ds, then the apparent
640 // viewport size change in the content space due to ds is:
642 // (w/s',h/s') - (w/s,h/s) = (w,h)(1/s' - 1/s) = (w,h)(1 - ds)/(s ds)
644 gfx::Vector2dF delta_from_pinch =
645 gfx::Rect(scroll_clip_layer_->bounds()).bottom_right() - gfx::PointF();
646 delta_from_pinch.Scale((1.f - scale_delta) / (scale * scale_delta));
648 return delta_from_scroll + delta_from_pinch;
651 base::DictionaryValue* LayerImpl::LayerTreeAsJson() const {
652 base::DictionaryValue* result = new base::DictionaryValue;
653 result->SetString("LayerType", LayerTypeAsString());
655 base::ListValue* list = new base::ListValue;
656 list->AppendInteger(bounds().width());
657 list->AppendInteger(bounds().height());
658 result->Set("Bounds", list);
660 list = new base::ListValue;
661 list->AppendDouble(position_.x());
662 list->AppendDouble(position_.y());
663 result->Set("Position", list);
665 const gfx::Transform& gfx_transform = draw_properties_.target_space_transform;
666 double transform[16];
667 gfx_transform.matrix().asColMajord(transform);
668 list = new base::ListValue;
669 for (int i = 0; i < 16; ++i)
670 list->AppendDouble(transform[i]);
671 result->Set("DrawTransform", list);
673 result->SetBoolean("DrawsContent", draws_content_);
674 result->SetBoolean("Is3dSorted", Is3dSorted());
675 result->SetDouble("OPACITY", opacity());
676 result->SetBoolean("ContentsOpaque", contents_opaque_);
678 if (scrollable())
679 result->SetBoolean("Scrollable", true);
681 if (have_wheel_event_handlers_)
682 result->SetBoolean("WheelHandler", have_wheel_event_handlers_);
683 if (have_scroll_event_handlers_)
684 result->SetBoolean("ScrollHandler", have_scroll_event_handlers_);
685 if (!touch_event_handler_region_.IsEmpty()) {
686 scoped_ptr<base::Value> region = touch_event_handler_region_.AsValue();
687 result->Set("TouchRegion", region.release());
690 if (scroll_blocks_on_) {
691 list = new base::ListValue;
692 if (scroll_blocks_on_ & SCROLL_BLOCKS_ON_START_TOUCH)
693 list->AppendString("StartTouch");
694 if (scroll_blocks_on_ & SCROLL_BLOCKS_ON_WHEEL_EVENT)
695 list->AppendString("WheelEvent");
696 if (scroll_blocks_on_ & SCROLL_BLOCKS_ON_SCROLL_EVENT)
697 list->AppendString("ScrollEvent");
698 result->Set("ScrollBlocksOn", list);
701 list = new base::ListValue;
702 for (size_t i = 0; i < children_.size(); ++i)
703 list->Append(children_[i]->LayerTreeAsJson());
704 result->Set("Children", list);
706 return result;
709 void LayerImpl::SetStackingOrderChanged(bool stacking_order_changed) {
710 if (stacking_order_changed) {
711 stacking_order_changed_ = true;
712 NoteLayerPropertyChangedForSubtree();
716 void LayerImpl::NoteLayerPropertyChanged() {
717 layer_property_changed_ = true;
718 layer_tree_impl()->set_needs_update_draw_properties();
719 SetNeedsPushProperties();
722 void LayerImpl::NoteLayerPropertyChangedForSubtree() {
723 layer_property_changed_ = true;
724 layer_tree_impl()->set_needs_update_draw_properties();
725 for (size_t i = 0; i < children_.size(); ++i)
726 children_[i]->NoteLayerPropertyChangedForDescendantsInternal();
727 SetNeedsPushProperties();
730 void LayerImpl::NoteLayerPropertyChangedForDescendantsInternal() {
731 layer_property_changed_ = true;
732 for (size_t i = 0; i < children_.size(); ++i)
733 children_[i]->NoteLayerPropertyChangedForDescendantsInternal();
736 void LayerImpl::NoteLayerPropertyChangedForDescendants() {
737 layer_tree_impl()->set_needs_update_draw_properties();
738 for (size_t i = 0; i < children_.size(); ++i)
739 children_[i]->NoteLayerPropertyChangedForDescendantsInternal();
740 SetNeedsPushProperties();
743 const char* LayerImpl::LayerTypeAsString() const {
744 return "cc::LayerImpl";
747 void LayerImpl::ResetAllChangeTrackingForSubtree() {
748 layer_property_changed_ = false;
750 update_rect_ = gfx::Rect();
751 damage_rect_ = gfx::RectF();
753 if (render_surface_)
754 render_surface_->ResetPropertyChangedFlag();
756 if (mask_layer_)
757 mask_layer_->ResetAllChangeTrackingForSubtree();
759 if (replica_layer_) {
760 // This also resets the replica mask, if it exists.
761 replica_layer_->ResetAllChangeTrackingForSubtree();
764 for (size_t i = 0; i < children_.size(); ++i)
765 children_[i]->ResetAllChangeTrackingForSubtree();
767 needs_push_properties_ = false;
768 num_dependents_need_push_properties_ = 0;
771 gfx::ScrollOffset LayerImpl::ScrollOffsetForAnimation() const {
772 return CurrentScrollOffset();
775 void LayerImpl::OnFilterAnimated(const FilterOperations& filters) {
776 SetFilters(filters);
779 void LayerImpl::OnOpacityAnimated(float opacity) {
780 SetOpacity(opacity);
783 void LayerImpl::OnTransformAnimated(const gfx::Transform& transform) {
784 SetTransform(transform);
787 void LayerImpl::OnScrollOffsetAnimated(const gfx::ScrollOffset& scroll_offset) {
788 // Only layers in the active tree should need to do anything here, since
789 // layers in the pending tree will find out about these changes as a
790 // result of the shared SyncedProperty.
791 if (!IsActive())
792 return;
794 SetCurrentScrollOffset(scroll_offset);
796 layer_tree_impl_->DidAnimateScrollOffset();
799 void LayerImpl::OnAnimationWaitingForDeletion() {}
801 bool LayerImpl::IsActive() const {
802 return layer_tree_impl_->IsActiveTree();
805 gfx::Size LayerImpl::bounds() const {
806 gfx::Vector2d delta = gfx::ToCeiledVector2d(bounds_delta_);
807 return gfx::Size(bounds_.width() + delta.x(),
808 bounds_.height() + delta.y());
811 gfx::SizeF LayerImpl::BoundsForScrolling() const {
812 return gfx::SizeF(bounds_.width() + bounds_delta_.x(),
813 bounds_.height() + bounds_delta_.y());
816 void LayerImpl::SetBounds(const gfx::Size& bounds) {
817 if (bounds_ == bounds)
818 return;
820 bounds_ = bounds;
822 ScrollbarParametersDidChange(true);
823 if (masks_to_bounds())
824 NoteLayerPropertyChangedForSubtree();
825 else
826 NoteLayerPropertyChanged();
829 void LayerImpl::SetBoundsDelta(const gfx::Vector2dF& bounds_delta) {
830 if (bounds_delta_ == bounds_delta)
831 return;
833 bounds_delta_ = bounds_delta;
835 ScrollbarParametersDidChange(true);
836 if (masks_to_bounds())
837 NoteLayerPropertyChangedForSubtree();
838 else
839 NoteLayerPropertyChanged();
842 void LayerImpl::SetMaskLayer(scoped_ptr<LayerImpl> mask_layer) {
843 int new_layer_id = mask_layer ? mask_layer->id() : -1;
845 if (mask_layer) {
846 DCHECK_EQ(layer_tree_impl(), mask_layer->layer_tree_impl());
847 DCHECK_NE(new_layer_id, mask_layer_id_);
848 } else if (new_layer_id == mask_layer_id_) {
849 return;
852 mask_layer_ = mask_layer.Pass();
853 mask_layer_id_ = new_layer_id;
854 if (mask_layer_)
855 mask_layer_->SetParent(this);
856 NoteLayerPropertyChangedForSubtree();
859 scoped_ptr<LayerImpl> LayerImpl::TakeMaskLayer() {
860 mask_layer_id_ = -1;
861 return mask_layer_.Pass();
864 void LayerImpl::SetReplicaLayer(scoped_ptr<LayerImpl> replica_layer) {
865 int new_layer_id = replica_layer ? replica_layer->id() : -1;
867 if (replica_layer) {
868 DCHECK_EQ(layer_tree_impl(), replica_layer->layer_tree_impl());
869 DCHECK_NE(new_layer_id, replica_layer_id_);
870 } else if (new_layer_id == replica_layer_id_) {
871 return;
874 replica_layer_ = replica_layer.Pass();
875 replica_layer_id_ = new_layer_id;
876 if (replica_layer_)
877 replica_layer_->SetParent(this);
878 NoteLayerPropertyChangedForSubtree();
881 scoped_ptr<LayerImpl> LayerImpl::TakeReplicaLayer() {
882 replica_layer_id_ = -1;
883 return replica_layer_.Pass();
886 ScrollbarLayerImplBase* LayerImpl::ToScrollbarLayer() {
887 return nullptr;
890 void LayerImpl::SetDrawsContent(bool draws_content) {
891 if (draws_content_ == draws_content)
892 return;
894 draws_content_ = draws_content;
895 NoteLayerPropertyChanged();
898 void LayerImpl::SetHideLayerAndSubtree(bool hide) {
899 if (hide_layer_and_subtree_ == hide)
900 return;
902 hide_layer_and_subtree_ = hide;
903 NoteLayerPropertyChangedForSubtree();
906 void LayerImpl::SetTransformOrigin(const gfx::Point3F& transform_origin) {
907 if (transform_origin_ == transform_origin)
908 return;
909 transform_origin_ = transform_origin;
910 NoteLayerPropertyChangedForSubtree();
913 void LayerImpl::SetBackgroundColor(SkColor background_color) {
914 if (background_color_ == background_color)
915 return;
917 background_color_ = background_color;
918 NoteLayerPropertyChanged();
921 SkColor LayerImpl::SafeOpaqueBackgroundColor() const {
922 SkColor color = background_color();
923 if (SkColorGetA(color) == 255 && !contents_opaque()) {
924 color = SK_ColorTRANSPARENT;
925 } else if (SkColorGetA(color) != 255 && contents_opaque()) {
926 for (const LayerImpl* layer = parent(); layer;
927 layer = layer->parent()) {
928 color = layer->background_color();
929 if (SkColorGetA(color) == 255)
930 break;
932 if (SkColorGetA(color) != 255)
933 color = layer_tree_impl()->background_color();
934 if (SkColorGetA(color) != 255)
935 color = SkColorSetA(color, 255);
937 return color;
940 void LayerImpl::SetFilters(const FilterOperations& filters) {
941 if (filters_ == filters)
942 return;
944 filters_ = filters;
945 NoteLayerPropertyChangedForSubtree();
948 bool LayerImpl::FilterIsAnimating() const {
949 return layer_animation_controller_->IsAnimatingProperty(Animation::FILTER);
952 bool LayerImpl::FilterIsAnimatingOnImplOnly() const {
953 Animation* filter_animation =
954 layer_animation_controller_->GetAnimation(Animation::FILTER);
955 return filter_animation && filter_animation->is_impl_only();
958 void LayerImpl::SetBackgroundFilters(
959 const FilterOperations& filters) {
960 if (background_filters_ == filters)
961 return;
963 background_filters_ = filters;
964 NoteLayerPropertyChanged();
967 void LayerImpl::SetMasksToBounds(bool masks_to_bounds) {
968 if (masks_to_bounds_ == masks_to_bounds)
969 return;
971 masks_to_bounds_ = masks_to_bounds;
972 NoteLayerPropertyChangedForSubtree();
975 void LayerImpl::SetContentsOpaque(bool opaque) {
976 if (contents_opaque_ == opaque)
977 return;
979 contents_opaque_ = opaque;
980 NoteLayerPropertyChangedForSubtree();
983 void LayerImpl::SetOpacity(float opacity) {
984 if (opacity_ == opacity)
985 return;
987 opacity_ = opacity;
988 NoteLayerPropertyChangedForSubtree();
991 bool LayerImpl::OpacityIsAnimating() const {
992 return layer_animation_controller_->IsAnimatingProperty(Animation::OPACITY);
995 bool LayerImpl::OpacityIsAnimatingOnImplOnly() const {
996 Animation* opacity_animation =
997 layer_animation_controller_->GetAnimation(Animation::OPACITY);
998 return opacity_animation && opacity_animation->is_impl_only();
1001 void LayerImpl::SetBlendMode(SkXfermode::Mode blend_mode) {
1002 if (blend_mode_ == blend_mode)
1003 return;
1005 blend_mode_ = blend_mode;
1006 NoteLayerPropertyChangedForSubtree();
1009 void LayerImpl::SetIsRootForIsolatedGroup(bool root) {
1010 if (is_root_for_isolated_group_ == root)
1011 return;
1013 is_root_for_isolated_group_ = root;
1014 SetNeedsPushProperties();
1017 void LayerImpl::SetPosition(const gfx::PointF& position) {
1018 if (position_ == position)
1019 return;
1021 position_ = position;
1022 NoteLayerPropertyChangedForSubtree();
1025 void LayerImpl::SetShouldFlattenTransform(bool flatten) {
1026 if (should_flatten_transform_ == flatten)
1027 return;
1029 should_flatten_transform_ = flatten;
1030 NoteLayerPropertyChangedForSubtree();
1033 void LayerImpl::Set3dSortingContextId(int id) {
1034 if (id == sorting_context_id_)
1035 return;
1036 sorting_context_id_ = id;
1037 NoteLayerPropertyChangedForSubtree();
1040 void LayerImpl::PassFrameTimingRequests(
1041 std::vector<FrameTimingRequest>* requests) {
1042 frame_timing_requests_.swap(*requests);
1043 frame_timing_requests_dirty_ = true;
1044 SetNeedsPushProperties();
1047 void LayerImpl::SetTransform(const gfx::Transform& transform) {
1048 if (transform_ == transform)
1049 return;
1051 transform_ = transform;
1052 transform_is_invertible_ = transform_.IsInvertible();
1053 NoteLayerPropertyChangedForSubtree();
1056 void LayerImpl::SetTransformAndInvertibility(const gfx::Transform& transform,
1057 bool transform_is_invertible) {
1058 if (transform_ == transform) {
1059 DCHECK(transform_is_invertible_ == transform_is_invertible)
1060 << "Can't change invertibility if transform is unchanged";
1061 return;
1063 transform_ = transform;
1064 transform_is_invertible_ = transform_is_invertible;
1065 NoteLayerPropertyChangedForSubtree();
1068 bool LayerImpl::TransformIsAnimating() const {
1069 return layer_animation_controller_->IsAnimatingProperty(Animation::TRANSFORM);
1072 bool LayerImpl::TransformIsAnimatingOnImplOnly() const {
1073 Animation* transform_animation =
1074 layer_animation_controller_->GetAnimation(Animation::TRANSFORM);
1075 return transform_animation && transform_animation->is_impl_only();
1078 void LayerImpl::SetUpdateRect(const gfx::Rect& update_rect) {
1079 update_rect_ = update_rect;
1080 SetNeedsPushProperties();
1083 void LayerImpl::AddDamageRect(const gfx::RectF& damage_rect) {
1084 damage_rect_ = gfx::UnionRects(damage_rect_, damage_rect);
1087 void LayerImpl::SetContentBounds(const gfx::Size& content_bounds) {
1088 if (this->content_bounds() == content_bounds)
1089 return;
1091 draw_properties_.content_bounds = content_bounds;
1092 NoteLayerPropertyChanged();
1095 void LayerImpl::SetContentsScale(float contents_scale_x,
1096 float contents_scale_y) {
1097 if (this->contents_scale_x() == contents_scale_x &&
1098 this->contents_scale_y() == contents_scale_y)
1099 return;
1101 draw_properties_.contents_scale_x = contents_scale_x;
1102 draw_properties_.contents_scale_y = contents_scale_y;
1103 NoteLayerPropertyChanged();
1106 void LayerImpl::SetScrollOffsetDelegate(
1107 ScrollOffsetDelegate* scroll_offset_delegate) {
1108 // Having both a scroll parent and a scroll offset delegate is unsupported.
1109 DCHECK(!scroll_parent_);
1110 RefreshFromScrollDelegate();
1111 scroll_offset_delegate_ = scroll_offset_delegate;
1112 if (scroll_offset_delegate_)
1113 scroll_offset_delegate_->SetCurrentScrollOffset(CurrentScrollOffset());
1116 bool LayerImpl::IsExternalFlingActive() const {
1117 return scroll_offset_delegate_ &&
1118 scroll_offset_delegate_->IsExternalFlingActive();
1121 void LayerImpl::SetCurrentScrollOffset(const gfx::ScrollOffset& scroll_offset) {
1122 DCHECK(IsActive());
1123 if (scroll_offset_->SetCurrent(scroll_offset))
1124 DidUpdateScrollOffset();
1127 void LayerImpl::PushScrollOffsetFromMainThread(
1128 const gfx::ScrollOffset& scroll_offset) {
1129 PushScrollOffset(&scroll_offset);
1132 void LayerImpl::PushScrollOffsetFromMainThreadAndClobberActiveValue(
1133 const gfx::ScrollOffset& scroll_offset) {
1134 scroll_offset_->set_clobber_active_value();
1135 PushScrollOffset(&scroll_offset);
1138 gfx::ScrollOffset LayerImpl::PullDeltaForMainThread() {
1139 RefreshFromScrollDelegate();
1140 return scroll_offset_->PullDeltaForMainThread();
1143 void LayerImpl::RefreshFromScrollDelegate() {
1144 if (scroll_offset_delegate_) {
1145 SetCurrentScrollOffset(
1146 gfx::ScrollOffset(scroll_offset_delegate_->GetCurrentScrollOffset()));
1150 gfx::ScrollOffset LayerImpl::CurrentScrollOffset() const {
1151 return scroll_offset_->Current(IsActive());
1154 gfx::Vector2dF LayerImpl::ScrollDelta() const {
1155 if (IsActive())
1156 return gfx::Vector2dF(scroll_offset_->Delta().x(),
1157 scroll_offset_->Delta().y());
1158 else
1159 return gfx::Vector2dF(scroll_offset_->PendingDelta().get().x(),
1160 scroll_offset_->PendingDelta().get().y());
1163 void LayerImpl::SetScrollDelta(const gfx::Vector2dF& delta) {
1164 DCHECK(IsActive());
1165 SetCurrentScrollOffset(scroll_offset_->ActiveBase() +
1166 gfx::ScrollOffset(delta));
1169 gfx::ScrollOffset LayerImpl::BaseScrollOffset() const {
1170 if (IsActive())
1171 return scroll_offset_->ActiveBase();
1172 else
1173 return scroll_offset_->PendingBase();
1176 void LayerImpl::PushScrollOffset(const gfx::ScrollOffset* scroll_offset) {
1177 DCHECK(scroll_offset || IsActive());
1178 bool changed = false;
1179 if (scroll_offset) {
1180 DCHECK(!IsActive() || !layer_tree_impl_->FindPendingTreeLayerById(id()));
1181 changed |= scroll_offset_->PushFromMainThread(*scroll_offset);
1183 if (IsActive()) {
1184 changed |= scroll_offset_->PushPendingToActive();
1187 if (changed)
1188 DidUpdateScrollOffset();
1191 void LayerImpl::DidUpdateScrollOffset() {
1192 if (scroll_offset_delegate_) {
1193 scroll_offset_delegate_->SetCurrentScrollOffset(CurrentScrollOffset());
1194 scroll_offset_delegate_->Update();
1195 RefreshFromScrollDelegate();
1198 NoteLayerPropertyChangedForSubtree();
1199 ScrollbarParametersDidChange(false);
1202 void LayerImpl::SetDoubleSided(bool double_sided) {
1203 if (double_sided_ == double_sided)
1204 return;
1206 double_sided_ = double_sided;
1207 NoteLayerPropertyChangedForSubtree();
1210 SimpleEnclosedRegion LayerImpl::VisibleContentOpaqueRegion() const {
1211 if (contents_opaque())
1212 return SimpleEnclosedRegion(visible_content_rect());
1213 return SimpleEnclosedRegion();
1216 void LayerImpl::DidBeginTracing() {}
1218 void LayerImpl::ReleaseResources() {}
1220 void LayerImpl::RecreateResources() {
1223 gfx::ScrollOffset LayerImpl::MaxScrollOffset() const {
1224 if (!scroll_clip_layer_ || bounds().IsEmpty())
1225 return gfx::ScrollOffset();
1227 LayerImpl const* page_scale_layer = layer_tree_impl()->page_scale_layer();
1228 DCHECK(this != page_scale_layer);
1229 DCHECK(this != layer_tree_impl()->InnerViewportScrollLayer() ||
1230 IsContainerForFixedPositionLayers());
1232 float scale_factor = 1.f;
1233 for (LayerImpl const* current_layer = this;
1234 current_layer != scroll_clip_layer_->parent();
1235 current_layer = current_layer->parent()) {
1236 if (current_layer == page_scale_layer)
1237 scale_factor = layer_tree_impl()->current_page_scale_factor();
1240 gfx::SizeF scaled_scroll_bounds =
1241 gfx::ToFlooredSize(gfx::ScaleSize(BoundsForScrolling(), scale_factor));
1242 scaled_scroll_bounds = gfx::ToFlooredSize(scaled_scroll_bounds);
1244 gfx::ScrollOffset max_offset(
1245 scaled_scroll_bounds.width() - scroll_clip_layer_->bounds().width(),
1246 scaled_scroll_bounds.height() - scroll_clip_layer_->bounds().height());
1247 // We need the final scroll offset to be in CSS coords.
1248 max_offset.Scale(1 / scale_factor);
1249 max_offset.SetToMax(gfx::ScrollOffset());
1250 return max_offset;
1253 gfx::ScrollOffset LayerImpl::ClampScrollOffsetToLimits(
1254 gfx::ScrollOffset offset) const {
1255 offset.SetToMin(MaxScrollOffset());
1256 offset.SetToMax(gfx::ScrollOffset());
1257 return offset;
1260 gfx::Vector2dF LayerImpl::ClampScrollToMaxScrollOffset() {
1261 gfx::ScrollOffset old_offset = CurrentScrollOffset();
1262 gfx::ScrollOffset clamped_offset = ClampScrollOffsetToLimits(old_offset);
1263 gfx::Vector2dF delta = clamped_offset.DeltaFrom(old_offset);
1264 if (!delta.IsZero())
1265 ScrollBy(delta);
1266 return delta;
1269 void LayerImpl::SetScrollbarPosition(ScrollbarLayerImplBase* scrollbar_layer,
1270 LayerImpl* scrollbar_clip_layer,
1271 bool on_resize) const {
1272 DCHECK(scrollbar_layer);
1273 LayerImpl* page_scale_layer = layer_tree_impl()->page_scale_layer();
1275 DCHECK(this != page_scale_layer);
1276 DCHECK(scrollbar_clip_layer);
1277 gfx::RectF clip_rect(gfx::PointF(),
1278 scrollbar_clip_layer->BoundsForScrolling());
1280 // See comment in MaxScrollOffset() regarding the use of the content layer
1281 // bounds here.
1282 gfx::RectF scroll_rect(gfx::PointF(), BoundsForScrolling());
1284 if (scroll_rect.size().IsEmpty())
1285 return;
1287 gfx::ScrollOffset current_offset;
1288 for (LayerImpl const* current_layer = this;
1289 current_layer != scrollbar_clip_layer->parent();
1290 current_layer = current_layer->parent()) {
1291 current_offset += current_layer->CurrentScrollOffset();
1292 if (current_layer == page_scale_layer) {
1293 float scale_factor = layer_tree_impl()->current_page_scale_factor();
1294 current_offset.Scale(scale_factor);
1295 scroll_rect.Scale(scale_factor);
1299 bool scrollbar_needs_animation = false;
1300 scrollbar_needs_animation |= scrollbar_layer->SetVerticalAdjust(
1301 scrollbar_clip_layer->bounds_delta().y());
1302 if (scrollbar_layer->orientation() == HORIZONTAL) {
1303 float visible_ratio = clip_rect.width() / scroll_rect.width();
1304 scrollbar_needs_animation |=
1305 scrollbar_layer->SetCurrentPos(current_offset.x());
1306 scrollbar_needs_animation |=
1307 scrollbar_layer->SetMaximum(scroll_rect.width() - clip_rect.width());
1308 scrollbar_needs_animation |=
1309 scrollbar_layer->SetVisibleToTotalLengthRatio(visible_ratio);
1310 } else {
1311 float visible_ratio = clip_rect.height() / scroll_rect.height();
1312 scrollbar_needs_animation |=
1313 scrollbar_layer->SetCurrentPos(current_offset.y());
1314 scrollbar_needs_animation |=
1315 scrollbar_layer->SetMaximum(scroll_rect.height() - clip_rect.height());
1316 scrollbar_needs_animation |=
1317 scrollbar_layer->SetVisibleToTotalLengthRatio(visible_ratio);
1319 if (scrollbar_needs_animation) {
1320 layer_tree_impl()->set_needs_update_draw_properties();
1321 // TODO(wjmaclean) The scrollbar animator for the pinch-zoom scrollbars
1322 // should activate for every scroll on the main frame, not just the
1323 // scrolls that move the pinch virtual viewport (i.e. trigger from
1324 // either inner or outer viewport).
1325 if (scrollbar_animation_controller_) {
1326 // Non-overlay scrollbars shouldn't trigger animations.
1327 if (scrollbar_layer->is_overlay_scrollbar())
1328 scrollbar_animation_controller_->DidScrollUpdate(on_resize);
1333 void LayerImpl::DidBecomeActive() {
1334 if (layer_tree_impl_->settings().scrollbar_animator ==
1335 LayerTreeSettings::NO_ANIMATOR) {
1336 return;
1339 bool need_scrollbar_animation_controller = scrollable() && scrollbars_;
1340 if (!need_scrollbar_animation_controller) {
1341 scrollbar_animation_controller_ = nullptr;
1342 return;
1345 if (scrollbar_animation_controller_)
1346 return;
1348 scrollbar_animation_controller_ =
1349 layer_tree_impl_->CreateScrollbarAnimationController(this);
1352 void LayerImpl::ClearScrollbars() {
1353 if (!scrollbars_)
1354 return;
1356 scrollbars_.reset(nullptr);
1359 void LayerImpl::AddScrollbar(ScrollbarLayerImplBase* layer) {
1360 DCHECK(layer);
1361 DCHECK(!scrollbars_ || scrollbars_->find(layer) == scrollbars_->end());
1362 if (!scrollbars_)
1363 scrollbars_.reset(new ScrollbarSet());
1365 scrollbars_->insert(layer);
1368 void LayerImpl::RemoveScrollbar(ScrollbarLayerImplBase* layer) {
1369 DCHECK(scrollbars_);
1370 DCHECK(layer);
1371 DCHECK(scrollbars_->find(layer) != scrollbars_->end());
1373 scrollbars_->erase(layer);
1374 if (scrollbars_->empty())
1375 scrollbars_ = nullptr;
1378 bool LayerImpl::HasScrollbar(ScrollbarOrientation orientation) const {
1379 if (!scrollbars_)
1380 return false;
1382 for (ScrollbarSet::iterator it = scrollbars_->begin();
1383 it != scrollbars_->end();
1384 ++it)
1385 if ((*it)->orientation() == orientation)
1386 return true;
1388 return false;
1391 void LayerImpl::ScrollbarParametersDidChange(bool on_resize) {
1392 if (!scrollbars_)
1393 return;
1395 for (ScrollbarSet::iterator it = scrollbars_->begin();
1396 it != scrollbars_->end();
1397 ++it) {
1398 bool is_scroll_layer = (*it)->ScrollLayerId() == layer_id_;
1399 bool scroll_layer_resized = is_scroll_layer && on_resize;
1400 (*it)->ScrollbarParametersDidChange(scroll_layer_resized);
1404 void LayerImpl::SetNeedsPushProperties() {
1405 if (needs_push_properties_)
1406 return;
1407 if (!parent_should_know_need_push_properties() && parent_)
1408 parent_->AddDependentNeedsPushProperties();
1409 needs_push_properties_ = true;
1412 void LayerImpl::AddDependentNeedsPushProperties() {
1413 DCHECK_GE(num_dependents_need_push_properties_, 0);
1415 if (!parent_should_know_need_push_properties() && parent_)
1416 parent_->AddDependentNeedsPushProperties();
1418 num_dependents_need_push_properties_++;
1421 void LayerImpl::RemoveDependentNeedsPushProperties() {
1422 num_dependents_need_push_properties_--;
1423 DCHECK_GE(num_dependents_need_push_properties_, 0);
1425 if (!parent_should_know_need_push_properties() && parent_)
1426 parent_->RemoveDependentNeedsPushProperties();
1429 void LayerImpl::GetAllTilesForTracing(std::set<const Tile*>* tiles) const {
1432 void LayerImpl::AsValueInto(base::trace_event::TracedValue* state) const {
1433 TracedValue::MakeDictIntoImplicitSnapshotWithCategory(
1434 TRACE_DISABLED_BY_DEFAULT("cc.debug"),
1435 state,
1436 "cc::LayerImpl",
1437 LayerTypeAsString(),
1438 this);
1439 state->SetInteger("layer_id", id());
1440 MathUtil::AddToTracedValue("bounds", bounds_, state);
1442 state->SetDouble("opacity", opacity());
1444 MathUtil::AddToTracedValue("position", position_, state);
1446 state->SetInteger("draws_content", DrawsContent());
1447 state->SetInteger("gpu_memory_usage", GPUMemoryUsageInBytes());
1449 MathUtil::AddToTracedValue(
1450 "scroll_offset", scroll_offset_ ? scroll_offset_->Current(IsActive())
1451 : gfx::ScrollOffset(),
1452 state);
1454 MathUtil::AddToTracedValue("transform_origin", transform_origin_, state);
1456 bool clipped;
1457 gfx::QuadF layer_quad = MathUtil::MapQuad(
1458 screen_space_transform(),
1459 gfx::QuadF(gfx::Rect(content_bounds())),
1460 &clipped);
1461 MathUtil::AddToTracedValue("layer_quad", layer_quad, state);
1462 if (!touch_event_handler_region_.IsEmpty()) {
1463 state->BeginArray("touch_event_handler_region");
1464 touch_event_handler_region_.AsValueInto(state);
1465 state->EndArray();
1467 if (have_wheel_event_handlers_) {
1468 gfx::Rect wheel_rect(content_bounds());
1469 Region wheel_region(wheel_rect);
1470 state->BeginArray("wheel_event_handler_region");
1471 wheel_region.AsValueInto(state);
1472 state->EndArray();
1474 if (have_scroll_event_handlers_) {
1475 gfx::Rect scroll_rect(content_bounds());
1476 Region scroll_region(scroll_rect);
1477 state->BeginArray("scroll_event_handler_region");
1478 scroll_region.AsValueInto(state);
1479 state->EndArray();
1481 if (!non_fast_scrollable_region_.IsEmpty()) {
1482 state->BeginArray("non_fast_scrollable_region");
1483 non_fast_scrollable_region_.AsValueInto(state);
1484 state->EndArray();
1486 if (scroll_blocks_on_) {
1487 state->SetInteger("scroll_blocks_on", scroll_blocks_on_);
1490 state->BeginArray("children");
1491 for (size_t i = 0; i < children_.size(); ++i) {
1492 state->BeginDictionary();
1493 children_[i]->AsValueInto(state);
1494 state->EndDictionary();
1496 state->EndArray();
1497 if (mask_layer_) {
1498 state->BeginDictionary("mask_layer");
1499 mask_layer_->AsValueInto(state);
1500 state->EndDictionary();
1502 if (replica_layer_) {
1503 state->BeginDictionary("replica_layer");
1504 replica_layer_->AsValueInto(state);
1505 state->EndDictionary();
1508 if (scroll_parent_)
1509 state->SetInteger("scroll_parent", scroll_parent_->id());
1511 if (clip_parent_)
1512 state->SetInteger("clip_parent", clip_parent_->id());
1514 state->SetBoolean("can_use_lcd_text", can_use_lcd_text());
1515 state->SetBoolean("contents_opaque", contents_opaque());
1517 state->SetBoolean(
1518 "has_animation_bounds",
1519 layer_animation_controller()->HasAnimationThatInflatesBounds());
1521 gfx::BoxF box;
1522 if (LayerUtils::GetAnimationBounds(*this, &box))
1523 MathUtil::AddToTracedValue("animation_bounds", box, state);
1525 if (debug_info_.get()) {
1526 std::string str;
1527 debug_info_->AppendAsTraceFormat(&str);
1528 base::JSONReader json_reader;
1529 scoped_ptr<base::Value> debug_info_value(json_reader.ReadToValue(str));
1531 if (debug_info_value->IsType(base::Value::TYPE_DICTIONARY)) {
1532 base::DictionaryValue* dictionary_value = nullptr;
1533 bool converted_to_dictionary =
1534 debug_info_value->GetAsDictionary(&dictionary_value);
1535 DCHECK(converted_to_dictionary);
1536 for (base::DictionaryValue::Iterator it(*dictionary_value); !it.IsAtEnd();
1537 it.Advance()) {
1538 state->SetValue(it.key().data(), it.value().DeepCopy());
1540 } else {
1541 NOTREACHED();
1545 if (!frame_timing_requests_.empty()) {
1546 state->BeginArray("frame_timing_requests");
1547 for (const auto& request : frame_timing_requests_) {
1548 state->BeginDictionary();
1549 state->SetInteger("request_id", request.id());
1550 MathUtil::AddToTracedValue("request_rect", request.rect(), state);
1551 state->EndDictionary();
1553 state->EndArray();
1557 bool LayerImpl::IsDrawnRenderSurfaceLayerListMember() const {
1558 return draw_properties_.last_drawn_render_surface_layer_list_id ==
1559 layer_tree_impl_->current_render_surface_list_id();
1562 size_t LayerImpl::GPUMemoryUsageInBytes() const { return 0; }
1564 void LayerImpl::RunMicroBenchmark(MicroBenchmarkImpl* benchmark) {
1565 benchmark->RunOnLayer(this);
1568 int LayerImpl::NumDescendantsThatDrawContent() const {
1569 return num_descendants_that_draw_content_;
1572 void LayerImpl::NotifyAnimationFinished(
1573 base::TimeTicks monotonic_time,
1574 Animation::TargetProperty target_property,
1575 int group) {
1576 if (target_property == Animation::SCROLL_OFFSET)
1577 layer_tree_impl_->InputScrollAnimationFinished();
1580 void LayerImpl::SetHasRenderSurface(bool should_have_render_surface) {
1581 if (!!render_surface() == should_have_render_surface)
1582 return;
1584 SetNeedsPushProperties();
1585 layer_tree_impl()->set_needs_update_draw_properties();
1586 if (should_have_render_surface) {
1587 render_surface_ = make_scoped_ptr(new RenderSurfaceImpl(this));
1588 return;
1590 render_surface_.reset();
1593 Region LayerImpl::GetInvalidationRegion() {
1594 return Region(update_rect_);
1597 } // namespace cc