зеркало из https://github.com/mozilla/gecko-dev.git
Bug 948265 - [CSS Filters] Move nsAutoFilterInstance filter region calculations into nsSVGFilterInstance. r=roc
This commit is contained in:
Родитель
09762be6af
Коммит
a2b1bff11b
|
@ -15,7 +15,7 @@
|
|||
typedef nsSVGElement SVGFilterElementBase;
|
||||
|
||||
class nsSVGFilterFrame;
|
||||
class nsAutoFilterInstance;
|
||||
class nsSVGFilterInstance;
|
||||
|
||||
nsresult NS_NewSVGFilterElement(nsIContent **aResult,
|
||||
already_AddRefed<nsINodeInfo> aNodeInfo);
|
||||
|
@ -27,7 +27,7 @@ class SVGAnimatedLength;
|
|||
class SVGFilterElement : public SVGFilterElementBase
|
||||
{
|
||||
friend class ::nsSVGFilterFrame;
|
||||
friend class ::nsAutoFilterInstance;
|
||||
friend class ::nsSVGFilterInstance;
|
||||
|
||||
protected:
|
||||
friend nsresult (::NS_NewSVGFilterElement(nsIContent **aResult,
|
||||
|
|
|
@ -30,74 +30,6 @@ NS_NewSVGFilterFrame(nsIPresShell* aPresShell, nsStyleContext* aContext)
|
|||
|
||||
NS_IMPL_FRAMEARENA_HELPERS(nsSVGFilterFrame)
|
||||
|
||||
/**
|
||||
* Converts an nsRect that is relative to a filtered frame's origin (i.e. the
|
||||
* top-left corner of its border box) into filter space.
|
||||
* Returns the entire filter region (a rect the width/height of aFilterRes) if
|
||||
* aFrameRect is null, or if the result is too large to be stored in an
|
||||
* nsIntRect.
|
||||
*/
|
||||
static nsIntRect
|
||||
MapFrameRectToFilterSpace(const nsRect* aRect,
|
||||
int32_t aAppUnitsPerCSSPx,
|
||||
const gfxMatrix& aFrameSpaceInCSSPxToFilterSpace,
|
||||
const gfxIntSize& aFilterRes)
|
||||
{
|
||||
nsIntRect rect(0, 0, aFilterRes.width, aFilterRes.height);
|
||||
if (aRect) {
|
||||
if (aRect->IsEmpty()) {
|
||||
return nsIntRect();
|
||||
}
|
||||
gfxRect rectInCSSPx =
|
||||
nsLayoutUtils::RectToGfxRect(*aRect, aAppUnitsPerCSSPx);
|
||||
gfxRect rectInFilterSpace =
|
||||
aFrameSpaceInCSSPxToFilterSpace.TransformBounds(rectInCSSPx);
|
||||
rectInFilterSpace.RoundOut();
|
||||
nsIntRect intRect;
|
||||
if (gfxUtils::GfxRectToIntRect(rectInFilterSpace, &intRect)) {
|
||||
rect = intRect;
|
||||
}
|
||||
}
|
||||
return rect;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the transform from frame space to the coordinate space that
|
||||
* GetCanvasTM transforms to. "Frame space" is the origin of a frame, aka the
|
||||
* top-left corner of its border box, aka the top left corner of its mRect.
|
||||
*/
|
||||
static gfxMatrix
|
||||
GetUserToFrameSpaceInCSSPxTransform(nsIFrame *aFrame)
|
||||
{
|
||||
gfxMatrix userToFrameSpaceInCSSPx;
|
||||
|
||||
if ((aFrame->GetStateBits() & NS_FRAME_SVG_LAYOUT)) {
|
||||
int32_t appUnitsPerCSSPx = aFrame->PresContext()->AppUnitsPerCSSPixel();
|
||||
// As currently implemented by Mozilla for the purposes of filters, user
|
||||
// space is the coordinate system established by GetCanvasTM(), since
|
||||
// that's what we use to set filterToDeviceSpace above. In other words,
|
||||
// for SVG, user space is actually the coordinate system aTarget
|
||||
// establishes for _its_ children (i.e. after taking account of any x/y
|
||||
// and viewBox attributes), not the coordinate system that is established
|
||||
// for it by its 'transform' attribute (or by its _parent_) as it's
|
||||
// normally defined. (XXX We should think about fixing this.) The only
|
||||
// frame type for which these extra transforms are not simply an x/y
|
||||
// translation is nsSVGInnerSVGFrame, hence we treat it specially here.
|
||||
if (aFrame->GetType() == nsGkAtoms::svgInnerSVGFrame) {
|
||||
userToFrameSpaceInCSSPx =
|
||||
static_cast<nsSVGElement*>(aFrame->GetContent())->
|
||||
PrependLocalTransformsTo(gfxMatrix());
|
||||
} else {
|
||||
gfxPoint targetsUserSpaceOffset =
|
||||
nsLayoutUtils::RectToGfxRect(aFrame->GetRect(), appUnitsPerCSSPx).
|
||||
TopLeft();
|
||||
userToFrameSpaceInCSSPx.Translate(-targetsUserSpaceOffset);
|
||||
}
|
||||
}
|
||||
// else, for all other frames, leave as the identity matrix
|
||||
return userToFrameSpaceInCSSPx;
|
||||
}
|
||||
|
||||
class MOZ_STACK_CLASS nsSVGFilterFrame::AutoFilterReferencer
|
||||
{
|
||||
public:
|
||||
|
@ -118,175 +50,6 @@ private:
|
|||
MOZ_DECL_USE_GUARD_OBJECT_NOTIFIER
|
||||
};
|
||||
|
||||
class MOZ_STACK_CLASS nsAutoFilterInstance {
|
||||
public:
|
||||
nsAutoFilterInstance(nsIFrame *aTarget,
|
||||
nsSVGFilterFrame *aFilterFrame,
|
||||
nsSVGFilterPaintCallback *aPaint,
|
||||
const nsRect *aPostFilterDirtyRect,
|
||||
const nsRect *aPreFilterDirtyRect,
|
||||
const nsRect *aOverridePreFilterVisualOverflowRect,
|
||||
const gfxRect *aOverrideBBox = nullptr,
|
||||
nsIFrame* aTransformRoot = nullptr);
|
||||
~nsAutoFilterInstance() {}
|
||||
|
||||
// If this returns null, then draw nothing. Either the filter draws
|
||||
// nothing or it is "in error".
|
||||
nsSVGFilterInstance* get() { return mInstance; }
|
||||
|
||||
private:
|
||||
nsAutoPtr<nsSVGFilterInstance> mInstance;
|
||||
};
|
||||
|
||||
nsAutoFilterInstance::nsAutoFilterInstance(nsIFrame *aTarget,
|
||||
nsSVGFilterFrame *aFilterFrame,
|
||||
nsSVGFilterPaintCallback *aPaint,
|
||||
const nsRect *aPostFilterDirtyRect,
|
||||
const nsRect *aPreFilterDirtyRect,
|
||||
const nsRect *aPreFilterVisualOverflowRectOverride,
|
||||
const gfxRect *aOverrideBBox,
|
||||
nsIFrame* aTransformRoot)
|
||||
{
|
||||
const SVGFilterElement *filter = aFilterFrame->GetFilterContent();
|
||||
|
||||
uint16_t filterUnits =
|
||||
aFilterFrame->GetEnumValue(SVGFilterElement::FILTERUNITS);
|
||||
uint16_t primitiveUnits =
|
||||
aFilterFrame->GetEnumValue(SVGFilterElement::PRIMITIVEUNITS);
|
||||
|
||||
gfxRect bbox = aOverrideBBox ? *aOverrideBBox : nsSVGUtils::GetBBox(aTarget);
|
||||
|
||||
// Get the filter region (in the filtered element's user space):
|
||||
|
||||
// XXX if filterUnits is set (or has defaulted) to objectBoundingBox, we
|
||||
// should send a warning to the error console if the author has used lengths
|
||||
// with units. This is a common mistake and can result in filterRes being
|
||||
// *massive* below (because we ignore the units and interpret the number as
|
||||
// a factor of the bbox width/height). We should also send a warning if the
|
||||
// user uses a number without units (a future SVG spec should really
|
||||
// deprecate that, since it's too confusing for a bare number to be sometimes
|
||||
// interpreted as a fraction of the bounding box and sometimes as user-space
|
||||
// units). So really only percentage values should be used in this case.
|
||||
|
||||
nsSVGLength2 XYWH[4];
|
||||
NS_ABORT_IF_FALSE(sizeof(filter->mLengthAttributes) == sizeof(XYWH),
|
||||
"XYWH size incorrect");
|
||||
memcpy(XYWH, filter->mLengthAttributes, sizeof(filter->mLengthAttributes));
|
||||
XYWH[0] = *aFilterFrame->GetLengthValue(SVGFilterElement::ATTR_X);
|
||||
XYWH[1] = *aFilterFrame->GetLengthValue(SVGFilterElement::ATTR_Y);
|
||||
XYWH[2] = *aFilterFrame->GetLengthValue(SVGFilterElement::ATTR_WIDTH);
|
||||
XYWH[3] = *aFilterFrame->GetLengthValue(SVGFilterElement::ATTR_HEIGHT);
|
||||
// The filter region in user space, in user units:
|
||||
gfxRect filterRegion = nsSVGUtils::GetRelativeRect(filterUnits,
|
||||
XYWH, bbox, aTarget);
|
||||
|
||||
if (filterRegion.Width() <= 0 || filterRegion.Height() <= 0) {
|
||||
// 0 disables rendering, < 0 is error. dispatch error console warning
|
||||
// or error as appropriate.
|
||||
return;
|
||||
}
|
||||
|
||||
// Calculate filterRes (the width and height of the pixel buffer of the
|
||||
// temporary offscreen surface that we would/will create to paint into when
|
||||
// painting the entire filtered element) and, if necessary, adjust
|
||||
// filterRegion out slightly so that it aligns with pixel boundaries of this
|
||||
// buffer:
|
||||
|
||||
gfxIntSize filterRes;
|
||||
const nsSVGIntegerPair* filterResAttrs =
|
||||
aFilterFrame->GetIntegerPairValue(SVGFilterElement::FILTERRES);
|
||||
if (filterResAttrs->IsExplicitlySet()) {
|
||||
int32_t filterResX = filterResAttrs->GetAnimValue(nsSVGIntegerPair::eFirst);
|
||||
int32_t filterResY = filterResAttrs->GetAnimValue(nsSVGIntegerPair::eSecond);
|
||||
if (filterResX <= 0 || filterResY <= 0) {
|
||||
// 0 disables rendering, < 0 is error. dispatch error console warning?
|
||||
return;
|
||||
}
|
||||
|
||||
filterRegion.Scale(filterResX, filterResY);
|
||||
filterRegion.RoundOut();
|
||||
filterRegion.Scale(1.0 / filterResX, 1.0 / filterResY);
|
||||
// We don't care if this overflows, because we can handle upscaling/
|
||||
// downscaling to filterRes
|
||||
bool overflow;
|
||||
filterRes =
|
||||
nsSVGUtils::ConvertToSurfaceSize(gfxSize(filterResX, filterResY),
|
||||
&overflow);
|
||||
// XXX we could send a warning to the error console if the author specified
|
||||
// filterRes doesn't align well with our outer 'svg' device space.
|
||||
} else {
|
||||
// Match filterRes as closely as possible to the pixel density of the nearest
|
||||
// outer 'svg' device space:
|
||||
gfxMatrix canvasTM =
|
||||
nsSVGUtils::GetCanvasTM(aTarget, nsISVGChildFrame::FOR_OUTERSVG_TM);
|
||||
if (canvasTM.IsSingular()) {
|
||||
// nothing to draw
|
||||
return;
|
||||
}
|
||||
|
||||
gfxSize scale = canvasTM.ScaleFactors(true);
|
||||
filterRegion.Scale(scale.width, scale.height);
|
||||
filterRegion.RoundOut();
|
||||
// We don't care if this overflows, because we can handle upscaling/
|
||||
// downscaling to filterRes
|
||||
bool overflow;
|
||||
filterRes = nsSVGUtils::ConvertToSurfaceSize(filterRegion.Size(),
|
||||
&overflow);
|
||||
filterRegion.Scale(1.0 / scale.width, 1.0 / scale.height);
|
||||
}
|
||||
|
||||
// Get various transforms:
|
||||
|
||||
gfxMatrix filterToUserSpace(filterRegion.Width() / filterRes.width, 0.0f,
|
||||
0.0f, filterRegion.Height() / filterRes.height,
|
||||
filterRegion.X(), filterRegion.Y());
|
||||
|
||||
// Only used (so only set) when we paint:
|
||||
gfxMatrix filterToDeviceSpace;
|
||||
if (aPaint) {
|
||||
filterToDeviceSpace = filterToUserSpace *
|
||||
nsSVGUtils::GetCanvasTM(aTarget, nsISVGChildFrame::FOR_PAINTING);
|
||||
}
|
||||
|
||||
// Convert the passed in rects from frame to filter space:
|
||||
|
||||
int32_t appUnitsPerCSSPx = aTarget->PresContext()->AppUnitsPerCSSPixel();
|
||||
|
||||
gfxMatrix filterToFrameSpaceInCSSPx =
|
||||
filterToUserSpace * GetUserToFrameSpaceInCSSPxTransform(aTarget);
|
||||
// filterToFrameSpaceInCSSPx is always invertible
|
||||
gfxMatrix frameSpaceInCSSPxTofilterSpace = filterToFrameSpaceInCSSPx;
|
||||
frameSpaceInCSSPxTofilterSpace.Invert();
|
||||
|
||||
nsIntRect postFilterDirtyRect =
|
||||
MapFrameRectToFilterSpace(aPostFilterDirtyRect, appUnitsPerCSSPx,
|
||||
frameSpaceInCSSPxTofilterSpace, filterRes);
|
||||
nsIntRect preFilterDirtyRect =
|
||||
MapFrameRectToFilterSpace(aPreFilterDirtyRect, appUnitsPerCSSPx,
|
||||
frameSpaceInCSSPxTofilterSpace, filterRes);
|
||||
nsIntRect preFilterVisualOverflowRect;
|
||||
if (aPreFilterVisualOverflowRectOverride) {
|
||||
preFilterVisualOverflowRect =
|
||||
MapFrameRectToFilterSpace(aPreFilterVisualOverflowRectOverride,
|
||||
appUnitsPerCSSPx,
|
||||
frameSpaceInCSSPxTofilterSpace, filterRes);
|
||||
} else {
|
||||
nsRect preFilterVOR = aTarget->GetPreEffectsVisualOverflowRect();
|
||||
preFilterVisualOverflowRect =
|
||||
MapFrameRectToFilterSpace(&preFilterVOR, appUnitsPerCSSPx,
|
||||
frameSpaceInCSSPxTofilterSpace, filterRes);
|
||||
}
|
||||
|
||||
// Setup instance data
|
||||
mInstance =
|
||||
new nsSVGFilterInstance(aTarget, aPaint, filter, bbox, filterRegion,
|
||||
nsIntSize(filterRes.width, filterRes.height),
|
||||
filterToDeviceSpace, filterToFrameSpaceInCSSPx,
|
||||
preFilterVisualOverflowRect, postFilterDirtyRect,
|
||||
preFilterDirtyRect, primitiveUnits,
|
||||
aTransformRoot);
|
||||
}
|
||||
|
||||
uint16_t
|
||||
nsSVGFilterFrame::GetEnumValue(uint32_t aIndex, nsIContent *aDefault)
|
||||
{
|
||||
|
@ -447,13 +210,13 @@ nsSVGFilterFrame::PaintFilteredFrame(nsRenderingContext *aContext,
|
|||
const nsRect *aDirtyArea,
|
||||
nsIFrame* aTransformRoot)
|
||||
{
|
||||
nsAutoFilterInstance instance(aFilteredFrame, this, aPaintCallback,
|
||||
aDirtyArea, nullptr, nullptr, nullptr,
|
||||
aTransformRoot);
|
||||
if (!instance.get()) {
|
||||
nsSVGFilterInstance instance(aFilteredFrame, this, aPaintCallback,
|
||||
aDirtyArea, nullptr, nullptr, nullptr,
|
||||
aTransformRoot);
|
||||
if (!instance.IsInitialized()) {
|
||||
return NS_OK;
|
||||
}
|
||||
return instance.get()->Render(aContext->ThebesContext());
|
||||
return instance.Render(aContext->ThebesContext());
|
||||
}
|
||||
|
||||
static nsRect
|
||||
|
@ -477,18 +240,18 @@ nsSVGFilterFrame::GetPostFilterDirtyArea(nsIFrame *aFilteredFrame,
|
|||
return nsRect();
|
||||
}
|
||||
|
||||
nsAutoFilterInstance instance(aFilteredFrame, this, nullptr, nullptr,
|
||||
&aPreFilterDirtyRect, nullptr);
|
||||
if (!instance.get()) {
|
||||
nsSVGFilterInstance instance(aFilteredFrame, this, nullptr, nullptr,
|
||||
&aPreFilterDirtyRect);
|
||||
if (!instance.IsInitialized()) {
|
||||
return nsRect();
|
||||
}
|
||||
// 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;
|
||||
nsresult rv = instance.get()->ComputePostFilterDirtyRect(&dirtyRect);
|
||||
nsresult rv = instance.ComputePostFilterDirtyRect(&dirtyRect);
|
||||
if (NS_SUCCEEDED(rv)) {
|
||||
return TransformFilterSpaceToFrameSpace(instance.get(), &dirtyRect);
|
||||
return TransformFilterSpaceToFrameSpace(&instance, &dirtyRect);
|
||||
}
|
||||
return nsRect();
|
||||
}
|
||||
|
@ -497,17 +260,17 @@ nsRect
|
|||
nsSVGFilterFrame::GetPreFilterNeededArea(nsIFrame *aFilteredFrame,
|
||||
const nsRect& aPostFilterDirtyRect)
|
||||
{
|
||||
nsAutoFilterInstance instance(aFilteredFrame, this, nullptr,
|
||||
&aPostFilterDirtyRect, nullptr, nullptr);
|
||||
if (!instance.get()) {
|
||||
nsSVGFilterInstance instance(aFilteredFrame, this, nullptr,
|
||||
&aPostFilterDirtyRect);
|
||||
if (!instance.IsInitialized()) {
|
||||
return nsRect();
|
||||
}
|
||||
// Now we can ask the instance to compute the area of the source
|
||||
// that's needed.
|
||||
nsIntRect neededRect;
|
||||
nsresult rv = instance.get()->ComputeSourceNeededRect(&neededRect);
|
||||
nsresult rv = instance.ComputeSourceNeededRect(&neededRect);
|
||||
if (NS_SUCCEEDED(rv)) {
|
||||
return TransformFilterSpaceToFrameSpace(instance.get(), &neededRect);
|
||||
return TransformFilterSpaceToFrameSpace(&instance, &neededRect);
|
||||
}
|
||||
return nsRect();
|
||||
}
|
||||
|
@ -521,16 +284,16 @@ nsSVGFilterFrame::GetPostFilterBounds(nsIFrame *aFilteredFrame,
|
|||
!(aFilteredFrame->GetStateBits() & NS_FRAME_IS_NONDISPLAY),
|
||||
"Non-display SVG do not maintain visual overflow rects");
|
||||
|
||||
nsAutoFilterInstance instance(aFilteredFrame, this, nullptr, nullptr,
|
||||
aPreFilterBounds, aPreFilterBounds,
|
||||
aOverrideBBox);
|
||||
if (!instance.get()) {
|
||||
nsSVGFilterInstance instance(aFilteredFrame, this, nullptr, nullptr,
|
||||
aPreFilterBounds, aPreFilterBounds,
|
||||
aOverrideBBox);
|
||||
if (!instance.IsInitialized()) {
|
||||
return nsRect();
|
||||
}
|
||||
nsIntRect bbox;
|
||||
nsresult rv = instance.get()->ComputePostFilterExtents(&bbox);
|
||||
nsresult rv = instance.ComputePostFilterExtents(&bbox);
|
||||
if (NS_SUCCEEDED(rv)) {
|
||||
return TransformFilterSpaceToFrameSpace(instance.get(), &bbox);
|
||||
return TransformFilterSpaceToFrameSpace(&instance, &bbox);
|
||||
}
|
||||
return nsRect();
|
||||
}
|
||||
|
|
|
@ -117,7 +117,7 @@ private:
|
|||
// reference another filter and we don't have a property. Return
|
||||
// the referenced filter's frame if available, null otherwise.
|
||||
class AutoFilterReferencer;
|
||||
friend class nsAutoFilterInstance;
|
||||
friend class nsSVGFilterInstance;
|
||||
nsSVGFilterFrame* GetReferencedFilter();
|
||||
nsSVGFilterFrame* GetReferencedFilterIfNotInUse();
|
||||
|
||||
|
|
|
@ -12,6 +12,7 @@
|
|||
#include "nsISVGChildFrame.h"
|
||||
#include "nsRenderingContext.h"
|
||||
#include "mozilla/dom/SVGFilterElement.h"
|
||||
#include "nsSVGFilterFrame.h"
|
||||
#include "nsSVGFilterPaintCallback.h"
|
||||
#include "nsSVGUtils.h"
|
||||
#include "SVGContentUtils.h"
|
||||
|
@ -22,6 +23,147 @@ using namespace mozilla;
|
|||
using namespace mozilla::dom;
|
||||
using namespace mozilla::gfx;
|
||||
|
||||
nsSVGFilterInstance::nsSVGFilterInstance(nsIFrame *aTargetFrame,
|
||||
nsSVGFilterFrame *aFilterFrame,
|
||||
nsSVGFilterPaintCallback *aPaintCallback,
|
||||
const nsRect *aPostFilterDirtyRect,
|
||||
const nsRect *aPreFilterDirtyRect,
|
||||
const nsRect *aPreFilterVisualOverflowRectOverride,
|
||||
const gfxRect *aOverrideBBox,
|
||||
nsIFrame* aTransformRoot) :
|
||||
mTargetFrame(aTargetFrame),
|
||||
mPaintCallback(aPaintCallback),
|
||||
mTransformRoot(aTransformRoot),
|
||||
mInitialized(false) {
|
||||
|
||||
mFilterElement = aFilterFrame->GetFilterContent();
|
||||
|
||||
mPrimitiveUnits =
|
||||
aFilterFrame->GetEnumValue(SVGFilterElement::PRIMITIVEUNITS);
|
||||
|
||||
mTargetBBox = aOverrideBBox ?
|
||||
*aOverrideBBox : nsSVGUtils::GetBBox(mTargetFrame);
|
||||
|
||||
// Get the filter region (in the filtered element's user space):
|
||||
|
||||
// XXX if filterUnits is set (or has defaulted) to objectBoundingBox, we
|
||||
// should send a warning to the error console if the author has used lengths
|
||||
// with units. This is a common mistake and can result in filterRes being
|
||||
// *massive* below (because we ignore the units and interpret the number as
|
||||
// a factor of the bbox width/height). We should also send a warning if the
|
||||
// user uses a number without units (a future SVG spec should really
|
||||
// deprecate that, since it's too confusing for a bare number to be sometimes
|
||||
// interpreted as a fraction of the bounding box and sometimes as user-space
|
||||
// units). So really only percentage values should be used in this case.
|
||||
|
||||
nsSVGLength2 XYWH[4];
|
||||
NS_ABORT_IF_FALSE(sizeof(mFilterElement->mLengthAttributes) == sizeof(XYWH),
|
||||
"XYWH size incorrect");
|
||||
memcpy(XYWH, mFilterElement->mLengthAttributes,
|
||||
sizeof(mFilterElement->mLengthAttributes));
|
||||
XYWH[0] = *aFilterFrame->GetLengthValue(SVGFilterElement::ATTR_X);
|
||||
XYWH[1] = *aFilterFrame->GetLengthValue(SVGFilterElement::ATTR_Y);
|
||||
XYWH[2] = *aFilterFrame->GetLengthValue(SVGFilterElement::ATTR_WIDTH);
|
||||
XYWH[3] = *aFilterFrame->GetLengthValue(SVGFilterElement::ATTR_HEIGHT);
|
||||
uint16_t filterUnits =
|
||||
aFilterFrame->GetEnumValue(SVGFilterElement::FILTERUNITS);
|
||||
// The filter region in user space, in user units:
|
||||
mFilterRegion = nsSVGUtils::GetRelativeRect(filterUnits,
|
||||
XYWH, mTargetBBox, mTargetFrame);
|
||||
|
||||
if (mFilterRegion.Width() <= 0 || mFilterRegion.Height() <= 0) {
|
||||
// 0 disables rendering, < 0 is error. dispatch error console warning
|
||||
// or error as appropriate.
|
||||
return;
|
||||
}
|
||||
|
||||
// Calculate filterRes (the width and height of the pixel buffer of the
|
||||
// temporary offscreen surface that we would/will create to paint into when
|
||||
// painting the entire filtered element) and, if necessary, adjust
|
||||
// mFilterRegion out slightly so that it aligns with pixel boundaries of this
|
||||
// buffer:
|
||||
|
||||
gfxIntSize filterRes;
|
||||
const nsSVGIntegerPair* filterResAttrs =
|
||||
aFilterFrame->GetIntegerPairValue(SVGFilterElement::FILTERRES);
|
||||
if (filterResAttrs->IsExplicitlySet()) {
|
||||
int32_t filterResX = filterResAttrs->GetAnimValue(nsSVGIntegerPair::eFirst);
|
||||
int32_t filterResY = filterResAttrs->GetAnimValue(nsSVGIntegerPair::eSecond);
|
||||
if (filterResX <= 0 || filterResY <= 0) {
|
||||
// 0 disables rendering, < 0 is error. dispatch error console warning?
|
||||
return;
|
||||
}
|
||||
|
||||
mFilterRegion.Scale(filterResX, filterResY);
|
||||
mFilterRegion.RoundOut();
|
||||
mFilterRegion.Scale(1.0 / filterResX, 1.0 / filterResY);
|
||||
// We don't care if this overflows, because we can handle upscaling/
|
||||
// downscaling to filterRes
|
||||
bool overflow;
|
||||
filterRes =
|
||||
nsSVGUtils::ConvertToSurfaceSize(gfxSize(filterResX, filterResY),
|
||||
&overflow);
|
||||
// XXX we could send a warning to the error console if the author specified
|
||||
// filterRes doesn't align well with our outer 'svg' device space.
|
||||
} else {
|
||||
// Match filterRes as closely as possible to the pixel density of the nearest
|
||||
// outer 'svg' device space:
|
||||
gfxMatrix canvasTM =
|
||||
nsSVGUtils::GetCanvasTM(mTargetFrame, nsISVGChildFrame::FOR_OUTERSVG_TM);
|
||||
if (canvasTM.IsSingular()) {
|
||||
// nothing to draw
|
||||
return;
|
||||
}
|
||||
|
||||
gfxSize scale = canvasTM.ScaleFactors(true);
|
||||
mFilterRegion.Scale(scale.width, scale.height);
|
||||
mFilterRegion.RoundOut();
|
||||
// We don't care if this overflows, because we can handle upscaling/
|
||||
// downscaling to filterRes
|
||||
bool overflow;
|
||||
filterRes = nsSVGUtils::ConvertToSurfaceSize(mFilterRegion.Size(),
|
||||
&overflow);
|
||||
mFilterRegion.Scale(1.0 / scale.width, 1.0 / scale.height);
|
||||
}
|
||||
|
||||
mFilterSpaceBounds.SetRect(nsIntPoint(0, 0), filterRes);
|
||||
|
||||
// Get various transforms:
|
||||
|
||||
gfxMatrix filterToUserSpace(mFilterRegion.Width() / filterRes.width, 0.0f,
|
||||
0.0f, mFilterRegion.Height() / filterRes.height,
|
||||
mFilterRegion.X(), mFilterRegion.Y());
|
||||
|
||||
// Only used (so only set) when we paint:
|
||||
if (mPaintCallback) {
|
||||
mFilterSpaceToDeviceSpaceTransform = filterToUserSpace *
|
||||
nsSVGUtils::GetCanvasTM(mTargetFrame, nsISVGChildFrame::FOR_PAINTING);
|
||||
}
|
||||
|
||||
// Convert the passed in rects from frame to filter space:
|
||||
|
||||
mAppUnitsPerCSSPx = mTargetFrame->PresContext()->AppUnitsPerCSSPixel();
|
||||
|
||||
mFilterSpaceToFrameSpaceInCSSPxTransform =
|
||||
filterToUserSpace * GetUserSpaceToFrameSpaceInCSSPxTransform();
|
||||
// mFilterSpaceToFrameSpaceInCSSPxTransform is always invertible
|
||||
mFrameSpaceInCSSPxToFilterSpaceTransform =
|
||||
mFilterSpaceToFrameSpaceInCSSPxTransform;
|
||||
mFrameSpaceInCSSPxToFilterSpaceTransform.Invert();
|
||||
|
||||
mPostFilterDirtyRect = FrameSpaceToFilterSpace(aPostFilterDirtyRect);
|
||||
mPreFilterDirtyRect = FrameSpaceToFilterSpace(aPreFilterDirtyRect);
|
||||
if (aPreFilterVisualOverflowRectOverride) {
|
||||
mTargetBounds =
|
||||
FrameSpaceToFilterSpace(aPreFilterVisualOverflowRectOverride);
|
||||
} else {
|
||||
nsRect preFilterVOR = mTargetFrame->GetPreEffectsVisualOverflowRect();
|
||||
mTargetBounds = FrameSpaceToFilterSpace(&preFilterVOR);
|
||||
}
|
||||
|
||||
mInitialized = true;
|
||||
}
|
||||
|
||||
float
|
||||
nsSVGFilterInstance::GetPrimitiveNumber(uint8_t aCtxType, float aValue) const
|
||||
{
|
||||
|
@ -593,3 +735,55 @@ nsSVGFilterInstance::ComputeSourceNeededRect(nsIntRect* aDirty)
|
|||
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
nsIntRect
|
||||
nsSVGFilterInstance::FrameSpaceToFilterSpace(const nsRect* aRect) const
|
||||
{
|
||||
nsIntRect rect = mFilterSpaceBounds;
|
||||
if (aRect) {
|
||||
if (aRect->IsEmpty()) {
|
||||
return nsIntRect();
|
||||
}
|
||||
gfxRect rectInCSSPx =
|
||||
nsLayoutUtils::RectToGfxRect(*aRect, mAppUnitsPerCSSPx);
|
||||
gfxRect rectInFilterSpace =
|
||||
mFrameSpaceInCSSPxToFilterSpaceTransform.TransformBounds(rectInCSSPx);
|
||||
rectInFilterSpace.RoundOut();
|
||||
nsIntRect intRect;
|
||||
if (gfxUtils::GfxRectToIntRect(rectInFilterSpace, &intRect)) {
|
||||
rect = intRect;
|
||||
}
|
||||
}
|
||||
return rect;
|
||||
}
|
||||
|
||||
gfxMatrix
|
||||
nsSVGFilterInstance::GetUserSpaceToFrameSpaceInCSSPxTransform() const
|
||||
{
|
||||
gfxMatrix userToFrameSpaceInCSSPx;
|
||||
|
||||
if ((mTargetFrame->GetStateBits() & NS_FRAME_SVG_LAYOUT)) {
|
||||
// As currently implemented by Mozilla for the purposes of filters, user
|
||||
// space is the coordinate system established by GetCanvasTM(), since
|
||||
// that's what we use to set filterToDeviceSpace above. In other words,
|
||||
// for SVG, user space is actually the coordinate system aTarget
|
||||
// establishes for _its_ children (i.e. after taking account of any x/y
|
||||
// and viewBox attributes), not the coordinate system that is established
|
||||
// for it by its 'transform' attribute (or by its _parent_) as it's
|
||||
// normally defined. (XXX We should think about fixing this.) The only
|
||||
// frame type for which these extra transforms are not simply an x/y
|
||||
// translation is nsSVGInnerSVGFrame, hence we treat it specially here.
|
||||
if (mTargetFrame->GetType() == nsGkAtoms::svgInnerSVGFrame) {
|
||||
userToFrameSpaceInCSSPx =
|
||||
static_cast<nsSVGElement*>(mTargetFrame->GetContent())->
|
||||
PrependLocalTransformsTo(gfxMatrix());
|
||||
} else {
|
||||
gfxPoint targetsUserSpaceOffset =
|
||||
nsLayoutUtils::RectToGfxRect(mTargetFrame->GetRect(),
|
||||
mAppUnitsPerCSSPx).TopLeft();
|
||||
userToFrameSpaceInCSSPx.Translate(-targetsUserSpaceOffset);
|
||||
}
|
||||
}
|
||||
// else, for all other frames, leave as the identity matrix
|
||||
return userToFrameSpaceInCSSPx;
|
||||
}
|
||||
|
|
|
@ -24,6 +24,7 @@
|
|||
class gfxASurface;
|
||||
class gfxImageSurface;
|
||||
class nsIFrame;
|
||||
class nsSVGFilterFrame;
|
||||
class nsSVGFilterPaintCallback;
|
||||
|
||||
namespace mozilla {
|
||||
|
@ -60,57 +61,34 @@ class nsSVGFilterInstance
|
|||
public:
|
||||
/**
|
||||
* @param aTargetFrame The frame of the filtered element under consideration.
|
||||
* @param aFilterFrame The frame of the SVG filter element.
|
||||
* @param aPaintCallback [optional] The callback that Render() should use to
|
||||
* paint. Only required if you will call Render().
|
||||
* @param aFilterElement The filter element referenced by aTargetFrame's
|
||||
* element.
|
||||
* @param aTargetBBox The filtered element's bbox, in the filtered element's
|
||||
* user space.
|
||||
* @param aFilterRegion The "filter region", in the filtered element's user
|
||||
* space. The caller must have already expanded the region out so that its
|
||||
* edges coincide with pixel boundaries in the offscreen surface that
|
||||
* would/will be created to paint the filter output.
|
||||
* @param aFilterSpaceSize The size of the user specified "filter region",
|
||||
* in filter space units.
|
||||
* @param aFilterSpaceToDeviceSpaceTransform The transform from filter
|
||||
* space to outer-<svg> device space.
|
||||
* @param aTargetBounds The pre-filter paint bounds of the filtered element,
|
||||
* in filter space.
|
||||
* @param aPostFilterDirtyRect [optional] The bounds of the post-filter area
|
||||
* that has to be repainted, in filter space. Only required if you will
|
||||
* call ComputeSourceNeededRect() or Render().
|
||||
* @param aPreFilterDirtyRect [optional] The bounds of the pre-filter area of
|
||||
* the filtered element that changed, in filter space. Only required if you
|
||||
* will call ComputePostFilterDirtyRect().
|
||||
* @param aPrimitiveUnits The value from the 'primitiveUnits' attribute.
|
||||
* @param aOverridePreFilterVisualOverflowRect [optional] Use a different
|
||||
* visual overflow rect for the target element.
|
||||
* @param aOverrideBBox [optional] Use a different SVG bbox for the target
|
||||
* element.
|
||||
* @param aTransformRoot [optional] The transform root frame for painting.
|
||||
*/
|
||||
nsSVGFilterInstance(nsIFrame *aTargetFrame,
|
||||
nsSVGFilterFrame *aFilterFrame,
|
||||
nsSVGFilterPaintCallback *aPaintCallback,
|
||||
const mozilla::dom::SVGFilterElement *aFilterElement,
|
||||
const gfxRect &aTargetBBox,
|
||||
const gfxRect& aFilterRegion,
|
||||
const nsIntSize& aFilterSpaceSize,
|
||||
const gfxMatrix &aFilterSpaceToDeviceSpaceTransform,
|
||||
const gfxMatrix &aFilterSpaceToFrameSpaceInCSSPxTransform,
|
||||
const nsIntRect& aTargetBounds,
|
||||
const nsIntRect& aPostFilterDirtyRect,
|
||||
const nsIntRect& aPreFilterDirtyRect,
|
||||
uint16_t aPrimitiveUnits,
|
||||
nsIFrame* aTransformRoot) :
|
||||
mTargetFrame(aTargetFrame),
|
||||
mPaintCallback(aPaintCallback),
|
||||
mFilterElement(aFilterElement),
|
||||
mTargetBBox(aTargetBBox),
|
||||
mFilterSpaceToDeviceSpaceTransform(aFilterSpaceToDeviceSpaceTransform),
|
||||
mFilterSpaceToFrameSpaceInCSSPxTransform(aFilterSpaceToFrameSpaceInCSSPxTransform),
|
||||
mFilterRegion(aFilterRegion),
|
||||
mFilterSpaceBounds(nsIntPoint(0, 0), aFilterSpaceSize),
|
||||
mTargetBounds(aTargetBounds),
|
||||
mPostFilterDirtyRect(aPostFilterDirtyRect),
|
||||
mPreFilterDirtyRect(aPreFilterDirtyRect),
|
||||
mPrimitiveUnits(aPrimitiveUnits),
|
||||
mTransformRoot(aTransformRoot) {
|
||||
}
|
||||
const nsRect *aPostFilterDirtyRect = nullptr,
|
||||
const nsRect *aPreFilterDirtyRect = nullptr,
|
||||
const nsRect *aOverridePreFilterVisualOverflowRect = nullptr,
|
||||
const gfxRect *aOverrideBBox = nullptr,
|
||||
nsIFrame* aTransformRoot = nullptr);
|
||||
|
||||
/**
|
||||
* Returns true if the filter instance was created successfully.
|
||||
*/
|
||||
bool IsInitialized() const { return mInitialized; }
|
||||
|
||||
/**
|
||||
* Returns the user specified "filter region", in the filtered element's user
|
||||
|
@ -209,9 +187,7 @@ public:
|
|||
return mFilterSpaceToFrameSpaceInCSSPxTransform;
|
||||
}
|
||||
|
||||
int32_t AppUnitsPerCSSPixel() const {
|
||||
return mTargetFrame->PresContext()->AppUnitsPerCSSPixel();
|
||||
}
|
||||
int32_t AppUnitsPerCSSPixel() const { return mAppUnitsPerCSSPx; }
|
||||
|
||||
private:
|
||||
struct SourceInfo {
|
||||
|
@ -286,12 +262,31 @@ private:
|
|||
|
||||
gfxRect UserSpaceToFilterSpace(const gfxRect& aUserSpace) const;
|
||||
|
||||
/**
|
||||
* Converts an nsRect that is relative to a filtered frame's origin (i.e. the
|
||||
* top-left corner of its border box) into filter space.
|
||||
* Returns the entire filter region if aRect is null, or if the result is too
|
||||
* large to be stored in an nsIntRect.
|
||||
*/
|
||||
nsIntRect FrameSpaceToFilterSpace(const nsRect* aRect) const;
|
||||
|
||||
/**
|
||||
* Returns the transform from frame space to the coordinate space that
|
||||
* GetCanvasTM transforms to. "Frame space" is the origin of a frame, aka the
|
||||
* top-left corner of its border box, aka the top left corner of its mRect.
|
||||
*/
|
||||
gfxMatrix GetUserSpaceToFrameSpaceInCSSPxTransform() const;
|
||||
|
||||
/**
|
||||
* The frame for the element that is currently being filtered.
|
||||
*/
|
||||
nsIFrame* mTargetFrame;
|
||||
|
||||
nsSVGFilterPaintCallback* mPaintCallback;
|
||||
|
||||
/**
|
||||
* The filter element referenced by mTargetFrame's element.
|
||||
*/
|
||||
const mozilla::dom::SVGFilterElement* mFilterElement;
|
||||
|
||||
/**
|
||||
|
@ -299,8 +294,20 @@ private:
|
|||
*/
|
||||
gfxRect mTargetBBox;
|
||||
|
||||
/**
|
||||
* The transform from filter space to outer-<svg> device space.
|
||||
*/
|
||||
gfxMatrix mFilterSpaceToDeviceSpaceTransform;
|
||||
|
||||
/**
|
||||
* Transform rects between filter space and frame space in CSS pixels.
|
||||
*/
|
||||
gfxMatrix mFilterSpaceToFrameSpaceInCSSPxTransform;
|
||||
gfxMatrix mFrameSpaceInCSSPxToFilterSpaceTransform;
|
||||
|
||||
/**
|
||||
* The "filter region", in the filtered element's user space.
|
||||
*/
|
||||
gfxRect mFilterRegion;
|
||||
nsIntRect mFilterSpaceBounds;
|
||||
|
||||
|
@ -336,6 +343,8 @@ private:
|
|||
nsIFrame* mTransformRoot;
|
||||
nsTArray<mozilla::RefPtr<SourceSurface>> mInputImages;
|
||||
nsTArray<FilterPrimitiveDescription> mPrimitiveDescriptions;
|
||||
int32_t mAppUnitsPerCSSPx;
|
||||
bool mInitialized;
|
||||
};
|
||||
|
||||
#endif
|
||||
|
|
Загрузка…
Ссылка в новой задаче