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