Remove WebKitTestRunner::setClientWindowRect.
[chromium-blink-merge.git] / ui / compositor / layer_animator.h
blob5fe0d5c189617d8434fee88cb7e20c0bf8cec99a
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.
5 #ifndef UI_COMPOSITOR_LAYER_ANIMATOR_H_
6 #define UI_COMPOSITOR_LAYER_ANIMATOR_H_
8 #include <deque>
9 #include <vector>
11 #include "base/compiler_specific.h"
12 #include "base/memory/linked_ptr.h"
13 #include "base/memory/ref_counted.h"
14 #include "base/observer_list.h"
15 #include "base/time/time.h"
16 #include "ui/compositor/compositor_export.h"
17 #include "ui/compositor/layer_animation_element.h"
18 #include "ui/gfx/animation/animation_container_element.h"
19 #include "ui/gfx/animation/tween.h"
21 namespace gfx {
22 class Animation;
23 class Rect;
24 class Transform;
27 namespace ui {
28 class Layer;
29 class LayerAnimationSequence;
30 class LayerAnimationDelegate;
31 class LayerAnimationObserver;
32 class ScopedLayerAnimationSettings;
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.
36 // NB: during many tests, set_disable_animations_for_test is used and causes
37 // all animations to complete immediately. The layer animation is ref counted
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 gfx::AnimationContainerElement,
45 public base::RefCounted<LayerAnimator> {
46 public:
47 enum PreemptionStrategy {
48 IMMEDIATELY_SET_NEW_TARGET,
49 IMMEDIATELY_ANIMATE_TO_NEW_TARGET,
50 ENQUEUE_NEW_ANIMATION,
51 REPLACE_QUEUED_ANIMATIONS,
52 BLEND_WITH_CURRENT_ANIMATION
55 explicit LayerAnimator(base::TimeDelta transition_duration);
57 // No implicit animations when properties are set.
58 static LayerAnimator* CreateDefaultAnimator();
60 // Implicitly animates when properties are set.
61 static LayerAnimator* CreateImplicitAnimator();
63 // Sets the transform on the delegate. May cause an implicit animation.
64 virtual void SetTransform(const gfx::Transform& transform);
65 gfx::Transform GetTargetTransform() const;
67 // Sets the bounds on the delegate. May cause an implicit animation.
68 virtual void SetBounds(const gfx::Rect& bounds);
69 gfx::Rect GetTargetBounds() const;
71 // Sets the opacity on the delegate. May cause an implicit animation.
72 virtual void SetOpacity(float opacity);
73 float GetTargetOpacity() const;
75 // Sets the visibility of the delegate. May cause an implicit animation.
76 virtual void SetVisibility(bool visibility);
77 bool GetTargetVisibility() const;
79 // Sets the brightness on the delegate. May cause an implicit animation.
80 virtual void SetBrightness(float brightness);
81 float GetTargetBrightness() const;
83 // Sets the grayscale on the delegate. May cause an implicit animation.
84 virtual void SetGrayscale(float grayscale);
85 float GetTargetGrayscale() const;
87 // Sets the color on the delegate. May cause an implicit animation.
88 virtual void SetColor(SkColor color);
89 SkColor GetTargetColor() const;
91 // Sets the layer animation delegate the animator is associated with. The
92 // animator does not own the delegate. The layer animator expects a non-NULL
93 // delegate for most of its operations, so do not call any methods without
94 // a valid delegate installed.
95 void SetDelegate(LayerAnimationDelegate* delegate);
97 // Sets the animation preemption strategy. This determines the behaviour if
98 // a property is set during an animation. The default is
99 // IMMEDIATELY_SET_NEW_TARGET (see ImmediatelySetNewTarget below).
100 void set_preemption_strategy(PreemptionStrategy strategy) {
101 preemption_strategy_ = strategy;
104 PreemptionStrategy preemption_strategy() const {
105 return preemption_strategy_;
108 // Start an animation sequence. If an animation for the same property is in
109 // progress, it needs to be interrupted with the new animation. The animator
110 // takes ownership of this animation sequence.
111 void StartAnimation(LayerAnimationSequence* animation);
113 // Schedule an animation to be run when possible. The animator takes ownership
114 // of this animation sequence.
115 void ScheduleAnimation(LayerAnimationSequence* animation);
117 // Starts the animations to be run together, ensuring that the first elements
118 // in these sequences have the same effective start time even when some of
119 // them start on the compositor thread (but there is no such guarantee for
120 // the effective start time of subsequent elements). Obviously will not work
121 // if they animate any common properties. The animator takes ownership of the
122 // animation sequences. Takes PreemptionStrategy into account.
123 void StartTogether(const std::vector<LayerAnimationSequence*>& animations);
125 // Schedules the animations to be run together, ensuring that the first
126 // elements in these sequences have the same effective start time even when
127 // some of them start on the compositor thread (but there is no such guarantee
128 // for the effective start time of subsequent elements). Obviously will not
129 // work if they animate any common properties. The animator takes ownership
130 // of the animation sequences.
131 void ScheduleTogether(const std::vector<LayerAnimationSequence*>& animations);
133 // Schedules a pause for length |duration| of all the specified properties.
134 // End the list with -1.
135 void SchedulePauseForProperties(
136 base::TimeDelta duration,
137 LayerAnimationElement::AnimatableProperty property,
138 ...);
140 // Returns true if there is an animation in the queue (animations remain in
141 // the queue until they complete, so this includes running animations).
142 bool is_animating() const { return !animation_queue_.empty(); }
144 // Returns true if there is an animation in the queue that animates the given
145 // property (animations remain in the queue until they complete, so this
146 // includes running animations).
147 bool IsAnimatingProperty(
148 LayerAnimationElement::AnimatableProperty property) const;
150 // Stops animating the given property. No effect if there is no running
151 // animation for the given property. Skips to the final state of the
152 // animation.
153 void StopAnimatingProperty(
154 LayerAnimationElement::AnimatableProperty property);
156 // Stops all animation and clears any queued animations. This call progresses
157 // animations to their end points and notifies all observers.
158 void StopAnimating() { StopAnimatingInternal(false); }
160 // This is similar to StopAnimating, but aborts rather than finishes the
161 // animations and notifies all observers.
162 void AbortAllAnimations() { StopAnimatingInternal(true); }
164 // These functions are used for adding or removing observers from the observer
165 // list. The observers are notified when animations end.
166 void AddObserver(LayerAnimationObserver* observer);
167 void RemoveObserver(LayerAnimationObserver* observer);
169 // Called when a threaded animation is actually started.
170 void OnThreadedAnimationStarted(const cc::AnimationEvent& event);
172 // This determines how implicit animations will be tweened. This has no
173 // effect on animations that are explicitly started or scheduled. The default
174 // is Tween::LINEAR.
175 void set_tween_type(gfx::Tween::Type tween_type) { tween_type_ = tween_type; }
176 gfx::Tween::Type tween_type() const { return tween_type_; }
178 // For testing purposes only.
179 void set_disable_timer_for_test(bool disable_timer) {
180 disable_timer_for_test_ = disable_timer;
183 void set_last_step_time(base::TimeTicks time) {
184 last_step_time_ = time;
186 base::TimeTicks last_step_time() const { return last_step_time_; }
188 protected:
189 virtual ~LayerAnimator();
191 LayerAnimationDelegate* delegate() { return delegate_; }
192 const LayerAnimationDelegate* delegate() const { return delegate_; }
194 // Virtual for testing.
195 virtual void ProgressAnimation(LayerAnimationSequence* sequence,
196 base::TimeTicks now);
198 void ProgressAnimationToEnd(LayerAnimationSequence* sequence);
200 // Returns true if the sequence is owned by this animator.
201 bool HasAnimation(LayerAnimationSequence* sequence) const;
203 private:
204 friend class base::RefCounted<LayerAnimator>;
205 friend class ScopedLayerAnimationSettings;
206 friend class LayerAnimatorTestController;
208 class RunningAnimation {
209 public:
210 RunningAnimation(const base::WeakPtr<LayerAnimationSequence>& sequence);
211 ~RunningAnimation();
213 bool is_sequence_alive() const { return !!sequence_.get(); }
214 LayerAnimationSequence* sequence() const { return sequence_.get(); }
216 private:
217 base::WeakPtr<LayerAnimationSequence> sequence_;
219 // Copy and assign are allowed.
222 typedef std::vector<RunningAnimation> RunningAnimations;
223 typedef std::deque<linked_ptr<LayerAnimationSequence> > AnimationQueue;
225 // Implementation of AnimationContainerElement
226 virtual void SetStartTime(base::TimeTicks start_time) OVERRIDE;
227 virtual void Step(base::TimeTicks time_now) OVERRIDE;
228 virtual base::TimeDelta GetTimerInterval() const OVERRIDE;
230 // Finishes all animations by either advancing them to their final state or by
231 // aborting them.
232 void StopAnimatingInternal(bool abort);
234 // Starts or stops stepping depending on whether thare are running animations.
235 void UpdateAnimationState();
237 // Removes the sequences from both the running animations and the queue.
238 // Returns a pointer to the removed animation, if any. NOTE: the caller is
239 // responsible for deleting the returned pointer.
240 LayerAnimationSequence* RemoveAnimation(
241 LayerAnimationSequence* sequence) WARN_UNUSED_RESULT;
243 // Progresses to the end of the sequence before removing it.
244 void FinishAnimation(LayerAnimationSequence* sequence, bool abort);
246 // Finishes any running animation with zero duration.
247 void FinishAnyAnimationWithZeroDuration();
249 // Clears the running animations and the queue. No sequences are progressed.
250 void ClearAnimations();
252 // Returns the running animation animating the given property, if any.
253 RunningAnimation* GetRunningAnimation(
254 LayerAnimationElement::AnimatableProperty property);
256 // Checks if the sequence has already been added to the queue and adds it
257 // to the front if note.
258 void AddToQueueIfNotPresent(LayerAnimationSequence* sequence);
260 // Any running or queued animation that affects a property in common with
261 // |sequence| is either finished or aborted depending on |abort|.
262 void RemoveAllAnimationsWithACommonProperty(LayerAnimationSequence* sequence,
263 bool abort);
265 // Preempts a running animation by progressing both the running animation and
266 // the given sequence to the end.
267 void ImmediatelySetNewTarget(LayerAnimationSequence* sequence);
269 // Preempts by aborting the running animation, and starts the given animation.
270 void ImmediatelyAnimateToNewTarget(LayerAnimationSequence* sequence);
272 // Preempts by adding the new animation to the queue.
273 void EnqueueNewAnimation(LayerAnimationSequence* sequence);
275 // Preempts by wiping out any unstarted animation in the queue and then
276 // enqueuing this animation.
277 void ReplaceQueuedAnimations(LayerAnimationSequence* sequence);
279 // If there's an animation in the queue that doesn't animate the same property
280 // as a running animation, or an animation schedule to run before it, start it
281 // up. Repeat until there are no such animations.
282 void ProcessQueue();
284 // Attempts to add the sequence to the list of running animations. Returns
285 // false if there is an animation running that already animates one of the
286 // properties affected by |sequence|.
287 bool StartSequenceImmediately(LayerAnimationSequence* sequence);
289 // Sets the value of target as if all the running and queued animations were
290 // allowed to finish.
291 void GetTargetValue(LayerAnimationElement::TargetValue* target) const;
293 // Called whenever an animation is added to the animation queue. Either by
294 // starting the animation or adding to the queue.
295 void OnScheduled(LayerAnimationSequence* sequence);
297 // Returns the default length of animations, including adjustment for slow
298 // animation mode if set.
299 base::TimeDelta GetTransitionDuration() const;
301 // Clears the animation queues and notifies any running animations that they
302 // have been aborted.
303 void ClearAnimationsInternal();
305 // Cleans up any running animations that may have been deleted.
306 void PurgeDeletedAnimations();
308 // This is the queue of animations to run.
309 AnimationQueue animation_queue_;
311 // The target of all layer animations.
312 LayerAnimationDelegate* delegate_;
314 // The currently running animations.
315 RunningAnimations running_animations_;
317 // Determines how animations are replaced.
318 PreemptionStrategy preemption_strategy_;
320 // The default length of animations.
321 base::TimeDelta transition_duration_;
323 // The default tween type for implicit transitions
324 gfx::Tween::Type tween_type_;
326 // Used for coordinating the starting of animations.
327 base::TimeTicks last_step_time_;
329 // True if we are being stepped by our container.
330 bool is_started_;
332 // This prevents the animator from automatically stepping through animations
333 // and allows for manual stepping.
334 bool disable_timer_for_test_;
336 // Prevents timer adjustments in case when we start multiple animations
337 // with preemption strategies that discard previous animations.
338 bool adding_animations_;
340 // Observers are notified when layer animations end, are scheduled or are
341 // aborted.
342 ObserverList<LayerAnimationObserver> observers_;
344 DISALLOW_COPY_AND_ASSIGN(LayerAnimator);
347 } // namespace ui
349 #endif // UI_COMPOSITOR_LAYER_ANIMATOR_H_