1#![allow(deprecated)]
5
6#[cfg(feature = "v4_6")]
7#[cfg_attr(docsrs, doc(cfg(feature = "v4_6")))]
8use crate::GLAPI;
9use crate::{ffi, Display, DrawContext, Surface};
10#[cfg(feature = "v4_6")]
11#[cfg_attr(docsrs, doc(cfg(feature = "v4_6")))]
12use glib::signal::{connect_raw, SignalHandlerId};
13use glib::{prelude::*, translate::*};
14#[cfg(feature = "v4_6")]
15#[cfg_attr(docsrs, doc(cfg(feature = "v4_6")))]
16use std::boxed::Box as Box_;
17
18glib::wrapper! {
19 #[doc(alias = "GdkGLContext")]
20 pub struct GLContext(Object<ffi::GdkGLContext>) @extends DrawContext;
21
22 match fn {
23 type_ => || ffi::gdk_gl_context_get_type(),
24 }
25}
26
27impl GLContext {
28 pub const NONE: Option<&'static GLContext> = None;
29
30 #[doc(alias = "gdk_gl_context_clear_current")]
31 pub fn clear_current() {
32 assert_initialized_main_thread!();
33 unsafe {
34 ffi::gdk_gl_context_clear_current();
35 }
36 }
37
38 #[doc(alias = "gdk_gl_context_get_current")]
39 #[doc(alias = "get_current")]
40 pub fn current() -> Option<GLContext> {
41 assert_initialized_main_thread!();
42 unsafe { from_glib_none(ffi::gdk_gl_context_get_current()) }
43 }
44}
45
46pub trait GLContextExt: IsA<GLContext> + 'static {
47 #[cfg(feature = "v4_6")]
48 #[cfg_attr(docsrs, doc(cfg(feature = "v4_6")))]
49 #[doc(alias = "gdk_gl_context_get_allowed_apis")]
50 #[doc(alias = "get_allowed_apis")]
51 #[doc(alias = "allowed-apis")]
52 fn allowed_apis(&self) -> GLAPI {
53 unsafe {
54 from_glib(ffi::gdk_gl_context_get_allowed_apis(
55 self.as_ref().to_glib_none().0,
56 ))
57 }
58 }
59
60 #[cfg(feature = "v4_6")]
61 #[cfg_attr(docsrs, doc(cfg(feature = "v4_6")))]
62 #[doc(alias = "gdk_gl_context_get_api")]
63 #[doc(alias = "get_api")]
64 fn api(&self) -> GLAPI {
65 unsafe { from_glib(ffi::gdk_gl_context_get_api(self.as_ref().to_glib_none().0)) }
66 }
67
68 #[doc(alias = "gdk_gl_context_get_debug_enabled")]
69 #[doc(alias = "get_debug_enabled")]
70 fn is_debug_enabled(&self) -> bool {
71 unsafe {
72 from_glib(ffi::gdk_gl_context_get_debug_enabled(
73 self.as_ref().to_glib_none().0,
74 ))
75 }
76 }
77
78 #[doc(alias = "gdk_gl_context_get_display")]
79 #[doc(alias = "get_display")]
80 fn display(&self) -> Option<Display> {
81 unsafe {
82 from_glib_none(ffi::gdk_gl_context_get_display(
83 self.as_ref().to_glib_none().0,
84 ))
85 }
86 }
87
88 #[doc(alias = "gdk_gl_context_get_forward_compatible")]
89 #[doc(alias = "get_forward_compatible")]
90 fn is_forward_compatible(&self) -> bool {
91 unsafe {
92 from_glib(ffi::gdk_gl_context_get_forward_compatible(
93 self.as_ref().to_glib_none().0,
94 ))
95 }
96 }
97
98 #[doc(alias = "gdk_gl_context_get_required_version")]
99 #[doc(alias = "get_required_version")]
100 fn required_version(&self) -> (i32, i32) {
101 unsafe {
102 let mut major = std::mem::MaybeUninit::uninit();
103 let mut minor = std::mem::MaybeUninit::uninit();
104 ffi::gdk_gl_context_get_required_version(
105 self.as_ref().to_glib_none().0,
106 major.as_mut_ptr(),
107 minor.as_mut_ptr(),
108 );
109 (major.assume_init(), minor.assume_init())
110 }
111 }
112
113 #[cfg_attr(feature = "v4_4", deprecated = "Since 4.4")]
114 #[allow(deprecated)]
115 #[doc(alias = "gdk_gl_context_get_shared_context")]
116 #[doc(alias = "get_shared_context")]
117 #[doc(alias = "shared-context")]
118 #[must_use]
119 fn shared_context(&self) -> Option<GLContext> {
120 unsafe {
121 from_glib_none(ffi::gdk_gl_context_get_shared_context(
122 self.as_ref().to_glib_none().0,
123 ))
124 }
125 }
126
127 #[doc(alias = "gdk_gl_context_get_surface")]
128 #[doc(alias = "get_surface")]
129 fn surface(&self) -> Option<Surface> {
130 unsafe {
131 from_glib_none(ffi::gdk_gl_context_get_surface(
132 self.as_ref().to_glib_none().0,
133 ))
134 }
135 }
136
137 #[doc(alias = "gdk_gl_context_get_use_es")]
138 #[doc(alias = "get_use_es")]
139 fn uses_es(&self) -> bool {
140 unsafe {
141 from_glib(ffi::gdk_gl_context_get_use_es(
142 self.as_ref().to_glib_none().0,
143 ))
144 }
145 }
146
147 #[doc(alias = "gdk_gl_context_get_version")]
148 #[doc(alias = "get_version")]
149 fn version(&self) -> (i32, i32) {
150 unsafe {
151 let mut major = std::mem::MaybeUninit::uninit();
152 let mut minor = std::mem::MaybeUninit::uninit();
153 ffi::gdk_gl_context_get_version(
154 self.as_ref().to_glib_none().0,
155 major.as_mut_ptr(),
156 minor.as_mut_ptr(),
157 );
158 (major.assume_init(), minor.assume_init())
159 }
160 }
161
162 #[doc(alias = "gdk_gl_context_is_legacy")]
163 fn is_legacy(&self) -> bool {
164 unsafe {
165 from_glib(ffi::gdk_gl_context_is_legacy(
166 self.as_ref().to_glib_none().0,
167 ))
168 }
169 }
170
171 #[cfg(feature = "v4_4")]
172 #[cfg_attr(docsrs, doc(cfg(feature = "v4_4")))]
173 #[doc(alias = "gdk_gl_context_is_shared")]
174 fn is_shared(&self, other: &impl IsA<GLContext>) -> bool {
175 unsafe {
176 from_glib(ffi::gdk_gl_context_is_shared(
177 self.as_ref().to_glib_none().0,
178 other.as_ref().to_glib_none().0,
179 ))
180 }
181 }
182
183 #[doc(alias = "gdk_gl_context_make_current")]
184 fn make_current(&self) {
185 unsafe {
186 ffi::gdk_gl_context_make_current(self.as_ref().to_glib_none().0);
187 }
188 }
189
190 #[doc(alias = "gdk_gl_context_realize")]
191 fn realize(&self) -> Result<(), glib::Error> {
192 unsafe {
193 let mut error = std::ptr::null_mut();
194 let is_ok = ffi::gdk_gl_context_realize(self.as_ref().to_glib_none().0, &mut error);
195 debug_assert_eq!(is_ok == glib::ffi::GFALSE, !error.is_null());
196 if error.is_null() {
197 Ok(())
198 } else {
199 Err(from_glib_full(error))
200 }
201 }
202 }
203
204 #[cfg(feature = "v4_6")]
205 #[cfg_attr(docsrs, doc(cfg(feature = "v4_6")))]
206 #[doc(alias = "gdk_gl_context_set_allowed_apis")]
207 #[doc(alias = "allowed-apis")]
208 fn set_allowed_apis(&self, apis: GLAPI) {
209 unsafe {
210 ffi::gdk_gl_context_set_allowed_apis(self.as_ref().to_glib_none().0, apis.into_glib());
211 }
212 }
213
214 #[doc(alias = "gdk_gl_context_set_debug_enabled")]
215 fn set_debug_enabled(&self, enabled: bool) {
216 unsafe {
217 ffi::gdk_gl_context_set_debug_enabled(
218 self.as_ref().to_glib_none().0,
219 enabled.into_glib(),
220 );
221 }
222 }
223
224 #[doc(alias = "gdk_gl_context_set_forward_compatible")]
225 fn set_forward_compatible(&self, compatible: bool) {
226 unsafe {
227 ffi::gdk_gl_context_set_forward_compatible(
228 self.as_ref().to_glib_none().0,
229 compatible.into_glib(),
230 );
231 }
232 }
233
234 #[doc(alias = "gdk_gl_context_set_required_version")]
235 fn set_required_version(&self, major: i32, minor: i32) {
236 unsafe {
237 ffi::gdk_gl_context_set_required_version(self.as_ref().to_glib_none().0, major, minor);
238 }
239 }
240
241 #[doc(alias = "gdk_gl_context_set_use_es")]
242 fn set_use_es(&self, use_es: i32) {
243 unsafe {
244 ffi::gdk_gl_context_set_use_es(self.as_ref().to_glib_none().0, use_es);
245 }
246 }
247
248 #[cfg(feature = "v4_6")]
249 #[cfg_attr(docsrs, doc(cfg(feature = "v4_6")))]
250 #[doc(alias = "allowed-apis")]
251 fn connect_allowed_apis_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
252 unsafe extern "C" fn notify_allowed_apis_trampoline<
253 P: IsA<GLContext>,
254 F: Fn(&P) + 'static,
255 >(
256 this: *mut ffi::GdkGLContext,
257 _param_spec: glib::ffi::gpointer,
258 f: glib::ffi::gpointer,
259 ) {
260 let f: &F = &*(f as *const F);
261 f(GLContext::from_glib_borrow(this).unsafe_cast_ref())
262 }
263 unsafe {
264 let f: Box_<F> = Box_::new(f);
265 connect_raw(
266 self.as_ptr() as *mut _,
267 c"notify::allowed-apis".as_ptr() as *const _,
268 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
269 notify_allowed_apis_trampoline::<Self, F> as *const (),
270 )),
271 Box_::into_raw(f),
272 )
273 }
274 }
275
276 #[cfg(feature = "v4_6")]
277 #[cfg_attr(docsrs, doc(cfg(feature = "v4_6")))]
278 #[doc(alias = "api")]
279 fn connect_api_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
280 unsafe extern "C" fn notify_api_trampoline<P: IsA<GLContext>, F: Fn(&P) + 'static>(
281 this: *mut ffi::GdkGLContext,
282 _param_spec: glib::ffi::gpointer,
283 f: glib::ffi::gpointer,
284 ) {
285 let f: &F = &*(f as *const F);
286 f(GLContext::from_glib_borrow(this).unsafe_cast_ref())
287 }
288 unsafe {
289 let f: Box_<F> = Box_::new(f);
290 connect_raw(
291 self.as_ptr() as *mut _,
292 c"notify::api".as_ptr() as *const _,
293 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
294 notify_api_trampoline::<Self, F> as *const (),
295 )),
296 Box_::into_raw(f),
297 )
298 }
299 }
300}
301
302impl<O: IsA<GLContext>> GLContextExt for O {}