Unreviewed, rolling out r134442.
[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 "CachedResourceRequest.h"
31 #include "ColorSpace.h"
32 #include "Document.h"
33 #include "Image.h"
34 #include "RenderObject.h"
35 #include "RenderSVGResource.h"
36 #include "SVGElementInstance.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     clearResourceReferences();
69 }
70
71 void SVGFEImageElement::clearResourceReferences()
72 {
73     if (m_cachedImage) {
74         m_cachedImage->removeClient(this);
75         m_cachedImage = 0;
76     }
77
78     ASSERT(document());
79     document()->accessSVGExtensions()->removeAllTargetReferencesForElement(this);
80 }
81
82 void SVGFEImageElement::requestImageResource()
83 {
84     CachedResourceRequest request(ResourceRequest(ownerDocument()->completeURL(href())));
85     m_cachedImage = document()->cachedResourceLoader()->requestImage(request);
86
87     if (m_cachedImage)
88         m_cachedImage->addClient(this);
89 }
90
91 void SVGFEImageElement::buildPendingResource()
92 {
93     clearResourceReferences();
94     if (!inDocument())
95         return;
96
97     String id;
98     Element* target = SVGURIReference::targetElementFromIRIString(href(), document(), &id);
99     if (!target) {
100         if (id.isEmpty())
101             requestImageResource();
102         else {
103             document()->accessSVGExtensions()->addPendingResource(id, this);
104             ASSERT(hasPendingResources());
105         }
106     } else if (target->isSVGElement()) {
107         // Register us with the target in the dependencies map. Any change of hrefElement
108         // that leads to relayout/repainting now informs us, so we can react to it.
109         document()->accessSVGExtensions()->addElementReferencingTarget(this, static_cast<SVGElement*>(target));
110     }
111
112     invalidate();
113 }
114
115 bool SVGFEImageElement::isSupportedAttribute(const QualifiedName& attrName)
116 {
117     DEFINE_STATIC_LOCAL(HashSet<QualifiedName>, supportedAttributes, ());
118     if (supportedAttributes.isEmpty()) {
119         SVGURIReference::addSupportedAttributes(supportedAttributes);
120         SVGLangSpace::addSupportedAttributes(supportedAttributes);
121         SVGExternalResourcesRequired::addSupportedAttributes(supportedAttributes);
122         supportedAttributes.add(SVGNames::preserveAspectRatioAttr);
123     }
124     return supportedAttributes.contains<QualifiedName, SVGAttributeHashTranslator>(attrName);
125 }
126
127 void SVGFEImageElement::parseAttribute(const Attribute& attribute)
128 {
129     if (!isSupportedAttribute(attribute.name())) {
130         SVGFilterPrimitiveStandardAttributes::parseAttribute(attribute);
131         return;
132     }
133
134     if (attribute.name() == SVGNames::preserveAspectRatioAttr) {
135         SVGPreserveAspectRatio preserveAspectRatio;
136         preserveAspectRatio.parse(attribute.value());
137         setPreserveAspectRatioBaseValue(preserveAspectRatio);
138         return;
139     }
140
141     if (SVGURIReference::parseAttribute(attribute))
142         return;
143     if (SVGLangSpace::parseAttribute(attribute))
144         return;
145     if (SVGExternalResourcesRequired::parseAttribute(attribute))
146         return;
147
148     ASSERT_NOT_REACHED();
149 }
150
151 void SVGFEImageElement::svgAttributeChanged(const QualifiedName& attrName)
152 {
153     if (!isSupportedAttribute(attrName)) {
154         SVGFilterPrimitiveStandardAttributes::svgAttributeChanged(attrName);
155         return;
156     }
157
158     SVGElementInstance::InvalidationGuard invalidationGuard(this);
159     
160     if (attrName == SVGNames::preserveAspectRatioAttr) {
161         invalidate();
162         return;
163     }
164
165     if (SVGURIReference::isKnownAttribute(attrName)) {
166         buildPendingResource();
167         return;
168     }
169
170     if (SVGLangSpace::isKnownAttribute(attrName) || SVGExternalResourcesRequired::isKnownAttribute(attrName))
171         return;
172
173     ASSERT_NOT_REACHED();
174 }
175
176 Node::InsertionNotificationRequest SVGFEImageElement::insertedInto(ContainerNode* rootParent)
177 {
178     SVGFilterPrimitiveStandardAttributes::insertedInto(rootParent);
179     buildPendingResource();
180     return InsertionDone;
181 }
182
183 void SVGFEImageElement::removedFrom(ContainerNode* rootParent)
184 {
185     SVGFilterPrimitiveStandardAttributes::removedFrom(rootParent);
186     if (rootParent->inDocument())
187         clearResourceReferences();
188 }
189
190 void SVGFEImageElement::notifyFinished(CachedResource*)
191 {
192     if (!inDocument())
193         return;
194
195     Element* parent = parentElement();
196     ASSERT(parent);
197
198     if (!parent->hasTagName(SVGNames::filterTag) || !parent->renderer())
199         return;
200
201     RenderSVGResource::markForLayoutAndParentResourceInvalidation(parent->renderer());
202 }
203
204 PassRefPtr<FilterEffect> SVGFEImageElement::build(SVGFilterBuilder*, Filter* filter)
205 {
206     if (m_cachedImage)
207         return FEImage::createWithImage(filter, m_cachedImage->imageForRenderer(renderer()), preserveAspectRatio());
208     return FEImage::createWithIRIReference(filter, document(), href(), preserveAspectRatio());
209 }
210
211 void SVGFEImageElement::addSubresourceAttributeURLs(ListHashSet<KURL>& urls) const
212 {
213     SVGFilterPrimitiveStandardAttributes::addSubresourceAttributeURLs(urls);
214
215     addSubresourceURL(urls, document()->completeURL(href()));
216 }
217
218 }
219
220 #endif // ENABLE(SVG)