diff options
author | Konstantin Käfer <mail@kkaefer.com> | 2015-06-25 14:39:42 +0200 |
---|---|---|
committer | John Firebaugh <john.firebaugh@gmail.com> | 2015-06-26 10:32:06 -0700 |
commit | dca42018d26aaab2148db0b8f2971821ebc3a4cf (patch) | |
tree | 376402b108bc895ea4e5c45cdcda14af959abb61 /android/cpp | |
parent | 84e2ea886dc55917a1d6b665337fff6d3862984a (diff) | |
download | qtlocation-mapboxgl-dca42018d26aaab2148db0b8f2971821ebc3a4cf.tar.gz |
drive Android rendering from the main thread via invalidate() calls
Diffstat (limited to 'android/cpp')
-rw-r--r-- | android/cpp/jni.cpp | 16 | ||||
-rw-r--r-- | android/cpp/native_map_view.cpp | 51 |
2 files changed, 64 insertions, 3 deletions
diff --git a/android/cpp/jni.cpp b/android/cpp/jni.cpp index 64e72e1499..36ef273d68 100644 --- a/android/cpp/jni.cpp +++ b/android/cpp/jni.cpp @@ -30,6 +30,7 @@ std::string dataPath; std::string apkPath; std::string androidRelease; +jmethodID onInvalidateId = nullptr; jmethodID onMapChangedId = nullptr; jmethodID onFpsChangedId = nullptr; @@ -277,6 +278,13 @@ void JNICALL nativeUpdate(JNIEnv *env, jobject obj, jlong nativeMapViewPtr) { nativeMapView->getMap().update(); } +void JNICALL nativeOnInvalidate(JNIEnv *env, jobject obj, jlong nativeMapViewPtr) { + mbgl::Log::Debug(mbgl::Event::JNI, "nativeOnInvalidate"); + assert(nativeMapViewPtr != 0); + NativeMapView *nativeMapView = reinterpret_cast<NativeMapView *>(nativeMapViewPtr); + nativeMapView->onInvalidate(); +} + void JNICALL nativeResize(JNIEnv *env, jobject obj, jlong nativeMapViewPtr, jint width, jint height, jfloat ratio, jint fbWidth, jint fbHeight) { mbgl::Log::Debug(mbgl::Event::JNI, "nativeResize"); @@ -805,6 +813,12 @@ extern "C" JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM *vm, void *reserved) { return JNI_ERR; } + onInvalidateId = env->GetMethodID(nativeMapViewClass, "onInvalidate", "()V"); + if (onInvalidateId == nullptr) { + env->ExceptionDescribe(); + return JNI_ERR; + } + onMapChangedId = env->GetMethodID(nativeMapViewClass, "onMapChanged", "()V"); if (onMapChangedId == nullptr) { env->ExceptionDescribe(); @@ -921,6 +935,7 @@ extern "C" JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM *vm, void *reserved) { {"nativePause", "(J)V", reinterpret_cast<void *>(&nativePause)}, {"nativeResume", "(J)V", reinterpret_cast<void *>(&nativeResume)}, {"nativeUpdate", "(J)V", reinterpret_cast<void *>(&nativeUpdate)}, + {"nativeOnInvalidate", "(J)V", reinterpret_cast<void *>(&nativeOnInvalidate)}, {"nativeResize", "(JIIFII)V", reinterpret_cast<void *>(static_cast<void JNICALL ( *)(JNIEnv *, jobject, jlong, jint, jint, jfloat, jint, jint)>(&nativeResize))}, @@ -1092,6 +1107,7 @@ extern "C" JNIEXPORT void JNICALL JNI_OnUnload(JavaVM *vm, void *reserved) { latLngZoomLatitudeId = nullptr; latLngZoomZoomId = nullptr; + onInvalidateId = nullptr; onMapChangedId = nullptr; onFpsChangedId = nullptr; diff --git a/android/cpp/native_map_view.cpp b/android/cpp/native_map_view.cpp index 845d10ea67..48f75865e6 100644 --- a/android/cpp/native_map_view.cpp +++ b/android/cpp/native_map_view.cpp @@ -125,13 +125,49 @@ void NativeMapView::deactivate() { } void NativeMapView::invalidate() { - mbgl::Log::Debug(mbgl::Event::Android, "NativeMapView::invalidate"); + mbgl::Log::Debug(mbgl::Event::Android, "NativeMapView::invalidate()"); - // TODO: inform the main thread that it must call map->renderSync(); + clean.clear(); + + assert(vm != nullptr); + assert(obj != nullptr); + + JavaVMAttachArgs args = {JNI_VERSION_1_2, "NativeMapView::invalidate()", NULL}; + + jint ret; + JNIEnv *env = nullptr; + bool detach = false; + ret = vm->GetEnv(reinterpret_cast<void **>(&env), JNI_VERSION_1_6); + if (ret != JNI_OK) { + if (ret != JNI_EDETACHED) { + mbgl::Log::Error(mbgl::Event::JNI, "GetEnv() failed with %i", ret); + throw new std::runtime_error("GetEnv() failed"); + } else { + ret = vm->AttachCurrentThread(&env, &args); + if (ret != JNI_OK) { + mbgl::Log::Error(mbgl::Event::JNI, "AttachCurrentThread() failed with %i", ret); + throw new std::runtime_error("AttachCurrentThread() failed"); + } + detach = true; + } + } + + env->CallVoidMethod(obj, onInvalidateId); + if (env->ExceptionCheck()) { + env->ExceptionDescribe(); + } + + if (detach) { + if ((ret = vm->DetachCurrentThread()) != JNI_OK) { + mbgl::Log::Error(mbgl::Event::JNI, "DetachCurrentThread() failed with %i", ret); + throw new std::runtime_error("DetachCurrentThread() failed"); + } + } + env = nullptr; } void NativeMapView::swap() { - mbgl::Log::Debug(mbgl::Event::Android, "NativeMapView::invalidate"); + mbgl::Log::Debug(mbgl::Event::Android, "NativeMapView::swap"); if ((display != EGL_NO_DISPLAY) && (surface != EGL_NO_SURFACE)) { if (!eglSwapBuffers(display, surface)) { @@ -709,5 +745,14 @@ void NativeMapView::updateFps() { env = nullptr; } +void NativeMapView::onInvalidate() { + mbgl::Log::Debug(mbgl::Event::Android, "NativeMapView::onInvalidate()"); + + const bool dirty = !clean.test_and_set(); + if (dirty) { + map.renderSync(); + } +} + } } |