[cairo] Threaded animation with cairo_gl backend over a GtkWidget

Henry (Yu) Song - SISA hsong at sisa.samsung.com
Thu Jan 10 15:13:43 PST 2013


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.

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: Thursday, January 10, 2013 11:26 AM
To: cairo at cairographics.org
Subject: [cairo] Threaded animation with cairo_gl backend over a GtkWidget

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.


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


More information about the cairo mailing list