Fix block_on in iced_wgpu hanging Wasm builds
This commit is contained in:
parent
1bb5a1b9a2
commit
ecf42b97df
8 changed files with 53 additions and 30 deletions
|
|
@ -1,4 +1,4 @@
|
|||
//! A `wasm-bindgein-futures` backend.
|
||||
//! A `wasm-bindgen-futures` backend.
|
||||
|
||||
/// A `wasm-bindgen-futures` executor.
|
||||
#[derive(Debug)]
|
||||
|
|
|
|||
|
|
@ -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.
|
||||
|
|
@ -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;
|
||||
|
|
|
|||
|
|
@ -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),
|
||||
|
|
@ -25,14 +26,16 @@ 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());
|
||||
|
||||
async move {
|
||||
let mut error = Error::GraphicsAdapterNotFound;
|
||||
|
||||
for candidate in candidates {
|
||||
match candidate.build(settings, compatible_window.clone()) {
|
||||
match candidate.build(settings, compatible_window.clone()).await
|
||||
{
|
||||
Ok(compositor) => return Ok(compositor),
|
||||
Err(new_error) => {
|
||||
error = new_error;
|
||||
|
|
@ -42,6 +45,7 @@ impl crate::graphics::Compositor for Compositor {
|
|||
|
||||
Err(error)
|
||||
}
|
||||
}
|
||||
|
||||
fn create_renderer(&self) -> Self::Renderer {
|
||||
match self {
|
||||
|
|
@ -225,7 +229,7 @@ impl Candidate {
|
|||
)
|
||||
}
|
||||
|
||||
fn build<W: Window>(
|
||||
async fn build<W: Window>(
|
||||
self,
|
||||
settings: Settings,
|
||||
_compatible_window: W,
|
||||
|
|
@ -252,7 +256,8 @@ impl Candidate {
|
|||
..iced_wgpu::Settings::from_env()
|
||||
},
|
||||
_compatible_window,
|
||||
)?;
|
||||
)
|
||||
.await?;
|
||||
|
||||
Ok(Compositor::Wgpu(compositor))
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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)?)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -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 {
|
||||
|
|
@ -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 {
|
||||
|
|
|
|||
|
|
@ -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 {
|
||||
|
|
@ -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`].
|
||||
|
|
@ -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)
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -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>
|
||||
|
|
@ -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 {
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
|
|
@ -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(
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue