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/. */
8 #include "SVGFilterInstance.h"
10 // Keep others in (case-insensitive) order:
11 #include "gfxPlatform.h"
13 #include "mozilla/ISVGDisplayableFrame.h"
14 #include "mozilla/SVGContentUtils.h"
15 #include "mozilla/SVGObserverUtils.h"
16 #include "mozilla/SVGUtils.h"
17 #include "mozilla/dom/HTMLCanvasElement.h"
18 #include "mozilla/dom/SVGLengthBinding.h"
19 #include "mozilla/dom/SVGUnitTypesBinding.h"
20 #include "mozilla/dom/SVGFilterElement.h"
21 #include "SVGFilterFrame.h"
22 #include "FilterSupport.h"
23 #include "gfx2DGlue.h"
25 using namespace mozilla::dom
;
26 using namespace mozilla::dom::SVGUnitTypes_Binding
;
27 using namespace mozilla::gfx
;
31 SVGFilterInstance::SVGFilterInstance(
32 const StyleFilter
& aFilter
, SVGFilterFrame
* aFilterFrame
,
33 nsIContent
* aTargetContent
, const UserSpaceMetrics
& aMetrics
,
34 const gfxRect
& aTargetBBox
,
35 const MatrixScalesDouble
& aUserSpaceToFilterSpaceScale
)
37 mTargetContent(aTargetContent
),
39 mFilterFrame(aFilterFrame
),
40 mTargetBBox(aTargetBBox
),
41 mUserSpaceToFilterSpaceScale(aUserSpaceToFilterSpaceScale
),
42 mSourceAlphaAvailable(false),
44 // Get the filter element.
45 mFilterElement
= mFilterFrame
->GetFilterContent();
46 if (!mFilterElement
) {
47 MOZ_ASSERT_UNREACHABLE("filter frame should have a related element");
52 mFilterFrame
->GetEnumValue(SVGFilterElement::PRIMITIVEUNITS
);
54 if (!ComputeBounds()) {
61 bool SVGFilterInstance::ComputeBounds() {
62 // XXX if filterUnits is set (or has defaulted) to objectBoundingBox, we
63 // should send a warning to the error console if the author has used lengths
64 // with units. This is a common mistake and can result in the filter region
65 // being *massive* below (because we ignore the units and interpret the number
66 // as a factor of the bbox width/height). We should also send a warning if the
67 // user uses a number without units (a future SVG spec should really
68 // deprecate that, since it's too confusing for a bare number to be sometimes
69 // interpreted as a fraction of the bounding box and sometimes as user-space
70 // units). So really only percentage values should be used in this case.
72 // Set the user space bounds (i.e. the filter region in user space).
73 SVGAnimatedLength XYWH
[4];
74 static_assert(sizeof(mFilterElement
->mLengthAttributes
) == sizeof(XYWH
),
75 "XYWH size incorrect");
76 memcpy(XYWH
, mFilterElement
->mLengthAttributes
,
77 sizeof(mFilterElement
->mLengthAttributes
));
78 XYWH
[0] = *mFilterFrame
->GetLengthValue(SVGFilterElement::ATTR_X
);
79 XYWH
[1] = *mFilterFrame
->GetLengthValue(SVGFilterElement::ATTR_Y
);
80 XYWH
[2] = *mFilterFrame
->GetLengthValue(SVGFilterElement::ATTR_WIDTH
);
81 XYWH
[3] = *mFilterFrame
->GetLengthValue(SVGFilterElement::ATTR_HEIGHT
);
82 uint16_t filterUnits
=
83 mFilterFrame
->GetEnumValue(SVGFilterElement::FILTERUNITS
);
84 gfxRect userSpaceBounds
=
85 SVGUtils::GetRelativeRect(filterUnits
, XYWH
, mTargetBBox
, mMetrics
);
87 // Transform the user space bounds to filter space, so we
88 // can align them with the pixel boundaries of the offscreen surface.
89 // The offscreen surface has the same scale as filter space.
90 gfxRect filterSpaceBounds
= UserSpaceToFilterSpace(userSpaceBounds
);
91 filterSpaceBounds
.RoundOut();
92 if (filterSpaceBounds
.width
<= 0 || filterSpaceBounds
.height
<= 0) {
93 // 0 disables rendering, < 0 is error. dispatch error console warning
94 // or error as appropriate.
98 // Set the filter space bounds.
99 if (!gfxUtils::GfxRectToIntRect(filterSpaceBounds
, &mFilterSpaceBounds
)) {
100 // The filter region is way too big if there is float -> int overflow.
107 float SVGFilterInstance::GetPrimitiveNumber(uint8_t aCtxType
,
108 float aValue
) const {
109 SVGAnimatedLength val
;
110 val
.Init(aCtxType
, 0xff, aValue
, SVGLength_Binding::SVG_LENGTHTYPE_NUMBER
);
113 if (mPrimitiveUnits
== SVG_UNIT_TYPE_OBJECTBOUNDINGBOX
) {
114 value
= SVGUtils::ObjectSpace(mTargetBBox
, &val
);
116 value
= SVGUtils::UserSpace(mMetrics
, &val
);
120 case SVGContentUtils::X
:
121 return value
* static_cast<float>(mUserSpaceToFilterSpaceScale
.xScale
);
122 case SVGContentUtils::Y
:
123 return value
* static_cast<float>(mUserSpaceToFilterSpaceScale
.yScale
);
124 case SVGContentUtils::XY
:
126 return value
* SVGContentUtils::ComputeNormalizedHypotenuse(
127 mUserSpaceToFilterSpaceScale
.xScale
,
128 mUserSpaceToFilterSpaceScale
.yScale
);
132 Point3D
SVGFilterInstance::ConvertLocation(const Point3D
& aPoint
) const {
133 SVGAnimatedLength val
[4];
134 val
[0].Init(SVGContentUtils::X
, 0xff, aPoint
.x
,
135 SVGLength_Binding::SVG_LENGTHTYPE_NUMBER
);
136 val
[1].Init(SVGContentUtils::Y
, 0xff, aPoint
.y
,
137 SVGLength_Binding::SVG_LENGTHTYPE_NUMBER
);
138 // Dummy width/height values
139 val
[2].Init(SVGContentUtils::X
, 0xff, 0,
140 SVGLength_Binding::SVG_LENGTHTYPE_NUMBER
);
141 val
[3].Init(SVGContentUtils::Y
, 0xff, 0,
142 SVGLength_Binding::SVG_LENGTHTYPE_NUMBER
);
145 SVGUtils::GetRelativeRect(mPrimitiveUnits
, val
, mTargetBBox
, mMetrics
);
146 gfxRect r
= UserSpaceToFilterSpace(feArea
);
147 return Point3D(r
.x
, r
.y
, GetPrimitiveNumber(SVGContentUtils::XY
, aPoint
.z
));
150 gfxRect
SVGFilterInstance::UserSpaceToFilterSpace(
151 const gfxRect
& aUserSpaceRect
) const {
152 gfxRect filterSpaceRect
= aUserSpaceRect
;
153 filterSpaceRect
.Scale(mUserSpaceToFilterSpaceScale
);
154 return filterSpaceRect
;
157 IntRect
SVGFilterInstance::ComputeFilterPrimitiveSubregion(
158 SVGFilterPrimitiveElement
* aFilterElement
,
159 const nsTArray
<FilterPrimitiveDescription
>& aPrimitiveDescrs
,
160 const nsTArray
<int32_t>& aInputIndices
) {
161 SVGFilterPrimitiveElement
* fE
= aFilterElement
;
163 IntRect
defaultFilterSubregion(0, 0, 0, 0);
164 if (fE
->SubregionIsUnionOfRegions()) {
165 for (const auto& inputIndex
: aInputIndices
) {
166 bool isStandardInput
=
167 inputIndex
< 0 || inputIndex
== mSourceGraphicIndex
;
168 IntRect inputSubregion
=
169 isStandardInput
? mFilterSpaceBounds
170 : aPrimitiveDescrs
[inputIndex
].PrimitiveSubregion();
172 defaultFilterSubregion
= defaultFilterSubregion
.Union(inputSubregion
);
175 defaultFilterSubregion
= mFilterSpaceBounds
;
178 gfxRect feArea
= SVGUtils::GetRelativeRect(
180 &fE
->mLengthAttributes
[SVGFilterPrimitiveElement::ATTR_X
], mTargetBBox
,
182 Rect region
= ToRect(UserSpaceToFilterSpace(feArea
));
184 if (!fE
->mLengthAttributes
[SVGFilterPrimitiveElement::ATTR_X
]
186 region
.x
= defaultFilterSubregion
.X();
187 if (!fE
->mLengthAttributes
[SVGFilterPrimitiveElement::ATTR_Y
]
189 region
.y
= defaultFilterSubregion
.Y();
190 if (!fE
->mLengthAttributes
[SVGFilterPrimitiveElement::ATTR_WIDTH
]
192 region
.width
= defaultFilterSubregion
.Width();
193 if (!fE
->mLengthAttributes
[SVGFilterPrimitiveElement::ATTR_HEIGHT
]
195 region
.height
= defaultFilterSubregion
.Height();
197 // We currently require filter primitive subregions to be pixel-aligned.
198 // Following the spec, any pixel partially in the region is included
201 return RoundedToInt(region
);
204 void SVGFilterInstance::GetInputsAreTainted(
205 const nsTArray
<FilterPrimitiveDescription
>& aPrimitiveDescrs
,
206 const nsTArray
<int32_t>& aInputIndices
, bool aFilterInputIsTainted
,
207 nsTArray
<bool>& aOutInputsAreTainted
) {
208 for (const auto& inputIndex
: aInputIndices
) {
209 if (inputIndex
< 0) {
210 aOutInputsAreTainted
.AppendElement(aFilterInputIsTainted
);
212 aOutInputsAreTainted
.AppendElement(
213 aPrimitiveDescrs
[inputIndex
].IsTainted());
218 static int32_t GetLastResultIndex(
219 const nsTArray
<FilterPrimitiveDescription
>& aPrimitiveDescrs
) {
220 uint32_t numPrimitiveDescrs
= aPrimitiveDescrs
.Length();
221 return !numPrimitiveDescrs
222 ? FilterPrimitiveDescription::kPrimitiveIndexSourceGraphic
223 : numPrimitiveDescrs
- 1;
226 int32_t SVGFilterInstance::GetOrCreateSourceAlphaIndex(
227 nsTArray
<FilterPrimitiveDescription
>& aPrimitiveDescrs
) {
228 // If the SourceAlpha index has already been determined or created for this
229 // SVG filter, just return it.
230 if (mSourceAlphaAvailable
) {
231 return mSourceAlphaIndex
;
234 // If this is the first filter in the chain, we can just use the
235 // kPrimitiveIndexSourceAlpha keyword to refer to the SourceAlpha of the
237 if (mSourceGraphicIndex
< 0) {
238 mSourceAlphaIndex
= FilterPrimitiveDescription::kPrimitiveIndexSourceAlpha
;
239 mSourceAlphaAvailable
= true;
240 return mSourceAlphaIndex
;
243 // Otherwise, create a primitive description to turn the previous filter's
244 // output into a SourceAlpha input.
245 FilterPrimitiveDescription
descr(AsVariant(ToAlphaAttributes()));
246 descr
.SetInputPrimitive(0, mSourceGraphicIndex
);
248 const FilterPrimitiveDescription
& sourcePrimitiveDescr
=
249 aPrimitiveDescrs
[mSourceGraphicIndex
];
250 descr
.SetPrimitiveSubregion(sourcePrimitiveDescr
.PrimitiveSubregion());
251 descr
.SetIsTainted(sourcePrimitiveDescr
.IsTainted());
253 ColorSpace colorSpace
= sourcePrimitiveDescr
.OutputColorSpace();
254 descr
.SetInputColorSpace(0, colorSpace
);
255 descr
.SetOutputColorSpace(colorSpace
);
257 aPrimitiveDescrs
.AppendElement(std::move(descr
));
258 mSourceAlphaIndex
= aPrimitiveDescrs
.Length() - 1;
259 mSourceAlphaAvailable
= true;
260 return mSourceAlphaIndex
;
263 nsresult
SVGFilterInstance::GetSourceIndices(
264 SVGFilterPrimitiveElement
* aPrimitiveElement
,
265 nsTArray
<FilterPrimitiveDescription
>& aPrimitiveDescrs
,
266 const nsTHashMap
<nsStringHashKey
, int32_t>& aImageTable
,
267 nsTArray
<int32_t>& aSourceIndices
) {
268 AutoTArray
<SVGStringInfo
, 2> sources
;
269 aPrimitiveElement
->GetSourceImageNames(sources
);
271 for (const auto& source
: sources
) {
273 source
.mString
->GetAnimValue(str
, source
.mElement
);
275 int32_t sourceIndex
= 0;
276 if (str
.EqualsLiteral("SourceGraphic")) {
277 sourceIndex
= mSourceGraphicIndex
;
278 } else if (str
.EqualsLiteral("SourceAlpha")) {
279 sourceIndex
= GetOrCreateSourceAlphaIndex(aPrimitiveDescrs
);
280 } else if (str
.EqualsLiteral("FillPaint")) {
281 sourceIndex
= FilterPrimitiveDescription::kPrimitiveIndexFillPaint
;
282 } else if (str
.EqualsLiteral("StrokePaint")) {
283 sourceIndex
= FilterPrimitiveDescription::kPrimitiveIndexStrokePaint
;
284 } else if (str
.EqualsLiteral("BackgroundImage") ||
285 str
.EqualsLiteral("BackgroundAlpha")) {
286 return NS_ERROR_NOT_IMPLEMENTED
;
287 } else if (str
.EqualsLiteral("")) {
288 sourceIndex
= GetLastResultIndex(aPrimitiveDescrs
);
290 bool inputExists
= aImageTable
.Get(str
, &sourceIndex
);
292 sourceIndex
= GetLastResultIndex(aPrimitiveDescrs
);
296 aSourceIndices
.AppendElement(sourceIndex
);
301 nsresult
SVGFilterInstance::BuildPrimitives(
302 nsTArray
<FilterPrimitiveDescription
>& aPrimitiveDescrs
,
303 nsTArray
<RefPtr
<SourceSurface
>>& aInputImages
, bool aInputIsTainted
) {
304 mSourceGraphicIndex
= GetLastResultIndex(aPrimitiveDescrs
);
306 // Clip previous filter's output to this filter's filter region.
307 if (mSourceGraphicIndex
>= 0) {
308 FilterPrimitiveDescription
& sourceDescr
=
309 aPrimitiveDescrs
[mSourceGraphicIndex
];
310 sourceDescr
.SetPrimitiveSubregion(
311 sourceDescr
.PrimitiveSubregion().Intersect(mFilterSpaceBounds
));
314 // Get the filter primitive elements.
315 AutoTArray
<RefPtr
<SVGFilterPrimitiveElement
>, 8> primitives
;
316 for (nsIContent
* child
= mFilterElement
->nsINode::GetFirstChild(); child
;
317 child
= child
->GetNextSibling()) {
318 if (auto* primitive
= SVGFilterPrimitiveElement::FromNode(child
)) {
319 primitives
.AppendElement(primitive
);
323 // Maps source image name to source index.
324 nsTHashMap
<nsStringHashKey
, int32_t> imageTable(8);
326 // The principal that we check principals of any loaded images against.
327 nsCOMPtr
<nsIPrincipal
> principal
= mTargetContent
->NodePrincipal();
329 for (uint32_t primitiveElementIndex
= 0;
330 primitiveElementIndex
< primitives
.Length(); ++primitiveElementIndex
) {
331 SVGFilterPrimitiveElement
* filter
= primitives
[primitiveElementIndex
];
333 AutoTArray
<int32_t, 2> sourceIndices
;
335 GetSourceIndices(filter
, aPrimitiveDescrs
, imageTable
, sourceIndices
);
340 IntRect primitiveSubregion
= ComputeFilterPrimitiveSubregion(
341 filter
, aPrimitiveDescrs
, sourceIndices
);
343 AutoTArray
<bool, 8> sourcesAreTainted
;
344 GetInputsAreTainted(aPrimitiveDescrs
, sourceIndices
, aInputIsTainted
,
347 FilterPrimitiveDescription descr
= filter
->GetPrimitiveDescription(
348 this, primitiveSubregion
, sourcesAreTainted
, aInputImages
);
350 descr
.SetIsTainted(filter
->OutputIsTainted(sourcesAreTainted
, principal
));
351 descr
.SetFilterSpaceBounds(mFilterSpaceBounds
);
352 descr
.SetPrimitiveSubregion(
353 primitiveSubregion
.Intersect(descr
.FilterSpaceBounds()));
355 for (uint32_t i
= 0; i
< sourceIndices
.Length(); i
++) {
356 int32_t inputIndex
= sourceIndices
[i
];
357 descr
.SetInputPrimitive(i
, inputIndex
);
359 ColorSpace inputColorSpace
=
360 inputIndex
>= 0 ? aPrimitiveDescrs
[inputIndex
].OutputColorSpace()
361 : ColorSpace(ColorSpace::SRGB
);
363 ColorSpace desiredInputColorSpace
=
364 filter
->GetInputColorSpace(i
, inputColorSpace
);
365 descr
.SetInputColorSpace(i
, desiredInputColorSpace
);
367 // the output color space is whatever in1 is if there is an in1
368 descr
.SetOutputColorSpace(desiredInputColorSpace
);
372 if (sourceIndices
.Length() == 0) {
373 descr
.SetOutputColorSpace(filter
->GetOutputColorSpace());
376 aPrimitiveDescrs
.AppendElement(std::move(descr
));
377 uint32_t primitiveDescrIndex
= aPrimitiveDescrs
.Length() - 1;
380 filter
->GetResultImageName().GetAnimValue(str
, filter
);
381 imageTable
.InsertOrUpdate(str
, primitiveDescrIndex
);
387 } // namespace mozilla