winit-sonoma-fix/src/x11/headless.rs

75 lines
2 KiB
Rust
Raw Normal View History

use BuilderAttribs;
2014-11-19 16:09:54 +11:00
use CreationError;
use CreationError::OsError;
2014-10-05 04:17:02 +11:00
use libc;
use std::{mem, ptr};
use super::ffi;
2015-01-21 04:09:13 +11:00
fn with_c_str<F, T>(s: &str, f: F) -> T where F: FnOnce(*const libc::c_char) -> T {
2015-01-08 22:20:11 +11:00
use std::ffi::CString;
let c_str = CString::from_slice(s.as_bytes());
f(c_str.as_ptr())
2015-01-08 22:20:11 +11:00
}
2014-10-05 04:17:02 +11:00
pub struct HeadlessContext {
context: ffi::OSMesaContext,
buffer: Vec<u32>,
width: u32,
height: u32,
2014-10-05 04:17:02 +11:00
}
impl HeadlessContext {
pub fn new(builder: BuilderAttribs) -> Result<HeadlessContext, CreationError> {
let dimensions = builder.dimensions.unwrap();
2014-10-05 04:17:02 +11:00
Ok(HeadlessContext {
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 {
2014-11-15 01:59:45 +11:00
let ctxt = ffi::OSMesaCreateContext(0x1908, ptr::null());
if ctxt.is_null() {
return Err(OsError("OSMesaCreateContext failed".to_string()));
}
ctxt
2014-10-05 04:17:02 +11:00
}
})
}
pub unsafe fn make_current(&self) {
2014-11-15 01:59:45 +11:00
let ret = ffi::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
}
pub fn get_proc_address(&self, addr: &str) -> *const () {
unsafe {
2015-01-08 22:20:11 +11:00
with_c_str(addr, |s| {
2014-10-05 04:17:02 +11:00
ffi::OSMesaGetProcAddress(mem::transmute(s)) as *const ()
})
}
}
2014-11-19 03:55:26 +11:00
/// See the docs in the crate root file.
pub fn get_api(&self) -> ::Api {
::Api::OpenGl
}
pub fn set_window_resize_callback(&mut self, _: Option<fn(u32, u32)>) {
}
2014-10-05 04:17:02 +11:00
}
impl Drop for HeadlessContext {
fn drop(&mut self) {
unsafe { ffi::OSMesaDestroyContext(self.context) }
}
}
2014-12-30 08:56:15 +11:00
unsafe impl Send for HeadlessContext {}
unsafe impl Sync for HeadlessContext {}