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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
|
#![cfg(any(target_os = "linux", target_os = "freebsd"))]
extern crate osmesa_sys;
use Api;
use BuilderAttribs;
use ContextError;
use CreationError;
use GlContext;
use PixelFormat;
use Robustness;
use libc;
use std::{mem, ptr};
use std::ffi::CString;
pub struct OsMesaContext {
context: osmesa_sys::OSMesaContext,
buffer: Vec<u32>,
width: u32,
height: u32,
}
pub enum OsMesaCreationError {
CreationError(CreationError),
NotSupported,
}
impl From<CreationError> for OsMesaCreationError {
fn from(e: CreationError) -> OsMesaCreationError {
OsMesaCreationError::CreationError(e)
}
}
impl OsMesaContext {
pub fn new(builder: BuilderAttribs) -> Result<OsMesaContext, OsMesaCreationError> {
if let Err(_) = osmesa_sys::OsMesa::try_loading() {
return Err(OsMesaCreationError::NotSupported);
}
let dimensions = builder.dimensions.unwrap();
match builder.gl_robustness {
Robustness::RobustNoResetNotification | Robustness::RobustLoseContextOnReset => {
return Err(CreationError::NotSupported.into());
},
_ => ()
}
Ok(OsMesaContext {
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 = osmesa_sys::OSMesaCreateContext(0x1908, ptr::null_mut());
if ctxt.is_null() {
return Err(CreationError::OsError("OSMesaCreateContext failed".to_string()).into());
}
ctxt
}
})
}
pub fn get_framebuffer(&self) -> &[u32] {
&self.buffer
}
pub fn get_dimensions(&self) -> (u32, u32) {
(self.width, self.height)
}
#[allow(dead_code)]
// TODO: can we remove this without causing havoc?
pub fn set_window_resize_callback(&mut self, _: Option<fn(u32, u32)>) {
}
}
impl GlContext for OsMesaContext {
unsafe fn make_current(&self) -> Result<(), ContextError> {
let ret = osmesa_sys::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);
// an error can only happen in case of invalid parameter, which would indicate a bug
// in glutin
if ret == 0 {
panic!("OSMesaMakeCurrent failed");
}
Ok(())
}
fn is_current(&self) -> bool {
unsafe { osmesa_sys::OSMesaGetCurrentContext() == self.context }
}
fn get_proc_address(&self, addr: &str) -> *const libc::c_void {
unsafe {
let c_str = CString::new(addr.as_bytes().to_vec()).unwrap();
mem::transmute(osmesa_sys::OSMesaGetProcAddress(mem::transmute(c_str.as_ptr())))
}
}
fn swap_buffers(&self) -> Result<(), ContextError> {
Ok(())
}
fn get_api(&self) -> Api {
Api::OpenGl
}
fn get_pixel_format(&self) -> PixelFormat {
unimplemented!();
}
}
impl Drop for OsMesaContext {
fn drop(&mut self) {
unsafe { osmesa_sys::OSMesaDestroyContext(self.context) }
}
}
unsafe impl Send for OsMesaContext {}
unsafe impl Sync for OsMesaContext {}
|