2014-02-24 19:22:58 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* 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/. */
|
|
|
|
|
|
|
|
// Main header first:
|
|
|
|
#include "nsFilterInstance.h"
|
|
|
|
|
|
|
|
// Keep others in (case-insensitive) order:
|
|
|
|
#include "gfxPlatform.h"
|
|
|
|
#include "gfxUtils.h"
|
2014-09-10 21:29:35 +04:00
|
|
|
#include "mozilla/gfx/Helpers.h"
|
2014-02-24 19:22:58 +04:00
|
|
|
#include "nsISVGChildFrame.h"
|
|
|
|
#include "nsRenderingContext.h"
|
2014-08-06 21:48:47 +04:00
|
|
|
#include "nsCSSFilterInstance.h"
|
2014-02-24 19:22:58 +04:00
|
|
|
#include "nsSVGFilterInstance.h"
|
|
|
|
#include "nsSVGFilterPaintCallback.h"
|
|
|
|
#include "nsSVGUtils.h"
|
|
|
|
#include "SVGContentUtils.h"
|
|
|
|
#include "FilterSupport.h"
|
|
|
|
#include "gfx2DGlue.h"
|
|
|
|
|
|
|
|
using namespace mozilla;
|
|
|
|
using namespace mozilla::dom;
|
|
|
|
using namespace mozilla::gfx;
|
|
|
|
|
|
|
|
nsresult
|
2014-08-29 23:42:07 +04:00
|
|
|
nsFilterInstance::PaintFilteredFrame(nsIFrame *aFilteredFrame,
|
|
|
|
nsRenderingContext *aContext,
|
|
|
|
const gfxMatrix& aTransform,
|
2014-02-24 19:22:58 +04:00
|
|
|
nsSVGFilterPaintCallback *aPaintCallback,
|
2014-08-29 23:42:07 +04:00
|
|
|
const nsRegion *aDirtyArea)
|
2014-02-24 19:22:58 +04:00
|
|
|
{
|
2014-08-29 23:42:07 +04:00
|
|
|
nsFilterInstance instance(aFilteredFrame, aPaintCallback, aTransform,
|
|
|
|
aDirtyArea, nullptr, nullptr, nullptr);
|
2014-02-24 19:22:58 +04:00
|
|
|
if (!instance.IsInitialized()) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
return instance.Render(aContext->ThebesContext());
|
|
|
|
}
|
|
|
|
|
2014-04-24 12:25:17 +04:00
|
|
|
nsRegion
|
2014-02-24 19:22:58 +04:00
|
|
|
nsFilterInstance::GetPostFilterDirtyArea(nsIFrame *aFilteredFrame,
|
2014-04-24 12:25:17 +04:00
|
|
|
const nsRegion& aPreFilterDirtyRegion)
|
2014-02-24 19:22:58 +04:00
|
|
|
{
|
2014-04-24 12:25:17 +04:00
|
|
|
if (aPreFilterDirtyRegion.IsEmpty()) {
|
|
|
|
return nsRegion();
|
2014-02-24 19:22:58 +04:00
|
|
|
}
|
|
|
|
|
2014-08-29 23:42:07 +04:00
|
|
|
gfxMatrix unused; // aPaintTransform arg not used since we're not painting
|
|
|
|
nsFilterInstance instance(aFilteredFrame, nullptr, unused, nullptr,
|
2014-04-24 12:25:17 +04:00
|
|
|
&aPreFilterDirtyRegion);
|
2014-02-24 19:22:58 +04:00
|
|
|
if (!instance.IsInitialized()) {
|
2014-04-24 12:25:17 +04:00
|
|
|
return nsRegion();
|
2014-02-24 19:22:58 +04:00
|
|
|
}
|
2014-09-04 21:31:03 +04:00
|
|
|
|
2014-02-24 19:22:58 +04: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.
|
2014-09-04 21:31:03 +04:00
|
|
|
return instance.ComputePostFilterDirtyRegion();
|
2014-02-24 19:22:58 +04:00
|
|
|
}
|
|
|
|
|
2014-04-24 12:25:17 +04:00
|
|
|
nsRegion
|
2014-02-24 19:22:58 +04:00
|
|
|
nsFilterInstance::GetPreFilterNeededArea(nsIFrame *aFilteredFrame,
|
2014-04-24 12:25:17 +04:00
|
|
|
const nsRegion& aPostFilterDirtyRegion)
|
2014-02-24 19:22:58 +04:00
|
|
|
{
|
2014-08-29 23:42:07 +04:00
|
|
|
gfxMatrix unused; // aPaintTransform arg not used since we're not painting
|
|
|
|
nsFilterInstance instance(aFilteredFrame, nullptr, unused,
|
|
|
|
&aPostFilterDirtyRegion);
|
2014-02-24 19:22:58 +04:00
|
|
|
if (!instance.IsInitialized()) {
|
|
|
|
return nsRect();
|
|
|
|
}
|
2014-09-04 21:31:03 +04:00
|
|
|
|
2014-02-24 19:22:58 +04:00
|
|
|
// Now we can ask the instance to compute the area of the source
|
|
|
|
// that's needed.
|
2014-09-04 21:31:03 +04:00
|
|
|
return instance.ComputeSourceNeededRect();
|
2014-02-24 19:22:58 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
nsRect
|
|
|
|
nsFilterInstance::GetPostFilterBounds(nsIFrame *aFilteredFrame,
|
|
|
|
const gfxRect *aOverrideBBox,
|
|
|
|
const nsRect *aPreFilterBounds)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(!(aFilteredFrame->GetStateBits() & NS_FRAME_SVG_LAYOUT) ||
|
|
|
|
!(aFilteredFrame->GetStateBits() & NS_FRAME_IS_NONDISPLAY),
|
|
|
|
"Non-display SVG do not maintain visual overflow rects");
|
|
|
|
|
2014-04-24 12:25:17 +04:00
|
|
|
nsRegion preFilterRegion;
|
|
|
|
nsRegion* preFilterRegionPtr = nullptr;
|
|
|
|
if (aPreFilterBounds) {
|
|
|
|
preFilterRegion = *aPreFilterBounds;
|
|
|
|
preFilterRegionPtr = &preFilterRegion;
|
|
|
|
}
|
2014-09-04 21:31:03 +04:00
|
|
|
|
2014-08-29 23:42:07 +04:00
|
|
|
gfxMatrix unused; // aPaintTransform arg not used since we're not painting
|
|
|
|
nsFilterInstance instance(aFilteredFrame, nullptr, unused, nullptr,
|
2014-04-24 12:25:17 +04:00
|
|
|
preFilterRegionPtr, aPreFilterBounds,
|
2014-02-24 19:22:58 +04:00
|
|
|
aOverrideBBox);
|
|
|
|
if (!instance.IsInitialized()) {
|
|
|
|
return nsRect();
|
|
|
|
}
|
2014-09-04 21:31:03 +04:00
|
|
|
|
|
|
|
return instance.ComputePostFilterExtents();
|
2014-02-24 19:22:58 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
nsFilterInstance::nsFilterInstance(nsIFrame *aTargetFrame,
|
|
|
|
nsSVGFilterPaintCallback *aPaintCallback,
|
2014-08-29 23:42:07 +04:00
|
|
|
const gfxMatrix& aPaintTransform,
|
2014-04-24 12:25:17 +04:00
|
|
|
const nsRegion *aPostFilterDirtyRegion,
|
|
|
|
const nsRegion *aPreFilterDirtyRegion,
|
2014-02-24 19:22:58 +04:00
|
|
|
const nsRect *aPreFilterVisualOverflowRectOverride,
|
2014-08-29 23:42:07 +04:00
|
|
|
const gfxRect *aOverrideBBox)
|
|
|
|
: mTargetFrame(aTargetFrame)
|
|
|
|
, mPaintCallback(aPaintCallback)
|
|
|
|
, mPaintTransform(aPaintTransform)
|
|
|
|
, mInitialized(false)
|
|
|
|
{
|
2014-02-24 19:22:58 +04:00
|
|
|
|
|
|
|
mTargetBBox = aOverrideBBox ?
|
|
|
|
*aOverrideBBox : nsSVGUtils::GetBBox(mTargetFrame);
|
|
|
|
|
2014-08-06 21:48:47 +04:00
|
|
|
// Compute user space to filter space transforms.
|
2014-03-27 03:21:48 +04:00
|
|
|
nsresult rv = ComputeUserSpaceToFilterSpaceScale();
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-08-06 21:48:47 +04:00
|
|
|
gfxRect targetBBoxInFilterSpace = UserSpaceToFilterSpace(mTargetBBox);
|
|
|
|
targetBBoxInFilterSpace.RoundOut();
|
|
|
|
if (!gfxUtils::GfxRectToIntRect(targetBBoxInFilterSpace, &mTargetBBoxInFilterSpace)) {
|
|
|
|
// The target's bbox is way too big if there is float->int overflow.
|
2014-02-24 19:22:58 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get various transforms:
|
|
|
|
|
2014-03-27 03:21:48 +04:00
|
|
|
gfxMatrix filterToUserSpace(mFilterSpaceToUserSpaceScale.width, 0.0f,
|
|
|
|
0.0f, mFilterSpaceToUserSpaceScale.height,
|
|
|
|
0.0f, 0.0f);
|
2014-02-24 19:22:58 +04:00
|
|
|
|
|
|
|
// Only used (so only set) when we paint:
|
|
|
|
if (mPaintCallback) {
|
2014-08-29 23:42:07 +04:00
|
|
|
mFilterSpaceToDeviceSpaceTransform = filterToUserSpace * mPaintTransform;
|
2014-02-24 19:22:58 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
mFilterSpaceToFrameSpaceInCSSPxTransform =
|
|
|
|
filterToUserSpace * GetUserSpaceToFrameSpaceInCSSPxTransform();
|
|
|
|
// mFilterSpaceToFrameSpaceInCSSPxTransform is always invertible
|
|
|
|
mFrameSpaceInCSSPxToFilterSpaceTransform =
|
|
|
|
mFilterSpaceToFrameSpaceInCSSPxTransform;
|
|
|
|
mFrameSpaceInCSSPxToFilterSpaceTransform.Invert();
|
|
|
|
|
2014-08-06 21:48:47 +04:00
|
|
|
// Build the filter graph.
|
|
|
|
rv = BuildPrimitives();
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mPrimitiveDescriptions.IsEmpty()) {
|
|
|
|
// Nothing should be rendered.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Convert the passed in rects from frame space to filter space:
|
2014-04-24 12:25:17 +04:00
|
|
|
mPostFilterDirtyRegion = FrameSpaceToFilterSpace(aPostFilterDirtyRegion);
|
|
|
|
mPreFilterDirtyRegion = FrameSpaceToFilterSpace(aPreFilterDirtyRegion);
|
2014-02-24 19:22:58 +04:00
|
|
|
if (aPreFilterVisualOverflowRectOverride) {
|
2014-08-01 04:37:35 +04:00
|
|
|
mTargetBounds =
|
2014-02-24 19:22:58 +04:00
|
|
|
FrameSpaceToFilterSpace(aPreFilterVisualOverflowRectOverride);
|
|
|
|
} else {
|
|
|
|
nsRect preFilterVOR = mTargetFrame->GetPreEffectsVisualOverflowRect();
|
|
|
|
mTargetBounds = FrameSpaceToFilterSpace(&preFilterVOR);
|
|
|
|
}
|
|
|
|
|
|
|
|
mInitialized = true;
|
|
|
|
}
|
|
|
|
|
2014-03-27 03:21:48 +04:00
|
|
|
nsresult
|
|
|
|
nsFilterInstance::ComputeUserSpaceToFilterSpaceScale()
|
|
|
|
{
|
2014-09-08 15:28:50 +04:00
|
|
|
gfxMatrix canvasTransform = nsSVGUtils::GetCanvasTM(mTargetFrame);
|
2014-03-27 03:21:48 +04:00
|
|
|
if (canvasTransform.IsSingular()) {
|
|
|
|
// Nothing should be rendered.
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
mUserSpaceToFilterSpaceScale = canvasTransform.ScaleFactors(true);
|
|
|
|
if (mUserSpaceToFilterSpaceScale.width <= 0.0f ||
|
|
|
|
mUserSpaceToFilterSpaceScale.height <= 0.0f) {
|
|
|
|
// Nothing should be rendered.
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
mFilterSpaceToUserSpaceScale = gfxSize(1.0f / mUserSpaceToFilterSpaceScale.width,
|
|
|
|
1.0f / mUserSpaceToFilterSpaceScale.height);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2014-02-24 19:22:58 +04:00
|
|
|
gfxRect
|
2014-03-27 03:21:48 +04:00
|
|
|
nsFilterInstance::UserSpaceToFilterSpace(const gfxRect& aUserSpaceRect) const
|
2014-02-24 19:22:58 +04:00
|
|
|
{
|
2014-03-27 03:21:48 +04:00
|
|
|
gfxRect filterSpaceRect = aUserSpaceRect;
|
|
|
|
filterSpaceRect.Scale(mUserSpaceToFilterSpaceScale.width,
|
|
|
|
mUserSpaceToFilterSpaceScale.height);
|
|
|
|
return filterSpaceRect;
|
2014-02-24 19:22:58 +04:00
|
|
|
}
|
|
|
|
|
2014-03-27 03:21:48 +04:00
|
|
|
gfxRect
|
|
|
|
nsFilterInstance::FilterSpaceToUserSpace(const gfxRect& aFilterSpaceRect) const
|
2014-02-24 19:22:58 +04:00
|
|
|
{
|
2014-03-27 03:21:48 +04:00
|
|
|
gfxRect userSpaceRect = aFilterSpaceRect;
|
|
|
|
userSpaceRect.Scale(mFilterSpaceToUserSpaceScale.width,
|
|
|
|
mFilterSpaceToUserSpaceScale.height);
|
|
|
|
return userSpaceRect;
|
2014-02-24 19:22:58 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsFilterInstance::BuildPrimitives()
|
|
|
|
{
|
|
|
|
NS_ASSERTION(!mPrimitiveDescriptions.Length(),
|
|
|
|
"expected to start building primitives from scratch");
|
|
|
|
|
|
|
|
const nsTArray<nsStyleFilter>& filters = mTargetFrame->StyleSVGReset()->mFilters;
|
|
|
|
for (uint32_t i = 0; i < filters.Length(); i++) {
|
|
|
|
nsresult rv = BuildPrimitivesForFilter(filters[i]);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
}
|
2014-09-10 21:06:07 +04:00
|
|
|
|
|
|
|
mFilterDescription = FilterDescription(mPrimitiveDescriptions);
|
|
|
|
|
2014-02-24 19:22:58 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsFilterInstance::BuildPrimitivesForFilter(const nsStyleFilter& aFilter)
|
|
|
|
{
|
2014-03-27 03:21:48 +04:00
|
|
|
NS_ASSERTION(mUserSpaceToFilterSpaceScale.width > 0.0f &&
|
|
|
|
mFilterSpaceToUserSpaceScale.height > 0.0f,
|
|
|
|
"scale factors between spaces should be positive values");
|
|
|
|
|
2014-02-24 19:22:58 +04:00
|
|
|
if (aFilter.GetType() == NS_STYLE_FILTER_URL) {
|
|
|
|
// Build primitives for an SVG filter.
|
2014-03-27 03:21:48 +04:00
|
|
|
nsSVGFilterInstance svgFilterInstance(aFilter, mTargetFrame, mTargetBBox,
|
|
|
|
mUserSpaceToFilterSpaceScale,
|
|
|
|
mFilterSpaceToUserSpaceScale);
|
2014-02-24 19:22:58 +04:00
|
|
|
if (!svgFilterInstance.IsInitialized()) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return svgFilterInstance.BuildPrimitives(mPrimitiveDescriptions, mInputImages);
|
|
|
|
}
|
|
|
|
|
2014-08-06 21:48:47 +04:00
|
|
|
// Build primitives for a CSS filter.
|
2014-08-14 22:29:56 +04:00
|
|
|
nsCSSFilterInstance cssFilterInstance(aFilter, mTargetFrame,
|
|
|
|
mTargetBBoxInFilterSpace,
|
2014-08-06 21:48:47 +04:00
|
|
|
mFrameSpaceInCSSPxToFilterSpaceTransform);
|
|
|
|
return cssFilterInstance.BuildPrimitives(mPrimitiveDescriptions);
|
2014-02-24 19:22:58 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsFilterInstance::ComputeNeededBoxes()
|
|
|
|
{
|
|
|
|
if (mPrimitiveDescriptions.IsEmpty())
|
|
|
|
return;
|
|
|
|
|
|
|
|
nsIntRegion sourceGraphicNeededRegion;
|
|
|
|
nsIntRegion fillPaintNeededRegion;
|
|
|
|
nsIntRegion strokePaintNeededRegion;
|
|
|
|
|
|
|
|
FilterSupport::ComputeSourceNeededRegions(
|
2014-09-10 21:06:07 +04:00
|
|
|
mFilterDescription, mPostFilterDirtyRegion,
|
2014-02-24 19:22:58 +04:00
|
|
|
sourceGraphicNeededRegion, fillPaintNeededRegion, strokePaintNeededRegion);
|
|
|
|
|
2014-08-06 21:48:47 +04:00
|
|
|
nsIntRect sourceBounds;
|
|
|
|
sourceBounds.UnionRect(mTargetBBoxInFilterSpace, mTargetBounds);
|
2014-02-24 19:22:58 +04:00
|
|
|
|
2014-08-06 21:48:47 +04:00
|
|
|
sourceGraphicNeededRegion.And(sourceGraphicNeededRegion, sourceBounds);
|
2014-02-24 19:22:58 +04:00
|
|
|
|
|
|
|
mSourceGraphic.mNeededBounds = sourceGraphicNeededRegion.GetBounds();
|
|
|
|
mFillPaint.mNeededBounds = fillPaintNeededRegion.GetBounds();
|
|
|
|
mStrokePaint.mNeededBounds = strokePaintNeededRegion.GetBounds();
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsFilterInstance::BuildSourcePaint(SourceInfo *aSource,
|
|
|
|
DrawTarget* aTargetDT)
|
|
|
|
{
|
|
|
|
nsIntRect neededRect = aSource->mNeededBounds;
|
|
|
|
|
2014-07-07 12:05:16 +04:00
|
|
|
RefPtr<DrawTarget> offscreenDT =
|
|
|
|
gfxPlatform::GetPlatform()->CreateOffscreenContentDrawTarget(
|
2014-02-24 19:22:58 +04:00
|
|
|
ToIntSize(neededRect.Size()), SurfaceFormat::B8G8R8A8);
|
2014-07-07 12:05:16 +04:00
|
|
|
if (!offscreenDT) {
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
2014-02-24 19:22:58 +04:00
|
|
|
}
|
|
|
|
|
2014-07-11 11:06:39 +04:00
|
|
|
gfxMatrix deviceToFilterSpace = GetFilterSpaceToDeviceSpaceTransform();
|
|
|
|
if (!deviceToFilterSpace.Invert()) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
2014-02-24 19:22:58 +04:00
|
|
|
|
2014-08-29 23:42:07 +04:00
|
|
|
if (!mPaintTransform.IsSingular()) {
|
2014-09-11 10:57:38 +04:00
|
|
|
nsRefPtr<gfxContext> gfx = new gfxContext(offscreenDT);
|
|
|
|
gfx->Save();
|
|
|
|
gfx->Multiply(mPaintTransform *
|
|
|
|
deviceToFilterSpace *
|
|
|
|
gfxMatrix::Translation(-neededRect.TopLeft()));
|
2014-07-29 20:48:16 +04:00
|
|
|
gfx->Rectangle(FilterSpaceToUserSpace(neededRect));
|
2014-08-01 04:37:35 +04:00
|
|
|
if ((aSource == &mFillPaint &&
|
2014-02-24 19:22:58 +04:00
|
|
|
nsSVGUtils::SetupCairoFillPaint(mTargetFrame, gfx)) ||
|
|
|
|
(aSource == &mStrokePaint &&
|
|
|
|
nsSVGUtils::SetupCairoStrokePaint(mTargetFrame, gfx))) {
|
|
|
|
gfx->Fill();
|
|
|
|
}
|
2014-09-11 10:57:38 +04:00
|
|
|
gfx->Restore();
|
2014-02-24 19:22:58 +04:00
|
|
|
}
|
|
|
|
|
2014-07-07 12:05:16 +04:00
|
|
|
|
|
|
|
aSource->mSourceSurface = offscreenDT->Snapshot();
|
2014-02-24 19:22:58 +04:00
|
|
|
aSource->mSurfaceRect = ToIntRect(neededRect);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2014-07-07 12:05:16 +04:00
|
|
|
nsFilterInstance::BuildSourcePaints(DrawTarget* aTargetDT)
|
2014-02-24 19:22:58 +04:00
|
|
|
{
|
|
|
|
nsresult rv = NS_OK;
|
|
|
|
|
|
|
|
if (!mFillPaint.mNeededBounds.IsEmpty()) {
|
2014-07-07 12:05:16 +04:00
|
|
|
rv = BuildSourcePaint(&mFillPaint, aTargetDT);
|
2014-02-24 19:22:58 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!mStrokePaint.mNeededBounds.IsEmpty()) {
|
2014-07-07 12:05:16 +04:00
|
|
|
rv = BuildSourcePaint(&mStrokePaint, aTargetDT);
|
2014-02-24 19:22:58 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2014-07-07 12:05:16 +04:00
|
|
|
nsFilterInstance::BuildSourceImage(DrawTarget* aTargetDT)
|
2014-02-24 19:22:58 +04:00
|
|
|
{
|
|
|
|
nsIntRect neededRect = mSourceGraphic.mNeededBounds;
|
|
|
|
if (neededRect.IsEmpty()) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2014-07-07 12:05:16 +04:00
|
|
|
RefPtr<DrawTarget> offscreenDT =
|
|
|
|
gfxPlatform::GetPlatform()->CreateOffscreenContentDrawTarget(
|
2014-02-24 19:22:58 +04:00
|
|
|
ToIntSize(neededRect.Size()), SurfaceFormat::B8G8R8A8);
|
2014-07-07 12:05:16 +04:00
|
|
|
if (!offscreenDT) {
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
2014-02-24 19:22:58 +04:00
|
|
|
}
|
|
|
|
|
2014-03-27 03:21:48 +04:00
|
|
|
gfxRect r = FilterSpaceToUserSpace(neededRect);
|
2014-02-24 19:22:58 +04:00
|
|
|
r.RoundOut();
|
|
|
|
nsIntRect dirty;
|
|
|
|
if (!gfxUtils::GfxRectToIntRect(r, &dirty))
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
|
|
|
|
// 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
|
|
|
|
// (since that would eliminate the transform multiplications from user
|
|
|
|
// 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.)
|
2014-07-11 11:06:39 +04:00
|
|
|
gfxMatrix deviceToFilterSpace = GetFilterSpaceToDeviceSpaceTransform();
|
|
|
|
if (!deviceToFilterSpace.Invert()) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
2014-09-11 10:57:38 +04:00
|
|
|
nsRefPtr<gfxContext> ctx = new gfxContext(offscreenDT);
|
|
|
|
ctx->SetMatrix(
|
|
|
|
ctx->CurrentMatrix().Translate(-neededRect.TopLeft()).
|
|
|
|
PreMultiply(deviceToFilterSpace));
|
|
|
|
|
|
|
|
nsRefPtr<nsRenderingContext> tmpCtx(new nsRenderingContext());
|
|
|
|
tmpCtx->Init(mTargetFrame->PresContext()->DeviceContext(), ctx);
|
2014-08-29 23:42:07 +04:00
|
|
|
mPaintCallback->Paint(tmpCtx, mTargetFrame, mPaintTransform, &dirty);
|
2014-02-24 19:22:58 +04:00
|
|
|
|
2014-07-07 12:05:16 +04:00
|
|
|
mSourceGraphic.mSourceSurface = offscreenDT->Snapshot();
|
2014-02-24 19:22:58 +04:00
|
|
|
mSourceGraphic.mSurfaceRect = ToIntRect(neededRect);
|
2014-08-01 04:37:35 +04:00
|
|
|
|
2014-02-24 19:22:58 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsFilterInstance::Render(gfxContext* aContext)
|
|
|
|
{
|
2014-07-29 20:48:16 +04:00
|
|
|
nsIntRect filterRect = mPostFilterDirtyRegion.GetBounds().Intersect(OutputFilterSpaceBounds());
|
2014-02-24 19:22:58 +04:00
|
|
|
gfxMatrix ctm = GetFilterSpaceToDeviceSpaceTransform();
|
|
|
|
|
|
|
|
if (filterRect.IsEmpty() || ctm.IsSingular()) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2014-07-07 12:05:16 +04:00
|
|
|
RefPtr<DrawTarget> dt = aContext->GetDrawTarget();
|
2014-09-10 21:29:35 +04:00
|
|
|
|
|
|
|
AutoSaveTransform autoSR(dt);
|
|
|
|
Matrix newTM = ToMatrix(ctm).PreTranslate(filterRect.x, filterRect.y) * dt->GetTransform();
|
|
|
|
dt->SetTransform(newTM);
|
2014-02-24 19:22:58 +04:00
|
|
|
|
|
|
|
ComputeNeededBoxes();
|
|
|
|
|
2014-07-07 12:05:16 +04:00
|
|
|
nsresult rv = BuildSourceImage(dt);
|
2014-02-24 19:22:58 +04:00
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
2014-07-07 12:05:16 +04:00
|
|
|
rv = BuildSourcePaints(dt);
|
2014-02-24 19:22:58 +04:00
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
|
|
|
|
FilterSupport::RenderFilterDescription(
|
2014-09-10 21:06:07 +04:00
|
|
|
dt, mFilterDescription, ToRect(filterRect),
|
2014-02-24 19:22:58 +04:00
|
|
|
mSourceGraphic.mSourceSurface, mSourceGraphic.mSurfaceRect,
|
|
|
|
mFillPaint.mSourceSurface, mFillPaint.mSurfaceRect,
|
|
|
|
mStrokePaint.mSourceSurface, mStrokePaint.mSurfaceRect,
|
|
|
|
mInputImages);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2014-09-04 21:31:03 +04:00
|
|
|
nsRegion
|
|
|
|
nsFilterInstance::ComputePostFilterDirtyRegion()
|
2014-02-24 19:22:58 +04:00
|
|
|
{
|
2014-04-24 12:25:17 +04:00
|
|
|
if (mPreFilterDirtyRegion.IsEmpty()) {
|
2014-09-04 21:31:03 +04:00
|
|
|
return nsRegion();
|
2014-02-24 19:22:58 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
nsIntRegion resultChangeRegion =
|
2014-09-10 21:06:07 +04:00
|
|
|
FilterSupport::ComputeResultChangeRegion(mFilterDescription,
|
2014-04-24 12:25:17 +04:00
|
|
|
mPreFilterDirtyRegion, nsIntRegion(), nsIntRegion());
|
2014-09-04 21:31:03 +04:00
|
|
|
return FilterSpaceToFrameSpace(resultChangeRegion);
|
2014-02-24 19:22:58 +04:00
|
|
|
}
|
|
|
|
|
2014-09-04 21:31:03 +04:00
|
|
|
nsRect
|
|
|
|
nsFilterInstance::ComputePostFilterExtents()
|
2014-02-24 19:22:58 +04:00
|
|
|
{
|
2014-08-06 21:48:47 +04:00
|
|
|
nsIntRect sourceBounds;
|
|
|
|
sourceBounds.UnionRect(mTargetBBoxInFilterSpace, mTargetBounds);
|
2014-02-24 19:22:58 +04:00
|
|
|
|
|
|
|
nsIntRegion postFilterExtents =
|
2014-09-10 21:06:07 +04:00
|
|
|
FilterSupport::ComputePostFilterExtents(mFilterDescription, sourceBounds);
|
2014-09-04 21:31:03 +04:00
|
|
|
return FilterSpaceToFrameSpace(postFilterExtents.GetBounds());
|
2014-02-24 19:22:58 +04:00
|
|
|
}
|
|
|
|
|
2014-09-04 21:31:03 +04:00
|
|
|
nsRect
|
|
|
|
nsFilterInstance::ComputeSourceNeededRect()
|
2014-02-24 19:22:58 +04:00
|
|
|
{
|
|
|
|
ComputeNeededBoxes();
|
2014-09-04 21:31:03 +04:00
|
|
|
return FilterSpaceToFrameSpace(mSourceGraphic.mNeededBounds);
|
2014-02-24 19:22:58 +04:00
|
|
|
}
|
|
|
|
|
2014-07-29 20:48:16 +04:00
|
|
|
nsIntRect
|
|
|
|
nsFilterInstance::OutputFilterSpaceBounds() const
|
|
|
|
{
|
|
|
|
uint32_t numPrimitives = mPrimitiveDescriptions.Length();
|
|
|
|
if (numPrimitives <= 0)
|
|
|
|
return nsIntRect();
|
|
|
|
|
|
|
|
nsIntRect bounds =
|
|
|
|
ThebesIntRect(mPrimitiveDescriptions[numPrimitives - 1].PrimitiveSubregion());
|
|
|
|
bool overflow;
|
|
|
|
gfxIntSize surfaceSize =
|
|
|
|
nsSVGUtils::ConvertToSurfaceSize(bounds.Size(), &overflow);
|
|
|
|
bounds.SizeTo(surfaceSize);
|
|
|
|
return bounds;
|
|
|
|
}
|
|
|
|
|
2014-02-24 19:22:58 +04:00
|
|
|
nsIntRect
|
|
|
|
nsFilterInstance::FrameSpaceToFilterSpace(const nsRect* aRect) const
|
|
|
|
{
|
2014-07-29 20:48:16 +04:00
|
|
|
nsIntRect rect = OutputFilterSpaceBounds();
|
2014-02-24 19:22:58 +04:00
|
|
|
if (aRect) {
|
|
|
|
if (aRect->IsEmpty()) {
|
|
|
|
return nsIntRect();
|
|
|
|
}
|
|
|
|
gfxRect rectInCSSPx =
|
2014-09-10 21:06:10 +04:00
|
|
|
nsLayoutUtils::RectToGfxRect(*aRect, nsPresContext::AppUnitsPerCSSPixel());
|
2014-02-24 19:22:58 +04:00
|
|
|
gfxRect rectInFilterSpace =
|
|
|
|
mFrameSpaceInCSSPxToFilterSpaceTransform.TransformBounds(rectInCSSPx);
|
|
|
|
rectInFilterSpace.RoundOut();
|
|
|
|
nsIntRect intRect;
|
|
|
|
if (gfxUtils::GfxRectToIntRect(rectInFilterSpace, &intRect)) {
|
|
|
|
rect = intRect;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return rect;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsRect
|
|
|
|
nsFilterInstance::FilterSpaceToFrameSpace(const nsIntRect& aRect) const
|
|
|
|
{
|
|
|
|
if (aRect.IsEmpty()) {
|
|
|
|
return nsRect();
|
|
|
|
}
|
|
|
|
gfxRect r(aRect.x, aRect.y, aRect.width, aRect.height);
|
|
|
|
r = mFilterSpaceToFrameSpaceInCSSPxTransform.TransformBounds(r);
|
2014-04-24 12:25:17 +04:00
|
|
|
// nsLayoutUtils::RoundGfxRectToAppRect rounds out.
|
2014-09-10 21:06:10 +04:00
|
|
|
return nsLayoutUtils::RoundGfxRectToAppRect(r, nsPresContext::AppUnitsPerCSSPixel());
|
2014-02-24 19:22:58 +04:00
|
|
|
}
|
|
|
|
|
2014-04-24 12:25:17 +04:00
|
|
|
nsIntRegion
|
|
|
|
nsFilterInstance::FrameSpaceToFilterSpace(const nsRegion* aRegion) const
|
|
|
|
{
|
|
|
|
if (!aRegion) {
|
2014-07-29 20:48:16 +04:00
|
|
|
return OutputFilterSpaceBounds();
|
2014-04-24 12:25:17 +04:00
|
|
|
}
|
|
|
|
nsIntRegion result;
|
|
|
|
nsRegionRectIterator it(*aRegion);
|
|
|
|
while (const nsRect* r = it.Next()) {
|
|
|
|
// FrameSpaceToFilterSpace rounds out, so this works.
|
|
|
|
result.Or(result, FrameSpaceToFilterSpace(r));
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsRegion
|
|
|
|
nsFilterInstance::FilterSpaceToFrameSpace(const nsIntRegion& aRegion) const
|
|
|
|
{
|
|
|
|
nsRegion result;
|
|
|
|
nsIntRegionRectIterator it(aRegion);
|
|
|
|
while (const nsIntRect* r = it.Next()) {
|
|
|
|
// FilterSpaceToFrameSpace rounds out, so this works.
|
|
|
|
result.Or(result, FilterSpaceToFrameSpace(*r));
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2014-02-24 19:22:58 +04:00
|
|
|
gfxMatrix
|
|
|
|
nsFilterInstance::GetUserSpaceToFrameSpaceInCSSPxTransform() const
|
|
|
|
{
|
2014-09-10 17:26:12 +04:00
|
|
|
return gfxMatrix::Translation(-nsSVGUtils::FrameSpaceInCSSPxToUserSpaceOffset(mTargetFrame));
|
2014-02-24 19:22:58 +04:00
|
|
|
}
|