2015-04-25 22:14:30 +02:00
|
|
|
#![cfg(target_os = "linux")]
|
2015-04-24 14:12:57 +02:00
|
|
|
|
|
|
|
|
use BuilderAttribs;
|
|
|
|
|
use CreationError;
|
2015-04-30 13:23:37 +02:00
|
|
|
use GlContext;
|
2015-04-24 14:12:57 +02:00
|
|
|
use GlRequest;
|
2015-04-29 22:11:16 +02:00
|
|
|
use PixelFormat;
|
2015-04-24 14:12:57 +02:00
|
|
|
use Api;
|
|
|
|
|
|
|
|
|
|
use libc;
|
|
|
|
|
use std::ffi::CString;
|
|
|
|
|
use std::{mem, ptr};
|
|
|
|
|
|
|
|
|
|
pub mod ffi;
|
|
|
|
|
|
|
|
|
|
pub struct Context {
|
|
|
|
|
egl: ffi::egl::Egl,
|
|
|
|
|
display: ffi::egl::types::EGLDisplay,
|
|
|
|
|
context: ffi::egl::types::EGLContext,
|
|
|
|
|
surface: ffi::egl::types::EGLSurface,
|
2015-04-29 22:11:16 +02:00
|
|
|
api: Api,
|
|
|
|
|
pixel_format: PixelFormat,
|
2015-04-24 14:12:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Context {
|
|
|
|
|
pub fn new(egl: ffi::egl::Egl, builder: BuilderAttribs,
|
|
|
|
|
native_display: Option<ffi::EGLNativeDisplayType>,
|
|
|
|
|
native_window: ffi::EGLNativeWindowType) -> Result<Context, CreationError>
|
|
|
|
|
{
|
|
|
|
|
if builder.sharing.is_some() {
|
|
|
|
|
unimplemented!()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let display = unsafe {
|
|
|
|
|
let display = egl.GetDisplay(native_display.unwrap_or(mem::transmute(ffi::egl::DEFAULT_DISPLAY)));
|
|
|
|
|
if display.is_null() {
|
|
|
|
|
return Err(CreationError::OsError("No EGL display connection available".to_string()));
|
|
|
|
|
}
|
|
|
|
|
display
|
|
|
|
|
};
|
|
|
|
|
|
2015-04-29 22:11:16 +02:00
|
|
|
let egl_version = unsafe {
|
2015-04-24 14:12:57 +02:00
|
|
|
let mut major: ffi::egl::types::EGLint = mem::uninitialized();
|
|
|
|
|
let mut minor: ffi::egl::types::EGLint = mem::uninitialized();
|
|
|
|
|
|
|
|
|
|
if egl.Initialize(display, &mut major, &mut minor) == 0 {
|
|
|
|
|
return Err(CreationError::OsError(format!("eglInitialize failed")))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
(major, minor)
|
|
|
|
|
};
|
|
|
|
|
|
2015-04-29 22:11:16 +02:00
|
|
|
// binding the right API and choosing the version
|
|
|
|
|
let (version, api) = unsafe {
|
|
|
|
|
match builder.gl_version {
|
|
|
|
|
GlRequest::Latest => {
|
|
|
|
|
if egl_version >= (1, 4) {
|
|
|
|
|
if egl.BindAPI(ffi::egl::OPENGL_API) != 0 {
|
|
|
|
|
(None, Api::OpenGl)
|
|
|
|
|
} else if egl.BindAPI(ffi::egl::OPENGL_ES_API) != 0 {
|
|
|
|
|
(None, Api::OpenGlEs)
|
|
|
|
|
} else {
|
|
|
|
|
return Err(CreationError::NotSupported);
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
(None, Api::OpenGlEs)
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
GlRequest::Specific(Api::OpenGlEs, version) => {
|
|
|
|
|
if egl_version >= (1, 2) {
|
|
|
|
|
if egl.BindAPI(ffi::egl::OPENGL_ES_API) == 0 {
|
|
|
|
|
return Err(CreationError::NotSupported);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
(Some(version), Api::OpenGlEs)
|
|
|
|
|
},
|
|
|
|
|
GlRequest::Specific(Api::OpenGl, version) => {
|
|
|
|
|
if egl_version < (1, 4) {
|
|
|
|
|
return Err(CreationError::NotSupported);
|
|
|
|
|
}
|
|
|
|
|
if egl.BindAPI(ffi::egl::OPENGL_API) == 0 {
|
|
|
|
|
return Err(CreationError::NotSupported);
|
|
|
|
|
}
|
|
|
|
|
(Some(version), Api::OpenGl)
|
|
|
|
|
},
|
|
|
|
|
GlRequest::Specific(_, _) => return Err(CreationError::NotSupported),
|
|
|
|
|
GlRequest::GlThenGles { opengles_version, opengl_version } => {
|
|
|
|
|
if egl_version >= (1, 4) {
|
|
|
|
|
if egl.BindAPI(ffi::egl::OPENGL_API) != 0 {
|
|
|
|
|
(Some(opengl_version), Api::OpenGl)
|
|
|
|
|
} else if egl.BindAPI(ffi::egl::OPENGL_ES_API) != 0 {
|
|
|
|
|
(Some(opengles_version), Api::OpenGlEs)
|
|
|
|
|
} else {
|
|
|
|
|
return Err(CreationError::NotSupported);
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
(Some(opengles_version), Api::OpenGlEs)
|
|
|
|
|
}
|
|
|
|
|
},
|
2015-04-24 14:12:57 +02:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2015-04-29 22:11:16 +02:00
|
|
|
let configs = unsafe { try!(enumerate_configs(&egl, display, &egl_version, api, version)) };
|
|
|
|
|
let (config_id, pixel_format) = try!(builder.choose_pixel_format(configs.into_iter()));
|
|
|
|
|
|
2015-04-24 14:12:57 +02:00
|
|
|
let surface = unsafe {
|
2015-04-29 22:11:16 +02:00
|
|
|
let surface = egl.CreateWindowSurface(display, config_id, native_window, ptr::null());
|
2015-04-24 14:12:57 +02:00
|
|
|
if surface.is_null() {
|
|
|
|
|
return Err(CreationError::OsError(format!("eglCreateWindowSurface failed")))
|
|
|
|
|
}
|
|
|
|
|
surface
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
let context = unsafe {
|
2015-04-29 22:11:16 +02:00
|
|
|
if let Some(version) = version {
|
|
|
|
|
try!(create_context(&egl, display, &egl_version, api, version, config_id,
|
|
|
|
|
builder.gl_debug).map_err(|_| CreationError::NotSupported))
|
|
|
|
|
|
|
|
|
|
} else if api == Api::OpenGlEs {
|
|
|
|
|
if let Ok(ctxt) = create_context(&egl, display, &egl_version, api, (2, 0),
|
|
|
|
|
config_id, builder.gl_debug)
|
|
|
|
|
{
|
|
|
|
|
ctxt
|
|
|
|
|
} else if let Ok(ctxt) = create_context(&egl, display, &egl_version, api, (1, 0),
|
|
|
|
|
config_id, builder.gl_debug)
|
|
|
|
|
{
|
|
|
|
|
ctxt
|
|
|
|
|
} else {
|
|
|
|
|
return Err(CreationError::NotSupported);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
if let Ok(ctxt) = create_context(&egl, display, &egl_version, api, (3, 2),
|
|
|
|
|
config_id, builder.gl_debug)
|
|
|
|
|
{
|
|
|
|
|
ctxt
|
|
|
|
|
} else if let Ok(ctxt) = create_context(&egl, display, &egl_version, api, (3, 1),
|
|
|
|
|
config_id, builder.gl_debug)
|
|
|
|
|
{
|
|
|
|
|
ctxt
|
|
|
|
|
} else if let Ok(ctxt) = create_context(&egl, display, &egl_version, api, (1, 0),
|
|
|
|
|
config_id, builder.gl_debug)
|
|
|
|
|
{
|
|
|
|
|
ctxt
|
|
|
|
|
} else {
|
|
|
|
|
return Err(CreationError::NotSupported);
|
|
|
|
|
}
|
2015-04-24 14:12:57 +02:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
Ok(Context {
|
|
|
|
|
egl: egl,
|
|
|
|
|
display: display,
|
|
|
|
|
context: context,
|
|
|
|
|
surface: surface,
|
2015-04-29 22:11:16 +02:00
|
|
|
api: api,
|
|
|
|
|
pixel_format: pixel_format,
|
2015-04-24 14:12:57 +02:00
|
|
|
})
|
|
|
|
|
}
|
2015-04-30 13:23:37 +02:00
|
|
|
}
|
2015-04-24 14:12:57 +02:00
|
|
|
|
2015-04-30 13:23:37 +02:00
|
|
|
impl GlContext for Context {
|
|
|
|
|
unsafe fn make_current(&self) {
|
2015-04-24 14:12:57 +02:00
|
|
|
let ret = unsafe {
|
|
|
|
|
self.egl.MakeCurrent(self.display, self.surface, self.surface, self.context)
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
if ret == 0 {
|
|
|
|
|
panic!("eglMakeCurrent failed");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-04-30 13:23:37 +02:00
|
|
|
fn is_current(&self) -> bool {
|
2015-04-24 14:12:57 +02:00
|
|
|
unsafe { self.egl.GetCurrentContext() == self.context }
|
|
|
|
|
}
|
|
|
|
|
|
2015-04-30 13:23:37 +02:00
|
|
|
fn get_proc_address(&self, addr: &str) -> *const libc::c_void {
|
2015-04-24 14:12:57 +02:00
|
|
|
let addr = CString::new(addr.as_bytes()).unwrap();
|
|
|
|
|
let addr = addr.as_ptr();
|
|
|
|
|
unsafe {
|
2015-04-30 13:23:37 +02:00
|
|
|
self.egl.GetProcAddress(addr) as *const _
|
2015-04-24 14:12:57 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-04-30 13:23:37 +02:00
|
|
|
fn swap_buffers(&self) {
|
2015-04-24 14:12:57 +02:00
|
|
|
let ret = unsafe {
|
|
|
|
|
self.egl.SwapBuffers(self.display, self.surface)
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
if ret == 0 {
|
|
|
|
|
panic!("eglSwapBuffers failed");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-04-30 13:23:37 +02:00
|
|
|
fn get_api(&self) -> Api {
|
2015-04-29 22:11:16 +02:00
|
|
|
self.api
|
2015-04-24 14:12:57 +02:00
|
|
|
}
|
2015-04-30 13:23:37 +02:00
|
|
|
|
|
|
|
|
fn get_pixel_format(&self) -> PixelFormat {
|
|
|
|
|
self.pixel_format.clone()
|
|
|
|
|
}
|
2015-04-24 14:12:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
unsafe impl Send for Context {}
|
|
|
|
|
unsafe impl Sync for Context {}
|
|
|
|
|
|
|
|
|
|
impl Drop for Context {
|
|
|
|
|
fn drop(&mut self) {
|
|
|
|
|
use std::ptr;
|
|
|
|
|
|
|
|
|
|
unsafe {
|
|
|
|
|
// we don't call MakeCurrent(0, 0) because we are not sure that the context
|
|
|
|
|
// is still the current one
|
|
|
|
|
self.egl.DestroyContext(self.display, self.context);
|
|
|
|
|
self.egl.DestroySurface(self.display, self.surface);
|
|
|
|
|
self.egl.Terminate(self.display);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2015-04-29 22:11:16 +02:00
|
|
|
|
|
|
|
|
unsafe fn enumerate_configs(egl: &ffi::egl::Egl, display: ffi::egl::types::EGLDisplay,
|
|
|
|
|
egl_version: &(ffi::egl::types::EGLint, ffi::egl::types::EGLint),
|
|
|
|
|
api: Api, version: Option<(u8, u8)>)
|
|
|
|
|
-> Result<Vec<(ffi::egl::types::EGLConfig, PixelFormat)>, CreationError>
|
|
|
|
|
{
|
|
|
|
|
let mut num_configs = mem::uninitialized();
|
|
|
|
|
if egl.GetConfigs(display, ptr::null_mut(), 0, &mut num_configs) == 0 {
|
|
|
|
|
return Err(CreationError::OsError(format!("eglGetConfigs failed")));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let mut configs_ids = Vec::with_capacity(num_configs as usize);
|
|
|
|
|
if egl.GetConfigs(display, configs_ids.as_mut_ptr(),
|
|
|
|
|
configs_ids.capacity() as ffi::egl::types::EGLint,
|
|
|
|
|
&mut num_configs) == 0
|
|
|
|
|
{
|
|
|
|
|
return Err(CreationError::OsError(format!("eglGetConfigs failed")));
|
|
|
|
|
}
|
|
|
|
|
configs_ids.set_len(num_configs as usize);
|
|
|
|
|
|
|
|
|
|
// analyzing each config
|
|
|
|
|
let mut result = Vec::with_capacity(num_configs as usize);
|
|
|
|
|
for config_id in configs_ids {
|
|
|
|
|
macro_rules! attrib {
|
|
|
|
|
($egl:expr, $display:expr, $config:expr, $attr:expr) => (
|
|
|
|
|
{
|
|
|
|
|
let mut value = mem::uninitialized();
|
|
|
|
|
let res = $egl.GetConfigAttrib($display, $config,
|
|
|
|
|
$attr as ffi::egl::types::EGLint, &mut value);
|
|
|
|
|
if res == 0 {
|
|
|
|
|
return Err(CreationError::OsError(format!("eglGetConfigAttrib failed")));
|
|
|
|
|
}
|
|
|
|
|
value
|
|
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
let renderable = attrib!(egl, display, config_id, ffi::egl::RENDERABLE_TYPE) as u32;
|
|
|
|
|
let conformant = attrib!(egl, display, config_id, ffi::egl::CONFORMANT) as u32;
|
|
|
|
|
|
|
|
|
|
if api == Api::OpenGlEs {
|
|
|
|
|
if let Some(version) = version {
|
|
|
|
|
if version.0 == 3 && (renderable & ffi::egl::OPENGL_ES3_BIT == 0 ||
|
|
|
|
|
conformant & ffi::egl::OPENGL_ES3_BIT == 0)
|
|
|
|
|
{
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if version.0 == 2 && (renderable & ffi::egl::OPENGL_ES2_BIT == 0 ||
|
|
|
|
|
conformant & ffi::egl::OPENGL_ES2_BIT == 0)
|
|
|
|
|
{
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if version.0 == 1 && (renderable & ffi::egl::OPENGL_ES_BIT == 0 ||
|
|
|
|
|
conformant & ffi::egl::OPENGL_ES_BIT == 0)
|
|
|
|
|
{
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} else if api == Api::OpenGl {
|
|
|
|
|
if renderable & ffi::egl::OPENGL_BIT == 0 ||
|
|
|
|
|
conformant & ffi::egl::OPENGL_BIT == 0
|
|
|
|
|
{
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if attrib!(egl, display, config_id, ffi::egl::SURFACE_TYPE) & ffi::egl::WINDOW_BIT as i32 == 0 {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if attrib!(egl, display, config_id, ffi::egl::TRANSPARENT_TYPE) != ffi::egl::NONE as i32 {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if attrib!(egl, display, config_id, ffi::egl::COLOR_BUFFER_TYPE) != ffi::egl::RGB_BUFFER as i32 {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
result.push((config_id, PixelFormat {
|
|
|
|
|
hardware_accelerated: attrib!(egl, display, config_id, ffi::egl::CONFIG_CAVEAT)
|
|
|
|
|
!= ffi::egl::SLOW_CONFIG as i32,
|
2015-04-30 16:15:37 +02:00
|
|
|
color_bits: attrib!(egl, display, config_id, ffi::egl::RED_SIZE) as u8 +
|
|
|
|
|
attrib!(egl, display, config_id, ffi::egl::BLUE_SIZE) as u8 +
|
|
|
|
|
attrib!(egl, display, config_id, ffi::egl::GREEN_SIZE) as u8,
|
2015-04-29 22:11:16 +02:00
|
|
|
alpha_bits: attrib!(egl, display, config_id, ffi::egl::ALPHA_SIZE) as u8,
|
|
|
|
|
depth_bits: attrib!(egl, display, config_id, ffi::egl::DEPTH_SIZE) as u8,
|
|
|
|
|
stencil_bits: attrib!(egl, display, config_id, ffi::egl::STENCIL_SIZE) as u8,
|
|
|
|
|
stereoscopy: false,
|
|
|
|
|
double_buffer: true,
|
|
|
|
|
multisampling: match attrib!(egl, display, config_id, ffi::egl::SAMPLES) {
|
|
|
|
|
0 | 1 => None,
|
|
|
|
|
a => Some(a as u16),
|
|
|
|
|
},
|
|
|
|
|
srgb: false, // TODO: use EGL_KHR_gl_colorspace to know that
|
|
|
|
|
}));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Ok(result)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
unsafe fn create_context(egl: &ffi::egl::Egl, display: ffi::egl::types::EGLDisplay,
|
|
|
|
|
egl_version: &(ffi::egl::types::EGLint, ffi::egl::types::EGLint),
|
|
|
|
|
api: Api, version: (u8, u8), config_id: ffi::egl::types::EGLConfig,
|
|
|
|
|
gl_debug: bool)
|
|
|
|
|
-> Result<ffi::egl::types::EGLContext, ()>
|
|
|
|
|
{
|
|
|
|
|
let mut context_attributes = vec![];
|
|
|
|
|
|
|
|
|
|
if egl_version >= &(1, 5) {
|
|
|
|
|
context_attributes.push(ffi::egl::CONTEXT_MAJOR_VERSION as i32);
|
|
|
|
|
context_attributes.push(version.0 as i32);
|
|
|
|
|
context_attributes.push(ffi::egl::CONTEXT_MINOR_VERSION as i32);
|
|
|
|
|
context_attributes.push(version.1 as i32);
|
|
|
|
|
|
|
|
|
|
if gl_debug {
|
|
|
|
|
context_attributes.push(ffi::egl::CONTEXT_OPENGL_DEBUG as i32);
|
|
|
|
|
context_attributes.push(ffi::egl::TRUE as i32);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
if api == Api::OpenGlEs {
|
|
|
|
|
context_attributes.push(ffi::egl::CONTEXT_CLIENT_VERSION as i32);
|
|
|
|
|
context_attributes.push(version.0 as i32);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
context_attributes.push(ffi::egl::NONE as i32);
|
|
|
|
|
|
|
|
|
|
let context = egl.CreateContext(display, config_id, ptr::null(),
|
|
|
|
|
context_attributes.as_ptr());
|
|
|
|
|
|
|
|
|
|
if context.is_null() {
|
|
|
|
|
return Err(());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Ok(context)
|
|
|
|
|
}
|