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