| 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 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 55 } | 55 } |
| 56 | 56 |
| 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 float GetMinTouchMajorDipsForTool(int android_tool_type) { |
| 66 switch (android_tool_type) { |
| 67 case TOOL_TYPE_MOUSE: |
| 68 case TOOL_TYPE_STYLUS: |
| 69 return 0.f; |
| 70 default: |
| 71 // This value is somewhat arbitrarily chosen, but was used with success in |
| 72 // the old gesture detection pipeline. |
| 73 return 24.f; |
| 74 }; |
| 75 } |
| 76 |
| 65 } // namespace | 77 } // namespace |
| 66 | 78 |
| 67 MotionEventAndroid::MotionEventAndroid(float pix_to_dip, | 79 MotionEventAndroid::MotionEventAndroid(float pix_to_dip, |
| 68 JNIEnv* env, | 80 JNIEnv* env, |
| 69 jobject event, | 81 jobject event, |
| 70 jlong time_ms, | 82 jlong time_ms, |
| 71 jint android_action, | 83 jint android_action, |
| 72 jint pointer_count, | 84 jint pointer_count, |
| 73 jint history_size, | 85 jint history_size, |
| 74 jint action_index, | 86 jint action_index, |
| 75 jfloat pos_x_0_pixels, | 87 jfloat pos_x_0_pixels, |
| 76 jfloat pos_y_0_pixels, | 88 jfloat pos_y_0_pixels, |
| 77 jfloat pos_x_1_pixels, | 89 jfloat pos_x_1_pixels, |
| 78 jfloat pos_y_1_pixels, | 90 jfloat pos_y_1_pixels, |
| 79 jint pointer_id_0, | 91 jint pointer_id_0, |
| 80 jint pointer_id_1, | 92 jint pointer_id_1, |
| 81 jfloat touch_major_0_pixels, | 93 jfloat touch_major_0_pixels, |
| 82 jfloat touch_major_1_pixels) | 94 jfloat touch_major_1_pixels, |
| 95 jint tool_type) |
| 83 : cached_time_(FromAndroidTime(time_ms)), | 96 : cached_time_(FromAndroidTime(time_ms)), |
| 84 cached_action_(FromAndroidAction(android_action)), | 97 cached_action_(FromAndroidAction(android_action)), |
| 85 cached_pointer_count_(pointer_count), | 98 cached_pointer_count_(pointer_count), |
| 86 cached_history_size_(history_size), | 99 cached_history_size_(history_size), |
| 87 cached_action_index_(action_index), | 100 cached_action_index_(action_index), |
| 88 pix_to_dip_(pix_to_dip), | 101 pix_to_dip_(pix_to_dip), |
| 102 min_touch_major_dips_(GetMinTouchMajorDipsForTool(tool_type)), |
| 89 should_recycle_(false) { | 103 should_recycle_(false) { |
| 90 DCHECK_GT(pointer_count, 0); | 104 DCHECK_GT(pointer_count, 0); |
| 91 DCHECK_GE(history_size, 0); | 105 DCHECK_GE(history_size, 0); |
| 92 | 106 |
| 93 event_.Reset(env, event); | 107 event_.Reset(env, event); |
| 94 DCHECK(event_.obj()); | 108 DCHECK(event_.obj()); |
| 95 | 109 |
| 96 cached_positions_[0] = ToDips(gfx::PointF(pos_x_0_pixels, pos_y_0_pixels)); | 110 cached_positions_[0] = ToDips(gfx::PointF(pos_x_0_pixels, pos_y_0_pixels)); |
| 97 cached_positions_[1] = ToDips(gfx::PointF(pos_x_1_pixels, pos_y_1_pixels)); | 111 cached_positions_[1] = ToDips(gfx::PointF(pos_x_1_pixels, pos_y_1_pixels)); |
| 98 cached_pointer_ids_[0] = pointer_id_0; | 112 cached_pointer_ids_[0] = pointer_id_0; |
| 99 cached_pointer_ids_[1] = pointer_id_1; | 113 cached_pointer_ids_[1] = pointer_id_1; |
| 100 cached_touch_majors_[0] = ToDips(touch_major_0_pixels); | 114 cached_touch_majors_[0] = ToDips(touch_major_0_pixels); |
| 101 cached_touch_majors_[1] = ToDips(touch_major_1_pixels); | 115 cached_touch_majors_[1] = ToDips(touch_major_1_pixels); |
| 102 } | 116 } |
| 103 | 117 |
| 104 MotionEventAndroid::MotionEventAndroid(float pix_to_dip, | 118 MotionEventAndroid::MotionEventAndroid(float pix_to_dip, |
| 119 float min_touch_major_dips_, |
| 105 JNIEnv* env, | 120 JNIEnv* env, |
| 106 jobject event) | 121 jobject event) |
| 107 : cached_time_(FromAndroidTime(Java_MotionEvent_getEventTime(env, event))), | 122 : cached_time_(FromAndroidTime(Java_MotionEvent_getEventTime(env, event))), |
| 108 cached_action_( | 123 cached_action_( |
| 109 FromAndroidAction(Java_MotionEvent_getActionMasked(env, event))), | 124 FromAndroidAction(Java_MotionEvent_getActionMasked(env, event))), |
| 110 cached_pointer_count_(Java_MotionEvent_getPointerCount(env, event)), | 125 cached_pointer_count_(Java_MotionEvent_getPointerCount(env, event)), |
| 111 cached_history_size_(Java_MotionEvent_getHistorySize(env, event)), | 126 cached_history_size_(Java_MotionEvent_getHistorySize(env, event)), |
| 112 cached_action_index_(Java_MotionEvent_getActionIndex(env, event)), | 127 cached_action_index_(Java_MotionEvent_getActionIndex(env, event)), |
| 113 pix_to_dip_(pix_to_dip), | 128 pix_to_dip_(pix_to_dip), |
| 129 min_touch_major_dips_(min_touch_major_dips_), |
| 114 should_recycle_(true) { | 130 should_recycle_(true) { |
| 115 event_.Reset(env, event); | 131 event_.Reset(env, event); |
| 116 DCHECK(event_.obj()); | 132 DCHECK(event_.obj()); |
| 117 | 133 |
| 118 for (size_t i = 0; i < MAX_POINTERS_TO_CACHE; ++i) { | 134 for (size_t i = 0; i < MAX_POINTERS_TO_CACHE; ++i) { |
| 119 if (i < cached_pointer_count_) { | 135 if (i < cached_pointer_count_) { |
| 120 cached_positions_[i] = | 136 cached_positions_[i] = |
| 121 ToDips(gfx::PointF(Java_MotionEvent_getXF_I(env, event, i), | 137 ToDips(gfx::PointF(Java_MotionEvent_getXF_I(env, event, i), |
| 122 Java_MotionEvent_getYF_I(env, event, i))); | 138 Java_MotionEvent_getYF_I(env, event, i))); |
| 123 cached_pointer_ids_[i] = Java_MotionEvent_getPointerId(env, event, i); | 139 cached_pointer_ids_[i] = Java_MotionEvent_getPointerId(env, event, i); |
| 124 cached_touch_majors_[i] = | 140 cached_touch_majors_[i] = |
| 125 ToDips(Java_MotionEvent_getTouchMajorF_I(env, event, i)); | 141 ToDips(Java_MotionEvent_getTouchMajorF_I(env, event, i)); |
| 126 } else { | 142 } else { |
| 127 cached_pointer_ids_[i] = 0; | 143 cached_pointer_ids_[i] = 0; |
| 128 cached_touch_majors_[i] = 0.f; | 144 cached_touch_majors_[i] = 0.f; |
| 129 } | 145 } |
| 130 } | 146 } |
| 131 } | 147 } |
| 132 | 148 |
| 133 MotionEventAndroid::MotionEventAndroid(const MotionEventAndroid& other) | 149 MotionEventAndroid::MotionEventAndroid(const MotionEventAndroid& other) |
| 134 : event_(Obtain(other)), | 150 : event_(Obtain(other)), |
| 135 cached_time_(other.cached_time_), | 151 cached_time_(other.cached_time_), |
| 136 cached_action_(other.cached_action_), | 152 cached_action_(other.cached_action_), |
| 137 cached_pointer_count_(other.cached_pointer_count_), | 153 cached_pointer_count_(other.cached_pointer_count_), |
| 138 cached_history_size_(other.cached_history_size_), | 154 cached_history_size_(other.cached_history_size_), |
| 139 cached_action_index_(other.cached_action_index_), | 155 cached_action_index_(other.cached_action_index_), |
| 140 pix_to_dip_(other.pix_to_dip_), | 156 pix_to_dip_(other.pix_to_dip_), |
| 157 min_touch_major_dips_(other.min_touch_major_dips_), |
| 141 should_recycle_(true) { | 158 should_recycle_(true) { |
| 142 DCHECK(event_.obj()); | 159 DCHECK(event_.obj()); |
| 143 for (size_t i = 0; i < MAX_POINTERS_TO_CACHE; ++i) { | 160 for (size_t i = 0; i < MAX_POINTERS_TO_CACHE; ++i) { |
| 144 cached_positions_[i] = other.cached_positions_[i]; | 161 cached_positions_[i] = other.cached_positions_[i]; |
| 145 cached_pointer_ids_[i] = other.cached_pointer_ids_[i]; | 162 cached_pointer_ids_[i] = other.cached_pointer_ids_[i]; |
| 146 cached_touch_majors_[i] = other.cached_touch_majors_[i]; | 163 cached_touch_majors_[i] = other.cached_touch_majors_[i]; |
| 147 } | 164 } |
| 148 } | 165 } |
| 149 | 166 |
| 150 MotionEventAndroid::~MotionEventAndroid() { | 167 MotionEventAndroid::~MotionEventAndroid() { |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 184 | 201 |
| 185 float MotionEventAndroid::GetY(size_t pointer_index) const { | 202 float MotionEventAndroid::GetY(size_t pointer_index) const { |
| 186 DCHECK_LT(pointer_index, cached_pointer_count_); | 203 DCHECK_LT(pointer_index, cached_pointer_count_); |
| 187 if (pointer_index < MAX_POINTERS_TO_CACHE) | 204 if (pointer_index < MAX_POINTERS_TO_CACHE) |
| 188 return cached_positions_[pointer_index].y(); | 205 return cached_positions_[pointer_index].y(); |
| 189 return ToDips(Java_MotionEvent_getYF_I( | 206 return ToDips(Java_MotionEvent_getYF_I( |
| 190 AttachCurrentThread(), event_.obj(), pointer_index)); | 207 AttachCurrentThread(), event_.obj(), pointer_index)); |
| 191 } | 208 } |
| 192 | 209 |
| 193 float MotionEventAndroid::GetTouchMajor(size_t pointer_index) const { | 210 float MotionEventAndroid::GetTouchMajor(size_t pointer_index) const { |
| 194 DCHECK_LT(pointer_index, cached_pointer_count_); | 211 return std::max(min_touch_major_dips_, GetRawTouchMajor(pointer_index)); |
| 195 if (pointer_index < MAX_POINTERS_TO_CACHE) | |
| 196 return cached_touch_majors_[pointer_index]; | |
| 197 return ToDips(Java_MotionEvent_getTouchMajorF_I( | |
| 198 AttachCurrentThread(), event_.obj(), pointer_index)); | |
| 199 } | 212 } |
| 200 | 213 |
| 201 float MotionEventAndroid::GetPressure(size_t pointer_index) const { | 214 float MotionEventAndroid::GetPressure(size_t pointer_index) const { |
| 202 DCHECK_LT(pointer_index, cached_pointer_count_); | 215 DCHECK_LT(pointer_index, cached_pointer_count_); |
| 203 return Java_MotionEvent_getPressureF_I( | 216 return Java_MotionEvent_getPressureF_I( |
| 204 AttachCurrentThread(), event_.obj(), pointer_index); | 217 AttachCurrentThread(), event_.obj(), pointer_index); |
| 205 } | 218 } |
| 206 | 219 |
| 207 base::TimeTicks MotionEventAndroid::GetEventTime() const { | 220 base::TimeTicks MotionEventAndroid::GetEventTime() const { |
| 208 return cached_time_; | 221 return cached_time_; |
| 209 } | 222 } |
| 210 | 223 |
| 211 size_t MotionEventAndroid::GetHistorySize() const { | 224 size_t MotionEventAndroid::GetHistorySize() const { |
| 212 return cached_history_size_; | 225 return cached_history_size_; |
| 213 } | 226 } |
| 214 | 227 |
| 215 base::TimeTicks MotionEventAndroid::GetHistoricalEventTime( | 228 base::TimeTicks MotionEventAndroid::GetHistoricalEventTime( |
| 216 size_t historical_index) const { | 229 size_t historical_index) const { |
| 217 return FromAndroidTime(Java_MotionEvent_getHistoricalEventTime( | 230 return FromAndroidTime(Java_MotionEvent_getHistoricalEventTime( |
| 218 AttachCurrentThread(), event_.obj(), historical_index)); | 231 AttachCurrentThread(), event_.obj(), historical_index)); |
| 219 } | 232 } |
| 220 | 233 |
| 221 float MotionEventAndroid::GetHistoricalTouchMajor( | 234 float MotionEventAndroid::GetHistoricalTouchMajor( |
| 222 size_t pointer_index, | 235 size_t pointer_index, |
| 223 size_t historical_index) const { | 236 size_t historical_index) const { |
| 224 return ToDips(Java_MotionEvent_getHistoricalTouchMajorF_I_I( | 237 return std::max(min_touch_major_dips_, |
| 225 AttachCurrentThread(), event_.obj(), pointer_index, historical_index)); | 238 ToDips(Java_MotionEvent_getHistoricalTouchMajorF_I_I( |
| 239 AttachCurrentThread(), |
| 240 event_.obj(), |
| 241 pointer_index, |
| 242 historical_index))); |
| 226 } | 243 } |
| 227 | 244 |
| 228 float MotionEventAndroid::GetHistoricalX(size_t pointer_index, | 245 float MotionEventAndroid::GetHistoricalX(size_t pointer_index, |
| 229 size_t historical_index) const { | 246 size_t historical_index) const { |
| 230 return ToDips(Java_MotionEvent_getHistoricalXF_I_I( | 247 return ToDips(Java_MotionEvent_getHistoricalXF_I_I( |
| 231 AttachCurrentThread(), event_.obj(), pointer_index, historical_index)); | 248 AttachCurrentThread(), event_.obj(), pointer_index, historical_index)); |
| 232 } | 249 } |
| 233 | 250 |
| 234 float MotionEventAndroid::GetHistoricalY(size_t pointer_index, | 251 float MotionEventAndroid::GetHistoricalY(size_t pointer_index, |
| 235 size_t historical_index) const { | 252 size_t historical_index) const { |
| 236 return ToDips(Java_MotionEvent_getHistoricalYF_I_I( | 253 return ToDips(Java_MotionEvent_getHistoricalYF_I_I( |
| 237 AttachCurrentThread(), event_.obj(), pointer_index, historical_index)); | 254 AttachCurrentThread(), event_.obj(), pointer_index, historical_index)); |
| 238 } | 255 } |
| 239 | 256 |
| 240 scoped_ptr<ui::MotionEvent> MotionEventAndroid::Clone() const { | 257 scoped_ptr<ui::MotionEvent> MotionEventAndroid::Clone() const { |
| 241 return scoped_ptr<MotionEvent>(new MotionEventAndroid(*this)); | 258 return scoped_ptr<MotionEvent>(new MotionEventAndroid(*this)); |
| 242 } | 259 } |
| 243 | 260 |
| 244 scoped_ptr<ui::MotionEvent> MotionEventAndroid::Cancel() const { | 261 scoped_ptr<ui::MotionEvent> MotionEventAndroid::Cancel() const { |
| 245 // The input coordinates to |MotionEventAndroid| are always in device pixels, | 262 // The input coordinates to |MotionEventAndroid| are always in device pixels, |
| 246 // but the cached coordinates are in DIPs. | 263 // but the cached coordinates are in DIPs. |
| 247 const gfx::PointF position_pixels = | 264 const gfx::PointF position_pixels = |
| 248 gfx::ScalePoint(cached_positions_[0], 1.f / pix_to_dip_); | 265 gfx::ScalePoint(cached_positions_[0], 1.f / pix_to_dip_); |
| 249 return scoped_ptr<MotionEvent>( | 266 return scoped_ptr<MotionEvent>( |
| 250 new MotionEventAndroid(pix_to_dip_, | 267 new MotionEventAndroid(pix_to_dip_, |
| 268 min_touch_major_dips_, |
| 251 AttachCurrentThread(), | 269 AttachCurrentThread(), |
| 252 Obtain(GetDownTime(), | 270 Obtain(GetDownTime(), |
| 253 GetEventTime(), | 271 GetEventTime(), |
| 254 MotionEventAndroid::ACTION_CANCEL, | 272 MotionEventAndroid::ACTION_CANCEL, |
| 255 position_pixels.x(), | 273 position_pixels.x(), |
| 256 position_pixels.y()).obj())); | 274 position_pixels.y()).obj())); |
| 257 } | 275 } |
| 258 | 276 |
| 259 float MotionEventAndroid::GetTouchMinor(size_t pointer_index) const { | 277 float MotionEventAndroid::GetRawTouchMajor(size_t pointer_index) const { |
| 278 DCHECK_LT(pointer_index, cached_pointer_count_); |
| 279 if (pointer_index < MAX_POINTERS_TO_CACHE) |
| 280 return cached_touch_majors_[pointer_index]; |
| 281 return ToDips(Java_MotionEvent_getTouchMajorF_I( |
| 282 AttachCurrentThread(), event_.obj(), pointer_index)); |
| 283 } |
| 284 |
| 285 float MotionEventAndroid::GetRawTouchMinor(size_t pointer_index) const { |
| 260 return ToDips(Java_MotionEvent_getTouchMinorF_I( | 286 return ToDips(Java_MotionEvent_getTouchMinorF_I( |
| 261 AttachCurrentThread(), event_.obj(), pointer_index)); | 287 AttachCurrentThread(), event_.obj(), pointer_index)); |
| 262 } | 288 } |
| 263 | 289 |
| 264 float MotionEventAndroid::GetOrientation() const { | 290 float MotionEventAndroid::GetOrientation() const { |
| 265 return Java_MotionEvent_getOrientationF(AttachCurrentThread(), event_.obj()); | 291 return Java_MotionEvent_getOrientationF(AttachCurrentThread(), event_.obj()); |
| 266 } | 292 } |
| 267 | 293 |
| 268 base::TimeTicks MotionEventAndroid::GetDownTime() const { | 294 base::TimeTicks MotionEventAndroid::GetDownTime() const { |
| 269 return FromAndroidTime( | 295 return FromAndroidTime( |
| (...skipping 30 matching lines...) Expand all Loading... |
| 300 return Java_MotionEvent_obtainAVME_J_J_I_F_F_I(AttachCurrentThread(), | 326 return Java_MotionEvent_obtainAVME_J_J_I_F_F_I(AttachCurrentThread(), |
| 301 ToAndroidTime(down_time), | 327 ToAndroidTime(down_time), |
| 302 ToAndroidTime(event_time), | 328 ToAndroidTime(event_time), |
| 303 ToAndroidAction(action), | 329 ToAndroidAction(action), |
| 304 x_pixels, | 330 x_pixels, |
| 305 y_pixels, | 331 y_pixels, |
| 306 0); | 332 0); |
| 307 } | 333 } |
| 308 | 334 |
| 309 } // namespace content | 335 } // namespace content |
| OLD | NEW |