CachedImage: ensure clients overrides imageChanged instead of notifyFinished
[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 "SVGElementInstance.h"
35 #include "SVGNames.h"
36 #include "SVGPreserveAspectRatio.h"
37 #include "XLinkNames.h"
38 #include <wtf/NeverDestroyed.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 Ref<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     document().accessSVGExtensions().removeAllTargetReferencesForElement(this);
79 }
80
81 void SVGFEImageElement::requestImageResource()
82 {
83     CachedResourceRequest request(ResourceRequest(document().completeURL(href())));
84     request.setInitiator(this);
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, downcast<SVGElement>(target));
110     }
111
112     invalidate();
113 }
114
115 bool SVGFEImageElement::isSupportedAttribute(const QualifiedName& attrName)
116 {
117     static NeverDestroyed<HashSet<QualifiedName>> supportedAttributes;
118     if (supportedAttributes.get().isEmpty()) {
119         SVGURIReference::addSupportedAttributes(supportedAttributes);
120         SVGLangSpace::addSupportedAttributes(supportedAttributes);
121         SVGExternalResourcesRequired::addSupportedAttributes(supportedAttributes);
122         supportedAttributes.get().add(SVGNames::preserveAspectRatioAttr);
123     }
124     return supportedAttributes.get().contains<SVGAttributeHashTranslator>(attrName);
125 }
126
127 void SVGFEImageElement::parseAttribute(const QualifiedName& name, const AtomicString& value)
128 {
129     if (!isSupportedAttribute(name)) {
130         SVGFilterPrimitiveStandardAttributes::parseAttribute(name, value);
131         return;
132     }
133
134     if (name == SVGNames::preserveAspectRatioAttr) {
135         SVGPreserveAspectRatio preserveAspectRatio;
136         preserveAspectRatio.parse(value);
137         setPreserveAspectRatioBaseValue(preserveAspectRatio);
138         return;
139     }
140
141     if (SVGURIReference::parseAttribute(name, value))
142         return;
143     if (SVGLangSpace::parseAttribute(name, value))
144         return;
145     if (SVGExternalResourcesRequired::parseAttribute(name, value))
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     return InsertionShouldCallDidNotifySubtreeInsertions;
180 }
181
182 void SVGFEImageElement::didNotifySubtreeInsertions(ContainerNode*)
183 {
184     buildPendingResource();
185 }
186
187 void SVGFEImageElement::removedFrom(ContainerNode& rootParent)
188 {
189     SVGFilterPrimitiveStandardAttributes::removedFrom(rootParent);
190     if (rootParent.inDocument())
191         clearResourceReferences();
192 }
193
194 void SVGFEImageElement::imageChanged(CachedImage* cachedImage, const IntRect*)
195 {
196     if (!cachedImage || !cachedImage->isLoaded())
197         return;
198
199     if (!inDocument())
200         return;
201
202     Element* parent = parentElement();
203     ASSERT(parent);
204
205     if (!parent->hasTagName(SVGNames::filterTag))
206         return;
207
208     RenderElement* parentRenderer = parent->renderer();
209     if (!parentRenderer)
210         return;
211
212     RenderSVGResource::markForLayoutAndParentResourceInvalidation(*parentRenderer);
213 }
214
215 PassRefPtr<FilterEffect> SVGFEImageElement::build(SVGFilterBuilder*, Filter* filter)
216 {
217     if (m_cachedImage)
218         return FEImage::createWithImage(filter, m_cachedImage->imageForRenderer(renderer()), preserveAspectRatio());
219     return FEImage::createWithIRIReference(filter, document(), href(), preserveAspectRatio());
220 }
221
222 void SVGFEImageElement::addSubresourceAttributeURLs(ListHashSet<URL>& urls) const
223 {
224     SVGFilterPrimitiveStandardAttributes::addSubresourceAttributeURLs(urls);
225
226     addSubresourceURL(urls, document().completeURL(href()));
227 }
228
229 }