blob: bd4e7206f8649ff94f0a22530b0e6812395294b4 [file] [log] [blame]
bsalomon@google.com68b58c92013-01-17 16:50:08 +00001/*
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 Phillips40fd7c92017-01-30 08:06:27 -05009#include "GrProxyMove.h"
egdaniel605dd0f2014-11-12 08:35:25 -080010#include "GrTexture.h"
brianosman77320db2016-09-07 08:09:10 -070011#include "glsl/GrGLSLColorSpaceXformHelper.h"
egdaniel64c47282015-11-13 06:54:19 -080012#include "glsl/GrGLSLFragmentProcessor.h"
egdaniel2d721d32015-11-11 13:06:05 -080013#include "glsl/GrGLSLFragmentShaderBuilder.h"
bsalomon@google.com68b58c92013-01-17 16:50:08 +000014
Robert Phillips40fd7c92017-01-30 08:06:27 -050015GrSimpleTextureEffect::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
28GrSimpleTextureEffect::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
egdaniel64c47282015-11-13 06:54:19 -080041class GrGLSimpleTextureEffect : public GrGLSLFragmentProcessor {
bsalomon@google.com68b58c92013-01-17 16:50:08 +000042public:
robertphillips9cdb9922016-02-03 12:25:40 -080043 void emitCode(EmitArgs& args) override {
brianosman77320db2016-09-07 08:09:10 -070044 const GrSimpleTextureEffect& textureEffect = args.fFp.cast<GrSimpleTextureEffect>();
45 GrGLSLColorSpaceXformHelper colorSpaceHelper(args.fUniformHandler,
46 textureEffect.colorSpaceXform(),
47 &fColorSpaceXformUni);
48
cdalton85285412016-02-18 12:37:07 -080049 GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder;
robertphillipsbf536af2016-02-04 06:11:53 -080050 fragBuilder->codeAppendf("%s = ", args.fOutputColor);
egdaniel4ca2e602015-11-18 08:01:26 -080051 fragBuilder->appendTextureLookupAndModulate(args.fInputColor,
brianosman77320db2016-09-07 08:09:10 -070052 args.fTexSamplers[0],
bsalomon1a1aa932016-09-12 09:30:36 -070053 args.fTransformedCoords[0].c_str(),
54 args.fTransformedCoords[0].getType(),
brianosman77320db2016-09-07 08:09:10 -070055 &colorSpaceHelper);
robertphillipsbf536af2016-02-04 06:11:53 -080056 fragBuilder->codeAppend(";");
bsalomon@google.com68b58c92013-01-17 16:50:08 +000057 }
58
Brian Salomon94efbf52016-11-29 13:43:05 -050059 static inline void GenKey(const GrProcessor& effect, const GrShaderCaps&,
brianosman77320db2016-09-07 08:09:10 -070060 GrProcessorKeyBuilder* b) {
61 const GrSimpleTextureEffect& textureEffect = effect.cast<GrSimpleTextureEffect>();
62 b->add32(GrColorSpaceXform::XformKey(textureEffect.colorSpaceXform()));
63 }
64
65protected:
66 void onSetData(const GrGLSLProgramDataManager& pdman, const GrProcessor& processor) override {
67 const GrSimpleTextureEffect& textureEffect = processor.cast<GrSimpleTextureEffect>();
68 if (SkToBool(textureEffect.colorSpaceXform())) {
brianosman51924752016-09-12 08:50:19 -070069 pdman.setSkMatrix44(fColorSpaceXformUni, textureEffect.colorSpaceXform()->srcToDst());
brianosman77320db2016-09-07 08:09:10 -070070 }
71 }
72
bsalomon@google.com68b58c92013-01-17 16:50:08 +000073private:
egdaniel64c47282015-11-13 06:54:19 -080074 typedef GrGLSLFragmentProcessor INHERITED;
brianosman77320db2016-09-07 08:09:10 -070075
76 UniformHandle fColorSpaceXformUni;
bsalomon@google.com68b58c92013-01-17 16:50:08 +000077};
78
79///////////////////////////////////////////////////////////////////////////////
80
Brian Salomon94efbf52016-11-29 13:43:05 -050081void GrSimpleTextureEffect::onGetGLSLProcessorKey(const GrShaderCaps& caps,
egdaniel57d3b032015-11-13 11:57:27 -080082 GrProcessorKeyBuilder* b) const {
joshualitteb2a6762014-12-04 11:35:33 -080083 GrGLSimpleTextureEffect::GenKey(*this, caps, b);
84}
85
egdaniel57d3b032015-11-13 11:57:27 -080086GrGLSLFragmentProcessor* GrSimpleTextureEffect::onCreateGLSLInstance() const {
robertphillips9cdb9922016-02-03 12:25:40 -080087 return new GrGLSimpleTextureEffect;
bsalomon@google.com68b58c92013-01-17 16:50:08 +000088}
89
90///////////////////////////////////////////////////////////////////////////////
91
joshualittb0a8a372014-09-23 09:50:21 -070092GR_DEFINE_FRAGMENT_PROCESSOR_TEST(GrSimpleTextureEffect);
bsalomon@google.com68b58c92013-01-17 16:50:08 +000093
Hal Canary6f6961e2017-01-31 13:50:44 -050094#if GR_TEST_UTILS
bungeman06ca8ec2016-06-09 08:01:03 -070095sk_sp<GrFragmentProcessor> GrSimpleTextureEffect::TestCreate(GrProcessorTestData* d) {
Robert Phillips40fd7c92017-01-30 08:06:27 -050096 int texIdx = d->fRandom->nextBool() ? GrProcessorUnitTest::kSkiaPMTextureIdx
97 : GrProcessorUnitTest::kAlphaTextureIdx;
bsalomon@google.comc7818882013-03-20 19:19:53 +000098 static const SkShader::TileMode kTileModes[] = {
99 SkShader::kClamp_TileMode,
100 SkShader::kRepeat_TileMode,
101 SkShader::kMirror_TileMode,
102 };
103 SkShader::TileMode tileModes[] = {
joshualitt0067ff52015-07-08 14:26:19 -0700104 kTileModes[d->fRandom->nextULessThan(SK_ARRAY_COUNT(kTileModes))],
105 kTileModes[d->fRandom->nextULessThan(SK_ARRAY_COUNT(kTileModes))],
bsalomon@google.comc7818882013-03-20 19:19:53 +0000106 };
Robert Phillips40fd7c92017-01-30 08:06:27 -0500107 GrSamplerParams params(tileModes, d->fRandom->nextBool() ? GrSamplerParams::kBilerp_FilterMode
108 : GrSamplerParams::kNone_FilterMode);
bsalomon@google.comc7818882013-03-20 19:19:53 +0000109
joshualitt0067ff52015-07-08 14:26:19 -0700110 const SkMatrix& matrix = GrTest::TestMatrix(d->fRandom);
Robert Phillips40fd7c92017-01-30 08:06:27 -0500111 sk_sp<GrColorSpaceXform> colorSpaceXform = GrTest::TestColorXform(d->fRandom);
112 return GrSimpleTextureEffect::Make(d->context(), d->textureProxy(texIdx),
113 std::move(colorSpaceXform), matrix);
bsalomon@google.com68b58c92013-01-17 16:50:08 +0000114}
Hal Canary6f6961e2017-01-31 13:50:44 -0500115#endif