Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* |
michael@0 | 2 | * Copyright 2013 Google Inc. |
michael@0 | 3 | * |
michael@0 | 4 | * Use of this source code is governed by a BSD-style license that can be |
michael@0 | 5 | * found in the LICENSE file. |
michael@0 | 6 | */ |
michael@0 | 7 | |
michael@0 | 8 | #ifndef SkPerlinNoiseShader_DEFINED |
michael@0 | 9 | #define SkPerlinNoiseShader_DEFINED |
michael@0 | 10 | |
michael@0 | 11 | #include "SkShader.h" |
michael@0 | 12 | |
michael@0 | 13 | /** \class SkPerlinNoiseShader |
michael@0 | 14 | |
michael@0 | 15 | SkPerlinNoiseShader creates an image using the Perlin turbulence function. |
michael@0 | 16 | |
michael@0 | 17 | It can produce tileable noise if asked to stitch tiles and provided a tile size. |
michael@0 | 18 | In order to fill a large area with repeating noise, set the stitchTiles flag to |
michael@0 | 19 | true, and render exactly a single tile of noise. Without this flag, the result |
michael@0 | 20 | will contain visible seams between tiles. |
michael@0 | 21 | |
michael@0 | 22 | The algorithm used is described here : |
michael@0 | 23 | http://www.w3.org/TR/SVG/filters.html#feTurbulenceElement |
michael@0 | 24 | */ |
michael@0 | 25 | class SK_API SkPerlinNoiseShader : public SkShader { |
michael@0 | 26 | struct PaintingData; |
michael@0 | 27 | public: |
michael@0 | 28 | struct StitchData; |
michael@0 | 29 | |
michael@0 | 30 | /** |
michael@0 | 31 | * About the noise types : the difference between the 2 is just minor tweaks to the algorithm, |
michael@0 | 32 | * they're not 2 entirely different noises. The output looks different, but once the noise is |
michael@0 | 33 | * generated in the [1, -1] range, the output is brought back in the [0, 1] range by doing : |
michael@0 | 34 | * kFractalNoise_Type : noise * 0.5 + 0.5 |
michael@0 | 35 | * kTurbulence_Type : abs(noise) |
michael@0 | 36 | * Very little differences between the 2 types, although you can tell the difference visually. |
michael@0 | 37 | */ |
michael@0 | 38 | enum Type { |
michael@0 | 39 | kFractalNoise_Type, |
michael@0 | 40 | kTurbulence_Type, |
michael@0 | 41 | kFirstType = kFractalNoise_Type, |
michael@0 | 42 | kLastType = kTurbulence_Type |
michael@0 | 43 | }; |
michael@0 | 44 | /** |
michael@0 | 45 | * This will construct Perlin noise of the given type (Fractal Noise or Turbulence). |
michael@0 | 46 | * |
michael@0 | 47 | * Both base frequencies (X and Y) have a usual range of (0..1). |
michael@0 | 48 | * |
michael@0 | 49 | * The number of octaves provided should be fairly small, although no limit is enforced. |
michael@0 | 50 | * Each octave doubles the frequency, so 10 octaves would produce noise from |
michael@0 | 51 | * baseFrequency * 1, * 2, * 4, ..., * 512, which quickly yields insignificantly small |
michael@0 | 52 | * periods and resembles regular unstructured noise rather than Perlin noise. |
michael@0 | 53 | * |
michael@0 | 54 | * If tileSize isn't NULL or an empty size, the tileSize parameter will be used to modify |
michael@0 | 55 | * the frequencies so that the noise will be tileable for the given tile size. If tileSize |
michael@0 | 56 | * is NULL or an empty size, the frequencies will be used as is without modification. |
michael@0 | 57 | */ |
michael@0 | 58 | static SkShader* CreateFractalNoise(SkScalar baseFrequencyX, SkScalar baseFrequencyY, |
michael@0 | 59 | int numOctaves, SkScalar seed, |
michael@0 | 60 | const SkISize* tileSize = NULL); |
michael@0 | 61 | static SkShader* CreateTubulence(SkScalar baseFrequencyX, SkScalar baseFrequencyY, |
michael@0 | 62 | int numOctaves, SkScalar seed, |
michael@0 | 63 | const SkISize* tileSize = NULL); |
michael@0 | 64 | |
michael@0 | 65 | virtual bool setContext(const SkBitmap& device, const SkPaint& paint, |
michael@0 | 66 | const SkMatrix& matrix); |
michael@0 | 67 | virtual void shadeSpan(int x, int y, SkPMColor[], int count) SK_OVERRIDE; |
michael@0 | 68 | virtual void shadeSpan16(int x, int y, uint16_t[], int count) SK_OVERRIDE; |
michael@0 | 69 | |
michael@0 | 70 | virtual GrEffectRef* asNewEffect(GrContext* context, const SkPaint&) const SK_OVERRIDE; |
michael@0 | 71 | |
michael@0 | 72 | SK_TO_STRING_OVERRIDE() |
michael@0 | 73 | SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(SkPerlinNoiseShader) |
michael@0 | 74 | |
michael@0 | 75 | protected: |
michael@0 | 76 | SkPerlinNoiseShader(SkReadBuffer&); |
michael@0 | 77 | virtual void flatten(SkWriteBuffer&) const SK_OVERRIDE; |
michael@0 | 78 | |
michael@0 | 79 | private: |
michael@0 | 80 | SkPerlinNoiseShader(SkPerlinNoiseShader::Type type, SkScalar baseFrequencyX, |
michael@0 | 81 | SkScalar baseFrequencyY, int numOctaves, SkScalar seed, |
michael@0 | 82 | const SkISize* tileSize); |
michael@0 | 83 | virtual ~SkPerlinNoiseShader(); |
michael@0 | 84 | |
michael@0 | 85 | SkScalar noise2D(int channel, const PaintingData& paintingData, |
michael@0 | 86 | const StitchData& stitchData, const SkPoint& noiseVector) const; |
michael@0 | 87 | |
michael@0 | 88 | SkScalar calculateTurbulenceValueForPoint(int channel, const PaintingData& paintingData, |
michael@0 | 89 | StitchData& stitchData, const SkPoint& point) const; |
michael@0 | 90 | |
michael@0 | 91 | SkPMColor shade(const SkPoint& point, StitchData& stitchData) const; |
michael@0 | 92 | |
michael@0 | 93 | // TODO (scroggo): Once all SkShaders are created from a factory, and we have removed the |
michael@0 | 94 | // constructor that creates SkPerlinNoiseShader from an SkReadBuffer, several fields can |
michael@0 | 95 | // be made constant. |
michael@0 | 96 | /*const*/ SkPerlinNoiseShader::Type fType; |
michael@0 | 97 | /*const*/ SkScalar fBaseFrequencyX; |
michael@0 | 98 | /*const*/ SkScalar fBaseFrequencyY; |
michael@0 | 99 | /*const*/ int fNumOctaves; |
michael@0 | 100 | /*const*/ SkScalar fSeed; |
michael@0 | 101 | /*const*/ SkISize fTileSize; |
michael@0 | 102 | /*const*/ bool fStitchTiles; |
michael@0 | 103 | // TODO (scroggo): Once setContext creates a new object, place this on that object. |
michael@0 | 104 | SkMatrix fMatrix; |
michael@0 | 105 | |
michael@0 | 106 | PaintingData* fPaintingData; |
michael@0 | 107 | |
michael@0 | 108 | typedef SkShader INHERITED; |
michael@0 | 109 | }; |
michael@0 | 110 | |
michael@0 | 111 | #endif |