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>(); |
Brian Osman | c624d9d | 2017-03-08 11:42:02 -0500 | [diff] [blame^] | 45 | fColorSpaceHelper.emitCode(args.fUniformHandler, textureEffect.colorSpaceXform()); |
brianosman | 77320db | 2016-09-07 08:09:10 -0700 | [diff] [blame] | 46 | |
cdalton | 8528541 | 2016-02-18 12:37:07 -0800 | [diff] [blame] | 47 | GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder; |
robertphillips | bf536af | 2016-02-04 06:11:53 -0800 | [diff] [blame] | 48 | fragBuilder->codeAppendf("%s = ", args.fOutputColor); |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 49 | fragBuilder->appendTextureLookupAndModulate(args.fInputColor, |
brianosman | 77320db | 2016-09-07 08:09:10 -0700 | [diff] [blame] | 50 | args.fTexSamplers[0], |
bsalomon | 1a1aa93 | 2016-09-12 09:30:36 -0700 | [diff] [blame] | 51 | args.fTransformedCoords[0].c_str(), |
| 52 | args.fTransformedCoords[0].getType(), |
Brian Osman | c624d9d | 2017-03-08 11:42:02 -0500 | [diff] [blame^] | 53 | &fColorSpaceHelper); |
robertphillips | bf536af | 2016-02-04 06:11:53 -0800 | [diff] [blame] | 54 | fragBuilder->codeAppend(";"); |
bsalomon@google.com | 68b58c9 | 2013-01-17 16:50:08 +0000 | [diff] [blame] | 55 | } |
| 56 | |
Brian Salomon | 94efbf5 | 2016-11-29 13:43:05 -0500 | [diff] [blame] | 57 | static inline void GenKey(const GrProcessor& effect, const GrShaderCaps&, |
brianosman | 77320db | 2016-09-07 08:09:10 -0700 | [diff] [blame] | 58 | GrProcessorKeyBuilder* b) { |
| 59 | const GrSimpleTextureEffect& textureEffect = effect.cast<GrSimpleTextureEffect>(); |
| 60 | b->add32(GrColorSpaceXform::XformKey(textureEffect.colorSpaceXform())); |
| 61 | } |
| 62 | |
| 63 | protected: |
| 64 | void onSetData(const GrGLSLProgramDataManager& pdman, const GrProcessor& processor) override { |
| 65 | const GrSimpleTextureEffect& textureEffect = processor.cast<GrSimpleTextureEffect>(); |
| 66 | if (SkToBool(textureEffect.colorSpaceXform())) { |
Brian Osman | c624d9d | 2017-03-08 11:42:02 -0500 | [diff] [blame^] | 67 | fColorSpaceHelper.setData(pdman, textureEffect.colorSpaceXform()); |
brianosman | 77320db | 2016-09-07 08:09:10 -0700 | [diff] [blame] | 68 | } |
| 69 | } |
| 70 | |
bsalomon@google.com | 68b58c9 | 2013-01-17 16:50:08 +0000 | [diff] [blame] | 71 | private: |
egdaniel | 64c4728 | 2015-11-13 06:54:19 -0800 | [diff] [blame] | 72 | typedef GrGLSLFragmentProcessor INHERITED; |
brianosman | 77320db | 2016-09-07 08:09:10 -0700 | [diff] [blame] | 73 | |
Brian Osman | c624d9d | 2017-03-08 11:42:02 -0500 | [diff] [blame^] | 74 | GrGLSLColorSpaceXformHelper fColorSpaceHelper; |
bsalomon@google.com | 68b58c9 | 2013-01-17 16:50:08 +0000 | [diff] [blame] | 75 | }; |
| 76 | |
| 77 | /////////////////////////////////////////////////////////////////////////////// |
| 78 | |
Brian Salomon | 94efbf5 | 2016-11-29 13:43:05 -0500 | [diff] [blame] | 79 | void GrSimpleTextureEffect::onGetGLSLProcessorKey(const GrShaderCaps& caps, |
egdaniel | 57d3b03 | 2015-11-13 11:57:27 -0800 | [diff] [blame] | 80 | GrProcessorKeyBuilder* b) const { |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 81 | GrGLSimpleTextureEffect::GenKey(*this, caps, b); |
| 82 | } |
| 83 | |
egdaniel | 57d3b03 | 2015-11-13 11:57:27 -0800 | [diff] [blame] | 84 | GrGLSLFragmentProcessor* GrSimpleTextureEffect::onCreateGLSLInstance() const { |
robertphillips | 9cdb992 | 2016-02-03 12:25:40 -0800 | [diff] [blame] | 85 | return new GrGLSimpleTextureEffect; |
bsalomon@google.com | 68b58c9 | 2013-01-17 16:50:08 +0000 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | /////////////////////////////////////////////////////////////////////////////// |
| 89 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 90 | GR_DEFINE_FRAGMENT_PROCESSOR_TEST(GrSimpleTextureEffect); |
bsalomon@google.com | 68b58c9 | 2013-01-17 16:50:08 +0000 | [diff] [blame] | 91 | |
Hal Canary | 6f6961e | 2017-01-31 13:50:44 -0500 | [diff] [blame] | 92 | #if GR_TEST_UTILS |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 93 | sk_sp<GrFragmentProcessor> GrSimpleTextureEffect::TestCreate(GrProcessorTestData* d) { |
Robert Phillips | 40fd7c9 | 2017-01-30 08:06:27 -0500 | [diff] [blame] | 94 | int texIdx = d->fRandom->nextBool() ? GrProcessorUnitTest::kSkiaPMTextureIdx |
| 95 | : GrProcessorUnitTest::kAlphaTextureIdx; |
bsalomon@google.com | c781888 | 2013-03-20 19:19:53 +0000 | [diff] [blame] | 96 | static const SkShader::TileMode kTileModes[] = { |
| 97 | SkShader::kClamp_TileMode, |
| 98 | SkShader::kRepeat_TileMode, |
| 99 | SkShader::kMirror_TileMode, |
| 100 | }; |
| 101 | SkShader::TileMode tileModes[] = { |
joshualitt | 0067ff5 | 2015-07-08 14:26:19 -0700 | [diff] [blame] | 102 | kTileModes[d->fRandom->nextULessThan(SK_ARRAY_COUNT(kTileModes))], |
| 103 | kTileModes[d->fRandom->nextULessThan(SK_ARRAY_COUNT(kTileModes))], |
bsalomon@google.com | c781888 | 2013-03-20 19:19:53 +0000 | [diff] [blame] | 104 | }; |
Robert Phillips | 40fd7c9 | 2017-01-30 08:06:27 -0500 | [diff] [blame] | 105 | GrSamplerParams params(tileModes, d->fRandom->nextBool() ? GrSamplerParams::kBilerp_FilterMode |
| 106 | : GrSamplerParams::kNone_FilterMode); |
bsalomon@google.com | c781888 | 2013-03-20 19:19:53 +0000 | [diff] [blame] | 107 | |
joshualitt | 0067ff5 | 2015-07-08 14:26:19 -0700 | [diff] [blame] | 108 | const SkMatrix& matrix = GrTest::TestMatrix(d->fRandom); |
Robert Phillips | 40fd7c9 | 2017-01-30 08:06:27 -0500 | [diff] [blame] | 109 | sk_sp<GrColorSpaceXform> colorSpaceXform = GrTest::TestColorXform(d->fRandom); |
| 110 | return GrSimpleTextureEffect::Make(d->context(), d->textureProxy(texIdx), |
| 111 | std::move(colorSpaceXform), matrix); |
bsalomon@google.com | 68b58c9 | 2013-01-17 16:50:08 +0000 | [diff] [blame] | 112 | } |
Hal Canary | 6f6961e | 2017-01-31 13:50:44 -0500 | [diff] [blame] | 113 | #endif |