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
// surfman/surfman/src/platform/generic/egl/context.rs
//
//! Functionality common to backends using EGL contexts.

use super::device::EGL_FUNCTIONS;
use super::error::ToWindowingApiError;
use super::ffi::EGL_CONTEXT_OPENGL_PROFILE_MASK;
use super::ffi::{EGL_CONTEXT_MINOR_VERSION_KHR, EGL_CONTEXT_OPENGL_COMPATIBILITY_PROFILE_BIT};
use super::surface::{EGLBackedSurface, ExternalEGLSurfaces};
use crate::context::{self, CREATE_CONTEXT_MUTEX};
use crate::egl;
use crate::egl::types::{EGLConfig, EGLContext, EGLDisplay, EGLSurface, EGLint};
use crate::surface::Framebuffer;
use crate::{ContextAttributeFlags, ContextAttributes, ContextID, Error, GLApi, GLVersion};
use crate::{Gl, SurfaceInfo};

use std::ffi::CString;
use std::mem;
use std::os::raw::c_void;
use std::ptr;
use std::thread;

#[allow(dead_code)]
const DUMMY_PBUFFER_SIZE: EGLint = 16;
const RGB_CHANNEL_BIT_DEPTH: EGLint = 8;

pub(crate) struct EGLBackedContext {
    pub(crate) egl_context: EGLContext,
    pub(crate) id: ContextID,
    framebuffer: Framebuffer<EGLBackedSurface, ExternalEGLSurfaces>,
    context_is_owned: bool,
}

/// Wrapper for a native `EGLContext`.
#[derive(Clone, Copy)]
pub struct NativeContext {
    /// The EGL context.
    pub egl_context: EGLContext,
    /// The EGL read surface that is to be attached to that context.
    pub egl_read_surface: EGLSurface,
    /// The EGL draw surface that is to be attached to that context.
    pub egl_draw_surface: EGLSurface,
}

/// Information needed to create a context. Some APIs call this a "config" or a "pixel format".
///
/// These are local to a device.
#[derive(Clone)]
pub struct ContextDescriptor {
    pub(crate) egl_config_id: EGLint,
    pub(crate) gl_version: GLVersion,
    pub(crate) compatibility_profile: bool,
}

#[must_use]
pub(crate) struct CurrentContextGuard {
    egl_display: EGLDisplay,
    old_egl_draw_surface: EGLSurface,
    old_egl_read_surface: EGLSurface,
    old_egl_context: EGLContext,
}

impl Drop for EGLBackedContext {
    #[inline]
    fn drop(&mut self) {
        if self.egl_context != egl::NO_CONTEXT && !thread::panicking() {
            panic!("Contexts must be destroyed explicitly with `destroy_context`!")
        }
    }
}

impl Drop for CurrentContextGuard {
    fn drop(&mut self) {
        EGL_FUNCTIONS.with(|egl| unsafe {
            if self.egl_display != egl::NO_DISPLAY {
                egl.MakeCurrent(
                    self.egl_display,
                    self.old_egl_draw_surface,
                    self.old_egl_read_surface,
                    self.old_egl_context,
                );
            }
        })
    }
}

impl EGLBackedContext {
    pub(crate) unsafe fn new(
        egl_display: EGLDisplay,
        descriptor: &ContextDescriptor,
        share_with: Option<&EGLBackedContext>,
        gl_api: GLApi,
    ) -> Result<EGLBackedContext, Error> {
        let mut next_context_id = CREATE_CONTEXT_MUTEX.lock().unwrap();

        // Create the context.
        let egl_context = create_context(
            egl_display,
            descriptor,
            share_with.map_or(egl::NO_CONTEXT, |ctx| ctx.egl_context),
            gl_api,
        )?;

        // Wrap and return it.
        let context = EGLBackedContext {
            egl_context,
            id: *next_context_id,
            framebuffer: Framebuffer::None,
            context_is_owned: true,
        };
        next_context_id.0 += 1;
        Ok(context)
    }

    pub(crate) unsafe fn from_native_context(native_context: NativeContext) -> EGLBackedContext {
        let mut next_context_id = CREATE_CONTEXT_MUTEX.lock().unwrap();
        let context = EGLBackedContext {
            egl_context: native_context.egl_context,
            id: *next_context_id,
            framebuffer: Framebuffer::External(ExternalEGLSurfaces {
                draw: native_context.egl_draw_surface,
                read: native_context.egl_read_surface,
            }),
            context_is_owned: false,
        };
        next_context_id.0 += 1;
        context
    }

