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
//! Everything related to finding and manipulating the `GLXFBConfig`.

use std::ops::Deref;
use std::os::raw::c_int;
use std::sync::Arc;
use std::{fmt, slice};

use glutin_glx_sys::glx::types::GLXFBConfig;
use glutin_glx_sys::{glx, glx_extra};
use raw_window_handle::RawWindowHandle;

use crate::config::{
    Api, AsRawConfig, ColorBufferType, ConfigSurfaceTypes, ConfigTemplate, GlConfig, RawConfig,
};
use crate::display::{DisplayFeatures, GetGlDisplay};
use crate::error::{ErrorKind, Result};
use crate::platform::x11::{X11GlConfigExt, X11VisualInfo, XLIB};
use crate::private::Sealed;

use super::display::Display;

impl Display {
    pub(crate) unsafe fn find_configs(
        &self,
        template: ConfigTemplate,
    ) -> Result<Box<dyn Iterator<Item = Config> + '_>> {
        let mut config_attributes = Vec::<c_int>::new();

        // Add color buffer type.
        match template.color_buffer_type {
            ColorBufferType::Rgb { r_size, g_size, b_size } => {
                // Type.
                config_attributes.push(glx::X_VISUAL_TYPE as c_int);
                config_attributes.push(glx::TRUE_COLOR as c_int);

                // R.
                config_attributes.push(glx::RED_SIZE as c_int);
                config_attributes.push(r_size as c_int);

                // G.
                config_attributes.push(glx::GREEN_SIZE as c_int);
                config_attributes.push(g_size as c_int);

                // B.
                config_attributes.push(glx::BLUE_SIZE as c_int);
                config_attributes.push(b_size as c_int);
            },
            ColorBufferType::Luminance(luminance) => {
                // Type.
                config_attributes.push(glx::X_VISUAL_TYPE as c_int);
                config_attributes.push(glx::GRAY_SCALE as c_int);

                // L.
                config_attributes.push(glx::RED_SIZE as c_int);
                config_attributes.push(luminance as c_int);
            },
        };

        // Render type.
        config_attributes.push(glx::RENDER_TYPE as c_int);

        if template.float_pixels
            && self.inner.features.contains(DisplayFeatures::FLOAT_PIXEL_FORMAT)
        {
            config_attributes.push(glx_extra::RGBA_FLOAT_BIT_ARB as c_int);
        } else if template.float_pixels {
            return Err(ErrorKind::NotSupported("float pixels are not supported").into());
        } else {
            config_attributes.push(glx::RGBA_BIT as c_int);
        }

        // Add caveat.
        if let Some(hardware_accelerated) = template.hardware_accelerated {
            config_attributes.push(glx::CONFIG_CAVEAT as c_int);
            if hardware_accelerated {
                config_attributes.push(glx::NONE as c_int);
            } else {
                config_attributes.push(glx::SLOW_CONFIG as c_int);
            }
        }

        // Double buffer.
        config_attributes.push(glx::DOUBLEBUFFER as c_int);
        config_attributes.push(!template.single_buffering as c_int);

        // Add alpha.
        config_attributes.push(glx::ALPHA_SIZE as c_int);
        config_attributes.push(template.alpha_size as c_int);

        // Add depth.
        config_attributes.push(glx::DEPTH_SIZE as c_int);
        config_attributes.push(template.depth_size as c_int);

        // Add stencil.
        config_attributes.push(glx::STENCIL_SIZE as c_int);
        config_attributes.push(template.stencil_size as c_int);

        // Add visual if was provided.
        if let Some(RawWindowHandle::Xlib(window)) = template.native_window {
            if window.visual_id > 0 {
                config_attributes.push(glx::VISUAL_ID as c_int);
                config_attributes.push(window.visual_id as c_int);
            }
        }

        // Add surface type.
        config_attributes.push(glx::DRAWABLE_TYPE as c_int);
        let mut surface_type = 0;
        if template.config_surface_types.contains(ConfigSurfaceTypes::WINDOW) {
            surface_type |= glx::WINDOW_BIT;
        }
        if template.config_surface_types.contains(ConfigSurfaceTypes::PBUFFER) {
            surface_type |= glx::PBUFFER_BIT;
        }
        if template.config_surface_types.contains(ConfigSurfaceTypes::PIXMAP) {
            surface_type |= glx::PIXMAP_BIT;
        }
        config_attributes.push(surface_type as c_int);

