Cast: Stop logging kVideoFrameSentToEncoder and rename a couple events.
[chromium-blink-merge.git] / cc / animation / animation.cc
blob2e924aca0e8df744929d6bfa5edf42e08e5f16ae
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"
13 namespace {
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"
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[] = {
32 "Transform",
33 "Opacity",
34 "Filter",
35 "ScrollOffset",
36 "BackgroundColor"
39 COMPILE_ASSERT(static_cast<int>(cc::Animation::TargetPropertyEnumSize) ==
40 arraysize(s_targetPropertyNames),
41 TargetProperty_names_match_enum);
43 } // namespace
45 namespace cc {
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)); }
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 direction_(Normal),
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 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) {
87 if (suspended_)
88 return;
90 char name_buffer[256];
91 base::snprintf(name_buffer,
92 sizeof(name_buffer),
93 "%s-%d%s",
94 s_targetPropertyNames[target_property_],
95 group_,
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),
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(double monotonic_time) {
138 SetRunState(Paused, monotonic_time);
139 suspended_ = true;
142 void Animation::Resume(double monotonic_time) {
143 suspended_ = false;
144 SetRunState(Running, monotonic_time);
147 bool Animation::IsFinishedAt(double monotonic_time) const {
148 if (is_finished())
149 return true;
151 if (needs_synchronized_start_time_)
152 return false;
154 return run_state_ == Running &&
155 iterations_ >= 0 &&
156 iterations_ * curve_->Duration() <= (monotonic_time -
157 start_time() -
158 total_paused_time_ +
159 time_offset_);
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
180 if (trimmed < 0)
181 return 0;
183 // Always return zero if we have no iterations.
184 if (!iterations_)
185 return 0;
187 // Don't attempt to trim if we have no duration.
188 if (curve_->Duration() <= 0)
189 return 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.
196 int iteration = 0;
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();
202 } else {
203 iteration = static_cast<int>(trimmed / curve_->Duration());
204 // Calculate x where trimmed = x + n * curve_->Duration() for some positive
205 // integer n.
206 trimmed = fmod(trimmed, curve_->Duration());
209 // check if we are running the animation in reverse direction for the current
210 // iteration
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
216 if (reverse)
217 return curve_->Duration() - trimmed;
219 return 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
240 // the main thread.
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_;
249 } // namespace cc