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