OLD | NEW |
---|---|
(Empty) | |
1 /* | |
2 * Copyright 2016 Google Inc. | |
3 * | |
4 * Use of this source code is governed by a BSD-style license that can be | |
5 * found in the LICENSE file. | |
6 */ | |
7 | |
8 #include "surface_glue_android.h" | |
9 | |
10 #include <jni.h> | |
11 #include <pthread.h> | |
12 #include <stdio.h> | |
13 #include <unistd.h> | |
14 | |
15 #include <android/native_window_jni.h> | |
16 #include <android_native_app_glue.h> | |
djsollen
2016/05/06 18:45:26
we should not need this include.
liyuqian
2016/05/06 20:42:30
This is needed for LOOPER_ID_USER
djsollen
2016/05/09 13:29:44
you don't need LOOPER_ID_USER as that was somethin
liyuqian
2016/05/09 14:27:27
Done.
| |
17 | |
18 #include "../Application.h" | |
19 #include "SkTypes.h" | |
20 #include "SkUtils.h" | |
21 #include "Window_android.h" | |
22 | |
23 namespace sk_app { | |
24 | |
25 static const int LOOPER_ID_MESSAGEPIPE = LOOPER_ID_USER; | |
26 | |
27 void* pthread_main(void* arg); | |
28 | |
29 AppThread::AppThread() { | |
30 nativeWindow = nullptr; | |
31 pthread_create(&thread, nullptr, pthread_main, this); | |
32 } | |
33 | |
34 AppThread::~AppThread() { | |
35 if (window) { | |
36 window->detach(); | |
37 } | |
38 if (nativeWindow) { | |
39 ANativeWindow_release(this->nativeWindow); | |
40 this->nativeWindow = nullptr; | |
41 } | |
42 if (app) { | |
43 delete app; | |
44 } | |
45 } | |
46 | |
47 void AppThread::postMessage(const Message& message) { | |
48 auto writeSize = write(pipes[1], &message, sizeof(message)); | |
49 SkASSERT(writeSize == sizeof(message)); | |
50 } | |
51 | |
52 void AppThread::readMessage(Message* message) { | |
53 auto readSize = read(pipes[0], message, sizeof(Message)); | |
54 SkASSERT(readSize == sizeof(Message)); | |
55 } | |
56 | |
57 static int message_callback(int fd, int events, void* data) { | |
58 auto appThread = (AppThread*)data; | |
59 Message message; | |
60 appThread->readMessage(&message); | |
61 SkDebugf("message_callback %d", message.type); | |
62 | |
63 switch (message.type) { | |
64 case kDestroyApp: { | |
65 delete appThread; | |
66 pthread_exit(nullptr); | |
67 return 0; | |
68 } | |
69 case kSurfaceCreated: { | |
70 auto window_android = (Window_android*)appThread->window; | |
71 window_android->initDisplay(appThread->nativeWindow); | |
72 window_android->paintIfNeeded(); | |
73 break; | |
74 } | |
75 case kSurfaceChanged: { | |
76 int width = ANativeWindow_getWidth(appThread->nativeWindow); | |
77 int height = ANativeWindow_getHeight(appThread->nativeWindow); | |
78 appThread->window->onResize(width, height); | |
79 auto window_android = (Window_android*)appThread->window; | |
80 window_android->setContentRect(0, 0, width, height); | |
81 window_android->paintIfNeeded(); | |
82 break; | |
83 } | |
84 default: { | |
85 // do nothing | |
86 } | |
87 } | |
88 | |
89 return 1; // continue receiving callbacks | |
90 } | |
91 | |
92 void* pthread_main(void* arg) { | |
93 SkDebugf("pthread_main begins"); | |
94 | |
95 auto appThread = (AppThread*)arg; | |
96 | |
97 ALooper* looper = ALooper_prepare(ALOOPER_PREPARE_ALLOW_NON_CALLBACKS); | |
98 pipe(appThread->pipes); | |
99 ALooper_addFd(looper, appThread->pipes[0], LOOPER_ID_MESSAGEPIPE, ALOOPER_EV ENT_INPUT, | |
100 message_callback, appThread); | |
101 | |
102 int ident; | |
103 int events; | |
104 struct android_poll_source* source; | |
105 | |
106 appThread->app = Application::Create(0, nullptr, appThread); | |
107 | |
108 while ((ident = ALooper_pollAll(-1, nullptr, &events, (void**)&source)) >= 0 ) { | |
109 SkDebugf("ALooper_pollAll ident=%d", ident); | |
110 } | |
111 | |
112 return nullptr; | |
113 } | |
114 | |
115 extern "C" // extern "C" is needed for JNI (although the method itself is in C+ +) | |
116 JNIEXPORT jlong JNICALL | |
117 Java_org_skia_viewer_ViewerApplication_createNativeApp(JNIEnv* env, jobject activity) { | |
118 AppThread* appThread = new AppThread; | |
119 return (jlong)((size_t)appThread); | |
120 } | |
121 | |
122 extern "C" JNIEXPORT void JNICALL Java_org_skia_viewer_ViewerApplication_destroy NativeApp( | |
123 JNIEnv* env, jobject activity, jlong handle) { | |
124 auto appThread = (AppThread*)handle; | |
125 Message message(kDestroyApp); | |
126 appThread->postMessage(message); | |
127 pthread_join(appThread->thread, nullptr); | |
128 } | |
129 | |
130 extern "C" JNIEXPORT void JNICALL Java_org_skia_viewer_MainActivity_onSurfaceCre ated( | |
131 JNIEnv* env, jobject activity, jlong handle, jobject surface) { | |
132 auto appThread = (AppThread*)handle; | |
133 | |
134 SkASSERT(!appThread->nativeWindow); | |
135 ANativeWindow* nativeWindow = ANativeWindow_fromSurface(env, surface); | |
136 appThread->nativeWindow = nativeWindow; | |
137 | |
138 Message message(kSurfaceCreated); | |
139 appThread->postMessage(message); | |
140 } | |
141 | |
142 extern "C" JNIEXPORT void JNICALL Java_org_skia_viewer_MainActivity_onSurfaceCha nged( | |
143 JNIEnv* env, jobject activity, jlong handle, jobject surface) { | |
144 auto appThread = (AppThread*)handle; | |
145 | |
146 ANativeWindow* nativeWindow = ANativeWindow_fromSurface(env, surface); | |
147 if (nativeWindow != appThread->nativeWindow) { | |
148 ANativeWindow_release(appThread->nativeWindow); | |
149 appThread->nativeWindow = nativeWindow; | |
150 } | |
151 | |
152 Message message(kSurfaceChanged); | |
153 appThread->postMessage(message); | |
154 } | |
155 | |
156 extern "C" JNIEXPORT void JNICALL | |
157 Java_org_skia_viewer_MainActivity_onSurfaceDestroyed(JNIEnv* env, jobject activi ty, jlong handle) { | |
158 auto appThread = (AppThread*)handle; | |
159 | |
160 if (appThread->nativeWindow) { | |
161 ANativeWindow_release(appThread->nativeWindow); | |
162 appThread->nativeWindow = nullptr; | |
163 } | |
164 | |
165 Message message(kSurfaceDestroyed); | |
166 appThread->postMessage(message); | |
167 } | |
168 | |
169 } // namespace sk_app | |
OLD | NEW |