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