Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / webkit / child / fling_animator_impl_android.cc
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.
4
5 #include "webkit/child/fling_animator_impl_android.h"
6
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"
15
16 namespace webkit_glue {
17
18 namespace {
19 static const float kEpsilon = 1e-4;
20 }
21
22 FlingAnimatorImpl::FlingAnimatorImpl()
23     : is_active_(false) {
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(
27       env,
28       base::android::GetApplicationContext()));
29 }
30
31 FlingAnimatorImpl::~FlingAnimatorImpl()
32 {
33 }
34
35 //static
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
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.
45
46   DCHECK(velocity.x() || velocity.y());
47   if (is_active_)
48     CancelFling();
49
50   is_active_ = true;
51   last_time_ = 0;
52   last_velocity_ = velocity;
53
54   JNIEnv* env = base::android::AttachCurrentThread();
55
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);
65 }
66
67 void FlingAnimatorImpl::CancelFling()
68 {
69   if (!is_active_)
70     return;
71
72   is_active_ = false;
73   JNIEnv* env = base::android::AttachCurrentThread();
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 }
103
104 bool FlingAnimatorImpl::apply(double time,
105                               blink::WebGestureCurveTarget* target) {
106   if (!UpdatePosition())
107     return false;
108
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);
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
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);
141   return true;
142 }
143
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;
150 }
151
152 } // namespace webkit_glue