Split wayland buffer code into a file
This commit is contained in:
parent
c46ab4f1e6
commit
8c690e9648
2 changed files with 63 additions and 49 deletions
58
src/wayland/buffer.rs
Normal file
58
src/wayland/buffer.rs
Normal file
|
|
@ -0,0 +1,58 @@
|
|||
use cctk::{
|
||||
screencopy::BufferInfo,
|
||||
sctk::{globals::ProvidesBoundGlobal, shm::raw::RawPool},
|
||||
wayland_client::{
|
||||
protocol::{wl_buffer, wl_shm},
|
||||
QueueHandle,
|
||||
},
|
||||
};
|
||||
use cosmic::iced::widget::image;
|
||||
|
||||
pub struct Buffer {
|
||||
pub pool: RawPool,
|
||||
pub buffer: wl_buffer::WlBuffer,
|
||||
pub buffer_info: BufferInfo,
|
||||
}
|
||||
|
||||
impl Buffer {
|
||||
pub fn new(
|
||||
buffer_info: BufferInfo,
|
||||
shm: &impl ProvidesBoundGlobal<wl_shm::WlShm, 1>,
|
||||
qh: &QueueHandle<super::AppData>,
|
||||
) -> Self {
|
||||
// Assume format is already known to be valid
|
||||
let mut pool =
|
||||
RawPool::new((buffer_info.stride * buffer_info.height) as usize, shm).unwrap();
|
||||
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,
|
||||
(),
|
||||
qh,
|
||||
);
|
||||
Self {
|
||||
pool,
|
||||
buffer,
|
||||
buffer_info,
|
||||
}
|
||||
}
|
||||
|
||||
// Buffer must be released by server for safety
|
||||
pub unsafe fn to_image(&mut self) -> image::Handle {
|
||||
// XXX is this at all a performance issue?
|
||||
image::Handle::from_pixels(
|
||||
self.buffer_info.width,
|
||||
self.buffer_info.height,
|
||||
self.pool.mmap().to_vec(),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
impl Drop for Buffer {
|
||||
fn drop(&mut self) {
|
||||
self.buffer.destroy();
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue