Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / cc / animation / layer_animation_controller.h
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.
4
5 #ifndef CC_ANIMATION_LAYER_ANIMATION_CONTROLLER_H_
6 #define CC_ANIMATION_LAYER_ANIMATION_CONTROLLER_H_
7
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"
19
20 namespace gfx {
21 class BoxF;
22 class Transform;
23 }
24
25 namespace cc {
26
27 class Animation;
28 class AnimationDelegate;
29 class AnimationRegistrar;
30 class FilterOperations;
31 class KeyframeValueList;
32 class LayerAnimationValueObserver;
33 class LayerAnimationValueProvider;
34
35 class CC_EXPORT LayerAnimationController
36     : public base::RefCounted<LayerAnimationController> {
37  public:
38   static scoped_refptr<LayerAnimationController> Create(int id);
39
40   int id() const { return id_; }
41
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);
48
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);
54
55   void Animate(base::TimeTicks monotonic_time);
56   void AccumulatePropertyUpdates(base::TimeTicks monotonic_time,
57                                  AnimationEventsVector* events);
58
59   void UpdateState(bool start_ready_animations,
60                    AnimationEventsVector* events);
61
62   // Make animations affect active observers if and only if they affect
63   // pending observers. Any animations that no longer affect any observers
64   // are deleted.
65   void ActivateAnimations();
66
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;
71
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;
75
76   // Returns true if there are any animations that have neither finished nor
77   // aborted.
78   bool HasActiveAnimation() const;
79
80   // Returns true if there are any animations at all to process.
81   bool has_any_animation() const { return !animations_.empty(); }
82
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
85   // the future.
86   bool IsAnimatingProperty(Animation::TargetProperty target_property) const;
87
88   void SetAnimationRegistrar(AnimationRegistrar* registrar);
89   AnimationRegistrar* animation_registrar() { return registrar_; }
90
91   void NotifyAnimationStarted(const AnimationEvent& event);
92   void NotifyAnimationFinished(const AnimationEvent& event);
93   void NotifyAnimationAborted(const AnimationEvent& event);
94   void NotifyAnimationPropertyUpdate(const AnimationEvent& event);
95
96   void AddValueObserver(LayerAnimationValueObserver* observer);
97   void RemoveValueObserver(LayerAnimationValueObserver* observer);
98
99   void AddEventObserver(LayerAnimationEventObserver* observer);
100   void RemoveEventObserver(LayerAnimationEventObserver* observer);
101
102   void set_value_provider(LayerAnimationValueProvider* provider) {
103     value_provider_ = provider;
104   }
105
106   void remove_value_provider(LayerAnimationValueProvider* provider) {
107     if (value_provider_ == provider)
108       value_provider_ = NULL;
109   }
110
111   void set_layer_animation_delegate(AnimationDelegate* delegate) {
112     layer_animation_delegate_ = delegate;
113   }
114
115   void remove_layer_animation_delegate(AnimationDelegate* delegate) {
116     if (layer_animation_delegate_ == delegate)
117       layer_animation_delegate_ = NULL;
118   }
119
120   bool HasFilterAnimationThatInflatesBounds() const;
121   bool HasTransformAnimationThatInflatesBounds() const;
122   bool HasAnimationThatInflatesBounds() const {
123     return HasTransformAnimationThatInflatesBounds() ||
124            HasFilterAnimationThatInflatesBounds();
125   }
126
127   bool FilterAnimationBoundsForBox(const gfx::BoxF& box,
128                                    gfx::BoxF* bounds) const;
129   bool TransformAnimationBoundsForBox(const gfx::BoxF& box,
130                                       gfx::BoxF* bounds) const;
131
132   bool HasAnimationThatAffectsScale() const;
133
134   bool HasOnlyTranslationTransforms() const;
135
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;
139
140   bool needs_to_start_animations_for_testing() {
141     return needs_to_start_animations_;
142   }
143
144  protected:
145   friend class base::RefCounted<LayerAnimationController>;
146
147   explicit LayerAnimationController(int id);
148   virtual ~LayerAnimationController();
149
150  private:
151   typedef base::hash_set<int> TargetProperties;
152
153   void PushNewAnimationsToImplThread(
154       LayerAnimationController* controller_impl) const;
155   void RemoveAnimationsCompletedOnMainThread(
156       LayerAnimationController* controller_impl) const;
157   void PushPropertiesToImplThread(
158       LayerAnimationController* controller_impl) const;
159
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();
167
168   void TickAnimations(base::TimeTicks monotonic_time);
169
170   enum UpdateActivationType {
171     NormalActivation,
172     ForceActivation
173   };
174   void UpdateActivation(UpdateActivationType type);
175
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);
188
189   void NotifyObserversAnimationWaitingForDeletion();
190
191   bool HasValueObserver();
192   bool HasActiveValueObserver();
193
194   AnimationRegistrar* registrar_;
195   int id_;
196   ScopedPtrVector<Animation> animations_;
197
198   // This is used to ensure that we don't spam the registrar.
199   bool is_active_;
200
201   base::TimeTicks last_tick_time_;
202
203   ObserverList<LayerAnimationValueObserver> value_observers_;
204   ObserverList<LayerAnimationEventObserver> event_observers_;
205
206   LayerAnimationValueProvider* value_provider_;
207
208   AnimationDelegate* layer_animation_delegate_;
209
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_;
213
214   DISALLOW_COPY_AND_ASSIGN(LayerAnimationController);
215 };
216
217 }  // namespace cc
218
219 #endif  // CC_ANIMATION_LAYER_ANIMATION_CONTROLLER_H_