    pub(crate) unsafe fn destroy(&mut self, egl_display: EGLDisplay) {
        EGL_FUNCTIONS.with(|egl| {
            egl.MakeCurrent(
                egl_display,
                egl::NO_SURFACE,
                egl::NO_SURFACE,
                egl::NO_CONTEXT,
            );

            if self.context_is_owned {
                let result = egl.DestroyContext(egl_display, self.egl_context);
                assert_ne!(result, egl::FALSE);
            }

            self.egl_context = egl::NO_CONTEXT;
        });
    }

    pub(crate) fn native_context(&self) -> NativeContext {
        let egl_surfaces = match self.framebuffer {
            Framebuffer::Surface(ref surface) => surface.egl_surfaces(),
            Framebuffer::External(ref surfaces) => (*surfaces).clone(),
            Framebuffer::None => ExternalEGLSurfaces::default(),
        };

        NativeContext {
            egl_context: self.egl_context,
            egl_draw_surface: egl_surfaces.draw,
            egl_read_surface: egl_surfaces.read,
        }
    }

    pub(crate) unsafe fn make_current(&self, egl_display: EGLDisplay) -> Result<(), Error> {
        let egl_surfaces = match self.framebuffer {
            Framebuffer::Surface(ref surface) => surface.egl_surfaces(),
            Framebuffer::External(ref surfaces) => (*surfaces).clone(),
            Framebuffer::None => ExternalEGLSurfaces::default(),
        };

        EGL_FUNCTIONS.with(|egl| {
            let result = egl.MakeCurrent(
                egl_display,
                egl_surfaces.draw,
                egl_surfaces.read,
                self.egl_context,
            );
            if result == egl::FALSE {
                let err = egl.GetError().to_windowing_api_error();
                return Err(Error::MakeCurrentFailed(err));
            }
            Ok(())
        })
    }

    #[inline]
    pub(crate) fn is_current(&self) -> bool {
        unsafe { EGL_FUNCTIONS.with(|egl| egl.GetCurrentContext() == self.egl_context) }
    }

    pub(crate) unsafe fn bind_surface(
        &mut self,
        egl_display: EGLDisplay,
        surface: EGLBackedSurface,
    ) -> Result<(), (Error, EGLBackedSurface)> {
        if self.id != surface.context_id {
            return Err((Error::IncompatibleSurface, surface));
        }

        match self.framebuffer {
            Framebuffer::None => self.framebuffer = Framebuffer::Surface(surface),
            Framebuffer::External(_) => return Err((Error::ExternalRenderTarget, surface)),
            Framebuffer::Surface(_) => return Err((Error::SurfaceAlreadyBound, surface)),
        }

        // If we're current, call `make_context_current()` again to switch to the new framebuffer.
        if self.is_current() {
            drop(self.make_current(egl_display))
        }

        Ok(())
    }

    pub(crate) unsafe fn unbind_surface(
        &mut self,
        gl: &Gl,
        egl_display: EGLDisplay,
    ) -> Result<Option<EGLBackedSurface>, Error> {
        match self.framebuffer {
            Framebuffer::None => return Ok(None),
            Framebuffer::Surface(_) => {}
            Framebuffer::External(_) => return Err(Error::ExternalRenderTarget),
        }

        let surface = match mem::replace(&mut self.framebuffer, Framebuffer::None) {
            Framebuffer::Surface(surface) => surface,
            Framebuffer::None | Framebuffer::External(_) => unreachable!(),
        };

        // If we're current, we stay current, but with no surface attached.
        surface.unbind(gl, egl_display, self.egl_context);

        Ok(Some(surface))
    }

    pub(crate) fn surface_info(&self) -> Result<Option<SurfaceInfo>, Error> {
        match self.framebuffer {
            Framebuffer::None => Ok(None),
            Framebuffer::External(_) => Err(Error::ExternalRenderTarget),
            Framebuffer::Surface(ref surface) => Ok(Some(surface.info())),
        }
    }
}

