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.
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
16 * You should have received a copy of the GNU Library General Public License
17 * along with this library; see the file COPYING.LIB. If not, write to
18 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19 * Boston, MA 02110-1301, USA.
22 #ifndef FilterEffect_h
23 #define FilterEffect_h
25 #include "ColorSpace.h"
26 #include "FloatRect.h"
29 #include <runtime/Uint8ClampedArray.h>
31 #include <wtf/HashSet.h>
32 #include <wtf/RefCounted.h>
33 #include <wtf/RefPtr.h>
34 #include <wtf/Vector.h>
37 #include "FilterContextOpenCL.h"
48 typedef Vector<RefPtr<FilterEffect>> FilterEffectVector;
50 enum FilterEffectType {
51 FilterEffectTypeUnknown,
52 FilterEffectTypeImage,
54 FilterEffectTypeSourceInput
57 class FilterEffect : public RefCounted<FilterEffect> {
59 virtual ~FilterEffect();
62 void clearResultsRecursive();
64 ImageBuffer* asImageBuffer();
65 PassRefPtr<Uint8ClampedArray> asUnmultipliedImage(const IntRect&);
66 PassRefPtr<Uint8ClampedArray> asPremultipliedImage(const IntRect&);
67 void copyUnmultipliedImage(Uint8ClampedArray* destination, const IntRect&);
68 void copyPremultipliedImage(Uint8ClampedArray* destination, const IntRect&);
71 OpenCLHandle openCLImage() { return m_openCLImageResult; }
72 void setOpenCLImage(OpenCLHandle openCLImage) { m_openCLImageResult = openCLImage; }
73 ImageBuffer* openCLImageToImageBuffer();
76 FilterEffectVector& inputEffects() { return m_inputEffects; }
77 FilterEffect* inputEffect(unsigned) const;
78 unsigned numberOfEffectInputs() const { return m_inputEffects.size(); }
79 unsigned totalNumberOfEffectInputs() const;
81 inline bool hasResult() const
83 // This function needs platform specific checks, if the memory managment is not done by FilterEffect.
84 return m_imageBufferResult
86 || m_openCLImageResult
88 || m_unmultipliedImageResult
89 || m_premultipliedImageResult;
92 FloatRect drawingRegionOfInputImage(const IntRect&) const;
93 IntRect requestedRegionOfInputImageData(const IntRect&) const;
95 // Solid black image with different alpha values.
96 bool isAlphaImage() const { return m_alphaImage; }
97 void setIsAlphaImage(bool alphaImage) { m_alphaImage = alphaImage; }
99 IntRect absolutePaintRect() const { return m_absolutePaintRect; }
100 void setAbsolutePaintRect(const IntRect& absolutePaintRect) { m_absolutePaintRect = absolutePaintRect; }
102 FloatRect maxEffectRect() const { return m_maxEffectRect; }
103 void setMaxEffectRect(const FloatRect& maxEffectRect) { m_maxEffectRect = maxEffectRect; }
109 inline void applyAll() { apply(); }
112 // Correct any invalid pixels, if necessary, in the result of a filter operation.
113 // This method is used to ensure valid pixel values on filter inputs and the final result.
114 // Only the arithmetic composite filter ever needs to perform correction.
115 virtual void correctFilterResultIfNeeded() { }
117 virtual void platformApplySoftware() = 0;
119 virtual bool platformApplyOpenCL();
121 virtual void dump() = 0;
123 virtual void determineAbsolutePaintRect();
125 virtual FilterEffectType filterEffectType() const { return FilterEffectTypeUnknown; }
127 virtual TextStream& externalRepresentation(TextStream&, int indention = 0) const;
130 // The following functions are SVG specific and will move to RenderSVGResourceFilterPrimitive.
131 // See bug https://bugs.webkit.org/show_bug.cgi?id=45614.
132 bool hasX() const { return m_hasX; }
133 void setHasX(bool value) { m_hasX = value; }
135 bool hasY() const { return m_hasY; }
136 void setHasY(bool value) { m_hasY = value; }
138 bool hasWidth() const { return m_hasWidth; }
139 void setHasWidth(bool value) { m_hasWidth = value; }
141 bool hasHeight() const { return m_hasHeight; }
142 void setHasHeight(bool value) { m_hasHeight = value; }
144 FloatRect filterPrimitiveSubregion() const { return m_filterPrimitiveSubregion; }
145 void setFilterPrimitiveSubregion(const FloatRect& filterPrimitiveSubregion) { m_filterPrimitiveSubregion = filterPrimitiveSubregion; }
147 FloatRect effectBoundaries() const { return m_effectBoundaries; }
148 void setEffectBoundaries(const FloatRect& effectBoundaries) { m_effectBoundaries = effectBoundaries; }
150 Filter& filter() { return m_filter; }
152 bool clipsToBounds() const { return m_clipsToBounds; }
153 void setClipsToBounds(bool value) { m_clipsToBounds = value; }
155 ColorSpace operatingColorSpace() const { return m_operatingColorSpace; }
156 virtual void setOperatingColorSpace(ColorSpace colorSpace) { m_operatingColorSpace = colorSpace; }
157 ColorSpace resultColorSpace() const { return m_resultColorSpace; }
158 virtual void setResultColorSpace(ColorSpace colorSpace) { m_resultColorSpace = colorSpace; }
160 virtual void transformResultColorSpace(FilterEffect* in, const int) { in->transformResultColorSpace(m_operatingColorSpace); }
161 void transformResultColorSpace(ColorSpace);
164 FilterEffect(Filter&);
166 ImageBuffer* createImageBufferResult();
167 Uint8ClampedArray* createUnmultipliedImageResult();
168 Uint8ClampedArray* createPremultipliedImageResult();
170 OpenCLHandle createOpenCLImageResult(uint8_t* = 0);
173 // Return true if the filter will only operate correctly on valid RGBA values, with
174 // alpha in [0,255] and each color component in [0, alpha].
175 virtual bool requiresValidPreMultipliedPixels() { return true; }
177 // If a pre-multiplied image, check every pixel for validity and correct if necessary.
178 void forceValidPreMultipliedPixels();
181 std::unique_ptr<ImageBuffer> m_imageBufferResult;
182 RefPtr<Uint8ClampedArray> m_unmultipliedImageResult;
183 RefPtr<Uint8ClampedArray> m_premultipliedImageResult;
184 FilterEffectVector m_inputEffects;
186 OpenCLHandle m_openCLImageResult;
191 IntRect m_absolutePaintRect;
193 // The maximum size of a filter primitive. In SVG this is the primitive subregion in absolute coordinate space.
194 // The absolute paint rect should never be bigger than m_maxEffectRect.
195 FloatRect m_maxEffectRect;
199 inline void copyImageBytes(Uint8ClampedArray* source, Uint8ClampedArray* destination, const IntRect&);
201 // The following member variables are SVG specific and will move to RenderSVGResourceFilterPrimitive.
202 // See bug https://bugs.webkit.org/show_bug.cgi?id=45614.
204 // The subregion of a filter primitive according to the SVG Filter specification in local coordinates.
205 // This is SVG specific and needs to move to RenderSVGResourceFilterPrimitive.
206 FloatRect m_filterPrimitiveSubregion;
208 // x, y, width and height of the actual SVGFE*Element. Is needed to determine the subregion of the
209 // filter primitive on a later step.
210 FloatRect m_effectBoundaries;
216 // Should the effect clip to its primitive region, or expand to use the combined region of its inputs.
217 bool m_clipsToBounds;
219 ColorSpace m_operatingColorSpace;
220 ColorSpace m_resultColorSpace;
223 } // namespace WebCore
225 #endif // FilterEffect_h