1/*
2 * Copyright (C) 2004, 2005, 2006, 2007 Nikolas Zimmermann <zimmermann@kde.org>
3 * Copyright (C) 2004, 2005 Rob Buis <buis@kde.org>
4 * Copyright (C) 2005 Eric Seidel <eric@webkit.org>
5 * Copyright (C) 2009 Dirk Schulze <krit@webkit.org>
6 * Copyright (C) 2010 Renata Hodovan <reni@inf.u-szeged.hu>
7 *
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Library General Public
10 * License as published by the Free Software Foundation; either
11 * version 2 of the License, or (at your option) any later version.
12 *
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16 * Library General Public License for more details.
17 *
18 * You should have received a copy of the GNU Library General Public License
19 * along with this library; see the file COPYING.LIB.  If not, write to
20 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21 * Boston, MA 02110-1301, USA.
22 */
23
24#ifndef FETurbulence_h
25#define FETurbulence_h
26
27#if ENABLE(FILTERS)
28#include "FilterEffect.h"
29#include "Filter.h"
30
31namespace WebCore {
32
33enum TurbulenceType {
34    FETURBULENCE_TYPE_UNKNOWN = 0,
35    FETURBULENCE_TYPE_FRACTALNOISE = 1,
36    FETURBULENCE_TYPE_TURBULENCE = 2
37};
38
39class FETurbulence : public FilterEffect {
40public:
41    static PassRefPtr<FETurbulence> create(Filter*, TurbulenceType, float, float, int, float, bool);
42
43    TurbulenceType type() const;
44    bool setType(TurbulenceType);
45
46    float baseFrequencyY() const;
47    bool setBaseFrequencyY(float);
48
49    float baseFrequencyX() const;
50    bool setBaseFrequencyX(float);
51
52    float seed() const;
53    bool setSeed(float);
54
55    int numOctaves() const;
56    bool setNumOctaves(int);
57
58    bool stitchTiles() const;
59    bool setStitchTiles(bool);
60
61    static void fillRegionWorker(void*);
62
63    virtual void platformApplySoftware();
64#if ENABLE(OPENCL)
65    virtual bool platformApplyOpenCL();
66#endif
67    virtual void dump();
68
69    virtual void determineAbsolutePaintRect() { setAbsolutePaintRect(enclosingIntRect(maxEffectRect())); }
70
71    virtual TextStream& externalRepresentation(TextStream&, int indention) const;
72
73private:
74    static const int s_blockSize = 256;
75    static const int s_blockMask = s_blockSize - 1;
76
77    static const int s_minimalRectDimension = (100 * 100); // Empirical data limit for parallel jobs.
78
79    struct PaintingData {
80        PaintingData(long paintingSeed, const IntSize& paintingSize)
81            : seed(paintingSeed)
82            , filterSize(paintingSize)
83        {
84        }
85
86        long seed;
87        int latticeSelector[2 * s_blockSize + 2];
88        float gradient[4][2 * s_blockSize + 2][2];
89        IntSize filterSize;
90
91        inline long random();
92    };
93
94    struct StitchData {
95        StitchData()
96            : width(0)
97            , wrapX(0)
98            , height(0)
99            , wrapY(0)
100        {
101        }
102
103        int width; // How much to subtract to wrap for stitching.
104        int wrapX; // Minimum value to wrap.
105        int height;
106        int wrapY;
107    };
108
109    template<typename Type>
110    friend class ParallelJobs;
111
112    struct FillRegionParameters {
113        FETurbulence* filter;
114        Uint8ClampedArray* pixelArray;
115        PaintingData* paintingData;
116        int startY;
117        int endY;
118    };
119
120    static void fillRegionWorker(FillRegionParameters*);
121
122    FETurbulence(Filter*, TurbulenceType, float, float, int, float, bool);
123
124    inline void initPaint(PaintingData&);
125    float noise2D(int channel, PaintingData&, StitchData&, const FloatPoint&);
126    unsigned char calculateTurbulenceValueForPoint(int channel, PaintingData&, StitchData&, const FloatPoint&);
127    inline void fillRegion(Uint8ClampedArray*, PaintingData&, int, int);
128
129    TurbulenceType m_type;
130    float m_baseFrequencyX;
131    float m_baseFrequencyY;
132    int m_numOctaves;
133    float m_seed;
134    bool m_stitchTiles;
135};
136
137} // namespace WebCore
138
139#endif // ENABLE(FILTERS)
140
141#endif // FETurbulence_h
142