-
-
Notifications
You must be signed in to change notification settings - Fork 468
/
mod.rs
1157 lines (1049 loc) · 37.6 KB
/
mod.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#![cfg(any(
target_os = "windows",
target_os = "linux",
target_os = "android",
target_os = "dragonfly",
target_os = "freebsd",
target_os = "netbsd",
target_os = "openbsd"
))]
#![allow(unused_variables)]
pub mod ffi;
#[cfg(not(target_os = "android"))]
mod egl {
use super::ffi;
use crate::api::dlloader::{SymTrait, SymWrapper};
#[derive(Clone)]
pub struct Egl(pub SymWrapper<ffi::egl::Egl>);
/// Because `*const libc::c_void` doesn't implement `Sync`.
unsafe impl Sync for Egl {}
impl SymTrait for ffi::egl::Egl {
fn load_with<F>(loadfn: F) -> Self
where
F: FnMut(&'static str) -> *const std::os::raw::c_void,
{
Self::load_with(loadfn)
}
}
impl Egl {
pub fn new() -> Result<Self, ()> {
#[cfg(target_os = "windows")]
let paths = vec!["libEGL.dll", "atioglxx.dll"];
#[cfg(not(target_os = "windows"))]
let paths = vec!["libEGL.so.1", "libEGL.so"];
SymWrapper::new(paths).map(|i| Egl(i))
}
}
}
#[cfg(target_os = "android")]
mod egl {
use super::ffi;
#[derive(Clone)]
pub struct Egl(pub ffi::egl::Egl);
impl Egl {
pub fn new() -> Result<Self, ()> {
Ok(Egl(ffi::egl::Egl))
}
}
}
pub use self::egl::Egl;
use crate::{
Api, ContextError, CreationError, GlAttributes, GlRequest, PixelFormat,
PixelFormatRequirements, ReleaseBehavior, Robustness,
};
use std::cell::Cell;
use std::ffi::{CStr, CString};
use std::ops::{Deref, DerefMut};
use std::os::raw;
impl Deref for Egl {
type Target = ffi::egl::Egl;
fn deref(&self) -> &ffi::egl::Egl {
&self.0
}
}
impl DerefMut for Egl {
fn deref_mut(&mut self) -> &mut ffi::egl::Egl {
&mut self.0
}
}
lazy_static! {
pub static ref EGL: Option<Egl> = Egl::new().ok();
}
/// Specifies the type of display passed as `native_display`.
#[allow(dead_code)]
pub enum NativeDisplay {
/// `None` means `EGL_DEFAULT_DISPLAY`.
X11(Option<ffi::EGLNativeDisplayType>),
/// `None` means `EGL_DEFAULT_DISPLAY`.
Gbm(Option<ffi::EGLNativeDisplayType>),
/// `None` means `EGL_DEFAULT_DISPLAY`.
Wayland(Option<ffi::EGLNativeDisplayType>),
/// `EGL_DEFAULT_DISPLAY` is mandatory for Android.
Android,
// TODO: should be `EGLDeviceEXT`
Device(ffi::EGLNativeDisplayType),
/// Don't specify any display type. Useful on windows. `None` means
/// `EGL_DEFAULT_DISPLAY`.
Other(Option<ffi::EGLNativeDisplayType>),
}
pub struct Context {
display: ffi::egl::types::EGLDisplay,
context: ffi::egl::types::EGLContext,
surface: Cell<ffi::egl::types::EGLSurface>,
api: Api,
pixel_format: PixelFormat,
#[cfg(target_os = "android")]
config_id: ffi::egl::types::EGLConfig,
}
#[cfg(target_os = "android")]
#[inline]
fn get_native_display(
egl: &Egl,
native_display: NativeDisplay,
) -> *const raw::c_void {
unsafe { egl.GetDisplay(ffi::egl::DEFAULT_DISPLAY as *mut _) }
}
#[cfg(not(target_os = "android"))]
fn get_native_display(
egl: &Egl,
native_display: NativeDisplay,
) -> *const raw::c_void {
// the first step is to query the list of extensions without any display, if
// supported
let dp_extensions = unsafe {
let p =
egl.QueryString(ffi::egl::NO_DISPLAY, ffi::egl::EXTENSIONS as i32);
// this possibility is available only with EGL 1.5 or
// EGL_EXT_platform_base, otherwise `eglQueryString` returns an
// error
if p.is_null() {
vec![]
} else {
let p = CStr::from_ptr(p);
let list = String::from_utf8(p.to_bytes().to_vec())
.unwrap_or_else(|_| format!(""));
list.split(' ').map(|e| e.to_string()).collect::<Vec<_>>()
}
};
let has_dp_extension =
|e: &str| dp_extensions.iter().find(|s| s == &e).is_some();
match native_display {
// Note: Some EGL implementations are missing the
// `eglGetPlatformDisplay(EXT)` symbol despite reporting
// `EGL_EXT_platform_base`. I'm pretty sure this is a bug.
// Therefore we detect whether the symbol is loaded in addition to
// checking for extensions.
NativeDisplay::X11(display)
if has_dp_extension("EGL_KHR_platform_x11")
&& egl.GetPlatformDisplay.is_loaded() =>
{
let d = display.unwrap_or(ffi::egl::DEFAULT_DISPLAY as *const _);
// TODO: `PLATFORM_X11_SCREEN_KHR`
unsafe {
egl.GetPlatformDisplay(
ffi::egl::PLATFORM_X11_KHR,
d as *mut _,
std::ptr::null(),
)
}
}
NativeDisplay::X11(display)
if has_dp_extension("EGL_EXT_platform_x11")
&& egl.GetPlatformDisplayEXT.is_loaded() =>
{
let d = display.unwrap_or(ffi::egl::DEFAULT_DISPLAY as *const _);
// TODO: `PLATFORM_X11_SCREEN_EXT`
unsafe {
egl.GetPlatformDisplayEXT(
ffi::egl::PLATFORM_X11_EXT,
d as *mut _,
std::ptr::null(),
)
}
}
NativeDisplay::Gbm(display)
if has_dp_extension("EGL_KHR_platform_gbm")
&& egl.GetPlatformDisplay.is_loaded() =>
{
let d = display.unwrap_or(ffi::egl::DEFAULT_DISPLAY as *const _);
unsafe {
egl.GetPlatformDisplay(
ffi::egl::PLATFORM_GBM_KHR,
d as *mut _,
std::ptr::null(),
)
}
}
NativeDisplay::Gbm(display)
if has_dp_extension("EGL_MESA_platform_gbm")
&& egl.GetPlatformDisplayEXT.is_loaded() =>
{
let d = display.unwrap_or(ffi::egl::DEFAULT_DISPLAY as *const _);
unsafe {
egl.GetPlatformDisplayEXT(
ffi::egl::PLATFORM_GBM_KHR,
d as *mut _,
std::ptr::null(),
)
}
}
NativeDisplay::Wayland(display)
if has_dp_extension("EGL_KHR_platform_wayland")
&& egl.GetPlatformDisplay.is_loaded() =>
{
let d = display.unwrap_or(ffi::egl::DEFAULT_DISPLAY as *const _);
unsafe {
egl.GetPlatformDisplay(
ffi::egl::PLATFORM_WAYLAND_KHR,
d as *mut _,
std::ptr::null(),
)
}
}
NativeDisplay::Wayland(display)
if has_dp_extension("EGL_EXT_platform_wayland")
&& egl.GetPlatformDisplayEXT.is_loaded() =>
{
let d = display.unwrap_or(ffi::egl::DEFAULT_DISPLAY as *const _);
unsafe {
egl.GetPlatformDisplayEXT(
ffi::egl::PLATFORM_WAYLAND_EXT,
d as *mut _,
std::ptr::null(),
)
}
}
// TODO: This will never be reached right now, as the android egl
// bindings use the static generator, so can't rely on
// GetPlatformDisplay(EXT).
NativeDisplay::Android
if has_dp_extension("EGL_KHR_platform_android")
&& egl.GetPlatformDisplay.is_loaded() =>
unsafe {
egl.GetPlatformDisplay(
ffi::egl::PLATFORM_ANDROID_KHR,
ffi::egl::DEFAULT_DISPLAY as *mut _,
std::ptr::null(),
)
}
NativeDisplay::Device(display)
if has_dp_extension("EGL_EXT_platform_device")
&& egl.GetPlatformDisplay.is_loaded() =>
unsafe {
egl.GetPlatformDisplay(
ffi::egl::PLATFORM_DEVICE_EXT,
display as *mut _,
std::ptr::null(),
)
}
NativeDisplay::X11(Some(display))
| NativeDisplay::Gbm(Some(display))
| NativeDisplay::Wayland(Some(display))
| NativeDisplay::Device(display)
| NativeDisplay::Other(Some(display)) => unsafe {
egl.GetDisplay(display as *mut _)
},
NativeDisplay::X11(None)
| NativeDisplay::Gbm(None)
| NativeDisplay::Wayland(None)
| NativeDisplay::Android
| NativeDisplay::Other(None) => unsafe {
egl.GetDisplay(ffi::egl::DEFAULT_DISPLAY as *mut _)
},
}
}
impl Context {
/// Start building an EGL context.
///
/// This function initializes some things and chooses the pixel format.
///
/// To finish the process, you must call `.finish(window)` on the
/// `ContextPrototype`.
pub fn new<'a>(
pf_reqs: &PixelFormatRequirements,
opengl: &'a GlAttributes<&'a Context>,
native_display: NativeDisplay,
) -> Result<ContextPrototype<'a>, CreationError> {
let egl = EGL.as_ref().unwrap();
// calling `eglGetDisplay` or equivalent
let display = get_native_display(egl, native_display);
if display.is_null() {
return Err(CreationError::OsError(
"Could not create EGL display object".to_string(),
));
}
let egl_version = unsafe {
let mut major: ffi::egl::types::EGLint = std::mem::uninitialized();
let mut minor: ffi::egl::types::EGLint = std::mem::uninitialized();
if egl.Initialize(display, &mut major, &mut minor) == 0 {
return Err(CreationError::OsError(format!(
"eglInitialize failed"
)));
}
(major, minor)
};
// the list of extensions supported by the client once initialized is
// different from the list of extensions obtained earlier
let extensions = if egl_version >= (1, 2) {
let p = unsafe {
CStr::from_ptr(
egl.QueryString(display, ffi::egl::EXTENSIONS as i32),
)
};
let list = String::from_utf8(p.to_bytes().to_vec())
.unwrap_or_else(|_| format!(""));
list.split(' ').map(|e| e.to_string()).collect::<Vec<_>>()
} else {
vec![]
};
// binding the right API and choosing the version
let (version, api) = unsafe {
match opengl.version {
GlRequest::Latest => {
if egl_version >= (1, 4) {
if egl.BindAPI(ffi::egl::OPENGL_API) != 0 {
(None, Api::OpenGl)
} else if egl.BindAPI(ffi::egl::OPENGL_ES_API) != 0 {
(None, Api::OpenGlEs)
} else {
return Err(
CreationError::OpenGlVersionNotSupported,
);
}
} else {
(None, Api::OpenGlEs)
}
}
GlRequest::Specific(Api::OpenGlEs, version) => {
if egl_version >= (1, 2) {
if egl.BindAPI(ffi::egl::OPENGL_ES_API) == 0 {
return Err(
CreationError::OpenGlVersionNotSupported,
);
}
}
(Some(version), Api::OpenGlEs)
}
GlRequest::Specific(Api::OpenGl, version) => {
if egl_version < (1, 4) {
return Err(CreationError::OpenGlVersionNotSupported);
}
if egl.BindAPI(ffi::egl::OPENGL_API) == 0 {
return Err(CreationError::OpenGlVersionNotSupported);
}
(Some(version), Api::OpenGl)
}
GlRequest::Specific(_, _) => {
return Err(CreationError::OpenGlVersionNotSupported);
}
GlRequest::GlThenGles {
opengles_version,
opengl_version,
} => {
if egl_version >= (1, 4) {
if egl.BindAPI(ffi::egl::OPENGL_API) != 0 {
(Some(opengl_version), Api::OpenGl)
} else if egl.BindAPI(ffi::egl::OPENGL_ES_API) != 0 {
(Some(opengles_version), Api::OpenGlEs)
} else {
return Err(
CreationError::OpenGlVersionNotSupported,
);
}
} else {
(Some(opengles_version), Api::OpenGlEs)
}
}
}
};
let (config_id, pixel_format) = unsafe {
choose_fbconfig(egl, display, &egl_version, api, version, pf_reqs)?
};
Ok(ContextPrototype {
opengl,
display,
egl_version,
extensions,
api,
version,
config_id,
pixel_format,
})
}
pub unsafe fn make_current(&self) -> Result<(), ContextError> {
let egl = EGL.as_ref().unwrap();
let ret = egl.MakeCurrent(
self.display,
self.surface.get(),
self.surface.get(),
self.context,
);
if ret == 0 {
match egl.GetError() as u32 {
ffi::egl::CONTEXT_LOST => {
return Err(ContextError::ContextLost)
}
err => panic!(
"make_current: eglMakeCurrent failed (eglGetError returned 0x{:x})",
err
),
}
} else {
Ok(())
}
}
#[inline]
pub fn is_current(&self) -> bool {
let egl = EGL.as_ref().unwrap();
unsafe { egl.GetCurrentContext() == self.context }
}
pub fn get_proc_address(&self, addr: &str) -> *const () {
let egl = EGL.as_ref().unwrap();
let addr = CString::new(addr.as_bytes()).unwrap();
let addr = addr.as_ptr();
unsafe { egl.GetProcAddress(addr) as *const _ }
}
#[inline]
pub fn swap_buffers(&self) -> Result<(), ContextError> {
let egl = EGL.as_ref().unwrap();
if self.surface.get() == ffi::egl::NO_SURFACE {
return Err(ContextError::ContextLost);
}
let ret = unsafe { egl.SwapBuffers(self.display, self.surface.get()) };
if ret == 0 {
match unsafe { egl.GetError() } as u32 {
ffi::egl::CONTEXT_LOST => {
return Err(ContextError::ContextLost)
}
err => panic!(
"swap_buffers: eglSwapBuffers failed (eglGetError returned 0x{:x})",
err
),
}
} else {
Ok(())
}
}
#[inline]
pub fn get_api(&self) -> Api {
self.api
}
#[inline]
pub fn get_pixel_format(&self) -> PixelFormat {
self.pixel_format.clone()
}
#[inline]
pub unsafe fn raw_handle(&self) -> ffi::egl::types::EGLContext {
self.context
}
#[inline]
pub unsafe fn get_egl_display(&self) -> ffi::egl::types::EGLDisplay {
self.display
}
// Handle Android Life Cycle.
// Android has started the activity or sent it to foreground.
// Create a new surface and attach it to the recreated ANativeWindow.
// Restore the EGLContext.
#[cfg(target_os = "android")]
pub unsafe fn on_surface_created(
&self,
native_window: ffi::EGLNativeWindowType,
) {
let egl = EGL.as_ref().unwrap();
if self.surface.get() != ffi::egl::NO_SURFACE {
return;
}
self.surface.set(egl.CreateWindowSurface(
self.display,
self.config_id,
native_window,
std::ptr::null(),
));
if self.surface.get().is_null() {
panic!(
"on_surface_created: eglCreateWindowSurface failed with 0x{:x}",
egl.GetError()
)
}
let ret = egl.MakeCurrent(
self.display,
self.surface.get(),
self.surface.get(),
self.context,
);
if ret == 0 {
panic!(
"on_surface_created: eglMakeCurrent failed with 0x{:x}",
egl.GetError()
)
}
}
// Handle Android Life Cycle.
// Android has stopped the activity or sent it to background.
// Release the surface attached to the destroyed ANativeWindow.
// The EGLContext is not destroyed so it can be restored later.
#[cfg(target_os = "android")]
pub unsafe fn on_surface_destroyed(&self) {
let egl = EGL.as_ref().unwrap();
if self.surface.get() == ffi::egl::NO_SURFACE {
return;
}
let ret = egl.MakeCurrent(
self.display,
ffi::egl::NO_SURFACE,
ffi::egl::NO_SURFACE,
ffi::egl::NO_CONTEXT,
);
if ret == 0 {
panic!(
"on_surface_destroyed: eglMakeCurrent failed with 0x{:x}",
egl.GetError()
)
}
egl.DestroySurface(self.display, self.surface.get());
self.surface.set(ffi::egl::NO_SURFACE);
}
}
unsafe impl Send for Context {}
unsafe impl Sync for Context {}
impl Drop for Context {
fn drop(&mut self) {
// https://stackoverflow.com/questions/54402688/recreate-eglcreatewindowsurface-with-same-native-window
let egl = EGL.as_ref().unwrap();
unsafe {
// Ok, so we got to call `glFinish` before destroying the context to
// insure it actually gets destroyed. This requires making the this
// context current.
//
// Now, if the user has multiple contexts, and they drop this one
// unintentionally between calls to the other context, this could
// result in a !FUN! time debuging.
//
// Then again, if they're **unintentionally** dropping contexts, I
// think they got bigger problems.
self.make_current().unwrap();
let gl_finish_fn = self.get_proc_address("glFinish");
assert!(gl_finish_fn != std::ptr::null());
let gl_finish_fn =
std::mem::transmute::<_, extern "system" fn()>(gl_finish_fn);
gl_finish_fn();
let ret = egl.MakeCurrent(
self.display,
ffi::egl::NO_SURFACE,
ffi::egl::NO_SURFACE,
ffi::egl::NO_CONTEXT,
);
if ret == 0 {
panic!(
"drop: eglMakeCurrent failed with 0x{:x}",
egl.GetError()
)
}
egl.DestroyContext(self.display, self.context);
egl.DestroySurface(self.display, self.surface.get());
egl.Terminate(self.display);
}
}
}
pub struct ContextPrototype<'a> {
opengl: &'a GlAttributes<&'a Context>,
display: ffi::egl::types::EGLDisplay,
egl_version: (ffi::egl::types::EGLint, ffi::egl::types::EGLint),
extensions: Vec<String>,
api: Api,
version: Option<(u8, u8)>,
config_id: ffi::egl::types::EGLConfig,
pixel_format: PixelFormat,
}
impl<'a> ContextPrototype<'a> {
pub fn get_native_visual_id(&self) -> ffi::egl::types::EGLint {
let egl = EGL.as_ref().unwrap();
let mut value = unsafe { std::mem::uninitialized() };
let ret = unsafe {
egl.GetConfigAttrib(
self.display,
self.config_id,
ffi::egl::NATIVE_VISUAL_ID as ffi::egl::types::EGLint,
&mut value,
)
};
if ret == 0 {
panic!(
"get_native_visual_id: eglGetConfigAttrib failed with 0x{:x}",
unsafe { egl.GetError() }
)
};
value
}
pub fn finish(
self,
native_window: ffi::EGLNativeWindowType,
) -> Result<Context, CreationError> {
let egl = EGL.as_ref().unwrap();
let surface = unsafe {
let surface = egl.CreateWindowSurface(
self.display,
self.config_id,
native_window,
std::ptr::null(),
);
if surface.is_null() {
return Err(CreationError::OsError(format!(
"eglCreateWindowSurface failed"
)));
}
surface
};
self.finish_impl(surface)
}
#[cfg(any(target_os = "android", target_os = "windows"))]
pub fn finish_pbuffer(
self,
dims: (u32, u32),
) -> Result<Context, CreationError> {
let egl = EGL.as_ref().unwrap();
let attrs = &[
ffi::egl::WIDTH as raw::c_int,
dims.0 as raw::c_int,
ffi::egl::HEIGHT as raw::c_int,
dims.1 as raw::c_int,
ffi::egl::NONE as raw::c_int,
];
let surface = unsafe {
let surface = egl.CreatePbufferSurface(
self.display,
self.config_id,
attrs.as_ptr(),
);
if surface.is_null() {
return Err(CreationError::OsError(format!(
"eglCreatePbufferSurface failed"
)));
}
surface
};
self.finish_impl(surface)
}
fn finish_impl(
self,
surface: ffi::egl::types::EGLSurface,
) -> Result<Context, CreationError> {
let share = match self.opengl.sharing {
Some(ctx) => ctx.context,
None => std::ptr::null(),
};
let context = unsafe {
if let Some(version) = self.version {
create_context(
self.display,
&self.egl_version,
&self.extensions,
self.api,
version,
self.config_id,
self.opengl.debug,
self.opengl.robustness,
share,
)?
} else if self.api == Api::OpenGlEs {
if let Ok(ctx) = create_context(
self.display,
&self.egl_version,
&self.extensions,
self.api,
(2, 0),
self.config_id,
self.opengl.debug,
self.opengl.robustness,
share,
) {
ctx
} else if let Ok(ctx) = create_context(
self.display,
&self.egl_version,
&self.extensions,
self.api,
(1, 0),
self.config_id,
self.opengl.debug,
self.opengl.robustness,
share,
) {
ctx
} else {
return Err(CreationError::OpenGlVersionNotSupported);
}
} else {
if let Ok(ctx) = create_context(
self.display,
&self.egl_version,
&self.extensions,
self.api,
(3, 2),
self.config_id,
self.opengl.debug,
self.opengl.robustness,
share,
) {
ctx
} else if let Ok(ctx) = create_context(
self.display,
&self.egl_version,
&self.extensions,
self.api,
(3, 1),
self.config_id,
self.opengl.debug,
self.opengl.robustness,
share,
) {
ctx
} else if let Ok(ctx) = create_context(
self.display,
&self.egl_version,
&self.extensions,
self.api,
(1, 0),
self.config_id,
self.opengl.debug,
self.opengl.robustness,
share,
) {
ctx
} else {
return Err(CreationError::OpenGlVersionNotSupported);
}
}
};
Ok(Context {
display: self.display,
context,
surface: Cell::new(surface),
api: self.api,
pixel_format: self.pixel_format,
#[cfg(target_os = "android")]
config_id: self.config_id,
})
}
}
unsafe fn choose_fbconfig(
egl: &Egl,
display: ffi::egl::types::EGLDisplay,
egl_version: &(ffi::egl::types::EGLint, ffi::egl::types::EGLint),
api: Api,
version: Option<(u8, u8)>,
reqs: &PixelFormatRequirements,
) -> Result<(ffi::egl::types::EGLConfig, PixelFormat), CreationError> {
let descriptor = {
let mut out: Vec<raw::c_int> = Vec::with_capacity(37);
if egl_version >= &(1, 2) {
out.push(ffi::egl::COLOR_BUFFER_TYPE as raw::c_int);
out.push(ffi::egl::RGB_BUFFER as raw::c_int);
}
out.push(ffi::egl::SURFACE_TYPE as raw::c_int);
// TODO: Some versions of Mesa report a BAD_ATTRIBUTE error
// if we ask for PBUFFER_BIT as well as WINDOW_BIT
out.push((ffi::egl::WINDOW_BIT) as raw::c_int);
match (api, version) {
(Api::OpenGlEs, Some((3, _))) => {
if egl_version < &(1, 3) {
return Err(CreationError::NoAvailablePixelFormat);
}
out.push(ffi::egl::RENDERABLE_TYPE as raw::c_int);
out.push(ffi::egl::OPENGL_ES3_BIT as raw::c_int);
out.push(ffi::egl::CONFORMANT as raw::c_int);
out.push(ffi::egl::OPENGL_ES3_BIT as raw::c_int);
}
(Api::OpenGlEs, Some((2, _))) => {
if egl_version < &(1, 3) {
return Err(CreationError::NoAvailablePixelFormat);
}
out.push(ffi::egl::RENDERABLE_TYPE as raw::c_int);
out.push(ffi::egl::OPENGL_ES2_BIT as raw::c_int);
out.push(ffi::egl::CONFORMANT as raw::c_int);
out.push(ffi::egl::OPENGL_ES2_BIT as raw::c_int);
}
(Api::OpenGlEs, Some((1, _))) => {
if egl_version >= &(1, 3) {
out.push(ffi::egl::RENDERABLE_TYPE as raw::c_int);
out.push(ffi::egl::OPENGL_ES_BIT as raw::c_int);
out.push(ffi::egl::CONFORMANT as raw::c_int);
out.push(ffi::egl::OPENGL_ES_BIT as raw::c_int);
}
}
(Api::OpenGlEs, _) => unimplemented!(),
(Api::OpenGl, _) => {
if egl_version < &(1, 3) {
return Err(CreationError::NoAvailablePixelFormat);
}
out.push(ffi::egl::RENDERABLE_TYPE as raw::c_int);
out.push(ffi::egl::OPENGL_BIT as raw::c_int);
out.push(ffi::egl::CONFORMANT as raw::c_int);
out.push(ffi::egl::OPENGL_BIT as raw::c_int);
}
(_, _) => unimplemented!(),
};
if let Some(hardware_accelerated) = reqs.hardware_accelerated {
out.push(ffi::egl::CONFIG_CAVEAT as raw::c_int);
out.push(if hardware_accelerated {
ffi::egl::NONE as raw::c_int
} else {
ffi::egl::SLOW_CONFIG as raw::c_int
});
}
if let Some(color) = reqs.color_bits {
out.push(ffi::egl::RED_SIZE as raw::c_int);
out.push((color / 3) as raw::c_int);
out.push(ffi::egl::GREEN_SIZE as raw::c_int);
out.push(
(color / 3 + if color % 3 != 0 { 1 } else { 0 }) as raw::c_int,
);
out.push(ffi::egl::BLUE_SIZE as raw::c_int);
out.push(
(color / 3 + if color % 3 == 2 { 1 } else { 0 }) as raw::c_int,
);
}
if let Some(alpha) = reqs.alpha_bits {
out.push(ffi::egl::ALPHA_SIZE as raw::c_int);
out.push(alpha as raw::c_int);
}
if let Some(depth) = reqs.depth_bits {
out.push(ffi::egl::DEPTH_SIZE as raw::c_int);
out.push(depth as raw::c_int);
}
if let Some(stencil) = reqs.stencil_bits {
out.push(ffi::egl::STENCIL_SIZE as raw::c_int);
out.push(stencil as raw::c_int);
}
if let Some(true) = reqs.double_buffer {
return Err(CreationError::NoAvailablePixelFormat);
}
if let Some(multisampling) = reqs.multisampling {
out.push(ffi::egl::SAMPLES as raw::c_int);
out.push(multisampling as raw::c_int);
}
if reqs.stereoscopy {
return Err(CreationError::NoAvailablePixelFormat);
}
if let Some(xid) = reqs.x11_visual_xid {
out.push(ffi::egl::NATIVE_VISUAL_ID as raw::c_int);
out.push(xid as raw::c_int);
}
// FIXME: srgb is not taken into account
match reqs.release_behavior {
ReleaseBehavior::Flush => (),
ReleaseBehavior::None => {
// TODO: with EGL you need to manually set the behavior
unimplemented!()
}
}
out.push(ffi::egl::NONE as raw::c_int);
out
};
// calling `eglChooseConfig`
let mut config_id = std::mem::uninitialized();
let mut num_configs = std::mem::uninitialized();
if egl.ChooseConfig(
display,
descriptor.as_ptr(),
&mut config_id,
1,
&mut num_configs,
) == 0
{
return Err(CreationError::OsError(format!("eglChooseConfig failed")));
}
if num_configs == 0 {
return Err(CreationError::NoAvailablePixelFormat);
}
// analyzing each config
macro_rules! attrib {
($egl:expr, $display:expr, $config:expr, $attr:expr) => {{
let mut value = std::mem::uninitialized();
let res = $egl.GetConfigAttrib(
$display,
$config,
$attr as ffi::egl::types::EGLint,
&mut value,
);
if res == 0 {
return Err(CreationError::OsError(format!(
"eglGetConfigAttrib failed"
)));
}
value
}};
};
let desc = PixelFormat {
hardware_accelerated: attrib!(
egl,
display,
config_id,
ffi::egl::CONFIG_CAVEAT
) != ffi::egl::SLOW_CONFIG as i32,
color_bits: attrib!(egl, display, config_id, ffi::egl::RED_SIZE) as u8
+ attrib!(egl, display, config_id, ffi::egl::BLUE_SIZE) as u8
+ attrib!(egl, display, config_id, ffi::egl::GREEN_SIZE) as u8,
alpha_bits: attrib!(egl, display, config_id, ffi::egl::ALPHA_SIZE)
as u8,
depth_bits: attrib!(egl, display, config_id, ffi::egl::DEPTH_SIZE)
as u8,
stencil_bits: attrib!(egl, display, config_id, ffi::egl::STENCIL_SIZE)
as u8,
stereoscopy: false,
double_buffer: true,
multisampling: match attrib!(egl, display, config_id, ffi::egl::SAMPLES)
{
0 | 1 => None,
a => Some(a as u16),
},
srgb: false, // TODO: use EGL_KHR_gl_colorspace to know that
};
Ok((config_id, desc))
}
unsafe fn create_context(
display: ffi::egl::types::EGLDisplay,
egl_version: &(ffi::egl::types::EGLint, ffi::egl::types::EGLint),
extensions: &[String],
api: Api,
version: (u8, u8),