Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / core / svg / SVGFEBlendElement.cpp
1 /*
2  * Copyright (C) 2004, 2005, 2007 Nikolas Zimmermann <zimmermann@kde.org>
3  * Copyright (C) 2004, 2005, 2006 Rob Buis <buis@kde.org>
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Library General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Library General Public License for more details.
14  *
15  * You should have received a copy of the GNU Library General Public License
16  * along with this library; see the file COPYING.LIB.  If not, write to
17  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18  * Boston, MA 02110-1301, USA.
19  */
20
21 #include "config.h"
22
23 #include "core/svg/SVGFEBlendElement.h"
24
25 #include "SVGNames.h"
26 #include "platform/graphics/filters/FilterEffect.h"
27 #include "core/svg/SVGElementInstance.h"
28 #include "core/svg/graphics/filters/SVGFilterBuilder.h"
29
30 namespace WebCore {
31
32 // Animated property definitions
33 DEFINE_ANIMATED_ENUMERATION(SVGFEBlendElement, SVGNames::modeAttr, Mode, mode, BlendModeType)
34
35 BEGIN_REGISTER_ANIMATED_PROPERTIES(SVGFEBlendElement)
36     REGISTER_LOCAL_ANIMATED_PROPERTY(mode)
37     REGISTER_PARENT_ANIMATED_PROPERTIES(SVGFilterPrimitiveStandardAttributes)
38 END_REGISTER_ANIMATED_PROPERTIES
39
40 inline SVGFEBlendElement::SVGFEBlendElement(Document& document)
41     : SVGFilterPrimitiveStandardAttributes(SVGNames::feBlendTag, document)
42     , m_in1(SVGAnimatedString::create(this, SVGNames::inAttr, SVGString::create()))
43     , m_in2(SVGAnimatedString::create(this, SVGNames::in2Attr, SVGString::create()))
44     , m_mode(FEBLEND_MODE_NORMAL)
45 {
46     ScriptWrappable::init(this);
47     addToPropertyMap(m_in1);
48     addToPropertyMap(m_in2);
49     registerAnimatedPropertiesForSVGFEBlendElement();
50 }
51
52 PassRefPtr<SVGFEBlendElement> SVGFEBlendElement::create(Document& document)
53 {
54     return adoptRef(new SVGFEBlendElement(document));
55 }
56
57 bool SVGFEBlendElement::isSupportedAttribute(const QualifiedName& attrName)
58 {
59     DEFINE_STATIC_LOCAL(HashSet<QualifiedName>, supportedAttributes, ());
60     if (supportedAttributes.isEmpty()) {
61         supportedAttributes.add(SVGNames::modeAttr);
62         supportedAttributes.add(SVGNames::inAttr);
63         supportedAttributes.add(SVGNames::in2Attr);
64     }
65     return supportedAttributes.contains<SVGAttributeHashTranslator>(attrName);
66 }
67
68 void SVGFEBlendElement::parseAttribute(const QualifiedName& name, const AtomicString& value)
69 {
70     if (!isSupportedAttribute(name)) {
71         SVGFilterPrimitiveStandardAttributes::parseAttribute(name, value);
72         return;
73     }
74
75     if (name == SVGNames::modeAttr) {
76         BlendModeType propertyValue = SVGPropertyTraits<BlendModeType>::fromString(value);
77         if (propertyValue > 0)
78             setModeBaseValue(propertyValue);
79         return;
80     }
81
82     SVGParsingError parseError = NoError;
83
84     if (name == SVGNames::inAttr)
85         m_in1->setBaseValueAsString(value, parseError);
86     else if (name == SVGNames::in2Attr)
87         m_in2->setBaseValueAsString(value, parseError);
88     else
89         ASSERT_NOT_REACHED();
90
91     reportAttributeParsingError(parseError, name, value);
92 }
93
94 bool SVGFEBlendElement::setFilterEffectAttribute(FilterEffect* effect, const QualifiedName& attrName)
95 {
96     FEBlend* blend = static_cast<FEBlend*>(effect);
97     if (attrName == SVGNames::modeAttr)
98         return blend->setBlendMode(modeCurrentValue());
99
100     ASSERT_NOT_REACHED();
101     return false;
102 }
103
104 void SVGFEBlendElement::svgAttributeChanged(const QualifiedName& attrName)
105 {
106     if (!isSupportedAttribute(attrName)) {
107         SVGFilterPrimitiveStandardAttributes::svgAttributeChanged(attrName);
108         return;
109     }
110
111     SVGElementInstance::InvalidationGuard invalidationGuard(this);
112
113     if (attrName == SVGNames::modeAttr) {
114         primitiveAttributeChanged(attrName);
115         return;
116     }
117
118     if (attrName == SVGNames::inAttr || attrName == SVGNames::in2Attr) {
119         invalidate();
120         return;
121     }
122
123     ASSERT_NOT_REACHED();
124 }
125
126 PassRefPtr<FilterEffect> SVGFEBlendElement::build(SVGFilterBuilder* filterBuilder, Filter* filter)
127 {
128     FilterEffect* input1 = filterBuilder->getEffectById(AtomicString(m_in1->currentValue()->value()));
129     FilterEffect* input2 = filterBuilder->getEffectById(AtomicString(m_in2->currentValue()->value()));
130
131     if (!input1 || !input2)
132         return 0;
133
134     RefPtr<FilterEffect> effect = FEBlend::create(filter, modeCurrentValue());
135     FilterEffectVector& inputEffects = effect->inputEffects();
136     inputEffects.reserveCapacity(2);
137     inputEffects.append(input1);
138     inputEffects.append(input2);
139     return effect.release();
140 }
141
142 }