[rs] Drop for surfaces and adapters

This commit is contained in:
Dzmitry Malyshau 2020-11-13 19:15:17 -05:00
parent dff98ac2a8
commit edbe112d23
4 changed files with 50 additions and 4 deletions

View File

@ -26,14 +26,14 @@ vulkan-portability = ["wgc/gfx-backend-vulkan", "gfx-backend-vulkan"]
package = "wgpu-core"
#version = "0.6"
git = "https://github.com/gfx-rs/wgpu"
rev = "789b09c7bde774f09b0cec87d6ea1b5700a6de2f"
rev = "af9713b249cc158276aeb643900f095e7699167a"
features = ["raw-window-handle"]
[dependencies.wgt]
package = "wgpu-types"
#version = "0.6"
git = "https://github.com/gfx-rs/wgpu"
rev = "789b09c7bde774f09b0cec87d6ea1b5700a6de2f"
rev = "af9713b249cc158276aeb643900f095e7699167a"
[dependencies]
arrayvec = "0.5"

View File

@ -33,7 +33,7 @@ impl Context {
#[cfg(any(target_os = "ios", target_os = "macos"))]
pub unsafe fn create_surface_from_core_animation_layer(
&self,
self: &Arc<Self>,
layer: *mut std::ffi::c_void,
) -> crate::Surface {
let surface = wgc::instance::Surface {
@ -52,7 +52,16 @@ impl Context {
self.0
.surfaces
.register(id, surface, &mut wgc::hub::Token::root());
crate::Surface { id }
crate::Surface {
context: Arc::clone(self),
id,
}
}
}
impl Drop for Context {
fn drop(&mut self) {
//nothing
}
}
@ -1207,6 +1216,15 @@ impl crate::Context for Context {
)
}
fn surface_drop(&self, surface: &Self::SurfaceId) {
self.0.surface_drop(*surface)
}
fn adapter_drop(&self, adapter: &Self::AdapterId) {
let global = &self.0;
wgc::gfx_select!(*adapter => global.adapter_drop(*adapter))
}
fn buffer_destroy(&self, buffer: &Self::BufferId) {
let global = &self.0;
wgc::gfx_select!(buffer.id => global.buffer_destroy(buffer.id)).unwrap_pretty()

View File

@ -1261,6 +1261,14 @@ impl crate::Context for Context {
Sendable(texture.0.create_view_with_descriptor(&mapped))
}
fn surface_drop(&self, _surface: &Self::SurfaceId) {
// Dropped automatically
}
fn adapter_drop(&self, _adapter: &Self::AdapterId) {
// Dropped automatically
}
fn buffer_destroy(&self, _buffer: &Self::BufferId) {
// TODO
}

View File

@ -302,6 +302,8 @@ trait Context: Debug + Send + Sized + Sync {
desc: &TextureViewDescriptor,
) -> Self::TextureViewId;
fn surface_drop(&self, surface: &Self::SurfaceId);
fn adapter_drop(&self, adapter: &Self::AdapterId);
fn buffer_destroy(&self, buffer: &Self::BufferId);
fn buffer_drop(&self, buffer: &Self::BufferId);
fn texture_destroy(&self, buffer: &Self::TextureId);
@ -434,6 +436,14 @@ pub struct Adapter {
id: <C as Context>::AdapterId,
}
impl Drop for Adapter {
fn drop(&mut self) {
if !thread::panicking() {
self.context.adapter_drop(&self.id)
}
}
}
/// Open connection to a graphics and/or compute device.
///
/// Responsible for the creation of most rendering and compute resources.
@ -586,9 +596,18 @@ impl Drop for Sampler {
/// be presented. A `Surface` may be created with the unsafe function [`Instance::create_surface`].
#[derive(Debug)]
pub struct Surface {
context: Arc<C>,
id: <C as Context>::SurfaceId,
}
impl Drop for Surface {
fn drop(&mut self) {
if !thread::panicking() {
self.context.surface_drop(&self.id)
}
}
}
/// Handle to a swap chain.
///
/// A `SwapChain` represents the image or series of images that will be presented to a [`Surface`].
@ -1335,6 +1354,7 @@ impl Instance {
window: &W,
) -> Surface {
Surface {
context: Arc::clone(&self.context),
id: Context::instance_create_surface(&*self.context, window),
}
}