Chromium Code Reviews| 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 "base/float_util.h" | |
| 8 #include "jni/MotionEvent_jni.h" | 9 #include "jni/MotionEvent_jni.h" |
| 9 | 10 |
| 10 using base::android::AttachCurrentThread; | 11 using base::android::AttachCurrentThread; |
| 11 using namespace JNI_MotionEvent; | 12 using namespace JNI_MotionEvent; |
| 12 | 13 |
| 13 namespace content { | 14 namespace content { |
| 14 namespace { | 15 namespace { |
| 15 | 16 |
| 16 int ToAndroidAction(MotionEventAndroid::Action action) { | 17 int ToAndroidAction(MotionEventAndroid::Action action) { |
| 17 switch (action) { | 18 switch (action) { |
| (...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 107 jint history_size, | 108 jint history_size, |
| 108 jint action_index, | 109 jint action_index, |
| 109 jfloat pos_x_0_pixels, | 110 jfloat pos_x_0_pixels, |
| 110 jfloat pos_y_0_pixels, | 111 jfloat pos_y_0_pixels, |
| 111 jfloat pos_x_1_pixels, | 112 jfloat pos_x_1_pixels, |
| 112 jfloat pos_y_1_pixels, | 113 jfloat pos_y_1_pixels, |
| 113 jint pointer_id_0, | 114 jint pointer_id_0, |
| 114 jint pointer_id_1, | 115 jint pointer_id_1, |
| 115 jfloat touch_major_0_pixels, | 116 jfloat touch_major_0_pixels, |
| 116 jfloat touch_major_1_pixels, | 117 jfloat touch_major_1_pixels, |
| 118 jfloat touch_minor_0_pixels, | |
| 119 jfloat touch_minor_1_pixels, | |
| 120 jfloat orientation_0_rad, | |
| 121 jfloat orientation_1_rad, | |
| 117 jfloat raw_pos_x_pixels, | 122 jfloat raw_pos_x_pixels, |
| 118 jfloat raw_pos_y_pixels, | 123 jfloat raw_pos_y_pixels, |
| 119 jint android_tool_type_0, | 124 jint android_tool_type_0, |
| 120 jint android_tool_type_1, | 125 jint android_tool_type_1, |
| 121 jint android_button_state) | 126 jint android_button_state) |
| 122 : cached_time_(FromAndroidTime(time_ms)), | 127 : cached_time_(FromAndroidTime(time_ms)), |
| 123 cached_action_(FromAndroidAction(android_action)), | 128 cached_action_(FromAndroidAction(android_action)), |
| 124 cached_pointer_count_(pointer_count), | 129 cached_pointer_count_(pointer_count), |
| 125 cached_history_size_(history_size), | 130 cached_history_size_(history_size), |
| 126 cached_action_index_(action_index), | 131 cached_action_index_(action_index), |
| 127 cached_button_state_(FromAndroidButtonState(android_button_state)), | 132 cached_button_state_(FromAndroidButtonState(android_button_state)), |
| 128 pix_to_dip_(pix_to_dip), | 133 pix_to_dip_(pix_to_dip), |
| 129 should_recycle_(false) { | 134 should_recycle_(false) { |
| 130 DCHECK_GT(pointer_count, 0); | 135 DCHECK_GT(pointer_count, 0); |
| 131 DCHECK_GE(history_size, 0); | 136 DCHECK_GE(history_size, 0); |
| 132 | 137 |
| 133 event_.Reset(env, event); | 138 event_.Reset(env, event); |
| 134 DCHECK(event_.obj()); | 139 DCHECK(event_.obj()); |
| 135 | 140 |
| 136 cached_positions_[0] = ToDips(gfx::PointF(pos_x_0_pixels, pos_y_0_pixels)); | 141 cached_positions_[0] = ToDips(gfx::PointF(pos_x_0_pixels, pos_y_0_pixels)); |
| 137 cached_positions_[1] = ToDips(gfx::PointF(pos_x_1_pixels, pos_y_1_pixels)); | 142 cached_positions_[1] = ToDips(gfx::PointF(pos_x_1_pixels, pos_y_1_pixels)); |
| 138 cached_pointer_ids_[0] = pointer_id_0; | 143 cached_pointer_ids_[0] = pointer_id_0; |
| 139 cached_pointer_ids_[1] = pointer_id_1; | 144 cached_pointer_ids_[1] = pointer_id_1; |
| 140 cached_touch_majors_[0] = ToDips(touch_major_0_pixels); | 145 cached_touch_majors_[0] = ToDips(touch_major_0_pixels); |
| 141 cached_touch_majors_[1] = ToDips(touch_major_1_pixels); | 146 cached_touch_majors_[1] = ToDips(touch_major_1_pixels); |
| 147 cached_touch_minors_[0] = ToDips(touch_minor_0_pixels); | |
| 148 cached_touch_minors_[1] = ToDips(touch_minor_1_pixels); | |
| 149 cached_orientations_[0] = ToValidFloat(orientation_0_rad); | |
| 150 cached_orientations_[1] = ToValidFloat(orientation_1_rad); | |
| 142 cached_raw_position_offset_ = | 151 cached_raw_position_offset_ = |
| 143 ToDips(gfx::PointF(raw_pos_x_pixels, raw_pos_y_pixels)) - | 152 ToDips(gfx::PointF(raw_pos_x_pixels, raw_pos_y_pixels)) - |
| 144 cached_positions_[0]; | 153 cached_positions_[0]; |
| 145 cached_tool_types_[0] = FromAndroidToolType(android_tool_type_0); | 154 cached_tool_types_[0] = FromAndroidToolType(android_tool_type_0); |
| 146 cached_tool_types_[1] = FromAndroidToolType(android_tool_type_1); | 155 cached_tool_types_[1] = FromAndroidToolType(android_tool_type_1); |
| 147 } | 156 } |
| 148 | 157 |
| 149 MotionEventAndroid::MotionEventAndroid(float pix_to_dip, | 158 MotionEventAndroid::MotionEventAndroid(float pix_to_dip, |
| 150 JNIEnv* env, | 159 JNIEnv* env, |
| 151 jobject event) | 160 jobject event) |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 163 DCHECK(event_.obj()); | 172 DCHECK(event_.obj()); |
| 164 | 173 |
| 165 for (size_t i = 0; i < MAX_POINTERS_TO_CACHE; ++i) { | 174 for (size_t i = 0; i < MAX_POINTERS_TO_CACHE; ++i) { |
| 166 if (i < cached_pointer_count_) { | 175 if (i < cached_pointer_count_) { |
| 167 cached_positions_[i] = | 176 cached_positions_[i] = |
| 168 ToDips(gfx::PointF(Java_MotionEvent_getXF_I(env, event, i), | 177 ToDips(gfx::PointF(Java_MotionEvent_getXF_I(env, event, i), |
| 169 Java_MotionEvent_getYF_I(env, event, i))); | 178 Java_MotionEvent_getYF_I(env, event, i))); |
| 170 cached_pointer_ids_[i] = Java_MotionEvent_getPointerId(env, event, i); | 179 cached_pointer_ids_[i] = Java_MotionEvent_getPointerId(env, event, i); |
| 171 cached_touch_majors_[i] = | 180 cached_touch_majors_[i] = |
| 172 ToDips(Java_MotionEvent_getTouchMajorF_I(env, event, i)); | 181 ToDips(Java_MotionEvent_getTouchMajorF_I(env, event, i)); |
| 182 cached_touch_minors_[i] = | |
| 183 ToDips(Java_MotionEvent_getTouchMinorF_I(env, event, i)); | |
| 184 cached_orientations_[i] = | |
| 185 ToValidFloat(Java_MotionEvent_getOrientationF_I(env, event, i)); | |
| 173 cached_tool_types_[i] = | 186 cached_tool_types_[i] = |
| 174 FromAndroidToolType(Java_MotionEvent_getToolType(env, event, i)); | 187 FromAndroidToolType(Java_MotionEvent_getToolType(env, event, i)); |
| 175 } else { | 188 } else { |
| 176 cached_pointer_ids_[i] = 0; | 189 cached_pointer_ids_[i] = 0; |
| 177 cached_touch_majors_[i] = 0.f; | 190 cached_touch_majors_[i] = 0.f; |
| 191 cached_touch_minors_[i] = 0.f; | |
| 192 cached_orientations_[i] = 0.f; | |
| 178 cached_tool_types_[i] = MotionEvent::TOOL_TYPE_UNKNOWN; | 193 cached_tool_types_[i] = MotionEvent::TOOL_TYPE_UNKNOWN; |
| 179 } | 194 } |
| 180 } | 195 } |
| 181 | 196 |
| 182 cached_raw_position_offset_ = | 197 cached_raw_position_offset_ = |
| 183 ToDips(gfx::PointF(Java_MotionEvent_getRawX(env, event), | 198 ToDips(gfx::PointF(Java_MotionEvent_getRawX(env, event), |
| 184 Java_MotionEvent_getRawY(env, event))) - | 199 Java_MotionEvent_getRawY(env, event))) - |
| 185 cached_positions_[0]; | 200 cached_positions_[0]; |
| 186 } | 201 } |
| 187 | 202 |
| 188 MotionEventAndroid::MotionEventAndroid(const MotionEventAndroid& other) | 203 MotionEventAndroid::MotionEventAndroid(const MotionEventAndroid& other) |
| 189 : event_(Obtain(other)), | 204 : event_(Obtain(other)), |
| 190 cached_time_(other.cached_time_), | 205 cached_time_(other.cached_time_), |
| 191 cached_action_(other.cached_action_), | 206 cached_action_(other.cached_action_), |
| 192 cached_pointer_count_(other.cached_pointer_count_), | 207 cached_pointer_count_(other.cached_pointer_count_), |
| 193 cached_history_size_(other.cached_history_size_), | 208 cached_history_size_(other.cached_history_size_), |
| 194 cached_action_index_(other.cached_action_index_), | 209 cached_action_index_(other.cached_action_index_), |
| 195 cached_raw_position_offset_(other.cached_raw_position_offset_), | 210 cached_raw_position_offset_(other.cached_raw_position_offset_), |
| 196 cached_button_state_(other.cached_button_state_), | 211 cached_button_state_(other.cached_button_state_), |
| 197 pix_to_dip_(other.pix_to_dip_), | 212 pix_to_dip_(other.pix_to_dip_), |
| 198 should_recycle_(true) { | 213 should_recycle_(true) { |
| 199 DCHECK(event_.obj()); | 214 DCHECK(event_.obj()); |
| 200 for (size_t i = 0; i < MAX_POINTERS_TO_CACHE; ++i) { | 215 for (size_t i = 0; i < MAX_POINTERS_TO_CACHE; ++i) { |
| 201 cached_positions_[i] = other.cached_positions_[i]; | 216 cached_positions_[i] = other.cached_positions_[i]; |
| 202 cached_pointer_ids_[i] = other.cached_pointer_ids_[i]; | 217 cached_pointer_ids_[i] = other.cached_pointer_ids_[i]; |
| 203 cached_touch_majors_[i] = other.cached_touch_majors_[i]; | 218 cached_touch_majors_[i] = other.cached_touch_majors_[i]; |
| 219 cached_touch_minors_[i] = other.cached_touch_minors_[i]; | |
| 220 cached_orientations_[i] = other.cached_orientations_[i]; | |
| 204 cached_tool_types_[i] = other.cached_tool_types_[i]; | 221 cached_tool_types_[i] = other.cached_tool_types_[i]; |
| 205 } | 222 } |
| 206 } | 223 } |
| 207 | 224 |
| 208 MotionEventAndroid::~MotionEventAndroid() { | 225 MotionEventAndroid::~MotionEventAndroid() { |
| 209 if (should_recycle_) | 226 if (should_recycle_) |
| 210 Java_MotionEvent_recycle(AttachCurrentThread(), event_.obj()); | 227 Java_MotionEvent_recycle(AttachCurrentThread(), event_.obj()); |
| 211 } | 228 } |
| 212 | 229 |
| 213 int MotionEventAndroid::GetId() const { | 230 int MotionEventAndroid::GetId() const { |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 259 } | 276 } |
| 260 | 277 |
| 261 float MotionEventAndroid::GetTouchMajor(size_t pointer_index) const { | 278 float MotionEventAndroid::GetTouchMajor(size_t pointer_index) const { |
| 262 DCHECK_LT(pointer_index, cached_pointer_count_); | 279 DCHECK_LT(pointer_index, cached_pointer_count_); |
| 263 if (pointer_index < MAX_POINTERS_TO_CACHE) | 280 if (pointer_index < MAX_POINTERS_TO_CACHE) |
| 264 return cached_touch_majors_[pointer_index]; | 281 return cached_touch_majors_[pointer_index]; |
| 265 return ToDips(Java_MotionEvent_getTouchMajorF_I( | 282 return ToDips(Java_MotionEvent_getTouchMajorF_I( |
| 266 AttachCurrentThread(), event_.obj(), pointer_index)); | 283 AttachCurrentThread(), event_.obj(), pointer_index)); |
| 267 } | 284 } |
| 268 | 285 |
| 286 float MotionEventAndroid::GetTouchMinor(size_t pointer_index) const { | |
| 287 DCHECK_LT(pointer_index, cached_pointer_count_); | |
| 288 if (pointer_index < MAX_POINTERS_TO_CACHE) | |
| 289 return cached_touch_minors_[pointer_index]; | |
| 290 return ToDips(Java_MotionEvent_getTouchMinorF_I(AttachCurrentThread(), | |
| 291 event_.obj(), pointer_index)); | |
| 292 } | |
| 293 | |
| 294 float MotionEventAndroid::GetOrientation(size_t pointer_index) const { | |
| 295 DCHECK_LT(pointer_index, cached_pointer_count_); | |
| 296 if (pointer_index < MAX_POINTERS_TO_CACHE) | |
| 297 return cached_orientations_[pointer_index]; | |
| 298 return ToValidFloat(Java_MotionEvent_getOrientationF_I( | |
| 299 AttachCurrentThread(), event_.obj(), pointer_index)); | |
| 300 } | |
| 301 | |
| 269 float MotionEventAndroid::GetPressure(size_t pointer_index) const { | 302 float MotionEventAndroid::GetPressure(size_t pointer_index) const { |
| 270 DCHECK_LT(pointer_index, cached_pointer_count_); | 303 DCHECK_LT(pointer_index, cached_pointer_count_); |
| 271 return Java_MotionEvent_getPressureF_I( | 304 return Java_MotionEvent_getPressureF_I( |
| 272 AttachCurrentThread(), event_.obj(), pointer_index); | 305 AttachCurrentThread(), event_.obj(), pointer_index); |
| 273 } | 306 } |
| 274 | 307 |
| 275 base::TimeTicks MotionEventAndroid::GetEventTime() const { | 308 base::TimeTicks MotionEventAndroid::GetEventTime() const { |
| 276 return cached_time_; | 309 return cached_time_; |
| 277 } | 310 } |
| 278 | 311 |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 330 return scoped_ptr<MotionEvent>( | 363 return scoped_ptr<MotionEvent>( |
| 331 new MotionEventAndroid(pix_to_dip_, | 364 new MotionEventAndroid(pix_to_dip_, |
| 332 AttachCurrentThread(), | 365 AttachCurrentThread(), |
| 333 Obtain(GetDownTime(), | 366 Obtain(GetDownTime(), |
| 334 GetEventTime(), | 367 GetEventTime(), |
| 335 MotionEventAndroid::ACTION_CANCEL, | 368 MotionEventAndroid::ACTION_CANCEL, |
| 336 position_pixels.x(), | 369 position_pixels.x(), |
| 337 position_pixels.y()).obj())); | 370 position_pixels.y()).obj())); |
| 338 } | 371 } |
| 339 | 372 |
| 340 float MotionEventAndroid::GetTouchMinor(size_t pointer_index) const { | |
| 341 return ToDips(Java_MotionEvent_getTouchMinorF_I( | |
| 342 AttachCurrentThread(), event_.obj(), pointer_index)); | |
| 343 } | |
| 344 | |
| 345 float MotionEventAndroid::GetOrientation() const { | |
| 346 return Java_MotionEvent_getOrientationF(AttachCurrentThread(), event_.obj()); | |
| 347 } | |
| 348 | |
| 349 base::TimeTicks MotionEventAndroid::GetDownTime() const { | 373 base::TimeTicks MotionEventAndroid::GetDownTime() const { |
| 350 return FromAndroidTime( | 374 return FromAndroidTime( |
| 351 Java_MotionEvent_getDownTime(AttachCurrentThread(), event_.obj())); | 375 Java_MotionEvent_getDownTime(AttachCurrentThread(), event_.obj())); |
| 352 } | 376 } |
| 353 | 377 |
| 378 float MotionEventAndroid::ToValidFloat(float x) const { | |
|
jdduke (slow)
2014/08/29 16:45:08
Nit: This should probably be a free function in th
mustaq
2014/08/29 20:54:01
Done.
| |
| 379 return base::IsNaN(x) ? 0.f : x; | |
| 380 } | |
| 381 | |
| 354 float MotionEventAndroid::ToDips(float pixels) const { | 382 float MotionEventAndroid::ToDips(float pixels) const { |
| 355 return pixels * pix_to_dip_; | 383 return pixels * pix_to_dip_; |
| 356 } | 384 } |
| 357 | 385 |
| 358 gfx::PointF MotionEventAndroid::ToDips(const gfx::PointF& point_pixels) const { | 386 gfx::PointF MotionEventAndroid::ToDips(const gfx::PointF& point_pixels) const { |
| 359 return gfx::ScalePoint(point_pixels, pix_to_dip_); | 387 return gfx::ScalePoint(point_pixels, pix_to_dip_); |
| 360 } | 388 } |
| 361 | 389 |
| 362 // static | 390 // static |
| 363 bool MotionEventAndroid::RegisterMotionEventAndroid(JNIEnv* env) { | 391 bool MotionEventAndroid::RegisterMotionEventAndroid(JNIEnv* env) { |
| (...skipping 17 matching lines...) Expand all Loading... | |
| 381 return Java_MotionEvent_obtainAVME_J_J_I_F_F_I(AttachCurrentThread(), | 409 return Java_MotionEvent_obtainAVME_J_J_I_F_F_I(AttachCurrentThread(), |
| 382 ToAndroidTime(down_time), | 410 ToAndroidTime(down_time), |
| 383 ToAndroidTime(event_time), | 411 ToAndroidTime(event_time), |
| 384 ToAndroidAction(action), | 412 ToAndroidAction(action), |
| 385 x_pixels, | 413 x_pixels, |
| 386 y_pixels, | 414 y_pixels, |
| 387 0); | 415 0); |
| 388 } | 416 } |
| 389 | 417 |
| 390 } // namespace content | 418 } // namespace content |
| OLD | NEW |