2 * Copyright 2013 Google Inc.
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
8 #ifndef SkPerlinNoiseShader_DEFINED
9 #define SkPerlinNoiseShader_DEFINED
13 /** \class SkPerlinNoiseShader
15 SkPerlinNoiseShader creates an image using the Perlin turbulence function.
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.
22 The algorithm used is described here :
23 http://www.w3.org/TR/SVG/filters.html#feTurbulenceElement
25 class SK_API SkPerlinNoiseShader : public SkShader {
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.
41 kFirstType = kFractalNoise_Type,
42 kLastType = kTurbulence_Type
45 * This will construct Perlin noise of the given type (Fractal Noise or Turbulence).
47 * Both base frequencies (X and Y) have a usual range of (0..1).
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.
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.
58 static SkShader* CreateFractalNoise(SkScalar baseFrequencyX, SkScalar baseFrequencyY,
59 int numOctaves, SkScalar seed,
60 const SkISize* tileSize = NULL);
61 static SkShader* CreateTubulence(SkScalar baseFrequencyX, SkScalar baseFrequencyY,
62 int numOctaves, SkScalar seed,
63 const SkISize* tileSize = NULL);
65 virtual bool setContext(const SkBitmap& device, const SkPaint& paint,
66 const SkMatrix& matrix);
67 virtual void shadeSpan(int x, int y, SkPMColor[], int count) SK_OVERRIDE;
68 virtual void shadeSpan16(int x, int y, uint16_t[], int count) SK_OVERRIDE;
70 virtual GrEffectRef* asNewEffect(GrContext* context, const SkPaint&) const SK_OVERRIDE;
72 SK_DEVELOPER_TO_STRING()
73 SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(SkPerlinNoiseShader)
76 SkPerlinNoiseShader(SkReadBuffer&);
77 virtual void flatten(SkWriteBuffer&) const SK_OVERRIDE;
80 SkPerlinNoiseShader(SkPerlinNoiseShader::Type type, SkScalar baseFrequencyX,
81 SkScalar baseFrequencyY, int numOctaves, SkScalar seed,
82 const SkISize* tileSize = NULL);
83 virtual ~SkPerlinNoiseShader();
85 void setTileSize(const SkISize&);
87 void initPaint(PaintingData& paintingData);
89 SkScalar noise2D(int channel, const PaintingData& paintingData,
90 const StitchData& stitchData, const SkPoint& noiseVector);
92 SkScalar calculateTurbulenceValueForPoint(int channel, const PaintingData& paintingData,
93 StitchData& stitchData, const SkPoint& point);
95 SkPMColor shade(const SkPoint& point, StitchData& stitchData);
97 SkPerlinNoiseShader::Type fType;
98 SkScalar fBaseFrequencyX;
99 SkScalar fBaseFrequencyY;
106 PaintingData* fPaintingData;
108 typedef SkShader INHERITED;