Skip to content

Commit c5f3eb8

Browse files
committed
High level api rework.
Signed-off-by: Hal Gentz <zegentzy@protonmail.com>
1 parent f08dad6 commit c5f3eb8

File tree

21 files changed

+814
-777
lines changed

21 files changed

+814
-777
lines changed

glutin/src/api/egl/mod.rs

+31-20
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ use crate::{
6464
Api, ContextError, CreationError, GlAttributes, GlRequest, PixelFormat,
6565
PixelFormatRequirements, ReleaseBehavior, Robustness,
6666
};
67+
use crate::platform_impl::PlatformAttributes;
6768

6869
use glutin_egl_sys as ffi;
6970
use parking_lot::Mutex;
@@ -156,11 +157,11 @@ fn get_egl_version(
156157
}
157158

158159
unsafe fn bind_and_get_api<'a>(
159-
opengl: &'a GlAttributes<&'a Context>,
160+
gl_attr: &'a GlAttributes<&'a Context>,
160161
egl_version: (ffi::egl::types::EGLint, ffi::egl::types::EGLint),
161162
) -> Result<(Option<(u8, u8)>, Api), CreationError> {
162163
let egl = EGL.as_ref().unwrap();
163-
match opengl.version {
164+
match gl_attr.version {
164165
GlRequest::Latest => {
165166
if egl_version >= (1, 4) {
166167
if egl.BindAPI(ffi::egl::OPENGL_API) != 0 {
@@ -390,7 +391,8 @@ impl Context {
390391
/// `ContextPrototype`.
391392
pub fn new<'a, F>(
392393
pf_reqs: &PixelFormatRequirements,
393-
opengl: &'a GlAttributes<&'a Context>,
394+
gl_attr: &'a GlAttributes<&'a Context>,
395+
plat_attr: &PlatformAttributes,
394396
native_display: NativeDisplay,
395397
surface_type: SurfaceType,
396398
config_selector: F,
@@ -429,7 +431,7 @@ impl Context {
429431
};
430432

431433
// binding the right API and choosing the version
432-
let (version, api) = unsafe { bind_and_get_api(&opengl, egl_version)? };
434+
let (version, api) = unsafe { bind_and_get_api(&gl_attr, egl_version)? };
433435

434436
let (config_id, pixel_format) = unsafe {
435437
choose_fbconfig(
@@ -438,13 +440,14 @@ impl Context {
438440
api,
439441
version,
440442
pf_reqs,
443+
plat_attr,
441444
surface_type,
442445
config_selector,
443446
)?
444447
};
445448

446449
Ok(ContextPrototype {
447-
opengl,
450+
gl_attr,
448451
display,
449452
egl_version,
450453
extensions,
@@ -475,6 +478,13 @@ impl Context {
475478

476479
pub unsafe fn make_current(&self) -> Result<(), ContextError> {
477480
let egl = EGL.as_ref().unwrap();
481+
// FIXME: Possible race condition: we lock the mutex, grab the surface,
482+
// unlock it. Then the other thread on android destroys the surface,
483+
// then we try to use the surface.
484+
//
485+
// Problem is rare enough, and can only possibly happen on android.
486+
//
487+
// Is it okay to use a drestroyed surface? I can't imagine so.
478488
let surface = self
479489
.surface
480490
.as_ref()
@@ -731,7 +741,7 @@ impl Drop for Context {
731741

732742
#[derive(Debug)]
733743
pub struct ContextPrototype<'a> {
734-
opengl: &'a GlAttributes<&'a Context>,
744+
gl_attr: &'a GlAttributes<&'a Context>,
735745
display: ffi::egl::types::EGLDisplay,
736746
egl_version: (ffi::egl::types::EGLint, ffi::egl::types::EGLint),
737747
extensions: Vec<String>,
@@ -881,7 +891,7 @@ impl<'a> ContextPrototype<'a> {
881891
self,
882892
surface: Option<ffi::egl::types::EGLSurface>,
883893
) -> Result<Context, CreationError> {
884-
let share = match self.opengl.sharing {
894+
let share = match self.gl_attr.sharing {
885895
Some(ctx) => ctx.context,
886896
None => std::ptr::null(),
887897
};
@@ -895,8 +905,8 @@ impl<'a> ContextPrototype<'a> {
895905
self.api,
896906
version,
897907
self.config_id,
898-
self.opengl.debug,
899-
self.opengl.robustness,
908+
self.gl_attr.debug,
909+
self.gl_attr.robustness,
900910
share,
901911
)?
902912
} else if self.api == Api::OpenGlEs {
@@ -907,8 +917,8 @@ impl<'a> ContextPrototype<'a> {
907917
self.api,
908918
(2, 0),
909919
self.config_id,
910-
self.opengl.debug,
911-
self.opengl.robustness,
920+
self.gl_attr.debug,
921+
self.gl_attr.robustness,
912922
share,
913923
) {
914924
ctx
@@ -919,8 +929,8 @@ impl<'a> ContextPrototype<'a> {
919929
self.api,
920930
(1, 0),
921931
self.config_id,
922-
self.opengl.debug,
923-
self.opengl.robustness,
932+
self.gl_attr.debug,
933+
self.gl_attr.robustness,
924934
share,
925935
) {
926936
ctx
@@ -935,8 +945,8 @@ impl<'a> ContextPrototype<'a> {
935945
self.api,
936946
(3, 2),
937947
self.config_id,
938-
self.opengl.debug,
939-
self.opengl.robustness,
948+
self.gl_attr.debug,
949+
self.gl_attr.robustness,
940950
share,
941951
) {
942952
ctx
@@ -947,8 +957,8 @@ impl<'a> ContextPrototype<'a> {
947957
self.api,
948958
(3, 1),
949959
self.config_id,
950-
self.opengl.debug,
951-
self.opengl.robustness,
960+
self.gl_attr.debug,
961+
self.gl_attr.robustness,
952962
share,
953963
) {
954964
ctx
@@ -959,8 +969,8 @@ impl<'a> ContextPrototype<'a> {
959969
self.api,
960970
(1, 0),
961971
self.config_id,
962-
self.opengl.debug,
963-
self.opengl.robustness,
972+
self.gl_attr.debug,
973+
self.gl_attr.robustness,
964974
share,
965975
) {
966976
ctx
@@ -988,6 +998,7 @@ unsafe fn choose_fbconfig<F>(
988998
api: Api,
989999
version: Option<(u8, u8)>,
9901000
pf_reqs: &PixelFormatRequirements,
1001+
plat_attr: &PlatformAttributes,
9911002
surface_type: SurfaceType,
9921003
mut config_selector: F,
9931004
) -> Result<(ffi::egl::types::EGLConfig, PixelFormat), CreationError>
@@ -1105,7 +1116,7 @@ where
11051116
return Err(CreationError::NoAvailablePixelFormat);
11061117
}
11071118

1108-
if let Some(xid) = pf_reqs.x11_visual_xid {
1119+
if let Some(xid) = plat_attr.x11_visual_xid {
11091120
out.push(ffi::egl::NATIVE_VISUAL_ID as raw::c_int);
11101121
out.push(xid as raw::c_int);
11111122
}

glutin/src/api/glx/mod.rs

+24-20
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ use crate::{
5959

6060
use crate::platform::unix::x11::XConnection;
6161
use crate::platform_impl::x11_utils::SurfaceType;
62+
use crate::platform_impl::PlatformAttributes;
6263
use glutin_glx_sys as ffi;
6364
use winit::dpi;
6465

@@ -83,7 +84,8 @@ impl Context {
8384
pub fn new<'a>(
8485
xconn: Arc<XConnection>,
8586
pf_reqs: &PixelFormatRequirements,
86-
opengl: &'a GlAttributes<&'a Context>,
87+
gl_attr: &'a GlAttributes<&'a Context>,
88+
plat_attr: &PlatformAttributes,
8789
screen_id: raw::c_int,
8890
surface_type: SurfaceType,
8991
transparent: Option<bool>,
@@ -112,6 +114,7 @@ impl Context {
112114
&xconn,
113115
screen_id,
114116
pf_reqs,
117+
plat_attr,
115118
surface_type,
116119
transparent,
117120
)?
@@ -120,7 +123,7 @@ impl Context {
120123
Ok(ContextPrototype {
121124
extensions,
122125
xconn,
123-
opengl,
126+
gl_attr,
124127
fb_config,
125128
visual_infos: unsafe { std::mem::transmute(visual_infos) },
126129
pixel_format,
@@ -249,7 +252,7 @@ impl Drop for Context {
249252
pub struct ContextPrototype<'a> {
250253
extensions: String,
251254
xconn: Arc<XConnection>,
252-
opengl: &'a GlAttributes<&'a Context>,
255+
gl_attr: &'a GlAttributes<&'a Context>,
253256
fb_config: ffi::glx::types::GLXFBConfig,
254257
visual_infos: ffi::XVisualInfo,
255258
pixel_format: PixelFormat,
@@ -266,7 +269,7 @@ impl<'a> ContextPrototype<'a> {
266269
&self,
267270
) -> Result<(ffi::glx_extra::Glx, ffi::GLXContext), CreationError> {
268271
let glx = GLX.as_ref().unwrap();
269-
let share = match self.opengl.sharing {
272+
let share = match self.gl_attr.sharing {
270273
Some(ctx) => ctx.context,
271274
None => std::ptr::null(),
272275
};
@@ -279,7 +282,7 @@ impl<'a> ContextPrototype<'a> {
279282
}
280283
});
281284

282-
let context = match self.opengl.version {
285+
let context = match self.gl_attr.version {
283286
GlRequest::Latest => {
284287
let opengl_versions = [
285288
(4, 6),
@@ -304,9 +307,9 @@ impl<'a> ContextPrototype<'a> {
304307
&self.extensions,
305308
&self.xconn.xlib,
306309
*opengl_version,
307-
self.opengl.profile,
308-
self.opengl.debug,
309-
self.opengl.robustness,
310+
self.gl_attr.profile,
311+
self.gl_attr.debug,
312+
self.gl_attr.robustness,
310313
share,
311314
self.xconn.display,
312315
self.fb_config,
@@ -324,9 +327,9 @@ impl<'a> ContextPrototype<'a> {
324327
&self.extensions,
325328
&self.xconn.xlib,
326329
(1, 0),
327-
self.opengl.profile,
328-
self.opengl.debug,
329-
self.opengl.robustness,
330+
self.gl_attr.profile,
331+
self.gl_attr.debug,
332+
self.gl_attr.robustness,
330333
share,
331334
self.xconn.display,
332335
self.fb_config,
@@ -341,9 +344,9 @@ impl<'a> ContextPrototype<'a> {
341344
&self.extensions,
342345
&self.xconn.xlib,
343346
(major, minor),
344-
self.opengl.profile,
345-
self.opengl.debug,
346-
self.opengl.robustness,
347+
self.gl_attr.profile,
348+
self.gl_attr.debug,
349+
self.gl_attr.robustness,
347350
share,
348351
self.xconn.display,
349352
self.fb_config,
@@ -358,9 +361,9 @@ impl<'a> ContextPrototype<'a> {
358361
&self.extensions,
359362
&self.xconn.xlib,
360363
(major, minor),
361-
self.opengl.profile,
362-
self.opengl.debug,
363-
self.opengl.robustness,
364+
self.gl_attr.profile,
365+
self.gl_attr.debug,
366+
self.gl_attr.robustness,
364367
share,
365368
self.xconn.display,
366369
self.fb_config,
@@ -408,7 +411,7 @@ impl<'a> ContextPrototype<'a> {
408411
let (extra_functions, context) = self.create_context()?;
409412

410413
// vsync
411-
if self.opengl.vsync {
414+
if self.gl_attr.vsync {
412415
let _guard = MakeCurrentGuard::new(&self.xconn, window, context)
413416
.map_err(|err| CreationError::OsError(err))?;
414417

@@ -608,6 +611,7 @@ unsafe fn choose_fbconfig(
608611
xconn: &Arc<XConnection>,
609612
screen_id: raw::c_int,
610613
pf_reqs: &PixelFormatRequirements,
614+
plat_attr: &PlatformAttributes,
611615
surface_type: SurfaceType,
612616
transparent: Option<bool>,
613617
) -> Result<
@@ -622,7 +626,7 @@ unsafe fn choose_fbconfig(
622626
out.push(ffi::glx::X_RENDERABLE as raw::c_int);
623627
out.push(1);
624628

625-
if let Some(xid) = pf_reqs.x11_visual_xid {
629+
if let Some(xid) = plat_attr.x11_visual_xid {
626630
// getting the visual infos
627631
let fvi = crate::platform_impl::x11_utils::get_visual_info_from_xid(
628632
&xconn, xid,
@@ -766,7 +770,7 @@ unsafe fn choose_fbconfig(
766770
match crate::platform_impl::x11_utils::select_config(
767771
xconn,
768772
transparent,
769-
pf_reqs,
773+
plat_attr,
770774
(0..num_configs).collect(),
771775
|config_id| {
772776
let visual_infos_raw = glx.GetVisualFromFBConfig(

glutin/src/api/osmesa/mod.rs

+2
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ use crate::{
1414
Api, ContextError, CreationError, GlAttributes, GlProfile, GlRequest,
1515
PixelFormatRequirements, Robustness,
1616
};
17+
use crate::platform_impl::PlatformAttributes;
1718

1819
use winit::dpi;
1920

@@ -71,6 +72,7 @@ impl OsMesaContext {
7172
pub fn new(
7273
_pf_reqs: &PixelFormatRequirements,
7374
opengl: &GlAttributes<&OsMesaContext>,
75+
_: &PlatformAttributes,
7476
size: dpi::PhysicalSize,
7577
) -> Result<Self, CreationError> {
7678
osmesa_sys::OsMesa::try_loading()

0 commit comments

Comments
 (0)