Bug 1814091 - Move CanvasContext.getPreferredFormat to GPU.getPreferredCanvasFormat...
[gecko.git] / layout / svg / FilterInstance.h
blobde59f2aaf772d26ef213f9ba420e5e5baad6e0a9
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_FILTERINSTANCE_H_
8 #define LAYOUT_SVG_FILTERINSTANCE_H_
10 #include "gfxMatrix.h"
11 #include "gfxPoint.h"
12 #include "gfxRect.h"
13 #include "nsCOMPtr.h"
14 #include "FilterDescription.h"
15 #include "nsHashKeys.h"
16 #include "nsPoint.h"
17 #include "nsRect.h"
18 #include "nsSize.h"
19 #include "nsTArray.h"
20 #include "mozilla/gfx/2D.h"
21 #include "mozilla/webrender/WebRenderTypes.h"
22 #include "mozilla/ServoStyleConsts.h"
23 #include "mozilla/SVGIntegrationUtils.h"
25 class gfxContext;
26 class nsIContent;
27 class nsIFrame;
28 struct WrFiltersHolder;
30 namespace mozilla {
32 namespace dom {
33 class UserSpaceMetrics;
34 } // namespace dom
36 namespace image {
37 struct imgDrawingParams;
40 /**
41 * This class performs all filter processing.
43 * We build a graph of the filter image data flow, essentially
44 * converting the filter graph to SSA. This lets us easily propagate
45 * analysis data (such as bounding-boxes) over the filter primitive graph.
47 * Definition of "filter space": filter space is a coordinate system that is
48 * aligned with the user space of the filtered element, with its origin located
49 * at the top left of the filter region, and with one unit equal in size to one
50 * pixel of the offscreen surface into which the filter output would/will be
51 * painted.
53 * The definition of "filter region" can be found here:
54 * http://www.w3.org/TR/SVG11/filters.html#FilterEffectsRegion
56 class FilterInstance {
57 using IntRect = gfx::IntRect;
58 using SourceSurface = gfx::SourceSurface;
59 using DrawTarget = gfx::DrawTarget;
60 using FilterPrimitiveDescription = gfx::FilterPrimitiveDescription;
61 using FilterDescription = gfx::FilterDescription;
62 using UserSpaceMetrics = dom::UserSpaceMetrics;
63 using imgDrawingParams = image::imgDrawingParams;
64 using SVGFilterPaintCallback = SVGIntegrationUtils::SVGFilterPaintCallback;
66 public:
67 /**
68 * Create a FilterDescription for the supplied filter. All coordinates in
69 * the description are in filter space.
70 * @param aFilterInputIsTainted Describes whether the SourceImage /
71 * SourceAlpha input is tainted. This affects whether feDisplacementMap
72 * will respect the filter input as its map input, and it affects the
73 * IsTainted() state on the filter primitives in the FilterDescription.
74 * "Tainted" is a term from the filters spec and means security-sensitive
75 * content, i.e. pixels that JS should not be able to read in any way.
76 * @param aOutAdditionalImages Will contain additional images needed to
77 * render the filter (from feImage primitives).
78 * @return A FilterDescription describing the filter.
80 static FilterDescription GetFilterDescription(
81 nsIContent* aFilteredElement, Span<const StyleFilter> aFilterChain,
82 bool aFilterInputIsTainted, const UserSpaceMetrics& aMetrics,
83 const gfxRect& aBBox,
84 nsTArray<RefPtr<SourceSurface>>& aOutAdditionalImages);
86 /**
87 * Paint the given filtered frame.
88 * @param aDirtyArea The area than needs to be painted, in aFilteredFrame's
89 * frame space (i.e. relative to its origin, the top-left corner of its
90 * border box).
92 static void PaintFilteredFrame(nsIFrame* aFilteredFrame,
93 Span<const StyleFilter> aFilterChain,
94 gfxContext* aCtx,
95 const SVGFilterPaintCallback& aPaintCallback,
96 const nsRegion* aDirtyArea,
97 imgDrawingParams& aImgParams,
98 float aOpacity = 1.0f);
101 * Returns the post-filter area that could be dirtied when the given
102 * pre-filter area of aFilteredFrame changes.
103 * @param aPreFilterDirtyRegion The pre-filter area of aFilteredFrame that
104 * has changed, relative to aFilteredFrame, in app units.
106 static nsRegion GetPostFilterDirtyArea(nsIFrame* aFilteredFrame,
107 const nsRegion& aPreFilterDirtyRegion);
110 * Returns the pre-filter area that is needed from aFilteredFrame when the
111 * given post-filter area needs to be repainted.
112 * @param aPostFilterDirtyRegion The post-filter area that is dirty, relative
113 * to aFilteredFrame, in app units.
115 static nsRegion GetPreFilterNeededArea(
116 nsIFrame* aFilteredFrame, const nsRegion& aPostFilterDirtyRegion);
119 * Returns the post-filter ink overflow rect (paint bounds) of
120 * aFilteredFrame.
121 * @param aOverrideBBox A user space rect, in user units, that should be used
122 * as aFilteredFrame's bbox ('bbox' is a specific SVG term), if non-null.
123 * @param aPreFilterBounds The pre-filter ink overflow rect of
124 * aFilteredFrame, if non-null.
126 static Maybe<nsRect> GetPostFilterBounds(
127 nsIFrame* aFilteredFrame, const gfxRect* aOverrideBBox = nullptr,
128 const nsRect* aPreFilterBounds = nullptr);
131 * Try to build WebRender filters for a frame if the filters applied to it are
132 * supported. aInitialized is set to true if the filter has been initialized
133 * and false otherwise (e.g. a bad url). If aInitialized is false the filter
134 * the filter contents should not be drawn.
136 static bool BuildWebRenderFilters(
137 nsIFrame* aFilteredFrame,
138 mozilla::Span<const mozilla::StyleFilter> aFilters,
139 WrFiltersHolder& aWrFilters, bool& aInitialized);
141 private:
143 * @param aTargetFrame The frame of the filtered element under consideration,
144 * may be null.
145 * @param aTargetContent The filtered element itself.
146 * @param aMetrics The metrics to resolve SVG lengths against.
147 * @param aFilterChain The list of filters to apply.
148 * @param aFilterInputIsTainted Describes whether the SourceImage /
149 * SourceAlpha input is tainted. This affects whether feDisplacementMap
150 * will respect the filter input as its map input.
151 * @param aPaintCallback [optional] The callback that Render() should use to
152 * paint. Only required if you will call Render().
153 * @param aPaintTransform The transform to apply to convert to
154 * aTargetFrame's SVG user space. Only used when painting.
155 * @param aPostFilterDirtyRegion [optional] The post-filter area
156 * that has to be repainted, in app units. Only required if you will
157 * call ComputeSourceNeededRect() or Render().
158 * @param aPreFilterDirtyRegion [optional] The pre-filter area of
159 * the filtered element that changed, in app units. Only required if you
160 * will call ComputePostFilterDirtyRegion().
161 * @param aPreFilterInkOverflowRectOverride [optional] Use a different
162 * ink overflow rect for the target element.
163 * @param aOverrideBBox [optional] Use a different SVG bbox for the target
164 * element. Must be non-null if aTargetFrame is null.
166 FilterInstance(
167 nsIFrame* aTargetFrame, nsIContent* aTargetContent,
168 const UserSpaceMetrics& aMetrics, Span<const StyleFilter> aFilterChain,
169 bool aFilterInputIsTainted,
170 const SVGIntegrationUtils::SVGFilterPaintCallback& aPaintCallback,
171 const gfxMatrix& aPaintTransform,
172 const nsRegion* aPostFilterDirtyRegion = nullptr,
173 const nsRegion* aPreFilterDirtyRegion = nullptr,
174 const nsRect* aPreFilterInkOverflowRectOverride = nullptr,
175 const gfxRect* aOverrideBBox = nullptr);
177 static bool BuildWebRenderFiltersImpl(
178 nsIFrame* aFilteredFrame,
179 mozilla::Span<const mozilla::StyleFilter> aFilters,
180 WrFiltersHolder& aWrFilters, bool& aInitialized);
183 * Returns true if the filter instance was created successfully.
185 bool IsInitialized() const { return mInitialized; }
188 * Draws the filter output into aDrawTarget. The area that
189 * needs to be painted must have been specified before calling this method
190 * by passing it as the aPostFilterDirtyRegion argument to the
191 * FilterInstance constructor.
193 void Render(gfxContext* aCtx, imgDrawingParams& aImgParams,
194 float aOpacity = 1.0f);
196 const FilterDescription& ExtractDescriptionAndAdditionalImages(
197 nsTArray<RefPtr<SourceSurface>>& aOutAdditionalImages) {
198 aOutAdditionalImages = std::move(mInputImages);
199 return mFilterDescription;
203 * Sets the aPostFilterDirtyRegion outparam to the post-filter area in frame
204 * space that would be dirtied by mTargetFrame when a given
205 * pre-filter area of mTargetFrame is dirtied. The pre-filter area must have
206 * been specified before calling this method by passing it as the
207 * aPreFilterDirtyRegion argument to the FilterInstance constructor.
209 nsRegion ComputePostFilterDirtyRegion();
212 * Sets the aPostFilterExtents outparam to the post-filter bounds in frame
213 * space for the whole filter output. This is not necessarily equivalent to
214 * the area that would be dirtied in the result when the entire pre-filter
215 * area is dirtied, because some filter primitives can generate output
216 * without any input.
218 nsRect ComputePostFilterExtents();
221 * Sets the aDirty outparam to the pre-filter bounds in frame space of the
222 * area of mTargetFrame that is needed in order to paint the filtered output
223 * for a given post-filter dirtied area. The post-filter area must have been
224 * specified before calling this method by passing it as the
225 * aPostFilterDirtyRegion argument to the FilterInstance constructor.
227 nsRect ComputeSourceNeededRect();
229 struct SourceInfo {
230 // Specifies which parts of the source need to be rendered.
231 // Set by ComputeNeededBoxes().
232 nsIntRect mNeededBounds;
234 // The surface that contains the input rendering.
235 // Set by BuildSourceImage / BuildSourcePaint.
236 RefPtr<SourceSurface> mSourceSurface;
238 // The position and size of mSourceSurface in filter space.
239 // Set by BuildSourceImage / BuildSourcePaint.
240 IntRect mSurfaceRect;
244 * Creates a SourceSurface for either the FillPaint or StrokePaint graph
245 * nodes
247 void BuildSourcePaint(SourceInfo* aSource, imgDrawingParams& aImgParams);
250 * Creates a SourceSurface for either the FillPaint and StrokePaint graph
251 * nodes, fills its contents and assigns it to mFillPaint.mSourceSurface and
252 * mStrokePaint.mSourceSurface respectively.
254 void BuildSourcePaints(imgDrawingParams& aImgParams);
257 * Creates the SourceSurface for the SourceGraphic graph node, paints its
258 * contents, and assigns it to mSourceGraphic.mSourceSurface.
260 void BuildSourceImage(DrawTarget* aDest, imgDrawingParams& aImgParams,
261 mozilla::gfx::FilterNode* aFilter,
262 mozilla::gfx::FilterNode* aSource,
263 const mozilla::gfx::Rect& aSourceRect);
266 * Build the list of FilterPrimitiveDescriptions that describes the filter's
267 * filter primitives and their connections. This populates
268 * mPrimitiveDescriptions and mInputImages. aFilterInputIsTainted describes
269 * whether the SourceGraphic is tainted.
271 nsresult BuildPrimitives(Span<const StyleFilter> aFilterChain,
272 nsIFrame* aTargetFrame, bool aFilterInputIsTainted);
275 * Add to the list of FilterPrimitiveDescriptions for a particular SVG
276 * reference filter or CSS filter. This populates mPrimitiveDescriptions and
277 * mInputImages. aInputIsTainted describes whether the input to aFilter is
278 * tainted.
280 nsresult BuildPrimitivesForFilter(
281 const StyleFilter& aFilter, nsIFrame* aTargetFrame, bool aInputIsTainted,
282 nsTArray<FilterPrimitiveDescription>& aPrimitiveDescriptions);
285 * Computes the filter space bounds of the areas that we actually *need* from
286 * the filter sources, based on the value of mPostFilterDirtyRegion.
287 * This sets mNeededBounds on the corresponding SourceInfo structs.
289 void ComputeNeededBoxes();
292 * Returns the output bounds of the final FilterPrimitiveDescription.
294 nsIntRect OutputFilterSpaceBounds() const;
297 * Compute the scale factors between user space and filter space.
299 bool ComputeUserSpaceToFilterSpaceScale();
302 * Transform a rect between user space and filter space.
304 gfxRect UserSpaceToFilterSpace(const gfxRect& aUserSpace) const;
305 gfxRect FilterSpaceToUserSpace(const gfxRect& aFilterSpaceRect) const;
308 * Converts an nsRect or an nsRegion that is relative to a filtered frame's
309 * origin (i.e. the top-left corner of its border box) into filter space,
310 * rounding out.
311 * Returns the entire filter region if aRect / aRegion is null, or if the
312 * result is too large to be stored in an nsIntRect.
314 nsIntRect FrameSpaceToFilterSpace(const nsRect* aRect) const;
315 nsIntRegion FrameSpaceToFilterSpace(const nsRegion* aRegion) const;
318 * Converts an nsIntRect or an nsIntRegion from filter space into the space
319 * that is relative to a filtered frame's origin (i.e. the top-left corner
320 * of its border box) in app units, rounding out.
322 nsRect FilterSpaceToFrameSpace(const nsIntRect& aRect) const;
323 nsRegion FilterSpaceToFrameSpace(const nsIntRegion& aRegion) const;
326 * Returns the transform from frame space to the coordinate space that
327 * GetCanvasTM transforms to. "Frame space" is the origin of a frame, aka the
328 * top-left corner of its border box, aka the top left corner of its mRect.
330 gfxMatrix GetUserSpaceToFrameSpaceInCSSPxTransform() const;
332 bool ComputeTargetBBoxInFilterSpace();
335 * The frame for the element that is currently being filtered.
337 nsIFrame* mTargetFrame;
340 * The filtered element.
342 nsIContent* mTargetContent;
345 * The user space metrics of the filtered frame.
347 const UserSpaceMetrics& mMetrics;
349 const SVGFilterPaintCallback& mPaintCallback;
352 * The SVG bbox of the element that is being filtered, in user space.
354 gfxRect mTargetBBox;
357 * The SVG bbox of the element that is being filtered, in filter space.
359 nsIntRect mTargetBBoxInFilterSpace;
362 * Transform rects between filter space and frame space in CSS pixels.
364 gfxMatrix mFilterSpaceToFrameSpaceInCSSPxTransform;
365 gfxMatrix mFrameSpaceInCSSPxToFilterSpaceTransform;
368 * The scale factors between user space and filter space.
370 gfx::MatrixScalesDouble mUserSpaceToFilterSpaceScale;
371 gfx::MatrixScalesDouble mFilterSpaceToUserSpaceScale;
374 * Pre-filter paint bounds of the element that is being filtered, in filter
375 * space.
377 nsIntRect mTargetBounds;
380 * The dirty area that needs to be repainted, in filter space.
382 nsIntRegion mPostFilterDirtyRegion;
385 * The pre-filter area of the filtered element that changed, in filter space.
387 nsIntRegion mPreFilterDirtyRegion;
389 SourceInfo mSourceGraphic;
390 SourceInfo mFillPaint;
391 SourceInfo mStrokePaint;
394 * The transform to the SVG user space of mTargetFrame.
396 gfxMatrix mPaintTransform;
398 nsTArray<RefPtr<SourceSurface>> mInputImages;
399 FilterDescription mFilterDescription;
400 bool mInitialized;
403 } // namespace mozilla
405 #endif // LAYOUT_SVG_FILTERINSTANCE_H_