Parallel Vector Drawing to a CGBitmapContext
I recently came across a post by Aurimas Gasiulis on Parallel vector graphics rasterization on CPU, which I found pretty intriguing. Gasiulis wrote a vector shape renderer named “Blaze” and made it work across multiple threads. Very cool stuff.
But I was shocked when performance charts were provided, which included comparisons to rendering with Apple’s Core Graphics library. Blaze was around 20x faster at rendering than CG.
This sounds like a wonderful nerd snipe to me.
Does Core Graphics (CG) already do parallel drawing? Not that I’m aware of, at least when it comes to rendering vector shapes. What if we just … draw on multiple threads with the same CGContextRef? That seems like a bad idea and as far as I’m aware, CGBitmapContext isn’t thread-safe. And it turns out that yes - this is a bad idea. Segfaults abound.
CGContextRef
CGBitmapContext
What if we make multiple CGContextRefs for each thread, but they all reference the same backing store? And what if we clip to horizontal stripes like the Blaze renderer does?
Well, that seems to work just fine, and it’s a nice speed boost. A quick little test in Acorn where it rendered 28,200 shapes took about 0.93 seconds single-threaded,