[cairo] Threaded animation with cairo_gl backend over a GtkWidget

Henry (Yu) Song - SISA hsong at sisa.samsung.com
Mon Jan 14 10:21:58 PST 2013


That is weird.  I have no problem on intel/nvidia/fglrx drivers.  Did you try to run gdb on it and where is the seg fault point?

Henry

________________________________
From: cairo-bounces+henry.song=samsung.com at cairographics.org [cairo-bounces+henry.song=samsung.com at cairographics.org] on behalf of Carlos López González [genetita at gmail.com]
Sent: Saturday, January 12, 2013 2:29 AM
To: Henry (Yu) Song - SISA
Cc: cairo at cairographics.org
Subject: Re: [cairo] Threaded animation with cairo_gl backend over a GtkWidget

Hi!
looks like it doesn't work. I've added XInitThreads() and it still crashing. Also added XLockDisplay and XUnlockDisplay with the same result.
Any idea?
Thanks!


int main (int argc, char *argv[])

{

    if(!XInitThreads())

         return 0;


    gdk_threads_init();

    gdk_threads_enter();

    gtk_init(&argc, &argv);


    GtkWidget *window = gtk_window_new(GTK_WINDOW_TOPLEVEL);

    g_signal_connect(window, "destroy", G_CALLBACK(gtk_main_quit), NULL);

    gtk_window_set_title(GTK_WINDOW(window), "cairo_gl");

    gtk_window_set_default_size(GTK_WINDOW(window), 400, 300);

    gtk_widget_set_app_paintable(window, TRUE);

    gtk_widget_set_double_buffered(window, FALSE);


    gtk_widget_show_all(window);

    window_surface=create_source_surface_for_widget(window);

    (void)g_timeout_add(33, (GSourceFunc)timer_exe, window);

    //do_draw(NULL);


    gtk_main();


    gdk_threads_leave();

    return 0;

}



void *do_draw(void *ptr)

{

    rendering=TRUE;

    cairo_t *cr = cairo_create(window_surface);

  if(cairo_status(cr))

return NULL;

  Display *dpy =cairo_glx_device_get_display((cairo_surface_get_device(window_surface)));

    XLockDisplay(dpy);

    //do some time-consuming drawing here ...

    // ...

    // ...


  cairo_destroy(cr);


    cairo_gl_surface_swapbuffers (window_surface);

  XUnlockDisplay(dpy);

  rendering=FALSE;


    return NULL;

}




2013/1/11 Carlos López González <genetita at gmail.com<mailto:genetita at gmail.com>>
Thanks Henry, I'll give it a try.


2013/1/11 Henry (Yu) Song - SISA <hsong at sisa.samsung.com<mailto:hsong at sisa.samsung.com>>

Hi,

Please add XInitThreads() in your main.  For multithreaded application using gl_surface is not a good idea for the moment unless you have intel/radeon/nouveau drivers, otherwise, you will have very sloooooow performance.

We will resolve that soon in cairo for gl backend, I hope.

Henry
________________________________________
From: cairo-bounces+henry.song=samsung.com at cairographics.org<mailto:samsung.com at cairographics.org> [cairo-bounces+henry.song=samsung.com at cairographics.org<mailto:samsung.com at cairographics.org>] on behalf of Carlos López González [genetita at gmail.com<mailto:genetita at gmail.com>]
Sent: Thursday, January 10, 2013 11:26 AM
To: cairo at cairographics.org<mailto:cairo at cairographics.org>
Subject: [cairo] Threaded animation with cairo_gl backend over a GtkWidget

Hi!
I've made a modified version [1] of the example of threaded animation using Cairo [2].

First I've used the method to create a cairo_gl_surface from the test/gl_source_surface.c file adapted to create the surface for a given widget instead for a defined size.

Also, for simplicity, I removed all the handled signals except the destroy one.

To handle the drawing for the window widget using the cairo_gl backend I use the create_gl_surface_for_window function passing it the proper widget information.

So far, for a single drawing execution it works as expected:
1) In main() create the window_surface
2) The do_draw function creates a context based on window_surface
3) Once done the drawings with Cairo then the buffers are swapped and the content is shown on screen.

Notice that the window_surface is never destroyed explicitly because it is owned by the window widget and so it will be destroyed when the window is destroyed by gtk.

The problem comes when I enable the multithreading support by uncommenting the line:


//(void)g_timeout_add(33, (GSourceFunc)timer_exe, window);

It triggers the timer_exe function 33 times per second and then it creates threads that will call do_draw each time.

Once enabled it crashes at the first painting function from Cairo.

I believe that the problem is that the window_surface belongs to the window widget when I created it and then I can't modify it outside the main gtk thread.

How can I solve this? If I enclose the Cairo painting operations inside a gdk_threads_enter and
gdk_threads_leave I would add a lot of time consuming operations to the main gtk thread, which is precisely what I want to avoid using multithreading.

Any help? I would like to know at last, if my assumptions of the window_surface ownership are true or not.

Thanks!

[1] https://github.com/genete/my-test-area/blob/direct_draw/src/cairo_sample.c
[2] http://cairographics.org/threaded_animation_with_cairo/

--
Carlos
http://synfig.org



--
Carlos
http://synfig.org



--
Carlos
http://synfig.org
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.cairographics.org/archives/cairo/attachments/20130114/d4ff9b37/attachment-0001.html>


More information about the cairo mailing list