blob: 60dc53a6a2b11fce45aa183b5c2f3b182b8a0e9a [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 */
reedfe630452016-03-25 09:08:00 -070058 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.come3b4c502013-04-05 13:47:09 +000066 static SkShader* CreateFractalNoise(SkScalar baseFrequencyX, SkScalar baseFrequencyY,
67 int numOctaves, SkScalar seed,
reedfe630452016-03-25 09:08:00 -070068 const SkISize* tileSize = NULL) {
69 return MakeFractalNoise(baseFrequencyX, baseFrequencyY, numOctaves, seed, tileSize).release();
70 }
commit-bot@chromium.org9fbbcca2014-04-01 16:09:37 +000071 static SkShader* CreateTurbulence(SkScalar baseFrequencyX, SkScalar baseFrequencyY,
reedfe630452016-03-25 09:08:00 -070072 int numOctaves, SkScalar seed,
73 const SkISize* tileSize = NULL) {
74 return MakeTurbulence(baseFrequencyX, baseFrequencyY, numOctaves, seed, tileSize).release();
75 }
commit-bot@chromium.org9fbbcca2014-04-01 16:09:37 +000076 static SkShader* CreateTubulence(SkScalar baseFrequencyX, SkScalar baseFrequencyY,
77 int numOctaves, SkScalar seed,
78 const SkISize* tileSize = NULL) {
reed773ceda2016-03-03 18:18:25 -080079 return CreateTurbulence(baseFrequencyX, baseFrequencyY, numOctaves, seed, tileSize);
commit-bot@chromium.org9fbbcca2014-04-01 16:09:37 +000080 }
reedfe630452016-03-25 09:08:00 -070081#endif
commit-bot@chromium.org9fbbcca2014-04-01 16:09:37 +000082
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +000083 class PerlinNoiseShaderContext : public SkShader::Context {
84 public:
commit-bot@chromium.orge901b6d2014-05-01 19:31:31 +000085 PerlinNoiseShaderContext(const SkPerlinNoiseShader& shader, const ContextRec&);
senorblancoca6a7c22014-06-27 13:35:52 -070086 virtual ~PerlinNoiseShaderContext();
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +000087
mtklein36352bf2015-03-25 18:17:31 -070088 void shadeSpan(int x, int y, SkPMColor[], int count) override;
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +000089
90 private:
91 SkPMColor shade(const SkPoint& point, StitchData& stitchData) const;
92 SkScalar calculateTurbulenceValueForPoint(
senorblancoca6a7c22014-06-27 13:35:52 -070093 int channel,
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +000094 StitchData& stitchData, const SkPoint& point) const;
senorblancoca6a7c22014-06-27 13:35:52 -070095 SkScalar noise2D(int channel,
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +000096 const StitchData& stitchData, const SkPoint& noiseVector) const;
97
98 SkMatrix fMatrix;
senorblancoca6a7c22014-06-27 13:35:52 -070099 PaintingData* fPaintingData;
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000100
101 typedef SkShader::Context INHERITED;
102 };
sugoi@google.come3b4c502013-04-05 13:47:09 +0000103
bsalomonc21b09e2015-08-28 18:46:56 -0700104#if SK_SUPPORT_GPU
brianosman839345d2016-07-22 11:04:53 -0700105 sk_sp<GrFragmentProcessor> asFragmentProcessor(const AsFPArgs&) const override;
bsalomonc21b09e2015-08-28 18:46:56 -0700106#endif
sugoi@google.come3b4c502013-04-05 13:47:09 +0000107
commit-bot@chromium.org0f10f7b2014-03-13 18:02:17 +0000108 SK_TO_STRING_OVERRIDE()
sugoi@google.come3b4c502013-04-05 13:47:09 +0000109 SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(SkPerlinNoiseShader)
110
111protected:
mtklein36352bf2015-03-25 18:17:31 -0700112 void flatten(SkWriteBuffer&) const override;
113 Context* onCreateContext(const ContextRec&, void* storage) const override;
reed773ceda2016-03-03 18:18:25 -0800114 size_t onContextSize(const ContextRec&) const override;
sugoi@google.come3b4c502013-04-05 13:47:09 +0000115
116private:
117 SkPerlinNoiseShader(SkPerlinNoiseShader::Type type, SkScalar baseFrequencyX,
118 SkScalar baseFrequencyY, int numOctaves, SkScalar seed,
commit-bot@chromium.orgfd5c9a62014-03-06 15:13:53 +0000119 const SkISize* tileSize);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000120 virtual ~SkPerlinNoiseShader();
121
mtklein3f3b3d02014-12-01 11:47:08 -0800122 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.come3b4c502013-04-05 13:47:09 +0000129
sugoi@google.come3b4c502013-04-05 13:47:09 +0000130 typedef SkShader INHERITED;
131};
132
133#endif