Note that there are some explanatory texts on larger screens.

plurals
  1. POMultithreading, gtk3 and cairo: drawing in a cairo surface
    text
    copied!<p>I'm trying to draw into a cairo image surface from a thread and I'm getting an assertion error:</p> <blockquote> <p>gtk_mt: /build/buildd/cairo-1.10.2/src/cairo-surface.c:385: _cairo_surface_begin_modification: Assertion `! surface->finished' failed. Aborted (core dumped)</p> </blockquote> <p>This is my program:</p> <pre><code>#include &lt;stdlib.h&gt; #include &lt;gtk/gtk.h&gt; #include &lt;gdk/gdk.h&gt; #include &lt;cairo.h&gt; #define IMAGE_WIDTH 320 #define IMAGE_HEIGHT 200 gpointer thread_func(gpointer data) { int x, y; unsigned char rgb_r, rgb_g, rgb_b; GtkWidget *widget = (GtkWidget*)data; GdkWindow *window; cairo_t *cr; cairo_format_t format = CAIRO_FORMAT_ARGB32; cairo_surface_t *surface; unsigned char *image_data, *rgbptr; int width = IMAGE_WIDTH; int height = IMAGE_HEIGHT; int stride; gdk_threads_enter(); stride = cairo_format_stride_for_width(format, width); image_data = malloc(stride * height); surface = cairo_image_surface_create_for_data(image_data, format, width, height, stride); window = gtk_widget_get_window(widget); cr = gdk_cairo_create(window); gdk_threads_leave(); while(1) { rgb_r = random() &amp; 0xff; rgb_g = random() &amp; 0xff; rgb_b = random() &amp; 0xff; for(y = 0; y &lt; height; ++y) { rgbptr = &amp;image_data[y * stride]; if(!(random() &amp; 0x3f)) { rgb_r = random() &amp; 0xff; rgb_g = random() &amp; 0xff; rgb_b = random() &amp; 0xff; } for(x = 0; x &lt; width; ++x) { *rgbptr++ = rgb_r; *rgbptr++ = rgb_g; *rgbptr++ = rgb_b; *rgbptr = 0xff; } } gdk_threads_enter(); cairo_set_source_surface(cr, surface, 0, 0); cairo_paint(cr); gdk_threads_leave(); sleep(2); } return( NULL ); } int main(int argc, char **argv) { GtkWidget *window; GtkWidget *darea; GThread *thread; GError *error = NULL; if(! g_thread_supported()) g_thread_init(NULL); gdk_threads_init(); gdk_threads_enter(); gtk_init(&amp;argc, &amp;argv); window = gtk_window_new(GTK_WINDOW_TOPLEVEL); darea = gtk_drawing_area_new(); gtk_widget_set_size_request(darea, IMAGE_WIDTH, IMAGE_HEIGHT); gtk_container_add(GTK_CONTAINER (window), darea); gtk_widget_show_all (window); thread = g_thread_create(thread_func, (gpointer)darea, FALSE, &amp;error); if(!thread) { g_print("Error: %s\n", error-&gt;message); return(-1); } gtk_main(); gdk_threads_leave(); return( 0 ); } </code></pre> <p>Any help appreciated!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload