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"
9 #include "GrProxyMove.h"
10 #include "GrTexture.h"
11 #include "glsl/GrGLSLColorSpaceXformHelper.h"
12 #include "glsl/GrGLSLFragmentProcessor.h"
13 #include "glsl/GrGLSLFragmentShaderBuilder.h"
14 
GrSimpleTextureEffect(GrResourceProvider * resourceProvider,sk_sp<GrTextureProxy> proxy,sk_sp<GrColorSpaceXform> colorSpaceXform,const SkMatrix & matrix,GrSamplerParams::FilterMode filterMode)15 GrSimpleTextureEffect::GrSimpleTextureEffect(GrResourceProvider* resourceProvider,
16                                              sk_sp<GrTextureProxy> proxy,
17                                              sk_sp<GrColorSpaceXform> colorSpaceXform,
18                                              const SkMatrix& matrix,
19                                              GrSamplerParams::FilterMode filterMode)
20         : INHERITED{resourceProvider,
21                     ModulationFlags(proxy->config()),
22                     GR_PROXY_MOVE(proxy),
23                     std::move(colorSpaceXform),
24                     matrix,
25                     filterMode} {
26     this->initClassID<GrSimpleTextureEffect>();
27 }
28 
GrSimpleTextureEffect(GrResourceProvider * resourceProvider,sk_sp<GrTextureProxy> proxy,sk_sp<GrColorSpaceXform> colorSpaceXform,const SkMatrix & matrix,const GrSamplerParams & params)29 GrSimpleTextureEffect::GrSimpleTextureEffect(GrResourceProvider* resourceProvider,
30                                              sk_sp<GrTextureProxy> proxy,
31                                              sk_sp<GrColorSpaceXform> colorSpaceXform,
32                                              const SkMatrix& matrix,
33                                              const GrSamplerParams& params)
34         : INHERITED{resourceProvider,
35                     ModulationFlags(proxy->config()),
36                     GR_PROXY_MOVE(proxy),
37                     std::move(colorSpaceXform),
38                     matrix,
39                     params} {
40     this->initClassID<GrSimpleTextureEffect>();
41 }
42 
43 class GrGLSimpleTextureEffect : public GrGLSLFragmentProcessor {
44 public:
emitCode(EmitArgs & args)45     void emitCode(EmitArgs& args) override {
46         const GrSimpleTextureEffect& textureEffect = args.fFp.cast<GrSimpleTextureEffect>();
47         fColorSpaceHelper.emitCode(args.fUniformHandler, textureEffect.colorSpaceXform());
48 
49         GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder;
50         fragBuilder->codeAppendf("%s = ", args.fOutputColor);
51         fragBuilder->appendTextureLookupAndModulate(args.fInputColor,
52                                                     args.fTexSamplers[0],
53                                                     args.fTransformedCoords[0].c_str(),
54                                                     args.fTransformedCoords[0].getType(),
55                                                     &fColorSpaceHelper);
56         fragBuilder->codeAppend(";");
57     }
58 
GenKey(const GrProcessor & effect,const GrShaderCaps &,GrProcessorKeyBuilder * b)59     static inline void GenKey(const GrProcessor& effect, const GrShaderCaps&,
60                               GrProcessorKeyBuilder* b) {
61         const GrSimpleTextureEffect& textureEffect = effect.cast<GrSimpleTextureEffect>();
62         b->add32(GrColorSpaceXform::XformKey(textureEffect.colorSpaceXform()));
63     }
64 
65 protected:
onSetData(const GrGLSLProgramDataManager & pdman,const GrProcessor & processor)66     void onSetData(const GrGLSLProgramDataManager& pdman, const GrProcessor& processor) override {
67         const GrSimpleTextureEffect& textureEffect = processor.cast<GrSimpleTextureEffect>();
68         if (SkToBool(textureEffect.colorSpaceXform())) {
69             fColorSpaceHelper.setData(pdman, textureEffect.colorSpaceXform());
70         }
71     }
72 
73 private:
74     typedef GrGLSLFragmentProcessor INHERITED;
75 
76     GrGLSLColorSpaceXformHelper fColorSpaceHelper;
77 };
78 
79 ///////////////////////////////////////////////////////////////////////////////
80 
onGetGLSLProcessorKey(const GrShaderCaps & caps,GrProcessorKeyBuilder * b) const81 void GrSimpleTextureEffect::onGetGLSLProcessorKey(const GrShaderCaps& caps,
82                                                   GrProcessorKeyBuilder* b) const {
83     GrGLSimpleTextureEffect::GenKey(*this, caps, b);
84 }
85 
onCreateGLSLInstance() const86 GrGLSLFragmentProcessor* GrSimpleTextureEffect::onCreateGLSLInstance() const  {
87     return new GrGLSimpleTextureEffect;
88 }
89 
90 ///////////////////////////////////////////////////////////////////////////////
91 
92 GR_DEFINE_FRAGMENT_PROCESSOR_TEST(GrSimpleTextureEffect);
93 
94 #if GR_TEST_UTILS
TestCreate(GrProcessorTestData * d)95 sk_sp<GrFragmentProcessor> GrSimpleTextureEffect::TestCreate(GrProcessorTestData* d) {
96     int texIdx = d->fRandom->nextBool() ? GrProcessorUnitTest::kSkiaPMTextureIdx
97                                         : GrProcessorUnitTest::kAlphaTextureIdx;
98     static const SkShader::TileMode kTileModes[] = {
99         SkShader::kClamp_TileMode,
100         SkShader::kRepeat_TileMode,
101         SkShader::kMirror_TileMode,
102     };
103     SkShader::TileMode tileModes[] = {
104         kTileModes[d->fRandom->nextULessThan(SK_ARRAY_COUNT(kTileModes))],
105         kTileModes[d->fRandom->nextULessThan(SK_ARRAY_COUNT(kTileModes))],
106     };
107     GrSamplerParams params(tileModes, d->fRandom->nextBool() ? GrSamplerParams::kBilerp_FilterMode
108                                                              : GrSamplerParams::kNone_FilterMode);
109 
110     const SkMatrix& matrix = GrTest::TestMatrix(d->fRandom);
111     sk_sp<GrColorSpaceXform> colorSpaceXform = GrTest::TestColorXform(d->fRandom);
112     return GrSimpleTextureEffect::Make(d->resourceProvider(), d->textureProxy(texIdx),
113                                        std::move(colorSpaceXform), matrix);
114 }
115 #endif
116