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