Skip to content
Merged
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
19 changes: 15 additions & 4 deletions src/buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use nix::{
sys::mman::{MapFlags, ProtFlags, mmap, munmap},
unistd::ftruncate,
};
use tracing::warn;
use wayland_client::{
Dispatch, QueueHandle,
protocol::{wl_buffer, wl_shm, wl_surface},
Expand Down Expand Up @@ -72,6 +73,14 @@ impl NLockBuffer {
format: wl_shm::Format,
qh: &QueueHandle<NLockState>,
) -> Option<Self> {
if width <= 0 || height <= 0 {
warn!(
"cannot create a buffer with dimensions: {}x{}",
width, height
);
return None;
}

let stride = width * 4;
let size = stride * height;

Expand All @@ -81,7 +90,7 @@ impl NLockBuffer {
let data = unsafe {
mmap(
None,
std::num::NonZeroUsize::new_unchecked(size as usize),
std::num::NonZeroUsize::new(size as usize)?,
ProtFlags::PROT_READ | ProtFlags::PROT_WRITE,
MapFlags::MAP_SHARED,
&fd,
Expand All @@ -105,7 +114,7 @@ impl NLockBuffer {
cairo::Format::ARgb32,
width,
height,
width * 4,
stride,
)
}
.ok()?;
Expand All @@ -128,16 +137,18 @@ impl NLockBuffer {
if self.state.in_use.swap(true, Ordering::AcqRel) {
None
} else {
// Buffer is now "in_use", explicit manage state
// Buffer is now "in_use", explicitly manage state
Some(NLockBufferGuard {
wl_buffer: &self.buffer,
state: &self.state,
committed: false,
})
}
}
}

pub fn destroy(&mut self) {
impl Drop for NLockBuffer {
fn drop(&mut self) {
self.buffer.destroy();
let _ = unsafe { munmap(self.data, self.size) };
}
Expand Down
3 changes: 2 additions & 1 deletion src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,8 @@ impl NLockState {
session_lock.destroy();
}

self.surfaces.iter_mut().for_each(|s| s.destroy());
// free any held surfaces
self.surfaces = Vec::new();

self.display.sync(qh, ());
self.session_lock = None;
Expand Down
8 changes: 6 additions & 2 deletions src/surface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -438,13 +438,17 @@ impl NLockSurface {

Ok(())
}
}

pub fn destroy(&mut self) {
impl Drop for NLockSurface {
fn drop(&mut self) {
if let Some(lock_surface) = &self.lock_surface {
lock_surface.destroy();
}

self.buffers.iter_mut().for_each(|buf| buf.destroy());
// free any held buffers
self.buffers = Vec::new();

self.output.release();
}
}
Expand Down
Loading