[wgpu-hal] replace Instance.destroy_surface() with Drop impls on Surfaces

Only the metal and vulkan backends require
destruction code and it can go in a `Drop` impl since
the `Instance` is unused in those implementations.
This commit is contained in:
teoxoy 2024-07-03 15:25:39 +02:00 committed by Teodor Tanasoaia
parent b145250ebc
commit 1cb7ebab99
14 changed files with 20 additions and 57 deletions

View File

@ -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();
}
}

View File

@ -248,7 +248,6 @@ impl<A: HalApi> Hub<A> {
let suf = A::surface_as_hal(surface);
unsafe {
suf.unwrap().unconfigure(device.raw());
//TODO: we could destroy the surface here
}
}
}

View File

@ -112,24 +112,6 @@ impl Instance {
flags: instance_desc.flags,
}
}
pub(crate) fn destroy_surface(&self, surface: Surface) {
fn destroy<A: HalApi>(instance: &Option<A::Instance>, mut surface: Option<HalSurface<A>>) {
if let Some(surface) = surface.take() {
unsafe {
instance.as_ref().unwrap().destroy_surface(surface);
}
}
}
#[cfg(vulkan)]
destroy::<hal::api::Vulkan>(&self.vulkan, surface.vulkan);
#[cfg(metal)]
destroy::<hal::api::Metal>(&self.metal, surface.metal);
#[cfg(dx12)]
destroy::<hal::api::Dx12>(&self.dx12, surface.dx12);
#[cfg(gles)]
destroy::<hal::api::Gles>(&self.gl, surface.gl);
}
}
pub struct Surface {
@ -707,7 +689,7 @@ impl Global {
#[cfg(gles)]
unconfigure::<hal::api::Gles>(self, &surface.gl, &present);
}
self.instance.destroy_surface(surface);
drop(surface)
}
fn enumerate<A: HalApi>(

View File

@ -578,7 +578,7 @@ impl<A: hal::Api> Example<A> {
self.surface.unconfigure(&self.device);
self.device.exit(self.queue);
self.instance.destroy_surface(self.surface);
drop(self.surface);
drop(self.adapter);
}
}

View File

@ -1039,7 +1039,7 @@ impl<A: hal::Api> Example<A> {
self.surface.unconfigure(&self.device);
self.device.exit(self.queue);
self.instance.destroy_surface(self.surface);
drop(self.surface);
drop(self.adapter);
}
}

View File

@ -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,

View File

@ -53,7 +53,6 @@ impl crate::Instance for Context {
) -> Result<Context, crate::InstanceError> {
Ok(Context)
}
unsafe fn destroy_surface(&self, surface: Context) {}
unsafe fn enumerate_adapters(
&self,
_surface_hint: Option<&Context>,

View File

@ -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>,

View File

@ -171,8 +171,6 @@ impl crate::Instance for Instance {
self.create_surface_from_canvas(canvas)
}
unsafe fn destroy_surface(&self, _surface: Surface) {}
}
#[derive(Debug)]

View File

@ -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,

View File

@ -443,7 +443,6 @@ pub trait Instance: Sized + WasmNotSendSync {
display_handle: raw_window_handle::RawDisplayHandle,
window_handle: raw_window_handle::RawWindowHandle,
) -> Result<<Self::A as Api>::Surface, InstanceError>;
unsafe fn destroy_surface(&self, surface: <Self::A as Api>::Surface);
/// `surface_hint` is only used by the GLES backend targeting WebGL2
unsafe fn enumerate_adapters(
&self,

View File

@ -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>,

View File

@ -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;

View File

@ -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;