From 1fbd9f9597d87b9dd29aecc87879538b168b06e4 Mon Sep 17 00:00:00 2001 From: loyso Date: Wed, 16 Dec 2015 23:43:13 -0800 Subject: CC Animations: Make ui::LayerAnimator a LayerAnimationEventObserver (instead of ui::Layer) This will allow us to move more animation code out of ui::Layer. BUG=394777 Review URL: https://codereview.chromium.org/1531913003 Cr-Commit-Position: refs/heads/master@{#365774} --- ui/compositor/layer.cc | 14 +++++--------- ui/compositor/layer.h | 8 ++------ ui/compositor/layer_animation_delegate.h | 5 +++++ ui/compositor/layer_animator.cc | 14 ++++++++++++++ ui/compositor/layer_animator.h | 17 +++++++++++++++-- ui/compositor/layer_animator_unittest.cc | 2 ++ ui/compositor/test/test_layer_animation_delegate.cc | 11 +++++++++++ ui/compositor/test/test_layer_animation_delegate.h | 5 +++++ 8 files changed, 59 insertions(+), 17 deletions(-) (limited to 'ui/compositor') diff --git a/ui/compositor/layer.cc b/ui/compositor/layer.cc index aa46790..8f86728 100644 --- a/ui/compositor/layer.cc +++ b/ui/compositor/layer.cc @@ -120,7 +120,6 @@ Layer::~Layer() { for (size_t i = 0; i < children_.size(); ++i) children_[i]->parent_ = NULL; - cc_layer_->RemoveLayerAnimationEventObserver(this); cc_layer_->RemoveFromParent(); } @@ -488,6 +487,7 @@ void Layer::SwitchToLayer(scoped_refptr new_layer) { if (animator_.get()) { animator_->StopAnimatingProperty(LayerAnimationElement::TRANSFORM); animator_->StopAnimatingProperty(LayerAnimationElement::OPACITY); + animator_->SwitchToLayer(new_layer); } if (texture_layer_.get()) @@ -499,7 +499,6 @@ void Layer::SwitchToLayer(scoped_refptr new_layer) { cc_layer_->parent()->ReplaceChild(cc_layer_, new_layer); } cc_layer_->SetLayerClient(NULL); - cc_layer_->RemoveLayerAnimationEventObserver(this); new_layer->SetOpacity(cc_layer_->opacity()); new_layer->SetTransform(cc_layer_->transform()); new_layer->SetPosition(cc_layer_->position()); @@ -512,7 +511,6 @@ void Layer::SwitchToLayer(scoped_refptr new_layer) { delegated_renderer_layer_ = NULL; surface_layer_ = NULL; - cc_layer_->AddLayerAnimationEventObserver(this); for (size_t i = 0; i < children_.size(); ++i) { DCHECK(children_[i]->cc_layer_); cc_layer_->AddChild(children_[i]->cc_layer_); @@ -819,11 +817,6 @@ scoped_refptr Layer::TakeDebugInfo( return new LayerDebugInfo(name_); } -void Layer::OnAnimationStarted(const cc::AnimationEvent& event) { - if (animator_.get()) - animator_->OnThreadedAnimationStarted(event); -} - void Layer::CollectAnimators( std::vector>* animators) { if (IsAnimating()) @@ -1007,6 +1000,10 @@ LayerAnimatorCollection* Layer::GetLayerAnimatorCollection() { return compositor ? compositor->layer_animator_collection() : NULL; } +cc::Layer* Layer::GetCcLayer() const { + return cc_layer_; +} + void Layer::SendPendingThreadedAnimations() { for (auto& animation : pending_threaded_animations_) cc_layer_->AddAnimation(std::move(animation)); @@ -1030,7 +1027,6 @@ void Layer::CreateCcLayer() { cc_layer_->SetTransformOrigin(gfx::Point3F()); cc_layer_->SetContentsOpaque(true); cc_layer_->SetIsDrawable(type_ != LAYER_NOT_DRAWN); - cc_layer_->AddLayerAnimationEventObserver(this); cc_layer_->SetLayerClient(this); RecomputePosition(); } diff --git a/ui/compositor/layer.h b/ui/compositor/layer.h index 89562b9..2628fc1 100644 --- a/ui/compositor/layer.h +++ b/ui/compositor/layer.h @@ -13,7 +13,6 @@ #include "base/memory/scoped_ptr.h" #include "base/message_loop/message_loop.h" #include "cc/animation/animation_events.h" -#include "cc/animation/layer_animation_event_observer.h" #include "cc/base/region.h" #include "cc/layers/content_layer_client.h" #include "cc/layers/layer_client.h" @@ -68,8 +67,7 @@ class COMPOSITOR_EXPORT Layer : public LayerAnimationDelegate, NON_EXPORTED_BASE(public cc::ContentLayerClient), NON_EXPORTED_BASE(public cc::TextureLayerClient), - NON_EXPORTED_BASE(public cc::LayerClient), - NON_EXPORTED_BASE(public cc::LayerAnimationEventObserver) { + NON_EXPORTED_BASE(public cc::LayerClient) { public: Layer(); explicit Layer(LayerType type); @@ -372,9 +370,6 @@ class COMPOSITOR_EXPORT Layer scoped_refptr TakeDebugInfo( cc::Layer* layer) override; - // LayerAnimationEventObserver - void OnAnimationStarted(const cc::AnimationEvent& event) override; - // Whether this layer has animations waiting to get sent to its cc::Layer. bool HasPendingThreadedAnimations() { return pending_threaded_animations_.size() != 0; @@ -415,6 +410,7 @@ class COMPOSITOR_EXPORT Layer void AddThreadedAnimation(scoped_ptr animation) override; void RemoveThreadedAnimation(int animation_id) override; LayerAnimatorCollection* GetLayerAnimatorCollection() override; + cc::Layer* GetCcLayer() const override; // Creates a corresponding composited layer for |type_|. void CreateCcLayer(); diff --git a/ui/compositor/layer_animation_delegate.h b/ui/compositor/layer_animation_delegate.h index 0643f35..cc83f43 100644 --- a/ui/compositor/layer_animation_delegate.h +++ b/ui/compositor/layer_animation_delegate.h @@ -12,6 +12,10 @@ #include "ui/gfx/geometry/rect.h" #include "ui/gfx/transform.h" +namespace cc { +class Layer; +} + namespace ui { class LayerAnimatorCollection; @@ -38,6 +42,7 @@ class COMPOSITOR_EXPORT LayerAnimationDelegate { virtual void AddThreadedAnimation(scoped_ptr animation) = 0; virtual void RemoveThreadedAnimation(int animation_id) = 0; virtual LayerAnimatorCollection* GetLayerAnimatorCollection() = 0; + virtual cc::Layer* GetCcLayer() const = 0; protected: virtual ~LayerAnimationDelegate() {} diff --git a/ui/compositor/layer_animator.cc b/ui/compositor/layer_animator.cc index 4d063ec..b4afd2e 100644 --- a/ui/compositor/layer_animator.cc +++ b/ui/compositor/layer_animator.cc @@ -115,6 +115,7 @@ void LayerAnimator::SetDelegate(LayerAnimationDelegate* delegate) { if (collection) collection->StopAnimator(this); } + SwitchToLayer(delegate ? delegate->GetCcLayer() : nullptr); delegate_ = delegate; if (delegate_ && is_started_) { LayerAnimatorCollection* collection = GetLayerAnimatorCollection(); @@ -123,6 +124,15 @@ void LayerAnimator::SetDelegate(LayerAnimationDelegate* delegate) { } } +void LayerAnimator::SwitchToLayer(scoped_refptr new_layer) { + if (delegate_) { + DCHECK(delegate_->GetCcLayer()); + delegate_->GetCcLayer()->RemoveLayerAnimationEventObserver(this); + } + if (new_layer) + new_layer->AddLayerAnimationEventObserver(this); +} + void LayerAnimator::StartAnimation(LayerAnimationSequence* animation) { scoped_refptr retain(this); OnScheduled(animation); @@ -850,6 +860,10 @@ LayerAnimatorCollection* LayerAnimator::GetLayerAnimatorCollection() { return delegate_ ? delegate_->GetLayerAnimatorCollection() : NULL; } +void LayerAnimator::OnAnimationStarted(const cc::AnimationEvent& event) { + OnThreadedAnimationStarted(event); +} + LayerAnimator::RunningAnimation::RunningAnimation( const base::WeakPtr& sequence) : sequence_(sequence) { diff --git a/ui/compositor/layer_animator.h b/ui/compositor/layer_animator.h index 99b3765..1c92ac0 100644 --- a/ui/compositor/layer_animator.h +++ b/ui/compositor/layer_animator.h @@ -14,10 +14,15 @@ #include "base/memory/ref_counted.h" #include "base/observer_list.h" #include "base/time/time.h" +#include "cc/animation/layer_animation_event_observer.h" #include "ui/compositor/compositor_export.h" #include "ui/compositor/layer_animation_element.h" #include "ui/gfx/animation/tween.h" +namespace cc { +class Layer; +} + namespace gfx { class Animation; class Rect; @@ -41,7 +46,9 @@ class ScopedLayerAnimationSettings; // ensure that it is not disposed of until it finishes executing. It does this // by holding a reference to itself for the duration of methods for which it // must guarantee that |this| is valid. -class COMPOSITOR_EXPORT LayerAnimator : public base::RefCounted { +class COMPOSITOR_EXPORT LayerAnimator + : public base::RefCounted, + NON_EXPORTED_BASE(public cc::LayerAnimationEventObserver) { public: enum PreemptionStrategy { IMMEDIATELY_SET_NEW_TARGET, @@ -97,6 +104,9 @@ class COMPOSITOR_EXPORT LayerAnimator : public base::RefCounted { // a valid delegate installed. void SetDelegate(LayerAnimationDelegate* delegate); + // Unsubscribe from |cc_layer_| and subscribe to |new_layer|. + void SwitchToLayer(scoped_refptr new_layer); + // Sets the animation preemption strategy. This determines the behaviour if // a property is set during an animation. The default is // IMMEDIATELY_SET_NEW_TARGET (see ImmediatelySetNewTarget below). @@ -193,7 +203,7 @@ class COMPOSITOR_EXPORT LayerAnimator : public base::RefCounted { void RemoveFromCollection(LayerAnimatorCollection* collection); protected: - virtual ~LayerAnimator(); + ~LayerAnimator() override; LayerAnimationDelegate* delegate() { return delegate_; } const LayerAnimationDelegate* delegate() const { return delegate_; } @@ -311,6 +321,9 @@ class COMPOSITOR_EXPORT LayerAnimator : public base::RefCounted { LayerAnimatorCollection* GetLayerAnimatorCollection(); + // LayerAnimationEventObserver + void OnAnimationStarted(const cc::AnimationEvent& event) override; + // This is the queue of animations to run. AnimationQueue animation_queue_; diff --git a/ui/compositor/layer_animator_unittest.cc b/ui/compositor/layer_animator_unittest.cc index 3ceeb4d..633a801 100644 --- a/ui/compositor/layer_animator_unittest.cc +++ b/ui/compositor/layer_animator_unittest.cc @@ -2452,6 +2452,8 @@ TEST(LayerAnimatorTest, LayerAnimatorCollectionTickTime) { collection->StopAnimator(layer.GetAnimator()); EXPECT_FALSE(collection->HasActiveAnimators()); + + animator->SetDelegate(nullptr); } TEST(LayerAnimatorTest, AnimatorStartedCorrectly) { diff --git a/ui/compositor/test/test_layer_animation_delegate.cc b/ui/compositor/test/test_layer_animation_delegate.cc index e6053d6..140e81e 100644 --- a/ui/compositor/test/test_layer_animation_delegate.cc +++ b/ui/compositor/test/test_layer_animation_delegate.cc @@ -2,6 +2,7 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. +#include "ui/compositor/layer.h" #include "ui/compositor/test/test_layer_animation_delegate.h" namespace ui { @@ -12,6 +13,7 @@ TestLayerAnimationDelegate::TestLayerAnimationDelegate() brightness_(0.0f), grayscale_(0.0f), color_(SK_ColorBLACK) { + CreateCcLayer(); } TestLayerAnimationDelegate::TestLayerAnimationDelegate( @@ -21,6 +23,7 @@ TestLayerAnimationDelegate::TestLayerAnimationDelegate( opacity_(other.GetOpacityForAnimation()), visibility_(other.GetVisibilityForAnimation()), color_(SK_ColorBLACK) { + CreateCcLayer(); } TestLayerAnimationDelegate::~TestLayerAnimationDelegate() { @@ -103,4 +106,12 @@ TestLayerAnimationDelegate::GetLayerAnimatorCollection() { return NULL; } +cc::Layer* TestLayerAnimationDelegate::GetCcLayer() const { + return cc_layer_.get(); +} + +void TestLayerAnimationDelegate::CreateCcLayer() { + cc_layer_ = cc::Layer::Create(ui::Layer::UILayerSettings()); +} + } // namespace ui diff --git a/ui/compositor/test/test_layer_animation_delegate.h b/ui/compositor/test/test_layer_animation_delegate.h index b906182..f06f054 100644 --- a/ui/compositor/test/test_layer_animation_delegate.h +++ b/ui/compositor/test/test_layer_animation_delegate.h @@ -6,6 +6,7 @@ #define UI_COMPOSITOR_TEST_TEST_LAYER_ANIMATION_DELEGATE_H_ #include "base/compiler_specific.h" +#include "cc/layers/layer.h" #include "ui/compositor/layer_animation_delegate.h" #include "ui/gfx/geometry/rect.h" #include "ui/gfx/transform.h" @@ -38,8 +39,11 @@ class TestLayerAnimationDelegate : public LayerAnimationDelegate { void AddThreadedAnimation(scoped_ptr animation) override; void RemoveThreadedAnimation(int animation_id) override; LayerAnimatorCollection* GetLayerAnimatorCollection() override; + cc::Layer* GetCcLayer() const override; private: + void CreateCcLayer(); + gfx::Rect bounds_; gfx::Transform transform_; float opacity_; @@ -47,6 +51,7 @@ class TestLayerAnimationDelegate : public LayerAnimationDelegate { float brightness_; float grayscale_; SkColor color_; + scoped_refptr cc_layer_; // Allow copy and assign. }; -- cgit v1.1