1 // Copyright 2012 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. 4 5 #ifndef CC_ANIMATION_LAYER_ANIMATION_CONTROLLER_H_ 6 #define CC_ANIMATION_LAYER_ANIMATION_CONTROLLER_H_ 7 8 #include "base/basictypes.h" 9 #include "base/containers/hash_tables.h" 10 #include "base/memory/ref_counted.h" 11 #include "base/memory/scoped_ptr.h" 12 #include "base/observer_list.h" 13 #include "base/time/time.h" 14 #include "cc/animation/animation_events.h" 15 #include "cc/animation/layer_animation_event_observer.h" 16 #include "cc/base/cc_export.h" 17 #include "cc/base/scoped_ptr_vector.h" 18 #include "ui/gfx/transform.h" 19 20 namespace gfx { 21 class BoxF; 22 class Transform; 23 } 24 25 namespace cc { 26 27 class Animation; 28 class AnimationDelegate; 29 class AnimationRegistrar; 30 class FilterOperations; 31 class KeyframeValueList; 32 class LayerAnimationValueObserver; 33 class LayerAnimationValueProvider; 34 35 class CC_EXPORT LayerAnimationController 36 : public base::RefCounted<LayerAnimationController> { 37 public: 38 static scoped_refptr<LayerAnimationController> Create(int id); 39 id()40 int id() const { return id_; } 41 42 void AddAnimation(scoped_ptr<Animation> animation); 43 void PauseAnimation(int animation_id, double time_offset); 44 void RemoveAnimation(int animation_id); 45 void RemoveAnimation(int animation_id, 46 Animation::TargetProperty target_property); 47 void AbortAnimations(Animation::TargetProperty target_property); 48 49 // Ensures that the list of active animations on the main thread and the impl 50 // thread are kept in sync. This function does not take ownership of the impl 51 // thread controller. This method is virtual for testing. 52 virtual void PushAnimationUpdatesTo( 53 LayerAnimationController* controller_impl); 54 55 void Animate(double monotonic_time); 56 void AccumulatePropertyUpdates(double monotonic_time, 57 AnimationEventsVector* events); 58 59 void UpdateState(bool start_ready_animations, 60 AnimationEventsVector* events); 61 62 // Returns the active animation in the given group, animating the given 63 // property, if such an animation exists. 64 Animation* GetAnimation(int group_id, 65 Animation::TargetProperty target_property) const; 66 67 // Returns the active animation animating the given property that is either 68 // running, or is next to run, if such an animation exists. 69 Animation* GetAnimation(Animation::TargetProperty target_property) const; 70 71 // Returns true if there are any animations that have neither finished nor 72 // aborted. 73 bool HasActiveAnimation() const; 74 75 // Returns true if there are any animations at all to process. has_any_animation()76 bool has_any_animation() const { return !active_animations_.empty(); } 77 78 // Returns true if there is an animation currently animating the given 79 // property, or if there is an animation scheduled to animate this property in 80 // the future. 81 bool IsAnimatingProperty(Animation::TargetProperty target_property) const; 82 83 void SetAnimationRegistrar(AnimationRegistrar* registrar); animation_registrar()84 AnimationRegistrar* animation_registrar() { return registrar_; } 85 86 void NotifyAnimationStarted(const AnimationEvent& event, 87 double wall_clock_time); 88 void NotifyAnimationFinished(const AnimationEvent& event, 89 double wall_clock_time); 90 void NotifyAnimationAborted(const AnimationEvent& event); 91 void NotifyAnimationPropertyUpdate(const AnimationEvent& event); 92 93 void AddValueObserver(LayerAnimationValueObserver* observer); 94 void RemoveValueObserver(LayerAnimationValueObserver* observer); 95 96 void AddEventObserver(LayerAnimationEventObserver* observer); 97 void RemoveEventObserver(LayerAnimationEventObserver* observer); 98 set_value_provider(LayerAnimationValueProvider * provider)99 void set_value_provider(LayerAnimationValueProvider* provider) { 100 value_provider_ = provider; 101 } 102 remove_value_provider(LayerAnimationValueProvider * provider)103 void remove_value_provider(LayerAnimationValueProvider* provider) { 104 if (value_provider_ == provider) 105 value_provider_ = NULL; 106 } 107 set_layer_animation_delegate(AnimationDelegate * delegate)108 void set_layer_animation_delegate(AnimationDelegate* delegate) { 109 layer_animation_delegate_ = delegate; 110 } 111 112 bool AnimatedBoundsForBox(const gfx::BoxF& box, gfx::BoxF* bounds); 113 114 protected: 115 friend class base::RefCounted<LayerAnimationController>; 116 117 explicit LayerAnimationController(int id); 118 virtual ~LayerAnimationController(); 119 120 private: 121 typedef base::hash_set<int> TargetProperties; 122 123 void PushNewAnimationsToImplThread( 124 LayerAnimationController* controller_impl) const; 125 void RemoveAnimationsCompletedOnMainThread( 126 LayerAnimationController* controller_impl) const; 127 void PushPropertiesToImplThread( 128 LayerAnimationController* controller_impl) const; 129 130 void StartAnimations(double monotonic_time); 131 void PromoteStartedAnimations(double monotonic_time, 132 AnimationEventsVector* events); 133 void MarkFinishedAnimations(double monotonic_time); 134 void MarkAnimationsForDeletion(double monotonic_time, 135 AnimationEventsVector* events); 136 void PurgeAnimationsMarkedForDeletion(); 137 138 void TickAnimations(double monotonic_time); 139 140 enum UpdateActivationType { 141 NormalActivation, 142 ForceActivation 143 }; 144 void UpdateActivation(UpdateActivationType type); 145 146 void NotifyObserversOpacityAnimated(float opacity); 147 void NotifyObserversTransformAnimated(const gfx::Transform& transform); 148 void NotifyObserversFilterAnimated(const FilterOperations& filter); 149 void NotifyObserversScrollOffsetAnimated(gfx::Vector2dF scroll_offset); 150 151 void NotifyObserversAnimationWaitingForDeletion(); 152 153 bool HasValueObserver(); 154 bool HasActiveValueObserver(); 155 156 AnimationRegistrar* registrar_; 157 int id_; 158 ScopedPtrVector<Animation> active_animations_; 159 160 // This is used to ensure that we don't spam the registrar. 161 bool is_active_; 162 163 double last_tick_time_; 164 165 ObserverList<LayerAnimationValueObserver> value_observers_; 166 ObserverList<LayerAnimationEventObserver> event_observers_; 167 168 LayerAnimationValueProvider* value_provider_; 169 170 AnimationDelegate* layer_animation_delegate_; 171 172 DISALLOW_COPY_AND_ASSIGN(LayerAnimationController); 173 }; 174 175 } // namespace cc 176 177 #endif // CC_ANIMATION_LAYER_ANIMATION_CONTROLLER_H_ 178