blob: fa46cbd44ac5c5e073b197a8b204170001a0f9c2 [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"
joshualittb0a8a372014-09-23 09:50:21 -070022#include "gl/GrGLProcessor.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.
skia.committer@gmail.comcff02432013-04-06 07:01:10 +0000215 if (SkScalarDiv(fBaseFrequency.fX, lowFrequencx) <
sugoi@google.come3b4c502013-04-05 13:47:09 +0000216 SkScalarDiv(highFrequencx, fBaseFrequency.fX)) {
217 fBaseFrequency.fX = lowFrequencx;
218 } else {
219 fBaseFrequency.fX = highFrequencx;
220 }
221 }
222 if (fBaseFrequency.fY) {
reed@google.com8015cdd2013-12-18 15:49:32 +0000223 SkScalar lowFrequency =
224 SkScalarFloorToScalar(tileHeight * fBaseFrequency.fY) / tileHeight;
225 SkScalar highFrequency =
226 SkScalarCeilToScalar(tileHeight * fBaseFrequency.fY) / tileHeight;
skia.committer@gmail.comcff02432013-04-06 07:01:10 +0000227 if (SkScalarDiv(fBaseFrequency.fY, lowFrequency) <
sugoi@google.come3b4c502013-04-05 13:47:09 +0000228 SkScalarDiv(highFrequency, fBaseFrequency.fY)) {
229 fBaseFrequency.fY = lowFrequency;
230 } else {
231 fBaseFrequency.fY = highFrequency;
232 }
233 }
234 // Set up TurbulenceInitial stitch values.
235 fStitchDataInit.fWidth =
reed@google.com8015cdd2013-12-18 15:49:32 +0000236 SkScalarRoundToInt(tileWidth * fBaseFrequency.fX);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000237 fStitchDataInit.fWrapX = kPerlinNoise + fStitchDataInit.fWidth;
238 fStitchDataInit.fHeight =
reed@google.com8015cdd2013-12-18 15:49:32 +0000239 SkScalarRoundToInt(tileHeight * fBaseFrequency.fY);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000240 fStitchDataInit.fWrapY = kPerlinNoise + fStitchDataInit.fHeight;
241 }
242
commit-bot@chromium.orgfd5c9a62014-03-06 15:13:53 +0000243public:
sugoi@google.come3b4c502013-04-05 13:47:09 +0000244
senorblancof3b50272014-06-16 10:49:58 -0700245#if SK_SUPPORT_GPU
commit-bot@chromium.orgfd5c9a62014-03-06 15:13:53 +0000246 const SkBitmap& getPermutationsBitmap() const { return fPermutationsBitmap; }
247
248 const SkBitmap& getNoiseBitmap() const { return fNoiseBitmap; }
249#endif
sugoi@google.come3b4c502013-04-05 13:47:09 +0000250};
251
252SkShader* SkPerlinNoiseShader::CreateFractalNoise(SkScalar baseFrequencyX, SkScalar baseFrequencyY,
253 int numOctaves, SkScalar seed,
254 const SkISize* tileSize) {
255 return SkNEW_ARGS(SkPerlinNoiseShader, (kFractalNoise_Type, baseFrequencyX, baseFrequencyY,
256 numOctaves, seed, tileSize));
257}
258
commit-bot@chromium.org9fbbcca2014-04-01 16:09:37 +0000259SkShader* SkPerlinNoiseShader::CreateTurbulence(SkScalar baseFrequencyX, SkScalar baseFrequencyY,
sugoi@google.come3b4c502013-04-05 13:47:09 +0000260 int numOctaves, SkScalar seed,
261 const SkISize* tileSize) {
262 return SkNEW_ARGS(SkPerlinNoiseShader, (kTurbulence_Type, baseFrequencyX, baseFrequencyY,
263 numOctaves, seed, tileSize));
264}
265
266SkPerlinNoiseShader::SkPerlinNoiseShader(SkPerlinNoiseShader::Type type,
267 SkScalar baseFrequencyX,
268 SkScalar baseFrequencyY,
269 int numOctaves,
270 SkScalar seed,
271 const SkISize* tileSize)
272 : fType(type)
273 , fBaseFrequencyX(baseFrequencyX)
274 , fBaseFrequencyY(baseFrequencyY)
commit-bot@chromium.orgce33d602013-11-25 21:46:31 +0000275 , fNumOctaves(numOctaves > 255 ? 255 : numOctaves/*[0,255] octaves allowed*/)
sugoi@google.come3b4c502013-04-05 13:47:09 +0000276 , fSeed(seed)
commit-bot@chromium.orgfd5c9a62014-03-06 15:13:53 +0000277 , fTileSize(NULL == tileSize ? SkISize::Make(0, 0) : *tileSize)
278 , fStitchTiles(!fTileSize.isEmpty())
sugoi@google.come3b4c502013-04-05 13:47:09 +0000279{
280 SkASSERT(numOctaves >= 0 && numOctaves < 256);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000281}
282
sugoi@google.come3b4c502013-04-05 13:47:09 +0000283SkPerlinNoiseShader::~SkPerlinNoiseShader() {
sugoi@google.come3b4c502013-04-05 13:47:09 +0000284}
285
reed9fa60da2014-08-21 07:59:51 -0700286SkFlattenable* SkPerlinNoiseShader::CreateProc(SkReadBuffer& buffer) {
287 Type type = (Type)buffer.readInt();
288 SkScalar freqX = buffer.readScalar();
289 SkScalar freqY = buffer.readScalar();
290 int octaves = buffer.readInt();
291 SkScalar seed = buffer.readScalar();
292 SkISize tileSize;
293 tileSize.fWidth = buffer.readInt();
294 tileSize.fHeight = buffer.readInt();
295
296 switch (type) {
297 case kFractalNoise_Type:
298 return SkPerlinNoiseShader::CreateFractalNoise(freqX, freqY, octaves, seed, &tileSize);
299 case kTurbulence_Type:
300 return SkPerlinNoiseShader::CreateTubulence(freqX, freqY, octaves, seed, &tileSize);
301 default:
302 return NULL;
303 }
304}
305
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +0000306void SkPerlinNoiseShader::flatten(SkWriteBuffer& buffer) const {
sugoi@google.come3b4c502013-04-05 13:47:09 +0000307 buffer.writeInt((int) fType);
308 buffer.writeScalar(fBaseFrequencyX);
309 buffer.writeScalar(fBaseFrequencyY);
310 buffer.writeInt(fNumOctaves);
311 buffer.writeScalar(fSeed);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000312 buffer.writeInt(fTileSize.fWidth);
313 buffer.writeInt(fTileSize.fHeight);
314}
315
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000316SkScalar SkPerlinNoiseShader::PerlinNoiseShaderContext::noise2D(
senorblancoca6a7c22014-06-27 13:35:52 -0700317 int channel, const StitchData& stitchData, const SkPoint& noiseVector) const {
sugoi@google.come3b4c502013-04-05 13:47:09 +0000318 struct Noise {
319 int noisePositionIntegerValue;
senorblancoce6a3542014-06-12 11:24:19 -0700320 int nextNoisePositionIntegerValue;
sugoi@google.come3b4c502013-04-05 13:47:09 +0000321 SkScalar noisePositionFractionValue;
322 Noise(SkScalar component)
323 {
324 SkScalar position = component + kPerlinNoise;
325 noisePositionIntegerValue = SkScalarFloorToInt(position);
326 noisePositionFractionValue = position - SkIntToScalar(noisePositionIntegerValue);
senorblancoce6a3542014-06-12 11:24:19 -0700327 nextNoisePositionIntegerValue = noisePositionIntegerValue + 1;
sugoi@google.come3b4c502013-04-05 13:47:09 +0000328 }
329 };
330 Noise noiseX(noiseVector.x());
331 Noise noiseY(noiseVector.y());
332 SkScalar u, v;
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000333 const SkPerlinNoiseShader& perlinNoiseShader = static_cast<const SkPerlinNoiseShader&>(fShader);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000334 // If stitching, adjust lattice points accordingly.
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000335 if (perlinNoiseShader.fStitchTiles) {
sugoi@google.come3b4c502013-04-05 13:47:09 +0000336 noiseX.noisePositionIntegerValue =
337 checkNoise(noiseX.noisePositionIntegerValue, stitchData.fWrapX, stitchData.fWidth);
338 noiseY.noisePositionIntegerValue =
339 checkNoise(noiseY.noisePositionIntegerValue, stitchData.fWrapY, stitchData.fHeight);
senorblancoce6a3542014-06-12 11:24:19 -0700340 noiseX.nextNoisePositionIntegerValue =
341 checkNoise(noiseX.nextNoisePositionIntegerValue, stitchData.fWrapX, stitchData.fWidth);
342 noiseY.nextNoisePositionIntegerValue =
343 checkNoise(noiseY.nextNoisePositionIntegerValue, stitchData.fWrapY, stitchData.fHeight);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000344 }
345 noiseX.noisePositionIntegerValue &= kBlockMask;
346 noiseY.noisePositionIntegerValue &= kBlockMask;
senorblancoce6a3542014-06-12 11:24:19 -0700347 noiseX.nextNoisePositionIntegerValue &= kBlockMask;
348 noiseY.nextNoisePositionIntegerValue &= kBlockMask;
349 int i =
senorblancoca6a7c22014-06-27 13:35:52 -0700350 fPaintingData->fLatticeSelector[noiseX.noisePositionIntegerValue];
senorblancoce6a3542014-06-12 11:24:19 -0700351 int j =
senorblancoca6a7c22014-06-27 13:35:52 -0700352 fPaintingData->fLatticeSelector[noiseX.nextNoisePositionIntegerValue];
senorblancoce6a3542014-06-12 11:24:19 -0700353 int b00 = (i + noiseY.noisePositionIntegerValue) & kBlockMask;
354 int b10 = (j + noiseY.noisePositionIntegerValue) & kBlockMask;
355 int b01 = (i + noiseY.nextNoisePositionIntegerValue) & kBlockMask;
356 int b11 = (j + noiseY.nextNoisePositionIntegerValue) & kBlockMask;
sugoi@google.come3b4c502013-04-05 13:47:09 +0000357 SkScalar sx = smoothCurve(noiseX.noisePositionFractionValue);
358 SkScalar sy = smoothCurve(noiseY.noisePositionFractionValue);
359 // This is taken 1:1 from SVG spec: http://www.w3.org/TR/SVG11/filters.html#feTurbulenceElement
360 SkPoint fractionValue = SkPoint::Make(noiseX.noisePositionFractionValue,
361 noiseY.noisePositionFractionValue); // Offset (0,0)
senorblancoca6a7c22014-06-27 13:35:52 -0700362 u = fPaintingData->fGradient[channel][b00].dot(fractionValue);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000363 fractionValue.fX -= SK_Scalar1; // Offset (-1,0)
senorblancoca6a7c22014-06-27 13:35:52 -0700364 v = fPaintingData->fGradient[channel][b10].dot(fractionValue);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000365 SkScalar a = SkScalarInterp(u, v, sx);
366 fractionValue.fY -= SK_Scalar1; // Offset (-1,-1)
senorblancoca6a7c22014-06-27 13:35:52 -0700367 v = fPaintingData->fGradient[channel][b11].dot(fractionValue);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000368 fractionValue.fX = noiseX.noisePositionFractionValue; // Offset (0,-1)
senorblancoca6a7c22014-06-27 13:35:52 -0700369 u = fPaintingData->fGradient[channel][b01].dot(fractionValue);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000370 SkScalar b = SkScalarInterp(u, v, sx);
371 return SkScalarInterp(a, b, sy);
372}
373
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000374SkScalar SkPerlinNoiseShader::PerlinNoiseShaderContext::calculateTurbulenceValueForPoint(
senorblancoca6a7c22014-06-27 13:35:52 -0700375 int channel, StitchData& stitchData, const SkPoint& point) const {
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000376 const SkPerlinNoiseShader& perlinNoiseShader = static_cast<const SkPerlinNoiseShader&>(fShader);
377 if (perlinNoiseShader.fStitchTiles) {
sugoi@google.come3b4c502013-04-05 13:47:09 +0000378 // Set up TurbulenceInitial stitch values.
senorblancoca6a7c22014-06-27 13:35:52 -0700379 stitchData = fPaintingData->fStitchDataInit;
sugoi@google.come3b4c502013-04-05 13:47:09 +0000380 }
381 SkScalar turbulenceFunctionResult = 0;
senorblancoca6a7c22014-06-27 13:35:52 -0700382 SkPoint noiseVector(SkPoint::Make(SkScalarMul(point.x(), fPaintingData->fBaseFrequency.fX),
383 SkScalarMul(point.y(), fPaintingData->fBaseFrequency.fY)));
sugoi@google.come3b4c502013-04-05 13:47:09 +0000384 SkScalar ratio = SK_Scalar1;
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000385 for (int octave = 0; octave < perlinNoiseShader.fNumOctaves; ++octave) {
senorblancoca6a7c22014-06-27 13:35:52 -0700386 SkScalar noise = noise2D(channel, stitchData, noiseVector);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000387 turbulenceFunctionResult += SkScalarDiv(
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000388 (perlinNoiseShader.fType == kFractalNoise_Type) ? noise : SkScalarAbs(noise), ratio);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000389 noiseVector.fX *= 2;
390 noiseVector.fY *= 2;
391 ratio *= 2;
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000392 if (perlinNoiseShader.fStitchTiles) {
sugoi@google.come3b4c502013-04-05 13:47:09 +0000393 // Update stitch values
394 stitchData.fWidth *= 2;
395 stitchData.fWrapX = stitchData.fWidth + kPerlinNoise;
396 stitchData.fHeight *= 2;
397 stitchData.fWrapY = stitchData.fHeight + kPerlinNoise;
398 }
399 }
400
401 // The value of turbulenceFunctionResult comes from ((turbulenceFunctionResult) + 1) / 2
402 // by fractalNoise and (turbulenceFunctionResult) by turbulence.
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000403 if (perlinNoiseShader.fType == kFractalNoise_Type) {
sugoi@google.come3b4c502013-04-05 13:47:09 +0000404 turbulenceFunctionResult =
405 SkScalarMul(turbulenceFunctionResult, SK_ScalarHalf) + SK_ScalarHalf;
406 }
407
408 if (channel == 3) { // Scale alpha by paint value
409 turbulenceFunctionResult = SkScalarMul(turbulenceFunctionResult,
410 SkScalarDiv(SkIntToScalar(getPaintAlpha()), SkIntToScalar(255)));
411 }
412
413 // Clamp result
414 return SkScalarPin(turbulenceFunctionResult, 0, SK_Scalar1);
415}
416
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000417SkPMColor SkPerlinNoiseShader::PerlinNoiseShaderContext::shade(
418 const SkPoint& point, StitchData& stitchData) const {
sugoi@google.come3b4c502013-04-05 13:47:09 +0000419 SkPoint newPoint;
senorblanco@chromium.orga8d95f82014-04-04 14:46:10 +0000420 fMatrix.mapPoints(&newPoint, &point, 1);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000421 newPoint.fX = SkScalarRoundToScalar(newPoint.fX);
422 newPoint.fY = SkScalarRoundToScalar(newPoint.fY);
423
424 U8CPU rgba[4];
425 for (int channel = 3; channel >= 0; --channel) {
426 rgba[channel] = SkScalarFloorToInt(255 *
senorblancoca6a7c22014-06-27 13:35:52 -0700427 calculateTurbulenceValueForPoint(channel, stitchData, newPoint));
sugoi@google.come3b4c502013-04-05 13:47:09 +0000428 }
429 return SkPreMultiplyARGB(rgba[3], rgba[0], rgba[1], rgba[2]);
430}
431
commit-bot@chromium.orgce56d962014-05-05 18:39:18 +0000432SkShader::Context* SkPerlinNoiseShader::onCreateContext(const ContextRec& rec,
433 void* storage) const {
commit-bot@chromium.orge901b6d2014-05-01 19:31:31 +0000434 return SkNEW_PLACEMENT_ARGS(storage, PerlinNoiseShaderContext, (*this, rec));
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000435}
436
437size_t SkPerlinNoiseShader::contextSize() const {
438 return sizeof(PerlinNoiseShaderContext);
439}
440
441SkPerlinNoiseShader::PerlinNoiseShaderContext::PerlinNoiseShaderContext(
commit-bot@chromium.orge901b6d2014-05-01 19:31:31 +0000442 const SkPerlinNoiseShader& shader, const ContextRec& rec)
443 : INHERITED(shader, rec)
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000444{
commit-bot@chromium.orge901b6d2014-05-01 19:31:31 +0000445 SkMatrix newMatrix = *rec.fMatrix;
reed@google.comb67b8e62014-05-12 18:12:24 +0000446 newMatrix.preConcat(shader.getLocalMatrix());
447 if (rec.fLocalMatrix) {
448 newMatrix.preConcat(*rec.fLocalMatrix);
449 }
senorblanco@chromium.orga8d95f82014-04-04 14:46:10 +0000450 // This (1,1) translation is due to WebKit's 1 based coordinates for the noise
451 // (as opposed to 0 based, usually). The same adjustment is in the setData() function.
senorblancoca6a7c22014-06-27 13:35:52 -0700452 fMatrix.setTranslate(-newMatrix.getTranslateX() + SK_Scalar1, -newMatrix.getTranslateY() + SK_Scalar1);
453 fPaintingData = SkNEW_ARGS(PaintingData, (shader.fTileSize, shader.fSeed, shader.fBaseFrequencyX, shader.fBaseFrequencyY, newMatrix));
454}
455
456SkPerlinNoiseShader::PerlinNoiseShaderContext::~PerlinNoiseShaderContext() {
457 SkDELETE(fPaintingData);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000458}
459
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000460void SkPerlinNoiseShader::PerlinNoiseShaderContext::shadeSpan(
461 int x, int y, SkPMColor result[], int count) {
sugoi@google.come3b4c502013-04-05 13:47:09 +0000462 SkPoint point = SkPoint::Make(SkIntToScalar(x), SkIntToScalar(y));
463 StitchData stitchData;
464 for (int i = 0; i < count; ++i) {
465 result[i] = shade(point, stitchData);
466 point.fX += SK_Scalar1;
467 }
468}
469
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000470void SkPerlinNoiseShader::PerlinNoiseShaderContext::shadeSpan16(
471 int x, int y, uint16_t result[], int count) {
sugoi@google.come3b4c502013-04-05 13:47:09 +0000472 SkPoint point = SkPoint::Make(SkIntToScalar(x), SkIntToScalar(y));
473 StitchData stitchData;
474 DITHER_565_SCAN(y);
475 for (int i = 0; i < count; ++i) {
476 unsigned dither = DITHER_VALUE(x);
477 result[i] = SkDitherRGB32To565(shade(point, stitchData), dither);
478 DITHER_INC_X(x);
479 point.fX += SK_Scalar1;
480 }
481}
482
483/////////////////////////////////////////////////////////////////////
484
commit-bot@chromium.org344cf452013-06-17 14:19:01 +0000485#if SK_SUPPORT_GPU
sugoi@google.come3b4c502013-04-05 13:47:09 +0000486
joshualittb0a8a372014-09-23 09:50:21 -0700487class GrGLPerlinNoise : public GrGLFragmentProcessor {
sugoi@google.com4775cba2013-04-17 13:46:56 +0000488public:
joshualitteb2a6762014-12-04 11:35:33 -0800489 GrGLPerlinNoise(const GrProcessor&);
sugoi@google.com4775cba2013-04-17 13:46:56 +0000490 virtual ~GrGLPerlinNoise() {}
sugoi@google.come3b4c502013-04-05 13:47:09 +0000491
joshualitt15988992014-10-09 15:04:05 -0700492 virtual void emitCode(GrGLFPBuilder*,
joshualittb0a8a372014-09-23 09:50:21 -0700493 const GrFragmentProcessor&,
sugoi@google.come3b4c502013-04-05 13:47:09 +0000494 const char* outputColor,
495 const char* inputColor,
bsalomon@google.com77af6802013-10-02 13:04:56 +0000496 const TransformedCoordsArray&,
sugoi@google.come3b4c502013-04-05 13:47:09 +0000497 const TextureSamplerArray&) SK_OVERRIDE;
498
mtklein72c9faa2015-01-09 10:06:39 -0800499 void setData(const GrGLProgramDataManager&, const GrProcessor&) SK_OVERRIDE;
sugoi@google.come3b4c502013-04-05 13:47:09 +0000500
joshualittb0a8a372014-09-23 09:50:21 -0700501 static inline void GenKey(const GrProcessor&, const GrGLCaps&, GrProcessorKeyBuilder* b);
sugoi@google.com4775cba2013-04-17 13:46:56 +0000502
503private:
sugoi@google.com4775cba2013-04-17 13:46:56 +0000504
kkinnunen7510b222014-07-30 00:04:16 -0700505 GrGLProgramDataManager::UniformHandle fStitchDataUni;
506 SkPerlinNoiseShader::Type fType;
507 bool fStitchTiles;
508 int fNumOctaves;
509 GrGLProgramDataManager::UniformHandle fBaseFrequencyUni;
510 GrGLProgramDataManager::UniformHandle fAlphaUni;
senorblancof3b50272014-06-16 10:49:58 -0700511
512private:
joshualittb0a8a372014-09-23 09:50:21 -0700513 typedef GrGLFragmentProcessor INHERITED;
sugoi@google.come3b4c502013-04-05 13:47:09 +0000514};
515
516/////////////////////////////////////////////////////////////////////
517
joshualittb0a8a372014-09-23 09:50:21 -0700518class GrPerlinNoiseEffect : public GrFragmentProcessor {
sugoi@google.come3b4c502013-04-05 13:47:09 +0000519public:
joshualittb0a8a372014-09-23 09:50:21 -0700520 static GrFragmentProcessor* Create(SkPerlinNoiseShader::Type type,
521 int numOctaves, bool stitchTiles,
522 SkPerlinNoiseShader::PaintingData* paintingData,
523 GrTexture* permutationsTexture, GrTexture* noiseTexture,
524 const SkMatrix& matrix, uint8_t alpha) {
bsalomon55fad7a2014-07-08 07:34:20 -0700525 return SkNEW_ARGS(GrPerlinNoiseEffect, (type, numOctaves, stitchTiles, paintingData,
526 permutationsTexture, noiseTexture, matrix, alpha));
sugoi@google.come3b4c502013-04-05 13:47:09 +0000527 }
528
senorblancoca6a7c22014-06-27 13:35:52 -0700529 virtual ~GrPerlinNoiseEffect() {
530 SkDELETE(fPaintingData);
531 }
sugoi@google.come3b4c502013-04-05 13:47:09 +0000532
mtklein72c9faa2015-01-09 10:06:39 -0800533 const char* name() const SK_OVERRIDE { return "PerlinNoise"; }
joshualitteb2a6762014-12-04 11:35:33 -0800534
535 virtual void getGLProcessorKey(const GrGLCaps& caps,
536 GrProcessorKeyBuilder* b) const SK_OVERRIDE {
537 GrGLPerlinNoise::GenKey(*this, caps, b);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000538 }
joshualitteb2a6762014-12-04 11:35:33 -0800539
mtklein72c9faa2015-01-09 10:06:39 -0800540 GrGLFragmentProcessor* createGLInstance() const SK_OVERRIDE {
joshualitteb2a6762014-12-04 11:35:33 -0800541 return SkNEW_ARGS(GrGLPerlinNoise, (*this));
542 }
543
senorblancoca6a7c22014-06-27 13:35:52 -0700544 const SkPerlinNoiseShader::StitchData& stitchData() const { return fPaintingData->fStitchDataInit; }
sugoi@google.come3b4c502013-04-05 13:47:09 +0000545
senorblancof3b50272014-06-16 10:49:58 -0700546 SkPerlinNoiseShader::Type type() const { return fType; }
547 bool stitchTiles() const { return fStitchTiles; }
senorblancoca6a7c22014-06-27 13:35:52 -0700548 const SkVector& baseFrequency() const { return fPaintingData->fBaseFrequency; }
senorblancof3b50272014-06-16 10:49:58 -0700549 int numOctaves() const { return fNumOctaves; }
550 const SkMatrix& matrix() const { return fCoordTransform.getMatrix(); }
551 uint8_t alpha() const { return fAlpha; }
552
sugoi@google.come3b4c502013-04-05 13:47:09 +0000553private:
mtklein72c9faa2015-01-09 10:06:39 -0800554 bool onIsEqual(const GrFragmentProcessor& sBase) const SK_OVERRIDE {
joshualitt49586be2014-09-16 08:21:41 -0700555 const GrPerlinNoiseEffect& s = sBase.cast<GrPerlinNoiseEffect>();
senorblancof3b50272014-06-16 10:49:58 -0700556 return fType == s.fType &&
senorblancoca6a7c22014-06-27 13:35:52 -0700557 fPaintingData->fBaseFrequency == s.fPaintingData->fBaseFrequency &&
senorblancof3b50272014-06-16 10:49:58 -0700558 fNumOctaves == s.fNumOctaves &&
559 fStitchTiles == s.fStitchTiles &&
senorblancof3b50272014-06-16 10:49:58 -0700560 fAlpha == s.fAlpha &&
senorblancoca6a7c22014-06-27 13:35:52 -0700561 fPaintingData->fStitchDataInit == s.fPaintingData->fStitchDataInit;
sugoi@google.come3b4c502013-04-05 13:47:09 +0000562 }
563
egdaniel605dd0f2014-11-12 08:35:25 -0800564 void onComputeInvariantOutput(GrInvariantOutput* inout) const SK_OVERRIDE {
565 inout->setToUnknown(GrInvariantOutput::kWillNot_ReadInput);
egdaniel1a8ecdf2014-10-03 06:24:12 -0700566 }
567
senorblancoca6a7c22014-06-27 13:35:52 -0700568 GrPerlinNoiseEffect(SkPerlinNoiseShader::Type type,
sugoi@google.come3b4c502013-04-05 13:47:09 +0000569 int numOctaves, bool stitchTiles,
senorblancoca6a7c22014-06-27 13:35:52 -0700570 SkPerlinNoiseShader::PaintingData* paintingData,
sugoi@google.come3b4c502013-04-05 13:47:09 +0000571 GrTexture* permutationsTexture, GrTexture* noiseTexture,
572 const SkMatrix& matrix, uint8_t alpha)
senorblancof3b50272014-06-16 10:49:58 -0700573 : fType(type)
senorblancof3b50272014-06-16 10:49:58 -0700574 , fNumOctaves(numOctaves)
575 , fStitchTiles(stitchTiles)
576 , fAlpha(alpha)
sugoi@google.com4775cba2013-04-17 13:46:56 +0000577 , fPermutationsAccess(permutationsTexture)
sugoi@google.come3b4c502013-04-05 13:47:09 +0000578 , fNoiseAccess(noiseTexture)
senorblancoca6a7c22014-06-27 13:35:52 -0700579 , fPaintingData(paintingData) {
joshualitteb2a6762014-12-04 11:35:33 -0800580 this->initClassID<GrPerlinNoiseEffect>();
sugoi@google.come3b4c502013-04-05 13:47:09 +0000581 this->addTextureAccess(&fPermutationsAccess);
582 this->addTextureAccess(&fNoiseAccess);
senorblancoca6a7c22014-06-27 13:35:52 -0700583 fCoordTransform.reset(kLocal_GrCoordSet, matrix);
senorblancof3b50272014-06-16 10:49:58 -0700584 this->addCoordTransform(&fCoordTransform);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000585 }
586
joshualittb0a8a372014-09-23 09:50:21 -0700587 GR_DECLARE_FRAGMENT_PROCESSOR_TEST;
sugoi@google.come3b4c502013-04-05 13:47:09 +0000588
senorblancof3b50272014-06-16 10:49:58 -0700589 SkPerlinNoiseShader::Type fType;
590 GrCoordTransform fCoordTransform;
senorblancof3b50272014-06-16 10:49:58 -0700591 int fNumOctaves;
592 bool fStitchTiles;
593 uint8_t fAlpha;
sugoi@google.come3b4c502013-04-05 13:47:09 +0000594 GrTextureAccess fPermutationsAccess;
595 GrTextureAccess fNoiseAccess;
senorblancoca6a7c22014-06-27 13:35:52 -0700596 SkPerlinNoiseShader::PaintingData *fPaintingData;
sugoi@google.come3b4c502013-04-05 13:47:09 +0000597
sugoi@google.com4775cba2013-04-17 13:46:56 +0000598private:
joshualittb0a8a372014-09-23 09:50:21 -0700599 typedef GrFragmentProcessor INHERITED;
sugoi@google.come3b4c502013-04-05 13:47:09 +0000600};
601
602/////////////////////////////////////////////////////////////////////
joshualittb0a8a372014-09-23 09:50:21 -0700603GR_DEFINE_FRAGMENT_PROCESSOR_TEST(GrPerlinNoiseEffect);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000604
joshualittb0a8a372014-09-23 09:50:21 -0700605GrFragmentProcessor* GrPerlinNoiseEffect::TestCreate(SkRandom* random,
606 GrContext* context,
607 const GrDrawTargetCaps&,
608 GrTexture**) {
sugoi@google.com423ac132013-04-18 14:04:57 +0000609 int numOctaves = random->nextRangeU(2, 10);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000610 bool stitchTiles = random->nextBool();
611 SkScalar seed = SkIntToScalar(random->nextU());
612 SkISize tileSize = SkISize::Make(random->nextRangeU(4, 4096), random->nextRangeU(4, 4096));
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +0000613 SkScalar baseFrequencyX = random->nextRangeScalar(0.01f,
614 0.99f);
615 SkScalar baseFrequencyY = random->nextRangeScalar(0.01f,
616 0.99f);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000617
618 SkShader* shader = random->nextBool() ?
619 SkPerlinNoiseShader::CreateFractalNoise(baseFrequencyX, baseFrequencyY, numOctaves, seed,
620 stitchTiles ? &tileSize : NULL) :
commit-bot@chromium.org9fbbcca2014-04-01 16:09:37 +0000621 SkPerlinNoiseShader::CreateTurbulence(baseFrequencyX, baseFrequencyY, numOctaves, seed,
sugoi@google.come3b4c502013-04-05 13:47:09 +0000622 stitchTiles ? &tileSize : NULL);
623
624 SkPaint paint;
bsalomon83d081a2014-07-08 09:56:10 -0700625 GrColor paintColor;
joshualittb0a8a372014-09-23 09:50:21 -0700626 GrFragmentProcessor* effect;
joshualitt5531d512014-12-17 15:50:11 -0800627 SkAssertResult(shader->asFragmentProcessor(context, paint,
628 GrProcessorUnitTest::TestMatrix(random), NULL,
629 &paintColor, &effect));
sugoi@google.come3b4c502013-04-05 13:47:09 +0000630
631 SkDELETE(shader);
632
633 return effect;
634}
sugoi@google.com4775cba2013-04-17 13:46:56 +0000635
joshualitteb2a6762014-12-04 11:35:33 -0800636GrGLPerlinNoise::GrGLPerlinNoise(const GrProcessor& processor)
637 : fType(processor.cast<GrPerlinNoiseEffect>().type())
joshualittb0a8a372014-09-23 09:50:21 -0700638 , fStitchTiles(processor.cast<GrPerlinNoiseEffect>().stitchTiles())
639 , fNumOctaves(processor.cast<GrPerlinNoiseEffect>().numOctaves()) {
sugoi@google.com4775cba2013-04-17 13:46:56 +0000640}
641
joshualitt15988992014-10-09 15:04:05 -0700642void GrGLPerlinNoise::emitCode(GrGLFPBuilder* builder,
joshualittb0a8a372014-09-23 09:50:21 -0700643 const GrFragmentProcessor&,
sugoi@google.come3b4c502013-04-05 13:47:09 +0000644 const char* outputColor,
645 const char* inputColor,
bsalomon@google.com77af6802013-10-02 13:04:56 +0000646 const TransformedCoordsArray& coords,
sugoi@google.come3b4c502013-04-05 13:47:09 +0000647 const TextureSamplerArray& samplers) {
648 sk_ignore_unused_variable(inputColor);
649
joshualitt15988992014-10-09 15:04:05 -0700650 GrGLFPFragmentBuilder* fsBuilder = builder->getFragmentShaderBuilder();
joshualitt30ba4362014-08-21 20:18:45 -0700651 SkString vCoords = fsBuilder->ensureFSCoords2D(coords, 0);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000652
joshualitt30ba4362014-08-21 20:18:45 -0700653 fBaseFrequencyUni = builder->addUniform(GrGLProgramBuilder::kFragment_Visibility,
bsalomon422f56f2014-12-09 10:18:12 -0800654 kVec2f_GrSLType, kDefault_GrSLPrecision,
655 "baseFrequency");
sugoi@google.come3b4c502013-04-05 13:47:09 +0000656 const char* baseFrequencyUni = builder->getUniformCStr(fBaseFrequencyUni);
joshualitt30ba4362014-08-21 20:18:45 -0700657 fAlphaUni = builder->addUniform(GrGLProgramBuilder::kFragment_Visibility,
bsalomon422f56f2014-12-09 10:18:12 -0800658 kFloat_GrSLType, kDefault_GrSLPrecision,
659 "alpha");
sugoi@google.come3b4c502013-04-05 13:47:09 +0000660 const char* alphaUni = builder->getUniformCStr(fAlphaUni);
661
662 const char* stitchDataUni = NULL;
663 if (fStitchTiles) {
joshualitt30ba4362014-08-21 20:18:45 -0700664 fStitchDataUni = builder->addUniform(GrGLProgramBuilder::kFragment_Visibility,
bsalomon422f56f2014-12-09 10:18:12 -0800665 kVec2f_GrSLType, kDefault_GrSLPrecision,
666 "stitchData");
sugoi@google.come3b4c502013-04-05 13:47:09 +0000667 stitchDataUni = builder->getUniformCStr(fStitchDataUni);
668 }
669
sugoi@google.comd537af52013-06-10 13:59:25 +0000670 // There are 4 lines, so the center of each line is 1/8, 3/8, 5/8 and 7/8
671 const char* chanCoordR = "0.125";
672 const char* chanCoordG = "0.375";
673 const char* chanCoordB = "0.625";
674 const char* chanCoordA = "0.875";
675 const char* chanCoord = "chanCoord";
sugoi@google.come3b4c502013-04-05 13:47:09 +0000676 const char* stitchData = "stitchData";
677 const char* ratio = "ratio";
sugoi@google.come3b4c502013-04-05 13:47:09 +0000678 const char* noiseVec = "noiseVec";
sugoi@google.come3b4c502013-04-05 13:47:09 +0000679 const char* noiseSmooth = "noiseSmooth";
senorblancoce6a3542014-06-12 11:24:19 -0700680 const char* floorVal = "floorVal";
sugoi@google.come3b4c502013-04-05 13:47:09 +0000681 const char* fractVal = "fractVal";
682 const char* uv = "uv";
683 const char* ab = "ab";
684 const char* latticeIdx = "latticeIdx";
senorblancoce6a3542014-06-12 11:24:19 -0700685 const char* bcoords = "bcoords";
sugoi@google.come3b4c502013-04-05 13:47:09 +0000686 const char* lattice = "lattice";
sugoi@google.come3b4c502013-04-05 13:47:09 +0000687 const char* inc8bit = "0.00390625"; // 1.0 / 256.0
688 // This is the math to convert the two 16bit integer packed into rgba 8 bit input into a
689 // [-1,1] vector and perform a dot product between that vector and the provided vector.
690 const char* dotLattice = "dot(((%s.ga + %s.rb * vec2(%s)) * vec2(2.0) - vec2(1.0)), %s);";
691
sugoi@google.comd537af52013-06-10 13:59:25 +0000692 // Add noise function
693 static const GrGLShaderVar gPerlinNoiseArgs[] = {
694 GrGLShaderVar(chanCoord, kFloat_GrSLType),
commit-bot@chromium.org98393202013-07-04 18:13:05 +0000695 GrGLShaderVar(noiseVec, kVec2f_GrSLType)
sugoi@google.comd537af52013-06-10 13:59:25 +0000696 };
sugoi@google.come3b4c502013-04-05 13:47:09 +0000697
sugoi@google.comd537af52013-06-10 13:59:25 +0000698 static const GrGLShaderVar gPerlinNoiseStitchArgs[] = {
699 GrGLShaderVar(chanCoord, kFloat_GrSLType),
commit-bot@chromium.org98393202013-07-04 18:13:05 +0000700 GrGLShaderVar(noiseVec, kVec2f_GrSLType),
701 GrGLShaderVar(stitchData, kVec2f_GrSLType)
sugoi@google.comd537af52013-06-10 13:59:25 +0000702 };
sugoi@google.come3b4c502013-04-05 13:47:09 +0000703
sugoi@google.comd537af52013-06-10 13:59:25 +0000704 SkString noiseCode;
sugoi@google.come3b4c502013-04-05 13:47:09 +0000705
senorblancoce6a3542014-06-12 11:24:19 -0700706 noiseCode.appendf("\tvec4 %s;\n", floorVal);
707 noiseCode.appendf("\t%s.xy = floor(%s);\n", floorVal, noiseVec);
708 noiseCode.appendf("\t%s.zw = %s.xy + vec2(1.0);\n", floorVal, floorVal);
709 noiseCode.appendf("\tvec2 %s = fract(%s);\n", fractVal, noiseVec);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000710
711 // smooth curve : t * t * (3 - 2 * t)
senorblancoce6a3542014-06-12 11:24:19 -0700712 noiseCode.appendf("\n\tvec2 %s = %s * %s * (vec2(3.0) - vec2(2.0) * %s);",
713 noiseSmooth, fractVal, fractVal, fractVal);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000714
715 // Adjust frequencies if we're stitching tiles
716 if (fStitchTiles) {
commit-bot@chromium.org98393202013-07-04 18:13:05 +0000717 noiseCode.appendf("\n\tif(%s.x >= %s.x) { %s.x -= %s.x; }",
senorblancoce6a3542014-06-12 11:24:19 -0700718 floorVal, stitchData, floorVal, stitchData);
commit-bot@chromium.org98393202013-07-04 18:13:05 +0000719 noiseCode.appendf("\n\tif(%s.y >= %s.y) { %s.y -= %s.y; }",
senorblancoce6a3542014-06-12 11:24:19 -0700720 floorVal, stitchData, floorVal, stitchData);
721 noiseCode.appendf("\n\tif(%s.z >= %s.x) { %s.z -= %s.x; }",
722 floorVal, stitchData, floorVal, stitchData);
723 noiseCode.appendf("\n\tif(%s.w >= %s.y) { %s.w -= %s.y; }",
724 floorVal, stitchData, floorVal, stitchData);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000725 }
726
727 // Get texture coordinates and normalize
senorblancoce6a3542014-06-12 11:24:19 -0700728 noiseCode.appendf("\n\t%s = fract(floor(mod(%s, 256.0)) / vec4(256.0));\n",
729 floorVal, floorVal);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000730
731 // Get permutation for x
732 {
733 SkString xCoords("");
senorblancoce6a3542014-06-12 11:24:19 -0700734 xCoords.appendf("vec2(%s.x, 0.5)", floorVal);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000735
sugoi@google.comd537af52013-06-10 13:59:25 +0000736 noiseCode.appendf("\n\tvec2 %s;\n\t%s.x = ", latticeIdx, latticeIdx);
joshualitt30ba4362014-08-21 20:18:45 -0700737 fsBuilder->appendTextureLookup(&noiseCode, samplers[0], xCoords.c_str(), kVec2f_GrSLType);
sugoi@google.comd537af52013-06-10 13:59:25 +0000738 noiseCode.append(".r;");
sugoi@google.come3b4c502013-04-05 13:47:09 +0000739 }
740
741 // Get permutation for x + 1
742 {
743 SkString xCoords("");
senorblancoce6a3542014-06-12 11:24:19 -0700744 xCoords.appendf("vec2(%s.z, 0.5)", floorVal);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000745
sugoi@google.comd537af52013-06-10 13:59:25 +0000746 noiseCode.appendf("\n\t%s.y = ", latticeIdx);
joshualitt30ba4362014-08-21 20:18:45 -0700747 fsBuilder->appendTextureLookup(&noiseCode, samplers[0], xCoords.c_str(), kVec2f_GrSLType);
sugoi@google.comd537af52013-06-10 13:59:25 +0000748 noiseCode.append(".r;");
sugoi@google.come3b4c502013-04-05 13:47:09 +0000749 }
750
commit-bot@chromium.org344cf452013-06-17 14:19:01 +0000751#if defined(SK_BUILD_FOR_ANDROID)
752 // Android rounding for Tegra devices, like, for example: Xoom (Tegra 2), Nexus 7 (Tegra 3).
753 // The issue is that colors aren't accurate enough on Tegra devices. For example, if an 8 bit
754 // value of 124 (or 0.486275 here) is entered, we can get a texture value of 123.513725
755 // (or 0.484368 here). The following rounding operation prevents these precision issues from
756 // affecting the result of the noise by making sure that we only have multiples of 1/255.
757 // (Note that 1/255 is about 0.003921569, which is the value used here).
758 noiseCode.appendf("\n\t%s = floor(%s * vec2(255.0) + vec2(0.5)) * vec2(0.003921569);",
759 latticeIdx, latticeIdx);
760#endif
761
sugoi@google.come3b4c502013-04-05 13:47:09 +0000762 // Get (x,y) coordinates with the permutated x
senorblancoce6a3542014-06-12 11:24:19 -0700763 noiseCode.appendf("\n\tvec4 %s = fract(%s.xyxy + %s.yyww);", bcoords, latticeIdx, floorVal);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000764
sugoi@google.comd537af52013-06-10 13:59:25 +0000765 noiseCode.appendf("\n\n\tvec2 %s;", uv);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000766 // Compute u, at offset (0,0)
767 {
768 SkString latticeCoords("");
senorblancoce6a3542014-06-12 11:24:19 -0700769 latticeCoords.appendf("vec2(%s.x, %s)", bcoords, chanCoord);
sugoi@google.comd537af52013-06-10 13:59:25 +0000770 noiseCode.appendf("\n\tvec4 %s = ", lattice);
joshualitt30ba4362014-08-21 20:18:45 -0700771 fsBuilder->appendTextureLookup(&noiseCode, samplers[1], latticeCoords.c_str(),
sugoi@google.comd537af52013-06-10 13:59:25 +0000772 kVec2f_GrSLType);
773 noiseCode.appendf(".bgra;\n\t%s.x = ", uv);
774 noiseCode.appendf(dotLattice, lattice, lattice, inc8bit, fractVal);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000775 }
776
sugoi@google.comd537af52013-06-10 13:59:25 +0000777 noiseCode.appendf("\n\t%s.x -= 1.0;", fractVal);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000778 // Compute v, at offset (-1,0)
779 {
780 SkString latticeCoords("");
senorblancoce6a3542014-06-12 11:24:19 -0700781 latticeCoords.appendf("vec2(%s.y, %s)", bcoords, chanCoord);
commit-bot@chromium.org344cf452013-06-17 14:19:01 +0000782 noiseCode.append("\n\tlattice = ");
joshualitt30ba4362014-08-21 20:18:45 -0700783 fsBuilder->appendTextureLookup(&noiseCode, samplers[1], latticeCoords.c_str(),
sugoi@google.comd537af52013-06-10 13:59:25 +0000784 kVec2f_GrSLType);
785 noiseCode.appendf(".bgra;\n\t%s.y = ", uv);
786 noiseCode.appendf(dotLattice, lattice, lattice, inc8bit, fractVal);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000787 }
788
789 // Compute 'a' as a linear interpolation of 'u' and 'v'
sugoi@google.comd537af52013-06-10 13:59:25 +0000790 noiseCode.appendf("\n\tvec2 %s;", ab);
791 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 +0000792
sugoi@google.comd537af52013-06-10 13:59:25 +0000793 noiseCode.appendf("\n\t%s.y -= 1.0;", fractVal);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000794 // Compute v, at offset (-1,-1)
795 {
796 SkString latticeCoords("");
senorblancoce6a3542014-06-12 11:24:19 -0700797 latticeCoords.appendf("vec2(%s.w, %s)", bcoords, chanCoord);
commit-bot@chromium.org344cf452013-06-17 14:19:01 +0000798 noiseCode.append("\n\tlattice = ");
joshualitt30ba4362014-08-21 20:18:45 -0700799 fsBuilder->appendTextureLookup(&noiseCode, samplers[1], latticeCoords.c_str(),
sugoi@google.comd537af52013-06-10 13:59:25 +0000800 kVec2f_GrSLType);
801 noiseCode.appendf(".bgra;\n\t%s.y = ", uv);
802 noiseCode.appendf(dotLattice, lattice, lattice, inc8bit, fractVal);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000803 }
804
sugoi@google.comd537af52013-06-10 13:59:25 +0000805 noiseCode.appendf("\n\t%s.x += 1.0;", fractVal);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000806 // Compute u, at offset (0,-1)
807 {
808 SkString latticeCoords("");
senorblancoce6a3542014-06-12 11:24:19 -0700809 latticeCoords.appendf("vec2(%s.z, %s)", bcoords, chanCoord);
commit-bot@chromium.org344cf452013-06-17 14:19:01 +0000810 noiseCode.append("\n\tlattice = ");
joshualitt30ba4362014-08-21 20:18:45 -0700811 fsBuilder->appendTextureLookup(&noiseCode, samplers[1], latticeCoords.c_str(),
sugoi@google.comd537af52013-06-10 13:59:25 +0000812 kVec2f_GrSLType);
813 noiseCode.appendf(".bgra;\n\t%s.x = ", uv);
814 noiseCode.appendf(dotLattice, lattice, lattice, inc8bit, fractVal);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000815 }
816
817 // Compute 'b' as a linear interpolation of 'u' and 'v'
sugoi@google.comd537af52013-06-10 13:59:25 +0000818 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 +0000819 // Compute the noise as a linear interpolation of 'a' and 'b'
sugoi@google.comd537af52013-06-10 13:59:25 +0000820 noiseCode.appendf("\n\treturn mix(%s.x, %s.y, %s.y);\n", ab, ab, noiseSmooth);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000821
sugoi@google.comd537af52013-06-10 13:59:25 +0000822 SkString noiseFuncName;
823 if (fStitchTiles) {
joshualitt30ba4362014-08-21 20:18:45 -0700824 fsBuilder->emitFunction(kFloat_GrSLType,
commit-bot@chromium.org74a3a212013-08-30 19:43:59 +0000825 "perlinnoise", SK_ARRAY_COUNT(gPerlinNoiseStitchArgs),
826 gPerlinNoiseStitchArgs, noiseCode.c_str(), &noiseFuncName);
sugoi@google.comd537af52013-06-10 13:59:25 +0000827 } else {
joshualitt30ba4362014-08-21 20:18:45 -0700828 fsBuilder->emitFunction(kFloat_GrSLType,
commit-bot@chromium.org74a3a212013-08-30 19:43:59 +0000829 "perlinnoise", SK_ARRAY_COUNT(gPerlinNoiseArgs),
830 gPerlinNoiseArgs, noiseCode.c_str(), &noiseFuncName);
sugoi@google.comd537af52013-06-10 13:59:25 +0000831 }
sugoi@google.come3b4c502013-04-05 13:47:09 +0000832
sugoi@google.comd537af52013-06-10 13:59:25 +0000833 // There are rounding errors if the floor operation is not performed here
joshualitt30ba4362014-08-21 20:18:45 -0700834 fsBuilder->codeAppendf("\n\t\tvec2 %s = floor(%s.xy) * %s;",
senorblancoca6a7c22014-06-27 13:35:52 -0700835 noiseVec, vCoords.c_str(), baseFrequencyUni);
sugoi@google.comd537af52013-06-10 13:59:25 +0000836
837 // Clear the color accumulator
joshualitt30ba4362014-08-21 20:18:45 -0700838 fsBuilder->codeAppendf("\n\t\t%s = vec4(0.0);", outputColor);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000839
840 if (fStitchTiles) {
sugoi@google.comd537af52013-06-10 13:59:25 +0000841 // Set up TurbulenceInitial stitch values.
joshualitt30ba4362014-08-21 20:18:45 -0700842 fsBuilder->codeAppendf("\n\t\tvec2 %s = %s;", stitchData, stitchDataUni);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000843 }
sugoi@google.come3b4c502013-04-05 13:47:09 +0000844
joshualitt30ba4362014-08-21 20:18:45 -0700845 fsBuilder->codeAppendf("\n\t\tfloat %s = 1.0;", ratio);
sugoi@google.comd537af52013-06-10 13:59:25 +0000846
847 // Loop over all octaves
joshualitt30ba4362014-08-21 20:18:45 -0700848 fsBuilder->codeAppendf("\n\t\tfor (int octave = 0; octave < %d; ++octave) {", fNumOctaves);
sugoi@google.comd537af52013-06-10 13:59:25 +0000849
joshualitt30ba4362014-08-21 20:18:45 -0700850 fsBuilder->codeAppendf("\n\t\t\t%s += ", outputColor);
sugoi@google.comd537af52013-06-10 13:59:25 +0000851 if (fType != SkPerlinNoiseShader::kFractalNoise_Type) {
joshualitt30ba4362014-08-21 20:18:45 -0700852 fsBuilder->codeAppend("abs(");
sugoi@google.comd537af52013-06-10 13:59:25 +0000853 }
854 if (fStitchTiles) {
joshualitt30ba4362014-08-21 20:18:45 -0700855 fsBuilder->codeAppendf(
sugoi@google.comd537af52013-06-10 13:59:25 +0000856 "vec4(\n\t\t\t\t%s(%s, %s, %s),\n\t\t\t\t%s(%s, %s, %s),"
857 "\n\t\t\t\t%s(%s, %s, %s),\n\t\t\t\t%s(%s, %s, %s))",
858 noiseFuncName.c_str(), chanCoordR, noiseVec, stitchData,
859 noiseFuncName.c_str(), chanCoordG, noiseVec, stitchData,
860 noiseFuncName.c_str(), chanCoordB, noiseVec, stitchData,
861 noiseFuncName.c_str(), chanCoordA, noiseVec, stitchData);
862 } else {
joshualitt30ba4362014-08-21 20:18:45 -0700863 fsBuilder->codeAppendf(
sugoi@google.comd537af52013-06-10 13:59:25 +0000864 "vec4(\n\t\t\t\t%s(%s, %s),\n\t\t\t\t%s(%s, %s),"
865 "\n\t\t\t\t%s(%s, %s),\n\t\t\t\t%s(%s, %s))",
866 noiseFuncName.c_str(), chanCoordR, noiseVec,
867 noiseFuncName.c_str(), chanCoordG, noiseVec,
868 noiseFuncName.c_str(), chanCoordB, noiseVec,
869 noiseFuncName.c_str(), chanCoordA, noiseVec);
870 }
871 if (fType != SkPerlinNoiseShader::kFractalNoise_Type) {
joshualitt30ba4362014-08-21 20:18:45 -0700872 fsBuilder->codeAppendf(")"); // end of "abs("
sugoi@google.comd537af52013-06-10 13:59:25 +0000873 }
joshualitt30ba4362014-08-21 20:18:45 -0700874 fsBuilder->codeAppendf(" * %s;", ratio);
sugoi@google.comd537af52013-06-10 13:59:25 +0000875
joshualitt30ba4362014-08-21 20:18:45 -0700876 fsBuilder->codeAppendf("\n\t\t\t%s *= vec2(2.0);", noiseVec);
877 fsBuilder->codeAppendf("\n\t\t\t%s *= 0.5;", ratio);
sugoi@google.comd537af52013-06-10 13:59:25 +0000878
879 if (fStitchTiles) {
joshualitt30ba4362014-08-21 20:18:45 -0700880 fsBuilder->codeAppendf("\n\t\t\t%s *= vec2(2.0);", stitchData);
sugoi@google.comd537af52013-06-10 13:59:25 +0000881 }
joshualitt30ba4362014-08-21 20:18:45 -0700882 fsBuilder->codeAppend("\n\t\t}"); // end of the for loop on octaves
sugoi@google.come3b4c502013-04-05 13:47:09 +0000883
884 if (fType == SkPerlinNoiseShader::kFractalNoise_Type) {
885 // The value of turbulenceFunctionResult comes from ((turbulenceFunctionResult) + 1) / 2
886 // by fractalNoise and (turbulenceFunctionResult) by turbulence.
joshualitt30ba4362014-08-21 20:18:45 -0700887 fsBuilder->codeAppendf("\n\t\t%s = %s * vec4(0.5) + vec4(0.5);", outputColor, outputColor);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000888 }
889
joshualitt30ba4362014-08-21 20:18:45 -0700890 fsBuilder->codeAppendf("\n\t\t%s.a *= %s;", outputColor, alphaUni);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000891
892 // Clamp values
joshualitt30ba4362014-08-21 20:18:45 -0700893 fsBuilder->codeAppendf("\n\t\t%s = clamp(%s, 0.0, 1.0);", outputColor, outputColor);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000894
895 // Pre-multiply the result
joshualitt30ba4362014-08-21 20:18:45 -0700896 fsBuilder->codeAppendf("\n\t\t%s = vec4(%s.rgb * %s.aaa, %s.a);\n",
sugoi@google.come3b4c502013-04-05 13:47:09 +0000897 outputColor, outputColor, outputColor, outputColor);
898}
899
joshualittb0a8a372014-09-23 09:50:21 -0700900void GrGLPerlinNoise::GenKey(const GrProcessor& processor, const GrGLCaps&,
901 GrProcessorKeyBuilder* b) {
902 const GrPerlinNoiseEffect& turbulence = processor.cast<GrPerlinNoiseEffect>();
sugoi@google.come3b4c502013-04-05 13:47:09 +0000903
bsalomon63e99f72014-07-21 08:03:14 -0700904 uint32_t key = turbulence.numOctaves();
sugoi@google.come3b4c502013-04-05 13:47:09 +0000905
906 key = key << 3; // Make room for next 3 bits
907
908 switch (turbulence.type()) {
909 case SkPerlinNoiseShader::kFractalNoise_Type:
910 key |= 0x1;
911 break;
912 case SkPerlinNoiseShader::kTurbulence_Type:
913 key |= 0x2;
914 break;
915 default:
916 // leave key at 0
917 break;
918 }
919
920 if (turbulence.stitchTiles()) {
921 key |= 0x4; // Flip the 3rd bit if tile stitching is on
922 }
923
bsalomon63e99f72014-07-21 08:03:14 -0700924 b->add32(key);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000925}
926
joshualittb0a8a372014-09-23 09:50:21 -0700927void GrGLPerlinNoise::setData(const GrGLProgramDataManager& pdman, const GrProcessor& processor) {
928 INHERITED::setData(pdman, processor);
senorblancof3b50272014-06-16 10:49:58 -0700929
joshualittb0a8a372014-09-23 09:50:21 -0700930 const GrPerlinNoiseEffect& turbulence = processor.cast<GrPerlinNoiseEffect>();
sugoi@google.come3b4c502013-04-05 13:47:09 +0000931
932 const SkVector& baseFrequency = turbulence.baseFrequency();
kkinnunen7510b222014-07-30 00:04:16 -0700933 pdman.set2f(fBaseFrequencyUni, baseFrequency.fX, baseFrequency.fY);
934 pdman.set1f(fAlphaUni, SkScalarDiv(SkIntToScalar(turbulence.alpha()), SkIntToScalar(255)));
sugoi@google.come3b4c502013-04-05 13:47:09 +0000935
sugoi@google.com4775cba2013-04-17 13:46:56 +0000936 if (turbulence.stitchTiles()) {
937 const SkPerlinNoiseShader::StitchData& stitchData = turbulence.stitchData();
kkinnunen7510b222014-07-30 00:04:16 -0700938 pdman.set2f(fStitchDataUni, SkIntToScalar(stitchData.fWidth),
commit-bot@chromium.org98393202013-07-04 18:13:05 +0000939 SkIntToScalar(stitchData.fHeight));
sugoi@google.com4775cba2013-04-17 13:46:56 +0000940 }
941}
942
sugoi@google.come3b4c502013-04-05 13:47:09 +0000943/////////////////////////////////////////////////////////////////////
944
joshualittb0a8a372014-09-23 09:50:21 -0700945bool SkPerlinNoiseShader::asFragmentProcessor(GrContext* context, const SkPaint& paint,
joshualitt5531d512014-12-17 15:50:11 -0800946 const SkMatrix& viewM,
joshualittb0a8a372014-09-23 09:50:21 -0700947 const SkMatrix* externalLocalMatrix,
948 GrColor* paintColor, GrFragmentProcessor** fp) const {
bsalomon49f085d2014-09-05 13:34:00 -0700949 SkASSERT(context);
mtklein3f3b3d02014-12-01 11:47:08 -0800950
bsalomon83d081a2014-07-08 09:56:10 -0700951 *paintColor = SkColor2GrColorJustAlpha(paint.getColor());
senorblancoca6a7c22014-06-27 13:35:52 -0700952
commit-bot@chromium.org96fb7482014-05-09 20:28:11 +0000953 SkMatrix localMatrix = this->getLocalMatrix();
954 if (externalLocalMatrix) {
955 localMatrix.preConcat(*externalLocalMatrix);
956 }
957
joshualitt5531d512014-12-17 15:50:11 -0800958 SkMatrix matrix = viewM;
senorblancoca6a7c22014-06-27 13:35:52 -0700959 matrix.preConcat(localMatrix);
960
commit-bot@chromium.orgc2a0ea62013-11-06 10:08:38 +0000961 if (0 == fNumOctaves) {
commit-bot@chromium.orgc2a0ea62013-11-06 10:08:38 +0000962 if (kFractalNoise_Type == fType) {
bsalomonea8b55d2015-03-04 11:03:52 -0800963 uint32_t alpha = paint.getAlpha() >> 1;
964 uint32_t rgb = alpha >> 1;
965 *paintColor = GrColorPackRGBA(rgb, rgb, rgb, alpha);
966 } else {
967 *paintColor = 0;
reedcff10b22015-03-03 06:41:45 -0800968 }
dandov9de5b512014-06-10 14:38:28 -0700969 return true;
commit-bot@chromium.orgc2a0ea62013-11-06 10:08:38 +0000970 }
971
sugoi@google.come3b4c502013-04-05 13:47:09 +0000972 // Either we don't stitch tiles, either we have a valid tile size
973 SkASSERT(!fStitchTiles || !fTileSize.isEmpty());
974
joshualittb0a8a372014-09-23 09:50:21 -0700975 SkPerlinNoiseShader::PaintingData* paintingData =
976 SkNEW_ARGS(PaintingData, (fTileSize, fSeed, fBaseFrequencyX, fBaseFrequencyY, matrix));
bsalomonbcf0a522014-10-08 08:40:09 -0700977 SkAutoTUnref<GrTexture> permutationsTexture(
978 GrRefCachedBitmapTexture(context, paintingData->getPermutationsBitmap(), NULL));
979 SkAutoTUnref<GrTexture> noiseTexture(
980 GrRefCachedBitmapTexture(context, paintingData->getNoiseBitmap(), NULL));
sugoi@google.come3b4c502013-04-05 13:47:09 +0000981
joshualitt5531d512014-12-17 15:50:11 -0800982 SkMatrix m = viewM;
senorblancoca6a7c22014-06-27 13:35:52 -0700983 m.setTranslateX(-localMatrix.getTranslateX() + SK_Scalar1);
984 m.setTranslateY(-localMatrix.getTranslateY() + SK_Scalar1);
bsalomon49f085d2014-09-05 13:34:00 -0700985 if ((permutationsTexture) && (noiseTexture)) {
joshualittb0a8a372014-09-23 09:50:21 -0700986 *fp = GrPerlinNoiseEffect::Create(fType,
987 fNumOctaves,
988 fStitchTiles,
989 paintingData,
990 permutationsTexture, noiseTexture,
991 m, paint.getAlpha());
senorblancoca6a7c22014-06-27 13:35:52 -0700992 } else {
993 SkDELETE(paintingData);
joshualittb0a8a372014-09-23 09:50:21 -0700994 *fp = NULL;
senorblancoca6a7c22014-06-27 13:35:52 -0700995 }
dandov9de5b512014-06-10 14:38:28 -0700996 return true;
sugoi@google.come3b4c502013-04-05 13:47:09 +0000997}
998
999#else
1000
joshualitt5531d512014-12-17 15:50:11 -08001001bool SkPerlinNoiseShader::asFragmentProcessor(GrContext*, const SkPaint&, const SkMatrix&,
1002 const SkMatrix*, GrColor*,
joshualittb0a8a372014-09-23 09:50:21 -07001003 GrFragmentProcessor**) const {
sugoi@google.come3b4c502013-04-05 13:47:09 +00001004 SkDEBUGFAIL("Should not call in GPU-less build");
dandov9de5b512014-06-10 14:38:28 -07001005 return false;
sugoi@google.come3b4c502013-04-05 13:47:09 +00001006}
1007
1008#endif
1009
commit-bot@chromium.org0f10f7b2014-03-13 18:02:17 +00001010#ifndef SK_IGNORE_TO_STRING
sugoi@google.come3b4c502013-04-05 13:47:09 +00001011void SkPerlinNoiseShader::toString(SkString* str) const {
1012 str->append("SkPerlinNoiseShader: (");
1013
1014 str->append("type: ");
1015 switch (fType) {
1016 case kFractalNoise_Type:
1017 str->append("\"fractal noise\"");
1018 break;
1019 case kTurbulence_Type:
1020 str->append("\"turbulence\"");
1021 break;
1022 default:
1023 str->append("\"unknown\"");
1024 break;
1025 }
1026 str->append(" base frequency: (");
1027 str->appendScalar(fBaseFrequencyX);
1028 str->append(", ");
1029 str->appendScalar(fBaseFrequencyY);
1030 str->append(") number of octaves: ");
1031 str->appendS32(fNumOctaves);
1032 str->append(" seed: ");
1033 str->appendScalar(fSeed);
1034 str->append(" stitch tiles: ");
1035 str->append(fStitchTiles ? "true " : "false ");
1036
1037 this->INHERITED::toString(str);
1038
1039 str->append(")");
1040}
1041#endif