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 115 bool HasFilterAnimationThatInflatesBounds() const; 116 bool HasTransformAnimationThatInflatesBounds() const; HasAnimationThatInflatesBounds()117 bool HasAnimationThatInflatesBounds() const { 118 return HasTransformAnimationThatInflatesBounds() || 119 HasFilterAnimationThatInflatesBounds(); 120 } 121 122 bool FilterAnimationBoundsForBox(const gfx::BoxF& box, 123 gfx::BoxF* bounds) const; 124 bool TransformAnimationBoundsForBox(const gfx::BoxF& box, 125 gfx::BoxF* bounds) const; 126 127 bool HasAnimationThatAffectsScale() const; 128 129 bool HasOnlyTranslationTransforms() const; 130 131 // Sets |max_scale| to the maximum scale along any dimension during active 132 // animations. Returns false if the maximum scale cannot be computed. 133 bool MaximumScale(float* max_scale) const; 134 needs_to_start_animations_for_testing()135 bool needs_to_start_animations_for_testing() { 136 return needs_to_start_animations_; 137 } 138 139 protected: 140 friend class base::RefCounted<LayerAnimationController>; 141 142 explicit LayerAnimationController(int id); 143 virtual ~LayerAnimationController(); 144 145 private: 146 typedef base::hash_set<int> TargetProperties; 147 148 void PushNewAnimationsToImplThread( 149 LayerAnimationController* controller_impl) const; 150 void RemoveAnimationsCompletedOnMainThread( 151 LayerAnimationController* controller_impl) const; 152 void PushPropertiesToImplThread( 153 LayerAnimationController* controller_impl) const; 154 155 void StartAnimations(base::TimeTicks monotonic_time); 156 void PromoteStartedAnimations(base::TimeTicks monotonic_time, 157 AnimationEventsVector* events); 158 void MarkFinishedAnimations(base::TimeTicks monotonic_time); 159 void MarkAnimationsForDeletion(base::TimeTicks monotonic_time, 160 AnimationEventsVector* events); 161 void PurgeAnimationsMarkedForDeletion(); 162 163 void TickAnimations(base::TimeTicks monotonic_time); 164 165 enum UpdateActivationType { 166 NormalActivation, 167 ForceActivation 168 }; 169 void UpdateActivation(UpdateActivationType type); 170 171 void NotifyObserversOpacityAnimated(float opacity, 172 bool notify_active_observers, 173 bool notify_pending_observers); 174 void NotifyObserversTransformAnimated(const gfx::Transform& transform, 175 bool notify_active_observers, 176 bool notify_pending_observers); 177 void NotifyObserversFilterAnimated(const FilterOperations& filter, 178 bool notify_active_observers, 179 bool notify_pending_observers); 180 void NotifyObserversScrollOffsetAnimated(const gfx::Vector2dF& scroll_offset, 181 bool notify_active_observers, 182 bool notify_pending_observers); 183 184 void NotifyObserversAnimationWaitingForDeletion(); 185 186 bool HasValueObserver(); 187 bool HasActiveValueObserver(); 188 189 AnimationRegistrar* registrar_; 190 int id_; 191 ScopedPtrVector<Animation> animations_; 192 193 // This is used to ensure that we don't spam the registrar. 194 bool is_active_; 195 196 base::TimeTicks last_tick_time_; 197 198 ObserverList<LayerAnimationValueObserver> value_observers_; 199 ObserverList<LayerAnimationEventObserver> event_observers_; 200 201 LayerAnimationValueProvider* value_provider_; 202 203 AnimationDelegate* layer_animation_delegate_; 204 205 // Only try to start animations when new animations are added or when the 206 // previous attempt at starting animations failed to start all animations. 207 bool needs_to_start_animations_; 208 209 DISALLOW_COPY_AND_ASSIGN(LayerAnimationController); 210 }; 211 212 } // namespace cc 213 214 #endif // CC_ANIMATION_LAYER_ANIMATION_CONTROLLER_H_ 215