Search code examples
graphics2dflushcairoframe-rate

cairo surface flushes only 1 fps


I have constructed a cairo (v1.12.16) image surface with:

surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, size.width, size.height);

and for 60 fps; cleared it, drew stuff and flushed with:

cairo_surface_flush(surface);

then, got the resulting canvas with:

unsigned char * data = cairo_image_surface_get_data(surface);

but the resulting data variable was only modified (approximately) every second, not 60 times a second. I got the same (unexpected) result even when using cairo's quartz backend... Are there any flush/refresh rate settings in cairo that I am not (yet) aware of?

Edit: I am just trying to draw some filled (random and/or calculated) rectangles; tested 100 to 10K rects in each frame. All related code is run in the same (display?) thread. I am not caching the 'data' variable. I even modified one corner of it to flicker and I could see flickers in 60fps (for 100 rects) and 2-3 fps (for 10K rects); meaning the 'data' variable returned is not refreshed!? In a different project using cairo's quartz backend, I got the same 1 fps result!??

Edit2: The culprit turned out to be the time() function; when used in srand(time(NULL)) it was producing the same random variables in the same second; used srand(std::clock()) instead. Thanks to the quick response/reply (and it still answers my question!!)..


Solution

  • No there are no such flush/refresh rate settings. Cairo draws everything you tell it to and then just returns control.

    I have two ideas:

    • Either cairo is drawing fast enough and something else is slowing things down (e.g. your copying the result of the drawing somewhere). You should measure the time that elapses between when you begin drawing and your call to cairo_surface_flush().
    • You are drawing something really, really complex and cairo really does need a second to render this (However, I have no idea how one could accidentally cause such a complex rendering).