1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4  * License, v. 2.0. If a copy of the MPL was not distributed with this
5  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 
7 #include "mozilla/dom/SVGFESpecularLightingElement.h"
8 #include "mozilla/dom/SVGFESpecularLightingElementBinding.h"
9 #include "mozilla/SVGFilterInstance.h"
10 #include "mozilla/dom/Document.h"
11 #include "mozilla/dom/BindContext.h"
12 
13 NS_IMPL_NS_NEW_SVG_ELEMENT(FESpecularLighting)
14 
15 using namespace mozilla::gfx;
16 
17 namespace mozilla {
18 namespace dom {
19 
WrapNode(JSContext * aCx,JS::Handle<JSObject * > aGivenProto)20 JSObject* SVGFESpecularLightingElement::WrapNode(
21     JSContext* aCx, JS::Handle<JSObject*> aGivenProto) {
22   return SVGFESpecularLightingElement_Binding::Wrap(aCx, this, aGivenProto);
23 }
24 
25 //----------------------------------------------------------------------
26 // nsINode methods
27 
NS_IMPL_ELEMENT_CLONE_WITH_INIT(SVGFESpecularLightingElement)28 NS_IMPL_ELEMENT_CLONE_WITH_INIT(SVGFESpecularLightingElement)
29 
30 already_AddRefed<DOMSVGAnimatedString> SVGFESpecularLightingElement::In1() {
31   return mStringAttributes[IN1].ToDOMAnimatedString(this);
32 }
33 
34 already_AddRefed<DOMSVGAnimatedNumber>
SurfaceScale()35 SVGFESpecularLightingElement::SurfaceScale() {
36   return mNumberAttributes[SURFACE_SCALE].ToDOMAnimatedNumber(this);
37 }
38 
39 already_AddRefed<DOMSVGAnimatedNumber>
SpecularConstant()40 SVGFESpecularLightingElement::SpecularConstant() {
41   return mNumberAttributes[SPECULAR_CONSTANT].ToDOMAnimatedNumber(this);
42 }
43 
44 already_AddRefed<DOMSVGAnimatedNumber>
SpecularExponent()45 SVGFESpecularLightingElement::SpecularExponent() {
46   return mNumberAttributes[SPECULAR_EXPONENT].ToDOMAnimatedNumber(this);
47 }
48 
49 already_AddRefed<DOMSVGAnimatedNumber>
KernelUnitLengthX()50 SVGFESpecularLightingElement::KernelUnitLengthX() {
51   return mNumberPairAttributes[KERNEL_UNIT_LENGTH].ToDOMAnimatedNumber(
52       SVGAnimatedNumberPair::eFirst, this);
53 }
54 
55 already_AddRefed<DOMSVGAnimatedNumber>
KernelUnitLengthY()56 SVGFESpecularLightingElement::KernelUnitLengthY() {
57   return mNumberPairAttributes[KERNEL_UNIT_LENGTH].ToDOMAnimatedNumber(
58       SVGAnimatedNumberPair::eSecond, this);
59 }
60 
61 //----------------------------------------------------------------------
62 // SVGElement methods
63 
64 FilterPrimitiveDescription
GetPrimitiveDescription(SVGFilterInstance * aInstance,const IntRect & aFilterSubregion,const nsTArray<bool> & aInputsAreTainted,nsTArray<RefPtr<SourceSurface>> & aInputImages)65 SVGFESpecularLightingElement::GetPrimitiveDescription(
66     SVGFilterInstance* aInstance, const IntRect& aFilterSubregion,
67     const nsTArray<bool>& aInputsAreTainted,
68     nsTArray<RefPtr<SourceSurface>>& aInputImages) {
69   float specularExponent = mNumberAttributes[SPECULAR_EXPONENT].GetAnimValue();
70   float specularConstant = mNumberAttributes[SPECULAR_CONSTANT].GetAnimValue();
71 
72   // specification defined range (15.22)
73   if (specularExponent < 1 || specularExponent > 128) {
74     return FilterPrimitiveDescription();
75   }
76 
77   SpecularLightingAttributes atts;
78   atts.mLightingConstant = specularConstant;
79   atts.mSpecularExponent = specularExponent;
80   if (!AddLightingAttributes(static_cast<DiffuseLightingAttributes*>(&atts),
81                              aInstance)) {
82     return FilterPrimitiveDescription();
83   }
84 
85   return FilterPrimitiveDescription(AsVariant(std::move(atts)));
86 }
87 
AttributeAffectsRendering(int32_t aNameSpaceID,nsAtom * aAttribute) const88 bool SVGFESpecularLightingElement::AttributeAffectsRendering(
89     int32_t aNameSpaceID, nsAtom* aAttribute) const {
90   return SVGFESpecularLightingElementBase::AttributeAffectsRendering(
91              aNameSpaceID, aAttribute) ||
92          (aNameSpaceID == kNameSpaceID_None &&
93           (aAttribute == nsGkAtoms::specularConstant ||
94            aAttribute == nsGkAtoms::specularExponent));
95 }
96 
BindToTree(BindContext & aCtx,nsINode & aParent)97 nsresult SVGFESpecularLightingElement::BindToTree(BindContext& aCtx,
98                                                   nsINode& aParent) {
99   if (aCtx.InComposedDoc()) {
100     aCtx.OwnerDoc().SetUseCounter(eUseCounter_custom_feSpecularLighting);
101   }
102 
103   return SVGFE::BindToTree(aCtx, aParent);
104 }
105 
106 }  // namespace dom
107 }  // namespace mozilla
108