Bug 1858915 [wpt PR 42525] - Use taskcluster-run in infra tests, a=testonly
[gecko.git] / layout / svg / SVGFilterInstance.h
blob3c67db308f9bdf3b3e104d07913f879fa2a14386
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 #ifndef LAYOUT_SVG_SVGFILTERINSTANCE_H_
8 #define LAYOUT_SVG_SVGFILTERINSTANCE_H_
10 #include "gfxMatrix.h"
11 #include "gfxRect.h"
12 #include "SVGAnimatedNumber.h"
13 #include "SVGAnimatedNumberPair.h"
14 #include "SVGFilters.h"
15 #include "mozilla/ServoStyleConsts.h"
17 namespace mozilla {
18 class SVGFilterFrame;
20 namespace dom {
21 class SVGFilterElement;
22 } // namespace dom
24 /**
25 * This class helps FilterInstance build its filter graph by processing a
26 * single SVG reference filter.
28 * In BuildPrimitives, this class iterates through the referenced <filter>
29 * element's primitive elements, creating a FilterPrimitiveDescription for
30 * each one.
32 * This class uses several different coordinate spaces, defined as follows:
34 * "user space"
35 * The filtered SVG element's user space or the filtered HTML element's
36 * CSS pixel space. The origin for an HTML element is the top left corner of
37 * its border box.
39 * "filter space"
40 * User space scaled to device pixels. Shares the same origin as user space.
41 * This space is the same across chained SVG and CSS filters. To compute the
42 * overall filter space for a chain, we first need to build each filter's
43 * FilterPrimitiveDescriptions in some common space. That space is
44 * filter space.
46 * To understand the spaces better, let's take an example filter:
47 * <filter id="f">...</filter>
49 * And apply the filter to a div element:
50 * <div style="filter: url(#f); ...">...</div>
52 * And let's say there are 2 device pixels for every 1 CSS pixel.
54 * Finally, let's define an arbitrary point in user space:
55 * "user space point" = (10, 10)
57 * The point will be inset 10 CSS pixels from both the top and left edges of the
58 * div element's border box.
60 * Now, let's transform the point from user space to filter space:
61 * "filter space point" = "user space point" * "device pixels per CSS pixel"
62 * "filter space point" = (10, 10) * 2
63 * "filter space point" = (20, 20)
65 class SVGFilterInstance {
66 using Point3D = gfx::Point3D;
67 using IntRect = gfx::IntRect;
68 using SourceSurface = gfx::SourceSurface;
69 using FilterPrimitiveDescription = gfx::FilterPrimitiveDescription;
70 using SVGFilterPrimitiveElement = dom::SVGFilterPrimitiveElement;
71 using UserSpaceMetrics = dom::UserSpaceMetrics;
73 public:
74 /**
75 * @param aFilter The SVG filter reference from the style system. This class
76 * stores aFilter by reference, so callers should avoid modifying or
77 * deleting aFilter during the lifetime of SVGFilterInstance.
78 * @param aTargetContent The filtered element.
79 * @param aTargetBBox The SVG bbox to use for the target frame, computed by
80 * the caller. The caller may decide to override the actual SVG bbox.
82 SVGFilterInstance(
83 const StyleFilter& aFilter, SVGFilterFrame* aFilterFrame,
84 nsIContent* aTargetContent, const UserSpaceMetrics& aMetrics,
85 const gfxRect& aTargetBBox,
86 const gfx::MatrixScalesDouble& aUserSpaceToFilterSpaceScale);
88 /**
89 * Returns true if the filter instance was created successfully.
91 bool IsInitialized() const { return mInitialized; }
93 /**
94 * Iterates through the <filter> element's primitive elements, creating a
95 * FilterPrimitiveDescription for each one. Appends the new
96 * FilterPrimitiveDescription(s) to the aPrimitiveDescrs list. Also, appends
97 * new images from feImage filter primitive elements to the aInputImages list.
98 * aInputIsTainted describes whether the input to this filter is tainted, i.e.
99 * whether it contains security-sensitive content. This is needed to propagate
100 * taintedness to the FilterPrimitive that take tainted inputs. Something
101 * being tainted means that it contains security sensitive content. The input
102 * to this filter is the previous filter's output, i.e. the last element in
103 * aPrimitiveDescrs, or the SourceGraphic input if this is the first filter in
104 * the filter chain.
106 nsresult BuildPrimitives(
107 nsTArray<FilterPrimitiveDescription>& aPrimitiveDescrs,
108 nsTArray<RefPtr<SourceSurface>>& aInputImages, bool aInputIsTainted);
110 float GetPrimitiveNumber(uint8_t aCtxType,
111 const SVGAnimatedNumber* aNumber) const {
112 return GetPrimitiveNumber(aCtxType, aNumber->GetAnimValue());
114 float GetPrimitiveNumber(uint8_t aCtxType,
115 const SVGAnimatedNumberPair* aNumberPair,
116 SVGAnimatedNumberPair::PairIndex aIndex) const {
117 return GetPrimitiveNumber(aCtxType, aNumberPair->GetAnimValue(aIndex));
121 * Converts a userSpaceOnUse/objectBoundingBoxUnits unitless point
122 * into filter space, depending on the value of mPrimitiveUnits. (For
123 * objectBoundingBoxUnits, the bounding box offset is applied to the point.)
125 Point3D ConvertLocation(const Point3D& aPoint) const;
128 * Transform a rect between user space and filter space.
130 gfxRect UserSpaceToFilterSpace(const gfxRect& aUserSpaceRect) const;
132 private:
134 * Computes the filter primitive subregion for the given primitive.
136 IntRect ComputeFilterPrimitiveSubregion(
137 SVGFilterPrimitiveElement* aFilterElement,
138 const nsTArray<FilterPrimitiveDescription>& aPrimitiveDescrs,
139 const nsTArray<int32_t>& aInputIndices);
142 * Takes the input indices of a filter primitive and returns for each input
143 * whether the input's output is tainted.
145 void GetInputsAreTainted(
146 const nsTArray<FilterPrimitiveDescription>& aPrimitiveDescrs,
147 const nsTArray<int32_t>& aInputIndices, bool aFilterInputIsTainted,
148 nsTArray<bool>& aOutInputsAreTainted);
151 * Scales a numeric filter primitive length in the X, Y or "XY" directions
152 * into a length in filter space (no offset is applied).
154 float GetPrimitiveNumber(uint8_t aCtxType, float aValue) const;
157 * Returns the transform from frame space to the coordinate space that
158 * GetCanvasTM transforms to. "Frame space" is the origin of a frame, aka the
159 * top-left corner of its border box, aka the top left corner of its mRect.
161 gfxMatrix GetUserSpaceToFrameSpaceInCSSPxTransform() const;
164 * Appends a new FilterPrimitiveDescription to aPrimitiveDescrs that
165 * converts the FilterPrimitiveDescription at mSourceGraphicIndex into
166 * a SourceAlpha input for the next FilterPrimitiveDescription.
168 * The new FilterPrimitiveDescription zeros out the SourceGraphic's RGB
169 * channels and keeps the alpha channel intact.
171 int32_t GetOrCreateSourceAlphaIndex(
172 nsTArray<FilterPrimitiveDescription>& aPrimitiveDescrs);
175 * Finds the index in aPrimitiveDescrs of each input to aPrimitiveElement.
176 * For example, if aPrimitiveElement is:
177 * <feGaussianBlur in="another-primitive" .../>
178 * Then, the resulting aSourceIndices will contain the index of the
179 * FilterPrimitiveDescription representing "another-primitive".
181 nsresult GetSourceIndices(
182 SVGFilterPrimitiveElement* aPrimitiveElement,
183 nsTArray<FilterPrimitiveDescription>& aPrimitiveDescrs,
184 const nsTHashMap<nsStringHashKey, int32_t>& aImageTable,
185 nsTArray<int32_t>& aSourceIndices);
188 * Compute the filter region in user space, filter space, and filter
189 * space.
191 bool ComputeBounds();
194 * The SVG reference filter originally from the style system.
196 const StyleFilter& mFilter;
199 * The filtered element.
201 nsIContent* mTargetContent;
204 * The SVG user space metrics that SVG lengths are resolved against.
206 const UserSpaceMetrics& mMetrics;
209 * The filter element referenced by mTargetFrame's element.
211 const dom::SVGFilterElement* mFilterElement;
214 * The frame for the SVG filter element.
216 SVGFilterFrame* mFilterFrame;
219 * The SVG bbox of the element that is being filtered, in user space.
221 gfxRect mTargetBBox;
224 * The "filter region" in various spaces.
226 nsIntRect mFilterSpaceBounds;
229 * The scale factors between user space and filter space.
231 gfx::MatrixScalesDouble mUserSpaceToFilterSpaceScale;
234 * The 'primitiveUnits' attribute value (objectBoundingBox or userSpaceOnUse).
236 uint16_t mPrimitiveUnits;
239 * The index of the FilterPrimitiveDescription that this SVG filter should use
240 * as its SourceGraphic, or the SourceGraphic keyword index if this is the
241 * first filter in a chain. Initialized in BuildPrimitives
243 MOZ_INIT_OUTSIDE_CTOR int32_t mSourceGraphicIndex;
246 * The index of the FilterPrimitiveDescription that this SVG filter should use
247 * as its SourceAlpha, or the SourceAlpha keyword index if this is the first
248 * filter in a chain. Initialized in BuildPrimitives
250 MOZ_INIT_OUTSIDE_CTOR int32_t mSourceAlphaIndex;
253 * SourceAlpha is available if GetOrCreateSourceAlphaIndex has been called.
255 int32_t mSourceAlphaAvailable;
257 bool mInitialized;
260 } // namespace mozilla
262 #endif // LAYOUT_SVG_SVGFILTERINSTANCE_H_