From dca42018d26aaab2148db0b8f2971821ebc3a4cf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Konstantin=20K=C3=A4fer?= Date: Thu, 25 Jun 2015 14:39:42 +0200 Subject: drive Android rendering from the main thread via invalidate() calls --- android/cpp/jni.cpp | 16 +++++++++++++ android/cpp/native_map_view.cpp | 51 ++++++++++++++++++++++++++++++++++++++--- 2 files changed, 64 insertions(+), 3 deletions(-) (limited to 'android/cpp') 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(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(&nativePause)}, {"nativeResume", "(J)V", reinterpret_cast(&nativeResume)}, {"nativeUpdate", "(J)V", reinterpret_cast(&nativeUpdate)}, + {"nativeOnInvalidate", "(J)V", reinterpret_cast(&nativeOnInvalidate)}, {"nativeResize", "(JIIFII)V", reinterpret_cast(static_cast(&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(&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(); + } +} + } } -- cgit v1.2.1