Chromium Code Reviews

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 Refactored as per the comments. Created 6 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff |
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...)
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_(base::TimeTicks()),
ajuma 2014/04/24 20:41:36 This line can be removed (the default constructor
Sikugu_ 2014/05/05 07:09:19 Done.
68 alternates_direction_(false), 68 alternates_direction_(false),
69 time_offset_(0), 69 time_offset_(base::TimeDelta()),
ajuma 2014/04/24 20:41:36 Can be removed.
Sikugu_ 2014/05/05 07:09:19 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_(base::TimeTicks()),
ajuma 2014/04/24 20:41:36 Remove.
Sikugu_ 2014/05/05 07:09:19 Done.
74 total_paused_time_(0), 74 total_paused_time_(base::TimeTicks()),
ajuma 2014/04/24 20:41:36 Remove.
Sikugu_ 2014/05/05 07:09:19 Done.
75 is_controlling_instance_(false), 75 is_controlling_instance_(false),
76 is_impl_only_(false) {} 76 is_impl_only_(false) {
77 }
77 78
78 Animation::~Animation() { 79 Animation::~Animation() {
79 if (run_state_ == Running || run_state_ == Paused) 80 if (run_state_ == Running || run_state_ == Paused)
80 SetRunState(Aborted, 0); 81 SetRunState(Aborted, base::TimeTicks());
81 } 82 }
82 83
83 void Animation::SetRunState(RunState run_state, double monotonic_time) { 84 void Animation::SetRunState(RunState run_state,
85 base::TimeTicks monotonic_time) {
84 if (suspended_) 86 if (suspended_)
85 return; 87 return;
86 88
87 char name_buffer[256]; 89 char name_buffer[256];
88 base::snprintf(name_buffer, 90 base::snprintf(name_buffer,
89 sizeof(name_buffer), 91 sizeof(name_buffer),
90 "%s-%d%s", 92 "%s-%d%s",
91 s_targetPropertyNames[target_property_], 93 s_targetPropertyNames[target_property_],
92 group_, 94 group_,
93 is_controlling_instance_ ? "(impl)" : ""); 95 is_controlling_instance_ ? "(impl)" : "");
(...skipping 30 matching lines...)
124 126
125 TRACE_EVENT_INSTANT2("cc", 127 TRACE_EVENT_INSTANT2("cc",
126 "LayerAnimationController::SetRunState", 128 "LayerAnimationController::SetRunState",
127 TRACE_EVENT_SCOPE_THREAD, 129 TRACE_EVENT_SCOPE_THREAD,
128 "Name", 130 "Name",
129 TRACE_STR_COPY(name_buffer), 131 TRACE_STR_COPY(name_buffer),
130 "State", 132 "State",
131 TRACE_STR_COPY(state_buffer)); 133 TRACE_STR_COPY(state_buffer));
132 } 134 }
133 135
134 void Animation::Suspend(double monotonic_time) { 136 void Animation::Suspend(base::TimeTicks monotonic_time) {
135 SetRunState(Paused, monotonic_time); 137 SetRunState(Paused, monotonic_time);
136 suspended_ = true; 138 suspended_ = true;
137 } 139 }
138 140
139 void Animation::Resume(double monotonic_time) { 141 void Animation::Resume(base::TimeTicks monotonic_time) {
140 suspended_ = false; 142 suspended_ = false;
141 SetRunState(Running, monotonic_time); 143 SetRunState(Running, monotonic_time);
142 } 144 }
143 145
144 bool Animation::IsFinishedAt(double monotonic_time) const { 146 bool Animation::IsFinishedAt(base::TimeTicks monotonic_time) const {
145 if (is_finished()) 147 if (is_finished())
146 return true; 148 return true;
147 149
148 if (needs_synchronized_start_time_) 150 if (needs_synchronized_start_time_)
149 return false; 151 return false;
150 152
151 return run_state_ == Running && 153 return run_state_ == Running && iterations_ >= 0 &&
152 iterations_ >= 0 && 154 iterations_ * curve_->Duration() <=
153 iterations_ * curve_->Duration() <= (monotonic_time - 155 base::TimeTicks::InSecondsF((monotonic_time + time_offset_) -
ajuma 2014/04/24 20:41:36 Duration should be compared to an amount of time,
Sikugu_ 2014/05/05 07:09:19 When we start adding the Timedelta to Timeticks we
Sikugu_ 2014/05/05 07:09:19 When we start adding the Timedelta to Timeticks we
ajuma 2014/05/05 15:13:32 Adding TimeDelta to TimeTicks should not lose prec
Sikugu_ 2014/05/07 14:49:07 Its a misinterpretation by me, it works fine with
154 start_time() - 156 (start_time_ - total_paused_time_));
155 total_paused_time_ +
156 time_offset_);
157 } 157 }
158 158
159 double Animation::TrimTimeToCurrentIteration(double monotonic_time) const { 159 double Animation::TrimTimeToCurrentIteration(
160 double trimmed = monotonic_time + time_offset_; 160 base::TimeTicks monotonic_time) const {
161 double trimmed = base::TimeTicks::InSecondsF(monotonic_time + time_offset_);
ajuma 2014/04/24 20:41:36 Instead of converting to seconds here, wait until
Sikugu_ 2014/05/05 07:09:19 Same here.
ajuma 2014/05/05 15:13:32 And same response :) Please give a specific examp
Sikugu_ 2014/05/07 14:49:07 Its a misinterpretation by me, it works fine with
161 162
162 // If we're paused, time is 'stuck' at the pause time. 163 // If we're paused, time is 'stuck' at the pause time.
163 if (run_state_ == Paused) 164 if (run_state_ == Paused)
164 trimmed = pause_time_; 165 trimmed = base::TimeTicks::InSecondsF(pause_time_);
165 166
166 // Returned time should always be relative to the start time and should 167 // Returned time should always be relative to the start time and should
167 // subtract all time spent paused. 168 // subtract all time spent paused.
168 trimmed -= start_time_ + total_paused_time_; 169 trimmed -= base::TimeTicks::InSecondsF(start_time_) +
170 base::TimeTicks::InSecondsF(total_paused_time_);
169 171
170 // If we're just starting or we're waiting on receiving a start time, 172 // If we're just starting or we're waiting on receiving a start time,
171 // time is 'stuck' at the initial state. 173 // time is 'stuck' at the initial state.
172 if ((run_state_ == Starting && !has_set_start_time()) || 174 if ((run_state_ == Starting && !has_set_start_time()) ||
173 needs_synchronized_start_time()) 175 needs_synchronized_start_time())
174 trimmed = time_offset_; 176 trimmed = time_offset_.InSecondsF();
175 177
176 // Zero is always the start of the animation. 178 // Zero is always the start of the animation.
177 if (trimmed <= 0) 179 if (trimmed <= 0)
178 return 0; 180 return 0;
179 181
180 // Always return zero if we have no iterations. 182 // Always return zero if we have no iterations.
181 if (!iterations_) 183 if (!iterations_)
182 return 0; 184 return 0;
183 185
184 // Don't attempt to trim if we have no duration. 186 // Don't attempt to trim if we have no duration.
(...skipping 46 matching lines...)
231 // the main thread. 233 // the main thread.
232 if (run_state_ == Animation::Paused || 234 if (run_state_ == Animation::Paused ||
233 other->run_state_ == Animation::Paused) { 235 other->run_state_ == Animation::Paused) {
234 other->run_state_ = run_state_; 236 other->run_state_ = run_state_;
235 other->pause_time_ = pause_time_; 237 other->pause_time_ = pause_time_;
236 other->total_paused_time_ = total_paused_time_; 238 other->total_paused_time_ = total_paused_time_;
237 } 239 }
238 } 240 }
239 241
240 } // namespace cc 242 } // namespace cc
OLDNEW

Powered by Google App Engine