Skip to content

Commit

Permalink
Use safe create_surface api
Browse files Browse the repository at this point in the history
  • Loading branch information
ratmice committed Feb 19, 2024
1 parent a8b33b3 commit 2bada83
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 25 deletions.
28 changes: 15 additions & 13 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 @@ -53,11 +53,11 @@ struct Args {
use_cpu: bool,
}

struct RenderState<'s> {
struct RenderState {
// SAFETY: We MUST drop the surface before the `window`, so the fields
// must be in this order
surface: RenderSurface<'s>,
window: Window,
surface: RenderSurface<'static>,
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

0 comments on commit 2bada83

Please sign in to comment.