Merge pull request #54 from ids1024/bsd

Add support and CI tests for BSDs
This commit is contained in:
Jeremy Soller 2023-01-04 07:04:37 -07:00 committed by GitHub
commit 13853fe967
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 77 additions and 16 deletions

View file

@ -10,13 +10,13 @@ extern crate core;
mod cg;
#[cfg(target_os = "redox")]
mod orbital;
#[cfg(all(feature = "wayland", any(target_os = "linux", target_os = "freebsd")))]
#[cfg(wayland_platform)]
mod wayland;
#[cfg(target_arch = "wasm32")]
mod web;
#[cfg(target_os = "windows")]
mod win32;
#[cfg(all(feature = "x11", any(target_os = "linux", target_os = "freebsd")))]
#[cfg(x11_platform)]
mod x11;
mod error;
@ -66,9 +66,9 @@ macro_rules! make_dispatch {
}
make_dispatch! {
#[cfg(all(feature = "x11", any(target_os = "linux", target_os = "freebsd")))]
#[cfg(x11_platform)]
X11(x11::X11Impl),
#[cfg(all(feature = "wayland", any(target_os = "linux", target_os = "freebsd")))]
#[cfg(wayland_platform)]
Wayland(wayland::WaylandImpl),
#[cfg(target_os = "windows")]
Win32(win32::Win32Impl),
@ -105,21 +105,21 @@ impl GraphicsContext {
raw_display_handle: RawDisplayHandle,
) -> Result<Self, SoftBufferError> {
let imple: Dispatch = match (raw_window_handle, raw_display_handle) {
#[cfg(all(feature = "x11", any(target_os = "linux", target_os = "freebsd")))]
#[cfg(x11_platform)]
(
RawWindowHandle::Xlib(xlib_window_handle),
RawDisplayHandle::Xlib(xlib_display_handle),
) => Dispatch::X11(unsafe {
x11::X11Impl::from_xlib(xlib_window_handle, xlib_display_handle)?
}),
#[cfg(all(feature = "x11", any(target_os = "linux", target_os = "freebsd")))]
#[cfg(x11_platform)]
(
RawWindowHandle::Xcb(xcb_window_handle),
RawDisplayHandle::Xcb(xcb_display_handle),
) => Dispatch::X11(unsafe {
x11::X11Impl::from_xcb(xcb_window_handle, xcb_display_handle)?
}),
#[cfg(all(feature = "wayland", any(target_os = "linux", target_os = "freebsd")))]
#[cfg(wayland_platform)]
(
RawWindowHandle::Wayland(wayland_window_handle),
RawDisplayHandle::Wayland(wayland_display_handle),

View file

@ -1,4 +1,3 @@
use nix::sys::memfd::{memfd_create, MemFdCreateFlag};
use std::{
ffi::CStr,
fs::File,
@ -15,6 +14,50 @@ use wayland_client::{
use super::State;
#[cfg(any(target_os = "linux", target_os = "freebsd"))]
fn create_memfile() -> File {
use nix::sys::memfd::{memfd_create, MemFdCreateFlag};
let name = unsafe { CStr::from_bytes_with_nul_unchecked("softbuffer\0".as_bytes()) };
let fd = memfd_create(name, MemFdCreateFlag::MFD_CLOEXEC)
.expect("Failed to create memfd to store buffer.");
unsafe { File::from_raw_fd(fd) }
}
#[cfg(not(any(target_os = "linux", target_os = "freebsd")))]
fn create_memfile() -> File {
use nix::{
errno::Errno,
fcntl::OFlag,
sys::{
mman::{shm_open, shm_unlink},
stat::Mode,
},
};
use std::iter;
for _ in 0..=4 {
let mut name = String::from("softbuffer-");
name.extend(iter::repeat_with(fastrand::alphanumeric).take(7));
name.push('\0');
let name = unsafe { CStr::from_bytes_with_nul_unchecked(name.as_bytes()) };
// `CLOEXEC` is implied with `shm_open`
let fd = shm_open(
name,
OFlag::O_RDWR | OFlag::O_CREAT | OFlag::O_EXCL,
Mode::S_IRWXU,
);
if fd != Err(Errno::EEXIST) {
let fd = fd.expect("Failed to create POSIX shm to store buffer.");
let _ = shm_unlink(name);
return unsafe { File::from_raw_fd(fd) };
}
}
panic!("Failed to generate non-existant shm name")
}
pub(super) struct WaylandBuffer {
qh: QueueHandle<State>,
tempfile: File,
@ -28,10 +71,7 @@ pub(super) struct WaylandBuffer {
impl WaylandBuffer {
pub fn new(shm: &wl_shm::WlShm, width: i32, height: i32, qh: &QueueHandle<State>) -> Self {
let name = unsafe { CStr::from_bytes_with_nul_unchecked("softbuffer\0".as_bytes()) };
let tempfile_fd = memfd_create(name, MemFdCreateFlag::MFD_CLOEXEC)
.expect("Failed to create memfd to store buffer.");
let tempfile = unsafe { File::from_raw_fd(tempfile_fd) };
let tempfile = create_memfile();
let pool_size = width * height * 4;
let pool = shm.create_pool(tempfile.as_raw_fd(), pool_size, qh, ());
let released = Arc::new(AtomicBool::new(true));