Chromium Code Reviews| 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/keycodes.h> | 16 #include <android/keycodes.h> |
| 17 #include <android/looper.h> | 17 #include <android/looper.h> |
| 18 #include <android/native_window_jni.h> | 18 #include <android/native_window_jni.h> |
| 19 #include <android/input.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_UP, Window::kUp_InputState}, | |
| 38 {AMOTION_EVENT_ACTION_MOVE, Window::kMove_InputState}, | |
| 39 {AMOTION_EVENT_ACTION_CANCEL, Window::kUp_InputState}, | |
| 40 }); | |
| 41 | |
| 34 SkiaAndroidApp::SkiaAndroidApp(JNIEnv* env, jobject androidApp) { | 42 SkiaAndroidApp::SkiaAndroidApp(JNIEnv* env, jobject androidApp) { |
| 35 env->GetJavaVM(&fJavaVM); | 43 env->GetJavaVM(&fJavaVM); |
| 36 fAndroidApp = env->NewGlobalRef(androidApp); | 44 fAndroidApp = env->NewGlobalRef(androidApp); |
| 37 jclass cls = env->GetObjectClass(fAndroidApp); | 45 jclass cls = env->GetObjectClass(fAndroidApp); |
| 38 fSetTitleMethodID = env->GetMethodID(cls, "setTitle", "(Ljava/lang/String;)V "); | 46 fSetTitleMethodID = env->GetMethodID(cls, "setTitle", "(Ljava/lang/String;)V "); |
| 39 fNativeWindow = nullptr; | 47 fNativeWindow = nullptr; |
| 40 pthread_create(&fThread, nullptr, pthread_main, this); | 48 pthread_create(&fThread, nullptr, pthread_main, this); |
| 41 } | 49 } |
| 42 | 50 |
| 43 SkiaAndroidApp::~SkiaAndroidApp() { | 51 SkiaAndroidApp::~SkiaAndroidApp() { |
| (...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 114 case kSurfaceDestroyed: { | 122 case kSurfaceDestroyed: { |
| 115 if (skiaAndroidApp->fNativeWindow) { | 123 if (skiaAndroidApp->fNativeWindow) { |
| 116 auto window_android = (Window_android*)skiaAndroidApp->fWindow; | 124 auto window_android = (Window_android*)skiaAndroidApp->fWindow; |
| 117 window_android->onDisplayDestroyed(); | 125 window_android->onDisplayDestroyed(); |
| 118 ANativeWindow_release(skiaAndroidApp->fNativeWindow); | 126 ANativeWindow_release(skiaAndroidApp->fNativeWindow); |
| 119 skiaAndroidApp->fNativeWindow = nullptr; | 127 skiaAndroidApp->fNativeWindow = nullptr; |
| 120 } | 128 } |
| 121 break; | 129 break; |
| 122 } | 130 } |
| 123 case kKeyPressed: { | 131 case kKeyPressed: { |
| 124 auto it = ANDROID_TO_WINDOW_KEYMAP.find(message.keycode); | 132 auto it = ANDROID_TO_WINDOW_KEYMAP.find(message.fKeycode); |
| 125 SkASSERT(it != ANDROID_TO_WINDOW_KEYMAP.end()); | 133 SkASSERT(it != ANDROID_TO_WINDOW_KEYMAP.end()); |
| 126 // No modifier is supported so far | 134 // No modifier is supported so far |
| 127 skiaAndroidApp->fWindow->onKey(it->second, Window::kDown_InputState, 0); | 135 skiaAndroidApp->fWindow->onKey(it->second, Window::kDown_InputState, 0); |
| 128 skiaAndroidApp->fWindow->onKey(it->second, Window::kUp_InputState, 0 ); | 136 skiaAndroidApp->fWindow->onKey(it->second, Window::kUp_InputState, 0 ); |
| 129 break; | 137 break; |
| 130 } | 138 } |
| 139 case kTouched: { | |
| 140 auto it = ANDROID_TO_WINDOW_STATEMAP.find(message.fTouchState); | |
| 141 SkASSERT(it != ANDROID_TO_WINDOW_STATEMAP.end()); | |
| 142 skiaAndroidApp->fWindow->onTouch(message.fTouchOwner, it->second, me ssage.fTouchX, message.fTouchY); | |
| 143 break; | |
| 144 } | |
| 131 default: { | 145 default: { |
| 132 // do nothing | 146 // do nothing |
| 133 } | 147 } |
| 134 } | 148 } |
| 135 | 149 |
| 136 return 1; // continue receiving callbacks | 150 return 1; // continue receiving callbacks |
| 137 } | 151 } |
| 138 | 152 |
| 139 void* SkiaAndroidApp::pthread_main(void* arg) { | 153 void* SkiaAndroidApp::pthread_main(void* arg) { |
| 140 SkDebugf("pthread_main begins"); | 154 SkDebugf("pthread_main begins"); |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 196 auto skiaAndroidApp = (SkiaAndroidApp*)handle; | 210 auto skiaAndroidApp = (SkiaAndroidApp*)handle; |
| 197 skiaAndroidApp->postMessage(Message(kSurfaceDestroyed)); | 211 skiaAndroidApp->postMessage(Message(kSurfaceDestroyed)); |
| 198 } | 212 } |
| 199 | 213 |
| 200 extern "C" JNIEXPORT void JNICALL Java_org_skia_viewer_ViewerActivity_onKeyPress ed(JNIEnv* env, | 214 extern "C" JNIEXPORT void JNICALL Java_org_skia_viewer_ViewerActivity_onKeyPress ed(JNIEnv* env, |
| 201 jobject activity, | 215 jobject activity, |
| 202 jlong handle, | 216 jlong handle, |
| 203 jint keycode) { | 217 jint keycode) { |
| 204 auto skiaAndroidApp = (SkiaAndroidApp*)handle; | 218 auto skiaAndroidApp = (SkiaAndroidApp*)handle; |
| 205 Message message(kKeyPressed); | 219 Message message(kKeyPressed); |
| 206 message.keycode = keycode; | 220 message.fKeycode = keycode; |
| 207 skiaAndroidApp->postMessage(message); | 221 skiaAndroidApp->postMessage(message); |
| 208 } | 222 } |
| 209 | 223 |
| 224 extern "C" JNIEXPORT void JNICALL Java_org_skia_viewer_ViewerActivity_onTouched( JNIEnv* env, | |
| 225 jobject activity, jlong handle, jint owner, jfloat x, jfloat y, jint state) { | |
| 226 auto skiaAndroidApp = (SkiaAndroidApp*)handle; | |
| 227 Message message(kTouched); | |
| 228 message.fTouchOwner = owner; | |
| 229 message.fTouchState = state; | |
| 230 message.fTouchX = x; | |
| 231 message.fTouchY = y; | |
| 232 skiaAndroidApp->postMessage(message); | |
|
djsollen
2016/05/16 18:16:00
do you need to post a message for this or can you
liyuqian
2016/05/16 18:32:37
I tend to defer as much work as possible to my own
| |
| 233 } | |
| 234 | |
| 210 } // namespace sk_app | 235 } // namespace sk_app |
| OLD | NEW |