gtk-rs / gtk3-rs

Rust bindings for GTK 3
https://gtk-rs.org
MIT License
508 stars 90 forks source link

[FEATURE REQUEST] From raw C GdkGlContext* #765

Closed lattice0 closed 1 year ago

lattice0 commented 1 year ago

I have a C++ Flutter code which generates me a GdkGLContext:

  GdkWindow *window = gtk_widget_get_parent_window(GTK_WIDGET(self->fl_view));
  GError *error = NULL;
  GdkGLContext *context = gdk_window_create_gl_context(window, &error);

where the pointers are C pointers.

On https://github.com/gtk-rs/gtk3-rs/blob/master/gdk/src/auto/gl_context.rs I added

    pub fn new_from_ptr(gl_context: *mut ffi::GdkGLContext) -> Self {
        assert_initialized_main_thread!();
        unsafe {
            from_glib_none(gl_context)
        }
    }

so I could initialize it from a raw C GdkGlContext, but I get a panic on assert_initialized_main_thread!, probably because the gtk3-rs has an internal watcher to see if the main thread is initialized, and I'm trying to initialize an object without having a context.

Can I do a PR without the assertion?

    pub fn new_from_ptr(gl_context: *mut ffi::GdkGLContext) -> Self {
        //assert_initialized_main_thread!();
        unsafe {
            from_glib_none(gl_context)
        }
    }

works but I can't be sure yet cause I get a function was not loaded panic probably on epoxy (cause I don't know how to do OpenGL on gtk3-rs)

bilelmoussaoui commented 1 year ago

you can use from_glib_none from your side as well?

lattice0 commented 1 year ago

Sorry I did not understand the question. i used it, it didn't crash, it crashed on epoxy as far as I understood.

-------- Mensagem Original -------- Em 23 de jul. de 2022 19:01, Bilal Elmoussaoui escreveu:

you can use from_glib_none from your side as well?

— Reply to this email directly, view it on GitHub, or unsubscribe. You are receiving this because you authored the thread.Message ID: @.***>

lattice0 commented 1 year ago

I fixed the epoxy crash, now no crashes (but also no image), but I need to know if it's ok to do

    pub fn new_from_ptr(gl_context: *mut ffi::GdkGLContext) -> Self {
        unsafe {
            from_glib_none(gl_context)
        }
    }

on the PR

sdroege commented 1 year ago

You don't need a new function, just call from_glib_none() yourself.

For initialization there's the unsafe gtk::set_initialized() that you'd need here if you assume something else called gtk_init() for you already.

lattice0 commented 1 year ago
    unsafe { gtk::set_initialized() };
    println!("gtk::is_initialized: {}", gtk::is_initialized());
    let context = unsafe { gdk::GLContext::from_glib_none(gdk_gl_context_ptr) };
    println!("version: {:?}", context.version());
    println!("is_legacy: {:?}", context.is_legacy());

gives

(my_app:13317): Gdk-CRITICAL **: 21:46:24.482: gdk_gl_context_get_version: assertion 'priv->realized' failed

(my_app:13317): Gdk-CRITICAL **: 21:46:24.482: gdk_gl_context_is_legacy: assertion 'priv->realized' failed

and prints

gtk::is_initialized: true
version: (0, 33742432)

I even tried putting gtk::init() before but same problem.

sdroege commented 1 year ago

As the error suggests, your problem is that the context is not realized yet and because of that can't be used.