blob: f66655ce3f48d7823cae3a0fbba6795a862174c7 [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
sugoi@google.come3b4c502013-04-05 13:47:09 +00008#include "SkPerlinNoiseShader.h"
commit-bot@chromium.orgc2a0ea62013-11-06 10:08:38 +00009#include "SkColorFilter.h"
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +000010#include "SkReadBuffer.h"
11#include "SkWriteBuffer.h"
sugoi@google.come3b4c502013-04-05 13:47:09 +000012#include "SkShader.h"
13#include "SkUnPreMultiply.h"
14#include "SkString.h"
15
16#if SK_SUPPORT_GPU
17#include "GrContext.h"
bsalomon@google.com77af6802013-10-02 13:04:56 +000018#include "GrCoordTransform.h"
egdaniel605dd0f2014-11-12 08:35:25 -080019#include "GrInvariantOutput.h"
joshualitteb2a6762014-12-04 11:35:33 -080020#include "SkGr.h"
bsalomonc21b09e2015-08-28 18:46:56 -070021#include "effects/GrConstColorProcessor.h"
egdaniel64c47282015-11-13 06:54:19 -080022#include "glsl/GrGLSLFragmentProcessor.h"
egdaniel2d721d32015-11-11 13:06:05 -080023#include "glsl/GrGLSLFragmentShaderBuilder.h"
egdaniel018fb622015-10-28 07:26:40 -070024#include "glsl/GrGLSLProgramDataManager.h"
egdaniel7ea439b2015-12-03 09:20:44 -080025#include "glsl/GrGLSLUniformHandler.h"
sugoi@google.come3b4c502013-04-05 13:47:09 +000026#endif
27
28static const int kBlockSize = 256;
29static const int kBlockMask = kBlockSize - 1;
30static const int kPerlinNoise = 4096;
31static const int kRandMaximum = SK_MaxS32; // 2**31 - 1
32
33namespace {
34
35// noiseValue is the color component's value (or color)
36// limitValue is the maximum perlin noise array index value allowed
37// newValue is the current noise dimension (either width or height)
38inline int checkNoise(int noiseValue, int limitValue, int newValue) {
39 // If the noise value would bring us out of bounds of the current noise array while we are
40 // stiching noise tiles together, wrap the noise around the current dimension of the noise to
41 // stay within the array bounds in a continuous fashion (so that tiling lines are not visible)
42 if (noiseValue >= limitValue) {
43 noiseValue -= newValue;
44 }
sugoi@google.come3b4c502013-04-05 13:47:09 +000045 return noiseValue;
46}
47
48inline SkScalar smoothCurve(SkScalar t) {
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +000049 static const SkScalar SK_Scalar3 = 3.0f;
sugoi@google.come3b4c502013-04-05 13:47:09 +000050
51 // returns t * t * (3 - 2 * t)
52 return SkScalarMul(SkScalarSquare(t), SK_Scalar3 - 2 * t);
53}
54
55} // end namespace
56
57struct SkPerlinNoiseShader::StitchData {
58 StitchData()
59 : fWidth(0)
60 , fWrapX(0)
61 , fHeight(0)
62 , fWrapY(0)
63 {}
64
65 bool operator==(const StitchData& other) const {
66 return fWidth == other.fWidth &&
67 fWrapX == other.fWrapX &&
68 fHeight == other.fHeight &&
69 fWrapY == other.fWrapY;
70 }
71
72 int fWidth; // How much to subtract to wrap for stitching.
73 int fWrapX; // Minimum value to wrap.
74 int fHeight;
75 int fWrapY;
76};
77
78struct SkPerlinNoiseShader::PaintingData {
commit-bot@chromium.orgfd5c9a62014-03-06 15:13:53 +000079 PaintingData(const SkISize& tileSize, SkScalar seed,
senorblancoca6a7c22014-06-27 13:35:52 -070080 SkScalar baseFrequencyX, SkScalar baseFrequencyY,
81 const SkMatrix& matrix)
sugoi@google.come3b4c502013-04-05 13:47:09 +000082 {
reed11fa2242015-03-13 06:08:28 -070083 SkVector vec[2] = {
84 { SkScalarInvert(baseFrequencyX), SkScalarInvert(baseFrequencyY) },
85 { SkIntToScalar(tileSize.fWidth), SkIntToScalar(tileSize.fHeight) },
86 };
87 matrix.mapVectors(vec, 2);
88
89 fBaseFrequency.set(SkScalarInvert(vec[0].fX), SkScalarInvert(vec[0].fY));
90 fTileSize.set(SkScalarRoundToInt(vec[1].fX), SkScalarRoundToInt(vec[1].fY));
commit-bot@chromium.orgfd5c9a62014-03-06 15:13:53 +000091 this->init(seed);
92 if (!fTileSize.isEmpty()) {
93 this->stitch();
94 }
95
senorblancof3b50272014-06-16 10:49:58 -070096#if SK_SUPPORT_GPU
commit-bot@chromium.orga3264e52014-05-30 13:26:10 +000097 fPermutationsBitmap.setInfo(SkImageInfo::MakeA8(kBlockSize, 1));
commit-bot@chromium.orgfd5c9a62014-03-06 15:13:53 +000098 fPermutationsBitmap.setPixels(fLatticeSelector);
99
commit-bot@chromium.orga3264e52014-05-30 13:26:10 +0000100 fNoiseBitmap.setInfo(SkImageInfo::MakeN32Premul(kBlockSize, 4));
commit-bot@chromium.orgfd5c9a62014-03-06 15:13:53 +0000101 fNoiseBitmap.setPixels(fNoise[0][0]);
102#endif
sugoi@google.come3b4c502013-04-05 13:47:09 +0000103 }
104
105 int fSeed;
106 uint8_t fLatticeSelector[kBlockSize];
107 uint16_t fNoise[4][kBlockSize][2];
108 SkPoint fGradient[4][kBlockSize];
109 SkISize fTileSize;
110 SkVector fBaseFrequency;
111 StitchData fStitchDataInit;
112
113private:
114
senorblancof3b50272014-06-16 10:49:58 -0700115#if SK_SUPPORT_GPU
commit-bot@chromium.orgfd5c9a62014-03-06 15:13:53 +0000116 SkBitmap fPermutationsBitmap;
117 SkBitmap fNoiseBitmap;
118#endif
sugoi@google.come3b4c502013-04-05 13:47:09 +0000119
120 inline int random() {
121 static const int gRandAmplitude = 16807; // 7**5; primitive root of m
122 static const int gRandQ = 127773; // m / a
123 static const int gRandR = 2836; // m % a
124
125 int result = gRandAmplitude * (fSeed % gRandQ) - gRandR * (fSeed / gRandQ);
126 if (result <= 0)
127 result += kRandMaximum;
128 fSeed = result;
129 return result;
130 }
131
commit-bot@chromium.orgfd5c9a62014-03-06 15:13:53 +0000132 // Only called once. Could be part of the constructor.
sugoi@google.come3b4c502013-04-05 13:47:09 +0000133 void init(SkScalar seed)
134 {
135 static const SkScalar gInvBlockSizef = SkScalarInvert(SkIntToScalar(kBlockSize));
136
senorblanco@chromium.org857e3202014-01-09 17:41:42 +0000137 // According to the SVG spec, we must truncate (not round) the seed value.
138 fSeed = SkScalarTruncToInt(seed);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000139 // The seed value clamp to the range [1, kRandMaximum - 1].
sugoi@google.come3b4c502013-04-05 13:47:09 +0000140 if (fSeed <= 0) {
141 fSeed = -(fSeed % (kRandMaximum - 1)) + 1;
142 }
143 if (fSeed > kRandMaximum - 1) {
144 fSeed = kRandMaximum - 1;
145 }
146 for (int channel = 0; channel < 4; ++channel) {
147 for (int i = 0; i < kBlockSize; ++i) {
148 fLatticeSelector[i] = i;
149 fNoise[channel][i][0] = (random() % (2 * kBlockSize));
150 fNoise[channel][i][1] = (random() % (2 * kBlockSize));
151 }
152 }
153 for (int i = kBlockSize - 1; i > 0; --i) {
154 int k = fLatticeSelector[i];
155 int j = random() % kBlockSize;
156 SkASSERT(j >= 0);
157 SkASSERT(j < kBlockSize);
158 fLatticeSelector[i] = fLatticeSelector[j];
159 fLatticeSelector[j] = k;
160 }
161
162 // Perform the permutations now
163 {
164 // Copy noise data
165 uint16_t noise[4][kBlockSize][2];
166 for (int i = 0; i < kBlockSize; ++i) {
167 for (int channel = 0; channel < 4; ++channel) {
168 for (int j = 0; j < 2; ++j) {
169 noise[channel][i][j] = fNoise[channel][i][j];
170 }
171 }
172 }
173 // Do permutations on noise data
174 for (int i = 0; i < kBlockSize; ++i) {
175 for (int channel = 0; channel < 4; ++channel) {
176 for (int j = 0; j < 2; ++j) {
177 fNoise[channel][i][j] = noise[channel][fLatticeSelector[i]][j];
178 }
179 }
180 }
181 }
182
183 // Half of the largest possible value for 16 bit unsigned int
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +0000184 static const SkScalar gHalfMax16bits = 32767.5f;
sugoi@google.come3b4c502013-04-05 13:47:09 +0000185
186 // Compute gradients from permutated noise data
187 for (int channel = 0; channel < 4; ++channel) {
188 for (int i = 0; i < kBlockSize; ++i) {
189 fGradient[channel][i] = SkPoint::Make(
190 SkScalarMul(SkIntToScalar(fNoise[channel][i][0] - kBlockSize),
191 gInvBlockSizef),
skia.committer@gmail.comcff02432013-04-06 07:01:10 +0000192 SkScalarMul(SkIntToScalar(fNoise[channel][i][1] - kBlockSize),
sugoi@google.come3b4c502013-04-05 13:47:09 +0000193 gInvBlockSizef));
194 fGradient[channel][i].normalize();
195 // Put the normalized gradient back into the noise data
196 fNoise[channel][i][0] = SkScalarRoundToInt(SkScalarMul(
sugoi@google.comd537af52013-06-10 13:59:25 +0000197 fGradient[channel][i].fX + SK_Scalar1, gHalfMax16bits));
sugoi@google.come3b4c502013-04-05 13:47:09 +0000198 fNoise[channel][i][1] = SkScalarRoundToInt(SkScalarMul(
sugoi@google.comd537af52013-06-10 13:59:25 +0000199 fGradient[channel][i].fY + SK_Scalar1, gHalfMax16bits));
sugoi@google.come3b4c502013-04-05 13:47:09 +0000200 }
201 }
sugoi@google.come3b4c502013-04-05 13:47:09 +0000202 }
203
commit-bot@chromium.orgfd5c9a62014-03-06 15:13:53 +0000204 // Only called once. Could be part of the constructor.
sugoi@google.come3b4c502013-04-05 13:47:09 +0000205 void stitch() {
206 SkScalar tileWidth = SkIntToScalar(fTileSize.width());
207 SkScalar tileHeight = SkIntToScalar(fTileSize.height());
208 SkASSERT(tileWidth > 0 && tileHeight > 0);
209 // When stitching tiled turbulence, the frequencies must be adjusted
210 // so that the tile borders will be continuous.
211 if (fBaseFrequency.fX) {
reed@google.com8015cdd2013-12-18 15:49:32 +0000212 SkScalar lowFrequencx =
213 SkScalarFloorToScalar(tileWidth * fBaseFrequency.fX) / tileWidth;
214 SkScalar highFrequencx =
215 SkScalarCeilToScalar(tileWidth * fBaseFrequency.fX) / tileWidth;
sugoi@google.come3b4c502013-04-05 13:47:09 +0000216 // BaseFrequency should be non-negative according to the standard.
reed80ea19c2015-05-12 10:37:34 -0700217 if (fBaseFrequency.fX / lowFrequencx < highFrequencx / fBaseFrequency.fX) {
sugoi@google.come3b4c502013-04-05 13:47:09 +0000218 fBaseFrequency.fX = lowFrequencx;
219 } else {
220 fBaseFrequency.fX = highFrequencx;
221 }
222 }
223 if (fBaseFrequency.fY) {
reed@google.com8015cdd2013-12-18 15:49:32 +0000224 SkScalar lowFrequency =
225 SkScalarFloorToScalar(tileHeight * fBaseFrequency.fY) / tileHeight;
226 SkScalar highFrequency =
227 SkScalarCeilToScalar(tileHeight * fBaseFrequency.fY) / tileHeight;
reed80ea19c2015-05-12 10:37:34 -0700228 if (fBaseFrequency.fY / lowFrequency < highFrequency / fBaseFrequency.fY) {
sugoi@google.come3b4c502013-04-05 13:47:09 +0000229 fBaseFrequency.fY = lowFrequency;
230 } else {
231 fBaseFrequency.fY = highFrequency;
232 }
233 }
234 // Set up TurbulenceInitial stitch values.
235 fStitchDataInit.fWidth =
reed@google.com8015cdd2013-12-18 15:49:32 +0000236 SkScalarRoundToInt(tileWidth * fBaseFrequency.fX);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000237 fStitchDataInit.fWrapX = kPerlinNoise + fStitchDataInit.fWidth;
238 fStitchDataInit.fHeight =
reed@google.com8015cdd2013-12-18 15:49:32 +0000239 SkScalarRoundToInt(tileHeight * fBaseFrequency.fY);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000240 fStitchDataInit.fWrapY = kPerlinNoise + fStitchDataInit.fHeight;
241 }
242
commit-bot@chromium.orgfd5c9a62014-03-06 15:13:53 +0000243public:
sugoi@google.come3b4c502013-04-05 13:47:09 +0000244
senorblancof3b50272014-06-16 10:49:58 -0700245#if SK_SUPPORT_GPU
commit-bot@chromium.orgfd5c9a62014-03-06 15:13:53 +0000246 const SkBitmap& getPermutationsBitmap() const { return fPermutationsBitmap; }
247
248 const SkBitmap& getNoiseBitmap() const { return fNoiseBitmap; }
249#endif
sugoi@google.come3b4c502013-04-05 13:47:09 +0000250};
251
252SkShader* SkPerlinNoiseShader::CreateFractalNoise(SkScalar baseFrequencyX, SkScalar baseFrequencyY,
253 int numOctaves, SkScalar seed,
254 const SkISize* tileSize) {
halcanary385fe4d2015-08-26 13:07:48 -0700255 return new SkPerlinNoiseShader(kFractalNoise_Type, baseFrequencyX, baseFrequencyY, numOctaves,
256 seed, tileSize);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000257}
258
commit-bot@chromium.org9fbbcca2014-04-01 16:09:37 +0000259SkShader* SkPerlinNoiseShader::CreateTurbulence(SkScalar baseFrequencyX, SkScalar baseFrequencyY,
sugoi@google.come3b4c502013-04-05 13:47:09 +0000260 int numOctaves, SkScalar seed,
261 const SkISize* tileSize) {
halcanary385fe4d2015-08-26 13:07:48 -0700262 return new SkPerlinNoiseShader(kTurbulence_Type, baseFrequencyX, baseFrequencyY, numOctaves,
263 seed, tileSize);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000264}
265
266SkPerlinNoiseShader::SkPerlinNoiseShader(SkPerlinNoiseShader::Type type,
267 SkScalar baseFrequencyX,
268 SkScalar baseFrequencyY,
269 int numOctaves,
270 SkScalar seed,
271 const SkISize* tileSize)
272 : fType(type)
273 , fBaseFrequencyX(baseFrequencyX)
274 , fBaseFrequencyY(baseFrequencyY)
commit-bot@chromium.orgce33d602013-11-25 21:46:31 +0000275 , fNumOctaves(numOctaves > 255 ? 255 : numOctaves/*[0,255] octaves allowed*/)
sugoi@google.come3b4c502013-04-05 13:47:09 +0000276 , fSeed(seed)
halcanary96fcdcc2015-08-27 07:41:13 -0700277 , fTileSize(nullptr == tileSize ? SkISize::Make(0, 0) : *tileSize)
commit-bot@chromium.orgfd5c9a62014-03-06 15:13:53 +0000278 , fStitchTiles(!fTileSize.isEmpty())
sugoi@google.come3b4c502013-04-05 13:47:09 +0000279{
280 SkASSERT(numOctaves >= 0 && numOctaves < 256);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000281}
282
sugoi@google.come3b4c502013-04-05 13:47:09 +0000283SkPerlinNoiseShader::~SkPerlinNoiseShader() {
sugoi@google.come3b4c502013-04-05 13:47:09 +0000284}
285
reed9fa60da2014-08-21 07:59:51 -0700286SkFlattenable* SkPerlinNoiseShader::CreateProc(SkReadBuffer& buffer) {
287 Type type = (Type)buffer.readInt();
288 SkScalar freqX = buffer.readScalar();
289 SkScalar freqY = buffer.readScalar();
290 int octaves = buffer.readInt();
291 SkScalar seed = buffer.readScalar();
292 SkISize tileSize;
293 tileSize.fWidth = buffer.readInt();
294 tileSize.fHeight = buffer.readInt();
295
296 switch (type) {
297 case kFractalNoise_Type:
298 return SkPerlinNoiseShader::CreateFractalNoise(freqX, freqY, octaves, seed, &tileSize);
299 case kTurbulence_Type:
300 return SkPerlinNoiseShader::CreateTubulence(freqX, freqY, octaves, seed, &tileSize);
301 default:
halcanary96fcdcc2015-08-27 07:41:13 -0700302 return nullptr;
reed9fa60da2014-08-21 07:59:51 -0700303 }
304}
305
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +0000306void SkPerlinNoiseShader::flatten(SkWriteBuffer& buffer) const {
sugoi@google.come3b4c502013-04-05 13:47:09 +0000307 buffer.writeInt((int) fType);
308 buffer.writeScalar(fBaseFrequencyX);
309 buffer.writeScalar(fBaseFrequencyY);
310 buffer.writeInt(fNumOctaves);
311 buffer.writeScalar(fSeed);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000312 buffer.writeInt(fTileSize.fWidth);
313 buffer.writeInt(fTileSize.fHeight);
314}
315
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000316SkScalar SkPerlinNoiseShader::PerlinNoiseShaderContext::noise2D(
senorblancoca6a7c22014-06-27 13:35:52 -0700317 int channel, const StitchData& stitchData, const SkPoint& noiseVector) const {
sugoi@google.come3b4c502013-04-05 13:47:09 +0000318 struct Noise {
319 int noisePositionIntegerValue;
senorblancoce6a3542014-06-12 11:24:19 -0700320 int nextNoisePositionIntegerValue;
sugoi@google.come3b4c502013-04-05 13:47:09 +0000321 SkScalar noisePositionFractionValue;
322 Noise(SkScalar component)
323 {
324 SkScalar position = component + kPerlinNoise;
325 noisePositionIntegerValue = SkScalarFloorToInt(position);
326 noisePositionFractionValue = position - SkIntToScalar(noisePositionIntegerValue);
senorblancoce6a3542014-06-12 11:24:19 -0700327 nextNoisePositionIntegerValue = noisePositionIntegerValue + 1;
sugoi@google.come3b4c502013-04-05 13:47:09 +0000328 }
329 };
330 Noise noiseX(noiseVector.x());
331 Noise noiseY(noiseVector.y());
332 SkScalar u, v;
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000333 const SkPerlinNoiseShader& perlinNoiseShader = static_cast<const SkPerlinNoiseShader&>(fShader);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000334 // If stitching, adjust lattice points accordingly.
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000335 if (perlinNoiseShader.fStitchTiles) {
sugoi@google.come3b4c502013-04-05 13:47:09 +0000336 noiseX.noisePositionIntegerValue =
337 checkNoise(noiseX.noisePositionIntegerValue, stitchData.fWrapX, stitchData.fWidth);
338 noiseY.noisePositionIntegerValue =
339 checkNoise(noiseY.noisePositionIntegerValue, stitchData.fWrapY, stitchData.fHeight);
senorblancoce6a3542014-06-12 11:24:19 -0700340 noiseX.nextNoisePositionIntegerValue =
341 checkNoise(noiseX.nextNoisePositionIntegerValue, stitchData.fWrapX, stitchData.fWidth);
342 noiseY.nextNoisePositionIntegerValue =
343 checkNoise(noiseY.nextNoisePositionIntegerValue, stitchData.fWrapY, stitchData.fHeight);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000344 }
345 noiseX.noisePositionIntegerValue &= kBlockMask;
346 noiseY.noisePositionIntegerValue &= kBlockMask;
senorblancoce6a3542014-06-12 11:24:19 -0700347 noiseX.nextNoisePositionIntegerValue &= kBlockMask;
348 noiseY.nextNoisePositionIntegerValue &= kBlockMask;
349 int i =
senorblancoca6a7c22014-06-27 13:35:52 -0700350 fPaintingData->fLatticeSelector[noiseX.noisePositionIntegerValue];
senorblancoce6a3542014-06-12 11:24:19 -0700351 int j =
senorblancoca6a7c22014-06-27 13:35:52 -0700352 fPaintingData->fLatticeSelector[noiseX.nextNoisePositionIntegerValue];
senorblancoce6a3542014-06-12 11:24:19 -0700353 int b00 = (i + noiseY.noisePositionIntegerValue) & kBlockMask;
354 int b10 = (j + noiseY.noisePositionIntegerValue) & kBlockMask;
355 int b01 = (i + noiseY.nextNoisePositionIntegerValue) & kBlockMask;
356 int b11 = (j + noiseY.nextNoisePositionIntegerValue) & kBlockMask;
sugoi@google.come3b4c502013-04-05 13:47:09 +0000357 SkScalar sx = smoothCurve(noiseX.noisePositionFractionValue);
358 SkScalar sy = smoothCurve(noiseY.noisePositionFractionValue);
359 // This is taken 1:1 from SVG spec: http://www.w3.org/TR/SVG11/filters.html#feTurbulenceElement
360 SkPoint fractionValue = SkPoint::Make(noiseX.noisePositionFractionValue,
361 noiseY.noisePositionFractionValue); // Offset (0,0)
senorblancoca6a7c22014-06-27 13:35:52 -0700362 u = fPaintingData->fGradient[channel][b00].dot(fractionValue);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000363 fractionValue.fX -= SK_Scalar1; // Offset (-1,0)
senorblancoca6a7c22014-06-27 13:35:52 -0700364 v = fPaintingData->fGradient[channel][b10].dot(fractionValue);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000365 SkScalar a = SkScalarInterp(u, v, sx);
366 fractionValue.fY -= SK_Scalar1; // Offset (-1,-1)
senorblancoca6a7c22014-06-27 13:35:52 -0700367 v = fPaintingData->fGradient[channel][b11].dot(fractionValue);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000368 fractionValue.fX = noiseX.noisePositionFractionValue; // Offset (0,-1)
senorblancoca6a7c22014-06-27 13:35:52 -0700369 u = fPaintingData->fGradient[channel][b01].dot(fractionValue);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000370 SkScalar b = SkScalarInterp(u, v, sx);
371 return SkScalarInterp(a, b, sy);
372}
373
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000374SkScalar SkPerlinNoiseShader::PerlinNoiseShaderContext::calculateTurbulenceValueForPoint(
senorblancoca6a7c22014-06-27 13:35:52 -0700375 int channel, StitchData& stitchData, const SkPoint& point) const {
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000376 const SkPerlinNoiseShader& perlinNoiseShader = static_cast<const SkPerlinNoiseShader&>(fShader);
377 if (perlinNoiseShader.fStitchTiles) {
sugoi@google.come3b4c502013-04-05 13:47:09 +0000378 // Set up TurbulenceInitial stitch values.
senorblancoca6a7c22014-06-27 13:35:52 -0700379 stitchData = fPaintingData->fStitchDataInit;
sugoi@google.come3b4c502013-04-05 13:47:09 +0000380 }
381 SkScalar turbulenceFunctionResult = 0;
senorblancoca6a7c22014-06-27 13:35:52 -0700382 SkPoint noiseVector(SkPoint::Make(SkScalarMul(point.x(), fPaintingData->fBaseFrequency.fX),
383 SkScalarMul(point.y(), fPaintingData->fBaseFrequency.fY)));
sugoi@google.come3b4c502013-04-05 13:47:09 +0000384 SkScalar ratio = SK_Scalar1;
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000385 for (int octave = 0; octave < perlinNoiseShader.fNumOctaves; ++octave) {
senorblancoca6a7c22014-06-27 13:35:52 -0700386 SkScalar noise = noise2D(channel, stitchData, noiseVector);
reed80ea19c2015-05-12 10:37:34 -0700387 SkScalar numer = (perlinNoiseShader.fType == kFractalNoise_Type) ?
388 noise : SkScalarAbs(noise);
389 turbulenceFunctionResult += numer / ratio;
sugoi@google.come3b4c502013-04-05 13:47:09 +0000390 noiseVector.fX *= 2;
391 noiseVector.fY *= 2;
392 ratio *= 2;
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000393 if (perlinNoiseShader.fStitchTiles) {
sugoi@google.come3b4c502013-04-05 13:47:09 +0000394 // Update stitch values
395 stitchData.fWidth *= 2;
396 stitchData.fWrapX = stitchData.fWidth + kPerlinNoise;
397 stitchData.fHeight *= 2;
398 stitchData.fWrapY = stitchData.fHeight + kPerlinNoise;
399 }
400 }
401
402 // The value of turbulenceFunctionResult comes from ((turbulenceFunctionResult) + 1) / 2
403 // by fractalNoise and (turbulenceFunctionResult) by turbulence.
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000404 if (perlinNoiseShader.fType == kFractalNoise_Type) {
sugoi@google.come3b4c502013-04-05 13:47:09 +0000405 turbulenceFunctionResult =
406 SkScalarMul(turbulenceFunctionResult, SK_ScalarHalf) + SK_ScalarHalf;
407 }
408
409 if (channel == 3) { // Scale alpha by paint value
reed80ea19c2015-05-12 10:37:34 -0700410 turbulenceFunctionResult *= SkIntToScalar(getPaintAlpha()) / 255;
sugoi@google.come3b4c502013-04-05 13:47:09 +0000411 }
412
413 // Clamp result
414 return SkScalarPin(turbulenceFunctionResult, 0, SK_Scalar1);
415}
416
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000417SkPMColor SkPerlinNoiseShader::PerlinNoiseShaderContext::shade(
418 const SkPoint& point, StitchData& stitchData) const {
sugoi@google.come3b4c502013-04-05 13:47:09 +0000419 SkPoint newPoint;
senorblanco@chromium.orga8d95f82014-04-04 14:46:10 +0000420 fMatrix.mapPoints(&newPoint, &point, 1);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000421 newPoint.fX = SkScalarRoundToScalar(newPoint.fX);
422 newPoint.fY = SkScalarRoundToScalar(newPoint.fY);
423
424 U8CPU rgba[4];
425 for (int channel = 3; channel >= 0; --channel) {
426 rgba[channel] = SkScalarFloorToInt(255 *
senorblancoca6a7c22014-06-27 13:35:52 -0700427 calculateTurbulenceValueForPoint(channel, stitchData, newPoint));
sugoi@google.come3b4c502013-04-05 13:47:09 +0000428 }
429 return SkPreMultiplyARGB(rgba[3], rgba[0], rgba[1], rgba[2]);
430}
431
commit-bot@chromium.orgce56d962014-05-05 18:39:18 +0000432SkShader::Context* SkPerlinNoiseShader::onCreateContext(const ContextRec& rec,
433 void* storage) const {
halcanary385fe4d2015-08-26 13:07:48 -0700434 return new (storage) PerlinNoiseShaderContext(*this, rec);
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000435}
436
437size_t SkPerlinNoiseShader::contextSize() const {
438 return sizeof(PerlinNoiseShaderContext);
439}
440
441SkPerlinNoiseShader::PerlinNoiseShaderContext::PerlinNoiseShaderContext(
commit-bot@chromium.orge901b6d2014-05-01 19:31:31 +0000442 const SkPerlinNoiseShader& shader, const ContextRec& rec)
443 : INHERITED(shader, rec)
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000444{
commit-bot@chromium.orge901b6d2014-05-01 19:31:31 +0000445 SkMatrix newMatrix = *rec.fMatrix;
reed@google.comb67b8e62014-05-12 18:12:24 +0000446 newMatrix.preConcat(shader.getLocalMatrix());
447 if (rec.fLocalMatrix) {
448 newMatrix.preConcat(*rec.fLocalMatrix);
449 }
senorblanco@chromium.orga8d95f82014-04-04 14:46:10 +0000450 // This (1,1) translation is due to WebKit's 1 based coordinates for the noise
451 // (as opposed to 0 based, usually). The same adjustment is in the setData() function.
senorblancoca6a7c22014-06-27 13:35:52 -0700452 fMatrix.setTranslate(-newMatrix.getTranslateX() + SK_Scalar1, -newMatrix.getTranslateY() + SK_Scalar1);
halcanary385fe4d2015-08-26 13:07:48 -0700453 fPaintingData = new PaintingData(shader.fTileSize, shader.fSeed, shader.fBaseFrequencyX,
454 shader.fBaseFrequencyY, newMatrix);
senorblancoca6a7c22014-06-27 13:35:52 -0700455}
456
halcanary385fe4d2015-08-26 13:07:48 -0700457SkPerlinNoiseShader::PerlinNoiseShaderContext::~PerlinNoiseShaderContext() { delete fPaintingData; }
sugoi@google.come3b4c502013-04-05 13:47:09 +0000458
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000459void SkPerlinNoiseShader::PerlinNoiseShaderContext::shadeSpan(
460 int x, int y, SkPMColor result[], int count) {
sugoi@google.come3b4c502013-04-05 13:47:09 +0000461 SkPoint point = SkPoint::Make(SkIntToScalar(x), SkIntToScalar(y));
462 StitchData stitchData;
463 for (int i = 0; i < count; ++i) {
464 result[i] = shade(point, stitchData);
465 point.fX += SK_Scalar1;
466 }
467}
468
sugoi@google.come3b4c502013-04-05 13:47:09 +0000469/////////////////////////////////////////////////////////////////////
470
commit-bot@chromium.org344cf452013-06-17 14:19:01 +0000471#if SK_SUPPORT_GPU
sugoi@google.come3b4c502013-04-05 13:47:09 +0000472
egdaniel64c47282015-11-13 06:54:19 -0800473class GrGLPerlinNoise : public GrGLSLFragmentProcessor {
sugoi@google.com4775cba2013-04-17 13:46:56 +0000474public:
joshualitteb2a6762014-12-04 11:35:33 -0800475 GrGLPerlinNoise(const GrProcessor&);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000476
robertphillips9cdb9922016-02-03 12:25:40 -0800477 void emitCode(EmitArgs&) override;
sugoi@google.come3b4c502013-04-05 13:47:09 +0000478
jvanverthcfc18862015-04-28 08:48:20 -0700479 static inline void GenKey(const GrProcessor&, const GrGLSLCaps&, GrProcessorKeyBuilder* b);
sugoi@google.com4775cba2013-04-17 13:46:56 +0000480
wangyixb1daa862015-08-18 11:29:31 -0700481protected:
egdaniel018fb622015-10-28 07:26:40 -0700482 void onSetData(const GrGLSLProgramDataManager&, const GrProcessor&) override;
wangyixb1daa862015-08-18 11:29:31 -0700483
sugoi@google.com4775cba2013-04-17 13:46:56 +0000484private:
sugoi@google.com4775cba2013-04-17 13:46:56 +0000485
egdaniel018fb622015-10-28 07:26:40 -0700486 GrGLSLProgramDataManager::UniformHandle fStitchDataUni;
487 SkPerlinNoiseShader::Type fType;
488 bool fStitchTiles;
489 int fNumOctaves;
490 GrGLSLProgramDataManager::UniformHandle fBaseFrequencyUni;
senorblancof3b50272014-06-16 10:49:58 -0700491
egdaniel64c47282015-11-13 06:54:19 -0800492 typedef GrGLSLFragmentProcessor INHERITED;
sugoi@google.come3b4c502013-04-05 13:47:09 +0000493};
494
495/////////////////////////////////////////////////////////////////////
496
joshualittb0a8a372014-09-23 09:50:21 -0700497class GrPerlinNoiseEffect : public GrFragmentProcessor {
sugoi@google.come3b4c502013-04-05 13:47:09 +0000498public:
bsalomon4a339522015-10-06 08:40:50 -0700499 static GrFragmentProcessor* Create(SkPerlinNoiseShader::Type type,
joshualittb0a8a372014-09-23 09:50:21 -0700500 int numOctaves, bool stitchTiles,
501 SkPerlinNoiseShader::PaintingData* paintingData,
502 GrTexture* permutationsTexture, GrTexture* noiseTexture,
bsalomonc21b09e2015-08-28 18:46:56 -0700503 const SkMatrix& matrix) {
bsalomon4a339522015-10-06 08:40:50 -0700504 return new GrPerlinNoiseEffect(type, numOctaves, stitchTiles, paintingData,
bsalomonc21b09e2015-08-28 18:46:56 -0700505 permutationsTexture, noiseTexture, matrix);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000506 }
507
halcanary385fe4d2015-08-26 13:07:48 -0700508 virtual ~GrPerlinNoiseEffect() { delete fPaintingData; }
sugoi@google.come3b4c502013-04-05 13:47:09 +0000509
mtklein36352bf2015-03-25 18:17:31 -0700510 const char* name() const override { return "PerlinNoise"; }
joshualitteb2a6762014-12-04 11:35:33 -0800511
senorblancoca6a7c22014-06-27 13:35:52 -0700512 const SkPerlinNoiseShader::StitchData& stitchData() const { return fPaintingData->fStitchDataInit; }
sugoi@google.come3b4c502013-04-05 13:47:09 +0000513
senorblancof3b50272014-06-16 10:49:58 -0700514 SkPerlinNoiseShader::Type type() const { return fType; }
515 bool stitchTiles() const { return fStitchTiles; }
senorblancoca6a7c22014-06-27 13:35:52 -0700516 const SkVector& baseFrequency() const { return fPaintingData->fBaseFrequency; }
senorblancof3b50272014-06-16 10:49:58 -0700517 int numOctaves() const { return fNumOctaves; }
518 const SkMatrix& matrix() const { return fCoordTransform.getMatrix(); }
senorblancof3b50272014-06-16 10:49:58 -0700519
sugoi@google.come3b4c502013-04-05 13:47:09 +0000520private:
egdaniel57d3b032015-11-13 11:57:27 -0800521 GrGLSLFragmentProcessor* onCreateGLSLInstance() const override {
halcanary385fe4d2015-08-26 13:07:48 -0700522 return new GrGLPerlinNoise(*this);
wangyixb1daa862015-08-18 11:29:31 -0700523 }
524
egdaniel57d3b032015-11-13 11:57:27 -0800525 virtual void onGetGLSLProcessorKey(const GrGLSLCaps& caps,
526 GrProcessorKeyBuilder* b) const override {
wangyix4b3050b2015-08-04 07:59:37 -0700527 GrGLPerlinNoise::GenKey(*this, caps, b);
528 }
529
mtklein36352bf2015-03-25 18:17:31 -0700530 bool onIsEqual(const GrFragmentProcessor& sBase) const override {
joshualitt49586be2014-09-16 08:21:41 -0700531 const GrPerlinNoiseEffect& s = sBase.cast<GrPerlinNoiseEffect>();
senorblancof3b50272014-06-16 10:49:58 -0700532 return fType == s.fType &&
senorblancoca6a7c22014-06-27 13:35:52 -0700533 fPaintingData->fBaseFrequency == s.fPaintingData->fBaseFrequency &&
senorblancof3b50272014-06-16 10:49:58 -0700534 fNumOctaves == s.fNumOctaves &&
535 fStitchTiles == s.fStitchTiles &&
senorblancoca6a7c22014-06-27 13:35:52 -0700536 fPaintingData->fStitchDataInit == s.fPaintingData->fStitchDataInit;
sugoi@google.come3b4c502013-04-05 13:47:09 +0000537 }
538
mtklein36352bf2015-03-25 18:17:31 -0700539 void onComputeInvariantOutput(GrInvariantOutput* inout) const override {
egdaniel605dd0f2014-11-12 08:35:25 -0800540 inout->setToUnknown(GrInvariantOutput::kWillNot_ReadInput);
egdaniel1a8ecdf2014-10-03 06:24:12 -0700541 }
542
bsalomon4a339522015-10-06 08:40:50 -0700543 GrPerlinNoiseEffect(SkPerlinNoiseShader::Type type,
sugoi@google.come3b4c502013-04-05 13:47:09 +0000544 int numOctaves, bool stitchTiles,
senorblancoca6a7c22014-06-27 13:35:52 -0700545 SkPerlinNoiseShader::PaintingData* paintingData,
sugoi@google.come3b4c502013-04-05 13:47:09 +0000546 GrTexture* permutationsTexture, GrTexture* noiseTexture,
bsalomonc21b09e2015-08-28 18:46:56 -0700547 const SkMatrix& matrix)
senorblancof3b50272014-06-16 10:49:58 -0700548 : fType(type)
senorblancof3b50272014-06-16 10:49:58 -0700549 , fNumOctaves(numOctaves)
550 , fStitchTiles(stitchTiles)
sugoi@google.com4775cba2013-04-17 13:46:56 +0000551 , fPermutationsAccess(permutationsTexture)
sugoi@google.come3b4c502013-04-05 13:47:09 +0000552 , fNoiseAccess(noiseTexture)
senorblancoca6a7c22014-06-27 13:35:52 -0700553 , fPaintingData(paintingData) {
joshualitteb2a6762014-12-04 11:35:33 -0800554 this->initClassID<GrPerlinNoiseEffect>();
sugoi@google.come3b4c502013-04-05 13:47:09 +0000555 this->addTextureAccess(&fPermutationsAccess);
556 this->addTextureAccess(&fNoiseAccess);
senorblancoca6a7c22014-06-27 13:35:52 -0700557 fCoordTransform.reset(kLocal_GrCoordSet, matrix);
senorblancof3b50272014-06-16 10:49:58 -0700558 this->addCoordTransform(&fCoordTransform);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000559 }
560
joshualittb0a8a372014-09-23 09:50:21 -0700561 GR_DECLARE_FRAGMENT_PROCESSOR_TEST;
sugoi@google.come3b4c502013-04-05 13:47:09 +0000562
senorblancof3b50272014-06-16 10:49:58 -0700563 SkPerlinNoiseShader::Type fType;
564 GrCoordTransform fCoordTransform;
senorblancof3b50272014-06-16 10:49:58 -0700565 int fNumOctaves;
566 bool fStitchTiles;
sugoi@google.come3b4c502013-04-05 13:47:09 +0000567 GrTextureAccess fPermutationsAccess;
568 GrTextureAccess fNoiseAccess;
senorblancoca6a7c22014-06-27 13:35:52 -0700569 SkPerlinNoiseShader::PaintingData *fPaintingData;
sugoi@google.come3b4c502013-04-05 13:47:09 +0000570
sugoi@google.com4775cba2013-04-17 13:46:56 +0000571private:
joshualittb0a8a372014-09-23 09:50:21 -0700572 typedef GrFragmentProcessor INHERITED;
sugoi@google.come3b4c502013-04-05 13:47:09 +0000573};
574
575/////////////////////////////////////////////////////////////////////
joshualittb0a8a372014-09-23 09:50:21 -0700576GR_DEFINE_FRAGMENT_PROCESSOR_TEST(GrPerlinNoiseEffect);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000577
bsalomonc21b09e2015-08-28 18:46:56 -0700578const GrFragmentProcessor* GrPerlinNoiseEffect::TestCreate(GrProcessorTestData* d) {
joshualitt0067ff52015-07-08 14:26:19 -0700579 int numOctaves = d->fRandom->nextRangeU(2, 10);
580 bool stitchTiles = d->fRandom->nextBool();
581 SkScalar seed = SkIntToScalar(d->fRandom->nextU());
582 SkISize tileSize = SkISize::Make(d->fRandom->nextRangeU(4, 4096),
583 d->fRandom->nextRangeU(4, 4096));
584 SkScalar baseFrequencyX = d->fRandom->nextRangeScalar(0.01f,
585 0.99f);
586 SkScalar baseFrequencyY = d->fRandom->nextRangeScalar(0.01f,
587 0.99f);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000588
bsalomon6d2a2032015-08-29 06:27:29 -0700589 SkAutoTUnref<SkShader> shader(d->fRandom->nextBool() ?
sugoi@google.come3b4c502013-04-05 13:47:09 +0000590 SkPerlinNoiseShader::CreateFractalNoise(baseFrequencyX, baseFrequencyY, numOctaves, seed,
halcanary96fcdcc2015-08-27 07:41:13 -0700591 stitchTiles ? &tileSize : nullptr) :
commit-bot@chromium.org9fbbcca2014-04-01 16:09:37 +0000592 SkPerlinNoiseShader::CreateTurbulence(baseFrequencyX, baseFrequencyY, numOctaves, seed,
bsalomon6d2a2032015-08-29 06:27:29 -0700593 stitchTiles ? &tileSize : nullptr));
sugoi@google.come3b4c502013-04-05 13:47:09 +0000594
bsalomonc21b09e2015-08-28 18:46:56 -0700595 return shader->asFragmentProcessor(d->fContext,
596 GrTest::TestMatrix(d->fRandom), nullptr,
bsalomon4a339522015-10-06 08:40:50 -0700597 kNone_SkFilterQuality);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000598}
sugoi@google.com4775cba2013-04-17 13:46:56 +0000599
joshualitteb2a6762014-12-04 11:35:33 -0800600GrGLPerlinNoise::GrGLPerlinNoise(const GrProcessor& processor)
601 : fType(processor.cast<GrPerlinNoiseEffect>().type())
joshualittb0a8a372014-09-23 09:50:21 -0700602 , fStitchTiles(processor.cast<GrPerlinNoiseEffect>().stitchTiles())
603 , fNumOctaves(processor.cast<GrPerlinNoiseEffect>().numOctaves()) {
sugoi@google.com4775cba2013-04-17 13:46:56 +0000604}
605
wangyix7c157a92015-07-22 15:08:53 -0700606void GrGLPerlinNoise::emitCode(EmitArgs& args) {
egdaniel4ca2e602015-11-18 08:01:26 -0800607 GrGLSLFragmentBuilder* fragBuilder = args.fFragBuilder;
egdaniel7ea439b2015-12-03 09:20:44 -0800608 GrGLSLUniformHandler* uniformHandler = args.fUniformHandler;
egdaniel4ca2e602015-11-18 08:01:26 -0800609 SkString vCoords = fragBuilder->ensureFSCoords2D(args.fCoords, 0);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000610
egdaniel7ea439b2015-12-03 09:20:44 -0800611 fBaseFrequencyUni = uniformHandler->addUniform(GrGLSLUniformHandler::kFragment_Visibility,
612 kVec2f_GrSLType, kDefault_GrSLPrecision,
613 "baseFrequency");
614 const char* baseFrequencyUni = uniformHandler->getUniformCStr(fBaseFrequencyUni);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000615
halcanary96fcdcc2015-08-27 07:41:13 -0700616 const char* stitchDataUni = nullptr;
sugoi@google.come3b4c502013-04-05 13:47:09 +0000617 if (fStitchTiles) {
egdaniel7ea439b2015-12-03 09:20:44 -0800618 fStitchDataUni = uniformHandler->addUniform(GrGLSLUniformHandler::kFragment_Visibility,
619 kVec2f_GrSLType, kDefault_GrSLPrecision,
620 "stitchData");
621 stitchDataUni = uniformHandler->getUniformCStr(fStitchDataUni);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000622 }
623
sugoi@google.comd537af52013-06-10 13:59:25 +0000624 // There are 4 lines, so the center of each line is 1/8, 3/8, 5/8 and 7/8
625 const char* chanCoordR = "0.125";
626 const char* chanCoordG = "0.375";
627 const char* chanCoordB = "0.625";
628 const char* chanCoordA = "0.875";
629 const char* chanCoord = "chanCoord";
sugoi@google.come3b4c502013-04-05 13:47:09 +0000630 const char* stitchData = "stitchData";
631 const char* ratio = "ratio";
sugoi@google.come3b4c502013-04-05 13:47:09 +0000632 const char* noiseVec = "noiseVec";
sugoi@google.come3b4c502013-04-05 13:47:09 +0000633 const char* noiseSmooth = "noiseSmooth";
senorblancoce6a3542014-06-12 11:24:19 -0700634 const char* floorVal = "floorVal";
sugoi@google.come3b4c502013-04-05 13:47:09 +0000635 const char* fractVal = "fractVal";
636 const char* uv = "uv";
637 const char* ab = "ab";
638 const char* latticeIdx = "latticeIdx";
senorblancoce6a3542014-06-12 11:24:19 -0700639 const char* bcoords = "bcoords";
sugoi@google.come3b4c502013-04-05 13:47:09 +0000640 const char* lattice = "lattice";
sugoi@google.come3b4c502013-04-05 13:47:09 +0000641 const char* inc8bit = "0.00390625"; // 1.0 / 256.0
642 // This is the math to convert the two 16bit integer packed into rgba 8 bit input into a
643 // [-1,1] vector and perform a dot product between that vector and the provided vector.
644 const char* dotLattice = "dot(((%s.ga + %s.rb * vec2(%s)) * vec2(2.0) - vec2(1.0)), %s);";
645
sugoi@google.comd537af52013-06-10 13:59:25 +0000646 // Add noise function
egdaniel0d3f0612015-10-21 10:45:48 -0700647 static const GrGLSLShaderVar gPerlinNoiseArgs[] = {
648 GrGLSLShaderVar(chanCoord, kFloat_GrSLType),
649 GrGLSLShaderVar(noiseVec, kVec2f_GrSLType)
sugoi@google.comd537af52013-06-10 13:59:25 +0000650 };
sugoi@google.come3b4c502013-04-05 13:47:09 +0000651
egdaniel0d3f0612015-10-21 10:45:48 -0700652 static const GrGLSLShaderVar gPerlinNoiseStitchArgs[] = {
653 GrGLSLShaderVar(chanCoord, kFloat_GrSLType),
654 GrGLSLShaderVar(noiseVec, kVec2f_GrSLType),
655 GrGLSLShaderVar(stitchData, kVec2f_GrSLType)
sugoi@google.comd537af52013-06-10 13:59:25 +0000656 };
sugoi@google.come3b4c502013-04-05 13:47:09 +0000657
sugoi@google.comd537af52013-06-10 13:59:25 +0000658 SkString noiseCode;
sugoi@google.come3b4c502013-04-05 13:47:09 +0000659
senorblancoce6a3542014-06-12 11:24:19 -0700660 noiseCode.appendf("\tvec4 %s;\n", floorVal);
661 noiseCode.appendf("\t%s.xy = floor(%s);\n", floorVal, noiseVec);
662 noiseCode.appendf("\t%s.zw = %s.xy + vec2(1.0);\n", floorVal, floorVal);
663 noiseCode.appendf("\tvec2 %s = fract(%s);\n", fractVal, noiseVec);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000664
665 // smooth curve : t * t * (3 - 2 * t)
senorblancoce6a3542014-06-12 11:24:19 -0700666 noiseCode.appendf("\n\tvec2 %s = %s * %s * (vec2(3.0) - vec2(2.0) * %s);",
667 noiseSmooth, fractVal, fractVal, fractVal);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000668
669 // Adjust frequencies if we're stitching tiles
670 if (fStitchTiles) {
commit-bot@chromium.org98393202013-07-04 18:13:05 +0000671 noiseCode.appendf("\n\tif(%s.x >= %s.x) { %s.x -= %s.x; }",
egdaniel4ca2e602015-11-18 08:01:26 -0800672 floorVal, stitchData, floorVal, stitchData);
commit-bot@chromium.org98393202013-07-04 18:13:05 +0000673 noiseCode.appendf("\n\tif(%s.y >= %s.y) { %s.y -= %s.y; }",
egdaniel4ca2e602015-11-18 08:01:26 -0800674 floorVal, stitchData, floorVal, stitchData);
senorblancoce6a3542014-06-12 11:24:19 -0700675 noiseCode.appendf("\n\tif(%s.z >= %s.x) { %s.z -= %s.x; }",
egdaniel4ca2e602015-11-18 08:01:26 -0800676 floorVal, stitchData, floorVal, stitchData);
senorblancoce6a3542014-06-12 11:24:19 -0700677 noiseCode.appendf("\n\tif(%s.w >= %s.y) { %s.w -= %s.y; }",
egdaniel4ca2e602015-11-18 08:01:26 -0800678 floorVal, stitchData, floorVal, stitchData);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000679 }
680
681 // Get texture coordinates and normalize
senorblancoce6a3542014-06-12 11:24:19 -0700682 noiseCode.appendf("\n\t%s = fract(floor(mod(%s, 256.0)) / vec4(256.0));\n",
egdaniel4ca2e602015-11-18 08:01:26 -0800683 floorVal, floorVal);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000684
685 // Get permutation for x
686 {
687 SkString xCoords("");
senorblancoce6a3542014-06-12 11:24:19 -0700688 xCoords.appendf("vec2(%s.x, 0.5)", floorVal);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000689
sugoi@google.comd537af52013-06-10 13:59:25 +0000690 noiseCode.appendf("\n\tvec2 %s;\n\t%s.x = ", latticeIdx, latticeIdx);
egdaniel4ca2e602015-11-18 08:01:26 -0800691 fragBuilder->appendTextureLookup(&noiseCode, args.fSamplers[0], xCoords.c_str(),
692 kVec2f_GrSLType);
sugoi@google.comd537af52013-06-10 13:59:25 +0000693 noiseCode.append(".r;");
sugoi@google.come3b4c502013-04-05 13:47:09 +0000694 }
695
696 // Get permutation for x + 1
697 {
698 SkString xCoords("");
senorblancoce6a3542014-06-12 11:24:19 -0700699 xCoords.appendf("vec2(%s.z, 0.5)", floorVal);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000700
sugoi@google.comd537af52013-06-10 13:59:25 +0000701 noiseCode.appendf("\n\t%s.y = ", latticeIdx);
egdaniel4ca2e602015-11-18 08:01:26 -0800702 fragBuilder->appendTextureLookup(&noiseCode, args.fSamplers[0], xCoords.c_str(),
703 kVec2f_GrSLType);
sugoi@google.comd537af52013-06-10 13:59:25 +0000704 noiseCode.append(".r;");
sugoi@google.come3b4c502013-04-05 13:47:09 +0000705 }
706
commit-bot@chromium.org344cf452013-06-17 14:19:01 +0000707#if defined(SK_BUILD_FOR_ANDROID)
708 // Android rounding for Tegra devices, like, for example: Xoom (Tegra 2), Nexus 7 (Tegra 3).
709 // The issue is that colors aren't accurate enough on Tegra devices. For example, if an 8 bit
710 // value of 124 (or 0.486275 here) is entered, we can get a texture value of 123.513725
711 // (or 0.484368 here). The following rounding operation prevents these precision issues from
712 // affecting the result of the noise by making sure that we only have multiples of 1/255.
713 // (Note that 1/255 is about 0.003921569, which is the value used here).
714 noiseCode.appendf("\n\t%s = floor(%s * vec2(255.0) + vec2(0.5)) * vec2(0.003921569);",
715 latticeIdx, latticeIdx);
716#endif
717
sugoi@google.come3b4c502013-04-05 13:47:09 +0000718 // Get (x,y) coordinates with the permutated x
senorblancoce6a3542014-06-12 11:24:19 -0700719 noiseCode.appendf("\n\tvec4 %s = fract(%s.xyxy + %s.yyww);", bcoords, latticeIdx, floorVal);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000720
sugoi@google.comd537af52013-06-10 13:59:25 +0000721 noiseCode.appendf("\n\n\tvec2 %s;", uv);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000722 // Compute u, at offset (0,0)
723 {
724 SkString latticeCoords("");
senorblancoce6a3542014-06-12 11:24:19 -0700725 latticeCoords.appendf("vec2(%s.x, %s)", bcoords, chanCoord);
sugoi@google.comd537af52013-06-10 13:59:25 +0000726 noiseCode.appendf("\n\tvec4 %s = ", lattice);
egdaniel4ca2e602015-11-18 08:01:26 -0800727 fragBuilder->appendTextureLookup(&noiseCode, args.fSamplers[1], latticeCoords.c_str(),
728 kVec2f_GrSLType);
sugoi@google.comd537af52013-06-10 13:59:25 +0000729 noiseCode.appendf(".bgra;\n\t%s.x = ", uv);
730 noiseCode.appendf(dotLattice, lattice, lattice, inc8bit, fractVal);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000731 }
732
sugoi@google.comd537af52013-06-10 13:59:25 +0000733 noiseCode.appendf("\n\t%s.x -= 1.0;", fractVal);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000734 // Compute v, at offset (-1,0)
735 {
736 SkString latticeCoords("");
senorblancoce6a3542014-06-12 11:24:19 -0700737 latticeCoords.appendf("vec2(%s.y, %s)", bcoords, chanCoord);
commit-bot@chromium.org344cf452013-06-17 14:19:01 +0000738 noiseCode.append("\n\tlattice = ");
egdaniel4ca2e602015-11-18 08:01:26 -0800739 fragBuilder->appendTextureLookup(&noiseCode, args.fSamplers[1], latticeCoords.c_str(),
740 kVec2f_GrSLType);
sugoi@google.comd537af52013-06-10 13:59:25 +0000741 noiseCode.appendf(".bgra;\n\t%s.y = ", uv);
742 noiseCode.appendf(dotLattice, lattice, lattice, inc8bit, fractVal);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000743 }
744
745 // Compute 'a' as a linear interpolation of 'u' and 'v'
sugoi@google.comd537af52013-06-10 13:59:25 +0000746 noiseCode.appendf("\n\tvec2 %s;", ab);
747 noiseCode.appendf("\n\t%s.x = mix(%s.x, %s.y, %s.x);", ab, uv, uv, noiseSmooth);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000748
sugoi@google.comd537af52013-06-10 13:59:25 +0000749 noiseCode.appendf("\n\t%s.y -= 1.0;", fractVal);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000750 // Compute v, at offset (-1,-1)
751 {
752 SkString latticeCoords("");
senorblancoce6a3542014-06-12 11:24:19 -0700753 latticeCoords.appendf("vec2(%s.w, %s)", bcoords, chanCoord);
commit-bot@chromium.org344cf452013-06-17 14:19:01 +0000754 noiseCode.append("\n\tlattice = ");
egdaniel4ca2e602015-11-18 08:01:26 -0800755 fragBuilder->appendTextureLookup(&noiseCode, args.fSamplers[1], latticeCoords.c_str(),
756 kVec2f_GrSLType);
sugoi@google.comd537af52013-06-10 13:59:25 +0000757 noiseCode.appendf(".bgra;\n\t%s.y = ", uv);
758 noiseCode.appendf(dotLattice, lattice, lattice, inc8bit, fractVal);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000759 }
760
sugoi@google.comd537af52013-06-10 13:59:25 +0000761 noiseCode.appendf("\n\t%s.x += 1.0;", fractVal);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000762 // Compute u, at offset (0,-1)
763 {
764 SkString latticeCoords("");
senorblancoce6a3542014-06-12 11:24:19 -0700765 latticeCoords.appendf("vec2(%s.z, %s)", bcoords, chanCoord);
commit-bot@chromium.org344cf452013-06-17 14:19:01 +0000766 noiseCode.append("\n\tlattice = ");
egdaniel4ca2e602015-11-18 08:01:26 -0800767 fragBuilder->appendTextureLookup(&noiseCode, args.fSamplers[1], latticeCoords.c_str(),
768 kVec2f_GrSLType);
sugoi@google.comd537af52013-06-10 13:59:25 +0000769 noiseCode.appendf(".bgra;\n\t%s.x = ", uv);
770 noiseCode.appendf(dotLattice, lattice, lattice, inc8bit, fractVal);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000771 }
772
773 // Compute 'b' as a linear interpolation of 'u' and 'v'
sugoi@google.comd537af52013-06-10 13:59:25 +0000774 noiseCode.appendf("\n\t%s.y = mix(%s.x, %s.y, %s.x);", ab, uv, uv, noiseSmooth);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000775 // Compute the noise as a linear interpolation of 'a' and 'b'
sugoi@google.comd537af52013-06-10 13:59:25 +0000776 noiseCode.appendf("\n\treturn mix(%s.x, %s.y, %s.y);\n", ab, ab, noiseSmooth);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000777
sugoi@google.comd537af52013-06-10 13:59:25 +0000778 SkString noiseFuncName;
779 if (fStitchTiles) {
egdaniel4ca2e602015-11-18 08:01:26 -0800780 fragBuilder->emitFunction(kFloat_GrSLType,
781 "perlinnoise", SK_ARRAY_COUNT(gPerlinNoiseStitchArgs),
782 gPerlinNoiseStitchArgs, noiseCode.c_str(), &noiseFuncName);
sugoi@google.comd537af52013-06-10 13:59:25 +0000783 } else {
egdaniel4ca2e602015-11-18 08:01:26 -0800784 fragBuilder->emitFunction(kFloat_GrSLType,
785 "perlinnoise", SK_ARRAY_COUNT(gPerlinNoiseArgs),
786 gPerlinNoiseArgs, noiseCode.c_str(), &noiseFuncName);
sugoi@google.comd537af52013-06-10 13:59:25 +0000787 }
sugoi@google.come3b4c502013-04-05 13:47:09 +0000788
sugoi@google.comd537af52013-06-10 13:59:25 +0000789 // There are rounding errors if the floor operation is not performed here
egdaniel4ca2e602015-11-18 08:01:26 -0800790 fragBuilder->codeAppendf("\n\t\tvec2 %s = floor(%s.xy) * %s;",
791 noiseVec, vCoords.c_str(), baseFrequencyUni);
sugoi@google.comd537af52013-06-10 13:59:25 +0000792
793 // Clear the color accumulator
egdaniel4ca2e602015-11-18 08:01:26 -0800794 fragBuilder->codeAppendf("\n\t\t%s = vec4(0.0);", args.fOutputColor);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000795
796 if (fStitchTiles) {
sugoi@google.comd537af52013-06-10 13:59:25 +0000797 // Set up TurbulenceInitial stitch values.
egdaniel4ca2e602015-11-18 08:01:26 -0800798 fragBuilder->codeAppendf("\n\t\tvec2 %s = %s;", stitchData, stitchDataUni);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000799 }
sugoi@google.come3b4c502013-04-05 13:47:09 +0000800
egdaniel4ca2e602015-11-18 08:01:26 -0800801 fragBuilder->codeAppendf("\n\t\tfloat %s = 1.0;", ratio);
sugoi@google.comd537af52013-06-10 13:59:25 +0000802
803 // Loop over all octaves
egdaniel4ca2e602015-11-18 08:01:26 -0800804 fragBuilder->codeAppendf("\n\t\tfor (int octave = 0; octave < %d; ++octave) {", fNumOctaves);
sugoi@google.comd537af52013-06-10 13:59:25 +0000805
egdaniel4ca2e602015-11-18 08:01:26 -0800806 fragBuilder->codeAppendf("\n\t\t\t%s += ", args.fOutputColor);
sugoi@google.comd537af52013-06-10 13:59:25 +0000807 if (fType != SkPerlinNoiseShader::kFractalNoise_Type) {
egdaniel4ca2e602015-11-18 08:01:26 -0800808 fragBuilder->codeAppend("abs(");
sugoi@google.comd537af52013-06-10 13:59:25 +0000809 }
810 if (fStitchTiles) {
egdaniel4ca2e602015-11-18 08:01:26 -0800811 fragBuilder->codeAppendf(
sugoi@google.comd537af52013-06-10 13:59:25 +0000812 "vec4(\n\t\t\t\t%s(%s, %s, %s),\n\t\t\t\t%s(%s, %s, %s),"
813 "\n\t\t\t\t%s(%s, %s, %s),\n\t\t\t\t%s(%s, %s, %s))",
814 noiseFuncName.c_str(), chanCoordR, noiseVec, stitchData,
815 noiseFuncName.c_str(), chanCoordG, noiseVec, stitchData,
816 noiseFuncName.c_str(), chanCoordB, noiseVec, stitchData,
817 noiseFuncName.c_str(), chanCoordA, noiseVec, stitchData);
818 } else {
egdaniel4ca2e602015-11-18 08:01:26 -0800819 fragBuilder->codeAppendf(
sugoi@google.comd537af52013-06-10 13:59:25 +0000820 "vec4(\n\t\t\t\t%s(%s, %s),\n\t\t\t\t%s(%s, %s),"
821 "\n\t\t\t\t%s(%s, %s),\n\t\t\t\t%s(%s, %s))",
822 noiseFuncName.c_str(), chanCoordR, noiseVec,
823 noiseFuncName.c_str(), chanCoordG, noiseVec,
824 noiseFuncName.c_str(), chanCoordB, noiseVec,
825 noiseFuncName.c_str(), chanCoordA, noiseVec);
826 }
827 if (fType != SkPerlinNoiseShader::kFractalNoise_Type) {
egdaniel4ca2e602015-11-18 08:01:26 -0800828 fragBuilder->codeAppendf(")"); // end of "abs("
sugoi@google.comd537af52013-06-10 13:59:25 +0000829 }
egdaniel4ca2e602015-11-18 08:01:26 -0800830 fragBuilder->codeAppendf(" * %s;", ratio);
sugoi@google.comd537af52013-06-10 13:59:25 +0000831
egdaniel4ca2e602015-11-18 08:01:26 -0800832 fragBuilder->codeAppendf("\n\t\t\t%s *= vec2(2.0);", noiseVec);
833 fragBuilder->codeAppendf("\n\t\t\t%s *= 0.5;", ratio);
sugoi@google.comd537af52013-06-10 13:59:25 +0000834
835 if (fStitchTiles) {
egdaniel4ca2e602015-11-18 08:01:26 -0800836 fragBuilder->codeAppendf("\n\t\t\t%s *= vec2(2.0);", stitchData);
sugoi@google.comd537af52013-06-10 13:59:25 +0000837 }
egdaniel4ca2e602015-11-18 08:01:26 -0800838 fragBuilder->codeAppend("\n\t\t}"); // end of the for loop on octaves
sugoi@google.come3b4c502013-04-05 13:47:09 +0000839
840 if (fType == SkPerlinNoiseShader::kFractalNoise_Type) {
841 // The value of turbulenceFunctionResult comes from ((turbulenceFunctionResult) + 1) / 2
842 // by fractalNoise and (turbulenceFunctionResult) by turbulence.
egdaniel4ca2e602015-11-18 08:01:26 -0800843 fragBuilder->codeAppendf("\n\t\t%s = %s * vec4(0.5) + vec4(0.5);",
844 args.fOutputColor,args.fOutputColor);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000845 }
846
sugoi@google.come3b4c502013-04-05 13:47:09 +0000847 // Clamp values
egdaniel4ca2e602015-11-18 08:01:26 -0800848 fragBuilder->codeAppendf("\n\t\t%s = clamp(%s, 0.0, 1.0);", args.fOutputColor, args.fOutputColor);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000849
850 // Pre-multiply the result
egdaniel4ca2e602015-11-18 08:01:26 -0800851 fragBuilder->codeAppendf("\n\t\t%s = vec4(%s.rgb * %s.aaa, %s.a);\n",
852 args.fOutputColor, args.fOutputColor,
853 args.fOutputColor, args.fOutputColor);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000854}
855
jvanverthcfc18862015-04-28 08:48:20 -0700856void GrGLPerlinNoise::GenKey(const GrProcessor& processor, const GrGLSLCaps&,
joshualittb0a8a372014-09-23 09:50:21 -0700857 GrProcessorKeyBuilder* b) {
858 const GrPerlinNoiseEffect& turbulence = processor.cast<GrPerlinNoiseEffect>();
sugoi@google.come3b4c502013-04-05 13:47:09 +0000859
bsalomon63e99f72014-07-21 08:03:14 -0700860 uint32_t key = turbulence.numOctaves();
sugoi@google.come3b4c502013-04-05 13:47:09 +0000861
862 key = key << 3; // Make room for next 3 bits
863
864 switch (turbulence.type()) {
865 case SkPerlinNoiseShader::kFractalNoise_Type:
866 key |= 0x1;
867 break;
868 case SkPerlinNoiseShader::kTurbulence_Type:
869 key |= 0x2;
870 break;
871 default:
872 // leave key at 0
873 break;
874 }
875
876 if (turbulence.stitchTiles()) {
877 key |= 0x4; // Flip the 3rd bit if tile stitching is on
878 }
879
bsalomon63e99f72014-07-21 08:03:14 -0700880 b->add32(key);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000881}
882
egdaniel018fb622015-10-28 07:26:40 -0700883void GrGLPerlinNoise::onSetData(const GrGLSLProgramDataManager& pdman,
884 const GrProcessor& processor) {
wangyixb1daa862015-08-18 11:29:31 -0700885 INHERITED::onSetData(pdman, processor);
senorblancof3b50272014-06-16 10:49:58 -0700886
joshualittb0a8a372014-09-23 09:50:21 -0700887 const GrPerlinNoiseEffect& turbulence = processor.cast<GrPerlinNoiseEffect>();
sugoi@google.come3b4c502013-04-05 13:47:09 +0000888
889 const SkVector& baseFrequency = turbulence.baseFrequency();
kkinnunen7510b222014-07-30 00:04:16 -0700890 pdman.set2f(fBaseFrequencyUni, baseFrequency.fX, baseFrequency.fY);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000891
sugoi@google.com4775cba2013-04-17 13:46:56 +0000892 if (turbulence.stitchTiles()) {
893 const SkPerlinNoiseShader::StitchData& stitchData = turbulence.stitchData();
kkinnunen7510b222014-07-30 00:04:16 -0700894 pdman.set2f(fStitchDataUni, SkIntToScalar(stitchData.fWidth),
commit-bot@chromium.org98393202013-07-04 18:13:05 +0000895 SkIntToScalar(stitchData.fHeight));
sugoi@google.com4775cba2013-04-17 13:46:56 +0000896 }
897}
898
sugoi@google.come3b4c502013-04-05 13:47:09 +0000899/////////////////////////////////////////////////////////////////////
bsalomonc21b09e2015-08-28 18:46:56 -0700900const GrFragmentProcessor* SkPerlinNoiseShader::asFragmentProcessor(
901 GrContext* context,
902 const SkMatrix& viewM,
903 const SkMatrix* externalLocalMatrix,
bsalomon4a339522015-10-06 08:40:50 -0700904 SkFilterQuality) const {
bsalomon49f085d2014-09-05 13:34:00 -0700905 SkASSERT(context);
mtklein3f3b3d02014-12-01 11:47:08 -0800906
commit-bot@chromium.org96fb7482014-05-09 20:28:11 +0000907 SkMatrix localMatrix = this->getLocalMatrix();
908 if (externalLocalMatrix) {
909 localMatrix.preConcat(*externalLocalMatrix);
910 }
911
joshualitt5531d512014-12-17 15:50:11 -0800912 SkMatrix matrix = viewM;
senorblancoca6a7c22014-06-27 13:35:52 -0700913 matrix.preConcat(localMatrix);
914
commit-bot@chromium.orgc2a0ea62013-11-06 10:08:38 +0000915 if (0 == fNumOctaves) {
commit-bot@chromium.orgc2a0ea62013-11-06 10:08:38 +0000916 if (kFractalNoise_Type == fType) {
bsalomonc21b09e2015-08-28 18:46:56 -0700917 // Extract the incoming alpha and emit rgba = (a/4, a/4, a/4, a/2)
918 SkAutoTUnref<const GrFragmentProcessor> inner(
919 GrConstColorProcessor::Create(0x80404040,
920 GrConstColorProcessor::kModulateRGBA_InputMode));
bsalomonf1b7a1d2015-09-28 06:26:28 -0700921 return GrFragmentProcessor::MulOutputByInputAlpha(inner);
reedcff10b22015-03-03 06:41:45 -0800922 }
bsalomonc21b09e2015-08-28 18:46:56 -0700923 // Emit zero.
924 return GrConstColorProcessor::Create(0x0, GrConstColorProcessor::kIgnore_InputMode);
commit-bot@chromium.orgc2a0ea62013-11-06 10:08:38 +0000925 }
926
sugoi@google.come3b4c502013-04-05 13:47:09 +0000927 // Either we don't stitch tiles, either we have a valid tile size
928 SkASSERT(!fStitchTiles || !fTileSize.isEmpty());
929
joshualittb0a8a372014-09-23 09:50:21 -0700930 SkPerlinNoiseShader::PaintingData* paintingData =
halcanary385fe4d2015-08-26 13:07:48 -0700931 new PaintingData(fTileSize, fSeed, fBaseFrequencyX, fBaseFrequencyY, matrix);
bsalomonbcf0a522014-10-08 08:40:09 -0700932 SkAutoTUnref<GrTexture> permutationsTexture(
bsalomonafa95e22015-10-12 10:39:46 -0700933 GrRefCachedBitmapTexture(context, paintingData->getPermutationsBitmap(),
934 GrTextureParams::ClampNoFilter()));
bsalomonbcf0a522014-10-08 08:40:09 -0700935 SkAutoTUnref<GrTexture> noiseTexture(
bsalomonafa95e22015-10-12 10:39:46 -0700936 GrRefCachedBitmapTexture(context, paintingData->getNoiseBitmap(),
937 GrTextureParams::ClampNoFilter()));
sugoi@google.come3b4c502013-04-05 13:47:09 +0000938
joshualitt5531d512014-12-17 15:50:11 -0800939 SkMatrix m = viewM;
senorblancoca6a7c22014-06-27 13:35:52 -0700940 m.setTranslateX(-localMatrix.getTranslateX() + SK_Scalar1);
941 m.setTranslateY(-localMatrix.getTranslateY() + SK_Scalar1);
bsalomon49f085d2014-09-05 13:34:00 -0700942 if ((permutationsTexture) && (noiseTexture)) {
bsalomonc21b09e2015-08-28 18:46:56 -0700943 SkAutoTUnref<GrFragmentProcessor> inner(
bsalomon4a339522015-10-06 08:40:50 -0700944 GrPerlinNoiseEffect::Create(fType,
bsalomonc21b09e2015-08-28 18:46:56 -0700945 fNumOctaves,
946 fStitchTiles,
947 paintingData,
948 permutationsTexture, noiseTexture,
949 m));
bsalomonf1b7a1d2015-09-28 06:26:28 -0700950 return GrFragmentProcessor::MulOutputByInputAlpha(inner);
senorblancoca6a7c22014-06-27 13:35:52 -0700951 }
bsalomonc21b09e2015-08-28 18:46:56 -0700952 delete paintingData;
953 return nullptr;
sugoi@google.come3b4c502013-04-05 13:47:09 +0000954}
955
956#endif
957
commit-bot@chromium.org0f10f7b2014-03-13 18:02:17 +0000958#ifndef SK_IGNORE_TO_STRING
sugoi@google.come3b4c502013-04-05 13:47:09 +0000959void SkPerlinNoiseShader::toString(SkString* str) const {
960 str->append("SkPerlinNoiseShader: (");
961
962 str->append("type: ");
963 switch (fType) {
964 case kFractalNoise_Type:
965 str->append("\"fractal noise\"");
966 break;
967 case kTurbulence_Type:
968 str->append("\"turbulence\"");
969 break;
970 default:
971 str->append("\"unknown\"");
972 break;
973 }
974 str->append(" base frequency: (");
975 str->appendScalar(fBaseFrequencyX);
976 str->append(", ");
977 str->appendScalar(fBaseFrequencyY);
978 str->append(") number of octaves: ");
979 str->appendS32(fNumOctaves);
980 str->append(" seed: ");
981 str->appendScalar(fSeed);
982 str->append(" stitch tiles: ");
983 str->append(fStitchTiles ? "true " : "false ");
984
985 this->INHERITED::toString(str);
986
987 str->append(")");
988}
989#endif