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>
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.
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.
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.
23 #include "core/svg/SVGFEImageElement.h"
25 #include "core/XLinkNames.h"
26 #include "core/dom/Document.h"
27 #include "core/fetch/FetchRequest.h"
28 #include "core/fetch/ResourceFetcher.h"
29 #include "core/svg/SVGDocumentExtensions.h"
30 #include "core/svg/SVGPreserveAspectRatio.h"
31 #include "core/svg/graphics/filters/SVGFEImage.h"
32 #include "platform/graphics/Image.h"
36 inline SVGFEImageElement::SVGFEImageElement(Document& document)
37 : SVGFilterPrimitiveStandardAttributes(SVGNames::feImageTag, document)
38 , SVGURIReference(this)
39 , m_preserveAspectRatio(SVGAnimatedPreserveAspectRatio::create(this, SVGNames::preserveAspectRatioAttr, SVGPreserveAspectRatio::create()))
41 addToPropertyMap(m_preserveAspectRatio);
44 DEFINE_NODE_FACTORY(SVGFEImageElement)
46 SVGFEImageElement::~SVGFEImageElement()
50 m_cachedImage->removeClient(this);
54 clearResourceReferences();
58 bool SVGFEImageElement::currentFrameHasSingleSecurityOrigin() const
60 if (m_cachedImage && m_cachedImage->image())
61 return m_cachedImage->image()->currentFrameHasSingleSecurityOrigin();
66 void SVGFEImageElement::clearResourceReferences()
69 m_cachedImage->removeClient(this);
73 removeAllOutgoingReferences();
76 void SVGFEImageElement::fetchImageResource()
78 FetchRequest request(ResourceRequest(ownerDocument()->completeURL(hrefString())), localName());
79 m_cachedImage = document().fetcher()->fetchImage(request);
82 m_cachedImage->addClient(this);
85 void SVGFEImageElement::buildPendingResource()
87 clearResourceReferences();
92 Element* target = SVGURIReference::targetElementFromIRIString(hrefString(), treeScope(), &id);
97 document().accessSVGExtensions().addPendingResource(id, this);
98 ASSERT(hasPendingResources());
100 } else if (target->isSVGElement()) {
101 // Register us with the target in the dependencies map. Any change of hrefElement
102 // that leads to relayout/repainting now informs us, so we can react to it.
103 addReferenceTo(toSVGElement(target));
109 bool SVGFEImageElement::isSupportedAttribute(const QualifiedName& attrName)
111 DEFINE_STATIC_LOCAL(HashSet<QualifiedName>, supportedAttributes, ());
112 if (supportedAttributes.isEmpty()) {
113 SVGURIReference::addSupportedAttributes(supportedAttributes);
114 supportedAttributes.add(SVGNames::preserveAspectRatioAttr);
116 return supportedAttributes.contains<SVGAttributeHashTranslator>(attrName);
119 void SVGFEImageElement::parseAttribute(const QualifiedName& name, const AtomicString& value)
121 parseAttributeNew(name, value);
124 void SVGFEImageElement::svgAttributeChanged(const QualifiedName& attrName)
126 if (!isSupportedAttribute(attrName)) {
127 SVGFilterPrimitiveStandardAttributes::svgAttributeChanged(attrName);
131 SVGElement::InvalidationGuard invalidationGuard(this);
133 if (attrName == SVGNames::preserveAspectRatioAttr) {
138 if (SVGURIReference::isKnownAttribute(attrName)) {
139 buildPendingResource();
143 ASSERT_NOT_REACHED();
146 Node::InsertionNotificationRequest SVGFEImageElement::insertedInto(ContainerNode* rootParent)
148 SVGFilterPrimitiveStandardAttributes::insertedInto(rootParent);
149 buildPendingResource();
150 return InsertionDone;
153 void SVGFEImageElement::removedFrom(ContainerNode* rootParent)
155 SVGFilterPrimitiveStandardAttributes::removedFrom(rootParent);
156 if (rootParent->inDocument())
157 clearResourceReferences();
160 void SVGFEImageElement::notifyFinished(Resource*)
165 Element* parent = parentElement();
168 if (!isSVGFilterElement(*parent) || !parent->renderer())
171 if (RenderObject* renderer = this->renderer())
172 markForLayoutAndParentResourceInvalidation(renderer);
175 PassRefPtr<FilterEffect> SVGFEImageElement::build(SVGFilterBuilder*, Filter* filter)
178 return FEImage::createWithImage(filter, m_cachedImage->imageForRenderer(renderer()), m_preserveAspectRatio->currentValue());
179 return FEImage::createWithIRIReference(filter, treeScope(), hrefString(), m_preserveAspectRatio->currentValue());