blob: 501e6361a53f0e2d1c6ede31b37dde9cc397e58c [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"
joshualitteb2a6762014-12-04 11:35:33 -080021#include "SkGr.h"
wangyix6af0c932015-07-22 10:21:17 -070022#include "gl/GrGLFragmentProcessor.h"
joshualitt30ba4362014-08-21 20:18:45 -070023#include "gl/builders/GrGLProgramBuilder.h"
sugoi@google.come3b4c502013-04-05 13:47:09 +000024#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
53} // end namespace
54
55struct SkPerlinNoiseShader::StitchData {
56 StitchData()
57 : fWidth(0)
58 , fWrapX(0)
59 , fHeight(0)
60 , fWrapY(0)
61 {}
62
63 bool operator==(const StitchData& other) const {
64 return fWidth == other.fWidth &&
65 fWrapX == other.fWrapX &&
66 fHeight == other.fHeight &&
67 fWrapY == other.fWrapY;
68 }
69
70 int fWidth; // How much to subtract to wrap for stitching.
71 int fWrapX; // Minimum value to wrap.
72 int fHeight;
73 int fWrapY;
74};
75
76struct SkPerlinNoiseShader::PaintingData {
commit-bot@chromium.orgfd5c9a62014-03-06 15:13:53 +000077 PaintingData(const SkISize& tileSize, SkScalar seed,
senorblancoca6a7c22014-06-27 13:35:52 -070078 SkScalar baseFrequencyX, SkScalar baseFrequencyY,
79 const SkMatrix& matrix)
sugoi@google.come3b4c502013-04-05 13:47:09 +000080 {
reed11fa2242015-03-13 06:08:28 -070081 SkVector vec[2] = {
82 { SkScalarInvert(baseFrequencyX), SkScalarInvert(baseFrequencyY) },
83 { SkIntToScalar(tileSize.fWidth), SkIntToScalar(tileSize.fHeight) },
84 };
85 matrix.mapVectors(vec, 2);
86
87 fBaseFrequency.set(SkScalarInvert(vec[0].fX), SkScalarInvert(vec[0].fY));
88 fTileSize.set(SkScalarRoundToInt(vec[1].fX), SkScalarRoundToInt(vec[1].fY));
commit-bot@chromium.orgfd5c9a62014-03-06 15:13:53 +000089 this->init(seed);
90 if (!fTileSize.isEmpty()) {
91 this->stitch();
92 }
93
senorblancof3b50272014-06-16 10:49:58 -070094#if SK_SUPPORT_GPU
commit-bot@chromium.orga3264e52014-05-30 13:26:10 +000095 fPermutationsBitmap.setInfo(SkImageInfo::MakeA8(kBlockSize, 1));
commit-bot@chromium.orgfd5c9a62014-03-06 15:13:53 +000096 fPermutationsBitmap.setPixels(fLatticeSelector);
97
commit-bot@chromium.orga3264e52014-05-30 13:26:10 +000098 fNoiseBitmap.setInfo(SkImageInfo::MakeN32Premul(kBlockSize, 4));
commit-bot@chromium.orgfd5c9a62014-03-06 15:13:53 +000099 fNoiseBitmap.setPixels(fNoise[0][0]);
100#endif
sugoi@google.come3b4c502013-04-05 13:47:09 +0000101 }
102
103 int fSeed;
104 uint8_t fLatticeSelector[kBlockSize];
105 uint16_t fNoise[4][kBlockSize][2];
106 SkPoint fGradient[4][kBlockSize];
107 SkISize fTileSize;
108 SkVector fBaseFrequency;
109 StitchData fStitchDataInit;
110
111private:
112
senorblancof3b50272014-06-16 10:49:58 -0700113#if SK_SUPPORT_GPU
commit-bot@chromium.orgfd5c9a62014-03-06 15:13:53 +0000114 SkBitmap fPermutationsBitmap;
115 SkBitmap fNoiseBitmap;
116#endif
sugoi@google.come3b4c502013-04-05 13:47:09 +0000117
118 inline int random() {
119 static const int gRandAmplitude = 16807; // 7**5; primitive root of m
120 static const int gRandQ = 127773; // m / a
121 static const int gRandR = 2836; // m % a
122
123 int result = gRandAmplitude * (fSeed % gRandQ) - gRandR * (fSeed / gRandQ);
124 if (result <= 0)
125 result += kRandMaximum;
126 fSeed = result;
127 return result;
128 }
129
commit-bot@chromium.orgfd5c9a62014-03-06 15:13:53 +0000130 // Only called once. Could be part of the constructor.
sugoi@google.come3b4c502013-04-05 13:47:09 +0000131 void init(SkScalar seed)
132 {
133 static const SkScalar gInvBlockSizef = SkScalarInvert(SkIntToScalar(kBlockSize));
134
senorblanco@chromium.org857e3202014-01-09 17:41:42 +0000135 // According to the SVG spec, we must truncate (not round) the seed value.
136 fSeed = SkScalarTruncToInt(seed);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000137 // The seed value clamp to the range [1, kRandMaximum - 1].
sugoi@google.come3b4c502013-04-05 13:47:09 +0000138 if (fSeed <= 0) {
139 fSeed = -(fSeed % (kRandMaximum - 1)) + 1;
140 }
141 if (fSeed > kRandMaximum - 1) {
142 fSeed = kRandMaximum - 1;
143 }
144 for (int channel = 0; channel < 4; ++channel) {
145 for (int i = 0; i < kBlockSize; ++i) {
146 fLatticeSelector[i] = i;
147 fNoise[channel][i][0] = (random() % (2 * kBlockSize));
148 fNoise[channel][i][1] = (random() % (2 * kBlockSize));
149 }
150 }
151 for (int i = kBlockSize - 1; i > 0; --i) {
152 int k = fLatticeSelector[i];
153 int j = random() % kBlockSize;
154 SkASSERT(j >= 0);
155 SkASSERT(j < kBlockSize);
156 fLatticeSelector[i] = fLatticeSelector[j];
157 fLatticeSelector[j] = k;
158 }
159
160 // Perform the permutations now
161 {
162 // Copy noise data
163 uint16_t noise[4][kBlockSize][2];
164 for (int i = 0; i < kBlockSize; ++i) {
165 for (int channel = 0; channel < 4; ++channel) {
166 for (int j = 0; j < 2; ++j) {
167 noise[channel][i][j] = fNoise[channel][i][j];
168 }
169 }
170 }
171 // Do permutations on noise data
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 fNoise[channel][i][j] = noise[channel][fLatticeSelector[i]][j];
176 }
177 }
178 }
179 }
180
181 // Half of the largest possible value for 16 bit unsigned int
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +0000182 static const SkScalar gHalfMax16bits = 32767.5f;
sugoi@google.come3b4c502013-04-05 13:47:09 +0000183
184 // Compute gradients from permutated noise data
185 for (int channel = 0; channel < 4; ++channel) {
186 for (int i = 0; i < kBlockSize; ++i) {
187 fGradient[channel][i] = SkPoint::Make(
188 SkScalarMul(SkIntToScalar(fNoise[channel][i][0] - kBlockSize),
189 gInvBlockSizef),
skia.committer@gmail.comcff02432013-04-06 07:01:10 +0000190 SkScalarMul(SkIntToScalar(fNoise[channel][i][1] - kBlockSize),
sugoi@google.come3b4c502013-04-05 13:47:09 +0000191 gInvBlockSizef));
192 fGradient[channel][i].normalize();
193 // Put the normalized gradient back into the noise data
194 fNoise[channel][i][0] = SkScalarRoundToInt(SkScalarMul(
sugoi@google.comd537af52013-06-10 13:59:25 +0000195 fGradient[channel][i].fX + SK_Scalar1, gHalfMax16bits));
sugoi@google.come3b4c502013-04-05 13:47:09 +0000196 fNoise[channel][i][1] = SkScalarRoundToInt(SkScalarMul(
sugoi@google.comd537af52013-06-10 13:59:25 +0000197 fGradient[channel][i].fY + SK_Scalar1, gHalfMax16bits));
sugoi@google.come3b4c502013-04-05 13:47:09 +0000198 }
199 }
sugoi@google.come3b4c502013-04-05 13:47:09 +0000200 }
201
commit-bot@chromium.orgfd5c9a62014-03-06 15:13:53 +0000202 // Only called once. Could be part of the constructor.
sugoi@google.come3b4c502013-04-05 13:47:09 +0000203 void stitch() {
204 SkScalar tileWidth = SkIntToScalar(fTileSize.width());
205 SkScalar tileHeight = SkIntToScalar(fTileSize.height());
206 SkASSERT(tileWidth > 0 && tileHeight > 0);
207 // When stitching tiled turbulence, the frequencies must be adjusted
208 // so that the tile borders will be continuous.
209 if (fBaseFrequency.fX) {
reed@google.com8015cdd2013-12-18 15:49:32 +0000210 SkScalar lowFrequencx =
211 SkScalarFloorToScalar(tileWidth * fBaseFrequency.fX) / tileWidth;
212 SkScalar highFrequencx =
213 SkScalarCeilToScalar(tileWidth * fBaseFrequency.fX) / tileWidth;
sugoi@google.come3b4c502013-04-05 13:47:09 +0000214 // BaseFrequency should be non-negative according to the standard.
reed80ea19c2015-05-12 10:37:34 -0700215 if (fBaseFrequency.fX / lowFrequencx < highFrequencx / fBaseFrequency.fX) {
sugoi@google.come3b4c502013-04-05 13:47:09 +0000216 fBaseFrequency.fX = lowFrequencx;
217 } else {
218 fBaseFrequency.fX = highFrequencx;
219 }
220 }
221 if (fBaseFrequency.fY) {
reed@google.com8015cdd2013-12-18 15:49:32 +0000222 SkScalar lowFrequency =
223 SkScalarFloorToScalar(tileHeight * fBaseFrequency.fY) / tileHeight;
224 SkScalar highFrequency =
225 SkScalarCeilToScalar(tileHeight * fBaseFrequency.fY) / tileHeight;
reed80ea19c2015-05-12 10:37:34 -0700226 if (fBaseFrequency.fY / lowFrequency < highFrequency / fBaseFrequency.fY) {
sugoi@google.come3b4c502013-04-05 13:47:09 +0000227 fBaseFrequency.fY = lowFrequency;
228 } else {
229 fBaseFrequency.fY = highFrequency;
230 }
231 }
232 // Set up TurbulenceInitial stitch values.
233 fStitchDataInit.fWidth =
reed@google.com8015cdd2013-12-18 15:49:32 +0000234 SkScalarRoundToInt(tileWidth * fBaseFrequency.fX);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000235 fStitchDataInit.fWrapX = kPerlinNoise + fStitchDataInit.fWidth;
236 fStitchDataInit.fHeight =
reed@google.com8015cdd2013-12-18 15:49:32 +0000237 SkScalarRoundToInt(tileHeight * fBaseFrequency.fY);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000238 fStitchDataInit.fWrapY = kPerlinNoise + fStitchDataInit.fHeight;
239 }
240
commit-bot@chromium.orgfd5c9a62014-03-06 15:13:53 +0000241public:
sugoi@google.come3b4c502013-04-05 13:47:09 +0000242
senorblancof3b50272014-06-16 10:49:58 -0700243#if SK_SUPPORT_GPU
commit-bot@chromium.orgfd5c9a62014-03-06 15:13:53 +0000244 const SkBitmap& getPermutationsBitmap() const { return fPermutationsBitmap; }
245
246 const SkBitmap& getNoiseBitmap() const { return fNoiseBitmap; }
247#endif
sugoi@google.come3b4c502013-04-05 13:47:09 +0000248};
249
250SkShader* SkPerlinNoiseShader::CreateFractalNoise(SkScalar baseFrequencyX, SkScalar baseFrequencyY,
251 int numOctaves, SkScalar seed,
252 const SkISize* tileSize) {
253 return SkNEW_ARGS(SkPerlinNoiseShader, (kFractalNoise_Type, baseFrequencyX, baseFrequencyY,
254 numOctaves, seed, tileSize));
255}
256
commit-bot@chromium.org9fbbcca2014-04-01 16:09:37 +0000257SkShader* SkPerlinNoiseShader::CreateTurbulence(SkScalar baseFrequencyX, SkScalar baseFrequencyY,
sugoi@google.come3b4c502013-04-05 13:47:09 +0000258 int numOctaves, SkScalar seed,
259 const SkISize* tileSize) {
260 return SkNEW_ARGS(SkPerlinNoiseShader, (kTurbulence_Type, baseFrequencyX, baseFrequencyY,
261 numOctaves, seed, tileSize));
262}
263
264SkPerlinNoiseShader::SkPerlinNoiseShader(SkPerlinNoiseShader::Type type,
265 SkScalar baseFrequencyX,
266 SkScalar baseFrequencyY,
267 int numOctaves,
268 SkScalar seed,
269 const SkISize* tileSize)
270 : fType(type)
271 , fBaseFrequencyX(baseFrequencyX)
272 , fBaseFrequencyY(baseFrequencyY)
commit-bot@chromium.orgce33d602013-11-25 21:46:31 +0000273 , fNumOctaves(numOctaves > 255 ? 255 : numOctaves/*[0,255] octaves allowed*/)
sugoi@google.come3b4c502013-04-05 13:47:09 +0000274 , fSeed(seed)
commit-bot@chromium.orgfd5c9a62014-03-06 15:13:53 +0000275 , fTileSize(NULL == tileSize ? SkISize::Make(0, 0) : *tileSize)
276 , fStitchTiles(!fTileSize.isEmpty())
sugoi@google.come3b4c502013-04-05 13:47:09 +0000277{
278 SkASSERT(numOctaves >= 0 && numOctaves < 256);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000279}
280
sugoi@google.come3b4c502013-04-05 13:47:09 +0000281SkPerlinNoiseShader::~SkPerlinNoiseShader() {
sugoi@google.come3b4c502013-04-05 13:47:09 +0000282}
283
reed9fa60da2014-08-21 07:59:51 -0700284SkFlattenable* SkPerlinNoiseShader::CreateProc(SkReadBuffer& buffer) {
285 Type type = (Type)buffer.readInt();
286 SkScalar freqX = buffer.readScalar();
287 SkScalar freqY = buffer.readScalar();
288 int octaves = buffer.readInt();
289 SkScalar seed = buffer.readScalar();
290 SkISize tileSize;
291 tileSize.fWidth = buffer.readInt();
292 tileSize.fHeight = buffer.readInt();
293
294 switch (type) {
295 case kFractalNoise_Type:
296 return SkPerlinNoiseShader::CreateFractalNoise(freqX, freqY, octaves, seed, &tileSize);
297 case kTurbulence_Type:
298 return SkPerlinNoiseShader::CreateTubulence(freqX, freqY, octaves, seed, &tileSize);
299 default:
300 return NULL;
301 }
302}
303
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +0000304void SkPerlinNoiseShader::flatten(SkWriteBuffer& buffer) const {
sugoi@google.come3b4c502013-04-05 13:47:09 +0000305 buffer.writeInt((int) fType);
306 buffer.writeScalar(fBaseFrequencyX);
307 buffer.writeScalar(fBaseFrequencyY);
308 buffer.writeInt(fNumOctaves);
309 buffer.writeScalar(fSeed);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000310 buffer.writeInt(fTileSize.fWidth);
311 buffer.writeInt(fTileSize.fHeight);
312}
313
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000314SkScalar SkPerlinNoiseShader::PerlinNoiseShaderContext::noise2D(
senorblancoca6a7c22014-06-27 13:35:52 -0700315 int channel, const StitchData& stitchData, const SkPoint& noiseVector) const {
sugoi@google.come3b4c502013-04-05 13:47:09 +0000316 struct Noise {
317 int noisePositionIntegerValue;
senorblancoce6a3542014-06-12 11:24:19 -0700318 int nextNoisePositionIntegerValue;
sugoi@google.come3b4c502013-04-05 13:47:09 +0000319 SkScalar noisePositionFractionValue;
320 Noise(SkScalar component)
321 {
322 SkScalar position = component + kPerlinNoise;
323 noisePositionIntegerValue = SkScalarFloorToInt(position);
324 noisePositionFractionValue = position - SkIntToScalar(noisePositionIntegerValue);
senorblancoce6a3542014-06-12 11:24:19 -0700325 nextNoisePositionIntegerValue = noisePositionIntegerValue + 1;
sugoi@google.come3b4c502013-04-05 13:47:09 +0000326 }
327 };
328 Noise noiseX(noiseVector.x());
329 Noise noiseY(noiseVector.y());
330 SkScalar u, v;
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000331 const SkPerlinNoiseShader& perlinNoiseShader = static_cast<const SkPerlinNoiseShader&>(fShader);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000332 // If stitching, adjust lattice points accordingly.
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000333 if (perlinNoiseShader.fStitchTiles) {
sugoi@google.come3b4c502013-04-05 13:47:09 +0000334 noiseX.noisePositionIntegerValue =
335 checkNoise(noiseX.noisePositionIntegerValue, stitchData.fWrapX, stitchData.fWidth);
336 noiseY.noisePositionIntegerValue =
337 checkNoise(noiseY.noisePositionIntegerValue, stitchData.fWrapY, stitchData.fHeight);
senorblancoce6a3542014-06-12 11:24:19 -0700338 noiseX.nextNoisePositionIntegerValue =
339 checkNoise(noiseX.nextNoisePositionIntegerValue, stitchData.fWrapX, stitchData.fWidth);
340 noiseY.nextNoisePositionIntegerValue =
341 checkNoise(noiseY.nextNoisePositionIntegerValue, stitchData.fWrapY, stitchData.fHeight);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000342 }
343 noiseX.noisePositionIntegerValue &= kBlockMask;
344 noiseY.noisePositionIntegerValue &= kBlockMask;
senorblancoce6a3542014-06-12 11:24:19 -0700345 noiseX.nextNoisePositionIntegerValue &= kBlockMask;
346 noiseY.nextNoisePositionIntegerValue &= kBlockMask;
347 int i =
senorblancoca6a7c22014-06-27 13:35:52 -0700348 fPaintingData->fLatticeSelector[noiseX.noisePositionIntegerValue];
senorblancoce6a3542014-06-12 11:24:19 -0700349 int j =
senorblancoca6a7c22014-06-27 13:35:52 -0700350 fPaintingData->fLatticeSelector[noiseX.nextNoisePositionIntegerValue];
senorblancoce6a3542014-06-12 11:24:19 -0700351 int b00 = (i + noiseY.noisePositionIntegerValue) & kBlockMask;
352 int b10 = (j + noiseY.noisePositionIntegerValue) & kBlockMask;
353 int b01 = (i + noiseY.nextNoisePositionIntegerValue) & kBlockMask;
354 int b11 = (j + noiseY.nextNoisePositionIntegerValue) & kBlockMask;
sugoi@google.come3b4c502013-04-05 13:47:09 +0000355 SkScalar sx = smoothCurve(noiseX.noisePositionFractionValue);
356 SkScalar sy = smoothCurve(noiseY.noisePositionFractionValue);
357 // This is taken 1:1 from SVG spec: http://www.w3.org/TR/SVG11/filters.html#feTurbulenceElement
358 SkPoint fractionValue = SkPoint::Make(noiseX.noisePositionFractionValue,
359 noiseY.noisePositionFractionValue); // Offset (0,0)
senorblancoca6a7c22014-06-27 13:35:52 -0700360 u = fPaintingData->fGradient[channel][b00].dot(fractionValue);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000361 fractionValue.fX -= SK_Scalar1; // Offset (-1,0)
senorblancoca6a7c22014-06-27 13:35:52 -0700362 v = fPaintingData->fGradient[channel][b10].dot(fractionValue);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000363 SkScalar a = SkScalarInterp(u, v, sx);
364 fractionValue.fY -= SK_Scalar1; // Offset (-1,-1)
senorblancoca6a7c22014-06-27 13:35:52 -0700365 v = fPaintingData->fGradient[channel][b11].dot(fractionValue);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000366 fractionValue.fX = noiseX.noisePositionFractionValue; // Offset (0,-1)
senorblancoca6a7c22014-06-27 13:35:52 -0700367 u = fPaintingData->fGradient[channel][b01].dot(fractionValue);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000368 SkScalar b = SkScalarInterp(u, v, sx);
369 return SkScalarInterp(a, b, sy);
370}
371
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000372SkScalar SkPerlinNoiseShader::PerlinNoiseShaderContext::calculateTurbulenceValueForPoint(
senorblancoca6a7c22014-06-27 13:35:52 -0700373 int channel, StitchData& stitchData, const SkPoint& point) const {
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000374 const SkPerlinNoiseShader& perlinNoiseShader = static_cast<const SkPerlinNoiseShader&>(fShader);
375 if (perlinNoiseShader.fStitchTiles) {
sugoi@google.come3b4c502013-04-05 13:47:09 +0000376 // Set up TurbulenceInitial stitch values.
senorblancoca6a7c22014-06-27 13:35:52 -0700377 stitchData = fPaintingData->fStitchDataInit;
sugoi@google.come3b4c502013-04-05 13:47:09 +0000378 }
379 SkScalar turbulenceFunctionResult = 0;
senorblancoca6a7c22014-06-27 13:35:52 -0700380 SkPoint noiseVector(SkPoint::Make(SkScalarMul(point.x(), fPaintingData->fBaseFrequency.fX),
381 SkScalarMul(point.y(), fPaintingData->fBaseFrequency.fY)));
sugoi@google.come3b4c502013-04-05 13:47:09 +0000382 SkScalar ratio = SK_Scalar1;
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000383 for (int octave = 0; octave < perlinNoiseShader.fNumOctaves; ++octave) {
senorblancoca6a7c22014-06-27 13:35:52 -0700384 SkScalar noise = noise2D(channel, stitchData, noiseVector);
reed80ea19c2015-05-12 10:37:34 -0700385 SkScalar numer = (perlinNoiseShader.fType == kFractalNoise_Type) ?
386 noise : SkScalarAbs(noise);
387 turbulenceFunctionResult += numer / ratio;
sugoi@google.come3b4c502013-04-05 13:47:09 +0000388 noiseVector.fX *= 2;
389 noiseVector.fY *= 2;
390 ratio *= 2;
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000391 if (perlinNoiseShader.fStitchTiles) {
sugoi@google.come3b4c502013-04-05 13:47:09 +0000392 // Update stitch values
393 stitchData.fWidth *= 2;
394 stitchData.fWrapX = stitchData.fWidth + kPerlinNoise;
395 stitchData.fHeight *= 2;
396 stitchData.fWrapY = stitchData.fHeight + kPerlinNoise;
397 }
398 }
399
400 // The value of turbulenceFunctionResult comes from ((turbulenceFunctionResult) + 1) / 2
401 // by fractalNoise and (turbulenceFunctionResult) by turbulence.
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000402 if (perlinNoiseShader.fType == kFractalNoise_Type) {
sugoi@google.come3b4c502013-04-05 13:47:09 +0000403 turbulenceFunctionResult =
404 SkScalarMul(turbulenceFunctionResult, SK_ScalarHalf) + SK_ScalarHalf;
405 }
406
407 if (channel == 3) { // Scale alpha by paint value
reed80ea19c2015-05-12 10:37:34 -0700408 turbulenceFunctionResult *= SkIntToScalar(getPaintAlpha()) / 255;
sugoi@google.come3b4c502013-04-05 13:47:09 +0000409 }
410
411 // Clamp result
412 return SkScalarPin(turbulenceFunctionResult, 0, SK_Scalar1);
413}
414
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000415SkPMColor SkPerlinNoiseShader::PerlinNoiseShaderContext::shade(
416 const SkPoint& point, StitchData& stitchData) const {
sugoi@google.come3b4c502013-04-05 13:47:09 +0000417 SkPoint newPoint;
senorblanco@chromium.orga8d95f82014-04-04 14:46:10 +0000418 fMatrix.mapPoints(&newPoint, &point, 1);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000419 newPoint.fX = SkScalarRoundToScalar(newPoint.fX);
420 newPoint.fY = SkScalarRoundToScalar(newPoint.fY);
421
422 U8CPU rgba[4];
423 for (int channel = 3; channel >= 0; --channel) {
424 rgba[channel] = SkScalarFloorToInt(255 *
senorblancoca6a7c22014-06-27 13:35:52 -0700425 calculateTurbulenceValueForPoint(channel, stitchData, newPoint));
sugoi@google.come3b4c502013-04-05 13:47:09 +0000426 }
427 return SkPreMultiplyARGB(rgba[3], rgba[0], rgba[1], rgba[2]);
428}
429
commit-bot@chromium.orgce56d962014-05-05 18:39:18 +0000430SkShader::Context* SkPerlinNoiseShader::onCreateContext(const ContextRec& rec,
431 void* storage) const {
commit-bot@chromium.orge901b6d2014-05-01 19:31:31 +0000432 return SkNEW_PLACEMENT_ARGS(storage, PerlinNoiseShaderContext, (*this, rec));
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000433}
434
435size_t SkPerlinNoiseShader::contextSize() const {
436 return sizeof(PerlinNoiseShaderContext);
437}
438
439SkPerlinNoiseShader::PerlinNoiseShaderContext::PerlinNoiseShaderContext(
commit-bot@chromium.orge901b6d2014-05-01 19:31:31 +0000440 const SkPerlinNoiseShader& shader, const ContextRec& rec)
441 : INHERITED(shader, rec)
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000442{
commit-bot@chromium.orge901b6d2014-05-01 19:31:31 +0000443 SkMatrix newMatrix = *rec.fMatrix;
reed@google.comb67b8e62014-05-12 18:12:24 +0000444 newMatrix.preConcat(shader.getLocalMatrix());
445 if (rec.fLocalMatrix) {
446 newMatrix.preConcat(*rec.fLocalMatrix);
447 }
senorblanco@chromium.orga8d95f82014-04-04 14:46:10 +0000448 // This (1,1) translation is due to WebKit's 1 based coordinates for the noise
449 // (as opposed to 0 based, usually). The same adjustment is in the setData() function.
senorblancoca6a7c22014-06-27 13:35:52 -0700450 fMatrix.setTranslate(-newMatrix.getTranslateX() + SK_Scalar1, -newMatrix.getTranslateY() + SK_Scalar1);
451 fPaintingData = SkNEW_ARGS(PaintingData, (shader.fTileSize, shader.fSeed, shader.fBaseFrequencyX, shader.fBaseFrequencyY, newMatrix));
452}
453
454SkPerlinNoiseShader::PerlinNoiseShaderContext::~PerlinNoiseShaderContext() {
455 SkDELETE(fPaintingData);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000456}
457
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000458void SkPerlinNoiseShader::PerlinNoiseShaderContext::shadeSpan(
459 int x, int y, SkPMColor result[], int count) {
sugoi@google.come3b4c502013-04-05 13:47:09 +0000460 SkPoint point = SkPoint::Make(SkIntToScalar(x), SkIntToScalar(y));
461 StitchData stitchData;
462 for (int i = 0; i < count; ++i) {
463 result[i] = shade(point, stitchData);
464 point.fX += SK_Scalar1;
465 }
466}
467
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000468void SkPerlinNoiseShader::PerlinNoiseShaderContext::shadeSpan16(
469 int x, int y, uint16_t result[], int count) {
sugoi@google.come3b4c502013-04-05 13:47:09 +0000470 SkPoint point = SkPoint::Make(SkIntToScalar(x), SkIntToScalar(y));
471 StitchData stitchData;
472 DITHER_565_SCAN(y);
473 for (int i = 0; i < count; ++i) {
474 unsigned dither = DITHER_VALUE(x);
475 result[i] = SkDitherRGB32To565(shade(point, stitchData), dither);
476 DITHER_INC_X(x);
477 point.fX += SK_Scalar1;
478 }
479}
480
481/////////////////////////////////////////////////////////////////////
482
commit-bot@chromium.org344cf452013-06-17 14:19:01 +0000483#if SK_SUPPORT_GPU
sugoi@google.come3b4c502013-04-05 13:47:09 +0000484
joshualittb0a8a372014-09-23 09:50:21 -0700485class GrGLPerlinNoise : public GrGLFragmentProcessor {
sugoi@google.com4775cba2013-04-17 13:46:56 +0000486public:
joshualitteb2a6762014-12-04 11:35:33 -0800487 GrGLPerlinNoise(const GrProcessor&);
sugoi@google.com4775cba2013-04-17 13:46:56 +0000488 virtual ~GrGLPerlinNoise() {}
sugoi@google.come3b4c502013-04-05 13:47:09 +0000489
wangyix7c157a92015-07-22 15:08:53 -0700490 virtual void emitCode(EmitArgs&) override;
sugoi@google.come3b4c502013-04-05 13:47:09 +0000491
jvanverthcfc18862015-04-28 08:48:20 -0700492 static inline void GenKey(const GrProcessor&, const GrGLSLCaps&, GrProcessorKeyBuilder* b);
sugoi@google.com4775cba2013-04-17 13:46:56 +0000493
wangyixb1daa862015-08-18 11:29:31 -0700494protected:
495 void onSetData(const GrGLProgramDataManager&, const GrProcessor&) override;
496
sugoi@google.com4775cba2013-04-17 13:46:56 +0000497private:
sugoi@google.com4775cba2013-04-17 13:46:56 +0000498
kkinnunen7510b222014-07-30 00:04:16 -0700499 GrGLProgramDataManager::UniformHandle fStitchDataUni;
500 SkPerlinNoiseShader::Type fType;
501 bool fStitchTiles;
502 int fNumOctaves;
503 GrGLProgramDataManager::UniformHandle fBaseFrequencyUni;
504 GrGLProgramDataManager::UniformHandle fAlphaUni;
senorblancof3b50272014-06-16 10:49:58 -0700505
506private:
joshualittb0a8a372014-09-23 09:50:21 -0700507 typedef GrGLFragmentProcessor INHERITED;
sugoi@google.come3b4c502013-04-05 13:47:09 +0000508};
509
510/////////////////////////////////////////////////////////////////////
511
joshualittb0a8a372014-09-23 09:50:21 -0700512class GrPerlinNoiseEffect : public GrFragmentProcessor {
sugoi@google.come3b4c502013-04-05 13:47:09 +0000513public:
joshualitt9cc17752015-07-09 06:28:14 -0700514 static GrFragmentProcessor* Create(GrProcessorDataManager* procDataManager,
joshualittb2456052015-07-08 09:36:59 -0700515 SkPerlinNoiseShader::Type type,
joshualittb0a8a372014-09-23 09:50:21 -0700516 int numOctaves, bool stitchTiles,
517 SkPerlinNoiseShader::PaintingData* paintingData,
518 GrTexture* permutationsTexture, GrTexture* noiseTexture,
519 const SkMatrix& matrix, uint8_t alpha) {
joshualitt9cc17752015-07-09 06:28:14 -0700520 return SkNEW_ARGS(GrPerlinNoiseEffect, (procDataManager, type, numOctaves, stitchTiles,
joshualittb2456052015-07-08 09:36:59 -0700521 paintingData, permutationsTexture, noiseTexture,
522 matrix, alpha));
sugoi@google.come3b4c502013-04-05 13:47:09 +0000523 }
524
senorblancoca6a7c22014-06-27 13:35:52 -0700525 virtual ~GrPerlinNoiseEffect() {
526 SkDELETE(fPaintingData);
527 }
sugoi@google.come3b4c502013-04-05 13:47:09 +0000528
mtklein36352bf2015-03-25 18:17:31 -0700529 const char* name() const override { return "PerlinNoise"; }
joshualitteb2a6762014-12-04 11:35:33 -0800530
senorblancoca6a7c22014-06-27 13:35:52 -0700531 const SkPerlinNoiseShader::StitchData& stitchData() const { return fPaintingData->fStitchDataInit; }
sugoi@google.come3b4c502013-04-05 13:47:09 +0000532
senorblancof3b50272014-06-16 10:49:58 -0700533 SkPerlinNoiseShader::Type type() const { return fType; }
534 bool stitchTiles() const { return fStitchTiles; }
senorblancoca6a7c22014-06-27 13:35:52 -0700535 const SkVector& baseFrequency() const { return fPaintingData->fBaseFrequency; }
senorblancof3b50272014-06-16 10:49:58 -0700536 int numOctaves() const { return fNumOctaves; }
537 const SkMatrix& matrix() const { return fCoordTransform.getMatrix(); }
538 uint8_t alpha() const { return fAlpha; }
539
sugoi@google.come3b4c502013-04-05 13:47:09 +0000540private:
wangyixb1daa862015-08-18 11:29:31 -0700541 GrGLFragmentProcessor* onCreateGLInstance() const override {
542 return SkNEW_ARGS(GrGLPerlinNoise, (*this));
543 }
544
wangyix4b3050b2015-08-04 07:59:37 -0700545 virtual void onGetGLProcessorKey(const GrGLSLCaps& caps,
546 GrProcessorKeyBuilder* b) const override {
547 GrGLPerlinNoise::GenKey(*this, caps, b);
548 }
549
mtklein36352bf2015-03-25 18:17:31 -0700550 bool onIsEqual(const GrFragmentProcessor& sBase) const override {
joshualitt49586be2014-09-16 08:21:41 -0700551 const GrPerlinNoiseEffect& s = sBase.cast<GrPerlinNoiseEffect>();
senorblancof3b50272014-06-16 10:49:58 -0700552 return fType == s.fType &&
senorblancoca6a7c22014-06-27 13:35:52 -0700553 fPaintingData->fBaseFrequency == s.fPaintingData->fBaseFrequency &&
senorblancof3b50272014-06-16 10:49:58 -0700554 fNumOctaves == s.fNumOctaves &&
555 fStitchTiles == s.fStitchTiles &&
senorblancof3b50272014-06-16 10:49:58 -0700556 fAlpha == s.fAlpha &&
senorblancoca6a7c22014-06-27 13:35:52 -0700557 fPaintingData->fStitchDataInit == s.fPaintingData->fStitchDataInit;
sugoi@google.come3b4c502013-04-05 13:47:09 +0000558 }
559
mtklein36352bf2015-03-25 18:17:31 -0700560 void onComputeInvariantOutput(GrInvariantOutput* inout) const override {
egdaniel605dd0f2014-11-12 08:35:25 -0800561 inout->setToUnknown(GrInvariantOutput::kWillNot_ReadInput);
egdaniel1a8ecdf2014-10-03 06:24:12 -0700562 }
563
joshualitt9cc17752015-07-09 06:28:14 -0700564 GrPerlinNoiseEffect(GrProcessorDataManager*, SkPerlinNoiseShader::Type type,
sugoi@google.come3b4c502013-04-05 13:47:09 +0000565 int numOctaves, bool stitchTiles,
senorblancoca6a7c22014-06-27 13:35:52 -0700566 SkPerlinNoiseShader::PaintingData* paintingData,
sugoi@google.come3b4c502013-04-05 13:47:09 +0000567 GrTexture* permutationsTexture, GrTexture* noiseTexture,
568 const SkMatrix& matrix, uint8_t alpha)
senorblancof3b50272014-06-16 10:49:58 -0700569 : fType(type)
senorblancof3b50272014-06-16 10:49:58 -0700570 , fNumOctaves(numOctaves)
571 , fStitchTiles(stitchTiles)
572 , fAlpha(alpha)
sugoi@google.com4775cba2013-04-17 13:46:56 +0000573 , fPermutationsAccess(permutationsTexture)
sugoi@google.come3b4c502013-04-05 13:47:09 +0000574 , fNoiseAccess(noiseTexture)
senorblancoca6a7c22014-06-27 13:35:52 -0700575 , fPaintingData(paintingData) {
joshualitteb2a6762014-12-04 11:35:33 -0800576 this->initClassID<GrPerlinNoiseEffect>();
sugoi@google.come3b4c502013-04-05 13:47:09 +0000577 this->addTextureAccess(&fPermutationsAccess);
578 this->addTextureAccess(&fNoiseAccess);
senorblancoca6a7c22014-06-27 13:35:52 -0700579 fCoordTransform.reset(kLocal_GrCoordSet, matrix);
senorblancof3b50272014-06-16 10:49:58 -0700580 this->addCoordTransform(&fCoordTransform);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000581 }
582
joshualittb0a8a372014-09-23 09:50:21 -0700583 GR_DECLARE_FRAGMENT_PROCESSOR_TEST;
sugoi@google.come3b4c502013-04-05 13:47:09 +0000584
senorblancof3b50272014-06-16 10:49:58 -0700585 SkPerlinNoiseShader::Type fType;
586 GrCoordTransform fCoordTransform;
senorblancof3b50272014-06-16 10:49:58 -0700587 int fNumOctaves;
588 bool fStitchTiles;
589 uint8_t fAlpha;
sugoi@google.come3b4c502013-04-05 13:47:09 +0000590 GrTextureAccess fPermutationsAccess;
591 GrTextureAccess fNoiseAccess;
senorblancoca6a7c22014-06-27 13:35:52 -0700592 SkPerlinNoiseShader::PaintingData *fPaintingData;
sugoi@google.come3b4c502013-04-05 13:47:09 +0000593
sugoi@google.com4775cba2013-04-17 13:46:56 +0000594private:
joshualittb0a8a372014-09-23 09:50:21 -0700595 typedef GrFragmentProcessor INHERITED;
sugoi@google.come3b4c502013-04-05 13:47:09 +0000596};
597
598/////////////////////////////////////////////////////////////////////
joshualittb0a8a372014-09-23 09:50:21 -0700599GR_DEFINE_FRAGMENT_PROCESSOR_TEST(GrPerlinNoiseEffect);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000600
joshualitt0067ff52015-07-08 14:26:19 -0700601GrFragmentProcessor* GrPerlinNoiseEffect::TestCreate(GrProcessorTestData* d) {
602 int numOctaves = d->fRandom->nextRangeU(2, 10);
603 bool stitchTiles = d->fRandom->nextBool();
604 SkScalar seed = SkIntToScalar(d->fRandom->nextU());
605 SkISize tileSize = SkISize::Make(d->fRandom->nextRangeU(4, 4096),
606 d->fRandom->nextRangeU(4, 4096));
607 SkScalar baseFrequencyX = d->fRandom->nextRangeScalar(0.01f,
608 0.99f);
609 SkScalar baseFrequencyY = d->fRandom->nextRangeScalar(0.01f,
610 0.99f);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000611
joshualitt0067ff52015-07-08 14:26:19 -0700612 SkShader* shader = d->fRandom->nextBool() ?
sugoi@google.come3b4c502013-04-05 13:47:09 +0000613 SkPerlinNoiseShader::CreateFractalNoise(baseFrequencyX, baseFrequencyY, numOctaves, seed,
614 stitchTiles ? &tileSize : NULL) :
commit-bot@chromium.org9fbbcca2014-04-01 16:09:37 +0000615 SkPerlinNoiseShader::CreateTurbulence(baseFrequencyX, baseFrequencyY, numOctaves, seed,
sugoi@google.come3b4c502013-04-05 13:47:09 +0000616 stitchTiles ? &tileSize : NULL);
617
618 SkPaint paint;
bsalomon83d081a2014-07-08 09:56:10 -0700619 GrColor paintColor;
joshualittb0a8a372014-09-23 09:50:21 -0700620 GrFragmentProcessor* effect;
joshualitt8ca93e72015-07-08 06:51:43 -0700621 GrPaint grPaint;
joshualitt0067ff52015-07-08 14:26:19 -0700622 SkAssertResult(shader->asFragmentProcessor(d->fContext, paint,
623 GrTest::TestMatrix(d->fRandom), NULL,
joshualitt9cc17752015-07-09 06:28:14 -0700624 &paintColor, grPaint.getProcessorDataManager(),
joshualitt8ca93e72015-07-08 06:51:43 -0700625 &effect));
sugoi@google.come3b4c502013-04-05 13:47:09 +0000626
627 SkDELETE(shader);
628
629 return effect;
630}
sugoi@google.com4775cba2013-04-17 13:46:56 +0000631
joshualitteb2a6762014-12-04 11:35:33 -0800632GrGLPerlinNoise::GrGLPerlinNoise(const GrProcessor& processor)
633 : fType(processor.cast<GrPerlinNoiseEffect>().type())
joshualittb0a8a372014-09-23 09:50:21 -0700634 , fStitchTiles(processor.cast<GrPerlinNoiseEffect>().stitchTiles())
635 , fNumOctaves(processor.cast<GrPerlinNoiseEffect>().numOctaves()) {
sugoi@google.com4775cba2013-04-17 13:46:56 +0000636}
637
wangyix7c157a92015-07-22 15:08:53 -0700638void GrGLPerlinNoise::emitCode(EmitArgs& args) {
639 GrGLFragmentBuilder* fsBuilder = args.fBuilder->getFragmentShaderBuilder();
640 SkString vCoords = fsBuilder->ensureFSCoords2D(args.fCoords, 0);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000641
wangyix7c157a92015-07-22 15:08:53 -0700642 fBaseFrequencyUni = args.fBuilder->addUniform(GrGLProgramBuilder::kFragment_Visibility,
bsalomon422f56f2014-12-09 10:18:12 -0800643 kVec2f_GrSLType, kDefault_GrSLPrecision,
644 "baseFrequency");
wangyix7c157a92015-07-22 15:08:53 -0700645 const char* baseFrequencyUni = args.fBuilder->getUniformCStr(fBaseFrequencyUni);
646 fAlphaUni = args.fBuilder->addUniform(GrGLProgramBuilder::kFragment_Visibility,
bsalomon422f56f2014-12-09 10:18:12 -0800647 kFloat_GrSLType, kDefault_GrSLPrecision,
648 "alpha");
wangyix7c157a92015-07-22 15:08:53 -0700649 const char* alphaUni = args.fBuilder->getUniformCStr(fAlphaUni);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000650
651 const char* stitchDataUni = NULL;
652 if (fStitchTiles) {
wangyix7c157a92015-07-22 15:08:53 -0700653 fStitchDataUni = args.fBuilder->addUniform(GrGLProgramBuilder::kFragment_Visibility,
bsalomon422f56f2014-12-09 10:18:12 -0800654 kVec2f_GrSLType, kDefault_GrSLPrecision,
655 "stitchData");
wangyix7c157a92015-07-22 15:08:53 -0700656 stitchDataUni = args.fBuilder->getUniformCStr(fStitchDataUni);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000657 }
658
sugoi@google.comd537af52013-06-10 13:59:25 +0000659 // There are 4 lines, so the center of each line is 1/8, 3/8, 5/8 and 7/8
660 const char* chanCoordR = "0.125";
661 const char* chanCoordG = "0.375";
662 const char* chanCoordB = "0.625";
663 const char* chanCoordA = "0.875";
664 const char* chanCoord = "chanCoord";
sugoi@google.come3b4c502013-04-05 13:47:09 +0000665 const char* stitchData = "stitchData";
666 const char* ratio = "ratio";
sugoi@google.come3b4c502013-04-05 13:47:09 +0000667 const char* noiseVec = "noiseVec";
sugoi@google.come3b4c502013-04-05 13:47:09 +0000668 const char* noiseSmooth = "noiseSmooth";
senorblancoce6a3542014-06-12 11:24:19 -0700669 const char* floorVal = "floorVal";
sugoi@google.come3b4c502013-04-05 13:47:09 +0000670 const char* fractVal = "fractVal";
671 const char* uv = "uv";
672 const char* ab = "ab";
673 const char* latticeIdx = "latticeIdx";
senorblancoce6a3542014-06-12 11:24:19 -0700674 const char* bcoords = "bcoords";
sugoi@google.come3b4c502013-04-05 13:47:09 +0000675 const char* lattice = "lattice";
sugoi@google.come3b4c502013-04-05 13:47:09 +0000676 const char* inc8bit = "0.00390625"; // 1.0 / 256.0
677 // This is the math to convert the two 16bit integer packed into rgba 8 bit input into a
678 // [-1,1] vector and perform a dot product between that vector and the provided vector.
679 const char* dotLattice = "dot(((%s.ga + %s.rb * vec2(%s)) * vec2(2.0) - vec2(1.0)), %s);";
680
sugoi@google.comd537af52013-06-10 13:59:25 +0000681 // Add noise function
682 static const GrGLShaderVar gPerlinNoiseArgs[] = {
683 GrGLShaderVar(chanCoord, kFloat_GrSLType),
commit-bot@chromium.org98393202013-07-04 18:13:05 +0000684 GrGLShaderVar(noiseVec, kVec2f_GrSLType)
sugoi@google.comd537af52013-06-10 13:59:25 +0000685 };
sugoi@google.come3b4c502013-04-05 13:47:09 +0000686
sugoi@google.comd537af52013-06-10 13:59:25 +0000687 static const GrGLShaderVar gPerlinNoiseStitchArgs[] = {
688 GrGLShaderVar(chanCoord, kFloat_GrSLType),
commit-bot@chromium.org98393202013-07-04 18:13:05 +0000689 GrGLShaderVar(noiseVec, kVec2f_GrSLType),
690 GrGLShaderVar(stitchData, kVec2f_GrSLType)
sugoi@google.comd537af52013-06-10 13:59:25 +0000691 };
sugoi@google.come3b4c502013-04-05 13:47:09 +0000692
sugoi@google.comd537af52013-06-10 13:59:25 +0000693 SkString noiseCode;
sugoi@google.come3b4c502013-04-05 13:47:09 +0000694
senorblancoce6a3542014-06-12 11:24:19 -0700695 noiseCode.appendf("\tvec4 %s;\n", floorVal);
696 noiseCode.appendf("\t%s.xy = floor(%s);\n", floorVal, noiseVec);
697 noiseCode.appendf("\t%s.zw = %s.xy + vec2(1.0);\n", floorVal, floorVal);
698 noiseCode.appendf("\tvec2 %s = fract(%s);\n", fractVal, noiseVec);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000699
700 // smooth curve : t * t * (3 - 2 * t)
senorblancoce6a3542014-06-12 11:24:19 -0700701 noiseCode.appendf("\n\tvec2 %s = %s * %s * (vec2(3.0) - vec2(2.0) * %s);",
702 noiseSmooth, fractVal, fractVal, fractVal);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000703
704 // Adjust frequencies if we're stitching tiles
705 if (fStitchTiles) {
commit-bot@chromium.org98393202013-07-04 18:13:05 +0000706 noiseCode.appendf("\n\tif(%s.x >= %s.x) { %s.x -= %s.x; }",
senorblancoce6a3542014-06-12 11:24:19 -0700707 floorVal, stitchData, floorVal, stitchData);
commit-bot@chromium.org98393202013-07-04 18:13:05 +0000708 noiseCode.appendf("\n\tif(%s.y >= %s.y) { %s.y -= %s.y; }",
senorblancoce6a3542014-06-12 11:24:19 -0700709 floorVal, stitchData, floorVal, stitchData);
710 noiseCode.appendf("\n\tif(%s.z >= %s.x) { %s.z -= %s.x; }",
711 floorVal, stitchData, floorVal, stitchData);
712 noiseCode.appendf("\n\tif(%s.w >= %s.y) { %s.w -= %s.y; }",
713 floorVal, stitchData, floorVal, stitchData);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000714 }
715
716 // Get texture coordinates and normalize
senorblancoce6a3542014-06-12 11:24:19 -0700717 noiseCode.appendf("\n\t%s = fract(floor(mod(%s, 256.0)) / vec4(256.0));\n",
718 floorVal, floorVal);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000719
720 // Get permutation for x
721 {
722 SkString xCoords("");
senorblancoce6a3542014-06-12 11:24:19 -0700723 xCoords.appendf("vec2(%s.x, 0.5)", floorVal);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000724
sugoi@google.comd537af52013-06-10 13:59:25 +0000725 noiseCode.appendf("\n\tvec2 %s;\n\t%s.x = ", latticeIdx, latticeIdx);
wangyix7c157a92015-07-22 15:08:53 -0700726 fsBuilder->appendTextureLookup(&noiseCode, args.fSamplers[0], xCoords.c_str(),
727 kVec2f_GrSLType);
sugoi@google.comd537af52013-06-10 13:59:25 +0000728 noiseCode.append(".r;");
sugoi@google.come3b4c502013-04-05 13:47:09 +0000729 }
730
731 // Get permutation for x + 1
732 {
733 SkString xCoords("");
senorblancoce6a3542014-06-12 11:24:19 -0700734 xCoords.appendf("vec2(%s.z, 0.5)", floorVal);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000735
sugoi@google.comd537af52013-06-10 13:59:25 +0000736 noiseCode.appendf("\n\t%s.y = ", latticeIdx);
wangyix7c157a92015-07-22 15:08:53 -0700737 fsBuilder->appendTextureLookup(&noiseCode, args.fSamplers[0], xCoords.c_str(),
738 kVec2f_GrSLType);
sugoi@google.comd537af52013-06-10 13:59:25 +0000739 noiseCode.append(".r;");
sugoi@google.come3b4c502013-04-05 13:47:09 +0000740 }
741
commit-bot@chromium.org344cf452013-06-17 14:19:01 +0000742#if defined(SK_BUILD_FOR_ANDROID)
743 // Android rounding for Tegra devices, like, for example: Xoom (Tegra 2), Nexus 7 (Tegra 3).
744 // The issue is that colors aren't accurate enough on Tegra devices. For example, if an 8 bit
745 // value of 124 (or 0.486275 here) is entered, we can get a texture value of 123.513725
746 // (or 0.484368 here). The following rounding operation prevents these precision issues from
747 // affecting the result of the noise by making sure that we only have multiples of 1/255.
748 // (Note that 1/255 is about 0.003921569, which is the value used here).
749 noiseCode.appendf("\n\t%s = floor(%s * vec2(255.0) + vec2(0.5)) * vec2(0.003921569);",
750 latticeIdx, latticeIdx);
751#endif
752
sugoi@google.come3b4c502013-04-05 13:47:09 +0000753 // Get (x,y) coordinates with the permutated x
senorblancoce6a3542014-06-12 11:24:19 -0700754 noiseCode.appendf("\n\tvec4 %s = fract(%s.xyxy + %s.yyww);", bcoords, latticeIdx, floorVal);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000755
sugoi@google.comd537af52013-06-10 13:59:25 +0000756 noiseCode.appendf("\n\n\tvec2 %s;", uv);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000757 // Compute u, at offset (0,0)
758 {
759 SkString latticeCoords("");
senorblancoce6a3542014-06-12 11:24:19 -0700760 latticeCoords.appendf("vec2(%s.x, %s)", bcoords, chanCoord);
sugoi@google.comd537af52013-06-10 13:59:25 +0000761 noiseCode.appendf("\n\tvec4 %s = ", lattice);
wangyix7c157a92015-07-22 15:08:53 -0700762 fsBuilder->appendTextureLookup(&noiseCode, args.fSamplers[1], latticeCoords.c_str(),
sugoi@google.comd537af52013-06-10 13:59:25 +0000763 kVec2f_GrSLType);
764 noiseCode.appendf(".bgra;\n\t%s.x = ", uv);
765 noiseCode.appendf(dotLattice, lattice, lattice, inc8bit, fractVal);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000766 }
767
sugoi@google.comd537af52013-06-10 13:59:25 +0000768 noiseCode.appendf("\n\t%s.x -= 1.0;", fractVal);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000769 // Compute v, at offset (-1,0)
770 {
771 SkString latticeCoords("");
senorblancoce6a3542014-06-12 11:24:19 -0700772 latticeCoords.appendf("vec2(%s.y, %s)", bcoords, chanCoord);
commit-bot@chromium.org344cf452013-06-17 14:19:01 +0000773 noiseCode.append("\n\tlattice = ");
wangyix7c157a92015-07-22 15:08:53 -0700774 fsBuilder->appendTextureLookup(&noiseCode, args.fSamplers[1], latticeCoords.c_str(),
sugoi@google.comd537af52013-06-10 13:59:25 +0000775 kVec2f_GrSLType);
776 noiseCode.appendf(".bgra;\n\t%s.y = ", uv);
777 noiseCode.appendf(dotLattice, lattice, lattice, inc8bit, fractVal);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000778 }
779
780 // Compute 'a' as a linear interpolation of 'u' and 'v'
sugoi@google.comd537af52013-06-10 13:59:25 +0000781 noiseCode.appendf("\n\tvec2 %s;", ab);
782 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 +0000783
sugoi@google.comd537af52013-06-10 13:59:25 +0000784 noiseCode.appendf("\n\t%s.y -= 1.0;", fractVal);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000785 // Compute v, at offset (-1,-1)
786 {
787 SkString latticeCoords("");
senorblancoce6a3542014-06-12 11:24:19 -0700788 latticeCoords.appendf("vec2(%s.w, %s)", bcoords, chanCoord);
commit-bot@chromium.org344cf452013-06-17 14:19:01 +0000789 noiseCode.append("\n\tlattice = ");
wangyix7c157a92015-07-22 15:08:53 -0700790 fsBuilder->appendTextureLookup(&noiseCode, args.fSamplers[1], latticeCoords.c_str(),
sugoi@google.comd537af52013-06-10 13:59:25 +0000791 kVec2f_GrSLType);
792 noiseCode.appendf(".bgra;\n\t%s.y = ", uv);
793 noiseCode.appendf(dotLattice, lattice, lattice, inc8bit, fractVal);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000794 }
795
sugoi@google.comd537af52013-06-10 13:59:25 +0000796 noiseCode.appendf("\n\t%s.x += 1.0;", fractVal);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000797 // Compute u, at offset (0,-1)
798 {
799 SkString latticeCoords("");
senorblancoce6a3542014-06-12 11:24:19 -0700800 latticeCoords.appendf("vec2(%s.z, %s)", bcoords, chanCoord);
commit-bot@chromium.org344cf452013-06-17 14:19:01 +0000801 noiseCode.append("\n\tlattice = ");
wangyix7c157a92015-07-22 15:08:53 -0700802 fsBuilder->appendTextureLookup(&noiseCode, args.fSamplers[1], latticeCoords.c_str(),
sugoi@google.comd537af52013-06-10 13:59:25 +0000803 kVec2f_GrSLType);
804 noiseCode.appendf(".bgra;\n\t%s.x = ", uv);
805 noiseCode.appendf(dotLattice, lattice, lattice, inc8bit, fractVal);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000806 }
807
808 // Compute 'b' as a linear interpolation of 'u' and 'v'
sugoi@google.comd537af52013-06-10 13:59:25 +0000809 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 +0000810 // Compute the noise as a linear interpolation of 'a' and 'b'
sugoi@google.comd537af52013-06-10 13:59:25 +0000811 noiseCode.appendf("\n\treturn mix(%s.x, %s.y, %s.y);\n", ab, ab, noiseSmooth);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000812
sugoi@google.comd537af52013-06-10 13:59:25 +0000813 SkString noiseFuncName;
814 if (fStitchTiles) {
joshualitt30ba4362014-08-21 20:18:45 -0700815 fsBuilder->emitFunction(kFloat_GrSLType,
commit-bot@chromium.org74a3a212013-08-30 19:43:59 +0000816 "perlinnoise", SK_ARRAY_COUNT(gPerlinNoiseStitchArgs),
817 gPerlinNoiseStitchArgs, noiseCode.c_str(), &noiseFuncName);
sugoi@google.comd537af52013-06-10 13:59:25 +0000818 } else {
joshualitt30ba4362014-08-21 20:18:45 -0700819 fsBuilder->emitFunction(kFloat_GrSLType,
commit-bot@chromium.org74a3a212013-08-30 19:43:59 +0000820 "perlinnoise", SK_ARRAY_COUNT(gPerlinNoiseArgs),
821 gPerlinNoiseArgs, noiseCode.c_str(), &noiseFuncName);
sugoi@google.comd537af52013-06-10 13:59:25 +0000822 }
sugoi@google.come3b4c502013-04-05 13:47:09 +0000823
sugoi@google.comd537af52013-06-10 13:59:25 +0000824 // There are rounding errors if the floor operation is not performed here
joshualitt30ba4362014-08-21 20:18:45 -0700825 fsBuilder->codeAppendf("\n\t\tvec2 %s = floor(%s.xy) * %s;",
senorblancoca6a7c22014-06-27 13:35:52 -0700826 noiseVec, vCoords.c_str(), baseFrequencyUni);
sugoi@google.comd537af52013-06-10 13:59:25 +0000827
828 // Clear the color accumulator
wangyix7c157a92015-07-22 15:08:53 -0700829 fsBuilder->codeAppendf("\n\t\t%s = vec4(0.0);", args.fOutputColor);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000830
831 if (fStitchTiles) {
sugoi@google.comd537af52013-06-10 13:59:25 +0000832 // Set up TurbulenceInitial stitch values.
joshualitt30ba4362014-08-21 20:18:45 -0700833 fsBuilder->codeAppendf("\n\t\tvec2 %s = %s;", stitchData, stitchDataUni);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000834 }
sugoi@google.come3b4c502013-04-05 13:47:09 +0000835
joshualitt30ba4362014-08-21 20:18:45 -0700836 fsBuilder->codeAppendf("\n\t\tfloat %s = 1.0;", ratio);
sugoi@google.comd537af52013-06-10 13:59:25 +0000837
838 // Loop over all octaves
joshualitt30ba4362014-08-21 20:18:45 -0700839 fsBuilder->codeAppendf("\n\t\tfor (int octave = 0; octave < %d; ++octave) {", fNumOctaves);
sugoi@google.comd537af52013-06-10 13:59:25 +0000840
wangyix7c157a92015-07-22 15:08:53 -0700841 fsBuilder->codeAppendf("\n\t\t\t%s += ", args.fOutputColor);
sugoi@google.comd537af52013-06-10 13:59:25 +0000842 if (fType != SkPerlinNoiseShader::kFractalNoise_Type) {
joshualitt30ba4362014-08-21 20:18:45 -0700843 fsBuilder->codeAppend("abs(");
sugoi@google.comd537af52013-06-10 13:59:25 +0000844 }
845 if (fStitchTiles) {
joshualitt30ba4362014-08-21 20:18:45 -0700846 fsBuilder->codeAppendf(
sugoi@google.comd537af52013-06-10 13:59:25 +0000847 "vec4(\n\t\t\t\t%s(%s, %s, %s),\n\t\t\t\t%s(%s, %s, %s),"
848 "\n\t\t\t\t%s(%s, %s, %s),\n\t\t\t\t%s(%s, %s, %s))",
849 noiseFuncName.c_str(), chanCoordR, noiseVec, stitchData,
850 noiseFuncName.c_str(), chanCoordG, noiseVec, stitchData,
851 noiseFuncName.c_str(), chanCoordB, noiseVec, stitchData,
852 noiseFuncName.c_str(), chanCoordA, noiseVec, stitchData);
853 } else {
joshualitt30ba4362014-08-21 20:18:45 -0700854 fsBuilder->codeAppendf(
sugoi@google.comd537af52013-06-10 13:59:25 +0000855 "vec4(\n\t\t\t\t%s(%s, %s),\n\t\t\t\t%s(%s, %s),"
856 "\n\t\t\t\t%s(%s, %s),\n\t\t\t\t%s(%s, %s))",
857 noiseFuncName.c_str(), chanCoordR, noiseVec,
858 noiseFuncName.c_str(), chanCoordG, noiseVec,
859 noiseFuncName.c_str(), chanCoordB, noiseVec,
860 noiseFuncName.c_str(), chanCoordA, noiseVec);
861 }
862 if (fType != SkPerlinNoiseShader::kFractalNoise_Type) {
joshualitt30ba4362014-08-21 20:18:45 -0700863 fsBuilder->codeAppendf(")"); // end of "abs("
sugoi@google.comd537af52013-06-10 13:59:25 +0000864 }
joshualitt30ba4362014-08-21 20:18:45 -0700865 fsBuilder->codeAppendf(" * %s;", ratio);
sugoi@google.comd537af52013-06-10 13:59:25 +0000866
joshualitt30ba4362014-08-21 20:18:45 -0700867 fsBuilder->codeAppendf("\n\t\t\t%s *= vec2(2.0);", noiseVec);
868 fsBuilder->codeAppendf("\n\t\t\t%s *= 0.5;", ratio);
sugoi@google.comd537af52013-06-10 13:59:25 +0000869
870 if (fStitchTiles) {
joshualitt30ba4362014-08-21 20:18:45 -0700871 fsBuilder->codeAppendf("\n\t\t\t%s *= vec2(2.0);", stitchData);
sugoi@google.comd537af52013-06-10 13:59:25 +0000872 }
joshualitt30ba4362014-08-21 20:18:45 -0700873 fsBuilder->codeAppend("\n\t\t}"); // end of the for loop on octaves
sugoi@google.come3b4c502013-04-05 13:47:09 +0000874
875 if (fType == SkPerlinNoiseShader::kFractalNoise_Type) {
876 // The value of turbulenceFunctionResult comes from ((turbulenceFunctionResult) + 1) / 2
877 // by fractalNoise and (turbulenceFunctionResult) by turbulence.
wangyix7c157a92015-07-22 15:08:53 -0700878 fsBuilder->codeAppendf("\n\t\t%s = %s * vec4(0.5) + vec4(0.5);",
879 args.fOutputColor,args.fOutputColor);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000880 }
881
wangyix7c157a92015-07-22 15:08:53 -0700882 fsBuilder->codeAppendf("\n\t\t%s.a *= %s;", args.fOutputColor, alphaUni);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000883
884 // Clamp values
wangyix7c157a92015-07-22 15:08:53 -0700885 fsBuilder->codeAppendf("\n\t\t%s = clamp(%s, 0.0, 1.0);", args.fOutputColor, args.fOutputColor);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000886
887 // Pre-multiply the result
joshualitt30ba4362014-08-21 20:18:45 -0700888 fsBuilder->codeAppendf("\n\t\t%s = vec4(%s.rgb * %s.aaa, %s.a);\n",
wangyix7c157a92015-07-22 15:08:53 -0700889 args.fOutputColor, args.fOutputColor,
890 args.fOutputColor, args.fOutputColor);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000891}
892
jvanverthcfc18862015-04-28 08:48:20 -0700893void GrGLPerlinNoise::GenKey(const GrProcessor& processor, const GrGLSLCaps&,
joshualittb0a8a372014-09-23 09:50:21 -0700894 GrProcessorKeyBuilder* b) {
895 const GrPerlinNoiseEffect& turbulence = processor.cast<GrPerlinNoiseEffect>();
sugoi@google.come3b4c502013-04-05 13:47:09 +0000896
bsalomon63e99f72014-07-21 08:03:14 -0700897 uint32_t key = turbulence.numOctaves();
sugoi@google.come3b4c502013-04-05 13:47:09 +0000898
899 key = key << 3; // Make room for next 3 bits
900
901 switch (turbulence.type()) {
902 case SkPerlinNoiseShader::kFractalNoise_Type:
903 key |= 0x1;
904 break;
905 case SkPerlinNoiseShader::kTurbulence_Type:
906 key |= 0x2;
907 break;
908 default:
909 // leave key at 0
910 break;
911 }
912
913 if (turbulence.stitchTiles()) {
914 key |= 0x4; // Flip the 3rd bit if tile stitching is on
915 }
916
bsalomon63e99f72014-07-21 08:03:14 -0700917 b->add32(key);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000918}
919
wangyixb1daa862015-08-18 11:29:31 -0700920void GrGLPerlinNoise::onSetData(const GrGLProgramDataManager& pdman, const GrProcessor& processor) {
921 INHERITED::onSetData(pdman, processor);
senorblancof3b50272014-06-16 10:49:58 -0700922
joshualittb0a8a372014-09-23 09:50:21 -0700923 const GrPerlinNoiseEffect& turbulence = processor.cast<GrPerlinNoiseEffect>();
sugoi@google.come3b4c502013-04-05 13:47:09 +0000924
925 const SkVector& baseFrequency = turbulence.baseFrequency();
kkinnunen7510b222014-07-30 00:04:16 -0700926 pdman.set2f(fBaseFrequencyUni, baseFrequency.fX, baseFrequency.fY);
reed80ea19c2015-05-12 10:37:34 -0700927 pdman.set1f(fAlphaUni, SkIntToScalar(turbulence.alpha()) / 255);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000928
sugoi@google.com4775cba2013-04-17 13:46:56 +0000929 if (turbulence.stitchTiles()) {
930 const SkPerlinNoiseShader::StitchData& stitchData = turbulence.stitchData();
kkinnunen7510b222014-07-30 00:04:16 -0700931 pdman.set2f(fStitchDataUni, SkIntToScalar(stitchData.fWidth),
commit-bot@chromium.org98393202013-07-04 18:13:05 +0000932 SkIntToScalar(stitchData.fHeight));
sugoi@google.com4775cba2013-04-17 13:46:56 +0000933 }
934}
935
sugoi@google.come3b4c502013-04-05 13:47:09 +0000936/////////////////////////////////////////////////////////////////////
937
joshualittb0a8a372014-09-23 09:50:21 -0700938bool SkPerlinNoiseShader::asFragmentProcessor(GrContext* context, const SkPaint& paint,
joshualitt5531d512014-12-17 15:50:11 -0800939 const SkMatrix& viewM,
joshualittb0a8a372014-09-23 09:50:21 -0700940 const SkMatrix* externalLocalMatrix,
joshualittb2456052015-07-08 09:36:59 -0700941 GrColor* paintColor,
joshualitt9cc17752015-07-09 06:28:14 -0700942 GrProcessorDataManager* procDataManager,
joshualitt8ca93e72015-07-08 06:51:43 -0700943 GrFragmentProcessor** fp) const {
bsalomon49f085d2014-09-05 13:34:00 -0700944 SkASSERT(context);
mtklein3f3b3d02014-12-01 11:47:08 -0800945
bsalomon83d081a2014-07-08 09:56:10 -0700946 *paintColor = SkColor2GrColorJustAlpha(paint.getColor());
senorblancoca6a7c22014-06-27 13:35:52 -0700947
commit-bot@chromium.org96fb7482014-05-09 20:28:11 +0000948 SkMatrix localMatrix = this->getLocalMatrix();
949 if (externalLocalMatrix) {
950 localMatrix.preConcat(*externalLocalMatrix);
951 }
952
joshualitt5531d512014-12-17 15:50:11 -0800953 SkMatrix matrix = viewM;
senorblancoca6a7c22014-06-27 13:35:52 -0700954 matrix.preConcat(localMatrix);
955
commit-bot@chromium.orgc2a0ea62013-11-06 10:08:38 +0000956 if (0 == fNumOctaves) {
commit-bot@chromium.orgc2a0ea62013-11-06 10:08:38 +0000957 if (kFractalNoise_Type == fType) {
bsalomonea8b55d2015-03-04 11:03:52 -0800958 uint32_t alpha = paint.getAlpha() >> 1;
959 uint32_t rgb = alpha >> 1;
960 *paintColor = GrColorPackRGBA(rgb, rgb, rgb, alpha);
961 } else {
962 *paintColor = 0;
reedcff10b22015-03-03 06:41:45 -0800963 }
dandov9de5b512014-06-10 14:38:28 -0700964 return true;
commit-bot@chromium.orgc2a0ea62013-11-06 10:08:38 +0000965 }
966
sugoi@google.come3b4c502013-04-05 13:47:09 +0000967 // Either we don't stitch tiles, either we have a valid tile size
968 SkASSERT(!fStitchTiles || !fTileSize.isEmpty());
969
joshualittb0a8a372014-09-23 09:50:21 -0700970 SkPerlinNoiseShader::PaintingData* paintingData =
971 SkNEW_ARGS(PaintingData, (fTileSize, fSeed, fBaseFrequencyX, fBaseFrequencyY, matrix));
bsalomonbcf0a522014-10-08 08:40:09 -0700972 SkAutoTUnref<GrTexture> permutationsTexture(
973 GrRefCachedBitmapTexture(context, paintingData->getPermutationsBitmap(), NULL));
974 SkAutoTUnref<GrTexture> noiseTexture(
975 GrRefCachedBitmapTexture(context, paintingData->getNoiseBitmap(), NULL));
sugoi@google.come3b4c502013-04-05 13:47:09 +0000976
joshualitt5531d512014-12-17 15:50:11 -0800977 SkMatrix m = viewM;
senorblancoca6a7c22014-06-27 13:35:52 -0700978 m.setTranslateX(-localMatrix.getTranslateX() + SK_Scalar1);
979 m.setTranslateY(-localMatrix.getTranslateY() + SK_Scalar1);
bsalomon49f085d2014-09-05 13:34:00 -0700980 if ((permutationsTexture) && (noiseTexture)) {
joshualitt9cc17752015-07-09 06:28:14 -0700981 *fp = GrPerlinNoiseEffect::Create(procDataManager,
joshualittb2456052015-07-08 09:36:59 -0700982 fType,
joshualittb0a8a372014-09-23 09:50:21 -0700983 fNumOctaves,
984 fStitchTiles,
985 paintingData,
986 permutationsTexture, noiseTexture,
987 m, paint.getAlpha());
senorblancoca6a7c22014-06-27 13:35:52 -0700988 } else {
989 SkDELETE(paintingData);
joshualittb0a8a372014-09-23 09:50:21 -0700990 *fp = NULL;
senorblancoca6a7c22014-06-27 13:35:52 -0700991 }
dandov9de5b512014-06-10 14:38:28 -0700992 return true;
sugoi@google.come3b4c502013-04-05 13:47:09 +0000993}
994
995#else
996
joshualitt5531d512014-12-17 15:50:11 -0800997bool SkPerlinNoiseShader::asFragmentProcessor(GrContext*, const SkPaint&, const SkMatrix&,
joshualitt9cc17752015-07-09 06:28:14 -0700998 const SkMatrix*, GrColor*, GrProcessorDataManager*,
joshualittb0a8a372014-09-23 09:50:21 -0700999 GrFragmentProcessor**) const {
sugoi@google.come3b4c502013-04-05 13:47:09 +00001000 SkDEBUGFAIL("Should not call in GPU-less build");
dandov9de5b512014-06-10 14:38:28 -07001001 return false;
sugoi@google.come3b4c502013-04-05 13:47:09 +00001002}
1003
1004#endif
1005
commit-bot@chromium.org0f10f7b2014-03-13 18:02:17 +00001006#ifndef SK_IGNORE_TO_STRING
sugoi@google.come3b4c502013-04-05 13:47:09 +00001007void SkPerlinNoiseShader::toString(SkString* str) const {
1008 str->append("SkPerlinNoiseShader: (");
1009
1010 str->append("type: ");
1011 switch (fType) {
1012 case kFractalNoise_Type:
1013 str->append("\"fractal noise\"");
1014 break;
1015 case kTurbulence_Type:
1016 str->append("\"turbulence\"");
1017 break;
1018 default:
1019 str->append("\"unknown\"");
1020 break;
1021 }
1022 str->append(" base frequency: (");
1023 str->appendScalar(fBaseFrequencyX);
1024 str->append(", ");
1025 str->appendScalar(fBaseFrequencyY);
1026 str->append(") number of octaves: ");
1027 str->appendS32(fNumOctaves);
1028 str->append(" seed: ");
1029 str->appendScalar(fSeed);
1030 str->append(" stitch tiles: ");
1031 str->append(fStitchTiles ? "true " : "false ");
1032
1033 this->INHERITED::toString(str);
1034
1035 str->append(")");
1036}
1037#endif