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
//! Everything related to `EGLContext` management.

use std::fmt;
use std::marker::PhantomData;
use std::ops::Deref;

use glutin_egl_sys::egl::types::{EGLenum, EGLint};
use glutin_egl_sys::{egl, EGLContext};

use crate::config::{Api, GetGlConfig};
use crate::context::{
    self, AsRawContext, ContextApi, ContextAttributes, GlProfile, RawContext, Robustness, Version,
};
use crate::display::{DisplayFeatures, GetGlDisplay};
use crate::error::{ErrorKind, Result};
use crate::prelude::*;
use crate::private::Sealed;
use crate::surface::SurfaceTypeTrait;

use super::config::Config;
use super::display::Display;
use super::surface::Surface;

impl Display {
    pub(crate) unsafe fn create_context(
        &self,
        config: &Config,
        context_attributes: &ContextAttributes,
    ) -> Result<NotCurrentContext> {
        let mut attrs = Vec::<EGLint>::new();

        let supports_opengl = self.inner.version > Version::new(1, 3);
        let config_api = config.api();

        let (api, mut version) = match context_attributes.api {
            api @ Some(ContextApi::OpenGl(_)) | api @ None
                if supports_opengl && config_api.contains(Api::OPENGL) =>
            {
                (egl::OPENGL_API, api.and_then(|api| api.version()))
            },
            api @ Some(ContextApi::Gles(_)) | api @ None => {
                let version = match api.and_then(|api| api.version()) {
                    Some(version) => version,
                    None if config_api.contains(Api::GLES3) => Version::new(3, 0),
                    None if config_api.contains(Api::GLES2) => Version::new(2, 0),
                    _ => Version::new(1, 0),
                };
                (egl::OPENGL_ES_API, Some(version))
            },
            _ => {
                return Err(
                    ErrorKind::NotSupported("the requested context Api isn't supported.").into()
                )
            },
        };

        let is_one_five = self.inner.version >= Version::new(1, 5);
        if is_one_five || self.inner.display_extensions.contains("EGL_KHR_create_context") {
            let mut flags = 0;

            // Add profile for the OpenGL Api.
            if api == egl::OPENGL_API {
                let (profile, new_version) =
                    context::pick_profile(context_attributes.profile, version);
                version = Some(new_version);
                let profile = match profile {
                    GlProfile::Core => egl::CONTEXT_OPENGL_CORE_PROFILE_BIT,
                    GlProfile::Compatibility => egl::CONTEXT_OPENGL_COMPATIBILITY_PROFILE_BIT,
                };

                attrs.push(egl::CONTEXT_OPENGL_PROFILE_MASK as EGLint);
                attrs.push(profile as EGLint);
            }

            if let Some(version) = version {
                attrs.push(egl::CONTEXT_MAJOR_VERSION as EGLint);
                attrs.push(version.major as EGLint);
                attrs.push(egl::CONTEXT_MINOR_VERSION as EGLint);
                attrs.push(version.minor as EGLint);
            }

            let has_robustsess = self.inner.features.contains(DisplayFeatures::CONTEXT_ROBUSTNESS);

            let mut requested_no_error = false;
            match context_attributes.robustness {
                Robustness::NotRobust => (),
                Robustness::NoError
                    if self.inner.features.contains(DisplayFeatures::CONTEXT_NO_ERROR) =>
                {
                    attrs.push(egl::CONTEXT_OPENGL_NO_ERROR_KHR as EGLint);
                    attrs.push(egl::TRUE as EGLint);
                    requested_no_error = true;
                },
                Robustness::RobustLoseContextOnReset if has_robustsess => {
                    attrs.push(egl::CONTEXT_OPENGL_RESET_NOTIFICATION_STRATEGY as EGLint);
                    attrs.push(egl::LOSE_CONTEXT_ON_RESET as EGLint);
                    flags |= egl::CONTEXT_OPENGL_ROBUST_ACCESS_BIT_KHR;
                },
                Robustness::RobustNoResetNotification if has_robustsess => {
                    attrs.push(egl::CONTEXT_OPENGL_RESET_NOTIFICATION_STRATEGY as EGLint);
                    attrs.push(egl::NO_RESET_NOTIFICATION as EGLint);
                    flags |= egl::CONTEXT_OPENGL_ROBUST_ACCESS_BIT_KHR;
                },
                _ => {
                    return Err(
                        ErrorKind::NotSupported("context robustness is not supported").into()
                    )
                },
            }

            if context_attributes.debug && is_one_five && !requested_no_error {
                attrs.push(egl::CONTEXT_OPENGL_DEBUG as EGLint);
                attrs.push(egl::TRUE as EGLint);
            }

            if flags != 0 {
                attrs.push(egl::CONTEXT_FLAGS_KHR as EGLint);
                attrs.push(flags as EGLint);
            }
        } else if self.inner.version >= Version::new(1, 3) {
            // EGL 1.3 uses that to indicate client version instead of major/minor. The
            // constant is the same as `CONTEXT_MAJOR_VERSION`.
            if let Some(version) = version {
                attrs.push(egl::CONTEXT_CLIENT_VERSION as EGLint);
                attrs.push(version.major as EGLint);
            }
        }

        attrs.push(egl::NONE as EGLint);

        let shared_context = if let Some(shared_context) =
            context_attributes.shared_context.as_ref()
        {
            match shared_context {
                RawContext::Egl(shared_context) => *shared_context,
                #[allow(unreachable_patterns)]
                _ => return Err(ErrorKind::NotSupported("passed incompatible raw context").into()),
            }
        } else {
            egl::NO_CONTEXT
        };

        // Bind the api.
        unsafe {
            if self.inner.egl.BindAPI(api) == egl::FALSE {
                return Err(super::check_error().err().unwrap());
            }

            let config = config.clone();
            let context = self.inner.egl.CreateContext(
                *self.inner.raw,
                *config.inner.raw,
                shared_context,
                attrs.as_ptr(),
            );

            if context == egl::NO_CONTEXT {
                return Err(super::check_error().err().unwrap());
            }

            let inner =
                ContextInner { display: self.clone(), config, raw: EglContext(context), api };
            Ok(NotCurrentContext::new(inner))
        }
    }
}

