Purge OpenGL from cocoa

This commit is contained in:
Felix Kaaman 2016-02-23 13:25:33 +01:00
parent 3548276f8f
commit 778b8fa87a
3 changed files with 6 additions and 348 deletions

View file

@ -1,98 +0,0 @@
use ContextError;
use CreationError;
use CreationError::OsError;
use GlAttributes;
use GlContext;
use PixelFormatRequirements;
use core_foundation::base::TCFType;
use core_foundation::string::CFString;
use core_foundation::bundle::{CFBundleGetBundleWithIdentifier, CFBundleGetFunctionPointerForName};
use cocoa::base::{id, nil};
use cocoa::appkit::*;
use PixelFormat;
use api::cocoa::helpers;
#[derive(Default)]
pub struct PlatformSpecificHeadlessBuilderAttributes;
pub struct HeadlessContext {
width: u32,
height: u32,
context: id,
}
impl HeadlessContext {
pub fn new((width, height): (u32, u32), pf_reqs: &PixelFormatRequirements,
opengl: &GlAttributes<&HeadlessContext>,
_: &PlatformSpecificHeadlessBuilderAttributes)
-> Result<HeadlessContext, CreationError>
{
let context = unsafe {
let attributes = try!(helpers::build_nsattributes(pf_reqs, opengl));
let pixelformat = NSOpenGLPixelFormat::alloc(nil).initWithAttributes_(&attributes);
if pixelformat == nil {
return Err(OsError(format!("Could not create the pixel format")));
}
let context = NSOpenGLContext::alloc(nil).initWithFormat_shareContext_(pixelformat, nil);
if context == nil {
return Err(OsError(format!("Could not create the rendering context")));
}
context
};
let headless = HeadlessContext {
width: width,
height: height,
context: context,
};
Ok(headless)
}
}
impl GlContext for HeadlessContext {
unsafe fn make_current(&self) -> Result<(), ContextError> {
self.context.makeCurrentContext();
Ok(())
}
#[inline]
fn is_current(&self) -> bool {
unimplemented!()
}
#[inline]
fn get_proc_address(&self, _addr: &str) -> *const () {
let symbol_name: CFString = _addr.parse().unwrap();
let framework_name: CFString = "com.apple.opengl".parse().unwrap();
let framework = unsafe {
CFBundleGetBundleWithIdentifier(framework_name.as_concrete_TypeRef())
};
let symbol = unsafe {
CFBundleGetFunctionPointerForName(framework, symbol_name.as_concrete_TypeRef())
};
symbol as *const ()
}
#[inline]
fn swap_buffers(&self) -> Result<(), ContextError> {
unsafe { self.context.flushBuffer(); }
Ok(())
}
#[inline]
fn get_api(&self) -> ::Api {
::Api::OpenGl
}
#[inline]
fn get_pixel_format(&self) -> PixelFormat {
unimplemented!();
}
}
unsafe impl Send for HeadlessContext {}
unsafe impl Sync for HeadlessContext {}

View file

@ -1,88 +0,0 @@
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

