Web MIDI: add MidiScheduler for send() with timestamp
[chromium-blink-merge.git] / cc / layers / layer_impl.cc
blobb4ba876d0944697bcabe19e15835c070f9f01811
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();
1141 // TODO(miletus): Remove all this temporary flooring machinery when
1142 // Blink fully supports fractional scrolls.
1143 gfx::ScrollOffset current_offset = CurrentScrollOffset();
1144 gfx::Vector2dF current_delta = ScrollDelta();
1145 gfx::Vector2dF floored_delta(floor(current_delta.x()),
1146 floor(current_delta.y()));
1147 gfx::Vector2dF diff_delta = floored_delta - current_delta;
1148 gfx::ScrollOffset tmp_offset = ScrollOffsetWithDelta(current_offset,
1149 diff_delta);
1150 scroll_offset_->SetCurrent(tmp_offset);
1151 gfx::ScrollOffset delta = scroll_offset_->PullDeltaForMainThread();
1152 scroll_offset_->SetCurrent(current_offset);
1153 return delta;
1156 void LayerImpl::RefreshFromScrollDelegate() {
1157 if (scroll_offset_delegate_) {
1158 SetCurrentScrollOffset(
1159 gfx::ScrollOffset(scroll_offset_delegate_->GetCurrentScrollOffset()));
1163 gfx::ScrollOffset LayerImpl::CurrentScrollOffset() const {
1164 return scroll_offset_->Current(IsActive());
1167 gfx::Vector2dF LayerImpl::ScrollDelta() const {
1168 if (IsActive())
1169 return gfx::Vector2dF(scroll_offset_->Delta().x(),
1170 scroll_offset_->Delta().y());
1171 else
1172 return gfx::Vector2dF(scroll_offset_->PendingDelta().get().x(),
1173 scroll_offset_->PendingDelta().get().y());
1176 void LayerImpl::SetScrollDelta(const gfx::Vector2dF& delta) {
1177 DCHECK(IsActive());
1178 SetCurrentScrollOffset(scroll_offset_->ActiveBase() +
1179 gfx::ScrollOffset(delta));
1182 gfx::ScrollOffset LayerImpl::BaseScrollOffset() const {
1183 if (IsActive())
1184 return scroll_offset_->ActiveBase();
1185 else
1186 return scroll_offset_->PendingBase();
1189 void LayerImpl::PushScrollOffset(const gfx::ScrollOffset* scroll_offset) {
1190 DCHECK(scroll_offset || IsActive());
1191 bool changed = false;
1192 if (scroll_offset) {
1193 DCHECK(!IsActive() || !layer_tree_impl_->FindPendingTreeLayerById(id()));
1194 changed |= scroll_offset_->PushFromMainThread(*scroll_offset);
1196 if (IsActive()) {
1197 changed |= scroll_offset_->PushPendingToActive();
1200 if (changed)
1201 DidUpdateScrollOffset();
1204 void LayerImpl::DidUpdateScrollOffset() {
1205 if (scroll_offset_delegate_) {
1206 scroll_offset_delegate_->SetCurrentScrollOffset(CurrentScrollOffset());
1207 scroll_offset_delegate_->Update();
1208 RefreshFromScrollDelegate();
1211 NoteLayerPropertyChangedForSubtree();
1212 ScrollbarParametersDidChange(false);
1215 void LayerImpl::SetDoubleSided(bool double_sided) {
1216 if (double_sided_ == double_sided)
1217 return;
1219 double_sided_ = double_sided;
1220 NoteLayerPropertyChangedForSubtree();
1223 SimpleEnclosedRegion LayerImpl::VisibleContentOpaqueRegion() const {
1224 if (contents_opaque())
1225 return SimpleEnclosedRegion(visible_content_rect());
1226 return SimpleEnclosedRegion();
1229 void LayerImpl::DidBeginTracing() {}
1231 void LayerImpl::ReleaseResources() {}
1233 void LayerImpl::RecreateResources() {
1236 gfx::ScrollOffset LayerImpl::MaxScrollOffset() const {
1237 if (!scroll_clip_layer_ || bounds().IsEmpty())
1238 return gfx::ScrollOffset();
1240 LayerImpl const* page_scale_layer = layer_tree_impl()->page_scale_layer();
1241 DCHECK(this != page_scale_layer);
1242 DCHECK(this != layer_tree_impl()->InnerViewportScrollLayer() ||
1243 IsContainerForFixedPositionLayers());
1245 float scale_factor = 1.f;
1246 for (LayerImpl const* current_layer = this;
1247 current_layer != scroll_clip_layer_->parent();
1248 current_layer = current_layer->parent()) {
1249 if (current_layer == page_scale_layer)
1250 scale_factor = layer_tree_impl()->current_page_scale_factor();
1253 gfx::SizeF scaled_scroll_bounds =
1254 gfx::ToFlooredSize(gfx::ScaleSize(BoundsForScrolling(), scale_factor));
1255 scaled_scroll_bounds = gfx::ToFlooredSize(scaled_scroll_bounds);
1257 gfx::ScrollOffset max_offset(
1258 scaled_scroll_bounds.width() - scroll_clip_layer_->bounds().width(),
1259 scaled_scroll_bounds.height() - scroll_clip_layer_->bounds().height());
1260 // We need the final scroll offset to be in CSS coords.
1261 max_offset.Scale(1 / scale_factor);
1262 max_offset.SetToMax(gfx::ScrollOffset());
1263 return max_offset;
1266 gfx::ScrollOffset LayerImpl::ClampScrollOffsetToLimits(
1267 gfx::ScrollOffset offset) const {
1268 offset.SetToMin(MaxScrollOffset());
1269 offset.SetToMax(gfx::ScrollOffset());
1270 return offset;
1273 gfx::Vector2dF LayerImpl::ClampScrollToMaxScrollOffset() {
1274 gfx::ScrollOffset old_offset = CurrentScrollOffset();
1275 gfx::ScrollOffset clamped_offset = ClampScrollOffsetToLimits(old_offset);
1276 gfx::Vector2dF delta = clamped_offset.DeltaFrom(old_offset);
1277 if (!delta.IsZero())
1278 ScrollBy(delta);
1279 return delta;
1282 void LayerImpl::SetScrollbarPosition(ScrollbarLayerImplBase* scrollbar_layer,
1283 LayerImpl* scrollbar_clip_layer,
1284 bool on_resize) const {
1285 DCHECK(scrollbar_layer);
1286 LayerImpl* page_scale_layer = layer_tree_impl()->page_scale_layer();
1288 DCHECK(this != page_scale_layer);
1289 DCHECK(scrollbar_clip_layer);
1290 gfx::RectF clip_rect(gfx::PointF(),
1291 scrollbar_clip_layer->BoundsForScrolling());
1293 // See comment in MaxScrollOffset() regarding the use of the content layer
1294 // bounds here.
1295 gfx::RectF scroll_rect(gfx::PointF(), BoundsForScrolling());
1297 if (scroll_rect.size().IsEmpty())
1298 return;
1300 gfx::ScrollOffset current_offset;
1301 for (LayerImpl const* current_layer = this;
1302 current_layer != scrollbar_clip_layer->parent();
1303 current_layer = current_layer->parent()) {
1304 current_offset += current_layer->CurrentScrollOffset();
1305 if (current_layer == page_scale_layer) {
1306 float scale_factor = layer_tree_impl()->current_page_scale_factor();
1307 current_offset.Scale(scale_factor);
1308 scroll_rect.Scale(scale_factor);
1312 bool scrollbar_needs_animation = false;
1313 scrollbar_needs_animation |= scrollbar_layer->SetVerticalAdjust(
1314 scrollbar_clip_layer->bounds_delta().y());
1315 if (scrollbar_layer->orientation() == HORIZONTAL) {
1316 float visible_ratio = clip_rect.width() / scroll_rect.width();
1317 scrollbar_needs_animation |=
1318 scrollbar_layer->SetCurrentPos(current_offset.x());
1319 scrollbar_needs_animation |=
1320 scrollbar_layer->SetMaximum(scroll_rect.width() - clip_rect.width());
1321 scrollbar_needs_animation |=
1322 scrollbar_layer->SetVisibleToTotalLengthRatio(visible_ratio);
1323 } else {
1324 float visible_ratio = clip_rect.height() / scroll_rect.height();
1325 bool y_offset_did_change =
1326 scrollbar_layer->SetCurrentPos(current_offset.y());
1327 scrollbar_needs_animation |= y_offset_did_change;
1328 scrollbar_needs_animation |=
1329 scrollbar_layer->SetMaximum(scroll_rect.height() - clip_rect.height());
1330 scrollbar_needs_animation |=
1331 scrollbar_layer->SetVisibleToTotalLengthRatio(visible_ratio);
1332 if (y_offset_did_change && layer_tree_impl()->IsActiveTree() &&
1333 this == layer_tree_impl()->InnerViewportScrollLayer()) {
1334 TRACE_COUNTER_ID1("cc", "scroll_offset_y", this->id(),
1335 current_offset.y());
1338 if (scrollbar_needs_animation) {
1339 layer_tree_impl()->set_needs_update_draw_properties();
1340 // TODO(wjmaclean) The scrollbar animator for the pinch-zoom scrollbars
1341 // should activate for every scroll on the main frame, not just the
1342 // scrolls that move the pinch virtual viewport (i.e. trigger from
1343 // either inner or outer viewport).
1344 if (scrollbar_animation_controller_) {
1345 // Non-overlay scrollbars shouldn't trigger animations.
1346 if (scrollbar_layer->is_overlay_scrollbar())
1347 scrollbar_animation_controller_->DidScrollUpdate(on_resize);
1352 void LayerImpl::DidBecomeActive() {
1353 if (layer_tree_impl_->settings().scrollbar_animator ==
1354 LayerTreeSettings::NO_ANIMATOR) {
1355 return;
1358 bool need_scrollbar_animation_controller = scrollable() && scrollbars_;
1359 if (!need_scrollbar_animation_controller) {
1360 scrollbar_animation_controller_ = nullptr;
1361 return;
1364 if (scrollbar_animation_controller_)
1365 return;
1367 scrollbar_animation_controller_ =
1368 layer_tree_impl_->CreateScrollbarAnimationController(this);
1371 void LayerImpl::ClearScrollbars() {
1372 if (!scrollbars_)
1373 return;
1375 scrollbars_.reset(nullptr);
1378 void LayerImpl::AddScrollbar(ScrollbarLayerImplBase* layer) {
1379 DCHECK(layer);
1380 DCHECK(!scrollbars_ || scrollbars_->find(layer) == scrollbars_->end());
1381 if (!scrollbars_)
1382 scrollbars_.reset(new ScrollbarSet());
1384 scrollbars_->insert(layer);
1387 void LayerImpl::RemoveScrollbar(ScrollbarLayerImplBase* layer) {
1388 DCHECK(scrollbars_);
1389 DCHECK(layer);
1390 DCHECK(scrollbars_->find(layer) != scrollbars_->end());
1392 scrollbars_->erase(layer);
1393 if (scrollbars_->empty())
1394 scrollbars_ = nullptr;
1397 bool LayerImpl::HasScrollbar(ScrollbarOrientation orientation) const {
1398 if (!scrollbars_)
1399 return false;
1401 for (ScrollbarSet::iterator it = scrollbars_->begin();
1402 it != scrollbars_->end();
1403 ++it)
1404 if ((*it)->orientation() == orientation)
1405 return true;
1407 return false;
1410 void LayerImpl::ScrollbarParametersDidChange(bool on_resize) {
1411 if (!scrollbars_)
1412 return;
1414 for (ScrollbarSet::iterator it = scrollbars_->begin();
1415 it != scrollbars_->end();
1416 ++it) {
1417 bool is_scroll_layer = (*it)->ScrollLayerId() == layer_id_;
1418 bool scroll_layer_resized = is_scroll_layer && on_resize;
1419 (*it)->ScrollbarParametersDidChange(scroll_layer_resized);
1423 void LayerImpl::SetNeedsPushProperties() {
1424 if (needs_push_properties_)
1425 return;
1426 if (!parent_should_know_need_push_properties() && parent_)
1427 parent_->AddDependentNeedsPushProperties();
1428 needs_push_properties_ = true;
1431 void LayerImpl::AddDependentNeedsPushProperties() {
1432 DCHECK_GE(num_dependents_need_push_properties_, 0);
1434 if (!parent_should_know_need_push_properties() && parent_)
1435 parent_->AddDependentNeedsPushProperties();
1437 num_dependents_need_push_properties_++;
1440 void LayerImpl::RemoveDependentNeedsPushProperties() {
1441 num_dependents_need_push_properties_--;
1442 DCHECK_GE(num_dependents_need_push_properties_, 0);
1444 if (!parent_should_know_need_push_properties() && parent_)
1445 parent_->RemoveDependentNeedsPushProperties();
1448 void LayerImpl::GetAllTilesAndPrioritiesForTracing(
1449 std::map<const Tile*, TilePriority>* tile_map) const {
1452 void LayerImpl::AsValueInto(base::trace_event::TracedValue* state) const {
1453 TracedValue::MakeDictIntoImplicitSnapshotWithCategory(
1454 TRACE_DISABLED_BY_DEFAULT("cc.debug"),
1455 state,
1456 "cc::LayerImpl",
1457 LayerTypeAsString(),
1458 this);
1459 state->SetInteger("layer_id", id());
1460 MathUtil::AddToTracedValue("bounds", bounds_, state);
1462 state->SetDouble("opacity", opacity());
1464 MathUtil::AddToTracedValue("position", position_, state);
1466 state->SetInteger("draws_content", DrawsContent());
1467 state->SetInteger("gpu_memory_usage", GPUMemoryUsageInBytes());
1469 MathUtil::AddToTracedValue(
1470 "scroll_offset", scroll_offset_ ? scroll_offset_->Current(IsActive())
1471 : gfx::ScrollOffset(),
1472 state);
1474 MathUtil::AddToTracedValue("transform_origin", transform_origin_, state);
1476 bool clipped;
1477 gfx::QuadF layer_quad = MathUtil::MapQuad(
1478 screen_space_transform(),
1479 gfx::QuadF(gfx::Rect(content_bounds())),
1480 &clipped);
1481 MathUtil::AddToTracedValue("layer_quad", layer_quad, state);
1482 if (!touch_event_handler_region_.IsEmpty()) {
1483 state->BeginArray("touch_event_handler_region");
1484 touch_event_handler_region_.AsValueInto(state);
1485 state->EndArray();
1487 if (have_wheel_event_handlers_) {
1488 gfx::Rect wheel_rect(content_bounds());
1489 Region wheel_region(wheel_rect);
1490 state->BeginArray("wheel_event_handler_region");
1491 wheel_region.AsValueInto(state);
1492 state->EndArray();
1494 if (have_scroll_event_handlers_) {
1495 gfx::Rect scroll_rect(content_bounds());
1496 Region scroll_region(scroll_rect);
1497 state->BeginArray("scroll_event_handler_region");
1498 scroll_region.AsValueInto(state);
1499 state->EndArray();
1501 if (!non_fast_scrollable_region_.IsEmpty()) {
1502 state->BeginArray("non_fast_scrollable_region");
1503 non_fast_scrollable_region_.AsValueInto(state);
1504 state->EndArray();
1506 if (scroll_blocks_on_) {
1507 state->SetInteger("scroll_blocks_on", scroll_blocks_on_);
1510 state->BeginArray("children");
1511 for (size_t i = 0; i < children_.size(); ++i) {
1512 state->BeginDictionary();
1513 children_[i]->AsValueInto(state);
1514 state->EndDictionary();
1516 state->EndArray();
1517 if (mask_layer_) {
1518 state->BeginDictionary("mask_layer");
1519 mask_layer_->AsValueInto(state);
1520 state->EndDictionary();
1522 if (replica_layer_) {
1523 state->BeginDictionary("replica_layer");
1524 replica_layer_->AsValueInto(state);
1525 state->EndDictionary();
1528 if (scroll_parent_)
1529 state->SetInteger("scroll_parent", scroll_parent_->id());
1531 if (clip_parent_)
1532 state->SetInteger("clip_parent", clip_parent_->id());
1534 state->SetBoolean("can_use_lcd_text", can_use_lcd_text());
1535 state->SetBoolean("contents_opaque", contents_opaque());
1537 state->SetBoolean(
1538 "has_animation_bounds",
1539 layer_animation_controller()->HasAnimationThatInflatesBounds());
1541 gfx::BoxF box;
1542 if (LayerUtils::GetAnimationBounds(*this, &box))
1543 MathUtil::AddToTracedValue("animation_bounds", box, state);
1545 if (debug_info_.get()) {
1546 std::string str;
1547 debug_info_->AppendAsTraceFormat(&str);
1548 base::JSONReader json_reader;
1549 scoped_ptr<base::Value> debug_info_value(json_reader.ReadToValue(str));
1551 if (debug_info_value->IsType(base::Value::TYPE_DICTIONARY)) {
1552 base::DictionaryValue* dictionary_value = nullptr;
1553 bool converted_to_dictionary =
1554 debug_info_value->GetAsDictionary(&dictionary_value);
1555 DCHECK(converted_to_dictionary);
1556 for (base::DictionaryValue::Iterator it(*dictionary_value); !it.IsAtEnd();
1557 it.Advance()) {
1558 state->SetValue(it.key().data(), it.value().DeepCopy());
1560 } else {
1561 NOTREACHED();
1565 if (!frame_timing_requests_.empty()) {
1566 state->BeginArray("frame_timing_requests");
1567 for (const auto& request : frame_timing_requests_) {
1568 state->BeginDictionary();
1569 state->SetInteger("request_id", request.id());
1570 MathUtil::AddToTracedValue("request_rect", request.rect(), state);
1571 state->EndDictionary();
1573 state->EndArray();
1577 bool LayerImpl::IsDrawnRenderSurfaceLayerListMember() const {
1578 return draw_properties_.last_drawn_render_surface_layer_list_id ==
1579 layer_tree_impl_->current_render_surface_list_id();
1582 size_t LayerImpl::GPUMemoryUsageInBytes() const { return 0; }
1584 void LayerImpl::RunMicroBenchmark(MicroBenchmarkImpl* benchmark) {
1585 benchmark->RunOnLayer(this);
1588 int LayerImpl::NumDescendantsThatDrawContent() const {
1589 return num_descendants_that_draw_content_;
1592 void LayerImpl::NotifyAnimationFinished(
1593 base::TimeTicks monotonic_time,
1594 Animation::TargetProperty target_property,
1595 int group) {
1596 if (target_property == Animation::SCROLL_OFFSET)
1597 layer_tree_impl_->InputScrollAnimationFinished();
1600 void LayerImpl::SetHasRenderSurface(bool should_have_render_surface) {
1601 if (!!render_surface() == should_have_render_surface)
1602 return;
1604 SetNeedsPushProperties();
1605 layer_tree_impl()->set_needs_update_draw_properties();
1606 if (should_have_render_surface) {
1607 render_surface_ = make_scoped_ptr(new RenderSurfaceImpl(this));
1608 return;
1610 render_surface_.reset();
1613 Region LayerImpl::GetInvalidationRegion() {
1614 return Region(update_rect_);
1617 gfx::Rect LayerImpl::GetEnclosingRectInTargetSpace() const {
1618 return MathUtil::MapEnclosingClippedRect(
1619 draw_properties_.target_space_transform,
1620 gfx::Rect(draw_properties_.content_bounds));
1623 gfx::Rect LayerImpl::GetScaledEnclosingRectInTargetSpace(float scale) const {
1624 gfx::Transform scaled_draw_transform =
1625 draw_properties_.target_space_transform;
1626 scaled_draw_transform.Scale(SK_MScalar1 / scale, SK_MScalar1 / scale);
1627 gfx::Size scaled_content_bounds =
1628 gfx::ToCeiledSize(gfx::ScaleSize(content_bounds(), scale));
1629 return MathUtil::MapEnclosingClippedRect(scaled_draw_transform,
1630 gfx::Rect(scaled_content_bounds));
1633 } // namespace cc