2 * Copyright (C) 2004, 2005, 2007, 2008 Nikolas Zimmermann <zimmermann@kde.org>
3 * Copyright (C) 2004, 2005, 2006, 2007, 2008 Rob Buis <buis@kde.org>
4 * Copyright (C) Research In Motion Limited 2009-2010. All rights reserved.
5 * Copyright (C) 2011 Dirk Schulze <krit@webkit.org>
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Library General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Library General Public License for more details.
17 * You should have received a copy of the GNU Library General Public License
18 * along with this library; see the file COPYING.LIB. If not, write to
19 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20 * Boston, MA 02110-1301, USA.
26 #include "RenderSVGResourceClipper.h"
28 #include "AffineTransform.h"
29 #include "ElementIterator.h"
30 #include "FloatRect.h"
32 #include "FrameView.h"
33 #include "GraphicsContext.h"
34 #include "HitTestRequest.h"
35 #include "HitTestResult.h"
36 #include "ImageBuffer.h"
38 #include "RenderObject.h"
39 #include "RenderSVGResource.h"
40 #include "RenderStyle.h"
41 #include "RenderView.h"
42 #include "SVGClipPathElement.h"
43 #include "SVGElement.h"
44 #include "SVGGraphicsElement.h"
46 #include "SVGRenderSupport.h"
47 #include "SVGRenderingContext.h"
48 #include "SVGResources.h"
49 #include "SVGResourcesCache.h"
50 #include "SVGUnitTypes.h"
51 #include "SVGUseElement.h"
55 RenderSVGResourceType RenderSVGResourceClipper::s_resourceType = ClipperResourceType;
57 RenderSVGResourceClipper::RenderSVGResourceClipper(SVGClipPathElement& element)
58 : RenderSVGResourceContainer(element)
62 RenderSVGResourceClipper::~RenderSVGResourceClipper()
66 void RenderSVGResourceClipper::removeAllClientsFromCache(bool markForInvalidation)
68 m_clipBoundaries = FloatRect();
71 markAllClientsForInvalidation(markForInvalidation ? LayoutAndBoundariesInvalidation : ParentOnlyInvalidation);
74 void RenderSVGResourceClipper::removeClientFromCache(RenderObject* client, bool markForInvalidation)
77 m_clipper.remove(client);
79 markClientForInvalidation(client, markForInvalidation ? BoundariesInvalidation : ParentOnlyInvalidation);
82 bool RenderSVGResourceClipper::applyResource(RenderObject* object, RenderStyle*, GraphicsContext*& context, unsigned short resourceMode)
86 ASSERT_UNUSED(resourceMode, resourceMode == ApplyToDefaultMode);
88 return applyClippingToContext(object, object->objectBoundingBox(), object->repaintRectInLocalCoordinates(), context);
91 bool RenderSVGResourceClipper::pathOnlyClipping(GraphicsContext* context, const AffineTransform& animatedLocalTransform, const FloatRect& objectBoundingBox)
93 // If the current clip-path gets clipped itself, we have to fallback to masking.
94 if (!style()->svgStyle()->clipperResource().isEmpty())
96 WindRule clipRule = RULE_NONZERO;
97 Path clipPath = Path();
99 // If clip-path only contains one visible shape or path, we can use path-based clipping. Invisible
100 // shapes don't affect the clipping and can be ignored. If clip-path contains more than one
101 // visible shape, the additive clipping may not work, caused by the clipRule. EvenOdd
102 // as well as NonZero can cause self-clipping of the elements.
103 // See also http://www.w3.org/TR/SVG/painting.html#FillRuleProperty
104 for (Node* childNode = clipPathElement().firstChild(); childNode; childNode = childNode->nextSibling()) {
105 RenderObject* renderer = childNode->renderer();
108 // Only shapes or paths are supported for direct clipping. We need to fallback to masking for texts.
109 if (renderer->isSVGText())
111 if (!childNode->isSVGElement() || !toSVGElement(childNode)->isSVGGraphicsElement())
113 SVGGraphicsElement* styled = toSVGGraphicsElement(childNode);
114 RenderStyle* style = renderer->style();
115 if (!style || style->display() == NONE || style->visibility() != VISIBLE)
117 const SVGRenderStyle* svgStyle = style->svgStyle();
118 // Current shape in clip-path gets clipped too. Fallback to masking.
119 if (!svgStyle->clipperResource().isEmpty())
121 // Fallback to masking, if there is more than one clipping path.
122 if (clipPath.isEmpty()) {
123 styled->toClipPath(clipPath);
124 clipRule = svgStyle->clipRule();
128 // Only one visible shape/path was found. Directly continue clipping and transform the content to userspace if necessary.
129 if (clipPathElement().clipPathUnits() == SVGUnitTypes::SVG_UNIT_TYPE_OBJECTBOUNDINGBOX) {
130 AffineTransform transform;
131 transform.translate(objectBoundingBox.x(), objectBoundingBox.y());
132 transform.scaleNonUniform(objectBoundingBox.width(), objectBoundingBox.height());
133 clipPath.transform(transform);
136 // Transform path by animatedLocalTransform.
137 clipPath.transform(animatedLocalTransform);
139 // The SVG specification wants us to clip everything, if clip-path doesn't have a child.
140 if (clipPath.isEmpty())
141 clipPath.addRect(FloatRect());
142 context->clipPath(clipPath, clipRule);
146 bool RenderSVGResourceClipper::applyClippingToContext(RenderObject* object, const FloatRect& objectBoundingBox,
147 const FloatRect& repaintRect, GraphicsContext* context)
149 bool missingClipperData = !m_clipper.contains(object);
150 if (missingClipperData)
151 m_clipper.set(object, createOwned<ClipperData>().release());
153 bool shouldCreateClipData = false;
154 AffineTransform animatedLocalTransform = clipPathElement().animatedLocalTransform();
155 ClipperData* clipperData = m_clipper.get(object);
156 if (!clipperData->clipMaskImage) {
157 if (pathOnlyClipping(context, animatedLocalTransform, objectBoundingBox))
159 shouldCreateClipData = true;
162 AffineTransform absoluteTransform;
163 SVGRenderingContext::calculateTransformationToOutermostCoordinateSystem(object, absoluteTransform);
165 if (shouldCreateClipData && !repaintRect.isEmpty()) {
166 if (!SVGRenderingContext::createImageBuffer(repaintRect, absoluteTransform, clipperData->clipMaskImage, ColorSpaceDeviceRGB, Unaccelerated))
169 GraphicsContext* maskContext = clipperData->clipMaskImage->context();
172 maskContext->concatCTM(animatedLocalTransform);
174 // clipPath can also be clipped by another clipPath.
175 SVGResources* resources = SVGResourcesCache::cachedResourcesForRenderObject(this);
176 RenderSVGResourceClipper* clipper;
178 if (resources && (clipper = resources->clipper())) {
179 GraphicsContextStateSaver stateSaver(*maskContext);
181 if (!clipper->applyClippingToContext(this, objectBoundingBox, repaintRect, maskContext))
184 succeeded = drawContentIntoMaskImage(clipperData, objectBoundingBox);
185 // The context restore applies the clipping on non-CG platforms.
187 succeeded = drawContentIntoMaskImage(clipperData, objectBoundingBox);
190 clipperData->clipMaskImage.clear();
193 if (!clipperData->clipMaskImage)
196 SVGRenderingContext::clipToImageBuffer(context, absoluteTransform, repaintRect, clipperData->clipMaskImage, missingClipperData);
200 bool RenderSVGResourceClipper::drawContentIntoMaskImage(ClipperData* clipperData, const FloatRect& objectBoundingBox)
203 ASSERT(clipperData->clipMaskImage);
205 GraphicsContext* maskContext = clipperData->clipMaskImage->context();
208 AffineTransform maskContentTransformation;
209 if (clipPathElement().clipPathUnits() == SVGUnitTypes::SVG_UNIT_TYPE_OBJECTBOUNDINGBOX) {
210 maskContentTransformation.translate(objectBoundingBox.x(), objectBoundingBox.y());
211 maskContentTransformation.scaleNonUniform(objectBoundingBox.width(), objectBoundingBox.height());
212 maskContext->concatCTM(maskContentTransformation);
215 // Switch to a paint behavior where all children of this <clipPath> will be rendered using special constraints:
216 // - fill-opacity/stroke-opacity/opacity set to 1
217 // - masker/filter not applied when rendering the children
218 // - fill is set to the initial fill paint server (solid, black)
219 // - stroke is set to the initial stroke paint server (none)
220 PaintBehavior oldBehavior = view().frameView().paintBehavior();
221 view().frameView().setPaintBehavior(oldBehavior | PaintBehaviorRenderingSVGMask);
223 // Draw all clipPath children into a global mask.
224 auto children = childrenOfType<SVGElement>(&clipPathElement());
225 for (auto it = children.begin(), end = children.end(); it != end; ++it) {
226 SVGElement& child = *it;
227 auto renderer = child.renderer();
230 if (renderer->needsLayout()) {
231 view().frameView().setPaintBehavior(oldBehavior);
234 RenderStyle* style = renderer->style();
235 if (!style || style->display() == NONE || style->visibility() != VISIBLE)
238 WindRule newClipRule = style->svgStyle()->clipRule();
239 bool isUseElement = child.hasTagName(SVGNames::useTag);
241 SVGUseElement& useElement = toSVGUseElement(child);
242 renderer = useElement.rendererClipChild();
245 if (!useElement.hasAttribute(SVGNames::clip_ruleAttr))
246 newClipRule = renderer->style()->svgStyle()->clipRule();
249 // Only shapes, paths and texts are allowed for clipping.
250 if (!renderer->isSVGShape() && !renderer->isSVGText())
253 maskContext->setFillRule(newClipRule);
255 // In the case of a <use> element, we obtained its renderere above, to retrieve its clipRule.
256 // We have to pass the <use> renderer itself to renderSubtreeToImageBuffer() to apply it's x/y/transform/etc. values when rendering.
257 // So if isUseElement is true, refetch the childNode->renderer(), as renderer got overriden above.
258 SVGRenderingContext::renderSubtreeToImageBuffer(clipperData->clipMaskImage.get(), isUseElement ? *child.renderer() : *renderer, maskContentTransformation);
261 view().frameView().setPaintBehavior(oldBehavior);
265 void RenderSVGResourceClipper::calculateClipContentRepaintRect()
267 // This is a rough heuristic to appraise the clip size and doesn't consider clip on clip.
268 for (Node* childNode = clipPathElement().firstChild(); childNode; childNode = childNode->nextSibling()) {
269 RenderObject* renderer = childNode->renderer();
270 if (!childNode->isSVGElement() || !renderer)
272 if (!renderer->isSVGShape() && !renderer->isSVGText() && !childNode->hasTagName(SVGNames::useTag))
274 RenderStyle* style = renderer->style();
275 if (!style || style->display() == NONE || style->visibility() != VISIBLE)
277 m_clipBoundaries.unite(renderer->localToParentTransform().mapRect(renderer->repaintRectInLocalCoordinates()));
279 m_clipBoundaries = clipPathElement().animatedLocalTransform().mapRect(m_clipBoundaries);
282 bool RenderSVGResourceClipper::hitTestClipContent(const FloatRect& objectBoundingBox, const FloatPoint& nodeAtPoint)
284 FloatPoint point = nodeAtPoint;
285 if (!SVGRenderSupport::pointInClippingArea(this, point))
288 if (clipPathElement().clipPathUnits() == SVGUnitTypes::SVG_UNIT_TYPE_OBJECTBOUNDINGBOX) {
289 AffineTransform transform;
290 transform.translate(objectBoundingBox.x(), objectBoundingBox.y());
291 transform.scaleNonUniform(objectBoundingBox.width(), objectBoundingBox.height());
292 point = transform.inverse().mapPoint(point);
295 point = clipPathElement().animatedLocalTransform().inverse().mapPoint(point);
297 for (Node* childNode = clipPathElement().firstChild(); childNode; childNode = childNode->nextSibling()) {
298 RenderObject* renderer = childNode->renderer();
299 if (!childNode->isSVGElement() || !renderer)
301 if (!renderer->isSVGShape() && !renderer->isSVGText() && !childNode->hasTagName(SVGNames::useTag))
304 HitTestResult result(hitPoint);
305 if (renderer->nodeAtFloatPoint(HitTestRequest(HitTestRequest::SVGClipContent | HitTestRequest::DisallowShadowContent), result, point, HitTestForeground))
312 FloatRect RenderSVGResourceClipper::resourceBoundingBox(RenderObject* object)
314 // Resource was not layouted yet. Give back the boundingBox of the object.
315 if (selfNeedsLayout())
316 return object->objectBoundingBox();
318 if (m_clipBoundaries.isEmpty())
319 calculateClipContentRepaintRect();
321 if (clipPathElement().clipPathUnits() == SVGUnitTypes::SVG_UNIT_TYPE_OBJECTBOUNDINGBOX) {
322 FloatRect objectBoundingBox = object->objectBoundingBox();
323 AffineTransform transform;
324 transform.translate(objectBoundingBox.x(), objectBoundingBox.y());
325 transform.scaleNonUniform(objectBoundingBox.width(), objectBoundingBox.height());
326 return transform.mapRect(m_clipBoundaries);
329 return m_clipBoundaries;
334 #endif // ENABLE(SVG)