a/android: Store a native pointer in the MonadoView.

This commit is contained in:
Ryan Pavlik 2020-10-28 17:15:59 -05:00
parent 3bc84cca9e
commit ce3750bfe1
5 changed files with 20 additions and 11 deletions

View file

@ -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) {

View file

@ -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(

View file

@ -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

View file

@ -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<jni::Object>(
Meta::data().attachToActivity, activity.object()));
Meta::data().attachToActivity, activity.object(),
static_cast<long long>(
reinterpret_cast<intptr_t>(nativePointer))));
}
inline android::view::SurfaceHolder

View file

@ -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();
}