<feImage> DOM mutation problems
[WebKit-https.git] / 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 "SVGNames.h"
37 #include "SVGPreserveAspectRatio.h"
38
39 namespace WebCore {
40
41 // Animated property definitions
42 DEFINE_ANIMATED_PRESERVEASPECTRATIO(SVGFEImageElement, SVGNames::preserveAspectRatioAttr, PreserveAspectRatio, preserveAspectRatio)
43 DEFINE_ANIMATED_STRING(SVGFEImageElement, XLinkNames::hrefAttr, Href, href)
44 DEFINE_ANIMATED_BOOLEAN(SVGFEImageElement, SVGNames::externalResourcesRequiredAttr, ExternalResourcesRequired, externalResourcesRequired)
45
46 BEGIN_REGISTER_ANIMATED_PROPERTIES(SVGFEImageElement)
47     REGISTER_LOCAL_ANIMATED_PROPERTY(preserveAspectRatio)
48     REGISTER_LOCAL_ANIMATED_PROPERTY(href)
49     REGISTER_LOCAL_ANIMATED_PROPERTY(externalResourcesRequired)
50     REGISTER_PARENT_ANIMATED_PROPERTIES(SVGFilterPrimitiveStandardAttributes)
51 END_REGISTER_ANIMATED_PROPERTIES
52
53 inline SVGFEImageElement::SVGFEImageElement(const QualifiedName& tagName, Document* document)
54     : SVGFilterPrimitiveStandardAttributes(tagName, document)
55 {
56     ASSERT(hasTagName(SVGNames::feImageTag));
57     registerAnimatedPropertiesForSVGFEImageElement();
58 }
59
60 PassRefPtr<SVGFEImageElement> SVGFEImageElement::create(const QualifiedName& tagName, Document* document)
61 {
62     return adoptRef(new SVGFEImageElement(tagName, document));
63 }
64
65 SVGFEImageElement::~SVGFEImageElement()
66 {
67     clearResourceReferences();
68 }
69
70 void SVGFEImageElement::clearResourceReferences()
71 {
72     if (m_cachedImage) {
73         m_cachedImage->removeClient(this);
74         m_cachedImage = 0;
75     }
76 }
77
78 void SVGFEImageElement::requestImageResource()
79 {
80     ResourceRequest request(ownerDocument()->completeURL(href()));
81     m_cachedImage = document()->cachedResourceLoader()->requestImage(request);
82
83     if (m_cachedImage)
84         m_cachedImage->addClient(this);
85 }
86
87 void SVGFEImageElement::buildPendingResource()
88 {
89     clearResourceReferences();
90     if (!inDocument())
91         return;
92
93     String id;
94     Element* target = SVGURIReference::targetElementFromIRIString(href(), document(), &id);
95     if (!target) {
96         if (hasPendingResources())
97             return;
98
99         if (id.isEmpty())
100             requestImageResource();
101         else {
102             document()->accessSVGExtensions()->addPendingResource(id, this);
103             ASSERT(hasPendingResources());
104         }
105     }
106
107     invalidate();
108 }
109
110 bool SVGFEImageElement::isSupportedAttribute(const QualifiedName& attrName)
111 {
112     DEFINE_STATIC_LOCAL(HashSet<QualifiedName>, supportedAttributes, ());
113     if (supportedAttributes.isEmpty()) {
114         SVGURIReference::addSupportedAttributes(supportedAttributes);
115         SVGLangSpace::addSupportedAttributes(supportedAttributes);
116         SVGExternalResourcesRequired::addSupportedAttributes(supportedAttributes);
117         supportedAttributes.add(SVGNames::preserveAspectRatioAttr);
118     }
119     return supportedAttributes.contains<QualifiedName, SVGAttributeHashTranslator>(attrName);
120 }
121
122 void SVGFEImageElement::parseMappedAttribute(Attribute* attr)
123 {
124     if (!isSupportedAttribute(attr->name())) {
125         SVGFilterPrimitiveStandardAttributes::parseMappedAttribute(attr);
126         return;
127     }
128
129     const AtomicString& value = attr->value();
130     if (attr->name() == SVGNames::preserveAspectRatioAttr) {
131         SVGPreserveAspectRatio::parsePreserveAspectRatio(this, value);
132         return;
133     }
134
135     if (SVGURIReference::parseMappedAttribute(attr))
136         return;
137     if (SVGLangSpace::parseMappedAttribute(attr))
138         return;
139     if (SVGExternalResourcesRequired::parseMappedAttribute(attr))
140         return;
141
142     ASSERT_NOT_REACHED();
143 }
144
145 void SVGFEImageElement::svgAttributeChanged(const QualifiedName& attrName)
146 {
147     if (!isSupportedAttribute(attrName)) {
148         SVGFilterPrimitiveStandardAttributes::svgAttributeChanged(attrName);
149         return;
150     }
151
152     SVGElementInstance::InvalidationGuard invalidationGuard(this);
153     
154     if (attrName == SVGNames::preserveAspectRatioAttr) {
155         invalidate();
156         return;
157     }
158
159     if (SVGURIReference::isKnownAttribute(attrName)) {
160         buildPendingResource();
161         return;
162     }
163
164     if (SVGLangSpace::isKnownAttribute(attrName) || SVGExternalResourcesRequired::isKnownAttribute(attrName))
165         return;
166
167     ASSERT_NOT_REACHED();
168 }
169
170 void SVGFEImageElement::insertedIntoDocument()
171 {
172     SVGFilterPrimitiveStandardAttributes::insertedIntoDocument();
173     buildPendingResource();
174 }
175
176 void SVGFEImageElement::removedFromDocument()
177 {
178     SVGFilterPrimitiveStandardAttributes::removedFromDocument();
179     clearResourceReferences();
180 }
181
182 void SVGFEImageElement::notifyFinished(CachedResource*)
183 {
184     if (!inDocument())
185         return;
186
187     Element* parent = parentElement();
188     ASSERT(parent);
189
190     if (!parent->hasTagName(SVGNames::filterTag) || !parent->renderer())
191         return;
192
193     RenderSVGResource::markForLayoutAndParentResourceInvalidation(parent->renderer());
194 }
195
196 PassRefPtr<FilterEffect> SVGFEImageElement::build(SVGFilterBuilder*, Filter* filter)
197 {
198     if (m_cachedImage)
199         return FEImage::createWithImage(filter, m_cachedImage->imageForRenderer(renderer()), preserveAspectRatio());
200     return FEImage::createWithIRIReference(filter, document(), href(), preserveAspectRatio());
201 }
202
203 void SVGFEImageElement::addSubresourceAttributeURLs(ListHashSet<KURL>& urls) const
204 {
205     SVGFilterPrimitiveStandardAttributes::addSubresourceAttributeURLs(urls);
206
207     addSubresourceURL(urls, document()->completeURL(href()));
208 }
209
210 }
211
212 #endif // ENABLE(SVG)