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