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.
4
5 #include "cc/animation/animation.h"
6
7 #include <cmath>
8
9 #include "base/debug/trace_event.h"
10 #include "base/strings/string_util.h"
11 #include "cc/animation/animation_curve.h"
12
13 namespace {
14
15 // This should match the RunState enum.
16 static const char* const s_runStateNames[] = {
17 "WaitingForTargetAvailability",
18 "WaitingForDeletion",
19 "Starting",
20 "Running",
21 "Paused",
22 "Finished",
23 "Aborted"
24 };
25
26 COMPILE_ASSERT(static_cast<int>(cc::Animation::RunStateEnumSize) ==
27 arraysize(s_runStateNames),
28 RunState_names_match_enum);
29
30 // This should match the TargetProperty enum.
31 static const char* const s_targetPropertyNames[] = {
32 "Transform",
33 "Opacity",
34 "Filter",
35 "BackgroundColor",
36 "ScrollOffset"
37 };
38
39 COMPILE_ASSERT(static_cast<int>(cc::Animation::TargetPropertyEnumSize) ==
40 arraysize(s_targetPropertyNames),
41 TargetProperty_names_match_enum);
42
43 } // namespace
44
45 namespace cc {
46
Create(scoped_ptr<AnimationCurve> curve,int animation_id,int group_id,TargetProperty target_property)47 scoped_ptr<Animation> Animation::Create(
48 scoped_ptr<AnimationCurve> curve,
49 int animation_id,
50 int group_id,
51 TargetProperty target_property) {
52 return make_scoped_ptr(new Animation(curve.Pass(),
53 animation_id,
54 group_id,
55 target_property)); }
56
Animation(scoped_ptr<AnimationCurve> curve,int animation_id,int group_id,TargetProperty target_property)57 Animation::Animation(scoped_ptr<AnimationCurve> curve,
58 int animation_id,
59 int group_id,
60 TargetProperty target_property)
61 : curve_(curve.Pass()),
62 id_(animation_id),
63 group_(group_id),
64 target_property_(target_property),
65 run_state_(WaitingForTargetAvailability),
66 iterations_(1),
67 start_time_(0),
68 alternates_direction_(false),
69 time_offset_(0),
70 needs_synchronized_start_time_(false),
71 received_finished_event_(false),
72 suspended_(false),
73 pause_time_(0),
74 total_paused_time_(0),
75 is_controlling_instance_(false),
76 is_impl_only_(false) {}
77
~Animation()78 Animation::~Animation() {
79 if (run_state_ == Running || run_state_ == Paused)
80 SetRunState(Aborted, 0);
81 }
82
SetRunState(RunState run_state,double monotonic_time)83 void Animation::SetRunState(RunState run_state, double monotonic_time) {
84 if (suspended_)
85 return;
86
87 char name_buffer[256];
88 base::snprintf(name_buffer,
89 sizeof(name_buffer),
90 "%s-%d%s",
91 s_targetPropertyNames[target_property_],
92 group_,
93 is_controlling_instance_ ? "(impl)" : "");
94
95 bool is_waiting_to_start = run_state_ == WaitingForTargetAvailability ||
96 run_state_ == Starting;
97
98 if (is_waiting_to_start && run_state == Running) {
99 TRACE_EVENT_ASYNC_BEGIN1(
100 "cc", "Animation", this, "Name", TRACE_STR_COPY(name_buffer));
101 }
102
103 bool was_finished = is_finished();
104
105 const char* old_run_state_name = s_runStateNames[run_state_];
106
107 if (run_state == Running && run_state_ == Paused)
108 total_paused_time_ += monotonic_time - pause_time_;
109 else if (run_state == Paused)
110 pause_time_ = monotonic_time;
111 run_state_ = run_state;
112
113 const char* new_run_state_name = s_runStateNames[run_state];
114
115 if (!was_finished && is_finished())
116 TRACE_EVENT_ASYNC_END0("cc", "Animation", this);
117
118 char state_buffer[256];
119 base::snprintf(state_buffer,
120 sizeof(state_buffer),
121 "%s->%s",
122 old_run_state_name,
123 new_run_state_name);
124
125 TRACE_EVENT_INSTANT2("cc",
126 "LayerAnimationController::SetRunState",
127 TRACE_EVENT_SCOPE_THREAD,
128 "Name",
129 TRACE_STR_COPY(name_buffer),
130 "State",
131 TRACE_STR_COPY(state_buffer));
132 }
133
Suspend(double monotonic_time)134 void Animation::Suspend(double monotonic_time) {
135 SetRunState(Paused, monotonic_time);
136 suspended_ = true;
137 }
138
Resume(double monotonic_time)139 void Animation::Resume(double monotonic_time) {
140 suspended_ = false;
141 SetRunState(Running, monotonic_time);
142 }
143
IsFinishedAt(double monotonic_time) const144 bool Animation::IsFinishedAt(double monotonic_time) const {
145 if (is_finished())
146 return true;
147
148 if (needs_synchronized_start_time_)
149 return false;
150
151 return run_state_ == Running &&
152 iterations_ >= 0 &&
153 iterations_ * curve_->Duration() <= (monotonic_time -
154 start_time() -
155 total_paused_time_ +
156 time_offset_);
157 }
158
TrimTimeToCurrentIteration(double monotonic_time) const159 double Animation::TrimTimeToCurrentIteration(double monotonic_time) const {
160 double trimmed = monotonic_time + time_offset_;
161
162 // If we're paused, time is 'stuck' at the pause time.
163 if (run_state_ == Paused)
164 trimmed = pause_time_;
165
166 // Returned time should always be relative to the start time and should
167 // subtract all time spent paused.
168 trimmed -= start_time_ + total_paused_time_;
169
170 // If we're just starting or we're waiting on receiving a start time,
171 // time is 'stuck' at the initial state.
172 if ((run_state_ == Starting && !has_set_start_time()) ||
173 needs_synchronized_start_time())
174 trimmed = time_offset_;
175
176 // Zero is always the start of the animation.
177 if (trimmed <= 0)
178 return 0;
179
180 // Always return zero if we have no iterations.
181 if (!iterations_)
182 return 0;
183
184 // Don't attempt to trim if we have no duration.
185 if (curve_->Duration() <= 0)
186 return 0;
187
188 // If less than an iteration duration, just return trimmed.
189 if (trimmed < curve_->Duration())
190 return trimmed;
191
192 // If greater than or equal to the total duration, return iteration duration.
193 if (iterations_ >= 0 && trimmed >= curve_->Duration() * iterations_) {
194 if (alternates_direction_ && !(iterations_ % 2))
195 return 0;
196 return curve_->Duration();
197 }
198
199 // We need to know the current iteration if we're alternating.
200 int iteration = static_cast<int>(trimmed / curve_->Duration());
201
202 // Calculate x where trimmed = x + n * curve_->Duration() for some positive
203 // integer n.
204 trimmed = fmod(trimmed, curve_->Duration());
205
206 // If we're alternating and on an odd iteration, reverse the direction.
207 if (alternates_direction_ && iteration % 2 == 1)
208 return curve_->Duration() - trimmed;
209
210 return trimmed;
211 }
212
Clone() const213 scoped_ptr<Animation> Animation::Clone() const {
214 return CloneAndInitialize(run_state_, start_time_);
215 }
216
CloneAndInitialize(RunState initial_run_state,double start_time) const217 scoped_ptr<Animation> Animation::CloneAndInitialize(RunState initial_run_state,
218 double start_time) const {
219 scoped_ptr<Animation> to_return(
220 new Animation(curve_->Clone(), id_, group_, target_property_));
221 to_return->run_state_ = initial_run_state;
222 to_return->iterations_ = iterations_;
223 to_return->start_time_ = start_time;
224 to_return->pause_time_ = pause_time_;
225 to_return->total_paused_time_ = total_paused_time_;
226 to_return->time_offset_ = time_offset_;
227 to_return->alternates_direction_ = alternates_direction_;
228 DCHECK(!to_return->is_controlling_instance_);
229 to_return->is_controlling_instance_ = true;
230 return to_return.Pass();
231 }
232
PushPropertiesTo(Animation * other) const233 void Animation::PushPropertiesTo(Animation* other) const {
234 // Currently, we only push changes due to pausing and resuming animations on
235 // the main thread.
236 if (run_state_ == Animation::Paused ||
237 other->run_state_ == Animation::Paused) {
238 other->run_state_ = run_state_;
239 other->pause_time_ = pause_time_;
240 other->total_paused_time_ = total_paused_time_;
241 }
242 }
243
244 } // namespace cc
245