winit/src/api/osmesa/mod.rs

141 lines
3.8 KiB
Rust
Raw Normal View History

#![cfg(any(target_os = "linux", target_os = "freebsd", target_os = "dragonfly"))]
extern crate osmesa_sys;
2015-04-30 13:23:37 +02:00
use Api;
use ContextError;
2014-11-19 06:09:54 +01:00
use CreationError;
2015-09-21 13:15:43 +02:00
use GlAttributes;
2015-04-30 13:23:37 +02:00
use GlContext;
use PixelFormat;
2015-09-21 13:15:43 +02:00
use PixelFormatRequirements;
use Robustness;
2014-10-04 19:17:02 +02:00
use libc;
use std::{mem, ptr};
use std::ffi::CString;
2014-10-04 19:17:02 +02:00
pub struct OsMesaContext {
context: osmesa_sys::OSMesaContext,
2014-10-04 19:17:02 +02:00
buffer: Vec<u32>,
width: u32,
height: u32,
2014-10-04 19:17:02 +02:00
}
2015-05-04 07:32:02 +02:00
pub enum OsMesaCreationError {
CreationError(CreationError),
NotSupported,
}
impl From<CreationError> for OsMesaCreationError {
2015-09-21 14:42:05 +02:00
#[inline]
2015-05-04 07:32:02 +02:00
fn from(e: CreationError) -> OsMesaCreationError {
OsMesaCreationError::CreationError(e)
}
}
impl OsMesaContext {
2015-09-21 13:15:43 +02:00
pub fn new(dimensions: (u32, u32), pf_reqs: &PixelFormatRequirements,
opengl: &GlAttributes<&OsMesaContext>) -> Result<OsMesaContext, OsMesaCreationError>
{
2015-05-04 07:32:02 +02:00
if let Err(_) = osmesa_sys::OsMesa::try_loading() {
return Err(OsMesaCreationError::NotSupported);
}
2015-09-21 13:15:43 +02:00
if opengl.sharing.is_some() { unimplemented!() } // TODO: proper error
2015-09-21 13:15:43 +02:00
match opengl.robustness {
Robustness::RobustNoResetNotification | Robustness::RobustLoseContextOnReset => {
return Err(CreationError::RobustnessNotSupported.into());
},
_ => ()
}
2015-09-21 13:15:43 +02:00
// TODO: use `pf_reqs` for the format
// TODO: check OpenGL version and return `OpenGlVersionNotSupported` if necessary
Ok(OsMesaContext {
width: dimensions.0,
height: dimensions.1,
buffer: ::std::iter::repeat(unsafe { mem::uninitialized() })
.take((dimensions.0 * dimensions.1) as usize).collect(),
2014-10-04 19:17:02 +02:00
context: unsafe {
let ctxt = osmesa_sys::OSMesaCreateContext(0x1908, ptr::null_mut());
2014-11-14 15:59:45 +01:00
if ctxt.is_null() {
2015-05-04 07:32:02 +02:00
return Err(CreationError::OsError("OSMesaCreateContext failed".to_string()).into());
2014-11-14 15:59:45 +01:00
}
ctxt
2014-10-04 19:17:02 +02:00
}
})
}
2015-09-21 14:42:05 +02:00
#[inline]
2015-04-26 16:23:22 +02:00
pub fn get_framebuffer(&self) -> &[u32] {
&self.buffer
}
2015-09-21 14:42:05 +02:00
#[inline]
2015-04-26 16:23:22 +02:00
pub fn get_dimensions(&self) -> (u32, u32) {
(self.width, self.height)
}
2015-05-08 12:31:56 -05:00
#[allow(dead_code)]
2015-04-30 13:23:37 +02:00
// TODO: can we remove this without causing havoc?
2015-09-21 14:42:05 +02:00
#[inline]
2015-04-30 13:23:37 +02:00
pub fn set_window_resize_callback(&mut self, _: Option<fn(u32, u32)>) {
}
}
impl GlContext for OsMesaContext {
2015-09-21 14:42:05 +02:00
#[inline]
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);
2014-11-14 15:59:45 +01:00
// an error can only happen in case of invalid parameter, which would indicate a bug
// in glutin
2014-11-14 15:59:45 +01:00
if ret == 0 {
panic!("OSMesaMakeCurrent failed");
2014-11-14 15:59:45 +01:00
}
Ok(())
2014-10-04 19:17:02 +02:00
}
2015-09-21 14:42:05 +02:00
#[inline]
2015-04-30 13:23:37 +02:00
fn is_current(&self) -> bool {
unsafe { osmesa_sys::OSMesaGetCurrentContext() == self.context }
2015-03-04 07:38:55 +01:00
}
2015-04-30 13:23:37 +02:00
fn get_proc_address(&self, addr: &str) -> *const libc::c_void {
2014-10-04 19:17:02 +02:00
unsafe {
2015-04-02 22:04:17 +02:00
let c_str = CString::new(addr.as_bytes().to_vec()).unwrap();
mem::transmute(osmesa_sys::OSMesaGetProcAddress(mem::transmute(c_str.as_ptr())))
2014-10-04 19:17:02 +02:00
}
}
2014-11-18 17:55:26 +01:00
2015-09-21 14:42:05 +02:00
#[inline]
fn swap_buffers(&self) -> Result<(), ContextError> {
Ok(())
2014-11-18 17:55:26 +01:00
}
2015-09-21 14:42:05 +02:00
#[inline]
2015-04-30 13:23:37 +02:00
fn get_api(&self) -> Api {
Api::OpenGl
}
2015-09-21 14:42:05 +02:00
#[inline]
2015-04-30 13:23:37 +02:00
fn get_pixel_format(&self) -> PixelFormat {
unimplemented!();
}
2014-10-04 19:17:02 +02:00
}
impl Drop for OsMesaContext {
2015-09-21 14:42:05 +02:00
#[inline]
2014-10-04 19:17:02 +02:00
fn drop(&mut self) {
unsafe { osmesa_sys::OSMesaDestroyContext(self.context) }
2014-10-04 19:17:02 +02:00
}
}
2014-12-29 22:56:15 +01:00
unsafe impl Send for OsMesaContext {}
unsafe impl Sync for OsMesaContext {}