Merge pull request #566 from tomaka/display-egl

Correct EGL display handling
This commit is contained in:
tomaka 2015-08-07 16:49:42 +02:00
commit 90e62083d6
7 changed files with 181 additions and 43 deletions

View file

@ -46,6 +46,15 @@ fn main() {
"EGL_KHR_create_context".to_string(), "EGL_KHR_create_context".to_string(),
"EGL_EXT_create_context_robustness".to_string(), "EGL_EXT_create_context_robustness".to_string(),
"EGL_KHR_create_context_no_error".to_string(), "EGL_KHR_create_context_no_error".to_string(),
"EGL_KHR_platform_x11".to_string(),
"EGL_KHR_platform_android".to_string(),
"EGL_KHR_platform_wayland".to_string(),
"EGL_KHR_platform_gbm".to_string(),
"EGL_EXT_platform_base".to_string(),
"EGL_EXT_platform_x11".to_string(),
"EGL_MESA_platform_gbm".to_string(),
"EGL_EXT_platform_wayland".to_string(),
"EGL_EXT_platform_device".to_string(),
], ],
"1.5", "core", &mut file).unwrap(); "1.5", "core", &mut file).unwrap();
} }
@ -83,6 +92,15 @@ fn main() {
"EGL_KHR_create_context".to_string(), "EGL_KHR_create_context".to_string(),
"EGL_EXT_create_context_robustness".to_string(), "EGL_EXT_create_context_robustness".to_string(),
"EGL_KHR_create_context_no_error".to_string(), "EGL_KHR_create_context_no_error".to_string(),
"EGL_KHR_platform_x11".to_string(),
"EGL_KHR_platform_android".to_string(),
"EGL_KHR_platform_wayland".to_string(),
"EGL_KHR_platform_gbm".to_string(),
"EGL_EXT_platform_base".to_string(),
"EGL_EXT_platform_x11".to_string(),
"EGL_MESA_platform_gbm".to_string(),
"EGL_EXT_platform_wayland".to_string(),
"EGL_EXT_platform_device".to_string(),
], ],
"1.5", "core", &mut file).unwrap(); "1.5", "core", &mut file).unwrap();
} }
@ -97,6 +115,15 @@ fn main() {
"EGL_KHR_create_context".to_string(), "EGL_KHR_create_context".to_string(),
"EGL_EXT_create_context_robustness".to_string(), "EGL_EXT_create_context_robustness".to_string(),
"EGL_KHR_create_context_no_error".to_string(), "EGL_KHR_create_context_no_error".to_string(),
"EGL_KHR_platform_x11".to_string(),
"EGL_KHR_platform_android".to_string(),
"EGL_KHR_platform_wayland".to_string(),
"EGL_KHR_platform_gbm".to_string(),
"EGL_EXT_platform_base".to_string(),
"EGL_EXT_platform_x11".to_string(),
"EGL_MESA_platform_gbm".to_string(),
"EGL_EXT_platform_wayland".to_string(),
"EGL_EXT_platform_device".to_string(),
], ],
"1.5", "core", &mut file).unwrap(); "1.5", "core", &mut file).unwrap();
} }
@ -108,7 +135,18 @@ fn main() {
gl_generator::Fallbacks::All, gl_generator::Fallbacks::All,
khronos_api::EGL_XML, khronos_api::EGL_XML,
vec![ vec![
"EGL_KHR_create_context".to_string() "EGL_KHR_create_context".to_string(),
"EGL_EXT_create_context_robustness".to_string(),
"EGL_KHR_create_context_no_error".to_string(),
"EGL_KHR_platform_x11".to_string(),
"EGL_KHR_platform_android".to_string(),
"EGL_KHR_platform_wayland".to_string(),
"EGL_KHR_platform_gbm".to_string(),
"EGL_EXT_platform_base".to_string(),
"EGL_EXT_platform_x11".to_string(),
"EGL_MESA_platform_gbm".to_string(),
"EGL_EXT_platform_wayland".to_string(),
"EGL_EXT_platform_device".to_string(),
], ],
"1.5", "core", &mut file).unwrap(); "1.5", "core", &mut file).unwrap();

