Chromium Code Reviews| 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" |
| 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 126 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 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 iterations_ * curve_->Duration() / std::abs(playback_rate_) <= |
| 158 (monotonic_time + time_offset_ - start_time_ - total_paused_time_) | 159 (monotonic_time + time_offset_ - start_time_ - total_paused_time_); |
| 159 .InSecondsF(); | |
| 160 } | 160 } |
| 161 | 161 |
| 162 bool Animation::InEffect(base::TimeTicks monotonic_time) const { | 162 bool Animation::InEffect(base::TimeTicks monotonic_time) const { |
| 163 return ConvertToActiveTime(monotonic_time) >= 0 || | 163 return ConvertToActiveTime(monotonic_time) >= base::TimeDelta() || |
| 164 (fill_mode_ == FillModeBoth || fill_mode_ == FillModeBackwards); | 164 (fill_mode_ == FillModeBoth || fill_mode_ == FillModeBackwards); |
| 165 } | 165 } |
| 166 | 166 |
| 167 double Animation::ConvertToActiveTime(base::TimeTicks monotonic_time) const { | 167 base::TimeDelta Animation::ConvertToActiveTime( |
| 168 base::TimeTicks monotonic_time) const { | |
| 168 base::TimeTicks trimmed = monotonic_time + time_offset_; | 169 base::TimeTicks trimmed = monotonic_time + time_offset_; |
| 169 | 170 |
| 170 // If we're paused, time is 'stuck' at the pause time. | 171 // If we're paused, time is 'stuck' at the pause time. |
| 171 if (run_state_ == Paused) | 172 if (run_state_ == Paused) |
| 172 trimmed = pause_time_; | 173 trimmed = pause_time_; |
| 173 | 174 |
| 174 // Returned time should always be relative to the start time and should | 175 // Returned time should always be relative to the start time and should |
| 175 // subtract all time spent paused. | 176 // subtract all time spent paused. |
| 176 trimmed -= (start_time_ - base::TimeTicks()) + total_paused_time_; | 177 trimmed -= (start_time_ - base::TimeTicks()) + total_paused_time_; |
| 177 | 178 |
| 178 // If we're just starting or we're waiting on receiving a start time, | 179 // If we're just starting or we're waiting on receiving a start time, |
| 179 // time is 'stuck' at the initial state. | 180 // time is 'stuck' at the initial state. |
| 180 if ((run_state_ == Starting && !has_set_start_time()) || | 181 if ((run_state_ == Starting && !has_set_start_time()) || |
| 181 needs_synchronized_start_time()) | 182 needs_synchronized_start_time()) |
| 182 trimmed = base::TimeTicks() + time_offset_; | 183 trimmed = base::TimeTicks() + time_offset_; |
| 183 | 184 |
| 184 return (trimmed - base::TimeTicks()).InSecondsF(); | 185 return (trimmed - base::TimeTicks()); |
| 185 } | 186 } |
| 186 | 187 |
| 187 double Animation::TrimTimeToCurrentIteration( | 188 base::TimeDelta Animation::TrimTimeToCurrentIteration( |
| 188 base::TimeTicks monotonic_time) const { | 189 base::TimeTicks monotonic_time) const { |
| 189 // Check for valid parameters | 190 // Check for valid parameters |
| 190 DCHECK(playback_rate_); | 191 DCHECK(playback_rate_); |
| 191 DCHECK_GE(iteration_start_, 0); | 192 DCHECK_GE(iteration_start_, 0); |
| 192 | 193 |
| 193 double active_time = ConvertToActiveTime(monotonic_time); | 194 base::TimeDelta active_time = ConvertToActiveTime(monotonic_time); |
| 194 double start_offset = iteration_start_ * curve_->Duration(); | 195 base::TimeDelta start_offset = iteration_start_ * curve_->Duration(); |
| 195 | 196 |
| 196 // Return start offset if we are before the start of the animation | 197 // Return start offset if we are before the start of the animation |
| 197 if (active_time < 0) | 198 if (active_time < base::TimeDelta()) |
| 198 return start_offset; | 199 return start_offset; |
| 199 | |
| 200 // Always return zero if we have no iterations. | 200 // Always return zero if we have no iterations. |
| 201 if (!iterations_) | 201 if (!iterations_) |
| 202 return 0; | 202 return base::TimeDelta(); |
| 203 | 203 |
| 204 // Don't attempt to trim if we have no duration. | 204 // Don't attempt to trim if we have no duration. |
| 205 if (curve_->Duration() <= 0) | 205 if (curve_->Duration() <= base::TimeDelta()) |
| 206 return 0; | 206 return base::TimeDelta(); |
| 207 | 207 |
| 208 double repeated_duration = iterations_ * curve_->Duration(); | 208 base::TimeDelta repeated_duration = iterations_ * curve_->Duration(); |
| 209 double active_duration = repeated_duration / std::abs(playback_rate_); | 209 base::TimeDelta active_duration = |
| 210 repeated_duration / std::abs(playback_rate_); | |
| 210 | 211 |
| 211 // Check if we are past active duration | 212 // Check if we are past active duration |
| 212 if (iterations_ > 0 && active_time >= active_duration) | 213 if (iterations_ > 0 && active_time >= active_duration) |
| 213 active_time = active_duration; | 214 active_time = active_duration; |
| 214 | 215 |
| 215 // Calculate the scaled active time | 216 // Calculate the scaled active time |
| 216 double scaled_active_time; | 217 base::TimeDelta scaled_active_time; |
| 217 if (playback_rate_ < 0) | 218 if (playback_rate_ < 0) |
| 218 scaled_active_time = | 219 scaled_active_time = |
| 219 (active_time - active_duration) * playback_rate_ + start_offset; | 220 (active_time - active_duration) * playback_rate_ + start_offset; |
| 220 else | 221 else |
| 221 scaled_active_time = active_time * playback_rate_ + start_offset; | 222 scaled_active_time = active_time * playback_rate_ + start_offset; |
| 222 | 223 |
| 223 // Calculate the iteration time | 224 // Calculate the iteration time |
| 224 double iteration_time; | 225 base::TimeDelta iteration_time; |
| 225 if (scaled_active_time - start_offset == repeated_duration && | 226 if (scaled_active_time - start_offset == repeated_duration && |
| 226 fmod(iterations_ + iteration_start_, 1) == 0) | 227 fmod(iterations_ + iteration_start_, 1) == 0) |
| 227 iteration_time = curve_->Duration(); | 228 iteration_time = curve_->Duration(); |
| 228 else | 229 else |
| 229 iteration_time = fmod(scaled_active_time, curve_->Duration()); | 230 iteration_time = base::TimeDelta::FromSecondsD( |
| 231 fmod(scaled_active_time.InSecondsF(), curve_->Duration().InSecondsF())); | |
|
ajuma
2014/11/06 18:03:23
Could we add a helper function (e.g. in the TimeUt
patro
2014/11/07 07:03:40
Done.
| |
| 230 | 232 |
| 231 // Calculate the current iteration | 233 // Calculate the current iteration |
| 232 int iteration; | 234 int iteration; |
| 233 if (scaled_active_time <= 0) | 235 if (scaled_active_time <= base::TimeDelta()) |
| 234 iteration = 0; | 236 iteration = 0; |
| 235 else if (iteration_time == curve_->Duration()) | 237 else if (iteration_time == curve_->Duration()) |
| 236 iteration = ceil(iteration_start_ + iterations_ - 1); | 238 iteration = ceil(iteration_start_ + iterations_ - 1); |
| 237 else | 239 else |
| 238 iteration = static_cast<int>(scaled_active_time / curve_->Duration()); | 240 iteration = static_cast<int>(scaled_active_time / curve_->Duration()); |
| 239 | 241 |
| 240 // Check if we are running the animation in reverse direction for the current | 242 // Check if we are running the animation in reverse direction for the current |
| 241 // iteration | 243 // iteration |
| 242 bool reverse = (direction_ == Reverse) || | 244 bool reverse = (direction_ == Reverse) || |
| 243 (direction_ == Alternate && iteration % 2 == 1) || | 245 (direction_ == Alternate && iteration % 2 == 1) || |
| 244 (direction_ == AlternateReverse && iteration % 2 == 0); | 246 (direction_ == AlternateReverse && iteration % 2 == 0); |
| 245 | 247 |
| 246 // If we are running the animation in reverse direction, reverse the result | 248 // If we are running the animation in reverse direction, reverse the result |
| 247 if (reverse) | 249 if (reverse) |
| 248 iteration_time = curve_->Duration() - iteration_time; | 250 iteration_time = curve_->Duration() - iteration_time; |
| 249 | |
| 250 return iteration_time; | 251 return iteration_time; |
| 251 } | 252 } |
| 252 | 253 |
| 253 scoped_ptr<Animation> Animation::CloneAndInitialize( | 254 scoped_ptr<Animation> Animation::CloneAndInitialize( |
| 254 RunState initial_run_state) const { | 255 RunState initial_run_state) const { |
| 255 scoped_ptr<Animation> to_return( | 256 scoped_ptr<Animation> to_return( |
| 256 new Animation(curve_->Clone(), id_, group_, target_property_)); | 257 new Animation(curve_->Clone(), id_, group_, target_property_)); |
| 257 to_return->run_state_ = initial_run_state; | 258 to_return->run_state_ = initial_run_state; |
| 258 to_return->iterations_ = iterations_; | 259 to_return->iterations_ = iterations_; |
| 259 to_return->iteration_start_ = iteration_start_; | 260 to_return->iteration_start_ = iteration_start_; |
| (...skipping 14 matching lines...) Expand all Loading... | |
| 274 // the main thread. | 275 // the main thread. |
| 275 if (run_state_ == Animation::Paused || | 276 if (run_state_ == Animation::Paused || |
| 276 other->run_state_ == Animation::Paused) { | 277 other->run_state_ == Animation::Paused) { |
| 277 other->run_state_ = run_state_; | 278 other->run_state_ = run_state_; |
| 278 other->pause_time_ = pause_time_; | 279 other->pause_time_ = pause_time_; |
| 279 other->total_paused_time_ = total_paused_time_; | 280 other->total_paused_time_ = total_paused_time_; |
| 280 } | 281 } |
| 281 } | 282 } |
| 282 | 283 |
| 283 } // namespace cc | 284 } // namespace cc |
| OLD | NEW |