blob: 88e6caddb9d6ee6f4f2aec32a505b59c10de08bf [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"
joshualittb0a8a372014-09-23 09:50:21 -070020#include "gl/GrGLProcessor.h"
joshualitt30ba4362014-08-21 20:18:45 -070021#include "gl/builders/GrGLProgramBuilder.h"
joshualittb0a8a372014-09-23 09:50:21 -070022#include "GrTBackendProcessorFactory.h"
sugoi@google.come3b4c502013-04-05 13:47:09 +000023#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
joshualittb0a8a372014-09-23 09:50:21 -0700510#include "GrTBackendProcessorFactory.h"
sugoi@google.come3b4c502013-04-05 13:47:09 +0000511
joshualittb0a8a372014-09-23 09:50:21 -0700512class GrGLPerlinNoise : public GrGLFragmentProcessor {
sugoi@google.com4775cba2013-04-17 13:46:56 +0000513public:
joshualittb0a8a372014-09-23 09:50:21 -0700514 GrGLPerlinNoise(const GrBackendProcessorFactory&,
515 const GrProcessor&);
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*,
joshualittb0a8a372014-09-23 09:50:21 -0700519 const GrFragmentProcessor&,
520 const GrProcessorKey&,
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
joshualittb0a8a372014-09-23 09:50:21 -0700526 virtual void setData(const GrGLProgramDataManager&, const GrProcessor&) SK_OVERRIDE;
sugoi@google.come3b4c502013-04-05 13:47:09 +0000527
joshualittb0a8a372014-09-23 09:50:21 -0700528 static inline void GenKey(const GrProcessor&, const GrGLCaps&, GrProcessorKeyBuilder* 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:
joshualittb0a8a372014-09-23 09:50:21 -0700540 typedef GrGLFragmentProcessor INHERITED;
sugoi@google.come3b4c502013-04-05 13:47:09 +0000541};
542
543/////////////////////////////////////////////////////////////////////
544
joshualittb0a8a372014-09-23 09:50:21 -0700545class GrPerlinNoiseEffect : public GrFragmentProcessor {
sugoi@google.come3b4c502013-04-05 13:47:09 +0000546public:
joshualittb0a8a372014-09-23 09:50:21 -0700547 static GrFragmentProcessor* 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"; }
joshualittb0a8a372014-09-23 09:50:21 -0700561 virtual const GrBackendFragmentProcessorFactory& getFactory() const SK_OVERRIDE {
562 return GrTBackendFragmentProcessorFactory<GrPerlinNoiseEffect>::getInstance();
sugoi@google.come3b4c502013-04-05 13:47:09 +0000563 }
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
joshualittb0a8a372014-09-23 09:50:21 -0700573 typedef GrGLPerlinNoise GLProcessor;
sugoi@google.come3b4c502013-04-05 13:47:09 +0000574
sugoi@google.come3b4c502013-04-05 13:47:09 +0000575private:
joshualittb0a8a372014-09-23 09:50:21 -0700576 virtual bool onIsEqual(const GrProcessor& sBase) const SK_OVERRIDE {
joshualitt49586be2014-09-16 08:21:41 -0700577 const GrPerlinNoiseEffect& s = sBase.cast<GrPerlinNoiseEffect>();
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
joshualittb0a8a372014-09-23 09:50:21 -0700608 GR_DECLARE_FRAGMENT_PROCESSOR_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:
joshualittb0a8a372014-09-23 09:50:21 -0700624 typedef GrFragmentProcessor INHERITED;
sugoi@google.come3b4c502013-04-05 13:47:09 +0000625};
626
627/////////////////////////////////////////////////////////////////////
joshualittb0a8a372014-09-23 09:50:21 -0700628GR_DEFINE_FRAGMENT_PROCESSOR_TEST(GrPerlinNoiseEffect);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000629
joshualittb0a8a372014-09-23 09:50:21 -0700630GrFragmentProcessor* 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;
joshualittb0a8a372014-09-23 09:50:21 -0700651 GrFragmentProcessor* effect;
652 SkAssertResult(shader->asFragmentProcessor(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
joshualittb0a8a372014-09-23 09:50:21 -0700659GrGLPerlinNoise::GrGLPerlinNoise(const GrBackendProcessorFactory& factory,
660 const GrProcessor& processor)
senorblancof3b50272014-06-16 10:49:58 -0700661 : INHERITED (factory)
joshualittb0a8a372014-09-23 09:50:21 -0700662 , fType(processor.cast<GrPerlinNoiseEffect>().type())
663 , fStitchTiles(processor.cast<GrPerlinNoiseEffect>().stitchTiles())
664 , fNumOctaves(processor.cast<GrPerlinNoiseEffect>().numOctaves()) {
sugoi@google.com4775cba2013-04-17 13:46:56 +0000665}
666
joshualitt30ba4362014-08-21 20:18:45 -0700667void GrGLPerlinNoise::emitCode(GrGLProgramBuilder* builder,
joshualittb0a8a372014-09-23 09:50:21 -0700668 const GrFragmentProcessor&,
669 const GrProcessorKey& key,
sugoi@google.come3b4c502013-04-05 13:47:09 +0000670 const char* outputColor,
671 const char* inputColor,
bsalomon@google.com77af6802013-10-02 13:04:56 +0000672 const TransformedCoordsArray& coords,
sugoi@google.come3b4c502013-04-05 13:47:09 +0000673 const TextureSamplerArray& samplers) {
674 sk_ignore_unused_variable(inputColor);
675
joshualitt30ba4362014-08-21 20:18:45 -0700676 GrGLFragmentShaderBuilder* fsBuilder = builder->getFragmentShaderBuilder();
677 SkString vCoords = fsBuilder->ensureFSCoords2D(coords, 0);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000678
joshualitt30ba4362014-08-21 20:18:45 -0700679 fBaseFrequencyUni = builder->addUniform(GrGLProgramBuilder::kFragment_Visibility,
sugoi@google.come3b4c502013-04-05 13:47:09 +0000680 kVec2f_GrSLType, "baseFrequency");
681 const char* baseFrequencyUni = builder->getUniformCStr(fBaseFrequencyUni);
joshualitt30ba4362014-08-21 20:18:45 -0700682 fAlphaUni = builder->addUniform(GrGLProgramBuilder::kFragment_Visibility,
sugoi@google.come3b4c502013-04-05 13:47:09 +0000683 kFloat_GrSLType, "alpha");
684 const char* alphaUni = builder->getUniformCStr(fAlphaUni);
685
686 const char* stitchDataUni = NULL;
687 if (fStitchTiles) {
joshualitt30ba4362014-08-21 20:18:45 -0700688 fStitchDataUni = builder->addUniform(GrGLProgramBuilder::kFragment_Visibility,
commit-bot@chromium.org98393202013-07-04 18:13:05 +0000689 kVec2f_GrSLType, "stitchData");
sugoi@google.come3b4c502013-04-05 13:47:09 +0000690 stitchDataUni = builder->getUniformCStr(fStitchDataUni);
691 }
692
sugoi@google.comd537af52013-06-10 13:59:25 +0000693 // There are 4 lines, so the center of each line is 1/8, 3/8, 5/8 and 7/8
694 const char* chanCoordR = "0.125";
695 const char* chanCoordG = "0.375";
696 const char* chanCoordB = "0.625";
697 const char* chanCoordA = "0.875";
698 const char* chanCoord = "chanCoord";
sugoi@google.come3b4c502013-04-05 13:47:09 +0000699 const char* stitchData = "stitchData";
700 const char* ratio = "ratio";
sugoi@google.come3b4c502013-04-05 13:47:09 +0000701 const char* noiseVec = "noiseVec";
sugoi@google.come3b4c502013-04-05 13:47:09 +0000702 const char* noiseSmooth = "noiseSmooth";
senorblancoce6a3542014-06-12 11:24:19 -0700703 const char* floorVal = "floorVal";
sugoi@google.come3b4c502013-04-05 13:47:09 +0000704 const char* fractVal = "fractVal";
705 const char* uv = "uv";
706 const char* ab = "ab";
707 const char* latticeIdx = "latticeIdx";
senorblancoce6a3542014-06-12 11:24:19 -0700708 const char* bcoords = "bcoords";
sugoi@google.come3b4c502013-04-05 13:47:09 +0000709 const char* lattice = "lattice";
sugoi@google.come3b4c502013-04-05 13:47:09 +0000710 const char* inc8bit = "0.00390625"; // 1.0 / 256.0
711 // This is the math to convert the two 16bit integer packed into rgba 8 bit input into a
712 // [-1,1] vector and perform a dot product between that vector and the provided vector.
713 const char* dotLattice = "dot(((%s.ga + %s.rb * vec2(%s)) * vec2(2.0) - vec2(1.0)), %s);";
714
sugoi@google.comd537af52013-06-10 13:59:25 +0000715 // Add noise function
716 static const GrGLShaderVar gPerlinNoiseArgs[] = {
717 GrGLShaderVar(chanCoord, kFloat_GrSLType),
commit-bot@chromium.org98393202013-07-04 18:13:05 +0000718 GrGLShaderVar(noiseVec, kVec2f_GrSLType)
sugoi@google.comd537af52013-06-10 13:59:25 +0000719 };
sugoi@google.come3b4c502013-04-05 13:47:09 +0000720
sugoi@google.comd537af52013-06-10 13:59:25 +0000721 static const GrGLShaderVar gPerlinNoiseStitchArgs[] = {
722 GrGLShaderVar(chanCoord, kFloat_GrSLType),
commit-bot@chromium.org98393202013-07-04 18:13:05 +0000723 GrGLShaderVar(noiseVec, kVec2f_GrSLType),
724 GrGLShaderVar(stitchData, kVec2f_GrSLType)
sugoi@google.comd537af52013-06-10 13:59:25 +0000725 };
sugoi@google.come3b4c502013-04-05 13:47:09 +0000726
sugoi@google.comd537af52013-06-10 13:59:25 +0000727 SkString noiseCode;
sugoi@google.come3b4c502013-04-05 13:47:09 +0000728
senorblancoce6a3542014-06-12 11:24:19 -0700729 noiseCode.appendf("\tvec4 %s;\n", floorVal);
730 noiseCode.appendf("\t%s.xy = floor(%s);\n", floorVal, noiseVec);
731 noiseCode.appendf("\t%s.zw = %s.xy + vec2(1.0);\n", floorVal, floorVal);
732 noiseCode.appendf("\tvec2 %s = fract(%s);\n", fractVal, noiseVec);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000733
734 // smooth curve : t * t * (3 - 2 * t)
senorblancoce6a3542014-06-12 11:24:19 -0700735 noiseCode.appendf("\n\tvec2 %s = %s * %s * (vec2(3.0) - vec2(2.0) * %s);",
736 noiseSmooth, fractVal, fractVal, fractVal);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000737
738 // Adjust frequencies if we're stitching tiles
739 if (fStitchTiles) {
commit-bot@chromium.org98393202013-07-04 18:13:05 +0000740 noiseCode.appendf("\n\tif(%s.x >= %s.x) { %s.x -= %s.x; }",
senorblancoce6a3542014-06-12 11:24:19 -0700741 floorVal, stitchData, floorVal, stitchData);
commit-bot@chromium.org98393202013-07-04 18:13:05 +0000742 noiseCode.appendf("\n\tif(%s.y >= %s.y) { %s.y -= %s.y; }",
senorblancoce6a3542014-06-12 11:24:19 -0700743 floorVal, stitchData, floorVal, stitchData);
744 noiseCode.appendf("\n\tif(%s.z >= %s.x) { %s.z -= %s.x; }",
745 floorVal, stitchData, floorVal, stitchData);
746 noiseCode.appendf("\n\tif(%s.w >= %s.y) { %s.w -= %s.y; }",
747 floorVal, stitchData, floorVal, stitchData);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000748 }
749
750 // Get texture coordinates and normalize
senorblancoce6a3542014-06-12 11:24:19 -0700751 noiseCode.appendf("\n\t%s = fract(floor(mod(%s, 256.0)) / vec4(256.0));\n",
752 floorVal, floorVal);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000753
754 // Get permutation for x
755 {
756 SkString xCoords("");
senorblancoce6a3542014-06-12 11:24:19 -0700757 xCoords.appendf("vec2(%s.x, 0.5)", floorVal);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000758
sugoi@google.comd537af52013-06-10 13:59:25 +0000759 noiseCode.appendf("\n\tvec2 %s;\n\t%s.x = ", latticeIdx, latticeIdx);
joshualitt30ba4362014-08-21 20:18:45 -0700760 fsBuilder->appendTextureLookup(&noiseCode, samplers[0], xCoords.c_str(), kVec2f_GrSLType);
sugoi@google.comd537af52013-06-10 13:59:25 +0000761 noiseCode.append(".r;");
sugoi@google.come3b4c502013-04-05 13:47:09 +0000762 }
763
764 // Get permutation for x + 1
765 {
766 SkString xCoords("");
senorblancoce6a3542014-06-12 11:24:19 -0700767 xCoords.appendf("vec2(%s.z, 0.5)", floorVal);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000768
sugoi@google.comd537af52013-06-10 13:59:25 +0000769 noiseCode.appendf("\n\t%s.y = ", latticeIdx);
joshualitt30ba4362014-08-21 20:18:45 -0700770 fsBuilder->appendTextureLookup(&noiseCode, samplers[0], xCoords.c_str(), kVec2f_GrSLType);
sugoi@google.comd537af52013-06-10 13:59:25 +0000771 noiseCode.append(".r;");
sugoi@google.come3b4c502013-04-05 13:47:09 +0000772 }
773
commit-bot@chromium.org344cf452013-06-17 14:19:01 +0000774#if defined(SK_BUILD_FOR_ANDROID)
775 // Android rounding for Tegra devices, like, for example: Xoom (Tegra 2), Nexus 7 (Tegra 3).
776 // The issue is that colors aren't accurate enough on Tegra devices. For example, if an 8 bit
777 // value of 124 (or 0.486275 here) is entered, we can get a texture value of 123.513725
778 // (or 0.484368 here). The following rounding operation prevents these precision issues from
779 // affecting the result of the noise by making sure that we only have multiples of 1/255.
780 // (Note that 1/255 is about 0.003921569, which is the value used here).
781 noiseCode.appendf("\n\t%s = floor(%s * vec2(255.0) + vec2(0.5)) * vec2(0.003921569);",
782 latticeIdx, latticeIdx);
783#endif
784
sugoi@google.come3b4c502013-04-05 13:47:09 +0000785 // Get (x,y) coordinates with the permutated x
senorblancoce6a3542014-06-12 11:24:19 -0700786 noiseCode.appendf("\n\tvec4 %s = fract(%s.xyxy + %s.yyww);", bcoords, latticeIdx, floorVal);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000787
sugoi@google.comd537af52013-06-10 13:59:25 +0000788 noiseCode.appendf("\n\n\tvec2 %s;", uv);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000789 // Compute u, at offset (0,0)
790 {
791 SkString latticeCoords("");
senorblancoce6a3542014-06-12 11:24:19 -0700792 latticeCoords.appendf("vec2(%s.x, %s)", bcoords, chanCoord);
sugoi@google.comd537af52013-06-10 13:59:25 +0000793 noiseCode.appendf("\n\tvec4 %s = ", lattice);
joshualitt30ba4362014-08-21 20:18:45 -0700794 fsBuilder->appendTextureLookup(&noiseCode, samplers[1], latticeCoords.c_str(),
sugoi@google.comd537af52013-06-10 13:59:25 +0000795 kVec2f_GrSLType);
796 noiseCode.appendf(".bgra;\n\t%s.x = ", uv);
797 noiseCode.appendf(dotLattice, lattice, lattice, inc8bit, fractVal);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000798 }
799
sugoi@google.comd537af52013-06-10 13:59:25 +0000800 noiseCode.appendf("\n\t%s.x -= 1.0;", fractVal);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000801 // Compute v, at offset (-1,0)
802 {
803 SkString latticeCoords("");
senorblancoce6a3542014-06-12 11:24:19 -0700804 latticeCoords.appendf("vec2(%s.y, %s)", bcoords, chanCoord);
commit-bot@chromium.org344cf452013-06-17 14:19:01 +0000805 noiseCode.append("\n\tlattice = ");
joshualitt30ba4362014-08-21 20:18:45 -0700806 fsBuilder->appendTextureLookup(&noiseCode, samplers[1], latticeCoords.c_str(),
sugoi@google.comd537af52013-06-10 13:59:25 +0000807 kVec2f_GrSLType);
808 noiseCode.appendf(".bgra;\n\t%s.y = ", uv);
809 noiseCode.appendf(dotLattice, lattice, lattice, inc8bit, fractVal);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000810 }
811
812 // Compute 'a' as a linear interpolation of 'u' and 'v'
sugoi@google.comd537af52013-06-10 13:59:25 +0000813 noiseCode.appendf("\n\tvec2 %s;", ab);
814 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 +0000815
sugoi@google.comd537af52013-06-10 13:59:25 +0000816 noiseCode.appendf("\n\t%s.y -= 1.0;", fractVal);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000817 // Compute v, at offset (-1,-1)
818 {
819 SkString latticeCoords("");
senorblancoce6a3542014-06-12 11:24:19 -0700820 latticeCoords.appendf("vec2(%s.w, %s)", bcoords, chanCoord);
commit-bot@chromium.org344cf452013-06-17 14:19:01 +0000821 noiseCode.append("\n\tlattice = ");
joshualitt30ba4362014-08-21 20:18:45 -0700822 fsBuilder->appendTextureLookup(&noiseCode, samplers[1], latticeCoords.c_str(),
sugoi@google.comd537af52013-06-10 13:59:25 +0000823 kVec2f_GrSLType);
824 noiseCode.appendf(".bgra;\n\t%s.y = ", uv);
825 noiseCode.appendf(dotLattice, lattice, lattice, inc8bit, fractVal);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000826 }
827
sugoi@google.comd537af52013-06-10 13:59:25 +0000828 noiseCode.appendf("\n\t%s.x += 1.0;", fractVal);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000829 // Compute u, at offset (0,-1)
830 {
831 SkString latticeCoords("");
senorblancoce6a3542014-06-12 11:24:19 -0700832 latticeCoords.appendf("vec2(%s.z, %s)", bcoords, chanCoord);
commit-bot@chromium.org344cf452013-06-17 14:19:01 +0000833 noiseCode.append("\n\tlattice = ");
joshualitt30ba4362014-08-21 20:18:45 -0700834 fsBuilder->appendTextureLookup(&noiseCode, samplers[1], latticeCoords.c_str(),
sugoi@google.comd537af52013-06-10 13:59:25 +0000835 kVec2f_GrSLType);
836 noiseCode.appendf(".bgra;\n\t%s.x = ", uv);
837 noiseCode.appendf(dotLattice, lattice, lattice, inc8bit, fractVal);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000838 }
839
840 // Compute 'b' as a linear interpolation of 'u' and 'v'
sugoi@google.comd537af52013-06-10 13:59:25 +0000841 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 +0000842 // Compute the noise as a linear interpolation of 'a' and 'b'
sugoi@google.comd537af52013-06-10 13:59:25 +0000843 noiseCode.appendf("\n\treturn mix(%s.x, %s.y, %s.y);\n", ab, ab, noiseSmooth);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000844
sugoi@google.comd537af52013-06-10 13:59:25 +0000845 SkString noiseFuncName;
846 if (fStitchTiles) {
joshualitt30ba4362014-08-21 20:18:45 -0700847 fsBuilder->emitFunction(kFloat_GrSLType,
commit-bot@chromium.org74a3a212013-08-30 19:43:59 +0000848 "perlinnoise", SK_ARRAY_COUNT(gPerlinNoiseStitchArgs),
849 gPerlinNoiseStitchArgs, noiseCode.c_str(), &noiseFuncName);
sugoi@google.comd537af52013-06-10 13:59:25 +0000850 } else {
joshualitt30ba4362014-08-21 20:18:45 -0700851 fsBuilder->emitFunction(kFloat_GrSLType,
commit-bot@chromium.org74a3a212013-08-30 19:43:59 +0000852 "perlinnoise", SK_ARRAY_COUNT(gPerlinNoiseArgs),
853 gPerlinNoiseArgs, noiseCode.c_str(), &noiseFuncName);
sugoi@google.comd537af52013-06-10 13:59:25 +0000854 }
sugoi@google.come3b4c502013-04-05 13:47:09 +0000855
sugoi@google.comd537af52013-06-10 13:59:25 +0000856 // There are rounding errors if the floor operation is not performed here
joshualitt30ba4362014-08-21 20:18:45 -0700857 fsBuilder->codeAppendf("\n\t\tvec2 %s = floor(%s.xy) * %s;",
senorblancoca6a7c22014-06-27 13:35:52 -0700858 noiseVec, vCoords.c_str(), baseFrequencyUni);
sugoi@google.comd537af52013-06-10 13:59:25 +0000859
860 // Clear the color accumulator
joshualitt30ba4362014-08-21 20:18:45 -0700861 fsBuilder->codeAppendf("\n\t\t%s = vec4(0.0);", outputColor);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000862
863 if (fStitchTiles) {
sugoi@google.comd537af52013-06-10 13:59:25 +0000864 // Set up TurbulenceInitial stitch values.
joshualitt30ba4362014-08-21 20:18:45 -0700865 fsBuilder->codeAppendf("\n\t\tvec2 %s = %s;", stitchData, stitchDataUni);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000866 }
sugoi@google.come3b4c502013-04-05 13:47:09 +0000867
joshualitt30ba4362014-08-21 20:18:45 -0700868 fsBuilder->codeAppendf("\n\t\tfloat %s = 1.0;", ratio);
sugoi@google.comd537af52013-06-10 13:59:25 +0000869
870 // Loop over all octaves
joshualitt30ba4362014-08-21 20:18:45 -0700871 fsBuilder->codeAppendf("\n\t\tfor (int octave = 0; octave < %d; ++octave) {", fNumOctaves);
sugoi@google.comd537af52013-06-10 13:59:25 +0000872
joshualitt30ba4362014-08-21 20:18:45 -0700873 fsBuilder->codeAppendf("\n\t\t\t%s += ", outputColor);
sugoi@google.comd537af52013-06-10 13:59:25 +0000874 if (fType != SkPerlinNoiseShader::kFractalNoise_Type) {
joshualitt30ba4362014-08-21 20:18:45 -0700875 fsBuilder->codeAppend("abs(");
sugoi@google.comd537af52013-06-10 13:59:25 +0000876 }
877 if (fStitchTiles) {
joshualitt30ba4362014-08-21 20:18:45 -0700878 fsBuilder->codeAppendf(
sugoi@google.comd537af52013-06-10 13:59:25 +0000879 "vec4(\n\t\t\t\t%s(%s, %s, %s),\n\t\t\t\t%s(%s, %s, %s),"
880 "\n\t\t\t\t%s(%s, %s, %s),\n\t\t\t\t%s(%s, %s, %s))",
881 noiseFuncName.c_str(), chanCoordR, noiseVec, stitchData,
882 noiseFuncName.c_str(), chanCoordG, noiseVec, stitchData,
883 noiseFuncName.c_str(), chanCoordB, noiseVec, stitchData,
884 noiseFuncName.c_str(), chanCoordA, noiseVec, stitchData);
885 } else {
joshualitt30ba4362014-08-21 20:18:45 -0700886 fsBuilder->codeAppendf(
sugoi@google.comd537af52013-06-10 13:59:25 +0000887 "vec4(\n\t\t\t\t%s(%s, %s),\n\t\t\t\t%s(%s, %s),"
888 "\n\t\t\t\t%s(%s, %s),\n\t\t\t\t%s(%s, %s))",
889 noiseFuncName.c_str(), chanCoordR, noiseVec,
890 noiseFuncName.c_str(), chanCoordG, noiseVec,
891 noiseFuncName.c_str(), chanCoordB, noiseVec,
892 noiseFuncName.c_str(), chanCoordA, noiseVec);
893 }
894 if (fType != SkPerlinNoiseShader::kFractalNoise_Type) {
joshualitt30ba4362014-08-21 20:18:45 -0700895 fsBuilder->codeAppendf(")"); // end of "abs("
sugoi@google.comd537af52013-06-10 13:59:25 +0000896 }
joshualitt30ba4362014-08-21 20:18:45 -0700897 fsBuilder->codeAppendf(" * %s;", ratio);
sugoi@google.comd537af52013-06-10 13:59:25 +0000898
joshualitt30ba4362014-08-21 20:18:45 -0700899 fsBuilder->codeAppendf("\n\t\t\t%s *= vec2(2.0);", noiseVec);
900 fsBuilder->codeAppendf("\n\t\t\t%s *= 0.5;", ratio);
sugoi@google.comd537af52013-06-10 13:59:25 +0000901
902 if (fStitchTiles) {
joshualitt30ba4362014-08-21 20:18:45 -0700903 fsBuilder->codeAppendf("\n\t\t\t%s *= vec2(2.0);", stitchData);
sugoi@google.comd537af52013-06-10 13:59:25 +0000904 }
joshualitt30ba4362014-08-21 20:18:45 -0700905 fsBuilder->codeAppend("\n\t\t}"); // end of the for loop on octaves
sugoi@google.come3b4c502013-04-05 13:47:09 +0000906
907 if (fType == SkPerlinNoiseShader::kFractalNoise_Type) {
908 // The value of turbulenceFunctionResult comes from ((turbulenceFunctionResult) + 1) / 2
909 // by fractalNoise and (turbulenceFunctionResult) by turbulence.
joshualitt30ba4362014-08-21 20:18:45 -0700910 fsBuilder->codeAppendf("\n\t\t%s = %s * vec4(0.5) + vec4(0.5);", outputColor, outputColor);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000911 }
912
joshualitt30ba4362014-08-21 20:18:45 -0700913 fsBuilder->codeAppendf("\n\t\t%s.a *= %s;", outputColor, alphaUni);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000914
915 // Clamp values
joshualitt30ba4362014-08-21 20:18:45 -0700916 fsBuilder->codeAppendf("\n\t\t%s = clamp(%s, 0.0, 1.0);", outputColor, outputColor);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000917
918 // Pre-multiply the result
joshualitt30ba4362014-08-21 20:18:45 -0700919 fsBuilder->codeAppendf("\n\t\t%s = vec4(%s.rgb * %s.aaa, %s.a);\n",
sugoi@google.come3b4c502013-04-05 13:47:09 +0000920 outputColor, outputColor, outputColor, outputColor);
921}
922
joshualittb0a8a372014-09-23 09:50:21 -0700923void GrGLPerlinNoise::GenKey(const GrProcessor& processor, const GrGLCaps&,
924 GrProcessorKeyBuilder* b) {
925 const GrPerlinNoiseEffect& turbulence = processor.cast<GrPerlinNoiseEffect>();
sugoi@google.come3b4c502013-04-05 13:47:09 +0000926
bsalomon63e99f72014-07-21 08:03:14 -0700927 uint32_t key = turbulence.numOctaves();
sugoi@google.come3b4c502013-04-05 13:47:09 +0000928
929 key = key << 3; // Make room for next 3 bits
930
931 switch (turbulence.type()) {
932 case SkPerlinNoiseShader::kFractalNoise_Type:
933 key |= 0x1;
934 break;
935 case SkPerlinNoiseShader::kTurbulence_Type:
936 key |= 0x2;
937 break;
938 default:
939 // leave key at 0
940 break;
941 }
942
943 if (turbulence.stitchTiles()) {
944 key |= 0x4; // Flip the 3rd bit if tile stitching is on
945 }
946
bsalomon63e99f72014-07-21 08:03:14 -0700947 b->add32(key);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000948}
949
joshualittb0a8a372014-09-23 09:50:21 -0700950void GrGLPerlinNoise::setData(const GrGLProgramDataManager& pdman, const GrProcessor& processor) {
951 INHERITED::setData(pdman, processor);
senorblancof3b50272014-06-16 10:49:58 -0700952
joshualittb0a8a372014-09-23 09:50:21 -0700953 const GrPerlinNoiseEffect& turbulence = processor.cast<GrPerlinNoiseEffect>();
sugoi@google.come3b4c502013-04-05 13:47:09 +0000954
955 const SkVector& baseFrequency = turbulence.baseFrequency();
kkinnunen7510b222014-07-30 00:04:16 -0700956 pdman.set2f(fBaseFrequencyUni, baseFrequency.fX, baseFrequency.fY);
957 pdman.set1f(fAlphaUni, SkScalarDiv(SkIntToScalar(turbulence.alpha()), SkIntToScalar(255)));
sugoi@google.come3b4c502013-04-05 13:47:09 +0000958
sugoi@google.com4775cba2013-04-17 13:46:56 +0000959 if (turbulence.stitchTiles()) {
960 const SkPerlinNoiseShader::StitchData& stitchData = turbulence.stitchData();
kkinnunen7510b222014-07-30 00:04:16 -0700961 pdman.set2f(fStitchDataUni, SkIntToScalar(stitchData.fWidth),
commit-bot@chromium.org98393202013-07-04 18:13:05 +0000962 SkIntToScalar(stitchData.fHeight));
sugoi@google.com4775cba2013-04-17 13:46:56 +0000963 }
964}
965
sugoi@google.come3b4c502013-04-05 13:47:09 +0000966/////////////////////////////////////////////////////////////////////
967
joshualittb0a8a372014-09-23 09:50:21 -0700968bool SkPerlinNoiseShader::asFragmentProcessor(GrContext* context, const SkPaint& paint,
969 const SkMatrix* externalLocalMatrix,
970 GrColor* paintColor, GrFragmentProcessor** fp) const {
bsalomon49f085d2014-09-05 13:34:00 -0700971 SkASSERT(context);
dandov9de5b512014-06-10 14:38:28 -0700972
bsalomon83d081a2014-07-08 09:56:10 -0700973 *paintColor = SkColor2GrColorJustAlpha(paint.getColor());
senorblancoca6a7c22014-06-27 13:35:52 -0700974
commit-bot@chromium.org96fb7482014-05-09 20:28:11 +0000975 SkMatrix localMatrix = this->getLocalMatrix();
976 if (externalLocalMatrix) {
977 localMatrix.preConcat(*externalLocalMatrix);
978 }
979
senorblancoca6a7c22014-06-27 13:35:52 -0700980 SkMatrix matrix = context->getMatrix();
981 matrix.preConcat(localMatrix);
982
commit-bot@chromium.orgc2a0ea62013-11-06 10:08:38 +0000983 if (0 == fNumOctaves) {
984 SkColor clearColor = 0;
985 if (kFractalNoise_Type == fType) {
986 clearColor = SkColorSetARGB(paint.getAlpha() / 2, 127, 127, 127);
987 }
988 SkAutoTUnref<SkColorFilter> cf(SkColorFilter::CreateModeFilter(
989 clearColor, SkXfermode::kSrc_Mode));
joshualittb0a8a372014-09-23 09:50:21 -0700990 *fp = cf->asFragmentProcessor(context);
dandov9de5b512014-06-10 14:38:28 -0700991 return true;
commit-bot@chromium.orgc2a0ea62013-11-06 10:08:38 +0000992 }
993
sugoi@google.come3b4c502013-04-05 13:47:09 +0000994 // Either we don't stitch tiles, either we have a valid tile size
995 SkASSERT(!fStitchTiles || !fTileSize.isEmpty());
996
joshualittb0a8a372014-09-23 09:50:21 -0700997 SkPerlinNoiseShader::PaintingData* paintingData =
998 SkNEW_ARGS(PaintingData, (fTileSize, fSeed, fBaseFrequencyX, fBaseFrequencyY, matrix));
sugoi@google.come3b4c502013-04-05 13:47:09 +0000999 GrTexture* permutationsTexture = GrLockAndRefCachedBitmapTexture(
senorblancoca6a7c22014-06-27 13:35:52 -07001000 context, paintingData->getPermutationsBitmap(), NULL);
sugoi@google.come3b4c502013-04-05 13:47:09 +00001001 GrTexture* noiseTexture = GrLockAndRefCachedBitmapTexture(
senorblancoca6a7c22014-06-27 13:35:52 -07001002 context, paintingData->getNoiseBitmap(), NULL);
sugoi@google.come3b4c502013-04-05 13:47:09 +00001003
senorblancoca6a7c22014-06-27 13:35:52 -07001004 SkMatrix m = context->getMatrix();
1005 m.setTranslateX(-localMatrix.getTranslateX() + SK_Scalar1);
1006 m.setTranslateY(-localMatrix.getTranslateY() + SK_Scalar1);
bsalomon49f085d2014-09-05 13:34:00 -07001007 if ((permutationsTexture) && (noiseTexture)) {
joshualittb0a8a372014-09-23 09:50:21 -07001008 *fp = GrPerlinNoiseEffect::Create(fType,
1009 fNumOctaves,
1010 fStitchTiles,
1011 paintingData,
1012 permutationsTexture, noiseTexture,
1013 m, paint.getAlpha());
senorblancoca6a7c22014-06-27 13:35:52 -07001014 } else {
1015 SkDELETE(paintingData);
joshualittb0a8a372014-09-23 09:50:21 -07001016 *fp = NULL;
senorblancoca6a7c22014-06-27 13:35:52 -07001017 }
sugoi@google.come3b4c502013-04-05 13:47:09 +00001018
1019 // Unlock immediately, this is not great, but we don't have a way of
1020 // knowing when else to unlock it currently. TODO: Remove this when
1021 // unref becomes the unlock replacement for all types of textures.
bsalomon49f085d2014-09-05 13:34:00 -07001022 if (permutationsTexture) {
sugoi@google.come3b4c502013-04-05 13:47:09 +00001023 GrUnlockAndUnrefCachedBitmapTexture(permutationsTexture);
1024 }
bsalomon49f085d2014-09-05 13:34:00 -07001025 if (noiseTexture) {
sugoi@google.come3b4c502013-04-05 13:47:09 +00001026 GrUnlockAndUnrefCachedBitmapTexture(noiseTexture);
1027 }
1028
dandov9de5b512014-06-10 14:38:28 -07001029 return true;
sugoi@google.come3b4c502013-04-05 13:47:09 +00001030}
1031
1032#else
1033
joshualittb0a8a372014-09-23 09:50:21 -07001034bool SkPerlinNoiseShader::asFragmentProcessor(GrContext*, const SkPaint&, const SkMatrix*, GrColor*,
1035 GrFragmentProcessor**) const {
sugoi@google.come3b4c502013-04-05 13:47:09 +00001036 SkDEBUGFAIL("Should not call in GPU-less build");
dandov9de5b512014-06-10 14:38:28 -07001037 return false;
sugoi@google.come3b4c502013-04-05 13:47:09 +00001038}
1039
1040#endif
1041
commit-bot@chromium.org0f10f7b2014-03-13 18:02:17 +00001042#ifndef SK_IGNORE_TO_STRING
sugoi@google.come3b4c502013-04-05 13:47:09 +00001043void SkPerlinNoiseShader::toString(SkString* str) const {
1044 str->append("SkPerlinNoiseShader: (");
1045
1046 str->append("type: ");
1047 switch (fType) {
1048 case kFractalNoise_Type:
1049 str->append("\"fractal noise\"");
1050 break;
1051 case kTurbulence_Type:
1052 str->append("\"turbulence\"");
1053 break;
1054 default:
1055 str->append("\"unknown\"");
1056 break;
1057 }
1058 str->append(" base frequency: (");
1059 str->appendScalar(fBaseFrequencyX);
1060 str->append(", ");
1061 str->appendScalar(fBaseFrequencyY);
1062 str->append(") number of octaves: ");
1063 str->appendS32(fNumOctaves);
1064 str->append(" seed: ");
1065 str->appendScalar(fSeed);
1066 str->append(" stitch tiles: ");
1067 str->append(fStitchTiles ? "true " : "false ");
1068
1069 this->INHERITED::toString(str);
1070
1071 str->append(")");
1072}
1073#endif