• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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 "include/effects/SkPerlinNoiseShader.h"
9 
10 #include "include/core/SkBitmap.h"
11 #include "include/core/SkColorFilter.h"
12 #include "include/core/SkShader.h"
13 #include "include/core/SkString.h"
14 #include "include/core/SkUnPreMultiply.h"
15 #include "include/private/SkTPin.h"
16 #include "src/core/SkArenaAlloc.h"
17 #include "src/core/SkMatrixProvider.h"
18 #include "src/core/SkReadBuffer.h"
19 #include "src/core/SkVM.h"
20 #include "src/core/SkWriteBuffer.h"
21 
22 #if SK_SUPPORT_GPU
23 #include "include/gpu/GrRecordingContext.h"
24 #include "src/gpu/GrFragmentProcessor.h"
25 #include "src/gpu/GrRecordingContextPriv.h"
26 #include "src/gpu/SkGr.h"
27 #include "src/gpu/effects/GrMatrixEffect.h"
28 #include "src/gpu/effects/GrTextureEffect.h"
29 #include "src/gpu/glsl/GrGLSLFragmentShaderBuilder.h"
30 #include "src/gpu/glsl/GrGLSLProgramDataManager.h"
31 #include "src/gpu/glsl/GrGLSLUniformHandler.h"
32 #endif
33 
34 static const int kBlockSize = 256;
35 static const int kBlockMask = kBlockSize - 1;
36 static const int kPerlinNoise = 4096;
37 static const int kRandMaximum = SK_MaxS32; // 2**31 - 1
38 
39 class SkPerlinNoiseShaderImpl : public SkShaderBase {
40 public:
41     struct StitchData {
StitchDataSkPerlinNoiseShaderImpl::StitchData42         StitchData()
43           : fWidth(0)
44           , fWrapX(0)
45           , fHeight(0)
46           , fWrapY(0)
47         {}
48 
StitchDataSkPerlinNoiseShaderImpl::StitchData49         StitchData(SkScalar w, SkScalar h)
50           : fWidth(std::min(SkScalarRoundToInt(w), SK_MaxS32 - kPerlinNoise))
51           , fWrapX(kPerlinNoise + fWidth)
52           , fHeight(std::min(SkScalarRoundToInt(h), SK_MaxS32 - kPerlinNoise))
53           , fWrapY(kPerlinNoise + fHeight) {}
54 
operator ==SkPerlinNoiseShaderImpl::StitchData55         bool operator==(const StitchData& other) const {
56             return fWidth == other.fWidth &&
57                    fWrapX == other.fWrapX &&
58                    fHeight == other.fHeight &&
59                    fWrapY == other.fWrapY;
60         }
61 
62         int fWidth; // How much to subtract to wrap for stitching.
63         int fWrapX; // Minimum value to wrap.
64         int fHeight;
65         int fWrapY;
66     };
67 
68     struct PaintingData {
PaintingDataSkPerlinNoiseShaderImpl::PaintingData69         PaintingData(const SkISize& tileSize, SkScalar seed,
70                      SkScalar baseFrequencyX, SkScalar baseFrequencyY,
71                      const SkMatrix& matrix)
72         {
73             SkVector tileVec;
74             matrix.mapVector(SkIntToScalar(tileSize.fWidth), SkIntToScalar(tileSize.fHeight),
75                              &tileVec);
76 
77             SkSize scale;
78             if (!matrix.decomposeScale(&scale, nullptr)) {
79                 scale.set(SK_ScalarNearlyZero, SK_ScalarNearlyZero);
80             }
81             fBaseFrequency.set(baseFrequencyX * SkScalarInvert(scale.width()),
82                                baseFrequencyY * SkScalarInvert(scale.height()));
83             fTileSize.set(SkScalarRoundToInt(tileVec.fX), SkScalarRoundToInt(tileVec.fY));
84             this->init(seed);
85             if (!fTileSize.isEmpty()) {
86                 this->stitch();
87             }
88 
89     #if SK_SUPPORT_GPU
90             SkImageInfo info = SkImageInfo::MakeA8(kBlockSize, 1);
91             fPermutationsBitmap.installPixels(info, fLatticeSelector, info.minRowBytes());
92             fPermutationsBitmap.setImmutable();
93 
94             info = SkImageInfo::Make(kBlockSize, 4, kRGBA_8888_SkColorType, kPremul_SkAlphaType);
95             fNoiseBitmap.installPixels(info, fNoise[0][0], info.minRowBytes());
96             fNoiseBitmap.setImmutable();
97     #endif
98         }
99 
100     #if SK_SUPPORT_GPU
PaintingDataSkPerlinNoiseShaderImpl::PaintingData101         PaintingData(const PaintingData& that)
102                 : fSeed(that.fSeed)
103                 , fTileSize(that.fTileSize)
104                 , fBaseFrequency(that.fBaseFrequency)
105                 , fStitchDataInit(that.fStitchDataInit)
106                 , fPermutationsBitmap(that.fPermutationsBitmap)
107                 , fNoiseBitmap(that.fNoiseBitmap) {
108             memcpy(fLatticeSelector, that.fLatticeSelector, sizeof(fLatticeSelector));
109             memcpy(fNoise, that.fNoise, sizeof(fNoise));
110             memcpy(fGradient, that.fGradient, sizeof(fGradient));
111         }
112     #endif
113 
114         int         fSeed;
115         uint8_t     fLatticeSelector[kBlockSize];
116         uint16_t    fNoise[4][kBlockSize][2];
117         SkPoint     fGradient[4][kBlockSize];
118         SkISize     fTileSize;
119         SkVector    fBaseFrequency;
120         StitchData  fStitchDataInit;
121 
122     private:
123 
124     #if SK_SUPPORT_GPU
125         SkBitmap fPermutationsBitmap;
126         SkBitmap fNoiseBitmap;
127     #endif
128 
randomSkPerlinNoiseShaderImpl::PaintingData129         inline int random()  {
130             // See https://www.w3.org/TR/SVG11/filters.html#feTurbulenceElement
131             // m = kRandMaximum, 2**31 - 1 (2147483647)
132             static constexpr int kRandAmplitude = 16807; // 7**5; primitive root of m
133             static constexpr int kRandQ = 127773; // m / a
134             static constexpr int kRandR = 2836; // m % a
135 
136             int result = kRandAmplitude * (fSeed % kRandQ) - kRandR * (fSeed / kRandQ);
137             if (result <= 0) {
138                 result += kRandMaximum;
139             }
140             fSeed = result;
141             return result;
142         }
143 
144         // Only called once. Could be part of the constructor.
initSkPerlinNoiseShaderImpl::PaintingData145         void init(SkScalar seed)
146         {
147             // According to the SVG spec, we must truncate (not round) the seed value.
148             fSeed = SkScalarTruncToInt(seed);
149             // The seed value clamp to the range [1, kRandMaximum - 1].
150             if (fSeed <= 0) {
151                 fSeed = -(fSeed % (kRandMaximum - 1)) + 1;
152             }
153             if (fSeed > kRandMaximum - 1) {
154                 fSeed = kRandMaximum - 1;
155             }
156             for (int channel = 0; channel < 4; ++channel) {
157                 for (int i = 0; i < kBlockSize; ++i) {
158                     fLatticeSelector[i] = i;
159                     fNoise[channel][i][0] = (random() % (2 * kBlockSize));
160                     fNoise[channel][i][1] = (random() % (2 * kBlockSize));
161                 }
162             }
163             for (int i = kBlockSize - 1; i > 0; --i) {
164                 int k = fLatticeSelector[i];
165                 int j = random() % kBlockSize;
166                 SkASSERT(j >= 0);
167                 SkASSERT(j < kBlockSize);
168                 fLatticeSelector[i] = fLatticeSelector[j];
169                 fLatticeSelector[j] = k;
170             }
171 
172             // Perform the permutations now
173             {
174                 // Copy noise data
175                 uint16_t noise[4][kBlockSize][2];
176                 for (int i = 0; i < kBlockSize; ++i) {
177                     for (int channel = 0; channel < 4; ++channel) {
178                         for (int j = 0; j < 2; ++j) {
179                             noise[channel][i][j] = fNoise[channel][i][j];
180                         }
181                     }
182                 }
183                 // Do permutations on noise data
184                 for (int i = 0; i < kBlockSize; ++i) {
185                     for (int channel = 0; channel < 4; ++channel) {
186                         for (int j = 0; j < 2; ++j) {
187                             fNoise[channel][i][j] = noise[channel][fLatticeSelector[i]][j];
188                         }
189                     }
190                 }
191             }
192 
193             // Half of the largest possible value for 16 bit unsigned int
194             static constexpr SkScalar kHalfMax16bits = 32767.5f;
195 
196             // Compute gradients from permutated noise data
197             static constexpr SkScalar kInvBlockSizef = 1.0 / SkIntToScalar(kBlockSize);
198             for (int channel = 0; channel < 4; ++channel) {
199                 for (int i = 0; i < kBlockSize; ++i) {
200                     fGradient[channel][i] = SkPoint::Make(
201                         (fNoise[channel][i][0] - kBlockSize) * kInvBlockSizef,
202                         (fNoise[channel][i][1] - kBlockSize) * kInvBlockSizef);
203                     fGradient[channel][i].normalize();
204                     // Put the normalized gradient back into the noise data
205                     fNoise[channel][i][0] =
206                             SkScalarRoundToInt((fGradient[channel][i].fX + 1) * kHalfMax16bits);
207                     fNoise[channel][i][1] =
208                             SkScalarRoundToInt((fGradient[channel][i].fY + 1) * kHalfMax16bits);
209                 }
210             }
211         }
212 
213         // Only called once. Could be part of the constructor.
stitchSkPerlinNoiseShaderImpl::PaintingData214         void stitch() {
215             SkScalar tileWidth  = SkIntToScalar(fTileSize.width());
216             SkScalar tileHeight = SkIntToScalar(fTileSize.height());
217             SkASSERT(tileWidth > 0 && tileHeight > 0);
218             // When stitching tiled turbulence, the frequencies must be adjusted
219             // so that the tile borders will be continuous.
220             if (fBaseFrequency.fX) {
221                 SkScalar lowFrequencx =
222                     SkScalarFloorToScalar(tileWidth * fBaseFrequency.fX) / tileWidth;
223                 SkScalar highFrequencx =
224                     SkScalarCeilToScalar(tileWidth * fBaseFrequency.fX) / tileWidth;
225                 // BaseFrequency should be non-negative according to the standard.
226                 // lowFrequencx can be 0 if fBaseFrequency.fX is very small.
227                 if (sk_ieee_float_divide(fBaseFrequency.fX, lowFrequencx) < highFrequencx / fBaseFrequency.fX) {
228                     fBaseFrequency.fX = lowFrequencx;
229                 } else {
230                     fBaseFrequency.fX = highFrequencx;
231                 }
232             }
233             if (fBaseFrequency.fY) {
234                 SkScalar lowFrequency =
235                     SkScalarFloorToScalar(tileHeight * fBaseFrequency.fY) / tileHeight;
236                 SkScalar highFrequency =
237                     SkScalarCeilToScalar(tileHeight * fBaseFrequency.fY) / tileHeight;
238                 // lowFrequency can be 0 if fBaseFrequency.fY is very small.
239                 if (sk_ieee_float_divide(fBaseFrequency.fY, lowFrequency) < highFrequency / fBaseFrequency.fY) {
240                     fBaseFrequency.fY = lowFrequency;
241                 } else {
242                     fBaseFrequency.fY = highFrequency;
243                 }
244             }
245             // Set up TurbulenceInitial stitch values.
246             fStitchDataInit = StitchData(tileWidth * fBaseFrequency.fX,
247                                          tileHeight * fBaseFrequency.fY);
248         }
249 
250     public:
251 
252 #if SK_SUPPORT_GPU
getPermutationsBitmapSkPerlinNoiseShaderImpl::PaintingData253         const SkBitmap& getPermutationsBitmap() const { return fPermutationsBitmap; }
254 
getNoiseBitmapSkPerlinNoiseShaderImpl::PaintingData255         const SkBitmap& getNoiseBitmap() const { return fNoiseBitmap; }
256 #endif
257     };
258 
259     /**
260      *  About the noise types : the difference between the first 2 is just minor tweaks to the
261      *  algorithm, they're not 2 entirely different noises. The output looks different, but once the
262      *  noise is generated in the [1, -1] range, the output is brought back in the [0, 1] range by
263      *  doing :
264      *  kFractalNoise_Type : noise * 0.5 + 0.5
265      *  kTurbulence_Type   : abs(noise)
266      *  Very little differences between the 2 types, although you can tell the difference visually.
267      */
268     enum Type {
269         kFractalNoise_Type,
270         kTurbulence_Type,
271         kLast_Type = kTurbulence_Type
272     };
273 
274     static const int kMaxOctaves = 255; // numOctaves must be <= 0 and <= kMaxOctaves
275 
276     SkPerlinNoiseShaderImpl(SkPerlinNoiseShaderImpl::Type type, SkScalar baseFrequencyX,
277                       SkScalar baseFrequencyY, int numOctaves, SkScalar seed,
278                       const SkISize* tileSize);
279 
280     class PerlinNoiseShaderContext : public Context {
281     public:
282         PerlinNoiseShaderContext(const SkPerlinNoiseShaderImpl& shader, const ContextRec&);
283 
284         void shadeSpan(int x, int y, SkPMColor[], int count) override;
285 
286     private:
287         SkPMColor shade(const SkPoint& point, StitchData& stitchData) const;
288         SkScalar calculateTurbulenceValueForPoint(
289                                                   int channel,
290                                                   StitchData& stitchData, const SkPoint& point) const;
291         SkScalar noise2D(int channel,
292                          const StitchData& stitchData, const SkPoint& noiseVector) const;
293 
294         SkMatrix     fMatrix;
295         PaintingData fPaintingData;
296 
297         using INHERITED = Context;
298     };
299 
300 #if SK_SUPPORT_GPU
301     std::unique_ptr<GrFragmentProcessor> asFragmentProcessor(const GrFPArgs&) const override;
302 #endif
303 
onProgram(skvm::Builder *,skvm::Coord,skvm::Coord,skvm::Color,const SkMatrixProvider &,const SkMatrix *,const SkColorInfo &,skvm::Uniforms *,SkArenaAlloc *) const304     skvm::Color onProgram(skvm::Builder*,
305                           skvm::Coord, skvm::Coord, skvm::Color,
306                           const SkMatrixProvider&, const SkMatrix*, const SkColorInfo&,
307                           skvm::Uniforms*, SkArenaAlloc*) const override {
308         // TODO?
309         return {};
310     }
311 
312 protected:
313     void flatten(SkWriteBuffer&) const override;
314 #ifdef SK_ENABLE_LEGACY_SHADERCONTEXT
315     Context* onMakeContext(const ContextRec&, SkArenaAlloc*) const override;
316 #endif
317 
318 private:
319     SK_FLATTENABLE_HOOKS(SkPerlinNoiseShaderImpl)
320 
321     const SkPerlinNoiseShaderImpl::Type fType;
322     const SkScalar                  fBaseFrequencyX;
323     const SkScalar                  fBaseFrequencyY;
324     const int                       fNumOctaves;
325     const SkScalar                  fSeed;
326     const SkISize                   fTileSize;
327     const bool                      fStitchTiles;
328 
329     friend class ::SkPerlinNoiseShader;
330 
331     using INHERITED = SkShaderBase;
332 };
333 
334 namespace {
335 
336 // noiseValue is the color component's value (or color)
337 // limitValue is the maximum perlin noise array index value allowed
338 // newValue is the current noise dimension (either width or height)
checkNoise(int noiseValue,int limitValue,int newValue)339 inline int checkNoise(int noiseValue, int limitValue, int newValue) {
340     // If the noise value would bring us out of bounds of the current noise array while we are
341     // stiching noise tiles together, wrap the noise around the current dimension of the noise to
342     // stay within the array bounds in a continuous fashion (so that tiling lines are not visible)
343     if (noiseValue >= limitValue) {
344         noiseValue -= newValue;
345     }
346     return noiseValue;
347 }
348 
smoothCurve(SkScalar t)349 inline SkScalar smoothCurve(SkScalar t) {
350     return t * t * (3 - 2 * t);
351 }
352 
353 } // end namespace
354 
SkPerlinNoiseShaderImpl(SkPerlinNoiseShaderImpl::Type type,SkScalar baseFrequencyX,SkScalar baseFrequencyY,int numOctaves,SkScalar seed,const SkISize * tileSize)355 SkPerlinNoiseShaderImpl::SkPerlinNoiseShaderImpl(SkPerlinNoiseShaderImpl::Type type,
356                                                  SkScalar baseFrequencyX,
357                                                  SkScalar baseFrequencyY,
358                                                  int numOctaves,
359                                                  SkScalar seed,
360                                                  const SkISize* tileSize)
361   : fType(type)
362   , fBaseFrequencyX(baseFrequencyX)
363   , fBaseFrequencyY(baseFrequencyY)
364   , fNumOctaves(numOctaves > kMaxOctaves ? kMaxOctaves : numOctaves/*[0,255] octaves allowed*/)
365   , fSeed(seed)
366   , fTileSize(nullptr == tileSize ? SkISize::Make(0, 0) : *tileSize)
367   , fStitchTiles(!fTileSize.isEmpty())
368 {
369     SkASSERT(numOctaves >= 0 && numOctaves <= kMaxOctaves);
370     SkASSERT(fBaseFrequencyX >= 0);
371     SkASSERT(fBaseFrequencyY >= 0);
372 }
373 
CreateProc(SkReadBuffer & buffer)374 sk_sp<SkFlattenable> SkPerlinNoiseShaderImpl::CreateProc(SkReadBuffer& buffer) {
375     Type type = buffer.read32LE(kLast_Type);
376 
377     SkScalar freqX = buffer.readScalar();
378     SkScalar freqY = buffer.readScalar();
379     int octaves = buffer.read32LE<int>(kMaxOctaves);
380 
381     SkScalar seed = buffer.readScalar();
382     SkISize tileSize;
383     tileSize.fWidth = buffer.readInt();
384     tileSize.fHeight = buffer.readInt();
385 
386     switch (type) {
387         case kFractalNoise_Type:
388             return SkPerlinNoiseShader::MakeFractalNoise(freqX, freqY, octaves, seed, &tileSize);
389         case kTurbulence_Type:
390             return SkPerlinNoiseShader::MakeTurbulence(freqX, freqY, octaves, seed, &tileSize);
391         default:
392             // Really shouldn't get here b.c. of earlier check on type
393             buffer.validate(false);
394             return nullptr;
395     }
396 }
397 
flatten(SkWriteBuffer & buffer) const398 void SkPerlinNoiseShaderImpl::flatten(SkWriteBuffer& buffer) const {
399     buffer.writeInt((int) fType);
400     buffer.writeScalar(fBaseFrequencyX);
401     buffer.writeScalar(fBaseFrequencyY);
402     buffer.writeInt(fNumOctaves);
403     buffer.writeScalar(fSeed);
404     buffer.writeInt(fTileSize.fWidth);
405     buffer.writeInt(fTileSize.fHeight);
406 }
407 
noise2D(int channel,const StitchData & stitchData,const SkPoint & noiseVector) const408 SkScalar SkPerlinNoiseShaderImpl::PerlinNoiseShaderContext::noise2D(
409         int channel, const StitchData& stitchData, const SkPoint& noiseVector) const {
410     struct Noise {
411         int noisePositionIntegerValue;
412         int nextNoisePositionIntegerValue;
413         SkScalar noisePositionFractionValue;
414         Noise(SkScalar component)
415         {
416             SkScalar position = component + kPerlinNoise;
417             noisePositionIntegerValue = SkScalarFloorToInt(position);
418             noisePositionFractionValue = position - SkIntToScalar(noisePositionIntegerValue);
419             nextNoisePositionIntegerValue = noisePositionIntegerValue + 1;
420         }
421     };
422     Noise noiseX(noiseVector.x());
423     Noise noiseY(noiseVector.y());
424     SkScalar u, v;
425     const SkPerlinNoiseShaderImpl& perlinNoiseShader = static_cast<const SkPerlinNoiseShaderImpl&>(fShader);
426     // If stitching, adjust lattice points accordingly.
427     if (perlinNoiseShader.fStitchTiles) {
428         noiseX.noisePositionIntegerValue =
429             checkNoise(noiseX.noisePositionIntegerValue, stitchData.fWrapX, stitchData.fWidth);
430         noiseY.noisePositionIntegerValue =
431             checkNoise(noiseY.noisePositionIntegerValue, stitchData.fWrapY, stitchData.fHeight);
432         noiseX.nextNoisePositionIntegerValue =
433             checkNoise(noiseX.nextNoisePositionIntegerValue, stitchData.fWrapX, stitchData.fWidth);
434         noiseY.nextNoisePositionIntegerValue =
435             checkNoise(noiseY.nextNoisePositionIntegerValue, stitchData.fWrapY, stitchData.fHeight);
436     }
437     noiseX.noisePositionIntegerValue &= kBlockMask;
438     noiseY.noisePositionIntegerValue &= kBlockMask;
439     noiseX.nextNoisePositionIntegerValue &= kBlockMask;
440     noiseY.nextNoisePositionIntegerValue &= kBlockMask;
441     int i = fPaintingData.fLatticeSelector[noiseX.noisePositionIntegerValue];
442     int j = fPaintingData.fLatticeSelector[noiseX.nextNoisePositionIntegerValue];
443     int b00 = (i + noiseY.noisePositionIntegerValue) & kBlockMask;
444     int b10 = (j + noiseY.noisePositionIntegerValue) & kBlockMask;
445     int b01 = (i + noiseY.nextNoisePositionIntegerValue) & kBlockMask;
446     int b11 = (j + noiseY.nextNoisePositionIntegerValue) & kBlockMask;
447     SkScalar sx = smoothCurve(noiseX.noisePositionFractionValue);
448     SkScalar sy = smoothCurve(noiseY.noisePositionFractionValue);
449 
450     if (sx < 0 || sy < 0 || sx > 1 || sy > 1) {
451         return 0;  // Check for pathological inputs.
452     }
453 
454     // This is taken 1:1 from SVG spec: http://www.w3.org/TR/SVG11/filters.html#feTurbulenceElement
455     SkPoint fractionValue = SkPoint::Make(noiseX.noisePositionFractionValue,
456                                           noiseY.noisePositionFractionValue); // Offset (0,0)
457     u = fPaintingData.fGradient[channel][b00].dot(fractionValue);
458     fractionValue.fX -= SK_Scalar1; // Offset (-1,0)
459     v = fPaintingData.fGradient[channel][b10].dot(fractionValue);
460     SkScalar a = SkScalarInterp(u, v, sx);
461     fractionValue.fY -= SK_Scalar1; // Offset (-1,-1)
462     v = fPaintingData.fGradient[channel][b11].dot(fractionValue);
463     fractionValue.fX = noiseX.noisePositionFractionValue; // Offset (0,-1)
464     u = fPaintingData.fGradient[channel][b01].dot(fractionValue);
465     SkScalar b = SkScalarInterp(u, v, sx);
466     return SkScalarInterp(a, b, sy);
467 }
468 
calculateTurbulenceValueForPoint(int channel,StitchData & stitchData,const SkPoint & point) const469 SkScalar SkPerlinNoiseShaderImpl::PerlinNoiseShaderContext::calculateTurbulenceValueForPoint(
470         int channel, StitchData& stitchData, const SkPoint& point) const {
471     const SkPerlinNoiseShaderImpl& perlinNoiseShader = static_cast<const SkPerlinNoiseShaderImpl&>(fShader);
472     if (perlinNoiseShader.fStitchTiles) {
473         // Set up TurbulenceInitial stitch values.
474         stitchData = fPaintingData.fStitchDataInit;
475     }
476     SkScalar turbulenceFunctionResult = 0;
477     SkPoint noiseVector(SkPoint::Make(point.x() * fPaintingData.fBaseFrequency.fX,
478                                       point.y() * fPaintingData.fBaseFrequency.fY));
479     SkScalar ratio = SK_Scalar1;
480     for (int octave = 0; octave < perlinNoiseShader.fNumOctaves; ++octave) {
481         SkScalar noise = noise2D(channel, stitchData, noiseVector);
482         SkScalar numer = (perlinNoiseShader.fType == kFractalNoise_Type) ?
483                             noise : SkScalarAbs(noise);
484         turbulenceFunctionResult += numer / ratio;
485         noiseVector.fX *= 2;
486         noiseVector.fY *= 2;
487         ratio *= 2;
488         if (perlinNoiseShader.fStitchTiles) {
489             // Update stitch values
490             stitchData = StitchData(SkIntToScalar(stitchData.fWidth) * 2,
491                                     SkIntToScalar(stitchData.fHeight) * 2);
492         }
493     }
494 
495     // The value of turbulenceFunctionResult comes from ((turbulenceFunctionResult) + 1) / 2
496     // by fractalNoise and (turbulenceFunctionResult) by turbulence.
497     if (perlinNoiseShader.fType == kFractalNoise_Type) {
498         turbulenceFunctionResult = SkScalarHalf(turbulenceFunctionResult + 1);
499     }
500 
501     if (channel == 3) { // Scale alpha by paint value
502         turbulenceFunctionResult *= SkIntToScalar(getPaintAlpha()) / 255;
503     }
504 
505     // Clamp result
506     return SkTPin(turbulenceFunctionResult, 0.0f, SK_Scalar1);
507 }
508 
509 ////////////////////////////////////////////////////////////////////////////////////////////////////
510 
shade(const SkPoint & point,StitchData & stitchData) const511 SkPMColor SkPerlinNoiseShaderImpl::PerlinNoiseShaderContext::shade(
512         const SkPoint& point, StitchData& stitchData) const {
513     SkPoint newPoint;
514     fMatrix.mapPoints(&newPoint, &point, 1);
515     newPoint.fX = SkScalarRoundToScalar(newPoint.fX);
516     newPoint.fY = SkScalarRoundToScalar(newPoint.fY);
517 
518     U8CPU rgba[4];
519     for (int channel = 3; channel >= 0; --channel) {
520         SkScalar value;
521         value = calculateTurbulenceValueForPoint(channel, stitchData, newPoint);
522         rgba[channel] = SkScalarFloorToInt(255 * value);
523     }
524     return SkPreMultiplyARGB(rgba[3], rgba[0], rgba[1], rgba[2]);
525 }
526 
527 #ifdef SK_ENABLE_LEGACY_SHADERCONTEXT
onMakeContext(const ContextRec & rec,SkArenaAlloc * alloc) const528 SkShaderBase::Context* SkPerlinNoiseShaderImpl::onMakeContext(const ContextRec& rec,
529                                                               SkArenaAlloc* alloc) const {
530     // should we pay attention to rec's device-colorspace?
531     return alloc->make<PerlinNoiseShaderContext>(*this, rec);
532 }
533 #endif
534 
total_matrix(const SkShaderBase::ContextRec & rec,const SkShaderBase & shader)535 static inline SkMatrix total_matrix(const SkShaderBase::ContextRec& rec,
536                                     const SkShaderBase& shader) {
537     SkMatrix matrix = SkMatrix::Concat(*rec.fMatrix, shader.getLocalMatrix());
538     if (rec.fLocalMatrix) {
539         matrix.preConcat(*rec.fLocalMatrix);
540     }
541 
542     return matrix;
543 }
544 
PerlinNoiseShaderContext(const SkPerlinNoiseShaderImpl & shader,const ContextRec & rec)545 SkPerlinNoiseShaderImpl::PerlinNoiseShaderContext::PerlinNoiseShaderContext(
546         const SkPerlinNoiseShaderImpl& shader, const ContextRec& rec)
547     : INHERITED(shader, rec)
548     , fMatrix(total_matrix(rec, shader)) // used for temp storage, adjusted below
549     , fPaintingData(shader.fTileSize, shader.fSeed, shader.fBaseFrequencyX,
550                     shader.fBaseFrequencyY, fMatrix)
551 {
552     // This (1,1) translation is due to WebKit's 1 based coordinates for the noise
553     // (as opposed to 0 based, usually). The same adjustment is in the setData() function.
554     fMatrix.setTranslate(-fMatrix.getTranslateX() + SK_Scalar1,
555                          -fMatrix.getTranslateY() + SK_Scalar1);
556 }
557 
shadeSpan(int x,int y,SkPMColor result[],int count)558 void SkPerlinNoiseShaderImpl::PerlinNoiseShaderContext::shadeSpan(
559         int x, int y, SkPMColor result[], int count) {
560     SkPoint point = SkPoint::Make(SkIntToScalar(x), SkIntToScalar(y));
561     StitchData stitchData;
562     for (int i = 0; i < count; ++i) {
563         result[i] = shade(point, stitchData);
564         point.fX += SK_Scalar1;
565     }
566 }
567 
568 /////////////////////////////////////////////////////////////////////
569 
570 #if SK_SUPPORT_GPU
571 
572 class GrPerlinNoise2Effect : public GrFragmentProcessor {
573 public:
Make(SkPerlinNoiseShaderImpl::Type type,int numOctaves,bool stitchTiles,std::unique_ptr<SkPerlinNoiseShaderImpl::PaintingData> paintingData,GrSurfaceProxyView permutationsView,GrSurfaceProxyView noiseView,const SkMatrix & matrix,const GrCaps & caps)574     static std::unique_ptr<GrFragmentProcessor> Make(
575             SkPerlinNoiseShaderImpl::Type type,
576             int numOctaves,
577             bool stitchTiles,
578             std::unique_ptr<SkPerlinNoiseShaderImpl::PaintingData> paintingData,
579             GrSurfaceProxyView permutationsView,
580             GrSurfaceProxyView noiseView,
581             const SkMatrix& matrix,
582             const GrCaps& caps) {
583         static constexpr GrSamplerState kRepeatXSampler = {GrSamplerState::WrapMode::kRepeat,
584                                                            GrSamplerState::WrapMode::kClamp,
585                                                            GrSamplerState::Filter::kNearest};
586         auto permutationsFP =
587                 GrTextureEffect::Make(std::move(permutationsView), kPremul_SkAlphaType,
588                                       SkMatrix::I(), kRepeatXSampler, caps);
589         auto noiseFP = GrTextureEffect::Make(std::move(noiseView), kPremul_SkAlphaType,
590                                              SkMatrix::I(), kRepeatXSampler, caps);
591 
592         return GrMatrixEffect::Make(matrix, std::unique_ptr<GrFragmentProcessor>(
593                 new GrPerlinNoise2Effect(type, numOctaves, stitchTiles, std::move(paintingData),
594                                          std::move(permutationsFP), std::move(noiseFP))));
595     }
596 
name() const597     const char* name() const override { return "PerlinNoise"; }
598 
clone() const599     std::unique_ptr<GrFragmentProcessor> clone() const override {
600         return std::unique_ptr<GrFragmentProcessor>(new GrPerlinNoise2Effect(*this));
601     }
602 
stitchData() const603     const SkPerlinNoiseShaderImpl::StitchData& stitchData() const { return fPaintingData->fStitchDataInit; }
604 
type() const605     SkPerlinNoiseShaderImpl::Type type() const { return fType; }
stitchTiles() const606     bool stitchTiles() const { return fStitchTiles; }
baseFrequency() const607     const SkVector& baseFrequency() const { return fPaintingData->fBaseFrequency; }
numOctaves() const608     int numOctaves() const { return fNumOctaves; }
609 
610 private:
611     class Impl : public ProgramImpl {
612     public:
613         void emitCode(EmitArgs&) override;
614 
615     private:
616         void onSetData(const GrGLSLProgramDataManager&, const GrFragmentProcessor&) override;
617 
618         GrGLSLProgramDataManager::UniformHandle fStitchDataUni;
619         GrGLSLProgramDataManager::UniformHandle fBaseFrequencyUni;
620     };
621 
onMakeProgramImpl() const622     std::unique_ptr<ProgramImpl> onMakeProgramImpl() const override {
623         return std::make_unique<Impl>();
624     }
625 
626     void onAddToKey(const GrShaderCaps& caps, GrProcessorKeyBuilder* b) const override;
627 
onIsEqual(const GrFragmentProcessor & sBase) const628     bool onIsEqual(const GrFragmentProcessor& sBase) const override {
629         const GrPerlinNoise2Effect& s = sBase.cast<GrPerlinNoise2Effect>();
630         return fType == s.fType &&
631                fPaintingData->fBaseFrequency == s.fPaintingData->fBaseFrequency &&
632                fNumOctaves == s.fNumOctaves &&
633                fStitchTiles == s.fStitchTiles &&
634                fPaintingData->fStitchDataInit == s.fPaintingData->fStitchDataInit;
635     }
636 
GrPerlinNoise2Effect(SkPerlinNoiseShaderImpl::Type type,int numOctaves,bool stitchTiles,std::unique_ptr<SkPerlinNoiseShaderImpl::PaintingData> paintingData,std::unique_ptr<GrFragmentProcessor> permutationsFP,std::unique_ptr<GrFragmentProcessor> noiseFP)637     GrPerlinNoise2Effect(SkPerlinNoiseShaderImpl::Type type,
638                          int numOctaves,
639                          bool stitchTiles,
640                          std::unique_ptr<SkPerlinNoiseShaderImpl::PaintingData> paintingData,
641                          std::unique_ptr<GrFragmentProcessor> permutationsFP,
642                          std::unique_ptr<GrFragmentProcessor> noiseFP)
643             : INHERITED(kGrPerlinNoise2Effect_ClassID, kNone_OptimizationFlags)
644             , fType(type)
645             , fNumOctaves(numOctaves)
646             , fStitchTiles(stitchTiles)
647             , fPaintingData(std::move(paintingData)) {
648         this->registerChild(std::move(permutationsFP), SkSL::SampleUsage::Explicit());
649         this->registerChild(std::move(noiseFP), SkSL::SampleUsage::Explicit());
650         this->setUsesSampleCoordsDirectly();
651     }
652 
GrPerlinNoise2Effect(const GrPerlinNoise2Effect & that)653     GrPerlinNoise2Effect(const GrPerlinNoise2Effect& that)
654             : INHERITED(that)
655             , fType(that.fType)
656             , fNumOctaves(that.fNumOctaves)
657             , fStitchTiles(that.fStitchTiles)
658             , fPaintingData(new SkPerlinNoiseShaderImpl::PaintingData(*that.fPaintingData)) {}
659 
660     GR_DECLARE_FRAGMENT_PROCESSOR_TEST
661 
662     SkPerlinNoiseShaderImpl::Type       fType;
663     int                                 fNumOctaves;
664     bool                                fStitchTiles;
665 
666     std::unique_ptr<SkPerlinNoiseShaderImpl::PaintingData> fPaintingData;
667 
668     using INHERITED = GrFragmentProcessor;
669 };
670 
671 /////////////////////////////////////////////////////////////////////
672 GR_DEFINE_FRAGMENT_PROCESSOR_TEST(GrPerlinNoise2Effect);
673 
674 #if GR_TEST_UTILS
TestCreate(GrProcessorTestData * d)675 std::unique_ptr<GrFragmentProcessor> GrPerlinNoise2Effect::TestCreate(GrProcessorTestData* d) {
676     int      numOctaves = d->fRandom->nextRangeU(2, 10);
677     bool     stitchTiles = d->fRandom->nextBool();
678     SkScalar seed = SkIntToScalar(d->fRandom->nextU());
679     SkISize  tileSize;
680     tileSize.fWidth = d->fRandom->nextRangeU(4, 4096);
681     tileSize.fHeight = d->fRandom->nextRangeU(4, 4096);
682     SkScalar baseFrequencyX = d->fRandom->nextRangeScalar(0.01f, 0.99f);
683     SkScalar baseFrequencyY = d->fRandom->nextRangeScalar(0.01f, 0.99f);
684 
685     sk_sp<SkShader> shader(d->fRandom->nextBool() ?
686         SkPerlinNoiseShader::MakeFractalNoise(baseFrequencyX, baseFrequencyY, numOctaves, seed,
687                                                stitchTiles ? &tileSize : nullptr) :
688         SkPerlinNoiseShader::MakeTurbulence(baseFrequencyX, baseFrequencyY, numOctaves, seed,
689                                              stitchTiles ? &tileSize : nullptr));
690 
691     GrTest::TestAsFPArgs asFPArgs(d);
692     return as_SB(shader)->asFragmentProcessor(asFPArgs.args());
693 }
694 #endif
695 
emitCode(EmitArgs & args)696 void GrPerlinNoise2Effect::Impl::emitCode(EmitArgs& args) {
697     const GrPerlinNoise2Effect& pne = args.fFp.cast<GrPerlinNoise2Effect>();
698 
699     GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder;
700     GrGLSLUniformHandler* uniformHandler = args.fUniformHandler;
701 
702     fBaseFrequencyUni = uniformHandler->addUniform(&pne, kFragment_GrShaderFlag, kHalf2_GrSLType,
703                                                    "baseFrequency");
704     const char* baseFrequencyUni = uniformHandler->getUniformCStr(fBaseFrequencyUni);
705 
706     const char* stitchDataUni = nullptr;
707     if (pne.stitchTiles()) {
708         fStitchDataUni = uniformHandler->addUniform(&pne, kFragment_GrShaderFlag, kHalf2_GrSLType,
709                                                     "stitchData");
710         stitchDataUni = uniformHandler->getUniformCStr(fStitchDataUni);
711     }
712 
713     // Add noise function
714     const GrShaderVar gPerlinNoiseArgs[] = {{"chanCoord", kHalf_GrSLType },
715                                             {"noiseVec ", kHalf2_GrSLType}};
716 
717     const GrShaderVar gPerlinNoiseStitchArgs[] = {{"chanCoord" , kHalf_GrSLType },
718                                                   {"noiseVec"  , kHalf2_GrSLType},
719                                                   {"stitchData", kHalf2_GrSLType}};
720 
721     SkString noiseCode;
722 
723     noiseCode.append(
724             R"(half4 floorVal;
725                floorVal.xy = floor(noiseVec);
726                floorVal.zw = floorVal.xy + half2(1);
727                half2 fractVal = fract(noiseVec);
728                // smooth curve : t^2*(3 - 2*t)
729                half2 noiseSmooth = fractVal*fractVal*(half2(3) - 2*fractVal);)");
730 
731     // Adjust frequencies if we're stitching tiles
732     if (pne.stitchTiles()) {
733         noiseCode.append(
734              R"(if (floorVal.x >= stitchData.x) { floorVal.x -= stitchData.x; };
735                 if (floorVal.y >= stitchData.y) { floorVal.y -= stitchData.y; };
736                 if (floorVal.z >= stitchData.x) { floorVal.z -= stitchData.x; };
737                 if (floorVal.w >= stitchData.y) { floorVal.w -= stitchData.y; };)");
738     }
739 
740     // NOTE: We need to explicitly pass half4(1) as input color here, because the helper function
741     // can't see fInputColor (which is "_input" in the FP's outer function). skbug.com/10506
742     SkString sampleX = this->invokeChild(0, "half4(1)", args, "half2(floorVal.x, 0.5)");
743     SkString sampleY = this->invokeChild(0, "half4(1)", args, "half2(floorVal.z, 0.5)");
744     noiseCode.appendf("half2 latticeIdx = half2(%s.a, %s.a);", sampleX.c_str(), sampleY.c_str());
745 
746 #if defined(SK_BUILD_FOR_ANDROID)
747     // Android rounding for Tegra devices, like, for example: Xoom (Tegra 2), Nexus 7 (Tegra 3).
748     // The issue is that colors aren't accurate enough on Tegra devices. For example, if an 8 bit
749     // value of 124 (or 0.486275 here) is entered, we can get a texture value of 123.513725
750     // (or 0.484368 here). The following rounding operation prevents these precision issues from
751     // affecting the result of the noise by making sure that we only have multiples of 1/255.
752     // (Note that 1/255 is about 0.003921569, which is the value used here).
753     noiseCode.append(
754             "latticeIdx = floor(latticeIdx * half2(255.0) + half2(0.5)) * half2(0.003921569);");
755 #endif
756 
757     // Get (x,y) coordinates with the permutated x
758     noiseCode.append("half4 bcoords = 256*latticeIdx.xyxy + floorVal.yyww;");
759 
760     noiseCode.append("half2 uv;");
761 
762     // This is the math to convert the two 16bit integer packed into rgba 8 bit input into a
763     // [-1,1] vector and perform a dot product between that vector and the provided vector.
764     // Save it as a string because we will repeat it 4x.
765     static constexpr const char* inc8bit = "0.00390625";  // 1.0 / 256.0
766     SkString dotLattice =
767             SkStringPrintf("dot((lattice.ga + lattice.rb*%s)*2 - half2(1), fractVal)", inc8bit);
768 
769     SkString sampleA = this->invokeChild(1, "half4(1)", args, "half2(bcoords.x, chanCoord)");
770     SkString sampleB = this->invokeChild(1, "half4(1)", args, "half2(bcoords.y, chanCoord)");
771     SkString sampleC = this->invokeChild(1, "half4(1)", args, "half2(bcoords.w, chanCoord)");
772     SkString sampleD = this->invokeChild(1, "half4(1)", args, "half2(bcoords.z, chanCoord)");
773 
774     // Compute u, at offset (0,0)
775     noiseCode.appendf("half4 lattice = %s;", sampleA.c_str());
776     noiseCode.appendf("uv.x = %s;", dotLattice.c_str());
777 
778     // Compute v, at offset (-1,0)
779     noiseCode.append("fractVal.x -= 1.0;");
780     noiseCode.appendf("lattice = %s;", sampleB.c_str());
781     noiseCode.appendf("uv.y = %s;", dotLattice.c_str());
782 
783     // Compute 'a' as a linear interpolation of 'u' and 'v'
784     noiseCode.append("half2 ab;");
785     noiseCode.append("ab.x = mix(uv.x, uv.y, noiseSmooth.x);");
786 
787     // Compute v, at offset (-1,-1)
788     noiseCode.append("fractVal.y -= 1.0;");
789     noiseCode.appendf("lattice = %s;", sampleC.c_str());
790     noiseCode.appendf("uv.y = %s;", dotLattice.c_str());
791 
792     // Compute u, at offset (0,-1)
793     noiseCode.append("fractVal.x += 1.0;");
794     noiseCode.appendf("lattice = %s;", sampleD.c_str());
795     noiseCode.appendf("uv.x = %s;", dotLattice.c_str());
796 
797     // Compute 'b' as a linear interpolation of 'u' and 'v'
798     noiseCode.append("ab.y = mix(uv.x, uv.y, noiseSmooth.x);");
799     // Compute the noise as a linear interpolation of 'a' and 'b'
800     noiseCode.append("return mix(ab.x, ab.y, noiseSmooth.y);");
801 
802     SkString noiseFuncName = fragBuilder->getMangledFunctionName("noiseFuncName");
803     if (pne.stitchTiles()) {
804         fragBuilder->emitFunction(kHalf_GrSLType, noiseFuncName.c_str(),
805                                   {gPerlinNoiseStitchArgs, SK_ARRAY_COUNT(gPerlinNoiseStitchArgs)},
806                                   noiseCode.c_str());
807     } else {
808         fragBuilder->emitFunction(kHalf_GrSLType, noiseFuncName.c_str(),
809                                   {gPerlinNoiseArgs, SK_ARRAY_COUNT(gPerlinNoiseArgs)},
810                                   noiseCode.c_str());
811     }
812 
813     // There are rounding errors if the floor operation is not performed here
814     fragBuilder->codeAppendf("half2 noiseVec = half2(floor(%s.xy) * %s);",
815                              args.fSampleCoord, baseFrequencyUni);
816 
817     // Clear the color accumulator
818     fragBuilder->codeAppendf("half4 color = half4(0);");
819 
820     if (pne.stitchTiles()) {
821         // Set up TurbulenceInitial stitch values.
822         fragBuilder->codeAppendf("half2 stitchData = %s;", stitchDataUni);
823     }
824 
825     fragBuilder->codeAppendf("half ratio = 1.0;");
826 
827     // Loop over all octaves
828     fragBuilder->codeAppendf("for (int octave = 0; octave < %d; ++octave) {", pne.numOctaves());
829     fragBuilder->codeAppendf("    color += ");
830     if (pne.type() != SkPerlinNoiseShaderImpl::kFractalNoise_Type) {
831         fragBuilder->codeAppend("abs(");
832     }
833 
834     // There are 4 lines, put y coords at center of each.
835     static constexpr const char* chanCoordR = "0.5";
836     static constexpr const char* chanCoordG = "1.5";
837     static constexpr const char* chanCoordB = "2.5";
838     static constexpr const char* chanCoordA = "3.5";
839     if (pne.stitchTiles()) {
840         fragBuilder->codeAppendf(R"(
841            half4(%s(%s, noiseVec, stitchData), %s(%s, noiseVec, stitchData),
842                  %s(%s, noiseVec, stitchData), %s(%s, noiseVec, stitchData)))",
843             noiseFuncName.c_str(), chanCoordR,
844             noiseFuncName.c_str(), chanCoordG,
845             noiseFuncName.c_str(), chanCoordB,
846             noiseFuncName.c_str(), chanCoordA);
847     } else {
848         fragBuilder->codeAppendf(R"(
849             half4(%s(%s, noiseVec), %s(%s, noiseVec),
850                   %s(%s, noiseVec), %s(%s, noiseVec)))",
851             noiseFuncName.c_str(), chanCoordR,
852             noiseFuncName.c_str(), chanCoordG,
853             noiseFuncName.c_str(), chanCoordB,
854             noiseFuncName.c_str(), chanCoordA);
855     }
856     if (pne.type() != SkPerlinNoiseShaderImpl::kFractalNoise_Type) {
857         fragBuilder->codeAppend(")");  // end of "abs("
858     }
859     fragBuilder->codeAppend(" * ratio;");
860 
861     fragBuilder->codeAppend(R"(noiseVec *= half2(2.0);
862                                ratio *= 0.5;)");
863 
864     if (pne.stitchTiles()) {
865         fragBuilder->codeAppend("stitchData *= half2(2.0);");
866     }
867     fragBuilder->codeAppend("}");  // end of the for loop on octaves
868 
869     if (pne.type() == SkPerlinNoiseShaderImpl::kFractalNoise_Type) {
870         // The value of turbulenceFunctionResult comes from ((turbulenceFunctionResult) + 1) / 2
871         // by fractalNoise and (turbulenceFunctionResult) by turbulence.
872         fragBuilder->codeAppendf("color = color * half4(0.5) + half4(0.5);");
873     }
874 
875     // Clamp values
876     fragBuilder->codeAppendf("color = saturate(color);");
877 
878     // Pre-multiply the result
879     fragBuilder->codeAppendf("return half4(color.rgb * color.aaa, color.a);");
880 }
881 
onSetData(const GrGLSLProgramDataManager & pdman,const GrFragmentProcessor & processor)882 void GrPerlinNoise2Effect::Impl::onSetData(const GrGLSLProgramDataManager& pdman,
883                                            const GrFragmentProcessor& processor) {
884     const GrPerlinNoise2Effect& turbulence = processor.cast<GrPerlinNoise2Effect>();
885 
886     const SkVector& baseFrequency = turbulence.baseFrequency();
887     pdman.set2f(fBaseFrequencyUni, baseFrequency.fX, baseFrequency.fY);
888 
889     if (turbulence.stitchTiles()) {
890         const SkPerlinNoiseShaderImpl::StitchData& stitchData = turbulence.stitchData();
891         pdman.set2f(fStitchDataUni,
892                     SkIntToScalar(stitchData.fWidth),
893                     SkIntToScalar(stitchData.fHeight));
894     }
895 }
896 
onAddToKey(const GrShaderCaps & caps,GrProcessorKeyBuilder * b) const897 void GrPerlinNoise2Effect::onAddToKey(const GrShaderCaps& caps, GrProcessorKeyBuilder* b) const {
898     uint32_t key = fNumOctaves;
899     key = key << 3;  // Make room for next 3 bits
900     switch (fType) {
901         case SkPerlinNoiseShaderImpl::kFractalNoise_Type:
902             key |= 0x1;
903             break;
904         case SkPerlinNoiseShaderImpl::kTurbulence_Type:
905             key |= 0x2;
906             break;
907         default:
908             // leave key at 0
909             break;
910     }
911     if (fStitchTiles) {
912         key |= 0x4; // Flip the 3rd bit if tile stitching is on
913     }
914     b->add32(key);
915 }
916 
917 /////////////////////////////////////////////////////////////////////
918 
asFragmentProcessor(const GrFPArgs & args) const919 std::unique_ptr<GrFragmentProcessor> SkPerlinNoiseShaderImpl::asFragmentProcessor(
920         const GrFPArgs& args) const {
921     SkASSERT(args.fContext);
922 
923     const auto localMatrix = this->totalLocalMatrix(args.fPreLocalMatrix);
924     const auto paintMatrix = SkMatrix::Concat(args.fMatrixProvider.localToDevice(), *localMatrix);
925 
926     // Either we don't stitch tiles, either we have a valid tile size
927     SkASSERT(!fStitchTiles || !fTileSize.isEmpty());
928 
929     std::unique_ptr<SkPerlinNoiseShaderImpl::PaintingData> paintingData =
930         std::make_unique<SkPerlinNoiseShaderImpl::PaintingData>(fTileSize,
931                                                                   fSeed,
932                                                                   fBaseFrequencyX,
933                                                                   fBaseFrequencyY,
934                                                                   paintMatrix);
935 
936     SkMatrix m = args.fMatrixProvider.localToDevice();
937     m.setTranslateX(-localMatrix->getTranslateX() + SK_Scalar1);
938     m.setTranslateY(-localMatrix->getTranslateY() + SK_Scalar1);
939 
940     auto context = args.fContext;
941 
942     if (0 == fNumOctaves) {
943         if (kFractalNoise_Type == fType) {
944             // Incoming alpha is assumed to be 1. So emit rgba = (1/4, 1/4, 1/4, 1/2)
945             // TODO: Either treat the output of this shader as sRGB or allow client to specify a
946             // color space of the noise. Either way, this case (and the GLSL) need to convert to
947             // the destination.
948             return GrFragmentProcessor::MakeColor(SkPMColor4f::FromBytes_RGBA(0x80404040));
949         }
950         // Emit zero.
951         return GrFragmentProcessor::MakeColor(SK_PMColor4fTRANSPARENT);
952     }
953 
954     const SkBitmap& permutationsBitmap = paintingData->getPermutationsBitmap();
955     const SkBitmap& noiseBitmap        = paintingData->getNoiseBitmap();
956 
957     auto permutationsView = std::get<0>(GrMakeCachedBitmapProxyView(context, permutationsBitmap));
958     auto noiseView        = std::get<0>(GrMakeCachedBitmapProxyView(context, noiseBitmap));
959 
960     if (permutationsView && noiseView) {
961         return GrPerlinNoise2Effect::Make(fType,
962                                           fNumOctaves,
963                                           fStitchTiles,
964                                           std::move(paintingData),
965                                           std::move(permutationsView),
966                                           std::move(noiseView),
967                                           m,
968                                           *context->priv().caps());
969     }
970     return nullptr;
971 }
972 
973 #endif
974 
975 ///////////////////////////////////////////////////////////////////////////////////////////////////
976 
valid_input(SkScalar baseX,SkScalar baseY,int numOctaves,const SkISize * tileSize,SkScalar seed)977 static bool valid_input(SkScalar baseX, SkScalar baseY, int numOctaves, const SkISize* tileSize,
978                         SkScalar seed) {
979     if (!(baseX >= 0 && baseY >= 0)) {
980         return false;
981     }
982     if (!(numOctaves >= 0 && numOctaves <= SkPerlinNoiseShaderImpl::kMaxOctaves)) {
983         return false;
984     }
985     if (tileSize && !(tileSize->width() >= 0 && tileSize->height() >= 0)) {
986         return false;
987     }
988     if (!SkScalarIsFinite(seed)) {
989         return false;
990     }
991     return true;
992 }
993 
MakeFractalNoise(SkScalar baseFrequencyX,SkScalar baseFrequencyY,int numOctaves,SkScalar seed,const SkISize * tileSize)994 sk_sp<SkShader> SkPerlinNoiseShader::MakeFractalNoise(SkScalar baseFrequencyX,
995                                                       SkScalar baseFrequencyY,
996                                                       int numOctaves, SkScalar seed,
997                                                       const SkISize* tileSize) {
998     if (!valid_input(baseFrequencyX, baseFrequencyY, numOctaves, tileSize, seed)) {
999         return nullptr;
1000     }
1001     return sk_sp<SkShader>(new SkPerlinNoiseShaderImpl(SkPerlinNoiseShaderImpl::kFractalNoise_Type,
1002                                                  baseFrequencyX, baseFrequencyY, numOctaves, seed,
1003                                                  tileSize));
1004 }
1005 
MakeTurbulence(SkScalar baseFrequencyX,SkScalar baseFrequencyY,int numOctaves,SkScalar seed,const SkISize * tileSize)1006 sk_sp<SkShader> SkPerlinNoiseShader::MakeTurbulence(SkScalar baseFrequencyX,
1007                                                     SkScalar baseFrequencyY,
1008                                                     int numOctaves, SkScalar seed,
1009                                                     const SkISize* tileSize) {
1010     if (!valid_input(baseFrequencyX, baseFrequencyY, numOctaves, tileSize, seed)) {
1011         return nullptr;
1012     }
1013     return sk_sp<SkShader>(new SkPerlinNoiseShaderImpl(SkPerlinNoiseShaderImpl::kTurbulence_Type,
1014                                                  baseFrequencyX, baseFrequencyY, numOctaves, seed,
1015                                                  tileSize));
1016 }
1017 
RegisterFlattenables()1018 void SkPerlinNoiseShader::RegisterFlattenables() {
1019     SK_REGISTER_FLATTENABLE(SkPerlinNoiseShaderImpl);
1020 }
1021