Add screen space opacity to opacity tree
[chromium-blink-merge.git] / cc / layers / layer_impl.cc
blobcde5339e2b3cab1c26d318c4de8107c1354d1aad
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/numerics/safe_conversions.h"
9 #include "base/strings/stringprintf.h"
10 #include "base/trace_event/trace_event.h"
11 #include "base/trace_event/trace_event_argument.h"
12 #include "cc/animation/animation_registrar.h"
13 #include "cc/animation/scrollbar_animation_controller.h"
14 #include "cc/base/math_util.h"
15 #include "cc/base/simple_enclosed_region.h"
16 #include "cc/debug/debug_colors.h"
17 #include "cc/debug/layer_tree_debug_state.h"
18 #include "cc/debug/micro_benchmark_impl.h"
19 #include "cc/debug/traced_value.h"
20 #include "cc/layers/layer_utils.h"
21 #include "cc/layers/painted_scrollbar_layer_impl.h"
22 #include "cc/output/copy_output_request.h"
23 #include "cc/quads/debug_border_draw_quad.h"
24 #include "cc/quads/render_pass.h"
25 #include "cc/trees/layer_tree_host_common.h"
26 #include "cc/trees/layer_tree_impl.h"
27 #include "cc/trees/layer_tree_settings.h"
28 #include "cc/trees/proxy.h"
29 #include "ui/gfx/geometry/box_f.h"
30 #include "ui/gfx/geometry/point_conversions.h"
31 #include "ui/gfx/geometry/quad_f.h"
32 #include "ui/gfx/geometry/rect_conversions.h"
33 #include "ui/gfx/geometry/size_conversions.h"
34 #include "ui/gfx/geometry/vector2d_conversions.h"
36 namespace cc {
37 LayerImpl::LayerImpl(LayerTreeImpl* layer_impl, int id)
38 : LayerImpl(layer_impl, id, new LayerImpl::SyncedScrollOffset) {
41 LayerImpl::LayerImpl(LayerTreeImpl* tree_impl,
42 int id,
43 scoped_refptr<SyncedScrollOffset> scroll_offset)
44 : parent_(nullptr),
45 scroll_parent_(nullptr),
46 clip_parent_(nullptr),
47 mask_layer_id_(-1),
48 replica_layer_id_(-1),
49 layer_id_(id),
50 layer_tree_impl_(tree_impl),
51 scroll_offset_(scroll_offset),
52 scroll_clip_layer_(nullptr),
53 should_scroll_on_main_thread_(false),
54 have_wheel_event_handlers_(false),
55 have_scroll_event_handlers_(false),
56 scroll_blocks_on_(SCROLL_BLOCKS_ON_NONE),
57 user_scrollable_horizontal_(true),
58 user_scrollable_vertical_(true),
59 stacking_order_changed_(false),
60 double_sided_(true),
61 should_flatten_transform_(true),
62 should_flatten_transform_from_property_tree_(false),
63 layer_property_changed_(false),
64 masks_to_bounds_(false),
65 contents_opaque_(false),
66 is_root_for_isolated_group_(false),
67 use_parent_backface_visibility_(false),
68 draw_checkerboard_for_missing_tiles_(false),
69 draws_content_(false),
70 hide_layer_and_subtree_(false),
71 transform_is_invertible_(true),
72 is_container_for_fixed_position_layers_(false),
73 background_color_(0),
74 opacity_(1.0),
75 blend_mode_(SkXfermode::kSrcOver_Mode),
76 num_descendants_that_draw_content_(0),
77 transform_tree_index_(-1),
78 opacity_tree_index_(-1),
79 clip_tree_index_(-1),
80 draw_depth_(0.f),
81 needs_push_properties_(false),
82 num_dependents_need_push_properties_(0),
83 sorting_context_id_(0),
84 current_draw_mode_(DRAW_MODE_NONE),
85 frame_timing_requests_dirty_(false),
86 visited_(false),
87 layer_or_descendant_is_drawn_(false),
88 sorted_for_recursion_(false) {
89 DCHECK_GT(layer_id_, 0);
90 DCHECK(layer_tree_impl_);
91 layer_tree_impl_->RegisterLayer(this);
92 AnimationRegistrar* registrar = layer_tree_impl_->GetAnimationRegistrar();
93 layer_animation_controller_ =
94 registrar->GetAnimationControllerForId(layer_id_);
95 layer_animation_controller_->AddValueObserver(this);
96 if (IsActive()) {
97 layer_animation_controller_->set_value_provider(this);
98 layer_animation_controller_->set_layer_animation_delegate(this);
100 SetNeedsPushProperties();
103 LayerImpl::~LayerImpl() {
104 DCHECK_EQ(DRAW_MODE_NONE, current_draw_mode_);
106 layer_animation_controller_->RemoveValueObserver(this);
107 layer_animation_controller_->remove_value_provider(this);
108 layer_animation_controller_->remove_layer_animation_delegate(this);
110 if (!copy_requests_.empty() && layer_tree_impl_->IsActiveTree())
111 layer_tree_impl()->RemoveLayerWithCopyOutputRequest(this);
112 layer_tree_impl_->UnregisterLayer(this);
114 TRACE_EVENT_OBJECT_DELETED_WITH_ID(
115 TRACE_DISABLED_BY_DEFAULT("cc.debug"), "cc::LayerImpl", this);
118 void LayerImpl::AddChild(scoped_ptr<LayerImpl> child) {
119 child->SetParent(this);
120 DCHECK_EQ(layer_tree_impl(), child->layer_tree_impl());
121 children_.push_back(child.Pass());
122 layer_tree_impl()->set_needs_update_draw_properties();
125 scoped_ptr<LayerImpl> LayerImpl::RemoveChild(LayerImpl* child) {
126 for (OwnedLayerImplList::iterator it = children_.begin();
127 it != children_.end();
128 ++it) {
129 if (*it == child) {
130 scoped_ptr<LayerImpl> ret = children_.take(it);
131 children_.erase(it);
132 layer_tree_impl()->set_needs_update_draw_properties();
133 return ret.Pass();
136 return nullptr;
139 void LayerImpl::SetParent(LayerImpl* parent) {
140 if (parent_should_know_need_push_properties()) {
141 if (parent_)
142 parent_->RemoveDependentNeedsPushProperties();
143 if (parent)
144 parent->AddDependentNeedsPushProperties();
146 parent_ = parent;
149 void LayerImpl::ClearChildList() {
150 if (children_.empty())
151 return;
153 children_.clear();
154 layer_tree_impl()->set_needs_update_draw_properties();
157 bool LayerImpl::HasAncestor(const LayerImpl* ancestor) const {
158 if (!ancestor)
159 return false;
161 for (const LayerImpl* layer = this; layer; layer = layer->parent()) {
162 if (layer == ancestor)
163 return true;
166 return false;
169 void LayerImpl::SetScrollParent(LayerImpl* parent) {
170 if (scroll_parent_ == parent)
171 return;
173 if (parent)
174 DCHECK_EQ(layer_tree_impl()->LayerById(parent->id()), parent);
176 scroll_parent_ = parent;
177 SetNeedsPushProperties();
180 void LayerImpl::SetDebugInfo(
181 scoped_refptr<base::trace_event::ConvertableToTraceFormat> other) {
182 debug_info_ = other;
183 SetNeedsPushProperties();
186 void LayerImpl::SetScrollChildren(std::set<LayerImpl*>* children) {
187 if (scroll_children_.get() == children)
188 return;
189 scroll_children_.reset(children);
190 SetNeedsPushProperties();
193 void LayerImpl::SetNumDescendantsThatDrawContent(int num_descendants) {
194 if (num_descendants_that_draw_content_ == num_descendants)
195 return;
196 num_descendants_that_draw_content_ = num_descendants;
197 SetNeedsPushProperties();
200 void LayerImpl::SetClipParent(LayerImpl* ancestor) {
201 if (clip_parent_ == ancestor)
202 return;
204 clip_parent_ = ancestor;
205 SetNeedsPushProperties();
208 void LayerImpl::SetClipChildren(std::set<LayerImpl*>* children) {
209 if (clip_children_.get() == children)
210 return;
211 clip_children_.reset(children);
212 SetNeedsPushProperties();
215 void LayerImpl::SetTransformTreeIndex(int index) {
216 transform_tree_index_ = index;
217 SetNeedsPushProperties();
220 void LayerImpl::SetClipTreeIndex(int index) {
221 clip_tree_index_ = index;
222 SetNeedsPushProperties();
225 void LayerImpl::SetOpacityTreeIndex(int index) {
226 opacity_tree_index_ = index;
227 SetNeedsPushProperties();
230 void LayerImpl::PassCopyRequests(ScopedPtrVector<CopyOutputRequest>* requests) {
231 // In the case that a layer still has a copy request, this means that there's
232 // a commit to the active tree without a draw. This only happens in some
233 // edge cases during lost context or visibility changes, so don't try to
234 // handle preserving these output requests (and their surface).
235 if (!copy_requests_.empty()) {
236 layer_tree_impl()->RemoveLayerWithCopyOutputRequest(this);
237 // Destroying these will abort them.
238 copy_requests_.clear();
241 if (requests->empty())
242 return;
244 DCHECK(render_surface());
245 bool was_empty = copy_requests_.empty();
246 copy_requests_.insert_and_take(copy_requests_.end(), requests);
247 requests->clear();
249 if (was_empty && layer_tree_impl()->IsActiveTree())
250 layer_tree_impl()->AddLayerWithCopyOutputRequest(this);
251 NoteLayerPropertyChangedForSubtree();
254 void LayerImpl::TakeCopyRequestsAndTransformToTarget(
255 ScopedPtrVector<CopyOutputRequest>* requests) {
256 DCHECK(!copy_requests_.empty());
257 DCHECK(layer_tree_impl()->IsActiveTree());
258 DCHECK_EQ(render_target(), this);
260 size_t first_inserted_request = requests->size();
261 requests->insert_and_take(requests->end(), &copy_requests_);
262 copy_requests_.clear();
264 for (size_t i = first_inserted_request; i < requests->size(); ++i) {
265 CopyOutputRequest* request = requests->at(i);
266 if (!request->has_area())
267 continue;
269 gfx::Rect request_in_layer_space = request->area();
270 request_in_layer_space.Intersect(gfx::Rect(bounds()));
271 request->set_area(MathUtil::MapEnclosingClippedRect(
272 draw_properties_.target_space_transform, request_in_layer_space));
275 layer_tree_impl()->RemoveLayerWithCopyOutputRequest(this);
276 layer_tree_impl()->set_needs_update_draw_properties();
279 void LayerImpl::ClearRenderSurfaceLayerList() {
280 if (render_surface_)
281 render_surface_->ClearLayerLists();
284 void LayerImpl::PopulateSharedQuadState(SharedQuadState* state) const {
285 state->SetAll(draw_properties_.target_space_transform, bounds(),
286 draw_properties_.visible_layer_rect, draw_properties_.clip_rect,
287 draw_properties_.is_clipped, draw_properties_.opacity,
288 draw_properties_.blend_mode, sorting_context_id_);
291 void LayerImpl::PopulateScaledSharedQuadState(SharedQuadState* state,
292 float scale) const {
293 gfx::Transform scaled_draw_transform =
294 draw_properties_.target_space_transform;
295 scaled_draw_transform.Scale(SK_MScalar1 / scale, SK_MScalar1 / scale);
296 gfx::Size scaled_bounds = gfx::ToCeiledSize(gfx::ScaleSize(bounds(), scale));
297 gfx::Rect scaled_visible_layer_rect =
298 gfx::ScaleToEnclosingRect(visible_layer_rect(), scale);
299 scaled_visible_layer_rect.Intersect(gfx::Rect(scaled_bounds));
301 state->SetAll(scaled_draw_transform, scaled_bounds, scaled_visible_layer_rect,
302 draw_properties().clip_rect, draw_properties().is_clipped,
303 draw_properties().opacity, draw_properties().blend_mode,
304 sorting_context_id_);
307 bool LayerImpl::WillDraw(DrawMode draw_mode,
308 ResourceProvider* resource_provider) {
309 // WillDraw/DidDraw must be matched.
310 DCHECK_NE(DRAW_MODE_NONE, draw_mode);
311 DCHECK_EQ(DRAW_MODE_NONE, current_draw_mode_);
312 current_draw_mode_ = draw_mode;
313 return true;
316 void LayerImpl::DidDraw(ResourceProvider* resource_provider) {
317 DCHECK_NE(DRAW_MODE_NONE, current_draw_mode_);
318 current_draw_mode_ = DRAW_MODE_NONE;
321 bool LayerImpl::ShowDebugBorders() const {
322 return layer_tree_impl()->debug_state().show_debug_borders;
325 void LayerImpl::GetDebugBorderProperties(SkColor* color, float* width) const {
326 if (draws_content_) {
327 *color = DebugColors::ContentLayerBorderColor();
328 *width = DebugColors::ContentLayerBorderWidth(layer_tree_impl());
329 return;
332 if (masks_to_bounds_) {
333 *color = DebugColors::MaskingLayerBorderColor();
334 *width = DebugColors::MaskingLayerBorderWidth(layer_tree_impl());
335 return;
338 *color = DebugColors::ContainerLayerBorderColor();
339 *width = DebugColors::ContainerLayerBorderWidth(layer_tree_impl());
342 void LayerImpl::AppendDebugBorderQuad(
343 RenderPass* render_pass,
344 const gfx::Size& bounds,
345 const SharedQuadState* shared_quad_state,
346 AppendQuadsData* append_quads_data) const {
347 SkColor color;
348 float width;
349 GetDebugBorderProperties(&color, &width);
350 AppendDebugBorderQuad(render_pass, bounds, shared_quad_state,
351 append_quads_data, color, width);
354 void LayerImpl::AppendDebugBorderQuad(RenderPass* render_pass,
355 const gfx::Size& bounds,
356 const SharedQuadState* shared_quad_state,
357 AppendQuadsData* append_quads_data,
358 SkColor color,
359 float width) const {
360 if (!ShowDebugBorders())
361 return;
363 gfx::Rect quad_rect(bounds);
364 gfx::Rect visible_quad_rect(quad_rect);
365 DebugBorderDrawQuad* debug_border_quad =
366 render_pass->CreateAndAppendDrawQuad<DebugBorderDrawQuad>();
367 debug_border_quad->SetNew(
368 shared_quad_state, quad_rect, visible_quad_rect, color, width);
369 if (contents_opaque()) {
370 // When opaque, draw a second inner border that is thicker than the outer
371 // border, but more transparent.
372 static const float kFillOpacity = 0.3f;
373 SkColor fill_color = SkColorSetA(
374 color, static_cast<uint8_t>(SkColorGetA(color) * kFillOpacity));
375 float fill_width = width * 3;
376 gfx::Rect fill_rect = quad_rect;
377 fill_rect.Inset(fill_width / 2.f, fill_width / 2.f);
378 if (fill_rect.IsEmpty())
379 return;
380 gfx::Rect visible_fill_rect =
381 gfx::IntersectRects(visible_quad_rect, fill_rect);
382 DebugBorderDrawQuad* fill_quad =
383 render_pass->CreateAndAppendDrawQuad<DebugBorderDrawQuad>();
384 fill_quad->SetNew(shared_quad_state, fill_rect, visible_fill_rect,
385 fill_color, fill_width);
389 bool LayerImpl::HasDelegatedContent() const {
390 return false;
393 bool LayerImpl::HasContributingDelegatedRenderPasses() const {
394 return false;
397 RenderPassId LayerImpl::FirstContributingRenderPassId() const {
398 return RenderPassId(0, 0);
401 RenderPassId LayerImpl::NextContributingRenderPassId(RenderPassId id) const {
402 return RenderPassId(0, 0);
405 void LayerImpl::GetContentsResourceId(ResourceId* resource_id,
406 gfx::Size* resource_size) const {
407 NOTREACHED();
408 *resource_id = 0;
411 gfx::Vector2dF LayerImpl::ScrollBy(const gfx::Vector2dF& scroll) {
412 gfx::ScrollOffset adjusted_scroll(scroll);
413 if (!user_scrollable_horizontal_)
414 adjusted_scroll.set_x(0);
415 if (!user_scrollable_vertical_)
416 adjusted_scroll.set_y(0);
417 DCHECK(scrollable());
418 gfx::ScrollOffset old_offset = CurrentScrollOffset();
419 gfx::ScrollOffset new_offset =
420 ClampScrollOffsetToLimits(old_offset + adjusted_scroll);
421 SetCurrentScrollOffset(new_offset);
423 gfx::ScrollOffset unscrolled =
424 old_offset + gfx::ScrollOffset(scroll) - new_offset;
425 return gfx::Vector2dF(unscrolled.x(), unscrolled.y());
428 void LayerImpl::SetScrollClipLayer(int scroll_clip_layer_id) {
429 scroll_clip_layer_ = layer_tree_impl()->LayerById(scroll_clip_layer_id);
432 bool LayerImpl::user_scrollable(ScrollbarOrientation orientation) const {
433 return (orientation == HORIZONTAL) ? user_scrollable_horizontal_
434 : user_scrollable_vertical_;
437 void LayerImpl::ApplySentScrollDeltasFromAbortedCommit() {
438 DCHECK(layer_tree_impl()->IsActiveTree());
439 scroll_offset_->AbortCommit();
442 InputHandler::ScrollStatus LayerImpl::TryScroll(
443 const gfx::PointF& screen_space_point,
444 InputHandler::ScrollInputType type,
445 ScrollBlocksOn effective_block_mode) const {
446 if (should_scroll_on_main_thread()) {
447 TRACE_EVENT0("cc", "LayerImpl::TryScroll: Failed ShouldScrollOnMainThread");
448 return InputHandler::SCROLL_ON_MAIN_THREAD;
451 if (!screen_space_transform().IsInvertible()) {
452 TRACE_EVENT0("cc", "LayerImpl::TryScroll: Ignored NonInvertibleTransform");
453 return InputHandler::SCROLL_IGNORED;
456 if (!non_fast_scrollable_region().IsEmpty()) {
457 bool clipped = false;
458 gfx::Transform inverse_screen_space_transform(
459 gfx::Transform::kSkipInitialization);
460 if (!screen_space_transform().GetInverse(&inverse_screen_space_transform)) {
461 // TODO(shawnsingh): We shouldn't be applying a projection if screen space
462 // transform is uninvertible here. Perhaps we should be returning
463 // SCROLL_ON_MAIN_THREAD in this case?
466 gfx::PointF hit_test_point_in_layer_space = MathUtil::ProjectPoint(
467 inverse_screen_space_transform, screen_space_point, &clipped);
468 if (!clipped &&
469 non_fast_scrollable_region().Contains(
470 gfx::ToRoundedPoint(hit_test_point_in_layer_space))) {
471 TRACE_EVENT0("cc",
472 "LayerImpl::tryScroll: Failed NonFastScrollableRegion");
473 return InputHandler::SCROLL_ON_MAIN_THREAD;
477 if (have_scroll_event_handlers() &&
478 effective_block_mode & SCROLL_BLOCKS_ON_SCROLL_EVENT) {
479 TRACE_EVENT0("cc", "LayerImpl::tryScroll: Failed ScrollEventHandlers");
480 return InputHandler::SCROLL_ON_MAIN_THREAD;
483 if (type == InputHandler::WHEEL && have_wheel_event_handlers() &&
484 effective_block_mode & SCROLL_BLOCKS_ON_WHEEL_EVENT) {
485 TRACE_EVENT0("cc", "LayerImpl::tryScroll: Failed WheelEventHandlers");
486 return InputHandler::SCROLL_ON_MAIN_THREAD;
489 if (!scrollable()) {
490 TRACE_EVENT0("cc", "LayerImpl::tryScroll: Ignored not scrollable");
491 return InputHandler::SCROLL_IGNORED;
494 gfx::ScrollOffset max_scroll_offset = MaxScrollOffset();
495 if (max_scroll_offset.x() <= 0 && max_scroll_offset.y() <= 0) {
496 TRACE_EVENT0("cc",
497 "LayerImpl::tryScroll: Ignored. Technically scrollable,"
498 " but has no affordance in either direction.");
499 return InputHandler::SCROLL_IGNORED;
502 return InputHandler::SCROLL_STARTED;
505 skia::RefPtr<SkPicture> LayerImpl::GetPicture() {
506 return skia::RefPtr<SkPicture>();
509 scoped_ptr<LayerImpl> LayerImpl::CreateLayerImpl(LayerTreeImpl* tree_impl) {
510 return LayerImpl::Create(tree_impl, layer_id_, scroll_offset_);
513 void LayerImpl::PushPropertiesTo(LayerImpl* layer) {
514 layer->SetTransformOrigin(transform_origin_);
515 layer->SetBackgroundColor(background_color_);
516 layer->SetBounds(bounds_);
517 layer->SetDoubleSided(double_sided_);
518 layer->SetDrawCheckerboardForMissingTiles(
519 draw_checkerboard_for_missing_tiles_);
520 layer->SetDrawsContent(DrawsContent());
521 layer->SetHideLayerAndSubtree(hide_layer_and_subtree_);
522 layer->SetHasRenderSurface(!!render_surface());
523 layer->SetFilters(filters());
524 layer->SetBackgroundFilters(background_filters());
525 layer->SetMasksToBounds(masks_to_bounds_);
526 layer->SetShouldScrollOnMainThread(should_scroll_on_main_thread_);
527 layer->SetHaveWheelEventHandlers(have_wheel_event_handlers_);
528 layer->SetHaveScrollEventHandlers(have_scroll_event_handlers_);
529 layer->SetScrollBlocksOn(scroll_blocks_on_);
530 layer->SetNonFastScrollableRegion(non_fast_scrollable_region_);
531 layer->SetTouchEventHandlerRegion(touch_event_handler_region_);
532 layer->SetContentsOpaque(contents_opaque_);
533 layer->SetOpacity(opacity_);
534 layer->SetBlendMode(blend_mode_);
535 layer->SetIsRootForIsolatedGroup(is_root_for_isolated_group_);
536 layer->SetPosition(position_);
537 layer->SetIsContainerForFixedPositionLayers(
538 is_container_for_fixed_position_layers_);
539 layer->SetPositionConstraint(position_constraint_);
540 layer->SetShouldFlattenTransform(should_flatten_transform_);
541 layer->set_should_flatten_transform_from_property_tree(
542 should_flatten_transform_from_property_tree_);
543 layer->SetUseParentBackfaceVisibility(use_parent_backface_visibility_);
544 layer->SetTransformAndInvertibility(transform_, transform_is_invertible_);
546 layer->SetScrollClipLayer(scroll_clip_layer_ ? scroll_clip_layer_->id()
547 : Layer::INVALID_ID);
548 layer->set_user_scrollable_horizontal(user_scrollable_horizontal_);
549 layer->set_user_scrollable_vertical(user_scrollable_vertical_);
551 layer->SetScrollCompensationAdjustment(scroll_compensation_adjustment_);
553 layer->PushScrollOffset(nullptr);
555 layer->Set3dSortingContextId(sorting_context_id_);
556 layer->SetNumDescendantsThatDrawContent(num_descendants_that_draw_content_);
558 layer->SetTransformTreeIndex(transform_tree_index_);
559 layer->SetClipTreeIndex(clip_tree_index_);
560 layer->SetOpacityTreeIndex(opacity_tree_index_);
561 layer->set_offset_to_transform_parent(offset_to_transform_parent_);
563 LayerImpl* scroll_parent = nullptr;
564 if (scroll_parent_) {
565 scroll_parent = layer->layer_tree_impl()->LayerById(scroll_parent_->id());
566 DCHECK(scroll_parent);
569 layer->SetScrollParent(scroll_parent);
570 if (scroll_children_) {
571 std::set<LayerImpl*>* scroll_children = new std::set<LayerImpl*>;
572 for (std::set<LayerImpl*>::iterator it = scroll_children_->begin();
573 it != scroll_children_->end();
574 ++it) {
575 DCHECK_EQ((*it)->scroll_parent(), this);
576 LayerImpl* scroll_child =
577 layer->layer_tree_impl()->LayerById((*it)->id());
578 DCHECK(scroll_child);
579 scroll_children->insert(scroll_child);
581 layer->SetScrollChildren(scroll_children);
582 } else {
583 layer->SetScrollChildren(nullptr);
586 LayerImpl* clip_parent = nullptr;
587 if (clip_parent_) {
588 clip_parent = layer->layer_tree_impl()->LayerById(
589 clip_parent_->id());
590 DCHECK(clip_parent);
593 layer->SetClipParent(clip_parent);
594 if (clip_children_) {
595 std::set<LayerImpl*>* clip_children = new std::set<LayerImpl*>;
596 for (std::set<LayerImpl*>::iterator it = clip_children_->begin();
597 it != clip_children_->end(); ++it)
598 clip_children->insert(layer->layer_tree_impl()->LayerById((*it)->id()));
599 layer->SetClipChildren(clip_children);
600 } else {
601 layer->SetClipChildren(nullptr);
604 layer->PassCopyRequests(&copy_requests_);
606 // If the main thread commits multiple times before the impl thread actually
607 // draws, then damage tracking will become incorrect if we simply clobber the
608 // update_rect here. The LayerImpl's update_rect needs to accumulate (i.e.
609 // union) any update changes that have occurred on the main thread.
610 update_rect_.Union(layer->update_rect());
611 layer->SetUpdateRect(update_rect_);
613 layer->SetStackingOrderChanged(stacking_order_changed_);
614 layer->SetDebugInfo(debug_info_);
616 if (frame_timing_requests_dirty_) {
617 layer->SetFrameTimingRequests(frame_timing_requests_);
618 frame_timing_requests_dirty_ = false;
621 // Reset any state that should be cleared for the next update.
622 stacking_order_changed_ = false;
623 update_rect_ = gfx::Rect();
624 needs_push_properties_ = false;
625 num_dependents_need_push_properties_ = 0;
628 gfx::Vector2dF LayerImpl::FixedContainerSizeDelta() const {
629 if (!scroll_clip_layer_)
630 return gfx::Vector2dF();
632 gfx::Vector2dF delta_from_scroll = scroll_clip_layer_->bounds_delta();
634 // In virtual-viewport mode, we don't need to compensate for pinch zoom or
635 // scale since the fixed container is the outer viewport, which sits below
636 // the page scale.
637 return delta_from_scroll;
640 base::DictionaryValue* LayerImpl::LayerTreeAsJson() const {
641 base::DictionaryValue* result = new base::DictionaryValue;
642 result->SetInteger("LayerId", id());
643 result->SetString("LayerType", LayerTypeAsString());
645 base::ListValue* list = new base::ListValue;
646 list->AppendInteger(bounds().width());
647 list->AppendInteger(bounds().height());
648 result->Set("Bounds", list);
650 list = new base::ListValue;
651 list->AppendDouble(position_.x());
652 list->AppendDouble(position_.y());
653 result->Set("Position", list);
655 const gfx::Transform& gfx_transform = draw_properties_.target_space_transform;
656 double transform[16];
657 gfx_transform.matrix().asColMajord(transform);
658 list = new base::ListValue;
659 for (int i = 0; i < 16; ++i)
660 list->AppendDouble(transform[i]);
661 result->Set("DrawTransform", list);
663 result->SetBoolean("DrawsContent", draws_content_);
664 result->SetBoolean("Is3dSorted", Is3dSorted());
665 result->SetDouble("OPACITY", opacity());
666 result->SetBoolean("ContentsOpaque", contents_opaque_);
668 if (scrollable())
669 result->SetBoolean("Scrollable", true);
671 if (have_wheel_event_handlers_)
672 result->SetBoolean("WheelHandler", have_wheel_event_handlers_);
673 if (have_scroll_event_handlers_)
674 result->SetBoolean("ScrollHandler", have_scroll_event_handlers_);
675 if (!touch_event_handler_region_.IsEmpty()) {
676 scoped_ptr<base::Value> region = touch_event_handler_region_.AsValue();
677 result->Set("TouchRegion", region.release());
680 if (scroll_blocks_on_) {
681 list = new base::ListValue;
682 if (scroll_blocks_on_ & SCROLL_BLOCKS_ON_START_TOUCH)
683 list->AppendString("StartTouch");
684 if (scroll_blocks_on_ & SCROLL_BLOCKS_ON_WHEEL_EVENT)
685 list->AppendString("WheelEvent");
686 if (scroll_blocks_on_ & SCROLL_BLOCKS_ON_SCROLL_EVENT)
687 list->AppendString("ScrollEvent");
688 result->Set("ScrollBlocksOn", list);
691 list = new base::ListValue;
692 for (size_t i = 0; i < children_.size(); ++i)
693 list->Append(children_[i]->LayerTreeAsJson());
694 result->Set("Children", list);
696 return result;
699 void LayerImpl::SetStackingOrderChanged(bool stacking_order_changed) {
700 if (stacking_order_changed) {
701 stacking_order_changed_ = true;
702 NoteLayerPropertyChangedForSubtree();
706 void LayerImpl::NoteLayerPropertyChanged() {
707 layer_property_changed_ = true;
708 layer_tree_impl()->set_needs_update_draw_properties();
709 SetNeedsPushProperties();
712 void LayerImpl::NoteLayerPropertyChangedForSubtree() {
713 layer_property_changed_ = true;
714 layer_tree_impl()->set_needs_update_draw_properties();
715 for (size_t i = 0; i < children_.size(); ++i)
716 children_[i]->NoteLayerPropertyChangedForDescendantsInternal();
717 SetNeedsPushProperties();
720 void LayerImpl::NoteLayerPropertyChangedForDescendantsInternal() {
721 layer_property_changed_ = true;
722 for (size_t i = 0; i < children_.size(); ++i)
723 children_[i]->NoteLayerPropertyChangedForDescendantsInternal();
726 void LayerImpl::NoteLayerPropertyChangedForDescendants() {
727 layer_tree_impl()->set_needs_update_draw_properties();
728 for (size_t i = 0; i < children_.size(); ++i)
729 children_[i]->NoteLayerPropertyChangedForDescendantsInternal();
730 SetNeedsPushProperties();
733 void LayerImpl::ValidateQuadResourcesInternal(DrawQuad* quad) const {
734 #if DCHECK_IS_ON()
735 const ResourceProvider* resource_provider =
736 layer_tree_impl_->resource_provider();
737 for (ResourceId resource_id : quad->resources)
738 resource_provider->ValidateResource(resource_id);
739 #endif
742 const char* LayerImpl::LayerTypeAsString() const {
743 return "cc::LayerImpl";
746 void LayerImpl::ResetAllChangeTrackingForSubtree() {
747 layer_property_changed_ = false;
749 update_rect_ = gfx::Rect();
750 damage_rect_ = gfx::RectF();
752 if (render_surface_)
753 render_surface_->ResetPropertyChangedFlag();
755 if (mask_layer_)
756 mask_layer_->ResetAllChangeTrackingForSubtree();
758 if (replica_layer_) {
759 // This also resets the replica mask, if it exists.
760 replica_layer_->ResetAllChangeTrackingForSubtree();
763 for (size_t i = 0; i < children_.size(); ++i)
764 children_[i]->ResetAllChangeTrackingForSubtree();
766 needs_push_properties_ = false;
767 num_dependents_need_push_properties_ = 0;
770 void LayerImpl::UpdatePropertyTreeTransform() {
771 if (transform_tree_index_ != -1) {
772 TransformTree& transform_tree =
773 layer_tree_impl()->property_trees()->transform_tree;
774 TransformNode* node = transform_tree.Node(transform_tree_index_);
775 if (node->data.local != transform_) {
776 node->data.local = transform_;
777 node->data.needs_local_transform_update = true;
778 transform_tree.set_needs_update(true);
779 // TODO(ajuma): The current criteria for creating clip nodes means that
780 // property trees may need to be rebuilt when the new transform isn't
781 // axis-aligned wrt the old transform (see Layer::SetTransform). Since
782 // rebuilding property trees every frame of a transform animation is
783 // something we should try to avoid, change property tree-building so that
784 // it doesn't depend on axis aliginment.
789 void LayerImpl::UpdatePropertyTreeOpacity() {
790 if (opacity_tree_index_ != -1) {
791 OpacityTree& opacity_tree =
792 layer_tree_impl()->property_trees()->opacity_tree;
793 OpacityNode* node = opacity_tree.Node(opacity_tree_index_);
794 node->data.opacity = opacity_;
795 opacity_tree.set_needs_update(true);
799 gfx::ScrollOffset LayerImpl::ScrollOffsetForAnimation() const {
800 return CurrentScrollOffset();
803 void LayerImpl::OnFilterAnimated(const FilterOperations& filters) {
804 SetFilters(filters);
807 void LayerImpl::OnOpacityAnimated(float opacity) {
808 SetOpacity(opacity);
809 UpdatePropertyTreeOpacity();
812 void LayerImpl::OnTransformAnimated(const gfx::Transform& transform) {
813 SetTransform(transform);
814 UpdatePropertyTreeTransform();
817 void LayerImpl::OnScrollOffsetAnimated(const gfx::ScrollOffset& scroll_offset) {
818 // Only layers in the active tree should need to do anything here, since
819 // layers in the pending tree will find out about these changes as a
820 // result of the shared SyncedProperty.
821 if (!IsActive())
822 return;
824 SetCurrentScrollOffset(scroll_offset);
826 layer_tree_impl_->DidAnimateScrollOffset();
829 void LayerImpl::OnAnimationWaitingForDeletion() {}
831 bool LayerImpl::IsActive() const {
832 return layer_tree_impl_->IsActiveTree();
835 gfx::Size LayerImpl::bounds() const {
836 gfx::Vector2d delta = gfx::ToCeiledVector2d(bounds_delta_);
837 return gfx::Size(bounds_.width() + delta.x(),
838 bounds_.height() + delta.y());
841 gfx::SizeF LayerImpl::BoundsForScrolling() const {
842 return gfx::SizeF(bounds_.width() + bounds_delta_.x(),
843 bounds_.height() + bounds_delta_.y());
846 void LayerImpl::SetBounds(const gfx::Size& bounds) {
847 if (bounds_ == bounds)
848 return;
850 bounds_ = bounds;
852 ScrollbarParametersDidChange(true);
853 if (masks_to_bounds())
854 NoteLayerPropertyChangedForSubtree();
855 else
856 NoteLayerPropertyChanged();
859 void LayerImpl::SetBoundsDelta(const gfx::Vector2dF& bounds_delta) {
860 DCHECK(IsActive());
861 if (bounds_delta_ == bounds_delta)
862 return;
864 bounds_delta_ = bounds_delta;
866 TransformTree& transform_tree =
867 layer_tree_impl()->property_trees()->transform_tree;
868 if (this == layer_tree_impl()->InnerViewportContainerLayer()) {
869 transform_tree.set_inner_viewport_bounds_delta(bounds_delta);
870 transform_tree.set_needs_update(true);
871 } else if (this == layer_tree_impl()->OuterViewportContainerLayer()) {
872 transform_tree.set_outer_viewport_bounds_delta(bounds_delta);
873 transform_tree.set_needs_update(true);
876 ScrollbarParametersDidChange(true);
878 if (masks_to_bounds()) {
879 // If layer is clipping, then update the clip node using the new bounds.
880 ClipNode* clip_node =
881 layer_tree_impl()->property_trees()->clip_tree.Node(clip_tree_index());
882 if (clip_node) {
883 DCHECK(id() == clip_node->owner_id);
884 clip_node->data.clip =
885 gfx::RectF(gfx::PointF() + offset_to_transform_parent(), bounds());
886 layer_tree_impl()->property_trees()->clip_tree.set_needs_update(true);
889 NoteLayerPropertyChangedForSubtree();
890 } else {
891 NoteLayerPropertyChanged();
895 void LayerImpl::SetMaskLayer(scoped_ptr<LayerImpl> mask_layer) {
896 int new_layer_id = mask_layer ? mask_layer->id() : -1;
898 if (mask_layer) {
899 DCHECK_EQ(layer_tree_impl(), mask_layer->layer_tree_impl());
900 DCHECK_NE(new_layer_id, mask_layer_id_);
901 } else if (new_layer_id == mask_layer_id_) {
902 return;
905 mask_layer_ = mask_layer.Pass();
906 mask_layer_id_ = new_layer_id;
907 if (mask_layer_)
908 mask_layer_->SetParent(this);
909 NoteLayerPropertyChangedForSubtree();
912 scoped_ptr<LayerImpl> LayerImpl::TakeMaskLayer() {
913 mask_layer_id_ = -1;
914 return mask_layer_.Pass();
917 void LayerImpl::SetReplicaLayer(scoped_ptr<LayerImpl> replica_layer) {
918 int new_layer_id = replica_layer ? replica_layer->id() : -1;
920 if (replica_layer) {
921 DCHECK_EQ(layer_tree_impl(), replica_layer->layer_tree_impl());
922 DCHECK_NE(new_layer_id, replica_layer_id_);
923 } else if (new_layer_id == replica_layer_id_) {
924 return;
927 replica_layer_ = replica_layer.Pass();
928 replica_layer_id_ = new_layer_id;
929 if (replica_layer_)
930 replica_layer_->SetParent(this);
931 NoteLayerPropertyChangedForSubtree();
934 scoped_ptr<LayerImpl> LayerImpl::TakeReplicaLayer() {
935 replica_layer_id_ = -1;
936 return replica_layer_.Pass();
939 ScrollbarLayerImplBase* LayerImpl::ToScrollbarLayer() {
940 return nullptr;
943 void LayerImpl::SetDrawsContent(bool draws_content) {
944 if (draws_content_ == draws_content)
945 return;
947 draws_content_ = draws_content;
948 NoteLayerPropertyChanged();
951 void LayerImpl::SetHideLayerAndSubtree(bool hide) {
952 if (hide_layer_and_subtree_ == hide)
953 return;
955 hide_layer_and_subtree_ = hide;
956 NoteLayerPropertyChangedForSubtree();
959 void LayerImpl::SetTransformOrigin(const gfx::Point3F& transform_origin) {
960 if (transform_origin_ == transform_origin)
961 return;
962 transform_origin_ = transform_origin;
963 NoteLayerPropertyChangedForSubtree();
966 void LayerImpl::SetBackgroundColor(SkColor background_color) {
967 if (background_color_ == background_color)
968 return;
970 background_color_ = background_color;
971 NoteLayerPropertyChanged();
974 SkColor LayerImpl::SafeOpaqueBackgroundColor() const {
975 SkColor color = background_color();
976 if (SkColorGetA(color) == 255 && !contents_opaque()) {
977 color = SK_ColorTRANSPARENT;
978 } else if (SkColorGetA(color) != 255 && contents_opaque()) {
979 for (const LayerImpl* layer = parent(); layer;
980 layer = layer->parent()) {
981 color = layer->background_color();
982 if (SkColorGetA(color) == 255)
983 break;
985 if (SkColorGetA(color) != 255)
986 color = layer_tree_impl()->background_color();
987 if (SkColorGetA(color) != 255)
988 color = SkColorSetA(color, 255);
990 return color;
993 void LayerImpl::SetFilters(const FilterOperations& filters) {
994 if (filters_ == filters)
995 return;
997 filters_ = filters;
998 NoteLayerPropertyChangedForSubtree();
1001 bool LayerImpl::FilterIsAnimating() const {
1002 return layer_animation_controller_->IsAnimatingProperty(Animation::FILTER);
1005 bool LayerImpl::FilterIsAnimatingOnImplOnly() const {
1006 Animation* filter_animation =
1007 layer_animation_controller_->GetAnimation(Animation::FILTER);
1008 return filter_animation && filter_animation->is_impl_only();
1011 void LayerImpl::SetBackgroundFilters(
1012 const FilterOperations& filters) {
1013 if (background_filters_ == filters)
1014 return;
1016 background_filters_ = filters;
1017 NoteLayerPropertyChanged();
1020 void LayerImpl::SetMasksToBounds(bool masks_to_bounds) {
1021 if (masks_to_bounds_ == masks_to_bounds)
1022 return;
1024 masks_to_bounds_ = masks_to_bounds;
1025 NoteLayerPropertyChangedForSubtree();
1028 void LayerImpl::SetContentsOpaque(bool opaque) {
1029 if (contents_opaque_ == opaque)
1030 return;
1032 contents_opaque_ = opaque;
1033 NoteLayerPropertyChangedForSubtree();
1036 void LayerImpl::SetOpacity(float opacity) {
1037 if (opacity_ == opacity)
1038 return;
1040 opacity_ = opacity;
1041 NoteLayerPropertyChangedForSubtree();
1044 bool LayerImpl::OpacityIsAnimating() const {
1045 return layer_animation_controller_->IsAnimatingProperty(Animation::OPACITY);
1048 bool LayerImpl::OpacityIsAnimatingOnImplOnly() const {
1049 Animation* opacity_animation =
1050 layer_animation_controller_->GetAnimation(Animation::OPACITY);
1051 return opacity_animation && opacity_animation->is_impl_only();
1054 void LayerImpl::SetBlendMode(SkXfermode::Mode blend_mode) {
1055 if (blend_mode_ == blend_mode)
1056 return;
1058 blend_mode_ = blend_mode;
1059 NoteLayerPropertyChangedForSubtree();
1062 void LayerImpl::SetIsRootForIsolatedGroup(bool root) {
1063 if (is_root_for_isolated_group_ == root)
1064 return;
1066 is_root_for_isolated_group_ = root;
1067 SetNeedsPushProperties();
1070 void LayerImpl::SetPosition(const gfx::PointF& position) {
1071 if (position_ == position)
1072 return;
1074 position_ = position;
1075 NoteLayerPropertyChangedForSubtree();
1078 void LayerImpl::SetShouldFlattenTransform(bool flatten) {
1079 if (should_flatten_transform_ == flatten)
1080 return;
1082 should_flatten_transform_ = flatten;
1083 NoteLayerPropertyChangedForSubtree();
1086 void LayerImpl::Set3dSortingContextId(int id) {
1087 if (id == sorting_context_id_)
1088 return;
1089 sorting_context_id_ = id;
1090 NoteLayerPropertyChangedForSubtree();
1093 void LayerImpl::SetFrameTimingRequests(
1094 const std::vector<FrameTimingRequest>& requests) {
1095 frame_timing_requests_ = requests;
1096 frame_timing_requests_dirty_ = true;
1097 SetNeedsPushProperties();
1100 void LayerImpl::GatherFrameTimingRequestIds(std::vector<int64_t>* request_ids) {
1101 for (const auto& request : frame_timing_requests_)
1102 request_ids->push_back(request.id());
1105 void LayerImpl::SetTransform(const gfx::Transform& transform) {
1106 if (transform_ == transform)
1107 return;
1109 transform_ = transform;
1110 transform_is_invertible_ = transform_.IsInvertible();
1111 NoteLayerPropertyChangedForSubtree();
1114 void LayerImpl::SetTransformAndInvertibility(const gfx::Transform& transform,
1115 bool transform_is_invertible) {
1116 if (transform_ == transform) {
1117 DCHECK(transform_is_invertible_ == transform_is_invertible)
1118 << "Can't change invertibility if transform is unchanged";
1119 return;
1121 transform_ = transform;
1122 transform_is_invertible_ = transform_is_invertible;
1123 NoteLayerPropertyChangedForSubtree();
1126 bool LayerImpl::TransformIsAnimating() const {
1127 return layer_animation_controller_->IsAnimatingProperty(Animation::TRANSFORM);
1130 bool LayerImpl::TransformIsAnimatingOnImplOnly() const {
1131 Animation* transform_animation =
1132 layer_animation_controller_->GetAnimation(Animation::TRANSFORM);
1133 return transform_animation && transform_animation->is_impl_only();
1136 void LayerImpl::SetUpdateRect(const gfx::Rect& update_rect) {
1137 update_rect_ = update_rect;
1138 SetNeedsPushProperties();
1141 void LayerImpl::AddDamageRect(const gfx::RectF& damage_rect) {
1142 damage_rect_ = gfx::UnionRects(damage_rect_, damage_rect);
1145 bool LayerImpl::IsExternalScrollActive() const {
1146 return layer_tree_impl_->IsExternalScrollActive();
1149 void LayerImpl::SetCurrentScrollOffset(const gfx::ScrollOffset& scroll_offset) {
1150 DCHECK(IsActive());
1151 if (scroll_offset_->SetCurrent(scroll_offset))
1152 DidUpdateScrollOffset(false);
1155 void LayerImpl::SetCurrentScrollOffsetFromDelegate(
1156 const gfx::ScrollOffset& scroll_offset) {
1157 DCHECK(IsActive());
1158 if (scroll_offset_->SetCurrent(scroll_offset))
1159 DidUpdateScrollOffset(true);
1162 void LayerImpl::PushScrollOffsetFromMainThread(
1163 const gfx::ScrollOffset& scroll_offset) {
1164 PushScrollOffset(&scroll_offset);
1167 void LayerImpl::PushScrollOffsetFromMainThreadAndClobberActiveValue(
1168 const gfx::ScrollOffset& scroll_offset) {
1169 scroll_offset_->set_clobber_active_value();
1170 PushScrollOffset(&scroll_offset);
1173 gfx::ScrollOffset LayerImpl::PullDeltaForMainThread() {
1174 // TODO(miletus): Remove all this temporary flooring machinery when
1175 // Blink fully supports fractional scrolls.
1176 gfx::ScrollOffset current_offset = CurrentScrollOffset();
1177 gfx::Vector2dF current_delta = ScrollDelta();
1178 gfx::Vector2dF floored_delta(floor(current_delta.x()),
1179 floor(current_delta.y()));
1180 gfx::Vector2dF diff_delta = floored_delta - current_delta;
1181 gfx::ScrollOffset tmp_offset = ScrollOffsetWithDelta(current_offset,
1182 diff_delta);
1183 scroll_offset_->SetCurrent(tmp_offset);
1184 gfx::ScrollOffset delta = scroll_offset_->PullDeltaForMainThread();
1185 scroll_offset_->SetCurrent(current_offset);
1186 return delta;
1189 gfx::ScrollOffset LayerImpl::CurrentScrollOffset() const {
1190 return scroll_offset_->Current(IsActive());
1193 gfx::Vector2dF LayerImpl::ScrollDelta() const {
1194 if (IsActive())
1195 return gfx::Vector2dF(scroll_offset_->Delta().x(),
1196 scroll_offset_->Delta().y());
1197 else
1198 return gfx::Vector2dF(scroll_offset_->PendingDelta().get().x(),
1199 scroll_offset_->PendingDelta().get().y());
1202 void LayerImpl::SetScrollDelta(const gfx::Vector2dF& delta) {
1203 DCHECK(IsActive());
1204 DCHECK(scrollable() || delta.IsZero());
1205 SetCurrentScrollOffset(scroll_offset_->ActiveBase() +
1206 gfx::ScrollOffset(delta));
1209 gfx::ScrollOffset LayerImpl::BaseScrollOffset() const {
1210 if (IsActive())
1211 return scroll_offset_->ActiveBase();
1212 else
1213 return scroll_offset_->PendingBase();
1216 void LayerImpl::PushScrollOffset(const gfx::ScrollOffset* scroll_offset) {
1217 DCHECK(scroll_offset || IsActive());
1218 bool changed = false;
1219 if (scroll_offset) {
1220 DCHECK(!IsActive() || !layer_tree_impl_->FindPendingTreeLayerById(id()));
1221 changed |= scroll_offset_->PushFromMainThread(*scroll_offset);
1223 if (IsActive()) {
1224 changed |= scroll_offset_->PushPendingToActive();
1227 if (changed)
1228 DidUpdateScrollOffset(false);
1231 void LayerImpl::UpdatePropertyTreeForScrollingIfNeeded() {
1232 // TODO(enne): in the future, scrolling should update the scroll tree
1233 // directly instead of going through layers.
1234 if (transform_tree_index_ != -1) {
1235 TransformTree& transform_tree =
1236 layer_tree_impl()->property_trees()->transform_tree;
1237 TransformNode* node = transform_tree.Node(transform_tree_index_);
1238 gfx::ScrollOffset current_offset = scroll_offset_->Current(IsActive());
1239 if (node->data.scroll_offset != current_offset) {
1240 node->data.scroll_offset = current_offset;
1241 node->data.needs_local_transform_update = true;
1242 transform_tree.set_needs_update(true);
1247 void LayerImpl::DidUpdateScrollOffset(bool is_from_root_delegate) {
1248 DCHECK(scroll_offset_);
1250 if (!is_from_root_delegate)
1251 layer_tree_impl()->DidUpdateScrollOffset(id());
1252 NoteLayerPropertyChangedForSubtree();
1253 ScrollbarParametersDidChange(false);
1255 UpdatePropertyTreeForScrollingIfNeeded();
1257 // Inform the pending twin that a property changed.
1258 if (layer_tree_impl()->IsActiveTree()) {
1259 LayerImpl* pending_twin = layer_tree_impl()->FindPendingTreeLayerById(id());
1260 if (pending_twin)
1261 pending_twin->DidUpdateScrollOffset(is_from_root_delegate);
1265 void LayerImpl::SetDoubleSided(bool double_sided) {
1266 if (double_sided_ == double_sided)
1267 return;
1269 double_sided_ = double_sided;
1270 NoteLayerPropertyChangedForSubtree();
1273 SimpleEnclosedRegion LayerImpl::VisibleOpaqueRegion() const {
1274 if (contents_opaque())
1275 return SimpleEnclosedRegion(visible_layer_rect());
1276 return SimpleEnclosedRegion();
1279 void LayerImpl::DidBeginTracing() {}
1281 void LayerImpl::ReleaseResources() {}
1283 void LayerImpl::RecreateResources() {
1286 gfx::ScrollOffset LayerImpl::MaxScrollOffset() const {
1287 if (!scroll_clip_layer_ || bounds().IsEmpty())
1288 return gfx::ScrollOffset();
1290 LayerImpl const* page_scale_layer = layer_tree_impl()->page_scale_layer();
1291 DCHECK(this != page_scale_layer);
1292 DCHECK(this != layer_tree_impl()->InnerViewportScrollLayer() ||
1293 IsContainerForFixedPositionLayers());
1295 float scale_factor = 1.f;
1296 for (LayerImpl const* current_layer = this;
1297 current_layer != scroll_clip_layer_->parent();
1298 current_layer = current_layer->parent()) {
1299 if (current_layer == page_scale_layer)
1300 scale_factor = layer_tree_impl()->current_page_scale_factor();
1303 gfx::SizeF scaled_scroll_bounds =
1304 gfx::ToFlooredSize(gfx::ScaleSize(BoundsForScrolling(), scale_factor));
1305 scaled_scroll_bounds = gfx::ToFlooredSize(scaled_scroll_bounds);
1307 gfx::ScrollOffset max_offset(
1308 scaled_scroll_bounds.width() - scroll_clip_layer_->bounds().width(),
1309 scaled_scroll_bounds.height() - scroll_clip_layer_->bounds().height());
1310 // We need the final scroll offset to be in CSS coords.
1311 max_offset.Scale(1 / scale_factor);
1312 max_offset.SetToMax(gfx::ScrollOffset());
1313 return max_offset;
1316 gfx::ScrollOffset LayerImpl::ClampScrollOffsetToLimits(
1317 gfx::ScrollOffset offset) const {
1318 offset.SetToMin(MaxScrollOffset());
1319 offset.SetToMax(gfx::ScrollOffset());
1320 return offset;
1323 gfx::Vector2dF LayerImpl::ClampScrollToMaxScrollOffset() {
1324 gfx::ScrollOffset old_offset = CurrentScrollOffset();
1325 gfx::ScrollOffset clamped_offset = ClampScrollOffsetToLimits(old_offset);
1326 gfx::Vector2dF delta = clamped_offset.DeltaFrom(old_offset);
1327 if (!delta.IsZero())
1328 ScrollBy(delta);
1329 return delta;
1332 void LayerImpl::SetScrollbarPosition(ScrollbarLayerImplBase* scrollbar_layer,
1333 LayerImpl* scrollbar_clip_layer,
1334 bool on_resize) const {
1335 DCHECK(scrollbar_layer);
1336 LayerImpl* page_scale_layer = layer_tree_impl()->page_scale_layer();
1338 DCHECK(this != page_scale_layer);
1339 DCHECK(scrollbar_clip_layer);
1340 gfx::RectF clip_rect(gfx::PointF(),
1341 scrollbar_clip_layer->BoundsForScrolling());
1343 // See comment in MaxScrollOffset() regarding the use of the content layer
1344 // bounds here.
1345 gfx::RectF scroll_rect(gfx::PointF(), BoundsForScrolling());
1347 if (scroll_rect.size().IsEmpty())
1348 return;
1350 gfx::ScrollOffset current_offset;
1351 for (LayerImpl const* current_layer = this;
1352 current_layer != scrollbar_clip_layer->parent();
1353 current_layer = current_layer->parent()) {
1354 current_offset += current_layer->CurrentScrollOffset();
1355 if (current_layer == page_scale_layer) {
1356 float scale_factor = layer_tree_impl()->current_page_scale_factor();
1357 current_offset.Scale(scale_factor);
1358 scroll_rect.Scale(scale_factor);
1362 bool scrollbar_needs_animation = false;
1363 scrollbar_needs_animation |= scrollbar_layer->SetVerticalAdjust(
1364 scrollbar_clip_layer->bounds_delta().y());
1365 if (scrollbar_layer->orientation() == HORIZONTAL) {
1366 float visible_ratio = clip_rect.width() / scroll_rect.width();
1367 scrollbar_needs_animation |=
1368 scrollbar_layer->SetCurrentPos(current_offset.x());
1369 scrollbar_needs_animation |=
1370 scrollbar_layer->SetMaximum(scroll_rect.width() - clip_rect.width());
1371 scrollbar_needs_animation |=
1372 scrollbar_layer->SetVisibleToTotalLengthRatio(visible_ratio);
1373 } else {
1374 float visible_ratio = clip_rect.height() / scroll_rect.height();
1375 bool y_offset_did_change =
1376 scrollbar_layer->SetCurrentPos(current_offset.y());
1377 scrollbar_needs_animation |= y_offset_did_change;
1378 scrollbar_needs_animation |=
1379 scrollbar_layer->SetMaximum(scroll_rect.height() - clip_rect.height());
1380 scrollbar_needs_animation |=
1381 scrollbar_layer->SetVisibleToTotalLengthRatio(visible_ratio);
1382 if (y_offset_did_change && layer_tree_impl()->IsActiveTree() &&
1383 this == layer_tree_impl()->InnerViewportScrollLayer()) {
1384 TRACE_COUNTER_ID1("cc", "scroll_offset_y", this->id(),
1385 current_offset.y());
1388 if (scrollbar_needs_animation) {
1389 layer_tree_impl()->set_needs_update_draw_properties();
1390 // TODO(wjmaclean) The scrollbar animator for the pinch-zoom scrollbars
1391 // should activate for every scroll on the main frame, not just the
1392 // scrolls that move the pinch virtual viewport (i.e. trigger from
1393 // either inner or outer viewport).
1394 if (scrollbar_animation_controller_) {
1395 // Non-overlay scrollbars shouldn't trigger animations.
1396 if (scrollbar_layer->is_overlay_scrollbar())
1397 scrollbar_animation_controller_->DidScrollUpdate(on_resize);
1402 void LayerImpl::DidBecomeActive() {
1403 if (layer_tree_impl_->settings().scrollbar_animator ==
1404 LayerTreeSettings::NO_ANIMATOR) {
1405 return;
1408 bool need_scrollbar_animation_controller = scrollable() && scrollbars_;
1409 if (!need_scrollbar_animation_controller) {
1410 scrollbar_animation_controller_ = nullptr;
1411 return;
1414 if (scrollbar_animation_controller_)
1415 return;
1417 scrollbar_animation_controller_ =
1418 layer_tree_impl_->CreateScrollbarAnimationController(this);
1421 void LayerImpl::ClearScrollbars() {
1422 if (!scrollbars_)
1423 return;
1425 scrollbars_.reset(nullptr);
1428 void LayerImpl::AddScrollbar(ScrollbarLayerImplBase* layer) {
1429 DCHECK(layer);
1430 DCHECK(!scrollbars_ || scrollbars_->find(layer) == scrollbars_->end());
1431 if (!scrollbars_)
1432 scrollbars_.reset(new ScrollbarSet());
1434 scrollbars_->insert(layer);
1437 void LayerImpl::RemoveScrollbar(ScrollbarLayerImplBase* layer) {
1438 DCHECK(scrollbars_);
1439 DCHECK(layer);
1440 DCHECK(scrollbars_->find(layer) != scrollbars_->end());
1442 scrollbars_->erase(layer);
1443 if (scrollbars_->empty())
1444 scrollbars_ = nullptr;
1447 bool LayerImpl::HasScrollbar(ScrollbarOrientation orientation) const {
1448 if (!scrollbars_)
1449 return false;
1451 for (ScrollbarSet::iterator it = scrollbars_->begin();
1452 it != scrollbars_->end();
1453 ++it)
1454 if ((*it)->orientation() == orientation)
1455 return true;
1457 return false;
1460 void LayerImpl::ScrollbarParametersDidChange(bool on_resize) {
1461 if (!scrollbars_)
1462 return;
1464 for (ScrollbarSet::iterator it = scrollbars_->begin();
1465 it != scrollbars_->end();
1466 ++it) {
1467 bool is_scroll_layer = (*it)->ScrollLayerId() == layer_id_;
1468 bool scroll_layer_resized = is_scroll_layer && on_resize;
1469 (*it)->ScrollbarParametersDidChange(scroll_layer_resized);
1473 void LayerImpl::SetNeedsPushProperties() {
1474 if (needs_push_properties_)
1475 return;
1476 if (!parent_should_know_need_push_properties() && parent_)
1477 parent_->AddDependentNeedsPushProperties();
1478 needs_push_properties_ = true;
1481 void LayerImpl::AddDependentNeedsPushProperties() {
1482 DCHECK_GE(num_dependents_need_push_properties_, 0);
1484 if (!parent_should_know_need_push_properties() && parent_)
1485 parent_->AddDependentNeedsPushProperties();
1487 num_dependents_need_push_properties_++;
1490 void LayerImpl::RemoveDependentNeedsPushProperties() {
1491 num_dependents_need_push_properties_--;
1492 DCHECK_GE(num_dependents_need_push_properties_, 0);
1494 if (!parent_should_know_need_push_properties() && parent_)
1495 parent_->RemoveDependentNeedsPushProperties();
1498 void LayerImpl::GetAllPrioritizedTilesForTracing(
1499 std::vector<PrioritizedTile>* prioritized_tiles) const {
1502 void LayerImpl::AsValueInto(base::trace_event::TracedValue* state) const {
1503 TracedValue::MakeDictIntoImplicitSnapshotWithCategory(
1504 TRACE_DISABLED_BY_DEFAULT("cc.debug"),
1505 state,
1506 "cc::LayerImpl",
1507 LayerTypeAsString(),
1508 this);
1509 state->SetInteger("layer_id", id());
1510 MathUtil::AddToTracedValue("bounds", bounds_, state);
1512 state->SetDouble("opacity", opacity());
1514 MathUtil::AddToTracedValue("position", position_, state);
1516 state->SetInteger("draws_content", DrawsContent());
1517 state->SetInteger("gpu_memory_usage",
1518 base::saturated_cast<int>(GPUMemoryUsageInBytes()));
1520 MathUtil::AddToTracedValue(
1521 "scroll_offset", scroll_offset_ ? scroll_offset_->Current(IsActive())
1522 : gfx::ScrollOffset(),
1523 state);
1525 MathUtil::AddToTracedValue("transform_origin", transform_origin_, state);
1527 bool clipped;
1528 gfx::QuadF layer_quad = MathUtil::MapQuad(
1529 screen_space_transform(), gfx::QuadF(gfx::Rect(bounds())), &clipped);
1530 MathUtil::AddToTracedValue("layer_quad", layer_quad, state);
1531 if (!touch_event_handler_region_.IsEmpty()) {
1532 state->BeginArray("touch_event_handler_region");
1533 touch_event_handler_region_.AsValueInto(state);
1534 state->EndArray();
1536 if (have_wheel_event_handlers_) {
1537 gfx::Rect wheel_rect(bounds());
1538 Region wheel_region(wheel_rect);
1539 state->BeginArray("wheel_event_handler_region");
1540 wheel_region.AsValueInto(state);
1541 state->EndArray();
1543 if (have_scroll_event_handlers_) {
1544 gfx::Rect scroll_rect(bounds());
1545 Region scroll_region(scroll_rect);
1546 state->BeginArray("scroll_event_handler_region");
1547 scroll_region.AsValueInto(state);
1548 state->EndArray();
1550 if (!non_fast_scrollable_region_.IsEmpty()) {
1551 state->BeginArray("non_fast_scrollable_region");
1552 non_fast_scrollable_region_.AsValueInto(state);
1553 state->EndArray();
1555 if (scroll_blocks_on_) {
1556 state->SetInteger("scroll_blocks_on", scroll_blocks_on_);
1559 state->BeginArray("children");
1560 for (size_t i = 0; i < children_.size(); ++i) {
1561 state->BeginDictionary();
1562 children_[i]->AsValueInto(state);
1563 state->EndDictionary();
1565 state->EndArray();
1566 if (mask_layer_) {
1567 state->BeginDictionary("mask_layer");
1568 mask_layer_->AsValueInto(state);
1569 state->EndDictionary();
1571 if (replica_layer_) {
1572 state->BeginDictionary("replica_layer");
1573 replica_layer_->AsValueInto(state);
1574 state->EndDictionary();
1577 if (scroll_parent_)
1578 state->SetInteger("scroll_parent", scroll_parent_->id());
1580 if (clip_parent_)
1581 state->SetInteger("clip_parent", clip_parent_->id());
1583 state->SetBoolean("can_use_lcd_text", can_use_lcd_text());
1584 state->SetBoolean("contents_opaque", contents_opaque());
1586 state->SetBoolean(
1587 "has_animation_bounds",
1588 layer_animation_controller()->HasAnimationThatInflatesBounds());
1590 gfx::BoxF box;
1591 if (LayerUtils::GetAnimationBounds(*this, &box))
1592 MathUtil::AddToTracedValue("animation_bounds", box, state);
1594 if (debug_info_.get()) {
1595 std::string str;
1596 debug_info_->AppendAsTraceFormat(&str);
1597 base::JSONReader json_reader;
1598 scoped_ptr<base::Value> debug_info_value(json_reader.ReadToValue(str));
1600 if (debug_info_value->IsType(base::Value::TYPE_DICTIONARY)) {
1601 base::DictionaryValue* dictionary_value = nullptr;
1602 bool converted_to_dictionary =
1603 debug_info_value->GetAsDictionary(&dictionary_value);
1604 DCHECK(converted_to_dictionary);
1605 for (base::DictionaryValue::Iterator it(*dictionary_value); !it.IsAtEnd();
1606 it.Advance()) {
1607 state->SetValue(it.key().data(), it.value().CreateDeepCopy());
1609 } else {
1610 NOTREACHED();
1614 if (!frame_timing_requests_.empty()) {
1615 state->BeginArray("frame_timing_requests");
1616 for (const auto& request : frame_timing_requests_) {
1617 state->BeginDictionary();
1618 state->SetInteger("request_id", request.id());
1619 MathUtil::AddToTracedValue("request_rect", request.rect(), state);
1620 state->EndDictionary();
1622 state->EndArray();
1626 bool LayerImpl::IsDrawnRenderSurfaceLayerListMember() const {
1627 return draw_properties_.last_drawn_render_surface_layer_list_id ==
1628 layer_tree_impl_->current_render_surface_list_id();
1631 size_t LayerImpl::GPUMemoryUsageInBytes() const { return 0; }
1633 void LayerImpl::RunMicroBenchmark(MicroBenchmarkImpl* benchmark) {
1634 benchmark->RunOnLayer(this);
1637 int LayerImpl::NumDescendantsThatDrawContent() const {
1638 return num_descendants_that_draw_content_;
1641 void LayerImpl::NotifyAnimationFinished(
1642 base::TimeTicks monotonic_time,
1643 Animation::TargetProperty target_property,
1644 int group) {
1645 if (target_property == Animation::SCROLL_OFFSET)
1646 layer_tree_impl_->InputScrollAnimationFinished();
1649 void LayerImpl::SetHasRenderSurface(bool should_have_render_surface) {
1650 if (!!render_surface() == should_have_render_surface)
1651 return;
1653 SetNeedsPushProperties();
1654 layer_tree_impl()->set_needs_update_draw_properties();
1655 if (should_have_render_surface) {
1656 render_surface_ = make_scoped_ptr(new RenderSurfaceImpl(this));
1657 return;
1659 render_surface_.reset();
1662 Region LayerImpl::GetInvalidationRegion() {
1663 return Region(update_rect_);
1666 gfx::Rect LayerImpl::GetEnclosingRectInTargetSpace() const {
1667 return MathUtil::MapEnclosingClippedRect(
1668 draw_properties_.target_space_transform, gfx::Rect(bounds()));
1671 gfx::Rect LayerImpl::GetScaledEnclosingRectInTargetSpace(float scale) const {
1672 gfx::Transform scaled_draw_transform =
1673 draw_properties_.target_space_transform;
1674 scaled_draw_transform.Scale(SK_MScalar1 / scale, SK_MScalar1 / scale);
1675 gfx::Size scaled_bounds = gfx::ToCeiledSize(gfx::ScaleSize(bounds(), scale));
1676 return MathUtil::MapEnclosingClippedRect(scaled_draw_transform,
1677 gfx::Rect(scaled_bounds));
1680 } // namespace cc