Construct cocoa headless context based on args

This commit is contained in:
Fredrik Noren 2016-01-18 13:12:40 -05:00
parent db074dc32f
commit 2a66d1cffc
4 changed files with 105 additions and 100 deletions

View file

@ -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<HeadlessContext, CreationError>
pub fn new((width, height): (u32, u32), pf_reqs: &PixelFormatRequirements,
opengl: &GlAttributes<&HeadlessContext>) -> Result<HeadlessContext, CreationError>
{
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]

88
src/api/cocoa/helpers.rs Normal file
View file

@ -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<T>(pf_reqs: &PixelFormatRequirements, opengl: &GlAttributes<&T>)
-> Result<Vec<u32>, 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)
}

View file

@ -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;
@ -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));

View file

@ -48,8 +48,6 @@ fn test_headless() {
let mut values: Vec<u8> = 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);