| 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 48 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
|   59     const gfx::SizeF& viewport_size, |   59     const gfx::SizeF& viewport_size, | 
|   60     const gfx::SizeF& root_layer_size, |   60     const gfx::SizeF& root_layer_size, | 
|   61     scoped_ptr<TimingFunction> timing_function) |   61     scoped_ptr<TimingFunction> timing_function) | 
|   62     : start_page_scale_factor_(start_page_scale_factor), |   62     : start_page_scale_factor_(start_page_scale_factor), | 
|   63       target_page_scale_factor_(0.f), |   63       target_page_scale_factor_(0.f), | 
|   64       start_scroll_offset_(start_scroll_offset), |   64       start_scroll_offset_(start_scroll_offset), | 
|   65       start_anchor_(), |   65       start_anchor_(), | 
|   66       target_anchor_(), |   66       target_anchor_(), | 
|   67       viewport_size_(viewport_size), |   67       viewport_size_(viewport_size), | 
|   68       root_layer_size_(root_layer_size), |   68       root_layer_size_(root_layer_size), | 
|   69       start_time_(-1.0), |   69       start_time_(TimeTicks::FromInternalValue( | 
|   70       duration_(0.0), |   70           -1.0 * base::Time::kMicrosecondsPerSecond)), | 
|   71       timing_function_(timing_function.Pass()) {} |   71       duration_(TimeDelta()), | 
 |   72       timing_function_(timing_function.Pass()) { | 
 |   73 } | 
|   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 TimeTicks::InSecondsF(start_time_) >= 0; | 
|  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_GT(0, TimeTicks::InSecondsF(start_time_)); | 
|  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_GE(TimeTicks::InSecondsF(start_time_), 0); | 
|  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_GE(TimeTicks::InSecondsF(start_time_), 0); | 
|  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_GE(TimeTicks::InSecondsF(start_time_), 0); | 
|  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_GE(TimeTicks::InSecondsF(start_time_), 0); | 
|  191   DCHECK_GE(time, start_time_); |  194   DCHECK_GE(TimeTicks::InSecondsF(monotonic_time), | 
|  192   if (IsAnimationCompleteAtTime(time)) |  195             TimeTicks::InSecondsF(start_time_)); | 
 |  196   if (IsAnimationCompleteAtTime(monotonic_time)) | 
|  193     return 1.f; |  197     return 1.f; | 
|  194  |  198   const double normalized_time = | 
|  195   const double normalized_time = (time - start_time_) / duration_; |  199       (monotonic_time - start_time_).InSecondsF() / duration_.InSecondsF(); | 
|  196   return timing_function_->GetValue(normalized_time); |  200   return timing_function_->GetValue(normalized_time); | 
|  197 } |  201 } | 
|  198  |  202  | 
|  199 gfx::Vector2dF PageScaleAnimation::ScrollOffsetAt(float interp) const { |  203 gfx::Vector2dF PageScaleAnimation::ScrollOffsetAt(float interp) const { | 
|  200   if (interp <= 0.f) |  204   if (interp <= 0.f) | 
|  201     return start_scroll_offset_; |  205     return start_scroll_offset_; | 
|  202   if (interp >= 1.f) |  206   if (interp >= 1.f) | 
|  203     return target_scroll_offset_; |  207     return target_scroll_offset_; | 
|  204  |  208  | 
|  205   return AnchorAt(interp) - ViewportRelativeAnchorAt(interp); |  209   return AnchorAt(interp) - ViewportRelativeAnchorAt(interp); | 
| (...skipping 27 matching lines...) Expand all  Loading... | 
|  233  |  237  | 
|  234   // Linearly interpolate the magnitude in log scale. |  238   // Linearly interpolate the magnitude in log scale. | 
|  235   float diff = target_page_scale_factor_ / start_page_scale_factor_; |  239   float diff = target_page_scale_factor_ / start_page_scale_factor_; | 
|  236   float log_diff = log(diff); |  240   float log_diff = log(diff); | 
|  237   log_diff *= interp; |  241   log_diff *= interp; | 
|  238   diff = exp(log_diff); |  242   diff = exp(log_diff); | 
|  239   return start_page_scale_factor_ * diff; |  243   return start_page_scale_factor_ * diff; | 
|  240 } |  244 } | 
|  241  |  245  | 
|  242 }  // namespace cc |  246 }  // namespace cc | 
| OLD | NEW |