| 1 | /* |
| 2 | * Copyright (C) 2011 Igalia S.L. |
| 3 | * Copyright (c) 2008, Google Inc. All rights reserved. |
| 4 | * Copyright (c) 2012, Intel Corporation |
| 5 | * |
| 6 | * Redistribution and use in source and binary forms, with or without |
| 7 | * modification, are permitted provided that the following conditions |
| 8 | * are met: |
| 9 | * 1. Redistributions of source code must retain the above copyright |
| 10 | * notice, this list of conditions and the following disclaimer. |
| 11 | * 2. Redistributions in binary form must reproduce the above copyright |
| 12 | * notice, this list of conditions and the following disclaimer in the |
| 13 | * documentation and/or other materials provided with the distribution. |
| 14 | * |
| 15 | * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY |
| 16 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 17 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 18 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR |
| 19 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 20 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 21 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 22 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY |
| 23 | * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 24 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 25 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 26 | */ |
| 27 | |
| 28 | #include "config.h" |
| 29 | #include "PlatformContextCairo.h" |
| 30 | |
| 31 | #if USE(CAIRO) |
| 32 | |
| 33 | #include "CairoUtilities.h" |
| 34 | #include "Gradient.h" |
| 35 | #include "GraphicsContext.h" |
| 36 | #include "Pattern.h" |
| 37 | #include <cairo.h> |
| 38 | |
| 39 | namespace WebCore { |
| 40 | |
| 41 | // In Cairo image masking is immediate, so to emulate image clipping we must save masking |
| 42 | // details as part of the context state and apply them during platform restore. |
| 43 | class ImageMaskInformation { |
| 44 | public: |
| 45 | void update(cairo_surface_t* maskSurface, const FloatRect& maskRect) |
| 46 | { |
| 47 | m_maskSurface = maskSurface; |
| 48 | m_maskRect = maskRect; |
| 49 | } |
| 50 | |
| 51 | bool isValid() const { return m_maskSurface; } |
| 52 | cairo_surface_t* maskSurface() const { return m_maskSurface.get(); } |
| 53 | const FloatRect& maskRect() const { return m_maskRect; } |
| 54 | |
| 55 | private: |
| 56 | RefPtr<cairo_surface_t> m_maskSurface; |
| 57 | FloatRect m_maskRect; |
| 58 | }; |
| 59 | |
| 60 | |
| 61 | // Encapsulates the additional painting state information we store for each |
| 62 | // pushed graphics state. |
| 63 | class PlatformContextCairo::State { |
| 64 | public: |
| 65 | State() = default; |
| 66 | |
| 67 | ImageMaskInformation m_imageMaskInformation; |
| 68 | }; |
| 69 | |
| 70 | PlatformContextCairo::PlatformContextCairo(cairo_t* cr) |
| 71 | : m_cr(cr) |
| 72 | { |
| 73 | m_stateStack.append(State()); |
| 74 | m_state = &m_stateStack.last(); |
| 75 | } |
| 76 | |
| 77 | void PlatformContextCairo::restore() |
| 78 | { |
| 79 | const ImageMaskInformation& maskInformation = m_state->m_imageMaskInformation; |
| 80 | if (maskInformation.isValid()) { |
| 81 | const FloatRect& maskRect = maskInformation.maskRect(); |
| 82 | cairo_pop_group_to_source(m_cr.get()); |
| 83 | cairo_mask_surface(m_cr.get(), maskInformation.maskSurface(), maskRect.x(), maskRect.y()); |
| 84 | } |
| 85 | |
| 86 | m_stateStack.removeLast(); |
| 87 | ASSERT(!m_stateStack.isEmpty()); |
| 88 | m_state = &m_stateStack.last(); |
| 89 | |
| 90 | cairo_restore(m_cr.get()); |
| 91 | } |
| 92 | |
| 93 | PlatformContextCairo::~PlatformContextCairo() = default; |
| 94 | |
| 95 | void PlatformContextCairo::save() |
| 96 | { |
| 97 | m_stateStack.append(State()); |
| 98 | m_state = &m_stateStack.last(); |
| 99 | |
| 100 | cairo_save(m_cr.get()); |
| 101 | } |
| 102 | |
| 103 | void PlatformContextCairo::pushImageMask(cairo_surface_t* surface, const FloatRect& rect) |
| 104 | { |
| 105 | // We must call savePlatformState at least once before we can use image masking, |
| 106 | // since we actually apply the mask in restorePlatformState. |
| 107 | ASSERT(!m_stateStack.isEmpty()); |
| 108 | m_state->m_imageMaskInformation.update(surface, rect); |
| 109 | |
| 110 | // Cairo doesn't support the notion of an image clip, so we push a group here |
| 111 | // and then paint it to the surface with an image mask (which is an immediate |
| 112 | // operation) during restorePlatformState. |
| 113 | |
| 114 | // We want to allow the clipped elements to composite with the surface as it |
| 115 | // is now, but they are isolated in another group. To make this work, we're |
| 116 | // going to blit the current surface contents onto the new group once we push it. |
| 117 | cairo_surface_t* currentTarget = cairo_get_target(m_cr.get()); |
| 118 | cairo_surface_flush(currentTarget); |
| 119 | |
| 120 | // Pushing a new group ensures that only things painted after this point are clipped. |
| 121 | cairo_push_group(m_cr.get()); |
| 122 | cairo_set_operator(m_cr.get(), CAIRO_OPERATOR_SOURCE); |
| 123 | |
| 124 | // To avoid the limit of Pixman backend, we need to reduce the size of pattern matrix |
| 125 | // See https://bugs.webkit.org/show_bug.cgi?id=154283 |
| 126 | cairo_set_source_surface(m_cr.get(), currentTarget, rect.x(), rect.y()); |
| 127 | cairo_translate(m_cr.get(), rect.x(), rect.y()); |
| 128 | cairo_rectangle(m_cr.get(), 0, 0, rect.width(), rect.height()); |
| 129 | cairo_fill(m_cr.get()); |
| 130 | cairo_translate(m_cr.get(), -rect.x(), -rect.y()); |
| 131 | } |
| 132 | |
| 133 | } // namespace WebCore |
| 134 | |
| 135 | #endif // USE(CAIRO) |
| 136 | |