Correctly handle eglGetDisplay

This commit is contained in:
Pierre Krieger 2015-08-07 16:22:31 +02:00
parent bee3e0e0f0
commit 587dedaa9e
7 changed files with 139 additions and 23 deletions

View file

@ -16,6 +16,22 @@ use std::{mem, ptr};
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 {
egl: ffi::egl::Egl,
display: ffi::egl::types::EGLDisplay,
@ -32,7 +48,7 @@ impl Context {
///
/// To finish the process, you must call `.finish(window)` on the `ContextPrototype`.
pub fn new<'a>(egl: ffi::egl::Egl, builder: &'a BuilderAttribs<'a>,
native_display: Option<ffi::EGLNativeDisplayType>)
native_display: NativeDisplay)
-> Result<ContextPrototype<'a>, CreationError>
{
if builder.sharing.is_some() {
@ -40,28 +56,88 @@ impl Context {
}
// the first step is to query the list of extensions without any display, if supported
let extensions = unsafe {
let dp_extensions = unsafe {
let p = egl.QueryString(ffi::egl::NO_DISPLAY, ffi::egl::EXTENSIONS as i32);
// this possibility is available only with EGL 1.5 or EGL_EXT_platform_base, otherwise
// `eglQueryString` returns an error
if p.is_null() {
None
vec![]
} else {
let p = CStr::from_ptr(p);
let list = String::from_utf8(p.to_bytes().to_vec()).unwrap_or_else(|_| format!(""));
Some(list.split(' ').map(|e| e.to_string()).collect::<Vec<_>>())
list.split(' ').map(|e| e.to_string()).collect::<Vec<_>>()
}
};
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()));
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 _) }
}
display
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 mut major: ffi::egl::types::EGLint = mem::uninitialized();
let mut minor: ffi::egl::types::EGLint = mem::uninitialized();
@ -73,11 +149,9 @@ impl Context {
(major, minor)
};
// getting the list of extensions for real
let extensions = if let Some(extensions) = extensions {
extensions
} else if egl_version >= (1, 2) {
// 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<_>>()