[cairo] cairo/gdk + clipping

James Morris james at jwm-art.net
Thu Aug 19 00:40:00 PDT 2010

Hi Ben, thanks for your response. It seems to have come a long time
before my "scrap that I'm trying something else" message which when I
wrote it, no responses had been delivered to me. Must be some lag in
the emails somewhere.

I've decided upon a different approach. I want to turn the backend of
my C code into a library so that I could use C++ for the GUI. Then I
will be able to investigate how FLTK might work for this. I may yet
still come back to GTK+Cairo, depends how things work out.


On 18 August 2010 14:49, Benjamin Otte <otte at redhat.com> wrote:
> 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.
> Benjamin
> 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