Skip to content
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

Update objc2 to v0.4 #1617

Merged
merged 3 commits into from
Aug 6, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion glutin/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,21 @@ x11-dl = { version = "2.20.0", optional = true }
[target.'cfg(any(target_os = "macos"))'.dependencies]
cgl = "0.3.2"
core-foundation = "0.9.3"
objc2 = ">=0.3.0-beta.3, <0.3.0-beta.4"
objc2 = "0.4.1"
dispatch = "0.2.0"

[target.'cfg(any(target_os = "macos"))'.dependencies.icrate]
version = "0.0.4"
features = [
"dispatch",
"Foundation",
"Foundation_NSArray",
"Foundation_NSThread",
"AppKit",
"AppKit_NSView",
"AppKit_NSWindow",
]

[build-dependencies]
cfg_aliases = "0.1.1"

Expand Down
162 changes: 37 additions & 125 deletions glutin/src/api/cgl/appkit.rs
Original file line number Diff line number Diff line change
@@ -1,46 +1,19 @@
//! The parts of AppKit related to OpenGL.
//!
//! TODO: Move this to another crate.
//! Parts of AppKit related to OpenGL that is not yet in `icrate`.
#![allow(dead_code)]
#![allow(non_snake_case)]

use std::ops::Deref;

use dispatch::Queue;
#[allow(deprecated)]
use icrate::AppKit::{NSOpenGLContextParameter, NSOpenGLPixelFormatAttribute, NSView};
use icrate::Foundation::{MainThreadMarker, NSObject};
use objc2::encode::{Encoding, RefEncode};
use objc2::foundation::{is_main_thread, NSInteger, NSObject};
use objc2::rc::{Id, Shared};
use objc2::{extern_class, extern_methods, msg_send_id, ClassType};
use objc2::rc::{Allocated, Id};
use objc2::{extern_class, extern_methods, mutability, ClassType};

pub type GLint = i32;

pub enum CGLContextObj {}

// XXX borrowed from winit.

// Unsafe wrapper type that allows us to dispatch things that aren't Send.
// This should *only* be used to dispatch to the main queue.
// While it is indeed not guaranteed that these types can safely be sent to
// other threads, we know that they're safe to use on the main thread.
pub(crate) struct MainThreadSafe<T>(pub(crate) T);

unsafe impl<T> Send for MainThreadSafe<T> {}

impl<T> Deref for MainThreadSafe<T> {
type Target = T;

fn deref(&self) -> &T {
&self.0
}
}

