-
Notifications
You must be signed in to change notification settings - Fork 124
Expand file tree
/
Copy pathmacos.rs
More file actions
135 lines (111 loc) · 4.79 KB
/
Copy pathmacos.rs
File metadata and controls
135 lines (111 loc) · 4.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#![allow(deprecated)] // OpenGL is deprecated on macOS
use super::{GlConfig, GlError, Profile};
use objc2::rc::Retained;
use objc2::AllocAnyThread;
use objc2::{MainThreadMarker, MainThreadOnly};
use objc2_app_kit::{
NSOpenGLContext, NSOpenGLContextParameter, NSOpenGLPFAAccelerated, NSOpenGLPFAAlphaSize,
NSOpenGLPFAColorSize, NSOpenGLPFADepthSize, NSOpenGLPFADoubleBuffer, NSOpenGLPFAMultisample,
NSOpenGLPFAOpenGLProfile, NSOpenGLPFASampleBuffers, NSOpenGLPFASamples, NSOpenGLPFAStencilSize,
NSOpenGLPixelFormat, NSOpenGLProfileVersion3_2Core, NSOpenGLProfileVersion4_1Core,
NSOpenGLProfileVersionLegacy, NSOpenGLView, NSView,
};
use objc2_core_foundation::{CFBundle, CFString};
use objc2_foundation::NSSize;
use raw_window_handle::RawWindowHandle;
use std::ffi::c_void;
use std::ptr::NonNull;
pub type CreationFailedError = ();
pub struct GlContext {
view: Retained<NSOpenGLView>,
context: Retained<NSOpenGLContext>,
}
impl GlContext {
pub unsafe fn create(parent: &RawWindowHandle, config: GlConfig) -> Result<GlContext, GlError> {
let handle = if let RawWindowHandle::AppKit(handle) = parent {
handle
} else {
return Err(GlError::InvalidWindowHandle);
};
let parent_view = handle.ns_view.cast::<NSView>();
let Some(parent_view) = parent_view.as_ref() else {
return Err(GlError::InvalidWindowHandle);
};
let version = if config.version < (3, 2) && config.profile == Profile::Compatibility {
NSOpenGLProfileVersionLegacy
} else if config.version == (3, 2) && config.profile == Profile::Core {
NSOpenGLProfileVersion3_2Core
} else if config.version > (3, 2) && config.profile == Profile::Core {
NSOpenGLProfileVersion4_1Core
} else {
return Err(GlError::VersionNotSupported);
};
#[rustfmt::skip]
let mut attrs = vec![
NSOpenGLPFAOpenGLProfile as u32, version as u32,
NSOpenGLPFAColorSize as u32, (config.red_bits + config.blue_bits + config.green_bits) as u32,
NSOpenGLPFAAlphaSize as u32, config.alpha_bits as u32,
NSOpenGLPFADepthSize as u32, config.depth_bits as u32,
NSOpenGLPFAStencilSize as u32, config.stencil_bits as u32,
NSOpenGLPFAAccelerated as u32,
];
if config.samples.is_some() {
#[rustfmt::skip]
attrs.extend_from_slice(&[
NSOpenGLPFAMultisample as u32,
NSOpenGLPFASampleBuffers as u32, 1,
NSOpenGLPFASamples as u32, config.samples.unwrap() as u32,
]);
}
if config.double_buffer {
attrs.push(NSOpenGLPFADoubleBuffer as u32);
}
attrs.push(0);
let pixel_format = NSOpenGLPixelFormat::initWithAttributes(
NSOpenGLPixelFormat::alloc(),
NonNull::new(attrs.as_mut_ptr()).unwrap(),
)
.ok_or(GlError::CreationFailed(()))?;
let view = NSOpenGLView::initWithFrame_pixelFormat(
NSOpenGLView::alloc(MainThreadMarker::new().unwrap()),
parent_view.frame(),
Some(&pixel_format),
)
.ok_or(GlError::CreationFailed(()))?;
view.setWantsBestResolutionOpenGLSurface(true);
view.display();
parent_view.addSubview(&view);
let context = view.openGLContext().ok_or(GlError::CreationFailed(()))?;
let value = config.vsync as i32;
context.setValues_forParameter((&value).into(), NSOpenGLContextParameter::SwapInterval);
Ok(GlContext { view, context })
}
pub unsafe fn make_current(&self) {
self.context.makeCurrentContext();
}
pub unsafe fn make_not_current(&self) {
NSOpenGLContext::clearCurrentContext();
}
pub fn get_proc_address(&self, symbol: &str) -> *const c_void {
let symbol_name = CFString::from_str(symbol);
let framework_name = CFString::from_static_str("com.apple.opengl");
let framework = CFBundle::bundle_with_identifier(Some(&framework_name)).unwrap();
CFBundle::function_pointer_for_name(&framework, Some(&symbol_name))
}
pub fn swap_buffers(&self) {
self.context.flushBuffer();
self.view.setNeedsDisplay(true);
}
/// On macOS the `NSOpenGLView` needs to be resized separtely from our main view.
pub(crate) fn resize(&self, size: NSSize) {
self.view.setFrameSize(size);
self.view.setNeedsDisplay(true);
}
/// Pointer to the `NSOpenGLView` this context renders into. Used by
/// the parent `NSView`'s `hitTest:` override to collapse hits on the
/// render subview to the parent, so AppKit routes `mouseDown:` on
/// first click in non-key windows.
pub(crate) fn ns_view(&self) -> &NSOpenGLView {
&self.view
}
}