[cairo] [cairo-gl] RFC: Dispatching mechanism for non-standard GL functions

Bill Spitzak spitzak at gmail.com
Thu Dec 2 11:31:00 PST 2010


If you really are trying to make it such that users of cairo just have 
to link with libcairo, why not use dlopen to open a cairo-specific 
backend. For the "foo" backend it would look something like this:

cairo_t* cairo_foo_surface_create(x,y,z) {
  lib = dlopen("cairo_foo.so", ...);
  sym = dlsym(lib, "surface_create");
  return sym(x,y,z);
}

For many surfaces all the rest of the operations would be through 
virtual function pointers in the returned cairo_t. This is the same 
redirection of all cairo calls that has been recommended before:

cairo_move_to(cairo_t* cr, x, y) {
   (cr->vtable->move_to)(x,y);
}

If there are back-end-specific operations they could be further symbol 
lookups, but there could also be reserved entries in the table, which 
may help for more complex backends that want to reuse the redirection of 
the table: (inline so there is zero code size if not used)

inline cairo_foo_blah(cr) {
   (cr->vtable->backend_specific_1)();
}

Some other backend-specific operations could be done inline trivially:

inline cairo_xlib_surface_get_width(cs) {
   return ((cairo_xlib_surface*)cs)->width;
}

Just checking the cairo_xlib.h header file it seems there are only two 
symbols to look up with this scheme (surface_create and 
surface_create_for_bitmap), and 9 methods on the surface. In any case 
this is much much less than the 50 or so symbols in xlib that are needed 
to implement cairo.


More information about the cairo mailing list