| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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_GFX_COMPOSITOR_LAYER_ANIMATOR_H_ | 5 #ifndef UI_GFX_COMPOSITOR_LAYER_ANIMATOR_H_ |
| 6 #define UI_GFX_COMPOSITOR_LAYER_ANIMATOR_H_ | 6 #define UI_GFX_COMPOSITOR_LAYER_ANIMATOR_H_ |
| 7 #pragma once | 7 #pragma once |
| 8 | 8 |
| 9 #include <map> | 9 #include <deque> |
| 10 | 10 #include <vector> |
| 11 #include "base/basictypes.h" | 11 |
| 12 #include "base/compiler_specific.h" | 12 #include "base/memory/linked_ptr.h" |
| 13 #include "base/memory/scoped_ptr.h" | 13 #include "base/memory/scoped_ptr.h" |
| 14 #include "third_party/skia/include/core/SkScalar.h" | 14 #include "base/time.h" |
| 15 #include "third_party/skia/include/utils/SkMatrix44.h" | 15 #include "ui/base/animation/animation_container_element.h" |
| 16 #include "ui/base/animation/animation_delegate.h" | |
| 17 #include "ui/gfx/compositor/compositor_export.h" | 16 #include "ui/gfx/compositor/compositor_export.h" |
| 17 #include "ui/gfx/compositor/layer_animation_element.h" |
| 18 | 18 |
| 19 namespace gfx { | 19 namespace gfx { |
| 20 class Point; | 20 class Rect; |
| 21 } | 21 } |
| 22 | 22 |
| 23 namespace ui { | 23 namespace ui { |
| 24 | |
| 25 class Animation; | 24 class Animation; |
| 26 class Layer; | 25 class Layer; |
| 27 class LayerAnimatorDelegate; | 26 class LayerAnimationSequence; |
| 28 class Transform; | 27 class Transform; |
| 29 | 28 |
| 30 // LayerAnimator manages animating various properties of a Layer. | 29 // When a property of layer needs to be changed it is set by way of |
| 31 class COMPOSITOR_EXPORT LayerAnimator : public ui::AnimationDelegate { | 30 // LayerAnimator. This enables LayerAnimator to animate property |
| 31 // changes. |
| 32 class COMPOSITOR_EXPORT LayerAnimator : public AnimationContainerElement { |
| 32 public: | 33 public: |
| 33 // Types of properties that can be animated. | 34 enum PreemptionStrategy { |
| 34 enum AnimationProperty { | 35 IMMEDIATELY_SET_NEW_TARGET, |
| 35 LOCATION, | 36 IMMEDIATELY_ANIMATE_TO_NEW_TARGET, |
| 36 OPACITY, | 37 ENQUEUE_NEW_ANIMATION, |
| 37 TRANSFORM, | 38 REPLACE_QUEUED_ANIMATIONS, |
| 39 BLEND_WITH_CURRENT_ANIMATION |
| 38 }; | 40 }; |
| 39 | 41 |
| 40 explicit LayerAnimator(Layer* layer); | 42 explicit LayerAnimator(base::TimeDelta transition_duration); |
| 41 virtual ~LayerAnimator(); | 43 virtual ~LayerAnimator(); |
| 42 | 44 |
| 43 // Sets the animation to use. LayerAnimator takes ownership of the animation. | 45 // No implicit animations when properties are set. |
| 44 void SetAnimation(Animation* animation); | 46 static LayerAnimator* CreateDefaultAnimator(); |
| 45 | 47 |
| 46 ui::Layer* layer() { return layer_; } | 48 // Implicitly animates when properties are set. |
| 47 | 49 static LayerAnimator* CreateImplicitAnimator(); |
| 48 // Animates the layer to the specified point. The point is relative to the | 50 |
| 49 // parent layer. | 51 // Sets the transform on the delegate. May cause an implicit animation. |
| 50 void AnimateToPoint(const gfx::Point& target); | 52 virtual void SetTransform(const Transform& transform); |
| 51 | 53 |
| 52 // Animates the transform from the current transform to |transform|. | 54 // Sets the bounds on the delegate. May cause an implicit animation. |
| 53 void AnimateTransform(const Transform& transform); | 55 virtual void SetBounds(const gfx::Rect& bounds); |
| 54 | 56 |
| 55 // Animates the opacity from the current opacity to |target_opacity|. | 57 // Sets the opacity on the delegate. May cause an implicit animation. |
| 56 void AnimateOpacity(float target_opacity); | 58 virtual void SetOpacity(float opacity); |
| 57 | 59 |
| 58 // Returns the target value for the specified type. If the specified property | 60 // Sets the layer animation delegate the animator is associated with. The |
| 59 // is not animating, the current value is returned. | 61 // animator does not own the delegate. |
| 60 gfx::Point GetTargetPoint(); | 62 void SetDelegate(LayerAnimationDelegate* delegate); |
| 61 float GetTargetOpacity(); | 63 |
| 62 ui::Transform GetTargetTransform(); | 64 // Sets the animation preemption strategy. This determines the behaviour if |
| 63 | 65 // a property is set during an animation. The default is |
| 64 // Returns true if animating |property|. | 66 // IMMEDIATELY_SET_NEW_TARGET (see ImmediatelySetNewTarget below). |
| 65 bool IsAnimating(AnimationProperty property) const; | 67 void set_preemption_strategy(PreemptionStrategy strategy) { |
| 66 | 68 preemption_strategy_ = strategy; |
| 67 // Returns true if the animation is running. | 69 } |
| 68 bool IsRunning() const; | 70 |
| 69 | 71 // Start an animation sequence. If an animation for the same property is in |
| 70 // Returns true if the animation has progressed at least once since | 72 // progress, it needs to be interrupted with the new animation. The animator |
| 71 // SetAnimation() was invoked. | 73 // takes ownership of this animation sequence. |
| 72 bool got_initial_tick() const { return got_initial_tick_; } | 74 void StartAnimation(LayerAnimationSequence* animation); |
| 73 | 75 |
| 74 // AnimationDelegate: | 76 // Schedule an animation to be run when possible. The animator takes ownership |
| 75 virtual void AnimationProgressed(const Animation* animation) OVERRIDE; | 77 // of this animation sequence. |
| 76 virtual void AnimationEnded(const Animation* animation) OVERRIDE; | 78 void ScheduleAnimation(LayerAnimationSequence* animation); |
| 79 |
| 80 // Schedules the animations to be run together. Obviously will no work if |
| 81 // they animate any common properties. The animator takes ownership of the |
| 82 // animation sequences. |
| 83 void ScheduleTogether(const std::vector<LayerAnimationSequence*>& animations); |
| 84 |
| 85 // Returns true if there is an animation in the queue (animations remain in |
| 86 // the queue until they complete). |
| 87 bool is_animating() const { return !animation_queue_.empty(); } |
| 88 |
| 89 // Stops animating the given property. No effect if there is no running |
| 90 // animation for the given property. Skips to the final state of the |
| 91 // animation. |
| 92 void StopAnimatingProperty( |
| 93 LayerAnimationElement::AnimatableProperty property); |
| 94 |
| 95 // Stops all animation and clears any queued animations. |
| 96 void StopAnimating(); |
| 97 |
| 98 // For testing purposes only. |
| 99 void set_disable_timer_for_test(bool enabled) { |
| 100 disable_timer_for_test_ = enabled; |
| 101 } |
| 102 base::TimeTicks get_last_step_time_for_test() { return last_step_time_; } |
| 103 |
| 104 protected: |
| 105 LayerAnimationDelegate* delegate() { return delegate_; } |
| 77 | 106 |
| 78 private: | 107 private: |
| 79 // Parameters used when animating the location. | 108 // We need to keep track of the start time of every running animation. |
| 80 struct LocationParams { | 109 struct RunningAnimation { |
| 81 int start_x; | 110 RunningAnimation(LayerAnimationSequence* sequence, |
| 82 int start_y; | 111 base::TimeTicks start_time) |
| 83 int target_x; | 112 : sequence(sequence), |
| 84 int target_y; | 113 start_time(start_time) { |
| 114 } |
| 115 LayerAnimationSequence* sequence; |
| 116 base::TimeTicks start_time; |
| 85 }; | 117 }; |
| 86 | 118 |
| 87 // Parameters used when animating the transform. | 119 typedef std::vector<RunningAnimation> RunningAnimations; |
| 88 struct TransformParams { | 120 typedef std::deque<linked_ptr<LayerAnimationSequence> > AnimationQueue; |
| 89 SkMScalar start[16]; | 121 |
| 90 SkMScalar target[16]; | 122 // Implementation of AnimationContainerElement |
| 91 }; | 123 virtual void SetStartTime(base::TimeTicks start_time) OVERRIDE; |
| 92 | 124 virtual void Step(base::TimeTicks time_now) OVERRIDE; |
| 93 // Parameters used when animating the opacity. | 125 virtual base::TimeDelta GetTimerInterval() const OVERRIDE; |
| 94 struct OpacityParams { | 126 |
| 95 float start; | 127 // Starts or stops stepping depending on whether thare are running animations. |
| 96 float target; | 128 void UpdateAnimationState(); |
| 97 }; | 129 |
| 98 | 130 // Removes the sequences from both the running animations and the queue. |
| 99 union Params { | 131 void RemoveAnimation(LayerAnimationSequence* sequence); |
| 100 LocationParams location; | 132 |
| 101 OpacityParams opacity; | 133 // Progresses to the end of the sequence before removing it. |
| 102 TransformParams transform; | 134 void FinishAnimation(LayerAnimationSequence* sequence); |
| 103 }; | 135 |
| 104 | 136 // Finishes any running animation with zero duration. |
| 105 typedef std::map<AnimationProperty, Params> Elements; | 137 void FinishAnyAnimationWithZeroDuration(); |
| 106 | 138 |
| 107 // Stops animating the specified property. This does not set the property | 139 // Clears the running animations and the queue. No sequences are progressed. |
| 108 // being animated to its final value. | 140 void ClearAnimations(); |
| 109 void StopAnimating(AnimationProperty property); | 141 |
| 110 | 142 // Returns the running animation animating the given property, if any. |
| 111 LayerAnimatorDelegate* delegate(); | 143 RunningAnimation* GetRunningAnimation( |
| 112 | 144 LayerAnimationElement::AnimatableProperty property); |
| 113 // The layer. | 145 |
| 114 Layer* layer_; | 146 // Checks if the sequence has already been added to the queue and adds it |
| 115 | 147 // to the front if note. |
| 116 // Properties being animated. | 148 void AddToQueueIfNotPresent(LayerAnimationSequence* sequence); |
| 117 Elements elements_; | 149 |
| 118 | 150 // Any running or queued animation that affects a property in common with |
| 119 scoped_ptr<ui::Animation> animation_; | 151 // |sequence| is either finished or aborted depending on |abort|. |
| 120 | 152 void RemoveAllAnimationsWithACommonProperty(LayerAnimationSequence* sequence, |
| 121 bool got_initial_tick_; | 153 bool abort); |
| 154 |
| 155 // Preempts a running animation by progressing both the running animation and |
| 156 // the given sequence to the end. |
| 157 void ImmediatelySetNewTarget(LayerAnimationSequence* sequence); |
| 158 |
| 159 // Preempts by aborting the running animation, and starts the given animation. |
| 160 void ImmediatelyAnimateToNewTarget(LayerAnimationSequence* sequence); |
| 161 |
| 162 // Preempts by adding the new animation to the queue. |
| 163 void EnqueueNewAnimation(LayerAnimationSequence* sequence); |
| 164 |
| 165 // Preempts by wiping out any unstarted animation in the queue and then |
| 166 // enqueuing this animation. |
| 167 void ReplaceQueuedAnimations(LayerAnimationSequence* sequence); |
| 168 |
| 169 // If there's an animation in the queue that doesn't animate the same property |
| 170 // as a running animation, or an animation schedule to run before it, start it |
| 171 // up. Repeat until there are no such animations. |
| 172 void ProcessQueue(); |
| 173 |
| 174 // Attempts to add the sequence to the list of running animations. Returns |
| 175 // false if there is an animation running that already animates one of the |
| 176 // properties affected by |sequence|. |
| 177 bool StartSequenceImmediately(LayerAnimationSequence* sequence); |
| 178 |
| 179 // This is the queue of animations to run. |
| 180 AnimationQueue animation_queue_; |
| 181 |
| 182 // The target of all layer animations. |
| 183 LayerAnimationDelegate* delegate_; |
| 184 |
| 185 // The currently running animations. |
| 186 RunningAnimations running_animations_; |
| 187 |
| 188 // Determines how animations are replaced. |
| 189 PreemptionStrategy preemption_strategy_; |
| 190 |
| 191 // The default length of animations. |
| 192 base::TimeDelta transition_duration_; |
| 193 |
| 194 // Used for coordinating the starting of animations. |
| 195 base::TimeTicks last_step_time_; |
| 196 |
| 197 // True if we are being stepped by our container. |
| 198 bool is_started_; |
| 199 |
| 200 // This prevents the animator from automatically stepping through animations |
| 201 // and allows for manual stepping. |
| 202 bool disable_timer_for_test_; |
| 122 | 203 |
| 123 DISALLOW_COPY_AND_ASSIGN(LayerAnimator); | 204 DISALLOW_COPY_AND_ASSIGN(LayerAnimator); |
| 124 }; | 205 }; |
| 125 | 206 |
| 126 } // namespace ui | 207 } // namespace ui |
| 127 | 208 |
| 128 #endif // UI_GFX_COMPOSITOR_LAYER_ANIMATOR_H_ | 209 #endif // UI_GFX_COMPOSITOR_LAYER_ANIMATOR_H_ |
| OLD | NEW |