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

Fix block_on in iced_wgpu hanging Wasm builds #2313

Merged
merged 1 commit into from
Mar 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion futures/src/backend/wasm/wasm_bindgen.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//! A `wasm-bindgein-futures` backend.
//! A `wasm-bindgen-futures` backend.

/// A `wasm-bindgen-futures` executor.
#[derive(Debug)]
Expand Down
3 changes: 2 additions & 1 deletion graphics/src/compositor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use crate::core::Color;
use crate::futures::{MaybeSend, MaybeSync};

use raw_window_handle::{HasDisplayHandle, HasWindowHandle};
use std::future::Future;
use thiserror::Error;

/// A graphics compositor that can draw to windows.
Expand All @@ -23,7 +24,7 @@ pub trait Compositor: Sized {
fn new<W: Window + Clone>(
settings: Self::Settings,
compatible_window: W,
) -> Result<Self, Error>;
) -> impl Future<Output = Result<Self, Error>>;

/// Creates a [`Self::Renderer`] for the [`Compositor`].
fn create_renderer(&self) -> Self::Renderer;
Expand Down
27 changes: 16 additions & 11 deletions renderer/src/compositor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use crate::graphics::{Error, Viewport};
use crate::{Renderer, Settings};

use std::env;
use std::future::Future;

pub enum Compositor {
TinySkia(iced_tiny_skia::window::Compositor),
Expand All @@ -25,22 +26,25 @@ impl crate::graphics::Compositor for Compositor {
fn new<W: Window + Clone>(
settings: Self::Settings,
compatible_window: W,
) -> Result<Self, Error> {
) -> impl Future<Output = Result<Self, Error>> {
let candidates =
Candidate::list_from_env().unwrap_or(Candidate::default_list());

let mut error = Error::GraphicsAdapterNotFound;
async move {
let mut error = Error::GraphicsAdapterNotFound;

for candidate in candidates {
match candidate.build(settings, compatible_window.clone()) {
Ok(compositor) => return Ok(compositor),
Err(new_error) => {
error = new_error;
for candidate in candidates {
match candidate.build(settings, compatible_window.clone()).await
{
Ok(compositor) => return Ok(compositor),
Err(new_error) => {
error = new_error;
}
}
}
}

Err(error)
Err(error)
}
}

fn create_renderer(&self) -> Self::Renderer {
Expand Down Expand Up @@ -225,7 +229,7 @@ impl Candidate {
)
}

fn build<W: Window>(
async fn build<W: Window>(
self,
settings: Settings,
_compatible_window: W,
Expand All @@ -252,7 +256,8 @@ impl Candidate {
..iced_wgpu::Settings::from_env()
},
_compatible_window,
)?;
)
.await?;

Ok(Compositor::Wgpu(compositor))
}
Expand Down
19 changes: 17 additions & 2 deletions src/application.rs
Original file line number Diff line number Diff line change
Expand Up @@ -205,11 +205,26 @@ pub trait Application: Sized {
..crate::renderer::Settings::default()
};

Ok(crate::shell::application::run::<
let run = crate::shell::application::run::<
Instance<Self>,
Self::Executor,
crate::renderer::Compositor,
>(settings.into(), renderer_settings)?)
>(settings.into(), renderer_settings);

#[cfg(target_arch = "wasm32")]
{
use crate::futures::FutureExt;
use iced_futures::backend::wasm::wasm_bindgen::Executor;

Executor::new()
.expect("Create Wasm executor")
.spawn(run.map(|_| ()));

Ok(())
}

#[cfg(not(target_arch = "wasm32"))]
Ok(crate::futures::executor::block_on(run)?)
}
}

Expand Down
5 changes: 3 additions & 2 deletions tiny_skia/src/window/compositor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use crate::graphics::{Error, Viewport};
use crate::{Backend, Primitive, Renderer, Settings};

use std::collections::VecDeque;
use std::future::{self, Future};
use std::num::NonZeroU32;

pub struct Compositor {
Expand All @@ -31,8 +32,8 @@ impl crate::graphics::Compositor for Compositor {
fn new<W: compositor::Window>(
settings: Self::Settings,
compatible_window: W,
) -> Result<Self, Error> {
Ok(new(settings, compatible_window))
) -> impl Future<Output = Result<Self, Error>> {
future::ready(Ok(new(settings, compatible_window)))
}

fn create_renderer(&self) -> Self::Renderer {
Expand Down
16 changes: 7 additions & 9 deletions wgpu/src/window/compositor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ use crate::graphics::compositor;
use crate::graphics::{Error, Viewport};
use crate::{Backend, Primitive, Renderer, Settings};

use std::future::Future;

/// A window graphics backend for iced powered by `wgpu`.
#[allow(missing_debug_implementations)]
pub struct Compositor {
Expand Down Expand Up @@ -158,17 +160,13 @@ impl Compositor {

/// Creates a [`Compositor`] and its [`Backend`] for the given [`Settings`] and
/// window.
pub fn new<W: compositor::Window>(
pub async fn new<W: compositor::Window>(
settings: Settings,
compatible_window: W,
) -> Result<Compositor, Error> {
let compositor = futures::executor::block_on(Compositor::request(
settings,
Some(compatible_window),
))
.ok_or(Error::GraphicsAdapterNotFound)?;

Ok(compositor)
Compositor::request(settings, Some(compatible_window))
.await
.ok_or(Error::GraphicsAdapterNotFound)
}

/// Presents the given primitives with the given [`Compositor`] and [`Backend`].
Expand Down Expand Up @@ -234,7 +232,7 @@ impl graphics::Compositor for Compositor {
fn new<W: compositor::Window>(
settings: Self::Settings,
compatible_window: W,
) -> Result<Self, Error> {
) -> impl Future<Output = Result<Self, Error>> {
new(settings, compatible_window)
}

Expand Down
4 changes: 2 additions & 2 deletions winit/src/application.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ where

/// Runs an [`Application`] with an executor, compositor, and the provided
/// settings.
pub fn run<A, E, C>(
pub async fn run<A, E, C>(
settings: Settings<A::Flags>,
compositor_settings: C::Settings,
) -> Result<(), Error>
Expand Down Expand Up @@ -188,7 +188,7 @@ where
};
}

let compositor = C::new(compositor_settings, window.clone())?;
let compositor = C::new(compositor_settings, window.clone()).await?;
let mut renderer = compositor.create_renderer();

for font in settings.fonts {
Expand Down
7 changes: 5 additions & 2 deletions winit/src/multi_window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ use crate::core::widget::operation;
use crate::core::window;
use crate::core::{Point, Size};
use crate::futures::futures::channel::mpsc;
use crate::futures::futures::{task, Future, StreamExt};
use crate::futures::futures::executor;
use crate::futures::futures::task;
use crate::futures::futures::{Future, StreamExt};
use crate::futures::{Executor, Runtime, Subscription};
use crate::graphics::{compositor, Compositor};
use crate::multi_window::window_manager::WindowManager;
Expand Down Expand Up @@ -183,7 +185,8 @@ where
};
}

let mut compositor = C::new(compositor_settings, main_window.clone())?;
let mut compositor =
executor::block_on(C::new(compositor_settings, main_window.clone()))?;

let mut window_manager = WindowManager::new();
let _ = window_manager.insert(
Expand Down
Loading