From 0a9fe3caa976dad5d7fbb83e7e9350459db17d43 Mon Sep 17 00:00:00 2001 From: Mis012 Date: Mon, 9 Jan 2023 13:55:25 +0100 Subject: [PATCH] src/libandroid/native_window.c: [belongs in openxr_bio]: use dlopen/dlsym to avoid hard dependency on openxr loader --- src/libandroid/native_window.c | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/src/libandroid/native_window.c b/src/libandroid/native_window.c index 3d3a6093..cd3009ec 100644 --- a/src/libandroid/native_window.c +++ b/src/libandroid/native_window.c @@ -45,6 +45,8 @@ #define XR_USE_PLATFORM_EGL #include +#include + #include // FIXME: put the header in a common place @@ -423,6 +425,19 @@ EGLSurface bionic_eglCreateWindowSurface(EGLDisplay display, EGLConfig config, s // FIXME 2: this BLATANTLY belongs elsewhere +typedef XrResult(*xr_func)(); + +// avoid hard dependency on libopenxr_loader for the three functions that we only ever call when running a VR app +static void *openxr_loader_handle = NULL; +static inline __attribute__((__always_inline__)) XrResult xr_lazy_call(char *func_name, ...) { + if(!openxr_loader_handle) { + openxr_loader_handle = dlopen("libopenxr_loader.so.1", RTLD_LAZY); + } + + xr_func func = dlsym(openxr_loader_handle, func_name); + return func(__builtin_va_arg_pack()); +} + static void xrInitializeLoaderKHR_noop() //FIXME: does it really return void? { printf("STUB: xrInitializeLoaderKHR_noop called\n"); @@ -447,7 +462,7 @@ XrResult bionic_xrCreateSession(XrInstance instance, XrSessionCreateInfo *create egl_bind.config = android_bind->config; egl_bind.context = android_bind->context; createInfo->next = &egl_bind; - xrCreateSession(instance, createInfo, session); + return xr_lazy_call("xrCreateSession", instance, createInfo, session); } @@ -458,14 +473,14 @@ XrResult bionic_xrGetInstanceProcAddr(XrInstance instance, const char *name, PFN *func = xrInitializeLoaderKHR_noop; return (XrResult)xrInitializeLoaderKHR_noop; //TODO: is this correct return value? } else { - return xrGetInstanceProcAddr(instance, name, func); + return xr_lazy_call("xrGetInstanceProcAddr", instance, name, func); } } -void * bionic_xrCreateInstance(XrInstanceCreateInfo *createInfo, XrInstance *instance) +XrResult bionic_xrCreateInstance(XrInstanceCreateInfo *createInfo, XrInstance *instance) { const char* enabled_exts[2] = {"XR_KHR_opengl_es_enable", "XR_MNDX_egl_enable"}; createInfo->enabledExtensionCount = 2; createInfo->enabledExtensionNames = enabled_exts; - xrCreateInstance(createInfo, instance); + return xr_lazy_call("xrCreateInstance", createInfo, instance); }