[cairo] cairo/gdk + clipping

Benjamin Otte otte at redhat.com
Wed Aug 18 06:49:54 PDT 2010

A Gtk widget's expose event always prompts you to repaint all of the
region provided completely. You cannot assume anything about what you
previously rendered.

So if you render everything with Cairo, what you usually want to for
creating your Cairo context is run code like this:

cr = gdk_cairo_create (event->window);
gdk_cairo_region (cr, event->region);
cairo_clip (cr);

Then you end up with a region that is properly clipped to the area that
actually needs to be repainted. But of course, you will still need to
paint everything in that given region.
Cairo will however be smart enough to ignore rectangles that are outside
of that region automatically, because they are clipped.

If that still doesn't turn out to be fast enough, you could maintain a
backing surface that you update and then just cairo_paint() to the
screen in the expose event. But I would be surprised if a few rectangles
caused any trouble.


On Wed, 2010-08-18 at 11:46 +0100, James Morris wrote:
> Hi,
> I'm having some difficulty with understanding cairo and gtk and clipping.
> I need to draw rectangles that appear and disappear over time. I've
> setup a gtk timeout which calls gtk_queue_draw on the window and the
> drawable in use by cairo as the surface - this provokes my expose
> event handler to be called. Here I call gdk_cairo_create, do my
> drawing, and then cairo_destroy.
> I asked about this on gtk-app-devel and had some responses but still
> seem to be stuck with erasure of what has been previously drawn each
> time something new is drawn (ie a new rectangle appears after some
> period of time has elapsed, drawing it erases all past rectangles).
> The solution seemed to be clipping. So I clip_preserve the rectangle
> and then fill it, but still everything else is erased.
> It was suggested I could dispense with the timeout. I don't think so,
> as the rectangles are dictated by another thread which only provides
> the dimensions of a rectangle when it should appear/disappear (getting
> this information in advance is not possible).
> My first working approach was to store in a list all the rectangles
> current in existence (and remove them when the disappear). Then on
> each call to the expose event handler the surface background and every
> rectangle would be drawn. With the timeout set at 33ms, and on average
> 200 rectangles appearing/disappearing per second, this approach proved
> too expensive of CPU resources. I decided then to try GDK and
> performance was a *lot* better and satisfactory, but then I discovered
> the GDK drawing and GDKGC functions are deprecated, hence a return to
> Cairo.
> Can anyone advise here please?
> Cheers,
> James
> --
> cairo mailing list
> cairo at cairographics.org
> http://lists.cairographics.org/mailman/listinfo/cairo

More information about the cairo mailing list