Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Android: Panic, VK_ERROR_NATIVE_WINDOW_IN_USE_KHR with minimal example. #4538

Closed
bit-garden opened this issue Apr 19, 2022 · 3 comments
Closed
Labels
C-Bug An unexpected or incorrect behavior C-Startup A crash that occurs when first attempting to run a Bevy app O-Android Specific to the Android mobile operating system P-Crash A sudden unexpected crash

Comments

@bit-garden
Copy link

Panic at 'AndroidSurface failed: ERROR_NATIVE_WINDOW_IN_USE_KHR'.

Bevy version

Dev 0.8.0

Operating system & version

Win 10, targeting Android

Example code

use bevy::prelude::*;

#[bevy_main]
fn main() {
  App::new()
    .add_plugins(DefaultPlugins)
    .run();
}

Modified Cargo.toml

[features]
default = [
  #"animation",
  #"bevy_audio",
  #"bevy_gilrs",
  "bevy_winit",
  "render",
  #"png",
  #"hdr",
  #"vorbis",
  #"x11",
  #"filesystem_watcher",
]

Logcat

adb logcat --pid $(adb shell pidof -s rust.example.android)
--------- beginning of main
04-19 10:57:02.210 20626 20626 I example.androi: Late-enabling -Xcheck:jni
04-19 10:57:02.237 20626 20626 I example.androi: Unquickening 20 vdex files!
04-19 10:57:02.290 20626 20626 W System  : ClassLoader referenced unknown path:
04-19 10:57:02.308 20626 20626 D NetworkSecurityConfig: No Network Security Config specified, using platform default
04-19 10:57:02.309 20626 20626 D NetworkSecurityConfig: No Network Security Config specified, using platform default
04-19 10:57:02.517 20626 20652 D vulkan  : searching for layers in '/data/app/~~PVSHiznKqSiGFvVN4GRIEg==/rust.example.android-mcwo1u-dLD53f7vxyqo0yQ==/lib/arm64'
04-19 10:57:02.517 20626 20652 D vulkan  : searching for layers in '/data/app/~~PVSHiznKqSiGFvVN4GRIEg==/rust.example.android-mcwo1u-dLD53f7vxyqo0yQ==/base.apk!/lib/arm64-v8a'
04-19 10:57:02.545 20626 20652 I AdrenoVK-0: ===== BEGIN DUMP OF OVERRIDDEN SETTINGS =====
04-19 10:57:02.545 20626 20652 I AdrenoVK-0: ===== END DUMP OF OVERRIDDEN SETTINGS =====
04-19 10:57:02.545 20626 20652 I AdrenoVK-0: QUALCOMM build          : d4cfdf3, Ic907de5ed0
04-19 10:57:02.545 20626 20652 I AdrenoVK-0: Build Date              : 09/25/20
04-19 10:57:02.545 20626 20652 I AdrenoVK-0: Shader Compiler Version : EV031.32.02.01
04-19 10:57:02.545 20626 20652 I AdrenoVK-0: Local Branch            : mybrancheaff3d46-e51f-f136-8926-1458d0b52af0
04-19 10:57:02.545 20626 20652 I AdrenoVK-0: Remote Branch           : quic/gfx-adreno.lnx.1.0.r116-rel
04-19 10:57:02.545 20626 20652 I AdrenoVK-0: Remote Branch           : NONE
04-19 10:57:02.545 20626 20652 I AdrenoVK-0: Reconstruct Branch      : NOTHING
04-19 10:57:02.545 20626 20652 I AdrenoVK-0: Build Config            : S P 10.0.7 AArch64
04-19 10:57:02.545 20626 20652 I AdrenoVK-0: Driver Path             : /vendor/lib64/hw/vulkan.adreno.so
04-19 10:57:02.568 20626 20652 I event crates\bevy_render\src\renderer\mod.rs:98:  AdapterInfo { name: "Adreno (TM) 610", vendor: 20803, device: 100728832, device_type: IntegratedGpu, backend: Vulkan }
04-19 10:57:02.739 20626 20652 E vulkan  : native_window_api_connect() failed: Invalid argument (-22)
04-19 10:57:02.740 20626 20651 I RustStdoutStderr: thread '<unnamed>' panicked at 'AndroidSurface failed: ERROR_NATIVE_WINDOW_IN_USE_KHR', C:\Users\Dakun\.cargo\registry\src\github.com-1ecc6299db9ec823\wgpu-hal-0.12.4\src\vulkan\instance.rs:335:69
04-19 10:57:02.740 20626 20651 I RustStdoutStderr: note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

More information
https://stackoverflow.com/questions/68725535/what-does-vk-error-native-window-in-use-khr-mean

VK_ERROR_NATIVE_WINDOW_IN_USE_KHR means that window already has a swapchain. A window can only have one swapchain (counting even other API's swapchains like OpenGL and DXGI).

If you recreate the swapchain, you must either first destroy the old swapchain, or you must provide it to the oldSwapchain parameter.
@bit-garden bit-garden added C-Bug An unexpected or incorrect behavior S-Needs-Triage This issue needs to be labelled labels Apr 19, 2022
@alice-i-cecile alice-i-cecile added O-Android Specific to the Android mobile operating system P-Crash A sudden unexpected crash C-Startup A crash that occurs when first attempting to run a Bevy app and removed S-Needs-Triage This issue needs to be labelled labels Apr 19, 2022
@bit-garden
Copy link
Author

This line hinted how to get the rendering to work with Bevy 0.8.0-dev.

Block this line for target_os android.

    #[cfg(not(target_os = "android"))]
    handle_create_window_events(&mut app.world, &event_loop, &mut create_window_reader.0);

Add this before Bevy app run to wait for window to initialize.

  println!("Waiting for NativeScreen");
  loop {
    match ndk_glue::native_window().as_ref() {
      Some(_) => {
        println!("NativeScreen Found:{:?}", ndk_glue::native_window());
        break;
      }
      None => (),
    }
  }

So far this works while having only render and bevy-winit features enabled. I'm not sure of the rest of the features yet.

@rib
Copy link
Contributor

rib commented Jul 12, 2022

This was an issue that I also came across while working on #4913

I'm fairly sure the issue comes from the transient surface that's created by Bevy while it's looking for a wgpu adapter that's compatible with a given surface. Instead of Bevy passing in an existing surface that's associated with any primary window that's created it creates a new surface. On Android that leads to this error.

I tend to think that this design isn't ideal for any platform, since it doesn't conceptually ensure that Bevy finds an adapter that's compatible with the real surface that it will be rendering to. It's also possible for surfaces allocations to be a somewhat limited resource on some platforms e.g. if GPUs have constraints on what types of memory they can render to - so I tend to think that this design should ideally be changed for all platforms to aim to refer to any existing primary window instead of creating an ad hoc surface.

For reference; here's a patch I made when testing the deferred render init work on Android that avoids creating this extra surface: rib@1ea04f0

@mockersf
Copy link
Member

this should be fixed now, no temporary surface is created and Bevy waits for the resumed event to create its first window

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-Bug An unexpected or incorrect behavior C-Startup A crash that occurs when first attempting to run a Bevy app O-Android Specific to the Android mobile operating system P-Crash A sudden unexpected crash
Projects
None yet
Development

No branches or pull requests

4 participants