Fix TextureLayer cleanup
[chromium-blink-merge.git] / cc / layers / layer.cc
blob9cb4708ecfd3a5d4225e26e0296f50ffd8a11745
1 // Copyright 2010 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.h"
7 #include <algorithm>
9 #include "base/location.h"
10 #include "base/metrics/histogram.h"
11 #include "base/single_thread_task_runner.h"
12 #include "cc/animation/animation.h"
13 #include "cc/animation/animation_events.h"
14 #include "cc/animation/layer_animation_controller.h"
15 #include "cc/layers/layer_impl.h"
16 #include "cc/output/copy_output_request.h"
17 #include "cc/output/copy_output_result.h"
18 #include "cc/trees/layer_tree_host.h"
19 #include "cc/trees/layer_tree_impl.h"
20 #include "third_party/skia/include/core/SkImageFilter.h"
21 #include "ui/gfx/rect_conversions.h"
23 namespace cc {
25 static int s_next_layer_id = 1;
27 scoped_refptr<Layer> Layer::Create() {
28 return make_scoped_refptr(new Layer());
31 Layer::Layer()
32 : needs_display_(false),
33 stacking_order_changed_(false),
34 layer_id_(s_next_layer_id++),
35 ignore_set_needs_commit_(false),
36 parent_(NULL),
37 layer_tree_host_(NULL),
38 scrollable_(false),
39 should_scroll_on_main_thread_(false),
40 have_wheel_event_handlers_(false),
41 anchor_point_(0.5f, 0.5f),
42 background_color_(0),
43 compositing_reasons_(kCompositingReasonUnknown),
44 opacity_(1.f),
45 anchor_point_z_(0.f),
46 is_container_for_fixed_position_layers_(false),
47 is_drawable_(false),
48 hide_layer_and_subtree_(false),
49 masks_to_bounds_(false),
50 contents_opaque_(false),
51 double_sided_(true),
52 preserves_3d_(false),
53 use_parent_backface_visibility_(false),
54 draw_checkerboard_for_missing_tiles_(false),
55 force_render_surface_(false),
56 replica_layer_(NULL),
57 raster_scale_(0.f) {
58 if (layer_id_ < 0) {
59 s_next_layer_id = 1;
60 layer_id_ = s_next_layer_id++;
63 layer_animation_controller_ = LayerAnimationController::Create(layer_id_);
64 layer_animation_controller_->AddValueObserver(this);
67 Layer::~Layer() {
68 // Our parent should be holding a reference to us so there should be no
69 // way for us to be destroyed while we still have a parent.
70 DCHECK(!parent());
71 // Similarly we shouldn't have a layer tree host since it also keeps a
72 // reference to us.
73 DCHECK(!layer_tree_host());
75 layer_animation_controller_->RemoveValueObserver(this);
77 // Remove the parent reference from all children and dependents.
78 RemoveAllChildren();
79 if (mask_layer_.get()) {
80 DCHECK_EQ(this, mask_layer_->parent());
81 mask_layer_->RemoveFromParent();
83 if (replica_layer_.get()) {
84 DCHECK_EQ(this, replica_layer_->parent());
85 replica_layer_->RemoveFromParent();
89 void Layer::SetLayerTreeHost(LayerTreeHost* host) {
90 if (layer_tree_host_ == host)
91 return;
93 layer_tree_host_ = host;
95 for (size_t i = 0; i < children_.size(); ++i)
96 children_[i]->SetLayerTreeHost(host);
98 if (mask_layer_.get())
99 mask_layer_->SetLayerTreeHost(host);
100 if (replica_layer_.get())
101 replica_layer_->SetLayerTreeHost(host);
103 if (host) {
104 layer_animation_controller_->SetAnimationRegistrar(
105 host->animation_registrar());
107 if (host->settings().layer_transforms_should_scale_layer_contents)
108 reset_raster_scale_to_unknown();
111 if (host && layer_animation_controller_->has_any_animation())
112 host->SetNeedsCommit();
113 if (host &&
114 (!filters_.IsEmpty() || !background_filters_.IsEmpty() || filter_))
115 layer_tree_host_->set_needs_filter_context();
118 void Layer::SetNeedsCommit() {
119 if (ignore_set_needs_commit_)
120 return;
121 if (layer_tree_host_)
122 layer_tree_host_->SetNeedsCommit();
125 void Layer::SetNeedsFullTreeSync() {
126 if (layer_tree_host_)
127 layer_tree_host_->SetNeedsFullTreeSync();
130 bool Layer::IsPropertyChangeAllowed() const {
131 if (!layer_tree_host_)
132 return true;
134 if (!layer_tree_host_->settings().strict_layer_property_change_checking)
135 return true;
137 return !layer_tree_host_->in_paint_layer_contents();
140 gfx::Rect Layer::LayerRectToContentRect(const gfx::RectF& layer_rect) const {
141 gfx::RectF content_rect =
142 gfx::ScaleRect(layer_rect, contents_scale_x(), contents_scale_y());
143 // Intersect with content rect to avoid the extra pixel because for some
144 // values x and y, ceil((x / y) * y) may be x + 1.
145 content_rect.Intersect(gfx::Rect(content_bounds()));
146 return gfx::ToEnclosingRect(content_rect);
149 bool Layer::BlocksPendingCommit() const {
150 return false;
153 bool Layer::CanClipSelf() const {
154 return false;
157 bool Layer::BlocksPendingCommitRecursive() const {
158 if (BlocksPendingCommit())
159 return true;
160 if (mask_layer() && mask_layer()->BlocksPendingCommitRecursive())
161 return true;
162 if (replica_layer() && replica_layer()->BlocksPendingCommitRecursive())
163 return true;
164 for (size_t i = 0; i < children_.size(); ++i) {
165 if (children_[i]->BlocksPendingCommitRecursive())
166 return true;
168 return false;
171 void Layer::SetParent(Layer* layer) {
172 DCHECK(!layer || !layer->HasAncestor(this));
173 parent_ = layer;
174 SetLayerTreeHost(parent_ ? parent_->layer_tree_host() : NULL);
176 if (!layer_tree_host_)
177 return;
178 const LayerTreeSettings& settings = layer_tree_host_->settings();
179 if (!settings.layer_transforms_should_scale_layer_contents)
180 return;
182 reset_raster_scale_to_unknown();
183 if (mask_layer_.get())
184 mask_layer_->reset_raster_scale_to_unknown();
185 if (replica_layer_.get() && replica_layer_->mask_layer_.get())
186 replica_layer_->mask_layer_->reset_raster_scale_to_unknown();
189 bool Layer::HasAncestor(Layer* ancestor) const {
190 for (const Layer* layer = parent(); layer; layer = layer->parent()) {
191 if (layer == ancestor)
192 return true;
194 return false;
197 void Layer::AddChild(scoped_refptr<Layer> child) {
198 InsertChild(child, children_.size());
201 void Layer::InsertChild(scoped_refptr<Layer> child, size_t index) {
202 DCHECK(IsPropertyChangeAllowed());
203 child->RemoveFromParent();
204 child->SetParent(this);
205 child->stacking_order_changed_ = true;
207 index = std::min(index, children_.size());
208 children_.insert(children_.begin() + index, child);
209 SetNeedsFullTreeSync();
212 void Layer::RemoveFromParent() {
213 DCHECK(IsPropertyChangeAllowed());
214 if (parent_)
215 parent_->RemoveChildOrDependent(this);
218 void Layer::RemoveChildOrDependent(Layer* child) {
219 if (mask_layer_.get() == child) {
220 mask_layer_->SetParent(NULL);
221 mask_layer_ = NULL;
222 SetNeedsFullTreeSync();
223 return;
225 if (replica_layer_.get() == child) {
226 replica_layer_->SetParent(NULL);
227 replica_layer_ = NULL;
228 SetNeedsFullTreeSync();
229 return;
232 for (LayerList::iterator iter = children_.begin();
233 iter != children_.end();
234 ++iter) {
235 if (iter->get() != child)
236 continue;
238 child->SetParent(NULL);
239 children_.erase(iter);
240 SetNeedsFullTreeSync();
241 return;
245 void Layer::ReplaceChild(Layer* reference, scoped_refptr<Layer> new_layer) {
246 DCHECK(reference);
247 DCHECK_EQ(reference->parent(), this);
248 DCHECK(IsPropertyChangeAllowed());
250 if (reference == new_layer.get())
251 return;
253 int reference_index = IndexOfChild(reference);
254 if (reference_index == -1) {
255 NOTREACHED();
256 return;
259 reference->RemoveFromParent();
261 if (new_layer.get()) {
262 new_layer->RemoveFromParent();
263 InsertChild(new_layer, reference_index);
267 int Layer::IndexOfChild(const Layer* reference) {
268 for (size_t i = 0; i < children_.size(); ++i) {
269 if (children_[i].get() == reference)
270 return i;
272 return -1;
275 void Layer::SetBounds(gfx::Size size) {
276 DCHECK(IsPropertyChangeAllowed());
277 if (bounds() == size)
278 return;
280 bool first_resize = bounds().IsEmpty() && !size.IsEmpty();
282 bounds_ = size;
284 if (first_resize)
285 SetNeedsDisplay();
286 else
287 SetNeedsCommit();
290 Layer* Layer::RootLayer() {
291 Layer* layer = this;
292 while (layer->parent())
293 layer = layer->parent();
294 return layer;
297 void Layer::RemoveAllChildren() {
298 DCHECK(IsPropertyChangeAllowed());
299 while (children_.size()) {
300 Layer* layer = children_[0].get();
301 DCHECK_EQ(this, layer->parent());
302 layer->RemoveFromParent();
306 void Layer::SetChildren(const LayerList& children) {
307 DCHECK(IsPropertyChangeAllowed());
308 if (children == children_)
309 return;
311 RemoveAllChildren();
312 for (size_t i = 0; i < children.size(); ++i)
313 AddChild(children[i]);
316 void Layer::RequestCopyOfOutput(
317 scoped_ptr<CopyOutputRequest> request) {
318 DCHECK(IsPropertyChangeAllowed());
319 if (request->IsEmpty())
320 return;
321 copy_requests_.push_back(request.Pass());
322 SetNeedsCommit();
325 void Layer::SetAnchorPoint(gfx::PointF anchor_point) {
326 DCHECK(IsPropertyChangeAllowed());
327 if (anchor_point_ == anchor_point)
328 return;
329 anchor_point_ = anchor_point;
330 SetNeedsCommit();
333 void Layer::SetAnchorPointZ(float anchor_point_z) {
334 DCHECK(IsPropertyChangeAllowed());
335 if (anchor_point_z_ == anchor_point_z)
336 return;
337 anchor_point_z_ = anchor_point_z;
338 SetNeedsCommit();
341 void Layer::SetBackgroundColor(SkColor background_color) {
342 DCHECK(IsPropertyChangeAllowed());
343 if (background_color_ == background_color)
344 return;
345 background_color_ = background_color;
346 SetNeedsCommit();
349 SkColor Layer::SafeOpaqueBackgroundColor() const {
350 SkColor color = background_color();
351 if (SkColorGetA(color) == 255 && !contents_opaque()) {
352 color = SK_ColorTRANSPARENT;
353 } else if (SkColorGetA(color) != 255 && contents_opaque()) {
354 for (const Layer* layer = parent(); layer;
355 layer = layer->parent()) {
356 color = layer->background_color();
357 if (SkColorGetA(color) == 255)
358 break;
360 if (SkColorGetA(color) != 255)
361 color = layer_tree_host_->background_color();
362 if (SkColorGetA(color) != 255)
363 color = SkColorSetA(color, 255);
365 return color;
368 void Layer::CalculateContentsScale(
369 float ideal_contents_scale,
370 float device_scale_factor,
371 float page_scale_factor,
372 bool animating_transform_to_screen,
373 float* contents_scale_x,
374 float* contents_scale_y,
375 gfx::Size* content_bounds) {
376 *contents_scale_x = 1;
377 *contents_scale_y = 1;
378 *content_bounds = bounds();
381 void Layer::SetMasksToBounds(bool masks_to_bounds) {
382 DCHECK(IsPropertyChangeAllowed());
383 if (masks_to_bounds_ == masks_to_bounds)
384 return;
385 masks_to_bounds_ = masks_to_bounds;
386 SetNeedsCommit();
389 void Layer::SetMaskLayer(Layer* mask_layer) {
390 DCHECK(IsPropertyChangeAllowed());
391 if (mask_layer_.get() == mask_layer)
392 return;
393 if (mask_layer_.get()) {
394 DCHECK_EQ(this, mask_layer_->parent());
395 mask_layer_->RemoveFromParent();
397 mask_layer_ = mask_layer;
398 if (mask_layer_.get()) {
399 DCHECK(!mask_layer_->parent());
400 mask_layer_->RemoveFromParent();
401 mask_layer_->SetParent(this);
402 mask_layer_->SetIsMask(true);
404 SetNeedsFullTreeSync();
407 void Layer::SetReplicaLayer(Layer* layer) {
408 DCHECK(IsPropertyChangeAllowed());
409 if (replica_layer_.get() == layer)
410 return;
411 if (replica_layer_.get()) {
412 DCHECK_EQ(this, replica_layer_->parent());
413 replica_layer_->RemoveFromParent();
415 replica_layer_ = layer;
416 if (replica_layer_.get()) {
417 DCHECK(!replica_layer_->parent());
418 replica_layer_->RemoveFromParent();
419 replica_layer_->SetParent(this);
421 SetNeedsFullTreeSync();
424 void Layer::SetFilters(const FilterOperations& filters) {
425 DCHECK(IsPropertyChangeAllowed());
426 if (filters_ == filters)
427 return;
428 DCHECK(!filter_);
429 filters_ = filters;
430 SetNeedsCommit();
431 if (!filters.IsEmpty() && layer_tree_host_)
432 layer_tree_host_->set_needs_filter_context();
435 void Layer::SetFilter(const skia::RefPtr<SkImageFilter>& filter) {
436 DCHECK(IsPropertyChangeAllowed());
437 if (filter_.get() == filter.get())
438 return;
439 DCHECK(filters_.IsEmpty());
440 filter_ = filter;
441 SetNeedsCommit();
442 if (filter && layer_tree_host_)
443 layer_tree_host_->set_needs_filter_context();
446 void Layer::SetBackgroundFilters(const FilterOperations& filters) {
447 DCHECK(IsPropertyChangeAllowed());
448 if (background_filters_ == filters)
449 return;
450 background_filters_ = filters;
451 SetNeedsCommit();
452 if (!filters.IsEmpty() && layer_tree_host_)
453 layer_tree_host_->set_needs_filter_context();
456 void Layer::SetOpacity(float opacity) {
457 DCHECK(IsPropertyChangeAllowed());
458 if (opacity_ == opacity)
459 return;
460 opacity_ = opacity;
461 SetNeedsCommit();
464 bool Layer::OpacityIsAnimating() const {
465 return layer_animation_controller_->IsAnimatingProperty(Animation::Opacity);
468 bool Layer::OpacityCanAnimateOnImplThread() const {
469 return false;
472 void Layer::SetContentsOpaque(bool opaque) {
473 DCHECK(IsPropertyChangeAllowed());
474 if (contents_opaque_ == opaque)
475 return;
476 contents_opaque_ = opaque;
477 SetNeedsCommit();
480 void Layer::SetPosition(gfx::PointF position) {
481 DCHECK(IsPropertyChangeAllowed());
482 if (position_ == position)
483 return;
484 position_ = position;
485 SetNeedsCommit();
488 bool Layer::IsContainerForFixedPositionLayers() const {
489 if (!transform_.IsIdentityOrTranslation())
490 return true;
491 if (parent_ && !parent_->sublayer_transform_.IsIdentityOrTranslation())
492 return true;
493 return is_container_for_fixed_position_layers_;
496 void Layer::SetSublayerTransform(const gfx::Transform& sublayer_transform) {
497 DCHECK(IsPropertyChangeAllowed());
498 if (sublayer_transform_ == sublayer_transform)
499 return;
500 sublayer_transform_ = sublayer_transform;
501 SetNeedsCommit();
504 void Layer::SetTransform(const gfx::Transform& transform) {
505 DCHECK(IsPropertyChangeAllowed());
506 if (transform_ == transform)
507 return;
508 transform_ = transform;
509 SetNeedsCommit();
512 bool Layer::TransformIsAnimating() const {
513 return layer_animation_controller_->IsAnimatingProperty(Animation::Transform);
516 void Layer::SetScrollOffset(gfx::Vector2d scroll_offset) {
517 DCHECK(IsPropertyChangeAllowed());
518 if (scroll_offset_ == scroll_offset)
519 return;
520 scroll_offset_ = scroll_offset;
521 SetNeedsCommit();
524 void Layer::SetScrollOffsetFromImplSide(gfx::Vector2d scroll_offset) {
525 DCHECK(IsPropertyChangeAllowed());
526 DCHECK(layer_tree_host_ && layer_tree_host_->CommitRequested());
527 if (scroll_offset_ == scroll_offset)
528 return;
529 scroll_offset_ = scroll_offset;
530 if (!did_scroll_callback_.is_null())
531 did_scroll_callback_.Run();
532 // Note: didScroll() could potentially change the layer structure.
533 // "this" may have been destroyed during the process.
536 void Layer::SetMaxScrollOffset(gfx::Vector2d max_scroll_offset) {
537 DCHECK(IsPropertyChangeAllowed());
538 if (max_scroll_offset_ == max_scroll_offset)
539 return;
540 max_scroll_offset_ = max_scroll_offset;
541 SetNeedsCommit();
544 void Layer::SetScrollable(bool scrollable) {
545 DCHECK(IsPropertyChangeAllowed());
546 if (scrollable_ == scrollable)
547 return;
548 scrollable_ = scrollable;
549 SetNeedsCommit();
552 void Layer::SetShouldScrollOnMainThread(bool should_scroll_on_main_thread) {
553 DCHECK(IsPropertyChangeAllowed());
554 if (should_scroll_on_main_thread_ == should_scroll_on_main_thread)
555 return;
556 should_scroll_on_main_thread_ = should_scroll_on_main_thread;
557 SetNeedsCommit();
560 void Layer::SetHaveWheelEventHandlers(bool have_wheel_event_handlers) {
561 DCHECK(IsPropertyChangeAllowed());
562 if (have_wheel_event_handlers_ == have_wheel_event_handlers)
563 return;
564 have_wheel_event_handlers_ = have_wheel_event_handlers;
565 SetNeedsCommit();
568 void Layer::SetNonFastScrollableRegion(const Region& region) {
569 DCHECK(IsPropertyChangeAllowed());
570 if (non_fast_scrollable_region_ == region)
571 return;
572 non_fast_scrollable_region_ = region;
573 SetNeedsCommit();
576 void Layer::SetTouchEventHandlerRegion(const Region& region) {
577 DCHECK(IsPropertyChangeAllowed());
578 if (touch_event_handler_region_ == region)
579 return;
580 touch_event_handler_region_ = region;
583 void Layer::SetDrawCheckerboardForMissingTiles(bool checkerboard) {
584 DCHECK(IsPropertyChangeAllowed());
585 if (draw_checkerboard_for_missing_tiles_ == checkerboard)
586 return;
587 draw_checkerboard_for_missing_tiles_ = checkerboard;
588 SetNeedsCommit();
591 void Layer::SetForceRenderSurface(bool force) {
592 DCHECK(IsPropertyChangeAllowed());
593 if (force_render_surface_ == force)
594 return;
595 force_render_surface_ = force;
596 SetNeedsCommit();
599 void Layer::SetDoubleSided(bool double_sided) {
600 DCHECK(IsPropertyChangeAllowed());
601 if (double_sided_ == double_sided)
602 return;
603 double_sided_ = double_sided;
604 SetNeedsCommit();
607 void Layer::SetIsDrawable(bool is_drawable) {
608 DCHECK(IsPropertyChangeAllowed());
609 if (is_drawable_ == is_drawable)
610 return;
612 is_drawable_ = is_drawable;
613 SetNeedsCommit();
616 void Layer::SetHideLayerAndSubtree(bool hide) {
617 DCHECK(IsPropertyChangeAllowed());
618 if (hide_layer_and_subtree_ == hide)
619 return;
621 hide_layer_and_subtree_ = hide;
622 SetNeedsCommit();
625 void Layer::SetNeedsDisplayRect(const gfx::RectF& dirty_rect) {
626 update_rect_.Union(dirty_rect);
627 needs_display_ = true;
629 // Simply mark the contents as dirty. For non-root layers, the call to
630 // SetNeedsCommit will schedule a fresh compositing pass.
631 // For the root layer, SetNeedsCommit has no effect.
632 if (DrawsContent() && !update_rect_.IsEmpty())
633 SetNeedsCommit();
636 bool Layer::DescendantIsFixedToContainerLayer() const {
637 for (size_t i = 0; i < children_.size(); ++i) {
638 if (children_[i]->position_constraint_.is_fixed_position() ||
639 children_[i]->DescendantIsFixedToContainerLayer())
640 return true;
642 return false;
645 void Layer::SetIsContainerForFixedPositionLayers(bool container) {
646 if (is_container_for_fixed_position_layers_ == container)
647 return;
648 is_container_for_fixed_position_layers_ = container;
650 if (layer_tree_host_ && layer_tree_host_->CommitRequested())
651 return;
653 // Only request a commit if we have a fixed positioned descendant.
654 if (DescendantIsFixedToContainerLayer())
655 SetNeedsCommit();
658 void Layer::SetPositionConstraint(const LayerPositionConstraint& constraint) {
659 DCHECK(IsPropertyChangeAllowed());
660 if (position_constraint_ == constraint)
661 return;
662 position_constraint_ = constraint;
663 SetNeedsCommit();
666 static void RunCopyCallbackOnMainThread(scoped_ptr<CopyOutputRequest> request,
667 scoped_ptr<CopyOutputResult> result) {
668 request->SendResult(result.Pass());
671 static void PostCopyCallbackToMainThread(
672 scoped_refptr<base::SingleThreadTaskRunner> main_thread_task_runner,
673 scoped_ptr<CopyOutputRequest> request,
674 scoped_ptr<CopyOutputResult> result) {
675 main_thread_task_runner->PostTask(FROM_HERE,
676 base::Bind(&RunCopyCallbackOnMainThread,
677 base::Passed(&request),
678 base::Passed(&result)));
681 void Layer::PushPropertiesTo(LayerImpl* layer) {
682 layer->SetAnchorPoint(anchor_point_);
683 layer->SetAnchorPointZ(anchor_point_z_);
684 layer->SetBackgroundColor(background_color_);
685 layer->SetBounds(paint_properties_.bounds);
686 layer->SetContentBounds(content_bounds());
687 layer->SetContentsScale(contents_scale_x(), contents_scale_y());
688 layer->SetDebugName(debug_name_);
689 layer->SetCompositingReasons(compositing_reasons_);
690 layer->SetDoubleSided(double_sided_);
691 layer->SetDrawCheckerboardForMissingTiles(
692 draw_checkerboard_for_missing_tiles_);
693 layer->SetForceRenderSurface(force_render_surface_);
694 layer->SetDrawsContent(DrawsContent());
695 layer->SetHideLayerAndSubtree(hide_layer_and_subtree_);
696 layer->SetFilters(filters());
697 layer->SetFilter(filter());
698 layer->SetBackgroundFilters(background_filters());
699 layer->SetMasksToBounds(masks_to_bounds_);
700 layer->SetShouldScrollOnMainThread(should_scroll_on_main_thread_);
701 layer->SetHaveWheelEventHandlers(have_wheel_event_handlers_);
702 layer->SetNonFastScrollableRegion(non_fast_scrollable_region_);
703 layer->SetTouchEventHandlerRegion(touch_event_handler_region_);
704 layer->SetContentsOpaque(contents_opaque_);
705 if (!layer->OpacityIsAnimatingOnImplOnly() && !OpacityIsAnimating())
706 layer->SetOpacity(opacity_);
707 DCHECK(!(OpacityIsAnimating() && layer->OpacityIsAnimatingOnImplOnly()));
708 layer->SetPosition(position_);
709 layer->SetIsContainerForFixedPositionLayers(
710 IsContainerForFixedPositionLayers());
711 layer->SetFixedContainerSizeDelta(gfx::Vector2dF());
712 layer->SetPositionConstraint(position_constraint_);
713 layer->SetPreserves3d(preserves_3d());
714 layer->SetUseParentBackfaceVisibility(use_parent_backface_visibility_);
715 layer->SetSublayerTransform(sublayer_transform_);
716 if (!layer->TransformIsAnimatingOnImplOnly() && !TransformIsAnimating())
717 layer->SetTransform(transform_);
718 DCHECK(!(TransformIsAnimating() && layer->TransformIsAnimatingOnImplOnly()));
720 layer->SetScrollable(scrollable_);
721 layer->SetScrollOffset(scroll_offset_);
722 layer->SetMaxScrollOffset(max_scroll_offset_);
724 // Wrap the copy_requests_ in a PostTask to the main thread.
725 ScopedPtrVector<CopyOutputRequest> main_thread_copy_requests;
726 for (ScopedPtrVector<CopyOutputRequest>::iterator it = copy_requests_.begin();
727 it != copy_requests_.end();
728 ++it) {
729 scoped_refptr<base::SingleThreadTaskRunner> main_thread_task_runner =
730 layer_tree_host()->proxy()->MainThreadTaskRunner();
731 scoped_ptr<CopyOutputRequest> original_request = copy_requests_.take(it);
732 const CopyOutputRequest& original_request_ref = *original_request;
733 scoped_ptr<CopyOutputRequest> main_thread_request =
734 CopyOutputRequest::CreateRelayRequest(
735 original_request_ref,
736 base::Bind(&PostCopyCallbackToMainThread,
737 main_thread_task_runner,
738 base::Passed(&original_request)));
739 main_thread_copy_requests.push_back(main_thread_request.Pass());
741 copy_requests_.clear();
742 layer->PassCopyRequests(&main_thread_copy_requests);
744 // If the main thread commits multiple times before the impl thread actually
745 // draws, then damage tracking will become incorrect if we simply clobber the
746 // update_rect here. The LayerImpl's update_rect needs to accumulate (i.e.
747 // union) any update changes that have occurred on the main thread.
748 update_rect_.Union(layer->update_rect());
749 layer->set_update_rect(update_rect_);
751 if (layer->layer_tree_impl()->settings().impl_side_painting) {
752 DCHECK(layer->layer_tree_impl()->IsPendingTree());
753 LayerImpl* active_twin =
754 layer->layer_tree_impl()->FindActiveTreeLayerById(id());
755 // Update the scroll delta from the active layer, which may have
756 // adjusted its scroll delta prior to this pending layer being created.
757 // This code is identical to that in LayerImpl::SetScrollDelta.
758 if (active_twin) {
759 DCHECK(layer->sent_scroll_delta().IsZero());
760 layer->SetScrollDelta(active_twin->ScrollDelta() -
761 active_twin->sent_scroll_delta());
763 } else {
764 layer->SetScrollDelta(layer->ScrollDelta() - layer->sent_scroll_delta());
765 layer->SetSentScrollDelta(gfx::Vector2d());
768 layer->SetStackingOrderChanged(stacking_order_changed_);
770 layer_animation_controller_->PushAnimationUpdatesTo(
771 layer->layer_animation_controller());
773 // Reset any state that should be cleared for the next update.
774 stacking_order_changed_ = false;
775 update_rect_ = gfx::RectF();
778 scoped_ptr<LayerImpl> Layer::CreateLayerImpl(LayerTreeImpl* tree_impl) {
779 return LayerImpl::Create(tree_impl, layer_id_);
782 bool Layer::DrawsContent() const {
783 return is_drawable_;
786 void Layer::SavePaintProperties() {
787 // TODO(reveman): Save all layer properties that we depend on not
788 // changing until PushProperties() has been called. crbug.com/231016
789 paint_properties_.bounds = bounds_;
792 bool Layer::Update(ResourceUpdateQueue* queue,
793 const OcclusionTracker* occlusion) {
794 return false;
797 bool Layer::NeedMoreUpdates() {
798 return false;
801 void Layer::SetDebugName(const std::string& debug_name) {
802 debug_name_ = debug_name;
803 SetNeedsCommit();
806 void Layer::SetCompositingReasons(CompositingReasons reasons) {
807 compositing_reasons_ = reasons;
810 void Layer::CreateRenderSurface() {
811 DCHECK(!draw_properties_.render_surface);
812 draw_properties_.render_surface = make_scoped_ptr(new RenderSurface(this));
813 draw_properties_.render_target = this;
816 void Layer::ClearRenderSurface() {
817 draw_properties_.render_surface.reset();
820 void Layer::OnOpacityAnimated(float opacity) {
821 // This is called due to an ongoing accelerated animation. Since this
822 // animation is also being run on the impl thread, there is no need to request
823 // a commit to push this value over, so set the value directly rather than
824 // calling SetOpacity.
825 opacity_ = opacity;
828 void Layer::OnTransformAnimated(const gfx::Transform& transform) {
829 // This is called due to an ongoing accelerated animation. Since this
830 // animation is also being run on the impl thread, there is no need to request
831 // a commit to push this value over, so set this value directly rather than
832 // calling SetTransform.
833 transform_ = transform;
836 bool Layer::IsActive() const {
837 return true;
840 bool Layer::AddAnimation(scoped_ptr <Animation> animation) {
841 if (!layer_animation_controller_->animation_registrar())
842 return false;
844 UMA_HISTOGRAM_BOOLEAN("Renderer.AnimationAddedToOrphanLayer",
845 !layer_tree_host_);
846 layer_animation_controller_->AddAnimation(animation.Pass());
847 SetNeedsCommit();
848 return true;
851 void Layer::PauseAnimation(int animation_id, double time_offset) {
852 layer_animation_controller_->PauseAnimation(animation_id, time_offset);
853 SetNeedsCommit();
856 void Layer::RemoveAnimation(int animation_id) {
857 layer_animation_controller_->RemoveAnimation(animation_id);
858 SetNeedsCommit();
861 void Layer::SuspendAnimations(double monotonic_time) {
862 layer_animation_controller_->SuspendAnimations(monotonic_time);
863 SetNeedsCommit();
866 void Layer::ResumeAnimations(double monotonic_time) {
867 layer_animation_controller_->ResumeAnimations(monotonic_time);
868 SetNeedsCommit();
871 void Layer::SetLayerAnimationControllerForTest(
872 scoped_refptr<LayerAnimationController> controller) {
873 layer_animation_controller_->RemoveValueObserver(this);
874 layer_animation_controller_ = controller;
875 layer_animation_controller_->set_force_sync();
876 layer_animation_controller_->AddValueObserver(this);
877 SetNeedsCommit();
880 bool Layer::HasActiveAnimation() const {
881 return layer_animation_controller_->HasActiveAnimation();
884 void Layer::AddLayerAnimationEventObserver(
885 LayerAnimationEventObserver* animation_observer) {
886 layer_animation_controller_->AddEventObserver(animation_observer);
889 void Layer::RemoveLayerAnimationEventObserver(
890 LayerAnimationEventObserver* animation_observer) {
891 layer_animation_controller_->RemoveEventObserver(animation_observer);
894 Region Layer::VisibleContentOpaqueRegion() const {
895 if (contents_opaque())
896 return visible_content_rect();
897 return Region();
900 ScrollbarLayer* Layer::ToScrollbarLayer() {
901 return NULL;
904 RenderingStatsInstrumentation* Layer::rendering_stats_instrumentation() const {
905 return layer_tree_host_->rendering_stats_instrumentation();
908 bool Layer::SupportsLCDText() const {
909 return false;
912 } // namespace cc