The open source OpenXR runtime
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

a/android: Migrate to using the MonadoView generated wrapper.

+21 -38
+21 -38
src/xrt/auxiliary/android/android_custom_surface.cpp
··· 16 16 17 17 #include "wrap/android.app.h" 18 18 #include "wrap/android.view.h" 19 + #include "org.freedesktop.monado.auxiliary.hpp" 19 20 20 21 #include <android/native_window_jni.h> 21 22 22 23 23 24 using wrap::android::app::Activity; 24 25 using wrap::android::view::SurfaceHolder; 26 + using wrap::org::freedesktop::monado::auxiliary::MonadoView; 25 27 26 28 27 29 struct android_custom_surface ··· 29 31 explicit android_custom_surface(jobject act); 30 32 ~android_custom_surface(); 31 33 Activity activity{}; 34 + MonadoView monadoView{}; 32 35 jni::Class monadoViewClass{}; 33 - jni::Object monadoView{}; 34 - jni::method_t waitGetSurfaceHolderMethod = nullptr; 35 - jni::method_t markAsDiscardedMethod = nullptr; 36 36 }; 37 37 38 38 ··· 41 41 android_custom_surface::~android_custom_surface() 42 42 { 43 43 // Tell Java that native code is done with this. 44 - if (!monadoView.isNull() && markAsDiscardedMethod != nullptr) { 45 - try { 46 - monadoView.call<void>(markAsDiscardedMethod); 47 - } catch (std::exception const &e) { 48 - U_LOG_E( 49 - "Failure while marking MonadoView as discarded: %s", 50 - e.what()); 44 + try { 45 + if (!monadoView.isNull()) { 46 + monadoView.markAsDiscardedByNative(); 51 47 } 48 + } catch (std::exception const &e) { 49 + // Must catch and ignore any exceptions in the destructor! 50 + U_LOG_E("Failure while marking MonadoView as discarded: %s", 51 + e.what()); 52 52 } 53 53 } 54 54 ··· 59 59 android_custom_surface_async_start(struct _JavaVM *vm, void *activity) 60 60 { 61 61 jni::init(vm); 62 - jni::method_t attachToActivity; 63 62 try { 64 - std::unique_ptr<android_custom_surface> ret = 65 - std::make_unique<android_custom_surface>((jobject)activity); 66 63 auto info = getAppInfo(XRT_ANDROID_PACKAGE, (jobject)activity); 67 64 if (info.isNull()) { 68 65 U_LOG_E( ··· 80 77 return nullptr; 81 78 } 82 79 80 + // Teach the wrapper our class before we start to use it. 81 + MonadoView::staticInitClass((jclass)clazz.object().getHandle()); 82 + std::unique_ptr<android_custom_surface> ret = 83 + std::make_unique<android_custom_surface>((jobject)activity); 84 + 83 85 // the 0 is to avoid this being considered "temporary" and to 84 86 // create a global ref. 85 87 ret->monadoViewClass = ··· 97 99 return nullptr; 98 100 } 99 101 100 - if (ret->monadoViewClass.isNull()) { 101 - U_LOG_E("MonadoView was null."); 102 - return nullptr; 103 - } 104 - 105 - ret->waitGetSurfaceHolderMethod = 106 - ret->monadoViewClass.getMethod( 107 - "waitGetSurfaceHolder", 108 - "(I)Landroid/view/SurfaceHolder;"); 109 - ret->markAsDiscardedMethod = ret->monadoViewClass.getMethod( 110 - "markAsDiscardedByNative", "()V;"); 111 - 112 - attachToActivity = ret->monadoViewClass.getStaticMethod( 113 - "attachToActivity", 114 - "(Landroid/app/Activity;)Lorg/freedesktop/monado/auxiliary/" 115 - "MonadoView;"); 102 + ret->monadoView = MonadoView::attachToActivity(ret->activity); 116 103 117 - ret->monadoView = ret->monadoViewClass.call<jni::Object>( 118 - attachToActivity, ret->activity.object()); 119 104 return ret.release(); 120 105 } catch (std::exception const &e) { 121 106 ··· 147 132 android_custom_surface_wait_get_surface( 148 133 struct android_custom_surface *custom_surface, uint64_t timeout_ms) 149 134 { 150 - jni::Object holder = nullptr; 135 + SurfaceHolder surfaceHolder{}; 151 136 try { 152 - 153 - holder = custom_surface->monadoView.call<jni::Object>( 154 - custom_surface->waitGetSurfaceHolderMethod, 155 - (int)timeout_ms); 137 + surfaceHolder = 138 + custom_surface->monadoView.waitGetSurfaceHolder(timeout_ms); 156 139 157 140 } catch (std::exception const &e) { 158 141 // do nothing right now. ··· 163 146 return nullptr; 164 147 } 165 148 166 - SurfaceHolder surfaceHolder(holder); 167 149 if (surfaceHolder.isNull()) { 168 150 return nullptr; 169 151 } ··· 171 153 if (surf.isNull()) { 172 154 return nullptr; 173 155 } 174 - return ANativeWindow_fromSurface(jni::env(), surf.object().getHandle()); 156 + return ANativeWindow_fromSurface(jni::env(), 157 + surf.object().makeLocalReference()); 175 158 }