2 * Copyright (C) Research In Motion Limited 2009-2010. All rights reserved.
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.
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.
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.
22 #include "core/rendering/svg/RenderSVGResourceMasker.h"
24 #include "core/rendering/svg/RenderSVGResource.h"
25 #include "core/rendering/svg/SVGRenderingContext.h"
26 #include "core/svg/SVGElement.h"
27 #include "platform/graphics/DisplayList.h"
28 #include "platform/graphics/GraphicsContextStateSaver.h"
29 #include "platform/transforms/AffineTransform.h"
30 #include "wtf/Vector.h"
34 const RenderSVGResourceType RenderSVGResourceMasker::s_resourceType = MaskerResourceType;
36 RenderSVGResourceMasker::RenderSVGResourceMasker(SVGMaskElement* node)
37 : RenderSVGResourceContainer(node)
41 RenderSVGResourceMasker::~RenderSVGResourceMasker()
45 void RenderSVGResourceMasker::removeAllClientsFromCache(bool markForInvalidation)
47 m_maskContentDisplayList.clear();
48 m_maskContentBoundaries = FloatRect();
49 markAllClientsForInvalidation(markForInvalidation ? LayoutAndBoundariesInvalidation : ParentOnlyInvalidation);
52 void RenderSVGResourceMasker::removeClientFromCache(RenderObject* client, bool markForInvalidation)
55 markClientForInvalidation(client, markForInvalidation ? BoundariesInvalidation : ParentOnlyInvalidation);
58 bool RenderSVGResourceMasker::applyResource(RenderObject* object, RenderStyle*,
59 GraphicsContext*& context, unsigned short resourceMode)
64 ASSERT_UNUSED(resourceMode, resourceMode == ApplyToDefaultMode);
65 ASSERT_WITH_SECURITY_IMPLICATION(!needsLayout());
67 clearInvalidationMask();
69 FloatRect repaintRect = object->repaintRectInLocalCoordinates();
70 if (repaintRect.isEmpty() || !element()->hasChildNodes())
73 // Content layer start.
74 context->beginTransparencyLayer(1, &repaintRect);
79 void RenderSVGResourceMasker::postApplyResource(RenderObject* object, GraphicsContext*& context,
80 unsigned short resourceMode, const Path*, const RenderSVGShape*)
85 ASSERT_UNUSED(resourceMode, resourceMode == ApplyToDefaultMode);
86 ASSERT_WITH_SECURITY_IMPLICATION(!needsLayout());
88 FloatRect repaintRect = object->repaintRectInLocalCoordinates();
90 const SVGRenderStyle* svgStyle = style()->svgStyle();
92 ColorFilter maskLayerFilter = svgStyle->maskType() == MT_LUMINANCE
93 ? ColorFilterLuminanceToAlpha : ColorFilterNone;
94 ColorFilter maskContentFilter = svgStyle->colorInterpolation() == CI_LINEARRGB
95 ? ColorFilterSRGBToLinearRGB : ColorFilterNone;
98 context->beginLayer(1, CompositeDestinationIn, &repaintRect, maskLayerFilter);
100 // Draw the mask with color conversion (when needed).
101 GraphicsContextStateSaver maskContentSaver(*context);
102 context->setColorFilter(maskContentFilter);
104 drawMaskForRenderer(context, object->objectBoundingBox());
107 // Transfer mask layer -> content layer (DstIn)
109 // Transfer content layer -> backdrop (SrcOver)
113 void RenderSVGResourceMasker::drawMaskForRenderer(GraphicsContext* context, const FloatRect& targetBoundingBox)
117 AffineTransform contentTransformation;
118 SVGUnitTypes::SVGUnitType contentUnits = toSVGMaskElement(element())->maskContentUnitsCurrentValue();
119 if (contentUnits == SVGUnitTypes::SVG_UNIT_TYPE_OBJECTBOUNDINGBOX) {
120 contentTransformation.translate(targetBoundingBox.x(), targetBoundingBox.y());
121 contentTransformation.scaleNonUniform(targetBoundingBox.width(), targetBoundingBox.height());
122 context->concatCTM(contentTransformation);
125 if (!m_maskContentDisplayList)
126 m_maskContentDisplayList = asDisplayList(context, contentTransformation);
127 ASSERT(m_maskContentDisplayList);
128 context->drawDisplayList(m_maskContentDisplayList.get());
131 PassRefPtr<DisplayList> RenderSVGResourceMasker::asDisplayList(GraphicsContext* context,
132 const AffineTransform& contentTransform)
136 // Using strokeBoundingBox (instead of repaintRectInLocalCoordinates) to avoid the intersection
137 // with local clips/mask, which may yield incorrect results when mixing objectBoundingBox and
138 // userSpaceOnUse units (http://crbug.com/294900).
139 context->beginRecording(strokeBoundingBox());
140 for (Node* childNode = element()->firstChild(); childNode; childNode = childNode->nextSibling()) {
141 RenderObject* renderer = childNode->renderer();
142 if (!childNode->isSVGElement() || !renderer)
144 RenderStyle* style = renderer->style();
145 if (!style || style->display() == NONE || style->visibility() != VISIBLE)
148 SVGRenderingContext::renderSubtree(context, renderer, contentTransform);
151 return context->endRecording();
154 void RenderSVGResourceMasker::calculateMaskContentRepaintRect()
156 for (Node* childNode = element()->firstChild(); childNode; childNode = childNode->nextSibling()) {
157 RenderObject* renderer = childNode->renderer();
158 if (!childNode->isSVGElement() || !renderer)
160 RenderStyle* style = renderer->style();
161 if (!style || style->display() == NONE || style->visibility() != VISIBLE)
163 m_maskContentBoundaries.unite(renderer->localToParentTransform().mapRect(renderer->repaintRectInLocalCoordinates()));
167 FloatRect RenderSVGResourceMasker::resourceBoundingBox(const RenderObject* object)
169 SVGMaskElement* maskElement = toSVGMaskElement(element());
172 FloatRect objectBoundingBox = object->objectBoundingBox();
173 FloatRect maskBoundaries = SVGLengthContext::resolveRectangle<SVGMaskElement>(maskElement, maskElement->maskUnitsCurrentValue(), objectBoundingBox);
175 // Resource was not layouted yet. Give back clipping rect of the mask.
176 if (selfNeedsLayout())
177 return maskBoundaries;
179 if (m_maskContentBoundaries.isEmpty())
180 calculateMaskContentRepaintRect();
182 FloatRect maskRect = m_maskContentBoundaries;
183 if (maskElement->maskContentUnitsCurrentValue() == SVGUnitTypes::SVG_UNIT_TYPE_OBJECTBOUNDINGBOX) {
184 AffineTransform transform;
185 transform.translate(objectBoundingBox.x(), objectBoundingBox.y());
186 transform.scaleNonUniform(objectBoundingBox.width(), objectBoundingBox.height());
187 maskRect = transform.mapRect(maskRect);
190 maskRect.intersect(maskBoundaries);