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 all commits
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
166 changes: 41 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 @@ -51,8 +24,11 @@ extern_class!(
#[derive(Debug, PartialEq, Eq, Hash)]
pub(crate) struct NSOpenGLContext;

// Strict order required by macro, tracked in https://github.com/madsmtm/objc2/issues/479
#[rustfmt::skip]
unsafe impl ClassType for NSOpenGLContext {
type Super = NSObject;
type Mutability = mutability::InteriorMutable;
}
);

Expand All @@ -61,50 +37,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 @@ -113,8 +82,11 @@ extern_class!(
#[derive(Debug, PartialEq, Eq, Hash)]
pub(crate) struct NSOpenGLPixelFormat;

// Strict order required by macro, tracked in https://github.com/madsmtm/objc2/issues/479
#[rustfmt::skip]
unsafe impl ClassType for NSOpenGLPixelFormat {
type Super = NSObject;
type Mutability = mutability::Immutable;
}
);

Expand All @@ -123,18 +95,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 +116,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
Loading
Loading