google / filament

Filament is a real-time physically based rendering engine for Android, iOS, Windows, Linux, macOS, and WebGL2
https://google.github.io/filament/
Apache License 2.0
17.63k stars 1.86k forks source link

assertion "sVirtualMachine" failed when calling Stream::Builder::build #2582

Closed balarayen closed 4 years ago

balarayen commented 4 years ago

Using sceneform sdk v1.16.0(uses filament-android 1.4.5) while trying to play a video on an object, streamsource creation crashes with the following error:

2020-05-24 21:00:36.950 28751-28751/com.example.ar A/libc: ~/filament-v145/filament/backend/src/android/VirtualMachineEnv.h:42: static JNIEnv *filament::VirtualMachineEnv::getThreadEnvironment(): assertion "sVirtualMachine" failed 2020-05-24 21:00:36.950 28751-28751/com.example.ar A/libc: Fatal signal 6 (SIGABRT), code -1 (SI_QUEUE) in tid 28751 (com.example.ar), pid 28751 (com.example.ar)

Looks like ::filament::VirtualMachineEnv::JNI_OnLoad(vm) is not getting invoked(but the same error is not happening when building Stream.Builder().stream(textureId) ).

Steps to reproduce the behavior: This error happens only when creating a streamsource from sceneformsdk below

  /** Creates an ExternalTexture with a new Android {@link SurfaceTexture} and {@link Surface}. */
  @SuppressWarnings("initialization")
  public ExternalTexture() {
    // Create the Android surface texture.
    SurfaceTexture surfaceTexture = new SurfaceTexture(0);
    surfaceTexture.detachFromGLContext();
    this.surfaceTexture = surfaceTexture;

    // Create the Android surface.
    surface = new Surface(surfaceTexture);

    // Create the filament stream.
    Stream stream =
        new Stream.Builder()
            .stream(surfaceTexture).build(EngineInstance.getEngine().getFilamentEngine());

    initialize(stream);
  }

Expected behavior It should not crash.

Smartphone:

Additional context But the crash is not happening when creating a CameraStream in sceneform sdk with the following stream creation:

  ExternalTexture(int textureId, int width, int height) {
    // Explicitly set the surface and surfaceTexture to null, since they are unused in this case.
    surfaceTexture = null;
    surface = null;

    // Create the filament stream.
    Stream stream =
        new Stream.Builder()
            .stream(textureId)
                .width(width)
                .height(height)
                .build(EngineInstance.getEngine().getFilamentEngine());

    initialize(stream);
  }

Unfortunately I couldn't upgrade the filament version in sceneform sdk due to matc incompatibility. If there are any know bug fixes available which I can cherry pick is also great.

romainguy commented 4 years ago

We've made a number of improvements related to JNI since 1.4.5. Here are all the commits with JNI related changes: https://github.com/search?l=&o=desc&q=jni+repo%3Agoogle%2Ffilament+committer-date%3A%3E2020-01-27&s=committer-date&type=Commits

Your particular issue is probably related to the fact that previous versions of the JNI bindings used multiple copies of Filament if you used gltfio and filament like Sceneform does. This would lead to issues with static intializers like sVirtualMachine. The fix is here: https://github.com/google/filament/commit/d84d095c97905f9017bbec2d0e0cd96f78485045 Since it's the first commit after we branched 1.4.5 you should be able to compile from that commit directly.

balarayen commented 4 years ago

Thanks @romainguy . It works after cherry picking few commits. I could play a video on a 3D object now. It works only with debug error but with release aar, getting the following error:

2020-05-25 20:47:42.170 26852-26852/com.example.ar A/libc: Fatal signal 11 (SIGSEGV), code 1 (SEGV_MAPERR), fault addr 0xb0 in tid 26852 (com.example.ar), pid 26852 (com.example.ar)

It crashes when calling nLoadResources in ResourceLoader

    public ResourceLoader loadResources(@NonNull FilamentAsset asset) {
        nLoadResources(this.mNativeObject, asset.getNativeObject());
        return this;
    }

Any pointers will help.

balarayen commented 4 years ago

Cherrypicking #2332 helped fixing this issue as well. Thanks a lot @romainguy for all your quick responses.

Thanks @romainguy . It works after cherry picking few commits. I could play a video on a 3D object now. It works only with debug error but with release aar, getting the following error:

2020-05-25 20:47:42.170 26852-26852/com.example.ar A/libc: Fatal signal 11 (SIGSEGV), code 1 (SEGV_MAPERR), fault addr 0xb0 in tid 26852 (com.example.ar), pid 26852 (com.example.ar)

It crashes when calling nLoadResources in ResourceLoader

    public ResourceLoader loadResources(@NonNull FilamentAsset asset) {
        nLoadResources(this.mNativeObject, asset.getNativeObject());
        return this;
    }

Any pointers will help.