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 "platform/graphics/filters/Filter.h"
29#include "platform/graphics/filters/FilterEffect.h"
30
31namespace blink {
32
33enum TurbulenceType {
34    FETURBULENCE_TYPE_UNKNOWN = 0,
35    FETURBULENCE_TYPE_FRACTALNOISE = 1,
36    FETURBULENCE_TYPE_TURBULENCE = 2
37};
38
39class PLATFORM_EXPORT 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 TextStream& externalRepresentation(TextStream&, int indention) const OVERRIDE;
64
65private:
66    static const int s_blockSize = 256;
67    static const int s_blockMask = s_blockSize - 1;
68
69    static const int s_minimalRectDimension = (100 * 100); // Empirical data limit for parallel jobs.
70
71    struct PaintingData {
72        PaintingData(long paintingSeed, const IntSize& paintingSize)
73            : seed(paintingSeed)
74            , filterSize(paintingSize)
75        {
76        }
77
78        long seed;
79        int latticeSelector[2 * s_blockSize + 2];
80        float gradient[4][2 * s_blockSize + 2][2];
81        IntSize filterSize;
82
83        inline long random();
84    };
85
86    struct StitchData {
87        StitchData()
88            : width(0)
89            , wrapX(0)
90            , height(0)
91            , wrapY(0)
92        {
93        }
94
95        int width; // How much to subtract to wrap for stitching.
96        int wrapX; // Minimum value to wrap.
97        int height;
98        int wrapY;
99    };
100
101    template<typename Type>
102    friend class ParallelJobs;
103
104    struct FillRegionParameters {
105        FETurbulence* filter;
106        Uint8ClampedArray* pixelArray;
107        PaintingData* paintingData;
108        int startY;
109        int endY;
110        float baseFrequencyX;
111        float baseFrequencyY;
112    };
113
114    static void fillRegionWorker(FillRegionParameters*);
115
116    FETurbulence(Filter*, TurbulenceType, float, float, int, float, bool);
117
118    virtual void applySoftware() OVERRIDE;
119    virtual PassRefPtr<SkImageFilter> createImageFilter(SkiaImageFilterBuilder*) OVERRIDE;
120    SkShader* createShader();
121
122    inline void initPaint(PaintingData&);
123    float noise2D(int channel, PaintingData&, StitchData&, const FloatPoint&);
124    unsigned char calculateTurbulenceValueForPoint(int channel, PaintingData&, StitchData&, const FloatPoint&, float, float);
125    inline void fillRegion(Uint8ClampedArray*, PaintingData&, int, int, float, float);
126
127    TurbulenceType m_type;
128    float m_baseFrequencyX;
129    float m_baseFrequencyY;
130    int m_numOctaves;
131    float m_seed;
132    bool m_stitchTiles;
133};
134
135} // namespace blink
136
137#endif // FETurbulence_h
138