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" | 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" |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
46 DCHECK(velocity.x() || velocity.y()); | 46 DCHECK(velocity.x() || velocity.y()); |
47 if (is_active_) | 47 if (is_active_) |
48 CancelFling(); | 48 CancelFling(); |
49 | 49 |
50 is_active_ = true; | 50 is_active_ = true; |
51 last_time_ = 0; | 51 last_time_ = 0; |
52 last_velocity_ = velocity; | 52 last_velocity_ = velocity; |
53 | 53 |
54 JNIEnv* env = base::android::AttachCurrentThread(); | 54 JNIEnv* env = base::android::AttachCurrentThread(); |
55 | 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(); |
56 JNI_OverScroller::Java_OverScroller_flingV_I_I_I_I_I_I_I_I( | 60 JNI_OverScroller::Java_OverScroller_flingV_I_I_I_I_I_I_I_I( |
57 env, java_scroller_.obj(), 0, 0, | 61 env, java_scroller_.obj(), 0, 0, |
58 static_cast<int>(velocity.x()), | 62 static_cast<int>(velocity.x() * dpi_scale), |
59 static_cast<int>(velocity.y()), | 63 static_cast<int>(velocity.y() * dpi_scale), |
60 INT_MIN, INT_MAX, INT_MIN, INT_MAX); | 64 INT_MIN, INT_MAX, INT_MIN, INT_MAX); |
61 } | 65 } |
62 | 66 |
63 void FlingAnimatorImpl::CancelFling() | 67 void FlingAnimatorImpl::CancelFling() |
64 { | 68 { |
65 if (!is_active_) | 69 if (!is_active_) |
66 return; | 70 return; |
67 | 71 |
68 is_active_ = false; | 72 is_active_ = false; |
69 JNIEnv* env = base::android::AttachCurrentThread(); | 73 JNIEnv* env = base::android::AttachCurrentThread(); |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
101 blink::WebGestureCurveTarget* target) { | 105 blink::WebGestureCurveTarget* target) { |
102 if (!UpdatePosition()) | 106 if (!UpdatePosition()) |
103 return false; | 107 return false; |
104 | 108 |
105 gfx::Point current_position = GetCurrentPosition(); | 109 gfx::Point current_position = GetCurrentPosition(); |
106 gfx::Vector2d diff(current_position - last_position_); | 110 gfx::Vector2d diff(current_position - last_position_); |
107 last_position_ = current_position; | 111 last_position_ = current_position; |
108 float dpi_scale = gfx::Screen::GetNativeScreen()->GetPrimaryDisplay() | 112 float dpi_scale = gfx::Screen::GetNativeScreen()->GetPrimaryDisplay() |
109 .device_scale_factor(); | 113 .device_scale_factor(); |
110 blink::WebFloatSize scroll_amount(diff.x() / dpi_scale, | 114 blink::WebFloatSize scroll_amount(diff.x() / dpi_scale, |
111 diff.y() / dpi_scale); | 115 diff.y() / dpi_scale); |
112 | 116 |
113 float delta_time = time - last_time_; | 117 float delta_time = time - last_time_; |
114 last_time_ = time; | 118 last_time_ = time; |
115 | 119 |
116 // Currently, the OverScroller only provides the velocity magnitude; use the | 120 // Currently, the OverScroller only provides the velocity magnitude; use the |
117 // angle of the scroll delta to yield approximate x and y velocity components. | 121 // 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. | 122 // TODO(jdduke): Remove this when we can properly poll OverScroller velocity. |
119 gfx::PointF current_velocity = last_velocity_; | 123 gfx::PointF current_velocity = last_velocity_; |
120 if (delta_time > kEpsilon) { | 124 if (delta_time > kEpsilon) { |
121 float diff_length = diff.Length(); | 125 float diff_length = diff.Length(); |
122 if (diff_length > kEpsilon) { | 126 if (diff_length > kEpsilon) { |
123 float velocity = GetCurrentVelocity(); | 127 float velocity = GetCurrentVelocity(); |
124 float scroll_to_velocity = velocity / diff_length; | 128 float scroll_to_velocity = velocity / diff_length; |
125 current_velocity = gfx::PointF(diff.x() * scroll_to_velocity, | 129 current_velocity = gfx::PointF(diff.x() * scroll_to_velocity, |
126 diff.y() * scroll_to_velocity); | 130 diff.y() * scroll_to_velocity); |
127 } | 131 } |
128 } | 132 } |
129 last_velocity_ = current_velocity; | 133 last_velocity_ = current_velocity; |
130 blink::WebFloatSize fling_velocity(current_velocity.x() / dpi_scale, | 134 blink::WebFloatSize fling_velocity(current_velocity.x() / dpi_scale, |
131 current_velocity.y() / dpi_scale); | 135 current_velocity.y() / dpi_scale); |
132 target->notifyCurrentFlingVelocity(fling_velocity); | 136 target->notifyCurrentFlingVelocity(fling_velocity); |
133 | 137 |
134 // scrollBy() could delete this curve if the animation is over, so don't touch | 138 // scrollBy() could delete this curve if the animation is over, so don't touch |
135 // any member variables after making that call. | 139 // any member variables after making that call. |
136 target->scrollBy(scroll_amount); | 140 target->scrollBy(scroll_amount); |
137 return true; | 141 return true; |
138 } | 142 } |
139 | 143 |
140 FlingAnimatorImpl* FlingAnimatorImpl::CreateAndroidGestureCurve( | 144 FlingAnimatorImpl* FlingAnimatorImpl::CreateAndroidGestureCurve( |
141 const blink::WebFloatPoint& velocity, | 145 const blink::WebFloatPoint& velocity, |
142 const blink::WebSize&) { | 146 const blink::WebSize&) { |
143 FlingAnimatorImpl* gesture_curve = new FlingAnimatorImpl(); | 147 FlingAnimatorImpl* gesture_curve = new FlingAnimatorImpl(); |
144 gesture_curve->StartFling(velocity); | 148 gesture_curve->StartFling(velocity); |
145 return gesture_curve; | 149 return gesture_curve; |
146 } | 150 } |
147 | 151 |
148 } // namespace webkit_glue | 152 } // namespace webkit_glue |
OLD | NEW |