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 507 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
518 GrGLUniformManager::UniformHandle fAlphaUni; | 518 GrGLUniformManager::UniformHandle fAlphaUni; |
519 | 519 |
520 private: | 520 private: |
521 typedef GrGLEffect INHERITED; | 521 typedef GrGLEffect INHERITED; |
522 }; | 522 }; |
523 | 523 |
524 ///////////////////////////////////////////////////////////////////// | 524 ///////////////////////////////////////////////////////////////////// |
525 | 525 |
526 class GrPerlinNoiseEffect : public GrEffect { | 526 class GrPerlinNoiseEffect : public GrEffect { |
527 public: | 527 public: |
528 static GrEffectRef* Create(SkPerlinNoiseShader::Type type, | 528 static GrEffect* Create(SkPerlinNoiseShader::Type type, |
529 int numOctaves, bool stitchTiles, | 529 int numOctaves, bool stitchTiles, |
530 SkPerlinNoiseShader::PaintingData* paintingData, | 530 SkPerlinNoiseShader::PaintingData* paintingData, |
531 GrTexture* permutationsTexture, GrTexture* noiseT
exture, | 531 GrTexture* permutationsTexture, GrTexture* noiseText
ure, |
532 const SkMatrix& matrix, uint8_t alpha) { | 532 const SkMatrix& matrix, uint8_t alpha) { |
533 return SkNEW_ARGS(GrPerlinNoiseEffect, (type, numOctaves, stitchTiles, p
aintingData, | 533 return SkNEW_ARGS(GrPerlinNoiseEffect, (type, numOctaves, stitchTiles, p
aintingData, |
534 permutationsTexture, noiseTextur
e, matrix, alpha)); | 534 permutationsTexture, noiseTextur
e, matrix, alpha)); |
535 } | 535 } |
536 | 536 |
537 virtual ~GrPerlinNoiseEffect() { | 537 virtual ~GrPerlinNoiseEffect() { |
538 SkDELETE(fPaintingData); | 538 SkDELETE(fPaintingData); |
539 } | 539 } |
540 | 540 |
541 static const char* Name() { return "PerlinNoise"; } | 541 static const char* Name() { return "PerlinNoise"; } |
542 virtual const GrBackendEffectFactory& getFactory() const SK_OVERRIDE { | 542 virtual const GrBackendEffectFactory& getFactory() const SK_OVERRIDE { |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
601 *validFlags = 0; // This is noise. Nothing is constant. | 601 *validFlags = 0; // This is noise. Nothing is constant. |
602 } | 602 } |
603 | 603 |
604 private: | 604 private: |
605 typedef GrEffect INHERITED; | 605 typedef GrEffect INHERITED; |
606 }; | 606 }; |
607 | 607 |
608 ///////////////////////////////////////////////////////////////////// | 608 ///////////////////////////////////////////////////////////////////// |
609 GR_DEFINE_EFFECT_TEST(GrPerlinNoiseEffect); | 609 GR_DEFINE_EFFECT_TEST(GrPerlinNoiseEffect); |
610 | 610 |
611 GrEffectRef* GrPerlinNoiseEffect::TestCreate(SkRandom* random, | 611 GrEffect* GrPerlinNoiseEffect::TestCreate(SkRandom* random, |
612 GrContext* context, | 612 GrContext* context, |
613 const GrDrawTargetCaps&, | 613 const GrDrawTargetCaps&, |
614 GrTexture**) { | 614 GrTexture**) { |
615 int numOctaves = random->nextRangeU(2, 10); | 615 int numOctaves = random->nextRangeU(2, 10); |
616 bool stitchTiles = random->nextBool(); | 616 bool stitchTiles = random->nextBool(); |
617 SkScalar seed = SkIntToScalar(random->nextU()); | 617 SkScalar seed = SkIntToScalar(random->nextU()); |
618 SkISize tileSize = SkISize::Make(random->nextRangeU(4, 4096), random->nextR
angeU(4, 4096)); | 618 SkISize tileSize = SkISize::Make(random->nextRangeU(4, 4096), random->nextR
angeU(4, 4096)); |
619 SkScalar baseFrequencyX = random->nextRangeScalar(0.01f, | 619 SkScalar baseFrequencyX = random->nextRangeScalar(0.01f, |
620 0.99f); | 620 0.99f); |
621 SkScalar baseFrequencyY = random->nextRangeScalar(0.01f, | 621 SkScalar baseFrequencyY = random->nextRangeScalar(0.01f, |
622 0.99f); | 622 0.99f); |
623 | 623 |
624 SkShader* shader = random->nextBool() ? | 624 SkShader* shader = random->nextBool() ? |
625 SkPerlinNoiseShader::CreateFractalNoise(baseFrequencyX, baseFrequencyY,
numOctaves, seed, | 625 SkPerlinNoiseShader::CreateFractalNoise(baseFrequencyX, baseFrequencyY,
numOctaves, seed, |
626 stitchTiles ? &tileSize : NULL)
: | 626 stitchTiles ? &tileSize : NULL)
: |
627 SkPerlinNoiseShader::CreateTurbulence(baseFrequencyX, baseFrequencyY, nu
mOctaves, seed, | 627 SkPerlinNoiseShader::CreateTurbulence(baseFrequencyX, baseFrequencyY, nu
mOctaves, seed, |
628 stitchTiles ? &tileSize : NULL); | 628 stitchTiles ? &tileSize : NULL); |
629 | 629 |
630 SkPaint paint; | 630 SkPaint paint; |
631 GrColor grColor; | 631 GrColor paintColor; |
632 GrEffectRef* effect; | 632 GrEffect* effect; |
633 shader->asNewEffect(context, paint, NULL, &grColor, &effect); | 633 SkAssertResult(shader->asNewEffect(context, paint, NULL, &paintColor, &effec
t)); |
634 | 634 |
635 SkDELETE(shader); | 635 SkDELETE(shader); |
636 | 636 |
637 return effect; | 637 return effect; |
638 } | 638 } |
639 | 639 |
640 GrGLPerlinNoise::GrGLPerlinNoise(const GrBackendEffectFactory& factory, const Gr
DrawEffect& drawEffect) | 640 GrGLPerlinNoise::GrGLPerlinNoise(const GrBackendEffectFactory& factory, const Gr
DrawEffect& drawEffect) |
641 : INHERITED (factory) | 641 : INHERITED (factory) |
642 , fType(drawEffect.castEffect<GrPerlinNoiseEffect>().type()) | 642 , fType(drawEffect.castEffect<GrPerlinNoiseEffect>().type()) |
643 , fStitchTiles(drawEffect.castEffect<GrPerlinNoiseEffect>().stitchTiles()) | 643 , fStitchTiles(drawEffect.castEffect<GrPerlinNoiseEffect>().stitchTiles()) |
(...skipping 293 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
937 if (turbulence.stitchTiles()) { | 937 if (turbulence.stitchTiles()) { |
938 const SkPerlinNoiseShader::StitchData& stitchData = turbulence.stitchDat
a(); | 938 const SkPerlinNoiseShader::StitchData& stitchData = turbulence.stitchDat
a(); |
939 uman.set2f(fStitchDataUni, SkIntToScalar(stitchData.fWidth), | 939 uman.set2f(fStitchDataUni, SkIntToScalar(stitchData.fWidth), |
940 SkIntToScalar(stitchData.fHeight)); | 940 SkIntToScalar(stitchData.fHeight)); |
941 } | 941 } |
942 } | 942 } |
943 | 943 |
944 ///////////////////////////////////////////////////////////////////// | 944 ///////////////////////////////////////////////////////////////////// |
945 | 945 |
946 bool SkPerlinNoiseShader::asNewEffect(GrContext* context, const SkPaint& paint, | 946 bool SkPerlinNoiseShader::asNewEffect(GrContext* context, const SkPaint& paint, |
947 const SkMatrix* externalLocalMatrix, GrCol
or* grColor, | 947 const SkMatrix* externalLocalMatrix, GrCol
or* paintColor, |
948 GrEffect** grEffect) const { | 948 GrEffect** effect) const { |
949 SkASSERT(NULL != context); | 949 SkASSERT(NULL != context); |
950 | 950 |
951 *grColor = SkColor2GrColorJustAlpha(paint.getColor()); | 951 *paintColor = SkColor2GrColorJustAlpha(paint.getColor()); |
952 | 952 |
953 SkMatrix localMatrix = this->getLocalMatrix(); | 953 SkMatrix localMatrix = this->getLocalMatrix(); |
954 if (externalLocalMatrix) { | 954 if (externalLocalMatrix) { |
955 localMatrix.preConcat(*externalLocalMatrix); | 955 localMatrix.preConcat(*externalLocalMatrix); |
956 } | 956 } |
957 | 957 |
958 SkMatrix matrix = context->getMatrix(); | 958 SkMatrix matrix = context->getMatrix(); |
959 matrix.preConcat(localMatrix); | 959 matrix.preConcat(localMatrix); |
960 | 960 |
961 if (0 == fNumOctaves) { | 961 if (0 == fNumOctaves) { |
962 SkColor clearColor = 0; | 962 SkColor clearColor = 0; |
963 if (kFractalNoise_Type == fType) { | 963 if (kFractalNoise_Type == fType) { |
964 clearColor = SkColorSetARGB(paint.getAlpha() / 2, 127, 127, 127); | 964 clearColor = SkColorSetARGB(paint.getAlpha() / 2, 127, 127, 127); |
965 } | 965 } |
966 SkAutoTUnref<SkColorFilter> cf(SkColorFilter::CreateModeFilter( | 966 SkAutoTUnref<SkColorFilter> cf(SkColorFilter::CreateModeFilter( |
967 clearColor, SkXfermode::kSrc_Mod
e)); | 967 clearColor, SkXfermode::kSrc_Mod
e)); |
968 *grEffect = cf->asNewEffect(context); | 968 *effect = cf->asNewEffect(context); |
969 return true; | 969 return true; |
970 } | 970 } |
971 | 971 |
972 // Either we don't stitch tiles, either we have a valid tile size | 972 // Either we don't stitch tiles, either we have a valid tile size |
973 SkASSERT(!fStitchTiles || !fTileSize.isEmpty()); | 973 SkASSERT(!fStitchTiles || !fTileSize.isEmpty()); |
974 | 974 |
975 SkPerlinNoiseShader::PaintingData* paintingData = SkNEW_ARGS(PaintingData, (
fTileSize, fSeed, fBaseFrequencyX, fBaseFrequencyY, matrix)); | 975 SkPerlinNoiseShader::PaintingData* paintingData = SkNEW_ARGS(PaintingData, (
fTileSize, fSeed, fBaseFrequencyX, fBaseFrequencyY, matrix)); |
976 GrTexture* permutationsTexture = GrLockAndRefCachedBitmapTexture( | 976 GrTexture* permutationsTexture = GrLockAndRefCachedBitmapTexture( |
977 context, paintingData->getPermutationsBitmap(), NULL); | 977 context, paintingData->getPermutationsBitmap(), NULL); |
978 GrTexture* noiseTexture = GrLockAndRefCachedBitmapTexture( | 978 GrTexture* noiseTexture = GrLockAndRefCachedBitmapTexture( |
979 context, paintingData->getNoiseBitmap(), NULL); | 979 context, paintingData->getNoiseBitmap(), NULL); |
980 | 980 |
981 SkMatrix m = context->getMatrix(); | 981 SkMatrix m = context->getMatrix(); |
982 m.setTranslateX(-localMatrix.getTranslateX() + SK_Scalar1); | 982 m.setTranslateX(-localMatrix.getTranslateX() + SK_Scalar1); |
983 m.setTranslateY(-localMatrix.getTranslateY() + SK_Scalar1); | 983 m.setTranslateY(-localMatrix.getTranslateY() + SK_Scalar1); |
984 if ((NULL != permutationsTexture) && (NULL != noiseTexture)) { | 984 if ((NULL != permutationsTexture) && (NULL != noiseTexture)) { |
985 *grEffect = GrPerlinNoiseEffect::Create(fType, | 985 *effect = GrPerlinNoiseEffect::Create(fType, |
986 fNumOctaves, | 986 fNumOctaves, |
987 fStitchTiles, | 987 fStitchTiles, |
988 paintingData, | 988 paintingData, |
989 permutationsTexture, noiseTextur
e, | 989 permutationsTexture, noiseTextur
e, |
990 m, paint.getAlpha()); | 990 m, paint.getAlpha()); |
991 } else { | 991 } else { |
992 SkDELETE(paintingData); | 992 SkDELETE(paintingData); |
993 *grEffect = NULL; | 993 *effect = NULL; |
994 } | 994 } |
995 | 995 |
996 // Unlock immediately, this is not great, but we don't have a way of | 996 // Unlock immediately, this is not great, but we don't have a way of |
997 // knowing when else to unlock it currently. TODO: Remove this when | 997 // knowing when else to unlock it currently. TODO: Remove this when |
998 // unref becomes the unlock replacement for all types of textures. | 998 // unref becomes the unlock replacement for all types of textures. |
999 if (NULL != permutationsTexture) { | 999 if (NULL != permutationsTexture) { |
1000 GrUnlockAndUnrefCachedBitmapTexture(permutationsTexture); | 1000 GrUnlockAndUnrefCachedBitmapTexture(permutationsTexture); |
1001 } | 1001 } |
1002 if (NULL != noiseTexture) { | 1002 if (NULL != noiseTexture) { |
1003 GrUnlockAndUnrefCachedBitmapTexture(noiseTexture); | 1003 GrUnlockAndUnrefCachedBitmapTexture(noiseTexture); |
1004 } | 1004 } |
1005 | 1005 |
1006 return true; | 1006 return true; |
1007 } | 1007 } |
1008 | 1008 |
1009 #else | 1009 #else |
1010 | 1010 |
1011 bool SkPerlinNoiseShader::asNewEffect(GrContext* context, const SkPaint& paint, | 1011 bool SkPerlinNoiseShader::asNewEffect(GrContext* context, const SkPaint& paint, |
1012 const SkMatrix* externalLocalMatrix, GrCol
or* grColor, | 1012 const SkMatrix* externalLocalMatrix, GrCol
or* paintColor, |
1013 GrEffect** grEffect) const { | 1013 GrEffect** effect) const { |
1014 SkDEBUGFAIL("Should not call in GPU-less build"); | 1014 SkDEBUGFAIL("Should not call in GPU-less build"); |
1015 return false; | 1015 return false; |
1016 } | 1016 } |
1017 | 1017 |
1018 #endif | 1018 #endif |
1019 | 1019 |
1020 #ifndef SK_IGNORE_TO_STRING | 1020 #ifndef SK_IGNORE_TO_STRING |
1021 void SkPerlinNoiseShader::toString(SkString* str) const { | 1021 void SkPerlinNoiseShader::toString(SkString* str) const { |
1022 str->append("SkPerlinNoiseShader: ("); | 1022 str->append("SkPerlinNoiseShader: ("); |
1023 | 1023 |
(...skipping 18 matching lines...) Expand all Loading... |
1042 str->append(" seed: "); | 1042 str->append(" seed: "); |
1043 str->appendScalar(fSeed); | 1043 str->appendScalar(fSeed); |
1044 str->append(" stitch tiles: "); | 1044 str->append(" stitch tiles: "); |
1045 str->append(fStitchTiles ? "true " : "false "); | 1045 str->append(fStitchTiles ? "true " : "false "); |
1046 | 1046 |
1047 this->INHERITED::toString(str); | 1047 this->INHERITED::toString(str); |
1048 | 1048 |
1049 str->append(")"); | 1049 str->append(")"); |
1050 } | 1050 } |
1051 #endif | 1051 #endif |
OLD | NEW |