2 * Copyright (C) 2006 Nikolas Zimmermann <zimmermann@kde.org>
3 * Copyright (C) Research In Motion Limited 2010. All rights reserved.
4 * Copyright 2014 The Chromium Authors. All rights reserved.
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
16 * You should have received a copy of the GNU Library General Public License
17 * along with this library; see the file COPYING.LIB. If not, write to
18 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19 * Boston, MA 02110-1301, USA.
23 #include "core/rendering/svg/RenderSVGResourcePattern.h"
25 #include "core/dom/ElementTraversal.h"
26 #include "core/rendering/svg/SVGRenderingContext.h"
27 #include "core/svg/SVGFitToViewBox.h"
28 #include "core/svg/SVGPatternElement.h"
29 #include "platform/graphics/DisplayList.h"
30 #include "platform/graphics/GraphicsContext.h"
35 WTF_MAKE_FAST_ALLOCATED;
37 RefPtr<Pattern> pattern;
38 AffineTransform transform;
41 RenderSVGResourcePattern::RenderSVGResourcePattern(SVGPatternElement* node)
42 : RenderSVGResourcePaintServer(node)
43 , m_shouldCollectPatternAttributes(true)
47 void RenderSVGResourcePattern::removeAllClientsFromCache(bool markForInvalidation)
50 m_shouldCollectPatternAttributes = true;
51 markAllClientsForInvalidation(markForInvalidation ? PaintInvalidation : ParentOnlyInvalidation);
54 void RenderSVGResourcePattern::removeClientFromCache(RenderObject* client, bool markForInvalidation)
57 m_patternMap.remove(client);
58 markClientForInvalidation(client, markForInvalidation ? PaintInvalidation : ParentOnlyInvalidation);
61 PatternData* RenderSVGResourcePattern::patternForRenderer(const RenderObject& object)
63 ASSERT(!m_shouldCollectPatternAttributes);
65 // FIXME: the double hash lookup is needed to guard against paint-time invalidation
66 // (painting animated images may trigger layout invals which delete our map entry).
67 // Hopefully that will be addressed at some point, and then we can optimize the lookup.
68 if (PatternData* currentData = m_patternMap.get(&object))
71 return m_patternMap.set(&object, buildPatternData(object)).storedValue->value.get();
74 PassOwnPtr<PatternData> RenderSVGResourcePattern::buildPatternData(const RenderObject& object)
76 // If we couldn't determine the pattern content element root, stop here.
77 if (!m_attributes.patternContentElement())
80 // An empty viewBox disables rendering.
81 if (m_attributes.hasViewBox() && m_attributes.viewBox().isEmpty())
85 // Compute tile metrics.
86 FloatRect clientBoundingBox = object.objectBoundingBox();
87 FloatRect tileBounds = SVGLengthContext::resolveRectangle(element(),
88 m_attributes.patternUnits(), clientBoundingBox,
89 m_attributes.x(), m_attributes.y(), m_attributes.width(), m_attributes.height());
90 if (tileBounds.isEmpty())
93 AffineTransform tileTransform;
94 if (m_attributes.hasViewBox()) {
95 if (m_attributes.viewBox().isEmpty())
97 tileTransform = SVGFitToViewBox::viewBoxToViewTransform(m_attributes.viewBox(),
98 m_attributes.preserveAspectRatio(), tileBounds.width(), tileBounds.height());
100 // A viewbox overrides patternContentUnits, per spec.
101 if (m_attributes.patternContentUnits() == SVGUnitTypes::SVG_UNIT_TYPE_OBJECTBOUNDINGBOX)
102 tileTransform.scale(clientBoundingBox.width(), clientBoundingBox.height());
105 OwnPtr<PatternData> patternData = adoptPtr(new PatternData);
106 patternData->pattern = Pattern::createDisplayListPattern(asDisplayList(tileBounds, tileTransform));
108 // Compute pattern space transformation.
109 patternData->transform.translate(tileBounds.x(), tileBounds.y());
110 AffineTransform patternTransform = m_attributes.patternTransform();
111 if (!patternTransform.isIdentity())
112 patternData->transform = patternTransform * patternData->transform;
114 return patternData.release();
117 SVGPaintServer RenderSVGResourcePattern::preparePaintServer(const RenderObject& object)
119 clearInvalidationMask();
121 SVGPatternElement* patternElement = toSVGPatternElement(element());
123 return SVGPaintServer::invalid();
125 if (m_shouldCollectPatternAttributes) {
126 patternElement->synchronizeAnimatedSVGAttribute(anyQName());
128 m_attributes = PatternAttributes();
129 patternElement->collectPatternAttributes(m_attributes);
130 m_shouldCollectPatternAttributes = false;
133 // Spec: When the geometry of the applicable element has no width or height and objectBoundingBox is specified,
134 // then the given effect (e.g. a gradient or a filter) will be ignored.
135 FloatRect objectBoundingBox = object.objectBoundingBox();
136 if (m_attributes.patternUnits() == SVGUnitTypes::SVG_UNIT_TYPE_OBJECTBOUNDINGBOX && objectBoundingBox.isEmpty())
137 return SVGPaintServer::invalid();
139 PatternData* patternData = patternForRenderer(object);
141 return SVGPaintServer::invalid();
143 patternData->pattern->setPatternSpaceTransform(patternData->transform);
145 return SVGPaintServer(patternData->pattern);
148 PassRefPtr<DisplayList> RenderSVGResourcePattern::asDisplayList(const FloatRect& tileBounds,
149 const AffineTransform& tileTransform) const
151 ASSERT(!m_shouldCollectPatternAttributes);
153 AffineTransform contentTransform;
154 if (m_attributes.patternContentUnits() == SVGUnitTypes::SVG_UNIT_TYPE_OBJECTBOUNDINGBOX)
155 contentTransform = tileTransform;
157 // Draw the content into a DisplayList.
158 GraphicsContext recordingContext(nullptr);
159 recordingContext.beginRecording(FloatRect(FloatPoint(), tileBounds.size()));
160 recordingContext.concatCTM(tileTransform);
162 ASSERT(m_attributes.patternContentElement());
163 RenderSVGResourceContainer* patternRenderer =
164 toRenderSVGResourceContainer(m_attributes.patternContentElement()->renderer());
165 ASSERT(patternRenderer);
166 ASSERT(!patternRenderer->needsLayout());
168 SubtreeContentTransformScope contentTransformScope(contentTransform);
169 for (RenderObject* child = patternRenderer->firstChild(); child; child = child->nextSibling())
170 SVGRenderingContext::renderSubtree(&recordingContext, child);
172 return recordingContext.endRecording();