winit-sonoma-fix/src/api/osmesa/mod.rs

96 lines
2.5 KiB
Rust
Raw Normal View History

#![cfg(all(any(target_os = "linux", target_os = "freebsd"), feature="headless"))]
extern crate osmesa_sys;
2015-04-30 21:23:37 +10:00
use Api;
use BuilderAttribs;
2014-11-19 16:09:54 +11:00
use CreationError;
use CreationError::OsError;
2015-04-30 21:23:37 +10:00
use GlContext;
use PixelFormat;
2014-10-05 04:17:02 +11:00
use libc;
use std::{mem, ptr};
use std::ffi::CString;
2014-10-05 04:17:02 +11:00
pub struct OsMesaContext {
context: osmesa_sys::OSMesaContext,
2014-10-05 04:17:02 +11:00
buffer: Vec<u32>,
width: u32,
height: u32,
2014-10-05 04:17:02 +11:00
}
impl OsMesaContext {
pub fn new(builder: BuilderAttribs) -> Result<OsMesaContext, CreationError> {
let dimensions = builder.dimensions.unwrap();
Ok(OsMesaContext {
width: dimensions.0,
height: dimensions.1,
buffer: ::std::iter::repeat(unsafe { mem::uninitialized() })
.take((dimensions.0 * dimensions.1) as usize).collect(),
2014-10-05 04:17:02 +11:00
context: unsafe {
let ctxt = osmesa_sys::OSMesaCreateContext(0x1908, ptr::null_mut());
2014-11-15 01:59:45 +11:00
if ctxt.is_null() {
return Err(OsError("OSMesaCreateContext failed".to_string()));
}
ctxt
2014-10-05 04:17:02 +11:00
}
})
}
2015-04-27 00:23:22 +10:00
pub fn get_framebuffer(&self) -> &[u32] {
&self.buffer
}
pub fn get_dimensions(&self) -> (u32, u32) {
(self.width, self.height)
}
2015-04-30 21:23:37 +10:00
// TODO: can we remove this without causing havoc?
pub fn set_window_resize_callback(&mut self, _: Option<fn(u32, u32)>) {
}
}
impl GlContext for OsMesaContext {
unsafe fn make_current(&self) {
let ret = osmesa_sys::OSMesaMakeCurrent(self.context,
2014-10-05 04:17:02 +11:00
self.buffer.as_ptr() as *mut libc::c_void,
0x1401, self.width as libc::c_int, self.height as libc::c_int);
2014-11-15 01:59:45 +11:00
if ret == 0 {
panic!("OSMesaMakeCurrent failed")
}
2014-10-05 04:17:02 +11:00
}
2015-04-30 21:23:37 +10:00
fn is_current(&self) -> bool {
unsafe { osmesa_sys::OSMesaGetCurrentContext() == self.context }
2015-03-04 17:38:55 +11:00
}
2015-04-30 21:23:37 +10:00
fn get_proc_address(&self, addr: &str) -> *const libc::c_void {
2014-10-05 04:17:02 +11:00
unsafe {
2015-04-03 07:04:17 +11:00
let c_str = CString::new(addr.as_bytes().to_vec()).unwrap();
mem::transmute(osmesa_sys::OSMesaGetProcAddress(mem::transmute(c_str.as_ptr())))
2014-10-05 04:17:02 +11:00
}
}
2014-11-19 03:55:26 +11:00
2015-04-30 21:23:37 +10:00
fn swap_buffers(&self) {
2014-11-19 03:55:26 +11:00
}
2015-04-30 21:23:37 +10:00
fn get_api(&self) -> Api {
Api::OpenGl
}
fn get_pixel_format(&self) -> PixelFormat {
unimplemented!();
}
2014-10-05 04:17:02 +11:00
}
impl Drop for OsMesaContext {
2014-10-05 04:17:02 +11:00
fn drop(&mut self) {
unsafe { osmesa_sys::OSMesaDestroyContext(self.context) }
2014-10-05 04:17:02 +11:00
}
}
2014-12-30 08:56:15 +11:00
unsafe impl Send for OsMesaContext {}
unsafe impl Sync for OsMesaContext {}