Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(464)

Side by Side Diff: webkit/child/fling_animator_impl_android.cc

Issue 172933004: [Android] Port Scroller.java to C++ and use for fling animations (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Rebase Created 6 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « webkit/child/fling_animator_impl_android.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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/view_configuration.h"
11 #include "ui/gfx/frame_time.h"
14 #include "ui/gfx/vector2d.h" 12 #include "ui/gfx/vector2d.h"
15 13
16 namespace webkit_glue { 14 namespace webkit_glue {
17 15
18 namespace { 16 namespace {
19 static const float kEpsilon = 1e-4; 17
18 gfx::Scroller::Config GetScrollerConfig() {
19 gfx::Scroller::Config config;
20 config.flywheel_enabled = false;
21 config.fling_friction = gfx::ViewConfiguration::GetScrollFriction();
22 return config;
20 } 23 }
21 24
25 } // namespace
26
22 FlingAnimatorImpl::FlingAnimatorImpl() 27 FlingAnimatorImpl::FlingAnimatorImpl()
23 : is_active_(false) { 28 : is_active_(false),
24 // hold the global reference of the Java objects. 29 scroller_(GetScrollerConfig()) {}
25 JNIEnv* env = base::android::AttachCurrentThread();
26 java_scroller_.Reset(JNI_OverScroller::Java_OverScroller_ConstructorAWOS_ACC(
27 env,
28 base::android::GetApplicationContext()));
29 }
30 30
31 FlingAnimatorImpl::~FlingAnimatorImpl() 31 FlingAnimatorImpl::~FlingAnimatorImpl() {}
32 {
33 }
34 32
35 //static 33 void FlingAnimatorImpl::StartFling(const gfx::PointF& velocity) {
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 34 // No bounds on the fling. See http://webkit.org/b/96403
43 // Instead, use the largest possible bounds for minX/maxX/minY/maxY. The 35 // 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. 36 // compositor will ignore any attempt to scroll beyond the end of the page.
45 37
46 DCHECK(velocity.x() || velocity.y()); 38 DCHECK(velocity.x() || velocity.y());
47 if (is_active_) 39 if (is_active_)
48 CancelFling(); 40 CancelFling();
49 41
50 is_active_ = true; 42 is_active_ = true;
51 last_time_ = 0; 43 scroller_.Fling(0,
52 last_velocity_ = velocity; 44 0,
53 45 velocity.x(),
54 JNIEnv* env = base::android::AttachCurrentThread(); 46 velocity.y(),
55 47 INT_MIN,
56 // The OverScroller deceleration constants work in pixel space. DIP scaling 48 INT_MAX,
57 // will be performed in |apply()| on the generated fling updates. 49 INT_MIN,
58 float dpi_scale = gfx::Screen::GetNativeScreen()->GetPrimaryDisplay() 50 INT_MAX,
59 .device_scale_factor(); 51 gfx::FrameTime::Now());
60 JNI_OverScroller::Java_OverScroller_flingV_I_I_I_I_I_I_I_I( 52 // TODO(jdduke): Initialize the fling at time 0 and use the monotonic
61 env, java_scroller_.obj(), 0, 0, 53 // time in |apply()| for updates, crbug.com/345459.
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 } 54 }
66 55
67 void FlingAnimatorImpl::CancelFling() 56 void FlingAnimatorImpl::CancelFling() {
68 {
69 if (!is_active_) 57 if (!is_active_)
70 return; 58 return;
71 59
72 is_active_ = false; 60 is_active_ = false;
73 JNIEnv* env = base::android::AttachCurrentThread(); 61 scroller_.AbortAnimation();
74 JNI_OverScroller::Java_OverScroller_abortAnimation(env, java_scroller_.obj());
75 } 62 }
76 63
77 bool FlingAnimatorImpl::UpdatePosition() 64 bool FlingAnimatorImpl::apply(double /* time */,
78 { 65 blink::WebGestureCurveTarget* target) {
79 JNIEnv* env = base::android::AttachCurrentThread(); 66 // Historically, Android's Scroller used |currentAnimationTimeMillis()|,
80 bool result = JNI_OverScroller::Java_OverScroller_computeScrollOffset( 67 // which is equivalent to gfx::FrameTime::Now(). In practice, this produces
81 env, 68 // smoother results than using |time|, so continue using FrameTime::Now().
82 java_scroller_.obj()); 69 // TODO(jdduke): Use |time| upon resolution of crbug.com/345459.
83 return is_active_ = result; 70 if (!scroller_.ComputeScrollOffset(gfx::FrameTime::Now())) {
84 } 71 is_active_ = false;
72 return false;
73 }
85 74
86 gfx::Point FlingAnimatorImpl::GetCurrentPosition() 75 target->notifyCurrentFlingVelocity(blink::WebFloatSize(
87 { 76 scroller_.GetCurrVelocityX(), scroller_.GetCurrVelocityY()));
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 77
95 float FlingAnimatorImpl::GetCurrentVelocity() 78 gfx::PointF current_position(scroller_.GetCurrX(), scroller_.GetCurrY());
96 { 79 gfx::Vector2dF scroll_amount(current_position - last_position_);
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; 80 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 81
138 // scrollBy() could delete this curve if the animation is over, so don't touch 82 // scrollBy() could delete this curve if the animation is over, so don't touch
139 // any member variables after making that call. 83 // any member variables after making that call.
140 target->scrollBy(scroll_amount); 84 target->scrollBy(blink::WebFloatSize(scroll_amount));
141 return true; 85 return true;
142 } 86 }
143 87
144 FlingAnimatorImpl* FlingAnimatorImpl::CreateAndroidGestureCurve( 88 FlingAnimatorImpl* FlingAnimatorImpl::CreateAndroidGestureCurve(
145 const blink::WebFloatPoint& velocity, 89 const blink::WebFloatPoint& velocity,
146 const blink::WebSize&) { 90 const blink::WebSize&) {
147 FlingAnimatorImpl* gesture_curve = new FlingAnimatorImpl(); 91 FlingAnimatorImpl* gesture_curve = new FlingAnimatorImpl();
148 gesture_curve->StartFling(velocity); 92 gesture_curve->StartFling(velocity);
149 return gesture_curve; 93 return gesture_curve;
150 } 94 }
151 95
152 } // namespace webkit_glue 96 } // namespace webkit_glue
OLDNEW
« no previous file with comments | « webkit/child/fling_animator_impl_android.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698