bsalomon@google.com | 68b58c9 | 2013-01-17 16:50:08 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2012 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
| 7 | |
| 8 | #include "GrSimpleTextureEffect.h" |
Robert Phillips | 40fd7c9 | 2017-01-30 08:06:27 -0500 | [diff] [blame] | 9 | #include "GrProxyMove.h" |
egdaniel | 605dd0f | 2014-11-12 08:35:25 -0800 | [diff] [blame] | 10 | #include "GrTexture.h" |
brianosman | 77320db | 2016-09-07 08:09:10 -0700 | [diff] [blame] | 11 | #include "glsl/GrGLSLColorSpaceXformHelper.h" |
egdaniel | 64c4728 | 2015-11-13 06:54:19 -0800 | [diff] [blame] | 12 | #include "glsl/GrGLSLFragmentProcessor.h" |
egdaniel | 2d721d3 | 2015-11-11 13:06:05 -0800 | [diff] [blame] | 13 | #include "glsl/GrGLSLFragmentShaderBuilder.h" |
bsalomon@google.com | 68b58c9 | 2013-01-17 16:50:08 +0000 | [diff] [blame] | 14 | |
Robert Phillips | 40fd7c9 | 2017-01-30 08:06:27 -0500 | [diff] [blame] | 15 | GrSimpleTextureEffect::GrSimpleTextureEffect(GrContext* ctx, sk_sp<GrTextureProxy> proxy, |
| 16 | sk_sp<GrColorSpaceXform> colorSpaceXform, |
| 17 | const SkMatrix& matrix, |
| 18 | GrSamplerParams::FilterMode filterMode) |
| 19 | : INHERITED{ctx, |
| 20 | ModulationFlags(proxy->config()), |
| 21 | GR_PROXY_MOVE(proxy), |
| 22 | std::move(colorSpaceXform), |
| 23 | matrix, |
| 24 | filterMode} { |
| 25 | this->initClassID<GrSimpleTextureEffect>(); |
| 26 | } |
| 27 | |
| 28 | GrSimpleTextureEffect::GrSimpleTextureEffect(GrContext* ctx, sk_sp<GrTextureProxy> proxy, |
| 29 | sk_sp<GrColorSpaceXform> colorSpaceXform, |
| 30 | const SkMatrix& matrix, |
| 31 | const GrSamplerParams& params) |
| 32 | : INHERITED{ctx, |
| 33 | ModulationFlags(proxy->config()), |
| 34 | GR_PROXY_MOVE(proxy), |
| 35 | std::move(colorSpaceXform), |
| 36 | matrix, |
| 37 | params} { |
| 38 | this->initClassID<GrSimpleTextureEffect>(); |
| 39 | } |
| 40 | |
egdaniel | 64c4728 | 2015-11-13 06:54:19 -0800 | [diff] [blame] | 41 | class GrGLSimpleTextureEffect : public GrGLSLFragmentProcessor { |
bsalomon@google.com | 68b58c9 | 2013-01-17 16:50:08 +0000 | [diff] [blame] | 42 | public: |
robertphillips | 9cdb992 | 2016-02-03 12:25:40 -0800 | [diff] [blame] | 43 | void emitCode(EmitArgs& args) override { |
brianosman | 77320db | 2016-09-07 08:09:10 -0700 | [diff] [blame] | 44 | const GrSimpleTextureEffect& textureEffect = args.fFp.cast<GrSimpleTextureEffect>(); |
| 45 | GrGLSLColorSpaceXformHelper colorSpaceHelper(args.fUniformHandler, |
| 46 | textureEffect.colorSpaceXform(), |
| 47 | &fColorSpaceXformUni); |
| 48 | |
cdalton | 8528541 | 2016-02-18 12:37:07 -0800 | [diff] [blame] | 49 | GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder; |
robertphillips | bf536af | 2016-02-04 06:11:53 -0800 | [diff] [blame] | 50 | fragBuilder->codeAppendf("%s = ", args.fOutputColor); |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 51 | fragBuilder->appendTextureLookupAndModulate(args.fInputColor, |
brianosman | 77320db | 2016-09-07 08:09:10 -0700 | [diff] [blame] | 52 | args.fTexSamplers[0], |
bsalomon | 1a1aa93 | 2016-09-12 09:30:36 -0700 | [diff] [blame] | 53 | args.fTransformedCoords[0].c_str(), |
| 54 | args.fTransformedCoords[0].getType(), |
brianosman | 77320db | 2016-09-07 08:09:10 -0700 | [diff] [blame] | 55 | &colorSpaceHelper); |
robertphillips | bf536af | 2016-02-04 06:11:53 -0800 | [diff] [blame] | 56 | fragBuilder->codeAppend(";"); |
bsalomon@google.com | 68b58c9 | 2013-01-17 16:50:08 +0000 | [diff] [blame] | 57 | } |
| 58 | |
Brian Salomon | 94efbf5 | 2016-11-29 13:43:05 -0500 | [diff] [blame] | 59 | static inline void GenKey(const GrProcessor& effect, const GrShaderCaps&, |
brianosman | 77320db | 2016-09-07 08:09:10 -0700 | [diff] [blame] | 60 | GrProcessorKeyBuilder* b) { |
| 61 | const GrSimpleTextureEffect& textureEffect = effect.cast<GrSimpleTextureEffect>(); |
| 62 | b->add32(GrColorSpaceXform::XformKey(textureEffect.colorSpaceXform())); |
| 63 | } |
| 64 | |
| 65 | protected: |
| 66 | void onSetData(const GrGLSLProgramDataManager& pdman, const GrProcessor& processor) override { |
| 67 | const GrSimpleTextureEffect& textureEffect = processor.cast<GrSimpleTextureEffect>(); |
| 68 | if (SkToBool(textureEffect.colorSpaceXform())) { |
brianosman | 5192475 | 2016-09-12 08:50:19 -0700 | [diff] [blame] | 69 | pdman.setSkMatrix44(fColorSpaceXformUni, textureEffect.colorSpaceXform()->srcToDst()); |
brianosman | 77320db | 2016-09-07 08:09:10 -0700 | [diff] [blame] | 70 | } |
| 71 | } |
| 72 | |
bsalomon@google.com | 68b58c9 | 2013-01-17 16:50:08 +0000 | [diff] [blame] | 73 | private: |
egdaniel | 64c4728 | 2015-11-13 06:54:19 -0800 | [diff] [blame] | 74 | typedef GrGLSLFragmentProcessor INHERITED; |
brianosman | 77320db | 2016-09-07 08:09:10 -0700 | [diff] [blame] | 75 | |
| 76 | UniformHandle fColorSpaceXformUni; |
bsalomon@google.com | 68b58c9 | 2013-01-17 16:50:08 +0000 | [diff] [blame] | 77 | }; |
| 78 | |
| 79 | /////////////////////////////////////////////////////////////////////////////// |
| 80 | |
Brian Salomon | 94efbf5 | 2016-11-29 13:43:05 -0500 | [diff] [blame] | 81 | void GrSimpleTextureEffect::onGetGLSLProcessorKey(const GrShaderCaps& caps, |
egdaniel | 57d3b03 | 2015-11-13 11:57:27 -0800 | [diff] [blame] | 82 | GrProcessorKeyBuilder* b) const { |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 83 | GrGLSimpleTextureEffect::GenKey(*this, caps, b); |
| 84 | } |
| 85 | |
egdaniel | 57d3b03 | 2015-11-13 11:57:27 -0800 | [diff] [blame] | 86 | GrGLSLFragmentProcessor* GrSimpleTextureEffect::onCreateGLSLInstance() const { |
robertphillips | 9cdb992 | 2016-02-03 12:25:40 -0800 | [diff] [blame] | 87 | return new GrGLSimpleTextureEffect; |
bsalomon@google.com | 68b58c9 | 2013-01-17 16:50:08 +0000 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | /////////////////////////////////////////////////////////////////////////////// |
| 91 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 92 | GR_DEFINE_FRAGMENT_PROCESSOR_TEST(GrSimpleTextureEffect); |
bsalomon@google.com | 68b58c9 | 2013-01-17 16:50:08 +0000 | [diff] [blame] | 93 | |
Hal Canary | 6f6961e | 2017-01-31 13:50:44 -0500 | [diff] [blame] | 94 | #if GR_TEST_UTILS |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 95 | sk_sp<GrFragmentProcessor> GrSimpleTextureEffect::TestCreate(GrProcessorTestData* d) { |
Robert Phillips | 40fd7c9 | 2017-01-30 08:06:27 -0500 | [diff] [blame] | 96 | int texIdx = d->fRandom->nextBool() ? GrProcessorUnitTest::kSkiaPMTextureIdx |
| 97 | : GrProcessorUnitTest::kAlphaTextureIdx; |
bsalomon@google.com | c781888 | 2013-03-20 19:19:53 +0000 | [diff] [blame] | 98 | static const SkShader::TileMode kTileModes[] = { |
| 99 | SkShader::kClamp_TileMode, |
| 100 | SkShader::kRepeat_TileMode, |
| 101 | SkShader::kMirror_TileMode, |
| 102 | }; |
| 103 | SkShader::TileMode tileModes[] = { |
joshualitt | 0067ff5 | 2015-07-08 14:26:19 -0700 | [diff] [blame] | 104 | kTileModes[d->fRandom->nextULessThan(SK_ARRAY_COUNT(kTileModes))], |
| 105 | kTileModes[d->fRandom->nextULessThan(SK_ARRAY_COUNT(kTileModes))], |
bsalomon@google.com | c781888 | 2013-03-20 19:19:53 +0000 | [diff] [blame] | 106 | }; |
Robert Phillips | 40fd7c9 | 2017-01-30 08:06:27 -0500 | [diff] [blame] | 107 | GrSamplerParams params(tileModes, d->fRandom->nextBool() ? GrSamplerParams::kBilerp_FilterMode |
| 108 | : GrSamplerParams::kNone_FilterMode); |
bsalomon@google.com | c781888 | 2013-03-20 19:19:53 +0000 | [diff] [blame] | 109 | |
joshualitt | 0067ff5 | 2015-07-08 14:26:19 -0700 | [diff] [blame] | 110 | const SkMatrix& matrix = GrTest::TestMatrix(d->fRandom); |
Robert Phillips | 40fd7c9 | 2017-01-30 08:06:27 -0500 | [diff] [blame] | 111 | sk_sp<GrColorSpaceXform> colorSpaceXform = GrTest::TestColorXform(d->fRandom); |
| 112 | return GrSimpleTextureEffect::Make(d->context(), d->textureProxy(texIdx), |
| 113 | std::move(colorSpaceXform), matrix); |
bsalomon@google.com | 68b58c9 | 2013-01-17 16:50:08 +0000 | [diff] [blame] | 114 | } |
Hal Canary | 6f6961e | 2017-01-31 13:50:44 -0500 | [diff] [blame] | 115 | #endif |