Bug 1732219 - Add API for fetching the preview image. r=geckoview-reviewers,agi,mconley
[gecko.git] / gfx / src / FilterSupport.h
blob94c535ab0fdb6e9174cd6b56c6da27f356c7d674
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 __FilterSupport_h
8 #define __FilterSupport_h
10 #include "mozilla/gfx/2D.h"
11 #include "mozilla/gfx/Matrix.h"
12 #include "mozilla/gfx/Point.h"
13 #include "mozilla/gfx/Rect.h"
14 #include "nsRegion.h"
15 #include "nsTArray.h"
17 namespace mozilla {
18 namespace gfx {
19 class FilterPrimitiveDescription;
20 class FilterNode;
21 struct FilterDescription;
22 } // namespace gfx
23 } // namespace mozilla
25 extern const float gsRGBToLinearRGBMap[256];
27 namespace mozilla {
28 namespace gfx {
29 namespace FilterWrappers {
30 extern already_AddRefed<FilterNode> Clear(DrawTarget* aDT);
31 extern already_AddRefed<FilterNode> ForSurface(
32 DrawTarget* aDT, SourceSurface* aSurface, const IntPoint& aSurfacePosition);
33 } // namespace FilterWrappers
35 // Morphology Operators
36 const unsigned short SVG_OPERATOR_UNKNOWN = 0;
37 const unsigned short SVG_OPERATOR_ERODE = 1;
38 const unsigned short SVG_OPERATOR_DILATE = 2;
40 // ColorMatrix types
41 const unsigned short SVG_FECOLORMATRIX_TYPE_UNKNOWN = 0;
42 const unsigned short SVG_FECOLORMATRIX_TYPE_MATRIX = 1;
43 const unsigned short SVG_FECOLORMATRIX_TYPE_SATURATE = 2;
44 const unsigned short SVG_FECOLORMATRIX_TYPE_HUE_ROTATE = 3;
45 const unsigned short SVG_FECOLORMATRIX_TYPE_LUMINANCE_TO_ALPHA = 4;
46 // ColorMatrix types for CSS filters
47 const unsigned short SVG_FECOLORMATRIX_TYPE_SEPIA = 5;
49 // ComponentTransfer types
50 const unsigned short SVG_FECOMPONENTTRANSFER_TYPE_UNKNOWN = 0;
51 const unsigned short SVG_FECOMPONENTTRANSFER_TYPE_IDENTITY = 1;
52 const unsigned short SVG_FECOMPONENTTRANSFER_TYPE_TABLE = 2;
53 const unsigned short SVG_FECOMPONENTTRANSFER_TYPE_DISCRETE = 3;
54 const unsigned short SVG_FECOMPONENTTRANSFER_TYPE_LINEAR = 4;
55 const unsigned short SVG_FECOMPONENTTRANSFER_TYPE_GAMMA = 5;
56 const unsigned short SVG_FECOMPONENTTRANSFER_SAME_AS_R = 6;
58 // Blend Mode Values
59 const unsigned short SVG_FEBLEND_MODE_UNKNOWN = 0;
60 const unsigned short SVG_FEBLEND_MODE_NORMAL = 1;
61 const unsigned short SVG_FEBLEND_MODE_MULTIPLY = 2;
62 const unsigned short SVG_FEBLEND_MODE_SCREEN = 3;
63 const unsigned short SVG_FEBLEND_MODE_DARKEN = 4;
64 const unsigned short SVG_FEBLEND_MODE_LIGHTEN = 5;
65 const unsigned short SVG_FEBLEND_MODE_OVERLAY = 6;
66 const unsigned short SVG_FEBLEND_MODE_COLOR_DODGE = 7;
67 const unsigned short SVG_FEBLEND_MODE_COLOR_BURN = 8;
68 const unsigned short SVG_FEBLEND_MODE_HARD_LIGHT = 9;
69 const unsigned short SVG_FEBLEND_MODE_SOFT_LIGHT = 10;
70 const unsigned short SVG_FEBLEND_MODE_DIFFERENCE = 11;
71 const unsigned short SVG_FEBLEND_MODE_EXCLUSION = 12;
72 const unsigned short SVG_FEBLEND_MODE_HUE = 13;
73 const unsigned short SVG_FEBLEND_MODE_SATURATION = 14;
74 const unsigned short SVG_FEBLEND_MODE_COLOR = 15;
75 const unsigned short SVG_FEBLEND_MODE_LUMINOSITY = 16;
77 // Edge Mode Values
78 const unsigned short SVG_EDGEMODE_UNKNOWN = 0;
79 const unsigned short SVG_EDGEMODE_DUPLICATE = 1;
80 const unsigned short SVG_EDGEMODE_WRAP = 2;
81 const unsigned short SVG_EDGEMODE_NONE = 3;
83 // Channel Selectors
84 const unsigned short SVG_CHANNEL_UNKNOWN = 0;
85 const unsigned short SVG_CHANNEL_R = 1;
86 const unsigned short SVG_CHANNEL_G = 2;
87 const unsigned short SVG_CHANNEL_B = 3;
88 const unsigned short SVG_CHANNEL_A = 4;
90 // Turbulence Types
91 const unsigned short SVG_TURBULENCE_TYPE_UNKNOWN = 0;
92 const unsigned short SVG_TURBULENCE_TYPE_FRACTALNOISE = 1;
93 const unsigned short SVG_TURBULENCE_TYPE_TURBULENCE = 2;
95 // Composite Operators
96 const unsigned short SVG_FECOMPOSITE_OPERATOR_UNKNOWN = 0;
97 const unsigned short SVG_FECOMPOSITE_OPERATOR_OVER = 1;
98 const unsigned short SVG_FECOMPOSITE_OPERATOR_IN = 2;
99 const unsigned short SVG_FECOMPOSITE_OPERATOR_OUT = 3;
100 const unsigned short SVG_FECOMPOSITE_OPERATOR_ATOP = 4;
101 const unsigned short SVG_FECOMPOSITE_OPERATOR_XOR = 5;
102 const unsigned short SVG_FECOMPOSITE_OPERATOR_ARITHMETIC = 6;
103 const unsigned short SVG_FECOMPOSITE_OPERATOR_LIGHTER = 7;
105 struct FilterAttribute;
107 // Limits
108 const float kMaxStdDeviation = 500;
110 // Simple PrimitiveAttributes:
112 struct EmptyAttributes {
113 bool operator==(const EmptyAttributes& aOther) const { return true; }
116 struct BlendAttributes {
117 uint32_t mBlendMode;
119 bool operator==(const BlendAttributes& aOther) const {
120 return mBlendMode == aOther.mBlendMode;
124 struct MorphologyAttributes {
125 uint32_t mOperator;
126 Size mRadii;
128 bool operator==(const MorphologyAttributes& aOther) const {
129 return mOperator == aOther.mOperator && mRadii == aOther.mRadii;
133 struct FloodAttributes {
134 sRGBColor mColor;
136 bool operator==(const FloodAttributes& aOther) const {
137 return mColor == aOther.mColor;
141 struct TileAttributes {
142 bool operator==(const TileAttributes& aOther) const { return true; }
145 struct OpacityAttributes {
146 float mOpacity;
148 bool operator==(const OpacityAttributes& aOther) const {
149 return mOpacity == aOther.mOpacity;
153 struct OffsetAttributes {
154 IntPoint mValue;
156 bool operator==(const OffsetAttributes& aOther) const {
157 return mValue == aOther.mValue;
161 struct DisplacementMapAttributes {
162 float mScale;
163 uint32_t mXChannel;
164 uint32_t mYChannel;
166 bool operator==(const DisplacementMapAttributes& aOther) const {
167 return mScale == aOther.mScale && mXChannel == aOther.mXChannel &&
168 mYChannel == aOther.mYChannel;
172 struct TurbulenceAttributes {
173 IntPoint mOffset;
174 Size mBaseFrequency;
175 float mSeed;
176 uint32_t mOctaves;
177 bool mStitchable;
178 uint32_t mType;
180 bool operator==(const TurbulenceAttributes& aOther) const {
181 return mOffset == aOther.mOffset &&
182 mBaseFrequency == aOther.mBaseFrequency && mSeed == aOther.mSeed &&
183 mOctaves == aOther.mOctaves && mStitchable == aOther.mStitchable &&
184 mType == aOther.mType;
188 struct MergeAttributes {
189 bool operator==(const MergeAttributes& aOther) const { return true; }
192 struct ImageAttributes {
193 uint32_t mFilter;
194 uint32_t mInputIndex;
195 Matrix mTransform;
197 bool operator==(const ImageAttributes& aOther) const {
198 return mFilter == aOther.mFilter && mInputIndex == aOther.mInputIndex &&
199 mTransform.ExactlyEquals(aOther.mTransform);
203 struct GaussianBlurAttributes {
204 Size mStdDeviation;
206 bool operator==(const GaussianBlurAttributes& aOther) const {
207 return mStdDeviation == aOther.mStdDeviation;
211 struct DropShadowAttributes {
212 Size mStdDeviation;
213 Point mOffset;
214 sRGBColor mColor;
216 bool operator==(const DropShadowAttributes& aOther) const {
217 return mStdDeviation == aOther.mStdDeviation && mOffset == aOther.mOffset &&
218 mColor == aOther.mColor;
222 struct ToAlphaAttributes {
223 bool operator==(const ToAlphaAttributes& aOther) const { return true; }
226 // Complex PrimitiveAttributes:
228 class ImplicitlyCopyableFloatArray : public CopyableTArray<float> {
229 public:
230 ImplicitlyCopyableFloatArray() = default;
232 ImplicitlyCopyableFloatArray(ImplicitlyCopyableFloatArray&& aOther) = default;
234 ImplicitlyCopyableFloatArray& operator=(
235 ImplicitlyCopyableFloatArray&& aOther) = default;
237 ImplicitlyCopyableFloatArray(const ImplicitlyCopyableFloatArray& aOther) =
238 default;
240 ImplicitlyCopyableFloatArray& operator=(
241 const ImplicitlyCopyableFloatArray& aOther) = default;
244 struct ColorMatrixAttributes {
245 uint32_t mType;
246 ImplicitlyCopyableFloatArray mValues;
248 bool operator==(const ColorMatrixAttributes& aOther) const {
249 return mType == aOther.mType && mValues == aOther.mValues;
253 // If the types for G and B are SVG_FECOMPONENTTRANSFER_SAME_AS_R,
254 // use the R channel values - this lets us avoid copies.
255 const uint32_t kChannelROrRGB = 0;
256 const uint32_t kChannelG = 1;
257 const uint32_t kChannelB = 2;
258 const uint32_t kChannelA = 3;
260 const uint32_t kComponentTransferSlopeIndex = 0;
261 const uint32_t kComponentTransferInterceptIndex = 1;
263 const uint32_t kComponentTransferAmplitudeIndex = 0;
264 const uint32_t kComponentTransferExponentIndex = 1;
265 const uint32_t kComponentTransferOffsetIndex = 2;
267 struct ComponentTransferAttributes {
268 uint8_t mTypes[4];
269 ImplicitlyCopyableFloatArray mValues[4];
271 bool operator==(const ComponentTransferAttributes& aOther) const {
272 return mTypes[0] == aOther.mTypes[0] && mTypes[1] == aOther.mTypes[1] &&
273 mTypes[2] == aOther.mTypes[2] && mTypes[3] == aOther.mTypes[3] &&
274 mValues[0] == aOther.mValues[0] && mValues[1] == aOther.mValues[1] &&
275 mValues[2] == aOther.mValues[2] && mValues[3] == aOther.mValues[3];
279 struct ConvolveMatrixAttributes {
280 IntSize mKernelSize;
281 ImplicitlyCopyableFloatArray mKernelMatrix;
282 float mDivisor;
283 float mBias;
284 IntPoint mTarget;
285 uint32_t mEdgeMode;
286 Size mKernelUnitLength;
287 bool mPreserveAlpha;
289 bool operator==(const ConvolveMatrixAttributes& aOther) const {
290 return mKernelSize == aOther.mKernelSize &&
291 mKernelMatrix == aOther.mKernelMatrix &&
292 mDivisor == aOther.mDivisor && mBias == aOther.mBias &&
293 mTarget == aOther.mTarget && mEdgeMode == aOther.mEdgeMode &&
294 mKernelUnitLength == aOther.mKernelUnitLength &&
295 mPreserveAlpha == aOther.mPreserveAlpha;
299 struct CompositeAttributes {
300 uint32_t mOperator;
301 ImplicitlyCopyableFloatArray mCoefficients;
303 bool operator==(const CompositeAttributes& aOther) const {
304 return mOperator == aOther.mOperator &&
305 mCoefficients == aOther.mCoefficients;
309 enum class LightType {
310 None = 0,
311 Point,
312 Spot,
313 Distant,
314 Max,
317 const uint32_t kDistantLightAzimuthIndex = 0;
318 const uint32_t kDistantLightElevationIndex = 1;
319 const uint32_t kDistantLightNumAttributes = 2;
321 const uint32_t kPointLightPositionXIndex = 0;
322 const uint32_t kPointLightPositionYIndex = 1;
323 const uint32_t kPointLightPositionZIndex = 2;
324 const uint32_t kPointLightNumAttributes = 3;
326 const uint32_t kSpotLightPositionXIndex = 0;
327 const uint32_t kSpotLightPositionYIndex = 1;
328 const uint32_t kSpotLightPositionZIndex = 2;
329 const uint32_t kSpotLightPointsAtXIndex = 3;
330 const uint32_t kSpotLightPointsAtYIndex = 4;
331 const uint32_t kSpotLightPointsAtZIndex = 5;
332 const uint32_t kSpotLightFocusIndex = 6;
333 const uint32_t kSpotLightLimitingConeAngleIndex = 7;
334 const uint32_t kSpotLightNumAttributes = 8;
336 struct DiffuseLightingAttributes {
337 LightType mLightType;
338 ImplicitlyCopyableFloatArray mLightValues;
339 float mSurfaceScale;
340 Size mKernelUnitLength;
341 sRGBColor mColor;
342 float mLightingConstant;
343 float mSpecularExponent;
345 bool operator==(const DiffuseLightingAttributes& aOther) const {
346 return mLightType == aOther.mLightType &&
347 mLightValues == aOther.mLightValues &&
348 mSurfaceScale == aOther.mSurfaceScale &&
349 mKernelUnitLength == aOther.mKernelUnitLength &&
350 mColor == aOther.mColor;
354 struct SpecularLightingAttributes : public DiffuseLightingAttributes {};
356 enum class ColorSpace { SRGB, LinearRGB, Max };
358 enum class AlphaModel { Unpremultiplied, Premultiplied };
360 class ColorModel {
361 public:
362 static ColorModel PremulSRGB() {
363 return ColorModel(ColorSpace::SRGB, AlphaModel::Premultiplied);
366 ColorModel(ColorSpace aColorSpace, AlphaModel aAlphaModel)
367 : mColorSpace(aColorSpace), mAlphaModel(aAlphaModel) {}
368 ColorModel()
369 : mColorSpace(ColorSpace::SRGB), mAlphaModel(AlphaModel::Premultiplied) {}
370 bool operator==(const ColorModel& aOther) const {
371 return mColorSpace == aOther.mColorSpace &&
372 mAlphaModel == aOther.mAlphaModel;
375 // Used to index FilterCachedColorModels::mFilterForColorModel.
376 uint8_t ToIndex() const {
377 return static_cast<uint8_t>(static_cast<uint8_t>(mColorSpace) << 1) |
378 static_cast<uint8_t>(mAlphaModel);
381 ColorSpace mColorSpace;
382 AlphaModel mAlphaModel;
385 already_AddRefed<FilterNode> FilterNodeGraphFromDescription(
386 DrawTarget* aDT, const FilterDescription& aFilter,
387 const Rect& aResultNeededRect, FilterNode* aSourceGraphic,
388 const IntRect& aSourceGraphicRect, FilterNode* aFillPaint,
389 FilterNode* aStrokePaint,
390 nsTArray<RefPtr<SourceSurface>>& aAdditionalImages);
393 * The methods of this class are not on FilterDescription because
394 * FilterDescription is designed as a simple value holder that can be used
395 * on any thread.
397 class FilterSupport {
398 public:
400 * Draw the filter described by aFilter. All rect parameters are in filter
401 * space coordinates. aRenderRect specifies the part of the filter output
402 * that will be drawn at (0, 0) into the draw target aDT, subject to the
403 * current transform on aDT but with no additional scaling.
404 * The source surfaces must match their corresponding rect in size.
405 * aAdditionalImages carries the images that are referenced by the
406 * eImageInputIndex attribute on any image primitives in the filter.
408 static void RenderFilterDescription(
409 DrawTarget* aDT, const FilterDescription& aFilter,
410 const Rect& aRenderRect, SourceSurface* aSourceGraphic,
411 const IntRect& aSourceGraphicRect, SourceSurface* aFillPaint,
412 const IntRect& aFillPaintRect, SourceSurface* aStrokePaint,
413 const IntRect& aStrokePaintRect,
414 nsTArray<RefPtr<SourceSurface>>& aAdditionalImages,
415 const Point& aDestPoint, const DrawOptions& aOptions = DrawOptions());
418 * Computes the region that changes in the filter output due to a change in
419 * input. This is primarily needed when an individual piece of content inside
420 * a filtered container element changes.
422 static nsIntRegion ComputeResultChangeRegion(
423 const FilterDescription& aFilter, const nsIntRegion& aSourceGraphicChange,
424 const nsIntRegion& aFillPaintChange,
425 const nsIntRegion& aStrokePaintChange);
428 * Computes the regions that need to be supplied in the filter inputs when
429 * painting aResultNeededRegion of the filter output.
431 static void ComputeSourceNeededRegions(
432 const FilterDescription& aFilter, const nsIntRegion& aResultNeededRegion,
433 nsIntRegion& aSourceGraphicNeededRegion,
434 nsIntRegion& aFillPaintNeededRegion,
435 nsIntRegion& aStrokePaintNeededRegion);
438 * Computes the size of the filter output.
440 static nsIntRegion ComputePostFilterExtents(
441 const FilterDescription& aFilter,
442 const nsIntRegion& aSourceGraphicExtents);
445 * Computes the size of a single FilterPrimitiveDescription's output given a
446 * set of input extents.
448 static nsIntRegion PostFilterExtentsForPrimitive(
449 const FilterPrimitiveDescription& aDescription,
450 const nsTArray<nsIntRegion>& aInputExtents);
454 * Create a 4x5 color matrix for the different ways to specify color matrices
455 * in SVG.
457 * Return false if the input is invalid or if the resulting matrix is the
458 * identity.
460 bool ComputeColorMatrix(const ColorMatrixAttributes& aMatrixAttributes,
461 float aOutMatrix[20]);
463 } // namespace gfx
464 } // namespace mozilla
466 #endif // __FilterSupport_h