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 in the given group, animating the given
69 // property, if such an animation exists.
70 Animation
* GetAnimation(int group_id
,
71 Animation::TargetProperty target_property
) const;
73 // Returns the active animation animating the given property that is either
74 // running, or is next to run, if such an animation exists.
75 Animation
* GetAnimation(Animation::TargetProperty target_property
) 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 currently animating the given
85 // property, or if there is an animation scheduled to animate this property in
87 bool IsAnimatingProperty(Animation::TargetProperty target_property
) const;
89 void SetAnimationRegistrar(AnimationRegistrar
* registrar
);
90 AnimationRegistrar
* animation_registrar() { return registrar_
; }
92 void NotifyAnimationStarted(const AnimationEvent
& event
);
93 void NotifyAnimationFinished(const AnimationEvent
& event
);
94 void NotifyAnimationAborted(const AnimationEvent
& event
);
95 void NotifyAnimationPropertyUpdate(const AnimationEvent
& event
);
97 void AddValueObserver(LayerAnimationValueObserver
* observer
);
98 void RemoveValueObserver(LayerAnimationValueObserver
* observer
);
100 void AddEventObserver(LayerAnimationEventObserver
* observer
);
101 void RemoveEventObserver(LayerAnimationEventObserver
* observer
);
103 void set_value_provider(LayerAnimationValueProvider
* provider
) {
104 value_provider_
= provider
;
107 void remove_value_provider(LayerAnimationValueProvider
* provider
) {
108 if (value_provider_
== provider
)
109 value_provider_
= nullptr;
112 void set_layer_animation_delegate(AnimationDelegate
* delegate
) {
113 layer_animation_delegate_
= delegate
;
116 void remove_layer_animation_delegate(AnimationDelegate
* delegate
) {
117 if (layer_animation_delegate_
== delegate
)
118 layer_animation_delegate_
= nullptr;
121 bool HasFilterAnimationThatInflatesBounds() const;
122 bool HasTransformAnimationThatInflatesBounds() const;
123 bool HasAnimationThatInflatesBounds() const {
124 return HasTransformAnimationThatInflatesBounds() ||
125 HasFilterAnimationThatInflatesBounds();
128 bool FilterAnimationBoundsForBox(const gfx::BoxF
& box
,
129 gfx::BoxF
* bounds
) const;
130 bool TransformAnimationBoundsForBox(const gfx::BoxF
& box
,
131 gfx::BoxF
* bounds
) const;
133 bool HasAnimationThatAffectsScale() const;
135 bool HasOnlyTranslationTransforms() 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 bool needs_to_start_animations_for_testing() {
143 return needs_to_start_animations_
;
147 friend class base::RefCounted
<LayerAnimationController
>;
149 explicit LayerAnimationController(int id
);
150 virtual ~LayerAnimationController();
153 typedef base::hash_set
<int> TargetProperties
;
155 void PushNewAnimationsToImplThread(
156 LayerAnimationController
* controller_impl
) const;
157 void RemoveAnimationsCompletedOnMainThread(
158 LayerAnimationController
* controller_impl
) const;
159 void PushPropertiesToImplThread(
160 LayerAnimationController
* controller_impl
) const;
162 void StartAnimations(base::TimeTicks monotonic_time
);
163 void PromoteStartedAnimations(base::TimeTicks monotonic_time
,
164 AnimationEventsVector
* events
);
165 void MarkFinishedAnimations(base::TimeTicks monotonic_time
);
166 void MarkAnimationsForDeletion(base::TimeTicks monotonic_time
,
167 AnimationEventsVector
* events
);
168 void PurgeAnimationsMarkedForDeletion();
170 void TickAnimations(base::TimeTicks monotonic_time
);
172 enum UpdateActivationType
{
176 void UpdateActivation(UpdateActivationType type
);
178 void NotifyObserversOpacityAnimated(float opacity
,
179 bool notify_active_observers
,
180 bool notify_pending_observers
);
181 void NotifyObserversTransformAnimated(const gfx::Transform
& transform
,
182 bool notify_active_observers
,
183 bool notify_pending_observers
);
184 void NotifyObserversFilterAnimated(const FilterOperations
& filter
,
185 bool notify_active_observers
,
186 bool notify_pending_observers
);
187 void NotifyObserversScrollOffsetAnimated(
188 const gfx::ScrollOffset
& scroll_offset
,
189 bool notify_active_observers
,
190 bool notify_pending_observers
);
192 void NotifyObserversAnimationWaitingForDeletion();
194 bool HasValueObserver();
195 bool HasActiveValueObserver();
197 AnimationRegistrar
* registrar_
;
199 ScopedPtrVector
<Animation
> animations_
;
201 // This is used to ensure that we don't spam the registrar.
204 base::TimeTicks last_tick_time_
;
206 ObserverList
<LayerAnimationValueObserver
> value_observers_
;
207 ObserverList
<LayerAnimationEventObserver
> event_observers_
;
209 LayerAnimationValueProvider
* value_provider_
;
211 AnimationDelegate
* layer_animation_delegate_
;
213 // Only try to start animations when new animations are added or when the
214 // previous attempt at starting animations failed to start all animations.
215 bool needs_to_start_animations_
;
217 DISALLOW_COPY_AND_ASSIGN(LayerAnimationController
);
222 #endif // CC_ANIMATION_LAYER_ANIMATION_CONTROLLER_H_