use crate::sys as ffi;
#[derive(Debug, Clone, Copy)]
pub struct FrameBufferPlane {
pub fd: i32,
pub offset: u32,
pub length: u32,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum FrameStatus {
Success,
Error,
Cancelled,
Startup,
}
#[derive(Debug, Clone, Copy)]
pub struct FrameMetadata {
pub status: FrameStatus,
pub sequence: u32,
pub timestamp: u64,
}
pub struct FrameBuffer {
pub(crate) ptr: *mut ffi::lc_frame_buffer_t,
}
unsafe impl Send for FrameBuffer {}
unsafe impl Sync for FrameBuffer {}
impl FrameBuffer {
pub(crate) fn from_raw(ptr: *mut ffi::lc_frame_buffer_t) -> Self {
Self { ptr }
}
pub fn planes_count(&self) -> usize {
unsafe { ffi::lc_frame_buffer_planes_count(self.ptr) }
}
pub fn plane(&self, index: usize) -> Option<FrameBufferPlane> {
if index >= self.planes_count() {
return None;
}
let raw = unsafe { ffi::lc_frame_buffer_get_plane(self.ptr, index) };
if raw.fd < 0 {
return None;
}
Some(FrameBufferPlane {
fd: raw.fd,
offset: raw.offset,
length: raw.length,
})
}
pub fn metadata(&self) -> FrameMetadata {
let raw = unsafe { ffi::lc_frame_buffer_metadata(self.ptr) };
let status = match raw.status {
0 => FrameStatus::Success,
1 => FrameStatus::Error,
2 => FrameStatus::Cancelled,
_ => FrameStatus::Startup,
};
FrameMetadata {
status,
sequence: raw.sequence,
timestamp: raw.timestamp,
}
}
}
impl Drop for FrameBuffer {
fn drop(&mut self) {
unsafe { ffi::lc_frame_buffer_ref_destroy(self.ptr) };
}
}
pub struct FrameBufferRef<'a> {
pub(crate) ptr: *mut ffi::lc_frame_buffer_t,
pub(crate) _phantom: std::marker::PhantomData<&'a ()>,
}
impl FrameBufferRef<'_> {
pub fn planes_count(&self) -> usize {
unsafe { ffi::lc_frame_buffer_planes_count(self.ptr) }
}
pub fn plane(&self, index: usize) -> Option<FrameBufferPlane> {
if index >= self.planes_count() {
return None;
}
let raw = unsafe { ffi::lc_frame_buffer_get_plane(self.ptr, index) };
if raw.fd < 0 {
return None;
}
Some(FrameBufferPlane {
fd: raw.fd,
offset: raw.offset,
length: raw.length,
})
}
pub fn metadata(&self) -> FrameMetadata {
let raw = unsafe { ffi::lc_frame_buffer_metadata(self.ptr) };
let status = match raw.status {
0 => FrameStatus::Success,
1 => FrameStatus::Error,
2 => FrameStatus::Cancelled,
_ => FrameStatus::Startup,
};
FrameMetadata {
status,
sequence: raw.sequence,
timestamp: raw.timestamp,
}
}
}