diff --git a/src/backend_dispatch.rs b/src/backend_dispatch.rs index e88f12c0..c7cfce2f 100644 --- a/src/backend_dispatch.rs +++ b/src/backend_dispatch.rs @@ -99,6 +99,15 @@ macro_rules! make_dispatch { } } + fn into_window(self) -> W { + match self { + $( + $(#[$attr])* + Self::$name(inner) => inner.into_window(), + )* + } + } + #[inline] fn supports_alpha_mode(&self, alpha_mode: AlphaMode) -> bool { match self { diff --git a/src/backend_interface.rs b/src/backend_interface.rs index 239d3fdc..a4e70a0d 100644 --- a/src/backend_interface.rs +++ b/src/backend_interface.rs @@ -26,6 +26,9 @@ pub(crate) trait SurfaceInterface &W; + /// Get the inner window handle and drop the surface. + fn into_window(self) -> W; + fn supports_alpha_mode(&self, alpha_mode: AlphaMode) -> bool; /// Reconfigure the internal buffer(s). diff --git a/src/backends/android.rs b/src/backends/android.rs index 7166c9d0..231c5ba4 100644 --- a/src/backends/android.rs +++ b/src/backends/android.rs @@ -52,6 +52,11 @@ impl SurfaceInterface for Android &self.window } + #[inline] + fn into_window(self) -> W { + self.window + } + #[inline] fn supports_alpha_mode(&self, alpha_mode: AlphaMode) -> bool { matches!(alpha_mode, AlphaMode::Opaque | AlphaMode::Ignored) diff --git a/src/backends/cg.rs b/src/backends/cg.rs index 477e67cb..1ad4a7d7 100644 --- a/src/backends/cg.rs +++ b/src/backends/cg.rs @@ -100,7 +100,7 @@ impl Observer { } #[derive(Debug)] -pub struct CGImpl { +struct CGResources { /// Our layer. layer: SendCALayer, /// The layer that our layer was created from. @@ -118,11 +118,18 @@ pub struct CGImpl { width: u32, /// The height of the buffer. height: u32, - window_handle: W, _display: PhantomData, } -impl Drop for CGImpl { +#[derive(Debug)] +pub struct CGImpl { + /// Resources needed for the surface. + resources: CGResources, + /// The window associated with the surface. + window_handle: W, +} + +impl Drop for CGResources { fn drop(&mut self) { // SAFETY: Registered in `new`, must be removed before the observer is deallocated. unsafe { @@ -265,7 +272,7 @@ impl SurfaceInterface for CGImpl< let width = (size.width * scale_factor) as u32; let height = (size.height * scale_factor) as u32; - Ok(Self { + let resources = CGResources { layer: SendCALayer(layer), root_layer: SendCALayer(root_layer), observer, @@ -274,6 +281,10 @@ impl SurfaceInterface for CGImpl< width, height, _display: PhantomData, + }; + + Ok(Self { + resources, window_handle: window_src, }) } @@ -283,6 +294,11 @@ impl SurfaceInterface for CGImpl< &self.window_handle } + #[inline] + fn into_window(self) -> W { + self.window_handle + } + #[inline] fn supports_alpha_mode(&self, alpha_mode: AlphaMode) -> bool { // Premultiplied doesn't seem to work, at least not with transparent windows. @@ -299,36 +315,36 @@ impl SurfaceInterface for CGImpl< alpha_mode: AlphaMode, ) -> Result<(), SoftBufferError> { let opaque = matches!(alpha_mode, AlphaMode::Opaque | AlphaMode::Ignored); - self.layer.setOpaque(opaque); + self.resources.layer.setOpaque(opaque); // TODO: Set opaque-ness on root layer too? Is that our responsibility, or Winit's? - // self.root_layer.setOpaque(opaque); + // self.resources.root_layer.setOpaque(opaque); let width = width.get(); let height = height.get(); // TODO: Is this check desirable? - if self.width == width && self.height == height { + if self.resources.width == width && self.resources.height == height { return Ok(()); } // Recreate buffer. It's fine to release the old one, `CALayer.contents` is going to keep // a reference to it around as long as it's still in use. - self.buffer = Buffer::new(width, height); - self.width = width; - self.height = height; + self.resources.buffer = Buffer::new(width, height); + self.resources.width = width; + self.resources.height = height; Ok(()) } fn next_buffer(&mut self, alpha_mode: AlphaMode) -> Result, SoftBufferError> { // Unlocked in `present_with_damage` or the buffer's `Drop`. - self.buffer.info().lock(); + self.resources.buffer.info().lock(); Ok(BufferImpl { - buffer: &mut self.buffer, - width: self.width, - height: self.height, - color_space: &self.color_space, + buffer: &mut self.resources.buffer, + width: self.resources.width, + height: self.resources.height, + color_space: &self.resources.color_space, alpha_info: match (alpha_mode, cfg!(target_endian = "little")) { (AlphaMode::Opaque | AlphaMode::Ignored, true) => CGImageAlphaInfo::NoneSkipFirst, (AlphaMode::Opaque | AlphaMode::Ignored, false) => CGImageAlphaInfo::NoneSkipLast, @@ -337,7 +353,7 @@ impl SurfaceInterface for CGImpl< (AlphaMode::Postmultiplied, true) => CGImageAlphaInfo::First, (AlphaMode::Postmultiplied, false) => CGImageAlphaInfo::Last, }, - layer: &mut self.layer, + layer: &mut self.resources.layer, }) } } diff --git a/src/backends/kms.rs b/src/backends/kms.rs index e82770c2..a7f0f643 100644 --- a/src/backends/kms.rs +++ b/src/backends/kms.rs @@ -70,7 +70,7 @@ impl ContextInterface for Arc /// All the necessary types for the Drm/Kms backend. #[derive(Debug)] -pub(crate) struct KmsImpl { +struct KmsResources { /// The display implementation. display: Arc>, @@ -82,6 +82,13 @@ pub(crate) struct KmsImpl { /// The dumb buffer we're using as a buffer. buffer: Option, +} + +/// Drm/Kms backend resources and the window handle. +#[derive(Debug)] +pub(crate) struct KmsImpl { + /// Resources necessary for the Drm/Kms backend. + resources: KmsResources, /// Window handle that we are keeping around. window_handle: W, @@ -211,11 +218,15 @@ impl SurfaceInterface fo .map(|info| info.handle()) .collect::>(); - Ok(Self { + let resources = KmsResources { crtc, connectors, display: display.clone(), buffer: None, + }; + + Ok(Self { + resources, window_handle: window, }) } @@ -225,6 +236,11 @@ impl SurfaceInterface fo &self.window_handle } + #[inline] + fn into_window(self) -> W { + self.window_handle + } + #[inline] fn supports_alpha_mode(&self, alpha_mode: AlphaMode) -> bool { matches!( @@ -240,7 +256,7 @@ impl SurfaceInterface fo alpha_mode: AlphaMode, ) -> Result<(), SoftBufferError> { // Don't resize if we don't have to. - if let Some(buffer) = &self.buffer { + if let Some(buffer) = &self.resources.buffer { let (buffer_width, buffer_height) = buffer.size(); if buffer_width == width && buffer_height == height { return Ok(()); @@ -254,10 +270,10 @@ impl SurfaceInterface fo }; // Create a new buffer set. - let front_buffer = SharedBuffer::new(&self.display, width, height, format)?; - let back_buffer = SharedBuffer::new(&self.display, width, height, format)?; + let front_buffer = SharedBuffer::new(&self.resources.display, width, height, format)?; + let back_buffer = SharedBuffer::new(&self.resources.display, width, height, format)?; - self.buffer = Some(Buffers { + self.resources.buffer = Some(Buffers { first_is_front: true, buffers: [front_buffer, back_buffer], }); @@ -274,6 +290,7 @@ impl SurfaceInterface fo fn next_buffer(&mut self, _alpha_mode: AlphaMode) -> Result, SoftBufferError> { // Map the dumb buffer. let set = self + .resources .buffer .as_mut() .expect("Must set size of surface before calling `next_buffer()`"); @@ -292,6 +309,7 @@ impl SurfaceInterface fo let back_age = &mut back_buffer.age; let mapping = self + .resources .display .device .map_dumb_buffer(&mut front_buffer.db) @@ -302,15 +320,15 @@ impl SurfaceInterface fo size, first_is_front: &mut set.first_is_front, front_fb, - crtc_handle: self.crtc.handle(), - device: self.display.device.clone(), + crtc_handle: self.resources.crtc.handle(), + device: self.resources.display.device.clone(), front_age, back_age, }) } } -impl Drop for KmsImpl { +impl Drop for KmsResources { fn drop(&mut self) { // Map the CRTC to the information that was there before. self.display diff --git a/src/backends/orbital.rs b/src/backends/orbital.rs index 516fc547..7a295a58 100644 --- a/src/backends/orbital.rs +++ b/src/backends/orbital.rs @@ -104,6 +104,11 @@ impl SurfaceInterface for Orbital &self.window_handle } + #[inline] + fn into_window(self) -> W { + self.window_handle + } + #[inline] fn supports_alpha_mode(&self, alpha_mode: AlphaMode) -> bool { matches!(alpha_mode, AlphaMode::Opaque | AlphaMode::Ignored) diff --git a/src/backends/wayland/mod.rs b/src/backends/wayland/mod.rs index 1c85e0db..9183c595 100644 --- a/src/backends/wayland/mod.rs +++ b/src/backends/wayland/mod.rs @@ -133,6 +133,11 @@ impl SurfaceInterface &self.window_handle } + #[inline] + fn into_window(self) -> W { + self.window_handle + } + #[inline] fn supports_alpha_mode(&self, alpha_mode: AlphaMode) -> bool { matches!( diff --git a/src/backends/web.rs b/src/backends/web.rs index 845d5329..395ff816 100644 --- a/src/backends/web.rs +++ b/src/backends/web.rs @@ -166,6 +166,11 @@ impl SurfaceInterface for WebImpl &self.window_handle } + #[inline] + fn into_window(self) -> W { + self.window_handle + } + #[inline] fn supports_alpha_mode(&self, alpha_mode: AlphaMode) -> bool { matches!(alpha_mode, AlphaMode::Opaque | AlphaMode::Postmultiplied) diff --git a/src/backends/win32.rs b/src/backends/win32.rs index 566fbff9..b56dc9c9 100644 --- a/src/backends/win32.rs +++ b/src/backends/win32.rs @@ -152,9 +152,9 @@ impl Buffer { } } -/// The handle to a window for software buffering. +/// Resources for a Win32 surface. #[derive(Debug)] -pub struct Win32Impl { +pub struct Win32Resources { /// The window handle. window: OnlyUsedFromOrigin, @@ -164,18 +164,25 @@ pub struct Win32Impl { /// The buffer used to hold the image. buffer: Option, - /// The handle for the window. - /// - /// This should be kept alive in order to keep `window` valid. - handle: W, - /// The display handle. /// /// We don't use this, but other code might. _display: PhantomData, } -impl Drop for Win32Impl { +/// The handle to a window for software buffering. +#[derive(Debug)] +pub struct Win32Impl { + /// Resources needed for the surface Implementation. + resources: Win32Resources, + + /// The handle for the window. + /// + /// This should be kept alive in order to keep `window` valid. + window_handle: W, +} + +impl Drop for Win32Resources { fn drop(&mut self) { // Release our resources. Allocator::get().release(self.window.0, self.dc.0); @@ -217,18 +224,27 @@ impl SurfaceInterface for Win32Im .into()); } - Ok(Self { + let resources = Win32Resources { dc: dc.into(), window: hwnd.into(), buffer: None, - handle: window, _display: PhantomData, + }; + + Ok(Self { + resources, + window_handle: window, }) } #[inline] fn window(&self) -> &W { - &self.handle + &self.window_handle + } + + #[inline] + fn into_window(self) -> W { + self.window_handle } #[inline] @@ -250,7 +266,7 @@ impl SurfaceInterface for Win32Im })() .ok_or(SoftBufferError::SizeOutOfRange { width, height })?; - if let Some(buffer) = self.buffer.as_ref() { + if let Some(buffer) = self.resources.buffer.as_ref() { if buffer.width == width && buffer.height == height { return Ok(()); } @@ -261,20 +277,21 @@ impl SurfaceInterface for Win32Im AlphaMode::Premultiplied => true, AlphaMode::Postmultiplied => unreachable!("unsupported alpha format"), }; - self.buffer = Some(Buffer::new(self.dc.0, width, height, alpha)); + self.resources.buffer = Some(Buffer::new(self.resources.dc.0, width, height, alpha)); Ok(()) } fn next_buffer(&mut self, _alpha_mode: AlphaMode) -> Result, SoftBufferError> { let buffer = self + .resources .buffer .as_mut() .expect("Must set size of surface before calling `next_buffer()`"); Ok(BufferImpl { - window: &self.window, - dc: &self.dc, + window: &self.resources.window, + dc: &self.resources.dc, buffer, }) } diff --git a/src/backends/x11.rs b/src/backends/x11.rs index 741d1410..26b4794f 100644 --- a/src/backends/x11.rs +++ b/src/backends/x11.rs @@ -148,7 +148,7 @@ pub struct X11Impl { /// Keep the window alive. /// NOTE: Drop this second-to-last! - window_handle: W, + window_handle: Option, /// X display this window belongs to. /// NOTE: Drop this last! @@ -301,13 +301,18 @@ impl SurfaceInterface fo buffer, buffer_presented: false, size: None, - window_handle: window_src, + window_handle: Some(window_src), }) } #[inline] fn window(&self) -> &W { - &self.window_handle + self.window_handle.as_ref().unwrap() + } + + #[inline] + fn into_window(mut self) -> W { + self.window_handle.take().unwrap() } #[inline] diff --git a/src/lib.rs b/src/lib.rs index 5fd3f778..b70dddd4 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -118,6 +118,11 @@ impl Surface { self.surface_impl.window() } + /// Get the underlying window handle and drop the surface. + pub fn into_window(self) -> W { + self.surface_impl.into_window() + } + /// The current alpha mode of the surface. #[doc(alias = "transparency")] pub fn alpha_mode(&self) -> AlphaMode {