blob: fc6748462ceedbb5c119384fa0931ff598e2b1b6 [file] [log] [blame]
sugoi@google.come3b4c502013-04-05 13:47:09 +00001/*
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*/
sugoi@google.com384f5e22013-04-25 14:54:15 +000025class SK_API SkPerlinNoiseShader : public SkShader {
sugoi@google.come3b4c502013-04-05 13:47:09 +000026public:
27 struct StitchData;
senorblancoca6a7c22014-06-27 13:35:52 -070028 struct PaintingData;
sugoi@google.come3b4c502013-04-05 13:47:09 +000029
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 SkShader* CreateFractalNoise(SkScalar baseFrequencyX, SkScalar baseFrequencyY,
59 int numOctaves, SkScalar seed,
60 const SkISize* tileSize = NULL);
commit-bot@chromium.org9fbbcca2014-04-01 16:09:37 +000061 static SkShader* CreateTurbulence(SkScalar baseFrequencyX, SkScalar baseFrequencyY,
sugoi@google.come3b4c502013-04-05 13:47:09 +000062 int numOctaves, SkScalar seed,
63 const SkISize* tileSize = NULL);
commit-bot@chromium.org9fbbcca2014-04-01 16:09:37 +000064 /**
65 * Create alias for CreateTurbulunce until all Skia users changed
66 * its code to use the new naming
67 */
68 static SkShader* CreateTubulence(SkScalar baseFrequencyX, SkScalar baseFrequencyY,
69 int numOctaves, SkScalar seed,
70 const SkISize* tileSize = NULL) {
skia.committer@gmail.comc282ba82014-04-02 03:05:59 +000071 return CreateTurbulence(baseFrequencyX, baseFrequencyY, numOctaves, seed, tileSize);
commit-bot@chromium.org9fbbcca2014-04-01 16:09:37 +000072 }
73
sugoi@google.come3b4c502013-04-05 13:47:09 +000074
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +000075 virtual size_t contextSize() const SK_OVERRIDE;
76
77 class PerlinNoiseShaderContext : public SkShader::Context {
78 public:
commit-bot@chromium.orge901b6d2014-05-01 19:31:31 +000079 PerlinNoiseShaderContext(const SkPerlinNoiseShader& shader, const ContextRec&);
senorblancoca6a7c22014-06-27 13:35:52 -070080 virtual ~PerlinNoiseShaderContext();
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +000081
82 virtual void shadeSpan(int x, int y, SkPMColor[], int count) SK_OVERRIDE;
83 virtual void shadeSpan16(int x, int y, uint16_t[], int count) SK_OVERRIDE;
84
85 private:
86 SkPMColor shade(const SkPoint& point, StitchData& stitchData) const;
87 SkScalar calculateTurbulenceValueForPoint(
senorblancoca6a7c22014-06-27 13:35:52 -070088 int channel,
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +000089 StitchData& stitchData, const SkPoint& point) const;
senorblancoca6a7c22014-06-27 13:35:52 -070090 SkScalar noise2D(int channel,
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +000091 const StitchData& stitchData, const SkPoint& noiseVector) const;
92
93 SkMatrix fMatrix;
senorblancoca6a7c22014-06-27 13:35:52 -070094 PaintingData* fPaintingData;
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +000095
96 typedef SkShader::Context INHERITED;
97 };
sugoi@google.come3b4c502013-04-05 13:47:09 +000098
joshualittb0a8a372014-09-23 09:50:21 -070099 virtual bool asFragmentProcessor(GrContext* context, const SkPaint&, const SkMatrix*, GrColor*,
100 GrFragmentProcessor**) const SK_OVERRIDE;
sugoi@google.come3b4c502013-04-05 13:47:09 +0000101
commit-bot@chromium.org0f10f7b2014-03-13 18:02:17 +0000102 SK_TO_STRING_OVERRIDE()
sugoi@google.come3b4c502013-04-05 13:47:09 +0000103 SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(SkPerlinNoiseShader)
104
105protected:
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +0000106 virtual void flatten(SkWriteBuffer&) const SK_OVERRIDE;
commit-bot@chromium.orgce56d962014-05-05 18:39:18 +0000107 virtual Context* onCreateContext(const ContextRec&, void* storage) const SK_OVERRIDE;
sugoi@google.come3b4c502013-04-05 13:47:09 +0000108
109private:
110 SkPerlinNoiseShader(SkPerlinNoiseShader::Type type, SkScalar baseFrequencyX,
111 SkScalar baseFrequencyY, int numOctaves, SkScalar seed,
commit-bot@chromium.orgfd5c9a62014-03-06 15:13:53 +0000112 const SkISize* tileSize);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000113 virtual ~SkPerlinNoiseShader();
114
mtklein3f3b3d02014-12-01 11:47:08 -0800115 const SkPerlinNoiseShader::Type fType;
116 const SkScalar fBaseFrequencyX;
117 const SkScalar fBaseFrequencyY;
118 const int fNumOctaves;
119 const SkScalar fSeed;
120 const SkISize fTileSize;
121 const bool fStitchTiles;
sugoi@google.come3b4c502013-04-05 13:47:09 +0000122
sugoi@google.come3b4c502013-04-05 13:47:09 +0000123 typedef SkShader INHERITED;
124};
125
126#endif