        // Add maximum height of pbuffer.
        if let Some(pbuffer_width) = template.max_pbuffer_width {
            config_attributes.push(glx::MAX_PBUFFER_WIDTH as c_int);
            config_attributes.push(pbuffer_width as c_int);
        }

        // Add maximum width of pbuffer.
        if let Some(pbuffer_height) = template.max_pbuffer_height {
            config_attributes.push(glx::MAX_PBUFFER_HEIGHT as c_int);
            config_attributes.push(pbuffer_height as c_int);
        }

        // Add stereoscopy, if present.
        if let Some(stereoscopy) = template.stereoscopy {
            config_attributes.push(glx::STEREO as c_int);
            config_attributes.push(stereoscopy as c_int);
        }

        // Add multisampling.
        if let Some(num_samples) = template.num_samples {
            if self.inner.features.contains(DisplayFeatures::MULTISAMPLING_PIXEL_FORMATS) {
                config_attributes.push(glx::SAMPLE_BUFFERS as c_int);
                config_attributes.push(1);
                config_attributes.push(glx::SAMPLES as c_int);
                config_attributes.push(num_samples as c_int);
            }
        }

        // Push X11 `None` to terminate the list.
        config_attributes.push(0);

        unsafe {
            let mut num_configs = 0;
            let raw_configs = self.inner.glx.ChooseFBConfig(
                self.inner.raw.cast(),
                self.inner.screen as _,
                config_attributes.as_ptr() as *const _,
                &mut num_configs,
            );

            if raw_configs.is_null() {
                return Err(ErrorKind::BadConfig.into());
            }

            let configs = slice::from_raw_parts_mut(raw_configs, num_configs as usize).to_vec();

            // Free the memory from the Xlib, since we've just copied it.
            (XLIB.as_ref().unwrap().XFree)(raw_configs as *mut _);

            let iter = configs
                .into_iter()
                .map(move |raw| {
                    let raw = GlxConfig(raw);
                    let inner = Arc::new(ConfigInner { display: self.clone(), raw });
                    Config { inner }
                })
                .filter(move |config| {
                    !template.transparency || config.supports_transparency().unwrap_or(false)
                });

            Ok(Box::new(iter))
        }
    }
}

/// A wrapper around `GLXFBConfig`.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Config {
    pub(crate) inner: Arc<ConfigInner>,
}

impl Config {
    /// # Safety
    ///
    /// The caller must ensure that the attribute could be present.
    unsafe fn raw_attribute(&self, attr: c_int) -> c_int {
        unsafe {
            let mut val = 0;
            self.inner.display.inner.glx.GetFBConfigAttrib(
                self.inner.display.inner.raw.cast(),
                *self.inner.raw,
                attr,
                &mut val,
            );
            val as c_int
        }
    }

    pub(crate) fn is_single_buffered(&self) -> bool {
        unsafe { self.raw_attribute(glx::DOUBLEBUFFER as c_int) == 0 }
    }
}

impl GlConfig for Config {
    fn color_buffer_type(&self) -> Option<ColorBufferType> {
        unsafe {
            match self.raw_attribute(glx::X_VISUAL_TYPE as c_int) as _ {
                glx::TRUE_COLOR => {
                    let r_size = self.raw_attribute(glx::RED_SIZE as c_int) as u8;
                    let g_size = self.raw_attribute(glx::GREEN_SIZE as c_int) as u8;
                    let b_size = self.raw_attribute(glx::BLUE_SIZE as c_int) as u8;
                    Some(ColorBufferType::Rgb { r_size, g_size, b_size })
                },
                glx::GRAY_SCALE => {
                    let luma = self.raw_attribute(glx::RED_SIZE as c_int);
                    Some(ColorBufferType::Luminance(luma as u8))
                },
                _ => None,
            }
        }
    }

