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/transform.h"
28 class AnimationDelegate
;
29 class AnimationRegistrar
;
30 class FilterOperations
;
31 class KeyframeValueList
;
32 class LayerAnimationValueObserver
;
33 class LayerAnimationValueProvider
;
35 class CC_EXPORT LayerAnimationController
36 : public base::RefCounted
<LayerAnimationController
> {
38 static scoped_refptr
<LayerAnimationController
> Create(int id
);
40 int id() const { return id_
; }
42 void AddAnimation(scoped_ptr
<Animation
> animation
);
43 void PauseAnimation(int animation_id
, base::TimeDelta time_offset
);
44 void RemoveAnimation(int animation_id
);
45 void RemoveAnimation(int animation_id
,
46 Animation::TargetProperty target_property
);
47 void AbortAnimations(Animation::TargetProperty target_property
);
49 // Ensures that the list of active animations on the main thread and the impl
50 // thread are kept in sync. This function does not take ownership of the impl
51 // thread controller. This method is virtual for testing.
52 virtual void PushAnimationUpdatesTo(
53 LayerAnimationController
* controller_impl
);
55 void Animate(base::TimeTicks monotonic_time
);
56 void AccumulatePropertyUpdates(base::TimeTicks monotonic_time
,
57 AnimationEventsVector
* events
);
59 void UpdateState(bool start_ready_animations
,
60 AnimationEventsVector
* events
);
62 // Make animations affect active observers if and only if they affect
63 // pending observers. Any animations that no longer affect any observers
65 void ActivateAnimations();
67 // Returns the active animation in the given group, animating the given
68 // property, if such an animation exists.
69 Animation
* GetAnimation(int group_id
,
70 Animation::TargetProperty target_property
) const;
72 // Returns the active animation animating the given property that is either
73 // running, or is next to run, if such an animation exists.
74 Animation
* GetAnimation(Animation::TargetProperty target_property
) const;
76 // Returns true if there are any animations that have neither finished nor
78 bool HasActiveAnimation() const;
80 // Returns true if there are any animations at all to process.
81 bool has_any_animation() const { return !animations_
.empty(); }
83 // Returns true if there is an animation currently animating the given
84 // property, or if there is an animation scheduled to animate this property in
86 bool IsAnimatingProperty(Animation::TargetProperty target_property
) const;
88 void SetAnimationRegistrar(AnimationRegistrar
* registrar
);
89 AnimationRegistrar
* animation_registrar() { return registrar_
; }
91 void NotifyAnimationStarted(const AnimationEvent
& event
);
92 void NotifyAnimationFinished(const AnimationEvent
& event
);
93 void NotifyAnimationAborted(const AnimationEvent
& event
);
94 void NotifyAnimationPropertyUpdate(const AnimationEvent
& event
);
96 void AddValueObserver(LayerAnimationValueObserver
* observer
);
97 void RemoveValueObserver(LayerAnimationValueObserver
* observer
);
99 void AddEventObserver(LayerAnimationEventObserver
* observer
);
100 void RemoveEventObserver(LayerAnimationEventObserver
* observer
);
102 void set_value_provider(LayerAnimationValueProvider
* provider
) {
103 value_provider_
= provider
;
106 void remove_value_provider(LayerAnimationValueProvider
* provider
) {
107 if (value_provider_
== provider
)
108 value_provider_
= NULL
;
111 void set_layer_animation_delegate(AnimationDelegate
* delegate
) {
112 layer_animation_delegate_
= delegate
;
115 void remove_layer_animation_delegate(AnimationDelegate
* delegate
) {
116 if (layer_animation_delegate_
== delegate
)
117 layer_animation_delegate_
= NULL
;
120 bool HasFilterAnimationThatInflatesBounds() const;
121 bool HasTransformAnimationThatInflatesBounds() const;
122 bool HasAnimationThatInflatesBounds() const {
123 return HasTransformAnimationThatInflatesBounds() ||
124 HasFilterAnimationThatInflatesBounds();
127 bool FilterAnimationBoundsForBox(const gfx::BoxF
& box
,
128 gfx::BoxF
* bounds
) const;
129 bool TransformAnimationBoundsForBox(const gfx::BoxF
& box
,
130 gfx::BoxF
* bounds
) const;
132 bool HasAnimationThatAffectsScale() const;
134 bool HasOnlyTranslationTransforms() const;
136 // Sets |max_scale| to the maximum scale along any dimension during active
137 // animations. Returns false if the maximum scale cannot be computed.
138 bool MaximumScale(float* max_scale
) const;
140 bool needs_to_start_animations_for_testing() {
141 return needs_to_start_animations_
;
145 friend class base::RefCounted
<LayerAnimationController
>;
147 explicit LayerAnimationController(int id
);
148 virtual ~LayerAnimationController();
151 typedef base::hash_set
<int> TargetProperties
;
153 void PushNewAnimationsToImplThread(
154 LayerAnimationController
* controller_impl
) const;
155 void RemoveAnimationsCompletedOnMainThread(
156 LayerAnimationController
* controller_impl
) const;
157 void PushPropertiesToImplThread(
158 LayerAnimationController
* controller_impl
) const;
160 void StartAnimations(base::TimeTicks monotonic_time
);
161 void PromoteStartedAnimations(base::TimeTicks monotonic_time
,
162 AnimationEventsVector
* events
);
163 void MarkFinishedAnimations(base::TimeTicks monotonic_time
);
164 void MarkAnimationsForDeletion(base::TimeTicks monotonic_time
,
165 AnimationEventsVector
* events
);
166 void PurgeAnimationsMarkedForDeletion();
168 void TickAnimations(base::TimeTicks monotonic_time
);
170 enum UpdateActivationType
{
174 void UpdateActivation(UpdateActivationType type
);
176 void NotifyObserversOpacityAnimated(float opacity
,
177 bool notify_active_observers
,
178 bool notify_pending_observers
);
179 void NotifyObserversTransformAnimated(const gfx::Transform
& transform
,
180 bool notify_active_observers
,
181 bool notify_pending_observers
);
182 void NotifyObserversFilterAnimated(const FilterOperations
& filter
,
183 bool notify_active_observers
,
184 bool notify_pending_observers
);
185 void NotifyObserversScrollOffsetAnimated(const gfx::Vector2dF
& scroll_offset
,
186 bool notify_active_observers
,
187 bool notify_pending_observers
);
189 void NotifyObserversAnimationWaitingForDeletion();
191 bool HasValueObserver();
192 bool HasActiveValueObserver();
194 AnimationRegistrar
* registrar_
;
196 ScopedPtrVector
<Animation
> animations_
;
198 // This is used to ensure that we don't spam the registrar.
201 base::TimeTicks last_tick_time_
;
203 ObserverList
<LayerAnimationValueObserver
> value_observers_
;
204 ObserverList
<LayerAnimationEventObserver
> event_observers_
;
206 LayerAnimationValueProvider
* value_provider_
;
208 AnimationDelegate
* layer_animation_delegate_
;
210 // Only try to start animations when new animations are added or when the
211 // previous attempt at starting animations failed to start all animations.
212 bool needs_to_start_animations_
;
214 DISALLOW_COPY_AND_ASSIGN(LayerAnimationController
);
219 #endif // CC_ANIMATION_LAYER_ANIMATION_CONTROLLER_H_