sandbox/linux/bpf_dsl: eliminate implicit dependency on C++ compiler behavior
[chromium-blink-merge.git] / cc / animation / animation.cc
blob96cb40ae712fbb5855723646424ec0f36e1aad61
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/strings/string_util.h"
10 #include "base/trace_event/trace_event.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[] = {"WAITING_FOR_TARGET_AVAILABILITY",
18 "WAITING_FOR_DELETION",
19 "STARTING",
20 "RUNNING",
21 "PAUSED",
22 "FINISHED",
23 "ABORTED"};
25 static_assert(static_cast<int>(cc::Animation::LAST_RUN_STATE) + 1 ==
26 arraysize(s_runStateNames),
27 "RunStateEnumSize should equal the number of elements in "
28 "s_runStateNames");
30 // This should match the TargetProperty enum.
31 static const char* const s_targetPropertyNames[] = {"TRANSFORM",
32 "OPACITY",
33 "FILTER",
34 "SCROLL_OFFSET",
35 "BACKGROUND_COLOR"};
37 static_assert(static_cast<int>(cc::Animation::LAST_TARGET_PROPERTY) + 1 ==
38 arraysize(s_targetPropertyNames),
39 "TargetPropertyEnumSize should equal the number of elements in "
40 "s_targetPropertyNames");
42 } // namespace
44 namespace cc {
46 scoped_ptr<Animation> Animation::Create(
47 scoped_ptr<AnimationCurve> curve,
48 int animation_id,
49 int group_id,
50 TargetProperty target_property) {
51 return make_scoped_ptr(new Animation(curve.Pass(),
52 animation_id,
53 group_id,
54 target_property)); }
56 Animation::Animation(scoped_ptr<AnimationCurve> curve,
57 int animation_id,
58 int group_id,
59 TargetProperty target_property)
60 : curve_(curve.Pass()),
61 id_(animation_id),
62 group_(group_id),
63 target_property_(target_property),
64 run_state_(WAITING_FOR_TARGET_AVAILABILITY),
65 iterations_(1),
66 iteration_start_(0),
67 direction_(DIRECTION_NORMAL),
68 playback_rate_(1),
69 fill_mode_(FILL_MODE_BOTH),
70 needs_synchronized_start_time_(false),
71 received_finished_event_(false),
72 suspended_(false),
73 is_controlling_instance_(false),
74 is_impl_only_(false),
75 affects_active_observers_(true),
76 affects_pending_observers_(true) {
79 Animation::~Animation() {
80 if (run_state_ == RUNNING || run_state_ == PAUSED)
81 SetRunState(ABORTED, base::TimeTicks());
84 void Animation::SetRunState(RunState run_state,
85 base::TimeTicks monotonic_time) {
86 if (suspended_)
87 return;
89 char name_buffer[256];
90 base::snprintf(name_buffer,
91 sizeof(name_buffer),
92 "%s-%d",
93 s_targetPropertyNames[target_property_],
94 group_);
96 bool is_waiting_to_start =
97 run_state_ == WAITING_FOR_TARGET_AVAILABILITY || run_state_ == STARTING;
99 if (is_controlling_instance_ && is_waiting_to_start && run_state == RUNNING) {
100 TRACE_EVENT_ASYNC_BEGIN1(
101 "cc", "Animation", this, "Name", TRACE_STR_COPY(name_buffer));
104 bool was_finished = is_finished();
106 const char* old_run_state_name = s_runStateNames[run_state_];
108 if (run_state == RUNNING && run_state_ == PAUSED)
109 total_paused_time_ += (monotonic_time - pause_time_);
110 else if (run_state == PAUSED)
111 pause_time_ = monotonic_time;
112 run_state_ = run_state;
114 const char* new_run_state_name = s_runStateNames[run_state];
116 if (is_controlling_instance_ && !was_finished && is_finished())
117 TRACE_EVENT_ASYNC_END0("cc", "Animation", this);
119 char state_buffer[256];
120 base::snprintf(state_buffer,
121 sizeof(state_buffer),
122 "%s->%s",
123 old_run_state_name,
124 new_run_state_name);
126 TRACE_EVENT_INSTANT2("cc",
127 "LayerAnimationController::SetRunState",
128 TRACE_EVENT_SCOPE_THREAD,
129 "Name",
130 TRACE_STR_COPY(name_buffer),
131 "State",
132 TRACE_STR_COPY(state_buffer));
135 void Animation::Suspend(base::TimeTicks monotonic_time) {
136 SetRunState(PAUSED, monotonic_time);
137 suspended_ = true;
140 void Animation::Resume(base::TimeTicks monotonic_time) {
141 suspended_ = false;
142 SetRunState(RUNNING, monotonic_time);
145 bool Animation::IsFinishedAt(base::TimeTicks monotonic_time) const {
146 if (is_finished())
147 return true;
149 if (needs_synchronized_start_time_)
150 return false;
152 if (playback_rate_ == 0)
153 return false;
155 return run_state_ == RUNNING && iterations_ >= 0 &&
156 TimeUtil::Scale(curve_->Duration(),
157 iterations_ / std::abs(playback_rate_)) <=
158 (monotonic_time + time_offset_ - start_time_ - total_paused_time_);
161 bool Animation::InEffect(base::TimeTicks monotonic_time) const {
162 return ConvertToActiveTime(monotonic_time) >= base::TimeDelta() ||
163 (fill_mode_ == FILL_MODE_BOTH || fill_mode_ == FILL_MODE_BACKWARDS);
166 base::TimeDelta Animation::ConvertToActiveTime(
167 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());
187 base::TimeDelta Animation::TrimTimeToCurrentIteration(
188 base::TimeTicks monotonic_time) const {
189 // Check for valid parameters
190 DCHECK(playback_rate_);
191 DCHECK_GE(iteration_start_, 0);
193 base::TimeDelta active_time = ConvertToActiveTime(monotonic_time);
194 base::TimeDelta start_offset =
195 TimeUtil::Scale(curve_->Duration(), iteration_start_);
197 // Return start offset if we are before the start of the animation
198 if (active_time < base::TimeDelta())
199 return start_offset;
200 // Always return zero if we have no iterations.
201 if (!iterations_)
202 return base::TimeDelta();
204 // Don't attempt to trim if we have no duration.
205 if (curve_->Duration() <= base::TimeDelta())
206 return base::TimeDelta();
208 base::TimeDelta repeated_duration =
209 TimeUtil::Scale(curve_->Duration(), iterations_);
210 base::TimeDelta active_duration =
211 TimeUtil::Scale(repeated_duration, 1.0 / std::abs(playback_rate_));
213 // Check if we are past active duration
214 if (iterations_ > 0 && active_time >= active_duration)
215 active_time = active_duration;
217 // Calculate the scaled active time
218 base::TimeDelta scaled_active_time;
219 if (playback_rate_ < 0)
220 scaled_active_time =
221 TimeUtil::Scale((active_time - active_duration), playback_rate_) +
222 start_offset;
223 else
224 scaled_active_time =
225 TimeUtil::Scale(active_time, playback_rate_) + start_offset;
227 // Calculate the iteration time
228 base::TimeDelta iteration_time;
229 if (scaled_active_time - start_offset == repeated_duration &&
230 fmod(iterations_ + iteration_start_, 1) == 0)
231 iteration_time = curve_->Duration();
232 else
233 iteration_time = TimeUtil::Mod(scaled_active_time, curve_->Duration());
235 // Calculate the current iteration
236 int iteration;
237 if (scaled_active_time <= base::TimeDelta())
238 iteration = 0;
239 else if (iteration_time == curve_->Duration())
240 iteration = ceil(iteration_start_ + iterations_ - 1);
241 else
242 iteration = static_cast<int>(scaled_active_time / curve_->Duration());
244 // Check if we are running the animation in reverse direction for the current
245 // iteration
246 bool reverse =
247 (direction_ == DIRECTION_REVERSE) ||
248 (direction_ == DIRECTION_ALTERNATE && iteration % 2 == 1) ||
249 (direction_ == DIRECTION_ALTERNATE_REVERSE && iteration % 2 == 0);
251 // If we are running the animation in reverse direction, reverse the result
252 if (reverse)
253 iteration_time = curve_->Duration() - iteration_time;
255 return iteration_time;
258 scoped_ptr<Animation> Animation::CloneAndInitialize(
259 RunState initial_run_state) const {
260 scoped_ptr<Animation> to_return(
261 new Animation(curve_->Clone(), id_, group_, target_property_));
262 to_return->run_state_ = initial_run_state;
263 to_return->iterations_ = iterations_;
264 to_return->iteration_start_ = iteration_start_;
265 to_return->start_time_ = start_time_;
266 to_return->pause_time_ = pause_time_;
267 to_return->total_paused_time_ = total_paused_time_;
268 to_return->time_offset_ = time_offset_;
269 to_return->direction_ = direction_;
270 to_return->playback_rate_ = playback_rate_;
271 to_return->fill_mode_ = fill_mode_;
272 DCHECK(!to_return->is_controlling_instance_);
273 to_return->is_controlling_instance_ = true;
274 return to_return.Pass();
277 void Animation::PushPropertiesTo(Animation* other) const {
278 // Currently, we only push changes due to pausing and resuming animations on
279 // the main thread.
280 if (run_state_ == Animation::PAUSED ||
281 other->run_state_ == Animation::PAUSED) {
282 other->run_state_ = run_state_;
283 other->pause_time_ = pause_time_;
284 other->total_paused_time_ = total_paused_time_;
288 } // namespace cc