[cairo] Cairo and layered application

Gauthier Quesnel quesnel at lil.univ-littoral.fr
Tue Feb 27 11:11:53 PST 2007


Hi all,

I want to build an application which display some layers on a
gtk.drawingarea like gimp or inkscape for instance, where each layer
have an opacity value. Their is three types of layer:

Background (to fill the pixmap with a colour):

        cairo.save()
        cairo.set_source_rgb(self.__red, self.__green, self.__blue)
        cairo.set_operator(cairo.OPERATOR_SOURCE)
        cairo.paint()
        cairo.set_operator(cairo.OPERATOR_OVER);
        cairo.restore()

A Grid (to draw a grid on the background layer):

        cairo.save()
        cairo.set_source_rgb(self.__red, self.__green, self.__blue)
        cairo.set_line_width(1)
        cellwidth = width / self.column
        cellheight = height / self.row
        for j in range(self.row):
            for i in range(self.column):
                cairo.rectangle(i * cellwidth, j * cellheight, width /
                        self.column, height / self.row)
        cairo.stroke()
        cairo.restore()

And the Matrix layer (a numpy array of float) which can be modified by
the user (mouse click):

        cellwidth = width / self.table.column
        cellheight = height / self.table.row
        cairo.save()
        for j in range(self.table.row):
            for i in range(self.table.column):
                value = self.table.get(i, j)
                cairo.set_source_rgb(self.r * value, self.g * value, 
                        self.b * value)
                cairo.rectangle(i * cellwidth, j * cellheight,
			cellwidth, cellheight)
                cairo.fill()
        cairo.stroke()
        cairo.restore()


With this technique, renders are very slow with five matrix of
40x40. I have added a gdk.pixmap to make a double buffer (all layers
are only redraw on configure event and not expose event). 

I would liked to attach every cairo_context a gdk.pixmap object in
order to compute only the layer modified by the user. But it seems not
possible with the gdk.pixmap object.

Have-you a solution to optimize this type of application?

Gauthier.


More information about the cairo mailing list