Lots of random cleanups, mostly for native_theme_win.cc:
[chromium-blink-merge.git] / cc / layers / layer.cc
blob82978daa03633a23d52b405414b4be0ead50b08c
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/atomic_sequence_num.h"
10 #include "base/debug/trace_event.h"
11 #include "base/location.h"
12 #include "base/metrics/histogram.h"
13 #include "base/single_thread_task_runner.h"
14 #include "base/time/time.h"
15 #include "cc/animation/animation.h"
16 #include "cc/animation/animation_events.h"
17 #include "cc/animation/animation_registrar.h"
18 #include "cc/animation/keyframed_animation_curve.h"
19 #include "cc/animation/layer_animation_controller.h"
20 #include "cc/layers/layer_client.h"
21 #include "cc/layers/layer_impl.h"
22 #include "cc/layers/scrollbar_layer_interface.h"
23 #include "cc/output/copy_output_request.h"
24 #include "cc/output/copy_output_result.h"
25 #include "cc/trees/layer_tree_host.h"
26 #include "cc/trees/layer_tree_impl.h"
27 #include "third_party/skia/include/core/SkImageFilter.h"
28 #include "ui/gfx/geometry/vector2d_conversions.h"
29 #include "ui/gfx/rect_conversions.h"
31 namespace cc {
33 base::StaticAtomicSequenceNumber g_next_layer_id;
35 scoped_refptr<Layer> Layer::Create() {
36 return make_scoped_refptr(new Layer());
39 Layer::Layer()
40 : needs_push_properties_(false),
41 num_dependents_need_push_properties_(false),
42 stacking_order_changed_(false),
43 // Layer IDs start from 1.
44 layer_id_(g_next_layer_id.GetNext() + 1),
45 ignore_set_needs_commit_(false),
46 sorting_context_id_(0),
47 parent_(NULL),
48 layer_tree_host_(NULL),
49 scroll_clip_layer_id_(INVALID_ID),
50 should_scroll_on_main_thread_(false),
51 have_wheel_event_handlers_(false),
52 have_scroll_event_handlers_(false),
53 user_scrollable_horizontal_(true),
54 user_scrollable_vertical_(true),
55 is_root_for_isolated_group_(false),
56 is_container_for_fixed_position_layers_(false),
57 is_drawable_(false),
58 hide_layer_and_subtree_(false),
59 masks_to_bounds_(false),
60 contents_opaque_(false),
61 double_sided_(true),
62 should_flatten_transform_(true),
63 use_parent_backface_visibility_(false),
64 draw_checkerboard_for_missing_tiles_(false),
65 force_render_surface_(false),
66 transform_is_invertible_(true),
67 background_color_(0),
68 opacity_(1.f),
69 blend_mode_(SkXfermode::kSrcOver_Mode),
70 scroll_parent_(NULL),
71 clip_parent_(NULL),
72 replica_layer_(NULL),
73 raster_scale_(0.f),
74 client_(NULL) {
75 layer_animation_controller_ = LayerAnimationController::Create(layer_id_);
76 layer_animation_controller_->AddValueObserver(this);
77 layer_animation_controller_->set_value_provider(this);
80 Layer::~Layer() {
81 // Our parent should be holding a reference to us so there should be no
82 // way for us to be destroyed while we still have a parent.
83 DCHECK(!parent());
84 // Similarly we shouldn't have a layer tree host since it also keeps a
85 // reference to us.
86 DCHECK(!layer_tree_host());
88 layer_animation_controller_->RemoveValueObserver(this);
89 layer_animation_controller_->remove_value_provider(this);
91 // Remove the parent reference from all children and dependents.
92 RemoveAllChildren();
93 if (mask_layer_.get()) {
94 DCHECK_EQ(this, mask_layer_->parent());
95 mask_layer_->RemoveFromParent();
97 if (replica_layer_.get()) {
98 DCHECK_EQ(this, replica_layer_->parent());
99 replica_layer_->RemoveFromParent();
102 RemoveFromScrollTree();
103 RemoveFromClipTree();
106 void Layer::SetLayerTreeHost(LayerTreeHost* host) {
107 if (layer_tree_host_ == host)
108 return;
110 layer_tree_host_ = host;
112 // When changing hosts, the layer needs to commit its properties to the impl
113 // side for the new host.
114 SetNeedsPushProperties();
116 for (size_t i = 0; i < children_.size(); ++i)
117 children_[i]->SetLayerTreeHost(host);
119 if (mask_layer_.get())
120 mask_layer_->SetLayerTreeHost(host);
121 if (replica_layer_.get())
122 replica_layer_->SetLayerTreeHost(host);
124 if (host) {
125 layer_animation_controller_->SetAnimationRegistrar(
126 host->animation_registrar());
128 if (host->settings().layer_transforms_should_scale_layer_contents)
129 reset_raster_scale_to_unknown();
132 if (host && layer_animation_controller_->has_any_animation())
133 host->SetNeedsCommit();
136 void Layer::SetNeedsUpdate() {
137 if (layer_tree_host_ && !ignore_set_needs_commit_)
138 layer_tree_host_->SetNeedsUpdateLayers();
141 void Layer::SetNeedsCommit() {
142 if (!layer_tree_host_)
143 return;
145 SetNeedsPushProperties();
147 if (ignore_set_needs_commit_)
148 return;
150 layer_tree_host_->SetNeedsCommit();
153 void Layer::SetNeedsFullTreeSync() {
154 if (!layer_tree_host_)
155 return;
157 layer_tree_host_->SetNeedsFullTreeSync();
160 void Layer::SetNextCommitWaitsForActivation() {
161 if (!layer_tree_host_)
162 return;
164 layer_tree_host_->SetNextCommitWaitsForActivation();
167 void Layer::SetNeedsPushProperties() {
168 if (needs_push_properties_)
169 return;
170 if (!parent_should_know_need_push_properties() && parent_)
171 parent_->AddDependentNeedsPushProperties();
172 needs_push_properties_ = true;
175 void Layer::AddDependentNeedsPushProperties() {
176 DCHECK_GE(num_dependents_need_push_properties_, 0);
178 if (!parent_should_know_need_push_properties() && parent_)
179 parent_->AddDependentNeedsPushProperties();
181 num_dependents_need_push_properties_++;
184 void Layer::RemoveDependentNeedsPushProperties() {
185 num_dependents_need_push_properties_--;
186 DCHECK_GE(num_dependents_need_push_properties_, 0);
188 if (!parent_should_know_need_push_properties() && parent_)
189 parent_->RemoveDependentNeedsPushProperties();
192 bool Layer::IsPropertyChangeAllowed() const {
193 if (!layer_tree_host_)
194 return true;
196 if (!layer_tree_host_->settings().strict_layer_property_change_checking)
197 return true;
199 return !layer_tree_host_->in_paint_layer_contents();
202 gfx::Rect Layer::LayerRectToContentRect(const gfx::RectF& layer_rect) const {
203 gfx::RectF content_rect =
204 gfx::ScaleRect(layer_rect, contents_scale_x(), contents_scale_y());
205 // Intersect with content rect to avoid the extra pixel because for some
206 // values x and y, ceil((x / y) * y) may be x + 1.
207 content_rect.Intersect(gfx::Rect(content_bounds()));
208 return gfx::ToEnclosingRect(content_rect);
211 skia::RefPtr<SkPicture> Layer::GetPicture() const {
212 return skia::RefPtr<SkPicture>();
215 void Layer::SetParent(Layer* layer) {
216 DCHECK(!layer || !layer->HasAncestor(this));
218 if (parent_should_know_need_push_properties()) {
219 if (parent_)
220 parent_->RemoveDependentNeedsPushProperties();
221 if (layer)
222 layer->AddDependentNeedsPushProperties();
225 parent_ = layer;
226 SetLayerTreeHost(parent_ ? parent_->layer_tree_host() : NULL);
228 if (!layer_tree_host_)
229 return;
230 const LayerTreeSettings& settings = layer_tree_host_->settings();
231 if (!settings.layer_transforms_should_scale_layer_contents)
232 return;
234 reset_raster_scale_to_unknown();
235 if (mask_layer_.get())
236 mask_layer_->reset_raster_scale_to_unknown();
237 if (replica_layer_.get() && replica_layer_->mask_layer_.get())
238 replica_layer_->mask_layer_->reset_raster_scale_to_unknown();
241 void Layer::AddChild(scoped_refptr<Layer> child) {
242 InsertChild(child, children_.size());
245 void Layer::InsertChild(scoped_refptr<Layer> child, size_t index) {
246 DCHECK(IsPropertyChangeAllowed());
247 child->RemoveFromParent();
248 child->SetParent(this);
249 child->stacking_order_changed_ = true;
251 index = std::min(index, children_.size());
252 children_.insert(children_.begin() + index, child);
253 SetNeedsFullTreeSync();
256 void Layer::RemoveFromParent() {
257 DCHECK(IsPropertyChangeAllowed());
258 if (parent_)
259 parent_->RemoveChildOrDependent(this);
262 void Layer::RemoveChildOrDependent(Layer* child) {
263 if (mask_layer_.get() == child) {
264 mask_layer_->SetParent(NULL);
265 mask_layer_ = NULL;
266 SetNeedsFullTreeSync();
267 return;
269 if (replica_layer_.get() == child) {
270 replica_layer_->SetParent(NULL);
271 replica_layer_ = NULL;
272 SetNeedsFullTreeSync();
273 return;
276 for (LayerList::iterator iter = children_.begin();
277 iter != children_.end();
278 ++iter) {
279 if (iter->get() != child)
280 continue;
282 child->SetParent(NULL);
283 children_.erase(iter);
284 SetNeedsFullTreeSync();
285 return;
289 void Layer::ReplaceChild(Layer* reference, scoped_refptr<Layer> new_layer) {
290 DCHECK(reference);
291 DCHECK_EQ(reference->parent(), this);
292 DCHECK(IsPropertyChangeAllowed());
294 if (reference == new_layer.get())
295 return;
297 int reference_index = IndexOfChild(reference);
298 if (reference_index == -1) {
299 NOTREACHED();
300 return;
303 reference->RemoveFromParent();
305 if (new_layer.get()) {
306 new_layer->RemoveFromParent();
307 InsertChild(new_layer, reference_index);
311 int Layer::IndexOfChild(const Layer* reference) {
312 for (size_t i = 0; i < children_.size(); ++i) {
313 if (children_[i].get() == reference)
314 return i;
316 return -1;
319 void Layer::SetBounds(const gfx::Size& size) {
320 DCHECK(IsPropertyChangeAllowed());
321 if (bounds() == size)
322 return;
324 bounds_ = size;
325 SetNeedsCommit();
328 Layer* Layer::RootLayer() {
329 Layer* layer = this;
330 while (layer->parent())
331 layer = layer->parent();
332 return layer;
335 void Layer::RemoveAllChildren() {
336 DCHECK(IsPropertyChangeAllowed());
337 while (children_.size()) {
338 Layer* layer = children_[0].get();
339 DCHECK_EQ(this, layer->parent());
340 layer->RemoveFromParent();
344 void Layer::SetChildren(const LayerList& children) {
345 DCHECK(IsPropertyChangeAllowed());
346 if (children == children_)
347 return;
349 RemoveAllChildren();
350 for (size_t i = 0; i < children.size(); ++i)
351 AddChild(children[i]);
354 bool Layer::HasAncestor(const Layer* ancestor) const {
355 for (const Layer* layer = parent(); layer; layer = layer->parent()) {
356 if (layer == ancestor)
357 return true;
359 return false;
362 void Layer::RequestCopyOfOutput(
363 scoped_ptr<CopyOutputRequest> request) {
364 DCHECK(IsPropertyChangeAllowed());
365 if (request->IsEmpty())
366 return;
367 copy_requests_.push_back(request.Pass());
368 SetNeedsCommit();
371 void Layer::SetBackgroundColor(SkColor background_color) {
372 DCHECK(IsPropertyChangeAllowed());
373 if (background_color_ == background_color)
374 return;
375 background_color_ = background_color;
376 SetNeedsCommit();
379 SkColor Layer::SafeOpaqueBackgroundColor() const {
380 SkColor color = background_color();
381 if (SkColorGetA(color) == 255 && !contents_opaque()) {
382 color = SK_ColorTRANSPARENT;
383 } else if (SkColorGetA(color) != 255 && contents_opaque()) {
384 for (const Layer* layer = parent(); layer;
385 layer = layer->parent()) {
386 color = layer->background_color();
387 if (SkColorGetA(color) == 255)
388 break;
390 if (SkColorGetA(color) != 255)
391 color = layer_tree_host_->background_color();
392 if (SkColorGetA(color) != 255)
393 color = SkColorSetA(color, 255);
395 return color;
398 void Layer::CalculateContentsScale(float ideal_contents_scale,
399 float device_scale_factor,
400 float page_scale_factor,
401 float maximum_animation_contents_scale,
402 bool animating_transform_to_screen,
403 float* contents_scale_x,
404 float* contents_scale_y,
405 gfx::Size* content_bounds) {
406 DCHECK(layer_tree_host_);
408 *contents_scale_x = 1;
409 *contents_scale_y = 1;
410 *content_bounds = bounds();
413 void Layer::SetMasksToBounds(bool masks_to_bounds) {
414 DCHECK(IsPropertyChangeAllowed());
415 if (masks_to_bounds_ == masks_to_bounds)
416 return;
417 masks_to_bounds_ = masks_to_bounds;
418 SetNeedsCommit();
421 void Layer::SetMaskLayer(Layer* mask_layer) {
422 DCHECK(IsPropertyChangeAllowed());
423 if (mask_layer_.get() == mask_layer)
424 return;
425 if (mask_layer_.get()) {
426 DCHECK_EQ(this, mask_layer_->parent());
427 mask_layer_->RemoveFromParent();
429 mask_layer_ = mask_layer;
430 if (mask_layer_.get()) {
431 DCHECK(!mask_layer_->parent());
432 mask_layer_->RemoveFromParent();
433 mask_layer_->SetParent(this);
434 mask_layer_->SetIsMask(true);
436 SetNeedsFullTreeSync();
439 void Layer::SetReplicaLayer(Layer* layer) {
440 DCHECK(IsPropertyChangeAllowed());
441 if (replica_layer_.get() == layer)
442 return;
443 if (replica_layer_.get()) {
444 DCHECK_EQ(this, replica_layer_->parent());
445 replica_layer_->RemoveFromParent();
447 replica_layer_ = layer;
448 if (replica_layer_.get()) {
449 DCHECK(!replica_layer_->parent());
450 replica_layer_->RemoveFromParent();
451 replica_layer_->SetParent(this);
453 SetNeedsFullTreeSync();
456 void Layer::SetFilters(const FilterOperations& filters) {
457 DCHECK(IsPropertyChangeAllowed());
458 if (filters_ == filters)
459 return;
460 filters_ = filters;
461 SetNeedsCommit();
464 bool Layer::FilterIsAnimating() const {
465 return layer_animation_controller_->IsAnimatingProperty(Animation::Filter);
468 void Layer::SetBackgroundFilters(const FilterOperations& filters) {
469 DCHECK(IsPropertyChangeAllowed());
470 if (background_filters_ == filters)
471 return;
472 background_filters_ = filters;
473 SetNeedsCommit();
476 void Layer::SetOpacity(float opacity) {
477 DCHECK(IsPropertyChangeAllowed());
478 if (opacity_ == opacity)
479 return;
480 opacity_ = opacity;
481 SetNeedsCommit();
484 bool Layer::OpacityIsAnimating() const {
485 return layer_animation_controller_->IsAnimatingProperty(Animation::Opacity);
488 bool Layer::OpacityCanAnimateOnImplThread() const {
489 return false;
492 void Layer::SetBlendMode(SkXfermode::Mode blend_mode) {
493 DCHECK(IsPropertyChangeAllowed());
494 if (blend_mode_ == blend_mode)
495 return;
497 // Allowing only blend modes that are defined in the CSS Compositing standard:
498 // http://dev.w3.org/fxtf/compositing-1/#blending
499 switch (blend_mode) {
500 case SkXfermode::kSrcOver_Mode:
501 case SkXfermode::kScreen_Mode:
502 case SkXfermode::kOverlay_Mode:
503 case SkXfermode::kDarken_Mode:
504 case SkXfermode::kLighten_Mode:
505 case SkXfermode::kColorDodge_Mode:
506 case SkXfermode::kColorBurn_Mode:
507 case SkXfermode::kHardLight_Mode:
508 case SkXfermode::kSoftLight_Mode:
509 case SkXfermode::kDifference_Mode:
510 case SkXfermode::kExclusion_Mode:
511 case SkXfermode::kMultiply_Mode:
512 case SkXfermode::kHue_Mode:
513 case SkXfermode::kSaturation_Mode:
514 case SkXfermode::kColor_Mode:
515 case SkXfermode::kLuminosity_Mode:
516 // supported blend modes
517 break;
518 case SkXfermode::kClear_Mode:
519 case SkXfermode::kSrc_Mode:
520 case SkXfermode::kDst_Mode:
521 case SkXfermode::kDstOver_Mode:
522 case SkXfermode::kSrcIn_Mode:
523 case SkXfermode::kDstIn_Mode:
524 case SkXfermode::kSrcOut_Mode:
525 case SkXfermode::kDstOut_Mode:
526 case SkXfermode::kSrcATop_Mode:
527 case SkXfermode::kDstATop_Mode:
528 case SkXfermode::kXor_Mode:
529 case SkXfermode::kPlus_Mode:
530 case SkXfermode::kModulate_Mode:
531 // Porter Duff Compositing Operators are not yet supported
532 // http://dev.w3.org/fxtf/compositing-1/#porterduffcompositingoperators
533 NOTREACHED();
534 return;
537 blend_mode_ = blend_mode;
538 SetNeedsCommit();
541 void Layer::SetIsRootForIsolatedGroup(bool root) {
542 DCHECK(IsPropertyChangeAllowed());
543 if (is_root_for_isolated_group_ == root)
544 return;
545 is_root_for_isolated_group_ = root;
546 SetNeedsCommit();
549 void Layer::SetContentsOpaque(bool opaque) {
550 DCHECK(IsPropertyChangeAllowed());
551 if (contents_opaque_ == opaque)
552 return;
553 contents_opaque_ = opaque;
554 SetNeedsCommit();
557 void Layer::SetPosition(const gfx::PointF& position) {
558 DCHECK(IsPropertyChangeAllowed());
559 if (position_ == position)
560 return;
561 position_ = position;
562 SetNeedsCommit();
565 bool Layer::IsContainerForFixedPositionLayers() const {
566 if (!transform_.IsIdentityOrTranslation())
567 return true;
568 if (parent_ && !parent_->transform_.IsIdentityOrTranslation())
569 return true;
570 return is_container_for_fixed_position_layers_;
573 void Layer::SetTransform(const gfx::Transform& transform) {
574 DCHECK(IsPropertyChangeAllowed());
575 if (transform_ == transform)
576 return;
577 transform_ = transform;
578 transform_is_invertible_ = transform.IsInvertible();
579 SetNeedsCommit();
582 void Layer::SetTransformOrigin(const gfx::Point3F& transform_origin) {
583 DCHECK(IsPropertyChangeAllowed());
584 if (transform_origin_ == transform_origin)
585 return;
586 transform_origin_ = transform_origin;
587 SetNeedsCommit();
590 bool Layer::TransformIsAnimating() const {
591 return layer_animation_controller_->IsAnimatingProperty(Animation::Transform);
594 void Layer::SetScrollParent(Layer* parent) {
595 DCHECK(IsPropertyChangeAllowed());
596 if (scroll_parent_ == parent)
597 return;
599 if (scroll_parent_)
600 scroll_parent_->RemoveScrollChild(this);
602 scroll_parent_ = parent;
604 if (scroll_parent_)
605 scroll_parent_->AddScrollChild(this);
607 SetNeedsCommit();
610 void Layer::AddScrollChild(Layer* child) {
611 if (!scroll_children_)
612 scroll_children_.reset(new std::set<Layer*>);
613 scroll_children_->insert(child);
614 SetNeedsCommit();
617 void Layer::RemoveScrollChild(Layer* child) {
618 scroll_children_->erase(child);
619 if (scroll_children_->empty())
620 scroll_children_.reset();
621 SetNeedsCommit();
624 void Layer::SetClipParent(Layer* ancestor) {
625 DCHECK(IsPropertyChangeAllowed());
626 if (clip_parent_ == ancestor)
627 return;
629 if (clip_parent_)
630 clip_parent_->RemoveClipChild(this);
632 clip_parent_ = ancestor;
634 if (clip_parent_)
635 clip_parent_->AddClipChild(this);
637 SetNeedsCommit();
640 void Layer::AddClipChild(Layer* child) {
641 if (!clip_children_)
642 clip_children_.reset(new std::set<Layer*>);
643 clip_children_->insert(child);
644 SetNeedsCommit();
647 void Layer::RemoveClipChild(Layer* child) {
648 clip_children_->erase(child);
649 if (clip_children_->empty())
650 clip_children_.reset();
651 SetNeedsCommit();
654 void Layer::SetScrollOffset(gfx::Vector2d scroll_offset) {
655 DCHECK(IsPropertyChangeAllowed());
657 if (scroll_offset_ == scroll_offset)
658 return;
659 scroll_offset_ = scroll_offset;
660 SetNeedsCommit();
663 void Layer::SetScrollOffsetFromImplSide(const gfx::Vector2d& scroll_offset) {
664 DCHECK(IsPropertyChangeAllowed());
665 // This function only gets called during a BeginMainFrame, so there
666 // is no need to call SetNeedsUpdate here.
667 DCHECK(layer_tree_host_ && layer_tree_host_->CommitRequested());
668 if (scroll_offset_ == scroll_offset)
669 return;
670 scroll_offset_ = scroll_offset;
671 SetNeedsPushProperties();
672 if (!did_scroll_callback_.is_null())
673 did_scroll_callback_.Run();
674 // The callback could potentially change the layer structure:
675 // "this" may have been destroyed during the process.
678 void Layer::SetScrollClipLayerId(int clip_layer_id) {
679 DCHECK(IsPropertyChangeAllowed());
680 if (scroll_clip_layer_id_ == clip_layer_id)
681 return;
682 scroll_clip_layer_id_ = clip_layer_id;
683 SetNeedsCommit();
686 void Layer::SetUserScrollable(bool horizontal, bool vertical) {
687 DCHECK(IsPropertyChangeAllowed());
688 if (user_scrollable_horizontal_ == horizontal &&
689 user_scrollable_vertical_ == vertical)
690 return;
691 user_scrollable_horizontal_ = horizontal;
692 user_scrollable_vertical_ = vertical;
693 SetNeedsCommit();
696 void Layer::SetShouldScrollOnMainThread(bool should_scroll_on_main_thread) {
697 DCHECK(IsPropertyChangeAllowed());
698 if (should_scroll_on_main_thread_ == should_scroll_on_main_thread)
699 return;
700 should_scroll_on_main_thread_ = should_scroll_on_main_thread;
701 SetNeedsCommit();
704 void Layer::SetHaveWheelEventHandlers(bool have_wheel_event_handlers) {
705 DCHECK(IsPropertyChangeAllowed());
706 if (have_wheel_event_handlers_ == have_wheel_event_handlers)
707 return;
708 have_wheel_event_handlers_ = have_wheel_event_handlers;
709 SetNeedsCommit();
712 void Layer::SetHaveScrollEventHandlers(bool have_scroll_event_handlers) {
713 DCHECK(IsPropertyChangeAllowed());
714 if (have_scroll_event_handlers_ == have_scroll_event_handlers)
715 return;
716 have_scroll_event_handlers_ = have_scroll_event_handlers;
717 SetNeedsCommit();
720 void Layer::SetNonFastScrollableRegion(const Region& region) {
721 DCHECK(IsPropertyChangeAllowed());
722 if (non_fast_scrollable_region_ == region)
723 return;
724 non_fast_scrollable_region_ = region;
725 SetNeedsCommit();
728 void Layer::SetTouchEventHandlerRegion(const Region& region) {
729 DCHECK(IsPropertyChangeAllowed());
730 if (touch_event_handler_region_ == region)
731 return;
732 touch_event_handler_region_ = region;
733 SetNeedsCommit();
736 void Layer::SetDrawCheckerboardForMissingTiles(bool checkerboard) {
737 DCHECK(IsPropertyChangeAllowed());
738 if (draw_checkerboard_for_missing_tiles_ == checkerboard)
739 return;
740 draw_checkerboard_for_missing_tiles_ = checkerboard;
741 SetNeedsCommit();
744 void Layer::SetForceRenderSurface(bool force) {
745 DCHECK(IsPropertyChangeAllowed());
746 if (force_render_surface_ == force)
747 return;
748 force_render_surface_ = force;
749 SetNeedsCommit();
752 void Layer::SetDoubleSided(bool double_sided) {
753 DCHECK(IsPropertyChangeAllowed());
754 if (double_sided_ == double_sided)
755 return;
756 double_sided_ = double_sided;
757 SetNeedsCommit();
760 void Layer::Set3dSortingContextId(int id) {
761 DCHECK(IsPropertyChangeAllowed());
762 if (id == sorting_context_id_)
763 return;
764 sorting_context_id_ = id;
765 SetNeedsCommit();
768 void Layer::SetShouldFlattenTransform(bool should_flatten) {
769 DCHECK(IsPropertyChangeAllowed());
770 if (should_flatten_transform_ == should_flatten)
771 return;
772 should_flatten_transform_ = should_flatten;
773 SetNeedsCommit();
776 void Layer::SetIsDrawable(bool is_drawable) {
777 DCHECK(IsPropertyChangeAllowed());
778 if (is_drawable_ == is_drawable)
779 return;
781 is_drawable_ = is_drawable;
782 SetNeedsCommit();
785 void Layer::SetHideLayerAndSubtree(bool hide) {
786 DCHECK(IsPropertyChangeAllowed());
787 if (hide_layer_and_subtree_ == hide)
788 return;
790 hide_layer_and_subtree_ = hide;
791 SetNeedsCommit();
794 void Layer::SetNeedsDisplayRect(const gfx::RectF& dirty_rect) {
795 if (dirty_rect.IsEmpty())
796 return;
798 SetNeedsPushProperties();
799 update_rect_.Union(dirty_rect);
801 if (DrawsContent())
802 SetNeedsUpdate();
805 bool Layer::DescendantIsFixedToContainerLayer() const {
806 for (size_t i = 0; i < children_.size(); ++i) {
807 if (children_[i]->position_constraint_.is_fixed_position() ||
808 children_[i]->DescendantIsFixedToContainerLayer())
809 return true;
811 return false;
814 void Layer::SetIsContainerForFixedPositionLayers(bool container) {
815 if (is_container_for_fixed_position_layers_ == container)
816 return;
817 is_container_for_fixed_position_layers_ = container;
819 if (layer_tree_host_ && layer_tree_host_->CommitRequested())
820 return;
822 // Only request a commit if we have a fixed positioned descendant.
823 if (DescendantIsFixedToContainerLayer())
824 SetNeedsCommit();
827 void Layer::SetPositionConstraint(const LayerPositionConstraint& constraint) {
828 DCHECK(IsPropertyChangeAllowed());
829 if (position_constraint_ == constraint)
830 return;
831 position_constraint_ = constraint;
832 SetNeedsCommit();
835 static void RunCopyCallbackOnMainThread(scoped_ptr<CopyOutputRequest> request,
836 scoped_ptr<CopyOutputResult> result) {
837 request->SendResult(result.Pass());
840 static void PostCopyCallbackToMainThread(
841 scoped_refptr<base::SingleThreadTaskRunner> main_thread_task_runner,
842 scoped_ptr<CopyOutputRequest> request,
843 scoped_ptr<CopyOutputResult> result) {
844 main_thread_task_runner->PostTask(FROM_HERE,
845 base::Bind(&RunCopyCallbackOnMainThread,
846 base::Passed(&request),
847 base::Passed(&result)));
850 void Layer::PushPropertiesTo(LayerImpl* layer) {
851 DCHECK(layer_tree_host_);
853 // If we did not SavePaintProperties() for the layer this frame, then push the
854 // real property values, not the paint property values.
855 bool use_paint_properties = paint_properties_.source_frame_number ==
856 layer_tree_host_->source_frame_number();
858 layer->SetTransformOrigin(transform_origin_);
859 layer->SetBackgroundColor(background_color_);
860 layer->SetBounds(use_paint_properties ? paint_properties_.bounds
861 : bounds_);
862 layer->SetContentBounds(content_bounds());
863 layer->SetContentsScale(contents_scale_x(), contents_scale_y());
865 bool is_tracing;
866 TRACE_EVENT_CATEGORY_GROUP_ENABLED(TRACE_DISABLED_BY_DEFAULT("cc.debug"),
867 &is_tracing);
868 if (is_tracing)
869 layer->SetDebugInfo(TakeDebugInfo());
871 layer->SetDoubleSided(double_sided_);
872 layer->SetDrawCheckerboardForMissingTiles(
873 draw_checkerboard_for_missing_tiles_);
874 layer->SetForceRenderSurface(force_render_surface_);
875 layer->SetDrawsContent(DrawsContent());
876 layer->SetHideLayerAndSubtree(hide_layer_and_subtree_);
877 if (!layer->FilterIsAnimatingOnImplOnly() && !FilterIsAnimating())
878 layer->SetFilters(filters_);
879 DCHECK(!(FilterIsAnimating() && layer->FilterIsAnimatingOnImplOnly()));
880 layer->SetBackgroundFilters(background_filters());
881 layer->SetMasksToBounds(masks_to_bounds_);
882 layer->SetShouldScrollOnMainThread(should_scroll_on_main_thread_);
883 layer->SetHaveWheelEventHandlers(have_wheel_event_handlers_);
884 layer->SetHaveScrollEventHandlers(have_scroll_event_handlers_);
885 layer->SetNonFastScrollableRegion(non_fast_scrollable_region_);
886 layer->SetTouchEventHandlerRegion(touch_event_handler_region_);
887 layer->SetContentsOpaque(contents_opaque_);
888 if (!layer->OpacityIsAnimatingOnImplOnly() && !OpacityIsAnimating())
889 layer->SetOpacity(opacity_);
890 DCHECK(!(OpacityIsAnimating() && layer->OpacityIsAnimatingOnImplOnly()));
891 layer->SetBlendMode(blend_mode_);
892 layer->SetIsRootForIsolatedGroup(is_root_for_isolated_group_);
893 layer->SetPosition(position_);
894 layer->SetIsContainerForFixedPositionLayers(
895 IsContainerForFixedPositionLayers());
896 layer->SetPositionConstraint(position_constraint_);
897 layer->SetShouldFlattenTransform(should_flatten_transform_);
898 layer->SetUseParentBackfaceVisibility(use_parent_backface_visibility_);
899 if (!layer->TransformIsAnimatingOnImplOnly() && !TransformIsAnimating())
900 layer->SetTransformAndInvertibility(transform_, transform_is_invertible_);
901 DCHECK(!(TransformIsAnimating() && layer->TransformIsAnimatingOnImplOnly()));
902 layer->Set3dSortingContextId(sorting_context_id_);
904 layer->SetScrollClipLayer(scroll_clip_layer_id_);
905 layer->set_user_scrollable_horizontal(user_scrollable_horizontal_);
906 layer->set_user_scrollable_vertical(user_scrollable_vertical_);
908 LayerImpl* scroll_parent = NULL;
909 if (scroll_parent_) {
910 scroll_parent = layer->layer_tree_impl()->LayerById(scroll_parent_->id());
911 DCHECK(scroll_parent);
914 layer->SetScrollParent(scroll_parent);
915 if (scroll_children_) {
916 std::set<LayerImpl*>* scroll_children = new std::set<LayerImpl*>;
917 for (std::set<Layer*>::iterator it = scroll_children_->begin();
918 it != scroll_children_->end();
919 ++it) {
920 DCHECK_EQ((*it)->scroll_parent(), this);
921 LayerImpl* scroll_child =
922 layer->layer_tree_impl()->LayerById((*it)->id());
923 DCHECK(scroll_child);
924 scroll_children->insert(scroll_child);
926 layer->SetScrollChildren(scroll_children);
927 } else {
928 layer->SetScrollChildren(NULL);
931 LayerImpl* clip_parent = NULL;
932 if (clip_parent_) {
933 clip_parent =
934 layer->layer_tree_impl()->LayerById(clip_parent_->id());
935 DCHECK(clip_parent);
938 layer->SetClipParent(clip_parent);
939 if (clip_children_) {
940 std::set<LayerImpl*>* clip_children = new std::set<LayerImpl*>;
941 for (std::set<Layer*>::iterator it = clip_children_->begin();
942 it != clip_children_->end(); ++it) {
943 DCHECK_EQ((*it)->clip_parent(), this);
944 LayerImpl* clip_child = layer->layer_tree_impl()->LayerById((*it)->id());
945 DCHECK(clip_child);
946 clip_children->insert(clip_child);
948 layer->SetClipChildren(clip_children);
949 } else {
950 layer->SetClipChildren(NULL);
953 // Adjust the scroll delta to be just the scrolls that have happened since
954 // the BeginMainFrame was sent. This happens for impl-side painting
955 // in LayerImpl::ApplyScrollDeltasSinceBeginMainFrame in a separate tree walk.
956 if (layer->layer_tree_impl()->settings().impl_side_painting) {
957 layer->SetScrollOffset(scroll_offset_);
958 } else {
959 layer->SetScrollOffsetAndDelta(
960 scroll_offset_, layer->ScrollDelta() - layer->sent_scroll_delta());
961 layer->SetSentScrollDelta(gfx::Vector2d());
964 // Wrap the copy_requests_ in a PostTask to the main thread.
965 ScopedPtrVector<CopyOutputRequest> main_thread_copy_requests;
966 for (ScopedPtrVector<CopyOutputRequest>::iterator it = copy_requests_.begin();
967 it != copy_requests_.end();
968 ++it) {
969 scoped_refptr<base::SingleThreadTaskRunner> main_thread_task_runner =
970 layer_tree_host()->proxy()->MainThreadTaskRunner();
971 scoped_ptr<CopyOutputRequest> original_request = copy_requests_.take(it);
972 const CopyOutputRequest& original_request_ref = *original_request;
973 scoped_ptr<CopyOutputRequest> main_thread_request =
974 CopyOutputRequest::CreateRelayRequest(
975 original_request_ref,
976 base::Bind(&PostCopyCallbackToMainThread,
977 main_thread_task_runner,
978 base::Passed(&original_request)));
979 main_thread_copy_requests.push_back(main_thread_request.Pass());
981 copy_requests_.clear();
982 layer->PassCopyRequests(&main_thread_copy_requests);
984 // If the main thread commits multiple times before the impl thread actually
985 // draws, then damage tracking will become incorrect if we simply clobber the
986 // update_rect here. The LayerImpl's update_rect needs to accumulate (i.e.
987 // union) any update changes that have occurred on the main thread.
988 update_rect_.Union(layer->update_rect());
989 layer->SetUpdateRect(update_rect_);
991 layer->SetStackingOrderChanged(stacking_order_changed_);
993 layer_animation_controller_->PushAnimationUpdatesTo(
994 layer->layer_animation_controller());
996 // Reset any state that should be cleared for the next update.
997 stacking_order_changed_ = false;
998 update_rect_ = gfx::RectF();
1000 needs_push_properties_ = false;
1001 num_dependents_need_push_properties_ = 0;
1004 scoped_ptr<LayerImpl> Layer::CreateLayerImpl(LayerTreeImpl* tree_impl) {
1005 return LayerImpl::Create(tree_impl, layer_id_);
1008 bool Layer::DrawsContent() const {
1009 return is_drawable_;
1012 void Layer::SavePaintProperties() {
1013 DCHECK(layer_tree_host_);
1015 // TODO(reveman): Save all layer properties that we depend on not
1016 // changing until PushProperties() has been called. crbug.com/231016
1017 paint_properties_.bounds = bounds_;
1018 paint_properties_.source_frame_number =
1019 layer_tree_host_->source_frame_number();
1022 bool Layer::Update(ResourceUpdateQueue* queue,
1023 const OcclusionTracker<Layer>* occlusion) {
1024 DCHECK(layer_tree_host_);
1025 DCHECK_EQ(layer_tree_host_->source_frame_number(),
1026 paint_properties_.source_frame_number) <<
1027 "SavePaintProperties must be called for any layer that is painted.";
1028 return false;
1031 bool Layer::NeedMoreUpdates() {
1032 return false;
1035 bool Layer::IsSuitableForGpuRasterization() const {
1036 return true;
1039 scoped_refptr<base::debug::ConvertableToTraceFormat> Layer::TakeDebugInfo() {
1040 if (client_)
1041 return client_->TakeDebugInfo();
1042 else
1043 return NULL;
1046 void Layer::CreateRenderSurface() {
1047 DCHECK(!draw_properties_.render_surface);
1048 draw_properties_.render_surface = make_scoped_ptr(new RenderSurface(this));
1049 draw_properties_.render_target = this;
1052 void Layer::ClearRenderSurface() {
1053 draw_properties_.render_surface.reset();
1056 void Layer::ClearRenderSurfaceLayerList() {
1057 if (draw_properties_.render_surface)
1058 draw_properties_.render_surface->layer_list().clear();
1061 gfx::Vector2dF Layer::ScrollOffsetForAnimation() const {
1062 return TotalScrollOffset();
1065 // On<Property>Animated is called due to an ongoing accelerated animation.
1066 // Since this animation is also being run on the compositor thread, there
1067 // is no need to request a commit to push this value over, so the value is
1068 // set directly rather than by calling Set<Property>.
1069 void Layer::OnFilterAnimated(const FilterOperations& filters) {
1070 filters_ = filters;
1073 void Layer::OnOpacityAnimated(float opacity) {
1074 opacity_ = opacity;
1077 void Layer::OnTransformAnimated(const gfx::Transform& transform) {
1078 if (transform_ == transform)
1079 return;
1080 transform_ = transform;
1081 transform_is_invertible_ = transform.IsInvertible();
1084 void Layer::OnScrollOffsetAnimated(const gfx::Vector2dF& scroll_offset) {
1085 // Do nothing. Scroll deltas will be sent from the compositor thread back
1086 // to the main thread in the same manner as during non-animated
1087 // compositor-driven scrolling.
1090 void Layer::OnAnimationWaitingForDeletion() {
1091 // Animations are only deleted during PushProperties.
1092 SetNeedsPushProperties();
1095 bool Layer::IsActive() const {
1096 return true;
1099 bool Layer::AddAnimation(scoped_ptr <Animation> animation) {
1100 if (!layer_animation_controller_->animation_registrar())
1101 return false;
1103 if (animation->target_property() == Animation::ScrollOffset &&
1104 !layer_animation_controller_->animation_registrar()
1105 ->supports_scroll_animations())
1106 return false;
1108 UMA_HISTOGRAM_BOOLEAN("Renderer.AnimationAddedToOrphanLayer",
1109 !layer_tree_host_);
1110 layer_animation_controller_->AddAnimation(animation.Pass());
1111 SetNeedsCommit();
1112 return true;
1115 void Layer::PauseAnimation(int animation_id, double time_offset) {
1116 layer_animation_controller_->PauseAnimation(
1117 animation_id, base::TimeDelta::FromSecondsD(time_offset));
1118 SetNeedsCommit();
1121 void Layer::RemoveAnimation(int animation_id) {
1122 layer_animation_controller_->RemoveAnimation(animation_id);
1123 SetNeedsCommit();
1126 void Layer::SetLayerAnimationControllerForTest(
1127 scoped_refptr<LayerAnimationController> controller) {
1128 layer_animation_controller_->RemoveValueObserver(this);
1129 layer_animation_controller_ = controller;
1130 layer_animation_controller_->AddValueObserver(this);
1131 SetNeedsCommit();
1134 bool Layer::HasActiveAnimation() const {
1135 return layer_animation_controller_->HasActiveAnimation();
1138 void Layer::AddLayerAnimationEventObserver(
1139 LayerAnimationEventObserver* animation_observer) {
1140 layer_animation_controller_->AddEventObserver(animation_observer);
1143 void Layer::RemoveLayerAnimationEventObserver(
1144 LayerAnimationEventObserver* animation_observer) {
1145 layer_animation_controller_->RemoveEventObserver(animation_observer);
1148 Region Layer::VisibleContentOpaqueRegion() const {
1149 if (contents_opaque())
1150 return visible_content_rect();
1151 return Region();
1154 ScrollbarLayerInterface* Layer::ToScrollbarLayer() {
1155 return NULL;
1158 RenderingStatsInstrumentation* Layer::rendering_stats_instrumentation() const {
1159 return layer_tree_host_->rendering_stats_instrumentation();
1162 bool Layer::SupportsLCDText() const {
1163 return false;
1166 void Layer::RemoveFromScrollTree() {
1167 if (scroll_children_.get()) {
1168 for (std::set<Layer*>::iterator it = scroll_children_->begin();
1169 it != scroll_children_->end(); ++it)
1170 (*it)->scroll_parent_ = NULL;
1173 if (scroll_parent_)
1174 scroll_parent_->RemoveScrollChild(this);
1176 scroll_parent_ = NULL;
1179 void Layer::RemoveFromClipTree() {
1180 if (clip_children_.get()) {
1181 for (std::set<Layer*>::iterator it = clip_children_->begin();
1182 it != clip_children_->end(); ++it)
1183 (*it)->clip_parent_ = NULL;
1186 if (clip_parent_)
1187 clip_parent_->RemoveClipChild(this);
1189 clip_parent_ = NULL;
1192 void Layer::RunMicroBenchmark(MicroBenchmark* benchmark) {
1193 benchmark->RunOnLayer(this);
1195 } // namespace cc