Skip to content

Commit

Permalink
Update to objc2 v0.6
Browse files Browse the repository at this point in the history
  • Loading branch information
madsmtm committed Jan 24, 2025
1 parent 73ce4f9 commit 3c6cf86
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 75 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Unreleased

- **Breaking:** Added `make_current_surfaceless(self)` for `{Possibly,Not}CurrentGlContext`.
- Updated `objc2` dependency to `v0.6`.

# Version 0.32.2

Expand Down
31 changes: 17 additions & 14 deletions glutin/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -46,33 +46,36 @@ glutin_egl_sys = { version = "0.7.1", path = "../glutin_egl_sys" }
[target.'cfg(any(target_os = "linux", target_os = "freebsd", target_os = "dragonfly", target_os = "netbsd", target_os = "openbsd"))'.dependencies]
glutin_egl_sys = { version = "0.7.1", path = "../glutin_egl_sys", optional = true }
glutin_glx_sys = { version = "0.6.1", path = "../glutin_glx_sys", optional = true }
wayland-sys = { version = "0.31.1", default-features = false, features = ["egl", "client", "dlopen"], optional = true }
wayland-sys = { version = "0.31.1", default-features = false, features = [
"egl",
"client",
"dlopen",
], optional = true }
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.5.1"
dispatch = "0.2.0"

[target.'cfg(any(target_os = "macos"))'.dependencies.objc2-foundation]
version = "0.2.0"
features = [
"dispatch",
dispatch2 = { version = "0.2.0", default-features = false, features = [
"std",
"objc2",
] }
objc2 = "0.6.0"
objc2-foundation = { version = "0.3.0", default-features = false, features = [
"std",
"NSArray",
"NSThread",
]

[target.'cfg(any(target_os = "macos"))'.dependencies.objc2-app-kit]
version = "0.2.0"
features = [
] }
objc2-app-kit = { version = "0.3.0", default-features = false, features = [
"std",
"objc2-core-foundation",
"NSApplication",
"NSResponder",
"NSView",
"NSWindow",
"NSOpenGL",
"NSOpenGLView",
]
] }

[build-dependencies]
cfg_aliases = "0.2.1"
Expand Down
80 changes: 34 additions & 46 deletions glutin/src/api/cgl/appkit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
#![allow(non_snake_case)]

use objc2::encode::{Encoding, RefEncode};
use objc2::rc::{Allocated, Id};
use objc2::{extern_class, extern_methods, mutability, ClassType};
use objc2::rc::{Allocated, Retained};
use objc2::{extern_class, extern_methods, AllocAnyThread, MainThreadMarker};
#[allow(deprecated)]
use objc2_app_kit::{NSOpenGLContextParameter, NSOpenGLPixelFormatAttribute, NSView};
use objc2_foundation::{MainThreadMarker, NSObject};
use objc2_foundation::NSObject;

pub type GLint = i32;

Expand All @@ -21,98 +21,86 @@ unsafe impl RefEncode for CGLContextObj {
}

extern_class!(
#[unsafe(super(NSObject))]
#[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;
}
);

unsafe impl Send for NSOpenGLContext {}
unsafe impl Sync for NSOpenGLContext {}

extern_methods!(
unsafe impl NSOpenGLContext {
#[method_id(currentContext)]
pub(crate) fn currentContext() -> Option<Id<Self>>;
impl NSOpenGLContext {
extern_methods!(
#[unsafe(method(currentContext))]
pub(crate) fn currentContext() -> Option<Retained<Self>>;

#[method_id(initWithFormat:shareContext:)]
#[unsafe(method(initWithFormat:shareContext:))]
pub(crate) fn initWithFormat_shareContext(
this: Allocated<Self>,
format: &NSOpenGLPixelFormat,
share: Option<&NSOpenGLContext>,
) -> Option<Id<Self>>;
) -> Option<Retained<Self>>;

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

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

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

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

#[method_id(view)]
pub(crate) fn view(&self, mtm: MainThreadMarker) -> Option<Id<NSView>>;
#[unsafe(method(view))]
pub(crate) fn view(&self, mtm: MainThreadMarker) -> Option<Retained<NSView>>;

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

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

#[method(CGLContextObj)]
#[unsafe(method(CGLContextObj))]
pub(crate) fn CGLContextObj(&self) -> *mut CGLContextObj;
}
);
);
}

extern_class!(
#[unsafe(super(NSObject))]
#[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;
}
);

unsafe impl Send for NSOpenGLPixelFormat {}
unsafe impl Sync for NSOpenGLPixelFormat {}

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

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

#[method(getValues:forAttribute:forVirtualScreen:)]
#[unsafe(method(getValues:forAttribute:forVirtualScreen:))]
pub(crate) unsafe fn getValues_forAttribute_forVirtualScreen(
&self,
vals: *mut GLint,
param: NSOpenGLPixelFormatAttribute,
screen: GLint,
);
);

pub(crate) unsafe fn newWithAttributes(
attrs: &[NSOpenGLPixelFormatAttribute],
) -> Option<Retained<Self>> {
unsafe { Self::initWithAttributes(Self::alloc(), attrs.as_ptr()) }
}
);
}
6 changes: 3 additions & 3 deletions glutin/src/api/cgl/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
use std::sync::Arc;
use std::{fmt, iter};

