#![crate_name = "gbm-rs"]
#![crate_type = "lib"]
#![feature(libc)]
#![feature(std_misc)]
extern crate libc;
use std::os::unix::prelude::*;
use libc::{
c_int,
c_void,
uint32_t,
uint64_t,
size_t,
};
pub struct Device {
ptr: *const gbm_device
}
impl Device {
pub fn from_fd(fd: Fd) -> Option<Device> {
unsafe {
let dev = gbm_create_device(fd);
if dev.is_null() {
return None;
}
return Some(Device { ptr: dev });
}
}
pub fn is_format_supported(&self, format: u32, usage: u32) -> bool {
unsafe { gbm_device_is_format_supported(self.ptr, format, usage) != 0 }
}
pub fn fd(&self) -> Fd {
unsafe { gbm_device_get_fd(self.ptr) }
}
}
impl Drop for Device {
fn drop(&mut self) {
unsafe { gbm_device_destroy(self.ptr) }
}
}
pub struct Surface {
ptr: *const gbm_surface
}
impl Surface {
pub fn new(dev: &Device, width: u32, height: u32,
format: u32, flags: u32) -> Option<Surface> {
unsafe {
let surf = gbm_surface_create(dev.ptr, width, height,
format, flags);
if surf.is_null() {
return None;
}
return Some(Surface { ptr: surf });
}
}
pub fn has_free_buffers(&self) -> bool {
unsafe { gbm_surface_has_free_buffers(self.ptr) != 0 }
}
pub fn lock_front_buffer(&self) -> Option<BufferObject> {
unsafe {
let bo = gbm_surface_lock_front_buffer(self.ptr);
if bo.is_null() {
return None;
}
return Some(BufferObject { ptr: bo, manual: false });
}
}
pub fn release_buffer(&self, bo: BufferObject) {
unsafe { gbm_surface_release_buffer(self.ptr, bo.ptr) }
}
}
impl Drop for Surface {
fn drop(&mut self) {
unsafe { gbm_surface_destroy(self.ptr) }
}
}
pub struct BufferObject {
ptr: *const gbm_bo,
manual: bool,
}
impl BufferObject {
pub fn new(dev: &Device, width: u32, height: u32,
format: u32, flags: u32) -> Option<BufferObject> {
unsafe {
let bo = gbm_bo_create(dev.ptr, width, height,
format, flags);
if bo.is_null() {
return None;
}
return Some(BufferObject { ptr: bo, manual: true });
}
}
pub fn width(&self) -> u32 {
unsafe { gbm_bo_get_width(self.ptr) }
}
pub fn height(&self) -> u32 {
unsafe { gbm_bo_get_height(self.ptr) }
}
pub fn stride(&self) -> u32 {
unsafe { gbm_bo_get_stride(self.ptr) }
}
pub fn format(&self) -> u32 {
unsafe { gbm_bo_get_format(self.ptr) }
}
pub fn device(&self) -> Device {
unsafe { Device { ptr: gbm_bo_get_device(self.ptr) } }
}
pub fn handle_u32(&self) -> u32 {
unsafe { gbm_bo_get_handle(self.ptr) as u32 }
}
pub fn handle_u64(&self) -> u64 {
unsafe { gbm_bo_get_handle(self.ptr) }
}
pub fn handle_i32(&self) -> i32 {
unsafe { gbm_bo_get_handle(self.ptr) as i32 }
}
pub fn handle_i64(&self) -> i64 {
unsafe { gbm_bo_get_handle(self.ptr) as i64 }
}
pub fn handle_ptr(&self) -> *const c_void {
unsafe { gbm_bo_get_handle(self.ptr) as *const c_void }
}
pub fn fd(&self) -> Fd {
unsafe { gbm_bo_get_fd(self.ptr) }
}
pub fn write<T>(&self, buf: *const T, count: usize) -> bool {
unsafe { gbm_bo_write(self.ptr, buf as *const c_void, count as u64) == 0 }
}
}
impl Drop for BufferObject {
fn drop(&mut self) {
unsafe { if self.manual { gbm_bo_destroy(self.ptr) } }
}
}
pub const USE_SCANOUT: u32 = (1 << 0);
pub const USE_CURSOR: u32 = (1 << 1);
pub const USE_RENDERING: u32 = (1 << 2);
pub const USE_WRITE: u32 = (1 << 3);
#[repr(C)]
struct gbm_device;
#[repr(C)]
struct gbm_bo;
#[repr(C)]
struct gbm_surface;
#[link(name = "gbm")]
extern {
fn gbm_device_get_fd(gbm: *const gbm_device) -> c_int;
fn gbm_device_is_format_supported(gbm: *const gbm_device,
format: uint32_t, usage: uint32_t) -> c_int;
fn gbm_device_destroy(gbm: *const gbm_device);
fn gbm_create_device(fd: c_int) -> *const gbm_device;
fn gbm_bo_create(gbm: *const gbm_device,
width: uint32_t, height:
uint32_t, format: uint32_t, flags: uint32_t) -> *const gbm_bo;
fn gbm_bo_get_width(bo: *const gbm_bo) -> uint32_t;
fn gbm_bo_get_height(bo: *const gbm_bo) -> uint32_t;
fn gbm_bo_get_stride(bo: *const gbm_bo) -> uint32_t;
fn gbm_bo_get_format(bo: *const gbm_bo) -> uint32_t;
fn gbm_bo_get_device(bo: *const gbm_bo) -> *const gbm_device;
fn gbm_bo_get_handle(bo: *const gbm_bo) -> uint64_t;
fn gbm_bo_get_fd(bo: *const gbm_bo) -> c_int;
fn gbm_bo_write(bo: *const gbm_bo, buf: *const c_void, count: size_t) -> c_int;
fn gbm_bo_destroy(bo: *const gbm_bo);
fn gbm_surface_create(gbm: *const gbm_device,
width: uint32_t, height: uint32_t,
format: uint32_t, flags: uint32_t) -> *const gbm_surface;
fn gbm_surface_lock_front_buffer(surface: *const gbm_surface) -> *const gbm_bo;
fn gbm_surface_release_buffer(surface: *const gbm_surface, bo: *const gbm_bo);
fn gbm_surface_has_free_buffers(surface: *const gbm_surface) -> c_int;
fn gbm_surface_destroy(surface: *const gbm_surface);
}