| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef UI_COMPOSITOR_LAYER_ANIMATOR_H_ | 5 #ifndef UI_COMPOSITOR_LAYER_ANIMATOR_H_ |
| 6 #define UI_COMPOSITOR_LAYER_ANIMATOR_H_ | 6 #define UI_COMPOSITOR_LAYER_ANIMATOR_H_ |
| 7 | 7 |
| 8 #include <deque> | 8 #include <deque> |
| 9 #include <vector> | 9 #include <vector> |
| 10 | 10 |
| (...skipping 16 matching lines...) Expand all Loading... |
| 27 class Layer; | 27 class Layer; |
| 28 class LayerAnimationSequence; | 28 class LayerAnimationSequence; |
| 29 class LayerAnimationDelegate; | 29 class LayerAnimationDelegate; |
| 30 class LayerAnimationObserver; | 30 class LayerAnimationObserver; |
| 31 class ScopedLayerAnimationSettings; | 31 class ScopedLayerAnimationSettings; |
| 32 class Transform; | 32 class Transform; |
| 33 | 33 |
| 34 // When a property of layer needs to be changed it is set by way of | 34 // When a property of layer needs to be changed it is set by way of |
| 35 // LayerAnimator. This enables LayerAnimator to animate property changes. | 35 // LayerAnimator. This enables LayerAnimator to animate property changes. |
| 36 // NB: during many tests, set_disable_animations_for_test is used and causes | 36 // NB: during many tests, set_disable_animations_for_test is used and causes |
| 37 // all animations to complete immediately. | 37 // all animations to complete immediately. The layer animation is ref counted |
| 38 class COMPOSITOR_EXPORT LayerAnimator : public AnimationContainerElement { | 38 // so that if its owning layer is deleted (and the owning layer is only other |
| 39 // class that should ever hold a ref ptr to a LayerAnimator), the animator can |
| 40 // ensure that it is not disposed of until it finishes executing. It does this |
| 41 // by holding a reference to itself for the duration of methods for which it |
| 42 // must guarantee that |this| is valid. |
| 43 class COMPOSITOR_EXPORT LayerAnimator |
| 44 : public AnimationContainerElement, public base::RefCounted<LayerAnimator> { |
| 39 public: | 45 public: |
| 40 enum PreemptionStrategy { | 46 enum PreemptionStrategy { |
| 41 IMMEDIATELY_SET_NEW_TARGET, | 47 IMMEDIATELY_SET_NEW_TARGET, |
| 42 IMMEDIATELY_ANIMATE_TO_NEW_TARGET, | 48 IMMEDIATELY_ANIMATE_TO_NEW_TARGET, |
| 43 ENQUEUE_NEW_ANIMATION, | 49 ENQUEUE_NEW_ANIMATION, |
| 44 REPLACE_QUEUED_ANIMATIONS, | 50 REPLACE_QUEUED_ANIMATIONS, |
| 45 BLEND_WITH_CURRENT_ANIMATION | 51 BLEND_WITH_CURRENT_ANIMATION |
| 46 }; | 52 }; |
| 47 | 53 |
| 48 explicit LayerAnimator(base::TimeDelta transition_duration); | 54 explicit LayerAnimator(base::TimeDelta transition_duration); |
| 49 virtual ~LayerAnimator(); | |
| 50 | 55 |
| 51 // No implicit animations when properties are set. | 56 // No implicit animations when properties are set. |
| 52 static LayerAnimator* CreateDefaultAnimator(); | 57 static LayerAnimator* CreateDefaultAnimator(); |
| 53 | 58 |
| 54 // Implicitly animates when properties are set. | 59 // Implicitly animates when properties are set. |
| 55 static LayerAnimator* CreateImplicitAnimator(); | 60 static LayerAnimator* CreateImplicitAnimator(); |
| 56 | 61 |
| 57 // Sets the transform on the delegate. May cause an implicit animation. | 62 // Sets the transform on the delegate. May cause an implicit animation. |
| 58 virtual void SetTransform(const Transform& transform); | 63 virtual void SetTransform(const Transform& transform); |
| 59 Transform GetTargetTransform() const; | 64 Transform GetTargetTransform() const; |
| (...skipping 12 matching lines...) Expand all Loading... |
| 72 | 77 |
| 73 // Sets the brightness on the delegate. May cause an implicit animation. | 78 // Sets the brightness on the delegate. May cause an implicit animation. |
| 74 virtual void SetBrightness(float brightness); | 79 virtual void SetBrightness(float brightness); |
| 75 float GetTargetBrightness() const; | 80 float GetTargetBrightness() const; |
| 76 | 81 |
| 77 // Sets the grayscale on the delegate. May cause an implicit animation. | 82 // Sets the grayscale on the delegate. May cause an implicit animation. |
| 78 virtual void SetGrayscale(float grayscale); | 83 virtual void SetGrayscale(float grayscale); |
| 79 float GetTargetGrayscale() const; | 84 float GetTargetGrayscale() const; |
| 80 | 85 |
| 81 // Sets the layer animation delegate the animator is associated with. The | 86 // Sets the layer animation delegate the animator is associated with. The |
| 82 // animator does not own the delegate. | 87 // animator does not own the delegate. The layer animator expects a non-NULL |
| 88 // delegate for most of its operations, so do not call any methods without |
| 89 // a valid delegate installed. |
| 83 void SetDelegate(LayerAnimationDelegate* delegate); | 90 void SetDelegate(LayerAnimationDelegate* delegate); |
| 84 | 91 |
| 85 // Sets the animation preemption strategy. This determines the behaviour if | 92 // Sets the animation preemption strategy. This determines the behaviour if |
| 86 // a property is set during an animation. The default is | 93 // a property is set during an animation. The default is |
| 87 // IMMEDIATELY_SET_NEW_TARGET (see ImmediatelySetNewTarget below). | 94 // IMMEDIATELY_SET_NEW_TARGET (see ImmediatelySetNewTarget below). |
| 88 void set_preemption_strategy(PreemptionStrategy strategy) { | 95 void set_preemption_strategy(PreemptionStrategy strategy) { |
| 89 preemption_strategy_ = strategy; | 96 preemption_strategy_ = strategy; |
| 90 } | 97 } |
| 91 | 98 |
| 92 PreemptionStrategy preemption_strategy() const { | 99 PreemptionStrategy preemption_strategy() const { |
| (...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 167 // When set to true, all animations complete immediately. | 174 // When set to true, all animations complete immediately. |
| 168 static void set_disable_animations_for_test(bool disable_animations) { | 175 static void set_disable_animations_for_test(bool disable_animations) { |
| 169 disable_animations_for_test_ = disable_animations; | 176 disable_animations_for_test_ = disable_animations; |
| 170 } | 177 } |
| 171 | 178 |
| 172 static bool disable_animations_for_test() { | 179 static bool disable_animations_for_test() { |
| 173 return disable_animations_for_test_; | 180 return disable_animations_for_test_; |
| 174 } | 181 } |
| 175 | 182 |
| 176 protected: | 183 protected: |
| 184 virtual ~LayerAnimator(); |
| 185 |
| 177 LayerAnimationDelegate* delegate() { return delegate_; } | 186 LayerAnimationDelegate* delegate() { return delegate_; } |
| 178 const LayerAnimationDelegate* delegate() const { return delegate_; } | 187 const LayerAnimationDelegate* delegate() const { return delegate_; } |
| 179 | 188 |
| 180 // Virtual for testing. | 189 // Virtual for testing. |
| 181 virtual bool ProgressAnimation(LayerAnimationSequence* sequence, | 190 virtual void ProgressAnimation(LayerAnimationSequence* sequence, |
| 182 base::TimeDelta delta); | 191 base::TimeDelta delta); |
| 183 | 192 |
| 184 // Returns true if the sequence is owned by this animator. | 193 // Returns true if the sequence is owned by this animator. |
| 185 bool HasAnimation(LayerAnimationSequence* sequence) const; | 194 bool HasAnimation(LayerAnimationSequence* sequence) const; |
| 186 | 195 |
| 187 private: | 196 private: |
| 197 friend class base::RefCounted<LayerAnimator>; |
| 188 friend class ScopedLayerAnimationSettings; | 198 friend class ScopedLayerAnimationSettings; |
| 189 | 199 |
| 190 class DestroyedTracker; | |
| 191 | |
| 192 // Used by FinishAnimation() to indicate if this has been destroyed. | |
| 193 enum DestroyedType { | |
| 194 DESTROYED, | |
| 195 NOT_DESTROYED, | |
| 196 }; | |
| 197 | |
| 198 // We need to keep track of the start time of every running animation. | 200 // We need to keep track of the start time of every running animation. |
| 199 struct RunningAnimation { | 201 struct RunningAnimation { |
| 200 RunningAnimation(LayerAnimationSequence* sequence, | 202 RunningAnimation(LayerAnimationSequence* sequence, |
| 201 base::TimeTicks start_time) | 203 base::TimeTicks start_time) |
| 202 : sequence(sequence), | 204 : sequence(sequence), |
| 203 start_time(start_time) { | 205 start_time(start_time) { |
| 204 } | 206 } |
| 205 LayerAnimationSequence* sequence; | 207 LayerAnimationSequence* sequence; |
| 206 base::TimeTicks start_time; | 208 base::TimeTicks start_time; |
| 207 }; | 209 }; |
| 208 | 210 |
| 209 typedef std::vector<RunningAnimation> RunningAnimations; | 211 typedef std::vector<RunningAnimation> RunningAnimations; |
| 210 typedef std::deque<linked_ptr<LayerAnimationSequence> > AnimationQueue; | 212 typedef std::deque<linked_ptr<LayerAnimationSequence> > AnimationQueue; |
| 211 | 213 |
| 212 // Implementation of AnimationContainerElement | 214 // Implementation of AnimationContainerElement |
| 213 virtual void SetStartTime(base::TimeTicks start_time) OVERRIDE; | 215 virtual void SetStartTime(base::TimeTicks start_time) OVERRIDE; |
| 214 virtual void Step(base::TimeTicks time_now) OVERRIDE; | 216 virtual void Step(base::TimeTicks time_now) OVERRIDE; |
| 215 virtual base::TimeDelta GetTimerInterval() const OVERRIDE; | 217 virtual base::TimeDelta GetTimerInterval() const OVERRIDE; |
| 216 | 218 |
| 217 // Starts or stops stepping depending on whether thare are running animations. | 219 // Starts or stops stepping depending on whether thare are running animations. |
| 218 void UpdateAnimationState(); | 220 void UpdateAnimationState(); |
| 219 | 221 |
| 220 // Removes the sequences from both the running animations and the queue. | 222 // Removes the sequences from both the running animations and the queue. |
| 221 // Returns a pointer to the removed animation, if any. NOTE: the caller is | 223 // Returns a pointer to the removed animation, if any. NOTE: the caller is |
| 222 // responsible for deleting the returned pointer. | 224 // responsible for deleting the returned pointer. |
| 223 LayerAnimationSequence* RemoveAnimation( | 225 LayerAnimationSequence* RemoveAnimation( |
| 224 LayerAnimationSequence* sequence) WARN_UNUSED_RESULT; | 226 LayerAnimationSequence* sequence) WARN_UNUSED_RESULT; |
| 225 | 227 |
| 226 // Progresses to the end of the sequence before removing it. | 228 // Progresses to the end of the sequence before removing it. |
| 227 DestroyedType FinishAnimation( | 229 void FinishAnimation(LayerAnimationSequence* sequence); |
| 228 LayerAnimationSequence* sequence) WARN_UNUSED_RESULT; | |
| 229 | 230 |
| 230 // Finishes any running animation with zero duration. | 231 // Finishes any running animation with zero duration. |
| 231 void FinishAnyAnimationWithZeroDuration(); | 232 void FinishAnyAnimationWithZeroDuration(); |
| 232 | 233 |
| 233 // Clears the running animations and the queue. No sequences are progressed. | 234 // Clears the running animations and the queue. No sequences are progressed. |
| 234 void ClearAnimations(); | 235 void ClearAnimations(); |
| 235 | 236 |
| 236 // Returns the running animation animating the given property, if any. | 237 // Returns the running animation animating the given property, if any. |
| 237 RunningAnimation* GetRunningAnimation( | 238 RunningAnimation* GetRunningAnimation( |
| 238 LayerAnimationElement::AnimatableProperty property); | 239 LayerAnimationElement::AnimatableProperty property); |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 275 void GetTargetValue(LayerAnimationElement::TargetValue* target) const; | 276 void GetTargetValue(LayerAnimationElement::TargetValue* target) const; |
| 276 | 277 |
| 277 // Called whenever an animation is added to the animation queue. Either by | 278 // Called whenever an animation is added to the animation queue. Either by |
| 278 // starting the animation or adding to the queue. | 279 // starting the animation or adding to the queue. |
| 279 void OnScheduled(LayerAnimationSequence* sequence); | 280 void OnScheduled(LayerAnimationSequence* sequence); |
| 280 | 281 |
| 281 // Returns the default length of animations, including adjustment for slow | 282 // Returns the default length of animations, including adjustment for slow |
| 282 // animation mode if set. | 283 // animation mode if set. |
| 283 base::TimeDelta GetTransitionDuration() const; | 284 base::TimeDelta GetTransitionDuration() const; |
| 284 | 285 |
| 286 // Clears the animation queues and notifies any running animations that they |
| 287 // have been aborted. |
| 288 void ClearAnimationsInternal(); |
| 289 |
| 285 // This is the queue of animations to run. | 290 // This is the queue of animations to run. |
| 286 AnimationQueue animation_queue_; | 291 AnimationQueue animation_queue_; |
| 287 | 292 |
| 288 // The target of all layer animations. | 293 // The target of all layer animations. |
| 289 LayerAnimationDelegate* delegate_; | 294 LayerAnimationDelegate* delegate_; |
| 290 | 295 |
| 291 // The currently running animations. | 296 // The currently running animations. |
| 292 RunningAnimations running_animations_; | 297 RunningAnimations running_animations_; |
| 293 | 298 |
| 294 // Determines how animations are replaced. | 299 // Determines how animations are replaced. |
| (...skipping 21 matching lines...) Expand all Loading... |
| 316 // Slows down all animations for visual debugging. | 321 // Slows down all animations for visual debugging. |
| 317 static bool slow_animation_mode_; | 322 static bool slow_animation_mode_; |
| 318 | 323 |
| 319 // Amount to slow animations for debugging. | 324 // Amount to slow animations for debugging. |
| 320 static int slow_animation_scale_factor_; | 325 static int slow_animation_scale_factor_; |
| 321 | 326 |
| 322 // Observers are notified when layer animations end, are scheduled or are | 327 // Observers are notified when layer animations end, are scheduled or are |
| 323 // aborted. | 328 // aborted. |
| 324 ObserverList<LayerAnimationObserver> observers_; | 329 ObserverList<LayerAnimationObserver> observers_; |
| 325 | 330 |
| 326 scoped_refptr<DestroyedTracker> destroyed_tracker_; | |
| 327 | |
| 328 DISALLOW_COPY_AND_ASSIGN(LayerAnimator); | 331 DISALLOW_COPY_AND_ASSIGN(LayerAnimator); |
| 329 }; | 332 }; |
| 330 | 333 |
| 331 } // namespace ui | 334 } // namespace ui |
| 332 | 335 |
| 333 #endif // UI_COMPOSITOR_LAYER_ANIMATOR_H_ | 336 #endif // UI_COMPOSITOR_LAYER_ANIMATOR_H_ |
| OLD | NEW |