Skip to content

Commit 1b35f2a

Browse files
committed
More refactoring.
Signed-off-by: Hal Gentz <zegentzy@protonmail.com>
1 parent 7471d39 commit 1b35f2a

25 files changed

+1302
-760
lines changed

Cargo.toml

+3
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,6 @@ members = [
88
"glutin_gles2_sys",
99
"glutin_emscripten_sys",
1010
]
11+
12+
[replace]
13+
"winit:0.20.0-alpha1" = { path = "../winit" }

glutin/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ serde = ["winit/serde"]
2020
lazy_static = "1.3"
2121
winit = "0.20.0-alpha1"
2222
log = "0.4"
23+
bitflags = "1.1"
2324

2425
[target.'cfg(target_os = "android")'.dependencies]
2526
android_glue = "0.2"

glutin/src/api/android/mod.rs

+5-3
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ use crate::{
88
Api, ContextError, GlAttributes, PixelFormat, PixelFormatRequirements,
99
};
1010

11+
use winit::window::WindowBuilder;
12+
use winit::event_loop::EventLoopWindowTarget;
1113
use crate::platform::android::EventLoopExtAndroid;
1214
use glutin_egl_sys as ffi;
1315
use parking_lot::Mutex;
@@ -52,8 +54,8 @@ impl android_glue::SyncEventHandler for AndroidSyncEventHandler {
5254
impl Context {
5355
#[inline]
5456
pub fn new_windowed<T>(
55-
wb: winit::window::WindowBuilder,
56-
el: &winit::event_loop::EventLoop<T>,
57+
wb: WindowBuilder,
58+
el: &EventLoopWindowTarget<T>,
5759
pf_reqs: &PixelFormatRequirements,
5860
gl_attr: &GlAttributes<&Self>,
5961
) -> Result<(winit::window::Window, Self), CreationError> {
@@ -105,7 +107,7 @@ impl Context {
105107

106108
#[inline]
107109
pub fn new_headless<T>(
108-
_el: &winit::event_loop::EventLoop<T>,
110+
_el: &EventLoopWindowTarget<T>,
109111
pf_reqs: &PixelFormatRequirements,
110112
gl_attr: &GlAttributes<&Context>,
111113
size: dpi::PhysicalSize,

glutin/src/api/egl/mod.rs

+58-109
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,9 @@ pub use self::egl::Egl;
6262
use self::make_current_guard::MakeCurrentGuard;
6363
use crate::platform_impl::PlatformAttributes;
6464
use crate::{
65-
Api, ContextBuilderWrapper, ContextError, CreationError, GlAttributes,
66-
GlRequest, PixelFormat, PixelFormatRequirements, ReleaseBehavior,
67-
Robustness,
65+
Api, ContextBuilderWrapper, ContextError, ContextSupports, CreationError,
66+
GlAttributes, GlRequest, PixelFormat, PixelFormatRequirements,
67+
ReleaseBehavior, Robustness,
6868
};
6969

7070
use glutin_egl_sys as ffi;
@@ -79,7 +79,7 @@ use parking_lot::Mutex;
7979
target_os = "openbsd",
8080
))]
8181
use winit::dpi;
82-
use winit::event_loop::EventLoop;
82+
use winit::event_loop::EventLoopWindowTarget;
8383

8484
use std::ffi::{CStr, CString};
8585
use std::marker::PhantomData;
@@ -387,25 +387,17 @@ fn get_native_display(native_display: &NativeDisplay) -> *const raw::c_void {
387387
}
388388
}
389389

390-
#[allow(dead_code)] // Not all platforms use all
391-
#[derive(Copy, Clone, PartialEq, Debug)]
392-
pub enum SurfaceType {
393-
PBuffer,
394-
Window,
395-
Surfaceless,
396-
}
397-
398390
impl Context {
399391
/// Start building an EGL context.
400392
///
401393
/// This function initializes some things and chooses the pixel format.
402394
///
403395
/// To finish the process, you must call `.finish(window)` on the
404396
/// `ContextPrototype`.
405-
pub fn new<'a, F>(
397+
pub(crate) fn new<'a, F>(
406398
cb: &'a ContextBuilderWrapper<&'a Context>,
407399
native_display: NativeDisplay,
408-
surface_type: SurfaceType,
400+
ctx_supports: ContextSupports,
409401
config_selector: F,
410402
) -> Result<Context, CreationError>
411403
where
@@ -441,6 +433,19 @@ impl Context {
441433
vec![]
442434
};
443435

436+
// FIXME: Also check for the GL_OES_surfaceless_context *CONTEXT*
437+
// extension
438+
if ctx_supports.contains(ContextSupports::SURFACELESS)
439+
&& extensions
440+
.iter()
441+
.find(|s| s == &"EGL_KHR_surfaceless_context")
442+
.is_none()
443+
{
444+
return Err(CreationError::NotSupported(
445+
"EGL surfaceless not supported".to_string(),
446+
));
447+
}
448+
444449
// binding the right API and choosing the version
445450
let (version, api) =
446451
unsafe { bind_and_get_api(&cb.gl_attr, egl_version)? };
@@ -452,7 +457,7 @@ impl Context {
452457
api,
453458
version,
454459
cb,
455-
surface_type,
460+
ctx_supports,
456461
config_selector,
457462
)?
458463
};
@@ -712,9 +717,7 @@ impl Context {
712717
}
713718
}
714719

