[Chromoting] Remove check for out/Release dir in build-webapp.py
[chromium-blink-merge.git] / cc / layers / layer_impl.cc
blob29fc3a33d95a6550b0ee88106773e6cc7a8c4d05
1 // Copyright 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "cc/layers/layer_impl.h"
7 #include "base/json/json_reader.h"
8 #include "base/strings/stringprintf.h"
9 #include "base/trace_event/trace_event.h"
10 #include "base/trace_event/trace_event_argument.h"
11 #include "cc/animation/animation_registrar.h"
12 #include "cc/animation/scrollbar_animation_controller.h"
13 #include "cc/base/math_util.h"
14 #include "cc/base/simple_enclosed_region.h"
15 #include "cc/debug/debug_colors.h"
16 #include "cc/debug/layer_tree_debug_state.h"
17 #include "cc/debug/micro_benchmark_impl.h"
18 #include "cc/debug/traced_value.h"
19 #include "cc/layers/layer_utils.h"
20 #include "cc/layers/painted_scrollbar_layer_impl.h"
21 #include "cc/output/copy_output_request.h"
22 #include "cc/quads/debug_border_draw_quad.h"
23 #include "cc/quads/render_pass.h"
24 #include "cc/trees/layer_tree_host_common.h"
25 #include "cc/trees/layer_tree_impl.h"
26 #include "cc/trees/layer_tree_settings.h"
27 #include "cc/trees/proxy.h"
28 #include "ui/gfx/geometry/box_f.h"
29 #include "ui/gfx/geometry/point_conversions.h"
30 #include "ui/gfx/geometry/quad_f.h"
31 #include "ui/gfx/geometry/rect_conversions.h"
32 #include "ui/gfx/geometry/size_conversions.h"
33 #include "ui/gfx/geometry/vector2d_conversions.h"
35 namespace cc {
36 LayerImpl::LayerImpl(LayerTreeImpl* layer_impl, int id)
37 : LayerImpl(layer_impl, id, new LayerImpl::SyncedScrollOffset) {
40 LayerImpl::LayerImpl(LayerTreeImpl* tree_impl,
41 int id,
42 scoped_refptr<SyncedScrollOffset> scroll_offset)
43 : parent_(nullptr),
44 scroll_parent_(nullptr),
45 clip_parent_(nullptr),
46 mask_layer_id_(-1),
47 replica_layer_id_(-1),
48 layer_id_(id),
49 layer_tree_impl_(tree_impl),
50 scroll_offset_(scroll_offset),
51 scroll_clip_layer_(nullptr),
52 should_scroll_on_main_thread_(false),
53 have_wheel_event_handlers_(false),
54 have_scroll_event_handlers_(false),
55 scroll_blocks_on_(SCROLL_BLOCKS_ON_NONE),
56 user_scrollable_horizontal_(true),
57 user_scrollable_vertical_(true),
58 stacking_order_changed_(false),
59 double_sided_(true),
60 should_flatten_transform_(true),
61 should_flatten_transform_from_property_tree_(false),
62 layer_property_changed_(false),
63 masks_to_bounds_(false),
64 contents_opaque_(false),
65 is_root_for_isolated_group_(false),
66 use_parent_backface_visibility_(false),
67 draw_checkerboard_for_missing_tiles_(false),
68 draws_content_(false),
69 hide_layer_and_subtree_(false),
70 transform_is_invertible_(true),
71 is_container_for_fixed_position_layers_(false),
72 background_color_(0),
73 opacity_(1.0),
74 blend_mode_(SkXfermode::kSrcOver_Mode),
75 num_descendants_that_draw_content_(0),
76 transform_tree_index_(-1),
77 opacity_tree_index_(-1),
78 clip_tree_index_(-1),
79 draw_depth_(0.f),
80 needs_push_properties_(false),
81 num_dependents_need_push_properties_(0),
82 sorting_context_id_(0),
83 current_draw_mode_(DRAW_MODE_NONE),
84 frame_timing_requests_dirty_(false) {
85 DCHECK_GT(layer_id_, 0);
86 DCHECK(layer_tree_impl_);
87 layer_tree_impl_->RegisterLayer(this);
88 AnimationRegistrar* registrar = layer_tree_impl_->GetAnimationRegistrar();
89 layer_animation_controller_ =
90 registrar->GetAnimationControllerForId(layer_id_);
91 layer_animation_controller_->AddValueObserver(this);
92 if (IsActive()) {
93 layer_animation_controller_->set_value_provider(this);
94 layer_animation_controller_->set_layer_animation_delegate(this);
96 SetNeedsPushProperties();
99 LayerImpl::~LayerImpl() {
100 DCHECK_EQ(DRAW_MODE_NONE, current_draw_mode_);
102 layer_animation_controller_->RemoveValueObserver(this);
103 layer_animation_controller_->remove_value_provider(this);
104 layer_animation_controller_->remove_layer_animation_delegate(this);
106 if (!copy_requests_.empty() && layer_tree_impl_->IsActiveTree())
107 layer_tree_impl()->RemoveLayerWithCopyOutputRequest(this);
108 layer_tree_impl_->UnregisterLayer(this);
110 TRACE_EVENT_OBJECT_DELETED_WITH_ID(
111 TRACE_DISABLED_BY_DEFAULT("cc.debug"), "cc::LayerImpl", this);
114 void LayerImpl::AddChild(scoped_ptr<LayerImpl> child) {
115 child->SetParent(this);
116 DCHECK_EQ(layer_tree_impl(), child->layer_tree_impl());
117 children_.push_back(child.Pass());
118 layer_tree_impl()->set_needs_update_draw_properties();
121 scoped_ptr<LayerImpl> LayerImpl::RemoveChild(LayerImpl* child) {
122 for (OwnedLayerImplList::iterator it = children_.begin();
123 it != children_.end();
124 ++it) {
125 if (*it == child) {
126 scoped_ptr<LayerImpl> ret = children_.take(it);
127 children_.erase(it);
128 layer_tree_impl()->set_needs_update_draw_properties();
129 return ret.Pass();
132 return nullptr;
135 void LayerImpl::SetParent(LayerImpl* parent) {
136 if (parent_should_know_need_push_properties()) {
137 if (parent_)
138 parent_->RemoveDependentNeedsPushProperties();
139 if (parent)
140 parent->AddDependentNeedsPushProperties();
142 parent_ = parent;
145 void LayerImpl::ClearChildList() {
146 if (children_.empty())
147 return;
149 children_.clear();
150 layer_tree_impl()->set_needs_update_draw_properties();
153 bool LayerImpl::HasAncestor(const LayerImpl* ancestor) const {
154 if (!ancestor)
155 return false;
157 for (const LayerImpl* layer = this; layer; layer = layer->parent()) {
158 if (layer == ancestor)
159 return true;
162 return false;
165 void LayerImpl::SetScrollParent(LayerImpl* parent) {
166 if (scroll_parent_ == parent)
167 return;
169 if (parent)
170 DCHECK_EQ(layer_tree_impl()->LayerById(parent->id()), parent);
172 scroll_parent_ = parent;
173 SetNeedsPushProperties();
176 void LayerImpl::SetDebugInfo(
177 scoped_refptr<base::trace_event::ConvertableToTraceFormat> other) {
178 debug_info_ = other;
179 SetNeedsPushProperties();
182 void LayerImpl::SetScrollChildren(std::set<LayerImpl*>* children) {
183 if (scroll_children_.get() == children)
184 return;
185 scroll_children_.reset(children);
186 SetNeedsPushProperties();
189 void LayerImpl::SetNumDescendantsThatDrawContent(int num_descendants) {
190 if (num_descendants_that_draw_content_ == num_descendants)
191 return;
192 num_descendants_that_draw_content_ = num_descendants;
193 SetNeedsPushProperties();
196 void LayerImpl::SetClipParent(LayerImpl* ancestor) {
197 if (clip_parent_ == ancestor)
198 return;
200 clip_parent_ = ancestor;
201 SetNeedsPushProperties();
204 void LayerImpl::SetClipChildren(std::set<LayerImpl*>* children) {
205 if (clip_children_.get() == children)
206 return;
207 clip_children_.reset(children);
208 SetNeedsPushProperties();
211 void LayerImpl::PassCopyRequests(ScopedPtrVector<CopyOutputRequest>* requests) {
212 if (requests->empty())
213 return;
214 DCHECK(render_surface());
215 bool was_empty = copy_requests_.empty();
216 copy_requests_.insert_and_take(copy_requests_.end(), requests);
217 requests->clear();
219 if (was_empty && layer_tree_impl()->IsActiveTree())
220 layer_tree_impl()->AddLayerWithCopyOutputRequest(this);
221 NoteLayerPropertyChangedForSubtree();
224 void LayerImpl::TakeCopyRequestsAndTransformToTarget(
225 ScopedPtrVector<CopyOutputRequest>* requests) {
226 DCHECK(!copy_requests_.empty());
227 DCHECK(layer_tree_impl()->IsActiveTree());
228 DCHECK_EQ(render_target(), this);
230 size_t first_inserted_request = requests->size();
231 requests->insert_and_take(requests->end(), &copy_requests_);
232 copy_requests_.clear();
234 for (size_t i = first_inserted_request; i < requests->size(); ++i) {
235 CopyOutputRequest* request = requests->at(i);
236 if (!request->has_area())
237 continue;
239 gfx::Rect request_in_layer_space = request->area();
240 gfx::Rect request_in_content_space =
241 LayerRectToContentRect(request_in_layer_space);
242 request->set_area(MathUtil::MapEnclosingClippedRect(
243 draw_properties_.target_space_transform, request_in_content_space));
246 layer_tree_impl()->RemoveLayerWithCopyOutputRequest(this);
247 layer_tree_impl()->set_needs_update_draw_properties();
250 void LayerImpl::ClearRenderSurfaceLayerList() {
251 if (render_surface_)
252 render_surface_->ClearLayerLists();
255 void LayerImpl::PopulateSharedQuadState(SharedQuadState* state) const {
256 state->SetAll(
257 draw_properties_.target_space_transform, draw_properties_.content_bounds,
258 draw_properties_.visible_content_rect, draw_properties_.clip_rect,
259 draw_properties_.is_clipped, draw_properties_.opacity,
260 draw_properties_.blend_mode, sorting_context_id_);
263 void LayerImpl::PopulateScaledSharedQuadState(SharedQuadState* state,
264 float scale) const {
265 gfx::Transform scaled_draw_transform =
266 draw_properties_.target_space_transform;
267 scaled_draw_transform.Scale(SK_MScalar1 / scale, SK_MScalar1 / scale);
268 gfx::Size scaled_content_bounds =
269 gfx::ToCeiledSize(gfx::ScaleSize(bounds(), scale));
270 gfx::Rect scaled_visible_content_rect =
271 gfx::ScaleToEnclosingRect(visible_content_rect(), scale);
272 scaled_visible_content_rect.Intersect(gfx::Rect(scaled_content_bounds));
274 state->SetAll(scaled_draw_transform, scaled_content_bounds,
275 scaled_visible_content_rect, draw_properties().clip_rect,
276 draw_properties().is_clipped, draw_properties().opacity,
277 draw_properties().blend_mode, sorting_context_id_);
280 bool LayerImpl::WillDraw(DrawMode draw_mode,
281 ResourceProvider* resource_provider) {
282 // WillDraw/DidDraw must be matched.
283 DCHECK_NE(DRAW_MODE_NONE, draw_mode);
284 DCHECK_EQ(DRAW_MODE_NONE, current_draw_mode_);
285 current_draw_mode_ = draw_mode;
286 return true;
289 void LayerImpl::DidDraw(ResourceProvider* resource_provider) {
290 DCHECK_NE(DRAW_MODE_NONE, current_draw_mode_);
291 current_draw_mode_ = DRAW_MODE_NONE;
294 bool LayerImpl::ShowDebugBorders() const {
295 return layer_tree_impl()->debug_state().show_debug_borders;
298 void LayerImpl::GetDebugBorderProperties(SkColor* color, float* width) const {
299 if (draws_content_) {
300 *color = DebugColors::ContentLayerBorderColor();
301 *width = DebugColors::ContentLayerBorderWidth(layer_tree_impl());
302 return;
305 if (masks_to_bounds_) {
306 *color = DebugColors::MaskingLayerBorderColor();
307 *width = DebugColors::MaskingLayerBorderWidth(layer_tree_impl());
308 return;
311 *color = DebugColors::ContainerLayerBorderColor();
312 *width = DebugColors::ContainerLayerBorderWidth(layer_tree_impl());
315 void LayerImpl::AppendDebugBorderQuad(
316 RenderPass* render_pass,
317 const gfx::Size& content_bounds,
318 const SharedQuadState* shared_quad_state,
319 AppendQuadsData* append_quads_data) const {
320 SkColor color;
321 float width;
322 GetDebugBorderProperties(&color, &width);
323 AppendDebugBorderQuad(render_pass,
324 content_bounds,
325 shared_quad_state,
326 append_quads_data,
327 color,
328 width);
331 void LayerImpl::AppendDebugBorderQuad(RenderPass* render_pass,
332 const gfx::Size& content_bounds,
333 const SharedQuadState* shared_quad_state,
334 AppendQuadsData* append_quads_data,
335 SkColor color,
336 float width) const {
337 if (!ShowDebugBorders())
338 return;
340 gfx::Rect quad_rect(content_bounds);
341 gfx::Rect visible_quad_rect(quad_rect);
342 DebugBorderDrawQuad* debug_border_quad =
343 render_pass->CreateAndAppendDrawQuad<DebugBorderDrawQuad>();
344 debug_border_quad->SetNew(
345 shared_quad_state, quad_rect, visible_quad_rect, color, width);
346 if (contents_opaque()) {
347 // When opaque, draw a second inner border that is thicker than the outer
348 // border, but more transparent.
349 static const float kFillOpacity = 0.3f;
350 SkColor fill_color = SkColorSetA(
351 color, static_cast<uint8_t>(SkColorGetA(color) * kFillOpacity));
352 float fill_width = width * 3;
353 gfx::Rect fill_rect = quad_rect;
354 fill_rect.Inset(fill_width / 2.f, fill_width / 2.f);
355 if (fill_rect.IsEmpty())
356 return;
357 gfx::Rect visible_fill_rect =
358 gfx::IntersectRects(visible_quad_rect, fill_rect);
359 DebugBorderDrawQuad* fill_quad =
360 render_pass->CreateAndAppendDrawQuad<DebugBorderDrawQuad>();
361 fill_quad->SetNew(shared_quad_state, fill_rect, visible_fill_rect,
362 fill_color, fill_width);
366 bool LayerImpl::HasDelegatedContent() const {
367 return false;
370 bool LayerImpl::HasContributingDelegatedRenderPasses() const {
371 return false;
374 RenderPassId LayerImpl::FirstContributingRenderPassId() const {
375 return RenderPassId(0, 0);
378 RenderPassId LayerImpl::NextContributingRenderPassId(RenderPassId id) const {
379 return RenderPassId(0, 0);
382 void LayerImpl::GetContentsResourceId(ResourceProvider::ResourceId* resource_id,
383 gfx::Size* resource_size) const {
384 NOTREACHED();
385 *resource_id = 0;
388 gfx::Vector2dF LayerImpl::ScrollBy(const gfx::Vector2dF& scroll) {
389 gfx::ScrollOffset adjusted_scroll(scroll);
390 if (layer_tree_impl()->settings().use_pinch_virtual_viewport) {
391 if (!user_scrollable_horizontal_)
392 adjusted_scroll.set_x(0);
393 if (!user_scrollable_vertical_)
394 adjusted_scroll.set_y(0);
396 DCHECK(scrollable());
397 gfx::ScrollOffset old_offset = CurrentScrollOffset();
398 gfx::ScrollOffset new_offset =
399 ClampScrollOffsetToLimits(old_offset + adjusted_scroll);
400 SetCurrentScrollOffset(new_offset);
402 gfx::ScrollOffset unscrolled =
403 old_offset + gfx::ScrollOffset(scroll) - new_offset;
404 return gfx::Vector2dF(unscrolled.x(), unscrolled.y());
407 void LayerImpl::SetScrollClipLayer(int scroll_clip_layer_id) {
408 scroll_clip_layer_ = layer_tree_impl()->LayerById(scroll_clip_layer_id);
411 bool LayerImpl::user_scrollable(ScrollbarOrientation orientation) const {
412 return (orientation == HORIZONTAL) ? user_scrollable_horizontal_
413 : user_scrollable_vertical_;
416 void LayerImpl::ApplySentScrollDeltasFromAbortedCommit() {
417 DCHECK(layer_tree_impl()->IsActiveTree());
418 scroll_offset_->AbortCommit();
421 InputHandler::ScrollStatus LayerImpl::TryScroll(
422 const gfx::PointF& screen_space_point,
423 InputHandler::ScrollInputType type,
424 ScrollBlocksOn effective_block_mode) const {
425 if (should_scroll_on_main_thread()) {
426 TRACE_EVENT0("cc", "LayerImpl::TryScroll: Failed ShouldScrollOnMainThread");
427 return InputHandler::SCROLL_ON_MAIN_THREAD;
430 if (!screen_space_transform().IsInvertible()) {
431 TRACE_EVENT0("cc", "LayerImpl::TryScroll: Ignored NonInvertibleTransform");
432 return InputHandler::SCROLL_IGNORED;
435 if (!non_fast_scrollable_region().IsEmpty()) {
436 bool clipped = false;
437 gfx::Transform inverse_screen_space_transform(
438 gfx::Transform::kSkipInitialization);
439 if (!screen_space_transform().GetInverse(&inverse_screen_space_transform)) {
440 // TODO(shawnsingh): We shouldn't be applying a projection if screen space
441 // transform is uninvertible here. Perhaps we should be returning
442 // SCROLL_ON_MAIN_THREAD in this case?
445 gfx::PointF hit_test_point_in_content_space =
446 MathUtil::ProjectPoint(inverse_screen_space_transform,
447 screen_space_point,
448 &clipped);
449 gfx::PointF hit_test_point_in_layer_space =
450 gfx::ScalePoint(hit_test_point_in_content_space,
451 1.f / contents_scale_x(),
452 1.f / contents_scale_y());
453 if (!clipped &&
454 non_fast_scrollable_region().Contains(
455 gfx::ToRoundedPoint(hit_test_point_in_layer_space))) {
456 TRACE_EVENT0("cc",
457 "LayerImpl::tryScroll: Failed NonFastScrollableRegion");
458 return InputHandler::SCROLL_ON_MAIN_THREAD;
462 if (have_scroll_event_handlers() &&
463 effective_block_mode & SCROLL_BLOCKS_ON_SCROLL_EVENT) {
464 TRACE_EVENT0("cc", "LayerImpl::tryScroll: Failed ScrollEventHandlers");
465 return InputHandler::SCROLL_ON_MAIN_THREAD;
468 if (type == InputHandler::WHEEL && have_wheel_event_handlers() &&
469 effective_block_mode & SCROLL_BLOCKS_ON_WHEEL_EVENT) {
470 TRACE_EVENT0("cc", "LayerImpl::tryScroll: Failed WheelEventHandlers");
471 return InputHandler::SCROLL_ON_MAIN_THREAD;
474 if (!scrollable()) {
475 TRACE_EVENT0("cc", "LayerImpl::tryScroll: Ignored not scrollable");
476 return InputHandler::SCROLL_IGNORED;
479 gfx::ScrollOffset max_scroll_offset = MaxScrollOffset();
480 if (max_scroll_offset.x() <= 0 && max_scroll_offset.y() <= 0) {
481 TRACE_EVENT0("cc",
482 "LayerImpl::tryScroll: Ignored. Technically scrollable,"
483 " but has no affordance in either direction.");
484 return InputHandler::SCROLL_IGNORED;
487 return InputHandler::SCROLL_STARTED;
490 gfx::Rect LayerImpl::LayerRectToContentRect(
491 const gfx::RectF& layer_rect) const {
492 gfx::RectF content_rect =
493 gfx::ScaleRect(layer_rect, contents_scale_x(), contents_scale_y());
494 // Intersect with content rect to avoid the extra pixel because for some
495 // values x and y, ceil((x / y) * y) may be x + 1.
496 content_rect.Intersect(gfx::Rect(content_bounds()));
497 return gfx::ToEnclosingRect(content_rect);
500 skia::RefPtr<SkPicture> LayerImpl::GetPicture() {
501 return skia::RefPtr<SkPicture>();
504 scoped_ptr<LayerImpl> LayerImpl::CreateLayerImpl(LayerTreeImpl* tree_impl) {
505 return LayerImpl::Create(tree_impl, layer_id_, scroll_offset_);
508 void LayerImpl::PushPropertiesTo(LayerImpl* layer) {
509 layer->SetTransformOrigin(transform_origin_);
510 layer->SetBackgroundColor(background_color_);
511 layer->SetBounds(bounds_);
512 layer->SetContentBounds(content_bounds());
513 layer->SetContentsScale(contents_scale_x(), contents_scale_y());
514 layer->SetDoubleSided(double_sided_);
515 layer->SetDrawCheckerboardForMissingTiles(
516 draw_checkerboard_for_missing_tiles_);
517 layer->SetDrawsContent(DrawsContent());
518 layer->SetHideLayerAndSubtree(hide_layer_and_subtree_);
519 layer->SetHasRenderSurface(!!render_surface() || layer->HasCopyRequest());
520 layer->SetFilters(filters());
521 layer->SetBackgroundFilters(background_filters());
522 layer->SetMasksToBounds(masks_to_bounds_);
523 layer->SetShouldScrollOnMainThread(should_scroll_on_main_thread_);
524 layer->SetHaveWheelEventHandlers(have_wheel_event_handlers_);
525 layer->SetHaveScrollEventHandlers(have_scroll_event_handlers_);
526 layer->SetScrollBlocksOn(scroll_blocks_on_);
527 layer->SetNonFastScrollableRegion(non_fast_scrollable_region_);
528 layer->SetTouchEventHandlerRegion(touch_event_handler_region_);
529 layer->SetContentsOpaque(contents_opaque_);
530 layer->SetOpacity(opacity_);
531 layer->SetBlendMode(blend_mode_);
532 layer->SetIsRootForIsolatedGroup(is_root_for_isolated_group_);
533 layer->SetPosition(position_);
534 layer->SetIsContainerForFixedPositionLayers(
535 is_container_for_fixed_position_layers_);
536 layer->SetPositionConstraint(position_constraint_);
537 layer->SetShouldFlattenTransform(should_flatten_transform_);
538 layer->set_should_flatten_transform_from_property_tree(
539 should_flatten_transform_from_property_tree_);
540 layer->SetUseParentBackfaceVisibility(use_parent_backface_visibility_);
541 layer->SetTransformAndInvertibility(transform_, transform_is_invertible_);
543 layer->SetScrollClipLayer(scroll_clip_layer_ ? scroll_clip_layer_->id()
544 : Layer::INVALID_ID);
545 layer->set_user_scrollable_horizontal(user_scrollable_horizontal_);
546 layer->set_user_scrollable_vertical(user_scrollable_vertical_);
548 layer->SetScrollCompensationAdjustment(scroll_compensation_adjustment_);
550 layer->PushScrollOffset(nullptr);
552 layer->Set3dSortingContextId(sorting_context_id_);
553 layer->SetNumDescendantsThatDrawContent(num_descendants_that_draw_content_);
555 layer->set_transform_tree_index(transform_tree_index_);
556 layer->set_opacity_tree_index(opacity_tree_index_);
557 layer->set_clip_tree_index(clip_tree_index_);
558 layer->set_offset_to_transform_parent(offset_to_transform_parent_);
560 LayerImpl* scroll_parent = nullptr;
561 if (scroll_parent_) {
562 scroll_parent = layer->layer_tree_impl()->LayerById(scroll_parent_->id());
563 DCHECK(scroll_parent);
566 layer->SetScrollParent(scroll_parent);
567 if (scroll_children_) {
568 std::set<LayerImpl*>* scroll_children = new std::set<LayerImpl*>;
569 for (std::set<LayerImpl*>::iterator it = scroll_children_->begin();
570 it != scroll_children_->end();
571 ++it) {
572 DCHECK_EQ((*it)->scroll_parent(), this);
573 LayerImpl* scroll_child =
574 layer->layer_tree_impl()->LayerById((*it)->id());
575 DCHECK(scroll_child);
576 scroll_children->insert(scroll_child);
578 layer->SetScrollChildren(scroll_children);
579 } else {
580 layer->SetScrollChildren(nullptr);
583 LayerImpl* clip_parent = nullptr;
584 if (clip_parent_) {
585 clip_parent = layer->layer_tree_impl()->LayerById(
586 clip_parent_->id());
587 DCHECK(clip_parent);
590 layer->SetClipParent(clip_parent);
591 if (clip_children_) {
592 std::set<LayerImpl*>* clip_children = new std::set<LayerImpl*>;
593 for (std::set<LayerImpl*>::iterator it = clip_children_->begin();
594 it != clip_children_->end(); ++it)
595 clip_children->insert(layer->layer_tree_impl()->LayerById((*it)->id()));
596 layer->SetClipChildren(clip_children);
597 } else {
598 layer->SetClipChildren(nullptr);
601 layer->PassCopyRequests(&copy_requests_);
603 // If the main thread commits multiple times before the impl thread actually
604 // draws, then damage tracking will become incorrect if we simply clobber the
605 // update_rect here. The LayerImpl's update_rect needs to accumulate (i.e.
606 // union) any update changes that have occurred on the main thread.
607 update_rect_.Union(layer->update_rect());
608 layer->SetUpdateRect(update_rect_);
610 layer->SetStackingOrderChanged(stacking_order_changed_);
611 layer->SetDebugInfo(debug_info_);
613 if (frame_timing_requests_dirty_) {
614 layer->PassFrameTimingRequests(&frame_timing_requests_);
615 frame_timing_requests_dirty_ = false;
618 // Reset any state that should be cleared for the next update.
619 stacking_order_changed_ = false;
620 update_rect_ = gfx::Rect();
621 needs_push_properties_ = false;
622 num_dependents_need_push_properties_ = 0;
625 gfx::Vector2dF LayerImpl::FixedContainerSizeDelta() const {
626 if (!scroll_clip_layer_)
627 return gfx::Vector2dF();
629 gfx::Vector2dF delta_from_scroll = scroll_clip_layer_->bounds_delta();
631 // In virtual-viewport mode, we don't need to compensate for pinch zoom or
632 // scale since the fixed container is the outer viewport, which sits below
633 // the page scale.
634 if (layer_tree_impl()->settings().use_pinch_virtual_viewport)
635 return delta_from_scroll;
637 float scale_delta = layer_tree_impl()->page_scale_delta();
638 float scale = layer_tree_impl()->current_page_scale_factor() /
639 layer_tree_impl()->page_scale_delta();
641 delta_from_scroll.Scale(1.f / scale);
643 // The delta-from-pinch component requires some explanation: A viewport of
644 // size (w,h) will appear to be size (w/s,h/s) under scale s in the content
645 // space. If s -> s' on the impl thread, where s' = s * ds, then the apparent
646 // viewport size change in the content space due to ds is:
648 // (w/s',h/s') - (w/s,h/s) = (w,h)(1/s' - 1/s) = (w,h)(1 - ds)/(s ds)
650 gfx::Vector2dF delta_from_pinch =
651 gfx::Rect(scroll_clip_layer_->bounds()).bottom_right() - gfx::PointF();
652 delta_from_pinch.Scale((1.f - scale_delta) / (scale * scale_delta));
654 return delta_from_scroll + delta_from_pinch;
657 base::DictionaryValue* LayerImpl::LayerTreeAsJson() const {
658 base::DictionaryValue* result = new base::DictionaryValue;
659 result->SetString("LayerType", LayerTypeAsString());
661 base::ListValue* list = new base::ListValue;
662 list->AppendInteger(bounds().width());
663 list->AppendInteger(bounds().height());
664 result->Set("Bounds", list);
666 list = new base::ListValue;
667 list->AppendDouble(position_.x());
668 list->AppendDouble(position_.y());
669 result->Set("Position", list);
671 const gfx::Transform& gfx_transform = draw_properties_.target_space_transform;
672 double transform[16];
673 gfx_transform.matrix().asColMajord(transform);
674 list = new base::ListValue;
675 for (int i = 0; i < 16; ++i)
676 list->AppendDouble(transform[i]);
677 result->Set("DrawTransform", list);
679 result->SetBoolean("DrawsContent", draws_content_);
680 result->SetBoolean("Is3dSorted", Is3dSorted());
681 result->SetDouble("OPACITY", opacity());
682 result->SetBoolean("ContentsOpaque", contents_opaque_);
684 if (scrollable())
685 result->SetBoolean("Scrollable", true);
687 if (have_wheel_event_handlers_)
688 result->SetBoolean("WheelHandler", have_wheel_event_handlers_);
689 if (have_scroll_event_handlers_)
690 result->SetBoolean("ScrollHandler", have_scroll_event_handlers_);
691 if (!touch_event_handler_region_.IsEmpty()) {
692 scoped_ptr<base::Value> region = touch_event_handler_region_.AsValue();
693 result->Set("TouchRegion", region.release());
696 if (scroll_blocks_on_) {
697 list = new base::ListValue;
698 if (scroll_blocks_on_ & SCROLL_BLOCKS_ON_START_TOUCH)
699 list->AppendString("StartTouch");
700 if (scroll_blocks_on_ & SCROLL_BLOCKS_ON_WHEEL_EVENT)
701 list->AppendString("WheelEvent");
702 if (scroll_blocks_on_ & SCROLL_BLOCKS_ON_SCROLL_EVENT)
703 list->AppendString("ScrollEvent");
704 result->Set("ScrollBlocksOn", list);
707 list = new base::ListValue;
708 for (size_t i = 0; i < children_.size(); ++i)
709 list->Append(children_[i]->LayerTreeAsJson());
710 result->Set("Children", list);
712 return result;
715 void LayerImpl::SetStackingOrderChanged(bool stacking_order_changed) {
716 if (stacking_order_changed) {
717 stacking_order_changed_ = true;
718 NoteLayerPropertyChangedForSubtree();
722 void LayerImpl::NoteLayerPropertyChanged() {
723 layer_property_changed_ = true;
724 layer_tree_impl()->set_needs_update_draw_properties();
725 SetNeedsPushProperties();
728 void LayerImpl::NoteLayerPropertyChangedForSubtree() {
729 layer_property_changed_ = true;
730 layer_tree_impl()->set_needs_update_draw_properties();
731 for (size_t i = 0; i < children_.size(); ++i)
732 children_[i]->NoteLayerPropertyChangedForDescendantsInternal();
733 SetNeedsPushProperties();
736 void LayerImpl::NoteLayerPropertyChangedForDescendantsInternal() {
737 layer_property_changed_ = true;
738 for (size_t i = 0; i < children_.size(); ++i)
739 children_[i]->NoteLayerPropertyChangedForDescendantsInternal();
742 void LayerImpl::NoteLayerPropertyChangedForDescendants() {
743 layer_tree_impl()->set_needs_update_draw_properties();
744 for (size_t i = 0; i < children_.size(); ++i)
745 children_[i]->NoteLayerPropertyChangedForDescendantsInternal();
746 SetNeedsPushProperties();
749 #if DCHECK_IS_ON()
750 // Verify that the resource id is valid.
751 static ResourceProvider::ResourceId ValidateResource(
752 const ResourceProvider* provider,
753 ResourceProvider::ResourceId id) {
754 provider->ValidateResource(id);
755 return id;
757 #endif
759 void LayerImpl::ValidateQuadResourcesInternal(DrawQuad* quad) const {
760 #if DCHECK_IS_ON()
761 quad->IterateResources(
762 base::Bind(&ValidateResource, layer_tree_impl_->resource_provider()));
763 #endif
766 const char* LayerImpl::LayerTypeAsString() const {
767 return "cc::LayerImpl";
770 void LayerImpl::ResetAllChangeTrackingForSubtree() {
771 layer_property_changed_ = false;
773 update_rect_ = gfx::Rect();
774 damage_rect_ = gfx::RectF();
776 if (render_surface_)
777 render_surface_->ResetPropertyChangedFlag();
779 if (mask_layer_)
780 mask_layer_->ResetAllChangeTrackingForSubtree();
782 if (replica_layer_) {
783 // This also resets the replica mask, if it exists.
784 replica_layer_->ResetAllChangeTrackingForSubtree();
787 for (size_t i = 0; i < children_.size(); ++i)
788 children_[i]->ResetAllChangeTrackingForSubtree();
790 needs_push_properties_ = false;
791 num_dependents_need_push_properties_ = 0;
794 gfx::ScrollOffset LayerImpl::ScrollOffsetForAnimation() const {
795 return CurrentScrollOffset();
798 void LayerImpl::OnFilterAnimated(const FilterOperations& filters) {
799 SetFilters(filters);
802 void LayerImpl::OnOpacityAnimated(float opacity) {
803 SetOpacity(opacity);
806 void LayerImpl::OnTransformAnimated(const gfx::Transform& transform) {
807 SetTransform(transform);
810 void LayerImpl::OnScrollOffsetAnimated(const gfx::ScrollOffset& scroll_offset) {
811 // Only layers in the active tree should need to do anything here, since
812 // layers in the pending tree will find out about these changes as a
813 // result of the shared SyncedProperty.
814 if (!IsActive())
815 return;
817 SetCurrentScrollOffset(scroll_offset);
819 layer_tree_impl_->DidAnimateScrollOffset();
822 void LayerImpl::OnAnimationWaitingForDeletion() {}
824 bool LayerImpl::IsActive() const {
825 return layer_tree_impl_->IsActiveTree();
828 gfx::Size LayerImpl::bounds() const {
829 gfx::Vector2d delta = gfx::ToCeiledVector2d(bounds_delta_);
830 return gfx::Size(bounds_.width() + delta.x(),
831 bounds_.height() + delta.y());
834 gfx::SizeF LayerImpl::BoundsForScrolling() const {
835 return gfx::SizeF(bounds_.width() + bounds_delta_.x(),
836 bounds_.height() + bounds_delta_.y());
839 void LayerImpl::SetBounds(const gfx::Size& bounds) {
840 if (bounds_ == bounds)
841 return;
843 bounds_ = bounds;
845 ScrollbarParametersDidChange(true);
846 if (masks_to_bounds())
847 NoteLayerPropertyChangedForSubtree();
848 else
849 NoteLayerPropertyChanged();
852 void LayerImpl::SetBoundsDelta(const gfx::Vector2dF& bounds_delta) {
853 if (bounds_delta_ == bounds_delta)
854 return;
856 bounds_delta_ = bounds_delta;
858 ScrollbarParametersDidChange(true);
859 if (masks_to_bounds())
860 NoteLayerPropertyChangedForSubtree();
861 else
862 NoteLayerPropertyChanged();
865 void LayerImpl::SetMaskLayer(scoped_ptr<LayerImpl> mask_layer) {
866 int new_layer_id = mask_layer ? mask_layer->id() : -1;
868 if (mask_layer) {
869 DCHECK_EQ(layer_tree_impl(), mask_layer->layer_tree_impl());
870 DCHECK_NE(new_layer_id, mask_layer_id_);
871 } else if (new_layer_id == mask_layer_id_) {
872 return;
875 mask_layer_ = mask_layer.Pass();
876 mask_layer_id_ = new_layer_id;
877 if (mask_layer_)
878 mask_layer_->SetParent(this);
879 NoteLayerPropertyChangedForSubtree();
882 scoped_ptr<LayerImpl> LayerImpl::TakeMaskLayer() {
883 mask_layer_id_ = -1;
884 return mask_layer_.Pass();
887 void LayerImpl::SetReplicaLayer(scoped_ptr<LayerImpl> replica_layer) {
888 int new_layer_id = replica_layer ? replica_layer->id() : -1;
890 if (replica_layer) {
891 DCHECK_EQ(layer_tree_impl(), replica_layer->layer_tree_impl());
892 DCHECK_NE(new_layer_id, replica_layer_id_);
893 } else if (new_layer_id == replica_layer_id_) {
894 return;
897 replica_layer_ = replica_layer.Pass();
898 replica_layer_id_ = new_layer_id;
899 if (replica_layer_)
900 replica_layer_->SetParent(this);
901 NoteLayerPropertyChangedForSubtree();
904 scoped_ptr<LayerImpl> LayerImpl::TakeReplicaLayer() {
905 replica_layer_id_ = -1;
906 return replica_layer_.Pass();
909 ScrollbarLayerImplBase* LayerImpl::ToScrollbarLayer() {
910 return nullptr;
913 void LayerImpl::SetDrawsContent(bool draws_content) {
914 if (draws_content_ == draws_content)
915 return;
917 draws_content_ = draws_content;
918 NoteLayerPropertyChanged();
921 void LayerImpl::SetHideLayerAndSubtree(bool hide) {
922 if (hide_layer_and_subtree_ == hide)
923 return;
925 hide_layer_and_subtree_ = hide;
926 NoteLayerPropertyChangedForSubtree();
929 void LayerImpl::SetTransformOrigin(const gfx::Point3F& transform_origin) {
930 if (transform_origin_ == transform_origin)
931 return;
932 transform_origin_ = transform_origin;
933 NoteLayerPropertyChangedForSubtree();
936 void LayerImpl::SetBackgroundColor(SkColor background_color) {
937 if (background_color_ == background_color)
938 return;
940 background_color_ = background_color;
941 NoteLayerPropertyChanged();
944 SkColor LayerImpl::SafeOpaqueBackgroundColor() const {
945 SkColor color = background_color();
946 if (SkColorGetA(color) == 255 && !contents_opaque()) {
947 color = SK_ColorTRANSPARENT;
948 } else if (SkColorGetA(color) != 255 && contents_opaque()) {
949 for (const LayerImpl* layer = parent(); layer;
950 layer = layer->parent()) {
951 color = layer->background_color();
952 if (SkColorGetA(color) == 255)
953 break;
955 if (SkColorGetA(color) != 255)
956 color = layer_tree_impl()->background_color();
957 if (SkColorGetA(color) != 255)
958 color = SkColorSetA(color, 255);
960 return color;
963 void LayerImpl::SetFilters(const FilterOperations& filters) {
964 if (filters_ == filters)
965 return;
967 filters_ = filters;
968 NoteLayerPropertyChangedForSubtree();
971 bool LayerImpl::FilterIsAnimating() const {
972 return layer_animation_controller_->IsAnimatingProperty(Animation::FILTER);
975 bool LayerImpl::FilterIsAnimatingOnImplOnly() const {
976 Animation* filter_animation =
977 layer_animation_controller_->GetAnimation(Animation::FILTER);
978 return filter_animation && filter_animation->is_impl_only();
981 void LayerImpl::SetBackgroundFilters(
982 const FilterOperations& filters) {
983 if (background_filters_ == filters)
984 return;
986 background_filters_ = filters;
987 NoteLayerPropertyChanged();
990 void LayerImpl::SetMasksToBounds(bool masks_to_bounds) {
991 if (masks_to_bounds_ == masks_to_bounds)
992 return;
994 masks_to_bounds_ = masks_to_bounds;
995 NoteLayerPropertyChangedForSubtree();
998 void LayerImpl::SetContentsOpaque(bool opaque) {
999 if (contents_opaque_ == opaque)
1000 return;
1002 contents_opaque_ = opaque;
1003 NoteLayerPropertyChangedForSubtree();
1006 void LayerImpl::SetOpacity(float opacity) {
1007 if (opacity_ == opacity)
1008 return;
1010 opacity_ = opacity;
1011 NoteLayerPropertyChangedForSubtree();
1014 bool LayerImpl::OpacityIsAnimating() const {
1015 return layer_animation_controller_->IsAnimatingProperty(Animation::OPACITY);
1018 bool LayerImpl::OpacityIsAnimatingOnImplOnly() const {
1019 Animation* opacity_animation =
1020 layer_animation_controller_->GetAnimation(Animation::OPACITY);
1021 return opacity_animation && opacity_animation->is_impl_only();
1024 void LayerImpl::SetBlendMode(SkXfermode::Mode blend_mode) {
1025 if (blend_mode_ == blend_mode)
1026 return;
1028 blend_mode_ = blend_mode;
1029 NoteLayerPropertyChangedForSubtree();
1032 void LayerImpl::SetIsRootForIsolatedGroup(bool root) {
1033 if (is_root_for_isolated_group_ == root)
1034 return;
1036 is_root_for_isolated_group_ = root;
1037 SetNeedsPushProperties();
1040 void LayerImpl::SetPosition(const gfx::PointF& position) {
1041 if (position_ == position)
1042 return;
1044 position_ = position;
1045 NoteLayerPropertyChangedForSubtree();
1048 void LayerImpl::SetShouldFlattenTransform(bool flatten) {
1049 if (should_flatten_transform_ == flatten)
1050 return;
1052 should_flatten_transform_ = flatten;
1053 NoteLayerPropertyChangedForSubtree();
1056 void LayerImpl::Set3dSortingContextId(int id) {
1057 if (id == sorting_context_id_)
1058 return;
1059 sorting_context_id_ = id;
1060 NoteLayerPropertyChangedForSubtree();
1063 void LayerImpl::PassFrameTimingRequests(
1064 std::vector<FrameTimingRequest>* requests) {
1065 frame_timing_requests_.swap(*requests);
1066 frame_timing_requests_dirty_ = true;
1067 SetNeedsPushProperties();
1070 void LayerImpl::GatherFrameTimingRequestIds(std::vector<int64_t>* request_ids) {
1071 for (const auto& request : frame_timing_requests_)
1072 request_ids->push_back(request.id());
1075 void LayerImpl::SetTransform(const gfx::Transform& transform) {
1076 if (transform_ == transform)
1077 return;
1079 transform_ = transform;
1080 transform_is_invertible_ = transform_.IsInvertible();
1081 NoteLayerPropertyChangedForSubtree();
1084 void LayerImpl::SetTransformAndInvertibility(const gfx::Transform& transform,
1085 bool transform_is_invertible) {
1086 if (transform_ == transform) {
1087 DCHECK(transform_is_invertible_ == transform_is_invertible)
1088 << "Can't change invertibility if transform is unchanged";
1089 return;
1091 transform_ = transform;
1092 transform_is_invertible_ = transform_is_invertible;
1093 NoteLayerPropertyChangedForSubtree();
1096 bool LayerImpl::TransformIsAnimating() const {
1097 return layer_animation_controller_->IsAnimatingProperty(Animation::TRANSFORM);
1100 bool LayerImpl::TransformIsAnimatingOnImplOnly() const {
1101 Animation* transform_animation =
1102 layer_animation_controller_->GetAnimation(Animation::TRANSFORM);
1103 return transform_animation && transform_animation->is_impl_only();
1106 void LayerImpl::SetUpdateRect(const gfx::Rect& update_rect) {
1107 update_rect_ = update_rect;
1108 SetNeedsPushProperties();
1111 void LayerImpl::AddDamageRect(const gfx::RectF& damage_rect) {
1112 damage_rect_ = gfx::UnionRects(damage_rect_, damage_rect);
1115 void LayerImpl::SetContentBounds(const gfx::Size& content_bounds) {
1116 if (this->content_bounds() == content_bounds)
1117 return;
1119 draw_properties_.content_bounds = content_bounds;
1120 NoteLayerPropertyChanged();
1123 void LayerImpl::SetContentsScale(float contents_scale_x,
1124 float contents_scale_y) {
1125 if (this->contents_scale_x() == contents_scale_x &&
1126 this->contents_scale_y() == contents_scale_y)
1127 return;
1129 draw_properties_.contents_scale_x = contents_scale_x;
1130 draw_properties_.contents_scale_y = contents_scale_y;
1131 NoteLayerPropertyChanged();
1134 bool LayerImpl::IsExternalFlingActive() const {
1135 return layer_tree_impl_->IsExternalFlingActive();
1138 void LayerImpl::SetCurrentScrollOffset(const gfx::ScrollOffset& scroll_offset) {
1139 DCHECK(IsActive());
1140 if (scroll_offset_->SetCurrent(scroll_offset))
1141 DidUpdateScrollOffset(false);
1144 void LayerImpl::SetCurrentScrollOffsetFromDelegate(
1145 const gfx::ScrollOffset& scroll_offset) {
1146 DCHECK(IsActive());
1147 if (scroll_offset_->SetCurrent(scroll_offset))
1148 DidUpdateScrollOffset(true);
1151 void LayerImpl::PushScrollOffsetFromMainThread(
1152 const gfx::ScrollOffset& scroll_offset) {
1153 PushScrollOffset(&scroll_offset);
1156 void LayerImpl::PushScrollOffsetFromMainThreadAndClobberActiveValue(
1157 const gfx::ScrollOffset& scroll_offset) {
1158 scroll_offset_->set_clobber_active_value();
1159 PushScrollOffset(&scroll_offset);
1162 gfx::ScrollOffset LayerImpl::PullDeltaForMainThread() {
1163 // TODO(miletus): Remove all this temporary flooring machinery when
1164 // Blink fully supports fractional scrolls.
1165 gfx::ScrollOffset current_offset = CurrentScrollOffset();
1166 gfx::Vector2dF current_delta = ScrollDelta();
1167 gfx::Vector2dF floored_delta(floor(current_delta.x()),
1168 floor(current_delta.y()));
1169 gfx::Vector2dF diff_delta = floored_delta - current_delta;
1170 gfx::ScrollOffset tmp_offset = ScrollOffsetWithDelta(current_offset,
1171 diff_delta);
1172 scroll_offset_->SetCurrent(tmp_offset);
1173 gfx::ScrollOffset delta = scroll_offset_->PullDeltaForMainThread();
1174 scroll_offset_->SetCurrent(current_offset);
1175 return delta;
1178 gfx::ScrollOffset LayerImpl::CurrentScrollOffset() const {
1179 return scroll_offset_->Current(IsActive());
1182 gfx::Vector2dF LayerImpl::ScrollDelta() const {
1183 if (IsActive())
1184 return gfx::Vector2dF(scroll_offset_->Delta().x(),
1185 scroll_offset_->Delta().y());
1186 else
1187 return gfx::Vector2dF(scroll_offset_->PendingDelta().get().x(),
1188 scroll_offset_->PendingDelta().get().y());
1191 void LayerImpl::SetScrollDelta(const gfx::Vector2dF& delta) {
1192 DCHECK(IsActive());
1193 SetCurrentScrollOffset(scroll_offset_->ActiveBase() +
1194 gfx::ScrollOffset(delta));
1197 gfx::ScrollOffset LayerImpl::BaseScrollOffset() const {
1198 if (IsActive())
1199 return scroll_offset_->ActiveBase();
1200 else
1201 return scroll_offset_->PendingBase();
1204 void LayerImpl::PushScrollOffset(const gfx::ScrollOffset* scroll_offset) {
1205 DCHECK(scroll_offset || IsActive());
1206 bool changed = false;
1207 if (scroll_offset) {
1208 DCHECK(!IsActive() || !layer_tree_impl_->FindPendingTreeLayerById(id()));
1209 changed |= scroll_offset_->PushFromMainThread(*scroll_offset);
1211 if (IsActive()) {
1212 changed |= scroll_offset_->PushPendingToActive();
1215 if (changed)
1216 DidUpdateScrollOffset(false);
1219 void LayerImpl::DidUpdateScrollOffset(bool is_from_root_delegate) {
1220 if (!is_from_root_delegate)
1221 layer_tree_impl()->DidUpdateScrollOffset(id());
1222 NoteLayerPropertyChangedForSubtree();
1223 ScrollbarParametersDidChange(false);
1226 void LayerImpl::SetDoubleSided(bool double_sided) {
1227 if (double_sided_ == double_sided)
1228 return;
1230 double_sided_ = double_sided;
1231 NoteLayerPropertyChangedForSubtree();
1234 SimpleEnclosedRegion LayerImpl::VisibleContentOpaqueRegion() const {
1235 if (contents_opaque())
1236 return SimpleEnclosedRegion(visible_content_rect());
1237 return SimpleEnclosedRegion();
1240 void LayerImpl::DidBeginTracing() {}
1242 void LayerImpl::ReleaseResources() {}
1244 void LayerImpl::RecreateResources() {
1247 gfx::ScrollOffset LayerImpl::MaxScrollOffset() const {
1248 if (!scroll_clip_layer_ || bounds().IsEmpty())
1249 return gfx::ScrollOffset();
1251 LayerImpl const* page_scale_layer = layer_tree_impl()->page_scale_layer();
1252 DCHECK(this != page_scale_layer);
1253 DCHECK(this != layer_tree_impl()->InnerViewportScrollLayer() ||
1254 IsContainerForFixedPositionLayers());
1256 float scale_factor = 1.f;
1257 for (LayerImpl const* current_layer = this;
1258 current_layer != scroll_clip_layer_->parent();
1259 current_layer = current_layer->parent()) {
1260 if (current_layer == page_scale_layer)
1261 scale_factor = layer_tree_impl()->current_page_scale_factor();
1264 gfx::SizeF scaled_scroll_bounds =
1265 gfx::ToFlooredSize(gfx::ScaleSize(BoundsForScrolling(), scale_factor));
1266 scaled_scroll_bounds = gfx::ToFlooredSize(scaled_scroll_bounds);
1268 gfx::ScrollOffset max_offset(
1269 scaled_scroll_bounds.width() - scroll_clip_layer_->bounds().width(),
1270 scaled_scroll_bounds.height() - scroll_clip_layer_->bounds().height());
1271 // We need the final scroll offset to be in CSS coords.
1272 max_offset.Scale(1 / scale_factor);
1273 max_offset.SetToMax(gfx::ScrollOffset());
1274 return max_offset;
1277 gfx::ScrollOffset LayerImpl::ClampScrollOffsetToLimits(
1278 gfx::ScrollOffset offset) const {
1279 offset.SetToMin(MaxScrollOffset());
1280 offset.SetToMax(gfx::ScrollOffset());
1281 return offset;
1284 gfx::Vector2dF LayerImpl::ClampScrollToMaxScrollOffset() {
1285 gfx::ScrollOffset old_offset = CurrentScrollOffset();
1286 gfx::ScrollOffset clamped_offset = ClampScrollOffsetToLimits(old_offset);
1287 gfx::Vector2dF delta = clamped_offset.DeltaFrom(old_offset);
1288 if (!delta.IsZero())
1289 ScrollBy(delta);
1290 return delta;
1293 void LayerImpl::SetScrollbarPosition(ScrollbarLayerImplBase* scrollbar_layer,
1294 LayerImpl* scrollbar_clip_layer,
1295 bool on_resize) const {
1296 DCHECK(scrollbar_layer);
1297 LayerImpl* page_scale_layer = layer_tree_impl()->page_scale_layer();
1299 DCHECK(this != page_scale_layer);
1300 DCHECK(scrollbar_clip_layer);
1301 gfx::RectF clip_rect(gfx::PointF(),
1302 scrollbar_clip_layer->BoundsForScrolling());
1304 // See comment in MaxScrollOffset() regarding the use of the content layer
1305 // bounds here.
1306 gfx::RectF scroll_rect(gfx::PointF(), BoundsForScrolling());
1308 if (scroll_rect.size().IsEmpty())
1309 return;
1311 gfx::ScrollOffset current_offset;
1312 for (LayerImpl const* current_layer = this;
1313 current_layer != scrollbar_clip_layer->parent();
1314 current_layer = current_layer->parent()) {
1315 current_offset += current_layer->CurrentScrollOffset();
1316 if (current_layer == page_scale_layer) {
1317 float scale_factor = layer_tree_impl()->current_page_scale_factor();
1318 current_offset.Scale(scale_factor);
1319 scroll_rect.Scale(scale_factor);
1323 bool scrollbar_needs_animation = false;
1324 scrollbar_needs_animation |= scrollbar_layer->SetVerticalAdjust(
1325 scrollbar_clip_layer->bounds_delta().y());
1326 if (scrollbar_layer->orientation() == HORIZONTAL) {
1327 float visible_ratio = clip_rect.width() / scroll_rect.width();
1328 scrollbar_needs_animation |=
1329 scrollbar_layer->SetCurrentPos(current_offset.x());
1330 scrollbar_needs_animation |=
1331 scrollbar_layer->SetMaximum(scroll_rect.width() - clip_rect.width());
1332 scrollbar_needs_animation |=
1333 scrollbar_layer->SetVisibleToTotalLengthRatio(visible_ratio);
1334 } else {
1335 float visible_ratio = clip_rect.height() / scroll_rect.height();
1336 bool y_offset_did_change =
1337 scrollbar_layer->SetCurrentPos(current_offset.y());
1338 scrollbar_needs_animation |= y_offset_did_change;
1339 scrollbar_needs_animation |=
1340 scrollbar_layer->SetMaximum(scroll_rect.height() - clip_rect.height());
1341 scrollbar_needs_animation |=
1342 scrollbar_layer->SetVisibleToTotalLengthRatio(visible_ratio);
1343 if (y_offset_did_change && layer_tree_impl()->IsActiveTree() &&
1344 this == layer_tree_impl()->InnerViewportScrollLayer()) {
1345 TRACE_COUNTER_ID1("cc", "scroll_offset_y", this->id(),
1346 current_offset.y());
1349 if (scrollbar_needs_animation) {
1350 layer_tree_impl()->set_needs_update_draw_properties();
1351 // TODO(wjmaclean) The scrollbar animator for the pinch-zoom scrollbars
1352 // should activate for every scroll on the main frame, not just the
1353 // scrolls that move the pinch virtual viewport (i.e. trigger from
1354 // either inner or outer viewport).
1355 if (scrollbar_animation_controller_) {
1356 // Non-overlay scrollbars shouldn't trigger animations.
1357 if (scrollbar_layer->is_overlay_scrollbar())
1358 scrollbar_animation_controller_->DidScrollUpdate(on_resize);
1363 void LayerImpl::DidBecomeActive() {
1364 if (layer_tree_impl_->settings().scrollbar_animator ==
1365 LayerTreeSettings::NO_ANIMATOR) {
1366 return;
1369 bool need_scrollbar_animation_controller = scrollable() && scrollbars_;
1370 if (!need_scrollbar_animation_controller) {
1371 scrollbar_animation_controller_ = nullptr;
1372 return;
1375 if (scrollbar_animation_controller_)
1376 return;
1378 scrollbar_animation_controller_ =
1379 layer_tree_impl_->CreateScrollbarAnimationController(this);
1382 void LayerImpl::ClearScrollbars() {
1383 if (!scrollbars_)
1384 return;
1386 scrollbars_.reset(nullptr);
1389 void LayerImpl::AddScrollbar(ScrollbarLayerImplBase* layer) {
1390 DCHECK(layer);
1391 DCHECK(!scrollbars_ || scrollbars_->find(layer) == scrollbars_->end());
1392 if (!scrollbars_)
1393 scrollbars_.reset(new ScrollbarSet());
1395 scrollbars_->insert(layer);
1398 void LayerImpl::RemoveScrollbar(ScrollbarLayerImplBase* layer) {
1399 DCHECK(scrollbars_);
1400 DCHECK(layer);
1401 DCHECK(scrollbars_->find(layer) != scrollbars_->end());
1403 scrollbars_->erase(layer);
1404 if (scrollbars_->empty())
1405 scrollbars_ = nullptr;
1408 bool LayerImpl::HasScrollbar(ScrollbarOrientation orientation) const {
1409 if (!scrollbars_)
1410 return false;
1412 for (ScrollbarSet::iterator it = scrollbars_->begin();
1413 it != scrollbars_->end();
1414 ++it)
1415 if ((*it)->orientation() == orientation)
1416 return true;
1418 return false;
1421 void LayerImpl::ScrollbarParametersDidChange(bool on_resize) {
1422 if (!scrollbars_)
1423 return;
1425 for (ScrollbarSet::iterator it = scrollbars_->begin();
1426 it != scrollbars_->end();
1427 ++it) {
1428 bool is_scroll_layer = (*it)->ScrollLayerId() == layer_id_;
1429 bool scroll_layer_resized = is_scroll_layer && on_resize;
1430 (*it)->ScrollbarParametersDidChange(scroll_layer_resized);
1434 void LayerImpl::SetNeedsPushProperties() {
1435 if (needs_push_properties_)
1436 return;
1437 if (!parent_should_know_need_push_properties() && parent_)
1438 parent_->AddDependentNeedsPushProperties();
1439 needs_push_properties_ = true;
1442 void LayerImpl::AddDependentNeedsPushProperties() {
1443 DCHECK_GE(num_dependents_need_push_properties_, 0);
1445 if (!parent_should_know_need_push_properties() && parent_)
1446 parent_->AddDependentNeedsPushProperties();
1448 num_dependents_need_push_properties_++;
1451 void LayerImpl::RemoveDependentNeedsPushProperties() {
1452 num_dependents_need_push_properties_--;
1453 DCHECK_GE(num_dependents_need_push_properties_, 0);
1455 if (!parent_should_know_need_push_properties() && parent_)
1456 parent_->RemoveDependentNeedsPushProperties();
1459 void LayerImpl::GetAllTilesAndPrioritiesForTracing(
1460 std::map<const Tile*, TilePriority>* tile_map) const {
1463 void LayerImpl::AsValueInto(base::trace_event::TracedValue* state) const {
1464 TracedValue::MakeDictIntoImplicitSnapshotWithCategory(
1465 TRACE_DISABLED_BY_DEFAULT("cc.debug"),
1466 state,
1467 "cc::LayerImpl",
1468 LayerTypeAsString(),
1469 this);
1470 state->SetInteger("layer_id", id());
1471 MathUtil::AddToTracedValue("bounds", bounds_, state);
1473 state->SetDouble("opacity", opacity());
1475 MathUtil::AddToTracedValue("position", position_, state);
1477 state->SetInteger("draws_content", DrawsContent());
1478 state->SetInteger("gpu_memory_usage", GPUMemoryUsageInBytes());
1480 MathUtil::AddToTracedValue(
1481 "scroll_offset", scroll_offset_ ? scroll_offset_->Current(IsActive())
1482 : gfx::ScrollOffset(),
1483 state);
1485 MathUtil::AddToTracedValue("transform_origin", transform_origin_, state);
1487 bool clipped;
1488 gfx::QuadF layer_quad = MathUtil::MapQuad(
1489 screen_space_transform(),
1490 gfx::QuadF(gfx::Rect(content_bounds())),
1491 &clipped);
1492 MathUtil::AddToTracedValue("layer_quad", layer_quad, state);
1493 if (!touch_event_handler_region_.IsEmpty()) {
1494 state->BeginArray("touch_event_handler_region");
1495 touch_event_handler_region_.AsValueInto(state);
1496 state->EndArray();
1498 if (have_wheel_event_handlers_) {
1499 gfx::Rect wheel_rect(content_bounds());
1500 Region wheel_region(wheel_rect);
1501 state->BeginArray("wheel_event_handler_region");
1502 wheel_region.AsValueInto(state);
1503 state->EndArray();
1505 if (have_scroll_event_handlers_) {
1506 gfx::Rect scroll_rect(content_bounds());
1507 Region scroll_region(scroll_rect);
1508 state->BeginArray("scroll_event_handler_region");
1509 scroll_region.AsValueInto(state);
1510 state->EndArray();
1512 if (!non_fast_scrollable_region_.IsEmpty()) {
1513 state->BeginArray("non_fast_scrollable_region");
1514 non_fast_scrollable_region_.AsValueInto(state);
1515 state->EndArray();
1517 if (scroll_blocks_on_) {
1518 state->SetInteger("scroll_blocks_on", scroll_blocks_on_);
1521 state->BeginArray("children");
1522 for (size_t i = 0; i < children_.size(); ++i) {
1523 state->BeginDictionary();
1524 children_[i]->AsValueInto(state);
1525 state->EndDictionary();
1527 state->EndArray();
1528 if (mask_layer_) {
1529 state->BeginDictionary("mask_layer");
1530 mask_layer_->AsValueInto(state);
1531 state->EndDictionary();
1533 if (replica_layer_) {
1534 state->BeginDictionary("replica_layer");
1535 replica_layer_->AsValueInto(state);
1536 state->EndDictionary();
1539 if (scroll_parent_)
1540 state->SetInteger("scroll_parent", scroll_parent_->id());
1542 if (clip_parent_)
1543 state->SetInteger("clip_parent", clip_parent_->id());
1545 state->SetBoolean("can_use_lcd_text", can_use_lcd_text());
1546 state->SetBoolean("contents_opaque", contents_opaque());
1548 state->SetBoolean(
1549 "has_animation_bounds",
1550 layer_animation_controller()->HasAnimationThatInflatesBounds());
1552 gfx::BoxF box;
1553 if (LayerUtils::GetAnimationBounds(*this, &box))
1554 MathUtil::AddToTracedValue("animation_bounds", box, state);
1556 if (debug_info_.get()) {
1557 std::string str;
1558 debug_info_->AppendAsTraceFormat(&str);
1559 base::JSONReader json_reader;
1560 scoped_ptr<base::Value> debug_info_value(json_reader.ReadToValue(str));
1562 if (debug_info_value->IsType(base::Value::TYPE_DICTIONARY)) {
1563 base::DictionaryValue* dictionary_value = nullptr;
1564 bool converted_to_dictionary =
1565 debug_info_value->GetAsDictionary(&dictionary_value);
1566 DCHECK(converted_to_dictionary);
1567 for (base::DictionaryValue::Iterator it(*dictionary_value); !it.IsAtEnd();
1568 it.Advance()) {
1569 state->SetValue(it.key().data(), it.value().CreateDeepCopy());
1571 } else {
1572 NOTREACHED();
1576 if (!frame_timing_requests_.empty()) {
1577 state->BeginArray("frame_timing_requests");
1578 for (const auto& request : frame_timing_requests_) {
1579 state->BeginDictionary();
1580 state->SetInteger("request_id", request.id());
1581 MathUtil::AddToTracedValue("request_rect", request.rect(), state);
1582 state->EndDictionary();
1584 state->EndArray();
1588 bool LayerImpl::IsDrawnRenderSurfaceLayerListMember() const {
1589 return draw_properties_.last_drawn_render_surface_layer_list_id ==
1590 layer_tree_impl_->current_render_surface_list_id();
1593 size_t LayerImpl::GPUMemoryUsageInBytes() const { return 0; }
1595 void LayerImpl::RunMicroBenchmark(MicroBenchmarkImpl* benchmark) {
1596 benchmark->RunOnLayer(this);
1599 int LayerImpl::NumDescendantsThatDrawContent() const {
1600 return num_descendants_that_draw_content_;
1603 void LayerImpl::NotifyAnimationFinished(
1604 base::TimeTicks monotonic_time,
1605 Animation::TargetProperty target_property,
1606 int group) {
1607 if (target_property == Animation::SCROLL_OFFSET)
1608 layer_tree_impl_->InputScrollAnimationFinished();
1611 void LayerImpl::SetHasRenderSurface(bool should_have_render_surface) {
1612 if (!!render_surface() == should_have_render_surface)
1613 return;
1615 SetNeedsPushProperties();
1616 layer_tree_impl()->set_needs_update_draw_properties();
1617 if (should_have_render_surface) {
1618 render_surface_ = make_scoped_ptr(new RenderSurfaceImpl(this));
1619 return;
1621 render_surface_.reset();
1624 Region LayerImpl::GetInvalidationRegion() {
1625 return Region(update_rect_);
1628 gfx::Rect LayerImpl::GetEnclosingRectInTargetSpace() const {
1629 return MathUtil::MapEnclosingClippedRect(
1630 draw_properties_.target_space_transform,
1631 gfx::Rect(draw_properties_.content_bounds));
1634 gfx::Rect LayerImpl::GetScaledEnclosingRectInTargetSpace(float scale) const {
1635 gfx::Transform scaled_draw_transform =
1636 draw_properties_.target_space_transform;
1637 scaled_draw_transform.Scale(SK_MScalar1 / scale, SK_MScalar1 / scale);
1638 gfx::Size scaled_content_bounds =
1639 gfx::ToCeiledSize(gfx::ScaleSize(content_bounds(), scale));
1640 return MathUtil::MapEnclosingClippedRect(scaled_draw_transform,
1641 gfx::Rect(scaled_content_bounds));
1644 } // namespace cc