diff --git a/wgpu-core/src/device/mod.rs b/wgpu-core/src/device/mod.rs index 5078d0e4ff..8b4da3b2ec 100644 --- a/wgpu-core/src/device/mod.rs +++ b/wgpu-core/src/device/mod.rs @@ -12,7 +12,6 @@ use crate::{ use arrayvec::ArrayVec; use smallvec::SmallVec; -use std::os::raw::c_char; use thiserror::Error; use wgt::{BufferAddress, DeviceLostReason, TextureFormat}; @@ -183,36 +182,15 @@ impl UserClosures { closure(); } for invocation in self.device_lost_invocations { - invocation - .closure - .call(invocation.reason, invocation.message); + (invocation.closure)(invocation.reason, invocation.message); } } } #[cfg(send_sync)] -pub type DeviceLostCallback = Box; +pub type DeviceLostClosure = Box; #[cfg(not(send_sync))] -pub type DeviceLostCallback = Box; - -pub struct DeviceLostClosureRust { - pub callback: DeviceLostCallback, -} - -#[repr(C)] -pub struct DeviceLostClosureC { - pub callback: unsafe extern "C" fn(user_data: *mut u8, reason: u8, message: *const c_char), - pub user_data: *mut u8, -} - -#[cfg(send_sync)] -unsafe impl Send for DeviceLostClosureC {} - -pub struct DeviceLostClosure { - // We wrap this so creating the enum in the C variant can be unsafe, - // allowing our call function to be safe. - inner: DeviceLostClosureInner, -} +pub type DeviceLostClosure = Box; pub struct DeviceLostInvocation { closure: DeviceLostClosure, @@ -220,46 +198,6 @@ pub struct DeviceLostInvocation { message: String, } -enum DeviceLostClosureInner { - Rust { inner: DeviceLostClosureRust }, - C { inner: DeviceLostClosureC }, -} - -impl DeviceLostClosure { - pub fn from_rust(callback: DeviceLostCallback) -> Self { - let inner = DeviceLostClosureRust { callback }; - Self { - inner: DeviceLostClosureInner::Rust { inner }, - } - } - - /// # Safety - /// - /// - The callback pointer must be valid to call with the provided `user_data` - /// pointer. - /// - /// - Both pointers must point to `'static` data, as the callback may happen at - /// an unspecified time. - pub unsafe fn from_c(inner: DeviceLostClosureC) -> Self { - Self { - inner: DeviceLostClosureInner::C { inner }, - } - } - - pub(crate) fn call(self, reason: DeviceLostReason, message: String) { - match self.inner { - DeviceLostClosureInner::Rust { inner } => (inner.callback)(reason, message), - // SAFETY: the contract of the call to from_c says that this unsafe is sound. - DeviceLostClosureInner::C { inner } => unsafe { - // Ensure message is structured as a null-terminated C string. It only - // needs to live as long as the callback invocation. - let message = std::ffi::CString::new(message).unwrap(); - (inner.callback)(inner.user_data, reason as u8, message.as_ptr()) - }, - } - } -} - pub(crate) fn map_buffer( buffer: &Buffer, offset: BufferAddress, diff --git a/wgpu-core/src/device/resource.rs b/wgpu-core/src/device/resource.rs index c9db29c43f..0dec93127e 100644 --- a/wgpu-core/src/device/resource.rs +++ b/wgpu-core/src/device/resource.rs @@ -3600,7 +3600,7 @@ impl Device { // 1) Resolve the GPUDevice device.lost promise. if let Some(device_lost_closure) = self.device_lost_closure.lock().take() { - device_lost_closure.call(DeviceLostReason::Unknown, message.to_string()); + device_lost_closure(DeviceLostReason::Unknown, message.to_string()); } // 2) Complete any outstanding mapAsync() steps. diff --git a/wgpu/src/backend/wgpu_core.rs b/wgpu/src/backend/wgpu_core.rs index 714d764cdb..824b84fc01 100644 --- a/wgpu/src/backend/wgpu_core.rs +++ b/wgpu/src/backend/wgpu_core.rs @@ -24,7 +24,7 @@ use std::{ sync::Arc, }; use wgc::error::ContextErrorSource; -use wgc::{command::bundle_ffi::*, device::DeviceLostClosure, pipeline::CreateShaderModuleError}; +use wgc::{command::bundle_ffi::*, pipeline::CreateShaderModuleError}; use wgt::WasmNotSendSync; pub struct ContextWgpuCore(wgc::global::Global); @@ -1352,9 +1352,8 @@ impl crate::Context for ContextWgpuCore { device_data: &Self::DeviceData, device_lost_callback: crate::context::DeviceLostCallback, ) { - let device_lost_closure = DeviceLostClosure::from_rust(device_lost_callback); self.0 - .device_set_device_lost_closure(device_data.id, device_lost_closure); + .device_set_device_lost_closure(device_data.id, device_lost_callback); } fn device_destroy(&self, device_data: &Self::DeviceData) { self.0.device_destroy(device_data.id);