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