playing with Cairo

I was recently reading a brilliant post A first-person engine in 265 lines by Hunter Loftis, and instantly wanted to port it to Perl. After doing half of work, though, I figured out that the original code uses alpha blending technique to achieve wall shading, rain, and drawing images with 8-bit transparency channel.

I've used Prima to write the port, even though it doesn't support alpha channel, because native x11 API doesn't do that. Googling led me to XRender that can do the stuff, but that sounded too heavy. However Cairo (which is also used by firefox to implement Canvas rendering) is exactly the tool for the job. So I needed to spend some days creating a bridge between Prima and Cairo, and here it is on github.

The programming itself went unexpectedly smooth, because of the Cairo's API design quality .. Unfortunately the perl demo resulted to be very slow, because perl's own speed - profiling shows that pure-perl calculations eat the biggest part of speed. The next biggest CPU eater is a set of calls to Cairo that draw rain drops - each drop requires two calls, $cairo->rectangle(..) and $cairo->fill and that is also expensive.

So I wonder if there can be done something to it. Any ideas?


Just rewrite to most expensive ones, like rotate, walk, cast, inside, update in Inline::C. Still readable, in the same file. No need to use perl ops for the float ops.

Using B::CC with types or rperl would be next level, but Inline::C should be good enough for the beginning.

Hey Dmitry,

Could this be sped up using PDL? As I'm sure you know, I would be *very* happy to have a Cairo-backed PDL::Graphics::Prima, so anything I can do to move this along I'll do. :-)

Context for those unfamiliar: I wrote PDL bindings to the basic Prima drawing operations called PDL::Drawing::Prima. I used those to write a plotting library called PDL::Graphics::Prima. A similar extension could be written for Cairo, too.

PDL::Graphics::Cairo would be awesome David !! Please do it.

Leave a comment

About Dmitry Karasik

user-pic I blog about Perl.