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/SVGFETurbulenceElement.h"
8 #include "mozilla/dom/SVGFETurbulenceElementBinding.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(FETurbulence
)
15 using namespace mozilla::gfx
;
17 namespace mozilla::dom
{
20 static const unsigned short SVG_STITCHTYPE_STITCH
= 1;
21 static const unsigned short SVG_STITCHTYPE_NOSTITCH
= 2;
23 static const int32_t MAX_OCTAVES
= 10;
25 JSObject
* SVGFETurbulenceElement::WrapNode(JSContext
* aCx
,
26 JS::Handle
<JSObject
*> aGivenProto
) {
27 return SVGFETurbulenceElement_Binding::Wrap(aCx
, this, aGivenProto
);
30 SVGElement::NumberInfo
SVGFETurbulenceElement::sNumberInfo
[1] = {
31 {nsGkAtoms::seed
, 0}};
33 SVGElement::NumberPairInfo
SVGFETurbulenceElement::sNumberPairInfo
[1] = {
34 {nsGkAtoms::baseFrequency
, 0, 0}};
36 SVGElement::IntegerInfo
SVGFETurbulenceElement::sIntegerInfo
[1] = {
37 {nsGkAtoms::numOctaves
, 1}};
39 SVGEnumMapping
SVGFETurbulenceElement::sTypeMap
[] = {
40 {nsGkAtoms::fractalNoise
, SVG_TURBULENCE_TYPE_FRACTALNOISE
},
41 {nsGkAtoms::turbulence
, SVG_TURBULENCE_TYPE_TURBULENCE
},
44 SVGEnumMapping
SVGFETurbulenceElement::sStitchTilesMap
[] = {
45 {nsGkAtoms::stitch
, SVG_STITCHTYPE_STITCH
},
46 {nsGkAtoms::noStitch
, SVG_STITCHTYPE_NOSTITCH
},
49 SVGElement::EnumInfo
SVGFETurbulenceElement::sEnumInfo
[2] = {
50 {nsGkAtoms::type
, sTypeMap
, SVG_TURBULENCE_TYPE_TURBULENCE
},
51 {nsGkAtoms::stitchTiles
, sStitchTilesMap
, SVG_STITCHTYPE_NOSTITCH
}};
53 SVGElement::StringInfo
SVGFETurbulenceElement::sStringInfo
[1] = {
54 {nsGkAtoms::result
, kNameSpaceID_None
, true}};
56 //----------------------------------------------------------------------
59 NS_IMPL_ELEMENT_CLONE_WITH_INIT(SVGFETurbulenceElement
)
61 //----------------------------------------------------------------------
63 already_AddRefed
<DOMSVGAnimatedNumber
>
64 SVGFETurbulenceElement::BaseFrequencyX() {
65 return mNumberPairAttributes
[BASE_FREQ
].ToDOMAnimatedNumber(
66 SVGAnimatedNumberPair::eFirst
, this);
69 already_AddRefed
<DOMSVGAnimatedNumber
>
70 SVGFETurbulenceElement::BaseFrequencyY() {
71 return mNumberPairAttributes
[BASE_FREQ
].ToDOMAnimatedNumber(
72 SVGAnimatedNumberPair::eSecond
, this);
75 already_AddRefed
<DOMSVGAnimatedInteger
> SVGFETurbulenceElement::NumOctaves() {
76 return mIntegerAttributes
[OCTAVES
].ToDOMAnimatedInteger(this);
79 already_AddRefed
<DOMSVGAnimatedNumber
> SVGFETurbulenceElement::Seed() {
80 return mNumberAttributes
[SEED
].ToDOMAnimatedNumber(this);
83 already_AddRefed
<DOMSVGAnimatedEnumeration
>
84 SVGFETurbulenceElement::StitchTiles() {
85 return mEnumAttributes
[STITCHTILES
].ToDOMAnimatedEnum(this);
88 already_AddRefed
<DOMSVGAnimatedEnumeration
> SVGFETurbulenceElement::Type() {
89 return mEnumAttributes
[TYPE
].ToDOMAnimatedEnum(this);
92 FilterPrimitiveDescription
SVGFETurbulenceElement::GetPrimitiveDescription(
93 SVGFilterInstance
* aInstance
, const IntRect
& aFilterSubregion
,
94 const nsTArray
<bool>& aInputsAreTainted
,
95 nsTArray
<RefPtr
<SourceSurface
>>& aInputImages
) {
96 float fX
= mNumberPairAttributes
[BASE_FREQ
].GetAnimValue(
97 SVGAnimatedNumberPair::eFirst
);
98 float fY
= mNumberPairAttributes
[BASE_FREQ
].GetAnimValue(
99 SVGAnimatedNumberPair::eSecond
);
100 float seed
= mNumberAttributes
[OCTAVES
].GetAnimValue();
102 std::clamp(mIntegerAttributes
[OCTAVES
].GetAnimValue(), 0, MAX_OCTAVES
);
103 uint32_t type
= mEnumAttributes
[TYPE
].GetAnimValue();
104 uint16_t stitch
= mEnumAttributes
[STITCHTILES
].GetAnimValue();
106 if (fX
== 0 && fY
== 0) {
107 // A base frequency of zero results in transparent black for
108 // type="turbulence" and in 50% alpha 50% gray for type="fractalNoise".
109 if (type
== SVG_TURBULENCE_TYPE_TURBULENCE
) {
110 return FilterPrimitiveDescription();
112 FloodAttributes atts
;
113 atts
.mColor
= sRGBColor(0.5, 0.5, 0.5, 0.5);
114 return FilterPrimitiveDescription(AsVariant(std::move(atts
)));
117 // We interpret the base frequency as relative to user space units. In other
118 // words, we consider one turbulence base period to be 1 / fX user space
119 // units wide and 1 / fY user space units high. We do not scale the frequency
120 // depending on the filter primitive region.
121 // We now convert the frequency from user space to filter space.
122 // If a frequency in user space units is zero, then it will also be zero in
123 // filter space. During the conversion we use a dummy period length of 1
124 // for those frequencies but then ignore the converted length and use 0
125 // for the converted frequency. This avoids division by zero.
126 gfxRect
firstPeriodInUserSpace(0, 0, fX
== 0 ? 1 : (1 / fX
),
127 fY
== 0 ? 1 : (1 / fY
));
128 gfxRect firstPeriodInFilterSpace
=
129 aInstance
->UserSpaceToFilterSpace(firstPeriodInUserSpace
);
130 Size
frequencyInFilterSpace(
131 fX
== 0 ? 0 : (1 / firstPeriodInFilterSpace
.width
),
132 fY
== 0 ? 0 : (1 / firstPeriodInFilterSpace
.height
));
133 gfxPoint offset
= firstPeriodInFilterSpace
.TopLeft();
135 TurbulenceAttributes atts
;
136 atts
.mOffset
= IntPoint::Truncate(offset
.x
, offset
.y
);
137 atts
.mBaseFrequency
= frequencyInFilterSpace
;
139 atts
.mOctaves
= octaves
;
140 atts
.mStitchable
= stitch
== SVG_STITCHTYPE_STITCH
;
142 return FilterPrimitiveDescription(AsVariant(std::move(atts
)));
145 bool SVGFETurbulenceElement::AttributeAffectsRendering(
146 int32_t aNameSpaceID
, nsAtom
* aAttribute
) const {
147 return SVGFETurbulenceElementBase::AttributeAffectsRendering(aNameSpaceID
,
149 (aNameSpaceID
== kNameSpaceID_None
&&
150 (aAttribute
== nsGkAtoms::seed
||
151 aAttribute
== nsGkAtoms::baseFrequency
||
152 aAttribute
== nsGkAtoms::numOctaves
||
153 aAttribute
== nsGkAtoms::type
||
154 aAttribute
== nsGkAtoms::stitchTiles
));
157 nsresult
SVGFETurbulenceElement::BindToTree(BindContext
& aCtx
,
159 if (aCtx
.InComposedDoc()) {
160 aCtx
.OwnerDoc().SetUseCounter(eUseCounter_custom_feTurbulence
);
163 return SVGFETurbulenceElementBase::BindToTree(aCtx
, aParent
);
166 //----------------------------------------------------------------------
167 // SVGElement methods
169 SVGElement::NumberAttributesInfo
SVGFETurbulenceElement::GetNumberInfo() {
170 return NumberAttributesInfo(mNumberAttributes
, sNumberInfo
,
171 std::size(sNumberInfo
));
174 SVGElement::NumberPairAttributesInfo
175 SVGFETurbulenceElement::GetNumberPairInfo() {
176 return NumberPairAttributesInfo(mNumberPairAttributes
, sNumberPairInfo
,
177 std::size(sNumberPairInfo
));
180 SVGElement::IntegerAttributesInfo
SVGFETurbulenceElement::GetIntegerInfo() {
181 return IntegerAttributesInfo(mIntegerAttributes
, sIntegerInfo
,
182 std::size(sIntegerInfo
));
185 SVGElement::EnumAttributesInfo
SVGFETurbulenceElement::GetEnumInfo() {
186 return EnumAttributesInfo(mEnumAttributes
, sEnumInfo
, std::size(sEnumInfo
));
189 SVGElement::StringAttributesInfo
SVGFETurbulenceElement::GetStringInfo() {
190 return StringAttributesInfo(mStringAttributes
, sStringInfo
,
191 std::size(sStringInfo
));
194 } // namespace mozilla::dom