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 * Copyright (C) 2013 Google Inc. All rights reserved.
8 *
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Library General Public
11 * License as published by the Free Software Foundation; either
12 * version 2 of the License, or (at your option) any later version.
13 *
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17 * Library General Public License for more details.
18 *
19 * You should have received a copy of the GNU Library General Public License
20 * along with this library; see the file COPYING.LIB.  If not, write to
21 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
22 * Boston, MA 02110-1301, USA.
23 */
24
25#ifndef FETurbulence_h
26#define FETurbulence_h
27
28#include "core/platform/graphics/filters/Filter.h"
29#include "core/platform/graphics/filters/FilterEffect.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 determineAbsolutePaintRect() { setAbsolutePaintRect(enclosingIntRect(maxEffectRect())); }
64
65    virtual TextStream& externalRepresentation(TextStream&, int indention) const;
66
67private:
68    static const int s_blockSize = 256;
69    static const int s_blockMask = s_blockSize - 1;
70
71    static const int s_minimalRectDimension = (100 * 100); // Empirical data limit for parallel jobs.
72
73    struct PaintingData {
74        PaintingData(long paintingSeed, const IntSize& paintingSize)
75            : seed(paintingSeed)
76            , filterSize(paintingSize)
77        {
78        }
79
80        long seed;
81        int latticeSelector[2 * s_blockSize + 2];
82        float gradient[4][2 * s_blockSize + 2][2];
83        IntSize filterSize;
84
85        inline long random();
86    };
87
88    struct StitchData {
89        StitchData()
90            : width(0)
91            , wrapX(0)
92            , height(0)
93            , wrapY(0)
94        {
95        }
96
97        int width; // How much to subtract to wrap for stitching.
98        int wrapX; // Minimum value to wrap.
99        int height;
100        int wrapY;
101    };
102
103    template<typename Type>
104    friend class ParallelJobs;
105
106    struct FillRegionParameters {
107        FETurbulence* filter;
108        Uint8ClampedArray* pixelArray;
109        PaintingData* paintingData;
110        int startY;
111        int endY;
112        float baseFrequencyX;
113        float baseFrequencyY;
114    };
115
116    static void fillRegionWorker(FillRegionParameters*);
117
118    FETurbulence(Filter*, TurbulenceType, float, float, int, float, bool);
119
120    virtual void applySoftware() OVERRIDE;
121    virtual bool applySkia() OVERRIDE;
122    virtual PassRefPtr<SkImageFilter> createImageFilter(SkiaImageFilterBuilder*) OVERRIDE;
123    SkShader* createShader(const IntRect& filterRegion);
124
125    inline void initPaint(PaintingData&);
126    float noise2D(int channel, PaintingData&, StitchData&, const FloatPoint&);
127    unsigned char calculateTurbulenceValueForPoint(int channel, PaintingData&, StitchData&, const FloatPoint&, float, float);
128    inline void fillRegion(Uint8ClampedArray*, PaintingData&, int, int, float, float);
129
130    TurbulenceType m_type;
131    float m_baseFrequencyX;
132    float m_baseFrequencyY;
133    int m_numOctaves;
134    float m_seed;
135    bool m_stitchTiles;
136};
137
138} // namespace WebCore
139
140#endif // FETurbulence_h
141