OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2016 Google Inc. | 2 * Copyright 2016 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #include "surface_glue_android.h" | 8 #include "surface_glue_android.h" |
9 | 9 |
10 #include <jni.h> | 10 #include <jni.h> |
11 #include <pthread.h> | 11 #include <pthread.h> |
12 #include <stdio.h> | 12 #include <stdio.h> |
13 #include <unistd.h> | 13 #include <unistd.h> |
14 #include <unordered_map> | 14 #include <unordered_map> |
15 | 15 |
| 16 #include <android/input.h> |
16 #include <android/keycodes.h> | 17 #include <android/keycodes.h> |
17 #include <android/looper.h> | 18 #include <android/looper.h> |
18 #include <android/native_window_jni.h> | 19 #include <android/native_window_jni.h> |
19 | 20 |
20 #include "../Application.h" | 21 #include "../Application.h" |
21 #include "SkTypes.h" | 22 #include "SkTypes.h" |
22 #include "SkUtils.h" | 23 #include "SkUtils.h" |
23 #include "Window_android.h" | 24 #include "Window_android.h" |
24 | 25 |
25 namespace sk_app { | 26 namespace sk_app { |
26 | 27 |
27 static const int LOOPER_ID_MESSAGEPIPE = 1; | 28 static const int LOOPER_ID_MESSAGEPIPE = 1; |
28 | 29 |
29 static const std::unordered_map<int, Window::Key> ANDROID_TO_WINDOW_KEYMAP({ | 30 static const std::unordered_map<int, Window::Key> ANDROID_TO_WINDOW_KEYMAP({ |
30 {AKEYCODE_SOFT_LEFT, Window::Key::kLeft}, | 31 {AKEYCODE_SOFT_LEFT, Window::Key::kLeft}, |
31 {AKEYCODE_SOFT_RIGHT, Window::Key::kRight} | 32 {AKEYCODE_SOFT_RIGHT, Window::Key::kRight} |
32 }); | 33 }); |
33 | 34 |
| 35 static const std::unordered_map<int, Window::InputState> ANDROID_TO_WINDOW_STATE
MAP({ |
| 36 {AMOTION_EVENT_ACTION_DOWN, Window::kDown_InputState}, |
| 37 {AMOTION_EVENT_ACTION_POINTER_DOWN, Window::kDown_InputState}, |
| 38 {AMOTION_EVENT_ACTION_UP, Window::kUp_InputState}, |
| 39 {AMOTION_EVENT_ACTION_POINTER_UP, Window::kUp_InputState}, |
| 40 {AMOTION_EVENT_ACTION_MOVE, Window::kMove_InputState}, |
| 41 {AMOTION_EVENT_ACTION_CANCEL, Window::kUp_InputState}, |
| 42 }); |
| 43 |
34 SkiaAndroidApp::SkiaAndroidApp(JNIEnv* env, jobject androidApp) { | 44 SkiaAndroidApp::SkiaAndroidApp(JNIEnv* env, jobject androidApp) { |
35 env->GetJavaVM(&fJavaVM); | 45 env->GetJavaVM(&fJavaVM); |
36 fAndroidApp = env->NewGlobalRef(androidApp); | 46 fAndroidApp = env->NewGlobalRef(androidApp); |
37 jclass cls = env->GetObjectClass(fAndroidApp); | 47 jclass cls = env->GetObjectClass(fAndroidApp); |
38 fSetTitleMethodID = env->GetMethodID(cls, "setTitle", "(Ljava/lang/String;)V
"); | 48 fSetTitleMethodID = env->GetMethodID(cls, "setTitle", "(Ljava/lang/String;)V
"); |
39 fNativeWindow = nullptr; | 49 fNativeWindow = nullptr; |
40 pthread_create(&fThread, nullptr, pthread_main, this); | 50 pthread_create(&fThread, nullptr, pthread_main, this); |
41 } | 51 } |
42 | 52 |
43 SkiaAndroidApp::~SkiaAndroidApp() { | 53 SkiaAndroidApp::~SkiaAndroidApp() { |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
83 SkDebugf("message_callback %d", message.fType); | 93 SkDebugf("message_callback %d", message.fType); |
84 SkASSERT(message.fType != kUndefined); | 94 SkASSERT(message.fType != kUndefined); |
85 | 95 |
86 switch (message.fType) { | 96 switch (message.fType) { |
87 case kDestroyApp: { | 97 case kDestroyApp: { |
88 delete skiaAndroidApp; | 98 delete skiaAndroidApp; |
89 pthread_exit(nullptr); | 99 pthread_exit(nullptr); |
90 return 0; | 100 return 0; |
91 } | 101 } |
92 case kContentInvalidated: { | 102 case kContentInvalidated: { |
| 103 skiaAndroidApp->fIsContentInvalidated = false; |
93 skiaAndroidApp->paintIfNeeded(); | 104 skiaAndroidApp->paintIfNeeded(); |
94 break; | 105 break; |
95 } | 106 } |
96 case kSurfaceCreated: { | 107 case kSurfaceCreated: { |
97 SkASSERT(!skiaAndroidApp->fNativeWindow && message.fNativeWindow); | 108 SkASSERT(!skiaAndroidApp->fNativeWindow && message.fNativeWindow); |
98 skiaAndroidApp->fNativeWindow = message.fNativeWindow; | 109 skiaAndroidApp->fNativeWindow = message.fNativeWindow; |
99 auto window_android = (Window_android*)skiaAndroidApp->fWindow; | 110 auto window_android = (Window_android*)skiaAndroidApp->fWindow; |
100 window_android->initDisplay(skiaAndroidApp->fNativeWindow); | 111 window_android->initDisplay(skiaAndroidApp->fNativeWindow); |
101 skiaAndroidApp->paintIfNeeded(); | 112 skiaAndroidApp->paintIfNeeded(); |
102 break; | 113 break; |
(...skipping 11 matching lines...) Expand all Loading... |
114 case kSurfaceDestroyed: { | 125 case kSurfaceDestroyed: { |
115 if (skiaAndroidApp->fNativeWindow) { | 126 if (skiaAndroidApp->fNativeWindow) { |
116 auto window_android = (Window_android*)skiaAndroidApp->fWindow; | 127 auto window_android = (Window_android*)skiaAndroidApp->fWindow; |
117 window_android->onDisplayDestroyed(); | 128 window_android->onDisplayDestroyed(); |
118 ANativeWindow_release(skiaAndroidApp->fNativeWindow); | 129 ANativeWindow_release(skiaAndroidApp->fNativeWindow); |
119 skiaAndroidApp->fNativeWindow = nullptr; | 130 skiaAndroidApp->fNativeWindow = nullptr; |
120 } | 131 } |
121 break; | 132 break; |
122 } | 133 } |
123 case kKeyPressed: { | 134 case kKeyPressed: { |
124 auto it = ANDROID_TO_WINDOW_KEYMAP.find(message.keycode); | 135 auto it = ANDROID_TO_WINDOW_KEYMAP.find(message.fKeycode); |
125 SkASSERT(it != ANDROID_TO_WINDOW_KEYMAP.end()); | 136 SkASSERT(it != ANDROID_TO_WINDOW_KEYMAP.end()); |
126 // No modifier is supported so far | 137 // No modifier is supported so far |
127 skiaAndroidApp->fWindow->onKey(it->second, Window::kDown_InputState,
0); | 138 skiaAndroidApp->fWindow->onKey(it->second, Window::kDown_InputState,
0); |
128 skiaAndroidApp->fWindow->onKey(it->second, Window::kUp_InputState, 0
); | 139 skiaAndroidApp->fWindow->onKey(it->second, Window::kUp_InputState, 0
); |
129 break; | 140 break; |
130 } | 141 } |
| 142 case kTouched: { |
| 143 auto it = ANDROID_TO_WINDOW_STATEMAP.find(message.fTouchState); |
| 144 SkASSERT(it != ANDROID_TO_WINDOW_STATEMAP.end()); |
| 145 skiaAndroidApp->fWindow->onTouch(message.fTouchOwner, it->second, me
ssage.fTouchX, |
| 146 message.fTouchY); |
| 147 break; |
| 148 } |
131 default: { | 149 default: { |
132 // do nothing | 150 // do nothing |
133 } | 151 } |
134 } | 152 } |
135 | 153 |
136 return 1; // continue receiving callbacks | 154 return 1; // continue receiving callbacks |
137 } | 155 } |
138 | 156 |
139 void* SkiaAndroidApp::pthread_main(void* arg) { | 157 void* SkiaAndroidApp::pthread_main(void* arg) { |
140 SkDebugf("pthread_main begins"); | 158 SkDebugf("pthread_main begins"); |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
196 auto skiaAndroidApp = (SkiaAndroidApp*)handle; | 214 auto skiaAndroidApp = (SkiaAndroidApp*)handle; |
197 skiaAndroidApp->postMessage(Message(kSurfaceDestroyed)); | 215 skiaAndroidApp->postMessage(Message(kSurfaceDestroyed)); |
198 } | 216 } |
199 | 217 |
200 extern "C" JNIEXPORT void JNICALL Java_org_skia_viewer_ViewerActivity_onKeyPress
ed(JNIEnv* env, | 218 extern "C" JNIEXPORT void JNICALL Java_org_skia_viewer_ViewerActivity_onKeyPress
ed(JNIEnv* env, |
201
jobject activity, | 219
jobject activity, |
202
jlong handle, | 220
jlong handle, |
203
jint keycode) { | 221
jint keycode) { |
204 auto skiaAndroidApp = (SkiaAndroidApp*)handle; | 222 auto skiaAndroidApp = (SkiaAndroidApp*)handle; |
205 Message message(kKeyPressed); | 223 Message message(kKeyPressed); |
206 message.keycode = keycode; | 224 message.fKeycode = keycode; |
207 skiaAndroidApp->postMessage(message); | 225 skiaAndroidApp->postMessage(message); |
208 } | 226 } |
209 | 227 |
| 228 extern "C" JNIEXPORT void JNICALL Java_org_skia_viewer_ViewerActivity_onTouched( |
| 229 JNIEnv* env, jobject activity, jlong handle, jint owner, jfloat x, jfloat y,
jint state) { |
| 230 auto skiaAndroidApp = (SkiaAndroidApp*)handle; |
| 231 Message message(kTouched); |
| 232 message.fTouchOwner = owner; |
| 233 message.fTouchState = state; |
| 234 message.fTouchX = x; |
| 235 message.fTouchY = y; |
| 236 skiaAndroidApp->postMessage(message); |
| 237 } |
| 238 |
210 } // namespace sk_app | 239 } // namespace sk_app |
OLD | NEW |