Rewrite AndroidSyncSettings to be significantly simpler.
[chromium-blink-merge.git] / cc / layers / layer.cc
blobbf2f26a4c4b5d06c36ceb10766a86219a2dccee0
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/location.h"
11 #include "base/metrics/histogram.h"
12 #include "base/single_thread_task_runner.h"
13 #include "base/time/time.h"
14 #include "base/trace_event/trace_event.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 scroll_blocks_on_(SCROLL_BLOCKS_ON_NONE),
75 background_color_(0),
76 opacity_(1.f),
77 blend_mode_(SkXfermode::kSrcOver_Mode),
78 scroll_parent_(nullptr),
79 clip_parent_(nullptr),
80 replica_layer_(nullptr),
81 raster_scale_(0.f),
82 client_(nullptr),
83 frame_timing_requests_dirty_(false) {
84 layer_animation_controller_ = LayerAnimationController::Create(layer_id_);
85 layer_animation_controller_->AddValueObserver(this);
86 layer_animation_controller_->set_value_provider(this);
89 Layer::~Layer() {
90 // Our parent should be holding a reference to us so there should be no
91 // way for us to be destroyed while we still have a parent.
92 DCHECK(!parent());
93 // Similarly we shouldn't have a layer tree host since it also keeps a
94 // reference to us.
95 DCHECK(!layer_tree_host());
97 layer_animation_controller_->RemoveValueObserver(this);
98 layer_animation_controller_->remove_value_provider(this);
100 RemoveFromScrollTree();
101 RemoveFromClipTree();
103 // Remove the parent reference from all children and dependents.
104 RemoveAllChildren();
105 if (mask_layer_.get()) {
106 DCHECK_EQ(this, mask_layer_->parent());
107 mask_layer_->RemoveFromParent();
109 if (replica_layer_.get()) {
110 DCHECK_EQ(this, replica_layer_->parent());
111 replica_layer_->RemoveFromParent();
115 void Layer::SetLayerTreeHost(LayerTreeHost* host) {
116 if (layer_tree_host_ == host)
117 return;
119 layer_tree_host_ = host;
121 // When changing hosts, the layer needs to commit its properties to the impl
122 // side for the new host.
123 SetNeedsPushProperties();
125 for (size_t i = 0; i < children_.size(); ++i)
126 children_[i]->SetLayerTreeHost(host);
128 if (mask_layer_.get())
129 mask_layer_->SetLayerTreeHost(host);
130 if (replica_layer_.get())
131 replica_layer_->SetLayerTreeHost(host);
133 if (host) {
134 layer_animation_controller_->SetAnimationRegistrar(
135 host->animation_registrar());
137 if (host->settings().layer_transforms_should_scale_layer_contents)
138 reset_raster_scale_to_unknown();
141 if (host && layer_animation_controller_->has_any_animation())
142 host->SetNeedsCommit();
145 void Layer::SetNeedsUpdate() {
146 if (layer_tree_host_ && !ignore_set_needs_commit_)
147 layer_tree_host_->SetNeedsUpdateLayers();
150 void Layer::SetNeedsCommit() {
151 if (!layer_tree_host_)
152 return;
154 SetNeedsPushProperties();
156 if (ignore_set_needs_commit_)
157 return;
159 layer_tree_host_->SetNeedsCommit();
162 void Layer::SetNeedsFullTreeSync() {
163 if (!layer_tree_host_)
164 return;
166 layer_tree_host_->SetNeedsFullTreeSync();
169 void Layer::SetNextCommitWaitsForActivation() {
170 if (!layer_tree_host_)
171 return;
173 layer_tree_host_->SetNextCommitWaitsForActivation();
176 void Layer::SetNeedsPushProperties() {
177 if (needs_push_properties_)
178 return;
179 if (!parent_should_know_need_push_properties() && parent_)
180 parent_->AddDependentNeedsPushProperties();
181 needs_push_properties_ = true;
184 void Layer::AddDependentNeedsPushProperties() {
185 DCHECK_GE(num_dependents_need_push_properties_, 0);
187 if (!parent_should_know_need_push_properties() && parent_)
188 parent_->AddDependentNeedsPushProperties();
190 num_dependents_need_push_properties_++;
193 void Layer::RemoveDependentNeedsPushProperties() {
194 num_dependents_need_push_properties_--;
195 DCHECK_GE(num_dependents_need_push_properties_, 0);
197 if (!parent_should_know_need_push_properties() && parent_)
198 parent_->RemoveDependentNeedsPushProperties();
201 bool Layer::IsPropertyChangeAllowed() const {
202 if (!layer_tree_host_)
203 return true;
205 if (!layer_tree_host_->settings().strict_layer_property_change_checking)
206 return true;
208 return !layer_tree_host_->in_paint_layer_contents();
211 gfx::Rect Layer::LayerRectToContentRect(const gfx::Rect& layer_rect) const {
212 gfx::Rect content_rect = gfx::ScaleToEnclosingRect(
213 layer_rect, contents_scale_x(), contents_scale_y());
214 // Intersect with content rect to avoid the extra pixel because for some
215 // values x and y, ceil((x / y) * y) may be x + 1.
216 content_rect.Intersect(gfx::Rect(content_bounds()));
217 return content_rect;
220 skia::RefPtr<SkPicture> Layer::GetPicture() const {
221 return skia::RefPtr<SkPicture>();
224 void Layer::SetParent(Layer* layer) {
225 DCHECK(!layer || !layer->HasAncestor(this));
227 if (parent_should_know_need_push_properties()) {
228 if (parent_)
229 parent_->RemoveDependentNeedsPushProperties();
230 if (layer)
231 layer->AddDependentNeedsPushProperties();
234 parent_ = layer;
235 SetLayerTreeHost(parent_ ? parent_->layer_tree_host() : nullptr);
237 if (!layer_tree_host_)
238 return;
239 const LayerTreeSettings& settings = layer_tree_host_->settings();
240 if (!settings.layer_transforms_should_scale_layer_contents)
241 return;
243 reset_raster_scale_to_unknown();
244 if (mask_layer_.get())
245 mask_layer_->reset_raster_scale_to_unknown();
246 if (replica_layer_.get() && replica_layer_->mask_layer_.get())
247 replica_layer_->mask_layer_->reset_raster_scale_to_unknown();
250 void Layer::AddChild(scoped_refptr<Layer> child) {
251 InsertChild(child, children_.size());
254 void Layer::InsertChild(scoped_refptr<Layer> child, size_t index) {
255 DCHECK(IsPropertyChangeAllowed());
256 child->RemoveFromParent();
257 AddDrawableDescendants(child->NumDescendantsThatDrawContent() +
258 (child->DrawsContent() ? 1 : 0));
259 child->SetParent(this);
260 child->stacking_order_changed_ = true;
262 index = std::min(index, children_.size());
263 children_.insert(children_.begin() + index, child);
264 SetNeedsFullTreeSync();
267 void Layer::RemoveFromParent() {
268 DCHECK(IsPropertyChangeAllowed());
269 if (parent_)
270 parent_->RemoveChildOrDependent(this);
273 void Layer::RemoveChildOrDependent(Layer* child) {
274 if (mask_layer_.get() == child) {
275 mask_layer_->SetParent(nullptr);
276 mask_layer_ = nullptr;
277 SetNeedsFullTreeSync();
278 return;
280 if (replica_layer_.get() == child) {
281 replica_layer_->SetParent(nullptr);
282 replica_layer_ = nullptr;
283 SetNeedsFullTreeSync();
284 return;
287 for (LayerList::iterator iter = children_.begin();
288 iter != children_.end();
289 ++iter) {
290 if (iter->get() != child)
291 continue;
293 child->SetParent(nullptr);
294 AddDrawableDescendants(-child->NumDescendantsThatDrawContent() -
295 (child->DrawsContent() ? 1 : 0));
296 children_.erase(iter);
297 SetNeedsFullTreeSync();
298 return;
302 void Layer::ReplaceChild(Layer* reference, scoped_refptr<Layer> new_layer) {
303 DCHECK(reference);
304 DCHECK_EQ(reference->parent(), this);
305 DCHECK(IsPropertyChangeAllowed());
307 if (reference == new_layer.get())
308 return;
310 int reference_index = IndexOfChild(reference);
311 if (reference_index == -1) {
312 NOTREACHED();
313 return;
316 reference->RemoveFromParent();
318 if (new_layer.get()) {
319 new_layer->RemoveFromParent();
320 InsertChild(new_layer, reference_index);
324 int Layer::IndexOfChild(const Layer* reference) {
325 for (size_t i = 0; i < children_.size(); ++i) {
326 if (children_[i].get() == reference)
327 return i;
329 return -1;
332 void Layer::SetBounds(const gfx::Size& size) {
333 DCHECK(IsPropertyChangeAllowed());
334 if (bounds() == size)
335 return;
337 bounds_ = size;
338 SetNeedsCommit();
341 Layer* Layer::RootLayer() {
342 Layer* layer = this;
343 while (layer->parent())
344 layer = layer->parent();
345 return layer;
348 void Layer::RemoveAllChildren() {
349 DCHECK(IsPropertyChangeAllowed());
350 while (children_.size()) {
351 Layer* layer = children_[0].get();
352 DCHECK_EQ(this, layer->parent());
353 layer->RemoveFromParent();
357 void Layer::SetChildren(const LayerList& children) {
358 DCHECK(IsPropertyChangeAllowed());
359 if (children == children_)
360 return;
362 RemoveAllChildren();
363 for (size_t i = 0; i < children.size(); ++i)
364 AddChild(children[i]);
367 bool Layer::HasAncestor(const Layer* ancestor) const {
368 for (const Layer* layer = parent(); layer; layer = layer->parent()) {
369 if (layer == ancestor)
370 return true;
372 return false;
375 void Layer::RequestCopyOfOutput(
376 scoped_ptr<CopyOutputRequest> request) {
377 DCHECK(IsPropertyChangeAllowed());
378 if (request->IsEmpty())
379 return;
380 copy_requests_.push_back(request.Pass());
381 SetNeedsCommit();
384 void Layer::SetBackgroundColor(SkColor background_color) {
385 DCHECK(IsPropertyChangeAllowed());
386 if (background_color_ == background_color)
387 return;
388 background_color_ = background_color;
389 SetNeedsCommit();
392 SkColor Layer::SafeOpaqueBackgroundColor() const {
393 SkColor color = background_color();
394 if (SkColorGetA(color) == 255 && !contents_opaque()) {
395 color = SK_ColorTRANSPARENT;
396 } else if (SkColorGetA(color) != 255 && contents_opaque()) {
397 for (const Layer* layer = parent(); layer;
398 layer = layer->parent()) {
399 color = layer->background_color();
400 if (SkColorGetA(color) == 255)
401 break;
403 if (SkColorGetA(color) != 255)
404 color = layer_tree_host_->background_color();
405 if (SkColorGetA(color) != 255)
406 color = SkColorSetA(color, 255);
408 return color;
411 void Layer::CalculateContentsScale(float ideal_contents_scale,
412 float* contents_scale_x,
413 float* contents_scale_y,
414 gfx::Size* content_bounds) {
415 DCHECK(layer_tree_host_);
417 *contents_scale_x = 1;
418 *contents_scale_y = 1;
419 *content_bounds = bounds();
422 void Layer::SetMasksToBounds(bool masks_to_bounds) {
423 DCHECK(IsPropertyChangeAllowed());
424 if (masks_to_bounds_ == masks_to_bounds)
425 return;
426 masks_to_bounds_ = masks_to_bounds;
427 SetNeedsCommit();
430 void Layer::SetMaskLayer(Layer* mask_layer) {
431 DCHECK(IsPropertyChangeAllowed());
432 if (mask_layer_.get() == mask_layer)
433 return;
434 if (mask_layer_.get()) {
435 DCHECK_EQ(this, mask_layer_->parent());
436 mask_layer_->RemoveFromParent();
438 mask_layer_ = mask_layer;
439 if (mask_layer_.get()) {
440 DCHECK(!mask_layer_->parent());
441 mask_layer_->RemoveFromParent();
442 mask_layer_->SetParent(this);
443 mask_layer_->SetIsMask(true);
445 SetNeedsFullTreeSync();
448 void Layer::SetReplicaLayer(Layer* layer) {
449 DCHECK(IsPropertyChangeAllowed());
450 if (replica_layer_.get() == layer)
451 return;
452 if (replica_layer_.get()) {
453 DCHECK_EQ(this, replica_layer_->parent());
454 replica_layer_->RemoveFromParent();
456 replica_layer_ = layer;
457 if (replica_layer_.get()) {
458 DCHECK(!replica_layer_->parent());
459 replica_layer_->RemoveFromParent();
460 replica_layer_->SetParent(this);
462 SetNeedsFullTreeSync();
465 void Layer::SetFilters(const FilterOperations& filters) {
466 DCHECK(IsPropertyChangeAllowed());
467 if (filters_ == filters)
468 return;
469 filters_ = filters;
470 SetNeedsCommit();
473 bool Layer::FilterIsAnimating() const {
474 return layer_animation_controller_->IsAnimatingProperty(Animation::FILTER);
477 void Layer::SetBackgroundFilters(const FilterOperations& filters) {
478 DCHECK(IsPropertyChangeAllowed());
479 if (background_filters_ == filters)
480 return;
481 background_filters_ = filters;
482 SetNeedsCommit();
485 void Layer::SetOpacity(float opacity) {
486 DCHECK(IsPropertyChangeAllowed());
487 if (opacity_ == opacity)
488 return;
489 opacity_ = opacity;
490 SetNeedsCommit();
493 bool Layer::OpacityIsAnimating() const {
494 return layer_animation_controller_->IsAnimatingProperty(Animation::OPACITY);
497 bool Layer::OpacityCanAnimateOnImplThread() const {
498 return false;
501 void Layer::SetBlendMode(SkXfermode::Mode blend_mode) {
502 DCHECK(IsPropertyChangeAllowed());
503 if (blend_mode_ == blend_mode)
504 return;
506 // Allowing only blend modes that are defined in the CSS Compositing standard:
507 // http://dev.w3.org/fxtf/compositing-1/#blending
508 switch (blend_mode) {
509 case SkXfermode::kSrcOver_Mode:
510 case SkXfermode::kScreen_Mode:
511 case SkXfermode::kOverlay_Mode:
512 case SkXfermode::kDarken_Mode:
513 case SkXfermode::kLighten_Mode:
514 case SkXfermode::kColorDodge_Mode:
515 case SkXfermode::kColorBurn_Mode:
516 case SkXfermode::kHardLight_Mode:
517 case SkXfermode::kSoftLight_Mode:
518 case SkXfermode::kDifference_Mode:
519 case SkXfermode::kExclusion_Mode:
520 case SkXfermode::kMultiply_Mode:
521 case SkXfermode::kHue_Mode:
522 case SkXfermode::kSaturation_Mode:
523 case SkXfermode::kColor_Mode:
524 case SkXfermode::kLuminosity_Mode:
525 // supported blend modes
526 break;
527 case SkXfermode::kClear_Mode:
528 case SkXfermode::kSrc_Mode:
529 case SkXfermode::kDst_Mode:
530 case SkXfermode::kDstOver_Mode:
531 case SkXfermode::kSrcIn_Mode:
532 case SkXfermode::kDstIn_Mode:
533 case SkXfermode::kSrcOut_Mode:
534 case SkXfermode::kDstOut_Mode:
535 case SkXfermode::kSrcATop_Mode:
536 case SkXfermode::kDstATop_Mode:
537 case SkXfermode::kXor_Mode:
538 case SkXfermode::kPlus_Mode:
539 case SkXfermode::kModulate_Mode:
540 // Porter Duff Compositing Operators are not yet supported
541 // http://dev.w3.org/fxtf/compositing-1/#porterduffcompositingoperators
542 NOTREACHED();
543 return;
546 blend_mode_ = blend_mode;
547 SetNeedsCommit();
550 void Layer::SetIsRootForIsolatedGroup(bool root) {
551 DCHECK(IsPropertyChangeAllowed());
552 if (is_root_for_isolated_group_ == root)
553 return;
554 is_root_for_isolated_group_ = root;
555 SetNeedsCommit();
558 void Layer::SetContentsOpaque(bool opaque) {
559 DCHECK(IsPropertyChangeAllowed());
560 if (contents_opaque_ == opaque)
561 return;
562 contents_opaque_ = opaque;
563 SetNeedsCommit();
566 void Layer::SetPosition(const gfx::PointF& position) {
567 DCHECK(IsPropertyChangeAllowed());
568 if (position_ == position)
569 return;
570 position_ = position;
571 SetNeedsCommit();
574 bool Layer::IsContainerForFixedPositionLayers() const {
575 if (!transform_.IsIdentityOrTranslation())
576 return true;
577 if (parent_ && !parent_->transform_.IsIdentityOrTranslation())
578 return true;
579 return is_container_for_fixed_position_layers_;
582 void Layer::SetTransform(const gfx::Transform& transform) {
583 DCHECK(IsPropertyChangeAllowed());
584 if (transform_ == transform)
585 return;
586 transform_ = transform;
587 transform_is_invertible_ = transform.IsInvertible();
588 SetNeedsCommit();
591 void Layer::SetTransformOrigin(const gfx::Point3F& transform_origin) {
592 DCHECK(IsPropertyChangeAllowed());
593 if (transform_origin_ == transform_origin)
594 return;
595 transform_origin_ = transform_origin;
596 SetNeedsCommit();
599 bool Layer::AnimationsPreserveAxisAlignment() const {
600 return layer_animation_controller_->AnimationsPreserveAxisAlignment();
603 bool Layer::TransformIsAnimating() const {
604 return layer_animation_controller_->IsAnimatingProperty(Animation::TRANSFORM);
607 void Layer::SetScrollParent(Layer* parent) {
608 DCHECK(IsPropertyChangeAllowed());
609 if (scroll_parent_ == parent)
610 return;
612 if (scroll_parent_)
613 scroll_parent_->RemoveScrollChild(this);
615 scroll_parent_ = parent;
617 if (scroll_parent_)
618 scroll_parent_->AddScrollChild(this);
620 SetNeedsCommit();
623 void Layer::AddScrollChild(Layer* child) {
624 if (!scroll_children_)
625 scroll_children_.reset(new std::set<Layer*>);
626 scroll_children_->insert(child);
627 SetNeedsCommit();
630 void Layer::RemoveScrollChild(Layer* child) {
631 scroll_children_->erase(child);
632 if (scroll_children_->empty())
633 scroll_children_ = nullptr;
634 SetNeedsCommit();
637 void Layer::SetClipParent(Layer* ancestor) {
638 DCHECK(IsPropertyChangeAllowed());
639 if (clip_parent_ == ancestor)
640 return;
642 if (clip_parent_)
643 clip_parent_->RemoveClipChild(this);
645 clip_parent_ = ancestor;
647 if (clip_parent_)
648 clip_parent_->AddClipChild(this);
650 SetNeedsCommit();
653 void Layer::AddClipChild(Layer* child) {
654 if (!clip_children_)
655 clip_children_.reset(new std::set<Layer*>);
656 clip_children_->insert(child);
657 SetNeedsCommit();
660 void Layer::RemoveClipChild(Layer* child) {
661 clip_children_->erase(child);
662 if (clip_children_->empty())
663 clip_children_ = nullptr;
664 SetNeedsCommit();
667 void Layer::SetScrollOffset(const gfx::ScrollOffset& scroll_offset) {
668 DCHECK(IsPropertyChangeAllowed());
670 if (scroll_offset_ == scroll_offset)
671 return;
672 scroll_offset_ = scroll_offset;
673 SetNeedsCommit();
676 void Layer::SetScrollCompensationAdjustment(
677 const gfx::Vector2dF& scroll_compensation_adjustment) {
678 if (scroll_compensation_adjustment_ == scroll_compensation_adjustment)
679 return;
680 scroll_compensation_adjustment_ = scroll_compensation_adjustment;
681 SetNeedsCommit();
684 gfx::Vector2dF Layer::ScrollCompensationAdjustment() const {
685 return scroll_compensation_adjustment_;
688 void Layer::SetScrollOffsetFromImplSide(
689 const gfx::ScrollOffset& scroll_offset) {
690 DCHECK(IsPropertyChangeAllowed());
691 // This function only gets called during a BeginMainFrame, so there
692 // is no need to call SetNeedsUpdate here.
693 DCHECK(layer_tree_host_ && layer_tree_host_->CommitRequested());
694 if (scroll_offset_ == scroll_offset)
695 return;
696 scroll_offset_ = scroll_offset;
697 SetNeedsPushProperties();
698 if (!did_scroll_callback_.is_null())
699 did_scroll_callback_.Run();
700 // The callback could potentially change the layer structure:
701 // "this" may have been destroyed during the process.
704 void Layer::SetScrollClipLayerId(int clip_layer_id) {
705 DCHECK(IsPropertyChangeAllowed());
706 if (scroll_clip_layer_id_ == clip_layer_id)
707 return;
708 scroll_clip_layer_id_ = clip_layer_id;
709 SetNeedsCommit();
712 void Layer::SetUserScrollable(bool horizontal, bool vertical) {
713 DCHECK(IsPropertyChangeAllowed());
714 if (user_scrollable_horizontal_ == horizontal &&
715 user_scrollable_vertical_ == vertical)
716 return;
717 user_scrollable_horizontal_ = horizontal;
718 user_scrollable_vertical_ = vertical;
719 SetNeedsCommit();
722 void Layer::SetShouldScrollOnMainThread(bool should_scroll_on_main_thread) {
723 DCHECK(IsPropertyChangeAllowed());
724 if (should_scroll_on_main_thread_ == should_scroll_on_main_thread)
725 return;
726 should_scroll_on_main_thread_ = should_scroll_on_main_thread;
727 SetNeedsCommit();
730 void Layer::SetHaveWheelEventHandlers(bool have_wheel_event_handlers) {
731 DCHECK(IsPropertyChangeAllowed());
732 if (have_wheel_event_handlers_ == have_wheel_event_handlers)
733 return;
734 have_wheel_event_handlers_ = have_wheel_event_handlers;
735 SetNeedsCommit();
738 void Layer::SetHaveScrollEventHandlers(bool have_scroll_event_handlers) {
739 DCHECK(IsPropertyChangeAllowed());
740 if (have_scroll_event_handlers_ == have_scroll_event_handlers)
741 return;
742 have_scroll_event_handlers_ = have_scroll_event_handlers;
743 SetNeedsCommit();
746 void Layer::SetNonFastScrollableRegion(const Region& region) {
747 DCHECK(IsPropertyChangeAllowed());
748 if (non_fast_scrollable_region_ == region)
749 return;
750 non_fast_scrollable_region_ = region;
751 SetNeedsCommit();
754 void Layer::SetTouchEventHandlerRegion(const Region& region) {
755 DCHECK(IsPropertyChangeAllowed());
756 if (touch_event_handler_region_ == region)
757 return;
758 touch_event_handler_region_ = region;
759 SetNeedsCommit();
762 void Layer::SetScrollBlocksOn(ScrollBlocksOn scroll_blocks_on) {
763 DCHECK(IsPropertyChangeAllowed());
764 if (scroll_blocks_on_ == scroll_blocks_on)
765 return;
766 scroll_blocks_on_ = scroll_blocks_on;
767 SetNeedsCommit();
770 void Layer::SetDrawCheckerboardForMissingTiles(bool checkerboard) {
771 DCHECK(IsPropertyChangeAllowed());
772 if (draw_checkerboard_for_missing_tiles_ == checkerboard)
773 return;
774 draw_checkerboard_for_missing_tiles_ = checkerboard;
775 SetNeedsCommit();
778 void Layer::SetForceRenderSurface(bool force) {
779 DCHECK(IsPropertyChangeAllowed());
780 if (force_render_surface_ == force)
781 return;
782 force_render_surface_ = force;
783 SetNeedsCommit();
786 void Layer::SetDoubleSided(bool double_sided) {
787 DCHECK(IsPropertyChangeAllowed());
788 if (double_sided_ == double_sided)
789 return;
790 double_sided_ = double_sided;
791 SetNeedsCommit();
794 void Layer::Set3dSortingContextId(int id) {
795 DCHECK(IsPropertyChangeAllowed());
796 if (id == sorting_context_id_)
797 return;
798 sorting_context_id_ = id;
799 SetNeedsCommit();
802 void Layer::SetShouldFlattenTransform(bool should_flatten) {
803 DCHECK(IsPropertyChangeAllowed());
804 if (should_flatten_transform_ == should_flatten)
805 return;
806 should_flatten_transform_ = should_flatten;
807 SetNeedsCommit();
810 void Layer::SetIsDrawable(bool is_drawable) {
811 DCHECK(IsPropertyChangeAllowed());
812 if (is_drawable_ == is_drawable)
813 return;
815 is_drawable_ = is_drawable;
816 UpdateDrawsContent(HasDrawableContent());
819 void Layer::SetHideLayerAndSubtree(bool hide) {
820 DCHECK(IsPropertyChangeAllowed());
821 if (hide_layer_and_subtree_ == hide)
822 return;
824 hide_layer_and_subtree_ = hide;
825 SetNeedsCommit();
828 void Layer::SetNeedsDisplayRect(const gfx::Rect& dirty_rect) {
829 if (dirty_rect.IsEmpty())
830 return;
832 SetNeedsPushProperties();
833 update_rect_.Union(dirty_rect);
835 if (DrawsContent())
836 SetNeedsUpdate();
839 bool Layer::DescendantIsFixedToContainerLayer() const {
840 for (size_t i = 0; i < children_.size(); ++i) {
841 if (children_[i]->position_constraint_.is_fixed_position() ||
842 children_[i]->DescendantIsFixedToContainerLayer())
843 return true;
845 return false;
848 void Layer::SetIsContainerForFixedPositionLayers(bool container) {
849 if (is_container_for_fixed_position_layers_ == container)
850 return;
851 is_container_for_fixed_position_layers_ = container;
853 if (layer_tree_host_ && layer_tree_host_->CommitRequested())
854 return;
856 // Only request a commit if we have a fixed positioned descendant.
857 if (DescendantIsFixedToContainerLayer())
858 SetNeedsCommit();
861 void Layer::SetPositionConstraint(const LayerPositionConstraint& constraint) {
862 DCHECK(IsPropertyChangeAllowed());
863 if (position_constraint_ == constraint)
864 return;
865 position_constraint_ = constraint;
866 SetNeedsCommit();
869 static void RunCopyCallbackOnMainThread(scoped_ptr<CopyOutputRequest> request,
870 scoped_ptr<CopyOutputResult> result) {
871 request->SendResult(result.Pass());
874 static void PostCopyCallbackToMainThread(
875 scoped_refptr<base::SingleThreadTaskRunner> main_thread_task_runner,
876 scoped_ptr<CopyOutputRequest> request,
877 scoped_ptr<CopyOutputResult> result) {
878 main_thread_task_runner->PostTask(FROM_HERE,
879 base::Bind(&RunCopyCallbackOnMainThread,
880 base::Passed(&request),
881 base::Passed(&result)));
884 void Layer::PushPropertiesTo(LayerImpl* layer) {
885 DCHECK(layer_tree_host_);
887 // If we did not SavePaintProperties() for the layer this frame, then push the
888 // real property values, not the paint property values.
889 bool use_paint_properties = paint_properties_.source_frame_number ==
890 layer_tree_host_->source_frame_number();
892 layer->SetTransformOrigin(transform_origin_);
893 layer->SetBackgroundColor(background_color_);
894 layer->SetBounds(use_paint_properties ? paint_properties_.bounds
895 : bounds_);
896 layer->SetContentBounds(content_bounds());
897 layer->SetContentsScale(contents_scale_x(), contents_scale_y());
899 bool is_tracing;
900 TRACE_EVENT_CATEGORY_GROUP_ENABLED(
901 TRACE_DISABLED_BY_DEFAULT("cc.debug") "," TRACE_DISABLED_BY_DEFAULT(
902 "devtools.timeline.layers"),
903 &is_tracing);
904 if (is_tracing)
905 layer->SetDebugInfo(TakeDebugInfo());
907 layer->SetDoubleSided(double_sided_);
908 layer->SetDrawCheckerboardForMissingTiles(
909 draw_checkerboard_for_missing_tiles_);
910 layer->SetDrawsContent(DrawsContent());
911 layer->SetHideLayerAndSubtree(hide_layer_and_subtree_);
912 layer->SetHasRenderSurface(has_render_surface_ || layer->HasCopyRequest());
913 if (!layer->FilterIsAnimatingOnImplOnly() && !FilterIsAnimating())
914 layer->SetFilters(filters_);
915 DCHECK(!(FilterIsAnimating() && layer->FilterIsAnimatingOnImplOnly()));
916 layer->SetBackgroundFilters(background_filters());
917 layer->SetMasksToBounds(masks_to_bounds_);
918 layer->SetShouldScrollOnMainThread(should_scroll_on_main_thread_);
919 layer->SetHaveWheelEventHandlers(have_wheel_event_handlers_);
920 layer->SetHaveScrollEventHandlers(have_scroll_event_handlers_);
921 layer->SetNonFastScrollableRegion(non_fast_scrollable_region_);
922 layer->SetTouchEventHandlerRegion(touch_event_handler_region_);
923 layer->SetScrollBlocksOn(scroll_blocks_on_);
924 layer->SetContentsOpaque(contents_opaque_);
925 if (!layer->OpacityIsAnimatingOnImplOnly() && !OpacityIsAnimating())
926 layer->SetOpacity(opacity_);
927 DCHECK(!(OpacityIsAnimating() && layer->OpacityIsAnimatingOnImplOnly()));
928 layer->SetBlendMode(blend_mode_);
929 layer->SetIsRootForIsolatedGroup(is_root_for_isolated_group_);
930 layer->SetPosition(position_);
931 layer->SetIsContainerForFixedPositionLayers(
932 IsContainerForFixedPositionLayers());
933 layer->SetPositionConstraint(position_constraint_);
934 layer->SetShouldFlattenTransform(should_flatten_transform_);
935 layer->SetUseParentBackfaceVisibility(use_parent_backface_visibility_);
936 if (!layer->TransformIsAnimatingOnImplOnly() && !TransformIsAnimating())
937 layer->SetTransformAndInvertibility(transform_, transform_is_invertible_);
938 DCHECK(!(TransformIsAnimating() && layer->TransformIsAnimatingOnImplOnly()));
939 layer->Set3dSortingContextId(sorting_context_id_);
940 layer->SetNumDescendantsThatDrawContent(num_descendants_that_draw_content_);
942 layer->SetScrollClipLayer(scroll_clip_layer_id_);
943 layer->set_user_scrollable_horizontal(user_scrollable_horizontal_);
944 layer->set_user_scrollable_vertical(user_scrollable_vertical_);
946 LayerImpl* scroll_parent = nullptr;
947 if (scroll_parent_) {
948 scroll_parent = layer->layer_tree_impl()->LayerById(scroll_parent_->id());
949 DCHECK(scroll_parent);
952 layer->SetScrollParent(scroll_parent);
953 if (scroll_children_) {
954 std::set<LayerImpl*>* scroll_children = new std::set<LayerImpl*>;
955 for (std::set<Layer*>::iterator it = scroll_children_->begin();
956 it != scroll_children_->end();
957 ++it) {
958 DCHECK_EQ((*it)->scroll_parent(), this);
959 LayerImpl* scroll_child =
960 layer->layer_tree_impl()->LayerById((*it)->id());
961 DCHECK(scroll_child);
962 scroll_children->insert(scroll_child);
964 layer->SetScrollChildren(scroll_children);
965 } else {
966 layer->SetScrollChildren(nullptr);
969 LayerImpl* clip_parent = nullptr;
970 if (clip_parent_) {
971 clip_parent =
972 layer->layer_tree_impl()->LayerById(clip_parent_->id());
973 DCHECK(clip_parent);
976 layer->SetClipParent(clip_parent);
977 if (clip_children_) {
978 std::set<LayerImpl*>* clip_children = new std::set<LayerImpl*>;
979 for (std::set<Layer*>::iterator it = clip_children_->begin();
980 it != clip_children_->end(); ++it) {
981 DCHECK_EQ((*it)->clip_parent(), this);
982 LayerImpl* clip_child = layer->layer_tree_impl()->LayerById((*it)->id());
983 DCHECK(clip_child);
984 clip_children->insert(clip_child);
986 layer->SetClipChildren(clip_children);
987 } else {
988 layer->SetClipChildren(nullptr);
991 // When a scroll offset animation is interrupted the new scroll position on
992 // the pending tree will clobber any impl-side scrolling occuring on the
993 // active tree. To do so, avoid scrolling the pending tree along with it
994 // instead of trying to undo that scrolling later.
995 if (layer_animation_controller_->scroll_offset_animation_was_interrupted())
996 layer->PushScrollOffsetFromMainThreadAndClobberActiveValue(scroll_offset_);
997 else
998 layer->PushScrollOffsetFromMainThread(scroll_offset_);
999 layer->SetScrollCompensationAdjustment(ScrollCompensationAdjustment());
1001 // Wrap the copy_requests_ in a PostTask to the main thread.
1002 ScopedPtrVector<CopyOutputRequest> main_thread_copy_requests;
1003 for (ScopedPtrVector<CopyOutputRequest>::iterator it = copy_requests_.begin();
1004 it != copy_requests_.end();
1005 ++it) {
1006 scoped_refptr<base::SingleThreadTaskRunner> main_thread_task_runner =
1007 layer_tree_host()->proxy()->MainThreadTaskRunner();
1008 scoped_ptr<CopyOutputRequest> original_request = copy_requests_.take(it);
1009 const CopyOutputRequest& original_request_ref = *original_request;
1010 scoped_ptr<CopyOutputRequest> main_thread_request =
1011 CopyOutputRequest::CreateRelayRequest(
1012 original_request_ref,
1013 base::Bind(&PostCopyCallbackToMainThread,
1014 main_thread_task_runner,
1015 base::Passed(&original_request)));
1016 main_thread_copy_requests.push_back(main_thread_request.Pass());
1018 copy_requests_.clear();
1019 layer->PassCopyRequests(&main_thread_copy_requests);
1021 // If the main thread commits multiple times before the impl thread actually
1022 // draws, then damage tracking will become incorrect if we simply clobber the
1023 // update_rect here. The LayerImpl's update_rect needs to accumulate (i.e.
1024 // union) any update changes that have occurred on the main thread.
1025 update_rect_.Union(layer->update_rect());
1026 layer->SetUpdateRect(update_rect_);
1028 layer->SetStackingOrderChanged(stacking_order_changed_);
1030 layer_animation_controller_->PushAnimationUpdatesTo(
1031 layer->layer_animation_controller());
1033 if (frame_timing_requests_dirty_) {
1034 layer->PassFrameTimingRequests(&frame_timing_requests_);
1035 frame_timing_requests_dirty_ = false;
1038 // Reset any state that should be cleared for the next update.
1039 stacking_order_changed_ = false;
1040 update_rect_ = gfx::Rect();
1042 needs_push_properties_ = false;
1043 num_dependents_need_push_properties_ = 0;
1046 scoped_ptr<LayerImpl> Layer::CreateLayerImpl(LayerTreeImpl* tree_impl) {
1047 return LayerImpl::Create(tree_impl, layer_id_,
1048 new LayerImpl::SyncedScrollOffset);
1051 bool Layer::DrawsContent() const {
1052 return draws_content_;
1055 bool Layer::HasDrawableContent() const {
1056 return is_drawable_;
1059 void Layer::UpdateDrawsContent(bool has_drawable_content) {
1060 bool draws_content = has_drawable_content;
1061 DCHECK(is_drawable_ || !has_drawable_content);
1062 if (draws_content == draws_content_)
1063 return;
1065 if (HasDelegatedContent()) {
1066 // Layers with delegated content need to be treated as if they have as
1067 // many children as the number of layers they own delegated quads for.
1068 // Since we don't know this number right now, we choose one that acts like
1069 // infinity for our purposes.
1070 AddDrawableDescendants(draws_content ? 1000 : -1000);
1073 if (parent())
1074 parent()->AddDrawableDescendants(draws_content ? 1 : -1);
1076 draws_content_ = draws_content;
1077 SetNeedsCommit();
1080 int Layer::NumDescendantsThatDrawContent() const {
1081 return num_descendants_that_draw_content_;
1084 void Layer::SavePaintProperties() {
1085 DCHECK(layer_tree_host_);
1087 // TODO(reveman): Save all layer properties that we depend on not
1088 // changing until PushProperties() has been called. crbug.com/231016
1089 paint_properties_.bounds = bounds_;
1090 paint_properties_.source_frame_number =
1091 layer_tree_host_->source_frame_number();
1094 bool Layer::Update(ResourceUpdateQueue* queue,
1095 const OcclusionTracker<Layer>* occlusion) {
1096 DCHECK(layer_tree_host_);
1097 DCHECK_EQ(layer_tree_host_->source_frame_number(),
1098 paint_properties_.source_frame_number) <<
1099 "SavePaintProperties must be called for any layer that is painted.";
1100 return false;
1103 bool Layer::NeedMoreUpdates() {
1104 return false;
1107 bool Layer::IsSuitableForGpuRasterization() const {
1108 return true;
1111 scoped_refptr<base::trace_event::ConvertableToTraceFormat>
1112 Layer::TakeDebugInfo() {
1113 if (client_)
1114 return client_->TakeDebugInfo();
1115 else
1116 return nullptr;
1119 void Layer::SetHasRenderSurface(bool has_render_surface) {
1120 if (has_render_surface_ == has_render_surface)
1121 return;
1122 has_render_surface_ = has_render_surface;
1123 // We do not need SetNeedsCommit here, since this is only ever called
1124 // during a commit, from CalculateDrawProperties.
1125 SetNeedsPushProperties();
1128 void Layer::CreateRenderSurface() {
1129 DCHECK(!render_surface_);
1130 render_surface_ = make_scoped_ptr(new RenderSurface(this));
1133 void Layer::ClearRenderSurface() {
1134 render_surface_ = nullptr;
1137 void Layer::ClearRenderSurfaceLayerList() {
1138 if (render_surface_)
1139 render_surface_->ClearLayerLists();
1142 gfx::ScrollOffset Layer::ScrollOffsetForAnimation() const {
1143 return CurrentScrollOffset();
1146 // On<Property>Animated is called due to an ongoing accelerated animation.
1147 // Since this animation is also being run on the compositor thread, there
1148 // is no need to request a commit to push this value over, so the value is
1149 // set directly rather than by calling Set<Property>.
1150 void Layer::OnFilterAnimated(const FilterOperations& filters) {
1151 filters_ = filters;
1154 void Layer::OnOpacityAnimated(float opacity) {
1155 opacity_ = opacity;
1158 void Layer::OnTransformAnimated(const gfx::Transform& transform) {
1159 if (transform_ == transform)
1160 return;
1161 transform_ = transform;
1162 transform_is_invertible_ = transform.IsInvertible();
1165 void Layer::OnScrollOffsetAnimated(const gfx::ScrollOffset& scroll_offset) {
1166 // Do nothing. Scroll deltas will be sent from the compositor thread back
1167 // to the main thread in the same manner as during non-animated
1168 // compositor-driven scrolling.
1171 void Layer::OnAnimationWaitingForDeletion() {
1172 // Animations are only deleted during PushProperties.
1173 SetNeedsPushProperties();
1176 bool Layer::IsActive() const {
1177 return true;
1180 bool Layer::AddAnimation(scoped_ptr <Animation> animation) {
1181 if (!layer_animation_controller_->animation_registrar())
1182 return false;
1184 if (animation->target_property() == Animation::SCROLL_OFFSET &&
1185 !layer_animation_controller_->animation_registrar()
1186 ->supports_scroll_animations())
1187 return false;
1189 UMA_HISTOGRAM_BOOLEAN("Renderer.AnimationAddedToOrphanLayer",
1190 !layer_tree_host_);
1191 layer_animation_controller_->AddAnimation(animation.Pass());
1192 SetNeedsCommit();
1193 return true;
1196 void Layer::PauseAnimation(int animation_id, double time_offset) {
1197 layer_animation_controller_->PauseAnimation(
1198 animation_id, base::TimeDelta::FromSecondsD(time_offset));
1199 SetNeedsCommit();
1202 void Layer::RemoveAnimation(int animation_id) {
1203 layer_animation_controller_->RemoveAnimation(animation_id);
1204 SetNeedsCommit();
1207 void Layer::RemoveAnimation(int animation_id,
1208 Animation::TargetProperty property) {
1209 layer_animation_controller_->RemoveAnimation(animation_id, property);
1210 SetNeedsCommit();
1213 void Layer::SetLayerAnimationControllerForTest(
1214 scoped_refptr<LayerAnimationController> controller) {
1215 layer_animation_controller_->RemoveValueObserver(this);
1216 layer_animation_controller_ = controller;
1217 layer_animation_controller_->AddValueObserver(this);
1218 SetNeedsCommit();
1221 bool Layer::HasActiveAnimation() const {
1222 return layer_animation_controller_->HasActiveAnimation();
1225 void Layer::AddLayerAnimationEventObserver(
1226 LayerAnimationEventObserver* animation_observer) {
1227 layer_animation_controller_->AddEventObserver(animation_observer);
1230 void Layer::RemoveLayerAnimationEventObserver(
1231 LayerAnimationEventObserver* animation_observer) {
1232 layer_animation_controller_->RemoveEventObserver(animation_observer);
1235 SimpleEnclosedRegion Layer::VisibleContentOpaqueRegion() const {
1236 if (contents_opaque())
1237 return SimpleEnclosedRegion(visible_content_rect());
1238 return SimpleEnclosedRegion();
1241 ScrollbarLayerInterface* Layer::ToScrollbarLayer() {
1242 return nullptr;
1245 RenderingStatsInstrumentation* Layer::rendering_stats_instrumentation() const {
1246 return layer_tree_host_->rendering_stats_instrumentation();
1249 void Layer::RemoveFromScrollTree() {
1250 if (scroll_children_.get()) {
1251 std::set<Layer*> copy = *scroll_children_;
1252 for (std::set<Layer*>::iterator it = copy.begin(); it != copy.end(); ++it)
1253 (*it)->SetScrollParent(nullptr);
1256 DCHECK(!scroll_children_);
1257 SetScrollParent(nullptr);
1260 void Layer::RemoveFromClipTree() {
1261 if (clip_children_.get()) {
1262 std::set<Layer*> copy = *clip_children_;
1263 for (std::set<Layer*>::iterator it = copy.begin(); it != copy.end(); ++it)
1264 (*it)->SetClipParent(nullptr);
1267 DCHECK(!clip_children_);
1268 SetClipParent(nullptr);
1271 void Layer::AddDrawableDescendants(int num) {
1272 DCHECK_GE(num_descendants_that_draw_content_, 0);
1273 DCHECK_GE(num_descendants_that_draw_content_ + num, 0);
1274 if (num == 0)
1275 return;
1276 num_descendants_that_draw_content_ += num;
1277 SetNeedsCommit();
1278 if (parent())
1279 parent()->AddDrawableDescendants(num);
1282 void Layer::RunMicroBenchmark(MicroBenchmark* benchmark) {
1283 benchmark->RunOnLayer(this);
1286 bool Layer::HasDelegatedContent() const {
1287 return false;
1290 gfx::Transform Layer::screen_space_transform_from_property_trees(
1291 const TransformTree& tree) const {
1292 gfx::Transform xform(1, 0, 0, 1, offset_to_transform_parent().x(),
1293 offset_to_transform_parent().y());
1294 if (transform_tree_index() >= 0) {
1295 gfx::Transform ssxform = tree.Node(transform_tree_index())->data.to_screen;
1296 xform.ConcatTransform(ssxform);
1298 xform.Scale(1.0 / contents_scale_x(), 1.0 / contents_scale_y());
1299 return xform;
1302 gfx::Transform Layer::draw_transform_from_property_trees(
1303 const TransformTree& tree) const {
1304 const TransformNode* node = tree.Node(transform_tree_index());
1305 // TODO(vollick): ultimately we'll need to find this information (whether or
1306 // not we establish a render surface) somewhere other than the layer.
1307 const TransformNode* target_node =
1308 has_render_surface_ ? node : tree.Node(node->data.content_target_id);
1310 gfx::Transform xform;
1311 const bool owns_non_root_surface = parent() && render_surface();
1312 if (!owns_non_root_surface) {
1313 // If you're not the root, or you don't own a surface, you need to apply
1314 // your local offset.
1315 xform = node->data.to_target;
1316 xform.Translate(offset_to_transform_parent().x(),
1317 offset_to_transform_parent().y());
1318 } else {
1319 // Surfaces need to apply their sublayer scale.
1320 xform.Scale(target_node->data.sublayer_scale.x(),
1321 target_node->data.sublayer_scale.y());
1323 xform.Scale(1.0 / contents_scale_x(), 1.0 / contents_scale_y());
1324 return xform;
1327 float Layer::DrawOpacityFromPropertyTrees(const OpacityTree& tree) const {
1328 if (!render_target())
1329 return 0.f;
1331 const OpacityNode* target_node =
1332 tree.Node(render_target()->opacity_tree_index());
1333 const OpacityNode* node = tree.Node(opacity_tree_index());
1334 if (node == target_node)
1335 return 1.f;
1337 float draw_opacity = 1.f;
1338 while (node != target_node) {
1339 draw_opacity *= node->data;
1340 node = tree.parent(node);
1342 return draw_opacity;
1345 void Layer::SetFrameTimingRequests(
1346 const std::vector<FrameTimingRequest>& requests) {
1347 frame_timing_requests_ = requests;
1348 frame_timing_requests_dirty_ = true;
1349 SetNeedsCommit();
1352 } // namespace cc