Bug 1874684 - Part 4: Prefer const references instead of copying Instant values....
[gecko.git] / dom / svg / SVGFEDisplacementMapElement.cpp
blobc80d0d20e3ec5cabc186b058bfc5b2bbd39096a1
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/. */
7 #include "mozilla/dom/SVGFEDisplacementMapElement.h"
8 #include "mozilla/dom/SVGFEDisplacementMapElementBinding.h"
9 #include "mozilla/SVGFilterInstance.h"
10 #include "mozilla/dom/Document.h"
11 #include "mozilla/dom/BindContext.h"
13 NS_IMPL_NS_NEW_SVG_ELEMENT(FEDisplacementMap)
15 using namespace mozilla::gfx;
17 namespace mozilla::dom {
19 JSObject* SVGFEDisplacementMapElement::WrapNode(
20 JSContext* aCx, JS::Handle<JSObject*> aGivenProto) {
21 return SVGFEDisplacementMapElement_Binding::Wrap(aCx, this, aGivenProto);
24 SVGElement::NumberInfo SVGFEDisplacementMapElement::sNumberInfo[1] = {
25 {nsGkAtoms::scale, 0},
28 SVGEnumMapping SVGFEDisplacementMapElement::sChannelMap[] = {
29 {nsGkAtoms::R, SVG_CHANNEL_R},
30 {nsGkAtoms::G, SVG_CHANNEL_G},
31 {nsGkAtoms::B, SVG_CHANNEL_B},
32 {nsGkAtoms::A, SVG_CHANNEL_A},
33 {nullptr, 0}};
35 SVGElement::EnumInfo SVGFEDisplacementMapElement::sEnumInfo[2] = {
36 {nsGkAtoms::xChannelSelector, sChannelMap, SVG_CHANNEL_A},
37 {nsGkAtoms::yChannelSelector, sChannelMap, SVG_CHANNEL_A}};
39 SVGElement::StringInfo SVGFEDisplacementMapElement::sStringInfo[3] = {
40 {nsGkAtoms::result, kNameSpaceID_None, true},
41 {nsGkAtoms::in, kNameSpaceID_None, true},
42 {nsGkAtoms::in2, kNameSpaceID_None, true}};
44 //----------------------------------------------------------------------
45 // nsINode methods
47 NS_IMPL_ELEMENT_CLONE_WITH_INIT(SVGFEDisplacementMapElement)
49 //----------------------------------------------------------------------
51 already_AddRefed<DOMSVGAnimatedString> SVGFEDisplacementMapElement::In1() {
52 return mStringAttributes[IN1].ToDOMAnimatedString(this);
55 already_AddRefed<DOMSVGAnimatedString> SVGFEDisplacementMapElement::In2() {
56 return mStringAttributes[IN2].ToDOMAnimatedString(this);
59 already_AddRefed<DOMSVGAnimatedNumber> SVGFEDisplacementMapElement::Scale() {
60 return mNumberAttributes[SCALE].ToDOMAnimatedNumber(this);
63 already_AddRefed<DOMSVGAnimatedEnumeration>
64 SVGFEDisplacementMapElement::XChannelSelector() {
65 return mEnumAttributes[CHANNEL_X].ToDOMAnimatedEnum(this);
68 already_AddRefed<DOMSVGAnimatedEnumeration>
69 SVGFEDisplacementMapElement::YChannelSelector() {
70 return mEnumAttributes[CHANNEL_Y].ToDOMAnimatedEnum(this);
73 FilterPrimitiveDescription SVGFEDisplacementMapElement::GetPrimitiveDescription(
74 SVGFilterInstance* aInstance, const IntRect& aFilterSubregion,
75 const nsTArray<bool>& aInputsAreTainted,
76 nsTArray<RefPtr<SourceSurface>>& aInputImages) {
77 if (aInputsAreTainted[1]) {
78 // If the map is tainted, refuse to apply the effect and act as a
79 // pass-through filter instead, as required by the spec.
80 OffsetAttributes atts;
81 atts.mValue = IntPoint(0, 0);
82 return FilterPrimitiveDescription(AsVariant(std::move(atts)));
85 float scale = aInstance->GetPrimitiveNumber(SVGContentUtils::XY,
86 &mNumberAttributes[SCALE]);
87 uint32_t xChannel = mEnumAttributes[CHANNEL_X].GetAnimValue();
88 uint32_t yChannel = mEnumAttributes[CHANNEL_Y].GetAnimValue();
89 DisplacementMapAttributes atts;
90 atts.mScale = scale;
91 atts.mXChannel = xChannel;
92 atts.mYChannel = yChannel;
93 return FilterPrimitiveDescription(AsVariant(std::move(atts)));
96 bool SVGFEDisplacementMapElement::AttributeAffectsRendering(
97 int32_t aNameSpaceID, nsAtom* aAttribute) const {
98 return SVGFEDisplacementMapElementBase::AttributeAffectsRendering(
99 aNameSpaceID, aAttribute) ||
100 (aNameSpaceID == kNameSpaceID_None &&
101 (aAttribute == nsGkAtoms::in || aAttribute == nsGkAtoms::in2 ||
102 aAttribute == nsGkAtoms::scale ||
103 aAttribute == nsGkAtoms::xChannelSelector ||
104 aAttribute == nsGkAtoms::yChannelSelector));
107 void SVGFEDisplacementMapElement::GetSourceImageNames(
108 nsTArray<SVGStringInfo>& aSources) {
109 aSources.AppendElement(SVGStringInfo(&mStringAttributes[IN1], this));
110 aSources.AppendElement(SVGStringInfo(&mStringAttributes[IN2], this));
113 nsresult SVGFEDisplacementMapElement::BindToTree(BindContext& aCtx,
114 nsINode& aParent) {
115 if (aCtx.InComposedDoc()) {
116 aCtx.OwnerDoc().SetUseCounter(eUseCounter_custom_feDisplacementMap);
119 return SVGFEDisplacementMapElementBase::BindToTree(aCtx, aParent);
122 //----------------------------------------------------------------------
123 // SVGElement methods
125 SVGElement::NumberAttributesInfo SVGFEDisplacementMapElement::GetNumberInfo() {
126 return NumberAttributesInfo(mNumberAttributes, sNumberInfo,
127 ArrayLength(sNumberInfo));
130 SVGElement::EnumAttributesInfo SVGFEDisplacementMapElement::GetEnumInfo() {
131 return EnumAttributesInfo(mEnumAttributes, sEnumInfo, ArrayLength(sEnumInfo));
134 SVGElement::StringAttributesInfo SVGFEDisplacementMapElement::GetStringInfo() {
135 return StringAttributesInfo(mStringAttributes, sStringInfo,
136 ArrayLength(sStringInfo));
139 } // namespace mozilla::dom