Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(37)

Side by Side Diff: cc/animation/animation.cc

Issue 693883004: Make cc::animation::TrimTimeToCurrentIteration,cc::AnimationCurve::Duration use TimeTicks/TimeDelta (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 6 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « cc/animation/animation.h ('k') | cc/animation/animation_curve.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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"
11 #include "cc/animation/animation_curve.h" 11 #include "cc/animation/animation_curve.h"
12 #include "cc/base/time_util.h"
12 13
13 namespace { 14 namespace {
14 15
15 // This should match the RunState enum. 16 // This should match the RunState enum.
16 static const char* const s_runStateNames[] = { 17 static const char* const s_runStateNames[] = {
17 "WaitingForTargetAvailability", 18 "WaitingForTargetAvailability",
18 "WaitingForDeletion", 19 "WaitingForDeletion",
19 "Starting", 20 "Starting",
20 "Running", 21 "Running",
21 "Paused", 22 "Paused",
(...skipping 125 matching lines...) Expand 10 before | Expand all | Expand 10 after
147 if (is_finished()) 148 if (is_finished())
148 return true; 149 return true;
149 150
150 if (needs_synchronized_start_time_) 151 if (needs_synchronized_start_time_)
151 return false; 152 return false;
152 153
153 if (playback_rate_ == 0) 154 if (playback_rate_ == 0)
154 return false; 155 return false;
155 156
156 return run_state_ == Running && iterations_ >= 0 && 157 return run_state_ == Running && iterations_ >= 0 &&
157 iterations_ * curve_->Duration() / std::abs(playback_rate_) <= 158 TimeUtil::Scale(curve_->Duration(),
158 (monotonic_time + time_offset_ - start_time_ - total_paused_time_) 159 iterations_ / std::abs(playback_rate_)) <=
159 .InSecondsF(); 160 (monotonic_time + time_offset_ - start_time_ - total_paused_time_);
160 } 161 }
161 162
162 bool Animation::InEffect(base::TimeTicks monotonic_time) const { 163 bool Animation::InEffect(base::TimeTicks monotonic_time) const {
163 return ConvertToActiveTime(monotonic_time) >= 0 || 164 return ConvertToActiveTime(monotonic_time) >= base::TimeDelta() ||
164 (fill_mode_ == FillModeBoth || fill_mode_ == FillModeBackwards); 165 (fill_mode_ == FillModeBoth || fill_mode_ == FillModeBackwards);
165 } 166 }
166 167
167 double Animation::ConvertToActiveTime(base::TimeTicks monotonic_time) const { 168 base::TimeDelta Animation::ConvertToActiveTime(
169 base::TimeTicks monotonic_time) const {
168 base::TimeTicks trimmed = monotonic_time + time_offset_; 170 base::TimeTicks trimmed = monotonic_time + time_offset_;
169 171
170 // If we're paused, time is 'stuck' at the pause time. 172 // If we're paused, time is 'stuck' at the pause time.
171 if (run_state_ == Paused) 173 if (run_state_ == Paused)
172 trimmed = pause_time_; 174 trimmed = pause_time_;
173 175
174 // Returned time should always be relative to the start time and should 176 // Returned time should always be relative to the start time and should
175 // subtract all time spent paused. 177 // subtract all time spent paused.
176 trimmed -= (start_time_ - base::TimeTicks()) + total_paused_time_; 178 trimmed -= (start_time_ - base::TimeTicks()) + total_paused_time_;
177 179
178 // If we're just starting or we're waiting on receiving a start time, 180 // If we're just starting or we're waiting on receiving a start time,
179 // time is 'stuck' at the initial state. 181 // time is 'stuck' at the initial state.
180 if ((run_state_ == Starting && !has_set_start_time()) || 182 if ((run_state_ == Starting && !has_set_start_time()) ||
181 needs_synchronized_start_time()) 183 needs_synchronized_start_time())
182 trimmed = base::TimeTicks() + time_offset_; 184 trimmed = base::TimeTicks() + time_offset_;
183 185
184 return (trimmed - base::TimeTicks()).InSecondsF(); 186 return (trimmed - base::TimeTicks());
185 } 187 }
186 188
187 double Animation::TrimTimeToCurrentIteration( 189 base::TimeDelta Animation::TrimTimeToCurrentIteration(
188 base::TimeTicks monotonic_time) const { 190 base::TimeTicks monotonic_time) const {
189 // Check for valid parameters 191 // Check for valid parameters
190 DCHECK(playback_rate_); 192 DCHECK(playback_rate_);
191 DCHECK_GE(iteration_start_, 0); 193 DCHECK_GE(iteration_start_, 0);
192 194
193 double active_time = ConvertToActiveTime(monotonic_time); 195 base::TimeDelta active_time = ConvertToActiveTime(monotonic_time);
194 double start_offset = iteration_start_ * curve_->Duration(); 196 base::TimeDelta start_offset =
197 TimeUtil::Scale(curve_->Duration(), iteration_start_);
195 198
196 // Return start offset if we are before the start of the animation 199 // Return start offset if we are before the start of the animation
197 if (active_time < 0) 200 if (active_time < base::TimeDelta())
198 return start_offset; 201 return start_offset;
199
200 // Always return zero if we have no iterations. 202 // Always return zero if we have no iterations.
201 if (!iterations_) 203 if (!iterations_)
202 return 0; 204 return base::TimeDelta();
203 205
204 // Don't attempt to trim if we have no duration. 206 // Don't attempt to trim if we have no duration.
205 if (curve_->Duration() <= 0) 207 if (curve_->Duration() <= base::TimeDelta())
206 return 0; 208 return base::TimeDelta();
207 209
208 double repeated_duration = iterations_ * curve_->Duration(); 210 base::TimeDelta repeated_duration =
209 double active_duration = repeated_duration / std::abs(playback_rate_); 211 TimeUtil::Scale(curve_->Duration(), iterations_);
212 base::TimeDelta active_duration =
213 TimeUtil::Scale(repeated_duration, 1.0 / std::abs(playback_rate_));
210 214
211 // Check if we are past active duration 215 // Check if we are past active duration
212 if (iterations_ > 0 && active_time >= active_duration) 216 if (iterations_ > 0 && active_time >= active_duration)
213 active_time = active_duration; 217 active_time = active_duration;
214 218
215 // Calculate the scaled active time 219 // Calculate the scaled active time
216 double scaled_active_time; 220 base::TimeDelta scaled_active_time;
217 if (playback_rate_ < 0) 221 if (playback_rate_ < 0)
218 scaled_active_time = 222 scaled_active_time =
219 (active_time - active_duration) * playback_rate_ + start_offset; 223 TimeUtil::Scale((active_time - active_duration), playback_rate_) +
224 start_offset;
220 else 225 else
221 scaled_active_time = active_time * playback_rate_ + start_offset; 226 scaled_active_time =
227 TimeUtil::Scale(active_time, playback_rate_) + start_offset;
222 228
223 // Calculate the iteration time 229 // Calculate the iteration time
224 double iteration_time; 230 base::TimeDelta iteration_time;
225 if (scaled_active_time - start_offset == repeated_duration && 231 if (scaled_active_time - start_offset == repeated_duration &&
226 fmod(iterations_ + iteration_start_, 1) == 0) 232 fmod(iterations_ + iteration_start_, 1) == 0)
227 iteration_time = curve_->Duration(); 233 iteration_time = curve_->Duration();
228 else 234 else
229 iteration_time = fmod(scaled_active_time, curve_->Duration()); 235 iteration_time = TimeUtil::Mod(scaled_active_time, curve_->Duration());
230 236
231 // Calculate the current iteration 237 // Calculate the current iteration
232 int iteration; 238 int iteration;
233 if (scaled_active_time <= 0) 239 if (scaled_active_time <= base::TimeDelta())
234 iteration = 0; 240 iteration = 0;
235 else if (iteration_time == curve_->Duration()) 241 else if (iteration_time == curve_->Duration())
236 iteration = ceil(iteration_start_ + iterations_ - 1); 242 iteration = ceil(iteration_start_ + iterations_ - 1);
237 else 243 else
238 iteration = static_cast<int>(scaled_active_time / curve_->Duration()); 244 iteration = static_cast<int>(scaled_active_time / curve_->Duration());
239 245
240 // Check if we are running the animation in reverse direction for the current 246 // Check if we are running the animation in reverse direction for the current
241 // iteration 247 // iteration
242 bool reverse = (direction_ == Reverse) || 248 bool reverse = (direction_ == Reverse) ||
243 (direction_ == Alternate && iteration % 2 == 1) || 249 (direction_ == Alternate && iteration % 2 == 1) ||
(...skipping 30 matching lines...) Expand all
274 // the main thread. 280 // the main thread.
275 if (run_state_ == Animation::Paused || 281 if (run_state_ == Animation::Paused ||
276 other->run_state_ == Animation::Paused) { 282 other->run_state_ == Animation::Paused) {
277 other->run_state_ = run_state_; 283 other->run_state_ = run_state_;
278 other->pause_time_ = pause_time_; 284 other->pause_time_ = pause_time_;
279 other->total_paused_time_ = total_paused_time_; 285 other->total_paused_time_ = total_paused_time_;
280 } 286 }
281 } 287 }
282 288
283 } // namespace cc 289 } // namespace cc
OLDNEW
« no previous file with comments | « cc/animation/animation.h ('k') | cc/animation/animation_curve.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698