|
| 1 | +use std::ffi::c_void; |
| 2 | +use std::str::FromStr; |
| 3 | + |
| 4 | +use raw_window_handle::{HasRawWindowHandle, RawWindowHandle}; |
| 5 | + |
| 6 | +use cocoa::appkit::{ |
| 7 | + NSOpenGLContext, NSOpenGLContextParameter, NSOpenGLPFAAccelerated, NSOpenGLPFAAlphaSize, |
| 8 | + NSOpenGLPFAColorSize, NSOpenGLPFADepthSize, NSOpenGLPFADoubleBuffer, NSOpenGLPFAMultisample, |
| 9 | + NSOpenGLPFAOpenGLProfile, NSOpenGLPFASampleBuffers, NSOpenGLPFASamples, NSOpenGLPFAStencilSize, |
| 10 | + NSOpenGLPixelFormat, NSOpenGLProfileVersion3_2Core, NSOpenGLProfileVersion4_1Core, |
| 11 | + NSOpenGLProfileVersionLegacy, NSOpenGLView, NSView, |
| 12 | +}; |
| 13 | +use cocoa::base::{id, nil, YES}; |
| 14 | + |
| 15 | +use core_foundation::base::TCFType; |
| 16 | +use core_foundation::bundle::{CFBundleGetBundleWithIdentifier, CFBundleGetFunctionPointerForName}; |
| 17 | +use core_foundation::string::CFString; |
| 18 | + |
| 19 | +use objc::{msg_send, sel, sel_impl}; |
| 20 | + |
| 21 | +use super::{GlConfig, GlError, Profile}; |
| 22 | + |
| 23 | +pub type CreationFailedError = (); |
| 24 | +pub struct GlContext { |
| 25 | + view: id, |
| 26 | + context: id, |
| 27 | +} |
| 28 | + |
| 29 | +impl GlContext { |
| 30 | + pub unsafe fn create( |
| 31 | + parent: &impl HasRawWindowHandle, config: GlConfig, |
| 32 | + ) -> Result<GlContext, GlError> { |
| 33 | + let handle = if let RawWindowHandle::AppKit(handle) = parent.raw_window_handle() { |
| 34 | + handle |
| 35 | + } else { |
| 36 | + return Err(GlError::InvalidWindowHandle); |
| 37 | + }; |
| 38 | + |
| 39 | + if handle.ns_view.is_null() { |
| 40 | + return Err(GlError::InvalidWindowHandle); |
| 41 | + } |
| 42 | + |
| 43 | + let parent_view = handle.ns_view as id; |
| 44 | + |
| 45 | + let version = if config.version < (3, 2) && config.profile == Profile::Compatibility { |
| 46 | + NSOpenGLProfileVersionLegacy |
| 47 | + } else if config.version == (3, 2) && config.profile == Profile::Core { |
| 48 | + NSOpenGLProfileVersion3_2Core |
| 49 | + } else if config.version > (3, 2) && config.profile == Profile::Core { |
| 50 | + NSOpenGLProfileVersion4_1Core |
| 51 | + } else { |
| 52 | + return Err(GlError::VersionNotSupported); |
| 53 | + }; |
| 54 | + |
| 55 | + #[rustfmt::skip] |
| 56 | + let mut attrs = vec![ |
| 57 | + NSOpenGLPFAOpenGLProfile as u32, version as u32, |
| 58 | + NSOpenGLPFAColorSize as u32, (config.red_bits + config.blue_bits + config.green_bits) as u32, |
| 59 | + NSOpenGLPFAAlphaSize as u32, config.alpha_bits as u32, |
| 60 | + NSOpenGLPFADepthSize as u32, config.depth_bits as u32, |
| 61 | + NSOpenGLPFAStencilSize as u32, config.stencil_bits as u32, |
| 62 | + NSOpenGLPFAAccelerated as u32, |
| 63 | + ]; |
| 64 | + |
| 65 | + if config.samples.is_some() { |
| 66 | + #[rustfmt::skip] |
| 67 | + attrs.extend_from_slice(&[ |
| 68 | + NSOpenGLPFAMultisample as u32, |
| 69 | + NSOpenGLPFASampleBuffers as u32, 1, |
| 70 | + NSOpenGLPFASamples as u32, config.samples.unwrap() as u32, |
| 71 | + ]); |
| 72 | + } |
| 73 | + |
| 74 | + if config.double_buffer { |
| 75 | + attrs.push(NSOpenGLPFADoubleBuffer as u32); |
| 76 | + } |
| 77 | + |
| 78 | + attrs.push(0); |
| 79 | + |
| 80 | + let pixel_format = NSOpenGLPixelFormat::alloc(nil).initWithAttributes_(&attrs); |
| 81 | + |
| 82 | + if pixel_format == nil { |
| 83 | + return Err(GlError::CreationFailed(())); |
| 84 | + } |
| 85 | + |
| 86 | + let view = |
| 87 | + NSOpenGLView::alloc(nil).initWithFrame_pixelFormat_(parent_view.frame(), pixel_format); |
| 88 | + |
| 89 | + if view == nil { |
| 90 | + return Err(GlError::CreationFailed(())); |
| 91 | + } |
| 92 | + |
| 93 | + view.setWantsBestResolutionOpenGLSurface_(YES); |
| 94 | + |
| 95 | + let () = msg_send![view, retain]; |
| 96 | + NSOpenGLView::display_(view); |
| 97 | + parent_view.addSubview_(view); |
| 98 | + |
| 99 | + let context: id = msg_send![view, openGLContext]; |
| 100 | + let () = msg_send![context, retain]; |
| 101 | + |
| 102 | + context.setValues_forParameter_( |
| 103 | + &(config.vsync as i32), |
| 104 | + NSOpenGLContextParameter::NSOpenGLCPSwapInterval, |
| 105 | + ); |
| 106 | + |
| 107 | + let () = msg_send![pixel_format, release]; |
| 108 | + |
| 109 | + Ok(GlContext { view, context }) |
| 110 | + } |
| 111 | + |
| 112 | + pub unsafe fn make_current(&self) { |
| 113 | + self.context.makeCurrentContext(); |
| 114 | + } |
| 115 | + |
| 116 | + pub unsafe fn make_not_current(&self) { |
| 117 | + NSOpenGLContext::clearCurrentContext(self.context); |
| 118 | + } |
| 119 | + |
| 120 | + pub fn get_proc_address(&self, symbol: &str) -> *const c_void { |
| 121 | + let symbol_name = CFString::from_str(symbol).unwrap(); |
| 122 | + let framework_name = CFString::from_str("com.apple.opengl").unwrap(); |
| 123 | + let framework = |
| 124 | + unsafe { CFBundleGetBundleWithIdentifier(framework_name.as_concrete_TypeRef()) }; |
| 125 | + let addr = unsafe { |
| 126 | + CFBundleGetFunctionPointerForName(framework, symbol_name.as_concrete_TypeRef()) |
| 127 | + }; |
| 128 | + addr as *const c_void |
| 129 | + } |
| 130 | + |
| 131 | + pub fn swap_buffers(&self) { |
| 132 | + unsafe { |
| 133 | + self.context.flushBuffer(); |
| 134 | + let () = msg_send![self.view, setNeedsDisplay: YES]; |
| 135 | + } |
| 136 | + } |
| 137 | +} |
| 138 | + |
| 139 | +impl Drop for GlContext { |
| 140 | + fn drop(&mut self) { |
| 141 | + unsafe { |
| 142 | + let () = msg_send![self.context, release]; |
| 143 | + let () = msg_send![self.view, release]; |
| 144 | + } |
| 145 | + } |
| 146 | +} |
0 commit comments