824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 1 | use crate::device::Shared; |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 2 | use ash::vk; |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 3 | use rustix::event::{PollFd, PollFlags, poll}; |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 4 | use smithay::backend::renderer::sync::{Fence, Interrupted}; |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 5 | use std::fmt; |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 6 | use std::os::fd::{FromRawFd, OwnedFd}; |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 7 | use std::sync::{Arc, Mutex}; |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 8 | |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 9 | enum State { |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 10 | Pending, |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 11 | /// Exporting a sync_file resets the Vulkan fence, so from then on the |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 12 | /// file is the only record of completion. `None` means it had already |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 13 | /// signalled when exported. |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 14 | Exported(Option<OwnedFd>), |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 15 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 16 | |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 17 | pub(crate) struct FenceInner { |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 18 | shared: Arc<Shared>, |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 19 | fence: vk::Fence, |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 20 | exportable: bool, |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 21 | state: Mutex<State>, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 22 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 23 | |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 24 | impl FenceInner { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 25 | pub fn new(shared: &Arc<Shared>) -> Result<Arc<Self>, vk::Result> { |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 26 | let exportable = shared.device.dmabuf.is_some(); |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 27 | let mut export = vk::ExportFenceCreateInfo::default() |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 28 | .handle_types(vk::ExternalFenceHandleTypeFlags::SYNC_FD); |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 29 | let mut info = vk::FenceCreateInfo::default(); |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 30 | if exportable { |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 31 | info = info.push_next(&mut export); |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 32 | } |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 33 | let fence = unsafe { shared.device.device.create_fence(&info, None)? }; |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 34 | Ok(Arc::new(Self { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 35 | shared: shared.clone(), |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 36 | fence, |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 37 | exportable, |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 38 | state: Mutex::new(State::Pending), |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 39 | })) |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 40 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 41 | |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 42 | pub fn fence(&self) -> vk::Fence { |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 43 | self.fence |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 44 | } |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 45 | |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 46 | pub fn signaled(&self) -> bool { |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 47 | match &*self.state.lock().unwrap_or_else(|e| e.into_inner()) { |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 48 | State::Pending => { |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 49 | unsafe { self.shared.device.device.get_fence_status(self.fence) }.unwrap_or(true) |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 50 | } |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 51 | State::Exported(None) => true, |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 52 | State::Exported(Some(fd)) => readable(fd, Some(&Default::default())), |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 53 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 54 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 55 | |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 56 | pub fn wait(&self) -> Result<(), vk::Result> { |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 57 | match &*self.state.lock().unwrap_or_else(|e| e.into_inner()) { |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 58 | State::Pending => unsafe { |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 59 | self.shared |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 60 | .device |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 61 | .device |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 62 | .wait_for_fences(&[self.fence], true, u64::MAX) |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 63 | }, |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 64 | State::Exported(None) => Ok(()), |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 65 | State::Exported(Some(fd)) => { |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 66 | readable(fd, None); |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 67 | Ok(()) |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 68 | } |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 69 | } |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 70 | } |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 71 | |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 72 | fn export(&self) -> Option<OwnedFd> { |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 73 | if !self.exportable { |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 74 | return None; |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 75 | } |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 76 | let ext = self.shared.device.dmabuf.as_ref()?; |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 77 | let mut state = self.state.lock().unwrap_or_else(|e| e.into_inner()); |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 78 | if let State::Pending = *state { |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 79 | let info = vk::FenceGetFdInfoKHR::default() |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 80 | .fence(self.fence) |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 81 | .handle_type(vk::ExternalFenceHandleTypeFlags::SYNC_FD); |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 82 | let raw = unsafe { ext.fence_fd.get_fence_fd(&info) }.ok()?; |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 83 | *state = State::Exported((raw >= 0).then(|| unsafe { OwnedFd::from_raw_fd(raw) })); |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 84 | } |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 85 | match &*state { |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 86 | State::Exported(Some(fd)) => fd.try_clone().ok(), |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 87 | _ => None, |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 88 | } |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 89 | } |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 90 | } |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 91 | |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 92 | fn readable(fd: &OwnedFd, timeout: Option<&rustix::event::Timespec>) -> bool { |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 93 | let mut fds = [PollFd::new(fd, PollFlags::IN)]; |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 94 | loop { |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 95 | match poll(&mut fds, timeout) { |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 96 | Ok(n) => return n > 0, |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 97 | Err(rustix::io::Errno::INTR) => continue, |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 98 | Err(_) => return true, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 99 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 100 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 101 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 102 | |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 103 | impl Drop for FenceInner { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 104 | fn drop(&mut self) { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 105 | unsafe { self.shared.device.device.destroy_fence(self.fence, None) }; |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 106 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 107 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 108 | |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 109 | impl fmt::Debug for FenceInner { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 110 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 111 | f.debug_tuple("FenceInner").field(&self.fence).finish() |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 112 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 113 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 114 | |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 115 | #[derive(Debug)] |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 116 | pub(crate) struct VkFence(pub Arc<FenceInner>); |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 117 | |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 118 | impl Fence for VkFence { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 119 | fn is_signaled(&self) -> bool { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 120 | self.0.signaled() |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 121 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 122 | |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 123 | fn wait(&self) -> Result<(), Interrupted> { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 124 | self.0.wait().map_err(|_| Interrupted) |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 125 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 126 | |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 127 | fn is_exportable(&self) -> bool { |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 128 | self.0.exportable |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 129 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 130 | |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 131 | fn export(&self) -> Option<OwnedFd> { |
7de49c2aa70b | atagen | 2026-10-16 09:27 UTC | 132 | self.0.export() |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 133 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 134 | } |