From 52c790d1a982c7cb62e6fc39d7703db8db88c775 Mon Sep 17 00:00:00 2001 From: Jarvis Huang Date: Wed, 31 Aug 2022 16:57:49 +0800 Subject: [PATCH] t/android: Store jvm/context in service-lib --- src/xrt/auxiliary/android/android_globals.cpp | 25 +++++++++---------- .../MonadoOpenXrApplication.java | 15 +++++++++++ .../targets/service-lib/service_target.cpp | 14 +++++++++++ 3 files changed, 41 insertions(+), 13 deletions(-) diff --git a/src/xrt/auxiliary/android/android_globals.cpp b/src/xrt/auxiliary/android/android_globals.cpp index fd7b68f8b..44b5838a0 100644 --- a/src/xrt/auxiliary/android/android_globals.cpp +++ b/src/xrt/auxiliary/android/android_globals.cpp @@ -13,31 +13,32 @@ #include /*! - * @todo Do we need locking here? Do we need to create global refs for the - * supplied jobjects? + * @todo Do we need locking here? */ static struct { struct _JavaVM *vm = nullptr; - void *activity = nullptr; - void *context = nullptr; + jni::Object activity = {}; + jni::Object context = {}; struct _ANativeWindow *window = nullptr; } android_globals; void android_globals_store_vm_and_activity(struct _JavaVM *vm, void *activity) { + jni::init(vm); android_globals.vm = vm; - android_globals.activity = activity; + android_globals.activity = jni::Object((jobject)activity); } void android_globals_store_vm_and_context(struct _JavaVM *vm, void *context) { + jni::init(vm); android_globals.vm = vm; - android_globals.context = context; + android_globals.context = jni::Object((jobject)context); if (android_globals_is_instance_of_activity(vm, context)) { - android_globals.activity = context; + android_globals.activity = jni::Object((jobject)context); } } @@ -49,6 +50,7 @@ android_globals_is_instance_of_activity(struct _JavaVM *vm, void *obj) auto activity_cls = jni::Class(wrap::android::app::Activity::getTypeName()); return JNI_TRUE == jni::env()->IsInstanceOf((jobject)obj, activity_cls.getHandle()); } + void android_globals_store_window(struct _ANativeWindow *window) { @@ -70,15 +72,12 @@ android_globals_get_vm() void * android_globals_get_activity() { - return android_globals.activity; + return android_globals.activity.getHandle(); } void * android_globals_get_context() { - void *ret = android_globals.context; - if (ret == NULL) { - ret = android_globals.activity; - } - return ret; + return android_globals.context.isNull() ? android_globals.activity.getHandle() + : android_globals.context.getHandle(); } diff --git a/src/xrt/targets/openxr_android/src/main/java/org/freedesktop/monado/openxr_runtime/MonadoOpenXrApplication.java b/src/xrt/targets/openxr_android/src/main/java/org/freedesktop/monado/openxr_runtime/MonadoOpenXrApplication.java index 60022b11e..507e3e924 100644 --- a/src/xrt/targets/openxr_android/src/main/java/org/freedesktop/monado/openxr_runtime/MonadoOpenXrApplication.java +++ b/src/xrt/targets/openxr_android/src/main/java/org/freedesktop/monado/openxr_runtime/MonadoOpenXrApplication.java @@ -8,6 +8,9 @@ package org.freedesktop.monado.openxr_runtime; import android.app.Application; +import android.content.Context; + +import androidx.annotation.NonNull; import dagger.hilt.android.HiltAndroidApp; @@ -16,4 +19,16 @@ import dagger.hilt.android.HiltAndroidApp; */ @HiltAndroidApp public class MonadoOpenXrApplication extends Application { + static { + System.loadLibrary("monado-service"); + } + + @Override + public void onCreate() { + super.onCreate(); + + nativeStoreContext(getApplicationContext()); + } + + private native void nativeStoreContext(@NonNull Context context); } diff --git a/src/xrt/targets/service-lib/service_target.cpp b/src/xrt/targets/service-lib/service_target.cpp index a51906ea7..f525cf546 100644 --- a/src/xrt/targets/service-lib/service_target.cpp +++ b/src/xrt/targets/service-lib/service_target.cpp @@ -169,3 +169,17 @@ Java_org_freedesktop_monado_ipc_MonadoImpl_nativeShutdownServer(JNIEnv *env, job return IpcServerHelper::instance().shutdownServer(); } + +extern "C" JNIEXPORT void JNICALL +Java_org_freedesktop_monado_openxr_1runtime_MonadoOpenXrApplication_nativeStoreContext(JNIEnv *env, + jobject thiz, + jobject context) +{ + JavaVM *jvm = nullptr; + jint result = env->GetJavaVM(&jvm); + assert(result == JNI_OK); + assert(jvm); + + jni::init(env); + android_globals_store_vm_and_context(jvm, context); +}