glutin/api/glx/
surface.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
//! Everything related to the GLXWindow.

use std::fmt;
use std::marker::PhantomData;
use std::num::NonZeroU32;
use std::os::raw::{c_int, c_uint};

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

use crate::config::GetGlConfig;
use crate::display::{DisplayFeatures, GetGlDisplay};
use crate::error::{ErrorKind, Result};
use crate::private::Sealed;
use crate::surface::{
    AsRawSurface, GlSurface, NativePixmap, PbufferSurface, PixmapSurface, RawSurface,
    SurfaceAttributes, SurfaceType, SurfaceTypeTrait, SwapInterval, WindowSurface,
};

use super::config::Config;
use super::context::PossiblyCurrentContext;
use super::display::Display;

/// Hint for the attributes array.
const ATTR_SIZE_HINT: usize = 8;

impl Display {
    pub(crate) unsafe fn create_pixmap_surface(
        &self,
        config: &Config,
        surface_attributes: &SurfaceAttributes<PixmapSurface>,
    ) -> Result<Surface<PixmapSurface>> {
        let native_pixmap = surface_attributes.native_pixmap.as_ref().unwrap();
        let xid = match native_pixmap {
            NativePixmap::XlibPixmap(xid) => {
                if *xid == 0 {
                    return Err(ErrorKind::BadNativePixmap.into());
                }

                *xid
            },
            _ => {
                return Err(
                    ErrorKind::NotSupported("provided native pixmap is not supported.").into()
                )
            },
        };

        let mut attrs = Vec::<c_int>::with_capacity(ATTR_SIZE_HINT);

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

        let config = config.clone();
        let surface = super::last_glx_error(|| unsafe {
            self.inner.glx.CreatePixmap(
                self.inner.raw.cast(),
                *config.inner.raw,
                xid,
                attrs.as_ptr(),
            )
        })?;

        Ok(Surface {
            display: self.clone(),
            config,
            raw: surface,
            _nosendsync: PhantomData,
            _ty: PhantomData,
        })
    }

    pub(crate) unsafe fn create_pbuffer_surface(
        &self,
        config: &Config,
        surface_attributes: &SurfaceAttributes<PbufferSurface>,
    ) -> Result<Surface<PbufferSurface>> {
        let width = surface_attributes.width.unwrap();
        let height = surface_attributes.height.unwrap();

        let mut attrs = Vec::<c_int>::with_capacity(ATTR_SIZE_HINT);

        attrs.push(glx::PBUFFER_WIDTH as c_int);
        attrs.push(width.get() as c_int);
        attrs.push(glx::PBUFFER_HEIGHT as c_int);
        attrs.push(height.get() as c_int);
        attrs.push(glx::LARGEST_PBUFFER as c_int);
        attrs.push(surface_attributes.largest_pbuffer as c_int);

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

        let config = config.clone();
        let surface = super::last_glx_error(|| unsafe {
            self.inner.glx.CreatePbuffer(self.inner.raw.cast(), *config.inner.raw, attrs.as_ptr())
        })?;

        Ok(Surface {
            display: self.clone(),
            config,
            raw: surface,
            _nosendsync: PhantomData,
            _ty: PhantomData,
        })
    }

    pub(crate) unsafe fn create_window_surface(
        &self,
        config: &Config,
        surface_attributes: &SurfaceAttributes<WindowSurface>,
    ) -> Result<Surface<WindowSurface>> {
        let window = match surface_attributes.raw_window_handle.unwrap() {
            RawWindowHandle::Xlib(window_handle) => {
                if window_handle.window == 0 {
                    return Err(ErrorKind::BadNativeWindow.into());
                }

                window_handle.window
            },
            _ => {
                return Err(
                    ErrorKind::NotSupported("provided native window is not supported").into()
                )
            },
        };

        let mut attrs = Vec::<c_int>::with_capacity(ATTR_SIZE_HINT);

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

        let config = config.clone();
        let surface = super::last_glx_error(|| unsafe {
            self.inner.glx.CreateWindow(
                self.inner.raw.cast(),
                *config.inner.raw,
                window,
                attrs.as_ptr() as *const _,
            )
        })?;

        Ok(Surface {
            display: self.clone(),
            config,
            raw: surface,
            _nosendsync: PhantomData,
            _ty: PhantomData,
        })
    }
}

/// A wrapper around the `GLXWindow`.
pub struct Surface<T: SurfaceTypeTrait> {
    display: Display,
    config: Config,
    pub(crate) raw: GLXWindow,
    _nosendsync: PhantomData<*const std::ffi::c_void>,
    _ty: PhantomData<T>,
}

// Impl only `Send` for Surface.
unsafe impl<T: SurfaceTypeTrait> Send for Surface<T> {}

