[GCM] Investigatory CHECKs for crash in parsing stream
[chromium-blink-merge.git] / cc / layers / layer_impl.cc
blob29bdfe778fdf522fc9a3a8f97d74ef4adb90b71c
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/debug/trace_event_argument.h"
9 #include "base/json/json_reader.h"
10 #include "base/strings/stringprintf.h"
11 #include "cc/animation/animation_registrar.h"
12 #include "cc/animation/scrollbar_animation_controller.h"
13 #include "cc/base/math_util.h"
14 #include "cc/base/simple_enclosed_region.h"
15 #include "cc/debug/debug_colors.h"
16 #include "cc/debug/layer_tree_debug_state.h"
17 #include "cc/debug/micro_benchmark_impl.h"
18 #include "cc/debug/traced_value.h"
19 #include "cc/input/layer_scroll_offset_delegate.h"
20 #include "cc/layers/layer_utils.h"
21 #include "cc/layers/painted_scrollbar_layer_impl.h"
22 #include "cc/output/copy_output_request.h"
23 #include "cc/quads/debug_border_draw_quad.h"
24 #include "cc/quads/render_pass.h"
25 #include "cc/trees/layer_tree_host_common.h"
26 #include "cc/trees/layer_tree_impl.h"
27 #include "cc/trees/layer_tree_settings.h"
28 #include "cc/trees/proxy.h"
29 #include "ui/gfx/box_f.h"
30 #include "ui/gfx/geometry/vector2d_conversions.h"
31 #include "ui/gfx/point_conversions.h"
32 #include "ui/gfx/quad_f.h"
33 #include "ui/gfx/rect_conversions.h"
34 #include "ui/gfx/size_conversions.h"
36 namespace cc {
37 LayerImpl::LayerImpl(LayerTreeImpl* tree_impl, int id)
38 : parent_(NULL),
39 scroll_parent_(NULL),
40 clip_parent_(NULL),
41 mask_layer_id_(-1),
42 replica_layer_id_(-1),
43 layer_id_(id),
44 layer_tree_impl_(tree_impl),
45 scroll_offset_delegate_(NULL),
46 scroll_clip_layer_(NULL),
47 should_scroll_on_main_thread_(false),
48 have_wheel_event_handlers_(false),
49 have_scroll_event_handlers_(false),
50 user_scrollable_horizontal_(true),
51 user_scrollable_vertical_(true),
52 stacking_order_changed_(false),
53 double_sided_(true),
54 should_flatten_transform_(true),
55 layer_property_changed_(false),
56 masks_to_bounds_(false),
57 contents_opaque_(false),
58 is_root_for_isolated_group_(false),
59 use_parent_backface_visibility_(false),
60 draw_checkerboard_for_missing_tiles_(false),
61 draws_content_(false),
62 hide_layer_and_subtree_(false),
63 force_render_surface_(false),
64 transform_is_invertible_(true),
65 is_container_for_fixed_position_layers_(false),
66 background_color_(0),
67 opacity_(1.0),
68 blend_mode_(SkXfermode::kSrcOver_Mode),
69 num_descendants_that_draw_content_(0),
70 draw_depth_(0.f),
71 needs_push_properties_(false),
72 num_dependents_need_push_properties_(0),
73 sorting_context_id_(0),
74 current_draw_mode_(DRAW_MODE_NONE) {
75 DCHECK_GT(layer_id_, 0);
76 DCHECK(layer_tree_impl_);
77 layer_tree_impl_->RegisterLayer(this);
78 AnimationRegistrar* registrar = layer_tree_impl_->animationRegistrar();
79 layer_animation_controller_ =
80 registrar->GetAnimationControllerForId(layer_id_);
81 layer_animation_controller_->AddValueObserver(this);
82 if (IsActive()) {
83 layer_animation_controller_->set_value_provider(this);
84 layer_animation_controller_->set_layer_animation_delegate(this);
86 SetNeedsPushProperties();
89 LayerImpl::~LayerImpl() {
90 DCHECK_EQ(DRAW_MODE_NONE, current_draw_mode_);
92 layer_animation_controller_->RemoveValueObserver(this);
93 layer_animation_controller_->remove_value_provider(this);
94 layer_animation_controller_->remove_layer_animation_delegate(this);
96 if (!copy_requests_.empty() && layer_tree_impl_->IsActiveTree())
97 layer_tree_impl()->RemoveLayerWithCopyOutputRequest(this);
98 layer_tree_impl_->UnregisterLayer(this);
100 TRACE_EVENT_OBJECT_DELETED_WITH_ID(
101 TRACE_DISABLED_BY_DEFAULT("cc.debug"), "cc::LayerImpl", this);
104 void LayerImpl::AddChild(scoped_ptr<LayerImpl> child) {
105 child->SetParent(this);
106 DCHECK_EQ(layer_tree_impl(), child->layer_tree_impl());
107 children_.push_back(child.Pass());
108 layer_tree_impl()->set_needs_update_draw_properties();
111 scoped_ptr<LayerImpl> LayerImpl::RemoveChild(LayerImpl* child) {
112 for (OwnedLayerImplList::iterator it = children_.begin();
113 it != children_.end();
114 ++it) {
115 if (*it == child) {
116 scoped_ptr<LayerImpl> ret = children_.take(it);
117 children_.erase(it);
118 layer_tree_impl()->set_needs_update_draw_properties();
119 return ret.Pass();
122 return scoped_ptr<LayerImpl>();
125 void LayerImpl::SetParent(LayerImpl* parent) {
126 if (parent_should_know_need_push_properties()) {
127 if (parent_)
128 parent_->RemoveDependentNeedsPushProperties();
129 if (parent)
130 parent->AddDependentNeedsPushProperties();
132 parent_ = parent;
135 void LayerImpl::ClearChildList() {
136 if (children_.empty())
137 return;
139 children_.clear();
140 layer_tree_impl()->set_needs_update_draw_properties();
143 bool LayerImpl::HasAncestor(const LayerImpl* ancestor) const {
144 if (!ancestor)
145 return false;
147 for (const LayerImpl* layer = this; layer; layer = layer->parent()) {
148 if (layer == ancestor)
149 return true;
152 return false;
155 void LayerImpl::SetScrollParent(LayerImpl* parent) {
156 if (scroll_parent_ == parent)
157 return;
159 // Having both a scroll parent and a scroll offset delegate is unsupported.
160 DCHECK(!scroll_offset_delegate_);
162 if (parent)
163 DCHECK_EQ(layer_tree_impl()->LayerById(parent->id()), parent);
165 scroll_parent_ = parent;
166 SetNeedsPushProperties();
169 void LayerImpl::SetDebugInfo(
170 scoped_refptr<base::debug::ConvertableToTraceFormat> other) {
171 debug_info_ = other;
172 SetNeedsPushProperties();
175 void LayerImpl::SetScrollChildren(std::set<LayerImpl*>* children) {
176 if (scroll_children_.get() == children)
177 return;
178 scroll_children_.reset(children);
179 SetNeedsPushProperties();
182 void LayerImpl::SetNumDescendantsThatDrawContent(int num_descendants) {
183 if (num_descendants_that_draw_content_ == num_descendants)
184 return;
185 num_descendants_that_draw_content_ = num_descendants;
186 SetNeedsPushProperties();
189 void LayerImpl::SetClipParent(LayerImpl* ancestor) {
190 if (clip_parent_ == ancestor)
191 return;
193 clip_parent_ = ancestor;
194 SetNeedsPushProperties();
197 void LayerImpl::SetClipChildren(std::set<LayerImpl*>* children) {
198 if (clip_children_.get() == children)
199 return;
200 clip_children_.reset(children);
201 SetNeedsPushProperties();
204 void LayerImpl::PassCopyRequests(ScopedPtrVector<CopyOutputRequest>* requests) {
205 if (requests->empty())
206 return;
208 bool was_empty = copy_requests_.empty();
209 copy_requests_.insert_and_take(copy_requests_.end(), requests);
210 requests->clear();
212 if (was_empty && layer_tree_impl()->IsActiveTree())
213 layer_tree_impl()->AddLayerWithCopyOutputRequest(this);
214 NoteLayerPropertyChangedForSubtree();
217 void LayerImpl::TakeCopyRequestsAndTransformToTarget(
218 ScopedPtrVector<CopyOutputRequest>* requests) {
219 DCHECK(!copy_requests_.empty());
220 DCHECK(layer_tree_impl()->IsActiveTree());
222 size_t first_inserted_request = requests->size();
223 requests->insert_and_take(requests->end(), &copy_requests_);
224 copy_requests_.clear();
226 for (size_t i = first_inserted_request; i < requests->size(); ++i) {
227 CopyOutputRequest* request = requests->at(i);
228 if (!request->has_area())
229 continue;
231 gfx::Rect request_in_layer_space = request->area();
232 gfx::Rect request_in_content_space =
233 LayerRectToContentRect(request_in_layer_space);
234 request->set_area(MathUtil::MapEnclosingClippedRect(
235 draw_properties_.target_space_transform, request_in_content_space));
238 layer_tree_impl()->RemoveLayerWithCopyOutputRequest(this);
241 void LayerImpl::CreateRenderSurface() {
242 DCHECK(!draw_properties_.render_surface);
243 draw_properties_.render_surface =
244 make_scoped_ptr(new RenderSurfaceImpl(this));
245 draw_properties_.render_target = this;
248 void LayerImpl::ClearRenderSurface() {
249 draw_properties_.render_surface.reset();
252 void LayerImpl::ClearRenderSurfaceLayerList() {
253 if (draw_properties_.render_surface)
254 draw_properties_.render_surface->layer_list().clear();
257 void LayerImpl::PopulateSharedQuadState(SharedQuadState* state) const {
258 state->SetAll(draw_properties_.target_space_transform,
259 draw_properties_.content_bounds,
260 draw_properties_.visible_content_rect,
261 draw_properties_.clip_rect,
262 draw_properties_.is_clipped,
263 draw_properties_.opacity,
264 blend_mode_,
265 sorting_context_id_);
268 bool LayerImpl::WillDraw(DrawMode draw_mode,
269 ResourceProvider* resource_provider) {
270 // WillDraw/DidDraw must be matched.
271 DCHECK_NE(DRAW_MODE_NONE, draw_mode);
272 DCHECK_EQ(DRAW_MODE_NONE, current_draw_mode_);
273 current_draw_mode_ = draw_mode;
274 return true;
277 void LayerImpl::DidDraw(ResourceProvider* resource_provider) {
278 DCHECK_NE(DRAW_MODE_NONE, current_draw_mode_);
279 current_draw_mode_ = DRAW_MODE_NONE;
282 bool LayerImpl::ShowDebugBorders() const {
283 return layer_tree_impl()->debug_state().show_debug_borders;
286 void LayerImpl::GetDebugBorderProperties(SkColor* color, float* width) const {
287 if (draws_content_) {
288 *color = DebugColors::ContentLayerBorderColor();
289 *width = DebugColors::ContentLayerBorderWidth(layer_tree_impl());
290 return;
293 if (masks_to_bounds_) {
294 *color = DebugColors::MaskingLayerBorderColor();
295 *width = DebugColors::MaskingLayerBorderWidth(layer_tree_impl());
296 return;
299 *color = DebugColors::ContainerLayerBorderColor();
300 *width = DebugColors::ContainerLayerBorderWidth(layer_tree_impl());
303 void LayerImpl::AppendDebugBorderQuad(
304 RenderPass* render_pass,
305 const gfx::Size& content_bounds,
306 const SharedQuadState* shared_quad_state,
307 AppendQuadsData* append_quads_data) const {
308 SkColor color;
309 float width;
310 GetDebugBorderProperties(&color, &width);
311 AppendDebugBorderQuad(render_pass,
312 content_bounds,
313 shared_quad_state,
314 append_quads_data,
315 color,
316 width);
319 void LayerImpl::AppendDebugBorderQuad(RenderPass* render_pass,
320 const gfx::Size& content_bounds,
321 const SharedQuadState* shared_quad_state,
322 AppendQuadsData* append_quads_data,
323 SkColor color,
324 float width) const {
325 if (!ShowDebugBorders())
326 return;
328 gfx::Rect quad_rect(content_bounds);
329 gfx::Rect visible_quad_rect(quad_rect);
330 DebugBorderDrawQuad* debug_border_quad =
331 render_pass->CreateAndAppendDrawQuad<DebugBorderDrawQuad>();
332 debug_border_quad->SetNew(
333 shared_quad_state, quad_rect, visible_quad_rect, color, width);
336 bool LayerImpl::HasDelegatedContent() const {
337 return false;
340 bool LayerImpl::HasContributingDelegatedRenderPasses() const {
341 return false;
344 RenderPassId LayerImpl::FirstContributingRenderPassId() const {
345 return RenderPassId(0, 0);
348 RenderPassId LayerImpl::NextContributingRenderPassId(RenderPassId id) const {
349 return RenderPassId(0, 0);
352 ResourceProvider::ResourceId LayerImpl::ContentsResourceId() const {
353 NOTREACHED();
354 return 0;
357 void LayerImpl::SetSentScrollDelta(const gfx::Vector2d& sent_scroll_delta) {
358 // Pending tree never has sent scroll deltas
359 DCHECK(layer_tree_impl()->IsActiveTree());
361 if (sent_scroll_delta_ == sent_scroll_delta)
362 return;
364 sent_scroll_delta_ = sent_scroll_delta;
367 gfx::Vector2dF LayerImpl::ScrollBy(const gfx::Vector2dF& scroll) {
368 DCHECK(scrollable());
369 gfx::Vector2dF min_delta = -scroll_offset_;
370 gfx::Vector2dF max_delta = MaxScrollOffset() - scroll_offset_;
371 // Clamp new_delta so that position + delta stays within scroll bounds.
372 gfx::Vector2dF new_delta = (ScrollDelta() + scroll);
373 new_delta.SetToMax(min_delta);
374 new_delta.SetToMin(max_delta);
375 gfx::Vector2dF unscrolled =
376 ScrollDelta() + scroll - new_delta;
377 SetScrollDelta(new_delta);
379 return unscrolled;
382 void LayerImpl::SetScrollClipLayer(int scroll_clip_layer_id) {
383 scroll_clip_layer_ = layer_tree_impl()->LayerById(scroll_clip_layer_id);
386 void LayerImpl::ApplySentScrollDeltasFromAbortedCommit() {
387 // Pending tree never has sent scroll deltas
388 DCHECK(layer_tree_impl()->IsActiveTree());
390 // Apply sent scroll deltas to scroll position / scroll delta as if the
391 // main thread had applied them and then committed those values.
393 // This function should not change the total scroll offset; it just shifts
394 // some of the scroll delta to the scroll offset. Therefore, adjust these
395 // variables directly rather than calling the scroll offset delegate to
396 // avoid sending it multiple spurious calls.
398 // Because of the way scroll delta is calculated with a delegate, this will
399 // leave the total scroll offset unchanged on this layer regardless of
400 // whether a delegate is being used.
401 scroll_offset_ += sent_scroll_delta_;
402 scroll_delta_ -= sent_scroll_delta_;
403 sent_scroll_delta_ = gfx::Vector2d();
406 void LayerImpl::ApplyScrollDeltasSinceBeginMainFrame() {
407 // Only the pending tree can have missing scrolls.
408 DCHECK(layer_tree_impl()->IsPendingTree());
409 if (!scrollable())
410 return;
412 // Pending tree should never have sent scroll deltas.
413 DCHECK(sent_scroll_delta().IsZero());
415 LayerImpl* active_twin = layer_tree_impl()->FindActiveTreeLayerById(id());
416 if (active_twin) {
417 // Scrolls that happens after begin frame (where the sent scroll delta
418 // comes from) and commit need to be applied to the pending tree
419 // so that it is up to date with the total scroll.
420 SetScrollDelta(active_twin->ScrollDelta() -
421 active_twin->sent_scroll_delta());
425 InputHandler::ScrollStatus LayerImpl::TryScroll(
426 const gfx::PointF& screen_space_point,
427 InputHandler::ScrollInputType type) const {
428 if (should_scroll_on_main_thread()) {
429 TRACE_EVENT0("cc", "LayerImpl::TryScroll: Failed ShouldScrollOnMainThread");
430 return InputHandler::ScrollOnMainThread;
433 if (!screen_space_transform().IsInvertible()) {
434 TRACE_EVENT0("cc", "LayerImpl::TryScroll: Ignored NonInvertibleTransform");
435 return InputHandler::ScrollIgnored;
438 if (!non_fast_scrollable_region().IsEmpty()) {
439 bool clipped = false;
440 gfx::Transform inverse_screen_space_transform(
441 gfx::Transform::kSkipInitialization);
442 if (!screen_space_transform().GetInverse(&inverse_screen_space_transform)) {
443 // TODO(shawnsingh): We shouldn't be applying a projection if screen space
444 // transform is uninvertible here. Perhaps we should be returning
445 // ScrollOnMainThread in this case?
448 gfx::PointF hit_test_point_in_content_space =
449 MathUtil::ProjectPoint(inverse_screen_space_transform,
450 screen_space_point,
451 &clipped);
452 gfx::PointF hit_test_point_in_layer_space =
453 gfx::ScalePoint(hit_test_point_in_content_space,
454 1.f / contents_scale_x(),
455 1.f / contents_scale_y());
456 if (!clipped &&
457 non_fast_scrollable_region().Contains(
458 gfx::ToRoundedPoint(hit_test_point_in_layer_space))) {
459 TRACE_EVENT0("cc",
460 "LayerImpl::tryScroll: Failed NonFastScrollableRegion");
461 return InputHandler::ScrollOnMainThread;
465 if (type == InputHandler::Wheel && have_wheel_event_handlers()) {
466 TRACE_EVENT0("cc", "LayerImpl::tryScroll: Failed WheelEventHandlers");
467 return InputHandler::ScrollOnMainThread;
470 if (!scrollable()) {
471 TRACE_EVENT0("cc", "LayerImpl::tryScroll: Ignored not scrollable");
472 return InputHandler::ScrollIgnored;
475 gfx::Vector2d max_scroll_offset = MaxScrollOffset();
476 if (max_scroll_offset.x() <= 0 && max_scroll_offset.y() <= 0) {
477 TRACE_EVENT0("cc",
478 "LayerImpl::tryScroll: Ignored. Technically scrollable,"
479 " but has no affordance in either direction.");
480 return InputHandler::ScrollIgnored;
483 return InputHandler::ScrollStarted;
486 gfx::Rect LayerImpl::LayerRectToContentRect(
487 const gfx::RectF& layer_rect) const {
488 gfx::RectF content_rect =
489 gfx::ScaleRect(layer_rect, contents_scale_x(), contents_scale_y());
490 // Intersect with content rect to avoid the extra pixel because for some
491 // values x and y, ceil((x / y) * y) may be x + 1.
492 content_rect.Intersect(gfx::Rect(content_bounds()));
493 return gfx::ToEnclosingRect(content_rect);
496 skia::RefPtr<SkPicture> LayerImpl::GetPicture() {
497 return skia::RefPtr<SkPicture>();
500 scoped_ptr<LayerImpl> LayerImpl::CreateLayerImpl(LayerTreeImpl* tree_impl) {
501 return LayerImpl::Create(tree_impl, layer_id_);
504 void LayerImpl::PushPropertiesTo(LayerImpl* layer) {
505 layer->SetTransformOrigin(transform_origin_);
506 layer->SetBackgroundColor(background_color_);
507 layer->SetBounds(bounds_);
508 layer->SetContentBounds(content_bounds());
509 layer->SetContentsScale(contents_scale_x(), contents_scale_y());
510 layer->SetDoubleSided(double_sided_);
511 layer->SetDrawCheckerboardForMissingTiles(
512 draw_checkerboard_for_missing_tiles_);
513 layer->SetForceRenderSurface(force_render_surface_);
514 layer->SetDrawsContent(DrawsContent());
515 layer->SetHideLayerAndSubtree(hide_layer_and_subtree_);
516 layer->SetFilters(filters());
517 layer->SetBackgroundFilters(background_filters());
518 layer->SetMasksToBounds(masks_to_bounds_);
519 layer->SetShouldScrollOnMainThread(should_scroll_on_main_thread_);
520 layer->SetHaveWheelEventHandlers(have_wheel_event_handlers_);
521 layer->SetHaveScrollEventHandlers(have_scroll_event_handlers_);
522 layer->SetNonFastScrollableRegion(non_fast_scrollable_region_);
523 layer->SetTouchEventHandlerRegion(touch_event_handler_region_);
524 layer->SetContentsOpaque(contents_opaque_);
525 layer->SetOpacity(opacity_);
526 layer->SetBlendMode(blend_mode_);
527 layer->SetIsRootForIsolatedGroup(is_root_for_isolated_group_);
528 layer->SetPosition(position_);
529 layer->SetIsContainerForFixedPositionLayers(
530 is_container_for_fixed_position_layers_);
531 layer->SetPositionConstraint(position_constraint_);
532 layer->SetShouldFlattenTransform(should_flatten_transform_);
533 layer->SetUseParentBackfaceVisibility(use_parent_backface_visibility_);
534 layer->SetTransformAndInvertibility(transform_, transform_is_invertible_);
536 layer->SetScrollClipLayer(scroll_clip_layer_ ? scroll_clip_layer_->id()
537 : Layer::INVALID_ID);
538 layer->set_user_scrollable_horizontal(user_scrollable_horizontal_);
539 layer->set_user_scrollable_vertical(user_scrollable_vertical_);
540 layer->SetScrollOffsetAndDelta(
541 scroll_offset_, layer->ScrollDelta() - layer->sent_scroll_delta());
542 layer->SetSentScrollDelta(gfx::Vector2d());
543 layer->Set3dSortingContextId(sorting_context_id_);
544 layer->SetNumDescendantsThatDrawContent(num_descendants_that_draw_content_);
546 LayerImpl* scroll_parent = NULL;
547 if (scroll_parent_) {
548 scroll_parent = layer->layer_tree_impl()->LayerById(scroll_parent_->id());
549 DCHECK(scroll_parent);
552 layer->SetScrollParent(scroll_parent);
553 if (scroll_children_) {
554 std::set<LayerImpl*>* scroll_children = new std::set<LayerImpl*>;
555 for (std::set<LayerImpl*>::iterator it = scroll_children_->begin();
556 it != scroll_children_->end();
557 ++it) {
558 DCHECK_EQ((*it)->scroll_parent(), this);
559 LayerImpl* scroll_child =
560 layer->layer_tree_impl()->LayerById((*it)->id());
561 DCHECK(scroll_child);
562 scroll_children->insert(scroll_child);
564 layer->SetScrollChildren(scroll_children);
565 } else {
566 layer->SetScrollChildren(NULL);
569 LayerImpl* clip_parent = NULL;
570 if (clip_parent_) {
571 clip_parent = layer->layer_tree_impl()->LayerById(
572 clip_parent_->id());
573 DCHECK(clip_parent);
576 layer->SetClipParent(clip_parent);
577 if (clip_children_) {
578 std::set<LayerImpl*>* clip_children = new std::set<LayerImpl*>;
579 for (std::set<LayerImpl*>::iterator it = clip_children_->begin();
580 it != clip_children_->end(); ++it)
581 clip_children->insert(layer->layer_tree_impl()->LayerById((*it)->id()));
582 layer->SetClipChildren(clip_children);
583 } else {
584 layer->SetClipChildren(NULL);
587 layer->PassCopyRequests(&copy_requests_);
589 // If the main thread commits multiple times before the impl thread actually
590 // draws, then damage tracking will become incorrect if we simply clobber the
591 // update_rect here. The LayerImpl's update_rect needs to accumulate (i.e.
592 // union) any update changes that have occurred on the main thread.
593 update_rect_.Union(layer->update_rect());
594 layer->SetUpdateRect(update_rect_);
596 layer->SetStackingOrderChanged(stacking_order_changed_);
597 layer->SetDebugInfo(debug_info_);
599 // Reset any state that should be cleared for the next update.
600 stacking_order_changed_ = false;
601 update_rect_ = gfx::RectF();
602 needs_push_properties_ = false;
603 num_dependents_need_push_properties_ = 0;
606 gfx::Vector2dF LayerImpl::FixedContainerSizeDelta() const {
607 if (!scroll_clip_layer_)
608 return gfx::Vector2dF();
610 float scale_delta = layer_tree_impl()->page_scale_delta();
611 float scale = layer_tree_impl()->page_scale_factor();
613 gfx::Vector2dF delta_from_scroll = scroll_clip_layer_->bounds_delta();
614 delta_from_scroll.Scale(1.f / scale);
616 // The delta-from-pinch component requires some explanation: A viewport of
617 // size (w,h) will appear to be size (w/s,h/s) under scale s in the content
618 // space. If s -> s' on the impl thread, where s' = s * ds, then the apparent
619 // viewport size change in the content space due to ds is:
621 // (w/s',h/s') - (w/s,h/s) = (w,h)(1/s' - 1/s) = (w,h)(1 - ds)/(s ds)
623 gfx::Vector2dF delta_from_pinch =
624 gfx::Rect(scroll_clip_layer_->bounds()).bottom_right() - gfx::PointF();
625 delta_from_pinch.Scale((1.f - scale_delta) / (scale * scale_delta));
627 return delta_from_scroll + delta_from_pinch;
630 base::DictionaryValue* LayerImpl::LayerTreeAsJson() const {
631 base::DictionaryValue* result = new base::DictionaryValue;
632 result->SetString("LayerType", LayerTypeAsString());
634 base::ListValue* list = new base::ListValue;
635 list->AppendInteger(bounds().width());
636 list->AppendInteger(bounds().height());
637 result->Set("Bounds", list);
639 list = new base::ListValue;
640 list->AppendDouble(position_.x());
641 list->AppendDouble(position_.y());
642 result->Set("Position", list);
644 const gfx::Transform& gfx_transform = draw_properties_.target_space_transform;
645 double transform[16];
646 gfx_transform.matrix().asColMajord(transform);
647 list = new base::ListValue;
648 for (int i = 0; i < 16; ++i)
649 list->AppendDouble(transform[i]);
650 result->Set("DrawTransform", list);
652 result->SetBoolean("DrawsContent", draws_content_);
653 result->SetBoolean("Is3dSorted", Is3dSorted());
654 result->SetDouble("Opacity", opacity());
655 result->SetBoolean("ContentsOpaque", contents_opaque_);
657 if (scrollable())
658 result->SetBoolean("Scrollable", true);
660 if (have_wheel_event_handlers_)
661 result->SetBoolean("WheelHandler", have_wheel_event_handlers_);
662 if (have_scroll_event_handlers_)
663 result->SetBoolean("ScrollHandler", have_scroll_event_handlers_);
664 if (!touch_event_handler_region_.IsEmpty()) {
665 scoped_ptr<base::Value> region = touch_event_handler_region_.AsValue();
666 result->Set("TouchRegion", region.release());
669 list = new base::ListValue;
670 for (size_t i = 0; i < children_.size(); ++i)
671 list->Append(children_[i]->LayerTreeAsJson());
672 result->Set("Children", list);
674 return result;
677 void LayerImpl::SetStackingOrderChanged(bool stacking_order_changed) {
678 if (stacking_order_changed) {
679 stacking_order_changed_ = true;
680 NoteLayerPropertyChangedForSubtree();
684 void LayerImpl::NoteLayerPropertyChanged() {
685 layer_property_changed_ = true;
686 layer_tree_impl()->set_needs_update_draw_properties();
687 SetNeedsPushProperties();
690 void LayerImpl::NoteLayerPropertyChangedForSubtree() {
691 layer_property_changed_ = true;
692 layer_tree_impl()->set_needs_update_draw_properties();
693 for (size_t i = 0; i < children_.size(); ++i)
694 children_[i]->NoteLayerPropertyChangedForDescendantsInternal();
695 SetNeedsPushProperties();
698 void LayerImpl::NoteLayerPropertyChangedForDescendantsInternal() {
699 layer_property_changed_ = true;
700 for (size_t i = 0; i < children_.size(); ++i)
701 children_[i]->NoteLayerPropertyChangedForDescendantsInternal();
704 void LayerImpl::NoteLayerPropertyChangedForDescendants() {
705 layer_tree_impl()->set_needs_update_draw_properties();
706 for (size_t i = 0; i < children_.size(); ++i)
707 children_[i]->NoteLayerPropertyChangedForDescendantsInternal();
708 SetNeedsPushProperties();
711 const char* LayerImpl::LayerTypeAsString() const {
712 return "cc::LayerImpl";
715 void LayerImpl::ResetAllChangeTrackingForSubtree() {
716 layer_property_changed_ = false;
718 update_rect_ = gfx::RectF();
719 damage_rect_ = gfx::RectF();
721 if (draw_properties_.render_surface)
722 draw_properties_.render_surface->ResetPropertyChangedFlag();
724 if (mask_layer_)
725 mask_layer_->ResetAllChangeTrackingForSubtree();
727 if (replica_layer_) {
728 // This also resets the replica mask, if it exists.
729 replica_layer_->ResetAllChangeTrackingForSubtree();
732 for (size_t i = 0; i < children_.size(); ++i)
733 children_[i]->ResetAllChangeTrackingForSubtree();
735 needs_push_properties_ = false;
736 num_dependents_need_push_properties_ = 0;
739 gfx::Vector2dF LayerImpl::ScrollOffsetForAnimation() const {
740 return TotalScrollOffset();
743 void LayerImpl::OnFilterAnimated(const FilterOperations& filters) {
744 SetFilters(filters);
747 void LayerImpl::OnOpacityAnimated(float opacity) {
748 SetOpacity(opacity);
751 void LayerImpl::OnTransformAnimated(const gfx::Transform& transform) {
752 SetTransform(transform);
755 void LayerImpl::OnScrollOffsetAnimated(const gfx::Vector2dF& scroll_offset) {
756 // Only layers in the active tree should need to do anything here, since
757 // layers in the pending tree will find out about these changes as a
758 // result of the call to SetScrollDelta.
759 if (!IsActive())
760 return;
762 SetScrollDelta(scroll_offset - scroll_offset_);
764 layer_tree_impl_->DidAnimateScrollOffset();
767 void LayerImpl::OnAnimationWaitingForDeletion() {}
769 bool LayerImpl::IsActive() const {
770 return layer_tree_impl_->IsActiveTree();
773 // TODO(aelias): Convert so that bounds returns SizeF.
774 gfx::Size LayerImpl::bounds() const {
775 return gfx::ToCeiledSize(gfx::SizeF(bounds_.width() + bounds_delta_.x(),
776 bounds_.height() + bounds_delta_.y()));
779 void LayerImpl::SetBounds(const gfx::Size& bounds) {
780 if (bounds_ == bounds)
781 return;
783 bounds_ = bounds;
785 ScrollbarParametersDidChange();
786 if (masks_to_bounds())
787 NoteLayerPropertyChangedForSubtree();
788 else
789 NoteLayerPropertyChanged();
792 void LayerImpl::SetBoundsDelta(const gfx::Vector2dF& bounds_delta) {
793 if (bounds_delta_ == bounds_delta)
794 return;
796 bounds_delta_ = bounds_delta;
798 ScrollbarParametersDidChange();
799 if (masks_to_bounds())
800 NoteLayerPropertyChangedForSubtree();
801 else
802 NoteLayerPropertyChanged();
805 void LayerImpl::SetMaskLayer(scoped_ptr<LayerImpl> mask_layer) {
806 int new_layer_id = mask_layer ? mask_layer->id() : -1;
808 if (mask_layer) {
809 DCHECK_EQ(layer_tree_impl(), mask_layer->layer_tree_impl());
810 DCHECK_NE(new_layer_id, mask_layer_id_);
811 } else if (new_layer_id == mask_layer_id_) {
812 return;
815 mask_layer_ = mask_layer.Pass();
816 mask_layer_id_ = new_layer_id;
817 if (mask_layer_)
818 mask_layer_->SetParent(this);
819 NoteLayerPropertyChangedForSubtree();
822 scoped_ptr<LayerImpl> LayerImpl::TakeMaskLayer() {
823 mask_layer_id_ = -1;
824 return mask_layer_.Pass();
827 void LayerImpl::SetReplicaLayer(scoped_ptr<LayerImpl> replica_layer) {
828 int new_layer_id = replica_layer ? replica_layer->id() : -1;
830 if (replica_layer) {
831 DCHECK_EQ(layer_tree_impl(), replica_layer->layer_tree_impl());
832 DCHECK_NE(new_layer_id, replica_layer_id_);
833 } else if (new_layer_id == replica_layer_id_) {
834 return;
837 replica_layer_ = replica_layer.Pass();
838 replica_layer_id_ = new_layer_id;
839 if (replica_layer_)
840 replica_layer_->SetParent(this);
841 NoteLayerPropertyChangedForSubtree();
844 scoped_ptr<LayerImpl> LayerImpl::TakeReplicaLayer() {
845 replica_layer_id_ = -1;
846 return replica_layer_.Pass();
849 ScrollbarLayerImplBase* LayerImpl::ToScrollbarLayer() {
850 return NULL;
853 void LayerImpl::SetDrawsContent(bool draws_content) {
854 if (draws_content_ == draws_content)
855 return;
857 draws_content_ = draws_content;
858 NoteLayerPropertyChanged();
861 void LayerImpl::SetHideLayerAndSubtree(bool hide) {
862 if (hide_layer_and_subtree_ == hide)
863 return;
865 hide_layer_and_subtree_ = hide;
866 NoteLayerPropertyChangedForSubtree();
869 void LayerImpl::SetTransformOrigin(const gfx::Point3F& transform_origin) {
870 if (transform_origin_ == transform_origin)
871 return;
872 transform_origin_ = transform_origin;
873 NoteLayerPropertyChangedForSubtree();
876 void LayerImpl::SetBackgroundColor(SkColor background_color) {
877 if (background_color_ == background_color)
878 return;
880 background_color_ = background_color;
881 NoteLayerPropertyChanged();
884 SkColor LayerImpl::SafeOpaqueBackgroundColor() const {
885 SkColor color = background_color();
886 if (SkColorGetA(color) == 255 && !contents_opaque()) {
887 color = SK_ColorTRANSPARENT;
888 } else if (SkColorGetA(color) != 255 && contents_opaque()) {
889 for (const LayerImpl* layer = parent(); layer;
890 layer = layer->parent()) {
891 color = layer->background_color();
892 if (SkColorGetA(color) == 255)
893 break;
895 if (SkColorGetA(color) != 255)
896 color = layer_tree_impl()->background_color();
897 if (SkColorGetA(color) != 255)
898 color = SkColorSetA(color, 255);
900 return color;
903 void LayerImpl::SetFilters(const FilterOperations& filters) {
904 if (filters_ == filters)
905 return;
907 filters_ = filters;
908 NoteLayerPropertyChangedForSubtree();
911 bool LayerImpl::FilterIsAnimating() const {
912 return layer_animation_controller_->IsAnimatingProperty(Animation::Filter);
915 bool LayerImpl::FilterIsAnimatingOnImplOnly() const {
916 Animation* filter_animation =
917 layer_animation_controller_->GetAnimation(Animation::Filter);
918 return filter_animation && filter_animation->is_impl_only();
921 void LayerImpl::SetBackgroundFilters(
922 const FilterOperations& filters) {
923 if (background_filters_ == filters)
924 return;
926 background_filters_ = filters;
927 NoteLayerPropertyChanged();
930 void LayerImpl::SetMasksToBounds(bool masks_to_bounds) {
931 if (masks_to_bounds_ == masks_to_bounds)
932 return;
934 masks_to_bounds_ = masks_to_bounds;
935 NoteLayerPropertyChangedForSubtree();
938 void LayerImpl::SetContentsOpaque(bool opaque) {
939 if (contents_opaque_ == opaque)
940 return;
942 contents_opaque_ = opaque;
943 NoteLayerPropertyChangedForSubtree();
946 void LayerImpl::SetOpacity(float opacity) {
947 if (opacity_ == opacity)
948 return;
950 opacity_ = opacity;
951 NoteLayerPropertyChangedForSubtree();
954 bool LayerImpl::OpacityIsAnimating() const {
955 return layer_animation_controller_->IsAnimatingProperty(Animation::Opacity);
958 bool LayerImpl::OpacityIsAnimatingOnImplOnly() const {
959 Animation* opacity_animation =
960 layer_animation_controller_->GetAnimation(Animation::Opacity);
961 return opacity_animation && opacity_animation->is_impl_only();
964 void LayerImpl::SetBlendMode(SkXfermode::Mode blend_mode) {
965 if (blend_mode_ == blend_mode)
966 return;
968 blend_mode_ = blend_mode;
969 NoteLayerPropertyChangedForSubtree();
972 void LayerImpl::SetIsRootForIsolatedGroup(bool root) {
973 if (is_root_for_isolated_group_ == root)
974 return;
976 is_root_for_isolated_group_ = root;
977 SetNeedsPushProperties();
980 void LayerImpl::SetPosition(const gfx::PointF& position) {
981 if (position_ == position)
982 return;
984 position_ = position;
985 NoteLayerPropertyChangedForSubtree();
988 void LayerImpl::SetShouldFlattenTransform(bool flatten) {
989 if (should_flatten_transform_ == flatten)
990 return;
992 should_flatten_transform_ = flatten;
993 NoteLayerPropertyChangedForSubtree();
996 void LayerImpl::Set3dSortingContextId(int id) {
997 if (id == sorting_context_id_)
998 return;
999 sorting_context_id_ = id;
1000 NoteLayerPropertyChangedForSubtree();
1003 void LayerImpl::SetTransform(const gfx::Transform& transform) {
1004 if (transform_ == transform)
1005 return;
1007 transform_ = transform;
1008 transform_is_invertible_ = transform_.IsInvertible();
1009 NoteLayerPropertyChangedForSubtree();
1012 void LayerImpl::SetTransformAndInvertibility(const gfx::Transform& transform,
1013 bool transform_is_invertible) {
1014 if (transform_ == transform) {
1015 DCHECK(transform_is_invertible_ == transform_is_invertible)
1016 << "Can't change invertibility if transform is unchanged";
1017 return;
1019 transform_ = transform;
1020 transform_is_invertible_ = transform_is_invertible;
1021 NoteLayerPropertyChangedForSubtree();
1024 bool LayerImpl::TransformIsAnimating() const {
1025 return layer_animation_controller_->IsAnimatingProperty(Animation::Transform);
1028 bool LayerImpl::TransformIsAnimatingOnImplOnly() const {
1029 Animation* transform_animation =
1030 layer_animation_controller_->GetAnimation(Animation::Transform);
1031 return transform_animation && transform_animation->is_impl_only();
1034 void LayerImpl::SetUpdateRect(const gfx::RectF& update_rect) {
1035 update_rect_ = update_rect;
1036 SetNeedsPushProperties();
1039 void LayerImpl::AddDamageRect(const gfx::RectF& damage_rect) {
1040 damage_rect_ = gfx::UnionRects(damage_rect_, damage_rect);
1043 void LayerImpl::SetContentBounds(const gfx::Size& content_bounds) {
1044 if (this->content_bounds() == content_bounds)
1045 return;
1047 draw_properties_.content_bounds = content_bounds;
1048 NoteLayerPropertyChanged();
1051 void LayerImpl::SetContentsScale(float contents_scale_x,
1052 float contents_scale_y) {
1053 if (this->contents_scale_x() == contents_scale_x &&
1054 this->contents_scale_y() == contents_scale_y)
1055 return;
1057 draw_properties_.contents_scale_x = contents_scale_x;
1058 draw_properties_.contents_scale_y = contents_scale_y;
1059 NoteLayerPropertyChanged();
1062 void LayerImpl::SetScrollOffsetDelegate(
1063 ScrollOffsetDelegate* scroll_offset_delegate) {
1064 // Having both a scroll parent and a scroll offset delegate is unsupported.
1065 DCHECK(!scroll_parent_);
1066 if (!scroll_offset_delegate && scroll_offset_delegate_) {
1067 scroll_delta_ =
1068 scroll_offset_delegate_->GetTotalScrollOffset() - scroll_offset_;
1070 gfx::Vector2dF total_offset = TotalScrollOffset();
1071 scroll_offset_delegate_ = scroll_offset_delegate;
1072 if (scroll_offset_delegate_)
1073 scroll_offset_delegate_->SetTotalScrollOffset(total_offset);
1076 bool LayerImpl::IsExternalFlingActive() const {
1077 return scroll_offset_delegate_ &&
1078 scroll_offset_delegate_->IsExternalFlingActive();
1081 void LayerImpl::SetScrollOffset(const gfx::Vector2d& scroll_offset) {
1082 SetScrollOffsetAndDelta(scroll_offset, ScrollDelta());
1085 void LayerImpl::SetScrollOffsetAndDelta(const gfx::Vector2d& scroll_offset,
1086 const gfx::Vector2dF& scroll_delta) {
1087 bool changed = false;
1089 last_scroll_offset_ = scroll_offset;
1091 if (scroll_offset_ != scroll_offset) {
1092 changed = true;
1093 scroll_offset_ = scroll_offset;
1095 if (scroll_offset_delegate_)
1096 scroll_offset_delegate_->SetTotalScrollOffset(TotalScrollOffset());
1099 if (ScrollDelta() != scroll_delta) {
1100 changed = true;
1101 if (layer_tree_impl()->IsActiveTree()) {
1102 LayerImpl* pending_twin =
1103 layer_tree_impl()->FindPendingTreeLayerById(id());
1104 if (pending_twin) {
1105 // The pending twin can't mirror the scroll delta of the active
1106 // layer. Although the delta - sent scroll delta difference is
1107 // identical for both twins, the sent scroll delta for the pending
1108 // layer is zero, as anything that has been sent has been baked
1109 // into the layer's position/scroll offset as a part of commit.
1110 DCHECK(pending_twin->sent_scroll_delta().IsZero());
1111 pending_twin->SetScrollDelta(scroll_delta - sent_scroll_delta());
1115 if (scroll_offset_delegate_) {
1116 scroll_offset_delegate_->SetTotalScrollOffset(scroll_offset_ +
1117 scroll_delta);
1118 } else {
1119 scroll_delta_ = scroll_delta;
1123 if (changed) {
1124 NoteLayerPropertyChangedForSubtree();
1125 ScrollbarParametersDidChange();
1129 gfx::Vector2dF LayerImpl::ScrollDelta() const {
1130 if (scroll_offset_delegate_)
1131 return scroll_offset_delegate_->GetTotalScrollOffset() - scroll_offset_;
1132 return scroll_delta_;
1135 void LayerImpl::SetScrollDelta(const gfx::Vector2dF& scroll_delta) {
1136 SetScrollOffsetAndDelta(scroll_offset_, scroll_delta);
1139 gfx::Vector2dF LayerImpl::TotalScrollOffset() const {
1140 return scroll_offset_ + ScrollDelta();
1143 void LayerImpl::SetDoubleSided(bool double_sided) {
1144 if (double_sided_ == double_sided)
1145 return;
1147 double_sided_ = double_sided;
1148 NoteLayerPropertyChangedForSubtree();
1151 SimpleEnclosedRegion LayerImpl::VisibleContentOpaqueRegion() const {
1152 if (contents_opaque())
1153 return SimpleEnclosedRegion(visible_content_rect());
1154 return SimpleEnclosedRegion();
1157 void LayerImpl::DidBeginTracing() {}
1159 void LayerImpl::ReleaseResources() {}
1161 gfx::Vector2d LayerImpl::MaxScrollOffset() const {
1162 if (!scroll_clip_layer_ || bounds().IsEmpty())
1163 return gfx::Vector2d();
1165 LayerImpl const* page_scale_layer = layer_tree_impl()->page_scale_layer();
1166 DCHECK(this != page_scale_layer);
1167 DCHECK(this != layer_tree_impl()->InnerViewportScrollLayer() ||
1168 IsContainerForFixedPositionLayers());
1170 gfx::SizeF scaled_scroll_bounds(bounds());
1172 float scale_factor = 1.f;
1173 for (LayerImpl const* current_layer = this;
1174 current_layer != scroll_clip_layer_;
1175 current_layer = current_layer->parent()) {
1176 DCHECK(current_layer);
1177 float current_layer_scale = 1.f;
1179 const gfx::Transform& layer_transform = current_layer->transform();
1180 if (current_layer == page_scale_layer) {
1181 DCHECK(layer_transform.IsIdentity());
1182 current_layer_scale = layer_tree_impl()->total_page_scale_factor();
1183 } else {
1184 // TODO(wjmaclean) Should we allow for translation too?
1185 DCHECK(layer_transform.IsScale2d());
1186 gfx::Vector2dF layer_scale = layer_transform.Scale2d();
1187 // TODO(wjmaclean) Allow for non-isotropic scales.
1188 DCHECK(layer_scale.x() == layer_scale.y());
1189 current_layer_scale = layer_scale.x();
1192 scale_factor *= current_layer_scale;
1194 // TODO(wjmaclean) Once we move to a model where the two-viewport model is
1195 // turned on in all builds, remove the next two lines. For now however, the
1196 // page scale layer may coincide with the clip layer, and so this is
1197 // necessary.
1198 if (page_scale_layer == scroll_clip_layer_)
1199 scale_factor *= layer_tree_impl()->total_page_scale_factor();
1201 scaled_scroll_bounds.SetSize(scale_factor * scaled_scroll_bounds.width(),
1202 scale_factor * scaled_scroll_bounds.height());
1203 scaled_scroll_bounds = gfx::ToFlooredSize(scaled_scroll_bounds);
1205 gfx::Vector2dF max_offset(
1206 scaled_scroll_bounds.width() - scroll_clip_layer_->bounds().width(),
1207 scaled_scroll_bounds.height() - scroll_clip_layer_->bounds().height());
1208 // We need the final scroll offset to be in CSS coords.
1209 max_offset.Scale(1 / scale_factor);
1210 max_offset.SetToMax(gfx::Vector2dF());
1211 return gfx::ToFlooredVector2d(max_offset);
1214 gfx::Vector2dF LayerImpl::ClampScrollToMaxScrollOffset() {
1215 gfx::Vector2dF max_offset = MaxScrollOffset();
1216 gfx::Vector2dF old_offset = TotalScrollOffset();
1217 gfx::Vector2dF clamped_offset = old_offset;
1219 clamped_offset.SetToMin(max_offset);
1220 clamped_offset.SetToMax(gfx::Vector2d());
1221 gfx::Vector2dF delta = clamped_offset - old_offset;
1222 if (!delta.IsZero())
1223 ScrollBy(delta);
1225 return delta;
1228 void LayerImpl::SetScrollbarPosition(ScrollbarLayerImplBase* scrollbar_layer,
1229 LayerImpl* scrollbar_clip_layer) const {
1230 DCHECK(scrollbar_layer);
1231 LayerImpl* page_scale_layer = layer_tree_impl()->page_scale_layer();
1233 DCHECK(this != page_scale_layer);
1234 DCHECK(scrollbar_clip_layer);
1235 DCHECK(this != layer_tree_impl()->InnerViewportScrollLayer() ||
1236 IsContainerForFixedPositionLayers());
1237 gfx::RectF clip_rect(gfx::PointF(), scrollbar_clip_layer->bounds());
1239 // See comment in MaxScrollOffset() regarding the use of the content layer
1240 // bounds here.
1241 gfx::RectF scroll_rect(gfx::PointF(), bounds());
1243 if (scroll_rect.size().IsEmpty())
1244 return;
1246 // TODO(wjmaclean) This computation is nearly identical to the one in
1247 // MaxScrollOffset. Find some way to combine these.
1248 gfx::Vector2dF current_offset;
1249 for (LayerImpl const* current_layer = this;
1250 current_layer != scrollbar_clip_layer;
1251 current_layer = current_layer->parent()) {
1252 DCHECK(current_layer);
1253 const gfx::Transform& layer_transform = current_layer->transform();
1254 if (current_layer == page_scale_layer) {
1255 DCHECK(layer_transform.IsIdentity());
1256 float scale_factor = layer_tree_impl()->total_page_scale_factor();
1257 current_offset.Scale(scale_factor);
1258 scroll_rect.Scale(scale_factor);
1259 } else {
1260 DCHECK(layer_transform.IsScale2d());
1261 gfx::Vector2dF layer_scale = layer_transform.Scale2d();
1262 DCHECK(layer_scale.x() == layer_scale.y());
1263 gfx::Vector2dF new_offset =
1264 current_layer->scroll_offset() + current_layer->ScrollDelta();
1265 new_offset.Scale(layer_scale.x(), layer_scale.y());
1266 current_offset += new_offset;
1269 // TODO(wjmaclean) Once we move to a model where the two-viewport model is
1270 // turned on in all builds, remove the next two lines. For now however, the
1271 // page scale layer may coincide with the clip layer, and so this is
1272 // necessary.
1273 if (page_scale_layer == scrollbar_clip_layer) {
1274 scroll_rect.Scale(layer_tree_impl()->total_page_scale_factor());
1275 current_offset.Scale(layer_tree_impl()->total_page_scale_factor());
1278 bool scrollbar_needs_animation = false;
1279 scrollbar_needs_animation |= scrollbar_layer->SetVerticalAdjust(
1280 scrollbar_clip_layer->bounds_delta().y());
1281 if (scrollbar_layer->orientation() == HORIZONTAL) {
1282 float visible_ratio = clip_rect.width() / scroll_rect.width();
1283 scrollbar_needs_animation |=
1284 scrollbar_layer->SetCurrentPos(current_offset.x());
1285 scrollbar_needs_animation |=
1286 scrollbar_layer->SetMaximum(scroll_rect.width() - clip_rect.width());
1287 scrollbar_needs_animation |=
1288 scrollbar_layer->SetVisibleToTotalLengthRatio(visible_ratio);
1289 } else {
1290 float visible_ratio = clip_rect.height() / scroll_rect.height();
1291 scrollbar_needs_animation |=
1292 scrollbar_layer->SetCurrentPos(current_offset.y());
1293 scrollbar_needs_animation |=
1294 scrollbar_layer->SetMaximum(scroll_rect.height() - clip_rect.height());
1295 scrollbar_needs_animation |=
1296 scrollbar_layer->SetVisibleToTotalLengthRatio(visible_ratio);
1298 if (scrollbar_needs_animation) {
1299 layer_tree_impl()->set_needs_update_draw_properties();
1300 // TODO(wjmaclean) The scrollbar animator for the pinch-zoom scrollbars
1301 // should activate for every scroll on the main frame, not just the
1302 // scrolls that move the pinch virtual viewport (i.e. trigger from
1303 // either inner or outer viewport).
1304 if (scrollbar_animation_controller_) {
1305 // When both non-overlay and overlay scrollbars are both present, don't
1306 // animate the overlay scrollbars when page scale factor is at the min.
1307 // Non-overlay scrollbars also shouldn't trigger animations.
1308 bool is_animatable_scrollbar =
1309 scrollbar_layer->is_overlay_scrollbar() &&
1310 ((layer_tree_impl()->total_page_scale_factor() >
1311 layer_tree_impl()->min_page_scale_factor()) ||
1312 !layer_tree_impl()->settings().use_pinch_zoom_scrollbars);
1313 if (is_animatable_scrollbar)
1314 scrollbar_animation_controller_->DidScrollUpdate();
1319 void LayerImpl::DidBecomeActive() {
1320 if (layer_tree_impl_->settings().scrollbar_animator ==
1321 LayerTreeSettings::NoAnimator) {
1322 return;
1325 bool need_scrollbar_animation_controller = scrollable() && scrollbars_;
1326 if (!need_scrollbar_animation_controller) {
1327 scrollbar_animation_controller_.reset();
1328 return;
1331 if (scrollbar_animation_controller_)
1332 return;
1334 scrollbar_animation_controller_ =
1335 layer_tree_impl_->CreateScrollbarAnimationController(this);
1338 void LayerImpl::ClearScrollbars() {
1339 if (!scrollbars_)
1340 return;
1342 scrollbars_.reset(NULL);
1345 void LayerImpl::AddScrollbar(ScrollbarLayerImplBase* layer) {
1346 DCHECK(layer);
1347 DCHECK(!scrollbars_ || scrollbars_->find(layer) == scrollbars_->end());
1348 if (!scrollbars_)
1349 scrollbars_.reset(new ScrollbarSet());
1351 scrollbars_->insert(layer);
1354 void LayerImpl::RemoveScrollbar(ScrollbarLayerImplBase* layer) {
1355 DCHECK(scrollbars_);
1356 DCHECK(layer);
1357 DCHECK(scrollbars_->find(layer) != scrollbars_->end());
1359 scrollbars_->erase(layer);
1360 if (scrollbars_->empty())
1361 scrollbars_.reset();
1364 bool LayerImpl::HasScrollbar(ScrollbarOrientation orientation) const {
1365 if (!scrollbars_)
1366 return false;
1368 for (ScrollbarSet::iterator it = scrollbars_->begin();
1369 it != scrollbars_->end();
1370 ++it)
1371 if ((*it)->orientation() == orientation)
1372 return true;
1374 return false;
1377 void LayerImpl::ScrollbarParametersDidChange() {
1378 if (!scrollbars_)
1379 return;
1381 for (ScrollbarSet::iterator it = scrollbars_->begin();
1382 it != scrollbars_->end();
1383 ++it)
1384 (*it)->ScrollbarParametersDidChange();
1387 void LayerImpl::SetNeedsPushProperties() {
1388 if (needs_push_properties_)
1389 return;
1390 if (!parent_should_know_need_push_properties() && parent_)
1391 parent_->AddDependentNeedsPushProperties();
1392 needs_push_properties_ = true;
1395 void LayerImpl::AddDependentNeedsPushProperties() {
1396 DCHECK_GE(num_dependents_need_push_properties_, 0);
1398 if (!parent_should_know_need_push_properties() && parent_)
1399 parent_->AddDependentNeedsPushProperties();
1401 num_dependents_need_push_properties_++;
1404 void LayerImpl::RemoveDependentNeedsPushProperties() {
1405 num_dependents_need_push_properties_--;
1406 DCHECK_GE(num_dependents_need_push_properties_, 0);
1408 if (!parent_should_know_need_push_properties() && parent_)
1409 parent_->RemoveDependentNeedsPushProperties();
1412 void LayerImpl::GetAllTilesForTracing(std::set<const Tile*>* tiles) const {
1415 void LayerImpl::AsValueInto(base::debug::TracedValue* state) const {
1416 TracedValue::MakeDictIntoImplicitSnapshotWithCategory(
1417 TRACE_DISABLED_BY_DEFAULT("cc.debug"),
1418 state,
1419 "cc::LayerImpl",
1420 LayerTypeAsString(),
1421 this);
1422 state->SetInteger("layer_id", id());
1423 state->BeginDictionary("bounds");
1424 MathUtil::AddToTracedValue(bounds_, state);
1425 state->EndDictionary();
1427 state->SetDouble("opacity", opacity());
1429 state->BeginArray("position");
1430 MathUtil::AddToTracedValue(position_, state);
1431 state->EndArray();
1433 state->SetInteger("draws_content", DrawsContent());
1434 state->SetInteger("gpu_memory_usage", GPUMemoryUsageInBytes());
1436 state->BeginArray("scroll_offset");
1437 MathUtil::AddToTracedValue(scroll_offset_, state);
1438 state->EndArray();
1440 state->BeginArray("transform_origin");
1441 MathUtil::AddToTracedValue(transform_origin_, state);
1442 state->EndArray();
1444 bool clipped;
1445 gfx::QuadF layer_quad = MathUtil::MapQuad(
1446 screen_space_transform(),
1447 gfx::QuadF(gfx::Rect(content_bounds())),
1448 &clipped);
1449 state->BeginArray("layer_quad");
1450 MathUtil::AddToTracedValue(layer_quad, state);
1451 state->EndArray();
1452 if (!touch_event_handler_region_.IsEmpty()) {
1453 state->BeginArray("touch_event_handler_region");
1454 touch_event_handler_region_.AsValueInto(state);
1455 state->EndArray();
1457 if (have_wheel_event_handlers_) {
1458 gfx::Rect wheel_rect(content_bounds());
1459 Region wheel_region(wheel_rect);
1460 state->BeginArray("wheel_event_handler_region");
1461 wheel_region.AsValueInto(state);
1462 state->EndArray();
1464 if (have_scroll_event_handlers_) {
1465 gfx::Rect scroll_rect(content_bounds());
1466 Region scroll_region(scroll_rect);
1467 state->BeginArray("scroll_event_handler_region");
1468 scroll_region.AsValueInto(state);
1469 state->EndArray();
1471 if (!non_fast_scrollable_region_.IsEmpty()) {
1472 state->BeginArray("non_fast_scrollable_region");
1473 non_fast_scrollable_region_.AsValueInto(state);
1474 state->EndArray();
1477 state->BeginArray("children");
1478 for (size_t i = 0; i < children_.size(); ++i) {
1479 state->BeginDictionary();
1480 children_[i]->AsValueInto(state);
1481 state->EndDictionary();
1483 state->EndArray();
1484 if (mask_layer_) {
1485 state->BeginDictionary("mask_layer");
1486 mask_layer_->AsValueInto(state);
1487 state->EndDictionary();
1489 if (replica_layer_) {
1490 state->BeginDictionary("replica_layer");
1491 replica_layer_->AsValueInto(state);
1492 state->EndDictionary();
1495 if (scroll_parent_)
1496 state->SetInteger("scroll_parent", scroll_parent_->id());
1498 if (clip_parent_)
1499 state->SetInteger("clip_parent", clip_parent_->id());
1501 state->SetBoolean("can_use_lcd_text", can_use_lcd_text());
1502 state->SetBoolean("contents_opaque", contents_opaque());
1504 state->SetBoolean(
1505 "has_animation_bounds",
1506 layer_animation_controller()->HasAnimationThatInflatesBounds());
1508 gfx::BoxF box;
1509 if (LayerUtils::GetAnimationBounds(*this, &box)) {
1510 state->BeginArray("animation_bounds");
1511 MathUtil::AddToTracedValue(box, state);
1512 state->EndArray();
1515 if (debug_info_.get()) {
1516 std::string str;
1517 debug_info_->AppendAsTraceFormat(&str);
1518 base::JSONReader json_reader;
1519 scoped_ptr<base::Value> debug_info_value(json_reader.ReadToValue(str));
1521 if (debug_info_value->IsType(base::Value::TYPE_DICTIONARY)) {
1522 base::DictionaryValue* dictionary_value = NULL;
1523 bool converted_to_dictionary =
1524 debug_info_value->GetAsDictionary(&dictionary_value);
1525 DCHECK(converted_to_dictionary);
1526 for (base::DictionaryValue::Iterator it(*dictionary_value); !it.IsAtEnd();
1527 it.Advance()) {
1528 state->SetValue(it.key().data(), it.value().DeepCopy());
1530 } else {
1531 NOTREACHED();
1536 bool LayerImpl::IsDrawnRenderSurfaceLayerListMember() const {
1537 return draw_properties_.last_drawn_render_surface_layer_list_id ==
1538 layer_tree_impl_->current_render_surface_list_id();
1541 size_t LayerImpl::GPUMemoryUsageInBytes() const { return 0; }
1543 void LayerImpl::RunMicroBenchmark(MicroBenchmarkImpl* benchmark) {
1544 benchmark->RunOnLayer(this);
1547 int LayerImpl::NumDescendantsThatDrawContent() const {
1548 return num_descendants_that_draw_content_;
1551 void LayerImpl::NotifyAnimationFinished(
1552 base::TimeTicks monotonic_time,
1553 Animation::TargetProperty target_property) {
1554 if (target_property == Animation::ScrollOffset)
1555 layer_tree_impl_->InputScrollAnimationFinished();
1558 } // namespace cc