View file

@ -112,7 +112,7 @@ impl Window {
return Err(OsError(format!("Android's native window is null"))); return Err(OsError(format!("Android's native window is null")));
} }
let context = try!(EglContext::new(egl::ffi::egl::Egl, &builder, None) let context = try!(EglContext::new(egl::ffi::egl::Egl, &builder, egl::NativeDisplay::Android)
.and_then(|p| p.finish(native_window as *const _))); .and_then(|p| p.finish(native_window as *const _)));
let (tx, rx) = channel(); let (tx, rx) = channel();
@ -255,7 +255,7 @@ pub struct HeadlessContext(EglContext);
impl HeadlessContext { impl HeadlessContext {
/// See the docs in the crate root file. /// See the docs in the crate root file.
pub fn new(builder: BuilderAttribs) -> Result<HeadlessContext, CreationError> { pub fn new(builder: BuilderAttribs) -> Result<HeadlessContext, CreationError> {
let context = try!(EglContext::new(egl::ffi::egl::Egl, &builder, None)); let context = try!(EglContext::new(egl::ffi::egl::Egl, &builder, egl::NativeDisplay::Android));
let context = try!(context.finish_pbuffer()); let context = try!(context.finish_pbuffer());
Ok(context) Ok(context)
} }

View file

@ -16,6 +16,22 @@ use std::{mem, ptr};
pub mod ffi; pub mod ffi;
/// Specifies the type of display passed as `native_display`.
pub enum NativeDisplay {
/// `None` means `EGL_DEFAULT_DISPLAY`.
X11(Option<ffi::EGLNativeDisplayType>),
/// `None` means `EGL_DEFAULT_DISPLAY`.
Gbm(Option<ffi::EGLNativeDisplayType>),
/// `None` means `EGL_DEFAULT_DISPLAY`.
Wayland(Option<ffi::EGLNativeDisplayType>),
/// `EGL_DEFAULT_DISPLAY` is mandatory for Android.
Android,
// TODO: should be `EGLDeviceEXT`
Device(ffi::EGLNativeDisplayType),
/// Don't specify any display type. Useful on windows. `None` means `EGL_DEFAULT_DISPLAY`.
Other(Option<ffi::EGLNativeDisplayType>),
}
pub struct Context { pub struct Context {
egl: ffi::egl::Egl, egl: ffi::egl::Egl,
display: ffi::egl::types::EGLDisplay, display: ffi::egl::types::EGLDisplay,
@ -32,21 +48,96 @@ impl Context {
/// ///
/// To finish the process, you must call `.finish(window)` on the `ContextPrototype`. /// To finish the process, you must call `.finish(window)` on the `ContextPrototype`.
pub fn new<'a>(egl: ffi::egl::Egl, builder: &'a BuilderAttribs<'a>, pub fn new<'a>(egl: ffi::egl::Egl, builder: &'a BuilderAttribs<'a>,
native_display: Option<ffi::EGLNativeDisplayType>) native_display: NativeDisplay)
-> Result<ContextPrototype<'a>, CreationError> -> Result<ContextPrototype<'a>, CreationError>
{ {
if builder.sharing.is_some() { if builder.sharing.is_some() {
unimplemented!() unimplemented!()
} }
let display = unsafe { // the first step is to query the list of extensions without any display, if supported
let display = egl.GetDisplay(native_display.unwrap_or(mem::transmute(ffi::egl::DEFAULT_DISPLAY))); let dp_extensions = unsafe {
if display.is_null() { let p = egl.QueryString(ffi::egl::NO_DISPLAY, ffi::egl::EXTENSIONS as i32);
return Err(CreationError::OsError("No EGL display connection available".to_string()));
// this possibility is available only with EGL 1.5 or EGL_EXT_platform_base, otherwise
// `eglQueryString` returns an error
if p.is_null() {
vec![]
} else {
let p = CStr::from_ptr(p);
let list = String::from_utf8(p.to_bytes().to_vec()).unwrap_or_else(|_| format!(""));
list.split(' ').map(|e| e.to_string()).collect::<Vec<_>>()
} }
display
}; };
let has_dp_extension = |e: &str| dp_extensions.iter().find(|s| s == &e).is_some();
// calling `eglGetDisplay` or equivalent
let display = match native_display {
NativeDisplay::X11(display) if has_dp_extension("EGL_KHR_platform_x11") => {
let d = display.unwrap_or(ffi::egl::DEFAULT_DISPLAY as *const _);
// TODO: `PLATFORM_X11_SCREEN_KHR`
unsafe { egl.GetPlatformDisplay(ffi::egl::PLATFORM_X11_KHR, d as *mut _,
ptr::null()) }
},
NativeDisplay::X11(display) if has_dp_extension("EGL_EXT_platform_x11") => {
let d = display.unwrap_or(ffi::egl::DEFAULT_DISPLAY as *const _);
// TODO: `PLATFORM_X11_SCREEN_EXT`
unsafe { egl.GetPlatformDisplayEXT(ffi::egl::PLATFORM_X11_EXT, d as *mut _,
ptr::null()) }
},
NativeDisplay::Gbm(display) if has_dp_extension("EGL_KHR_platform_gbm") => {
let d = display.unwrap_or(ffi::egl::DEFAULT_DISPLAY as *const _);
unsafe { egl.GetPlatformDisplay(ffi::egl::PLATFORM_GBM_KHR, d as *mut _,
ptr::null()) }
},
NativeDisplay::Gbm(display) if has_dp_extension("EGL_MESA_platform_gbm") => {
let d = display.unwrap_or(ffi::egl::DEFAULT_DISPLAY as *const _);
unsafe { egl.GetPlatformDisplayEXT(ffi::egl::PLATFORM_GBM_KHR, d as *mut _,
ptr::null()) }
},
NativeDisplay::Wayland(display) if has_dp_extension("EGL_KHR_platform_wayland") => {
let d = display.unwrap_or(ffi::egl::DEFAULT_DISPLAY as *const _);
unsafe { egl.GetPlatformDisplay(ffi::egl::PLATFORM_WAYLAND_KHR, d as *mut _,
ptr::null()) }
},
NativeDisplay::Wayland(display) if has_dp_extension("EGL_EXT_platform_wayland") => {
let d = display.unwrap_or(ffi::egl::DEFAULT_DISPLAY as *const _);
unsafe { egl.GetPlatformDisplayEXT(ffi::egl::PLATFORM_WAYLAND_EXT, d as *mut _,
ptr::null()) }
},
NativeDisplay::Android if has_dp_extension("EGL_KHR_platform_android") => {
unsafe { egl.GetPlatformDisplay(ffi::egl::PLATFORM_ANDROID_KHR,
ffi::egl::DEFAULT_DISPLAY as *mut _, ptr::null()) }
},
NativeDisplay::Device(display) if has_dp_extension("EGL_EXT_platform_device") => {
unsafe { egl.GetPlatformDisplay(ffi::egl::PLATFORM_DEVICE_EXT, display as *mut _,
ptr::null()) }
},
NativeDisplay::X11(Some(display)) | NativeDisplay::Gbm(Some(display)) |
NativeDisplay::Wayland(Some(display)) | NativeDisplay::Device(display) |
NativeDisplay::Other(Some(display)) => {
unsafe { egl.GetDisplay(display as *mut _) }
}
NativeDisplay::X11(None) | NativeDisplay::Gbm(None) | NativeDisplay::Wayland(None) |
NativeDisplay::Android | NativeDisplay::Other(None) => {
unsafe { egl.GetDisplay(ffi::egl::DEFAULT_DISPLAY as *mut _) }
},
};
if display.is_null() {
return Err(CreationError::OsError("Could not create EGL display object".to_string()));
}
let egl_version = unsafe { let egl_version = unsafe {
let mut major: ffi::egl::types::EGLint = mem::uninitialized(); let mut major: ffi::egl::types::EGLint = mem::uninitialized();
let mut minor: ffi::egl::types::EGLint = mem::uninitialized(); let mut minor: ffi::egl::types::EGLint = mem::uninitialized();
@ -58,6 +149,17 @@ impl Context {
(major, minor) (major, minor)
}; };
// the list of extensions supported by the client once initialized is different from the
// list of extensions obtained earlier
let extensions = if egl_version >= (1, 2) {
let p = unsafe { CStr::from_ptr(egl.QueryString(display, ffi::egl::EXTENSIONS as i32)) };
let list = String::from_utf8(p.to_bytes().to_vec()).unwrap_or_else(|_| format!(""));
list.split(' ').map(|e| e.to_string()).collect::<Vec<_>>()
} else {
vec![]
};
// binding the right API and choosing the version // binding the right API and choosing the version
let (version, api) = unsafe { let (version, api) = unsafe {
match builder.gl_version { match builder.gl_version {
@ -116,6 +218,7 @@ impl Context {
egl: egl, egl: egl,
display: display, display: display,
egl_version: egl_version, egl_version: egl_version,
extensions: extensions,
api: api, api: api,
version: version, version: version,
config_id: config_id, config_id: config_id,
@ -196,6 +299,7 @@ pub struct ContextPrototype<'a> {
egl: ffi::egl::Egl, egl: ffi::egl::Egl,
display: ffi::egl::types::EGLDisplay, display: ffi::egl::types::EGLDisplay,
egl_version: (ffi::egl::types::EGLint, ffi::egl::types::EGLint), egl_version: (ffi::egl::types::EGLint, ffi::egl::types::EGLint),
extensions: Vec<String>,
api: Api, api: Api,
version: Option<(u8, u8)>, version: Option<(u8, u8)>,
config_id: ffi::egl::types::EGLConfig, config_id: ffi::egl::types::EGLConfig,
@ -253,19 +357,19 @@ impl<'a> ContextPrototype<'a> {
{ {
let context = unsafe { let context = unsafe {
if let Some(version) = self.version { if let Some(version) = self.version {
try!(create_context(&self.egl, self.display, &self.egl_version, self.api, try!(create_context(&self.egl, self.display, &self.egl_version,
version, self.config_id, self.builder.gl_debug, &self.extensions, self.api, version, self.config_id,
self.builder.gl_robustness)) self.builder.gl_debug, self.builder.gl_robustness))
} else if self.api == Api::OpenGlEs { } else if self.api == Api::OpenGlEs {
if let Ok(ctxt) = create_context(&self.egl, self.display, &self.egl_version, if let Ok(ctxt) = create_context(&self.egl, self.display, &self.egl_version,
self.api, (2, 0), self.config_id, &self.extensions, self.api, (2, 0), self.config_id,
self.builder.gl_debug, self.builder.gl_robustness) self.builder.gl_debug, self.builder.gl_robustness)
{ {
ctxt ctxt
} else if let Ok(ctxt) = create_context(&self.egl, self.display, &self.egl_version, } else if let Ok(ctxt) = create_context(&self.egl, self.display, &self.egl_version,
self.api, (1, 0), self.config_id, &self.extensions, self.api, (1, 0),
self.builder.gl_debug, self.config_id, self.builder.gl_debug,
self.builder.gl_robustness) self.builder.gl_robustness)
{ {
ctxt ctxt
@ -275,19 +379,19 @@ impl<'a> ContextPrototype<'a> {
} else { } else {
if let Ok(ctxt) = create_context(&self.egl, self.display, &self.egl_version, if let Ok(ctxt) = create_context(&self.egl, self.display, &self.egl_version,
self.api, (3, 2), self.config_id, &self.extensions, self.api, (3, 2), self.config_id,
self.builder.gl_debug, self.builder.gl_robustness) self.builder.gl_debug, self.builder.gl_robustness)
{ {
ctxt ctxt
} else if let Ok(ctxt) = create_context(&self.egl, self.display, &self.egl_version, } else if let Ok(ctxt) = create_context(&self.egl, self.display, &self.egl_version,
self.api, (3, 1), self.config_id, &self.extensions, self.api, (3, 1),
self.builder.gl_debug, self.config_id, self.builder.gl_debug,
self.builder.gl_robustness) self.builder.gl_robustness)
{ {
ctxt ctxt
} else if let Ok(ctxt) = create_context(&self.egl, self.display, &self.egl_version, } else if let Ok(ctxt) = create_context(&self.egl, self.display, &self.egl_version,
self.api, (1, 0), self.config_id, &self.extensions, self.api, (1, 0),
self.builder.gl_debug, self.config_id, self.builder.gl_debug,
self.builder.gl_robustness) self.builder.gl_robustness)
{ {
ctxt ctxt
@ -414,23 +518,16 @@ unsafe fn enumerate_configs(egl: &ffi::egl::Egl, display: ffi::egl::types::EGLDi
unsafe fn create_context(egl: &ffi::egl::Egl, display: ffi::egl::types::EGLDisplay, unsafe fn create_context(egl: &ffi::egl::Egl, display: ffi::egl::types::EGLDisplay,
egl_version: &(ffi::egl::types::EGLint, ffi::egl::types::EGLint), egl_version: &(ffi::egl::types::EGLint, ffi::egl::types::EGLint),
api: Api, version: (u8, u8), config_id: ffi::egl::types::EGLConfig, extensions: &[String], api: Api, version: (u8, u8),
gl_debug: bool, gl_robustness: Robustness) config_id: ffi::egl::types::EGLConfig, gl_debug: bool,
gl_robustness: Robustness)
-> Result<ffi::egl::types::EGLContext, CreationError> -> Result<ffi::egl::types::EGLContext, CreationError>
{ {
let extensions = if egl_version >= &(1, 2) {
let p = CStr::from_ptr(egl.QueryString(display, ffi::egl::EXTENSIONS as i32));
String::from_utf8(p.to_bytes().to_vec()).unwrap_or_else(|_| format!(""))
} else {
format!("")
};
let mut context_attributes = Vec::with_capacity(10); let mut context_attributes = Vec::with_capacity(10);
let mut flags = 0; let mut flags = 0;
if egl_version >= &(1, 5) || if egl_version >= &(1, 5) || extensions.iter().find(|s| s == &"EGL_KHR_create_context")
extensions.contains("EGL_KHR_create_context ") || .is_some()
extensions.ends_with("EGL_KHR_create_context")
{ {
context_attributes.push(ffi::egl::CONTEXT_MAJOR_VERSION as i32); context_attributes.push(ffi::egl::CONTEXT_MAJOR_VERSION as i32);
context_attributes.push(version.0 as i32); context_attributes.push(version.0 as i32);
@ -439,16 +536,15 @@ unsafe fn create_context(egl: &ffi::egl::Egl, display: ffi::egl::types::EGLDispl
// handling robustness // handling robustness
let supports_robustness = egl_version >= &(1, 5) || let supports_robustness = egl_version >= &(1, 5) ||
extensions.contains("EGL_EXT_create_context_robustness ") || extensions.iter()
extensions.ends_with("EGL_EXT_create_context_robustness"); .find(|s| s == &"EGL_EXT_create_context_robustness")
.is_some();
match gl_robustness { match gl_robustness {
Robustness::NotRobust => (), Robustness::NotRobust => (),
Robustness::NoError => { Robustness::NoError => {
if extensions.contains("EGL_KHR_create_context_no_error ") || if extensions.iter().find(|s| s == &"EGL_KHR_create_context_no_error").is_some() {
extensions.ends_with("EGL_KHR_create_context_no_error")
{
context_attributes.push(ffi::egl::CONTEXT_OPENGL_NO_ERROR_KHR as libc::c_int); context_attributes.push(ffi::egl::CONTEXT_OPENGL_NO_ERROR_KHR as libc::c_int);
context_attributes.push(1); context_attributes.push(1);
} }

View file

@ -8,6 +8,7 @@ use self::wayland::core::shell::{ShellSurface, ShellFullscreenMethod};
use libc; use libc;
use api::dlopen; use api::dlopen;
use api::egl;
use api::egl::Context as EglContext; use api::egl::Context as EglContext;
use BuilderAttribs; use BuilderAttribs;
@ -169,7 +170,7 @@ impl Window {
try!(EglContext::new( try!(EglContext::new(
egl, egl,
&builder, &builder,
Some(wayland_context.display.ptr() as *const _)) egl::NativeDisplay::Wayland(Some(wayland_context.display.ptr() as *const _)))
.and_then(|p| p.finish((*shell_surface).ptr() as *const _)) .and_then(|p| p.finish((*shell_surface).ptr() as *const _))
) )
}; };

View file

@ -165,7 +165,8 @@ unsafe fn init(title: Vec<u16>, builder: BuilderAttribs<'static>,
let context = match builder.gl_version { let context = match builder.gl_version {
GlRequest::Specific(Api::OpenGlEs, (major, minor)) => { GlRequest::Specific(Api::OpenGlEs, (major, minor)) => {
if let Some(egl) = egl { if let Some(egl) = egl {
if let Ok(c) = EglContext::new(egl, &builder, Some(ptr::null())) if let Ok(c) = EglContext::new(egl, &builder,
egl::NativeDisplay::Other(Some(ptr::null())))
.and_then(|p| p.finish(real_window.0)) .and_then(|p| p.finish(real_window.0))
{ {
Context::Egl(c) Context::Egl(c)

View file

@ -17,6 +17,7 @@ use GlRequest;
use PixelFormat; use PixelFormat;
use api::glx::Context as GlxContext; use api::glx::Context as GlxContext;
use api::egl;
use api::egl::Context as EglContext; use api::egl::Context as EglContext;
use platform::MonitorID as PlatformMonitorID; use platform::MonitorID as PlatformMonitorID;
@ -335,7 +336,7 @@ impl Window {
let context = match builder.gl_version { let context = match builder.gl_version {
GlRequest::Latest | GlRequest::Specific(Api::OpenGl, _) | GlRequest::GlThenGles { .. } => { GlRequest::Latest | GlRequest::Specific(Api::OpenGl, _) | GlRequest::GlThenGles { .. } => {
if let Some(ref egl) = display.egl { if let Some(ref egl) = display.egl {
Prototype::Egl(try!(EglContext::new(egl.clone(), &builder_clone, Some(display.display as *const _)))) Prototype::Egl(try!(EglContext::new(egl.clone(), &builder_clone, egl::NativeDisplay::X11(Some(display.display as *const _)))))
} else if let Some(ref glx) = display.glx { } else if let Some(ref glx) = display.glx {
Prototype::Glx(try!(GlxContext::new(glx.clone(), &display.xlib, &builder_clone, display.display))) Prototype::Glx(try!(GlxContext::new(glx.clone(), &display.xlib, &builder_clone, display.display)))
} else { } else {
@ -344,7 +345,7 @@ impl Window {
}, },
GlRequest::Specific(Api::OpenGlEs, _) => { GlRequest::Specific(Api::OpenGlEs, _) => {
if let Some(ref egl) = display.egl { if let Some(ref egl) = display.egl {
Prototype::Egl(try!(EglContext::new(egl.clone(), &builder_clone, Some(display.display as *const _)))) Prototype::Egl(try!(EglContext::new(egl.clone(), &builder_clone, egl::NativeDisplay::X11(Some(display.display as *const _)))))
} else { } else {
return Err(CreationError::NotSupported); return Err(CreationError::NotSupported);
} }

View file

@ -14,6 +14,7 @@ use PixelFormat;
use GlContext; use GlContext;
use api::egl::ffi::egl::Egl; use api::egl::ffi::egl::Egl;
use api::egl;
use api::egl::Context as EglContext; use api::egl::Context as EglContext;
use std::ffi::CString; use std::ffi::CString;
@ -90,7 +91,7 @@ impl HeadlessContext {
// if EGL is available, we try using EGL first // if EGL is available, we try using EGL first
// if EGL returns an error, we try the hidden window method // if EGL returns an error, we try the hidden window method
if let &Some(ref egl) = &*EGL { if let &Some(ref egl) = &*EGL {
let context = EglContext::new(egl.0.clone(), &builder, None) let context = EglContext::new(egl.0.clone(), &builder, egl::NativeDisplay::Other(None))
.and_then(|prototype| prototype.finish_pbuffer()) .and_then(|prototype| prototype.finish_pbuffer())
.map(|ctxt| HeadlessContext::EglPbuffer(ctxt)); .map(|ctxt| HeadlessContext::EglPbuffer(ctxt));