OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 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 "webkit/child/fling_animator_impl_android.h" | 5 #include "webkit/child/fling_animator_impl_android.h" |
6 | 6 |
7 #include "base/android/jni_android.h" | |
8 #include "base/android/scoped_java_ref.h" | |
9 #include "base/logging.h" | 7 #include "base/logging.h" |
10 #include "jni/OverScroller_jni.h" | |
11 #include "third_party/WebKit/public/platform/WebFloatSize.h" | 8 #include "third_party/WebKit/public/platform/WebFloatSize.h" |
12 #include "third_party/WebKit/public/platform/WebGestureCurveTarget.h" | 9 #include "third_party/WebKit/public/platform/WebGestureCurveTarget.h" |
13 #include "ui/gfx/screen.h" | 10 #include "ui/gfx/android/scroller.h" |
11 #include "ui/gfx/android/view_configuration.h" | |
14 #include "ui/gfx/vector2d.h" | 12 #include "ui/gfx/vector2d.h" |
15 | 13 |
16 namespace webkit_glue { | 14 namespace webkit_glue { |
17 | 15 |
18 namespace { | 16 namespace { |
19 static const float kEpsilon = 1e-4; | 17 |
18 gfx::Scroller::Config GetScrollerConfig() { | |
19 gfx::Scroller::Config config; | |
20 config.flywheel_enabled = false; | |
21 config.fling_friction = gfx::ViewConfiguration::GetScrollFriction(); | |
22 return config; | |
20 } | 23 } |
21 | 24 |
25 } // namespace | |
26 | |
22 FlingAnimatorImpl::FlingAnimatorImpl() | 27 FlingAnimatorImpl::FlingAnimatorImpl() |
23 : is_active_(false) { | 28 : is_active_(false), |
24 // hold the global reference of the Java objects. | 29 scroller_(new gfx::Scroller(GetScrollerConfig())) {} |
25 JNIEnv* env = base::android::AttachCurrentThread(); | |
26 java_scroller_.Reset(JNI_OverScroller::Java_OverScroller_ConstructorAWOS_ACC( | |
27 env, | |
28 base::android::GetApplicationContext())); | |
29 } | |
30 | 30 |
31 FlingAnimatorImpl::~FlingAnimatorImpl() | 31 FlingAnimatorImpl::~FlingAnimatorImpl() {} |
32 { | |
33 } | |
34 | 32 |
35 //static | 33 void FlingAnimatorImpl::StartFling(const gfx::PointF& velocity) { |
36 bool FlingAnimatorImpl::RegisterJni(JNIEnv* env) { | |
37 return JNI_OverScroller::RegisterNativesImpl(env); | |
38 } | |
39 | |
40 void FlingAnimatorImpl::StartFling(const gfx::PointF& velocity) | |
41 { | |
42 // No bounds on the fling. See http://webkit.org/b/96403 | 34 // No bounds on the fling. See http://webkit.org/b/96403 |
43 // Instead, use the largest possible bounds for minX/maxX/minY/maxY. The | 35 // Instead, use the largest possible bounds for minX/maxX/minY/maxY. The |
44 // compositor will ignore any attempt to scroll beyond the end of the page. | 36 // compositor will ignore any attempt to scroll beyond the end of the page. |
45 | 37 |
46 DCHECK(velocity.x() || velocity.y()); | 38 DCHECK(velocity.x() || velocity.y()); |
47 if (is_active_) | 39 if (is_active_) |
48 CancelFling(); | 40 CancelFling(); |
49 | 41 |
50 is_active_ = true; | 42 is_active_ = true; |
51 last_time_ = 0; | 43 scroller_->Fling(0, |
52 last_velocity_ = velocity; | 44 0, |
53 | 45 velocity.x(), |
54 JNIEnv* env = base::android::AttachCurrentThread(); | 46 velocity.y(), |
55 | 47 INT_MIN, |
56 // The OverScroller deceleration constants work in pixel space. DIP scaling | 48 INT_MAX, |
57 // will be performed in |apply()| on the generated fling updates. | 49 INT_MIN, |
58 float dpi_scale = gfx::Screen::GetNativeScreen()->GetPrimaryDisplay() | 50 INT_MAX, |
59 .device_scale_factor(); | 51 base::TimeTicks::Now()); |
60 JNI_OverScroller::Java_OverScroller_flingV_I_I_I_I_I_I_I_I( | 52 // TODO(jdduke): Use |base::TimeTicks()| upon resolution of crbug.com/345459. |
jamesr
2014/02/21 23:41:13
it looks like this code *is* using base::TimeTicks
jdduke (slow)
2014/02/22 00:06:57
It's using Now(), not an empty |base::TimeTicks()|
| |
61 env, java_scroller_.obj(), 0, 0, | |
62 static_cast<int>(velocity.x() * dpi_scale), | |
63 static_cast<int>(velocity.y() * dpi_scale), | |
64 INT_MIN, INT_MAX, INT_MIN, INT_MAX); | |
65 } | 53 } |
66 | 54 |
67 void FlingAnimatorImpl::CancelFling() | 55 void FlingAnimatorImpl::CancelFling() { |
68 { | |
69 if (!is_active_) | 56 if (!is_active_) |
70 return; | 57 return; |
71 | 58 |
72 is_active_ = false; | 59 is_active_ = false; |
73 JNIEnv* env = base::android::AttachCurrentThread(); | 60 scroller_->AbortAnimation(); |
74 JNI_OverScroller::Java_OverScroller_abortAnimation(env, java_scroller_.obj()); | |
75 } | |
76 | |
77 bool FlingAnimatorImpl::UpdatePosition() | |
78 { | |
79 JNIEnv* env = base::android::AttachCurrentThread(); | |
80 bool result = JNI_OverScroller::Java_OverScroller_computeScrollOffset( | |
81 env, | |
82 java_scroller_.obj()); | |
83 return is_active_ = result; | |
84 } | |
85 | |
86 gfx::Point FlingAnimatorImpl::GetCurrentPosition() | |
87 { | |
88 JNIEnv* env = base::android::AttachCurrentThread(); | |
89 gfx::Point position( | |
90 JNI_OverScroller::Java_OverScroller_getCurrX(env, java_scroller_.obj()), | |
91 JNI_OverScroller::Java_OverScroller_getCurrY(env, java_scroller_.obj())); | |
92 return position; | |
93 } | |
94 | |
95 float FlingAnimatorImpl::GetCurrentVelocity() | |
96 { | |
97 JNIEnv* env = base::android::AttachCurrentThread(); | |
98 // TODO(jdduke): Add Java-side hooks for getCurrVelocityX/Y, and return | |
99 // vector velocity. | |
100 return JNI_OverScroller::Java_OverScroller_getCurrVelocity( | |
101 env, java_scroller_.obj()); | |
102 } | 61 } |
103 | 62 |
104 bool FlingAnimatorImpl::apply(double time, | 63 bool FlingAnimatorImpl::apply(double time, |
105 blink::WebGestureCurveTarget* target) { | 64 blink::WebGestureCurveTarget* target) { |
106 if (!UpdatePosition()) | 65 // Historically, Android's Scroller used |currentAnimationTimeMillis()|, |
66 // which is equivalent to TimeTicks::Now(). In practice, this produces | |
67 // smoother results than using |time|, so we continue using TimeTicks::Now(). | |
68 // TODO(jdduke): Use |time| upon resolution of crbug.com/345459. | |
jamesr
2014/02/21 23:41:13
this comment seems to say the opposite of the earl
jdduke (slow)
2014/02/22 00:06:57
We can either zero-initialize the fling start time
| |
69 base::TimeTicks timeticks = base::TimeTicks::Now(); | |
jamesr
2014/02/21 23:41:13
why not gfx::FrameTime ?
jdduke (slow)
2014/02/22 00:06:57
Done.
| |
70 if (!scroller_->ComputeScrollOffset(timeticks)) { | |
71 is_active_ = false; | |
107 return false; | 72 return false; |
73 } | |
108 | 74 |
109 gfx::Point current_position = GetCurrentPosition(); | 75 target->notifyCurrentFlingVelocity(blink::WebFloatSize( |
110 gfx::Vector2d diff(current_position - last_position_); | 76 scroller_->GetCurrVelocityX(), scroller_->GetCurrVelocityY())); |
77 | |
78 gfx::PointF current_position(scroller_->GetCurrX(), scroller_->GetCurrY()); | |
79 gfx::Vector2dF scroll_amount(current_position - last_position_); | |
111 last_position_ = current_position; | 80 last_position_ = current_position; |
112 float dpi_scale = gfx::Screen::GetNativeScreen()->GetPrimaryDisplay() | |
113 .device_scale_factor(); | |
114 blink::WebFloatSize scroll_amount(diff.x() / dpi_scale, | |
115 diff.y() / dpi_scale); | |
116 | |
117 float delta_time = time - last_time_; | |
118 last_time_ = time; | |
119 | |
120 // Currently, the OverScroller only provides the velocity magnitude; use the | |
121 // angle of the scroll delta to yield approximate x and y velocity components. | |
122 // TODO(jdduke): Remove this when we can properly poll OverScroller velocity. | |
123 gfx::PointF current_velocity = last_velocity_; | |
124 if (delta_time > kEpsilon) { | |
125 float diff_length = diff.Length(); | |
126 if (diff_length > kEpsilon) { | |
127 float velocity = GetCurrentVelocity(); | |
128 float scroll_to_velocity = velocity / diff_length; | |
129 current_velocity = gfx::PointF(diff.x() * scroll_to_velocity, | |
130 diff.y() * scroll_to_velocity); | |
131 } | |
132 } | |
133 last_velocity_ = current_velocity; | |
134 blink::WebFloatSize fling_velocity(current_velocity.x() / dpi_scale, | |
135 current_velocity.y() / dpi_scale); | |
136 target->notifyCurrentFlingVelocity(fling_velocity); | |
137 | 81 |
138 // scrollBy() could delete this curve if the animation is over, so don't touch | 82 // scrollBy() could delete this curve if the animation is over, so don't touch |
139 // any member variables after making that call. | 83 // any member variables after making that call. |
140 target->scrollBy(scroll_amount); | 84 target->scrollBy(blink::WebFloatSize(scroll_amount)); |
141 return true; | 85 return true; |
142 } | 86 } |
143 | 87 |
144 FlingAnimatorImpl* FlingAnimatorImpl::CreateAndroidGestureCurve( | 88 FlingAnimatorImpl* FlingAnimatorImpl::CreateAndroidGestureCurve( |
145 const blink::WebFloatPoint& velocity, | 89 const blink::WebFloatPoint& velocity, |
146 const blink::WebSize&) { | 90 const blink::WebSize&) { |
147 FlingAnimatorImpl* gesture_curve = new FlingAnimatorImpl(); | 91 FlingAnimatorImpl* gesture_curve = new FlingAnimatorImpl(); |
148 gesture_curve->StartFling(velocity); | 92 gesture_curve->StartFling(velocity); |
149 return gesture_curve; | 93 return gesture_curve; |
150 } | 94 } |
151 | 95 |
152 } // namespace webkit_glue | 96 } // namespace webkit_glue |
OLD | NEW |