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

Side by Side Diff: cc/input/page_scale_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/input/page_scale_animation.h" 5 #include "cc/input/page_scale_animation.h"
6 6
7 #include <math.h> 7 #include <math.h>
8 8
9 #include "base/logging.h" 9 #include "base/logging.h"
10 #include "cc/animation/timing_function.h" 10 #include "cc/animation/timing_function.h"
(...skipping 22 matching lines...) Expand all
33 gfx::Vector2dF InterpolateBetween(const gfx::Vector2dF& start, 33 gfx::Vector2dF InterpolateBetween(const gfx::Vector2dF& start,
34 const gfx::Vector2dF& end, 34 const gfx::Vector2dF& end,
35 float interp) { 35 float interp) {
36 return start + gfx::ScaleVector2d(end - start, interp); 36 return start + gfx::ScaleVector2d(end - start, interp);
37 } 37 }
38 38
39 } // namespace 39 } // namespace
40 40
41 namespace cc { 41 namespace cc {
42 42
43 using base::TimeTicks;
44 using base::TimeDelta;
45
43 scoped_ptr<PageScaleAnimation> PageScaleAnimation::Create( 46 scoped_ptr<PageScaleAnimation> PageScaleAnimation::Create(
44 const gfx::Vector2dF& start_scroll_offset, 47 const gfx::Vector2dF& start_scroll_offset,
45 float start_page_scale_factor, 48 float start_page_scale_factor,
46 const gfx::SizeF& viewport_size, 49 const gfx::SizeF& viewport_size,
47 const gfx::SizeF& root_layer_size, 50 const gfx::SizeF& root_layer_size,
48 scoped_ptr<TimingFunction> timing_function) { 51 scoped_ptr<TimingFunction> timing_function) {
49 return make_scoped_ptr(new PageScaleAnimation(start_scroll_offset, 52 return make_scoped_ptr(new PageScaleAnimation(start_scroll_offset,
50 start_page_scale_factor, 53 start_page_scale_factor,
51 viewport_size, 54 viewport_size,
52 root_layer_size, 55 root_layer_size,
53 timing_function.Pass())); 56 timing_function.Pass()));
54 } 57 }
55 58
56 PageScaleAnimation::PageScaleAnimation( 59 PageScaleAnimation::PageScaleAnimation(
57 const gfx::Vector2dF& start_scroll_offset, 60 const gfx::Vector2dF& start_scroll_offset,
58 float start_page_scale_factor, 61 float start_page_scale_factor,
59 const gfx::SizeF& viewport_size, 62 const gfx::SizeF& viewport_size,
60 const gfx::SizeF& root_layer_size, 63 const gfx::SizeF& root_layer_size,
61 scoped_ptr<TimingFunction> timing_function) 64 scoped_ptr<TimingFunction> timing_function)
62 : start_page_scale_factor_(start_page_scale_factor), 65 : start_page_scale_factor_(start_page_scale_factor),
63 target_page_scale_factor_(0.f), 66 target_page_scale_factor_(0.f),
64 start_scroll_offset_(start_scroll_offset), 67 start_scroll_offset_(start_scroll_offset),
65 start_anchor_(), 68 start_anchor_(),
66 target_anchor_(), 69 target_anchor_(),
67 viewport_size_(viewport_size), 70 viewport_size_(viewport_size),
68 root_layer_size_(root_layer_size), 71 root_layer_size_(root_layer_size),
69 start_time_(-1.0), 72 start_time_(TimeTicks::FromInternalValue(
70 duration_(0.0), 73 -1.0 * base::Time::kMicrosecondsPerSecond)),
71 timing_function_(timing_function.Pass()) {} 74 duration_(),
ajuma 2014/05/07 17:04:42 Not needed.
Sikugu_ 2014/05/12 16:01:53 Done.
75 timing_function_(timing_function.Pass()) {
76 }
72 77
73 PageScaleAnimation::~PageScaleAnimation() {} 78 PageScaleAnimation::~PageScaleAnimation() {}
74 79
75 void PageScaleAnimation::ZoomTo(const gfx::Vector2dF& target_scroll_offset, 80 void PageScaleAnimation::ZoomTo(const gfx::Vector2dF& target_scroll_offset,
76 float target_page_scale_factor, 81 float target_page_scale_factor,
77 double duration) { 82 double duration) {
78 target_page_scale_factor_ = target_page_scale_factor; 83 target_page_scale_factor_ = target_page_scale_factor;
79 target_scroll_offset_ = target_scroll_offset; 84 target_scroll_offset_ = target_scroll_offset;
80 ClampTargetScrollOffset(); 85 ClampTargetScrollOffset();
81 duration_ = duration; 86 duration_ = TimeDelta::FromSecondsD(duration);
82 87
83 if (start_page_scale_factor_ == target_page_scale_factor) { 88 if (start_page_scale_factor_ == target_page_scale_factor) {
84 start_anchor_ = start_scroll_offset_; 89 start_anchor_ = start_scroll_offset_;
85 target_anchor_ = target_scroll_offset; 90 target_anchor_ = target_scroll_offset;
86 return; 91 return;
87 } 92 }
88 93
89 // For uniform-looking zooming, infer an anchor from the start and target 94 // For uniform-looking zooming, infer an anchor from the start and target
90 // viewport rects. 95 // viewport rects.
91 InferTargetAnchorFromScrollOffsets(); 96 InferTargetAnchorFromScrollOffsets();
92 start_anchor_ = target_anchor_; 97 start_anchor_ = target_anchor_;
93 } 98 }
94 99
95 void PageScaleAnimation::ZoomWithAnchor(const gfx::Vector2dF& anchor, 100 void PageScaleAnimation::ZoomWithAnchor(const gfx::Vector2dF& anchor,
96 float target_page_scale_factor, 101 float target_page_scale_factor,
97 double duration) { 102 double duration) {
98 start_anchor_ = anchor; 103 start_anchor_ = anchor;
99 target_page_scale_factor_ = target_page_scale_factor; 104 target_page_scale_factor_ = target_page_scale_factor;
100 duration_ = duration; 105 duration_ = TimeDelta::FromSecondsD(duration);
101 106
102 // We start zooming out from the anchor tapped by the user. But if 107 // We start zooming out from the anchor tapped by the user. But if
103 // the target scale is impossible to attain without hitting the root layer 108 // the target scale is impossible to attain without hitting the root layer
104 // edges, then infer an anchor that doesn't collide with the edges. 109 // edges, then infer an anchor that doesn't collide with the edges.
105 // We will interpolate between the two anchors during the animation. 110 // We will interpolate between the two anchors during the animation.
106 InferTargetScrollOffsetFromStartAnchor(); 111 InferTargetScrollOffsetFromStartAnchor();
107 ClampTargetScrollOffset(); 112 ClampTargetScrollOffset();
108 113
109 if (start_page_scale_factor_ == target_page_scale_factor_) { 114 if (start_page_scale_factor_ == target_page_scale_factor_) {
110 target_anchor_ = start_anchor_; 115 target_anchor_ = start_anchor_;
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
156 161
157 gfx::SizeF PageScaleAnimation::TargetViewportSize() const { 162 gfx::SizeF PageScaleAnimation::TargetViewportSize() const {
158 return gfx::ScaleSize(viewport_size_, 1.f / target_page_scale_factor_); 163 return gfx::ScaleSize(viewport_size_, 1.f / target_page_scale_factor_);
159 } 164 }
160 165
161 gfx::SizeF PageScaleAnimation::ViewportSizeAt(float interp) const { 166 gfx::SizeF PageScaleAnimation::ViewportSizeAt(float interp) const {
162 return gfx::ScaleSize(viewport_size_, 1.f / PageScaleFactorAt(interp)); 167 return gfx::ScaleSize(viewport_size_, 1.f / PageScaleFactorAt(interp));
163 } 168 }
164 169
165 bool PageScaleAnimation::IsAnimationStarted() const { 170 bool PageScaleAnimation::IsAnimationStarted() const {
166 return start_time_ >= 0; 171 return (start_time_ - base::TimeTicks()).InSecondsF() >= 0;
ajuma 2014/05/07 17:04:42 return start_time_ > base::TimeTicks(); is more e
ajuma 2014/05/07 17:06:49 Sorry, make that: return start_time_ >= base::Tim
Sikugu_ 2014/05/12 16:01:53 Done.
167 } 172 }
168 173
169 void PageScaleAnimation::StartAnimation(double time) { 174 void PageScaleAnimation::StartAnimation(base::TimeTicks time) {
170 DCHECK_GT(0, start_time_); 175 DCHECK_GT(0, (start_time_ - base::TimeTicks()).InSecondsF());
ajuma 2014/05/07 17:04:42 DCHECK(start_time_ < base::TimeTicks()); is easie
Sikugu_ 2014/05/12 16:01:53 Done.
171 start_time_ = time; 176 start_time_ = time;
172 } 177 }
173 178
174 gfx::Vector2dF PageScaleAnimation::ScrollOffsetAtTime(double time) const { 179 gfx::Vector2dF PageScaleAnimation::ScrollOffsetAtTime(
175 DCHECK_GE(start_time_, 0); 180 base::TimeTicks time) const {
181 DCHECK_GE((start_time_ - base::TimeTicks()).InSecondsF(), 0);
ajuma 2014/05/07 17:04:42 DCHECK(start_time_>= base::TimeTicks());
Sikugu_ 2014/05/12 16:01:53 Done.
176 return ScrollOffsetAt(InterpAtTime(time)); 182 return ScrollOffsetAt(InterpAtTime(time));
177 } 183 }
178 184
179 float PageScaleAnimation::PageScaleFactorAtTime(double time) const { 185 float PageScaleAnimation::PageScaleFactorAtTime(base::TimeTicks time) const {
180 DCHECK_GE(start_time_, 0); 186 DCHECK_GE((start_time_ - base::TimeTicks()).InSecondsF(), 0);
ajuma 2014/05/07 17:04:42 DCHECK(start_time_ >= base::TimeTicks());
Sikugu_ 2014/05/12 16:01:53 Done.
181 return PageScaleFactorAt(InterpAtTime(time)); 187 return PageScaleFactorAt(InterpAtTime(time));
182 } 188 }
183 189
184 bool PageScaleAnimation::IsAnimationCompleteAtTime(double time) const { 190 bool PageScaleAnimation::IsAnimationCompleteAtTime(base::TimeTicks time) const {
185 DCHECK_GE(start_time_, 0); 191 DCHECK_GE((start_time_ - base::TimeTicks()).InSecondsF(), 0);
ajuma 2014/05/07 17:04:42 DCHECK(start_time_ >= base::TimeTicks());
Sikugu_ 2014/05/12 16:01:53 Done.
186 return time >= end_time(); 192 return time >= end_time();
187 } 193 }
188 194
189 float PageScaleAnimation::InterpAtTime(double time) const { 195 float PageScaleAnimation::InterpAtTime(base::TimeTicks monotonic_time) const {
190 DCHECK_GE(start_time_, 0); 196 DCHECK_GE((start_time_ - base::TimeTicks()).InSecondsF(), 0);
ajuma 2014/05/07 17:04:42 DCHECK(start_time_ >= base::TimeTicks());
Sikugu_ 2014/05/12 16:01:53 Done.
191 DCHECK_GE(time, start_time_); 197 DCHECK_GE((monotonic_time - base::TimeTicks()).InSecondsF(),
192 if (IsAnimationCompleteAtTime(time)) 198 (start_time_ - base::TimeTicks()).InSecondsF());
ajuma 2014/05/07 17:04:42 DCHECK(monotonic_time >= start_time_);
Sikugu_ 2014/05/12 16:01:53 Done.
199 if (IsAnimationCompleteAtTime(monotonic_time))
193 return 1.f; 200 return 1.f;
194 201 const double normalized_time =
195 const double normalized_time = (time - start_time_) / duration_; 202 (monotonic_time - start_time_).InSecondsF() / duration_.InSecondsF();
196 return timing_function_->GetValue(normalized_time); 203 return timing_function_->GetValue(normalized_time);
197 } 204 }
198 205
199 gfx::Vector2dF PageScaleAnimation::ScrollOffsetAt(float interp) const { 206 gfx::Vector2dF PageScaleAnimation::ScrollOffsetAt(float interp) const {
200 if (interp <= 0.f) 207 if (interp <= 0.f)
201 return start_scroll_offset_; 208 return start_scroll_offset_;
202 if (interp >= 1.f) 209 if (interp >= 1.f)
203 return target_scroll_offset_; 210 return target_scroll_offset_;
204 211
205 return AnchorAt(interp) - ViewportRelativeAnchorAt(interp); 212 return AnchorAt(interp) - ViewportRelativeAnchorAt(interp);
(...skipping 27 matching lines...) Expand all
233 240
234 // Linearly interpolate the magnitude in log scale. 241 // Linearly interpolate the magnitude in log scale.
235 float diff = target_page_scale_factor_ / start_page_scale_factor_; 242 float diff = target_page_scale_factor_ / start_page_scale_factor_;
236 float log_diff = log(diff); 243 float log_diff = log(diff);
237 log_diff *= interp; 244 log_diff *= interp;
238 diff = exp(log_diff); 245 diff = exp(log_diff);
239 return start_page_scale_factor_ * diff; 246 return start_page_scale_factor_ * diff;
240 } 247 }
241 248
242 } // namespace cc 249 } // namespace cc
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698