Skip to content

Commit

Permalink
bindings: add conversions between v4l2_buffer and v4l2_plane's memory…
Browse files Browse the repository at this point in the history
… backing information

Also add a few tests of dubious usefulness.
  • Loading branch information
Gnurou committed May 7, 2024
1 parent 1e50a11 commit d6e41b0
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 33 deletions.
17 changes: 1 addition & 16 deletions lib/src/ioctl/qbuf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ use crate::ioctl::QueryBuf;
use crate::ioctl::V4l2Buffer;
use crate::ioctl::V4l2BufferPlanes;
use crate::memory::Memory;
use crate::memory::MemoryType;
use crate::memory::PlaneHandle;
use crate::QueueType;

Expand Down Expand Up @@ -181,21 +180,7 @@ impl<H: PlaneHandle, Q: QueryBuf> QBuf<Q> for QBuffer<H> {

v4l2_buf.length = plane.0.length;
v4l2_buf.bytesused = plane.0.bytesused;
v4l2_buf.m = match H::Memory::MEMORY_TYPE {
MemoryType::Mmap => bindings::v4l2_buffer__bindgen_ty_1 {
// Safe because the buffer type is determined to be MMAP.
offset: unsafe { plane.0.m.mem_offset },
},
MemoryType::UserPtr => bindings::v4l2_buffer__bindgen_ty_1 {
// Safe because the buffer type is determined to be USERPTR.
userptr: unsafe { plane.0.m.userptr },
},
MemoryType::DmaBuf => bindings::v4l2_buffer__bindgen_ty_1 {
// Safe because the buffer type is determined to be DMABUF.
fd: unsafe { plane.0.m.fd },
},
_ => Default::default(),
};
v4l2_buf.m = (&plane.0.m, H::Memory::MEMORY_TYPE).into();

Ok(())
}
Expand Down
18 changes: 2 additions & 16 deletions lib/src/ioctl/querybuf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ impl QueryBuf for V4l2Buffer {
) -> Result<Self, Self::Error> {
QueueType::n(v4l2_buf.type_)
.ok_or(V4l2BufferFromError::UnknownQueueType(v4l2_buf.type_))?;
let mem = match MemoryType::n(v4l2_buf.memory) {
let memory = match MemoryType::n(v4l2_buf.memory) {
Some(m) => m,
None => return Err(V4l2BufferFromError::UnknownMemoryType(v4l2_buf.memory)),
};
Expand All @@ -72,21 +72,7 @@ impl QueryBuf for V4l2Buffer {
bytesused: v4l2_buf.bytesused,
length: v4l2_buf.length,
data_offset: 0,
m: match mem {
MemoryType::Mmap => bindings::v4l2_plane__bindgen_ty_1 {
// Safe because the buffer's memory type is MMAP.
mem_offset: unsafe { v4l2_buf.m.offset },
},
MemoryType::UserPtr => bindings::v4l2_plane__bindgen_ty_1 {
// Safe because the buffer's memory type is USERPTR.
userptr: unsafe { v4l2_buf.m.userptr },
},
MemoryType::DmaBuf => bindings::v4l2_plane__bindgen_ty_1 {
// Safe because the buffer's memory type is DMABUF.
fd: unsafe { v4l2_buf.m.fd },
},
_ => Default::default(),
},
m: (&v4l2_buf.m, memory).into(),
reserved: Default::default(),
};

Expand Down
101 changes: 100 additions & 1 deletion lib/src/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ pub use mmap::*;
pub use userptr::*;

use crate::{
bindings,
bindings::{self, v4l2_buffer__bindgen_ty_1, v4l2_plane__bindgen_ty_1},
ioctl::{PlaneMapping, QueryBufPlane},
};
use enumn::N;
Expand Down Expand Up @@ -127,3 +127,102 @@ impl<P: PlaneHandle> PrimitiveBufferHandles for Vec<P> {
type HandleType = P;
const MEMORY_TYPE: Self::SupportedMemoryType = P::Memory::MEMORY_TYPE;
}

/// Conversion from `v4l2_buffer`'s backing information to `v4l2_plane`'s.
impl From<(&v4l2_buffer__bindgen_ty_1, MemoryType)> for v4l2_plane__bindgen_ty_1 {
fn from((m, memory): (&v4l2_buffer__bindgen_ty_1, MemoryType)) -> Self {
match memory {
MemoryType::Mmap => v4l2_plane__bindgen_ty_1 {
// Safe because the buffer type is determined to be MMAP.
mem_offset: unsafe { m.offset },
},
MemoryType::UserPtr => v4l2_plane__bindgen_ty_1 {
// Safe because the buffer type is determined to be USERPTR.
userptr: unsafe { m.userptr },
},
MemoryType::DmaBuf => v4l2_plane__bindgen_ty_1 {
// Safe because the buffer type is determined to be DMABUF.
fd: unsafe { m.fd },
},
MemoryType::Overlay => Default::default(),
}
}
}

/// Conversion from `v4l2_plane`'s backing information to `v4l2_buffer`'s.
impl From<(&v4l2_plane__bindgen_ty_1, MemoryType)> for v4l2_buffer__bindgen_ty_1 {
fn from((m, memory): (&v4l2_plane__bindgen_ty_1, MemoryType)) -> Self {
match memory {
MemoryType::Mmap => v4l2_buffer__bindgen_ty_1 {
// Safe because the buffer type is determined to be MMAP.
offset: unsafe { m.mem_offset },
},
MemoryType::UserPtr => v4l2_buffer__bindgen_ty_1 {
// Safe because the buffer type is determined to be USERPTR.
userptr: unsafe { m.userptr },
},
MemoryType::DmaBuf => v4l2_buffer__bindgen_ty_1 {
// Safe because the buffer type is determined to be DMABUF.
fd: unsafe { m.fd },
},
MemoryType::Overlay => Default::default(),
}
}
}

#[cfg(test)]
mod tests {
use crate::bindings::v4l2_buffer__bindgen_ty_1;
use crate::bindings::v4l2_plane__bindgen_ty_1;
use crate::memory::MemoryType;

#[test]
// Purpose of this test is dubious as the members are overlapping anyway.
fn plane_m_to_buffer_m() {
let plane_m = v4l2_plane__bindgen_ty_1 {
mem_offset: 0xfeedc0fe,
};
assert_eq!(
unsafe { v4l2_buffer__bindgen_ty_1::from((&plane_m, MemoryType::Mmap)).offset },
0xfeedc0fe
);

let plane_m = v4l2_plane__bindgen_ty_1 {
userptr: 0xfeedc0fe,
};
assert_eq!(
unsafe { v4l2_buffer__bindgen_ty_1::from((&plane_m, MemoryType::UserPtr)).userptr },
0xfeedc0fe
);

let plane_m = v4l2_plane__bindgen_ty_1 { fd: 0x76543210 };
assert_eq!(
unsafe { v4l2_buffer__bindgen_ty_1::from((&plane_m, MemoryType::DmaBuf)).fd },
0x76543210
);
}

#[test]
// Purpose of this test is dubious as the members are overlapping anyway.
fn buffer_m_to_plane_m() {
let buffer_m = v4l2_buffer__bindgen_ty_1 { offset: 0xfeedc0fe };
assert_eq!(
unsafe { v4l2_plane__bindgen_ty_1::from((&buffer_m, MemoryType::Mmap)).mem_offset },
0xfeedc0fe
);

let buffer_m = v4l2_buffer__bindgen_ty_1 {
userptr: 0xfeedc0fe,
};
assert_eq!(
unsafe { v4l2_plane__bindgen_ty_1::from((&buffer_m, MemoryType::UserPtr)).userptr },
0xfeedc0fe
);

let buffer_m = v4l2_buffer__bindgen_ty_1 { fd: 0x76543210 };
assert_eq!(
unsafe { v4l2_plane__bindgen_ty_1::from((&buffer_m, MemoryType::DmaBuf)).fd },
0x76543210
);
}
}

0 comments on commit d6e41b0

Please sign in to comment.