1 /*
2  * Copyright 2013 Google Inc.
3  *
4  * Use of this source code is governed by a BSD-style license that can be
5  * found in the LICENSE file.
6  */
7 
8 #ifndef SkPerlinNoiseShader_DEFINED
9 #define SkPerlinNoiseShader_DEFINED
10 
11 #include "SkShader.h"
12 
13 /** \class SkPerlinNoiseShader
14 
15     SkPerlinNoiseShader creates an image using the Perlin turbulence function.
16 
17     It can produce tileable noise if asked to stitch tiles and provided a tile size.
18     In order to fill a large area with repeating noise, set the stitchTiles flag to
19     true, and render exactly a single tile of noise. Without this flag, the result
20     will contain visible seams between tiles.
21 
22     The algorithm used is described here :
23     http://www.w3.org/TR/SVG/filters.html#feTurbulenceElement
24 */
25 class SK_API SkPerlinNoiseShader : public SkShader {
26 public:
27     struct StitchData;
28     struct PaintingData;
29 
30     /**
31      *  About the noise types : the difference between the 2 is just minor tweaks to the algorithm,
32      *  they're not 2 entirely different noises. The output looks different, but once the noise is
33      *  generated in the [1, -1] range, the output is brought back in the [0, 1] range by doing :
34      *  kFractalNoise_Type : noise * 0.5 + 0.5
35      *  kTurbulence_Type   : abs(noise)
36      *  Very little differences between the 2 types, although you can tell the difference visually.
37      */
38     enum Type {
39         kFractalNoise_Type,
40         kTurbulence_Type,
41         kFirstType = kFractalNoise_Type,
42         kLastType = kTurbulence_Type
43     };
44     /**
45      *  This will construct Perlin noise of the given type (Fractal Noise or Turbulence).
46      *
47      *  Both base frequencies (X and Y) have a usual range of (0..1).
48      *
49      *  The number of octaves provided should be fairly small, although no limit is enforced.
50      *  Each octave doubles the frequency, so 10 octaves would produce noise from
51      *  baseFrequency * 1, * 2, * 4, ..., * 512, which quickly yields insignificantly small
52      *  periods and resembles regular unstructured noise rather than Perlin noise.
53      *
54      *  If tileSize isn't NULL or an empty size, the tileSize parameter will be used to modify
55      *  the frequencies so that the noise will be tileable for the given tile size. If tileSize
56      *  is NULL or an empty size, the frequencies will be used as is without modification.
57      */
58     static sk_sp<SkShader> MakeFractalNoise(SkScalar baseFrequencyX, SkScalar baseFrequencyY,
59                                             int numOctaves, SkScalar seed,
60                                             const SkISize* tileSize = nullptr);
61     static sk_sp<SkShader> MakeTurbulence(SkScalar baseFrequencyX, SkScalar baseFrequencyY,
62                                           int numOctaves, SkScalar seed,
63                                           const SkISize* tileSize = nullptr);
64 
65     class PerlinNoiseShaderContext : public SkShader::Context {
66     public:
67         PerlinNoiseShaderContext(const SkPerlinNoiseShader& shader, const ContextRec&);
68         ~PerlinNoiseShaderContext() override;
69 
70         void shadeSpan(int x, int y, SkPMColor[], int count) override;
71 
72     private:
73         SkPMColor shade(const SkPoint& point, StitchData& stitchData) const;
74         SkScalar calculateTurbulenceValueForPoint(
75             int channel,
76             StitchData& stitchData, const SkPoint& point) const;
77         SkScalar noise2D(int channel,
78                          const StitchData& stitchData, const SkPoint& noiseVector) const;
79 
80         SkMatrix fMatrix;
81         PaintingData* fPaintingData;
82 
83         typedef SkShader::Context INHERITED;
84     };
85 
86 #if SK_SUPPORT_GPU
87     sk_sp<GrFragmentProcessor> asFragmentProcessor(const AsFPArgs&) const override;
88 #endif
89 
90     SK_TO_STRING_OVERRIDE()
91     SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(SkPerlinNoiseShader)
92 
93 protected:
94     void flatten(SkWriteBuffer&) const override;
95     Context* onMakeContext(const ContextRec&, SkArenaAlloc* storage) const override;
96 
97 private:
98     SkPerlinNoiseShader(SkPerlinNoiseShader::Type type, SkScalar baseFrequencyX,
99                         SkScalar baseFrequencyY, int numOctaves, SkScalar seed,
100                         const SkISize* tileSize);
101     ~SkPerlinNoiseShader() override;
102 
103     const SkPerlinNoiseShader::Type fType;
104     const SkScalar                  fBaseFrequencyX;
105     const SkScalar                  fBaseFrequencyY;
106     const int                       fNumOctaves;
107     const SkScalar                  fSeed;
108     const SkISize                   fTileSize;
109     const bool                      fStitchTiles;
110 
111     typedef SkShader INHERITED;
112 };
113 
114 #endif
115