From c6c4dfdd1e1c28878d5049c825db377dd5c747ee Mon Sep 17 00:00:00 2001 From: Fredrik Noren Date: Fri, 8 Jan 2016 00:03:54 -0500 Subject: [PATCH 1/7] Fix cocoa headless --- build.rs | 4 ++-- examples/support/mod.rs | 1 - src/api/cocoa/headless.rs | 48 +++++++------------------------------ tests/headless.rs | 50 ++++++++++++++++++++++++++++++++------- 4 files changed, 52 insertions(+), 51 deletions(-) diff --git a/build.rs b/build.rs index bf25c959..985bb59c 100644 --- a/build.rs +++ b/build.rs @@ -180,8 +180,8 @@ fn main() { let mut file = File::create(&dest.join("test_gl_bindings.rs")).unwrap(); gl_generator::generate_bindings(gl_generator::StructGenerator, - gl_generator::registry::Ns::Gles2, + gl_generator::registry::Ns::Gl, gl_generator::Fallbacks::All, khronos_api::GL_XML, vec![], - "3.0", "core", &mut file).unwrap(); + "3.3", "core", &mut file).unwrap(); } diff --git a/examples/support/mod.rs b/examples/support/mod.rs index ebbe379d..bb65e154 100644 --- a/examples/support/mod.rs +++ b/examples/support/mod.rs @@ -4,7 +4,6 @@ use std::ptr; use glutin; mod gl { - pub use self::Gles2 as Gl; include!(concat!(env!("OUT_DIR"), "/test_gl_bindings.rs")); } diff --git a/src/api/cocoa/headless.rs b/src/api/cocoa/headless.rs index d0d59f30..c12f0162 100644 --- a/src/api/cocoa/headless.rs +++ b/src/api/cocoa/headless.rs @@ -14,13 +14,6 @@ use cocoa::base::{id, nil}; use cocoa::appkit::*; use PixelFormat; -mod gl { - include!(concat!(env!("OUT_DIR"), "/gl_bindings.rs")); -} - -static mut framebuffer: u32 = 0; -static mut texture: u32 = 0; - pub struct HeadlessContext { width: u32, height: u32, @@ -33,9 +26,14 @@ impl HeadlessContext { { let context = unsafe { let attributes = [ - NSOpenGLPFAAccelerated as u32, - NSOpenGLPFAAllowOfflineRenderers as u32, NSOpenGLPFADoubleBuffer as u32, + NSOpenGLPFAClosestPolicy as u32, + NSOpenGLPFAColorSize as u32, 24, + NSOpenGLPFAAlphaSize as u32, 8, + NSOpenGLPFADepthSize as u32, 24, + NSOpenGLPFAStencilSize as u32, 8, + + NSOpenGLPFAOpenGLProfile as u32, NSOpenGLProfileVersion3_2Core as u32, 0 ]; @@ -56,9 +54,6 @@ impl HeadlessContext { context: context, }; - // Load the function pointers as we need them to create the FBO - gl::load_with(|s| headless.get_proc_address(s) as *const c_void); - Ok(headless) } } @@ -66,28 +61,12 @@ impl HeadlessContext { impl GlContext for HeadlessContext { unsafe fn make_current(&self) -> Result<(), ContextError> { self.context.makeCurrentContext(); - - gl::GenFramebuffersEXT(1, &mut framebuffer); - gl::BindFramebufferEXT(gl::FRAMEBUFFER_EXT, framebuffer); - gl::GenTextures(1, &mut texture); - gl::BindTexture(gl::TEXTURE_2D, texture); - gl::TexParameteri(gl::TEXTURE_2D, gl::TEXTURE_MAG_FILTER, gl::LINEAR as i32); - gl::TexParameteri(gl::TEXTURE_2D, gl::TEXTURE_MIN_FILTER, gl::LINEAR as i32); - gl::TexImage2D(gl::TEXTURE_2D, 0, gl::RGBA8 as i32, self.width as i32, self.height as i32, - 0, gl::RGBA, gl::UNSIGNED_BYTE, ptr::null()); - gl::FramebufferTexture2DEXT(gl::FRAMEBUFFER_EXT, gl::COLOR_ATTACHMENT0_EXT, - gl::TEXTURE_2D, texture, 0); - let status = gl::CheckFramebufferStatusEXT(gl::FRAMEBUFFER_EXT); - if status != gl::FRAMEBUFFER_COMPLETE_EXT { - panic!("Error while creating the framebuffer"); - } - Ok(()) } #[inline] fn is_current(&self) -> bool { - unimplemented!() + true } #[inline] @@ -105,6 +84,7 @@ impl GlContext for HeadlessContext { #[inline] fn swap_buffers(&self) -> Result<(), ContextError> { + unsafe { self.context.flushBuffer(); } Ok(()) } @@ -121,13 +101,3 @@ impl GlContext for HeadlessContext { unsafe impl Send for HeadlessContext {} unsafe impl Sync for HeadlessContext {} - -impl Drop for HeadlessContext { - #[inline] - fn drop(&mut self) { - unsafe { - gl::DeleteTextures(1, &texture); - gl::DeleteFramebuffersEXT(1, &framebuffer); - } - } -} diff --git a/tests/headless.rs b/tests/headless.rs index fdbb6c28..69d06b8f 100644 --- a/tests/headless.rs +++ b/tests/headless.rs @@ -1,28 +1,60 @@ extern crate glutin; extern crate libc; +use glutin::*; +use std::ptr; mod gl { include!(concat!(env!("OUT_DIR"), "/test_gl_bindings.rs")); } +use gl::types::*; -#[cfg(feature = "headless")] #[test] fn main() { - let window = glutin::HeadlessRendererBuilder::new(1024, 768).build().unwrap(); + let width: i32 = 2; + let height: i32 = 1; + let window = glutin::HeadlessRendererBuilder::new(width as u32, height as u32).build().unwrap(); unsafe { window.make_current() }; - let gl = gl::Gl::load_with(|symbol| window.get_proc_address(symbol)); + let gl = gl::Gl::load_with(|symbol| window.get_proc_address(symbol) as *const _); unsafe { + let mut framebuffer = 0; + let mut texture = 0; + gl.GenFramebuffers(1, &mut framebuffer); + gl.BindFramebuffer(gl::FRAMEBUFFER, framebuffer); + gl.GenTextures(1, &mut texture); + gl.BindTexture(gl::TEXTURE_2D, texture); + gl.TexParameteri(gl::TEXTURE_2D, gl::TEXTURE_MAG_FILTER, gl::LINEAR as i32); + gl.TexParameteri(gl::TEXTURE_2D, gl::TEXTURE_MIN_FILTER, gl::LINEAR as i32); + gl.TexImage2D(gl::TEXTURE_2D, 0, gl::RGBA as i32, width, height, + 0, gl::RGBA, gl::UNSIGNED_BYTE, ptr::null()); + gl.FramebufferTexture(gl::FRAMEBUFFER, gl::COLOR_ATTACHMENT0, texture, 0); + let status = gl.CheckFramebufferStatus(gl::FRAMEBUFFER); + if status != gl::FRAMEBUFFER_COMPLETE { + panic!("Error while creating the framebuffer"); + } + gl.ClearColor(0.0, 1.0, 0.0, 1.0); gl.Clear(gl::COLOR_BUFFER_BIT); + gl.Enable(gl::SCISSOR_TEST); + gl.Scissor(1, 0, 1, 1); + gl.ClearColor(1.0, 0.0, 0.0, 1.0); + gl.Clear(gl::COLOR_BUFFER_BIT); - let mut value: (u8, u8, u8, u8) = std::mem::uninitialized(); - gl.ReadPixels(0, 0, 1, 1, gl::RGBA, gl::UNSIGNED_BYTE, std::mem::transmute(&mut value)); - - assert!(value == (0, 255, 0, 255) || value == (0, 64, 0, 255) || - value == (0, 64, 0, 255) || value == (0, 64, 0, 0), - "value is: {:?}", value); + let mut values: Vec = vec![0;(width*height*4) as usize]; + gl.ReadPixels(0, 0, width, height, gl::RGBA, gl::UNSIGNED_BYTE, values.as_mut_ptr() as *mut GLvoid); + + println!("{:?}", values); + + assert_eq!(values[0], 0); + assert_eq!(values[1], 255); + assert_eq!(values[2], 0); + assert_eq!(values[3], 255); + + assert_eq!(values[4], 255); + assert_eq!(values[5], 0); + assert_eq!(values[6], 0); + assert_eq!(values[7], 255); } } From 898a411c86b833b7ef72bd584eb922834a8966b4 Mon Sep 17 00:00:00 2001 From: Fredrik Noren Date: Fri, 8 Jan 2016 00:34:10 -0500 Subject: [PATCH 2/7] Don't run headless test on linux --- tests/headless.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/headless.rs b/tests/headless.rs index 69d06b8f..f175477b 100644 --- a/tests/headless.rs +++ b/tests/headless.rs @@ -8,8 +8,9 @@ mod gl { } use gl::types::*; +#[cfg(not(target_os = "linux"))] #[test] -fn main() { +fn test_headless() { let width: i32 = 2; let height: i32 = 1; let window = glutin::HeadlessRendererBuilder::new(width as u32, height as u32).build().unwrap(); From 3d2f71e59b83d00685ee6432aa7804367f9b734e Mon Sep 17 00:00:00 2001 From: Fredrik Noren Date: Fri, 8 Jan 2016 00:35:51 -0500 Subject: [PATCH 3/7] Bigger headless window size in test --- tests/headless.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/headless.rs b/tests/headless.rs index f175477b..b1161390 100644 --- a/tests/headless.rs +++ b/tests/headless.rs @@ -11,8 +11,8 @@ use gl::types::*; #[cfg(not(target_os = "linux"))] #[test] fn test_headless() { - let width: i32 = 2; - let height: i32 = 1; + let width: i32 = 256; + let height: i32 = 256; let window = glutin::HeadlessRendererBuilder::new(width as u32, height as u32).build().unwrap(); unsafe { window.make_current() }; From c344f36e93e44f6d8189f904053c585fd9dd8ccc Mon Sep 17 00:00:00 2001 From: Fredrik Noren Date: Sat, 9 Jan 2016 13:32:48 -0500 Subject: [PATCH 4/7] Trigger build --- tests/headless.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/headless.rs b/tests/headless.rs index b1161390..57e02759 100644 --- a/tests/headless.rs +++ b/tests/headless.rs @@ -8,6 +8,7 @@ mod gl { } use gl::types::*; + #[cfg(not(target_os = "linux"))] #[test] fn test_headless() { From a5de08485685230cd1197cbb5ee059a10b34710e Mon Sep 17 00:00:00 2001 From: Fredrik Noren Date: Sat, 9 Jan 2016 14:31:56 -0500 Subject: [PATCH 5/7] Only run headless test on osx for now --- tests/headless.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/headless.rs b/tests/headless.rs index 57e02759..c8064c75 100644 --- a/tests/headless.rs +++ b/tests/headless.rs @@ -9,7 +9,7 @@ mod gl { use gl::types::*; -#[cfg(not(target_os = "linux"))] +#[cfg(target_os = "macos")] #[test] fn test_headless() { let width: i32 = 256; From db074dc32ff55a5c6ad3a48c6453ff83c38b0caf Mon Sep 17 00:00:00 2001 From: Fredrik Noren Date: Mon, 18 Jan 2016 12:45:55 -0500 Subject: [PATCH 6/7] Use GLES --- build.rs | 4 ++-- examples/support/mod.rs | 1 + tests/headless.rs | 3 ++- 3 files changed, 5 insertions(+), 3 deletions(-) diff --git a/build.rs b/build.rs index 985bb59c..bf25c959 100644 --- a/build.rs +++ b/build.rs @@ -180,8 +180,8 @@ fn main() { let mut file = File::create(&dest.join("test_gl_bindings.rs")).unwrap(); gl_generator::generate_bindings(gl_generator::StructGenerator, - gl_generator::registry::Ns::Gl, + gl_generator::registry::Ns::Gles2, gl_generator::Fallbacks::All, khronos_api::GL_XML, vec![], - "3.3", "core", &mut file).unwrap(); + "3.0", "core", &mut file).unwrap(); } diff --git a/examples/support/mod.rs b/examples/support/mod.rs index bb65e154..ebbe379d 100644 --- a/examples/support/mod.rs +++ b/examples/support/mod.rs @@ -4,6 +4,7 @@ use std::ptr; use glutin; mod gl { + pub use self::Gles2 as Gl; include!(concat!(env!("OUT_DIR"), "/test_gl_bindings.rs")); } diff --git a/tests/headless.rs b/tests/headless.rs index c8064c75..c6a56365 100644 --- a/tests/headless.rs +++ b/tests/headless.rs @@ -4,6 +4,7 @@ use glutin::*; use std::ptr; mod gl { + pub use self::Gles2 as Gl; include!(concat!(env!("OUT_DIR"), "/test_gl_bindings.rs")); } use gl::types::*; @@ -31,7 +32,7 @@ fn test_headless() { gl.TexParameteri(gl::TEXTURE_2D, gl::TEXTURE_MIN_FILTER, gl::LINEAR as i32); gl.TexImage2D(gl::TEXTURE_2D, 0, gl::RGBA as i32, width, height, 0, gl::RGBA, gl::UNSIGNED_BYTE, ptr::null()); - gl.FramebufferTexture(gl::FRAMEBUFFER, gl::COLOR_ATTACHMENT0, texture, 0); + gl.FramebufferTexture2D(gl::FRAMEBUFFER, gl::COLOR_ATTACHMENT0, gl::TEXTURE_2D, texture, 0); let status = gl.CheckFramebufferStatus(gl::FRAMEBUFFER); if status != gl::FRAMEBUFFER_COMPLETE { panic!("Error while creating the framebuffer"); From 2a66d1cffccd74db2dd52925da31860f9330f27e Mon Sep 17 00:00:00 2001 From: Fredrik Noren Date: Mon, 18 Jan 2016 13:12:40 -0500 Subject: [PATCH 7/7] Construct cocoa headless context based on args --- src/api/cocoa/headless.rs | 20 +++------ src/api/cocoa/helpers.rs | 88 ++++++++++++++++++++++++++++++++++++ src/api/cocoa/mod.rs | 95 +++++---------------------------------- tests/headless.rs | 2 - 4 files changed, 105 insertions(+), 100 deletions(-) create mode 100644 src/api/cocoa/helpers.rs diff --git a/src/api/cocoa/headless.rs b/src/api/cocoa/headless.rs index c12f0162..7fe2046a 100644 --- a/src/api/cocoa/headless.rs +++ b/src/api/cocoa/headless.rs @@ -4,8 +4,6 @@ use CreationError::OsError; use GlAttributes; use GlContext; use PixelFormatRequirements; -use std::os::raw::c_void; -use std::ptr; use core_foundation::base::TCFType; use core_foundation::string::CFString; @@ -13,6 +11,7 @@ use core_foundation::bundle::{CFBundleGetBundleWithIdentifier, CFBundleGetFuncti use cocoa::base::{id, nil}; use cocoa::appkit::*; use PixelFormat; +use api::cocoa::helpers; pub struct HeadlessContext { width: u32, @@ -21,21 +20,12 @@ pub struct HeadlessContext { } impl HeadlessContext { - pub fn new((width, height): (u32, u32), _pf_reqs: &PixelFormatRequirements, - _opengl: &GlAttributes<&HeadlessContext>) -> Result + pub fn new((width, height): (u32, u32), pf_reqs: &PixelFormatRequirements, + opengl: &GlAttributes<&HeadlessContext>) -> Result { let context = unsafe { - let attributes = [ - NSOpenGLPFADoubleBuffer as u32, - NSOpenGLPFAClosestPolicy as u32, - NSOpenGLPFAColorSize as u32, 24, - NSOpenGLPFAAlphaSize as u32, 8, - NSOpenGLPFADepthSize as u32, 24, - NSOpenGLPFAStencilSize as u32, 8, - NSOpenGLPFAOpenGLProfile as u32, NSOpenGLProfileVersion3_2Core as u32, - 0 - ]; + let attributes = try!(helpers::build_nsattributes(pf_reqs, opengl)); let pixelformat = NSOpenGLPixelFormat::alloc(nil).initWithAttributes_(&attributes); if pixelformat == nil { @@ -66,7 +56,7 @@ impl GlContext for HeadlessContext { #[inline] fn is_current(&self) -> bool { - true + unimplemented!() } #[inline] diff --git a/src/api/cocoa/helpers.rs b/src/api/cocoa/helpers.rs new file mode 100644 index 00000000..7fd75710 --- /dev/null +++ b/src/api/cocoa/helpers.rs @@ -0,0 +1,88 @@ + +use CreationError; +use GlAttributes; +use GlProfile; +use GlRequest; +use PixelFormatRequirements; +use ReleaseBehavior; +use cocoa::foundation::NSAutoreleasePool; +use cocoa::appkit::*; + +pub fn build_nsattributes(pf_reqs: &PixelFormatRequirements, opengl: &GlAttributes<&T>) + -> Result, CreationError> { + + let profile = match (opengl.version, opengl.version.to_gl_version(), opengl.profile) { + + // Note: we are not using ranges because of a rust bug that should be fixed here: + // https://github.com/rust-lang/rust/pull/27050 + + (GlRequest::Latest, _, Some(GlProfile::Compatibility)) => NSOpenGLProfileVersionLegacy as u32, + (GlRequest::Latest, _, _) => { + if NSAppKitVersionNumber.floor() >= NSAppKitVersionNumber10_9 { + NSOpenGLProfileVersion4_1Core as u32 + } else if NSAppKitVersionNumber.floor() >= NSAppKitVersionNumber10_7 { + NSOpenGLProfileVersion3_2Core as u32 + } else { + NSOpenGLProfileVersionLegacy as u32 + } + }, + + (_, Some((1, _)), _) => NSOpenGLProfileVersionLegacy as u32, + (_, Some((2, _)), _) => NSOpenGLProfileVersionLegacy as u32, + (_, Some((3, 0)), _) => NSOpenGLProfileVersionLegacy as u32, + (_, Some((3, 1)), _) => NSOpenGLProfileVersionLegacy as u32, + (_, Some((3, 2)), _) => NSOpenGLProfileVersion3_2Core as u32, + (_, Some((3, _)), Some(GlProfile::Compatibility)) => return Err(CreationError::OpenGlVersionNotSupported), + (_, Some((3, _)), _) => NSOpenGLProfileVersion4_1Core as u32, + (_, Some((4, _)), Some(GlProfile::Compatibility)) => return Err(CreationError::OpenGlVersionNotSupported), + (_, Some((4, _)), _) => NSOpenGLProfileVersion4_1Core as u32, + _ => return Err(CreationError::OpenGlVersionNotSupported), + }; + + // NOTE: OS X no longer has the concept of setting individual + // color component's bit size. Instead we can only specify the + // full color size and hope for the best. Another hiccup is that + // `NSOpenGLPFAColorSize` also includes `NSOpenGLPFAAlphaSize`, + // so we have to account for that as well. + let alpha_depth = pf_reqs.alpha_bits.unwrap_or(8); + let color_depth = pf_reqs.color_bits.unwrap_or(24) + alpha_depth; + + // TODO: handle hardware_accelerated parameter of pf_reqs + + let mut attributes = vec![ + NSOpenGLPFADoubleBuffer as u32, + NSOpenGLPFAClosestPolicy as u32, + NSOpenGLPFAColorSize as u32, color_depth as u32, + NSOpenGLPFAAlphaSize as u32, alpha_depth as u32, + NSOpenGLPFADepthSize as u32, pf_reqs.depth_bits.unwrap_or(24) as u32, + NSOpenGLPFAStencilSize as u32, pf_reqs.stencil_bits.unwrap_or(8) as u32, + NSOpenGLPFAOpenGLProfile as u32, profile, + ]; + + if pf_reqs.release_behavior != ReleaseBehavior::Flush { + return Err(CreationError::NoAvailablePixelFormat); + } + + if pf_reqs.stereoscopy { + unimplemented!(); // TODO: + } + + if pf_reqs.double_buffer == Some(false) { + unimplemented!(); // TODO: + } + + if pf_reqs.float_color_buffer { + attributes.push(NSOpenGLPFAColorFloat as u32); + } + + pf_reqs.multisampling.map(|samples| { + attributes.push(NSOpenGLPFAMultisample as u32); + attributes.push(NSOpenGLPFASampleBuffers as u32); attributes.push(1); + attributes.push(NSOpenGLPFASamples as u32); attributes.push(samples as u32); + }); + + // attribute list must be null terminated. + attributes.push(0); + + Ok(attributes) +} diff --git a/src/api/cocoa/mod.rs b/src/api/cocoa/mod.rs index a720a512..28cbb552 100644 --- a/src/api/cocoa/mod.rs +++ b/src/api/cocoa/mod.rs @@ -24,8 +24,8 @@ use objc::declare::ClassDecl; use cgl::{CGLEnable, kCGLCECrashOnRemovedFunctions, CGLSetParameter, kCGLCPSurfaceOpacity}; use cocoa::base::{id, nil}; -use cocoa::foundation::{NSAutoreleasePool, NSDate, NSDefaultRunLoopMode, NSPoint, NSRect, NSSize, - NSString, NSUInteger}; +use cocoa::foundation::{NSAutoreleasePool, NSDate, NSDefaultRunLoopMode, NSPoint, NSRect, NSSize, + NSString, NSUInteger}; use cocoa::appkit; use cocoa::appkit::*; use cocoa::appkit::NSEventSubtype::*; @@ -54,6 +54,7 @@ pub use self::monitor::{MonitorId, get_available_monitors, get_primary_monitor}; mod monitor; mod event; mod headless; +mod helpers; static mut shift_pressed: bool = false; static mut ctrl_pressed: bool = false; @@ -137,7 +138,7 @@ impl WindowDelegate { window_should_close as extern fn(&Object, Sel, id) -> BOOL); decl.add_method(sel!(windowDidResize:), window_did_resize as extern fn(&Object, Sel, id)); - + decl.add_method(sel!(windowDidBecomeKey:), window_did_become_key as extern fn(&Object, Sel, id)); decl.add_method(sel!(windowDidResignKey:), @@ -317,7 +318,7 @@ impl Window { let mut opacity = 0; CGLSetParameter(obj as *mut _, kCGLCPSurfaceOpacity, &mut opacity); } - + app.activateIgnoringOtherApps_(YES); if win_attribs.visible { window.makeKeyAndOrderFront_(nil); @@ -359,7 +360,7 @@ impl Window { } fn create_window(attrs: &WindowAttributes) -> Option { - unsafe { + unsafe { let screen = match attrs.monitor { Some(ref monitor_id) => { let native_id = match monitor_id.get_native_identifier() { @@ -414,7 +415,7 @@ impl Window { NSTitledWindowMask as NSUInteger | NSFullSizeContentViewWindowMask as NSUInteger }; - + let window = IdRef::new(NSWindow::alloc(nil).initWithContentRect_styleMask_backing_defer_( frame, masks, @@ -456,79 +457,7 @@ impl Window { fn create_context(view: id, pf_reqs: &PixelFormatRequirements, opengl: &GlAttributes<&Window>) -> Result<(IdRef, PixelFormat), CreationError> { - let profile = match (opengl.version, opengl.version.to_gl_version(), opengl.profile) { - - // Note: we are not using ranges because of a rust bug that should be fixed here: - // https://github.com/rust-lang/rust/pull/27050 - - (GlRequest::Latest, _, Some(GlProfile::Compatibility)) => NSOpenGLProfileVersionLegacy as u32, - (GlRequest::Latest, _, _) => { - if NSAppKitVersionNumber.floor() >= NSAppKitVersionNumber10_9 { - NSOpenGLProfileVersion4_1Core as u32 - } else if NSAppKitVersionNumber.floor() >= NSAppKitVersionNumber10_7 { - NSOpenGLProfileVersion3_2Core as u32 - } else { - NSOpenGLProfileVersionLegacy as u32 - } - }, - - (_, Some((1, _)), _) => NSOpenGLProfileVersionLegacy as u32, - (_, Some((2, _)), _) => NSOpenGLProfileVersionLegacy as u32, - (_, Some((3, 0)), _) => NSOpenGLProfileVersionLegacy as u32, - (_, Some((3, 1)), _) => NSOpenGLProfileVersionLegacy as u32, - (_, Some((3, 2)), _) => NSOpenGLProfileVersion3_2Core as u32, - (_, Some((3, _)), Some(GlProfile::Compatibility)) => return Err(CreationError::OpenGlVersionNotSupported), - (_, Some((3, _)), _) => NSOpenGLProfileVersion4_1Core as u32, - (_, Some((4, _)), Some(GlProfile::Compatibility)) => return Err(CreationError::OpenGlVersionNotSupported), - (_, Some((4, _)), _) => NSOpenGLProfileVersion4_1Core as u32, - _ => return Err(CreationError::OpenGlVersionNotSupported), - }; - - // NOTE: OS X no longer has the concept of setting individual - // color component's bit size. Instead we can only specify the - // full color size and hope for the best. Another hiccup is that - // `NSOpenGLPFAColorSize` also includes `NSOpenGLPFAAlphaSize`, - // so we have to account for that as well. - let alpha_depth = pf_reqs.alpha_bits.unwrap_or(8); - let color_depth = pf_reqs.color_bits.unwrap_or(24) + alpha_depth; - - // TODO: handle hardware_accelerated parameter of pf_reqs - - let mut attributes = vec![ - NSOpenGLPFADoubleBuffer as u32, - NSOpenGLPFAClosestPolicy as u32, - NSOpenGLPFAColorSize as u32, color_depth as u32, - NSOpenGLPFAAlphaSize as u32, alpha_depth as u32, - NSOpenGLPFADepthSize as u32, pf_reqs.depth_bits.unwrap_or(24) as u32, - NSOpenGLPFAStencilSize as u32, pf_reqs.stencil_bits.unwrap_or(8) as u32, - NSOpenGLPFAOpenGLProfile as u32, profile, - ]; - - if pf_reqs.release_behavior != ReleaseBehavior::Flush { - return Err(CreationError::NoAvailablePixelFormat); - } - - if pf_reqs.stereoscopy { - unimplemented!(); // TODO: - } - - if pf_reqs.double_buffer == Some(false) { - unimplemented!(); // TODO: - } - - if pf_reqs.float_color_buffer { - attributes.push(NSOpenGLPFAColorFloat as u32); - } - - pf_reqs.multisampling.map(|samples| { - attributes.push(NSOpenGLPFAMultisample as u32); - attributes.push(NSOpenGLPFASampleBuffers as u32); attributes.push(1); - attributes.push(NSOpenGLPFASamples as u32); attributes.push(samples as u32); - }); - - // attribute list must be null terminated. - attributes.push(0); - + let attributes = try!(helpers::build_nsattributes(pf_reqs, opengl)); unsafe { let pixelformat = IdRef::new(NSOpenGLPixelFormat::alloc(nil).initWithAttributes_(&attributes)); @@ -604,7 +533,7 @@ impl Window { pub fn get_position(&self) -> Option<(i32, i32)> { unsafe { let content_rect = NSWindow::contentRectForFrameRect_(*self.window, NSWindow::frame(*self.window)); - + // TODO: consider extrapolating the calculations for the y axis to // a private method Some((content_rect.origin.x as i32, (CGDisplayPixelsHigh(CGMainDisplayID()) as f64 - (content_rect.origin.y + content_rect.size.height)) as i32)) @@ -616,7 +545,7 @@ impl Window { let frame = NSWindow::frame(*self.view); // NOTE: `setFrameOrigin` might not give desirable results when - // setting window, as it treats bottom left as origin. + // setting window, as it treats bottom left as origin. // `setFrameTopLeftPoint` treats top left as origin (duh), but // does not equal the value returned by `get_window_position` // (there is a difference by 22 for me on yosemite) @@ -699,7 +628,7 @@ impl Window { } pub fn set_cursor(&self, cursor: MouseCursor) { - let cursor_name = match cursor { + let cursor_name = match cursor { MouseCursor::Arrow | MouseCursor::Default => "arrowCursor", MouseCursor::Hand => "pointingHandCursor", MouseCursor::Grabbing | MouseCursor::Grab => "closedHandCursor", @@ -768,7 +697,7 @@ impl Window { pub fn set_cursor_position(&self, x: i32, y: i32) -> Result<(), ()> { let (window_x, window_y) = self.get_position().unwrap_or((0, 0)); let (cursor_x, cursor_y) = (window_x + x, window_y + y); - + unsafe { // TODO: Check for errors. let _ = CGWarpMouseCursorPosition(CGPoint { x: cursor_x as CGFloat, y: cursor_y as CGFloat }); diff --git a/tests/headless.rs b/tests/headless.rs index c6a56365..c22be40e 100644 --- a/tests/headless.rs +++ b/tests/headless.rs @@ -48,8 +48,6 @@ fn test_headless() { let mut values: Vec = vec![0;(width*height*4) as usize]; gl.ReadPixels(0, 0, width, height, gl::RGBA, gl::UNSIGNED_BYTE, values.as_mut_ptr() as *mut GLvoid); - println!("{:?}", values); - assert_eq!(values[0], 0); assert_eq!(values[1], 255); assert_eq!(values[2], 0);