[cairo] drawing tons of circles

Chris Wilson chris at chris-wilson.co.uk
Mon Oct 5 08:15:08 PDT 2009


Excerpts from Nicola Fontana's message of Mon Oct 05 15:44:17 +0100 2009:
> Il giorno Mon, 5 Oct 2009 15:26:13 +0200
> Mami <wpmami at gmail.com> ha scritto:
> 
> > I need to draw tons of filled circles (thousand, actually) and I
> > found that using cairo_arc is not the way to go; it lacks the
> > performance I need.
> > 
> > Can you recommend a method for drawing such a lot of objects in an
> > acceptable timeframe?
> 
> If the bottleneck is not in the backend I'd compute only once the circle
> path, rendering it multiple times with different matrices. Something
> like this (not tested):

That still converts the path to a mask multiple times. The trick, if you
are willing to sacrifice sub-pixel accuracy (and there are times when
that is desired or mitigated by other means) is to render the circle to
a 'large' alpha-only similar surface:

mask_surface
    = cairo_surface_create_similar (cairo_get_target (cr),
                                    CAIRO_CONTENT_ALPHA,
				    128, 128);
cr_mask = cairo_create (mask_surface);
cairo_surface_destroy (mask_surface);

cairo_arc (cr_mask, 64, 64, 64, 0, 2 * M_PI);
cairo_fill (cr_mask);
mask = cairo_pattern_create_for_surface (cairo_get_target (cr_mask));
cairo_destroy (cr_mask);

Then to use, something like:
cairo_matrix_init_scale (&m, r/128., r/128.);
cairo_matrix_translate (&m, -x, -y);
cairo_pattern_set_matrix (mask, &m);
cairo_set_source (cr, source);
cairo_mask (cr, mask);

It is tempting to create first class paths to do the same, but that is
not trivial and so the complexity and trade-offs are pushed to the
higher layers.

Hope this helps (and the rushed transformations are correct!).
Have fun with cairo!
-ickle
-- 
Chris Wilson, Intel Open Source Technology Centre


More information about the cairo mailing list