2023-02-09 14:10:31 -08:00
|
|
|
use cctk::{
|
2023-11-08 15:05:45 -08:00
|
|
|
cosmic_protocols::screencopy::v1::client::zcosmic_screencopy_session_v1::BufferType,
|
2023-02-09 14:10:31 -08:00
|
|
|
screencopy::BufferInfo,
|
2023-11-08 15:05:45 -08:00
|
|
|
sctk::shm::raw::RawPool,
|
2023-02-09 14:10:31 -08:00
|
|
|
wayland_client::{
|
2024-01-26 14:13:58 -08:00
|
|
|
protocol::{wl_buffer, wl_shm, wl_shm_pool},
|
2023-11-08 15:05:45 -08:00
|
|
|
Connection, Dispatch, QueueHandle, WEnum,
|
2023-02-09 14:10:31 -08:00
|
|
|
},
|
|
|
|
|
};
|
2023-11-21 16:15:02 -05:00
|
|
|
use cosmic::cctk;
|
2023-02-09 14:10:31 -08:00
|
|
|
use cosmic::iced::widget::image;
|
2024-01-26 14:13:58 -08:00
|
|
|
use memmap2::MmapMut;
|
|
|
|
|
use rustix::{io::Errno, shm::ShmOFlags};
|
2023-11-09 13:45:28 -08:00
|
|
|
use std::{
|
|
|
|
|
os::fd::{AsFd, OwnedFd},
|
|
|
|
|
path::{Path, PathBuf},
|
2024-01-26 14:13:58 -08:00
|
|
|
time::{SystemTime, UNIX_EPOCH},
|
2023-11-09 13:45:28 -08:00
|
|
|
};
|
2023-11-08 15:51:12 -08:00
|
|
|
use wayland_protocols::wp::linux_dmabuf::zv1::client::zwp_linux_buffer_params_v1;
|
2023-02-09 14:10:31 -08:00
|
|
|
|
2023-02-09 16:04:36 -08:00
|
|
|
use super::AppData;
|
|
|
|
|
|
2024-01-26 14:13:58 -08:00
|
|
|
#[cfg(target_os = "linux")]
|
|
|
|
|
fn create_memfd() -> rustix::io::Result<OwnedFd> {
|
|
|
|
|
let fd = rustix::io::retry_on_intr(|| {
|
|
|
|
|
rustix::fs::memfd_create(
|
|
|
|
|
"cosmic-workspaces-shm",
|
|
|
|
|
rustix::fs::MemfdFlags::CLOEXEC | rustix::fs::MemfdFlags::ALLOW_SEALING,
|
|
|
|
|
)
|
|
|
|
|
})?;
|
|
|
|
|
let _ = rustix::fs::fcntl_add_seals(
|
|
|
|
|
&fd,
|
|
|
|
|
rustix::fs::SealFlags::SHRINK | rustix::fs::SealFlags::SEAL,
|
|
|
|
|
);
|
|
|
|
|
Ok(fd)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn create_memfile() -> rustix::io::Result<OwnedFd> {
|
|
|
|
|
#[cfg(target_os = "linux")]
|
|
|
|
|
if let Ok(fd) = create_memfd() {
|
|
|
|
|
return Ok(fd);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
loop {
|
|
|
|
|
let flags = ShmOFlags::CREATE | ShmOFlags::EXCL | ShmOFlags::RDWR;
|
|
|
|
|
|
|
|
|
|
let time = SystemTime::now();
|
|
|
|
|
let name = format!(
|
|
|
|
|
"/cosmic-workspaces-shm-{}",
|
|
|
|
|
time.duration_since(UNIX_EPOCH).unwrap().subsec_nanos()
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
match rustix::shm::shm_open(&name, flags, 0600.into()) {
|
|
|
|
|
Ok(fd) => match rustix::shm::shm_unlink(&name) {
|
|
|
|
|
Ok(_) => return Ok(fd),
|
|
|
|
|
Err(errno) => {
|
|
|
|
|
return Err(errno.into());
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
Err(Errno::EXIST | Errno::EXIST) => {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
Err(err) => return Err(err.into()),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-11-08 15:51:12 -08:00
|
|
|
enum BufferBacking {
|
2023-11-09 13:45:28 -08:00
|
|
|
Shm {
|
2024-01-26 14:13:58 -08:00
|
|
|
fd: OwnedFd,
|
|
|
|
|
mmap: MmapMut,
|
2023-11-09 13:45:28 -08:00
|
|
|
},
|
|
|
|
|
Dmabuf {
|
|
|
|
|
fd: OwnedFd,
|
|
|
|
|
node: PathBuf,
|
|
|
|
|
stride: u32,
|
|
|
|
|
},
|
2023-11-08 15:51:12 -08:00
|
|
|
}
|
|
|
|
|
|
2023-02-09 14:10:31 -08:00
|
|
|
pub struct Buffer {
|
2023-11-08 15:51:12 -08:00
|
|
|
backing: BufferBacking,
|
2023-02-09 14:10:31 -08:00
|
|
|
pub buffer: wl_buffer::WlBuffer,
|
|
|
|
|
pub buffer_info: BufferInfo,
|
|
|
|
|
}
|
|
|
|
|
|
2023-11-08 15:05:45 -08:00
|
|
|
impl AppData {
|
2023-11-08 15:51:12 -08:00
|
|
|
fn create_shm_backing(&self, buffer_info: &BufferInfo) -> (BufferBacking, wl_buffer::WlBuffer) {
|
2024-01-26 14:13:58 -08:00
|
|
|
let fd = create_memfile().unwrap(); // XXX?
|
|
|
|
|
rustix::fs::ftruncate(&fd, buffer_info.stride as u64 * buffer_info.height as u64);
|
|
|
|
|
|
|
|
|
|
let pool = self.shm_state.wl_shm().create_pool(
|
|
|
|
|
fd.as_fd(),
|
|
|
|
|
buffer_info.stride as i32 * buffer_info.height as i32,
|
|
|
|
|
&self.qh,
|
|
|
|
|
(),
|
|
|
|
|
);
|
2023-11-08 15:51:12 -08:00
|
|
|
|
2023-02-09 14:10:31 -08:00
|
|
|
let format = wl_shm::Format::try_from(buffer_info.format).unwrap();
|
|
|
|
|
let buffer = pool.create_buffer(
|
|
|
|
|
0,
|
|
|
|
|
buffer_info.width as i32,
|
|
|
|
|
buffer_info.height as i32,
|
|
|
|
|
buffer_info.stride as i32,
|
|
|
|
|
format,
|
2023-11-08 15:05:45 -08:00
|
|
|
&self.qh,
|
2024-01-26 14:13:58 -08:00
|
|
|
(),
|
2023-02-09 14:10:31 -08:00
|
|
|
);
|
2023-11-08 15:51:12 -08:00
|
|
|
|
2024-01-26 14:13:58 -08:00
|
|
|
let mmap = unsafe { MmapMut::map_mut(&fd).unwrap() };
|
|
|
|
|
|
|
|
|
|
(BufferBacking::Shm { fd, mmap }, buffer)
|
2023-11-08 15:51:12 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[allow(dead_code)]
|
|
|
|
|
fn create_gbm_backing(
|
|
|
|
|
&self,
|
|
|
|
|
buffer_info: &BufferInfo,
|
|
|
|
|
needs_linear: bool,
|
2023-11-16 13:36:38 -08:00
|
|
|
) -> anyhow::Result<Option<(BufferBacking, wl_buffer::WlBuffer)>> {
|
|
|
|
|
let (Some((node, gbm)), Some(feedback)) =
|
|
|
|
|
(self.gbm.as_ref(), self.dmabuf_feedback.as_ref())
|
|
|
|
|
else {
|
|
|
|
|
return Ok(None);
|
|
|
|
|
};
|
2023-11-08 15:51:12 -08:00
|
|
|
let formats = feedback.format_table();
|
2023-11-16 13:36:38 -08:00
|
|
|
let Some(format_info) = feedback
|
2023-11-08 15:51:12 -08:00
|
|
|
.tranches()
|
|
|
|
|
.iter()
|
|
|
|
|
.flat_map(|x| &x.formats)
|
|
|
|
|
.filter_map(|x| formats.get(*x as usize))
|
|
|
|
|
.find(|x| {
|
|
|
|
|
x.format == buffer_info.format
|
|
|
|
|
&& (!needs_linear || x.modifier == u64::from(gbm::Modifier::Linear))
|
2023-11-16 13:36:38 -08:00
|
|
|
})
|
|
|
|
|
else {
|
|
|
|
|
return Ok(None);
|
|
|
|
|
};
|
|
|
|
|
let format = gbm::Format::try_from(buffer_info.format)?;
|
|
|
|
|
let modifier = gbm::Modifier::try_from(format_info.modifier)?;
|
2024-01-25 18:32:17 -08:00
|
|
|
let bo = if modifier != gbm::Modifier::Invalid {
|
|
|
|
|
gbm.create_buffer_object_with_modifiers::<()>(
|
|
|
|
|
buffer_info.width,
|
|
|
|
|
buffer_info.height,
|
|
|
|
|
format,
|
|
|
|
|
[modifier].into_iter(),
|
|
|
|
|
)?
|
|
|
|
|
} else {
|
|
|
|
|
// TODO make sure this isn't used across different GPUs
|
|
|
|
|
gbm.create_buffer_object::<()>(
|
|
|
|
|
buffer_info.width,
|
|
|
|
|
buffer_info.height,
|
|
|
|
|
format,
|
|
|
|
|
gbm::BufferObjectFlags::empty(),
|
|
|
|
|
)?
|
|
|
|
|
};
|
2023-11-08 15:51:12 -08:00
|
|
|
|
2023-11-16 13:36:38 -08:00
|
|
|
let fd = bo.fd()?;
|
|
|
|
|
let stride = bo.stride()?;
|
|
|
|
|
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)?;
|
|
|
|
|
let plane_offset = bo.offset(i)?;
|
|
|
|
|
let plane_stride = bo.stride_for_plane(i)?;
|
2023-11-16 13:23:26 -08:00
|
|
|
params.add(
|
|
|
|
|
plane_fd.as_fd(),
|
|
|
|
|
i as u32,
|
|
|
|
|
plane_offset,
|
|
|
|
|
plane_stride,
|
|
|
|
|
modifier.into(),
|
|
|
|
|
);
|
|
|
|
|
}
|
2023-11-08 15:51:12 -08:00
|
|
|
let buffer = params
|
|
|
|
|
.create_immed(
|
|
|
|
|
buffer_info.width as i32,
|
|
|
|
|
buffer_info.height as i32,
|
|
|
|
|
buffer_info.format,
|
|
|
|
|
zwp_linux_buffer_params_v1::Flags::empty(),
|
|
|
|
|
&self.qh,
|
|
|
|
|
)
|
|
|
|
|
.0;
|
|
|
|
|
|
2023-11-16 13:36:38 -08:00
|
|
|
Ok(Some((
|
2023-11-09 13:45:28 -08:00
|
|
|
BufferBacking::Dmabuf {
|
|
|
|
|
fd,
|
|
|
|
|
node: node.clone(),
|
|
|
|
|
stride,
|
|
|
|
|
},
|
|
|
|
|
buffer,
|
2023-11-16 13:36:38 -08:00
|
|
|
)))
|
2023-11-08 15:51:12 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn create_buffer(&self, buffer_infos: &[BufferInfo]) -> Buffer {
|
|
|
|
|
// XXX Handle other formats?
|
|
|
|
|
let format = wl_shm::Format::Abgr8888.into();
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
if let Some(buffer_info) = buffer_infos
|
|
|
|
|
.iter()
|
|
|
|
|
.find(|x| x.type_ == WEnum::Value(BufferType::Dmabuf) && x.format == format)
|
|
|
|
|
{
|
2023-11-16 13:36:38 -08:00
|
|
|
match self.create_gbm_backing(buffer_info, true) {
|
|
|
|
|
Ok(Some((backing, buffer))) => {
|
|
|
|
|
return Buffer {
|
|
|
|
|
backing,
|
|
|
|
|
buffer,
|
|
|
|
|
buffer_info: buffer_info.clone(),
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
Ok(None) => {}
|
|
|
|
|
Err(err) => eprintln!("Failed to create gbm buffer: {}", err),
|
2023-11-08 15:51:12 -08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
// Fallback to shm buffer
|
|
|
|
|
// Assume format is already known to be valid
|
|
|
|
|
let buffer_info = buffer_infos
|
|
|
|
|
.iter()
|
|
|
|
|
.find(|x| x.type_ == WEnum::Value(BufferType::WlShm) && x.format == format)
|
|
|
|
|
.unwrap();
|
|
|
|
|
let (backing, buffer) = self.create_shm_backing(buffer_info);
|
2023-11-08 15:05:45 -08:00
|
|
|
Buffer {
|
2023-11-08 15:51:12 -08:00
|
|
|
backing,
|
2023-02-09 14:10:31 -08:00
|
|
|
buffer,
|
2023-11-08 15:05:45 -08:00
|
|
|
buffer_info: buffer_info.clone(),
|
2023-02-09 14:10:31 -08:00
|
|
|
}
|
|
|
|
|
}
|
2023-11-08 15:05:45 -08:00
|
|
|
}
|
2023-02-09 14:10:31 -08:00
|
|
|
|
2023-11-08 15:05:45 -08:00
|
|
|
impl Buffer {
|
2023-02-09 14:10:31 -08:00
|
|
|
// Buffer must be released by server for safety
|
2023-11-08 15:51:12 -08:00
|
|
|
// XXX is this at all a performance issue?
|
2023-02-09 14:29:34 -08:00
|
|
|
#[allow(clippy::wrong_self_convention)]
|
2023-02-09 14:10:31 -08:00
|
|
|
pub unsafe fn to_image(&mut self) -> image::Handle {
|
2023-11-08 15:51:12 -08:00
|
|
|
let pixels = match &mut self.backing {
|
2024-01-26 14:13:58 -08:00
|
|
|
BufferBacking::Shm { mmap, .. } => mmap.to_vec(),
|
2023-11-08 15:51:12 -08:00
|
|
|
// NOTE: Only will work with linear modifier
|
2023-11-09 13:45:28 -08:00
|
|
|
BufferBacking::Dmabuf {
|
|
|
|
|
fd,
|
|
|
|
|
node: _,
|
|
|
|
|
stride,
|
|
|
|
|
} => {
|
2023-11-08 15:51:12 -08:00
|
|
|
// XXX Error handling?
|
|
|
|
|
let mmap = memmap2::Mmap::map(&*fd).unwrap();
|
|
|
|
|
if self.buffer_info.stride == self.buffer_info.width * 4 {
|
|
|
|
|
mmap.to_vec()
|
|
|
|
|
} else {
|
|
|
|
|
let width = self.buffer_info.width as usize;
|
|
|
|
|
let height = self.buffer_info.height as usize;
|
2023-11-08 16:28:53 -08:00
|
|
|
let stride = *stride as usize;
|
2023-11-08 15:51:12 -08:00
|
|
|
let output_stride = width * 4;
|
|
|
|
|
let mut pixels = vec![0; height * output_stride];
|
|
|
|
|
for y in 0..height {
|
|
|
|
|
pixels[y * output_stride..y * output_stride + output_stride]
|
|
|
|
|
.copy_from_slice(&mmap[y * stride..y * stride + output_stride]);
|
|
|
|
|
}
|
|
|
|
|
pixels
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
image::Handle::from_pixels(self.buffer_info.width, self.buffer_info.height, pixels)
|
2023-02-09 14:10:31 -08:00
|
|
|
}
|
2023-11-09 13:45:28 -08:00
|
|
|
|
|
|
|
|
pub fn node(&self) -> Option<&Path> {
|
|
|
|
|
match &self.backing {
|
|
|
|
|
BufferBacking::Shm { .. } => None,
|
|
|
|
|
BufferBacking::Dmabuf { node, .. } => Some(node),
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-02-09 14:10:31 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Drop for Buffer {
|
|
|
|
|
fn drop(&mut self) {
|
|
|
|
|
self.buffer.destroy();
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-02-09 16:04:36 -08:00
|
|
|
|
|
|
|
|
impl Dispatch<wl_buffer::WlBuffer, ()> for AppData {
|
|
|
|
|
fn event(
|
|
|
|
|
_app_data: &mut Self,
|
|
|
|
|
_buffer: &wl_buffer::WlBuffer,
|
|
|
|
|
event: wl_buffer::Event,
|
|
|
|
|
_: &(),
|
|
|
|
|
_: &Connection,
|
|
|
|
|
_qh: &QueueHandle<Self>,
|
|
|
|
|
) {
|
|
|
|
|
match event {
|
|
|
|
|
wl_buffer::Event::Release => {}
|
|
|
|
|
_ => unreachable!(),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-01-26 14:13:58 -08:00
|
|
|
|
|
|
|
|
impl Dispatch<wl_shm_pool::WlShmPool, ()> for AppData {
|
|
|
|
|
fn event(
|
|
|
|
|
_app_data: &mut Self,
|
|
|
|
|
_shm: &wl_shm_pool::WlShmPool,
|
|
|
|
|
_event: wl_shm_pool::Event,
|
|
|
|
|
_: &(),
|
|
|
|
|
_: &Connection,
|
|
|
|
|
_qh: &QueueHandle<Self>,
|
|
|
|
|
) {
|
|
|
|
|
}
|
|
|
|
|
}
|