GrSimpleTextureEffect.cpp revision 76eaf749cfb903916488bfaf90c40470033ed216
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 "gl/GrGLEffect.h"
10#include "gl/GrGLEffectMatrix.h"
11#include "gl/GrGLSL.h"
12#include "gl/GrGLTexture.h"
13#include "GrTBackendEffectFactory.h"
14#include "GrTexture.h"
15
16class GrGLSimpleTextureEffect : public GrGLEffect {
17public:
18    GrGLSimpleTextureEffect(const GrBackendEffectFactory& factory, const GrDrawEffect& drawEffect)
19        : INHERITED (factory)
20        , fEffectMatrix(drawEffect.castEffect<GrSimpleTextureEffect>().coordsType()) {
21    }
22
23    virtual void emitCode(GrGLShaderBuilder* builder,
24                          const GrDrawEffect& drawEffect,
25                          EffectKey key,
26                          const char* outputColor,
27                          const char* inputColor,
28                          const TextureSamplerArray& samplers) SK_OVERRIDE {
29        SkString fsCoordName;
30        GrSLType fsCoordSLType;
31        fsCoordSLType = fEffectMatrix.emitCode(builder, key, &fsCoordName);
32
33        builder->fsCodeAppendf("\t%s = ", outputColor);
34        builder->fsAppendTextureLookupAndModulate(inputColor,
35                                                  samplers[0],
36                                                  fsCoordName.c_str(),
37                                                  fsCoordSLType);
38        builder->fsCodeAppend(";\n");
39    }
40
41    static inline EffectKey GenKey(const GrDrawEffect& drawEffect, const GrGLCaps&) {
42        const GrSimpleTextureEffect& ste = drawEffect.castEffect<GrSimpleTextureEffect>();
43        return GrGLEffectMatrix::GenKey(ste.getMatrix(),
44                                        drawEffect,
45                                        ste.coordsType(),
46                                        ste.texture(0));
47    }
48
49    virtual void setData(const GrGLUniformManager& uman,
50                         const GrDrawEffect& drawEffect) SK_OVERRIDE {
51        const GrSimpleTextureEffect& ste = drawEffect.castEffect<GrSimpleTextureEffect>();
52        fEffectMatrix.setData(uman, ste.getMatrix(), drawEffect, ste.texture(0));
53    }
54
55private:
56    GrGLEffectMatrix fEffectMatrix;
57    typedef GrGLEffect INHERITED;
58};
59
60///////////////////////////////////////////////////////////////////////////////
61
62void GrSimpleTextureEffect::getConstantColorComponents(GrColor* color, uint32_t* validFlags) const {
63    this->updateConstantColorComponentsForModulation(color, validFlags);
64}
65
66const GrBackendEffectFactory& GrSimpleTextureEffect::getFactory() const {
67    return GrTBackendEffectFactory<GrSimpleTextureEffect>::getInstance();
68}
69
70///////////////////////////////////////////////////////////////////////////////
71
72GR_DEFINE_EFFECT_TEST(GrSimpleTextureEffect);
73
74GrEffectRef* GrSimpleTextureEffect::TestCreate(SkRandom* random,
75                                               GrContext*,
76                                               const GrDrawTargetCaps&,
77                                               GrTexture* textures[]) {
78    int texIdx = random->nextBool() ? GrEffectUnitTest::kSkiaPMTextureIdx :
79                                      GrEffectUnitTest::kAlphaTextureIdx;
80    static const SkShader::TileMode kTileModes[] = {
81        SkShader::kClamp_TileMode,
82        SkShader::kRepeat_TileMode,
83        SkShader::kMirror_TileMode,
84    };
85    SkShader::TileMode tileModes[] = {
86        kTileModes[random->nextULessThan(SK_ARRAY_COUNT(kTileModes))],
87        kTileModes[random->nextULessThan(SK_ARRAY_COUNT(kTileModes))],
88    };
89    GrTextureParams params(tileModes, random->nextBool() ? GrTextureParams::kBilerp_FilterMode :
90                                                           GrTextureParams::kNone_FilterMode);
91
92    static const CoordsType kCoordsTypes[] = {
93        kLocal_CoordsType,
94        kPosition_CoordsType
95    };
96    CoordsType coordsType = kCoordsTypes[random->nextULessThan(GR_ARRAY_COUNT(kCoordsTypes))];
97
98    const SkMatrix& matrix = GrEffectUnitTest::TestMatrix(random);
99    return GrSimpleTextureEffect::Create(textures[texIdx], matrix, coordsType);
100}
101