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

Side by Side Diff: cc/animation/scroll_offset_animation_curve.cc

Issue 393713002: Scroll offset animation curve retargeting. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Possible workaround for clang bug. Created 6 years, 5 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 | Annotate | Revision Log
OLDNEW
1 // Copyright 2013 The Chromium Authors. All rights reserved. 1 // Copyright 2013 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/animation/scroll_offset_animation_curve.h" 5 #include "cc/animation/scroll_offset_animation_curve.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 #include <cmath> 8 #include <cmath>
9 9
10 #include "base/logging.h" 10 #include "base/logging.h"
11 #include "cc/animation/timing_function.h" 11 #include "cc/animation/timing_function.h"
12 #include "ui/gfx/animation/tween.h" 12 #include "ui/gfx/animation/tween.h"
13 13
14 const double kDurationDivisor = 60.0; 14 const double kDurationDivisor = 60.0;
15 15
16 namespace cc { 16 namespace cc {
17 17
18 namespace {
19
20 #if __clang__
21 // http://b/16376945
22 static inline float AbsClangWorkaround(float f) {
23 return f < 0.0f ? -f : f;
24 }
25 #define ABS AbsClangWorkaround
26 #else
27 #define ABS std::abs
28 #endif
ajuma 2014/07/18 01:18:51 Rather than adding this #if, let's just use the wo
skobes 2014/07/18 16:48:02 The offending compiler optimization has been disab
29
30 static float MaximumDimension(const gfx::Vector2dF& delta) {
31 return std::max(ABS(delta.x()), ABS(delta.y()));
32 }
33
34 static base::TimeDelta DurationFromDelta(const gfx::Vector2dF& delta) {
35 // The duration of a scroll animation depends on the size of the scroll.
36 // The exact relationship between the size and the duration isn't specified
37 // by the CSSOM View smooth scroll spec and is instead left up to user agents
38 // to decide. The calculation performed here will very likely be further
39 // tweaked before the smooth scroll API ships.
40 return base::TimeDelta::FromMicroseconds(
41 (std::sqrt(MaximumDimension(delta)) / kDurationDivisor) *
42 base::Time::kMicrosecondsPerSecond);
43 }
44
45 static scoped_ptr<TimingFunction> EaseOutWithInitialVelocity(double velocity) {
46 // Based on EaseInOutTimingFunction::Create with first control point rotated.
47 const double r2 = 0.42 * 0.42;
48 const double v2 = velocity * velocity;
49 const double x1 = std::sqrt(r2 / (v2 + 1));
50 const double y1 = std::sqrt(r2 * v2 / (v2 + 1));
51 return CubicBezierTimingFunction::Create(x1, y1, 0.58, 1)
52 .PassAs<TimingFunction>();
53 }
54
55 } // namespace
56
18 scoped_ptr<ScrollOffsetAnimationCurve> ScrollOffsetAnimationCurve::Create( 57 scoped_ptr<ScrollOffsetAnimationCurve> ScrollOffsetAnimationCurve::Create(
19 const gfx::Vector2dF& target_value, 58 const gfx::Vector2dF& target_value,
20 scoped_ptr<TimingFunction> timing_function) { 59 scoped_ptr<TimingFunction> timing_function) {
21 return make_scoped_ptr( 60 return make_scoped_ptr(
22 new ScrollOffsetAnimationCurve(target_value, timing_function.Pass())); 61 new ScrollOffsetAnimationCurve(target_value, timing_function.Pass()));
23 } 62 }
24 63
25 ScrollOffsetAnimationCurve::ScrollOffsetAnimationCurve( 64 ScrollOffsetAnimationCurve::ScrollOffsetAnimationCurve(
26 const gfx::Vector2dF& target_value, 65 const gfx::Vector2dF& target_value,
27 scoped_ptr<TimingFunction> timing_function) 66 scoped_ptr<TimingFunction> timing_function)
28 : target_value_(target_value), timing_function_(timing_function.Pass()) { 67 : target_value_(target_value), timing_function_(timing_function.Pass()) {
29 } 68 }
30 69
31 ScrollOffsetAnimationCurve::~ScrollOffsetAnimationCurve() {} 70 ScrollOffsetAnimationCurve::~ScrollOffsetAnimationCurve() {}
32 71
33 void ScrollOffsetAnimationCurve::SetInitialValue( 72 void ScrollOffsetAnimationCurve::SetInitialValue(
34 const gfx::Vector2dF& initial_value) { 73 const gfx::Vector2dF& initial_value) {
35 initial_value_ = initial_value; 74 initial_value_ = initial_value;
36 75 total_animation_duration_ = DurationFromDelta(target_value_ - initial_value_);
37 // The duration of a scroll animation depends on the size of the scroll.
38 // The exact relationship between the size and the duration isn't specified
39 // by the CSSOM View smooth scroll spec and is instead left up to user agents
40 // to decide. The calculation performed here will very likely be further
41 // tweaked before the smooth scroll API ships.
42 float delta_x = std::abs(target_value_.x() - initial_value_.x());
43 float delta_y = std::abs(target_value_.y() - initial_value_.y());
44 float max_delta = std::max(delta_x, delta_y);
45 duration_ = base::TimeDelta::FromMicroseconds(
46 (std::sqrt(max_delta) / kDurationDivisor) *
47 base::Time::kMicrosecondsPerSecond);
48 } 76 }
49 77
50 gfx::Vector2dF ScrollOffsetAnimationCurve::GetValue(double t) const { 78 gfx::Vector2dF ScrollOffsetAnimationCurve::GetValue(double t) const {
51 double duration = duration_.InSecondsF(); 79 double duration = (total_animation_duration_ - last_retarget_).InSecondsF();
80 t -= last_retarget_.InSecondsF();
52 81
53 if (t <= 0) 82 if (t <= 0)
54 return initial_value_; 83 return initial_value_;
55 84
56 if (t >= duration) 85 if (t >= duration)
57 return target_value_; 86 return target_value_;
58 87
59 double progress = (timing_function_->GetValue(t / duration)); 88 double progress = (timing_function_->GetValue(t / duration));
60 return gfx::Vector2dF(gfx::Tween::FloatValueBetween( 89 return gfx::Vector2dF(gfx::Tween::FloatValueBetween(
61 progress, initial_value_.x(), target_value_.x()), 90 progress, initial_value_.x(), target_value_.x()),
62 gfx::Tween::FloatValueBetween( 91 gfx::Tween::FloatValueBetween(
63 progress, initial_value_.y(), target_value_.y())); 92 progress, initial_value_.y(), target_value_.y()));
64 } 93 }
65 94
66 double ScrollOffsetAnimationCurve::Duration() const { 95 double ScrollOffsetAnimationCurve::Duration() const {
67 return duration_.InSecondsF(); 96 return total_animation_duration_.InSecondsF();
68 } 97 }
69 98
70 AnimationCurve::CurveType ScrollOffsetAnimationCurve::Type() const { 99 AnimationCurve::CurveType ScrollOffsetAnimationCurve::Type() const {
71 return ScrollOffset; 100 return ScrollOffset;
72 } 101 }
73 102
74 scoped_ptr<AnimationCurve> ScrollOffsetAnimationCurve::Clone() const { 103 scoped_ptr<AnimationCurve> ScrollOffsetAnimationCurve::Clone() const {
75 scoped_ptr<TimingFunction> timing_function( 104 scoped_ptr<TimingFunction> timing_function(
76 static_cast<TimingFunction*>(timing_function_->Clone().release())); 105 static_cast<TimingFunction*>(timing_function_->Clone().release()));
77 scoped_ptr<ScrollOffsetAnimationCurve> curve_clone = 106 scoped_ptr<ScrollOffsetAnimationCurve> curve_clone =
78 Create(target_value_, timing_function.Pass()); 107 Create(target_value_, timing_function.Pass());
79 curve_clone->initial_value_ = initial_value_; 108 curve_clone->initial_value_ = initial_value_;
80 curve_clone->duration_ = duration_; 109 curve_clone->total_animation_duration_ = total_animation_duration_;
110 curve_clone->last_retarget_ = last_retarget_;
81 return curve_clone.PassAs<AnimationCurve>(); 111 return curve_clone.PassAs<AnimationCurve>();
82 } 112 }
83 113
114 void ScrollOffsetAnimationCurve::UpdateTarget(
115 double t,
116 const gfx::Vector2dF& new_target) {
117 gfx::Vector2dF current_position = GetValue(t);
118 gfx::Vector2dF old_delta = target_value_ - initial_value_;
119 gfx::Vector2dF new_delta = new_target - current_position;
120
121 double old_duration =
122 (total_animation_duration_ - last_retarget_).InSecondsF();
123 double new_duration = DurationFromDelta(new_delta).InSecondsF();
124
125 double old_velocity = timing_function_->Velocity(
126 (t - last_retarget_.InSecondsF()) / old_duration);
127
128 // TimingFunction::Velocity gives the slope of the curve from 0 to 1.
129 // To match the "true" velocity in px/sec we must adjust this slope for
130 // differences in duration and scroll delta between old and new curves.
131 double new_velocity =
132 old_velocity * (new_duration / old_duration) *
133 (MaximumDimension(old_delta) / MaximumDimension(new_delta));
134
135 initial_value_ = current_position;
136 target_value_ = new_target;
137 total_animation_duration_ = base::TimeDelta::FromSecondsD(t + new_duration);
138 last_retarget_ = base::TimeDelta::FromSecondsD(t);
139 timing_function_ = EaseOutWithInitialVelocity(new_velocity);
140 }
141
84 } // namespace cc 142 } // namespace cc
OLDNEW
« no previous file with comments | « cc/animation/scroll_offset_animation_curve.h ('k') | cc/animation/scroll_offset_animation_curve_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698