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