From f116b1af351cd8a444cd1f519b8707254198d32d Mon Sep 17 00:00:00 2001 From: Mads Marquart Date: Wed, 11 Sep 2024 12:14:15 +0200 Subject: [PATCH 1/3] Set up NSView the way Glutin requires it In particular `wantsBestResolutionOpenGLSurface` and `wantsLayer`. This is currently done by Winit, but it's really the job of Glutin to do this, Winit shouldn't be concerned with OpenGL-specific details. --- glutin/src/api/cgl/surface.rs | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/glutin/src/api/cgl/surface.rs b/glutin/src/api/cgl/surface.rs index 26044ea77c..65b87b4243 100644 --- a/glutin/src/api/cgl/surface.rs +++ b/glutin/src/api/cgl/surface.rs @@ -5,7 +5,7 @@ use std::marker::PhantomData; use std::num::NonZeroU32; use objc2::rc::Id; -use objc2_app_kit::NSView; +use objc2_app_kit::{NSAppKitVersionNumber, NSAppKitVersionNumber10_12, NSView}; use objc2_foundation::{run_on_main, MainThreadBound, MainThreadMarker}; use raw_window_handle::RawWindowHandle; @@ -60,12 +60,29 @@ 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()) } + unsafe { Id::retain(native_window.ns_view.as_ptr().cast::()) } { ns_view } else { return Err(ErrorKind::NotSupported("ns_view of provided native window is nil").into()); }; + + // The default value of `wantsBestResolutionOpenGLSurface` is `false` when + // linked with the macOS 10.14 SDK and `true` if linked with a macOS 10.15 SDK + // or newer. We always set it to `true` because we want High DPI surfaces, and + // we want to avoid this confusing default system value. + #[allow(deprecated)] + ns_view.setWantsBestResolutionOpenGLSurface(true); + + // On Mojave, views apparently automatically become layer-backed shortly after + // being added to a window. Changing the layer-backedness of a view breaks the + // association between the view and its associated OpenGL context. To work + // around this, we explicitly make the view layer-backed up front so that AppKit + // doesn't do it itself and break the association with its context. + if unsafe { NSAppKitVersionNumber }.floor() > NSAppKitVersionNumber10_12 { + ns_view.setWantsLayer(true); + } + let ns_view = MainThreadBound::new(ns_view, mtm); let surface = Surface { From fa3cb12f7189701ccb522c268048ca118705051b Mon Sep 17 00:00:00 2001 From: Mads Marquart Date: Wed, 11 Sep 2024 12:26:15 +0200 Subject: [PATCH 2/3] Fix features --- glutin/Cargo.toml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/glutin/Cargo.toml b/glutin/Cargo.toml index ecc94998cb..4566af5fa7 100644 --- a/glutin/Cargo.toml +++ b/glutin/Cargo.toml @@ -66,10 +66,12 @@ features = [ [target.'cfg(any(target_os = "macos"))'.dependencies.objc2-app-kit] version = "0.2.0" features = [ + "NSApplication", "NSResponder", "NSView", "NSWindow", "NSOpenGL", + "NSOpenGLView", ] [build-dependencies] From 9b7b53467c29dbf7b0c9cd0c0ebfb6d0be15a6be Mon Sep 17 00:00:00 2001 From: Mads Marquart Date: Wed, 11 Sep 2024 13:26:05 +0200 Subject: [PATCH 3/3] Add changelog entry --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 55e675ec97..4f5b5a45e6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,7 @@ - Fixed EGL's `Display::new()` making an `EGLDisplay::Khr` when the EGL version for the display is 1.4 or lower. - Added `Device::drm_device_node_path()` and `Device::drm_render_device_node_path()` getters to EGL via `EGL_EXT_device_drm`. - Added support for `DrmDisplayHandle` in EGL's `Display::with_device()` using `EGL_DRM_MASTER_FD_EXT` from `EGL_EXT_device_drm`. +- Properly set up OpenGL-specific stuff on the `NSView`, instead of relying on Winit to do it. # Version 0.32.0