Provide `compatible_surface` in `iced_wgpu::Compositor`

This commit is contained in:
Héctor Ramón 2021-05-05 14:33:03 +07:00
parent 6b4bf34bf9
commit 3840b75bea
4 changed files with 25 additions and 8 deletions

View File

@ -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<W: HasRawWindowHandle>(
settings: Self::Settings,
compatible_window: Option<&W>,
) -> Result<(Self, Self::Renderer), Error>;
/// Crates a new [`Surface`] for the given window.
///

View File

@ -1 +1,2 @@
max_width=80
edition="2018"

View File

@ -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<Self> {
pub async fn request<W: HasRawWindowHandle>(
settings: Settings,
compatible_window: Option<&W>,
) -> Option<Self> {
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,8 +84,14 @@ 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))
fn new<W: HasRawWindowHandle>(
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();

View File

@ -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::<A, E, C>(