3
votes

So, I want to draw an image using PyCairo. Efficiency is pretty important, but I am loading the images using PyGtk's gtk.gdk.Pixbuf. My question is, is there a performance difference in rendering the Pixbuf through retrieving a Cairo context through gtk.gdk.CairoContext vs converting the Pixbuf into a cairo.ImageSurface object, and if so, how big a difference is it? This is run in a separate drawing thread that is updated 30 times a second or so (though only when the image needs updating). Note that the conversion would only be run once.

gdkcr = gtk.gdk.CairoContext(cr)

gdkcr.set_source_pixbuf(img, 0, 0)
gdkcr.paint()

vs

cr.set_source(imgSurface)
cr.rectangle(0, 0, imgSurface.get_width(), imgSurface.get_height())
cr.fill()
1
How would you convert the pixbuf into a cairo.ImageSurface? Wouldn't that need set_source_pixbuf(), too?Uli Schlachter

1 Answers

3
votes

As @UliSchlachter asked, how do you get an ImageSurface in your second solution? I can only guess that it involves .set_source_pixbuf() and .get_source().get_surface().

The way pixbuf and cairo store pixels in memory is not the same (RGB(A) vs. ARGB), so set_source_pixbuf() has to copy the image pixel-by-pixel while shuffling bits around. This is relatively fast but still has a cost. If the image is always the same, it should be easy to do the conversion once, outside of the loop.