-
Notifications
You must be signed in to change notification settings - Fork 102
macOS: move from objc and cocoa crates to the objc2 crates
#240
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
prokopyl
wants to merge
12
commits into
master
Choose a base branch
from
obcj2
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
bc2e336
wip on objc2 migration
prokopyl adcc5d1
wip
prokopyl 1cc2807
fixes
prokopyl c939c41
Use "C-unwind" calling convention
prokopyl 6b48cb8
Leaner objc2
prokopyl 44b4b5b
opengl fixes
prokopyl 4cabe2f
smol fix
prokopyl 31ccb0b
Fix setDelegate call
prokopyl 7f35382
Extra safety comments
prokopyl 1a43f82
More comments
prokopyl cb976e7
Fix remaining TODOs
prokopyl acc4ea1
Remove objc workaround
prokopyl File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,30 +1,26 @@ | ||
| use std::ffi::c_void; | ||
| use std::str::FromStr; | ||
|
|
||
| use raw_window_handle::RawWindowHandle; | ||
| #![allow(deprecated)] // OpenGL is deprecated on macOS | ||
|
|
||
| use cocoa::appkit::{ | ||
| 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 cocoa::base::{id, nil, YES}; | ||
| use cocoa::foundation::NSSize; | ||
|
|
||
| use core_foundation::base::TCFType; | ||
| use core_foundation::bundle::{CFBundleGetBundleWithIdentifier, CFBundleGetFunctionPointerForName}; | ||
| use core_foundation::string::CFString; | ||
|
|
||
| use objc::{msg_send, sel, sel_impl}; | ||
|
|
||
| use super::{GlConfig, GlError, Profile}; | ||
| 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: id, | ||
| context: id, | ||
| view: Retained<NSOpenGLView>, | ||
| context: Retained<NSOpenGLContext>, | ||
| } | ||
|
|
||
| impl GlContext { | ||
|
|
@@ -35,11 +31,10 @@ impl GlContext { | |
| return Err(GlError::InvalidWindowHandle); | ||
| }; | ||
|
|
||
| if handle.ns_view.is_null() { | ||
| let parent_view = handle.ns_view.cast::<NSView>(); | ||
| let Some(parent_view) = parent_view.as_ref() else { | ||
| return Err(GlError::InvalidWindowHandle); | ||
| } | ||
|
|
||
| let parent_view = handle.ns_view as id; | ||
| }; | ||
|
|
||
| let version = if config.version < (3, 2) && config.profile == Profile::Compatibility { | ||
| NSOpenGLProfileVersionLegacy | ||
|
|
@@ -76,33 +71,29 @@ impl GlContext { | |
|
|
||
| attrs.push(0); | ||
|
|
||
| let pixel_format = NSOpenGLPixelFormat::alloc(nil).initWithAttributes_(&attrs); | ||
|
|
||
| if pixel_format == nil { | ||
| return Err(GlError::CreationFailed(())); | ||
| } | ||
|
|
||
| let view = | ||
| NSOpenGLView::alloc(nil).initWithFrame_pixelFormat_(parent_view.frame(), pixel_format); | ||
| let pixel_format = NSOpenGLPixelFormat::initWithAttributes( | ||
| NSOpenGLPixelFormat::alloc(), | ||
| NonNull::new(attrs.as_mut_ptr()).unwrap(), | ||
| ) | ||
| .ok_or(GlError::CreationFailed(()))?; | ||
|
|
||
| if view == nil { | ||
| return Err(GlError::CreationFailed(())); | ||
| } | ||
| let view = NSOpenGLView::initWithFrame_pixelFormat( | ||
| NSOpenGLView::alloc(MainThreadMarker::new().unwrap()), | ||
| parent_view.frame(), | ||
| Some(&pixel_format), | ||
| ) | ||
| .ok_or(GlError::CreationFailed(()))?; | ||
|
|
||
| view.setWantsBestResolutionOpenGLSurface_(YES); | ||
| view.setWantsBestResolutionOpenGLSurface(true); | ||
|
|
||
| NSOpenGLView::display_(view); | ||
| parent_view.addSubview_(view); | ||
| view.display(); | ||
| parent_view.addSubview(&view); | ||
|
|
||
| let context: id = msg_send![view, openGLContext]; | ||
| let () = msg_send![context, retain]; | ||
| let context = view.openGLContext().ok_or(GlError::CreationFailed(()))?; | ||
|
|
||
| context.setValues_forParameter_( | ||
| &(config.vsync as i32), | ||
| NSOpenGLContextParameter::NSOpenGLCPSwapInterval, | ||
| ); | ||
| let value = config.vsync as i32; | ||
|
|
||
| let () = msg_send![pixel_format, release]; | ||
| context.setValues_forParameter((&value).into(), NSOpenGLContextParameter::SwapInterval); | ||
|
|
||
| Ok(GlContext { view, context }) | ||
| } | ||
|
|
@@ -112,47 +103,35 @@ impl GlContext { | |
| } | ||
|
|
||
| pub unsafe fn make_not_current(&self) { | ||
| NSOpenGLContext::clearCurrentContext(self.context); | ||
| if Some(&self.context) == NSOpenGLContext::currentContext().as_ref() { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since this changes the logic from how it was before, just wanted to confirm that you've specifically tested that this still works (that the equality check does what you'd expect). |
||
| NSOpenGLContext::clearCurrentContext(); | ||
| } | ||
| } | ||
|
|
||
| pub fn get_proc_address(&self, symbol: &str) -> *const c_void { | ||
| let symbol_name = CFString::from_str(symbol).unwrap(); | ||
| let framework_name = CFString::from_str("com.apple.opengl").unwrap(); | ||
| let framework = | ||
| unsafe { CFBundleGetBundleWithIdentifier(framework_name.as_concrete_TypeRef()) }; | ||
| 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(); | ||
|
|
||
| unsafe { CFBundleGetFunctionPointerForName(framework, symbol_name.as_concrete_TypeRef()) } | ||
| CFBundle::function_pointer_for_name(&framework, Some(&symbol_name)) | ||
| } | ||
|
|
||
| pub fn swap_buffers(&self) { | ||
| unsafe { | ||
| self.context.flushBuffer(); | ||
| let () = msg_send![self.view, setNeedsDisplay: YES]; | ||
| } | ||
| 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) { | ||
| unsafe { NSView::setFrameSize(self.view, size) }; | ||
| unsafe { | ||
| let _: () = msg_send![self.view, setNeedsDisplay: YES]; | ||
| } | ||
| 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) -> id { | ||
| self.view | ||
| } | ||
| } | ||
|
|
||
| impl Drop for GlContext { | ||
| fn drop(&mut self) { | ||
| unsafe { | ||
| let () = msg_send![self.context, release]; | ||
| let () = msg_send![self.view, release]; | ||
| } | ||
| pub(crate) fn ns_view(&self) -> &NSOpenGLView { | ||
| &self.view | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Any reason for doing this in the same change as migrating to
objc2?