1 // Copyright 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 CC_ANIMATION_LAYER_ANIMATION_CONTROLLER_H_
6 #define CC_ANIMATION_LAYER_ANIMATION_CONTROLLER_H_
8 #include "base/basictypes.h"
9 #include "base/containers/hash_tables.h"
10 #include "base/memory/ref_counted.h"
11 #include "base/memory/scoped_ptr.h"
12 #include "base/observer_list.h"
13 #include "base/time/time.h"
14 #include "cc/animation/animation_events.h"
15 #include "cc/animation/layer_animation_event_observer.h"
16 #include "cc/base/cc_export.h"
17 #include "cc/base/scoped_ptr_vector.h"
18 #include "ui/gfx/geometry/scroll_offset.h"
19 #include "ui/gfx/transform.h"
29 class AnimationDelegate
;
30 class AnimationRegistrar
;
31 class FilterOperations
;
32 class KeyframeValueList
;
33 class LayerAnimationValueObserver
;
34 class LayerAnimationValueProvider
;
36 class CC_EXPORT LayerAnimationController
37 : public base::RefCounted
<LayerAnimationController
> {
39 static scoped_refptr
<LayerAnimationController
> Create(int id
);
41 int id() const { return id_
; }
43 void AddAnimation(scoped_ptr
<Animation
> animation
);
44 void PauseAnimation(int animation_id
, base::TimeDelta time_offset
);
45 void RemoveAnimation(int animation_id
);
46 void RemoveAnimation(int animation_id
,
47 Animation::TargetProperty target_property
);
48 void AbortAnimations(Animation::TargetProperty target_property
);
50 // Ensures that the list of active animations on the main thread and the impl
51 // thread are kept in sync. This function does not take ownership of the impl
52 // thread controller. This method is virtual for testing.
53 virtual void PushAnimationUpdatesTo(
54 LayerAnimationController
* controller_impl
);
56 void Animate(base::TimeTicks monotonic_time
);
57 void AccumulatePropertyUpdates(base::TimeTicks monotonic_time
,
58 AnimationEventsVector
* events
);
60 void UpdateState(bool start_ready_animations
,
61 AnimationEventsVector
* events
);
63 // Make animations affect active observers if and only if they affect
64 // pending observers. Any animations that no longer affect any observers
66 void ActivateAnimations();
68 // Returns the active animation animating the given property that is either
69 // running, or is next to run, if such an animation exists.
70 Animation
* GetAnimation(Animation::TargetProperty target_property
) const;
72 // Returns the active animation for the given unique animation id.
73 Animation
* GetAnimationById(int animation_id
) const;
75 // Returns true if there are any animations that have neither finished nor
77 bool HasActiveAnimation() const;
79 // Returns true if there are any animations at all to process.
80 bool has_any_animation() const { return !animations_
.empty(); }
82 // Returns true if there is an animation currently animating the given
83 // property, or if there is an animation scheduled to animate this property in
85 bool IsAnimatingProperty(Animation::TargetProperty target_property
) const;
87 void SetAnimationRegistrar(AnimationRegistrar
* registrar
);
88 AnimationRegistrar
* animation_registrar() { return registrar_
; }
90 void NotifyAnimationStarted(const AnimationEvent
& event
);
91 void NotifyAnimationFinished(const AnimationEvent
& event
);
92 void NotifyAnimationAborted(const AnimationEvent
& event
);
93 void NotifyAnimationPropertyUpdate(const AnimationEvent
& event
);
95 void AddValueObserver(LayerAnimationValueObserver
* observer
);
96 void RemoveValueObserver(LayerAnimationValueObserver
* observer
);
98 void AddEventObserver(LayerAnimationEventObserver
* observer
);
99 void RemoveEventObserver(LayerAnimationEventObserver
* observer
);
101 void set_value_provider(LayerAnimationValueProvider
* provider
) {
102 value_provider_
= provider
;
105 void remove_value_provider(LayerAnimationValueProvider
* provider
) {
106 if (value_provider_
== provider
)
107 value_provider_
= nullptr;
110 void set_layer_animation_delegate(AnimationDelegate
* delegate
) {
111 layer_animation_delegate_
= delegate
;
114 void remove_layer_animation_delegate(AnimationDelegate
* delegate
) {
115 if (layer_animation_delegate_
== delegate
)
116 layer_animation_delegate_
= nullptr;
119 bool HasFilterAnimationThatInflatesBounds() const;
120 bool HasTransformAnimationThatInflatesBounds() const;
121 bool HasAnimationThatInflatesBounds() const {
122 return HasTransformAnimationThatInflatesBounds() ||
123 HasFilterAnimationThatInflatesBounds();
126 bool FilterAnimationBoundsForBox(const gfx::BoxF
& box
,
127 gfx::BoxF
* bounds
) const;
128 bool TransformAnimationBoundsForBox(const gfx::BoxF
& box
,
129 gfx::BoxF
* bounds
) const;
131 bool HasAnimationThatAffectsScale() const;
133 bool HasOnlyTranslationTransforms() const;
135 bool AnimationsPreserveAxisAlignment() const;
137 // Sets |start_scale| to the maximum of starting animation scale along any
138 // dimension at any destination in active animations. Returns false if the
139 // starting scale cannot be computed.
140 bool AnimationStartScale(float* start_scale
) const;
142 // Sets |max_scale| to the maximum scale along any dimension at any
143 // destination in active animations. Returns false if the maximum scale cannot
145 bool MaximumTargetScale(float* max_scale
) const;
147 // When a scroll animation is removed on the main thread, its compositor
148 // thread counterpart continues producing scroll deltas until activation.
149 // These scroll deltas need to be cleared at activation, so that the active
150 // layer's scroll offset matches the offset provided by the main thread
151 // rather than a combination of this offset and scroll deltas produced by
152 // the removed animation. This is to provide the illusion of synchronicity to
153 // JS that simultaneously removes an animation and sets the scroll offset.
154 bool scroll_offset_animation_was_interrupted() const {
155 return scroll_offset_animation_was_interrupted_
;
158 bool needs_to_start_animations_for_testing() {
159 return needs_to_start_animations_
;
163 friend class base::RefCounted
<LayerAnimationController
>;
165 explicit LayerAnimationController(int id
);
166 virtual ~LayerAnimationController();
169 typedef base::hash_set
<int> TargetProperties
;
171 void PushNewAnimationsToImplThread(
172 LayerAnimationController
* controller_impl
) const;
173 void RemoveAnimationsCompletedOnMainThread(
174 LayerAnimationController
* controller_impl
) const;
175 void PushPropertiesToImplThread(LayerAnimationController
* controller_impl
);
177 void StartAnimations(base::TimeTicks monotonic_time
);
178 void PromoteStartedAnimations(base::TimeTicks monotonic_time
,
179 AnimationEventsVector
* events
);
180 void MarkFinishedAnimations(base::TimeTicks monotonic_time
);
181 void MarkAnimationsForDeletion(base::TimeTicks monotonic_time
,
182 AnimationEventsVector
* events
);
183 void PurgeAnimationsMarkedForDeletion();
185 void TickAnimations(base::TimeTicks monotonic_time
);
187 enum UpdateActivationType
{ NORMAL_ACTIVATION
, FORCE_ACTIVATION
};
188 void UpdateActivation(UpdateActivationType type
);
190 void NotifyObserversOpacityAnimated(float opacity
,
191 bool notify_active_observers
,
192 bool notify_pending_observers
);
193 void NotifyObserversTransformAnimated(const gfx::Transform
& transform
,
194 bool notify_active_observers
,
195 bool notify_pending_observers
);
196 void NotifyObserversFilterAnimated(const FilterOperations
& filter
,
197 bool notify_active_observers
,
198 bool notify_pending_observers
);
199 void NotifyObserversScrollOffsetAnimated(
200 const gfx::ScrollOffset
& scroll_offset
,
201 bool notify_active_observers
,
202 bool notify_pending_observers
);
204 void NotifyObserversAnimationWaitingForDeletion();
206 bool HasValueObserver();
207 bool HasActiveValueObserver();
209 AnimationRegistrar
* registrar_
;
211 ScopedPtrVector
<Animation
> animations_
;
213 // This is used to ensure that we don't spam the registrar.
216 base::TimeTicks last_tick_time_
;
218 ObserverList
<LayerAnimationValueObserver
> value_observers_
;
219 ObserverList
<LayerAnimationEventObserver
> event_observers_
;
221 LayerAnimationValueProvider
* value_provider_
;
223 AnimationDelegate
* layer_animation_delegate_
;
225 // Only try to start animations when new animations are added or when the
226 // previous attempt at starting animations failed to start all animations.
227 bool needs_to_start_animations_
;
229 bool scroll_offset_animation_was_interrupted_
;
231 DISALLOW_COPY_AND_ASSIGN(LayerAnimationController
);
236 #endif // CC_ANIMATION_LAYER_ANIMATION_CONTROLLER_H_