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 <deque> | 9 #include <deque> |
10 #include <vector> | 10 #include <vector> |
11 | 11 |
12 #include "base/memory/linked_ptr.h" | 12 #include "base/memory/linked_ptr.h" |
13 #include "base/memory/scoped_ptr.h" | 13 #include "base/memory/scoped_ptr.h" |
14 #include "base/time.h" | 14 #include "base/time.h" |
15 #include "ui/base/animation/animation_container_element.h" | 15 #include "ui/base/animation/animation_container_element.h" |
16 #include "ui/gfx/compositor/compositor_export.h" | 16 #include "ui/gfx/compositor/compositor_export.h" |
17 #include "ui/gfx/compositor/layer_animation_element.h" | 17 #include "ui/gfx/compositor/layer_animation_element.h" |
18 | 18 |
19 namespace gfx { | 19 namespace gfx { |
20 class Rect; | 20 class Rect; |
21 } | 21 } |
22 | 22 |
23 namespace ui { | 23 namespace ui { |
24 class Animation; | 24 class Animation; |
25 class Layer; | 25 class Layer; |
26 class LayerAnimationSequence; | 26 class LayerAnimationSequence; |
27 class LayerAnimatorDelegate; | |
28 class Transform; | 27 class Transform; |
29 | 28 |
30 // When a property of layer needs to be changed it is set by way of | 29 // When a property of layer needs to be changed it is set by way of |
31 // LayerAnimator. This enables LayerAnimator to animate property changes. | 30 // LayerAnimator. This enables LayerAnimator to animate property |
| 31 // changes. |
32 class COMPOSITOR_EXPORT LayerAnimator : public AnimationContainerElement { | 32 class COMPOSITOR_EXPORT LayerAnimator : public AnimationContainerElement { |
33 public: | 33 public: |
34 enum PreemptionStrategy { | 34 enum PreemptionStrategy { |
35 IMMEDIATELY_SET_NEW_TARGET, | 35 IMMEDIATELY_SET_NEW_TARGET, |
36 IMMEDIATELY_ANIMATE_TO_NEW_TARGET, | 36 IMMEDIATELY_ANIMATE_TO_NEW_TARGET, |
37 ENQUEUE_NEW_ANIMATION, | 37 ENQUEUE_NEW_ANIMATION, |
38 REPLACE_QUEUED_ANIMATIONS, | 38 REPLACE_QUEUED_ANIMATIONS, |
39 BLEND_WITH_CURRENT_ANIMATION | 39 BLEND_WITH_CURRENT_ANIMATION |
40 }; | 40 }; |
41 | 41 |
42 explicit LayerAnimator(base::TimeDelta transition_duration); | 42 explicit LayerAnimator(base::TimeDelta transition_duration); |
43 virtual ~LayerAnimator(); | 43 virtual ~LayerAnimator(); |
44 | 44 |
45 // No implicit animations when properties are set. | 45 // No implicit animations when properties are set. |
46 static LayerAnimator* CreateDefaultAnimator(); | 46 static LayerAnimator* CreateDefaultAnimator(); |
47 | 47 |
48 // Implicitly animates when properties are set. | 48 // Implicitly animates when properties are set. |
49 static LayerAnimator* CreateImplicitAnimator(); | 49 static LayerAnimator* CreateImplicitAnimator(); |
50 | 50 |
51 // Sets the transform on the delegate. May cause an implicit animation. | 51 // Sets the transform on the delegate. May cause an implicit animation. |
52 virtual void SetTransform(const Transform& transform); | 52 virtual void SetTransform(const Transform& transform); |
53 Transform GetTargetTransform() const; | |
54 | 53 |
55 // Sets the bounds on the delegate. May cause an implicit animation. | 54 // Sets the bounds on the delegate. May cause an implicit animation. |
56 virtual void SetBounds(const gfx::Rect& bounds); | 55 virtual void SetBounds(const gfx::Rect& bounds); |
57 gfx::Rect GetTargetBounds() const; | |
58 | 56 |
59 // Sets the opacity on the delegate. May cause an implicit animation. | 57 // Sets the opacity on the delegate. May cause an implicit animation. |
60 virtual void SetOpacity(float opacity); | 58 virtual void SetOpacity(float opacity); |
61 float GetTargetOpacity() const; | |
62 | 59 |
63 // Sets the layer animation delegate the animator is associated with. The | 60 // Sets the layer animation delegate the animator is associated with. The |
64 // animator does not own the delegate. | 61 // animator does not own the delegate. |
65 void SetDelegate(LayerAnimatorDelegate* delegate); | 62 void SetDelegate(LayerAnimationDelegate* delegate); |
66 | 63 |
67 // Sets the animation preemption strategy. This determines the behaviour if | 64 // Sets the animation preemption strategy. This determines the behaviour if |
68 // a property is set during an animation. The default is | 65 // a property is set during an animation. The default is |
69 // IMMEDIATELY_SET_NEW_TARGET (see ImmediatelySetNewTarget below). | 66 // IMMEDIATELY_SET_NEW_TARGET (see ImmediatelySetNewTarget below). |
70 void set_preemption_strategy(PreemptionStrategy strategy) { | 67 void set_preemption_strategy(PreemptionStrategy strategy) { |
71 preemption_strategy_ = strategy; | 68 preemption_strategy_ = strategy; |
72 } | 69 } |
73 | 70 |
74 // Start an animation sequence. If an animation for the same property is in | 71 // Start an animation sequence. If an animation for the same property is in |
75 // progress, it needs to be interrupted with the new animation. The animator | 72 // progress, it needs to be interrupted with the new animation. The animator |
76 // takes ownership of this animation sequence. | 73 // takes ownership of this animation sequence. |
77 void StartAnimation(LayerAnimationSequence* animation); | 74 void StartAnimation(LayerAnimationSequence* animation); |
78 | 75 |
79 // Schedule an animation to be run when possible. The animator takes ownership | 76 // Schedule an animation to be run when possible. The animator takes ownership |
80 // of this animation sequence. | 77 // of this animation sequence. |
81 void ScheduleAnimation(LayerAnimationSequence* animation); | 78 void ScheduleAnimation(LayerAnimationSequence* animation); |
82 | 79 |
83 // Schedules the animations to be run together. Obviously will no work if | 80 // Schedules the animations to be run together. Obviously will no work if |
84 // they animate any common properties. The animator takes ownership of the | 81 // they animate any common properties. The animator takes ownership of the |
85 // animation sequences. | 82 // animation sequences. |
86 void ScheduleTogether(const std::vector<LayerAnimationSequence*>& animations); | 83 void ScheduleTogether(const std::vector<LayerAnimationSequence*>& animations); |
87 | 84 |
88 // These are cover functions that create sequences for you to wrap the given | |
89 // elements. These sequences are then passed to the corresponding function | |
90 // above. | |
91 void StartAnimationElement(LayerAnimationElement* element); | |
92 void ScheduleAnimationElement(LayerAnimationElement* element); | |
93 void ScheduleElementsTogether( | |
94 const std::vector<LayerAnimationElement*>& element); | |
95 | |
96 // Returns true if there is an animation in the queue (animations remain in | 85 // Returns true if there is an animation in the queue (animations remain in |
97 // the queue until they complete). | 86 // the queue until they complete). |
98 bool is_animating() const { return !animation_queue_.empty(); } | 87 bool is_animating() const { return !animation_queue_.empty(); } |
99 | 88 |
100 // Stops animating the given property. No effect if there is no running | 89 // Stops animating the given property. No effect if there is no running |
101 // animation for the given property. Skips to the final state of the | 90 // animation for the given property. Skips to the final state of the |
102 // animation. | 91 // animation. |
103 void StopAnimatingProperty( | 92 void StopAnimatingProperty( |
104 LayerAnimationElement::AnimatableProperty property); | 93 LayerAnimationElement::AnimatableProperty property); |
105 | 94 |
106 // Stops all animation and clears any queued animations. | 95 // Stops all animation and clears any queued animations. |
107 void StopAnimating(); | 96 void StopAnimating(); |
108 | 97 |
109 // For testing purposes only. | 98 // For testing purposes only. |
110 void set_disable_timer_for_test(bool enabled) { | 99 void set_disable_timer_for_test(bool enabled) { |
111 disable_timer_for_test_ = enabled; | 100 disable_timer_for_test_ = enabled; |
112 } | 101 } |
113 base::TimeTicks get_last_step_time_for_test() { return last_step_time_; } | 102 base::TimeTicks get_last_step_time_for_test() { return last_step_time_; } |
114 | 103 |
115 // Scoped settings allow you to temporarily change the animator's settings and | |
116 // these changes are reverted when the object is destroyed. NOTE: when the | |
117 // settings object is created, it applies the default transition duration | |
118 // (200ms). | |
119 class ScopedSettings { | |
120 public: | |
121 explicit ScopedSettings(LayerAnimator* animator); | |
122 virtual ~ScopedSettings(); | |
123 | |
124 void SetTransitionDuration(base::TimeDelta duration); | |
125 | |
126 private: | |
127 LayerAnimator* animator_; | |
128 base::TimeDelta old_transition_duration_; | |
129 | |
130 DISALLOW_COPY_AND_ASSIGN(ScopedSettings); | |
131 }; | |
132 | |
133 protected: | 104 protected: |
134 LayerAnimatorDelegate* delegate() { return delegate_; } | 105 LayerAnimationDelegate* delegate() { return delegate_; } |
135 | 106 |
136 private: | 107 private: |
137 friend class TransientSettings; | |
138 | |
139 // We need to keep track of the start time of every running animation. | 108 // We need to keep track of the start time of every running animation. |
140 struct RunningAnimation { | 109 struct RunningAnimation { |
141 RunningAnimation(LayerAnimationSequence* sequence, | 110 RunningAnimation(LayerAnimationSequence* sequence, |
142 base::TimeTicks start_time) | 111 base::TimeTicks start_time) |
143 : sequence(sequence), | 112 : sequence(sequence), |
144 start_time(start_time) { | 113 start_time(start_time) { |
145 } | 114 } |
146 LayerAnimationSequence* sequence; | 115 LayerAnimationSequence* sequence; |
147 base::TimeTicks start_time; | 116 base::TimeTicks start_time; |
148 }; | 117 }; |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
200 // If there's an animation in the queue that doesn't animate the same property | 169 // If there's an animation in the queue that doesn't animate the same property |
201 // as a running animation, or an animation schedule to run before it, start it | 170 // as a running animation, or an animation schedule to run before it, start it |
202 // up. Repeat until there are no such animations. | 171 // up. Repeat until there are no such animations. |
203 void ProcessQueue(); | 172 void ProcessQueue(); |
204 | 173 |
205 // Attempts to add the sequence to the list of running animations. Returns | 174 // Attempts to add the sequence to the list of running animations. Returns |
206 // false if there is an animation running that already animates one of the | 175 // false if there is an animation running that already animates one of the |
207 // properties affected by |sequence|. | 176 // properties affected by |sequence|. |
208 bool StartSequenceImmediately(LayerAnimationSequence* sequence); | 177 bool StartSequenceImmediately(LayerAnimationSequence* sequence); |
209 | 178 |
210 // Sets the value of target as if all the running and queued animations were | |
211 // allowed to finish. | |
212 void GetTargetValue(LayerAnimationElement::TargetValue* target) const; | |
213 | |
214 // This is the queue of animations to run. | 179 // This is the queue of animations to run. |
215 AnimationQueue animation_queue_; | 180 AnimationQueue animation_queue_; |
216 | 181 |
217 // The target of all layer animations. | 182 // The target of all layer animations. |
218 LayerAnimatorDelegate* delegate_; | 183 LayerAnimationDelegate* delegate_; |
219 | 184 |
220 // The currently running animations. | 185 // The currently running animations. |
221 RunningAnimations running_animations_; | 186 RunningAnimations running_animations_; |
222 | 187 |
223 // Determines how animations are replaced. | 188 // Determines how animations are replaced. |
224 PreemptionStrategy preemption_strategy_; | 189 PreemptionStrategy preemption_strategy_; |
225 | 190 |
226 // The default length of animations. | 191 // The default length of animations. |
227 base::TimeDelta transition_duration_; | 192 base::TimeDelta transition_duration_; |
228 | 193 |
229 // Used for coordinating the starting of animations. | 194 // Used for coordinating the starting of animations. |
230 base::TimeTicks last_step_time_; | 195 base::TimeTicks last_step_time_; |
231 | 196 |
232 // True if we are being stepped by our container. | 197 // True if we are being stepped by our container. |
233 bool is_started_; | 198 bool is_started_; |
234 | 199 |
235 // This prevents the animator from automatically stepping through animations | 200 // This prevents the animator from automatically stepping through animations |
236 // and allows for manual stepping. | 201 // and allows for manual stepping. |
237 bool disable_timer_for_test_; | 202 bool disable_timer_for_test_; |
238 | 203 |
239 DISALLOW_COPY_AND_ASSIGN(LayerAnimator); | 204 DISALLOW_COPY_AND_ASSIGN(LayerAnimator); |
240 }; | 205 }; |
241 | 206 |
242 } // namespace ui | 207 } // namespace ui |
243 | 208 |
244 #endif // UI_GFX_COMPOSITOR_LAYER_ANIMATOR_H_ | 209 #endif // UI_GFX_COMPOSITOR_LAYER_ANIMATOR_H_ |
OLD | NEW |