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/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.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"
21 class WebAnimationDelegate
;
24 namespace gfx
{ class Transform
; }
29 class AnimationRegistrar
;
30 class KeyframeValueList
;
31 class LayerAnimationValueObserver
;
33 class CC_EXPORT LayerAnimationController
34 : public base::RefCounted
<LayerAnimationController
> {
36 static scoped_refptr
<LayerAnimationController
> Create(int id
);
38 int id() const { return id_
; }
40 // These methods are virtual for testing.
41 virtual void AddAnimation(scoped_ptr
<Animation
> animation
);
42 virtual void PauseAnimation(int animation_id
, double time_offset
);
43 virtual void RemoveAnimation(int animation_id
);
44 virtual void RemoveAnimation(int animation_id
,
45 Animation::TargetProperty target_property
);
46 virtual void SuspendAnimations(double monotonic_time
);
47 virtual void ResumeAnimations(double monotonic_time
);
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
52 virtual void PushAnimationUpdatesTo(
53 LayerAnimationController
* controller_impl
);
55 // Transfers ownership of all animations to other_controller, replacing
56 // any animations currently owned by other_controller. This is intended
57 // to be used for transferring animations between main thread controllers,
58 // so the run state of each transferred animation is preserved (note that
59 // this differs from ReplaceImplThreadAnimations, which is used for copying
60 // animations from a main thread controller to an impl thread controller).
61 void TransferAnimationsTo(LayerAnimationController
* other_controller
);
63 void Animate(double monotonic_time
);
64 void AccumulatePropertyUpdates(double monotonic_time
,
65 AnimationEventsVector
* events
);
67 void UpdateState(bool start_ready_animations
,
68 AnimationEventsVector
* events
);
70 // Returns the active animation in the given group, animating the given
71 // property, if such an animation exists.
72 Animation
* GetAnimation(int group_id
,
73 Animation::TargetProperty target_property
) const;
75 // Returns the active animation animating the given property that is either
76 // running, or is next to run, if such an animation exists.
77 Animation
* GetAnimation(Animation::TargetProperty target_property
) const;
79 // Returns true if there are any animations that have neither finished nor
81 bool HasActiveAnimation() const;
83 // Returns true if there are any animations at all to process.
84 bool has_any_animation() const { return !active_animations_
.empty(); }
86 // Returns true if there is an animation currently animating the given
87 // property, or if there is an animation scheduled to animate this property in
89 bool IsAnimatingProperty(Animation::TargetProperty target_property
) const;
91 // If a sync is forced, then the next time animation updates are pushed to the
92 // impl thread, all animations will be transferred.
93 void set_force_sync() { force_sync_
= true; }
95 void SetAnimationRegistrar(AnimationRegistrar
* registrar
);
96 AnimationRegistrar
* animation_registrar() { return registrar_
; }
98 void NotifyAnimationStarted(const AnimationEvent
& event
,
99 double wall_clock_time
);
100 void NotifyAnimationFinished(const AnimationEvent
& event
,
101 double wall_clock_time
);
102 void NotifyAnimationPropertyUpdate(const AnimationEvent
& event
);
104 void AddValueObserver(LayerAnimationValueObserver
* observer
);
105 void RemoveValueObserver(LayerAnimationValueObserver
* observer
);
107 void AddEventObserver(LayerAnimationEventObserver
* observer
);
108 void RemoveEventObserver(LayerAnimationEventObserver
* observer
);
110 void set_layer_animation_delegate(WebKit::WebAnimationDelegate
* delegate
) {
111 layer_animation_delegate_
= delegate
;
115 friend class base::RefCounted
<LayerAnimationController
>;
117 explicit LayerAnimationController(int id
);
118 virtual ~LayerAnimationController();
121 typedef base::hash_set
<int> TargetProperties
;
123 void PushNewAnimationsToImplThread(
124 LayerAnimationController
* controller_impl
) const;
125 void RemoveAnimationsCompletedOnMainThread(
126 LayerAnimationController
* controller_impl
) const;
127 void PushPropertiesToImplThread(
128 LayerAnimationController
* controller_impl
) const;
129 void ReplaceImplThreadAnimations(
130 LayerAnimationController
* controller_impl
) const;
132 void StartAnimationsWaitingForNextTick(double monotonic_time
);
133 void StartAnimationsWaitingForStartTime(double monotonic_time
);
134 void StartAnimationsWaitingForTargetAvailability(double monotonic_time
);
135 void ResolveConflicts(double monotonic_time
);
136 void PromoteStartedAnimations(double monotonic_time
,
137 AnimationEventsVector
* events
);
138 void MarkFinishedAnimations(double monotonic_time
);
139 void MarkAnimationsForDeletion(double monotonic_time
,
140 AnimationEventsVector
* events
);
141 void PurgeAnimationsMarkedForDeletion();
143 void TickAnimations(double monotonic_time
);
145 enum UpdateActivationType
{
149 void UpdateActivation(UpdateActivationType type
);
151 void NotifyObserversOpacityAnimated(float opacity
);
152 void NotifyObserversTransformAnimated(const gfx::Transform
& transform
);
154 bool HasValueObserver();
155 bool HasActiveValueObserver();
157 // If this is true, we force a sync to the impl thread.
160 AnimationRegistrar
* registrar_
;
162 ScopedPtrVector
<Animation
> active_animations_
;
164 // This is used to ensure that we don't spam the registrar.
167 double last_tick_time_
;
169 ObserverList
<LayerAnimationValueObserver
> value_observers_
;
170 ObserverList
<LayerAnimationEventObserver
> event_observers_
;
172 WebKit::WebAnimationDelegate
* layer_animation_delegate_
;
174 DISALLOW_COPY_AND_ASSIGN(LayerAnimationController
);
179 #endif // CC_ANIMATION_LAYER_ANIMATION_CONTROLLER_H_