Stop using PassRefPtr in platform/graphics
[WebKit-https.git] / Source / WebCore / svg / SVGFESpecularLightingElement.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  * Copyright (C) 2005 Oliver Hunt <oliver@nerget.com>
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 "SVGFESpecularLightingElement.h"
24
25 #include "FilterEffect.h"
26 #include "RenderStyle.h"
27 #include "SVGFELightElement.h"
28 #include "SVGFilterBuilder.h"
29 #include "SVGNames.h"
30 #include "SVGParserUtilities.h"
31
32 namespace WebCore {
33
34 // Animated property definitions
35 DEFINE_ANIMATED_STRING(SVGFESpecularLightingElement, SVGNames::inAttr, In1, in1)
36 DEFINE_ANIMATED_NUMBER(SVGFESpecularLightingElement, SVGNames::specularConstantAttr, SpecularConstant, specularConstant)
37 DEFINE_ANIMATED_NUMBER(SVGFESpecularLightingElement, SVGNames::specularExponentAttr, SpecularExponent, specularExponent)
38 DEFINE_ANIMATED_NUMBER(SVGFESpecularLightingElement, SVGNames::surfaceScaleAttr, SurfaceScale, surfaceScale)
39 DEFINE_ANIMATED_NUMBER_MULTIPLE_WRAPPERS(SVGFESpecularLightingElement, SVGNames::kernelUnitLengthAttr, kernelUnitLengthXIdentifier(), KernelUnitLengthX, kernelUnitLengthX)
40 DEFINE_ANIMATED_NUMBER_MULTIPLE_WRAPPERS(SVGFESpecularLightingElement, SVGNames::kernelUnitLengthAttr, kernelUnitLengthYIdentifier(), KernelUnitLengthY, kernelUnitLengthY)
41
42 BEGIN_REGISTER_ANIMATED_PROPERTIES(SVGFESpecularLightingElement)
43     REGISTER_LOCAL_ANIMATED_PROPERTY(in1)
44     REGISTER_LOCAL_ANIMATED_PROPERTY(specularConstant)
45     REGISTER_LOCAL_ANIMATED_PROPERTY(specularExponent)
46     REGISTER_LOCAL_ANIMATED_PROPERTY(surfaceScale)
47     REGISTER_LOCAL_ANIMATED_PROPERTY(kernelUnitLengthX)
48     REGISTER_LOCAL_ANIMATED_PROPERTY(kernelUnitLengthY)
49     REGISTER_PARENT_ANIMATED_PROPERTIES(SVGFilterPrimitiveStandardAttributes)
50 END_REGISTER_ANIMATED_PROPERTIES
51
52 inline SVGFESpecularLightingElement::SVGFESpecularLightingElement(const QualifiedName& tagName, Document& document)
53     : SVGFilterPrimitiveStandardAttributes(tagName, document)
54     , m_specularConstant(1)
55     , m_specularExponent(1)
56     , m_surfaceScale(1)
57 {
58     ASSERT(hasTagName(SVGNames::feSpecularLightingTag));
59     registerAnimatedPropertiesForSVGFESpecularLightingElement();
60 }
61
62 Ref<SVGFESpecularLightingElement> SVGFESpecularLightingElement::create(const QualifiedName& tagName, Document& document)
63 {
64     return adoptRef(*new SVGFESpecularLightingElement(tagName, document));
65 }
66
67 const AtomicString& SVGFESpecularLightingElement::kernelUnitLengthXIdentifier()
68 {
69     static NeverDestroyed<AtomicString> s_identifier("SVGKernelUnitLengthX", AtomicString::ConstructFromLiteral);
70     return s_identifier;
71 }
72
73 const AtomicString& SVGFESpecularLightingElement::kernelUnitLengthYIdentifier()
74 {
75     static NeverDestroyed<AtomicString> s_identifier("SVGKernelUnitLengthY", AtomicString::ConstructFromLiteral);
76     return s_identifier;
77 }
78
79 void SVGFESpecularLightingElement::parseAttribute(const QualifiedName& name, const AtomicString& value)
80 {
81     if (name == SVGNames::inAttr) {
82         setIn1BaseValue(value);
83         return;
84     }
85
86     if (name == SVGNames::surfaceScaleAttr) {
87         setSurfaceScaleBaseValue(value.toFloat());
88         return;
89     }
90
91     if (name == SVGNames::specularConstantAttr) {
92         setSpecularConstantBaseValue(value.toFloat());
93         return;
94     }
95
96     if (name == SVGNames::specularExponentAttr) {
97         setSpecularExponentBaseValue(value.toFloat());
98         return;
99     }
100
101     if (name == SVGNames::kernelUnitLengthAttr) {
102         float x, y;
103         if (parseNumberOptionalNumber(value, x, y)) {
104             setKernelUnitLengthXBaseValue(x);
105             setKernelUnitLengthYBaseValue(y);
106         }
107         return;
108     }
109
110     SVGFilterPrimitiveStandardAttributes::parseAttribute(name, value);
111 }
112
113 bool SVGFESpecularLightingElement::setFilterEffectAttribute(FilterEffect* effect, const QualifiedName& attrName)
114 {
115     FESpecularLighting* specularLighting = static_cast<FESpecularLighting*>(effect);
116
117     if (attrName == SVGNames::lighting_colorAttr) {
118         RenderObject* renderer = this->renderer();
119         ASSERT(renderer);
120         return specularLighting->setLightingColor(renderer->style().svgStyle().lightingColor());
121     }
122     if (attrName == SVGNames::surfaceScaleAttr)
123         return specularLighting->setSurfaceScale(surfaceScale());
124     if (attrName == SVGNames::specularConstantAttr)
125         return specularLighting->setSpecularConstant(specularConstant());
126     if (attrName == SVGNames::specularExponentAttr)
127         return specularLighting->setSpecularExponent(specularExponent());
128
129     auto& lightSource = const_cast<LightSource&>(specularLighting->lightSource());
130     const SVGFELightElement* lightElement = SVGFELightElement::findLightElement(this);
131     ASSERT(lightElement);
132
133     if (attrName == SVGNames::azimuthAttr)
134         return lightSource.setAzimuth(lightElement->azimuth());
135     if (attrName == SVGNames::elevationAttr)
136         return lightSource.setElevation(lightElement->elevation());
137     if (attrName == SVGNames::xAttr)
138         return lightSource.setX(lightElement->x());
139     if (attrName == SVGNames::yAttr)
140         return lightSource.setY(lightElement->y());
141     if (attrName == SVGNames::zAttr)
142         return lightSource.setZ(lightElement->z());
143     if (attrName == SVGNames::pointsAtXAttr)
144         return lightSource.setPointsAtX(lightElement->pointsAtX());
145     if (attrName == SVGNames::pointsAtYAttr)
146         return lightSource.setPointsAtY(lightElement->pointsAtY());
147     if (attrName == SVGNames::pointsAtZAttr)
148         return lightSource.setPointsAtZ(lightElement->pointsAtZ());
149     if (attrName == SVGNames::specularExponentAttr)
150         return lightSource.setSpecularExponent(lightElement->specularExponent());
151     if (attrName == SVGNames::limitingConeAngleAttr)
152         return lightSource.setLimitingConeAngle(lightElement->limitingConeAngle());
153
154     ASSERT_NOT_REACHED();
155     return false;
156 }
157
158 void SVGFESpecularLightingElement::svgAttributeChanged(const QualifiedName& attrName)
159 {
160     if (attrName == SVGNames::surfaceScaleAttr || attrName == SVGNames::specularConstantAttr || attrName == SVGNames::specularExponentAttr || attrName == SVGNames::kernelUnitLengthAttr) {
161         InstanceInvalidationGuard guard(*this);
162         primitiveAttributeChanged(attrName);
163         return;
164     }
165
166     if (attrName == SVGNames::inAttr) {
167         InstanceInvalidationGuard guard(*this);
168         invalidate();
169         return;
170     }
171
172     SVGFilterPrimitiveStandardAttributes::svgAttributeChanged(attrName);
173 }
174
175 void SVGFESpecularLightingElement::lightElementAttributeChanged(const SVGFELightElement* lightElement, const QualifiedName& attrName)
176 {
177     if (SVGFELightElement::findLightElement(this) != lightElement)
178         return;
179
180     // The light element has different attribute names so attrName can identify the requested attribute.
181     primitiveAttributeChanged(attrName);
182 }
183
184 RefPtr<FilterEffect> SVGFESpecularLightingElement::build(SVGFilterBuilder* filterBuilder, Filter& filter)
185 {
186     FilterEffect* input1 = filterBuilder->getEffectById(in1());
187
188     if (!input1)
189         return nullptr;
190
191     auto lightSource = SVGFELightElement::findLightSource(this);
192     if (!lightSource)
193         return nullptr;
194
195     RenderObject* renderer = this->renderer();
196     if (!renderer)
197         return nullptr;
198     
199     const Color& color = renderer->style().svgStyle().lightingColor();
200
201     RefPtr<FilterEffect> effect = FESpecularLighting::create(filter, color, surfaceScale(), specularConstant(), specularExponent(), kernelUnitLengthX(), kernelUnitLengthY(), lightSource.releaseNonNull());
202     effect->inputEffects().append(input1);
203     return effect;
204 }
205
206 }