Bug 470455 - test_database_sync_embed_visits.js leaks, r=sdwilsh
[wine-gecko.git] / layout / svg / base / src / nsSVGMaskFrame.cpp
blob0cda9d6d1a83ee4b887d1ed2c22d289bc60f75a0
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
13 * License.
15 * The Original Code is the Mozilla SVG project.
17 * The Initial Developer of the Original Code is IBM Corporation.
18 * Portions created by the Initial Developer are Copyright (C) 2004
19 * the Initial Developer. All Rights Reserved.
21 * Contributor(s):
23 * Alternatively, the contents of this file may be used under the terms of
24 * either the GNU General Public License Version 2 or later (the "GPL"), or
25 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
26 * in which case the provisions of the GPL or the LGPL are applicable instead
27 * of those above. If you wish to allow use of your version of this file only
28 * under the terms of either the GPL or the LGPL, and not to allow others to
29 * use your version of this file under the terms of the MPL, indicate your
30 * decision by deleting the provisions above and replace them with the notice
31 * and other provisions required by the GPL or the LGPL. If you do not delete
32 * the provisions above, a recipient may use your version of this file under
33 * the terms of any one of the MPL, the GPL or the LGPL.
35 * ***** END LICENSE BLOCK ***** */
37 #include "nsIDocument.h"
38 #include "nsSVGMaskFrame.h"
39 #include "nsSVGContainerFrame.h"
40 #include "nsSVGMaskElement.h"
41 #include "nsIDOMSVGMatrix.h"
42 #include "gfxContext.h"
43 #include "nsIDOMSVGRect.h"
44 #include "gfxImageSurface.h"
46 //----------------------------------------------------------------------
47 // Implementation
49 nsIFrame*
50 NS_NewSVGMaskFrame(nsIPresShell* aPresShell, nsIContent* aContent, nsStyleContext* aContext)
52 nsCOMPtr<nsIDOMSVGMaskElement> mask = do_QueryInterface(aContent);
54 if (!mask) {
55 NS_ERROR("Can't create frame! Content is not an SVG mask");
56 return nsnull;
59 return new (aPresShell) nsSVGMaskFrame(aContext);
62 already_AddRefed<gfxPattern>
63 nsSVGMaskFrame::ComputeMaskAlpha(nsSVGRenderState *aContext,
64 nsIFrame* aParent,
65 nsIDOMSVGMatrix* aMatrix,
66 float aOpacity)
68 // If the flag is set when we get here, it means this mask frame
69 // has already been used painting the current mask, and the document
70 // has a mask reference loop.
71 if (mInUse) {
72 NS_WARNING("Mask loop detected!");
73 return nsnull;
75 AutoMaskReferencer maskRef(this);
77 gfxContext *gfx = aContext->GetGfxContext();
79 gfx->PushGroup(gfxASurface::CONTENT_COLOR_ALPHA);
82 nsSVGMaskElement *mask = static_cast<nsSVGMaskElement*>(mContent);
84 PRUint16 units =
85 mask->mEnumAttributes[nsSVGMaskElement::MASKUNITS].GetAnimValue();
86 nsCOMPtr<nsIDOMSVGRect> bbox;
87 if (units == nsIDOMSVGUnitTypes::SVG_UNIT_TYPE_OBJECTBOUNDINGBOX) {
88 bbox = nsSVGUtils::GetBBox(aParent);
89 if (!bbox)
90 return nsnull;
93 gfxRect maskArea = nsSVGUtils::GetRelativeRect(units,
94 &mask->mLengthAttributes[nsSVGMaskElement::X], bbox, aParent);
96 gfx->Save();
97 nsSVGUtils::SetClipRect(gfx, aMatrix, maskArea.X(), maskArea.Y(),
98 maskArea.Width(), maskArea.Height());
101 mMaskParent = aParent;
102 mMaskParentMatrix = aMatrix;
104 for (nsIFrame* kid = mFrames.FirstChild(); kid;
105 kid = kid->GetNextSibling()) {
106 nsSVGUtils::PaintFrameWithEffects(aContext, nsnull, kid);
109 gfxRect clipExtents = gfx->GetClipExtents();
110 gfx->Restore();
112 nsRefPtr<gfxPattern> pattern = gfx->PopGroup();
113 if (!pattern || pattern->CairoStatus())
114 return nsnull;
116 #ifdef DEBUG_tor
117 fprintf(stderr, "clip extent: %f,%f %fx%f\n",
118 clipExtents.X(), clipExtents.Y(),
119 clipExtents.Width(), clipExtents.Height());
120 #endif
122 PRBool resultOverflows;
123 gfxIntSize surfaceSize =
124 nsSVGUtils::ConvertToSurfaceSize(gfxSize(clipExtents.Width(),
125 clipExtents.Height()),
126 &resultOverflows);
128 // 0 disables mask, < 0 is an error
129 if (surfaceSize.width <= 0 || surfaceSize.height <= 0)
130 return nsnull;
132 if (resultOverflows)
133 return nsnull;
135 nsRefPtr<gfxImageSurface> image =
136 new gfxImageSurface(surfaceSize, gfxASurface::ImageFormatARGB32);
137 if (!image || image->CairoStatus())
138 return nsnull;
139 image->SetDeviceOffset(-clipExtents.pos);
141 gfxContext transferCtx(image);
142 transferCtx.SetOperator(gfxContext::OPERATOR_SOURCE);
143 transferCtx.SetPattern(pattern);
144 transferCtx.Paint();
146 PRUint8 *data = image->Data();
147 PRInt32 stride = image->Stride();
149 nsIntRect rect(0, 0, surfaceSize.width, surfaceSize.height);
150 nsSVGUtils::UnPremultiplyImageDataAlpha(data, stride, rect);
151 nsSVGUtils::ConvertImageDataToLinearRGB(data, stride, rect);
153 for (PRInt32 y = 0; y < surfaceSize.height; y++)
154 for (PRInt32 x = 0; x < surfaceSize.width; x++) {
155 PRUint8 *pixel = data + stride * y + 4 * x;
157 /* linearRGB -> intensity */
158 PRUint8 alpha =
159 static_cast<PRUint8>
160 ((pixel[GFX_ARGB32_OFFSET_R] * 0.2125 +
161 pixel[GFX_ARGB32_OFFSET_G] * 0.7154 +
162 pixel[GFX_ARGB32_OFFSET_B] * 0.0721) *
163 (pixel[GFX_ARGB32_OFFSET_A] / 255.0) * aOpacity);
165 memset(pixel, alpha, 4);
168 gfxPattern *retval = new gfxPattern(image);
169 NS_IF_ADDREF(retval);
170 return retval;
173 nsIAtom *
174 nsSVGMaskFrame::GetType() const
176 return nsGkAtoms::svgMaskFrame;
179 already_AddRefed<nsIDOMSVGMatrix>
180 nsSVGMaskFrame::GetCanvasTM()
182 NS_ASSERTION(mMaskParentMatrix, "null parent matrix");
184 nsSVGMaskElement *mask = static_cast<nsSVGMaskElement*>(mContent);
186 return nsSVGUtils::AdjustMatrixForUnits(mMaskParentMatrix,
187 &mask->mEnumAttributes[nsSVGMaskElement::MASKCONTENTUNITS],
188 mMaskParent);