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> |
(...skipping 13 matching lines...) Expand all Loading... |
24 | 24 |
25 namespace sk_app { | 25 namespace sk_app { |
26 | 26 |
27 static const int LOOPER_ID_MESSAGEPIPE = 1; | 27 static const int LOOPER_ID_MESSAGEPIPE = 1; |
28 | 28 |
29 static const std::unordered_map<int, Window::Key> ANDROID_TO_WINDOW_KEYMAP({ | 29 static const std::unordered_map<int, Window::Key> ANDROID_TO_WINDOW_KEYMAP({ |
30 {AKEYCODE_SOFT_LEFT, Window::Key::kLeft}, | 30 {AKEYCODE_SOFT_LEFT, Window::Key::kLeft}, |
31 {AKEYCODE_SOFT_RIGHT, Window::Key::kRight} | 31 {AKEYCODE_SOFT_RIGHT, Window::Key::kRight} |
32 }); | 32 }); |
33 | 33 |
34 void* pthread_main(void* arg); | 34 SkiaAndroidApp::SkiaAndroidApp(JNIEnv* env, jobject androidApp) { |
35 | 35 env->GetJavaVM(&fJavaVM); |
36 SkiaAndroidApp::SkiaAndroidApp() { | 36 fAndroidApp = env->NewGlobalRef(androidApp); |
| 37 jclass cls = env->GetObjectClass(fAndroidApp); |
| 38 fSetTitleMethodID = env->GetMethodID(cls, "setTitle", "(Ljava/lang/String;)V
"); |
37 fNativeWindow = nullptr; | 39 fNativeWindow = nullptr; |
38 pthread_create(&fThread, nullptr, pthread_main, this); | 40 pthread_create(&fThread, nullptr, pthread_main, this); |
39 } | 41 } |
40 | 42 |
41 SkiaAndroidApp::~SkiaAndroidApp() { | 43 SkiaAndroidApp::~SkiaAndroidApp() { |
| 44 fPThreadEnv->DeleteGlobalRef(fAndroidApp); |
42 if (fWindow) { | 45 if (fWindow) { |
43 fWindow->detach(); | 46 fWindow->detach(); |
44 } | 47 } |
45 if (fNativeWindow) { | 48 if (fNativeWindow) { |
46 ANativeWindow_release(fNativeWindow); | 49 ANativeWindow_release(fNativeWindow); |
47 fNativeWindow = nullptr; | 50 fNativeWindow = nullptr; |
48 } | 51 } |
49 if (fApp) { | 52 if (fApp) { |
50 delete fApp; | 53 delete fApp; |
51 } | 54 } |
52 } | 55 } |
53 | 56 |
| 57 void SkiaAndroidApp::setTitle(const char* title) const { |
| 58 jstring titleString = fPThreadEnv->NewStringUTF(title); |
| 59 fPThreadEnv->CallVoidMethod(fAndroidApp, fSetTitleMethodID, titleString); |
| 60 fPThreadEnv->DeleteLocalRef(titleString); |
| 61 } |
| 62 |
54 void SkiaAndroidApp::paintIfNeeded() { | 63 void SkiaAndroidApp::paintIfNeeded() { |
55 if (fNativeWindow && fWindow) { | 64 if (fNativeWindow && fWindow) { |
56 fWindow->onPaint(); | 65 fWindow->onPaint(); |
57 } | 66 } |
58 } | 67 } |
59 | 68 |
60 void SkiaAndroidApp::postMessage(const Message& message) { | 69 void SkiaAndroidApp::postMessage(const Message& message) const { |
61 auto writeSize = write(fPipes[1], &message, sizeof(message)); | 70 auto writeSize = write(fPipes[1], &message, sizeof(message)); |
62 SkASSERT(writeSize == sizeof(message)); | 71 SkASSERT(writeSize == sizeof(message)); |
63 } | 72 } |
64 | 73 |
65 void SkiaAndroidApp::readMessage(Message* message) { | 74 void SkiaAndroidApp::readMessage(Message* message) const { |
66 auto readSize = read(fPipes[0], message, sizeof(Message)); | 75 auto readSize = read(fPipes[0], message, sizeof(Message)); |
67 SkASSERT(readSize == sizeof(Message)); | 76 SkASSERT(readSize == sizeof(Message)); |
68 } | 77 } |
69 | 78 |
70 static int message_callback(int fd, int events, void* data) { | 79 int SkiaAndroidApp::message_callback(int fd, int events, void* data) { |
71 auto skiaAndroidApp = (SkiaAndroidApp*)data; | 80 auto skiaAndroidApp = (SkiaAndroidApp*)data; |
72 Message message; | 81 Message message; |
73 skiaAndroidApp->readMessage(&message); | 82 skiaAndroidApp->readMessage(&message); |
74 SkDebugf("message_callback %d", message.fType); | 83 SkDebugf("message_callback %d", message.fType); |
75 SkASSERT(message.fType != kUndefined); | 84 SkASSERT(message.fType != kUndefined); |
76 | 85 |
77 switch (message.fType) { | 86 switch (message.fType) { |
78 case kDestroyApp: { | 87 case kDestroyApp: { |
79 delete skiaAndroidApp; | 88 delete skiaAndroidApp; |
80 pthread_exit(nullptr); | 89 pthread_exit(nullptr); |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
120 break; | 129 break; |
121 } | 130 } |
122 default: { | 131 default: { |
123 // do nothing | 132 // do nothing |
124 } | 133 } |
125 } | 134 } |
126 | 135 |
127 return 1; // continue receiving callbacks | 136 return 1; // continue receiving callbacks |
128 } | 137 } |
129 | 138 |
130 void* pthread_main(void* arg) { | 139 void* SkiaAndroidApp::pthread_main(void* arg) { |
131 SkDebugf("pthread_main begins"); | 140 SkDebugf("pthread_main begins"); |
132 | 141 |
133 auto skiaAndroidApp = (SkiaAndroidApp*)arg; | 142 auto skiaAndroidApp = (SkiaAndroidApp*)arg; |
134 | 143 |
| 144 // Because JNIEnv is thread sensitive, we need AttachCurrentThread to set ou
r fPThreadEnv |
| 145 skiaAndroidApp->fJavaVM->AttachCurrentThread(&(skiaAndroidApp->fPThreadEnv),
nullptr); |
| 146 |
135 ALooper* looper = ALooper_prepare(ALOOPER_PREPARE_ALLOW_NON_CALLBACKS); | 147 ALooper* looper = ALooper_prepare(ALOOPER_PREPARE_ALLOW_NON_CALLBACKS); |
136 pipe(skiaAndroidApp->fPipes); | 148 pipe(skiaAndroidApp->fPipes); |
137 ALooper_addFd(looper, skiaAndroidApp->fPipes[0], LOOPER_ID_MESSAGEPIPE, ALOO
PER_EVENT_INPUT, | 149 ALooper_addFd(looper, skiaAndroidApp->fPipes[0], LOOPER_ID_MESSAGEPIPE, ALOO
PER_EVENT_INPUT, |
138 message_callback, skiaAndroidApp); | 150 message_callback, skiaAndroidApp); |
139 | 151 |
140 int ident; | 152 int ident; |
141 int events; | 153 int events; |
142 struct android_poll_source* source; | 154 struct android_poll_source* source; |
143 | 155 |
144 skiaAndroidApp->fApp = Application::Create(0, nullptr, skiaAndroidApp); | 156 skiaAndroidApp->fApp = Application::Create(0, nullptr, skiaAndroidApp); |
145 | 157 |
146 while ((ident = ALooper_pollAll(-1, nullptr, &events, (void**)&source)) >= 0
) { | 158 while ((ident = ALooper_pollAll(-1, nullptr, &events, (void**)&source)) >= 0
) { |
147 SkDebugf("ALooper_pollAll ident=%d", ident); | 159 SkDebugf("ALooper_pollAll ident=%d", ident); |
148 } | 160 } |
149 | 161 |
150 return nullptr; | 162 return nullptr; |
151 } | 163 } |
152 | 164 |
153 extern "C" // extern "C" is needed for JNI (although the method itself is in C+
+) | 165 extern "C" // extern "C" is needed for JNI (although the method itself is in C+
+) |
154 JNIEXPORT jlong JNICALL | 166 JNIEXPORT jlong JNICALL |
155 Java_org_skia_viewer_ViewerApplication_createNativeApp(JNIEnv* env, jobject
activity) { | 167 Java_org_skia_viewer_ViewerApplication_createNativeApp(JNIEnv* env, jobject
application) { |
156 SkiaAndroidApp* skiaAndroidApp = new SkiaAndroidApp; | 168 SkiaAndroidApp* skiaAndroidApp = new SkiaAndroidApp(env, application); |
157 return (jlong)((size_t)skiaAndroidApp); | 169 return (jlong)((size_t)skiaAndroidApp); |
158 } | 170 } |
159 | 171 |
160 extern "C" JNIEXPORT void JNICALL Java_org_skia_viewer_ViewerApplication_destroy
NativeApp( | 172 extern "C" JNIEXPORT void JNICALL Java_org_skia_viewer_ViewerApplication_destroy
NativeApp( |
161 JNIEnv* env, jobject activity, jlong handle) { | 173 JNIEnv* env, jobject application, jlong handle) { |
162 auto skiaAndroidApp = (SkiaAndroidApp*)handle; | 174 auto skiaAndroidApp = (SkiaAndroidApp*)handle; |
163 skiaAndroidApp->postMessage(Message(kDestroyApp)); | 175 skiaAndroidApp->postMessage(Message(kDestroyApp)); |
164 } | 176 } |
165 | 177 |
166 extern "C" JNIEXPORT void JNICALL Java_org_skia_viewer_ViewerActivity_onSurfaceC
reated( | 178 extern "C" JNIEXPORT void JNICALL Java_org_skia_viewer_ViewerActivity_onSurfaceC
reated( |
167 JNIEnv* env, jobject activity, jlong handle, jobject surface) { | 179 JNIEnv* env, jobject activity, jlong handle, jobject surface) { |
168 auto skiaAndroidApp = (SkiaAndroidApp*)handle; | 180 auto skiaAndroidApp = (SkiaAndroidApp*)handle; |
169 Message message(kSurfaceCreated); | 181 Message message(kSurfaceCreated); |
170 message.fNativeWindow = ANativeWindow_fromSurface(env, surface); | 182 message.fNativeWindow = ANativeWindow_fromSurface(env, surface); |
171 skiaAndroidApp->postMessage(message); | 183 skiaAndroidApp->postMessage(message); |
(...skipping 17 matching lines...) Expand all Loading... |
189
jobject activity, | 201
jobject activity, |
190
jlong handle, | 202
jlong handle, |
191
jint keycode) { | 203
jint keycode) { |
192 auto skiaAndroidApp = (SkiaAndroidApp*)handle; | 204 auto skiaAndroidApp = (SkiaAndroidApp*)handle; |
193 Message message(kKeyPressed); | 205 Message message(kKeyPressed); |
194 message.keycode = keycode; | 206 message.keycode = keycode; |
195 skiaAndroidApp->postMessage(message); | 207 skiaAndroidApp->postMessage(message); |
196 } | 208 } |
197 | 209 |
198 } // namespace sk_app | 210 } // namespace sk_app |
OLD | NEW |