From ce3750bfe19d4b8e0895ad82032b6ea583bbefc4 Mon Sep 17 00:00:00 2001 From: Ryan Pavlik Date: Wed, 28 Oct 2020 17:15:59 -0500 Subject: [PATCH] a/android: Store a native pointer in the MonadoView. --- src/xrt/auxiliary/android/android_custom_surface.cpp | 3 ++- .../android/org.freedesktop.monado.auxiliary.cpp | 4 ++-- .../android/org.freedesktop.monado.auxiliary.hpp | 7 ++++--- .../android/org.freedesktop.monado.auxiliary.impl.hpp | 7 +++++-- .../org/freedesktop/monado/auxiliary/MonadoView.java | 10 +++++++--- 5 files changed, 20 insertions(+), 11 deletions(-) diff --git a/src/xrt/auxiliary/android/android_custom_surface.cpp b/src/xrt/auxiliary/android/android_custom_surface.cpp index f9f308690..2f3a4f1f9 100644 --- a/src/xrt/auxiliary/android/android_custom_surface.cpp +++ b/src/xrt/auxiliary/android/android_custom_surface.cpp @@ -99,7 +99,8 @@ android_custom_surface_async_start(struct _JavaVM *vm, void *activity) return nullptr; } - ret->monadoView = MonadoView::attachToActivity(ret->activity); + ret->monadoView = + MonadoView::attachToActivity(ret->activity, ret.get()); return ret.release(); } catch (std::exception const &e) { diff --git a/src/xrt/auxiliary/android/org.freedesktop.monado.auxiliary.cpp b/src/xrt/auxiliary/android/org.freedesktop.monado.auxiliary.cpp index 4564f92e0..86bbe1ab1 100644 --- a/src/xrt/auxiliary/android/org.freedesktop.monado.auxiliary.cpp +++ b/src/xrt/auxiliary/android/org.freedesktop.monado.auxiliary.cpp @@ -16,8 +16,8 @@ namespace org::freedesktop::monado::auxiliary { : MetaBase(MonadoView::getTypeName(), clazz), attachToActivity(classRef().getStaticMethod( "attachToActivity", - "(Landroid/app/Activity;)Lorg/freedesktop/monado/" - "auxiliary/MonadoView;")), + "(Landroid/app/Activity;J)Lorg/freedesktop/" + "monado/auxiliary/MonadoView;")), waitGetSurfaceHolder(classRef().getMethod( "waitGetSurfaceHolder", "(I)Landroid/view/SurfaceHolder;")), markAsDiscardedByNative( diff --git a/src/xrt/auxiliary/android/org.freedesktop.monado.auxiliary.hpp b/src/xrt/auxiliary/android/org.freedesktop.monado.auxiliary.hpp index 0b759f71f..2221227e6 100644 --- a/src/xrt/auxiliary/android/org.freedesktop.monado.auxiliary.hpp +++ b/src/xrt/auxiliary/android/org.freedesktop.monado.auxiliary.hpp @@ -47,14 +47,15 @@ namespace org::freedesktop::monado::auxiliary { * * Java prototype: * `public static org.freedesktop.monado.auxiliary.MonadoView - * attachToActivity(android.app.Activity);` + * attachToActivity(android.app.Activity, long);` * * JNI signature: - * (Landroid/app/Activity;)Lorg/freedesktop/monado/auxiliary/MonadoView; + * (Landroid/app/Activity;J)Lorg/freedesktop/monado/auxiliary/MonadoView; * */ static MonadoView - attachToActivity(android::app::Activity const &activity); + attachToActivity(android::app::Activity const &activity, + void *nativePointer); /*! * Wrapper for the waitGetSurfaceHolder method diff --git a/src/xrt/auxiliary/android/org.freedesktop.monado.auxiliary.impl.hpp b/src/xrt/auxiliary/android/org.freedesktop.monado.auxiliary.impl.hpp index e1fa17f57..7ea15118b 100644 --- a/src/xrt/auxiliary/android/org.freedesktop.monado.auxiliary.impl.hpp +++ b/src/xrt/auxiliary/android/org.freedesktop.monado.auxiliary.impl.hpp @@ -16,10 +16,13 @@ namespace wrap { namespace org::freedesktop::monado::auxiliary { inline MonadoView - MonadoView::attachToActivity(android::app::Activity const &activity) + MonadoView::attachToActivity(android::app::Activity const &activity, + void *nativePointer) { return MonadoView(Meta::data().clazz().call( - Meta::data().attachToActivity, activity.object())); + Meta::data().attachToActivity, activity.object(), + static_cast( + reinterpret_cast(nativePointer)))); } inline android::view::SurfaceHolder diff --git a/src/xrt/auxiliary/android/src/main/java/org/freedesktop/monado/auxiliary/MonadoView.java b/src/xrt/auxiliary/android/src/main/java/org/freedesktop/monado/auxiliary/MonadoView.java index 9787e4714..74c1e0166 100644 --- a/src/xrt/auxiliary/android/src/main/java/org/freedesktop/monado/auxiliary/MonadoView.java +++ b/src/xrt/auxiliary/android/src/main/java/org/freedesktop/monado/auxiliary/MonadoView.java @@ -55,10 +55,13 @@ public class MonadoView extends SurfaceView implements SurfaceHolder.Callback, S private SurfaceHolder currentSurfaceHolder = null; /// Guarded by usedByNativeCodeSync private boolean usedByNativeCode = false; + /// Contains the pointer to the native android_custom_surface object. + private long nativePointer = 0; - private MonadoView(Activity activity) { + private MonadoView(Activity activity, long nativePointer) { super(activity); this.activity = activity; + this.nativePointer = nativePointer; Method method; try { method = activity.getWindow().getDecorView().getClass().getMethod("setSystemUiVisibility", int.class); @@ -78,10 +81,10 @@ public class MonadoView extends SurfaceView implements SurfaceHolder.Callback, S @NonNull @Keep @SuppressWarnings("deprecation") - public static MonadoView attachToActivity(@NonNull final Activity activity) { + public static MonadoView attachToActivity(@NonNull final Activity activity, long nativePointer) { Log.i(TAG, "Starting to add a new surface!"); - final MonadoView view = new MonadoView(activity); + final MonadoView view = new MonadoView(activity, nativePointer); activity.runOnUiThread(() -> { Log.i(TAG, "Starting runOnUiThread"); @@ -152,6 +155,7 @@ public class MonadoView extends SurfaceView implements SurfaceHolder.Callback, S Log.w(TAG, "This should not have happened: Discarding by native code, but not marked as used!"); } usedByNativeCode = false; + nativePointer = 0; usedByNativeCodeSync.notifyAll(); }