blob: 5f400b57cf19dcf0404281cd03ec4bf2efd5f746 [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"
egdaniel605dd0f2014-11-12 08:35:25 -080020#include "GrInvariantOutput.h"
joshualittb0a8a372014-09-23 09:50:21 -070021#include "gl/GrGLProcessor.h"
joshualitt30ba4362014-08-21 20:18:45 -070022#include "gl/builders/GrGLProgramBuilder.h"
joshualittb0a8a372014-09-23 09:50:21 -070023#include "GrTBackendProcessorFactory.h"
sugoi@google.come3b4c502013-04-05 13:47:09 +000024#include "SkGr.h"
25#endif
26
27static const int kBlockSize = 256;
28static const int kBlockMask = kBlockSize - 1;
29static const int kPerlinNoise = 4096;
30static const int kRandMaximum = SK_MaxS32; // 2**31 - 1
31
32namespace {
33
34// noiseValue is the color component's value (or color)
35// limitValue is the maximum perlin noise array index value allowed
36// newValue is the current noise dimension (either width or height)
37inline int checkNoise(int noiseValue, int limitValue, int newValue) {
38 // If the noise value would bring us out of bounds of the current noise array while we are
39 // stiching noise tiles together, wrap the noise around the current dimension of the noise to
40 // stay within the array bounds in a continuous fashion (so that tiling lines are not visible)
41 if (noiseValue >= limitValue) {
42 noiseValue -= newValue;
43 }
sugoi@google.come3b4c502013-04-05 13:47:09 +000044 return noiseValue;
45}
46
47inline SkScalar smoothCurve(SkScalar t) {
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +000048 static const SkScalar SK_Scalar3 = 3.0f;
sugoi@google.come3b4c502013-04-05 13:47:09 +000049
50 // returns t * t * (3 - 2 * t)
51 return SkScalarMul(SkScalarSquare(t), SK_Scalar3 - 2 * t);
52}
53
commit-bot@chromium.orgce33d602013-11-25 21:46:31 +000054bool perlin_noise_type_is_valid(SkPerlinNoiseShader::Type type) {
55 return (SkPerlinNoiseShader::kFractalNoise_Type == type) ||
56 (SkPerlinNoiseShader::kTurbulence_Type == type);
57}
58
sugoi@google.come3b4c502013-04-05 13:47:09 +000059} // end namespace
60
61struct SkPerlinNoiseShader::StitchData {
62 StitchData()
63 : fWidth(0)
64 , fWrapX(0)
65 , fHeight(0)
66 , fWrapY(0)
67 {}
68
69 bool operator==(const StitchData& other) const {
70 return fWidth == other.fWidth &&
71 fWrapX == other.fWrapX &&
72 fHeight == other.fHeight &&
73 fWrapY == other.fWrapY;
74 }
75
76 int fWidth; // How much to subtract to wrap for stitching.
77 int fWrapX; // Minimum value to wrap.
78 int fHeight;
79 int fWrapY;
80};
81
82struct SkPerlinNoiseShader::PaintingData {
commit-bot@chromium.orgfd5c9a62014-03-06 15:13:53 +000083 PaintingData(const SkISize& tileSize, SkScalar seed,
senorblancoca6a7c22014-06-27 13:35:52 -070084 SkScalar baseFrequencyX, SkScalar baseFrequencyY,
85 const SkMatrix& matrix)
sugoi@google.come3b4c502013-04-05 13:47:09 +000086 {
senorblancoca6a7c22014-06-27 13:35:52 -070087 SkVector wavelength = SkVector::Make(SkScalarInvert(baseFrequencyX),
88 SkScalarInvert(baseFrequencyY));
89 matrix.mapVectors(&wavelength, 1);
90 fBaseFrequency.fX = SkScalarInvert(wavelength.fX);
91 fBaseFrequency.fY = SkScalarInvert(wavelength.fY);
92 SkVector sizeVec = SkVector::Make(SkIntToScalar(tileSize.fWidth),
93 SkIntToScalar(tileSize.fHeight));
94 matrix.mapVectors(&sizeVec, 1);
95 fTileSize.fWidth = SkScalarRoundToInt(sizeVec.fX);
96 fTileSize.fHeight = SkScalarRoundToInt(sizeVec.fY);
commit-bot@chromium.orgfd5c9a62014-03-06 15:13:53 +000097 this->init(seed);
98 if (!fTileSize.isEmpty()) {
99 this->stitch();
100 }
101
senorblancof3b50272014-06-16 10:49:58 -0700102#if SK_SUPPORT_GPU
commit-bot@chromium.orga3264e52014-05-30 13:26:10 +0000103 fPermutationsBitmap.setInfo(SkImageInfo::MakeA8(kBlockSize, 1));
commit-bot@chromium.orgfd5c9a62014-03-06 15:13:53 +0000104 fPermutationsBitmap.setPixels(fLatticeSelector);
105
commit-bot@chromium.orga3264e52014-05-30 13:26:10 +0000106 fNoiseBitmap.setInfo(SkImageInfo::MakeN32Premul(kBlockSize, 4));
commit-bot@chromium.orgfd5c9a62014-03-06 15:13:53 +0000107 fNoiseBitmap.setPixels(fNoise[0][0]);
108#endif
sugoi@google.come3b4c502013-04-05 13:47:09 +0000109 }
110
111 int fSeed;
112 uint8_t fLatticeSelector[kBlockSize];
113 uint16_t fNoise[4][kBlockSize][2];
114 SkPoint fGradient[4][kBlockSize];
115 SkISize fTileSize;
116 SkVector fBaseFrequency;
117 StitchData fStitchDataInit;
118
119private:
120
senorblancof3b50272014-06-16 10:49:58 -0700121#if SK_SUPPORT_GPU
commit-bot@chromium.orgfd5c9a62014-03-06 15:13:53 +0000122 SkBitmap fPermutationsBitmap;
123 SkBitmap fNoiseBitmap;
124#endif
sugoi@google.come3b4c502013-04-05 13:47:09 +0000125
126 inline int random() {
127 static const int gRandAmplitude = 16807; // 7**5; primitive root of m
128 static const int gRandQ = 127773; // m / a
129 static const int gRandR = 2836; // m % a
130
131 int result = gRandAmplitude * (fSeed % gRandQ) - gRandR * (fSeed / gRandQ);
132 if (result <= 0)
133 result += kRandMaximum;
134 fSeed = result;
135 return result;
136 }
137
commit-bot@chromium.orgfd5c9a62014-03-06 15:13:53 +0000138 // Only called once. Could be part of the constructor.
sugoi@google.come3b4c502013-04-05 13:47:09 +0000139 void init(SkScalar seed)
140 {
141 static const SkScalar gInvBlockSizef = SkScalarInvert(SkIntToScalar(kBlockSize));
142
senorblanco@chromium.org857e3202014-01-09 17:41:42 +0000143 // According to the SVG spec, we must truncate (not round) the seed value.
144 fSeed = SkScalarTruncToInt(seed);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000145 // The seed value clamp to the range [1, kRandMaximum - 1].
sugoi@google.come3b4c502013-04-05 13:47:09 +0000146 if (fSeed <= 0) {
147 fSeed = -(fSeed % (kRandMaximum - 1)) + 1;
148 }
149 if (fSeed > kRandMaximum - 1) {
150 fSeed = kRandMaximum - 1;
151 }
152 for (int channel = 0; channel < 4; ++channel) {
153 for (int i = 0; i < kBlockSize; ++i) {
154 fLatticeSelector[i] = i;
155 fNoise[channel][i][0] = (random() % (2 * kBlockSize));
156 fNoise[channel][i][1] = (random() % (2 * kBlockSize));
157 }
158 }
159 for (int i = kBlockSize - 1; i > 0; --i) {
160 int k = fLatticeSelector[i];
161 int j = random() % kBlockSize;
162 SkASSERT(j >= 0);
163 SkASSERT(j < kBlockSize);
164 fLatticeSelector[i] = fLatticeSelector[j];
165 fLatticeSelector[j] = k;
166 }
167
168 // Perform the permutations now
169 {
170 // Copy noise data
171 uint16_t noise[4][kBlockSize][2];
172 for (int i = 0; i < kBlockSize; ++i) {
173 for (int channel = 0; channel < 4; ++channel) {
174 for (int j = 0; j < 2; ++j) {
175 noise[channel][i][j] = fNoise[channel][i][j];
176 }
177 }
178 }
179 // Do permutations on noise data
180 for (int i = 0; i < kBlockSize; ++i) {
181 for (int channel = 0; channel < 4; ++channel) {
182 for (int j = 0; j < 2; ++j) {
183 fNoise[channel][i][j] = noise[channel][fLatticeSelector[i]][j];
184 }
185 }
186 }
187 }
188
189 // Half of the largest possible value for 16 bit unsigned int
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +0000190 static const SkScalar gHalfMax16bits = 32767.5f;
sugoi@google.come3b4c502013-04-05 13:47:09 +0000191
192 // Compute gradients from permutated noise data
193 for (int channel = 0; channel < 4; ++channel) {
194 for (int i = 0; i < kBlockSize; ++i) {
195 fGradient[channel][i] = SkPoint::Make(
196 SkScalarMul(SkIntToScalar(fNoise[channel][i][0] - kBlockSize),
197 gInvBlockSizef),
skia.committer@gmail.comcff02432013-04-06 07:01:10 +0000198 SkScalarMul(SkIntToScalar(fNoise[channel][i][1] - kBlockSize),
sugoi@google.come3b4c502013-04-05 13:47:09 +0000199 gInvBlockSizef));
200 fGradient[channel][i].normalize();
201 // Put the normalized gradient back into the noise data
202 fNoise[channel][i][0] = SkScalarRoundToInt(SkScalarMul(
sugoi@google.comd537af52013-06-10 13:59:25 +0000203 fGradient[channel][i].fX + SK_Scalar1, gHalfMax16bits));
sugoi@google.come3b4c502013-04-05 13:47:09 +0000204 fNoise[channel][i][1] = SkScalarRoundToInt(SkScalarMul(
sugoi@google.comd537af52013-06-10 13:59:25 +0000205 fGradient[channel][i].fY + SK_Scalar1, gHalfMax16bits));
sugoi@google.come3b4c502013-04-05 13:47:09 +0000206 }
207 }
sugoi@google.come3b4c502013-04-05 13:47:09 +0000208 }
209
commit-bot@chromium.orgfd5c9a62014-03-06 15:13:53 +0000210 // Only called once. Could be part of the constructor.
sugoi@google.come3b4c502013-04-05 13:47:09 +0000211 void stitch() {
212 SkScalar tileWidth = SkIntToScalar(fTileSize.width());
213 SkScalar tileHeight = SkIntToScalar(fTileSize.height());
214 SkASSERT(tileWidth > 0 && tileHeight > 0);
215 // When stitching tiled turbulence, the frequencies must be adjusted
216 // so that the tile borders will be continuous.
217 if (fBaseFrequency.fX) {
reed@google.com8015cdd2013-12-18 15:49:32 +0000218 SkScalar lowFrequencx =
219 SkScalarFloorToScalar(tileWidth * fBaseFrequency.fX) / tileWidth;
220 SkScalar highFrequencx =
221 SkScalarCeilToScalar(tileWidth * fBaseFrequency.fX) / tileWidth;
sugoi@google.come3b4c502013-04-05 13:47:09 +0000222 // BaseFrequency should be non-negative according to the standard.
skia.committer@gmail.comcff02432013-04-06 07:01:10 +0000223 if (SkScalarDiv(fBaseFrequency.fX, lowFrequencx) <
sugoi@google.come3b4c502013-04-05 13:47:09 +0000224 SkScalarDiv(highFrequencx, fBaseFrequency.fX)) {
225 fBaseFrequency.fX = lowFrequencx;
226 } else {
227 fBaseFrequency.fX = highFrequencx;
228 }
229 }
230 if (fBaseFrequency.fY) {
reed@google.com8015cdd2013-12-18 15:49:32 +0000231 SkScalar lowFrequency =
232 SkScalarFloorToScalar(tileHeight * fBaseFrequency.fY) / tileHeight;
233 SkScalar highFrequency =
234 SkScalarCeilToScalar(tileHeight * fBaseFrequency.fY) / tileHeight;
skia.committer@gmail.comcff02432013-04-06 07:01:10 +0000235 if (SkScalarDiv(fBaseFrequency.fY, lowFrequency) <
sugoi@google.come3b4c502013-04-05 13:47:09 +0000236 SkScalarDiv(highFrequency, fBaseFrequency.fY)) {
237 fBaseFrequency.fY = lowFrequency;
238 } else {
239 fBaseFrequency.fY = highFrequency;
240 }
241 }
242 // Set up TurbulenceInitial stitch values.
243 fStitchDataInit.fWidth =
reed@google.com8015cdd2013-12-18 15:49:32 +0000244 SkScalarRoundToInt(tileWidth * fBaseFrequency.fX);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000245 fStitchDataInit.fWrapX = kPerlinNoise + fStitchDataInit.fWidth;
246 fStitchDataInit.fHeight =
reed@google.com8015cdd2013-12-18 15:49:32 +0000247 SkScalarRoundToInt(tileHeight * fBaseFrequency.fY);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000248 fStitchDataInit.fWrapY = kPerlinNoise + fStitchDataInit.fHeight;
249 }
250
commit-bot@chromium.orgfd5c9a62014-03-06 15:13:53 +0000251public:
sugoi@google.come3b4c502013-04-05 13:47:09 +0000252
senorblancof3b50272014-06-16 10:49:58 -0700253#if SK_SUPPORT_GPU
commit-bot@chromium.orgfd5c9a62014-03-06 15:13:53 +0000254 const SkBitmap& getPermutationsBitmap() const { return fPermutationsBitmap; }
255
256 const SkBitmap& getNoiseBitmap() const { return fNoiseBitmap; }
257#endif
sugoi@google.come3b4c502013-04-05 13:47:09 +0000258};
259
260SkShader* SkPerlinNoiseShader::CreateFractalNoise(SkScalar baseFrequencyX, SkScalar baseFrequencyY,
261 int numOctaves, SkScalar seed,
262 const SkISize* tileSize) {
263 return SkNEW_ARGS(SkPerlinNoiseShader, (kFractalNoise_Type, baseFrequencyX, baseFrequencyY,
264 numOctaves, seed, tileSize));
265}
266
commit-bot@chromium.org9fbbcca2014-04-01 16:09:37 +0000267SkShader* SkPerlinNoiseShader::CreateTurbulence(SkScalar baseFrequencyX, SkScalar baseFrequencyY,
sugoi@google.come3b4c502013-04-05 13:47:09 +0000268 int numOctaves, SkScalar seed,
269 const SkISize* tileSize) {
270 return SkNEW_ARGS(SkPerlinNoiseShader, (kTurbulence_Type, baseFrequencyX, baseFrequencyY,
271 numOctaves, seed, tileSize));
272}
273
274SkPerlinNoiseShader::SkPerlinNoiseShader(SkPerlinNoiseShader::Type type,
275 SkScalar baseFrequencyX,
276 SkScalar baseFrequencyY,
277 int numOctaves,
278 SkScalar seed,
279 const SkISize* tileSize)
280 : fType(type)
281 , fBaseFrequencyX(baseFrequencyX)
282 , fBaseFrequencyY(baseFrequencyY)
commit-bot@chromium.orgce33d602013-11-25 21:46:31 +0000283 , fNumOctaves(numOctaves > 255 ? 255 : numOctaves/*[0,255] octaves allowed*/)
sugoi@google.come3b4c502013-04-05 13:47:09 +0000284 , fSeed(seed)
commit-bot@chromium.orgfd5c9a62014-03-06 15:13:53 +0000285 , fTileSize(NULL == tileSize ? SkISize::Make(0, 0) : *tileSize)
286 , fStitchTiles(!fTileSize.isEmpty())
sugoi@google.come3b4c502013-04-05 13:47:09 +0000287{
288 SkASSERT(numOctaves >= 0 && numOctaves < 256);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000289}
290
reed9fa60da2014-08-21 07:59:51 -0700291#ifdef SK_SUPPORT_LEGACY_DEEPFLATTENING
292SkPerlinNoiseShader::SkPerlinNoiseShader(SkReadBuffer& buffer) : INHERITED(buffer) {
sugoi@google.come3b4c502013-04-05 13:47:09 +0000293 fType = (SkPerlinNoiseShader::Type) buffer.readInt();
294 fBaseFrequencyX = buffer.readScalar();
295 fBaseFrequencyY = buffer.readScalar();
296 fNumOctaves = buffer.readInt();
297 fSeed = buffer.readScalar();
298 fStitchTiles = buffer.readBool();
299 fTileSize.fWidth = buffer.readInt();
300 fTileSize.fHeight = buffer.readInt();
commit-bot@chromium.orgce33d602013-11-25 21:46:31 +0000301 buffer.validate(perlin_noise_type_is_valid(fType) &&
commit-bot@chromium.orgfd5c9a62014-03-06 15:13:53 +0000302 (fNumOctaves >= 0) && (fNumOctaves <= 255) &&
303 (fStitchTiles != fTileSize.isEmpty()));
sugoi@google.come3b4c502013-04-05 13:47:09 +0000304}
reed9fa60da2014-08-21 07:59:51 -0700305#endif
sugoi@google.come3b4c502013-04-05 13:47:09 +0000306
307SkPerlinNoiseShader::~SkPerlinNoiseShader() {
sugoi@google.come3b4c502013-04-05 13:47:09 +0000308}
309
reed9fa60da2014-08-21 07:59:51 -0700310SkFlattenable* SkPerlinNoiseShader::CreateProc(SkReadBuffer& buffer) {
311 Type type = (Type)buffer.readInt();
312 SkScalar freqX = buffer.readScalar();
313 SkScalar freqY = buffer.readScalar();
314 int octaves = buffer.readInt();
315 SkScalar seed = buffer.readScalar();
316 SkISize tileSize;
317 tileSize.fWidth = buffer.readInt();
318 tileSize.fHeight = buffer.readInt();
319
320 switch (type) {
321 case kFractalNoise_Type:
322 return SkPerlinNoiseShader::CreateFractalNoise(freqX, freqY, octaves, seed, &tileSize);
323 case kTurbulence_Type:
324 return SkPerlinNoiseShader::CreateTubulence(freqX, freqY, octaves, seed, &tileSize);
325 default:
326 return NULL;
327 }
328}
329
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +0000330void SkPerlinNoiseShader::flatten(SkWriteBuffer& buffer) const {
sugoi@google.come3b4c502013-04-05 13:47:09 +0000331 buffer.writeInt((int) fType);
332 buffer.writeScalar(fBaseFrequencyX);
333 buffer.writeScalar(fBaseFrequencyY);
334 buffer.writeInt(fNumOctaves);
335 buffer.writeScalar(fSeed);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000336 buffer.writeInt(fTileSize.fWidth);
337 buffer.writeInt(fTileSize.fHeight);
338}
339
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000340SkScalar SkPerlinNoiseShader::PerlinNoiseShaderContext::noise2D(
senorblancoca6a7c22014-06-27 13:35:52 -0700341 int channel, const StitchData& stitchData, const SkPoint& noiseVector) const {
sugoi@google.come3b4c502013-04-05 13:47:09 +0000342 struct Noise {
343 int noisePositionIntegerValue;
senorblancoce6a3542014-06-12 11:24:19 -0700344 int nextNoisePositionIntegerValue;
sugoi@google.come3b4c502013-04-05 13:47:09 +0000345 SkScalar noisePositionFractionValue;
346 Noise(SkScalar component)
347 {
348 SkScalar position = component + kPerlinNoise;
349 noisePositionIntegerValue = SkScalarFloorToInt(position);
350 noisePositionFractionValue = position - SkIntToScalar(noisePositionIntegerValue);
senorblancoce6a3542014-06-12 11:24:19 -0700351 nextNoisePositionIntegerValue = noisePositionIntegerValue + 1;
sugoi@google.come3b4c502013-04-05 13:47:09 +0000352 }
353 };
354 Noise noiseX(noiseVector.x());
355 Noise noiseY(noiseVector.y());
356 SkScalar u, v;
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000357 const SkPerlinNoiseShader& perlinNoiseShader = static_cast<const SkPerlinNoiseShader&>(fShader);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000358 // If stitching, adjust lattice points accordingly.
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000359 if (perlinNoiseShader.fStitchTiles) {
sugoi@google.come3b4c502013-04-05 13:47:09 +0000360 noiseX.noisePositionIntegerValue =
361 checkNoise(noiseX.noisePositionIntegerValue, stitchData.fWrapX, stitchData.fWidth);
362 noiseY.noisePositionIntegerValue =
363 checkNoise(noiseY.noisePositionIntegerValue, stitchData.fWrapY, stitchData.fHeight);
senorblancoce6a3542014-06-12 11:24:19 -0700364 noiseX.nextNoisePositionIntegerValue =
365 checkNoise(noiseX.nextNoisePositionIntegerValue, stitchData.fWrapX, stitchData.fWidth);
366 noiseY.nextNoisePositionIntegerValue =
367 checkNoise(noiseY.nextNoisePositionIntegerValue, stitchData.fWrapY, stitchData.fHeight);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000368 }
369 noiseX.noisePositionIntegerValue &= kBlockMask;
370 noiseY.noisePositionIntegerValue &= kBlockMask;
senorblancoce6a3542014-06-12 11:24:19 -0700371 noiseX.nextNoisePositionIntegerValue &= kBlockMask;
372 noiseY.nextNoisePositionIntegerValue &= kBlockMask;
373 int i =
senorblancoca6a7c22014-06-27 13:35:52 -0700374 fPaintingData->fLatticeSelector[noiseX.noisePositionIntegerValue];
senorblancoce6a3542014-06-12 11:24:19 -0700375 int j =
senorblancoca6a7c22014-06-27 13:35:52 -0700376 fPaintingData->fLatticeSelector[noiseX.nextNoisePositionIntegerValue];
senorblancoce6a3542014-06-12 11:24:19 -0700377 int b00 = (i + noiseY.noisePositionIntegerValue) & kBlockMask;
378 int b10 = (j + noiseY.noisePositionIntegerValue) & kBlockMask;
379 int b01 = (i + noiseY.nextNoisePositionIntegerValue) & kBlockMask;
380 int b11 = (j + noiseY.nextNoisePositionIntegerValue) & kBlockMask;
sugoi@google.come3b4c502013-04-05 13:47:09 +0000381 SkScalar sx = smoothCurve(noiseX.noisePositionFractionValue);
382 SkScalar sy = smoothCurve(noiseY.noisePositionFractionValue);
383 // This is taken 1:1 from SVG spec: http://www.w3.org/TR/SVG11/filters.html#feTurbulenceElement
384 SkPoint fractionValue = SkPoint::Make(noiseX.noisePositionFractionValue,
385 noiseY.noisePositionFractionValue); // Offset (0,0)
senorblancoca6a7c22014-06-27 13:35:52 -0700386 u = fPaintingData->fGradient[channel][b00].dot(fractionValue);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000387 fractionValue.fX -= SK_Scalar1; // Offset (-1,0)
senorblancoca6a7c22014-06-27 13:35:52 -0700388 v = fPaintingData->fGradient[channel][b10].dot(fractionValue);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000389 SkScalar a = SkScalarInterp(u, v, sx);
390 fractionValue.fY -= SK_Scalar1; // Offset (-1,-1)
senorblancoca6a7c22014-06-27 13:35:52 -0700391 v = fPaintingData->fGradient[channel][b11].dot(fractionValue);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000392 fractionValue.fX = noiseX.noisePositionFractionValue; // Offset (0,-1)
senorblancoca6a7c22014-06-27 13:35:52 -0700393 u = fPaintingData->fGradient[channel][b01].dot(fractionValue);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000394 SkScalar b = SkScalarInterp(u, v, sx);
395 return SkScalarInterp(a, b, sy);
396}
397
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000398SkScalar SkPerlinNoiseShader::PerlinNoiseShaderContext::calculateTurbulenceValueForPoint(
senorblancoca6a7c22014-06-27 13:35:52 -0700399 int channel, StitchData& stitchData, const SkPoint& point) const {
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000400 const SkPerlinNoiseShader& perlinNoiseShader = static_cast<const SkPerlinNoiseShader&>(fShader);
401 if (perlinNoiseShader.fStitchTiles) {
sugoi@google.come3b4c502013-04-05 13:47:09 +0000402 // Set up TurbulenceInitial stitch values.
senorblancoca6a7c22014-06-27 13:35:52 -0700403 stitchData = fPaintingData->fStitchDataInit;
sugoi@google.come3b4c502013-04-05 13:47:09 +0000404 }
405 SkScalar turbulenceFunctionResult = 0;
senorblancoca6a7c22014-06-27 13:35:52 -0700406 SkPoint noiseVector(SkPoint::Make(SkScalarMul(point.x(), fPaintingData->fBaseFrequency.fX),
407 SkScalarMul(point.y(), fPaintingData->fBaseFrequency.fY)));
sugoi@google.come3b4c502013-04-05 13:47:09 +0000408 SkScalar ratio = SK_Scalar1;
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000409 for (int octave = 0; octave < perlinNoiseShader.fNumOctaves; ++octave) {
senorblancoca6a7c22014-06-27 13:35:52 -0700410 SkScalar noise = noise2D(channel, stitchData, noiseVector);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000411 turbulenceFunctionResult += SkScalarDiv(
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000412 (perlinNoiseShader.fType == kFractalNoise_Type) ? noise : SkScalarAbs(noise), ratio);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000413 noiseVector.fX *= 2;
414 noiseVector.fY *= 2;
415 ratio *= 2;
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000416 if (perlinNoiseShader.fStitchTiles) {
sugoi@google.come3b4c502013-04-05 13:47:09 +0000417 // Update stitch values
418 stitchData.fWidth *= 2;
419 stitchData.fWrapX = stitchData.fWidth + kPerlinNoise;
420 stitchData.fHeight *= 2;
421 stitchData.fWrapY = stitchData.fHeight + kPerlinNoise;
422 }
423 }
424
425 // The value of turbulenceFunctionResult comes from ((turbulenceFunctionResult) + 1) / 2
426 // by fractalNoise and (turbulenceFunctionResult) by turbulence.
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000427 if (perlinNoiseShader.fType == kFractalNoise_Type) {
sugoi@google.come3b4c502013-04-05 13:47:09 +0000428 turbulenceFunctionResult =
429 SkScalarMul(turbulenceFunctionResult, SK_ScalarHalf) + SK_ScalarHalf;
430 }
431
432 if (channel == 3) { // Scale alpha by paint value
433 turbulenceFunctionResult = SkScalarMul(turbulenceFunctionResult,
434 SkScalarDiv(SkIntToScalar(getPaintAlpha()), SkIntToScalar(255)));
435 }
436
437 // Clamp result
438 return SkScalarPin(turbulenceFunctionResult, 0, SK_Scalar1);
439}
440
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000441SkPMColor SkPerlinNoiseShader::PerlinNoiseShaderContext::shade(
442 const SkPoint& point, StitchData& stitchData) const {
sugoi@google.come3b4c502013-04-05 13:47:09 +0000443 SkPoint newPoint;
senorblanco@chromium.orga8d95f82014-04-04 14:46:10 +0000444 fMatrix.mapPoints(&newPoint, &point, 1);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000445 newPoint.fX = SkScalarRoundToScalar(newPoint.fX);
446 newPoint.fY = SkScalarRoundToScalar(newPoint.fY);
447
448 U8CPU rgba[4];
449 for (int channel = 3; channel >= 0; --channel) {
450 rgba[channel] = SkScalarFloorToInt(255 *
senorblancoca6a7c22014-06-27 13:35:52 -0700451 calculateTurbulenceValueForPoint(channel, stitchData, newPoint));
sugoi@google.come3b4c502013-04-05 13:47:09 +0000452 }
453 return SkPreMultiplyARGB(rgba[3], rgba[0], rgba[1], rgba[2]);
454}
455
commit-bot@chromium.orgce56d962014-05-05 18:39:18 +0000456SkShader::Context* SkPerlinNoiseShader::onCreateContext(const ContextRec& rec,
457 void* storage) const {
commit-bot@chromium.orge901b6d2014-05-01 19:31:31 +0000458 return SkNEW_PLACEMENT_ARGS(storage, PerlinNoiseShaderContext, (*this, rec));
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000459}
460
461size_t SkPerlinNoiseShader::contextSize() const {
462 return sizeof(PerlinNoiseShaderContext);
463}
464
465SkPerlinNoiseShader::PerlinNoiseShaderContext::PerlinNoiseShaderContext(
commit-bot@chromium.orge901b6d2014-05-01 19:31:31 +0000466 const SkPerlinNoiseShader& shader, const ContextRec& rec)
467 : INHERITED(shader, rec)
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000468{
commit-bot@chromium.orge901b6d2014-05-01 19:31:31 +0000469 SkMatrix newMatrix = *rec.fMatrix;
reed@google.comb67b8e62014-05-12 18:12:24 +0000470 newMatrix.preConcat(shader.getLocalMatrix());
471 if (rec.fLocalMatrix) {
472 newMatrix.preConcat(*rec.fLocalMatrix);
473 }
senorblanco@chromium.orga8d95f82014-04-04 14:46:10 +0000474 // This (1,1) translation is due to WebKit's 1 based coordinates for the noise
475 // (as opposed to 0 based, usually). The same adjustment is in the setData() function.
senorblancoca6a7c22014-06-27 13:35:52 -0700476 fMatrix.setTranslate(-newMatrix.getTranslateX() + SK_Scalar1, -newMatrix.getTranslateY() + SK_Scalar1);
477 fPaintingData = SkNEW_ARGS(PaintingData, (shader.fTileSize, shader.fSeed, shader.fBaseFrequencyX, shader.fBaseFrequencyY, newMatrix));
478}
479
480SkPerlinNoiseShader::PerlinNoiseShaderContext::~PerlinNoiseShaderContext() {
481 SkDELETE(fPaintingData);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000482}
483
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000484void SkPerlinNoiseShader::PerlinNoiseShaderContext::shadeSpan(
485 int x, int y, SkPMColor result[], int count) {
sugoi@google.come3b4c502013-04-05 13:47:09 +0000486 SkPoint point = SkPoint::Make(SkIntToScalar(x), SkIntToScalar(y));
487 StitchData stitchData;
488 for (int i = 0; i < count; ++i) {
489 result[i] = shade(point, stitchData);
490 point.fX += SK_Scalar1;
491 }
492}
493
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000494void SkPerlinNoiseShader::PerlinNoiseShaderContext::shadeSpan16(
495 int x, int y, uint16_t result[], int count) {
sugoi@google.come3b4c502013-04-05 13:47:09 +0000496 SkPoint point = SkPoint::Make(SkIntToScalar(x), SkIntToScalar(y));
497 StitchData stitchData;
498 DITHER_565_SCAN(y);
499 for (int i = 0; i < count; ++i) {
500 unsigned dither = DITHER_VALUE(x);
501 result[i] = SkDitherRGB32To565(shade(point, stitchData), dither);
502 DITHER_INC_X(x);
503 point.fX += SK_Scalar1;
504 }
505}
506
507/////////////////////////////////////////////////////////////////////
508
commit-bot@chromium.org344cf452013-06-17 14:19:01 +0000509#if SK_SUPPORT_GPU
sugoi@google.come3b4c502013-04-05 13:47:09 +0000510
joshualittb0a8a372014-09-23 09:50:21 -0700511#include "GrTBackendProcessorFactory.h"
sugoi@google.come3b4c502013-04-05 13:47:09 +0000512
joshualittb0a8a372014-09-23 09:50:21 -0700513class GrGLPerlinNoise : public GrGLFragmentProcessor {
sugoi@google.com4775cba2013-04-17 13:46:56 +0000514public:
joshualittb0a8a372014-09-23 09:50:21 -0700515 GrGLPerlinNoise(const GrBackendProcessorFactory&,
516 const GrProcessor&);
sugoi@google.com4775cba2013-04-17 13:46:56 +0000517 virtual ~GrGLPerlinNoise() {}
sugoi@google.come3b4c502013-04-05 13:47:09 +0000518
joshualitt15988992014-10-09 15:04:05 -0700519 virtual void emitCode(GrGLFPBuilder*,
joshualittb0a8a372014-09-23 09:50:21 -0700520 const GrFragmentProcessor&,
521 const GrProcessorKey&,
sugoi@google.come3b4c502013-04-05 13:47:09 +0000522 const char* outputColor,
523 const char* inputColor,
bsalomon@google.com77af6802013-10-02 13:04:56 +0000524 const TransformedCoordsArray&,
sugoi@google.come3b4c502013-04-05 13:47:09 +0000525 const TextureSamplerArray&) SK_OVERRIDE;
526
joshualittb0a8a372014-09-23 09:50:21 -0700527 virtual void setData(const GrGLProgramDataManager&, const GrProcessor&) SK_OVERRIDE;
sugoi@google.come3b4c502013-04-05 13:47:09 +0000528
joshualittb0a8a372014-09-23 09:50:21 -0700529 static inline void GenKey(const GrProcessor&, const GrGLCaps&, GrProcessorKeyBuilder* b);
sugoi@google.com4775cba2013-04-17 13:46:56 +0000530
531private:
sugoi@google.com4775cba2013-04-17 13:46:56 +0000532
kkinnunen7510b222014-07-30 00:04:16 -0700533 GrGLProgramDataManager::UniformHandle fStitchDataUni;
534 SkPerlinNoiseShader::Type fType;
535 bool fStitchTiles;
536 int fNumOctaves;
537 GrGLProgramDataManager::UniformHandle fBaseFrequencyUni;
538 GrGLProgramDataManager::UniformHandle fAlphaUni;
senorblancof3b50272014-06-16 10:49:58 -0700539
540private:
joshualittb0a8a372014-09-23 09:50:21 -0700541 typedef GrGLFragmentProcessor INHERITED;
sugoi@google.come3b4c502013-04-05 13:47:09 +0000542};
543
544/////////////////////////////////////////////////////////////////////
545
joshualittb0a8a372014-09-23 09:50:21 -0700546class GrPerlinNoiseEffect : public GrFragmentProcessor {
sugoi@google.come3b4c502013-04-05 13:47:09 +0000547public:
joshualittb0a8a372014-09-23 09:50:21 -0700548 static GrFragmentProcessor* Create(SkPerlinNoiseShader::Type type,
549 int numOctaves, bool stitchTiles,
550 SkPerlinNoiseShader::PaintingData* paintingData,
551 GrTexture* permutationsTexture, GrTexture* noiseTexture,
552 const SkMatrix& matrix, uint8_t alpha) {
bsalomon55fad7a2014-07-08 07:34:20 -0700553 return SkNEW_ARGS(GrPerlinNoiseEffect, (type, numOctaves, stitchTiles, paintingData,
554 permutationsTexture, noiseTexture, matrix, alpha));
sugoi@google.come3b4c502013-04-05 13:47:09 +0000555 }
556
senorblancoca6a7c22014-06-27 13:35:52 -0700557 virtual ~GrPerlinNoiseEffect() {
558 SkDELETE(fPaintingData);
559 }
sugoi@google.come3b4c502013-04-05 13:47:09 +0000560
561 static const char* Name() { return "PerlinNoise"; }
joshualittb0a8a372014-09-23 09:50:21 -0700562 virtual const GrBackendFragmentProcessorFactory& getFactory() const SK_OVERRIDE {
563 return GrTBackendFragmentProcessorFactory<GrPerlinNoiseEffect>::getInstance();
sugoi@google.come3b4c502013-04-05 13:47:09 +0000564 }
senorblancoca6a7c22014-06-27 13:35:52 -0700565 const SkPerlinNoiseShader::StitchData& stitchData() const { return fPaintingData->fStitchDataInit; }
sugoi@google.come3b4c502013-04-05 13:47:09 +0000566
senorblancof3b50272014-06-16 10:49:58 -0700567 SkPerlinNoiseShader::Type type() const { return fType; }
568 bool stitchTiles() const { return fStitchTiles; }
senorblancoca6a7c22014-06-27 13:35:52 -0700569 const SkVector& baseFrequency() const { return fPaintingData->fBaseFrequency; }
senorblancof3b50272014-06-16 10:49:58 -0700570 int numOctaves() const { return fNumOctaves; }
571 const SkMatrix& matrix() const { return fCoordTransform.getMatrix(); }
572 uint8_t alpha() const { return fAlpha; }
573
joshualittb0a8a372014-09-23 09:50:21 -0700574 typedef GrGLPerlinNoise GLProcessor;
sugoi@google.come3b4c502013-04-05 13:47:09 +0000575
sugoi@google.come3b4c502013-04-05 13:47:09 +0000576private:
bsalomon0e08fc12014-10-15 08:19:04 -0700577 virtual bool onIsEqual(const GrFragmentProcessor& sBase) const SK_OVERRIDE {
joshualitt49586be2014-09-16 08:21:41 -0700578 const GrPerlinNoiseEffect& s = sBase.cast<GrPerlinNoiseEffect>();
senorblancof3b50272014-06-16 10:49:58 -0700579 return fType == s.fType &&
senorblancoca6a7c22014-06-27 13:35:52 -0700580 fPaintingData->fBaseFrequency == s.fPaintingData->fBaseFrequency &&
senorblancof3b50272014-06-16 10:49:58 -0700581 fNumOctaves == s.fNumOctaves &&
582 fStitchTiles == s.fStitchTiles &&
senorblancof3b50272014-06-16 10:49:58 -0700583 fAlpha == s.fAlpha &&
senorblancoca6a7c22014-06-27 13:35:52 -0700584 fPaintingData->fStitchDataInit == s.fPaintingData->fStitchDataInit;
sugoi@google.come3b4c502013-04-05 13:47:09 +0000585 }
586
egdaniel605dd0f2014-11-12 08:35:25 -0800587 void onComputeInvariantOutput(GrInvariantOutput* inout) const SK_OVERRIDE {
588 inout->setToUnknown(GrInvariantOutput::kWillNot_ReadInput);
egdaniel1a8ecdf2014-10-03 06:24:12 -0700589 }
590
senorblancoca6a7c22014-06-27 13:35:52 -0700591 GrPerlinNoiseEffect(SkPerlinNoiseShader::Type type,
sugoi@google.come3b4c502013-04-05 13:47:09 +0000592 int numOctaves, bool stitchTiles,
senorblancoca6a7c22014-06-27 13:35:52 -0700593 SkPerlinNoiseShader::PaintingData* paintingData,
sugoi@google.come3b4c502013-04-05 13:47:09 +0000594 GrTexture* permutationsTexture, GrTexture* noiseTexture,
595 const SkMatrix& matrix, uint8_t alpha)
senorblancof3b50272014-06-16 10:49:58 -0700596 : fType(type)
senorblancof3b50272014-06-16 10:49:58 -0700597 , fNumOctaves(numOctaves)
598 , fStitchTiles(stitchTiles)
599 , fAlpha(alpha)
sugoi@google.com4775cba2013-04-17 13:46:56 +0000600 , fPermutationsAccess(permutationsTexture)
sugoi@google.come3b4c502013-04-05 13:47:09 +0000601 , fNoiseAccess(noiseTexture)
senorblancoca6a7c22014-06-27 13:35:52 -0700602 , fPaintingData(paintingData) {
sugoi@google.come3b4c502013-04-05 13:47:09 +0000603 this->addTextureAccess(&fPermutationsAccess);
604 this->addTextureAccess(&fNoiseAccess);
senorblancoca6a7c22014-06-27 13:35:52 -0700605 fCoordTransform.reset(kLocal_GrCoordSet, matrix);
senorblancof3b50272014-06-16 10:49:58 -0700606 this->addCoordTransform(&fCoordTransform);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000607 }
608
joshualittb0a8a372014-09-23 09:50:21 -0700609 GR_DECLARE_FRAGMENT_PROCESSOR_TEST;
sugoi@google.come3b4c502013-04-05 13:47:09 +0000610
senorblancof3b50272014-06-16 10:49:58 -0700611 SkPerlinNoiseShader::Type fType;
612 GrCoordTransform fCoordTransform;
senorblancof3b50272014-06-16 10:49:58 -0700613 int fNumOctaves;
614 bool fStitchTiles;
615 uint8_t fAlpha;
sugoi@google.come3b4c502013-04-05 13:47:09 +0000616 GrTextureAccess fPermutationsAccess;
617 GrTextureAccess fNoiseAccess;
senorblancoca6a7c22014-06-27 13:35:52 -0700618 SkPerlinNoiseShader::PaintingData *fPaintingData;
sugoi@google.come3b4c502013-04-05 13:47:09 +0000619
sugoi@google.com4775cba2013-04-17 13:46:56 +0000620private:
joshualittb0a8a372014-09-23 09:50:21 -0700621 typedef GrFragmentProcessor INHERITED;
sugoi@google.come3b4c502013-04-05 13:47:09 +0000622};
623
624/////////////////////////////////////////////////////////////////////
joshualittb0a8a372014-09-23 09:50:21 -0700625GR_DEFINE_FRAGMENT_PROCESSOR_TEST(GrPerlinNoiseEffect);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000626
joshualittb0a8a372014-09-23 09:50:21 -0700627GrFragmentProcessor* GrPerlinNoiseEffect::TestCreate(SkRandom* random,
628 GrContext* context,
629 const GrDrawTargetCaps&,
630 GrTexture**) {
sugoi@google.com423ac132013-04-18 14:04:57 +0000631 int numOctaves = random->nextRangeU(2, 10);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000632 bool stitchTiles = random->nextBool();
633 SkScalar seed = SkIntToScalar(random->nextU());
634 SkISize tileSize = SkISize::Make(random->nextRangeU(4, 4096), random->nextRangeU(4, 4096));
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +0000635 SkScalar baseFrequencyX = random->nextRangeScalar(0.01f,
636 0.99f);
637 SkScalar baseFrequencyY = random->nextRangeScalar(0.01f,
638 0.99f);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000639
640 SkShader* shader = random->nextBool() ?
641 SkPerlinNoiseShader::CreateFractalNoise(baseFrequencyX, baseFrequencyY, numOctaves, seed,
642 stitchTiles ? &tileSize : NULL) :
commit-bot@chromium.org9fbbcca2014-04-01 16:09:37 +0000643 SkPerlinNoiseShader::CreateTurbulence(baseFrequencyX, baseFrequencyY, numOctaves, seed,
sugoi@google.come3b4c502013-04-05 13:47:09 +0000644 stitchTiles ? &tileSize : NULL);
645
646 SkPaint paint;
bsalomon83d081a2014-07-08 09:56:10 -0700647 GrColor paintColor;
joshualittb0a8a372014-09-23 09:50:21 -0700648 GrFragmentProcessor* effect;
649 SkAssertResult(shader->asFragmentProcessor(context, paint, NULL, &paintColor, &effect));
sugoi@google.come3b4c502013-04-05 13:47:09 +0000650
651 SkDELETE(shader);
652
653 return effect;
654}
sugoi@google.com4775cba2013-04-17 13:46:56 +0000655
joshualittb0a8a372014-09-23 09:50:21 -0700656GrGLPerlinNoise::GrGLPerlinNoise(const GrBackendProcessorFactory& factory,
657 const GrProcessor& processor)
senorblancof3b50272014-06-16 10:49:58 -0700658 : INHERITED (factory)
joshualittb0a8a372014-09-23 09:50:21 -0700659 , fType(processor.cast<GrPerlinNoiseEffect>().type())
660 , fStitchTiles(processor.cast<GrPerlinNoiseEffect>().stitchTiles())
661 , fNumOctaves(processor.cast<GrPerlinNoiseEffect>().numOctaves()) {
sugoi@google.com4775cba2013-04-17 13:46:56 +0000662}
663
joshualitt15988992014-10-09 15:04:05 -0700664void GrGLPerlinNoise::emitCode(GrGLFPBuilder* builder,
joshualittb0a8a372014-09-23 09:50:21 -0700665 const GrFragmentProcessor&,
666 const GrProcessorKey& key,
sugoi@google.come3b4c502013-04-05 13:47:09 +0000667 const char* outputColor,
668 const char* inputColor,
bsalomon@google.com77af6802013-10-02 13:04:56 +0000669 const TransformedCoordsArray& coords,
sugoi@google.come3b4c502013-04-05 13:47:09 +0000670 const TextureSamplerArray& samplers) {
671 sk_ignore_unused_variable(inputColor);
672
joshualitt15988992014-10-09 15:04:05 -0700673 GrGLFPFragmentBuilder* fsBuilder = builder->getFragmentShaderBuilder();
joshualitt30ba4362014-08-21 20:18:45 -0700674 SkString vCoords = fsBuilder->ensureFSCoords2D(coords, 0);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000675
joshualitt30ba4362014-08-21 20:18:45 -0700676 fBaseFrequencyUni = builder->addUniform(GrGLProgramBuilder::kFragment_Visibility,
sugoi@google.come3b4c502013-04-05 13:47:09 +0000677 kVec2f_GrSLType, "baseFrequency");
678 const char* baseFrequencyUni = builder->getUniformCStr(fBaseFrequencyUni);
joshualitt30ba4362014-08-21 20:18:45 -0700679 fAlphaUni = builder->addUniform(GrGLProgramBuilder::kFragment_Visibility,
sugoi@google.come3b4c502013-04-05 13:47:09 +0000680 kFloat_GrSLType, "alpha");
681 const char* alphaUni = builder->getUniformCStr(fAlphaUni);
682
683 const char* stitchDataUni = NULL;
684 if (fStitchTiles) {
joshualitt30ba4362014-08-21 20:18:45 -0700685 fStitchDataUni = builder->addUniform(GrGLProgramBuilder::kFragment_Visibility,
commit-bot@chromium.org98393202013-07-04 18:13:05 +0000686 kVec2f_GrSLType, "stitchData");
sugoi@google.come3b4c502013-04-05 13:47:09 +0000687 stitchDataUni = builder->getUniformCStr(fStitchDataUni);
688 }
689
sugoi@google.comd537af52013-06-10 13:59:25 +0000690 // There are 4 lines, so the center of each line is 1/8, 3/8, 5/8 and 7/8
691 const char* chanCoordR = "0.125";
692 const char* chanCoordG = "0.375";
693 const char* chanCoordB = "0.625";
694 const char* chanCoordA = "0.875";
695 const char* chanCoord = "chanCoord";
sugoi@google.come3b4c502013-04-05 13:47:09 +0000696 const char* stitchData = "stitchData";
697 const char* ratio = "ratio";
sugoi@google.come3b4c502013-04-05 13:47:09 +0000698 const char* noiseVec = "noiseVec";
sugoi@google.come3b4c502013-04-05 13:47:09 +0000699 const char* noiseSmooth = "noiseSmooth";
senorblancoce6a3542014-06-12 11:24:19 -0700700 const char* floorVal = "floorVal";
sugoi@google.come3b4c502013-04-05 13:47:09 +0000701 const char* fractVal = "fractVal";
702 const char* uv = "uv";
703 const char* ab = "ab";
704 const char* latticeIdx = "latticeIdx";
senorblancoce6a3542014-06-12 11:24:19 -0700705 const char* bcoords = "bcoords";
sugoi@google.come3b4c502013-04-05 13:47:09 +0000706 const char* lattice = "lattice";
sugoi@google.come3b4c502013-04-05 13:47:09 +0000707 const char* inc8bit = "0.00390625"; // 1.0 / 256.0
708 // This is the math to convert the two 16bit integer packed into rgba 8 bit input into a
709 // [-1,1] vector and perform a dot product between that vector and the provided vector.
710 const char* dotLattice = "dot(((%s.ga + %s.rb * vec2(%s)) * vec2(2.0) - vec2(1.0)), %s);";
711
sugoi@google.comd537af52013-06-10 13:59:25 +0000712 // Add noise function
713 static const GrGLShaderVar gPerlinNoiseArgs[] = {
714 GrGLShaderVar(chanCoord, kFloat_GrSLType),
commit-bot@chromium.org98393202013-07-04 18:13:05 +0000715 GrGLShaderVar(noiseVec, kVec2f_GrSLType)
sugoi@google.comd537af52013-06-10 13:59:25 +0000716 };
sugoi@google.come3b4c502013-04-05 13:47:09 +0000717
sugoi@google.comd537af52013-06-10 13:59:25 +0000718 static const GrGLShaderVar gPerlinNoiseStitchArgs[] = {
719 GrGLShaderVar(chanCoord, kFloat_GrSLType),
commit-bot@chromium.org98393202013-07-04 18:13:05 +0000720 GrGLShaderVar(noiseVec, kVec2f_GrSLType),
721 GrGLShaderVar(stitchData, kVec2f_GrSLType)
sugoi@google.comd537af52013-06-10 13:59:25 +0000722 };
sugoi@google.come3b4c502013-04-05 13:47:09 +0000723
sugoi@google.comd537af52013-06-10 13:59:25 +0000724 SkString noiseCode;
sugoi@google.come3b4c502013-04-05 13:47:09 +0000725
senorblancoce6a3542014-06-12 11:24:19 -0700726 noiseCode.appendf("\tvec4 %s;\n", floorVal);
727 noiseCode.appendf("\t%s.xy = floor(%s);\n", floorVal, noiseVec);
728 noiseCode.appendf("\t%s.zw = %s.xy + vec2(1.0);\n", floorVal, floorVal);
729 noiseCode.appendf("\tvec2 %s = fract(%s);\n", fractVal, noiseVec);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000730
731 // smooth curve : t * t * (3 - 2 * t)
senorblancoce6a3542014-06-12 11:24:19 -0700732 noiseCode.appendf("\n\tvec2 %s = %s * %s * (vec2(3.0) - vec2(2.0) * %s);",
733 noiseSmooth, fractVal, fractVal, fractVal);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000734
735 // Adjust frequencies if we're stitching tiles
736 if (fStitchTiles) {
commit-bot@chromium.org98393202013-07-04 18:13:05 +0000737 noiseCode.appendf("\n\tif(%s.x >= %s.x) { %s.x -= %s.x; }",
senorblancoce6a3542014-06-12 11:24:19 -0700738 floorVal, stitchData, floorVal, stitchData);
commit-bot@chromium.org98393202013-07-04 18:13:05 +0000739 noiseCode.appendf("\n\tif(%s.y >= %s.y) { %s.y -= %s.y; }",
senorblancoce6a3542014-06-12 11:24:19 -0700740 floorVal, stitchData, floorVal, stitchData);
741 noiseCode.appendf("\n\tif(%s.z >= %s.x) { %s.z -= %s.x; }",
742 floorVal, stitchData, floorVal, stitchData);
743 noiseCode.appendf("\n\tif(%s.w >= %s.y) { %s.w -= %s.y; }",
744 floorVal, stitchData, floorVal, stitchData);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000745 }
746
747 // Get texture coordinates and normalize
senorblancoce6a3542014-06-12 11:24:19 -0700748 noiseCode.appendf("\n\t%s = fract(floor(mod(%s, 256.0)) / vec4(256.0));\n",
749 floorVal, floorVal);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000750
751 // Get permutation for x
752 {
753 SkString xCoords("");
senorblancoce6a3542014-06-12 11:24:19 -0700754 xCoords.appendf("vec2(%s.x, 0.5)", floorVal);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000755
sugoi@google.comd537af52013-06-10 13:59:25 +0000756 noiseCode.appendf("\n\tvec2 %s;\n\t%s.x = ", latticeIdx, latticeIdx);
joshualitt30ba4362014-08-21 20:18:45 -0700757 fsBuilder->appendTextureLookup(&noiseCode, samplers[0], xCoords.c_str(), kVec2f_GrSLType);
sugoi@google.comd537af52013-06-10 13:59:25 +0000758 noiseCode.append(".r;");
sugoi@google.come3b4c502013-04-05 13:47:09 +0000759 }
760
761 // Get permutation for x + 1
762 {
763 SkString xCoords("");
senorblancoce6a3542014-06-12 11:24:19 -0700764 xCoords.appendf("vec2(%s.z, 0.5)", floorVal);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000765
sugoi@google.comd537af52013-06-10 13:59:25 +0000766 noiseCode.appendf("\n\t%s.y = ", latticeIdx);
joshualitt30ba4362014-08-21 20:18:45 -0700767 fsBuilder->appendTextureLookup(&noiseCode, samplers[0], xCoords.c_str(), kVec2f_GrSLType);
sugoi@google.comd537af52013-06-10 13:59:25 +0000768 noiseCode.append(".r;");
sugoi@google.come3b4c502013-04-05 13:47:09 +0000769 }
770
commit-bot@chromium.org344cf452013-06-17 14:19:01 +0000771#if defined(SK_BUILD_FOR_ANDROID)
772 // Android rounding for Tegra devices, like, for example: Xoom (Tegra 2), Nexus 7 (Tegra 3).
773 // The issue is that colors aren't accurate enough on Tegra devices. For example, if an 8 bit
774 // value of 124 (or 0.486275 here) is entered, we can get a texture value of 123.513725
775 // (or 0.484368 here). The following rounding operation prevents these precision issues from
776 // affecting the result of the noise by making sure that we only have multiples of 1/255.
777 // (Note that 1/255 is about 0.003921569, which is the value used here).
778 noiseCode.appendf("\n\t%s = floor(%s * vec2(255.0) + vec2(0.5)) * vec2(0.003921569);",
779 latticeIdx, latticeIdx);
780#endif
781
sugoi@google.come3b4c502013-04-05 13:47:09 +0000782 // Get (x,y) coordinates with the permutated x
senorblancoce6a3542014-06-12 11:24:19 -0700783 noiseCode.appendf("\n\tvec4 %s = fract(%s.xyxy + %s.yyww);", bcoords, latticeIdx, floorVal);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000784
sugoi@google.comd537af52013-06-10 13:59:25 +0000785 noiseCode.appendf("\n\n\tvec2 %s;", uv);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000786 // Compute u, at offset (0,0)
787 {
788 SkString latticeCoords("");
senorblancoce6a3542014-06-12 11:24:19 -0700789 latticeCoords.appendf("vec2(%s.x, %s)", bcoords, chanCoord);
sugoi@google.comd537af52013-06-10 13:59:25 +0000790 noiseCode.appendf("\n\tvec4 %s = ", lattice);
joshualitt30ba4362014-08-21 20:18:45 -0700791 fsBuilder->appendTextureLookup(&noiseCode, samplers[1], latticeCoords.c_str(),
sugoi@google.comd537af52013-06-10 13:59:25 +0000792 kVec2f_GrSLType);
793 noiseCode.appendf(".bgra;\n\t%s.x = ", uv);
794 noiseCode.appendf(dotLattice, lattice, lattice, inc8bit, fractVal);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000795 }
796
sugoi@google.comd537af52013-06-10 13:59:25 +0000797 noiseCode.appendf("\n\t%s.x -= 1.0;", fractVal);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000798 // Compute v, at offset (-1,0)
799 {
800 SkString latticeCoords("");
senorblancoce6a3542014-06-12 11:24:19 -0700801 latticeCoords.appendf("vec2(%s.y, %s)", bcoords, chanCoord);
commit-bot@chromium.org344cf452013-06-17 14:19:01 +0000802 noiseCode.append("\n\tlattice = ");
joshualitt30ba4362014-08-21 20:18:45 -0700803 fsBuilder->appendTextureLookup(&noiseCode, samplers[1], latticeCoords.c_str(),
sugoi@google.comd537af52013-06-10 13:59:25 +0000804 kVec2f_GrSLType);
805 noiseCode.appendf(".bgra;\n\t%s.y = ", uv);
806 noiseCode.appendf(dotLattice, lattice, lattice, inc8bit, fractVal);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000807 }
808
809 // Compute 'a' as a linear interpolation of 'u' and 'v'
sugoi@google.comd537af52013-06-10 13:59:25 +0000810 noiseCode.appendf("\n\tvec2 %s;", ab);
811 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 +0000812
sugoi@google.comd537af52013-06-10 13:59:25 +0000813 noiseCode.appendf("\n\t%s.y -= 1.0;", fractVal);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000814 // Compute v, at offset (-1,-1)
815 {
816 SkString latticeCoords("");
senorblancoce6a3542014-06-12 11:24:19 -0700817 latticeCoords.appendf("vec2(%s.w, %s)", bcoords, chanCoord);
commit-bot@chromium.org344cf452013-06-17 14:19:01 +0000818 noiseCode.append("\n\tlattice = ");
joshualitt30ba4362014-08-21 20:18:45 -0700819 fsBuilder->appendTextureLookup(&noiseCode, samplers[1], latticeCoords.c_str(),
sugoi@google.comd537af52013-06-10 13:59:25 +0000820 kVec2f_GrSLType);
821 noiseCode.appendf(".bgra;\n\t%s.y = ", uv);
822 noiseCode.appendf(dotLattice, lattice, lattice, inc8bit, fractVal);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000823 }
824
sugoi@google.comd537af52013-06-10 13:59:25 +0000825 noiseCode.appendf("\n\t%s.x += 1.0;", fractVal);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000826 // Compute u, at offset (0,-1)
827 {
828 SkString latticeCoords("");
senorblancoce6a3542014-06-12 11:24:19 -0700829 latticeCoords.appendf("vec2(%s.z, %s)", bcoords, chanCoord);
commit-bot@chromium.org344cf452013-06-17 14:19:01 +0000830 noiseCode.append("\n\tlattice = ");
joshualitt30ba4362014-08-21 20:18:45 -0700831 fsBuilder->appendTextureLookup(&noiseCode, samplers[1], latticeCoords.c_str(),
sugoi@google.comd537af52013-06-10 13:59:25 +0000832 kVec2f_GrSLType);
833 noiseCode.appendf(".bgra;\n\t%s.x = ", uv);
834 noiseCode.appendf(dotLattice, lattice, lattice, inc8bit, fractVal);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000835 }
836
837 // Compute 'b' as a linear interpolation of 'u' and 'v'
sugoi@google.comd537af52013-06-10 13:59:25 +0000838 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 +0000839 // Compute the noise as a linear interpolation of 'a' and 'b'
sugoi@google.comd537af52013-06-10 13:59:25 +0000840 noiseCode.appendf("\n\treturn mix(%s.x, %s.y, %s.y);\n", ab, ab, noiseSmooth);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000841
sugoi@google.comd537af52013-06-10 13:59:25 +0000842 SkString noiseFuncName;
843 if (fStitchTiles) {
joshualitt30ba4362014-08-21 20:18:45 -0700844 fsBuilder->emitFunction(kFloat_GrSLType,
commit-bot@chromium.org74a3a212013-08-30 19:43:59 +0000845 "perlinnoise", SK_ARRAY_COUNT(gPerlinNoiseStitchArgs),
846 gPerlinNoiseStitchArgs, noiseCode.c_str(), &noiseFuncName);
sugoi@google.comd537af52013-06-10 13:59:25 +0000847 } else {
joshualitt30ba4362014-08-21 20:18:45 -0700848 fsBuilder->emitFunction(kFloat_GrSLType,
commit-bot@chromium.org74a3a212013-08-30 19:43:59 +0000849 "perlinnoise", SK_ARRAY_COUNT(gPerlinNoiseArgs),
850 gPerlinNoiseArgs, noiseCode.c_str(), &noiseFuncName);
sugoi@google.comd537af52013-06-10 13:59:25 +0000851 }
sugoi@google.come3b4c502013-04-05 13:47:09 +0000852
sugoi@google.comd537af52013-06-10 13:59:25 +0000853 // There are rounding errors if the floor operation is not performed here
joshualitt30ba4362014-08-21 20:18:45 -0700854 fsBuilder->codeAppendf("\n\t\tvec2 %s = floor(%s.xy) * %s;",
senorblancoca6a7c22014-06-27 13:35:52 -0700855 noiseVec, vCoords.c_str(), baseFrequencyUni);
sugoi@google.comd537af52013-06-10 13:59:25 +0000856
857 // Clear the color accumulator
joshualitt30ba4362014-08-21 20:18:45 -0700858 fsBuilder->codeAppendf("\n\t\t%s = vec4(0.0);", outputColor);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000859
860 if (fStitchTiles) {
sugoi@google.comd537af52013-06-10 13:59:25 +0000861 // Set up TurbulenceInitial stitch values.
joshualitt30ba4362014-08-21 20:18:45 -0700862 fsBuilder->codeAppendf("\n\t\tvec2 %s = %s;", stitchData, stitchDataUni);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000863 }
sugoi@google.come3b4c502013-04-05 13:47:09 +0000864
joshualitt30ba4362014-08-21 20:18:45 -0700865 fsBuilder->codeAppendf("\n\t\tfloat %s = 1.0;", ratio);
sugoi@google.comd537af52013-06-10 13:59:25 +0000866
867 // Loop over all octaves
joshualitt30ba4362014-08-21 20:18:45 -0700868 fsBuilder->codeAppendf("\n\t\tfor (int octave = 0; octave < %d; ++octave) {", fNumOctaves);
sugoi@google.comd537af52013-06-10 13:59:25 +0000869
joshualitt30ba4362014-08-21 20:18:45 -0700870 fsBuilder->codeAppendf("\n\t\t\t%s += ", outputColor);
sugoi@google.comd537af52013-06-10 13:59:25 +0000871 if (fType != SkPerlinNoiseShader::kFractalNoise_Type) {
joshualitt30ba4362014-08-21 20:18:45 -0700872 fsBuilder->codeAppend("abs(");
sugoi@google.comd537af52013-06-10 13:59:25 +0000873 }
874 if (fStitchTiles) {
joshualitt30ba4362014-08-21 20:18:45 -0700875 fsBuilder->codeAppendf(
sugoi@google.comd537af52013-06-10 13:59:25 +0000876 "vec4(\n\t\t\t\t%s(%s, %s, %s),\n\t\t\t\t%s(%s, %s, %s),"
877 "\n\t\t\t\t%s(%s, %s, %s),\n\t\t\t\t%s(%s, %s, %s))",
878 noiseFuncName.c_str(), chanCoordR, noiseVec, stitchData,
879 noiseFuncName.c_str(), chanCoordG, noiseVec, stitchData,
880 noiseFuncName.c_str(), chanCoordB, noiseVec, stitchData,
881 noiseFuncName.c_str(), chanCoordA, noiseVec, stitchData);
882 } else {
joshualitt30ba4362014-08-21 20:18:45 -0700883 fsBuilder->codeAppendf(
sugoi@google.comd537af52013-06-10 13:59:25 +0000884 "vec4(\n\t\t\t\t%s(%s, %s),\n\t\t\t\t%s(%s, %s),"
885 "\n\t\t\t\t%s(%s, %s),\n\t\t\t\t%s(%s, %s))",
886 noiseFuncName.c_str(), chanCoordR, noiseVec,
887 noiseFuncName.c_str(), chanCoordG, noiseVec,
888 noiseFuncName.c_str(), chanCoordB, noiseVec,
889 noiseFuncName.c_str(), chanCoordA, noiseVec);
890 }
891 if (fType != SkPerlinNoiseShader::kFractalNoise_Type) {
joshualitt30ba4362014-08-21 20:18:45 -0700892 fsBuilder->codeAppendf(")"); // end of "abs("
sugoi@google.comd537af52013-06-10 13:59:25 +0000893 }
joshualitt30ba4362014-08-21 20:18:45 -0700894 fsBuilder->codeAppendf(" * %s;", ratio);
sugoi@google.comd537af52013-06-10 13:59:25 +0000895
joshualitt30ba4362014-08-21 20:18:45 -0700896 fsBuilder->codeAppendf("\n\t\t\t%s *= vec2(2.0);", noiseVec);
897 fsBuilder->codeAppendf("\n\t\t\t%s *= 0.5;", ratio);
sugoi@google.comd537af52013-06-10 13:59:25 +0000898
899 if (fStitchTiles) {
joshualitt30ba4362014-08-21 20:18:45 -0700900 fsBuilder->codeAppendf("\n\t\t\t%s *= vec2(2.0);", stitchData);
sugoi@google.comd537af52013-06-10 13:59:25 +0000901 }
joshualitt30ba4362014-08-21 20:18:45 -0700902 fsBuilder->codeAppend("\n\t\t}"); // end of the for loop on octaves
sugoi@google.come3b4c502013-04-05 13:47:09 +0000903
904 if (fType == SkPerlinNoiseShader::kFractalNoise_Type) {
905 // The value of turbulenceFunctionResult comes from ((turbulenceFunctionResult) + 1) / 2
906 // by fractalNoise and (turbulenceFunctionResult) by turbulence.
joshualitt30ba4362014-08-21 20:18:45 -0700907 fsBuilder->codeAppendf("\n\t\t%s = %s * vec4(0.5) + vec4(0.5);", outputColor, outputColor);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000908 }
909
joshualitt30ba4362014-08-21 20:18:45 -0700910 fsBuilder->codeAppendf("\n\t\t%s.a *= %s;", outputColor, alphaUni);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000911
912 // Clamp values
joshualitt30ba4362014-08-21 20:18:45 -0700913 fsBuilder->codeAppendf("\n\t\t%s = clamp(%s, 0.0, 1.0);", outputColor, outputColor);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000914
915 // Pre-multiply the result
joshualitt30ba4362014-08-21 20:18:45 -0700916 fsBuilder->codeAppendf("\n\t\t%s = vec4(%s.rgb * %s.aaa, %s.a);\n",
sugoi@google.come3b4c502013-04-05 13:47:09 +0000917 outputColor, outputColor, outputColor, outputColor);
918}
919
joshualittb0a8a372014-09-23 09:50:21 -0700920void GrGLPerlinNoise::GenKey(const GrProcessor& processor, const GrGLCaps&,
921 GrProcessorKeyBuilder* b) {
922 const GrPerlinNoiseEffect& turbulence = processor.cast<GrPerlinNoiseEffect>();
sugoi@google.come3b4c502013-04-05 13:47:09 +0000923
bsalomon63e99f72014-07-21 08:03:14 -0700924 uint32_t key = turbulence.numOctaves();
sugoi@google.come3b4c502013-04-05 13:47:09 +0000925
926 key = key << 3; // Make room for next 3 bits
927
928 switch (turbulence.type()) {
929 case SkPerlinNoiseShader::kFractalNoise_Type:
930 key |= 0x1;
931 break;
932 case SkPerlinNoiseShader::kTurbulence_Type:
933 key |= 0x2;
934 break;
935 default:
936 // leave key at 0
937 break;
938 }
939
940 if (turbulence.stitchTiles()) {
941 key |= 0x4; // Flip the 3rd bit if tile stitching is on
942 }
943
bsalomon63e99f72014-07-21 08:03:14 -0700944 b->add32(key);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000945}
946
joshualittb0a8a372014-09-23 09:50:21 -0700947void GrGLPerlinNoise::setData(const GrGLProgramDataManager& pdman, const GrProcessor& processor) {
948 INHERITED::setData(pdman, processor);
senorblancof3b50272014-06-16 10:49:58 -0700949
joshualittb0a8a372014-09-23 09:50:21 -0700950 const GrPerlinNoiseEffect& turbulence = processor.cast<GrPerlinNoiseEffect>();
sugoi@google.come3b4c502013-04-05 13:47:09 +0000951
952 const SkVector& baseFrequency = turbulence.baseFrequency();
kkinnunen7510b222014-07-30 00:04:16 -0700953 pdman.set2f(fBaseFrequencyUni, baseFrequency.fX, baseFrequency.fY);
954 pdman.set1f(fAlphaUni, SkScalarDiv(SkIntToScalar(turbulence.alpha()), SkIntToScalar(255)));
sugoi@google.come3b4c502013-04-05 13:47:09 +0000955
sugoi@google.com4775cba2013-04-17 13:46:56 +0000956 if (turbulence.stitchTiles()) {
957 const SkPerlinNoiseShader::StitchData& stitchData = turbulence.stitchData();
kkinnunen7510b222014-07-30 00:04:16 -0700958 pdman.set2f(fStitchDataUni, SkIntToScalar(stitchData.fWidth),
commit-bot@chromium.org98393202013-07-04 18:13:05 +0000959 SkIntToScalar(stitchData.fHeight));
sugoi@google.com4775cba2013-04-17 13:46:56 +0000960 }
961}
962
sugoi@google.come3b4c502013-04-05 13:47:09 +0000963/////////////////////////////////////////////////////////////////////
964
joshualittb0a8a372014-09-23 09:50:21 -0700965bool SkPerlinNoiseShader::asFragmentProcessor(GrContext* context, const SkPaint& paint,
966 const SkMatrix* externalLocalMatrix,
967 GrColor* paintColor, GrFragmentProcessor** fp) const {
bsalomon49f085d2014-09-05 13:34:00 -0700968 SkASSERT(context);
dandov9de5b512014-06-10 14:38:28 -0700969
bsalomon83d081a2014-07-08 09:56:10 -0700970 *paintColor = SkColor2GrColorJustAlpha(paint.getColor());
senorblancoca6a7c22014-06-27 13:35:52 -0700971
commit-bot@chromium.org96fb7482014-05-09 20:28:11 +0000972 SkMatrix localMatrix = this->getLocalMatrix();
973 if (externalLocalMatrix) {
974 localMatrix.preConcat(*externalLocalMatrix);
975 }
976
senorblancoca6a7c22014-06-27 13:35:52 -0700977 SkMatrix matrix = context->getMatrix();
978 matrix.preConcat(localMatrix);
979
commit-bot@chromium.orgc2a0ea62013-11-06 10:08:38 +0000980 if (0 == fNumOctaves) {
981 SkColor clearColor = 0;
982 if (kFractalNoise_Type == fType) {
983 clearColor = SkColorSetARGB(paint.getAlpha() / 2, 127, 127, 127);
984 }
985 SkAutoTUnref<SkColorFilter> cf(SkColorFilter::CreateModeFilter(
986 clearColor, SkXfermode::kSrc_Mode));
joshualittb0a8a372014-09-23 09:50:21 -0700987 *fp = cf->asFragmentProcessor(context);
dandov9de5b512014-06-10 14:38:28 -0700988 return true;
commit-bot@chromium.orgc2a0ea62013-11-06 10:08:38 +0000989 }
990
sugoi@google.come3b4c502013-04-05 13:47:09 +0000991 // Either we don't stitch tiles, either we have a valid tile size
992 SkASSERT(!fStitchTiles || !fTileSize.isEmpty());
993
joshualittb0a8a372014-09-23 09:50:21 -0700994 SkPerlinNoiseShader::PaintingData* paintingData =
995 SkNEW_ARGS(PaintingData, (fTileSize, fSeed, fBaseFrequencyX, fBaseFrequencyY, matrix));
bsalomonbcf0a522014-10-08 08:40:09 -0700996 SkAutoTUnref<GrTexture> permutationsTexture(
997 GrRefCachedBitmapTexture(context, paintingData->getPermutationsBitmap(), NULL));
998 SkAutoTUnref<GrTexture> noiseTexture(
999 GrRefCachedBitmapTexture(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)) {
joshualittb0a8a372014-09-23 09:50:21 -07001005 *fp = GrPerlinNoiseEffect::Create(fType,
1006 fNumOctaves,
1007 fStitchTiles,
1008 paintingData,
1009 permutationsTexture, noiseTexture,
1010 m, paint.getAlpha());
senorblancoca6a7c22014-06-27 13:35:52 -07001011 } else {
1012 SkDELETE(paintingData);
joshualittb0a8a372014-09-23 09:50:21 -07001013 *fp = NULL;
senorblancoca6a7c22014-06-27 13:35:52 -07001014 }
dandov9de5b512014-06-10 14:38:28 -07001015 return true;
sugoi@google.come3b4c502013-04-05 13:47:09 +00001016}
1017
1018#else
1019
joshualittb0a8a372014-09-23 09:50:21 -07001020bool SkPerlinNoiseShader::asFragmentProcessor(GrContext*, const SkPaint&, const SkMatrix*, GrColor*,
1021 GrFragmentProcessor**) const {
sugoi@google.come3b4c502013-04-05 13:47:09 +00001022 SkDEBUGFAIL("Should not call in GPU-less build");
dandov9de5b512014-06-10 14:38:28 -07001023 return false;
sugoi@google.come3b4c502013-04-05 13:47:09 +00001024}
1025
1026#endif
1027
commit-bot@chromium.org0f10f7b2014-03-13 18:02:17 +00001028#ifndef SK_IGNORE_TO_STRING
sugoi@google.come3b4c502013-04-05 13:47:09 +00001029void SkPerlinNoiseShader::toString(SkString* str) const {
1030 str->append("SkPerlinNoiseShader: (");
1031
1032 str->append("type: ");
1033 switch (fType) {
1034 case kFractalNoise_Type:
1035 str->append("\"fractal noise\"");
1036 break;
1037 case kTurbulence_Type:
1038 str->append("\"turbulence\"");
1039 break;
1040 default:
1041 str->append("\"unknown\"");
1042 break;
1043 }
1044 str->append(" base frequency: (");
1045 str->appendScalar(fBaseFrequencyX);
1046 str->append(", ");
1047 str->appendScalar(fBaseFrequencyY);
1048 str->append(") number of octaves: ");
1049 str->appendS32(fNumOctaves);
1050 str->append(" seed: ");
1051 str->appendScalar(fSeed);
1052 str->append(" stitch tiles: ");
1053 str->append(fStitchTiles ? "true " : "false ");
1054
1055 this->INHERITED::toString(str);
1056
1057 str->append(")");
1058}
1059#endif