| 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" |
| 11 #include "SkReadBuffer.h" | 11 #include "SkReadBuffer.h" |
| 12 #include "SkWriteBuffer.h" | 12 #include "SkWriteBuffer.h" |
| 13 #include "SkShader.h" | 13 #include "SkShader.h" |
| 14 #include "SkUnPreMultiply.h" | 14 #include "SkUnPreMultiply.h" |
| 15 #include "SkString.h" | 15 #include "SkString.h" |
| 16 | 16 |
| 17 #if SK_SUPPORT_GPU | 17 #if SK_SUPPORT_GPU |
| 18 #include "GrContext.h" | 18 #include "GrContext.h" |
| 19 #include "GrCoordTransform.h" | 19 #include "GrCoordTransform.h" |
| 20 #include "GrInvariantOutput.h" | 20 #include "GrInvariantOutput.h" |
| 21 #include "SkGr.h" | 21 #include "SkGr.h" |
| 22 #include "effects/GrConstColorProcessor.h" |
| 23 #include "effects/GrExtractAlphaFragmentProcessor.h" |
| 22 #include "gl/GrGLFragmentProcessor.h" | 24 #include "gl/GrGLFragmentProcessor.h" |
| 23 #include "gl/builders/GrGLProgramBuilder.h" | 25 #include "gl/builders/GrGLProgramBuilder.h" |
| 24 #endif | 26 #endif |
| 25 | 27 |
| 26 static const int kBlockSize = 256; | 28 static const int kBlockSize = 256; |
| 27 static const int kBlockMask = kBlockSize - 1; | 29 static const int kBlockMask = kBlockSize - 1; |
| 28 static const int kPerlinNoise = 4096; | 30 static const int kPerlinNoise = 4096; |
| 29 static const int kRandMaximum = SK_MaxS32; // 2**31 - 1 | 31 static const int kRandMaximum = SK_MaxS32; // 2**31 - 1 |
| 30 | 32 |
| 31 namespace { | 33 namespace { |
| (...skipping 461 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 493 protected: | 495 protected: |
| 494 void onSetData(const GrGLProgramDataManager&, const GrProcessor&) override; | 496 void onSetData(const GrGLProgramDataManager&, const GrProcessor&) override; |
| 495 | 497 |
| 496 private: | 498 private: |
| 497 | 499 |
| 498 GrGLProgramDataManager::UniformHandle fStitchDataUni; | 500 GrGLProgramDataManager::UniformHandle fStitchDataUni; |
| 499 SkPerlinNoiseShader::Type fType; | 501 SkPerlinNoiseShader::Type fType; |
| 500 bool fStitchTiles; | 502 bool fStitchTiles; |
| 501 int fNumOctaves; | 503 int fNumOctaves; |
| 502 GrGLProgramDataManager::UniformHandle fBaseFrequencyUni; | 504 GrGLProgramDataManager::UniformHandle fBaseFrequencyUni; |
| 503 GrGLProgramDataManager::UniformHandle fAlphaUni; | |
| 504 | 505 |
| 505 private: | 506 private: |
| 506 typedef GrGLFragmentProcessor INHERITED; | 507 typedef GrGLFragmentProcessor INHERITED; |
| 507 }; | 508 }; |
| 508 | 509 |
| 509 ///////////////////////////////////////////////////////////////////// | 510 ///////////////////////////////////////////////////////////////////// |
| 510 | 511 |
| 511 class GrPerlinNoiseEffect : public GrFragmentProcessor { | 512 class GrPerlinNoiseEffect : public GrFragmentProcessor { |
| 512 public: | 513 public: |
| 513 static GrFragmentProcessor* Create(GrProcessorDataManager* procDataManager, | 514 static GrFragmentProcessor* Create(GrProcessorDataManager* procDataManager, |
| 514 SkPerlinNoiseShader::Type type, | 515 SkPerlinNoiseShader::Type type, |
| 515 int numOctaves, bool stitchTiles, | 516 int numOctaves, bool stitchTiles, |
| 516 SkPerlinNoiseShader::PaintingData* painti
ngData, | 517 SkPerlinNoiseShader::PaintingData* painti
ngData, |
| 517 GrTexture* permutationsTexture, GrTexture
* noiseTexture, | 518 GrTexture* permutationsTexture, GrTexture
* noiseTexture, |
| 518 const SkMatrix& matrix, uint8_t alpha) { | 519 const SkMatrix& matrix) { |
| 519 return new GrPerlinNoiseEffect(procDataManager, type, numOctaves, stitch
Tiles, paintingData, | 520 return new GrPerlinNoiseEffect(procDataManager, type, numOctaves, stitch
Tiles, paintingData, |
| 520 permutationsTexture, noiseTexture, matrix
, alpha); | 521 permutationsTexture, noiseTexture, matrix
); |
| 521 } | 522 } |
| 522 | 523 |
| 523 virtual ~GrPerlinNoiseEffect() { delete fPaintingData; } | 524 virtual ~GrPerlinNoiseEffect() { delete fPaintingData; } |
| 524 | 525 |
| 525 const char* name() const override { return "PerlinNoise"; } | 526 const char* name() const override { return "PerlinNoise"; } |
| 526 | 527 |
| 527 const SkPerlinNoiseShader::StitchData& stitchData() const { return fPainting
Data->fStitchDataInit; } | 528 const SkPerlinNoiseShader::StitchData& stitchData() const { return fPainting
Data->fStitchDataInit; } |
| 528 | 529 |
| 529 SkPerlinNoiseShader::Type type() const { return fType; } | 530 SkPerlinNoiseShader::Type type() const { return fType; } |
| 530 bool stitchTiles() const { return fStitchTiles; } | 531 bool stitchTiles() const { return fStitchTiles; } |
| 531 const SkVector& baseFrequency() const { return fPaintingData->fBaseFrequency
; } | 532 const SkVector& baseFrequency() const { return fPaintingData->fBaseFrequency
; } |
| 532 int numOctaves() const { return fNumOctaves; } | 533 int numOctaves() const { return fNumOctaves; } |
| 533 const SkMatrix& matrix() const { return fCoordTransform.getMatrix(); } | 534 const SkMatrix& matrix() const { return fCoordTransform.getMatrix(); } |
| 534 uint8_t alpha() const { return fAlpha; } | |
| 535 | 535 |
| 536 private: | 536 private: |
| 537 GrGLFragmentProcessor* onCreateGLInstance() const override { | 537 GrGLFragmentProcessor* onCreateGLInstance() const override { |
| 538 return new GrGLPerlinNoise(*this); | 538 return new GrGLPerlinNoise(*this); |
| 539 } | 539 } |
| 540 | 540 |
| 541 virtual void onGetGLProcessorKey(const GrGLSLCaps& caps, | 541 virtual void onGetGLProcessorKey(const GrGLSLCaps& caps, |
| 542 GrProcessorKeyBuilder* b) const override { | 542 GrProcessorKeyBuilder* b) const override { |
| 543 GrGLPerlinNoise::GenKey(*this, caps, b); | 543 GrGLPerlinNoise::GenKey(*this, caps, b); |
| 544 } | 544 } |
| 545 | 545 |
| 546 bool onIsEqual(const GrFragmentProcessor& sBase) const override { | 546 bool onIsEqual(const GrFragmentProcessor& sBase) const override { |
| 547 const GrPerlinNoiseEffect& s = sBase.cast<GrPerlinNoiseEffect>(); | 547 const GrPerlinNoiseEffect& s = sBase.cast<GrPerlinNoiseEffect>(); |
| 548 return fType == s.fType && | 548 return fType == s.fType && |
| 549 fPaintingData->fBaseFrequency == s.fPaintingData->fBaseFrequency
&& | 549 fPaintingData->fBaseFrequency == s.fPaintingData->fBaseFrequency
&& |
| 550 fNumOctaves == s.fNumOctaves && | 550 fNumOctaves == s.fNumOctaves && |
| 551 fStitchTiles == s.fStitchTiles && | 551 fStitchTiles == s.fStitchTiles && |
| 552 fAlpha == s.fAlpha && | |
| 553 fPaintingData->fStitchDataInit == s.fPaintingData->fStitchDataIni
t; | 552 fPaintingData->fStitchDataInit == s.fPaintingData->fStitchDataIni
t; |
| 554 } | 553 } |
| 555 | 554 |
| 556 void onComputeInvariantOutput(GrInvariantOutput* inout) const override { | 555 void onComputeInvariantOutput(GrInvariantOutput* inout) const override { |
| 557 inout->setToUnknown(GrInvariantOutput::kWillNot_ReadInput); | 556 inout->setToUnknown(GrInvariantOutput::kWillNot_ReadInput); |
| 558 } | 557 } |
| 559 | 558 |
| 560 GrPerlinNoiseEffect(GrProcessorDataManager*, SkPerlinNoiseShader::Type type, | 559 GrPerlinNoiseEffect(GrProcessorDataManager*, SkPerlinNoiseShader::Type type, |
| 561 int numOctaves, bool stitchTiles, | 560 int numOctaves, bool stitchTiles, |
| 562 SkPerlinNoiseShader::PaintingData* paintingData, | 561 SkPerlinNoiseShader::PaintingData* paintingData, |
| 563 GrTexture* permutationsTexture, GrTexture* noiseTexture, | 562 GrTexture* permutationsTexture, GrTexture* noiseTexture, |
| 564 const SkMatrix& matrix, uint8_t alpha) | 563 const SkMatrix& matrix) |
| 565 : fType(type) | 564 : fType(type) |
| 566 , fNumOctaves(numOctaves) | 565 , fNumOctaves(numOctaves) |
| 567 , fStitchTiles(stitchTiles) | 566 , fStitchTiles(stitchTiles) |
| 568 , fAlpha(alpha) | |
| 569 , fPermutationsAccess(permutationsTexture) | 567 , fPermutationsAccess(permutationsTexture) |
| 570 , fNoiseAccess(noiseTexture) | 568 , fNoiseAccess(noiseTexture) |
| 571 , fPaintingData(paintingData) { | 569 , fPaintingData(paintingData) { |
| 572 this->initClassID<GrPerlinNoiseEffect>(); | 570 this->initClassID<GrPerlinNoiseEffect>(); |
| 573 this->addTextureAccess(&fPermutationsAccess); | 571 this->addTextureAccess(&fPermutationsAccess); |
| 574 this->addTextureAccess(&fNoiseAccess); | 572 this->addTextureAccess(&fNoiseAccess); |
| 575 fCoordTransform.reset(kLocal_GrCoordSet, matrix); | 573 fCoordTransform.reset(kLocal_GrCoordSet, matrix); |
| 576 this->addCoordTransform(&fCoordTransform); | 574 this->addCoordTransform(&fCoordTransform); |
| 577 } | 575 } |
| 578 | 576 |
| 579 GR_DECLARE_FRAGMENT_PROCESSOR_TEST; | 577 GR_DECLARE_FRAGMENT_PROCESSOR_TEST; |
| 580 | 578 |
| 581 SkPerlinNoiseShader::Type fType; | 579 SkPerlinNoiseShader::Type fType; |
| 582 GrCoordTransform fCoordTransform; | 580 GrCoordTransform fCoordTransform; |
| 583 int fNumOctaves; | 581 int fNumOctaves; |
| 584 bool fStitchTiles; | 582 bool fStitchTiles; |
| 585 uint8_t fAlpha; | |
| 586 GrTextureAccess fPermutationsAccess; | 583 GrTextureAccess fPermutationsAccess; |
| 587 GrTextureAccess fNoiseAccess; | 584 GrTextureAccess fNoiseAccess; |
| 588 SkPerlinNoiseShader::PaintingData *fPaintingData; | 585 SkPerlinNoiseShader::PaintingData *fPaintingData; |
| 589 | 586 |
| 590 private: | 587 private: |
| 591 typedef GrFragmentProcessor INHERITED; | 588 typedef GrFragmentProcessor INHERITED; |
| 592 }; | 589 }; |
| 593 | 590 |
| 594 ///////////////////////////////////////////////////////////////////// | 591 ///////////////////////////////////////////////////////////////////// |
| 595 GR_DEFINE_FRAGMENT_PROCESSOR_TEST(GrPerlinNoiseEffect); | 592 GR_DEFINE_FRAGMENT_PROCESSOR_TEST(GrPerlinNoiseEffect); |
| 596 | 593 |
| 597 GrFragmentProcessor* GrPerlinNoiseEffect::TestCreate(GrProcessorTestData* d) { | 594 const GrFragmentProcessor* GrPerlinNoiseEffect::TestCreate(GrProcessorTestData*
d) { |
| 598 int numOctaves = d->fRandom->nextRangeU(2, 10); | 595 int numOctaves = d->fRandom->nextRangeU(2, 10); |
| 599 bool stitchTiles = d->fRandom->nextBool(); | 596 bool stitchTiles = d->fRandom->nextBool(); |
| 600 SkScalar seed = SkIntToScalar(d->fRandom->nextU()); | 597 SkScalar seed = SkIntToScalar(d->fRandom->nextU()); |
| 601 SkISize tileSize = SkISize::Make(d->fRandom->nextRangeU(4, 4096), | 598 SkISize tileSize = SkISize::Make(d->fRandom->nextRangeU(4, 4096), |
| 602 d->fRandom->nextRangeU(4, 4096)); | 599 d->fRandom->nextRangeU(4, 4096)); |
| 603 SkScalar baseFrequencyX = d->fRandom->nextRangeScalar(0.01f, | 600 SkScalar baseFrequencyX = d->fRandom->nextRangeScalar(0.01f, |
| 604 0.99f); | 601 0.99f); |
| 605 SkScalar baseFrequencyY = d->fRandom->nextRangeScalar(0.01f, | 602 SkScalar baseFrequencyY = d->fRandom->nextRangeScalar(0.01f, |
| 606 0.99f); | 603 0.99f); |
| 607 | 604 |
| 608 SkShader* shader = d->fRandom->nextBool() ? | 605 SkShader* shader = d->fRandom->nextBool() ? |
| 609 SkPerlinNoiseShader::CreateFractalNoise(baseFrequencyX, baseFrequencyY,
numOctaves, seed, | 606 SkPerlinNoiseShader::CreateFractalNoise(baseFrequencyX, baseFrequencyY,
numOctaves, seed, |
| 610 stitchTiles ? &tileSize : nullpt
r) : | 607 stitchTiles ? &tileSize : nullpt
r) : |
| 611 SkPerlinNoiseShader::CreateTurbulence(baseFrequencyX, baseFrequencyY, nu
mOctaves, seed, | 608 SkPerlinNoiseShader::CreateTurbulence(baseFrequencyX, baseFrequencyY, nu
mOctaves, seed, |
| 612 stitchTiles ? &tileSize : nullptr); | 609 stitchTiles ? &tileSize : nullptr); |
| 613 | 610 |
| 614 SkPaint paint; | |
| 615 GrColor paintColor; | |
| 616 GrFragmentProcessor* effect; | 611 GrFragmentProcessor* effect; |
| 617 GrPaint grPaint; | 612 GrPaint grPaint; |
| 618 SkAssertResult(shader->asFragmentProcessor(d->fContext, paint, | 613 return shader->asFragmentProcessor(d->fContext, |
| 619 GrTest::TestMatrix(d->fRandom), n
ullptr, | 614 GrTest::TestMatrix(d->fRandom), NULL, |
| 620 &paintColor, grPaint.getProcessor
DataManager(), | 615 kNone_SkFilterQuality, |
| 621 &effect)); | 616 grPaint.getProcessorDataManager()); |
| 622 | 617 |
| 623 delete shader; | 618 delete shader; |
| 624 | 619 |
| 625 return effect; | 620 return effect; |
| 626 } | 621 } |
| 627 | 622 |
| 628 GrGLPerlinNoise::GrGLPerlinNoise(const GrProcessor& processor) | 623 GrGLPerlinNoise::GrGLPerlinNoise(const GrProcessor& processor) |
| 629 : fType(processor.cast<GrPerlinNoiseEffect>().type()) | 624 : fType(processor.cast<GrPerlinNoiseEffect>().type()) |
| 630 , fStitchTiles(processor.cast<GrPerlinNoiseEffect>().stitchTiles()) | 625 , fStitchTiles(processor.cast<GrPerlinNoiseEffect>().stitchTiles()) |
| 631 , fNumOctaves(processor.cast<GrPerlinNoiseEffect>().numOctaves()) { | 626 , fNumOctaves(processor.cast<GrPerlinNoiseEffect>().numOctaves()) { |
| 632 } | 627 } |
| 633 | 628 |
| 634 void GrGLPerlinNoise::emitCode(EmitArgs& args) { | 629 void GrGLPerlinNoise::emitCode(EmitArgs& args) { |
| 635 GrGLFragmentBuilder* fsBuilder = args.fBuilder->getFragmentShaderBuilder(); | 630 GrGLFragmentBuilder* fsBuilder = args.fBuilder->getFragmentShaderBuilder(); |
| 636 SkString vCoords = fsBuilder->ensureFSCoords2D(args.fCoords, 0); | 631 SkString vCoords = fsBuilder->ensureFSCoords2D(args.fCoords, 0); |
| 637 | 632 |
| 638 fBaseFrequencyUni = args.fBuilder->addUniform(GrGLProgramBuilder::kFragment_
Visibility, | 633 fBaseFrequencyUni = args.fBuilder->addUniform(GrGLProgramBuilder::kFragment_
Visibility, |
| 639 kVec2f_GrSLType, kDefault_GrSLPrecis
ion, | 634 kVec2f_GrSLType, kDefault_GrSLPrecis
ion, |
| 640 "baseFrequency"); | 635 "baseFrequency"); |
| 641 const char* baseFrequencyUni = args.fBuilder->getUniformCStr(fBaseFrequencyU
ni); | 636 const char* baseFrequencyUni = args.fBuilder->getUniformCStr(fBaseFrequencyU
ni); |
| 642 fAlphaUni = args.fBuilder->addUniform(GrGLProgramBuilder::kFragment_Visibili
ty, | |
| 643 kFloat_GrSLType, kDefault_GrSLPrecision, | |
| 644 "alpha"); | |
| 645 const char* alphaUni = args.fBuilder->getUniformCStr(fAlphaUni); | |
| 646 | 637 |
| 647 const char* stitchDataUni = nullptr; | 638 const char* stitchDataUni = nullptr; |
| 648 if (fStitchTiles) { | 639 if (fStitchTiles) { |
| 649 fStitchDataUni = args.fBuilder->addUniform(GrGLProgramBuilder::kFragment
_Visibility, | 640 fStitchDataUni = args.fBuilder->addUniform(GrGLProgramBuilder::kFragment
_Visibility, |
| 650 kVec2f_GrSLType, kDefault_GrSLPreci
sion, | 641 kVec2f_GrSLType, kDefault_GrSLPreci
sion, |
| 651 "stitchData"); | 642 "stitchData"); |
| 652 stitchDataUni = args.fBuilder->getUniformCStr(fStitchDataUni); | 643 stitchDataUni = args.fBuilder->getUniformCStr(fStitchDataUni); |
| 653 } | 644 } |
| 654 | 645 |
| 655 // There are 4 lines, so the center of each line is 1/8, 3/8, 5/8 and 7/8 | 646 // There are 4 lines, so the center of each line is 1/8, 3/8, 5/8 and 7/8 |
| (...skipping 212 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 868 } | 859 } |
| 869 fsBuilder->codeAppend("\n\t\t}"); // end of the for loop on octaves | 860 fsBuilder->codeAppend("\n\t\t}"); // end of the for loop on octaves |
| 870 | 861 |
| 871 if (fType == SkPerlinNoiseShader::kFractalNoise_Type) { | 862 if (fType == SkPerlinNoiseShader::kFractalNoise_Type) { |
| 872 // The value of turbulenceFunctionResult comes from ((turbulenceFunction
Result) + 1) / 2 | 863 // The value of turbulenceFunctionResult comes from ((turbulenceFunction
Result) + 1) / 2 |
| 873 // by fractalNoise and (turbulenceFunctionResult) by turbulence. | 864 // by fractalNoise and (turbulenceFunctionResult) by turbulence. |
| 874 fsBuilder->codeAppendf("\n\t\t%s = %s * vec4(0.5) + vec4(0.5);", | 865 fsBuilder->codeAppendf("\n\t\t%s = %s * vec4(0.5) + vec4(0.5);", |
| 875 args.fOutputColor,args.fOutputColor); | 866 args.fOutputColor,args.fOutputColor); |
| 876 } | 867 } |
| 877 | 868 |
| 878 fsBuilder->codeAppendf("\n\t\t%s.a *= %s;", args.fOutputColor, alphaUni); | |
| 879 | |
| 880 // Clamp values | 869 // Clamp values |
| 881 fsBuilder->codeAppendf("\n\t\t%s = clamp(%s, 0.0, 1.0);", args.fOutputColor,
args.fOutputColor); | 870 fsBuilder->codeAppendf("\n\t\t%s = clamp(%s, 0.0, 1.0);", args.fOutputColor,
args.fOutputColor); |
| 882 | 871 |
| 883 // Pre-multiply the result | 872 // Pre-multiply the result |
| 884 fsBuilder->codeAppendf("\n\t\t%s = vec4(%s.rgb * %s.aaa, %s.a);\n", | 873 fsBuilder->codeAppendf("\n\t\t%s = vec4(%s.rgb * %s.aaa, %s.a);\n", |
| 885 args.fOutputColor, args.fOutputColor, | 874 args.fOutputColor, args.fOutputColor, |
| 886 args.fOutputColor, args.fOutputColor); | 875 args.fOutputColor, args.fOutputColor); |
| 887 } | 876 } |
| 888 | 877 |
| 889 void GrGLPerlinNoise::GenKey(const GrProcessor& processor, const GrGLSLCaps&, | 878 void GrGLPerlinNoise::GenKey(const GrProcessor& processor, const GrGLSLCaps&, |
| (...skipping 23 matching lines...) Expand all Loading... |
| 913 b->add32(key); | 902 b->add32(key); |
| 914 } | 903 } |
| 915 | 904 |
| 916 void GrGLPerlinNoise::onSetData(const GrGLProgramDataManager& pdman, const GrPro
cessor& processor) { | 905 void GrGLPerlinNoise::onSetData(const GrGLProgramDataManager& pdman, const GrPro
cessor& processor) { |
| 917 INHERITED::onSetData(pdman, processor); | 906 INHERITED::onSetData(pdman, processor); |
| 918 | 907 |
| 919 const GrPerlinNoiseEffect& turbulence = processor.cast<GrPerlinNoiseEffect>(
); | 908 const GrPerlinNoiseEffect& turbulence = processor.cast<GrPerlinNoiseEffect>(
); |
| 920 | 909 |
| 921 const SkVector& baseFrequency = turbulence.baseFrequency(); | 910 const SkVector& baseFrequency = turbulence.baseFrequency(); |
| 922 pdman.set2f(fBaseFrequencyUni, baseFrequency.fX, baseFrequency.fY); | 911 pdman.set2f(fBaseFrequencyUni, baseFrequency.fX, baseFrequency.fY); |
| 923 pdman.set1f(fAlphaUni, SkIntToScalar(turbulence.alpha()) / 255); | |
| 924 | 912 |
| 925 if (turbulence.stitchTiles()) { | 913 if (turbulence.stitchTiles()) { |
| 926 const SkPerlinNoiseShader::StitchData& stitchData = turbulence.stitchDat
a(); | 914 const SkPerlinNoiseShader::StitchData& stitchData = turbulence.stitchDat
a(); |
| 927 pdman.set2f(fStitchDataUni, SkIntToScalar(stitchData.fWidth), | 915 pdman.set2f(fStitchDataUni, SkIntToScalar(stitchData.fWidth), |
| 928 SkIntToScalar(stitchData.fHeight)); | 916 SkIntToScalar(stitchData.fHeight)); |
| 929 } | 917 } |
| 930 } | 918 } |
| 931 | 919 |
| 932 ///////////////////////////////////////////////////////////////////// | 920 ///////////////////////////////////////////////////////////////////// |
| 933 | 921 const GrFragmentProcessor* SkPerlinNoiseShader::asFragmentProcessor(GrContext* c
ontext, |
| 934 bool SkPerlinNoiseShader::asFragmentProcessor(GrContext* context, const SkPaint&
paint, | 922 const SkMatrix& viewM, const SkMatrix* externalLocalMatrix, SkFilterQuality, |
| 935 const SkMatrix& viewM, | 923 GrProcessorDataManager* procDataManager) const { |
| 936 const SkMatrix* externalLocalMatri
x, | |
| 937 GrColor* paintColor, | |
| 938 GrProcessorDataManager* procDataMa
nager, | |
| 939 GrFragmentProcessor** fp) const { | |
| 940 SkASSERT(context); | 924 SkASSERT(context); |
| 941 | 925 |
| 942 *paintColor = SkColor2GrColorJustAlpha(paint.getColor()); | |
| 943 | |
| 944 SkMatrix localMatrix = this->getLocalMatrix(); | 926 SkMatrix localMatrix = this->getLocalMatrix(); |
| 945 if (externalLocalMatrix) { | 927 if (externalLocalMatrix) { |
| 946 localMatrix.preConcat(*externalLocalMatrix); | 928 localMatrix.preConcat(*externalLocalMatrix); |
| 947 } | 929 } |
| 948 | 930 |
| 949 SkMatrix matrix = viewM; | 931 SkMatrix matrix = viewM; |
| 950 matrix.preConcat(localMatrix); | 932 matrix.preConcat(localMatrix); |
| 951 | 933 |
| 952 if (0 == fNumOctaves) { | 934 if (0 == fNumOctaves) { |
| 953 if (kFractalNoise_Type == fType) { | 935 if (kFractalNoise_Type == fType) { |
| 954 uint32_t alpha = paint.getAlpha() >> 1; | 936 // Extract the incoming alpha and emit rgba = (a/4, a/4, a/4, a/2) |
| 955 uint32_t rgb = alpha >> 1; | 937 SkAutoTUnref<const GrFragmentProcessor> inner( |
| 956 *paintColor = GrColorPackRGBA(rgb, rgb, rgb, alpha); | 938 GrConstColorProcessor::Create(0x80404040, |
| 957 } else { | 939 GrConstColorProcessor::kModulateRG
BA_InputMode)); |
| 958 *paintColor = 0; | 940 return GrExtractAlphaFragmentProcessor::Create(inner); |
| 959 } | 941 } |
| 960 return true; | 942 // Emit zero. |
| 943 return GrConstColorProcessor::Create(0x0, GrConstColorProcessor::kIgnore
_InputMode); |
| 961 } | 944 } |
| 962 | 945 |
| 963 // Either we don't stitch tiles, either we have a valid tile size | 946 // Either we don't stitch tiles, either we have a valid tile size |
| 964 SkASSERT(!fStitchTiles || !fTileSize.isEmpty()); | 947 SkASSERT(!fStitchTiles || !fTileSize.isEmpty()); |
| 965 | 948 |
| 966 SkPerlinNoiseShader::PaintingData* paintingData = | 949 SkPerlinNoiseShader::PaintingData* paintingData = |
| 967 new PaintingData(fTileSize, fSeed, fBaseFrequencyX, fBaseFrequencyY,
matrix); | 950 new PaintingData(fTileSize, fSeed, fBaseFrequencyX, fBaseFrequencyY,
matrix); |
| 968 SkAutoTUnref<GrTexture> permutationsTexture( | 951 SkAutoTUnref<GrTexture> permutationsTexture( |
| 969 GrRefCachedBitmapTexture(context, paintingData->getPermutationsBitmap(),
nullptr)); | 952 GrRefCachedBitmapTexture(context, paintingData->getPermutationsBitmap(),
nullptr)); |
| 970 SkAutoTUnref<GrTexture> noiseTexture( | 953 SkAutoTUnref<GrTexture> noiseTexture( |
| 971 GrRefCachedBitmapTexture(context, paintingData->getNoiseBitmap(), nullpt
r)); | 954 GrRefCachedBitmapTexture(context, paintingData->getNoiseBitmap(), nullpt
r)); |
| 972 | 955 |
| 973 SkMatrix m = viewM; | 956 SkMatrix m = viewM; |
| 974 m.setTranslateX(-localMatrix.getTranslateX() + SK_Scalar1); | 957 m.setTranslateX(-localMatrix.getTranslateX() + SK_Scalar1); |
| 975 m.setTranslateY(-localMatrix.getTranslateY() + SK_Scalar1); | 958 m.setTranslateY(-localMatrix.getTranslateY() + SK_Scalar1); |
| 976 if ((permutationsTexture) && (noiseTexture)) { | 959 if ((permutationsTexture) && (noiseTexture)) { |
| 977 *fp = GrPerlinNoiseEffect::Create(procDataManager, | 960 SkAutoTUnref<GrFragmentProcessor> inner( |
| 978 fType, | 961 GrPerlinNoiseEffect::Create(procDataManager, |
| 979 fNumOctaves, | 962 fType, |
| 980 fStitchTiles, | 963 fNumOctaves, |
| 981 paintingData, | 964 fStitchTiles, |
| 982 permutationsTexture, noiseTexture, | 965 paintingData, |
| 983 m, paint.getAlpha()); | 966 permutationsTexture, noiseTexture, |
| 984 } else { | 967 m)); |
| 985 delete paintingData; | 968 return GrExtractAlphaFragmentProcessor::Create(inner); |
| 986 *fp = nullptr; | |
| 987 } | 969 } |
| 988 return true; | 970 delete paintingData; |
| 989 } | 971 return nullptr; |
| 990 | |
| 991 #else | |
| 992 | |
| 993 bool SkPerlinNoiseShader::asFragmentProcessor(GrContext*, const SkPaint&, const
SkMatrix&, | |
| 994 const SkMatrix*, GrColor*, GrProce
ssorDataManager*, | |
| 995 GrFragmentProcessor**) const { | |
| 996 SkDEBUGFAIL("Should not call in GPU-less build"); | |
| 997 return false; | |
| 998 } | 972 } |
| 999 | 973 |
| 1000 #endif | 974 #endif |
| 1001 | 975 |
| 1002 #ifndef SK_IGNORE_TO_STRING | 976 #ifndef SK_IGNORE_TO_STRING |
| 1003 void SkPerlinNoiseShader::toString(SkString* str) const { | 977 void SkPerlinNoiseShader::toString(SkString* str) const { |
| 1004 str->append("SkPerlinNoiseShader: ("); | 978 str->append("SkPerlinNoiseShader: ("); |
| 1005 | 979 |
| 1006 str->append("type: "); | 980 str->append("type: "); |
| 1007 switch (fType) { | 981 switch (fType) { |
| (...skipping 16 matching lines...) Expand all Loading... |
| 1024 str->append(" seed: "); | 998 str->append(" seed: "); |
| 1025 str->appendScalar(fSeed); | 999 str->appendScalar(fSeed); |
| 1026 str->append(" stitch tiles: "); | 1000 str->append(" stitch tiles: "); |
| 1027 str->append(fStitchTiles ? "true " : "false "); | 1001 str->append(fStitchTiles ? "true " : "false "); |
| 1028 | 1002 |
| 1029 this->INHERITED::toString(str); | 1003 this->INHERITED::toString(str); |
| 1030 | 1004 |
| 1031 str->append(")"); | 1005 str->append(")"); |
| 1032 } | 1006 } |
| 1033 #endif | 1007 #endif |
| OLD | NEW |