多线程,gtk3和cairo:绘制在cairo表面

时间:2012-07-26 11:48:24

标签: c drawing cairo gtk3 gdk

我正试图从一个线程中绘制一个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 );
}

任何帮助表示赞赏!

1 个答案:

答案 0 :(得分:2)

好吧,似乎不可能重复使用表面和cr。我将这些调用移到gdk_threads_enter() - 循环的一部分,然后它工作。 (此外,还有一个错误,我通过执行* rgbptr = 0xff写入A通道,我忘了将指针后置。)