• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
Create(scoped_ptr<AnimationCurve> curve,int animation_id,int group_id,TargetProperty target_property)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 
Animation(scoped_ptr<AnimationCurve> curve,int animation_id,int group_id,TargetProperty 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       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 
~Animation()80 Animation::~Animation() {
81   if (run_state_ == Running || run_state_ == Paused)
82     SetRunState(Aborted, base::TimeTicks());
83 }
84 
SetRunState(RunState run_state,base::TimeTicks monotonic_time)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%s",
94                  s_targetPropertyNames[target_property_],
95                  group_,
96                  is_controlling_instance_ ? "(impl)" : "");
97 
98   bool is_waiting_to_start = run_state_ == WaitingForTargetAvailability ||
99                              run_state_ == Starting;
100 
101   if (is_waiting_to_start && run_state == Running) {
102     TRACE_EVENT_ASYNC_BEGIN1(
103         "cc", "Animation", this, "Name", TRACE_STR_COPY(name_buffer));
104   }
105 
106   bool was_finished = is_finished();
107 
108   const char* old_run_state_name = s_runStateNames[run_state_];
109 
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;
115 
116   const char* new_run_state_name = s_runStateNames[run_state];
117 
118   if (!was_finished && is_finished())
119     TRACE_EVENT_ASYNC_END0("cc", "Animation", this);
120 
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);
127 
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));
135 }
136 
Suspend(base::TimeTicks monotonic_time)137 void Animation::Suspend(base::TimeTicks monotonic_time) {
138   SetRunState(Paused, monotonic_time);
139   suspended_ = true;
140 }
141 
Resume(base::TimeTicks monotonic_time)142 void Animation::Resume(base::TimeTicks monotonic_time) {
143   suspended_ = false;
144   SetRunState(Running, monotonic_time);
145 }
146 
IsFinishedAt(base::TimeTicks monotonic_time) const147 bool Animation::IsFinishedAt(base::TimeTicks monotonic_time) const {
148   if (is_finished())
149     return true;
150 
151   if (needs_synchronized_start_time_)
152     return false;
153 
154   if (playback_rate_ == 0)
155     return false;
156 
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_)
160                  .InSecondsF();
161 }
162 
InEffect(base::TimeTicks monotonic_time) const163 bool Animation::InEffect(base::TimeTicks monotonic_time) const {
164   return ConvertToActiveTime(monotonic_time) >= 0 ||
165          (fill_mode_ == FillModeBoth || fill_mode_ == FillModeBackwards);
166 }
167 
ConvertToActiveTime(base::TimeTicks monotonic_time) const168 double Animation::ConvertToActiveTime(base::TimeTicks monotonic_time) const {
169   base::TimeTicks trimmed = monotonic_time + time_offset_;
170 
171   // If we're paused, time is 'stuck' at the pause time.
172   if (run_state_ == Paused)
173     trimmed = pause_time_;
174 
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_;
178 
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_;
184 
185   return (trimmed - base::TimeTicks()).InSecondsF();
186 }
187 
TrimTimeToCurrentIteration(base::TimeTicks monotonic_time) const188 double Animation::TrimTimeToCurrentIteration(
189     base::TimeTicks monotonic_time) const {
190   // Check for valid parameters
191   DCHECK(playback_rate_);
192   DCHECK_GE(iteration_start_, 0);
193 
194   double active_time = ConvertToActiveTime(monotonic_time);
195   double start_offset = iteration_start_ * curve_->Duration();
196 
197   // Return start offset if we are before the start of the animation
198   if (active_time < 0)
199     return start_offset;
200 
201   // Always return zero if we have no iterations.
202   if (!iterations_)
203     return 0;
204 
205   // Don't attempt to trim if we have no duration.
206   if (curve_->Duration() <= 0)
207     return 0;
208 
209   double repeated_duration = iterations_ * curve_->Duration();
210   double active_duration = repeated_duration / std::abs(playback_rate_);
211 
212   // Check if we are past active duration
213   if (iterations_ > 0 && active_time >= active_duration)
214     active_time = active_duration;
215 
216   // Calculate the scaled active time
217   double scaled_active_time;
218   if (playback_rate_ < 0)
219     scaled_active_time =
220         (active_time - active_duration) * playback_rate_ + start_offset;
221   else
222     scaled_active_time = active_time * playback_rate_ + start_offset;
223 
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();
229   else
230     iteration_time = fmod(scaled_active_time, curve_->Duration());
231 
232   // Calculate the current iteration
233   int iteration;
234   if (scaled_active_time <= 0)
235     iteration = 0;
236   else if (iteration_time == curve_->Duration())
237     iteration = ceil(iteration_start_ + iterations_ - 1);
238   else
239     iteration = static_cast<int>(scaled_active_time / curve_->Duration());
240 
241   // Check if we are running the animation in reverse direction for the current
242   // iteration
243   bool reverse = (direction_ == Reverse) ||
244                  (direction_ == Alternate && iteration % 2 == 1) ||
245                  (direction_ == AlternateReverse && iteration % 2 == 0);
246 
247   // If we are running the animation in reverse direction, reverse the result
248   if (reverse)
249     iteration_time = curve_->Duration() - iteration_time;
250 
251   return iteration_time;
252 }
253 
CloneAndInitialize(RunState initial_run_state) const254 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();
271 }
272 
PushPropertiesTo(Animation * other) const273 void Animation::PushPropertiesTo(Animation* other) const {
274   // Currently, we only push changes due to pausing and resuming animations on
275   // the main thread.
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_;
281   }
282 }
283 
284 }  // namespace cc
285