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() 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(float* start_scale
) const;
150 // Sets |max_scale| to the maximum scale along any dimension at any
151 // destination in active animations. Returns false if the maximum scale cannot
153 bool MaximumTargetScale(float* max_scale
) const;
155 // When a scroll animation is removed on the main thread, its compositor
156 // thread counterpart continues producing scroll deltas until activation.
157 // These scroll deltas need to be cleared at activation, so that the active
158 // layer's scroll offset matches the offset provided by the main thread
159 // rather than a combination of this offset and scroll deltas produced by
160 // the removed animation. This is to provide the illusion of synchronicity to
161 // JS that simultaneously removes an animation and sets the scroll offset.
162 bool scroll_offset_animation_was_interrupted() const {
163 return scroll_offset_animation_was_interrupted_
;
166 bool needs_to_start_animations_for_testing() {
167 return needs_to_start_animations_
;
171 friend class base::RefCounted
<LayerAnimationController
>;
173 explicit LayerAnimationController(int id
);
174 virtual ~LayerAnimationController();
177 typedef base::hash_set
<int> TargetProperties
;
179 void PushNewAnimationsToImplThread(
180 LayerAnimationController
* controller_impl
) const;
181 void RemoveAnimationsCompletedOnMainThread(
182 LayerAnimationController
* controller_impl
) const;
183 void PushPropertiesToImplThread(LayerAnimationController
* controller_impl
);
185 void StartAnimations(base::TimeTicks monotonic_time
);
186 void PromoteStartedAnimations(base::TimeTicks monotonic_time
,
187 AnimationEventsVector
* events
);
188 void MarkFinishedAnimations(base::TimeTicks monotonic_time
);
189 void MarkAnimationsForDeletion(base::TimeTicks monotonic_time
,
190 AnimationEventsVector
* events
);
191 void PurgeAnimationsMarkedForDeletion();
193 void TickAnimations(base::TimeTicks monotonic_time
);
195 enum UpdateActivationType
{ NORMAL_ACTIVATION
, FORCE_ACTIVATION
};
196 void UpdateActivation(UpdateActivationType type
);
198 void NotifyObserversOpacityAnimated(float opacity
,
199 bool notify_active_observers
,
200 bool notify_pending_observers
);
201 void NotifyObserversTransformAnimated(const gfx::Transform
& transform
,
202 bool notify_active_observers
,
203 bool notify_pending_observers
);
204 void NotifyObserversFilterAnimated(const FilterOperations
& filter
,
205 bool notify_active_observers
,
206 bool notify_pending_observers
);
207 void NotifyObserversScrollOffsetAnimated(
208 const gfx::ScrollOffset
& scroll_offset
,
209 bool notify_active_observers
,
210 bool notify_pending_observers
);
212 void NotifyObserversAnimationWaitingForDeletion();
214 void NotifyObserversTransformIsPotentiallyAnimatingChanged(
215 bool notify_active_observers
,
216 bool notify_pending_observers
);
218 void UpdatePotentiallyAnimatingTransform();
220 bool HasValueObserver();
221 bool HasActiveValueObserver();
223 AnimationRegistrar
* registrar_
;
225 ScopedPtrVector
<Animation
> animations_
;
227 // This is used to ensure that we don't spam the registrar.
230 base::TimeTicks last_tick_time_
;
232 base::ObserverList
<LayerAnimationValueObserver
> value_observers_
;
233 base::ObserverList
<LayerAnimationEventObserver
> event_observers_
;
235 LayerAnimationValueProvider
* value_provider_
;
237 AnimationDelegate
* layer_animation_delegate_
;
239 // Only try to start animations when new animations are added or when the
240 // previous attempt at starting animations failed to start all animations.
241 bool needs_to_start_animations_
;
243 bool scroll_offset_animation_was_interrupted_
;
245 bool potentially_animating_transform_for_active_observers_
;
246 bool potentially_animating_transform_for_pending_observers_
;
248 DISALLOW_COPY_AND_ASSIGN(LayerAnimationController
);
253 #endif // CC_ANIMATION_LAYER_ANIMATION_CONTROLLER_H_