1 // Licensed under the Apache License, Version 2.0.
2 // This file may not be copied, modified, or distributed except according to those terms.
3 
4 //! Based on https://github.com/tomaka/glutin/blob/1b2d62c0e9/src/api/egl/mod.rs
5 #![cfg(windows)]
6 #![allow(unused_variables)]
7 
8 use glutin::ContextError;
9 use glutin::CreationError;
10 use glutin::GlAttributes;
11 use glutin::GlRequest;
12 use glutin::PixelFormat;
13 use glutin::PixelFormatRequirements;
14 use glutin::ReleaseBehavior;
15 use glutin::Robustness;
16 use glutin::Api;
17 
18 use std::ffi::{CStr, CString};
19 use std::os::raw::c_int;
20 use std::ptr;
21 use std::cell::Cell;
22 
23 use mozangle::egl::ffi as egl;
24 mod ffi {
25     pub use mozangle::egl::ffi as egl;
26     pub use mozangle::egl::ffi::*;
27 }
28 
29 pub struct Context {
30     display: ffi::egl::types::EGLDisplay,
31     context: ffi::egl::types::EGLContext,
32     surface: Cell<ffi::egl::types::EGLSurface>,
33     api: Api,
34     pixel_format: PixelFormat,
35 }
36 
37 impl Context {
38     /// Start building an EGL context.
39     ///
40     /// This function initializes some things and chooses the pixel format.
41     ///
42     /// To finish the process, you must call `.finish(window)` on the `ContextPrototype`.
new<'a>( pf_reqs: &PixelFormatRequirements, opengl: &'a GlAttributes<&'a Context>, ) -> Result<ContextPrototype<'a>, CreationError>43     pub fn new<'a>(
44         pf_reqs: &PixelFormatRequirements,
45         opengl: &'a GlAttributes<&'a Context>,
46     ) -> Result<ContextPrototype<'a>, CreationError>
47     {
48         if opengl.sharing.is_some() {
49             unimplemented!()
50         }
51 
52         // calling `eglGetDisplay` or equivalent
53         let display = unsafe { egl::GetDisplay(ptr::null_mut()) };
54 
55         if display.is_null() {
56             return Err(CreationError::PlatformSpecific("Could not create EGL display object".to_string()));
57         }
58 
59         let egl_version = unsafe {
60             let mut major: ffi::egl::types::EGLint = 0; // out param
61             let mut minor: ffi::egl::types::EGLint = 0; // out param
62 
63             if egl::Initialize(display, &mut major, &mut minor) == 0 {
64                 return Err(CreationError::OsError(format!("eglInitialize failed")))
65             }
66 
67             (major, minor)
68         };
69 
70         // the list of extensions supported by the client once initialized is different from the
71         // list of extensions obtained earlier
72         let extensions = if egl_version >= (1, 2) {
73             let p = unsafe { CStr::from_ptr(egl::QueryString(display, ffi::egl::EXTENSIONS as i32)) };
74             let list = String::from_utf8(p.to_bytes().to_vec()).unwrap_or_else(|_| format!(""));
75             list.split(' ').map(|e| e.to_string()).collect::<Vec<_>>()
76 
77         } else {
78             vec![]
79         };
80 
81         // binding the right API and choosing the version
82         let (version, api) = unsafe {
83             match opengl.version {
84                 GlRequest::Latest => {
85                     if egl_version >= (1, 4) {
86                         if egl::BindAPI(ffi::egl::OPENGL_API) != 0 {
87                             (None, Api::OpenGl)
88                         } else if egl::BindAPI(ffi::egl::OPENGL_ES_API) != 0 {
89                             (None, Api::OpenGlEs)
90                         } else {
91                             return Err(CreationError::OpenGlVersionNotSupported);
92                         }
93                     } else {
94                         (None, Api::OpenGlEs)
95                     }
96                 },
97                 GlRequest::Specific(Api::OpenGlEs, version) => {
98                     if egl_version >= (1, 2) {
99                         if egl::BindAPI(ffi::egl::OPENGL_ES_API) == 0 {
100                             return Err(CreationError::OpenGlVersionNotSupported);
101                         }
102                     }
103                     (Some(version), Api::OpenGlEs)
104                 },
105                 GlRequest::Specific(Api::OpenGl, version) => {
106                     if egl_version < (1, 4) {
107                         return Err(CreationError::OpenGlVersionNotSupported);
108                     }
109                     if egl::BindAPI(ffi::egl::OPENGL_API) == 0 {
110                         return Err(CreationError::OpenGlVersionNotSupported);
111                     }
112                     (Some(version), Api::OpenGl)
113                 },
114                 GlRequest::Specific(_, _) => return Err(CreationError::OpenGlVersionNotSupported),
115                 GlRequest::GlThenGles { opengles_version, opengl_version } => {
116                     if egl_version >= (1, 4) {
117                         if egl::BindAPI(ffi::egl::OPENGL_API) != 0 {
118                             (Some(opengl_version), Api::OpenGl)
119                         } else if egl::BindAPI(ffi::egl::OPENGL_ES_API) != 0 {
120                             (Some(opengles_version), Api::OpenGlEs)
121                         } else {
122                             return Err(CreationError::OpenGlVersionNotSupported);
123                         }
124                     } else {
125                         (Some(opengles_version), Api::OpenGlEs)
126                     }
127                 },
128             }
129         };
130 
131         let (config_id, pixel_format) = unsafe {
132             choose_fbconfig(display, &egl_version, api, version, pf_reqs)?
133         };
134 
135         Ok(ContextPrototype {
136             opengl: opengl,
137             display: display,
138             egl_version: egl_version,
139             extensions: extensions,
140             api: api,
141             version: version,
142             config_id: config_id,
143             pixel_format: pixel_format,
144         })
145     }
146 
147     #[inline]
swap_buffers(&self) -> Result<(), ContextError>148     pub fn swap_buffers(&self) -> Result<(), ContextError> {
149         if self.surface.get() == ffi::egl::NO_SURFACE {
150             return Err(ContextError::ContextLost);
151         }
152 
153         let ret = unsafe {
154             egl::SwapBuffers(self.display, self.surface.get())
155         };
156 
157         if ret == 0 {
158             match unsafe { egl::GetError() } as u32 {
159                 ffi::egl::CONTEXT_LOST => return Err(ContextError::ContextLost),
160                 err => panic!("eglSwapBuffers failed (eglGetError returned 0x{:x})", err)
161             }
162 
163         } else {
164             Ok(())
165         }
166     }
167 
make_current(&self) -> Result<(), ContextError>168     pub unsafe fn make_current(&self) -> Result<(), ContextError> {
169         let ret = egl::MakeCurrent(self.display, self.surface.get(), self.surface.get(), self.context);
170 
171         if ret == 0 {
172             match egl::GetError() as u32 {
173                 ffi::egl::CONTEXT_LOST => return Err(ContextError::ContextLost),
174                 err => panic!("eglMakeCurrent failed (eglGetError returned 0x{:x})", err)
175             }
176 
177         } else {
178             Ok(())
179         }
180     }
181 
182     #[inline]
is_current(&self) -> bool183     pub fn is_current(&self) -> bool {
184         unsafe { egl::GetCurrentContext() == self.context }
185     }
186 
get_proc_address(&self, addr: &str) -> *const ()187     pub fn get_proc_address(&self, addr: &str) -> *const () {
188         let addr = CString::new(addr.as_bytes()).unwrap();
189         let addr = addr.as_ptr();
190         unsafe {
191             egl::GetProcAddress(addr) as *const _
192         }
193     }
194 
195     #[inline]
get_api(&self) -> Api196     pub fn get_api(&self) -> Api {
197         self.api
198     }
199 
200     #[inline]
get_pixel_format(&self) -> PixelFormat201     pub fn get_pixel_format(&self) -> PixelFormat {
202         self.pixel_format.clone()
203     }
204 }
205 
206 unsafe impl Send for Context {}
207 unsafe impl Sync for Context {}
208 
209 impl Drop for Context {
drop(&mut self)210     fn drop(&mut self) {
211         unsafe {
212             // we don't call MakeCurrent(0, 0) because we are not sure that the context
213             // is still the current one
214             egl::DestroyContext(self.display, self.context);
215             egl::DestroySurface(self.display, self.surface.get());
216             egl::Terminate(self.display);
217         }
218     }
219 }
220 
221 pub struct ContextPrototype<'a> {
222     opengl: &'a GlAttributes<&'a Context>,
223     display: ffi::egl::types::EGLDisplay,
224     egl_version: (ffi::egl::types::EGLint, ffi::egl::types::EGLint),
225     extensions: Vec<String>,
226     api: Api,
227     version: Option<(u8, u8)>,
228     config_id: ffi::egl::types::EGLConfig,
229     pixel_format: PixelFormat,
230 }
231 
232 impl<'a> ContextPrototype<'a> {
get_native_visual_id(&self) -> ffi::egl::types::EGLint233     pub fn get_native_visual_id(&self) -> ffi::egl::types::EGLint {
234         let mut value = 0;
235         let ret = unsafe { egl::GetConfigAttrib(self.display, self.config_id,
236                                                     ffi::egl::NATIVE_VISUAL_ID
237                                                     as ffi::egl::types::EGLint, &mut value) };
238         if ret == 0 { panic!("eglGetConfigAttrib failed") };
239         value
240     }
241 
finish(self, native_window: ffi::EGLNativeWindowType) -> Result<Context, CreationError>242     pub fn finish(self, native_window: ffi::EGLNativeWindowType)
243                   -> Result<Context, CreationError>
244     {
245         let surface = unsafe {
246             let surface = egl::CreateWindowSurface(self.display, self.config_id, native_window,
247                                                        ptr::null());
248             if surface.is_null() {
249                 return Err(CreationError::OsError(format!("eglCreateWindowSurface failed")))
250             }
251             surface
252         };
253 
254         self.finish_impl(surface)
255     }
256 
finish_pbuffer(self, dimensions: (u32, u32)) -> Result<Context, CreationError>257     pub fn finish_pbuffer(self, dimensions: (u32, u32)) -> Result<Context, CreationError> {
258         let attrs = &[
259             ffi::egl::WIDTH as c_int, dimensions.0 as c_int,
260             ffi::egl::HEIGHT as c_int, dimensions.1 as c_int,
261             ffi::egl::NONE as c_int,
262         ];
263 
264         let surface = unsafe {
265             let surface = egl::CreatePbufferSurface(self.display, self.config_id,
266                                                         attrs.as_ptr());
267             if surface.is_null() {
268                 return Err(CreationError::OsError(format!("eglCreatePbufferSurface failed")))
269             }
270             surface
271         };
272 
273         self.finish_impl(surface)
274     }
275 
finish_impl(self, surface: ffi::egl::types::EGLSurface) -> Result<Context, CreationError>276     fn finish_impl(self, surface: ffi::egl::types::EGLSurface)
277                    -> Result<Context, CreationError>
278     {
279         let context = unsafe {
280             if let Some(version) = self.version {
281                 create_context(self.display, &self.egl_version,
282                                &self.extensions, self.api, version, self.config_id,
283                                self.opengl.debug, self.opengl.robustness)?
284 
285             } else if self.api == Api::OpenGlEs {
286                 if let Ok(ctxt) = create_context(self.display, &self.egl_version,
287                                                  &self.extensions, self.api, (2, 0), self.config_id,
288                                                  self.opengl.debug, self.opengl.robustness)
289                 {
290                     ctxt
291                 } else if let Ok(ctxt) = create_context(self.display, &self.egl_version,
292                                                         &self.extensions, self.api, (1, 0),
293                                                         self.config_id, self.opengl.debug,
294                                                         self.opengl.robustness)
295                 {
296                     ctxt
297                 } else {
298                     return Err(CreationError::OpenGlVersionNotSupported);
299                 }
300 
301             } else {
302                 if let Ok(ctxt) = create_context(self.display, &self.egl_version,
303                                                  &self.extensions, self.api, (3, 2), self.config_id,
304                                                  self.opengl.debug, self.opengl.robustness)
305                 {
306                     ctxt
307                 } else if let Ok(ctxt) = create_context(self.display, &self.egl_version,
308                                                         &self.extensions, self.api, (3, 1),
309                                                         self.config_id, self.opengl.debug,
310                                                         self.opengl.robustness)
311                 {
312                     ctxt
313                 } else if let Ok(ctxt) = create_context(self.display, &self.egl_version,
314                                                         &self.extensions, self.api, (1, 0),
315                                                         self.config_id, self.opengl.debug,
316                                                         self.opengl.robustness)
317                 {
318                     ctxt
319                 } else {
320                     return Err(CreationError::OpenGlVersionNotSupported);
321                 }
322             }
323         };
324 
325         Ok(Context {
326             display: self.display,
327             context: context,
328             surface: Cell::new(surface),
329             api: self.api,
330             pixel_format: self.pixel_format,
331         })
332     }
333 }
334 
choose_fbconfig(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>335 unsafe fn choose_fbconfig(display: ffi::egl::types::EGLDisplay,
336                           egl_version: &(ffi::egl::types::EGLint, ffi::egl::types::EGLint),
337                           api: Api, version: Option<(u8, u8)>, reqs: &PixelFormatRequirements)
338                           -> Result<(ffi::egl::types::EGLConfig, PixelFormat), CreationError>
339 {
340     let descriptor = {
341         let mut out: Vec<c_int> = Vec::with_capacity(37);
342 
343         if egl_version >= &(1, 2) {
344             out.push(ffi::egl::COLOR_BUFFER_TYPE as c_int);
345             out.push(ffi::egl::RGB_BUFFER as c_int);
346         }
347 
348         out.push(ffi::egl::SURFACE_TYPE as c_int);
349         // TODO: Some versions of Mesa report a BAD_ATTRIBUTE error
350         // if we ask for PBUFFER_BIT as well as WINDOW_BIT
351         out.push((ffi::egl::WINDOW_BIT) as c_int);
352 
353         match (api, version) {
354             (Api::OpenGlEs, Some((3, _))) => {
355                 if egl_version < &(1, 3) { return Err(CreationError::NoAvailablePixelFormat); }
356                 out.push(ffi::egl::RENDERABLE_TYPE as c_int);
357                 out.push(ffi::egl::OPENGL_ES3_BIT as c_int);
358                 out.push(ffi::egl::CONFORMANT as c_int);
359                 out.push(ffi::egl::OPENGL_ES3_BIT as c_int);
360             },
361             (Api::OpenGlEs, Some((2, _))) => {
362                 if egl_version < &(1, 3) { return Err(CreationError::NoAvailablePixelFormat); }
363                 out.push(ffi::egl::RENDERABLE_TYPE as c_int);
364                 out.push(ffi::egl::OPENGL_ES2_BIT as c_int);
365                 out.push(ffi::egl::CONFORMANT as c_int);
366                 out.push(ffi::egl::OPENGL_ES2_BIT as c_int);
367             },
368             (Api::OpenGlEs, Some((1, _))) => {
369                 if egl_version >= &(1, 3) {
370                     out.push(ffi::egl::RENDERABLE_TYPE as c_int);
371                     out.push(ffi::egl::OPENGL_ES_BIT as c_int);
372                     out.push(ffi::egl::CONFORMANT as c_int);
373                     out.push(ffi::egl::OPENGL_ES_BIT as c_int);
374                 }
375             },
376             (Api::OpenGlEs, _) => unimplemented!(),
377             (Api::OpenGl, _) => {
378                 if egl_version < &(1, 3) { return Err(CreationError::NoAvailablePixelFormat); }
379                 out.push(ffi::egl::RENDERABLE_TYPE as c_int);
380                 out.push(ffi::egl::OPENGL_BIT as c_int);
381                 out.push(ffi::egl::CONFORMANT as c_int);
382                 out.push(ffi::egl::OPENGL_BIT as c_int);
383             },
384             (_, _) => unimplemented!(),
385         };
386 
387         if let Some(hardware_accelerated) = reqs.hardware_accelerated {
388             out.push(ffi::egl::CONFIG_CAVEAT as c_int);
389             out.push(if hardware_accelerated {
390                 ffi::egl::NONE as c_int
391             } else {
392                 ffi::egl::SLOW_CONFIG as c_int
393             });
394         }
395 
396         if let Some(color) = reqs.color_bits {
397             out.push(ffi::egl::RED_SIZE as c_int);
398             out.push((color / 3) as c_int);
399             out.push(ffi::egl::GREEN_SIZE as c_int);
400             out.push((color / 3 + if color % 3 != 0 { 1 } else { 0 }) as c_int);
401             out.push(ffi::egl::BLUE_SIZE as c_int);
402             out.push((color / 3 + if color % 3 == 2 { 1 } else { 0 }) as c_int);
403         }
404 
405         if let Some(alpha) = reqs.alpha_bits {
406             out.push(ffi::egl::ALPHA_SIZE as c_int);
407             out.push(alpha as c_int);
408         }
409 
410         if let Some(depth) = reqs.depth_bits {
411             out.push(ffi::egl::DEPTH_SIZE as c_int);
412             out.push(depth as c_int);
413         }
414 
415         if let Some(stencil) = reqs.stencil_bits {
416             out.push(ffi::egl::STENCIL_SIZE as c_int);
417             out.push(stencil as c_int);
418         }
419 
420         if let Some(true) = reqs.double_buffer {
421             return Err(CreationError::NoAvailablePixelFormat);
422         }
423 
424         if let Some(multisampling) = reqs.multisampling {
425             out.push(ffi::egl::SAMPLES as c_int);
426             out.push(multisampling as c_int);
427         }
428 
429         if reqs.stereoscopy {
430             return Err(CreationError::NoAvailablePixelFormat);
431         }
432 
433         // FIXME: srgb is not taken into account
434 
435         match reqs.release_behavior {
436             ReleaseBehavior::Flush => (),
437             ReleaseBehavior::None => {
438                 // TODO: with EGL you need to manually set the behavior
439                 unimplemented!()
440             },
441         }
442 
443         out.push(ffi::egl::NONE as c_int);
444         out
445     };
446 
447     // calling `eglChooseConfig`
448     let mut config_id = ptr::null(); // out param
449     let mut num_configs = 0;         // out param
450     if egl::ChooseConfig(display, descriptor.as_ptr(), &mut config_id, 1, &mut num_configs) == 0 {
451         return Err(CreationError::OsError(format!("eglChooseConfig failed")));
452     }
453     if num_configs == 0 {
454         return Err(CreationError::NoAvailablePixelFormat);
455     }
456 
457     // analyzing each config
458     macro_rules! attrib {
459         ($display:expr, $config:expr, $attr:expr) => (
460             {
461                 let mut value = 0; // out param
462                 let res = egl::GetConfigAttrib($display, $config,
463                                                $attr as ffi::egl::types::EGLint, &mut value);
464                 if res == 0 {
465                     return Err(CreationError::OsError(format!("eglGetConfigAttrib failed")));
466                 }
467                 value
468             }
469         )
470     }
471 
472     let desc = PixelFormat {
473         hardware_accelerated: attrib!(display, config_id, ffi::egl::CONFIG_CAVEAT)
474                                       != ffi::egl::SLOW_CONFIG as i32,
475         color_bits: attrib!(display, config_id, ffi::egl::RED_SIZE) as u8 +
476                     attrib!(display, config_id, ffi::egl::BLUE_SIZE) as u8 +
477                     attrib!(display, config_id, ffi::egl::GREEN_SIZE) as u8,
478         alpha_bits: attrib!(display, config_id, ffi::egl::ALPHA_SIZE) as u8,
479         depth_bits: attrib!(display, config_id, ffi::egl::DEPTH_SIZE) as u8,
480         stencil_bits: attrib!(display, config_id, ffi::egl::STENCIL_SIZE) as u8,
481         stereoscopy: false,
482         double_buffer: true,
483         multisampling: match attrib!(display, config_id, ffi::egl::SAMPLES) {
484             0 | 1 => None,
485             a => Some(a as u16),
486         },
487         srgb: false,        // TODO: use EGL_KHR_gl_colorspace to know that
488     };
489 
490     Ok((config_id, desc))
491 }
492 
create_context(display: ffi::egl::types::EGLDisplay, egl_version: &(ffi::egl::types::EGLint, ffi::egl::types::EGLint), extensions: &[String], api: Api, version: (u8, u8), config_id: ffi::egl::types::EGLConfig, gl_debug: bool, gl_robustness: Robustness) -> Result<ffi::egl::types::EGLContext, CreationError>493 unsafe fn create_context(display: ffi::egl::types::EGLDisplay,
494                          egl_version: &(ffi::egl::types::EGLint, ffi::egl::types::EGLint),
495                          extensions: &[String], api: Api, version: (u8, u8),
496                          config_id: ffi::egl::types::EGLConfig, gl_debug: bool,
497                          gl_robustness: Robustness)
498                          -> Result<ffi::egl::types::EGLContext, CreationError>
499 {
500     let mut context_attributes = Vec::with_capacity(10);
501     let mut flags = 0;
502 
503     if egl_version >= &(1, 5) || extensions.iter().find(|s| s == &"EGL_KHR_create_context")
504                                                   .is_some()
505     {
506         context_attributes.push(ffi::egl::CONTEXT_MAJOR_VERSION as i32);
507         context_attributes.push(version.0 as i32);
508         context_attributes.push(ffi::egl::CONTEXT_MINOR_VERSION as i32);
509         context_attributes.push(version.1 as i32);
510 
511         // handling robustness
512         let supports_robustness = egl_version >= &(1, 5) ||
513                                   extensions.iter()
514                                             .find(|s| s == &"EGL_EXT_create_context_robustness")
515                                             .is_some();
516 
517         match gl_robustness {
518             Robustness::NotRobust => (),
519 
520             Robustness::NoError => {
521                 if extensions.iter().find(|s| s == &"EGL_KHR_create_context_no_error").is_some() {
522                     context_attributes.push(ffi::egl::CONTEXT_OPENGL_NO_ERROR_KHR as c_int);
523                     context_attributes.push(1);
524                 }
525             },
526 
527             Robustness::RobustNoResetNotification => {
528                 if supports_robustness {
529                     context_attributes.push(ffi::egl::CONTEXT_OPENGL_RESET_NOTIFICATION_STRATEGY
530                                             as c_int);
531                     context_attributes.push(ffi::egl::NO_RESET_NOTIFICATION as c_int);
532                     flags = flags | ffi::egl::CONTEXT_OPENGL_ROBUST_ACCESS as c_int;
533                 } else {
534                     return Err(CreationError::RobustnessNotSupported);
535                 }
536             },
537 
538             Robustness::TryRobustNoResetNotification => {
539                 if supports_robustness {
540                     context_attributes.push(ffi::egl::CONTEXT_OPENGL_RESET_NOTIFICATION_STRATEGY
541                                             as c_int);
542                     context_attributes.push(ffi::egl::NO_RESET_NOTIFICATION as c_int);
543                     flags = flags | ffi::egl::CONTEXT_OPENGL_ROBUST_ACCESS as c_int;
544                 }
545             },
546 
547             Robustness::RobustLoseContextOnReset => {
548                 if supports_robustness {
549                     context_attributes.push(ffi::egl::CONTEXT_OPENGL_RESET_NOTIFICATION_STRATEGY
550                                             as c_int);
551                     context_attributes.push(ffi::egl::LOSE_CONTEXT_ON_RESET as c_int);
552                     flags = flags | ffi::egl::CONTEXT_OPENGL_ROBUST_ACCESS as c_int;
553                 } else {
554                     return Err(CreationError::RobustnessNotSupported);
555                 }
556             },
557 
558             Robustness::TryRobustLoseContextOnReset => {
559                 if supports_robustness {
560                     context_attributes.push(ffi::egl::CONTEXT_OPENGL_RESET_NOTIFICATION_STRATEGY
561                                             as c_int);
562                     context_attributes.push(ffi::egl::LOSE_CONTEXT_ON_RESET as c_int);
563                     flags = flags | ffi::egl::CONTEXT_OPENGL_ROBUST_ACCESS as c_int;
564                 }
565             },
566         }
567 
568         if gl_debug {
569             if egl_version >= &(1, 5) {
570                 context_attributes.push(ffi::egl::CONTEXT_OPENGL_DEBUG as i32);
571                 context_attributes.push(ffi::egl::TRUE as i32);
572             }
573 
574             // TODO: using this flag sometimes generates an error
575             //       there was a change in the specs that added this flag, so it may not be
576             //       supported everywhere ; however it is not possible to know whether it is
577             //       supported or not
578             //flags = flags | ffi::egl::CONTEXT_OPENGL_DEBUG_BIT_KHR as i32;
579         }
580 
581         context_attributes.push(ffi::egl::CONTEXT_FLAGS_KHR as i32);
582         context_attributes.push(flags);
583 
584     } else if egl_version >= &(1, 3) && api == Api::OpenGlEs {
585         // robustness is not supported
586         match gl_robustness {
587             Robustness::RobustNoResetNotification | Robustness::RobustLoseContextOnReset => {
588                 return Err(CreationError::RobustnessNotSupported);
589             },
590             _ => ()
591         }
592 
593         context_attributes.push(ffi::egl::CONTEXT_CLIENT_VERSION as i32);
594         context_attributes.push(version.0 as i32);
595     }
596 
597     context_attributes.push(ffi::egl::NONE as i32);
598 
599     let context = egl::CreateContext(display, config_id, ptr::null(),
600                                     context_attributes.as_ptr());
601 
602     if context.is_null() {
603         match egl::GetError() as u32 {
604             ffi::egl::BAD_ATTRIBUTE => return Err(CreationError::OpenGlVersionNotSupported),
605             e => panic!("eglCreateContext failed: 0x{:x}", e),
606         }
607     }
608 
609     Ok(context)
610 }
611 
612