| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "ui/compositor/layer.h" | 5 #include "ui/compositor/layer.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 | 8 |
| 9 #include "base/command_line.h" | 9 #include "base/command_line.h" |
| 10 #include "base/debug/trace_event.h" | 10 #include "base/debug/trace_event.h" |
| (...skipping 124 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 135 float Layer::opacity() const { | 135 float Layer::opacity() const { |
| 136 return cc_layer_->opacity(); | 136 return cc_layer_->opacity(); |
| 137 } | 137 } |
| 138 | 138 |
| 139 void Layer::SetCompositor(Compositor* compositor) { | 139 void Layer::SetCompositor(Compositor* compositor) { |
| 140 // This function must only be called to set the compositor on the root layer, | 140 // This function must only be called to set the compositor on the root layer, |
| 141 // or to reset it. | 141 // or to reset it. |
| 142 DCHECK(!compositor || !compositor_); | 142 DCHECK(!compositor || !compositor_); |
| 143 DCHECK(!compositor || compositor->root_layer() == this); | 143 DCHECK(!compositor || compositor->root_layer() == this); |
| 144 DCHECK(!parent_); | 144 DCHECK(!parent_); |
| 145 if (compositor_) { |
| 146 RemoveAnimatorsInTreeFromCollection( |
| 147 compositor_->layer_animator_collection()); |
| 148 } |
| 145 compositor_ = compositor; | 149 compositor_ = compositor; |
| 146 if (compositor) { | 150 if (compositor) { |
| 147 OnDeviceScaleFactorChanged(compositor->device_scale_factor()); | 151 OnDeviceScaleFactorChanged(compositor->device_scale_factor()); |
| 148 SendPendingThreadedAnimations(); | 152 SendPendingThreadedAnimations(); |
| 153 AddAnimatorsInTreeToCollection(compositor_->layer_animator_collection()); |
| 149 } | 154 } |
| 150 } | 155 } |
| 151 | 156 |
| 152 void Layer::Add(Layer* child) { | 157 void Layer::Add(Layer* child) { |
| 153 DCHECK(!child->compositor_); | 158 DCHECK(!child->compositor_); |
| 154 if (child->parent_) | 159 if (child->parent_) |
| 155 child->parent_->Remove(child); | 160 child->parent_->Remove(child); |
| 156 child->parent_ = this; | 161 child->parent_ = this; |
| 157 children_.push_back(child); | 162 children_.push_back(child); |
| 158 cc_layer_->AddChild(child->cc_layer_); | 163 cc_layer_->AddChild(child->cc_layer_); |
| 159 child->OnDeviceScaleFactorChanged(device_scale_factor_); | 164 child->OnDeviceScaleFactorChanged(device_scale_factor_); |
| 160 if (GetCompositor()) | 165 if (GetCompositor()) |
| 161 child->SendPendingThreadedAnimations(); | 166 child->SendPendingThreadedAnimations(); |
| 167 LayerAnimatorCollection* collection = GetLayerAnimatorCollection(); |
| 168 if (collection) |
| 169 child->AddAnimatorsInTreeToCollection(collection); |
| 162 } | 170 } |
| 163 | 171 |
| 164 void Layer::Remove(Layer* child) { | 172 void Layer::Remove(Layer* child) { |
| 165 // Current bounds are used to calculate offsets when layers are reparented. | 173 // Current bounds are used to calculate offsets when layers are reparented. |
| 166 // Stop (and complete) an ongoing animation to update the bounds immediately. | 174 // Stop (and complete) an ongoing animation to update the bounds immediately. |
| 167 if (child->GetAnimator()) { | 175 LayerAnimator* child_animator = child->animator_; |
| 168 child->GetAnimator()->StopAnimatingProperty( | 176 if (child_animator) |
| 169 ui::LayerAnimationElement::BOUNDS); | 177 child_animator->StopAnimatingProperty(ui::LayerAnimationElement::BOUNDS); |
| 170 } | 178 LayerAnimatorCollection* collection = GetLayerAnimatorCollection(); |
| 179 if (collection) |
| 180 child->RemoveAnimatorsInTreeFromCollection(collection); |
| 181 |
| 171 std::vector<Layer*>::iterator i = | 182 std::vector<Layer*>::iterator i = |
| 172 std::find(children_.begin(), children_.end(), child); | 183 std::find(children_.begin(), children_.end(), child); |
| 173 DCHECK(i != children_.end()); | 184 DCHECK(i != children_.end()); |
| 174 children_.erase(i); | 185 children_.erase(i); |
| 175 child->parent_ = NULL; | 186 child->parent_ = NULL; |
| 176 child->cc_layer_->RemoveFromParent(); | 187 child->cc_layer_->RemoveFromParent(); |
| 177 } | 188 } |
| 178 | 189 |
| 179 void Layer::StackAtTop(Layer* child) { | 190 void Layer::StackAtTop(Layer* child) { |
| 180 if (children_.size() <= 1 || child == children_.back()) | 191 if (children_.size() <= 1 || child == children_.back()) |
| (...skipping 417 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 598 sk_damaged.width(), | 609 sk_damaged.width(), |
| 599 sk_damaged.height()); | 610 sk_damaged.height()); |
| 600 cc_layer_->SetNeedsDisplayRect(damaged); | 611 cc_layer_->SetNeedsDisplayRect(damaged); |
| 601 } | 612 } |
| 602 damaged_region_.setEmpty(); | 613 damaged_region_.setEmpty(); |
| 603 } | 614 } |
| 604 for (size_t i = 0; i < children_.size(); ++i) | 615 for (size_t i = 0; i < children_.size(); ++i) |
| 605 children_[i]->SendDamagedRects(); | 616 children_[i]->SendDamagedRects(); |
| 606 } | 617 } |
| 607 | 618 |
| 619 void Layer::CompleteAllAnimations() { |
| 620 std::vector<scoped_refptr<LayerAnimator> > animators; |
| 621 CollectAnimators(&animators); |
| 622 std::for_each(animators.begin(), animators.end(), |
| 623 std::mem_fun(&LayerAnimator::StopAnimating)); |
| 624 } |
| 625 |
| 608 void Layer::SuppressPaint() { | 626 void Layer::SuppressPaint() { |
| 609 if (!delegate_) | 627 if (!delegate_) |
| 610 return; | 628 return; |
| 611 delegate_ = NULL; | 629 delegate_ = NULL; |
| 612 for (size_t i = 0; i < children_.size(); ++i) | 630 for (size_t i = 0; i < children_.size(); ++i) |
| 613 children_[i]->SuppressPaint(); | 631 children_[i]->SuppressPaint(); |
| 614 } | 632 } |
| 615 | 633 |
| 616 void Layer::OnDeviceScaleFactorChanged(float device_scale_factor) { | 634 void Layer::OnDeviceScaleFactorChanged(float device_scale_factor) { |
| 617 if (device_scale_factor_ == device_scale_factor) | 635 if (device_scale_factor_ == device_scale_factor) |
| (...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 682 | 700 |
| 683 scoped_refptr<base::debug::ConvertableToTraceFormat> Layer::TakeDebugInfo() { | 701 scoped_refptr<base::debug::ConvertableToTraceFormat> Layer::TakeDebugInfo() { |
| 684 return new LayerDebugInfo(name_); | 702 return new LayerDebugInfo(name_); |
| 685 } | 703 } |
| 686 | 704 |
| 687 void Layer::OnAnimationStarted(const cc::AnimationEvent& event) { | 705 void Layer::OnAnimationStarted(const cc::AnimationEvent& event) { |
| 688 if (animator_.get()) | 706 if (animator_.get()) |
| 689 animator_->OnThreadedAnimationStarted(event); | 707 animator_->OnThreadedAnimationStarted(event); |
| 690 } | 708 } |
| 691 | 709 |
| 710 void Layer::CollectAnimators( |
| 711 std::vector<scoped_refptr<LayerAnimator> >* animators) { |
| 712 if (IsAnimating()) |
| 713 animators->push_back(animator_); |
| 714 std::for_each(children_.begin(), children_.end(), |
| 715 std::bind2nd(std::mem_fun(&Layer::CollectAnimators), |
| 716 animators)); |
| 717 } |
| 718 |
| 692 void Layer::StackRelativeTo(Layer* child, Layer* other, bool above) { | 719 void Layer::StackRelativeTo(Layer* child, Layer* other, bool above) { |
| 693 DCHECK_NE(child, other); | 720 DCHECK_NE(child, other); |
| 694 DCHECK_EQ(this, child->parent()); | 721 DCHECK_EQ(this, child->parent()); |
| 695 DCHECK_EQ(this, other->parent()); | 722 DCHECK_EQ(this, other->parent()); |
| 696 | 723 |
| 697 const size_t child_i = | 724 const size_t child_i = |
| 698 std::find(children_.begin(), children_.end(), child) - children_.begin(); | 725 std::find(children_.begin(), children_.end(), child) - children_.begin(); |
| 699 const size_t other_i = | 726 const size_t other_i = |
| 700 std::find(children_.begin(), children_.end(), other) - children_.begin(); | 727 std::find(children_.begin(), children_.end(), other) - children_.begin(); |
| 701 if ((above && child_i == other_i + 1) || (!above && child_i + 1 == other_i)) | 728 if ((above && child_i == other_i + 1) || (!above && child_i + 1 == other_i)) |
| (...skipping 164 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 866 } | 893 } |
| 867 | 894 |
| 868 pending_threaded_animations_.erase( | 895 pending_threaded_animations_.erase( |
| 869 cc::remove_if(&pending_threaded_animations_, | 896 cc::remove_if(&pending_threaded_animations_, |
| 870 pending_threaded_animations_.begin(), | 897 pending_threaded_animations_.begin(), |
| 871 pending_threaded_animations_.end(), | 898 pending_threaded_animations_.end(), |
| 872 HasAnimationId(animation_id)), | 899 HasAnimationId(animation_id)), |
| 873 pending_threaded_animations_.end()); | 900 pending_threaded_animations_.end()); |
| 874 } | 901 } |
| 875 | 902 |
| 903 LayerAnimatorCollection* Layer::GetLayerAnimatorCollection() { |
| 904 Compositor* compositor = GetCompositor(); |
| 905 return compositor ? compositor->layer_animator_collection() : NULL; |
| 906 } |
| 907 |
| 876 void Layer::SendPendingThreadedAnimations() { | 908 void Layer::SendPendingThreadedAnimations() { |
| 877 for (cc::ScopedPtrVector<cc::Animation>::iterator it = | 909 for (cc::ScopedPtrVector<cc::Animation>::iterator it = |
| 878 pending_threaded_animations_.begin(); | 910 pending_threaded_animations_.begin(); |
| 879 it != pending_threaded_animations_.end(); | 911 it != pending_threaded_animations_.end(); |
| 880 ++it) | 912 ++it) |
| 881 cc_layer_->AddAnimation(pending_threaded_animations_.take(it)); | 913 cc_layer_->AddAnimation(pending_threaded_animations_.take(it)); |
| 882 | 914 |
| 883 pending_threaded_animations_.clear(); | 915 pending_threaded_animations_.clear(); |
| 884 | 916 |
| 885 for (size_t i = 0; i < children_.size(); ++i) | 917 for (size_t i = 0; i < children_.size(); ++i) |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 923 size.SetToMin(frame_size_in_dip_); | 955 size.SetToMin(frame_size_in_dip_); |
| 924 delegated_renderer_layer_->SetDisplaySize(frame_size_in_dip_); | 956 delegated_renderer_layer_->SetDisplaySize(frame_size_in_dip_); |
| 925 } | 957 } |
| 926 cc_layer_->SetBounds(size); | 958 cc_layer_->SetBounds(size); |
| 927 } | 959 } |
| 928 | 960 |
| 929 void Layer::RecomputePosition() { | 961 void Layer::RecomputePosition() { |
| 930 cc_layer_->SetPosition(gfx::PointF(bounds_.x(), bounds_.y())); | 962 cc_layer_->SetPosition(gfx::PointF(bounds_.x(), bounds_.y())); |
| 931 } | 963 } |
| 932 | 964 |
| 965 void Layer::AddAnimatorsInTreeToCollection( |
| 966 LayerAnimatorCollection* collection) { |
| 967 DCHECK(collection); |
| 968 if (IsAnimating()) |
| 969 animator_->AddToCollection(collection); |
| 970 std::for_each( |
| 971 children_.begin(), |
| 972 children_.end(), |
| 973 std::bind2nd(std::mem_fun(&Layer::AddAnimatorsInTreeToCollection), |
| 974 collection)); |
| 975 } |
| 976 |
| 977 void Layer::RemoveAnimatorsInTreeFromCollection( |
| 978 LayerAnimatorCollection* collection) { |
| 979 DCHECK(collection); |
| 980 if (IsAnimating()) |
| 981 animator_->RemoveFromCollection(collection); |
| 982 std::for_each( |
| 983 children_.begin(), |
| 984 children_.end(), |
| 985 std::bind2nd(std::mem_fun(&Layer::RemoveAnimatorsInTreeFromCollection), |
| 986 collection)); |
| 987 } |
| 988 |
| 989 bool Layer::IsAnimating() const { |
| 990 return animator_ && animator_->is_animating(); |
| 991 } |
| 992 |
| 933 } // namespace ui | 993 } // namespace ui |
| OLD | NEW |