|
| 1 | +//! EGL Sync Fences. |
| 2 | +
|
| 3 | +use std::ffi::c_void; |
| 4 | +use std::mem::MaybeUninit; |
| 5 | +use std::sync::Arc; |
| 6 | +use std::time::Duration; |
| 7 | + |
| 8 | +use glutin_egl_sys::egl::types::{EGLenum, EGLint}; |
| 9 | + |
| 10 | +use super::display::DisplayInner; |
| 11 | +use super::{egl, ErrorKind, VERSION_1_5}; |
| 12 | +use crate::error::Result; |
| 13 | + |
| 14 | +/// EGL sync object. |
| 15 | +#[derive(Debug, Clone)] |
| 16 | +pub struct Sync(pub(super) Arc<Inner>); |
| 17 | + |
| 18 | +impl Sync { |
| 19 | + /// Insert this sync into the currently bound context. |
| 20 | + /// |
| 21 | + /// If the EGL version is not at least 1.5 or the `EGL_KHR_wait_sync` |
| 22 | + /// extension is not available, this returns [`ErrorKind::NotSupported`]. |
| 23 | + /// |
| 24 | + /// This will return [`ErrorKind::BadParameter`] if there is no currently |
| 25 | + /// bound context. |
| 26 | + pub fn wait(&self) -> Result<()> { |
| 27 | + if self.0.display.version < VERSION_1_5 |
| 28 | + && !self.0.display.display_extensions.contains("EGL_KHR_wait_sync") |
| 29 | + { |
| 30 | + return Err(ErrorKind::NotSupported( |
| 31 | + "Sync::wait is not supported if EGL_KHR_wait_sync isn't available", |
| 32 | + ) |
| 33 | + .into()); |
| 34 | + } |
| 35 | + |
| 36 | + if unsafe { self.0.display.egl.WaitSyncKHR(*self.0.display.raw, self.0.inner, 0) } |
| 37 | + == egl::FALSE as EGLint |
| 38 | + { |
| 39 | + return Err(super::check_error().err().unwrap()); |
| 40 | + } |
| 41 | + |
| 42 | + Ok(()) |
| 43 | + } |
| 44 | + |
| 45 | + /// Query if the sync is already |
| 46 | + pub fn is_signalled(&self) -> Result<bool> { |
| 47 | + let status = unsafe { self.get_attrib(egl::SYNC_STATUS) }? as EGLenum; |
| 48 | + Ok(status == egl::SIGNALED) |
| 49 | + } |
| 50 | + |
| 51 | + /// Block and wait for the sync object to be signalled. |
| 52 | + /// |
| 53 | + /// A timeout of [`None`] will wait forever. If the timeout is [`Some`], the |
| 54 | + /// maximum timeout is [`u64::MAX`] - 1 nanoseconds. Anything larger will be |
| 55 | + /// truncated. If the timeout is reached this function returns [`false`]. |
| 56 | + /// |
| 57 | + /// If `flush` is [`true`], the currently bound context is flushed. |
| 58 | + pub fn client_wait(&self, timeout: Option<Duration>, flush: bool) -> Result<bool> { |
| 59 | + let flags = if flush { egl::SYNC_FLUSH_COMMANDS_BIT } else { 0 }; |
| 60 | + let timeout = timeout |
| 61 | + .as_ref() |
| 62 | + .map(Duration::as_nanos) |
| 63 | + .map(|nanos| nanos.max(u128::from(u64::MAX)) as u64) |
| 64 | + .unwrap_or(egl::FOREVER); |
| 65 | + |
| 66 | + let result = unsafe { |
| 67 | + self.0.display.egl.ClientWaitSyncKHR( |
| 68 | + *self.0.display.raw, |
| 69 | + self.0.inner, |
| 70 | + flags as EGLint, |
| 71 | + timeout, |
| 72 | + ) |
| 73 | + } as EGLenum; |
| 74 | + |
| 75 | + match result { |
| 76 | + egl::FALSE => Err(super::check_error().err().unwrap()), |
| 77 | + egl::TIMEOUT_EXPIRED => Ok(false), |
| 78 | + egl::CONDITION_SATISFIED => Ok(true), |
| 79 | + _ => unreachable!(), |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + /// Export the fence's underlying sync fd. |
| 84 | + /// |
| 85 | + /// Returns [`ErrorKind::NotSupported`] if the sync is not a native fence. |
| 86 | + /// |
| 87 | + /// # Availability |
| 88 | + /// |
| 89 | + /// This is available on Android and Linux when the |
| 90 | + /// `EGL_ANDROID_native_fence_sync` extension is available. |
| 91 | + #[cfg(unix)] |
| 92 | + pub fn export_sync_fd(&self) -> Result<std::os::unix::prelude::OwnedFd> { |
| 93 | + // Invariants: |
| 94 | + // - EGL_KHR_fence_sync must be supported if a Sync is creatable. |
| 95 | + use std::os::unix::prelude::FromRawFd; |
| 96 | + |
| 97 | + // Check the type of the fence to see if it can be exported. |
| 98 | + let ty = unsafe { self.get_attrib(egl::SYNC_TYPE) }?; |
| 99 | + |
| 100 | + // SAFETY: GetSyncAttribKHR was successful. |
| 101 | + if ty as EGLenum != egl::SYNC_NATIVE_FENCE_ANDROID { |
| 102 | + return Err(ErrorKind::NotSupported("The sync is not a native fence").into()); |
| 103 | + } |
| 104 | + |
| 105 | + // SAFETY: The fence type is SYNC_NATIVE_FENCE_ANDROID, making it possible to |
| 106 | + // export the native fence. |
| 107 | + let fd = unsafe { |
| 108 | + self.0.display.egl.DupNativeFenceFDANDROID(*self.0.display.raw, self.0.inner) |
| 109 | + }; |
| 110 | + |
| 111 | + if fd == egl::NO_NATIVE_FENCE_FD_ANDROID { |
| 112 | + return Err(super::check_error().err().unwrap()); |
| 113 | + } |
| 114 | + |
| 115 | + // SAFETY: |
| 116 | + // - The file descriptor from EGL is valid if the return value is not |
| 117 | + // NO_NATIVE_FENCE_FD_ANDROID. |
| 118 | + // - The EGL implemention duplicates the underlying file descriptor and |
| 119 | + // transfers ownership to the application. |
| 120 | + Ok(unsafe { std::os::unix::prelude::OwnedFd::from_raw_fd(fd) }) |
| 121 | + } |
| 122 | + |
| 123 | + /// Get a raw handle to the `EGLSync`. |
| 124 | + pub fn raw_device(&self) -> *const c_void { |
| 125 | + self.0.inner |
| 126 | + } |
| 127 | + |
| 128 | + unsafe fn get_attrib(&self, attrib: EGLenum) -> Result<EGLint> { |
| 129 | + let mut result = MaybeUninit::<EGLint>::uninit(); |
| 130 | + |
| 131 | + if unsafe { |
| 132 | + self.0.display.egl.GetSyncAttribKHR( |
| 133 | + *self.0.display.raw, |
| 134 | + self.0.inner, |
| 135 | + attrib as EGLint, |
| 136 | + result.as_mut_ptr().cast(), |
| 137 | + ) |
| 138 | + } == egl::FALSE |
| 139 | + { |
| 140 | + return Err(super::check_error().err().unwrap()); |
| 141 | + }; |
| 142 | + |
| 143 | + Ok(unsafe { result.assume_init() }) |
| 144 | + } |
| 145 | +} |
| 146 | + |
| 147 | +#[derive(Debug)] |
| 148 | +pub(super) struct Inner { |
| 149 | + pub(super) inner: egl::types::EGLSyncKHR, |
| 150 | + pub(super) display: Arc<DisplayInner>, |
| 151 | +} |
| 152 | + |
| 153 | +impl Drop for Inner { |
| 154 | + fn drop(&mut self) { |
| 155 | + // SAFETY: The Sync owns the underlying EGLSyncKHR |
| 156 | + if unsafe { self.display.egl.DestroySyncKHR(*self.display.raw, self.inner) } == egl::FALSE { |
| 157 | + // If this fails we can't do much in Drop. At least drain the error. |
| 158 | + let _ = super::check_error(); |
| 159 | + } |
| 160 | + } |
| 161 | +} |
| 162 | + |
| 163 | +// SAFETY: The Inner owns the sync and the display is valid. |
| 164 | +unsafe impl Send for Inner {} |
| 165 | +// SAFETY: EGL allows destroying the sync on any thread. |
| 166 | +unsafe impl std::marker::Sync for Inner {} |
0 commit comments