2008-07-13 18:31:34 -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/. */
|
2008-07-13 18:31:34 -07:00
|
|
|
|
2012-03-26 04:58:59 -07:00
|
|
|
// Main header first:
|
2008-07-13 18:31:34 -07:00
|
|
|
#include "nsSVGFilterInstance.h"
|
2012-03-20 05:15:55 -07:00
|
|
|
|
2012-03-26 04:58:59 -07:00
|
|
|
// Keep others in (case-insensitive) order:
|
2008-09-10 17:24:16 -07:00
|
|
|
#include "gfxPlatform.h"
|
2010-11-24 01:35:21 -08:00
|
|
|
#include "gfxUtils.h"
|
2012-03-26 04:58:59 -07:00
|
|
|
#include "nsIDOMSVGUnitTypes.h"
|
2012-08-05 12:10:21 -07:00
|
|
|
#include "nsISVGChildFrame.h"
|
2012-03-26 04:58:59 -07:00
|
|
|
#include "nsRenderingContext.h"
|
|
|
|
#include "nsSVGFilterElement.h"
|
|
|
|
#include "nsSVGFilterPaintCallback.h"
|
|
|
|
#include "nsSVGUtils.h"
|
2012-09-22 12:26:05 -07:00
|
|
|
#include "SVGContentUtils.h"
|
2008-07-13 18:31:34 -07:00
|
|
|
|
|
|
|
float
|
2012-08-22 08:56:38 -07:00
|
|
|
nsSVGFilterInstance::GetPrimitiveNumber(uint8_t aCtxType, float aValue) const
|
2008-07-13 18:31:34 -07:00
|
|
|
{
|
2011-07-23 01:44:52 -07:00
|
|
|
nsSVGLength2 val;
|
|
|
|
val.Init(aCtxType, 0xff, aValue,
|
|
|
|
nsIDOMSVGLength::SVG_LENGTHTYPE_NUMBER);
|
|
|
|
|
2008-07-13 18:31:34 -07:00
|
|
|
float value;
|
2008-09-10 17:24:16 -07:00
|
|
|
if (mPrimitiveUnits == nsIDOMSVGUnitTypes::SVG_UNIT_TYPE_OBJECTBOUNDINGBOX) {
|
2011-07-23 01:44:52 -07:00
|
|
|
value = nsSVGUtils::ObjectSpace(mTargetBBox, &val);
|
2008-09-10 17:24:16 -07:00
|
|
|
} else {
|
2011-07-23 01:44:52 -07:00
|
|
|
value = nsSVGUtils::UserSpace(mTargetFrame, &val);
|
2008-09-10 17:24:16 -07:00
|
|
|
}
|
2008-07-13 18:31:34 -07:00
|
|
|
|
2011-07-23 01:44:52 -07:00
|
|
|
switch (aCtxType) {
|
2012-09-22 12:26:05 -07:00
|
|
|
case SVGContentUtils::X:
|
2012-06-16 13:23:48 -07:00
|
|
|
return value * mFilterSpaceSize.width / mFilterRegion.Width();
|
2012-09-22 12:26:05 -07:00
|
|
|
case SVGContentUtils::Y:
|
2012-06-16 13:23:48 -07:00
|
|
|
return value * mFilterSpaceSize.height / mFilterRegion.Height();
|
2012-09-22 12:26:05 -07:00
|
|
|
case SVGContentUtils::XY:
|
2008-07-13 18:31:34 -07:00
|
|
|
default:
|
2012-09-22 12:26:05 -07:00
|
|
|
return value * SVGContentUtils::ComputeNormalizedHypotenuse(
|
2012-06-16 13:23:48 -07:00
|
|
|
mFilterSpaceSize.width / mFilterRegion.Width(),
|
|
|
|
mFilterSpaceSize.height / mFilterRegion.Height());
|
2008-07-13 18:31:34 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-02-08 01:51:19 -08:00
|
|
|
void
|
|
|
|
nsSVGFilterInstance::ConvertLocation(float aValues[3]) const
|
|
|
|
{
|
2011-09-14 07:48:32 -07:00
|
|
|
nsSVGLength2 val[4];
|
2012-09-22 12:26:05 -07:00
|
|
|
val[0].Init(SVGContentUtils::X, 0xff, aValues[0],
|
2011-09-14 07:48:32 -07:00
|
|
|
nsIDOMSVGLength::SVG_LENGTHTYPE_NUMBER);
|
2012-09-22 12:26:05 -07:00
|
|
|
val[1].Init(SVGContentUtils::Y, 0xff, aValues[1],
|
2011-09-14 07:48:32 -07:00
|
|
|
nsIDOMSVGLength::SVG_LENGTHTYPE_NUMBER);
|
|
|
|
// Dummy width/height values
|
2012-09-22 12:26:05 -07:00
|
|
|
val[2].Init(SVGContentUtils::X, 0xff, 0,
|
2011-09-14 07:48:32 -07:00
|
|
|
nsIDOMSVGLength::SVG_LENGTHTYPE_NUMBER);
|
2012-09-22 12:26:05 -07:00
|
|
|
val[3].Init(SVGContentUtils::Y, 0xff, 0,
|
2011-09-14 07:48:32 -07:00
|
|
|
nsIDOMSVGLength::SVG_LENGTHTYPE_NUMBER);
|
|
|
|
|
|
|
|
gfxRect feArea = nsSVGUtils::GetRelativeRect(mPrimitiveUnits,
|
|
|
|
val, mTargetBBox, mTargetFrame);
|
|
|
|
aValues[0] = feArea.X();
|
|
|
|
aValues[1] = feArea.Y();
|
2012-09-22 12:26:05 -07:00
|
|
|
aValues[2] = GetPrimitiveNumber(SVGContentUtils::XY, aValues[2]);
|
2010-02-08 01:51:19 -08:00
|
|
|
}
|
|
|
|
|
2008-07-13 18:31:34 -07:00
|
|
|
already_AddRefed<gfxImageSurface>
|
|
|
|
nsSVGFilterInstance::CreateImage()
|
|
|
|
{
|
|
|
|
nsRefPtr<gfxImageSurface> surface =
|
|
|
|
new gfxImageSurface(gfxIntSize(mSurfaceRect.width, mSurfaceRect.height),
|
|
|
|
gfxASurface::ImageFormatARGB32);
|
|
|
|
|
|
|
|
if (!surface || surface->CairoStatus())
|
2012-07-30 07:20:58 -07:00
|
|
|
return nullptr;
|
2008-07-13 18:31:34 -07:00
|
|
|
|
|
|
|
surface->SetDeviceOffset(gfxPoint(-mSurfaceRect.x, -mSurfaceRect.y));
|
|
|
|
|
2012-07-30 07:20:58 -07:00
|
|
|
gfxImageSurface *retval = nullptr;
|
2008-07-13 18:31:34 -07:00
|
|
|
surface.swap(retval);
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
|
|
|
gfxRect
|
|
|
|
nsSVGFilterInstance::UserSpaceToFilterSpace(const gfxRect& aRect) const
|
|
|
|
{
|
2012-06-16 13:23:48 -07:00
|
|
|
gfxRect r = aRect - mFilterRegion.TopLeft();
|
|
|
|
r.Scale(mFilterSpaceSize.width / mFilterRegion.Width(),
|
|
|
|
mFilterSpaceSize.height / mFilterRegion.Height());
|
2008-07-13 18:31:34 -07:00
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2011-09-14 07:48:32 -07:00
|
|
|
gfxPoint
|
|
|
|
nsSVGFilterInstance::FilterSpaceToUserSpace(const gfxPoint& aPt) const
|
|
|
|
{
|
2012-06-16 13:23:48 -07:00
|
|
|
return gfxPoint(aPt.x * mFilterRegion.Width() / mFilterSpaceSize.width + mFilterRegion.X(),
|
|
|
|
aPt.y * mFilterRegion.Height() / mFilterSpaceSize.height + mFilterRegion.Y());
|
2011-09-14 07:48:32 -07:00
|
|
|
}
|
|
|
|
|
2009-07-23 01:35:59 -07:00
|
|
|
gfxMatrix
|
2008-07-22 19:15:27 -07:00
|
|
|
nsSVGFilterInstance::GetUserSpaceToFilterSpaceTransform() const
|
|
|
|
{
|
2012-06-16 13:23:48 -07:00
|
|
|
gfxFloat widthScale = mFilterSpaceSize.width / mFilterRegion.Width();
|
|
|
|
gfxFloat heightScale = mFilterSpaceSize.height / mFilterRegion.Height();
|
2009-07-23 01:35:59 -07:00
|
|
|
return gfxMatrix(widthScale, 0.0f,
|
|
|
|
0.0f, heightScale,
|
2012-06-16 13:23:48 -07:00
|
|
|
-mFilterRegion.X() * widthScale, -mFilterRegion.Y() * heightScale);
|
2008-07-22 19:15:27 -07:00
|
|
|
}
|
|
|
|
|
2008-07-13 18:31:34 -07:00
|
|
|
void
|
|
|
|
nsSVGFilterInstance::ComputeFilterPrimitiveSubregion(PrimitiveInfo* aPrimitive)
|
|
|
|
{
|
|
|
|
nsSVGFE* fE = aPrimitive->mFE;
|
|
|
|
|
2008-07-15 01:49:42 -07:00
|
|
|
gfxRect defaultFilterSubregion(0,0,0,0);
|
2008-07-13 18:31:34 -07:00
|
|
|
if (fE->SubregionIsUnionOfRegions()) {
|
2012-08-22 08:56:38 -07:00
|
|
|
for (uint32_t i = 0; i < aPrimitive->mInputs.Length(); ++i) {
|
2011-07-23 01:44:52 -07:00
|
|
|
defaultFilterSubregion =
|
2008-07-13 18:31:34 -07:00
|
|
|
defaultFilterSubregion.Union(
|
|
|
|
aPrimitive->mInputs[i]->mImage.mFilterPrimitiveSubregion);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
defaultFilterSubregion =
|
|
|
|
gfxRect(0, 0, mFilterSpaceSize.width, mFilterSpaceSize.height);
|
|
|
|
}
|
|
|
|
|
2008-09-10 17:24:16 -07:00
|
|
|
gfxRect feArea = nsSVGUtils::GetRelativeRect(mPrimitiveUnits,
|
|
|
|
&fE->mLengthAttributes[nsSVGFE::X], mTargetBBox, mTargetFrame);
|
|
|
|
gfxRect region = UserSpaceToFilterSpace(feArea);
|
2008-07-13 18:31:34 -07:00
|
|
|
|
2011-07-01 00:19:52 -07:00
|
|
|
if (!fE->mLengthAttributes[nsSVGFE::X].IsExplicitlySet())
|
2011-04-18 20:07:21 -07:00
|
|
|
region.x = defaultFilterSubregion.X();
|
2011-07-01 00:19:52 -07:00
|
|
|
if (!fE->mLengthAttributes[nsSVGFE::Y].IsExplicitlySet())
|
2011-04-18 20:07:21 -07:00
|
|
|
region.y = defaultFilterSubregion.Y();
|
2011-07-01 00:19:52 -07:00
|
|
|
if (!fE->mLengthAttributes[nsSVGFE::WIDTH].IsExplicitlySet())
|
2011-04-18 20:07:21 -07:00
|
|
|
region.width = defaultFilterSubregion.Width();
|
2011-07-01 00:19:52 -07:00
|
|
|
if (!fE->mLengthAttributes[nsSVGFE::HEIGHT].IsExplicitlySet())
|
2011-04-18 20:07:21 -07:00
|
|
|
region.height = defaultFilterSubregion.Height();
|
2008-07-13 18:31:34 -07:00
|
|
|
|
|
|
|
// We currently require filter primitive subregions to be pixel-aligned.
|
|
|
|
// Following the spec, any pixel partially in the region is included
|
|
|
|
// in the region.
|
|
|
|
region.RoundOut();
|
|
|
|
aPrimitive->mImage.mFilterPrimitiveSubregion = region;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsSVGFilterInstance::BuildSources()
|
|
|
|
{
|
|
|
|
gfxRect filterRegion = gfxRect(0, 0, mFilterSpaceSize.width, mFilterSpaceSize.height);
|
|
|
|
mSourceColorAlpha.mImage.mFilterPrimitiveSubregion = filterRegion;
|
|
|
|
mSourceAlpha.mImage.mFilterPrimitiveSubregion = filterRegion;
|
2012-08-05 12:10:21 -07:00
|
|
|
mFillPaint.mImage.mFilterPrimitiveSubregion = filterRegion;
|
|
|
|
mStrokePaint.mImage.mFilterPrimitiveSubregion = filterRegion;
|
2008-07-13 18:31:34 -07:00
|
|
|
|
|
|
|
nsIntRect sourceBoundsInt;
|
2009-06-11 08:21:03 -07:00
|
|
|
gfxRect sourceBounds = UserSpaceToFilterSpace(mTargetBBox);
|
|
|
|
sourceBounds.RoundOut();
|
|
|
|
// Detect possible float->int overflow
|
2010-11-24 01:35:21 -08:00
|
|
|
if (!gfxUtils::GfxRectToIntRect(sourceBounds, &sourceBoundsInt))
|
2009-06-11 08:21:03 -07:00
|
|
|
return NS_ERROR_FAILURE;
|
2011-09-17 01:29:51 -07:00
|
|
|
sourceBoundsInt.UnionRect(sourceBoundsInt, mTargetBounds);
|
2008-07-13 18:31:34 -07:00
|
|
|
|
|
|
|
mSourceColorAlpha.mResultBoundingBox = sourceBoundsInt;
|
|
|
|
mSourceAlpha.mResultBoundingBox = sourceBoundsInt;
|
2012-08-05 12:10:21 -07:00
|
|
|
mFillPaint.mResultBoundingBox = sourceBoundsInt;
|
|
|
|
mStrokePaint.mResultBoundingBox = sourceBoundsInt;
|
2008-07-13 18:31:34 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsSVGFilterInstance::BuildPrimitives()
|
|
|
|
{
|
|
|
|
// First build mFilterInfo. It's important that we don't change that
|
|
|
|
// array after we start storing pointers to its elements!
|
2012-03-29 01:41:37 -07:00
|
|
|
for (nsIContent* child = mFilterElement->nsINode::GetFirstChild();
|
|
|
|
child;
|
|
|
|
child = child->GetNextSibling()) {
|
2008-07-13 18:31:34 -07:00
|
|
|
nsRefPtr<nsSVGFE> primitive;
|
|
|
|
CallQueryInterface(child, (nsSVGFE**)getter_AddRefs(primitive));
|
|
|
|
if (!primitive)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
PrimitiveInfo* info = mPrimitives.AppendElement();
|
|
|
|
info->mFE = primitive;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now fill in all the links
|
|
|
|
nsTHashtable<ImageAnalysisEntry> imageTable;
|
|
|
|
imageTable.Init(10);
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
for (uint32_t i = 0; i < mPrimitives.Length(); ++i) {
|
2008-07-13 18:31:34 -07:00
|
|
|
PrimitiveInfo* info = &mPrimitives[i];
|
|
|
|
nsSVGFE* filter = info->mFE;
|
2009-01-21 16:56:51 -08:00
|
|
|
nsAutoTArray<nsSVGStringInfo,2> sources;
|
|
|
|
filter->GetSourceImageNames(sources);
|
2008-07-13 18:31:34 -07:00
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
for (uint32_t j=0; j<sources.Length(); ++j) {
|
2009-01-21 16:56:51 -08:00
|
|
|
nsAutoString str;
|
|
|
|
sources[j].mString->GetAnimValue(str, sources[j].mElement);
|
2008-07-13 18:31:34 -07:00
|
|
|
PrimitiveInfo* sourceInfo;
|
|
|
|
|
|
|
|
if (str.EqualsLiteral("SourceGraphic")) {
|
|
|
|
sourceInfo = &mSourceColorAlpha;
|
|
|
|
} else if (str.EqualsLiteral("SourceAlpha")) {
|
|
|
|
sourceInfo = &mSourceAlpha;
|
2012-08-05 12:10:21 -07:00
|
|
|
} else if (str.EqualsLiteral("FillPaint")) {
|
|
|
|
sourceInfo = &mFillPaint;
|
|
|
|
} else if (str.EqualsLiteral("StrokePaint")) {
|
|
|
|
sourceInfo = &mStrokePaint;
|
2008-07-13 18:31:34 -07:00
|
|
|
} else if (str.EqualsLiteral("BackgroundImage") ||
|
2012-08-05 12:10:21 -07:00
|
|
|
str.EqualsLiteral("BackgroundAlpha")) {
|
2008-07-13 18:31:34 -07:00
|
|
|
return NS_ERROR_NOT_IMPLEMENTED;
|
|
|
|
} else if (str.EqualsLiteral("")) {
|
|
|
|
sourceInfo = i == 0 ? &mSourceColorAlpha : &mPrimitives[i - 1];
|
|
|
|
} else {
|
|
|
|
ImageAnalysisEntry* entry = imageTable.GetEntry(str);
|
|
|
|
if (!entry)
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
sourceInfo = entry->mInfo;
|
|
|
|
}
|
|
|
|
|
|
|
|
++sourceInfo->mImageUsers;
|
|
|
|
info->mInputs.AppendElement(sourceInfo);
|
|
|
|
}
|
|
|
|
|
|
|
|
ComputeFilterPrimitiveSubregion(info);
|
|
|
|
|
2009-01-21 16:56:51 -08:00
|
|
|
nsAutoString str;
|
|
|
|
filter->GetResultImageName().GetAnimValue(str, filter);
|
|
|
|
|
|
|
|
ImageAnalysisEntry* entry = imageTable.PutEntry(str);
|
2008-07-13 18:31:34 -07:00
|
|
|
if (entry) {
|
|
|
|
entry->mInfo = info;
|
|
|
|
}
|
|
|
|
|
|
|
|
// The last filter primitive is the filter result, so mark it used
|
|
|
|
if (i == mPrimitives.Length() - 1) {
|
|
|
|
++info->mImageUsers;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsSVGFilterInstance::ComputeResultBoundingBoxes()
|
|
|
|
{
|
2012-08-22 08:56:38 -07:00
|
|
|
for (uint32_t i = 0; i < mPrimitives.Length(); ++i) {
|
2008-07-13 18:31:34 -07:00
|
|
|
PrimitiveInfo* info = &mPrimitives[i];
|
|
|
|
nsAutoTArray<nsIntRect,2> sourceBBoxes;
|
2012-08-22 08:56:38 -07:00
|
|
|
for (uint32_t j = 0; j < info->mInputs.Length(); ++j) {
|
2008-07-13 18:31:34 -07:00
|
|
|
sourceBBoxes.AppendElement(info->mInputs[j]->mResultBoundingBox);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsIntRect resultBBox = info->mFE->ComputeTargetBBox(sourceBBoxes, *this);
|
|
|
|
ClipToFilterSpace(&resultBBox);
|
|
|
|
nsSVGUtils::ClipToGfxRect(&resultBBox, info->mImage.mFilterPrimitiveSubregion);
|
|
|
|
info->mResultBoundingBox = resultBBox;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-07-22 19:15:27 -07:00
|
|
|
void
|
|
|
|
nsSVGFilterInstance::ComputeResultChangeBoxes()
|
|
|
|
{
|
2012-08-22 08:56:38 -07:00
|
|
|
for (uint32_t i = 0; i < mPrimitives.Length(); ++i) {
|
2008-07-22 19:15:27 -07:00
|
|
|
PrimitiveInfo* info = &mPrimitives[i];
|
|
|
|
nsAutoTArray<nsIntRect,2> sourceChangeBoxes;
|
2012-08-22 08:56:38 -07:00
|
|
|
for (uint32_t j = 0; j < info->mInputs.Length(); ++j) {
|
2008-07-22 19:15:27 -07:00
|
|
|
sourceChangeBoxes.AppendElement(info->mInputs[j]->mResultChangeBox);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsIntRect resultChangeBox = info->mFE->ComputeChangeBBox(sourceChangeBoxes, *this);
|
|
|
|
info->mResultChangeBox.IntersectRect(resultChangeBox, info->mResultBoundingBox);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-07-13 18:31:34 -07:00
|
|
|
void
|
|
|
|
nsSVGFilterInstance::ComputeNeededBoxes()
|
|
|
|
{
|
|
|
|
if (mPrimitives.IsEmpty())
|
|
|
|
return;
|
|
|
|
|
2008-07-22 19:15:27 -07:00
|
|
|
// In the end, we need whatever the final filter primitive will draw that
|
|
|
|
// intersects the destination dirty area.
|
2008-07-14 20:08:47 -07:00
|
|
|
mPrimitives[mPrimitives.Length() - 1].mResultNeededBox.IntersectRect(
|
2012-06-16 13:23:48 -07:00
|
|
|
mPrimitives[mPrimitives.Length() - 1].mResultBoundingBox, mPostFilterDirtyRect);
|
2008-07-13 18:31:34 -07:00
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
for (int32_t i = mPrimitives.Length() - 1; i >= 0; --i) {
|
2008-07-13 18:31:34 -07:00
|
|
|
PrimitiveInfo* info = &mPrimitives[i];
|
|
|
|
nsAutoTArray<nsIntRect,2> sourceBBoxes;
|
2012-08-22 08:56:38 -07:00
|
|
|
for (uint32_t j = 0; j < info->mInputs.Length(); ++j) {
|
2008-07-13 18:31:34 -07:00
|
|
|
sourceBBoxes.AppendElement(info->mInputs[j]->mResultBoundingBox);
|
|
|
|
}
|
|
|
|
|
|
|
|
info->mFE->ComputeNeededSourceBBoxes(
|
|
|
|
info->mResultNeededBox, sourceBBoxes, *this);
|
|
|
|
// Update each source with the rectangle we need
|
2012-08-22 08:56:38 -07:00
|
|
|
for (uint32_t j = 0; j < info->mInputs.Length(); ++j) {
|
2008-07-13 18:31:34 -07:00
|
|
|
nsIntRect* r = &info->mInputs[j]->mResultNeededBox;
|
|
|
|
r->UnionRect(*r, sourceBBoxes[j]);
|
|
|
|
// Keep everything within the filter effects region
|
|
|
|
ClipToFilterSpace(r);
|
|
|
|
nsSVGUtils::ClipToGfxRect(r, info->mInputs[j]->mImage.mFilterPrimitiveSubregion);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
nsIntRect
|
|
|
|
nsSVGFilterInstance::ComputeUnionOfAllNeededBoxes()
|
|
|
|
{
|
|
|
|
nsIntRect r;
|
|
|
|
r.UnionRect(mSourceColorAlpha.mResultNeededBox,
|
|
|
|
mSourceAlpha.mResultNeededBox);
|
2012-08-05 12:10:21 -07:00
|
|
|
r.UnionRect(r, mFillPaint.mResultNeededBox);
|
|
|
|
r.UnionRect(r, mStrokePaint.mResultNeededBox);
|
2012-08-22 08:56:38 -07:00
|
|
|
for (uint32_t i = 0; i < mPrimitives.Length(); ++i) {
|
2008-07-13 18:31:34 -07:00
|
|
|
r.UnionRect(r, mPrimitives[i].mResultNeededBox);
|
|
|
|
}
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2012-08-05 12:10:21 -07:00
|
|
|
nsresult
|
|
|
|
nsSVGFilterInstance::BuildSourcePaint(PrimitiveInfo *aPrimitive)
|
|
|
|
{
|
|
|
|
NS_ASSERTION(aPrimitive->mImageUsers > 0, "Some user must have needed this");
|
|
|
|
|
|
|
|
nsRefPtr<gfxImageSurface> image = CreateImage();
|
|
|
|
if (!image)
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
|
|
|
|
nsRefPtr<gfxASurface> offscreen =
|
|
|
|
gfxPlatform::GetPlatform()->CreateOffscreenSurface(
|
|
|
|
gfxIntSize(mSurfaceRect.width, mSurfaceRect.height),
|
|
|
|
gfxASurface::CONTENT_COLOR_ALPHA);
|
|
|
|
if (!offscreen || offscreen->CairoStatus())
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
offscreen->SetDeviceOffset(gfxPoint(-mSurfaceRect.x, -mSurfaceRect.y));
|
|
|
|
|
|
|
|
nsRenderingContext tmpCtx;
|
|
|
|
tmpCtx.Init(mTargetFrame->PresContext()->DeviceContext(), offscreen);
|
|
|
|
|
|
|
|
gfxRect r = aPrimitive->mImage.mFilterPrimitiveSubregion;
|
|
|
|
gfxMatrix m = GetUserSpaceToFilterSpaceTransform();
|
|
|
|
m.Invert();
|
|
|
|
r = m.TransformBounds(r);
|
|
|
|
|
|
|
|
gfxMatrix deviceToFilterSpace = GetFilterSpaceToDeviceSpaceTransform().Invert();
|
|
|
|
gfxContext *gfx = tmpCtx.ThebesContext();
|
|
|
|
gfx->Multiply(deviceToFilterSpace);
|
|
|
|
|
|
|
|
gfx->Save();
|
|
|
|
|
|
|
|
gfxMatrix matrix =
|
|
|
|
nsSVGUtils::GetCanvasTM(mTargetFrame, nsISVGChildFrame::FOR_PAINTING);
|
|
|
|
if (!matrix.IsSingular()) {
|
|
|
|
gfx->Multiply(matrix);
|
|
|
|
gfx->Rectangle(r);
|
|
|
|
if ((aPrimitive == &mFillPaint &&
|
2012-08-10 04:13:43 -07:00
|
|
|
nsSVGUtils::SetupCairoFillPaint(mTargetFrame, gfx)) ||
|
2012-08-05 12:10:21 -07:00
|
|
|
(aPrimitive == &mStrokePaint &&
|
2012-08-10 04:13:43 -07:00
|
|
|
nsSVGUtils::SetupCairoStrokePaint(mTargetFrame, gfx))) {
|
2012-08-05 12:10:21 -07:00
|
|
|
gfx->Fill();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
gfx->Restore();
|
|
|
|
|
|
|
|
gfxContext copyContext(image);
|
|
|
|
copyContext.SetSource(offscreen);
|
|
|
|
copyContext.Paint();
|
|
|
|
|
|
|
|
aPrimitive->mImage.mImage = image;
|
|
|
|
// color model is PREMULTIPLIED SRGB by default.
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsSVGFilterInstance::BuildSourcePaints()
|
|
|
|
{
|
|
|
|
nsresult rv = NS_OK;
|
|
|
|
|
|
|
|
if (!mFillPaint.mResultNeededBox.IsEmpty()) {
|
|
|
|
rv = BuildSourcePaint(&mFillPaint);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!mStrokePaint.mResultNeededBox.IsEmpty()) {
|
|
|
|
rv = BuildSourcePaint(&mStrokePaint);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2008-07-13 18:31:34 -07:00
|
|
|
nsresult
|
|
|
|
nsSVGFilterInstance::BuildSourceImages()
|
|
|
|
{
|
2008-09-10 17:24:16 -07:00
|
|
|
nsIntRect neededRect;
|
|
|
|
neededRect.UnionRect(mSourceColorAlpha.mResultNeededBox,
|
|
|
|
mSourceAlpha.mResultNeededBox);
|
|
|
|
if (neededRect.IsEmpty())
|
2008-07-13 18:31:34 -07:00
|
|
|
return NS_OK;
|
|
|
|
|
|
|
|
nsRefPtr<gfxImageSurface> sourceColorAlpha = CreateImage();
|
|
|
|
if (!sourceColorAlpha)
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
|
2008-09-10 17:24:16 -07:00
|
|
|
{
|
|
|
|
// Paint to an offscreen surface first, then copy it to an image
|
|
|
|
// surface. This can be faster especially when the stuff we're painting
|
|
|
|
// contains native themes.
|
|
|
|
nsRefPtr<gfxASurface> offscreen =
|
|
|
|
gfxPlatform::GetPlatform()->CreateOffscreenSurface(
|
|
|
|
gfxIntSize(mSurfaceRect.width, mSurfaceRect.height),
|
2010-09-16 14:34:53 -07:00
|
|
|
gfxASurface::CONTENT_COLOR_ALPHA);
|
2008-09-10 17:24:16 -07:00
|
|
|
if (!offscreen || offscreen->CairoStatus())
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
offscreen->SetDeviceOffset(gfxPoint(-mSurfaceRect.x, -mSurfaceRect.y));
|
2012-03-02 00:28:59 -08:00
|
|
|
|
|
|
|
nsRenderingContext tmpCtx;
|
|
|
|
tmpCtx.Init(mTargetFrame->PresContext()->DeviceContext(), offscreen);
|
2008-10-16 00:55:10 -07:00
|
|
|
|
2008-09-10 17:24:16 -07:00
|
|
|
gfxRect r(neededRect.x, neededRect.y, neededRect.width, neededRect.height);
|
2012-08-05 12:10:21 -07:00
|
|
|
gfxMatrix m = GetUserSpaceToFilterSpaceTransform();
|
2008-09-10 17:24:16 -07:00
|
|
|
m.Invert();
|
|
|
|
r = m.TransformBounds(r);
|
|
|
|
r.RoundOut();
|
|
|
|
nsIntRect dirty;
|
2010-11-24 01:35:21 -08:00
|
|
|
if (!gfxUtils::GfxRectToIntRect(r, &dirty))
|
|
|
|
return NS_ERROR_FAILURE;
|
2008-10-16 00:55:10 -07:00
|
|
|
|
2009-02-09 16:54:28 -08:00
|
|
|
// SVG graphics paint to device space, so we need to set an initial device
|
|
|
|
// space to filter space transform on the gfxContext that SourceGraphic
|
|
|
|
// and SourceAlpha will paint to.
|
|
|
|
//
|
|
|
|
// (In theory it would be better to minimize error by having filtered SVG
|
|
|
|
// graphics temporarily paint to user space when painting the sources and
|
|
|
|
// only set a user space to filter space transform on the gfxContext
|
2011-08-15 11:41:57 -07:00
|
|
|
// (since that would eliminate the transform multiplications from user
|
2009-02-09 16:54:28 -08:00
|
|
|
// space to device space and back again). However, that would make the
|
|
|
|
// code more complex while being hard to get right without introducing
|
|
|
|
// subtle bugs, and in practice it probably makes no real difference.)
|
2009-07-23 01:35:59 -07:00
|
|
|
gfxMatrix deviceToFilterSpace = GetFilterSpaceToDeviceSpaceTransform().Invert();
|
2012-03-02 00:28:59 -08:00
|
|
|
tmpCtx.ThebesContext()->Multiply(deviceToFilterSpace);
|
|
|
|
mPaintCallback->Paint(&tmpCtx, mTargetFrame, &dirty);
|
2008-09-10 17:24:16 -07:00
|
|
|
|
|
|
|
gfxContext copyContext(sourceColorAlpha);
|
|
|
|
copyContext.SetSource(offscreen);
|
|
|
|
copyContext.Paint();
|
|
|
|
}
|
2008-10-16 00:55:10 -07:00
|
|
|
|
2008-07-13 18:31:34 -07:00
|
|
|
if (!mSourceColorAlpha.mResultNeededBox.IsEmpty()) {
|
|
|
|
NS_ASSERTION(mSourceColorAlpha.mImageUsers > 0, "Some user must have needed this");
|
|
|
|
mSourceColorAlpha.mImage.mImage = sourceColorAlpha;
|
|
|
|
// color model is PREMULTIPLIED SRGB by default.
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!mSourceAlpha.mResultNeededBox.IsEmpty()) {
|
|
|
|
NS_ASSERTION(mSourceAlpha.mImageUsers > 0, "Some user must have needed this");
|
|
|
|
|
|
|
|
mSourceAlpha.mImage.mImage = CreateImage();
|
|
|
|
if (!mSourceAlpha.mImage.mImage)
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
// color model is PREMULTIPLIED SRGB by default.
|
|
|
|
|
|
|
|
// Clear the color channel
|
2012-08-22 08:56:38 -07:00
|
|
|
const uint32_t* src = reinterpret_cast<uint32_t*>(sourceColorAlpha->Data());
|
|
|
|
uint32_t* dest = reinterpret_cast<uint32_t*>(mSourceAlpha.mImage.mImage->Data());
|
|
|
|
for (int32_t y = 0; y < mSurfaceRect.height; y++) {
|
|
|
|
uint32_t rowOffset = (mSourceAlpha.mImage.mImage->Stride()*y) >> 2;
|
|
|
|
for (int32_t x = 0; x < mSurfaceRect.width; x++) {
|
2008-07-13 18:31:34 -07:00
|
|
|
dest[rowOffset + x] = src[rowOffset + x] & 0xFF000000U;
|
|
|
|
}
|
|
|
|
}
|
2011-10-17 07:59:28 -07:00
|
|
|
mSourceAlpha.mImage.mConstantColorChannels = true;
|
2008-07-13 18:31:34 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsSVGFilterInstance::EnsureColorModel(PrimitiveInfo* aPrimitive,
|
|
|
|
ColorModel aColorModel)
|
|
|
|
{
|
|
|
|
ColorModel currentModel = aPrimitive->mImage.mColorModel;
|
|
|
|
if (aColorModel == currentModel)
|
|
|
|
return;
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
uint8_t* data = aPrimitive->mImage.mImage->Data();
|
|
|
|
int32_t stride = aPrimitive->mImage.mImage->Stride();
|
2008-07-13 18:31:34 -07:00
|
|
|
|
|
|
|
nsIntRect r = aPrimitive->mResultNeededBox - mSurfaceRect.TopLeft();
|
|
|
|
|
|
|
|
if (currentModel.mAlphaChannel == ColorModel::PREMULTIPLIED) {
|
|
|
|
nsSVGUtils::UnPremultiplyImageDataAlpha(data, stride, r);
|
|
|
|
}
|
|
|
|
if (aColorModel.mColorSpace != currentModel.mColorSpace) {
|
|
|
|
if (aColorModel.mColorSpace == ColorModel::LINEAR_RGB) {
|
|
|
|
nsSVGUtils::ConvertImageDataToLinearRGB(data, stride, r);
|
|
|
|
} else {
|
|
|
|
nsSVGUtils::ConvertImageDataFromLinearRGB(data, stride, r);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (aColorModel.mAlphaChannel == ColorModel::PREMULTIPLIED) {
|
|
|
|
nsSVGUtils::PremultiplyImageDataAlpha(data, stride, r);
|
|
|
|
}
|
|
|
|
aPrimitive->mImage.mColorModel = aColorModel;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsSVGFilterInstance::Render(gfxASurface** aOutput)
|
|
|
|
{
|
2012-07-30 07:20:58 -07:00
|
|
|
*aOutput = nullptr;
|
2008-07-13 18:31:34 -07:00
|
|
|
|
|
|
|
nsresult rv = BuildSources();
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
|
|
|
|
rv = BuildPrimitives();
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
|
|
|
|
if (mPrimitives.IsEmpty()) {
|
|
|
|
// Nothing should be rendered.
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
ComputeResultBoundingBoxes();
|
|
|
|
ComputeNeededBoxes();
|
|
|
|
// For now, we make all surface sizes equal to the union of the
|
|
|
|
// bounding boxes needed for each temporary image
|
|
|
|
mSurfaceRect = ComputeUnionOfAllNeededBoxes();
|
|
|
|
|
|
|
|
rv = BuildSourceImages();
|
2012-08-05 12:10:21 -07:00
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
rv = BuildSourcePaints();
|
2008-07-13 18:31:34 -07:00
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
for (uint32_t i = 0; i < mPrimitives.Length(); ++i) {
|
2008-07-13 18:31:34 -07:00
|
|
|
PrimitiveInfo* primitive = &mPrimitives[i];
|
|
|
|
|
|
|
|
nsIntRect dataRect;
|
|
|
|
// Since mResultNeededBox is clipped to the filter primitive subregion,
|
|
|
|
// dataRect is also limited to the filter primitive subregion.
|
|
|
|
if (!dataRect.IntersectRect(primitive->mResultNeededBox, mSurfaceRect))
|
|
|
|
continue;
|
|
|
|
dataRect -= mSurfaceRect.TopLeft();
|
|
|
|
|
|
|
|
primitive->mImage.mImage = CreateImage();
|
|
|
|
if (!primitive->mImage.mImage)
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
|
|
|
|
nsAutoTArray<const Image*,2> inputs;
|
2012-08-22 08:56:38 -07:00
|
|
|
for (uint32_t j = 0; j < primitive->mInputs.Length(); ++j) {
|
2008-07-13 18:31:34 -07:00
|
|
|
PrimitiveInfo* input = primitive->mInputs[j];
|
|
|
|
|
|
|
|
if (!input->mImage.mImage) {
|
|
|
|
// This image data is not really going to be used, but we'd better
|
|
|
|
// have an image object here so the filter primitive doesn't die.
|
2008-12-12 00:14:26 -08:00
|
|
|
input->mImage.mImage = CreateImage();
|
2008-07-13 18:31:34 -07:00
|
|
|
if (!input->mImage.mImage)
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
|
|
|
|
ColorModel desiredColorModel =
|
|
|
|
primitive->mFE->GetInputColorModel(this, j, &input->mImage);
|
2010-10-26 01:23:41 -07:00
|
|
|
if (j == 0) {
|
|
|
|
// the output colour model is whatever in1 is if there is an in1
|
|
|
|
primitive->mImage.mColorModel = desiredColorModel;
|
|
|
|
}
|
2008-07-13 18:31:34 -07:00
|
|
|
EnsureColorModel(input, desiredColorModel);
|
|
|
|
NS_ASSERTION(input->mImage.mImage->Stride() == primitive->mImage.mImage->Stride(),
|
|
|
|
"stride mismatch");
|
|
|
|
inputs.AppendElement(&input->mImage);
|
|
|
|
}
|
|
|
|
|
2010-10-26 01:23:41 -07:00
|
|
|
if (primitive->mInputs.Length() == 0) {
|
|
|
|
primitive->mImage.mColorModel = primitive->mFE->GetOutputColorModel(this);
|
|
|
|
}
|
2008-07-13 18:31:34 -07:00
|
|
|
|
|
|
|
rv = primitive->mFE->Filter(this, inputs, &primitive->mImage, dataRect);
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
for (uint32_t j = 0; j < primitive->mInputs.Length(); ++j) {
|
2008-07-13 18:31:34 -07:00
|
|
|
PrimitiveInfo* input = primitive->mInputs[j];
|
|
|
|
--input->mImageUsers;
|
|
|
|
NS_ASSERTION(input->mImageUsers >= 0, "Bad mImageUsers tracking");
|
|
|
|
if (input->mImageUsers == 0) {
|
|
|
|
// Release the image, it's no longer needed
|
2012-07-30 07:20:58 -07:00
|
|
|
input->mImage.mImage = nullptr;
|
2008-07-13 18:31:34 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
PrimitiveInfo* result = &mPrimitives[mPrimitives.Length() - 1];
|
|
|
|
ColorModel premulSRGB; // default
|
|
|
|
EnsureColorModel(result, premulSRGB);
|
2012-07-30 07:20:58 -07:00
|
|
|
gfxImageSurface* surf = nullptr;
|
2008-07-13 18:31:34 -07:00
|
|
|
result->mImage.mImage.swap(surf);
|
|
|
|
*aOutput = surf;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2008-07-22 19:15:27 -07:00
|
|
|
|
|
|
|
nsresult
|
2012-06-16 13:23:48 -07:00
|
|
|
nsSVGFilterInstance::ComputePostFilterDirtyRect(nsIntRect* aPostFilterDirtyRect)
|
2008-07-22 19:15:27 -07:00
|
|
|
{
|
2012-06-16 13:23:48 -07:00
|
|
|
*aPostFilterDirtyRect = nsIntRect();
|
2008-07-22 19:15:27 -07:00
|
|
|
|
|
|
|
nsresult rv = BuildSources();
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
|
|
|
|
rv = BuildPrimitives();
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
|
|
|
|
if (mPrimitives.IsEmpty()) {
|
|
|
|
// Nothing should be rendered, so nothing can be dirty.
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
ComputeResultBoundingBoxes();
|
|
|
|
|
2012-06-16 13:23:48 -07:00
|
|
|
mSourceColorAlpha.mResultChangeBox = mPreFilterDirtyRect;
|
|
|
|
mSourceAlpha.mResultChangeBox = mPreFilterDirtyRect;
|
2008-07-22 19:15:27 -07:00
|
|
|
ComputeResultChangeBoxes();
|
|
|
|
|
|
|
|
PrimitiveInfo* result = &mPrimitives[mPrimitives.Length() - 1];
|
2012-06-16 13:23:48 -07:00
|
|
|
*aPostFilterDirtyRect = result->mResultChangeBox;
|
2008-07-22 19:15:27 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2008-09-10 17:24:16 -07:00
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsSVGFilterInstance::ComputeSourceNeededRect(nsIntRect* aDirty)
|
|
|
|
{
|
|
|
|
nsresult rv = BuildSources();
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
|
|
|
|
rv = BuildPrimitives();
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
|
|
|
|
if (mPrimitives.IsEmpty()) {
|
|
|
|
// Nothing should be rendered, so nothing is needed.
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
ComputeResultBoundingBoxes();
|
|
|
|
ComputeNeededBoxes();
|
|
|
|
aDirty->UnionRect(mSourceColorAlpha.mResultNeededBox,
|
|
|
|
mSourceAlpha.mResultNeededBox);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsSVGFilterInstance::ComputeOutputBBox(nsIntRect* aDirty)
|
|
|
|
{
|
|
|
|
nsresult rv = BuildSources();
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
|
|
|
|
rv = BuildPrimitives();
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
|
|
|
|
if (mPrimitives.IsEmpty()) {
|
|
|
|
// Nothing should be rendered.
|
|
|
|
*aDirty = nsIntRect();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
ComputeResultBoundingBoxes();
|
|
|
|
|
|
|
|
PrimitiveInfo* result = &mPrimitives[mPrimitives.Length() - 1];
|
|
|
|
*aDirty = result->mResultBoundingBox;
|
|
|
|
return NS_OK;
|
|
|
|
}
|