Upstream version 11.40.277.0
[platform/framework/web/crosswalk.git] / src / cc / animation / animation.cc
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   "ScrollOffset",
36   "BackgroundColor"
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
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
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       iteration_start_(0),
68       direction_(Normal),
69       playback_rate_(1),
70       fill_mode_(FillModeBoth),
71       needs_synchronized_start_time_(false),
72       received_finished_event_(false),
73       suspended_(false),
74       is_controlling_instance_(false),
75       is_impl_only_(false),
76       affects_active_observers_(true),
77       affects_pending_observers_(true) {
78 }
79
80 Animation::~Animation() {
81   if (run_state_ == Running || run_state_ == Paused)
82     SetRunState(Aborted, base::TimeTicks());
83 }
84
85 void Animation::SetRunState(RunState run_state,
86                             base::TimeTicks monotonic_time) {
87   if (suspended_)
88     return;
89
90   char name_buffer[256];
91   base::snprintf(name_buffer,
92                  sizeof(name_buffer),
93                  "%s-%d",
94                  s_targetPropertyNames[target_property_],
95                  group_);
96
97   bool is_waiting_to_start = run_state_ == WaitingForTargetAvailability ||
98                              run_state_ == Starting;
99
100   if (is_controlling_instance_ && is_waiting_to_start && run_state == Running) {
101     TRACE_EVENT_ASYNC_BEGIN1(
102         "cc", "Animation", this, "Name", TRACE_STR_COPY(name_buffer));
103   }
104
105   bool was_finished = is_finished();
106
107   const char* old_run_state_name = s_runStateNames[run_state_];
108
109   if (run_state == Running && run_state_ == Paused)
110     total_paused_time_ += (monotonic_time - pause_time_);
111   else if (run_state == Paused)
112     pause_time_ = monotonic_time;
113   run_state_ = run_state;
114
115   const char* new_run_state_name = s_runStateNames[run_state];
116
117   if (is_controlling_instance_ && !was_finished && is_finished())
118     TRACE_EVENT_ASYNC_END0("cc", "Animation", this);
119
120   char state_buffer[256];
121   base::snprintf(state_buffer,
122                  sizeof(state_buffer),
123                  "%s->%s",
124                  old_run_state_name,
125                  new_run_state_name);
126
127   TRACE_EVENT_INSTANT2("cc",
128                        "LayerAnimationController::SetRunState",
129                        TRACE_EVENT_SCOPE_THREAD,
130                        "Name",
131                        TRACE_STR_COPY(name_buffer),
132                        "State",
133                        TRACE_STR_COPY(state_buffer));
134 }
135
136 void Animation::Suspend(base::TimeTicks monotonic_time) {
137   SetRunState(Paused, monotonic_time);
138   suspended_ = true;
139 }
140
141 void Animation::Resume(base::TimeTicks monotonic_time) {
142   suspended_ = false;
143   SetRunState(Running, monotonic_time);
144 }
145
146 bool Animation::IsFinishedAt(base::TimeTicks monotonic_time) const {
147   if (is_finished())
148     return true;
149
150   if (needs_synchronized_start_time_)
151     return false;
152
153   if (playback_rate_ == 0)
154     return false;
155
156   return run_state_ == Running && iterations_ >= 0 &&
157          iterations_ * curve_->Duration() / std::abs(playback_rate_) <=
158              (monotonic_time + time_offset_ - start_time_ - total_paused_time_)
159                  .InSecondsF();
160 }
161
162 bool Animation::InEffect(base::TimeTicks monotonic_time) const {
163   return ConvertToActiveTime(monotonic_time) >= 0 ||
164          (fill_mode_ == FillModeBoth || fill_mode_ == FillModeBackwards);
165 }
166
167 double Animation::ConvertToActiveTime(base::TimeTicks monotonic_time) const {
168   base::TimeTicks trimmed = monotonic_time + time_offset_;
169
170   // If we're paused, time is 'stuck' at the pause time.
171   if (run_state_ == Paused)
172     trimmed = pause_time_;
173
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_;
177
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_;
183
184   return (trimmed - base::TimeTicks()).InSecondsF();
185 }
186
187 double Animation::TrimTimeToCurrentIteration(
188     base::TimeTicks monotonic_time) const {
189   // Check for valid parameters
190   DCHECK(playback_rate_);
191   DCHECK_GE(iteration_start_, 0);
192
193   double active_time = ConvertToActiveTime(monotonic_time);
194   double start_offset = iteration_start_ * curve_->Duration();
195
196   // Return start offset if we are before the start of the animation
197   if (active_time < 0)
198     return start_offset;
199
200   // Always return zero if we have no iterations.
201   if (!iterations_)
202     return 0;
203
204   // Don't attempt to trim if we have no duration.
205   if (curve_->Duration() <= 0)
206     return 0;
207
208   double repeated_duration = iterations_ * curve_->Duration();
209   double active_duration = repeated_duration / std::abs(playback_rate_);
210
211   // Check if we are past active duration
212   if (iterations_ > 0 && active_time >= active_duration)
213     active_time = active_duration;
214
215   // Calculate the scaled active time
216   double scaled_active_time;
217   if (playback_rate_ < 0)
218     scaled_active_time =
219         (active_time - active_duration) * playback_rate_ + start_offset;
220   else
221     scaled_active_time = active_time * playback_rate_ + start_offset;
222
223   // Calculate the iteration time
224   double iteration_time;
225   if (scaled_active_time - start_offset == repeated_duration &&
226       fmod(iterations_ + iteration_start_, 1) == 0)
227     iteration_time = curve_->Duration();
228   else
229     iteration_time = fmod(scaled_active_time, curve_->Duration());
230
231   // Calculate the current iteration
232   int iteration;
233   if (scaled_active_time <= 0)
234     iteration = 0;
235   else if (iteration_time == curve_->Duration())
236     iteration = ceil(iteration_start_ + iterations_ - 1);
237   else
238     iteration = static_cast<int>(scaled_active_time / curve_->Duration());
239
240   // Check if we are running the animation in reverse direction for the current
241   // iteration
242   bool reverse = (direction_ == Reverse) ||
243                  (direction_ == Alternate && iteration % 2 == 1) ||
244                  (direction_ == AlternateReverse && iteration % 2 == 0);
245
246   // If we are running the animation in reverse direction, reverse the result
247   if (reverse)
248     iteration_time = curve_->Duration() - iteration_time;
249
250   return iteration_time;
251 }
252
253 scoped_ptr<Animation> Animation::CloneAndInitialize(
254     RunState initial_run_state) const {
255   scoped_ptr<Animation> to_return(
256       new Animation(curve_->Clone(), id_, group_, target_property_));
257   to_return->run_state_ = initial_run_state;
258   to_return->iterations_ = iterations_;
259   to_return->iteration_start_ = iteration_start_;
260   to_return->start_time_ = start_time_;
261   to_return->pause_time_ = pause_time_;
262   to_return->total_paused_time_ = total_paused_time_;
263   to_return->time_offset_ = time_offset_;
264   to_return->direction_ = direction_;
265   to_return->playback_rate_ = playback_rate_;
266   to_return->fill_mode_ = fill_mode_;
267   DCHECK(!to_return->is_controlling_instance_);
268   to_return->is_controlling_instance_ = true;
269   return to_return.Pass();
270 }
271
272 void Animation::PushPropertiesTo(Animation* other) const {
273   // Currently, we only push changes due to pausing and resuming animations on
274   // the main thread.
275   if (run_state_ == Animation::Paused ||
276       other->run_state_ == Animation::Paused) {
277     other->run_state_ = run_state_;
278     other->pause_time_ = pause_time_;
279     other->total_paused_time_ = total_paused_time_;
280   }
281 }
282
283 }  // namespace cc