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 "Timer.h" | |
scroggo
2016/06/03 19:11:06
I think you just use SkTIme, which is defined in S
liyuqian
2016/06/06 13:38:27
Done.
| |
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::GetNSecs() * 1e-6; } | |
scroggo
2016/06/03 19:11:06
Why not call SkTime::GetMSecs()?
And while we're
liyuqian
2016/06/06 13:38:27
Done.
| |
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 int ident; | |
scroggo
2016/06/03 19:11:06
It's easier to follow (and less error-prone) if yo
liyuqian
2016/06/06 13:38:27
Done.
| |
185 int events; | |
186 struct android_poll_source* source; | |
scroggo
2016/06/03 19:11:06
You don't use either of these variables. Will ALoo
liyuqian
2016/06/06 13:38:27
Unfortunately, the Android developer document does
scroggo
2016/06/06 14:17:00
I suppose their implementation could vary based on
| |
187 ident = ALooper_pollAll(0, nullptr, &events, (void**)&source); | |
188 | |
189 if (ident >= 0) { | |
190 SkDebugf("Unhandled ALooper_pollAll ident=%d !", ident); | |
191 } else { | |
192 previousTime = currentTime; | |
193 currentTime = now_ms(); | |
194 skiaAndroidApp->fApp->onIdle(currentTime - previousTime); | |
195 } | |
184 } | 196 } |
185 | 197 |
198 SkDebugf("pthread_main ends"); | |
199 | |
186 return nullptr; | 200 return nullptr; |
187 } | 201 } |
188 | 202 |
189 extern "C" // extern "C" is needed for JNI (although the method itself is in C+ +) | 203 extern "C" // extern "C" is needed for JNI (although the method itself is in C+ +) |
190 JNIEXPORT jlong JNICALL | 204 JNIEXPORT jlong JNICALL |
191 Java_org_skia_viewer_ViewerApplication_createNativeApp(JNIEnv* env, jobject application) { | 205 Java_org_skia_viewer_ViewerApplication_createNativeApp(JNIEnv* env, jobject application) { |
192 SkiaAndroidApp* skiaAndroidApp = new SkiaAndroidApp(env, application); | 206 SkiaAndroidApp* skiaAndroidApp = new SkiaAndroidApp(env, application); |
193 return (jlong)((size_t)skiaAndroidApp); | 207 return (jlong)((size_t)skiaAndroidApp); |
194 } | 208 } |
195 | 209 |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
249 const char* nameChars = env->GetStringUTFChars(stateName, nullptr); | 263 const char* nameChars = env->GetStringUTFChars(stateName, nullptr); |
250 const char* valueChars = env->GetStringUTFChars(stateValue, nullptr); | 264 const char* valueChars = env->GetStringUTFChars(stateValue, nullptr); |
251 message.stateName = new SkString(nameChars); | 265 message.stateName = new SkString(nameChars); |
252 message.stateValue = new SkString(valueChars); | 266 message.stateValue = new SkString(valueChars); |
253 skiaAndroidApp->postMessage(message); | 267 skiaAndroidApp->postMessage(message); |
254 env->ReleaseStringUTFChars(stateName, nameChars); | 268 env->ReleaseStringUTFChars(stateName, nameChars); |
255 env->ReleaseStringUTFChars(stateValue, valueChars); | 269 env->ReleaseStringUTFChars(stateValue, valueChars); |
256 } | 270 } |
257 | 271 |
258 } // namespace sk_app | 272 } // namespace sk_app |
OLD | NEW |