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

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: Self review changes. 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"
11 #include "cc/base/time_util.h"
11 #include "ui/gfx/point_f.h" 12 #include "ui/gfx/point_f.h"
12 #include "ui/gfx/rect_f.h" 13 #include "ui/gfx/rect_f.h"
13 #include "ui/gfx/vector2d_conversions.h" 14 #include "ui/gfx/vector2d_conversions.h"
14 15
15 namespace { 16 namespace {
16 17
17 // This takes a viewport-relative vector and returns a vector whose values are 18 // This takes a viewport-relative vector and returns a vector whose values are
18 // between 0 and 1, representing the percentage position within the viewport. 19 // between 0 and 1, representing the percentage position within the viewport.
19 gfx::Vector2dF NormalizeFromViewport(const gfx::Vector2dF& denormalized, 20 gfx::Vector2dF NormalizeFromViewport(const gfx::Vector2dF& denormalized,
20 const gfx::SizeF& viewport_size) { 21 const gfx::SizeF& viewport_size) {
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
59 const gfx::SizeF& viewport_size, 60 const gfx::SizeF& viewport_size,
60 const gfx::SizeF& root_layer_size, 61 const gfx::SizeF& root_layer_size,
61 scoped_ptr<TimingFunction> timing_function) 62 scoped_ptr<TimingFunction> timing_function)
62 : start_page_scale_factor_(start_page_scale_factor), 63 : start_page_scale_factor_(start_page_scale_factor),
63 target_page_scale_factor_(0.f), 64 target_page_scale_factor_(0.f),
64 start_scroll_offset_(start_scroll_offset), 65 start_scroll_offset_(start_scroll_offset),
65 start_anchor_(), 66 start_anchor_(),
66 target_anchor_(), 67 target_anchor_(),
67 viewport_size_(viewport_size), 68 viewport_size_(viewport_size),
68 root_layer_size_(root_layer_size), 69 root_layer_size_(root_layer_size),
69 start_time_(-1.0), 70 start_time_(TimeTicks::FromInternalValue(
70 duration_(0.0), 71 -1.0 * base::Time::kMicrosecondsPerSecond)),
71 timing_function_(timing_function.Pass()) {} 72 duration_(),
73 timing_function_(timing_function.Pass()) {
74 }
72 75
73 PageScaleAnimation::~PageScaleAnimation() {} 76 PageScaleAnimation::~PageScaleAnimation() {}
74 77
75 void PageScaleAnimation::ZoomTo(const gfx::Vector2dF& target_scroll_offset, 78 void PageScaleAnimation::ZoomTo(const gfx::Vector2dF& target_scroll_offset,
76 float target_page_scale_factor, 79 float target_page_scale_factor,
77 double duration) { 80 double duration) {
78 target_page_scale_factor_ = target_page_scale_factor; 81 target_page_scale_factor_ = target_page_scale_factor;
79 target_scroll_offset_ = target_scroll_offset; 82 target_scroll_offset_ = target_scroll_offset;
80 ClampTargetScrollOffset(); 83 ClampTargetScrollOffset();
81 duration_ = duration; 84 duration_ = TimeDelta::FromSecondsD(duration);
82 85
83 if (start_page_scale_factor_ == target_page_scale_factor) { 86 if (start_page_scale_factor_ == target_page_scale_factor) {
84 start_anchor_ = start_scroll_offset_; 87 start_anchor_ = start_scroll_offset_;
85 target_anchor_ = target_scroll_offset; 88 target_anchor_ = target_scroll_offset;
86 return; 89 return;
87 } 90 }
88 91
89 // For uniform-looking zooming, infer an anchor from the start and target 92 // For uniform-looking zooming, infer an anchor from the start and target
90 // viewport rects. 93 // viewport rects.
91 InferTargetAnchorFromScrollOffsets(); 94 InferTargetAnchorFromScrollOffsets();
92 start_anchor_ = target_anchor_; 95 start_anchor_ = target_anchor_;
93 } 96 }
94 97
95 void PageScaleAnimation::ZoomWithAnchor(const gfx::Vector2dF& anchor, 98 void PageScaleAnimation::ZoomWithAnchor(const gfx::Vector2dF& anchor,
96 float target_page_scale_factor, 99 float target_page_scale_factor,
97 double duration) { 100 double duration) {
98 start_anchor_ = anchor; 101 start_anchor_ = anchor;
99 target_page_scale_factor_ = target_page_scale_factor; 102 target_page_scale_factor_ = target_page_scale_factor;
100 duration_ = duration; 103 duration_ = TimeDelta::FromSecondsD(duration);
101 104
102 // We start zooming out from the anchor tapped by the user. But if 105 // 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 106 // 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. 107 // edges, then infer an anchor that doesn't collide with the edges.
105 // We will interpolate between the two anchors during the animation. 108 // We will interpolate between the two anchors during the animation.
106 InferTargetScrollOffsetFromStartAnchor(); 109 InferTargetScrollOffsetFromStartAnchor();
107 ClampTargetScrollOffset(); 110 ClampTargetScrollOffset();
108 111
109 if (start_page_scale_factor_ == target_page_scale_factor_) { 112 if (start_page_scale_factor_ == target_page_scale_factor_) {
110 target_anchor_ = start_anchor_; 113 target_anchor_ = start_anchor_;
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
156 159
157 gfx::SizeF PageScaleAnimation::TargetViewportSize() const { 160 gfx::SizeF PageScaleAnimation::TargetViewportSize() const {
158 return gfx::ScaleSize(viewport_size_, 1.f / target_page_scale_factor_); 161 return gfx::ScaleSize(viewport_size_, 1.f / target_page_scale_factor_);
159 } 162 }
160 163
161 gfx::SizeF PageScaleAnimation::ViewportSizeAt(float interp) const { 164 gfx::SizeF PageScaleAnimation::ViewportSizeAt(float interp) const {
162 return gfx::ScaleSize(viewport_size_, 1.f / PageScaleFactorAt(interp)); 165 return gfx::ScaleSize(viewport_size_, 1.f / PageScaleFactorAt(interp));
163 } 166 }
164 167
165 bool PageScaleAnimation::IsAnimationStarted() const { 168 bool PageScaleAnimation::IsAnimationStarted() const {
166 return start_time_ >= 0; 169 return TimeUtil::TicksInSecondsF(start_time_) >= 0;
167 } 170 }
168 171
169 void PageScaleAnimation::StartAnimation(double time) { 172 void PageScaleAnimation::StartAnimation(base::TimeTicks time) {
170 DCHECK_GT(0, start_time_); 173 DCHECK_GT(0, TimeUtil::TicksInSecondsF(start_time_));
171 start_time_ = time; 174 start_time_ = time;
172 } 175 }
173 176
174 gfx::Vector2dF PageScaleAnimation::ScrollOffsetAtTime(double time) const { 177 gfx::Vector2dF PageScaleAnimation::ScrollOffsetAtTime(
175 DCHECK_GE(start_time_, 0); 178 base::TimeTicks time) const {
179 DCHECK_GE(TimeUtil::TicksInSecondsF(start_time_), 0);
176 return ScrollOffsetAt(InterpAtTime(time)); 180 return ScrollOffsetAt(InterpAtTime(time));
177 } 181 }
178 182
179 float PageScaleAnimation::PageScaleFactorAtTime(double time) const { 183 float PageScaleAnimation::PageScaleFactorAtTime(base::TimeTicks time) const {
180 DCHECK_GE(start_time_, 0); 184 DCHECK_GE(TimeUtil::TicksInSecondsF(start_time_), 0);
181 return PageScaleFactorAt(InterpAtTime(time)); 185 return PageScaleFactorAt(InterpAtTime(time));
182 } 186 }
183 187
184 bool PageScaleAnimation::IsAnimationCompleteAtTime(double time) const { 188 bool PageScaleAnimation::IsAnimationCompleteAtTime(base::TimeTicks time) const {
185 DCHECK_GE(start_time_, 0); 189 DCHECK_GE(TimeUtil::TicksInSecondsF(start_time_), 0);
186 return time >= end_time(); 190 return time >= end_time();
187 } 191 }
188 192
189 float PageScaleAnimation::InterpAtTime(double time) const { 193 float PageScaleAnimation::InterpAtTime(base::TimeTicks monotonic_time) const {
190 DCHECK_GE(start_time_, 0); 194 DCHECK_GE(TimeUtil::TicksInSecondsF(start_time_), 0);
191 DCHECK_GE(time, start_time_); 195 DCHECK_GE(TimeUtil::TicksInSecondsF(monotonic_time),
192 if (IsAnimationCompleteAtTime(time)) 196 TimeUtil::TicksInSecondsF(start_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