Upstream version 11.40.277.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / platform / graphics / filters / FilterEffect.h
1 /*
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.
6  *
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.
11  *
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.
16  *
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.
21  */
22
23 #ifndef FilterEffect_h
24 #define FilterEffect_h
25
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"
31
32 #include "third_party/skia/include/core/SkImageFilter.h"
33
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"
39
40 namespace blink {
41
42 class Filter;
43 class FilterEffect;
44 class TextStream;
45
46 class SkiaImageFilterBuilder;
47
48 typedef Vector<RefPtr<FilterEffect> > FilterEffectVector;
49
50 enum FilterEffectType {
51     FilterEffectTypeUnknown,
52     FilterEffectTypeImage,
53     FilterEffectTypeTile,
54     FilterEffectTypeSourceInput
55 };
56
57 enum DetermineSubregionFlag {
58     DetermineSubregionNone = 0,
59     MapRectForward = 1,
60     ClipToFilterRegion = 1 << 1
61 };
62
63 typedef int DetermineSubregionFlags;
64
65 class PLATFORM_EXPORT FilterEffect : public RefCounted<FilterEffect> {
66 public:
67     virtual ~FilterEffect();
68
69     static bool isFilterSizeValid(const FloatRect&);
70     static float maxFilterArea();
71
72     void clearResult();
73     void clearResultsRecursive();
74
75     FilterEffectVector& inputEffects() { return m_inputEffects; }
76     FilterEffect* inputEffect(unsigned) const;
77     unsigned numberOfEffectInputs() const { return m_inputEffects.size(); }
78
79     inline bool hasImageFilter() const
80     {
81         return m_imageFilters[0] || m_imageFilters[1] || m_imageFilters[2] || m_imageFilters[3];
82     }
83
84     // Solid black image with different alpha values.
85     bool isAlphaImage() const { return m_alphaImage; }
86     void setIsAlphaImage(bool alphaImage) { m_alphaImage = alphaImage; }
87
88     IntRect absolutePaintRect() const { return m_absolutePaintRect; }
89
90     FloatRect maxEffectRect() const { return m_maxEffectRect; }
91     void setMaxEffectRect(const FloatRect& maxEffectRect) { m_maxEffectRect = maxEffectRect; }
92
93     virtual PassRefPtr<SkImageFilter> createImageFilter(SkiaImageFilterBuilder*);
94     virtual PassRefPtr<SkImageFilter> createImageFilterWithoutValidation(SkiaImageFilterBuilder*);
95
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
101     // transformation.
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
106     // displacement map.
107     virtual FloatRect mapPaintRect(const FloatRect& rect, bool forward)
108     {
109         return mapRect(rect, forward);
110     }
111     FloatRect mapRectRecursive(const FloatRect&);
112
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);
117
118     virtual FilterEffectType filterEffectType() const { return FilterEffectTypeUnknown; }
119
120     virtual TextStream& externalRepresentation(TextStream&, int indention = 0) const;
121
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; }
126
127     bool hasY() const { return m_hasY; }
128     void setHasY(bool value) { m_hasY = value; }
129
130     bool hasWidth() const { return m_hasWidth; }
131     void setHasWidth(bool value) { m_hasWidth = value; }
132
133     bool hasHeight() const { return m_hasHeight; }
134     void setHasHeight(bool value) { m_hasHeight = value; }
135
136     FloatRect filterPrimitiveSubregion() const { return m_filterPrimitiveSubregion; }
137     void setFilterPrimitiveSubregion(const FloatRect& filterPrimitiveSubregion) { m_filterPrimitiveSubregion = filterPrimitiveSubregion; }
138
139     FloatRect effectBoundaries() const { return m_effectBoundaries; }
140     void setEffectBoundaries(const FloatRect& effectBoundaries) { m_effectBoundaries = effectBoundaries; }
141
142     Filter* filter() { return m_filter; }
143     const Filter* filter() const { return m_filter; }
144
145     bool clipsToBounds() const { return m_clipsToBounds; }
146     void setClipsToBounds(bool value) { m_clipsToBounds = value; }
147
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; }
152
153     FloatRect determineFilterPrimitiveSubregion(DetermineSubregionFlags = DetermineSubregionNone);
154     void determineAllAbsolutePaintRects();
155
156     virtual FloatRect determineAbsolutePaintRect(const FloatRect& requestedAbsoluteRect);
157     virtual bool affectsTransparentPixels() { return false; }
158
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; }
162
163     SkImageFilter* getImageFilter(ColorSpace, bool requiresPMColorValidation) const;
164     void setImageFilter(ColorSpace, bool requiresPMColorValidation, PassRefPtr<SkImageFilter>);
165
166 protected:
167     FilterEffect(Filter*);
168
169     Color adaptColorToOperatingColorSpace(const Color& deviceColor);
170
171     SkImageFilter::CropRect getCropRect(const FloatSize& cropOffset) const;
172
173     void addAbsolutePaintRect(const FloatRect& absolutePaintRect);
174
175 private:
176     FilterEffectVector m_inputEffects;
177
178     bool m_alphaImage;
179
180     IntRect m_absolutePaintRect;
181
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;
185     Filter* m_filter;
186
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.
189
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;
193
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;
197     bool m_hasX;
198     bool m_hasY;
199     bool m_hasWidth;
200     bool m_hasHeight;
201
202     // Should the effect clip to its primitive region, or expand to use the combined region of its inputs.
203     bool m_clipsToBounds;
204
205     ColorSpace m_operatingColorSpace;
206     ColorSpace m_resultColorSpace;
207
208     RefPtr<SkImageFilter> m_imageFilters[4];
209 };
210
211 } // namespace blink
212
213 #endif // FilterEffect_h