Chromium Code Reviews| 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" | |
| 8 #include "base/android/scoped_java_ref.h" | |
| 9 #include "base/logging.h" | 7 #include "base/logging.h" |
| 10 #include "jni/OverScroller_jni.h" | |
| 11 #include "third_party/WebKit/public/platform/WebFloatSize.h" | 8 #include "third_party/WebKit/public/platform/WebFloatSize.h" |
| 12 #include "third_party/WebKit/public/platform/WebGestureCurveTarget.h" | 9 #include "third_party/WebKit/public/platform/WebGestureCurveTarget.h" |
| 13 #include "ui/gfx/screen.h" | 10 #include "ui/gfx/android/scroller.h" |
| 14 #include "ui/gfx/vector2d.h" | 11 #include "ui/gfx/vector2d.h" |
| 15 | 12 |
| 16 namespace webkit_glue { | 13 namespace webkit_glue { |
| 17 | 14 |
| 18 namespace { | 15 FlingAnimatorImpl::FlingAnimatorImpl() |
| 19 static const float kEpsilon = 1e-4; | 16 : is_active_(false), scroller_(new gfx::Scroller(false)) {} |
| 20 } | |
| 21 | 17 |
| 22 FlingAnimatorImpl::FlingAnimatorImpl() | 18 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 | 19 |
| 31 FlingAnimatorImpl::~FlingAnimatorImpl() | 20 void FlingAnimatorImpl::StartFling(const gfx::PointF& velocity) { |
| 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 | 21 // No bounds on the fling. See http://webkit.org/b/96403 |
| 43 // Instead, use the largest possible bounds for minX/maxX/minY/maxY. The | 22 // 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. | 23 // compositor will ignore any attempt to scroll beyond the end of the page. |
| 45 | 24 |
| 46 DCHECK(velocity.x() || velocity.y()); | 25 DCHECK(velocity.x() || velocity.y()); |
| 47 if (is_active_) | 26 if (is_active_) |
| 48 CancelFling(); | 27 CancelFling(); |
| 49 | 28 |
| 50 is_active_ = true; | 29 is_active_ = true; |
| 51 last_time_ = 0; | 30 scroller_->Fling(0, |
| 52 last_velocity_ = velocity; | 31 0, |
| 53 | 32 velocity.x(), |
| 54 JNIEnv* env = base::android::AttachCurrentThread(); | 33 velocity.y(), |
| 55 | 34 INT_MIN, |
| 56 // The OverScroller deceleration constants work in pixel space. DIP scaling | 35 INT_MAX, |
| 57 // will be performed in |apply()| on the generated fling updates. | 36 INT_MIN, |
| 58 float dpi_scale = gfx::Screen::GetNativeScreen()->GetPrimaryDisplay() | 37 INT_MAX, |
| 59 .device_scale_factor(); | 38 base::TimeTicks::Now()); |
| 60 JNI_OverScroller::Java_OverScroller_flingV_I_I_I_I_I_I_I_I( | 39 // TODO(jdduke): Feed |base::TimeTicks()| upon resolution of crbug.com/345459. |
| 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 } | 40 } |
| 66 | 41 |
| 67 void FlingAnimatorImpl::CancelFling() | 42 void FlingAnimatorImpl::CancelFling() { |
| 68 { | |
| 69 if (!is_active_) | 43 if (!is_active_) |
| 70 return; | 44 return; |
| 71 | 45 |
| 72 is_active_ = false; | 46 is_active_ = false; |
| 73 JNIEnv* env = base::android::AttachCurrentThread(); | 47 scroller_->AbortAnimation(); |
| 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 } | 48 } |
| 103 | 49 |
| 104 bool FlingAnimatorImpl::apply(double time, | 50 bool FlingAnimatorImpl::apply(double time, |
| 105 blink::WebGestureCurveTarget* target) { | 51 blink::WebGestureCurveTarget* target) { |
| 106 if (!UpdatePosition()) | 52 // TODO(jdduke): Use the provided |time|, resolving why time deltas are more |
|
aelias_OOO_until_Jul13
2014/02/21 02:42:35
Wouldn't this be a preexisting problem? Are you a
jdduke (slow)
2014/02/21 23:33:02
Yeah, definitely pre-existing, I'll make that more
| |
| 53 // erratic with the provided time than with |Now()|, crbug.com/345459. | |
| 54 base::TimeTicks timeticks = base::TimeTicks::Now(); | |
| 55 if (!scroller_->ComputeScrollOffset(timeticks)) { | |
| 56 is_active_ = false; | |
| 107 return false; | 57 return false; |
| 58 } | |
| 108 | 59 |
| 109 gfx::Point current_position = GetCurrentPosition(); | 60 target->notifyCurrentFlingVelocity(blink::WebFloatSize( |
| 110 gfx::Vector2d diff(current_position - last_position_); | 61 scroller_->GetCurrVelocityX(), scroller_->GetCurrVelocityY())); |
| 62 | |
| 63 gfx::PointF current_position(scroller_->GetCurrX(), scroller_->GetCurrY()); | |
| 64 gfx::Vector2dF scroll_amount(current_position - last_position_); | |
| 111 last_position_ = current_position; | 65 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 | 66 |
| 138 // scrollBy() could delete this curve if the animation is over, so don't touch | 67 // scrollBy() could delete this curve if the animation is over, so don't touch |
| 139 // any member variables after making that call. | 68 // any member variables after making that call. |
| 140 target->scrollBy(scroll_amount); | 69 target->scrollBy(blink::WebFloatSize(scroll_amount)); |
| 141 return true; | 70 return true; |
| 142 } | 71 } |
| 143 | 72 |
| 144 FlingAnimatorImpl* FlingAnimatorImpl::CreateAndroidGestureCurve( | 73 FlingAnimatorImpl* FlingAnimatorImpl::CreateAndroidGestureCurve( |
| 145 const blink::WebFloatPoint& velocity, | 74 const blink::WebFloatPoint& velocity, |
| 146 const blink::WebSize&) { | 75 const blink::WebSize&) { |
| 147 FlingAnimatorImpl* gesture_curve = new FlingAnimatorImpl(); | 76 FlingAnimatorImpl* gesture_curve = new FlingAnimatorImpl(); |
| 148 gesture_curve->StartFling(velocity); | 77 gesture_curve->StartFling(velocity); |
| 149 return gesture_curve; | 78 return gesture_curve; |
| 150 } | 79 } |
| 151 | 80 |
| 152 } // namespace webkit_glue | 81 } // namespace webkit_glue |
| OLD | NEW |