aboutsummaryrefslogtreecommitdiffstats
path: root/src/x11/headless.rs
blob: 71232e5650a9643070042004d602c438e0b253c8 (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
use HeadlessRendererBuilder;
use CreationError;
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: HeadlessRendererBuilder) -> Result<HeadlessContext, CreationError> {
        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 {
                // TODO: check errors
                ffi::OSMesaCreateContext(0x1908, ptr::null())
            }
        })
    }

    pub unsafe fn make_current(&self) {
        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);
    }

    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 ()
            })
        }
    }
}

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