From c936477018294b5805e416978543fb8d3f7cd84a Mon Sep 17 00:00:00 2001 From: Alex Zolotarev Date: Sun, 24 Jul 2011 17:36:26 +0300 Subject: [PATCH] [android] JNIEnv should be local for each thread --- android/jni/android_framework.cpp | 18 +++++++++------ android/jni/android_framework.hpp | 6 ++--- android/jni/android_platform.cpp | 3 +-- android/jni/android_platform.hpp | 2 +- android/jni/main_native.cpp | 37 ++++++++++++++++++++++--------- 5 files changed, 42 insertions(+), 24 deletions(-) diff --git a/android/jni/android_framework.cpp b/android/jni/android_framework.cpp index fd7c8080c0..1224b6ae9d 100644 --- a/android/jni/android_framework.cpp +++ b/android/jni/android_framework.cpp @@ -17,12 +17,15 @@ void AndroidFramework::ViewHandle::invalidateImpl() void AndroidFramework::CallRepaint() { - m_env->CallVoidMethod(m_parentView, - jni::GetJavaMethodID(m_env, m_parentView, "requestRender", "()V")); + // Always get current env pointer, it's different for each thread + JNIEnv * env; + m_jvm->AttachCurrentThread(&env, NULL); + LOG(LDEBUG, ("AF::CallRepaint", env)); + env->CallVoidMethod(m_parentView, jni::GetJavaMethodID(env, m_parentView, "requestRender", "()V")); } -AndroidFramework::AndroidFramework() -: m_view(new ViewHandle(this)), m_work(m_view, 0) +AndroidFramework::AndroidFramework(JavaVM * jvm) +: m_jvm(jvm), m_view(new ViewHandle(this)), m_work(m_view, 0) { m_work.InitStorage(m_storage); shared_ptr renderPolicy(new RenderPolicyST(m_view, bind(&Framework::DrawModel, &m_work, _1, _2, _3, _4))); @@ -31,9 +34,8 @@ AndroidFramework::AndroidFramework() m_storage.ReInitCountries(false); } -void AndroidFramework::SetParentView(JNIEnv * env, jobject view) +void AndroidFramework::SetParentView(jobject view) { - m_env = env; m_parentView = view; } @@ -52,9 +54,11 @@ void AndroidFramework::InitRenderer() LOG(LDEBUG, ("AF::InitRenderer 4")); m_work.initializeGL(pRC, pRM); + LOG(LDEBUG, ("AF::InitRenderer 5")); + m_work.ShowAll(); - LOG(LDEBUG, ("AF::InitRenderer 5")); + LOG(LDEBUG, ("AF::InitRenderer 6")); } void AndroidFramework::Resize(int w, int h) diff --git a/android/jni/android_framework.hpp b/android/jni/android_framework.hpp index 812e244c0a..3ba0194676 100644 --- a/android/jni/android_framework.hpp +++ b/android/jni/android_framework.hpp @@ -25,17 +25,17 @@ private: Framework m_work; storage::Storage m_storage; - JNIEnv * m_env; + JavaVM * m_jvm; jobject m_parentView; void CallRepaint(); public: - AndroidFramework(); + AndroidFramework(JavaVM * jvm); storage::Storage & Storage() { return m_storage; } - void SetParentView(JNIEnv * env, jobject view); + void SetParentView(jobject view); void InitRenderer(); diff --git a/android/jni/android_platform.cpp b/android/jni/android_platform.cpp index 058df63c12..d9dfbfff3b 100644 --- a/android/jni/android_platform.cpp +++ b/android/jni/android_platform.cpp @@ -5,8 +5,7 @@ #include "../../coding/zip_reader.hpp" - -void AndroidPlatform::Initialize(JNIEnv * env, jobject activity, jstring apkPath, jstring storagePath) +void AndroidPlatform::Initialize(JNIEnv * env, jstring apkPath, jstring storagePath) { m_resourcesDir = jni::ToString(env, apkPath); m_writableDir = jni::ToString(env, storagePath); diff --git a/android/jni/android_platform.hpp b/android/jni/android_platform.hpp index d3b9a205f3..3a462e0a8c 100644 --- a/android/jni/android_platform.hpp +++ b/android/jni/android_platform.hpp @@ -7,7 +7,7 @@ class AndroidPlatform : public BasePlatformImpl { public: - void Initialize(JNIEnv * env, jobject activity, jstring apkPath, jstring storagePath); + void Initialize(JNIEnv * env, jstring apkPath, jstring storagePath); virtual ModelReader * GetReader(string const & file) const; /// Overrided to support zip file listing diff --git a/android/jni/main_native.cpp b/android/jni/main_native.cpp index 58c0657e94..c416b8e428 100644 --- a/android/jni/main_native.cpp +++ b/android/jni/main_native.cpp @@ -7,7 +7,8 @@ #include #include -AndroidFramework * g_work = 0; +static AndroidFramework * g_work = 0; +static JavaVM * g_jvm = 0; extern "C" { @@ -15,19 +16,33 @@ extern "C" // MWMActivity /////////////////////////////////////////////////////////////////////////////////// +JNIEXPORT jint JNICALL +JNI_OnLoad(JavaVM * jvm, void * reserved) +{ + g_jvm = jvm; + jni::InitSystemLog(); + jni::InitAssertLog(); + LOG(LDEBUG, ("JNI_OnLoad")); + return JNI_VERSION_1_4; +} + +JNIEXPORT void JNICALL +JNI_OnUnload(JavaVM * vm, void * reserved) +{ + delete g_work; +} + JNIEXPORT void JNICALL Java_com_mapswithme_maps_MWMActivity_nativeInit(JNIEnv * env, jobject thiz, jstring apkPath, jstring storagePath) { - jni::InitSystemLog(); - jni::InitAssertLog(); + LOG(LDEBUG, ("Java_com_mapswithme_maps_MWMActivity_nativeInit 1")); + if (!g_work) + { + GetAndroidPlatform().Initialize(env, apkPath, storagePath); + g_work = new AndroidFramework(g_jvm); + } - LOG(LDEBUG, ("MWMActivity::Init 1")); - GetAndroidPlatform().Initialize(env, thiz, apkPath, storagePath); - - LOG(LDEBUG, ("MWMActivity::Init 2")); - g_work = new AndroidFramework(); - - LOG(LDEBUG, ("MWMActivity::Init 3")); + LOG(LDEBUG, ("Java_com_mapswithme_maps_MWMActivity_nativeInit 2")); } /////////////////////////////////////////////////////////////////////////////////// @@ -38,7 +53,7 @@ JNIEXPORT void JNICALL Java_com_mapswithme_maps_MainGLView_nativeInit(JNIEnv * env, jobject thiz) { ASSERT ( g_work, () ); - g_work->SetParentView(env, thiz); + g_work->SetParentView(thiz); } JNIEXPORT void JNICALL