我正在尝试从线程中绘制到 cairo 图像表面,但出现断言错误:
gtk_mt:/build/buildd/cairo-1.10.2/src/cairo-surface.c:385:_cairo_surface_begin_modification:断言`!表面->完成'失败。中止(核心转储)
这是我的程序:
#include <stdlib.h>
#include <gtk/gtk.h>
#include <gdk/gdk.h>
#include <cairo.h>
#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() & 0xff;
rgb_g = random() & 0xff;
rgb_b = random() & 0xff;
for(y = 0; y < height; ++y)
{
rgbptr = &image_data[y * stride];
if(!(random() & 0x3f))
{
rgb_r = random() & 0xff;
rgb_g = random() & 0xff;
rgb_b = random() & 0xff;
}
for(x = 0; x < 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(&argc, &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, &error);
if(!thread)
{
g_print("Error: %s\n", error->message);
return(-1);
}
gtk_main();
gdk_threads_leave();
return( 0 );
}
任何帮助表示赞赏!