blob: 8492f5b80b335cb6947c61606496bfc731de75ee [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>();
Brian Osmanc624d9d2017-03-08 11:42:02 -050045 fColorSpaceHelper.emitCode(args.fUniformHandler, textureEffect.colorSpaceXform());
brianosman77320db2016-09-07 08:09:10 -070046
cdalton85285412016-02-18 12:37:07 -080047 GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder;
robertphillipsbf536af2016-02-04 06:11:53 -080048 fragBuilder->codeAppendf("%s = ", args.fOutputColor);
egdaniel4ca2e602015-11-18 08:01:26 -080049 fragBuilder->appendTextureLookupAndModulate(args.fInputColor,
brianosman77320db2016-09-07 08:09:10 -070050 args.fTexSamplers[0],
bsalomon1a1aa932016-09-12 09:30:36 -070051 args.fTransformedCoords[0].c_str(),
52 args.fTransformedCoords[0].getType(),
Brian Osmanc624d9d2017-03-08 11:42:02 -050053 &fColorSpaceHelper);
robertphillipsbf536af2016-02-04 06:11:53 -080054 fragBuilder->codeAppend(";");
bsalomon@google.com68b58c92013-01-17 16:50:08 +000055 }
56
Brian Salomon94efbf52016-11-29 13:43:05 -050057 static inline void GenKey(const GrProcessor& effect, const GrShaderCaps&,
brianosman77320db2016-09-07 08:09:10 -070058 GrProcessorKeyBuilder* b) {
59 const GrSimpleTextureEffect& textureEffect = effect.cast<GrSimpleTextureEffect>();
60 b->add32(GrColorSpaceXform::XformKey(textureEffect.colorSpaceXform()));
61 }
62
63protected:
64 void onSetData(const GrGLSLProgramDataManager& pdman, const GrProcessor& processor) override {
65 const GrSimpleTextureEffect& textureEffect = processor.cast<GrSimpleTextureEffect>();
66 if (SkToBool(textureEffect.colorSpaceXform())) {
Brian Osmanc624d9d2017-03-08 11:42:02 -050067 fColorSpaceHelper.setData(pdman, textureEffect.colorSpaceXform());
brianosman77320db2016-09-07 08:09:10 -070068 }
69 }
70
bsalomon@google.com68b58c92013-01-17 16:50:08 +000071private:
egdaniel64c47282015-11-13 06:54:19 -080072 typedef GrGLSLFragmentProcessor INHERITED;
brianosman77320db2016-09-07 08:09:10 -070073
Brian Osmanc624d9d2017-03-08 11:42:02 -050074 GrGLSLColorSpaceXformHelper fColorSpaceHelper;
bsalomon@google.com68b58c92013-01-17 16:50:08 +000075};
76
77///////////////////////////////////////////////////////////////////////////////
78
Brian Salomon94efbf52016-11-29 13:43:05 -050079void GrSimpleTextureEffect::onGetGLSLProcessorKey(const GrShaderCaps& caps,
egdaniel57d3b032015-11-13 11:57:27 -080080 GrProcessorKeyBuilder* b) const {
joshualitteb2a6762014-12-04 11:35:33 -080081 GrGLSimpleTextureEffect::GenKey(*this, caps, b);
82}
83
egdaniel57d3b032015-11-13 11:57:27 -080084GrGLSLFragmentProcessor* GrSimpleTextureEffect::onCreateGLSLInstance() const {
robertphillips9cdb9922016-02-03 12:25:40 -080085 return new GrGLSimpleTextureEffect;
bsalomon@google.com68b58c92013-01-17 16:50:08 +000086}
87
88///////////////////////////////////////////////////////////////////////////////
89
joshualittb0a8a372014-09-23 09:50:21 -070090GR_DEFINE_FRAGMENT_PROCESSOR_TEST(GrSimpleTextureEffect);
bsalomon@google.com68b58c92013-01-17 16:50:08 +000091
Hal Canary6f6961e2017-01-31 13:50:44 -050092#if GR_TEST_UTILS
bungeman06ca8ec2016-06-09 08:01:03 -070093sk_sp<GrFragmentProcessor> GrSimpleTextureEffect::TestCreate(GrProcessorTestData* d) {
Robert Phillips40fd7c92017-01-30 08:06:27 -050094 int texIdx = d->fRandom->nextBool() ? GrProcessorUnitTest::kSkiaPMTextureIdx
95 : GrProcessorUnitTest::kAlphaTextureIdx;
bsalomon@google.comc7818882013-03-20 19:19:53 +000096 static const SkShader::TileMode kTileModes[] = {
97 SkShader::kClamp_TileMode,
98 SkShader::kRepeat_TileMode,
99 SkShader::kMirror_TileMode,
100 };
101 SkShader::TileMode tileModes[] = {
joshualitt0067ff52015-07-08 14:26:19 -0700102 kTileModes[d->fRandom->nextULessThan(SK_ARRAY_COUNT(kTileModes))],
103 kTileModes[d->fRandom->nextULessThan(SK_ARRAY_COUNT(kTileModes))],
bsalomon@google.comc7818882013-03-20 19:19:53 +0000104 };
Robert Phillips40fd7c92017-01-30 08:06:27 -0500105 GrSamplerParams params(tileModes, d->fRandom->nextBool() ? GrSamplerParams::kBilerp_FilterMode
106 : GrSamplerParams::kNone_FilterMode);
bsalomon@google.comc7818882013-03-20 19:19:53 +0000107
joshualitt0067ff52015-07-08 14:26:19 -0700108 const SkMatrix& matrix = GrTest::TestMatrix(d->fRandom);
Robert Phillips40fd7c92017-01-30 08:06:27 -0500109 sk_sp<GrColorSpaceXform> colorSpaceXform = GrTest::TestColorXform(d->fRandom);
110 return GrSimpleTextureEffect::Make(d->context(), d->textureProxy(texIdx),
111 std::move(colorSpaceXform), matrix);
bsalomon@google.com68b58c92013-01-17 16:50:08 +0000112}
Hal Canary6f6961e2017-01-31 13:50:44 -0500113#endif