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 21 namespace gfx { 22 class Animation; 23 class Rect; 24 class Transform; 25 } 26 27 namespace ui { 28 class Layer; 29 class LayerAnimationSequence; 30 class LayerAnimationDelegate; 31 class LayerAnimationObserver; 32 class 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. 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 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::AnimatableProperty property, 142 ...); 143 144 // Returns true if there is an animation in the queue (animations remain in 145 // the queue until they complete, so this includes running animations). 146 bool is_animating() const { return !animation_queue_.empty(); } 147 148 // Returns true if there is an animation in the queue that animates the given 149 // property (animations remain in the queue until they complete, so this 150 // includes running animations). 151 bool IsAnimatingProperty( 152 LayerAnimationElement::AnimatableProperty property) const; 153 154 // Stops animating the given property. No effect if there is no running 155 // animation for the given property. Skips to the final state of the 156 // animation. 157 void StopAnimatingProperty( 158 LayerAnimationElement::AnimatableProperty property); 159 160 // Stops all animation and clears any queued animations. This call progresses 161 // animations to their end points and notifies all observers. 162 void StopAnimating() { StopAnimatingInternal(false); } 163 164 // This is similar to StopAnimating, but aborts rather than finishes the 165 // animations and notifies all observers. 166 void AbortAllAnimations() { StopAnimatingInternal(true); } 167 168 // These functions are used for adding or removing observers from the observer 169 // list. The observers are notified when animations end. 170 void AddObserver(LayerAnimationObserver* observer); 171 void RemoveObserver(LayerAnimationObserver* observer); 172 173 // Called when a threaded animation is actually started. 174 void OnThreadedAnimationStarted(const cc::AnimationEvent& event); 175 176 // This determines how implicit animations will be tweened. This has no 177 // effect on animations that are explicitly started or scheduled. The default 178 // is Tween::LINEAR. 179 void set_tween_type(gfx::Tween::Type tween_type) { tween_type_ = tween_type; } 180 gfx::Tween::Type tween_type() const { return tween_type_; } 181 182 // For testing purposes only. 183 void set_disable_timer_for_test(bool disable_timer) { 184 disable_timer_for_test_ = disable_timer; 185 } 186 187 void set_last_step_time(base::TimeTicks time) { 188 last_step_time_ = time; 189 } 190 base::TimeTicks last_step_time() const { return last_step_time_; } 191 192 protected: 193 virtual ~LayerAnimator(); 194 195 LayerAnimationDelegate* delegate() { return delegate_; } 196 const LayerAnimationDelegate* delegate() const { return delegate_; } 197 198 // Virtual for testing. 199 virtual void ProgressAnimation(LayerAnimationSequence* sequence, 200 base::TimeTicks now); 201 202 void ProgressAnimationToEnd(LayerAnimationSequence* sequence); 203 204 // Returns true if the sequence is owned by this animator. 205 bool HasAnimation(LayerAnimationSequence* sequence) const; 206 207 private: 208 friend class base::RefCounted<LayerAnimator>; 209 friend class ScopedLayerAnimationSettings; 210 friend class LayerAnimatorTestController; 211 212 class RunningAnimation { 213 public: 214 RunningAnimation(const base::WeakPtr<LayerAnimationSequence>& sequence); 215 ~RunningAnimation(); 216 217 bool is_sequence_alive() const { return !!sequence_.get(); } 218 LayerAnimationSequence* sequence() const { return sequence_.get(); } 219 220 private: 221 base::WeakPtr<LayerAnimationSequence> sequence_; 222 223 // Copy and assign are allowed. 224 }; 225 226 typedef std::vector<RunningAnimation> RunningAnimations; 227 typedef std::deque<linked_ptr<LayerAnimationSequence> > AnimationQueue; 228 229 // Implementation of AnimationContainerElement 230 virtual void SetStartTime(base::TimeTicks start_time) OVERRIDE; 231 virtual void Step(base::TimeTicks time_now) OVERRIDE; 232 virtual base::TimeDelta GetTimerInterval() const OVERRIDE; 233 234 // Finishes all animations by either advancing them to their final state or by 235 // aborting them. 236 void StopAnimatingInternal(bool abort); 237 238 // Starts or stops stepping depending on whether thare are running animations. 239 void UpdateAnimationState(); 240 241 // Removes the sequences from both the running animations and the queue. 242 // Returns a pointer to the removed animation, if any. NOTE: the caller is 243 // responsible for deleting the returned pointer. 244 LayerAnimationSequence* RemoveAnimation( 245 LayerAnimationSequence* sequence) WARN_UNUSED_RESULT; 246 247 // Progresses to the end of the sequence before removing it. 248 void FinishAnimation(LayerAnimationSequence* sequence, bool abort); 249 250 // Finishes any running animation with zero duration. 251 void FinishAnyAnimationWithZeroDuration(); 252 253 // Clears the running animations and the queue. No sequences are progressed. 254 void ClearAnimations(); 255 256 // Returns the running animation animating the given property, if any. 257 RunningAnimation* GetRunningAnimation( 258 LayerAnimationElement::AnimatableProperty property); 259 260 // Checks if the sequence has already been added to the queue and adds it 261 // to the front if note. 262 void AddToQueueIfNotPresent(LayerAnimationSequence* sequence); 263 264 // Any running or queued animation that affects a property in common with 265 // |sequence| is either finished or aborted depending on |abort|. 266 void RemoveAllAnimationsWithACommonProperty(LayerAnimationSequence* sequence, 267 bool abort); 268 269 // Preempts a running animation by progressing both the running animation and 270 // the given sequence to the end. 271 void ImmediatelySetNewTarget(LayerAnimationSequence* sequence); 272 273 // Preempts by aborting the running animation, and starts the given animation. 274 void ImmediatelyAnimateToNewTarget(LayerAnimationSequence* sequence); 275 276 // Preempts by adding the new animation to the queue. 277 void EnqueueNewAnimation(LayerAnimationSequence* sequence); 278 279 // Preempts by wiping out any unstarted animation in the queue and then 280 // enqueuing this animation. 281 void ReplaceQueuedAnimations(LayerAnimationSequence* sequence); 282 283 // If there's an animation in the queue that doesn't animate the same property 284 // as a running animation, or an animation schedule to run before it, start it 285 // up. Repeat until there are no such animations. 286 void ProcessQueue(); 287 288 // Attempts to add the sequence to the list of running animations. Returns 289 // false if there is an animation running that already animates one of the 290 // properties affected by |sequence|. 291 bool StartSequenceImmediately(LayerAnimationSequence* sequence); 292 293 // Sets the value of target as if all the running and queued animations were 294 // allowed to finish. 295 void GetTargetValue(LayerAnimationElement::TargetValue* target) const; 296 297 // Called whenever an animation is added to the animation queue. Either by 298 // starting the animation or adding to the queue. 299 void OnScheduled(LayerAnimationSequence* sequence); 300 301 // Sets |transition_duration_| unless |is_transition_duration_locked_| is set. 302 void SetTransitionDuration(base::TimeDelta duration); 303 304 // Clears the animation queues and notifies any running animations that they 305 // have been aborted. 306 void ClearAnimationsInternal(); 307 308 // Cleans up any running animations that may have been deleted. 309 void PurgeDeletedAnimations(); 310 311 // This is the queue of animations to run. 312 AnimationQueue animation_queue_; 313 314 // The target of all layer animations. 315 LayerAnimationDelegate* delegate_; 316 317 // The currently running animations. 318 RunningAnimations running_animations_; 319 320 // Determines how animations are replaced. 321 PreemptionStrategy preemption_strategy_; 322 323 // Whether the length of animations is locked. While it is locked 324 // SetTransitionDuration does not set |transition_duration_|. 325 bool is_transition_duration_locked_; 326 327 // The default length of animations. 328 base::TimeDelta transition_duration_; 329 330 // The default tween type for implicit transitions 331 gfx::Tween::Type tween_type_; 332 333 // Used for coordinating the starting of animations. 334 base::TimeTicks last_step_time_; 335 336 // True if we are being stepped by our container. 337 bool is_started_; 338 339 // This prevents the animator from automatically stepping through animations 340 // and allows for manual stepping. 341 bool disable_timer_for_test_; 342 343 // Prevents timer adjustments in case when we start multiple animations 344 // with preemption strategies that discard previous animations. 345 bool adding_animations_; 346 347 // Observers are notified when layer animations end, are scheduled or are 348 // aborted. 349 ObserverList<LayerAnimationObserver> observers_; 350 351 DISALLOW_COPY_AND_ASSIGN(LayerAnimator); 352 }; 353 354 } // namespace ui 355 356 #endif // UI_COMPOSITOR_LAYER_ANIMATOR_H_ 357