Bug 1705289 Remove spurious mozMediaSource media track constraint from tests r=jib
[gecko.git] / layout / svg / SVGFilterInstance.cpp
blob5a61d04fd0fb8c1b8c9335ed7130863dd9aaa506
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 // Main header first:
8 #include "SVGFilterInstance.h"
10 // Keep others in (case-insensitive) order:
11 #include "gfxPlatform.h"
12 #include "gfxUtils.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/IDTracker.h"
19 #include "mozilla/dom/SVGLengthBinding.h"
20 #include "mozilla/dom/SVGUnitTypesBinding.h"
21 #include "mozilla/dom/SVGFilterElement.h"
22 #include "SVGFilterFrame.h"
23 #include "FilterSupport.h"
24 #include "gfx2DGlue.h"
26 using namespace mozilla::dom;
27 using namespace mozilla::dom::SVGUnitTypes_Binding;
28 using namespace mozilla::gfx;
30 namespace mozilla {
32 SVGFilterInstance::SVGFilterInstance(
33 const StyleFilter& aFilter, nsIFrame* aTargetFrame,
34 nsIContent* aTargetContent, const UserSpaceMetrics& aMetrics,
35 const gfxRect& aTargetBBox, const gfxSize& aUserSpaceToFilterSpaceScale)
36 : mFilter(aFilter),
37 mTargetContent(aTargetContent),
38 mMetrics(aMetrics),
39 mTargetBBox(aTargetBBox),
40 mUserSpaceToFilterSpaceScale(aUserSpaceToFilterSpaceScale),
41 mSourceAlphaAvailable(false),
42 mInitialized(false) {
43 // Get the filter frame.
44 mFilterFrame = GetFilterFrame(aTargetFrame);
45 if (!mFilterFrame) {
46 return;
49 // Get the filter element.
50 mFilterElement = mFilterFrame->GetFilterContent();
51 if (!mFilterElement) {
52 MOZ_ASSERT_UNREACHABLE("filter frame should have a related element");
53 return;
56 mPrimitiveUnits =
57 mFilterFrame->GetEnumValue(SVGFilterElement::PRIMITIVEUNITS);
59 if (!ComputeBounds()) {
60 return;
63 mInitialized = true;
66 bool SVGFilterInstance::ComputeBounds() {
67 // XXX if filterUnits is set (or has defaulted) to objectBoundingBox, we
68 // should send a warning to the error console if the author has used lengths
69 // with units. This is a common mistake and can result in the filter region
70 // being *massive* below (because we ignore the units and interpret the number
71 // as a factor of the bbox width/height). We should also send a warning if the
72 // user uses a number without units (a future SVG spec should really
73 // deprecate that, since it's too confusing for a bare number to be sometimes
74 // interpreted as a fraction of the bounding box and sometimes as user-space
75 // units). So really only percentage values should be used in this case.
77 // Set the user space bounds (i.e. the filter region in user space).
78 SVGAnimatedLength XYWH[4];
79 static_assert(sizeof(mFilterElement->mLengthAttributes) == sizeof(XYWH),
80 "XYWH size incorrect");
81 memcpy(XYWH, mFilterElement->mLengthAttributes,
82 sizeof(mFilterElement->mLengthAttributes));
83 XYWH[0] = *mFilterFrame->GetLengthValue(SVGFilterElement::ATTR_X);
84 XYWH[1] = *mFilterFrame->GetLengthValue(SVGFilterElement::ATTR_Y);
85 XYWH[2] = *mFilterFrame->GetLengthValue(SVGFilterElement::ATTR_WIDTH);
86 XYWH[3] = *mFilterFrame->GetLengthValue(SVGFilterElement::ATTR_HEIGHT);
87 uint16_t filterUnits =
88 mFilterFrame->GetEnumValue(SVGFilterElement::FILTERUNITS);
89 gfxRect userSpaceBounds =
90 SVGUtils::GetRelativeRect(filterUnits, XYWH, mTargetBBox, mMetrics);
92 // Transform the user space bounds to filter space, so we
93 // can align them with the pixel boundaries of the offscreen surface.
94 // The offscreen surface has the same scale as filter space.
95 gfxRect filterSpaceBounds = UserSpaceToFilterSpace(userSpaceBounds);
96 filterSpaceBounds.RoundOut();
97 if (filterSpaceBounds.width <= 0 || filterSpaceBounds.height <= 0) {
98 // 0 disables rendering, < 0 is error. dispatch error console warning
99 // or error as appropriate.
100 return false;
103 // Set the filter space bounds.
104 if (!gfxUtils::GfxRectToIntRect(filterSpaceBounds, &mFilterSpaceBounds)) {
105 // The filter region is way too big if there is float -> int overflow.
106 return false;
109 return true;
112 SVGFilterFrame* SVGFilterInstance::GetFilterFrame(nsIFrame* aTargetFrame) {
113 if (!mFilter.IsUrl()) {
114 // The filter is not an SVG reference filter.
115 return nullptr;
118 // Get the target element to use as a point of reference for looking up the
119 // filter element.
120 if (!mTargetContent) {
121 return nullptr;
124 // aTargetFrame can be null if this filter belongs to a
125 // CanvasRenderingContext2D.
126 nsCOMPtr<nsIURI> url;
127 if (aTargetFrame) {
128 RefPtr<URLAndReferrerInfo> urlExtraReferrer =
129 SVGObserverUtils::GetFilterURI(aTargetFrame, mFilter);
131 // urlExtraReferrer might be null when mFilter has an invalid url
132 if (!urlExtraReferrer) {
133 return nullptr;
136 url = urlExtraReferrer->GetURI();
137 } else {
138 url = mFilter.AsUrl().ResolveLocalRef(mTargetContent);
141 if (!url) {
142 return nullptr;
145 // Look up the filter element by URL.
146 IDTracker idTracker;
147 bool watch = false;
148 idTracker.ResetToURIFragmentID(
149 mTargetContent, url, mFilter.AsUrl().ExtraData().ReferrerInfo(), watch);
150 Element* element = idTracker.get();
151 if (!element) {
152 // The URL points to no element.
153 return nullptr;
156 // Get the frame of the filter element.
157 nsIFrame* frame = element->GetPrimaryFrame();
158 if (!frame || !frame->IsSVGFilterFrame()) {
159 // The URL points to an element that's not an SVG filter element, or to an
160 // element that hasn't had its frame constructed yet.
161 return nullptr;
164 return static_cast<SVGFilterFrame*>(frame);
167 float SVGFilterInstance::GetPrimitiveNumber(uint8_t aCtxType,
168 float aValue) const {
169 SVGAnimatedLength val;
170 val.Init(aCtxType, 0xff, aValue, SVGLength_Binding::SVG_LENGTHTYPE_NUMBER);
172 float value;
173 if (mPrimitiveUnits == SVG_UNIT_TYPE_OBJECTBOUNDINGBOX) {
174 value = SVGUtils::ObjectSpace(mTargetBBox, &val);
175 } else {
176 value = SVGUtils::UserSpace(mMetrics, &val);
179 switch (aCtxType) {
180 case SVGContentUtils::X:
181 return value * mUserSpaceToFilterSpaceScale.width;
182 case SVGContentUtils::Y:
183 return value * mUserSpaceToFilterSpaceScale.height;
184 case SVGContentUtils::XY:
185 default:
186 return value * SVGContentUtils::ComputeNormalizedHypotenuse(
187 mUserSpaceToFilterSpaceScale.width,
188 mUserSpaceToFilterSpaceScale.height);
192 Point3D SVGFilterInstance::ConvertLocation(const Point3D& aPoint) const {
193 SVGAnimatedLength val[4];
194 val[0].Init(SVGContentUtils::X, 0xff, aPoint.x,
195 SVGLength_Binding::SVG_LENGTHTYPE_NUMBER);
196 val[1].Init(SVGContentUtils::Y, 0xff, aPoint.y,
197 SVGLength_Binding::SVG_LENGTHTYPE_NUMBER);
198 // Dummy width/height values
199 val[2].Init(SVGContentUtils::X, 0xff, 0,
200 SVGLength_Binding::SVG_LENGTHTYPE_NUMBER);
201 val[3].Init(SVGContentUtils::Y, 0xff, 0,
202 SVGLength_Binding::SVG_LENGTHTYPE_NUMBER);
204 gfxRect feArea =
205 SVGUtils::GetRelativeRect(mPrimitiveUnits, val, mTargetBBox, mMetrics);
206 gfxRect r = UserSpaceToFilterSpace(feArea);
207 return Point3D(r.x, r.y, GetPrimitiveNumber(SVGContentUtils::XY, aPoint.z));
210 gfxRect SVGFilterInstance::UserSpaceToFilterSpace(
211 const gfxRect& aUserSpaceRect) const {
212 gfxRect filterSpaceRect = aUserSpaceRect;
213 filterSpaceRect.Scale(mUserSpaceToFilterSpaceScale.width,
214 mUserSpaceToFilterSpaceScale.height);
215 return filterSpaceRect;
218 IntRect SVGFilterInstance::ComputeFilterPrimitiveSubregion(
219 SVGFE* aFilterElement,
220 const nsTArray<FilterPrimitiveDescription>& aPrimitiveDescrs,
221 const nsTArray<int32_t>& aInputIndices) {
222 SVGFE* fE = aFilterElement;
224 IntRect defaultFilterSubregion(0, 0, 0, 0);
225 if (fE->SubregionIsUnionOfRegions()) {
226 for (uint32_t i = 0; i < aInputIndices.Length(); ++i) {
227 int32_t inputIndex = aInputIndices[i];
228 bool isStandardInput =
229 inputIndex < 0 || inputIndex == mSourceGraphicIndex;
230 IntRect inputSubregion =
231 isStandardInput ? mFilterSpaceBounds
232 : aPrimitiveDescrs[inputIndex].PrimitiveSubregion();
234 defaultFilterSubregion = defaultFilterSubregion.Union(inputSubregion);
236 } else {
237 defaultFilterSubregion = mFilterSpaceBounds;
240 gfxRect feArea = SVGUtils::GetRelativeRect(
241 mPrimitiveUnits, &fE->mLengthAttributes[SVGFE::ATTR_X], mTargetBBox,
242 mMetrics);
243 Rect region = ToRect(UserSpaceToFilterSpace(feArea));
245 if (!fE->mLengthAttributes[SVGFE::ATTR_X].IsExplicitlySet())
246 region.x = defaultFilterSubregion.X();
247 if (!fE->mLengthAttributes[SVGFE::ATTR_Y].IsExplicitlySet())
248 region.y = defaultFilterSubregion.Y();
249 if (!fE->mLengthAttributes[SVGFE::ATTR_WIDTH].IsExplicitlySet())
250 region.width = defaultFilterSubregion.Width();
251 if (!fE->mLengthAttributes[SVGFE::ATTR_HEIGHT].IsExplicitlySet())
252 region.height = defaultFilterSubregion.Height();
254 // We currently require filter primitive subregions to be pixel-aligned.
255 // Following the spec, any pixel partially in the region is included
256 // in the region.
257 region.RoundOut();
258 return RoundedToInt(region);
261 void SVGFilterInstance::GetInputsAreTainted(
262 const nsTArray<FilterPrimitiveDescription>& aPrimitiveDescrs,
263 const nsTArray<int32_t>& aInputIndices, bool aFilterInputIsTainted,
264 nsTArray<bool>& aOutInputsAreTainted) {
265 for (uint32_t i = 0; i < aInputIndices.Length(); i++) {
266 int32_t inputIndex = aInputIndices[i];
267 if (inputIndex < 0) {
268 aOutInputsAreTainted.AppendElement(aFilterInputIsTainted);
269 } else {
270 aOutInputsAreTainted.AppendElement(
271 aPrimitiveDescrs[inputIndex].IsTainted());
276 static int32_t GetLastResultIndex(
277 const nsTArray<FilterPrimitiveDescription>& aPrimitiveDescrs) {
278 uint32_t numPrimitiveDescrs = aPrimitiveDescrs.Length();
279 return !numPrimitiveDescrs
280 ? FilterPrimitiveDescription::kPrimitiveIndexSourceGraphic
281 : numPrimitiveDescrs - 1;
284 int32_t SVGFilterInstance::GetOrCreateSourceAlphaIndex(
285 nsTArray<FilterPrimitiveDescription>& aPrimitiveDescrs) {
286 // If the SourceAlpha index has already been determined or created for this
287 // SVG filter, just return it.
288 if (mSourceAlphaAvailable) {
289 return mSourceAlphaIndex;
292 // If this is the first filter in the chain, we can just use the
293 // kPrimitiveIndexSourceAlpha keyword to refer to the SourceAlpha of the
294 // original image.
295 if (mSourceGraphicIndex < 0) {
296 mSourceAlphaIndex = FilterPrimitiveDescription::kPrimitiveIndexSourceAlpha;
297 mSourceAlphaAvailable = true;
298 return mSourceAlphaIndex;
301 // Otherwise, create a primitive description to turn the previous filter's
302 // output into a SourceAlpha input.
303 FilterPrimitiveDescription descr(AsVariant(ToAlphaAttributes()));
304 descr.SetInputPrimitive(0, mSourceGraphicIndex);
306 const FilterPrimitiveDescription& sourcePrimitiveDescr =
307 aPrimitiveDescrs[mSourceGraphicIndex];
308 descr.SetPrimitiveSubregion(sourcePrimitiveDescr.PrimitiveSubregion());
309 descr.SetIsTainted(sourcePrimitiveDescr.IsTainted());
311 ColorSpace colorSpace = sourcePrimitiveDescr.OutputColorSpace();
312 descr.SetInputColorSpace(0, colorSpace);
313 descr.SetOutputColorSpace(colorSpace);
315 aPrimitiveDescrs.AppendElement(std::move(descr));
316 mSourceAlphaIndex = aPrimitiveDescrs.Length() - 1;
317 mSourceAlphaAvailable = true;
318 return mSourceAlphaIndex;
321 nsresult SVGFilterInstance::GetSourceIndices(
322 SVGFE* aPrimitiveElement,
323 nsTArray<FilterPrimitiveDescription>& aPrimitiveDescrs,
324 const nsTHashMap<nsStringHashKey, int32_t>& aImageTable,
325 nsTArray<int32_t>& aSourceIndices) {
326 AutoTArray<SVGStringInfo, 2> sources;
327 aPrimitiveElement->GetSourceImageNames(sources);
329 for (uint32_t j = 0; j < sources.Length(); j++) {
330 nsAutoString str;
331 sources[j].mString->GetAnimValue(str, sources[j].mElement);
333 int32_t sourceIndex = 0;
334 if (str.EqualsLiteral("SourceGraphic")) {
335 sourceIndex = mSourceGraphicIndex;
336 } else if (str.EqualsLiteral("SourceAlpha")) {
337 sourceIndex = GetOrCreateSourceAlphaIndex(aPrimitiveDescrs);
338 } else if (str.EqualsLiteral("FillPaint")) {
339 sourceIndex = FilterPrimitiveDescription::kPrimitiveIndexFillPaint;
340 } else if (str.EqualsLiteral("StrokePaint")) {
341 sourceIndex = FilterPrimitiveDescription::kPrimitiveIndexStrokePaint;
342 } else if (str.EqualsLiteral("BackgroundImage") ||
343 str.EqualsLiteral("BackgroundAlpha")) {
344 return NS_ERROR_NOT_IMPLEMENTED;
345 } else if (str.EqualsLiteral("")) {
346 sourceIndex = GetLastResultIndex(aPrimitiveDescrs);
347 } else {
348 bool inputExists = aImageTable.Get(str, &sourceIndex);
349 if (!inputExists) {
350 sourceIndex = GetLastResultIndex(aPrimitiveDescrs);
354 aSourceIndices.AppendElement(sourceIndex);
356 return NS_OK;
359 nsresult SVGFilterInstance::BuildPrimitives(
360 nsTArray<FilterPrimitiveDescription>& aPrimitiveDescrs,
361 nsTArray<RefPtr<SourceSurface>>& aInputImages, bool aInputIsTainted) {
362 mSourceGraphicIndex = GetLastResultIndex(aPrimitiveDescrs);
364 // Clip previous filter's output to this filter's filter region.
365 if (mSourceGraphicIndex >= 0) {
366 FilterPrimitiveDescription& sourceDescr =
367 aPrimitiveDescrs[mSourceGraphicIndex];
368 sourceDescr.SetPrimitiveSubregion(
369 sourceDescr.PrimitiveSubregion().Intersect(mFilterSpaceBounds));
372 // Get the filter primitive elements.
373 nsTArray<RefPtr<SVGFE>> primitives;
374 for (nsIContent* child = mFilterElement->nsINode::GetFirstChild(); child;
375 child = child->GetNextSibling()) {
376 RefPtr<SVGFE> primitive;
377 CallQueryInterface(child, (SVGFE**)getter_AddRefs(primitive));
378 if (primitive) {
379 primitives.AppendElement(primitive);
383 // Maps source image name to source index.
384 nsTHashMap<nsStringHashKey, int32_t> imageTable(8);
386 // The principal that we check principals of any loaded images against.
387 nsCOMPtr<nsIPrincipal> principal = mTargetContent->NodePrincipal();
389 for (uint32_t primitiveElementIndex = 0;
390 primitiveElementIndex < primitives.Length(); ++primitiveElementIndex) {
391 SVGFE* filter = primitives[primitiveElementIndex];
393 AutoTArray<int32_t, 2> sourceIndices;
394 nsresult rv =
395 GetSourceIndices(filter, aPrimitiveDescrs, imageTable, sourceIndices);
396 if (NS_FAILED(rv)) {
397 return rv;
400 IntRect primitiveSubregion = ComputeFilterPrimitiveSubregion(
401 filter, aPrimitiveDescrs, sourceIndices);
403 nsTArray<bool> sourcesAreTainted;
404 GetInputsAreTainted(aPrimitiveDescrs, sourceIndices, aInputIsTainted,
405 sourcesAreTainted);
407 FilterPrimitiveDescription descr = filter->GetPrimitiveDescription(
408 this, primitiveSubregion, sourcesAreTainted, aInputImages);
410 descr.SetIsTainted(filter->OutputIsTainted(sourcesAreTainted, principal));
411 descr.SetFilterSpaceBounds(mFilterSpaceBounds);
412 descr.SetPrimitiveSubregion(
413 primitiveSubregion.Intersect(descr.FilterSpaceBounds()));
415 for (uint32_t i = 0; i < sourceIndices.Length(); i++) {
416 int32_t inputIndex = sourceIndices[i];
417 descr.SetInputPrimitive(i, inputIndex);
419 ColorSpace inputColorSpace =
420 inputIndex >= 0 ? aPrimitiveDescrs[inputIndex].OutputColorSpace()
421 : ColorSpace(ColorSpace::SRGB);
423 ColorSpace desiredInputColorSpace =
424 filter->GetInputColorSpace(i, inputColorSpace);
425 descr.SetInputColorSpace(i, desiredInputColorSpace);
426 if (i == 0) {
427 // the output color space is whatever in1 is if there is an in1
428 descr.SetOutputColorSpace(desiredInputColorSpace);
432 if (sourceIndices.Length() == 0) {
433 descr.SetOutputColorSpace(filter->GetOutputColorSpace());
436 aPrimitiveDescrs.AppendElement(std::move(descr));
437 uint32_t primitiveDescrIndex = aPrimitiveDescrs.Length() - 1;
439 nsAutoString str;
440 filter->GetResultImageName().GetAnimValue(str, filter);
441 imageTable.InsertOrUpdate(str, primitiveDescrIndex);
444 return NS_OK;
447 } // namespace mozilla