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
3 // found in the LICENSE file.
5 #include "webkit/child/fling_animator_impl_android.h"
7 #include "base/android/jni_android.h"
8 #include "base/android/scoped_java_ref.h"
9 #include "base/logging.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"
14 #include "ui/gfx/vector2d.h"
16 namespace webkit_glue {
19 static const float kEpsilon = 1e-4;
22 FlingAnimatorImpl::FlingAnimatorImpl()
24 // hold the global reference of the Java objects.
25 JNIEnv* env = base::android::AttachCurrentThread();
26 java_scroller_.Reset(JNI_OverScroller::Java_OverScroller_ConstructorAWOS_ACC(
28 base::android::GetApplicationContext()));
31 FlingAnimatorImpl::~FlingAnimatorImpl()
36 bool FlingAnimatorImpl::RegisterJni(JNIEnv* env) {
37 return JNI_OverScroller::RegisterNativesImpl(env);
40 void FlingAnimatorImpl::StartFling(const gfx::PointF& velocity)
42 // No bounds on the fling. See http://webkit.org/b/96403
43 // 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.
46 DCHECK(velocity.x() || velocity.y());
52 last_velocity_ = velocity;
54 JNIEnv* env = base::android::AttachCurrentThread();
56 // The OverScroller deceleration constants work in pixel space. DIP scaling
57 // will be performed in |apply()| on the generated fling updates.
58 float dpi_scale = gfx::Screen::GetNativeScreen()->GetPrimaryDisplay()
59 .device_scale_factor();
60 JNI_OverScroller::Java_OverScroller_flingV_I_I_I_I_I_I_I_I(
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);
67 void FlingAnimatorImpl::CancelFling()
73 JNIEnv* env = base::android::AttachCurrentThread();
74 JNI_OverScroller::Java_OverScroller_abortAnimation(env, java_scroller_.obj());
77 bool FlingAnimatorImpl::UpdatePosition()
79 JNIEnv* env = base::android::AttachCurrentThread();
80 bool result = JNI_OverScroller::Java_OverScroller_computeScrollOffset(
82 java_scroller_.obj());
83 return is_active_ = result;
86 gfx::Point FlingAnimatorImpl::GetCurrentPosition()
88 JNIEnv* env = base::android::AttachCurrentThread();
90 JNI_OverScroller::Java_OverScroller_getCurrX(env, java_scroller_.obj()),
91 JNI_OverScroller::Java_OverScroller_getCurrY(env, java_scroller_.obj()));
95 float FlingAnimatorImpl::GetCurrentVelocity()
97 JNIEnv* env = base::android::AttachCurrentThread();
98 // TODO(jdduke): Add Java-side hooks for getCurrVelocityX/Y, and return
100 return JNI_OverScroller::Java_OverScroller_getCurrVelocity(
101 env, java_scroller_.obj());
104 bool FlingAnimatorImpl::apply(double time,
105 blink::WebGestureCurveTarget* target) {
106 if (!UpdatePosition())
109 gfx::Point current_position = GetCurrentPosition();
110 gfx::Vector2d diff(current_position - last_position_);
111 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);
117 float delta_time = time - last_time_;
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);
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);
138 // scrollBy() could delete this curve if the animation is over, so don't touch
139 // any member variables after making that call.
140 target->scrollBy(scroll_amount);
144 FlingAnimatorImpl* FlingAnimatorImpl::CreateAndroidGestureCurve(
145 const blink::WebFloatPoint& velocity,
146 const blink::WebSize&) {
147 FlingAnimatorImpl* gesture_curve = new FlingAnimatorImpl();
148 gesture_curve->StartFling(velocity);
149 return gesture_curve;
152 } // namespace webkit_glue