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

70 lines
1.8 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;
pub struct HeadlessContext {
context: ffi::OSMesaContext,
buffer: Vec<u32>,
width: uint,
height: uint,
}
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: Vec::from_elem(dimensions.0 * dimensions.1, unsafe { mem::uninitialized() }),
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 () {
use std::c_str::ToCStr;
unsafe {
addr.with_c_str(|s| {
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(uint, uint)>) {
}
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 {}