Skip to content

Commit

Permalink
Add Surface::as_hal_mut (#3123)
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaopengli89 authored Oct 19, 2022
1 parent d3ab5a1 commit 754a4ba
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 0 deletions.
19 changes: 19 additions & 0 deletions wgpu-core/src/resource.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down
14 changes: 14 additions & 0 deletions wgpu/src/backend/direct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
15 changes: 15 additions & 0 deletions wgpu/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 754a4ba

Please sign in to comment.