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 enum class ObserverType
{ ACTIVE
, PENDING
};
41 static scoped_refptr
<LayerAnimationController
> Create(int id
);
43 int id() const { return id_
; }
45 void AddAnimation(scoped_ptr
<Animation
> animation
);
46 void PauseAnimation(int animation_id
, base::TimeDelta time_offset
);
47 void RemoveAnimation(int animation_id
);
48 void RemoveAnimation(int animation_id
,
49 Animation::TargetProperty target_property
);
50 void AbortAnimations(Animation::TargetProperty target_property
);
52 // Ensures that the list of active animations on the main thread and the impl
53 // thread are kept in sync. This function does not take ownership of the impl
54 // thread controller. This method is virtual for testing.
55 virtual void PushAnimationUpdatesTo(
56 LayerAnimationController
* controller_impl
);
58 void Animate(base::TimeTicks monotonic_time
);
59 void AccumulatePropertyUpdates(base::TimeTicks monotonic_time
,
60 AnimationEventsVector
* events
);
62 void UpdateState(bool start_ready_animations
,
63 AnimationEventsVector
* events
);
65 // Make animations affect active observers if and only if they affect
66 // pending observers. Any animations that no longer affect any observers
68 void ActivateAnimations();
70 // Returns the active animation animating the given property that is either
71 // running, or is next to run, if such an animation exists.
72 Animation
* GetAnimation(Animation::TargetProperty target_property
) const;
74 // Returns the active animation for the given unique animation id.
75 Animation
* GetAnimationById(int animation_id
) const;
77 // Returns true if there are any animations that have neither finished nor
79 bool HasActiveAnimation() const;
81 // Returns true if there are any animations at all to process.
82 bool has_any_animation() const { return !animations_
.empty(); }
84 // Returns true if there is an animation that is either currently animating
85 // the given property or scheduled to animate this property in the future, and
86 // that affects the given observer type.
87 bool IsPotentiallyAnimatingProperty(Animation::TargetProperty target_property
,
88 ObserverType observer_type
) const;
90 // Returns true if there is an animation that is currently animating the given
91 // property and that affects the given observer type.
92 bool IsCurrentlyAnimatingProperty(Animation::TargetProperty target_property
,
93 ObserverType observer_type
) const;
95 void SetAnimationRegistrar(AnimationRegistrar
* registrar
);
96 AnimationRegistrar
* animation_registrar() { return registrar_
; }
98 void NotifyAnimationStarted(const AnimationEvent
& event
);
99 void NotifyAnimationFinished(const AnimationEvent
& event
);
100 void NotifyAnimationAborted(const AnimationEvent
& event
);
101 void NotifyAnimationPropertyUpdate(const AnimationEvent
& event
);
103 void AddValueObserver(LayerAnimationValueObserver
* observer
);
104 void RemoveValueObserver(LayerAnimationValueObserver
* observer
);
106 void AddEventObserver(LayerAnimationEventObserver
* observer
);
107 void RemoveEventObserver(LayerAnimationEventObserver
* observer
);
109 void set_value_provider(LayerAnimationValueProvider
* provider
) {
110 value_provider_
= provider
;
113 void remove_value_provider(LayerAnimationValueProvider
* provider
) {
114 if (value_provider_
== provider
)
115 value_provider_
= nullptr;
118 void set_layer_animation_delegate(AnimationDelegate
* delegate
) {
119 layer_animation_delegate_
= delegate
;
122 void remove_layer_animation_delegate(AnimationDelegate
* delegate
) {
123 if (layer_animation_delegate_
== delegate
)
124 layer_animation_delegate_
= nullptr;
127 bool HasFilterAnimationThatInflatesBounds() const;
128 bool HasTransformAnimationThatInflatesBounds() const;
129 bool HasAnimationThatInflatesBounds() const {
130 return HasTransformAnimationThatInflatesBounds() ||
131 HasFilterAnimationThatInflatesBounds();
134 bool FilterAnimationBoundsForBox(const gfx::BoxF
& box
,
135 gfx::BoxF
* bounds
) const;
136 bool TransformAnimationBoundsForBox(const gfx::BoxF
& box
,
137 gfx::BoxF
* bounds
) const;
139 bool HasAnimationThatAffectsScale() const;
141 bool HasOnlyTranslationTransforms(ObserverType observer_type
) const;
143 bool AnimationsPreserveAxisAlignment() const;
145 // Sets |start_scale| to the maximum of starting animation scale along any
146 // dimension at any destination in active animations. Returns false if the
147 // starting scale cannot be computed.
148 bool AnimationStartScale(ObserverType observer_type
,
149 float* start_scale
) const;
151 // Sets |max_scale| to the maximum scale along any dimension at any
152 // destination in active animations. Returns false if the maximum scale cannot
154 bool MaximumTargetScale(ObserverType event_observers_
,
155 float* max_scale
) const;
157 // When a scroll animation is removed on the main thread, its compositor
158 // thread counterpart continues producing scroll deltas until activation.
159 // These scroll deltas need to be cleared at activation, so that the active
160 // layer's scroll offset matches the offset provided by the main thread
161 // rather than a combination of this offset and scroll deltas produced by
162 // the removed animation. This is to provide the illusion of synchronicity to
163 // JS that simultaneously removes an animation and sets the scroll offset.
164 bool scroll_offset_animation_was_interrupted() const {
165 return scroll_offset_animation_was_interrupted_
;
168 bool needs_to_start_animations_for_testing() {
169 return needs_to_start_animations_
;
173 friend class base::RefCounted
<LayerAnimationController
>;
175 explicit LayerAnimationController(int id
);
176 virtual ~LayerAnimationController();
179 typedef base::hash_set
<int> TargetProperties
;
181 void PushNewAnimationsToImplThread(
182 LayerAnimationController
* controller_impl
) const;
183 void RemoveAnimationsCompletedOnMainThread(
184 LayerAnimationController
* controller_impl
) const;
185 void PushPropertiesToImplThread(LayerAnimationController
* controller_impl
);
187 void StartAnimations(base::TimeTicks monotonic_time
);
188 void PromoteStartedAnimations(base::TimeTicks monotonic_time
,
189 AnimationEventsVector
* events
);
190 void MarkFinishedAnimations(base::TimeTicks monotonic_time
);
191 void MarkAnimationsForDeletion(base::TimeTicks monotonic_time
,
192 AnimationEventsVector
* events
);
193 void PurgeAnimationsMarkedForDeletion();
195 void TickAnimations(base::TimeTicks monotonic_time
);
197 enum UpdateActivationType
{ NORMAL_ACTIVATION
, FORCE_ACTIVATION
};
198 void UpdateActivation(UpdateActivationType type
);
200 void NotifyObserversOpacityAnimated(float opacity
,
201 bool notify_active_observers
,
202 bool notify_pending_observers
);
203 void NotifyObserversTransformAnimated(const gfx::Transform
& transform
,
204 bool notify_active_observers
,
205 bool notify_pending_observers
);
206 void NotifyObserversFilterAnimated(const FilterOperations
& filter
,
207 bool notify_active_observers
,
208 bool notify_pending_observers
);
209 void NotifyObserversScrollOffsetAnimated(
210 const gfx::ScrollOffset
& scroll_offset
,
211 bool notify_active_observers
,
212 bool notify_pending_observers
);
214 void NotifyObserversAnimationWaitingForDeletion();
216 void NotifyObserversTransformIsPotentiallyAnimatingChanged(
217 bool notify_active_observers
,
218 bool notify_pending_observers
);
220 void UpdatePotentiallyAnimatingTransform();
222 bool HasValueObserver();
223 bool HasActiveValueObserver();
225 AnimationRegistrar
* registrar_
;
227 ScopedPtrVector
<Animation
> animations_
;
229 // This is used to ensure that we don't spam the registrar.
232 base::TimeTicks last_tick_time_
;
234 base::ObserverList
<LayerAnimationValueObserver
> value_observers_
;
235 base::ObserverList
<LayerAnimationEventObserver
> event_observers_
;
237 LayerAnimationValueProvider
* value_provider_
;
239 AnimationDelegate
* layer_animation_delegate_
;
241 // Only try to start animations when new animations are added or when the
242 // previous attempt at starting animations failed to start all animations.
243 bool needs_to_start_animations_
;
245 bool scroll_offset_animation_was_interrupted_
;
247 bool potentially_animating_transform_for_active_observers_
;
248 bool potentially_animating_transform_for_pending_observers_
;
250 DISALLOW_COPY_AND_ASSIGN(LayerAnimationController
);
255 #endif // CC_ANIMATION_LAYER_ANIMATION_CONTROLLER_H_