| 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 "gl/GrGLEffect.h" | 20 #include "gl/GrGLProcessor.h" |
| 21 #include "gl/builders/GrGLProgramBuilder.h" | 21 #include "gl/builders/GrGLProgramBuilder.h" |
| 22 #include "GrTBackendEffectFactory.h" | 22 #include "GrTBackendProcessorFactory.h" |
| 23 #include "SkGr.h" | 23 #include "SkGr.h" |
| 24 #endif | 24 #endif |
| 25 | 25 |
| 26 static const int kBlockSize = 256; | 26 static const int kBlockSize = 256; |
| 27 static const int kBlockMask = kBlockSize - 1; | 27 static const int kBlockMask = kBlockSize - 1; |
| 28 static const int kPerlinNoise = 4096; | 28 static const int kPerlinNoise = 4096; |
| 29 static const int kRandMaximum = SK_MaxS32; // 2**31 - 1 | 29 static const int kRandMaximum = SK_MaxS32; // 2**31 - 1 |
| 30 | 30 |
| 31 namespace { | 31 namespace { |
| 32 | 32 |
| (...skipping 467 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 500 result[i] = SkDitherRGB32To565(shade(point, stitchData), dither); | 500 result[i] = SkDitherRGB32To565(shade(point, stitchData), dither); |
| 501 DITHER_INC_X(x); | 501 DITHER_INC_X(x); |
| 502 point.fX += SK_Scalar1; | 502 point.fX += SK_Scalar1; |
| 503 } | 503 } |
| 504 } | 504 } |
| 505 | 505 |
| 506 ///////////////////////////////////////////////////////////////////// | 506 ///////////////////////////////////////////////////////////////////// |
| 507 | 507 |
| 508 #if SK_SUPPORT_GPU | 508 #if SK_SUPPORT_GPU |
| 509 | 509 |
| 510 #include "GrTBackendEffectFactory.h" | 510 #include "GrTBackendProcessorFactory.h" |
| 511 | 511 |
| 512 class GrGLPerlinNoise : public GrGLEffect { | 512 class GrGLPerlinNoise : public GrGLFragmentProcessor { |
| 513 public: | 513 public: |
| 514 GrGLPerlinNoise(const GrBackendEffectFactory& factory, | 514 GrGLPerlinNoise(const GrBackendProcessorFactory&, |
| 515 const GrEffect& effect); | 515 const GrProcessor&); |
| 516 virtual ~GrGLPerlinNoise() {} | 516 virtual ~GrGLPerlinNoise() {} |
| 517 | 517 |
| 518 virtual void emitCode(GrGLProgramBuilder*, | 518 virtual void emitCode(GrGLProgramBuilder*, |
| 519 const GrEffect&, | 519 const GrFragmentProcessor&, |
| 520 const GrEffectKey&, | 520 const GrProcessorKey&, |
| 521 const char* outputColor, | 521 const char* outputColor, |
| 522 const char* inputColor, | 522 const char* inputColor, |
| 523 const TransformedCoordsArray&, | 523 const TransformedCoordsArray&, |
| 524 const TextureSamplerArray&) SK_OVERRIDE; | 524 const TextureSamplerArray&) SK_OVERRIDE; |
| 525 | 525 |
| 526 virtual void setData(const GrGLProgramDataManager&, const GrEffect&) SK_OVER
RIDE; | 526 virtual void setData(const GrGLProgramDataManager&, const GrProcessor&) SK_O
VERRIDE; |
| 527 | 527 |
| 528 static inline void GenKey(const GrEffect&, const GrGLCaps&, GrEffectKeyBuild
er* b); | 528 static inline void GenKey(const GrProcessor&, const GrGLCaps&, GrProcessorKe
yBuilder* b); |
| 529 | 529 |
| 530 private: | 530 private: |
| 531 | 531 |
| 532 GrGLProgramDataManager::UniformHandle fStitchDataUni; | 532 GrGLProgramDataManager::UniformHandle fStitchDataUni; |
| 533 SkPerlinNoiseShader::Type fType; | 533 SkPerlinNoiseShader::Type fType; |
| 534 bool fStitchTiles; | 534 bool fStitchTiles; |
| 535 int fNumOctaves; | 535 int fNumOctaves; |
| 536 GrGLProgramDataManager::UniformHandle fBaseFrequencyUni; | 536 GrGLProgramDataManager::UniformHandle fBaseFrequencyUni; |
| 537 GrGLProgramDataManager::UniformHandle fAlphaUni; | 537 GrGLProgramDataManager::UniformHandle fAlphaUni; |
| 538 | 538 |
| 539 private: | 539 private: |
| 540 typedef GrGLEffect INHERITED; | 540 typedef GrGLFragmentProcessor INHERITED; |
| 541 }; | 541 }; |
| 542 | 542 |
| 543 ///////////////////////////////////////////////////////////////////// | 543 ///////////////////////////////////////////////////////////////////// |
| 544 | 544 |
| 545 class GrPerlinNoiseEffect : public GrEffect { | 545 class GrPerlinNoiseEffect : public GrFragmentProcessor { |
| 546 public: | 546 public: |
| 547 static GrEffect* Create(SkPerlinNoiseShader::Type type, | 547 static GrFragmentProcessor* Create(SkPerlinNoiseShader::Type type, |
| 548 int numOctaves, bool stitchTiles, | 548 int numOctaves, bool stitchTiles, |
| 549 SkPerlinNoiseShader::PaintingData* paintingData, | 549 SkPerlinNoiseShader::PaintingData* painti
ngData, |
| 550 GrTexture* permutationsTexture, GrTexture* noiseText
ure, | 550 GrTexture* permutationsTexture, GrTexture
* noiseTexture, |
| 551 const SkMatrix& matrix, uint8_t alpha) { | 551 const SkMatrix& matrix, uint8_t alpha) { |
| 552 return SkNEW_ARGS(GrPerlinNoiseEffect, (type, numOctaves, stitchTiles, p
aintingData, | 552 return SkNEW_ARGS(GrPerlinNoiseEffect, (type, numOctaves, stitchTiles, p
aintingData, |
| 553 permutationsTexture, noiseTextur
e, matrix, alpha)); | 553 permutationsTexture, noiseTextur
e, matrix, alpha)); |
| 554 } | 554 } |
| 555 | 555 |
| 556 virtual ~GrPerlinNoiseEffect() { | 556 virtual ~GrPerlinNoiseEffect() { |
| 557 SkDELETE(fPaintingData); | 557 SkDELETE(fPaintingData); |
| 558 } | 558 } |
| 559 | 559 |
| 560 static const char* Name() { return "PerlinNoise"; } | 560 static const char* Name() { return "PerlinNoise"; } |
| 561 virtual const GrBackendEffectFactory& getFactory() const SK_OVERRIDE { | 561 virtual const GrBackendFragmentProcessorFactory& getFactory() const SK_OVERR
IDE { |
| 562 return GrTBackendEffectFactory<GrPerlinNoiseEffect>::getInstance(); | 562 return GrTBackendFragmentProcessorFactory<GrPerlinNoiseEffect>::getInsta
nce(); |
| 563 } | 563 } |
| 564 const SkPerlinNoiseShader::StitchData& stitchData() const { return fPainting
Data->fStitchDataInit; } | 564 const SkPerlinNoiseShader::StitchData& stitchData() const { return fPainting
Data->fStitchDataInit; } |
| 565 | 565 |
| 566 SkPerlinNoiseShader::Type type() const { return fType; } | 566 SkPerlinNoiseShader::Type type() const { return fType; } |
| 567 bool stitchTiles() const { return fStitchTiles; } | 567 bool stitchTiles() const { return fStitchTiles; } |
| 568 const SkVector& baseFrequency() const { return fPaintingData->fBaseFrequency
; } | 568 const SkVector& baseFrequency() const { return fPaintingData->fBaseFrequency
; } |
| 569 int numOctaves() const { return fNumOctaves; } | 569 int numOctaves() const { return fNumOctaves; } |
| 570 const SkMatrix& matrix() const { return fCoordTransform.getMatrix(); } | 570 const SkMatrix& matrix() const { return fCoordTransform.getMatrix(); } |
| 571 uint8_t alpha() const { return fAlpha; } | 571 uint8_t alpha() const { return fAlpha; } |
| 572 | 572 |
| 573 typedef GrGLPerlinNoise GLEffect; | 573 typedef GrGLPerlinNoise GLProcessor; |
| 574 | 574 |
| 575 private: | 575 private: |
| 576 virtual bool onIsEqual(const GrEffect& sBase) const SK_OVERRIDE { | 576 virtual bool onIsEqual(const GrProcessor& sBase) const SK_OVERRIDE { |
| 577 const GrPerlinNoiseEffect& s = sBase.cast<GrPerlinNoiseEffect>(); | 577 const GrPerlinNoiseEffect& s = sBase.cast<GrPerlinNoiseEffect>(); |
| 578 return fType == s.fType && | 578 return fType == s.fType && |
| 579 fPaintingData->fBaseFrequency == s.fPaintingData->fBaseFrequency
&& | 579 fPaintingData->fBaseFrequency == s.fPaintingData->fBaseFrequency
&& |
| 580 fNumOctaves == s.fNumOctaves && | 580 fNumOctaves == s.fNumOctaves && |
| 581 fStitchTiles == s.fStitchTiles && | 581 fStitchTiles == s.fStitchTiles && |
| 582 fCoordTransform.getMatrix() == s.fCoordTransform.getMatrix() && | 582 fCoordTransform.getMatrix() == s.fCoordTransform.getMatrix() && |
| 583 fAlpha == s.fAlpha && | 583 fAlpha == s.fAlpha && |
| 584 fPermutationsAccess.getTexture() == s.fPermutationsAccess.getText
ure() && | 584 fPermutationsAccess.getTexture() == s.fPermutationsAccess.getText
ure() && |
| 585 fNoiseAccess.getTexture() == s.fNoiseAccess.getTexture() && | 585 fNoiseAccess.getTexture() == s.fNoiseAccess.getTexture() && |
| 586 fPaintingData->fStitchDataInit == s.fPaintingData->fStitchDataIni
t; | 586 fPaintingData->fStitchDataInit == s.fPaintingData->fStitchDataIni
t; |
| (...skipping 11 matching lines...) Expand all Loading... |
| 598 , fPermutationsAccess(permutationsTexture) | 598 , fPermutationsAccess(permutationsTexture) |
| 599 , fNoiseAccess(noiseTexture) | 599 , fNoiseAccess(noiseTexture) |
| 600 , fPaintingData(paintingData) { | 600 , fPaintingData(paintingData) { |
| 601 this->addTextureAccess(&fPermutationsAccess); | 601 this->addTextureAccess(&fPermutationsAccess); |
| 602 this->addTextureAccess(&fNoiseAccess); | 602 this->addTextureAccess(&fNoiseAccess); |
| 603 fCoordTransform.reset(kLocal_GrCoordSet, matrix); | 603 fCoordTransform.reset(kLocal_GrCoordSet, matrix); |
| 604 this->addCoordTransform(&fCoordTransform); | 604 this->addCoordTransform(&fCoordTransform); |
| 605 this->setWillNotUseInputColor(); | 605 this->setWillNotUseInputColor(); |
| 606 } | 606 } |
| 607 | 607 |
| 608 GR_DECLARE_EFFECT_TEST; | 608 GR_DECLARE_FRAGMENT_PROCESSOR_TEST; |
| 609 | 609 |
| 610 SkPerlinNoiseShader::Type fType; | 610 SkPerlinNoiseShader::Type fType; |
| 611 GrCoordTransform fCoordTransform; | 611 GrCoordTransform fCoordTransform; |
| 612 int fNumOctaves; | 612 int fNumOctaves; |
| 613 bool fStitchTiles; | 613 bool fStitchTiles; |
| 614 uint8_t fAlpha; | 614 uint8_t fAlpha; |
| 615 GrTextureAccess fPermutationsAccess; | 615 GrTextureAccess fPermutationsAccess; |
| 616 GrTextureAccess fNoiseAccess; | 616 GrTextureAccess fNoiseAccess; |
| 617 SkPerlinNoiseShader::PaintingData *fPaintingData; | 617 SkPerlinNoiseShader::PaintingData *fPaintingData; |
| 618 | 618 |
| 619 void getConstantColorComponents(GrColor*, uint32_t* validFlags) const SK_OVE
RRIDE { | 619 void getConstantColorComponents(GrColor*, uint32_t* validFlags) const SK_OVE
RRIDE { |
| 620 *validFlags = 0; // This is noise. Nothing is constant. | 620 *validFlags = 0; // This is noise. Nothing is constant. |
| 621 } | 621 } |
| 622 | 622 |
| 623 private: | 623 private: |
| 624 typedef GrEffect INHERITED; | 624 typedef GrFragmentProcessor INHERITED; |
| 625 }; | 625 }; |
| 626 | 626 |
| 627 ///////////////////////////////////////////////////////////////////// | 627 ///////////////////////////////////////////////////////////////////// |
| 628 GR_DEFINE_EFFECT_TEST(GrPerlinNoiseEffect); | 628 GR_DEFINE_FRAGMENT_PROCESSOR_TEST(GrPerlinNoiseEffect); |
| 629 | 629 |
| 630 GrEffect* GrPerlinNoiseEffect::TestCreate(SkRandom* random, | 630 GrFragmentProcessor* GrPerlinNoiseEffect::TestCreate(SkRandom* random, |
| 631 GrContext* context, | 631 GrContext* context, |
| 632 const GrDrawTargetCaps&, | 632 const GrDrawTargetCaps&, |
| 633 GrTexture**) { | 633 GrTexture**) { |
| 634 int numOctaves = random->nextRangeU(2, 10); | 634 int numOctaves = random->nextRangeU(2, 10); |
| 635 bool stitchTiles = random->nextBool(); | 635 bool stitchTiles = random->nextBool(); |
| 636 SkScalar seed = SkIntToScalar(random->nextU()); | 636 SkScalar seed = SkIntToScalar(random->nextU()); |
| 637 SkISize tileSize = SkISize::Make(random->nextRangeU(4, 4096), random->nextR
angeU(4, 4096)); | 637 SkISize tileSize = SkISize::Make(random->nextRangeU(4, 4096), random->nextR
angeU(4, 4096)); |
| 638 SkScalar baseFrequencyX = random->nextRangeScalar(0.01f, | 638 SkScalar baseFrequencyX = random->nextRangeScalar(0.01f, |
| 639 0.99f); | 639 0.99f); |
| 640 SkScalar baseFrequencyY = random->nextRangeScalar(0.01f, | 640 SkScalar baseFrequencyY = random->nextRangeScalar(0.01f, |
| 641 0.99f); | 641 0.99f); |
| 642 | 642 |
| 643 SkShader* shader = random->nextBool() ? | 643 SkShader* shader = random->nextBool() ? |
| 644 SkPerlinNoiseShader::CreateFractalNoise(baseFrequencyX, baseFrequencyY,
numOctaves, seed, | 644 SkPerlinNoiseShader::CreateFractalNoise(baseFrequencyX, baseFrequencyY,
numOctaves, seed, |
| 645 stitchTiles ? &tileSize : NULL)
: | 645 stitchTiles ? &tileSize : NULL)
: |
| 646 SkPerlinNoiseShader::CreateTurbulence(baseFrequencyX, baseFrequencyY, nu
mOctaves, seed, | 646 SkPerlinNoiseShader::CreateTurbulence(baseFrequencyX, baseFrequencyY, nu
mOctaves, seed, |
| 647 stitchTiles ? &tileSize : NULL); | 647 stitchTiles ? &tileSize : NULL); |
| 648 | 648 |
| 649 SkPaint paint; | 649 SkPaint paint; |
| 650 GrColor paintColor; | 650 GrColor paintColor; |
| 651 GrEffect* effect; | 651 GrFragmentProcessor* effect; |
| 652 SkAssertResult(shader->asNewEffect(context, paint, NULL, &paintColor, &effec
t)); | 652 SkAssertResult(shader->asFragmentProcessor(context, paint, NULL, &paintColor
, &effect)); |
| 653 | 653 |
| 654 SkDELETE(shader); | 654 SkDELETE(shader); |
| 655 | 655 |
| 656 return effect; | 656 return effect; |
| 657 } | 657 } |
| 658 | 658 |
| 659 GrGLPerlinNoise::GrGLPerlinNoise(const GrBackendEffectFactory& factory, const Gr
Effect& effect) | 659 GrGLPerlinNoise::GrGLPerlinNoise(const GrBackendProcessorFactory& factory, |
| 660 const GrProcessor& processor) |
| 660 : INHERITED (factory) | 661 : INHERITED (factory) |
| 661 , fType(effect.cast<GrPerlinNoiseEffect>().type()) | 662 , fType(processor.cast<GrPerlinNoiseEffect>().type()) |
| 662 , fStitchTiles(effect.cast<GrPerlinNoiseEffect>().stitchTiles()) | 663 , fStitchTiles(processor.cast<GrPerlinNoiseEffect>().stitchTiles()) |
| 663 , fNumOctaves(effect.cast<GrPerlinNoiseEffect>().numOctaves()) { | 664 , fNumOctaves(processor.cast<GrPerlinNoiseEffect>().numOctaves()) { |
| 664 } | 665 } |
| 665 | 666 |
| 666 void GrGLPerlinNoise::emitCode(GrGLProgramBuilder* builder, | 667 void GrGLPerlinNoise::emitCode(GrGLProgramBuilder* builder, |
| 667 const GrEffect&, | 668 const GrFragmentProcessor&, |
| 668 const GrEffectKey& key, | 669 const GrProcessorKey& key, |
| 669 const char* outputColor, | 670 const char* outputColor, |
| 670 const char* inputColor, | 671 const char* inputColor, |
| 671 const TransformedCoordsArray& coords, | 672 const TransformedCoordsArray& coords, |
| 672 const TextureSamplerArray& samplers) { | 673 const TextureSamplerArray& samplers) { |
| 673 sk_ignore_unused_variable(inputColor); | 674 sk_ignore_unused_variable(inputColor); |
| 674 | 675 |
| 675 GrGLFragmentShaderBuilder* fsBuilder = builder->getFragmentShaderBuilder(); | 676 GrGLFragmentShaderBuilder* fsBuilder = builder->getFragmentShaderBuilder(); |
| 676 SkString vCoords = fsBuilder->ensureFSCoords2D(coords, 0); | 677 SkString vCoords = fsBuilder->ensureFSCoords2D(coords, 0); |
| 677 | 678 |
| 678 fBaseFrequencyUni = builder->addUniform(GrGLProgramBuilder::kFragment_Visibi
lity, | 679 fBaseFrequencyUni = builder->addUniform(GrGLProgramBuilder::kFragment_Visibi
lity, |
| (...skipping 233 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 912 fsBuilder->codeAppendf("\n\t\t%s.a *= %s;", outputColor, alphaUni); | 913 fsBuilder->codeAppendf("\n\t\t%s.a *= %s;", outputColor, alphaUni); |
| 913 | 914 |
| 914 // Clamp values | 915 // Clamp values |
| 915 fsBuilder->codeAppendf("\n\t\t%s = clamp(%s, 0.0, 1.0);", outputColor, outpu
tColor); | 916 fsBuilder->codeAppendf("\n\t\t%s = clamp(%s, 0.0, 1.0);", outputColor, outpu
tColor); |
| 916 | 917 |
| 917 // Pre-multiply the result | 918 // Pre-multiply the result |
| 918 fsBuilder->codeAppendf("\n\t\t%s = vec4(%s.rgb * %s.aaa, %s.a);\n", | 919 fsBuilder->codeAppendf("\n\t\t%s = vec4(%s.rgb * %s.aaa, %s.a);\n", |
| 919 outputColor, outputColor, outputColor, outputColor); | 920 outputColor, outputColor, outputColor, outputColor); |
| 920 } | 921 } |
| 921 | 922 |
| 922 void GrGLPerlinNoise::GenKey(const GrEffect& effect, const GrGLCaps&, GrEffectKe
yBuilder* b) { | 923 void GrGLPerlinNoise::GenKey(const GrProcessor& processor, const GrGLCaps&, |
| 923 const GrPerlinNoiseEffect& turbulence = effect.cast<GrPerlinNoiseEffect>(); | 924 GrProcessorKeyBuilder* b) { |
| 925 const GrPerlinNoiseEffect& turbulence = processor.cast<GrPerlinNoiseEffect>(
); |
| 924 | 926 |
| 925 uint32_t key = turbulence.numOctaves(); | 927 uint32_t key = turbulence.numOctaves(); |
| 926 | 928 |
| 927 key = key << 3; // Make room for next 3 bits | 929 key = key << 3; // Make room for next 3 bits |
| 928 | 930 |
| 929 switch (turbulence.type()) { | 931 switch (turbulence.type()) { |
| 930 case SkPerlinNoiseShader::kFractalNoise_Type: | 932 case SkPerlinNoiseShader::kFractalNoise_Type: |
| 931 key |= 0x1; | 933 key |= 0x1; |
| 932 break; | 934 break; |
| 933 case SkPerlinNoiseShader::kTurbulence_Type: | 935 case SkPerlinNoiseShader::kTurbulence_Type: |
| 934 key |= 0x2; | 936 key |= 0x2; |
| 935 break; | 937 break; |
| 936 default: | 938 default: |
| 937 // leave key at 0 | 939 // leave key at 0 |
| 938 break; | 940 break; |
| 939 } | 941 } |
| 940 | 942 |
| 941 if (turbulence.stitchTiles()) { | 943 if (turbulence.stitchTiles()) { |
| 942 key |= 0x4; // Flip the 3rd bit if tile stitching is on | 944 key |= 0x4; // Flip the 3rd bit if tile stitching is on |
| 943 } | 945 } |
| 944 | 946 |
| 945 b->add32(key); | 947 b->add32(key); |
| 946 } | 948 } |
| 947 | 949 |
| 948 void GrGLPerlinNoise::setData(const GrGLProgramDataManager& pdman, const GrEffec
t& effect) { | 950 void GrGLPerlinNoise::setData(const GrGLProgramDataManager& pdman, const GrProce
ssor& processor) { |
| 949 INHERITED::setData(pdman, effect); | 951 INHERITED::setData(pdman, processor); |
| 950 | 952 |
| 951 const GrPerlinNoiseEffect& turbulence = effect.cast<GrPerlinNoiseEffect>(); | 953 const GrPerlinNoiseEffect& turbulence = processor.cast<GrPerlinNoiseEffect>(
); |
| 952 | 954 |
| 953 const SkVector& baseFrequency = turbulence.baseFrequency(); | 955 const SkVector& baseFrequency = turbulence.baseFrequency(); |
| 954 pdman.set2f(fBaseFrequencyUni, baseFrequency.fX, baseFrequency.fY); | 956 pdman.set2f(fBaseFrequencyUni, baseFrequency.fX, baseFrequency.fY); |
| 955 pdman.set1f(fAlphaUni, SkScalarDiv(SkIntToScalar(turbulence.alpha()), SkIntT
oScalar(255))); | 957 pdman.set1f(fAlphaUni, SkScalarDiv(SkIntToScalar(turbulence.alpha()), SkIntT
oScalar(255))); |
| 956 | 958 |
| 957 if (turbulence.stitchTiles()) { | 959 if (turbulence.stitchTiles()) { |
| 958 const SkPerlinNoiseShader::StitchData& stitchData = turbulence.stitchDat
a(); | 960 const SkPerlinNoiseShader::StitchData& stitchData = turbulence.stitchDat
a(); |
| 959 pdman.set2f(fStitchDataUni, SkIntToScalar(stitchData.fWidth), | 961 pdman.set2f(fStitchDataUni, SkIntToScalar(stitchData.fWidth), |
| 960 SkIntToScalar(stitchData.fHeight)); | 962 SkIntToScalar(stitchData.fHeight)); |
| 961 } | 963 } |
| 962 } | 964 } |
| 963 | 965 |
| 964 ///////////////////////////////////////////////////////////////////// | 966 ///////////////////////////////////////////////////////////////////// |
| 965 | 967 |
| 966 bool SkPerlinNoiseShader::asNewEffect(GrContext* context, const SkPaint& paint, | 968 bool SkPerlinNoiseShader::asFragmentProcessor(GrContext* context, const SkPaint&
paint, |
| 967 const SkMatrix* externalLocalMatrix, GrCol
or* paintColor, | 969 const SkMatrix* externalLocalMatri
x, |
| 968 GrEffect** effect) const { | 970 GrColor* paintColor, GrFragmentPro
cessor** fp) const { |
| 969 SkASSERT(context); | 971 SkASSERT(context); |
| 970 | 972 |
| 971 *paintColor = SkColor2GrColorJustAlpha(paint.getColor()); | 973 *paintColor = SkColor2GrColorJustAlpha(paint.getColor()); |
| 972 | 974 |
| 973 SkMatrix localMatrix = this->getLocalMatrix(); | 975 SkMatrix localMatrix = this->getLocalMatrix(); |
| 974 if (externalLocalMatrix) { | 976 if (externalLocalMatrix) { |
| 975 localMatrix.preConcat(*externalLocalMatrix); | 977 localMatrix.preConcat(*externalLocalMatrix); |
| 976 } | 978 } |
| 977 | 979 |
| 978 SkMatrix matrix = context->getMatrix(); | 980 SkMatrix matrix = context->getMatrix(); |
| 979 matrix.preConcat(localMatrix); | 981 matrix.preConcat(localMatrix); |
| 980 | 982 |
| 981 if (0 == fNumOctaves) { | 983 if (0 == fNumOctaves) { |
| 982 SkColor clearColor = 0; | 984 SkColor clearColor = 0; |
| 983 if (kFractalNoise_Type == fType) { | 985 if (kFractalNoise_Type == fType) { |
| 984 clearColor = SkColorSetARGB(paint.getAlpha() / 2, 127, 127, 127); | 986 clearColor = SkColorSetARGB(paint.getAlpha() / 2, 127, 127, 127); |
| 985 } | 987 } |
| 986 SkAutoTUnref<SkColorFilter> cf(SkColorFilter::CreateModeFilter( | 988 SkAutoTUnref<SkColorFilter> cf(SkColorFilter::CreateModeFilter( |
| 987 clearColor, SkXfermode::kSrc_Mod
e)); | 989 clearColor, SkXfermode::kSrc_Mod
e)); |
| 988 *effect = cf->asNewEffect(context); | 990 *fp = cf->asFragmentProcessor(context); |
| 989 return true; | 991 return true; |
| 990 } | 992 } |
| 991 | 993 |
| 992 // Either we don't stitch tiles, either we have a valid tile size | 994 // Either we don't stitch tiles, either we have a valid tile size |
| 993 SkASSERT(!fStitchTiles || !fTileSize.isEmpty()); | 995 SkASSERT(!fStitchTiles || !fTileSize.isEmpty()); |
| 994 | 996 |
| 995 SkPerlinNoiseShader::PaintingData* paintingData = SkNEW_ARGS(PaintingData, (
fTileSize, fSeed, fBaseFrequencyX, fBaseFrequencyY, matrix)); | 997 SkPerlinNoiseShader::PaintingData* paintingData = |
| 998 SkNEW_ARGS(PaintingData, (fTileSize, fSeed, fBaseFrequencyX, fBaseFr
equencyY, matrix)); |
| 996 GrTexture* permutationsTexture = GrLockAndRefCachedBitmapTexture( | 999 GrTexture* permutationsTexture = GrLockAndRefCachedBitmapTexture( |
| 997 context, paintingData->getPermutationsBitmap(), NULL); | 1000 context, paintingData->getPermutationsBitmap(), NULL); |
| 998 GrTexture* noiseTexture = GrLockAndRefCachedBitmapTexture( | 1001 GrTexture* noiseTexture = GrLockAndRefCachedBitmapTexture( |
| 999 context, paintingData->getNoiseBitmap(), NULL); | 1002 context, paintingData->getNoiseBitmap(), NULL); |
| 1000 | 1003 |
| 1001 SkMatrix m = context->getMatrix(); | 1004 SkMatrix m = context->getMatrix(); |
| 1002 m.setTranslateX(-localMatrix.getTranslateX() + SK_Scalar1); | 1005 m.setTranslateX(-localMatrix.getTranslateX() + SK_Scalar1); |
| 1003 m.setTranslateY(-localMatrix.getTranslateY() + SK_Scalar1); | 1006 m.setTranslateY(-localMatrix.getTranslateY() + SK_Scalar1); |
| 1004 if ((permutationsTexture) && (noiseTexture)) { | 1007 if ((permutationsTexture) && (noiseTexture)) { |
| 1005 *effect = GrPerlinNoiseEffect::Create(fType, | 1008 *fp = GrPerlinNoiseEffect::Create(fType, |
| 1006 fNumOctaves, | 1009 fNumOctaves, |
| 1007 fStitchTiles, | 1010 fStitchTiles, |
| 1008 paintingData, | 1011 paintingData, |
| 1009 permutationsTexture, noiseTextur
e, | 1012 permutationsTexture, noiseTexture, |
| 1010 m, paint.getAlpha()); | 1013 m, paint.getAlpha()); |
| 1011 } else { | 1014 } else { |
| 1012 SkDELETE(paintingData); | 1015 SkDELETE(paintingData); |
| 1013 *effect = NULL; | 1016 *fp = NULL; |
| 1014 } | 1017 } |
| 1015 | 1018 |
| 1016 // Unlock immediately, this is not great, but we don't have a way of | 1019 // Unlock immediately, this is not great, but we don't have a way of |
| 1017 // knowing when else to unlock it currently. TODO: Remove this when | 1020 // knowing when else to unlock it currently. TODO: Remove this when |
| 1018 // unref becomes the unlock replacement for all types of textures. | 1021 // unref becomes the unlock replacement for all types of textures. |
| 1019 if (permutationsTexture) { | 1022 if (permutationsTexture) { |
| 1020 GrUnlockAndUnrefCachedBitmapTexture(permutationsTexture); | 1023 GrUnlockAndUnrefCachedBitmapTexture(permutationsTexture); |
| 1021 } | 1024 } |
| 1022 if (noiseTexture) { | 1025 if (noiseTexture) { |
| 1023 GrUnlockAndUnrefCachedBitmapTexture(noiseTexture); | 1026 GrUnlockAndUnrefCachedBitmapTexture(noiseTexture); |
| 1024 } | 1027 } |
| 1025 | 1028 |
| 1026 return true; | 1029 return true; |
| 1027 } | 1030 } |
| 1028 | 1031 |
| 1029 #else | 1032 #else |
| 1030 | 1033 |
| 1031 bool SkPerlinNoiseShader::asNewEffect(GrContext* context, const SkPaint& paint, | 1034 bool SkPerlinNoiseShader::asFragmentProcessor(GrContext*, const SkPaint&, const
SkMatrix*, GrColor*, |
| 1032 const SkMatrix* externalLocalMatrix, GrCol
or* paintColor, | 1035 GrFragmentProcessor**) const { |
| 1033 GrEffect** effect) const { | |
| 1034 SkDEBUGFAIL("Should not call in GPU-less build"); | 1036 SkDEBUGFAIL("Should not call in GPU-less build"); |
| 1035 return false; | 1037 return false; |
| 1036 } | 1038 } |
| 1037 | 1039 |
| 1038 #endif | 1040 #endif |
| 1039 | 1041 |
| 1040 #ifndef SK_IGNORE_TO_STRING | 1042 #ifndef SK_IGNORE_TO_STRING |
| 1041 void SkPerlinNoiseShader::toString(SkString* str) const { | 1043 void SkPerlinNoiseShader::toString(SkString* str) const { |
| 1042 str->append("SkPerlinNoiseShader: ("); | 1044 str->append("SkPerlinNoiseShader: ("); |
| 1043 | 1045 |
| (...skipping 18 matching lines...) Expand all Loading... |
| 1062 str->append(" seed: "); | 1064 str->append(" seed: "); |
| 1063 str->appendScalar(fSeed); | 1065 str->appendScalar(fSeed); |
| 1064 str->append(" stitch tiles: "); | 1066 str->append(" stitch tiles: "); |
| 1065 str->append(fStitchTiles ? "true " : "false "); | 1067 str->append(fStitchTiles ? "true " : "false "); |
| 1066 | 1068 |
| 1067 this->INHERITED::toString(str); | 1069 this->INHERITED::toString(str); |
| 1068 | 1070 |
| 1069 str->append(")"); | 1071 str->append(")"); |
| 1070 } | 1072 } |
| 1071 #endif | 1073 #endif |
| OLD | NEW |