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, base::TimeDelta 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(base::TimeTicks monotonic_time); 56 void AccumulatePropertyUpdates(base::TimeTicks monotonic_time, 57 AnimationEventsVector* events); 58 59 void UpdateState(bool start_ready_animations, 60 AnimationEventsVector* events); 61 62 // Make animations affect active observers if and only if they affect 63 // pending observers. Any animations that no longer affect any observers 64 // are deleted. 65 void ActivateAnimations(); 66 67 // Returns the active animation in the given group, animating the given 68 // property, if such an animation exists. 69 Animation* GetAnimation(int group_id, 70 Animation::TargetProperty target_property) const; 71 72 // Returns the active animation animating the given property that is either 73 // running, or is next to run, if such an animation exists. 74 Animation* GetAnimation(Animation::TargetProperty target_property) const; 75 76 // Returns true if there are any animations that have neither finished nor 77 // aborted. 78 bool HasActiveAnimation() const; 79 80 // Returns true if there are any animations at all to process. has_any_animation()81 bool has_any_animation() const { return !animations_.empty(); } 82 83 // Returns true if there is an animation currently animating the given 84 // property, or if there is an animation scheduled to animate this property in 85 // the future. 86 bool IsAnimatingProperty(Animation::TargetProperty target_property) const; 87 88 void SetAnimationRegistrar(AnimationRegistrar* registrar); animation_registrar()89 AnimationRegistrar* animation_registrar() { return registrar_; } 90 91 void NotifyAnimationStarted(const AnimationEvent& event); 92 void NotifyAnimationFinished(const AnimationEvent& event); 93 void NotifyAnimationAborted(const AnimationEvent& event); 94 void NotifyAnimationPropertyUpdate(const AnimationEvent& event); 95 96 void AddValueObserver(LayerAnimationValueObserver* observer); 97 void RemoveValueObserver(LayerAnimationValueObserver* observer); 98 99 void AddEventObserver(LayerAnimationEventObserver* observer); 100 void RemoveEventObserver(LayerAnimationEventObserver* observer); 101 set_value_provider(LayerAnimationValueProvider * provider)102 void set_value_provider(LayerAnimationValueProvider* provider) { 103 value_provider_ = provider; 104 } 105 remove_value_provider(LayerAnimationValueProvider * provider)106 void remove_value_provider(LayerAnimationValueProvider* provider) { 107 if (value_provider_ == provider) 108 value_provider_ = NULL; 109 } 110 set_layer_animation_delegate(AnimationDelegate * delegate)111 void set_layer_animation_delegate(AnimationDelegate* delegate) { 112 layer_animation_delegate_ = delegate; 113 } 114 remove_layer_animation_delegate(AnimationDelegate * delegate)115 void remove_layer_animation_delegate(AnimationDelegate* delegate) { 116 if (layer_animation_delegate_ == delegate) 117 layer_animation_delegate_ = NULL; 118 } 119 120 bool HasFilterAnimationThatInflatesBounds() const; 121 bool HasTransformAnimationThatInflatesBounds() const; HasAnimationThatInflatesBounds()122 bool HasAnimationThatInflatesBounds() const { 123 return HasTransformAnimationThatInflatesBounds() || 124 HasFilterAnimationThatInflatesBounds(); 125 } 126 127 bool FilterAnimationBoundsForBox(const gfx::BoxF& box, 128 gfx::BoxF* bounds) const; 129 bool TransformAnimationBoundsForBox(const gfx::BoxF& box, 130 gfx::BoxF* bounds) const; 131 132 bool HasAnimationThatAffectsScale() const; 133 134 bool HasOnlyTranslationTransforms() const; 135 136 // Sets |max_scale| to the maximum scale along any dimension at any 137 // destination in active animations. Returns false if the maximum scale cannot 138 // be computed. 139 bool MaximumTargetScale(float* max_scale) const; 140 needs_to_start_animations_for_testing()141 bool needs_to_start_animations_for_testing() { 142 return needs_to_start_animations_; 143 } 144 145 protected: 146 friend class base::RefCounted<LayerAnimationController>; 147 148 explicit LayerAnimationController(int id); 149 virtual ~LayerAnimationController(); 150 151 private: 152 typedef base::hash_set<int> TargetProperties; 153 154 void PushNewAnimationsToImplThread( 155 LayerAnimationController* controller_impl) const; 156 void RemoveAnimationsCompletedOnMainThread( 157 LayerAnimationController* controller_impl) const; 158 void PushPropertiesToImplThread( 159 LayerAnimationController* controller_impl) const; 160 161 void StartAnimations(base::TimeTicks monotonic_time); 162 void PromoteStartedAnimations(base::TimeTicks monotonic_time, 163 AnimationEventsVector* events); 164 void MarkFinishedAnimations(base::TimeTicks monotonic_time); 165 void MarkAnimationsForDeletion(base::TimeTicks monotonic_time, 166 AnimationEventsVector* events); 167 void PurgeAnimationsMarkedForDeletion(); 168 169 void TickAnimations(base::TimeTicks monotonic_time); 170 171 enum UpdateActivationType { 172 NormalActivation, 173 ForceActivation 174 }; 175 void UpdateActivation(UpdateActivationType type); 176 177 void NotifyObserversOpacityAnimated(float opacity, 178 bool notify_active_observers, 179 bool notify_pending_observers); 180 void NotifyObserversTransformAnimated(const gfx::Transform& transform, 181 bool notify_active_observers, 182 bool notify_pending_observers); 183 void NotifyObserversFilterAnimated(const FilterOperations& filter, 184 bool notify_active_observers, 185 bool notify_pending_observers); 186 void NotifyObserversScrollOffsetAnimated(const gfx::Vector2dF& scroll_offset, 187 bool notify_active_observers, 188 bool notify_pending_observers); 189 190 void NotifyObserversAnimationWaitingForDeletion(); 191 192 bool HasValueObserver(); 193 bool HasActiveValueObserver(); 194 195 AnimationRegistrar* registrar_; 196 int id_; 197 ScopedPtrVector<Animation> animations_; 198 199 // This is used to ensure that we don't spam the registrar. 200 bool is_active_; 201 202 base::TimeTicks last_tick_time_; 203 204 ObserverList<LayerAnimationValueObserver> value_observers_; 205 ObserverList<LayerAnimationEventObserver> event_observers_; 206 207 LayerAnimationValueProvider* value_provider_; 208 209 AnimationDelegate* layer_animation_delegate_; 210 211 // Only try to start animations when new animations are added or when the 212 // previous attempt at starting animations failed to start all animations. 213 bool needs_to_start_animations_; 214 215 DISALLOW_COPY_AND_ASSIGN(LayerAnimationController); 216 }; 217 218 } // namespace cc 219 220 #endif // CC_ANIMATION_LAYER_ANIMATION_CONTROLLER_H_ 221