blob: 0247ed9e1b56487a9537cb612c73794117348cd9 [file] [log] [blame]
sugoi@google.come3b4c502013-04-05 13:47:09 +00001/*
2 * Copyright 2013 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8#include "SkDither.h"
9#include "SkPerlinNoiseShader.h"
commit-bot@chromium.orgc2a0ea62013-11-06 10:08:38 +000010#include "SkColorFilter.h"
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +000011#include "SkReadBuffer.h"
12#include "SkWriteBuffer.h"
sugoi@google.come3b4c502013-04-05 13:47:09 +000013#include "SkShader.h"
14#include "SkUnPreMultiply.h"
15#include "SkString.h"
16
17#if SK_SUPPORT_GPU
18#include "GrContext.h"
bsalomon@google.com77af6802013-10-02 13:04:56 +000019#include "GrCoordTransform.h"
sugoi@google.come3b4c502013-04-05 13:47:09 +000020#include "gl/GrGLEffect.h"
joshualitt30ba4362014-08-21 20:18:45 -070021#include "gl/builders/GrGLProgramBuilder.h"
sugoi@google.come3b4c502013-04-05 13:47:09 +000022#include "GrTBackendEffectFactory.h"
23#include "SkGr.h"
24#endif
25
26static const int kBlockSize = 256;
27static const int kBlockMask = kBlockSize - 1;
28static const int kPerlinNoise = 4096;
29static const int kRandMaximum = SK_MaxS32; // 2**31 - 1
30
31namespace {
32
33// noiseValue is the color component's value (or color)
34// limitValue is the maximum perlin noise array index value allowed
35// newValue is the current noise dimension (either width or height)
36inline int checkNoise(int noiseValue, int limitValue, int newValue) {
37 // If the noise value would bring us out of bounds of the current noise array while we are
38 // stiching noise tiles together, wrap the noise around the current dimension of the noise to
39 // stay within the array bounds in a continuous fashion (so that tiling lines are not visible)
40 if (noiseValue >= limitValue) {
41 noiseValue -= newValue;
42 }
sugoi@google.come3b4c502013-04-05 13:47:09 +000043 return noiseValue;
44}
45
46inline SkScalar smoothCurve(SkScalar t) {
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +000047 static const SkScalar SK_Scalar3 = 3.0f;
sugoi@google.come3b4c502013-04-05 13:47:09 +000048
49 // returns t * t * (3 - 2 * t)
50 return SkScalarMul(SkScalarSquare(t), SK_Scalar3 - 2 * t);
51}
52
commit-bot@chromium.orgce33d602013-11-25 21:46:31 +000053bool perlin_noise_type_is_valid(SkPerlinNoiseShader::Type type) {
54 return (SkPerlinNoiseShader::kFractalNoise_Type == type) ||
55 (SkPerlinNoiseShader::kTurbulence_Type == type);
56}
57
sugoi@google.come3b4c502013-04-05 13:47:09 +000058} // end namespace
59
60struct SkPerlinNoiseShader::StitchData {
61 StitchData()
62 : fWidth(0)
63 , fWrapX(0)
64 , fHeight(0)
65 , fWrapY(0)
66 {}
67
68 bool operator==(const StitchData& other) const {
69 return fWidth == other.fWidth &&
70 fWrapX == other.fWrapX &&
71 fHeight == other.fHeight &&
72 fWrapY == other.fWrapY;
73 }
74
75 int fWidth; // How much to subtract to wrap for stitching.
76 int fWrapX; // Minimum value to wrap.
77 int fHeight;
78 int fWrapY;
79};
80
81struct SkPerlinNoiseShader::PaintingData {
commit-bot@chromium.orgfd5c9a62014-03-06 15:13:53 +000082 PaintingData(const SkISize& tileSize, SkScalar seed,
senorblancoca6a7c22014-06-27 13:35:52 -070083 SkScalar baseFrequencyX, SkScalar baseFrequencyY,
84 const SkMatrix& matrix)
sugoi@google.come3b4c502013-04-05 13:47:09 +000085 {
senorblancoca6a7c22014-06-27 13:35:52 -070086 SkVector wavelength = SkVector::Make(SkScalarInvert(baseFrequencyX),
87 SkScalarInvert(baseFrequencyY));
88 matrix.mapVectors(&wavelength, 1);
89 fBaseFrequency.fX = SkScalarInvert(wavelength.fX);
90 fBaseFrequency.fY = SkScalarInvert(wavelength.fY);
91 SkVector sizeVec = SkVector::Make(SkIntToScalar(tileSize.fWidth),
92 SkIntToScalar(tileSize.fHeight));
93 matrix.mapVectors(&sizeVec, 1);
94 fTileSize.fWidth = SkScalarRoundToInt(sizeVec.fX);
95 fTileSize.fHeight = SkScalarRoundToInt(sizeVec.fY);
commit-bot@chromium.orgfd5c9a62014-03-06 15:13:53 +000096 this->init(seed);
97 if (!fTileSize.isEmpty()) {
98 this->stitch();
99 }
100
senorblancof3b50272014-06-16 10:49:58 -0700101#if SK_SUPPORT_GPU
commit-bot@chromium.orga3264e52014-05-30 13:26:10 +0000102 fPermutationsBitmap.setInfo(SkImageInfo::MakeA8(kBlockSize, 1));
commit-bot@chromium.orgfd5c9a62014-03-06 15:13:53 +0000103 fPermutationsBitmap.setPixels(fLatticeSelector);
104
commit-bot@chromium.orga3264e52014-05-30 13:26:10 +0000105 fNoiseBitmap.setInfo(SkImageInfo::MakeN32Premul(kBlockSize, 4));
commit-bot@chromium.orgfd5c9a62014-03-06 15:13:53 +0000106 fNoiseBitmap.setPixels(fNoise[0][0]);
107#endif
sugoi@google.come3b4c502013-04-05 13:47:09 +0000108 }
109
110 int fSeed;
111 uint8_t fLatticeSelector[kBlockSize];
112 uint16_t fNoise[4][kBlockSize][2];
113 SkPoint fGradient[4][kBlockSize];
114 SkISize fTileSize;
115 SkVector fBaseFrequency;
116 StitchData fStitchDataInit;
117
118private:
119
senorblancof3b50272014-06-16 10:49:58 -0700120#if SK_SUPPORT_GPU
commit-bot@chromium.orgfd5c9a62014-03-06 15:13:53 +0000121 SkBitmap fPermutationsBitmap;
122 SkBitmap fNoiseBitmap;
123#endif
sugoi@google.come3b4c502013-04-05 13:47:09 +0000124
125 inline int random() {
126 static const int gRandAmplitude = 16807; // 7**5; primitive root of m
127 static const int gRandQ = 127773; // m / a
128 static const int gRandR = 2836; // m % a
129
130 int result = gRandAmplitude * (fSeed % gRandQ) - gRandR * (fSeed / gRandQ);
131 if (result <= 0)
132 result += kRandMaximum;
133 fSeed = result;
134 return result;
135 }
136
commit-bot@chromium.orgfd5c9a62014-03-06 15:13:53 +0000137 // Only called once. Could be part of the constructor.
sugoi@google.come3b4c502013-04-05 13:47:09 +0000138 void init(SkScalar seed)
139 {
140 static const SkScalar gInvBlockSizef = SkScalarInvert(SkIntToScalar(kBlockSize));
141
senorblanco@chromium.org857e3202014-01-09 17:41:42 +0000142 // According to the SVG spec, we must truncate (not round) the seed value.
143 fSeed = SkScalarTruncToInt(seed);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000144 // The seed value clamp to the range [1, kRandMaximum - 1].
sugoi@google.come3b4c502013-04-05 13:47:09 +0000145 if (fSeed <= 0) {
146 fSeed = -(fSeed % (kRandMaximum - 1)) + 1;
147 }
148 if (fSeed > kRandMaximum - 1) {
149 fSeed = kRandMaximum - 1;
150 }
151 for (int channel = 0; channel < 4; ++channel) {
152 for (int i = 0; i < kBlockSize; ++i) {
153 fLatticeSelector[i] = i;
154 fNoise[channel][i][0] = (random() % (2 * kBlockSize));
155 fNoise[channel][i][1] = (random() % (2 * kBlockSize));
156 }
157 }
158 for (int i = kBlockSize - 1; i > 0; --i) {
159 int k = fLatticeSelector[i];
160 int j = random() % kBlockSize;
161 SkASSERT(j >= 0);
162 SkASSERT(j < kBlockSize);
163 fLatticeSelector[i] = fLatticeSelector[j];
164 fLatticeSelector[j] = k;
165 }
166
167 // Perform the permutations now
168 {
169 // Copy noise data
170 uint16_t noise[4][kBlockSize][2];
171 for (int i = 0; i < kBlockSize; ++i) {
172 for (int channel = 0; channel < 4; ++channel) {
173 for (int j = 0; j < 2; ++j) {
174 noise[channel][i][j] = fNoise[channel][i][j];
175 }
176 }
177 }
178 // Do permutations on noise data
179 for (int i = 0; i < kBlockSize; ++i) {
180 for (int channel = 0; channel < 4; ++channel) {
181 for (int j = 0; j < 2; ++j) {
182 fNoise[channel][i][j] = noise[channel][fLatticeSelector[i]][j];
183 }
184 }
185 }
186 }
187
188 // Half of the largest possible value for 16 bit unsigned int
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +0000189 static const SkScalar gHalfMax16bits = 32767.5f;
sugoi@google.come3b4c502013-04-05 13:47:09 +0000190
191 // Compute gradients from permutated noise data
192 for (int channel = 0; channel < 4; ++channel) {
193 for (int i = 0; i < kBlockSize; ++i) {
194 fGradient[channel][i] = SkPoint::Make(
195 SkScalarMul(SkIntToScalar(fNoise[channel][i][0] - kBlockSize),
196 gInvBlockSizef),
skia.committer@gmail.comcff02432013-04-06 07:01:10 +0000197 SkScalarMul(SkIntToScalar(fNoise[channel][i][1] - kBlockSize),
sugoi@google.come3b4c502013-04-05 13:47:09 +0000198 gInvBlockSizef));
199 fGradient[channel][i].normalize();
200 // Put the normalized gradient back into the noise data
201 fNoise[channel][i][0] = SkScalarRoundToInt(SkScalarMul(
sugoi@google.comd537af52013-06-10 13:59:25 +0000202 fGradient[channel][i].fX + SK_Scalar1, gHalfMax16bits));
sugoi@google.come3b4c502013-04-05 13:47:09 +0000203 fNoise[channel][i][1] = SkScalarRoundToInt(SkScalarMul(
sugoi@google.comd537af52013-06-10 13:59:25 +0000204 fGradient[channel][i].fY + SK_Scalar1, gHalfMax16bits));
sugoi@google.come3b4c502013-04-05 13:47:09 +0000205 }
206 }
sugoi@google.come3b4c502013-04-05 13:47:09 +0000207 }
208
commit-bot@chromium.orgfd5c9a62014-03-06 15:13:53 +0000209 // Only called once. Could be part of the constructor.
sugoi@google.come3b4c502013-04-05 13:47:09 +0000210 void stitch() {
211 SkScalar tileWidth = SkIntToScalar(fTileSize.width());
212 SkScalar tileHeight = SkIntToScalar(fTileSize.height());
213 SkASSERT(tileWidth > 0 && tileHeight > 0);
214 // When stitching tiled turbulence, the frequencies must be adjusted
215 // so that the tile borders will be continuous.
216 if (fBaseFrequency.fX) {
reed@google.com8015cdd2013-12-18 15:49:32 +0000217 SkScalar lowFrequencx =
218 SkScalarFloorToScalar(tileWidth * fBaseFrequency.fX) / tileWidth;
219 SkScalar highFrequencx =
220 SkScalarCeilToScalar(tileWidth * fBaseFrequency.fX) / tileWidth;
sugoi@google.come3b4c502013-04-05 13:47:09 +0000221 // BaseFrequency should be non-negative according to the standard.
skia.committer@gmail.comcff02432013-04-06 07:01:10 +0000222 if (SkScalarDiv(fBaseFrequency.fX, lowFrequencx) <
sugoi@google.come3b4c502013-04-05 13:47:09 +0000223 SkScalarDiv(highFrequencx, fBaseFrequency.fX)) {
224 fBaseFrequency.fX = lowFrequencx;
225 } else {
226 fBaseFrequency.fX = highFrequencx;
227 }
228 }
229 if (fBaseFrequency.fY) {
reed@google.com8015cdd2013-12-18 15:49:32 +0000230 SkScalar lowFrequency =
231 SkScalarFloorToScalar(tileHeight * fBaseFrequency.fY) / tileHeight;
232 SkScalar highFrequency =
233 SkScalarCeilToScalar(tileHeight * fBaseFrequency.fY) / tileHeight;
skia.committer@gmail.comcff02432013-04-06 07:01:10 +0000234 if (SkScalarDiv(fBaseFrequency.fY, lowFrequency) <
sugoi@google.come3b4c502013-04-05 13:47:09 +0000235 SkScalarDiv(highFrequency, fBaseFrequency.fY)) {
236 fBaseFrequency.fY = lowFrequency;
237 } else {
238 fBaseFrequency.fY = highFrequency;
239 }
240 }
241 // Set up TurbulenceInitial stitch values.
242 fStitchDataInit.fWidth =
reed@google.com8015cdd2013-12-18 15:49:32 +0000243 SkScalarRoundToInt(tileWidth * fBaseFrequency.fX);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000244 fStitchDataInit.fWrapX = kPerlinNoise + fStitchDataInit.fWidth;
245 fStitchDataInit.fHeight =
reed@google.com8015cdd2013-12-18 15:49:32 +0000246 SkScalarRoundToInt(tileHeight * fBaseFrequency.fY);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000247 fStitchDataInit.fWrapY = kPerlinNoise + fStitchDataInit.fHeight;
248 }
249
commit-bot@chromium.orgfd5c9a62014-03-06 15:13:53 +0000250public:
sugoi@google.come3b4c502013-04-05 13:47:09 +0000251
senorblancof3b50272014-06-16 10:49:58 -0700252#if SK_SUPPORT_GPU
commit-bot@chromium.orgfd5c9a62014-03-06 15:13:53 +0000253 const SkBitmap& getPermutationsBitmap() const { return fPermutationsBitmap; }
254
255 const SkBitmap& getNoiseBitmap() const { return fNoiseBitmap; }
256#endif
sugoi@google.come3b4c502013-04-05 13:47:09 +0000257};
258
259SkShader* SkPerlinNoiseShader::CreateFractalNoise(SkScalar baseFrequencyX, SkScalar baseFrequencyY,
260 int numOctaves, SkScalar seed,
261 const SkISize* tileSize) {
262 return SkNEW_ARGS(SkPerlinNoiseShader, (kFractalNoise_Type, baseFrequencyX, baseFrequencyY,
263 numOctaves, seed, tileSize));
264}
265
commit-bot@chromium.org9fbbcca2014-04-01 16:09:37 +0000266SkShader* SkPerlinNoiseShader::CreateTurbulence(SkScalar baseFrequencyX, SkScalar baseFrequencyY,
sugoi@google.come3b4c502013-04-05 13:47:09 +0000267 int numOctaves, SkScalar seed,
268 const SkISize* tileSize) {
269 return SkNEW_ARGS(SkPerlinNoiseShader, (kTurbulence_Type, baseFrequencyX, baseFrequencyY,
270 numOctaves, seed, tileSize));
271}
272
273SkPerlinNoiseShader::SkPerlinNoiseShader(SkPerlinNoiseShader::Type type,
274 SkScalar baseFrequencyX,
275 SkScalar baseFrequencyY,
276 int numOctaves,
277 SkScalar seed,
278 const SkISize* tileSize)
279 : fType(type)
280 , fBaseFrequencyX(baseFrequencyX)
281 , fBaseFrequencyY(baseFrequencyY)
commit-bot@chromium.orgce33d602013-11-25 21:46:31 +0000282 , fNumOctaves(numOctaves > 255 ? 255 : numOctaves/*[0,255] octaves allowed*/)
sugoi@google.come3b4c502013-04-05 13:47:09 +0000283 , fSeed(seed)
commit-bot@chromium.orgfd5c9a62014-03-06 15:13:53 +0000284 , fTileSize(NULL == tileSize ? SkISize::Make(0, 0) : *tileSize)
285 , fStitchTiles(!fTileSize.isEmpty())
sugoi@google.come3b4c502013-04-05 13:47:09 +0000286{
287 SkASSERT(numOctaves >= 0 && numOctaves < 256);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000288}
289
reed9fa60da2014-08-21 07:59:51 -0700290#ifdef SK_SUPPORT_LEGACY_DEEPFLATTENING
291SkPerlinNoiseShader::SkPerlinNoiseShader(SkReadBuffer& buffer) : INHERITED(buffer) {
sugoi@google.come3b4c502013-04-05 13:47:09 +0000292 fType = (SkPerlinNoiseShader::Type) buffer.readInt();
293 fBaseFrequencyX = buffer.readScalar();
294 fBaseFrequencyY = buffer.readScalar();
295 fNumOctaves = buffer.readInt();
296 fSeed = buffer.readScalar();
297 fStitchTiles = buffer.readBool();
298 fTileSize.fWidth = buffer.readInt();
299 fTileSize.fHeight = buffer.readInt();
commit-bot@chromium.orgce33d602013-11-25 21:46:31 +0000300 buffer.validate(perlin_noise_type_is_valid(fType) &&
commit-bot@chromium.orgfd5c9a62014-03-06 15:13:53 +0000301 (fNumOctaves >= 0) && (fNumOctaves <= 255) &&
302 (fStitchTiles != fTileSize.isEmpty()));
sugoi@google.come3b4c502013-04-05 13:47:09 +0000303}
reed9fa60da2014-08-21 07:59:51 -0700304#endif
sugoi@google.come3b4c502013-04-05 13:47:09 +0000305
306SkPerlinNoiseShader::~SkPerlinNoiseShader() {
sugoi@google.come3b4c502013-04-05 13:47:09 +0000307}
308
reed9fa60da2014-08-21 07:59:51 -0700309SkFlattenable* SkPerlinNoiseShader::CreateProc(SkReadBuffer& buffer) {
310 Type type = (Type)buffer.readInt();
311 SkScalar freqX = buffer.readScalar();
312 SkScalar freqY = buffer.readScalar();
313 int octaves = buffer.readInt();
314 SkScalar seed = buffer.readScalar();
315 SkISize tileSize;
316 tileSize.fWidth = buffer.readInt();
317 tileSize.fHeight = buffer.readInt();
318
319 switch (type) {
320 case kFractalNoise_Type:
321 return SkPerlinNoiseShader::CreateFractalNoise(freqX, freqY, octaves, seed, &tileSize);
322 case kTurbulence_Type:
323 return SkPerlinNoiseShader::CreateTubulence(freqX, freqY, octaves, seed, &tileSize);
324 default:
325 return NULL;
326 }
327}
328
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +0000329void SkPerlinNoiseShader::flatten(SkWriteBuffer& buffer) const {
sugoi@google.come3b4c502013-04-05 13:47:09 +0000330 buffer.writeInt((int) fType);
331 buffer.writeScalar(fBaseFrequencyX);
332 buffer.writeScalar(fBaseFrequencyY);
333 buffer.writeInt(fNumOctaves);
334 buffer.writeScalar(fSeed);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000335 buffer.writeInt(fTileSize.fWidth);
336 buffer.writeInt(fTileSize.fHeight);
337}
338
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000339SkScalar SkPerlinNoiseShader::PerlinNoiseShaderContext::noise2D(
senorblancoca6a7c22014-06-27 13:35:52 -0700340 int channel, const StitchData& stitchData, const SkPoint& noiseVector) const {
sugoi@google.come3b4c502013-04-05 13:47:09 +0000341 struct Noise {
342 int noisePositionIntegerValue;
senorblancoce6a3542014-06-12 11:24:19 -0700343 int nextNoisePositionIntegerValue;
sugoi@google.come3b4c502013-04-05 13:47:09 +0000344 SkScalar noisePositionFractionValue;
345 Noise(SkScalar component)
346 {
347 SkScalar position = component + kPerlinNoise;
348 noisePositionIntegerValue = SkScalarFloorToInt(position);
349 noisePositionFractionValue = position - SkIntToScalar(noisePositionIntegerValue);
senorblancoce6a3542014-06-12 11:24:19 -0700350 nextNoisePositionIntegerValue = noisePositionIntegerValue + 1;
sugoi@google.come3b4c502013-04-05 13:47:09 +0000351 }
352 };
353 Noise noiseX(noiseVector.x());
354 Noise noiseY(noiseVector.y());
355 SkScalar u, v;
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000356 const SkPerlinNoiseShader& perlinNoiseShader = static_cast<const SkPerlinNoiseShader&>(fShader);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000357 // If stitching, adjust lattice points accordingly.
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000358 if (perlinNoiseShader.fStitchTiles) {
sugoi@google.come3b4c502013-04-05 13:47:09 +0000359 noiseX.noisePositionIntegerValue =
360 checkNoise(noiseX.noisePositionIntegerValue, stitchData.fWrapX, stitchData.fWidth);
361 noiseY.noisePositionIntegerValue =
362 checkNoise(noiseY.noisePositionIntegerValue, stitchData.fWrapY, stitchData.fHeight);
senorblancoce6a3542014-06-12 11:24:19 -0700363 noiseX.nextNoisePositionIntegerValue =
364 checkNoise(noiseX.nextNoisePositionIntegerValue, stitchData.fWrapX, stitchData.fWidth);
365 noiseY.nextNoisePositionIntegerValue =
366 checkNoise(noiseY.nextNoisePositionIntegerValue, stitchData.fWrapY, stitchData.fHeight);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000367 }
368 noiseX.noisePositionIntegerValue &= kBlockMask;
369 noiseY.noisePositionIntegerValue &= kBlockMask;
senorblancoce6a3542014-06-12 11:24:19 -0700370 noiseX.nextNoisePositionIntegerValue &= kBlockMask;
371 noiseY.nextNoisePositionIntegerValue &= kBlockMask;
372 int i =
senorblancoca6a7c22014-06-27 13:35:52 -0700373 fPaintingData->fLatticeSelector[noiseX.noisePositionIntegerValue];
senorblancoce6a3542014-06-12 11:24:19 -0700374 int j =
senorblancoca6a7c22014-06-27 13:35:52 -0700375 fPaintingData->fLatticeSelector[noiseX.nextNoisePositionIntegerValue];
senorblancoce6a3542014-06-12 11:24:19 -0700376 int b00 = (i + noiseY.noisePositionIntegerValue) & kBlockMask;
377 int b10 = (j + noiseY.noisePositionIntegerValue) & kBlockMask;
378 int b01 = (i + noiseY.nextNoisePositionIntegerValue) & kBlockMask;
379 int b11 = (j + noiseY.nextNoisePositionIntegerValue) & kBlockMask;
sugoi@google.come3b4c502013-04-05 13:47:09 +0000380 SkScalar sx = smoothCurve(noiseX.noisePositionFractionValue);
381 SkScalar sy = smoothCurve(noiseY.noisePositionFractionValue);
382 // This is taken 1:1 from SVG spec: http://www.w3.org/TR/SVG11/filters.html#feTurbulenceElement
383 SkPoint fractionValue = SkPoint::Make(noiseX.noisePositionFractionValue,
384 noiseY.noisePositionFractionValue); // Offset (0,0)
senorblancoca6a7c22014-06-27 13:35:52 -0700385 u = fPaintingData->fGradient[channel][b00].dot(fractionValue);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000386 fractionValue.fX -= SK_Scalar1; // Offset (-1,0)
senorblancoca6a7c22014-06-27 13:35:52 -0700387 v = fPaintingData->fGradient[channel][b10].dot(fractionValue);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000388 SkScalar a = SkScalarInterp(u, v, sx);
389 fractionValue.fY -= SK_Scalar1; // Offset (-1,-1)
senorblancoca6a7c22014-06-27 13:35:52 -0700390 v = fPaintingData->fGradient[channel][b11].dot(fractionValue);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000391 fractionValue.fX = noiseX.noisePositionFractionValue; // Offset (0,-1)
senorblancoca6a7c22014-06-27 13:35:52 -0700392 u = fPaintingData->fGradient[channel][b01].dot(fractionValue);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000393 SkScalar b = SkScalarInterp(u, v, sx);
394 return SkScalarInterp(a, b, sy);
395}
396
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000397SkScalar SkPerlinNoiseShader::PerlinNoiseShaderContext::calculateTurbulenceValueForPoint(
senorblancoca6a7c22014-06-27 13:35:52 -0700398 int channel, StitchData& stitchData, const SkPoint& point) const {
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000399 const SkPerlinNoiseShader& perlinNoiseShader = static_cast<const SkPerlinNoiseShader&>(fShader);
400 if (perlinNoiseShader.fStitchTiles) {
sugoi@google.come3b4c502013-04-05 13:47:09 +0000401 // Set up TurbulenceInitial stitch values.
senorblancoca6a7c22014-06-27 13:35:52 -0700402 stitchData = fPaintingData->fStitchDataInit;
sugoi@google.come3b4c502013-04-05 13:47:09 +0000403 }
404 SkScalar turbulenceFunctionResult = 0;
senorblancoca6a7c22014-06-27 13:35:52 -0700405 SkPoint noiseVector(SkPoint::Make(SkScalarMul(point.x(), fPaintingData->fBaseFrequency.fX),
406 SkScalarMul(point.y(), fPaintingData->fBaseFrequency.fY)));
sugoi@google.come3b4c502013-04-05 13:47:09 +0000407 SkScalar ratio = SK_Scalar1;
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000408 for (int octave = 0; octave < perlinNoiseShader.fNumOctaves; ++octave) {
senorblancoca6a7c22014-06-27 13:35:52 -0700409 SkScalar noise = noise2D(channel, stitchData, noiseVector);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000410 turbulenceFunctionResult += SkScalarDiv(
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000411 (perlinNoiseShader.fType == kFractalNoise_Type) ? noise : SkScalarAbs(noise), ratio);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000412 noiseVector.fX *= 2;
413 noiseVector.fY *= 2;
414 ratio *= 2;
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000415 if (perlinNoiseShader.fStitchTiles) {
sugoi@google.come3b4c502013-04-05 13:47:09 +0000416 // Update stitch values
417 stitchData.fWidth *= 2;
418 stitchData.fWrapX = stitchData.fWidth + kPerlinNoise;
419 stitchData.fHeight *= 2;
420 stitchData.fWrapY = stitchData.fHeight + kPerlinNoise;
421 }
422 }
423
424 // The value of turbulenceFunctionResult comes from ((turbulenceFunctionResult) + 1) / 2
425 // by fractalNoise and (turbulenceFunctionResult) by turbulence.
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000426 if (perlinNoiseShader.fType == kFractalNoise_Type) {
sugoi@google.come3b4c502013-04-05 13:47:09 +0000427 turbulenceFunctionResult =
428 SkScalarMul(turbulenceFunctionResult, SK_ScalarHalf) + SK_ScalarHalf;
429 }
430
431 if (channel == 3) { // Scale alpha by paint value
432 turbulenceFunctionResult = SkScalarMul(turbulenceFunctionResult,
433 SkScalarDiv(SkIntToScalar(getPaintAlpha()), SkIntToScalar(255)));
434 }
435
436 // Clamp result
437 return SkScalarPin(turbulenceFunctionResult, 0, SK_Scalar1);
438}
439
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000440SkPMColor SkPerlinNoiseShader::PerlinNoiseShaderContext::shade(
441 const SkPoint& point, StitchData& stitchData) const {
sugoi@google.come3b4c502013-04-05 13:47:09 +0000442 SkPoint newPoint;
senorblanco@chromium.orga8d95f82014-04-04 14:46:10 +0000443 fMatrix.mapPoints(&newPoint, &point, 1);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000444 newPoint.fX = SkScalarRoundToScalar(newPoint.fX);
445 newPoint.fY = SkScalarRoundToScalar(newPoint.fY);
446
447 U8CPU rgba[4];
448 for (int channel = 3; channel >= 0; --channel) {
449 rgba[channel] = SkScalarFloorToInt(255 *
senorblancoca6a7c22014-06-27 13:35:52 -0700450 calculateTurbulenceValueForPoint(channel, stitchData, newPoint));
sugoi@google.come3b4c502013-04-05 13:47:09 +0000451 }
452 return SkPreMultiplyARGB(rgba[3], rgba[0], rgba[1], rgba[2]);
453}
454
commit-bot@chromium.orgce56d962014-05-05 18:39:18 +0000455SkShader::Context* SkPerlinNoiseShader::onCreateContext(const ContextRec& rec,
456 void* storage) const {
commit-bot@chromium.orge901b6d2014-05-01 19:31:31 +0000457 return SkNEW_PLACEMENT_ARGS(storage, PerlinNoiseShaderContext, (*this, rec));
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000458}
459
460size_t SkPerlinNoiseShader::contextSize() const {
461 return sizeof(PerlinNoiseShaderContext);
462}
463
464SkPerlinNoiseShader::PerlinNoiseShaderContext::PerlinNoiseShaderContext(
commit-bot@chromium.orge901b6d2014-05-01 19:31:31 +0000465 const SkPerlinNoiseShader& shader, const ContextRec& rec)
466 : INHERITED(shader, rec)
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000467{
commit-bot@chromium.orge901b6d2014-05-01 19:31:31 +0000468 SkMatrix newMatrix = *rec.fMatrix;
reed@google.comb67b8e62014-05-12 18:12:24 +0000469 newMatrix.preConcat(shader.getLocalMatrix());
470 if (rec.fLocalMatrix) {
471 newMatrix.preConcat(*rec.fLocalMatrix);
472 }
senorblanco@chromium.orga8d95f82014-04-04 14:46:10 +0000473 // This (1,1) translation is due to WebKit's 1 based coordinates for the noise
474 // (as opposed to 0 based, usually). The same adjustment is in the setData() function.
senorblancoca6a7c22014-06-27 13:35:52 -0700475 fMatrix.setTranslate(-newMatrix.getTranslateX() + SK_Scalar1, -newMatrix.getTranslateY() + SK_Scalar1);
476 fPaintingData = SkNEW_ARGS(PaintingData, (shader.fTileSize, shader.fSeed, shader.fBaseFrequencyX, shader.fBaseFrequencyY, newMatrix));
477}
478
479SkPerlinNoiseShader::PerlinNoiseShaderContext::~PerlinNoiseShaderContext() {
480 SkDELETE(fPaintingData);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000481}
482
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000483void SkPerlinNoiseShader::PerlinNoiseShaderContext::shadeSpan(
484 int x, int y, SkPMColor result[], int count) {
sugoi@google.come3b4c502013-04-05 13:47:09 +0000485 SkPoint point = SkPoint::Make(SkIntToScalar(x), SkIntToScalar(y));
486 StitchData stitchData;
487 for (int i = 0; i < count; ++i) {
488 result[i] = shade(point, stitchData);
489 point.fX += SK_Scalar1;
490 }
491}
492
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000493void SkPerlinNoiseShader::PerlinNoiseShaderContext::shadeSpan16(
494 int x, int y, uint16_t result[], int count) {
sugoi@google.come3b4c502013-04-05 13:47:09 +0000495 SkPoint point = SkPoint::Make(SkIntToScalar(x), SkIntToScalar(y));
496 StitchData stitchData;
497 DITHER_565_SCAN(y);
498 for (int i = 0; i < count; ++i) {
499 unsigned dither = DITHER_VALUE(x);
500 result[i] = SkDitherRGB32To565(shade(point, stitchData), dither);
501 DITHER_INC_X(x);
502 point.fX += SK_Scalar1;
503 }
504}
505
506/////////////////////////////////////////////////////////////////////
507
commit-bot@chromium.org344cf452013-06-17 14:19:01 +0000508#if SK_SUPPORT_GPU
sugoi@google.come3b4c502013-04-05 13:47:09 +0000509
510#include "GrTBackendEffectFactory.h"
511
senorblancof3b50272014-06-16 10:49:58 -0700512class GrGLPerlinNoise : public GrGLEffect {
sugoi@google.com4775cba2013-04-17 13:46:56 +0000513public:
sugoi@google.come3b4c502013-04-05 13:47:09 +0000514 GrGLPerlinNoise(const GrBackendEffectFactory& factory,
senorblancof3b50272014-06-16 10:49:58 -0700515 const GrDrawEffect& drawEffect);
sugoi@google.com4775cba2013-04-17 13:46:56 +0000516 virtual ~GrGLPerlinNoise() {}
sugoi@google.come3b4c502013-04-05 13:47:09 +0000517
joshualitt30ba4362014-08-21 20:18:45 -0700518 virtual void emitCode(GrGLProgramBuilder*,
sugoi@google.come3b4c502013-04-05 13:47:09 +0000519 const GrDrawEffect&,
bsalomon63e99f72014-07-21 08:03:14 -0700520 const GrEffectKey&,
sugoi@google.come3b4c502013-04-05 13:47:09 +0000521 const char* outputColor,
522 const char* inputColor,
bsalomon@google.com77af6802013-10-02 13:04:56 +0000523 const TransformedCoordsArray&,
sugoi@google.come3b4c502013-04-05 13:47:09 +0000524 const TextureSamplerArray&) SK_OVERRIDE;
525
kkinnunen7510b222014-07-30 00:04:16 -0700526 virtual void setData(const GrGLProgramDataManager&, const GrDrawEffect&) SK_OVERRIDE;
sugoi@google.come3b4c502013-04-05 13:47:09 +0000527
bsalomon63e99f72014-07-21 08:03:14 -0700528 static inline void GenKey(const GrDrawEffect&, const GrGLCaps&, GrEffectKeyBuilder* b);
sugoi@google.com4775cba2013-04-17 13:46:56 +0000529
530private:
sugoi@google.com4775cba2013-04-17 13:46:56 +0000531
kkinnunen7510b222014-07-30 00:04:16 -0700532 GrGLProgramDataManager::UniformHandle fStitchDataUni;
533 SkPerlinNoiseShader::Type fType;
534 bool fStitchTiles;
535 int fNumOctaves;
536 GrGLProgramDataManager::UniformHandle fBaseFrequencyUni;
537 GrGLProgramDataManager::UniformHandle fAlphaUni;
senorblancof3b50272014-06-16 10:49:58 -0700538
539private:
540 typedef GrGLEffect INHERITED;
sugoi@google.come3b4c502013-04-05 13:47:09 +0000541};
542
543/////////////////////////////////////////////////////////////////////
544
senorblancof3b50272014-06-16 10:49:58 -0700545class GrPerlinNoiseEffect : public GrEffect {
sugoi@google.come3b4c502013-04-05 13:47:09 +0000546public:
bsalomon83d081a2014-07-08 09:56:10 -0700547 static GrEffect* Create(SkPerlinNoiseShader::Type type,
548 int numOctaves, bool stitchTiles,
549 SkPerlinNoiseShader::PaintingData* paintingData,
550 GrTexture* permutationsTexture, GrTexture* noiseTexture,
551 const SkMatrix& matrix, uint8_t alpha) {
bsalomon55fad7a2014-07-08 07:34:20 -0700552 return SkNEW_ARGS(GrPerlinNoiseEffect, (type, numOctaves, stitchTiles, paintingData,
553 permutationsTexture, noiseTexture, matrix, alpha));
sugoi@google.come3b4c502013-04-05 13:47:09 +0000554 }
555
senorblancoca6a7c22014-06-27 13:35:52 -0700556 virtual ~GrPerlinNoiseEffect() {
557 SkDELETE(fPaintingData);
558 }
sugoi@google.come3b4c502013-04-05 13:47:09 +0000559
560 static const char* Name() { return "PerlinNoise"; }
561 virtual const GrBackendEffectFactory& getFactory() const SK_OVERRIDE {
562 return GrTBackendEffectFactory<GrPerlinNoiseEffect>::getInstance();
563 }
senorblancoca6a7c22014-06-27 13:35:52 -0700564 const SkPerlinNoiseShader::StitchData& stitchData() const { return fPaintingData->fStitchDataInit; }
sugoi@google.come3b4c502013-04-05 13:47:09 +0000565
senorblancof3b50272014-06-16 10:49:58 -0700566 SkPerlinNoiseShader::Type type() const { return fType; }
567 bool stitchTiles() const { return fStitchTiles; }
senorblancoca6a7c22014-06-27 13:35:52 -0700568 const SkVector& baseFrequency() const { return fPaintingData->fBaseFrequency; }
senorblancof3b50272014-06-16 10:49:58 -0700569 int numOctaves() const { return fNumOctaves; }
570 const SkMatrix& matrix() const { return fCoordTransform.getMatrix(); }
571 uint8_t alpha() const { return fAlpha; }
572
sugoi@google.come3b4c502013-04-05 13:47:09 +0000573 typedef GrGLPerlinNoise GLEffect;
574
sugoi@google.come3b4c502013-04-05 13:47:09 +0000575private:
576 virtual bool onIsEqual(const GrEffect& sBase) const SK_OVERRIDE {
577 const GrPerlinNoiseEffect& s = CastEffect<GrPerlinNoiseEffect>(sBase);
senorblancof3b50272014-06-16 10:49:58 -0700578 return fType == s.fType &&
senorblancoca6a7c22014-06-27 13:35:52 -0700579 fPaintingData->fBaseFrequency == s.fPaintingData->fBaseFrequency &&
senorblancof3b50272014-06-16 10:49:58 -0700580 fNumOctaves == s.fNumOctaves &&
581 fStitchTiles == s.fStitchTiles &&
582 fCoordTransform.getMatrix() == s.fCoordTransform.getMatrix() &&
583 fAlpha == s.fAlpha &&
sugoi@google.com4775cba2013-04-17 13:46:56 +0000584 fPermutationsAccess.getTexture() == s.fPermutationsAccess.getTexture() &&
sugoi@google.come3b4c502013-04-05 13:47:09 +0000585 fNoiseAccess.getTexture() == s.fNoiseAccess.getTexture() &&
senorblancoca6a7c22014-06-27 13:35:52 -0700586 fPaintingData->fStitchDataInit == s.fPaintingData->fStitchDataInit;
sugoi@google.come3b4c502013-04-05 13:47:09 +0000587 }
588
senorblancoca6a7c22014-06-27 13:35:52 -0700589 GrPerlinNoiseEffect(SkPerlinNoiseShader::Type type,
sugoi@google.come3b4c502013-04-05 13:47:09 +0000590 int numOctaves, bool stitchTiles,
senorblancoca6a7c22014-06-27 13:35:52 -0700591 SkPerlinNoiseShader::PaintingData* paintingData,
sugoi@google.come3b4c502013-04-05 13:47:09 +0000592 GrTexture* permutationsTexture, GrTexture* noiseTexture,
593 const SkMatrix& matrix, uint8_t alpha)
senorblancof3b50272014-06-16 10:49:58 -0700594 : fType(type)
senorblancof3b50272014-06-16 10:49:58 -0700595 , fNumOctaves(numOctaves)
596 , fStitchTiles(stitchTiles)
597 , fAlpha(alpha)
sugoi@google.com4775cba2013-04-17 13:46:56 +0000598 , fPermutationsAccess(permutationsTexture)
sugoi@google.come3b4c502013-04-05 13:47:09 +0000599 , fNoiseAccess(noiseTexture)
senorblancoca6a7c22014-06-27 13:35:52 -0700600 , fPaintingData(paintingData) {
sugoi@google.come3b4c502013-04-05 13:47:09 +0000601 this->addTextureAccess(&fPermutationsAccess);
602 this->addTextureAccess(&fNoiseAccess);
senorblancoca6a7c22014-06-27 13:35:52 -0700603 fCoordTransform.reset(kLocal_GrCoordSet, matrix);
senorblancof3b50272014-06-16 10:49:58 -0700604 this->addCoordTransform(&fCoordTransform);
605 this->setWillNotUseInputColor();
sugoi@google.come3b4c502013-04-05 13:47:09 +0000606 }
607
sugoi@google.com4775cba2013-04-17 13:46:56 +0000608 GR_DECLARE_EFFECT_TEST;
sugoi@google.come3b4c502013-04-05 13:47:09 +0000609
senorblancof3b50272014-06-16 10:49:58 -0700610 SkPerlinNoiseShader::Type fType;
611 GrCoordTransform fCoordTransform;
senorblancof3b50272014-06-16 10:49:58 -0700612 int fNumOctaves;
613 bool fStitchTiles;
614 uint8_t fAlpha;
sugoi@google.come3b4c502013-04-05 13:47:09 +0000615 GrTextureAccess fPermutationsAccess;
616 GrTextureAccess fNoiseAccess;
senorblancoca6a7c22014-06-27 13:35:52 -0700617 SkPerlinNoiseShader::PaintingData *fPaintingData;
sugoi@google.come3b4c502013-04-05 13:47:09 +0000618
senorblancof3b50272014-06-16 10:49:58 -0700619 void getConstantColorComponents(GrColor*, uint32_t* validFlags) const SK_OVERRIDE {
620 *validFlags = 0; // This is noise. Nothing is constant.
sugoi@google.com4775cba2013-04-17 13:46:56 +0000621 }
622
sugoi@google.com4775cba2013-04-17 13:46:56 +0000623private:
senorblancof3b50272014-06-16 10:49:58 -0700624 typedef GrEffect INHERITED;
sugoi@google.come3b4c502013-04-05 13:47:09 +0000625};
626
627/////////////////////////////////////////////////////////////////////
sugoi@google.come3b4c502013-04-05 13:47:09 +0000628GR_DEFINE_EFFECT_TEST(GrPerlinNoiseEffect);
629
bsalomon83d081a2014-07-08 09:56:10 -0700630GrEffect* GrPerlinNoiseEffect::TestCreate(SkRandom* random,
631 GrContext* context,
632 const GrDrawTargetCaps&,
633 GrTexture**) {
sugoi@google.com423ac132013-04-18 14:04:57 +0000634 int numOctaves = random->nextRangeU(2, 10);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000635 bool stitchTiles = random->nextBool();
636 SkScalar seed = SkIntToScalar(random->nextU());
637 SkISize tileSize = SkISize::Make(random->nextRangeU(4, 4096), random->nextRangeU(4, 4096));
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +0000638 SkScalar baseFrequencyX = random->nextRangeScalar(0.01f,
639 0.99f);
640 SkScalar baseFrequencyY = random->nextRangeScalar(0.01f,
641 0.99f);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000642
643 SkShader* shader = random->nextBool() ?
644 SkPerlinNoiseShader::CreateFractalNoise(baseFrequencyX, baseFrequencyY, numOctaves, seed,
645 stitchTiles ? &tileSize : NULL) :
commit-bot@chromium.org9fbbcca2014-04-01 16:09:37 +0000646 SkPerlinNoiseShader::CreateTurbulence(baseFrequencyX, baseFrequencyY, numOctaves, seed,
sugoi@google.come3b4c502013-04-05 13:47:09 +0000647 stitchTiles ? &tileSize : NULL);
648
649 SkPaint paint;
bsalomon83d081a2014-07-08 09:56:10 -0700650 GrColor paintColor;
651 GrEffect* effect;
652 SkAssertResult(shader->asNewEffect(context, paint, NULL, &paintColor, &effect));
sugoi@google.come3b4c502013-04-05 13:47:09 +0000653
654 SkDELETE(shader);
655
656 return effect;
657}
sugoi@google.com4775cba2013-04-17 13:46:56 +0000658
senorblancof3b50272014-06-16 10:49:58 -0700659GrGLPerlinNoise::GrGLPerlinNoise(const GrBackendEffectFactory& factory, const GrDrawEffect& drawEffect)
660 : INHERITED (factory)
661 , fType(drawEffect.castEffect<GrPerlinNoiseEffect>().type())
662 , fStitchTiles(drawEffect.castEffect<GrPerlinNoiseEffect>().stitchTiles())
663 , fNumOctaves(drawEffect.castEffect<GrPerlinNoiseEffect>().numOctaves()) {
sugoi@google.com4775cba2013-04-17 13:46:56 +0000664}
665
joshualitt30ba4362014-08-21 20:18:45 -0700666void GrGLPerlinNoise::emitCode(GrGLProgramBuilder* builder,
sugoi@google.come3b4c502013-04-05 13:47:09 +0000667 const GrDrawEffect&,
bsalomon63e99f72014-07-21 08:03:14 -0700668 const GrEffectKey& key,
sugoi@google.come3b4c502013-04-05 13:47:09 +0000669 const char* outputColor,
670 const char* inputColor,
bsalomon@google.com77af6802013-10-02 13:04:56 +0000671 const TransformedCoordsArray& coords,
sugoi@google.come3b4c502013-04-05 13:47:09 +0000672 const TextureSamplerArray& samplers) {
673 sk_ignore_unused_variable(inputColor);
674
joshualitt30ba4362014-08-21 20:18:45 -0700675 GrGLFragmentShaderBuilder* fsBuilder = builder->getFragmentShaderBuilder();
676 SkString vCoords = fsBuilder->ensureFSCoords2D(coords, 0);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000677
joshualitt30ba4362014-08-21 20:18:45 -0700678 fBaseFrequencyUni = builder->addUniform(GrGLProgramBuilder::kFragment_Visibility,
sugoi@google.come3b4c502013-04-05 13:47:09 +0000679 kVec2f_GrSLType, "baseFrequency");
680 const char* baseFrequencyUni = builder->getUniformCStr(fBaseFrequencyUni);
joshualitt30ba4362014-08-21 20:18:45 -0700681 fAlphaUni = builder->addUniform(GrGLProgramBuilder::kFragment_Visibility,
sugoi@google.come3b4c502013-04-05 13:47:09 +0000682 kFloat_GrSLType, "alpha");
683 const char* alphaUni = builder->getUniformCStr(fAlphaUni);
684
685 const char* stitchDataUni = NULL;
686 if (fStitchTiles) {
joshualitt30ba4362014-08-21 20:18:45 -0700687 fStitchDataUni = builder->addUniform(GrGLProgramBuilder::kFragment_Visibility,
commit-bot@chromium.org98393202013-07-04 18:13:05 +0000688 kVec2f_GrSLType, "stitchData");
sugoi@google.come3b4c502013-04-05 13:47:09 +0000689 stitchDataUni = builder->getUniformCStr(fStitchDataUni);
690 }
691
sugoi@google.comd537af52013-06-10 13:59:25 +0000692 // There are 4 lines, so the center of each line is 1/8, 3/8, 5/8 and 7/8
693 const char* chanCoordR = "0.125";
694 const char* chanCoordG = "0.375";
695 const char* chanCoordB = "0.625";
696 const char* chanCoordA = "0.875";
697 const char* chanCoord = "chanCoord";
sugoi@google.come3b4c502013-04-05 13:47:09 +0000698 const char* stitchData = "stitchData";
699 const char* ratio = "ratio";
sugoi@google.come3b4c502013-04-05 13:47:09 +0000700 const char* noiseVec = "noiseVec";
sugoi@google.come3b4c502013-04-05 13:47:09 +0000701 const char* noiseSmooth = "noiseSmooth";
senorblancoce6a3542014-06-12 11:24:19 -0700702 const char* floorVal = "floorVal";
sugoi@google.come3b4c502013-04-05 13:47:09 +0000703 const char* fractVal = "fractVal";
704 const char* uv = "uv";
705 const char* ab = "ab";
706 const char* latticeIdx = "latticeIdx";
senorblancoce6a3542014-06-12 11:24:19 -0700707 const char* bcoords = "bcoords";
sugoi@google.come3b4c502013-04-05 13:47:09 +0000708 const char* lattice = "lattice";
sugoi@google.come3b4c502013-04-05 13:47:09 +0000709 const char* inc8bit = "0.00390625"; // 1.0 / 256.0
710 // This is the math to convert the two 16bit integer packed into rgba 8 bit input into a
711 // [-1,1] vector and perform a dot product between that vector and the provided vector.
712 const char* dotLattice = "dot(((%s.ga + %s.rb * vec2(%s)) * vec2(2.0) - vec2(1.0)), %s);";
713
sugoi@google.comd537af52013-06-10 13:59:25 +0000714 // Add noise function
715 static const GrGLShaderVar gPerlinNoiseArgs[] = {
716 GrGLShaderVar(chanCoord, kFloat_GrSLType),
commit-bot@chromium.org98393202013-07-04 18:13:05 +0000717 GrGLShaderVar(noiseVec, kVec2f_GrSLType)
sugoi@google.comd537af52013-06-10 13:59:25 +0000718 };
sugoi@google.come3b4c502013-04-05 13:47:09 +0000719
sugoi@google.comd537af52013-06-10 13:59:25 +0000720 static const GrGLShaderVar gPerlinNoiseStitchArgs[] = {
721 GrGLShaderVar(chanCoord, kFloat_GrSLType),
commit-bot@chromium.org98393202013-07-04 18:13:05 +0000722 GrGLShaderVar(noiseVec, kVec2f_GrSLType),
723 GrGLShaderVar(stitchData, kVec2f_GrSLType)
sugoi@google.comd537af52013-06-10 13:59:25 +0000724 };
sugoi@google.come3b4c502013-04-05 13:47:09 +0000725
sugoi@google.comd537af52013-06-10 13:59:25 +0000726 SkString noiseCode;
sugoi@google.come3b4c502013-04-05 13:47:09 +0000727
senorblancoce6a3542014-06-12 11:24:19 -0700728 noiseCode.appendf("\tvec4 %s;\n", floorVal);
729 noiseCode.appendf("\t%s.xy = floor(%s);\n", floorVal, noiseVec);
730 noiseCode.appendf("\t%s.zw = %s.xy + vec2(1.0);\n", floorVal, floorVal);
731 noiseCode.appendf("\tvec2 %s = fract(%s);\n", fractVal, noiseVec);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000732
733 // smooth curve : t * t * (3 - 2 * t)
senorblancoce6a3542014-06-12 11:24:19 -0700734 noiseCode.appendf("\n\tvec2 %s = %s * %s * (vec2(3.0) - vec2(2.0) * %s);",
735 noiseSmooth, fractVal, fractVal, fractVal);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000736
737 // Adjust frequencies if we're stitching tiles
738 if (fStitchTiles) {
commit-bot@chromium.org98393202013-07-04 18:13:05 +0000739 noiseCode.appendf("\n\tif(%s.x >= %s.x) { %s.x -= %s.x; }",
senorblancoce6a3542014-06-12 11:24:19 -0700740 floorVal, stitchData, floorVal, stitchData);
commit-bot@chromium.org98393202013-07-04 18:13:05 +0000741 noiseCode.appendf("\n\tif(%s.y >= %s.y) { %s.y -= %s.y; }",
senorblancoce6a3542014-06-12 11:24:19 -0700742 floorVal, stitchData, floorVal, stitchData);
743 noiseCode.appendf("\n\tif(%s.z >= %s.x) { %s.z -= %s.x; }",
744 floorVal, stitchData, floorVal, stitchData);
745 noiseCode.appendf("\n\tif(%s.w >= %s.y) { %s.w -= %s.y; }",
746 floorVal, stitchData, floorVal, stitchData);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000747 }
748
749 // Get texture coordinates and normalize
senorblancoce6a3542014-06-12 11:24:19 -0700750 noiseCode.appendf("\n\t%s = fract(floor(mod(%s, 256.0)) / vec4(256.0));\n",
751 floorVal, floorVal);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000752
753 // Get permutation for x
754 {
755 SkString xCoords("");
senorblancoce6a3542014-06-12 11:24:19 -0700756 xCoords.appendf("vec2(%s.x, 0.5)", floorVal);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000757
sugoi@google.comd537af52013-06-10 13:59:25 +0000758 noiseCode.appendf("\n\tvec2 %s;\n\t%s.x = ", latticeIdx, latticeIdx);
joshualitt30ba4362014-08-21 20:18:45 -0700759 fsBuilder->appendTextureLookup(&noiseCode, samplers[0], xCoords.c_str(), kVec2f_GrSLType);
sugoi@google.comd537af52013-06-10 13:59:25 +0000760 noiseCode.append(".r;");
sugoi@google.come3b4c502013-04-05 13:47:09 +0000761 }
762
763 // Get permutation for x + 1
764 {
765 SkString xCoords("");
senorblancoce6a3542014-06-12 11:24:19 -0700766 xCoords.appendf("vec2(%s.z, 0.5)", floorVal);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000767
sugoi@google.comd537af52013-06-10 13:59:25 +0000768 noiseCode.appendf("\n\t%s.y = ", latticeIdx);
joshualitt30ba4362014-08-21 20:18:45 -0700769 fsBuilder->appendTextureLookup(&noiseCode, samplers[0], xCoords.c_str(), kVec2f_GrSLType);
sugoi@google.comd537af52013-06-10 13:59:25 +0000770 noiseCode.append(".r;");
sugoi@google.come3b4c502013-04-05 13:47:09 +0000771 }
772
commit-bot@chromium.org344cf452013-06-17 14:19:01 +0000773#if defined(SK_BUILD_FOR_ANDROID)
774 // Android rounding for Tegra devices, like, for example: Xoom (Tegra 2), Nexus 7 (Tegra 3).
775 // The issue is that colors aren't accurate enough on Tegra devices. For example, if an 8 bit
776 // value of 124 (or 0.486275 here) is entered, we can get a texture value of 123.513725
777 // (or 0.484368 here). The following rounding operation prevents these precision issues from
778 // affecting the result of the noise by making sure that we only have multiples of 1/255.
779 // (Note that 1/255 is about 0.003921569, which is the value used here).
780 noiseCode.appendf("\n\t%s = floor(%s * vec2(255.0) + vec2(0.5)) * vec2(0.003921569);",
781 latticeIdx, latticeIdx);
782#endif
783
sugoi@google.come3b4c502013-04-05 13:47:09 +0000784 // Get (x,y) coordinates with the permutated x
senorblancoce6a3542014-06-12 11:24:19 -0700785 noiseCode.appendf("\n\tvec4 %s = fract(%s.xyxy + %s.yyww);", bcoords, latticeIdx, floorVal);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000786
sugoi@google.comd537af52013-06-10 13:59:25 +0000787 noiseCode.appendf("\n\n\tvec2 %s;", uv);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000788 // Compute u, at offset (0,0)
789 {
790 SkString latticeCoords("");
senorblancoce6a3542014-06-12 11:24:19 -0700791 latticeCoords.appendf("vec2(%s.x, %s)", bcoords, chanCoord);
sugoi@google.comd537af52013-06-10 13:59:25 +0000792 noiseCode.appendf("\n\tvec4 %s = ", lattice);
joshualitt30ba4362014-08-21 20:18:45 -0700793 fsBuilder->appendTextureLookup(&noiseCode, samplers[1], latticeCoords.c_str(),
sugoi@google.comd537af52013-06-10 13:59:25 +0000794 kVec2f_GrSLType);
795 noiseCode.appendf(".bgra;\n\t%s.x = ", uv);
796 noiseCode.appendf(dotLattice, lattice, lattice, inc8bit, fractVal);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000797 }
798
sugoi@google.comd537af52013-06-10 13:59:25 +0000799 noiseCode.appendf("\n\t%s.x -= 1.0;", fractVal);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000800 // Compute v, at offset (-1,0)
801 {
802 SkString latticeCoords("");
senorblancoce6a3542014-06-12 11:24:19 -0700803 latticeCoords.appendf("vec2(%s.y, %s)", bcoords, chanCoord);
commit-bot@chromium.org344cf452013-06-17 14:19:01 +0000804 noiseCode.append("\n\tlattice = ");
joshualitt30ba4362014-08-21 20:18:45 -0700805 fsBuilder->appendTextureLookup(&noiseCode, samplers[1], latticeCoords.c_str(),
sugoi@google.comd537af52013-06-10 13:59:25 +0000806 kVec2f_GrSLType);
807 noiseCode.appendf(".bgra;\n\t%s.y = ", uv);
808 noiseCode.appendf(dotLattice, lattice, lattice, inc8bit, fractVal);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000809 }
810
811 // Compute 'a' as a linear interpolation of 'u' and 'v'
sugoi@google.comd537af52013-06-10 13:59:25 +0000812 noiseCode.appendf("\n\tvec2 %s;", ab);
813 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 +0000814
sugoi@google.comd537af52013-06-10 13:59:25 +0000815 noiseCode.appendf("\n\t%s.y -= 1.0;", fractVal);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000816 // Compute v, at offset (-1,-1)
817 {
818 SkString latticeCoords("");
senorblancoce6a3542014-06-12 11:24:19 -0700819 latticeCoords.appendf("vec2(%s.w, %s)", bcoords, chanCoord);
commit-bot@chromium.org344cf452013-06-17 14:19:01 +0000820 noiseCode.append("\n\tlattice = ");
joshualitt30ba4362014-08-21 20:18:45 -0700821 fsBuilder->appendTextureLookup(&noiseCode, samplers[1], latticeCoords.c_str(),
sugoi@google.comd537af52013-06-10 13:59:25 +0000822 kVec2f_GrSLType);
823 noiseCode.appendf(".bgra;\n\t%s.y = ", uv);
824 noiseCode.appendf(dotLattice, lattice, lattice, inc8bit, fractVal);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000825 }
826
sugoi@google.comd537af52013-06-10 13:59:25 +0000827 noiseCode.appendf("\n\t%s.x += 1.0;", fractVal);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000828 // Compute u, at offset (0,-1)
829 {
830 SkString latticeCoords("");
senorblancoce6a3542014-06-12 11:24:19 -0700831 latticeCoords.appendf("vec2(%s.z, %s)", bcoords, chanCoord);
commit-bot@chromium.org344cf452013-06-17 14:19:01 +0000832 noiseCode.append("\n\tlattice = ");
joshualitt30ba4362014-08-21 20:18:45 -0700833 fsBuilder->appendTextureLookup(&noiseCode, samplers[1], latticeCoords.c_str(),
sugoi@google.comd537af52013-06-10 13:59:25 +0000834 kVec2f_GrSLType);
835 noiseCode.appendf(".bgra;\n\t%s.x = ", uv);
836 noiseCode.appendf(dotLattice, lattice, lattice, inc8bit, fractVal);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000837 }
838
839 // Compute 'b' as a linear interpolation of 'u' and 'v'
sugoi@google.comd537af52013-06-10 13:59:25 +0000840 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 +0000841 // Compute the noise as a linear interpolation of 'a' and 'b'
sugoi@google.comd537af52013-06-10 13:59:25 +0000842 noiseCode.appendf("\n\treturn mix(%s.x, %s.y, %s.y);\n", ab, ab, noiseSmooth);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000843
sugoi@google.comd537af52013-06-10 13:59:25 +0000844 SkString noiseFuncName;
845 if (fStitchTiles) {
joshualitt30ba4362014-08-21 20:18:45 -0700846 fsBuilder->emitFunction(kFloat_GrSLType,
commit-bot@chromium.org74a3a212013-08-30 19:43:59 +0000847 "perlinnoise", SK_ARRAY_COUNT(gPerlinNoiseStitchArgs),
848 gPerlinNoiseStitchArgs, noiseCode.c_str(), &noiseFuncName);
sugoi@google.comd537af52013-06-10 13:59:25 +0000849 } else {
joshualitt30ba4362014-08-21 20:18:45 -0700850 fsBuilder->emitFunction(kFloat_GrSLType,
commit-bot@chromium.org74a3a212013-08-30 19:43:59 +0000851 "perlinnoise", SK_ARRAY_COUNT(gPerlinNoiseArgs),
852 gPerlinNoiseArgs, noiseCode.c_str(), &noiseFuncName);
sugoi@google.comd537af52013-06-10 13:59:25 +0000853 }
sugoi@google.come3b4c502013-04-05 13:47:09 +0000854
sugoi@google.comd537af52013-06-10 13:59:25 +0000855 // There are rounding errors if the floor operation is not performed here
joshualitt30ba4362014-08-21 20:18:45 -0700856 fsBuilder->codeAppendf("\n\t\tvec2 %s = floor(%s.xy) * %s;",
senorblancoca6a7c22014-06-27 13:35:52 -0700857 noiseVec, vCoords.c_str(), baseFrequencyUni);
sugoi@google.comd537af52013-06-10 13:59:25 +0000858
859 // Clear the color accumulator
joshualitt30ba4362014-08-21 20:18:45 -0700860 fsBuilder->codeAppendf("\n\t\t%s = vec4(0.0);", outputColor);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000861
862 if (fStitchTiles) {
sugoi@google.comd537af52013-06-10 13:59:25 +0000863 // Set up TurbulenceInitial stitch values.
joshualitt30ba4362014-08-21 20:18:45 -0700864 fsBuilder->codeAppendf("\n\t\tvec2 %s = %s;", stitchData, stitchDataUni);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000865 }
sugoi@google.come3b4c502013-04-05 13:47:09 +0000866
joshualitt30ba4362014-08-21 20:18:45 -0700867 fsBuilder->codeAppendf("\n\t\tfloat %s = 1.0;", ratio);
sugoi@google.comd537af52013-06-10 13:59:25 +0000868
869 // Loop over all octaves
joshualitt30ba4362014-08-21 20:18:45 -0700870 fsBuilder->codeAppendf("\n\t\tfor (int octave = 0; octave < %d; ++octave) {", fNumOctaves);
sugoi@google.comd537af52013-06-10 13:59:25 +0000871
joshualitt30ba4362014-08-21 20:18:45 -0700872 fsBuilder->codeAppendf("\n\t\t\t%s += ", outputColor);
sugoi@google.comd537af52013-06-10 13:59:25 +0000873 if (fType != SkPerlinNoiseShader::kFractalNoise_Type) {
joshualitt30ba4362014-08-21 20:18:45 -0700874 fsBuilder->codeAppend("abs(");
sugoi@google.comd537af52013-06-10 13:59:25 +0000875 }
876 if (fStitchTiles) {
joshualitt30ba4362014-08-21 20:18:45 -0700877 fsBuilder->codeAppendf(
sugoi@google.comd537af52013-06-10 13:59:25 +0000878 "vec4(\n\t\t\t\t%s(%s, %s, %s),\n\t\t\t\t%s(%s, %s, %s),"
879 "\n\t\t\t\t%s(%s, %s, %s),\n\t\t\t\t%s(%s, %s, %s))",
880 noiseFuncName.c_str(), chanCoordR, noiseVec, stitchData,
881 noiseFuncName.c_str(), chanCoordG, noiseVec, stitchData,
882 noiseFuncName.c_str(), chanCoordB, noiseVec, stitchData,
883 noiseFuncName.c_str(), chanCoordA, noiseVec, stitchData);
884 } else {
joshualitt30ba4362014-08-21 20:18:45 -0700885 fsBuilder->codeAppendf(
sugoi@google.comd537af52013-06-10 13:59:25 +0000886 "vec4(\n\t\t\t\t%s(%s, %s),\n\t\t\t\t%s(%s, %s),"
887 "\n\t\t\t\t%s(%s, %s),\n\t\t\t\t%s(%s, %s))",
888 noiseFuncName.c_str(), chanCoordR, noiseVec,
889 noiseFuncName.c_str(), chanCoordG, noiseVec,
890 noiseFuncName.c_str(), chanCoordB, noiseVec,
891 noiseFuncName.c_str(), chanCoordA, noiseVec);
892 }
893 if (fType != SkPerlinNoiseShader::kFractalNoise_Type) {
joshualitt30ba4362014-08-21 20:18:45 -0700894 fsBuilder->codeAppendf(")"); // end of "abs("
sugoi@google.comd537af52013-06-10 13:59:25 +0000895 }
joshualitt30ba4362014-08-21 20:18:45 -0700896 fsBuilder->codeAppendf(" * %s;", ratio);
sugoi@google.comd537af52013-06-10 13:59:25 +0000897
joshualitt30ba4362014-08-21 20:18:45 -0700898 fsBuilder->codeAppendf("\n\t\t\t%s *= vec2(2.0);", noiseVec);
899 fsBuilder->codeAppendf("\n\t\t\t%s *= 0.5;", ratio);
sugoi@google.comd537af52013-06-10 13:59:25 +0000900
901 if (fStitchTiles) {
joshualitt30ba4362014-08-21 20:18:45 -0700902 fsBuilder->codeAppendf("\n\t\t\t%s *= vec2(2.0);", stitchData);
sugoi@google.comd537af52013-06-10 13:59:25 +0000903 }
joshualitt30ba4362014-08-21 20:18:45 -0700904 fsBuilder->codeAppend("\n\t\t}"); // end of the for loop on octaves
sugoi@google.come3b4c502013-04-05 13:47:09 +0000905
906 if (fType == SkPerlinNoiseShader::kFractalNoise_Type) {
907 // The value of turbulenceFunctionResult comes from ((turbulenceFunctionResult) + 1) / 2
908 // by fractalNoise and (turbulenceFunctionResult) by turbulence.
joshualitt30ba4362014-08-21 20:18:45 -0700909 fsBuilder->codeAppendf("\n\t\t%s = %s * vec4(0.5) + vec4(0.5);", outputColor, outputColor);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000910 }
911
joshualitt30ba4362014-08-21 20:18:45 -0700912 fsBuilder->codeAppendf("\n\t\t%s.a *= %s;", outputColor, alphaUni);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000913
914 // Clamp values
joshualitt30ba4362014-08-21 20:18:45 -0700915 fsBuilder->codeAppendf("\n\t\t%s = clamp(%s, 0.0, 1.0);", outputColor, outputColor);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000916
917 // Pre-multiply the result
joshualitt30ba4362014-08-21 20:18:45 -0700918 fsBuilder->codeAppendf("\n\t\t%s = vec4(%s.rgb * %s.aaa, %s.a);\n",
sugoi@google.come3b4c502013-04-05 13:47:09 +0000919 outputColor, outputColor, outputColor, outputColor);
920}
921
bsalomon63e99f72014-07-21 08:03:14 -0700922void GrGLPerlinNoise::GenKey(const GrDrawEffect& drawEffect, const GrGLCaps&, GrEffectKeyBuilder* b) {
sugoi@google.come3b4c502013-04-05 13:47:09 +0000923 const GrPerlinNoiseEffect& turbulence = drawEffect.castEffect<GrPerlinNoiseEffect>();
924
bsalomon63e99f72014-07-21 08:03:14 -0700925 uint32_t key = turbulence.numOctaves();
sugoi@google.come3b4c502013-04-05 13:47:09 +0000926
927 key = key << 3; // Make room for next 3 bits
928
929 switch (turbulence.type()) {
930 case SkPerlinNoiseShader::kFractalNoise_Type:
931 key |= 0x1;
932 break;
933 case SkPerlinNoiseShader::kTurbulence_Type:
934 key |= 0x2;
935 break;
936 default:
937 // leave key at 0
938 break;
939 }
940
941 if (turbulence.stitchTiles()) {
942 key |= 0x4; // Flip the 3rd bit if tile stitching is on
943 }
944
bsalomon63e99f72014-07-21 08:03:14 -0700945 b->add32(key);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000946}
947
kkinnunen7510b222014-07-30 00:04:16 -0700948void GrGLPerlinNoise::setData(const GrGLProgramDataManager& pdman, const GrDrawEffect& drawEffect) {
949 INHERITED::setData(pdman, drawEffect);
senorblancof3b50272014-06-16 10:49:58 -0700950
sugoi@google.come3b4c502013-04-05 13:47:09 +0000951 const GrPerlinNoiseEffect& turbulence = drawEffect.castEffect<GrPerlinNoiseEffect>();
952
953 const SkVector& baseFrequency = turbulence.baseFrequency();
kkinnunen7510b222014-07-30 00:04:16 -0700954 pdman.set2f(fBaseFrequencyUni, baseFrequency.fX, baseFrequency.fY);
955 pdman.set1f(fAlphaUni, SkScalarDiv(SkIntToScalar(turbulence.alpha()), SkIntToScalar(255)));
sugoi@google.come3b4c502013-04-05 13:47:09 +0000956
sugoi@google.com4775cba2013-04-17 13:46:56 +0000957 if (turbulence.stitchTiles()) {
958 const SkPerlinNoiseShader::StitchData& stitchData = turbulence.stitchData();
kkinnunen7510b222014-07-30 00:04:16 -0700959 pdman.set2f(fStitchDataUni, SkIntToScalar(stitchData.fWidth),
commit-bot@chromium.org98393202013-07-04 18:13:05 +0000960 SkIntToScalar(stitchData.fHeight));
sugoi@google.com4775cba2013-04-17 13:46:56 +0000961 }
962}
963
sugoi@google.come3b4c502013-04-05 13:47:09 +0000964/////////////////////////////////////////////////////////////////////
965
dandov9de5b512014-06-10 14:38:28 -0700966bool SkPerlinNoiseShader::asNewEffect(GrContext* context, const SkPaint& paint,
bsalomon83d081a2014-07-08 09:56:10 -0700967 const SkMatrix* externalLocalMatrix, GrColor* paintColor,
968 GrEffect** effect) const {
bsalomon49f085d2014-09-05 13:34:00 -0700969 SkASSERT(context);
dandov9de5b512014-06-10 14:38:28 -0700970
bsalomon83d081a2014-07-08 09:56:10 -0700971 *paintColor = SkColor2GrColorJustAlpha(paint.getColor());
senorblancoca6a7c22014-06-27 13:35:52 -0700972
commit-bot@chromium.org96fb7482014-05-09 20:28:11 +0000973 SkMatrix localMatrix = this->getLocalMatrix();
974 if (externalLocalMatrix) {
975 localMatrix.preConcat(*externalLocalMatrix);
976 }
977
senorblancoca6a7c22014-06-27 13:35:52 -0700978 SkMatrix matrix = context->getMatrix();
979 matrix.preConcat(localMatrix);
980
commit-bot@chromium.orgc2a0ea62013-11-06 10:08:38 +0000981 if (0 == fNumOctaves) {
982 SkColor clearColor = 0;
983 if (kFractalNoise_Type == fType) {
984 clearColor = SkColorSetARGB(paint.getAlpha() / 2, 127, 127, 127);
985 }
986 SkAutoTUnref<SkColorFilter> cf(SkColorFilter::CreateModeFilter(
987 clearColor, SkXfermode::kSrc_Mode));
bsalomon83d081a2014-07-08 09:56:10 -0700988 *effect = cf->asNewEffect(context);
dandov9de5b512014-06-10 14:38:28 -0700989 return true;
commit-bot@chromium.orgc2a0ea62013-11-06 10:08:38 +0000990 }
991
sugoi@google.come3b4c502013-04-05 13:47:09 +0000992 // Either we don't stitch tiles, either we have a valid tile size
993 SkASSERT(!fStitchTiles || !fTileSize.isEmpty());
994
senorblancoca6a7c22014-06-27 13:35:52 -0700995 SkPerlinNoiseShader::PaintingData* paintingData = SkNEW_ARGS(PaintingData, (fTileSize, fSeed, fBaseFrequencyX, fBaseFrequencyY, matrix));
sugoi@google.come3b4c502013-04-05 13:47:09 +0000996 GrTexture* permutationsTexture = GrLockAndRefCachedBitmapTexture(
senorblancoca6a7c22014-06-27 13:35:52 -0700997 context, paintingData->getPermutationsBitmap(), NULL);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000998 GrTexture* noiseTexture = GrLockAndRefCachedBitmapTexture(
senorblancoca6a7c22014-06-27 13:35:52 -0700999 context, paintingData->getNoiseBitmap(), NULL);
sugoi@google.come3b4c502013-04-05 13:47:09 +00001000
senorblancoca6a7c22014-06-27 13:35:52 -07001001 SkMatrix m = context->getMatrix();
1002 m.setTranslateX(-localMatrix.getTranslateX() + SK_Scalar1);
1003 m.setTranslateY(-localMatrix.getTranslateY() + SK_Scalar1);
bsalomon49f085d2014-09-05 13:34:00 -07001004 if ((permutationsTexture) && (noiseTexture)) {
bsalomon83d081a2014-07-08 09:56:10 -07001005 *effect = GrPerlinNoiseEffect::Create(fType,
senorblancoca6a7c22014-06-27 13:35:52 -07001006 fNumOctaves,
1007 fStitchTiles,
1008 paintingData,
1009 permutationsTexture, noiseTexture,
1010 m, paint.getAlpha());
1011 } else {
1012 SkDELETE(paintingData);
bsalomon83d081a2014-07-08 09:56:10 -07001013 *effect = NULL;
senorblancoca6a7c22014-06-27 13:35:52 -07001014 }
sugoi@google.come3b4c502013-04-05 13:47:09 +00001015
1016 // Unlock immediately, this is not great, but we don't have a way of
1017 // knowing when else to unlock it currently. TODO: Remove this when
1018 // unref becomes the unlock replacement for all types of textures.
bsalomon49f085d2014-09-05 13:34:00 -07001019 if (permutationsTexture) {
sugoi@google.come3b4c502013-04-05 13:47:09 +00001020 GrUnlockAndUnrefCachedBitmapTexture(permutationsTexture);
1021 }
bsalomon49f085d2014-09-05 13:34:00 -07001022 if (noiseTexture) {
sugoi@google.come3b4c502013-04-05 13:47:09 +00001023 GrUnlockAndUnrefCachedBitmapTexture(noiseTexture);
1024 }
1025
dandov9de5b512014-06-10 14:38:28 -07001026 return true;
sugoi@google.come3b4c502013-04-05 13:47:09 +00001027}
1028
1029#else
1030
dandov9de5b512014-06-10 14:38:28 -07001031bool SkPerlinNoiseShader::asNewEffect(GrContext* context, const SkPaint& paint,
bsalomon83d081a2014-07-08 09:56:10 -07001032 const SkMatrix* externalLocalMatrix, GrColor* paintColor,
1033 GrEffect** effect) const {
sugoi@google.come3b4c502013-04-05 13:47:09 +00001034 SkDEBUGFAIL("Should not call in GPU-less build");
dandov9de5b512014-06-10 14:38:28 -07001035 return false;
sugoi@google.come3b4c502013-04-05 13:47:09 +00001036}
1037
1038#endif
1039
commit-bot@chromium.org0f10f7b2014-03-13 18:02:17 +00001040#ifndef SK_IGNORE_TO_STRING
sugoi@google.come3b4c502013-04-05 13:47:09 +00001041void SkPerlinNoiseShader::toString(SkString* str) const {
1042 str->append("SkPerlinNoiseShader: (");
1043
1044 str->append("type: ");
1045 switch (fType) {
1046 case kFractalNoise_Type:
1047 str->append("\"fractal noise\"");
1048 break;
1049 case kTurbulence_Type:
1050 str->append("\"turbulence\"");
1051 break;
1052 default:
1053 str->append("\"unknown\"");
1054 break;
1055 }
1056 str->append(" base frequency: (");
1057 str->appendScalar(fBaseFrequencyX);
1058 str->append(", ");
1059 str->appendScalar(fBaseFrequencyY);
1060 str->append(") number of octaves: ");
1061 str->appendS32(fNumOctaves);
1062 str->append(" seed: ");
1063 str->appendScalar(fSeed);
1064 str->append(" stitch tiles: ");
1065 str->append(fStitchTiles ? "true " : "false ");
1066
1067 this->INHERITED::toString(str);
1068
1069 str->append(")");
1070}
1071#endif