Add Surface::as_hal_mut (#3123)

This commit is contained in:
Xiaopeng Li
2022-10-20 04:06:23 +08:00
committed by GitHub
parent d3ab5a197e
commit 754a4bad80
3 changed files with 48 additions and 0 deletions

View File

@@ -428,6 +428,25 @@ impl<G: GlobalIdentityHandlerFactory> Global<G> {
hal_device_callback(hal_device)
}
/// # Safety
/// - The raw surface handle must not be manually destroyed
pub unsafe fn surface_as_hal_mut<A: HalApi, F: FnOnce(Option<&mut A::Surface>) -> R, R>(
&self,
id: SurfaceId,
hal_surface_callback: F,
) -> R {
profiling::scope!("Surface::as_hal_mut");
let mut token = Token::root();
let (mut guard, _) = self.surfaces.write(&mut token);
let surface = guard.get_mut(id).ok();
let hal_surface = surface
.and_then(|surface| A::get_surface_mut(surface))
.map(|surface| &mut surface.raw);
hal_surface_callback(hal_surface)
}
}
#[derive(Clone, Copy, Debug)]

View File

@@ -154,6 +154,20 @@ impl Context {
.device_as_hal::<A, F, R>(device.id, hal_device_callback)
}
#[cfg(any(not(target_arch = "wasm32"), feature = "emscripten"))]
pub unsafe fn surface_as_hal_mut<
A: wgc::hub::HalApi,
F: FnOnce(Option<&mut A::Surface>) -> R,
R,
>(
&self,
surface: &Surface,
hal_surface_callback: F,
) -> R {
self.0
.surface_as_hal_mut::<A, F, R>(surface.id, hal_surface_callback)
}
#[cfg(any(not(target_arch = "wasm32"), feature = "emscripten"))]
pub unsafe fn texture_as_hal<A: wgc::hub::HalApi, F: FnOnce(Option<&A::Texture>)>(
&self,

View File

@@ -3773,6 +3773,21 @@ impl Surface {
})
.ok_or(SurfaceError::Lost)
}
/// Returns the inner hal Surface using a callback. The hal surface will be `None` if the
/// backend type argument does not match with this wgpu Surface
///
/// # Safety
///
/// - The raw handle obtained from the hal Surface must not be manually destroyed
#[cfg(any(not(target_arch = "wasm32"), feature = "emscripten"))]
pub unsafe fn as_hal_mut<A: wgc::hub::HalApi, F: FnOnce(Option<&mut A::Surface>) -> R, R>(
&mut self,
hal_surface_callback: F,
) -> R {
self.context
.surface_as_hal_mut::<A, F, R>(&self.id, hal_surface_callback)
}
}
/// Type for the callback of uncaptured error handler