From 9c58fbbace68fc6b6ca2f5b924af55792abbed91 Mon Sep 17 00:00:00 2001 From: Brian Schwind Date: Mon, 28 Oct 2024 15:01:39 +0900 Subject: [PATCH] cgl: add `make_current_surfaceless` This follows the EGL api to make surfaceless platforms. --- CHANGELOG.md | 1 + glutin/src/api/cgl/context.rs | 27 +++++++++++++++++++++++++++ 2 files changed, 28 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index cfc3032ce9..4c2865336d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,7 @@ # Unreleased - Add `PossiblyCurrentContext::make_not_current_in_place(&self)` for when `Send` capability of `NotCurrentContext` is not required. +- Add `NotCurrentContext::make_current_surfaceless(self)` and `PossiblyCurrentContext::make_current_surfaceless(&self)` in the `Cgl` implementation to allow the use of surfaceless contexts on MacOS. # Version 0.32.1 diff --git a/glutin/src/api/cgl/context.rs b/glutin/src/api/cgl/context.rs index 2b5d04f791..8450286709 100644 --- a/glutin/src/api/cgl/context.rs +++ b/glutin/src/api/cgl/context.rs @@ -74,6 +74,13 @@ pub struct NotCurrentContext { } impl NotCurrentContext { + /// Make a [`Self::PossiblyCurrentContext`] indicating that the context + /// could be current on the thread. + pub fn make_current_surfaceless(self) -> Result { + self.inner.make_current_surfaceless()?; + Ok(PossiblyCurrentContext { inner: self.inner, _nosendsync: PhantomData }) + } + fn new(inner: ContextInner) -> Self { Self { inner, _nosync: PhantomData } } @@ -143,6 +150,13 @@ pub struct PossiblyCurrentContext { _nosendsync: PhantomData<*mut ()>, } +impl PossiblyCurrentContext { + /// Make this context current on the calling thread. + pub fn make_current_surfaceless(&self) -> Result<()> { + self.inner.make_current_surfaceless() + } +} + impl PossiblyCurrentGlContext for PossiblyCurrentContext { type NotCurrentContext = NotCurrentContext; type Surface = Surface; @@ -236,6 +250,19 @@ impl ContextInner { }) } + fn make_current_surfaceless(&self) -> Result<()> { + autoreleasepool(|_| { + self.update(); + self.raw.makeCurrentContext(); + + run_on_main(|_mtm| unsafe { + self.raw.setView(None); + }); + + Ok(()) + }) + } + fn context_api(&self) -> ContextApi { ContextApi::OpenGl(None) }