layer_animator.h revision 5d1f7b1de12d16ceb2c938c56701a3e8bfa558f7
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_COMPOSITOR_LAYER_ANIMATOR_H_ 6#define UI_COMPOSITOR_LAYER_ANIMATOR_H_ 7 8#include <deque> 9#include <vector> 10 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" 20 21namespace gfx { 22class Animation; 23class Rect; 24class Transform; 25} 26 27namespace ui { 28class Layer; 29class LayerAnimationSequence; 30class LayerAnimationDelegate; 31class LayerAnimationObserver; 32class ScopedLayerAnimationSettings; 33 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. 43class 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 53 }; 54 55 explicit LayerAnimator(base::TimeDelta transition_duration); 56 57 // No implicit animations when properties are set. 58 static LayerAnimator* CreateDefaultAnimator(); 59 60 // Implicitly animates when properties are set. 61 static LayerAnimator* CreateImplicitAnimator(); 62 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; 66 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; 70 71 // Sets the opacity on the delegate. May cause an implicit animation. 72 virtual void SetOpacity(float opacity); 73 float GetTargetOpacity() const; 74 75 // Sets the visibility of the delegate. May cause an implicit animation. 76 virtual void SetVisibility(bool visibility); 77 bool GetTargetVisibility() const; 78 79 // Sets the brightness on the delegate. May cause an implicit animation. 80 virtual void SetBrightness(float brightness); 81 float GetTargetBrightness() const; 82 83 // Sets the grayscale on the delegate. May cause an implicit animation. 84 virtual void SetGrayscale(float grayscale); 85 float GetTargetGrayscale() const; 86 87 // Sets the color on the delegate. May cause an implicit animation. 88 virtual void SetColor(SkColor color); 89 SkColor GetTargetColor() const; 90 91 // Returns the default length of animations, including adjustment for slow 92 // animation mode if set. 93 base::TimeDelta GetTransitionDuration() const; 94 95 // Sets the layer animation delegate the animator is associated with. The 96 // animator does not own the delegate. The layer animator expects a non-NULL 97 // delegate for most of its operations, so do not call any methods without 98 // a valid delegate installed. 99 void SetDelegate(LayerAnimationDelegate* delegate); 100 101 // Sets the animation preemption strategy. This determines the behaviour if 102 // a property is set during an animation. The default is 103 // IMMEDIATELY_SET_NEW_TARGET (see ImmediatelySetNewTarget below). 104 void set_preemption_strategy(PreemptionStrategy strategy) { 105 preemption_strategy_ = strategy; 106 } 107 108 PreemptionStrategy preemption_strategy() const { 109 return preemption_strategy_; 110 } 111 112 // Start an animation sequence. If an animation for the same property is in 113 // progress, it needs to be interrupted with the new animation. The animator 114 // takes ownership of this animation sequence. 115 void StartAnimation(LayerAnimationSequence* animation); 116 117 // Schedule an animation to be run when possible. The animator takes ownership 118 // of this animation sequence. 119 void ScheduleAnimation(LayerAnimationSequence* animation); 120 121 // Starts the animations to be run together, ensuring that the first elements 122 // in these sequences have the same effective start time even when some of 123 // them start on the compositor thread (but there is no such guarantee for 124 // the effective start time of subsequent elements). Obviously will not work 125 // if they animate any common properties. The animator takes ownership of the 126 // animation sequences. Takes PreemptionStrategy into account. 127 void StartTogether(const std::vector<LayerAnimationSequence*>& animations); 128 129 // Schedules the animations to be run together, ensuring that the first 130 // elements in these sequences have the same effective start time even when 131 // some of them start on the compositor thread (but there is no such guarantee 132 // for the effective start time of subsequent elements). Obviously will not 133 // work if they animate any common properties. The animator takes ownership 134 // of the animation sequences. 135 void ScheduleTogether(const std::vector<LayerAnimationSequence*>& animations); 136 137 // Schedules a pause for length |duration| of all the specified properties. 138 // End the list with -1. 139 void SchedulePauseForProperties( 140 base::TimeDelta duration, 141 LayerAnimationElement::AnimatableProperties properties_to_pause); 142 143 // Returns true if there is an animation in the queue (animations remain in 144 // the queue until they complete, so this includes running animations). 145 bool is_animating() const { return !animation_queue_.empty(); } 146 147 // Returns true if there is an animation in the queue that animates the given 148 // property (animations remain in the queue until they complete, so this 149 // includes running animations). 150 bool IsAnimatingProperty( 151 LayerAnimationElement::AnimatableProperty property) const; 152 153 // Stops animating the given property. No effect if there is no running 154 // animation for the given property. Skips to the final state of the 155 // animation. 156 void StopAnimatingProperty( 157 LayerAnimationElement::AnimatableProperty property); 158 159 // Stops all animation and clears any queued animations. This call progresses 160 // animations to their end points and notifies all observers. 161 void StopAnimating() { StopAnimatingInternal(false); } 162 163 // This is similar to StopAnimating, but aborts rather than finishes the 164 // animations and notifies all observers. 165 void AbortAllAnimations() { StopAnimatingInternal(true); } 166 167 // These functions are used for adding or removing observers from the observer 168 // list. The observers are notified when animations end. 169 void AddObserver(LayerAnimationObserver* observer); 170 void RemoveObserver(LayerAnimationObserver* observer); 171 172 // Called when a threaded animation is actually started. 173 void OnThreadedAnimationStarted(const cc::AnimationEvent& event); 174 175 // This determines how implicit animations will be tweened. This has no 176 // effect on animations that are explicitly started or scheduled. The default 177 // is Tween::LINEAR. 178 void set_tween_type(gfx::Tween::Type tween_type) { tween_type_ = tween_type; } 179 gfx::Tween::Type tween_type() const { return tween_type_; } 180 181 // For testing purposes only. 182 void set_disable_timer_for_test(bool disable_timer) { 183 disable_timer_for_test_ = disable_timer; 184 } 185 186 void set_last_step_time(base::TimeTicks time) { 187 last_step_time_ = time; 188 } 189 base::TimeTicks last_step_time() const { return last_step_time_; } 190 191 protected: 192 virtual ~LayerAnimator(); 193 194 LayerAnimationDelegate* delegate() { return delegate_; } 195 const LayerAnimationDelegate* delegate() const { return delegate_; } 196 197 // Virtual for testing. 198 virtual void ProgressAnimation(LayerAnimationSequence* sequence, 199 base::TimeTicks now); 200 201 void ProgressAnimationToEnd(LayerAnimationSequence* sequence); 202 203 // Returns true if the sequence is owned by this animator. 204 bool HasAnimation(LayerAnimationSequence* sequence) const; 205 206 private: 207 friend class base::RefCounted<LayerAnimator>; 208 friend class ScopedLayerAnimationSettings; 209 friend class LayerAnimatorTestController; 210 211 class RunningAnimation { 212 public: 213 RunningAnimation(const base::WeakPtr<LayerAnimationSequence>& sequence); 214 ~RunningAnimation(); 215 216 bool is_sequence_alive() const { return !!sequence_.get(); } 217 LayerAnimationSequence* sequence() const { return sequence_.get(); } 218 219 private: 220 base::WeakPtr<LayerAnimationSequence> sequence_; 221 222 // Copy and assign are allowed. 223 }; 224 225 typedef std::vector<RunningAnimation> RunningAnimations; 226 typedef std::deque<linked_ptr<LayerAnimationSequence> > AnimationQueue; 227 228 // Implementation of AnimationContainerElement 229 virtual void SetStartTime(base::TimeTicks start_time) OVERRIDE; 230 virtual void Step(base::TimeTicks time_now) OVERRIDE; 231 virtual base::TimeDelta GetTimerInterval() const OVERRIDE; 232 233 // Finishes all animations by either advancing them to their final state or by 234 // aborting them. 235 void StopAnimatingInternal(bool abort); 236 237 // Starts or stops stepping depending on whether thare are running animations. 238 void UpdateAnimationState(); 239 240 // Removes the sequences from both the running animations and the queue. 241 // Returns a pointer to the removed animation, if any. NOTE: the caller is 242 // responsible for deleting the returned pointer. 243 LayerAnimationSequence* RemoveAnimation( 244 LayerAnimationSequence* sequence) WARN_UNUSED_RESULT; 245 246 // Progresses to the end of the sequence before removing it. 247 void FinishAnimation(LayerAnimationSequence* sequence, bool abort); 248 249 // Finishes any running animation with zero duration. 250 void FinishAnyAnimationWithZeroDuration(); 251 252 // Clears the running animations and the queue. No sequences are progressed. 253 void ClearAnimations(); 254 255 // Returns the running animation animating the given property, if any. 256 RunningAnimation* GetRunningAnimation( 257 LayerAnimationElement::AnimatableProperty property); 258 259 // Checks if the sequence has already been added to the queue and adds it 260 // to the front if note. 261 void AddToQueueIfNotPresent(LayerAnimationSequence* sequence); 262 263 // Any running or queued animation that affects a property in common with 264 // |sequence| is either finished or aborted depending on |abort|. 265 void RemoveAllAnimationsWithACommonProperty(LayerAnimationSequence* sequence, 266 bool abort); 267 268 // Preempts a running animation by progressing both the running animation and 269 // the given sequence to the end. 270 void ImmediatelySetNewTarget(LayerAnimationSequence* sequence); 271 272 // Preempts by aborting the running animation, and starts the given animation. 273 void ImmediatelyAnimateToNewTarget(LayerAnimationSequence* sequence); 274 275 // Preempts by adding the new animation to the queue. 276 void EnqueueNewAnimation(LayerAnimationSequence* sequence); 277 278 // Preempts by wiping out any unstarted animation in the queue and then 279 // enqueuing this animation. 280 void ReplaceQueuedAnimations(LayerAnimationSequence* sequence); 281 282 // If there's an animation in the queue that doesn't animate the same property 283 // as a running animation, or an animation schedule to run before it, start it 284 // up. Repeat until there are no such animations. 285 void ProcessQueue(); 286 287 // Attempts to add the sequence to the list of running animations. Returns 288 // false if there is an animation running that already animates one of the 289 // properties affected by |sequence|. 290 bool StartSequenceImmediately(LayerAnimationSequence* sequence); 291 292 // Sets the value of target as if all the running and queued animations were 293 // allowed to finish. 294 void GetTargetValue(LayerAnimationElement::TargetValue* target) const; 295 296 // Called whenever an animation is added to the animation queue. Either by 297 // starting the animation or adding to the queue. 298 void OnScheduled(LayerAnimationSequence* sequence); 299 300 // Sets |transition_duration_| unless |is_transition_duration_locked_| is set. 301 void SetTransitionDuration(base::TimeDelta duration); 302 303 // Clears the animation queues and notifies any running animations that they 304 // have been aborted. 305 void ClearAnimationsInternal(); 306 307 // Cleans up any running animations that may have been deleted. 308 void PurgeDeletedAnimations(); 309 310 // This is the queue of animations to run. 311 AnimationQueue animation_queue_; 312 313 // The target of all layer animations. 314 LayerAnimationDelegate* delegate_; 315 316 // The currently running animations. 317 RunningAnimations running_animations_; 318 319 // Determines how animations are replaced. 320 PreemptionStrategy preemption_strategy_; 321 322 // Whether the length of animations is locked. While it is locked 323 // SetTransitionDuration does not set |transition_duration_|. 324 bool is_transition_duration_locked_; 325 326 // The default length of animations. 327 base::TimeDelta transition_duration_; 328 329 // The default tween type for implicit transitions 330 gfx::Tween::Type tween_type_; 331 332 // Used for coordinating the starting of animations. 333 base::TimeTicks last_step_time_; 334 335 // True if we are being stepped by our container. 336 bool is_started_; 337 338 // This prevents the animator from automatically stepping through animations 339 // and allows for manual stepping. 340 bool disable_timer_for_test_; 341 342 // Prevents timer adjustments in case when we start multiple animations 343 // with preemption strategies that discard previous animations. 344 bool adding_animations_; 345 346 // Observers are notified when layer animations end, are scheduled or are 347 // aborted. 348 ObserverList<LayerAnimationObserver> observers_; 349 350 DISALLOW_COPY_AND_ASSIGN(LayerAnimator); 351}; 352 353} // namespace ui 354 355#endif // UI_COMPOSITOR_LAYER_ANIMATOR_H_ 356