    fn float_pixels(&self) -> bool {
        if self.inner.display.inner.features.contains(DisplayFeatures::FLOAT_PIXEL_FORMAT) {
            let render_type =
                unsafe { self.raw_attribute(glx::RENDER_TYPE as c_int) as glx::types::GLenum };
            render_type == glx_extra::RGBA_FLOAT_BIT_ARB
        } else {
            false
        }
    }

    fn alpha_size(&self) -> u8 {
        unsafe { self.raw_attribute(glx::ALPHA_SIZE as c_int) as u8 }
    }

    fn hardware_accelerated(&self) -> bool {
        unsafe { self.raw_attribute(glx::CONFIG_CAVEAT as c_int) != glx::SLOW_CONFIG as c_int }
    }

    fn srgb_capable(&self) -> bool {
        if self.inner.display.inner.client_extensions.contains("GLX_ARB_framebuffer_sRGB") {
            unsafe { self.raw_attribute(glx_extra::FRAMEBUFFER_SRGB_CAPABLE_ARB as c_int) != 0 }
        } else if self.inner.display.inner.client_extensions.contains("GLX_EXT_framebuffer_sRGB") {
            unsafe { self.raw_attribute(glx_extra::FRAMEBUFFER_SRGB_CAPABLE_EXT as c_int) != 0 }
        } else {
            false
        }
    }

    fn depth_size(&self) -> u8 {
        unsafe { self.raw_attribute(glx::DEPTH_SIZE as c_int) as u8 }
    }

    fn stencil_size(&self) -> u8 {
        unsafe { self.raw_attribute(glx::STENCIL_SIZE as c_int) as u8 }
    }

    fn num_samples(&self) -> u8 {
        unsafe { self.raw_attribute(glx::SAMPLES as c_int) as u8 }
    }

    fn config_surface_types(&self) -> ConfigSurfaceTypes {
        let mut ty = ConfigSurfaceTypes::empty();

        let raw_ty = unsafe { self.raw_attribute(glx::DRAWABLE_TYPE as c_int) as u32 };
        if raw_ty & glx::WINDOW_BIT as u32 != 0 {
            ty.insert(ConfigSurfaceTypes::WINDOW);
        }
        if raw_ty & glx::PBUFFER_BIT as u32 != 0 {
            ty.insert(ConfigSurfaceTypes::PBUFFER);
        }
        if raw_ty & glx::PIXMAP_BIT as u32 != 0 {
            ty.insert(ConfigSurfaceTypes::PIXMAP);
        }

        ty
    }

    fn supports_transparency(&self) -> Option<bool> {
        self.x11_visual().map(|visual| visual.supports_transparency())
    }

    fn api(&self) -> Api {
        let mut api = Api::OPENGL;
        if self.inner.display.inner.features.contains(DisplayFeatures::CREATE_ES_CONTEXT) {
            api |= Api::GLES1 | Api::GLES2;
        }

        api
    }
}

impl X11GlConfigExt for Config {
    fn x11_visual(&self) -> Option<X11VisualInfo> {
        unsafe {
            let raw_visual = self
                .inner
                .display
                .inner
                .glx
                .GetVisualFromFBConfig(self.inner.display.inner.raw.cast(), *self.inner.raw);
            if raw_visual.is_null() {
                None
            } else {
                Some(X11VisualInfo::from_raw(
                    self.inner.display.inner.raw.cast(),
                    raw_visual as *mut _,
                ))
            }
        }
    }
}

impl GetGlDisplay for Config {
    type Target = Display;

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

impl AsRawConfig for Config {
    fn raw_config(&self) -> RawConfig {
        RawConfig::Glx(*self.inner.raw)
    }
}

impl Sealed for Config {}

pub(crate) struct ConfigInner {
    display: Display,
    pub(crate) raw: GlxConfig,
}

impl PartialEq for ConfigInner {
    fn eq(&self, other: &Self) -> bool {
        self.raw == other.raw
    }
}

impl Eq for ConfigInner {}

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

#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) struct GlxConfig(GLXFBConfig);

unsafe impl Send for GlxConfig {}
unsafe impl Sync for GlxConfig {}

impl Deref for GlxConfig {
    type Target = GLXFBConfig;

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