BabylonNative - Is it possible to restart an Android activity?

Indeed onDestroy is called, but the question is how to properly stop XR?

something like mView.eval("exitAR();", "ActivityScript");
where exitAR is a JS function defined in experience.js

1 Like

Ohh interesting, I was trying to stop XR in the native C ++ part and not in the JS part … that’s probably what I was doing wrong …
I’m trying this this afternoon

I’m trying to make it work on my side. destruction is async, might need some await or something to make it work.

my function is :


function exitAR()
{
    if (currentXR) {
        console.log("Exiting XR");
        currentXR.baseExperience.exitXRAsync();
        currentXR = null;
    }
}

and currentXR is assigned with xr just after enterXRAsync call

:sob: I still have an issue when g_graphics tried to be released (forcing g_graphics.reset(); when activity is destroying or when si restarting …)…

I’m still investigating with another idea…

1 Like

It’s an opengl context switching issue. At some point, a (new?) context is bound for the thread but I find no call to make it current. This newly bound context is not the expected one. Still searching …

1 Like

Don’t hesitate to let me know if I can help you somewhere …

1 Like

I’m still debugging today. At this stage, I didn’t figure out what is changing the GL context on the BN side. My guess is with Surface/View life cycle, the system destroys something that change the GL Context. I tried wo unbind the current GL context on the Java side in the surfaceDestroyed callback without much luck.
Today, I’ll try to put a breakpoint in eglMakeCurrent to see if anything calls it. And I’ll also try with RenderDoc to see if I get better logs.

1 Like

No stacktrace with lldb/AndroidStudio. RenderDoc doesn’t hook on the app (might be my configuration in this case).
I still believe the Android system does something when XR view is destroyed. I’ll try now to do a minimal repro wihtin XR: keep view management but do not re-create surface in XR.cpp

I removed xrSurfaceView from BabylonView.java. No view = no surface for XR = no rendering and there is still a context issue. So, it’s might not be related to the surface, I leasst not the way I imagined it. Next on the list is XR resources initialization. Now I’ll disable nativeXR code until it works !

I’m removing more and more code. Crash still occurs with same wrong GL context.
Surface is now never changed. No rendering code in XR.cpp and less rendering code in NativeXR.cpp
I’m starting to wonder if it might be related to ARCore.
@Eric_Beets Can you please try to use a more recent version ? Changing the versison in the following lines should do the trick :

I think that I have already tried it, I just re-test, and still a crash.
But I also have the feeling it come from the ARCore, some resources are not released/close or something like that …

1 Like

yes, I’ve update the issue on GitHub:

2 Likes

Doing a bit of progress: Binding opengl context only and not the surface make it not crash in later use of the context.

EGLDisplay previousDisplay{ eglGetDisplay(EGL_DEFAULT_DISPLAY) };
EGLSurface previousDrawSurface{ eglGetCurrentSurface(EGL_DRAW) };
EGLSurface previousReadSurface{ eglGetCurrentSurface(EGL_READ) };
EGLContext previousContext{ eglGetCurrentContext() };
eglMakeCurrent(eglGetDisplay(EGL_DEFAULT_DISPLAY), EGL_NO_SURFACE, EGL_NO_SURFACE, previousContext);
ArSession_update(xrContext->Session, xrContext->Frame);
eglMakeCurrent(previousDisplay, previousDrawSurface, previousReadSurface, previousContext);

Now, there are a bunch of issues with engine destruction/re-initialization…

@Eric_Beets

I’ve opened a PR on your branch, can you test it please?

I just tested the, it crashes to renderer_gl.h:1338

With the following stack:

2021-09-13 18:46:26.662 21920-22035/com.android.babylonnative.playground I/BabylonNative: BJS - [18:46:26]: FPS: Infinity
2021-09-13 18:46:47.522 21920-22095/com.android.babylonnative.playground E/native: E0913 18:46:47.522607   22095 camera_image_stream.cc:271] FAILED_PRECONDITION: Failed to extract the native metadata, status=DEADLINE_EXCEEDED: Timed out waiting for metadata.
    === Source Location Trace: ===
    third_party/arcore/ar/camera/android/metadata_cache.cc:53
    third_party/arcore/ar/camera/android/metadata_extraction.cc:54
    
    === Source Location Trace: ===
    third_party/arcore/ar/camera/android/camera_image_stream.cc:205
2021-09-13 18:46:57.905 21920-22095/com.android.babylonnative.playground E/native: E0913 18:46:57.905314   22095 camera_image_stream.cc:271] FAILED_PRECONDITION: Failed to extract the native metadata, status=DEADLINE_EXCEEDED: Timed out waiting for metadata.
    === Source Location Trace: ===
    third_party/arcore/ar/camera/android/metadata_cache.cc:53
    third_party/arcore/ar/camera/android/metadata_extraction.cc:54
    
    === Source Location Trace: ===
    third_party/arcore/ar/camera/android/camera_image_stream.cc:205
2021-09-13 18:46:57.906 21920-22095/com.android.babylonnative.playground E/native: E0913 18:46:57.906103   22095 camera_image_stream.cc:232] Camera Image Stream failed to dequeue Image from ImageReader. status=UNAVAILABLE: AImageReader_acquireLatestImage [type.googleapis.com/util.ErrorSpacePayload='ImageReaderStatusErrorSpaceClass::AMEDIA_IMGREADER_NO_BUFFER_AVAILABLE']
    === Source Location Trace: ===
    third_party/arcore/ar/camera/android/imagereader_status_error_space.cc:112
    third_party/arcore/ar/camera/android/imagereader_ndk_wrapper.cc:241
     acquired_image_count=3
2021-09-13 18:47:14.422 21920-21941/com.android.babylonnative.playground E/ACameraDevice: Frame number out of order. Expect 5 but get 10
2021-09-13 18:47:21.402 21920-22095/com.android.babylonnative.playground E/native: E0913 18:47:21.402012   22095 camera_image_stream.cc:271] FAILED_PRECONDITION: Failed to extract the native metadata, status=DEADLINE_EXCEEDED: Timed out waiting for metadata.
    === Source Location Trace: ===
    third_party/arcore/ar/camera/android/metadata_cache.cc:53
    third_party/arcore/ar/camera/android/metadata_extraction.cc:54

I moved engine shutdown in main activity. I get no more crash with multiple back and forth between main & playground activities.
PR is in sync with my latest changes.

1 Like

:thinking: I just tried it on S8+ and S9+, and it still crashes …
I tested it on a Xiaomi MiA3, the app does not crash, but when I restart the AR activity, the default box is not in the scene.
Is there something particular on Samsung devices ?

yes, we faced some issues with Samsung. Like this one : AR view broken on Samsung SM-T820 Ā· Issue #788 Ā· BabylonJS/BabylonNative Ā· GitHub
Let me take a look for the default box.