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 | struct PaintingData; |
| 27 | public: |
| 28 | struct StitchData; |
| 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 SkShader* CreateFractalNoise(SkScalar baseFrequencyX, SkScalar baseFrequencyY, |
| 59 | int numOctaves, SkScalar seed, |
| 60 | const SkISize* tileSize = NULL); |
commit-bot@chromium.org | 9fbbcca | 2014-04-01 16:09:37 +0000 | [diff] [blame^] | 61 | static SkShader* CreateTurbulence(SkScalar baseFrequencyX, SkScalar baseFrequencyY, |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 62 | int numOctaves, SkScalar seed, |
| 63 | const SkISize* tileSize = NULL); |
commit-bot@chromium.org | 9fbbcca | 2014-04-01 16:09:37 +0000 | [diff] [blame^] | 64 | /** |
| 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) { |
| 71 | return CreateTurbulence(baseFrequencyX, baseFrequencyY, numOctaves, seed, tileSize); |
| 72 | } |
| 73 | |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 74 | |
| 75 | virtual bool setContext(const SkBitmap& device, const SkPaint& paint, |
| 76 | const SkMatrix& matrix); |
| 77 | virtual void shadeSpan(int x, int y, SkPMColor[], int count) SK_OVERRIDE; |
| 78 | virtual void shadeSpan16(int x, int y, uint16_t[], int count) SK_OVERRIDE; |
| 79 | |
| 80 | virtual GrEffectRef* asNewEffect(GrContext* context, const SkPaint&) const SK_OVERRIDE; |
| 81 | |
commit-bot@chromium.org | 0f10f7b | 2014-03-13 18:02:17 +0000 | [diff] [blame] | 82 | SK_TO_STRING_OVERRIDE() |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 83 | SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(SkPerlinNoiseShader) |
| 84 | |
| 85 | protected: |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 86 | SkPerlinNoiseShader(SkReadBuffer&); |
| 87 | virtual void flatten(SkWriteBuffer&) const SK_OVERRIDE; |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 88 | |
| 89 | private: |
| 90 | SkPerlinNoiseShader(SkPerlinNoiseShader::Type type, SkScalar baseFrequencyX, |
| 91 | SkScalar baseFrequencyY, int numOctaves, SkScalar seed, |
commit-bot@chromium.org | fd5c9a6 | 2014-03-06 15:13:53 +0000 | [diff] [blame] | 92 | const SkISize* tileSize); |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 93 | virtual ~SkPerlinNoiseShader(); |
| 94 | |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 95 | SkScalar noise2D(int channel, const PaintingData& paintingData, |
commit-bot@chromium.org | fd5c9a6 | 2014-03-06 15:13:53 +0000 | [diff] [blame] | 96 | const StitchData& stitchData, const SkPoint& noiseVector) const; |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 97 | |
| 98 | SkScalar calculateTurbulenceValueForPoint(int channel, const PaintingData& paintingData, |
commit-bot@chromium.org | fd5c9a6 | 2014-03-06 15:13:53 +0000 | [diff] [blame] | 99 | StitchData& stitchData, const SkPoint& point) const; |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 100 | |
commit-bot@chromium.org | fd5c9a6 | 2014-03-06 15:13:53 +0000 | [diff] [blame] | 101 | SkPMColor shade(const SkPoint& point, StitchData& stitchData) const; |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 102 | |
commit-bot@chromium.org | fd5c9a6 | 2014-03-06 15:13:53 +0000 | [diff] [blame] | 103 | // TODO (scroggo): Once all SkShaders are created from a factory, and we have removed the |
| 104 | // constructor that creates SkPerlinNoiseShader from an SkReadBuffer, several fields can |
| 105 | // be made constant. |
| 106 | /*const*/ SkPerlinNoiseShader::Type fType; |
| 107 | /*const*/ SkScalar fBaseFrequencyX; |
| 108 | /*const*/ SkScalar fBaseFrequencyY; |
| 109 | /*const*/ int fNumOctaves; |
| 110 | /*const*/ SkScalar fSeed; |
| 111 | /*const*/ SkISize fTileSize; |
| 112 | /*const*/ bool fStitchTiles; |
| 113 | // TODO (scroggo): Once setContext creates a new object, place this on that object. |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 114 | SkMatrix fMatrix; |
| 115 | |
| 116 | PaintingData* fPaintingData; |
| 117 | |
| 118 | typedef SkShader INHERITED; |
| 119 | }; |
| 120 | |
| 121 | #endif |