mirror of
https://github.com/italicsjenga/winit-sonoma-fix.git
synced 2024-12-25 23:01:30 +11:00
Merge pull request #703 from FredrikNoren/master
Fix cocoa headless. Fixes #635
This commit is contained in:
commit
1113fa1c13
|
@ -4,8 +4,6 @@ use CreationError::OsError;
|
||||||
use GlAttributes;
|
use GlAttributes;
|
||||||
use GlContext;
|
use GlContext;
|
||||||
use PixelFormatRequirements;
|
use PixelFormatRequirements;
|
||||||
use std::os::raw::c_void;
|
|
||||||
use std::ptr;
|
|
||||||
|
|
||||||
use core_foundation::base::TCFType;
|
use core_foundation::base::TCFType;
|
||||||
use core_foundation::string::CFString;
|
use core_foundation::string::CFString;
|
||||||
|
@ -13,13 +11,7 @@ use core_foundation::bundle::{CFBundleGetBundleWithIdentifier, CFBundleGetFuncti
|
||||||
use cocoa::base::{id, nil};
|
use cocoa::base::{id, nil};
|
||||||
use cocoa::appkit::*;
|
use cocoa::appkit::*;
|
||||||
use PixelFormat;
|
use PixelFormat;
|
||||||
|
use api::cocoa::helpers;
|
||||||
mod gl {
|
|
||||||
include!(concat!(env!("OUT_DIR"), "/gl_bindings.rs"));
|
|
||||||
}
|
|
||||||
|
|
||||||
static mut framebuffer: u32 = 0;
|
|
||||||
static mut texture: u32 = 0;
|
|
||||||
|
|
||||||
pub struct HeadlessContext {
|
pub struct HeadlessContext {
|
||||||
width: u32,
|
width: u32,
|
||||||
|
@ -28,16 +20,12 @@ pub struct HeadlessContext {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl HeadlessContext {
|
impl HeadlessContext {
|
||||||
pub fn new((width, height): (u32, u32), _pf_reqs: &PixelFormatRequirements,
|
pub fn new((width, height): (u32, u32), pf_reqs: &PixelFormatRequirements,
|
||||||
_opengl: &GlAttributes<&HeadlessContext>) -> Result<HeadlessContext, CreationError>
|
opengl: &GlAttributes<&HeadlessContext>) -> Result<HeadlessContext, CreationError>
|
||||||
{
|
{
|
||||||
let context = unsafe {
|
let context = unsafe {
|
||||||
let attributes = [
|
|
||||||
NSOpenGLPFAAccelerated as u32,
|
let attributes = try!(helpers::build_nsattributes(pf_reqs, opengl));
|
||||||
NSOpenGLPFAAllowOfflineRenderers as u32,
|
|
||||||
NSOpenGLPFADoubleBuffer as u32,
|
|
||||||
0
|
|
||||||
];
|
|
||||||
|
|
||||||
let pixelformat = NSOpenGLPixelFormat::alloc(nil).initWithAttributes_(&attributes);
|
let pixelformat = NSOpenGLPixelFormat::alloc(nil).initWithAttributes_(&attributes);
|
||||||
if pixelformat == nil {
|
if pixelformat == nil {
|
||||||
|
@ -56,9 +44,6 @@ impl HeadlessContext {
|
||||||
context: context,
|
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)
|
Ok(headless)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -66,22 +51,6 @@ impl HeadlessContext {
|
||||||
impl GlContext for HeadlessContext {
|
impl GlContext for HeadlessContext {
|
||||||
unsafe fn make_current(&self) -> Result<(), ContextError> {
|
unsafe fn make_current(&self) -> Result<(), ContextError> {
|
||||||
self.context.makeCurrentContext();
|
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(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -105,6 +74,7 @@ impl GlContext for HeadlessContext {
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
fn swap_buffers(&self) -> Result<(), ContextError> {
|
fn swap_buffers(&self) -> Result<(), ContextError> {
|
||||||
|
unsafe { self.context.flushBuffer(); }
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -121,13 +91,3 @@ impl GlContext for HeadlessContext {
|
||||||
|
|
||||||
unsafe impl Send for HeadlessContext {}
|
unsafe impl Send for HeadlessContext {}
|
||||||
unsafe impl Sync 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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
88
src/api/cocoa/helpers.rs
Normal file
88
src/api/cocoa/helpers.rs
Normal 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)
|
||||||
|
}
|
|
@ -54,6 +54,7 @@ pub use self::monitor::{MonitorId, get_available_monitors, get_primary_monitor};
|
||||||
mod monitor;
|
mod monitor;
|
||||||
mod event;
|
mod event;
|
||||||
mod headless;
|
mod headless;
|
||||||
|
mod helpers;
|
||||||
|
|
||||||
static mut shift_pressed: bool = false;
|
static mut shift_pressed: bool = false;
|
||||||
static mut ctrl_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>)
|
fn create_context(view: id, pf_reqs: &PixelFormatRequirements, opengl: &GlAttributes<&Window>)
|
||||||
-> Result<(IdRef, PixelFormat), CreationError>
|
-> Result<(IdRef, PixelFormat), CreationError>
|
||||||
{
|
{
|
||||||
let profile = match (opengl.version, opengl.version.to_gl_version(), opengl.profile) {
|
let attributes = try!(helpers::build_nsattributes(pf_reqs, opengl));
|
||||||
|
|
||||||
// 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);
|
|
||||||
|
|
||||||
unsafe {
|
unsafe {
|
||||||
let pixelformat = IdRef::new(NSOpenGLPixelFormat::alloc(nil).initWithAttributes_(&attributes));
|
let pixelformat = IdRef::new(NSOpenGLPixelFormat::alloc(nil).initWithAttributes_(&attributes));
|
||||||
|
|
||||||
|
|
|
@ -1,28 +1,61 @@
|
||||||
extern crate glutin;
|
extern crate glutin;
|
||||||
extern crate libc;
|
extern crate libc;
|
||||||
|
use glutin::*;
|
||||||
|
use std::ptr;
|
||||||
|
|
||||||
mod gl {
|
mod gl {
|
||||||
|
pub use self::Gles2 as Gl;
|
||||||
include!(concat!(env!("OUT_DIR"), "/test_gl_bindings.rs"));
|
include!(concat!(env!("OUT_DIR"), "/test_gl_bindings.rs"));
|
||||||
}
|
}
|
||||||
|
use gl::types::*;
|
||||||
|
|
||||||
#[cfg(feature = "headless")]
|
|
||||||
|
#[cfg(target_os = "macos")]
|
||||||
#[test]
|
#[test]
|
||||||
fn main() {
|
fn test_headless() {
|
||||||
let window = glutin::HeadlessRendererBuilder::new(1024, 768).build().unwrap();
|
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() };
|
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 {
|
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.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");
|
||||||
|
}
|
||||||
|
|
||||||
gl.ClearColor(0.0, 1.0, 0.0, 1.0);
|
gl.ClearColor(0.0, 1.0, 0.0, 1.0);
|
||||||
gl.Clear(gl::COLOR_BUFFER_BIT);
|
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();
|
let mut values: Vec<u8> = vec![0;(width*height*4) as usize];
|
||||||
gl.ReadPixels(0, 0, 1, 1, gl::RGBA, gl::UNSIGNED_BYTE, std::mem::transmute(&mut value));
|
gl.ReadPixels(0, 0, width, height, gl::RGBA, gl::UNSIGNED_BYTE, values.as_mut_ptr() as *mut GLvoid);
|
||||||
|
|
||||||
assert!(value == (0, 255, 0, 255) || value == (0, 64, 0, 255) ||
|
assert_eq!(values[0], 0);
|
||||||
value == (0, 64, 0, 255) || value == (0, 64, 0, 0),
|
assert_eq!(values[1], 255);
|
||||||
"value is: {:?}", value);
|
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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue