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/rendering/svg/RenderSVGResource.h"
30 #include "core/svg/SVGDocumentExtensions.h"
31 #include "core/svg/SVGPreserveAspectRatio.h"
32 #include "core/svg/graphics/filters/SVGFEImage.h"
33 #include "platform/graphics/Image.h"
34 #include "platform/graphics/ImageBuffer.h"
38 inline SVGFEImageElement::SVGFEImageElement(Document& document)
39 : SVGFilterPrimitiveStandardAttributes(SVGNames::feImageTag, document)
40 , SVGURIReference(this)
41 , m_preserveAspectRatio(SVGAnimatedPreserveAspectRatio::create(this, SVGNames::preserveAspectRatioAttr, SVGPreserveAspectRatio::create()))
43 addToPropertyMap(m_preserveAspectRatio);
46 DEFINE_NODE_FACTORY(SVGFEImageElement)
48 SVGFEImageElement::~SVGFEImageElement()
52 m_cachedImage->removeClient(this);
56 clearResourceReferences();
60 bool SVGFEImageElement::currentFrameHasSingleSecurityOrigin() const
62 if (m_cachedImage && m_cachedImage->image())
63 return m_cachedImage->image()->currentFrameHasSingleSecurityOrigin();
68 void SVGFEImageElement::clearResourceReferences()
71 m_cachedImage->removeClient(this);
75 removeAllOutgoingReferences();
78 void SVGFEImageElement::fetchImageResource()
80 FetchRequest request(ResourceRequest(ownerDocument()->completeURL(hrefString())), localName());
81 m_cachedImage = document().fetcher()->fetchImage(request);
84 m_cachedImage->addClient(this);
87 void SVGFEImageElement::buildPendingResource()
89 clearResourceReferences();
94 Element* target = SVGURIReference::targetElementFromIRIString(hrefString(), treeScope(), &id);
99 document().accessSVGExtensions().addPendingResource(id, this);
100 ASSERT(hasPendingResources());
102 } else if (target->isSVGElement()) {
103 // Register us with the target in the dependencies map. Any change of hrefElement
104 // that leads to relayout/repainting now informs us, so we can react to it.
105 addReferenceTo(toSVGElement(target));
111 bool SVGFEImageElement::isSupportedAttribute(const QualifiedName& attrName)
113 DEFINE_STATIC_LOCAL(HashSet<QualifiedName>, supportedAttributes, ());
114 if (supportedAttributes.isEmpty()) {
115 SVGURIReference::addSupportedAttributes(supportedAttributes);
116 supportedAttributes.add(SVGNames::preserveAspectRatioAttr);
118 return supportedAttributes.contains<SVGAttributeHashTranslator>(attrName);
121 void SVGFEImageElement::parseAttribute(const QualifiedName& name, const AtomicString& value)
123 parseAttributeNew(name, value);
126 void SVGFEImageElement::svgAttributeChanged(const QualifiedName& attrName)
128 if (!isSupportedAttribute(attrName)) {
129 SVGFilterPrimitiveStandardAttributes::svgAttributeChanged(attrName);
133 SVGElement::InvalidationGuard invalidationGuard(this);
135 if (attrName == SVGNames::preserveAspectRatioAttr) {
140 if (SVGURIReference::isKnownAttribute(attrName)) {
141 buildPendingResource();
145 ASSERT_NOT_REACHED();
148 Node::InsertionNotificationRequest SVGFEImageElement::insertedInto(ContainerNode* rootParent)
150 SVGFilterPrimitiveStandardAttributes::insertedInto(rootParent);
151 buildPendingResource();
152 return InsertionDone;
155 void SVGFEImageElement::removedFrom(ContainerNode* rootParent)
157 SVGFilterPrimitiveStandardAttributes::removedFrom(rootParent);
158 if (rootParent->inDocument())
159 clearResourceReferences();
162 void SVGFEImageElement::notifyFinished(Resource*)
167 Element* parent = parentElement();
170 if (!isSVGFilterElement(*parent) || !parent->renderer())
173 if (RenderObject* renderer = this->renderer())
174 RenderSVGResource::markForLayoutAndParentResourceInvalidation(renderer);
177 PassRefPtr<FilterEffect> SVGFEImageElement::build(SVGFilterBuilder*, Filter* filter)
180 return FEImage::createWithImage(filter, m_cachedImage->imageForRenderer(renderer()), m_preserveAspectRatio->currentValue());
181 return FEImage::createWithIRIReference(filter, treeScope(), hrefString(), m_preserveAspectRatio->currentValue());