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