2015-06-16 10:15:31 +02:00
|
|
|
use ContextError;
|
2014-11-25 23:30:09 +01:00
|
|
|
use CreationError;
|
|
|
|
|
use CreationError::OsError;
|
2015-09-21 13:15:43 +02:00
|
|
|
use GlAttributes;
|
2015-04-30 13:23:37 +02:00
|
|
|
use GlContext;
|
2015-09-21 13:15:43 +02:00
|
|
|
use PixelFormatRequirements;
|
2015-12-01 02:11:54 +01:00
|
|
|
use std::os::raw::c_void;
|
2014-11-25 23:30:09 +01:00
|
|
|
use std::ptr;
|
|
|
|
|
|
|
|
|
|
use core_foundation::base::TCFType;
|
|
|
|
|
use core_foundation::string::CFString;
|
|
|
|
|
use core_foundation::bundle::{CFBundleGetBundleWithIdentifier, CFBundleGetFunctionPointerForName};
|
|
|
|
|
use cocoa::base::{id, nil};
|
|
|
|
|
use cocoa::appkit::*;
|
2015-05-04 16:30:44 +02:00
|
|
|
use PixelFormat;
|
2014-11-25 23:30:09 +01:00
|
|
|
|
|
|
|
|
pub struct HeadlessContext {
|
2015-01-21 10:58:08 -05:00
|
|
|
width: u32,
|
|
|
|
|
height: u32,
|
2014-11-25 23:30:09 +01:00
|
|
|
context: id,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl HeadlessContext {
|
2015-09-28 12:19:36 -04:00
|
|
|
pub fn new((width, height): (u32, u32), _pf_reqs: &PixelFormatRequirements,
|
|
|
|
|
_opengl: &GlAttributes<&HeadlessContext>) -> Result<HeadlessContext, CreationError>
|
2015-09-21 13:15:43 +02:00
|
|
|
{
|
2014-11-25 23:30:09 +01:00
|
|
|
let context = unsafe {
|
|
|
|
|
let attributes = [
|
2015-01-21 10:58:08 -05:00
|
|
|
NSOpenGLPFADoubleBuffer as u32,
|
2016-01-08 00:03:54 -05:00
|
|
|
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,
|
2014-11-25 23:30:09 +01:00
|
|
|
0
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
}
|
2015-04-30 13:23:37 +02:00
|
|
|
}
|
2014-11-25 23:30:09 +01:00
|
|
|
|
2015-04-30 13:23:37 +02:00
|
|
|
impl GlContext for HeadlessContext {
|
2015-06-16 10:15:31 +02:00
|
|
|
unsafe fn make_current(&self) -> Result<(), ContextError> {
|
2014-11-25 23:30:09 +01:00
|
|
|
self.context.makeCurrentContext();
|
2015-06-16 10:15:31 +02:00
|
|
|
Ok(())
|
2014-11-25 23:30:09 +01:00
|
|
|
}
|
|
|
|
|
|
2015-09-21 14:42:05 +02:00
|
|
|
#[inline]
|
2015-04-30 13:23:37 +02:00
|
|
|
fn is_current(&self) -> bool {
|
2016-01-08 00:03:54 -05:00
|
|
|
true
|
2015-03-04 07:38:55 +01:00
|
|
|
}
|
|
|
|
|
|
2015-09-21 14:42:05 +02:00
|
|
|
#[inline]
|
2015-11-04 11:27:50 +01:00
|
|
|
fn get_proc_address(&self, _addr: &str) -> *const () {
|
2015-01-21 10:58:08 -05:00
|
|
|
let symbol_name: CFString = _addr.parse().unwrap();
|
|
|
|
|
let framework_name: CFString = "com.apple.opengl".parse().unwrap();
|
2014-11-25 23:30:09 +01:00
|
|
|
let framework = unsafe {
|
|
|
|
|
CFBundleGetBundleWithIdentifier(framework_name.as_concrete_TypeRef())
|
|
|
|
|
};
|
|
|
|
|
let symbol = unsafe {
|
|
|
|
|
CFBundleGetFunctionPointerForName(framework, symbol_name.as_concrete_TypeRef())
|
|
|
|
|
};
|
2015-11-04 11:27:50 +01:00
|
|
|
symbol as *const ()
|
2014-11-25 23:30:09 +01:00
|
|
|
}
|
2014-12-19 11:27:03 +10:00
|
|
|
|
2015-09-21 14:42:05 +02:00
|
|
|
#[inline]
|
2015-06-16 10:15:31 +02:00
|
|
|
fn swap_buffers(&self) -> Result<(), ContextError> {
|
2016-01-08 00:03:54 -05:00
|
|
|
unsafe { self.context.flushBuffer(); }
|
2015-06-16 10:15:31 +02:00
|
|
|
Ok(())
|
2015-04-30 13:23:37 +02:00
|
|
|
}
|
|
|
|
|
|
2015-09-21 14:42:05 +02:00
|
|
|
#[inline]
|
2015-04-30 13:23:37 +02:00
|
|
|
fn get_api(&self) -> ::Api {
|
2014-12-19 11:27:03 +10:00
|
|
|
::Api::OpenGl
|
|
|
|
|
}
|
2015-04-30 13:23:37 +02:00
|
|
|
|
2015-09-21 14:42:05 +02:00
|
|
|
#[inline]
|
2015-04-30 13:23:37 +02:00
|
|
|
fn get_pixel_format(&self) -> PixelFormat {
|
|
|
|
|
unimplemented!();
|
|
|
|
|
}
|
2014-11-25 23:30:09 +01:00
|
|
|
}
|
|
|
|
|
|
2014-12-29 22:56:15 +01:00
|
|
|
unsafe impl Send for HeadlessContext {}
|
|
|
|
|
unsafe impl Sync for HeadlessContext {}
|