Using Cairo with OpenGL ES

Cairo is an excellent toolkit for creating 2D textures in OpenGL applications, which is immensely useful for 2D overlays, including user interfaces. You can create a Cairo image surface using “cairo_image_surface_create”, and then use “cairo_image_surface_get_data” as the input for “glTexImage2D”.

Unfortunately, there is a pitfall.

First, note that despite its name, Cairo’s CAIRO_FORMAT_ARGB32 is actually equivalent to OpenGL’s GL_BGRA. (In Pixman, it’s PIXMAN_a8r8g8b8.)

That’s nice if you’re using OpenGL ES 3.0, but OpenGL ES 2.0 does not support BGR, unless you have the EXT_texture_format_BGRA8888 extension, and this extension is not available on many devices.

So, what to do? Well, note that OpenGL’s GL_RGBA format, which OpenGL ES 2.0 does support, is equivalent to PIXMAN_a8b8g8r8. So, Cairo could do what we need, except that this underlying functionality is not normally exposed via the Cairo API. You can solve this by simply patching Cairo to expose this format. I’ve opened a bug about it, where you’ll also see my suggested patch.

Dec 31, 1969, 18:00 CST