Skip to content

Commit 5a7783c

Browse files
api/context: add make_not_current_as_possibly_current
Allow making the context not current by reference.
1 parent 23e4ca1 commit 5a7783c

File tree

7 files changed

+38
-29
lines changed

7 files changed

+38
-29
lines changed

CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# Unreleased
22

3+
- Add `PossiblyCurrentContext::make_not_current_in_place(&self)` for when `Send` capability of `NotCurrentContext` is not required.
4+
35
# Version 0.32.1
46

57
- Fixed EGL's `Device::query_devices()` being too strict about required extensions.

glutin/src/api/cgl/context.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -148,10 +148,14 @@ impl PossiblyCurrentGlContext for PossiblyCurrentContext {
148148
type Surface<T: SurfaceTypeTrait> = Surface<T>;
149149

150150
fn make_not_current(self) -> Result<Self::NotCurrentContext> {
151-
self.inner.make_not_current()?;
151+
self.make_not_current_in_place()?;
152152
Ok(NotCurrentContext::new(self.inner))
153153
}
154154

155+
fn make_not_current_in_place(&self) -> Result<()> {
156+
self.inner.make_not_current()
157+
}
158+
155159
fn is_current(&self) -> bool {
156160
if let Some(current) = NSOpenGLContext::currentContext() {
157161
current == self.inner.raw

glutin/src/api/egl/context.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -259,10 +259,14 @@ impl PossiblyCurrentGlContext for PossiblyCurrentContext {
259259
type Surface<T: SurfaceTypeTrait> = Surface<T>;
260260

261261
fn make_not_current(self) -> Result<Self::NotCurrentContext> {
262-
self.inner.make_not_current()?;
262+
self.make_not_current_in_place()?;
263263
Ok(NotCurrentContext::new(self.inner))
264264
}
265265

266+
fn make_not_current_in_place(&self) -> Result<()> {
267+
self.inner.make_not_current()
268+
}
269+
266270
fn is_current(&self) -> bool {
267271
unsafe {
268272
self.inner.bind_api();

glutin/src/api/glx/context.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -302,10 +302,14 @@ impl PossiblyCurrentGlContext for PossiblyCurrentContext {
302302
type Surface<T: SurfaceTypeTrait> = Surface<T>;
303303

304304
fn make_not_current(self) -> Result<Self::NotCurrentContext> {
305-
self.inner.make_not_current()?;
305+
self.make_not_current_in_place()?;
306306
Ok(NotCurrentContext::new(self.inner))
307307
}
308308

309+
fn make_not_current_in_place(&self) -> Result<()> {
310+
self.inner.make_not_current()
311+
}
312+
309313
fn is_current(&self) -> bool {
310314
unsafe { self.inner.display.inner.glx.GetCurrentContext() == *self.inner.raw }
311315
}

glutin/src/api/wgl/context.rs

+6-2
Original file line numberDiff line numberDiff line change
@@ -288,16 +288,20 @@ impl PossiblyCurrentGlContext for PossiblyCurrentContext {
288288
type Surface<T: SurfaceTypeTrait> = Surface<T>;
289289

290290
fn make_not_current(self) -> Result<Self::NotCurrentContext> {
291+
self.make_not_current_in_place()?;
292+
Ok(NotCurrentContext::new(self.inner))
293+
}
294+
295+
fn make_not_current_in_place(&self) -> Result<()> {
291296
unsafe {
292297
if self.is_current() {
293298
let hdc = wgl::GetCurrentDC();
294299
if wgl::MakeCurrent(hdc, std::ptr::null()) == 0 {
295300
return Err(IoError::last_os_error().into());
296301
}
297302
}
298-
299-
Ok(NotCurrentContext::new(self.inner))
300303
}
304+
Ok(())
301305
}
302306

303307
fn is_current(&self) -> bool {

glutin/src/context.rs

+9
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,11 @@ pub trait PossiblyCurrentGlContext: Sealed {
9797
/// - **macOS: this will block if your main thread is blocked.**
9898
fn make_not_current(self) -> Result<Self::NotCurrentContext>;
9999

100+
/// Make the context not current to the current thread. If you need to
101+
/// send the context to another thread, use [`Self::make_not_current`]
102+
/// instead.
103+
fn make_not_current_in_place(&self) -> Result<()>;
104+
100105
/// Make [`Self::Surface`] current on the calling thread.
101106
///
102107
/// # Platform specific
@@ -517,6 +522,10 @@ impl PossiblyCurrentGlContext for PossiblyCurrentContext {
517522
)
518523
}
519524

525+
fn make_not_current_in_place(&self) -> Result<()> {
526+
Ok(gl_api_dispatch!(self; Self(context) => context.make_not_current_in_place()?))
527+
}
528+
520529
fn make_current<T: SurfaceTypeTrait>(&self, surface: &Self::Surface<T>) -> Result<()> {
521530
match (self, surface) {
522531
#[cfg(egl_backend)]

glutin_examples/examples/switch_render_thread.rs

+6-24
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ use std::thread;
77
use glutin::config::ConfigTemplateBuilder;
88
use glutin::context::{ContextAttributesBuilder, PossiblyCurrentContext};
99
use glutin::display::GetGlDisplay;
10-
use glutin::error::{Error as GlutinError, ErrorKind};
1110
use glutin::prelude::*;
1211
use glutin::surface::{Surface, WindowSurface};
1312
use glutin_examples::gl::types::GLfloat;
@@ -148,7 +147,7 @@ impl AppState {
148147

149148
/// A rendering context that can be shared between tasks.
150149
struct RenderContext {
151-
context: Option<PossiblyCurrentContext>,
150+
context: PossiblyCurrentContext,
152151
surface: Surface<WindowSurface>,
153152
renderer: Renderer,
154153
}
@@ -161,44 +160,27 @@ impl RenderContext {
161160
surface: Surface<WindowSurface>,
162161
renderer: Renderer,
163162
) -> Self {
164-
Self { context: Some(context), surface, renderer }
163+
Self { context, surface, renderer }
165164
}
166165

167166
fn make_current(&mut self) -> Result<(), impl Error> {
168-
let ctx =
169-
self.context.take().ok_or_else(|| GlutinError::from(ErrorKind::BadContextState))?;
170-
let result = ctx.make_current(&self.surface);
171-
self.context = Some(ctx);
172-
result
167+
self.context.make_current(&self.surface)
173168
}
174169

175170
fn make_not_current(&mut self) -> Result<(), impl Error> {
176-
let ctx =
177-
self.context.take().ok_or_else(|| GlutinError::from(ErrorKind::BadContextState))?;
178-
let not_current_ctx = ctx.make_not_current()?;
179-
self.context = Some(not_current_ctx.treat_as_possibly_current());
180-
Ok::<(), GlutinError>(())
171+
self.context.make_not_current_in_place()
181172
}
182173

183174
fn swap_buffers(&mut self) -> Result<(), impl Error> {
184-
let ctx =
185-
self.context.take().ok_or_else(|| GlutinError::from(ErrorKind::BadContextState))?;
186-
let result = self.surface.swap_buffers(&ctx);
187-
self.context = Some(ctx);
188-
result
175+
self.surface.swap_buffers(&self.context)
189176
}
190177

191178
fn draw_with_clear_color(&self, red: GLfloat, green: GLfloat, blue: GLfloat, alpha: GLfloat) {
192179
self.renderer.draw_with_clear_color(red, green, blue, alpha)
193180
}
194181

195182
fn resize(&mut self, size: PhysicalSize<NonZeroU32>) {
196-
let Some(ctx) = self.context.take() else {
197-
return;
198-
};
199-
self.surface.resize(&ctx, size.width, size.height);
200-
self.context = Some(ctx);
201-
183+
self.surface.resize(&self.context, size.width, size.height);
202184
self.renderer.resize(size.width.get() as i32, size.height.get() as i32);
203185
}
204186
}

0 commit comments

Comments
 (0)