animation.h revision 6e8cce623b6e4fe0c9e4af605d675dd9d0338c38
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_ANIMATION_H_
6#define CC_ANIMATION_ANIMATION_H_
7
8#include "base/basictypes.h"
9#include "base/memory/scoped_ptr.h"
10#include "base/time/time.h"
11#include "cc/base/cc_export.h"
12
13namespace cc {
14
15class AnimationCurve;
16
17// An Animation contains all the state required to play an AnimationCurve.
18// Specifically, the affected property, the run state (paused, finished, etc.),
19// loop count, last pause time, and the total time spent paused.
20class CC_EXPORT Animation {
21 public:
22  // Animations begin in the 'WaitingForTargetAvailability' state. An Animation
23  // waiting for target availibility will run as soon as its target property
24  // is free (and all the animations animating with it are also able to run).
25  // When this time arrives, the controller will move the animation into the
26  // Starting state, and then into the Running state. Running animations may
27  // toggle between Running and Paused, and may be stopped by moving into either
28  // the Aborted or Finished states. A Finished animation was allowed to run to
29  // completion, but an Aborted animation was not.
30  enum RunState {
31    WaitingForTargetAvailability = 0,
32    WaitingForDeletion,
33    Starting,
34    Running,
35    Paused,
36    Finished,
37    Aborted,
38    // This sentinel must be last.
39    RunStateEnumSize
40  };
41
42  enum TargetProperty {
43    Transform = 0,
44    Opacity,
45    Filter,
46    ScrollOffset,
47    BackgroundColor,
48    // This sentinel must be last.
49    TargetPropertyEnumSize
50  };
51
52  enum Direction { Normal, Reverse, Alternate, AlternateReverse };
53
54  static scoped_ptr<Animation> Create(scoped_ptr<AnimationCurve> curve,
55                                      int animation_id,
56                                      int group_id,
57                                      TargetProperty target_property);
58
59  virtual ~Animation();
60
61  int id() const { return id_; }
62  int group() const { return group_; }
63  TargetProperty target_property() const { return target_property_; }
64
65  RunState run_state() const { return run_state_; }
66  void SetRunState(RunState run_state, base::TimeTicks monotonic_time);
67
68  // This is the number of times that the animation will play. If this
69  // value is zero the animation will not play. If it is negative, then
70  // the animation will loop indefinitely.
71  double iterations() const { return iterations_; }
72  void set_iterations(double n) { iterations_ = n; }
73
74  base::TimeTicks start_time() const { return start_time_; }
75
76  void set_start_time(base::TimeTicks monotonic_time) {
77    start_time_ = monotonic_time;
78  }
79  bool has_set_start_time() const { return !start_time_.is_null(); }
80
81  base::TimeDelta time_offset() const { return time_offset_; }
82  void set_time_offset(base::TimeDelta monotonic_time) {
83    time_offset_ = monotonic_time;
84  }
85
86  void Suspend(base::TimeTicks monotonic_time);
87  void Resume(base::TimeTicks monotonic_time);
88
89  Direction direction() { return direction_; }
90  void set_direction(Direction direction) { direction_ = direction; }
91
92  bool IsFinishedAt(base::TimeTicks monotonic_time) const;
93  bool is_finished() const {
94    return run_state_ == Finished ||
95        run_state_ == Aborted ||
96        run_state_ == WaitingForDeletion;
97  }
98
99  AnimationCurve* curve() { return curve_.get(); }
100  const AnimationCurve* curve() const { return curve_.get(); }
101
102  // If this is true, even if the animation is running, it will not be tickable
103  // until it is given a start time. This is true for animations running on the
104  // main thread.
105  bool needs_synchronized_start_time() const {
106    return needs_synchronized_start_time_;
107  }
108  void set_needs_synchronized_start_time(bool needs_synchronized_start_time) {
109    needs_synchronized_start_time_ = needs_synchronized_start_time;
110  }
111
112  // This is true for animations running on the main thread when the Finished
113  // event sent by the corresponding impl animation has been received.
114  bool received_finished_event() const {
115    return received_finished_event_;
116  }
117  void set_received_finished_event(bool received_finished_event) {
118    received_finished_event_ = received_finished_event;
119  }
120
121  // Takes the given absolute time, and using the start time and the number
122  // of iterations, returns the relative time in the current iteration.
123  double TrimTimeToCurrentIteration(base::TimeTicks monotonic_time) const;
124
125  scoped_ptr<Animation> CloneAndInitialize(RunState initial_run_state) const;
126
127  bool is_controlling_instance() const { return is_controlling_instance_; }
128
129  void PushPropertiesTo(Animation* other) const;
130
131  void set_is_impl_only(bool is_impl_only) { is_impl_only_ = is_impl_only; }
132  bool is_impl_only() const { return is_impl_only_; }
133
134  void set_affects_active_observers(bool affects_active_observers) {
135    affects_active_observers_ = affects_active_observers;
136  }
137  bool affects_active_observers() const { return affects_active_observers_; }
138
139  void set_affects_pending_observers(bool affects_pending_observers) {
140    affects_pending_observers_ = affects_pending_observers;
141  }
142  bool affects_pending_observers() const { return affects_pending_observers_; }
143
144 private:
145  Animation(scoped_ptr<AnimationCurve> curve,
146            int animation_id,
147            int group_id,
148            TargetProperty target_property);
149
150  scoped_ptr<AnimationCurve> curve_;
151
152  // IDs are not necessarily unique.
153  int id_;
154
155  // Animations that must be run together are called 'grouped' and have the same
156  // group id. Grouped animations are guaranteed to start at the same time and
157  // no other animations may animate any of the group's target properties until
158  // all animations in the group have finished animating. Note: an active
159  // animation's group id and target property uniquely identify that animation.
160  int group_;
161
162  TargetProperty target_property_;
163  RunState run_state_;
164  double iterations_;
165  base::TimeTicks start_time_;
166  Direction direction_;
167
168  // The time offset effectively pushes the start of the animation back in time.
169  // This is used for resuming paused animations -- an animation is added with a
170  // non-zero time offset, causing the animation to skip ahead to the desired
171  // point in time.
172  base::TimeDelta time_offset_;
173
174  bool needs_synchronized_start_time_;
175  bool received_finished_event_;
176
177  // When an animation is suspended, it behaves as if it is paused and it also
178  // ignores all run state changes until it is resumed. This is used for testing
179  // purposes.
180  bool suspended_;
181
182  // These are used in TrimTimeToCurrentIteration to account for time
183  // spent while paused. This is not included in AnimationState since it
184  // there is absolutely no need for clients of this controller to know
185  // about these values.
186  base::TimeTicks pause_time_;
187  base::TimeDelta total_paused_time_;
188
189  // Animations lead dual lives. An active animation will be conceptually owned
190  // by two controllers, one on the impl thread and one on the main. In reality,
191  // there will be two separate Animation instances for the same animation. They
192  // will have the same group id and the same target property (these two values
193  // uniquely identify an animation). The instance on the impl thread is the
194  // instance that ultimately controls the values of the animating layer and so
195  // we will refer to it as the 'controlling instance'.
196  bool is_controlling_instance_;
197
198  bool is_impl_only_;
199
200  // When pushed from a main-thread controller to a compositor-thread
201  // controller, an animation will initially only affect pending observers
202  // (corresponding to layers in the pending tree). Animations that only
203  // affect pending observers are able to reach the Starting state and tick
204  // pending observers, but cannot proceed any further and do not tick active
205  // observers. After activation, such animations affect both kinds of observers
206  // and are able to proceed past the Starting state. When the removal of
207  // an animation is pushed from a main-thread controller to a
208  // compositor-thread controller, this initially only makes the animation
209  // stop affecting pending observers. After activation, such animations no
210  // longer affect any observers, and are deleted.
211  bool affects_active_observers_;
212  bool affects_pending_observers_;
213
214  DISALLOW_COPY_AND_ASSIGN(Animation);
215};
216
217}  // namespace cc
218
219#endif  // CC_ANIMATION_ANIMATION_H_
220