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_
],
96 is_controlling_instance_
? "(impl)" : "");
98 bool is_waiting_to_start
= run_state_
== WaitingForTargetAvailability
||
99 run_state_
== Starting
;
101 if (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 (!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
),
128 TRACE_EVENT_INSTANT2("cc",
129 "LayerAnimationController::SetRunState",
130 TRACE_EVENT_SCOPE_THREAD
,
132 TRACE_STR_COPY(name_buffer
),
134 TRACE_STR_COPY(state_buffer
));
137 void Animation::Suspend(base::TimeTicks monotonic_time
) {
138 SetRunState(Paused
, monotonic_time
);
142 void Animation::Resume(base::TimeTicks monotonic_time
) {
144 SetRunState(Running
, monotonic_time
);
147 bool Animation::IsFinishedAt(base::TimeTicks monotonic_time
) const {
151 if (needs_synchronized_start_time_
)
154 if (playback_rate_
== 0)
157 return run_state_
== Running
&& iterations_
>= 0 &&
158 iterations_
* curve_
->Duration() / std::abs(playback_rate_
) <=
159 (monotonic_time
+ time_offset_
- start_time_
- total_paused_time_
)
163 bool Animation::InEffect(base::TimeTicks monotonic_time
) const {
164 return ConvertToActiveTime(monotonic_time
) >= 0 ||
165 (fill_mode_
== FillModeBoth
|| fill_mode_
== FillModeBackwards
);
168 double Animation::ConvertToActiveTime(base::TimeTicks monotonic_time
) const {
169 base::TimeTicks trimmed
= monotonic_time
+ time_offset_
;
171 // If we're paused, time is 'stuck' at the pause time.
172 if (run_state_
== Paused
)
173 trimmed
= pause_time_
;
175 // Returned time should always be relative to the start time and should
176 // subtract all time spent paused.
177 trimmed
-= (start_time_
- base::TimeTicks()) + total_paused_time_
;
179 // If we're just starting or we're waiting on receiving a start time,
180 // time is 'stuck' at the initial state.
181 if ((run_state_
== Starting
&& !has_set_start_time()) ||
182 needs_synchronized_start_time())
183 trimmed
= base::TimeTicks() + time_offset_
;
185 return (trimmed
- base::TimeTicks()).InSecondsF();
188 double Animation::TrimTimeToCurrentIteration(
189 base::TimeTicks monotonic_time
) const {
190 // Check for valid parameters
191 DCHECK(playback_rate_
);
192 DCHECK_GE(iteration_start_
, 0);
194 double active_time
= ConvertToActiveTime(monotonic_time
);
196 // Return 0 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_
);
210 double start_offset
= iteration_start_
* curve_
->Duration();
212 // Check if we are past active duration
213 if (iterations_
> 0 && active_time
>= active_duration
)
214 active_time
= active_duration
;
216 // Calculate the scaled active time
217 double scaled_active_time
;
218 if (playback_rate_
< 0)
220 (active_time
- active_duration
) * playback_rate_
+ start_offset
;
222 scaled_active_time
= active_time
* playback_rate_
+ start_offset
;
224 // Calculate the iteration time
225 double iteration_time
;
226 if (scaled_active_time
- start_offset
== repeated_duration
&&
227 fmod(iterations_
+ iteration_start_
, 1) == 0)
228 iteration_time
= curve_
->Duration();
230 iteration_time
= fmod(scaled_active_time
, curve_
->Duration());
232 // Calculate the current iteration
234 if (scaled_active_time
<= 0)
236 else if (iteration_time
== curve_
->Duration())
237 iteration
= ceil(iteration_start_
+ iterations_
- 1);
239 iteration
= static_cast<int>(scaled_active_time
/ curve_
->Duration());
241 // Check if we are running the animation in reverse direction for the current
243 bool reverse
= (direction_
== Reverse
) ||
244 (direction_
== Alternate
&& iteration
% 2 == 1) ||
245 (direction_
== AlternateReverse
&& iteration
% 2 == 0);
247 // If we are running the animation in reverse direction, reverse the result
249 iteration_time
= curve_
->Duration() - iteration_time
;
251 return iteration_time
;
254 scoped_ptr
<Animation
> Animation::CloneAndInitialize(
255 RunState initial_run_state
) const {
256 scoped_ptr
<Animation
> to_return(
257 new Animation(curve_
->Clone(), id_
, group_
, target_property_
));
258 to_return
->run_state_
= initial_run_state
;
259 to_return
->iterations_
= iterations_
;
260 to_return
->iteration_start_
= iteration_start_
;
261 to_return
->start_time_
= start_time_
;
262 to_return
->pause_time_
= pause_time_
;
263 to_return
->total_paused_time_
= total_paused_time_
;
264 to_return
->time_offset_
= time_offset_
;
265 to_return
->direction_
= direction_
;
266 to_return
->playback_rate_
= playback_rate_
;
267 to_return
->fill_mode_
= fill_mode_
;
268 DCHECK(!to_return
->is_controlling_instance_
);
269 to_return
->is_controlling_instance_
= true;
270 return to_return
.Pass();
273 void Animation::PushPropertiesTo(Animation
* other
) const {
274 // Currently, we only push changes due to pausing and resuming animations on
276 if (run_state_
== Animation::Paused
||
277 other
->run_state_
== Animation::Paused
) {
278 other
->run_state_
= run_state_
;
279 other
->pause_time_
= pause_time_
;
280 other
->total_paused_time_
= total_paused_time_
;