OLD | NEW |
---|---|
1 // Copyright 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "cc/animation.h" | 5 #include "cc/animation.h" |
6 | 6 |
7 #include <cmath> | 7 #include <cmath> |
8 | 8 |
9 #include "base/debug/trace_event.h" | 9 #include "base/debug/trace_event.h" |
10 #include "base/string_util.h" | 10 #include "base/string_util.h" |
11 #include "cc/animation_curve.h" | 11 #include "cc/animation_curve.h" |
12 | 12 |
13 namespace { | 13 namespace { |
14 | 14 |
15 // This should match the RunState enum. | 15 // This should match the RunState enum. |
16 static const char* const s_runStateNames[] = { | 16 static const char* const s_runStateNames[] = { |
17 "WaitingForNextTick", | 17 "WaitingForNextTick", |
18 "WaitingForTargetAvailability", | 18 "WaitingForTargetAvailability", |
19 "WaitingForStartTime", | 19 "WaitingForStartTime", |
20 "WaitingForDeletion", | 20 "WaitingForDeletion", |
21 "Starting", | 21 "Starting", |
22 "Running", | 22 "Running", |
23 "Paused", | 23 "Paused", |
24 "Finished", | 24 "Finished", |
25 "Aborted" | 25 "Aborted" |
26 }; | 26 }; |
27 | 27 |
28 COMPILE_ASSERT(static_cast<int>(cc::Animation::RunStateEnumSize) == arraysize(s_ runStateNames), RunState_names_match_enum); | 28 COMPILE_ASSERT(static_cast<int>(cc::Animation::RunStateEnumSize) == |
29 arraysize(s_runStateNames), | |
30 RunState_names_match_enum); | |
29 | 31 |
30 // This should match the TargetProperty enum. | 32 // This should match the TargetProperty enum. |
31 static const char* const s_targetPropertyNames[] = { | 33 static const char* const s_targetPropertyNames[] = { |
32 "Transform", | 34 "Transform", |
33 "Opacity" | 35 "Opacity" |
34 }; | 36 }; |
35 | 37 |
36 COMPILE_ASSERT(static_cast<int>(cc::Animation::TargetPropertyEnumSize) == arrays ize(s_targetPropertyNames), TargetProperty_names_match_enum); | 38 COMPILE_ASSERT(static_cast<int>(cc::Animation::TargetPropertyEnumSize) == |
37 | 39 arraysize(s_targetPropertyNames), |
38 } // namespace | 40 TargetProperty_names_match_enum); |
39 | 41 |
40 namespace cc { | 42 } // namespace |
41 | 43 |
42 scoped_ptr<Animation> Animation::create(scoped_ptr<AnimationCurve> curve, int an imationId, int groupId, TargetProperty targetProperty) | 44 namespace cc { scoped_ptr<Animation> Animation::Create( |
jamesr
2013/03/09 02:44:56
namespace cc { should be on its own line. probably
danakj
2013/03/09 03:01:11
oops, bad script. thanks.
| |
43 { | 45 scoped_ptr<AnimationCurve> curve, |
44 return make_scoped_ptr(new Animation(curve.Pass(), animationId, groupId, tar getProperty)); | 46 int animation_id, |
45 } | 47 int group_id, |
46 | 48 TargetProperty target_property) { |
47 Animation::Animation(scoped_ptr<AnimationCurve> curve, int animationId, int grou pId, TargetProperty targetProperty) | 49 return make_scoped_ptr(new Animation(curve.Pass(), |
48 : m_curve(curve.Pass()) | 50 animation_id, |
49 , m_id(animationId) | 51 group_id, |
50 , m_group(groupId) | 52 target_property)); } |
51 , m_targetProperty(targetProperty) | 53 |
52 , m_runState(WaitingForTargetAvailability) | 54 Animation::Animation(scoped_ptr<AnimationCurve> curve, |
53 , m_iterations(1) | 55 int animation_id, |
54 , m_startTime(0) | 56 int group_id, |
55 , m_alternatesDirection(false) | 57 TargetProperty target_property) |
56 , m_timeOffset(0) | 58 : curve_(curve.Pass()), |
57 , m_needsSynchronizedStartTime(false) | 59 id_(animation_id), |
58 , m_suspended(false) | 60 group_(group_id), |
59 , m_pauseTime(0) | 61 target_property_(target_property), |
60 , m_totalPausedTime(0) | 62 run_state_(WaitingForTargetAvailability), |
61 , m_isControllingInstance(false) | 63 iterations_(1), |
62 , m_isImplOnly(false) | 64 start_time_(0), |
63 { | 65 alternates_direction_(false), |
64 } | 66 time_offset_(0), |
65 | 67 needs_synchronized_start_time_(false), |
66 Animation::~Animation() | 68 suspended_(false), |
67 { | 69 pause_time_(0), |
68 if (m_runState == Running || m_runState == Paused) | 70 total_paused_time_(0), |
69 setRunState(Aborted, 0); | 71 is_controlling_instance_(false), |
70 } | 72 is_impl_only_(false) {} |
71 | 73 |
72 void Animation::setRunState(RunState runState, double monotonicTime) | 74 Animation::~Animation() { |
73 { | 75 if (run_state_ == Running || run_state_ == Paused) |
74 if (m_suspended) | 76 SetRunState(Aborted, 0); |
75 return; | 77 } |
76 | 78 |
77 char nameBuffer[256]; | 79 void Animation::SetRunState(RunState run_state, double monotonic_time) { |
78 base::snprintf(nameBuffer, sizeof(nameBuffer), "%s-%d%s", s_targetPropertyNa mes[m_targetProperty], m_group, m_isControllingInstance ? "(impl)" : ""); | 80 if (suspended_) |
79 | 81 return; |
80 bool isWaitingToStart = m_runState == WaitingForNextTick | 82 |
81 || m_runState == WaitingForTargetAvailability | 83 char nameBuffer[256]; |
82 || m_runState == WaitingForStartTime | 84 base::snprintf(nameBuffer, |
83 || m_runState == Starting; | 85 sizeof(nameBuffer), |
84 | 86 "%s-%d%s", |
85 if (isWaitingToStart && runState == Running) | 87 s_targetPropertyNames[target_property_], |
86 TRACE_EVENT_ASYNC_BEGIN1("cc", "Animation", this, "Name", TRACE_STR_COPY (nameBuffer)); | 88 group_, |
87 | 89 is_controlling_instance_ ? "(impl)" : ""); |
88 bool wasFinished = isFinished(); | 90 |
89 | 91 bool is_waiting_to_start = run_state_ == WaitingForNextTick || |
90 const char* oldRunStateName = s_runStateNames[m_runState]; | 92 run_state_ == WaitingForTargetAvailability || |
91 | 93 run_state_ == WaitingForStartTime || |
92 if (runState == Running && m_runState == Paused) | 94 run_state_ == Starting; |
93 m_totalPausedTime += monotonicTime - m_pauseTime; | 95 |
94 else if (runState == Paused) | 96 if (is_waiting_to_start && run_state == Running) { |
95 m_pauseTime = monotonicTime; | 97 TRACE_EVENT_ASYNC_BEGIN1( |
96 m_runState = runState; | 98 "cc", "Animation", this, "Name", TRACE_STR_COPY(nameBuffer)); |
97 | 99 } |
98 const char* newRunStateName = s_runStateNames[runState]; | 100 |
99 | 101 bool was_finished = is_finished(); |
100 if (!wasFinished && isFinished()) | 102 |
101 TRACE_EVENT_ASYNC_END0("cc", "Animation", this); | 103 const char* old_run_state_name = s_runStateNames[run_state_]; |
102 | 104 |
103 char stateBuffer[256]; | 105 if (run_state == Running && run_state_ == Paused) |
104 base::snprintf(stateBuffer, sizeof(stateBuffer), "%s->%s", oldRunStateName, newRunStateName); | 106 total_paused_time_ += monotonic_time - pause_time_; |
105 | 107 else if (run_state == Paused) |
106 TRACE_EVENT_INSTANT2("cc", "LayerAnimationController::setRunState", "Name", TRACE_STR_COPY(nameBuffer), "State", TRACE_STR_COPY(stateBuffer)); | 108 pause_time_ = monotonic_time; |
107 } | 109 run_state_ = run_state; |
108 | 110 |
109 void Animation::suspend(double monotonicTime) | 111 const char* new_run_state_name = s_runStateNames[run_state]; |
110 { | 112 |
111 setRunState(Paused, monotonicTime); | 113 if (!was_finished && is_finished()) |
112 m_suspended = true; | 114 TRACE_EVENT_ASYNC_END0("cc", "Animation", this); |
113 } | 115 |
114 | 116 char stateBuffer[256]; |
115 void Animation::resume(double monotonicTime) | 117 base::snprintf(stateBuffer, |
116 { | 118 sizeof(stateBuffer), |
117 m_suspended = false; | 119 "%s->%s", |
118 setRunState(Running, monotonicTime); | 120 old_run_state_name, |
119 } | 121 new_run_state_name); |
120 | 122 |
121 bool Animation::isFinishedAt(double monotonicTime) const | 123 TRACE_EVENT_INSTANT2("cc", |
122 { | 124 "LayerAnimationController::setRunState", |
123 if (isFinished()) | 125 "Name", |
124 return true; | 126 TRACE_STR_COPY(nameBuffer), |
125 | 127 "State", |
126 if (m_needsSynchronizedStartTime) | 128 TRACE_STR_COPY(stateBuffer)); |
127 return false; | 129 } |
128 | 130 |
129 return m_runState == Running | 131 void Animation::Suspend(double monotonic_time) { |
130 && m_iterations >= 0 | 132 SetRunState(Paused, monotonic_time); |
131 && m_iterations * m_curve->duration() <= monotonicTime - startTime() - m _totalPausedTime; | 133 suspended_ = true; |
132 } | 134 } |
133 | 135 |
134 double Animation::trimTimeToCurrentIteration(double monotonicTime) const | 136 void Animation::Resume(double monotonic_time) { |
135 { | 137 suspended_ = false; |
136 double trimmed = monotonicTime + m_timeOffset; | 138 SetRunState(Running, monotonic_time); |
137 | 139 } |
138 // If we're paused, time is 'stuck' at the pause time. | 140 |
139 if (m_runState == Paused) | 141 bool Animation::IsFinishedAt(double monotonic_time) const { |
140 trimmed = m_pauseTime; | 142 if (is_finished()) |
141 | 143 return true; |
142 // Returned time should always be relative to the start time and should subt ract | 144 |
143 // all time spent paused. | 145 if (needs_synchronized_start_time_) |
144 trimmed -= m_startTime + m_totalPausedTime; | 146 return false; |
145 | 147 |
146 // Zero is always the start of the animation. | 148 return run_state_ == Running && |
147 if (trimmed <= 0) | 149 iterations_ >= 0 && |
148 return 0; | 150 iterations_ * curve_->Duration() <= (monotonic_time - |
149 | 151 start_time() - |
150 // Always return zero if we have no iterations. | 152 total_paused_time_); |
151 if (!m_iterations) | 153 } |
152 return 0; | 154 |
153 | 155 double Animation::TrimTimeToCurrentIteration(double monotonic_time) const { |
154 // Don't attempt to trim if we have no duration. | 156 double trimmed = monotonic_time + time_offset_; |
155 if (m_curve->duration() <= 0) | 157 |
156 return 0; | 158 // If we're paused, time is 'stuck' at the pause time. |
157 | 159 if (run_state_ == Paused) |
158 // If less than an iteration duration, just return trimmed. | 160 trimmed = pause_time_; |
159 if (trimmed < m_curve->duration()) | 161 |
160 return trimmed; | 162 // Returned time should always be relative to the start time and should |
161 | 163 // subtract all time spent paused. |
162 // If greater than or equal to the total duration, return iteration duration . | 164 trimmed -= start_time_ + total_paused_time_; |
163 if (m_iterations >= 0 && trimmed >= m_curve->duration() * m_iterations) { | 165 |
164 if (m_alternatesDirection && !(m_iterations % 2)) | 166 // Zero is always the start of the animation. |
165 return 0; | 167 if (trimmed <= 0) |
166 return m_curve->duration(); | 168 return 0; |
167 } | 169 |
168 | 170 // Always return zero if we have no iterations. |
169 // We need to know the current iteration if we're alternating. | 171 if (!iterations_) |
170 int iteration = static_cast<int>(trimmed / m_curve->duration()); | 172 return 0; |
171 | 173 |
172 // Calculate x where trimmed = x + n * m_curve->duration() for some positive integer n. | 174 // Don't attempt to trim if we have no duration. |
173 trimmed = fmod(trimmed, m_curve->duration()); | 175 if (curve_->Duration() <= 0) |
174 | 176 return 0; |
175 // If we're alternating and on an odd iteration, reverse the direction. | 177 |
176 if (m_alternatesDirection && iteration % 2 == 1) | 178 // If less than an iteration duration, just return trimmed. |
177 return m_curve->duration() - trimmed; | 179 if (trimmed < curve_->Duration()) |
178 | |
179 return trimmed; | 180 return trimmed; |
180 } | 181 |
181 | 182 // If greater than or equal to the total duration, return iteration duration. |
182 scoped_ptr<Animation> Animation::clone(InstanceType instanceType) const | 183 if (iterations_ >= 0 && trimmed >= curve_->Duration() * iterations_) { |
183 { | 184 if (alternates_direction_ && !(iterations_ % 2)) |
184 return cloneAndInitialize(instanceType, m_runState, m_startTime); | 185 return 0; |
185 } | 186 return curve_->Duration(); |
186 | 187 } |
187 scoped_ptr<Animation> Animation::cloneAndInitialize(InstanceType instanceType, R unState initialRunState, double startTime) const | 188 |
188 { | 189 // We need to know the current iteration if we're alternating. |
189 scoped_ptr<Animation> toReturn(new Animation(m_curve->clone(), m_id, m_group , m_targetProperty)); | 190 int iteration = static_cast<int>(trimmed / curve_->Duration()); |
190 toReturn->m_runState = initialRunState; | 191 |
191 toReturn->m_iterations = m_iterations; | 192 // Calculate x where trimmed = x + n * curve_->Duration() for some positive |
192 toReturn->m_startTime = startTime; | 193 // integer n. |
193 toReturn->m_pauseTime = m_pauseTime; | 194 trimmed = fmod(trimmed, curve_->Duration()); |
194 toReturn->m_totalPausedTime = m_totalPausedTime; | 195 |
195 toReturn->m_timeOffset = m_timeOffset; | 196 // If we're alternating and on an odd iteration, reverse the direction. |
196 toReturn->m_alternatesDirection = m_alternatesDirection; | 197 if (alternates_direction_ && iteration % 2 == 1) |
197 toReturn->m_isControllingInstance = instanceType == ControllingInstance; | 198 return curve_->Duration() - trimmed; |
198 return toReturn.Pass(); | 199 |
199 } | 200 return trimmed; |
200 | 201 } |
201 void Animation::pushPropertiesTo(Animation* other) const | 202 |
202 { | 203 scoped_ptr<Animation> Animation::Clone(InstanceType instance_type) const { |
203 // Currently, we only push changes due to pausing and resuming animations on the main thread. | 204 return CloneAndInitialize(instance_type, run_state_, start_time_); |
204 if (m_runState == Animation::Paused || other->m_runState == Animation::Pause d) { | 205 } |
205 other->m_runState = m_runState; | 206 |
206 other->m_pauseTime = m_pauseTime; | 207 scoped_ptr<Animation> Animation::CloneAndInitialize(InstanceType instance_type, |
207 other->m_totalPausedTime = m_totalPausedTime; | 208 RunState initial_run_state, |
208 } | 209 double start_time) const { |
210 scoped_ptr<Animation> to_return( | |
211 new Animation(curve_->Clone(), id_, group_, target_property_)); | |
212 to_return->run_state_ = initial_run_state; | |
213 to_return->iterations_ = iterations_; | |
214 to_return->start_time_ = start_time; | |
215 to_return->pause_time_ = pause_time_; | |
216 to_return->total_paused_time_ = total_paused_time_; | |
217 to_return->time_offset_ = time_offset_; | |
218 to_return->alternates_direction_ = alternates_direction_; | |
219 to_return->is_controlling_instance_ = instance_type == ControllingInstance; | |
220 return to_return.Pass(); | |
221 } | |
222 | |
223 void Animation::PushPropertiesTo(Animation* other) const { | |
224 // Currently, we only push changes due to pausing and resuming animations on | |
225 // the main thread. | |
226 if (run_state_ == Animation::Paused || | |
227 other->run_state_ == Animation::Paused) { | |
228 other->run_state_ = run_state_; | |
229 other->pause_time_ = pause_time_; | |
230 other->total_paused_time_ = total_paused_time_; | |
231 } | |
209 } | 232 } |
210 | 233 |
211 } // namespace cc | 234 } // namespace cc |
OLD | NEW |