blob: a41ed8b0b99b152e52d546b7b76215870737d1e3 [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 Phillips296b1cc2017-03-15 10:42:12 -040015GrSimpleTextureEffect::GrSimpleTextureEffect(GrResourceProvider* resourceProvider,
16 sk_sp<GrTextureProxy> proxy,
Robert Phillips40fd7c92017-01-30 08:06:27 -050017 sk_sp<GrColorSpaceXform> colorSpaceXform,
18 const SkMatrix& matrix,
19 GrSamplerParams::FilterMode filterMode)
Robert Phillips296b1cc2017-03-15 10:42:12 -040020 : INHERITED{resourceProvider,
Robert Phillips40fd7c92017-01-30 08:06:27 -050021 ModulationFlags(proxy->config()),
22 GR_PROXY_MOVE(proxy),
23 std::move(colorSpaceXform),
24 matrix,
25 filterMode} {
26 this->initClassID<GrSimpleTextureEffect>();
27}
28
Robert Phillips296b1cc2017-03-15 10:42:12 -040029GrSimpleTextureEffect::GrSimpleTextureEffect(GrResourceProvider* resourceProvider,
30 sk_sp<GrTextureProxy> proxy,
Robert Phillips40fd7c92017-01-30 08:06:27 -050031 sk_sp<GrColorSpaceXform> colorSpaceXform,
32 const SkMatrix& matrix,
33 const GrSamplerParams& params)
Robert Phillips296b1cc2017-03-15 10:42:12 -040034 : INHERITED{resourceProvider,
Robert Phillips40fd7c92017-01-30 08:06:27 -050035 ModulationFlags(proxy->config()),
36 GR_PROXY_MOVE(proxy),
37 std::move(colorSpaceXform),
38 matrix,
39 params} {
40 this->initClassID<GrSimpleTextureEffect>();
41}
42
egdaniel64c47282015-11-13 06:54:19 -080043class GrGLSimpleTextureEffect : public GrGLSLFragmentProcessor {
bsalomon@google.com68b58c92013-01-17 16:50:08 +000044public:
robertphillips9cdb9922016-02-03 12:25:40 -080045 void emitCode(EmitArgs& args) override {
brianosman77320db2016-09-07 08:09:10 -070046 const GrSimpleTextureEffect& textureEffect = args.fFp.cast<GrSimpleTextureEffect>();
Brian Osmanc624d9d2017-03-08 11:42:02 -050047 fColorSpaceHelper.emitCode(args.fUniformHandler, textureEffect.colorSpaceXform());
brianosman77320db2016-09-07 08:09:10 -070048
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(),
Brian Osmanc624d9d2017-03-08 11:42:02 -050055 &fColorSpaceHelper);
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:
Brian Salomonab015ef2017-04-04 10:15:51 -040066 void onSetData(const GrGLSLProgramDataManager& pdman,
67 const GrFragmentProcessor& processor) override {
brianosman77320db2016-09-07 08:09:10 -070068 const GrSimpleTextureEffect& textureEffect = processor.cast<GrSimpleTextureEffect>();
69 if (SkToBool(textureEffect.colorSpaceXform())) {
Brian Osmanc624d9d2017-03-08 11:42:02 -050070 fColorSpaceHelper.setData(pdman, textureEffect.colorSpaceXform());
brianosman77320db2016-09-07 08:09:10 -070071 }
72 }
73
bsalomon@google.com68b58c92013-01-17 16:50:08 +000074private:
egdaniel64c47282015-11-13 06:54:19 -080075 typedef GrGLSLFragmentProcessor INHERITED;
brianosman77320db2016-09-07 08:09:10 -070076
Brian Osmanc624d9d2017-03-08 11:42:02 -050077 GrGLSLColorSpaceXformHelper fColorSpaceHelper;
bsalomon@google.com68b58c92013-01-17 16:50:08 +000078};
79
80///////////////////////////////////////////////////////////////////////////////
81
Brian Salomon94efbf52016-11-29 13:43:05 -050082void GrSimpleTextureEffect::onGetGLSLProcessorKey(const GrShaderCaps& caps,
egdaniel57d3b032015-11-13 11:57:27 -080083 GrProcessorKeyBuilder* b) const {
joshualitteb2a6762014-12-04 11:35:33 -080084 GrGLSimpleTextureEffect::GenKey(*this, caps, b);
85}
86
egdaniel57d3b032015-11-13 11:57:27 -080087GrGLSLFragmentProcessor* GrSimpleTextureEffect::onCreateGLSLInstance() const {
robertphillips9cdb9922016-02-03 12:25:40 -080088 return new GrGLSimpleTextureEffect;
bsalomon@google.com68b58c92013-01-17 16:50:08 +000089}
90
91///////////////////////////////////////////////////////////////////////////////
92
joshualittb0a8a372014-09-23 09:50:21 -070093GR_DEFINE_FRAGMENT_PROCESSOR_TEST(GrSimpleTextureEffect);
bsalomon@google.com68b58c92013-01-17 16:50:08 +000094
Hal Canary6f6961e2017-01-31 13:50:44 -050095#if GR_TEST_UTILS
bungeman06ca8ec2016-06-09 08:01:03 -070096sk_sp<GrFragmentProcessor> GrSimpleTextureEffect::TestCreate(GrProcessorTestData* d) {
Robert Phillips40fd7c92017-01-30 08:06:27 -050097 int texIdx = d->fRandom->nextBool() ? GrProcessorUnitTest::kSkiaPMTextureIdx
98 : GrProcessorUnitTest::kAlphaTextureIdx;
bsalomon@google.comc7818882013-03-20 19:19:53 +000099 static const SkShader::TileMode kTileModes[] = {
100 SkShader::kClamp_TileMode,
101 SkShader::kRepeat_TileMode,
102 SkShader::kMirror_TileMode,
103 };
104 SkShader::TileMode tileModes[] = {
joshualitt0067ff52015-07-08 14:26:19 -0700105 kTileModes[d->fRandom->nextULessThan(SK_ARRAY_COUNT(kTileModes))],
106 kTileModes[d->fRandom->nextULessThan(SK_ARRAY_COUNT(kTileModes))],
bsalomon@google.comc7818882013-03-20 19:19:53 +0000107 };
Robert Phillips40fd7c92017-01-30 08:06:27 -0500108 GrSamplerParams params(tileModes, d->fRandom->nextBool() ? GrSamplerParams::kBilerp_FilterMode
109 : GrSamplerParams::kNone_FilterMode);
bsalomon@google.comc7818882013-03-20 19:19:53 +0000110
joshualitt0067ff52015-07-08 14:26:19 -0700111 const SkMatrix& matrix = GrTest::TestMatrix(d->fRandom);
Robert Phillips40fd7c92017-01-30 08:06:27 -0500112 sk_sp<GrColorSpaceXform> colorSpaceXform = GrTest::TestColorXform(d->fRandom);
Robert Phillips296b1cc2017-03-15 10:42:12 -0400113 return GrSimpleTextureEffect::Make(d->resourceProvider(), d->textureProxy(texIdx),
Robert Phillips40fd7c92017-01-30 08:06:27 -0500114 std::move(colorSpaceXform), matrix);
bsalomon@google.com68b58c92013-01-17 16:50:08 +0000115}
Hal Canary6f6961e2017-01-31 13:50:44 -0500116#endif