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 needs_synchronized_start_time_(false),
71 received_finished_event_(false),
74 total_paused_time_(0),
75 is_controlling_instance_(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
, 0);
86 void Animation::SetRunState(RunState run_state
, double 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(double monotonic_time
) {
138 SetRunState(Paused
, monotonic_time
);
142 void Animation::Resume(double monotonic_time
) {
144 SetRunState(Running
, monotonic_time
);
147 bool Animation::IsFinishedAt(double monotonic_time
) const {
151 if (needs_synchronized_start_time_
)
154 return run_state_
== Running
&&
156 iterations_
* curve_
->Duration() <= (monotonic_time
-
162 double Animation::TrimTimeToCurrentIteration(double monotonic_time
) const {
163 double trimmed
= monotonic_time
+ time_offset_
;
165 // If we're paused, time is 'stuck' at the pause time.
166 if (run_state_
== Paused
)
167 trimmed
= pause_time_
;
169 // Returned time should always be relative to the start time and should
170 // subtract all time spent paused.
171 trimmed
-= start_time_
+ total_paused_time_
;
173 // If we're just starting or we're waiting on receiving a start time,
174 // time is 'stuck' at the initial state.
175 if ((run_state_
== Starting
&& !has_set_start_time()) ||
176 needs_synchronized_start_time())
177 trimmed
= time_offset_
;
179 // Return 0 if we are before the start of the animation
183 // Always return zero if we have no iterations.
187 // Don't attempt to trim if we have no duration.
188 if (curve_
->Duration() <= 0)
191 // check if we are past active interval
192 bool is_past_total_duration
=
193 (iterations_
> 0 && trimmed
>= curve_
->Duration() * iterations_
);
195 // We need to know the current iteration if we're alternating.
198 // If we are past the active interval, return iteration duration.
199 if (is_past_total_duration
) {
200 iteration
= iterations_
- 1;
201 trimmed
= curve_
->Duration();
203 iteration
= static_cast<int>(trimmed
/ curve_
->Duration());
204 // Calculate x where trimmed = x + n * curve_->Duration() for some positive
206 trimmed
= fmod(trimmed
, curve_
->Duration());
209 // check if we are running the animation in reverse direction for the current
211 bool reverse
= (direction_
== Reverse
) ||
212 (direction_
== Alternate
&& iteration
% 2 == 1) ||
213 (direction_
== AlternateReverse
&& iteration
% 2 == 0);
215 // if we are running the animation in reverse direction, reverse the result
217 return curve_
->Duration() - trimmed
;
222 scoped_ptr
<Animation
> Animation::CloneAndInitialize(
223 RunState initial_run_state
) const {
224 scoped_ptr
<Animation
> to_return(
225 new Animation(curve_
->Clone(), id_
, group_
, target_property_
));
226 to_return
->run_state_
= initial_run_state
;
227 to_return
->iterations_
= iterations_
;
228 to_return
->start_time_
= start_time_
;
229 to_return
->pause_time_
= pause_time_
;
230 to_return
->total_paused_time_
= total_paused_time_
;
231 to_return
->time_offset_
= time_offset_
;
232 to_return
->direction_
= direction_
;
233 DCHECK(!to_return
->is_controlling_instance_
);
234 to_return
->is_controlling_instance_
= true;
235 return to_return
.Pass();
238 void Animation::PushPropertiesTo(Animation
* other
) const {
239 // Currently, we only push changes due to pausing and resuming animations on
241 if (run_state_
== Animation::Paused
||
242 other
->run_state_
== Animation::Paused
) {
243 other
->run_state_
= run_state_
;
244 other
->pause_time_
= pause_time_
;
245 other
->total_paused_time_
= total_paused_time_
;