| 1 | /* |
| 2 | * Copyright (C) Research In Motion Limited 2009-2010. All rights reserved. |
| 3 | * |
| 4 | * This library is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU Library General Public |
| 6 | * License as published by the Free Software Foundation; either |
| 7 | * version 2 of the License, or (at your option) any later version. |
| 8 | * |
| 9 | * This library is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 12 | * Library General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU Library General Public License |
| 15 | * along with this library; see the file COPYING.LIB. If not, write to |
| 16 | * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
| 17 | * Boston, MA 02110-1301, USA. |
| 18 | */ |
| 19 | |
| 20 | #include "config.h" |
| 21 | #include "RenderSVGResourceMasker.h" |
| 22 | |
| 23 | #include "Element.h" |
| 24 | #include "ElementIterator.h" |
| 25 | #include "FloatPoint.h" |
| 26 | #include "Image.h" |
| 27 | #include "IntRect.h" |
| 28 | #include "SVGRenderingContext.h" |
| 29 | #include <wtf/IsoMallocInlines.h> |
| 30 | |
| 31 | namespace WebCore { |
| 32 | |
| 33 | WTF_MAKE_ISO_ALLOCATED_IMPL(RenderSVGResourceMasker); |
| 34 | |
| 35 | RenderSVGResourceMasker::RenderSVGResourceMasker(SVGMaskElement& element, RenderStyle&& style) |
| 36 | : RenderSVGResourceContainer(element, WTFMove(style)) |
| 37 | { |
| 38 | } |
| 39 | |
| 40 | RenderSVGResourceMasker::~RenderSVGResourceMasker() = default; |
| 41 | |
| 42 | void RenderSVGResourceMasker::removeAllClientsFromCache(bool markForInvalidation) |
| 43 | { |
| 44 | m_maskContentBoundaries = FloatRect(); |
| 45 | m_masker.clear(); |
| 46 | |
| 47 | markAllClientsForInvalidation(markForInvalidation ? LayoutAndBoundariesInvalidation : ParentOnlyInvalidation); |
| 48 | } |
| 49 | |
| 50 | void RenderSVGResourceMasker::removeClientFromCache(RenderElement& client, bool markForInvalidation) |
| 51 | { |
| 52 | m_masker.remove(&client); |
| 53 | |
| 54 | markClientForInvalidation(client, markForInvalidation ? BoundariesInvalidation : ParentOnlyInvalidation); |
| 55 | } |
| 56 | |
| 57 | bool RenderSVGResourceMasker::applyResource(RenderElement& renderer, const RenderStyle&, GraphicsContext*& context, OptionSet<RenderSVGResourceMode> resourceMode) |
| 58 | { |
| 59 | ASSERT(context); |
| 60 | ASSERT_UNUSED(resourceMode, !resourceMode); |
| 61 | |
| 62 | bool missingMaskerData = !m_masker.contains(&renderer); |
| 63 | if (missingMaskerData) |
| 64 | m_masker.set(&renderer, std::make_unique<MaskerData>()); |
| 65 | |
| 66 | MaskerData* maskerData = m_masker.get(&renderer); |
| 67 | AffineTransform absoluteTransform = SVGRenderingContext::calculateTransformationToOutermostCoordinateSystem(renderer); |
| 68 | FloatRect repaintRect = renderer.repaintRectInLocalCoordinates(); |
| 69 | |
| 70 | if (!maskerData->maskImage && !repaintRect.isEmpty()) { |
| 71 | const SVGRenderStyle& svgStyle = style().svgStyle(); |
| 72 | ColorSpace colorSpace = svgStyle.colorInterpolation() == ColorInterpolation::LinearRGB ? ColorSpaceLinearRGB : ColorSpaceSRGB; |
| 73 | // FIXME (149470): This image buffer should not be unconditionally unaccelerated. Making it match the context breaks alpha masking, though. |
| 74 | maskerData->maskImage = SVGRenderingContext::createImageBuffer(repaintRect, absoluteTransform, colorSpace, Unaccelerated, context); |
| 75 | if (!maskerData->maskImage) |
| 76 | return false; |
| 77 | |
| 78 | if (!drawContentIntoMaskImage(maskerData, colorSpace, &renderer)) |
| 79 | maskerData->maskImage.reset(); |
| 80 | } |
| 81 | |
| 82 | if (!maskerData->maskImage) |
| 83 | return false; |
| 84 | |
| 85 | SVGRenderingContext::clipToImageBuffer(*context, absoluteTransform, repaintRect, maskerData->maskImage, missingMaskerData); |
| 86 | return true; |
| 87 | } |
| 88 | |
| 89 | bool RenderSVGResourceMasker::drawContentIntoMaskImage(MaskerData* maskerData, ColorSpace colorSpace, RenderObject* object) |
| 90 | { |
| 91 | GraphicsContext& maskImageContext = maskerData->maskImage->context(); |
| 92 | |
| 93 | // Eventually adjust the mask image context according to the target objectBoundingBox. |
| 94 | AffineTransform maskContentTransformation; |
| 95 | if (maskElement().maskContentUnits() == SVGUnitTypes::SVG_UNIT_TYPE_OBJECTBOUNDINGBOX) { |
| 96 | FloatRect objectBoundingBox = object->objectBoundingBox(); |
| 97 | maskContentTransformation.translate(objectBoundingBox.location()); |
| 98 | maskContentTransformation.scale(objectBoundingBox.size()); |
| 99 | maskImageContext.concatCTM(maskContentTransformation); |
| 100 | } |
| 101 | |
| 102 | // Draw the content into the ImageBuffer. |
| 103 | for (auto& child : childrenOfType<SVGElement>(maskElement())) { |
| 104 | auto renderer = child.renderer(); |
| 105 | if (!renderer) |
| 106 | continue; |
| 107 | if (renderer->needsLayout()) |
| 108 | return false; |
| 109 | const RenderStyle& style = renderer->style(); |
| 110 | if (style.display() == DisplayType::None || style.visibility() != Visibility::Visible) |
| 111 | continue; |
| 112 | SVGRenderingContext::renderSubtreeToImageBuffer(maskerData->maskImage.get(), *renderer, maskContentTransformation); |
| 113 | } |
| 114 | |
| 115 | #if !USE(CG) |
| 116 | maskerData->maskImage->transformColorSpace(ColorSpaceSRGB, colorSpace); |
| 117 | #else |
| 118 | UNUSED_PARAM(colorSpace); |
| 119 | #endif |
| 120 | |
| 121 | // Create the luminance mask. |
| 122 | if (style().svgStyle().maskType() == MaskType::Luminance) |
| 123 | maskerData->maskImage->convertToLuminanceMask(); |
| 124 | |
| 125 | return true; |
| 126 | } |
| 127 | |
| 128 | void RenderSVGResourceMasker::calculateMaskContentRepaintRect() |
| 129 | { |
| 130 | for (Node* childNode = maskElement().firstChild(); childNode; childNode = childNode->nextSibling()) { |
| 131 | RenderObject* renderer = childNode->renderer(); |
| 132 | if (!childNode->isSVGElement() || !renderer) |
| 133 | continue; |
| 134 | const RenderStyle& style = renderer->style(); |
| 135 | if (style.display() == DisplayType::None || style.visibility() != Visibility::Visible) |
| 136 | continue; |
| 137 | m_maskContentBoundaries.unite(renderer->localToParentTransform().mapRect(renderer->repaintRectInLocalCoordinates())); |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | FloatRect RenderSVGResourceMasker::resourceBoundingBox(const RenderObject& object) |
| 142 | { |
| 143 | FloatRect objectBoundingBox = object.objectBoundingBox(); |
| 144 | FloatRect maskBoundaries = SVGLengthContext::resolveRectangle<SVGMaskElement>(&maskElement(), maskElement().maskUnits(), objectBoundingBox); |
| 145 | |
| 146 | // Resource was not layouted yet. Give back clipping rect of the mask. |
| 147 | if (selfNeedsLayout()) |
| 148 | return maskBoundaries; |
| 149 | |
| 150 | if (m_maskContentBoundaries.isEmpty()) |
| 151 | calculateMaskContentRepaintRect(); |
| 152 | |
| 153 | FloatRect maskRect = m_maskContentBoundaries; |
| 154 | if (maskElement().maskContentUnits() == SVGUnitTypes::SVG_UNIT_TYPE_OBJECTBOUNDINGBOX) { |
| 155 | AffineTransform transform; |
| 156 | transform.translate(objectBoundingBox.location()); |
| 157 | transform.scale(objectBoundingBox.size()); |
| 158 | maskRect = transform.mapRect(maskRect); |
| 159 | } |
| 160 | |
| 161 | maskRect.intersect(maskBoundaries); |
| 162 | return maskRect; |
| 163 | } |
| 164 | |
| 165 | } |
| 166 | |