
# Objective - Upgrade winit to v0.30 - Fixes https://github.com/bevyengine/bevy/issues/13331 ## Solution This is a rewrite/adaptation of the new trait system described and implemented in `winit` v0.30. ## Migration Guide The custom UserEvent is now renamed as WakeUp, used to wake up the loop if anything happens outside the app (a new [custom_user_event](https://github.com/bevyengine/bevy/pull/13366/files#diff-2de8c0a8d3028d0059a3d80ae31b2bbc1cde2595ce2d317ea378fe3e0cf6ef2d) shows this behavior. The internal `UpdateState` has been removed and replaced internally by the AppLifecycle. When changed, the AppLifecycle is sent as an event. The `UpdateMode` now accepts only two values: `Continuous` and `Reactive`, but the latter exposes 3 new properties to enable reactive to device, user or window events. The previous `UpdateMode::Reactive` is now equivalent to `UpdateMode::reactive()`, while `UpdateMode::ReactiveLowPower` to `UpdateMode::reactive_low_power()`. The `ApplicationLifecycle` has been renamed as `AppLifecycle`, and now contains the possible values of the application state inside the event loop: * `Idle`: the loop has not started yet * `Running` (previously called `Started`): the loop is running * `WillSuspend`: the loop is going to be suspended * `Suspended`: the loop is suspended * `WillResume`: the loop is going to be resumed Note: the `Resumed` state has been removed since the resumed app is just running. Finally, now that `winit` enables this, it extends the `WinitPlugin` to support custom events. ## Test platforms - [x] Windows - [x] MacOs - [x] Linux (x11) - [x] Linux (Wayland) - [x] Android - [x] iOS - [x] WASM/WebGPU - [x] WASM/WebGL2 ## Outstanding issues / regressions - [ ] iOS: build failed in CI - blocking, but may just be flakiness - [x] Cross-platform: when the window is maximised, changes in the scale factor don't apply, to make them apply one has to make the window smaller again. (Re-maximising keeps the updated scale factor) - non-blocking, but good to fix - [ ] Android: it's pretty easy to quickly open and close the app and then the music keeps playing when suspended. - non-blocking but worrying - [ ] Web: the application will hang when switching tabs - Not new, duplicate of https://github.com/bevyengine/bevy/issues/13486 - [ ] Cross-platform?: Screenshot failure, `ERROR present_frames: wgpu_core::present: No work has been submitted for this frame before` taking the first screenshot, but after pressing space - non-blocking, but good to fix --------- Co-authored-by: François <francois.mockers@vleue.com>
128 lines
5.5 KiB
Rust
128 lines
5.5 KiB
Rust
#![allow(unsafe_code)]
|
|
|
|
use bevy_ecs::prelude::Component;
|
|
use raw_window_handle::{
|
|
DisplayHandle, HandleError, HasDisplayHandle, HasWindowHandle, RawDisplayHandle,
|
|
RawWindowHandle, WindowHandle,
|
|
};
|
|
use std::{
|
|
any::Any,
|
|
marker::PhantomData,
|
|
ops::Deref,
|
|
sync::{Arc, Mutex},
|
|
};
|
|
|
|
/// A wrapper over a window.
|
|
///
|
|
/// This allows us to extend the lifetime of the window, so it doesn't get eagerly dropped while a
|
|
/// pipelined renderer still has frames in flight that need to draw to it.
|
|
///
|
|
/// This is achieved by storing a shared reference to the window in the [`RawHandleWrapper`],
|
|
/// which gets picked up by the renderer during extraction.
|
|
#[derive(Debug)]
|
|
pub struct WindowWrapper<W> {
|
|
reference: Arc<dyn Any + Send + Sync>,
|
|
ty: PhantomData<W>,
|
|
}
|
|
|
|
impl<W: Send + Sync + 'static> WindowWrapper<W> {
|
|
/// Creates a `WindowWrapper` from a window.
|
|
pub fn new(window: W) -> WindowWrapper<W> {
|
|
WindowWrapper {
|
|
reference: Arc::new(window),
|
|
ty: PhantomData,
|
|
}
|
|
}
|
|
}
|
|
|
|
impl<W: 'static> Deref for WindowWrapper<W> {
|
|
type Target = W;
|
|
|
|
fn deref(&self) -> &Self::Target {
|
|
self.reference.downcast_ref::<W>().unwrap()
|
|
}
|
|
}
|
|
|
|
/// A wrapper over [`RawWindowHandle`] and [`RawDisplayHandle`] that allows us to safely pass it across threads.
|
|
///
|
|
/// Depending on the platform, the underlying pointer-containing handle cannot be used on all threads,
|
|
/// and so we cannot simply make it (or any type that has a safe operation to get a [`RawWindowHandle`] or [`RawDisplayHandle`])
|
|
/// thread-safe.
|
|
#[derive(Debug, Clone, Component)]
|
|
pub struct RawHandleWrapper {
|
|
_window: Arc<dyn Any + Send + Sync>,
|
|
/// Raw handle to a window.
|
|
pub window_handle: RawWindowHandle,
|
|
/// Raw handle to the display server.
|
|
pub display_handle: RawDisplayHandle,
|
|
}
|
|
|
|
impl RawHandleWrapper {
|
|
/// Creates a `RawHandleWrapper` from a `WindowWrapper`.
|
|
pub fn new<W: HasWindowHandle + HasDisplayHandle + 'static>(
|
|
window: &WindowWrapper<W>,
|
|
) -> Result<RawHandleWrapper, HandleError> {
|
|
Ok(RawHandleWrapper {
|
|
_window: window.reference.clone(),
|
|
window_handle: window.window_handle()?.as_raw(),
|
|
display_handle: window.display_handle()?.as_raw(),
|
|
})
|
|
}
|
|
|
|
/// Returns a [`HasWindowHandle`] + [`HasDisplayHandle`] impl, which exposes [`WindowHandle`] and [`DisplayHandle`].
|
|
///
|
|
/// # Safety
|
|
///
|
|
/// Some platforms have constraints on where/how this handle can be used. For example, some platforms don't support doing window
|
|
/// operations off of the main thread. The caller must ensure the [`RawHandleWrapper`] is only used in valid contexts.
|
|
pub unsafe fn get_handle(&self) -> ThreadLockedRawWindowHandleWrapper {
|
|
ThreadLockedRawWindowHandleWrapper(self.clone())
|
|
}
|
|
}
|
|
|
|
// SAFETY: [`RawHandleWrapper`] is just a normal "raw pointer", which doesn't impl Send/Sync. However the pointer is only
|
|
// exposed via an unsafe method that forces the user to make a call for a given platform. (ex: some platforms don't
|
|
// support doing window operations off of the main thread).
|
|
// A recommendation for this pattern (and more context) is available here:
|
|
// https://github.com/rust-windowing/raw-window-handle/issues/59
|
|
unsafe impl Send for RawHandleWrapper {}
|
|
// SAFETY: This is safe for the same reasons as the Send impl above.
|
|
unsafe impl Sync for RawHandleWrapper {}
|
|
|
|
/// A [`RawHandleWrapper`] that cannot be sent across threads.
|
|
///
|
|
/// This safely exposes [`RawWindowHandle`] and [`RawDisplayHandle`], but care must be taken to ensure that the construction itself is correct.
|
|
///
|
|
/// This can only be constructed via the [`RawHandleWrapper::get_handle()`] method;
|
|
/// be sure to read the safety docs there about platform-specific limitations.
|
|
/// In many cases, this should only be constructed on the main thread.
|
|
pub struct ThreadLockedRawWindowHandleWrapper(RawHandleWrapper);
|
|
|
|
impl HasWindowHandle for ThreadLockedRawWindowHandleWrapper {
|
|
fn window_handle(&self) -> Result<WindowHandle, HandleError> {
|
|
// SAFETY: the caller has validated that this is a valid context to get [`RawHandleWrapper`]
|
|
// as otherwise an instance of this type could not have been constructed
|
|
// NOTE: we cannot simply impl HasRawWindowHandle for RawHandleWrapper,
|
|
// as the `raw_window_handle` method is safe. We cannot guarantee that all calls
|
|
// of this method are correct (as it may be off the main thread on an incompatible platform),
|
|
// and so exposing a safe method to get a [`RawWindowHandle`] directly would be UB.
|
|
Ok(unsafe { WindowHandle::borrow_raw(self.0.window_handle) })
|
|
}
|
|
}
|
|
|
|
impl HasDisplayHandle for ThreadLockedRawWindowHandleWrapper {
|
|
fn display_handle(&self) -> Result<DisplayHandle, HandleError> {
|
|
// SAFETY: the caller has validated that this is a valid context to get [`RawDisplayHandle`]
|
|
// as otherwise an instance of this type could not have been constructed
|
|
// NOTE: we cannot simply impl HasRawDisplayHandle for RawHandleWrapper,
|
|
// as the `raw_display_handle` method is safe. We cannot guarantee that all calls
|
|
// of this method are correct (as it may be off the main thread on an incompatible platform),
|
|
// and so exposing a safe method to get a [`RawDisplayHandle`] directly would be UB.
|
|
Ok(unsafe { DisplayHandle::borrow_raw(self.0.display_handle) })
|
|
}
|
|
}
|
|
|
|
/// Holder of the [`RawHandleWrapper`] with wrappers, to allow use in asynchronous context
|
|
#[derive(Debug, Clone, Component)]
|
|
pub struct RawHandleWrapperHolder(pub Arc<Mutex<Option<RawHandleWrapper>>>);
|