OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "content/browser/renderer_host/input/motion_event_android.h" | 5 #include "content/browser/renderer_host/input/motion_event_android.h" |
6 | 6 |
7 #include "base/android/jni_android.h" | 7 #include "base/android/jni_android.h" |
8 #include "jni/MotionEvent_jni.h" | 8 #include "jni/MotionEvent_jni.h" |
9 | 9 |
10 using base::android::AttachCurrentThread; | 10 using base::android::AttachCurrentThread; |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
57 int64 ToAndroidTime(base::TimeTicks time) { | 57 int64 ToAndroidTime(base::TimeTicks time) { |
58 return (time - base::TimeTicks()).InMilliseconds(); | 58 return (time - base::TimeTicks()).InMilliseconds(); |
59 } | 59 } |
60 | 60 |
61 base::TimeTicks FromAndroidTime(int64 time_ms) { | 61 base::TimeTicks FromAndroidTime(int64 time_ms) { |
62 return base::TimeTicks() + base::TimeDelta::FromMilliseconds(time_ms); | 62 return base::TimeTicks() + base::TimeDelta::FromMilliseconds(time_ms); |
63 } | 63 } |
64 | 64 |
65 } // namespace | 65 } // namespace |
66 | 66 |
67 MotionEventAndroid::MotionEventAndroid(JNIEnv* env, jobject event, bool recycle) | 67 MotionEventAndroid::MotionEventAndroid(JNIEnv* env, |
| 68 jobject event, |
| 69 jlong time_ms, |
| 70 jint android_action, |
| 71 jint pointer_count, |
| 72 jint history_size, |
| 73 jint action_index, |
| 74 jfloat pos_x_0, |
| 75 jfloat pos_y_0, |
| 76 jfloat pos_x_1, |
| 77 jfloat pos_y_1, |
| 78 jint pointer_id_0, |
| 79 jint pointer_id_1, |
| 80 jfloat touch_major_0, |
| 81 jfloat touch_major_1) |
| 82 : cached_time_(FromAndroidTime(time_ms)), |
| 83 cached_action_(FromAndroidAction(android_action)), |
| 84 cached_pointer_count_(pointer_count), |
| 85 cached_history_size_(history_size), |
| 86 cached_action_index_(action_index), |
| 87 should_recycle_(false) { |
| 88 DCHECK_GT(pointer_count, 0); |
| 89 DCHECK_GE(history_size, 0); |
| 90 |
| 91 event_.Reset(env, event); |
| 92 DCHECK(event_.obj()); |
| 93 |
| 94 cached_positions_[0] = gfx::PointF(pos_x_0, pos_y_0); |
| 95 cached_positions_[1] = gfx::PointF(pos_x_1, pos_y_1); |
| 96 cached_pointer_ids_[0] = pointer_id_0; |
| 97 cached_pointer_ids_[1] = pointer_id_1; |
| 98 cached_touch_majors_[0] = touch_major_0; |
| 99 cached_touch_majors_[1] = touch_major_1; |
| 100 } |
| 101 |
| 102 MotionEventAndroid::MotionEventAndroid(JNIEnv* env, jobject event) |
68 : cached_time_(FromAndroidTime(Java_MotionEvent_getEventTime(env, event))), | 103 : cached_time_(FromAndroidTime(Java_MotionEvent_getEventTime(env, event))), |
69 cached_action_(FromAndroidAction( | 104 cached_action_(FromAndroidAction( |
70 Java_MotionEvent_getActionMasked(env, event))), | 105 Java_MotionEvent_getActionMasked(env, event))), |
71 cached_pointer_count_(Java_MotionEvent_getPointerCount(env, event)), | 106 cached_pointer_count_(Java_MotionEvent_getPointerCount(env, event)), |
72 cached_history_size_(Java_MotionEvent_getHistorySize(env, event)), | 107 cached_history_size_(Java_MotionEvent_getHistorySize(env, event)), |
73 cached_action_index_(Java_MotionEvent_getActionIndex(env, event)), | 108 cached_action_index_(Java_MotionEvent_getActionIndex(env, event)), |
74 cached_x_(Java_MotionEvent_getXF(env, event)), | 109 should_recycle_(true) { |
75 cached_y_(Java_MotionEvent_getYF(env, event)), | |
76 should_recycle_(recycle) { | |
77 event_.Reset(env, event); | 110 event_.Reset(env, event); |
78 DCHECK(event_.obj()); | 111 DCHECK(event_.obj()); |
| 112 |
| 113 for (size_t i = 0; i < MAX_POINTERS_TO_CACHE; ++i) { |
| 114 if (i < cached_pointer_count_) { |
| 115 cached_positions_[i].set_x(Java_MotionEvent_getXF_I(env, event, i)); |
| 116 cached_positions_[i].set_y(Java_MotionEvent_getYF_I(env, event, i)); |
| 117 cached_pointer_ids_[i] = Java_MotionEvent_getPointerId(env, event, i); |
| 118 cached_touch_majors_[i] = |
| 119 Java_MotionEvent_getTouchMajorF_I(env, event, i); |
| 120 } else { |
| 121 cached_pointer_ids_[i] = 0; |
| 122 cached_touch_majors_[i] = 0.f; |
| 123 } |
| 124 } |
79 } | 125 } |
80 | 126 |
81 MotionEventAndroid::MotionEventAndroid(const MotionEventAndroid& other) | 127 MotionEventAndroid::MotionEventAndroid(const MotionEventAndroid& other) |
82 : event_(Obtain(other)), | 128 : event_(Obtain(other)), |
83 cached_time_(other.cached_time_), | 129 cached_time_(other.cached_time_), |
84 cached_action_(other.cached_action_), | 130 cached_action_(other.cached_action_), |
85 cached_pointer_count_(other.cached_pointer_count_), | 131 cached_pointer_count_(other.cached_pointer_count_), |
86 cached_history_size_(other.cached_history_size_), | 132 cached_history_size_(other.cached_history_size_), |
87 cached_action_index_(other.cached_action_index_), | 133 cached_action_index_(other.cached_action_index_), |
88 cached_x_(other.cached_x_), | |
89 cached_y_(other.cached_y_), | |
90 should_recycle_(true) { | 134 should_recycle_(true) { |
91 DCHECK(event_.obj()); | 135 DCHECK(event_.obj()); |
| 136 for (size_t i = 0; i < MAX_POINTERS_TO_CACHE; ++i) { |
| 137 cached_positions_[i] = other.cached_positions_[i]; |
| 138 cached_pointer_ids_[i] = other.cached_pointer_ids_[i]; |
| 139 cached_touch_majors_[i] = other.cached_touch_majors_[i]; |
| 140 } |
92 } | 141 } |
93 | 142 |
94 MotionEventAndroid::~MotionEventAndroid() { | 143 MotionEventAndroid::~MotionEventAndroid() { |
95 if (should_recycle_) | 144 if (should_recycle_) |
96 Java_MotionEvent_recycle(AttachCurrentThread(), event_.obj()); | 145 Java_MotionEvent_recycle(AttachCurrentThread(), event_.obj()); |
97 } | 146 } |
98 | 147 |
99 MotionEventAndroid::Action MotionEventAndroid::GetAction() const { | 148 MotionEventAndroid::Action MotionEventAndroid::GetAction() const { |
100 return cached_action_; | 149 return cached_action_; |
101 } | 150 } |
102 | 151 |
103 int MotionEventAndroid::GetActionIndex() const { | 152 int MotionEventAndroid::GetActionIndex() const { |
104 return cached_action_index_; | 153 return cached_action_index_; |
105 } | 154 } |
106 | 155 |
107 size_t MotionEventAndroid::GetPointerCount() const { | 156 size_t MotionEventAndroid::GetPointerCount() const { |
108 return cached_pointer_count_; | 157 return cached_pointer_count_; |
109 } | 158 } |
110 | 159 |
111 int MotionEventAndroid::GetPointerId(size_t pointer_index) const { | 160 int MotionEventAndroid::GetPointerId(size_t pointer_index) const { |
112 DCHECK_LT(pointer_index, cached_pointer_count_); | 161 DCHECK_LT(pointer_index, cached_pointer_count_); |
| 162 if (pointer_index < MAX_POINTERS_TO_CACHE) |
| 163 return cached_pointer_ids_[pointer_index]; |
113 return Java_MotionEvent_getPointerId( | 164 return Java_MotionEvent_getPointerId( |
114 AttachCurrentThread(), event_.obj(), pointer_index); | 165 AttachCurrentThread(), event_.obj(), pointer_index); |
115 } | 166 } |
116 | 167 |
117 float MotionEventAndroid::GetX(size_t pointer_index) const { | 168 float MotionEventAndroid::GetX(size_t pointer_index) const { |
118 DCHECK_LT(pointer_index, cached_pointer_count_); | 169 DCHECK_LT(pointer_index, cached_pointer_count_); |
119 if (pointer_index == 0) | 170 if (pointer_index < MAX_POINTERS_TO_CACHE) |
120 return cached_x_; | 171 return cached_positions_[pointer_index].x(); |
121 return Java_MotionEvent_getXF_I( | 172 return Java_MotionEvent_getXF_I( |
122 AttachCurrentThread(), event_.obj(), pointer_index); | 173 AttachCurrentThread(), event_.obj(), pointer_index); |
123 } | 174 } |
124 | 175 |
125 float MotionEventAndroid::GetY(size_t pointer_index) const { | 176 float MotionEventAndroid::GetY(size_t pointer_index) const { |
126 DCHECK_LT(pointer_index, cached_pointer_count_); | 177 DCHECK_LT(pointer_index, cached_pointer_count_); |
127 if (pointer_index == 0) | 178 if (pointer_index < MAX_POINTERS_TO_CACHE) |
128 return cached_y_; | 179 return cached_positions_[pointer_index].y(); |
129 return Java_MotionEvent_getYF_I( | 180 return Java_MotionEvent_getYF_I( |
130 AttachCurrentThread(), event_.obj(), pointer_index); | 181 AttachCurrentThread(), event_.obj(), pointer_index); |
131 } | 182 } |
132 | 183 |
133 float MotionEventAndroid::GetTouchMajor(size_t pointer_index) const { | 184 float MotionEventAndroid::GetTouchMajor(size_t pointer_index) const { |
134 DCHECK_LT(pointer_index, cached_pointer_count_); | 185 DCHECK_LT(pointer_index, cached_pointer_count_); |
| 186 if (pointer_index < MAX_POINTERS_TO_CACHE) |
| 187 return cached_touch_majors_[pointer_index]; |
135 return Java_MotionEvent_getTouchMajorF_I( | 188 return Java_MotionEvent_getTouchMajorF_I( |
136 AttachCurrentThread(), event_.obj(), pointer_index); | 189 AttachCurrentThread(), event_.obj(), pointer_index); |
137 } | 190 } |
138 | 191 |
139 float MotionEventAndroid::GetPressure(size_t pointer_index) const { | 192 float MotionEventAndroid::GetPressure(size_t pointer_index) const { |
140 DCHECK_LT(pointer_index, cached_pointer_count_); | 193 DCHECK_LT(pointer_index, cached_pointer_count_); |
141 return Java_MotionEvent_getPressureF_I( | 194 return Java_MotionEvent_getPressureF_I( |
142 AttachCurrentThread(), event_.obj(), pointer_index); | 195 AttachCurrentThread(), event_.obj(), pointer_index); |
143 } | 196 } |
144 | 197 |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
179 return scoped_ptr<MotionEvent>(new MotionEventAndroid(*this)); | 232 return scoped_ptr<MotionEvent>(new MotionEventAndroid(*this)); |
180 } | 233 } |
181 | 234 |
182 scoped_ptr<ui::MotionEvent> MotionEventAndroid::Cancel() const { | 235 scoped_ptr<ui::MotionEvent> MotionEventAndroid::Cancel() const { |
183 return scoped_ptr<MotionEvent>(new MotionEventAndroid( | 236 return scoped_ptr<MotionEvent>(new MotionEventAndroid( |
184 AttachCurrentThread(), | 237 AttachCurrentThread(), |
185 Obtain(GetDownTime(), | 238 Obtain(GetDownTime(), |
186 GetEventTime(), | 239 GetEventTime(), |
187 MotionEventAndroid::ACTION_CANCEL, | 240 MotionEventAndroid::ACTION_CANCEL, |
188 GetX(0), | 241 GetX(0), |
189 GetY(0)).obj(), | 242 GetY(0)).obj())); |
190 true)); | |
191 } | 243 } |
192 | 244 |
193 float MotionEventAndroid::GetTouchMinor(size_t pointer_index) const { | 245 float MotionEventAndroid::GetTouchMinor(size_t pointer_index) const { |
194 return Java_MotionEvent_getTouchMinorF_I( | 246 return Java_MotionEvent_getTouchMinorF_I( |
195 AttachCurrentThread(), event_.obj(), pointer_index); | 247 AttachCurrentThread(), event_.obj(), pointer_index); |
196 } | 248 } |
197 | 249 |
198 float MotionEventAndroid::GetOrientation() const { | 250 float MotionEventAndroid::GetOrientation() const { |
199 return Java_MotionEvent_getOrientationF(AttachCurrentThread(), event_.obj()); | 251 return Java_MotionEvent_getOrientationF(AttachCurrentThread(), event_.obj()); |
200 } | 252 } |
(...skipping 25 matching lines...) Expand all Loading... |
226 return Java_MotionEvent_obtainAVME_J_J_I_F_F_I(AttachCurrentThread(), | 278 return Java_MotionEvent_obtainAVME_J_J_I_F_F_I(AttachCurrentThread(), |
227 ToAndroidTime(down_time), | 279 ToAndroidTime(down_time), |
228 ToAndroidTime(event_time), | 280 ToAndroidTime(event_time), |
229 ToAndroidAction(action), | 281 ToAndroidAction(action), |
230 x, | 282 x, |
231 y, | 283 y, |
232 0); | 284 0); |
233 } | 285 } |
234 | 286 |
235 } // namespace content | 287 } // namespace content |
OLD | NEW |