OLD | NEW |
1 // Copyright 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "cc/animation/animation.h" | 5 #include "cc/animation/animation.h" |
6 | 6 |
7 #include <cmath> | 7 #include <cmath> |
8 | 8 |
9 #include "base/strings/string_util.h" | 9 #include "base/strings/string_util.h" |
10 #include "base/trace_event/trace_event.h" | 10 #include "base/trace_event/trace_event.h" |
11 #include "cc/animation/animation_curve.h" | 11 #include "cc/animation/animation_curve.h" |
12 #include "cc/base/time_util.h" | 12 #include "cc/base/time_util.h" |
13 | 13 |
14 namespace { | 14 namespace { |
15 | 15 |
16 // This should match the RunState enum. | 16 // This should match the RunState enum. |
17 static const char* const s_runStateNames[] = { | 17 static const char* const s_runStateNames[] = {"WAITING_FOR_TARGET_AVAILABILITY", |
18 "WaitingForTargetAvailability", | 18 "WAITING_FOR_DELETION", |
19 "WaitingForDeletion", | 19 "STARTING", |
20 "Starting", | 20 "RUNNING", |
21 "Running", | 21 "PAUSED", |
22 "Paused", | 22 "FINISHED", |
23 "Finished", | 23 "ABORTED"}; |
24 "Aborted" | |
25 }; | |
26 | 24 |
27 static_assert(static_cast<int>(cc::Animation::RunStateEnumSize) == | 25 static_assert(static_cast<int>(cc::Animation::LAST_RUN_STATE) + 1 == |
28 arraysize(s_runStateNames), | 26 arraysize(s_runStateNames), |
29 "RunStateEnumSize should equal the number of elements in " | 27 "RunStateEnumSize should equal the number of elements in " |
30 "s_runStateNames"); | 28 "s_runStateNames"); |
31 | 29 |
32 // This should match the TargetProperty enum. | 30 // This should match the TargetProperty enum. |
33 static const char* const s_targetPropertyNames[] = { | 31 static const char* const s_targetPropertyNames[] = {"TRANSFORM", |
34 "Transform", | 32 "OPACITY", |
35 "Opacity", | 33 "FILTER", |
36 "Filter", | 34 "SCROLL_OFFSET", |
37 "ScrollOffset", | 35 "BACKGROUND_COLOR"}; |
38 "BackgroundColor" | |
39 }; | |
40 | 36 |
41 static_assert(static_cast<int>(cc::Animation::TargetPropertyEnumSize) == | 37 static_assert(static_cast<int>(cc::Animation::LAST_TARGET_PROPERTY) + 1 == |
42 arraysize(s_targetPropertyNames), | 38 arraysize(s_targetPropertyNames), |
43 "TargetPropertyEnumSize should equal the number of elements in " | 39 "TargetPropertyEnumSize should equal the number of elements in " |
44 "s_targetPropertyNames"); | 40 "s_targetPropertyNames"); |
45 | 41 |
46 } // namespace | 42 } // namespace |
47 | 43 |
48 namespace cc { | 44 namespace cc { |
49 | 45 |
50 scoped_ptr<Animation> Animation::Create( | 46 scoped_ptr<Animation> Animation::Create( |
51 scoped_ptr<AnimationCurve> curve, | 47 scoped_ptr<AnimationCurve> curve, |
52 int animation_id, | 48 int animation_id, |
53 int group_id, | 49 int group_id, |
54 TargetProperty target_property) { | 50 TargetProperty target_property) { |
55 return make_scoped_ptr(new Animation(curve.Pass(), | 51 return make_scoped_ptr(new Animation(curve.Pass(), |
56 animation_id, | 52 animation_id, |
57 group_id, | 53 group_id, |
58 target_property)); } | 54 target_property)); } |
59 | 55 |
60 Animation::Animation(scoped_ptr<AnimationCurve> curve, | 56 Animation::Animation(scoped_ptr<AnimationCurve> curve, |
61 int animation_id, | 57 int animation_id, |
62 int group_id, | 58 int group_id, |
63 TargetProperty target_property) | 59 TargetProperty target_property) |
64 : curve_(curve.Pass()), | 60 : curve_(curve.Pass()), |
65 id_(animation_id), | 61 id_(animation_id), |
66 group_(group_id), | 62 group_(group_id), |
67 target_property_(target_property), | 63 target_property_(target_property), |
68 run_state_(WaitingForTargetAvailability), | 64 run_state_(WAITING_FOR_TARGET_AVAILABILITY), |
69 iterations_(1), | 65 iterations_(1), |
70 iteration_start_(0), | 66 iteration_start_(0), |
71 direction_(Normal), | 67 direction_(DIRECTION_NORMAL), |
72 playback_rate_(1), | 68 playback_rate_(1), |
73 fill_mode_(FillModeBoth), | 69 fill_mode_(FILL_MODE_BOTH), |
74 needs_synchronized_start_time_(false), | 70 needs_synchronized_start_time_(false), |
75 received_finished_event_(false), | 71 received_finished_event_(false), |
76 suspended_(false), | 72 suspended_(false), |
77 is_controlling_instance_(false), | 73 is_controlling_instance_(false), |
78 is_impl_only_(false), | 74 is_impl_only_(false), |
79 affects_active_observers_(true), | 75 affects_active_observers_(true), |
80 affects_pending_observers_(true) { | 76 affects_pending_observers_(true) { |
81 } | 77 } |
82 | 78 |
83 Animation::~Animation() { | 79 Animation::~Animation() { |
84 if (run_state_ == Running || run_state_ == Paused) | 80 if (run_state_ == RUNNING || run_state_ == PAUSED) |
85 SetRunState(Aborted, base::TimeTicks()); | 81 SetRunState(ABORTED, base::TimeTicks()); |
86 } | 82 } |
87 | 83 |
88 void Animation::SetRunState(RunState run_state, | 84 void Animation::SetRunState(RunState run_state, |
89 base::TimeTicks monotonic_time) { | 85 base::TimeTicks monotonic_time) { |
90 if (suspended_) | 86 if (suspended_) |
91 return; | 87 return; |
92 | 88 |
93 char name_buffer[256]; | 89 char name_buffer[256]; |
94 base::snprintf(name_buffer, | 90 base::snprintf(name_buffer, |
95 sizeof(name_buffer), | 91 sizeof(name_buffer), |
96 "%s-%d", | 92 "%s-%d", |
97 s_targetPropertyNames[target_property_], | 93 s_targetPropertyNames[target_property_], |
98 group_); | 94 group_); |
99 | 95 |
100 bool is_waiting_to_start = run_state_ == WaitingForTargetAvailability || | 96 bool is_waiting_to_start = |
101 run_state_ == Starting; | 97 run_state_ == WAITING_FOR_TARGET_AVAILABILITY || run_state_ == STARTING; |
102 | 98 |
103 if (is_controlling_instance_ && is_waiting_to_start && run_state == Running) { | 99 if (is_controlling_instance_ && is_waiting_to_start && run_state == RUNNING) { |
104 TRACE_EVENT_ASYNC_BEGIN1( | 100 TRACE_EVENT_ASYNC_BEGIN1( |
105 "cc", "Animation", this, "Name", TRACE_STR_COPY(name_buffer)); | 101 "cc", "Animation", this, "Name", TRACE_STR_COPY(name_buffer)); |
106 } | 102 } |
107 | 103 |
108 bool was_finished = is_finished(); | 104 bool was_finished = is_finished(); |
109 | 105 |
110 const char* old_run_state_name = s_runStateNames[run_state_]; | 106 const char* old_run_state_name = s_runStateNames[run_state_]; |
111 | 107 |
112 if (run_state == Running && run_state_ == Paused) | 108 if (run_state == RUNNING && run_state_ == PAUSED) |
113 total_paused_time_ += (monotonic_time - pause_time_); | 109 total_paused_time_ += (monotonic_time - pause_time_); |
114 else if (run_state == Paused) | 110 else if (run_state == PAUSED) |
115 pause_time_ = monotonic_time; | 111 pause_time_ = monotonic_time; |
116 run_state_ = run_state; | 112 run_state_ = run_state; |
117 | 113 |
118 const char* new_run_state_name = s_runStateNames[run_state]; | 114 const char* new_run_state_name = s_runStateNames[run_state]; |
119 | 115 |
120 if (is_controlling_instance_ && !was_finished && is_finished()) | 116 if (is_controlling_instance_ && !was_finished && is_finished()) |
121 TRACE_EVENT_ASYNC_END0("cc", "Animation", this); | 117 TRACE_EVENT_ASYNC_END0("cc", "Animation", this); |
122 | 118 |
123 char state_buffer[256]; | 119 char state_buffer[256]; |
124 base::snprintf(state_buffer, | 120 base::snprintf(state_buffer, |
125 sizeof(state_buffer), | 121 sizeof(state_buffer), |
126 "%s->%s", | 122 "%s->%s", |
127 old_run_state_name, | 123 old_run_state_name, |
128 new_run_state_name); | 124 new_run_state_name); |
129 | 125 |
130 TRACE_EVENT_INSTANT2("cc", | 126 TRACE_EVENT_INSTANT2("cc", |
131 "LayerAnimationController::SetRunState", | 127 "LayerAnimationController::SetRunState", |
132 TRACE_EVENT_SCOPE_THREAD, | 128 TRACE_EVENT_SCOPE_THREAD, |
133 "Name", | 129 "Name", |
134 TRACE_STR_COPY(name_buffer), | 130 TRACE_STR_COPY(name_buffer), |
135 "State", | 131 "State", |
136 TRACE_STR_COPY(state_buffer)); | 132 TRACE_STR_COPY(state_buffer)); |
137 } | 133 } |
138 | 134 |
139 void Animation::Suspend(base::TimeTicks monotonic_time) { | 135 void Animation::Suspend(base::TimeTicks monotonic_time) { |
140 SetRunState(Paused, monotonic_time); | 136 SetRunState(PAUSED, monotonic_time); |
141 suspended_ = true; | 137 suspended_ = true; |
142 } | 138 } |
143 | 139 |
144 void Animation::Resume(base::TimeTicks monotonic_time) { | 140 void Animation::Resume(base::TimeTicks monotonic_time) { |
145 suspended_ = false; | 141 suspended_ = false; |
146 SetRunState(Running, monotonic_time); | 142 SetRunState(RUNNING, monotonic_time); |
147 } | 143 } |
148 | 144 |
149 bool Animation::IsFinishedAt(base::TimeTicks monotonic_time) const { | 145 bool Animation::IsFinishedAt(base::TimeTicks monotonic_time) const { |
150 if (is_finished()) | 146 if (is_finished()) |
151 return true; | 147 return true; |
152 | 148 |
153 if (needs_synchronized_start_time_) | 149 if (needs_synchronized_start_time_) |
154 return false; | 150 return false; |
155 | 151 |
156 if (playback_rate_ == 0) | 152 if (playback_rate_ == 0) |
157 return false; | 153 return false; |
158 | 154 |
159 return run_state_ == Running && iterations_ >= 0 && | 155 return run_state_ == RUNNING && iterations_ >= 0 && |
160 TimeUtil::Scale(curve_->Duration(), | 156 TimeUtil::Scale(curve_->Duration(), |
161 iterations_ / std::abs(playback_rate_)) <= | 157 iterations_ / std::abs(playback_rate_)) <= |
162 (monotonic_time + time_offset_ - start_time_ - total_paused_time_); | 158 (monotonic_time + time_offset_ - start_time_ - total_paused_time_); |
163 } | 159 } |
164 | 160 |
165 bool Animation::InEffect(base::TimeTicks monotonic_time) const { | 161 bool Animation::InEffect(base::TimeTicks monotonic_time) const { |
166 return ConvertToActiveTime(monotonic_time) >= base::TimeDelta() || | 162 return ConvertToActiveTime(monotonic_time) >= base::TimeDelta() || |
167 (fill_mode_ == FillModeBoth || fill_mode_ == FillModeBackwards); | 163 (fill_mode_ == FILL_MODE_BOTH || fill_mode_ == FILL_MODE_BACKWARDS); |
168 } | 164 } |
169 | 165 |
170 base::TimeDelta Animation::ConvertToActiveTime( | 166 base::TimeDelta Animation::ConvertToActiveTime( |
171 base::TimeTicks monotonic_time) const { | 167 base::TimeTicks monotonic_time) const { |
172 base::TimeTicks trimmed = monotonic_time + time_offset_; | 168 base::TimeTicks trimmed = monotonic_time + time_offset_; |
173 | 169 |
174 // If we're paused, time is 'stuck' at the pause time. | 170 // If we're paused, time is 'stuck' at the pause time. |
175 if (run_state_ == Paused) | 171 if (run_state_ == PAUSED) |
176 trimmed = pause_time_; | 172 trimmed = pause_time_; |
177 | 173 |
178 // Returned time should always be relative to the start time and should | 174 // Returned time should always be relative to the start time and should |
179 // subtract all time spent paused. | 175 // subtract all time spent paused. |
180 trimmed -= (start_time_ - base::TimeTicks()) + total_paused_time_; | 176 trimmed -= (start_time_ - base::TimeTicks()) + total_paused_time_; |
181 | 177 |
182 // If we're just starting or we're waiting on receiving a start time, | 178 // If we're just starting or we're waiting on receiving a start time, |
183 // time is 'stuck' at the initial state. | 179 // time is 'stuck' at the initial state. |
184 if ((run_state_ == Starting && !has_set_start_time()) || | 180 if ((run_state_ == STARTING && !has_set_start_time()) || |
185 needs_synchronized_start_time()) | 181 needs_synchronized_start_time()) |
186 trimmed = base::TimeTicks() + time_offset_; | 182 trimmed = base::TimeTicks() + time_offset_; |
187 | 183 |
188 return (trimmed - base::TimeTicks()); | 184 return (trimmed - base::TimeTicks()); |
189 } | 185 } |
190 | 186 |
191 base::TimeDelta Animation::TrimTimeToCurrentIteration( | 187 base::TimeDelta Animation::TrimTimeToCurrentIteration( |
192 base::TimeTicks monotonic_time) const { | 188 base::TimeTicks monotonic_time) const { |
193 // Check for valid parameters | 189 // Check for valid parameters |
194 DCHECK(playback_rate_); | 190 DCHECK(playback_rate_); |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
240 int iteration; | 236 int iteration; |
241 if (scaled_active_time <= base::TimeDelta()) | 237 if (scaled_active_time <= base::TimeDelta()) |
242 iteration = 0; | 238 iteration = 0; |
243 else if (iteration_time == curve_->Duration()) | 239 else if (iteration_time == curve_->Duration()) |
244 iteration = ceil(iteration_start_ + iterations_ - 1); | 240 iteration = ceil(iteration_start_ + iterations_ - 1); |
245 else | 241 else |
246 iteration = static_cast<int>(scaled_active_time / curve_->Duration()); | 242 iteration = static_cast<int>(scaled_active_time / curve_->Duration()); |
247 | 243 |
248 // Check if we are running the animation in reverse direction for the current | 244 // Check if we are running the animation in reverse direction for the current |
249 // iteration | 245 // iteration |
250 bool reverse = (direction_ == Reverse) || | 246 bool reverse = |
251 (direction_ == Alternate && iteration % 2 == 1) || | 247 (direction_ == DIRECTION_REVERSE) || |
252 (direction_ == AlternateReverse && iteration % 2 == 0); | 248 (direction_ == DIRECTION_ALTERNATE && iteration % 2 == 1) || |
| 249 (direction_ == DIRECTION_ALTERNATE_REVERSE && iteration % 2 == 0); |
253 | 250 |
254 // If we are running the animation in reverse direction, reverse the result | 251 // If we are running the animation in reverse direction, reverse the result |
255 if (reverse) | 252 if (reverse) |
256 iteration_time = curve_->Duration() - iteration_time; | 253 iteration_time = curve_->Duration() - iteration_time; |
257 | 254 |
258 return iteration_time; | 255 return iteration_time; |
259 } | 256 } |
260 | 257 |
261 scoped_ptr<Animation> Animation::CloneAndInitialize( | 258 scoped_ptr<Animation> Animation::CloneAndInitialize( |
262 RunState initial_run_state) const { | 259 RunState initial_run_state) const { |
(...skipping 10 matching lines...) Expand all Loading... |
273 to_return->playback_rate_ = playback_rate_; | 270 to_return->playback_rate_ = playback_rate_; |
274 to_return->fill_mode_ = fill_mode_; | 271 to_return->fill_mode_ = fill_mode_; |
275 DCHECK(!to_return->is_controlling_instance_); | 272 DCHECK(!to_return->is_controlling_instance_); |
276 to_return->is_controlling_instance_ = true; | 273 to_return->is_controlling_instance_ = true; |
277 return to_return.Pass(); | 274 return to_return.Pass(); |
278 } | 275 } |
279 | 276 |
280 void Animation::PushPropertiesTo(Animation* other) const { | 277 void Animation::PushPropertiesTo(Animation* other) const { |
281 // Currently, we only push changes due to pausing and resuming animations on | 278 // Currently, we only push changes due to pausing and resuming animations on |
282 // the main thread. | 279 // the main thread. |
283 if (run_state_ == Animation::Paused || | 280 if (run_state_ == Animation::PAUSED || |
284 other->run_state_ == Animation::Paused) { | 281 other->run_state_ == Animation::PAUSED) { |
285 other->run_state_ = run_state_; | 282 other->run_state_ = run_state_; |
286 other->pause_time_ = pause_time_; | 283 other->pause_time_ = pause_time_; |
287 other->total_paused_time_ = total_paused_time_; | 284 other->total_paused_time_ = total_paused_time_; |
288 } | 285 } |
289 } | 286 } |
290 | 287 |
291 } // namespace cc | 288 } // namespace cc |
OLD | NEW |