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/debug/trace_event.h" | 9 #include "base/debug/trace_event.h" |
10 #include "base/strings/string_util.h" | 10 #include "base/strings/string_util.h" |
11 #include "cc/animation/animation_curve.h" | 11 #include "cc/animation/animation_curve.h" |
| 12 #include "cc/base/time_util.h" |
12 | 13 |
13 namespace { | 14 namespace { |
14 | 15 |
15 // This should match the RunState enum. | 16 // This should match the RunState enum. |
16 static const char* const s_runStateNames[] = { | 17 static const char* const s_runStateNames[] = { |
17 "WaitingForTargetAvailability", | 18 "WaitingForTargetAvailability", |
18 "WaitingForDeletion", | 19 "WaitingForDeletion", |
19 "Starting", | 20 "Starting", |
20 "Running", | 21 "Running", |
21 "Paused", | 22 "Paused", |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
57 Animation::Animation(scoped_ptr<AnimationCurve> curve, | 58 Animation::Animation(scoped_ptr<AnimationCurve> curve, |
58 int animation_id, | 59 int animation_id, |
59 int group_id, | 60 int group_id, |
60 TargetProperty target_property) | 61 TargetProperty target_property) |
61 : curve_(curve.Pass()), | 62 : curve_(curve.Pass()), |
62 id_(animation_id), | 63 id_(animation_id), |
63 group_(group_id), | 64 group_(group_id), |
64 target_property_(target_property), | 65 target_property_(target_property), |
65 run_state_(WaitingForTargetAvailability), | 66 run_state_(WaitingForTargetAvailability), |
66 iterations_(1), | 67 iterations_(1), |
67 start_time_(0), | 68 start_time_(), |
68 direction_(Normal), | 69 direction_(Normal), |
69 time_offset_(0), | 70 time_offset_(), |
70 needs_synchronized_start_time_(false), | 71 needs_synchronized_start_time_(false), |
71 received_finished_event_(false), | 72 received_finished_event_(false), |
72 suspended_(false), | 73 suspended_(false), |
73 pause_time_(0), | 74 pause_time_(), |
74 total_paused_time_(0), | 75 total_paused_time_(), |
75 is_controlling_instance_(false), | 76 is_controlling_instance_(false), |
76 is_impl_only_(false), | 77 is_impl_only_(false), |
77 affects_active_observers_(true), | 78 affects_active_observers_(true), |
78 affects_pending_observers_(true) { | 79 affects_pending_observers_(true) { |
79 } | 80 } |
80 | 81 |
81 Animation::~Animation() { | 82 Animation::~Animation() { |
82 if (run_state_ == Running || run_state_ == Paused) | 83 if (run_state_ == Running || run_state_ == Paused) |
83 SetRunState(Aborted, 0); | 84 SetRunState(Aborted, base::TimeTicks()); |
84 } | 85 } |
85 | 86 |
86 void Animation::SetRunState(RunState run_state, double monotonic_time) { | 87 void Animation::SetRunState(RunState run_state, |
| 88 base::TimeTicks monotonic_time) { |
87 if (suspended_) | 89 if (suspended_) |
88 return; | 90 return; |
89 | 91 |
90 char name_buffer[256]; | 92 char name_buffer[256]; |
91 base::snprintf(name_buffer, | 93 base::snprintf(name_buffer, |
92 sizeof(name_buffer), | 94 sizeof(name_buffer), |
93 "%s-%d%s", | 95 "%s-%d%s", |
94 s_targetPropertyNames[target_property_], | 96 s_targetPropertyNames[target_property_], |
95 group_, | 97 group_, |
96 is_controlling_instance_ ? "(impl)" : ""); | 98 is_controlling_instance_ ? "(impl)" : ""); |
97 | 99 |
98 bool is_waiting_to_start = run_state_ == WaitingForTargetAvailability || | 100 bool is_waiting_to_start = run_state_ == WaitingForTargetAvailability || |
99 run_state_ == Starting; | 101 run_state_ == Starting; |
100 | 102 |
101 if (is_waiting_to_start && run_state == Running) { | 103 if (is_waiting_to_start && run_state == Running) { |
102 TRACE_EVENT_ASYNC_BEGIN1( | 104 TRACE_EVENT_ASYNC_BEGIN1( |
103 "cc", "Animation", this, "Name", TRACE_STR_COPY(name_buffer)); | 105 "cc", "Animation", this, "Name", TRACE_STR_COPY(name_buffer)); |
104 } | 106 } |
105 | 107 |
106 bool was_finished = is_finished(); | 108 bool was_finished = is_finished(); |
107 | 109 |
108 const char* old_run_state_name = s_runStateNames[run_state_]; | 110 const char* old_run_state_name = s_runStateNames[run_state_]; |
109 | 111 |
110 if (run_state == Running && run_state_ == Paused) | 112 if (run_state == Running && run_state_ == Paused) |
111 total_paused_time_ += monotonic_time - pause_time_; | 113 total_paused_time_ += (monotonic_time - pause_time_); |
112 else if (run_state == Paused) | 114 else if (run_state == Paused) |
113 pause_time_ = monotonic_time; | 115 pause_time_ = monotonic_time; |
114 run_state_ = run_state; | 116 run_state_ = run_state; |
115 | 117 |
116 const char* new_run_state_name = s_runStateNames[run_state]; | 118 const char* new_run_state_name = s_runStateNames[run_state]; |
117 | 119 |
118 if (!was_finished && is_finished()) | 120 if (!was_finished && is_finished()) |
119 TRACE_EVENT_ASYNC_END0("cc", "Animation", this); | 121 TRACE_EVENT_ASYNC_END0("cc", "Animation", this); |
120 | 122 |
121 char state_buffer[256]; | 123 char state_buffer[256]; |
122 base::snprintf(state_buffer, | 124 base::snprintf(state_buffer, |
123 sizeof(state_buffer), | 125 sizeof(state_buffer), |
124 "%s->%s", | 126 "%s->%s", |
125 old_run_state_name, | 127 old_run_state_name, |
126 new_run_state_name); | 128 new_run_state_name); |
127 | 129 |
128 TRACE_EVENT_INSTANT2("cc", | 130 TRACE_EVENT_INSTANT2("cc", |
129 "LayerAnimationController::SetRunState", | 131 "LayerAnimationController::SetRunState", |
130 TRACE_EVENT_SCOPE_THREAD, | 132 TRACE_EVENT_SCOPE_THREAD, |
131 "Name", | 133 "Name", |
132 TRACE_STR_COPY(name_buffer), | 134 TRACE_STR_COPY(name_buffer), |
133 "State", | 135 "State", |
134 TRACE_STR_COPY(state_buffer)); | 136 TRACE_STR_COPY(state_buffer)); |
135 } | 137 } |
136 | 138 |
137 void Animation::Suspend(double monotonic_time) { | 139 void Animation::Suspend(base::TimeTicks monotonic_time) { |
138 SetRunState(Paused, monotonic_time); | 140 SetRunState(Paused, monotonic_time); |
139 suspended_ = true; | 141 suspended_ = true; |
140 } | 142 } |
141 | 143 |
142 void Animation::Resume(double monotonic_time) { | 144 void Animation::Resume(base::TimeTicks monotonic_time) { |
143 suspended_ = false; | 145 suspended_ = false; |
144 SetRunState(Running, monotonic_time); | 146 SetRunState(Running, monotonic_time); |
145 } | 147 } |
146 | 148 |
147 bool Animation::IsFinishedAt(double monotonic_time) const { | 149 bool Animation::IsFinishedAt(base::TimeTicks monotonic_time) const { |
148 if (is_finished()) | 150 if (is_finished()) |
149 return true; | 151 return true; |
150 | 152 |
151 if (needs_synchronized_start_time_) | 153 if (needs_synchronized_start_time_) |
152 return false; | 154 return false; |
153 | 155 |
154 return run_state_ == Running && | 156 return run_state_ == Running && iterations_ >= 0 && |
155 iterations_ >= 0 && | 157 iterations_ * curve_->Duration() <= |
156 iterations_ * curve_->Duration() <= (monotonic_time - | 158 (TimeUtil::TicksInSecondsF(monotonic_time) + |
157 start_time() - | 159 time_offset_.InSecondsF() - |
158 total_paused_time_ + | 160 TimeUtil::TicksInSecondsF(start_time_) - |
159 time_offset_); | 161 total_paused_time_.InSecondsF()); |
160 } | 162 } |
161 | 163 |
162 double Animation::TrimTimeToCurrentIteration(double monotonic_time) const { | 164 double Animation::TrimTimeToCurrentIteration( |
163 double trimmed = monotonic_time + time_offset_; | 165 base::TimeTicks monotonic_time) const { |
| 166 double trimmed = |
| 167 TimeUtil::TicksInSecondsF(monotonic_time) + time_offset_.InSecondsF(); |
164 | 168 |
165 // If we're paused, time is 'stuck' at the pause time. | 169 // If we're paused, time is 'stuck' at the pause time. |
166 if (run_state_ == Paused) | 170 if (run_state_ == Paused) |
167 trimmed = pause_time_; | 171 trimmed = TimeUtil::TicksInSecondsF(pause_time_); |
168 | 172 |
169 // Returned time should always be relative to the start time and should | 173 // Returned time should always be relative to the start time and should |
170 // subtract all time spent paused. | 174 // subtract all time spent paused. |
171 trimmed -= start_time_ + total_paused_time_; | 175 trimmed -= |
| 176 TimeUtil::TicksInSecondsF(start_time_) + total_paused_time_.InSecondsF(); |
172 | 177 |
173 // 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, |
174 // time is 'stuck' at the initial state. | 179 // time is 'stuck' at the initial state. |
175 if ((run_state_ == Starting && !has_set_start_time()) || | 180 if ((run_state_ == Starting && !has_set_start_time()) || |
176 needs_synchronized_start_time()) | 181 needs_synchronized_start_time()) |
177 trimmed = time_offset_; | 182 trimmed = time_offset_.InSecondsF(); |
178 | 183 |
179 // Return 0 if we are before the start of the animation | 184 // Return 0 if we are before the start of the animation |
180 if (trimmed < 0) | 185 if (trimmed < 0) |
181 return 0; | 186 return 0; |
182 | 187 |
183 // Always return zero if we have no iterations. | 188 // Always return zero if we have no iterations. |
184 if (!iterations_) | 189 if (!iterations_) |
185 return 0; | 190 return 0; |
186 | 191 |
187 // Don't attempt to trim if we have no duration. | 192 // Don't attempt to trim if we have no duration. |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
240 // the main thread. | 245 // the main thread. |
241 if (run_state_ == Animation::Paused || | 246 if (run_state_ == Animation::Paused || |
242 other->run_state_ == Animation::Paused) { | 247 other->run_state_ == Animation::Paused) { |
243 other->run_state_ = run_state_; | 248 other->run_state_ = run_state_; |
244 other->pause_time_ = pause_time_; | 249 other->pause_time_ = pause_time_; |
245 other->total_paused_time_ = total_paused_time_; | 250 other->total_paused_time_ = total_paused_time_; |
246 } | 251 } |
247 } | 252 } |
248 | 253 |
249 } // namespace cc | 254 } // namespace cc |
OLD | NEW |