2006-01-20 17:00:43 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2012-05-21 11:12:37 +00:00
|
|
|
/* 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/. */
|
2006-01-20 17:00:43 +00:00
|
|
|
|
2012-03-26 11:58:59 +00:00
|
|
|
// Main header first:
|
2006-01-20 17:00:43 +00:00
|
|
|
#include "nsSVGMaskFrame.h"
|
2012-03-20 12:15:55 +00:00
|
|
|
|
2012-03-26 11:58:59 +00:00
|
|
|
// Keep others in (case-insensitive) order:
|
2017-02-21 10:47:22 +00:00
|
|
|
#include "AutoReferenceChainGuard.h"
|
2014-06-23 04:45:31 +00:00
|
|
|
#include "gfx2DGlue.h"
|
2006-11-27 17:30:57 +00:00
|
|
|
#include "gfxContext.h"
|
2014-06-23 04:45:31 +00:00
|
|
|
#include "mozilla/gfx/2D.h"
|
2015-10-18 05:24:48 +00:00
|
|
|
#include "mozilla/RefPtr.h"
|
2012-03-26 11:58:59 +00:00
|
|
|
#include "nsSVGEffects.h"
|
2013-01-23 20:59:06 +00:00
|
|
|
#include "mozilla/dom/SVGMaskElement.h"
|
|
|
|
|
2014-06-23 04:45:31 +00:00
|
|
|
using namespace mozilla;
|
2013-01-23 20:59:06 +00:00
|
|
|
using namespace mozilla::dom;
|
2014-06-23 04:45:31 +00:00
|
|
|
using namespace mozilla::gfx;
|
2017-03-08 04:07:00 +00:00
|
|
|
using namespace mozilla::image;
|
2006-01-20 17:00:43 +00:00
|
|
|
|
2017-06-02 18:13:36 +00:00
|
|
|
static LuminanceType
|
|
|
|
GetLuminanceType(uint8_t aNSMaskType)
|
2014-01-02 12:01:29 +00:00
|
|
|
{
|
2017-06-02 18:13:36 +00:00
|
|
|
switch (aNSMaskType) {
|
|
|
|
case NS_STYLE_MASK_TYPE_LUMINANCE:
|
|
|
|
return LuminanceType::LUMINANCE;
|
|
|
|
case NS_STYLE_COLOR_INTERPOLATION_LINEARRGB:
|
|
|
|
return LuminanceType::LINEARRGB;
|
|
|
|
default:
|
|
|
|
{
|
|
|
|
NS_WARNING("Unknown SVG mask type, defaulting to luminance");
|
|
|
|
return LuminanceType::LUMINANCE;
|
2014-01-02 12:01:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-01-20 17:00:43 +00:00
|
|
|
nsIFrame*
|
2009-01-19 18:31:34 +00:00
|
|
|
NS_NewSVGMaskFrame(nsIPresShell* aPresShell, nsStyleContext* aContext)
|
2006-01-20 17:00:43 +00:00
|
|
|
{
|
2006-03-26 21:30:36 +00:00
|
|
|
return new (aPresShell) nsSVGMaskFrame(aContext);
|
2006-01-20 17:00:43 +00:00
|
|
|
}
|
|
|
|
|
2009-09-12 16:49:24 +00:00
|
|
|
NS_IMPL_FRAMEARENA_HELPERS(nsSVGMaskFrame)
|
|
|
|
|
2017-05-18 20:03:45 +00:00
|
|
|
already_AddRefed<SourceSurface>
|
2016-11-16 04:59:52 +00:00
|
|
|
nsSVGMaskFrame::GetMaskForMaskedFrame(MaskParams& aParams)
|
2006-01-20 17:00:43 +00:00
|
|
|
{
|
2017-02-21 10:47:22 +00:00
|
|
|
// Make sure we break reference loops and over long reference chains:
|
|
|
|
static int16_t sRefChainLengthCounter = AutoReferenceChainGuard::noChain;
|
|
|
|
AutoReferenceChainGuard refChainGuard(this, &mInUse,
|
|
|
|
&sRefChainLengthCounter);
|
|
|
|
if (MOZ_UNLIKELY(!refChainGuard.Reference())) {
|
|
|
|
// Break reference chain
|
2017-05-18 20:03:45 +00:00
|
|
|
return nullptr;
|
2007-06-21 11:01:41 +00:00
|
|
|
}
|
|
|
|
|
2016-11-16 04:59:52 +00:00
|
|
|
gfxRect maskArea = GetMaskArea(aParams.maskedFrame);
|
|
|
|
gfxContext* context = aParams.ctx;
|
2012-06-28 22:59:05 +00:00
|
|
|
// Get the clip extents in device space:
|
2014-06-23 04:45:31 +00:00
|
|
|
// Minimizing the mask surface extents (using both the current clip extents
|
|
|
|
// and maskArea) is important for performance.
|
2016-11-16 04:59:52 +00:00
|
|
|
context->Save();
|
|
|
|
nsSVGUtils::SetClipRect(context, aParams.toUserSpace, maskArea);
|
|
|
|
context->SetMatrix(gfxMatrix());
|
|
|
|
gfxRect maskSurfaceRect = context->GetClipExtents();
|
2014-06-23 04:45:31 +00:00
|
|
|
maskSurfaceRect.RoundOut();
|
2016-11-16 04:59:52 +00:00
|
|
|
context->Restore();
|
2006-11-27 17:30:57 +00:00
|
|
|
|
2011-09-29 06:19:26 +00:00
|
|
|
bool resultOverflows;
|
2014-06-23 04:45:31 +00:00
|
|
|
IntSize maskSurfaceSize =
|
2015-04-07 14:08:57 +00:00
|
|
|
nsSVGUtils::ConvertToSurfaceSize(maskSurfaceRect.Size(), &resultOverflows);
|
2007-06-13 09:02:48 +00:00
|
|
|
|
2014-06-23 04:45:31 +00:00
|
|
|
if (resultOverflows || maskSurfaceSize.IsEmpty()) {
|
2016-11-18 14:06:51 +00:00
|
|
|
// Return value other then DrawResult::SUCCESS, so the caller can skip
|
|
|
|
// painting the masked frame(aParams.maskedFrame).
|
2017-05-18 20:03:45 +00:00
|
|
|
return nullptr;
|
2014-06-23 04:45:31 +00:00
|
|
|
}
|
2007-06-13 09:02:48 +00:00
|
|
|
|
2017-05-27 02:19:44 +00:00
|
|
|
uint8_t maskType;
|
|
|
|
if (aParams.maskMode == NS_STYLE_MASK_MODE_MATCH_SOURCE) {
|
|
|
|
maskType = StyleSVGReset()->mMaskType;
|
|
|
|
} else {
|
|
|
|
maskType = aParams.maskMode == NS_STYLE_MASK_MODE_LUMINANCE
|
|
|
|
? NS_STYLE_MASK_TYPE_LUMINANCE : NS_STYLE_MASK_TYPE_ALPHA;
|
|
|
|
}
|
|
|
|
|
|
|
|
RefPtr<DrawTarget> maskDT;
|
|
|
|
if (maskType == NS_STYLE_MASK_TYPE_LUMINANCE) {
|
|
|
|
maskDT = gfxPlatform::GetPlatform()->CreateOffscreenContentDrawTarget(
|
|
|
|
maskSurfaceSize, SurfaceFormat::B8G8R8A8);
|
|
|
|
} else {
|
|
|
|
maskDT = gfxPlatform::GetPlatform()->CreateOffscreenContentDrawTarget(
|
|
|
|
maskSurfaceSize, SurfaceFormat::A8);
|
|
|
|
}
|
2017-01-11 10:14:16 +00:00
|
|
|
|
2016-04-12 19:18:11 +00:00
|
|
|
if (!maskDT || !maskDT->IsValid()) {
|
2017-05-18 20:03:45 +00:00
|
|
|
return nullptr;
|
2014-06-23 04:45:31 +00:00
|
|
|
}
|
2012-06-28 22:59:05 +00:00
|
|
|
|
2014-06-23 04:45:31 +00:00
|
|
|
gfxMatrix maskSurfaceMatrix =
|
2016-11-16 04:59:52 +00:00
|
|
|
context->CurrentMatrix() * gfxMatrix::Translation(-maskSurfaceRect.TopLeft());
|
2006-01-20 17:00:43 +00:00
|
|
|
|
2016-06-06 23:39:56 +00:00
|
|
|
RefPtr<gfxContext> tmpCtx = gfxContext::CreateOrNull(maskDT);
|
2016-04-12 19:18:11 +00:00
|
|
|
MOZ_ASSERT(tmpCtx); // already checked the draw target above
|
2014-10-31 20:08:54 +00:00
|
|
|
tmpCtx->SetMatrix(maskSurfaceMatrix);
|
2010-11-24 09:44:34 +00:00
|
|
|
|
2016-11-16 04:59:52 +00:00
|
|
|
mMatrixForChildren = GetMaskTransform(aParams.maskedFrame) *
|
|
|
|
aParams.toUserSpace;
|
2010-11-24 09:44:34 +00:00
|
|
|
|
|
|
|
for (nsIFrame* kid = mFrames.FirstChild(); kid;
|
|
|
|
kid = kid->GetNextSibling()) {
|
2010-12-24 09:30:01 +00:00
|
|
|
// The CTM of each frame referencing us can be different
|
2017-02-09 18:24:31 +00:00
|
|
|
nsSVGDisplayableFrame* SVGFrame = do_QueryFrame(kid);
|
2010-12-24 09:30:01 +00:00
|
|
|
if (SVGFrame) {
|
2017-02-09 18:24:31 +00:00
|
|
|
SVGFrame->NotifySVGChanged(nsSVGDisplayableFrame::TRANSFORM_CHANGED);
|
2010-12-24 09:30:01 +00:00
|
|
|
}
|
2014-08-29 19:42:07 +00:00
|
|
|
gfxMatrix m = mMatrixForChildren;
|
2015-03-03 11:08:59 +00:00
|
|
|
if (kid->GetContent()->IsSVGElement()) {
|
2014-08-29 19:42:07 +00:00
|
|
|
m = static_cast<nsSVGElement*>(kid->GetContent())->
|
2017-01-13 09:02:09 +00:00
|
|
|
PrependLocalTransformsTo(m, eUserSpaceToParent);
|
2014-08-29 19:42:07 +00:00
|
|
|
}
|
2017-05-18 20:03:45 +00:00
|
|
|
nsSVGUtils::PaintFrameWithEffects(kid, *tmpCtx, m, aParams.imgParams);
|
2010-11-24 09:44:34 +00:00
|
|
|
}
|
2006-09-28 15:08:41 +00:00
|
|
|
|
2017-05-27 02:19:44 +00:00
|
|
|
RefPtr<SourceSurface> surface;
|
|
|
|
if (maskType == NS_STYLE_MASK_TYPE_LUMINANCE) {
|
2017-06-09 14:57:44 +00:00
|
|
|
if (StyleSVG()->mColorInterpolation ==
|
|
|
|
NS_STYLE_COLOR_INTERPOLATION_LINEARRGB) {
|
|
|
|
maskType = NS_STYLE_COLOR_INTERPOLATION_LINEARRGB;
|
|
|
|
}
|
|
|
|
|
2017-06-02 18:13:36 +00:00
|
|
|
RefPtr<SourceSurface> maskSnapshot =
|
|
|
|
maskDT->IntoLuminanceSource(GetLuminanceType(maskType),
|
|
|
|
aParams.opacity);
|
2017-05-27 02:19:44 +00:00
|
|
|
if (!maskSnapshot) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2017-06-02 18:13:36 +00:00
|
|
|
surface = maskSnapshot.forget();
|
2012-12-21 00:15:22 +00:00
|
|
|
} else {
|
2017-05-27 02:19:44 +00:00
|
|
|
maskDT->SetTransform(Matrix());
|
|
|
|
maskDT->FillRect(Rect(0, 0, maskSurfaceSize.width, maskSurfaceSize.height), ColorPattern(Color(1.0f, 1.0f, 1.0f, aParams.opacity)), DrawOptions(1, CompositionOp::OP_IN));
|
|
|
|
RefPtr<SourceSurface> maskSnapshot = maskDT->Snapshot();
|
|
|
|
if (!maskSnapshot) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
surface = maskSnapshot.forget();
|
2012-12-21 00:15:22 +00:00
|
|
|
}
|
2006-01-20 17:00:43 +00:00
|
|
|
|
2014-06-23 04:45:31 +00:00
|
|
|
// Moz2D transforms in the opposite direction to Thebes
|
2014-07-11 07:06:39 +00:00
|
|
|
if (!maskSurfaceMatrix.Invert()) {
|
2017-05-18 20:03:45 +00:00
|
|
|
return nullptr;
|
2014-06-23 04:45:31 +00:00
|
|
|
}
|
2014-09-12 07:20:12 +00:00
|
|
|
|
2016-11-16 04:59:52 +00:00
|
|
|
*aParams.maskTransform = ToMatrix(maskSurfaceMatrix);
|
2017-05-27 02:19:44 +00:00
|
|
|
return surface.forget();
|
2006-01-20 17:00:43 +00:00
|
|
|
}
|
|
|
|
|
2016-06-17 23:24:26 +00:00
|
|
|
gfxRect
|
|
|
|
nsSVGMaskFrame::GetMaskArea(nsIFrame* aMaskedFrame)
|
|
|
|
{
|
|
|
|
SVGMaskElement *maskElem = static_cast<SVGMaskElement*>(mContent);
|
|
|
|
|
|
|
|
uint16_t units =
|
|
|
|
maskElem->mEnumAttributes[SVGMaskElement::MASKUNITS].GetAnimValue();
|
|
|
|
gfxRect bbox;
|
|
|
|
if (units == SVG_UNIT_TYPE_OBJECTBOUNDINGBOX) {
|
2017-03-14 05:26:05 +00:00
|
|
|
bbox =
|
|
|
|
nsSVGUtils::GetBBox(aMaskedFrame,
|
|
|
|
nsSVGUtils::eUseFrameBoundsForOuterSVG |
|
|
|
|
nsSVGUtils::eBBoxIncludeFillGeometry);
|
2016-06-17 23:24:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Bounds in the user space of aMaskedFrame
|
|
|
|
gfxRect maskArea = nsSVGUtils::GetRelativeRect(units,
|
|
|
|
&maskElem->mLengthAttributes[SVGMaskElement::ATTR_X],
|
|
|
|
bbox, aMaskedFrame);
|
|
|
|
|
|
|
|
return maskArea;
|
|
|
|
}
|
|
|
|
|
2014-02-18 07:47:48 +00:00
|
|
|
nsresult
|
2012-08-22 15:56:38 +00:00
|
|
|
nsSVGMaskFrame::AttributeChanged(int32_t aNameSpaceID,
|
2011-09-19 12:59:52 +00:00
|
|
|
nsIAtom* aAttribute,
|
2012-08-22 15:56:38 +00:00
|
|
|
int32_t aModType)
|
2011-09-19 12:59:52 +00:00
|
|
|
{
|
|
|
|
if (aNameSpaceID == kNameSpaceID_None &&
|
|
|
|
(aAttribute == nsGkAtoms::x ||
|
|
|
|
aAttribute == nsGkAtoms::y ||
|
|
|
|
aAttribute == nsGkAtoms::width ||
|
|
|
|
aAttribute == nsGkAtoms::height||
|
|
|
|
aAttribute == nsGkAtoms::maskUnits ||
|
|
|
|
aAttribute == nsGkAtoms::maskContentUnits)) {
|
2012-06-23 14:19:00 +00:00
|
|
|
nsSVGEffects::InvalidateDirectRenderingObservers(this);
|
2011-09-19 12:59:52 +00:00
|
|
|
}
|
|
|
|
|
2016-04-18 07:30:09 +00:00
|
|
|
return nsSVGContainerFrame::AttributeChanged(aNameSpaceID,
|
|
|
|
aAttribute, aModType);
|
2011-09-19 12:59:52 +00:00
|
|
|
}
|
|
|
|
|
2009-01-19 18:31:34 +00:00
|
|
|
#ifdef DEBUG
|
2013-03-20 01:47:48 +00:00
|
|
|
void
|
2014-05-24 22:20:40 +00:00
|
|
|
nsSVGMaskFrame::Init(nsIContent* aContent,
|
|
|
|
nsContainerFrame* aParent,
|
|
|
|
nsIFrame* aPrevInFlow)
|
2009-01-19 18:31:34 +00:00
|
|
|
{
|
2015-03-03 11:08:59 +00:00
|
|
|
NS_ASSERTION(aContent->IsSVGElement(nsGkAtoms::mask),
|
2013-01-08 03:22:41 +00:00
|
|
|
"Content is not an SVG mask");
|
2009-01-19 18:31:34 +00:00
|
|
|
|
2016-04-18 07:30:09 +00:00
|
|
|
nsSVGContainerFrame::Init(aContent, aParent, aPrevInFlow);
|
2009-01-19 18:31:34 +00:00
|
|
|
}
|
|
|
|
#endif /* DEBUG */
|
|
|
|
|
2009-04-29 04:31:34 +00:00
|
|
|
gfxMatrix
|
2014-09-08 11:28:50 +00:00
|
|
|
nsSVGMaskFrame::GetCanvasTM()
|
2006-01-20 17:00:43 +00:00
|
|
|
{
|
2014-08-18 09:35:26 +00:00
|
|
|
return mMatrixForChildren;
|
|
|
|
}
|
2006-01-20 17:00:43 +00:00
|
|
|
|
2014-08-18 09:35:26 +00:00
|
|
|
gfxMatrix
|
|
|
|
nsSVGMaskFrame::GetMaskTransform(nsIFrame* aMaskedFrame)
|
|
|
|
{
|
|
|
|
SVGMaskElement *content = static_cast<SVGMaskElement*>(mContent);
|
2007-04-26 08:36:16 +00:00
|
|
|
|
2014-08-18 09:35:26 +00:00
|
|
|
nsSVGEnum* maskContentUnits =
|
|
|
|
&content->mEnumAttributes[SVGMaskElement::MASKCONTENTUNITS];
|
2006-06-28 12:32:26 +00:00
|
|
|
|
2014-08-18 09:35:26 +00:00
|
|
|
return nsSVGUtils::AdjustMatrixForUnits(gfxMatrix(), maskContentUnits,
|
|
|
|
aMaskedFrame);
|
|
|
|
}
|