| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2013 Google Inc. | 2 * Copyright 2013 Google Inc. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
| 5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
| 6 */ | 6 */ |
| 7 | 7 |
| 8 #include "SkDither.h" | 8 #include "SkDither.h" |
| 9 #include "SkPerlinNoiseShader.h" | 9 #include "SkPerlinNoiseShader.h" |
| 10 #include "SkFlattenableBuffers.h" | 10 #include "SkFlattenableBuffers.h" |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 67 fWrapY == other.fWrapY; | 67 fWrapY == other.fWrapY; |
| 68 } | 68 } |
| 69 | 69 |
| 70 int fWidth; // How much to subtract to wrap for stitching. | 70 int fWidth; // How much to subtract to wrap for stitching. |
| 71 int fWrapX; // Minimum value to wrap. | 71 int fWrapX; // Minimum value to wrap. |
| 72 int fHeight; | 72 int fHeight; |
| 73 int fWrapY; | 73 int fWrapY; |
| 74 }; | 74 }; |
| 75 | 75 |
| 76 struct SkPerlinNoiseShader::PaintingData { | 76 struct SkPerlinNoiseShader::PaintingData { |
| 77 PaintingData(int paintingSeed, const SkISize& tileSize) | 77 PaintingData(const SkISize& tileSize) |
| 78 : fSeed(paintingSeed) | 78 : fSeed(0) |
| 79 , fTileSize(tileSize) | 79 , fTileSize(tileSize) |
| 80 , fPermutationsBitmap(NULL) | 80 , fPermutationsBitmap(NULL) |
| 81 , fNoiseBitmap(NULL) | 81 , fNoiseBitmap(NULL) |
| 82 {} | 82 {} |
| 83 | 83 |
| 84 ~PaintingData() | 84 ~PaintingData() |
| 85 { | 85 { |
| 86 SkDELETE(fPermutationsBitmap); | 86 SkDELETE(fPermutationsBitmap); |
| 87 SkDELETE(fNoiseBitmap); | 87 SkDELETE(fNoiseBitmap); |
| 88 } | 88 } |
| (...skipping 18 matching lines...) Expand all Loading... |
| 107 static const int gRandQ = 127773; // m / a | 107 static const int gRandQ = 127773; // m / a |
| 108 static const int gRandR = 2836; // m % a | 108 static const int gRandR = 2836; // m % a |
| 109 | 109 |
| 110 int result = gRandAmplitude * (fSeed % gRandQ) - gRandR * (fSeed / gRand
Q); | 110 int result = gRandAmplitude * (fSeed % gRandQ) - gRandR * (fSeed / gRand
Q); |
| 111 if (result <= 0) | 111 if (result <= 0) |
| 112 result += kRandMaximum; | 112 result += kRandMaximum; |
| 113 fSeed = result; | 113 fSeed = result; |
| 114 return result; | 114 return result; |
| 115 } | 115 } |
| 116 | 116 |
| 117 void init() | 117 void init(SkScalar seed) |
| 118 { | 118 { |
| 119 static const SkScalar gInvBlockSizef = SkScalarInvert(SkIntToScalar(kBlo
ckSize)); | 119 static const SkScalar gInvBlockSizef = SkScalarInvert(SkIntToScalar(kBlo
ckSize)); |
| 120 | 120 |
| 121 // The seed value clamp to the range [1, kRandMaximum - 1]. | 121 // The seed value clamp to the range [1, kRandMaximum - 1]. |
| 122 fSeed = SkScalarRoundToInt(seed); |
| 122 if (fSeed <= 0) { | 123 if (fSeed <= 0) { |
| 123 fSeed = -(fSeed % (kRandMaximum - 1)) + 1; | 124 fSeed = -(fSeed % (kRandMaximum - 1)) + 1; |
| 124 } | 125 } |
| 125 if (fSeed > kRandMaximum - 1) { | 126 if (fSeed > kRandMaximum - 1) { |
| 126 fSeed = kRandMaximum - 1; | 127 fSeed = kRandMaximum - 1; |
| 127 } | 128 } |
| 128 for (int channel = 0; channel < 4; ++channel) { | 129 for (int channel = 0; channel < 4; ++channel) { |
| 129 for (int i = 0; i < kBlockSize; ++i) { | 130 for (int i = 0; i < kBlockSize; ++i) { |
| 130 fLatticeSelector[i] = i; | 131 fLatticeSelector[i] = i; |
| 131 fNoise[channel][i][0] = (random() % (2 * kBlockSize)); | 132 fNoise[channel][i][0] = (random() % (2 * kBlockSize)); |
| (...skipping 180 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 312 buffer.writeScalar(fBaseFrequencyY); | 313 buffer.writeScalar(fBaseFrequencyY); |
| 313 buffer.writeInt(fNumOctaves); | 314 buffer.writeInt(fNumOctaves); |
| 314 buffer.writeScalar(fSeed); | 315 buffer.writeScalar(fSeed); |
| 315 buffer.writeBool(fStitchTiles); | 316 buffer.writeBool(fStitchTiles); |
| 316 buffer.writeInt(fTileSize.fWidth); | 317 buffer.writeInt(fTileSize.fWidth); |
| 317 buffer.writeInt(fTileSize.fHeight); | 318 buffer.writeInt(fTileSize.fHeight); |
| 318 } | 319 } |
| 319 | 320 |
| 320 void SkPerlinNoiseShader::initPaint(PaintingData& paintingData) | 321 void SkPerlinNoiseShader::initPaint(PaintingData& paintingData) |
| 321 { | 322 { |
| 322 paintingData.init(); | 323 paintingData.init(fSeed); |
| 323 | 324 |
| 324 // Set frequencies to original values | 325 // Set frequencies to original values |
| 325 paintingData.fBaseFrequency.set(fBaseFrequencyX, fBaseFrequencyY); | 326 paintingData.fBaseFrequency.set(fBaseFrequencyX, fBaseFrequencyY); |
| 326 // Adjust frequecies based on size if stitching is enabled | 327 // Adjust frequecies based on size if stitching is enabled |
| 327 if (fStitchTiles) { | 328 if (fStitchTiles) { |
| 328 paintingData.stitch(); | 329 paintingData.stitch(); |
| 329 } | 330 } |
| 330 } | 331 } |
| 331 | 332 |
| 332 void SkPerlinNoiseShader::setTileSize(const SkISize& tileSize) { | 333 void SkPerlinNoiseShader::setTileSize(const SkISize& tileSize) { |
| 333 fTileSize = tileSize; | 334 fTileSize = tileSize; |
| 334 | 335 |
| 335 if (NULL == fPaintingData) { | 336 if (NULL == fPaintingData) { |
| 336 fPaintingData = SkNEW_ARGS(PaintingData, (SkScalarRoundToInt(fSeed), fTi
leSize)); | 337 fPaintingData = SkNEW_ARGS(PaintingData, (fTileSize)); |
| 337 initPaint(*fPaintingData); | 338 initPaint(*fPaintingData); |
| 338 } else { | 339 } else { |
| 339 // Set Size | 340 // Set Size |
| 340 fPaintingData->fTileSize = fTileSize; | 341 fPaintingData->fTileSize = fTileSize; |
| 341 // Set frequencies to original values | 342 // Set frequencies to original values |
| 342 fPaintingData->fBaseFrequency.set(fBaseFrequencyX, fBaseFrequencyY); | 343 fPaintingData->fBaseFrequency.set(fBaseFrequencyX, fBaseFrequencyY); |
| 343 // Adjust frequecies based on size if stitching is enabled | 344 // Adjust frequecies based on size if stitching is enabled |
| 344 if (fStitchTiles) { | 345 if (fStitchTiles) { |
| 345 fPaintingData->stitch(); | 346 fPaintingData->stitch(); |
| 346 } | 347 } |
| (...skipping 261 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 608 | 609 |
| 609 typedef GrEffect INHERITED; | 610 typedef GrEffect INHERITED; |
| 610 }; | 611 }; |
| 611 | 612 |
| 612 ///////////////////////////////////////////////////////////////////// | 613 ///////////////////////////////////////////////////////////////////// |
| 613 | 614 |
| 614 GR_DEFINE_EFFECT_TEST(GrPerlinNoiseEffect); | 615 GR_DEFINE_EFFECT_TEST(GrPerlinNoiseEffect); |
| 615 | 616 |
| 616 GrEffectRef* GrPerlinNoiseEffect::TestCreate(SkMWCRandom* random, | 617 GrEffectRef* GrPerlinNoiseEffect::TestCreate(SkMWCRandom* random, |
| 617 GrContext* context, | 618 GrContext* context, |
| 619 const GrDrawTargetCaps&, |
| 618 GrTexture**) { | 620 GrTexture**) { |
| 619 int numOctaves = random->nextRangeU(2, 10); | 621 int numOctaves = random->nextRangeU(2, 10); |
| 620 bool stitchTiles = random->nextBool(); | 622 bool stitchTiles = random->nextBool(); |
| 621 SkScalar seed = SkIntToScalar(random->nextU()); | 623 SkScalar seed = SkIntToScalar(random->nextU()); |
| 622 SkISize tileSize = SkISize::Make(random->nextRangeU(4, 4096), random->nextR
angeU(4, 4096)); | 624 SkISize tileSize = SkISize::Make(random->nextRangeU(4, 4096), random->nextR
angeU(4, 4096)); |
| 623 SkScalar baseFrequencyX = random->nextRangeScalar(SkFloatToScalar(0.01f), | 625 SkScalar baseFrequencyX = random->nextRangeScalar(SkFloatToScalar(0.01f), |
| 624 SkFloatToScalar(0.99f)); | 626 SkFloatToScalar(0.99f)); |
| 625 SkScalar baseFrequencyY = random->nextRangeScalar(SkFloatToScalar(0.01f), | 627 SkScalar baseFrequencyY = random->nextRangeScalar(SkFloatToScalar(0.01f), |
| 626 SkFloatToScalar(0.99f)); | 628 SkFloatToScalar(0.99f)); |
| 627 | 629 |
| (...skipping 361 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 989 str->append(" seed: "); | 991 str->append(" seed: "); |
| 990 str->appendScalar(fSeed); | 992 str->appendScalar(fSeed); |
| 991 str->append(" stitch tiles: "); | 993 str->append(" stitch tiles: "); |
| 992 str->append(fStitchTiles ? "true " : "false "); | 994 str->append(fStitchTiles ? "true " : "false "); |
| 993 | 995 |
| 994 this->INHERITED::toString(str); | 996 this->INHERITED::toString(str); |
| 995 | 997 |
| 996 str->append(")"); | 998 str->append(")"); |
| 997 } | 999 } |
| 998 #endif | 1000 #endif |
| OLD | NEW |