[wgpu-core] use .strict_get() & .strict_unregister() for surfaces

This works because we never assign errors to surfaces (they are never invalid).
This commit is contained in:
teoxoy 2024-09-05 14:41:48 +02:00 committed by Teodor Tanasoaia
parent 98426329a4
commit da9afea271
5 changed files with 20 additions and 70 deletions

View File

@ -39,15 +39,10 @@ impl Global {
&self,
adapter_id: AdapterId,
surface_id: SurfaceId,
) -> Result<bool, instance::IsSurfaceSupportedError> {
let hub = &self.hub;
let surface_guard = self.surfaces.read();
let adapter = hub.adapters.strict_get(adapter_id);
let surface = surface_guard
.get(surface_id)
.map_err(|_| instance::IsSurfaceSupportedError::InvalidSurface)?;
Ok(adapter.is_surface_supported(surface))
) -> bool {
let surface = self.surfaces.strict_get(surface_id);
let adapter = self.hub.adapters.strict_get(adapter_id);
adapter.is_surface_supported(&surface)
}
pub fn surface_get_capabilities(
@ -72,25 +67,15 @@ impl Global {
})
}
fn fetch_adapter_and_surface<
F: FnOnce(&Adapter, &Surface) -> Result<B, instance::GetSurfaceSupportError>,
B,
>(
fn fetch_adapter_and_surface<F: FnOnce(&Adapter, &Surface) -> B, B>(
&self,
surface_id: SurfaceId,
adapter_id: AdapterId,
get_supported_callback: F,
) -> Result<B, instance::GetSurfaceSupportError> {
let hub = &self.hub;
let surface_guard = self.surfaces.read();
let adapter = hub.adapters.strict_get(adapter_id);
let surface = surface_guard
.get(surface_id)
.map_err(|_| instance::GetSurfaceSupportError::InvalidSurface)?;
get_supported_callback(&adapter, surface)
) -> B {
let surface = self.surfaces.strict_get(surface_id);
let adapter = self.hub.adapters.strict_get(adapter_id);
get_supported_callback(&adapter, &surface)
}
pub fn device_features(&self, device_id: DeviceId) -> Result<wgt::Features, DeviceError> {
@ -1901,7 +1886,6 @@ impl Global {
let user_callbacks;
{
let hub = &self.hub;
let surface_guard = self.surfaces.read();
let device = match hub.devices.get(device_id) {
Ok(device) => device,
@ -1917,10 +1901,7 @@ impl Global {
break 'error e.into();
}
let surface = match surface_guard.get(surface_id) {
Ok(surface) => surface,
Err(_) => break 'error E::InvalidSurface,
};
let surface = self.surfaces.strict_get(surface_id);
let caps = match surface.get_capabilities(&device.adapter) {
Ok(caps) => caps,

View File

@ -346,18 +346,9 @@ impl Adapter {
crate::impl_resource_type!(Adapter);
crate::impl_storage_item!(Adapter);
#[derive(Clone, Debug, Error)]
#[non_exhaustive]
pub enum IsSurfaceSupportedError {
#[error("Invalid surface")]
InvalidSurface,
}
#[derive(Clone, Debug, Error)]
#[non_exhaustive]
pub enum GetSurfaceSupportError {
#[error("Invalid surface")]
InvalidSurface,
#[error("Surface is not supported by the adapter")]
Unsupported,
}
@ -403,8 +394,6 @@ impl<M: Marker> AdapterInputs<'_, M> {
pub enum RequestAdapterError {
#[error("No suitable adapter found")]
NotFound,
#[error("Surface {0:?} is invalid")]
InvalidSurface(SurfaceId),
}
#[derive(Clone, Debug, Error)]
@ -604,9 +593,9 @@ impl Global {
api_log!("Surface::drop {id:?}");
let surface = self.surfaces.unregister(id);
let surface = Arc::into_inner(surface.unwrap())
.expect("Surface cannot be destroyed because is still in use");
let surface = self.surfaces.strict_unregister(id);
let surface =
Arc::into_inner(surface).expect("Surface cannot be destroyed because is still in use");
if let Some(present) = surface.presentation.lock().take() {
for (&backend, surface) in &surface.surface_per_backend {
@ -723,12 +712,7 @@ impl Global {
let compatible_surface = desc
.compatible_surface
.map(|id| {
self.surfaces
.get(id)
.map_err(|_| RequestAdapterError::InvalidSurface(id))
})
.transpose()?;
.map(|id| self.surfaces.strict_get(id));
let compatible_surface = compatible_surface.as_ref().map(|surface| surface.as_ref());
let mut device_types = Vec::new();

View File

@ -122,10 +122,7 @@ impl Global {
let hub = &self.hub;
let surface = self
.surfaces
.get(surface_id)
.map_err(|_| SurfaceError::Invalid)?;
let surface = self.surfaces.strict_get(surface_id);
let (device, config) = if let Some(ref present) = *surface.presentation.lock() {
present.device.check_is_valid()?;
@ -257,10 +254,7 @@ impl Global {
let hub = &self.hub;
let surface = self
.surfaces
.get(surface_id)
.map_err(|_| SurfaceError::Invalid)?;
let surface = self.surfaces.strict_get(surface_id);
let mut presentation = surface.presentation.lock();
let present = match presentation.as_mut() {
@ -332,10 +326,7 @@ impl Global {
let hub = &self.hub;
let surface = self
.surfaces
.get(surface_id)
.map_err(|_| SurfaceError::Invalid)?;
let surface = self.surfaces.strict_get(surface_id);
let mut presentation = surface.presentation.lock();
let present = match presentation.as_mut() {
Some(present) => present,

View File

@ -1327,10 +1327,9 @@ impl Global {
) -> R {
profiling::scope!("Surface::as_hal");
let surface = self.surfaces.get(id).ok();
let surface = self.surfaces.strict_get(id);
let hal_surface = surface
.as_ref()
.and_then(|surface| surface.raw(A::VARIANT))
.raw(A::VARIANT)
.and_then(|surface| surface.as_any().downcast_ref());
hal_surface_callback(hal_surface)

View File

@ -641,13 +641,8 @@ impl crate::Context for ContextWgpuCore {
adapter_data: &Self::AdapterData,
surface_data: &Self::SurfaceData,
) -> bool {
match self
.0
self.0
.adapter_is_surface_supported(*adapter_data, surface_data.id)
{
Ok(result) => result,
Err(err) => self.handle_error_fatal(err, "Adapter::is_surface_supported"),
}
}
fn adapter_features(&self, adapter_data: &Self::AdapterData) -> Features {