Skip to content

Commit 13c68b3

Browse files
committed
syncobj: Fix #224
1 parent 2ab388f commit 13c68b3

File tree

3 files changed

+85
-34
lines changed

3 files changed

+85
-34
lines changed

drm-ffi/src/syncobj.rs

Lines changed: 63 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@ use drm_sys::*;
77

88
use std::{
99
io,
10-
os::unix::io::{AsRawFd, BorrowedFd},
10+
os::{
11+
fd::{FromRawFd, OwnedFd},
12+
unix::io::{AsRawFd, BorrowedFd},
13+
},
1114
};
1215

1316
/// Creates a syncobj.
@@ -39,54 +42,91 @@ pub fn destroy(fd: BorrowedFd<'_>, handle: u32) -> io::Result<drm_syncobj_destro
3942
Ok(args)
4043
}
4144

42-
/// Exports a syncobj as an inter-process file descriptor or as a poll()-able sync file.
43-
pub fn handle_to_fd(
45+
/// Exports a syncobj as an inter-process file descriptor.
46+
pub fn handle_to_fd(fd: BorrowedFd<'_>, handle: u32) -> io::Result<OwnedFd> {
47+
let mut args = drm_syncobj_handle {
48+
handle,
49+
flags: 0,
50+
fd: 0,
51+
pad: 0,
52+
point: 0,
53+
};
54+
55+
unsafe {
56+
ioctl::syncobj::handle_to_fd(fd, &mut args)?;
57+
}
58+
59+
Ok(unsafe { OwnedFd::from_raw_fd(args.fd) })
60+
}
61+
62+
/// Exports a syncobj as a poll-able sync file optionally for a specified timeline point.
63+
pub fn handle_to_sync_file(
4464
fd: BorrowedFd<'_>,
4565
handle: u32,
46-
export_sync_file: bool,
47-
) -> io::Result<drm_syncobj_handle> {
66+
timeline_point: Option<u64>,
67+
) -> io::Result<OwnedFd> {
4868
let mut args = drm_syncobj_handle {
4969
handle,
50-
flags: if export_sync_file {
51-
DRM_SYNCOBJ_HANDLE_TO_FD_FLAGS_EXPORT_SYNC_FILE
52-
} else {
53-
0
54-
},
70+
flags: DRM_SYNCOBJ_HANDLE_TO_FD_FLAGS_EXPORT_SYNC_FILE
71+
| if timeline_point.is_some() {
72+
DRM_SYNCOBJ_HANDLE_TO_FD_FLAGS_TIMELINE
73+
} else {
74+
0
75+
},
5576
fd: 0,
5677
pad: 0,
57-
point: 0, // TODO: Add support for TIMELINE sync files
78+
point: timeline_point.unwrap_or(0),
5879
};
5980

6081
unsafe {
6182
ioctl::syncobj::handle_to_fd(fd, &mut args)?;
6283
}
6384

64-
Ok(args)
85+
Ok(unsafe { OwnedFd::from_raw_fd(args.fd) })
6586
}
6687

6788
/// Imports a file descriptor exported by [`handle_to_fd`] back into a process-local handle.
68-
pub fn fd_to_handle(
89+
pub fn fd_to_handle(fd: BorrowedFd<'_>, opaque_fd: BorrowedFd<'_>) -> io::Result<u32> {
90+
let mut args = drm_syncobj_handle {
91+
handle: 0,
92+
flags: 0,
93+
fd: opaque_fd.as_raw_fd(),
94+
pad: 0,
95+
point: 0,
96+
};
97+
98+
unsafe {
99+
ioctl::syncobj::fd_to_handle(fd, &mut args)?;
100+
}
101+
102+
Ok(args.handle)
103+
}
104+
105+
/// Imports a sync file exported by [`handle_to_sync_file`] into an existing process-local handle optionally for a specified timeline point.
106+
pub fn sync_file_into_handle(
69107
fd: BorrowedFd<'_>,
70108
syncobj_fd: BorrowedFd<'_>,
71-
import_sync_file: bool,
72-
) -> io::Result<drm_syncobj_handle> {
109+
handle: u32,
110+
timeline_point: Option<u64>,
111+
) -> io::Result<()> {
73112
let mut args = drm_syncobj_handle {
74-
handle: 0,
75-
flags: if import_sync_file {
76-
DRM_SYNCOBJ_FD_TO_HANDLE_FLAGS_IMPORT_SYNC_FILE
77-
} else {
78-
0
79-
},
113+
handle,
114+
flags: DRM_SYNCOBJ_FD_TO_HANDLE_FLAGS_IMPORT_SYNC_FILE
115+
| if timeline_point.is_some() {
116+
DRM_SYNCOBJ_FD_TO_HANDLE_FLAGS_TIMELINE
117+
} else {
118+
0
119+
},
80120
fd: syncobj_fd.as_raw_fd(),
81121
pad: 0,
82-
point: 0, // TODO: Add support for TIMELINE sync files
122+
point: timeline_point.unwrap_or(0),
83123
};
84124

85125
unsafe {
86126
ioctl::syncobj::fd_to_handle(fd, &mut args)?;
87127
}
88128

89-
Ok(args)
129+
Ok(())
90130
}
91131

92132
/// Waits for one or more syncobjs to become signalled.

examples/syncobj.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ impl Card {
2424
self.syncobj_signal(&[syncobj])?;
2525

2626
// Export fence set by previous ioctl to file descriptor.
27-
self.syncobj_to_fd(syncobj, true)
27+
self.syncobj_to_sync_file(syncobj, None)
2828
};
2929

3030
// The sync file descriptor constitutes ownership of the fence, so the syncobj can be

src/control/mod.rs

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -838,24 +838,35 @@ pub trait Device: super::Device {
838838
Ok(())
839839
}
840840

841-
/// Exports a syncobj as an inter-process file descriptor or as a poll()-able sync file.
842-
fn syncobj_to_fd(
841+
/// Exports a syncobj as an inter-process file descriptor.
842+
fn syncobj_to_fd(&self, handle: syncobj::Handle) -> io::Result<OwnedFd> {
843+
ffi::syncobj::handle_to_fd(self.as_fd(), handle.into())
844+
}
845+
846+
/// Exports a syncobj as a poll-able sync file optionally for a specified timeline point.
847+
fn syncobj_to_sync_file(
843848
&self,
844849
handle: syncobj::Handle,
845-
export_sync_file: bool,
850+
timeline_point: Option<u64>,
846851
) -> io::Result<OwnedFd> {
847-
let info = ffi::syncobj::handle_to_fd(self.as_fd(), handle.into(), export_sync_file)?;
848-
Ok(unsafe { OwnedFd::from_raw_fd(info.fd) })
852+
ffi::syncobj::handle_to_sync_file(self.as_fd(), handle.into(), timeline_point)
849853
}
850854

851855
/// Imports a file descriptor exported by [`Self::syncobj_to_fd`] back into a process-local handle.
852-
fn fd_to_syncobj(
856+
fn fd_to_syncobj(&self, fd: BorrowedFd<'_>) -> io::Result<syncobj::Handle> {
857+
let handle = ffi::syncobj::fd_to_handle(self.as_fd(), fd)?;
858+
Ok(from_u32(handle).unwrap())
859+
}
860+
861+
/// Imports a sync file exported by [`Self::syncobj_to_sync_file`] into an existing process-local handle
862+
/// optionally for a specified timeline point.
863+
fn sync_file_into_syncobj(
853864
&self,
854865
fd: BorrowedFd<'_>,
855-
import_sync_file: bool,
856-
) -> io::Result<syncobj::Handle> {
857-
let info = ffi::syncobj::fd_to_handle(self.as_fd(), fd, import_sync_file)?;
858-
Ok(from_u32(info.handle).unwrap())
866+
handle: syncobj::Handle,
867+
timeline_point: Option<u64>,
868+
) -> io::Result<()> {
869+
ffi::syncobj::sync_file_into_handle(self.as_fd(), fd, handle.into(), timeline_point)
859870
}
860871

861872
/// Waits for one or more syncobjs to become signalled.

0 commit comments

Comments
 (0)