2 * Copyright (C) 2008 Alex Mathews <possessedpenguinbob@gmail.com>
3 * Copyright (C) 2009 Dirk Schulze <krit@webkit.org>
4 * Copyright (C) Research In Motion Limited 2010. All rights reserved.
5 * Copyright (C) 2013 Google Inc. All rights reserved.
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Library General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Library General Public License for more details.
17 * You should have received a copy of the GNU Library General Public License
18 * along with this library; see the file COPYING.LIB. If not, write to
19 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20 * Boston, MA 02110-1301, USA.
23 #ifndef FilterEffect_h
24 #define FilterEffect_h
26 #include "platform/PlatformExport.h"
27 #include "platform/geometry/FloatRect.h"
28 #include "platform/geometry/IntRect.h"
29 #include "platform/graphics/Color.h"
30 #include "platform/graphics/ColorSpace.h"
32 #include "third_party/skia/include/core/SkImageFilter.h"
34 #include "wtf/PassOwnPtr.h"
35 #include "wtf/RefCounted.h"
36 #include "wtf/RefPtr.h"
37 #include "wtf/Uint8ClampedArray.h"
38 #include "wtf/Vector.h"
46 class SkiaImageFilterBuilder;
48 typedef Vector<RefPtr<FilterEffect> > FilterEffectVector;
50 enum FilterEffectType {
51 FilterEffectTypeUnknown,
52 FilterEffectTypeImage,
54 FilterEffectTypeSourceInput
57 enum DetermineSubregionFlag {
58 DetermineSubregionNone = 0,
60 ClipToFilterRegion = 1 << 1
63 typedef int DetermineSubregionFlags;
65 class PLATFORM_EXPORT FilterEffect : public RefCounted<FilterEffect> {
67 virtual ~FilterEffect();
69 static bool isFilterSizeValid(const FloatRect&);
70 static float maxFilterArea();
73 void clearResultsRecursive();
75 FilterEffectVector& inputEffects() { return m_inputEffects; }
76 FilterEffect* inputEffect(unsigned) const;
77 unsigned numberOfEffectInputs() const { return m_inputEffects.size(); }
79 inline bool hasImageFilter() const
81 return m_imageFilters[0] || m_imageFilters[1] || m_imageFilters[2] || m_imageFilters[3];
84 // Solid black image with different alpha values.
85 bool isAlphaImage() const { return m_alphaImage; }
86 void setIsAlphaImage(bool alphaImage) { m_alphaImage = alphaImage; }
88 IntRect absolutePaintRect() const { return m_absolutePaintRect; }
90 FloatRect maxEffectRect() const { return m_maxEffectRect; }
91 void setMaxEffectRect(const FloatRect& maxEffectRect) { m_maxEffectRect = maxEffectRect; }
93 virtual PassRefPtr<SkImageFilter> createImageFilter(SkiaImageFilterBuilder*);
94 virtual PassRefPtr<SkImageFilter> createImageFilterWithoutValidation(SkiaImageFilterBuilder*);
96 // Mapping a rect forwards determines which which destination pixels a
97 // given source rect would affect. Mapping a rect backwards determines
98 // which pixels from the source rect would be required to fill a given
99 // destination rect. Note that these are not necessarily the inverse of
100 // each other. For example, for FEGaussianBlur, they are the same
102 virtual FloatRect mapRect(const FloatRect& rect, bool forward = true) { return rect; }
103 // A version of the above that is used for calculating paint rects. We can't
104 // use mapRect above for that, because that is also used for calculating effect
105 // regions for CSS filters and has undesirable effects for tile and
107 virtual FloatRect mapPaintRect(const FloatRect& rect, bool forward)
109 return mapRect(rect, forward);
111 FloatRect mapRectRecursive(const FloatRect&);
113 // This is a recursive version of a backwards mapRect(), which also takes
114 // into account the filter primitive subregion of each effect.
115 // Note: This works in absolute coordinates!
116 FloatRect getSourceRect(const FloatRect& destRect, const FloatRect& clipRect);
118 virtual FilterEffectType filterEffectType() const { return FilterEffectTypeUnknown; }
120 virtual TextStream& externalRepresentation(TextStream&, int indention = 0) const;
122 // The following functions are SVG specific and will move to RenderSVGResourceFilterPrimitive.
123 // See bug https://bugs.webkit.org/show_bug.cgi?id=45614.
124 bool hasX() const { return m_hasX; }
125 void setHasX(bool value) { m_hasX = value; }
127 bool hasY() const { return m_hasY; }
128 void setHasY(bool value) { m_hasY = value; }
130 bool hasWidth() const { return m_hasWidth; }
131 void setHasWidth(bool value) { m_hasWidth = value; }
133 bool hasHeight() const { return m_hasHeight; }
134 void setHasHeight(bool value) { m_hasHeight = value; }
136 FloatRect filterPrimitiveSubregion() const { return m_filterPrimitiveSubregion; }
137 void setFilterPrimitiveSubregion(const FloatRect& filterPrimitiveSubregion) { m_filterPrimitiveSubregion = filterPrimitiveSubregion; }
139 FloatRect effectBoundaries() const { return m_effectBoundaries; }
140 void setEffectBoundaries(const FloatRect& effectBoundaries) { m_effectBoundaries = effectBoundaries; }
142 Filter* filter() { return m_filter; }
143 const Filter* filter() const { return m_filter; }
145 bool clipsToBounds() const { return m_clipsToBounds; }
146 void setClipsToBounds(bool value) { m_clipsToBounds = value; }
148 ColorSpace operatingColorSpace() const { return m_operatingColorSpace; }
149 virtual void setOperatingColorSpace(ColorSpace colorSpace) { m_operatingColorSpace = colorSpace; }
150 ColorSpace resultColorSpace() const { return m_resultColorSpace; }
151 virtual void setResultColorSpace(ColorSpace colorSpace) { m_resultColorSpace = colorSpace; }
153 FloatRect determineFilterPrimitiveSubregion(DetermineSubregionFlags = DetermineSubregionNone);
154 void determineAllAbsolutePaintRects();
156 virtual FloatRect determineAbsolutePaintRect(const FloatRect& requestedAbsoluteRect);
157 virtual bool affectsTransparentPixels() { return false; }
159 // Return false if the filter will only operate correctly on valid RGBA values, with
160 // alpha in [0,255] and each color component in [0, alpha].
161 virtual bool mayProduceInvalidPreMultipliedPixels() { return false; }
163 SkImageFilter* getImageFilter(ColorSpace, bool requiresPMColorValidation) const;
164 void setImageFilter(ColorSpace, bool requiresPMColorValidation, PassRefPtr<SkImageFilter>);
167 FilterEffect(Filter*);
169 Color adaptColorToOperatingColorSpace(const Color& deviceColor);
171 SkImageFilter::CropRect getCropRect(const FloatSize& cropOffset) const;
173 void addAbsolutePaintRect(const FloatRect& absolutePaintRect);
176 FilterEffectVector m_inputEffects;
180 IntRect m_absolutePaintRect;
182 // The maximum size of a filter primitive. In SVG this is the primitive subregion in absolute coordinate space.
183 // The absolute paint rect should never be bigger than m_maxEffectRect.
184 FloatRect m_maxEffectRect;
187 // The following member variables are SVG specific and will move to RenderSVGResourceFilterPrimitive.
188 // See bug https://bugs.webkit.org/show_bug.cgi?id=45614.
190 // The subregion of a filter primitive according to the SVG Filter specification in local coordinates.
191 // This is SVG specific and needs to move to RenderSVGResourceFilterPrimitive.
192 FloatRect m_filterPrimitiveSubregion;
194 // x, y, width and height of the actual SVGFE*Element. Is needed to determine the subregion of the
195 // filter primitive on a later step.
196 FloatRect m_effectBoundaries;
202 // Should the effect clip to its primitive region, or expand to use the combined region of its inputs.
203 bool m_clipsToBounds;
205 ColorSpace m_operatingColorSpace;
206 ColorSpace m_resultColorSpace;
208 RefPtr<SkImageFilter> m_imageFilters[4];
213 #endif // FilterEffect_h