Element's renderer factory should return RenderPtrs.
[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 #include "XLinkNames.h"
40
41 namespace WebCore {
42
43 // Animated property definitions
44 DEFINE_ANIMATED_PRESERVEASPECTRATIO(SVGFEImageElement, SVGNames::preserveAspectRatioAttr, PreserveAspectRatio, preserveAspectRatio)
45 DEFINE_ANIMATED_STRING(SVGFEImageElement, XLinkNames::hrefAttr, Href, href)
46 DEFINE_ANIMATED_BOOLEAN(SVGFEImageElement, SVGNames::externalResourcesRequiredAttr, ExternalResourcesRequired, externalResourcesRequired)
47
48 BEGIN_REGISTER_ANIMATED_PROPERTIES(SVGFEImageElement)
49     REGISTER_LOCAL_ANIMATED_PROPERTY(preserveAspectRatio)
50     REGISTER_LOCAL_ANIMATED_PROPERTY(href)
51     REGISTER_LOCAL_ANIMATED_PROPERTY(externalResourcesRequired)
52     REGISTER_PARENT_ANIMATED_PROPERTIES(SVGFilterPrimitiveStandardAttributes)
53 END_REGISTER_ANIMATED_PROPERTIES
54
55 inline SVGFEImageElement::SVGFEImageElement(const QualifiedName& tagName, Document& document)
56     : SVGFilterPrimitiveStandardAttributes(tagName, document)
57 {
58     ASSERT(hasTagName(SVGNames::feImageTag));
59     registerAnimatedPropertiesForSVGFEImageElement();
60 }
61
62 PassRefPtr<SVGFEImageElement> SVGFEImageElement::create(const QualifiedName& tagName, Document& document)
63 {
64     return adoptRef(new SVGFEImageElement(tagName, document));
65 }
66
67 SVGFEImageElement::~SVGFEImageElement()
68 {
69     clearResourceReferences();
70 }
71
72 void SVGFEImageElement::clearResourceReferences()
73 {
74     if (m_cachedImage) {
75         m_cachedImage->removeClient(this);
76         m_cachedImage = 0;
77     }
78
79     document().accessSVGExtensions()->removeAllTargetReferencesForElement(this);
80 }
81
82 void SVGFEImageElement::requestImageResource()
83 {
84     CachedResourceRequest request(ResourceRequest(document().completeURL(href())));
85     request.setInitiator(this);
86     m_cachedImage = document().cachedResourceLoader()->requestImage(request);
87
88     if (m_cachedImage)
89         m_cachedImage->addClient(this);
90 }
91
92 void SVGFEImageElement::buildPendingResource()
93 {
94     clearResourceReferences();
95     if (!inDocument())
96         return;
97
98     String id;
99     Element* target = SVGURIReference::targetElementFromIRIString(href(), document(), &id);
100     if (!target) {
101         if (id.isEmpty())
102             requestImageResource();
103         else {
104             document().accessSVGExtensions()->addPendingResource(id, this);
105             ASSERT(hasPendingResources());
106         }
107     } else if (target->isSVGElement()) {
108         // Register us with the target in the dependencies map. Any change of hrefElement
109         // that leads to relayout/repainting now informs us, so we can react to it.
110         document().accessSVGExtensions()->addElementReferencingTarget(this, toSVGElement(target));
111     }
112
113     invalidate();
114 }
115
116 bool SVGFEImageElement::isSupportedAttribute(const QualifiedName& attrName)
117 {
118     DEFINE_STATIC_LOCAL(HashSet<QualifiedName>, supportedAttributes, ());
119     if (supportedAttributes.isEmpty()) {
120         SVGURIReference::addSupportedAttributes(supportedAttributes);
121         SVGLangSpace::addSupportedAttributes(supportedAttributes);
122         SVGExternalResourcesRequired::addSupportedAttributes(supportedAttributes);
123         supportedAttributes.add(SVGNames::preserveAspectRatioAttr);
124     }
125     return supportedAttributes.contains<SVGAttributeHashTranslator>(attrName);
126 }
127
128 void SVGFEImageElement::parseAttribute(const QualifiedName& name, const AtomicString& value)
129 {
130     if (!isSupportedAttribute(name)) {
131         SVGFilterPrimitiveStandardAttributes::parseAttribute(name, value);
132         return;
133     }
134
135     if (name == SVGNames::preserveAspectRatioAttr) {
136         SVGPreserveAspectRatio preserveAspectRatio;
137         preserveAspectRatio.parse(value);
138         setPreserveAspectRatioBaseValue(preserveAspectRatio);
139         return;
140     }
141
142     if (SVGURIReference::parseAttribute(name, value))
143         return;
144     if (SVGLangSpace::parseAttribute(name, value))
145         return;
146     if (SVGExternalResourcesRequired::parseAttribute(name, value))
147         return;
148
149     ASSERT_NOT_REACHED();
150 }
151
152 void SVGFEImageElement::svgAttributeChanged(const QualifiedName& attrName)
153 {
154     if (!isSupportedAttribute(attrName)) {
155         SVGFilterPrimitiveStandardAttributes::svgAttributeChanged(attrName);
156         return;
157     }
158
159     SVGElementInstance::InvalidationGuard invalidationGuard(this);
160     
161     if (attrName == SVGNames::preserveAspectRatioAttr) {
162         invalidate();
163         return;
164     }
165
166     if (SVGURIReference::isKnownAttribute(attrName)) {
167         buildPendingResource();
168         return;
169     }
170
171     if (SVGLangSpace::isKnownAttribute(attrName) || SVGExternalResourcesRequired::isKnownAttribute(attrName))
172         return;
173
174     ASSERT_NOT_REACHED();
175 }
176
177 Node::InsertionNotificationRequest SVGFEImageElement::insertedInto(ContainerNode& rootParent)
178 {
179     SVGFilterPrimitiveStandardAttributes::insertedInto(rootParent);
180     buildPendingResource();
181     return InsertionDone;
182 }
183
184 void SVGFEImageElement::removedFrom(ContainerNode& rootParent)
185 {
186     SVGFilterPrimitiveStandardAttributes::removedFrom(rootParent);
187     if (rootParent.inDocument())
188         clearResourceReferences();
189 }
190
191 void SVGFEImageElement::notifyFinished(CachedResource*)
192 {
193     if (!inDocument())
194         return;
195
196     Element* parent = parentElement();
197     ASSERT(parent);
198
199     if (!parent->hasTagName(SVGNames::filterTag))
200         return;
201
202     RenderElement* parentRenderer = parent->renderer();
203     if (!parentRenderer)
204         return;
205
206     RenderSVGResource::markForLayoutAndParentResourceInvalidation(*parentRenderer);
207 }
208
209 PassRefPtr<FilterEffect> SVGFEImageElement::build(SVGFilterBuilder*, Filter* filter)
210 {
211     if (m_cachedImage)
212         return FEImage::createWithImage(filter, m_cachedImage->imageForRenderer(renderer()), preserveAspectRatio());
213     return FEImage::createWithIRIReference(filter, document(), href(), preserveAspectRatio());
214 }
215
216 void SVGFEImageElement::addSubresourceAttributeURLs(ListHashSet<URL>& urls) const
217 {
218     SVGFilterPrimitiveStandardAttributes::addSubresourceAttributeURLs(urls);
219
220     addSubresourceURL(urls, document().completeURL(href()));
221 }
222
223 }
224
225 #endif // ENABLE(SVG)