Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / core / rendering / svg / RenderSVGResourcePattern.cpp
1 /*
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.
5  *
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.
10  *
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.
15  *
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.
20  */
21
22 #include "config.h"
23 #include "core/rendering/svg/RenderSVGResourcePattern.h"
24
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"
31
32 namespace blink {
33
34 struct PatternData {
35     WTF_MAKE_FAST_ALLOCATED;
36 public:
37     RefPtr<Pattern> pattern;
38     AffineTransform transform;
39 };
40
41 RenderSVGResourcePattern::RenderSVGResourcePattern(SVGPatternElement* node)
42     : RenderSVGResourcePaintServer(node)
43     , m_shouldCollectPatternAttributes(true)
44 {
45 }
46
47 void RenderSVGResourcePattern::removeAllClientsFromCache(bool markForInvalidation)
48 {
49     m_patternMap.clear();
50     m_shouldCollectPatternAttributes = true;
51     markAllClientsForInvalidation(markForInvalidation ? PaintInvalidation : ParentOnlyInvalidation);
52 }
53
54 void RenderSVGResourcePattern::removeClientFromCache(RenderObject* client, bool markForInvalidation)
55 {
56     ASSERT(client);
57     m_patternMap.remove(client);
58     markClientForInvalidation(client, markForInvalidation ? PaintInvalidation : ParentOnlyInvalidation);
59 }
60
61 PatternData* RenderSVGResourcePattern::patternForRenderer(const RenderObject& object)
62 {
63     ASSERT(!m_shouldCollectPatternAttributes);
64
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))
69         return currentData;
70
71     return m_patternMap.set(&object, buildPatternData(object)).storedValue->value.get();
72 }
73
74 PassOwnPtr<PatternData> RenderSVGResourcePattern::buildPatternData(const RenderObject& object)
75 {
76     // If we couldn't determine the pattern content element root, stop here.
77     if (!m_attributes.patternContentElement())
78         return nullptr;
79
80     // An empty viewBox disables rendering.
81     if (m_attributes.hasViewBox() && m_attributes.viewBox().isEmpty())
82         return nullptr;
83
84     ASSERT(element());
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())
91         return nullptr;
92
93     AffineTransform tileTransform;
94     if (m_attributes.hasViewBox()) {
95         if (m_attributes.viewBox().isEmpty())
96             return nullptr;
97         tileTransform = SVGFitToViewBox::viewBoxToViewTransform(m_attributes.viewBox(),
98             m_attributes.preserveAspectRatio(), tileBounds.width(), tileBounds.height());
99     } else {
100         // A viewbox overrides patternContentUnits, per spec.
101         if (m_attributes.patternContentUnits() == SVGUnitTypes::SVG_UNIT_TYPE_OBJECTBOUNDINGBOX)
102             tileTransform.scale(clientBoundingBox.width(), clientBoundingBox.height());
103     }
104
105     OwnPtr<PatternData> patternData = adoptPtr(new PatternData);
106     patternData->pattern = Pattern::createDisplayListPattern(asDisplayList(tileBounds, tileTransform));
107
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;
113
114     return patternData.release();
115 }
116
117 SVGPaintServer RenderSVGResourcePattern::preparePaintServer(const RenderObject& object)
118 {
119     clearInvalidationMask();
120
121     SVGPatternElement* patternElement = toSVGPatternElement(element());
122     if (!patternElement)
123         return SVGPaintServer::invalid();
124
125     if (m_shouldCollectPatternAttributes) {
126         patternElement->synchronizeAnimatedSVGAttribute(anyQName());
127
128         m_attributes = PatternAttributes();
129         patternElement->collectPatternAttributes(m_attributes);
130         m_shouldCollectPatternAttributes = false;
131     }
132
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();
138
139     PatternData* patternData = patternForRenderer(object);
140     if (!patternData)
141         return SVGPaintServer::invalid();
142
143     patternData->pattern->setPatternSpaceTransform(patternData->transform);
144
145     return SVGPaintServer(patternData->pattern);
146 }
147
148 PassRefPtr<DisplayList> RenderSVGResourcePattern::asDisplayList(const FloatRect& tileBounds,
149     const AffineTransform& tileTransform) const
150 {
151     ASSERT(!m_shouldCollectPatternAttributes);
152
153     AffineTransform contentTransform;
154     if (m_attributes.patternContentUnits() == SVGUnitTypes::SVG_UNIT_TYPE_OBJECTBOUNDINGBOX)
155         contentTransform = tileTransform;
156
157     // Draw the content into a DisplayList.
158     GraphicsContext recordingContext(nullptr);
159     recordingContext.beginRecording(FloatRect(FloatPoint(), tileBounds.size()));
160     recordingContext.concatCTM(tileTransform);
161
162     ASSERT(m_attributes.patternContentElement());
163     RenderSVGResourceContainer* patternRenderer =
164         toRenderSVGResourceContainer(m_attributes.patternContentElement()->renderer());
165     ASSERT(patternRenderer);
166     ASSERT(!patternRenderer->needsLayout());
167
168     SubtreeContentTransformScope contentTransformScope(contentTransform);
169     for (RenderObject* child = patternRenderer->firstChild(); child; child = child->nextSibling())
170         SVGRenderingContext::renderSubtree(&recordingContext, child);
171
172     return recordingContext.endRecording();
173 }
174
175 }