|
| 1 | +// SPDX-License-Identifier: GPL-2.0 OR MIT |
| 2 | + |
| 3 | +//! DRM Sync Objects |
| 4 | +//! |
| 5 | +//! C header: [`include/linux/drm/drm_syncobj.h`](../../../../include/linux/drm/drm_syncobj.h) |
| 6 | +
|
| 7 | +use crate::{bindings, dma_fence::*, drm, error::Result, prelude::*}; |
| 8 | + |
| 9 | +/// A DRM Sync Object |
| 10 | +/// |
| 11 | +/// # Invariants |
| 12 | +/// ptr is a valid pointer to a drm_syncobj and we own a reference to it. |
| 13 | +pub struct SyncObj { |
| 14 | + ptr: *mut bindings::drm_syncobj, |
| 15 | +} |
| 16 | + |
| 17 | +impl SyncObj { |
| 18 | + /// Looks up a sync object by its handle for a given `File`. |
| 19 | + pub fn lookup_handle(file: &impl drm::file::GenericFile, handle: u32) -> Result<SyncObj> { |
| 20 | + // SAFETY: The arguments are all valid per the type invariants. |
| 21 | + let ptr = unsafe { bindings::drm_syncobj_find(file.raw() as *mut _, handle) }; |
| 22 | + |
| 23 | + if ptr.is_null() { |
| 24 | + Err(ENOENT) |
| 25 | + } else { |
| 26 | + Ok(SyncObj { ptr }) |
| 27 | + } |
| 28 | + } |
| 29 | + |
| 30 | + /// Returns the DMA fence associated with this sync object, if any. |
| 31 | + pub fn fence_get(&self) -> Option<Fence> { |
| 32 | + let fence = unsafe { bindings::drm_syncobj_fence_get(self.ptr) }; |
| 33 | + if fence.is_null() { |
| 34 | + None |
| 35 | + } else { |
| 36 | + // SAFETY: The pointer is non-NULL and drm_syncobj_fence_get acquired an |
| 37 | + // additional reference. |
| 38 | + Some(unsafe { Fence::from_raw(fence) }) |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + /// Replaces the DMA fence with a new one, or removes it if fence is None. |
| 43 | + pub fn replace_fence(&self, fence: Option<&Fence>) { |
| 44 | + unsafe { |
| 45 | + bindings::drm_syncobj_replace_fence( |
| 46 | + self.ptr, |
| 47 | + fence.map_or(core::ptr::null_mut(), |a| a.raw()), |
| 48 | + ) |
| 49 | + }; |
| 50 | + } |
| 51 | + |
| 52 | + /// Adds a new timeline point to the syncobj. |
| 53 | + pub fn add_point(&self, chain: FenceChain, fence: &Fence, point: u64) { |
| 54 | + // SAFETY: All arguments should be valid per the respective type invariants. |
| 55 | + // This takes over the FenceChain ownership. |
| 56 | + unsafe { bindings::drm_syncobj_add_point(self.ptr, chain.into_raw(), fence.raw(), point) }; |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +impl Drop for SyncObj { |
| 61 | + fn drop(&mut self) { |
| 62 | + // SAFETY: We own a reference to this syncobj. |
| 63 | + unsafe { bindings::drm_syncobj_put(self.ptr) }; |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +impl Clone for SyncObj { |
| 68 | + fn clone(&self) -> Self { |
| 69 | + // SAFETY: `ptr` is valid per the type invariant and we own a reference to it. |
| 70 | + unsafe { bindings::drm_syncobj_get(self.ptr) }; |
| 71 | + SyncObj { ptr: self.ptr } |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +// SAFETY: drm_syncobj operations are internally locked. |
| 76 | +unsafe impl Sync for SyncObj {} |
| 77 | +unsafe impl Send for SyncObj {} |
0 commit comments