2
votes

I am making a Gtk2 program that renders a pre-calculated data heatmap image to a GtkDrawingArea. The procedure is generally like this:

  • generate heatmap image on a Cairo image surface;
  • connect expose event of the GtkDrawingArea;
  • in expose callback, following functions are called:

in first function, clear previous drawing to plain grey color:

cairo_t* cr = gdk_cairo_create(widget->window);
cairo_set_source_rgb(cr, 0.5, 0.5, 0.5);
cairo_paint(cr);
cairo_destroy(cr);

in the second function, paint the pre-rendered image:

cairo_t* cr = gdk_cairo_create(widget->window);
cairo_set_source_surface(cr, buffer_surface, 0, 0);
cairo_paint(cr);
cairo_destroy(cr);

in 3rd function, plot some vector indications:

cairo_t* cr = gdk_cairo_create(widget->window);
cairo_move_to(cr, xxx, xxx);
cairo_line_to(cr, xxx, xxx);
cairo_line_to(cr, xxx, xxx);
cairo_line_to(cr, xxx, xxx);
......
cairo_stroke(cr);
cairo_destroy(cr);

However, only the 1st and 3rd function has effect, the image painting in 2nd function is not shown: resultant GtkDrawingArea only have mid-grey background with black lines.

I am sure the source image surface is filled with content, as I have exported it to png file and it does have content. Why it is not showing in GUI?

1

1 Answers

2
votes

This is caused by unmatched color depth between Cairo image surface and GDK window. I created the image surface in 32-bit ARGB, but the window is RGB. After I modified the image surface from ARGB to RGB, everything solved.