1 | use memmap2::MmapMut; |
2 | use std::{ |
3 | ffi::CStr, |
4 | fs::File, |
5 | os::unix::prelude::{AsFd, AsRawFd}, |
6 | slice, |
7 | sync::{ |
8 | atomic::{AtomicBool, Ordering}, |
9 | Arc, |
10 | }, |
11 | }; |
12 | use wayland_client::{ |
13 | protocol::{wl_buffer, wl_shm, wl_shm_pool, wl_surface}, |
14 | Connection, Dispatch, QueueHandle, |
15 | }; |
16 | |
17 | use super::State; |
18 | |
19 | #[cfg (any(target_os = "linux" , target_os = "freebsd" ))] |
20 | fn create_memfile() -> File { |
21 | use rustix::fs::{MemfdFlags, SealFlags}; |
22 | |
23 | let name: &CStr = unsafe { CStr::from_bytes_with_nul_unchecked("softbuffer \0" .as_bytes()) }; |
24 | let fd: OwnedFd = rustix::fs::memfd_create(name, MemfdFlags::CLOEXEC | MemfdFlags::ALLOW_SEALING) |
25 | .expect(msg:"Failed to create memfd to store buffer." ); |
26 | rustix::fs::fcntl_add_seals(&fd, SealFlags::SHRINK | SealFlags::SEAL) |
27 | .expect(msg:"Failed to seal memfd." ); |
28 | File::from(fd) |
29 | } |
30 | |
31 | #[cfg (not(any(target_os = "linux" , target_os = "freebsd" )))] |
32 | fn create_memfile() -> File { |
33 | use rustix::{fs::Mode, io::Errno, shm::ShmOFlags}; |
34 | use std::iter; |
35 | |
36 | // Use a cached RNG to avoid hammering the thread local. |
37 | let mut rng = fastrand::Rng::new(); |
38 | |
39 | for _ in 0..=4 { |
40 | let mut name = String::from("softbuffer-" ); |
41 | name.extend(iter::repeat_with(|| rng.alphanumeric()).take(7)); |
42 | name.push(' \0' ); |
43 | |
44 | let name = unsafe { CStr::from_bytes_with_nul_unchecked(name.as_bytes()) }; |
45 | // `CLOEXEC` is implied with `shm_open` |
46 | let fd = rustix::shm::shm_open( |
47 | name, |
48 | ShmOFlags::RDWR | ShmOFlags::CREATE | ShmOFlags::EXCL, |
49 | Mode::RWXU, |
50 | ); |
51 | if !matches!(fd, Err(Errno::EXIST)) { |
52 | let fd = fd.expect("Failed to create POSIX shm to store buffer." ); |
53 | let _ = rustix::shm::shm_unlink(name); |
54 | return File::from(fd); |
55 | } |
56 | } |
57 | |
58 | panic!("Failed to generate non-existant shm name" ) |
59 | } |
60 | |
61 | // Round size to use for pool for given dimentions, rounding up to power of 2 |
62 | fn get_pool_size(width: i32, height: i32) -> i32 { |
63 | ((width * height * 4) as u32).next_power_of_two() as i32 |
64 | } |
65 | |
66 | unsafe fn map_file(file: &File) -> MmapMut { |
67 | unsafe { MmapMut::map_mut(file.as_raw_fd()).expect(msg:"Failed to map shared memory" ) } |
68 | } |
69 | |
70 | pub(super) struct WaylandBuffer { |
71 | qh: QueueHandle<State>, |
72 | tempfile: File, |
73 | map: MmapMut, |
74 | pool: wl_shm_pool::WlShmPool, |
75 | pool_size: i32, |
76 | buffer: wl_buffer::WlBuffer, |
77 | width: i32, |
78 | height: i32, |
79 | released: Arc<AtomicBool>, |
80 | pub age: u8, |
81 | } |
82 | |
83 | impl WaylandBuffer { |
84 | pub fn new(shm: &wl_shm::WlShm, width: i32, height: i32, qh: &QueueHandle<State>) -> Self { |
85 | // Calculate size to use for shm pool |
86 | let pool_size = get_pool_size(width, height); |
87 | |
88 | // Create an `mmap` shared memory |
89 | let tempfile = create_memfile(); |
90 | let _ = tempfile.set_len(pool_size as u64); |
91 | let map = unsafe { map_file(&tempfile) }; |
92 | |
93 | // Create wayland shm pool and buffer |
94 | let pool = shm.create_pool(tempfile.as_fd(), pool_size, qh, ()); |
95 | let released = Arc::new(AtomicBool::new(true)); |
96 | let buffer = pool.create_buffer( |
97 | 0, |
98 | width, |
99 | height, |
100 | width * 4, |
101 | wl_shm::Format::Xrgb8888, |
102 | qh, |
103 | released.clone(), |
104 | ); |
105 | |
106 | Self { |
107 | qh: qh.clone(), |
108 | map, |
109 | tempfile, |
110 | pool, |
111 | pool_size, |
112 | buffer, |
113 | width, |
114 | height, |
115 | released, |
116 | age: 0, |
117 | } |
118 | } |
119 | |
120 | pub fn resize(&mut self, width: i32, height: i32) { |
121 | // If size is the same, there's nothing to do |
122 | if self.width != width || self.height != height { |
123 | // Destroy old buffer |
124 | self.buffer.destroy(); |
125 | |
126 | // Grow pool, if needed |
127 | let size = ((width * height * 4) as u32).next_power_of_two() as i32; |
128 | if size > self.pool_size { |
129 | let _ = self.tempfile.set_len(size as u64); |
130 | self.pool.resize(size); |
131 | self.pool_size = size; |
132 | self.map = unsafe { map_file(&self.tempfile) }; |
133 | } |
134 | |
135 | // Create buffer with correct size |
136 | self.buffer = self.pool.create_buffer( |
137 | 0, |
138 | width, |
139 | height, |
140 | width * 4, |
141 | wl_shm::Format::Xrgb8888, |
142 | &self.qh, |
143 | self.released.clone(), |
144 | ); |
145 | self.width = width; |
146 | self.height = height; |
147 | } |
148 | } |
149 | |
150 | pub fn attach(&self, surface: &wl_surface::WlSurface) { |
151 | self.released.store(false, Ordering::SeqCst); |
152 | surface.attach(Some(&self.buffer), 0, 0); |
153 | } |
154 | |
155 | pub fn released(&self) -> bool { |
156 | self.released.load(Ordering::SeqCst) |
157 | } |
158 | |
159 | fn len(&self) -> usize { |
160 | self.width as usize * self.height as usize |
161 | } |
162 | |
163 | pub unsafe fn mapped_mut(&mut self) -> &mut [u32] { |
164 | unsafe { slice::from_raw_parts_mut(self.map.as_mut_ptr() as *mut u32, self.len()) } |
165 | } |
166 | } |
167 | |
168 | impl Drop for WaylandBuffer { |
169 | fn drop(&mut self) { |
170 | self.buffer.destroy(); |
171 | self.pool.destroy(); |
172 | } |
173 | } |
174 | |
175 | impl Dispatch<wl_shm_pool::WlShmPool, ()> for State { |
176 | fn event( |
177 | _: &mut State, |
178 | _: &wl_shm_pool::WlShmPool, |
179 | _: wl_shm_pool::Event, |
180 | _: &(), |
181 | _: &Connection, |
182 | _: &QueueHandle<State>, |
183 | ) { |
184 | } |
185 | } |
186 | |
187 | impl Dispatch<wl_buffer::WlBuffer, Arc<AtomicBool>> for State { |
188 | fn event( |
189 | _: &mut State, |
190 | _: &wl_buffer::WlBuffer, |
191 | event: wl_buffer::Event, |
192 | released: &Arc<AtomicBool>, |
193 | _: &Connection, |
194 | _: &QueueHandle<State>, |
195 | ) { |
196 | if let wl_buffer::Event::Release = event { |
197 | released.store(val:true, order:Ordering::SeqCst); |
198 | } |
199 | } |
200 | } |
201 | |