simplify DeviceLostClosure by removing its C variant

This commit is contained in:
teoxoy
2024-11-25 13:36:48 +01:00
committed by Teodor Tanasoaia
parent 13fbf30813
commit 86656e182b
3 changed files with 6 additions and 69 deletions

View File

@@ -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<dyn FnOnce(DeviceLostReason, String) + Send + 'static>;
pub type DeviceLostClosure = Box<dyn FnOnce(DeviceLostReason, String) + Send + 'static>;
#[cfg(not(send_sync))]
pub type DeviceLostCallback = Box<dyn FnOnce(DeviceLostReason, String) + 'static>;
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<dyn FnOnce(DeviceLostReason, String) + 'static>;
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,

View File

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

View File

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