1/*
2 * Copyright (C) 2011 Igalia S.L.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 *    notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 *    notice, this list of conditions and the following disclaimer in the
11 *    documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE COMPUTER, INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#include "config.h"
27#include "PlatformContextCairo.h"
28
29#include <cairo.h>
30
31namespace WebCore {
32
33PlatformContextCairo::PlatformContextCairo(cairo_t* cr)
34    : m_cr(cr)
35{
36}
37
38void PlatformContextCairo::restore()
39{
40    const ImageMaskInformation& maskInformation = m_maskImageStack.last();
41    if (maskInformation.isValid()) {
42        const FloatRect& maskRect = maskInformation.maskRect();
43        cairo_pop_group_to_source(m_cr.get());
44        cairo_mask_surface(m_cr.get(), maskInformation.maskSurface(), maskRect.x(), maskRect.y());
45    }
46    m_maskImageStack.removeLast();
47
48    cairo_restore(m_cr.get());
49}
50
51void PlatformContextCairo::save()
52{
53    m_maskImageStack.append(ImageMaskInformation());
54
55    cairo_save(m_cr.get());
56}
57
58void PlatformContextCairo::pushImageMask(cairo_surface_t* surface, const FloatRect& rect)
59{
60    // We must call savePlatformState at least once before we can use image masking,
61    // since we actually apply the mask in restorePlatformState.
62    ASSERT(!m_maskImageStack.isEmpty());
63    m_maskImageStack.last().update(surface, rect);
64
65    // Cairo doesn't support the notion of an image clip, so we push a group here
66    // and then paint it to the surface with an image mask (which is an immediate
67    // operation) during restorePlatformState.
68
69    // We want to allow the clipped elements to composite with the surface as it
70    // is now, but they are isolated in another group. To make this work, we're
71    // going to blit the current surface contents onto the new group once we push it.
72    cairo_surface_t* currentTarget = cairo_get_target(m_cr.get());
73    cairo_surface_flush(currentTarget);
74
75    // Pushing a new group ensures that only things painted after this point are clipped.
76    cairo_push_group(m_cr.get());
77    cairo_set_operator(m_cr.get(), CAIRO_OPERATOR_SOURCE);
78
79    cairo_set_source_surface(m_cr.get(), currentTarget, 0, 0);
80    cairo_rectangle(m_cr.get(), rect.x(), rect.y(), rect.width(), rect.height());
81    cairo_fill(m_cr.get());
82}
83
84
85} // namespace WebCore
86