Skip to content

Commit

Permalink
Update to new subsurface API, handling buffer releases
Browse files Browse the repository at this point in the history
  • Loading branch information
ids1024 committed Jan 27, 2024
1 parent 45d73a8 commit 0ea9398
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 9 deletions.
40 changes: 34 additions & 6 deletions src/wayland/buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@ use cctk::{
};
use cosmic::cctk;
use cosmic::iced::widget::image;
use cosmic::iced_sctk::subsurface_widget::{BufferSource, Dmabuf, Plane, Shmbuf, SubsurfaceBuffer};
use memmap2::Mmap;
use rustix::{io::Errno, shm::ShmOFlags};
use std::{
os::fd::{AsFd, OwnedFd},
path::{Path, PathBuf},
sync::Arc,
time::{SystemTime, UNIX_EPOCH},
};
use wayland_protocols::wp::linux_dmabuf::zv1::client::zwp_linux_buffer_params_v1;
Expand Down Expand Up @@ -71,7 +73,7 @@ enum BufferBacking {
}

pub struct Buffer {
backing: BufferBacking,
pub backing: Arc<BufferSource>,
pub buffer: wl_buffer::WlBuffer,
pub buffer_info: BufferInfo,
node: Option<PathBuf>,
Expand All @@ -89,6 +91,12 @@ impl AppData {
(),
);

pool.destroy();

// XXX
let fd = rustix::fs::memfd_create("shm-buffer", rustix::fs::MemfdFlags::CLOEXEC).unwrap();
rustix::fs::ftruncate(&fd, buffer_info.stride as u64 * buffer_info.height as u64).unwrap();

let format = wl_shm::Format::try_from(buffer_info.format).unwrap();
let buffer = pool.create_buffer(
0,
Expand All @@ -101,7 +109,15 @@ impl AppData {
);

Buffer {
backing: BufferBacking::Shm { fd },
backing: Arc::new(Shmbuf {
fd,
offset: 0,
width: buffer_info.width as i32,
height: buffer_info.height as i32,
stride: buffer_info.stride as i32,
format,
}
.into()),
buffer,
buffer_info: buffer_info.clone(),
node: None,
Expand Down Expand Up @@ -151,8 +167,8 @@ impl AppData {
)?
};

let fd = bo.fd()?;
let stride = bo.stride()?;
let mut planes = Vec::new();

let params = self.dmabuf_state.create_params(&self.qh)?;
for i in 0..bo.plane_count()? as i32 {
let plane_fd = bo.fd_for_plane(i)?;
Expand All @@ -165,6 +181,12 @@ impl AppData {
plane_stride,
modifier.into(),
);
planes.push(Plane {
fd: plane_fd,
plane_idx: i as u32,
offset: plane_offset,
stride: plane_stride,
});
}
let buffer = params
.create_immed(
Expand All @@ -177,7 +199,13 @@ impl AppData {
.0;

Ok(Some(Buffer {
backing: BufferBacking::Dmabuf { fd, stride },
backing: Arc::new(Dmabuf {
width: buffer_info.width as i32,
height: buffer_info.height as i32,
planes,
format: buffer_info.format,
modifier: modifier.into(),
}.into()),
buffer,
buffer_info: buffer_info.clone(),
node: Some(node.clone()),
Expand Down Expand Up @@ -219,7 +247,7 @@ impl Buffer {

impl Drop for Buffer {
fn drop(&mut self) {
// self.buffer.destroy();
self.buffer.destroy();
}
}

Expand Down
3 changes: 2 additions & 1 deletion src/wayland/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ use cosmic::iced::{
self,
futures::{executor::block_on, FutureExt, SinkExt},
};
use cosmic::iced_sctk::subsurface_widget::SubsurfaceBuffer;
use futures_channel::mpsc;
use std::{
cell::RefCell,
Expand Down Expand Up @@ -85,7 +86,7 @@ pub enum Event {
pub struct CaptureImage {
pub width: u32,
pub height: u32,
pub wl_buffer: wl_buffer::WlBuffer,
pub wl_buffer: SubsurfaceBuffer,
}

pub fn subscription(conn: Connection) -> iced::Subscription<Event> {
Expand Down
24 changes: 22 additions & 2 deletions src/wayland/screencopy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use cosmic::cctk::{
},
wayland_client::{Connection, QueueHandle, WEnum},
};
use cosmic::iced_sctk::subsurface_widget::{SubsurfaceBuffer, SubsurfaceBufferRelease};
use std::{
array,
sync::{Arc, Weak},
Expand All @@ -21,6 +22,9 @@ pub struct ScreencopySession {
buffers: Option<[Buffer; 2]>,
session: zcosmic_screencopy_session_v1::ZcosmicScreencopySessionV1,
first_frame: bool,
// Future signaled when buffer is signaled.
// if triple buffer is used, will need more than one.
release: Option<SubsurfaceBufferRelease>
}

impl ScreencopySession {
Expand Down Expand Up @@ -54,6 +58,7 @@ impl ScreencopySession {
buffers: None,
session,
first_frame: true,
release: None,
}
}

Expand Down Expand Up @@ -150,14 +155,29 @@ impl ScreencopyHandler for AppData {

// Capture again on damage
// XXX wait for buffer release
session.attach_buffer_and_commit(&capture, conn);
let capture_clone = capture.clone();
let conn = conn.clone();
let release = session.release.take();
self.scheduler.schedule(async move {
if let Some(release) = release {
// Wait for buffer to be released by server
release.await;
}
let mut session = capture_clone.session.lock().unwrap();
let Some(session) = session.as_mut() else {
return;
};
session.attach_buffer_and_commit(&capture_clone, &conn);
});

let front = session.buffers.as_mut().unwrap().first_mut().unwrap();
let (buffer, release) = SubsurfaceBuffer::new(front.backing.clone());
session.release = Some(release);
// let img = unsafe { front.to_image() };
// let image = CaptureImage { img };
let buffer_info = &front.buffer_info;
let image = CaptureImage {
wl_buffer: front.buffer.clone(),
wl_buffer: buffer,
width: buffer_info.width,
height: buffer_info.height,
};
Expand Down

0 comments on commit 0ea9398

Please sign in to comment.