2015-04-24 17:51:23 +10:00
|
|
|
#![cfg(target_os = "windows")]
|
|
|
|
|
2015-07-27 17:52:51 +10:00
|
|
|
pub use api::win32;
|
2015-09-24 17:11:59 +10:00
|
|
|
pub use api::win32::{MonitorId, get_available_monitors, get_primary_monitor};
|
2015-07-27 17:52:51 +10:00
|
|
|
pub use api::win32::{WindowProxy, PollEventsIterator, WaitEventsIterator};
|
2015-04-29 18:19:59 +10:00
|
|
|
|
|
|
|
use Api;
|
2015-06-16 18:15:31 +10:00
|
|
|
use ContextError;
|
2015-04-29 18:19:59 +10:00
|
|
|
use CreationError;
|
2015-04-30 21:23:37 +10:00
|
|
|
use PixelFormat;
|
2015-09-21 21:15:43 +10:00
|
|
|
use PixelFormatRequirements;
|
|
|
|
use GlAttributes;
|
2015-04-30 21:23:37 +10:00
|
|
|
use GlContext;
|
2015-09-21 21:15:43 +10:00
|
|
|
use WindowAttributes;
|
2015-04-29 18:19:59 +10:00
|
|
|
|
2015-07-27 17:52:51 +10:00
|
|
|
use api::egl::ffi::egl::Egl;
|
2015-08-08 00:22:31 +10:00
|
|
|
use api::egl;
|
2015-07-27 18:13:00 +10:00
|
|
|
use api::egl::Context as EglContext;
|
2015-07-27 17:52:51 +10:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2016-01-08 02:01:18 +11:00
|
|
|
#[derive(Default)]
|
|
|
|
pub struct PlatformSpecificWindowBuilderAttributes;
|
|
|
|
#[derive(Default)]
|
|
|
|
pub struct PlatformSpecificHeadlessBuilderAttributes;
|
2015-07-27 17:52:51 +10:00
|
|
|
|
|
|
|
/// The Win32 implementation of the main `Window` object.
|
|
|
|
pub struct Window(win32::Window);
|
|
|
|
|
|
|
|
impl Window {
|
|
|
|
/// See the docs in the crate root file.
|
2015-09-21 22:42:05 +10:00
|
|
|
#[inline]
|
2015-09-21 21:15:43 +10:00
|
|
|
pub fn new(window: &WindowAttributes, pf_reqs: &PixelFormatRequirements,
|
2016-01-08 02:01:18 +11:00
|
|
|
opengl: &GlAttributes<&Window>, _: &PlatformSpecificWindowBuilderAttributes)
|
|
|
|
-> Result<Window, CreationError>
|
2015-09-21 21:15:43 +10:00
|
|
|
{
|
|
|
|
win32::Window::new(window, pf_reqs, &opengl.clone().map_sharing(|w| &w.0),
|
2015-09-21 19:11:11 +10:00
|
|
|
EGL.as_ref().map(|w| &w.0)).map(|w| Window(w))
|
2015-07-27 17:52:51 +10:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Deref for Window {
|
|
|
|
type Target = win32::Window;
|
|
|
|
|
2015-09-21 22:42:05 +10:00
|
|
|
#[inline]
|
2015-07-27 17:52:51 +10:00
|
|
|
fn deref(&self) -> &win32::Window {
|
|
|
|
&self.0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl DerefMut for Window {
|
2015-09-21 22:42:05 +10:00
|
|
|
#[inline]
|
2015-07-27 17:52:51 +10:00
|
|
|
fn deref_mut(&mut self) -> &mut win32::Window {
|
|
|
|
&mut self.0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-29 18:19:59 +10:00
|
|
|
///
|
2015-07-27 18:13:00 +10:00
|
|
|
pub enum HeadlessContext {
|
|
|
|
/// A regular window, but invisible.
|
|
|
|
HiddenWindow(win32::Window),
|
|
|
|
/// An EGL pbuffer.
|
|
|
|
EglPbuffer(EglContext),
|
|
|
|
}
|
2015-04-29 18:19:59 +10:00
|
|
|
|
|
|
|
impl HeadlessContext {
|
2015-09-21 21:15:43 +10:00
|
|
|
pub fn new(dimensions: (u32, u32), pf_reqs: &PixelFormatRequirements,
|
2016-01-08 02:01:18 +11:00
|
|
|
opengl: &GlAttributes<&HeadlessContext>,
|
|
|
|
_: &PlatformSpecificHeadlessBuilderAttributes)
|
|
|
|
-> Result<HeadlessContext, CreationError>
|
2015-09-21 21:15:43 +10:00
|
|
|
{
|
2015-07-27 18:13:00 +10:00
|
|
|
// 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 {
|
2015-09-21 21:15:43 +10:00
|
|
|
let context = EglContext::new(egl.0.clone(), pf_reqs, &opengl.clone().map_sharing(|_| unimplemented!()), // TODO:
|
2015-09-21 18:11:32 +10:00
|
|
|
egl::NativeDisplay::Other(None))
|
2015-09-21 21:15:43 +10:00
|
|
|
.and_then(|prototype| prototype.finish_pbuffer(dimensions))
|
2015-07-27 18:13:00 +10:00
|
|
|
.map(|ctxt| HeadlessContext::EglPbuffer(ctxt));
|
|
|
|
|
|
|
|
if let Ok(context) = context {
|
|
|
|
return Ok(context);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-21 21:15:43 +10:00
|
|
|
let window = try!(win32::Window::new(&WindowAttributes { visible: false, .. Default::default() },
|
|
|
|
pf_reqs, &opengl.clone().map_sharing(|_| unimplemented!()), //TODO:
|
2015-09-21 19:11:11 +10:00
|
|
|
EGL.as_ref().map(|w| &w.0)));
|
2015-07-27 18:13:00 +10:00
|
|
|
Ok(HeadlessContext::HiddenWindow(window))
|
2015-04-29 18:19:59 +10:00
|
|
|
}
|
2015-04-30 21:23:37 +10:00
|
|
|
}
|
2015-04-29 18:19:59 +10:00
|
|
|
|
2015-04-30 21:23:37 +10:00
|
|
|
impl GlContext for HeadlessContext {
|
2015-09-21 22:42:05 +10:00
|
|
|
#[inline]
|
2015-06-16 18:15:31 +10:00
|
|
|
unsafe fn make_current(&self) -> Result<(), ContextError> {
|
2015-07-27 18:13:00 +10:00
|
|
|
match self {
|
|
|
|
&HeadlessContext::HiddenWindow(ref ctxt) => ctxt.make_current(),
|
|
|
|
&HeadlessContext::EglPbuffer(ref ctxt) => ctxt.make_current(),
|
|
|
|
}
|
2015-04-29 18:19:59 +10:00
|
|
|
}
|
|
|
|
|
2015-09-21 22:42:05 +10:00
|
|
|
#[inline]
|
2015-04-30 21:23:37 +10:00
|
|
|
fn is_current(&self) -> bool {
|
2015-07-27 18:13:00 +10:00
|
|
|
match self {
|
|
|
|
&HeadlessContext::HiddenWindow(ref ctxt) => ctxt.is_current(),
|
|
|
|
&HeadlessContext::EglPbuffer(ref ctxt) => ctxt.is_current(),
|
|
|
|
}
|
2015-04-29 18:19:59 +10:00
|
|
|
}
|
|
|
|
|
2015-09-21 22:42:05 +10:00
|
|
|
#[inline]
|
2015-11-04 21:27:50 +11:00
|
|
|
fn get_proc_address(&self, addr: &str) -> *const () {
|
2015-07-27 18:13:00 +10:00
|
|
|
match self {
|
|
|
|
&HeadlessContext::HiddenWindow(ref ctxt) => ctxt.get_proc_address(addr),
|
|
|
|
&HeadlessContext::EglPbuffer(ref ctxt) => ctxt.get_proc_address(addr),
|
|
|
|
}
|
2015-04-29 18:19:59 +10:00
|
|
|
}
|
|
|
|
|
2015-09-21 22:42:05 +10:00
|
|
|
#[inline]
|
2015-06-16 18:15:31 +10:00
|
|
|
fn swap_buffers(&self) -> Result<(), ContextError> {
|
2015-07-27 18:13:00 +10:00
|
|
|
match self {
|
|
|
|
&HeadlessContext::HiddenWindow(ref ctxt) => ctxt.swap_buffers(),
|
|
|
|
&HeadlessContext::EglPbuffer(ref ctxt) => ctxt.swap_buffers(),
|
|
|
|
}
|
2015-04-30 21:23:37 +10:00
|
|
|
}
|
|
|
|
|
2015-09-21 22:42:05 +10:00
|
|
|
#[inline]
|
2015-04-30 21:23:37 +10:00
|
|
|
fn get_api(&self) -> Api {
|
2015-07-27 18:13:00 +10:00
|
|
|
match self {
|
|
|
|
&HeadlessContext::HiddenWindow(ref ctxt) => ctxt.get_api(),
|
|
|
|
&HeadlessContext::EglPbuffer(ref ctxt) => ctxt.get_api(),
|
|
|
|
}
|
2015-04-29 18:19:59 +10:00
|
|
|
}
|
2015-04-30 21:23:37 +10:00
|
|
|
|
2015-09-21 22:42:05 +10:00
|
|
|
#[inline]
|
2015-04-30 21:23:37 +10:00
|
|
|
fn get_pixel_format(&self) -> PixelFormat {
|
2015-07-27 18:13:00 +10:00
|
|
|
match self {
|
|
|
|
&HeadlessContext::HiddenWindow(ref ctxt) => ctxt.get_pixel_format(),
|
|
|
|
&HeadlessContext::EglPbuffer(ref ctxt) => ctxt.get_pixel_format(),
|
|
|
|
}
|
2015-04-30 21:23:37 +10:00
|
|
|
}
|
2015-04-29 18:19:59 +10:00
|
|
|
}
|