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"
12 #include "SVGAnimatedNumber.h"
13 #include "SVGAnimatedNumberPair.h"
14 #include "SVGFilters.h"
15 #include "mozilla/ServoStyleConsts.h"
21 class SVGFilterElement
;
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
32 * This class uses several different coordinate spaces, defined as follows:
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
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
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
;
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.
83 const StyleFilter
& aFilter
, SVGFilterFrame
* aFilterFrame
,
84 nsIContent
* aTargetContent
, const UserSpaceMetrics
& aMetrics
,
85 const gfxRect
& aTargetBBox
,
86 const gfx::MatrixScalesDouble
& aUserSpaceToFilterSpaceScale
);
89 * Returns true if the filter instance was created successfully.
91 bool IsInitialized() const { return mInitialized
; }
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
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;
134 * Finds the filter frame associated with this SVG filter.
136 SVGFilterFrame
* GetFilterFrame(nsIFrame
* aTargetFrame
);
139 * Computes the filter primitive subregion for the given primitive.
141 IntRect
ComputeFilterPrimitiveSubregion(
142 SVGFilterPrimitiveElement
* aFilterElement
,
143 const nsTArray
<FilterPrimitiveDescription
>& aPrimitiveDescrs
,
144 const nsTArray
<int32_t>& aInputIndices
);
147 * Takes the input indices of a filter primitive and returns for each input
148 * whether the input's output is tainted.
150 void GetInputsAreTainted(
151 const nsTArray
<FilterPrimitiveDescription
>& aPrimitiveDescrs
,
152 const nsTArray
<int32_t>& aInputIndices
, bool aFilterInputIsTainted
,
153 nsTArray
<bool>& aOutInputsAreTainted
);
156 * Scales a numeric filter primitive length in the X, Y or "XY" directions
157 * into a length in filter space (no offset is applied).
159 float GetPrimitiveNumber(uint8_t aCtxType
, float aValue
) const;
162 * Returns the transform from frame space to the coordinate space that
163 * GetCanvasTM transforms to. "Frame space" is the origin of a frame, aka the
164 * top-left corner of its border box, aka the top left corner of its mRect.
166 gfxMatrix
GetUserSpaceToFrameSpaceInCSSPxTransform() const;
169 * Appends a new FilterPrimitiveDescription to aPrimitiveDescrs that
170 * converts the FilterPrimitiveDescription at mSourceGraphicIndex into
171 * a SourceAlpha input for the next FilterPrimitiveDescription.
173 * The new FilterPrimitiveDescription zeros out the SourceGraphic's RGB
174 * channels and keeps the alpha channel intact.
176 int32_t GetOrCreateSourceAlphaIndex(
177 nsTArray
<FilterPrimitiveDescription
>& aPrimitiveDescrs
);
180 * Finds the index in aPrimitiveDescrs of each input to aPrimitiveElement.
181 * For example, if aPrimitiveElement is:
182 * <feGaussianBlur in="another-primitive" .../>
183 * Then, the resulting aSourceIndices will contain the index of the
184 * FilterPrimitiveDescription representing "another-primitive".
186 nsresult
GetSourceIndices(
187 SVGFilterPrimitiveElement
* aPrimitiveElement
,
188 nsTArray
<FilterPrimitiveDescription
>& aPrimitiveDescrs
,
189 const nsTHashMap
<nsStringHashKey
, int32_t>& aImageTable
,
190 nsTArray
<int32_t>& aSourceIndices
);
193 * Compute the filter region in user space, filter space, and filter
196 bool ComputeBounds();
199 * The SVG reference filter originally from the style system.
201 const StyleFilter
& mFilter
;
204 * The filtered element.
206 nsIContent
* mTargetContent
;
209 * The SVG user space metrics that SVG lengths are resolved against.
211 const UserSpaceMetrics
& mMetrics
;
214 * The filter element referenced by mTargetFrame's element.
216 const dom::SVGFilterElement
* mFilterElement
;
219 * The frame for the SVG filter element.
221 SVGFilterFrame
* mFilterFrame
;
224 * The SVG bbox of the element that is being filtered, in user space.
229 * The "filter region" in various spaces.
231 nsIntRect mFilterSpaceBounds
;
234 * The scale factors between user space and filter space.
236 gfx::MatrixScalesDouble mUserSpaceToFilterSpaceScale
;
239 * The 'primitiveUnits' attribute value (objectBoundingBox or userSpaceOnUse).
241 uint16_t mPrimitiveUnits
;
244 * The index of the FilterPrimitiveDescription that this SVG filter should use
245 * as its SourceGraphic, or the SourceGraphic keyword index if this is the
246 * first filter in a chain. Initialized in BuildPrimitives
248 MOZ_INIT_OUTSIDE_CTOR
int32_t mSourceGraphicIndex
;
251 * The index of the FilterPrimitiveDescription that this SVG filter should use
252 * as its SourceAlpha, or the SourceAlpha keyword index if this is the first
253 * filter in a chain. Initialized in BuildPrimitives
255 MOZ_INIT_OUTSIDE_CTOR
int32_t mSourceAlphaIndex
;
258 * SourceAlpha is available if GetOrCreateSourceAlphaIndex has been called.
260 int32_t mSourceAlphaAvailable
;
265 } // namespace mozilla
267 #endif // LAYOUT_SVG_SVGFILTERINSTANCE_H_