Upstream version 11.40.277.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     parseAttributeNew(name, value);
63 }
64
65 bool SVGFEDiffuseLightingElement::setFilterEffectAttribute(FilterEffect* effect, const QualifiedName& attrName)
66 {
67     FEDiffuseLighting* diffuseLighting = static_cast<FEDiffuseLighting*>(effect);
68
69     if (attrName == SVGNames::lighting_colorAttr) {
70         RenderObject* renderer = this->renderer();
71         ASSERT(renderer);
72         ASSERT(renderer->style());
73         return diffuseLighting->setLightingColor(renderer->style()->svgStyle().lightingColor());
74     }
75     if (attrName == SVGNames::surfaceScaleAttr)
76         return diffuseLighting->setSurfaceScale(m_surfaceScale->currentValue()->value());
77     if (attrName == SVGNames::diffuseConstantAttr)
78         return diffuseLighting->setDiffuseConstant(m_diffuseConstant->currentValue()->value());
79
80     LightSource* lightSource = const_cast<LightSource*>(diffuseLighting->lightSource());
81     const SVGFELightElement* lightElement = SVGFELightElement::findLightElement(*this);
82     ASSERT(lightSource);
83     ASSERT(lightElement);
84     ASSERT(effect->filter());
85
86     if (attrName == SVGNames::azimuthAttr)
87         return lightSource->setAzimuth(lightElement->azimuth()->currentValue()->value());
88     if (attrName == SVGNames::elevationAttr)
89         return lightSource->setElevation(lightElement->elevation()->currentValue()->value());
90     if (attrName == SVGNames::xAttr || attrName == SVGNames::yAttr || attrName == SVGNames::zAttr)
91         return lightSource->setPosition(effect->filter()->resolve3dPoint(lightElement->position()));
92     if (attrName == SVGNames::pointsAtXAttr || attrName == SVGNames::pointsAtYAttr || attrName == SVGNames::pointsAtZAttr)
93         return lightSource->setPointsAt(effect->filter()->resolve3dPoint(lightElement->pointsAt()));
94     if (attrName == SVGNames::specularExponentAttr)
95         return lightSource->setSpecularExponent(lightElement->specularExponent()->currentValue()->value());
96     if (attrName == SVGNames::limitingConeAngleAttr)
97         return lightSource->setLimitingConeAngle(lightElement->limitingConeAngle()->currentValue()->value());
98
99     ASSERT_NOT_REACHED();
100     return false;
101 }
102
103 void SVGFEDiffuseLightingElement::svgAttributeChanged(const QualifiedName& attrName)
104 {
105     if (!isSupportedAttribute(attrName)) {
106         SVGFilterPrimitiveStandardAttributes::svgAttributeChanged(attrName);
107         return;
108     }
109
110     SVGElement::InvalidationGuard invalidationGuard(this);
111
112     if (attrName == SVGNames::surfaceScaleAttr
113         || attrName == SVGNames::diffuseConstantAttr
114         || attrName == SVGNames::kernelUnitLengthAttr
115         || attrName == SVGNames::lighting_colorAttr) {
116         primitiveAttributeChanged(attrName);
117         return;
118     }
119
120     if (attrName == SVGNames::inAttr) {
121         invalidate();
122         return;
123     }
124
125     ASSERT_NOT_REACHED();
126 }
127
128 void SVGFEDiffuseLightingElement::lightElementAttributeChanged(const SVGFELightElement* lightElement, const QualifiedName& attrName)
129 {
130     if (SVGFELightElement::findLightElement(*this) != lightElement)
131         return;
132
133     // The light element has different attribute names.
134     primitiveAttributeChanged(attrName);
135 }
136
137 PassRefPtr<FilterEffect> SVGFEDiffuseLightingElement::build(SVGFilterBuilder* filterBuilder, Filter* filter)
138 {
139     FilterEffect* input1 = filterBuilder->getEffectById(AtomicString(m_in1->currentValue()->value()));
140
141     if (!input1)
142         return nullptr;
143
144     SVGFELightElement* lightNode = SVGFELightElement::findLightElement(*this);
145     if (!lightNode)
146         return nullptr;
147
148     RenderObject* renderer = this->renderer();
149     if (!renderer)
150         return nullptr;
151
152     ASSERT(renderer->style());
153     Color color = renderer->style()->svgStyle().lightingColor();
154
155     RefPtr<LightSource> lightSource = lightNode->lightSource(filter);
156     RefPtr<FilterEffect> effect = FEDiffuseLighting::create(filter, color, m_surfaceScale->currentValue()->value(), m_diffuseConstant->currentValue()->value(),
157         kernelUnitLengthX()->currentValue()->value(), kernelUnitLengthY()->currentValue()->value(), lightSource.release());
158     effect->inputEffects().append(input1);
159     return effect.release();
160 }
161
162 } // namespace blink