OLD | NEW |
| 1 |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 2 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 3 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 4 // found in the LICENSE file. |
4 | 5 |
5 #ifndef CONTENT_BROWSER_RENDERER_HOST_INPUT_MOTION_EVENT_ANDROID_H_ | 6 #ifndef CONTENT_BROWSER_RENDERER_HOST_INPUT_MOTION_EVENT_ANDROID_H_ |
6 #define CONTENT_BROWSER_RENDERER_HOST_INPUT_MOTION_EVENT_ANDROID_H_ | 7 #define CONTENT_BROWSER_RENDERER_HOST_INPUT_MOTION_EVENT_ANDROID_H_ |
7 | 8 |
8 #include <jni.h> | 9 #include <jni.h> |
9 | 10 |
10 #include "base/android/scoped_java_ref.h" | 11 #include "base/android/scoped_java_ref.h" |
11 #include "base/memory/scoped_ptr.h" | 12 #include "base/memory/scoped_ptr.h" |
12 #include "base/time/time.h" | 13 #include "base/time/time.h" |
13 #include "content/common/content_export.h" | 14 #include "content/common/content_export.h" |
14 #include "ui/events/gesture_detection/motion_event.h" | 15 #include "ui/events/gesture_detection/motion_event.h" |
15 #include "ui/gfx/geometry/point_f.h" | 16 #include "ui/gfx/geometry/point_f.h" |
16 | 17 |
17 namespace content { | 18 namespace content { |
18 | 19 |
19 // Implementation of ui::MotionEvent wrapping a native Android MotionEvent. | 20 // Implementation of ui::MotionEvent wrapping a native Android MotionEvent. |
20 // All *input* coordinates are in device pixels (as with Android MotionEvent), | 21 // All *input* coordinates are in device pixels (as with Android MotionEvent), |
21 // while all *output* coordinates are in DIPs (as with WebTouchEvent). | 22 // while all *output* coordinates are in DIPs (as with WebTouchEvent). |
22 class CONTENT_EXPORT MotionEventAndroid : public ui::MotionEvent { | 23 class CONTENT_EXPORT MotionEventAndroid : public ui::MotionEvent { |
23 public: | 24 public: |
| 25 struct Pointer { |
| 26 Pointer(jint id, |
| 27 jfloat pos_x_pixels, |
| 28 jfloat pos_y_pixels, |
| 29 jfloat touch_major_pixels, |
| 30 jfloat touch_minor_pixels, |
| 31 jfloat orientation_rad, |
| 32 jint tool_type); |
| 33 jint id; |
| 34 jfloat pos_x_pixels; |
| 35 jfloat pos_y_pixels; |
| 36 jfloat touch_major_pixels; |
| 37 jfloat touch_minor_pixels; |
| 38 jfloat orientation_rad; |
| 39 jint tool_type; |
| 40 }; |
| 41 |
24 // Forcing the caller to provide all cached values upon construction | 42 // Forcing the caller to provide all cached values upon construction |
25 // eliminates the need to perform a JNI call to retrieve values individually. | 43 // eliminates the need to perform a JNI call to retrieve values individually. |
26 MotionEventAndroid(float pix_to_dip, | 44 MotionEventAndroid(float pix_to_dip, |
27 JNIEnv* env, | 45 JNIEnv* env, |
28 jobject event, | 46 jobject event, |
29 jlong time_ms, | 47 jlong time_ms, |
30 jint android_action, | 48 jint android_action, |
31 jint pointer_count, | 49 jint pointer_count, |
32 jint history_size, | 50 jint history_size, |
33 jint action_index, | 51 jint action_index, |
34 jfloat pos_x_0_pixels, | |
35 jfloat pos_y_0_pixels, | |
36 jfloat pos_x_1_pixels, | |
37 jfloat pos_y_1_pixels, | |
38 jint pointer_id_0, | |
39 jint pointer_id_1, | |
40 jfloat touch_major_0_pixels, | |
41 jfloat touch_major_1_pixels, | |
42 jfloat touch_minor_0_pixels, | |
43 jfloat touch_minor_1_pixels, | |
44 jfloat orientation_0_rad, | |
45 jfloat orientation_1_rad, | |
46 jfloat raw_pos_x_pixels, | |
47 jfloat raw_pos_y_pixels, | |
48 jint android_tool_type_0, | |
49 jint android_tool_type_1, | |
50 jint android_button_state, | 52 jint android_button_state, |
51 jint meta_state); | 53 jint meta_state, |
| 54 jfloat raw_offset_x_pixels, |
| 55 jfloat raw_offset_y_pixels, |
| 56 const Pointer& pointer0, |
| 57 const Pointer& pointer1); |
52 virtual ~MotionEventAndroid(); | 58 virtual ~MotionEventAndroid(); |
53 | 59 |
54 // ui::MotionEvent methods. | 60 // ui::MotionEvent methods. |
55 virtual int GetId() const override; | 61 virtual int GetId() const override; |
56 virtual Action GetAction() const override; | 62 virtual Action GetAction() const override; |
57 virtual int GetActionIndex() const override; | 63 virtual int GetActionIndex() const override; |
58 virtual size_t GetPointerCount() const override; | 64 virtual size_t GetPointerCount() const override; |
59 virtual int GetPointerId(size_t pointer_index) const override; | 65 virtual int GetPointerId(size_t pointer_index) const override; |
60 virtual float GetX(size_t pointer_index) const override; | 66 virtual float GetX(size_t pointer_index) const override; |
61 virtual float GetY(size_t pointer_index) const override; | 67 virtual float GetY(size_t pointer_index) const override; |
62 virtual float GetRawX(size_t pointer_index) const override; | 68 virtual float GetRawX(size_t pointer_index) const override; |
63 virtual float GetRawY(size_t pointer_index) const override; | 69 virtual float GetRawY(size_t pointer_index) const override; |
64 virtual float GetTouchMajor(size_t pointer_index) const override; | 70 virtual float GetTouchMajor(size_t pointer_index) const override; |
65 virtual float GetTouchMinor(size_t pointer_index) const override; | 71 virtual float GetTouchMinor(size_t pointer_index) const override; |
66 virtual float GetOrientation(size_t pointer_index) const override; | 72 virtual float GetOrientation(size_t pointer_index) const override; |
67 virtual float GetPressure(size_t pointer_index) const override; | 73 virtual float GetPressure(size_t pointer_index) const override; |
68 virtual base::TimeTicks GetEventTime() const override; | 74 virtual base::TimeTicks GetEventTime() const override; |
69 virtual size_t GetHistorySize() const override; | 75 virtual size_t GetHistorySize() const override; |
70 virtual base::TimeTicks GetHistoricalEventTime( | 76 virtual base::TimeTicks GetHistoricalEventTime( |
71 size_t historical_index) const override; | 77 size_t historical_index) const override; |
72 virtual float GetHistoricalTouchMajor(size_t pointer_index, | 78 virtual float GetHistoricalTouchMajor(size_t pointer_index, |
73 size_t historical_index) const override; | 79 size_t historical_index) const override; |
74 virtual float GetHistoricalX(size_t pointer_index, | 80 virtual float GetHistoricalX(size_t pointer_index, |
75 size_t historical_index) const override; | 81 size_t historical_index) const override; |
76 virtual float GetHistoricalY(size_t pointer_index, | 82 virtual float GetHistoricalY(size_t pointer_index, |
77 size_t historical_index) const override; | 83 size_t historical_index) const override; |
78 virtual ToolType GetToolType(size_t pointer_index) const override; | 84 virtual ToolType GetToolType(size_t pointer_index) const override; |
79 virtual int GetButtonState() const override; | 85 virtual int GetButtonState() const override; |
80 virtual int GetFlags() const override; | 86 virtual int GetFlags() const override; |
81 virtual scoped_ptr<MotionEvent> Clone() const override; | |
82 virtual scoped_ptr<MotionEvent> Cancel() const override; | |
83 | |
84 // Additional Android MotionEvent methods. | |
85 base::TimeTicks GetDownTime() const; | |
86 | 87 |
87 static bool RegisterMotionEventAndroid(JNIEnv* env); | 88 static bool RegisterMotionEventAndroid(JNIEnv* env); |
88 | 89 |
89 static base::android::ScopedJavaLocalRef<jobject> Obtain( | |
90 const MotionEventAndroid& event); | |
91 static base::android::ScopedJavaLocalRef<jobject> Obtain( | |
92 base::TimeTicks down_time, | |
93 base::TimeTicks event_time, | |
94 Action action, | |
95 float x_pixels, | |
96 float y_pixels); | |
97 | |
98 private: | 90 private: |
99 MotionEventAndroid(); | 91 struct CachedPointer; |
100 MotionEventAndroid(float pix_to_dip, JNIEnv* env, jobject event); | |
101 MotionEventAndroid(const MotionEventAndroid&); | |
102 MotionEventAndroid& operator=(const MotionEventAndroid&); | |
103 | 92 |
104 float ToDips(float pixels) const; | 93 float ToDips(float pixels) const; |
105 gfx::PointF ToDips(const gfx::PointF& pixels) const; | 94 CachedPointer FromAndroidPointer(const Pointer& pointer) const; |
106 | 95 |
107 // Cache pointer coords, id's and major lengths for the most common | 96 // Cache pointer coords, id's and major lengths for the most common |
108 // touch-related scenarios, i.e., scrolling and pinching. This prevents | 97 // touch-related scenarios, i.e., scrolling and pinching. This prevents |
109 // redundant JNI fetches for the same bits. | 98 // redundant JNI fetches for the same bits. |
110 enum { MAX_POINTERS_TO_CACHE = 2 }; | 99 enum { MAX_POINTERS_TO_CACHE = 2 }; |
111 | 100 |
112 // The Java reference to the underlying MotionEvent. | 101 // The Java reference to the underlying MotionEvent. |
113 base::android::ScopedJavaGlobalRef<jobject> event_; | 102 base::android::ScopedJavaGlobalRef<jobject> event_; |
114 | 103 |
115 base::TimeTicks cached_time_; | |
116 Action cached_action_; | |
117 size_t cached_pointer_count_; | |
118 size_t cached_history_size_; | |
119 int cached_action_index_; | |
120 gfx::PointF cached_positions_[MAX_POINTERS_TO_CACHE]; | |
121 int cached_pointer_ids_[MAX_POINTERS_TO_CACHE]; | |
122 float cached_touch_majors_[MAX_POINTERS_TO_CACHE]; | |
123 float cached_touch_minors_[MAX_POINTERS_TO_CACHE]; | |
124 float cached_orientations_[MAX_POINTERS_TO_CACHE]; | |
125 gfx::Vector2dF cached_raw_position_offset_; | |
126 ToolType cached_tool_types_[MAX_POINTERS_TO_CACHE]; | |
127 int cached_button_state_; | |
128 int cached_flags_; | |
129 | |
130 // Used to convert pixel coordinates from the Java-backed MotionEvent to | 104 // Used to convert pixel coordinates from the Java-backed MotionEvent to |
131 // DIP coordinates cached/returned by the MotionEventAndroid. | 105 // DIP coordinates cached/returned by the MotionEventAndroid. |
132 const float pix_to_dip_; | 106 const float pix_to_dip_; |
133 | 107 |
134 // Whether |event_| should be recycled on destruction. This will only be true | 108 const base::TimeTicks cached_time_; |
135 // for those events generated via |Obtain(...)|. | 109 const Action cached_action_; |
136 bool should_recycle_; | 110 const size_t cached_pointer_count_; |
| 111 const size_t cached_history_size_; |
| 112 const int cached_action_index_; |
| 113 const int cached_button_state_; |
| 114 const int cached_flags_; |
| 115 const gfx::Vector2dF cached_raw_position_offset_; |
| 116 struct CachedPointer { |
| 117 CachedPointer(); |
| 118 int id; |
| 119 gfx::PointF position; |
| 120 float touch_major; |
| 121 float touch_minor; |
| 122 float orientation; |
| 123 ToolType tool_type; |
| 124 } cached_pointers_[MAX_POINTERS_TO_CACHE]; |
| 125 |
| 126 DISALLOW_COPY_AND_ASSIGN(MotionEventAndroid); |
137 }; | 127 }; |
138 | 128 |
139 } // namespace content | 129 } // namespace content |
140 | 130 |
141 #endif // CONTENT_BROWSER_RENDERER_HOST_INPUT_MOTION_EVENT_ANDROID_H_ | 131 #endif // CONTENT_BROWSER_RENDERER_HOST_INPUT_MOTION_EVENT_ANDROID_H_ |
OLD | NEW |