SVGFECompositeElement.cpp (5562B)
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/SVGFECompositeElement.h" 8 9 #include "mozilla/dom/BindContext.h" 10 #include "mozilla/dom/Document.h" 11 #include "mozilla/dom/SVGFECompositeElementBinding.h" 12 13 NS_IMPL_NS_NEW_SVG_ELEMENT(FEComposite) 14 15 using namespace mozilla::gfx; 16 17 namespace mozilla::dom { 18 19 JSObject* SVGFECompositeElement::WrapNode(JSContext* aCx, 20 JS::Handle<JSObject*> aGivenProto) { 21 return SVGFECompositeElement_Binding::Wrap(aCx, this, aGivenProto); 22 } 23 24 SVGElement::NumberInfo SVGFECompositeElement::sNumberInfo[4] = { 25 {nsGkAtoms::k1, 0}, 26 {nsGkAtoms::k2, 0}, 27 {nsGkAtoms::k3, 0}, 28 {nsGkAtoms::k4, 0}}; 29 30 SVGEnumMapping SVGFECompositeElement::sOperatorMap[] = { 31 {nsGkAtoms::over, SVG_FECOMPOSITE_OPERATOR_OVER}, 32 {nsGkAtoms::in, SVG_FECOMPOSITE_OPERATOR_IN}, 33 {nsGkAtoms::out, SVG_FECOMPOSITE_OPERATOR_OUT}, 34 {nsGkAtoms::atop, SVG_FECOMPOSITE_OPERATOR_ATOP}, 35 {nsGkAtoms::xor_, SVG_FECOMPOSITE_OPERATOR_XOR}, 36 {nsGkAtoms::arithmetic, SVG_FECOMPOSITE_OPERATOR_ARITHMETIC}, 37 {nsGkAtoms::lighter, SVG_FECOMPOSITE_OPERATOR_LIGHTER}, 38 {nullptr, 0}}; 39 40 SVGElement::EnumInfo SVGFECompositeElement::sEnumInfo[1] = { 41 {nsGkAtoms::_operator, sOperatorMap, SVG_FECOMPOSITE_OPERATOR_OVER}}; 42 43 SVGElement::StringInfo SVGFECompositeElement::sStringInfo[3] = { 44 {nsGkAtoms::result, kNameSpaceID_None, true}, 45 {nsGkAtoms::in, kNameSpaceID_None, true}, 46 {nsGkAtoms::in2, kNameSpaceID_None, true}}; 47 48 //---------------------------------------------------------------------- 49 // nsINode methods 50 51 NS_IMPL_ELEMENT_CLONE_WITH_INIT(SVGFECompositeElement) 52 53 already_AddRefed<DOMSVGAnimatedString> SVGFECompositeElement::In1() { 54 return mStringAttributes[IN1].ToDOMAnimatedString(this); 55 } 56 57 already_AddRefed<DOMSVGAnimatedString> SVGFECompositeElement::In2() { 58 return mStringAttributes[IN2].ToDOMAnimatedString(this); 59 } 60 61 already_AddRefed<DOMSVGAnimatedEnumeration> SVGFECompositeElement::Operator() { 62 return mEnumAttributes[OPERATOR].ToDOMAnimatedEnum(this); 63 } 64 65 already_AddRefed<DOMSVGAnimatedNumber> SVGFECompositeElement::K1() { 66 return mNumberAttributes[ATTR_K1].ToDOMAnimatedNumber(this); 67 } 68 69 already_AddRefed<DOMSVGAnimatedNumber> SVGFECompositeElement::K2() { 70 return mNumberAttributes[ATTR_K2].ToDOMAnimatedNumber(this); 71 } 72 73 already_AddRefed<DOMSVGAnimatedNumber> SVGFECompositeElement::K3() { 74 return mNumberAttributes[ATTR_K3].ToDOMAnimatedNumber(this); 75 } 76 77 already_AddRefed<DOMSVGAnimatedNumber> SVGFECompositeElement::K4() { 78 return mNumberAttributes[ATTR_K4].ToDOMAnimatedNumber(this); 79 } 80 81 void SVGFECompositeElement::SetK(float k1, float k2, float k3, float k4) { 82 mNumberAttributes[ATTR_K1].SetBaseValue(k1, this); 83 mNumberAttributes[ATTR_K2].SetBaseValue(k2, this); 84 mNumberAttributes[ATTR_K3].SetBaseValue(k3, this); 85 mNumberAttributes[ATTR_K4].SetBaseValue(k4, this); 86 } 87 88 FilterPrimitiveDescription SVGFECompositeElement::GetPrimitiveDescription( 89 SVGFilterInstance* aInstance, const IntRect& aFilterSubregion, 90 const nsTArray<bool>& aInputsAreTainted, 91 nsTArray<RefPtr<SourceSurface>>& aInputImages) { 92 CompositeAttributes atts; 93 uint32_t op = mEnumAttributes[OPERATOR].GetAnimValue(); 94 atts.mOperator = op; 95 96 if (op == SVG_FECOMPOSITE_OPERATOR_ARITHMETIC) { 97 std::array<float, 4> k; 98 GetAnimatedNumberValues(&k[0], &k[1], &k[2], &k[3], nullptr); 99 atts.mCoefficients.AppendElements(Span(k)); 100 } 101 102 return FilterPrimitiveDescription(AsVariant(std::move(atts))); 103 } 104 105 bool SVGFECompositeElement::AttributeAffectsRendering( 106 int32_t aNameSpaceID, nsAtom* aAttribute) const { 107 return SVGFECompositeElementBase::AttributeAffectsRendering(aNameSpaceID, 108 aAttribute) || 109 (aNameSpaceID == kNameSpaceID_None && 110 (aAttribute == nsGkAtoms::in || aAttribute == nsGkAtoms::in2 || 111 aAttribute == nsGkAtoms::k1 || aAttribute == nsGkAtoms::k2 || 112 aAttribute == nsGkAtoms::k3 || aAttribute == nsGkAtoms::k4 || 113 aAttribute == nsGkAtoms::_operator)); 114 } 115 116 void SVGFECompositeElement::GetSourceImageNames( 117 nsTArray<SVGStringInfo>& aSources) { 118 aSources.AppendElement(SVGStringInfo(&mStringAttributes[IN1], this)); 119 aSources.AppendElement(SVGStringInfo(&mStringAttributes[IN2], this)); 120 } 121 122 nsresult SVGFECompositeElement::BindToTree(BindContext& aCtx, 123 nsINode& aParent) { 124 if (aCtx.InComposedDoc()) { 125 aCtx.OwnerDoc().SetUseCounter(eUseCounter_custom_feComposite); 126 } 127 128 return SVGFECompositeElementBase::BindToTree(aCtx, aParent); 129 } 130 131 //---------------------------------------------------------------------- 132 // SVGElement methods 133 134 SVGElement::NumberAttributesInfo SVGFECompositeElement::GetNumberInfo() { 135 return NumberAttributesInfo(mNumberAttributes, sNumberInfo, 136 std::size(sNumberInfo)); 137 } 138 139 SVGElement::EnumAttributesInfo SVGFECompositeElement::GetEnumInfo() { 140 return EnumAttributesInfo(mEnumAttributes, sEnumInfo, std::size(sEnumInfo)); 141 } 142 143 SVGElement::StringAttributesInfo SVGFECompositeElement::GetStringInfo() { 144 return StringAttributesInfo(mStringAttributes, sStringInfo, 145 std::size(sStringInfo)); 146 } 147 148 } // namespace mozilla::dom