Skip to main content

gdk/auto/
gl_context.rs

1// This file was generated by gir (https://github.com/gtk-rs/gir)
2// from gir-files (https://github.com/gtk-rs/gir-files)
3// DO NOT EDIT
4
5use crate::{Display, Window};
6use glib::translate::*;
7use std::{fmt, mem, ptr};
8
9glib::wrapper! {
10    /// [`GLContext`][crate::GLContext] is an object representing the platform-specific
11    /// OpenGL drawing context.
12    ///
13    /// `GdkGLContexts` are created for a [`Window`][crate::Window] using
14    /// [`Window::create_gl_context()`][crate::Window::create_gl_context()], and the context will match
15    /// the [`Visual`][crate::Visual] of the window.
16    ///
17    /// A [`GLContext`][crate::GLContext] is not tied to any particular normal framebuffer.
18    /// For instance, it cannot draw to the [`Window`][crate::Window] back buffer. The GDK
19    /// repaint system is in full control of the painting to that. Instead,
20    /// you can create render buffers or textures and use `gdk_cairo_draw_from_gl()`
21    /// in the draw function of your widget to draw them. Then GDK will handle
22    /// the integration of your rendering with that of other widgets.
23    ///
24    /// Support for [`GLContext`][crate::GLContext] is platform-specific, context creation
25    /// can fail, returning [`None`] context.
26    ///
27    /// A [`GLContext`][crate::GLContext] has to be made "current" in order to start using
28    /// it, otherwise any OpenGL call will be ignored.
29    ///
30    /// ## Creating a new OpenGL context ##
31    ///
32    /// In order to create a new [`GLContext`][crate::GLContext] instance you need a
33    /// [`Window`][crate::Window], which you typically get during the realize call
34    /// of a widget.
35    ///
36    /// A [`GLContext`][crate::GLContext] is not realized until either [`make_current()`][Self::make_current()],
37    /// or until it is realized using [`realize()`][Self::realize()]. It is possible to
38    /// specify details of the GL context like the OpenGL version to be used, or
39    /// whether the GL context should have extra state validation enabled after
40    /// calling [`Window::create_gl_context()`][crate::Window::create_gl_context()] by calling [`realize()`][Self::realize()].
41    /// If the realization fails you have the option to change the settings of the
42    /// [`GLContext`][crate::GLContext] and try again.
43    ///
44    /// ## Using a GdkGLContext ##
45    ///
46    /// You will need to make the [`GLContext`][crate::GLContext] the current context
47    /// before issuing OpenGL calls; the system sends OpenGL commands to
48    /// whichever context is current. It is possible to have multiple
49    /// contexts, so you always need to ensure that the one which you
50    /// want to draw with is the current one before issuing commands:
51    ///
52    ///
53    ///
54    /// **⚠️ The following code is in C ⚠️**
55    ///
56    /// ```C
57    ///   gdk_gl_context_make_current (context);
58    /// ```
59    ///
60    /// You can now perform your drawing using OpenGL commands.
61    ///
62    /// You can check which [`GLContext`][crate::GLContext] is the current one by using
63    /// [`current()`][Self::current()]; you can also unset any [`GLContext`][crate::GLContext]
64    /// that is currently set by calling [`clear_current()`][Self::clear_current()].
65    ///
66    /// This is an Abstract Base Class, you cannot instantiate it.
67    ///
68    /// ## Properties
69    ///
70    ///
71    /// #### `display`
72    ///  The [`Display`][crate::Display] used to create the [`GLContext`][crate::GLContext].
73    ///
74    /// Readable | Writeable | Construct Only
75    ///
76    ///
77    /// #### `shared-context`
78    ///  The [`GLContext`][crate::GLContext] that this context is sharing data with, or [`None`]
79    ///
80    /// Readable | Writeable | Construct Only
81    ///
82    ///
83    /// #### `window`
84    ///  The [`Window`][crate::Window] the gl context is bound to.
85    ///
86    /// Readable | Writeable | Construct Only
87    #[doc(alias = "GdkGLContext")]
88    pub struct GLContext(Object<ffi::GdkGLContext>);
89
90    match fn {
91        type_ => || ffi::gdk_gl_context_get_type(),
92    }
93}
94
95impl GLContext {
96    /// Retrieves the value set using [`set_debug_enabled()`][Self::set_debug_enabled()].
97    ///
98    /// # Returns
99    ///
100    /// [`true`] if debugging is enabled
101    #[doc(alias = "gdk_gl_context_get_debug_enabled")]
102    #[doc(alias = "get_debug_enabled")]
103    pub fn is_debug_enabled(&self) -> bool {
104        unsafe { from_glib(ffi::gdk_gl_context_get_debug_enabled(self.to_glib_none().0)) }
105    }
106
107    /// Retrieves the [`Display`][crate::Display] the `self` is created for
108    ///
109    /// # Returns
110    ///
111    /// a [`Display`][crate::Display] or [`None`]
112    #[doc(alias = "gdk_gl_context_get_display")]
113    #[doc(alias = "get_display")]
114    pub fn display(&self) -> Option<Display> {
115        unsafe { from_glib_none(ffi::gdk_gl_context_get_display(self.to_glib_none().0)) }
116    }
117
118    /// Retrieves the value set using [`set_forward_compatible()`][Self::set_forward_compatible()].
119    ///
120    /// # Returns
121    ///
122    /// [`true`] if the context should be forward compatible
123    #[doc(alias = "gdk_gl_context_get_forward_compatible")]
124    #[doc(alias = "get_forward_compatible")]
125    pub fn is_forward_compatible(&self) -> bool {
126        unsafe {
127            from_glib(ffi::gdk_gl_context_get_forward_compatible(
128                self.to_glib_none().0,
129            ))
130        }
131    }
132
133    /// Retrieves the major and minor version requested by calling
134    /// [`set_required_version()`][Self::set_required_version()].
135    ///
136    /// # Returns
137    ///
138    ///
139    /// ## `major`
140    /// return location for the major version to request
141    ///
142    /// ## `minor`
143    /// return location for the minor version to request
144    #[doc(alias = "gdk_gl_context_get_required_version")]
145    #[doc(alias = "get_required_version")]
146    pub fn required_version(&self) -> (i32, i32) {
147        unsafe {
148            let mut major = mem::MaybeUninit::uninit();
149            let mut minor = mem::MaybeUninit::uninit();
150            ffi::gdk_gl_context_get_required_version(
151                self.to_glib_none().0,
152                major.as_mut_ptr(),
153                minor.as_mut_ptr(),
154            );
155            (major.assume_init(), minor.assume_init())
156        }
157    }
158
159    /// Retrieves the [`GLContext`][crate::GLContext] that this `self` share data with.
160    ///
161    /// # Returns
162    ///
163    /// a [`GLContext`][crate::GLContext] or [`None`]
164    #[doc(alias = "gdk_gl_context_get_shared_context")]
165    #[doc(alias = "get_shared_context")]
166    #[must_use]
167    pub fn shared_context(&self) -> Option<GLContext> {
168        unsafe {
169            from_glib_none(ffi::gdk_gl_context_get_shared_context(
170                self.to_glib_none().0,
171            ))
172        }
173    }
174
175    /// Checks whether the `self` is using an OpenGL or OpenGL ES profile.
176    ///
177    /// # Returns
178    ///
179    /// [`true`] if the [`GLContext`][crate::GLContext] is using an OpenGL ES profile
180    #[doc(alias = "gdk_gl_context_get_use_es")]
181    #[doc(alias = "get_use_es")]
182    pub fn uses_es(&self) -> bool {
183        unsafe { from_glib(ffi::gdk_gl_context_get_use_es(self.to_glib_none().0)) }
184    }
185
186    /// Retrieves the OpenGL version of the `self`.
187    ///
188    /// The `self` must be realized prior to calling this function.
189    ///
190    /// # Returns
191    ///
192    ///
193    /// ## `major`
194    /// return location for the major version
195    ///
196    /// ## `minor`
197    /// return location for the minor version
198    #[doc(alias = "gdk_gl_context_get_version")]
199    #[doc(alias = "get_version")]
200    pub fn version(&self) -> (i32, i32) {
201        unsafe {
202            let mut major = mem::MaybeUninit::uninit();
203            let mut minor = mem::MaybeUninit::uninit();
204            ffi::gdk_gl_context_get_version(
205                self.to_glib_none().0,
206                major.as_mut_ptr(),
207                minor.as_mut_ptr(),
208            );
209            (major.assume_init(), minor.assume_init())
210        }
211    }
212
213    /// Retrieves the [`Window`][crate::Window] used by the `self`.
214    ///
215    /// # Returns
216    ///
217    /// a [`Window`][crate::Window] or [`None`]
218    #[doc(alias = "gdk_gl_context_get_window")]
219    #[doc(alias = "get_window")]
220    pub fn window(&self) -> Option<Window> {
221        unsafe { from_glib_none(ffi::gdk_gl_context_get_window(self.to_glib_none().0)) }
222    }
223
224    /// Whether the [`GLContext`][crate::GLContext] is in legacy mode or not.
225    ///
226    /// The [`GLContext`][crate::GLContext] must be realized before calling this function.
227    ///
228    /// When realizing a GL context, GDK will try to use the OpenGL 3.2 core
229    /// profile; this profile removes all the OpenGL API that was deprecated
230    /// prior to the 3.2 version of the specification. If the realization is
231    /// successful, this function will return [`false`].
232    ///
233    /// If the underlying OpenGL implementation does not support core profiles,
234    /// GDK will fall back to a pre-3.2 compatibility profile, and this function
235    /// will return [`true`].
236    ///
237    /// You can use the value returned by this function to decide which kind
238    /// of OpenGL API to use, or whether to do extension discovery, or what
239    /// kind of shader programs to load.
240    ///
241    /// # Returns
242    ///
243    /// [`true`] if the GL context is in legacy mode
244    #[doc(alias = "gdk_gl_context_is_legacy")]
245    pub fn is_legacy(&self) -> bool {
246        unsafe { from_glib(ffi::gdk_gl_context_is_legacy(self.to_glib_none().0)) }
247    }
248
249    /// Makes the `self` the current one.
250    #[doc(alias = "gdk_gl_context_make_current")]
251    pub fn make_current(&self) {
252        unsafe {
253            ffi::gdk_gl_context_make_current(self.to_glib_none().0);
254        }
255    }
256
257    /// Realizes the given [`GLContext`][crate::GLContext].
258    ///
259    /// It is safe to call this function on a realized [`GLContext`][crate::GLContext].
260    ///
261    /// # Returns
262    ///
263    /// [`true`] if the context is realized
264    #[doc(alias = "gdk_gl_context_realize")]
265    pub fn realize(&self) -> Result<(), glib::Error> {
266        unsafe {
267            let mut error = ptr::null_mut();
268            let is_ok = ffi::gdk_gl_context_realize(self.to_glib_none().0, &mut error);
269            debug_assert_eq!(is_ok == glib::ffi::GFALSE, !error.is_null());
270            if error.is_null() {
271                Ok(())
272            } else {
273                Err(from_glib_full(error))
274            }
275        }
276    }
277
278    /// Sets whether the [`GLContext`][crate::GLContext] should perform extra validations and
279    /// run time checking. This is useful during development, but has
280    /// additional overhead.
281    ///
282    /// The [`GLContext`][crate::GLContext] must not be realized or made current prior to
283    /// calling this function.
284    /// ## `enabled`
285    /// whether to enable debugging in the context
286    #[doc(alias = "gdk_gl_context_set_debug_enabled")]
287    pub fn set_debug_enabled(&self, enabled: bool) {
288        unsafe {
289            ffi::gdk_gl_context_set_debug_enabled(self.to_glib_none().0, enabled.into_glib());
290        }
291    }
292
293    /// Sets whether the [`GLContext`][crate::GLContext] should be forward compatible.
294    ///
295    /// Forward compatibile contexts must not support OpenGL functionality that
296    /// has been marked as deprecated in the requested version; non-forward
297    /// compatible contexts, on the other hand, must support both deprecated and
298    /// non deprecated functionality.
299    ///
300    /// The [`GLContext`][crate::GLContext] must not be realized or made current prior to calling
301    /// this function.
302    /// ## `compatible`
303    /// whether the context should be forward compatible
304    #[doc(alias = "gdk_gl_context_set_forward_compatible")]
305    pub fn set_forward_compatible(&self, compatible: bool) {
306        unsafe {
307            ffi::gdk_gl_context_set_forward_compatible(
308                self.to_glib_none().0,
309                compatible.into_glib(),
310            );
311        }
312    }
313
314    /// Sets the major and minor version of OpenGL to request.
315    ///
316    /// Setting `major` and `minor` to zero will use the default values.
317    ///
318    /// The [`GLContext`][crate::GLContext] must not be realized or made current prior to calling
319    /// this function.
320    /// ## `major`
321    /// the major version to request
322    /// ## `minor`
323    /// the minor version to request
324    #[doc(alias = "gdk_gl_context_set_required_version")]
325    pub fn set_required_version(&self, major: i32, minor: i32) {
326        unsafe {
327            ffi::gdk_gl_context_set_required_version(self.to_glib_none().0, major, minor);
328        }
329    }
330
331    /// Requests that GDK create a OpenGL ES context instead of an OpenGL one,
332    /// if the platform and windowing system allows it.
333    ///
334    /// The `self` must not have been realized.
335    ///
336    /// By default, GDK will attempt to automatically detect whether the
337    /// underlying GL implementation is OpenGL or OpenGL ES once the `self`
338    /// is realized.
339    ///
340    /// You should check the return value of [`uses_es()`][Self::uses_es()] after
341    /// calling [`realize()`][Self::realize()] to decide whether to use the OpenGL or
342    /// OpenGL ES API, extensions, or shaders.
343    /// ## `use_es`
344    /// whether the context should use OpenGL ES instead of OpenGL,
345    ///  or -1 to allow auto-detection
346    #[doc(alias = "gdk_gl_context_set_use_es")]
347    pub fn set_use_es(&self, use_es: i32) {
348        unsafe {
349            ffi::gdk_gl_context_set_use_es(self.to_glib_none().0, use_es);
350        }
351    }
352
353    /// Clears the current [`GLContext`][crate::GLContext].
354    ///
355    /// Any OpenGL call after this function returns will be ignored
356    /// until [`make_current()`][Self::make_current()] is called.
357    #[doc(alias = "gdk_gl_context_clear_current")]
358    pub fn clear_current() {
359        assert_initialized_main_thread!();
360        unsafe {
361            ffi::gdk_gl_context_clear_current();
362        }
363    }
364
365    /// Retrieves the current [`GLContext`][crate::GLContext].
366    ///
367    /// # Returns
368    ///
369    /// the current [`GLContext`][crate::GLContext], or [`None`]
370    #[doc(alias = "gdk_gl_context_get_current")]
371    #[doc(alias = "get_current")]
372    pub fn current() -> Option<GLContext> {
373        assert_initialized_main_thread!();
374        unsafe { from_glib_none(ffi::gdk_gl_context_get_current()) }
375    }
376}
377
378impl fmt::Display for GLContext {
379    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
380        f.write_str("GLContext")
381    }
382}