2017-10-27 20:33:53 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
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"
|
2019-04-16 10:24:49 +03:00
|
|
|
#include "mozilla/PresShell.h"
|
2015-10-18 08:24:48 +03:00
|
|
|
#include "mozilla/RefPtr.h"
|
2013-01-24 00:59:06 +04:00
|
|
|
#include "mozilla/dom/SVGMaskElement.h"
|
2018-02-18 18:53:13 +03:00
|
|
|
#include "mozilla/dom/SVGUnitTypesBinding.h"
|
2019-04-16 10:24:49 +03:00
|
|
|
#include "mozilla/gfx/2D.h"
|
|
|
|
#include "SVGObserverUtils.h"
|
2013-01-24 00:59:06 +04:00
|
|
|
|
2014-06-23 08:45:31 +04:00
|
|
|
using namespace mozilla;
|
2013-01-24 00:59:06 +04:00
|
|
|
using namespace mozilla::dom;
|
2018-06-26 00:20:54 +03:00
|
|
|
using namespace mozilla::dom::SVGUnitTypes_Binding;
|
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) {
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-16 10:24:49 +03:00
|
|
|
nsIFrame* NS_NewSVGMaskFrame(PresShell* aPresShell, ComputedStyle* aStyle) {
|
2019-02-05 19:45:54 +03:00
|
|
|
return new (aPresShell) nsSVGMaskFrame(aStyle, aPresShell->GetPresContext());
|
2006-01-20 20:00:43 +03:00
|
|
|
}
|
|
|
|
|
2009-09-12 20:49:24 +04:00
|
|
|
NS_IMPL_FRAMEARENA_HELPERS(nsSVGMaskFrame)
|
|
|
|
|
2016-11-16 07:59:52 +03:00
|
|
|
already_AddRefed<SourceSurface> nsSVGMaskFrame::GetMaskForMaskedFrame(
|
|
|
|
MaskParams& aParams) {
|
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);
|
2019-06-21 12:51:00 +03:00
|
|
|
if (maskArea.IsEmpty()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2016-11-16 07:59:52 +03:00
|
|
|
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.
|
2019-06-21 12:51:00 +03:00
|
|
|
//
|
|
|
|
gfxRect maskSurfaceRectDouble = aParams.toUserSpace.TransformBounds(maskArea);
|
|
|
|
Rect maskSurfaceRect = ToRect(maskSurfaceRectDouble);
|
2014-06-23 08:45:31 +04:00
|
|
|
maskSurfaceRect.RoundOut();
|
2007-06-13 13:02:48 +04:00
|
|
|
|
2017-05-27 05:19:44 +03:00
|
|
|
uint8_t maskType;
|
2019-02-19 19:54:52 +03:00
|
|
|
if (aParams.maskMode == StyleMaskMode::MatchSource) {
|
2017-05-27 05:19:44 +03:00
|
|
|
maskType = StyleSVGReset()->mMaskType;
|
|
|
|
} else {
|
2019-02-19 19:54:52 +03:00
|
|
|
maskType = aParams.maskMode == StyleMaskMode::Luminance
|
2017-05-27 05:19:44 +03:00
|
|
|
? NS_STYLE_MASK_TYPE_LUMINANCE
|
|
|
|
: NS_STYLE_MASK_TYPE_ALPHA;
|
|
|
|
}
|
|
|
|
|
|
|
|
RefPtr<DrawTarget> maskDT;
|
|
|
|
if (maskType == NS_STYLE_MASK_TYPE_LUMINANCE) {
|
2019-03-05 06:30:09 +03:00
|
|
|
maskDT = context->GetDrawTarget()->CreateClippedDrawTarget(
|
2019-06-21 12:51:00 +03:00
|
|
|
maskSurfaceRect, SurfaceFormat::B8G8R8A8);
|
2017-05-27 05:19:44 +03:00
|
|
|
} else {
|
2019-03-05 06:30:09 +03:00
|
|
|
maskDT = context->GetDrawTarget()->CreateClippedDrawTarget(
|
2019-06-21 12:51:00 +03:00
|
|
|
maskSurfaceRect, SurfaceFormat::A8);
|
2017-05-27 05:19:44 +03:00
|
|
|
}
|
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
|
|
|
|
2019-06-21 12:51:00 +03:00
|
|
|
RefPtr<gfxContext> tmpCtx =
|
|
|
|
gfxContext::CreatePreservingTransformOrNull(maskDT);
|
2016-04-12 22:18:11 +03:00
|
|
|
MOZ_ASSERT(tmpCtx); // already checked the draw target above
|
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()) {
|
2019-04-18 20:19:42 +03:00
|
|
|
gfxMatrix m = mMatrixForChildren;
|
|
|
|
|
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);
|
2019-04-27 10:22:53 +03:00
|
|
|
m = nsSVGUtils::GetTransformMatrixInUserSpace(kid) * m;
|
2010-12-24 12:30:01 +03:00
|
|
|
}
|
2019-04-18 20:19:42 +03: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 {
|
2019-06-21 12:51:00 +03:00
|
|
|
maskDT->FillRect(maskSurfaceRect,
|
2017-05-27 05:19:44 +03:00
|
|
|
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
|
|
|
|
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) {
|
2017-08-27 01:58:38 +03:00
|
|
|
SVGMaskElement* maskElem = static_cast<SVGMaskElement*>(GetContent());
|
2016-06-18 02:24:26 +03:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
nsresult nsSVGMaskFrame::AttributeChanged(int32_t aNameSpaceID,
|
2017-10-03 01:05:19 +03:00
|
|
|
nsAtom* 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)) {
|
2017-08-30 17:58:31 +03:00
|
|
|
SVGObserverUtils::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
|
2014-05-25 02:20:40 +04:00
|
|
|
void nsSVGMaskFrame::Init(nsIContent* aContent, nsContainerFrame* aParent,
|
|
|
|
nsIFrame* aPrevInFlow) {
|
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 */
|
|
|
|
|
2014-09-08 15:28:50 +04:00
|
|
|
gfxMatrix nsSVGMaskFrame::GetCanvasTM() { return mMatrixForChildren; }
|
2006-01-20 20:00:43 +03:00
|
|
|
|
2014-08-18 13:35:26 +04:00
|
|
|
gfxMatrix nsSVGMaskFrame::GetMaskTransform(nsIFrame* aMaskedFrame) {
|
2017-08-27 01:58:38 +03:00
|
|
|
SVGMaskElement* content = static_cast<SVGMaskElement*>(GetContent());
|
2007-04-26 12:36:16 +04:00
|
|
|
|
2019-04-04 20:40:56 +03:00
|
|
|
SVGAnimatedEnumeration* maskContentUnits =
|
2014-08-18 13:35:26 +04:00
|
|
|
&content->mEnumAttributes[SVGMaskElement::MASKCONTENTUNITS];
|
2006-06-28 16:32:26 +04:00
|
|
|
|
2017-08-17 04:52:17 +03:00
|
|
|
uint32_t flags = nsSVGUtils::eBBoxIncludeFillGeometry |
|
|
|
|
(aMaskedFrame->StyleBorder()->mBoxDecorationBreak ==
|
|
|
|
StyleBoxDecorationBreak::Clone
|
|
|
|
? nsSVGUtils::eIncludeOnlyCurrentFrameForNonSVGElement
|
|
|
|
: 0);
|
|
|
|
|
2014-08-18 13:35:26 +04:00
|
|
|
return nsSVGUtils::AdjustMatrixForUnits(gfxMatrix(), maskContentUnits,
|
2017-08-17 04:52:17 +03:00
|
|
|
aMaskedFrame, flags);
|
2014-08-18 13:35:26 +04:00
|
|
|
}
|