GrSimpleTextureEffect.cpp revision 76eaf749cfb903916488bfaf90c40470033ed216
168b58c95384dd6c2fd389a5b4bbf8fc468819454bsalomon@google.com/*
268b58c95384dd6c2fd389a5b4bbf8fc468819454bsalomon@google.com * Copyright 2012 Google Inc.
368b58c95384dd6c2fd389a5b4bbf8fc468819454bsalomon@google.com *
468b58c95384dd6c2fd389a5b4bbf8fc468819454bsalomon@google.com * Use of this source code is governed by a BSD-style license that can be
568b58c95384dd6c2fd389a5b4bbf8fc468819454bsalomon@google.com * found in the LICENSE file.
668b58c95384dd6c2fd389a5b4bbf8fc468819454bsalomon@google.com */
768b58c95384dd6c2fd389a5b4bbf8fc468819454bsalomon@google.com
868b58c95384dd6c2fd389a5b4bbf8fc468819454bsalomon@google.com#include "GrSimpleTextureEffect.h"
968b58c95384dd6c2fd389a5b4bbf8fc468819454bsalomon@google.com#include "gl/GrGLEffect.h"
1068b58c95384dd6c2fd389a5b4bbf8fc468819454bsalomon@google.com#include "gl/GrGLEffectMatrix.h"
1168b58c95384dd6c2fd389a5b4bbf8fc468819454bsalomon@google.com#include "gl/GrGLSL.h"
1268b58c95384dd6c2fd389a5b4bbf8fc468819454bsalomon@google.com#include "gl/GrGLTexture.h"
1368b58c95384dd6c2fd389a5b4bbf8fc468819454bsalomon@google.com#include "GrTBackendEffectFactory.h"
1468b58c95384dd6c2fd389a5b4bbf8fc468819454bsalomon@google.com#include "GrTexture.h"
1568b58c95384dd6c2fd389a5b4bbf8fc468819454bsalomon@google.com
1668b58c95384dd6c2fd389a5b4bbf8fc468819454bsalomon@google.comclass GrGLSimpleTextureEffect : public GrGLEffect {
1768b58c95384dd6c2fd389a5b4bbf8fc468819454bsalomon@google.compublic:
18c78188896e28a4ae49e406a7422b345ae177dafebsalomon@google.com    GrGLSimpleTextureEffect(const GrBackendEffectFactory& factory, const GrDrawEffect& drawEffect)
1976eaf749cfb903916488bfaf90c40470033ed216commit-bot@chromium.org        : INHERITED (factory)
2076eaf749cfb903916488bfaf90c40470033ed216commit-bot@chromium.org        , fEffectMatrix(drawEffect.castEffect<GrSimpleTextureEffect>().coordsType()) {
21c78188896e28a4ae49e406a7422b345ae177dafebsalomon@google.com    }
2268b58c95384dd6c2fd389a5b4bbf8fc468819454bsalomon@google.com
2368b58c95384dd6c2fd389a5b4bbf8fc468819454bsalomon@google.com    virtual void emitCode(GrGLShaderBuilder* builder,
24c78188896e28a4ae49e406a7422b345ae177dafebsalomon@google.com                          const GrDrawEffect& drawEffect,
2568b58c95384dd6c2fd389a5b4bbf8fc468819454bsalomon@google.com                          EffectKey key,
2668b58c95384dd6c2fd389a5b4bbf8fc468819454bsalomon@google.com                          const char* outputColor,
2768b58c95384dd6c2fd389a5b4bbf8fc468819454bsalomon@google.com                          const char* inputColor,
2868b58c95384dd6c2fd389a5b4bbf8fc468819454bsalomon@google.com                          const TextureSamplerArray& samplers) SK_OVERRIDE {
297ab7ca4199e97126de01d507d34f60a07843937fcommit-bot@chromium.org        SkString fsCoordName;
30c78188896e28a4ae49e406a7422b345ae177dafebsalomon@google.com        GrSLType fsCoordSLType;
3176eaf749cfb903916488bfaf90c40470033ed216commit-bot@chromium.org        fsCoordSLType = fEffectMatrix.emitCode(builder, key, &fsCoordName);
3276eaf749cfb903916488bfaf90c40470033ed216commit-bot@chromium.org
33f910d3b23bcf590ee937628dbab8e39a98ee5860bsalomon@google.com        builder->fsCodeAppendf("\t%s = ", outputColor);
3474a3a2135ca82ab9324b7e499caa3280348a4fdacommit-bot@chromium.org        builder->fsAppendTextureLookupAndModulate(inputColor,
3574a3a2135ca82ab9324b7e499caa3280348a4fdacommit-bot@chromium.org                                                  samplers[0],
3674a3a2135ca82ab9324b7e499caa3280348a4fdacommit-bot@chromium.org                                                  fsCoordName.c_str(),
3774a3a2135ca82ab9324b7e499caa3280348a4fdacommit-bot@chromium.org                                                  fsCoordSLType);
38f910d3b23bcf590ee937628dbab8e39a98ee5860bsalomon@google.com        builder->fsCodeAppend(";\n");
3968b58c95384dd6c2fd389a5b4bbf8fc468819454bsalomon@google.com    }
4068b58c95384dd6c2fd389a5b4bbf8fc468819454bsalomon@google.com
41c78188896e28a4ae49e406a7422b345ae177dafebsalomon@google.com    static inline EffectKey GenKey(const GrDrawEffect& drawEffect, const GrGLCaps&) {
42c78188896e28a4ae49e406a7422b345ae177dafebsalomon@google.com        const GrSimpleTextureEffect& ste = drawEffect.castEffect<GrSimpleTextureEffect>();
4376eaf749cfb903916488bfaf90c40470033ed216commit-bot@chromium.org        return GrGLEffectMatrix::GenKey(ste.getMatrix(),
4476eaf749cfb903916488bfaf90c40470033ed216commit-bot@chromium.org                                        drawEffect,
4576eaf749cfb903916488bfaf90c40470033ed216commit-bot@chromium.org                                        ste.coordsType(),
4676eaf749cfb903916488bfaf90c40470033ed216commit-bot@chromium.org                                        ste.texture(0));
4768b58c95384dd6c2fd389a5b4bbf8fc468819454bsalomon@google.com    }
4868b58c95384dd6c2fd389a5b4bbf8fc468819454bsalomon@google.com
49c78188896e28a4ae49e406a7422b345ae177dafebsalomon@google.com    virtual void setData(const GrGLUniformManager& uman,
50c78188896e28a4ae49e406a7422b345ae177dafebsalomon@google.com                         const GrDrawEffect& drawEffect) SK_OVERRIDE {
51c78188896e28a4ae49e406a7422b345ae177dafebsalomon@google.com        const GrSimpleTextureEffect& ste = drawEffect.castEffect<GrSimpleTextureEffect>();
5276eaf749cfb903916488bfaf90c40470033ed216commit-bot@chromium.org        fEffectMatrix.setData(uman, ste.getMatrix(), drawEffect, ste.texture(0));
5368b58c95384dd6c2fd389a5b4bbf8fc468819454bsalomon@google.com    }
5468b58c95384dd6c2fd389a5b4bbf8fc468819454bsalomon@google.com
5568b58c95384dd6c2fd389a5b4bbf8fc468819454bsalomon@google.comprivate:
5676eaf749cfb903916488bfaf90c40470033ed216commit-bot@chromium.org    GrGLEffectMatrix fEffectMatrix;
5768b58c95384dd6c2fd389a5b4bbf8fc468819454bsalomon@google.com    typedef GrGLEffect INHERITED;
5868b58c95384dd6c2fd389a5b4bbf8fc468819454bsalomon@google.com};
5968b58c95384dd6c2fd389a5b4bbf8fc468819454bsalomon@google.com
6068b58c95384dd6c2fd389a5b4bbf8fc468819454bsalomon@google.com///////////////////////////////////////////////////////////////////////////////
6168b58c95384dd6c2fd389a5b4bbf8fc468819454bsalomon@google.com
6268b58c95384dd6c2fd389a5b4bbf8fc468819454bsalomon@google.comvoid GrSimpleTextureEffect::getConstantColorComponents(GrColor* color, uint32_t* validFlags) const {
6368b58c95384dd6c2fd389a5b4bbf8fc468819454bsalomon@google.com    this->updateConstantColorComponentsForModulation(color, validFlags);
6468b58c95384dd6c2fd389a5b4bbf8fc468819454bsalomon@google.com}
6568b58c95384dd6c2fd389a5b4bbf8fc468819454bsalomon@google.com
6668b58c95384dd6c2fd389a5b4bbf8fc468819454bsalomon@google.comconst GrBackendEffectFactory& GrSimpleTextureEffect::getFactory() const {
6768b58c95384dd6c2fd389a5b4bbf8fc468819454bsalomon@google.com    return GrTBackendEffectFactory<GrSimpleTextureEffect>::getInstance();
6868b58c95384dd6c2fd389a5b4bbf8fc468819454bsalomon@google.com}
6968b58c95384dd6c2fd389a5b4bbf8fc468819454bsalomon@google.com
7068b58c95384dd6c2fd389a5b4bbf8fc468819454bsalomon@google.com///////////////////////////////////////////////////////////////////////////////
7168b58c95384dd6c2fd389a5b4bbf8fc468819454bsalomon@google.com
7268b58c95384dd6c2fd389a5b4bbf8fc468819454bsalomon@google.comGR_DEFINE_EFFECT_TEST(GrSimpleTextureEffect);
7368b58c95384dd6c2fd389a5b4bbf8fc468819454bsalomon@google.com
74e0e7cfe44bb9d66d76120a79e5275c294bacaa22commit-bot@chromium.orgGrEffectRef* GrSimpleTextureEffect::TestCreate(SkRandom* random,
75e0e385c1d4171e065348ba17c546b3463a0bd651sugoi@google.com                                               GrContext*,
76c26d94fd7dc0b00cd6d0e42d28285f4a38aff021bsalomon@google.com                                               const GrDrawTargetCaps&,
7768b58c95384dd6c2fd389a5b4bbf8fc468819454bsalomon@google.com                                               GrTexture* textures[]) {
7868b58c95384dd6c2fd389a5b4bbf8fc468819454bsalomon@google.com    int texIdx = random->nextBool() ? GrEffectUnitTest::kSkiaPMTextureIdx :
7968b58c95384dd6c2fd389a5b4bbf8fc468819454bsalomon@google.com                                      GrEffectUnitTest::kAlphaTextureIdx;
80c78188896e28a4ae49e406a7422b345ae177dafebsalomon@google.com    static const SkShader::TileMode kTileModes[] = {
81c78188896e28a4ae49e406a7422b345ae177dafebsalomon@google.com        SkShader::kClamp_TileMode,
82c78188896e28a4ae49e406a7422b345ae177dafebsalomon@google.com        SkShader::kRepeat_TileMode,
83c78188896e28a4ae49e406a7422b345ae177dafebsalomon@google.com        SkShader::kMirror_TileMode,
84c78188896e28a4ae49e406a7422b345ae177dafebsalomon@google.com    };
85c78188896e28a4ae49e406a7422b345ae177dafebsalomon@google.com    SkShader::TileMode tileModes[] = {
86c78188896e28a4ae49e406a7422b345ae177dafebsalomon@google.com        kTileModes[random->nextULessThan(SK_ARRAY_COUNT(kTileModes))],
87c78188896e28a4ae49e406a7422b345ae177dafebsalomon@google.com        kTileModes[random->nextULessThan(SK_ARRAY_COUNT(kTileModes))],
88c78188896e28a4ae49e406a7422b345ae177dafebsalomon@google.com    };
89b86add1ad37776818e1f730359ec587c9fdbff5fhumper@google.com    GrTextureParams params(tileModes, random->nextBool() ? GrTextureParams::kBilerp_FilterMode :
90b86add1ad37776818e1f730359ec587c9fdbff5fhumper@google.com                                                           GrTextureParams::kNone_FilterMode);
91c78188896e28a4ae49e406a7422b345ae177dafebsalomon@google.com
92c78188896e28a4ae49e406a7422b345ae177dafebsalomon@google.com    static const CoordsType kCoordsTypes[] = {
93c78188896e28a4ae49e406a7422b345ae177dafebsalomon@google.com        kLocal_CoordsType,
9476eaf749cfb903916488bfaf90c40470033ed216commit-bot@chromium.org        kPosition_CoordsType
95c78188896e28a4ae49e406a7422b345ae177dafebsalomon@google.com    };
96c78188896e28a4ae49e406a7422b345ae177dafebsalomon@google.com    CoordsType coordsType = kCoordsTypes[random->nextULessThan(GR_ARRAY_COUNT(kCoordsTypes))];
97c78188896e28a4ae49e406a7422b345ae177dafebsalomon@google.com
9876eaf749cfb903916488bfaf90c40470033ed216commit-bot@chromium.org    const SkMatrix& matrix = GrEffectUnitTest::TestMatrix(random);
9976eaf749cfb903916488bfaf90c40470033ed216commit-bot@chromium.org    return GrSimpleTextureEffect::Create(textures[texIdx], matrix, coordsType);
10068b58c95384dd6c2fd389a5b4bbf8fc468819454bsalomon@google.com}
101