sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 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 | */ |
sugoi@google.com | 384f5e2 | 2013-04-25 14:54:15 +0000 | [diff] [blame] | 25 | class SK_API SkPerlinNoiseShader : public SkShader { |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 26 | public: |
| 27 | struct StitchData; |
senorblanco | ca6a7c2 | 2014-06-27 13:35:52 -0700 | [diff] [blame] | 28 | struct PaintingData; |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 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 | */ |
reed | fe63045 | 2016-03-25 09:08:00 -0700 | [diff] [blame] | 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 | #ifdef SK_SUPPORT_LEGACY_CREATESHADER_PTR |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 66 | static SkShader* CreateFractalNoise(SkScalar baseFrequencyX, SkScalar baseFrequencyY, |
| 67 | int numOctaves, SkScalar seed, |
reed | fe63045 | 2016-03-25 09:08:00 -0700 | [diff] [blame] | 68 | const SkISize* tileSize = NULL) { |
| 69 | return MakeFractalNoise(baseFrequencyX, baseFrequencyY, numOctaves, seed, tileSize).release(); |
| 70 | } |
commit-bot@chromium.org | 9fbbcca | 2014-04-01 16:09:37 +0000 | [diff] [blame] | 71 | static SkShader* CreateTurbulence(SkScalar baseFrequencyX, SkScalar baseFrequencyY, |
reed | fe63045 | 2016-03-25 09:08:00 -0700 | [diff] [blame] | 72 | int numOctaves, SkScalar seed, |
| 73 | const SkISize* tileSize = NULL) { |
| 74 | return MakeTurbulence(baseFrequencyX, baseFrequencyY, numOctaves, seed, tileSize).release(); |
| 75 | } |
commit-bot@chromium.org | 9fbbcca | 2014-04-01 16:09:37 +0000 | [diff] [blame] | 76 | static SkShader* CreateTubulence(SkScalar baseFrequencyX, SkScalar baseFrequencyY, |
| 77 | int numOctaves, SkScalar seed, |
| 78 | const SkISize* tileSize = NULL) { |
reed | 773ceda | 2016-03-03 18:18:25 -0800 | [diff] [blame] | 79 | return CreateTurbulence(baseFrequencyX, baseFrequencyY, numOctaves, seed, tileSize); |
commit-bot@chromium.org | 9fbbcca | 2014-04-01 16:09:37 +0000 | [diff] [blame] | 80 | } |
reed | fe63045 | 2016-03-25 09:08:00 -0700 | [diff] [blame] | 81 | #endif |
commit-bot@chromium.org | 9fbbcca | 2014-04-01 16:09:37 +0000 | [diff] [blame] | 82 | |
commit-bot@chromium.org | 87fcd95 | 2014-04-23 19:10:51 +0000 | [diff] [blame] | 83 | class PerlinNoiseShaderContext : public SkShader::Context { |
| 84 | public: |
commit-bot@chromium.org | e901b6d | 2014-05-01 19:31:31 +0000 | [diff] [blame] | 85 | PerlinNoiseShaderContext(const SkPerlinNoiseShader& shader, const ContextRec&); |
senorblanco | ca6a7c2 | 2014-06-27 13:35:52 -0700 | [diff] [blame] | 86 | virtual ~PerlinNoiseShaderContext(); |
commit-bot@chromium.org | 87fcd95 | 2014-04-23 19:10:51 +0000 | [diff] [blame] | 87 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 88 | void shadeSpan(int x, int y, SkPMColor[], int count) override; |
commit-bot@chromium.org | 87fcd95 | 2014-04-23 19:10:51 +0000 | [diff] [blame] | 89 | |
| 90 | private: |
| 91 | SkPMColor shade(const SkPoint& point, StitchData& stitchData) const; |
| 92 | SkScalar calculateTurbulenceValueForPoint( |
senorblanco | ca6a7c2 | 2014-06-27 13:35:52 -0700 | [diff] [blame] | 93 | int channel, |
commit-bot@chromium.org | 87fcd95 | 2014-04-23 19:10:51 +0000 | [diff] [blame] | 94 | StitchData& stitchData, const SkPoint& point) const; |
senorblanco | ca6a7c2 | 2014-06-27 13:35:52 -0700 | [diff] [blame] | 95 | SkScalar noise2D(int channel, |
commit-bot@chromium.org | 87fcd95 | 2014-04-23 19:10:51 +0000 | [diff] [blame] | 96 | const StitchData& stitchData, const SkPoint& noiseVector) const; |
| 97 | |
| 98 | SkMatrix fMatrix; |
senorblanco | ca6a7c2 | 2014-06-27 13:35:52 -0700 | [diff] [blame] | 99 | PaintingData* fPaintingData; |
commit-bot@chromium.org | 87fcd95 | 2014-04-23 19:10:51 +0000 | [diff] [blame] | 100 | |
| 101 | typedef SkShader::Context INHERITED; |
| 102 | }; |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 103 | |
bsalomon | c21b09e | 2015-08-28 18:46:56 -0700 | [diff] [blame] | 104 | #if SK_SUPPORT_GPU |
brianosman | 839345d | 2016-07-22 11:04:53 -0700 | [diff] [blame^] | 105 | sk_sp<GrFragmentProcessor> asFragmentProcessor(const AsFPArgs&) const override; |
bsalomon | c21b09e | 2015-08-28 18:46:56 -0700 | [diff] [blame] | 106 | #endif |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 107 | |
commit-bot@chromium.org | 0f10f7b | 2014-03-13 18:02:17 +0000 | [diff] [blame] | 108 | SK_TO_STRING_OVERRIDE() |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 109 | SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(SkPerlinNoiseShader) |
| 110 | |
| 111 | protected: |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 112 | void flatten(SkWriteBuffer&) const override; |
| 113 | Context* onCreateContext(const ContextRec&, void* storage) const override; |
reed | 773ceda | 2016-03-03 18:18:25 -0800 | [diff] [blame] | 114 | size_t onContextSize(const ContextRec&) const override; |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 115 | |
| 116 | private: |
| 117 | SkPerlinNoiseShader(SkPerlinNoiseShader::Type type, SkScalar baseFrequencyX, |
| 118 | SkScalar baseFrequencyY, int numOctaves, SkScalar seed, |
commit-bot@chromium.org | fd5c9a6 | 2014-03-06 15:13:53 +0000 | [diff] [blame] | 119 | const SkISize* tileSize); |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 120 | virtual ~SkPerlinNoiseShader(); |
| 121 | |
mtklein | 3f3b3d0 | 2014-12-01 11:47:08 -0800 | [diff] [blame] | 122 | const SkPerlinNoiseShader::Type fType; |
| 123 | const SkScalar fBaseFrequencyX; |
| 124 | const SkScalar fBaseFrequencyY; |
| 125 | const int fNumOctaves; |
| 126 | const SkScalar fSeed; |
| 127 | const SkISize fTileSize; |
| 128 | const bool fStitchTiles; |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 129 | |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 130 | typedef SkShader INHERITED; |
| 131 | }; |
| 132 | |
| 133 | #endif |