| 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/auto_reset.h" | 9 #include "base/auto_reset.h" |
| 10 #include "base/command_line.h" | 10 #include "base/command_line.h" |
| (...skipping 136 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 147 scoped_refptr<cc::Layer> root_layer) { | 147 scoped_refptr<cc::Layer> root_layer) { |
| 148 // This function must only be called to set the compositor on the root ui | 148 // This function must only be called to set the compositor on the root ui |
| 149 // layer. | 149 // layer. |
| 150 DCHECK(compositor); | 150 DCHECK(compositor); |
| 151 DCHECK(!compositor_); | 151 DCHECK(!compositor_); |
| 152 DCHECK(compositor->root_layer() == this); | 152 DCHECK(compositor->root_layer() == this); |
| 153 DCHECK(!parent_); | 153 DCHECK(!parent_); |
| 154 | 154 |
| 155 compositor_ = compositor; | 155 compositor_ = compositor; |
| 156 OnDeviceScaleFactorChanged(compositor->device_scale_factor()); | 156 OnDeviceScaleFactorChanged(compositor->device_scale_factor()); |
| 157 AddAnimatorsInTreeToCollection(compositor_->layer_animator_collection()); | |
| 158 | 157 |
| 159 root_layer->AddChild(cc_layer_); | 158 root_layer->AddChild(cc_layer_); |
| 159 SetCompositorForAnimatorsInTree(compositor); |
| 160 SendPendingThreadedAnimations(); | 160 SendPendingThreadedAnimations(); |
| 161 } | 161 } |
| 162 | 162 |
| 163 void Layer::ResetCompositor() { | 163 void Layer::ResetCompositor() { |
| 164 DCHECK(!parent_); | 164 DCHECK(!parent_); |
| 165 if (compositor_) | 165 if (compositor_) { |
| 166 RemoveAnimatorsInTreeFromCollection( | 166 ResetCompositorForAnimatorsInTree(compositor_); |
| 167 compositor_->layer_animator_collection()); | 167 compositor_ = nullptr; |
| 168 compositor_ = nullptr; | 168 } |
| 169 } | 169 } |
| 170 | 170 |
| 171 void Layer::Add(Layer* child) { | 171 void Layer::Add(Layer* child) { |
| 172 DCHECK(!child->compositor_); | 172 DCHECK(!child->compositor_); |
| 173 if (child->parent_) | 173 if (child->parent_) |
| 174 child->parent_->Remove(child); | 174 child->parent_->Remove(child); |
| 175 child->parent_ = this; | 175 child->parent_ = this; |
| 176 children_.push_back(child); | 176 children_.push_back(child); |
| 177 cc_layer_->AddChild(child->cc_layer_); | 177 cc_layer_->AddChild(child->cc_layer_); |
| 178 child->OnDeviceScaleFactorChanged(device_scale_factor_); | 178 child->OnDeviceScaleFactorChanged(device_scale_factor_); |
| 179 if (GetCompositor()) | 179 Compositor* compositor = GetCompositor(); |
| 180 if (compositor) { |
| 181 child->SetCompositorForAnimatorsInTree(compositor); |
| 180 child->SendPendingThreadedAnimations(); | 182 child->SendPendingThreadedAnimations(); |
| 181 LayerAnimatorCollection* collection = GetLayerAnimatorCollection(); | 183 } |
| 182 if (collection) | |
| 183 child->AddAnimatorsInTreeToCollection(collection); | |
| 184 } | 184 } |
| 185 | 185 |
| 186 void Layer::Remove(Layer* child) { | 186 void Layer::Remove(Layer* child) { |
| 187 // Current bounds are used to calculate offsets when layers are reparented. | 187 // Current bounds are used to calculate offsets when layers are reparented. |
| 188 // Stop (and complete) an ongoing animation to update the bounds immediately. | 188 // Stop (and complete) an ongoing animation to update the bounds immediately. |
| 189 LayerAnimator* child_animator = child->animator_.get(); | 189 LayerAnimator* child_animator = child->animator_.get(); |
| 190 if (child_animator) | 190 if (child_animator) |
| 191 child_animator->StopAnimatingProperty(ui::LayerAnimationElement::BOUNDS); | 191 child_animator->StopAnimatingProperty(ui::LayerAnimationElement::BOUNDS); |
| 192 LayerAnimatorCollection* collection = GetLayerAnimatorCollection(); | 192 |
| 193 if (collection) | 193 Compositor* compositor = GetCompositor(); |
| 194 child->RemoveAnimatorsInTreeFromCollection(collection); | 194 if (compositor) |
| 195 child->ResetCompositorForAnimatorsInTree(compositor); |
| 195 | 196 |
| 196 std::vector<Layer*>::iterator i = | 197 std::vector<Layer*>::iterator i = |
| 197 std::find(children_.begin(), children_.end(), child); | 198 std::find(children_.begin(), children_.end(), child); |
| 198 DCHECK(i != children_.end()); | 199 DCHECK(i != children_.end()); |
| 199 children_.erase(i); | 200 children_.erase(i); |
| 200 child->parent_ = NULL; | 201 child->parent_ = NULL; |
| 201 child->cc_layer_->RemoveFromParent(); | 202 child->cc_layer_->RemoveFromParent(); |
| 202 } | 203 } |
| 203 | 204 |
| 204 void Layer::StackAtTop(Layer* child) { | 205 void Layer::StackAtTop(Layer* child) { |
| (...skipping 607 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 812 std::string name_; | 813 std::string name_; |
| 813 }; | 814 }; |
| 814 | 815 |
| 815 scoped_refptr<base::trace_event::ConvertableToTraceFormat> Layer::TakeDebugInfo( | 816 scoped_refptr<base::trace_event::ConvertableToTraceFormat> Layer::TakeDebugInfo( |
| 816 cc::Layer* layer) { | 817 cc::Layer* layer) { |
| 817 return new LayerDebugInfo(name_); | 818 return new LayerDebugInfo(name_); |
| 818 } | 819 } |
| 819 | 820 |
| 820 void Layer::CollectAnimators( | 821 void Layer::CollectAnimators( |
| 821 std::vector<scoped_refptr<LayerAnimator>>* animators) { | 822 std::vector<scoped_refptr<LayerAnimator>>* animators) { |
| 822 if (IsAnimating()) | 823 if (animator_ && animator_->is_animating()) |
| 823 animators->push_back(animator_); | 824 animators->push_back(animator_); |
| 824 for (auto* child : children_) | 825 for (auto* child : children_) |
| 825 child->CollectAnimators(animators); | 826 child->CollectAnimators(animators); |
| 826 } | 827 } |
| 827 | 828 |
| 828 void Layer::StackRelativeTo(Layer* child, Layer* other, bool above) { | 829 void Layer::StackRelativeTo(Layer* child, Layer* other, bool above) { |
| 829 DCHECK_NE(child, other); | 830 DCHECK_NE(child, other); |
| 830 DCHECK_EQ(this, child->parent()); | 831 DCHECK_EQ(this, child->parent()); |
| 831 DCHECK_EQ(this, other->parent()); | 832 DCHECK_EQ(this, other->parent()); |
| 832 | 833 |
| (...skipping 132 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 965 } | 966 } |
| 966 | 967 |
| 967 float Layer::GetDeviceScaleFactor() const { | 968 float Layer::GetDeviceScaleFactor() const { |
| 968 return device_scale_factor_; | 969 return device_scale_factor_; |
| 969 } | 970 } |
| 970 | 971 |
| 971 void Layer::AddThreadedAnimation(scoped_ptr<cc::Animation> animation) { | 972 void Layer::AddThreadedAnimation(scoped_ptr<cc::Animation> animation) { |
| 972 DCHECK(cc_layer_); | 973 DCHECK(cc_layer_); |
| 973 // Until this layer has a compositor (and hence cc_layer_ has a | 974 // Until this layer has a compositor (and hence cc_layer_ has a |
| 974 // LayerTreeHost), addAnimation will fail. | 975 // LayerTreeHost), addAnimation will fail. |
| 975 if (GetCompositor()) | 976 if (GetCompositor()) { |
| 976 cc_layer_->AddAnimation(animation.Pass()); | 977 if (UILayerSettings().use_compositor_animation_timelines) { |
| 977 else | 978 DCHECK(animator_); |
| 979 animator_->AddThreadedAnimation(animation.Pass()); |
| 980 } else { |
| 981 cc_layer_->AddAnimation(animation.Pass()); |
| 982 } |
| 983 } else { |
| 978 pending_threaded_animations_.push_back(animation.Pass()); | 984 pending_threaded_animations_.push_back(animation.Pass()); |
| 985 } |
| 979 } | 986 } |
| 980 | 987 |
| 981 void Layer::RemoveThreadedAnimation(int animation_id) { | 988 void Layer::RemoveThreadedAnimation(int animation_id) { |
| 982 DCHECK(cc_layer_); | 989 DCHECK(cc_layer_); |
| 983 if (pending_threaded_animations_.size() == 0) { | 990 if (pending_threaded_animations_.size() == 0) { |
| 984 cc_layer_->RemoveAnimation(animation_id); | 991 if (UILayerSettings().use_compositor_animation_timelines) { |
| 992 DCHECK(animator_); |
| 993 animator_->RemoveThreadedAnimation(animation_id); |
| 994 } else { |
| 995 cc_layer_->RemoveAnimation(animation_id); |
| 996 } |
| 985 return; | 997 return; |
| 986 } | 998 } |
| 987 | 999 |
| 988 pending_threaded_animations_.erase( | 1000 pending_threaded_animations_.erase( |
| 989 std::remove_if( | 1001 std::remove_if( |
| 990 pending_threaded_animations_.begin(), | 1002 pending_threaded_animations_.begin(), |
| 991 pending_threaded_animations_.end(), | 1003 pending_threaded_animations_.end(), |
| 992 [animation_id](const scoped_ptr<cc::Animation>& animation) { | 1004 [animation_id](const scoped_ptr<cc::Animation>& animation) { |
| 993 return animation->id() == animation_id; | 1005 return animation->id() == animation_id; |
| 994 }), | 1006 }), |
| 995 pending_threaded_animations_.end()); | 1007 pending_threaded_animations_.end()); |
| 996 } | 1008 } |
| 997 | 1009 |
| 998 LayerAnimatorCollection* Layer::GetLayerAnimatorCollection() { | 1010 LayerAnimatorCollection* Layer::GetLayerAnimatorCollection() { |
| 999 Compositor* compositor = GetCompositor(); | 1011 Compositor* compositor = GetCompositor(); |
| 1000 return compositor ? compositor->layer_animator_collection() : NULL; | 1012 return compositor ? compositor->layer_animator_collection() : NULL; |
| 1001 } | 1013 } |
| 1002 | 1014 |
| 1003 cc::Layer* Layer::GetCcLayer() const { | 1015 cc::Layer* Layer::GetCcLayer() const { |
| 1004 return cc_layer_; | 1016 return cc_layer_; |
| 1005 } | 1017 } |
| 1006 | 1018 |
| 1007 void Layer::SendPendingThreadedAnimations() { | 1019 void Layer::SendPendingThreadedAnimations() { |
| 1008 for (auto& animation : pending_threaded_animations_) | 1020 for (auto& animation : pending_threaded_animations_) { |
| 1009 cc_layer_->AddAnimation(std::move(animation)); | 1021 if (UILayerSettings().use_compositor_animation_timelines) { |
| 1022 DCHECK(animator_); |
| 1023 animator_->AddThreadedAnimation(std::move(animation)); |
| 1024 } else { |
| 1025 cc_layer_->AddAnimation(std::move(animation)); |
| 1026 } |
| 1027 } |
| 1010 pending_threaded_animations_.clear(); | 1028 pending_threaded_animations_.clear(); |
| 1011 | 1029 |
| 1012 for (auto* child : children_) | 1030 for (auto* child : children_) |
| 1013 child->SendPendingThreadedAnimations(); | 1031 child->SendPendingThreadedAnimations(); |
| 1014 } | 1032 } |
| 1015 | 1033 |
| 1016 void Layer::CreateCcLayer() { | 1034 void Layer::CreateCcLayer() { |
| 1017 if (type_ == LAYER_SOLID_COLOR) { | 1035 if (type_ == LAYER_SOLID_COLOR) { |
| 1018 solid_color_layer_ = cc::SolidColorLayer::Create(UILayerSettings()); | 1036 solid_color_layer_ = cc::SolidColorLayer::Create(UILayerSettings()); |
| 1019 cc_layer_ = solid_color_layer_.get(); | 1037 cc_layer_ = solid_color_layer_.get(); |
| (...skipping 29 matching lines...) Expand all Loading... |
| 1049 size.SetToMin(frame_size_in_dip_); | 1067 size.SetToMin(frame_size_in_dip_); |
| 1050 } | 1068 } |
| 1051 cc_layer_->SetBounds(size); | 1069 cc_layer_->SetBounds(size); |
| 1052 } | 1070 } |
| 1053 | 1071 |
| 1054 void Layer::RecomputePosition() { | 1072 void Layer::RecomputePosition() { |
| 1055 cc_layer_->SetPosition(gfx::PointF(bounds_.origin()) + | 1073 cc_layer_->SetPosition(gfx::PointF(bounds_.origin()) + |
| 1056 subpixel_position_offset_); | 1074 subpixel_position_offset_); |
| 1057 } | 1075 } |
| 1058 | 1076 |
| 1059 void Layer::AddAnimatorsInTreeToCollection( | 1077 void Layer::SetCompositorForAnimatorsInTree(Compositor* compositor) { |
| 1060 LayerAnimatorCollection* collection) { | 1078 DCHECK(compositor); |
| 1061 DCHECK(collection); | 1079 LayerAnimatorCollection* collection = compositor->layer_animator_collection(); |
| 1062 if (IsAnimating()) | 1080 |
| 1063 animator_->AddToCollection(collection); | 1081 if (animator_) { |
| 1082 if (animator_->is_animating()) |
| 1083 animator_->AddToCollection(collection); |
| 1084 animator_->SetCompositor(compositor); |
| 1085 } |
| 1086 |
| 1064 for (auto* child : children_) | 1087 for (auto* child : children_) |
| 1065 child->AddAnimatorsInTreeToCollection(collection); | 1088 child->SetCompositorForAnimatorsInTree(compositor); |
| 1066 } | 1089 } |
| 1067 | 1090 |
| 1068 void Layer::RemoveAnimatorsInTreeFromCollection( | 1091 void Layer::ResetCompositorForAnimatorsInTree(Compositor* compositor) { |
| 1069 LayerAnimatorCollection* collection) { | 1092 DCHECK(compositor); |
| 1070 DCHECK(collection); | 1093 LayerAnimatorCollection* collection = compositor->layer_animator_collection(); |
| 1071 if (IsAnimating()) | 1094 |
| 1072 animator_->RemoveFromCollection(collection); | 1095 if (animator_) { |
| 1096 animator_->ResetCompositor(compositor); |
| 1097 if (animator_->is_animating()) |
| 1098 animator_->RemoveFromCollection(collection); |
| 1099 } |
| 1100 |
| 1073 for (auto* child : children_) | 1101 for (auto* child : children_) |
| 1074 child->RemoveAnimatorsInTreeFromCollection(collection); | 1102 child->ResetCompositorForAnimatorsInTree(compositor); |
| 1075 } | |
| 1076 | |
| 1077 bool Layer::IsAnimating() const { | |
| 1078 return animator_.get() && animator_->is_animating(); | |
| 1079 } | 1103 } |
| 1080 | 1104 |
| 1081 } // namespace ui | 1105 } // namespace ui |
| OLD | NEW |