Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions src/backend_dispatch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
3 changes: 3 additions & 0 deletions src/backend_interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ pub(crate) trait SurfaceInterface<D: HasDisplayHandle + ?Sized, W: HasWindowHand
/// Get the inner window handle.
fn window(&self) -> &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).
Expand Down
5 changes: 5 additions & 0 deletions src/backends/android.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@ impl<D: HasDisplayHandle, W: HasWindowHandle> SurfaceInterface<D, W> 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)
Expand Down
48 changes: 32 additions & 16 deletions src/backends/cg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ impl Observer {
}

#[derive(Debug)]
pub struct CGImpl<D, W> {
struct CGResources<D> {
/// Our layer.
layer: SendCALayer,
/// The layer that our layer was created from.
Expand All @@ -118,11 +118,18 @@ pub struct CGImpl<D, W> {
width: u32,
/// The height of the buffer.
height: u32,
window_handle: W,
_display: PhantomData<D>,
}

impl<D, W> Drop for CGImpl<D, W> {
#[derive(Debug)]
pub struct CGImpl<D, W> {
/// Resources needed for the surface.
resources: CGResources<D>,
/// The window associated with the surface.
window_handle: W,
}

impl<D> Drop for CGResources<D> {
fn drop(&mut self) {
// SAFETY: Registered in `new`, must be removed before the observer is deallocated.
unsafe {
Expand Down Expand Up @@ -265,7 +272,7 @@ impl<D: HasDisplayHandle, W: HasWindowHandle> SurfaceInterface<D, W> 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,
Expand All @@ -274,6 +281,10 @@ impl<D: HasDisplayHandle, W: HasWindowHandle> SurfaceInterface<D, W> for CGImpl<
width,
height,
_display: PhantomData,
};

Ok(Self {
resources,
window_handle: window_src,
})
}
Expand All @@ -283,6 +294,11 @@ impl<D: HasDisplayHandle, W: HasWindowHandle> SurfaceInterface<D, W> 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.
Expand All @@ -299,36 +315,36 @@ impl<D: HasDisplayHandle, W: HasWindowHandle> SurfaceInterface<D, W> 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<BufferImpl<'_>, 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,
Expand All @@ -337,7 +353,7 @@ impl<D: HasDisplayHandle, W: HasWindowHandle> SurfaceInterface<D, W> for CGImpl<
(AlphaMode::Postmultiplied, true) => CGImageAlphaInfo::First,
(AlphaMode::Postmultiplied, false) => CGImageAlphaInfo::Last,
},
layer: &mut self.layer,
layer: &mut self.resources.layer,
})
}
}
Expand Down
36 changes: 27 additions & 9 deletions src/backends/kms.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ impl<D: HasDisplayHandle + ?Sized> ContextInterface<D> for Arc<KmsDisplayImpl<D>

/// All the necessary types for the Drm/Kms backend.
#[derive(Debug)]
pub(crate) struct KmsImpl<D: ?Sized, W: ?Sized> {
struct KmsResources<D: ?Sized> {
/// The display implementation.
display: Arc<KmsDisplayImpl<D>>,

Expand All @@ -82,6 +82,13 @@ pub(crate) struct KmsImpl<D: ?Sized, W: ?Sized> {

/// The dumb buffer we're using as a buffer.
buffer: Option<Buffers>,
}

/// Drm/Kms backend resources and the window handle.
#[derive(Debug)]
pub(crate) struct KmsImpl<D: ?Sized, W: ?Sized> {
/// Resources necessary for the Drm/Kms backend.
resources: KmsResources<D>,

/// Window handle that we are keeping around.
window_handle: W,
Expand Down Expand Up @@ -211,11 +218,15 @@ impl<D: HasDisplayHandle + ?Sized, W: HasWindowHandle> SurfaceInterface<D, W> fo
.map(|info| info.handle())
.collect::<Vec<_>>();

Ok(Self {
let resources = KmsResources {
crtc,
connectors,
display: display.clone(),
buffer: None,
};

Ok(Self {
resources,
window_handle: window,
})
}
Expand All @@ -225,6 +236,11 @@ impl<D: HasDisplayHandle + ?Sized, W: HasWindowHandle> SurfaceInterface<D, W> fo
&self.window_handle
}

#[inline]
fn into_window(self) -> W {
self.window_handle
}

#[inline]
fn supports_alpha_mode(&self, alpha_mode: AlphaMode) -> bool {
matches!(
Expand All @@ -240,7 +256,7 @@ impl<D: HasDisplayHandle + ?Sized, W: HasWindowHandle> SurfaceInterface<D, W> 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(());
Expand All @@ -254,10 +270,10 @@ impl<D: HasDisplayHandle + ?Sized, W: HasWindowHandle> SurfaceInterface<D, W> 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],
});
Expand All @@ -274,6 +290,7 @@ impl<D: HasDisplayHandle + ?Sized, W: HasWindowHandle> SurfaceInterface<D, W> fo
fn next_buffer(&mut self, _alpha_mode: AlphaMode) -> Result<BufferImpl<'_>, SoftBufferError> {
// Map the dumb buffer.
let set = self
.resources
.buffer
.as_mut()
.expect("Must set size of surface before calling `next_buffer()`");
Expand All @@ -292,6 +309,7 @@ impl<D: HasDisplayHandle + ?Sized, W: HasWindowHandle> SurfaceInterface<D, W> fo
let back_age = &mut back_buffer.age;

let mapping = self
.resources
.display
.device
.map_dumb_buffer(&mut front_buffer.db)
Expand All @@ -302,15 +320,15 @@ impl<D: HasDisplayHandle + ?Sized, W: HasWindowHandle> SurfaceInterface<D, W> 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<D: ?Sized, W: ?Sized> Drop for KmsImpl<D, W> {
impl<D: ?Sized> Drop for KmsResources<D> {
fn drop(&mut self) {
// Map the CRTC to the information that was there before.
self.display
Expand Down
5 changes: 5 additions & 0 deletions src/backends/orbital.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,11 @@ impl<D: HasDisplayHandle, W: HasWindowHandle> SurfaceInterface<D, W> 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)
Expand Down
5 changes: 5 additions & 0 deletions src/backends/wayland/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,11 @@ impl<D: HasDisplayHandle + ?Sized, W: HasWindowHandle> SurfaceInterface<D, W>
&self.window_handle
}

#[inline]
fn into_window(self) -> W {
self.window_handle
}

#[inline]
fn supports_alpha_mode(&self, alpha_mode: AlphaMode) -> bool {
matches!(
Expand Down
5 changes: 5 additions & 0 deletions src/backends/web.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,11 @@ impl<D: HasDisplayHandle, W: HasWindowHandle> SurfaceInterface<D, W> 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)
Expand Down
Loading
Loading