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 25 matching lines...) Expand all Loading... |
69 void SkiaAndroidApp::postMessage(const Message& message) const { | 79 void SkiaAndroidApp::postMessage(const Message& message) const { |
70 auto writeSize = write(fPipes[1], &message, sizeof(message)); | 80 auto writeSize = write(fPipes[1], &message, sizeof(message)); |
71 SkASSERT(writeSize == sizeof(message)); | 81 SkASSERT(writeSize == sizeof(message)); |
72 } | 82 } |
73 | 83 |
74 void SkiaAndroidApp::readMessage(Message* message) const { | 84 void SkiaAndroidApp::readMessage(Message* message) const { |
75 auto readSize = read(fPipes[0], message, sizeof(Message)); | 85 auto readSize = read(fPipes[0], message, sizeof(Message)); |
76 SkASSERT(readSize == sizeof(Message)); | 86 SkASSERT(readSize == sizeof(Message)); |
77 } | 87 } |
78 | 88 |
| 89 void SkiaAndroidApp::inval() { |
| 90 SkAutoMutexAcquire ama(fMutex); |
| 91 if (!fIsContentInvalidated) { |
| 92 postMessage(Message(kContentInvalidated)); |
| 93 fIsContentInvalidated = true; |
| 94 } |
| 95 } |
| 96 |
79 int SkiaAndroidApp::message_callback(int fd, int events, void* data) { | 97 int SkiaAndroidApp::message_callback(int fd, int events, void* data) { |
80 auto skiaAndroidApp = (SkiaAndroidApp*)data; | 98 auto skiaAndroidApp = (SkiaAndroidApp*)data; |
81 Message message; | 99 Message message; |
82 skiaAndroidApp->readMessage(&message); | 100 skiaAndroidApp->readMessage(&message); |
83 SkDebugf("message_callback %d", message.fType); | 101 SkDebugf("message_callback %d", message.fType); |
84 SkASSERT(message.fType != kUndefined); | 102 SkASSERT(message.fType != kUndefined); |
85 | 103 |
86 switch (message.fType) { | 104 switch (message.fType) { |
87 case kDestroyApp: { | 105 case kDestroyApp: { |
88 delete skiaAndroidApp; | 106 delete skiaAndroidApp; |
89 pthread_exit(nullptr); | 107 pthread_exit(nullptr); |
90 return 0; | 108 return 0; |
91 } | 109 } |
92 case kContentInvalidated: { | 110 case kContentInvalidated: { |
| 111 SkAutoMutexAcquire ama(skiaAndroidApp->fMutex); |
| 112 skiaAndroidApp->fIsContentInvalidated = false; |
93 skiaAndroidApp->paintIfNeeded(); | 113 skiaAndroidApp->paintIfNeeded(); |
94 break; | 114 break; |
95 } | 115 } |
96 case kSurfaceCreated: { | 116 case kSurfaceCreated: { |
97 SkASSERT(!skiaAndroidApp->fNativeWindow && message.fNativeWindow); | 117 SkASSERT(!skiaAndroidApp->fNativeWindow && message.fNativeWindow); |
98 skiaAndroidApp->fNativeWindow = message.fNativeWindow; | 118 skiaAndroidApp->fNativeWindow = message.fNativeWindow; |
99 auto window_android = (Window_android*)skiaAndroidApp->fWindow; | 119 auto window_android = (Window_android*)skiaAndroidApp->fWindow; |
100 window_android->initDisplay(skiaAndroidApp->fNativeWindow); | 120 window_android->initDisplay(skiaAndroidApp->fNativeWindow); |
101 skiaAndroidApp->paintIfNeeded(); | 121 skiaAndroidApp->paintIfNeeded(); |
102 break; | 122 break; |
(...skipping 11 matching lines...) Expand all Loading... |
114 case kSurfaceDestroyed: { | 134 case kSurfaceDestroyed: { |
115 if (skiaAndroidApp->fNativeWindow) { | 135 if (skiaAndroidApp->fNativeWindow) { |
116 auto window_android = (Window_android*)skiaAndroidApp->fWindow; | 136 auto window_android = (Window_android*)skiaAndroidApp->fWindow; |
117 window_android->onDisplayDestroyed(); | 137 window_android->onDisplayDestroyed(); |
118 ANativeWindow_release(skiaAndroidApp->fNativeWindow); | 138 ANativeWindow_release(skiaAndroidApp->fNativeWindow); |
119 skiaAndroidApp->fNativeWindow = nullptr; | 139 skiaAndroidApp->fNativeWindow = nullptr; |
120 } | 140 } |
121 break; | 141 break; |
122 } | 142 } |
123 case kKeyPressed: { | 143 case kKeyPressed: { |
124 auto it = ANDROID_TO_WINDOW_KEYMAP.find(message.keycode); | 144 auto it = ANDROID_TO_WINDOW_KEYMAP.find(message.fKeycode); |
125 SkASSERT(it != ANDROID_TO_WINDOW_KEYMAP.end()); | 145 SkASSERT(it != ANDROID_TO_WINDOW_KEYMAP.end()); |
126 // No modifier is supported so far | 146 // No modifier is supported so far |
127 skiaAndroidApp->fWindow->onKey(it->second, Window::kDown_InputState,
0); | 147 skiaAndroidApp->fWindow->onKey(it->second, Window::kDown_InputState,
0); |
128 skiaAndroidApp->fWindow->onKey(it->second, Window::kUp_InputState, 0
); | 148 skiaAndroidApp->fWindow->onKey(it->second, Window::kUp_InputState, 0
); |
129 break; | 149 break; |
130 } | 150 } |
| 151 case kTouched: { |
| 152 auto it = ANDROID_TO_WINDOW_STATEMAP.find(message.fTouchState); |
| 153 SkASSERT(it != ANDROID_TO_WINDOW_STATEMAP.end()); |
| 154 skiaAndroidApp->fWindow->onTouch(message.fTouchOwner, it->second, me
ssage.fTouchX, |
| 155 message.fTouchY); |
| 156 break; |
| 157 } |
131 default: { | 158 default: { |
132 // do nothing | 159 // do nothing |
133 } | 160 } |
134 } | 161 } |
135 | 162 |
136 return 1; // continue receiving callbacks | 163 return 1; // continue receiving callbacks |
137 } | 164 } |
138 | 165 |
139 void* SkiaAndroidApp::pthread_main(void* arg) { | 166 void* SkiaAndroidApp::pthread_main(void* arg) { |
140 SkDebugf("pthread_main begins"); | 167 SkDebugf("pthread_main begins"); |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
196 auto skiaAndroidApp = (SkiaAndroidApp*)handle; | 223 auto skiaAndroidApp = (SkiaAndroidApp*)handle; |
197 skiaAndroidApp->postMessage(Message(kSurfaceDestroyed)); | 224 skiaAndroidApp->postMessage(Message(kSurfaceDestroyed)); |
198 } | 225 } |
199 | 226 |
200 extern "C" JNIEXPORT void JNICALL Java_org_skia_viewer_ViewerActivity_onKeyPress
ed(JNIEnv* env, | 227 extern "C" JNIEXPORT void JNICALL Java_org_skia_viewer_ViewerActivity_onKeyPress
ed(JNIEnv* env, |
201
jobject activity, | 228
jobject activity, |
202
jlong handle, | 229
jlong handle, |
203
jint keycode) { | 230
jint keycode) { |
204 auto skiaAndroidApp = (SkiaAndroidApp*)handle; | 231 auto skiaAndroidApp = (SkiaAndroidApp*)handle; |
205 Message message(kKeyPressed); | 232 Message message(kKeyPressed); |
206 message.keycode = keycode; | 233 message.fKeycode = keycode; |
207 skiaAndroidApp->postMessage(message); | 234 skiaAndroidApp->postMessage(message); |
208 } | 235 } |
209 | 236 |
| 237 extern "C" JNIEXPORT void JNICALL Java_org_skia_viewer_ViewerActivity_onTouched( |
| 238 JNIEnv* env, jobject activity, jlong handle, jint owner, jfloat x, jfloat y,
jint state) { |
| 239 auto skiaAndroidApp = (SkiaAndroidApp*)handle; |
| 240 Message message(kTouched); |
| 241 message.fTouchOwner = owner; |
| 242 message.fTouchState = state; |
| 243 message.fTouchX = x; |
| 244 message.fTouchY = y; |
| 245 skiaAndroidApp->postMessage(message); |
| 246 } |
| 247 |
210 } // namespace sk_app | 248 } // namespace sk_app |
OLD | NEW |