/// Run closure on the main thread.
pub(crate) fn run_on_main<R: Send>(f: impl FnOnce() -> R + Send) -> R {
if is_main_thread() {
f()
} else {
Queue::main().exec_sync(f)
}
#[repr(C)]
pub struct CGLContextObj {
__inner: [u8; 0],
}
kchibisov marked this conversation as resolved.
Show resolved Hide resolved

unsafe impl RefEncode for CGLContextObj {
Expand All @@ -53,6 +26,7 @@ extern_class!(

unsafe impl ClassType for NSOpenGLContext {
type Super = NSObject;
type Mutability = mutability::InteriorMutable;
}
);

Expand All @@ -61,50 +35,43 @@ unsafe impl Sync for NSOpenGLContext {}

extern_methods!(
unsafe impl NSOpenGLContext {
pub(crate) fn currentContext() -> Option<Id<Self, Shared>> {
unsafe { msg_send_id![Self::class(), currentContext] }
}
#[method_id(currentContext)]
pub(crate) fn currentContext() -> Option<Id<Self>>;

pub(crate) fn newWithFormat_shareContext(
#[method_id(initWithFormat:shareContext:)]
pub(crate) fn initWithFormat_shareContext(
this: Option<Allocated<Self>>,
format: &NSOpenGLPixelFormat,
share: Option<&NSOpenGLContext>,
) -> Option<Id<Self, Shared>> {
unsafe {
msg_send_id![
msg_send_id![Self::class(), alloc],
initWithFormat: format,
shareContext: share,
]
}
}
) -> Option<Id<Self>>;

#[sel(clearCurrentContext)]
#[method(clearCurrentContext)]
pub(crate) fn clearCurrentContext();

#[sel(makeCurrentContext)]
#[method(makeCurrentContext)]
pub(crate) fn makeCurrentContext(&self);

#[sel(update)]
#[method(update)]
pub(crate) fn update(&self);

#[sel(flushBuffer)]
#[method(flushBuffer)]
pub(crate) fn flushBuffer(&self);

pub(crate) fn view(&self) -> Option<Id<NSObject, Shared>> {
unsafe { msg_send_id![self, view] }
}
#[method_id(view)]
pub(crate) fn view(&self, mtm: MainThreadMarker) -> Option<Id<NSView>>;

#[sel(setView:)]
pub(crate) unsafe fn setView(&self, view: Option<&NSObject>);
#[method(setView:)]
pub(crate) unsafe fn setView(&self, view: Option<&NSView>);

#[sel(setValues:forParameter:)]
#[allow(deprecated)]
#[method(setValues:forParameter:)]
pub(crate) unsafe fn setValues_forParameter(
&self,
vals: *const GLint,
param: NSOpenGLContextParameter,
);

#[sel(CGLContextObj)]
#[method(CGLContextObj)]
pub(crate) fn CGLContextObj(&self) -> *mut CGLContextObj;
}
);
Expand All @@ -115,6 +82,7 @@ extern_class!(

unsafe impl ClassType for NSOpenGLPixelFormat {
type Super = NSObject;
type Mutability = mutability::Immutable;
}
);

Expand All @@ -123,18 +91,19 @@ unsafe impl Sync for NSOpenGLPixelFormat {}

extern_methods!(
unsafe impl NSOpenGLPixelFormat {
#[method_id(initWithAttributes:)]
unsafe fn initWithAttributes(
this: Option<Allocated<Self>>,
attrs: *const NSOpenGLPixelFormatAttribute,
) -> Option<Id<Self>>;

pub(crate) unsafe fn newWithAttributes(
attrs: &[NSOpenGLPixelFormatAttribute],
) -> Option<Id<Self, Shared>> {
unsafe {
msg_send_id![
msg_send_id![Self::class(), alloc],
initWithAttributes: attrs.as_ptr(),
]
}
) -> Option<Id<Self>> {
unsafe { Self::initWithAttributes(Self::alloc(), attrs.as_ptr()) }
}

#[sel(getValues:forAttribute:forVirtualScreen:)]
#[method(getValues:forAttribute:forVirtualScreen:)]
pub(crate) unsafe fn getValues_forAttribute_forVirtualScreen(
&self,
vals: *mut GLint,
Expand All @@ -143,60 +112,3 @@ extern_methods!(
);
}
);

type NSOpenGLContextParameter = NSInteger;
kchibisov marked this conversation as resolved.
Show resolved Hide resolved
pub(crate) const NSOpenGLCPSwapInterval: NSOpenGLContextParameter = 222;
pub(crate) const NSOpenGLCPSurfaceOrder: NSOpenGLContextParameter = 235;
pub(crate) const NSOpenGLCPSurfaceOpacity: NSOpenGLContextParameter = 236;
pub(crate) const NSOpenGLCPSurfaceBackingSize: NSOpenGLContextParameter = 304;
pub(crate) const NSOpenGLCPReclaimResources: NSOpenGLContextParameter = 308;
pub(crate) const NSOpenGLCPCurrentRendererID: NSOpenGLContextParameter = 309;
pub(crate) const NSOpenGLCPGPUVertexProcessing: NSOpenGLContextParameter = 310;
pub(crate) const NSOpenGLCPGPUFragmentProcessing: NSOpenGLContextParameter = 311;
pub(crate) const NSOpenGLCPHasDrawable: NSOpenGLContextParameter = 314;
pub(crate) const NSOpenGLCPMPSwapsInFlight: NSOpenGLContextParameter = 315;

pub(crate) type NSOpenGLPixelFormatAttribute = u32;
pub(crate) const NSOpenGLPFAAllRenderers: NSOpenGLPixelFormatAttribute = 1;
pub(crate) const NSOpenGLPFATripleBuffer: NSOpenGLPixelFormatAttribute = 3;
pub(crate) const NSOpenGLPFADoubleBuffer: NSOpenGLPixelFormatAttribute = 5;
pub(crate) const NSOpenGLPFAStereo: NSOpenGLPixelFormatAttribute = 6;
pub(crate) const NSOpenGLPFAAuxBuffers: NSOpenGLPixelFormatAttribute = 7;
pub(crate) const NSOpenGLPFAColorSize: NSOpenGLPixelFormatAttribute = 8;
pub(crate) const NSOpenGLPFAAlphaSize: NSOpenGLPixelFormatAttribute = 11;
pub(crate) const NSOpenGLPFADepthSize: NSOpenGLPixelFormatAttribute = 12;
pub(crate) const NSOpenGLPFAStencilSize: NSOpenGLPixelFormatAttribute = 13;
pub(crate) const NSOpenGLPFAAccumSize: NSOpenGLPixelFormatAttribute = 14;
pub(crate) const NSOpenGLPFAMinimumPolicy: NSOpenGLPixelFormatAttribute = 51;
pub(crate) const NSOpenGLPFAMaximumPolicy: NSOpenGLPixelFormatAttribute = 52;
pub(crate) const NSOpenGLPFAOffScreen: NSOpenGLPixelFormatAttribute = 53;
pub(crate) const NSOpenGLPFAFullScreen: NSOpenGLPixelFormatAttribute = 54;
pub(crate) const NSOpenGLPFASampleBuffers: NSOpenGLPixelFormatAttribute = 55;
pub(crate) const NSOpenGLPFASamples: NSOpenGLPixelFormatAttribute = 56;
pub(crate) const NSOpenGLPFAAuxDepthStencil: NSOpenGLPixelFormatAttribute = 57;
pub(crate) const NSOpenGLPFAColorFloat: NSOpenGLPixelFormatAttribute = 58;
pub(crate) const NSOpenGLPFAMultisample: NSOpenGLPixelFormatAttribute = 59;
pub(crate) const NSOpenGLPFASupersample: NSOpenGLPixelFormatAttribute = 60;
pub(crate) const NSOpenGLPFASampleAlpha: NSOpenGLPixelFormatAttribute = 61;
pub(crate) const NSOpenGLPFARendererID: NSOpenGLPixelFormatAttribute = 70;
pub(crate) const NSOpenGLPFASingleRenderer: NSOpenGLPixelFormatAttribute = 71;
pub(crate) const NSOpenGLPFANoRecovery: NSOpenGLPixelFormatAttribute = 72;
pub(crate) const NSOpenGLPFAAccelerated: NSOpenGLPixelFormatAttribute = 73;
pub(crate) const NSOpenGLPFAClosestPolicy: NSOpenGLPixelFormatAttribute = 74;
pub(crate) const NSOpenGLPFARobust: NSOpenGLPixelFormatAttribute = 75;
pub(crate) const NSOpenGLPFABackingStore: NSOpenGLPixelFormatAttribute = 76;
pub(crate) const NSOpenGLPFAMPSafe: NSOpenGLPixelFormatAttribute = 78;
pub(crate) const NSOpenGLPFAWindow: NSOpenGLPixelFormatAttribute = 80;
pub(crate) const NSOpenGLPFAMultiScreen: NSOpenGLPixelFormatAttribute = 81;
pub(crate) const NSOpenGLPFACompliant: NSOpenGLPixelFormatAttribute = 83;
pub(crate) const NSOpenGLPFAScreenMask: NSOpenGLPixelFormatAttribute = 84;
pub(crate) const NSOpenGLPFAPixelBuffer: NSOpenGLPixelFormatAttribute = 90;
pub(crate) const NSOpenGLPFARemotePixelBuffer: NSOpenGLPixelFormatAttribute = 91;
pub(crate) const NSOpenGLPFAAllowOfflineRenderers: NSOpenGLPixelFormatAttribute = 96;
pub(crate) const NSOpenGLPFAAcceleratedCompute: NSOpenGLPixelFormatAttribute = 97;
pub(crate) const NSOpenGLPFAOpenGLProfile: NSOpenGLPixelFormatAttribute = 99;
pub(crate) const NSOpenGLPFAVirtualScreenCount: NSOpenGLPixelFormatAttribute = 128;
// OpenGL Profiles
pub(crate) const NSOpenGLProfileVersionLegacy: NSOpenGLPixelFormatAttribute = 0x1000;
pub(crate) const NSOpenGLProfileVersion3_2Core: NSOpenGLPixelFormatAttribute = 0x3200;
pub(crate) const NSOpenGLProfileVersion4_1Core: NSOpenGLPixelFormatAttribute = 0x4100;
25 changes: 15 additions & 10 deletions glutin/src/api/cgl/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,16 @@
use std::sync::Arc;
use std::{fmt, iter};

use objc2::rc::{Id, Shared};
#[allow(deprecated)]
use icrate::AppKit::{
NSOpenGLPFAAccelerated, NSOpenGLPFAAllowOfflineRenderers, NSOpenGLPFAAlphaSize,
NSOpenGLPFAColorFloat, NSOpenGLPFAColorSize, NSOpenGLPFADepthSize, NSOpenGLPFADoubleBuffer,
NSOpenGLPFAMinimumPolicy, NSOpenGLPFAMultisample, NSOpenGLPFAOpenGLProfile,
NSOpenGLPFASampleBuffers, NSOpenGLPFASamples, NSOpenGLPFAStencilSize, NSOpenGLPFAStereo,
NSOpenGLPFATripleBuffer, NSOpenGLPixelFormatAttribute, NSOpenGLProfileVersion3_2Core,
NSOpenGLProfileVersion4_1Core, NSOpenGLProfileVersionLegacy,
};
use objc2::rc::Id;

use crate::config::{
Api, AsRawConfig, ColorBufferType, ConfigSurfaceTypes, ConfigTemplate, GlConfig, RawConfig,
Expand All @@ -12,17 +21,11 @@ use crate::display::GetGlDisplay;
use crate::error::{ErrorKind, Result};
use crate::private::Sealed;

use super::appkit::{
NSOpenGLPFAAccelerated, NSOpenGLPFAAllowOfflineRenderers, NSOpenGLPFAAlphaSize,
NSOpenGLPFAColorFloat, NSOpenGLPFAColorSize, NSOpenGLPFADepthSize, NSOpenGLPFADoubleBuffer,
NSOpenGLPFAMinimumPolicy, NSOpenGLPFAMultisample, NSOpenGLPFAOpenGLProfile,
NSOpenGLPFASampleBuffers, NSOpenGLPFASamples, NSOpenGLPFAStencilSize, NSOpenGLPFAStereo,
NSOpenGLPFATripleBuffer, NSOpenGLPixelFormat, NSOpenGLPixelFormatAttribute,
NSOpenGLProfileVersion3_2Core, NSOpenGLProfileVersion4_1Core, NSOpenGLProfileVersionLegacy,
};
use super::appkit::NSOpenGLPixelFormat;
use super::display::Display;

impl Display {
#[allow(deprecated)]
pub(crate) unsafe fn find_configs(
&self,
template: ConfigTemplate,
Expand Down Expand Up @@ -145,12 +148,14 @@ impl Config {
}
}

#[allow(deprecated)]
pub(crate) fn is_single_buffered(&self) -> bool {
self.raw_attribute(NSOpenGLPFATripleBuffer) == 0
&& self.raw_attribute(NSOpenGLPFADoubleBuffer) == 0
}
}

#[allow(deprecated)]
impl GlConfig for Config {
fn color_buffer_type(&self) -> Option<ColorBufferType> {
// On macos all color formats divide by 3 without reminder, except for the RGB
Expand Down Expand Up @@ -223,7 +228,7 @@ impl Sealed for Config {}
pub(crate) struct ConfigInner {
display: Display,
pub(crate) transparency: bool,
pub(crate) raw: Id<NSOpenGLPixelFormat, Shared>,
pub(crate) raw: Id<NSOpenGLPixelFormat>,
}

impl PartialEq for ConfigInner {
Expand Down
36 changes: 20 additions & 16 deletions glutin/src/api/cgl/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@
use std::marker::PhantomData;

use cgl::CGLSetParameter;
use objc2::foundation::NSObject;
use objc2::rc::{autoreleasepool, Id, Shared};
use icrate::AppKit::{NSOpenGLCPSwapInterval, NSView};
use icrate::Foundation::{MainThreadBound, MainThreadMarker};
use objc2::rc::{autoreleasepool, Id};
use objc2::ClassType;

use crate::config::GetGlConfig;
use crate::context::{AsRawContext, ContextApi, ContextAttributes, RawContext, Robustness};
Expand All @@ -15,7 +17,7 @@
use crate::private::Sealed;
use crate::surface::{SurfaceTypeTrait, SwapInterval};

use super::appkit::{run_on_main, MainThreadSafe, NSOpenGLCPSwapInterval, NSOpenGLContext};
use super::appkit::NSOpenGLContext;
use super::config::Config;
use super::display::Display;
use super::surface::Surface;
Expand All @@ -42,8 +44,12 @@
}

let config = config.clone();
let raw = NSOpenGLContext::newWithFormat_shareContext(&config.inner.raw, share_context)
.ok_or(ErrorKind::BadConfig)?;
let raw = NSOpenGLContext::initWithFormat_shareContext(
NSOpenGLContext::alloc(),
&config.inner.raw,
share_context,
)
.ok_or(ErrorKind::BadConfig)?;

if config.inner.transparency {
let opacity = 0;
Expand Down Expand Up @@ -200,7 +206,7 @@
pub(crate) struct ContextInner {
display: Display,
config: Config,
pub(crate) raw: Id<NSOpenGLContext, Shared>,
pub(crate) raw: Id<NSOpenGLContext>,
}

impl ContextInner {
Expand All @@ -216,11 +222,10 @@
autoreleasepool(|_| {
self.update();
self.raw.makeCurrentContext();
let raw = MainThreadSafe(&self.raw);
let ns_view = MainThreadSafe(&surface.ns_view);

run_on_main(move || unsafe {
raw.setView(Some(*ns_view));
let view = &surface.ns_view;
MainThreadMarker::run_on_main(|mtm| unsafe {
self.raw.setView(Some(&view.get(mtm)));

Check failure on line 228 in glutin/src/api/cgl/context.rs

View workflow job for this annotation

GitHub Actions / Tests (1.65.0, x86_64-apple-darwin, macos-latest)

this expression creates a reference which is immediately dereferenced by the compiler
madsmtm marked this conversation as resolved.
Show resolved Hide resolved
});

Ok(())
Expand All @@ -243,10 +248,7 @@
}

pub(crate) fn update(&self) {
let raw = MainThreadSafe(&self.raw);
run_on_main(move || {
raw.update();
});
MainThreadMarker::run_on_main(|_| self.raw.update());
}

pub(crate) fn flush_buffer(&self) -> Result<()> {
Expand All @@ -256,8 +258,10 @@
})
}

pub(crate) fn current_view(&self) -> Id<NSObject, Shared> {
self.raw.view().expect("context to have a current view")
pub(crate) fn is_view_current(&self, view: &MainThreadBound<Id<NSView>>) -> bool {
MainThreadMarker::run_on_main(|mtm| {
self.raw.view(mtm).expect("context to have a current view") == *view.get(mtm)
})
}

fn make_not_current(&self) -> Result<()> {
Expand Down
Loading
Loading