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/GrGLSL.h"
1168b58c95384dd6c2fd389a5b4bbf8fc468819454bsalomon@google.com#include "gl/GrGLTexture.h"
1268b58c95384dd6c2fd389a5b4bbf8fc468819454bsalomon@google.com#include "GrTBackendEffectFactory.h"
1368b58c95384dd6c2fd389a5b4bbf8fc468819454bsalomon@google.com#include "GrTexture.h"
1468b58c95384dd6c2fd389a5b4bbf8fc468819454bsalomon@google.com
1568b58c95384dd6c2fd389a5b4bbf8fc468819454bsalomon@google.comclass GrGLSimpleTextureEffect : public GrGLEffect {
1668b58c95384dd6c2fd389a5b4bbf8fc468819454bsalomon@google.compublic:
1777af6805e5faea1e2a5c0220098aec9082f3a6e5bsalomon@google.com    GrGLSimpleTextureEffect(const GrBackendEffectFactory& factory, const GrDrawEffect&)
1877af6805e5faea1e2a5c0220098aec9082f3a6e5bsalomon@google.com        : INHERITED (factory) {
19c78188896e28a4ae49e406a7422b345ae177dafebsalomon@google.com    }
2068b58c95384dd6c2fd389a5b4bbf8fc468819454bsalomon@google.com
2168b58c95384dd6c2fd389a5b4bbf8fc468819454bsalomon@google.com    virtual void emitCode(GrGLShaderBuilder* builder,
22c78188896e28a4ae49e406a7422b345ae177dafebsalomon@google.com                          const GrDrawEffect& drawEffect,
2368b58c95384dd6c2fd389a5b4bbf8fc468819454bsalomon@google.com                          EffectKey key,
2468b58c95384dd6c2fd389a5b4bbf8fc468819454bsalomon@google.com                          const char* outputColor,
2568b58c95384dd6c2fd389a5b4bbf8fc468819454bsalomon@google.com                          const char* inputColor,
2677af6805e5faea1e2a5c0220098aec9082f3a6e5bsalomon@google.com                          const TransformedCoordsArray& coords,
2768b58c95384dd6c2fd389a5b4bbf8fc468819454bsalomon@google.com                          const TextureSamplerArray& samplers) SK_OVERRIDE {
28f910d3b23bcf590ee937628dbab8e39a98ee5860bsalomon@google.com        builder->fsCodeAppendf("\t%s = ", outputColor);
2974a3a2135ca82ab9324b7e499caa3280348a4fdacommit-bot@chromium.org        builder->fsAppendTextureLookupAndModulate(inputColor,
3074a3a2135ca82ab9324b7e499caa3280348a4fdacommit-bot@chromium.org                                                  samplers[0],
3177af6805e5faea1e2a5c0220098aec9082f3a6e5bsalomon@google.com                                                  coords[0].c_str(),
3277af6805e5faea1e2a5c0220098aec9082f3a6e5bsalomon@google.com                                                  coords[0].type());
33f910d3b23bcf590ee937628dbab8e39a98ee5860bsalomon@google.com        builder->fsCodeAppend(";\n");
3468b58c95384dd6c2fd389a5b4bbf8fc468819454bsalomon@google.com    }
3568b58c95384dd6c2fd389a5b4bbf8fc468819454bsalomon@google.com
3668b58c95384dd6c2fd389a5b4bbf8fc468819454bsalomon@google.comprivate:
3768b58c95384dd6c2fd389a5b4bbf8fc468819454bsalomon@google.com    typedef GrGLEffect INHERITED;
3868b58c95384dd6c2fd389a5b4bbf8fc468819454bsalomon@google.com};
3968b58c95384dd6c2fd389a5b4bbf8fc468819454bsalomon@google.com
4068b58c95384dd6c2fd389a5b4bbf8fc468819454bsalomon@google.com///////////////////////////////////////////////////////////////////////////////
4168b58c95384dd6c2fd389a5b4bbf8fc468819454bsalomon@google.com
4268b58c95384dd6c2fd389a5b4bbf8fc468819454bsalomon@google.comvoid GrSimpleTextureEffect::getConstantColorComponents(GrColor* color, uint32_t* validFlags) const {
4368b58c95384dd6c2fd389a5b4bbf8fc468819454bsalomon@google.com    this->updateConstantColorComponentsForModulation(color, validFlags);
4468b58c95384dd6c2fd389a5b4bbf8fc468819454bsalomon@google.com}
4568b58c95384dd6c2fd389a5b4bbf8fc468819454bsalomon@google.com
4668b58c95384dd6c2fd389a5b4bbf8fc468819454bsalomon@google.comconst GrBackendEffectFactory& GrSimpleTextureEffect::getFactory() const {
4768b58c95384dd6c2fd389a5b4bbf8fc468819454bsalomon@google.com    return GrTBackendEffectFactory<GrSimpleTextureEffect>::getInstance();
4868b58c95384dd6c2fd389a5b4bbf8fc468819454bsalomon@google.com}
4968b58c95384dd6c2fd389a5b4bbf8fc468819454bsalomon@google.com
5068b58c95384dd6c2fd389a5b4bbf8fc468819454bsalomon@google.com///////////////////////////////////////////////////////////////////////////////
5168b58c95384dd6c2fd389a5b4bbf8fc468819454bsalomon@google.com
5268b58c95384dd6c2fd389a5b4bbf8fc468819454bsalomon@google.comGR_DEFINE_EFFECT_TEST(GrSimpleTextureEffect);
5368b58c95384dd6c2fd389a5b4bbf8fc468819454bsalomon@google.com
54e0e7cfe44bb9d66d76120a79e5275c294bacaa22commit-bot@chromium.orgGrEffectRef* GrSimpleTextureEffect::TestCreate(SkRandom* random,
55e0e385c1d4171e065348ba17c546b3463a0bd651sugoi@google.com                                               GrContext*,
56c26d94fd7dc0b00cd6d0e42d28285f4a38aff021bsalomon@google.com                                               const GrDrawTargetCaps&,
5768b58c95384dd6c2fd389a5b4bbf8fc468819454bsalomon@google.com                                               GrTexture* textures[]) {
5868b58c95384dd6c2fd389a5b4bbf8fc468819454bsalomon@google.com    int texIdx = random->nextBool() ? GrEffectUnitTest::kSkiaPMTextureIdx :
5968b58c95384dd6c2fd389a5b4bbf8fc468819454bsalomon@google.com                                      GrEffectUnitTest::kAlphaTextureIdx;
60c78188896e28a4ae49e406a7422b345ae177dafebsalomon@google.com    static const SkShader::TileMode kTileModes[] = {
61c78188896e28a4ae49e406a7422b345ae177dafebsalomon@google.com        SkShader::kClamp_TileMode,
62c78188896e28a4ae49e406a7422b345ae177dafebsalomon@google.com        SkShader::kRepeat_TileMode,
63c78188896e28a4ae49e406a7422b345ae177dafebsalomon@google.com        SkShader::kMirror_TileMode,
64c78188896e28a4ae49e406a7422b345ae177dafebsalomon@google.com    };
65c78188896e28a4ae49e406a7422b345ae177dafebsalomon@google.com    SkShader::TileMode tileModes[] = {
66c78188896e28a4ae49e406a7422b345ae177dafebsalomon@google.com        kTileModes[random->nextULessThan(SK_ARRAY_COUNT(kTileModes))],
67c78188896e28a4ae49e406a7422b345ae177dafebsalomon@google.com        kTileModes[random->nextULessThan(SK_ARRAY_COUNT(kTileModes))],
68c78188896e28a4ae49e406a7422b345ae177dafebsalomon@google.com    };
69b86add1ad37776818e1f730359ec587c9fdbff5fhumper@google.com    GrTextureParams params(tileModes, random->nextBool() ? GrTextureParams::kBilerp_FilterMode :
70b86add1ad37776818e1f730359ec587c9fdbff5fhumper@google.com                                                           GrTextureParams::kNone_FilterMode);
71c78188896e28a4ae49e406a7422b345ae177dafebsalomon@google.com
7277af6805e5faea1e2a5c0220098aec9082f3a6e5bsalomon@google.com    static const GrCoordSet kCoordSets[] = {
7377af6805e5faea1e2a5c0220098aec9082f3a6e5bsalomon@google.com        kLocal_GrCoordSet,
7477af6805e5faea1e2a5c0220098aec9082f3a6e5bsalomon@google.com        kPosition_GrCoordSet
75c78188896e28a4ae49e406a7422b345ae177dafebsalomon@google.com    };
76972f9cd7a063d0544f8c919fd12b9a3adbd12b24commit-bot@chromium.org    GrCoordSet coordSet = kCoordSets[random->nextULessThan(SK_ARRAY_COUNT(kCoordSets))];
77c78188896e28a4ae49e406a7422b345ae177dafebsalomon@google.com
7876eaf749cfb903916488bfaf90c40470033ed216commit-bot@chromium.org    const SkMatrix& matrix = GrEffectUnitTest::TestMatrix(random);
7977af6805e5faea1e2a5c0220098aec9082f3a6e5bsalomon@google.com    return GrSimpleTextureEffect::Create(textures[texIdx], matrix, coordSet);
8068b58c95384dd6c2fd389a5b4bbf8fc468819454bsalomon@google.com}
81