Upstream version 10.39.225.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / core / svg / SVGFEDiffuseLightingElement.cpp
1 /*
2  * Copyright (C) 2005 Oliver Hunt <ojh16@student.canterbury.ac.nz>
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Library General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public License
15  * along with this library; see the file COPYING.LIB.  If not, write to
16  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17  * Boston, MA 02110-1301, USA.
18  */
19
20 #include "config.h"
21 #include "core/svg/SVGFEDiffuseLightingElement.h"
22
23 #include "core/rendering/RenderObject.h"
24 #include "core/rendering/style/RenderStyle.h"
25 #include "core/svg/SVGParserUtilities.h"
26 #include "core/svg/graphics/filters/SVGFilterBuilder.h"
27 #include "platform/graphics/filters/FEDiffuseLighting.h"
28 #include "platform/graphics/filters/FilterEffect.h"
29
30 namespace blink {
31
32 inline SVGFEDiffuseLightingElement::SVGFEDiffuseLightingElement(Document& document)
33     : SVGFilterPrimitiveStandardAttributes(SVGNames::feDiffuseLightingTag, document)
34     , m_diffuseConstant(SVGAnimatedNumber::create(this, SVGNames::diffuseConstantAttr, SVGNumber::create(1)))
35     , m_surfaceScale(SVGAnimatedNumber::create(this, SVGNames::surfaceScaleAttr, SVGNumber::create(1)))
36     , m_kernelUnitLength(SVGAnimatedNumberOptionalNumber::create(this, SVGNames::kernelUnitLengthAttr))
37     , m_in1(SVGAnimatedString::create(this, SVGNames::inAttr, SVGString::create()))
38 {
39     addToPropertyMap(m_diffuseConstant);
40     addToPropertyMap(m_surfaceScale);
41     addToPropertyMap(m_kernelUnitLength);
42     addToPropertyMap(m_in1);
43 }
44
45 DEFINE_NODE_FACTORY(SVGFEDiffuseLightingElement)
46
47 bool SVGFEDiffuseLightingElement::isSupportedAttribute(const QualifiedName& attrName)
48 {
49     DEFINE_STATIC_LOCAL(HashSet<QualifiedName>, supportedAttributes, ());
50     if (supportedAttributes.isEmpty()) {
51         supportedAttributes.add(SVGNames::inAttr);
52         supportedAttributes.add(SVGNames::diffuseConstantAttr);
53         supportedAttributes.add(SVGNames::surfaceScaleAttr);
54         supportedAttributes.add(SVGNames::kernelUnitLengthAttr);
55         supportedAttributes.add(SVGNames::lighting_colorAttr); // Even though it's a SVG-CSS property, we override its handling here.
56     }
57     return supportedAttributes.contains<SVGAttributeHashTranslator>(attrName);
58 }
59
60 void SVGFEDiffuseLightingElement::parseAttribute(const QualifiedName& name, const AtomicString& value)
61 {
62     if (!isSupportedAttribute(name) || name == SVGNames::lighting_colorAttr) {
63         SVGFilterPrimitiveStandardAttributes::parseAttribute(name, value);
64         return;
65     }
66
67     SVGParsingError parseError = NoError;
68
69     if (name == SVGNames::inAttr)
70         m_in1->setBaseValueAsString(value, parseError);
71     else if (name == SVGNames::diffuseConstantAttr)
72         m_diffuseConstant->setBaseValueAsString(value, parseError);
73     else if (name == SVGNames::surfaceScaleAttr)
74         m_surfaceScale->setBaseValueAsString(value, parseError);
75     else if (name == SVGNames::kernelUnitLengthAttr)
76         m_kernelUnitLength->setBaseValueAsString(value, parseError);
77     else
78         ASSERT_NOT_REACHED();
79
80     reportAttributeParsingError(parseError, name, value);
81 }
82
83 bool SVGFEDiffuseLightingElement::setFilterEffectAttribute(FilterEffect* effect, const QualifiedName& attrName)
84 {
85     FEDiffuseLighting* diffuseLighting = static_cast<FEDiffuseLighting*>(effect);
86
87     if (attrName == SVGNames::lighting_colorAttr) {
88         RenderObject* renderer = this->renderer();
89         ASSERT(renderer);
90         ASSERT(renderer->style());
91         return diffuseLighting->setLightingColor(renderer->style()->svgStyle().lightingColor());
92     }
93     if (attrName == SVGNames::surfaceScaleAttr)
94         return diffuseLighting->setSurfaceScale(m_surfaceScale->currentValue()->value());
95     if (attrName == SVGNames::diffuseConstantAttr)
96         return diffuseLighting->setDiffuseConstant(m_diffuseConstant->currentValue()->value());
97
98     LightSource* lightSource = const_cast<LightSource*>(diffuseLighting->lightSource());
99     const SVGFELightElement* lightElement = SVGFELightElement::findLightElement(*this);
100     ASSERT(lightSource);
101     ASSERT(lightElement);
102     ASSERT(effect->filter());
103
104     if (attrName == SVGNames::azimuthAttr)
105         return lightSource->setAzimuth(lightElement->azimuth()->currentValue()->value());
106     if (attrName == SVGNames::elevationAttr)
107         return lightSource->setElevation(lightElement->elevation()->currentValue()->value());
108     if (attrName == SVGNames::xAttr || attrName == SVGNames::yAttr || attrName == SVGNames::zAttr)
109         return lightSource->setPosition(effect->filter()->resolve3dPoint(lightElement->position()));
110     if (attrName == SVGNames::pointsAtXAttr || attrName == SVGNames::pointsAtYAttr || attrName == SVGNames::pointsAtZAttr)
111         return lightSource->setPointsAt(effect->filter()->resolve3dPoint(lightElement->pointsAt()));
112     if (attrName == SVGNames::specularExponentAttr)
113         return lightSource->setSpecularExponent(lightElement->specularExponent()->currentValue()->value());
114     if (attrName == SVGNames::limitingConeAngleAttr)
115         return lightSource->setLimitingConeAngle(lightElement->limitingConeAngle()->currentValue()->value());
116
117     ASSERT_NOT_REACHED();
118     return false;
119 }
120
121 void SVGFEDiffuseLightingElement::svgAttributeChanged(const QualifiedName& attrName)
122 {
123     if (!isSupportedAttribute(attrName)) {
124         SVGFilterPrimitiveStandardAttributes::svgAttributeChanged(attrName);
125         return;
126     }
127
128     SVGElement::InvalidationGuard invalidationGuard(this);
129
130     if (attrName == SVGNames::surfaceScaleAttr
131         || attrName == SVGNames::diffuseConstantAttr
132         || attrName == SVGNames::kernelUnitLengthAttr
133         || attrName == SVGNames::lighting_colorAttr) {
134         primitiveAttributeChanged(attrName);
135         return;
136     }
137
138     if (attrName == SVGNames::inAttr) {
139         invalidate();
140         return;
141     }
142
143     ASSERT_NOT_REACHED();
144 }
145
146 void SVGFEDiffuseLightingElement::lightElementAttributeChanged(const SVGFELightElement* lightElement, const QualifiedName& attrName)
147 {
148     if (SVGFELightElement::findLightElement(*this) != lightElement)
149         return;
150
151     // The light element has different attribute names.
152     primitiveAttributeChanged(attrName);
153 }
154
155 PassRefPtr<FilterEffect> SVGFEDiffuseLightingElement::build(SVGFilterBuilder* filterBuilder, Filter* filter)
156 {
157     FilterEffect* input1 = filterBuilder->getEffectById(AtomicString(m_in1->currentValue()->value()));
158
159     if (!input1)
160         return nullptr;
161
162     SVGFELightElement* lightNode = SVGFELightElement::findLightElement(*this);
163     if (!lightNode)
164         return nullptr;
165
166     RenderObject* renderer = this->renderer();
167     if (!renderer)
168         return nullptr;
169
170     ASSERT(renderer->style());
171     Color color = renderer->style()->svgStyle().lightingColor();
172
173     RefPtr<LightSource> lightSource = lightNode->lightSource(filter);
174     RefPtr<FilterEffect> effect = FEDiffuseLighting::create(filter, color, m_surfaceScale->currentValue()->value(), m_diffuseConstant->currentValue()->value(),
175         kernelUnitLengthX()->currentValue()->value(), kernelUnitLengthY()->currentValue()->value(), lightSource.release());
176     effect->inputEffects().append(input1);
177     return effect.release();
178 }
179
180 } // namespace blink