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