[cairo] How to copy one cairo_t's context to another?

Kalle Vahlman kalle.vahlman at gmail.com
Mon Aug 13 03:54:41 PDT 2007

2007/8/13, Magicloud Magiclouds <magicloud.magiclouds at gmail.com>:
> Dear,
>     I created a cairo_t from: layer =
> cairo_create(cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
> rect->width, rect->height)).

First, if you really don't store the surface pointer, you'll leak the
surface that you create.

> And draw something on it. Then I want to
> copy them to the main cairo_t. How to do that?

This works by setting the source of the operations to the surface
which the main cairo context is targeted to. So, something like

  surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
rect->width, rect->height);
  layer = cairo_create(surface);
  ...drawing stuff here..
  cairo_set_source_surface(main_cairo_context, surface, 0, 0);
  cairo_paint(main_cairo_context); /* or draw a path and fill() it or
whatever */

should work.

Remember that you need to destroy the surface after you don't need it anymore.

Also, if the surface you create is short-lived (like for
double-buffering), you could use cairo_push_group() and
cairo_pop_group() instead:


Kalle Vahlman, zuh at iki.fi
Powered by http://movial.fi
Interesting stuff at http://syslog.movial.fi

More information about the cairo mailing list