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 |max_scale| to the maximum scale along any dimension at any
138 // destination in active animations. Returns false if the maximum scale cannot
140 bool MaximumTargetScale(float* max_scale
) const;
142 // When a scroll animation is removed on the main thread, its compositor
143 // thread counterpart continues producing scroll deltas until activation.
144 // These scroll deltas need to be cleared at activation, so that the active
145 // layer's scroll offset matches the offset provided by the main thread
146 // rather than a combination of this offset and scroll deltas produced by
147 // the removed animation. This is to provide the illusion of synchronicity to
148 // JS that simultaneously removes an animation and sets the scroll offset.
149 bool scroll_offset_animation_was_interrupted() const {
150 return scroll_offset_animation_was_interrupted_
;
153 bool needs_to_start_animations_for_testing() {
154 return needs_to_start_animations_
;
158 friend class base::RefCounted
<LayerAnimationController
>;
160 explicit LayerAnimationController(int id
);
161 virtual ~LayerAnimationController();
164 typedef base::hash_set
<int> TargetProperties
;
166 void PushNewAnimationsToImplThread(
167 LayerAnimationController
* controller_impl
) const;
168 void RemoveAnimationsCompletedOnMainThread(
169 LayerAnimationController
* controller_impl
) const;
170 void PushPropertiesToImplThread(LayerAnimationController
* controller_impl
);
172 void StartAnimations(base::TimeTicks monotonic_time
);
173 void PromoteStartedAnimations(base::TimeTicks monotonic_time
,
174 AnimationEventsVector
* events
);
175 void MarkFinishedAnimations(base::TimeTicks monotonic_time
);
176 void MarkAnimationsForDeletion(base::TimeTicks monotonic_time
,
177 AnimationEventsVector
* events
);
178 void PurgeAnimationsMarkedForDeletion();
180 void TickAnimations(base::TimeTicks monotonic_time
);
182 enum UpdateActivationType
{ NORMAL_ACTIVATION
, FORCE_ACTIVATION
};
183 void UpdateActivation(UpdateActivationType type
);
185 void NotifyObserversOpacityAnimated(float opacity
,
186 bool notify_active_observers
,
187 bool notify_pending_observers
);
188 void NotifyObserversTransformAnimated(const gfx::Transform
& transform
,
189 bool notify_active_observers
,
190 bool notify_pending_observers
);
191 void NotifyObserversFilterAnimated(const FilterOperations
& filter
,
192 bool notify_active_observers
,
193 bool notify_pending_observers
);
194 void NotifyObserversScrollOffsetAnimated(
195 const gfx::ScrollOffset
& scroll_offset
,
196 bool notify_active_observers
,
197 bool notify_pending_observers
);
199 void NotifyObserversAnimationWaitingForDeletion();
201 bool HasValueObserver();
202 bool HasActiveValueObserver();
204 AnimationRegistrar
* registrar_
;
206 ScopedPtrVector
<Animation
> animations_
;
208 // This is used to ensure that we don't spam the registrar.
211 base::TimeTicks last_tick_time_
;
213 ObserverList
<LayerAnimationValueObserver
> value_observers_
;
214 ObserverList
<LayerAnimationEventObserver
> event_observers_
;
216 LayerAnimationValueProvider
* value_provider_
;
218 AnimationDelegate
* layer_animation_delegate_
;
220 // Only try to start animations when new animations are added or when the
221 // previous attempt at starting animations failed to start all animations.
222 bool needs_to_start_animations_
;
224 bool scroll_offset_animation_was_interrupted_
;
226 DISALLOW_COPY_AND_ASSIGN(LayerAnimationController
);
231 #endif // CC_ANIMATION_LAYER_ANIMATION_CONTROLLER_H_