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 | 15 |
16 #include <android/keycodes.h> | |
15 #include <android/looper.h> | 17 #include <android/looper.h> |
16 #include <android/native_window_jni.h> | 18 #include <android/native_window_jni.h> |
17 | 19 |
18 #include "../Application.h" | 20 #include "../Application.h" |
19 #include "SkTypes.h" | 21 #include "SkTypes.h" |
20 #include "SkUtils.h" | 22 #include "SkUtils.h" |
21 #include "Window_android.h" | 23 #include "Window_android.h" |
22 | 24 |
23 namespace sk_app { | 25 namespace sk_app { |
24 | 26 |
25 static const int LOOPER_ID_MESSAGEPIPE = 1; | 27 static const int LOOPER_ID_MESSAGEPIPE = 1; |
26 | 28 |
29 static const std::unordered_map<int, Window::Key> ANDROID_TO_WINDOW_KEYMAP( | |
30 {{AKEYCODE_SOFT_LEFT, Window::Key::kLeft}, {AKEYCODE_SOFT_RIGHT, Window::Key ::kRight}}); | |
djsollen
2016/05/11 19:05:25
order these with one per line so that they are eas
liyuqian
2016/05/11 19:11:24
Done.
| |
31 | |
27 void* pthread_main(void* arg); | 32 void* pthread_main(void* arg); |
28 | 33 |
29 SkiaAndroidApp::SkiaAndroidApp() { | 34 SkiaAndroidApp::SkiaAndroidApp() { |
30 fNativeWindow = nullptr; | 35 fNativeWindow = nullptr; |
31 pthread_create(&fThread, nullptr, pthread_main, this); | 36 pthread_create(&fThread, nullptr, pthread_main, this); |
32 } | 37 } |
33 | 38 |
34 SkiaAndroidApp::~SkiaAndroidApp() { | 39 SkiaAndroidApp::~SkiaAndroidApp() { |
35 if (fWindow) { | 40 if (fWindow) { |
36 fWindow->detach(); | 41 fWindow->detach(); |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
83 auto window_android = (Window_android*)skiaAndroidApp->fWindow; | 88 auto window_android = (Window_android*)skiaAndroidApp->fWindow; |
84 window_android->initDisplay(skiaAndroidApp->fNativeWindow); | 89 window_android->initDisplay(skiaAndroidApp->fNativeWindow); |
85 skiaAndroidApp->paintIfNeeded(); | 90 skiaAndroidApp->paintIfNeeded(); |
86 break; | 91 break; |
87 } | 92 } |
88 case kSurfaceChanged: { | 93 case kSurfaceChanged: { |
89 SkASSERT(message.fNativeWindow == skiaAndroidApp->fNativeWindow && | 94 SkASSERT(message.fNativeWindow == skiaAndroidApp->fNativeWindow && |
90 message.fNativeWindow); | 95 message.fNativeWindow); |
91 int width = ANativeWindow_getWidth(skiaAndroidApp->fNativeWindow); | 96 int width = ANativeWindow_getWidth(skiaAndroidApp->fNativeWindow); |
92 int height = ANativeWindow_getHeight(skiaAndroidApp->fNativeWindow); | 97 int height = ANativeWindow_getHeight(skiaAndroidApp->fNativeWindow); |
93 skiaAndroidApp->fWindow->onResize(width, height); | |
94 auto window_android = (Window_android*)skiaAndroidApp->fWindow; | 98 auto window_android = (Window_android*)skiaAndroidApp->fWindow; |
95 window_android->setContentRect(0, 0, width, height); | 99 window_android->setContentRect(0, 0, width, height); |
96 skiaAndroidApp->paintIfNeeded(); | 100 skiaAndroidApp->paintIfNeeded(); |
97 break; | 101 break; |
98 } | 102 } |
99 case kSurfaceDestroyed: { | 103 case kSurfaceDestroyed: { |
100 if (skiaAndroidApp->fNativeWindow) { | 104 if (skiaAndroidApp->fNativeWindow) { |
101 auto window_android = (Window_android*)skiaAndroidApp->fWindow; | 105 auto window_android = (Window_android*)skiaAndroidApp->fWindow; |
102 window_android->onDisplayDestroyed(); | 106 window_android->onDisplayDestroyed(); |
103 ANativeWindow_release(skiaAndroidApp->fNativeWindow); | 107 ANativeWindow_release(skiaAndroidApp->fNativeWindow); |
104 skiaAndroidApp->fNativeWindow = nullptr; | 108 skiaAndroidApp->fNativeWindow = nullptr; |
105 } | 109 } |
106 break; | 110 break; |
107 } | 111 } |
112 case kKeyPressed: { | |
113 auto it = ANDROID_TO_WINDOW_KEYMAP.find(message.keycode); | |
114 SkASSERT(it != ANDROID_TO_WINDOW_KEYMAP.end()); | |
115 // No modifier is supported so far | |
116 skiaAndroidApp->fWindow->onKey(it->second, Window::kDown_InputState, 0); | |
117 skiaAndroidApp->fWindow->onKey(it->second, Window::kUp_InputState, 0 ); | |
118 break; | |
119 } | |
108 default: { | 120 default: { |
109 // do nothing | 121 // do nothing |
110 } | 122 } |
111 } | 123 } |
112 | 124 |
113 return 1; // continue receiving callbacks | 125 return 1; // continue receiving callbacks |
114 } | 126 } |
115 | 127 |
116 void* pthread_main(void* arg) { | 128 void* pthread_main(void* arg) { |
117 SkDebugf("pthread_main begins"); | 129 SkDebugf("pthread_main begins"); |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
164 message.fNativeWindow = ANativeWindow_fromSurface(env, surface); | 176 message.fNativeWindow = ANativeWindow_fromSurface(env, surface); |
165 skiaAndroidApp->postMessage(message); | 177 skiaAndroidApp->postMessage(message); |
166 } | 178 } |
167 | 179 |
168 extern "C" JNIEXPORT void JNICALL Java_org_skia_viewer_ViewerActivity_onSurfaceD estroyed( | 180 extern "C" JNIEXPORT void JNICALL Java_org_skia_viewer_ViewerActivity_onSurfaceD estroyed( |
169 JNIEnv* env, jobject activity, jlong handle) { | 181 JNIEnv* env, jobject activity, jlong handle) { |
170 auto skiaAndroidApp = (SkiaAndroidApp*)handle; | 182 auto skiaAndroidApp = (SkiaAndroidApp*)handle; |
171 skiaAndroidApp->postMessage(Message(kSurfaceDestroyed)); | 183 skiaAndroidApp->postMessage(Message(kSurfaceDestroyed)); |
172 } | 184 } |
173 | 185 |
186 extern "C" JNIEXPORT void JNICALL Java_org_skia_viewer_ViewerActivity_onKeyPress ed(JNIEnv* env, | |
187 jobject activity, | |
188 jlong handle, | |
189 jint keycode) { | |
190 auto skiaAndroidApp = (SkiaAndroidApp*)handle; | |
191 Message message(kKeyPressed); | |
192 message.keycode = keycode; | |
193 skiaAndroidApp->postMessage(message); | |
194 } | |
195 | |
174 } // namespace sk_app | 196 } // namespace sk_app |
OLD | NEW |