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