Bug 1895153 - Implement "Find in page..." menu functionality r=android-reviewers...
[gecko.git] / layout / svg / SVGFilterInstance.h
blob0bdfcf65134d1d8c23a48db6e1e5f27aa613300e
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(const StyleFilter& aFilter, SVGFilterFrame* aFilterFrame,
83 nsIContent* aTargetContent,
84 const UserSpaceMetrics& aMetrics,
85 const gfxRect& aTargetBBox,
86 const gfx::MatrixScalesDouble& aUserSpaceToFilterSpaceScale,
87 gfxRect& aFilterSpaceBoundsNotSnapped);
89 /**
90 * Returns true if the filter instance was created successfully.
92 bool IsInitialized() const { return mInitialized; }
94 /**
95 * Iterates through the <filter> element's primitive elements, creating a
96 * FilterPrimitiveDescription for each one. Appends the new
97 * FilterPrimitiveDescription(s) to the aPrimitiveDescrs list. Also, appends
98 * new images from feImage filter primitive elements to the aInputImages list.
99 * aInputIsTainted describes whether the input to this filter is tainted, i.e.
100 * whether it contains security-sensitive content. This is needed to propagate
101 * taintedness to the FilterPrimitive that take tainted inputs. Something
102 * being tainted means that it contains security sensitive content. The input
103 * to this filter is the previous filter's output, i.e. the last element in
104 * aPrimitiveDescrs, or the SourceGraphic input if this is the first filter in
105 * the filter chain.
107 nsresult BuildPrimitives(
108 nsTArray<FilterPrimitiveDescription>& aPrimitiveDescrs,
109 nsTArray<RefPtr<SourceSurface>>& aInputImages, bool aInputIsTainted);
111 float GetPrimitiveNumber(uint8_t aCtxType,
112 const SVGAnimatedNumber* aNumber) const {
113 return GetPrimitiveNumber(aCtxType, aNumber->GetAnimValue());
115 float GetPrimitiveNumber(uint8_t aCtxType,
116 const SVGAnimatedNumberPair* aNumberPair,
117 SVGAnimatedNumberPair::PairIndex aIndex) const {
118 return GetPrimitiveNumber(aCtxType, aNumberPair->GetAnimValue(aIndex));
122 * Converts a userSpaceOnUse/objectBoundingBoxUnits unitless point
123 * into filter space, depending on the value of mPrimitiveUnits. (For
124 * objectBoundingBoxUnits, the bounding box offset is applied to the point.)
126 Point3D ConvertLocation(const Point3D& aPoint) const;
129 * Transform a rect between user space and filter space.
131 gfxRect UserSpaceToFilterSpace(const gfxRect& aUserSpaceRect) const;
133 private:
135 * Computes the filter primitive subregion for the given primitive.
137 IntRect ComputeFilterPrimitiveSubregion(
138 SVGFilterPrimitiveElement* aFilterElement,
139 const nsTArray<FilterPrimitiveDescription>& aPrimitiveDescrs,
140 const nsTArray<int32_t>& aInputIndices);
143 * Takes the input indices of a filter primitive and returns for each input
144 * whether the input's output is tainted.
146 void GetInputsAreTainted(
147 const nsTArray<FilterPrimitiveDescription>& aPrimitiveDescrs,
148 const nsTArray<int32_t>& aInputIndices, bool aFilterInputIsTainted,
149 nsTArray<bool>& aOutInputsAreTainted);
152 * Scales a numeric filter primitive length in the X, Y or "XY" directions
153 * into a length in filter space (no offset is applied).
155 float GetPrimitiveNumber(uint8_t aCtxType, float aValue) const;
158 * Returns the transform from frame space to the coordinate space that
159 * GetCanvasTM transforms to. "Frame space" is the origin of a frame, aka the
160 * top-left corner of its border box, aka the top left corner of its mRect.
162 gfxMatrix GetUserSpaceToFrameSpaceInCSSPxTransform() const;
165 * Appends a new FilterPrimitiveDescription to aPrimitiveDescrs that
166 * converts the FilterPrimitiveDescription at mSourceGraphicIndex into
167 * a SourceAlpha input for the next FilterPrimitiveDescription.
169 * The new FilterPrimitiveDescription zeros out the SourceGraphic's RGB
170 * channels and keeps the alpha channel intact.
172 int32_t GetOrCreateSourceAlphaIndex(
173 nsTArray<FilterPrimitiveDescription>& aPrimitiveDescrs);
176 * Finds the index in aPrimitiveDescrs of each input to aPrimitiveElement.
177 * For example, if aPrimitiveElement is:
178 * <feGaussianBlur in="another-primitive" .../>
179 * Then, the resulting aSourceIndices will contain the index of the
180 * FilterPrimitiveDescription representing "another-primitive".
182 nsresult GetSourceIndices(
183 SVGFilterPrimitiveElement* aPrimitiveElement,
184 nsTArray<FilterPrimitiveDescription>& aPrimitiveDescrs,
185 const nsTHashMap<nsStringHashKey, int32_t>& aImageTable,
186 nsTArray<int32_t>& aSourceIndices);
189 * Compute the filter region in user space, filter space, and filter
190 * space.
192 bool ComputeBounds();
195 * The SVG reference filter originally from the style system.
197 const StyleFilter& mFilter;
200 * The filtered element.
202 nsIContent* mTargetContent;
205 * The SVG user space metrics that SVG lengths are resolved against.
207 const UserSpaceMetrics& mMetrics;
210 * The filter element referenced by mTargetFrame's element.
212 const dom::SVGFilterElement* mFilterElement;
215 * The frame for the SVG filter element.
217 SVGFilterFrame* mFilterFrame;
220 * The SVG bbox of the element that is being filtered, in user space.
222 gfxRect mTargetBBox;
225 * The "filter region" in various spaces.
227 nsIntRect mFilterSpaceBounds;
230 * The bounds of the filter element itself, which may be non-integer.
232 gfxRect mFilterSpaceBoundsNotSnapped;
235 * The scale factors between user space and filter space.
237 gfx::MatrixScalesDouble mUserSpaceToFilterSpaceScale;
240 * The 'primitiveUnits' attribute value (objectBoundingBox or userSpaceOnUse).
242 uint16_t mPrimitiveUnits;
245 * The index of the FilterPrimitiveDescription that this SVG filter should use
246 * as its SourceGraphic, or the SourceGraphic keyword index if this is the
247 * first filter in a chain. Initialized in BuildPrimitives
249 MOZ_INIT_OUTSIDE_CTOR int32_t mSourceGraphicIndex;
252 * The index of the FilterPrimitiveDescription that this SVG filter should use
253 * as its SourceAlpha, or the SourceAlpha keyword index if this is the first
254 * filter in a chain. Initialized in BuildPrimitives
256 MOZ_INIT_OUTSIDE_CTOR int32_t mSourceAlphaIndex;
259 * SourceAlpha is available if GetOrCreateSourceAlphaIndex has been called.
261 int32_t mSourceAlphaAvailable;
263 bool mInitialized;
266 } // namespace mozilla
268 #endif // LAYOUT_SVG_SVGFILTERINSTANCE_H_