Unfortunately I have to ask because the documentation doesn't specify this very well.
Is the cairo_t
and the cairo_surface_t
life-time limited ?
In many examples or samples found over the web, the surface and the context are almost always recreated (more oftently the context) for each repaint operation.
Actually it seems to work almost finely if I only create the surface and the context once, lazily, like here, when a x11 windows is resized:
void updateWindowSize()
{
if(!display || !_win)
return;
int w = cast(uint) lround(width);
int h = cast(uint) lround(height);
if (!_osSetSizePos)
XResizeWindow(display, _win, w, h);
if (!cairoSurface)
cairoSurface = cairo_xlib_surface_create(display, _win, _visual, w, h);
cairo_xlib_surface_set_size(cairoSurface, w, h);
if (!_cr) _cr = cairo_create(cairoSurface);
_cv.setContext(_cr); // _cv = canvas
}
However the context has to be passed each time to the canvas _cv.setContext(_cr);
otherwise the settings are never applied (color, pen width,...), which is crazy since the context itself never changes.
This completely goes against what I've seen before., including the answers to this Q.
The underlying problem is that if the context is recreated for each redrawing then the operations such as cairo_set_source_rgba
, cairo_set_source
, cairo_set_line_width
, etc., have to be done for each redrawing too, which can be seen as a performance issue.
No, the lifetimes aren't limited (at least by cairo). You can use both for as long as you want. You don't even need to recreate the surface for window resizes, because there is cairo_xlib_surface_set_size()
. (There even is cairo_xlib_surface_set_drawable()
which can change the drawable, but personally I don't like that function.)
However, libraries like GTK might add some requirements. For example, GTK does double buffering and there contexts can't be cached.