impl<T: SurfaceTypeTrait> Surface<T> {
    /// # Safety
    ///
    /// The caller must ensure that the attribute could be present.
    unsafe fn raw_attribute(&self, attr: c_int) -> c_uint {
        unsafe {
            let mut value = 0;
            // This shouldn't generate any errors given that we know that the surface is
            // valid.
            self.display.inner.glx.QueryDrawable(
                self.display.inner.raw.cast(),
                self.raw,
                attr,
                &mut value,
            );
            value
        }
    }
}

impl<T: SurfaceTypeTrait> Drop for Surface<T> {
    fn drop(&mut self) {
        let _ = super::last_glx_error(|| unsafe {
            match T::surface_type() {
                SurfaceType::Pbuffer => {
                    self.display.inner.glx.DestroyPbuffer(self.display.inner.raw.cast(), self.raw);
                },
                SurfaceType::Window => {
                    self.display.inner.glx.DestroyWindow(self.display.inner.raw.cast(), self.raw);
                },
                SurfaceType::Pixmap => {
                    self.display.inner.glx.DestroyPixmap(self.display.inner.raw.cast(), self.raw);
                },
            }
        });
    }
}

impl<T: SurfaceTypeTrait> GlSurface<T> for Surface<T> {
    type Context = PossiblyCurrentContext;
    type SurfaceType = T;

    fn buffer_age(&self) -> u32 {
        self.display
            .inner
            .client_extensions
            .contains("GLX_EXT_buffer_age")
            .then(|| unsafe { self.raw_attribute(glx_extra::BACK_BUFFER_AGE_EXT as c_int) })
            .unwrap_or(0) as u32
    }

    fn width(&self) -> Option<u32> {
        unsafe { Some(self.raw_attribute(glx::WIDTH as c_int) as u32) }
    }

    fn height(&self) -> Option<u32> {
        unsafe { Some(self.raw_attribute(glx::HEIGHT as c_int) as u32) }
    }

    fn is_single_buffered(&self) -> bool {
        self.config.is_single_buffered()
    }

    fn swap_buffers(&self, _context: &Self::Context) -> Result<()> {
        super::last_glx_error(|| unsafe {
            self.display.inner.glx.SwapBuffers(self.display.inner.raw.cast(), self.raw);
        })
    }

    fn set_swap_interval(&self, _context: &Self::Context, interval: SwapInterval) -> Result<()> {
        let extra = match self.display.inner.glx_extra {
            Some(extra) if self.display.inner.features.contains(DisplayFeatures::SWAP_CONTROL) => {
                extra
            },
            _ => {
                return Err(
                    ErrorKind::NotSupported("swap control extensions are not supported").into()
                );
            },
        };

        let interval = match interval {
            SwapInterval::DontWait => 0,
            SwapInterval::Wait(n) => n.get(),
        };

        let mut applied = false;

        // Apply the `EXT` first since it's per window.
        if !applied && self.display.inner.client_extensions.contains("GLX_EXT_swap_control") {
            super::last_glx_error(|| unsafe {
                // Check for error explicitly here, other apis do have indication for failure.
                extra.SwapIntervalEXT(self.display.inner.raw.cast(), self.raw, interval as _);
                applied = true;
            })?;
        }

        if !applied && self.display.inner.client_extensions.contains("GLX_MESA_swap_control") {
            unsafe {
                applied = extra.SwapIntervalMESA(interval as _) != glx::BAD_CONTEXT as _;
            }
        }

        if !applied && self.display.inner.client_extensions.contains("GLX_SGI_swap_control") {
            unsafe {
                applied = extra.SwapIntervalSGI(interval as _) != glx::BAD_CONTEXT as _;
            }
        }

        if applied {
            Ok(())
        } else {
            Err(ErrorKind::BadContext.into())
        }
    }

    fn is_current(&self, context: &Self::Context) -> bool {
        self.is_current_draw(context) && self.is_current_read(context)
    }

    fn is_current_draw(&self, _context: &Self::Context) -> bool {
        unsafe { self.display.inner.glx.GetCurrentDrawable() == self.raw }
    }

    fn is_current_read(&self, _context: &Self::Context) -> bool {
        unsafe { self.display.inner.glx.GetCurrentReadDrawable() == self.raw }
    }

    fn resize(&self, _context: &Self::Context, _width: NonZeroU32, _height: NonZeroU32) {
        // This isn't supported with GLXDrawable.
    }
}

impl<T: SurfaceTypeTrait> GetGlConfig for Surface<T> {
    type Target = Config;

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

impl<T: SurfaceTypeTrait> GetGlDisplay for Surface<T> {
    type Target = Display;

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

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

impl<T: SurfaceTypeTrait> AsRawSurface for Surface<T> {
    fn raw_surface(&self) -> RawSurface {
        RawSurface::Glx(self.raw as u64)
    }
}

impl<T: SurfaceTypeTrait> Sealed for Surface<T> {}