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"
|
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"
|
2017-08-30 16:14:46 +03:00
|
|
|
#include "SVGObserverUtils.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"
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-22 21:20:41 +03:00
|
|
|
nsIFrame* NS_NewSVGMaskFrame(nsIPresShell* aPresShell, ComputedStyle* aStyle) {
|
|
|
|
return new (aPresShell) nsSVGMaskFrame(aStyle);
|
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);
|
|
|
|
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);
|
2017-09-04 09:04:17 +03:00
|
|
|
gfxRect maskSurfaceRect = context->GetClipExtents(gfxContext::eDeviceSpace);
|
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;
|
2015-04-07 17:08:57 +03:00
|
|
|
IntSize maskSurfaceSize = 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()) {
|
2017-12-11 18:37:59 +03:00
|
|
|
// Return value other then ImgDrawResult::SUCCESS, so the caller can skip
|
2016-11-18 17:06:51 +03:00
|
|
|
// 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) {
|
2018-02-16 23:52:45 +03:00
|
|
|
maskDT = context->GetDrawTarget()->CreateSimilarDrawTarget(
|
2017-05-27 05:19:44 +03:00
|
|
|
maskSurfaceSize, SurfaceFormat::B8G8R8A8);
|
|
|
|
} else {
|
2018-02-16 23:52:45 +03:00
|
|
|
maskDT = context->GetDrawTarget()->CreateSimilarDrawTarget(
|
2017-05-27 05:19:44 +03:00
|
|
|
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
|
|
|
|
2017-11-11 05:14:09 +03:00
|
|
|
Matrix maskSurfaceMatrix =
|
|
|
|
context->CurrentMatrix() *
|
|
|
|
ToMatrix(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()) {
|
2018-12-21 11:58:14 +03:00
|
|
|
m = static_cast<SVGElement*>(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
|
|
|
|
2017-11-11 05:14:09 +03:00
|
|
|
*aParams.maskTransform = 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) {
|
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-01-02 21:24:11 +03:00
|
|
|
SVGEnum* 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
|
|
|
}
|