Skip to content

Commit 415f0b6

Browse files
YaLTeRgoddessfreya
authored andcommitted
Set VSync in EGL (rust-windowing#1203)
* [rust-windowing#1202]: set VSync in EGL * Update CHANGELOG.md * egl: filter configs which don't have desired vsync
1 parent 54dd8ca commit 415f0b6

File tree

2 files changed

+59
-1
lines changed

2 files changed

+59
-1
lines changed

CHANGELOG.md

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

33
- Fixed attribute handling for sRGB in WGL.
4+
- Fixed VSync being always enabled on EGL.
45

56
# Version 0.22.0-alpha1 (2019-06-21)
67

glutin/src/api/egl/mod.rs

+58-1
Original file line numberDiff line numberDiff line change
@@ -439,6 +439,7 @@ impl Context {
439439
version,
440440
pf_reqs,
441441
surface_type,
442+
opengl,
442443
config_selector,
443444
)?
444445
};
@@ -969,6 +970,26 @@ impl<'a> ContextPrototype<'a> {
969970
}
970971
};
971972

973+
if let Some(surface) = surface {
974+
// VSync defaults to enabled; disable it if it was not requested.
975+
if !self.opengl.vsync {
976+
let _guard = MakeCurrentGuard::new(
977+
self.display,
978+
surface,
979+
surface,
980+
context,
981+
)
982+
.map_err(|err| CreationError::OsError(err))?;
983+
984+
let egl = EGL.as_ref().unwrap();
985+
unsafe {
986+
if egl.SwapInterval(self.display, 0) == ffi::egl::FALSE {
987+
panic!("finish_impl: eglSwapInterval failed: 0x{:x}", egl.GetError());
988+
}
989+
}
990+
}
991+
}
992+
972993
Ok(Context {
973994
display: self.display,
974995
context,
@@ -988,6 +1009,7 @@ unsafe fn choose_fbconfig<F>(
9881009
version: Option<(u8, u8)>,
9891010
pf_reqs: &PixelFormatRequirements,
9901011
surface_type: SurfaceType,
1012+
opengl: &GlAttributes<&Context>,
9911013
mut config_selector: F,
9921014
) -> Result<(ffi::egl::types::EGLConfig, PixelFormat), CreationError>
9931015
where
@@ -1157,7 +1179,42 @@ where
11571179
));
11581180
}
11591181

1160-
if num_configs == 0 {
1182+
// We're interested in those configs which allow our desired VSync.
1183+
let desired_swap_interval = if opengl.vsync {
1184+
1
1185+
} else {
1186+
0
1187+
};
1188+
1189+
let config_ids = config_ids.into_iter().filter(|&config| {
1190+
let mut min_swap_interval = 0;
1191+
let res = egl.GetConfigAttrib(
1192+
display,
1193+
config,
1194+
ffi::egl::MIN_SWAP_INTERVAL as ffi::egl::types::EGLint,
1195+
&mut min_swap_interval,
1196+
);
1197+
1198+
if desired_swap_interval < min_swap_interval {
1199+
return false;
1200+
}
1201+
1202+
let mut max_swap_interval = 0;
1203+
let res = egl.GetConfigAttrib(
1204+
display,
1205+
config,
1206+
ffi::egl::MAX_SWAP_INTERVAL as ffi::egl::types::EGLint,
1207+
&mut max_swap_interval,
1208+
);
1209+
1210+
if desired_swap_interval > max_swap_interval {
1211+
return false;
1212+
}
1213+
1214+
true
1215+
}).collect::<Vec<_>>();
1216+
1217+
if config_ids.is_empty() {
11611218
return Err(CreationError::NoAvailablePixelFormat);
11621219
}
11631220

0 commit comments

Comments
 (0)