use {AsRaw, Device, DeviceDestroyedError, Format};
#[cfg(feature = "drm-support")]
use drm::buffer::{Buffer as DrmBuffer, Id as DrmId, PixelFormat as DrmPixelFormat};
use std::error;
use std::fmt;
use std::io::{Error as IoError, Result as IoResult};
use std::marker::PhantomData;
use std::ops::{Deref, DerefMut};
use std::os::unix::io::{AsRawFd, RawFd};
use std::rc::Weak;
use std::ptr;
use std::slice;
pub struct BufferObject<T: 'static> {
ffi: *mut ::ffi::gbm_bo,
pub(crate) _device: Weak<*mut ::ffi::gbm_device>,
_userdata: PhantomData<T>,
}
bitflags! {
pub struct BufferObjectFlags: u32 {
const SCANOUT = ::ffi::gbm_bo_flags_GBM_BO_USE_SCANOUT as u32;
const CURSOR = ::ffi::gbm_bo_flags_GBM_BO_USE_CURSOR as u32;
const RENDERING = ::ffi::gbm_bo_flags_GBM_BO_USE_RENDERING as u32;
const WRITE = ::ffi::gbm_bo_flags_GBM_BO_USE_WRITE as u32;
const LINEAR = ::ffi::gbm_bo_flags_GBM_BO_USE_LINEAR as u32;
}
}
pub type BufferObjectHandle = ::ffi::gbm_bo_handle;
enum BORef<'a, T: 'static> {
Ref(&'a BufferObject<T>),
Mut(&'a mut BufferObject<T>),
}
pub struct MappedBufferObject<'a, T: 'static> {
bo: BORef<'a, T>,
addr: *mut ::libc::c_void,
buffer: &'a mut [u8],
stride: u32,
height: u32,
width: u32,
x: u32,
y: u32,
}
impl<'a, T: 'static> MappedBufferObject<'a, T> {
pub fn stride(&self) -> u32 {
self.stride
}
pub fn height(&self) -> u32 {
self.height
}
pub fn width(&self) -> u32 {
self.width
}
pub fn x(&self) -> u32 {
self.x
}
pub fn y(&self) -> u32 {
self.y
}
pub fn buffer(&'a self) -> &'a [u8] {
self.buffer
}
pub fn buffer_mut(&'a mut self) -> &'a mut [u8] {
self.buffer
}
}
impl<'a, T: 'static> Deref for MappedBufferObject<'a, T> {
type Target = BufferObject<T>;
fn deref(&self) -> &BufferObject<T> {
match &self.bo {
&BORef::Ref(bo) => bo,
&BORef::Mut(ref bo) => bo,
}
}
}
impl<'a, T: 'static> DerefMut for MappedBufferObject<'a, T> {
fn deref_mut(&mut self) -> &mut BufferObject<T> {
match &mut self.bo {
&mut BORef::Ref(_) => unreachable!(),
&mut BORef::Mut(ref mut bo) => bo,
}
}
}
impl<'a, T: 'static> Drop for MappedBufferObject<'a, T> {
fn drop(&mut self) {
let ffi = match &self.bo {
&BORef::Ref(bo) => bo.ffi,
&BORef::Mut(ref bo) => bo.ffi,
};
unsafe { ::ffi::gbm_bo_unmap(ffi, self.addr) }
}
}
unsafe extern "C" fn destroy<T: 'static>(_: *mut ::ffi::gbm_bo, ptr: *mut ::libc::c_void) {
let ptr = ptr as *mut T;
if !ptr.is_null() {
let _ = Box::from_raw(ptr);
}
}
impl<T: 'static> BufferObject<T> {
pub fn width(&self) -> Result<u32, DeviceDestroyedError> {
if self._device.upgrade().is_some() {
Ok(unsafe { ::ffi::gbm_bo_get_width(self.ffi) })
} else {
Err(DeviceDestroyedError)
}
}
pub fn height(&self) -> Result<u32, DeviceDestroyedError> {
if self._device.upgrade().is_some() {
Ok(unsafe { ::ffi::gbm_bo_get_height(self.ffi) })
} else {
Err(DeviceDestroyedError)
}
}
pub fn stride(&self) -> Result<u32, DeviceDestroyedError> {
if self._device.upgrade().is_some() {
Ok(unsafe { ::ffi::gbm_bo_get_stride(self.ffi) })
} else {
Err(DeviceDestroyedError)
}
}
pub fn format(&self) -> Result<Format, DeviceDestroyedError> {
if self._device.upgrade().is_some() {
Ok(Format::from_ffi(unsafe { ::ffi::gbm_bo_get_format(self.ffi) })
.expect("libgbm returned invalid buffer format"))
} else {
Err(DeviceDestroyedError)
}
}
pub fn handle(&self) -> Result<BufferObjectHandle, DeviceDestroyedError> {
if self._device.upgrade().is_some() {
Ok(unsafe { ::ffi::gbm_bo_get_handle(self.ffi) })
} else {
Err(DeviceDestroyedError)
}
}
pub fn map<'a, D, F, S>(&'a self, device: &Device<D>, x: u32, y: u32, width: u32, height: u32, f: F) -> Result<IoResult<S>, WrongDeviceError>
where
D: AsRawFd + 'static,
F: FnOnce(&MappedBufferObject<'a, T>) -> S,
{
if let Some(_device) = self._device.upgrade() {
if *_device != device.as_raw_mut() { return Err(WrongDeviceError);
}
} else { return Err(WrongDeviceError);
}
unsafe {
let mut buffer: *mut ::libc::c_void = ptr::null_mut();
let mut stride = 0;
let ptr = ::ffi::gbm_bo_map(
self.ffi,
x,
y,
width,
height,
::ffi::gbm_bo_transfer_flags::GBM_BO_TRANSFER_READ as u32,
&mut stride as *mut _,
&mut buffer as *mut _,
);
if ptr.is_null() {
Ok(Err(IoError::last_os_error()))
} else {
Ok(Ok(f(&MappedBufferObject {
bo: BORef::Ref(self),
addr: ptr,
buffer: slice::from_raw_parts_mut(
buffer as *mut _,
((height * stride + height * width) * 4) as usize,
),
stride,
height,
width,
x,
y,
})))
}
}
}
pub fn map_mut<'a, D, F, S>(
&'a mut self,
device: &Device<D>,
x: u32,
y: u32,
width: u32,
height: u32,
f: F,
) -> Result<IoResult<S>, WrongDeviceError>
where
D: AsRawFd + 'static,
F: FnOnce(&mut MappedBufferObject<'a, T>) -> S,
{
if let Some(_device) = self._device.upgrade() {
if *_device != device.as_raw_mut() { return Err(WrongDeviceError);
}
} else { return Err(WrongDeviceError);
}
unsafe {
let mut buffer: *mut ::libc::c_void = ptr::null_mut();
let mut stride = 0;
let ptr = ::ffi::gbm_bo_map(
self.ffi,
x,
y,
width,
height,
::ffi::gbm_bo_transfer_flags::GBM_BO_TRANSFER_READ_WRITE as u32,
&mut stride as *mut _,
&mut buffer as *mut _,
);
if ptr.is_null() {
Ok(Err(IoError::last_os_error()))
} else {
Ok(Ok(f(&mut MappedBufferObject {
bo: BORef::Mut(self),
addr: ptr,
buffer: slice::from_raw_parts_mut(
buffer as *mut _,
((height * stride + height * width) * 4) as usize,
),
stride,
height,
width,
x,
y,
})))
}
}
}
pub fn write(&mut self, buffer: &[u8]) -> Result<IoResult<()>, DeviceDestroyedError> {
if self._device.upgrade().is_some() {
let result = unsafe { ::ffi::gbm_bo_write(self.ffi, buffer.as_ptr() as *const _, buffer.len()) };
if result != 0 {
Ok(Err(IoError::last_os_error()))
} else {
Ok(Ok(()))
}
} else {
Err(DeviceDestroyedError)
}
}
pub fn set_userdata(&mut self, userdata: T) -> Result<Option<T>, DeviceDestroyedError> {
if self._device.upgrade().is_some() {
let old = self.take_userdata();
let boxed = Box::new(userdata);
unsafe {
::ffi::gbm_bo_set_user_data(self.ffi, Box::into_raw(boxed) as *mut _, Some(destroy::<T>));
}
old
} else {
Err(DeviceDestroyedError)
}
}
pub fn clear_userdata(&mut self) -> Result<(), DeviceDestroyedError> {
if self._device.upgrade().is_some() {
let _ = self.take_userdata();
Ok(())
} else {
Err(DeviceDestroyedError)
}
}
pub fn userdata(&self) -> Result<Option<&T>, DeviceDestroyedError> {
if self._device.upgrade().is_some() {
let raw = unsafe { ::ffi::gbm_bo_get_user_data(self.ffi) };
if raw.is_null() {
Ok(None)
} else {
unsafe { Ok(Some(&*(raw as *mut T))) }
}
} else {
Err(DeviceDestroyedError)
}
}
pub fn userdata_mut(&mut self) -> Result<Option<&mut T>, DeviceDestroyedError> {
if self._device.upgrade().is_some() {
let raw = unsafe { ::ffi::gbm_bo_get_user_data(self.ffi) };
if raw.is_null() {
Ok(None)
} else {
unsafe { Ok(Some(&mut *(raw as *mut T))) }
}
} else {
Err(DeviceDestroyedError)
}
}
pub fn take_userdata(&mut self) -> Result<Option<T>, DeviceDestroyedError> {
if self._device.upgrade().is_some() {
let raw = unsafe { ::ffi::gbm_bo_get_user_data(self.ffi) };
if raw.is_null() {
Ok(None)
} else {
unsafe {
let boxed = Box::from_raw(raw as *mut T);
::ffi::gbm_bo_set_user_data(self.ffi, ptr::null_mut(), None);
Ok(Some(*boxed))
}
}
} else {
Err(DeviceDestroyedError)
}
}
pub(crate) unsafe fn new(ffi: *mut ::ffi::gbm_bo, device: Weak<*mut ::ffi::gbm_device>) -> BufferObject<T> {
BufferObject {
ffi,
_device: device,
_userdata: PhantomData,
}
}
}
impl<T: 'static> AsRawFd for BufferObject<T> {
fn as_raw_fd(&self) -> RawFd {
unsafe { ::ffi::gbm_bo_get_fd(self.ffi) }
}
}
impl<T: 'static> AsRaw<::ffi::gbm_bo> for BufferObject<T> {
fn as_raw(&self) -> *const ::ffi::gbm_bo {
self.ffi
}
}
impl<T: 'static> Drop for BufferObject<T> {
fn drop(&mut self) {
if self._device.upgrade().is_some() {
unsafe { ::ffi::gbm_bo_destroy(self.ffi) }
}
}
}
#[cfg(feature = "drm-support")]
impl<T: 'static> DrmBuffer for BufferObject<T> {
fn size(&self) -> (u32, u32) {
(self.width().expect("GbmDevice does not exist anymore"), self.height().expect("GbmDevice does not exist anymore"))
}
fn format(&self) -> DrmPixelFormat {
DrmPixelFormat::from_raw(self.format().expect("GbmDevice does not exist anymore").as_ffi()).unwrap()
}
fn pitch(&self) -> u32 {
self.stride().expect("GbmDevice does not exist anymore")
}
fn handle(&self) -> DrmId {
unsafe { DrmId::from_raw(*self.handle().expect("GbmDevice does not exist anymore").u32.as_ref()) }
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct WrongDeviceError;
impl fmt::Display for WrongDeviceError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
use std::error::Error;
write!(f, "{}", self.description())
}
}
impl error::Error for WrongDeviceError {
fn description(&self) -> &str {
"The gbm specified is not the one this buffer object belongs to"
}
fn cause(&self) -> Option<&error::Error> { None }
}