aboutsummaryrefslogtreecommitdiffstats
path: root/src/api/osmesa/mod.rs
diff options
context:
space:
mode:
authortomaka <pierre.krieger1708@gmail.com>2015-04-24 19:27:41 +0200
committertomaka <pierre.krieger1708@gmail.com>2015-04-24 19:27:41 +0200
commitd6c50df294a7832b1d9233aaa0c5e0bee8929c8f (patch)
tree3577f29635ebd3c1340740eee19250b716400578 /src/api/osmesa/mod.rs
parent4dee36631c520726179139bd543453ff0f3b78d6 (diff)
parenta72e06cf1b8f0e037be8a0e3152b9b0ab9ef2c95 (diff)
downloadglutin-d6c50df294a7832b1d9233aaa0c5e0bee8929c8f.tar.gz
glutin-d6c50df294a7832b1d9233aaa0c5e0bee8929c8f.zip
Merge pull request #395 from tomaka/x-reorg
Reorganize the Linux implementation and split OSMesa and X11
Diffstat (limited to 'src/api/osmesa/mod.rs')
-rw-r--r--src/api/osmesa/mod.rs75
1 files changed, 75 insertions, 0 deletions
diff --git a/src/api/osmesa/mod.rs b/src/api/osmesa/mod.rs
new file mode 100644
index 0000000..585ce72
--- /dev/null
+++ b/src/api/osmesa/mod.rs
@@ -0,0 +1,75 @@
+#![cfg(all(any(target_os = "linux", target_os = "freebsd"), feature="headless"))]
+
+extern crate osmesa_sys;
+
+use BuilderAttribs;
+use CreationError;
+use CreationError::OsError;
+use libc;
+use std::{mem, ptr};
+use std::ffi::CString;
+
+pub struct OsMesaContext {
+ context: osmesa_sys::OSMesaContext,
+ buffer: Vec<u32>,
+ width: u32,
+ height: u32,
+}
+
+impl OsMesaContext {
+ pub fn new(builder: BuilderAttribs) -> Result<OsMesaContext, CreationError> {
+ let dimensions = builder.dimensions.unwrap();
+
+ 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(OsError("OSMesaCreateContext failed".to_string()));
+ }
+ ctxt
+ }
+ })
+ }
+
+ pub unsafe fn make_current(&self) {
+ 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);
+
+ if ret == 0 {
+ panic!("OSMesaMakeCurrent failed")
+ }
+ }
+
+ pub fn is_current(&self) -> bool {
+ unsafe { osmesa_sys::OSMesaGetCurrentContext() == self.context }
+ }
+
+ pub fn get_proc_address(&self, addr: &str) -> *const () {
+ unsafe {
+ let c_str = CString::new(addr.as_bytes().to_vec()).unwrap();
+ mem::transmute(osmesa_sys::OSMesaGetProcAddress(mem::transmute(c_str.as_ptr())))
+ }
+ }
+
+ /// 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 OsMesaContext {
+ fn drop(&mut self) {
+ unsafe { osmesa_sys::OSMesaDestroyContext(self.context) }
+ }
+}
+
+unsafe impl Send for OsMesaContext {}
+unsafe impl Sync for OsMesaContext {}