initial import
[vuplus_webkit] / Source / WebCore / svg / SVGFEImageElement.cpp
1 /*
2  * Copyright (C) 2004, 2005, 2007 Nikolas Zimmermann <zimmermann@kde.org>
3  * Copyright (C) 2004, 2005 Rob Buis <buis@kde.org>
4  * Copyright (C) 2010 Dirk Schulze <krit@webkit.org>
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
24 #if ENABLE(SVG) && ENABLE(FILTERS)
25 #include "SVGFEImageElement.h"
26
27 #include "Attr.h"
28 #include "CachedImage.h"
29 #include "CachedResourceLoader.h"
30 #include "ColorSpace.h"
31 #include "Document.h"
32 #include "Image.h"
33 #include "RenderObject.h"
34 #include "RenderSVGResource.h"
35 #include "SVGElementInstance.h"
36 #include "SVGImageBufferTools.h"
37 #include "SVGNames.h"
38 #include "SVGPreserveAspectRatio.h"
39
40 namespace WebCore {
41
42 // Animated property definitions
43 DEFINE_ANIMATED_PRESERVEASPECTRATIO(SVGFEImageElement, SVGNames::preserveAspectRatioAttr, PreserveAspectRatio, preserveAspectRatio)
44 DEFINE_ANIMATED_STRING(SVGFEImageElement, XLinkNames::hrefAttr, Href, href)
45 DEFINE_ANIMATED_BOOLEAN(SVGFEImageElement, SVGNames::externalResourcesRequiredAttr, ExternalResourcesRequired, externalResourcesRequired)
46
47 BEGIN_REGISTER_ANIMATED_PROPERTIES(SVGFEImageElement)
48     REGISTER_LOCAL_ANIMATED_PROPERTY(preserveAspectRatio)
49     REGISTER_LOCAL_ANIMATED_PROPERTY(href)
50     REGISTER_LOCAL_ANIMATED_PROPERTY(externalResourcesRequired)
51     REGISTER_PARENT_ANIMATED_PROPERTIES(SVGFilterPrimitiveStandardAttributes)
52 END_REGISTER_ANIMATED_PROPERTIES
53
54 inline SVGFEImageElement::SVGFEImageElement(const QualifiedName& tagName, Document* document)
55     : SVGFilterPrimitiveStandardAttributes(tagName, document)
56 {
57     ASSERT(hasTagName(SVGNames::feImageTag));
58     registerAnimatedPropertiesForSVGFEImageElement();
59 }
60
61 PassRefPtr<SVGFEImageElement> SVGFEImageElement::create(const QualifiedName& tagName, Document* document)
62 {
63     return adoptRef(new SVGFEImageElement(tagName, document));
64 }
65
66 SVGFEImageElement::~SVGFEImageElement()
67 {
68     if (m_cachedImage)
69         m_cachedImage->removeClient(this);
70 }
71
72 void SVGFEImageElement::requestImageResource()
73 {
74     if (m_cachedImage) {
75         m_cachedImage->removeClient(this);
76         m_cachedImage = 0;
77     }
78
79     Element* hrefElement = SVGURIReference::targetElementFromIRIString(href(), document());
80     if (hrefElement && hrefElement->isSVGElement() && hrefElement->renderer())
81         return;
82
83     ResourceRequest request(ownerDocument()->completeURL(href()));
84     m_cachedImage = ownerDocument()->cachedResourceLoader()->requestImage(request);
85
86     if (m_cachedImage)
87         m_cachedImage->addClient(this);
88 }
89
90 bool SVGFEImageElement::isSupportedAttribute(const QualifiedName& attrName)
91 {
92     DEFINE_STATIC_LOCAL(HashSet<QualifiedName>, supportedAttributes, ());
93     if (supportedAttributes.isEmpty()) {
94         SVGURIReference::addSupportedAttributes(supportedAttributes);
95         SVGLangSpace::addSupportedAttributes(supportedAttributes);
96         SVGExternalResourcesRequired::addSupportedAttributes(supportedAttributes);
97         supportedAttributes.add(SVGNames::preserveAspectRatioAttr);
98     }
99     return supportedAttributes.contains(attrName);
100 }
101
102 void SVGFEImageElement::parseMappedAttribute(Attribute* attr)
103 {
104     if (!isSupportedAttribute(attr->name())) {
105         SVGFilterPrimitiveStandardAttributes::parseMappedAttribute(attr);
106         return;
107     }
108
109     const AtomicString& value = attr->value();
110     if (attr->name() == SVGNames::preserveAspectRatioAttr) {
111         SVGPreserveAspectRatio::parsePreserveAspectRatio(this, value);
112         return;
113     }
114
115     if (SVGURIReference::parseMappedAttribute(attr)) {
116         requestImageResource();
117         return;
118     }
119
120     if (SVGLangSpace::parseMappedAttribute(attr))
121         return;
122     if (SVGExternalResourcesRequired::parseMappedAttribute(attr))
123         return;
124
125     ASSERT_NOT_REACHED();
126 }
127
128 void SVGFEImageElement::svgAttributeChanged(const QualifiedName& attrName)
129 {
130     if (!isSupportedAttribute(attrName)) {
131         SVGFilterPrimitiveStandardAttributes::svgAttributeChanged(attrName);
132         return;
133     }
134
135     SVGElementInstance::InvalidationGuard invalidationGuard(this);
136     
137     if (attrName == SVGNames::preserveAspectRatioAttr) {
138         invalidate();
139         return;
140     }
141
142     // FIXME: This can't be correct, I'm just preserving existing code. <feImage> + SVG DOM 'href' changes need testing.
143     if (SVGLangSpace::isKnownAttribute(attrName) || SVGExternalResourcesRequired::isKnownAttribute(attrName) || SVGURIReference::isKnownAttribute(attrName))
144         return;
145
146     ASSERT_NOT_REACHED();
147 }
148
149 void SVGFEImageElement::notifyFinished(CachedResource*)
150 {
151     if (!inDocument())
152         return;
153
154     Element* parent = parentElement();
155     ASSERT(parent);
156
157     if (!parent->hasTagName(SVGNames::filterTag) || !parent->renderer())
158         return;
159
160     RenderSVGResource::markForLayoutAndParentResourceInvalidation(parent->renderer());
161 }
162
163 PassRefPtr<FilterEffect> SVGFEImageElement::build(SVGFilterBuilder*, Filter* filter)
164 {
165     if (!m_cachedImage && !m_targetImage) {
166         Element* hrefElement = SVGURIReference::targetElementFromIRIString(href(), document());
167         if (!hrefElement || !hrefElement->isSVGElement())
168             return 0;
169
170         RenderObject* renderer = hrefElement->renderer();
171         if (!renderer)
172             return 0;
173
174         IntRect targetRect = enclosingIntRect(renderer->objectBoundingBox());
175         m_targetImage = ImageBuffer::create(targetRect.size(), ColorSpaceLinearRGB);
176
177         AffineTransform contentTransformation;
178         SVGImageBufferTools::renderSubtreeToImageBuffer(m_targetImage.get(), renderer, contentTransformation);
179     }
180
181     return FEImage::create(filter, m_targetImage ? m_targetImage->copyImage(CopyBackingStore) : m_cachedImage->image(), preserveAspectRatio());
182 }
183
184 void SVGFEImageElement::addSubresourceAttributeURLs(ListHashSet<KURL>& urls) const
185 {
186     SVGFilterPrimitiveStandardAttributes::addSubresourceAttributeURLs(urls);
187
188     addSubresourceURL(urls, document()->completeURL(href()));
189 }
190
191 }
192
193 #endif // ENABLE(SVG)