OLD | NEW |
| (Empty) |
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 | |
13 namespace cc { | |
14 | |
15 class 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. | |
20 class CC_EXPORT Animation { | |
21 public: | |
22 // Animations begin in the 'WAITING_FOR_TARGET_AVAILABILITY' state. An | |
23 // Animation waiting for target availibility will run as soon as its target | |
24 // property is free (and all the animations animating with it are also able to | |
25 // run). When this time arrives, the controller will move the animation into | |
26 // the 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 WAITING_FOR_TARGET_AVAILABILITY = 0, | |
32 WAITING_FOR_DELETION, | |
33 STARTING, | |
34 RUNNING, | |
35 PAUSED, | |
36 FINISHED, | |
37 ABORTED, | |
38 // This sentinel must be last. | |
39 LAST_RUN_STATE = ABORTED | |
40 }; | |
41 | |
42 enum TargetProperty { | |
43 TRANSFORM = 0, | |
44 OPACITY, | |
45 FILTER, | |
46 SCROLL_OFFSET, | |
47 BACKGROUND_COLOR, | |
48 // This sentinel must be last. | |
49 LAST_TARGET_PROPERTY = BACKGROUND_COLOR | |
50 }; | |
51 | |
52 enum Direction { | |
53 DIRECTION_NORMAL, | |
54 DIRECTION_REVERSE, | |
55 DIRECTION_ALTERNATE, | |
56 DIRECTION_ALTERNATE_REVERSE | |
57 }; | |
58 | |
59 enum FillMode { | |
60 FILL_MODE_NONE, | |
61 FILL_MODE_FORWARDS, | |
62 FILL_MODE_BACKWARDS, | |
63 FILL_MODE_BOTH | |
64 }; | |
65 | |
66 static scoped_ptr<Animation> Create(scoped_ptr<AnimationCurve> curve, | |
67 int animation_id, | |
68 int group_id, | |
69 TargetProperty target_property); | |
70 | |
71 virtual ~Animation(); | |
72 | |
73 int id() const { return id_; } | |
74 int group() const { return group_; } | |
75 TargetProperty target_property() const { return target_property_; } | |
76 | |
77 RunState run_state() const { return run_state_; } | |
78 void SetRunState(RunState run_state, base::TimeTicks monotonic_time); | |
79 | |
80 // This is the number of times that the animation will play. If this | |
81 // value is zero the animation will not play. If it is negative, then | |
82 // the animation will loop indefinitely. | |
83 double iterations() const { return iterations_; } | |
84 void set_iterations(double n) { iterations_ = n; } | |
85 | |
86 double iteration_start() const { return iteration_start_; } | |
87 void set_iteration_start(double iteration_start) { | |
88 iteration_start_ = iteration_start; | |
89 } | |
90 | |
91 base::TimeTicks start_time() const { return start_time_; } | |
92 | |
93 void set_start_time(base::TimeTicks monotonic_time) { | |
94 start_time_ = monotonic_time; | |
95 } | |
96 bool has_set_start_time() const { return !start_time_.is_null(); } | |
97 | |
98 base::TimeDelta time_offset() const { return time_offset_; } | |
99 void set_time_offset(base::TimeDelta monotonic_time) { | |
100 time_offset_ = monotonic_time; | |
101 } | |
102 | |
103 void Suspend(base::TimeTicks monotonic_time); | |
104 void Resume(base::TimeTicks monotonic_time); | |
105 | |
106 Direction direction() { return direction_; } | |
107 void set_direction(Direction direction) { direction_ = direction; } | |
108 | |
109 FillMode fill_mode() { return fill_mode_; } | |
110 void set_fill_mode(FillMode fill_mode) { fill_mode_ = fill_mode; } | |
111 | |
112 double playback_rate() { return playback_rate_; } | |
113 void set_playback_rate(double playback_rate) { | |
114 playback_rate_ = playback_rate; | |
115 } | |
116 | |
117 bool IsFinishedAt(base::TimeTicks monotonic_time) const; | |
118 bool is_finished() const { | |
119 return run_state_ == FINISHED || run_state_ == ABORTED || | |
120 run_state_ == WAITING_FOR_DELETION; | |
121 } | |
122 | |
123 bool InEffect(base::TimeTicks monotonic_time) const; | |
124 | |
125 AnimationCurve* curve() { return curve_.get(); } | |
126 const AnimationCurve* curve() const { return curve_.get(); } | |
127 | |
128 // If this is true, even if the animation is running, it will not be tickable | |
129 // until it is given a start time. This is true for animations running on the | |
130 // main thread. | |
131 bool needs_synchronized_start_time() const { | |
132 return needs_synchronized_start_time_; | |
133 } | |
134 void set_needs_synchronized_start_time(bool needs_synchronized_start_time) { | |
135 needs_synchronized_start_time_ = needs_synchronized_start_time; | |
136 } | |
137 | |
138 // This is true for animations running on the main thread when the FINISHED | |
139 // event sent by the corresponding impl animation has been received. | |
140 bool received_finished_event() const { | |
141 return received_finished_event_; | |
142 } | |
143 void set_received_finished_event(bool received_finished_event) { | |
144 received_finished_event_ = received_finished_event; | |
145 } | |
146 | |
147 // Takes the given absolute time, and using the start time and the number | |
148 // of iterations, returns the relative time in the current iteration. | |
149 base::TimeDelta TrimTimeToCurrentIteration( | |
150 base::TimeTicks monotonic_time) const; | |
151 | |
152 scoped_ptr<Animation> CloneAndInitialize(RunState initial_run_state) const; | |
153 | |
154 bool is_controlling_instance() const { return is_controlling_instance_; } | |
155 | |
156 void PushPropertiesTo(Animation* other) const; | |
157 | |
158 void set_is_impl_only(bool is_impl_only) { is_impl_only_ = is_impl_only; } | |
159 bool is_impl_only() const { return is_impl_only_; } | |
160 | |
161 void set_affects_active_observers(bool affects_active_observers) { | |
162 affects_active_observers_ = affects_active_observers; | |
163 } | |
164 bool affects_active_observers() const { return affects_active_observers_; } | |
165 | |
166 void set_affects_pending_observers(bool affects_pending_observers) { | |
167 affects_pending_observers_ = affects_pending_observers; | |
168 } | |
169 bool affects_pending_observers() const { return affects_pending_observers_; } | |
170 | |
171 private: | |
172 Animation(scoped_ptr<AnimationCurve> curve, | |
173 int animation_id, | |
174 int group_id, | |
175 TargetProperty target_property); | |
176 | |
177 base::TimeDelta ConvertToActiveTime(base::TimeTicks monotonic_time) const; | |
178 | |
179 scoped_ptr<AnimationCurve> curve_; | |
180 | |
181 // IDs must be unique. | |
182 int id_; | |
183 | |
184 // Animations that must be run together are called 'grouped' and have the same | |
185 // group id. Grouped animations are guaranteed to start at the same time and | |
186 // no other animations may animate any of the group's target properties until | |
187 // all animations in the group have finished animating. | |
188 int group_; | |
189 | |
190 TargetProperty target_property_; | |
191 RunState run_state_; | |
192 double iterations_; | |
193 double iteration_start_; | |
194 base::TimeTicks start_time_; | |
195 Direction direction_; | |
196 double playback_rate_; | |
197 FillMode fill_mode_; | |
198 | |
199 // The time offset effectively pushes the start of the animation back in time. | |
200 // This is used for resuming paused animations -- an animation is added with a | |
201 // non-zero time offset, causing the animation to skip ahead to the desired | |
202 // point in time. | |
203 base::TimeDelta time_offset_; | |
204 | |
205 bool needs_synchronized_start_time_; | |
206 bool received_finished_event_; | |
207 | |
208 // When an animation is suspended, it behaves as if it is paused and it also | |
209 // ignores all run state changes until it is resumed. This is used for testing | |
210 // purposes. | |
211 bool suspended_; | |
212 | |
213 // These are used in TrimTimeToCurrentIteration to account for time | |
214 // spent while paused. This is not included in AnimationState since it | |
215 // there is absolutely no need for clients of this controller to know | |
216 // about these values. | |
217 base::TimeTicks pause_time_; | |
218 base::TimeDelta total_paused_time_; | |
219 | |
220 // Animations lead dual lives. An active animation will be conceptually owned | |
221 // by two controllers, one on the impl thread and one on the main. In reality, | |
222 // there will be two separate Animation instances for the same animation. They | |
223 // will have the same group id and the same target property (these two values | |
224 // uniquely identify an animation). The instance on the impl thread is the | |
225 // instance that ultimately controls the values of the animating layer and so | |
226 // we will refer to it as the 'controlling instance'. | |
227 bool is_controlling_instance_; | |
228 | |
229 bool is_impl_only_; | |
230 | |
231 // When pushed from a main-thread controller to a compositor-thread | |
232 // controller, an animation will initially only affect pending observers | |
233 // (corresponding to layers in the pending tree). Animations that only | |
234 // affect pending observers are able to reach the STARTING state and tick | |
235 // pending observers, but cannot proceed any further and do not tick active | |
236 // observers. After activation, such animations affect both kinds of observers | |
237 // and are able to proceed past the STARTING state. When the removal of | |
238 // an animation is pushed from a main-thread controller to a | |
239 // compositor-thread controller, this initially only makes the animation | |
240 // stop affecting pending observers. After activation, such animations no | |
241 // longer affect any observers, and are deleted. | |
242 bool affects_active_observers_; | |
243 bool affects_pending_observers_; | |
244 | |
245 DISALLOW_COPY_AND_ASSIGN(Animation); | |
246 }; | |
247 | |
248 } // namespace cc | |
249 | |
250 #endif // CC_ANIMATION_ANIMATION_H_ | |
OLD | NEW |