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 bool HasFilterAnimationThatInflatesBounds() const;
116 bool HasTransformAnimationThatInflatesBounds() const;
117 bool HasAnimationThatInflatesBounds() const {
118 return HasTransformAnimationThatInflatesBounds() ||
119 HasFilterAnimationThatInflatesBounds();
122 bool FilterAnimationBoundsForBox(const gfx::BoxF
& box
,
123 gfx::BoxF
* bounds
) const;
124 bool TransformAnimationBoundsForBox(const gfx::BoxF
& box
,
125 gfx::BoxF
* bounds
) const;
127 bool HasAnimationThatAffectsScale() const;
129 bool HasOnlyTranslationTransforms() const;
131 // Sets |max_scale| to the maximum scale along any dimension during active
132 // animations. Returns false if the maximum scale cannot be computed.
133 bool MaximumScale(float* max_scale
) const;
135 bool needs_to_start_animations_for_testing() {
136 return needs_to_start_animations_
;
140 friend class base::RefCounted
<LayerAnimationController
>;
142 explicit LayerAnimationController(int id
);
143 virtual ~LayerAnimationController();
146 typedef base::hash_set
<int> TargetProperties
;
148 void PushNewAnimationsToImplThread(
149 LayerAnimationController
* controller_impl
) const;
150 void RemoveAnimationsCompletedOnMainThread(
151 LayerAnimationController
* controller_impl
) const;
152 void PushPropertiesToImplThread(
153 LayerAnimationController
* controller_impl
) const;
155 void StartAnimations(base::TimeTicks monotonic_time
);
156 void PromoteStartedAnimations(base::TimeTicks monotonic_time
,
157 AnimationEventsVector
* events
);
158 void MarkFinishedAnimations(base::TimeTicks monotonic_time
);
159 void MarkAnimationsForDeletion(base::TimeTicks monotonic_time
,
160 AnimationEventsVector
* events
);
161 void PurgeAnimationsMarkedForDeletion();
163 void TickAnimations(base::TimeTicks monotonic_time
);
165 enum UpdateActivationType
{
169 void UpdateActivation(UpdateActivationType type
);
171 void NotifyObserversOpacityAnimated(float opacity
,
172 bool notify_active_observers
,
173 bool notify_pending_observers
);
174 void NotifyObserversTransformAnimated(const gfx::Transform
& transform
,
175 bool notify_active_observers
,
176 bool notify_pending_observers
);
177 void NotifyObserversFilterAnimated(const FilterOperations
& filter
,
178 bool notify_active_observers
,
179 bool notify_pending_observers
);
180 void NotifyObserversScrollOffsetAnimated(const gfx::Vector2dF
& scroll_offset
,
181 bool notify_active_observers
,
182 bool notify_pending_observers
);
184 void NotifyObserversAnimationWaitingForDeletion();
186 bool HasValueObserver();
187 bool HasActiveValueObserver();
189 AnimationRegistrar
* registrar_
;
191 ScopedPtrVector
<Animation
> animations_
;
193 // This is used to ensure that we don't spam the registrar.
196 base::TimeTicks last_tick_time_
;
198 ObserverList
<LayerAnimationValueObserver
> value_observers_
;
199 ObserverList
<LayerAnimationEventObserver
> event_observers_
;
201 LayerAnimationValueProvider
* value_provider_
;
203 AnimationDelegate
* layer_animation_delegate_
;
205 // Only try to start animations when new animations are added or when the
206 // previous attempt at starting animations failed to start all animations.
207 bool needs_to_start_animations_
;
209 DISALLOW_COPY_AND_ASSIGN(LayerAnimationController
);
214 #endif // CC_ANIMATION_LAYER_ANIMATION_CONTROLLER_H_