blob: e4811627b2da19f4bef53d38b46ac332333f62f4 [file] [log] [blame]
sugoi@google.come3b4c502013-04-05 13:47:09 +00001/*
2 * Copyright 2013 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8#include "SkDither.h"
9#include "SkPerlinNoiseShader.h"
commit-bot@chromium.orgc2a0ea62013-11-06 10:08:38 +000010#include "SkColorFilter.h"
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +000011#include "SkReadBuffer.h"
12#include "SkWriteBuffer.h"
sugoi@google.come3b4c502013-04-05 13:47:09 +000013#include "SkShader.h"
14#include "SkUnPreMultiply.h"
15#include "SkString.h"
16
17#if SK_SUPPORT_GPU
18#include "GrContext.h"
bsalomon@google.com77af6802013-10-02 13:04:56 +000019#include "GrCoordTransform.h"
sugoi@google.come3b4c502013-04-05 13:47:09 +000020#include "gl/GrGLEffect.h"
bsalomon848faf02014-07-11 10:01:02 -070021#include "gl/GrGLShaderBuilder.h"
sugoi@google.come3b4c502013-04-05 13:47:09 +000022#include "GrTBackendEffectFactory.h"
23#include "SkGr.h"
24#endif
25
26static const int kBlockSize = 256;
27static const int kBlockMask = kBlockSize - 1;
28static const int kPerlinNoise = 4096;
29static const int kRandMaximum = SK_MaxS32; // 2**31 - 1
30
31namespace {
32
33// noiseValue is the color component's value (or color)
34// limitValue is the maximum perlin noise array index value allowed
35// newValue is the current noise dimension (either width or height)
36inline int checkNoise(int noiseValue, int limitValue, int newValue) {
37 // If the noise value would bring us out of bounds of the current noise array while we are
38 // stiching noise tiles together, wrap the noise around the current dimension of the noise to
39 // stay within the array bounds in a continuous fashion (so that tiling lines are not visible)
40 if (noiseValue >= limitValue) {
41 noiseValue -= newValue;
42 }
sugoi@google.come3b4c502013-04-05 13:47:09 +000043 return noiseValue;
44}
45
46inline SkScalar smoothCurve(SkScalar t) {
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +000047 static const SkScalar SK_Scalar3 = 3.0f;
sugoi@google.come3b4c502013-04-05 13:47:09 +000048
49 // returns t * t * (3 - 2 * t)
50 return SkScalarMul(SkScalarSquare(t), SK_Scalar3 - 2 * t);
51}
52
commit-bot@chromium.orgce33d602013-11-25 21:46:31 +000053bool perlin_noise_type_is_valid(SkPerlinNoiseShader::Type type) {
54 return (SkPerlinNoiseShader::kFractalNoise_Type == type) ||
55 (SkPerlinNoiseShader::kTurbulence_Type == type);
56}
57
sugoi@google.come3b4c502013-04-05 13:47:09 +000058} // end namespace
59
60struct SkPerlinNoiseShader::StitchData {
61 StitchData()
62 : fWidth(0)
63 , fWrapX(0)
64 , fHeight(0)
65 , fWrapY(0)
66 {}
67
68 bool operator==(const StitchData& other) const {
69 return fWidth == other.fWidth &&
70 fWrapX == other.fWrapX &&
71 fHeight == other.fHeight &&
72 fWrapY == other.fWrapY;
73 }
74
75 int fWidth; // How much to subtract to wrap for stitching.
76 int fWrapX; // Minimum value to wrap.
77 int fHeight;
78 int fWrapY;
79};
80
81struct SkPerlinNoiseShader::PaintingData {
commit-bot@chromium.orgfd5c9a62014-03-06 15:13:53 +000082 PaintingData(const SkISize& tileSize, SkScalar seed,
senorblancoca6a7c22014-06-27 13:35:52 -070083 SkScalar baseFrequencyX, SkScalar baseFrequencyY,
84 const SkMatrix& matrix)
sugoi@google.come3b4c502013-04-05 13:47:09 +000085 {
senorblancoca6a7c22014-06-27 13:35:52 -070086 SkVector wavelength = SkVector::Make(SkScalarInvert(baseFrequencyX),
87 SkScalarInvert(baseFrequencyY));
88 matrix.mapVectors(&wavelength, 1);
89 fBaseFrequency.fX = SkScalarInvert(wavelength.fX);
90 fBaseFrequency.fY = SkScalarInvert(wavelength.fY);
91 SkVector sizeVec = SkVector::Make(SkIntToScalar(tileSize.fWidth),
92 SkIntToScalar(tileSize.fHeight));
93 matrix.mapVectors(&sizeVec, 1);
94 fTileSize.fWidth = SkScalarRoundToInt(sizeVec.fX);
95 fTileSize.fHeight = SkScalarRoundToInt(sizeVec.fY);
commit-bot@chromium.orgfd5c9a62014-03-06 15:13:53 +000096 this->init(seed);
97 if (!fTileSize.isEmpty()) {
98 this->stitch();
99 }
100
senorblancof3b50272014-06-16 10:49:58 -0700101#if SK_SUPPORT_GPU
commit-bot@chromium.orga3264e52014-05-30 13:26:10 +0000102 fPermutationsBitmap.setInfo(SkImageInfo::MakeA8(kBlockSize, 1));
commit-bot@chromium.orgfd5c9a62014-03-06 15:13:53 +0000103 fPermutationsBitmap.setPixels(fLatticeSelector);
104
commit-bot@chromium.orga3264e52014-05-30 13:26:10 +0000105 fNoiseBitmap.setInfo(SkImageInfo::MakeN32Premul(kBlockSize, 4));
commit-bot@chromium.orgfd5c9a62014-03-06 15:13:53 +0000106 fNoiseBitmap.setPixels(fNoise[0][0]);
107#endif
sugoi@google.come3b4c502013-04-05 13:47:09 +0000108 }
109
110 int fSeed;
111 uint8_t fLatticeSelector[kBlockSize];
112 uint16_t fNoise[4][kBlockSize][2];
113 SkPoint fGradient[4][kBlockSize];
114 SkISize fTileSize;
115 SkVector fBaseFrequency;
116 StitchData fStitchDataInit;
117
118private:
119
senorblancof3b50272014-06-16 10:49:58 -0700120#if SK_SUPPORT_GPU
commit-bot@chromium.orgfd5c9a62014-03-06 15:13:53 +0000121 SkBitmap fPermutationsBitmap;
122 SkBitmap fNoiseBitmap;
123#endif
sugoi@google.come3b4c502013-04-05 13:47:09 +0000124
125 inline int random() {
126 static const int gRandAmplitude = 16807; // 7**5; primitive root of m
127 static const int gRandQ = 127773; // m / a
128 static const int gRandR = 2836; // m % a
129
130 int result = gRandAmplitude * (fSeed % gRandQ) - gRandR * (fSeed / gRandQ);
131 if (result <= 0)
132 result += kRandMaximum;
133 fSeed = result;
134 return result;
135 }
136
commit-bot@chromium.orgfd5c9a62014-03-06 15:13:53 +0000137 // Only called once. Could be part of the constructor.
sugoi@google.come3b4c502013-04-05 13:47:09 +0000138 void init(SkScalar seed)
139 {
140 static const SkScalar gInvBlockSizef = SkScalarInvert(SkIntToScalar(kBlockSize));
141
senorblanco@chromium.org857e3202014-01-09 17:41:42 +0000142 // According to the SVG spec, we must truncate (not round) the seed value.
143 fSeed = SkScalarTruncToInt(seed);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000144 // The seed value clamp to the range [1, kRandMaximum - 1].
sugoi@google.come3b4c502013-04-05 13:47:09 +0000145 if (fSeed <= 0) {
146 fSeed = -(fSeed % (kRandMaximum - 1)) + 1;
147 }
148 if (fSeed > kRandMaximum - 1) {
149 fSeed = kRandMaximum - 1;
150 }
151 for (int channel = 0; channel < 4; ++channel) {
152 for (int i = 0; i < kBlockSize; ++i) {
153 fLatticeSelector[i] = i;
154 fNoise[channel][i][0] = (random() % (2 * kBlockSize));
155 fNoise[channel][i][1] = (random() % (2 * kBlockSize));
156 }
157 }
158 for (int i = kBlockSize - 1; i > 0; --i) {
159 int k = fLatticeSelector[i];
160 int j = random() % kBlockSize;
161 SkASSERT(j >= 0);
162 SkASSERT(j < kBlockSize);
163 fLatticeSelector[i] = fLatticeSelector[j];
164 fLatticeSelector[j] = k;
165 }
166
167 // Perform the permutations now
168 {
169 // Copy noise data
170 uint16_t noise[4][kBlockSize][2];
171 for (int i = 0; i < kBlockSize; ++i) {
172 for (int channel = 0; channel < 4; ++channel) {
173 for (int j = 0; j < 2; ++j) {
174 noise[channel][i][j] = fNoise[channel][i][j];
175 }
176 }
177 }
178 // Do permutations on noise data
179 for (int i = 0; i < kBlockSize; ++i) {
180 for (int channel = 0; channel < 4; ++channel) {
181 for (int j = 0; j < 2; ++j) {
182 fNoise[channel][i][j] = noise[channel][fLatticeSelector[i]][j];
183 }
184 }
185 }
186 }
187
188 // Half of the largest possible value for 16 bit unsigned int
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +0000189 static const SkScalar gHalfMax16bits = 32767.5f;
sugoi@google.come3b4c502013-04-05 13:47:09 +0000190
191 // Compute gradients from permutated noise data
192 for (int channel = 0; channel < 4; ++channel) {
193 for (int i = 0; i < kBlockSize; ++i) {
194 fGradient[channel][i] = SkPoint::Make(
195 SkScalarMul(SkIntToScalar(fNoise[channel][i][0] - kBlockSize),
196 gInvBlockSizef),
skia.committer@gmail.comcff02432013-04-06 07:01:10 +0000197 SkScalarMul(SkIntToScalar(fNoise[channel][i][1] - kBlockSize),
sugoi@google.come3b4c502013-04-05 13:47:09 +0000198 gInvBlockSizef));
199 fGradient[channel][i].normalize();
200 // Put the normalized gradient back into the noise data
201 fNoise[channel][i][0] = SkScalarRoundToInt(SkScalarMul(
sugoi@google.comd537af52013-06-10 13:59:25 +0000202 fGradient[channel][i].fX + SK_Scalar1, gHalfMax16bits));
sugoi@google.come3b4c502013-04-05 13:47:09 +0000203 fNoise[channel][i][1] = SkScalarRoundToInt(SkScalarMul(
sugoi@google.comd537af52013-06-10 13:59:25 +0000204 fGradient[channel][i].fY + SK_Scalar1, gHalfMax16bits));
sugoi@google.come3b4c502013-04-05 13:47:09 +0000205 }
206 }
sugoi@google.come3b4c502013-04-05 13:47:09 +0000207 }
208
commit-bot@chromium.orgfd5c9a62014-03-06 15:13:53 +0000209 // Only called once. Could be part of the constructor.
sugoi@google.come3b4c502013-04-05 13:47:09 +0000210 void stitch() {
211 SkScalar tileWidth = SkIntToScalar(fTileSize.width());
212 SkScalar tileHeight = SkIntToScalar(fTileSize.height());
213 SkASSERT(tileWidth > 0 && tileHeight > 0);
214 // When stitching tiled turbulence, the frequencies must be adjusted
215 // so that the tile borders will be continuous.
216 if (fBaseFrequency.fX) {
reed@google.com8015cdd2013-12-18 15:49:32 +0000217 SkScalar lowFrequencx =
218 SkScalarFloorToScalar(tileWidth * fBaseFrequency.fX) / tileWidth;
219 SkScalar highFrequencx =
220 SkScalarCeilToScalar(tileWidth * fBaseFrequency.fX) / tileWidth;
sugoi@google.come3b4c502013-04-05 13:47:09 +0000221 // BaseFrequency should be non-negative according to the standard.
skia.committer@gmail.comcff02432013-04-06 07:01:10 +0000222 if (SkScalarDiv(fBaseFrequency.fX, lowFrequencx) <
sugoi@google.come3b4c502013-04-05 13:47:09 +0000223 SkScalarDiv(highFrequencx, fBaseFrequency.fX)) {
224 fBaseFrequency.fX = lowFrequencx;
225 } else {
226 fBaseFrequency.fX = highFrequencx;
227 }
228 }
229 if (fBaseFrequency.fY) {
reed@google.com8015cdd2013-12-18 15:49:32 +0000230 SkScalar lowFrequency =
231 SkScalarFloorToScalar(tileHeight * fBaseFrequency.fY) / tileHeight;
232 SkScalar highFrequency =
233 SkScalarCeilToScalar(tileHeight * fBaseFrequency.fY) / tileHeight;
skia.committer@gmail.comcff02432013-04-06 07:01:10 +0000234 if (SkScalarDiv(fBaseFrequency.fY, lowFrequency) <
sugoi@google.come3b4c502013-04-05 13:47:09 +0000235 SkScalarDiv(highFrequency, fBaseFrequency.fY)) {
236 fBaseFrequency.fY = lowFrequency;
237 } else {
238 fBaseFrequency.fY = highFrequency;
239 }
240 }
241 // Set up TurbulenceInitial stitch values.
242 fStitchDataInit.fWidth =
reed@google.com8015cdd2013-12-18 15:49:32 +0000243 SkScalarRoundToInt(tileWidth * fBaseFrequency.fX);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000244 fStitchDataInit.fWrapX = kPerlinNoise + fStitchDataInit.fWidth;
245 fStitchDataInit.fHeight =
reed@google.com8015cdd2013-12-18 15:49:32 +0000246 SkScalarRoundToInt(tileHeight * fBaseFrequency.fY);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000247 fStitchDataInit.fWrapY = kPerlinNoise + fStitchDataInit.fHeight;
248 }
249
commit-bot@chromium.orgfd5c9a62014-03-06 15:13:53 +0000250public:
sugoi@google.come3b4c502013-04-05 13:47:09 +0000251
senorblancof3b50272014-06-16 10:49:58 -0700252#if SK_SUPPORT_GPU
commit-bot@chromium.orgfd5c9a62014-03-06 15:13:53 +0000253 const SkBitmap& getPermutationsBitmap() const { return fPermutationsBitmap; }
254
255 const SkBitmap& getNoiseBitmap() const { return fNoiseBitmap; }
256#endif
sugoi@google.come3b4c502013-04-05 13:47:09 +0000257};
258
259SkShader* SkPerlinNoiseShader::CreateFractalNoise(SkScalar baseFrequencyX, SkScalar baseFrequencyY,
260 int numOctaves, SkScalar seed,
261 const SkISize* tileSize) {
262 return SkNEW_ARGS(SkPerlinNoiseShader, (kFractalNoise_Type, baseFrequencyX, baseFrequencyY,
263 numOctaves, seed, tileSize));
264}
265
commit-bot@chromium.org9fbbcca2014-04-01 16:09:37 +0000266SkShader* SkPerlinNoiseShader::CreateTurbulence(SkScalar baseFrequencyX, SkScalar baseFrequencyY,
sugoi@google.come3b4c502013-04-05 13:47:09 +0000267 int numOctaves, SkScalar seed,
268 const SkISize* tileSize) {
269 return SkNEW_ARGS(SkPerlinNoiseShader, (kTurbulence_Type, baseFrequencyX, baseFrequencyY,
270 numOctaves, seed, tileSize));
271}
272
273SkPerlinNoiseShader::SkPerlinNoiseShader(SkPerlinNoiseShader::Type type,
274 SkScalar baseFrequencyX,
275 SkScalar baseFrequencyY,
276 int numOctaves,
277 SkScalar seed,
278 const SkISize* tileSize)
279 : fType(type)
280 , fBaseFrequencyX(baseFrequencyX)
281 , fBaseFrequencyY(baseFrequencyY)
commit-bot@chromium.orgce33d602013-11-25 21:46:31 +0000282 , fNumOctaves(numOctaves > 255 ? 255 : numOctaves/*[0,255] octaves allowed*/)
sugoi@google.come3b4c502013-04-05 13:47:09 +0000283 , fSeed(seed)
commit-bot@chromium.orgfd5c9a62014-03-06 15:13:53 +0000284 , fTileSize(NULL == tileSize ? SkISize::Make(0, 0) : *tileSize)
285 , fStitchTiles(!fTileSize.isEmpty())
sugoi@google.come3b4c502013-04-05 13:47:09 +0000286{
287 SkASSERT(numOctaves >= 0 && numOctaves < 256);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000288}
289
commit-bot@chromium.orgfd5c9a62014-03-06 15:13:53 +0000290SkPerlinNoiseShader::SkPerlinNoiseShader(SkReadBuffer& buffer)
291 : INHERITED(buffer)
292{
sugoi@google.come3b4c502013-04-05 13:47:09 +0000293 fType = (SkPerlinNoiseShader::Type) buffer.readInt();
294 fBaseFrequencyX = buffer.readScalar();
295 fBaseFrequencyY = buffer.readScalar();
296 fNumOctaves = buffer.readInt();
297 fSeed = buffer.readScalar();
298 fStitchTiles = buffer.readBool();
299 fTileSize.fWidth = buffer.readInt();
300 fTileSize.fHeight = buffer.readInt();
commit-bot@chromium.orgce33d602013-11-25 21:46:31 +0000301 buffer.validate(perlin_noise_type_is_valid(fType) &&
commit-bot@chromium.orgfd5c9a62014-03-06 15:13:53 +0000302 (fNumOctaves >= 0) && (fNumOctaves <= 255) &&
303 (fStitchTiles != fTileSize.isEmpty()));
sugoi@google.come3b4c502013-04-05 13:47:09 +0000304}
305
306SkPerlinNoiseShader::~SkPerlinNoiseShader() {
sugoi@google.come3b4c502013-04-05 13:47:09 +0000307}
308
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +0000309void SkPerlinNoiseShader::flatten(SkWriteBuffer& buffer) const {
sugoi@google.come3b4c502013-04-05 13:47:09 +0000310 this->INHERITED::flatten(buffer);
311 buffer.writeInt((int) fType);
312 buffer.writeScalar(fBaseFrequencyX);
313 buffer.writeScalar(fBaseFrequencyY);
314 buffer.writeInt(fNumOctaves);
315 buffer.writeScalar(fSeed);
316 buffer.writeBool(fStitchTiles);
317 buffer.writeInt(fTileSize.fWidth);
318 buffer.writeInt(fTileSize.fHeight);
319}
320
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000321SkScalar SkPerlinNoiseShader::PerlinNoiseShaderContext::noise2D(
senorblancoca6a7c22014-06-27 13:35:52 -0700322 int channel, const StitchData& stitchData, const SkPoint& noiseVector) const {
sugoi@google.come3b4c502013-04-05 13:47:09 +0000323 struct Noise {
324 int noisePositionIntegerValue;
senorblancoce6a3542014-06-12 11:24:19 -0700325 int nextNoisePositionIntegerValue;
sugoi@google.come3b4c502013-04-05 13:47:09 +0000326 SkScalar noisePositionFractionValue;
327 Noise(SkScalar component)
328 {
329 SkScalar position = component + kPerlinNoise;
330 noisePositionIntegerValue = SkScalarFloorToInt(position);
331 noisePositionFractionValue = position - SkIntToScalar(noisePositionIntegerValue);
senorblancoce6a3542014-06-12 11:24:19 -0700332 nextNoisePositionIntegerValue = noisePositionIntegerValue + 1;
sugoi@google.come3b4c502013-04-05 13:47:09 +0000333 }
334 };
335 Noise noiseX(noiseVector.x());
336 Noise noiseY(noiseVector.y());
337 SkScalar u, v;
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000338 const SkPerlinNoiseShader& perlinNoiseShader = static_cast<const SkPerlinNoiseShader&>(fShader);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000339 // If stitching, adjust lattice points accordingly.
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000340 if (perlinNoiseShader.fStitchTiles) {
sugoi@google.come3b4c502013-04-05 13:47:09 +0000341 noiseX.noisePositionIntegerValue =
342 checkNoise(noiseX.noisePositionIntegerValue, stitchData.fWrapX, stitchData.fWidth);
343 noiseY.noisePositionIntegerValue =
344 checkNoise(noiseY.noisePositionIntegerValue, stitchData.fWrapY, stitchData.fHeight);
senorblancoce6a3542014-06-12 11:24:19 -0700345 noiseX.nextNoisePositionIntegerValue =
346 checkNoise(noiseX.nextNoisePositionIntegerValue, stitchData.fWrapX, stitchData.fWidth);
347 noiseY.nextNoisePositionIntegerValue =
348 checkNoise(noiseY.nextNoisePositionIntegerValue, stitchData.fWrapY, stitchData.fHeight);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000349 }
350 noiseX.noisePositionIntegerValue &= kBlockMask;
351 noiseY.noisePositionIntegerValue &= kBlockMask;
senorblancoce6a3542014-06-12 11:24:19 -0700352 noiseX.nextNoisePositionIntegerValue &= kBlockMask;
353 noiseY.nextNoisePositionIntegerValue &= kBlockMask;
354 int i =
senorblancoca6a7c22014-06-27 13:35:52 -0700355 fPaintingData->fLatticeSelector[noiseX.noisePositionIntegerValue];
senorblancoce6a3542014-06-12 11:24:19 -0700356 int j =
senorblancoca6a7c22014-06-27 13:35:52 -0700357 fPaintingData->fLatticeSelector[noiseX.nextNoisePositionIntegerValue];
senorblancoce6a3542014-06-12 11:24:19 -0700358 int b00 = (i + noiseY.noisePositionIntegerValue) & kBlockMask;
359 int b10 = (j + noiseY.noisePositionIntegerValue) & kBlockMask;
360 int b01 = (i + noiseY.nextNoisePositionIntegerValue) & kBlockMask;
361 int b11 = (j + noiseY.nextNoisePositionIntegerValue) & kBlockMask;
sugoi@google.come3b4c502013-04-05 13:47:09 +0000362 SkScalar sx = smoothCurve(noiseX.noisePositionFractionValue);
363 SkScalar sy = smoothCurve(noiseY.noisePositionFractionValue);
364 // This is taken 1:1 from SVG spec: http://www.w3.org/TR/SVG11/filters.html#feTurbulenceElement
365 SkPoint fractionValue = SkPoint::Make(noiseX.noisePositionFractionValue,
366 noiseY.noisePositionFractionValue); // Offset (0,0)
senorblancoca6a7c22014-06-27 13:35:52 -0700367 u = fPaintingData->fGradient[channel][b00].dot(fractionValue);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000368 fractionValue.fX -= SK_Scalar1; // Offset (-1,0)
senorblancoca6a7c22014-06-27 13:35:52 -0700369 v = fPaintingData->fGradient[channel][b10].dot(fractionValue);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000370 SkScalar a = SkScalarInterp(u, v, sx);
371 fractionValue.fY -= SK_Scalar1; // Offset (-1,-1)
senorblancoca6a7c22014-06-27 13:35:52 -0700372 v = fPaintingData->fGradient[channel][b11].dot(fractionValue);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000373 fractionValue.fX = noiseX.noisePositionFractionValue; // Offset (0,-1)
senorblancoca6a7c22014-06-27 13:35:52 -0700374 u = fPaintingData->fGradient[channel][b01].dot(fractionValue);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000375 SkScalar b = SkScalarInterp(u, v, sx);
376 return SkScalarInterp(a, b, sy);
377}
378
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000379SkScalar SkPerlinNoiseShader::PerlinNoiseShaderContext::calculateTurbulenceValueForPoint(
senorblancoca6a7c22014-06-27 13:35:52 -0700380 int channel, StitchData& stitchData, const SkPoint& point) const {
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000381 const SkPerlinNoiseShader& perlinNoiseShader = static_cast<const SkPerlinNoiseShader&>(fShader);
382 if (perlinNoiseShader.fStitchTiles) {
sugoi@google.come3b4c502013-04-05 13:47:09 +0000383 // Set up TurbulenceInitial stitch values.
senorblancoca6a7c22014-06-27 13:35:52 -0700384 stitchData = fPaintingData->fStitchDataInit;
sugoi@google.come3b4c502013-04-05 13:47:09 +0000385 }
386 SkScalar turbulenceFunctionResult = 0;
senorblancoca6a7c22014-06-27 13:35:52 -0700387 SkPoint noiseVector(SkPoint::Make(SkScalarMul(point.x(), fPaintingData->fBaseFrequency.fX),
388 SkScalarMul(point.y(), fPaintingData->fBaseFrequency.fY)));
sugoi@google.come3b4c502013-04-05 13:47:09 +0000389 SkScalar ratio = SK_Scalar1;
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000390 for (int octave = 0; octave < perlinNoiseShader.fNumOctaves; ++octave) {
senorblancoca6a7c22014-06-27 13:35:52 -0700391 SkScalar noise = noise2D(channel, stitchData, noiseVector);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000392 turbulenceFunctionResult += SkScalarDiv(
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000393 (perlinNoiseShader.fType == kFractalNoise_Type) ? noise : SkScalarAbs(noise), ratio);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000394 noiseVector.fX *= 2;
395 noiseVector.fY *= 2;
396 ratio *= 2;
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000397 if (perlinNoiseShader.fStitchTiles) {
sugoi@google.come3b4c502013-04-05 13:47:09 +0000398 // Update stitch values
399 stitchData.fWidth *= 2;
400 stitchData.fWrapX = stitchData.fWidth + kPerlinNoise;
401 stitchData.fHeight *= 2;
402 stitchData.fWrapY = stitchData.fHeight + kPerlinNoise;
403 }
404 }
405
406 // The value of turbulenceFunctionResult comes from ((turbulenceFunctionResult) + 1) / 2
407 // by fractalNoise and (turbulenceFunctionResult) by turbulence.
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000408 if (perlinNoiseShader.fType == kFractalNoise_Type) {
sugoi@google.come3b4c502013-04-05 13:47:09 +0000409 turbulenceFunctionResult =
410 SkScalarMul(turbulenceFunctionResult, SK_ScalarHalf) + SK_ScalarHalf;
411 }
412
413 if (channel == 3) { // Scale alpha by paint value
414 turbulenceFunctionResult = SkScalarMul(turbulenceFunctionResult,
415 SkScalarDiv(SkIntToScalar(getPaintAlpha()), SkIntToScalar(255)));
416 }
417
418 // Clamp result
419 return SkScalarPin(turbulenceFunctionResult, 0, SK_Scalar1);
420}
421
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000422SkPMColor SkPerlinNoiseShader::PerlinNoiseShaderContext::shade(
423 const SkPoint& point, StitchData& stitchData) const {
sugoi@google.come3b4c502013-04-05 13:47:09 +0000424 SkPoint newPoint;
senorblanco@chromium.orga8d95f82014-04-04 14:46:10 +0000425 fMatrix.mapPoints(&newPoint, &point, 1);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000426 newPoint.fX = SkScalarRoundToScalar(newPoint.fX);
427 newPoint.fY = SkScalarRoundToScalar(newPoint.fY);
428
429 U8CPU rgba[4];
430 for (int channel = 3; channel >= 0; --channel) {
431 rgba[channel] = SkScalarFloorToInt(255 *
senorblancoca6a7c22014-06-27 13:35:52 -0700432 calculateTurbulenceValueForPoint(channel, stitchData, newPoint));
sugoi@google.come3b4c502013-04-05 13:47:09 +0000433 }
434 return SkPreMultiplyARGB(rgba[3], rgba[0], rgba[1], rgba[2]);
435}
436
commit-bot@chromium.orgce56d962014-05-05 18:39:18 +0000437SkShader::Context* SkPerlinNoiseShader::onCreateContext(const ContextRec& rec,
438 void* storage) const {
commit-bot@chromium.orge901b6d2014-05-01 19:31:31 +0000439 return SkNEW_PLACEMENT_ARGS(storage, PerlinNoiseShaderContext, (*this, rec));
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000440}
441
442size_t SkPerlinNoiseShader::contextSize() const {
443 return sizeof(PerlinNoiseShaderContext);
444}
445
446SkPerlinNoiseShader::PerlinNoiseShaderContext::PerlinNoiseShaderContext(
commit-bot@chromium.orge901b6d2014-05-01 19:31:31 +0000447 const SkPerlinNoiseShader& shader, const ContextRec& rec)
448 : INHERITED(shader, rec)
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000449{
commit-bot@chromium.orge901b6d2014-05-01 19:31:31 +0000450 SkMatrix newMatrix = *rec.fMatrix;
reed@google.comb67b8e62014-05-12 18:12:24 +0000451 newMatrix.preConcat(shader.getLocalMatrix());
452 if (rec.fLocalMatrix) {
453 newMatrix.preConcat(*rec.fLocalMatrix);
454 }
senorblanco@chromium.orga8d95f82014-04-04 14:46:10 +0000455 // This (1,1) translation is due to WebKit's 1 based coordinates for the noise
456 // (as opposed to 0 based, usually). The same adjustment is in the setData() function.
senorblancoca6a7c22014-06-27 13:35:52 -0700457 fMatrix.setTranslate(-newMatrix.getTranslateX() + SK_Scalar1, -newMatrix.getTranslateY() + SK_Scalar1);
458 fPaintingData = SkNEW_ARGS(PaintingData, (shader.fTileSize, shader.fSeed, shader.fBaseFrequencyX, shader.fBaseFrequencyY, newMatrix));
459}
460
461SkPerlinNoiseShader::PerlinNoiseShaderContext::~PerlinNoiseShaderContext() {
462 SkDELETE(fPaintingData);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000463}
464
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000465void SkPerlinNoiseShader::PerlinNoiseShaderContext::shadeSpan(
466 int x, int y, SkPMColor result[], int count) {
sugoi@google.come3b4c502013-04-05 13:47:09 +0000467 SkPoint point = SkPoint::Make(SkIntToScalar(x), SkIntToScalar(y));
468 StitchData stitchData;
469 for (int i = 0; i < count; ++i) {
470 result[i] = shade(point, stitchData);
471 point.fX += SK_Scalar1;
472 }
473}
474
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000475void SkPerlinNoiseShader::PerlinNoiseShaderContext::shadeSpan16(
476 int x, int y, uint16_t result[], int count) {
sugoi@google.come3b4c502013-04-05 13:47:09 +0000477 SkPoint point = SkPoint::Make(SkIntToScalar(x), SkIntToScalar(y));
478 StitchData stitchData;
479 DITHER_565_SCAN(y);
480 for (int i = 0; i < count; ++i) {
481 unsigned dither = DITHER_VALUE(x);
482 result[i] = SkDitherRGB32To565(shade(point, stitchData), dither);
483 DITHER_INC_X(x);
484 point.fX += SK_Scalar1;
485 }
486}
487
488/////////////////////////////////////////////////////////////////////
489
commit-bot@chromium.org344cf452013-06-17 14:19:01 +0000490#if SK_SUPPORT_GPU
sugoi@google.come3b4c502013-04-05 13:47:09 +0000491
492#include "GrTBackendEffectFactory.h"
493
senorblancof3b50272014-06-16 10:49:58 -0700494class GrGLPerlinNoise : public GrGLEffect {
sugoi@google.com4775cba2013-04-17 13:46:56 +0000495public:
sugoi@google.come3b4c502013-04-05 13:47:09 +0000496 GrGLPerlinNoise(const GrBackendEffectFactory& factory,
senorblancof3b50272014-06-16 10:49:58 -0700497 const GrDrawEffect& drawEffect);
sugoi@google.com4775cba2013-04-17 13:46:56 +0000498 virtual ~GrGLPerlinNoise() {}
sugoi@google.come3b4c502013-04-05 13:47:09 +0000499
500 virtual void emitCode(GrGLShaderBuilder*,
501 const GrDrawEffect&,
502 EffectKey,
503 const char* outputColor,
504 const char* inputColor,
bsalomon@google.com77af6802013-10-02 13:04:56 +0000505 const TransformedCoordsArray&,
sugoi@google.come3b4c502013-04-05 13:47:09 +0000506 const TextureSamplerArray&) SK_OVERRIDE;
507
sugoi@google.com4775cba2013-04-17 13:46:56 +0000508 virtual void setData(const GrGLUniformManager&, const GrDrawEffect&) SK_OVERRIDE;
sugoi@google.come3b4c502013-04-05 13:47:09 +0000509
senorblancof3b50272014-06-16 10:49:58 -0700510 static inline EffectKey GenKey(const GrDrawEffect&, const GrGLCaps&);
sugoi@google.com4775cba2013-04-17 13:46:56 +0000511
512private:
sugoi@google.com4775cba2013-04-17 13:46:56 +0000513
senorblancof3b50272014-06-16 10:49:58 -0700514 GrGLUniformManager::UniformHandle fStitchDataUni;
515 SkPerlinNoiseShader::Type fType;
516 bool fStitchTiles;
517 int fNumOctaves;
518 GrGLUniformManager::UniformHandle fBaseFrequencyUni;
519 GrGLUniformManager::UniformHandle fAlphaUni;
senorblancof3b50272014-06-16 10:49:58 -0700520
521private:
522 typedef GrGLEffect INHERITED;
sugoi@google.come3b4c502013-04-05 13:47:09 +0000523};
524
525/////////////////////////////////////////////////////////////////////
526
senorblancof3b50272014-06-16 10:49:58 -0700527class GrPerlinNoiseEffect : public GrEffect {
sugoi@google.come3b4c502013-04-05 13:47:09 +0000528public:
bsalomon83d081a2014-07-08 09:56:10 -0700529 static GrEffect* Create(SkPerlinNoiseShader::Type type,
530 int numOctaves, bool stitchTiles,
531 SkPerlinNoiseShader::PaintingData* paintingData,
532 GrTexture* permutationsTexture, GrTexture* noiseTexture,
533 const SkMatrix& matrix, uint8_t alpha) {
bsalomon55fad7a2014-07-08 07:34:20 -0700534 return SkNEW_ARGS(GrPerlinNoiseEffect, (type, numOctaves, stitchTiles, paintingData,
535 permutationsTexture, noiseTexture, matrix, alpha));
sugoi@google.come3b4c502013-04-05 13:47:09 +0000536 }
537
senorblancoca6a7c22014-06-27 13:35:52 -0700538 virtual ~GrPerlinNoiseEffect() {
539 SkDELETE(fPaintingData);
540 }
sugoi@google.come3b4c502013-04-05 13:47:09 +0000541
542 static const char* Name() { return "PerlinNoise"; }
543 virtual const GrBackendEffectFactory& getFactory() const SK_OVERRIDE {
544 return GrTBackendEffectFactory<GrPerlinNoiseEffect>::getInstance();
545 }
senorblancoca6a7c22014-06-27 13:35:52 -0700546 const SkPerlinNoiseShader::StitchData& stitchData() const { return fPaintingData->fStitchDataInit; }
sugoi@google.come3b4c502013-04-05 13:47:09 +0000547
senorblancof3b50272014-06-16 10:49:58 -0700548 SkPerlinNoiseShader::Type type() const { return fType; }
549 bool stitchTiles() const { return fStitchTiles; }
senorblancoca6a7c22014-06-27 13:35:52 -0700550 const SkVector& baseFrequency() const { return fPaintingData->fBaseFrequency; }
senorblancof3b50272014-06-16 10:49:58 -0700551 int numOctaves() const { return fNumOctaves; }
552 const SkMatrix& matrix() const { return fCoordTransform.getMatrix(); }
553 uint8_t alpha() const { return fAlpha; }
554
sugoi@google.come3b4c502013-04-05 13:47:09 +0000555 typedef GrGLPerlinNoise GLEffect;
556
sugoi@google.come3b4c502013-04-05 13:47:09 +0000557private:
558 virtual bool onIsEqual(const GrEffect& sBase) const SK_OVERRIDE {
559 const GrPerlinNoiseEffect& s = CastEffect<GrPerlinNoiseEffect>(sBase);
senorblancof3b50272014-06-16 10:49:58 -0700560 return fType == s.fType &&
senorblancoca6a7c22014-06-27 13:35:52 -0700561 fPaintingData->fBaseFrequency == s.fPaintingData->fBaseFrequency &&
senorblancof3b50272014-06-16 10:49:58 -0700562 fNumOctaves == s.fNumOctaves &&
563 fStitchTiles == s.fStitchTiles &&
564 fCoordTransform.getMatrix() == s.fCoordTransform.getMatrix() &&
565 fAlpha == s.fAlpha &&
sugoi@google.com4775cba2013-04-17 13:46:56 +0000566 fPermutationsAccess.getTexture() == s.fPermutationsAccess.getTexture() &&
sugoi@google.come3b4c502013-04-05 13:47:09 +0000567 fNoiseAccess.getTexture() == s.fNoiseAccess.getTexture() &&
senorblancoca6a7c22014-06-27 13:35:52 -0700568 fPaintingData->fStitchDataInit == s.fPaintingData->fStitchDataInit;
sugoi@google.come3b4c502013-04-05 13:47:09 +0000569 }
570
senorblancoca6a7c22014-06-27 13:35:52 -0700571 GrPerlinNoiseEffect(SkPerlinNoiseShader::Type type,
sugoi@google.come3b4c502013-04-05 13:47:09 +0000572 int numOctaves, bool stitchTiles,
senorblancoca6a7c22014-06-27 13:35:52 -0700573 SkPerlinNoiseShader::PaintingData* paintingData,
sugoi@google.come3b4c502013-04-05 13:47:09 +0000574 GrTexture* permutationsTexture, GrTexture* noiseTexture,
575 const SkMatrix& matrix, uint8_t alpha)
senorblancof3b50272014-06-16 10:49:58 -0700576 : fType(type)
senorblancof3b50272014-06-16 10:49:58 -0700577 , fNumOctaves(numOctaves)
578 , fStitchTiles(stitchTiles)
579 , fAlpha(alpha)
sugoi@google.com4775cba2013-04-17 13:46:56 +0000580 , fPermutationsAccess(permutationsTexture)
sugoi@google.come3b4c502013-04-05 13:47:09 +0000581 , fNoiseAccess(noiseTexture)
senorblancoca6a7c22014-06-27 13:35:52 -0700582 , fPaintingData(paintingData) {
sugoi@google.come3b4c502013-04-05 13:47:09 +0000583 this->addTextureAccess(&fPermutationsAccess);
584 this->addTextureAccess(&fNoiseAccess);
senorblancoca6a7c22014-06-27 13:35:52 -0700585 fCoordTransform.reset(kLocal_GrCoordSet, matrix);
senorblancof3b50272014-06-16 10:49:58 -0700586 this->addCoordTransform(&fCoordTransform);
587 this->setWillNotUseInputColor();
sugoi@google.come3b4c502013-04-05 13:47:09 +0000588 }
589
sugoi@google.com4775cba2013-04-17 13:46:56 +0000590 GR_DECLARE_EFFECT_TEST;
sugoi@google.come3b4c502013-04-05 13:47:09 +0000591
senorblancof3b50272014-06-16 10:49:58 -0700592 SkPerlinNoiseShader::Type fType;
593 GrCoordTransform fCoordTransform;
senorblancof3b50272014-06-16 10:49:58 -0700594 int fNumOctaves;
595 bool fStitchTiles;
596 uint8_t fAlpha;
sugoi@google.come3b4c502013-04-05 13:47:09 +0000597 GrTextureAccess fPermutationsAccess;
598 GrTextureAccess fNoiseAccess;
senorblancoca6a7c22014-06-27 13:35:52 -0700599 SkPerlinNoiseShader::PaintingData *fPaintingData;
sugoi@google.come3b4c502013-04-05 13:47:09 +0000600
senorblancof3b50272014-06-16 10:49:58 -0700601 void getConstantColorComponents(GrColor*, uint32_t* validFlags) const SK_OVERRIDE {
602 *validFlags = 0; // This is noise. Nothing is constant.
sugoi@google.com4775cba2013-04-17 13:46:56 +0000603 }
604
sugoi@google.com4775cba2013-04-17 13:46:56 +0000605private:
senorblancof3b50272014-06-16 10:49:58 -0700606 typedef GrEffect INHERITED;
sugoi@google.come3b4c502013-04-05 13:47:09 +0000607};
608
609/////////////////////////////////////////////////////////////////////
sugoi@google.come3b4c502013-04-05 13:47:09 +0000610GR_DEFINE_EFFECT_TEST(GrPerlinNoiseEffect);
611
bsalomon83d081a2014-07-08 09:56:10 -0700612GrEffect* GrPerlinNoiseEffect::TestCreate(SkRandom* random,
613 GrContext* context,
614 const GrDrawTargetCaps&,
615 GrTexture**) {
sugoi@google.com423ac132013-04-18 14:04:57 +0000616 int numOctaves = random->nextRangeU(2, 10);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000617 bool stitchTiles = random->nextBool();
618 SkScalar seed = SkIntToScalar(random->nextU());
619 SkISize tileSize = SkISize::Make(random->nextRangeU(4, 4096), random->nextRangeU(4, 4096));
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +0000620 SkScalar baseFrequencyX = random->nextRangeScalar(0.01f,
621 0.99f);
622 SkScalar baseFrequencyY = random->nextRangeScalar(0.01f,
623 0.99f);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000624
625 SkShader* shader = random->nextBool() ?
626 SkPerlinNoiseShader::CreateFractalNoise(baseFrequencyX, baseFrequencyY, numOctaves, seed,
627 stitchTiles ? &tileSize : NULL) :
commit-bot@chromium.org9fbbcca2014-04-01 16:09:37 +0000628 SkPerlinNoiseShader::CreateTurbulence(baseFrequencyX, baseFrequencyY, numOctaves, seed,
sugoi@google.come3b4c502013-04-05 13:47:09 +0000629 stitchTiles ? &tileSize : NULL);
630
631 SkPaint paint;
bsalomon83d081a2014-07-08 09:56:10 -0700632 GrColor paintColor;
633 GrEffect* effect;
634 SkAssertResult(shader->asNewEffect(context, paint, NULL, &paintColor, &effect));
sugoi@google.come3b4c502013-04-05 13:47:09 +0000635
636 SkDELETE(shader);
637
638 return effect;
639}
sugoi@google.com4775cba2013-04-17 13:46:56 +0000640
senorblancof3b50272014-06-16 10:49:58 -0700641GrGLPerlinNoise::GrGLPerlinNoise(const GrBackendEffectFactory& factory, const GrDrawEffect& drawEffect)
642 : INHERITED (factory)
643 , fType(drawEffect.castEffect<GrPerlinNoiseEffect>().type())
644 , fStitchTiles(drawEffect.castEffect<GrPerlinNoiseEffect>().stitchTiles())
645 , fNumOctaves(drawEffect.castEffect<GrPerlinNoiseEffect>().numOctaves()) {
sugoi@google.com4775cba2013-04-17 13:46:56 +0000646}
647
sugoi@google.come3b4c502013-04-05 13:47:09 +0000648void GrGLPerlinNoise::emitCode(GrGLShaderBuilder* builder,
649 const GrDrawEffect&,
650 EffectKey key,
651 const char* outputColor,
652 const char* inputColor,
bsalomon@google.com77af6802013-10-02 13:04:56 +0000653 const TransformedCoordsArray& coords,
sugoi@google.come3b4c502013-04-05 13:47:09 +0000654 const TextureSamplerArray& samplers) {
655 sk_ignore_unused_variable(inputColor);
656
bsalomon@google.com77af6802013-10-02 13:04:56 +0000657 SkString vCoords = builder->ensureFSCoords2D(coords, 0);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000658
commit-bot@chromium.org74a3a212013-08-30 19:43:59 +0000659 fBaseFrequencyUni = builder->addUniform(GrGLShaderBuilder::kFragment_Visibility,
sugoi@google.come3b4c502013-04-05 13:47:09 +0000660 kVec2f_GrSLType, "baseFrequency");
661 const char* baseFrequencyUni = builder->getUniformCStr(fBaseFrequencyUni);
commit-bot@chromium.org74a3a212013-08-30 19:43:59 +0000662 fAlphaUni = builder->addUniform(GrGLShaderBuilder::kFragment_Visibility,
sugoi@google.come3b4c502013-04-05 13:47:09 +0000663 kFloat_GrSLType, "alpha");
664 const char* alphaUni = builder->getUniformCStr(fAlphaUni);
665
666 const char* stitchDataUni = NULL;
667 if (fStitchTiles) {
commit-bot@chromium.org74a3a212013-08-30 19:43:59 +0000668 fStitchDataUni = builder->addUniform(GrGLShaderBuilder::kFragment_Visibility,
commit-bot@chromium.org98393202013-07-04 18:13:05 +0000669 kVec2f_GrSLType, "stitchData");
sugoi@google.come3b4c502013-04-05 13:47:09 +0000670 stitchDataUni = builder->getUniformCStr(fStitchDataUni);
671 }
672
sugoi@google.comd537af52013-06-10 13:59:25 +0000673 // There are 4 lines, so the center of each line is 1/8, 3/8, 5/8 and 7/8
674 const char* chanCoordR = "0.125";
675 const char* chanCoordG = "0.375";
676 const char* chanCoordB = "0.625";
677 const char* chanCoordA = "0.875";
678 const char* chanCoord = "chanCoord";
sugoi@google.come3b4c502013-04-05 13:47:09 +0000679 const char* stitchData = "stitchData";
680 const char* ratio = "ratio";
sugoi@google.come3b4c502013-04-05 13:47:09 +0000681 const char* noiseVec = "noiseVec";
sugoi@google.come3b4c502013-04-05 13:47:09 +0000682 const char* noiseSmooth = "noiseSmooth";
senorblancoce6a3542014-06-12 11:24:19 -0700683 const char* floorVal = "floorVal";
sugoi@google.come3b4c502013-04-05 13:47:09 +0000684 const char* fractVal = "fractVal";
685 const char* uv = "uv";
686 const char* ab = "ab";
687 const char* latticeIdx = "latticeIdx";
senorblancoce6a3542014-06-12 11:24:19 -0700688 const char* bcoords = "bcoords";
sugoi@google.come3b4c502013-04-05 13:47:09 +0000689 const char* lattice = "lattice";
sugoi@google.come3b4c502013-04-05 13:47:09 +0000690 const char* inc8bit = "0.00390625"; // 1.0 / 256.0
691 // This is the math to convert the two 16bit integer packed into rgba 8 bit input into a
692 // [-1,1] vector and perform a dot product between that vector and the provided vector.
693 const char* dotLattice = "dot(((%s.ga + %s.rb * vec2(%s)) * vec2(2.0) - vec2(1.0)), %s);";
694
sugoi@google.comd537af52013-06-10 13:59:25 +0000695 // Add noise function
696 static const GrGLShaderVar gPerlinNoiseArgs[] = {
697 GrGLShaderVar(chanCoord, kFloat_GrSLType),
commit-bot@chromium.org98393202013-07-04 18:13:05 +0000698 GrGLShaderVar(noiseVec, kVec2f_GrSLType)
sugoi@google.comd537af52013-06-10 13:59:25 +0000699 };
sugoi@google.come3b4c502013-04-05 13:47:09 +0000700
sugoi@google.comd537af52013-06-10 13:59:25 +0000701 static const GrGLShaderVar gPerlinNoiseStitchArgs[] = {
702 GrGLShaderVar(chanCoord, kFloat_GrSLType),
commit-bot@chromium.org98393202013-07-04 18:13:05 +0000703 GrGLShaderVar(noiseVec, kVec2f_GrSLType),
704 GrGLShaderVar(stitchData, kVec2f_GrSLType)
sugoi@google.comd537af52013-06-10 13:59:25 +0000705 };
sugoi@google.come3b4c502013-04-05 13:47:09 +0000706
sugoi@google.comd537af52013-06-10 13:59:25 +0000707 SkString noiseCode;
sugoi@google.come3b4c502013-04-05 13:47:09 +0000708
senorblancoce6a3542014-06-12 11:24:19 -0700709 noiseCode.appendf("\tvec4 %s;\n", floorVal);
710 noiseCode.appendf("\t%s.xy = floor(%s);\n", floorVal, noiseVec);
711 noiseCode.appendf("\t%s.zw = %s.xy + vec2(1.0);\n", floorVal, floorVal);
712 noiseCode.appendf("\tvec2 %s = fract(%s);\n", fractVal, noiseVec);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000713
714 // smooth curve : t * t * (3 - 2 * t)
senorblancoce6a3542014-06-12 11:24:19 -0700715 noiseCode.appendf("\n\tvec2 %s = %s * %s * (vec2(3.0) - vec2(2.0) * %s);",
716 noiseSmooth, fractVal, fractVal, fractVal);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000717
718 // Adjust frequencies if we're stitching tiles
719 if (fStitchTiles) {
commit-bot@chromium.org98393202013-07-04 18:13:05 +0000720 noiseCode.appendf("\n\tif(%s.x >= %s.x) { %s.x -= %s.x; }",
senorblancoce6a3542014-06-12 11:24:19 -0700721 floorVal, stitchData, floorVal, stitchData);
commit-bot@chromium.org98393202013-07-04 18:13:05 +0000722 noiseCode.appendf("\n\tif(%s.y >= %s.y) { %s.y -= %s.y; }",
senorblancoce6a3542014-06-12 11:24:19 -0700723 floorVal, stitchData, floorVal, stitchData);
724 noiseCode.appendf("\n\tif(%s.z >= %s.x) { %s.z -= %s.x; }",
725 floorVal, stitchData, floorVal, stitchData);
726 noiseCode.appendf("\n\tif(%s.w >= %s.y) { %s.w -= %s.y; }",
727 floorVal, stitchData, floorVal, stitchData);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000728 }
729
730 // Get texture coordinates and normalize
senorblancoce6a3542014-06-12 11:24:19 -0700731 noiseCode.appendf("\n\t%s = fract(floor(mod(%s, 256.0)) / vec4(256.0));\n",
732 floorVal, floorVal);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000733
734 // Get permutation for x
735 {
736 SkString xCoords("");
senorblancoce6a3542014-06-12 11:24:19 -0700737 xCoords.appendf("vec2(%s.x, 0.5)", floorVal);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000738
sugoi@google.comd537af52013-06-10 13:59:25 +0000739 noiseCode.appendf("\n\tvec2 %s;\n\t%s.x = ", latticeIdx, latticeIdx);
740 builder->appendTextureLookup(&noiseCode, samplers[0], xCoords.c_str(), kVec2f_GrSLType);
741 noiseCode.append(".r;");
sugoi@google.come3b4c502013-04-05 13:47:09 +0000742 }
743
744 // Get permutation for x + 1
745 {
746 SkString xCoords("");
senorblancoce6a3542014-06-12 11:24:19 -0700747 xCoords.appendf("vec2(%s.z, 0.5)", floorVal);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000748
sugoi@google.comd537af52013-06-10 13:59:25 +0000749 noiseCode.appendf("\n\t%s.y = ", latticeIdx);
750 builder->appendTextureLookup(&noiseCode, samplers[0], xCoords.c_str(), kVec2f_GrSLType);
751 noiseCode.append(".r;");
sugoi@google.come3b4c502013-04-05 13:47:09 +0000752 }
753
commit-bot@chromium.org344cf452013-06-17 14:19:01 +0000754#if defined(SK_BUILD_FOR_ANDROID)
755 // Android rounding for Tegra devices, like, for example: Xoom (Tegra 2), Nexus 7 (Tegra 3).
756 // The issue is that colors aren't accurate enough on Tegra devices. For example, if an 8 bit
757 // value of 124 (or 0.486275 here) is entered, we can get a texture value of 123.513725
758 // (or 0.484368 here). The following rounding operation prevents these precision issues from
759 // affecting the result of the noise by making sure that we only have multiples of 1/255.
760 // (Note that 1/255 is about 0.003921569, which is the value used here).
761 noiseCode.appendf("\n\t%s = floor(%s * vec2(255.0) + vec2(0.5)) * vec2(0.003921569);",
762 latticeIdx, latticeIdx);
763#endif
764
sugoi@google.come3b4c502013-04-05 13:47:09 +0000765 // Get (x,y) coordinates with the permutated x
senorblancoce6a3542014-06-12 11:24:19 -0700766 noiseCode.appendf("\n\tvec4 %s = fract(%s.xyxy + %s.yyww);", bcoords, latticeIdx, floorVal);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000767
sugoi@google.comd537af52013-06-10 13:59:25 +0000768 noiseCode.appendf("\n\n\tvec2 %s;", uv);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000769 // Compute u, at offset (0,0)
770 {
771 SkString latticeCoords("");
senorblancoce6a3542014-06-12 11:24:19 -0700772 latticeCoords.appendf("vec2(%s.x, %s)", bcoords, chanCoord);
sugoi@google.comd537af52013-06-10 13:59:25 +0000773 noiseCode.appendf("\n\tvec4 %s = ", lattice);
774 builder->appendTextureLookup(&noiseCode, samplers[1], latticeCoords.c_str(),
775 kVec2f_GrSLType);
776 noiseCode.appendf(".bgra;\n\t%s.x = ", uv);
777 noiseCode.appendf(dotLattice, lattice, lattice, inc8bit, fractVal);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000778 }
779
sugoi@google.comd537af52013-06-10 13:59:25 +0000780 noiseCode.appendf("\n\t%s.x -= 1.0;", fractVal);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000781 // Compute v, at offset (-1,0)
782 {
783 SkString latticeCoords("");
senorblancoce6a3542014-06-12 11:24:19 -0700784 latticeCoords.appendf("vec2(%s.y, %s)", bcoords, chanCoord);
commit-bot@chromium.org344cf452013-06-17 14:19:01 +0000785 noiseCode.append("\n\tlattice = ");
sugoi@google.comd537af52013-06-10 13:59:25 +0000786 builder->appendTextureLookup(&noiseCode, samplers[1], latticeCoords.c_str(),
787 kVec2f_GrSLType);
788 noiseCode.appendf(".bgra;\n\t%s.y = ", uv);
789 noiseCode.appendf(dotLattice, lattice, lattice, inc8bit, fractVal);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000790 }
791
792 // Compute 'a' as a linear interpolation of 'u' and 'v'
sugoi@google.comd537af52013-06-10 13:59:25 +0000793 noiseCode.appendf("\n\tvec2 %s;", ab);
794 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 +0000795
sugoi@google.comd537af52013-06-10 13:59:25 +0000796 noiseCode.appendf("\n\t%s.y -= 1.0;", fractVal);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000797 // Compute v, at offset (-1,-1)
798 {
799 SkString latticeCoords("");
senorblancoce6a3542014-06-12 11:24:19 -0700800 latticeCoords.appendf("vec2(%s.w, %s)", bcoords, chanCoord);
commit-bot@chromium.org344cf452013-06-17 14:19:01 +0000801 noiseCode.append("\n\tlattice = ");
sugoi@google.comd537af52013-06-10 13:59:25 +0000802 builder->appendTextureLookup(&noiseCode, samplers[1], latticeCoords.c_str(),
803 kVec2f_GrSLType);
804 noiseCode.appendf(".bgra;\n\t%s.y = ", uv);
805 noiseCode.appendf(dotLattice, lattice, lattice, inc8bit, fractVal);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000806 }
807
sugoi@google.comd537af52013-06-10 13:59:25 +0000808 noiseCode.appendf("\n\t%s.x += 1.0;", fractVal);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000809 // Compute u, at offset (0,-1)
810 {
811 SkString latticeCoords("");
senorblancoce6a3542014-06-12 11:24:19 -0700812 latticeCoords.appendf("vec2(%s.z, %s)", bcoords, chanCoord);
commit-bot@chromium.org344cf452013-06-17 14:19:01 +0000813 noiseCode.append("\n\tlattice = ");
sugoi@google.comd537af52013-06-10 13:59:25 +0000814 builder->appendTextureLookup(&noiseCode, samplers[1], latticeCoords.c_str(),
815 kVec2f_GrSLType);
816 noiseCode.appendf(".bgra;\n\t%s.x = ", uv);
817 noiseCode.appendf(dotLattice, lattice, lattice, inc8bit, fractVal);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000818 }
819
820 // Compute 'b' as a linear interpolation of 'u' and 'v'
sugoi@google.comd537af52013-06-10 13:59:25 +0000821 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 +0000822 // Compute the noise as a linear interpolation of 'a' and 'b'
sugoi@google.comd537af52013-06-10 13:59:25 +0000823 noiseCode.appendf("\n\treturn mix(%s.x, %s.y, %s.y);\n", ab, ab, noiseSmooth);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000824
sugoi@google.comd537af52013-06-10 13:59:25 +0000825 SkString noiseFuncName;
826 if (fStitchTiles) {
commit-bot@chromium.org74a3a212013-08-30 19:43:59 +0000827 builder->fsEmitFunction(kFloat_GrSLType,
828 "perlinnoise", SK_ARRAY_COUNT(gPerlinNoiseStitchArgs),
829 gPerlinNoiseStitchArgs, noiseCode.c_str(), &noiseFuncName);
sugoi@google.comd537af52013-06-10 13:59:25 +0000830 } else {
commit-bot@chromium.org74a3a212013-08-30 19:43:59 +0000831 builder->fsEmitFunction(kFloat_GrSLType,
832 "perlinnoise", SK_ARRAY_COUNT(gPerlinNoiseArgs),
833 gPerlinNoiseArgs, noiseCode.c_str(), &noiseFuncName);
sugoi@google.comd537af52013-06-10 13:59:25 +0000834 }
sugoi@google.come3b4c502013-04-05 13:47:09 +0000835
sugoi@google.comd537af52013-06-10 13:59:25 +0000836 // There are rounding errors if the floor operation is not performed here
senorblancoca6a7c22014-06-27 13:35:52 -0700837 builder->fsCodeAppendf("\n\t\tvec2 %s = floor(%s.xy) * %s;",
838 noiseVec, vCoords.c_str(), baseFrequencyUni);
sugoi@google.comd537af52013-06-10 13:59:25 +0000839
840 // Clear the color accumulator
841 builder->fsCodeAppendf("\n\t\t%s = vec4(0.0);", outputColor);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000842
843 if (fStitchTiles) {
sugoi@google.comd537af52013-06-10 13:59:25 +0000844 // Set up TurbulenceInitial stitch values.
commit-bot@chromium.org98393202013-07-04 18:13:05 +0000845 builder->fsCodeAppendf("\n\t\tvec2 %s = %s;", stitchData, stitchDataUni);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000846 }
sugoi@google.come3b4c502013-04-05 13:47:09 +0000847
sugoi@google.comd537af52013-06-10 13:59:25 +0000848 builder->fsCodeAppendf("\n\t\tfloat %s = 1.0;", ratio);
849
850 // Loop over all octaves
851 builder->fsCodeAppendf("\n\t\tfor (int octave = 0; octave < %d; ++octave) {", fNumOctaves);
852
853 builder->fsCodeAppendf("\n\t\t\t%s += ", outputColor);
854 if (fType != SkPerlinNoiseShader::kFractalNoise_Type) {
855 builder->fsCodeAppend("abs(");
856 }
857 if (fStitchTiles) {
858 builder->fsCodeAppendf(
859 "vec4(\n\t\t\t\t%s(%s, %s, %s),\n\t\t\t\t%s(%s, %s, %s),"
860 "\n\t\t\t\t%s(%s, %s, %s),\n\t\t\t\t%s(%s, %s, %s))",
861 noiseFuncName.c_str(), chanCoordR, noiseVec, stitchData,
862 noiseFuncName.c_str(), chanCoordG, noiseVec, stitchData,
863 noiseFuncName.c_str(), chanCoordB, noiseVec, stitchData,
864 noiseFuncName.c_str(), chanCoordA, noiseVec, stitchData);
865 } else {
866 builder->fsCodeAppendf(
867 "vec4(\n\t\t\t\t%s(%s, %s),\n\t\t\t\t%s(%s, %s),"
868 "\n\t\t\t\t%s(%s, %s),\n\t\t\t\t%s(%s, %s))",
869 noiseFuncName.c_str(), chanCoordR, noiseVec,
870 noiseFuncName.c_str(), chanCoordG, noiseVec,
871 noiseFuncName.c_str(), chanCoordB, noiseVec,
872 noiseFuncName.c_str(), chanCoordA, noiseVec);
873 }
874 if (fType != SkPerlinNoiseShader::kFractalNoise_Type) {
875 builder->fsCodeAppendf(")"); // end of "abs("
876 }
877 builder->fsCodeAppendf(" * %s;", ratio);
878
879 builder->fsCodeAppendf("\n\t\t\t%s *= vec2(2.0);", noiseVec);
880 builder->fsCodeAppendf("\n\t\t\t%s *= 0.5;", ratio);
881
882 if (fStitchTiles) {
commit-bot@chromium.org98393202013-07-04 18:13:05 +0000883 builder->fsCodeAppendf("\n\t\t\t%s *= vec2(2.0);", stitchData);
sugoi@google.comd537af52013-06-10 13:59:25 +0000884 }
885 builder->fsCodeAppend("\n\t\t}"); // end of the for loop on octaves
sugoi@google.come3b4c502013-04-05 13:47:09 +0000886
887 if (fType == SkPerlinNoiseShader::kFractalNoise_Type) {
888 // The value of turbulenceFunctionResult comes from ((turbulenceFunctionResult) + 1) / 2
889 // by fractalNoise and (turbulenceFunctionResult) by turbulence.
sugoi@google.comd537af52013-06-10 13:59:25 +0000890 builder->fsCodeAppendf("\n\t\t%s = %s * vec4(0.5) + vec4(0.5);", outputColor, outputColor);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000891 }
892
sugoi@google.comd537af52013-06-10 13:59:25 +0000893 builder->fsCodeAppendf("\n\t\t%s.a *= %s;", outputColor, alphaUni);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000894
895 // Clamp values
sugoi@google.comd537af52013-06-10 13:59:25 +0000896 builder->fsCodeAppendf("\n\t\t%s = clamp(%s, 0.0, 1.0);", outputColor, outputColor);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000897
898 // Pre-multiply the result
sugoi@google.comd537af52013-06-10 13:59:25 +0000899 builder->fsCodeAppendf("\n\t\t%s = vec4(%s.rgb * %s.aaa, %s.a);\n",
sugoi@google.come3b4c502013-04-05 13:47:09 +0000900 outputColor, outputColor, outputColor, outputColor);
901}
902
senorblancof3b50272014-06-16 10:49:58 -0700903GrGLEffect::EffectKey GrGLPerlinNoise::GenKey(const GrDrawEffect& drawEffect, const GrGLCaps&) {
sugoi@google.come3b4c502013-04-05 13:47:09 +0000904 const GrPerlinNoiseEffect& turbulence = drawEffect.castEffect<GrPerlinNoiseEffect>();
905
906 EffectKey key = turbulence.numOctaves();
907
908 key = key << 3; // Make room for next 3 bits
909
910 switch (turbulence.type()) {
911 case SkPerlinNoiseShader::kFractalNoise_Type:
912 key |= 0x1;
913 break;
914 case SkPerlinNoiseShader::kTurbulence_Type:
915 key |= 0x2;
916 break;
917 default:
918 // leave key at 0
919 break;
920 }
921
922 if (turbulence.stitchTiles()) {
923 key |= 0x4; // Flip the 3rd bit if tile stitching is on
924 }
925
bsalomon@google.com77af6802013-10-02 13:04:56 +0000926 return key;
sugoi@google.come3b4c502013-04-05 13:47:09 +0000927}
928
senorblancof3b50272014-06-16 10:49:58 -0700929void GrGLPerlinNoise::setData(const GrGLUniformManager& uman, const GrDrawEffect& drawEffect) {
930 INHERITED::setData(uman, drawEffect);
931
sugoi@google.come3b4c502013-04-05 13:47:09 +0000932 const GrPerlinNoiseEffect& turbulence = drawEffect.castEffect<GrPerlinNoiseEffect>();
933
934 const SkVector& baseFrequency = turbulence.baseFrequency();
935 uman.set2f(fBaseFrequencyUni, baseFrequency.fX, baseFrequency.fY);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000936 uman.set1f(fAlphaUni, SkScalarDiv(SkIntToScalar(turbulence.alpha()), SkIntToScalar(255)));
937
sugoi@google.com4775cba2013-04-17 13:46:56 +0000938 if (turbulence.stitchTiles()) {
939 const SkPerlinNoiseShader::StitchData& stitchData = turbulence.stitchData();
commit-bot@chromium.org98393202013-07-04 18:13:05 +0000940 uman.set2f(fStitchDataUni, SkIntToScalar(stitchData.fWidth),
941 SkIntToScalar(stitchData.fHeight));
sugoi@google.com4775cba2013-04-17 13:46:56 +0000942 }
943}
944
sugoi@google.come3b4c502013-04-05 13:47:09 +0000945/////////////////////////////////////////////////////////////////////
946
dandov9de5b512014-06-10 14:38:28 -0700947bool SkPerlinNoiseShader::asNewEffect(GrContext* context, const SkPaint& paint,
bsalomon83d081a2014-07-08 09:56:10 -0700948 const SkMatrix* externalLocalMatrix, GrColor* paintColor,
949 GrEffect** effect) const {
sugoi@google.come3b4c502013-04-05 13:47:09 +0000950 SkASSERT(NULL != context);
dandov9de5b512014-06-10 14:38:28 -0700951
bsalomon83d081a2014-07-08 09:56:10 -0700952 *paintColor = SkColor2GrColorJustAlpha(paint.getColor());
senorblancoca6a7c22014-06-27 13:35:52 -0700953
commit-bot@chromium.org96fb7482014-05-09 20:28:11 +0000954 SkMatrix localMatrix = this->getLocalMatrix();
955 if (externalLocalMatrix) {
956 localMatrix.preConcat(*externalLocalMatrix);
957 }
958
senorblancoca6a7c22014-06-27 13:35:52 -0700959 SkMatrix matrix = context->getMatrix();
960 matrix.preConcat(localMatrix);
961
commit-bot@chromium.orgc2a0ea62013-11-06 10:08:38 +0000962 if (0 == fNumOctaves) {
963 SkColor clearColor = 0;
964 if (kFractalNoise_Type == fType) {
965 clearColor = SkColorSetARGB(paint.getAlpha() / 2, 127, 127, 127);
966 }
967 SkAutoTUnref<SkColorFilter> cf(SkColorFilter::CreateModeFilter(
968 clearColor, SkXfermode::kSrc_Mode));
bsalomon83d081a2014-07-08 09:56:10 -0700969 *effect = cf->asNewEffect(context);
dandov9de5b512014-06-10 14:38:28 -0700970 return true;
commit-bot@chromium.orgc2a0ea62013-11-06 10:08:38 +0000971 }
972
sugoi@google.come3b4c502013-04-05 13:47:09 +0000973 // Either we don't stitch tiles, either we have a valid tile size
974 SkASSERT(!fStitchTiles || !fTileSize.isEmpty());
975
senorblancoca6a7c22014-06-27 13:35:52 -0700976 SkPerlinNoiseShader::PaintingData* paintingData = SkNEW_ARGS(PaintingData, (fTileSize, fSeed, fBaseFrequencyX, fBaseFrequencyY, matrix));
sugoi@google.come3b4c502013-04-05 13:47:09 +0000977 GrTexture* permutationsTexture = GrLockAndRefCachedBitmapTexture(
senorblancoca6a7c22014-06-27 13:35:52 -0700978 context, paintingData->getPermutationsBitmap(), NULL);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000979 GrTexture* noiseTexture = GrLockAndRefCachedBitmapTexture(
senorblancoca6a7c22014-06-27 13:35:52 -0700980 context, paintingData->getNoiseBitmap(), NULL);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000981
senorblancoca6a7c22014-06-27 13:35:52 -0700982 SkMatrix m = context->getMatrix();
983 m.setTranslateX(-localMatrix.getTranslateX() + SK_Scalar1);
984 m.setTranslateY(-localMatrix.getTranslateY() + SK_Scalar1);
985 if ((NULL != permutationsTexture) && (NULL != noiseTexture)) {
bsalomon83d081a2014-07-08 09:56:10 -0700986 *effect = GrPerlinNoiseEffect::Create(fType,
senorblancoca6a7c22014-06-27 13:35:52 -0700987 fNumOctaves,
988 fStitchTiles,
989 paintingData,
990 permutationsTexture, noiseTexture,
991 m, paint.getAlpha());
992 } else {
993 SkDELETE(paintingData);
bsalomon83d081a2014-07-08 09:56:10 -0700994 *effect = NULL;
senorblancoca6a7c22014-06-27 13:35:52 -0700995 }
sugoi@google.come3b4c502013-04-05 13:47:09 +0000996
997 // Unlock immediately, this is not great, but we don't have a way of
998 // knowing when else to unlock it currently. TODO: Remove this when
999 // unref becomes the unlock replacement for all types of textures.
1000 if (NULL != permutationsTexture) {
1001 GrUnlockAndUnrefCachedBitmapTexture(permutationsTexture);
1002 }
1003 if (NULL != noiseTexture) {
1004 GrUnlockAndUnrefCachedBitmapTexture(noiseTexture);
1005 }
1006
dandov9de5b512014-06-10 14:38:28 -07001007 return true;
sugoi@google.come3b4c502013-04-05 13:47:09 +00001008}
1009
1010#else
1011
dandov9de5b512014-06-10 14:38:28 -07001012bool SkPerlinNoiseShader::asNewEffect(GrContext* context, const SkPaint& paint,
bsalomon83d081a2014-07-08 09:56:10 -07001013 const SkMatrix* externalLocalMatrix, GrColor* paintColor,
1014 GrEffect** effect) const {
sugoi@google.come3b4c502013-04-05 13:47:09 +00001015 SkDEBUGFAIL("Should not call in GPU-less build");
dandov9de5b512014-06-10 14:38:28 -07001016 return false;
sugoi@google.come3b4c502013-04-05 13:47:09 +00001017}
1018
1019#endif
1020
commit-bot@chromium.org0f10f7b2014-03-13 18:02:17 +00001021#ifndef SK_IGNORE_TO_STRING
sugoi@google.come3b4c502013-04-05 13:47:09 +00001022void SkPerlinNoiseShader::toString(SkString* str) const {
1023 str->append("SkPerlinNoiseShader: (");
1024
1025 str->append("type: ");
1026 switch (fType) {
1027 case kFractalNoise_Type:
1028 str->append("\"fractal noise\"");
1029 break;
1030 case kTurbulence_Type:
1031 str->append("\"turbulence\"");
1032 break;
1033 default:
1034 str->append("\"unknown\"");
1035 break;
1036 }
1037 str->append(" base frequency: (");
1038 str->appendScalar(fBaseFrequencyX);
1039 str->append(", ");
1040 str->appendScalar(fBaseFrequencyY);
1041 str->append(") number of octaves: ");
1042 str->appendS32(fNumOctaves);
1043 str->append(" seed: ");
1044 str->appendScalar(fSeed);
1045 str->append(" stitch tiles: ");
1046 str->append(fStitchTiles ? "true " : "false ");
1047
1048 this->INHERITED::toString(str);
1049
1050 str->append(")");
1051}
1052#endif