michael@0: /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #ifndef __NS_SVGFILTERINSTANCE_H__ michael@0: #define __NS_SVGFILTERINSTANCE_H__ michael@0: michael@0: #include "gfxMatrix.h" michael@0: #include "gfxRect.h" michael@0: #include "nsSVGFilters.h" michael@0: #include "nsSVGNumber2.h" michael@0: #include "nsSVGNumberPair.h" michael@0: #include "nsTArray.h" michael@0: #include "nsIFrame.h" michael@0: michael@0: class nsIFrame; michael@0: class nsSVGFilterFrame; michael@0: class nsSVGFilterPaintCallback; michael@0: michael@0: namespace mozilla { michael@0: namespace dom { michael@0: class SVGFilterElement; michael@0: } michael@0: } michael@0: michael@0: /** michael@0: * This class helps nsFilterInstance build its filter graph by processing a michael@0: * single SVG reference filter. michael@0: * michael@0: * In BuildPrimitives, this class iterates through the referenced michael@0: * element's primitive elements, creating a FilterPrimitiveDescription for michael@0: * each one. michael@0: * michael@0: * This class uses several different coordinate spaces, defined as follows: michael@0: * michael@0: * "user space" michael@0: * The filtered SVG element's user space or the filtered HTML element's michael@0: * CSS pixel space. The origin for an HTML element is the top left corner of michael@0: * its border box. michael@0: * michael@0: * "filter space" michael@0: * User space scaled to device pixels. Shares the same origin as user space. michael@0: * This space is the same across chained SVG and CSS filters. To compute the michael@0: * overall filter space for a chain, we first need to build each filter's michael@0: * FilterPrimitiveDescriptions in some common space. That space is michael@0: * filter space. michael@0: * michael@0: * To understand the spaces better, let's take an example filter: michael@0: * ... michael@0: * michael@0: * And apply the filter to a div element: michael@0: *
...
michael@0: * michael@0: * And let's say there are 2 device pixels for every 1 CSS pixel. michael@0: * michael@0: * Finally, let's define an arbitrary point in user space: michael@0: * "user space point" = (10, 10) michael@0: * michael@0: * The point will be inset 10 CSS pixels from both the top and left edges of the michael@0: * div element's border box. michael@0: * michael@0: * Now, let's transform the point from user space to filter space: michael@0: * "filter space point" = "user space point" * "device pixels per CSS pixel" michael@0: * "filter space point" = (10, 10) * 2 michael@0: * "filter space point" = (20, 20) michael@0: */ michael@0: class nsSVGFilterInstance michael@0: { michael@0: typedef mozilla::gfx::Point3D Point3D; michael@0: typedef mozilla::gfx::IntRect IntRect; michael@0: typedef mozilla::gfx::SourceSurface SourceSurface; michael@0: typedef mozilla::gfx::FilterPrimitiveDescription FilterPrimitiveDescription; michael@0: michael@0: public: michael@0: /** michael@0: * @param aFilter The SVG reference filter to process. michael@0: * @param aTargetFrame The frame of the filtered element under consideration. michael@0: * @param aTargetBBox The SVG bbox to use for the target frame, computed by michael@0: * the caller. The caller may decide to override the actual SVG bbox. michael@0: */ michael@0: nsSVGFilterInstance(const nsStyleFilter& aFilter, michael@0: nsIFrame *aTargetFrame, michael@0: const gfxRect& aTargetBBox, michael@0: const gfxSize& aUserSpaceToFilterSpaceScale, michael@0: const gfxSize& aFilterSpaceToUserSpaceScale); michael@0: michael@0: /** michael@0: * Returns true if the filter instance was created successfully. michael@0: */ michael@0: bool IsInitialized() const { return mInitialized; } michael@0: michael@0: /** michael@0: * Iterates through the element's primitive elements, creating a michael@0: * FilterPrimitiveDescription for each one. Appends the new michael@0: * FilterPrimitiveDescription(s) to the aPrimitiveDescrs list. Also, appends michael@0: * new images from feImage filter primitive elements to the aInputImages list. michael@0: */ michael@0: nsresult BuildPrimitives(nsTArray& aPrimitiveDescrs, michael@0: nsTArray>& aInputImages); michael@0: michael@0: /** michael@0: * Returns the user specified "filter region", in the filtered element's user michael@0: * space, after it has been adjusted out (if necessary) so that its edges michael@0: * coincide with pixel boundaries of the offscreen surface into which the michael@0: * filtered output would/will be painted. michael@0: */ michael@0: gfxRect GetFilterRegion() const { return mUserSpaceBounds; } michael@0: michael@0: /** michael@0: * Returns the size of the user specified "filter region", in filter space. michael@0: */ michael@0: nsIntRect GetFilterSpaceBounds() const { return mFilterSpaceBounds; } michael@0: michael@0: float GetPrimitiveNumber(uint8_t aCtxType, const nsSVGNumber2 *aNumber) const michael@0: { michael@0: return GetPrimitiveNumber(aCtxType, aNumber->GetAnimValue()); michael@0: } michael@0: float GetPrimitiveNumber(uint8_t aCtxType, const nsSVGNumberPair *aNumberPair, michael@0: nsSVGNumberPair::PairIndex aIndex) const michael@0: { michael@0: return GetPrimitiveNumber(aCtxType, aNumberPair->GetAnimValue(aIndex)); michael@0: } michael@0: michael@0: /** michael@0: * Converts a userSpaceOnUse/objectBoundingBoxUnits unitless point michael@0: * into filter space, depending on the value of mPrimitiveUnits. (For michael@0: * objectBoundingBoxUnits, the bounding box offset is applied to the point.) michael@0: */ michael@0: Point3D ConvertLocation(const Point3D& aPoint) const; michael@0: michael@0: /** michael@0: * Transform a rect between user space and filter space. michael@0: */ michael@0: gfxRect UserSpaceToFilterSpace(const gfxRect& aUserSpaceRect) const; michael@0: michael@0: private: michael@0: /** michael@0: * Finds the filter frame associated with this SVG filter. michael@0: */ michael@0: nsSVGFilterFrame* GetFilterFrame(); michael@0: michael@0: /** michael@0: * Computes the filter primitive subregion for the given primitive. michael@0: */ michael@0: IntRect ComputeFilterPrimitiveSubregion(nsSVGFE* aFilterElement, michael@0: const nsTArray& aPrimitiveDescrs, michael@0: const nsTArray& aInputIndices); michael@0: michael@0: /** michael@0: * Takes the input indices of a filter primitive and returns for each input michael@0: * whether the input's output is tainted. michael@0: */ michael@0: void GetInputsAreTainted(const nsTArray& aPrimitiveDescrs, michael@0: const nsTArray& aInputIndices, michael@0: nsTArray& aOutInputsAreTainted); michael@0: michael@0: /** michael@0: * Scales a numeric filter primitive length in the X, Y or "XY" directions michael@0: * into a length in filter space (no offset is applied). michael@0: */ michael@0: float GetPrimitiveNumber(uint8_t aCtxType, float aValue) const; michael@0: michael@0: /** michael@0: * Transform a rect between user space and filter space. michael@0: */ michael@0: gfxRect FilterSpaceToUserSpace(const gfxRect& aFilterSpaceRect) const; michael@0: michael@0: /** michael@0: * Returns the transform from frame space to the coordinate space that michael@0: * GetCanvasTM transforms to. "Frame space" is the origin of a frame, aka the michael@0: * top-left corner of its border box, aka the top left corner of its mRect. michael@0: */ michael@0: gfxMatrix GetUserSpaceToFrameSpaceInCSSPxTransform() const; michael@0: michael@0: /** michael@0: * Finds the index in aPrimitiveDescrs of each input to aPrimitiveElement. michael@0: * For example, if aPrimitiveElement is: michael@0: * michael@0: * Then, the resulting aSourceIndices will contain the index of the michael@0: * FilterPrimitiveDescription representing "another-primitive". michael@0: */ michael@0: nsresult GetSourceIndices(nsSVGFE* aPrimitiveElement, michael@0: const nsTArray& aPrimitiveDescrs, michael@0: const nsDataHashtable& aImageTable, michael@0: nsTArray& aSourceIndices); michael@0: michael@0: /** michael@0: * Compute the filter region in user space, filter space, and filter michael@0: * space. michael@0: */ michael@0: nsresult ComputeBounds(); michael@0: michael@0: /** michael@0: * The SVG reference filter originally from the style system. michael@0: */ michael@0: const nsStyleFilter mFilter; michael@0: michael@0: /** michael@0: * The frame for the element that is currently being filtered. michael@0: */ michael@0: nsIFrame* mTargetFrame; michael@0: michael@0: /** michael@0: * The filter element referenced by mTargetFrame's element. michael@0: */ michael@0: const mozilla::dom::SVGFilterElement* mFilterElement; michael@0: michael@0: /** michael@0: * The frame for the SVG filter element. michael@0: */ michael@0: nsSVGFilterFrame* mFilterFrame; michael@0: michael@0: /** michael@0: * The SVG bbox of the element that is being filtered, in user space. michael@0: */ michael@0: gfxRect mTargetBBox; michael@0: michael@0: /** michael@0: * The "filter region" in various spaces. michael@0: */ michael@0: gfxRect mUserSpaceBounds; michael@0: nsIntRect mFilterSpaceBounds; michael@0: michael@0: /** michael@0: * The scale factors between user space and filter space. michael@0: */ michael@0: gfxSize mUserSpaceToFilterSpaceScale; michael@0: gfxSize mFilterSpaceToUserSpaceScale; michael@0: michael@0: /** michael@0: * The 'primitiveUnits' attribute value (objectBoundingBox or userSpaceOnUse). michael@0: */ michael@0: uint16_t mPrimitiveUnits; michael@0: michael@0: /** michael@0: * The index of the FilterPrimitiveDescription that this SVG filter should use michael@0: * as its SourceGraphic, or the SourceGraphic keyword index if this is the michael@0: * first filter in a chain. michael@0: */ michael@0: int32_t mSourceGraphicIndex; michael@0: michael@0: bool mInitialized; michael@0: }; michael@0: michael@0: #endif