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*/ 25class SK_API SkPerlinNoiseShader2 : public SkShader { 26public: 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 SkShader* CreateFractalNoise(SkScalar baseFrequencyX, SkScalar baseFrequencyY, 65 int numOctaves, SkScalar seed, 66 const SkISize* tileSize = NULL); 67 static SkShader* CreateTurbulence(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 SkShader* CreateImprovedNoise(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 SkShader* CreateTubulence(SkScalar baseFrequencyX, SkScalar baseFrequencyY, 81 int numOctaves, SkScalar seed, 82 const SkISize* tileSize = NULL) { 83 return CreateTurbulence(baseFrequencyX, baseFrequencyY, numOctaves, seed, tileSize); 84 } 85 86 87 size_t contextSize(const ContextRec&) const override; 88 89 class PerlinNoiseShaderContext : public SkShader::Context { 90 public: 91 PerlinNoiseShaderContext(const SkPerlinNoiseShader2& shader, const ContextRec&); 92 virtual ~PerlinNoiseShaderContext(); 93 94 void shadeSpan(int x, int y, SkPMColor[], int count) override; 95 96 private: 97 SkPMColor shade(const SkPoint& point, StitchData& stitchData) const; 98 SkScalar calculateTurbulenceValueForPoint( 99 int channel, 100 StitchData& stitchData, const SkPoint& point) const; 101 SkScalar calculateImprovedNoiseValueForPoint(int channel, const SkPoint& point) const; 102 SkScalar noise2D(int channel, 103 const StitchData& stitchData, const SkPoint& noiseVector) const; 104 105 SkMatrix fMatrix; 106 PaintingData* fPaintingData; 107 108 typedef SkShader::Context INHERITED; 109 }; 110 111#if SK_SUPPORT_GPU 112 const GrFragmentProcessor* asFragmentProcessor(GrContext* context, const SkMatrix& viewM, 113 const SkMatrix*, SkFilterQuality) const override; 114#endif 115 116 SK_TO_STRING_OVERRIDE() 117 SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(SkPerlinNoiseShader2) 118 119protected: 120 void flatten(SkWriteBuffer&) const override; 121 Context* onCreateContext(const ContextRec&, void* storage) const override; 122 123private: 124 SkPerlinNoiseShader2(SkPerlinNoiseShader2::Type type, SkScalar baseFrequencyX, 125 SkScalar baseFrequencyY, int numOctaves, SkScalar seed, 126 const SkISize* tileSize); 127 virtual ~SkPerlinNoiseShader2(); 128 129 const SkPerlinNoiseShader2::Type fType; 130 const SkScalar fBaseFrequencyX; 131 const SkScalar fBaseFrequencyY; 132 const int fNumOctaves; 133 const SkScalar fSeed; 134 const SkISize fTileSize; 135 const bool fStitchTiles; 136 137 typedef SkShader INHERITED; 138}; 139 140#endif 141