Avoiding seams (was: [cairo] Best strategy for complex screen drawing)

Timothée Lecomte timothee.lecomte at ens.fr
Mon Nov 28 14:29:38 PST 2005


Hi all !

I would like to sum up the situation about my speed issues using cairo.
Alexander Larsson explained that it was a bad idea to use fill and
stroke to avoid seams between polygons, but suggested to use an
intermediate surface drawn with CAIRO_OPERATOR_ADD.

Here are peaces of my code :

Somewhere before actual drawing, I create the intermediate drawing surface :
    cairo_surface_t *polygon_surface = cairo_surface_create_similar(
cairo_get_target(cr),
                                                                       
  CAIRO_CONTENT_COLOR_ALPHA,
                                                                       
  current_xmax, current_ymax);
    polygon_cr = cairo_create (polygon_surface);
    cairo_set_operator (polygon_cr, CAIRO_OPERATOR_ADD); /* or SATURATE,
or OVER for testing purpose */

Later, I draw polygons with :
    cr = polygon_cr;
    cairo_move_to(cr, device_x(corners[0].x), device_y(corners[0].y));

    for (int i=1;i<n;i++)
        cairo_line_to(cr, device_x(corners[i].x), device_y(corners[i].y));

    cairo_close_path(cr);

    wxt_cairo_fill(fillstyle, fillpar ); /* set a plain color via
cairo_set_source_rgb() */
    cairo_fill(cr);
    cr = main_cr;

And finally, I copy my polygon surface to the main cairo context :
    cairo_destroy (polygon_cr);
    cairo_set_source_surface (cr, polygon_surface, 0, 0);
    cairo_paint (cr);
    cairo_surface_destroy (polygon_surface);

Let's talk about results :

* desired output (obtained with cairo_fill_preserve and cairo_stroke ):
       http://tipote.free.fr/wxt-desired.png
    No seams between polygons, but really slow as expected.

* with CAIRO_OPERATOR_OVER :
       http://tipote.free.fr/wxt-over.png
    As expected, seams between polygons.

* with CAIRO_OPERATOR_ADD :
       http://tipote.free.fr/wxt-add.png
    No seam, but problems of 'transparency' induced by the ADD operation

* with CAIRO_OPERATOR_SATURATE :
       http://tipote.free.fr/wxt-saturate.png
    No seam, but first-drawn polygons hide last ones, so to get the
desired output, it would imply to render front-to-back and it would add
a lot of complexity to my code...

Can someone help me to sort all these things and get both speed and
accuracy ?

Thanks for your consideration and time.

Timothée Lecomte


More information about the cairo mailing list