<div dir="ltr">Hi!<div style>looks like it doesn't work. I've added XInitThreads() and it still crashing. Also added XLockDisplay and XUnlockDisplay with the same result.</div><div style>Any idea?</div><div style>Thanks!</div>
<div style><p style="margin:0px;font-size:11px;font-family:Menlo;min-height:13px"><br></p>
<p style="margin:0px;font-size:11px;font-family:Menlo"><span style="color:rgb(187,44,162)">int</span> main (<span style="color:rgb(187,44,162)">int</span> argc, <span style="color:rgb(187,44,162)">char</span> *argv[])</p>

<p style="margin:0px;font-size:11px;font-family:Menlo">{</p><p style="margin:0px;font-size:11px;font-family:Menlo"><span style="color:rgb(187,44,162)">    if</span><span style="color:rgb(0,0,0)">(!</span><span style="color:rgb(61,29,129)">XInitThreads</span><span style="color:rgb(0,0,0)">())</span></p>

<p style="margin:0px;font-size:11px;font-family:Menlo;color:rgb(187,44,162)">         return<span style="color:rgb(0,0,0)"> </span><span style="color:rgb(39,42,216)">0</span><span style="color:rgb(0,0,0)">;</span></p><p style="margin:0px;font-size:11px;font-family:Menlo;color:rgb(187,44,162)">
<span style="color:rgb(0,0,0)"><br></span></p>
<p style="margin:0px;font-size:11px;font-family:Menlo;color:rgb(61,29,129)">    gdk_threads_init<span style="color:rgb(0,0,0)">();</span><span style="white-space:pre">   </span></p><p style="margin:0px;font-size:11px;font-family:Menlo;color:rgb(61,29,129)">
    gdk_threads_enter<span style="color:rgb(0,0,0)">();</span></p>
<p style="margin:0px;font-size:11px;font-family:Menlo;min-height:13px"><span style="white-space:pre">   </span></p><p style="margin:0px;font-size:11px;font-family:Menlo;min-height:13px"><span style="color:rgb(61,29,129)">    gtk_init</span>(&argc, &argv);</p>

<p style="margin:0px;font-size:11px;font-family:Menlo;min-height:13px"><span style="color:rgb(112,61,170)"><br></span></p><p style="margin:0px;font-size:11px;font-family:Menlo;min-height:13px"><span style="color:rgb(112,61,170)">    GtkWidget</span><span style="color:rgb(0,0,0)"> *window = </span><span style="color:rgb(61,29,129)">gtk_window_new</span><span style="color:rgb(0,0,0)">(</span><span style="color:rgb(61,29,129)">GTK_WINDOW_TOPLEVEL</span><span style="color:rgb(0,0,0)">);</span><br>
</p>
<p style="margin:0px;font-size:11px;font-family:Menlo;min-height:13px"><span style="color:rgb(120,73,42)">    g_signal_connect</span><span style="color:rgb(0,0,0)">(window, </span><span style="color:rgb(209,47,27)">"destroy"</span><span style="color:rgb(0,0,0)">, </span><span style="color:rgb(120,73,42)">G_CALLBACK</span><span style="color:rgb(0,0,0)">(</span><span style="color:rgb(61,29,129)">gtk_main_quit</span><span style="color:rgb(0,0,0)">), </span><span style="color:rgb(187,44,162)">NULL</span><span style="color:rgb(0,0,0)">);</span></p>

<p style="margin:0px;font-size:11px;font-family:Menlo;min-height:13px"><span style="color:rgb(61,29,129)">    gtk_window_set_title</span><span style="color:rgb(0,0,0)">(</span><span style="color:rgb(120,73,42)">GTK_WINDOW</span><span style="color:rgb(0,0,0)">(window), </span><span style="color:rgb(209,47,27)">"cairo_gl"</span><span style="color:rgb(0,0,0)">);</span></p>

<p style="margin:0px;font-size:11px;font-family:Menlo;color:rgb(61,29,129)">    gtk_window_set_default_size<span style="color:rgb(0,0,0)">(</span><span style="color:rgb(120,73,42)">GTK_WINDOW</span><span style="color:rgb(0,0,0)">(window), </span><span style="color:rgb(39,42,216)">400</span><span style="color:rgb(0,0,0)">, </span><span style="color:rgb(39,42,216)">300</span><span style="color:rgb(0,0,0)">);</span></p>

<p style="margin:0px;font-size:11px;font-family:Menlo;color:rgb(61,29,129)">    gtk_widget_set_app_paintable<span style="color:rgb(0,0,0)">(window, </span><span style="color:rgb(187,44,162)">TRUE</span><span style="color:rgb(0,0,0)">);</span></p>

<p style="margin:0px;font-size:11px;font-family:Menlo;color:rgb(61,29,129)">    gtk_widget_set_double_buffered<span style="color:rgb(0,0,0)">(window, </span><span style="color:rgb(187,44,162)">FALSE</span><span style="color:rgb(0,0,0)">);</span></p>
<p style="margin:0px;font-size:11px;font-family:Menlo;color:rgb(61,29,129)"><span style="color:rgb(0,0,0)"><br></span></p>
<p style="margin:0px;font-size:11px;font-family:Menlo;color:rgb(61,29,129)">    gtk_widget_show_all<span style="color:rgb(0,0,0)">(window);</span></p>
<p style="margin:0px;font-size:11px;font-family:Menlo;min-height:13px"><span class="" style="white-space:pre">        </span></p>
<p style="margin:0px;font-size:11px;font-family:Menlo;color:rgb(49,89,93)"><span style="color:rgb(79,129,135)">    window_surface</span><span style="color:rgb(0,0,0)">=</span>create_source_surface_for_widget<span style="color:rgb(0,0,0)">(window);</span></p>

<p style="margin:0px;font-size:11px;font-family:Menlo;min-height:13px"><span class="" style="white-space:pre">        </span></p>
<p style="margin:0px;font-size:11px;font-family:Menlo">    (<span style="color:rgb(187,44,162)">void</span>)<span style="color:rgb(61,29,129)">g_timeout_add</span>(<span style="color:rgb(39,42,216)">33</span>, (<span style="color:rgb(112,61,170)">GSourceFunc</span>)<span style="color:rgb(49,89,93)">timer_exe</span>, window);</p>

<p style="margin:0px;font-size:11px;font-family:Menlo;min-height:13px"><span class="" style="white-space:pre">        </span></p>
<p style="margin:0px;font-size:11px;font-family:Menlo;color:rgb(0,132,0)">    //do_draw(NULL);</p>
<p style="margin:0px;font-size:11px;font-family:Menlo;min-height:13px"><br></p>
<p style="margin:0px;font-size:11px;font-family:Menlo;color:rgb(61,29,129)">    gtk_main<span style="color:rgb(0,0,0)">();</span></p>
<p style="margin:0px;font-size:11px;font-family:Menlo;min-height:13px"><br></p>
<p style="margin:0px;font-size:11px;font-family:Menlo;color:rgb(61,29,129)">    gdk_threads_leave<span style="color:rgb(0,0,0)">();</span></p>
<p style="margin:0px;font-size:11px;font-family:Menlo;color:rgb(187,44,162)">    return<span style="color:rgb(0,0,0)"> </span><span style="color:rgb(39,42,216)">0</span><span style="color:rgb(0,0,0)">;</span></p>
<p style="margin:0px;font-size:11px;font-family:Menlo">}</p><div><br></div></div><div style><br></div><div style><p style="margin:0px;font-size:11px;font-family:Menlo"><span style="color:rgb(187,44,162)">void</span> *do_draw(<span style="color:rgb(187,44,162)">void</span> *ptr)</p>
<p style="margin:0px;font-size:11px;font-family:Menlo">{</p>
<p style="margin:0px;font-size:11px;font-family:Menlo;min-height:13px"><span class="" style="white-space:pre">        </span></p>
<p style="margin:0px;font-size:11px;font-family:Menlo;color:rgb(79,129,135)"><span style="color:rgb(0,0,0)">    </span>rendering<span style="color:rgb(0,0,0)">=</span><span style="color:rgb(187,44,162)">TRUE</span><span style="color:rgb(0,0,0)">;</span></p>

<p style="margin:0px;font-size:11px;font-family:Menlo;min-height:13px"><span class="" style="white-space:pre">        </span></p>
<p style="margin:0px;font-size:11px;font-family:Menlo">    <span style="color:rgb(112,61,170)">cairo_t</span> *cr = <span style="color:rgb(61,29,129)">cairo_create</span>(<span style="color:rgb(79,129,135)">window_surface</span>);</p>

<p style="margin:0px;font-size:11px;font-family:Menlo"><font color="#000000"><span style="white-space:pre">    </span></font><span style="color:rgb(187,44,162)">if</span><span style="color:rgb(0,0,0)">(</span><font color="#3d1d81">cairo_status</font><span style="color:rgb(0,0,0)">(cr))</span></p>

<p style="margin:0px;font-size:11px;font-family:Menlo;color:rgb(187,44,162)"><span style="color:rgb(0,0,0)"><span class="" style="white-space:pre">   </span></span>return<span style="color:rgb(0,0,0)"> </span>NULL<span style="color:rgb(0,0,0)">;</span></p>

<p style="margin:0px;font-size:11px;font-family:Menlo;min-height:13px"><span class="" style="white-space:pre">        </span></p>
<p style="margin:0px;font-size:11px;font-family:Menlo"><font color="#000000"><span style="white-space:pre">    </span></font><span style="color:rgb(112,61,170)">Display</span><span style="color:rgb(0,0,0)"> *dpy =</span><font color="#3d1d81">cairo_glx_device_get_display</font><span style="color:rgb(0,0,0)">((</span><font color="#3d1d81">cairo_surface_get_device</font><span style="color:rgb(0,0,0)">(</span><span style="color:rgb(79,129,135)">window_surface</span><span style="color:rgb(0,0,0)">)));</span></p>

<p style="margin:0px;font-size:11px;font-family:Menlo;min-height:13px"><span class="" style="white-space:pre">        </span></p>
<p style="margin:0px;font-size:11px;font-family:Menlo;color:rgb(0,132,0)"><span style="color:rgb(0,0,0)">    </span><span style="color:rgb(61,29,129)">XLockDisplay</span><span style="color:rgb(0,0,0)">(dpy);</span><br></p>

<p style="margin:0px;font-size:11px;font-family:Menlo;color:rgb(0,132,0)"><span style="color:rgb(0,0,0)">    </span>//do some time-consuming drawing here ...</p><p style="margin:0px;font-size:11px;font-family:Menlo;min-height:13px">
</p><p style="margin:0px;color:rgb(0,132,0)"><span style="color:rgb(0,0,0)">    </span>// ...</p><div><p style="margin:0px;color:rgb(0,132,0)"><span style="color:rgb(0,0,0)">    </span>// ...</p></div><div><br></div><p></p>

<p style="margin:0px;font-size:11px;font-family:Menlo"><font color="#000000"><span style="white-space:pre">    </span></font><span style="color:rgb(61,29,129)">cairo_destroy</span><span style="color:rgb(0,0,0)">(cr);</span><br>
</p>
<p style="margin:0px;font-size:11px;font-family:Menlo;min-height:13px"><span style="color:rgb(61,29,129)"><br></span></p><p style="margin:0px;font-size:11px;font-family:Menlo;min-height:13px"><span style="color:rgb(61,29,129)">    cairo_gl_surface_swapbuffers</span><span style="color:rgb(0,0,0)"> (</span><span style="color:rgb(79,129,135)">window_surface</span><span style="color:rgb(0,0,0)">);</span><br>
</p>
<p style="margin:0px;font-size:11px;font-family:Menlo;min-height:13px"><span class="" style="white-space:pre">        </span></p>
<p style="margin:0px;font-size:11px;font-family:Menlo"><font color="#000000"><span style="white-space:pre">    </span></font><span style="color:rgb(61,29,129)">XUnlockDisplay<span style="color:rgb(0,0,0)">(dpy);</span></span></p>

<p style="margin:0px;font-size:11px;font-family:Menlo;min-height:13px"><span class="" style="white-space:pre">        </span></p>
<p style="margin:0px;font-size:11px;font-family:Menlo"><font color="#000000"><span style="white-space:pre">    </span></font><span style="color:rgb(79,129,135)">rendering<span style="color:rgb(0,0,0)">=</span><span style="color:rgb(187,44,162)">FALSE</span><span style="color:rgb(0,0,0)">;</span></span></p>

<p style="margin:0px;font-size:11px;font-family:Menlo;min-height:13px"><br></p>
<p style="margin:0px;font-size:11px;font-family:Menlo;color:rgb(187,44,162)"><span style="color:rgb(0,0,0)">    </span>return<span style="color:rgb(0,0,0)"> </span>NULL<span style="color:rgb(0,0,0)">;</span></p>
<p style="margin:0px;font-size:11px;font-family:Menlo">}</p><div><br></div></div><div style><br></div></div><div class="gmail_extra"><br><br><div class="gmail_quote">2013/1/11 Carlos López González <span dir="ltr"><<a href="mailto:genetita@gmail.com" target="_blank">genetita@gmail.com</a>></span><br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr">Thanks Henry, I'll give it a try.<br></div><div class="gmail_extra"><br><br><div class="gmail_quote">
2013/1/11 Henry (Yu) Song - SISA <span dir="ltr"><<a href="mailto:hsong@sisa.samsung.com" target="_blank">hsong@sisa.samsung.com</a>></span><div><div class="h5"><br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Hi,<br>
<br>
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.<br>


