michael@0: /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: // Main header first: michael@0: #include "nsSVGMaskFrame.h" michael@0: michael@0: // Keep others in (case-insensitive) order: michael@0: #include "gfxContext.h" michael@0: #include "gfxImageSurface.h" michael@0: #include "nsRenderingContext.h" michael@0: #include "nsSVGEffects.h" michael@0: #include "mozilla/dom/SVGMaskElement.h" michael@0: michael@0: using namespace mozilla::dom; michael@0: michael@0: /** michael@0: * Byte offsets of channels in a native packed gfxColor or cairo image surface. michael@0: */ michael@0: #ifdef IS_BIG_ENDIAN michael@0: #define GFX_ARGB32_OFFSET_A 0 michael@0: #define GFX_ARGB32_OFFSET_R 1 michael@0: #define GFX_ARGB32_OFFSET_G 2 michael@0: #define GFX_ARGB32_OFFSET_B 3 michael@0: #else michael@0: #define GFX_ARGB32_OFFSET_A 3 michael@0: #define GFX_ARGB32_OFFSET_R 2 michael@0: #define GFX_ARGB32_OFFSET_G 1 michael@0: #define GFX_ARGB32_OFFSET_B 0 michael@0: #endif michael@0: michael@0: // c = n / 255 michael@0: // c <= 0.04045 ? c / 12.92 : pow((c + 0.055) / 1.055, 2.4)) * 255 + 0.5 michael@0: static const uint8_t gsRGBToLinearRGBMap[256] = { michael@0: 0, 0, 0, 0, 0, 0, 0, 1, michael@0: 1, 1, 1, 1, 1, 1, 1, 1, michael@0: 1, 1, 2, 2, 2, 2, 2, 2, michael@0: 2, 2, 3, 3, 3, 3, 3, 3, michael@0: 4, 4, 4, 4, 4, 5, 5, 5, michael@0: 5, 6, 6, 6, 6, 7, 7, 7, michael@0: 8, 8, 8, 8, 9, 9, 9, 10, michael@0: 10, 10, 11, 11, 12, 12, 12, 13, michael@0: 13, 13, 14, 14, 15, 15, 16, 16, michael@0: 17, 17, 17, 18, 18, 19, 19, 20, michael@0: 20, 21, 22, 22, 23, 23, 24, 24, michael@0: 25, 25, 26, 27, 27, 28, 29, 29, michael@0: 30, 30, 31, 32, 32, 33, 34, 35, michael@0: 35, 36, 37, 37, 38, 39, 40, 41, michael@0: 41, 42, 43, 44, 45, 45, 46, 47, michael@0: 48, 49, 50, 51, 51, 52, 53, 54, michael@0: 55, 56, 57, 58, 59, 60, 61, 62, michael@0: 63, 64, 65, 66, 67, 68, 69, 70, michael@0: 71, 72, 73, 74, 76, 77, 78, 79, michael@0: 80, 81, 82, 84, 85, 86, 87, 88, michael@0: 90, 91, 92, 93, 95, 96, 97, 99, michael@0: 100, 101, 103, 104, 105, 107, 108, 109, michael@0: 111, 112, 114, 115, 116, 118, 119, 121, michael@0: 122, 124, 125, 127, 128, 130, 131, 133, michael@0: 134, 136, 138, 139, 141, 142, 144, 146, michael@0: 147, 149, 151, 152, 154, 156, 157, 159, michael@0: 161, 163, 164, 166, 168, 170, 171, 173, michael@0: 175, 177, 179, 181, 183, 184, 186, 188, michael@0: 190, 192, 194, 196, 198, 200, 202, 204, michael@0: 206, 208, 210, 212, 214, 216, 218, 220, michael@0: 222, 224, 226, 229, 231, 233, 235, 237, michael@0: 239, 242, 244, 246, 248, 250, 253, 255 michael@0: }; michael@0: michael@0: static void michael@0: ComputesRGBLuminanceMask(uint8_t *aData, michael@0: int32_t aStride, michael@0: const nsIntRect &aRect, michael@0: float aOpacity) michael@0: { michael@0: for (int32_t y = aRect.y; y < aRect.YMost(); y++) { michael@0: for (int32_t x = aRect.x; x < aRect.XMost(); x++) { michael@0: uint8_t *pixel = aData + aStride * y + 4 * x; michael@0: uint8_t a = pixel[GFX_ARGB32_OFFSET_A]; michael@0: michael@0: uint8_t luminance; michael@0: if (a) { michael@0: /* sRGB -> intensity (unpremultiply cancels out the michael@0: * (a/255.0) multiplication with aOpacity */ michael@0: luminance = michael@0: static_cast michael@0: ((pixel[GFX_ARGB32_OFFSET_R] * 0.2125 + michael@0: pixel[GFX_ARGB32_OFFSET_G] * 0.7154 + michael@0: pixel[GFX_ARGB32_OFFSET_B] * 0.0721) * michael@0: aOpacity); michael@0: } else { michael@0: luminance = 0; michael@0: } michael@0: memset(pixel, luminance, 4); michael@0: } michael@0: } michael@0: } michael@0: michael@0: static void michael@0: ComputeLinearRGBLuminanceMask(uint8_t *aData, michael@0: int32_t aStride, michael@0: const nsIntRect &aRect, michael@0: float aOpacity) michael@0: { michael@0: for (int32_t y = aRect.y; y < aRect.YMost(); y++) { michael@0: for (int32_t x = aRect.x; x < aRect.XMost(); x++) { michael@0: uint8_t *pixel = aData + aStride * y + 4 * x; michael@0: uint8_t a = pixel[GFX_ARGB32_OFFSET_A]; michael@0: michael@0: uint8_t luminance; michael@0: // unpremultiply michael@0: if (a) { michael@0: if (a != 255) { michael@0: pixel[GFX_ARGB32_OFFSET_B] = michael@0: (255 * pixel[GFX_ARGB32_OFFSET_B]) / a; michael@0: pixel[GFX_ARGB32_OFFSET_G] = michael@0: (255 * pixel[GFX_ARGB32_OFFSET_G]) / a; michael@0: pixel[GFX_ARGB32_OFFSET_R] = michael@0: (255 * pixel[GFX_ARGB32_OFFSET_R]) / a; michael@0: } michael@0: michael@0: /* sRGB -> linearRGB -> intensity */ michael@0: luminance = michael@0: static_cast michael@0: ((gsRGBToLinearRGBMap[pixel[GFX_ARGB32_OFFSET_R]] * michael@0: 0.2125 + michael@0: gsRGBToLinearRGBMap[pixel[GFX_ARGB32_OFFSET_G]] * michael@0: 0.7154 + michael@0: gsRGBToLinearRGBMap[pixel[GFX_ARGB32_OFFSET_B]] * michael@0: 0.0721) * (a / 255.0) * aOpacity); michael@0: } else { michael@0: luminance = 0; michael@0: } michael@0: memset(pixel, luminance, 4); michael@0: } michael@0: } michael@0: } michael@0: michael@0: static void michael@0: ComputeAlphaMask(uint8_t *aData, michael@0: int32_t aStride, michael@0: const nsIntRect &aRect, michael@0: float aOpacity) michael@0: { michael@0: for (int32_t y = aRect.y; y < aRect.YMost(); y++) { michael@0: for (int32_t x = aRect.x; x < aRect.XMost(); x++) { michael@0: uint8_t *pixel = aData + aStride * y + 4 * x; michael@0: uint8_t luminance = pixel[GFX_ARGB32_OFFSET_A] * aOpacity; michael@0: memset(pixel, luminance, 4); michael@0: } michael@0: } michael@0: } michael@0: michael@0: //---------------------------------------------------------------------- michael@0: // Implementation michael@0: michael@0: nsIFrame* michael@0: NS_NewSVGMaskFrame(nsIPresShell* aPresShell, nsStyleContext* aContext) michael@0: { michael@0: return new (aPresShell) nsSVGMaskFrame(aContext); michael@0: } michael@0: michael@0: NS_IMPL_FRAMEARENA_HELPERS(nsSVGMaskFrame) michael@0: michael@0: already_AddRefed michael@0: nsSVGMaskFrame::ComputeMaskAlpha(nsRenderingContext *aContext, michael@0: nsIFrame* aParent, michael@0: const gfxMatrix &aMatrix, michael@0: float aOpacity) michael@0: { michael@0: // If the flag is set when we get here, it means this mask frame michael@0: // has already been used painting the current mask, and the document michael@0: // has a mask reference loop. michael@0: if (mInUse) { michael@0: NS_WARNING("Mask loop detected!"); michael@0: return nullptr; michael@0: } michael@0: AutoMaskReferencer maskRef(this); michael@0: michael@0: SVGMaskElement *mask = static_cast(mContent); michael@0: michael@0: uint16_t units = michael@0: mask->mEnumAttributes[SVGMaskElement::MASKUNITS].GetAnimValue(); michael@0: gfxRect bbox; michael@0: if (units == SVG_UNIT_TYPE_OBJECTBOUNDINGBOX) { michael@0: bbox = nsSVGUtils::GetBBox(aParent); michael@0: } michael@0: michael@0: gfxRect maskArea = nsSVGUtils::GetRelativeRect(units, michael@0: &mask->mLengthAttributes[SVGMaskElement::ATTR_X], bbox, aParent); michael@0: michael@0: gfxContext *gfx = aContext->ThebesContext(); michael@0: michael@0: // Get the clip extents in device space: michael@0: gfx->Save(); michael@0: nsSVGUtils::SetClipRect(gfx, aMatrix, maskArea); michael@0: gfx->IdentityMatrix(); michael@0: gfxRect clipExtents = gfx->GetClipExtents(); michael@0: clipExtents.RoundOut(); michael@0: gfx->Restore(); michael@0: michael@0: bool resultOverflows; michael@0: gfxIntSize surfaceSize = michael@0: nsSVGUtils::ConvertToSurfaceSize(gfxSize(clipExtents.Width(), michael@0: clipExtents.Height()), michael@0: &resultOverflows); michael@0: michael@0: // 0 disables mask, < 0 is an error michael@0: if (surfaceSize.width <= 0 || surfaceSize.height <= 0) michael@0: return nullptr; michael@0: michael@0: if (resultOverflows) michael@0: return nullptr; michael@0: michael@0: nsRefPtr image = michael@0: new gfxImageSurface(surfaceSize, gfxImageFormat::ARGB32); michael@0: if (!image || image->CairoStatus()) michael@0: return nullptr; michael@0: michael@0: // We would like to use gfxImageSurface::SetDeviceOffset() to position michael@0: // 'image'. However, we need to set the same matrix on the temporary context michael@0: // and pattern that we create below as is currently set on 'gfx'. michael@0: // Unfortunately, any device offset set by SetDeviceOffset() is affected by michael@0: // the transform passed to the SetMatrix() calls, so to avoid that we account michael@0: // for the device offset in the transform rather than use SetDeviceOffset(). michael@0: gfxMatrix matrix = michael@0: gfx->CurrentMatrix() * gfxMatrix().Translate(-clipExtents.TopLeft()); michael@0: michael@0: nsRefPtr tmpCtx(new nsRenderingContext); michael@0: tmpCtx->Init(this->PresContext()->DeviceContext(), image); michael@0: tmpCtx->ThebesContext()->SetMatrix(matrix); michael@0: michael@0: mMaskParent = aParent; michael@0: if (mMaskParentMatrix) { michael@0: *mMaskParentMatrix = aMatrix; michael@0: } else { michael@0: mMaskParentMatrix = new gfxMatrix(aMatrix); michael@0: } michael@0: michael@0: for (nsIFrame* kid = mFrames.FirstChild(); kid; michael@0: kid = kid->GetNextSibling()) { michael@0: // The CTM of each frame referencing us can be different michael@0: nsISVGChildFrame* SVGFrame = do_QueryFrame(kid); michael@0: if (SVGFrame) { michael@0: SVGFrame->NotifySVGChanged(nsISVGChildFrame::TRANSFORM_CHANGED); michael@0: } michael@0: nsSVGUtils::PaintFrameWithEffects(tmpCtx, nullptr, kid); michael@0: } michael@0: michael@0: uint8_t *data = image->Data(); michael@0: int32_t stride = image->Stride(); michael@0: nsIntRect rect(0, 0, surfaceSize.width, surfaceSize.height); michael@0: michael@0: if (StyleSVGReset()->mMaskType == NS_STYLE_MASK_TYPE_LUMINANCE) { michael@0: if (StyleSVG()->mColorInterpolation == michael@0: NS_STYLE_COLOR_INTERPOLATION_LINEARRGB) { michael@0: ComputeLinearRGBLuminanceMask(data, stride, rect, aOpacity); michael@0: } else { michael@0: ComputesRGBLuminanceMask(data, stride, rect, aOpacity); michael@0: } michael@0: } else { michael@0: ComputeAlphaMask(data, stride, rect, aOpacity); michael@0: } michael@0: michael@0: nsRefPtr retval = new gfxPattern(image); michael@0: retval->SetMatrix(matrix); michael@0: return retval.forget(); michael@0: } michael@0: michael@0: nsresult michael@0: nsSVGMaskFrame::AttributeChanged(int32_t aNameSpaceID, michael@0: nsIAtom* aAttribute, michael@0: int32_t aModType) michael@0: { michael@0: if (aNameSpaceID == kNameSpaceID_None && michael@0: (aAttribute == nsGkAtoms::x || michael@0: aAttribute == nsGkAtoms::y || michael@0: aAttribute == nsGkAtoms::width || michael@0: aAttribute == nsGkAtoms::height|| michael@0: aAttribute == nsGkAtoms::maskUnits || michael@0: aAttribute == nsGkAtoms::maskContentUnits)) { michael@0: nsSVGEffects::InvalidateDirectRenderingObservers(this); michael@0: } michael@0: michael@0: return nsSVGMaskFrameBase::AttributeChanged(aNameSpaceID, michael@0: aAttribute, aModType); michael@0: } michael@0: michael@0: #ifdef DEBUG michael@0: void michael@0: nsSVGMaskFrame::Init(nsIContent* aContent, michael@0: nsIFrame* aParent, michael@0: nsIFrame* aPrevInFlow) michael@0: { michael@0: NS_ASSERTION(aContent->IsSVG(nsGkAtoms::mask), michael@0: "Content is not an SVG mask"); michael@0: michael@0: nsSVGMaskFrameBase::Init(aContent, aParent, aPrevInFlow); michael@0: } michael@0: #endif /* DEBUG */ michael@0: michael@0: nsIAtom * michael@0: nsSVGMaskFrame::GetType() const michael@0: { michael@0: return nsGkAtoms::svgMaskFrame; michael@0: } michael@0: michael@0: gfxMatrix michael@0: nsSVGMaskFrame::GetCanvasTM(uint32_t aFor, nsIFrame* aTransformRoot) michael@0: { michael@0: NS_ASSERTION(mMaskParentMatrix, "null parent matrix"); michael@0: michael@0: SVGMaskElement *mask = static_cast(mContent); michael@0: michael@0: return nsSVGUtils::AdjustMatrixForUnits( michael@0: mMaskParentMatrix ? *mMaskParentMatrix : gfxMatrix(), michael@0: &mask->mEnumAttributes[SVGMaskElement::MASKCONTENTUNITS], michael@0: mMaskParent); michael@0: } michael@0: