From 632d0d762d1b7461b997e786b1647eaf0511397b Mon Sep 17 00:00:00 2001 From: Torstein Grindvik Date: Wed, 4 Oct 2023 18:12:06 +0200 Subject: [PATCH 1/7] Allow Bevy to start from non-main threads on supported platforms Signed-off-by: Torstein Grindvik --- crates/bevy_winit/src/lib.rs | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/crates/bevy_winit/src/lib.rs b/crates/bevy_winit/src/lib.rs index 21b694cdc2982..88f66ae3b27f3 100644 --- a/crates/bevy_winit/src/lib.rs +++ b/crates/bevy_winit/src/lib.rs @@ -77,6 +77,24 @@ pub struct WinitPlugin; impl Plugin for WinitPlugin { fn build(&self, app: &mut App) { let mut event_loop_builder = EventLoopBuilder::<()>::with_user_event(); + + #[cfg(feature = "x11")] + { + use winit::platform::x11::EventLoopBuilderExtX11; + + // This allows a Bevy app to be started and ran outside of the main thread. + // A use case for this is to allow external applications to spawn a thread + // which runs a Bevy app without requiring the Bevy app to need to reside on + // the main thread, which can be problematic. + event_loop_builder.with_any_thread(true); + } + + #[cfg(feature = "wayland")] + { + use winit::platform::wayland::EventLoopBuilderExtWayland; + event_loop_builder.with_any_thread(true); + } + #[cfg(target_os = "android")] { use winit::platform::android::EventLoopBuilderExtAndroid; From a01d7ce13a630cffc6bc9cd74a63fc05ebf265a2 Mon Sep 17 00:00:00 2001 From: Torstein Grindvik Date: Wed, 4 Oct 2023 18:28:03 +0200 Subject: [PATCH 2/7] Workaround for X11 feature being enable on macos Signed-off-by: Torstein Grindvik --- crates/bevy_winit/src/lib.rs | 31 ++++++++++++++++++------------- 1 file changed, 18 insertions(+), 13 deletions(-) diff --git a/crates/bevy_winit/src/lib.rs b/crates/bevy_winit/src/lib.rs index 88f66ae3b27f3..6d09a8e297f56 100644 --- a/crates/bevy_winit/src/lib.rs +++ b/crates/bevy_winit/src/lib.rs @@ -78,21 +78,26 @@ impl Plugin for WinitPlugin { fn build(&self, app: &mut App) { let mut event_loop_builder = EventLoopBuilder::<()>::with_user_event(); - #[cfg(feature = "x11")] + // This is needed because the X11 feature + // might be enabled for macos too. + #[cfg(target_os = "linux")] { - use winit::platform::x11::EventLoopBuilderExtX11; - - // This allows a Bevy app to be started and ran outside of the main thread. - // A use case for this is to allow external applications to spawn a thread - // which runs a Bevy app without requiring the Bevy app to need to reside on - // the main thread, which can be problematic. - event_loop_builder.with_any_thread(true); - } + #[cfg(feature = "x11")] + { + use winit::platform::x11::EventLoopBuilderExtX11; + + // This allows a Bevy app to be started and ran outside of the main thread. + // A use case for this is to allow external applications to spawn a thread + // which runs a Bevy app without requiring the Bevy app to need to reside on + // the main thread, which can be problematic. + event_loop_builder.with_any_thread(true); + } - #[cfg(feature = "wayland")] - { - use winit::platform::wayland::EventLoopBuilderExtWayland; - event_loop_builder.with_any_thread(true); + #[cfg(feature = "wayland")] + { + use winit::platform::wayland::EventLoopBuilderExtWayland; + event_loop_builder.with_any_thread(true); + } } #[cfg(target_os = "android")] From ba604c73fb42f7e621c87232cafd515603eac079 Mon Sep 17 00:00:00 2001 From: Torstein Grindvik Date: Wed, 4 Oct 2023 19:22:42 +0200 Subject: [PATCH 3/7] Make comment more precise Signed-off-by: Torstein Grindvik --- crates/bevy_winit/src/lib.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/crates/bevy_winit/src/lib.rs b/crates/bevy_winit/src/lib.rs index 6d09a8e297f56..7f7c763082c4b 100644 --- a/crates/bevy_winit/src/lib.rs +++ b/crates/bevy_winit/src/lib.rs @@ -78,8 +78,8 @@ impl Plugin for WinitPlugin { fn build(&self, app: &mut App) { let mut event_loop_builder = EventLoopBuilder::<()>::with_user_event(); - // This is needed because the X11 feature - // might be enabled for macos too. + // This is needed because the features checked in the inner + // block might be enabled on other platforms than linux. #[cfg(target_os = "linux")] { #[cfg(feature = "x11")] From cef667192107a262fe4601f22ff72fa06ebd4233 Mon Sep 17 00:00:00 2001 From: Torstein Grindvik Date: Wed, 4 Oct 2023 19:32:38 +0200 Subject: [PATCH 4/7] Allow on win too Signed-off-by: Torstein Grindvik --- crates/bevy_winit/src/lib.rs | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/crates/bevy_winit/src/lib.rs b/crates/bevy_winit/src/lib.rs index 7f7c763082c4b..7a92632b24edf 100644 --- a/crates/bevy_winit/src/lib.rs +++ b/crates/bevy_winit/src/lib.rs @@ -100,6 +100,12 @@ impl Plugin for WinitPlugin { } } + #[cfg(target_os = "windows")] + { + use winit::platform::windows::EventLoopBuilderExtWindows; + event_loop_builder.with_any_thread(true); + } + #[cfg(target_os = "android")] { use winit::platform::android::EventLoopBuilderExtAndroid; From ec70e2cea56a5c2551f3e29d496197b3bc7012ad Mon Sep 17 00:00:00 2001 From: Torstein Grindvik Date: Thu, 5 Oct 2023 09:25:29 +0200 Subject: [PATCH 5/7] Default off, field in plugin Signed-off-by: Torstein Grindvik --- crates/bevy_internal/src/default_plugins.rs | 2 +- crates/bevy_winit/src/lib.rs | 21 +++++++++++++++++---- 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/crates/bevy_internal/src/default_plugins.rs b/crates/bevy_internal/src/default_plugins.rs index c5eac80e7cae3..d95957ab43dbf 100644 --- a/crates/bevy_internal/src/default_plugins.rs +++ b/crates/bevy_internal/src/default_plugins.rs @@ -64,7 +64,7 @@ impl PluginGroup for DefaultPlugins { #[cfg(feature = "bevy_winit")] { - group = group.add(bevy_winit::WinitPlugin); + group = group.add(bevy_winit::WinitPlugin::default()); } #[cfg(feature = "bevy_render")] diff --git a/crates/bevy_winit/src/lib.rs b/crates/bevy_winit/src/lib.rs index 7a92632b24edf..9c1ddc76f09d2 100644 --- a/crates/bevy_winit/src/lib.rs +++ b/crates/bevy_winit/src/lib.rs @@ -72,7 +72,20 @@ pub static ANDROID_APP: std::sync::OnceLock = std::sync::OnceLock::n /// replace the existing [`App`] runner with one that constructs an [event loop](EventLoop) to /// receive window and input events from the OS. #[derive(Default)] -pub struct WinitPlugin; +pub struct WinitPlugin { + /// Allows the window (and the event loop) to be created on any thread + /// instead of only the main thread. + /// + /// See [`winit with_any_thread`]. + /// + /// # Supported platforms + /// + /// Only works on Linux (X11/Wayland) and Windows. + /// This field is ignored on other platforms. + /// + /// [`winit with_any_thread`]: https://docs.rs/winit/latest/winit/event_loop/struct.EventLoopBuilder.html#method.with_any_thread + pub run_on_any_thread: bool, +} impl Plugin for WinitPlugin { fn build(&self, app: &mut App) { @@ -90,20 +103,20 @@ impl Plugin for WinitPlugin { // A use case for this is to allow external applications to spawn a thread // which runs a Bevy app without requiring the Bevy app to need to reside on // the main thread, which can be problematic. - event_loop_builder.with_any_thread(true); + event_loop_builder.with_any_thread(self.run_on_any_thread); } #[cfg(feature = "wayland")] { use winit::platform::wayland::EventLoopBuilderExtWayland; - event_loop_builder.with_any_thread(true); + event_loop_builder.with_any_thread(self.run_on_any_thread); } } #[cfg(target_os = "windows")] { use winit::platform::windows::EventLoopBuilderExtWindows; - event_loop_builder.with_any_thread(true); + event_loop_builder.with_any_thread(self.run_on_any_thread); } #[cfg(target_os = "android")] From f4ee6f9e862bcd8545d71a15b7d805a81295c626 Mon Sep 17 00:00:00 2001 From: James Liu Date: Thu, 5 Oct 2023 21:46:51 -0400 Subject: [PATCH 6/7] Link directly to symbol in dependency instead of docs.rs --- crates/bevy_winit/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/bevy_winit/src/lib.rs b/crates/bevy_winit/src/lib.rs index 9c1ddc76f09d2..52fdee107fe7d 100644 --- a/crates/bevy_winit/src/lib.rs +++ b/crates/bevy_winit/src/lib.rs @@ -83,7 +83,7 @@ pub struct WinitPlugin { /// Only works on Linux (X11/Wayland) and Windows. /// This field is ignored on other platforms. /// - /// [`winit with_any_thread`]: https://docs.rs/winit/latest/winit/event_loop/struct.EventLoopBuilder.html#method.with_any_thread + /// [`winit with_any_thread`]: winit::event_loop::EventLoopBuilder::with_any_thread pub run_on_any_thread: bool, } From 2e8341cab5544ecf8476ea929113143b1197ad90 Mon Sep 17 00:00:00 2001 From: Torstein Grindvik Date: Fri, 6 Oct 2023 12:48:14 +0200 Subject: [PATCH 7/7] Point docs to where any_thread is discussed Signed-off-by: Torstein Grindvik --- crates/bevy_winit/src/lib.rs | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/crates/bevy_winit/src/lib.rs b/crates/bevy_winit/src/lib.rs index 52fdee107fe7d..b22b54e86708b 100644 --- a/crates/bevy_winit/src/lib.rs +++ b/crates/bevy_winit/src/lib.rs @@ -76,14 +76,12 @@ pub struct WinitPlugin { /// Allows the window (and the event loop) to be created on any thread /// instead of only the main thread. /// - /// See [`winit with_any_thread`]. + /// See [`EventLoopBuilder::build`] for more information on this. /// /// # Supported platforms /// /// Only works on Linux (X11/Wayland) and Windows. /// This field is ignored on other platforms. - /// - /// [`winit with_any_thread`]: winit::event_loop::EventLoopBuilder::with_any_thread pub run_on_any_thread: bool, }