Home | History | Annotate | Download | only in filters
      1 /*
      2  * Copyright (C) 2008 Alex Mathews <possessedpenguinbob (at) gmail.com>
      3  * Copyright (C) 2009 Dirk Schulze <krit (at) webkit.org>
      4  * Copyright (C) Research In Motion Limited 2010. All rights reserved.
      5  *
      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.
     10  *
     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.
     15  *
     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.
     20  */
     21 
     22 #ifndef FilterEffect_h
     23 #define FilterEffect_h
     24 
     25 #if ENABLE(FILTERS)
     26 #include "FloatRect.h"
     27 #include "IntRect.h"
     28 
     29 #include <wtf/ByteArray.h>
     30 #include <wtf/PassOwnPtr.h>
     31 #include <wtf/RefCounted.h>
     32 #include <wtf/RefPtr.h>
     33 #include <wtf/Vector.h>
     34 
     35 static const float kMaxFilterSize = 5000.0f;
     36 
     37 namespace WebCore {
     38 
     39 class Filter;
     40 class FilterEffect;
     41 class ImageBuffer;
     42 class TextStream;
     43 
     44 typedef Vector<RefPtr<FilterEffect> > FilterEffectVector;
     45 
     46 enum FilterEffectType {
     47     FilterEffectTypeUnknown,
     48     FilterEffectTypeImage,
     49     FilterEffectTypeTile,
     50     FilterEffectTypeSourceInput
     51 };
     52 
     53 class FilterEffect : public RefCounted<FilterEffect> {
     54 public:
     55     virtual ~FilterEffect();
     56 
     57     bool hasResult() const { return m_imageBufferResult || m_unmultipliedImageResult || m_premultipliedImageResult; }
     58     void clearResult();
     59     ImageBuffer* asImageBuffer();
     60     PassRefPtr<ByteArray> asUnmultipliedImage(const IntRect&);
     61     PassRefPtr<ByteArray> asPremultipliedImage(const IntRect&);
     62     void copyUnmultipliedImage(ByteArray* destination, const IntRect&);
     63     void copyPremultipliedImage(ByteArray* destination, const IntRect&);
     64 
     65     FilterEffectVector& inputEffects() { return m_inputEffects; }
     66     FilterEffect* inputEffect(unsigned) const;
     67     unsigned numberOfEffectInputs() const { return m_inputEffects.size(); }
     68 
     69     IntRect drawingRegionOfInputImage(const IntRect&) const;
     70     IntRect requestedRegionOfInputImageData(const IntRect&) const;
     71 
     72     // Solid black image with different alpha values.
     73     bool isAlphaImage() const { return m_alphaImage; }
     74     void setIsAlphaImage(bool alphaImage) { m_alphaImage = alphaImage; }
     75 
     76     IntRect absolutePaintRect() const { return m_absolutePaintRect; }
     77     void setAbsolutePaintRect(const IntRect& absolutePaintRect) { m_absolutePaintRect = absolutePaintRect; }
     78 
     79     FloatRect maxEffectRect() const { return m_maxEffectRect; }
     80     void setMaxEffectRect(const FloatRect& maxEffectRect) { m_maxEffectRect = maxEffectRect; }
     81 
     82     virtual void apply() = 0;
     83     virtual void dump() = 0;
     84 
     85     virtual void determineAbsolutePaintRect();
     86 
     87     virtual FilterEffectType filterEffectType() const { return FilterEffectTypeUnknown; }
     88 
     89     virtual TextStream& externalRepresentation(TextStream&, int indention = 0) const;
     90 
     91 public:
     92     // The following functions are SVG specific and will move to RenderSVGResourceFilterPrimitive.
     93     // See bug https://bugs.webkit.org/show_bug.cgi?id=45614.
     94     bool hasX() const { return m_hasX; }
     95     void setHasX(bool value) { m_hasX = value; }
     96 
     97     bool hasY() const { return m_hasY; }
     98     void setHasY(bool value) { m_hasY = value; }
     99 
    100     bool hasWidth() const { return m_hasWidth; }
    101     void setHasWidth(bool value) { m_hasWidth = value; }
    102 
    103     bool hasHeight() const { return m_hasHeight; }
    104     void setHasHeight(bool value) { m_hasHeight = value; }
    105 
    106     FloatRect filterPrimitiveSubregion() const { return m_filterPrimitiveSubregion; }
    107     void setFilterPrimitiveSubregion(const FloatRect& filterPrimitiveSubregion) { m_filterPrimitiveSubregion = filterPrimitiveSubregion; }
    108 
    109     FloatRect effectBoundaries() const { return m_effectBoundaries; }
    110     void setEffectBoundaries(const FloatRect& effectBoundaries) { m_effectBoundaries = effectBoundaries; }
    111 
    112     Filter* filter() { return m_filter; }
    113 
    114 protected:
    115     FilterEffect(Filter*);
    116 
    117     ImageBuffer* createImageBufferResult();
    118     ByteArray* createUnmultipliedImageResult();
    119     ByteArray* createPremultipliedImageResult();
    120 
    121 private:
    122     OwnPtr<ImageBuffer> m_imageBufferResult;
    123     RefPtr<ByteArray> m_unmultipliedImageResult;
    124     RefPtr<ByteArray> m_premultipliedImageResult;
    125     FilterEffectVector m_inputEffects;
    126 
    127     bool m_alphaImage;
    128 
    129     IntRect m_absolutePaintRect;
    130 
    131     // The maximum size of a filter primitive. In SVG this is the primitive subregion in absolute coordinate space.
    132     // The absolute paint rect should never be bigger than m_maxEffectRect.
    133     FloatRect m_maxEffectRect;
    134     Filter* m_filter;
    135 
    136 private:
    137     inline void copyImageBytes(ByteArray* source, ByteArray* destination, const IntRect&);
    138 
    139     // The following member variables are SVG specific and will move to RenderSVGResourceFilterPrimitive.
    140     // See bug https://bugs.webkit.org/show_bug.cgi?id=45614.
    141 
    142     // The subregion of a filter primitive according to the SVG Filter specification in local coordinates.
    143     // This is SVG specific and needs to move to RenderSVGResourceFilterPrimitive.
    144     FloatRect m_filterPrimitiveSubregion;
    145 
    146     // x, y, width and height of the actual SVGFE*Element. Is needed to determine the subregion of the
    147     // filter primitive on a later step.
    148     FloatRect m_effectBoundaries;
    149     bool m_hasX;
    150     bool m_hasY;
    151     bool m_hasWidth;
    152     bool m_hasHeight;
    153 };
    154 
    155 } // namespace WebCore
    156 
    157 #endif // ENABLE(FILTERS)
    158 
    159 #endif // FilterEffect_h
    160