OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #ifndef UI_GFX_COMPOSITOR_LAYER_ANIMATOR_H_ | |
6 #define UI_GFX_COMPOSITOR_LAYER_ANIMATOR_H_ | |
7 #pragma once | |
8 | |
9 #include <deque> | |
10 #include <vector> | |
11 | |
12 #include "base/compiler_specific.h" | |
13 #include "base/memory/linked_ptr.h" | |
14 #include "base/memory/scoped_ptr.h" | |
15 #include "base/observer_list.h" | |
16 #include "base/time.h" | |
17 #include "ui/base/animation/animation_container_element.h" | |
18 #include "ui/base/animation/tween.h" | |
19 #include "ui/gfx/compositor/compositor_export.h" | |
20 #include "ui/gfx/compositor/layer_animation_element.h" | |
21 | |
22 namespace gfx { | |
23 class Rect; | |
24 } | |
25 | |
26 namespace ui { | |
27 class Animation; | |
28 class Layer; | |
29 class LayerAnimationSequence; | |
30 class LayerAnimationDelegate; | |
31 class LayerAnimationObserver; | |
32 class ScopedLayerAnimationSettings; | |
33 class Transform; | |
34 | |
35 // When a property of layer needs to be changed it is set by way of | |
36 // LayerAnimator. This enables LayerAnimator to animate property changes. | |
37 // NB: during many tests, set_disable_animations_for_test is used and causes | |
38 // all animations to complete immediately. | |
39 class COMPOSITOR_EXPORT LayerAnimator : public AnimationContainerElement { | |
40 public: | |
41 enum PreemptionStrategy { | |
42 IMMEDIATELY_SET_NEW_TARGET, | |
43 IMMEDIATELY_ANIMATE_TO_NEW_TARGET, | |
44 ENQUEUE_NEW_ANIMATION, | |
45 REPLACE_QUEUED_ANIMATIONS, | |
46 BLEND_WITH_CURRENT_ANIMATION | |
47 }; | |
48 | |
49 explicit LayerAnimator(base::TimeDelta transition_duration); | |
50 virtual ~LayerAnimator(); | |
51 | |
52 // No implicit animations when properties are set. | |
53 static LayerAnimator* CreateDefaultAnimator(); | |
54 | |
55 // Implicitly animates when properties are set. | |
56 static LayerAnimator* CreateImplicitAnimator(); | |
57 | |
58 // Sets the transform on the delegate. May cause an implicit animation. | |
59 virtual void SetTransform(const Transform& transform); | |
60 Transform GetTargetTransform() const; | |
61 | |
62 // Sets the bounds on the delegate. May cause an implicit animation. | |
63 virtual void SetBounds(const gfx::Rect& bounds); | |
64 gfx::Rect GetTargetBounds() const; | |
65 | |
66 // Sets the opacity on the delegate. May cause an implicit animation. | |
67 virtual void SetOpacity(float opacity); | |
68 float GetTargetOpacity() const; | |
69 | |
70 // Sets the visibility of the delegate. May cause an implicit animation. | |
71 virtual void SetVisibility(bool visibility); | |
72 bool GetTargetVisibility() const; | |
73 | |
74 // Sets the layer animation delegate the animator is associated with. The | |
75 // animator does not own the delegate. | |
76 void SetDelegate(LayerAnimationDelegate* delegate); | |
77 | |
78 // Sets the animation preemption strategy. This determines the behaviour if | |
79 // a property is set during an animation. The default is | |
80 // IMMEDIATELY_SET_NEW_TARGET (see ImmediatelySetNewTarget below). | |
81 void set_preemption_strategy(PreemptionStrategy strategy) { | |
82 preemption_strategy_ = strategy; | |
83 } | |
84 | |
85 PreemptionStrategy preemption_strategy() const { | |
86 return preemption_strategy_; | |
87 } | |
88 | |
89 // Start an animation sequence. If an animation for the same property is in | |
90 // progress, it needs to be interrupted with the new animation. The animator | |
91 // takes ownership of this animation sequence. | |
92 void StartAnimation(LayerAnimationSequence* animation); | |
93 | |
94 // Schedule an animation to be run when possible. The animator takes ownership | |
95 // of this animation sequence. | |
96 void ScheduleAnimation(LayerAnimationSequence* animation); | |
97 | |
98 // Schedules the animations to be run together. Obviously will no work if | |
99 // they animate any common properties. The animator takes ownership of the | |
100 // animation sequences. | |
101 void ScheduleTogether(const std::vector<LayerAnimationSequence*>& animations); | |
102 | |
103 // Returns true if there is an animation in the queue (animations remain in | |
104 // the queue until they complete, so this includes running animations). | |
105 bool is_animating() const { return !animation_queue_.empty(); } | |
106 | |
107 // Returns true if there is an animation in the queue that animates the given | |
108 // property (animations remain in the queue until they complete, so this | |
109 // includes running animations). | |
110 bool IsAnimatingProperty( | |
111 LayerAnimationElement::AnimatableProperty property) const; | |
112 | |
113 // Stops animating the given property. No effect if there is no running | |
114 // animation for the given property. Skips to the final state of the | |
115 // animation. | |
116 void StopAnimatingProperty( | |
117 LayerAnimationElement::AnimatableProperty property); | |
118 | |
119 // Stops all animation and clears any queued animations. | |
120 void StopAnimating(); | |
121 | |
122 // These functions are used for adding or removing observers from the observer | |
123 // list. The observers are notified when animations end. | |
124 void AddObserver(LayerAnimationObserver* observer); | |
125 void RemoveObserver(LayerAnimationObserver* observer); | |
126 | |
127 // This determines how implicit animations will be tweened. This has no | |
128 // effect on animations that are explicitly started or scheduled. The default | |
129 // is Tween::LINEAR. | |
130 void set_tween_type(Tween::Type tween_type) { tween_type_ = tween_type; } | |
131 Tween::Type tween_type() const { return tween_type_; } | |
132 | |
133 // For testing purposes only. | |
134 void set_disable_timer_for_test(bool disable_timer) { | |
135 disable_timer_for_test_ = disable_timer; | |
136 } | |
137 base::TimeTicks last_step_time() const { return last_step_time_; } | |
138 | |
139 // When set to true, all animations complete immediately. | |
140 static void set_disable_animations_for_test(bool disable_animations) { | |
141 disable_animations_for_test_ = disable_animations; | |
142 } | |
143 | |
144 static bool disable_animations_for_test() { | |
145 return disable_animations_for_test_; | |
146 } | |
147 | |
148 protected: | |
149 LayerAnimationDelegate* delegate() { return delegate_; } | |
150 const LayerAnimationDelegate* delegate() const { return delegate_; } | |
151 | |
152 // Virtual for testing. | |
153 virtual bool ProgressAnimation(LayerAnimationSequence* sequence, | |
154 base::TimeDelta delta); | |
155 | |
156 // Returns true if the sequence is owned by this animator. | |
157 bool HasAnimation(LayerAnimationSequence* sequence) const; | |
158 | |
159 private: | |
160 friend class ScopedLayerAnimationSettings; | |
161 | |
162 // We need to keep track of the start time of every running animation. | |
163 struct RunningAnimation { | |
164 RunningAnimation(LayerAnimationSequence* sequence, | |
165 base::TimeTicks start_time) | |
166 : sequence(sequence), | |
167 start_time(start_time) { | |
168 } | |
169 LayerAnimationSequence* sequence; | |
170 base::TimeTicks start_time; | |
171 }; | |
172 | |
173 typedef std::vector<RunningAnimation> RunningAnimations; | |
174 typedef std::deque<linked_ptr<LayerAnimationSequence> > AnimationQueue; | |
175 | |
176 // Implementation of AnimationContainerElement | |
177 virtual void SetStartTime(base::TimeTicks start_time) OVERRIDE; | |
178 virtual void Step(base::TimeTicks time_now) OVERRIDE; | |
179 virtual base::TimeDelta GetTimerInterval() const OVERRIDE; | |
180 | |
181 // Starts or stops stepping depending on whether thare are running animations. | |
182 void UpdateAnimationState(); | |
183 | |
184 // Removes the sequences from both the running animations and the queue. | |
185 // Returns a pointer to the removed animation, if any. NOTE: the caller is | |
186 // responsible for deleting the returned pointer. | |
187 LayerAnimationSequence* RemoveAnimation( | |
188 LayerAnimationSequence* sequence) WARN_UNUSED_RESULT; | |
189 | |
190 // Progresses to the end of the sequence before removing it. | |
191 void FinishAnimation(LayerAnimationSequence* sequence); | |
192 | |
193 // Finishes any running animation with zero duration. | |
194 void FinishAnyAnimationWithZeroDuration(); | |
195 | |
196 // Clears the running animations and the queue. No sequences are progressed. | |
197 void ClearAnimations(); | |
198 | |
199 // Returns the running animation animating the given property, if any. | |
200 RunningAnimation* GetRunningAnimation( | |
201 LayerAnimationElement::AnimatableProperty property); | |
202 | |
203 // Checks if the sequence has already been added to the queue and adds it | |
204 // to the front if note. | |
205 void AddToQueueIfNotPresent(LayerAnimationSequence* sequence); | |
206 | |
207 // Any running or queued animation that affects a property in common with | |
208 // |sequence| is either finished or aborted depending on |abort|. | |
209 void RemoveAllAnimationsWithACommonProperty(LayerAnimationSequence* sequence, | |
210 bool abort); | |
211 | |
212 // Preempts a running animation by progressing both the running animation and | |
213 // the given sequence to the end. | |
214 void ImmediatelySetNewTarget(LayerAnimationSequence* sequence); | |
215 | |
216 // Preempts by aborting the running animation, and starts the given animation. | |
217 void ImmediatelyAnimateToNewTarget(LayerAnimationSequence* sequence); | |
218 | |
219 // Preempts by adding the new animation to the queue. | |
220 void EnqueueNewAnimation(LayerAnimationSequence* sequence); | |
221 | |
222 // Preempts by wiping out any unstarted animation in the queue and then | |
223 // enqueuing this animation. | |
224 void ReplaceQueuedAnimations(LayerAnimationSequence* sequence); | |
225 | |
226 // If there's an animation in the queue that doesn't animate the same property | |
227 // as a running animation, or an animation schedule to run before it, start it | |
228 // up. Repeat until there are no such animations. | |
229 void ProcessQueue(); | |
230 | |
231 // Attempts to add the sequence to the list of running animations. Returns | |
232 // false if there is an animation running that already animates one of the | |
233 // properties affected by |sequence|. | |
234 bool StartSequenceImmediately(LayerAnimationSequence* sequence); | |
235 | |
236 // Sets the value of target as if all the running and queued animations were | |
237 // allowed to finish. | |
238 void GetTargetValue(LayerAnimationElement::TargetValue* target) const; | |
239 | |
240 // Called whenever an animation is added to the animation queue. Either by | |
241 // starting the animation or adding to the queue. | |
242 void OnScheduled(LayerAnimationSequence* sequence); | |
243 | |
244 // This is the queue of animations to run. | |
245 AnimationQueue animation_queue_; | |
246 | |
247 // The target of all layer animations. | |
248 LayerAnimationDelegate* delegate_; | |
249 | |
250 // The currently running animations. | |
251 RunningAnimations running_animations_; | |
252 | |
253 // Determines how animations are replaced. | |
254 PreemptionStrategy preemption_strategy_; | |
255 | |
256 // The default length of animations. | |
257 base::TimeDelta transition_duration_; | |
258 | |
259 // The default tween type for implicit transitions | |
260 Tween::Type tween_type_; | |
261 | |
262 // Used for coordinating the starting of animations. | |
263 base::TimeTicks last_step_time_; | |
264 | |
265 // True if we are being stepped by our container. | |
266 bool is_started_; | |
267 | |
268 // This prevents the animator from automatically stepping through animations | |
269 // and allows for manual stepping. | |
270 bool disable_timer_for_test_; | |
271 | |
272 // This causes all animations to complete immediately. | |
273 static bool disable_animations_for_test_; | |
274 | |
275 // Observers are notified when layer animations end, are scheduled or are | |
276 // aborted. | |
277 ObserverList<LayerAnimationObserver> observers_; | |
278 | |
279 DISALLOW_COPY_AND_ASSIGN(LayerAnimator); | |
280 }; | |
281 | |
282 } // namespace ui | |
283 | |
284 #endif // UI_GFX_COMPOSITOR_LAYER_ANIMATOR_H_ | |
OLD | NEW |