Switch EGL to use only pf_reqs and opengl

This commit is contained in:
Pierre Krieger 2015-09-21 10:11:32 +02:00
parent 3ff08d747c
commit 3054e2ee02
7 changed files with 58 additions and 48 deletions

View file

@ -112,7 +112,8 @@ 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, egl::NativeDisplay::Android) let context = try!(EglContext::new(egl::ffi::egl::Egl, &builder.pf_reqs, &builder.opengl,
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();
@ -256,7 +257,7 @@ 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, egl::NativeDisplay::Android)); 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(builder.window.dimensions.unwrap_or((800, 600)))); // TODO:
Ok(HeadlessContext(context)) Ok(HeadlessContext(context))
} }
} }

View file

@ -2,12 +2,13 @@
target_os = "dragonfly", target_os = "freebsd"))] target_os = "dragonfly", target_os = "freebsd"))]
#![allow(unused_variables)] #![allow(unused_variables)]
use BuilderAttribs;
use ContextError; use ContextError;
use CreationError; use CreationError;
use GlAttributes;
use GlContext; use GlContext;
use GlRequest; use GlRequest;
use PixelFormat; use PixelFormat;
use PixelFormatRequirements;
use Robustness; use Robustness;
use Api; use Api;
@ -158,11 +159,11 @@ impl Context {
/// This function initializes some things and chooses the pixel format. /// This function initializes some things and chooses the pixel format.
/// ///
/// 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, pf_reqs: &PixelFormatRequirements,
native_display: NativeDisplay) opengl: &'a GlAttributes<&'a Context>, native_display: NativeDisplay)
-> Result<ContextPrototype<'a>, CreationError> -> Result<ContextPrototype<'a>, CreationError>
{ {
if builder.opengl.sharing.is_some() { if opengl.sharing.is_some() {
unimplemented!() unimplemented!()
} }
@ -197,7 +198,7 @@ impl Context {
// 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.opengl.version { match opengl.version {
GlRequest::Latest => { GlRequest::Latest => {
if egl_version >= (1, 4) { if egl_version >= (1, 4) {
if egl.BindAPI(ffi::egl::OPENGL_API) != 0 { if egl.BindAPI(ffi::egl::OPENGL_API) != 0 {
@ -246,10 +247,10 @@ impl Context {
}; };
let configs = unsafe { try!(enumerate_configs(&egl, display, &egl_version, api, version)) }; 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())); let (config_id, pixel_format) = try!(pf_reqs.choose_pixel_format(configs.into_iter()));
Ok(ContextPrototype { Ok(ContextPrototype {
builder: builder, opengl: opengl,
egl: egl, egl: egl,
display: display, display: display,
egl_version: egl_version, egl_version: egl_version,
@ -330,7 +331,7 @@ impl Drop for Context {
} }
pub struct ContextPrototype<'a> { pub struct ContextPrototype<'a> {
builder: &'a BuilderAttribs<'a>, opengl: &'a GlAttributes<&'a Context>,
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),
@ -366,9 +367,7 @@ impl<'a> ContextPrototype<'a> {
self.finish_impl(surface) self.finish_impl(surface)
} }
pub fn finish_pbuffer(self) -> Result<Context, CreationError> { pub fn finish_pbuffer(self, dimensions: (u32, u32)) -> Result<Context, CreationError> {
let dimensions = self.builder.window.dimensions.unwrap_or((800, 600));
let attrs = &[ let attrs = &[
ffi::egl::WIDTH as libc::c_int, dimensions.0 as libc::c_int, ffi::egl::WIDTH as libc::c_int, dimensions.0 as libc::c_int,
ffi::egl::HEIGHT as libc::c_int, dimensions.1 as libc::c_int, ffi::egl::HEIGHT as libc::c_int, dimensions.1 as libc::c_int,
@ -394,18 +393,18 @@ impl<'a> ContextPrototype<'a> {
if let Some(version) = self.version { if let Some(version) = self.version {
try!(create_context(&self.egl, self.display, &self.egl_version, try!(create_context(&self.egl, self.display, &self.egl_version,
&self.extensions, self.api, version, self.config_id, &self.extensions, self.api, version, self.config_id,
self.builder.opengl.debug, self.builder.opengl.robustness)) self.opengl.debug, self.opengl.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.extensions, self.api, (2, 0), self.config_id, &self.extensions, self.api, (2, 0), self.config_id,
self.builder.opengl.debug, self.builder.opengl.robustness) self.opengl.debug, self.opengl.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.extensions, self.api, (1, 0), &self.extensions, self.api, (1, 0),
self.config_id, self.builder.opengl.debug, self.config_id, self.opengl.debug,
self.builder.opengl.robustness) self.opengl.robustness)
{ {
ctxt ctxt
} else { } else {
@ -415,19 +414,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.extensions, self.api, (3, 2), self.config_id, &self.extensions, self.api, (3, 2), self.config_id,
self.builder.opengl.debug, self.builder.opengl.robustness) self.opengl.debug, self.opengl.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.extensions, self.api, (3, 1), &self.extensions, self.api, (3, 1),
self.config_id, self.builder.opengl.debug, self.config_id, self.opengl.debug,
self.builder.opengl.robustness) self.opengl.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.extensions, self.api, (1, 0), &self.extensions, self.api, (1, 0),
self.config_id, self.builder.opengl.debug, self.config_id, self.opengl.debug,
self.builder.opengl.robustness) self.opengl.robustness)
{ {
ctxt ctxt
} else { } else {

View file

@ -291,7 +291,7 @@ impl Window {
}); });
try!(EglContext::new( try!(EglContext::new(
egl, egl,
&builder, &builder.pf_reqs, &builder.opengl.clone().map_sharing(|_| unimplemented!()), // TODO:
egl::NativeDisplay::Wayland(Some(wayland_context.display.ptr() as *const _))) egl::NativeDisplay::Wayland(Some(wayland_context.display.ptr() as *const _)))
.and_then(|p| p.finish((**shell_window.get_shell()).ptr() as *const _)) .and_then(|p| p.finish((**shell_window.get_shell()).ptr() as *const _))
) )

View file

@ -162,7 +162,7 @@ unsafe fn init(title: Vec<u16>, builder: BuilderAttribs<'static>,
let context = match builder.opengl.version { let context = match builder.opengl.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, if let Ok(c) = EglContext::new(egl, &builder.pf_reqs, &builder.opengl.clone().map_sharing(|_| unimplemented!()),
egl::NativeDisplay::Other(Some(ptr::null()))) egl::NativeDisplay::Other(Some(ptr::null())))
.and_then(|p| p.finish(real_window.0)) .and_then(|p| p.finish(real_window.0))
{ {

View file

@ -349,6 +349,7 @@ impl Window {
Egl(::api::egl::ContextPrototype<'a>), Egl(::api::egl::ContextPrototype<'a>),
} }
let builder_clone = builder.clone(); let builder_clone = builder.clone();
let builder_clone_opengl = builder_clone.opengl.clone().map_sharing(|_| unimplemented!());
let context = match builder.opengl.version { let context = match builder.opengl.version {
GlRequest::Latest | GlRequest::Specific(Api::OpenGl, _) | GlRequest::GlThenGles { .. } => { GlRequest::Latest | GlRequest::Specific(Api::OpenGl, _) | GlRequest::GlThenGles { .. } => {
// GLX should be preferred over EGL, otherwise crashes may occur // GLX should be preferred over EGL, otherwise crashes may occur
@ -356,14 +357,14 @@ impl Window {
if let Some(ref glx) = display.glx { 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 if let Some(ref egl) = display.egl { } else if let Some(ref egl) = display.egl {
Prototype::Egl(try!(EglContext::new(egl.clone(), &builder_clone, egl::NativeDisplay::X11(Some(display.display as *const _))))) Prototype::Egl(try!(EglContext::new(egl.clone(), &builder_clone.pf_reqs, &builder_clone_opengl, egl::NativeDisplay::X11(Some(display.display as *const _)))))
} else { } else {
return Err(CreationError::NotSupported); return Err(CreationError::NotSupported);
} }
}, },
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, egl::NativeDisplay::X11(Some(display.display as *const _))))) Prototype::Egl(try!(EglContext::new(egl.clone(), &builder_clone.pf_reqs, &builder_clone_opengl, egl::NativeDisplay::X11(Some(display.display as *const _)))))
} else { } else {
return Err(CreationError::NotSupported); return Err(CreationError::NotSupported);
} }

View file

@ -411,32 +411,53 @@ impl<'a> BuilderAttribs<'a> {
(new_attribs, sharing) (new_attribs, sharing)
} }
fn choose_pixel_format<T, I>(&self, iter: I) -> Result<(T, PixelFormat), CreationError>
where I: IntoIterator<Item=(T, PixelFormat)>, T: Clone
{
self.pf_reqs.choose_pixel_format(iter)
}
}
/// VERY UNSTABLE! Describes how the backend should choose a pixel format.
#[derive(Clone, Debug)]
#[allow(missing_docs)]
pub struct PixelFormatRequirements {
pub multisampling: Option<u16>,
pub depth_bits: Option<u8>,
pub stencil_bits: Option<u8>,
pub color_bits: Option<u8>,
pub alpha_bits: Option<u8>,
pub stereoscopy: bool,
pub srgb: Option<bool>,
}
impl PixelFormatRequirements {
fn choose_pixel_format<T, I>(&self, iter: I) -> Result<(T, PixelFormat), CreationError> fn choose_pixel_format<T, I>(&self, iter: I) -> Result<(T, PixelFormat), CreationError>
where I: IntoIterator<Item=(T, PixelFormat)>, T: Clone where I: IntoIterator<Item=(T, PixelFormat)>, T: Clone
{ {
// filtering formats that don't match the requirements // filtering formats that don't match the requirements
let iter = iter.into_iter().filter(|&(_, ref format)| { let iter = iter.into_iter().filter(|&(_, ref format)| {
if format.color_bits < self.pf_reqs.color_bits.unwrap_or(0) { if format.color_bits < self.color_bits.unwrap_or(0) {
return false; return false;
} }
if format.alpha_bits < self.pf_reqs.alpha_bits.unwrap_or(0) { if format.alpha_bits < self.alpha_bits.unwrap_or(0) {
return false; return false;
} }
if format.depth_bits < self.pf_reqs.depth_bits.unwrap_or(0) { if format.depth_bits < self.depth_bits.unwrap_or(0) {
return false; return false;
} }
if format.stencil_bits < self.pf_reqs.stencil_bits.unwrap_or(0) { if format.stencil_bits < self.stencil_bits.unwrap_or(0) {
return false; return false;
} }
if !format.stereoscopy && self.pf_reqs.stereoscopy { if !format.stereoscopy && self.stereoscopy {
return false; return false;
} }
if let Some(req_ms) = self.pf_reqs.multisampling { if let Some(req_ms) = self.multisampling {
match format.multisampling { match format.multisampling {
Some(val) if val >= req_ms => (), Some(val) if val >= req_ms => (),
_ => return false _ => return false
@ -447,7 +468,7 @@ impl<'a> BuilderAttribs<'a> {
} }
} }
if let Some(srgb) = self.pf_reqs.srgb { if let Some(srgb) = self.srgb {
if srgb != format.srgb { if srgb != format.srgb {
return false; return false;
} }
@ -511,19 +532,6 @@ impl<'a> BuilderAttribs<'a> {
} }
} }
/// VERY UNSTABLE! Describes how the backend should choose a pixel format.
#[derive(Clone, Debug)]
#[allow(missing_docs)]
pub struct PixelFormatRequirements {
pub multisampling: Option<u16>,
pub depth_bits: Option<u8>,
pub stencil_bits: Option<u8>,
pub color_bits: Option<u8>,
pub alpha_bits: Option<u8>,
pub stereoscopy: bool,
pub srgb: Option<bool>,
}
impl Default for PixelFormatRequirements { impl Default for PixelFormatRequirements {
fn default() -> PixelFormatRequirements { fn default() -> PixelFormatRequirements {
PixelFormatRequirements { PixelFormatRequirements {

View file

@ -91,8 +91,9 @@ 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, egl::NativeDisplay::Other(None)) let context = EglContext::new(egl.0.clone(), &builder.pf_reqs, &builder.opengl.clone().map_sharing(|_| unimplemented!()), // TODO:
.and_then(|prototype| prototype.finish_pbuffer()) egl::NativeDisplay::Other(None))
.and_then(|prototype| prototype.finish_pbuffer(builder.window.dimensions.unwrap_or((800, 600)))) // TODO:
.map(|ctxt| HeadlessContext::EglPbuffer(ctxt)); .map(|ctxt| HeadlessContext::EglPbuffer(ctxt));
if let Ok(context) = context { if let Ok(context) = context {