use objc2::rc::Id;
use objc2::rc::Retained;
#[allow(deprecated)]
use objc2_app_kit::{
NSOpenGLPFAAccelerated, NSOpenGLPFAAllowOfflineRenderers, NSOpenGLPFAAlphaSize,
Expand Down Expand Up @@ -219,7 +219,7 @@ impl GetGlDisplay for Config {

impl AsRawConfig for Config {
fn raw_config(&self) -> RawConfig {
RawConfig::Cgl(Id::as_ptr(&self.inner.raw).cast())
RawConfig::Cgl(Retained::as_ptr(&self.inner.raw).cast())
}
}

Expand All @@ -228,7 +228,7 @@ impl Sealed for Config {}
pub(crate) struct ConfigInner {
display: Display,
pub(crate) transparency: bool,
pub(crate) raw: Id<NSOpenGLPixelFormat>,
pub(crate) raw: Retained<NSOpenGLPixelFormat>,
}

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

use cgl::CGLSetParameter;
use objc2::rc::{autoreleasepool, Id};
use objc2::ClassType;
use dispatch2::{run_on_main, MainThreadBound};
use objc2::rc::{autoreleasepool, Retained};
use objc2::AllocAnyThread;
use objc2_app_kit::{NSOpenGLCPSwapInterval, NSView};
use objc2_foundation::{run_on_main, MainThreadBound};

use crate::config::GetGlConfig;
use crate::context::{
Expand Down Expand Up @@ -139,7 +139,7 @@ impl GetGlDisplay for NotCurrentContext {

impl AsRawContext for NotCurrentContext {
fn raw_context(&self) -> RawContext {
RawContext::Cgl(Id::as_ptr(&self.inner.raw).cast())
RawContext::Cgl(Retained::as_ptr(&self.inner.raw).cast())
}
}

Expand Down Expand Up @@ -220,7 +220,7 @@ impl GetGlDisplay for PossiblyCurrentContext {

impl AsRawContext for PossiblyCurrentContext {
fn raw_context(&self) -> RawContext {
RawContext::Cgl(Id::as_ptr(&self.inner.raw).cast())
RawContext::Cgl(Retained::as_ptr(&self.inner.raw).cast())
}
}

Expand All @@ -229,7 +229,7 @@ impl Sealed for PossiblyCurrentContext {}
pub(crate) struct ContextInner {
display: Display,
config: Config,
pub(crate) raw: Id<NSOpenGLContext>,
pub(crate) raw: Retained<NSOpenGLContext>,
}

impl ContextInner {
Expand Down Expand Up @@ -294,7 +294,7 @@ impl ContextInner {
})
}

pub(crate) fn is_view_current(&self, view: &MainThreadBound<Id<NSView>>) -> bool {
pub(crate) fn is_view_current(&self, view: &MainThreadBound<Retained<NSView>>) -> bool {
run_on_main(|mtm| {
self.raw.view(mtm).expect("context to have a current view") == *view.get(mtm)
})
Expand Down
11 changes: 6 additions & 5 deletions glutin/src/api/cgl/surface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ use std::fmt;
use std::marker::PhantomData;
use std::num::NonZeroU32;

use objc2::rc::Id;
use dispatch2::{run_on_main, MainThreadBound};
use objc2::rc::Retained;
use objc2::MainThreadMarker;
use objc2_app_kit::{NSAppKitVersionNumber, NSAppKitVersionNumber10_12, NSView};
use objc2_foundation::{run_on_main, MainThreadBound, MainThreadMarker};
use raw_window_handle::RawWindowHandle;

use crate::config::GetGlConfig;
Expand Down Expand Up @@ -60,7 +61,7 @@ impl Display {
// SAFETY: Validity of the view and window is ensured by caller
// This function makes sure the window is non null.
let ns_view = if let Some(ns_view) =
unsafe { Id::retain(native_window.ns_view.as_ptr().cast::<NSView>()) }
unsafe { Retained::retain(native_window.ns_view.as_ptr().cast::<NSView>()) }
{
ns_view
} else {
Expand Down Expand Up @@ -100,7 +101,7 @@ impl Display {
pub struct Surface<T: SurfaceTypeTrait> {
display: Display,
config: Config,
pub(crate) ns_view: MainThreadBound<Id<NSView>>,
pub(crate) ns_view: MainThreadBound<Retained<NSView>>,
_nosync: PhantomData<*const std::ffi::c_void>,
_ty: PhantomData<T>,
}
Expand Down Expand Up @@ -192,7 +193,7 @@ impl<T: SurfaceTypeTrait> AsRawSurface for Surface<T> {
fn raw_surface(&self) -> RawSurface {
// SAFETY: We only use the thread marker to get the pointer value of the view
let mtm = unsafe { MainThreadMarker::new_unchecked() };
RawSurface::Cgl(Id::as_ptr(self.ns_view.get(mtm)).cast())
RawSurface::Cgl(Retained::as_ptr(self.ns_view.get(mtm)).cast())
}
}

Expand Down

0 comments on commit 3c6cf86

Please sign in to comment.