impl NativeContext {
    /// Returns the current EGL context and surfaces, if applicable.
    ///
    /// If there is no current EGL context, this returns a `NoCurrentContext` error.
    pub fn current() -> Result<NativeContext, Error> {
        EGL_FUNCTIONS.with(|egl| unsafe {
            let egl_context = egl.GetCurrentContext();
            if egl_context == egl::NO_CONTEXT {
                Err(Error::NoCurrentContext)
            } else {
                Ok(NativeContext {
                    egl_context,
                    egl_read_surface: egl.GetCurrentSurface(egl::READ as EGLint),
                    egl_draw_surface: egl.GetCurrentSurface(egl::DRAW as EGLint),
                })
            }
        })
    }
}

impl ContextDescriptor {
    pub(crate) unsafe fn new(
        egl_display: EGLDisplay,
        attributes: &ContextAttributes,
        extra_config_attributes: &[EGLint],
    ) -> Result<ContextDescriptor, Error> {
        let flags = attributes.flags;

        let alpha_size = if flags.contains(ContextAttributeFlags::ALPHA) {
            8
        } else {
            0
        };
        let depth_size = if flags.contains(ContextAttributeFlags::DEPTH) {
            24
        } else {
            0
        };
        let stencil_size = if flags.contains(ContextAttributeFlags::STENCIL) {
            8
        } else {
            0
        };

        let compatibility_profile = flags.contains(ContextAttributeFlags::COMPATIBILITY_PROFILE);

        // Mesa doesn't support the OpenGL compatibility profile post version 3.0. Take that into
        // account.
        if compatibility_profile
            && (attributes.version.major > 3
                || attributes.version.major == 3 && attributes.version.minor > 0)
        {
            return Err(Error::UnsupportedGLProfile);
        }

        // Create required config attributes.
        //
        // We check these separately because `eglChooseConfig` on its own might give us 32-bit
        // color when 24-bit color is requested, and that can break code.
        let required_config_attributes = [
            egl::RED_SIZE as EGLint,
            RGB_CHANNEL_BIT_DEPTH,
            egl::GREEN_SIZE as EGLint,
            RGB_CHANNEL_BIT_DEPTH,
            egl::BLUE_SIZE as EGLint,
            RGB_CHANNEL_BIT_DEPTH,
        ];

        // Create config attributes.
        let mut requested_config_attributes = required_config_attributes.to_vec();
        requested_config_attributes.extend_from_slice(&[
            egl::ALPHA_SIZE as EGLint,
            alpha_size,
            egl::DEPTH_SIZE as EGLint,
            depth_size,
            egl::STENCIL_SIZE as EGLint,
            stencil_size,
        ]);
        requested_config_attributes.extend_from_slice(extra_config_attributes);
        requested_config_attributes.extend_from_slice(&[egl::NONE as EGLint, 0, 0, 0]);

        EGL_FUNCTIONS.with(|egl| {
            // See how many applicable configs there are.
            let mut config_count = 0;
            let result = egl.ChooseConfig(
                egl_display,
                requested_config_attributes.as_ptr(),
                ptr::null_mut(),
                0,
                &mut config_count,
            );
            if result == egl::FALSE {
                let err = egl.GetError().to_windowing_api_error();
                return Err(Error::PixelFormatSelectionFailed(err));
            }
            if config_count == 0 {
                return Err(Error::NoPixelFormatFound);
            }

            // Enumerate all those configs.
            let mut configs = vec![ptr::null(); config_count as usize];
            let mut real_config_count = config_count;
            let result = egl.ChooseConfig(
                egl_display,
                requested_config_attributes.as_ptr(),
                configs.as_mut_ptr(),
                config_count,
                &mut real_config_count,
            );
            if result == egl::FALSE {
                let err = egl.GetError().to_windowing_api_error();
                return Err(Error::PixelFormatSelectionFailed(err));
            }

            // Sanitize configs.
            let egl_config = configs.into_iter().find(|&egl_config| {
                required_config_attributes
                    .chunks(2)
                    .all(|pair| get_config_attr(egl_display, egl_config, pair[0]) == pair[1])
            });
            let egl_config = match egl_config {
                None => return Err(Error::NoPixelFormatFound),
                Some(egl_config) => egl_config,
            };

            // Get the config ID and version.
            let egl_config_id = get_config_attr(egl_display, egl_config, egl::CONFIG_ID as EGLint);
            let gl_version = attributes.version;

            Ok(ContextDescriptor {
                egl_config_id,
                gl_version,
                compatibility_profile,
            })
        })
    }

