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/input.h> |
17 #include <android/keycodes.h> | 17 #include <android/keycodes.h> |
18 #include <android/looper.h> | 18 #include <android/looper.h> |
19 #include <android/native_window_jni.h> | 19 #include <android/native_window_jni.h> |
20 | 20 |
21 #include "../Application.h" | 21 #include "../Application.h" |
22 #include "SkTypes.h" | 22 #include "SkTypes.h" |
23 #include "SkUtils.h" | 23 #include "SkUtils.h" |
24 #include "Window_android.h" | 24 #include "Window_android.h" |
25 #include "SkTime.h" | |
25 | 26 |
26 namespace sk_app { | 27 namespace sk_app { |
27 | 28 |
28 static const int LOOPER_ID_MESSAGEPIPE = 1; | 29 static const int LOOPER_ID_MESSAGEPIPE = 1; |
29 | 30 |
30 static const std::unordered_map<int, Window::Key> ANDROID_TO_WINDOW_KEYMAP({ | 31 static const std::unordered_map<int, Window::Key> ANDROID_TO_WINDOW_KEYMAP({ |
31 {AKEYCODE_SOFT_LEFT, Window::Key::kLeft}, | 32 {AKEYCODE_SOFT_LEFT, Window::Key::kLeft}, |
32 {AKEYCODE_SOFT_RIGHT, Window::Key::kRight} | 33 {AKEYCODE_SOFT_RIGHT, Window::Key::kRight} |
33 }); | 34 }); |
34 | 35 |
(...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
153 break; | 154 break; |
154 } | 155 } |
155 default: { | 156 default: { |
156 // do nothing | 157 // do nothing |
157 } | 158 } |
158 } | 159 } |
159 | 160 |
160 return 1; // continue receiving callbacks | 161 return 1; // continue receiving callbacks |
161 } | 162 } |
162 | 163 |
164 static double now_ms() { return SkTime::GetMSecs(); } | |
165 | |
163 void* SkiaAndroidApp::pthread_main(void* arg) { | 166 void* SkiaAndroidApp::pthread_main(void* arg) { |
164 SkDebugf("pthread_main begins"); | 167 SkDebugf("pthread_main begins"); |
165 | 168 |
166 auto skiaAndroidApp = (SkiaAndroidApp*)arg; | 169 auto skiaAndroidApp = (SkiaAndroidApp*)arg; |
167 | 170 |
168 // Because JNIEnv is thread sensitive, we need AttachCurrentThread to set ou r fPThreadEnv | 171 // Because JNIEnv is thread sensitive, we need AttachCurrentThread to set ou r fPThreadEnv |
169 skiaAndroidApp->fJavaVM->AttachCurrentThread(&(skiaAndroidApp->fPThreadEnv), nullptr); | 172 skiaAndroidApp->fJavaVM->AttachCurrentThread(&(skiaAndroidApp->fPThreadEnv), nullptr); |
170 | 173 |
171 ALooper* looper = ALooper_prepare(ALOOPER_PREPARE_ALLOW_NON_CALLBACKS); | 174 ALooper* looper = ALooper_prepare(ALOOPER_PREPARE_ALLOW_NON_CALLBACKS); |
172 pipe(skiaAndroidApp->fPipes); | 175 pipe(skiaAndroidApp->fPipes); |
173 ALooper_addFd(looper, skiaAndroidApp->fPipes[0], LOOPER_ID_MESSAGEPIPE, ALOO PER_EVENT_INPUT, | 176 ALooper_addFd(looper, skiaAndroidApp->fPipes[0], LOOPER_ID_MESSAGEPIPE, ALOO PER_EVENT_INPUT, |
174 message_callback, skiaAndroidApp); | 177 message_callback, skiaAndroidApp); |
175 | 178 |
176 int ident; | |
177 int events; | |
178 struct android_poll_source* source; | |
179 | |
180 skiaAndroidApp->fApp = Application::Create(0, nullptr, skiaAndroidApp); | 179 skiaAndroidApp->fApp = Application::Create(0, nullptr, skiaAndroidApp); |
181 | 180 |
182 while ((ident = ALooper_pollAll(-1, nullptr, &events, (void**)&source)) >= 0 ) { | 181 double currentTime = 0.0; |
183 SkDebugf("ALooper_pollAll ident=%d", ident); | 182 double previousTime = 0.0; |
183 while (true) { | |
184 const int ident = ALooper_pollAll(0, nullptr, nullptr, nullptr); | |
jvanverth1
2016/06/06 14:18:11
I'm surprised a timeout of 0 doesn't cause a busy
liyuqian
2016/06/06 14:30:46
I checked the CPU usage on Android and it indeed d
| |
185 | |
186 if (ident >= 0) { | |
187 SkDebugf("Unhandled ALooper_pollAll ident=%d !", ident); | |
188 } else { | |
189 previousTime = currentTime; | |
190 currentTime = now_ms(); | |
191 skiaAndroidApp->fApp->onIdle(currentTime - previousTime); | |
192 } | |
184 } | 193 } |
185 | 194 |
195 SkDebugf("pthread_main ends"); | |
196 | |
186 return nullptr; | 197 return nullptr; |
187 } | 198 } |
188 | 199 |
189 extern "C" // extern "C" is needed for JNI (although the method itself is in C+ +) | 200 extern "C" // extern "C" is needed for JNI (although the method itself is in C+ +) |
190 JNIEXPORT jlong JNICALL | 201 JNIEXPORT jlong JNICALL |
191 Java_org_skia_viewer_ViewerApplication_createNativeApp(JNIEnv* env, jobject application) { | 202 Java_org_skia_viewer_ViewerApplication_createNativeApp(JNIEnv* env, jobject application) { |
192 SkiaAndroidApp* skiaAndroidApp = new SkiaAndroidApp(env, application); | 203 SkiaAndroidApp* skiaAndroidApp = new SkiaAndroidApp(env, application); |
193 return (jlong)((size_t)skiaAndroidApp); | 204 return (jlong)((size_t)skiaAndroidApp); |
194 } | 205 } |
195 | 206 |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
249 const char* nameChars = env->GetStringUTFChars(stateName, nullptr); | 260 const char* nameChars = env->GetStringUTFChars(stateName, nullptr); |
250 const char* valueChars = env->GetStringUTFChars(stateValue, nullptr); | 261 const char* valueChars = env->GetStringUTFChars(stateValue, nullptr); |
251 message.stateName = new SkString(nameChars); | 262 message.stateName = new SkString(nameChars); |
252 message.stateValue = new SkString(valueChars); | 263 message.stateValue = new SkString(valueChars); |
253 skiaAndroidApp->postMessage(message); | 264 skiaAndroidApp->postMessage(message); |
254 env->ReleaseStringUTFChars(stateName, nameChars); | 265 env->ReleaseStringUTFChars(stateName, nameChars); |
255 env->ReleaseStringUTFChars(stateValue, valueChars); | 266 env->ReleaseStringUTFChars(stateValue, valueChars); |
256 } | 267 } |
257 | 268 |
258 } // namespace sk_app | 269 } // namespace sk_app |
OLD | NEW |