st/oxr,a/android,ipc/android: Move Android looper trick into the IPC client.

Part-of: <https://gitlab.freedesktop.org/monado/monado/-/merge_requests/2308>
This commit is contained in:
Jarvis Huang 2023-01-17 13:21:09 +08:00 committed by Rylie Pavlik
parent 2685fb1dc6
commit 9e46d56c03
4 changed files with 14 additions and 13 deletions

View file

@ -9,21 +9,18 @@
#include "android_looper.h"
#include "android/android_globals.h"
#include "util/u_logging.h"
#include "wrap/android.app.h"
#include <android_native_app_glue.h>
#include <android/looper.h>
#include <android/log.h>
void
android_looper_poll_until_activity_resumed()
android_looper_poll_until_activity_resumed(struct _JavaVM *vm, void *activity)
{
jni::init(android_globals_get_vm());
wrap::android::app::Activity activity{(jobject)android_globals_get_activity()};
if (!jni::env()->IsInstanceOf(activity.object().getHandle(),
jni::init(vm);
wrap::android::app::Activity activityObject{(jobject)activity};
if (!jni::env()->IsInstanceOf(activityObject.object().getHandle(),
jni::Class("android/app/NativeActivity").getHandle())) {
// skip if given activity is not android.app.NativeActivity
U_LOG_I("Activity is not NativeActivity, skip");
@ -31,7 +28,7 @@ android_looper_poll_until_activity_resumed()
}
// Activity is in resumed state if window is active. Check Activity#onPostResume for detail.
if (!activity.getWindow().isNull() && activity.getWindow().call<bool>("isActive()Z")) {
if (!activityObject.getWindow().isNull() && activityObject.getWindow().call<bool>("isActive()Z")) {
// Already in resume state, skip
U_LOG_I("Activity is NativeActivity and already in resume state with window available, skip");
return;

View file

@ -17,11 +17,13 @@
extern "C" {
#endif
struct _JavaVM;
/*!
* Poll the looper until activity is in resume state.
*/
void
android_looper_poll_until_activity_resumed();
android_looper_poll_until_activity_resumed(struct _JavaVM *vm, void *activity);
#ifdef __cplusplus
}

View file

@ -15,6 +15,7 @@
#include "util/u_logging.h"
#include "android/android_load_class.hpp"
#include "android/android_looper.h"
#include "wrap/android.app.h"
@ -76,10 +77,14 @@ int
ipc_client_android_blocking_connect(struct ipc_client_android *ica)
{
try {
// Trick to avoid deadlock on main thread: only applicable to NativeActivity with app-glue.
// blockingConnect will block until binder is ready, the app-glue code will deadlock without this.
JavaVM *vm = nullptr;
jni::env()->GetJavaVM(&vm);
android_looper_poll_until_activity_resumed(vm, ica->activity.object().getHandle());
int fd = ica->client.blockingConnect(ica->activity, XRT_ANDROID_PACKAGE);
return fd;
} catch (std::exception const &e) {
// Must catch and ignore any exceptions in the destructor!
U_LOG_E("Failure while connecting to IPC server: %s", e.what());
return -1;
}

View file

@ -27,7 +27,6 @@
#ifdef XRT_OS_ANDROID
#include "android/android_globals.h"
#include "android/android_looper.h"
#endif
#include "oxr_objects.h"
@ -291,8 +290,6 @@ oxr_instance_create(struct oxr_logger *log,
createInfo, XR_TYPE_INSTANCE_CREATE_INFO_ANDROID_KHR, XrInstanceCreateInfoAndroidKHR);
android_globals_store_vm_and_activity((struct _JavaVM *)create_info_android->applicationVM,
create_info_android->applicationActivity);
// Trick to avoid deadlock on main thread. Only works for NativeActivity with app-glue.
android_looper_poll_until_activity_resumed();
#endif