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 { class Transform; } 21 22 namespace cc { 23 24 class Animation; 25 class AnimationDelegate; 26 class AnimationRegistrar; 27 class KeyframeValueList; 28 class LayerAnimationValueObserver; 29 30 class CC_EXPORT LayerAnimationController 31 : public base::RefCounted<LayerAnimationController> { 32 public: 33 static scoped_refptr<LayerAnimationController> Create(int id); 34 35 int id() const { return id_; } 36 37 // These methods are virtual for testing. 38 virtual void AddAnimation(scoped_ptr<Animation> animation); 39 virtual void PauseAnimation(int animation_id, double time_offset); 40 virtual void RemoveAnimation(int animation_id); 41 virtual void RemoveAnimation(int animation_id, 42 Animation::TargetProperty target_property); 43 virtual void SuspendAnimations(double monotonic_time); 44 virtual void ResumeAnimations(double monotonic_time); 45 46 // Ensures that the list of active animations on the main thread and the impl 47 // thread are kept in sync. This function does not take ownership of the impl 48 // thread controller. 49 virtual void PushAnimationUpdatesTo( 50 LayerAnimationController* controller_impl); 51 52 void Animate(double monotonic_time); 53 void AccumulatePropertyUpdates(double monotonic_time, 54 AnimationEventsVector* events); 55 56 void UpdateState(bool start_ready_animations, 57 AnimationEventsVector* events); 58 59 // Returns the active animation in the given group, animating the given 60 // property, if such an animation exists. 61 Animation* GetAnimation(int group_id, 62 Animation::TargetProperty target_property) const; 63 64 // Returns the active animation animating the given property that is either 65 // running, or is next to run, if such an animation exists. 66 Animation* GetAnimation(Animation::TargetProperty target_property) const; 67 68 // Returns true if there are any animations that have neither finished nor 69 // aborted. 70 bool HasActiveAnimation() const; 71 72 // Returns true if there are any animations at all to process. 73 bool has_any_animation() const { return !active_animations_.empty(); } 74 75 // Returns true if there is an animation currently animating the given 76 // property, or if there is an animation scheduled to animate this property in 77 // the future. 78 bool IsAnimatingProperty(Animation::TargetProperty target_property) const; 79 80 // If a sync is forced, then the next time animation updates are pushed to the 81 // impl thread, all animations will be transferred. 82 void set_force_sync() { force_sync_ = true; } 83 84 void SetAnimationRegistrar(AnimationRegistrar* registrar); 85 AnimationRegistrar* animation_registrar() { return registrar_; } 86 87 void NotifyAnimationStarted(const AnimationEvent& event, 88 double wall_clock_time); 89 void NotifyAnimationFinished(const AnimationEvent& event, 90 double wall_clock_time); 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 99 void set_layer_animation_delegate(AnimationDelegate* delegate) { 100 layer_animation_delegate_ = delegate; 101 } 102 103 protected: 104 friend class base::RefCounted<LayerAnimationController>; 105 106 explicit LayerAnimationController(int id); 107 virtual ~LayerAnimationController(); 108 109 private: 110 typedef base::hash_set<int> TargetProperties; 111 112 void PushNewAnimationsToImplThread( 113 LayerAnimationController* controller_impl) const; 114 void RemoveAnimationsCompletedOnMainThread( 115 LayerAnimationController* controller_impl) const; 116 void PushPropertiesToImplThread( 117 LayerAnimationController* controller_impl) const; 118 void ReplaceImplThreadAnimations( 119 LayerAnimationController* controller_impl) const; 120 121 void StartAnimationsWaitingForNextTick(double monotonic_time); 122 void StartAnimationsWaitingForStartTime(double monotonic_time); 123 void StartAnimationsWaitingForTargetAvailability(double monotonic_time); 124 void ResolveConflicts(double monotonic_time); 125 void PromoteStartedAnimations(double monotonic_time, 126 AnimationEventsVector* events); 127 void MarkFinishedAnimations(double monotonic_time); 128 void MarkAnimationsForDeletion(double monotonic_time, 129 AnimationEventsVector* events); 130 void PurgeAnimationsMarkedForDeletion(); 131 132 void TickAnimations(double monotonic_time); 133 134 enum UpdateActivationType { 135 NormalActivation, 136 ForceActivation 137 }; 138 void UpdateActivation(UpdateActivationType type); 139 140 void NotifyObserversOpacityAnimated(float opacity); 141 void NotifyObserversTransformAnimated(const gfx::Transform& transform); 142 143 bool HasValueObserver(); 144 bool HasActiveValueObserver(); 145 146 // If this is true, we force a sync to the impl thread. 147 bool force_sync_; 148 149 AnimationRegistrar* registrar_; 150 int id_; 151 ScopedPtrVector<Animation> active_animations_; 152 153 // This is used to ensure that we don't spam the registrar. 154 bool is_active_; 155 156 double last_tick_time_; 157 158 ObserverList<LayerAnimationValueObserver> value_observers_; 159 ObserverList<LayerAnimationEventObserver> event_observers_; 160 161 AnimationDelegate* layer_animation_delegate_; 162 163 DISALLOW_COPY_AND_ASSIGN(LayerAnimationController); 164 }; 165 166 } // namespace cc 167 168 #endif // CC_ANIMATION_LAYER_ANIMATION_CONTROLLER_H_ 169