2007-03-22 10:30:00 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2012-05-21 04:12:37 -07: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/. */
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-03-26 04:58:59 -07:00
|
|
|
// Main header first:
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "nsSVGFilterFrame.h"
|
2012-03-20 05:15:55 -07:00
|
|
|
|
2012-03-26 04:58:59 -07:00
|
|
|
// Keep others in (case-insensitive) order:
|
|
|
|
#include "gfxASurface.h"
|
|
|
|
#include "gfxUtils.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "nsGkAtoms.h"
|
2012-03-26 04:58:59 -07:00
|
|
|
#include "nsRenderingContext.h"
|
2011-10-09 10:17:29 -07:00
|
|
|
#include "nsSVGEffects.h"
|
2012-06-26 03:49:23 -07:00
|
|
|
#include "nsSVGElement.h"
|
2013-02-17 15:28:47 -08:00
|
|
|
#include "mozilla/dom/SVGFilterElement.h"
|
2008-09-10 17:24:16 -07:00
|
|
|
#include "nsSVGFilterInstance.h"
|
2012-03-26 04:58:59 -07:00
|
|
|
#include "nsSVGFilterPaintCallback.h"
|
2012-06-24 05:59:26 -07:00
|
|
|
#include "nsSVGIntegrationUtils.h"
|
2012-03-26 04:58:59 -07:00
|
|
|
#include "nsSVGUtils.h"
|
2013-01-31 15:11:49 -08:00
|
|
|
#include "nsContentUtils.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2013-02-17 15:28:47 -08:00
|
|
|
using namespace mozilla::dom;
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
nsIFrame*
|
2009-01-19 10:31:34 -08:00
|
|
|
NS_NewSVGFilterFrame(nsIPresShell* aPresShell, nsStyleContext* aContext)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
return new (aPresShell) nsSVGFilterFrame(aContext);
|
|
|
|
}
|
|
|
|
|
2009-09-12 09:49:24 -07:00
|
|
|
NS_IMPL_FRAMEARENA_HELPERS(nsSVGFilterFrame)
|
|
|
|
|
2013-12-26 10:49:49 -08:00
|
|
|
class MOZ_STACK_CLASS nsSVGFilterFrame::AutoFilterReferencer
|
2012-03-28 04:31:47 -07:00
|
|
|
{
|
|
|
|
public:
|
2013-12-26 10:49:49 -08:00
|
|
|
AutoFilterReferencer(nsSVGFilterFrame *aFrame MOZ_GUARD_OBJECT_NOTIFIER_PARAM)
|
2012-03-28 04:31:47 -07:00
|
|
|
: mFrame(aFrame)
|
|
|
|
{
|
2013-12-26 10:49:49 -08:00
|
|
|
MOZ_GUARD_OBJECT_NOTIFIER_INIT;
|
2012-03-28 04:31:47 -07:00
|
|
|
// Reference loops should normally be detected in advance and handled, so
|
|
|
|
// we're not expecting to encounter them here
|
|
|
|
NS_ABORT_IF_FALSE(!mFrame->mLoopFlag, "Undetected reference loop!");
|
|
|
|
mFrame->mLoopFlag = true;
|
|
|
|
}
|
|
|
|
~AutoFilterReferencer() {
|
|
|
|
mFrame->mLoopFlag = false;
|
|
|
|
}
|
|
|
|
private:
|
|
|
|
nsSVGFilterFrame *mFrame;
|
2013-12-26 10:49:49 -08:00
|
|
|
MOZ_DECL_USE_GUARD_OBJECT_NOTIFIER
|
2012-03-28 04:31:47 -07:00
|
|
|
};
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
uint16_t
|
|
|
|
nsSVGFilterFrame::GetEnumValue(uint32_t aIndex, nsIContent *aDefault)
|
2012-03-28 04:31:47 -07:00
|
|
|
{
|
|
|
|
nsSVGEnum& thisEnum =
|
2013-02-17 15:28:47 -08:00
|
|
|
static_cast<SVGFilterElement *>(mContent)->mEnumAttributes[aIndex];
|
2012-03-28 04:31:47 -07:00
|
|
|
|
|
|
|
if (thisEnum.IsExplicitlySet())
|
|
|
|
return thisEnum.GetAnimValue();
|
|
|
|
|
|
|
|
AutoFilterReferencer filterRef(this);
|
|
|
|
|
|
|
|
nsSVGFilterFrame *next = GetReferencedFilterIfNotInUse();
|
|
|
|
return next ? next->GetEnumValue(aIndex, aDefault) :
|
2013-02-17 15:28:47 -08:00
|
|
|
static_cast<SVGFilterElement *>(aDefault)->
|
2012-03-28 04:31:47 -07:00
|
|
|
mEnumAttributes[aIndex].GetAnimValue();
|
|
|
|
}
|
|
|
|
|
|
|
|
const nsSVGIntegerPair *
|
2012-08-22 08:56:38 -07:00
|
|
|
nsSVGFilterFrame::GetIntegerPairValue(uint32_t aIndex, nsIContent *aDefault)
|
2012-03-28 04:31:47 -07:00
|
|
|
{
|
|
|
|
const nsSVGIntegerPair *thisIntegerPair =
|
2013-02-17 15:28:47 -08:00
|
|
|
&static_cast<SVGFilterElement *>(mContent)->mIntegerPairAttributes[aIndex];
|
2012-03-28 04:31:47 -07:00
|
|
|
|
|
|
|
if (thisIntegerPair->IsExplicitlySet())
|
|
|
|
return thisIntegerPair;
|
|
|
|
|
|
|
|
AutoFilterReferencer filterRef(this);
|
|
|
|
|
|
|
|
nsSVGFilterFrame *next = GetReferencedFilterIfNotInUse();
|
|
|
|
return next ? next->GetIntegerPairValue(aIndex, aDefault) :
|
2013-02-17 15:28:47 -08:00
|
|
|
&static_cast<SVGFilterElement *>(aDefault)->mIntegerPairAttributes[aIndex];
|
2012-03-28 04:31:47 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
const nsSVGLength2 *
|
2012-08-22 08:56:38 -07:00
|
|
|
nsSVGFilterFrame::GetLengthValue(uint32_t aIndex, nsIContent *aDefault)
|
2012-03-28 04:31:47 -07:00
|
|
|
{
|
|
|
|
const nsSVGLength2 *thisLength =
|
2013-02-17 15:28:47 -08:00
|
|
|
&static_cast<SVGFilterElement *>(mContent)->mLengthAttributes[aIndex];
|
2012-03-28 04:31:47 -07:00
|
|
|
|
|
|
|
if (thisLength->IsExplicitlySet())
|
|
|
|
return thisLength;
|
|
|
|
|
|
|
|
AutoFilterReferencer filterRef(this);
|
|
|
|
|
|
|
|
nsSVGFilterFrame *next = GetReferencedFilterIfNotInUse();
|
|
|
|
return next ? next->GetLengthValue(aIndex, aDefault) :
|
2013-02-17 15:28:47 -08:00
|
|
|
&static_cast<SVGFilterElement *>(aDefault)->mLengthAttributes[aIndex];
|
2012-03-28 04:31:47 -07:00
|
|
|
}
|
|
|
|
|
2013-02-17 15:28:47 -08:00
|
|
|
const SVGFilterElement *
|
2012-03-28 04:31:47 -07:00
|
|
|
nsSVGFilterFrame::GetFilterContent(nsIContent *aDefault)
|
|
|
|
{
|
2012-03-29 01:41:37 -07:00
|
|
|
for (nsIContent* child = mContent->GetFirstChild();
|
|
|
|
child;
|
|
|
|
child = child->GetNextSibling()) {
|
2012-03-28 04:31:47 -07:00
|
|
|
nsRefPtr<nsSVGFE> primitive;
|
|
|
|
CallQueryInterface(child, (nsSVGFE**)getter_AddRefs(primitive));
|
|
|
|
if (primitive) {
|
2013-02-17 15:28:47 -08:00
|
|
|
return static_cast<SVGFilterElement *>(mContent);
|
2012-03-28 04:31:47 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
AutoFilterReferencer filterRef(this);
|
|
|
|
|
|
|
|
nsSVGFilterFrame *next = GetReferencedFilterIfNotInUse();
|
|
|
|
return next ? next->GetFilterContent(aDefault) :
|
2013-02-17 15:28:47 -08:00
|
|
|
static_cast<SVGFilterElement *>(aDefault);
|
2012-03-28 04:31:47 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
nsSVGFilterFrame *
|
|
|
|
nsSVGFilterFrame::GetReferencedFilter()
|
|
|
|
{
|
|
|
|
if (mNoHRefURI)
|
2012-07-30 07:20:58 -07:00
|
|
|
return nullptr;
|
2012-03-28 04:31:47 -07:00
|
|
|
|
|
|
|
nsSVGPaintingProperty *property = static_cast<nsSVGPaintingProperty*>
|
|
|
|
(Properties().Get(nsSVGEffects::HrefProperty()));
|
|
|
|
|
|
|
|
if (!property) {
|
|
|
|
// Fetch our Filter element's xlink:href attribute
|
2013-02-17 15:28:47 -08:00
|
|
|
SVGFilterElement *filter = static_cast<SVGFilterElement *>(mContent);
|
2012-03-28 04:31:47 -07:00
|
|
|
nsAutoString href;
|
2013-02-17 15:28:47 -08:00
|
|
|
filter->mStringAttributes[SVGFilterElement::HREF].GetAnimValue(href, filter);
|
2012-03-28 04:31:47 -07:00
|
|
|
if (href.IsEmpty()) {
|
|
|
|
mNoHRefURI = true;
|
2012-07-30 07:20:58 -07:00
|
|
|
return nullptr; // no URL
|
2012-03-28 04:31:47 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Convert href to an nsIURI
|
|
|
|
nsCOMPtr<nsIURI> targetURI;
|
|
|
|
nsCOMPtr<nsIURI> base = mContent->GetBaseURI();
|
|
|
|
nsContentUtils::NewURIWithDocumentCharset(getter_AddRefs(targetURI), href,
|
|
|
|
mContent->GetCurrentDoc(), base);
|
|
|
|
|
|
|
|
property =
|
|
|
|
nsSVGEffects::GetPaintingProperty(targetURI, this, nsSVGEffects::HrefProperty());
|
|
|
|
if (!property)
|
2012-07-30 07:20:58 -07:00
|
|
|
return nullptr;
|
2012-03-28 04:31:47 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
nsIFrame *result = property->GetReferencedFrame();
|
|
|
|
if (!result)
|
2012-07-30 07:20:58 -07:00
|
|
|
return nullptr;
|
2012-03-28 04:31:47 -07:00
|
|
|
|
|
|
|
nsIAtom* frameType = result->GetType();
|
|
|
|
if (frameType != nsGkAtoms::svgFilterFrame)
|
2012-07-30 07:20:58 -07:00
|
|
|
return nullptr;
|
2012-03-28 04:31:47 -07:00
|
|
|
|
|
|
|
return static_cast<nsSVGFilterFrame*>(result);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsSVGFilterFrame *
|
|
|
|
nsSVGFilterFrame::GetReferencedFilterIfNotInUse()
|
|
|
|
{
|
|
|
|
nsSVGFilterFrame *referenced = GetReferencedFilter();
|
|
|
|
if (!referenced)
|
2012-07-30 07:20:58 -07:00
|
|
|
return nullptr;
|
2012-03-28 04:31:47 -07:00
|
|
|
|
|
|
|
if (referenced->mLoopFlag) {
|
|
|
|
// XXXjwatt: we should really send an error to the JavaScript Console here:
|
|
|
|
NS_WARNING("Filter reference loop detected while inheriting attribute!");
|
2012-07-30 07:20:58 -07:00
|
|
|
return nullptr;
|
2012-03-28 04:31:47 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return referenced;
|
|
|
|
}
|
|
|
|
|
2011-10-09 10:17:29 -07:00
|
|
|
NS_IMETHODIMP
|
2012-08-22 08:56:38 -07:00
|
|
|
nsSVGFilterFrame::AttributeChanged(int32_t aNameSpaceID,
|
2011-10-09 10:17:29 -07:00
|
|
|
nsIAtom* aAttribute,
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t aModType)
|
2011-10-09 10:17:29 -07:00
|
|
|
{
|
2012-03-28 04:31:47 -07:00
|
|
|
if (aNameSpaceID == kNameSpaceID_None &&
|
|
|
|
(aAttribute == nsGkAtoms::x ||
|
|
|
|
aAttribute == nsGkAtoms::y ||
|
|
|
|
aAttribute == nsGkAtoms::width ||
|
|
|
|
aAttribute == nsGkAtoms::height ||
|
|
|
|
aAttribute == nsGkAtoms::filterRes ||
|
|
|
|
aAttribute == nsGkAtoms::filterUnits ||
|
|
|
|
aAttribute == nsGkAtoms::primitiveUnits)) {
|
2012-06-23 07:19:00 -07:00
|
|
|
nsSVGEffects::InvalidateDirectRenderingObservers(this);
|
2012-03-28 04:31:47 -07:00
|
|
|
} else if (aNameSpaceID == kNameSpaceID_XLink &&
|
|
|
|
aAttribute == nsGkAtoms::href) {
|
|
|
|
// Blow away our reference, if any
|
|
|
|
Properties().Delete(nsSVGEffects::HrefProperty());
|
|
|
|
mNoHRefURI = false;
|
|
|
|
// And update whoever references us
|
2012-06-23 07:19:00 -07:00
|
|
|
nsSVGEffects::InvalidateDirectRenderingObservers(this);
|
2011-10-09 10:17:29 -07:00
|
|
|
}
|
|
|
|
return nsSVGFilterFrameBase::AttributeChanged(aNameSpaceID,
|
|
|
|
aAttribute, aModType);
|
|
|
|
}
|
|
|
|
|
2008-07-22 19:15:27 -07:00
|
|
|
nsresult
|
2012-06-16 13:23:48 -07:00
|
|
|
nsSVGFilterFrame::PaintFilteredFrame(nsRenderingContext *aContext,
|
|
|
|
nsIFrame *aFilteredFrame,
|
|
|
|
nsSVGFilterPaintCallback *aPaintCallback,
|
2013-09-11 00:27:45 -07:00
|
|
|
const nsRect *aDirtyArea,
|
|
|
|
nsIFrame* aTransformRoot)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2014-02-05 14:04:42 -08:00
|
|
|
nsSVGFilterInstance instance(aFilteredFrame, this, aPaintCallback,
|
|
|
|
aDirtyArea, nullptr, nullptr, nullptr,
|
|
|
|
aTransformRoot);
|
|
|
|
if (!instance.IsInitialized()) {
|
2008-09-10 17:24:16 -07:00
|
|
|
return NS_OK;
|
2012-06-26 03:49:23 -07:00
|
|
|
}
|
2014-02-05 14:04:42 -08:00
|
|
|
return instance.Render(aContext->ThebesContext());
|
2008-07-22 19:15:27 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-06-26 03:49:23 -07:00
|
|
|
static nsRect
|
|
|
|
TransformFilterSpaceToFrameSpace(nsSVGFilterInstance *aInstance,
|
|
|
|
nsIntRect *aRect)
|
2008-07-22 19:15:27 -07:00
|
|
|
{
|
2013-09-06 02:31:17 -07:00
|
|
|
if (aRect->IsEmpty()) {
|
|
|
|
return nsRect();
|
|
|
|
}
|
2012-06-26 03:49:23 -07:00
|
|
|
gfxMatrix m = aInstance->GetFilterSpaceToFrameSpaceInCSSPxTransform();
|
2008-09-10 17:24:16 -07:00
|
|
|
gfxRect r(aRect->x, aRect->y, aRect->width, aRect->height);
|
|
|
|
r = m.TransformBounds(r);
|
2012-06-26 03:49:23 -07:00
|
|
|
return nsLayoutUtils::RoundGfxRectToAppRect(r, aInstance->AppUnitsPerCSSPixel());
|
2008-09-10 17:24:16 -07:00
|
|
|
}
|
2008-07-22 19:15:27 -07:00
|
|
|
|
2012-06-26 03:49:23 -07:00
|
|
|
nsRect
|
2012-06-16 13:23:48 -07:00
|
|
|
nsSVGFilterFrame::GetPostFilterDirtyArea(nsIFrame *aFilteredFrame,
|
2012-06-26 03:49:23 -07:00
|
|
|
const nsRect& aPreFilterDirtyRect)
|
2008-09-10 17:24:16 -07:00
|
|
|
{
|
2013-09-06 02:31:17 -07:00
|
|
|
if (aPreFilterDirtyRect.IsEmpty()) {
|
|
|
|
return nsRect();
|
|
|
|
}
|
|
|
|
|
2014-02-05 14:04:42 -08:00
|
|
|
nsSVGFilterInstance instance(aFilteredFrame, this, nullptr, nullptr,
|
|
|
|
&aPreFilterDirtyRect);
|
|
|
|
if (!instance.IsInitialized()) {
|
2012-06-26 03:49:23 -07:00
|
|
|
return nsRect();
|
|
|
|
}
|
2008-09-10 17:24:16 -07:00
|
|
|
// We've passed in the source's dirty area so the instance knows about it.
|
|
|
|
// Now we can ask the instance to compute the area of the filter output
|
|
|
|
// that's dirty.
|
|
|
|
nsIntRect dirtyRect;
|
2014-02-05 14:04:42 -08:00
|
|
|
nsresult rv = instance.ComputePostFilterDirtyRect(&dirtyRect);
|
2008-07-22 19:15:27 -07:00
|
|
|
if (NS_SUCCEEDED(rv)) {
|
2014-02-05 14:04:42 -08:00
|
|
|
return TransformFilterSpaceToFrameSpace(&instance, &dirtyRect);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2012-06-26 03:49:23 -07:00
|
|
|
return nsRect();
|
2008-09-10 17:24:16 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-06-26 03:49:23 -07:00
|
|
|
nsRect
|
2012-06-16 13:23:48 -07:00
|
|
|
nsSVGFilterFrame::GetPreFilterNeededArea(nsIFrame *aFilteredFrame,
|
2012-06-26 03:49:23 -07:00
|
|
|
const nsRect& aPostFilterDirtyRect)
|
2008-09-10 17:24:16 -07:00
|
|
|
{
|
2014-02-05 14:04:42 -08:00
|
|
|
nsSVGFilterInstance instance(aFilteredFrame, this, nullptr,
|
|
|
|
&aPostFilterDirtyRect);
|
|
|
|
if (!instance.IsInitialized()) {
|
2012-06-26 03:49:23 -07:00
|
|
|
return nsRect();
|
|
|
|
}
|
2008-09-10 17:24:16 -07:00
|
|
|
// Now we can ask the instance to compute the area of the source
|
|
|
|
// that's needed.
|
|
|
|
nsIntRect neededRect;
|
2014-02-05 14:04:42 -08:00
|
|
|
nsresult rv = instance.ComputeSourceNeededRect(&neededRect);
|
2008-09-10 17:24:16 -07:00
|
|
|
if (NS_SUCCEEDED(rv)) {
|
2014-02-05 14:04:42 -08:00
|
|
|
return TransformFilterSpaceToFrameSpace(&instance, &neededRect);
|
2008-09-10 17:24:16 -07:00
|
|
|
}
|
2012-06-26 03:49:23 -07:00
|
|
|
return nsRect();
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2012-06-26 03:49:23 -07:00
|
|
|
nsRect
|
2012-06-16 13:23:48 -07:00
|
|
|
nsSVGFilterFrame::GetPostFilterBounds(nsIFrame *aFilteredFrame,
|
2012-06-22 20:01:36 -07:00
|
|
|
const gfxRect *aOverrideBBox,
|
2012-06-26 03:49:23 -07:00
|
|
|
const nsRect *aPreFilterBounds)
|
2008-09-10 17:24:16 -07:00
|
|
|
{
|
2012-08-14 02:04:24 -07:00
|
|
|
MOZ_ASSERT(!(aFilteredFrame->GetStateBits() & NS_FRAME_SVG_LAYOUT) ||
|
2013-07-12 00:13:07 -07:00
|
|
|
!(aFilteredFrame->GetStateBits() & NS_FRAME_IS_NONDISPLAY),
|
2012-08-14 02:04:24 -07:00
|
|
|
"Non-display SVG do not maintain visual overflow rects");
|
|
|
|
|
2014-02-05 14:04:42 -08:00
|
|
|
nsSVGFilterInstance instance(aFilteredFrame, this, nullptr, nullptr,
|
|
|
|
aPreFilterBounds, aPreFilterBounds,
|
|
|
|
aOverrideBBox);
|
|
|
|
if (!instance.IsInitialized()) {
|
2012-06-26 03:49:23 -07:00
|
|
|
return nsRect();
|
|
|
|
}
|
2008-09-10 17:24:16 -07:00
|
|
|
nsIntRect bbox;
|
2014-02-05 14:04:42 -08:00
|
|
|
nsresult rv = instance.ComputePostFilterExtents(&bbox);
|
2008-09-10 17:24:16 -07:00
|
|
|
if (NS_SUCCEEDED(rv)) {
|
2014-02-05 14:04:42 -08:00
|
|
|
return TransformFilterSpaceToFrameSpace(&instance, &bbox);
|
2008-09-10 17:24:16 -07:00
|
|
|
}
|
2012-06-26 03:49:23 -07:00
|
|
|
return nsRect();
|
2008-09-10 17:24:16 -07:00
|
|
|
}
|
2013-01-07 19:22:41 -08:00
|
|
|
|
2009-01-19 10:31:34 -08:00
|
|
|
#ifdef DEBUG
|
2013-03-19 18:47:48 -07:00
|
|
|
void
|
2009-01-19 10:31:34 -08:00
|
|
|
nsSVGFilterFrame::Init(nsIContent* aContent,
|
|
|
|
nsIFrame* aParent,
|
|
|
|
nsIFrame* aPrevInFlow)
|
|
|
|
{
|
2013-01-07 19:22:41 -08:00
|
|
|
NS_ASSERTION(aContent->IsSVG(nsGkAtoms::filter),
|
|
|
|
"Content is not an SVG filter");
|
2009-01-19 10:31:34 -08:00
|
|
|
|
2013-03-19 18:47:48 -07:00
|
|
|
nsSVGFilterFrameBase::Init(aContent, aParent, aPrevInFlow);
|
2009-01-19 10:31:34 -08:00
|
|
|
}
|
|
|
|
#endif /* DEBUG */
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
nsIAtom *
|
|
|
|
nsSVGFilterFrame::GetType() const
|
|
|
|
{
|
|
|
|
return nsGkAtoms::svgFilterFrame;
|
|
|
|
}
|