Line data Source code
1 : /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 : /* This Source Code Form is subject to the terms of the Mozilla Public
3 : * License, v. 2.0. If a copy of the MPL was not distributed with this
4 : * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5 :
6 : #ifndef __NS_SVGFILTERINSTANCE_H__
7 : #define __NS_SVGFILTERINSTANCE_H__
8 :
9 : #include "gfxMatrix.h"
10 : #include "gfxRect.h"
11 : #include "nsSVGFilters.h"
12 : #include "nsSVGNumber2.h"
13 : #include "nsSVGNumberPair.h"
14 : #include "nsTArray.h"
15 :
16 : class nsSVGFilterFrame;
17 : struct nsStyleFilter;
18 :
19 : namespace mozilla {
20 : namespace dom {
21 : class SVGFilterElement;
22 : } // namespace dom
23 : } // namespace mozilla
24 :
25 : /**
26 : * This class helps nsFilterInstance build its filter graph by processing a
27 : * single SVG reference filter.
28 : *
29 : * In BuildPrimitives, this class iterates through the referenced <filter>
30 : * element's primitive elements, creating a FilterPrimitiveDescription for
31 : * each one.
32 : *
33 : * This class uses several different coordinate spaces, defined as follows:
34 : *
35 : * "user space"
36 : * The filtered SVG element's user space or the filtered HTML element's
37 : * CSS pixel space. The origin for an HTML element is the top left corner of
38 : * its border box.
39 : *
40 : * "filter space"
41 : * User space scaled to device pixels. Shares the same origin as user space.
42 : * This space is the same across chained SVG and CSS filters. To compute the
43 : * overall filter space for a chain, we first need to build each filter's
44 : * FilterPrimitiveDescriptions in some common space. That space is
45 : * filter space.
46 : *
47 : * To understand the spaces better, let's take an example filter:
48 : * <filter id="f">...</filter>
49 : *
50 : * And apply the filter to a div element:
51 : * <div style="filter: url(#f); ...">...</div>
52 : *
53 : * And let's say there are 2 device pixels for every 1 CSS pixel.
54 : *
55 : * Finally, let's define an arbitrary point in user space:
56 : * "user space point" = (10, 10)
57 : *
58 : * The point will be inset 10 CSS pixels from both the top and left edges of the
59 : * div element's border box.
60 : *
61 : * Now, let's transform the point from user space to filter space:
62 : * "filter space point" = "user space point" * "device pixels per CSS pixel"
63 : * "filter space point" = (10, 10) * 2
64 : * "filter space point" = (20, 20)
65 : */
66 : class nsSVGFilterInstance
67 : {
68 : typedef mozilla::gfx::Point3D Point3D;
69 : typedef mozilla::gfx::IntRect IntRect;
70 : typedef mozilla::gfx::SourceSurface SourceSurface;
71 : typedef mozilla::gfx::FilterPrimitiveDescription FilterPrimitiveDescription;
72 : typedef mozilla::dom::UserSpaceMetrics UserSpaceMetrics;
73 :
74 : public:
75 : /**
76 : * @param aFilter The SVG filter reference from the style system. This class
77 : * stores aFilter by reference, so callers should avoid modifying or
78 : * deleting aFilter during the lifetime of nsSVGFilterInstance.
79 : * @param aTargetContent The filtered element.
80 : * @param aTargetBBox The SVG bbox to use for the target frame, computed by
81 : * the caller. The caller may decide to override the actual SVG bbox.
82 : */
83 : nsSVGFilterInstance(const nsStyleFilter& aFilter,
84 : nsIFrame* aTargetFrame,
85 : nsIContent* aTargetContent,
86 : const UserSpaceMetrics& aMetrics,
87 : const gfxRect& aTargetBBox,
88 : const gfxSize& aUserSpaceToFilterSpaceScale);
89 :
90 : /**
91 : * Returns true if the filter instance was created successfully.
92 : */
93 0 : bool IsInitialized() const { return mInitialized; }
94 :
95 : /**
96 : * Iterates through the <filter> element's primitive elements, creating a
97 : * FilterPrimitiveDescription for each one. Appends the new
98 : * FilterPrimitiveDescription(s) to the aPrimitiveDescrs list. Also, appends
99 : * new images from feImage filter primitive elements to the aInputImages list.
100 : * aInputIsTainted describes whether the input to this filter is tainted, i.e.
101 : * whether it contains security-sensitive content. This is needed to propagate
102 : * taintedness to the FilterPrimitive that take tainted inputs. Something being
103 : * tainted means that it contains security sensitive content.
104 : * The input to this filter is the previous filter's output, i.e. the last
105 : * element in aPrimitiveDescrs, or the SourceGraphic input if this is the first
106 : * filter in the filter chain.
107 : */
108 : nsresult BuildPrimitives(nsTArray<FilterPrimitiveDescription>& aPrimitiveDescrs,
109 : nsTArray<RefPtr<SourceSurface>>& aInputImages,
110 : bool aInputIsTainted);
111 :
112 0 : float GetPrimitiveNumber(uint8_t aCtxType, const nsSVGNumber2 *aNumber) const
113 : {
114 0 : return GetPrimitiveNumber(aCtxType, aNumber->GetAnimValue());
115 : }
116 0 : float GetPrimitiveNumber(uint8_t aCtxType, const nsSVGNumberPair *aNumberPair,
117 : nsSVGNumberPair::PairIndex aIndex) const
118 : {
119 0 : return GetPrimitiveNumber(aCtxType, aNumberPair->GetAnimValue(aIndex));
120 : }
121 :
122 : /**
123 : * Converts a userSpaceOnUse/objectBoundingBoxUnits unitless point
124 : * into filter space, depending on the value of mPrimitiveUnits. (For
125 : * objectBoundingBoxUnits, the bounding box offset is applied to the point.)
126 : */
127 : Point3D ConvertLocation(const Point3D& aPoint) const;
128 :
129 : /**
130 : * Transform a rect between user space and filter space.
131 : */
132 : gfxRect UserSpaceToFilterSpace(const gfxRect& aUserSpaceRect) const;
133 :
134 : private:
135 : /**
136 : * Finds the filter frame associated with this SVG filter.
137 : */
138 : nsSVGFilterFrame* GetFilterFrame(nsIFrame* aTargetFrame);
139 :
140 : /**
141 : * Computes the filter primitive subregion for the given primitive.
142 : */
143 : IntRect ComputeFilterPrimitiveSubregion(nsSVGFE* aFilterElement,
144 : const nsTArray<FilterPrimitiveDescription>& aPrimitiveDescrs,
145 : const nsTArray<int32_t>& aInputIndices);
146 :
147 : /**
148 : * Takes the input indices of a filter primitive and returns for each input
149 : * whether the input's output is tainted.
150 : */
151 : void GetInputsAreTainted(const nsTArray<FilterPrimitiveDescription>& aPrimitiveDescrs,
152 : const nsTArray<int32_t>& aInputIndices,
153 : bool aFilterInputIsTainted,
154 : nsTArray<bool>& aOutInputsAreTainted);
155 :
156 : /**
157 : * Scales a numeric filter primitive length in the X, Y or "XY" directions
158 : * into a length in filter space (no offset is applied).
159 : */
160 : float GetPrimitiveNumber(uint8_t aCtxType, float aValue) const;
161 :
162 : /**
163 : * Returns the transform from frame space to the coordinate space that
164 : * GetCanvasTM transforms to. "Frame space" is the origin of a frame, aka the
165 : * top-left corner of its border box, aka the top left corner of its mRect.
166 : */
167 : gfxMatrix GetUserSpaceToFrameSpaceInCSSPxTransform() const;
168 :
169 : /**
170 : * Appends a new FilterPrimitiveDescription to aPrimitiveDescrs that
171 : * converts the FilterPrimitiveDescription at mSourceGraphicIndex into
172 : * a SourceAlpha input for the next FilterPrimitiveDescription.
173 : *
174 : * The new FilterPrimitiveDescription zeros out the SourceGraphic's RGB
175 : * channels and keeps the alpha channel intact.
176 : */
177 : int32_t GetOrCreateSourceAlphaIndex(nsTArray<FilterPrimitiveDescription>& aPrimitiveDescrs);
178 :
179 : /**
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".
185 : */
186 : nsresult GetSourceIndices(nsSVGFE* aPrimitiveElement,
187 : nsTArray<FilterPrimitiveDescription>& aPrimitiveDescrs,
188 : const nsDataHashtable<nsStringHashKey, int32_t>& aImageTable,
189 : nsTArray<int32_t>& aSourceIndices);
190 :
191 : /**
192 : * Compute the filter region in user space, filter space, and filter
193 : * space.
194 : */
195 : bool ComputeBounds();
196 :
197 : /**
198 : * The SVG reference filter originally from the style system.
199 : */
200 : const nsStyleFilter& mFilter;
201 :
202 : /**
203 : * The filtered element.
204 : */
205 : nsIContent* mTargetContent;
206 :
207 : /**
208 : * The SVG user space metrics that SVG lengths are resolved against.
209 : */
210 : const UserSpaceMetrics& mMetrics;
211 :
212 : /**
213 : * The filter element referenced by mTargetFrame's element.
214 : */
215 : const mozilla::dom::SVGFilterElement* mFilterElement;
216 :
217 : /**
218 : * The frame for the SVG filter element.
219 : */
220 : nsSVGFilterFrame* mFilterFrame;
221 :
222 : /**
223 : * The SVG bbox of the element that is being filtered, in user space.
224 : */
225 : gfxRect mTargetBBox;
226 :
227 : /**
228 : * The "filter region" in various spaces.
229 : */
230 : nsIntRect mFilterSpaceBounds;
231 :
232 : /**
233 : * The scale factors between user space and filter space.
234 : */
235 : gfxSize mUserSpaceToFilterSpaceScale;
236 :
237 : /**
238 : * The 'primitiveUnits' attribute value (objectBoundingBox or userSpaceOnUse).
239 : */
240 : uint16_t mPrimitiveUnits;
241 :
242 : /**
243 : * The index of the FilterPrimitiveDescription that this SVG filter should use
244 : * as its SourceGraphic, or the SourceGraphic keyword index if this is the
245 : * first filter in a chain. Initialized in BuildPrimitives
246 : */
247 : MOZ_INIT_OUTSIDE_CTOR int32_t mSourceGraphicIndex;
248 :
249 : /**
250 : * The index of the FilterPrimitiveDescription that this SVG filter should use
251 : * as its SourceAlpha, or the SourceAlpha keyword index if this is the first
252 : * filter in a chain. Initialized in BuildPrimitives
253 : */
254 : MOZ_INIT_OUTSIDE_CTOR int32_t mSourceAlphaIndex;
255 :
256 : /**
257 : * SourceAlpha is available if GetOrCreateSourceAlphaIndex has been called.
258 : */
259 : int32_t mSourceAlphaAvailable;
260 :
261 : bool mInitialized;
262 : };
263 :
264 : #endif
|