[cairo] Public API for pattern duplication

Chris Wilson chris at chris-wilson.co.uk
Sat Mar 3 04:04:44 PST 2012

On Sat, 3 Mar 2012 08:48:32 +0100, Andrea Canciani <ranma42 at gmail.com> wrote:
> On Fri, Mar 2, 2012 at 8:47 PM, Mark Vender <markv743 at yahoo.co.uk> wrote:
> > On 02/27/2012 08:46 PM, Chris Wilson wrote:
> >>
> >> On Mon, 27 Feb 2012 20:23:20 +0200, Mark Vender<markv743 at yahoo.co.uk>
> >>  wrote:
> >>>
> >>> Could someone indicate whether this function is still welcome to cairo?
> >>> If the problem is that just nobody else is interested, I could easily
> >>> contribute a patch adding this functionality myself.
> >>
> >>
> >> If you have a concrete use case and such functionality will be useful to
> >> you, please do submit the patch to give a public entry point to
> >> _cairo_pattern_create_copy(). Be prepared to justify your use case
> >> though. ;)
> >> -Chris
> >>
> >
> > I have a fairly simple use case, in which it is almost impossible to
> > workaround the lack of pattern copying routine. I have several threads, that
> > do drawing on a separate surfaces using a separate contexts. An equivalent
> > pattern needs to be passed to each of the threads. The pattern
> > transformation matrix is modified during the drawing, therefore I can't use
> > a single pattern without adding much complexity to the code and incurring a
> > performance penalty. I don't use surface patterns, so
> > _cairo_pattern_create_copy would solve this issue completely for me.
> I was about to suggest setting the source, saving the context and
> transforming the pattern, but then I realized that saving the context
> does not perform a copy of the pattern.
> This means that when the context is restored, the source might be different.
> Chris, is this by-design?

Not really, the design is that patterns are immutable once set on the
context and really we should be taking a snapshot of the user pattern
during cairo_set_source(). The reason why doing so was neglected, I
presume, is to avoid the overhead of that copy. Also the current method
of using a reference has leaked into applications as can be seen above
where the pattern is modified after being used (or even whilst set).
Whilst such usage works with image/xlib/quartz/win32 it breaks with
ps/pdf/svg/script etc.

Note that snapshotting during set-source alone does not solve all the
thread-safety issues, the application itself has to synchronise its
modifications to the pattern before the threads start using it as a

An alternative method in this case would be to use the same base pattern
between all threads and adjust the CTM around each set-source.

Chris Wilson, Intel Open Source Technology Centre

More information about the cairo mailing list