<br>
We will resolve that soon in cairo for gl backend, I hope.<br>
<br>
Henry<br>
________________________________________<br>
From: cairo-bounces+henry.song=<a href="mailto:samsung.com@cairographics.org" target="_blank">samsung.com@cairographics.org</a> [cairo-bounces+henry.song=<a href="mailto:samsung.com@cairographics.org" target="_blank">samsung.com@cairographics.org</a>] on behalf of Carlos López González [<a href="mailto:genetita@gmail.com" target="_blank">genetita@gmail.com</a>]<br>


Sent: Thursday, January 10, 2013 11:26 AM<br>
To: <a href="mailto:cairo@cairographics.org" target="_blank">cairo@cairographics.org</a><br>
Subject: [cairo] Threaded animation with cairo_gl backend over a GtkWidget<br>
<div><div><br>
Hi!<br>
I've made a modified version [1] of the example of threaded animation using Cairo [2].<br>
<br>
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.<br>
<br>
Also, for simplicity, I removed all the handled signals except the destroy one.<br>
<br>
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.<br>
<br>
So far, for a single drawing execution it works as expected:<br>
1) In main() create the window_surface<br>
2) The do_draw function creates a context based on window_surface<br>
3) Once done the drawings with Cairo then the buffers are swapped and the content is shown on screen.<br>
<br>
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.<br>
<br>
The problem comes when I enable the multithreading support by uncommenting the line:<br>
<br>
<br>
//(void)g_timeout_add(33, (GSourceFunc)timer_exe, window);<br>
<br>
It triggers the timer_exe function 33 times per second and then it creates threads that will call do_draw each time.<br>
<br>
Once enabled it crashes at the first painting function from Cairo.<br>
<br>
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.<br>
<br>
How can I solve this? If I enclose the Cairo painting operations inside a gdk_threads_enter and<br>
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.<br>
<br>
Any help? I would like to know at last, if my assumptions of the window_surface ownership are true or not.<br>
<br>
Thanks!<br>
<br>
[1] <a href="https://github.com/genete/my-test-area/blob/direct_draw/src/cairo_sample.c" target="_blank">https://github.com/genete/my-test-area/blob/direct_draw/src/cairo_sample.c</a><br>
[2] <a href="http://cairographics.org/threaded_animation_with_cairo/" target="_blank">http://cairographics.org/threaded_animation_with_cairo/</a><br>
<br>
--<br>
Carlos<br>
<a href="http://synfig.org" target="_blank">http://synfig.org</a><br>
</div></div></blockquote></div></div></div><span class="HOEnZb"><font color="#888888"><br><br clear="all"><br>-- <br>Carlos<br><a href="http://synfig.org" target="_blank">http://synfig.org</a><br>
</font></span></div>
</blockquote></div><br><br clear="all"><div><br></div>-- <br>Carlos<br><a href="http://synfig.org" target="_blank">http://synfig.org</a><br>
</div>