    pub(crate) unsafe fn from_egl_context(
        gl: &Gl,
        egl_display: EGLDisplay,
        egl_context: EGLContext,
    ) -> ContextDescriptor {
        let egl_config_id = get_context_attr(egl_display, egl_context, egl::CONFIG_ID as EGLint);

        EGL_FUNCTIONS.with(|egl| {
            let _guard = CurrentContextGuard::new();
            egl.MakeCurrent(egl_display, egl::NO_SURFACE, egl::NO_SURFACE, egl_context);
            let gl_version = GLVersion::current(gl);
            let compatibility_profile = context::current_context_uses_compatibility_profile(gl);

            ContextDescriptor {
                egl_config_id,
                gl_version,
                compatibility_profile,
            }
        })
    }

    #[allow(dead_code)]
    pub(crate) unsafe fn to_egl_config(&self, egl_display: EGLDisplay) -> EGLConfig {
        let config_attributes = [
            egl::CONFIG_ID as EGLint,
            self.egl_config_id,
            egl::NONE as EGLint,
            0,
            0,
            0,
        ];

        EGL_FUNCTIONS.with(|egl| {
            let (mut config, mut config_count) = (ptr::null(), 0);
            let result = egl.ChooseConfig(
                egl_display,
                config_attributes.as_ptr(),
                &mut config,
                1,
                &mut config_count,
            );
            assert_ne!(result, egl::FALSE);
            assert!(config_count > 0);
            config
        })
    }

    pub(crate) unsafe fn attributes(&self, egl_display: EGLDisplay) -> ContextAttributes {
        let egl_config = egl_config_from_id(egl_display, self.egl_config_id);

        let alpha_size = get_config_attr(egl_display, egl_config, egl::ALPHA_SIZE as EGLint);
        let depth_size = get_config_attr(egl_display, egl_config, egl::DEPTH_SIZE as EGLint);
        let stencil_size = get_config_attr(egl_display, egl_config, egl::STENCIL_SIZE as EGLint);

        // Convert to `surfman` context attribute flags.
        let mut attribute_flags = ContextAttributeFlags::empty();
        attribute_flags.set(ContextAttributeFlags::ALPHA, alpha_size != 0);
        attribute_flags.set(ContextAttributeFlags::DEPTH, depth_size != 0);
        attribute_flags.set(ContextAttributeFlags::STENCIL, stencil_size != 0);

        attribute_flags.set(
            ContextAttributeFlags::COMPATIBILITY_PROFILE,
            self.compatibility_profile,
        );

        // Create appropriate context attributes.
        ContextAttributes {
            flags: attribute_flags,
            version: self.gl_version,
        }
    }
}

impl CurrentContextGuard {
    pub(crate) fn new() -> CurrentContextGuard {
        EGL_FUNCTIONS.with(|egl| unsafe {
            CurrentContextGuard {
                egl_display: egl.GetCurrentDisplay(),
                old_egl_draw_surface: egl.GetCurrentSurface(egl::DRAW as EGLint),
                old_egl_read_surface: egl.GetCurrentSurface(egl::READ as EGLint),
                old_egl_context: egl.GetCurrentContext(),
            }
        })
    }
}

pub(crate) unsafe fn create_context(
    egl_display: EGLDisplay,
    descriptor: &ContextDescriptor,
    share_with: EGLContext,
    gl_api: GLApi,
) -> Result<EGLContext, Error> {
    EGL_FUNCTIONS.with(|egl| {
        let ok = egl.BindAPI(match gl_api {
            GLApi::GL => egl::OPENGL_API,
            GLApi::GLES => egl::OPENGL_ES_API,
        });
        assert_ne!(ok, egl::FALSE);
    });

    let egl_config = egl_config_from_id(egl_display, descriptor.egl_config_id);

    let mut egl_context_attributes = vec![
        egl::CONTEXT_CLIENT_VERSION as EGLint,
        descriptor.gl_version.major as EGLint,
        EGL_CONTEXT_MINOR_VERSION_KHR as EGLint,
        descriptor.gl_version.minor as EGLint,
    ];

    // D3D11 ANGLE doesn't seem happy if EGL_CONTEXT_OPENGL_PROFILE_MASK is set
    // to be a core profile.
    if descriptor.compatibility_profile {
        egl_context_attributes.extend(&[
            EGL_CONTEXT_OPENGL_PROFILE_MASK as EGLint,
            EGL_CONTEXT_OPENGL_COMPATIBILITY_PROFILE_BIT,
        ]);
    }

    // Include some extra zeroes to work around broken implementations.
    //
    // FIXME(pcwalton): Which implementations are those? (This is copied from Gecko.)
    egl_context_attributes.extend(&[egl::NONE as EGLint, 0, 0, 0]);

    EGL_FUNCTIONS.with(|egl| {
        let egl_context = egl.CreateContext(
            egl_display,
            egl_config,
            share_with,
            egl_context_attributes.as_ptr(),
        );
        if egl_context == egl::NO_CONTEXT {
            let err = egl.GetError();
            let err = err.to_windowing_api_error();
            return Err(Error::ContextCreationFailed(err));
        }

        Ok(egl_context)
    })
}

