| 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 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 107 jint history_size, | 107 jint history_size, |
| 108 jint action_index, | 108 jint action_index, |
| 109 jfloat pos_x_0_pixels, | 109 jfloat pos_x_0_pixels, |
| 110 jfloat pos_y_0_pixels, | 110 jfloat pos_y_0_pixels, |
| 111 jfloat pos_x_1_pixels, | 111 jfloat pos_x_1_pixels, |
| 112 jfloat pos_y_1_pixels, | 112 jfloat pos_y_1_pixels, |
| 113 jint pointer_id_0, | 113 jint pointer_id_0, |
| 114 jint pointer_id_1, | 114 jint pointer_id_1, |
| 115 jfloat touch_major_0_pixels, | 115 jfloat touch_major_0_pixels, |
| 116 jfloat touch_major_1_pixels, | 116 jfloat touch_major_1_pixels, |
| 117 jfloat touch_minor_0_pixels, |
| 118 jfloat touch_minor_1_pixels, |
| 119 jfloat orientation_0_rad, |
| 120 jfloat orientation_1_rad, |
| 117 jfloat raw_pos_x_pixels, | 121 jfloat raw_pos_x_pixels, |
| 118 jfloat raw_pos_y_pixels, | 122 jfloat raw_pos_y_pixels, |
| 119 jint android_tool_type_0, | 123 jint android_tool_type_0, |
| 120 jint android_tool_type_1, | 124 jint android_tool_type_1, |
| 121 jint android_button_state) | 125 jint android_button_state) |
| 122 : cached_time_(FromAndroidTime(time_ms)), | 126 : cached_time_(FromAndroidTime(time_ms)), |
| 123 cached_action_(FromAndroidAction(android_action)), | 127 cached_action_(FromAndroidAction(android_action)), |
| 124 cached_pointer_count_(pointer_count), | 128 cached_pointer_count_(pointer_count), |
| 125 cached_history_size_(history_size), | 129 cached_history_size_(history_size), |
| 126 cached_action_index_(action_index), | 130 cached_action_index_(action_index), |
| 127 cached_button_state_(FromAndroidButtonState(android_button_state)), | 131 cached_button_state_(FromAndroidButtonState(android_button_state)), |
| 128 pix_to_dip_(pix_to_dip), | 132 pix_to_dip_(pix_to_dip), |
| 129 should_recycle_(false) { | 133 should_recycle_(false) { |
| 130 DCHECK_GT(pointer_count, 0); | 134 DCHECK_GT(pointer_count, 0); |
| 131 DCHECK_GE(history_size, 0); | 135 DCHECK_GE(history_size, 0); |
| 132 | 136 |
| 133 event_.Reset(env, event); | 137 event_.Reset(env, event); |
| 134 DCHECK(event_.obj()); | 138 DCHECK(event_.obj()); |
| 135 | 139 |
| 136 cached_positions_[0] = ToDips(gfx::PointF(pos_x_0_pixels, pos_y_0_pixels)); | 140 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)); | 141 cached_positions_[1] = ToDips(gfx::PointF(pos_x_1_pixels, pos_y_1_pixels)); |
| 138 cached_pointer_ids_[0] = pointer_id_0; | 142 cached_pointer_ids_[0] = pointer_id_0; |
| 139 cached_pointer_ids_[1] = pointer_id_1; | 143 cached_pointer_ids_[1] = pointer_id_1; |
| 140 cached_touch_majors_[0] = ToDips(touch_major_0_pixels); | 144 cached_touch_majors_[0] = ToDips(touch_major_0_pixels); |
| 141 cached_touch_majors_[1] = ToDips(touch_major_1_pixels); | 145 cached_touch_majors_[1] = ToDips(touch_major_1_pixels); |
| 146 cached_touch_minors_[0] = ToDips(touch_minor_0_pixels); |
| 147 cached_touch_minors_[1] = ToDips(touch_minor_1_pixels); |
| 148 cached_orientations_[0] = orientation_0_rad; |
| 149 cached_orientations_[1] = orientation_1_rad; |
| 142 cached_raw_position_offset_ = | 150 cached_raw_position_offset_ = |
| 143 ToDips(gfx::PointF(raw_pos_x_pixels, raw_pos_y_pixels)) - | 151 ToDips(gfx::PointF(raw_pos_x_pixels, raw_pos_y_pixels)) - |
| 144 cached_positions_[0]; | 152 cached_positions_[0]; |
| 145 cached_tool_types_[0] = FromAndroidToolType(android_tool_type_0); | 153 cached_tool_types_[0] = FromAndroidToolType(android_tool_type_0); |
| 146 cached_tool_types_[1] = FromAndroidToolType(android_tool_type_1); | 154 cached_tool_types_[1] = FromAndroidToolType(android_tool_type_1); |
| 147 } | 155 } |
| 148 | 156 |
| 149 MotionEventAndroid::MotionEventAndroid(float pix_to_dip, | 157 MotionEventAndroid::MotionEventAndroid(float pix_to_dip, |
| 150 JNIEnv* env, | 158 JNIEnv* env, |
| 151 jobject event) | 159 jobject event) |
| (...skipping 11 matching lines...) Expand all Loading... |
| 163 DCHECK(event_.obj()); | 171 DCHECK(event_.obj()); |
| 164 | 172 |
| 165 for (size_t i = 0; i < MAX_POINTERS_TO_CACHE; ++i) { | 173 for (size_t i = 0; i < MAX_POINTERS_TO_CACHE; ++i) { |
| 166 if (i < cached_pointer_count_) { | 174 if (i < cached_pointer_count_) { |
| 167 cached_positions_[i] = | 175 cached_positions_[i] = |
| 168 ToDips(gfx::PointF(Java_MotionEvent_getXF_I(env, event, i), | 176 ToDips(gfx::PointF(Java_MotionEvent_getXF_I(env, event, i), |
| 169 Java_MotionEvent_getYF_I(env, event, i))); | 177 Java_MotionEvent_getYF_I(env, event, i))); |
| 170 cached_pointer_ids_[i] = Java_MotionEvent_getPointerId(env, event, i); | 178 cached_pointer_ids_[i] = Java_MotionEvent_getPointerId(env, event, i); |
| 171 cached_touch_majors_[i] = | 179 cached_touch_majors_[i] = |
| 172 ToDips(Java_MotionEvent_getTouchMajorF_I(env, event, i)); | 180 ToDips(Java_MotionEvent_getTouchMajorF_I(env, event, i)); |
| 181 cached_touch_minors_[i] = |
| 182 ToDips(Java_MotionEvent_getTouchMinorF_I(env, event, i)); |
| 183 cached_orientations_[i] = |
| 184 Java_MotionEvent_getOrientationF_I(env, event, i); |
| 173 cached_tool_types_[i] = | 185 cached_tool_types_[i] = |
| 174 FromAndroidToolType(Java_MotionEvent_getToolType(env, event, i)); | 186 FromAndroidToolType(Java_MotionEvent_getToolType(env, event, i)); |
| 175 } else { | 187 } else { |
| 176 cached_pointer_ids_[i] = 0; | 188 cached_pointer_ids_[i] = 0; |
| 177 cached_touch_majors_[i] = 0.f; | 189 cached_touch_majors_[i] = 0.f; |
| 190 cached_touch_minors_[i] = 0.f; |
| 191 cached_orientations_[i] = 0.f; |
| 178 cached_tool_types_[i] = MotionEvent::TOOL_TYPE_UNKNOWN; | 192 cached_tool_types_[i] = MotionEvent::TOOL_TYPE_UNKNOWN; |
| 179 } | 193 } |
| 180 } | 194 } |
| 181 | 195 |
| 182 cached_raw_position_offset_ = | 196 cached_raw_position_offset_ = |
| 183 ToDips(gfx::PointF(Java_MotionEvent_getRawX(env, event), | 197 ToDips(gfx::PointF(Java_MotionEvent_getRawX(env, event), |
| 184 Java_MotionEvent_getRawY(env, event))) - | 198 Java_MotionEvent_getRawY(env, event))) - |
| 185 cached_positions_[0]; | 199 cached_positions_[0]; |
| 186 } | 200 } |
| 187 | 201 |
| 188 MotionEventAndroid::MotionEventAndroid(const MotionEventAndroid& other) | 202 MotionEventAndroid::MotionEventAndroid(const MotionEventAndroid& other) |
| 189 : event_(Obtain(other)), | 203 : event_(Obtain(other)), |
| 190 cached_time_(other.cached_time_), | 204 cached_time_(other.cached_time_), |
| 191 cached_action_(other.cached_action_), | 205 cached_action_(other.cached_action_), |
| 192 cached_pointer_count_(other.cached_pointer_count_), | 206 cached_pointer_count_(other.cached_pointer_count_), |
| 193 cached_history_size_(other.cached_history_size_), | 207 cached_history_size_(other.cached_history_size_), |
| 194 cached_action_index_(other.cached_action_index_), | 208 cached_action_index_(other.cached_action_index_), |
| 195 cached_raw_position_offset_(other.cached_raw_position_offset_), | 209 cached_raw_position_offset_(other.cached_raw_position_offset_), |
| 196 cached_button_state_(other.cached_button_state_), | 210 cached_button_state_(other.cached_button_state_), |
| 197 pix_to_dip_(other.pix_to_dip_), | 211 pix_to_dip_(other.pix_to_dip_), |
| 198 should_recycle_(true) { | 212 should_recycle_(true) { |
| 199 DCHECK(event_.obj()); | 213 DCHECK(event_.obj()); |
| 200 for (size_t i = 0; i < MAX_POINTERS_TO_CACHE; ++i) { | 214 for (size_t i = 0; i < MAX_POINTERS_TO_CACHE; ++i) { |
| 201 cached_positions_[i] = other.cached_positions_[i]; | 215 cached_positions_[i] = other.cached_positions_[i]; |
| 202 cached_pointer_ids_[i] = other.cached_pointer_ids_[i]; | 216 cached_pointer_ids_[i] = other.cached_pointer_ids_[i]; |
| 203 cached_touch_majors_[i] = other.cached_touch_majors_[i]; | 217 cached_touch_majors_[i] = other.cached_touch_majors_[i]; |
| 218 cached_touch_minors_[i] = other.cached_touch_minors_[i]; |
| 219 cached_orientations_[i] = other.cached_orientations_[i]; |
| 204 cached_tool_types_[i] = other.cached_tool_types_[i]; | 220 cached_tool_types_[i] = other.cached_tool_types_[i]; |
| 205 } | 221 } |
| 206 } | 222 } |
| 207 | 223 |
| 208 MotionEventAndroid::~MotionEventAndroid() { | 224 MotionEventAndroid::~MotionEventAndroid() { |
| 209 if (should_recycle_) | 225 if (should_recycle_) |
| 210 Java_MotionEvent_recycle(AttachCurrentThread(), event_.obj()); | 226 Java_MotionEvent_recycle(AttachCurrentThread(), event_.obj()); |
| 211 } | 227 } |
| 212 | 228 |
| 213 int MotionEventAndroid::GetId() const { | 229 int MotionEventAndroid::GetId() const { |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 259 } | 275 } |
| 260 | 276 |
| 261 float MotionEventAndroid::GetTouchMajor(size_t pointer_index) const { | 277 float MotionEventAndroid::GetTouchMajor(size_t pointer_index) const { |
| 262 DCHECK_LT(pointer_index, cached_pointer_count_); | 278 DCHECK_LT(pointer_index, cached_pointer_count_); |
| 263 if (pointer_index < MAX_POINTERS_TO_CACHE) | 279 if (pointer_index < MAX_POINTERS_TO_CACHE) |
| 264 return cached_touch_majors_[pointer_index]; | 280 return cached_touch_majors_[pointer_index]; |
| 265 return ToDips(Java_MotionEvent_getTouchMajorF_I( | 281 return ToDips(Java_MotionEvent_getTouchMajorF_I( |
| 266 AttachCurrentThread(), event_.obj(), pointer_index)); | 282 AttachCurrentThread(), event_.obj(), pointer_index)); |
| 267 } | 283 } |
| 268 | 284 |
| 285 float MotionEventAndroid::GetTouchMinor(size_t pointer_index) const { |
| 286 DCHECK_LT(pointer_index, cached_pointer_count_); |
| 287 if (pointer_index < MAX_POINTERS_TO_CACHE) |
| 288 return cached_touch_minors_[pointer_index]; |
| 289 return ToDips(Java_MotionEvent_getTouchMinorF_I(AttachCurrentThread(), |
| 290 event_.obj(), pointer_index)); |
| 291 } |
| 292 |
| 293 float MotionEventAndroid::GetOrientation(size_t pointer_index) const { |
| 294 DCHECK_LT(pointer_index, cached_pointer_count_); |
| 295 if (pointer_index < MAX_POINTERS_TO_CACHE) |
| 296 return cached_orientations_[pointer_index]; |
| 297 float orientation = Java_MotionEvent_getOrientationF_I( |
| 298 AttachCurrentThread(), event_.obj(), pointer_index); |
| 299 return isnan(orientation) ? 0 : orientation; |
| 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 |
| 279 size_t MotionEventAndroid::GetHistorySize() const { | 312 size_t MotionEventAndroid::GetHistorySize() const { |
| 280 return cached_history_size_; | 313 return cached_history_size_; |
| 281 } | 314 } |
| 282 | 315 |
| 283 base::TimeTicks MotionEventAndroid::GetHistoricalEventTime( | 316 base::TimeTicks MotionEventAndroid::GetHistoricalEventTime( |
| 284 size_t historical_index) const { | 317 size_t historical_index) const { |
| 285 return FromAndroidTime(Java_MotionEvent_getHistoricalEventTime( | 318 return FromAndroidTime(Java_MotionEvent_getHistoricalEventTime( |
| 286 AttachCurrentThread(), event_.obj(), historical_index)); | 319 AttachCurrentThread(), event_.obj(), historical_index)); |
| 287 } | 320 } |
| 288 | 321 |
| 289 float MotionEventAndroid::GetHistoricalTouchMajor( | 322 float MotionEventAndroid::GetHistoricalTouchMajor( |
| 290 size_t pointer_index, | 323 size_t pointer_index, |
| 291 size_t historical_index) const { | 324 size_t historical_index) const { |
| 292 return ToDips(Java_MotionEvent_getHistoricalTouchMajorF_I_I( | 325 return ToDips(Java_MotionEvent_getHistoricalTouchMajorF_I_I( |
| 293 AttachCurrentThread(), event_.obj(), pointer_index, historical_index)); | 326 AttachCurrentThread(), event_.obj(), pointer_index, historical_index)); |
| 294 } | 327 } |
| 295 | 328 |
| 329 float MotionEventAndroid::GetHistoricalTouchMinor( |
| 330 size_t pointer_index, |
| 331 size_t historical_index) const { |
| 332 return ToDips(Java_MotionEvent_getHistoricalTouchMinorF_I_I( |
| 333 AttachCurrentThread(), event_.obj(), pointer_index, historical_index)); |
| 334 } |
| 335 |
| 336 float MotionEventAndroid::GetHistoricalOrientation( |
| 337 size_t pointer_index, |
| 338 size_t historical_index) const { |
| 339 return Java_MotionEvent_getHistoricalOrientationF_I_I( |
| 340 AttachCurrentThread(), event_.obj(), pointer_index, historical_index); |
| 341 } |
| 342 |
| 296 float MotionEventAndroid::GetHistoricalX(size_t pointer_index, | 343 float MotionEventAndroid::GetHistoricalX(size_t pointer_index, |
| 297 size_t historical_index) const { | 344 size_t historical_index) const { |
| 298 return ToDips(Java_MotionEvent_getHistoricalXF_I_I( | 345 return ToDips(Java_MotionEvent_getHistoricalXF_I_I( |
| 299 AttachCurrentThread(), event_.obj(), pointer_index, historical_index)); | 346 AttachCurrentThread(), event_.obj(), pointer_index, historical_index)); |
| 300 } | 347 } |
| 301 | 348 |
| 302 float MotionEventAndroid::GetHistoricalY(size_t pointer_index, | 349 float MotionEventAndroid::GetHistoricalY(size_t pointer_index, |
| 303 size_t historical_index) const { | 350 size_t historical_index) const { |
| 304 return ToDips(Java_MotionEvent_getHistoricalYF_I_I( | 351 return ToDips(Java_MotionEvent_getHistoricalYF_I_I( |
| 305 AttachCurrentThread(), event_.obj(), pointer_index, historical_index)); | 352 AttachCurrentThread(), event_.obj(), pointer_index, historical_index)); |
| (...skipping 24 matching lines...) Expand all Loading... |
| 330 return scoped_ptr<MotionEvent>( | 377 return scoped_ptr<MotionEvent>( |
| 331 new MotionEventAndroid(pix_to_dip_, | 378 new MotionEventAndroid(pix_to_dip_, |
| 332 AttachCurrentThread(), | 379 AttachCurrentThread(), |
| 333 Obtain(GetDownTime(), | 380 Obtain(GetDownTime(), |
| 334 GetEventTime(), | 381 GetEventTime(), |
| 335 MotionEventAndroid::ACTION_CANCEL, | 382 MotionEventAndroid::ACTION_CANCEL, |
| 336 position_pixels.x(), | 383 position_pixels.x(), |
| 337 position_pixels.y()).obj())); | 384 position_pixels.y()).obj())); |
| 338 } | 385 } |
| 339 | 386 |
| 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 { | 387 base::TimeTicks MotionEventAndroid::GetDownTime() const { |
| 350 return FromAndroidTime( | 388 return FromAndroidTime( |
| 351 Java_MotionEvent_getDownTime(AttachCurrentThread(), event_.obj())); | 389 Java_MotionEvent_getDownTime(AttachCurrentThread(), event_.obj())); |
| 352 } | 390 } |
| 353 | 391 |
| 354 float MotionEventAndroid::ToDips(float pixels) const { | 392 float MotionEventAndroid::ToDips(float pixels) const { |
| 355 return pixels * pix_to_dip_; | 393 return pixels * pix_to_dip_; |
| 356 } | 394 } |
| 357 | 395 |
| 358 gfx::PointF MotionEventAndroid::ToDips(const gfx::PointF& point_pixels) const { | 396 gfx::PointF MotionEventAndroid::ToDips(const gfx::PointF& point_pixels) const { |
| (...skipping 22 matching lines...) Expand all Loading... |
| 381 return Java_MotionEvent_obtainAVME_J_J_I_F_F_I(AttachCurrentThread(), | 419 return Java_MotionEvent_obtainAVME_J_J_I_F_F_I(AttachCurrentThread(), |
| 382 ToAndroidTime(down_time), | 420 ToAndroidTime(down_time), |
| 383 ToAndroidTime(event_time), | 421 ToAndroidTime(event_time), |
| 384 ToAndroidAction(action), | 422 ToAndroidAction(action), |
| 385 x_pixels, | 423 x_pixels, |
| 386 y_pixels, | 424 y_pixels, |
| 387 0); | 425 0); |
| 388 } | 426 } |
| 389 | 427 |
| 390 } // namespace content | 428 } // namespace content |
| OLD | NEW |