Load the EGL library in platform/windows

This commit is contained in:
Pierre Krieger 2015-07-27 09:52:51 +02:00
parent 991b15df87
commit c9b1b91b8c
3 changed files with 78 additions and 26 deletions

View file

@ -32,6 +32,7 @@ use api::wgl;
use api::wgl::Context as WglContext; use api::wgl::Context as WglContext;
use api::egl; use api::egl;
use api::egl::Context as EglContext; use api::egl::Context as EglContext;
use api::egl::ffi::egl::Egl;
pub enum RawContext { pub enum RawContext {
Egl(egl::ffi::egl::types::EGLContext), Egl(egl::ffi::egl::types::EGLContext),
@ -41,9 +42,12 @@ pub enum RawContext {
unsafe impl Send for RawContext {} unsafe impl Send for RawContext {}
unsafe impl Sync for RawContext {} unsafe impl Sync for RawContext {}
pub fn new_window(builder: BuilderAttribs<'static>, builder_sharelists: Option<RawContext>) pub fn new_window(builder: BuilderAttribs<'static>, builder_sharelists: Option<RawContext>,
egl: Option<&Egl>)
-> Result<Window, CreationError> -> Result<Window, CreationError>
{ {
let egl = egl.map(|e| e.clone());
// initializing variables to be sent to the task // initializing variables to be sent to the task
let title = OsStr::new(&builder.title).encode_wide().chain(Some(0).into_iter()) let title = OsStr::new(&builder.title).encode_wide().chain(Some(0).into_iter())
@ -56,7 +60,7 @@ pub fn new_window(builder: BuilderAttribs<'static>, builder_sharelists: Option<R
thread::spawn(move || { thread::spawn(move || {
unsafe { unsafe {
// creating and sending the `Window` // creating and sending the `Window`
match init(title, builder, builder_sharelists) { match init(title, builder, builder_sharelists, egl) {
Ok(w) => tx.send(Ok(w)).ok(), Ok(w) => tx.send(Ok(w)).ok(),
Err(e) => { Err(e) => {
tx.send(Err(e)).ok(); tx.send(Err(e)).ok();
@ -83,7 +87,8 @@ pub fn new_window(builder: BuilderAttribs<'static>, builder_sharelists: Option<R
} }
unsafe fn init(title: Vec<u16>, builder: BuilderAttribs<'static>, unsafe fn init(title: Vec<u16>, builder: BuilderAttribs<'static>,
builder_sharelists: Option<RawContext>) -> Result<Window, CreationError> builder_sharelists: Option<RawContext>, egl: Option<Egl>)
-> Result<Window, CreationError>
{ {
// registering the window class // registering the window class
let class_name = register_window_class(); let class_name = register_window_class();
@ -159,24 +164,7 @@ unsafe fn init(title: Vec<u16>, builder: BuilderAttribs<'static>,
// creating the OpenGL context // creating the OpenGL context
let context = match builder.gl_version { let context = match builder.gl_version {
GlRequest::Specific(Api::OpenGlEs, (major, minor)) => { GlRequest::Specific(Api::OpenGlEs, (major, minor)) => {
// trying to load EGL from the ATI drivers if let Some(egl) = egl {
// TODO: use LoadLibraryA instead
let dll_name = if cfg!(target_pointer_width = "64") {
"atio6axx.dll"
} else {
"atioglxx.dll"
};
let dll_name = OsStr::new(dll_name).encode_wide().chain(Some(0).into_iter())
.collect::<Vec<_>>();
let dll = unsafe { kernel32::LoadLibraryW(dll_name.as_ptr()) };
if !dll.is_null() {
let egl = ::api::egl::ffi::egl::Egl::load_with(|name| {
let name = CString::new(name).unwrap();
unsafe { kernel32::GetProcAddress(dll, name.as_ptr()) as *const libc::c_void }
});
if let Ok(c) = EglContext::new(egl, &builder, Some(ptr::null())) if let Ok(c) = EglContext::new(egl, &builder, Some(ptr::null()))
.and_then(|p| p.finish(real_window.0)) .and_then(|p| p.finish(real_window.0))
{ {

View file

@ -29,6 +29,7 @@ use kernel32;
use api::wgl; use api::wgl;
use api::wgl::Context as WglContext; use api::wgl::Context as WglContext;
use api::egl::Context as EglContext; use api::egl::Context as EglContext;
use api::egl::ffi::egl::Egl;
use self::init::RawContext; use self::init::RawContext;
@ -84,7 +85,7 @@ impl WindowProxy {
impl Window { impl Window {
/// See the docs in the crate root file. /// See the docs in the crate root file.
pub fn new(builder: BuilderAttribs) -> Result<Window, CreationError> { pub fn new(builder: BuilderAttribs, egl: Option<&Egl>) -> Result<Window, CreationError> {
let (builder, sharing) = builder.extract_non_static(); let (builder, sharing) = builder.extract_non_static();
let sharing = sharing.map(|w| match w.context { let sharing = sharing.map(|w| match w.context {
@ -92,7 +93,7 @@ impl Window {
Context::Egl(_) => unimplemented!(), // FIXME: Context::Egl(_) => unimplemented!(), // FIXME:
}); });
init::new_window(builder, sharing) init::new_window(builder, sharing, egl)
} }
/// See the docs in the crate root file. /// See the docs in the crate root file.

View file

@ -1,6 +1,8 @@
#![cfg(target_os = "windows")] #![cfg(target_os = "windows")]
pub use api::win32::*; pub use api::win32;
pub use api::win32::{MonitorID, get_available_monitors, get_primary_monitor};
pub use api::win32::{WindowProxy, PollEventsIterator, WaitEventsIterator};
use libc; use libc;
@ -11,13 +13,74 @@ use CreationError;
use PixelFormat; use PixelFormat;
use GlContext; use GlContext;
use api::egl::ffi::egl::Egl;
use std::ffi::CString;
use std::ops::{Deref, DerefMut};
use kernel32;
/// Stupid wrapper because `*const libc::c_void` doesn't implement `Sync`.
struct EglWrapper(Egl);
unsafe impl Sync for EglWrapper {}
lazy_static! {
// An EGL implementation available on the system.
static ref EGL: Option<EglWrapper> = {
// the ATI drivers provide an EGL implementation in their DLLs
let dll_name = if cfg!(target_pointer_width = "64") {
b"atio6axx.dll\0"
} else {
b"atioglxx.dll\0"
};
let dll = unsafe { kernel32::LoadLibraryA(dll_name.as_ptr() as *const _) };
if !dll.is_null() {
let egl = Egl::load_with(|name| {
let name = CString::new(name).unwrap();
unsafe { kernel32::GetProcAddress(dll, name.as_ptr()) as *const _ }
});
Some(EglWrapper(egl))
} else {
None
}
};
}
/// The Win32 implementation of the main `Window` object.
pub struct Window(win32::Window);
impl Window {
/// See the docs in the crate root file.
pub fn new(builder: BuilderAttribs) -> Result<Window, CreationError> {
win32::Window::new(builder, EGL.as_ref().map(|w| &w.0)).map(|w| Window(w))
}
}
impl Deref for Window {
type Target = win32::Window;
fn deref(&self) -> &win32::Window {
&self.0
}
}
impl DerefMut for Window {
fn deref_mut(&mut self) -> &mut win32::Window {
&mut self.0
}
}
/// ///
pub struct HeadlessContext(Window); pub struct HeadlessContext(win32::Window);
impl HeadlessContext { impl HeadlessContext {
pub fn new(mut builder: BuilderAttribs) -> Result<HeadlessContext, CreationError> { pub fn new(mut builder: BuilderAttribs) -> Result<HeadlessContext, CreationError> {
builder.visible = false; builder.visible = false;
Window::new(builder).map(|w| HeadlessContext(w)) win32::Window::new(builder, EGL.as_ref().map(|w| &w.0)).map(|w| HeadlessContext(w))
} }
} }