pub(crate) unsafe fn make_no_context_current(egl_display: EGLDisplay) -> Result<(), Error> {
    EGL_FUNCTIONS.with(|egl| {
        let result = egl.MakeCurrent(
            egl_display,
            egl::NO_SURFACE,
            egl::NO_SURFACE,
            egl::NO_CONTEXT,
        );
        if result == egl::FALSE {
            let err = egl.GetError().to_windowing_api_error();
            return Err(Error::MakeCurrentFailed(err));
        }
        Ok(())
    })
}

pub(crate) unsafe fn get_config_attr(
    egl_display: EGLDisplay,
    egl_config: EGLConfig,
    attr: EGLint,
) -> EGLint {
    EGL_FUNCTIONS.with(|egl| {
        let mut value = 0;
        let result = egl.GetConfigAttrib(egl_display, egl_config, attr, &mut value);
        assert_ne!(result, egl::FALSE);
        value
    })
}

pub(crate) unsafe fn get_context_attr(
    egl_display: EGLDisplay,
    egl_context: EGLContext,
    attr: EGLint,
) -> EGLint {
    EGL_FUNCTIONS.with(|egl| {
        let mut value = 0;
        let result = egl.QueryContext(egl_display, egl_context, attr, &mut value);
        assert_ne!(result, egl::FALSE);
        value
    })
}

pub(crate) unsafe fn egl_config_from_id(
    egl_display: EGLDisplay,
    egl_config_id: EGLint,
) -> EGLConfig {
    let config_attributes = [
        egl::CONFIG_ID as EGLint,
        egl_config_id,
        egl::NONE as EGLint,
        0,
        0,
        0,
    ];

    EGL_FUNCTIONS.with(|egl| {
        let (mut config, mut config_count) = (ptr::null(), 0);
        let result = egl.ChooseConfig(
            egl_display,
            config_attributes.as_ptr(),
            &mut config,
            1,
            &mut config_count,
        );
        assert_ne!(result, egl::FALSE);
        assert!(config_count > 0);
        config
    })
}

pub(crate) fn get_proc_address(symbol_name: &str) -> *const c_void {
    EGL_FUNCTIONS.with(|egl| unsafe {
        let symbol_name: CString = CString::new(symbol_name).unwrap();
        egl.GetProcAddress(symbol_name.as_ptr()).cast()
    })
}

// Creates and returns a dummy pbuffer surface for the given context. This is used as the default
// framebuffer on some backends.
#[allow(dead_code)]
pub(crate) unsafe fn create_dummy_pbuffer(
    egl_display: EGLDisplay,
    egl_context: EGLContext,
) -> EGLSurface {
    let egl_config_id = get_context_attr(egl_display, egl_context, egl::CONFIG_ID as EGLint);
    let egl_config = egl_config_from_id(egl_display, egl_config_id);

    let pbuffer_attributes = [
        egl::WIDTH as EGLint,
        DUMMY_PBUFFER_SIZE,
        egl::HEIGHT as EGLint,
        DUMMY_PBUFFER_SIZE,
        egl::NONE as EGLint,
        0,
        0,
        0,
    ];

    EGL_FUNCTIONS.with(|egl| {
        let pbuffer =
            egl.CreatePbufferSurface(egl_display, egl_config, pbuffer_attributes.as_ptr());
        assert_ne!(pbuffer, egl::NO_SURFACE);
        pbuffer
    })
}