Delete chrome.mediaGalleriesPrivate because the functionality unique to it has since...
[chromium-blink-merge.git] / cc / layers / layer.cc
blob45e8a83bffd7e02df055d7aaa70f4c8c25f1846a
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/base/simple_enclosed_region.h"
21 #include "cc/layers/layer_client.h"
22 #include "cc/layers/layer_impl.h"
23 #include "cc/layers/scrollbar_layer_interface.h"
24 #include "cc/output/copy_output_request.h"
25 #include "cc/output/copy_output_result.h"
26 #include "cc/trees/layer_tree_host.h"
27 #include "cc/trees/layer_tree_impl.h"
28 #include "third_party/skia/include/core/SkImageFilter.h"
29 #include "ui/gfx/geometry/rect_conversions.h"
30 #include "ui/gfx/geometry/vector2d_conversions.h"
32 namespace cc {
34 base::StaticAtomicSequenceNumber g_next_layer_id;
36 scoped_refptr<Layer> Layer::Create() {
37 return make_scoped_refptr(new Layer());
40 Layer::Layer()
41 : needs_push_properties_(false),
42 num_dependents_need_push_properties_(false),
43 stacking_order_changed_(false),
44 // Layer IDs start from 1.
45 layer_id_(g_next_layer_id.GetNext() + 1),
46 ignore_set_needs_commit_(false),
47 sorting_context_id_(0),
48 parent_(nullptr),
49 layer_tree_host_(nullptr),
50 scroll_clip_layer_id_(INVALID_ID),
51 num_descendants_that_draw_content_(0),
52 transform_tree_index_(-1),
53 opacity_tree_index_(-1),
54 clip_tree_index_(-1),
55 should_scroll_on_main_thread_(false),
56 have_wheel_event_handlers_(false),
57 have_scroll_event_handlers_(false),
58 user_scrollable_horizontal_(true),
59 user_scrollable_vertical_(true),
60 is_root_for_isolated_group_(false),
61 is_container_for_fixed_position_layers_(false),
62 is_drawable_(false),
63 draws_content_(false),
64 hide_layer_and_subtree_(false),
65 masks_to_bounds_(false),
66 contents_opaque_(false),
67 double_sided_(true),
68 should_flatten_transform_(true),
69 use_parent_backface_visibility_(false),
70 draw_checkerboard_for_missing_tiles_(false),
71 force_render_surface_(false),
72 transform_is_invertible_(true),
73 has_render_surface_(false),
74 background_color_(0),
75 opacity_(1.f),
76 blend_mode_(SkXfermode::kSrcOver_Mode),
77 scroll_parent_(nullptr),
78 clip_parent_(nullptr),
79 replica_layer_(nullptr),
80 raster_scale_(0.f),
81 client_(nullptr) {
82 layer_animation_controller_ = LayerAnimationController::Create(layer_id_);
83 layer_animation_controller_->AddValueObserver(this);
84 layer_animation_controller_->set_value_provider(this);
87 Layer::~Layer() {
88 // Our parent should be holding a reference to us so there should be no
89 // way for us to be destroyed while we still have a parent.
90 DCHECK(!parent());
91 // Similarly we shouldn't have a layer tree host since it also keeps a
92 // reference to us.
93 DCHECK(!layer_tree_host());
95 layer_animation_controller_->RemoveValueObserver(this);
96 layer_animation_controller_->remove_value_provider(this);
98 RemoveFromScrollTree();
99 RemoveFromClipTree();
101 // Remove the parent reference from all children and dependents.
102 RemoveAllChildren();
103 if (mask_layer_.get()) {
104 DCHECK_EQ(this, mask_layer_->parent());
105 mask_layer_->RemoveFromParent();
107 if (replica_layer_.get()) {
108 DCHECK_EQ(this, replica_layer_->parent());
109 replica_layer_->RemoveFromParent();
113 void Layer::SetLayerTreeHost(LayerTreeHost* host) {
114 if (layer_tree_host_ == host)
115 return;
117 layer_tree_host_ = host;
119 // When changing hosts, the layer needs to commit its properties to the impl
120 // side for the new host.
121 SetNeedsPushProperties();
123 for (size_t i = 0; i < children_.size(); ++i)
124 children_[i]->SetLayerTreeHost(host);
126 if (mask_layer_.get())
127 mask_layer_->SetLayerTreeHost(host);
128 if (replica_layer_.get())
129 replica_layer_->SetLayerTreeHost(host);
131 if (host) {
132 layer_animation_controller_->SetAnimationRegistrar(
133 host->animation_registrar());
135 if (host->settings().layer_transforms_should_scale_layer_contents)
136 reset_raster_scale_to_unknown();
139 if (host && layer_animation_controller_->has_any_animation())
140 host->SetNeedsCommit();
143 void Layer::SetNeedsUpdate() {
144 if (layer_tree_host_ && !ignore_set_needs_commit_)
145 layer_tree_host_->SetNeedsUpdateLayers();
148 void Layer::SetNeedsCommit() {
149 if (!layer_tree_host_)
150 return;
152 SetNeedsPushProperties();
154 if (ignore_set_needs_commit_)
155 return;
157 layer_tree_host_->SetNeedsCommit();
160 void Layer::SetNeedsFullTreeSync() {
161 if (!layer_tree_host_)
162 return;
164 layer_tree_host_->SetNeedsFullTreeSync();
167 void Layer::SetNextCommitWaitsForActivation() {
168 if (!layer_tree_host_)
169 return;
171 layer_tree_host_->SetNextCommitWaitsForActivation();
174 void Layer::SetNeedsPushProperties() {
175 if (needs_push_properties_)
176 return;
177 if (!parent_should_know_need_push_properties() && parent_)
178 parent_->AddDependentNeedsPushProperties();
179 needs_push_properties_ = true;
182 void Layer::AddDependentNeedsPushProperties() {
183 DCHECK_GE(num_dependents_need_push_properties_, 0);
185 if (!parent_should_know_need_push_properties() && parent_)
186 parent_->AddDependentNeedsPushProperties();
188 num_dependents_need_push_properties_++;
191 void Layer::RemoveDependentNeedsPushProperties() {
192 num_dependents_need_push_properties_--;
193 DCHECK_GE(num_dependents_need_push_properties_, 0);
195 if (!parent_should_know_need_push_properties() && parent_)
196 parent_->RemoveDependentNeedsPushProperties();
199 bool Layer::IsPropertyChangeAllowed() const {
200 if (!layer_tree_host_)
201 return true;
203 if (!layer_tree_host_->settings().strict_layer_property_change_checking)
204 return true;
206 return !layer_tree_host_->in_paint_layer_contents();
209 gfx::Rect Layer::LayerRectToContentRect(const gfx::Rect& layer_rect) const {
210 gfx::Rect content_rect = gfx::ScaleToEnclosingRect(
211 layer_rect, contents_scale_x(), contents_scale_y());
212 // Intersect with content rect to avoid the extra pixel because for some
213 // values x and y, ceil((x / y) * y) may be x + 1.
214 content_rect.Intersect(gfx::Rect(content_bounds()));
215 return content_rect;
218 skia::RefPtr<SkPicture> Layer::GetPicture() const {
219 return skia::RefPtr<SkPicture>();
222 void Layer::SetParent(Layer* layer) {
223 DCHECK(!layer || !layer->HasAncestor(this));
225 if (parent_should_know_need_push_properties()) {
226 if (parent_)
227 parent_->RemoveDependentNeedsPushProperties();
228 if (layer)
229 layer->AddDependentNeedsPushProperties();
232 parent_ = layer;
233 SetLayerTreeHost(parent_ ? parent_->layer_tree_host() : nullptr);
235 if (!layer_tree_host_)
236 return;
237 const LayerTreeSettings& settings = layer_tree_host_->settings();
238 if (!settings.layer_transforms_should_scale_layer_contents)
239 return;
241 reset_raster_scale_to_unknown();
242 if (mask_layer_.get())
243 mask_layer_->reset_raster_scale_to_unknown();
244 if (replica_layer_.get() && replica_layer_->mask_layer_.get())
245 replica_layer_->mask_layer_->reset_raster_scale_to_unknown();
248 void Layer::AddChild(scoped_refptr<Layer> child) {
249 InsertChild(child, children_.size());
252 void Layer::InsertChild(scoped_refptr<Layer> child, size_t index) {
253 DCHECK(IsPropertyChangeAllowed());
254 child->RemoveFromParent();
255 AddDrawableDescendants(child->NumDescendantsThatDrawContent() +
256 (child->DrawsContent() ? 1 : 0));
257 child->SetParent(this);
258 child->stacking_order_changed_ = true;
260 index = std::min(index, children_.size());
261 children_.insert(children_.begin() + index, child);
262 SetNeedsFullTreeSync();
265 void Layer::RemoveFromParent() {
266 DCHECK(IsPropertyChangeAllowed());
267 if (parent_)
268 parent_->RemoveChildOrDependent(this);
271 void Layer::RemoveChildOrDependent(Layer* child) {
272 if (mask_layer_.get() == child) {
273 mask_layer_->SetParent(nullptr);
274 mask_layer_ = nullptr;
275 SetNeedsFullTreeSync();
276 return;
278 if (replica_layer_.get() == child) {
279 replica_layer_->SetParent(nullptr);
280 replica_layer_ = nullptr;
281 SetNeedsFullTreeSync();
282 return;
285 for (LayerList::iterator iter = children_.begin();
286 iter != children_.end();
287 ++iter) {
288 if (iter->get() != child)
289 continue;
291 child->SetParent(nullptr);
292 AddDrawableDescendants(-child->NumDescendantsThatDrawContent() -
293 (child->DrawsContent() ? 1 : 0));
294 children_.erase(iter);
295 SetNeedsFullTreeSync();
296 return;
300 void Layer::ReplaceChild(Layer* reference, scoped_refptr<Layer> new_layer) {
301 DCHECK(reference);
302 DCHECK_EQ(reference->parent(), this);
303 DCHECK(IsPropertyChangeAllowed());
305 if (reference == new_layer.get())
306 return;
308 int reference_index = IndexOfChild(reference);
309 if (reference_index == -1) {
310 NOTREACHED();
311 return;
314 reference->RemoveFromParent();
316 if (new_layer.get()) {
317 new_layer->RemoveFromParent();
318 InsertChild(new_layer, reference_index);
322 int Layer::IndexOfChild(const Layer* reference) {
323 for (size_t i = 0; i < children_.size(); ++i) {
324 if (children_[i].get() == reference)
325 return i;
327 return -1;
330 void Layer::SetBounds(const gfx::Size& size) {
331 DCHECK(IsPropertyChangeAllowed());
332 if (bounds() == size)
333 return;
335 bounds_ = size;
336 SetNeedsCommit();
339 Layer* Layer::RootLayer() {
340 Layer* layer = this;
341 while (layer->parent())
342 layer = layer->parent();
343 return layer;
346 void Layer::RemoveAllChildren() {
347 DCHECK(IsPropertyChangeAllowed());
348 while (children_.size()) {
349 Layer* layer = children_[0].get();
350 DCHECK_EQ(this, layer->parent());
351 layer->RemoveFromParent();
355 void Layer::SetChildren(const LayerList& children) {
356 DCHECK(IsPropertyChangeAllowed());
357 if (children == children_)
358 return;
360 RemoveAllChildren();
361 for (size_t i = 0; i < children.size(); ++i)
362 AddChild(children[i]);
365 bool Layer::HasAncestor(const Layer* ancestor) const {
366 for (const Layer* layer = parent(); layer; layer = layer->parent()) {
367 if (layer == ancestor)
368 return true;
370 return false;
373 void Layer::RequestCopyOfOutput(
374 scoped_ptr<CopyOutputRequest> request) {
375 DCHECK(IsPropertyChangeAllowed());
376 if (request->IsEmpty())
377 return;
378 copy_requests_.push_back(request.Pass());
379 SetNeedsCommit();
382 void Layer::SetBackgroundColor(SkColor background_color) {
383 DCHECK(IsPropertyChangeAllowed());
384 if (background_color_ == background_color)
385 return;
386 background_color_ = background_color;
387 SetNeedsCommit();
390 SkColor Layer::SafeOpaqueBackgroundColor() const {
391 SkColor color = background_color();
392 if (SkColorGetA(color) == 255 && !contents_opaque()) {
393 color = SK_ColorTRANSPARENT;
394 } else if (SkColorGetA(color) != 255 && contents_opaque()) {
395 for (const Layer* layer = parent(); layer;
396 layer = layer->parent()) {
397 color = layer->background_color();
398 if (SkColorGetA(color) == 255)
399 break;
401 if (SkColorGetA(color) != 255)
402 color = layer_tree_host_->background_color();
403 if (SkColorGetA(color) != 255)
404 color = SkColorSetA(color, 255);
406 return color;
409 void Layer::CalculateContentsScale(float ideal_contents_scale,
410 float* contents_scale_x,
411 float* contents_scale_y,
412 gfx::Size* content_bounds) {
413 DCHECK(layer_tree_host_);
415 *contents_scale_x = 1;
416 *contents_scale_y = 1;
417 *content_bounds = bounds();
420 void Layer::SetMasksToBounds(bool masks_to_bounds) {
421 DCHECK(IsPropertyChangeAllowed());
422 if (masks_to_bounds_ == masks_to_bounds)
423 return;
424 masks_to_bounds_ = masks_to_bounds;
425 SetNeedsCommit();
428 void Layer::SetMaskLayer(Layer* mask_layer) {
429 DCHECK(IsPropertyChangeAllowed());
430 if (mask_layer_.get() == mask_layer)
431 return;
432 if (mask_layer_.get()) {
433 DCHECK_EQ(this, mask_layer_->parent());
434 mask_layer_->RemoveFromParent();
436 mask_layer_ = mask_layer;
437 if (mask_layer_.get()) {
438 DCHECK(!mask_layer_->parent());
439 mask_layer_->RemoveFromParent();
440 mask_layer_->SetParent(this);
441 mask_layer_->SetIsMask(true);
443 SetNeedsFullTreeSync();
446 void Layer::SetReplicaLayer(Layer* layer) {
447 DCHECK(IsPropertyChangeAllowed());
448 if (replica_layer_.get() == layer)
449 return;
450 if (replica_layer_.get()) {
451 DCHECK_EQ(this, replica_layer_->parent());
452 replica_layer_->RemoveFromParent();
454 replica_layer_ = layer;
455 if (replica_layer_.get()) {
456 DCHECK(!replica_layer_->parent());
457 replica_layer_->RemoveFromParent();
458 replica_layer_->SetParent(this);
460 SetNeedsFullTreeSync();
463 void Layer::SetFilters(const FilterOperations& filters) {
464 DCHECK(IsPropertyChangeAllowed());
465 if (filters_ == filters)
466 return;
467 filters_ = filters;
468 SetNeedsCommit();
471 bool Layer::FilterIsAnimating() const {
472 return layer_animation_controller_->IsAnimatingProperty(Animation::Filter);
475 void Layer::SetBackgroundFilters(const FilterOperations& filters) {
476 DCHECK(IsPropertyChangeAllowed());
477 if (background_filters_ == filters)
478 return;
479 background_filters_ = filters;
480 SetNeedsCommit();
483 void Layer::SetOpacity(float opacity) {
484 DCHECK(IsPropertyChangeAllowed());
485 if (opacity_ == opacity)
486 return;
487 opacity_ = opacity;
488 SetNeedsCommit();
491 bool Layer::OpacityIsAnimating() const {
492 return layer_animation_controller_->IsAnimatingProperty(Animation::Opacity);
495 bool Layer::OpacityCanAnimateOnImplThread() const {
496 return false;
499 void Layer::SetBlendMode(SkXfermode::Mode blend_mode) {
500 DCHECK(IsPropertyChangeAllowed());
501 if (blend_mode_ == blend_mode)
502 return;
504 // Allowing only blend modes that are defined in the CSS Compositing standard:
505 // http://dev.w3.org/fxtf/compositing-1/#blending
506 switch (blend_mode) {
507 case SkXfermode::kSrcOver_Mode:
508 case SkXfermode::kScreen_Mode:
509 case SkXfermode::kOverlay_Mode:
510 case SkXfermode::kDarken_Mode:
511 case SkXfermode::kLighten_Mode:
512 case SkXfermode::kColorDodge_Mode:
513 case SkXfermode::kColorBurn_Mode:
514 case SkXfermode::kHardLight_Mode:
515 case SkXfermode::kSoftLight_Mode:
516 case SkXfermode::kDifference_Mode:
517 case SkXfermode::kExclusion_Mode:
518 case SkXfermode::kMultiply_Mode:
519 case SkXfermode::kHue_Mode:
520 case SkXfermode::kSaturation_Mode:
521 case SkXfermode::kColor_Mode:
522 case SkXfermode::kLuminosity_Mode:
523 // supported blend modes
524 break;
525 case SkXfermode::kClear_Mode:
526 case SkXfermode::kSrc_Mode:
527 case SkXfermode::kDst_Mode:
528 case SkXfermode::kDstOver_Mode:
529 case SkXfermode::kSrcIn_Mode:
530 case SkXfermode::kDstIn_Mode:
531 case SkXfermode::kSrcOut_Mode:
532 case SkXfermode::kDstOut_Mode:
533 case SkXfermode::kSrcATop_Mode:
534 case SkXfermode::kDstATop_Mode:
535 case SkXfermode::kXor_Mode:
536 case SkXfermode::kPlus_Mode:
537 case SkXfermode::kModulate_Mode:
538 // Porter Duff Compositing Operators are not yet supported
539 // http://dev.w3.org/fxtf/compositing-1/#porterduffcompositingoperators
540 NOTREACHED();
541 return;
544 blend_mode_ = blend_mode;
545 SetNeedsCommit();
548 void Layer::SetIsRootForIsolatedGroup(bool root) {
549 DCHECK(IsPropertyChangeAllowed());
550 if (is_root_for_isolated_group_ == root)
551 return;
552 is_root_for_isolated_group_ = root;
553 SetNeedsCommit();
556 void Layer::SetContentsOpaque(bool opaque) {
557 DCHECK(IsPropertyChangeAllowed());
558 if (contents_opaque_ == opaque)
559 return;
560 contents_opaque_ = opaque;
561 SetNeedsCommit();
564 void Layer::SetPosition(const gfx::PointF& position) {
565 DCHECK(IsPropertyChangeAllowed());
566 if (position_ == position)
567 return;
568 position_ = position;
569 SetNeedsCommit();
572 bool Layer::IsContainerForFixedPositionLayers() const {
573 if (!transform_.IsIdentityOrTranslation())
574 return true;
575 if (parent_ && !parent_->transform_.IsIdentityOrTranslation())
576 return true;
577 return is_container_for_fixed_position_layers_;
580 void Layer::SetTransform(const gfx::Transform& transform) {
581 DCHECK(IsPropertyChangeAllowed());
582 if (transform_ == transform)
583 return;
584 transform_ = transform;
585 transform_is_invertible_ = transform.IsInvertible();
586 SetNeedsCommit();
589 void Layer::SetTransformOrigin(const gfx::Point3F& transform_origin) {
590 DCHECK(IsPropertyChangeAllowed());
591 if (transform_origin_ == transform_origin)
592 return;
593 transform_origin_ = transform_origin;
594 SetNeedsCommit();
597 bool Layer::AnimationsPreserveAxisAlignment() const {
598 return layer_animation_controller_->AnimationsPreserveAxisAlignment();
601 bool Layer::TransformIsAnimating() const {
602 return layer_animation_controller_->IsAnimatingProperty(Animation::Transform);
605 void Layer::SetScrollParent(Layer* parent) {
606 DCHECK(IsPropertyChangeAllowed());
607 if (scroll_parent_ == parent)
608 return;
610 if (scroll_parent_)
611 scroll_parent_->RemoveScrollChild(this);
613 scroll_parent_ = parent;
615 if (scroll_parent_)
616 scroll_parent_->AddScrollChild(this);
618 SetNeedsCommit();
621 void Layer::AddScrollChild(Layer* child) {
622 if (!scroll_children_)
623 scroll_children_.reset(new std::set<Layer*>);
624 scroll_children_->insert(child);
625 SetNeedsCommit();
628 void Layer::RemoveScrollChild(Layer* child) {
629 scroll_children_->erase(child);
630 if (scroll_children_->empty())
631 scroll_children_ = nullptr;
632 SetNeedsCommit();
635 void Layer::SetClipParent(Layer* ancestor) {
636 DCHECK(IsPropertyChangeAllowed());
637 if (clip_parent_ == ancestor)
638 return;
640 if (clip_parent_)
641 clip_parent_->RemoveClipChild(this);
643 clip_parent_ = ancestor;
645 if (clip_parent_)
646 clip_parent_->AddClipChild(this);
648 SetNeedsCommit();
651 void Layer::AddClipChild(Layer* child) {
652 if (!clip_children_)
653 clip_children_.reset(new std::set<Layer*>);
654 clip_children_->insert(child);
655 SetNeedsCommit();
658 void Layer::RemoveClipChild(Layer* child) {
659 clip_children_->erase(child);
660 if (clip_children_->empty())
661 clip_children_ = nullptr;
662 SetNeedsCommit();
665 void Layer::SetScrollOffset(const gfx::ScrollOffset& scroll_offset) {
666 DCHECK(IsPropertyChangeAllowed());
668 if (scroll_offset_ == scroll_offset)
669 return;
670 scroll_offset_ = scroll_offset;
671 SetNeedsCommit();
674 void Layer::SetScrollOffsetFromImplSide(
675 const gfx::ScrollOffset& scroll_offset) {
676 DCHECK(IsPropertyChangeAllowed());
677 // This function only gets called during a BeginMainFrame, so there
678 // is no need to call SetNeedsUpdate here.
679 DCHECK(layer_tree_host_ && layer_tree_host_->CommitRequested());
680 if (scroll_offset_ == scroll_offset)
681 return;
682 scroll_offset_ = scroll_offset;
683 SetNeedsPushProperties();
684 if (!did_scroll_callback_.is_null())
685 did_scroll_callback_.Run();
686 // The callback could potentially change the layer structure:
687 // "this" may have been destroyed during the process.
690 void Layer::SetScrollClipLayerId(int clip_layer_id) {
691 DCHECK(IsPropertyChangeAllowed());
692 if (scroll_clip_layer_id_ == clip_layer_id)
693 return;
694 scroll_clip_layer_id_ = clip_layer_id;
695 SetNeedsCommit();
698 void Layer::SetUserScrollable(bool horizontal, bool vertical) {
699 DCHECK(IsPropertyChangeAllowed());
700 if (user_scrollable_horizontal_ == horizontal &&
701 user_scrollable_vertical_ == vertical)
702 return;
703 user_scrollable_horizontal_ = horizontal;
704 user_scrollable_vertical_ = vertical;
705 SetNeedsCommit();
708 void Layer::SetShouldScrollOnMainThread(bool should_scroll_on_main_thread) {
709 DCHECK(IsPropertyChangeAllowed());
710 if (should_scroll_on_main_thread_ == should_scroll_on_main_thread)
711 return;
712 should_scroll_on_main_thread_ = should_scroll_on_main_thread;
713 SetNeedsCommit();
716 void Layer::SetHaveWheelEventHandlers(bool have_wheel_event_handlers) {
717 DCHECK(IsPropertyChangeAllowed());
718 if (have_wheel_event_handlers_ == have_wheel_event_handlers)
719 return;
720 have_wheel_event_handlers_ = have_wheel_event_handlers;
721 SetNeedsCommit();
724 void Layer::SetHaveScrollEventHandlers(bool have_scroll_event_handlers) {
725 DCHECK(IsPropertyChangeAllowed());
726 if (have_scroll_event_handlers_ == have_scroll_event_handlers)
727 return;
728 have_scroll_event_handlers_ = have_scroll_event_handlers;
729 SetNeedsCommit();
732 void Layer::SetNonFastScrollableRegion(const Region& region) {
733 DCHECK(IsPropertyChangeAllowed());
734 if (non_fast_scrollable_region_ == region)
735 return;
736 non_fast_scrollable_region_ = region;
737 SetNeedsCommit();
740 void Layer::SetTouchEventHandlerRegion(const Region& region) {
741 DCHECK(IsPropertyChangeAllowed());
742 if (touch_event_handler_region_ == region)
743 return;
744 touch_event_handler_region_ = region;
745 SetNeedsCommit();
748 void Layer::SetDrawCheckerboardForMissingTiles(bool checkerboard) {
749 DCHECK(IsPropertyChangeAllowed());
750 if (draw_checkerboard_for_missing_tiles_ == checkerboard)
751 return;
752 draw_checkerboard_for_missing_tiles_ = checkerboard;
753 SetNeedsCommit();
756 void Layer::SetForceRenderSurface(bool force) {
757 DCHECK(IsPropertyChangeAllowed());
758 if (force_render_surface_ == force)
759 return;
760 force_render_surface_ = force;
761 SetNeedsCommit();
764 void Layer::SetDoubleSided(bool double_sided) {
765 DCHECK(IsPropertyChangeAllowed());
766 if (double_sided_ == double_sided)
767 return;
768 double_sided_ = double_sided;
769 SetNeedsCommit();
772 void Layer::Set3dSortingContextId(int id) {
773 DCHECK(IsPropertyChangeAllowed());
774 if (id == sorting_context_id_)
775 return;
776 sorting_context_id_ = id;
777 SetNeedsCommit();
780 void Layer::SetShouldFlattenTransform(bool should_flatten) {
781 DCHECK(IsPropertyChangeAllowed());
782 if (should_flatten_transform_ == should_flatten)
783 return;
784 should_flatten_transform_ = should_flatten;
785 SetNeedsCommit();
788 void Layer::SetIsDrawable(bool is_drawable) {
789 DCHECK(IsPropertyChangeAllowed());
790 if (is_drawable_ == is_drawable)
791 return;
793 is_drawable_ = is_drawable;
794 UpdateDrawsContent(HasDrawableContent());
797 void Layer::SetHideLayerAndSubtree(bool hide) {
798 DCHECK(IsPropertyChangeAllowed());
799 if (hide_layer_and_subtree_ == hide)
800 return;
802 hide_layer_and_subtree_ = hide;
803 SetNeedsCommit();
806 void Layer::SetNeedsDisplayRect(const gfx::Rect& dirty_rect) {
807 if (dirty_rect.IsEmpty())
808 return;
810 SetNeedsPushProperties();
811 update_rect_.Union(dirty_rect);
813 if (DrawsContent())
814 SetNeedsUpdate();
817 bool Layer::DescendantIsFixedToContainerLayer() const {
818 for (size_t i = 0; i < children_.size(); ++i) {
819 if (children_[i]->position_constraint_.is_fixed_position() ||
820 children_[i]->DescendantIsFixedToContainerLayer())
821 return true;
823 return false;
826 void Layer::SetIsContainerForFixedPositionLayers(bool container) {
827 if (is_container_for_fixed_position_layers_ == container)
828 return;
829 is_container_for_fixed_position_layers_ = container;
831 if (layer_tree_host_ && layer_tree_host_->CommitRequested())
832 return;
834 // Only request a commit if we have a fixed positioned descendant.
835 if (DescendantIsFixedToContainerLayer())
836 SetNeedsCommit();
839 void Layer::SetPositionConstraint(const LayerPositionConstraint& constraint) {
840 DCHECK(IsPropertyChangeAllowed());
841 if (position_constraint_ == constraint)
842 return;
843 position_constraint_ = constraint;
844 SetNeedsCommit();
847 static void RunCopyCallbackOnMainThread(scoped_ptr<CopyOutputRequest> request,
848 scoped_ptr<CopyOutputResult> result) {
849 request->SendResult(result.Pass());
852 static void PostCopyCallbackToMainThread(
853 scoped_refptr<base::SingleThreadTaskRunner> main_thread_task_runner,
854 scoped_ptr<CopyOutputRequest> request,
855 scoped_ptr<CopyOutputResult> result) {
856 main_thread_task_runner->PostTask(FROM_HERE,
857 base::Bind(&RunCopyCallbackOnMainThread,
858 base::Passed(&request),
859 base::Passed(&result)));
862 void Layer::PushPropertiesTo(LayerImpl* layer) {
863 DCHECK(layer_tree_host_);
865 // If we did not SavePaintProperties() for the layer this frame, then push the
866 // real property values, not the paint property values.
867 bool use_paint_properties = paint_properties_.source_frame_number ==
868 layer_tree_host_->source_frame_number();
870 layer->SetTransformOrigin(transform_origin_);
871 layer->SetBackgroundColor(background_color_);
872 layer->SetBounds(use_paint_properties ? paint_properties_.bounds
873 : bounds_);
874 layer->SetContentBounds(content_bounds());
875 layer->SetContentsScale(contents_scale_x(), contents_scale_y());
877 bool is_tracing;
878 TRACE_EVENT_CATEGORY_GROUP_ENABLED(
879 TRACE_DISABLED_BY_DEFAULT("cc.debug") "," TRACE_DISABLED_BY_DEFAULT(
880 "devtools.timeline.layers"),
881 &is_tracing);
882 if (is_tracing)
883 layer->SetDebugInfo(TakeDebugInfo());
885 layer->SetDoubleSided(double_sided_);
886 layer->SetDrawCheckerboardForMissingTiles(
887 draw_checkerboard_for_missing_tiles_);
888 layer->SetDrawsContent(DrawsContent());
889 layer->SetHideLayerAndSubtree(hide_layer_and_subtree_);
890 layer->SetHasRenderSurface(has_render_surface_);
891 if (!layer->FilterIsAnimatingOnImplOnly() && !FilterIsAnimating())
892 layer->SetFilters(filters_);
893 DCHECK(!(FilterIsAnimating() && layer->FilterIsAnimatingOnImplOnly()));
894 layer->SetBackgroundFilters(background_filters());
895 layer->SetMasksToBounds(masks_to_bounds_);
896 layer->SetShouldScrollOnMainThread(should_scroll_on_main_thread_);
897 layer->SetHaveWheelEventHandlers(have_wheel_event_handlers_);
898 layer->SetHaveScrollEventHandlers(have_scroll_event_handlers_);
899 layer->SetNonFastScrollableRegion(non_fast_scrollable_region_);
900 layer->SetTouchEventHandlerRegion(touch_event_handler_region_);
901 layer->SetContentsOpaque(contents_opaque_);
902 if (!layer->OpacityIsAnimatingOnImplOnly() && !OpacityIsAnimating())
903 layer->SetOpacity(opacity_);
904 DCHECK(!(OpacityIsAnimating() && layer->OpacityIsAnimatingOnImplOnly()));
905 layer->SetBlendMode(blend_mode_);
906 layer->SetIsRootForIsolatedGroup(is_root_for_isolated_group_);
907 layer->SetPosition(position_);
908 layer->SetIsContainerForFixedPositionLayers(
909 IsContainerForFixedPositionLayers());
910 layer->SetPositionConstraint(position_constraint_);
911 layer->SetShouldFlattenTransform(should_flatten_transform_);
912 layer->SetUseParentBackfaceVisibility(use_parent_backface_visibility_);
913 if (!layer->TransformIsAnimatingOnImplOnly() && !TransformIsAnimating())
914 layer->SetTransformAndInvertibility(transform_, transform_is_invertible_);
915 DCHECK(!(TransformIsAnimating() && layer->TransformIsAnimatingOnImplOnly()));
916 layer->Set3dSortingContextId(sorting_context_id_);
917 layer->SetNumDescendantsThatDrawContent(num_descendants_that_draw_content_);
919 layer->SetScrollClipLayer(scroll_clip_layer_id_);
920 layer->set_user_scrollable_horizontal(user_scrollable_horizontal_);
921 layer->set_user_scrollable_vertical(user_scrollable_vertical_);
923 LayerImpl* scroll_parent = nullptr;
924 if (scroll_parent_) {
925 scroll_parent = layer->layer_tree_impl()->LayerById(scroll_parent_->id());
926 DCHECK(scroll_parent);
929 layer->SetScrollParent(scroll_parent);
930 if (scroll_children_) {
931 std::set<LayerImpl*>* scroll_children = new std::set<LayerImpl*>;
932 for (std::set<Layer*>::iterator it = scroll_children_->begin();
933 it != scroll_children_->end();
934 ++it) {
935 DCHECK_EQ((*it)->scroll_parent(), this);
936 LayerImpl* scroll_child =
937 layer->layer_tree_impl()->LayerById((*it)->id());
938 DCHECK(scroll_child);
939 scroll_children->insert(scroll_child);
941 layer->SetScrollChildren(scroll_children);
942 } else {
943 layer->SetScrollChildren(nullptr);
946 LayerImpl* clip_parent = nullptr;
947 if (clip_parent_) {
948 clip_parent =
949 layer->layer_tree_impl()->LayerById(clip_parent_->id());
950 DCHECK(clip_parent);
953 layer->SetClipParent(clip_parent);
954 if (clip_children_) {
955 std::set<LayerImpl*>* clip_children = new std::set<LayerImpl*>;
956 for (std::set<Layer*>::iterator it = clip_children_->begin();
957 it != clip_children_->end(); ++it) {
958 DCHECK_EQ((*it)->clip_parent(), this);
959 LayerImpl* clip_child = layer->layer_tree_impl()->LayerById((*it)->id());
960 DCHECK(clip_child);
961 clip_children->insert(clip_child);
963 layer->SetClipChildren(clip_children);
964 } else {
965 layer->SetClipChildren(nullptr);
968 // Adjust the scroll delta to be just the scrolls that have happened since
969 // the BeginMainFrame was sent. This happens for impl-side painting
970 // in LayerImpl::ApplyScrollDeltasSinceBeginMainFrame in a separate tree walk.
971 if (layer->layer_tree_impl()->settings().impl_side_painting) {
972 layer->SetScrollOffset(scroll_offset_);
973 } else {
974 if (layer_animation_controller_
975 ->scroll_offset_animation_was_interrupted()) {
976 layer->SetScrollOffsetAndDelta(scroll_offset_, gfx::Vector2dF());
977 } else {
978 layer->SetScrollOffsetAndDelta(
979 scroll_offset_, layer->ScrollDelta() - layer->sent_scroll_delta());
981 layer->SetSentScrollDelta(gfx::Vector2dF());
984 // Wrap the copy_requests_ in a PostTask to the main thread.
985 ScopedPtrVector<CopyOutputRequest> main_thread_copy_requests;
986 for (ScopedPtrVector<CopyOutputRequest>::iterator it = copy_requests_.begin();
987 it != copy_requests_.end();
988 ++it) {
989 scoped_refptr<base::SingleThreadTaskRunner> main_thread_task_runner =
990 layer_tree_host()->proxy()->MainThreadTaskRunner();
991 scoped_ptr<CopyOutputRequest> original_request = copy_requests_.take(it);
992 const CopyOutputRequest& original_request_ref = *original_request;
993 scoped_ptr<CopyOutputRequest> main_thread_request =
994 CopyOutputRequest::CreateRelayRequest(
995 original_request_ref,
996 base::Bind(&PostCopyCallbackToMainThread,
997 main_thread_task_runner,
998 base::Passed(&original_request)));
999 main_thread_copy_requests.push_back(main_thread_request.Pass());
1001 copy_requests_.clear();
1002 layer->PassCopyRequests(&main_thread_copy_requests);
1004 // If the main thread commits multiple times before the impl thread actually
1005 // draws, then damage tracking will become incorrect if we simply clobber the
1006 // update_rect here. The LayerImpl's update_rect needs to accumulate (i.e.
1007 // union) any update changes that have occurred on the main thread.
1008 update_rect_.Union(layer->update_rect());
1009 layer->SetUpdateRect(update_rect_);
1011 layer->SetStackingOrderChanged(stacking_order_changed_);
1013 layer_animation_controller_->PushAnimationUpdatesTo(
1014 layer->layer_animation_controller());
1016 // Reset any state that should be cleared for the next update.
1017 stacking_order_changed_ = false;
1018 update_rect_ = gfx::Rect();
1020 needs_push_properties_ = false;
1021 num_dependents_need_push_properties_ = 0;
1024 scoped_ptr<LayerImpl> Layer::CreateLayerImpl(LayerTreeImpl* tree_impl) {
1025 return LayerImpl::Create(tree_impl, layer_id_);
1028 bool Layer::DrawsContent() const {
1029 return draws_content_;
1032 bool Layer::HasDrawableContent() const {
1033 return is_drawable_;
1036 void Layer::UpdateDrawsContent(bool has_drawable_content) {
1037 bool draws_content = has_drawable_content;
1038 DCHECK(is_drawable_ || !has_drawable_content);
1039 if (draws_content == draws_content_)
1040 return;
1042 if (HasDelegatedContent()) {
1043 // Layers with delegated content need to be treated as if they have as
1044 // many children as the number of layers they own delegated quads for.
1045 // Since we don't know this number right now, we choose one that acts like
1046 // infinity for our purposes.
1047 AddDrawableDescendants(draws_content ? 1000 : -1000);
1050 if (parent())
1051 parent()->AddDrawableDescendants(draws_content ? 1 : -1);
1053 draws_content_ = draws_content;
1054 SetNeedsCommit();
1057 int Layer::NumDescendantsThatDrawContent() const {
1058 return num_descendants_that_draw_content_;
1061 void Layer::SavePaintProperties() {
1062 DCHECK(layer_tree_host_);
1064 // TODO(reveman): Save all layer properties that we depend on not
1065 // changing until PushProperties() has been called. crbug.com/231016
1066 paint_properties_.bounds = bounds_;
1067 paint_properties_.source_frame_number =
1068 layer_tree_host_->source_frame_number();
1071 bool Layer::Update(ResourceUpdateQueue* queue,
1072 const OcclusionTracker<Layer>* occlusion) {
1073 DCHECK(layer_tree_host_);
1074 DCHECK_EQ(layer_tree_host_->source_frame_number(),
1075 paint_properties_.source_frame_number) <<
1076 "SavePaintProperties must be called for any layer that is painted.";
1077 return false;
1080 bool Layer::NeedMoreUpdates() {
1081 return false;
1084 bool Layer::IsSuitableForGpuRasterization() const {
1085 return true;
1088 scoped_refptr<base::debug::ConvertableToTraceFormat> Layer::TakeDebugInfo() {
1089 if (client_)
1090 return client_->TakeDebugInfo();
1091 else
1092 return nullptr;
1095 void Layer::SetHasRenderSurface(bool has_render_surface) {
1096 if (has_render_surface_ == has_render_surface)
1097 return;
1098 has_render_surface_ = has_render_surface;
1099 // We do not need SetNeedsCommit here, since this is only ever called
1100 // during a commit, from CalculateDrawProperties.
1101 SetNeedsPushProperties();
1104 void Layer::CreateRenderSurface() {
1105 DCHECK(!render_surface_);
1106 render_surface_ = make_scoped_ptr(new RenderSurface(this));
1109 void Layer::ClearRenderSurface() {
1110 render_surface_ = nullptr;
1113 void Layer::ClearRenderSurfaceLayerList() {
1114 if (render_surface_)
1115 render_surface_->ClearLayerLists();
1118 gfx::ScrollOffset Layer::ScrollOffsetForAnimation() const {
1119 return TotalScrollOffset();
1122 // On<Property>Animated is called due to an ongoing accelerated animation.
1123 // Since this animation is also being run on the compositor thread, there
1124 // is no need to request a commit to push this value over, so the value is
1125 // set directly rather than by calling Set<Property>.
1126 void Layer::OnFilterAnimated(const FilterOperations& filters) {
1127 filters_ = filters;
1130 void Layer::OnOpacityAnimated(float opacity) {
1131 opacity_ = opacity;
1134 void Layer::OnTransformAnimated(const gfx::Transform& transform) {
1135 if (transform_ == transform)
1136 return;
1137 transform_ = transform;
1138 transform_is_invertible_ = transform.IsInvertible();
1141 void Layer::OnScrollOffsetAnimated(const gfx::ScrollOffset& scroll_offset) {
1142 // Do nothing. Scroll deltas will be sent from the compositor thread back
1143 // to the main thread in the same manner as during non-animated
1144 // compositor-driven scrolling.
1147 void Layer::OnAnimationWaitingForDeletion() {
1148 // Animations are only deleted during PushProperties.
1149 SetNeedsPushProperties();
1152 bool Layer::IsActive() const {
1153 return true;
1156 bool Layer::AddAnimation(scoped_ptr <Animation> animation) {
1157 if (!layer_animation_controller_->animation_registrar())
1158 return false;
1160 if (animation->target_property() == Animation::ScrollOffset &&
1161 !layer_animation_controller_->animation_registrar()
1162 ->supports_scroll_animations())
1163 return false;
1165 UMA_HISTOGRAM_BOOLEAN("Renderer.AnimationAddedToOrphanLayer",
1166 !layer_tree_host_);
1167 layer_animation_controller_->AddAnimation(animation.Pass());
1168 SetNeedsCommit();
1169 return true;
1172 void Layer::PauseAnimation(int animation_id, double time_offset) {
1173 layer_animation_controller_->PauseAnimation(
1174 animation_id, base::TimeDelta::FromSecondsD(time_offset));
1175 SetNeedsCommit();
1178 void Layer::RemoveAnimation(int animation_id) {
1179 layer_animation_controller_->RemoveAnimation(animation_id);
1180 SetNeedsCommit();
1183 void Layer::SetLayerAnimationControllerForTest(
1184 scoped_refptr<LayerAnimationController> controller) {
1185 layer_animation_controller_->RemoveValueObserver(this);
1186 layer_animation_controller_ = controller;
1187 layer_animation_controller_->AddValueObserver(this);
1188 SetNeedsCommit();
1191 bool Layer::HasActiveAnimation() const {
1192 return layer_animation_controller_->HasActiveAnimation();
1195 void Layer::AddLayerAnimationEventObserver(
1196 LayerAnimationEventObserver* animation_observer) {
1197 layer_animation_controller_->AddEventObserver(animation_observer);
1200 void Layer::RemoveLayerAnimationEventObserver(
1201 LayerAnimationEventObserver* animation_observer) {
1202 layer_animation_controller_->RemoveEventObserver(animation_observer);
1205 SimpleEnclosedRegion Layer::VisibleContentOpaqueRegion() const {
1206 if (contents_opaque())
1207 return SimpleEnclosedRegion(visible_content_rect());
1208 return SimpleEnclosedRegion();
1211 ScrollbarLayerInterface* Layer::ToScrollbarLayer() {
1212 return nullptr;
1215 RenderingStatsInstrumentation* Layer::rendering_stats_instrumentation() const {
1216 return layer_tree_host_->rendering_stats_instrumentation();
1219 bool Layer::SupportsLCDText() const {
1220 return false;
1223 void Layer::RemoveFromScrollTree() {
1224 if (scroll_children_.get()) {
1225 std::set<Layer*> copy = *scroll_children_;
1226 for (std::set<Layer*>::iterator it = copy.begin(); it != copy.end(); ++it)
1227 (*it)->SetScrollParent(nullptr);
1230 DCHECK(!scroll_children_);
1231 SetScrollParent(nullptr);
1234 void Layer::RemoveFromClipTree() {
1235 if (clip_children_.get()) {
1236 std::set<Layer*> copy = *clip_children_;
1237 for (std::set<Layer*>::iterator it = copy.begin(); it != copy.end(); ++it)
1238 (*it)->SetClipParent(nullptr);
1241 DCHECK(!clip_children_);
1242 SetClipParent(nullptr);
1245 void Layer::AddDrawableDescendants(int num) {
1246 DCHECK_GE(num_descendants_that_draw_content_, 0);
1247 DCHECK_GE(num_descendants_that_draw_content_ + num, 0);
1248 if (num == 0)
1249 return;
1250 num_descendants_that_draw_content_ += num;
1251 SetNeedsCommit();
1252 if (parent())
1253 parent()->AddDrawableDescendants(num);
1256 void Layer::RunMicroBenchmark(MicroBenchmark* benchmark) {
1257 benchmark->RunOnLayer(this);
1260 bool Layer::HasDelegatedContent() const {
1261 return false;
1264 gfx::Transform Layer::screen_space_transform_from_property_trees(
1265 const TransformTree& tree) const {
1266 gfx::Transform xform(1, 0, 0, 1, offset_to_transform_parent().x(),
1267 offset_to_transform_parent().y());
1268 if (transform_tree_index() >= 0) {
1269 gfx::Transform ssxform = tree.Node(transform_tree_index())->data.to_screen;
1270 xform.ConcatTransform(ssxform);
1272 xform.Scale(1.0 / contents_scale_x(), 1.0 / contents_scale_y());
1273 return xform;
1276 gfx::Transform Layer::draw_transform_from_property_trees(
1277 const TransformTree& tree) const {
1278 gfx::Transform xform(1, 0, 0, 1, offset_to_transform_parent().x(),
1279 offset_to_transform_parent().y());
1280 if (transform_tree_index() >= 0) {
1281 const TransformNode* node = tree.Node(transform_tree_index());
1282 gfx::Transform ssxform;
1283 tree.ComputeTransform(node->id, node->data.target_id, &ssxform);
1284 xform.ConcatTransform(ssxform);
1286 xform.Scale(1.0 / contents_scale_x(), 1.0 / contents_scale_y());
1287 return xform;
1290 } // namespace cc