Line data Source code
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/. */
6 :
7 : #ifndef __NS_SVGFILTERSELEMENT_H__
8 : #define __NS_SVGFILTERSELEMENT_H__
9 :
10 : #include "mozilla/Attributes.h"
11 : #include "nsImageLoadingContent.h"
12 : #include "nsSVGLength2.h"
13 : #include "nsSVGString.h"
14 : #include "nsSVGElement.h"
15 : #include "nsSVGNumber2.h"
16 : #include "nsSVGNumberPair.h"
17 : #include "FilterSupport.h"
18 :
19 : class nsSVGFilterInstance;
20 : class nsSVGNumberPair;
21 :
22 : struct nsSVGStringInfo {
23 0 : nsSVGStringInfo(const nsSVGString* aString,
24 0 : nsSVGElement *aElement) :
25 0 : mString(aString), mElement(aElement) {}
26 :
27 : const nsSVGString* mString;
28 : nsSVGElement* mElement;
29 : };
30 :
31 : typedef nsSVGElement nsSVGFEBase;
32 :
33 : #define NS_SVG_FE_CID \
34 : { 0x60483958, 0xd229, 0x4a77, \
35 : { 0x96, 0xb2, 0x62, 0x3e, 0x69, 0x95, 0x1e, 0x0e } }
36 :
37 : /**
38 : * Base class for filter primitive elements
39 : * Children of those elements e.g. feMergeNode
40 : * derive from SVGFEUnstyledElement instead
41 : */
42 : class nsSVGFE : public nsSVGFEBase
43 : {
44 : friend class nsSVGFilterInstance;
45 :
46 : protected:
47 : typedef mozilla::gfx::SourceSurface SourceSurface;
48 : typedef mozilla::gfx::Size Size;
49 : typedef mozilla::gfx::IntRect IntRect;
50 : typedef mozilla::gfx::ColorSpace ColorSpace;
51 : typedef mozilla::gfx::FilterPrimitiveDescription FilterPrimitiveDescription;
52 :
53 0 : explicit nsSVGFE(already_AddRefed<mozilla::dom::NodeInfo>& aNodeInfo)
54 0 : : nsSVGFEBase(aNodeInfo) {}
55 0 : virtual ~nsSVGFE() {}
56 :
57 : public:
58 : typedef mozilla::gfx::AttributeMap AttributeMap;
59 :
60 : ColorSpace
61 0 : GetInputColorSpace(int32_t aInputIndex, ColorSpace aUnchangedInputColorSpace) {
62 0 : return OperatesOnSRGB(aInputIndex, aUnchangedInputColorSpace == ColorSpace::SRGB) ?
63 0 : ColorSpace::SRGB : ColorSpace::LinearRGB;
64 : }
65 :
66 : // This is only called for filter primitives without inputs. For primitives
67 : // with inputs, the output color model is the same as of the first input.
68 : ColorSpace
69 0 : GetOutputColorSpace() {
70 0 : return ProducesSRGB() ? ColorSpace::SRGB : ColorSpace::LinearRGB;
71 : }
72 :
73 : // See http://www.w3.org/TR/SVG/filters.html#FilterPrimitiveSubRegion
74 0 : virtual bool SubregionIsUnionOfRegions() { return true; }
75 :
76 : NS_DECLARE_STATIC_IID_ACCESSOR(NS_SVG_FE_CID)
77 :
78 : // interfaces:
79 : NS_DECL_ISUPPORTS_INHERITED
80 :
81 : // nsIContent interface
82 : NS_IMETHOD_(bool) IsAttributeMapped(const nsIAtom* aAttribute) const override;
83 :
84 : // nsSVGElement interface
85 : virtual nsresult Clone(mozilla::dom::NodeInfo *aNodeInfo, nsINode **aResult,
86 : bool aPreallocateChildren) const override = 0;
87 :
88 : virtual bool HasValidDimensions() const override;
89 :
90 0 : bool IsNodeOfType(uint32_t aFlags) const override
91 0 : { return !(aFlags & ~(eCONTENT | eFILTER)); }
92 :
93 : virtual nsSVGString& 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<nsSVGStringInfo>& aSources);
97 :
98 : virtual FilterPrimitiveDescription
99 : GetPrimitiveDescription(nsSVGFilterInstance* aInstance,
100 : const IntRect& aFilterSubregion,
101 : const nsTArray<bool>& aInputsAreTainted,
102 : nsTArray<RefPtr<SourceSurface>>& aInputImages) = 0;
103 :
104 : // returns true if changes to the attribute should cause us to
105 : // repaint the filter
106 : virtual bool AttributeAffectsRendering(
107 : int32_t aNameSpaceID, nsIAtom* aAttribute) const;
108 :
109 : // Return whether this filter primitive has tainted output. A filter's
110 : // output is tainted if it depends on things that the web page is not
111 : // allowed to read from, e.g. the source graphic or cross-origin images.
112 : // aReferencePrincipal is the node principal of the filtered frame's element.
113 : virtual bool OutputIsTainted(const nsTArray<bool>& aInputsAreTainted,
114 : nsIPrincipal* aReferencePrincipal);
115 :
116 : static nsIntRect GetMaxRect() {
117 : // Try to avoid overflow errors dealing with this rect. It will
118 : // be intersected with some other reasonable-sized rect eventually.
119 : return nsIntRect(INT32_MIN/2, INT32_MIN/2, INT32_MAX, INT32_MAX);
120 : }
121 :
122 : operator nsISupports*() { return static_cast<nsIContent*>(this); }
123 :
124 : // WebIDL
125 : already_AddRefed<mozilla::dom::SVGAnimatedLength> X();
126 : already_AddRefed<mozilla::dom::SVGAnimatedLength> Y();
127 : already_AddRefed<mozilla::dom::SVGAnimatedLength> Width();
128 : already_AddRefed<mozilla::dom::SVGAnimatedLength> Height();
129 : already_AddRefed<mozilla::dom::SVGAnimatedString> Result();
130 :
131 : protected:
132 0 : virtual bool OperatesOnSRGB(int32_t aInputIndex, bool aInputIsAlreadySRGB) {
133 0 : return StyleIsSetToSRGB();
134 : }
135 :
136 : // Only called for filter primitives without inputs.
137 0 : virtual bool ProducesSRGB() { return StyleIsSetToSRGB(); }
138 :
139 : bool StyleIsSetToSRGB();
140 :
141 : // nsSVGElement specializations:
142 : virtual LengthAttributesInfo GetLengthInfo() override;
143 :
144 : Size GetKernelUnitLength(nsSVGFilterInstance* aInstance,
145 : nsSVGNumberPair *aKernelUnitLength);
146 :
147 : enum { ATTR_X, ATTR_Y, ATTR_WIDTH, ATTR_HEIGHT };
148 : nsSVGLength2 mLengthAttributes[4];
149 : static LengthInfo sLengthInfo[4];
150 : };
151 :
152 : NS_DEFINE_STATIC_IID_ACCESSOR(nsSVGFE, NS_SVG_FE_CID)
153 :
154 : typedef nsSVGElement SVGFEUnstyledElementBase;
155 :
156 0 : class SVGFEUnstyledElement : public SVGFEUnstyledElementBase
157 : {
158 : protected:
159 0 : explicit SVGFEUnstyledElement(already_AddRefed<mozilla::dom::NodeInfo>& aNodeInfo)
160 0 : : SVGFEUnstyledElementBase(aNodeInfo) {}
161 :
162 : public:
163 : virtual nsresult Clone(mozilla::dom::NodeInfo *aNodeInfo, nsINode **aResult,
164 : bool aPreallocateChildren) const override = 0;
165 :
166 : // returns true if changes to the attribute should cause us to
167 : // repaint the filter
168 : virtual bool AttributeAffectsRendering(
169 : int32_t aNameSpaceID, nsIAtom* aAttribute) const = 0;
170 : };
171 :
172 : //------------------------------------------------------------
173 :
174 : typedef nsSVGFE nsSVGFELightingElementBase;
175 :
176 : class nsSVGFELightingElement : public nsSVGFELightingElementBase
177 : {
178 : protected:
179 0 : explicit nsSVGFELightingElement(already_AddRefed<mozilla::dom::NodeInfo>& aNodeInfo)
180 0 : : nsSVGFELightingElementBase(aNodeInfo) {}
181 :
182 0 : virtual ~nsSVGFELightingElement() {}
183 :
184 : public:
185 : // interfaces:
186 : NS_DECL_ISUPPORTS_INHERITED
187 :
188 : virtual bool AttributeAffectsRendering(
189 : int32_t aNameSpaceID, nsIAtom* aAttribute) const override;
190 0 : virtual nsSVGString& GetResultImageName() override { return mStringAttributes[RESULT]; }
191 : virtual void GetSourceImageNames(nsTArray<nsSVGStringInfo>& aSources) override;
192 0 : NS_FORWARD_NSIDOMSVGELEMENT(nsSVGFELightingElementBase::)
193 :
194 : NS_IMETHOD_(bool) IsAttributeMapped(const nsIAtom* aAttribute) const override;
195 :
196 : protected:
197 0 : virtual bool OperatesOnSRGB(int32_t aInputIndex,
198 0 : bool aInputIsAlreadySRGB) override { return true; }
199 :
200 : virtual NumberAttributesInfo GetNumberInfo() override;
201 : virtual NumberPairAttributesInfo GetNumberPairInfo() override;
202 : virtual StringAttributesInfo GetStringInfo() override;
203 :
204 : AttributeMap ComputeLightAttributes(nsSVGFilterInstance* aInstance);
205 :
206 : FilterPrimitiveDescription
207 : AddLightingAttributes(const FilterPrimitiveDescription& aDescription,
208 : nsSVGFilterInstance* aInstance);
209 :
210 : enum { SURFACE_SCALE, DIFFUSE_CONSTANT, SPECULAR_CONSTANT, SPECULAR_EXPONENT };
211 : nsSVGNumber2 mNumberAttributes[4];
212 : static NumberInfo sNumberInfo[4];
213 :
214 : enum { KERNEL_UNIT_LENGTH };
215 : nsSVGNumberPair mNumberPairAttributes[1];
216 : static NumberPairInfo sNumberPairInfo[1];
217 :
218 : enum { RESULT, IN1 };
219 : nsSVGString mStringAttributes[2];
220 : static StringInfo sStringInfo[2];
221 : };
222 :
223 : namespace mozilla {
224 : namespace dom {
225 :
226 : typedef SVGFEUnstyledElement SVGFELightElementBase;
227 :
228 0 : class SVGFELightElement : public SVGFELightElementBase
229 : {
230 : protected:
231 0 : explicit SVGFELightElement(already_AddRefed<mozilla::dom::NodeInfo>& aNodeInfo)
232 0 : : SVGFELightElementBase(aNodeInfo) {}
233 :
234 : public:
235 : typedef gfx::AttributeMap AttributeMap;
236 :
237 : virtual AttributeMap
238 : ComputeLightAttributes(nsSVGFilterInstance* aInstance) = 0;
239 : };
240 :
241 : } // namespace dom
242 : } // namespace mozilla
243 :
244 : #endif
|