Switch WGL and part of win32 to the new system

This commit is contained in:
Pierre Krieger 2015-09-21 10:55:41 +02:00
parent 3054e2ee02
commit 1b836f6040
3 changed files with 66 additions and 64 deletions

View file

@ -1,12 +1,13 @@
#![cfg(any(target_os = "windows"))] #![cfg(any(target_os = "windows"))]
use BuilderAttribs;
use ContextError; use ContextError;
use CreationError; use CreationError;
use GlAttributes;
use GlContext; use GlContext;
use GlRequest; use GlRequest;
use GlProfile; use GlProfile;
use PixelFormat; use PixelFormat;
use PixelFormatRequirements;
use Robustness; use Robustness;
use Api; use Api;
@ -74,9 +75,8 @@ impl Context {
/// # Unsafety /// # Unsafety
/// ///
/// The `window` must continue to exist as long as the resulting `Context` exists. /// The `window` must continue to exist as long as the resulting `Context` exists.
pub unsafe fn new(builder: &BuilderAttribs<'static>, window: winapi::HWND, pub unsafe fn new(pf_reqs: &PixelFormatRequirements, opengl: &GlAttributes<winapi::HGLRC>,
builder_sharelists: Option<winapi::HGLRC>) window: winapi::HWND) -> Result<Context, CreationError>
-> Result<Context, CreationError>
{ {
let hdc = user32::GetDC(window); let hdc = user32::GetDC(window);
if hdc.is_null() { if hdc.is_null() {
@ -118,20 +118,20 @@ impl Context {
enumerate_native_pixel_formats(hdc) enumerate_native_pixel_formats(hdc)
}; };
let (id, f) = try!(builder.choose_pixel_format(formats)); let (id, f) = try!(pf_reqs.choose_pixel_format(formats));
try!(set_pixel_format(hdc, id)); try!(set_pixel_format(hdc, id));
f f
}; };
// creating the OpenGL context // creating the OpenGL context
let context = try!(create_context(Some((&extra_functions, builder, &extensions)), let context = try!(create_context(Some((&extra_functions, pf_reqs, opengl, &extensions)),
window, hdc, builder_sharelists)); window, hdc));
// loading the opengl32 module // loading the opengl32 module
let gl_library = try!(load_opengl32_dll()); let gl_library = try!(load_opengl32_dll());
// handling vsync // handling vsync
if builder.opengl.vsync { if opengl.vsync {
if extensions.split(' ').find(|&i| i == "WGL_EXT_swap_control").is_some() { if extensions.split(' ').find(|&i| i == "WGL_EXT_swap_control").is_some() {
let _guard = try!(CurrentContextGuard::make_current(hdc, context.0)); let _guard = try!(CurrentContextGuard::make_current(hdc, context.0));
@ -210,17 +210,20 @@ unsafe impl Sync for Context {}
/// ///
/// Otherwise, only the basic API will be used and the chances of `CreationError::NotSupported` /// Otherwise, only the basic API will be used and the chances of `CreationError::NotSupported`
/// being returned increase. /// being returned increase.
unsafe fn create_context(extra: Option<(&gl::wgl_extra::Wgl, &BuilderAttribs<'static>, &str)>, unsafe fn create_context(extra: Option<(&gl::wgl_extra::Wgl, &PixelFormatRequirements,
_: winapi::HWND, hdc: winapi::HDC, share: Option<winapi::HGLRC>) &GlAttributes<winapi::HGLRC>, &str)>,
_: winapi::HWND, hdc: winapi::HDC)
-> Result<ContextWrapper, CreationError> -> Result<ContextWrapper, CreationError>
{ {
let share = share.unwrap_or(ptr::null_mut()); let share;
if let Some((extra_functions, pf_reqs, opengl, extensions)) = extra {
share = opengl.sharing.unwrap_or(ptr::null_mut());
if let Some((extra_functions, builder, extensions)) = extra {
if extensions.split(' ').find(|&i| i == "WGL_ARB_create_context").is_some() { if extensions.split(' ').find(|&i| i == "WGL_ARB_create_context").is_some() {
let mut attributes = Vec::new(); let mut attributes = Vec::new();
match builder.opengl.version { match opengl.version {
GlRequest::Latest => {}, GlRequest::Latest => {},
GlRequest::Specific(Api::OpenGl, (major, minor)) => { GlRequest::Specific(Api::OpenGl, (major, minor)) => {
attributes.push(gl::wgl_extra::CONTEXT_MAJOR_VERSION_ARB as libc::c_int); attributes.push(gl::wgl_extra::CONTEXT_MAJOR_VERSION_ARB as libc::c_int);
@ -252,7 +255,7 @@ unsafe fn create_context(extra: Option<(&gl::wgl_extra::Wgl, &BuilderAttribs<'st
}, },
} }
if let Some(profile) = builder.opengl.profile { if let Some(profile) = opengl.profile {
if extensions.split(' ').find(|&i| i == "WGL_ARB_create_context_profile").is_some() if extensions.split(' ').find(|&i| i == "WGL_ARB_create_context_profile").is_some()
{ {
let flag = match profile { let flag = match profile {
@ -273,7 +276,7 @@ unsafe fn create_context(extra: Option<(&gl::wgl_extra::Wgl, &BuilderAttribs<'st
// robustness // robustness
if extensions.split(' ').find(|&i| i == "WGL_ARB_create_context_robustness").is_some() { if extensions.split(' ').find(|&i| i == "WGL_ARB_create_context_robustness").is_some() {
match builder.opengl.robustness { match opengl.robustness {
Robustness::RobustNoResetNotification | Robustness::TryRobustNoResetNotification => { Robustness::RobustNoResetNotification | Robustness::TryRobustNoResetNotification => {
attributes.push(gl::wgl_extra::CONTEXT_RESET_NOTIFICATION_STRATEGY_ARB as libc::c_int); attributes.push(gl::wgl_extra::CONTEXT_RESET_NOTIFICATION_STRATEGY_ARB as libc::c_int);
attributes.push(gl::wgl_extra::NO_RESET_NOTIFICATION_ARB as libc::c_int); attributes.push(gl::wgl_extra::NO_RESET_NOTIFICATION_ARB as libc::c_int);
@ -288,7 +291,7 @@ unsafe fn create_context(extra: Option<(&gl::wgl_extra::Wgl, &BuilderAttribs<'st
Robustness::NoError => (), Robustness::NoError => (),
} }
} else { } else {
match builder.opengl.robustness { match opengl.robustness {
Robustness::RobustNoResetNotification | Robustness::RobustLoseContextOnReset => { Robustness::RobustNoResetNotification | Robustness::RobustLoseContextOnReset => {
return Err(CreationError::RobustnessNotSupported); return Err(CreationError::RobustnessNotSupported);
}, },
@ -296,7 +299,7 @@ unsafe fn create_context(extra: Option<(&gl::wgl_extra::Wgl, &BuilderAttribs<'st
} }
} }
if builder.opengl.debug { if opengl.debug {
flags = flags | gl::wgl_extra::CONTEXT_DEBUG_BIT_ARB as libc::c_int; flags = flags | gl::wgl_extra::CONTEXT_DEBUG_BIT_ARB as libc::c_int;
} }
@ -319,7 +322,10 @@ unsafe fn create_context(extra: Option<(&gl::wgl_extra::Wgl, &BuilderAttribs<'st
return Ok(ContextWrapper(ctxt as winapi::HGLRC)); return Ok(ContextWrapper(ctxt as winapi::HGLRC));
} }
} }
};
} else {
share = ptr::null_mut();
}
let ctxt = gl::wgl::CreateContext(hdc as *const libc::c_void); let ctxt = gl::wgl::CreateContext(hdc as *const libc::c_void);
if ctxt.is_null() { if ctxt.is_null() {
@ -544,7 +550,7 @@ unsafe fn load_extra_functions(window: winapi::HWND) -> Result<gl::wgl_extra::Wg
} }
// creating the dummy OpenGL context and making it current // creating the dummy OpenGL context and making it current
let dummy_context = try!(create_context(None, dummy_window.0, dummy_window.1, None)); let dummy_context = try!(create_context(None, dummy_window.0, dummy_window.1));
let _current_context = try!(CurrentContextGuard::make_current(dummy_window.1, let _current_context = try!(CurrentContextGuard::make_current(dummy_window.1,
dummy_context.0)); dummy_context.0));

View file

@ -15,7 +15,10 @@ use BuilderAttribs;
use CreationError; use CreationError;
use CreationError::OsError; use CreationError::OsError;
use CursorState; use CursorState;
use GlAttributes;
use GlRequest; use GlRequest;
use PixelFormatRequirements;
use WindowAttributes;
use std::ffi::{OsStr}; use std::ffi::{OsStr};
use std::os::windows::ffi::OsStrExt; use std::os::windows::ffi::OsStrExt;
@ -31,6 +34,7 @@ use api::egl;
use api::egl::Context as EglContext; use api::egl::Context as EglContext;
use api::egl::ffi::egl::Egl; use api::egl::ffi::egl::Egl;
#[derive(Clone)]
pub enum RawContext { pub enum RawContext {
Egl(egl::ffi::egl::types::EGLContext), Egl(egl::ffi::egl::types::EGLContext),
Wgl(winapi::HGLRC), Wgl(winapi::HGLRC),
@ -39,15 +43,18 @@ 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(window: &WindowAttributes, pf_reqs: &PixelFormatRequirements,
egl: Option<&Egl>) opengl: &GlAttributes<RawContext>, egl: Option<&Egl>)
-> Result<Window, CreationError> -> Result<Window, CreationError>
{ {
let egl = egl.map(|e| e.clone()); let egl = egl.map(|e| e.clone());
let window = window.clone();
let pf_reqs = pf_reqs.clone();
let opengl = opengl.clone();
// initializing variables to be sent to the task // initializing variables to be sent to the task
let title = OsStr::new(&builder.window.title).encode_wide().chain(Some(0).into_iter()) let title = OsStr::new(&window.title).encode_wide().chain(Some(0).into_iter())
.collect::<Vec<_>>(); .collect::<Vec<_>>();
let (tx, rx) = channel(); let (tx, rx) = channel();
@ -57,7 +64,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, egl) { match init(title, &window, &pf_reqs, &opengl, 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,29 +90,36 @@ pub fn new_window(builder: BuilderAttribs<'static>, builder_sharelists: Option<R
rx.recv().unwrap() rx.recv().unwrap()
} }
unsafe fn init(title: Vec<u16>, builder: BuilderAttribs<'static>, unsafe fn init(title: Vec<u16>, window: &WindowAttributes, pf_reqs: &PixelFormatRequirements,
builder_sharelists: Option<RawContext>, egl: Option<Egl>) opengl: &GlAttributes<RawContext>, egl: Option<Egl>)
-> Result<Window, CreationError> -> Result<Window, CreationError>
{ {
let opengl = opengl.clone().map_sharing(|sharelists| {
match sharelists {
RawContext::Wgl(c) => c,
_ => unimplemented!()
}
});
// registering the window class // registering the window class
let class_name = register_window_class(); let class_name = register_window_class();
// building a RECT object with coordinates // building a RECT object with coordinates
let mut rect = winapi::RECT { let mut rect = winapi::RECT {
left: 0, right: builder.window.dimensions.unwrap_or((1024, 768)).0 as winapi::LONG, left: 0, right: window.dimensions.unwrap_or((1024, 768)).0 as winapi::LONG,
top: 0, bottom: builder.window.dimensions.unwrap_or((1024, 768)).1 as winapi::LONG, top: 0, bottom: window.dimensions.unwrap_or((1024, 768)).1 as winapi::LONG,
}; };
// switching to fullscreen if necessary // switching to fullscreen if necessary
// this means adjusting the window's position so that it overlaps the right monitor, // this means adjusting the window's position so that it overlaps the right monitor,
// and change the monitor's resolution if necessary // and change the monitor's resolution if necessary
if builder.window.monitor.is_some() { if window.monitor.is_some() {
let monitor = builder.window.monitor.as_ref().unwrap(); let monitor = window.monitor.as_ref().unwrap();
try!(switch_to_fullscreen(&mut rect, monitor)); try!(switch_to_fullscreen(&mut rect, monitor));
} }
// computing the style and extended style of the window // computing the style and extended style of the window
let (ex_style, style) = if builder.window.monitor.is_some() || builder.window.decorations == false { let (ex_style, style) = if window.monitor.is_some() || window.decorations == false {
(winapi::WS_EX_APPWINDOW, winapi::WS_POPUP | winapi::WS_CLIPSIBLINGS | winapi::WS_CLIPCHILDREN) (winapi::WS_EX_APPWINDOW, winapi::WS_POPUP | winapi::WS_CLIPSIBLINGS | winapi::WS_CLIPCHILDREN)
} else { } else {
(winapi::WS_EX_APPWINDOW | winapi::WS_EX_WINDOWEDGE, (winapi::WS_EX_APPWINDOW | winapi::WS_EX_WINDOWEDGE,
@ -117,19 +131,19 @@ unsafe fn init(title: Vec<u16>, builder: BuilderAttribs<'static>,
// creating the real window this time, by using the functions in `extra_functions` // creating the real window this time, by using the functions in `extra_functions`
let real_window = { let real_window = {
let (width, height) = if builder.window.monitor.is_some() || builder.window.dimensions.is_some() { let (width, height) = if window.monitor.is_some() || window.dimensions.is_some() {
(Some(rect.right - rect.left), Some(rect.bottom - rect.top)) (Some(rect.right - rect.left), Some(rect.bottom - rect.top))
} else { } else {
(None, None) (None, None)
}; };
let (x, y) = if builder.window.monitor.is_some() { let (x, y) = if window.monitor.is_some() {
(Some(rect.left), Some(rect.top)) (Some(rect.left), Some(rect.top))
} else { } else {
(None, None) (None, None)
}; };
let style = if !builder.window.visible || builder.headless { let style = if !window.visible {
style style
} else { } else {
style | winapi::WS_VISIBLE style | winapi::WS_VISIBLE
@ -159,51 +173,33 @@ unsafe fn init(title: Vec<u16>, builder: BuilderAttribs<'static>,
}; };
// creating the OpenGL context // creating the OpenGL context
let context = match builder.opengl.version { let context = match 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.pf_reqs, &builder.opengl.clone().map_sharing(|_| unimplemented!()), if let Ok(c) = EglContext::new(egl, &pf_reqs, &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))
{ {
Context::Egl(c) Context::Egl(c)
} else { } else {
let builder_sharelists = match builder_sharelists { try!(WglContext::new(&pf_reqs, &opengl, real_window.0)
None => None,
Some(RawContext::Wgl(c)) => Some(c),
_ => unimplemented!()
};
try!(WglContext::new(&builder, real_window.0, builder_sharelists)
.map(Context::Wgl)) .map(Context::Wgl))
} }
} else { } else {
// falling back to WGL, which is always available // falling back to WGL, which is always available
let builder_sharelists = match builder_sharelists { try!(WglContext::new(&pf_reqs, &opengl, real_window.0)
None => None,
Some(RawContext::Wgl(c)) => Some(c),
_ => unimplemented!()
};
try!(WglContext::new(&builder, real_window.0, builder_sharelists)
.map(Context::Wgl)) .map(Context::Wgl))
} }
}, },
_ => { _ => {
let builder_sharelists = match builder_sharelists { try!(WglContext::new(&pf_reqs, &opengl, real_window.0).map(Context::Wgl))
None => None,
Some(RawContext::Wgl(c)) => Some(c),
_ => unimplemented!()
};
try!(WglContext::new(&builder, real_window.0, builder_sharelists).map(Context::Wgl))
} }
}; };
// making the window transparent // making the window transparent
if builder.window.transparent { if window.transparent {
let bb = winapi::DWM_BLURBEHIND { let bb = winapi::DWM_BLURBEHIND {
dwFlags: 0x1, // FIXME: DWM_BB_ENABLE; dwFlags: 0x1, // FIXME: DWM_BB_ENABLE;
fEnable: 1, fEnable: 1,
@ -215,7 +211,7 @@ unsafe fn init(title: Vec<u16>, builder: BuilderAttribs<'static>,
} }
// calling SetForegroundWindow if fullscreen // calling SetForegroundWindow if fullscreen
if builder.window.monitor.is_some() { if window.monitor.is_some() {
user32::SetForegroundWindow(real_window.0); user32::SetForegroundWindow(real_window.0);
} }

View file

@ -84,14 +84,14 @@ 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, egl: Option<&Egl>) -> Result<Window, CreationError> { pub fn new(builder: BuilderAttribs, egl: Option<&Egl>) -> Result<Window, CreationError> {
let (builder, sharing) = builder.extract_non_static(); let opengl = builder.opengl.clone().map_sharing(|sharing| {
match sharing.context {
let sharing = sharing.map(|w| match w.context {
Context::Wgl(ref c) => RawContext::Wgl(c.get_hglrc()), Context::Wgl(ref c) => RawContext::Wgl(c.get_hglrc()),
Context::Egl(_) => unimplemented!(), // FIXME: Context::Egl(_) => unimplemented!(), // FIXME:
}
}); });
init::new_window(builder, sharing, egl) init::new_window(&builder.window, &builder.pf_reqs, &opengl, egl)
} }
/// See the docs in the crate root file. /// See the docs in the crate root file.