use std;
use std::os::unix::io::RawFd;
pub const MAX_PLANES: usize = 3;
#[derive(PartialEq)]
pub enum ValidationResult {
PlaneIdx,
PlaneSet,
Incomplete,
InvalidFormat,
InvalidDimensions,
OutOfBounds,
Ok,
}
pub type RawHwImage = *const std::os::raw::c_void;
#[derive(Debug, Clone)]
pub struct EglAttributes {
pub name: u32,
pub width: i32,
pub height: i32,
pub stride: u32,
pub format: u32,
}
impl EglAttributes {
pub fn new(name: u32, width: i32, height: i32, stride: u32, format: u32) -> Self {
EglAttributes {
name: name,
width: width,
height: height,
stride: stride,
format: format,
}
}
}
#[derive(Debug, Copy, Clone)]
pub struct DmabufPlane {
pub fd: RawFd,
pub offset: u32,
pub stride: u32,
pub modifier_hi: u32,
pub modifier_lo: u32,
}
impl DmabufPlane {
fn new(fd: RawFd, offset: u32, stride: u32, modifier_hi: u32, modifier_lo: u32) -> Self {
DmabufPlane {
fd: fd,
offset: offset,
stride: stride,
modifier_hi: modifier_hi,
modifier_lo: modifier_lo,
}
}
fn default() -> Self {
DmabufPlane {
fd: -1,
offset: 0,
stride: 0,
modifier_hi: 0x0,
modifier_lo: 0x0,
}
}
}
#[derive(Debug, Clone)]
pub struct DmabufAttributes {
pub width: i32,
pub height: i32,
pub format: u32,
pub flags: u32,
pub num_planes: usize,
pub planes: [DmabufPlane; MAX_PLANES],
}
impl DmabufAttributes {
pub fn new() -> Self {
DmabufAttributes {
width: 0,
height: 0,
format: 0,
flags: 0x0,
num_planes: 0,
planes: [DmabufPlane::default(), DmabufPlane::default(), DmabufPlane::default()],
}
}
pub fn add(&mut self,
plane_idx: usize,
fd: RawFd,
offset: u32,
stride: u32,
modifier_hi: u32,
modifier_lo: u32)
-> ValidationResult {
if plane_idx >= MAX_PLANES {
return ValidationResult::PlaneIdx;
}
if self.planes[plane_idx].fd != -1 {
return ValidationResult::PlaneSet;
}
self.planes[plane_idx] = DmabufPlane::new(fd, offset, stride, modifier_hi, modifier_lo);
self.num_planes += 1;
ValidationResult::Ok
}
pub fn create(&mut self, width: i32, height: i32, format: u32, flags: u32) {
self.width = width;
self.height = height;
self.format = format;
self.flags = flags;
}
pub fn validate(&self) -> ValidationResult {
for i in 0..self.num_planes {
if self.planes[i].fd == -1 {
return ValidationResult::Incomplete;
}
}
ValidationResult::Ok
}
pub fn get_num_of_planes(&self) -> usize {
self.num_planes
}
}