From c9b1b91b8cb7e43c0908c04afaefb8e49e0f2b35 Mon Sep 17 00:00:00 2001 From: Pierre Krieger Date: Mon, 27 Jul 2015 09:52:51 +0200 Subject: Load the EGL library in platform/windows --- src/api/win32/init.rs | 30 ++++++-------------- src/api/win32/mod.rs | 5 ++-- src/platform/windows/mod.rs | 69 +++++++++++++++++++++++++++++++++++++++++++-- 3 files changed, 78 insertions(+), 26 deletions(-) (limited to 'src') diff --git a/src/api/win32/init.rs b/src/api/win32/init.rs index fdb88c9..e6f0648 100644 --- a/src/api/win32/init.rs +++ b/src/api/win32/init.rs @@ -32,6 +32,7 @@ use api::wgl; use api::wgl::Context as WglContext; use api::egl; use api::egl::Context as EglContext; +use api::egl::ffi::egl::Egl; pub enum RawContext { Egl(egl::ffi::egl::types::EGLContext), @@ -41,9 +42,12 @@ pub enum RawContext { unsafe impl Send for RawContext {} unsafe impl Sync for RawContext {} -pub fn new_window(builder: BuilderAttribs<'static>, builder_sharelists: Option) +pub fn new_window(builder: BuilderAttribs<'static>, builder_sharelists: Option, + egl: Option<&Egl>) -> Result { + let egl = egl.map(|e| e.clone()); + // initializing variables to be sent to the task 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 tx.send(Ok(w)).ok(), Err(e) => { tx.send(Err(e)).ok(); @@ -83,7 +87,8 @@ pub fn new_window(builder: BuilderAttribs<'static>, builder_sharelists: Option, builder: BuilderAttribs<'static>, - builder_sharelists: Option) -> Result + builder_sharelists: Option, egl: Option) + -> Result { // registering the window class let class_name = register_window_class(); @@ -159,24 +164,7 @@ unsafe fn init(title: Vec, builder: BuilderAttribs<'static>, // creating the OpenGL context let context = match builder.gl_version { GlRequest::Specific(Api::OpenGlEs, (major, minor)) => { - // trying to load EGL from the ATI drivers - - // 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::>(); - 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 Some(egl) = egl { if let Ok(c) = EglContext::new(egl, &builder, Some(ptr::null())) .and_then(|p| p.finish(real_window.0)) { diff --git a/src/api/win32/mod.rs b/src/api/win32/mod.rs index d21f16d..9b03f64 100644 --- a/src/api/win32/mod.rs +++ b/src/api/win32/mod.rs @@ -29,6 +29,7 @@ use kernel32; use api::wgl; use api::wgl::Context as WglContext; use api::egl::Context as EglContext; +use api::egl::ffi::egl::Egl; use self::init::RawContext; @@ -84,7 +85,7 @@ impl WindowProxy { impl Window { /// See the docs in the crate root file. - pub fn new(builder: BuilderAttribs) -> Result { + pub fn new(builder: BuilderAttribs, egl: Option<&Egl>) -> Result { let (builder, sharing) = builder.extract_non_static(); let sharing = sharing.map(|w| match w.context { @@ -92,7 +93,7 @@ impl Window { Context::Egl(_) => unimplemented!(), // FIXME: }); - init::new_window(builder, sharing) + init::new_window(builder, sharing, egl) } /// See the docs in the crate root file. diff --git a/src/platform/windows/mod.rs b/src/platform/windows/mod.rs index b33b8ee..c062dfc 100644 --- a/src/platform/windows/mod.rs +++ b/src/platform/windows/mod.rs @@ -1,6 +1,8 @@ #![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; @@ -11,13 +13,74 @@ use CreationError; use PixelFormat; 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 = { + // 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 { + 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 { pub fn new(mut builder: BuilderAttribs) -> Result { 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)) } } -- cgit v1.2.3 From f7cc80fbb1607d9af05476dfe5bbb404c55b1b69 Mon Sep 17 00:00:00 2001 From: Pierre Krieger Date: Mon, 27 Jul 2015 10:13:00 +0200 Subject: Headless contexts now create an EGL pbuffer on win32 --- src/platform/windows/mod.rs | 54 ++++++++++++++++++++++++++++++++++++++------- 1 file changed, 46 insertions(+), 8 deletions(-) (limited to 'src') diff --git a/src/platform/windows/mod.rs b/src/platform/windows/mod.rs index c062dfc..cf63784 100644 --- a/src/platform/windows/mod.rs +++ b/src/platform/windows/mod.rs @@ -14,6 +14,7 @@ use PixelFormat; use GlContext; use api::egl::ffi::egl::Egl; +use api::egl::Context as EglContext; use std::ffi::CString; use std::ops::{Deref, DerefMut}; @@ -75,37 +76,74 @@ impl DerefMut for Window { } /// -pub struct HeadlessContext(win32::Window); +pub enum HeadlessContext { + /// A regular window, but invisible. + HiddenWindow(win32::Window), + /// An EGL pbuffer. + EglPbuffer(EglContext), +} impl HeadlessContext { pub fn new(mut builder: BuilderAttribs) -> Result { builder.visible = false; - win32::Window::new(builder, EGL.as_ref().map(|w| &w.0)).map(|w| HeadlessContext(w)) + + // if EGL is available, we try using EGL first + // if EGL returns an error, we try the hidden window method + if let &Some(ref egl) = &*EGL { + let context = EglContext::new(egl.0.clone(), &builder, None) + .and_then(|prototype| prototype.finish_pbuffer()) + .map(|ctxt| HeadlessContext::EglPbuffer(ctxt)); + + if let Ok(context) = context { + return Ok(context); + } + } + + let window = try!(win32::Window::new(builder, EGL.as_ref().map(|w| &w.0))); + Ok(HeadlessContext::HiddenWindow(window)) } } impl GlContext for HeadlessContext { unsafe fn make_current(&self) -> Result<(), ContextError> { - self.0.make_current() + match self { + &HeadlessContext::HiddenWindow(ref ctxt) => ctxt.make_current(), + &HeadlessContext::EglPbuffer(ref ctxt) => ctxt.make_current(), + } } fn is_current(&self) -> bool { - self.0.is_current() + match self { + &HeadlessContext::HiddenWindow(ref ctxt) => ctxt.is_current(), + &HeadlessContext::EglPbuffer(ref ctxt) => ctxt.is_current(), + } } fn get_proc_address(&self, addr: &str) -> *const libc::c_void { - self.0.get_proc_address(addr) + match self { + &HeadlessContext::HiddenWindow(ref ctxt) => ctxt.get_proc_address(addr), + &HeadlessContext::EglPbuffer(ref ctxt) => ctxt.get_proc_address(addr), + } } fn swap_buffers(&self) -> Result<(), ContextError> { - self.0.swap_buffers() + match self { + &HeadlessContext::HiddenWindow(ref ctxt) => ctxt.swap_buffers(), + &HeadlessContext::EglPbuffer(ref ctxt) => ctxt.swap_buffers(), + } } fn get_api(&self) -> Api { - self.0.get_api() + match self { + &HeadlessContext::HiddenWindow(ref ctxt) => ctxt.get_api(), + &HeadlessContext::EglPbuffer(ref ctxt) => ctxt.get_api(), + } } fn get_pixel_format(&self) -> PixelFormat { - self.0.get_pixel_format() + match self { + &HeadlessContext::HiddenWindow(ref ctxt) => ctxt.get_pixel_format(), + &HeadlessContext::EglPbuffer(ref ctxt) => ctxt.get_pixel_format(), + } } } -- cgit v1.2.3