Explicitly add python-numpy dependency to install-build-deps.
[chromium-blink-merge.git] / cc / animation / animation.cc
blobf5abfa0febd45e6bebdd4a92e290f5bf21a9b97c
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"
7 #include <cmath>
9 #include "base/debug/trace_event.h"
10 #include "base/strings/string_util.h"
11 #include "cc/animation/animation_curve.h"
12 #include "cc/base/time_util.h"
14 namespace {
16 // This should match the RunState enum.
17 static const char* const s_runStateNames[] = {
18 "WaitingForTargetAvailability",
19 "WaitingForDeletion",
20 "Starting",
21 "Running",
22 "Paused",
23 "Finished",
24 "Aborted"
27 COMPILE_ASSERT(static_cast<int>(cc::Animation::RunStateEnumSize) ==
28 arraysize(s_runStateNames),
29 RunState_names_match_enum);
31 // This should match the TargetProperty enum.
32 static const char* const s_targetPropertyNames[] = {
33 "Transform",
34 "Opacity",
35 "Filter",
36 "ScrollOffset",
37 "BackgroundColor"
40 COMPILE_ASSERT(static_cast<int>(cc::Animation::TargetPropertyEnumSize) ==
41 arraysize(s_targetPropertyNames),
42 TargetProperty_names_match_enum);
44 } // namespace
46 namespace cc {
48 scoped_ptr<Animation> Animation::Create(
49 scoped_ptr<AnimationCurve> curve,
50 int animation_id,
51 int group_id,
52 TargetProperty target_property) {
53 return make_scoped_ptr(new Animation(curve.Pass(),
54 animation_id,
55 group_id,
56 target_property)); }
58 Animation::Animation(scoped_ptr<AnimationCurve> curve,
59 int animation_id,
60 int group_id,
61 TargetProperty target_property)
62 : curve_(curve.Pass()),
63 id_(animation_id),
64 group_(group_id),
65 target_property_(target_property),
66 run_state_(WaitingForTargetAvailability),
67 iterations_(1),
68 iteration_start_(0),
69 direction_(Normal),
70 playback_rate_(1),
71 fill_mode_(FillModeBoth),
72 needs_synchronized_start_time_(false),
73 received_finished_event_(false),
74 suspended_(false),
75 is_controlling_instance_(false),
76 is_impl_only_(false),
77 affects_active_observers_(true),
78 affects_pending_observers_(true) {
81 Animation::~Animation() {
82 if (run_state_ == Running || run_state_ == Paused)
83 SetRunState(Aborted, base::TimeTicks());
86 void Animation::SetRunState(RunState run_state,
87 base::TimeTicks monotonic_time) {
88 if (suspended_)
89 return;
91 char name_buffer[256];
92 base::snprintf(name_buffer,
93 sizeof(name_buffer),
94 "%s-%d",
95 s_targetPropertyNames[target_property_],
96 group_);
98 bool is_waiting_to_start = run_state_ == WaitingForTargetAvailability ||
99 run_state_ == Starting;
101 if (is_controlling_instance_ && is_waiting_to_start && run_state == Running) {
102 TRACE_EVENT_ASYNC_BEGIN1(
103 "cc", "Animation", this, "Name", TRACE_STR_COPY(name_buffer));
106 bool was_finished = is_finished();
108 const char* old_run_state_name = s_runStateNames[run_state_];
110 if (run_state == Running && run_state_ == Paused)
111 total_paused_time_ += (monotonic_time - pause_time_);
112 else if (run_state == Paused)
113 pause_time_ = monotonic_time;
114 run_state_ = run_state;
116 const char* new_run_state_name = s_runStateNames[run_state];
118 if (is_controlling_instance_ && !was_finished && is_finished())
119 TRACE_EVENT_ASYNC_END0("cc", "Animation", this);
121 char state_buffer[256];
122 base::snprintf(state_buffer,
123 sizeof(state_buffer),
124 "%s->%s",
125 old_run_state_name,
126 new_run_state_name);
128 TRACE_EVENT_INSTANT2("cc",
129 "LayerAnimationController::SetRunState",
130 TRACE_EVENT_SCOPE_THREAD,
131 "Name",
132 TRACE_STR_COPY(name_buffer),
133 "State",
134 TRACE_STR_COPY(state_buffer));
137 void Animation::Suspend(base::TimeTicks monotonic_time) {
138 SetRunState(Paused, monotonic_time);
139 suspended_ = true;
142 void Animation::Resume(base::TimeTicks monotonic_time) {
143 suspended_ = false;
144 SetRunState(Running, monotonic_time);
147 bool Animation::IsFinishedAt(base::TimeTicks monotonic_time) const {
148 if (is_finished())
149 return true;
151 if (needs_synchronized_start_time_)
152 return false;
154 if (playback_rate_ == 0)
155 return false;
157 return run_state_ == Running && iterations_ >= 0 &&
158 TimeUtil::Scale(curve_->Duration(),
159 iterations_ / std::abs(playback_rate_)) <=
160 (monotonic_time + time_offset_ - start_time_ - total_paused_time_);
163 bool Animation::InEffect(base::TimeTicks monotonic_time) const {
164 return ConvertToActiveTime(monotonic_time) >= base::TimeDelta() ||
165 (fill_mode_ == FillModeBoth || fill_mode_ == FillModeBackwards);
168 base::TimeDelta Animation::ConvertToActiveTime(
169 base::TimeTicks monotonic_time) const {
170 base::TimeTicks trimmed = monotonic_time + time_offset_;
172 // If we're paused, time is 'stuck' at the pause time.
173 if (run_state_ == Paused)
174 trimmed = pause_time_;
176 // Returned time should always be relative to the start time and should
177 // subtract all time spent paused.
178 trimmed -= (start_time_ - base::TimeTicks()) + total_paused_time_;
180 // If we're just starting or we're waiting on receiving a start time,
181 // time is 'stuck' at the initial state.
182 if ((run_state_ == Starting && !has_set_start_time()) ||
183 needs_synchronized_start_time())
184 trimmed = base::TimeTicks() + time_offset_;
186 return (trimmed - base::TimeTicks());
189 base::TimeDelta Animation::TrimTimeToCurrentIteration(
190 base::TimeTicks monotonic_time) const {
191 // Check for valid parameters
192 DCHECK(playback_rate_);
193 DCHECK_GE(iteration_start_, 0);
195 base::TimeDelta active_time = ConvertToActiveTime(monotonic_time);
196 base::TimeDelta start_offset =
197 TimeUtil::Scale(curve_->Duration(), iteration_start_);
199 // Return start offset if we are before the start of the animation
200 if (active_time < base::TimeDelta())
201 return start_offset;
202 // Always return zero if we have no iterations.
203 if (!iterations_)
204 return base::TimeDelta();
206 // Don't attempt to trim if we have no duration.
207 if (curve_->Duration() <= base::TimeDelta())
208 return base::TimeDelta();
210 base::TimeDelta repeated_duration =
211 TimeUtil::Scale(curve_->Duration(), iterations_);
212 base::TimeDelta active_duration =
213 TimeUtil::Scale(repeated_duration, 1.0 / std::abs(playback_rate_));
215 // Check if we are past active duration
216 if (iterations_ > 0 && active_time >= active_duration)
217 active_time = active_duration;
219 // Calculate the scaled active time
220 base::TimeDelta scaled_active_time;
221 if (playback_rate_ < 0)
222 scaled_active_time =
223 TimeUtil::Scale((active_time - active_duration), playback_rate_) +
224 start_offset;
225 else
226 scaled_active_time =
227 TimeUtil::Scale(active_time, playback_rate_) + start_offset;
229 // Calculate the iteration time
230 base::TimeDelta iteration_time;
231 if (scaled_active_time - start_offset == repeated_duration &&
232 fmod(iterations_ + iteration_start_, 1) == 0)
233 iteration_time = curve_->Duration();
234 else
235 iteration_time = TimeUtil::Mod(scaled_active_time, curve_->Duration());
237 // Calculate the current iteration
238 int iteration;
239 if (scaled_active_time <= base::TimeDelta())
240 iteration = 0;
241 else if (iteration_time == curve_->Duration())
242 iteration = ceil(iteration_start_ + iterations_ - 1);
243 else
244 iteration = static_cast<int>(scaled_active_time / curve_->Duration());
246 // Check if we are running the animation in reverse direction for the current
247 // iteration
248 bool reverse = (direction_ == Reverse) ||
249 (direction_ == Alternate && iteration % 2 == 1) ||
250 (direction_ == AlternateReverse && iteration % 2 == 0);
252 // If we are running the animation in reverse direction, reverse the result
253 if (reverse)
254 iteration_time = curve_->Duration() - iteration_time;
256 return iteration_time;
259 scoped_ptr<Animation> Animation::CloneAndInitialize(
260 RunState initial_run_state) const {
261 scoped_ptr<Animation> to_return(
262 new Animation(curve_->Clone(), id_, group_, target_property_));
263 to_return->run_state_ = initial_run_state;
264 to_return->iterations_ = iterations_;
265 to_return->iteration_start_ = iteration_start_;
266 to_return->start_time_ = start_time_;
267 to_return->pause_time_ = pause_time_;
268 to_return->total_paused_time_ = total_paused_time_;
269 to_return->time_offset_ = time_offset_;
270 to_return->direction_ = direction_;
271 to_return->playback_rate_ = playback_rate_;
272 to_return->fill_mode_ = fill_mode_;
273 DCHECK(!to_return->is_controlling_instance_);
274 to_return->is_controlling_instance_ = true;
275 return to_return.Pass();
278 void Animation::PushPropertiesTo(Animation* other) const {
279 // Currently, we only push changes due to pausing and resuming animations on
280 // the main thread.
281 if (run_state_ == Animation::Paused ||
282 other->run_state_ == Animation::Paused) {
283 other->run_state_ = run_state_;
284 other->pause_time_ = pause_time_;
285 other->total_paused_time_ = total_paused_time_;
289 } // namespace cc