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