Use multiline attribute to check for IA2_STATE_MULTILINE.
[chromium-blink-merge.git] / cc / layers / layer_impl.cc
blob8b37d12eef225b75ca6267302d693bd836205e38
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 layer_property_changed_(false),
62 masks_to_bounds_(false),
63 contents_opaque_(false),
64 is_root_for_isolated_group_(false),
65 use_parent_backface_visibility_(false),
66 draw_checkerboard_for_missing_tiles_(false),
67 draws_content_(false),
68 hide_layer_and_subtree_(false),
69 transform_is_invertible_(true),
70 is_container_for_fixed_position_layers_(false),
71 background_color_(0),
72 opacity_(1.0),
73 blend_mode_(SkXfermode::kSrcOver_Mode),
74 num_descendants_that_draw_content_(0),
75 draw_depth_(0.f),
76 needs_push_properties_(false),
77 num_dependents_need_push_properties_(0),
78 sorting_context_id_(0),
79 current_draw_mode_(DRAW_MODE_NONE),
80 frame_timing_requests_dirty_(false) {
81 DCHECK_GT(layer_id_, 0);
82 DCHECK(layer_tree_impl_);
83 layer_tree_impl_->RegisterLayer(this);
84 AnimationRegistrar* registrar = layer_tree_impl_->GetAnimationRegistrar();
85 layer_animation_controller_ =
86 registrar->GetAnimationControllerForId(layer_id_);
87 layer_animation_controller_->AddValueObserver(this);
88 if (IsActive()) {
89 layer_animation_controller_->set_value_provider(this);
90 layer_animation_controller_->set_layer_animation_delegate(this);
92 SetNeedsPushProperties();
95 LayerImpl::~LayerImpl() {
96 DCHECK_EQ(DRAW_MODE_NONE, current_draw_mode_);
98 layer_animation_controller_->RemoveValueObserver(this);
99 layer_animation_controller_->remove_value_provider(this);
100 layer_animation_controller_->remove_layer_animation_delegate(this);
102 if (!copy_requests_.empty() && layer_tree_impl_->IsActiveTree())
103 layer_tree_impl()->RemoveLayerWithCopyOutputRequest(this);
104 layer_tree_impl_->UnregisterLayer(this);
106 TRACE_EVENT_OBJECT_DELETED_WITH_ID(
107 TRACE_DISABLED_BY_DEFAULT("cc.debug"), "cc::LayerImpl", this);
110 void LayerImpl::AddChild(scoped_ptr<LayerImpl> child) {
111 child->SetParent(this);
112 DCHECK_EQ(layer_tree_impl(), child->layer_tree_impl());
113 children_.push_back(child.Pass());
114 layer_tree_impl()->set_needs_update_draw_properties();
117 scoped_ptr<LayerImpl> LayerImpl::RemoveChild(LayerImpl* child) {
118 for (OwnedLayerImplList::iterator it = children_.begin();
119 it != children_.end();
120 ++it) {
121 if (*it == child) {
122 scoped_ptr<LayerImpl> ret = children_.take(it);
123 children_.erase(it);
124 layer_tree_impl()->set_needs_update_draw_properties();
125 return ret.Pass();
128 return nullptr;
131 void LayerImpl::SetParent(LayerImpl* parent) {
132 if (parent_should_know_need_push_properties()) {
133 if (parent_)
134 parent_->RemoveDependentNeedsPushProperties();
135 if (parent)
136 parent->AddDependentNeedsPushProperties();
138 parent_ = parent;
141 void LayerImpl::ClearChildList() {
142 if (children_.empty())
143 return;
145 children_.clear();
146 layer_tree_impl()->set_needs_update_draw_properties();
149 bool LayerImpl::HasAncestor(const LayerImpl* ancestor) const {
150 if (!ancestor)
151 return false;
153 for (const LayerImpl* layer = this; layer; layer = layer->parent()) {
154 if (layer == ancestor)
155 return true;
158 return false;
161 void LayerImpl::SetScrollParent(LayerImpl* parent) {
162 if (scroll_parent_ == parent)
163 return;
165 if (parent)
166 DCHECK_EQ(layer_tree_impl()->LayerById(parent->id()), parent);
168 scroll_parent_ = parent;
169 SetNeedsPushProperties();
172 void LayerImpl::SetDebugInfo(
173 scoped_refptr<base::trace_event::ConvertableToTraceFormat> other) {
174 debug_info_ = other;
175 SetNeedsPushProperties();
178 void LayerImpl::SetScrollChildren(std::set<LayerImpl*>* children) {
179 if (scroll_children_.get() == children)
180 return;
181 scroll_children_.reset(children);
182 SetNeedsPushProperties();
185 void LayerImpl::SetNumDescendantsThatDrawContent(int num_descendants) {
186 if (num_descendants_that_draw_content_ == num_descendants)
187 return;
188 num_descendants_that_draw_content_ = num_descendants;
189 SetNeedsPushProperties();
192 void LayerImpl::SetClipParent(LayerImpl* ancestor) {
193 if (clip_parent_ == ancestor)
194 return;
196 clip_parent_ = ancestor;
197 SetNeedsPushProperties();
200 void LayerImpl::SetClipChildren(std::set<LayerImpl*>* children) {
201 if (clip_children_.get() == children)
202 return;
203 clip_children_.reset(children);
204 SetNeedsPushProperties();
207 void LayerImpl::PassCopyRequests(ScopedPtrVector<CopyOutputRequest>* requests) {
208 if (requests->empty())
209 return;
210 DCHECK(render_surface());
211 bool was_empty = copy_requests_.empty();
212 copy_requests_.insert_and_take(copy_requests_.end(), requests);
213 requests->clear();
215 if (was_empty && layer_tree_impl()->IsActiveTree())
216 layer_tree_impl()->AddLayerWithCopyOutputRequest(this);
217 NoteLayerPropertyChangedForSubtree();
220 void LayerImpl::TakeCopyRequestsAndTransformToTarget(
221 ScopedPtrVector<CopyOutputRequest>* requests) {
222 DCHECK(!copy_requests_.empty());
223 DCHECK(layer_tree_impl()->IsActiveTree());
224 DCHECK_EQ(render_target(), this);
226 size_t first_inserted_request = requests->size();
227 requests->insert_and_take(requests->end(), &copy_requests_);
228 copy_requests_.clear();
230 for (size_t i = first_inserted_request; i < requests->size(); ++i) {
231 CopyOutputRequest* request = requests->at(i);
232 if (!request->has_area())
233 continue;
235 gfx::Rect request_in_layer_space = request->area();
236 gfx::Rect request_in_content_space =
237 LayerRectToContentRect(request_in_layer_space);
238 request->set_area(MathUtil::MapEnclosingClippedRect(
239 draw_properties_.target_space_transform, request_in_content_space));
242 layer_tree_impl()->RemoveLayerWithCopyOutputRequest(this);
243 layer_tree_impl()->set_needs_update_draw_properties();
246 void LayerImpl::ClearRenderSurfaceLayerList() {
247 if (render_surface_)
248 render_surface_->ClearLayerLists();
251 void LayerImpl::PopulateSharedQuadState(SharedQuadState* state) const {
252 state->SetAll(
253 draw_properties_.target_space_transform, draw_properties_.content_bounds,
254 draw_properties_.visible_content_rect, draw_properties_.clip_rect,
255 draw_properties_.is_clipped, draw_properties_.opacity,
256 draw_properties_.blend_mode, sorting_context_id_);
259 void LayerImpl::PopulateScaledSharedQuadState(SharedQuadState* state,
260 float scale) const {
261 gfx::Transform scaled_draw_transform =
262 draw_properties_.target_space_transform;
263 scaled_draw_transform.Scale(SK_MScalar1 / scale, SK_MScalar1 / scale);
264 gfx::Size scaled_content_bounds =
265 gfx::ToCeiledSize(gfx::ScaleSize(bounds(), scale));
266 gfx::Rect scaled_visible_content_rect =
267 gfx::ScaleToEnclosingRect(visible_content_rect(), scale);
268 scaled_visible_content_rect.Intersect(gfx::Rect(scaled_content_bounds));
270 state->SetAll(scaled_draw_transform, scaled_content_bounds,
271 scaled_visible_content_rect, draw_properties().clip_rect,
272 draw_properties().is_clipped, draw_properties().opacity,
273 draw_properties().blend_mode, sorting_context_id_);
276 bool LayerImpl::WillDraw(DrawMode draw_mode,
277 ResourceProvider* resource_provider) {
278 // WillDraw/DidDraw must be matched.
279 DCHECK_NE(DRAW_MODE_NONE, draw_mode);
280 DCHECK_EQ(DRAW_MODE_NONE, current_draw_mode_);
281 current_draw_mode_ = draw_mode;
282 return true;
285 void LayerImpl::DidDraw(ResourceProvider* resource_provider) {
286 DCHECK_NE(DRAW_MODE_NONE, current_draw_mode_);
287 current_draw_mode_ = DRAW_MODE_NONE;
290 bool LayerImpl::ShowDebugBorders() const {
291 return layer_tree_impl()->debug_state().show_debug_borders;
294 void LayerImpl::GetDebugBorderProperties(SkColor* color, float* width) const {
295 if (draws_content_) {
296 *color = DebugColors::ContentLayerBorderColor();
297 *width = DebugColors::ContentLayerBorderWidth(layer_tree_impl());
298 return;
301 if (masks_to_bounds_) {
302 *color = DebugColors::MaskingLayerBorderColor();
303 *width = DebugColors::MaskingLayerBorderWidth(layer_tree_impl());
304 return;
307 *color = DebugColors::ContainerLayerBorderColor();
308 *width = DebugColors::ContainerLayerBorderWidth(layer_tree_impl());
311 void LayerImpl::AppendDebugBorderQuad(
312 RenderPass* render_pass,
313 const gfx::Size& content_bounds,
314 const SharedQuadState* shared_quad_state,
315 AppendQuadsData* append_quads_data) const {
316 SkColor color;
317 float width;
318 GetDebugBorderProperties(&color, &width);
319 AppendDebugBorderQuad(render_pass,
320 content_bounds,
321 shared_quad_state,
322 append_quads_data,
323 color,
324 width);
327 void LayerImpl::AppendDebugBorderQuad(RenderPass* render_pass,
328 const gfx::Size& content_bounds,
329 const SharedQuadState* shared_quad_state,
330 AppendQuadsData* append_quads_data,
331 SkColor color,
332 float width) const {
333 if (!ShowDebugBorders())
334 return;
336 gfx::Rect quad_rect(content_bounds);
337 gfx::Rect visible_quad_rect(quad_rect);
338 DebugBorderDrawQuad* debug_border_quad =
339 render_pass->CreateAndAppendDrawQuad<DebugBorderDrawQuad>();
340 debug_border_quad->SetNew(
341 shared_quad_state, quad_rect, visible_quad_rect, color, width);
342 if (contents_opaque()) {
343 // When opaque, draw a second inner border that is thicker than the outer
344 // border, but more transparent.
345 static const float kFillOpacity = 0.3f;
346 SkColor fill_color = SkColorSetA(
347 color, static_cast<uint8_t>(SkColorGetA(color) * kFillOpacity));
348 float fill_width = width * 3;
349 gfx::Rect fill_rect = quad_rect;
350 fill_rect.Inset(fill_width / 2.f, fill_width / 2.f);
351 gfx::Rect visible_fill_rect =
352 gfx::IntersectRects(visible_quad_rect, fill_rect);
353 DebugBorderDrawQuad* fill_quad =
354 render_pass->CreateAndAppendDrawQuad<DebugBorderDrawQuad>();
355 fill_quad->SetNew(shared_quad_state, fill_rect, visible_fill_rect,
356 fill_color, fill_width);
360 bool LayerImpl::HasDelegatedContent() const {
361 return false;
364 bool LayerImpl::HasContributingDelegatedRenderPasses() const {
365 return false;
368 RenderPassId LayerImpl::FirstContributingRenderPassId() const {
369 return RenderPassId(0, 0);
372 RenderPassId LayerImpl::NextContributingRenderPassId(RenderPassId id) const {
373 return RenderPassId(0, 0);
376 void LayerImpl::GetContentsResourceId(ResourceProvider::ResourceId* resource_id,
377 gfx::Size* resource_size) const {
378 NOTREACHED();
379 *resource_id = 0;
382 gfx::Vector2dF LayerImpl::ScrollBy(const gfx::Vector2dF& scroll) {
383 gfx::ScrollOffset adjusted_scroll(scroll);
384 if (layer_tree_impl()->settings().use_pinch_virtual_viewport) {
385 if (!user_scrollable_horizontal_)
386 adjusted_scroll.set_x(0);
387 if (!user_scrollable_vertical_)
388 adjusted_scroll.set_y(0);
390 DCHECK(scrollable());
391 gfx::ScrollOffset old_offset = CurrentScrollOffset();
392 gfx::ScrollOffset new_offset =
393 ClampScrollOffsetToLimits(old_offset + adjusted_scroll);
394 SetCurrentScrollOffset(new_offset);
396 gfx::ScrollOffset unscrolled =
397 old_offset + gfx::ScrollOffset(scroll) - new_offset;
398 return gfx::Vector2dF(unscrolled.x(), unscrolled.y());
401 void LayerImpl::SetScrollClipLayer(int scroll_clip_layer_id) {
402 scroll_clip_layer_ = layer_tree_impl()->LayerById(scroll_clip_layer_id);
405 bool LayerImpl::user_scrollable(ScrollbarOrientation orientation) const {
406 return (orientation == HORIZONTAL) ? user_scrollable_horizontal_
407 : user_scrollable_vertical_;
410 void LayerImpl::ApplySentScrollDeltasFromAbortedCommit() {
411 DCHECK(layer_tree_impl()->IsActiveTree());
412 scroll_offset_->AbortCommit();
415 InputHandler::ScrollStatus LayerImpl::TryScroll(
416 const gfx::PointF& screen_space_point,
417 InputHandler::ScrollInputType type,
418 ScrollBlocksOn effective_block_mode) const {
419 if (should_scroll_on_main_thread()) {
420 TRACE_EVENT0("cc", "LayerImpl::TryScroll: Failed ShouldScrollOnMainThread");
421 return InputHandler::SCROLL_ON_MAIN_THREAD;
424 if (!screen_space_transform().IsInvertible()) {
425 TRACE_EVENT0("cc", "LayerImpl::TryScroll: Ignored NonInvertibleTransform");
426 return InputHandler::SCROLL_IGNORED;
429 if (!non_fast_scrollable_region().IsEmpty()) {
430 bool clipped = false;
431 gfx::Transform inverse_screen_space_transform(
432 gfx::Transform::kSkipInitialization);
433 if (!screen_space_transform().GetInverse(&inverse_screen_space_transform)) {
434 // TODO(shawnsingh): We shouldn't be applying a projection if screen space
435 // transform is uninvertible here. Perhaps we should be returning
436 // SCROLL_ON_MAIN_THREAD in this case?
439 gfx::PointF hit_test_point_in_content_space =
440 MathUtil::ProjectPoint(inverse_screen_space_transform,
441 screen_space_point,
442 &clipped);
443 gfx::PointF hit_test_point_in_layer_space =
444 gfx::ScalePoint(hit_test_point_in_content_space,
445 1.f / contents_scale_x(),
446 1.f / contents_scale_y());
447 if (!clipped &&
448 non_fast_scrollable_region().Contains(
449 gfx::ToRoundedPoint(hit_test_point_in_layer_space))) {
450 TRACE_EVENT0("cc",
451 "LayerImpl::tryScroll: Failed NonFastScrollableRegion");
452 return InputHandler::SCROLL_ON_MAIN_THREAD;
456 if (have_scroll_event_handlers() &&
457 effective_block_mode & SCROLL_BLOCKS_ON_SCROLL_EVENT) {
458 TRACE_EVENT0("cc", "LayerImpl::tryScroll: Failed ScrollEventHandlers");
459 return InputHandler::SCROLL_ON_MAIN_THREAD;
462 if (type == InputHandler::WHEEL && have_wheel_event_handlers() &&
463 effective_block_mode & SCROLL_BLOCKS_ON_WHEEL_EVENT) {
464 TRACE_EVENT0("cc", "LayerImpl::tryScroll: Failed WheelEventHandlers");
465 return InputHandler::SCROLL_ON_MAIN_THREAD;
468 if (!scrollable()) {
469 TRACE_EVENT0("cc", "LayerImpl::tryScroll: Ignored not scrollable");
470 return InputHandler::SCROLL_IGNORED;
473 gfx::ScrollOffset max_scroll_offset = MaxScrollOffset();
474 if (max_scroll_offset.x() <= 0 && max_scroll_offset.y() <= 0) {
475 TRACE_EVENT0("cc",
476 "LayerImpl::tryScroll: Ignored. Technically scrollable,"
477 " but has no affordance in either direction.");
478 return InputHandler::SCROLL_IGNORED;
481 return InputHandler::SCROLL_STARTED;
484 gfx::Rect LayerImpl::LayerRectToContentRect(
485 const gfx::RectF& layer_rect) const {
486 gfx::RectF content_rect =
487 gfx::ScaleRect(layer_rect, contents_scale_x(), contents_scale_y());
488 // Intersect with content rect to avoid the extra pixel because for some
489 // values x and y, ceil((x / y) * y) may be x + 1.
490 content_rect.Intersect(gfx::Rect(content_bounds()));
491 return gfx::ToEnclosingRect(content_rect);
494 skia::RefPtr<SkPicture> LayerImpl::GetPicture() {
495 return skia::RefPtr<SkPicture>();
498 scoped_ptr<LayerImpl> LayerImpl::CreateLayerImpl(LayerTreeImpl* tree_impl) {
499 return LayerImpl::Create(tree_impl, layer_id_, scroll_offset_);
502 void LayerImpl::PushPropertiesTo(LayerImpl* layer) {
503 layer->SetTransformOrigin(transform_origin_);
504 layer->SetBackgroundColor(background_color_);
505 layer->SetBounds(bounds_);
506 layer->SetContentBounds(content_bounds());
507 layer->SetContentsScale(contents_scale_x(), contents_scale_y());
508 layer->SetDoubleSided(double_sided_);
509 layer->SetDrawCheckerboardForMissingTiles(
510 draw_checkerboard_for_missing_tiles_);
511 layer->SetDrawsContent(DrawsContent());
512 layer->SetHideLayerAndSubtree(hide_layer_and_subtree_);
513 layer->SetHasRenderSurface(!!render_surface() || layer->HasCopyRequest());
514 layer->SetFilters(filters());
515 layer->SetBackgroundFilters(background_filters());
516 layer->SetMasksToBounds(masks_to_bounds_);
517 layer->SetShouldScrollOnMainThread(should_scroll_on_main_thread_);
518 layer->SetHaveWheelEventHandlers(have_wheel_event_handlers_);
519 layer->SetHaveScrollEventHandlers(have_scroll_event_handlers_);
520 layer->SetScrollBlocksOn(scroll_blocks_on_);
521 layer->SetNonFastScrollableRegion(non_fast_scrollable_region_);
522 layer->SetTouchEventHandlerRegion(touch_event_handler_region_);
523 layer->SetContentsOpaque(contents_opaque_);
524 layer->SetOpacity(opacity_);
525 layer->SetBlendMode(blend_mode_);
526 layer->SetIsRootForIsolatedGroup(is_root_for_isolated_group_);
527 layer->SetPosition(position_);
528 layer->SetIsContainerForFixedPositionLayers(
529 is_container_for_fixed_position_layers_);
530 layer->SetPositionConstraint(position_constraint_);
531 layer->SetShouldFlattenTransform(should_flatten_transform_);
532 layer->SetUseParentBackfaceVisibility(use_parent_backface_visibility_);
533 layer->SetTransformAndInvertibility(transform_, transform_is_invertible_);
535 layer->SetScrollClipLayer(scroll_clip_layer_ ? scroll_clip_layer_->id()
536 : Layer::INVALID_ID);
537 layer->set_user_scrollable_horizontal(user_scrollable_horizontal_);
538 layer->set_user_scrollable_vertical(user_scrollable_vertical_);
540 layer->SetScrollCompensationAdjustment(scroll_compensation_adjustment_);
542 layer->PushScrollOffset(nullptr);
544 layer->Set3dSortingContextId(sorting_context_id_);
545 layer->SetNumDescendantsThatDrawContent(num_descendants_that_draw_content_);
547 LayerImpl* scroll_parent = nullptr;
548 if (scroll_parent_) {
549 scroll_parent = layer->layer_tree_impl()->LayerById(scroll_parent_->id());
550 DCHECK(scroll_parent);
553 layer->SetScrollParent(scroll_parent);
554 if (scroll_children_) {
555 std::set<LayerImpl*>* scroll_children = new std::set<LayerImpl*>;
556 for (std::set<LayerImpl*>::iterator it = scroll_children_->begin();
557 it != scroll_children_->end();
558 ++it) {
559 DCHECK_EQ((*it)->scroll_parent(), this);
560 LayerImpl* scroll_child =
561 layer->layer_tree_impl()->LayerById((*it)->id());
562 DCHECK(scroll_child);
563 scroll_children->insert(scroll_child);
565 layer->SetScrollChildren(scroll_children);
566 } else {
567 layer->SetScrollChildren(nullptr);
570 LayerImpl* clip_parent = nullptr;
571 if (clip_parent_) {
572 clip_parent = layer->layer_tree_impl()->LayerById(
573 clip_parent_->id());
574 DCHECK(clip_parent);
577 layer->SetClipParent(clip_parent);
578 if (clip_children_) {
579 std::set<LayerImpl*>* clip_children = new std::set<LayerImpl*>;
580 for (std::set<LayerImpl*>::iterator it = clip_children_->begin();
581 it != clip_children_->end(); ++it)
582 clip_children->insert(layer->layer_tree_impl()->LayerById((*it)->id()));
583 layer->SetClipChildren(clip_children);
584 } else {
585 layer->SetClipChildren(nullptr);
588 layer->PassCopyRequests(&copy_requests_);
590 // If the main thread commits multiple times before the impl thread actually
591 // draws, then damage tracking will become incorrect if we simply clobber the
592 // update_rect here. The LayerImpl's update_rect needs to accumulate (i.e.
593 // union) any update changes that have occurred on the main thread.
594 update_rect_.Union(layer->update_rect());
595 layer->SetUpdateRect(update_rect_);
597 layer->SetStackingOrderChanged(stacking_order_changed_);
598 layer->SetDebugInfo(debug_info_);
600 if (frame_timing_requests_dirty_) {
601 layer->PassFrameTimingRequests(&frame_timing_requests_);
602 frame_timing_requests_dirty_ = false;
605 // Reset any state that should be cleared for the next update.
606 stacking_order_changed_ = false;
607 update_rect_ = gfx::Rect();
608 needs_push_properties_ = false;
609 num_dependents_need_push_properties_ = 0;
612 gfx::Vector2dF LayerImpl::FixedContainerSizeDelta() const {
613 if (!scroll_clip_layer_)
614 return gfx::Vector2dF();
616 gfx::Vector2dF delta_from_scroll = scroll_clip_layer_->bounds_delta();
618 // In virtual-viewport mode, we don't need to compensate for pinch zoom or
619 // scale since the fixed container is the outer viewport, which sits below
620 // the page scale.
621 if (layer_tree_impl()->settings().use_pinch_virtual_viewport)
622 return delta_from_scroll;
624 float scale_delta = layer_tree_impl()->page_scale_delta();
625 float scale = layer_tree_impl()->current_page_scale_factor() /
626 layer_tree_impl()->page_scale_delta();
628 delta_from_scroll.Scale(1.f / scale);
630 // The delta-from-pinch component requires some explanation: A viewport of
631 // size (w,h) will appear to be size (w/s,h/s) under scale s in the content
632 // space. If s -> s' on the impl thread, where s' = s * ds, then the apparent
633 // viewport size change in the content space due to ds is:
635 // (w/s',h/s') - (w/s,h/s) = (w,h)(1/s' - 1/s) = (w,h)(1 - ds)/(s ds)
637 gfx::Vector2dF delta_from_pinch =
638 gfx::Rect(scroll_clip_layer_->bounds()).bottom_right() - gfx::PointF();
639 delta_from_pinch.Scale((1.f - scale_delta) / (scale * scale_delta));
641 return delta_from_scroll + delta_from_pinch;
644 base::DictionaryValue* LayerImpl::LayerTreeAsJson() const {
645 base::DictionaryValue* result = new base::DictionaryValue;
646 result->SetString("LayerType", LayerTypeAsString());
648 base::ListValue* list = new base::ListValue;
649 list->AppendInteger(bounds().width());
650 list->AppendInteger(bounds().height());
651 result->Set("Bounds", list);
653 list = new base::ListValue;
654 list->AppendDouble(position_.x());
655 list->AppendDouble(position_.y());
656 result->Set("Position", list);
658 const gfx::Transform& gfx_transform = draw_properties_.target_space_transform;
659 double transform[16];
660 gfx_transform.matrix().asColMajord(transform);
661 list = new base::ListValue;
662 for (int i = 0; i < 16; ++i)
663 list->AppendDouble(transform[i]);
664 result->Set("DrawTransform", list);
666 result->SetBoolean("DrawsContent", draws_content_);
667 result->SetBoolean("Is3dSorted", Is3dSorted());
668 result->SetDouble("OPACITY", opacity());
669 result->SetBoolean("ContentsOpaque", contents_opaque_);
671 if (scrollable())
672 result->SetBoolean("Scrollable", true);
674 if (have_wheel_event_handlers_)
675 result->SetBoolean("WheelHandler", have_wheel_event_handlers_);
676 if (have_scroll_event_handlers_)
677 result->SetBoolean("ScrollHandler", have_scroll_event_handlers_);
678 if (!touch_event_handler_region_.IsEmpty()) {
679 scoped_ptr<base::Value> region = touch_event_handler_region_.AsValue();
680 result->Set("TouchRegion", region.release());
683 if (scroll_blocks_on_) {
684 list = new base::ListValue;
685 if (scroll_blocks_on_ & SCROLL_BLOCKS_ON_START_TOUCH)
686 list->AppendString("StartTouch");
687 if (scroll_blocks_on_ & SCROLL_BLOCKS_ON_WHEEL_EVENT)
688 list->AppendString("WheelEvent");
689 if (scroll_blocks_on_ & SCROLL_BLOCKS_ON_SCROLL_EVENT)
690 list->AppendString("ScrollEvent");
691 result->Set("ScrollBlocksOn", list);
694 list = new base::ListValue;
695 for (size_t i = 0; i < children_.size(); ++i)
696 list->Append(children_[i]->LayerTreeAsJson());
697 result->Set("Children", list);
699 return result;
702 void LayerImpl::SetStackingOrderChanged(bool stacking_order_changed) {
703 if (stacking_order_changed) {
704 stacking_order_changed_ = true;
705 NoteLayerPropertyChangedForSubtree();
709 void LayerImpl::NoteLayerPropertyChanged() {
710 layer_property_changed_ = true;
711 layer_tree_impl()->set_needs_update_draw_properties();
712 SetNeedsPushProperties();
715 void LayerImpl::NoteLayerPropertyChangedForSubtree() {
716 layer_property_changed_ = true;
717 layer_tree_impl()->set_needs_update_draw_properties();
718 for (size_t i = 0; i < children_.size(); ++i)
719 children_[i]->NoteLayerPropertyChangedForDescendantsInternal();
720 SetNeedsPushProperties();
723 void LayerImpl::NoteLayerPropertyChangedForDescendantsInternal() {
724 layer_property_changed_ = true;
725 for (size_t i = 0; i < children_.size(); ++i)
726 children_[i]->NoteLayerPropertyChangedForDescendantsInternal();
729 void LayerImpl::NoteLayerPropertyChangedForDescendants() {
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 const char* LayerImpl::LayerTypeAsString() const {
737 return "cc::LayerImpl";
740 void LayerImpl::ResetAllChangeTrackingForSubtree() {
741 layer_property_changed_ = false;
743 update_rect_ = gfx::Rect();
744 damage_rect_ = gfx::RectF();
746 if (render_surface_)
747 render_surface_->ResetPropertyChangedFlag();
749 if (mask_layer_)
750 mask_layer_->ResetAllChangeTrackingForSubtree();
752 if (replica_layer_) {
753 // This also resets the replica mask, if it exists.
754 replica_layer_->ResetAllChangeTrackingForSubtree();
757 for (size_t i = 0; i < children_.size(); ++i)
758 children_[i]->ResetAllChangeTrackingForSubtree();
760 needs_push_properties_ = false;
761 num_dependents_need_push_properties_ = 0;
764 gfx::ScrollOffset LayerImpl::ScrollOffsetForAnimation() const {
765 return CurrentScrollOffset();
768 void LayerImpl::OnFilterAnimated(const FilterOperations& filters) {
769 SetFilters(filters);
772 void LayerImpl::OnOpacityAnimated(float opacity) {
773 SetOpacity(opacity);
776 void LayerImpl::OnTransformAnimated(const gfx::Transform& transform) {
777 SetTransform(transform);
780 void LayerImpl::OnScrollOffsetAnimated(const gfx::ScrollOffset& scroll_offset) {
781 // Only layers in the active tree should need to do anything here, since
782 // layers in the pending tree will find out about these changes as a
783 // result of the shared SyncedProperty.
784 if (!IsActive())
785 return;
787 SetCurrentScrollOffset(scroll_offset);
789 layer_tree_impl_->DidAnimateScrollOffset();
792 void LayerImpl::OnAnimationWaitingForDeletion() {}
794 bool LayerImpl::IsActive() const {
795 return layer_tree_impl_->IsActiveTree();
798 gfx::Size LayerImpl::bounds() const {
799 gfx::Vector2d delta = gfx::ToCeiledVector2d(bounds_delta_);
800 return gfx::Size(bounds_.width() + delta.x(),
801 bounds_.height() + delta.y());
804 gfx::SizeF LayerImpl::BoundsForScrolling() const {
805 return gfx::SizeF(bounds_.width() + bounds_delta_.x(),
806 bounds_.height() + bounds_delta_.y());
809 void LayerImpl::SetBounds(const gfx::Size& bounds) {
810 if (bounds_ == bounds)
811 return;
813 bounds_ = bounds;
815 ScrollbarParametersDidChange(true);
816 if (masks_to_bounds())
817 NoteLayerPropertyChangedForSubtree();
818 else
819 NoteLayerPropertyChanged();
822 void LayerImpl::SetBoundsDelta(const gfx::Vector2dF& bounds_delta) {
823 if (bounds_delta_ == bounds_delta)
824 return;
826 bounds_delta_ = bounds_delta;
828 ScrollbarParametersDidChange(true);
829 if (masks_to_bounds())
830 NoteLayerPropertyChangedForSubtree();
831 else
832 NoteLayerPropertyChanged();
835 void LayerImpl::SetMaskLayer(scoped_ptr<LayerImpl> mask_layer) {
836 int new_layer_id = mask_layer ? mask_layer->id() : -1;
838 if (mask_layer) {
839 DCHECK_EQ(layer_tree_impl(), mask_layer->layer_tree_impl());
840 DCHECK_NE(new_layer_id, mask_layer_id_);
841 } else if (new_layer_id == mask_layer_id_) {
842 return;
845 mask_layer_ = mask_layer.Pass();
846 mask_layer_id_ = new_layer_id;
847 if (mask_layer_)
848 mask_layer_->SetParent(this);
849 NoteLayerPropertyChangedForSubtree();
852 scoped_ptr<LayerImpl> LayerImpl::TakeMaskLayer() {
853 mask_layer_id_ = -1;
854 return mask_layer_.Pass();
857 void LayerImpl::SetReplicaLayer(scoped_ptr<LayerImpl> replica_layer) {
858 int new_layer_id = replica_layer ? replica_layer->id() : -1;
860 if (replica_layer) {
861 DCHECK_EQ(layer_tree_impl(), replica_layer->layer_tree_impl());
862 DCHECK_NE(new_layer_id, replica_layer_id_);
863 } else if (new_layer_id == replica_layer_id_) {
864 return;
867 replica_layer_ = replica_layer.Pass();
868 replica_layer_id_ = new_layer_id;
869 if (replica_layer_)
870 replica_layer_->SetParent(this);
871 NoteLayerPropertyChangedForSubtree();
874 scoped_ptr<LayerImpl> LayerImpl::TakeReplicaLayer() {
875 replica_layer_id_ = -1;
876 return replica_layer_.Pass();
879 ScrollbarLayerImplBase* LayerImpl::ToScrollbarLayer() {
880 return nullptr;
883 void LayerImpl::SetDrawsContent(bool draws_content) {
884 if (draws_content_ == draws_content)
885 return;
887 draws_content_ = draws_content;
888 NoteLayerPropertyChanged();
891 void LayerImpl::SetHideLayerAndSubtree(bool hide) {
892 if (hide_layer_and_subtree_ == hide)
893 return;
895 hide_layer_and_subtree_ = hide;
896 NoteLayerPropertyChangedForSubtree();
899 void LayerImpl::SetTransformOrigin(const gfx::Point3F& transform_origin) {
900 if (transform_origin_ == transform_origin)
901 return;
902 transform_origin_ = transform_origin;
903 NoteLayerPropertyChangedForSubtree();
906 void LayerImpl::SetBackgroundColor(SkColor background_color) {
907 if (background_color_ == background_color)
908 return;
910 background_color_ = background_color;
911 NoteLayerPropertyChanged();
914 SkColor LayerImpl::SafeOpaqueBackgroundColor() const {
915 SkColor color = background_color();
916 if (SkColorGetA(color) == 255 && !contents_opaque()) {
917 color = SK_ColorTRANSPARENT;
918 } else if (SkColorGetA(color) != 255 && contents_opaque()) {
919 for (const LayerImpl* layer = parent(); layer;
920 layer = layer->parent()) {
921 color = layer->background_color();
922 if (SkColorGetA(color) == 255)
923 break;
925 if (SkColorGetA(color) != 255)
926 color = layer_tree_impl()->background_color();
927 if (SkColorGetA(color) != 255)
928 color = SkColorSetA(color, 255);
930 return color;
933 void LayerImpl::SetFilters(const FilterOperations& filters) {
934 if (filters_ == filters)
935 return;
937 filters_ = filters;
938 NoteLayerPropertyChangedForSubtree();
941 bool LayerImpl::FilterIsAnimating() const {
942 return layer_animation_controller_->IsAnimatingProperty(Animation::FILTER);
945 bool LayerImpl::FilterIsAnimatingOnImplOnly() const {
946 Animation* filter_animation =
947 layer_animation_controller_->GetAnimation(Animation::FILTER);
948 return filter_animation && filter_animation->is_impl_only();
951 void LayerImpl::SetBackgroundFilters(
952 const FilterOperations& filters) {
953 if (background_filters_ == filters)
954 return;
956 background_filters_ = filters;
957 NoteLayerPropertyChanged();
960 void LayerImpl::SetMasksToBounds(bool masks_to_bounds) {
961 if (masks_to_bounds_ == masks_to_bounds)
962 return;
964 masks_to_bounds_ = masks_to_bounds;
965 NoteLayerPropertyChangedForSubtree();
968 void LayerImpl::SetContentsOpaque(bool opaque) {
969 if (contents_opaque_ == opaque)
970 return;
972 contents_opaque_ = opaque;
973 NoteLayerPropertyChangedForSubtree();
976 void LayerImpl::SetOpacity(float opacity) {
977 if (opacity_ == opacity)
978 return;
980 opacity_ = opacity;
981 NoteLayerPropertyChangedForSubtree();
984 bool LayerImpl::OpacityIsAnimating() const {
985 return layer_animation_controller_->IsAnimatingProperty(Animation::OPACITY);
988 bool LayerImpl::OpacityIsAnimatingOnImplOnly() const {
989 Animation* opacity_animation =
990 layer_animation_controller_->GetAnimation(Animation::OPACITY);
991 return opacity_animation && opacity_animation->is_impl_only();
994 void LayerImpl::SetBlendMode(SkXfermode::Mode blend_mode) {
995 if (blend_mode_ == blend_mode)
996 return;
998 blend_mode_ = blend_mode;
999 NoteLayerPropertyChangedForSubtree();
1002 void LayerImpl::SetIsRootForIsolatedGroup(bool root) {
1003 if (is_root_for_isolated_group_ == root)
1004 return;
1006 is_root_for_isolated_group_ = root;
1007 SetNeedsPushProperties();
1010 void LayerImpl::SetPosition(const gfx::PointF& position) {
1011 if (position_ == position)
1012 return;
1014 position_ = position;
1015 NoteLayerPropertyChangedForSubtree();
1018 void LayerImpl::SetShouldFlattenTransform(bool flatten) {
1019 if (should_flatten_transform_ == flatten)
1020 return;
1022 should_flatten_transform_ = flatten;
1023 NoteLayerPropertyChangedForSubtree();
1026 void LayerImpl::Set3dSortingContextId(int id) {
1027 if (id == sorting_context_id_)
1028 return;
1029 sorting_context_id_ = id;
1030 NoteLayerPropertyChangedForSubtree();
1033 void LayerImpl::PassFrameTimingRequests(
1034 std::vector<FrameTimingRequest>* requests) {
1035 frame_timing_requests_.swap(*requests);
1036 frame_timing_requests_dirty_ = true;
1037 SetNeedsPushProperties();
1040 void LayerImpl::GatherFrameTimingRequestIds(std::vector<int64_t>* request_ids) {
1041 for (const auto& request : frame_timing_requests_)
1042 request_ids->push_back(request.id());
1045 void LayerImpl::SetTransform(const gfx::Transform& transform) {
1046 if (transform_ == transform)
1047 return;
1049 transform_ = transform;
1050 transform_is_invertible_ = transform_.IsInvertible();
1051 NoteLayerPropertyChangedForSubtree();
1054 void LayerImpl::SetTransformAndInvertibility(const gfx::Transform& transform,
1055 bool transform_is_invertible) {
1056 if (transform_ == transform) {
1057 DCHECK(transform_is_invertible_ == transform_is_invertible)
1058 << "Can't change invertibility if transform is unchanged";
1059 return;
1061 transform_ = transform;
1062 transform_is_invertible_ = transform_is_invertible;
1063 NoteLayerPropertyChangedForSubtree();
1066 bool LayerImpl::TransformIsAnimating() const {
1067 return layer_animation_controller_->IsAnimatingProperty(Animation::TRANSFORM);
1070 bool LayerImpl::TransformIsAnimatingOnImplOnly() const {
1071 Animation* transform_animation =
1072 layer_animation_controller_->GetAnimation(Animation::TRANSFORM);
1073 return transform_animation && transform_animation->is_impl_only();
1076 void LayerImpl::SetUpdateRect(const gfx::Rect& update_rect) {
1077 update_rect_ = update_rect;
1078 SetNeedsPushProperties();
1081 void LayerImpl::AddDamageRect(const gfx::RectF& damage_rect) {
1082 damage_rect_ = gfx::UnionRects(damage_rect_, damage_rect);
1085 void LayerImpl::SetContentBounds(const gfx::Size& content_bounds) {
1086 if (this->content_bounds() == content_bounds)
1087 return;
1089 draw_properties_.content_bounds = content_bounds;
1090 NoteLayerPropertyChanged();
1093 void LayerImpl::SetContentsScale(float contents_scale_x,
1094 float contents_scale_y) {
1095 if (this->contents_scale_x() == contents_scale_x &&
1096 this->contents_scale_y() == contents_scale_y)
1097 return;
1099 draw_properties_.contents_scale_x = contents_scale_x;
1100 draw_properties_.contents_scale_y = contents_scale_y;
1101 NoteLayerPropertyChanged();
1104 bool LayerImpl::IsExternalFlingActive() const {
1105 return layer_tree_impl_->IsExternalFlingActive();
1108 void LayerImpl::SetCurrentScrollOffset(const gfx::ScrollOffset& scroll_offset) {
1109 DCHECK(IsActive());
1110 if (scroll_offset_->SetCurrent(scroll_offset))
1111 DidUpdateScrollOffset(false);
1114 void LayerImpl::SetCurrentScrollOffsetFromDelegate(
1115 const gfx::ScrollOffset& scroll_offset) {
1116 DCHECK(IsActive());
1117 if (scroll_offset_->SetCurrent(scroll_offset))
1118 DidUpdateScrollOffset(true);
1121 void LayerImpl::PushScrollOffsetFromMainThread(
1122 const gfx::ScrollOffset& scroll_offset) {
1123 PushScrollOffset(&scroll_offset);
1126 void LayerImpl::PushScrollOffsetFromMainThreadAndClobberActiveValue(
1127 const gfx::ScrollOffset& scroll_offset) {
1128 scroll_offset_->set_clobber_active_value();
1129 PushScrollOffset(&scroll_offset);
1132 gfx::ScrollOffset LayerImpl::PullDeltaForMainThread() {
1133 // TODO(miletus): Remove all this temporary flooring machinery when
1134 // Blink fully supports fractional scrolls.
1135 gfx::ScrollOffset current_offset = CurrentScrollOffset();
1136 gfx::Vector2dF current_delta = ScrollDelta();
1137 gfx::Vector2dF floored_delta(floor(current_delta.x()),
1138 floor(current_delta.y()));
1139 gfx::Vector2dF diff_delta = floored_delta - current_delta;
1140 gfx::ScrollOffset tmp_offset = ScrollOffsetWithDelta(current_offset,
1141 diff_delta);
1142 scroll_offset_->SetCurrent(tmp_offset);
1143 gfx::ScrollOffset delta = scroll_offset_->PullDeltaForMainThread();
1144 scroll_offset_->SetCurrent(current_offset);
1145 return delta;
1148 gfx::ScrollOffset LayerImpl::CurrentScrollOffset() const {
1149 return scroll_offset_->Current(IsActive());
1152 gfx::Vector2dF LayerImpl::ScrollDelta() const {
1153 if (IsActive())
1154 return gfx::Vector2dF(scroll_offset_->Delta().x(),
1155 scroll_offset_->Delta().y());
1156 else
1157 return gfx::Vector2dF(scroll_offset_->PendingDelta().get().x(),
1158 scroll_offset_->PendingDelta().get().y());
1161 void LayerImpl::SetScrollDelta(const gfx::Vector2dF& delta) {
1162 DCHECK(IsActive());
1163 SetCurrentScrollOffset(scroll_offset_->ActiveBase() +
1164 gfx::ScrollOffset(delta));
1167 gfx::ScrollOffset LayerImpl::BaseScrollOffset() const {
1168 if (IsActive())
1169 return scroll_offset_->ActiveBase();
1170 else
1171 return scroll_offset_->PendingBase();
1174 void LayerImpl::PushScrollOffset(const gfx::ScrollOffset* scroll_offset) {
1175 DCHECK(scroll_offset || IsActive());
1176 bool changed = false;
1177 if (scroll_offset) {
1178 DCHECK(!IsActive() || !layer_tree_impl_->FindPendingTreeLayerById(id()));
1179 changed |= scroll_offset_->PushFromMainThread(*scroll_offset);
1181 if (IsActive()) {
1182 changed |= scroll_offset_->PushPendingToActive();
1185 if (changed)
1186 DidUpdateScrollOffset(false);
1189 void LayerImpl::DidUpdateScrollOffset(bool is_from_root_delegate) {
1190 if (!is_from_root_delegate)
1191 layer_tree_impl()->DidUpdateScrollOffset(id());
1192 NoteLayerPropertyChangedForSubtree();
1193 ScrollbarParametersDidChange(false);
1196 void LayerImpl::SetDoubleSided(bool double_sided) {
1197 if (double_sided_ == double_sided)
1198 return;
1200 double_sided_ = double_sided;
1201 NoteLayerPropertyChangedForSubtree();
1204 SimpleEnclosedRegion LayerImpl::VisibleContentOpaqueRegion() const {
1205 if (contents_opaque())
1206 return SimpleEnclosedRegion(visible_content_rect());
1207 return SimpleEnclosedRegion();
1210 void LayerImpl::DidBeginTracing() {}
1212 void LayerImpl::ReleaseResources() {}
1214 void LayerImpl::RecreateResources() {
1217 gfx::ScrollOffset LayerImpl::MaxScrollOffset() const {
1218 if (!scroll_clip_layer_ || bounds().IsEmpty())
1219 return gfx::ScrollOffset();
1221 LayerImpl const* page_scale_layer = layer_tree_impl()->page_scale_layer();
1222 DCHECK(this != page_scale_layer);
1223 DCHECK(this != layer_tree_impl()->InnerViewportScrollLayer() ||
1224 IsContainerForFixedPositionLayers());
1226 float scale_factor = 1.f;
1227 for (LayerImpl const* current_layer = this;
1228 current_layer != scroll_clip_layer_->parent();
1229 current_layer = current_layer->parent()) {
1230 if (current_layer == page_scale_layer)
1231 scale_factor = layer_tree_impl()->current_page_scale_factor();
1234 gfx::SizeF scaled_scroll_bounds =
1235 gfx::ToFlooredSize(gfx::ScaleSize(BoundsForScrolling(), scale_factor));
1236 scaled_scroll_bounds = gfx::ToFlooredSize(scaled_scroll_bounds);
1238 gfx::ScrollOffset max_offset(
1239 scaled_scroll_bounds.width() - scroll_clip_layer_->bounds().width(),
1240 scaled_scroll_bounds.height() - scroll_clip_layer_->bounds().height());
1241 // We need the final scroll offset to be in CSS coords.
1242 max_offset.Scale(1 / scale_factor);
1243 max_offset.SetToMax(gfx::ScrollOffset());
1244 return max_offset;
1247 gfx::ScrollOffset LayerImpl::ClampScrollOffsetToLimits(
1248 gfx::ScrollOffset offset) const {
1249 offset.SetToMin(MaxScrollOffset());
1250 offset.SetToMax(gfx::ScrollOffset());
1251 return offset;
1254 gfx::Vector2dF LayerImpl::ClampScrollToMaxScrollOffset() {
1255 gfx::ScrollOffset old_offset = CurrentScrollOffset();
1256 gfx::ScrollOffset clamped_offset = ClampScrollOffsetToLimits(old_offset);
1257 gfx::Vector2dF delta = clamped_offset.DeltaFrom(old_offset);
1258 if (!delta.IsZero())
1259 ScrollBy(delta);
1260 return delta;
1263 void LayerImpl::SetScrollbarPosition(ScrollbarLayerImplBase* scrollbar_layer,
1264 LayerImpl* scrollbar_clip_layer,
1265 bool on_resize) const {
1266 DCHECK(scrollbar_layer);
1267 LayerImpl* page_scale_layer = layer_tree_impl()->page_scale_layer();
1269 DCHECK(this != page_scale_layer);
1270 DCHECK(scrollbar_clip_layer);
1271 gfx::RectF clip_rect(gfx::PointF(),
1272 scrollbar_clip_layer->BoundsForScrolling());
1274 // See comment in MaxScrollOffset() regarding the use of the content layer
1275 // bounds here.
1276 gfx::RectF scroll_rect(gfx::PointF(), BoundsForScrolling());
1278 if (scroll_rect.size().IsEmpty())
1279 return;
1281 gfx::ScrollOffset current_offset;
1282 for (LayerImpl const* current_layer = this;
1283 current_layer != scrollbar_clip_layer->parent();
1284 current_layer = current_layer->parent()) {
1285 current_offset += current_layer->CurrentScrollOffset();
1286 if (current_layer == page_scale_layer) {
1287 float scale_factor = layer_tree_impl()->current_page_scale_factor();
1288 current_offset.Scale(scale_factor);
1289 scroll_rect.Scale(scale_factor);
1293 bool scrollbar_needs_animation = false;
1294 scrollbar_needs_animation |= scrollbar_layer->SetVerticalAdjust(
1295 scrollbar_clip_layer->bounds_delta().y());
1296 if (scrollbar_layer->orientation() == HORIZONTAL) {
1297 float visible_ratio = clip_rect.width() / scroll_rect.width();
1298 scrollbar_needs_animation |=
1299 scrollbar_layer->SetCurrentPos(current_offset.x());
1300 scrollbar_needs_animation |=
1301 scrollbar_layer->SetMaximum(scroll_rect.width() - clip_rect.width());
1302 scrollbar_needs_animation |=
1303 scrollbar_layer->SetVisibleToTotalLengthRatio(visible_ratio);
1304 } else {
1305 float visible_ratio = clip_rect.height() / scroll_rect.height();
1306 bool y_offset_did_change =
1307 scrollbar_layer->SetCurrentPos(current_offset.y());
1308 scrollbar_needs_animation |= y_offset_did_change;
1309 scrollbar_needs_animation |=
1310 scrollbar_layer->SetMaximum(scroll_rect.height() - clip_rect.height());
1311 scrollbar_needs_animation |=
1312 scrollbar_layer->SetVisibleToTotalLengthRatio(visible_ratio);
1313 if (y_offset_did_change && layer_tree_impl()->IsActiveTree() &&
1314 this == layer_tree_impl()->InnerViewportScrollLayer()) {
1315 TRACE_COUNTER_ID1("cc", "scroll_offset_y", this->id(),
1316 current_offset.y());
1319 if (scrollbar_needs_animation) {
1320 layer_tree_impl()->set_needs_update_draw_properties();
1321 // TODO(wjmaclean) The scrollbar animator for the pinch-zoom scrollbars
1322 // should activate for every scroll on the main frame, not just the
1323 // scrolls that move the pinch virtual viewport (i.e. trigger from
1324 // either inner or outer viewport).
1325 if (scrollbar_animation_controller_) {
1326 // Non-overlay scrollbars shouldn't trigger animations.
1327 if (scrollbar_layer->is_overlay_scrollbar())
1328 scrollbar_animation_controller_->DidScrollUpdate(on_resize);
1333 void LayerImpl::DidBecomeActive() {
1334 if (layer_tree_impl_->settings().scrollbar_animator ==
1335 LayerTreeSettings::NO_ANIMATOR) {
1336 return;
1339 bool need_scrollbar_animation_controller = scrollable() && scrollbars_;
1340 if (!need_scrollbar_animation_controller) {
1341 scrollbar_animation_controller_ = nullptr;
1342 return;
1345 if (scrollbar_animation_controller_)
1346 return;
1348 scrollbar_animation_controller_ =
1349 layer_tree_impl_->CreateScrollbarAnimationController(this);
1352 void LayerImpl::ClearScrollbars() {
1353 if (!scrollbars_)
1354 return;
1356 scrollbars_.reset(nullptr);
1359 void LayerImpl::AddScrollbar(ScrollbarLayerImplBase* layer) {
1360 DCHECK(layer);
1361 DCHECK(!scrollbars_ || scrollbars_->find(layer) == scrollbars_->end());
1362 if (!scrollbars_)
1363 scrollbars_.reset(new ScrollbarSet());
1365 scrollbars_->insert(layer);
1368 void LayerImpl::RemoveScrollbar(ScrollbarLayerImplBase* layer) {
1369 DCHECK(scrollbars_);
1370 DCHECK(layer);
1371 DCHECK(scrollbars_->find(layer) != scrollbars_->end());
1373 scrollbars_->erase(layer);
1374 if (scrollbars_->empty())
1375 scrollbars_ = nullptr;
1378 bool LayerImpl::HasScrollbar(ScrollbarOrientation orientation) const {
1379 if (!scrollbars_)
1380 return false;
1382 for (ScrollbarSet::iterator it = scrollbars_->begin();
1383 it != scrollbars_->end();
1384 ++it)
1385 if ((*it)->orientation() == orientation)
1386 return true;
1388 return false;
1391 void LayerImpl::ScrollbarParametersDidChange(bool on_resize) {
1392 if (!scrollbars_)
1393 return;
1395 for (ScrollbarSet::iterator it = scrollbars_->begin();
1396 it != scrollbars_->end();
1397 ++it) {
1398 bool is_scroll_layer = (*it)->ScrollLayerId() == layer_id_;
1399 bool scroll_layer_resized = is_scroll_layer && on_resize;
1400 (*it)->ScrollbarParametersDidChange(scroll_layer_resized);
1404 void LayerImpl::SetNeedsPushProperties() {
1405 if (needs_push_properties_)
1406 return;
1407 if (!parent_should_know_need_push_properties() && parent_)
1408 parent_->AddDependentNeedsPushProperties();
1409 needs_push_properties_ = true;
1412 void LayerImpl::AddDependentNeedsPushProperties() {
1413 DCHECK_GE(num_dependents_need_push_properties_, 0);
1415 if (!parent_should_know_need_push_properties() && parent_)
1416 parent_->AddDependentNeedsPushProperties();
1418 num_dependents_need_push_properties_++;
1421 void LayerImpl::RemoveDependentNeedsPushProperties() {
1422 num_dependents_need_push_properties_--;
1423 DCHECK_GE(num_dependents_need_push_properties_, 0);
1425 if (!parent_should_know_need_push_properties() && parent_)
1426 parent_->RemoveDependentNeedsPushProperties();
1429 void LayerImpl::GetAllTilesAndPrioritiesForTracing(
1430 std::map<const Tile*, TilePriority>* tile_map) const {
1433 void LayerImpl::AsValueInto(base::trace_event::TracedValue* state) const {
1434 TracedValue::MakeDictIntoImplicitSnapshotWithCategory(
1435 TRACE_DISABLED_BY_DEFAULT("cc.debug"),
1436 state,
1437 "cc::LayerImpl",
1438 LayerTypeAsString(),
1439 this);
1440 state->SetInteger("layer_id", id());
1441 MathUtil::AddToTracedValue("bounds", bounds_, state);
1443 state->SetDouble("opacity", opacity());
1445 MathUtil::AddToTracedValue("position", position_, state);
1447 state->SetInteger("draws_content", DrawsContent());
1448 state->SetInteger("gpu_memory_usage", GPUMemoryUsageInBytes());
1450 MathUtil::AddToTracedValue(
1451 "scroll_offset", scroll_offset_ ? scroll_offset_->Current(IsActive())
1452 : gfx::ScrollOffset(),
1453 state);
1455 MathUtil::AddToTracedValue("transform_origin", transform_origin_, state);
1457 bool clipped;
1458 gfx::QuadF layer_quad = MathUtil::MapQuad(
1459 screen_space_transform(),
1460 gfx::QuadF(gfx::Rect(content_bounds())),
1461 &clipped);
1462 MathUtil::AddToTracedValue("layer_quad", layer_quad, state);
1463 if (!touch_event_handler_region_.IsEmpty()) {
1464 state->BeginArray("touch_event_handler_region");
1465 touch_event_handler_region_.AsValueInto(state);
1466 state->EndArray();
1468 if (have_wheel_event_handlers_) {
1469 gfx::Rect wheel_rect(content_bounds());
1470 Region wheel_region(wheel_rect);
1471 state->BeginArray("wheel_event_handler_region");
1472 wheel_region.AsValueInto(state);
1473 state->EndArray();
1475 if (have_scroll_event_handlers_) {
1476 gfx::Rect scroll_rect(content_bounds());
1477 Region scroll_region(scroll_rect);
1478 state->BeginArray("scroll_event_handler_region");
1479 scroll_region.AsValueInto(state);
1480 state->EndArray();
1482 if (!non_fast_scrollable_region_.IsEmpty()) {
1483 state->BeginArray("non_fast_scrollable_region");
1484 non_fast_scrollable_region_.AsValueInto(state);
1485 state->EndArray();
1487 if (scroll_blocks_on_) {
1488 state->SetInteger("scroll_blocks_on", scroll_blocks_on_);
1491 state->BeginArray("children");
1492 for (size_t i = 0; i < children_.size(); ++i) {
1493 state->BeginDictionary();
1494 children_[i]->AsValueInto(state);
1495 state->EndDictionary();
1497 state->EndArray();
1498 if (mask_layer_) {
1499 state->BeginDictionary("mask_layer");
1500 mask_layer_->AsValueInto(state);
1501 state->EndDictionary();
1503 if (replica_layer_) {
1504 state->BeginDictionary("replica_layer");
1505 replica_layer_->AsValueInto(state);
1506 state->EndDictionary();
1509 if (scroll_parent_)
1510 state->SetInteger("scroll_parent", scroll_parent_->id());
1512 if (clip_parent_)
1513 state->SetInteger("clip_parent", clip_parent_->id());
1515 state->SetBoolean("can_use_lcd_text", can_use_lcd_text());
1516 state->SetBoolean("contents_opaque", contents_opaque());
1518 state->SetBoolean(
1519 "has_animation_bounds",
1520 layer_animation_controller()->HasAnimationThatInflatesBounds());
1522 gfx::BoxF box;
1523 if (LayerUtils::GetAnimationBounds(*this, &box))
1524 MathUtil::AddToTracedValue("animation_bounds", box, state);
1526 if (debug_info_.get()) {
1527 std::string str;
1528 debug_info_->AppendAsTraceFormat(&str);
1529 base::JSONReader json_reader;
1530 scoped_ptr<base::Value> debug_info_value(json_reader.ReadToValue(str));
1532 if (debug_info_value->IsType(base::Value::TYPE_DICTIONARY)) {
1533 base::DictionaryValue* dictionary_value = nullptr;
1534 bool converted_to_dictionary =
1535 debug_info_value->GetAsDictionary(&dictionary_value);
1536 DCHECK(converted_to_dictionary);
1537 for (base::DictionaryValue::Iterator it(*dictionary_value); !it.IsAtEnd();
1538 it.Advance()) {
1539 state->SetValue(it.key().data(), it.value().DeepCopy());
1541 } else {
1542 NOTREACHED();
1546 if (!frame_timing_requests_.empty()) {
1547 state->BeginArray("frame_timing_requests");
1548 for (const auto& request : frame_timing_requests_) {
1549 state->BeginDictionary();
1550 state->SetInteger("request_id", request.id());
1551 MathUtil::AddToTracedValue("request_rect", request.rect(), state);
1552 state->EndDictionary();
1554 state->EndArray();
1558 bool LayerImpl::IsDrawnRenderSurfaceLayerListMember() const {
1559 return draw_properties_.last_drawn_render_surface_layer_list_id ==
1560 layer_tree_impl_->current_render_surface_list_id();
1563 size_t LayerImpl::GPUMemoryUsageInBytes() const { return 0; }
1565 void LayerImpl::RunMicroBenchmark(MicroBenchmarkImpl* benchmark) {
1566 benchmark->RunOnLayer(this);
1569 int LayerImpl::NumDescendantsThatDrawContent() const {
1570 return num_descendants_that_draw_content_;
1573 void LayerImpl::NotifyAnimationFinished(
1574 base::TimeTicks monotonic_time,
1575 Animation::TargetProperty target_property,
1576 int group) {
1577 if (target_property == Animation::SCROLL_OFFSET)
1578 layer_tree_impl_->InputScrollAnimationFinished();
1581 void LayerImpl::SetHasRenderSurface(bool should_have_render_surface) {
1582 if (!!render_surface() == should_have_render_surface)
1583 return;
1585 SetNeedsPushProperties();
1586 layer_tree_impl()->set_needs_update_draw_properties();
1587 if (should_have_render_surface) {
1588 render_surface_ = make_scoped_ptr(new RenderSurfaceImpl(this));
1589 return;
1591 render_surface_.reset();
1594 Region LayerImpl::GetInvalidationRegion() {
1595 return Region(update_rect_);
1598 gfx::Rect LayerImpl::GetEnclosingRectInTargetSpace() const {
1599 return MathUtil::MapEnclosingClippedRect(
1600 draw_properties_.target_space_transform,
1601 gfx::Rect(draw_properties_.content_bounds));
1604 gfx::Rect LayerImpl::GetScaledEnclosingRectInTargetSpace(float scale) const {
1605 gfx::Transform scaled_draw_transform =
1606 draw_properties_.target_space_transform;
1607 scaled_draw_transform.Scale(SK_MScalar1 / scale, SK_MScalar1 / scale);
1608 gfx::Size scaled_content_bounds =
1609 gfx::ToCeiledSize(gfx::ScaleSize(content_bounds(), scale));
1610 return MathUtil::MapEnclosingClippedRect(scaled_draw_transform,
1611 gfx::Rect(scaled_content_bounds));
1614 } // namespace cc