blob: 4ebf36a803538db03b223d6fe77ef0054654666a [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"
Herb Derby83e939b2017-02-07 14:25:11 -05009
10#include "SkArenaAlloc.h"
commit-bot@chromium.orgc2a0ea62013-11-06 10:08:38 +000011#include "SkColorFilter.h"
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +000012#include "SkReadBuffer.h"
13#include "SkWriteBuffer.h"
sugoi@google.come3b4c502013-04-05 13:47:09 +000014#include "SkShader.h"
15#include "SkUnPreMultiply.h"
16#include "SkString.h"
17
18#if SK_SUPPORT_GPU
19#include "GrContext.h"
bsalomon@google.com77af6802013-10-02 13:04:56 +000020#include "GrCoordTransform.h"
joshualitteb2a6762014-12-04 11:35:33 -080021#include "SkGr.h"
Robert Phillips6f9f7eb2017-02-18 15:15:51 -050022#include "SkGrPriv.h"
bsalomonc21b09e2015-08-28 18:46:56 -070023#include "effects/GrConstColorProcessor.h"
egdaniel64c47282015-11-13 06:54:19 -080024#include "glsl/GrGLSLFragmentProcessor.h"
egdaniel2d721d32015-11-11 13:06:05 -080025#include "glsl/GrGLSLFragmentShaderBuilder.h"
egdaniel018fb622015-10-28 07:26:40 -070026#include "glsl/GrGLSLProgramDataManager.h"
egdaniel7ea439b2015-12-03 09:20:44 -080027#include "glsl/GrGLSLUniformHandler.h"
sugoi@google.come3b4c502013-04-05 13:47:09 +000028#endif
29
30static const int kBlockSize = 256;
31static const int kBlockMask = kBlockSize - 1;
32static const int kPerlinNoise = 4096;
33static const int kRandMaximum = SK_MaxS32; // 2**31 - 1
34
35namespace {
36
37// noiseValue is the color component's value (or color)
38// limitValue is the maximum perlin noise array index value allowed
39// newValue is the current noise dimension (either width or height)
40inline int checkNoise(int noiseValue, int limitValue, int newValue) {
41 // If the noise value would bring us out of bounds of the current noise array while we are
42 // stiching noise tiles together, wrap the noise around the current dimension of the noise to
43 // stay within the array bounds in a continuous fashion (so that tiling lines are not visible)
44 if (noiseValue >= limitValue) {
45 noiseValue -= newValue;
46 }
sugoi@google.come3b4c502013-04-05 13:47:09 +000047 return noiseValue;
48}
49
50inline SkScalar smoothCurve(SkScalar t) {
Mike Reed8be952a2017-02-13 20:44:33 -050051 return t * t * (3 - 2 * t);
sugoi@google.come3b4c502013-04-05 13:47:09 +000052}
53
54} // end namespace
55
56struct SkPerlinNoiseShader::StitchData {
57 StitchData()
58 : fWidth(0)
59 , fWrapX(0)
60 , fHeight(0)
61 , fWrapY(0)
62 {}
63
64 bool operator==(const StitchData& other) const {
65 return fWidth == other.fWidth &&
66 fWrapX == other.fWrapX &&
67 fHeight == other.fHeight &&
68 fWrapY == other.fWrapY;
69 }
70
71 int fWidth; // How much to subtract to wrap for stitching.
72 int fWrapX; // Minimum value to wrap.
73 int fHeight;
74 int fWrapY;
75};
76
77struct SkPerlinNoiseShader::PaintingData {
commit-bot@chromium.orgfd5c9a62014-03-06 15:13:53 +000078 PaintingData(const SkISize& tileSize, SkScalar seed,
senorblancoca6a7c22014-06-27 13:35:52 -070079 SkScalar baseFrequencyX, SkScalar baseFrequencyY,
80 const SkMatrix& matrix)
sugoi@google.come3b4c502013-04-05 13:47:09 +000081 {
reed11fa2242015-03-13 06:08:28 -070082 SkVector vec[2] = {
83 { SkScalarInvert(baseFrequencyX), SkScalarInvert(baseFrequencyY) },
84 { SkIntToScalar(tileSize.fWidth), SkIntToScalar(tileSize.fHeight) },
85 };
86 matrix.mapVectors(vec, 2);
87
88 fBaseFrequency.set(SkScalarInvert(vec[0].fX), SkScalarInvert(vec[0].fY));
89 fTileSize.set(SkScalarRoundToInt(vec[1].fX), SkScalarRoundToInt(vec[1].fY));
commit-bot@chromium.orgfd5c9a62014-03-06 15:13:53 +000090 this->init(seed);
91 if (!fTileSize.isEmpty()) {
92 this->stitch();
93 }
94
senorblancof3b50272014-06-16 10:49:58 -070095#if SK_SUPPORT_GPU
commit-bot@chromium.orga3264e52014-05-30 13:26:10 +000096 fPermutationsBitmap.setInfo(SkImageInfo::MakeA8(kBlockSize, 1));
commit-bot@chromium.orgfd5c9a62014-03-06 15:13:53 +000097 fPermutationsBitmap.setPixels(fLatticeSelector);
98
commit-bot@chromium.orga3264e52014-05-30 13:26:10 +000099 fNoiseBitmap.setInfo(SkImageInfo::MakeN32Premul(kBlockSize, 4));
commit-bot@chromium.orgfd5c9a62014-03-06 15:13:53 +0000100 fNoiseBitmap.setPixels(fNoise[0][0]);
101#endif
sugoi@google.come3b4c502013-04-05 13:47:09 +0000102 }
103
104 int fSeed;
105 uint8_t fLatticeSelector[kBlockSize];
106 uint16_t fNoise[4][kBlockSize][2];
107 SkPoint fGradient[4][kBlockSize];
108 SkISize fTileSize;
109 SkVector fBaseFrequency;
110 StitchData fStitchDataInit;
111
112private:
113
senorblancof3b50272014-06-16 10:49:58 -0700114#if SK_SUPPORT_GPU
commit-bot@chromium.orgfd5c9a62014-03-06 15:13:53 +0000115 SkBitmap fPermutationsBitmap;
116 SkBitmap fNoiseBitmap;
117#endif
sugoi@google.come3b4c502013-04-05 13:47:09 +0000118
119 inline int random() {
120 static const int gRandAmplitude = 16807; // 7**5; primitive root of m
121 static const int gRandQ = 127773; // m / a
122 static const int gRandR = 2836; // m % a
123
124 int result = gRandAmplitude * (fSeed % gRandQ) - gRandR * (fSeed / gRandQ);
125 if (result <= 0)
126 result += kRandMaximum;
127 fSeed = result;
128 return result;
129 }
130
commit-bot@chromium.orgfd5c9a62014-03-06 15:13:53 +0000131 // Only called once. Could be part of the constructor.
sugoi@google.come3b4c502013-04-05 13:47:09 +0000132 void init(SkScalar seed)
133 {
134 static const SkScalar gInvBlockSizef = SkScalarInvert(SkIntToScalar(kBlockSize));
135
senorblanco@chromium.org857e3202014-01-09 17:41:42 +0000136 // According to the SVG spec, we must truncate (not round) the seed value.
137 fSeed = SkScalarTruncToInt(seed);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000138 // The seed value clamp to the range [1, kRandMaximum - 1].
sugoi@google.come3b4c502013-04-05 13:47:09 +0000139 if (fSeed <= 0) {
140 fSeed = -(fSeed % (kRandMaximum - 1)) + 1;
141 }
142 if (fSeed > kRandMaximum - 1) {
143 fSeed = kRandMaximum - 1;
144 }
145 for (int channel = 0; channel < 4; ++channel) {
146 for (int i = 0; i < kBlockSize; ++i) {
147 fLatticeSelector[i] = i;
148 fNoise[channel][i][0] = (random() % (2 * kBlockSize));
149 fNoise[channel][i][1] = (random() % (2 * kBlockSize));
150 }
151 }
152 for (int i = kBlockSize - 1; i > 0; --i) {
153 int k = fLatticeSelector[i];
154 int j = random() % kBlockSize;
155 SkASSERT(j >= 0);
156 SkASSERT(j < kBlockSize);
157 fLatticeSelector[i] = fLatticeSelector[j];
158 fLatticeSelector[j] = k;
159 }
160
161 // Perform the permutations now
162 {
163 // Copy noise data
164 uint16_t noise[4][kBlockSize][2];
165 for (int i = 0; i < kBlockSize; ++i) {
166 for (int channel = 0; channel < 4; ++channel) {
167 for (int j = 0; j < 2; ++j) {
168 noise[channel][i][j] = fNoise[channel][i][j];
169 }
170 }
171 }
172 // Do permutations on noise data
173 for (int i = 0; i < kBlockSize; ++i) {
174 for (int channel = 0; channel < 4; ++channel) {
175 for (int j = 0; j < 2; ++j) {
176 fNoise[channel][i][j] = noise[channel][fLatticeSelector[i]][j];
177 }
178 }
179 }
180 }
181
182 // Half of the largest possible value for 16 bit unsigned int
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +0000183 static const SkScalar gHalfMax16bits = 32767.5f;
sugoi@google.come3b4c502013-04-05 13:47:09 +0000184
185 // Compute gradients from permutated noise data
186 for (int channel = 0; channel < 4; ++channel) {
187 for (int i = 0; i < kBlockSize; ++i) {
188 fGradient[channel][i] = SkPoint::Make(
Mike Reed8be952a2017-02-13 20:44:33 -0500189 (fNoise[channel][i][0] - kBlockSize) * gInvBlockSizef,
190 (fNoise[channel][i][1] - kBlockSize) * gInvBlockSizef);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000191 fGradient[channel][i].normalize();
192 // Put the normalized gradient back into the noise data
Mike Reed8be952a2017-02-13 20:44:33 -0500193 fNoise[channel][i][0] = SkScalarRoundToInt(
194 (fGradient[channel][i].fX + 1) * gHalfMax16bits);
195 fNoise[channel][i][1] = SkScalarRoundToInt(
196 (fGradient[channel][i].fY + 1) * gHalfMax16bits);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000197 }
198 }
sugoi@google.come3b4c502013-04-05 13:47:09 +0000199 }
200
commit-bot@chromium.orgfd5c9a62014-03-06 15:13:53 +0000201 // Only called once. Could be part of the constructor.
sugoi@google.come3b4c502013-04-05 13:47:09 +0000202 void stitch() {
203 SkScalar tileWidth = SkIntToScalar(fTileSize.width());
204 SkScalar tileHeight = SkIntToScalar(fTileSize.height());
205 SkASSERT(tileWidth > 0 && tileHeight > 0);
206 // When stitching tiled turbulence, the frequencies must be adjusted
207 // so that the tile borders will be continuous.
208 if (fBaseFrequency.fX) {
reed@google.com8015cdd2013-12-18 15:49:32 +0000209 SkScalar lowFrequencx =
210 SkScalarFloorToScalar(tileWidth * fBaseFrequency.fX) / tileWidth;
211 SkScalar highFrequencx =
212 SkScalarCeilToScalar(tileWidth * fBaseFrequency.fX) / tileWidth;
sugoi@google.come3b4c502013-04-05 13:47:09 +0000213 // BaseFrequency should be non-negative according to the standard.
reed80ea19c2015-05-12 10:37:34 -0700214 if (fBaseFrequency.fX / lowFrequencx < highFrequencx / fBaseFrequency.fX) {
sugoi@google.come3b4c502013-04-05 13:47:09 +0000215 fBaseFrequency.fX = lowFrequencx;
216 } else {
217 fBaseFrequency.fX = highFrequencx;
218 }
219 }
220 if (fBaseFrequency.fY) {
reed@google.com8015cdd2013-12-18 15:49:32 +0000221 SkScalar lowFrequency =
222 SkScalarFloorToScalar(tileHeight * fBaseFrequency.fY) / tileHeight;
223 SkScalar highFrequency =
224 SkScalarCeilToScalar(tileHeight * fBaseFrequency.fY) / tileHeight;
reed80ea19c2015-05-12 10:37:34 -0700225 if (fBaseFrequency.fY / lowFrequency < highFrequency / fBaseFrequency.fY) {
sugoi@google.come3b4c502013-04-05 13:47:09 +0000226 fBaseFrequency.fY = lowFrequency;
227 } else {
228 fBaseFrequency.fY = highFrequency;
229 }
230 }
231 // Set up TurbulenceInitial stitch values.
232 fStitchDataInit.fWidth =
reed@google.com8015cdd2013-12-18 15:49:32 +0000233 SkScalarRoundToInt(tileWidth * fBaseFrequency.fX);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000234 fStitchDataInit.fWrapX = kPerlinNoise + fStitchDataInit.fWidth;
235 fStitchDataInit.fHeight =
reed@google.com8015cdd2013-12-18 15:49:32 +0000236 SkScalarRoundToInt(tileHeight * fBaseFrequency.fY);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000237 fStitchDataInit.fWrapY = kPerlinNoise + fStitchDataInit.fHeight;
238 }
239
commit-bot@chromium.orgfd5c9a62014-03-06 15:13:53 +0000240public:
sugoi@google.come3b4c502013-04-05 13:47:09 +0000241
senorblancof3b50272014-06-16 10:49:58 -0700242#if SK_SUPPORT_GPU
commit-bot@chromium.orgfd5c9a62014-03-06 15:13:53 +0000243 const SkBitmap& getPermutationsBitmap() const { return fPermutationsBitmap; }
244
245 const SkBitmap& getNoiseBitmap() const { return fNoiseBitmap; }
246#endif
sugoi@google.come3b4c502013-04-05 13:47:09 +0000247};
248
reedfe630452016-03-25 09:08:00 -0700249sk_sp<SkShader> SkPerlinNoiseShader::MakeFractalNoise(SkScalar baseFrequencyX,
250 SkScalar baseFrequencyY,
251 int numOctaves, SkScalar seed,
252 const SkISize* tileSize) {
253 return sk_sp<SkShader>(new SkPerlinNoiseShader(kFractalNoise_Type, baseFrequencyX,
254 baseFrequencyY, numOctaves,
255 seed, tileSize));
sugoi@google.come3b4c502013-04-05 13:47:09 +0000256}
257
reedfe630452016-03-25 09:08:00 -0700258sk_sp<SkShader> SkPerlinNoiseShader::MakeTurbulence(SkScalar baseFrequencyX,
259 SkScalar baseFrequencyY,
260 int numOctaves, SkScalar seed,
261 const SkISize* tileSize) {
262 return sk_sp<SkShader>(new SkPerlinNoiseShader(kTurbulence_Type, baseFrequencyX, baseFrequencyY,
263 numOctaves, 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)
robertphillips6a16fd32016-06-27 12:26:29 -0700275 , fNumOctaves(SkTPin<int>(numOctaves, 0, 255)) // [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{
robertphillips6a16fd32016-06-27 12:26:29 -0700280 SkASSERT(fNumOctaves >= 0 && fNumOctaves < 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
reed60c9b582016-04-03 09:11:13 -0700286sk_sp<SkFlattenable> SkPerlinNoiseShader::CreateProc(SkReadBuffer& buffer) {
reed9fa60da2014-08-21 07:59:51 -0700287 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:
reedfe630452016-03-25 09:08:00 -0700298 return SkPerlinNoiseShader::MakeFractalNoise(freqX, freqY, octaves, seed,
reed60c9b582016-04-03 09:11:13 -0700299 &tileSize);
reed9fa60da2014-08-21 07:59:51 -0700300 case kTurbulence_Type:
reedfe630452016-03-25 09:08:00 -0700301 return SkPerlinNoiseShader::MakeTurbulence(freqX, freqY, octaves, seed,
reed60c9b582016-04-03 09:11:13 -0700302 &tileSize);
reed9fa60da2014-08-21 07:59:51 -0700303 default:
halcanary96fcdcc2015-08-27 07:41:13 -0700304 return nullptr;
reed9fa60da2014-08-21 07:59:51 -0700305 }
306}
307
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +0000308void SkPerlinNoiseShader::flatten(SkWriteBuffer& buffer) const {
sugoi@google.come3b4c502013-04-05 13:47:09 +0000309 buffer.writeInt((int) fType);
310 buffer.writeScalar(fBaseFrequencyX);
311 buffer.writeScalar(fBaseFrequencyY);
312 buffer.writeInt(fNumOctaves);
313 buffer.writeScalar(fSeed);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000314 buffer.writeInt(fTileSize.fWidth);
315 buffer.writeInt(fTileSize.fHeight);
316}
317
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000318SkScalar SkPerlinNoiseShader::PerlinNoiseShaderContext::noise2D(
senorblancoca6a7c22014-06-27 13:35:52 -0700319 int channel, const StitchData& stitchData, const SkPoint& noiseVector) const {
sugoi@google.come3b4c502013-04-05 13:47:09 +0000320 struct Noise {
321 int noisePositionIntegerValue;
senorblancoce6a3542014-06-12 11:24:19 -0700322 int nextNoisePositionIntegerValue;
sugoi@google.come3b4c502013-04-05 13:47:09 +0000323 SkScalar noisePositionFractionValue;
324 Noise(SkScalar component)
325 {
326 SkScalar position = component + kPerlinNoise;
327 noisePositionIntegerValue = SkScalarFloorToInt(position);
328 noisePositionFractionValue = position - SkIntToScalar(noisePositionIntegerValue);
senorblancoce6a3542014-06-12 11:24:19 -0700329 nextNoisePositionIntegerValue = noisePositionIntegerValue + 1;
sugoi@google.come3b4c502013-04-05 13:47:09 +0000330 }
331 };
332 Noise noiseX(noiseVector.x());
333 Noise noiseY(noiseVector.y());
334 SkScalar u, v;
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000335 const SkPerlinNoiseShader& perlinNoiseShader = static_cast<const SkPerlinNoiseShader&>(fShader);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000336 // If stitching, adjust lattice points accordingly.
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000337 if (perlinNoiseShader.fStitchTiles) {
sugoi@google.come3b4c502013-04-05 13:47:09 +0000338 noiseX.noisePositionIntegerValue =
339 checkNoise(noiseX.noisePositionIntegerValue, stitchData.fWrapX, stitchData.fWidth);
340 noiseY.noisePositionIntegerValue =
341 checkNoise(noiseY.noisePositionIntegerValue, stitchData.fWrapY, stitchData.fHeight);
senorblancoce6a3542014-06-12 11:24:19 -0700342 noiseX.nextNoisePositionIntegerValue =
343 checkNoise(noiseX.nextNoisePositionIntegerValue, stitchData.fWrapX, stitchData.fWidth);
344 noiseY.nextNoisePositionIntegerValue =
345 checkNoise(noiseY.nextNoisePositionIntegerValue, stitchData.fWrapY, stitchData.fHeight);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000346 }
347 noiseX.noisePositionIntegerValue &= kBlockMask;
348 noiseY.noisePositionIntegerValue &= kBlockMask;
senorblancoce6a3542014-06-12 11:24:19 -0700349 noiseX.nextNoisePositionIntegerValue &= kBlockMask;
350 noiseY.nextNoisePositionIntegerValue &= kBlockMask;
351 int i =
senorblancoca6a7c22014-06-27 13:35:52 -0700352 fPaintingData->fLatticeSelector[noiseX.noisePositionIntegerValue];
senorblancoce6a3542014-06-12 11:24:19 -0700353 int j =
senorblancoca6a7c22014-06-27 13:35:52 -0700354 fPaintingData->fLatticeSelector[noiseX.nextNoisePositionIntegerValue];
senorblancoce6a3542014-06-12 11:24:19 -0700355 int b00 = (i + noiseY.noisePositionIntegerValue) & kBlockMask;
356 int b10 = (j + noiseY.noisePositionIntegerValue) & kBlockMask;
357 int b01 = (i + noiseY.nextNoisePositionIntegerValue) & kBlockMask;
358 int b11 = (j + noiseY.nextNoisePositionIntegerValue) & kBlockMask;
sugoi@google.come3b4c502013-04-05 13:47:09 +0000359 SkScalar sx = smoothCurve(noiseX.noisePositionFractionValue);
360 SkScalar sy = smoothCurve(noiseY.noisePositionFractionValue);
361 // This is taken 1:1 from SVG spec: http://www.w3.org/TR/SVG11/filters.html#feTurbulenceElement
362 SkPoint fractionValue = SkPoint::Make(noiseX.noisePositionFractionValue,
363 noiseY.noisePositionFractionValue); // Offset (0,0)
senorblancoca6a7c22014-06-27 13:35:52 -0700364 u = fPaintingData->fGradient[channel][b00].dot(fractionValue);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000365 fractionValue.fX -= SK_Scalar1; // Offset (-1,0)
senorblancoca6a7c22014-06-27 13:35:52 -0700366 v = fPaintingData->fGradient[channel][b10].dot(fractionValue);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000367 SkScalar a = SkScalarInterp(u, v, sx);
368 fractionValue.fY -= SK_Scalar1; // Offset (-1,-1)
senorblancoca6a7c22014-06-27 13:35:52 -0700369 v = fPaintingData->fGradient[channel][b11].dot(fractionValue);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000370 fractionValue.fX = noiseX.noisePositionFractionValue; // Offset (0,-1)
senorblancoca6a7c22014-06-27 13:35:52 -0700371 u = fPaintingData->fGradient[channel][b01].dot(fractionValue);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000372 SkScalar b = SkScalarInterp(u, v, sx);
373 return SkScalarInterp(a, b, sy);
374}
375
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000376SkScalar SkPerlinNoiseShader::PerlinNoiseShaderContext::calculateTurbulenceValueForPoint(
senorblancoca6a7c22014-06-27 13:35:52 -0700377 int channel, StitchData& stitchData, const SkPoint& point) const {
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000378 const SkPerlinNoiseShader& perlinNoiseShader = static_cast<const SkPerlinNoiseShader&>(fShader);
379 if (perlinNoiseShader.fStitchTiles) {
sugoi@google.come3b4c502013-04-05 13:47:09 +0000380 // Set up TurbulenceInitial stitch values.
senorblancoca6a7c22014-06-27 13:35:52 -0700381 stitchData = fPaintingData->fStitchDataInit;
sugoi@google.come3b4c502013-04-05 13:47:09 +0000382 }
383 SkScalar turbulenceFunctionResult = 0;
Mike Reed8be952a2017-02-13 20:44:33 -0500384 SkPoint noiseVector(SkPoint::Make(point.x() * fPaintingData->fBaseFrequency.fX,
385 point.y() * fPaintingData->fBaseFrequency.fY));
sugoi@google.come3b4c502013-04-05 13:47:09 +0000386 SkScalar ratio = SK_Scalar1;
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000387 for (int octave = 0; octave < perlinNoiseShader.fNumOctaves; ++octave) {
senorblancoca6a7c22014-06-27 13:35:52 -0700388 SkScalar noise = noise2D(channel, stitchData, noiseVector);
reed80ea19c2015-05-12 10:37:34 -0700389 SkScalar numer = (perlinNoiseShader.fType == kFractalNoise_Type) ?
390 noise : SkScalarAbs(noise);
391 turbulenceFunctionResult += numer / ratio;
sugoi@google.come3b4c502013-04-05 13:47:09 +0000392 noiseVector.fX *= 2;
393 noiseVector.fY *= 2;
394 ratio *= 2;
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000395 if (perlinNoiseShader.fStitchTiles) {
sugoi@google.come3b4c502013-04-05 13:47:09 +0000396 // Update stitch values
397 stitchData.fWidth *= 2;
398 stitchData.fWrapX = stitchData.fWidth + kPerlinNoise;
399 stitchData.fHeight *= 2;
400 stitchData.fWrapY = stitchData.fHeight + kPerlinNoise;
401 }
402 }
403
404 // The value of turbulenceFunctionResult comes from ((turbulenceFunctionResult) + 1) / 2
405 // by fractalNoise and (turbulenceFunctionResult) by turbulence.
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000406 if (perlinNoiseShader.fType == kFractalNoise_Type) {
Mike Reed8be952a2017-02-13 20:44:33 -0500407 turbulenceFunctionResult = turbulenceFunctionResult * SK_ScalarHalf + SK_ScalarHalf;
sugoi@google.come3b4c502013-04-05 13:47:09 +0000408 }
409
410 if (channel == 3) { // Scale alpha by paint value
reed80ea19c2015-05-12 10:37:34 -0700411 turbulenceFunctionResult *= SkIntToScalar(getPaintAlpha()) / 255;
sugoi@google.come3b4c502013-04-05 13:47:09 +0000412 }
413
414 // Clamp result
415 return SkScalarPin(turbulenceFunctionResult, 0, SK_Scalar1);
416}
417
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000418SkPMColor SkPerlinNoiseShader::PerlinNoiseShaderContext::shade(
419 const SkPoint& point, StitchData& stitchData) const {
sugoi@google.come3b4c502013-04-05 13:47:09 +0000420 SkPoint newPoint;
senorblanco@chromium.orga8d95f82014-04-04 14:46:10 +0000421 fMatrix.mapPoints(&newPoint, &point, 1);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000422 newPoint.fX = SkScalarRoundToScalar(newPoint.fX);
423 newPoint.fY = SkScalarRoundToScalar(newPoint.fY);
424
425 U8CPU rgba[4];
426 for (int channel = 3; channel >= 0; --channel) {
427 rgba[channel] = SkScalarFloorToInt(255 *
senorblancoca6a7c22014-06-27 13:35:52 -0700428 calculateTurbulenceValueForPoint(channel, stitchData, newPoint));
sugoi@google.come3b4c502013-04-05 13:47:09 +0000429 }
430 return SkPreMultiplyARGB(rgba[3], rgba[0], rgba[1], rgba[2]);
431}
432
Herb Derby83e939b2017-02-07 14:25:11 -0500433SkShader::Context* SkPerlinNoiseShader::onMakeContext(
434 const ContextRec& rec, SkArenaAlloc* alloc) const {
435 return alloc->make<PerlinNoiseShaderContext>(*this, rec);
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000436}
437
438SkPerlinNoiseShader::PerlinNoiseShaderContext::PerlinNoiseShaderContext(
commit-bot@chromium.orge901b6d2014-05-01 19:31:31 +0000439 const SkPerlinNoiseShader& shader, const ContextRec& rec)
440 : INHERITED(shader, rec)
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000441{
Florin Malitabbeb5732017-01-26 16:23:06 -0500442 SkMatrix newMatrix = SkMatrix::Concat(*rec.fMatrix, shader.getLocalMatrix());
reed@google.comb67b8e62014-05-12 18:12:24 +0000443 if (rec.fLocalMatrix) {
444 newMatrix.preConcat(*rec.fLocalMatrix);
445 }
senorblanco@chromium.orga8d95f82014-04-04 14:46:10 +0000446 // This (1,1) translation is due to WebKit's 1 based coordinates for the noise
447 // (as opposed to 0 based, usually). The same adjustment is in the setData() function.
senorblancoca6a7c22014-06-27 13:35:52 -0700448 fMatrix.setTranslate(-newMatrix.getTranslateX() + SK_Scalar1, -newMatrix.getTranslateY() + SK_Scalar1);
halcanary385fe4d2015-08-26 13:07:48 -0700449 fPaintingData = new PaintingData(shader.fTileSize, shader.fSeed, shader.fBaseFrequencyX,
450 shader.fBaseFrequencyY, newMatrix);
senorblancoca6a7c22014-06-27 13:35:52 -0700451}
452
halcanary385fe4d2015-08-26 13:07:48 -0700453SkPerlinNoiseShader::PerlinNoiseShaderContext::~PerlinNoiseShaderContext() { delete fPaintingData; }
sugoi@google.come3b4c502013-04-05 13:47:09 +0000454
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000455void SkPerlinNoiseShader::PerlinNoiseShaderContext::shadeSpan(
456 int x, int y, SkPMColor result[], int count) {
sugoi@google.come3b4c502013-04-05 13:47:09 +0000457 SkPoint point = SkPoint::Make(SkIntToScalar(x), SkIntToScalar(y));
458 StitchData stitchData;
459 for (int i = 0; i < count; ++i) {
460 result[i] = shade(point, stitchData);
461 point.fX += SK_Scalar1;
462 }
463}
464
sugoi@google.come3b4c502013-04-05 13:47:09 +0000465/////////////////////////////////////////////////////////////////////
466
commit-bot@chromium.org344cf452013-06-17 14:19:01 +0000467#if SK_SUPPORT_GPU
sugoi@google.come3b4c502013-04-05 13:47:09 +0000468
egdaniel64c47282015-11-13 06:54:19 -0800469class GrGLPerlinNoise : public GrGLSLFragmentProcessor {
sugoi@google.com4775cba2013-04-17 13:46:56 +0000470public:
robertphillips9cdb9922016-02-03 12:25:40 -0800471 void emitCode(EmitArgs&) override;
sugoi@google.come3b4c502013-04-05 13:47:09 +0000472
Brian Salomon94efbf52016-11-29 13:43:05 -0500473 static inline void GenKey(const GrProcessor&, const GrShaderCaps&, GrProcessorKeyBuilder*);
sugoi@google.com4775cba2013-04-17 13:46:56 +0000474
wangyixb1daa862015-08-18 11:29:31 -0700475protected:
egdaniel018fb622015-10-28 07:26:40 -0700476 void onSetData(const GrGLSLProgramDataManager&, const GrProcessor&) override;
wangyixb1daa862015-08-18 11:29:31 -0700477
sugoi@google.com4775cba2013-04-17 13:46:56 +0000478private:
egdaniel018fb622015-10-28 07:26:40 -0700479 GrGLSLProgramDataManager::UniformHandle fStitchDataUni;
egdaniel018fb622015-10-28 07:26:40 -0700480 GrGLSLProgramDataManager::UniformHandle fBaseFrequencyUni;
senorblancof3b50272014-06-16 10:49:58 -0700481
egdaniel64c47282015-11-13 06:54:19 -0800482 typedef GrGLSLFragmentProcessor INHERITED;
sugoi@google.come3b4c502013-04-05 13:47:09 +0000483};
484
485/////////////////////////////////////////////////////////////////////
486
joshualittb0a8a372014-09-23 09:50:21 -0700487class GrPerlinNoiseEffect : public GrFragmentProcessor {
sugoi@google.come3b4c502013-04-05 13:47:09 +0000488public:
Brian Osman32342f02017-03-04 08:12:46 -0500489 static sk_sp<GrFragmentProcessor> Make(GrResourceProvider* resourceProvider,
Robert Phillips6f9f7eb2017-02-18 15:15:51 -0500490 SkPerlinNoiseShader::Type type,
bungeman06ca8ec2016-06-09 08:01:03 -0700491 int numOctaves, bool stitchTiles,
492 SkPerlinNoiseShader::PaintingData* paintingData,
Robert Phillips6f9f7eb2017-02-18 15:15:51 -0500493 sk_sp<GrTextureProxy> permutationsProxy,
494 sk_sp<GrTextureProxy> noiseProxy,
bungeman06ca8ec2016-06-09 08:01:03 -0700495 const SkMatrix& matrix) {
496 return sk_sp<GrFragmentProcessor>(
Brian Osman32342f02017-03-04 08:12:46 -0500497 new GrPerlinNoiseEffect(resourceProvider, type, numOctaves, stitchTiles, paintingData,
Robert Phillips6f9f7eb2017-02-18 15:15:51 -0500498 std::move(permutationsProxy), std::move(noiseProxy), matrix));
sugoi@google.come3b4c502013-04-05 13:47:09 +0000499 }
500
halcanary385fe4d2015-08-26 13:07:48 -0700501 virtual ~GrPerlinNoiseEffect() { delete fPaintingData; }
sugoi@google.come3b4c502013-04-05 13:47:09 +0000502
mtklein36352bf2015-03-25 18:17:31 -0700503 const char* name() const override { return "PerlinNoise"; }
joshualitteb2a6762014-12-04 11:35:33 -0800504
senorblancoca6a7c22014-06-27 13:35:52 -0700505 const SkPerlinNoiseShader::StitchData& stitchData() const { return fPaintingData->fStitchDataInit; }
sugoi@google.come3b4c502013-04-05 13:47:09 +0000506
senorblancof3b50272014-06-16 10:49:58 -0700507 SkPerlinNoiseShader::Type type() const { return fType; }
508 bool stitchTiles() const { return fStitchTiles; }
senorblancoca6a7c22014-06-27 13:35:52 -0700509 const SkVector& baseFrequency() const { return fPaintingData->fBaseFrequency; }
senorblancof3b50272014-06-16 10:49:58 -0700510 int numOctaves() const { return fNumOctaves; }
senorblancof3b50272014-06-16 10:49:58 -0700511
sugoi@google.come3b4c502013-04-05 13:47:09 +0000512private:
egdaniel57d3b032015-11-13 11:57:27 -0800513 GrGLSLFragmentProcessor* onCreateGLSLInstance() const override {
robertphillipsbf536af2016-02-04 06:11:53 -0800514 return new GrGLPerlinNoise;
wangyixb1daa862015-08-18 11:29:31 -0700515 }
516
Brian Salomon94efbf52016-11-29 13:43:05 -0500517 virtual void onGetGLSLProcessorKey(const GrShaderCaps& caps,
egdaniel57d3b032015-11-13 11:57:27 -0800518 GrProcessorKeyBuilder* b) const override {
wangyix4b3050b2015-08-04 07:59:37 -0700519 GrGLPerlinNoise::GenKey(*this, caps, b);
520 }
521
mtklein36352bf2015-03-25 18:17:31 -0700522 bool onIsEqual(const GrFragmentProcessor& sBase) const override {
joshualitt49586be2014-09-16 08:21:41 -0700523 const GrPerlinNoiseEffect& s = sBase.cast<GrPerlinNoiseEffect>();
senorblancof3b50272014-06-16 10:49:58 -0700524 return fType == s.fType &&
senorblancoca6a7c22014-06-27 13:35:52 -0700525 fPaintingData->fBaseFrequency == s.fPaintingData->fBaseFrequency &&
senorblancof3b50272014-06-16 10:49:58 -0700526 fNumOctaves == s.fNumOctaves &&
527 fStitchTiles == s.fStitchTiles &&
senorblancoca6a7c22014-06-27 13:35:52 -0700528 fPaintingData->fStitchDataInit == s.fPaintingData->fStitchDataInit;
sugoi@google.come3b4c502013-04-05 13:47:09 +0000529 }
530
Brian Osman32342f02017-03-04 08:12:46 -0500531 GrPerlinNoiseEffect(GrResourceProvider* resourceProvider,
Robert Phillips6f9f7eb2017-02-18 15:15:51 -0500532 SkPerlinNoiseShader::Type type, int numOctaves, bool stitchTiles,
senorblancoca6a7c22014-06-27 13:35:52 -0700533 SkPerlinNoiseShader::PaintingData* paintingData,
Robert Phillips6f9f7eb2017-02-18 15:15:51 -0500534 sk_sp<GrTextureProxy> permutationsProxy, sk_sp<GrTextureProxy> noiseProxy,
bsalomonc21b09e2015-08-28 18:46:56 -0700535 const SkMatrix& matrix)
Brian Salomon587e08f2017-01-27 10:59:27 -0500536 : INHERITED(kNone_OptimizationFlags)
537 , fType(type)
538 , fCoordTransform(matrix)
539 , fNumOctaves(numOctaves)
540 , fStitchTiles(stitchTiles)
Brian Osman32342f02017-03-04 08:12:46 -0500541 , fPermutationsSampler(resourceProvider, std::move(permutationsProxy))
542 , fNoiseSampler(resourceProvider, std::move(noiseProxy))
Brian Salomon587e08f2017-01-27 10:59:27 -0500543 , fPaintingData(paintingData) {
joshualitteb2a6762014-12-04 11:35:33 -0800544 this->initClassID<GrPerlinNoiseEffect>();
Brian Salomon0bbecb22016-11-17 11:38:22 -0500545 this->addTextureSampler(&fPermutationsSampler);
546 this->addTextureSampler(&fNoiseSampler);
senorblancof3b50272014-06-16 10:49:58 -0700547 this->addCoordTransform(&fCoordTransform);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000548 }
549
joshualittb0a8a372014-09-23 09:50:21 -0700550 GR_DECLARE_FRAGMENT_PROCESSOR_TEST;
sugoi@google.come3b4c502013-04-05 13:47:09 +0000551
senorblancof3b50272014-06-16 10:49:58 -0700552 SkPerlinNoiseShader::Type fType;
553 GrCoordTransform fCoordTransform;
senorblancof3b50272014-06-16 10:49:58 -0700554 int fNumOctaves;
555 bool fStitchTiles;
Brian Salomon0bbecb22016-11-17 11:38:22 -0500556 TextureSampler fPermutationsSampler;
557 TextureSampler fNoiseSampler;
senorblancoca6a7c22014-06-27 13:35:52 -0700558 SkPerlinNoiseShader::PaintingData *fPaintingData;
sugoi@google.come3b4c502013-04-05 13:47:09 +0000559
sugoi@google.com4775cba2013-04-17 13:46:56 +0000560private:
joshualittb0a8a372014-09-23 09:50:21 -0700561 typedef GrFragmentProcessor INHERITED;
sugoi@google.come3b4c502013-04-05 13:47:09 +0000562};
563
564/////////////////////////////////////////////////////////////////////
joshualittb0a8a372014-09-23 09:50:21 -0700565GR_DEFINE_FRAGMENT_PROCESSOR_TEST(GrPerlinNoiseEffect);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000566
Hal Canary6f6961e2017-01-31 13:50:44 -0500567#if GR_TEST_UTILS
bungeman06ca8ec2016-06-09 08:01:03 -0700568sk_sp<GrFragmentProcessor> GrPerlinNoiseEffect::TestCreate(GrProcessorTestData* d) {
joshualitt0067ff52015-07-08 14:26:19 -0700569 int numOctaves = d->fRandom->nextRangeU(2, 10);
570 bool stitchTiles = d->fRandom->nextBool();
571 SkScalar seed = SkIntToScalar(d->fRandom->nextU());
572 SkISize tileSize = SkISize::Make(d->fRandom->nextRangeU(4, 4096),
573 d->fRandom->nextRangeU(4, 4096));
574 SkScalar baseFrequencyX = d->fRandom->nextRangeScalar(0.01f,
575 0.99f);
576 SkScalar baseFrequencyY = d->fRandom->nextRangeScalar(0.01f,
577 0.99f);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000578
reedfe630452016-03-25 09:08:00 -0700579 sk_sp<SkShader> shader(d->fRandom->nextBool() ?
580 SkPerlinNoiseShader::MakeFractalNoise(baseFrequencyX, baseFrequencyY, numOctaves, seed,
581 stitchTiles ? &tileSize : nullptr) :
582 SkPerlinNoiseShader::MakeTurbulence(baseFrequencyX, baseFrequencyY, numOctaves, seed,
583 stitchTiles ? &tileSize : nullptr));
sugoi@google.come3b4c502013-04-05 13:47:09 +0000584
Brian Osman9f532a32016-10-19 11:12:09 -0400585 GrTest::TestAsFPArgs asFPArgs(d);
586 return shader->asFragmentProcessor(asFPArgs.args());
sugoi@google.come3b4c502013-04-05 13:47:09 +0000587}
Hal Canary6f6961e2017-01-31 13:50:44 -0500588#endif
sugoi@google.com4775cba2013-04-17 13:46:56 +0000589
wangyix7c157a92015-07-22 15:08:53 -0700590void GrGLPerlinNoise::emitCode(EmitArgs& args) {
robertphillipsbf536af2016-02-04 06:11:53 -0800591 const GrPerlinNoiseEffect& pne = args.fFp.cast<GrPerlinNoiseEffect>();
592
cdalton85285412016-02-18 12:37:07 -0800593 GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder;
egdaniel7ea439b2015-12-03 09:20:44 -0800594 GrGLSLUniformHandler* uniformHandler = args.fUniformHandler;
bsalomon1a1aa932016-09-12 09:30:36 -0700595 SkString vCoords = fragBuilder->ensureCoords2D(args.fTransformedCoords[0]);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000596
cdalton5e58cee2016-02-11 12:49:47 -0800597 fBaseFrequencyUni = uniformHandler->addUniform(kFragment_GrShaderFlag,
egdaniel7ea439b2015-12-03 09:20:44 -0800598 kVec2f_GrSLType, kDefault_GrSLPrecision,
599 "baseFrequency");
600 const char* baseFrequencyUni = uniformHandler->getUniformCStr(fBaseFrequencyUni);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000601
halcanary96fcdcc2015-08-27 07:41:13 -0700602 const char* stitchDataUni = nullptr;
robertphillipsbf536af2016-02-04 06:11:53 -0800603 if (pne.stitchTiles()) {
cdalton5e58cee2016-02-11 12:49:47 -0800604 fStitchDataUni = uniformHandler->addUniform(kFragment_GrShaderFlag,
egdaniel7ea439b2015-12-03 09:20:44 -0800605 kVec2f_GrSLType, kDefault_GrSLPrecision,
606 "stitchData");
607 stitchDataUni = uniformHandler->getUniformCStr(fStitchDataUni);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000608 }
609
sugoi@google.comd537af52013-06-10 13:59:25 +0000610 // There are 4 lines, so the center of each line is 1/8, 3/8, 5/8 and 7/8
611 const char* chanCoordR = "0.125";
612 const char* chanCoordG = "0.375";
613 const char* chanCoordB = "0.625";
614 const char* chanCoordA = "0.875";
615 const char* chanCoord = "chanCoord";
sugoi@google.come3b4c502013-04-05 13:47:09 +0000616 const char* stitchData = "stitchData";
617 const char* ratio = "ratio";
sugoi@google.come3b4c502013-04-05 13:47:09 +0000618 const char* noiseVec = "noiseVec";
sugoi@google.come3b4c502013-04-05 13:47:09 +0000619 const char* noiseSmooth = "noiseSmooth";
senorblancoce6a3542014-06-12 11:24:19 -0700620 const char* floorVal = "floorVal";
sugoi@google.come3b4c502013-04-05 13:47:09 +0000621 const char* fractVal = "fractVal";
622 const char* uv = "uv";
623 const char* ab = "ab";
624 const char* latticeIdx = "latticeIdx";
senorblancoce6a3542014-06-12 11:24:19 -0700625 const char* bcoords = "bcoords";
sugoi@google.come3b4c502013-04-05 13:47:09 +0000626 const char* lattice = "lattice";
sugoi@google.come3b4c502013-04-05 13:47:09 +0000627 const char* inc8bit = "0.00390625"; // 1.0 / 256.0
628 // This is the math to convert the two 16bit integer packed into rgba 8 bit input into a
629 // [-1,1] vector and perform a dot product between that vector and the provided vector.
630 const char* dotLattice = "dot(((%s.ga + %s.rb * vec2(%s)) * vec2(2.0) - vec2(1.0)), %s);";
631
sugoi@google.comd537af52013-06-10 13:59:25 +0000632 // Add noise function
Brian Salomon99938a82016-11-21 13:41:08 -0500633 static const GrShaderVar gPerlinNoiseArgs[] = {
634 GrShaderVar(chanCoord, kFloat_GrSLType),
635 GrShaderVar(noiseVec, kVec2f_GrSLType)
sugoi@google.comd537af52013-06-10 13:59:25 +0000636 };
sugoi@google.come3b4c502013-04-05 13:47:09 +0000637
Brian Salomon99938a82016-11-21 13:41:08 -0500638 static const GrShaderVar gPerlinNoiseStitchArgs[] = {
639 GrShaderVar(chanCoord, kFloat_GrSLType),
640 GrShaderVar(noiseVec, kVec2f_GrSLType),
641 GrShaderVar(stitchData, kVec2f_GrSLType)
sugoi@google.comd537af52013-06-10 13:59:25 +0000642 };
sugoi@google.come3b4c502013-04-05 13:47:09 +0000643
sugoi@google.comd537af52013-06-10 13:59:25 +0000644 SkString noiseCode;
sugoi@google.come3b4c502013-04-05 13:47:09 +0000645
senorblancoce6a3542014-06-12 11:24:19 -0700646 noiseCode.appendf("\tvec4 %s;\n", floorVal);
647 noiseCode.appendf("\t%s.xy = floor(%s);\n", floorVal, noiseVec);
648 noiseCode.appendf("\t%s.zw = %s.xy + vec2(1.0);\n", floorVal, floorVal);
649 noiseCode.appendf("\tvec2 %s = fract(%s);\n", fractVal, noiseVec);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000650
651 // smooth curve : t * t * (3 - 2 * t)
senorblancoce6a3542014-06-12 11:24:19 -0700652 noiseCode.appendf("\n\tvec2 %s = %s * %s * (vec2(3.0) - vec2(2.0) * %s);",
653 noiseSmooth, fractVal, fractVal, fractVal);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000654
655 // Adjust frequencies if we're stitching tiles
robertphillipsbf536af2016-02-04 06:11:53 -0800656 if (pne.stitchTiles()) {
commit-bot@chromium.org98393202013-07-04 18:13:05 +0000657 noiseCode.appendf("\n\tif(%s.x >= %s.x) { %s.x -= %s.x; }",
egdaniel4ca2e602015-11-18 08:01:26 -0800658 floorVal, stitchData, floorVal, stitchData);
commit-bot@chromium.org98393202013-07-04 18:13:05 +0000659 noiseCode.appendf("\n\tif(%s.y >= %s.y) { %s.y -= %s.y; }",
egdaniel4ca2e602015-11-18 08:01:26 -0800660 floorVal, stitchData, floorVal, stitchData);
senorblancoce6a3542014-06-12 11:24:19 -0700661 noiseCode.appendf("\n\tif(%s.z >= %s.x) { %s.z -= %s.x; }",
egdaniel4ca2e602015-11-18 08:01:26 -0800662 floorVal, stitchData, floorVal, stitchData);
senorblancoce6a3542014-06-12 11:24:19 -0700663 noiseCode.appendf("\n\tif(%s.w >= %s.y) { %s.w -= %s.y; }",
egdaniel4ca2e602015-11-18 08:01:26 -0800664 floorVal, stitchData, floorVal, stitchData);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000665 }
666
667 // Get texture coordinates and normalize
senorblancoce6a3542014-06-12 11:24:19 -0700668 noiseCode.appendf("\n\t%s = fract(floor(mod(%s, 256.0)) / vec4(256.0));\n",
egdaniel4ca2e602015-11-18 08:01:26 -0800669 floorVal, floorVal);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000670
671 // Get permutation for x
672 {
673 SkString xCoords("");
senorblancoce6a3542014-06-12 11:24:19 -0700674 xCoords.appendf("vec2(%s.x, 0.5)", floorVal);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000675
sugoi@google.comd537af52013-06-10 13:59:25 +0000676 noiseCode.appendf("\n\tvec2 %s;\n\t%s.x = ", latticeIdx, latticeIdx);
cdalton3f6f76f2016-04-11 12:18:09 -0700677 fragBuilder->appendTextureLookup(&noiseCode, args.fTexSamplers[0], xCoords.c_str(),
egdaniel4ca2e602015-11-18 08:01:26 -0800678 kVec2f_GrSLType);
sugoi@google.comd537af52013-06-10 13:59:25 +0000679 noiseCode.append(".r;");
sugoi@google.come3b4c502013-04-05 13:47:09 +0000680 }
681
682 // Get permutation for x + 1
683 {
684 SkString xCoords("");
senorblancoce6a3542014-06-12 11:24:19 -0700685 xCoords.appendf("vec2(%s.z, 0.5)", floorVal);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000686
sugoi@google.comd537af52013-06-10 13:59:25 +0000687 noiseCode.appendf("\n\t%s.y = ", latticeIdx);
cdalton3f6f76f2016-04-11 12:18:09 -0700688 fragBuilder->appendTextureLookup(&noiseCode, args.fTexSamplers[0], xCoords.c_str(),
egdaniel4ca2e602015-11-18 08:01:26 -0800689 kVec2f_GrSLType);
sugoi@google.comd537af52013-06-10 13:59:25 +0000690 noiseCode.append(".r;");
sugoi@google.come3b4c502013-04-05 13:47:09 +0000691 }
692
commit-bot@chromium.org344cf452013-06-17 14:19:01 +0000693#if defined(SK_BUILD_FOR_ANDROID)
694 // Android rounding for Tegra devices, like, for example: Xoom (Tegra 2), Nexus 7 (Tegra 3).
695 // The issue is that colors aren't accurate enough on Tegra devices. For example, if an 8 bit
696 // value of 124 (or 0.486275 here) is entered, we can get a texture value of 123.513725
697 // (or 0.484368 here). The following rounding operation prevents these precision issues from
698 // affecting the result of the noise by making sure that we only have multiples of 1/255.
699 // (Note that 1/255 is about 0.003921569, which is the value used here).
700 noiseCode.appendf("\n\t%s = floor(%s * vec2(255.0) + vec2(0.5)) * vec2(0.003921569);",
701 latticeIdx, latticeIdx);
702#endif
703
sugoi@google.come3b4c502013-04-05 13:47:09 +0000704 // Get (x,y) coordinates with the permutated x
senorblancoce6a3542014-06-12 11:24:19 -0700705 noiseCode.appendf("\n\tvec4 %s = fract(%s.xyxy + %s.yyww);", bcoords, latticeIdx, floorVal);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000706
sugoi@google.comd537af52013-06-10 13:59:25 +0000707 noiseCode.appendf("\n\n\tvec2 %s;", uv);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000708 // Compute u, at offset (0,0)
709 {
710 SkString latticeCoords("");
senorblancoce6a3542014-06-12 11:24:19 -0700711 latticeCoords.appendf("vec2(%s.x, %s)", bcoords, chanCoord);
sugoi@google.comd537af52013-06-10 13:59:25 +0000712 noiseCode.appendf("\n\tvec4 %s = ", lattice);
cdalton3f6f76f2016-04-11 12:18:09 -0700713 fragBuilder->appendTextureLookup(&noiseCode, args.fTexSamplers[1], latticeCoords.c_str(),
egdaniel4ca2e602015-11-18 08:01:26 -0800714 kVec2f_GrSLType);
sugoi@google.comd537af52013-06-10 13:59:25 +0000715 noiseCode.appendf(".bgra;\n\t%s.x = ", uv);
716 noiseCode.appendf(dotLattice, lattice, lattice, inc8bit, fractVal);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000717 }
718
sugoi@google.comd537af52013-06-10 13:59:25 +0000719 noiseCode.appendf("\n\t%s.x -= 1.0;", fractVal);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000720 // Compute v, at offset (-1,0)
721 {
722 SkString latticeCoords("");
senorblancoce6a3542014-06-12 11:24:19 -0700723 latticeCoords.appendf("vec2(%s.y, %s)", bcoords, chanCoord);
commit-bot@chromium.org344cf452013-06-17 14:19:01 +0000724 noiseCode.append("\n\tlattice = ");
cdalton3f6f76f2016-04-11 12:18:09 -0700725 fragBuilder->appendTextureLookup(&noiseCode, args.fTexSamplers[1], latticeCoords.c_str(),
egdaniel4ca2e602015-11-18 08:01:26 -0800726 kVec2f_GrSLType);
sugoi@google.comd537af52013-06-10 13:59:25 +0000727 noiseCode.appendf(".bgra;\n\t%s.y = ", uv);
728 noiseCode.appendf(dotLattice, lattice, lattice, inc8bit, fractVal);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000729 }
730
731 // Compute 'a' as a linear interpolation of 'u' and 'v'
sugoi@google.comd537af52013-06-10 13:59:25 +0000732 noiseCode.appendf("\n\tvec2 %s;", ab);
733 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 +0000734
sugoi@google.comd537af52013-06-10 13:59:25 +0000735 noiseCode.appendf("\n\t%s.y -= 1.0;", fractVal);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000736 // Compute v, at offset (-1,-1)
737 {
738 SkString latticeCoords("");
senorblancoce6a3542014-06-12 11:24:19 -0700739 latticeCoords.appendf("vec2(%s.w, %s)", bcoords, chanCoord);
commit-bot@chromium.org344cf452013-06-17 14:19:01 +0000740 noiseCode.append("\n\tlattice = ");
cdalton3f6f76f2016-04-11 12:18:09 -0700741 fragBuilder->appendTextureLookup(&noiseCode, args.fTexSamplers[1], latticeCoords.c_str(),
egdaniel4ca2e602015-11-18 08:01:26 -0800742 kVec2f_GrSLType);
sugoi@google.comd537af52013-06-10 13:59:25 +0000743 noiseCode.appendf(".bgra;\n\t%s.y = ", uv);
744 noiseCode.appendf(dotLattice, lattice, lattice, inc8bit, fractVal);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000745 }
746
sugoi@google.comd537af52013-06-10 13:59:25 +0000747 noiseCode.appendf("\n\t%s.x += 1.0;", fractVal);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000748 // Compute u, at offset (0,-1)
749 {
750 SkString latticeCoords("");
senorblancoce6a3542014-06-12 11:24:19 -0700751 latticeCoords.appendf("vec2(%s.z, %s)", bcoords, chanCoord);
commit-bot@chromium.org344cf452013-06-17 14:19:01 +0000752 noiseCode.append("\n\tlattice = ");
cdalton3f6f76f2016-04-11 12:18:09 -0700753 fragBuilder->appendTextureLookup(&noiseCode, args.fTexSamplers[1], latticeCoords.c_str(),
egdaniel4ca2e602015-11-18 08:01:26 -0800754 kVec2f_GrSLType);
sugoi@google.comd537af52013-06-10 13:59:25 +0000755 noiseCode.appendf(".bgra;\n\t%s.x = ", uv);
756 noiseCode.appendf(dotLattice, lattice, lattice, inc8bit, fractVal);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000757 }
758
759 // Compute 'b' as a linear interpolation of 'u' and 'v'
sugoi@google.comd537af52013-06-10 13:59:25 +0000760 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 +0000761 // Compute the noise as a linear interpolation of 'a' and 'b'
sugoi@google.comd537af52013-06-10 13:59:25 +0000762 noiseCode.appendf("\n\treturn mix(%s.x, %s.y, %s.y);\n", ab, ab, noiseSmooth);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000763
sugoi@google.comd537af52013-06-10 13:59:25 +0000764 SkString noiseFuncName;
robertphillipsbf536af2016-02-04 06:11:53 -0800765 if (pne.stitchTiles()) {
egdaniel4ca2e602015-11-18 08:01:26 -0800766 fragBuilder->emitFunction(kFloat_GrSLType,
767 "perlinnoise", SK_ARRAY_COUNT(gPerlinNoiseStitchArgs),
768 gPerlinNoiseStitchArgs, noiseCode.c_str(), &noiseFuncName);
sugoi@google.comd537af52013-06-10 13:59:25 +0000769 } else {
egdaniel4ca2e602015-11-18 08:01:26 -0800770 fragBuilder->emitFunction(kFloat_GrSLType,
771 "perlinnoise", SK_ARRAY_COUNT(gPerlinNoiseArgs),
772 gPerlinNoiseArgs, noiseCode.c_str(), &noiseFuncName);
sugoi@google.comd537af52013-06-10 13:59:25 +0000773 }
sugoi@google.come3b4c502013-04-05 13:47:09 +0000774
sugoi@google.comd537af52013-06-10 13:59:25 +0000775 // There are rounding errors if the floor operation is not performed here
egdaniel4ca2e602015-11-18 08:01:26 -0800776 fragBuilder->codeAppendf("\n\t\tvec2 %s = floor(%s.xy) * %s;",
777 noiseVec, vCoords.c_str(), baseFrequencyUni);
sugoi@google.comd537af52013-06-10 13:59:25 +0000778
779 // Clear the color accumulator
egdaniel4ca2e602015-11-18 08:01:26 -0800780 fragBuilder->codeAppendf("\n\t\t%s = vec4(0.0);", args.fOutputColor);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000781
robertphillipsbf536af2016-02-04 06:11:53 -0800782 if (pne.stitchTiles()) {
sugoi@google.comd537af52013-06-10 13:59:25 +0000783 // Set up TurbulenceInitial stitch values.
robertphillipsbf536af2016-02-04 06:11:53 -0800784 fragBuilder->codeAppendf("vec2 %s = %s;", stitchData, stitchDataUni);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000785 }
sugoi@google.come3b4c502013-04-05 13:47:09 +0000786
robertphillipsbf536af2016-02-04 06:11:53 -0800787 fragBuilder->codeAppendf("float %s = 1.0;", ratio);
sugoi@google.comd537af52013-06-10 13:59:25 +0000788
789 // Loop over all octaves
robertphillipsbf536af2016-02-04 06:11:53 -0800790 fragBuilder->codeAppendf("for (int octave = 0; octave < %d; ++octave) {", pne.numOctaves());
sugoi@google.comd537af52013-06-10 13:59:25 +0000791
robertphillipsbf536af2016-02-04 06:11:53 -0800792 fragBuilder->codeAppendf("%s += ", args.fOutputColor);
793 if (pne.type() != SkPerlinNoiseShader::kFractalNoise_Type) {
egdaniel4ca2e602015-11-18 08:01:26 -0800794 fragBuilder->codeAppend("abs(");
sugoi@google.comd537af52013-06-10 13:59:25 +0000795 }
robertphillipsbf536af2016-02-04 06:11:53 -0800796 if (pne.stitchTiles()) {
egdaniel4ca2e602015-11-18 08:01:26 -0800797 fragBuilder->codeAppendf(
sugoi@google.comd537af52013-06-10 13:59:25 +0000798 "vec4(\n\t\t\t\t%s(%s, %s, %s),\n\t\t\t\t%s(%s, %s, %s),"
799 "\n\t\t\t\t%s(%s, %s, %s),\n\t\t\t\t%s(%s, %s, %s))",
800 noiseFuncName.c_str(), chanCoordR, noiseVec, stitchData,
801 noiseFuncName.c_str(), chanCoordG, noiseVec, stitchData,
802 noiseFuncName.c_str(), chanCoordB, noiseVec, stitchData,
803 noiseFuncName.c_str(), chanCoordA, noiseVec, stitchData);
804 } else {
egdaniel4ca2e602015-11-18 08:01:26 -0800805 fragBuilder->codeAppendf(
sugoi@google.comd537af52013-06-10 13:59:25 +0000806 "vec4(\n\t\t\t\t%s(%s, %s),\n\t\t\t\t%s(%s, %s),"
807 "\n\t\t\t\t%s(%s, %s),\n\t\t\t\t%s(%s, %s))",
808 noiseFuncName.c_str(), chanCoordR, noiseVec,
809 noiseFuncName.c_str(), chanCoordG, noiseVec,
810 noiseFuncName.c_str(), chanCoordB, noiseVec,
811 noiseFuncName.c_str(), chanCoordA, noiseVec);
812 }
robertphillipsbf536af2016-02-04 06:11:53 -0800813 if (pne.type() != SkPerlinNoiseShader::kFractalNoise_Type) {
egdaniel4ca2e602015-11-18 08:01:26 -0800814 fragBuilder->codeAppendf(")"); // end of "abs("
sugoi@google.comd537af52013-06-10 13:59:25 +0000815 }
egdaniel4ca2e602015-11-18 08:01:26 -0800816 fragBuilder->codeAppendf(" * %s;", ratio);
sugoi@google.comd537af52013-06-10 13:59:25 +0000817
egdaniel4ca2e602015-11-18 08:01:26 -0800818 fragBuilder->codeAppendf("\n\t\t\t%s *= vec2(2.0);", noiseVec);
819 fragBuilder->codeAppendf("\n\t\t\t%s *= 0.5;", ratio);
sugoi@google.comd537af52013-06-10 13:59:25 +0000820
robertphillipsbf536af2016-02-04 06:11:53 -0800821 if (pne.stitchTiles()) {
egdaniel4ca2e602015-11-18 08:01:26 -0800822 fragBuilder->codeAppendf("\n\t\t\t%s *= vec2(2.0);", stitchData);
sugoi@google.comd537af52013-06-10 13:59:25 +0000823 }
egdaniel4ca2e602015-11-18 08:01:26 -0800824 fragBuilder->codeAppend("\n\t\t}"); // end of the for loop on octaves
sugoi@google.come3b4c502013-04-05 13:47:09 +0000825
robertphillipsbf536af2016-02-04 06:11:53 -0800826 if (pne.type() == SkPerlinNoiseShader::kFractalNoise_Type) {
sugoi@google.come3b4c502013-04-05 13:47:09 +0000827 // The value of turbulenceFunctionResult comes from ((turbulenceFunctionResult) + 1) / 2
828 // by fractalNoise and (turbulenceFunctionResult) by turbulence.
egdaniel4ca2e602015-11-18 08:01:26 -0800829 fragBuilder->codeAppendf("\n\t\t%s = %s * vec4(0.5) + vec4(0.5);",
830 args.fOutputColor,args.fOutputColor);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000831 }
832
sugoi@google.come3b4c502013-04-05 13:47:09 +0000833 // Clamp values
egdaniel4ca2e602015-11-18 08:01:26 -0800834 fragBuilder->codeAppendf("\n\t\t%s = clamp(%s, 0.0, 1.0);", args.fOutputColor, args.fOutputColor);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000835
836 // Pre-multiply the result
egdaniel4ca2e602015-11-18 08:01:26 -0800837 fragBuilder->codeAppendf("\n\t\t%s = vec4(%s.rgb * %s.aaa, %s.a);\n",
838 args.fOutputColor, args.fOutputColor,
839 args.fOutputColor, args.fOutputColor);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000840}
841
Brian Salomon94efbf52016-11-29 13:43:05 -0500842void GrGLPerlinNoise::GenKey(const GrProcessor& processor, const GrShaderCaps&,
joshualittb0a8a372014-09-23 09:50:21 -0700843 GrProcessorKeyBuilder* b) {
844 const GrPerlinNoiseEffect& turbulence = processor.cast<GrPerlinNoiseEffect>();
sugoi@google.come3b4c502013-04-05 13:47:09 +0000845
bsalomon63e99f72014-07-21 08:03:14 -0700846 uint32_t key = turbulence.numOctaves();
sugoi@google.come3b4c502013-04-05 13:47:09 +0000847
848 key = key << 3; // Make room for next 3 bits
849
850 switch (turbulence.type()) {
851 case SkPerlinNoiseShader::kFractalNoise_Type:
852 key |= 0x1;
853 break;
854 case SkPerlinNoiseShader::kTurbulence_Type:
855 key |= 0x2;
856 break;
857 default:
858 // leave key at 0
859 break;
860 }
861
862 if (turbulence.stitchTiles()) {
863 key |= 0x4; // Flip the 3rd bit if tile stitching is on
864 }
865
bsalomon63e99f72014-07-21 08:03:14 -0700866 b->add32(key);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000867}
868
egdaniel018fb622015-10-28 07:26:40 -0700869void GrGLPerlinNoise::onSetData(const GrGLSLProgramDataManager& pdman,
870 const GrProcessor& processor) {
wangyixb1daa862015-08-18 11:29:31 -0700871 INHERITED::onSetData(pdman, processor);
senorblancof3b50272014-06-16 10:49:58 -0700872
joshualittb0a8a372014-09-23 09:50:21 -0700873 const GrPerlinNoiseEffect& turbulence = processor.cast<GrPerlinNoiseEffect>();
sugoi@google.come3b4c502013-04-05 13:47:09 +0000874
875 const SkVector& baseFrequency = turbulence.baseFrequency();
kkinnunen7510b222014-07-30 00:04:16 -0700876 pdman.set2f(fBaseFrequencyUni, baseFrequency.fX, baseFrequency.fY);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000877
sugoi@google.com4775cba2013-04-17 13:46:56 +0000878 if (turbulence.stitchTiles()) {
879 const SkPerlinNoiseShader::StitchData& stitchData = turbulence.stitchData();
kkinnunen7510b222014-07-30 00:04:16 -0700880 pdman.set2f(fStitchDataUni, SkIntToScalar(stitchData.fWidth),
commit-bot@chromium.org98393202013-07-04 18:13:05 +0000881 SkIntToScalar(stitchData.fHeight));
sugoi@google.com4775cba2013-04-17 13:46:56 +0000882 }
883}
884
sugoi@google.come3b4c502013-04-05 13:47:09 +0000885/////////////////////////////////////////////////////////////////////
brianosman839345d2016-07-22 11:04:53 -0700886sk_sp<GrFragmentProcessor> SkPerlinNoiseShader::asFragmentProcessor(const AsFPArgs& args) const {
887 SkASSERT(args.fContext);
mtklein3f3b3d02014-12-01 11:47:08 -0800888
commit-bot@chromium.org96fb7482014-05-09 20:28:11 +0000889 SkMatrix localMatrix = this->getLocalMatrix();
brianosman839345d2016-07-22 11:04:53 -0700890 if (args.fLocalMatrix) {
891 localMatrix.preConcat(*args.fLocalMatrix);
commit-bot@chromium.org96fb7482014-05-09 20:28:11 +0000892 }
893
brianosman839345d2016-07-22 11:04:53 -0700894 SkMatrix matrix = *args.fViewMatrix;
senorblancoca6a7c22014-06-27 13:35:52 -0700895 matrix.preConcat(localMatrix);
896
commit-bot@chromium.orgc2a0ea62013-11-06 10:08:38 +0000897 if (0 == fNumOctaves) {
commit-bot@chromium.orgc2a0ea62013-11-06 10:08:38 +0000898 if (kFractalNoise_Type == fType) {
bsalomonc21b09e2015-08-28 18:46:56 -0700899 // Extract the incoming alpha and emit rgba = (a/4, a/4, a/4, a/2)
Brian Osman618d3042016-10-25 10:51:28 -0400900 // TODO: Either treat the output of this shader as sRGB or allow client to specify a
901 // color space of the noise. Either way, this case (and the GLSL) need to convert to
902 // the destination.
bungeman06ca8ec2016-06-09 08:01:03 -0700903 sk_sp<GrFragmentProcessor> inner(
Brian Osman618d3042016-10-25 10:51:28 -0400904 GrConstColorProcessor::Make(GrColor4f::FromGrColor(0x80404040),
bungeman06ca8ec2016-06-09 08:01:03 -0700905 GrConstColorProcessor::kModulateRGBA_InputMode));
906 return GrFragmentProcessor::MulOutputByInputAlpha(std::move(inner));
reedcff10b22015-03-03 06:41:45 -0800907 }
bsalomonc21b09e2015-08-28 18:46:56 -0700908 // Emit zero.
Brian Osman618d3042016-10-25 10:51:28 -0400909 return GrConstColorProcessor::Make(GrColor4f::TransparentBlack(),
910 GrConstColorProcessor::kIgnore_InputMode);
commit-bot@chromium.orgc2a0ea62013-11-06 10:08:38 +0000911 }
912
sugoi@google.come3b4c502013-04-05 13:47:09 +0000913 // Either we don't stitch tiles, either we have a valid tile size
914 SkASSERT(!fStitchTiles || !fTileSize.isEmpty());
915
joshualittb0a8a372014-09-23 09:50:21 -0700916 SkPerlinNoiseShader::PaintingData* paintingData =
halcanary385fe4d2015-08-26 13:07:48 -0700917 new PaintingData(fTileSize, fSeed, fBaseFrequencyX, fBaseFrequencyY, matrix);
Robert Phillips6f9f7eb2017-02-18 15:15:51 -0500918 sk_sp<GrTextureProxy> permutationsProxy(GrMakeCachedBitmapProxy(
919 args.fContext,
920 paintingData->getPermutationsBitmap()));
921 sk_sp<GrTextureProxy> noiseProxy(GrMakeCachedBitmapProxy(args.fContext,
922 paintingData->getNoiseBitmap()));
sugoi@google.come3b4c502013-04-05 13:47:09 +0000923
brianosman839345d2016-07-22 11:04:53 -0700924 SkMatrix m = *args.fViewMatrix;
senorblancoca6a7c22014-06-27 13:35:52 -0700925 m.setTranslateX(-localMatrix.getTranslateX() + SK_Scalar1);
926 m.setTranslateY(-localMatrix.getTranslateY() + SK_Scalar1);
Robert Phillips6f9f7eb2017-02-18 15:15:51 -0500927 if (permutationsProxy && noiseProxy) {
bungeman06ca8ec2016-06-09 08:01:03 -0700928 sk_sp<GrFragmentProcessor> inner(
Brian Osman32342f02017-03-04 08:12:46 -0500929 GrPerlinNoiseEffect::Make(args.fContext->resourceProvider(),
Robert Phillips6f9f7eb2017-02-18 15:15:51 -0500930 fType,
bungeman06ca8ec2016-06-09 08:01:03 -0700931 fNumOctaves,
932 fStitchTiles,
933 paintingData,
Robert Phillips6f9f7eb2017-02-18 15:15:51 -0500934 std::move(permutationsProxy),
935 std::move(noiseProxy),
bungeman06ca8ec2016-06-09 08:01:03 -0700936 m));
937 return GrFragmentProcessor::MulOutputByInputAlpha(std::move(inner));
senorblancoca6a7c22014-06-27 13:35:52 -0700938 }
bsalomonc21b09e2015-08-28 18:46:56 -0700939 delete paintingData;
940 return nullptr;
sugoi@google.come3b4c502013-04-05 13:47:09 +0000941}
942
943#endif
944
commit-bot@chromium.org0f10f7b2014-03-13 18:02:17 +0000945#ifndef SK_IGNORE_TO_STRING
sugoi@google.come3b4c502013-04-05 13:47:09 +0000946void SkPerlinNoiseShader::toString(SkString* str) const {
947 str->append("SkPerlinNoiseShader: (");
948
949 str->append("type: ");
950 switch (fType) {
951 case kFractalNoise_Type:
952 str->append("\"fractal noise\"");
953 break;
954 case kTurbulence_Type:
955 str->append("\"turbulence\"");
956 break;
957 default:
958 str->append("\"unknown\"");
959 break;
960 }
961 str->append(" base frequency: (");
962 str->appendScalar(fBaseFrequencyX);
963 str->append(", ");
964 str->appendScalar(fBaseFrequencyY);
965 str->append(") number of octaves: ");
966 str->appendS32(fNumOctaves);
967 str->append(" seed: ");
968 str->appendScalar(fSeed);
969 str->append(" stitch tiles: ");
970 str->append(fStitchTiles ? "true " : "false ");
971
972 this->INHERITED::toString(str);
973
974 str->append(")");
975}
976#endif