715-
unsafe fn check_make_current(
716-
ret: Option<u32>,
717-
) -> Result<(), ContextError> {
720+
unsafe fn check_make_current(ret: Option<u32>) -> Result<(), ContextError> {
718721
let egl = EGL.as_ref().unwrap();
719722
if ret == Some(0) {
720723
match egl.GetError() as u32 {
@@ -841,42 +844,6 @@ pub fn get_native_visual_id(
841844
value
842845
}
843846

844-
// impl<'a> ContextPrototype<'a> {
845-
// #[cfg(any(
846-
// target_os = "linux",
847-
// target_os = "dragonfly",
848-
// target_os = "freebsd",
849-
// target_os = "netbsd",
850-
// target_os = "openbsd",
851-
// ))]
852-
// pub fn finish_surfaceless(self) -> Result<Context, CreationError> {
853-
// FIXME: Also check for the GL_OES_surfaceless_context *CONTEXT*
854-
// extension
855-
// if self
856-
// .extensions
857-
// .iter()
858-
// .find(|s| s == &"EGL_KHR_surfaceless_context")
859-
// .is_none()
860-
// {
861-
// Err(CreationError::NotSupported(
862-
// "EGL surfaceless not supported".to_string(),
863-
// ))
864-
// } else {
865-
// self.finish_impl(None)
866-
// }
867-
// }
868-
//
869-
// #[cfg(any(
870-
// target_os = "android",
871-
// target_os = "windows",
872-
// target_os = "linux",
873-
// target_os = "dragonfly",
874-
// target_os = "freebsd",
875-
// target_os = "netbsd",
876-
// target_os = "openbsd",
877-
// ))]
878-
// }
879-
880847
pub trait SurfaceTypeTrait {}
881848

882849
#[derive(Debug)]
@@ -901,7 +868,7 @@ pub struct EGLSurface<T: SurfaceTypeTrait> {
901868
impl WindowSurface {
902869
#[inline]
903870
pub fn new_window_surface<T>(
904-
el: &EventLoop<T>,
871+
el: &EventLoopWindowTarget<T>,
905872
ctx: &Context,
906873
nwin: ffi::EGLNativeWindowType,
907874
) -> Result<Self, CreationError> {
@@ -953,33 +920,12 @@ impl WindowSurface {
953920
Ok(())
954921
}
955922
}
956-
957-
#[inline]
958-
pub unsafe fn make_not_current(&self) -> Result<(), ContextError> {
959-
let egl = EGL.as_ref().unwrap();
960-
961-
if
962-
egl.GetCurrentSurface(ffi::egl::DRAW as i32) == self.surface
963-
|| egl.GetCurrentSurface(ffi::egl::READ as i32) == self.surface {
964-
let ret = egl.MakeCurrent(
965-
**self.display,
966-
ffi::egl::NO_SURFACE,
967-
ffi::egl::NO_SURFACE,
968-
ffi::egl::NO_CONTEXT,
969-
);
970-
971-
check_make_current(Some(ret))
972-
} else {
973-
check_make_current(None)
974-
}
975-
}
976-
977923
}
978924

979925
impl PBuffer {
980926
#[inline]
981927
pub fn new_pbuffer<T>(
982-
_el: &EventLoop<T>,
928+
_el: &EventLoopWindowTarget<T>,
983929
ctx: &Context,
984930
size: dpi::PhysicalSize,
985931
) -> Result<Self, CreationError> {
@@ -1022,14 +968,30 @@ impl PBuffer {
1022968
phantom: PhantomData,
1023969
})
1024970
}
971+
}
972+
973+
impl<T: SurfaceTypeTrait> EGLSurface<T> {
974+
#[inline]
975+
pub fn is_current(&self) -> bool {
976+
let egl = EGL.as_ref().unwrap();
977+
unsafe {
978+
egl.GetCurrentSurface(ffi::egl::DRAW as i32) == self.surface
979+
|| egl.GetCurrentSurface(ffi::egl::READ as i32) == self.surface
980+
}
981+
}
982+
983+
#[inline]
984+
pub fn get_pixel_format(&self) -> PixelFormat {
985+
self.pixel_format.clone()
986+
}
1025987

1026988
#[inline]
1027989
pub unsafe fn make_not_current(&self) -> Result<(), ContextError> {
1028990
let egl = EGL.as_ref().unwrap();
1029991

1030-
if
1031-
egl.GetCurrentSurface(ffi::egl::DRAW as i32) == self.surface
1032-
|| egl.GetCurrentSurface(ffi::egl::READ as i32) == self.surface {
992+
if egl.GetCurrentSurface(ffi::egl::DRAW as i32) == self.surface
993+
|| egl.GetCurrentSurface(ffi::egl::READ as i32) == self.surface
994+
{
1033995
let ret = egl.MakeCurrent(
1034996
**self.display,
1035997
ffi::egl::NO_SURFACE,
@@ -1042,23 +1004,6 @@ impl PBuffer {
10421004
check_make_current(None)
10431005
}
10441006
}
1045-
1046-
}
1047-
1048-
impl<T: SurfaceTypeTrait> EGLSurface<T> {
1049-
#[inline]
1050-
pub fn is_current(&self) -> bool {
1051-
let egl = EGL.as_ref().unwrap();
1052-
unsafe {
1053-
egl.GetCurrentSurface(ffi::egl::DRAW as i32) == self.surface
1054-
|| egl.GetCurrentSurface(ffi::egl::READ as i32) == self.surface
1055-
}
1056-
}
1057-
1058-
#[inline]
1059-
pub fn get_pixel_format(&self) -> PixelFormat {
1060-
self.pixel_format.clone()
1061-
}
10621007
}
10631008

10641009
impl<T: SurfaceTypeTrait> Drop for EGLSurface<T> {
@@ -1086,7 +1031,7 @@ unsafe fn choose_fbconfig<F>(
10861031
api: Api,
10871032
version: Option<(u8, u8)>,
10881033
cb: &ContextBuilderWrapper<&Context>,
1089-
surface_type: SurfaceType,
1034+
ctx_supports: ContextSupports,
10901035
mut config_selector: F,
10911036
) -> Result<(ffi::egl::types::EGLConfig, PixelFormat), CreationError>
10921037
where
@@ -1106,11 +1051,15 @@ where
11061051
}
11071052

11081053
out.push(ffi::egl::SURFACE_TYPE as raw::c_int);
1109-
let surface_type = match surface_type {
1110-
SurfaceType::Window => ffi::egl::WINDOW_BIT,
1111-
SurfaceType::PBuffer => ffi::egl::PBUFFER_BIT,
1112-
SurfaceType::Surfaceless => 0,
1113-
};
1054+
let mut surface_type =
1055+
if ctx_supports.contains(ContextSupports::WINDOW_SURFACES) {
1056+
ffi::egl::WINDOW_BIT
1057+
} else {
1058+
0
1059+
};
1060+
if ctx_supports.contains(ContextSupports::PBUFFERS) {
1061+
surface_type = surface_type | ffi::egl::PBUFFER_BIT
1062+
}
11141063
out.push(surface_type as raw::c_int);
11151064

11161065
match (api, version) {
@@ -1424,12 +1373,12 @@ unsafe fn create_context(
14241373
context_attributes.push(ffi::egl::TRUE as i32);
14251374
}
14261375

1427-
// TODO: using this flag sometimes generates an error
1428-
// there was a change in the specs that added this flag, so it
1429-
// may not be supported everywhere ; however it is
1430-
// not possible to know whether it is supported or
1431-
// not flags = flags |
1432-
// ffi::egl::CONTEXT_OPENGL_DEBUG_BIT_KHR as i32;
1376+
// TODO: using this flag sometimes generates an error there was a
1377+
// change in the specs that added this flag, so it may not be
1378+
// supported everywhere; however it is not possible to know whether
1379+
// it is supported or not
1380+
//
1381+
// flags = flags | ffi::egl::CONTEXT_OPENGL_DEBUG_BIT_KHR as i32;
14331382
}
14341383

14351384
// In at least some configurations, the Android emulator’s GL

glutin/src/api/ios/mod.rs

+6-4
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,8 @@ use glutin_gles2_sys as ffi;
7070
use objc::declare::ClassDecl;
7171
use objc::runtime::{Class, Object, Sel, BOOL, NO, YES};
7272
use winit::dpi;
73+
use winit::window::WindowBuilder;
74+
use winit::event_loop::EventLoopWindowTarget;
7375

7476
use std::ffi::CString;
7577
use std::os::raw;
@@ -172,8 +174,8 @@ fn validate_version(version: u8) -> Result<ffi::NSUInteger, CreationError> {
172174
impl Context {
173175
#[inline]
174176
pub fn new_windowed<T>(
175-
builder: winit::window::WindowBuilder,
176-
el: &winit::event_loop::EventLoop<T>,
177+
builder: WindowBuilder,
178+
el: &EventLoopWindowTarget<T>,
177179
_: &PixelFormatRequirements,
178180
gl_attrs: &GlAttributes<&Context>,
179181
) -> Result<(winit::window::Window, Self), CreationError> {
@@ -215,12 +217,12 @@ impl Context {
215217

216218
#[inline]
217219
pub fn new_headless<T>(
218-
el: &winit::event_loop::EventLoop<T>,
220+
el: &EventLoopWindowTarget<T>,
219221
pf_reqs: &PixelFormatRequirements,
220222
gl_attr: &GlAttributes<&Context>,
221223
size: dpi::PhysicalSize,
222224
) -> Result<Self, CreationError> {
223-
let wb = winit::window::WindowBuilder::new()
225+
let wb = WindowBuilder::new()
224226
.with_visibility(false)
225227
.with_inner_size(size.to_logical(1.));
226228
Self::new_windowed(wb, el, pf_reqs, gl_attr)

glutin/src/api/osmesa/mod.rs

-5
Original file line numberDiff line numberDiff line change
@@ -261,11 +261,6 @@ impl OsMesaBuffer {
261261
})
262262
}
263263

264-
#[inline]
265-
pub fn is_current(&self) -> bool {
266-
panic!("This cannot be implemented with OsMesa.")
267-
}
268-
269264
#[inline]
270265
pub fn get_pixel_format(&self) -> PixelFormat {
271266
unimplemented!()

0 commit comments

Comments
 (0)