2014-10-05 04:17:02 +11:00
|
|
|
use HeadlessRendererBuilder;
|
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 {
|
2014-11-06 02:42:18 +11:00
|
|
|
pub fn new(builder: HeadlessRendererBuilder) -> Result<HeadlessContext, CreationError> {
|
2014-10-05 04:17:02 +11:00
|
|
|
Ok(HeadlessContext {
|
|
|
|
width: builder.dimensions.0,
|
|
|
|
height: builder.dimensions.1,
|
|
|
|
buffer: Vec::from_elem(builder.dimensions.0 * builder.dimensions.1, unsafe { mem::uninitialized() }),
|
|
|
|
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
|
|
|
|
}
|
2014-12-16 16:49:22 +11:00
|
|
|
|
|
|
|
pub fn set_window_resize_callback(&mut self, _: fn(uint, uint)) {
|
|
|
|
}
|
2014-10-05 04:17:02 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Drop for HeadlessContext {
|
|
|
|
fn drop(&mut self) {
|
|
|
|
unsafe { ffi::OSMesaDestroyContext(self.context) }
|
|
|
|
}
|
|
|
|
}
|