From 3840b75beaa3925f3438a7b40f01aaac221b8206 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A9ctor=20Ram=C3=B3n?= Date: Wed, 5 May 2021 14:33:03 +0700 Subject: [PATCH] Provide `compatible_surface` in `iced_wgpu::Compositor` --- graphics/src/window/compositor.rs | 5 ++++- rustfmt.toml | 1 + wgpu/src/window/compositor.rs | 23 ++++++++++++++++++----- winit/src/application.rs | 4 ++-- 4 files changed, 25 insertions(+), 8 deletions(-) diff --git a/graphics/src/window/compositor.rs b/graphics/src/window/compositor.rs index 0bc8cbc8..7d5d789b 100644 --- a/graphics/src/window/compositor.rs +++ b/graphics/src/window/compositor.rs @@ -17,7 +17,10 @@ pub trait Compositor: Sized { type SwapChain; /// Creates a new [`Compositor`]. - fn new(settings: Self::Settings) -> Result<(Self, Self::Renderer), Error>; + fn new( + settings: Self::Settings, + compatible_window: Option<&W>, + ) -> Result<(Self, Self::Renderer), Error>; /// Crates a new [`Surface`] for the given window. /// diff --git a/rustfmt.toml b/rustfmt.toml index d979d317..fa50ab92 100644 --- a/rustfmt.toml +++ b/rustfmt.toml @@ -1 +1,2 @@ max_width=80 +edition="2018" diff --git a/wgpu/src/window/compositor.rs b/wgpu/src/window/compositor.rs index fdd648e8..aa873df8 100644 --- a/wgpu/src/window/compositor.rs +++ b/wgpu/src/window/compositor.rs @@ -21,9 +21,16 @@ impl Compositor { /// Requests a new [`Compositor`] with the given [`Settings`]. /// /// Returns `None` if no compatible graphics adapter could be found. - pub async fn request(settings: Settings) -> Option { + pub async fn request( + settings: Settings, + compatible_window: Option<&W>, + ) -> Option { let instance = wgpu::Instance::new(settings.internal_backend); + #[allow(unsafe_code)] + let compatible_surface = compatible_window + .map(|window| unsafe { instance.create_surface(window) }); + let adapter = instance .request_adapter(&wgpu::RequestAdapterOptions { power_preference: if settings.antialiasing.is_none() { @@ -31,7 +38,7 @@ impl Compositor { } else { wgpu::PowerPreference::HighPerformance }, - compatible_surface: None, + compatible_surface: compatible_surface.as_ref(), }) .await?; @@ -77,9 +84,15 @@ impl iced_graphics::window::Compositor for Compositor { type Surface = wgpu::Surface; type SwapChain = wgpu::SwapChain; - fn new(settings: Self::Settings) -> Result<(Self, Renderer), Error> { - let compositor = futures::executor::block_on(Self::request(settings)) - .ok_or(Error::AdapterNotFound)?; + fn new( + settings: Self::Settings, + compatible_window: Option<&W>, + ) -> Result<(Self, Renderer), Error> { + let compositor = futures::executor::block_on(Self::request( + settings, + compatible_window, + )) + .ok_or(Error::AdapterNotFound)?; let backend = compositor.create_backend(); diff --git a/winit/src/application.rs b/winit/src/application.rs index 106d5218..b1d5f418 100644 --- a/winit/src/application.rs +++ b/winit/src/application.rs @@ -118,8 +118,6 @@ where let mut debug = Debug::new(); debug.startup_started(); - let (compositor, renderer) = C::new(compositor_settings)?; - let event_loop = EventLoop::with_user_event(); let mut runtime = { @@ -150,6 +148,8 @@ where .build(&event_loop) .map_err(Error::WindowCreationFailed)?; + let (compositor, renderer) = C::new(compositor_settings, Some(&window))?; + let (mut sender, receiver) = mpsc::unbounded(); let mut instance = Box::pin(run_instance::(