Fix race condition in gyp/ninja builds.
[chromium-blink-merge.git] / cc / layers / layer_impl.cc
bloba5ae0607ff6bbce62557f1410883141a66062a6b
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/debug/trace_event.h"
8 #include "base/json/json_reader.h"
9 #include "base/strings/stringprintf.h"
10 #include "cc/animation/animation_registrar.h"
11 #include "cc/animation/scrollbar_animation_controller.h"
12 #include "cc/base/math_util.h"
13 #include "cc/debug/debug_colors.h"
14 #include "cc/debug/layer_tree_debug_state.h"
15 #include "cc/debug/micro_benchmark_impl.h"
16 #include "cc/debug/traced_value.h"
17 #include "cc/input/layer_scroll_offset_delegate.h"
18 #include "cc/layers/layer_utils.h"
19 #include "cc/layers/painted_scrollbar_layer_impl.h"
20 #include "cc/output/copy_output_request.h"
21 #include "cc/quads/debug_border_draw_quad.h"
22 #include "cc/trees/layer_tree_host_common.h"
23 #include "cc/trees/layer_tree_impl.h"
24 #include "cc/trees/layer_tree_settings.h"
25 #include "cc/trees/proxy.h"
26 #include "ui/gfx/box_f.h"
27 #include "ui/gfx/geometry/vector2d_conversions.h"
28 #include "ui/gfx/point_conversions.h"
29 #include "ui/gfx/quad_f.h"
30 #include "ui/gfx/rect_conversions.h"
31 #include "ui/gfx/size_conversions.h"
33 namespace cc {
34 LayerImpl::LayerImpl(LayerTreeImpl* tree_impl, int id)
35 : parent_(NULL),
36 scroll_parent_(NULL),
37 clip_parent_(NULL),
38 mask_layer_id_(-1),
39 replica_layer_id_(-1),
40 layer_id_(id),
41 layer_tree_impl_(tree_impl),
42 scroll_offset_delegate_(NULL),
43 scroll_clip_layer_(NULL),
44 should_scroll_on_main_thread_(false),
45 have_wheel_event_handlers_(false),
46 have_scroll_event_handlers_(false),
47 user_scrollable_horizontal_(true),
48 user_scrollable_vertical_(true),
49 stacking_order_changed_(false),
50 double_sided_(true),
51 should_flatten_transform_(true),
52 layer_property_changed_(false),
53 masks_to_bounds_(false),
54 contents_opaque_(false),
55 is_root_for_isolated_group_(false),
56 use_parent_backface_visibility_(false),
57 draw_checkerboard_for_missing_tiles_(false),
58 draws_content_(false),
59 hide_layer_and_subtree_(false),
60 force_render_surface_(false),
61 transform_is_invertible_(true),
62 is_container_for_fixed_position_layers_(false),
63 background_color_(0),
64 opacity_(1.0),
65 blend_mode_(SkXfermode::kSrcOver_Mode),
66 draw_depth_(0.f),
67 needs_push_properties_(false),
68 num_dependents_need_push_properties_(0),
69 sorting_context_id_(0),
70 current_draw_mode_(DRAW_MODE_NONE) {
71 DCHECK_GT(layer_id_, 0);
72 DCHECK(layer_tree_impl_);
73 layer_tree_impl_->RegisterLayer(this);
74 AnimationRegistrar* registrar = layer_tree_impl_->animationRegistrar();
75 layer_animation_controller_ =
76 registrar->GetAnimationControllerForId(layer_id_);
77 layer_animation_controller_->AddValueObserver(this);
78 if (IsActive())
79 layer_animation_controller_->set_value_provider(this);
80 SetNeedsPushProperties();
83 LayerImpl::~LayerImpl() {
84 DCHECK_EQ(DRAW_MODE_NONE, current_draw_mode_);
86 layer_animation_controller_->RemoveValueObserver(this);
87 layer_animation_controller_->remove_value_provider(this);
89 if (!copy_requests_.empty() && layer_tree_impl_->IsActiveTree())
90 layer_tree_impl()->RemoveLayerWithCopyOutputRequest(this);
91 layer_tree_impl_->UnregisterLayer(this);
93 TRACE_EVENT_OBJECT_DELETED_WITH_ID(
94 TRACE_DISABLED_BY_DEFAULT("cc.debug"), "cc::LayerImpl", this);
97 void LayerImpl::AddChild(scoped_ptr<LayerImpl> child) {
98 child->SetParent(this);
99 DCHECK_EQ(layer_tree_impl(), child->layer_tree_impl());
100 children_.push_back(child.Pass());
101 layer_tree_impl()->set_needs_update_draw_properties();
104 scoped_ptr<LayerImpl> LayerImpl::RemoveChild(LayerImpl* child) {
105 for (OwnedLayerImplList::iterator it = children_.begin();
106 it != children_.end();
107 ++it) {
108 if (*it == child) {
109 scoped_ptr<LayerImpl> ret = children_.take(it);
110 children_.erase(it);
111 layer_tree_impl()->set_needs_update_draw_properties();
112 return ret.Pass();
115 return scoped_ptr<LayerImpl>();
118 void LayerImpl::SetParent(LayerImpl* parent) {
119 if (parent_should_know_need_push_properties()) {
120 if (parent_)
121 parent_->RemoveDependentNeedsPushProperties();
122 if (parent)
123 parent->AddDependentNeedsPushProperties();
125 parent_ = parent;
128 void LayerImpl::ClearChildList() {
129 if (children_.empty())
130 return;
132 children_.clear();
133 layer_tree_impl()->set_needs_update_draw_properties();
136 bool LayerImpl::HasAncestor(const LayerImpl* ancestor) const {
137 if (!ancestor)
138 return false;
140 for (const LayerImpl* layer = this; layer; layer = layer->parent()) {
141 if (layer == ancestor)
142 return true;
145 return false;
148 void LayerImpl::SetScrollParent(LayerImpl* parent) {
149 if (scroll_parent_ == parent)
150 return;
152 // Having both a scroll parent and a scroll offset delegate is unsupported.
153 DCHECK(!scroll_offset_delegate_);
155 if (parent)
156 DCHECK_EQ(layer_tree_impl()->LayerById(parent->id()), parent);
158 scroll_parent_ = parent;
159 SetNeedsPushProperties();
162 void LayerImpl::SetDebugInfo(
163 scoped_refptr<base::debug::ConvertableToTraceFormat> other) {
164 debug_info_ = other;
165 SetNeedsPushProperties();
168 void LayerImpl::SetScrollChildren(std::set<LayerImpl*>* children) {
169 if (scroll_children_.get() == children)
170 return;
171 scroll_children_.reset(children);
172 SetNeedsPushProperties();
175 void LayerImpl::SetClipParent(LayerImpl* ancestor) {
176 if (clip_parent_ == ancestor)
177 return;
179 clip_parent_ = ancestor;
180 SetNeedsPushProperties();
183 void LayerImpl::SetClipChildren(std::set<LayerImpl*>* children) {
184 if (clip_children_.get() == children)
185 return;
186 clip_children_.reset(children);
187 SetNeedsPushProperties();
190 void LayerImpl::PassCopyRequests(ScopedPtrVector<CopyOutputRequest>* requests) {
191 if (requests->empty())
192 return;
194 bool was_empty = copy_requests_.empty();
195 copy_requests_.insert_and_take(copy_requests_.end(), *requests);
196 requests->clear();
198 if (was_empty && layer_tree_impl()->IsActiveTree())
199 layer_tree_impl()->AddLayerWithCopyOutputRequest(this);
200 NoteLayerPropertyChangedForSubtree();
203 void LayerImpl::TakeCopyRequestsAndTransformToTarget(
204 ScopedPtrVector<CopyOutputRequest>* requests) {
205 DCHECK(!copy_requests_.empty());
206 DCHECK(layer_tree_impl()->IsActiveTree());
208 size_t first_inserted_request = requests->size();
209 requests->insert_and_take(requests->end(), copy_requests_);
210 copy_requests_.clear();
212 for (size_t i = first_inserted_request; i < requests->size(); ++i) {
213 CopyOutputRequest* request = requests->at(i);
214 if (!request->has_area())
215 continue;
217 gfx::Rect request_in_layer_space = request->area();
218 gfx::Rect request_in_content_space =
219 LayerRectToContentRect(request_in_layer_space);
220 request->set_area(MathUtil::MapEnclosingClippedRect(
221 draw_properties_.target_space_transform, request_in_content_space));
224 layer_tree_impl()->RemoveLayerWithCopyOutputRequest(this);
227 void LayerImpl::CreateRenderSurface() {
228 DCHECK(!draw_properties_.render_surface);
229 draw_properties_.render_surface =
230 make_scoped_ptr(new RenderSurfaceImpl(this));
231 draw_properties_.render_target = this;
234 void LayerImpl::ClearRenderSurface() {
235 draw_properties_.render_surface.reset();
238 void LayerImpl::ClearRenderSurfaceLayerList() {
239 if (draw_properties_.render_surface)
240 draw_properties_.render_surface->layer_list().clear();
243 void LayerImpl::PopulateSharedQuadState(SharedQuadState* state) const {
244 state->SetAll(draw_properties_.target_space_transform,
245 draw_properties_.content_bounds,
246 draw_properties_.visible_content_rect,
247 draw_properties_.clip_rect,
248 draw_properties_.is_clipped,
249 draw_properties_.opacity,
250 blend_mode_,
251 sorting_context_id_);
254 bool LayerImpl::WillDraw(DrawMode draw_mode,
255 ResourceProvider* resource_provider) {
256 // WillDraw/DidDraw must be matched.
257 DCHECK_NE(DRAW_MODE_NONE, draw_mode);
258 DCHECK_EQ(DRAW_MODE_NONE, current_draw_mode_);
259 current_draw_mode_ = draw_mode;
260 return true;
263 void LayerImpl::DidDraw(ResourceProvider* resource_provider) {
264 DCHECK_NE(DRAW_MODE_NONE, current_draw_mode_);
265 current_draw_mode_ = DRAW_MODE_NONE;
268 bool LayerImpl::ShowDebugBorders() const {
269 return layer_tree_impl()->debug_state().show_debug_borders;
272 void LayerImpl::GetDebugBorderProperties(SkColor* color, float* width) const {
273 if (draws_content_) {
274 *color = DebugColors::ContentLayerBorderColor();
275 *width = DebugColors::ContentLayerBorderWidth(layer_tree_impl());
276 return;
279 if (masks_to_bounds_) {
280 *color = DebugColors::MaskingLayerBorderColor();
281 *width = DebugColors::MaskingLayerBorderWidth(layer_tree_impl());
282 return;
285 *color = DebugColors::ContainerLayerBorderColor();
286 *width = DebugColors::ContainerLayerBorderWidth(layer_tree_impl());
289 void LayerImpl::AppendDebugBorderQuad(
290 RenderPass* render_pass,
291 const gfx::Size& content_bounds,
292 const SharedQuadState* shared_quad_state,
293 AppendQuadsData* append_quads_data) const {
294 SkColor color;
295 float width;
296 GetDebugBorderProperties(&color, &width);
297 AppendDebugBorderQuad(render_pass,
298 content_bounds,
299 shared_quad_state,
300 append_quads_data,
301 color,
302 width);
305 void LayerImpl::AppendDebugBorderQuad(RenderPass* render_pass,
306 const gfx::Size& content_bounds,
307 const SharedQuadState* shared_quad_state,
308 AppendQuadsData* append_quads_data,
309 SkColor color,
310 float width) const {
311 if (!ShowDebugBorders())
312 return;
314 gfx::Rect quad_rect(content_bounds);
315 gfx::Rect visible_quad_rect(quad_rect);
316 DebugBorderDrawQuad* debug_border_quad =
317 render_pass->CreateAndAppendDrawQuad<DebugBorderDrawQuad>();
318 debug_border_quad->SetNew(
319 shared_quad_state, quad_rect, visible_quad_rect, color, width);
322 bool LayerImpl::HasDelegatedContent() const {
323 return false;
326 bool LayerImpl::HasContributingDelegatedRenderPasses() const {
327 return false;
330 RenderPass::Id LayerImpl::FirstContributingRenderPassId() const {
331 return RenderPass::Id(0, 0);
334 RenderPass::Id LayerImpl::NextContributingRenderPassId(RenderPass::Id id)
335 const {
336 return RenderPass::Id(0, 0);
339 ResourceProvider::ResourceId LayerImpl::ContentsResourceId() const {
340 NOTREACHED();
341 return 0;
344 void LayerImpl::SetSentScrollDelta(const gfx::Vector2d& sent_scroll_delta) {
345 // Pending tree never has sent scroll deltas
346 DCHECK(layer_tree_impl()->IsActiveTree());
348 if (sent_scroll_delta_ == sent_scroll_delta)
349 return;
351 sent_scroll_delta_ = sent_scroll_delta;
354 gfx::Vector2dF LayerImpl::ScrollBy(const gfx::Vector2dF& scroll) {
355 DCHECK(scrollable());
356 gfx::Vector2dF min_delta = -scroll_offset_;
357 gfx::Vector2dF max_delta = MaxScrollOffset() - scroll_offset_;
358 // Clamp new_delta so that position + delta stays within scroll bounds.
359 gfx::Vector2dF new_delta = (ScrollDelta() + scroll);
360 new_delta.SetToMax(min_delta);
361 new_delta.SetToMin(max_delta);
362 gfx::Vector2dF unscrolled =
363 ScrollDelta() + scroll - new_delta;
364 SetScrollDelta(new_delta);
366 return unscrolled;
369 void LayerImpl::SetScrollClipLayer(int scroll_clip_layer_id) {
370 scroll_clip_layer_ = layer_tree_impl()->LayerById(scroll_clip_layer_id);
373 void LayerImpl::ApplySentScrollDeltasFromAbortedCommit() {
374 // Pending tree never has sent scroll deltas
375 DCHECK(layer_tree_impl()->IsActiveTree());
377 // Apply sent scroll deltas to scroll position / scroll delta as if the
378 // main thread had applied them and then committed those values.
380 // This function should not change the total scroll offset; it just shifts
381 // some of the scroll delta to the scroll offset. Therefore, adjust these
382 // variables directly rather than calling the scroll offset delegate to
383 // avoid sending it multiple spurious calls.
385 // Because of the way scroll delta is calculated with a delegate, this will
386 // leave the total scroll offset unchanged on this layer regardless of
387 // whether a delegate is being used.
388 scroll_offset_ += sent_scroll_delta_;
389 scroll_delta_ -= sent_scroll_delta_;
390 sent_scroll_delta_ = gfx::Vector2d();
393 void LayerImpl::ApplyScrollDeltasSinceBeginMainFrame() {
394 // Only the pending tree can have missing scrolls.
395 DCHECK(layer_tree_impl()->IsPendingTree());
396 if (!scrollable())
397 return;
399 // Pending tree should never have sent scroll deltas.
400 DCHECK(sent_scroll_delta().IsZero());
402 LayerImpl* active_twin = layer_tree_impl()->FindActiveTreeLayerById(id());
403 if (active_twin) {
404 // Scrolls that happens after begin frame (where the sent scroll delta
405 // comes from) and commit need to be applied to the pending tree
406 // so that it is up to date with the total scroll.
407 SetScrollDelta(active_twin->ScrollDelta() -
408 active_twin->sent_scroll_delta());
412 InputHandler::ScrollStatus LayerImpl::TryScroll(
413 const gfx::PointF& screen_space_point,
414 InputHandler::ScrollInputType type) const {
415 if (should_scroll_on_main_thread()) {
416 TRACE_EVENT0("cc", "LayerImpl::TryScroll: Failed ShouldScrollOnMainThread");
417 return InputHandler::ScrollOnMainThread;
420 if (!screen_space_transform().IsInvertible()) {
421 TRACE_EVENT0("cc", "LayerImpl::TryScroll: Ignored NonInvertibleTransform");
422 return InputHandler::ScrollIgnored;
425 if (!non_fast_scrollable_region().IsEmpty()) {
426 bool clipped = false;
427 gfx::Transform inverse_screen_space_transform(
428 gfx::Transform::kSkipInitialization);
429 if (!screen_space_transform().GetInverse(&inverse_screen_space_transform)) {
430 // TODO(shawnsingh): We shouldn't be applying a projection if screen space
431 // transform is uninvertible here. Perhaps we should be returning
432 // ScrollOnMainThread in this case?
435 gfx::PointF hit_test_point_in_content_space =
436 MathUtil::ProjectPoint(inverse_screen_space_transform,
437 screen_space_point,
438 &clipped);
439 gfx::PointF hit_test_point_in_layer_space =
440 gfx::ScalePoint(hit_test_point_in_content_space,
441 1.f / contents_scale_x(),
442 1.f / contents_scale_y());
443 if (!clipped &&
444 non_fast_scrollable_region().Contains(
445 gfx::ToRoundedPoint(hit_test_point_in_layer_space))) {
446 TRACE_EVENT0("cc",
447 "LayerImpl::tryScroll: Failed NonFastScrollableRegion");
448 return InputHandler::ScrollOnMainThread;
452 if (type == InputHandler::Wheel && have_wheel_event_handlers()) {
453 TRACE_EVENT0("cc", "LayerImpl::tryScroll: Failed WheelEventHandlers");
454 return InputHandler::ScrollOnMainThread;
457 if (!scrollable()) {
458 TRACE_EVENT0("cc", "LayerImpl::tryScroll: Ignored not scrollable");
459 return InputHandler::ScrollIgnored;
462 gfx::Vector2d max_scroll_offset = MaxScrollOffset();
463 if (max_scroll_offset.x() <= 0 && max_scroll_offset.y() <= 0) {
464 TRACE_EVENT0("cc",
465 "LayerImpl::tryScroll: Ignored. Technically scrollable,"
466 " but has no affordance in either direction.");
467 return InputHandler::ScrollIgnored;
470 return InputHandler::ScrollStarted;
473 gfx::Rect LayerImpl::LayerRectToContentRect(
474 const gfx::RectF& layer_rect) const {
475 gfx::RectF content_rect =
476 gfx::ScaleRect(layer_rect, contents_scale_x(), contents_scale_y());
477 // Intersect with content rect to avoid the extra pixel because for some
478 // values x and y, ceil((x / y) * y) may be x + 1.
479 content_rect.Intersect(gfx::Rect(content_bounds()));
480 return gfx::ToEnclosingRect(content_rect);
483 skia::RefPtr<SkPicture> LayerImpl::GetPicture() {
484 return skia::RefPtr<SkPicture>();
487 scoped_ptr<LayerImpl> LayerImpl::CreateLayerImpl(LayerTreeImpl* tree_impl) {
488 return LayerImpl::Create(tree_impl, layer_id_);
491 void LayerImpl::PushPropertiesTo(LayerImpl* layer) {
492 layer->SetTransformOrigin(transform_origin_);
493 layer->SetBackgroundColor(background_color_);
494 layer->SetBounds(bounds_);
495 layer->SetContentBounds(content_bounds());
496 layer->SetContentsScale(contents_scale_x(), contents_scale_y());
497 layer->SetDoubleSided(double_sided_);
498 layer->SetDrawCheckerboardForMissingTiles(
499 draw_checkerboard_for_missing_tiles_);
500 layer->SetForceRenderSurface(force_render_surface_);
501 layer->SetDrawsContent(DrawsContent());
502 layer->SetHideLayerAndSubtree(hide_layer_and_subtree_);
503 layer->SetFilters(filters());
504 layer->SetBackgroundFilters(background_filters());
505 layer->SetMasksToBounds(masks_to_bounds_);
506 layer->SetShouldScrollOnMainThread(should_scroll_on_main_thread_);
507 layer->SetHaveWheelEventHandlers(have_wheel_event_handlers_);
508 layer->SetHaveScrollEventHandlers(have_scroll_event_handlers_);
509 layer->SetNonFastScrollableRegion(non_fast_scrollable_region_);
510 layer->SetTouchEventHandlerRegion(touch_event_handler_region_);
511 layer->SetContentsOpaque(contents_opaque_);
512 layer->SetOpacity(opacity_);
513 layer->SetBlendMode(blend_mode_);
514 layer->SetIsRootForIsolatedGroup(is_root_for_isolated_group_);
515 layer->SetPosition(position_);
516 layer->SetIsContainerForFixedPositionLayers(
517 is_container_for_fixed_position_layers_);
518 layer->SetPositionConstraint(position_constraint_);
519 layer->SetShouldFlattenTransform(should_flatten_transform_);
520 layer->SetUseParentBackfaceVisibility(use_parent_backface_visibility_);
521 layer->SetTransformAndInvertibility(transform_, transform_is_invertible_);
523 layer->SetScrollClipLayer(scroll_clip_layer_ ? scroll_clip_layer_->id()
524 : Layer::INVALID_ID);
525 layer->set_user_scrollable_horizontal(user_scrollable_horizontal_);
526 layer->set_user_scrollable_vertical(user_scrollable_vertical_);
527 layer->SetScrollOffsetAndDelta(
528 scroll_offset_, layer->ScrollDelta() - layer->sent_scroll_delta());
529 layer->SetSentScrollDelta(gfx::Vector2d());
530 layer->Set3dSortingContextId(sorting_context_id_);
532 LayerImpl* scroll_parent = NULL;
533 if (scroll_parent_) {
534 scroll_parent = layer->layer_tree_impl()->LayerById(scroll_parent_->id());
535 DCHECK(scroll_parent);
538 layer->SetScrollParent(scroll_parent);
539 if (scroll_children_) {
540 std::set<LayerImpl*>* scroll_children = new std::set<LayerImpl*>;
541 for (std::set<LayerImpl*>::iterator it = scroll_children_->begin();
542 it != scroll_children_->end();
543 ++it) {
544 DCHECK_EQ((*it)->scroll_parent(), this);
545 LayerImpl* scroll_child =
546 layer->layer_tree_impl()->LayerById((*it)->id());
547 DCHECK(scroll_child);
548 scroll_children->insert(scroll_child);
550 layer->SetScrollChildren(scroll_children);
551 } else {
552 layer->SetScrollChildren(NULL);
555 LayerImpl* clip_parent = NULL;
556 if (clip_parent_) {
557 clip_parent = layer->layer_tree_impl()->LayerById(
558 clip_parent_->id());
559 DCHECK(clip_parent);
562 layer->SetClipParent(clip_parent);
563 if (clip_children_) {
564 std::set<LayerImpl*>* clip_children = new std::set<LayerImpl*>;
565 for (std::set<LayerImpl*>::iterator it = clip_children_->begin();
566 it != clip_children_->end(); ++it)
567 clip_children->insert(layer->layer_tree_impl()->LayerById((*it)->id()));
568 layer->SetClipChildren(clip_children);
569 } else {
570 layer->SetClipChildren(NULL);
573 layer->PassCopyRequests(&copy_requests_);
575 // If the main thread commits multiple times before the impl thread actually
576 // draws, then damage tracking will become incorrect if we simply clobber the
577 // update_rect here. The LayerImpl's update_rect needs to accumulate (i.e.
578 // union) any update changes that have occurred on the main thread.
579 update_rect_.Union(layer->update_rect());
580 layer->SetUpdateRect(update_rect_);
582 layer->SetStackingOrderChanged(stacking_order_changed_);
583 layer->SetDebugInfo(debug_info_);
585 // Reset any state that should be cleared for the next update.
586 stacking_order_changed_ = false;
587 update_rect_ = gfx::RectF();
588 needs_push_properties_ = false;
589 num_dependents_need_push_properties_ = 0;
592 gfx::Vector2dF LayerImpl::FixedContainerSizeDelta() const {
593 if (!scroll_clip_layer_)
594 return gfx::Vector2dF();
596 float scale_delta = layer_tree_impl()->page_scale_delta();
597 float scale = layer_tree_impl()->page_scale_factor();
599 gfx::Vector2dF delta_from_scroll = scroll_clip_layer_->BoundsDelta();
600 delta_from_scroll.Scale(1.f / scale);
602 // The delta-from-pinch component requires some explanation: A viewport of
603 // size (w,h) will appear to be size (w/s,h/s) under scale s in the content
604 // space. If s -> s' on the impl thread, where s' = s * ds, then the apparent
605 // viewport size change in the content space due to ds is:
607 // (w/s',h/s') - (w/s,h/s) = (w,h)(1/s' - 1/s) = (w,h)(1 - ds)/(s ds)
609 gfx::Vector2dF delta_from_pinch =
610 gfx::Rect(scroll_clip_layer_->bounds()).bottom_right() - gfx::PointF();
611 delta_from_pinch.Scale((1.f - scale_delta) / (scale * scale_delta));
613 return delta_from_scroll + delta_from_pinch;
616 base::DictionaryValue* LayerImpl::LayerTreeAsJson() const {
617 base::DictionaryValue* result = new base::DictionaryValue;
618 result->SetString("LayerType", LayerTypeAsString());
620 base::ListValue* list = new base::ListValue;
621 list->AppendInteger(bounds().width());
622 list->AppendInteger(bounds().height());
623 result->Set("Bounds", list);
625 list = new base::ListValue;
626 list->AppendDouble(position_.x());
627 list->AppendDouble(position_.y());
628 result->Set("Position", list);
630 const gfx::Transform& gfx_transform = draw_properties_.target_space_transform;
631 double transform[16];
632 gfx_transform.matrix().asColMajord(transform);
633 list = new base::ListValue;
634 for (int i = 0; i < 16; ++i)
635 list->AppendDouble(transform[i]);
636 result->Set("DrawTransform", list);
638 result->SetBoolean("DrawsContent", draws_content_);
639 result->SetBoolean("Is3dSorted", Is3dSorted());
640 result->SetDouble("Opacity", opacity());
641 result->SetBoolean("ContentsOpaque", contents_opaque_);
643 if (scrollable())
644 result->SetBoolean("Scrollable", true);
646 if (have_wheel_event_handlers_)
647 result->SetBoolean("WheelHandler", have_wheel_event_handlers_);
648 if (have_scroll_event_handlers_)
649 result->SetBoolean("ScrollHandler", have_scroll_event_handlers_);
650 if (!touch_event_handler_region_.IsEmpty()) {
651 scoped_ptr<base::Value> region = touch_event_handler_region_.AsValue();
652 result->Set("TouchRegion", region.release());
655 list = new base::ListValue;
656 for (size_t i = 0; i < children_.size(); ++i)
657 list->Append(children_[i]->LayerTreeAsJson());
658 result->Set("Children", list);
660 return result;
663 void LayerImpl::SetStackingOrderChanged(bool stacking_order_changed) {
664 if (stacking_order_changed) {
665 stacking_order_changed_ = true;
666 NoteLayerPropertyChangedForSubtree();
670 void LayerImpl::NoteLayerPropertyChanged() {
671 layer_property_changed_ = true;
672 layer_tree_impl()->set_needs_update_draw_properties();
673 SetNeedsPushProperties();
676 void LayerImpl::NoteLayerPropertyChangedForSubtree() {
677 layer_property_changed_ = true;
678 layer_tree_impl()->set_needs_update_draw_properties();
679 for (size_t i = 0; i < children_.size(); ++i)
680 children_[i]->NoteLayerPropertyChangedForDescendantsInternal();
681 SetNeedsPushProperties();
684 void LayerImpl::NoteLayerPropertyChangedForDescendantsInternal() {
685 layer_property_changed_ = true;
686 for (size_t i = 0; i < children_.size(); ++i)
687 children_[i]->NoteLayerPropertyChangedForDescendantsInternal();
690 void LayerImpl::NoteLayerPropertyChangedForDescendants() {
691 layer_tree_impl()->set_needs_update_draw_properties();
692 for (size_t i = 0; i < children_.size(); ++i)
693 children_[i]->NoteLayerPropertyChangedForDescendantsInternal();
694 SetNeedsPushProperties();
697 const char* LayerImpl::LayerTypeAsString() const {
698 return "cc::LayerImpl";
701 void LayerImpl::ResetAllChangeTrackingForSubtree() {
702 layer_property_changed_ = false;
704 update_rect_ = gfx::RectF();
705 damage_rect_ = gfx::RectF();
707 if (draw_properties_.render_surface)
708 draw_properties_.render_surface->ResetPropertyChangedFlag();
710 if (mask_layer_)
711 mask_layer_->ResetAllChangeTrackingForSubtree();
713 if (replica_layer_) {
714 // This also resets the replica mask, if it exists.
715 replica_layer_->ResetAllChangeTrackingForSubtree();
718 for (size_t i = 0; i < children_.size(); ++i)
719 children_[i]->ResetAllChangeTrackingForSubtree();
721 needs_push_properties_ = false;
722 num_dependents_need_push_properties_ = 0;
725 gfx::Vector2dF LayerImpl::ScrollOffsetForAnimation() const {
726 return TotalScrollOffset();
729 void LayerImpl::OnFilterAnimated(const FilterOperations& filters) {
730 SetFilters(filters);
733 void LayerImpl::OnOpacityAnimated(float opacity) {
734 SetOpacity(opacity);
737 void LayerImpl::OnTransformAnimated(const gfx::Transform& transform) {
738 SetTransform(transform);
741 void LayerImpl::OnScrollOffsetAnimated(const gfx::Vector2dF& scroll_offset) {
742 // Only layers in the active tree should need to do anything here, since
743 // layers in the pending tree will find out about these changes as a
744 // result of the call to SetScrollDelta.
745 if (!IsActive())
746 return;
748 SetScrollDelta(scroll_offset - scroll_offset_);
750 layer_tree_impl_->DidAnimateScrollOffset();
753 void LayerImpl::OnAnimationWaitingForDeletion() {}
755 bool LayerImpl::IsActive() const {
756 return layer_tree_impl_->IsActiveTree();
759 // TODO(wjmaclean) Convert so that bounds returns SizeF.
760 gfx::Size LayerImpl::bounds() const {
761 return ToFlooredSize(temporary_impl_bounds_);
764 void LayerImpl::SetBounds(const gfx::Size& bounds) {
765 if (bounds_ == bounds)
766 return;
768 bounds_ = bounds;
769 temporary_impl_bounds_ = bounds;
771 ScrollbarParametersDidChange();
772 if (masks_to_bounds())
773 NoteLayerPropertyChangedForSubtree();
774 else
775 NoteLayerPropertyChanged();
778 void LayerImpl::SetTemporaryImplBounds(const gfx::SizeF& bounds) {
779 if (temporary_impl_bounds_ == bounds)
780 return;
782 temporary_impl_bounds_ = bounds;
784 ScrollbarParametersDidChange();
785 if (masks_to_bounds())
786 NoteLayerPropertyChangedForSubtree();
787 else
788 NoteLayerPropertyChanged();
791 void LayerImpl::SetMaskLayer(scoped_ptr<LayerImpl> mask_layer) {
792 int new_layer_id = mask_layer ? mask_layer->id() : -1;
794 if (mask_layer) {
795 DCHECK_EQ(layer_tree_impl(), mask_layer->layer_tree_impl());
796 DCHECK_NE(new_layer_id, mask_layer_id_);
797 } else if (new_layer_id == mask_layer_id_) {
798 return;
801 mask_layer_ = mask_layer.Pass();
802 mask_layer_id_ = new_layer_id;
803 if (mask_layer_)
804 mask_layer_->SetParent(this);
805 NoteLayerPropertyChangedForSubtree();
808 scoped_ptr<LayerImpl> LayerImpl::TakeMaskLayer() {
809 mask_layer_id_ = -1;
810 return mask_layer_.Pass();
813 void LayerImpl::SetReplicaLayer(scoped_ptr<LayerImpl> replica_layer) {
814 int new_layer_id = replica_layer ? replica_layer->id() : -1;
816 if (replica_layer) {
817 DCHECK_EQ(layer_tree_impl(), replica_layer->layer_tree_impl());
818 DCHECK_NE(new_layer_id, replica_layer_id_);
819 } else if (new_layer_id == replica_layer_id_) {
820 return;
823 replica_layer_ = replica_layer.Pass();
824 replica_layer_id_ = new_layer_id;
825 if (replica_layer_)
826 replica_layer_->SetParent(this);
827 NoteLayerPropertyChangedForSubtree();
830 scoped_ptr<LayerImpl> LayerImpl::TakeReplicaLayer() {
831 replica_layer_id_ = -1;
832 return replica_layer_.Pass();
835 ScrollbarLayerImplBase* LayerImpl::ToScrollbarLayer() {
836 return NULL;
839 void LayerImpl::SetDrawsContent(bool draws_content) {
840 if (draws_content_ == draws_content)
841 return;
843 draws_content_ = draws_content;
844 NoteLayerPropertyChanged();
847 void LayerImpl::SetHideLayerAndSubtree(bool hide) {
848 if (hide_layer_and_subtree_ == hide)
849 return;
851 hide_layer_and_subtree_ = hide;
852 NoteLayerPropertyChangedForSubtree();
855 void LayerImpl::SetTransformOrigin(const gfx::Point3F& transform_origin) {
856 if (transform_origin_ == transform_origin)
857 return;
858 transform_origin_ = transform_origin;
859 NoteLayerPropertyChangedForSubtree();
862 void LayerImpl::SetBackgroundColor(SkColor background_color) {
863 if (background_color_ == background_color)
864 return;
866 background_color_ = background_color;
867 NoteLayerPropertyChanged();
870 SkColor LayerImpl::SafeOpaqueBackgroundColor() const {
871 SkColor color = background_color();
872 if (SkColorGetA(color) == 255 && !contents_opaque()) {
873 color = SK_ColorTRANSPARENT;
874 } else if (SkColorGetA(color) != 255 && contents_opaque()) {
875 for (const LayerImpl* layer = parent(); layer;
876 layer = layer->parent()) {
877 color = layer->background_color();
878 if (SkColorGetA(color) == 255)
879 break;
881 if (SkColorGetA(color) != 255)
882 color = layer_tree_impl()->background_color();
883 if (SkColorGetA(color) != 255)
884 color = SkColorSetA(color, 255);
886 return color;
889 void LayerImpl::SetFilters(const FilterOperations& filters) {
890 if (filters_ == filters)
891 return;
893 filters_ = filters;
894 NoteLayerPropertyChangedForSubtree();
897 bool LayerImpl::FilterIsAnimating() const {
898 return layer_animation_controller_->IsAnimatingProperty(Animation::Filter);
901 bool LayerImpl::FilterIsAnimatingOnImplOnly() const {
902 Animation* filter_animation =
903 layer_animation_controller_->GetAnimation(Animation::Filter);
904 return filter_animation && filter_animation->is_impl_only();
907 void LayerImpl::SetBackgroundFilters(
908 const FilterOperations& filters) {
909 if (background_filters_ == filters)
910 return;
912 background_filters_ = filters;
913 NoteLayerPropertyChanged();
916 void LayerImpl::SetMasksToBounds(bool masks_to_bounds) {
917 if (masks_to_bounds_ == masks_to_bounds)
918 return;
920 masks_to_bounds_ = masks_to_bounds;
921 NoteLayerPropertyChangedForSubtree();
924 void LayerImpl::SetContentsOpaque(bool opaque) {
925 if (contents_opaque_ == opaque)
926 return;
928 contents_opaque_ = opaque;
929 NoteLayerPropertyChangedForSubtree();
932 void LayerImpl::SetOpacity(float opacity) {
933 if (opacity_ == opacity)
934 return;
936 opacity_ = opacity;
937 NoteLayerPropertyChangedForSubtree();
940 bool LayerImpl::OpacityIsAnimating() const {
941 return layer_animation_controller_->IsAnimatingProperty(Animation::Opacity);
944 bool LayerImpl::OpacityIsAnimatingOnImplOnly() const {
945 Animation* opacity_animation =
946 layer_animation_controller_->GetAnimation(Animation::Opacity);
947 return opacity_animation && opacity_animation->is_impl_only();
950 void LayerImpl::SetBlendMode(SkXfermode::Mode blend_mode) {
951 if (blend_mode_ == blend_mode)
952 return;
954 blend_mode_ = blend_mode;
955 NoteLayerPropertyChangedForSubtree();
958 void LayerImpl::SetIsRootForIsolatedGroup(bool root) {
959 if (is_root_for_isolated_group_ == root)
960 return;
962 is_root_for_isolated_group_ = root;
963 SetNeedsPushProperties();
966 void LayerImpl::SetPosition(const gfx::PointF& position) {
967 if (position_ == position)
968 return;
970 position_ = position;
971 NoteLayerPropertyChangedForSubtree();
974 void LayerImpl::SetShouldFlattenTransform(bool flatten) {
975 if (should_flatten_transform_ == flatten)
976 return;
978 should_flatten_transform_ = flatten;
979 NoteLayerPropertyChangedForSubtree();
982 void LayerImpl::Set3dSortingContextId(int id) {
983 if (id == sorting_context_id_)
984 return;
985 sorting_context_id_ = id;
986 NoteLayerPropertyChangedForSubtree();
989 void LayerImpl::SetTransform(const gfx::Transform& transform) {
990 if (transform_ == transform)
991 return;
993 transform_ = transform;
994 transform_is_invertible_ = transform_.IsInvertible();
995 NoteLayerPropertyChangedForSubtree();
998 void LayerImpl::SetTransformAndInvertibility(const gfx::Transform& transform,
999 bool transform_is_invertible) {
1000 if (transform_ == transform) {
1001 DCHECK(transform_is_invertible_ == transform_is_invertible)
1002 << "Can't change invertibility if transform is unchanged";
1003 return;
1005 transform_ = transform;
1006 transform_is_invertible_ = transform_is_invertible;
1007 NoteLayerPropertyChangedForSubtree();
1010 bool LayerImpl::TransformIsAnimating() const {
1011 return layer_animation_controller_->IsAnimatingProperty(Animation::Transform);
1014 bool LayerImpl::TransformIsAnimatingOnImplOnly() const {
1015 Animation* transform_animation =
1016 layer_animation_controller_->GetAnimation(Animation::Transform);
1017 return transform_animation && transform_animation->is_impl_only();
1020 void LayerImpl::SetUpdateRect(const gfx::RectF& update_rect) {
1021 update_rect_ = update_rect;
1022 SetNeedsPushProperties();
1025 void LayerImpl::AddDamageRect(const gfx::RectF& damage_rect) {
1026 damage_rect_ = gfx::UnionRects(damage_rect_, damage_rect);
1029 void LayerImpl::SetContentBounds(const gfx::Size& content_bounds) {
1030 if (this->content_bounds() == content_bounds)
1031 return;
1033 draw_properties_.content_bounds = content_bounds;
1034 NoteLayerPropertyChanged();
1037 void LayerImpl::SetContentsScale(float contents_scale_x,
1038 float contents_scale_y) {
1039 if (this->contents_scale_x() == contents_scale_x &&
1040 this->contents_scale_y() == contents_scale_y)
1041 return;
1043 draw_properties_.contents_scale_x = contents_scale_x;
1044 draw_properties_.contents_scale_y = contents_scale_y;
1045 NoteLayerPropertyChanged();
1048 void LayerImpl::SetScrollOffsetDelegate(
1049 ScrollOffsetDelegate* scroll_offset_delegate) {
1050 // Having both a scroll parent and a scroll offset delegate is unsupported.
1051 DCHECK(!scroll_parent_);
1052 if (!scroll_offset_delegate && scroll_offset_delegate_) {
1053 scroll_delta_ =
1054 scroll_offset_delegate_->GetTotalScrollOffset() - scroll_offset_;
1056 gfx::Vector2dF total_offset = TotalScrollOffset();
1057 scroll_offset_delegate_ = scroll_offset_delegate;
1058 if (scroll_offset_delegate_)
1059 scroll_offset_delegate_->SetTotalScrollOffset(total_offset);
1062 bool LayerImpl::IsExternalFlingActive() const {
1063 return scroll_offset_delegate_ &&
1064 scroll_offset_delegate_->IsExternalFlingActive();
1067 void LayerImpl::SetScrollOffset(const gfx::Vector2d& scroll_offset) {
1068 SetScrollOffsetAndDelta(scroll_offset, ScrollDelta());
1071 void LayerImpl::SetScrollOffsetAndDelta(const gfx::Vector2d& scroll_offset,
1072 const gfx::Vector2dF& scroll_delta) {
1073 bool changed = false;
1075 last_scroll_offset_ = scroll_offset;
1077 if (scroll_offset_ != scroll_offset) {
1078 changed = true;
1079 scroll_offset_ = scroll_offset;
1081 if (scroll_offset_delegate_)
1082 scroll_offset_delegate_->SetTotalScrollOffset(TotalScrollOffset());
1085 if (ScrollDelta() != scroll_delta) {
1086 changed = true;
1087 if (layer_tree_impl()->IsActiveTree()) {
1088 LayerImpl* pending_twin =
1089 layer_tree_impl()->FindPendingTreeLayerById(id());
1090 if (pending_twin) {
1091 // The pending twin can't mirror the scroll delta of the active
1092 // layer. Although the delta - sent scroll delta difference is
1093 // identical for both twins, the sent scroll delta for the pending
1094 // layer is zero, as anything that has been sent has been baked
1095 // into the layer's position/scroll offset as a part of commit.
1096 DCHECK(pending_twin->sent_scroll_delta().IsZero());
1097 pending_twin->SetScrollDelta(scroll_delta - sent_scroll_delta());
1101 if (scroll_offset_delegate_) {
1102 scroll_offset_delegate_->SetTotalScrollOffset(scroll_offset_ +
1103 scroll_delta);
1104 } else {
1105 scroll_delta_ = scroll_delta;
1109 if (changed) {
1110 NoteLayerPropertyChangedForSubtree();
1111 ScrollbarParametersDidChange();
1115 gfx::Vector2dF LayerImpl::ScrollDelta() const {
1116 if (scroll_offset_delegate_)
1117 return scroll_offset_delegate_->GetTotalScrollOffset() - scroll_offset_;
1118 return scroll_delta_;
1121 void LayerImpl::SetScrollDelta(const gfx::Vector2dF& scroll_delta) {
1122 SetScrollOffsetAndDelta(scroll_offset_, scroll_delta);
1125 gfx::Vector2dF LayerImpl::TotalScrollOffset() const {
1126 return scroll_offset_ + ScrollDelta();
1129 void LayerImpl::SetDoubleSided(bool double_sided) {
1130 if (double_sided_ == double_sided)
1131 return;
1133 double_sided_ = double_sided;
1134 NoteLayerPropertyChangedForSubtree();
1137 Region LayerImpl::VisibleContentOpaqueRegion() const {
1138 if (contents_opaque())
1139 return visible_content_rect();
1140 return Region();
1143 void LayerImpl::DidBeginTracing() {}
1145 void LayerImpl::ReleaseResources() {}
1147 gfx::Vector2d LayerImpl::MaxScrollOffset() const {
1148 if (!scroll_clip_layer_ || bounds().IsEmpty())
1149 return gfx::Vector2d();
1151 LayerImpl const* page_scale_layer = layer_tree_impl()->page_scale_layer();
1152 DCHECK(this != page_scale_layer);
1153 DCHECK(this != layer_tree_impl()->InnerViewportScrollLayer() ||
1154 IsContainerForFixedPositionLayers());
1156 gfx::SizeF scaled_scroll_bounds(bounds());
1158 float scale_factor = 1.f;
1159 for (LayerImpl const* current_layer = this;
1160 current_layer != scroll_clip_layer_;
1161 current_layer = current_layer->parent()) {
1162 DCHECK(current_layer);
1163 float current_layer_scale = 1.f;
1165 const gfx::Transform& layer_transform = current_layer->transform();
1166 if (current_layer == page_scale_layer) {
1167 DCHECK(layer_transform.IsIdentity());
1168 current_layer_scale = layer_tree_impl()->total_page_scale_factor();
1169 } else {
1170 // TODO(wjmaclean) Should we allow for translation too?
1171 DCHECK(layer_transform.IsScale2d());
1172 gfx::Vector2dF layer_scale = layer_transform.Scale2d();
1173 // TODO(wjmaclean) Allow for non-isotropic scales.
1174 DCHECK(layer_scale.x() == layer_scale.y());
1175 current_layer_scale = layer_scale.x();
1178 scale_factor *= current_layer_scale;
1180 // TODO(wjmaclean) Once we move to a model where the two-viewport model is
1181 // turned on in all builds, remove the next two lines. For now however, the
1182 // page scale layer may coincide with the clip layer, and so this is
1183 // necessary.
1184 if (page_scale_layer == scroll_clip_layer_)
1185 scale_factor *= layer_tree_impl()->total_page_scale_factor();
1187 scaled_scroll_bounds.SetSize(scale_factor * scaled_scroll_bounds.width(),
1188 scale_factor * scaled_scroll_bounds.height());
1189 scaled_scroll_bounds = gfx::ToFlooredSize(scaled_scroll_bounds);
1191 gfx::Vector2dF max_offset(
1192 scaled_scroll_bounds.width() - scroll_clip_layer_->bounds().width(),
1193 scaled_scroll_bounds.height() - scroll_clip_layer_->bounds().height());
1194 // We need the final scroll offset to be in CSS coords.
1195 max_offset.Scale(1 / scale_factor);
1196 max_offset.SetToMax(gfx::Vector2dF());
1197 return gfx::ToFlooredVector2d(max_offset);
1200 gfx::Vector2dF LayerImpl::ClampScrollToMaxScrollOffset() {
1201 gfx::Vector2dF max_offset = MaxScrollOffset();
1202 gfx::Vector2dF old_offset = TotalScrollOffset();
1203 gfx::Vector2dF clamped_offset = old_offset;
1205 clamped_offset.SetToMin(max_offset);
1206 clamped_offset.SetToMax(gfx::Vector2d());
1207 gfx::Vector2dF delta = clamped_offset - old_offset;
1208 if (!delta.IsZero())
1209 ScrollBy(delta);
1211 return delta;
1214 void LayerImpl::SetScrollbarPosition(ScrollbarLayerImplBase* scrollbar_layer,
1215 LayerImpl* scrollbar_clip_layer) const {
1216 DCHECK(scrollbar_layer);
1217 LayerImpl* page_scale_layer = layer_tree_impl()->page_scale_layer();
1219 DCHECK(this != page_scale_layer);
1220 DCHECK(scrollbar_clip_layer);
1221 DCHECK(this != layer_tree_impl()->InnerViewportScrollLayer() ||
1222 IsContainerForFixedPositionLayers());
1223 gfx::RectF clip_rect(gfx::PointF(), scrollbar_clip_layer->bounds());
1225 // See comment in MaxScrollOffset() regarding the use of the content layer
1226 // bounds here.
1227 gfx::RectF scroll_rect(gfx::PointF(), bounds());
1229 if (scroll_rect.size().IsEmpty())
1230 return;
1232 // TODO(wjmaclean) This computation is nearly identical to the one in
1233 // MaxScrollOffset. Find some way to combine these.
1234 gfx::Vector2dF current_offset;
1235 for (LayerImpl const* current_layer = this;
1236 current_layer != scrollbar_clip_layer;
1237 current_layer = current_layer->parent()) {
1238 DCHECK(current_layer);
1239 const gfx::Transform& layer_transform = current_layer->transform();
1240 if (current_layer == page_scale_layer) {
1241 DCHECK(layer_transform.IsIdentity());
1242 float scale_factor = layer_tree_impl()->total_page_scale_factor();
1243 current_offset.Scale(scale_factor);
1244 scroll_rect.Scale(scale_factor);
1245 } else {
1246 DCHECK(layer_transform.IsScale2d());
1247 gfx::Vector2dF layer_scale = layer_transform.Scale2d();
1248 DCHECK(layer_scale.x() == layer_scale.y());
1249 gfx::Vector2dF new_offset =
1250 current_layer->scroll_offset() + current_layer->ScrollDelta();
1251 new_offset.Scale(layer_scale.x(), layer_scale.y());
1252 current_offset += new_offset;
1255 // TODO(wjmaclean) Once we move to a model where the two-viewport model is
1256 // turned on in all builds, remove the next two lines. For now however, the
1257 // page scale layer may coincide with the clip layer, and so this is
1258 // necessary.
1259 if (page_scale_layer == scrollbar_clip_layer) {
1260 scroll_rect.Scale(layer_tree_impl()->total_page_scale_factor());
1261 current_offset.Scale(layer_tree_impl()->total_page_scale_factor());
1264 scrollbar_layer->SetVerticalAdjust(
1265 layer_tree_impl()->VerticalAdjust(scrollbar_clip_layer->id()));
1266 if (scrollbar_layer->orientation() == HORIZONTAL) {
1267 float visible_ratio = clip_rect.width() / scroll_rect.width();
1268 scrollbar_layer->SetCurrentPos(current_offset.x());
1269 scrollbar_layer->SetMaximum(scroll_rect.width() - clip_rect.width());
1270 scrollbar_layer->SetVisibleToTotalLengthRatio(visible_ratio);
1271 } else {
1272 float visible_ratio = clip_rect.height() / scroll_rect.height();
1273 scrollbar_layer->SetCurrentPos(current_offset.y());
1274 scrollbar_layer->SetMaximum(scroll_rect.height() - clip_rect.height());
1275 scrollbar_layer->SetVisibleToTotalLengthRatio(visible_ratio);
1278 layer_tree_impl()->set_needs_update_draw_properties();
1279 // TODO(wjmaclean) The scrollbar animator for the pinch-zoom scrollbars should
1280 // activate for every scroll on the main frame, not just the scrolls that move
1281 // the pinch virtual viewport (i.e. trigger from either inner or outer
1282 // viewport).
1283 if (scrollbar_animation_controller_) {
1284 // When both non-overlay and overlay scrollbars are both present, don't
1285 // animate the overlay scrollbars when page scale factor is at the min.
1286 // Non-overlay scrollbars also shouldn't trigger animations.
1287 bool is_animatable_scrollbar =
1288 scrollbar_layer->is_overlay_scrollbar() &&
1289 ((layer_tree_impl()->total_page_scale_factor() >
1290 layer_tree_impl()->min_page_scale_factor()) ||
1291 !layer_tree_impl()->settings().use_pinch_zoom_scrollbars);
1292 if (is_animatable_scrollbar)
1293 scrollbar_animation_controller_->DidScrollUpdate();
1297 void LayerImpl::DidBecomeActive() {
1298 if (layer_tree_impl_->settings().scrollbar_animator ==
1299 LayerTreeSettings::NoAnimator) {
1300 return;
1303 bool need_scrollbar_animation_controller = scrollable() && scrollbars_;
1304 if (!need_scrollbar_animation_controller) {
1305 scrollbar_animation_controller_.reset();
1306 return;
1309 if (scrollbar_animation_controller_)
1310 return;
1312 scrollbar_animation_controller_ =
1313 layer_tree_impl_->CreateScrollbarAnimationController(this);
1316 void LayerImpl::ClearScrollbars() {
1317 if (!scrollbars_)
1318 return;
1320 scrollbars_.reset(NULL);
1323 void LayerImpl::AddScrollbar(ScrollbarLayerImplBase* layer) {
1324 DCHECK(layer);
1325 DCHECK(!scrollbars_ || scrollbars_->find(layer) == scrollbars_->end());
1326 if (!scrollbars_)
1327 scrollbars_.reset(new ScrollbarSet());
1329 scrollbars_->insert(layer);
1332 void LayerImpl::RemoveScrollbar(ScrollbarLayerImplBase* layer) {
1333 DCHECK(scrollbars_);
1334 DCHECK(layer);
1335 DCHECK(scrollbars_->find(layer) != scrollbars_->end());
1337 scrollbars_->erase(layer);
1338 if (scrollbars_->empty())
1339 scrollbars_.reset();
1342 bool LayerImpl::HasScrollbar(ScrollbarOrientation orientation) const {
1343 if (!scrollbars_)
1344 return false;
1346 for (ScrollbarSet::iterator it = scrollbars_->begin();
1347 it != scrollbars_->end();
1348 ++it)
1349 if ((*it)->orientation() == orientation)
1350 return true;
1352 return false;
1355 void LayerImpl::ScrollbarParametersDidChange() {
1356 if (!scrollbars_)
1357 return;
1359 for (ScrollbarSet::iterator it = scrollbars_->begin();
1360 it != scrollbars_->end();
1361 ++it)
1362 (*it)->ScrollbarParametersDidChange();
1365 void LayerImpl::SetNeedsPushProperties() {
1366 if (needs_push_properties_)
1367 return;
1368 if (!parent_should_know_need_push_properties() && parent_)
1369 parent_->AddDependentNeedsPushProperties();
1370 needs_push_properties_ = true;
1373 void LayerImpl::AddDependentNeedsPushProperties() {
1374 DCHECK_GE(num_dependents_need_push_properties_, 0);
1376 if (!parent_should_know_need_push_properties() && parent_)
1377 parent_->AddDependentNeedsPushProperties();
1379 num_dependents_need_push_properties_++;
1382 void LayerImpl::RemoveDependentNeedsPushProperties() {
1383 num_dependents_need_push_properties_--;
1384 DCHECK_GE(num_dependents_need_push_properties_, 0);
1386 if (!parent_should_know_need_push_properties() && parent_)
1387 parent_->RemoveDependentNeedsPushProperties();
1390 void LayerImpl::AsValueInto(base::DictionaryValue* state) const {
1391 TracedValue::MakeDictIntoImplicitSnapshotWithCategory(
1392 TRACE_DISABLED_BY_DEFAULT("cc.debug"),
1393 state,
1394 "cc::LayerImpl",
1395 LayerTypeAsString(),
1396 this);
1397 state->SetInteger("layer_id", id());
1398 state->Set("bounds", MathUtil::AsValue(bounds_).release());
1399 state->Set("position", MathUtil::AsValue(position_).release());
1400 state->SetInteger("draws_content", DrawsContent());
1401 state->SetInteger("gpu_memory_usage", GPUMemoryUsageInBytes());
1402 state->Set("scroll_offset", MathUtil::AsValue(scroll_offset_).release());
1403 state->Set("transform_origin",
1404 MathUtil::AsValue(transform_origin_).release());
1406 bool clipped;
1407 gfx::QuadF layer_quad = MathUtil::MapQuad(
1408 screen_space_transform(),
1409 gfx::QuadF(gfx::Rect(content_bounds())),
1410 &clipped);
1411 state->Set("layer_quad", MathUtil::AsValue(layer_quad).release());
1413 if (!touch_event_handler_region_.IsEmpty()) {
1414 state->Set("touch_event_handler_region",
1415 touch_event_handler_region_.AsValue().release());
1417 if (have_wheel_event_handlers_) {
1418 gfx::Rect wheel_rect(content_bounds());
1419 Region wheel_region(wheel_rect);
1420 state->Set("wheel_event_handler_region",
1421 wheel_region.AsValue().release());
1423 if (have_scroll_event_handlers_) {
1424 gfx::Rect scroll_rect(content_bounds());
1425 Region scroll_region(scroll_rect);
1426 state->Set("scroll_event_handler_region",
1427 scroll_region.AsValue().release());
1429 if (!non_fast_scrollable_region_.IsEmpty()) {
1430 state->Set("non_fast_scrollable_region",
1431 non_fast_scrollable_region_.AsValue().release());
1434 scoped_ptr<base::ListValue> children_list(new base::ListValue());
1435 for (size_t i = 0; i < children_.size(); ++i)
1436 children_list->Append(children_[i]->AsValue().release());
1437 state->Set("children", children_list.release());
1438 if (mask_layer_)
1439 state->Set("mask_layer", mask_layer_->AsValue().release());
1440 if (replica_layer_)
1441 state->Set("replica_layer", replica_layer_->AsValue().release());
1443 if (scroll_parent_)
1444 state->SetInteger("scroll_parent", scroll_parent_->id());
1446 if (clip_parent_)
1447 state->SetInteger("clip_parent", clip_parent_->id());
1449 state->SetBoolean("can_use_lcd_text", can_use_lcd_text());
1450 state->SetBoolean("contents_opaque", contents_opaque());
1452 state->SetBoolean(
1453 "has_animation_bounds",
1454 layer_animation_controller()->HasAnimationThatInflatesBounds());
1456 gfx::BoxF box;
1457 if (LayerUtils::GetAnimationBounds(*this, &box))
1458 state->Set("animation_bounds", MathUtil::AsValue(box).release());
1460 if (debug_info_.get()) {
1461 std::string str;
1462 debug_info_->AppendAsTraceFormat(&str);
1463 base::JSONReader json_reader;
1464 scoped_ptr<base::Value> debug_info_value(json_reader.ReadToValue(str));
1466 if (debug_info_value->IsType(base::Value::TYPE_DICTIONARY)) {
1467 base::DictionaryValue* dictionary_value = NULL;
1468 bool converted_to_dictionary =
1469 debug_info_value->GetAsDictionary(&dictionary_value);
1470 DCHECK(converted_to_dictionary);
1471 state->MergeDictionary(dictionary_value);
1472 } else {
1473 NOTREACHED();
1478 bool LayerImpl::IsDrawnRenderSurfaceLayerListMember() const {
1479 return draw_properties_.last_drawn_render_surface_layer_list_id ==
1480 layer_tree_impl_->current_render_surface_list_id();
1483 size_t LayerImpl::GPUMemoryUsageInBytes() const { return 0; }
1485 scoped_ptr<base::Value> LayerImpl::AsValue() const {
1486 scoped_ptr<base::DictionaryValue> state(new base::DictionaryValue());
1487 AsValueInto(state.get());
1488 return state.PassAs<base::Value>();
1491 void LayerImpl::RunMicroBenchmark(MicroBenchmarkImpl* benchmark) {
1492 benchmark->RunOnLayer(this);
1494 } // namespace cc