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.
5 #include "cc/animation/animation.h"
9 #include "base/debug/trace_event.h"
10 #include "base/strings/string_util.h"
11 #include "cc/animation/animation_curve.h"
15 // This should match the RunState enum.
16 static const char* const s_runStateNames[] = {
17 "WaitingForTargetAvailability",
26 COMPILE_ASSERT(static_cast<int>(cc::Animation::RunStateEnumSize) ==
27 arraysize(s_runStateNames),
28 RunState_names_match_enum);
30 // This should match the TargetProperty enum.
31 static const char* const s_targetPropertyNames[] = {
39 COMPILE_ASSERT(static_cast<int>(cc::Animation::TargetPropertyEnumSize) ==
40 arraysize(s_targetPropertyNames),
41 TargetProperty_names_match_enum);
47 scoped_ptr<Animation> Animation::Create(
48 scoped_ptr<AnimationCurve> curve,
51 TargetProperty target_property) {
52 return make_scoped_ptr(new Animation(curve.Pass(),
57 Animation::Animation(scoped_ptr<AnimationCurve> curve,
60 TargetProperty target_property)
61 : curve_(curve.Pass()),
64 target_property_(target_property),
65 run_state_(WaitingForTargetAvailability),
70 fill_mode_(FillModeBoth),
71 needs_synchronized_start_time_(false),
72 received_finished_event_(false),
74 is_controlling_instance_(false),
76 affects_active_observers_(true),
77 affects_pending_observers_(true) {
80 Animation::~Animation() {
81 if (run_state_ == Running || run_state_ == Paused)
82 SetRunState(Aborted, base::TimeTicks());
85 void Animation::SetRunState(RunState run_state,
86 base::TimeTicks monotonic_time) {
90 char name_buffer[256];
91 base::snprintf(name_buffer,
94 s_targetPropertyNames[target_property_],
97 bool is_waiting_to_start = run_state_ == WaitingForTargetAvailability ||
98 run_state_ == Starting;
100 if (is_controlling_instance_ && is_waiting_to_start && run_state == Running) {
101 TRACE_EVENT_ASYNC_BEGIN1(
102 "cc", "Animation", this, "Name", TRACE_STR_COPY(name_buffer));
105 bool was_finished = is_finished();
107 const char* old_run_state_name = s_runStateNames[run_state_];
109 if (run_state == Running && run_state_ == Paused)
110 total_paused_time_ += (monotonic_time - pause_time_);
111 else if (run_state == Paused)
112 pause_time_ = monotonic_time;
113 run_state_ = run_state;
115 const char* new_run_state_name = s_runStateNames[run_state];
117 if (is_controlling_instance_ && !was_finished && is_finished())
118 TRACE_EVENT_ASYNC_END0("cc", "Animation", this);
120 char state_buffer[256];
121 base::snprintf(state_buffer,
122 sizeof(state_buffer),
127 TRACE_EVENT_INSTANT2("cc",
128 "LayerAnimationController::SetRunState",
129 TRACE_EVENT_SCOPE_THREAD,
131 TRACE_STR_COPY(name_buffer),
133 TRACE_STR_COPY(state_buffer));
136 void Animation::Suspend(base::TimeTicks monotonic_time) {
137 SetRunState(Paused, monotonic_time);
141 void Animation::Resume(base::TimeTicks monotonic_time) {
143 SetRunState(Running, monotonic_time);
146 bool Animation::IsFinishedAt(base::TimeTicks monotonic_time) const {
150 if (needs_synchronized_start_time_)
153 if (playback_rate_ == 0)
156 return run_state_ == Running && iterations_ >= 0 &&
157 iterations_ * curve_->Duration() / std::abs(playback_rate_) <=
158 (monotonic_time + time_offset_ - start_time_ - total_paused_time_)
162 bool Animation::InEffect(base::TimeTicks monotonic_time) const {
163 return ConvertToActiveTime(monotonic_time) >= 0 ||
164 (fill_mode_ == FillModeBoth || fill_mode_ == FillModeBackwards);
167 double Animation::ConvertToActiveTime(base::TimeTicks monotonic_time) const {
168 base::TimeTicks trimmed = monotonic_time + time_offset_;
170 // If we're paused, time is 'stuck' at the pause time.
171 if (run_state_ == Paused)
172 trimmed = pause_time_;
174 // Returned time should always be relative to the start time and should
175 // subtract all time spent paused.
176 trimmed -= (start_time_ - base::TimeTicks()) + total_paused_time_;
178 // If we're just starting or we're waiting on receiving a start time,
179 // time is 'stuck' at the initial state.
180 if ((run_state_ == Starting && !has_set_start_time()) ||
181 needs_synchronized_start_time())
182 trimmed = base::TimeTicks() + time_offset_;
184 return (trimmed - base::TimeTicks()).InSecondsF();
187 double Animation::TrimTimeToCurrentIteration(
188 base::TimeTicks monotonic_time) const {
189 // Check for valid parameters
190 DCHECK(playback_rate_);
191 DCHECK_GE(iteration_start_, 0);
193 double active_time = ConvertToActiveTime(monotonic_time);
194 double start_offset = iteration_start_ * curve_->Duration();
196 // Return start offset if we are before the start of the animation
200 // Always return zero if we have no iterations.
204 // Don't attempt to trim if we have no duration.
205 if (curve_->Duration() <= 0)
208 double repeated_duration = iterations_ * curve_->Duration();
209 double active_duration = repeated_duration / std::abs(playback_rate_);
211 // Check if we are past active duration
212 if (iterations_ > 0 && active_time >= active_duration)
213 active_time = active_duration;
215 // Calculate the scaled active time
216 double scaled_active_time;
217 if (playback_rate_ < 0)
219 (active_time - active_duration) * playback_rate_ + start_offset;
221 scaled_active_time = active_time * playback_rate_ + start_offset;
223 // Calculate the iteration time
224 double iteration_time;
225 if (scaled_active_time - start_offset == repeated_duration &&
226 fmod(iterations_ + iteration_start_, 1) == 0)
227 iteration_time = curve_->Duration();
229 iteration_time = fmod(scaled_active_time, curve_->Duration());
231 // Calculate the current iteration
233 if (scaled_active_time <= 0)
235 else if (iteration_time == curve_->Duration())
236 iteration = ceil(iteration_start_ + iterations_ - 1);
238 iteration = static_cast<int>(scaled_active_time / curve_->Duration());
240 // Check if we are running the animation in reverse direction for the current
242 bool reverse = (direction_ == Reverse) ||
243 (direction_ == Alternate && iteration % 2 == 1) ||
244 (direction_ == AlternateReverse && iteration % 2 == 0);
246 // If we are running the animation in reverse direction, reverse the result
248 iteration_time = curve_->Duration() - iteration_time;
250 return iteration_time;
253 scoped_ptr<Animation> Animation::CloneAndInitialize(
254 RunState initial_run_state) const {
255 scoped_ptr<Animation> to_return(
256 new Animation(curve_->Clone(), id_, group_, target_property_));
257 to_return->run_state_ = initial_run_state;
258 to_return->iterations_ = iterations_;
259 to_return->iteration_start_ = iteration_start_;
260 to_return->start_time_ = start_time_;
261 to_return->pause_time_ = pause_time_;
262 to_return->total_paused_time_ = total_paused_time_;
263 to_return->time_offset_ = time_offset_;
264 to_return->direction_ = direction_;
265 to_return->playback_rate_ = playback_rate_;
266 to_return->fill_mode_ = fill_mode_;
267 DCHECK(!to_return->is_controlling_instance_);
268 to_return->is_controlling_instance_ = true;
269 return to_return.Pass();
272 void Animation::PushPropertiesTo(Animation* other) const {
273 // Currently, we only push changes due to pausing and resuming animations on
275 if (run_state_ == Animation::Paused ||
276 other->run_state_ == Animation::Paused) {
277 other->run_state_ = run_state_;
278 other->pause_time_ = pause_time_;
279 other->total_paused_time_ = total_paused_time_;