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 "SkColorFilter.h" | 10 #include "SkColorFilter.h" |
(...skipping 254 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
265 SkScalar baseFrequencyX, | 265 SkScalar baseFrequencyX, |
266 SkScalar baseFrequencyY, | 266 SkScalar baseFrequencyY, |
267 int numOctaves, | 267 int numOctaves, |
268 SkScalar seed, | 268 SkScalar seed, |
269 const SkISize* tileSize) | 269 const SkISize* tileSize) |
270 : fType(type) | 270 : fType(type) |
271 , fBaseFrequencyX(baseFrequencyX) | 271 , fBaseFrequencyX(baseFrequencyX) |
272 , fBaseFrequencyY(baseFrequencyY) | 272 , fBaseFrequencyY(baseFrequencyY) |
273 , fNumOctaves(numOctaves > 255 ? 255 : numOctaves/*[0,255] octaves allowed*/) | 273 , fNumOctaves(numOctaves > 255 ? 255 : numOctaves/*[0,255] octaves allowed*/) |
274 , fSeed(seed) | 274 , fSeed(seed) |
275 , fTileSize(NULL == tileSize ? SkISize::Make(0, 0) : *tileSize) | 275 , fTileSize(nullptr == tileSize ? SkISize::Make(0, 0) : *tileSize) |
276 , fStitchTiles(!fTileSize.isEmpty()) | 276 , fStitchTiles(!fTileSize.isEmpty()) |
277 { | 277 { |
278 SkASSERT(numOctaves >= 0 && numOctaves < 256); | 278 SkASSERT(numOctaves >= 0 && numOctaves < 256); |
279 } | 279 } |
280 | 280 |
281 SkPerlinNoiseShader::~SkPerlinNoiseShader() { | 281 SkPerlinNoiseShader::~SkPerlinNoiseShader() { |
282 } | 282 } |
283 | 283 |
284 SkFlattenable* SkPerlinNoiseShader::CreateProc(SkReadBuffer& buffer) { | 284 SkFlattenable* SkPerlinNoiseShader::CreateProc(SkReadBuffer& buffer) { |
285 Type type = (Type)buffer.readInt(); | 285 Type type = (Type)buffer.readInt(); |
286 SkScalar freqX = buffer.readScalar(); | 286 SkScalar freqX = buffer.readScalar(); |
287 SkScalar freqY = buffer.readScalar(); | 287 SkScalar freqY = buffer.readScalar(); |
288 int octaves = buffer.readInt(); | 288 int octaves = buffer.readInt(); |
289 SkScalar seed = buffer.readScalar(); | 289 SkScalar seed = buffer.readScalar(); |
290 SkISize tileSize; | 290 SkISize tileSize; |
291 tileSize.fWidth = buffer.readInt(); | 291 tileSize.fWidth = buffer.readInt(); |
292 tileSize.fHeight = buffer.readInt(); | 292 tileSize.fHeight = buffer.readInt(); |
293 | 293 |
294 switch (type) { | 294 switch (type) { |
295 case kFractalNoise_Type: | 295 case kFractalNoise_Type: |
296 return SkPerlinNoiseShader::CreateFractalNoise(freqX, freqY, octaves
, seed, &tileSize); | 296 return SkPerlinNoiseShader::CreateFractalNoise(freqX, freqY, octaves
, seed, &tileSize); |
297 case kTurbulence_Type: | 297 case kTurbulence_Type: |
298 return SkPerlinNoiseShader::CreateTubulence(freqX, freqY, octaves, s
eed, &tileSize); | 298 return SkPerlinNoiseShader::CreateTubulence(freqX, freqY, octaves, s
eed, &tileSize); |
299 default: | 299 default: |
300 return NULL; | 300 return nullptr; |
301 } | 301 } |
302 } | 302 } |
303 | 303 |
304 void SkPerlinNoiseShader::flatten(SkWriteBuffer& buffer) const { | 304 void SkPerlinNoiseShader::flatten(SkWriteBuffer& buffer) const { |
305 buffer.writeInt((int) fType); | 305 buffer.writeInt((int) fType); |
306 buffer.writeScalar(fBaseFrequencyX); | 306 buffer.writeScalar(fBaseFrequencyX); |
307 buffer.writeScalar(fBaseFrequencyY); | 307 buffer.writeScalar(fBaseFrequencyY); |
308 buffer.writeInt(fNumOctaves); | 308 buffer.writeInt(fNumOctaves); |
309 buffer.writeScalar(fSeed); | 309 buffer.writeScalar(fSeed); |
310 buffer.writeInt(fTileSize.fWidth); | 310 buffer.writeInt(fTileSize.fWidth); |
(...skipping 289 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
600 SkScalar seed = SkIntToScalar(d->fRandom->nextU()); | 600 SkScalar seed = SkIntToScalar(d->fRandom->nextU()); |
601 SkISize tileSize = SkISize::Make(d->fRandom->nextRangeU(4, 4096), | 601 SkISize tileSize = SkISize::Make(d->fRandom->nextRangeU(4, 4096), |
602 d->fRandom->nextRangeU(4, 4096)); | 602 d->fRandom->nextRangeU(4, 4096)); |
603 SkScalar baseFrequencyX = d->fRandom->nextRangeScalar(0.01f, | 603 SkScalar baseFrequencyX = d->fRandom->nextRangeScalar(0.01f, |
604 0.99f); | 604 0.99f); |
605 SkScalar baseFrequencyY = d->fRandom->nextRangeScalar(0.01f, | 605 SkScalar baseFrequencyY = d->fRandom->nextRangeScalar(0.01f, |
606 0.99f); | 606 0.99f); |
607 | 607 |
608 SkShader* shader = d->fRandom->nextBool() ? | 608 SkShader* shader = d->fRandom->nextBool() ? |
609 SkPerlinNoiseShader::CreateFractalNoise(baseFrequencyX, baseFrequencyY,
numOctaves, seed, | 609 SkPerlinNoiseShader::CreateFractalNoise(baseFrequencyX, baseFrequencyY,
numOctaves, seed, |
610 stitchTiles ? &tileSize : NULL)
: | 610 stitchTiles ? &tileSize : nullpt
r) : |
611 SkPerlinNoiseShader::CreateTurbulence(baseFrequencyX, baseFrequencyY, nu
mOctaves, seed, | 611 SkPerlinNoiseShader::CreateTurbulence(baseFrequencyX, baseFrequencyY, nu
mOctaves, seed, |
612 stitchTiles ? &tileSize : NULL); | 612 stitchTiles ? &tileSize : nullptr); |
613 | 613 |
614 SkPaint paint; | 614 SkPaint paint; |
615 GrColor paintColor; | 615 GrColor paintColor; |
616 GrFragmentProcessor* effect; | 616 GrFragmentProcessor* effect; |
617 GrPaint grPaint; | 617 GrPaint grPaint; |
618 SkAssertResult(shader->asFragmentProcessor(d->fContext, paint, | 618 SkAssertResult(shader->asFragmentProcessor(d->fContext, paint, |
619 GrTest::TestMatrix(d->fRandom), N
ULL, | 619 GrTest::TestMatrix(d->fRandom), n
ullptr, |
620 &paintColor, grPaint.getProcessor
DataManager(), | 620 &paintColor, grPaint.getProcessor
DataManager(), |
621 &effect)); | 621 &effect)); |
622 | 622 |
623 delete shader; | 623 delete shader; |
624 | 624 |
625 return effect; | 625 return effect; |
626 } | 626 } |
627 | 627 |
628 GrGLPerlinNoise::GrGLPerlinNoise(const GrProcessor& processor) | 628 GrGLPerlinNoise::GrGLPerlinNoise(const GrProcessor& processor) |
629 : fType(processor.cast<GrPerlinNoiseEffect>().type()) | 629 : fType(processor.cast<GrPerlinNoiseEffect>().type()) |
630 , fStitchTiles(processor.cast<GrPerlinNoiseEffect>().stitchTiles()) | 630 , fStitchTiles(processor.cast<GrPerlinNoiseEffect>().stitchTiles()) |
631 , fNumOctaves(processor.cast<GrPerlinNoiseEffect>().numOctaves()) { | 631 , fNumOctaves(processor.cast<GrPerlinNoiseEffect>().numOctaves()) { |
632 } | 632 } |
633 | 633 |
634 void GrGLPerlinNoise::emitCode(EmitArgs& args) { | 634 void GrGLPerlinNoise::emitCode(EmitArgs& args) { |
635 GrGLFragmentBuilder* fsBuilder = args.fBuilder->getFragmentShaderBuilder(); | 635 GrGLFragmentBuilder* fsBuilder = args.fBuilder->getFragmentShaderBuilder(); |
636 SkString vCoords = fsBuilder->ensureFSCoords2D(args.fCoords, 0); | 636 SkString vCoords = fsBuilder->ensureFSCoords2D(args.fCoords, 0); |
637 | 637 |
638 fBaseFrequencyUni = args.fBuilder->addUniform(GrGLProgramBuilder::kFragment_
Visibility, | 638 fBaseFrequencyUni = args.fBuilder->addUniform(GrGLProgramBuilder::kFragment_
Visibility, |
639 kVec2f_GrSLType, kDefault_GrSLPrecis
ion, | 639 kVec2f_GrSLType, kDefault_GrSLPrecis
ion, |
640 "baseFrequency"); | 640 "baseFrequency"); |
641 const char* baseFrequencyUni = args.fBuilder->getUniformCStr(fBaseFrequencyU
ni); | 641 const char* baseFrequencyUni = args.fBuilder->getUniformCStr(fBaseFrequencyU
ni); |
642 fAlphaUni = args.fBuilder->addUniform(GrGLProgramBuilder::kFragment_Visibili
ty, | 642 fAlphaUni = args.fBuilder->addUniform(GrGLProgramBuilder::kFragment_Visibili
ty, |
643 kFloat_GrSLType, kDefault_GrSLPrecision, | 643 kFloat_GrSLType, kDefault_GrSLPrecision, |
644 "alpha"); | 644 "alpha"); |
645 const char* alphaUni = args.fBuilder->getUniformCStr(fAlphaUni); | 645 const char* alphaUni = args.fBuilder->getUniformCStr(fAlphaUni); |
646 | 646 |
647 const char* stitchDataUni = NULL; | 647 const char* stitchDataUni = nullptr; |
648 if (fStitchTiles) { | 648 if (fStitchTiles) { |
649 fStitchDataUni = args.fBuilder->addUniform(GrGLProgramBuilder::kFragment
_Visibility, | 649 fStitchDataUni = args.fBuilder->addUniform(GrGLProgramBuilder::kFragment
_Visibility, |
650 kVec2f_GrSLType, kDefault_GrSLPreci
sion, | 650 kVec2f_GrSLType, kDefault_GrSLPreci
sion, |
651 "stitchData"); | 651 "stitchData"); |
652 stitchDataUni = args.fBuilder->getUniformCStr(fStitchDataUni); | 652 stitchDataUni = args.fBuilder->getUniformCStr(fStitchDataUni); |
653 } | 653 } |
654 | 654 |
655 // There are 4 lines, so the center of each line is 1/8, 3/8, 5/8 and 7/8 | 655 // There are 4 lines, so the center of each line is 1/8, 3/8, 5/8 and 7/8 |
656 const char* chanCoordR = "0.125"; | 656 const char* chanCoordR = "0.125"; |
657 const char* chanCoordG = "0.375"; | 657 const char* chanCoordG = "0.375"; |
(...skipping 301 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
959 } | 959 } |
960 return true; | 960 return true; |
961 } | 961 } |
962 | 962 |
963 // Either we don't stitch tiles, either we have a valid tile size | 963 // Either we don't stitch tiles, either we have a valid tile size |
964 SkASSERT(!fStitchTiles || !fTileSize.isEmpty()); | 964 SkASSERT(!fStitchTiles || !fTileSize.isEmpty()); |
965 | 965 |
966 SkPerlinNoiseShader::PaintingData* paintingData = | 966 SkPerlinNoiseShader::PaintingData* paintingData = |
967 new PaintingData(fTileSize, fSeed, fBaseFrequencyX, fBaseFrequencyY,
matrix); | 967 new PaintingData(fTileSize, fSeed, fBaseFrequencyX, fBaseFrequencyY,
matrix); |
968 SkAutoTUnref<GrTexture> permutationsTexture( | 968 SkAutoTUnref<GrTexture> permutationsTexture( |
969 GrRefCachedBitmapTexture(context, paintingData->getPermutationsBitmap(),
NULL)); | 969 GrRefCachedBitmapTexture(context, paintingData->getPermutationsBitmap(),
nullptr)); |
970 SkAutoTUnref<GrTexture> noiseTexture( | 970 SkAutoTUnref<GrTexture> noiseTexture( |
971 GrRefCachedBitmapTexture(context, paintingData->getNoiseBitmap(), NULL))
; | 971 GrRefCachedBitmapTexture(context, paintingData->getNoiseBitmap(), nullpt
r)); |
972 | 972 |
973 SkMatrix m = viewM; | 973 SkMatrix m = viewM; |
974 m.setTranslateX(-localMatrix.getTranslateX() + SK_Scalar1); | 974 m.setTranslateX(-localMatrix.getTranslateX() + SK_Scalar1); |
975 m.setTranslateY(-localMatrix.getTranslateY() + SK_Scalar1); | 975 m.setTranslateY(-localMatrix.getTranslateY() + SK_Scalar1); |
976 if ((permutationsTexture) && (noiseTexture)) { | 976 if ((permutationsTexture) && (noiseTexture)) { |
977 *fp = GrPerlinNoiseEffect::Create(procDataManager, | 977 *fp = GrPerlinNoiseEffect::Create(procDataManager, |
978 fType, | 978 fType, |
979 fNumOctaves, | 979 fNumOctaves, |
980 fStitchTiles, | 980 fStitchTiles, |
981 paintingData, | 981 paintingData, |
982 permutationsTexture, noiseTexture, | 982 permutationsTexture, noiseTexture, |
983 m, paint.getAlpha()); | 983 m, paint.getAlpha()); |
984 } else { | 984 } else { |
985 delete paintingData; | 985 delete paintingData; |
986 *fp = NULL; | 986 *fp = nullptr; |
987 } | 987 } |
988 return true; | 988 return true; |
989 } | 989 } |
990 | 990 |
991 #else | 991 #else |
992 | 992 |
993 bool SkPerlinNoiseShader::asFragmentProcessor(GrContext*, const SkPaint&, const
SkMatrix&, | 993 bool SkPerlinNoiseShader::asFragmentProcessor(GrContext*, const SkPaint&, const
SkMatrix&, |
994 const SkMatrix*, GrColor*, GrProce
ssorDataManager*, | 994 const SkMatrix*, GrColor*, GrProce
ssorDataManager*, |
995 GrFragmentProcessor**) const { | 995 GrFragmentProcessor**) const { |
996 SkDEBUGFAIL("Should not call in GPU-less build"); | 996 SkDEBUGFAIL("Should not call in GPU-less build"); |
(...skipping 27 matching lines...) Expand all Loading... |
1024 str->append(" seed: "); | 1024 str->append(" seed: "); |
1025 str->appendScalar(fSeed); | 1025 str->appendScalar(fSeed); |
1026 str->append(" stitch tiles: "); | 1026 str->append(" stitch tiles: "); |
1027 str->append(fStitchTiles ? "true " : "false "); | 1027 str->append(fStitchTiles ? "true " : "false "); |
1028 | 1028 |
1029 this->INHERITED::toString(str); | 1029 this->INHERITED::toString(str); |
1030 | 1030 |
1031 str->append(")"); | 1031 str->append(")"); |
1032 } | 1032 } |
1033 #endif | 1033 #endif |
OLD | NEW |