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    /// [`GLContext`][crate::GLContext] is an object representing the platform-specific
10    /// OpenGL drawing context.
11    ///
12    /// `GdkGLContexts` are created for a [`Window`][crate::Window] using
13    /// [`Window::create_gl_context()`][crate::Window::create_gl_context()], and the context will match
14    /// the [`Visual`][crate::Visual] of the window.
15    ///
16    /// A [`GLContext`][crate::GLContext] is not tied to any particular normal framebuffer.
17    /// For instance, it cannot draw to the [`Window`][crate::Window] back buffer. The GDK
18    /// repaint system is in full control of the painting to that. Instead,
19    /// you can create render buffers or textures and use `gdk_cairo_draw_from_gl()`
20    /// in the draw function of your widget to draw them. Then GDK will handle
21    /// the integration of your rendering with that of other widgets.
22    ///
23    /// Support for [`GLContext`][crate::GLContext] is platform-specific, context creation
24    /// can fail, returning [`None`] context.
25    ///
26    /// A [`GLContext`][crate::GLContext] has to be made "current" in order to start using
27    /// it, otherwise any OpenGL call will be ignored.
28    ///
29    /// ## Creating a new OpenGL context ##
30    ///
31    /// In order to create a new [`GLContext`][crate::GLContext] instance you need a
32    /// [`Window`][crate::Window], which you typically get during the realize call
33    /// of a widget.
34    ///
35    /// A [`GLContext`][crate::GLContext] is not realized until either [`make_current()`][Self::make_current()],
36    /// or until it is realized using [`realize()`][Self::realize()]. It is possible to
37    /// specify details of the GL context like the OpenGL version to be used, or
38    /// whether the GL context should have extra state validation enabled after
39    /// calling [`Window::create_gl_context()`][crate::Window::create_gl_context()] by calling [`realize()`][Self::realize()].
40    /// If the realization fails you have the option to change the settings of the
41    /// [`GLContext`][crate::GLContext] and try again.
42    ///
43    /// ## Using a GdkGLContext ##
44    ///
45    /// You will need to make the [`GLContext`][crate::GLContext] the current context
46    /// before issuing OpenGL calls; the system sends OpenGL commands to
47    /// whichever context is current. It is possible to have multiple
48    /// contexts, so you always need to ensure that the one which you
49    /// want to draw with is the current one before issuing commands:
50    ///
51    ///
52    ///
53    /// **⚠️ The following code is in C ⚠️**
54    ///
55    /// ```C
56    ///   gdk_gl_context_make_current (context);
57    /// ```
58    ///
59    /// You can now perform your drawing using OpenGL commands.
60    ///
61    /// You can check which [`GLContext`][crate::GLContext] is the current one by using
62    /// [`current()`][Self::current()]; you can also unset any [`GLContext`][crate::GLContext]
63    /// that is currently set by calling [`clear_current()`][Self::clear_current()].
64    ///
65    /// This is an Abstract Base Class, you cannot instantiate it.
66    ///
67    /// ## Properties
68    ///
69    ///
70    /// #### `display`
71    ///  The [`Display`][crate::Display] used to create the [`GLContext`][crate::GLContext].
72    ///
73    /// Readable | Writable | Construct Only
74    ///
75    ///
76    /// #### `shared-context`
77    ///  The [`GLContext`][crate::GLContext] that this context is sharing data with, or [`None`]
78    ///
79    /// Readable | Writable | Construct Only
80    ///
81    ///
82    /// #### `window`
83    ///  The [`Window`][crate::Window] the gl context is bound to.
84    ///
85    /// Readable | Writable | Construct Only
86    #[doc(alias = "GdkGLContext")]
87    pub struct GLContext(Object<ffi::GdkGLContext>);
88
89    match fn {
90        type_ => || ffi::gdk_gl_context_get_type(),
91    }
92}
93
94impl GLContext {
95    /// Retrieves the value set using [`set_debug_enabled()`][Self::set_debug_enabled()].
96    ///
97    /// # Returns
98    ///
99    /// [`true`] if debugging is enabled
100    #[doc(alias = "gdk_gl_context_get_debug_enabled")]
101    #[doc(alias = "get_debug_enabled")]
102    pub fn is_debug_enabled(&self) -> bool {
103        unsafe { from_glib(ffi::gdk_gl_context_get_debug_enabled(self.to_glib_none().0)) }
104    }
105
106    /// Retrieves the [`Display`][crate::Display] the `self` is created for
107    ///
108    /// # Returns
109    ///
110    /// a [`Display`][crate::Display] or [`None`]
111    #[doc(alias = "gdk_gl_context_get_display")]
112    #[doc(alias = "get_display")]
113    pub fn display(&self) -> Option<Display> {
114        unsafe { from_glib_none(ffi::gdk_gl_context_get_display(self.to_glib_none().0)) }
115    }
116
117    /// Retrieves the value set using [`set_forward_compatible()`][Self::set_forward_compatible()].
118    ///
119    /// # Returns
120    ///
121    /// [`true`] if the context should be forward compatible
122    #[doc(alias = "gdk_gl_context_get_forward_compatible")]
123    #[doc(alias = "get_forward_compatible")]
124    pub fn is_forward_compatible(&self) -> bool {
125        unsafe {
126            from_glib(ffi::gdk_gl_context_get_forward_compatible(
127                self.to_glib_none().0,
128            ))
129        }
130    }
131
132    /// Retrieves the major and minor version requested by calling
133    /// [`set_required_version()`][Self::set_required_version()].
134    ///
135    /// # Returns
136    ///
137    ///
138    /// ## `major`
139    /// return location for the major version to request
140    ///
141    /// ## `minor`
142    /// return location for the minor version to request
143    #[doc(alias = "gdk_gl_context_get_required_version")]
144    #[doc(alias = "get_required_version")]
145    pub fn required_version(&self) -> (i32, i32) {
146        unsafe {
147            let mut major = std::mem::MaybeUninit::uninit();
148            let mut minor = std::mem::MaybeUninit::uninit();
149            ffi::gdk_gl_context_get_required_version(
150                self.to_glib_none().0,
151                major.as_mut_ptr(),
152                minor.as_mut_ptr(),
153            );
154            (major.assume_init(), minor.assume_init())
155        }
156    }
157
158    /// Retrieves the [`GLContext`][crate::GLContext] that this `self` share data with.
159    ///
160    /// # Returns
161    ///
162    /// a [`GLContext`][crate::GLContext] or [`None`]
163    #[doc(alias = "gdk_gl_context_get_shared_context")]
164    #[doc(alias = "get_shared_context")]
165    #[doc(alias = "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 = std::mem::MaybeUninit::uninit();
203            let mut minor = std::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 = std::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}