| 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 "gl/GrGLProcessor.h" | 22 #include "gl/GrGLProcessor.h" |
| 22 #include "gl/builders/GrGLProgramBuilder.h" | 23 #include "gl/builders/GrGLProgramBuilder.h" |
| 23 #include "GrTBackendProcessorFactory.h" | |
| 24 #include "SkGr.h" | |
| 25 #endif | 24 #endif |
| 26 | 25 |
| 27 static const int kBlockSize = 256; | 26 static const int kBlockSize = 256; |
| 28 static const int kBlockMask = kBlockSize - 1; | 27 static const int kBlockMask = kBlockSize - 1; |
| 29 static const int kPerlinNoise = 4096; | 28 static const int kPerlinNoise = 4096; |
| 30 static const int kRandMaximum = SK_MaxS32; // 2**31 - 1 | 29 static const int kRandMaximum = SK_MaxS32; // 2**31 - 1 |
| 31 | 30 |
| 32 namespace { | 31 namespace { |
| 33 | 32 |
| 34 // noiseValue is the color component's value (or color) | 33 // noiseValue is the color component's value (or color) |
| (...skipping 445 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 480 result[i] = SkDitherRGB32To565(shade(point, stitchData), dither); | 479 result[i] = SkDitherRGB32To565(shade(point, stitchData), dither); |
| 481 DITHER_INC_X(x); | 480 DITHER_INC_X(x); |
| 482 point.fX += SK_Scalar1; | 481 point.fX += SK_Scalar1; |
| 483 } | 482 } |
| 484 } | 483 } |
| 485 | 484 |
| 486 ///////////////////////////////////////////////////////////////////// | 485 ///////////////////////////////////////////////////////////////////// |
| 487 | 486 |
| 488 #if SK_SUPPORT_GPU | 487 #if SK_SUPPORT_GPU |
| 489 | 488 |
| 490 #include "GrTBackendProcessorFactory.h" | |
| 491 | |
| 492 class GrGLPerlinNoise : public GrGLFragmentProcessor { | 489 class GrGLPerlinNoise : public GrGLFragmentProcessor { |
| 493 public: | 490 public: |
| 494 GrGLPerlinNoise(const GrBackendProcessorFactory&, | 491 GrGLPerlinNoise(const GrProcessor&); |
| 495 const GrProcessor&); | |
| 496 virtual ~GrGLPerlinNoise() {} | 492 virtual ~GrGLPerlinNoise() {} |
| 497 | 493 |
| 498 virtual void emitCode(GrGLFPBuilder*, | 494 virtual void emitCode(GrGLFPBuilder*, |
| 499 const GrFragmentProcessor&, | 495 const GrFragmentProcessor&, |
| 500 const char* outputColor, | 496 const char* outputColor, |
| 501 const char* inputColor, | 497 const char* inputColor, |
| 502 const TransformedCoordsArray&, | 498 const TransformedCoordsArray&, |
| 503 const TextureSamplerArray&) SK_OVERRIDE; | 499 const TextureSamplerArray&) SK_OVERRIDE; |
| 504 | 500 |
| 505 virtual void setData(const GrGLProgramDataManager&, const GrProcessor&) SK_O
VERRIDE; | 501 virtual void setData(const GrGLProgramDataManager&, const GrProcessor&) SK_O
VERRIDE; |
| (...skipping 23 matching lines...) Expand all Loading... |
| 529 GrTexture* permutationsTexture, GrTexture
* noiseTexture, | 525 GrTexture* permutationsTexture, GrTexture
* noiseTexture, |
| 530 const SkMatrix& matrix, uint8_t alpha) { | 526 const SkMatrix& matrix, uint8_t alpha) { |
| 531 return SkNEW_ARGS(GrPerlinNoiseEffect, (type, numOctaves, stitchTiles, p
aintingData, | 527 return SkNEW_ARGS(GrPerlinNoiseEffect, (type, numOctaves, stitchTiles, p
aintingData, |
| 532 permutationsTexture, noiseTextur
e, matrix, alpha)); | 528 permutationsTexture, noiseTextur
e, matrix, alpha)); |
| 533 } | 529 } |
| 534 | 530 |
| 535 virtual ~GrPerlinNoiseEffect() { | 531 virtual ~GrPerlinNoiseEffect() { |
| 536 SkDELETE(fPaintingData); | 532 SkDELETE(fPaintingData); |
| 537 } | 533 } |
| 538 | 534 |
| 539 static const char* Name() { return "PerlinNoise"; } | 535 virtual const char* name() const SK_OVERRIDE { return "PerlinNoise"; } |
| 540 virtual const GrBackendFragmentProcessorFactory& getFactory() const SK_OVERR
IDE { | 536 |
| 541 return GrTBackendFragmentProcessorFactory<GrPerlinNoiseEffect>::getInsta
nce(); | 537 virtual void getGLProcessorKey(const GrGLCaps& caps, |
| 538 GrProcessorKeyBuilder* b) const SK_OVERRIDE { |
| 539 GrGLPerlinNoise::GenKey(*this, caps, b); |
| 542 } | 540 } |
| 541 |
| 542 virtual GrGLFragmentProcessor* createGLInstance() const SK_OVERRIDE { |
| 543 return SkNEW_ARGS(GrGLPerlinNoise, (*this)); |
| 544 } |
| 545 |
| 543 const SkPerlinNoiseShader::StitchData& stitchData() const { return fPainting
Data->fStitchDataInit; } | 546 const SkPerlinNoiseShader::StitchData& stitchData() const { return fPainting
Data->fStitchDataInit; } |
| 544 | 547 |
| 545 SkPerlinNoiseShader::Type type() const { return fType; } | 548 SkPerlinNoiseShader::Type type() const { return fType; } |
| 546 bool stitchTiles() const { return fStitchTiles; } | 549 bool stitchTiles() const { return fStitchTiles; } |
| 547 const SkVector& baseFrequency() const { return fPaintingData->fBaseFrequency
; } | 550 const SkVector& baseFrequency() const { return fPaintingData->fBaseFrequency
; } |
| 548 int numOctaves() const { return fNumOctaves; } | 551 int numOctaves() const { return fNumOctaves; } |
| 549 const SkMatrix& matrix() const { return fCoordTransform.getMatrix(); } | 552 const SkMatrix& matrix() const { return fCoordTransform.getMatrix(); } |
| 550 uint8_t alpha() const { return fAlpha; } | 553 uint8_t alpha() const { return fAlpha; } |
| 551 | 554 |
| 552 typedef GrGLPerlinNoise GLProcessor; | |
| 553 | |
| 554 private: | 555 private: |
| 555 virtual bool onIsEqual(const GrFragmentProcessor& sBase) const SK_OVERRIDE { | 556 virtual bool onIsEqual(const GrFragmentProcessor& sBase) const SK_OVERRIDE { |
| 556 const GrPerlinNoiseEffect& s = sBase.cast<GrPerlinNoiseEffect>(); | 557 const GrPerlinNoiseEffect& s = sBase.cast<GrPerlinNoiseEffect>(); |
| 557 return fType == s.fType && | 558 return fType == s.fType && |
| 558 fPaintingData->fBaseFrequency == s.fPaintingData->fBaseFrequency
&& | 559 fPaintingData->fBaseFrequency == s.fPaintingData->fBaseFrequency
&& |
| 559 fNumOctaves == s.fNumOctaves && | 560 fNumOctaves == s.fNumOctaves && |
| 560 fStitchTiles == s.fStitchTiles && | 561 fStitchTiles == s.fStitchTiles && |
| 561 fAlpha == s.fAlpha && | 562 fAlpha == s.fAlpha && |
| 562 fPaintingData->fStitchDataInit == s.fPaintingData->fStitchDataIni
t; | 563 fPaintingData->fStitchDataInit == s.fPaintingData->fStitchDataIni
t; |
| 563 } | 564 } |
| 564 | 565 |
| 565 void onComputeInvariantOutput(GrInvariantOutput* inout) const SK_OVERRIDE { | 566 void onComputeInvariantOutput(GrInvariantOutput* inout) const SK_OVERRIDE { |
| 566 inout->setToUnknown(GrInvariantOutput::kWillNot_ReadInput); | 567 inout->setToUnknown(GrInvariantOutput::kWillNot_ReadInput); |
| 567 } | 568 } |
| 568 | 569 |
| 569 GrPerlinNoiseEffect(SkPerlinNoiseShader::Type type, | 570 GrPerlinNoiseEffect(SkPerlinNoiseShader::Type type, |
| 570 int numOctaves, bool stitchTiles, | 571 int numOctaves, bool stitchTiles, |
| 571 SkPerlinNoiseShader::PaintingData* paintingData, | 572 SkPerlinNoiseShader::PaintingData* paintingData, |
| 572 GrTexture* permutationsTexture, GrTexture* noiseTexture, | 573 GrTexture* permutationsTexture, GrTexture* noiseTexture, |
| 573 const SkMatrix& matrix, uint8_t alpha) | 574 const SkMatrix& matrix, uint8_t alpha) |
| 574 : fType(type) | 575 : fType(type) |
| 575 , fNumOctaves(numOctaves) | 576 , fNumOctaves(numOctaves) |
| 576 , fStitchTiles(stitchTiles) | 577 , fStitchTiles(stitchTiles) |
| 577 , fAlpha(alpha) | 578 , fAlpha(alpha) |
| 578 , fPermutationsAccess(permutationsTexture) | 579 , fPermutationsAccess(permutationsTexture) |
| 579 , fNoiseAccess(noiseTexture) | 580 , fNoiseAccess(noiseTexture) |
| 580 , fPaintingData(paintingData) { | 581 , fPaintingData(paintingData) { |
| 582 this->initClassID<GrPerlinNoiseEffect>(); |
| 581 this->addTextureAccess(&fPermutationsAccess); | 583 this->addTextureAccess(&fPermutationsAccess); |
| 582 this->addTextureAccess(&fNoiseAccess); | 584 this->addTextureAccess(&fNoiseAccess); |
| 583 fCoordTransform.reset(kLocal_GrCoordSet, matrix); | 585 fCoordTransform.reset(kLocal_GrCoordSet, matrix); |
| 584 this->addCoordTransform(&fCoordTransform); | 586 this->addCoordTransform(&fCoordTransform); |
| 585 } | 587 } |
| 586 | 588 |
| 587 GR_DECLARE_FRAGMENT_PROCESSOR_TEST; | 589 GR_DECLARE_FRAGMENT_PROCESSOR_TEST; |
| 588 | 590 |
| 589 SkPerlinNoiseShader::Type fType; | 591 SkPerlinNoiseShader::Type fType; |
| 590 GrCoordTransform fCoordTransform; | 592 GrCoordTransform fCoordTransform; |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 624 SkPaint paint; | 626 SkPaint paint; |
| 625 GrColor paintColor; | 627 GrColor paintColor; |
| 626 GrFragmentProcessor* effect; | 628 GrFragmentProcessor* effect; |
| 627 SkAssertResult(shader->asFragmentProcessor(context, paint, NULL, &paintColor
, &effect)); | 629 SkAssertResult(shader->asFragmentProcessor(context, paint, NULL, &paintColor
, &effect)); |
| 628 | 630 |
| 629 SkDELETE(shader); | 631 SkDELETE(shader); |
| 630 | 632 |
| 631 return effect; | 633 return effect; |
| 632 } | 634 } |
| 633 | 635 |
| 634 GrGLPerlinNoise::GrGLPerlinNoise(const GrBackendProcessorFactory& factory, | 636 GrGLPerlinNoise::GrGLPerlinNoise(const GrProcessor& processor) |
| 635 const GrProcessor& processor) | 637 : fType(processor.cast<GrPerlinNoiseEffect>().type()) |
| 636 : INHERITED (factory) | |
| 637 , fType(processor.cast<GrPerlinNoiseEffect>().type()) | |
| 638 , fStitchTiles(processor.cast<GrPerlinNoiseEffect>().stitchTiles()) | 638 , fStitchTiles(processor.cast<GrPerlinNoiseEffect>().stitchTiles()) |
| 639 , fNumOctaves(processor.cast<GrPerlinNoiseEffect>().numOctaves()) { | 639 , fNumOctaves(processor.cast<GrPerlinNoiseEffect>().numOctaves()) { |
| 640 } | 640 } |
| 641 | 641 |
| 642 void GrGLPerlinNoise::emitCode(GrGLFPBuilder* builder, | 642 void GrGLPerlinNoise::emitCode(GrGLFPBuilder* builder, |
| 643 const GrFragmentProcessor&, | 643 const GrFragmentProcessor&, |
| 644 const char* outputColor, | 644 const char* outputColor, |
| 645 const char* inputColor, | 645 const char* inputColor, |
| 646 const TransformedCoordsArray& coords, | 646 const TransformedCoordsArray& coords, |
| 647 const TextureSamplerArray& samplers) { | 647 const TextureSamplerArray& samplers) { |
| (...skipping 379 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1027 str->append(" seed: "); | 1027 str->append(" seed: "); |
| 1028 str->appendScalar(fSeed); | 1028 str->appendScalar(fSeed); |
| 1029 str->append(" stitch tiles: "); | 1029 str->append(" stitch tiles: "); |
| 1030 str->append(fStitchTiles ? "true " : "false "); | 1030 str->append(fStitchTiles ? "true " : "false "); |
| 1031 | 1031 |
| 1032 this->INHERITED::toString(str); | 1032 this->INHERITED::toString(str); |
| 1033 | 1033 |
| 1034 str->append(")"); | 1034 str->append(")"); |
| 1035 } | 1035 } |
| 1036 #endif | 1036 #endif |
| OLD | NEW |