| 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" | |
| 26 | 25 |
| 27 namespace sk_app { | 26 namespace sk_app { |
| 28 | 27 |
| 29 static const int LOOPER_ID_MESSAGEPIPE = 1; | 28 static const int LOOPER_ID_MESSAGEPIPE = 1; |
| 30 | 29 |
| 31 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({ |
| 32 {AKEYCODE_SOFT_LEFT, Window::Key::kLeft}, | 31 {AKEYCODE_SOFT_LEFT, Window::Key::kLeft}, |
| 33 {AKEYCODE_SOFT_RIGHT, Window::Key::kRight} | 32 {AKEYCODE_SOFT_RIGHT, Window::Key::kRight} |
| 34 }); | 33 }); |
| 35 | 34 |
| (...skipping 124 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 160 break; | 159 break; |
| 161 } | 160 } |
| 162 default: { | 161 default: { |
| 163 // do nothing | 162 // do nothing |
| 164 } | 163 } |
| 165 } | 164 } |
| 166 | 165 |
| 167 return 1; // continue receiving callbacks | 166 return 1; // continue receiving callbacks |
| 168 } | 167 } |
| 169 | 168 |
| 170 static double now_ms() { return SkTime::GetMSecs(); } | |
| 171 | |
| 172 void* SkiaAndroidApp::pthread_main(void* arg) { | 169 void* SkiaAndroidApp::pthread_main(void* arg) { |
| 173 SkDebugf("pthread_main begins"); | 170 SkDebugf("pthread_main begins"); |
| 174 | 171 |
| 175 auto skiaAndroidApp = (SkiaAndroidApp*)arg; | 172 auto skiaAndroidApp = (SkiaAndroidApp*)arg; |
| 176 | 173 |
| 177 // Because JNIEnv is thread sensitive, we need AttachCurrentThread to set ou
r fPThreadEnv | 174 // Because JNIEnv is thread sensitive, we need AttachCurrentThread to set ou
r fPThreadEnv |
| 178 skiaAndroidApp->fJavaVM->AttachCurrentThread(&(skiaAndroidApp->fPThreadEnv),
nullptr); | 175 skiaAndroidApp->fJavaVM->AttachCurrentThread(&(skiaAndroidApp->fPThreadEnv),
nullptr); |
| 179 | 176 |
| 180 ALooper* looper = ALooper_prepare(ALOOPER_PREPARE_ALLOW_NON_CALLBACKS); | 177 ALooper* looper = ALooper_prepare(ALOOPER_PREPARE_ALLOW_NON_CALLBACKS); |
| 181 pipe(skiaAndroidApp->fPipes); | 178 pipe(skiaAndroidApp->fPipes); |
| 182 ALooper_addFd(looper, skiaAndroidApp->fPipes[0], LOOPER_ID_MESSAGEPIPE, ALOO
PER_EVENT_INPUT, | 179 ALooper_addFd(looper, skiaAndroidApp->fPipes[0], LOOPER_ID_MESSAGEPIPE, ALOO
PER_EVENT_INPUT, |
| 183 message_callback, skiaAndroidApp); | 180 message_callback, skiaAndroidApp); |
| 184 | 181 |
| 185 skiaAndroidApp->fApp = Application::Create(0, nullptr, skiaAndroidApp); | 182 skiaAndroidApp->fApp = Application::Create(0, nullptr, skiaAndroidApp); |
| 186 | 183 |
| 187 double currentTime = 0.0; | |
| 188 double previousTime = 0.0; | |
| 189 while (true) { | 184 while (true) { |
| 190 const int ident = ALooper_pollAll(0, nullptr, nullptr, nullptr); | 185 const int ident = ALooper_pollAll(0, nullptr, nullptr, nullptr); |
| 191 | 186 |
| 192 if (ident >= 0) { | 187 if (ident >= 0) { |
| 193 SkDebugf("Unhandled ALooper_pollAll ident=%d !", ident); | 188 SkDebugf("Unhandled ALooper_pollAll ident=%d !", ident); |
| 194 } else { | 189 } else { |
| 195 previousTime = currentTime; | 190 skiaAndroidApp->fApp->onIdle(); |
| 196 currentTime = now_ms(); | |
| 197 skiaAndroidApp->fApp->onIdle(currentTime - previousTime); | |
| 198 } | 191 } |
| 199 } | 192 } |
| 200 | 193 |
| 201 SkDebugf("pthread_main ends"); | 194 SkDebugf("pthread_main ends"); |
| 202 | 195 |
| 203 return nullptr; | 196 return nullptr; |
| 204 } | 197 } |
| 205 | 198 |
| 206 extern "C" // extern "C" is needed for JNI (although the method itself is in C+
+) | 199 extern "C" // extern "C" is needed for JNI (although the method itself is in C+
+) |
| 207 JNIEXPORT jlong JNICALL | 200 JNIEXPORT jlong JNICALL |
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 266 const char* nameChars = env->GetStringUTFChars(stateName, nullptr); | 259 const char* nameChars = env->GetStringUTFChars(stateName, nullptr); |
| 267 const char* valueChars = env->GetStringUTFChars(stateValue, nullptr); | 260 const char* valueChars = env->GetStringUTFChars(stateValue, nullptr); |
| 268 message.stateName = new SkString(nameChars); | 261 message.stateName = new SkString(nameChars); |
| 269 message.stateValue = new SkString(valueChars); | 262 message.stateValue = new SkString(valueChars); |
| 270 skiaAndroidApp->postMessage(message); | 263 skiaAndroidApp->postMessage(message); |
| 271 env->ReleaseStringUTFChars(stateName, nameChars); | 264 env->ReleaseStringUTFChars(stateName, nameChars); |
| 272 env->ReleaseStringUTFChars(stateValue, valueChars); | 265 env->ReleaseStringUTFChars(stateValue, valueChars); |
| 273 } | 266 } |
| 274 | 267 |
| 275 } // namespace sk_app | 268 } // namespace sk_app |
| OLD | NEW |