Skip to content

Commit

Permalink
cgl: update objc2 crates
Browse files Browse the repository at this point in the history
- `icrate` has been deprecated in favour of separate crates per
  framework, in our case `objc2-foundation` and `objc2-app-kit`.
- Moved `MainThreadMarker::run_on_main` to free-standing function
  `run_on_main`.
- Changed how features work, this should result in less code that needs
  to be compiled.

Links: madsmtm/objc2#526
  • Loading branch information
madsmtm authored Apr 18, 2024
1 parent 9228126 commit 08e1000
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 28 deletions.
24 changes: 14 additions & 10 deletions glutin/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -52,20 +52,24 @@ x11-dl = { version = "2.20.0", optional = true }
[target.'cfg(any(target_os = "macos"))'.dependencies]
cgl = "0.3.2"
core-foundation = "0.9.3"
# Enable `relax-void-encoding` until https://github.com/madsmtm/objc2/pull/526 is resolved
objc2 = { version = "0.4.1", features = ["relax-void-encoding"] }
objc2 = "0.5.1"
dispatch = "0.2.0"

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

[target.'cfg(any(target_os = "macos"))'.dependencies.objc2-app-kit]
version = "0.2.0"
features = [
"NSResponder",
"NSView",
"NSWindow",
"NSOpenGL",
]

[build-dependencies]
Expand Down
14 changes: 7 additions & 7 deletions glutin/src/api/cgl/appkit.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
//! Parts of AppKit related to OpenGL that is not yet in `icrate`.
//! Parts of AppKit related to OpenGL that is not yet in `objc2-app-kit`.
#![allow(dead_code)]
#![allow(non_snake_case)]

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

pub type GLint = i32;

Expand All @@ -17,7 +17,7 @@ pub struct CGLContextObj {
}

unsafe impl RefEncode for CGLContextObj {
const ENCODING_REF: Encoding = Encoding::Pointer(&Encoding::Void);
const ENCODING_REF: Encoding = Encoding::Pointer(&Encoding::Struct("_CGLContextObject", &[]));
}

extern_class!(
Expand All @@ -42,7 +42,7 @@ extern_methods!(

#[method_id(initWithFormat:shareContext:)]
pub(crate) fn initWithFormat_shareContext(
this: Option<Allocated<Self>>,
this: Allocated<Self>,
format: &NSOpenGLPixelFormat,
share: Option<&NSOpenGLContext>,
) -> Option<Id<Self>>;
Expand Down Expand Up @@ -97,7 +97,7 @@ extern_methods!(
unsafe impl NSOpenGLPixelFormat {
#[method_id(initWithAttributes:)]
unsafe fn initWithAttributes(
this: Option<Allocated<Self>>,
this: Allocated<Self>,
attrs: *const NSOpenGLPixelFormatAttribute,
) -> Option<Id<Self>>;

Expand Down
4 changes: 2 additions & 2 deletions glutin/src/api/cgl/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@
use std::sync::Arc;
use std::{fmt, iter};

use objc2::rc::Id;
#[allow(deprecated)]
use icrate::AppKit::{
use objc2_app_kit::{
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 Down
10 changes: 5 additions & 5 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 icrate::AppKit::{NSOpenGLCPSwapInterval, NSView};
use icrate::Foundation::{MainThreadBound, MainThreadMarker};
use objc2::rc::{autoreleasepool, Id};
use objc2::ClassType;
use objc2_app_kit::{NSOpenGLCPSwapInterval, NSView};
use objc2_foundation::{run_on_main, MainThreadBound};

use crate::config::GetGlConfig;
use crate::context::{AsRawContext, ContextApi, ContextAttributes, RawContext, Robustness};
Expand Down Expand Up @@ -224,7 +224,7 @@ impl ContextInner {
self.raw.makeCurrentContext();

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

Expand All @@ -248,7 +248,7 @@ impl ContextInner {
}

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

pub(crate) fn flush_buffer(&self) -> Result<()> {
Expand All @@ -259,7 +259,7 @@ impl ContextInner {
}

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

use icrate::AppKit::{NSView, NSWindow};
use icrate::Foundation::{MainThreadBound, MainThreadMarker};
use objc2::rc::Id;
use objc2_app_kit::{NSView, NSWindow};
use objc2_foundation::{run_on_main, MainThreadBound, MainThreadMarker};
use raw_window_handle::RawWindowHandle;

use crate::config::GetGlConfig;
Expand Down Expand Up @@ -112,7 +112,7 @@ impl<T: SurfaceTypeTrait> GlSurface<T> for Surface<T> {
fn width(&self) -> Option<u32> {
let window = &self.ns_window;
let view = &self.ns_view;
MainThreadMarker::run_on_main(|mtm| unsafe {
run_on_main(|mtm| {
let scale_factor = window.get(mtm).backingScaleFactor();
let frame = view.get(mtm).frame();
Some((frame.size.width * scale_factor) as u32)
Expand All @@ -122,7 +122,7 @@ impl<T: SurfaceTypeTrait> GlSurface<T> for Surface<T> {
fn height(&self) -> Option<u32> {
let window = &self.ns_window;
let view = &self.ns_view;
MainThreadMarker::run_on_main(|mtm| unsafe {
run_on_main(|mtm| {
let scale_factor = window.get(mtm).backingScaleFactor();
let frame = view.get(mtm).frame();
Some((frame.size.height * scale_factor) as u32)
Expand Down

0 comments on commit 08e1000

Please sign in to comment.