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 "content/browser/renderer_host/input/fling/fling_curve_impl_android.h" |
6 | 6 |
7 #include "base/android/jni_android.h" | 7 #include "base/android/jni_android.h" |
8 #include "base/android/scoped_java_ref.h" | 8 #include "base/android/scoped_java_ref.h" |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 #include "jni/OverScroller_jni.h" | 10 #include "jni/OverScroller_jni.h" |
11 #include "third_party/WebKit/public/platform/WebFloatSize.h" | |
12 #include "third_party/WebKit/public/platform/WebGestureCurveTarget.h" | |
13 #include "ui/gfx/screen.h" | 11 #include "ui/gfx/screen.h" |
14 #include "ui/gfx/vector2d.h" | 12 #include "ui/gfx/vector2d.h" |
15 | 13 |
16 namespace webkit_glue { | 14 namespace content { |
17 | 15 |
18 namespace { | 16 namespace { |
19 static const float kEpsilon = 1e-4; | 17 static const float kEpsilon = 1e-4; |
20 } | 18 } |
21 | 19 |
22 FlingAnimatorImpl::FlingAnimatorImpl() | 20 FlingCurveImplAndroid::FlingCurveImplAndroid() |
23 : is_active_(false) { | 21 : is_active_(false) { |
24 // hold the global reference of the Java objects. | 22 // hold the global reference of the Java objects. |
25 JNIEnv* env = base::android::AttachCurrentThread(); | 23 JNIEnv* env = base::android::AttachCurrentThread(); |
26 java_scroller_.Reset(JNI_OverScroller::Java_OverScroller_ConstructorAWOS_ACC( | 24 java_scroller_.Reset(JNI_OverScroller::Java_OverScroller_ConstructorAWOS_ACC( |
27 env, | 25 env, |
28 base::android::GetApplicationContext())); | 26 base::android::GetApplicationContext())); |
29 } | 27 } |
30 | 28 |
31 FlingAnimatorImpl::~FlingAnimatorImpl() | 29 FlingCurveImplAndroid::~FlingCurveImplAndroid() |
32 { | 30 { |
33 } | 31 } |
34 | 32 |
35 //static | 33 //static |
36 bool FlingAnimatorImpl::RegisterJni(JNIEnv* env) { | 34 bool FlingCurveImplAndroid::RegisterJni(JNIEnv* env) { |
37 return JNI_OverScroller::RegisterNativesImpl(env); | 35 return JNI_OverScroller::RegisterNativesImpl(env); |
38 } | 36 } |
39 | 37 |
40 void FlingAnimatorImpl::StartFling(const gfx::PointF& velocity) | 38 void FlingCurveImplAndroid::StartFling(const gfx::PointF& velocity) |
41 { | 39 { |
42 // No bounds on the fling. See http://webkit.org/b/96403 | 40 // No bounds on the fling. See http://webkit.org/b/96403 |
43 // Instead, use the largest possible bounds for minX/maxX/minY/maxY. The | 41 // 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. | 42 // compositor will ignore any attempt to scroll beyond the end of the page. |
45 | 43 |
46 DCHECK(velocity.x() || velocity.y()); | 44 DCHECK(velocity.x() || velocity.y()); |
47 if (is_active_) | 45 if (is_active_) |
48 CancelFling(); | 46 CancelFling(); |
49 | 47 |
50 is_active_ = true; | 48 is_active_ = true; |
51 last_time_ = 0; | 49 last_time_ = 0; |
52 last_velocity_ = velocity; | 50 last_velocity_ = velocity; |
53 | 51 |
54 JNIEnv* env = base::android::AttachCurrentThread(); | 52 JNIEnv* env = base::android::AttachCurrentThread(); |
55 | 53 |
56 JNI_OverScroller::Java_OverScroller_flingV_I_I_I_I_I_I_I_I( | 54 JNI_OverScroller::Java_OverScroller_flingV_I_I_I_I_I_I_I_I( |
57 env, java_scroller_.obj(), 0, 0, | 55 env, java_scroller_.obj(), 0, 0, |
58 static_cast<int>(velocity.x()), | 56 static_cast<int>(velocity.x()), |
59 static_cast<int>(velocity.y()), | 57 static_cast<int>(velocity.y()), |
60 INT_MIN, INT_MAX, INT_MIN, INT_MAX); | 58 INT_MIN, INT_MAX, INT_MIN, INT_MAX); |
61 } | 59 } |
62 | 60 |
63 void FlingAnimatorImpl::CancelFling() | 61 void FlingCurveImplAndroid::CancelFling() |
64 { | 62 { |
65 if (!is_active_) | 63 if (!is_active_) |
66 return; | 64 return; |
67 | 65 |
68 is_active_ = false; | 66 is_active_ = false; |
69 JNIEnv* env = base::android::AttachCurrentThread(); | 67 JNIEnv* env = base::android::AttachCurrentThread(); |
70 JNI_OverScroller::Java_OverScroller_abortAnimation(env, java_scroller_.obj()); | 68 JNI_OverScroller::Java_OverScroller_abortAnimation(env, java_scroller_.obj()); |
71 } | 69 } |
72 | 70 |
73 bool FlingAnimatorImpl::UpdatePosition() | 71 bool FlingCurveImplAndroid::UpdatePosition() |
74 { | 72 { |
75 JNIEnv* env = base::android::AttachCurrentThread(); | 73 JNIEnv* env = base::android::AttachCurrentThread(); |
76 bool result = JNI_OverScroller::Java_OverScroller_computeScrollOffset( | 74 bool result = JNI_OverScroller::Java_OverScroller_computeScrollOffset( |
77 env, | 75 env, |
78 java_scroller_.obj()); | 76 java_scroller_.obj()); |
79 return is_active_ = result; | 77 return is_active_ = result; |
80 } | 78 } |
81 | 79 |
82 gfx::Point FlingAnimatorImpl::GetCurrentPosition() | 80 gfx::Point FlingCurveImplAndroid::GetCurrentPosition() |
83 { | 81 { |
84 JNIEnv* env = base::android::AttachCurrentThread(); | 82 JNIEnv* env = base::android::AttachCurrentThread(); |
85 gfx::Point position( | 83 gfx::Point position( |
86 JNI_OverScroller::Java_OverScroller_getCurrX(env, java_scroller_.obj()), | 84 JNI_OverScroller::Java_OverScroller_getCurrX(env, java_scroller_.obj()), |
87 JNI_OverScroller::Java_OverScroller_getCurrY(env, java_scroller_.obj())); | 85 JNI_OverScroller::Java_OverScroller_getCurrY(env, java_scroller_.obj())); |
88 return position; | 86 return position; |
89 } | 87 } |
90 | 88 |
91 float FlingAnimatorImpl::GetCurrentVelocity() | 89 float FlingCurveImplAndroid::GetCurrentVelocity() |
92 { | 90 { |
93 JNIEnv* env = base::android::AttachCurrentThread(); | 91 JNIEnv* env = base::android::AttachCurrentThread(); |
94 // TODO(jdduke): Add Java-side hooks for getCurrVelocityX/Y, and return | 92 // TODO(jdduke): Add Java-side hooks for getCurrVelocityX/Y, and return |
95 // vector velocity. | 93 // vector velocity. |
96 return JNI_OverScroller::Java_OverScroller_getCurrVelocity( | 94 return JNI_OverScroller::Java_OverScroller_getCurrVelocity( |
97 env, java_scroller_.obj()); | 95 env, java_scroller_.obj()); |
98 } | 96 } |
99 | 97 |
100 bool FlingAnimatorImpl::apply(double time, | 98 bool FlingCurveImplAndroid::Apply(double time_in_secs, |
101 blink::WebGestureCurveTarget* target) { | 99 FlingCurveTarget* target) { |
102 if (!UpdatePosition()) | 100 if (!UpdatePosition()) |
103 return false; | 101 return false; |
104 | 102 |
105 gfx::Point current_position = GetCurrentPosition(); | 103 gfx::Point current_position = GetCurrentPosition(); |
106 gfx::Vector2d diff(current_position - last_position_); | 104 gfx::Vector2d diff(current_position - last_position_); |
107 last_position_ = current_position; | 105 last_position_ = current_position; |
108 float dpi_scale = gfx::Screen::GetNativeScreen()->GetPrimaryDisplay() | 106 float dpi_scale = gfx::Screen::GetNativeScreen()->GetPrimaryDisplay() |
109 .device_scale_factor(); | 107 .device_scale_factor(); |
110 blink::WebFloatSize scroll_amount(diff.x() / dpi_scale, | 108 gfx::PointF scroll_amount(diff.x() / dpi_scale, |
111 diff.y() / dpi_scale); | 109 diff.y() / dpi_scale); |
112 | 110 |
113 float delta_time = time - last_time_; | 111 float delta_time = time_in_secs - last_time_; |
114 last_time_ = time; | 112 last_time_ = time_in_secs; |
115 | 113 |
116 // Currently, the OverScroller only provides the velocity magnitude; use the | 114 // Currently, the OverScroller only provides the velocity magnitude; use the |
117 // angle of the scroll delta to yield approximate x and y velocity components. | 115 // angle of the scroll delta to yield approximate x and y velocity components. |
118 // TODO(jdduke): Remove this when we can properly poll OverScroller velocity. | 116 // TODO(jdduke): Remove this when we can properly poll OverScroller velocity. |
119 gfx::PointF current_velocity = last_velocity_; | 117 gfx::PointF current_velocity = last_velocity_; |
120 if (delta_time > kEpsilon) { | 118 if (delta_time > kEpsilon) { |
121 float diff_length = diff.Length(); | 119 float diff_length = diff.Length(); |
122 if (diff_length > kEpsilon) { | 120 if (diff_length > kEpsilon) { |
123 float velocity = GetCurrentVelocity(); | 121 float velocity = GetCurrentVelocity(); |
124 float scroll_to_velocity = velocity / diff_length; | 122 float scroll_to_velocity = velocity / diff_length; |
125 current_velocity = gfx::PointF(diff.x() * scroll_to_velocity, | 123 current_velocity = gfx::PointF(diff.x() * scroll_to_velocity, |
126 diff.y() * scroll_to_velocity); | 124 diff.y() * scroll_to_velocity); |
127 } | 125 } |
128 } | 126 } |
129 last_velocity_ = current_velocity; | 127 last_velocity_ = current_velocity; |
130 blink::WebFloatSize fling_velocity(current_velocity.x() / dpi_scale, | 128 gfx::PointF fling_velocity(current_velocity.x() / dpi_scale, |
131 current_velocity.y() / dpi_scale); | 129 current_velocity.y() / dpi_scale); |
132 target->notifyCurrentFlingVelocity(fling_velocity); | 130 target->NotifyCurrentFlingVelocity(fling_velocity); |
133 | 131 |
134 // scrollBy() could delete this curve if the animation is over, so don't touch | 132 // scrollBy() could delete this curve if the animation is over, so don't touch |
135 // any member variables after making that call. | 133 // any member variables after making that call. |
136 target->scrollBy(scroll_amount); | 134 target->ScrollBy(scroll_amount); |
137 return true; | 135 return true; |
138 } | 136 } |
139 | 137 |
140 FlingAnimatorImpl* FlingAnimatorImpl::CreateAndroidGestureCurve( | 138 // static |
141 const blink::WebFloatPoint& velocity, | 139 FlingCurve* FlingCurve::Create(blink::WebGestureEvent::SourceDevice source, |
142 const blink::WebSize&) { | 140 const gfx::PointF& velocity, |
143 FlingAnimatorImpl* gesture_curve = new FlingAnimatorImpl(); | 141 const gfx::Point& cumulative_scroll) { |
| 142 FlingCurveImplAndroid* gesture_curve = new FlingCurveImplAndroid(); |
144 gesture_curve->StartFling(velocity); | 143 gesture_curve->StartFling(velocity); |
145 return gesture_curve; | 144 return gesture_curve; |
146 } | 145 } |
147 | 146 |
148 } // namespace webkit_glue | 147 } // namespace content |
OLD | NEW |