Skip to content
Open
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
1,553 changes: 1,500 additions & 53 deletions Cargo.lock

Large diffs are not rendered by default.

4 changes: 3 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ members = [
"examples/player",
"examples/bunnymark",
"examples/serialize",
"examples/wasm",
]
resolver = "2"

Expand Down Expand Up @@ -78,7 +79,8 @@ pollster = "0.4"
# Dev-dependencies
winit = { version = "0.30.2", features = ["rwh_06"] }

# [patch.crates-io]
[patch.crates-io]
anyrender = { path = "./crates/anyrender" }
# vello = { path = "../vello/vello" }
# vello_cpu = { path = "../vello/sparse_strips/vello_cpu" }
# vello_hybrid = { path = "../vello/sparse_strips/vello_hybrid" }
Expand Down
7 changes: 6 additions & 1 deletion crates/anyrender/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,12 @@ pub trait WindowRenderer {
type ScenePainter<'a>: PaintScene
where
Self: 'a;
fn resume(&mut self, window: Arc<dyn WindowHandle>, width: u32, height: u32);
fn resume(
&mut self,
window: Arc<dyn WindowHandle>,
width: u32,
height: u32,
) -> impl std::future::Future<Output = ()> + '_;
fn suspend(&mut self);
fn is_active(&self) -> bool;
fn set_size(&mut self, width: u32, height: u32);
Expand Down
4 changes: 2 additions & 2 deletions crates/anyrender/src/null_backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
use crate::{ImageRenderer, PaintScene, WindowHandle, WindowRenderer};
use std::sync::Arc;

#[derive(Copy, Clone, Default)]
#[derive(Clone, Default)]
pub struct NullWindowRenderer {
is_active: bool,
}
Expand All @@ -20,7 +20,7 @@ impl WindowRenderer for NullWindowRenderer {
where
Self: 'a;

fn resume(&mut self, _window: Arc<dyn WindowHandle>, _width: u32, _height: u32) {
async fn resume(&mut self, _window: Arc<dyn WindowHandle>, _width: u32, _height: u32) {
self.is_active = true;
}

Expand Down
2 changes: 1 addition & 1 deletion crates/anyrender_skia/src/window_renderer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ impl WindowRenderer for SkiaWindowRenderer {
where
Self: 'a;

fn resume(&mut self, window: Arc<dyn anyrender::WindowHandle>, width: u32, height: u32) {
async fn resume(&mut self, window: Arc<dyn anyrender::WindowHandle>, width: u32, height: u32) {
graphics::set_font_cache_count_limit(100);
graphics::set_typeface_cache_count_limit(100);
graphics::set_resource_cache_total_bytes_limit(10485760);
Expand Down
39 changes: 21 additions & 18 deletions crates/anyrender_vello/src/window_renderer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,25 +127,28 @@ impl WindowRenderer for VelloWindowRenderer {
matches!(self.render_state, RenderState::Active(_))
}

fn resume(&mut self, window_handle: Arc<dyn WindowHandle>, width: u32, height: u32) {
async fn resume(&mut self, window_handle: Arc<dyn WindowHandle>, width: u32, height: u32) {
// Create wgpu_context::SurfaceRenderer
let render_surface = pollster::block_on(self.wgpu_context.create_surface(
window_handle.clone(),
SurfaceRendererConfiguration {
usage: wgpu::TextureUsages::RENDER_ATTACHMENT,
formats: vec![TextureFormat::Rgba8Unorm, TextureFormat::Bgra8Unorm],
width,
height,
present_mode: PresentMode::AutoVsync,
desired_maximum_frame_latency: 2,
alpha_mode: wgpu::CompositeAlphaMode::Auto,
view_formats: vec![],
},
Some(TextureConfiguration {
usage: TextureUsages::STORAGE_BINDING | TextureUsages::TEXTURE_BINDING,
}),
))
.expect("Error creating surface");
let render_surface = self
.wgpu_context
.create_surface(
window_handle.clone(),
SurfaceRendererConfiguration {
usage: wgpu::TextureUsages::RENDER_ATTACHMENT,
formats: vec![TextureFormat::Rgba8Unorm, TextureFormat::Bgra8Unorm],
width,
height,
present_mode: PresentMode::AutoVsync,
desired_maximum_frame_latency: 2,
alpha_mode: wgpu::CompositeAlphaMode::Auto,
view_formats: vec![],
},
Some(TextureConfiguration {
usage: TextureUsages::STORAGE_BINDING | TextureUsages::TEXTURE_BINDING,
}),
)
.await
.expect("Error creating surface");

// Create vello::Renderer
let renderer = VelloRenderer::new(
Expand Down
1 change: 0 additions & 1 deletion crates/anyrender_vello_hybrid/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,5 @@ peniko = { workspace = true }
vello_hybrid = { workspace = true }
vello_common = { workspace = true }
wgpu = { workspace = true }
pollster = { workspace = true }
rustc-hash = { workspace = true }
wgpu_context = { workspace = true }
41 changes: 22 additions & 19 deletions crates/anyrender_vello_hybrid/src/window_renderer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,26 +122,29 @@ impl WindowRenderer for VelloHybridWindowRenderer {
matches!(self.render_state, RenderState::Active(_))
}

fn resume(&mut self, window_handle: Arc<dyn WindowHandle>, width: u32, height: u32) {
async fn resume(&mut self, window_handle: Arc<dyn WindowHandle>, width: u32, height: u32) {
// Create wgpu_context::SurfaceRenderer
let render_surface = pollster::block_on(self.wgpu_context.create_surface(
window_handle.clone(),
SurfaceRendererConfiguration {
usage: wgpu::TextureUsages::RENDER_ATTACHMENT,
formats: vec![DEFAULT_TEXTURE_FORMAT],
width,
height,
present_mode: PresentMode::AutoVsync,
desired_maximum_frame_latency: 2,
alpha_mode: wgpu::CompositeAlphaMode::Auto,
view_formats: vec![],
},
None,
// Some(TextureConfiguration {
// usage: TextureUsages::STORAGE_BINDING | TextureUsages::TEXTURE_BINDING,
// }),
))
.expect("Error creating surface");
let render_surface = self
.wgpu_context
.create_surface(
window_handle.clone(),
SurfaceRendererConfiguration {
usage: wgpu::TextureUsages::RENDER_ATTACHMENT,
formats: vec![DEFAULT_TEXTURE_FORMAT],
width,
height,
present_mode: PresentMode::AutoVsync,
desired_maximum_frame_latency: 2,
alpha_mode: wgpu::CompositeAlphaMode::Auto,
view_formats: vec![],
},
None,
// Some(TextureConfiguration {
// usage: TextureUsages::STORAGE_BINDING | TextureUsages::TEXTURE_BINDING,
// }),
)
.await
.expect("Error creating surface");

// Create vello::Renderer
let renderer = VelloHybridRenderer::new(
Expand Down
2 changes: 1 addition & 1 deletion crates/pixels_window_renderer/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ impl<Renderer: ImageRenderer> WindowRenderer for PixelsWindowRenderer<Renderer>
matches!(self.render_state, RenderState::Active(_))
}

fn resume(&mut self, window_handle: Arc<dyn WindowHandle>, width: u32, height: u32) {
async fn resume(&mut self, window_handle: Arc<dyn WindowHandle>, width: u32, height: u32) {
let surface = SurfaceTexture::new(width, height, window_handle.clone());
let mut pixels = Pixels::new(width, height, surface).unwrap();
pixels.enable_vsync(true);
Expand Down
2 changes: 1 addition & 1 deletion crates/softbuffer_window_renderer/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ impl<Renderer: ImageRenderer> WindowRenderer for SoftbufferWindowRenderer<Render
matches!(self.render_state, RenderState::Active(_))
}

fn resume(&mut self, window_handle: Arc<dyn WindowHandle>, width: u32, height: u32) {
async fn resume(&mut self, window_handle: Arc<dyn WindowHandle>, width: u32, height: u32) {
let context = Context::new(window_handle.clone()).unwrap();
let surface = Surface::new(&context, window_handle.clone()).unwrap();
self.render_state = RenderState::Active(ActiveRenderState {
Expand Down
1 change: 1 addition & 0 deletions examples/bunnymark/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ license.workspace = true
publish = false

[dependencies]
pollster = { workspace = true }
kurbo = { workspace = true }
winit = { workspace = true }
peniko = { workspace = true }
Expand Down
4 changes: 3 additions & 1 deletion examples/bunnymark/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,9 @@ impl App {
self.scale_factor = window.scale_factor();

let physical_size = window.inner_size();
renderer.resume(window.clone(), physical_size.width, physical_size.height);
pollster::block_on(
renderer.resume(window.clone(), physical_size.width, physical_size.height),
);
self.render_state = RenderState::Active {
window,
renderer: f(renderer),
Expand Down
1 change: 1 addition & 0 deletions examples/player/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ license.workspace = true
publish = false

[dependencies]
pollster = { workspace = true }
kurbo = { workspace = true }
winit = { workspace = true }
peniko = { workspace = true }
Expand Down
2 changes: 1 addition & 1 deletion examples/player/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ impl App {
Arc::new(event_loop.create_window(attr).unwrap())
});

renderer.resume(window.clone(), self.width, self.height);
pollster::block_on(renderer.resume(window.clone(), self.width, self.height));
let renderer = renderer.into();
self.render_state = RenderState::Active { window, renderer };
self.request_redraw();
Expand Down
31 changes: 31 additions & 0 deletions examples/wasm/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
[package]
name = "anyrender_wasm_example"
version = "0.1.0"
publish = false
edition = "2024"

[lib]
crate-type = ["cdylib", "rlib"]

[dependencies]
anyrender = { workspace = true }
anyrender_vello = { workspace = true }
blitz-dom = { git = "https://github.com/DioxusLabs/blitz", package = "blitz-dom", default-features = false, features = ["woff-rust"] }
blitz-html = { git = "https://github.com/DioxusLabs/blitz", package = "blitz-html", default-features = false }
blitz-paint = { git = "https://github.com/DioxusLabs/blitz", package = "blitz-paint", default-features = false, features = ["svg"] }
blitz-traits = { git = "https://github.com/DioxusLabs/blitz", package = "blitz-traits", default-features = false }
console_error_panic_hook = "0.1.7"
js-sys = "0.3"
linebender_resource_handle = { workspace = true }
raw-window-handle = { workspace = true, features = ["wasm-bindgen-0-2"] }
wasm-bindgen = "0.2"
wasm-bindgen-futures = "0.4"

[dependencies.web-sys]
version = "0.3"
features = ["HtmlCanvasElement", "Response", "Window"]

[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
axum = "0.7"
tokio = { version = "1.37", features = ["macros", "net", "rt-multi-thread"] }
tower-http = { version = "0.6", features = ["fs"] }
24 changes: 24 additions & 0 deletions examples/wasm/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
## Build with `wasm-pack`

From the repo root:

```bash
# one-time
rustup toolchain install 1.92.0
rustup target add wasm32-unknown-unknown --toolchain 1.92.0
cargo +1.92.0 install wasm-pack

# build into examples/wasm/src/pkg so index.html can import it
export RUSTUP_TOOLCHAIN=1.92
wasm-pack build examples/wasm --target web --out-dir src/pkg
```

## Run the demo

Serve the `examples/wasm/src` folder (needs a web server; `file://` won't work).

```bash
cargo run -p anyrender_wasm_example --bin serve -- 8080
```

Then open `http://localhost:8080` and click **Paint** (or just edit the HTML).
36 changes: 36 additions & 0 deletions examples/wasm/src/bin/serve.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
//! Serves `src/` over HTTP so `index.html` and the WASM ES module load correctly (not `file://`).
use std::net::SocketAddr;
use std::path::PathBuf;

use axum::Router;
use tokio::net::TcpListener;
use tower_http::services::ServeDir;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let web_root = PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("src");
if !web_root.join("index.html").is_file() {
eprintln!(
"expected {} (run wasm-pack first for src/pkg).",
web_root.join("index.html").display()
);
}

let port: u16 = std::env::args()
.nth(1)
.and_then(|s| s.parse().ok())
.unwrap_or(8080);

let addr = SocketAddr::from(([127, 0, 0, 1], port));
let listener = TcpListener::bind(addr).await?;

let service = ServeDir::new(&web_root).append_index_html_on_directories(true);
let app = Router::new().fallback_service(service);

let url = format!("http://{}", addr);
eprintln!("Serving {} -> {}", web_root.display(), url);
eprintln!("Open {url}/ in your browser.");

axum::serve(listener, app).await?;
Ok(())
}
Loading
Loading