/// A wrapper around `EGLContext` that is known to be not current.
#[derive(Debug)]
pub struct NotCurrentContext {
    inner: ContextInner,
}

impl NotCurrentContext {
    /// Make a [`Self::PossiblyCurrentContext`] indicating that the context
    /// could be current on the thread.
    pub fn make_current_surfaceless(self) -> Result<PossiblyCurrentContext> {
        self.inner.make_current_surfaceless()?;
        Ok(PossiblyCurrentContext { inner: self.inner, _nosendsync: PhantomData })
    }

    fn new(inner: ContextInner) -> Self {
        Self { inner }
    }
}

impl NotCurrentGlContext for NotCurrentContext {
    type PossiblyCurrentContext = PossiblyCurrentContext;
    type Surface<T: SurfaceTypeTrait> = Surface<T>;

    fn treat_as_possibly_current(self) -> Self::PossiblyCurrentContext {
        PossiblyCurrentContext { inner: self.inner, _nosendsync: PhantomData }
    }

    fn make_current<T: SurfaceTypeTrait>(
        self,
        surface: &Surface<T>,
    ) -> Result<PossiblyCurrentContext> {
        self.inner.make_current_draw_read(surface, surface)?;
        Ok(PossiblyCurrentContext { inner: self.inner, _nosendsync: PhantomData })
    }

    fn make_current_draw_read<T: SurfaceTypeTrait>(
        self,
        surface_draw: &Surface<T>,
        surface_read: &Surface<T>,
    ) -> Result<PossiblyCurrentContext> {
        self.inner.make_current_draw_read(surface_draw, surface_read)?;
        Ok(PossiblyCurrentContext { inner: self.inner, _nosendsync: PhantomData })
    }
}

impl GlContext for NotCurrentContext {
    fn context_api(&self) -> ContextApi {
        self.inner.context_api()
    }
}

impl GetGlConfig for NotCurrentContext {
    type Target = Config;

    fn config(&self) -> Self::Target {
        self.inner.config.clone()
    }
}

impl GetGlDisplay for NotCurrentContext {
    type Target = Display;

    fn display(&self) -> Self::Target {
        self.inner.display.clone()
    }
}

impl AsRawContext for NotCurrentContext {
    fn raw_context(&self) -> RawContext {
        RawContext::Egl(*self.inner.raw)
    }
}

impl Sealed for NotCurrentContext {}

/// A wrapper around `EGLContext` that could be current for the current thread.
#[derive(Debug)]
pub struct PossiblyCurrentContext {
    pub(crate) inner: ContextInner,
    _nosendsync: PhantomData<EGLContext>,
}

impl PossiblyCurrentContext {
    /// Make this context current on the calling thread.
    pub fn make_current_surfaceless(&self) -> Result<()> {
        self.inner.make_current_surfaceless()
    }
}

impl PossiblyCurrentGlContext for PossiblyCurrentContext {
    type NotCurrentContext = NotCurrentContext;
    type Surface<T: SurfaceTypeTrait> = Surface<T>;

    fn make_not_current(self) -> Result<Self::NotCurrentContext> {
        self.inner.make_not_current()?;
        Ok(NotCurrentContext::new(self.inner))
    }

