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

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: Fix for build errors in ui/compositor unittests. 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)),
aelias_OOO_until_Jul13 2014/05/13 06:00:07 This default value is a bad idea here, particularl
ajuma 2014/05/13 13:44:48 Fwiw, TimeTicks uses a (signed) int64 internally.
Sikugu_ 2014/05/14 14:23:18 Done.
Sikugu_ 2014/05/14 14:23:18 Done.
71 timing_function_(timing_function.Pass()) {} 74 timing_function_(timing_function.Pass()) {
75 }
72 76
73 PageScaleAnimation::~PageScaleAnimation() {} 77 PageScaleAnimation::~PageScaleAnimation() {}
74 78
75 void PageScaleAnimation::ZoomTo(const gfx::Vector2dF& target_scroll_offset, 79 void PageScaleAnimation::ZoomTo(const gfx::Vector2dF& target_scroll_offset,
76 float target_page_scale_factor, 80 float target_page_scale_factor,
77 double duration) { 81 double duration) {
78 target_page_scale_factor_ = target_page_scale_factor; 82 target_page_scale_factor_ = target_page_scale_factor;
79 target_scroll_offset_ = target_scroll_offset; 83 target_scroll_offset_ = target_scroll_offset;
80 ClampTargetScrollOffset(); 84 ClampTargetScrollOffset();
81 duration_ = duration; 85 duration_ = TimeDelta::FromSecondsD(duration);
82 86
83 if (start_page_scale_factor_ == target_page_scale_factor) { 87 if (start_page_scale_factor_ == target_page_scale_factor) {
84 start_anchor_ = start_scroll_offset_; 88 start_anchor_ = start_scroll_offset_;
85 target_anchor_ = target_scroll_offset; 89 target_anchor_ = target_scroll_offset;
86 return; 90 return;
87 } 91 }
88 92
89 // For uniform-looking zooming, infer an anchor from the start and target 93 // For uniform-looking zooming, infer an anchor from the start and target
90 // viewport rects. 94 // viewport rects.
91 InferTargetAnchorFromScrollOffsets(); 95 InferTargetAnchorFromScrollOffsets();
92 start_anchor_ = target_anchor_; 96 start_anchor_ = target_anchor_;
93 } 97 }
94 98
95 void PageScaleAnimation::ZoomWithAnchor(const gfx::Vector2dF& anchor, 99 void PageScaleAnimation::ZoomWithAnchor(const gfx::Vector2dF& anchor,
96 float target_page_scale_factor, 100 float target_page_scale_factor,
97 double duration) { 101 double duration) {
98 start_anchor_ = anchor; 102 start_anchor_ = anchor;
99 target_page_scale_factor_ = target_page_scale_factor; 103 target_page_scale_factor_ = target_page_scale_factor;
100 duration_ = duration; 104 duration_ = TimeDelta::FromSecondsD(duration);
101 105
102 // We start zooming out from the anchor tapped by the user. But if 106 // 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 107 // 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. 108 // edges, then infer an anchor that doesn't collide with the edges.
105 // We will interpolate between the two anchors during the animation. 109 // We will interpolate between the two anchors during the animation.
106 InferTargetScrollOffsetFromStartAnchor(); 110 InferTargetScrollOffsetFromStartAnchor();
107 ClampTargetScrollOffset(); 111 ClampTargetScrollOffset();
108 112
109 if (start_page_scale_factor_ == target_page_scale_factor_) { 113 if (start_page_scale_factor_ == target_page_scale_factor_) {
110 target_anchor_ = start_anchor_; 114 target_anchor_ = start_anchor_;
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
156 160
157 gfx::SizeF PageScaleAnimation::TargetViewportSize() const { 161 gfx::SizeF PageScaleAnimation::TargetViewportSize() const {
158 return gfx::ScaleSize(viewport_size_, 1.f / target_page_scale_factor_); 162 return gfx::ScaleSize(viewport_size_, 1.f / target_page_scale_factor_);
159 } 163 }
160 164
161 gfx::SizeF PageScaleAnimation::ViewportSizeAt(float interp) const { 165 gfx::SizeF PageScaleAnimation::ViewportSizeAt(float interp) const {
162 return gfx::ScaleSize(viewport_size_, 1.f / PageScaleFactorAt(interp)); 166 return gfx::ScaleSize(viewport_size_, 1.f / PageScaleFactorAt(interp));
163 } 167 }
164 168
165 bool PageScaleAnimation::IsAnimationStarted() const { 169 bool PageScaleAnimation::IsAnimationStarted() const {
166 return start_time_ >= 0; 170 return start_time_ >= base::TimeTicks();
167 } 171 }
168 172
169 void PageScaleAnimation::StartAnimation(double time) { 173 void PageScaleAnimation::StartAnimation(base::TimeTicks time) {
170 DCHECK_GT(0, start_time_); 174 DCHECK(start_time_ < base::TimeTicks());
171 start_time_ = time; 175 start_time_ = time;
172 } 176 }
173 177
174 gfx::Vector2dF PageScaleAnimation::ScrollOffsetAtTime(double time) const { 178 gfx::Vector2dF PageScaleAnimation::ScrollOffsetAtTime(
175 DCHECK_GE(start_time_, 0); 179 base::TimeTicks time) const {
180 DCHECK(start_time_ >= base::TimeTicks());
176 return ScrollOffsetAt(InterpAtTime(time)); 181 return ScrollOffsetAt(InterpAtTime(time));
177 } 182 }
178 183
179 float PageScaleAnimation::PageScaleFactorAtTime(double time) const { 184 float PageScaleAnimation::PageScaleFactorAtTime(base::TimeTicks time) const {
180 DCHECK_GE(start_time_, 0); 185 DCHECK(start_time_ >= base::TimeTicks());
181 return PageScaleFactorAt(InterpAtTime(time)); 186 return PageScaleFactorAt(InterpAtTime(time));
182 } 187 }
183 188
184 bool PageScaleAnimation::IsAnimationCompleteAtTime(double time) const { 189 bool PageScaleAnimation::IsAnimationCompleteAtTime(base::TimeTicks time) const {
185 DCHECK_GE(start_time_, 0); 190 DCHECK(start_time_ >= base::TimeTicks());
186 return time >= end_time(); 191 return time >= end_time();
187 } 192 }
188 193
189 float PageScaleAnimation::InterpAtTime(double time) const { 194 float PageScaleAnimation::InterpAtTime(base::TimeTicks monotonic_time) const {
190 DCHECK_GE(start_time_, 0); 195 DCHECK(start_time_ >= base::TimeTicks());
191 DCHECK_GE(time, start_time_); 196 DCHECK(monotonic_time >= start_time_);
192 if (IsAnimationCompleteAtTime(time)) 197 if (IsAnimationCompleteAtTime(monotonic_time))
193 return 1.f; 198 return 1.f;
194 199 const double normalized_time =
195 const double normalized_time = (time - start_time_) / duration_; 200 (monotonic_time - start_time_).InSecondsF() / duration_.InSecondsF();
196 return timing_function_->GetValue(normalized_time); 201 return timing_function_->GetValue(normalized_time);
197 } 202 }
198 203
199 gfx::Vector2dF PageScaleAnimation::ScrollOffsetAt(float interp) const { 204 gfx::Vector2dF PageScaleAnimation::ScrollOffsetAt(float interp) const {
200 if (interp <= 0.f) 205 if (interp <= 0.f)
201 return start_scroll_offset_; 206 return start_scroll_offset_;
202 if (interp >= 1.f) 207 if (interp >= 1.f)
203 return target_scroll_offset_; 208 return target_scroll_offset_;
204 209
205 return AnchorAt(interp) - ViewportRelativeAnchorAt(interp); 210 return AnchorAt(interp) - ViewportRelativeAnchorAt(interp);
(...skipping 27 matching lines...) Expand all
233 238
234 // Linearly interpolate the magnitude in log scale. 239 // Linearly interpolate the magnitude in log scale.
235 float diff = target_page_scale_factor_ / start_page_scale_factor_; 240 float diff = target_page_scale_factor_ / start_page_scale_factor_;
236 float log_diff = log(diff); 241 float log_diff = log(diff);
237 log_diff *= interp; 242 log_diff *= interp;
238 diff = exp(log_diff); 243 diff = exp(log_diff);
239 return start_page_scale_factor_ * diff; 244 return start_page_scale_factor_ * diff;
240 } 245 }
241 246
242 } // namespace cc 247 } // namespace cc
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698