diff --git a/glutin/src/api/egl/mod.rs b/glutin/src/api/egl/mod.rs index c81d9176c4..a2acbfcb40 100644 --- a/glutin/src/api/egl/mod.rs +++ b/glutin/src/api/egl/mod.rs @@ -1086,19 +1086,21 @@ impl<'a> ContextPrototype<'a> { if let Some(surface) = surface { // VSync defaults to enabled; disable it if it was not requested. - if !self.opengl.vsync { - let _guard = MakeCurrentGuard::new( - self.display, - surface, - surface, - context, - ) - .map_err(|err| CreationError::OsError(err))?; - - let egl = EGL.as_ref().unwrap(); - unsafe { - if egl.SwapInterval(self.display, 0) == ffi::egl::FALSE { - panic!("finish_impl: eglSwapInterval failed: 0x{:x}", egl.GetError()); + if let Some(vsync) = self.opengl.vsync { + if !vsync { + let _guard = MakeCurrentGuard::new( + self.display, + surface, + surface, + context, + ) + .map_err(|err| CreationError::OsError(err))?; + + let egl = EGL.as_ref().unwrap(); + unsafe { + if egl.SwapInterval(self.display, 0) == ffi::egl::FALSE { + panic!("finish_impl: eglSwapInterval failed: 0x{:x}", egl.GetError()); + } } } } @@ -1294,8 +1296,12 @@ where } // We're interested in those configs which allow our desired VSync. - let desired_swap_interval = if opengl.vsync { - 1 + let desired_swap_interval = if let Some(vsync) = opengl.vsync { + if vsync { + 1 + } else { + 0 + } } else { 0 }; diff --git a/glutin/src/api/glx/mod.rs b/glutin/src/api/glx/mod.rs index 1f01927531..51d2933b49 100644 --- a/glutin/src/api/glx/mod.rs +++ b/glutin/src/api/glx/mod.rs @@ -413,51 +413,53 @@ impl<'a> ContextPrototype<'a> { let (extra_functions, context) = self.create_context()?; // vsync - if self.opengl.vsync { - let _guard = MakeCurrentGuard::new(&self.xconn, window, context) - .map_err(|err| CreationError::OsError(err))?; - - if check_ext(&self.extensions, "GLX_EXT_swap_control") - && extra_functions.SwapIntervalEXT.is_loaded() - { - // this should be the most common extension - unsafe { - extra_functions.SwapIntervalEXT( - self.xconn.display as *mut _, - window, - 1, - ); - } + if let Some(vsync) = self.opengl.vsync { + if vsync { + let _guard = MakeCurrentGuard::new(&self.xconn, window, context) + .map_err(|err| CreationError::OsError(err))?; + + if check_ext(&self.extensions, "GLX_EXT_swap_control") + && extra_functions.SwapIntervalEXT.is_loaded() + { + // this should be the most common extension + unsafe { + extra_functions.SwapIntervalEXT( + self.xconn.display as *mut _, + window, + 1, + ); + } - let mut swap = unsafe { std::mem::zeroed() }; - unsafe { - glx.QueryDrawable( - self.xconn.display as *mut _, - window, - ffi::glx_extra::SWAP_INTERVAL_EXT as i32, - &mut swap, - ); - } + let mut swap = unsafe { std::mem::zeroed() }; + unsafe { + glx.QueryDrawable( + self.xconn.display as *mut _, + window, + ffi::glx_extra::SWAP_INTERVAL_EXT as i32, + &mut swap, + ); + } - if swap != 1 { - return Err(CreationError::OsError(format!("Couldn't setup vsync: expected interval `1` but got `{}`", swap))); - } - } else if check_ext(&self.extensions, "GLX_MESA_swap_control") - && extra_functions.SwapIntervalMESA.is_loaded() - { - unsafe { - extra_functions.SwapIntervalMESA(1); - } - } else if check_ext(&self.extensions, "GLX_SGI_swap_control") - && extra_functions.SwapIntervalSGI.is_loaded() - { - unsafe { - extra_functions.SwapIntervalSGI(1); - } - } else { - return Err(CreationError::OsError( - "Couldn't find any available vsync extension".to_string(), - )); + if swap != 1 { + return Err(CreationError::OsError(format!("Couldn't setup vsync: expected interval `1` but got `{}`", swap))); + } + } else if check_ext(&self.extensions, "GLX_MESA_swap_control") + && extra_functions.SwapIntervalMESA.is_loaded() + { + unsafe { + extra_functions.SwapIntervalMESA(1); + } + } else if check_ext(&self.extensions, "GLX_SGI_swap_control") + && extra_functions.SwapIntervalSGI.is_loaded() + { + unsafe { + extra_functions.SwapIntervalSGI(1); + } + } else { + return Err(CreationError::OsError( + "Couldn't find any available vsync extension".to_string(), + )); + } } } diff --git a/glutin/src/lib.rs b/glutin/src/lib.rs index 8562983c67..81bd27a664 100644 --- a/glutin/src/lib.rs +++ b/glutin/src/lib.rs @@ -181,7 +181,7 @@ impl<'a, T: ContextCurrentState> ContextBuilder<'a, T> { /// /// By default, vsync is not enabled. #[inline] - pub fn with_vsync(mut self, vsync: bool) -> Self { + pub fn with_vsync(mut self, vsync: Option) -> Self { self.gl_attr.vsync = vsync; self } @@ -705,7 +705,7 @@ pub struct GlAttributes { /// screen tearing. /// /// The default is `false`. - pub vsync: bool, + pub vsync: Option, } impl GlAttributes { @@ -748,7 +748,7 @@ impl Default for GlAttributes { profile: None, debug: cfg!(debug_assertions), robustness: Robustness::NotRobust, - vsync: false, + vsync: Some(false), } } }