Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(86)

Side by Side Diff: cc/animation/animation.cc

Issue 231133002: CC::Animations should use TimeTicks & TimeDelta to represent time (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Code changed as per review comments. Created 6 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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"
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after
57 Animation::Animation(scoped_ptr<AnimationCurve> curve, 57 Animation::Animation(scoped_ptr<AnimationCurve> curve,
58 int animation_id, 58 int animation_id,
59 int group_id, 59 int group_id,
60 TargetProperty target_property) 60 TargetProperty target_property)
61 : curve_(curve.Pass()), 61 : curve_(curve.Pass()),
62 id_(animation_id), 62 id_(animation_id),
63 group_(group_id), 63 group_(group_id),
64 target_property_(target_property), 64 target_property_(target_property),
65 run_state_(WaitingForTargetAvailability), 65 run_state_(WaitingForTargetAvailability),
66 iterations_(1), 66 iterations_(1),
67 start_time_(0), 67 start_time_(),
ajuma 2014/05/07 17:04:42 This line isn't needed (the default constructor wi
Sikugu_ 2014/05/12 16:01:53 Done.
68 direction_(Normal), 68 direction_(Normal),
69 time_offset_(0), 69 time_offset_(),
ajuma 2014/05/07 17:04:42 Not needed.
Sikugu_ 2014/05/12 16:01:53 Done.
70 needs_synchronized_start_time_(false), 70 needs_synchronized_start_time_(false),
71 received_finished_event_(false), 71 received_finished_event_(false),
72 suspended_(false), 72 suspended_(false),
73 pause_time_(0), 73 pause_time_(),
ajuma 2014/05/07 17:04:42 Not needed.
Sikugu_ 2014/05/12 16:01:53 Done.
74 total_paused_time_(0), 74 total_paused_time_(),
ajuma 2014/05/07 17:04:42 Not needed.
Sikugu_ 2014/05/12 16:01:53 Done.
75 is_controlling_instance_(false), 75 is_controlling_instance_(false),
76 is_impl_only_(false), 76 is_impl_only_(false),
77 affects_active_observers_(true), 77 affects_active_observers_(true),
78 affects_pending_observers_(true) { 78 affects_pending_observers_(true) {
79 } 79 }
80 80
81 Animation::~Animation() { 81 Animation::~Animation() {
82 if (run_state_ == Running || run_state_ == Paused) 82 if (run_state_ == Running || run_state_ == Paused)
83 SetRunState(Aborted, 0); 83 SetRunState(Aborted, base::TimeTicks());
84 } 84 }
85 85
86 void Animation::SetRunState(RunState run_state, double monotonic_time) { 86 void Animation::SetRunState(RunState run_state,
87 base::TimeTicks monotonic_time) {
87 if (suspended_) 88 if (suspended_)
88 return; 89 return;
89 90
90 char name_buffer[256]; 91 char name_buffer[256];
91 base::snprintf(name_buffer, 92 base::snprintf(name_buffer,
92 sizeof(name_buffer), 93 sizeof(name_buffer),
93 "%s-%d%s", 94 "%s-%d%s",
94 s_targetPropertyNames[target_property_], 95 s_targetPropertyNames[target_property_],
95 group_, 96 group_,
96 is_controlling_instance_ ? "(impl)" : ""); 97 is_controlling_instance_ ? "(impl)" : "");
97 98
98 bool is_waiting_to_start = run_state_ == WaitingForTargetAvailability || 99 bool is_waiting_to_start = run_state_ == WaitingForTargetAvailability ||
99 run_state_ == Starting; 100 run_state_ == Starting;
100 101
101 if (is_waiting_to_start && run_state == Running) { 102 if (is_waiting_to_start && run_state == Running) {
102 TRACE_EVENT_ASYNC_BEGIN1( 103 TRACE_EVENT_ASYNC_BEGIN1(
103 "cc", "Animation", this, "Name", TRACE_STR_COPY(name_buffer)); 104 "cc", "Animation", this, "Name", TRACE_STR_COPY(name_buffer));
104 } 105 }
105 106
106 bool was_finished = is_finished(); 107 bool was_finished = is_finished();
107 108
108 const char* old_run_state_name = s_runStateNames[run_state_]; 109 const char* old_run_state_name = s_runStateNames[run_state_];
109 110
110 if (run_state == Running && run_state_ == Paused) 111 if (run_state == Running && run_state_ == Paused)
111 total_paused_time_ += monotonic_time - pause_time_; 112 total_paused_time_ += (monotonic_time - pause_time_);
112 else if (run_state == Paused) 113 else if (run_state == Paused)
113 pause_time_ = monotonic_time; 114 pause_time_ = monotonic_time;
114 run_state_ = run_state; 115 run_state_ = run_state;
115 116
116 const char* new_run_state_name = s_runStateNames[run_state]; 117 const char* new_run_state_name = s_runStateNames[run_state];
117 118
118 if (!was_finished && is_finished()) 119 if (!was_finished && is_finished())
119 TRACE_EVENT_ASYNC_END0("cc", "Animation", this); 120 TRACE_EVENT_ASYNC_END0("cc", "Animation", this);
120 121
121 char state_buffer[256]; 122 char state_buffer[256];
122 base::snprintf(state_buffer, 123 base::snprintf(state_buffer,
123 sizeof(state_buffer), 124 sizeof(state_buffer),
124 "%s->%s", 125 "%s->%s",
125 old_run_state_name, 126 old_run_state_name,
126 new_run_state_name); 127 new_run_state_name);
127 128
128 TRACE_EVENT_INSTANT2("cc", 129 TRACE_EVENT_INSTANT2("cc",
129 "LayerAnimationController::SetRunState", 130 "LayerAnimationController::SetRunState",
130 TRACE_EVENT_SCOPE_THREAD, 131 TRACE_EVENT_SCOPE_THREAD,
131 "Name", 132 "Name",
132 TRACE_STR_COPY(name_buffer), 133 TRACE_STR_COPY(name_buffer),
133 "State", 134 "State",
134 TRACE_STR_COPY(state_buffer)); 135 TRACE_STR_COPY(state_buffer));
135 } 136 }
136 137
137 void Animation::Suspend(double monotonic_time) { 138 void Animation::Suspend(base::TimeTicks monotonic_time) {
138 SetRunState(Paused, monotonic_time); 139 SetRunState(Paused, monotonic_time);
139 suspended_ = true; 140 suspended_ = true;
140 } 141 }
141 142
142 void Animation::Resume(double monotonic_time) { 143 void Animation::Resume(base::TimeTicks monotonic_time) {
143 suspended_ = false; 144 suspended_ = false;
144 SetRunState(Running, monotonic_time); 145 SetRunState(Running, monotonic_time);
145 } 146 }
146 147
147 bool Animation::IsFinishedAt(double monotonic_time) const { 148 bool Animation::IsFinishedAt(base::TimeTicks monotonic_time) const {
148 if (is_finished()) 149 if (is_finished())
149 return true; 150 return true;
150 151
151 if (needs_synchronized_start_time_) 152 if (needs_synchronized_start_time_)
152 return false; 153 return false;
153 154
154 return run_state_ == Running && 155 return run_state_ == Running && iterations_ >= 0 &&
155 iterations_ >= 0 && 156 iterations_ * curve_->Duration() <=
156 iterations_ * curve_->Duration() <= (monotonic_time - 157 (monotonic_time + time_offset_ - start_time_ - total_paused_time_)
157 start_time() - 158 .InSecondsF();
158 total_paused_time_ +
159 time_offset_);
160 } 159 }
161 160
162 double Animation::TrimTimeToCurrentIteration(double monotonic_time) const { 161 double Animation::TrimTimeToCurrentIteration(
163 double trimmed = monotonic_time + time_offset_; 162 base::TimeTicks monotonic_time) const {
163 base::TimeTicks trimmed = monotonic_time + time_offset_;
164 164
165 // If we're paused, time is 'stuck' at the pause time. 165 // If we're paused, time is 'stuck' at the pause time.
166 if (run_state_ == Paused) 166 if (run_state_ == Paused)
167 trimmed = pause_time_; 167 trimmed = pause_time_;
168 168
169 // Returned time should always be relative to the start time and should 169 // Returned time should always be relative to the start time and should
170 // subtract all time spent paused. 170 // subtract all time spent paused.
171 trimmed -= start_time_ + total_paused_time_; 171 trimmed -= start_time_ + total_paused_time_ - base::TimeTicks();
ajuma 2014/05/07 17:04:42 trimmed -= (start_time_ - base::TimeTicks()) + tot
Sikugu_ 2014/05/12 16:01:53 Done.
172 172
173 // If we're just starting or we're waiting on receiving a start time, 173 // If we're just starting or we're waiting on receiving a start time,
174 // time is 'stuck' at the initial state. 174 // time is 'stuck' at the initial state.
175 if ((run_state_ == Starting && !has_set_start_time()) || 175 if ((run_state_ == Starting && !has_set_start_time()) ||
176 needs_synchronized_start_time()) 176 needs_synchronized_start_time())
177 trimmed = time_offset_; 177 trimmed = base::TimeTicks() + time_offset_;
178
179 double trimmed_in_seconds = (trimmed - base::TimeTicks()).InSecondsF();
178 180
179 // Return 0 if we are before the start of the animation 181 // Return 0 if we are before the start of the animation
180 if (trimmed < 0) 182 if (trimmed_in_seconds < 0)
181 return 0; 183 return 0;
182 184
183 // Always return zero if we have no iterations. 185 // Always return zero if we have no iterations.
184 if (!iterations_) 186 if (!iterations_)
185 return 0; 187 return 0;
186 188
187 // Don't attempt to trim if we have no duration. 189 // Don't attempt to trim if we have no duration.
188 if (curve_->Duration() <= 0) 190 if (curve_->Duration() <= 0)
189 return 0; 191 return 0;
190 192
191 // check if we are past active interval 193 // check if we are past active interval
192 bool is_past_total_duration = 194 bool is_past_total_duration =
193 (iterations_ > 0 && trimmed >= curve_->Duration() * iterations_); 195 (iterations_ > 0 &&
196 trimmed_in_seconds >= curve_->Duration() * iterations_);
194 197
195 // We need to know the current iteration if we're alternating. 198 // We need to know the current iteration if we're alternating.
196 int iteration = 0; 199 int iteration = 0;
197 200
198 // If we are past the active interval, return iteration duration. 201 // If we are past the active interval, return iteration duration.
199 if (is_past_total_duration) { 202 if (is_past_total_duration) {
200 iteration = iterations_ - 1; 203 iteration = iterations_ - 1;
201 trimmed = curve_->Duration(); 204 trimmed_in_seconds = curve_->Duration();
202 } else { 205 } else {
203 iteration = static_cast<int>(trimmed / curve_->Duration()); 206 iteration = static_cast<int>(trimmed_in_seconds / curve_->Duration());
204 // Calculate x where trimmed = x + n * curve_->Duration() for some positive 207 // Calculate x where trimmed = x + n * curve_->Duration() for some positive
205 // integer n. 208 // integer n.
206 trimmed = fmod(trimmed, curve_->Duration()); 209 trimmed_in_seconds = fmod(trimmed_in_seconds, curve_->Duration());
207 } 210 }
208 211
209 // check if we are running the animation in reverse direction for the current 212 // check if we are running the animation in reverse direction for the current
210 // iteration 213 // iteration
211 bool reverse = (direction_ == Reverse) || 214 bool reverse = (direction_ == Reverse) ||
212 (direction_ == Alternate && iteration % 2 == 1) || 215 (direction_ == Alternate && iteration % 2 == 1) ||
213 (direction_ == AlternateReverse && iteration % 2 == 0); 216 (direction_ == AlternateReverse && iteration % 2 == 0);
214 217
215 // if we are running the animation in reverse direction, reverse the result 218 // if we are running the animation in reverse direction, reverse the result
216 if (reverse) 219 if (reverse)
217 return curve_->Duration() - trimmed; 220 return curve_->Duration() - trimmed_in_seconds;
218 221
219 return trimmed; 222 return trimmed_in_seconds;
220 } 223 }
221 224
222 scoped_ptr<Animation> Animation::CloneAndInitialize( 225 scoped_ptr<Animation> Animation::CloneAndInitialize(
223 RunState initial_run_state) const { 226 RunState initial_run_state) const {
224 scoped_ptr<Animation> to_return( 227 scoped_ptr<Animation> to_return(
225 new Animation(curve_->Clone(), id_, group_, target_property_)); 228 new Animation(curve_->Clone(), id_, group_, target_property_));
226 to_return->run_state_ = initial_run_state; 229 to_return->run_state_ = initial_run_state;
227 to_return->iterations_ = iterations_; 230 to_return->iterations_ = iterations_;
228 to_return->start_time_ = start_time_; 231 to_return->start_time_ = start_time_;
229 to_return->pause_time_ = pause_time_; 232 to_return->pause_time_ = pause_time_;
(...skipping 10 matching lines...) Expand all
240 // the main thread. 243 // the main thread.
241 if (run_state_ == Animation::Paused || 244 if (run_state_ == Animation::Paused ||
242 other->run_state_ == Animation::Paused) { 245 other->run_state_ == Animation::Paused) {
243 other->run_state_ = run_state_; 246 other->run_state_ = run_state_;
244 other->pause_time_ = pause_time_; 247 other->pause_time_ = pause_time_;
245 other->total_paused_time_ = total_paused_time_; 248 other->total_paused_time_ = total_paused_time_;
246 } 249 }
247 } 250 }
248 251
249 } // namespace cc 252 } // namespace cc
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698