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

Use safe create_surface api #446

Merged
merged 1 commit into from
Feb 20, 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
24 changes: 13 additions & 11 deletions examples/with_winit/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
// Also licensed under MIT license, at your choice.

use instant::{Duration, Instant};
use std::collections::HashSet;
use std::{collections::HashSet, sync::Arc};

use anyhow::Result;
use clap::{CommandFactory, Parser};
Expand Down Expand Up @@ -57,7 +57,7 @@ struct RenderState<'s> {
// SAFETY: We MUST drop the surface before the `window`, so the fields
// must be in this order
surface: RenderSurface<'s>,
window: Window,
window: Arc<Window>,
}

fn run(
Expand Down Expand Up @@ -522,7 +522,7 @@ fn run(
.take()
.unwrap_or_else(|| create_window(event_loop));
let size = window.inner_size();
let surface_future = render_cx.create_surface(&window, size.width, size.height);
let surface_future = render_cx.create_surface(window.clone(), size.width, size.height);
// We need to block here, in case a Suspended event appeared
let surface =
pollster::block_on(surface_future).expect("Error creating surface");
Expand Down Expand Up @@ -552,14 +552,16 @@ fn run(
.expect("run to completion");
}

fn create_window(event_loop: &winit::event_loop::EventLoopWindowTarget<UserEvent>) -> Window {
fn create_window(event_loop: &winit::event_loop::EventLoopWindowTarget<UserEvent>) -> Arc<Window> {
use winit::{dpi::LogicalSize, window::WindowBuilder};
WindowBuilder::new()
.with_inner_size(LogicalSize::new(1044, 800))
.with_resizable(true)
.with_title("Vello demo")
.build(event_loop)
.unwrap()
Arc::new(
WindowBuilder::new()
.with_inner_size(LogicalSize::new(1044, 800))
.with_resizable(true)
.with_title("Vello demo")
.build(event_loop)
.unwrap(),
)
}

#[derive(Debug)]
Expand Down Expand Up @@ -631,7 +633,7 @@ pub fn main() -> Result<()> {
wasm_bindgen_futures::spawn_local(async move {
let size = window.inner_size();
let surface = render_cx
.create_surface(&window, size.width, size.height)
.create_surface(window.clone(), size.width, size.height)
.await;
if let Ok(surface) = surface {
let render_state = RenderState { window, surface };
Expand Down
18 changes: 6 additions & 12 deletions src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ use std::future::Future;

use super::Result;

use raw_window_handle::{HasDisplayHandle, HasWindowHandle};
use wgpu::{
Adapter, Device, Instance, Limits, Queue, Surface, SurfaceConfiguration, TextureFormat,
Adapter, Device, Instance, Limits, Queue, Surface, SurfaceConfiguration, SurfaceTarget,
TextureFormat,
};

/// Simple render context that maintains wgpu state for rendering the pipeline.
Expand Down Expand Up @@ -51,19 +51,13 @@ impl RenderContext {
}

/// Creates a new surface for the specified window and dimensions.
pub async fn create_surface<'w, W>(
pub async fn create_surface<'w>(
&mut self,
window: &W,
window: impl Into<SurfaceTarget<'w>>,
width: u32,
height: u32,
) -> Result<RenderSurface<'w>>
where
W: HasWindowHandle + HasDisplayHandle,
{
let surface = unsafe {
self.instance
.create_surface_unsafe(wgpu::SurfaceTargetUnsafe::from_window(window)?)
}?;
) -> Result<RenderSurface<'w>> {
let surface = self.instance.create_surface(window.into())?;
let dev_id = self
.device(Some(&surface))
.await
Expand Down