    fn is_current(&self) -> bool {
        unsafe {
            self.inner.bind_api();
            self.inner.display.inner.egl.GetCurrentContext() == *self.inner.raw
        }
    }

    fn make_current<T: SurfaceTypeTrait>(&self, surface: &Self::Surface<T>) -> Result<()> {
        self.inner.make_current_draw_read(surface, surface)
    }

    fn make_current_draw_read<T: SurfaceTypeTrait>(
        &self,
        surface_draw: &Self::Surface<T>,
        surface_read: &Self::Surface<T>,
    ) -> Result<()> {
        self.inner.make_current_draw_read(surface_draw, surface_read)
    }
}

impl GlContext for PossiblyCurrentContext {
    fn context_api(&self) -> ContextApi {
        self.inner.context_api()
    }
}

impl GetGlConfig for PossiblyCurrentContext {
    type Target = Config;

    fn config(&self) -> Self::Target {
        self.inner.config.clone()
    }
}

impl GetGlDisplay for PossiblyCurrentContext {
    type Target = Display;

    fn display(&self) -> Self::Target {
        self.inner.display.clone()
    }
}

impl AsRawContext for PossiblyCurrentContext {
    fn raw_context(&self) -> RawContext {
        RawContext::Egl(*self.inner.raw)
    }
}

impl Sealed for PossiblyCurrentContext {}

pub(crate) struct ContextInner {
    display: Display,
    config: Config,
    raw: EglContext,
    api: egl::types::EGLenum,
}

impl ContextInner {
    fn make_current_surfaceless(&self) -> Result<()> {
        unsafe {
            if self.display.inner.egl.MakeCurrent(
                *self.display.inner.raw,
                egl::NO_SURFACE,
                egl::NO_SURFACE,
                *self.raw,
            ) == egl::FALSE
            {
                super::check_error()
            } else {
                Ok(())
            }
        }
    }

    fn make_current_draw_read<T: SurfaceTypeTrait>(
        &self,
        surface_draw: &Surface<T>,
        surface_read: &Surface<T>,
    ) -> Result<()> {
        unsafe {
            let draw = surface_draw.raw;
            let read = surface_read.raw;
            if self.display.inner.egl.MakeCurrent(*self.display.inner.raw, draw, read, *self.raw)
                == egl::FALSE
            {
                super::check_error()
            } else {
                Ok(())
            }
        }
    }

    fn make_not_current(&self) -> Result<()> {
        unsafe {
            self.bind_api();

            if self.display.inner.egl.MakeCurrent(
                *self.display.inner.raw,
                egl::NO_SURFACE,
                egl::NO_SURFACE,
                egl::NO_CONTEXT,
            ) == egl::FALSE
            {
                super::check_error()
            } else {
                Ok(())
            }
        }
    }

    fn context_api(&self) -> ContextApi {
        match self.query_attribute(egl::CONTEXT_CLIENT_TYPE as EGLint).map(|a| a as EGLenum) {
            Some(egl::OPENGL_API) => ContextApi::OpenGl(None),
            // Map the rest to the GLES.
            _ => ContextApi::Gles(None),
        }
    }

    /// Query the context attribute.
    fn query_attribute(&self, attribute: EGLint) -> Option<EGLint> {
        unsafe {
            let mut attribute_value = 0;
            if self.display.inner.egl.QueryContext(
                self.display.inner.raw.cast(),
                self.raw.cast(),
                attribute,
                &mut attribute_value,
            ) == egl::FALSE
            {
                None
            } else {
                Some(attribute_value)
            }
        }
    }

    /// This function could panic, but it does that for sanity reasons.
    ///
    /// When we create context we bind api and then store it and rebind
    /// on functions requiring it, so if it fails it means that it worked
    /// before, but for some reason stopped working, which should not
    /// happen according to the specification.
    pub(crate) fn bind_api(&self) {
        unsafe {
            if self.display.inner.egl.QueryAPI() == self.api {
                return;
            }

            if self.display.inner.egl.BindAPI(self.api) == egl::FALSE {
                panic!("EGL Api couldn't be bound anymore.");
            }
        }
    }
}

impl Drop for ContextInner {
    fn drop(&mut self) {
        unsafe {
            self.display.inner.egl.DestroyContext(*self.display.inner.raw, *self.raw);
        }
    }
}

impl fmt::Debug for ContextInner {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("Context")
            .field("display", &self.display.inner.raw)
            .field("config", &self.config.inner.raw)
            .field("raw", &self.raw)
            .finish()
    }
}

#[derive(Debug)]
struct EglContext(EGLContext);

// Impl only `Send` for EglContext.
unsafe impl Send for EglContext {}

impl Deref for EglContext {
    type Target = EGLContext;

    fn deref(&self) -> &Self::Target {
        &self.0
    }
}