• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 SkPerlinNoiseShader2_DEFINED
9 #define SkPerlinNoiseShader2_DEFINED
10 
11 #include "SkShader.h"
12 
13 /** \class SkPerlinNoiseShader2
14 
15     SkPerlinNoiseShader2 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 SkPerlinNoiseShader2 : public SkShader {
26 public:
27     struct StitchData;
28     struct PaintingData;
29 
30     /**
31      *  About the noise types : the difference between the first 2 is just minor tweaks to the
32      *  algorithm, they're not 2 entirely different noises. The output looks different, but once the
33      *  noise is generated in the [1, -1] range, the output is brought back in the [0, 1] range by
34      *  doing :
35      *  kFractalNoise_Type : noise * 0.5 + 0.5
36      *  kTurbulence_Type   : abs(noise)
37      *  Very little differences between the 2 types, although you can tell the difference visually.
38      *  "Improved" is based on the Improved Perlin Noise algorithm described at
39      *  http://mrl.nyu.edu/~perlin/noise/. It is quite distinct from the other two, and the noise is
40      *  a 2D slice of a 3D noise texture. Minor changes to the Z coordinate will result in minor
41      *  changes to the noise, making it suitable for animated noise.
42      */
43     enum Type {
44         kFractalNoise_Type,
45         kTurbulence_Type,
46         kImprovedNoise_Type,
47         kFirstType = kFractalNoise_Type,
48         kLastType = kImprovedNoise_Type
49     };
50     /**
51      *  This will construct Perlin noise of the given type (Fractal Noise or Turbulence).
52      *
53      *  Both base frequencies (X and Y) have a usual range of (0..1).
54      *
55      *  The number of octaves provided should be fairly small, although no limit is enforced.
56      *  Each octave doubles the frequency, so 10 octaves would produce noise from
57      *  baseFrequency * 1, * 2, * 4, ..., * 512, which quickly yields insignificantly small
58      *  periods and resembles regular unstructured noise rather than Perlin noise.
59      *
60      *  If tileSize isn't NULL or an empty size, the tileSize parameter will be used to modify
61      *  the frequencies so that the noise will be tileable for the given tile size. If tileSize
62      *  is NULL or an empty size, the frequencies will be used as is without modification.
63      */
64     static sk_sp<SkShader> MakeFractalNoise(SkScalar baseFrequencyX, SkScalar baseFrequencyY,
65                                             int numOctaves, SkScalar seed,
66                                             const SkISize* tileSize = NULL);
67     static sk_sp<SkShader> MakeTurbulence(SkScalar baseFrequencyX, SkScalar baseFrequencyY,
68                                           int numOctaves, SkScalar seed,
69                                           const SkISize* tileSize = NULL);
70     /**
71      * Creates an Improved Perlin Noise shader. The z value is roughly equivalent to the seed of the
72      * other two types, but minor variations to z will only slightly change the noise.
73      */
74     static sk_sp<SkShader> MakeImprovedNoise(SkScalar baseFrequencyX, SkScalar baseFrequencyY,
75                                              int numOctaves, SkScalar z);
76     /**
77      * Create alias for CreateTurbulunce until all Skia users changed
78      * its code to use the new naming
79      */
80     static sk_sp<SkShader> MakeTubulence(SkScalar baseFrequencyX, SkScalar baseFrequencyY,
81                                          int numOctaves, SkScalar seed,
82                                          const SkISize* tileSize = NULL) {
83         return MakeTurbulence(baseFrequencyX, baseFrequencyY, numOctaves, seed, tileSize);
84     }
85 
86     class PerlinNoiseShaderContext : public SkShader::Context {
87     public:
88         PerlinNoiseShaderContext(const SkPerlinNoiseShader2& shader, const ContextRec&);
89         ~PerlinNoiseShaderContext() override;
90 
91         void shadeSpan(int x, int y, SkPMColor[], int count) override;
92 
93     private:
94         SkPMColor shade(const SkPoint& point, StitchData& stitchData) const;
95         SkScalar calculateTurbulenceValueForPoint(
96             int channel,
97             StitchData& stitchData, const SkPoint& point) const;
98         SkScalar calculateImprovedNoiseValueForPoint(int channel, const SkPoint& point) const;
99         SkScalar noise2D(int channel,
100                          const StitchData& stitchData, const SkPoint& noiseVector) const;
101 
102         SkMatrix fMatrix;
103         PaintingData* fPaintingData;
104 
105         typedef SkShader::Context INHERITED;
106     };
107 
108 #if SK_SUPPORT_GPU
109     sk_sp<GrFragmentProcessor> asFragmentProcessor(const AsFPArgs&) const override;
110 #endif
111 
112     SK_TO_STRING_OVERRIDE()
113     SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(SkPerlinNoiseShader2)
114 
115 protected:
116     void flatten(SkWriteBuffer&) const override;
117     Context* onMakeContext(const ContextRec&, SkArenaAlloc*) const override;
118 
119 private:
120     SkPerlinNoiseShader2(SkPerlinNoiseShader2::Type type, SkScalar baseFrequencyX,
121                         SkScalar baseFrequencyY, int numOctaves, SkScalar seed,
122                         const SkISize* tileSize);
123     ~SkPerlinNoiseShader2() override;
124 
125     const SkPerlinNoiseShader2::Type fType;
126     const SkScalar                  fBaseFrequencyX;
127     const SkScalar                  fBaseFrequencyY;
128     const int                       fNumOctaves;
129     const SkScalar                  fSeed;
130     const SkISize                   fTileSize;
131     const bool                      fStitchTiles;
132 
133     typedef SkShader INHERITED;
134 };
135 
136 #endif
137