Skip to content

Commit c898846

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 Signed-off-by: Hal Gentz <zegentzy@protonmail.com>
1 parent 198d919 commit c898846

File tree

4 files changed

+56
-3
lines changed

4 files changed

+56
-3
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.20.1 (2019-08-08)
67

Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,4 @@ members = [
1010
]
1111

1212
[replace]
13-
"winit:0.20.0-alpha1" = { path = "../winit" }
13+
"winit:0.20.0-alpha2" = { path = "../winit" }

glutin/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ serde = ["winit/serde"]
1818

1919
[dependencies]
2020
lazy_static = "1.3"
21-
winit = "0.20.0-alpha1"
21+
winit = "0.20.0-alpha2"
2222
bitflags = "1.1"
2323

2424
[target.'cfg(target_os = "android")'.dependencies]

glutin/src/api/egl.rs

+53-1
Original file line numberDiff line numberDiff line change
@@ -895,6 +895,23 @@ impl WindowSurface {
895895
surface
896896
};
897897

898+
// VSync defaults to enabled; disable it if it was not requested.
899+
if !ctx.pixel_format.vsync {
900+
let _guard = MakeCurrentGuard::new(
901+
**ctx.display,
902+
surface,
903+
surface,
904+
ctx.context,
905+
)
906+
.map_err(|err| CreationError::OsError(err))?;
907+
908+
unsafe {
909+
if egl.SwapInterval(**ctx.display, 0) == ffi::egl::FALSE {
910+
panic!("finish_impl: eglSwapInterval failed: 0x{:x}", egl.GetError());
911+
}
912+
}
913+
}
914+
898915
Ok(WindowSurface {
899916
display: Arc::clone(&ctx.display),
900917
pixel_format: ctx.pixel_format.clone(),
@@ -1211,7 +1228,42 @@ where
12111228
));
12121229
}
12131230

1214-
if num_configs == 0 {
1231+
// We're interested in those configs which allow our desired VSync.
1232+
let desired_swap_interval = if cb.pf_reqs.vsync {
1233+
1
1234+
} else {
1235+
0
1236+
};
1237+
1238+
let config_ids = config_ids.into_iter().filter(|&config| {
1239+
let mut min_swap_interval = 0;
1240+
let res = egl.GetConfigAttrib(
1241+
display,
1242+
config,
1243+
ffi::egl::MIN_SWAP_INTERVAL as ffi::egl::types::EGLint,
1244+
&mut min_swap_interval,
1245+
);
1246+
1247+
if desired_swap_interval < min_swap_interval {
1248+
return false;
1249+
}
1250+
1251+
let mut max_swap_interval = 0;
1252+
let res = egl.GetConfigAttrib(
1253+
display,
1254+
config,
1255+
ffi::egl::MAX_SWAP_INTERVAL as ffi::egl::types::EGLint,
1256+
&mut max_swap_interval,
1257+
);
1258+
1259+
if desired_swap_interval > max_swap_interval {
1260+
return false;
1261+
}
1262+
1263+
true
1264+
}).collect::<Vec<_>>();
1265+
1266+
if config_ids.is_empty() {
12151267
return Err(CreationError::NoAvailablePixelFormat);
12161268
}
12171269

0 commit comments

Comments
 (0)