Bug 1805294 [wpt PR 37463] - WebKit export of https://bugs.webkit.org/show_bug.cgi...
[gecko.git] / dom / svg / SVGFilters.h
blob540281dd9b3c0e3068c5f3476d50878aa8081825
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 DOM_SVG_SVGFILTERS_H_
8 #define DOM_SVG_SVGFILTERS_H_
10 #include "mozilla/Attributes.h"
11 #include "mozilla/dom/SVGElement.h"
12 #include "FilterDescription.h"
13 #include "nsImageLoadingContent.h"
14 #include "SVGAnimatedLength.h"
15 #include "SVGAnimatedNumber.h"
16 #include "SVGAnimatedNumberPair.h"
17 #include "SVGAnimatedString.h"
19 namespace mozilla {
20 class SVGFilterInstance;
22 namespace dom {
24 struct SVGStringInfo {
25 SVGStringInfo(const SVGAnimatedString* aString, SVGElement* aElement)
26 : mString(aString), mElement(aElement) {}
28 const SVGAnimatedString* mString;
29 SVGElement* mElement;
32 using SVGFEBase = SVGElement;
34 #define NS_SVG_FE_CID \
35 { \
36 0x60483958, 0xd229, 0x4a77, { \
37 0x96, 0xb2, 0x62, 0x3e, 0x69, 0x95, 0x1e, 0x0e \
38 } \
41 /**
42 * Base class for filter primitive elements
43 * Children of those elements e.g. feMergeNode
44 * derive from SVGFEUnstyledElement instead
46 class SVGFE : public SVGFEBase {
47 friend class mozilla::SVGFilterInstance;
49 protected:
50 using SourceSurface = mozilla::gfx::SourceSurface;
51 using Size = mozilla::gfx::Size;
52 using IntRect = mozilla::gfx::IntRect;
53 using ColorSpace = mozilla::gfx::ColorSpace;
54 using FilterPrimitiveDescription = mozilla::gfx::FilterPrimitiveDescription;
56 explicit SVGFE(already_AddRefed<mozilla::dom::NodeInfo>&& aNodeInfo)
57 : SVGFEBase(std::move(aNodeInfo)) {}
58 virtual ~SVGFE() = default;
60 public:
61 using PrimitiveAttributes = mozilla::gfx::PrimitiveAttributes;
63 ColorSpace GetInputColorSpace(int32_t aInputIndex,
64 ColorSpace aUnchangedInputColorSpace) {
65 return OperatesOnSRGB(aInputIndex,
66 aUnchangedInputColorSpace == ColorSpace::SRGB)
67 ? ColorSpace::SRGB
68 : ColorSpace::LinearRGB;
71 // This is only called for filter primitives without inputs. For primitives
72 // with inputs, the output color model is the same as of the first input.
73 ColorSpace GetOutputColorSpace() {
74 return ProducesSRGB() ? ColorSpace::SRGB : ColorSpace::LinearRGB;
77 // See http://www.w3.org/TR/SVG/filters.html#FilterPrimitiveSubRegion
78 virtual bool SubregionIsUnionOfRegions() { return true; }
80 NS_DECLARE_STATIC_IID_ACCESSOR(NS_SVG_FE_CID)
82 // interfaces:
83 NS_DECL_ISUPPORTS_INHERITED
85 // nsIContent interface
86 NS_IMETHOD_(bool) IsAttributeMapped(const nsAtom* aAttribute) const override;
88 // SVGElement interface
89 nsresult Clone(mozilla::dom::NodeInfo*, nsINode** aResult) const override = 0;
91 bool HasValidDimensions() const override;
93 virtual SVGAnimatedString& GetResultImageName() = 0;
94 // Return a list of all image names used as sources. Default is to
95 // return no sources.
96 virtual void GetSourceImageNames(nsTArray<SVGStringInfo>& aSources);
98 virtual FilterPrimitiveDescription GetPrimitiveDescription(
99 SVGFilterInstance* aInstance, const IntRect& aFilterSubregion,
100 const nsTArray<bool>& aInputsAreTainted,
101 nsTArray<RefPtr<SourceSurface>>& aInputImages) = 0;
103 // returns true if changes to the attribute should cause us to
104 // repaint the filter
105 virtual bool AttributeAffectsRendering(int32_t aNameSpaceID,
106 nsAtom* aAttribute) const;
108 // Return whether this filter primitive has tainted output. A filter's
109 // output is tainted if it depends on things that the web page is not
110 // allowed to read from, e.g. the source graphic or cross-origin images.
111 // aReferencePrincipal is the node principal of the filtered frame's element.
112 virtual bool OutputIsTainted(const nsTArray<bool>& aInputsAreTainted,
113 nsIPrincipal* aReferencePrincipal);
115 static nsIntRect GetMaxRect() {
116 // Try to avoid overflow errors dealing with this rect. It will
117 // be intersected with some other reasonable-sized rect eventually.
118 return nsIntRect(INT32_MIN / 2, INT32_MIN / 2, INT32_MAX, INT32_MAX);
121 operator nsISupports*() { return static_cast<nsIContent*>(this); }
123 // WebIDL
124 already_AddRefed<DOMSVGAnimatedLength> X();
125 already_AddRefed<DOMSVGAnimatedLength> Y();
126 already_AddRefed<DOMSVGAnimatedLength> Width();
127 already_AddRefed<DOMSVGAnimatedLength> Height();
128 already_AddRefed<DOMSVGAnimatedString> Result();
130 protected:
131 virtual bool OperatesOnSRGB(int32_t aInputIndex, bool aInputIsAlreadySRGB) {
132 return StyleIsSetToSRGB();
135 // Only called for filter primitives without inputs.
136 virtual bool ProducesSRGB() { return StyleIsSetToSRGB(); }
138 bool StyleIsSetToSRGB();
140 // SVGElement specializations:
141 LengthAttributesInfo GetLengthInfo() override;
143 Size GetKernelUnitLength(SVGFilterInstance* aInstance,
144 SVGAnimatedNumberPair* aKernelUnitLength);
146 enum { ATTR_X, ATTR_Y, ATTR_WIDTH, ATTR_HEIGHT };
147 SVGAnimatedLength mLengthAttributes[4];
148 static LengthInfo sLengthInfo[4];
151 NS_DEFINE_STATIC_IID_ACCESSOR(SVGFE, NS_SVG_FE_CID)
153 using SVGFEUnstyledElementBase = SVGElement;
155 class SVGFEUnstyledElement : public SVGFEUnstyledElementBase {
156 protected:
157 explicit SVGFEUnstyledElement(
158 already_AddRefed<mozilla::dom::NodeInfo>&& aNodeInfo)
159 : SVGFEUnstyledElementBase(std::move(aNodeInfo)) {}
161 public:
162 nsresult Clone(mozilla::dom::NodeInfo*, nsINode** aResult) const override = 0;
164 // returns true if changes to the attribute should cause us to
165 // repaint the filter
166 virtual bool AttributeAffectsRendering(int32_t aNameSpaceID,
167 nsAtom* aAttribute) const = 0;
170 //------------------------------------------------------------
172 using SVGFELightingElementBase = SVGFE;
174 class SVGFELightingElement : public SVGFELightingElementBase {
175 protected:
176 explicit SVGFELightingElement(
177 already_AddRefed<mozilla::dom::NodeInfo>&& aNodeInfo)
178 : SVGFELightingElementBase(std::move(aNodeInfo)) {}
180 virtual ~SVGFELightingElement() = default;
182 public:
183 // interfaces:
184 NS_INLINE_DECL_REFCOUNTING_INHERITED(SVGFELightingElement,
185 SVGFELightingElementBase)
187 bool AttributeAffectsRendering(int32_t aNameSpaceID,
188 nsAtom* aAttribute) const override;
189 SVGAnimatedString& GetResultImageName() override {
190 return mStringAttributes[RESULT];
192 void GetSourceImageNames(nsTArray<SVGStringInfo>& aSources) override;
193 NS_IMETHOD_(bool) IsAttributeMapped(const nsAtom* aAttribute) const override;
195 protected:
196 bool OperatesOnSRGB(int32_t aInputIndex, bool aInputIsAlreadySRGB) override {
197 return true;
200 NumberAttributesInfo GetNumberInfo() override;
201 NumberPairAttributesInfo GetNumberPairInfo() override;
202 StringAttributesInfo GetStringInfo() override;
204 mozilla::gfx::LightType ComputeLightAttributes(
205 SVGFilterInstance* aInstance, nsTArray<float>& aFloatAttributes);
207 bool AddLightingAttributes(
208 mozilla::gfx::DiffuseLightingAttributes* aAttributes,
209 SVGFilterInstance* aInstance);
211 enum {
212 SURFACE_SCALE,
213 DIFFUSE_CONSTANT,
214 SPECULAR_CONSTANT,
215 SPECULAR_EXPONENT
217 SVGAnimatedNumber mNumberAttributes[4];
218 static NumberInfo sNumberInfo[4];
220 enum { KERNEL_UNIT_LENGTH };
221 SVGAnimatedNumberPair mNumberPairAttributes[1];
222 static NumberPairInfo sNumberPairInfo[1];
224 enum { RESULT, IN1 };
225 SVGAnimatedString mStringAttributes[2];
226 static StringInfo sStringInfo[2];
229 using SVGFELightElementBase = SVGFEUnstyledElement;
231 class SVGFELightElement : public SVGFELightElementBase {
232 protected:
233 explicit SVGFELightElement(
234 already_AddRefed<mozilla::dom::NodeInfo>&& aNodeInfo)
235 : SVGFELightElementBase(std::move(aNodeInfo)) {}
237 public:
238 using PrimitiveAttributes = gfx::PrimitiveAttributes;
240 virtual mozilla::gfx::LightType ComputeLightAttributes(
241 SVGFilterInstance* aInstance, nsTArray<float>& aFloatAttributes) = 0;
244 } // namespace dom
245 } // namespace mozilla
247 #endif // DOM_SVG_SVGFILTERS_H_