aboutsummaryrefslogtreecommitdiffstats
path: root/src/x11/headless.rs
blob: 7692832c2eb997f6ba37446fa49cb48455b67fd0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
use BuilderAttribs;
use CreationError;
use CreationError::OsError;
use libc;
use std::{mem, ptr};
use super::ffi;

fn with_c_str<F, T>(s: &str, f: F) -> T where F: FnOnce(*const i8) -> T {
    use std::ffi::CString;
    let c_str = CString::from_slice(s.as_bytes());
    f(c_str.as_slice_with_nul().as_ptr())    
}

pub struct HeadlessContext {
    context: ffi::OSMesaContext,
    buffer: Vec<u32>,
    width: u32,
    height: u32,
}

impl HeadlessContext {
    pub fn new(builder: BuilderAttribs) -> Result<HeadlessContext, CreationError> {
        let dimensions = builder.dimensions.unwrap();

        Ok(HeadlessContext {
            width: dimensions.0,
            height: dimensions.1,
            buffer: ::std::iter::repeat(unsafe { mem::uninitialized() })
                .take((dimensions.0 * dimensions.1) as usize).collect(),
            context: unsafe {
                let ctxt = ffi::OSMesaCreateContext(0x1908, ptr::null());
                if ctxt.is_null() {
                    return Err(OsError("OSMesaCreateContext failed".to_string()));
                }
                ctxt
            }
        })
    }

    pub unsafe fn make_current(&self) {
        let ret = ffi::OSMesaMakeCurrent(self.context,
            self.buffer.as_ptr() as *mut libc::c_void,
            0x1401, self.width as libc::c_int, self.height as libc::c_int);

        if ret == 0 {
            panic!("OSMesaMakeCurrent failed")
        }
    }

    pub fn get_proc_address(&self, addr: &str) -> *const () {
        unsafe {
            with_c_str(addr, |s| {
                ffi::OSMesaGetProcAddress(mem::transmute(s)) as *const ()
            })
        }
    }

    /// 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)>) {
    }
}

impl Drop for HeadlessContext {
    fn drop(&mut self) {
        unsafe { ffi::OSMesaDestroyContext(self.context) }
    }
}

unsafe impl Send for HeadlessContext {}
unsafe impl Sync for HeadlessContext {}