|
| 1 | +//! Bindings for [sync functions] |
| 2 | +//! |
| 3 | +//! [sync functions]: https://developer.android.com/ndk/reference/group/sync |
| 4 | +#![cfg(feature = "sync")] |
| 5 | + |
| 6 | +use std::{ |
| 7 | + ffi::CStr, |
| 8 | + fmt::Debug, |
| 9 | + // TODO: Import from std::os::fd::{} since Rust 1.66 |
| 10 | + os::unix::io::{AsRawFd, BorrowedFd, FromRawFd, OwnedFd}, |
| 11 | + ptr::NonNull, |
| 12 | +}; |
| 13 | + |
| 14 | +#[doc(alias = "sync_file_info")] |
| 15 | +#[repr(transparent)] |
| 16 | +pub struct SyncFileInfo { |
| 17 | + inner: NonNull<ffi::sync_file_info>, |
| 18 | +} |
| 19 | + |
| 20 | +impl Debug for SyncFileInfo { |
| 21 | + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 22 | + f.debug_struct("SyncFileInfo") |
| 23 | + .field("name", &self.name()) |
| 24 | + .field("status", &self.status()) |
| 25 | + .field("flags", &self.flags()) |
| 26 | + .field("num_fences", &self.num_fences()) |
| 27 | + .field("fence_info", &self.fence_info()) |
| 28 | + .finish() |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | +impl SyncFileInfo { |
| 33 | + /// Retrieve detailed information about a sync file and its fences. |
| 34 | + #[doc(alias = "sync_file_info")] |
| 35 | + pub fn new(fd: BorrowedFd<'_>) -> Option<Self> { |
| 36 | + let inner = NonNull::new(unsafe { ffi::sync_file_info(fd.as_raw_fd()) })?; |
| 37 | + Some(Self { inner }) |
| 38 | + } |
| 39 | + |
| 40 | + pub fn name(&self) -> &CStr { |
| 41 | + let inner = unsafe { self.inner.as_ref() }; |
| 42 | + // TODO: Switch to CStr::from_bytes_until_nul (with c_char -> u8 transmute) since MSRV 1.69 |
| 43 | + // https://github.com/ash-rs/ash/pull/746 |
| 44 | + unsafe { CStr::from_ptr(inner.name.as_ptr()) } |
| 45 | + } |
| 46 | + |
| 47 | + pub fn status(&self) -> i32 { |
| 48 | + let inner = unsafe { self.inner.as_ref() }; |
| 49 | + inner.status |
| 50 | + } |
| 51 | + |
| 52 | + pub fn flags(&self) -> u32 { |
| 53 | + let inner = unsafe { self.inner.as_ref() }; |
| 54 | + inner.flags |
| 55 | + } |
| 56 | + |
| 57 | + pub fn num_fences(&self) -> usize { |
| 58 | + let inner = unsafe { self.inner.as_ref() }; |
| 59 | + inner.num_fences as usize |
| 60 | + } |
| 61 | + |
| 62 | + /// Get the array of fence infos from the sync file's info. |
| 63 | + #[doc(alias = "sync_get_fence_info")] |
| 64 | + pub fn fence_info(&self) -> &[SyncFenceInfo] { |
| 65 | + let inner = unsafe { self.inner.as_ref() }; |
| 66 | + |
| 67 | + if inner.num_fences == 0 { |
| 68 | + &[] |
| 69 | + } else { |
| 70 | + let sync_fence_info = NonNull::new(inner.sync_fence_info as *mut _) |
| 71 | + .expect("sync_fence_info cannot be null if num_fences > 0"); |
| 72 | + unsafe { |
| 73 | + std::slice::from_raw_parts(sync_fence_info.as_ptr(), inner.num_fences as usize) |
| 74 | + } |
| 75 | + } |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +impl Drop for SyncFileInfo { |
| 80 | + /// Free a [`struct@ffi::sync_file_info`] structure. |
| 81 | + #[doc(alias = "sync_file_info_free")] |
| 82 | + fn drop(&mut self) { |
| 83 | + unsafe { ffi::sync_file_info_free(self.inner.as_ptr()) } |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +#[doc(alias = "sync_fence_info")] |
| 88 | +#[repr(transparent)] |
| 89 | +pub struct SyncFenceInfo(ffi::sync_fence_info); |
| 90 | + |
| 91 | +impl Debug for SyncFenceInfo { |
| 92 | + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 93 | + f.debug_struct("SyncFenceInfo") |
| 94 | + .field("obj_name", &self.obj_name()) |
| 95 | + .field("driver_name", &self.driver_name()) |
| 96 | + .field("status", &self.status()) |
| 97 | + .field("flags", &self.flags()) |
| 98 | + .field("timestamp_ns", &self.timestamp_ns()) |
| 99 | + .finish() |
| 100 | + } |
| 101 | +} |
| 102 | + |
| 103 | +impl SyncFenceInfo { |
| 104 | + pub fn obj_name(&self) -> &CStr { |
| 105 | + // TODO: Switch to CStr::from_bytes_until_nul (with c_char -> u8 transmute) since MSRV 1.69 |
| 106 | + unsafe { CStr::from_ptr(self.0.obj_name.as_ptr()) } |
| 107 | + } |
| 108 | + |
| 109 | + pub fn driver_name(&self) -> &CStr { |
| 110 | + // TODO: Switch to CStr::from_bytes_until_nul (with c_char -> u8 transmute) since MSRV 1.69 |
| 111 | + unsafe { CStr::from_ptr(self.0.driver_name.as_ptr()) } |
| 112 | + } |
| 113 | + |
| 114 | + pub fn status(&self) -> i32 { |
| 115 | + self.0.status |
| 116 | + } |
| 117 | + |
| 118 | + pub fn flags(&self) -> u32 { |
| 119 | + self.0.flags |
| 120 | + } |
| 121 | + |
| 122 | + pub fn timestamp_ns(&self) -> u64 { |
| 123 | + self.0.timestamp_ns |
| 124 | + } |
| 125 | +} |
| 126 | + |
| 127 | +/// Merge two sync files. |
| 128 | +/// |
| 129 | +/// This produces a new sync file with the given name which has the union of the two original sync |
| 130 | +/// file's fences; redundant fences may be removed. |
| 131 | +/// |
| 132 | +/// If one of the input sync files is signaled or invalid, then this function may behave like |
| 133 | +/// `dup()`: the new file descriptor refers to the valid/unsignaled sync file with its original |
| 134 | +/// name, rather than a new sync file. |
| 135 | +pub fn sync_merge(name: &CStr, fd1: BorrowedFd<'_>, fd2: BorrowedFd<'_>) -> OwnedFd { |
| 136 | + unsafe { |
| 137 | + OwnedFd::from_raw_fd(ffi::sync_merge( |
| 138 | + name.as_ptr(), |
| 139 | + fd1.as_raw_fd(), |
| 140 | + fd2.as_raw_fd(), |
| 141 | + )) |
| 142 | + } |
| 143 | +} |
0 commit comments