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/animation.h" | 5 #include "cc/animation/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/strings/string_util.h" | 10 #include "base/strings/string_util.h" |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
57 Animation::Animation(scoped_ptr<AnimationCurve> curve, | 57 Animation::Animation(scoped_ptr<AnimationCurve> curve, |
58 int animation_id, | 58 int animation_id, |
59 int group_id, | 59 int group_id, |
60 TargetProperty target_property) | 60 TargetProperty target_property) |
61 : curve_(curve.Pass()), | 61 : curve_(curve.Pass()), |
62 id_(animation_id), | 62 id_(animation_id), |
63 group_(group_id), | 63 group_(group_id), |
64 target_property_(target_property), | 64 target_property_(target_property), |
65 run_state_(WaitingForTargetAvailability), | 65 run_state_(WaitingForTargetAvailability), |
66 iterations_(1), | 66 iterations_(1), |
67 start_time_(0), | |
68 direction_(Normal), | 67 direction_(Normal), |
69 time_offset_(0), | |
70 needs_synchronized_start_time_(false), | 68 needs_synchronized_start_time_(false), |
71 received_finished_event_(false), | 69 received_finished_event_(false), |
72 suspended_(false), | 70 suspended_(false), |
73 pause_time_(0), | |
74 total_paused_time_(0), | |
75 is_controlling_instance_(false), | 71 is_controlling_instance_(false), |
76 is_impl_only_(false), | 72 is_impl_only_(false), |
77 affects_active_observers_(true), | 73 affects_active_observers_(true), |
78 affects_pending_observers_(true) { | 74 affects_pending_observers_(true) { |
79 } | 75 } |
80 | 76 |
81 Animation::~Animation() { | 77 Animation::~Animation() { |
82 if (run_state_ == Running || run_state_ == Paused) | 78 if (run_state_ == Running || run_state_ == Paused) |
83 SetRunState(Aborted, 0); | 79 SetRunState(Aborted, base::TimeTicks()); |
84 } | 80 } |
85 | 81 |
86 void Animation::SetRunState(RunState run_state, double monotonic_time) { | 82 void Animation::SetRunState(RunState run_state, |
83 base::TimeTicks monotonic_time) { | |
87 if (suspended_) | 84 if (suspended_) |
88 return; | 85 return; |
89 | 86 |
90 char name_buffer[256]; | 87 char name_buffer[256]; |
91 base::snprintf(name_buffer, | 88 base::snprintf(name_buffer, |
92 sizeof(name_buffer), | 89 sizeof(name_buffer), |
93 "%s-%d%s", | 90 "%s-%d%s", |
94 s_targetPropertyNames[target_property_], | 91 s_targetPropertyNames[target_property_], |
95 group_, | 92 group_, |
96 is_controlling_instance_ ? "(impl)" : ""); | 93 is_controlling_instance_ ? "(impl)" : ""); |
97 | 94 |
98 bool is_waiting_to_start = run_state_ == WaitingForTargetAvailability || | 95 bool is_waiting_to_start = run_state_ == WaitingForTargetAvailability || |
99 run_state_ == Starting; | 96 run_state_ == Starting; |
100 | 97 |
101 if (is_waiting_to_start && run_state == Running) { | 98 if (is_waiting_to_start && run_state == Running) { |
102 TRACE_EVENT_ASYNC_BEGIN1( | 99 TRACE_EVENT_ASYNC_BEGIN1( |
103 "cc", "Animation", this, "Name", TRACE_STR_COPY(name_buffer)); | 100 "cc", "Animation", this, "Name", TRACE_STR_COPY(name_buffer)); |
104 } | 101 } |
105 | 102 |
106 bool was_finished = is_finished(); | 103 bool was_finished = is_finished(); |
107 | 104 |
108 const char* old_run_state_name = s_runStateNames[run_state_]; | 105 const char* old_run_state_name = s_runStateNames[run_state_]; |
109 | 106 |
110 if (run_state == Running && run_state_ == Paused) | 107 if (run_state == Running && run_state_ == Paused) |
111 total_paused_time_ += monotonic_time - pause_time_; | 108 total_paused_time_ += (monotonic_time - pause_time_); |
112 else if (run_state == Paused) | 109 else if (run_state == Paused) |
113 pause_time_ = monotonic_time; | 110 pause_time_ = monotonic_time; |
114 run_state_ = run_state; | 111 run_state_ = run_state; |
115 | 112 |
116 const char* new_run_state_name = s_runStateNames[run_state]; | 113 const char* new_run_state_name = s_runStateNames[run_state]; |
117 | 114 |
118 if (!was_finished && is_finished()) | 115 if (!was_finished && is_finished()) |
119 TRACE_EVENT_ASYNC_END0("cc", "Animation", this); | 116 TRACE_EVENT_ASYNC_END0("cc", "Animation", this); |
120 | 117 |
121 char state_buffer[256]; | 118 char state_buffer[256]; |
122 base::snprintf(state_buffer, | 119 base::snprintf(state_buffer, |
123 sizeof(state_buffer), | 120 sizeof(state_buffer), |
124 "%s->%s", | 121 "%s->%s", |
125 old_run_state_name, | 122 old_run_state_name, |
126 new_run_state_name); | 123 new_run_state_name); |
127 | 124 |
128 TRACE_EVENT_INSTANT2("cc", | 125 TRACE_EVENT_INSTANT2("cc", |
129 "LayerAnimationController::SetRunState", | 126 "LayerAnimationController::SetRunState", |
130 TRACE_EVENT_SCOPE_THREAD, | 127 TRACE_EVENT_SCOPE_THREAD, |
131 "Name", | 128 "Name", |
132 TRACE_STR_COPY(name_buffer), | 129 TRACE_STR_COPY(name_buffer), |
133 "State", | 130 "State", |
134 TRACE_STR_COPY(state_buffer)); | 131 TRACE_STR_COPY(state_buffer)); |
135 } | 132 } |
136 | 133 |
137 void Animation::Suspend(double monotonic_time) { | 134 void Animation::Suspend(base::TimeTicks monotonic_time) { |
138 SetRunState(Paused, monotonic_time); | 135 SetRunState(Paused, monotonic_time); |
139 suspended_ = true; | 136 suspended_ = true; |
140 } | 137 } |
141 | 138 |
142 void Animation::Resume(double monotonic_time) { | 139 void Animation::Resume(base::TimeTicks monotonic_time) { |
143 suspended_ = false; | 140 suspended_ = false; |
144 SetRunState(Running, monotonic_time); | 141 SetRunState(Running, monotonic_time); |
145 } | 142 } |
146 | 143 |
147 bool Animation::IsFinishedAt(double monotonic_time) const { | 144 bool Animation::IsFinishedAt(base::TimeTicks monotonic_time) const { |
148 if (is_finished()) | 145 if (is_finished()) |
149 return true; | 146 return true; |
150 | 147 |
151 if (needs_synchronized_start_time_) | 148 if (needs_synchronized_start_time_) |
152 return false; | 149 return false; |
153 | 150 |
154 return run_state_ == Running && | 151 return run_state_ == Running && iterations_ >= 0 && |
155 iterations_ >= 0 && | 152 iterations_ * curve_->Duration() <= |
156 iterations_ * curve_->Duration() <= (monotonic_time - | 153 (monotonic_time + time_offset_ - start_time_ - total_paused_time_) |
157 start_time() - | 154 .InSecondsF(); |
158 total_paused_time_ + | |
159 time_offset_); | |
160 } | 155 } |
161 | 156 |
162 double Animation::TrimTimeToCurrentIteration(double monotonic_time) const { | 157 double Animation::TrimTimeToCurrentIteration( |
mithro-old
2014/05/13 09:25:30
Why does this function still return a double? Are
Sikugu_
2014/05/14 14:23:18
I would like to change this once we convert the an
| |
163 double trimmed = monotonic_time + time_offset_; | 158 base::TimeTicks monotonic_time) const { |
159 base::TimeTicks trimmed = monotonic_time + time_offset_; | |
164 | 160 |
165 // If we're paused, time is 'stuck' at the pause time. | 161 // If we're paused, time is 'stuck' at the pause time. |
166 if (run_state_ == Paused) | 162 if (run_state_ == Paused) |
167 trimmed = pause_time_; | 163 trimmed = pause_time_; |
168 | 164 |
169 // Returned time should always be relative to the start time and should | 165 // Returned time should always be relative to the start time and should |
170 // subtract all time spent paused. | 166 // subtract all time spent paused. |
171 trimmed -= start_time_ + total_paused_time_; | 167 trimmed -= (start_time_ - base::TimeTicks()) + total_paused_time_; |
mithro-old
2014/05/13 09:25:30
I don't think you need the "- base::TimeTicks()" h
Sikugu_
2014/05/14 14:23:18
We can just add or delete TimeDeltas from TimeTick
| |
172 | 168 |
173 // If we're just starting or we're waiting on receiving a start time, | 169 // If we're just starting or we're waiting on receiving a start time, |
174 // time is 'stuck' at the initial state. | 170 // time is 'stuck' at the initial state. |
175 if ((run_state_ == Starting && !has_set_start_time()) || | 171 if ((run_state_ == Starting && !has_set_start_time()) || |
176 needs_synchronized_start_time()) | 172 needs_synchronized_start_time()) |
177 trimmed = time_offset_; | 173 trimmed = base::TimeTicks() + time_offset_; |
174 | |
175 double trimmed_in_seconds = (trimmed - base::TimeTicks()).InSecondsF(); | |
mithro-old
2014/05/13 09:25:30
I don't see why you need to convert to seconds in
Sikugu_
2014/05/14 14:23:18
fmod and division won't work well with TimeTicks.
| |
178 | 176 |
179 // Return 0 if we are before the start of the animation | 177 // Return 0 if we are before the start of the animation |
180 if (trimmed < 0) | 178 if (trimmed_in_seconds < 0) |
181 return 0; | 179 return 0; |
182 | 180 |
183 // Always return zero if we have no iterations. | 181 // Always return zero if we have no iterations. |
184 if (!iterations_) | 182 if (!iterations_) |
185 return 0; | 183 return 0; |
186 | 184 |
187 // Don't attempt to trim if we have no duration. | 185 // Don't attempt to trim if we have no duration. |
188 if (curve_->Duration() <= 0) | 186 if (curve_->Duration() <= 0) |
189 return 0; | 187 return 0; |
190 | 188 |
191 // check if we are past active interval | 189 // check if we are past active interval |
192 bool is_past_total_duration = | 190 bool is_past_total_duration = |
193 (iterations_ > 0 && trimmed >= curve_->Duration() * iterations_); | 191 (iterations_ > 0 && |
192 trimmed_in_seconds >= curve_->Duration() * iterations_); | |
194 | 193 |
195 // We need to know the current iteration if we're alternating. | 194 // We need to know the current iteration if we're alternating. |
196 int iteration = 0; | 195 int iteration = 0; |
197 | 196 |
198 // If we are past the active interval, return iteration duration. | 197 // If we are past the active interval, return iteration duration. |
199 if (is_past_total_duration) { | 198 if (is_past_total_duration) { |
200 iteration = iterations_ - 1; | 199 iteration = iterations_ - 1; |
201 trimmed = curve_->Duration(); | 200 trimmed_in_seconds = curve_->Duration(); |
202 } else { | 201 } else { |
203 iteration = static_cast<int>(trimmed / curve_->Duration()); | 202 iteration = static_cast<int>(trimmed_in_seconds / curve_->Duration()); |
204 // Calculate x where trimmed = x + n * curve_->Duration() for some positive | 203 // Calculate x where trimmed = x + n * curve_->Duration() for some positive |
205 // integer n. | 204 // integer n. |
206 trimmed = fmod(trimmed, curve_->Duration()); | 205 trimmed_in_seconds = fmod(trimmed_in_seconds, curve_->Duration()); |
207 } | 206 } |
208 | 207 |
209 // check if we are running the animation in reverse direction for the current | 208 // check if we are running the animation in reverse direction for the current |
210 // iteration | 209 // iteration |
211 bool reverse = (direction_ == Reverse) || | 210 bool reverse = (direction_ == Reverse) || |
212 (direction_ == Alternate && iteration % 2 == 1) || | 211 (direction_ == Alternate && iteration % 2 == 1) || |
213 (direction_ == AlternateReverse && iteration % 2 == 0); | 212 (direction_ == AlternateReverse && iteration % 2 == 0); |
214 | 213 |
215 // if we are running the animation in reverse direction, reverse the result | 214 // if we are running the animation in reverse direction, reverse the result |
216 if (reverse) | 215 if (reverse) |
217 return curve_->Duration() - trimmed; | 216 return curve_->Duration() - trimmed_in_seconds; |
218 | 217 |
219 return trimmed; | 218 return trimmed_in_seconds; |
220 } | 219 } |
221 | 220 |
222 scoped_ptr<Animation> Animation::CloneAndInitialize( | 221 scoped_ptr<Animation> Animation::CloneAndInitialize( |
223 RunState initial_run_state) const { | 222 RunState initial_run_state) const { |
224 scoped_ptr<Animation> to_return( | 223 scoped_ptr<Animation> to_return( |
225 new Animation(curve_->Clone(), id_, group_, target_property_)); | 224 new Animation(curve_->Clone(), id_, group_, target_property_)); |
226 to_return->run_state_ = initial_run_state; | 225 to_return->run_state_ = initial_run_state; |
227 to_return->iterations_ = iterations_; | 226 to_return->iterations_ = iterations_; |
228 to_return->start_time_ = start_time_; | 227 to_return->start_time_ = start_time_; |
229 to_return->pause_time_ = pause_time_; | 228 to_return->pause_time_ = pause_time_; |
(...skipping 10 matching lines...) Expand all Loading... | |
240 // the main thread. | 239 // the main thread. |
241 if (run_state_ == Animation::Paused || | 240 if (run_state_ == Animation::Paused || |
242 other->run_state_ == Animation::Paused) { | 241 other->run_state_ == Animation::Paused) { |
243 other->run_state_ = run_state_; | 242 other->run_state_ = run_state_; |
244 other->pause_time_ = pause_time_; | 243 other->pause_time_ = pause_time_; |
245 other->total_paused_time_ = total_paused_time_; | 244 other->total_paused_time_ = total_paused_time_; |
246 } | 245 } |
247 } | 246 } |
248 | 247 |
249 } // namespace cc | 248 } // namespace cc |
OLD | NEW |