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" |
11 #include "third_party/WebKit/public/platform/WebFloatSize.h" | 11 #include "third_party/WebKit/public/platform/WebFloatSize.h" |
12 #include "third_party/WebKit/public/platform/WebGestureCurveTarget.h" | 12 #include "third_party/WebKit/public/platform/WebGestureCurveTarget.h" |
13 #include "ui/gfx/screen.h" | 13 #include "ui/gfx/screen.h" |
14 #include "ui/gfx/vector2d.h" | 14 #include "ui/gfx/vector2d.h" |
15 | 15 |
16 namespace webkit_glue { | 16 namespace webkit_glue { |
17 | 17 |
18 namespace { | 18 namespace { |
19 static const float kEpsilon = 1e-4; | 19 static const float kEpsilon = 1e-4; |
20 } | 20 } |
21 | 21 |
22 FlingAnimatorImpl::FlingAnimatorImpl() | 22 FlingAnimatorImpl::FlingAnimatorImpl() |
23 : is_active_(false) { | 23 : is_active_(false), |
24 last_time_(0), | |
25 dpi_scale_(1) { | |
24 // hold the global reference of the Java objects. | 26 // hold the global reference of the Java objects. |
25 JNIEnv* env = base::android::AttachCurrentThread(); | 27 JNIEnv* env = base::android::AttachCurrentThread(); |
26 java_scroller_.Reset(JNI_OverScroller::Java_OverScroller_ConstructorAWOS_ACC( | 28 java_scroller_.Reset(JNI_OverScroller::Java_OverScroller_ConstructorAWOS_ACC( |
27 env, | 29 env, |
28 base::android::GetApplicationContext())); | 30 base::android::GetApplicationContext())); |
29 } | 31 } |
30 | 32 |
31 FlingAnimatorImpl::~FlingAnimatorImpl() | 33 FlingAnimatorImpl::~FlingAnimatorImpl() |
32 { | 34 { |
33 } | 35 } |
34 | 36 |
35 //static | 37 //static |
36 bool FlingAnimatorImpl::RegisterJni(JNIEnv* env) { | 38 bool FlingAnimatorImpl::RegisterJni(JNIEnv* env) { |
37 return JNI_OverScroller::RegisterNativesImpl(env); | 39 return JNI_OverScroller::RegisterNativesImpl(env); |
38 } | 40 } |
39 | 41 |
40 void FlingAnimatorImpl::StartFling(const gfx::PointF& velocity) | 42 void FlingAnimatorImpl::StartFling(const gfx::PointF& velocity) |
41 { | 43 { |
42 // No bounds on the fling. See http://webkit.org/b/96403 | 44 // No bounds on the fling. See http://webkit.org/b/96403 |
43 // Instead, use the largest possible bounds for minX/maxX/minY/maxY. The | 45 // 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 // compositor will ignore any attempt to scroll beyond the end of the page. |
45 | 47 |
46 DCHECK(velocity.x() || velocity.y()); | 48 DCHECK(velocity.x() || velocity.y()); |
47 if (is_active_) | 49 if (is_active_) |
48 CancelFling(); | 50 CancelFling(); |
49 | 51 |
50 is_active_ = true; | 52 is_active_ = true; |
51 last_time_ = 0; | 53 last_time_ = 0; |
52 last_velocity_ = velocity; | 54 last_velocity_ = velocity; |
55 dpi_scale_ = gfx::Screen::GetNativeScreen()->GetPrimaryDisplay() | |
jamesr
2014/02/18 22:40:46
let's not do this. if you want to add a cache for
jdduke (slow)
2014/02/18 22:51:29
Done.
| |
56 .device_scale_factor(); | |
53 | 57 |
54 JNIEnv* env = base::android::AttachCurrentThread(); | 58 JNIEnv* env = base::android::AttachCurrentThread(); |
55 | 59 |
60 // The OverScroller deceleration constants work in pixel space. DIP scaling | |
61 // will be performed in |apply()| on the generated fling updates. | |
56 JNI_OverScroller::Java_OverScroller_flingV_I_I_I_I_I_I_I_I( | 62 JNI_OverScroller::Java_OverScroller_flingV_I_I_I_I_I_I_I_I( |
57 env, java_scroller_.obj(), 0, 0, | 63 env, java_scroller_.obj(), 0, 0, |
58 static_cast<int>(velocity.x()), | 64 static_cast<int>(velocity.x() * dpi_scale_), |
59 static_cast<int>(velocity.y()), | 65 static_cast<int>(velocity.y() * dpi_scale_), |
60 INT_MIN, INT_MAX, INT_MIN, INT_MAX); | 66 INT_MIN, INT_MAX, INT_MIN, INT_MAX); |
61 } | 67 } |
62 | 68 |
63 void FlingAnimatorImpl::CancelFling() | 69 void FlingAnimatorImpl::CancelFling() |
64 { | 70 { |
65 if (!is_active_) | 71 if (!is_active_) |
66 return; | 72 return; |
67 | 73 |
68 is_active_ = false; | 74 is_active_ = false; |
69 JNIEnv* env = base::android::AttachCurrentThread(); | 75 JNIEnv* env = base::android::AttachCurrentThread(); |
(...skipping 28 matching lines...) Expand all Loading... | |
98 } | 104 } |
99 | 105 |
100 bool FlingAnimatorImpl::apply(double time, | 106 bool FlingAnimatorImpl::apply(double time, |
101 blink::WebGestureCurveTarget* target) { | 107 blink::WebGestureCurveTarget* target) { |
102 if (!UpdatePosition()) | 108 if (!UpdatePosition()) |
103 return false; | 109 return false; |
104 | 110 |
105 gfx::Point current_position = GetCurrentPosition(); | 111 gfx::Point current_position = GetCurrentPosition(); |
106 gfx::Vector2d diff(current_position - last_position_); | 112 gfx::Vector2d diff(current_position - last_position_); |
107 last_position_ = current_position; | 113 last_position_ = current_position; |
108 float dpi_scale = gfx::Screen::GetNativeScreen()->GetPrimaryDisplay() | 114 blink::WebFloatSize scroll_amount(diff.x() / dpi_scale_, |
109 .device_scale_factor(); | 115 diff.y() / dpi_scale_); |
jamesr
2014/02/18 22:40:46
bad alignment
jdduke (slow)
2014/02/18 22:51:29
Done.
| |
110 blink::WebFloatSize scroll_amount(diff.x() / dpi_scale, | |
111 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_); |
jamesr
2014/02/18 22:40:46
bad alignment
jdduke (slow)
2014/02/18 22:51:29
Done.
| |
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 |