aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/gpu/effects/GrSimpleTextureEffect.cpp
blob: a41ed8b0b99b152e52d546b7b76215870737d1e3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
/*
 * Copyright 2012 Google Inc.
 *
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

#include "GrSimpleTextureEffect.h"
#include "GrProxyMove.h"
#include "GrTexture.h"
#include "glsl/GrGLSLColorSpaceXformHelper.h"
#include "glsl/GrGLSLFragmentProcessor.h"
#include "glsl/GrGLSLFragmentShaderBuilder.h"

GrSimpleTextureEffect::GrSimpleTextureEffect(GrResourceProvider* resourceProvider,
                                             sk_sp<GrTextureProxy> proxy,
                                             sk_sp<GrColorSpaceXform> colorSpaceXform,
                                             const SkMatrix& matrix,
                                             GrSamplerParams::FilterMode filterMode)
        : INHERITED{resourceProvider,
                    ModulationFlags(proxy->config()),
                    GR_PROXY_MOVE(proxy),
                    std::move(colorSpaceXform),
                    matrix,
                    filterMode} {
    this->initClassID<GrSimpleTextureEffect>();
}

GrSimpleTextureEffect::GrSimpleTextureEffect(GrResourceProvider* resourceProvider,
                                             sk_sp<GrTextureProxy> proxy,
                                             sk_sp<GrColorSpaceXform> colorSpaceXform,
                                             const SkMatrix& matrix,
                                             const GrSamplerParams& params)
        : INHERITED{resourceProvider,
                    ModulationFlags(proxy->config()),
                    GR_PROXY_MOVE(proxy),
                    std::move(colorSpaceXform),
                    matrix,
                    params} {
    this->initClassID<GrSimpleTextureEffect>();
}

class GrGLSimpleTextureEffect : public GrGLSLFragmentProcessor {
public:
    void emitCode(EmitArgs& args) override {
        const GrSimpleTextureEffect& textureEffect = args.fFp.cast<GrSimpleTextureEffect>();
        fColorSpaceHelper.emitCode(args.fUniformHandler, textureEffect.colorSpaceXform());

        GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder;
        fragBuilder->codeAppendf("%s = ", args.fOutputColor);
        fragBuilder->appendTextureLookupAndModulate(args.fInputColor,
                                                    args.fTexSamplers[0],
                                                    args.fTransformedCoords[0].c_str(),
                                                    args.fTransformedCoords[0].getType(),
                                                    &fColorSpaceHelper);
        fragBuilder->codeAppend(";");
    }

    static inline void GenKey(const GrProcessor& effect, const GrShaderCaps&,
                              GrProcessorKeyBuilder* b) {
        const GrSimpleTextureEffect& textureEffect = effect.cast<GrSimpleTextureEffect>();
        b->add32(GrColorSpaceXform::XformKey(textureEffect.colorSpaceXform()));
    }

protected:
    void onSetData(const GrGLSLProgramDataManager& pdman,
                   const GrFragmentProcessor& processor) override {
        const GrSimpleTextureEffect& textureEffect = processor.cast<GrSimpleTextureEffect>();
        if (SkToBool(textureEffect.colorSpaceXform())) {
            fColorSpaceHelper.setData(pdman, textureEffect.colorSpaceXform());
        }
    }

private:
    typedef GrGLSLFragmentProcessor INHERITED;

    GrGLSLColorSpaceXformHelper fColorSpaceHelper;
};

///////////////////////////////////////////////////////////////////////////////

void GrSimpleTextureEffect::onGetGLSLProcessorKey(const GrShaderCaps& caps,
                                                  GrProcessorKeyBuilder* b) const {
    GrGLSimpleTextureEffect::GenKey(*this, caps, b);
}

GrGLSLFragmentProcessor* GrSimpleTextureEffect::onCreateGLSLInstance() const  {
    return new GrGLSimpleTextureEffect;
}

///////////////////////////////////////////////////////////////////////////////

GR_DEFINE_FRAGMENT_PROCESSOR_TEST(GrSimpleTextureEffect);

#if GR_TEST_UTILS
sk_sp<GrFragmentProcessor> GrSimpleTextureEffect::TestCreate(GrProcessorTestData* d) {
    int texIdx = d->fRandom->nextBool() ? GrProcessorUnitTest::kSkiaPMTextureIdx
                                        : GrProcessorUnitTest::kAlphaTextureIdx;
    static const SkShader::TileMode kTileModes[] = {
        SkShader::kClamp_TileMode,
        SkShader::kRepeat_TileMode,
        SkShader::kMirror_TileMode,
    };
    SkShader::TileMode tileModes[] = {
        kTileModes[d->fRandom->nextULessThan(SK_ARRAY_COUNT(kTileModes))],
        kTileModes[d->fRandom->nextULessThan(SK_ARRAY_COUNT(kTileModes))],
    };
    GrSamplerParams params(tileModes, d->fRandom->nextBool() ? GrSamplerParams::kBilerp_FilterMode
                                                             : GrSamplerParams::kNone_FilterMode);

    const SkMatrix& matrix = GrTest::TestMatrix(d->fRandom);
    sk_sp<GrColorSpaceXform> colorSpaceXform = GrTest::TestColorXform(d->fRandom);
    return GrSimpleTextureEffect::Make(d->resourceProvider(), d->textureProxy(texIdx),
                                       std::move(colorSpaceXform), matrix);
}
#endif