@ -4,15 +4,6 @@ use {CreationError, Event, MouseCursor, CursorState};
use CreationError::OsError;
use libc;
use ContextError;
use GlAttributes;
use GlContext;
use GlProfile;
use GlRequest;
use PixelFormat;
use PixelFormatRequirements;
use ReleaseBehavior;
use Robustness;
use WindowAttributes;
use native_monitor::NativeMonitorId;
@ -48,13 +39,9 @@ use events::MouseButton;
use events;
pub use self::monitor::{MonitorId, get_available_monitors, get_primary_monitor};
pub use self::headless::HeadlessContext;
pub use self::headless::PlatformSpecificHeadlessBuilderAttributes;
mod monitor;
mod event;
mod headless;
mod helpers;
static mut shift_pressed: bool = false;
static mut ctrl_pressed: bool = false;
@ -62,7 +49,6 @@ static mut win_pressed: bool = false;
static mut alt_pressed: bool = false;
struct DelegateState {
context: IdRef,
view: IdRef,
window: IdRef,
resize_handler: Option<fn(u32, u32)>,
@ -95,7 +81,8 @@ impl WindowDelegate {
let state: *mut libc::c_void = *this.get_ivar("glutinState");
let state = &mut *(state as *mut DelegateState);
let _: () = msg_send![*state.context, update];
// need to notify context before (?) event
// let _: () = msg_send![*state.context, update];
if let Some(handler) = state.resize_handler {
let rect = NSView::frame(*state.view);
@ -185,8 +172,6 @@ pub struct PlatformSpecificWindowBuilderAttributes;
pub struct Window {
view: IdRef,
window: IdRef,
context: IdRef,
pixel_format: PixelFormat,
delegate: WindowDelegate,
}
@ -266,25 +251,14 @@ impl<'a> Iterator for WaitEventsIterator<'a> {
}
impl Window {
pub fn new(win_attribs: &WindowAttributes, pf_reqs: &PixelFormatRequirements,
opengl: &GlAttributes<&Window>, _: &PlatformSpecificWindowBuilderAttributes)
-> Result<Window, CreationError>
pub fn new(win_attribs: &WindowAttributes,
_: &PlatformSpecificWindowBuilderAttributes)
-> Result<Window, CreationError>
{
if opengl.sharing.is_some() {
unimplemented!()
}
// not implemented
assert!(win_attribs.min_dimensions.is_none());
assert!(win_attribs.max_dimensions.is_none());
match opengl.robustness {
Robustness::RobustNoResetNotification | Robustness::RobustLoseContextOnReset => {
return Err(CreationError::RobustnessNotSupported);
},
_ => ()
}
let app = match Window::create_app() {
Some(app) => app,
None => { return Err(OsError(format!("Couldn't create NSApplication"))); },
@ -300,27 +274,9 @@ impl Window {
None => { return Err(OsError(format!("Couldn't create NSView"))); },
};
// TODO: perhaps we should return error from create_context so we can
// determine the cause of failure and possibly recover?
let (context, pf) = match Window::create_context(*view, pf_reqs, opengl) {
Ok((context, pf)) => (context, pf),
Err(e) => { return Err(OsError(format!("Couldn't create OpenGL context: {}", e))); },
};
unsafe {
if win_attribs.transparent {
let clear_col = {
let cls = Class::get("NSColor").unwrap();
msg_send![cls, clearColor]
};
window.setOpaque_(NO);
window.setBackgroundColor_(clear_col);
let obj = context.CGLContextObj();
let mut opacity = 0;
CGLSetParameter(obj as *mut _, kCGLCPSurfaceOpacity, &mut opacity);
unimplemented!();
}
app.activateIgnoringOtherApps_(YES);
@ -332,7 +288,6 @@ impl Window {
}
let ds = DelegateState {
context: context.clone(),
view: view.clone(),
window: window.clone(),
resize_handler: None,
@ -342,8 +297,6 @@ impl Window {
let window = Window {
view: view,
window: window,
context: context,
pixel_format: pf,
delegate: WindowDelegate::new(ds),
};
@ -458,65 +411,6 @@ impl Window {
}
}
fn create_context(view: id, pf_reqs: &PixelFormatRequirements, opengl: &GlAttributes<&Window>)
-> Result<(IdRef, PixelFormat), CreationError>
{
let attributes = try!(helpers::build_nsattributes(pf_reqs, opengl));
unsafe {
let pixelformat = IdRef::new(NSOpenGLPixelFormat::alloc(nil).initWithAttributes_(&attributes));
if let Some(pixelformat) = pixelformat.non_nil() {
// TODO: Add context sharing
let context = IdRef::new(NSOpenGLContext::alloc(nil).initWithFormat_shareContext_(*pixelformat, nil));
if let Some(cxt) = context.non_nil() {
let pf = {
let get_attr = |attrib: NSOpenGLPixelFormatAttribute| -> i32 {
let mut value = 0;
NSOpenGLPixelFormat::getValues_forAttribute_forVirtualScreen_(
*pixelformat,
&mut value,
attrib,
NSOpenGLContext::currentVirtualScreen(*cxt));
value
};
PixelFormat {
hardware_accelerated: get_attr(NSOpenGLPFAAccelerated) != 0,
color_bits: (get_attr(NSOpenGLPFAColorSize) - get_attr(NSOpenGLPFAAlphaSize)) as u8,
alpha_bits: get_attr(NSOpenGLPFAAlphaSize) as u8,
depth_bits: get_attr(NSOpenGLPFADepthSize) as u8,
stencil_bits: get_attr(NSOpenGLPFAStencilSize) as u8,
stereoscopy: get_attr(NSOpenGLPFAStereo) != 0,
double_buffer: get_attr(NSOpenGLPFADoubleBuffer) != 0,
multisampling: if get_attr(NSOpenGLPFAMultisample) > 0 {
Some(get_attr(NSOpenGLPFASamples) as u16)
} else {
None
},
srgb: true,
}
};
cxt.setView_(view);
let value = if opengl.vsync { 1 } else { 0 };
cxt.setValues_forParameter_(&value, NSOpenGLContextParameter::NSOpenGLCPSwapInterval);
CGLEnable(cxt.CGLContextObj() as *mut _, kCGLCECrashOnRemovedFunctions);
Ok((cxt, pf))
} else {
Err(CreationError::NotSupported)
}
} else {
Err(CreationError::NoAvailablePixelFormat)
}
}
}
pub fn set_title(&self, title: &str) {
unsafe {
let title = IdRef::new(NSString::alloc(nil).init_str(title));
@ -712,56 +606,6 @@ impl Window {
}
}
impl GlContext for Window {
#[inline]
unsafe fn make_current(&self) -> Result<(), ContextError> {
let _: () = msg_send![*self.context, update];
self.context.makeCurrentContext();
Ok(())
}
#[inline]
fn is_current(&self) -> bool {
unsafe {
let current = NSOpenGLContext::currentContext(nil);
if current != nil {
let is_equal: BOOL = msg_send![current, isEqual:*self.context];
is_equal != NO
} else {
false
}
}
}
fn get_proc_address(&self, addr: &str) -> *const () {
let symbol_name: CFString = FromStr::from_str(addr).unwrap();
let framework_name: CFString = FromStr::from_str("com.apple.opengl").unwrap();
let framework = unsafe {
CFBundleGetBundleWithIdentifier(framework_name.as_concrete_TypeRef())
};
let symbol = unsafe {
CFBundleGetFunctionPointerForName(framework, symbol_name.as_concrete_TypeRef())
};
symbol as *const _
}
#[inline]
fn swap_buffers(&self) -> Result<(), ContextError> {
unsafe { self.context.flushBuffer(); }
Ok(())
}
#[inline]
fn get_api(&self) -> ::Api {
::Api::OpenGl
}
#[inline]
fn get_pixel_format(&self) -> PixelFormat {
self.pixel_format.clone()
}
}
struct IdRef(id);
impl IdRef {