Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / core / 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 #include "core/svg/SVGFEImageElement.h"
25
26 #include "core/XLinkNames.h"
27 #include "core/dom/Document.h"
28 #include "core/fetch/FetchRequest.h"
29 #include "core/fetch/ResourceFetcher.h"
30 #include "core/rendering/svg/RenderSVGResource.h"
31 #include "core/svg/SVGPreserveAspectRatio.h"
32 #include "platform/graphics/Image.h"
33
34 namespace blink {
35
36 inline SVGFEImageElement::SVGFEImageElement(Document& document)
37     : SVGFilterPrimitiveStandardAttributes(SVGNames::feImageTag, document)
38     , SVGURIReference(this)
39     , m_preserveAspectRatio(SVGAnimatedPreserveAspectRatio::create(this, SVGNames::preserveAspectRatioAttr, SVGPreserveAspectRatio::create()))
40 {
41     ScriptWrappable::init(this);
42     addToPropertyMap(m_preserveAspectRatio);
43 }
44
45 DEFINE_NODE_FACTORY(SVGFEImageElement)
46
47 SVGFEImageElement::~SVGFEImageElement()
48 {
49 #if ENABLE(OILPAN)
50     if (m_cachedImage) {
51         m_cachedImage->removeClient(this);
52         m_cachedImage = 0;
53     }
54 #else
55     clearResourceReferences();
56 #endif
57 }
58
59 bool SVGFEImageElement::currentFrameHasSingleSecurityOrigin() const
60 {
61     if (m_cachedImage && m_cachedImage->image())
62         return m_cachedImage->image()->currentFrameHasSingleSecurityOrigin();
63
64     return true;
65 }
66
67 void SVGFEImageElement::clearResourceReferences()
68 {
69     if (m_cachedImage) {
70         m_cachedImage->removeClient(this);
71         m_cachedImage = 0;
72     }
73
74     removeAllOutgoingReferences();
75 }
76
77 void SVGFEImageElement::fetchImageResource()
78 {
79     FetchRequest request(ResourceRequest(ownerDocument()->completeURL(hrefString())), localName());
80     m_cachedImage = document().fetcher()->fetchImage(request);
81
82     if (m_cachedImage)
83         m_cachedImage->addClient(this);
84 }
85
86 void SVGFEImageElement::buildPendingResource()
87 {
88     clearResourceReferences();
89     if (!inDocument())
90         return;
91
92     AtomicString id;
93     Element* target = SVGURIReference::targetElementFromIRIString(hrefString(), treeScope(), &id);
94     if (!target) {
95         if (id.isEmpty())
96             fetchImageResource();
97         else {
98             document().accessSVGExtensions().addPendingResource(id, this);
99             ASSERT(hasPendingResources());
100         }
101     } else if (target->isSVGElement()) {
102         // Register us with the target in the dependencies map. Any change of hrefElement
103         // that leads to relayout/repainting now informs us, so we can react to it.
104         addReferenceTo(toSVGElement(target));
105     }
106
107     invalidate();
108 }
109
110 bool SVGFEImageElement::isSupportedAttribute(const QualifiedName& attrName)
111 {
112     DEFINE_STATIC_LOCAL(HashSet<QualifiedName>, supportedAttributes, ());
113     if (supportedAttributes.isEmpty()) {
114         SVGURIReference::addSupportedAttributes(supportedAttributes);
115         supportedAttributes.add(SVGNames::preserveAspectRatioAttr);
116     }
117     return supportedAttributes.contains<SVGAttributeHashTranslator>(attrName);
118 }
119
120 void SVGFEImageElement::parseAttribute(const QualifiedName& name, const AtomicString& value)
121 {
122     parseAttributeNew(name, value);
123 }
124
125 void SVGFEImageElement::svgAttributeChanged(const QualifiedName& attrName)
126 {
127     if (!isSupportedAttribute(attrName)) {
128         SVGFilterPrimitiveStandardAttributes::svgAttributeChanged(attrName);
129         return;
130     }
131
132     SVGElement::InvalidationGuard invalidationGuard(this);
133
134     if (attrName == SVGNames::preserveAspectRatioAttr) {
135         invalidate();
136         return;
137     }
138
139     if (SVGURIReference::isKnownAttribute(attrName)) {
140         buildPendingResource();
141         return;
142     }
143
144     ASSERT_NOT_REACHED();
145 }
146
147 Node::InsertionNotificationRequest SVGFEImageElement::insertedInto(ContainerNode* rootParent)
148 {
149     SVGFilterPrimitiveStandardAttributes::insertedInto(rootParent);
150     buildPendingResource();
151     return InsertionDone;
152 }
153
154 void SVGFEImageElement::removedFrom(ContainerNode* rootParent)
155 {
156     SVGFilterPrimitiveStandardAttributes::removedFrom(rootParent);
157     if (rootParent->inDocument())
158         clearResourceReferences();
159 }
160
161 void SVGFEImageElement::notifyFinished(Resource*)
162 {
163     if (!inDocument())
164         return;
165
166     Element* parent = parentElement();
167     ASSERT(parent);
168
169     if (!isSVGFilterElement(*parent) || !parent->renderer())
170         return;
171
172     if (RenderObject* renderer = this->renderer())
173         RenderSVGResource::markForLayoutAndParentResourceInvalidation(renderer);
174 }
175
176 PassRefPtr<FilterEffect> SVGFEImageElement::build(SVGFilterBuilder*, Filter* filter)
177 {
178     if (m_cachedImage)
179         return FEImage::createWithImage(filter, m_cachedImage->imageForRenderer(renderer()), m_preserveAspectRatio->currentValue());
180     return FEImage::createWithIRIReference(filter, treeScope(), hrefString(), m_preserveAspectRatio->currentValue());
181 }
182
183 }