gecko-dev/layout/svg/nsSVGMaskFrame.cpp

192 строки
6.0 KiB
C++

/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
// Main header first:
#include "nsSVGMaskFrame.h"
// Keep others in (case-insensitive) order:
#include "gfxContext.h"
#include "gfxImageSurface.h"
#include "nsRenderingContext.h"
#include "nsSVGEffects.h"
#include "mozilla/dom/SVGMaskElement.h"
using namespace mozilla::dom;
//----------------------------------------------------------------------
// Implementation
nsIFrame*
NS_NewSVGMaskFrame(nsIPresShell* aPresShell, nsStyleContext* aContext)
{
return new (aPresShell) nsSVGMaskFrame(aContext);
}
NS_IMPL_FRAMEARENA_HELPERS(nsSVGMaskFrame)
already_AddRefed<gfxPattern>
nsSVGMaskFrame::ComputeMaskAlpha(nsRenderingContext *aContext,
nsIFrame* aParent,
const gfxMatrix &aMatrix,
float aOpacity)
{
// If the flag is set when we get here, it means this mask frame
// has already been used painting the current mask, and the document
// has a mask reference loop.
if (mInUse) {
NS_WARNING("Mask loop detected!");
return nullptr;
}
AutoMaskReferencer maskRef(this);
SVGMaskElement *mask = static_cast<SVGMaskElement*>(mContent);
uint16_t units =
mask->mEnumAttributes[SVGMaskElement::MASKUNITS].GetAnimValue();
gfxRect bbox;
if (units == SVG_UNIT_TYPE_OBJECTBOUNDINGBOX) {
bbox = nsSVGUtils::GetBBox(aParent);
}
gfxRect maskArea = nsSVGUtils::GetRelativeRect(units,
&mask->mLengthAttributes[SVGMaskElement::ATTR_X], bbox, aParent);
gfxContext *gfx = aContext->ThebesContext();
// Get the clip extents in device space:
gfx->Save();
nsSVGUtils::SetClipRect(gfx, aMatrix, maskArea);
gfx->IdentityMatrix();
gfxRect clipExtents = gfx->GetClipExtents();
clipExtents.RoundOut();
gfx->Restore();
bool resultOverflows;
gfxIntSize surfaceSize =
nsSVGUtils::ConvertToSurfaceSize(gfxSize(clipExtents.Width(),
clipExtents.Height()),
&resultOverflows);
// 0 disables mask, < 0 is an error
if (surfaceSize.width <= 0 || surfaceSize.height <= 0)
return nullptr;
if (resultOverflows)
return nullptr;
nsRefPtr<gfxImageSurface> image =
new gfxImageSurface(surfaceSize, gfxASurface::ImageFormatARGB32);
if (!image || image->CairoStatus())
return nullptr;
// We would like to use gfxImageSurface::SetDeviceOffset() to position
// 'image'. However, we need to set the same matrix on the temporary context
// and pattern that we create below as is currently set on 'gfx'.
// Unfortunately, any device offset set by SetDeviceOffset() is affected by
// the transform passed to the SetMatrix() calls, so to avoid that we account
// for the device offset in the transform rather than use SetDeviceOffset().
gfxMatrix matrix =
gfx->CurrentMatrix() * gfxMatrix().Translate(-clipExtents.TopLeft());
nsRenderingContext tmpCtx;
tmpCtx.Init(this->PresContext()->DeviceContext(), image);
tmpCtx.ThebesContext()->SetMatrix(matrix);
mMaskParent = aParent;
if (mMaskParentMatrix) {
*mMaskParentMatrix = aMatrix;
} else {
mMaskParentMatrix = new gfxMatrix(aMatrix);
}
for (nsIFrame* kid = mFrames.FirstChild(); kid;
kid = kid->GetNextSibling()) {
// The CTM of each frame referencing us can be different
nsISVGChildFrame* SVGFrame = do_QueryFrame(kid);
if (SVGFrame) {
SVGFrame->NotifySVGChanged(nsISVGChildFrame::TRANSFORM_CHANGED);
}
nsSVGUtils::PaintFrameWithEffects(&tmpCtx, nullptr, kid);
}
uint8_t *data = image->Data();
int32_t stride = image->Stride();
nsIntRect rect(0, 0, surfaceSize.width, surfaceSize.height);
if (StyleSVGReset()->mMaskType == NS_STYLE_MASK_TYPE_LUMINANCE) {
if (StyleSVG()->mColorInterpolation ==
NS_STYLE_COLOR_INTERPOLATION_LINEARRGB) {
nsSVGUtils::ComputeLinearRGBLuminanceMask(data, stride, rect, aOpacity);
} else {
nsSVGUtils::ComputesRGBLuminanceMask(data, stride, rect, aOpacity);
}
} else {
nsSVGUtils::ComputeAlphaMask(data, stride, rect, aOpacity);
}
nsRefPtr<gfxPattern> retval = new gfxPattern(image);
retval->SetMatrix(matrix);
return retval.forget();
}
/* virtual */ void
nsSVGMaskFrame::DidSetStyleContext(nsStyleContext* aOldStyleContext)
{
nsSVGEffects::InvalidateDirectRenderingObservers(this);
nsSVGMaskFrameBase::DidSetStyleContext(aOldStyleContext);
}
NS_IMETHODIMP
nsSVGMaskFrame::AttributeChanged(int32_t aNameSpaceID,
nsIAtom* aAttribute,
int32_t aModType)
{
if (aNameSpaceID == kNameSpaceID_None &&
(aAttribute == nsGkAtoms::x ||
aAttribute == nsGkAtoms::y ||
aAttribute == nsGkAtoms::width ||
aAttribute == nsGkAtoms::height||
aAttribute == nsGkAtoms::maskUnits ||
aAttribute == nsGkAtoms::maskContentUnits)) {
nsSVGEffects::InvalidateDirectRenderingObservers(this);
}
return nsSVGMaskFrameBase::AttributeChanged(aNameSpaceID,
aAttribute, aModType);
}
#ifdef DEBUG
void
nsSVGMaskFrame::Init(nsIContent* aContent,
nsIFrame* aParent,
nsIFrame* aPrevInFlow)
{
NS_ASSERTION(aContent->IsSVG(nsGkAtoms::mask),
"Content is not an SVG mask");
nsSVGMaskFrameBase::Init(aContent, aParent, aPrevInFlow);
}
#endif /* DEBUG */
nsIAtom *
nsSVGMaskFrame::GetType() const
{
return nsGkAtoms::svgMaskFrame;
}
gfxMatrix
nsSVGMaskFrame::GetCanvasTM(uint32_t aFor)
{
NS_ASSERTION(mMaskParentMatrix, "null parent matrix");
SVGMaskElement *mask = static_cast<SVGMaskElement*>(mContent);
return nsSVGUtils::AdjustMatrixForUnits(
mMaskParentMatrix ? *mMaskParentMatrix : gfxMatrix(),
&mask->mEnumAttributes[SVGMaskElement::MASKCONTENTUNITS],
mMaskParent);
}