diff --git a/wgpu-core/src/global.rs b/wgpu-core/src/global.rs index 6f6756a88..7116f357b 100644 --- a/wgpu-core/src/global.rs +++ b/wgpu-core/src/global.rs @@ -1,5 +1,3 @@ -use std::sync::Arc; - use wgt::Backend; use crate::{ @@ -8,7 +6,6 @@ use crate::{ instance::{Instance, Surface}, registry::{Registry, RegistryReport}, resource_log, - storage::Element, }; #[derive(Debug, PartialEq, Eq)] @@ -152,14 +149,7 @@ impl Drop for Global { self.hubs.gl.clear(&surfaces_locked, true); } - // destroy surfaces - for element in surfaces_locked.map.drain(..) { - if let Element::Occupied(arc_surface, _) = element { - let surface = Arc::into_inner(arc_surface) - .expect("Surface cannot be destroyed because is still in use"); - self.instance.destroy_surface(surface); - } - } + surfaces_locked.map.clear(); } } diff --git a/wgpu-core/src/hub.rs b/wgpu-core/src/hub.rs index a318f91fc..559f3c47c 100644 --- a/wgpu-core/src/hub.rs +++ b/wgpu-core/src/hub.rs @@ -248,7 +248,6 @@ impl Hub { let suf = A::surface_as_hal(surface); unsafe { suf.unwrap().unconfigure(device.raw()); - //TODO: we could destroy the surface here } } } diff --git a/wgpu-core/src/instance.rs b/wgpu-core/src/instance.rs index c4433ed14..1a74de83e 100644 --- a/wgpu-core/src/instance.rs +++ b/wgpu-core/src/instance.rs @@ -112,24 +112,6 @@ impl Instance { flags: instance_desc.flags, } } - - pub(crate) fn destroy_surface(&self, surface: Surface) { - fn destroy(instance: &Option, mut surface: Option>) { - if let Some(surface) = surface.take() { - unsafe { - instance.as_ref().unwrap().destroy_surface(surface); - } - } - } - #[cfg(vulkan)] - destroy::(&self.vulkan, surface.vulkan); - #[cfg(metal)] - destroy::(&self.metal, surface.metal); - #[cfg(dx12)] - destroy::(&self.dx12, surface.dx12); - #[cfg(gles)] - destroy::(&self.gl, surface.gl); - } } pub struct Surface { @@ -707,7 +689,7 @@ impl Global { #[cfg(gles)] unconfigure::(self, &surface.gl, &present); } - self.instance.destroy_surface(surface); + drop(surface) } fn enumerate( diff --git a/wgpu-hal/examples/halmark/main.rs b/wgpu-hal/examples/halmark/main.rs index 30ff45ff5..dabcea418 100644 --- a/wgpu-hal/examples/halmark/main.rs +++ b/wgpu-hal/examples/halmark/main.rs @@ -578,7 +578,7 @@ impl Example { self.surface.unconfigure(&self.device); self.device.exit(self.queue); - self.instance.destroy_surface(self.surface); + drop(self.surface); drop(self.adapter); } } diff --git a/wgpu-hal/examples/ray-traced-triangle/main.rs b/wgpu-hal/examples/ray-traced-triangle/main.rs index 7cd6547f2..b1aceeb10 100644 --- a/wgpu-hal/examples/ray-traced-triangle/main.rs +++ b/wgpu-hal/examples/ray-traced-triangle/main.rs @@ -1039,7 +1039,7 @@ impl Example { self.surface.unconfigure(&self.device); self.device.exit(self.queue); - self.instance.destroy_surface(self.surface); + drop(self.surface); drop(self.adapter); } } diff --git a/wgpu-hal/src/dx12/instance.rs b/wgpu-hal/src/dx12/instance.rs index a62901840..c9557355f 100644 --- a/wgpu-hal/src/dx12/instance.rs +++ b/wgpu-hal/src/dx12/instance.rs @@ -143,9 +143,6 @@ impl crate::Instance for super::Instance { ))), } } - unsafe fn destroy_surface(&self, _surface: super::Surface) { - // just drop - } unsafe fn enumerate_adapters( &self, diff --git a/wgpu-hal/src/empty.rs b/wgpu-hal/src/empty.rs index 89a04ce48..956b7b08a 100644 --- a/wgpu-hal/src/empty.rs +++ b/wgpu-hal/src/empty.rs @@ -53,7 +53,6 @@ impl crate::Instance for Context { ) -> Result { Ok(Context) } - unsafe fn destroy_surface(&self, surface: Context) {} unsafe fn enumerate_adapters( &self, _surface_hint: Option<&Context>, diff --git a/wgpu-hal/src/gles/egl.rs b/wgpu-hal/src/gles/egl.rs index 8cf69cc07..e0340d829 100644 --- a/wgpu-hal/src/gles/egl.rs +++ b/wgpu-hal/src/gles/egl.rs @@ -1002,8 +1002,6 @@ impl crate::Instance for Instance { }) } - unsafe fn destroy_surface(&self, _surface: Surface) {} - unsafe fn enumerate_adapters( &self, _surface_hint: Option<&Surface>, diff --git a/wgpu-hal/src/gles/web.rs b/wgpu-hal/src/gles/web.rs index a6c79721b..99d4ff59b 100644 --- a/wgpu-hal/src/gles/web.rs +++ b/wgpu-hal/src/gles/web.rs @@ -171,8 +171,6 @@ impl crate::Instance for Instance { self.create_surface_from_canvas(canvas) } - - unsafe fn destroy_surface(&self, _surface: Surface) {} } #[derive(Debug)] diff --git a/wgpu-hal/src/gles/wgl.rs b/wgpu-hal/src/gles/wgl.rs index 64ed06325..68bedb11d 100644 --- a/wgpu-hal/src/gles/wgl.rs +++ b/wgpu-hal/src/gles/wgl.rs @@ -535,7 +535,6 @@ impl crate::Instance for Instance { srgb_capable: self.srgb_capable, }) } - unsafe fn destroy_surface(&self, _surface: Surface) {} unsafe fn enumerate_adapters( &self, diff --git a/wgpu-hal/src/lib.rs b/wgpu-hal/src/lib.rs index 812bb7299..9b6d49135 100644 --- a/wgpu-hal/src/lib.rs +++ b/wgpu-hal/src/lib.rs @@ -443,7 +443,6 @@ pub trait Instance: Sized + WasmNotSendSync { display_handle: raw_window_handle::RawDisplayHandle, window_handle: raw_window_handle::RawWindowHandle, ) -> Result<::Surface, InstanceError>; - unsafe fn destroy_surface(&self, surface: ::Surface); /// `surface_hint` is only used by the GLES backend targeting WebGL2 unsafe fn enumerate_adapters( &self, diff --git a/wgpu-hal/src/metal/mod.rs b/wgpu-hal/src/metal/mod.rs index 177b02569..000398370 100644 --- a/wgpu-hal/src/metal/mod.rs +++ b/wgpu-hal/src/metal/mod.rs @@ -117,10 +117,6 @@ impl crate::Instance for Instance { } } - unsafe fn destroy_surface(&self, surface: Surface) { - unsafe { surface.dispose() }; - } - unsafe fn enumerate_adapters( &self, _surface_hint: Option<&Surface>, diff --git a/wgpu-hal/src/metal/surface.rs b/wgpu-hal/src/metal/surface.rs index b0ea55e9f..8bbdb6378 100644 --- a/wgpu-hal/src/metal/surface.rs +++ b/wgpu-hal/src/metal/surface.rs @@ -70,12 +70,6 @@ impl super::Surface { } } - pub unsafe fn dispose(self) { - if let Some(view) = self.view { - let () = msg_send![view.as_ptr(), release]; - } - } - /// If not called on the main thread, this will panic. #[allow(clippy::transmute_ptr_to_ref)] pub unsafe fn from_view( @@ -178,6 +172,16 @@ impl super::Surface { } } +impl Drop for super::Surface { + fn drop(&mut self) { + if let Some(view) = self.view { + unsafe { + let () = msg_send![view.as_ptr(), release]; + } + } + } +} + impl crate::Surface for super::Surface { type A = super::Api; diff --git a/wgpu-hal/src/vulkan/instance.rs b/wgpu-hal/src/vulkan/instance.rs index b3ced3275..1d7386e62 100644 --- a/wgpu-hal/src/vulkan/instance.rs +++ b/wgpu-hal/src/vulkan/instance.rs @@ -880,10 +880,6 @@ impl crate::Instance for super::Instance { } } - unsafe fn destroy_surface(&self, surface: super::Surface) { - unsafe { surface.functor.destroy_surface(surface.raw, None) }; - } - unsafe fn enumerate_adapters( &self, _surface_hint: Option<&super::Surface>, @@ -942,6 +938,12 @@ impl crate::Instance for super::Instance { } } +impl Drop for super::Surface { + fn drop(&mut self) { + unsafe { self.functor.destroy_surface(self.raw, None) }; + } +} + impl crate::Surface for super::Surface { type A = super::Api;