| |
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
| |
2 /* This Source Code Form is subject to the terms of the Mozilla Public |
| |
3 * License, v. 2.0. If a copy of the MPL was not distributed with this |
| |
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
| |
5 |
| |
6 // Main header first: |
| |
7 #include "nsSVGMaskFrame.h" |
| |
8 |
| |
9 // Keep others in (case-insensitive) order: |
| |
10 #include "gfxContext.h" |
| |
11 #include "gfxImageSurface.h" |
| |
12 #include "nsRenderingContext.h" |
| |
13 #include "nsSVGEffects.h" |
| |
14 #include "mozilla/dom/SVGMaskElement.h" |
| |
15 |
| |
16 using namespace mozilla::dom; |
| |
17 |
| |
18 /** |
| |
19 * Byte offsets of channels in a native packed gfxColor or cairo image surface. |
| |
20 */ |
| |
21 #ifdef IS_BIG_ENDIAN |
| |
22 #define GFX_ARGB32_OFFSET_A 0 |
| |
23 #define GFX_ARGB32_OFFSET_R 1 |
| |
24 #define GFX_ARGB32_OFFSET_G 2 |
| |
25 #define GFX_ARGB32_OFFSET_B 3 |
| |
26 #else |
| |
27 #define GFX_ARGB32_OFFSET_A 3 |
| |
28 #define GFX_ARGB32_OFFSET_R 2 |
| |
29 #define GFX_ARGB32_OFFSET_G 1 |
| |
30 #define GFX_ARGB32_OFFSET_B 0 |
| |
31 #endif |
| |
32 |
| |
33 // c = n / 255 |
| |
34 // c <= 0.04045 ? c / 12.92 : pow((c + 0.055) / 1.055, 2.4)) * 255 + 0.5 |
| |
35 static const uint8_t gsRGBToLinearRGBMap[256] = { |
| |
36 0, 0, 0, 0, 0, 0, 0, 1, |
| |
37 1, 1, 1, 1, 1, 1, 1, 1, |
| |
38 1, 1, 2, 2, 2, 2, 2, 2, |
| |
39 2, 2, 3, 3, 3, 3, 3, 3, |
| |
40 4, 4, 4, 4, 4, 5, 5, 5, |
| |
41 5, 6, 6, 6, 6, 7, 7, 7, |
| |
42 8, 8, 8, 8, 9, 9, 9, 10, |
| |
43 10, 10, 11, 11, 12, 12, 12, 13, |
| |
44 13, 13, 14, 14, 15, 15, 16, 16, |
| |
45 17, 17, 17, 18, 18, 19, 19, 20, |
| |
46 20, 21, 22, 22, 23, 23, 24, 24, |
| |
47 25, 25, 26, 27, 27, 28, 29, 29, |
| |
48 30, 30, 31, 32, 32, 33, 34, 35, |
| |
49 35, 36, 37, 37, 38, 39, 40, 41, |
| |
50 41, 42, 43, 44, 45, 45, 46, 47, |
| |
51 48, 49, 50, 51, 51, 52, 53, 54, |
| |
52 55, 56, 57, 58, 59, 60, 61, 62, |
| |
53 63, 64, 65, 66, 67, 68, 69, 70, |
| |
54 71, 72, 73, 74, 76, 77, 78, 79, |
| |
55 80, 81, 82, 84, 85, 86, 87, 88, |
| |
56 90, 91, 92, 93, 95, 96, 97, 99, |
| |
57 100, 101, 103, 104, 105, 107, 108, 109, |
| |
58 111, 112, 114, 115, 116, 118, 119, 121, |
| |
59 122, 124, 125, 127, 128, 130, 131, 133, |
| |
60 134, 136, 138, 139, 141, 142, 144, 146, |
| |
61 147, 149, 151, 152, 154, 156, 157, 159, |
| |
62 161, 163, 164, 166, 168, 170, 171, 173, |
| |
63 175, 177, 179, 181, 183, 184, 186, 188, |
| |
64 190, 192, 194, 196, 198, 200, 202, 204, |
| |
65 206, 208, 210, 212, 214, 216, 218, 220, |
| |
66 222, 224, 226, 229, 231, 233, 235, 237, |
| |
67 239, 242, 244, 246, 248, 250, 253, 255 |
| |
68 }; |
| |
69 |
| |
70 static void |
| |
71 ComputesRGBLuminanceMask(uint8_t *aData, |
| |
72 int32_t aStride, |
| |
73 const nsIntRect &aRect, |
| |
74 float aOpacity) |
| |
75 { |
| |
76 for (int32_t y = aRect.y; y < aRect.YMost(); y++) { |
| |
77 for (int32_t x = aRect.x; x < aRect.XMost(); x++) { |
| |
78 uint8_t *pixel = aData + aStride * y + 4 * x; |
| |
79 uint8_t a = pixel[GFX_ARGB32_OFFSET_A]; |
| |
80 |
| |
81 uint8_t luminance; |
| |
82 if (a) { |
| |
83 /* sRGB -> intensity (unpremultiply cancels out the |
| |
84 * (a/255.0) multiplication with aOpacity */ |
| |
85 luminance = |
| |
86 static_cast<uint8_t> |
| |
87 ((pixel[GFX_ARGB32_OFFSET_R] * 0.2125 + |
| |
88 pixel[GFX_ARGB32_OFFSET_G] * 0.7154 + |
| |
89 pixel[GFX_ARGB32_OFFSET_B] * 0.0721) * |
| |
90 aOpacity); |
| |
91 } else { |
| |
92 luminance = 0; |
| |
93 } |
| |
94 memset(pixel, luminance, 4); |
| |
95 } |
| |
96 } |
| |
97 } |
| |
98 |
| |
99 static void |
| |
100 ComputeLinearRGBLuminanceMask(uint8_t *aData, |
| |
101 int32_t aStride, |
| |
102 const nsIntRect &aRect, |
| |
103 float aOpacity) |
| |
104 { |
| |
105 for (int32_t y = aRect.y; y < aRect.YMost(); y++) { |
| |
106 for (int32_t x = aRect.x; x < aRect.XMost(); x++) { |
| |
107 uint8_t *pixel = aData + aStride * y + 4 * x; |
| |
108 uint8_t a = pixel[GFX_ARGB32_OFFSET_A]; |
| |
109 |
| |
110 uint8_t luminance; |
| |
111 // unpremultiply |
| |
112 if (a) { |
| |
113 if (a != 255) { |
| |
114 pixel[GFX_ARGB32_OFFSET_B] = |
| |
115 (255 * pixel[GFX_ARGB32_OFFSET_B]) / a; |
| |
116 pixel[GFX_ARGB32_OFFSET_G] = |
| |
117 (255 * pixel[GFX_ARGB32_OFFSET_G]) / a; |
| |
118 pixel[GFX_ARGB32_OFFSET_R] = |
| |
119 (255 * pixel[GFX_ARGB32_OFFSET_R]) / a; |
| |
120 } |
| |
121 |
| |
122 /* sRGB -> linearRGB -> intensity */ |
| |
123 luminance = |
| |
124 static_cast<uint8_t> |
| |
125 ((gsRGBToLinearRGBMap[pixel[GFX_ARGB32_OFFSET_R]] * |
| |
126 0.2125 + |
| |
127 gsRGBToLinearRGBMap[pixel[GFX_ARGB32_OFFSET_G]] * |
| |
128 0.7154 + |
| |
129 gsRGBToLinearRGBMap[pixel[GFX_ARGB32_OFFSET_B]] * |
| |
130 0.0721) * (a / 255.0) * aOpacity); |
| |
131 } else { |
| |
132 luminance = 0; |
| |
133 } |
| |
134 memset(pixel, luminance, 4); |
| |
135 } |
| |
136 } |
| |
137 } |
| |
138 |
| |
139 static void |
| |
140 ComputeAlphaMask(uint8_t *aData, |
| |
141 int32_t aStride, |
| |
142 const nsIntRect &aRect, |
| |
143 float aOpacity) |
| |
144 { |
| |
145 for (int32_t y = aRect.y; y < aRect.YMost(); y++) { |
| |
146 for (int32_t x = aRect.x; x < aRect.XMost(); x++) { |
| |
147 uint8_t *pixel = aData + aStride * y + 4 * x; |
| |
148 uint8_t luminance = pixel[GFX_ARGB32_OFFSET_A] * aOpacity; |
| |
149 memset(pixel, luminance, 4); |
| |
150 } |
| |
151 } |
| |
152 } |
| |
153 |
| |
154 //---------------------------------------------------------------------- |
| |
155 // Implementation |
| |
156 |
| |
157 nsIFrame* |
| |
158 NS_NewSVGMaskFrame(nsIPresShell* aPresShell, nsStyleContext* aContext) |
| |
159 { |
| |
160 return new (aPresShell) nsSVGMaskFrame(aContext); |
| |
161 } |
| |
162 |
| |
163 NS_IMPL_FRAMEARENA_HELPERS(nsSVGMaskFrame) |
| |
164 |
| |
165 already_AddRefed<gfxPattern> |
| |
166 nsSVGMaskFrame::ComputeMaskAlpha(nsRenderingContext *aContext, |
| |
167 nsIFrame* aParent, |
| |
168 const gfxMatrix &aMatrix, |
| |
169 float aOpacity) |
| |
170 { |
| |
171 // If the flag is set when we get here, it means this mask frame |
| |
172 // has already been used painting the current mask, and the document |
| |
173 // has a mask reference loop. |
| |
174 if (mInUse) { |
| |
175 NS_WARNING("Mask loop detected!"); |
| |
176 return nullptr; |
| |
177 } |
| |
178 AutoMaskReferencer maskRef(this); |
| |
179 |
| |
180 SVGMaskElement *mask = static_cast<SVGMaskElement*>(mContent); |
| |
181 |
| |
182 uint16_t units = |
| |
183 mask->mEnumAttributes[SVGMaskElement::MASKUNITS].GetAnimValue(); |
| |
184 gfxRect bbox; |
| |
185 if (units == SVG_UNIT_TYPE_OBJECTBOUNDINGBOX) { |
| |
186 bbox = nsSVGUtils::GetBBox(aParent); |
| |
187 } |
| |
188 |
| |
189 gfxRect maskArea = nsSVGUtils::GetRelativeRect(units, |
| |
190 &mask->mLengthAttributes[SVGMaskElement::ATTR_X], bbox, aParent); |
| |
191 |
| |
192 gfxContext *gfx = aContext->ThebesContext(); |
| |
193 |
| |
194 // Get the clip extents in device space: |
| |
195 gfx->Save(); |
| |
196 nsSVGUtils::SetClipRect(gfx, aMatrix, maskArea); |
| |
197 gfx->IdentityMatrix(); |
| |
198 gfxRect clipExtents = gfx->GetClipExtents(); |
| |
199 clipExtents.RoundOut(); |
| |
200 gfx->Restore(); |
| |
201 |
| |
202 bool resultOverflows; |
| |
203 gfxIntSize surfaceSize = |
| |
204 nsSVGUtils::ConvertToSurfaceSize(gfxSize(clipExtents.Width(), |
| |
205 clipExtents.Height()), |
| |
206 &resultOverflows); |
| |
207 |
| |
208 // 0 disables mask, < 0 is an error |
| |
209 if (surfaceSize.width <= 0 || surfaceSize.height <= 0) |
| |
210 return nullptr; |
| |
211 |
| |
212 if (resultOverflows) |
| |
213 return nullptr; |
| |
214 |
| |
215 nsRefPtr<gfxImageSurface> image = |
| |
216 new gfxImageSurface(surfaceSize, gfxImageFormat::ARGB32); |
| |
217 if (!image || image->CairoStatus()) |
| |
218 return nullptr; |
| |
219 |
| |
220 // We would like to use gfxImageSurface::SetDeviceOffset() to position |
| |
221 // 'image'. However, we need to set the same matrix on the temporary context |
| |
222 // and pattern that we create below as is currently set on 'gfx'. |
| |
223 // Unfortunately, any device offset set by SetDeviceOffset() is affected by |
| |
224 // the transform passed to the SetMatrix() calls, so to avoid that we account |
| |
225 // for the device offset in the transform rather than use SetDeviceOffset(). |
| |
226 gfxMatrix matrix = |
| |
227 gfx->CurrentMatrix() * gfxMatrix().Translate(-clipExtents.TopLeft()); |
| |
228 |
| |
229 nsRefPtr<nsRenderingContext> tmpCtx(new nsRenderingContext); |
| |
230 tmpCtx->Init(this->PresContext()->DeviceContext(), image); |
| |
231 tmpCtx->ThebesContext()->SetMatrix(matrix); |
| |
232 |
| |
233 mMaskParent = aParent; |
| |
234 if (mMaskParentMatrix) { |
| |
235 *mMaskParentMatrix = aMatrix; |
| |
236 } else { |
| |
237 mMaskParentMatrix = new gfxMatrix(aMatrix); |
| |
238 } |
| |
239 |
| |
240 for (nsIFrame* kid = mFrames.FirstChild(); kid; |
| |
241 kid = kid->GetNextSibling()) { |
| |
242 // The CTM of each frame referencing us can be different |
| |
243 nsISVGChildFrame* SVGFrame = do_QueryFrame(kid); |
| |
244 if (SVGFrame) { |
| |
245 SVGFrame->NotifySVGChanged(nsISVGChildFrame::TRANSFORM_CHANGED); |
| |
246 } |
| |
247 nsSVGUtils::PaintFrameWithEffects(tmpCtx, nullptr, kid); |
| |
248 } |
| |
249 |
| |
250 uint8_t *data = image->Data(); |
| |
251 int32_t stride = image->Stride(); |
| |
252 nsIntRect rect(0, 0, surfaceSize.width, surfaceSize.height); |
| |
253 |
| |
254 if (StyleSVGReset()->mMaskType == NS_STYLE_MASK_TYPE_LUMINANCE) { |
| |
255 if (StyleSVG()->mColorInterpolation == |
| |
256 NS_STYLE_COLOR_INTERPOLATION_LINEARRGB) { |
| |
257 ComputeLinearRGBLuminanceMask(data, stride, rect, aOpacity); |
| |
258 } else { |
| |
259 ComputesRGBLuminanceMask(data, stride, rect, aOpacity); |
| |
260 } |
| |
261 } else { |
| |
262 ComputeAlphaMask(data, stride, rect, aOpacity); |
| |
263 } |
| |
264 |
| |
265 nsRefPtr<gfxPattern> retval = new gfxPattern(image); |
| |
266 retval->SetMatrix(matrix); |
| |
267 return retval.forget(); |
| |
268 } |
| |
269 |
| |
270 nsresult |
| |
271 nsSVGMaskFrame::AttributeChanged(int32_t aNameSpaceID, |
| |
272 nsIAtom* aAttribute, |
| |
273 int32_t aModType) |
| |
274 { |
| |
275 if (aNameSpaceID == kNameSpaceID_None && |
| |
276 (aAttribute == nsGkAtoms::x || |
| |
277 aAttribute == nsGkAtoms::y || |
| |
278 aAttribute == nsGkAtoms::width || |
| |
279 aAttribute == nsGkAtoms::height|| |
| |
280 aAttribute == nsGkAtoms::maskUnits || |
| |
281 aAttribute == nsGkAtoms::maskContentUnits)) { |
| |
282 nsSVGEffects::InvalidateDirectRenderingObservers(this); |
| |
283 } |
| |
284 |
| |
285 return nsSVGMaskFrameBase::AttributeChanged(aNameSpaceID, |
| |
286 aAttribute, aModType); |
| |
287 } |
| |
288 |
| |
289 #ifdef DEBUG |
| |
290 void |
| |
291 nsSVGMaskFrame::Init(nsIContent* aContent, |
| |
292 nsIFrame* aParent, |
| |
293 nsIFrame* aPrevInFlow) |
| |
294 { |
| |
295 NS_ASSERTION(aContent->IsSVG(nsGkAtoms::mask), |
| |
296 "Content is not an SVG mask"); |
| |
297 |
| |
298 nsSVGMaskFrameBase::Init(aContent, aParent, aPrevInFlow); |
| |
299 } |
| |
300 #endif /* DEBUG */ |
| |
301 |
| |
302 nsIAtom * |
| |
303 nsSVGMaskFrame::GetType() const |
| |
304 { |
| |
305 return nsGkAtoms::svgMaskFrame; |
| |
306 } |
| |
307 |
| |
308 gfxMatrix |
| |
309 nsSVGMaskFrame::GetCanvasTM(uint32_t aFor, nsIFrame* aTransformRoot) |
| |
310 { |
| |
311 NS_ASSERTION(mMaskParentMatrix, "null parent matrix"); |
| |
312 |
| |
313 SVGMaskElement *mask = static_cast<SVGMaskElement*>(mContent); |
| |
314 |
| |
315 return nsSVGUtils::AdjustMatrixForUnits( |
| |
316 mMaskParentMatrix ? *mMaskParentMatrix : gfxMatrix(), |
| |
317 &mask->mEnumAttributes[SVGMaskElement::MASKCONTENTUNITS], |
| |
318 mMaskParent); |
| |
319 } |
| |
320 |