commit-bot@chromium.org | 76eaf74 | 2013-09-30 18:41:38 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2013 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 "GrCustomCoordsTextureEffect.h" |
| 9 | #include "gl/GrGLEffect.h" |
commit-bot@chromium.org | 76eaf74 | 2013-09-30 18:41:38 +0000 | [diff] [blame] | 10 | #include "gl/GrGLSL.h" |
| 11 | #include "gl/GrGLTexture.h" |
| 12 | #include "GrTBackendEffectFactory.h" |
| 13 | #include "GrTexture.h" |
| 14 | |
| 15 | class GrGLCustomCoordsTextureEffect : public GrGLEffect { |
| 16 | public: |
| 17 | GrGLCustomCoordsTextureEffect(const GrBackendEffectFactory& factory, const GrDrawEffect& drawEffect) |
| 18 | : INHERITED (factory) {} |
| 19 | |
| 20 | virtual void emitCode(GrGLShaderBuilder* builder, |
| 21 | const GrDrawEffect& drawEffect, |
| 22 | EffectKey key, |
| 23 | const char* outputColor, |
| 24 | const char* inputColor, |
bsalomon@google.com | 77af680 | 2013-10-02 13:04:56 +0000 | [diff] [blame] | 25 | const TransformedCoordsArray&, |
commit-bot@chromium.org | 76eaf74 | 2013-09-30 18:41:38 +0000 | [diff] [blame] | 26 | const TextureSamplerArray& samplers) SK_OVERRIDE { |
| 27 | GrGLShaderBuilder::VertexBuilder* vertexBuilder = builder->getVertexBuilder(); |
| 28 | SkASSERT(NULL != vertexBuilder); |
| 29 | SkASSERT(1 == drawEffect.castEffect<GrCustomCoordsTextureEffect>().numVertexAttribs()); |
| 30 | |
| 31 | SkString fsCoordName; |
| 32 | const char* vsVaryingName; |
| 33 | const char* fsVaryingNamePtr; |
| 34 | vertexBuilder->addVarying(kVec2f_GrSLType, "textureCoords", &vsVaryingName, &fsVaryingNamePtr); |
| 35 | fsCoordName = fsVaryingNamePtr; |
| 36 | |
| 37 | const char* attrName = |
| 38 | vertexBuilder->getEffectAttributeName(drawEffect.getVertexAttribIndices()[0])->c_str(); |
| 39 | vertexBuilder->vsCodeAppendf("\t%s = %s;\n", vsVaryingName, attrName); |
| 40 | |
| 41 | builder->fsCodeAppendf("\t%s = ", outputColor); |
| 42 | builder->fsAppendTextureLookupAndModulate(inputColor, |
| 43 | samplers[0], |
| 44 | fsCoordName.c_str(), |
| 45 | kVec2f_GrSLType); |
| 46 | builder->fsCodeAppend(";\n"); |
| 47 | } |
| 48 | |
commit-bot@chromium.org | 76eaf74 | 2013-09-30 18:41:38 +0000 | [diff] [blame] | 49 | virtual void setData(const GrGLUniformManager& uman, |
| 50 | const GrDrawEffect& drawEffect) SK_OVERRIDE {} |
| 51 | |
| 52 | private: |
| 53 | typedef GrGLEffect INHERITED; |
| 54 | }; |
| 55 | |
| 56 | /////////////////////////////////////////////////////////////////////////////// |
| 57 | |
| 58 | GrCustomCoordsTextureEffect::GrCustomCoordsTextureEffect(GrTexture* texture, |
| 59 | const GrTextureParams& params) |
| 60 | : fTextureAccess(texture, params) { |
| 61 | this->addTextureAccess(&fTextureAccess); |
| 62 | this->addVertexAttrib(kVec2f_GrSLType); |
| 63 | } |
| 64 | |
| 65 | bool GrCustomCoordsTextureEffect::onIsEqual(const GrEffect& other) const { |
| 66 | const GrCustomCoordsTextureEffect& cte = CastEffect<GrCustomCoordsTextureEffect>(other); |
| 67 | return fTextureAccess == cte.fTextureAccess; |
| 68 | } |
| 69 | |
| 70 | void GrCustomCoordsTextureEffect::getConstantColorComponents(GrColor* color, |
| 71 | uint32_t* validFlags) const { |
| 72 | if ((*validFlags & kA_GrColorComponentFlag) && 0xFF == GrColorUnpackA(*color) && |
| 73 | GrPixelConfigIsOpaque(this->texture(0)->config())) { |
| 74 | *validFlags = kA_GrColorComponentFlag; |
| 75 | } else { |
| 76 | *validFlags = 0; |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | const GrBackendEffectFactory& GrCustomCoordsTextureEffect::getFactory() const { |
| 81 | return GrTBackendEffectFactory<GrCustomCoordsTextureEffect>::getInstance(); |
| 82 | } |
| 83 | |
| 84 | /////////////////////////////////////////////////////////////////////////////// |
| 85 | |
| 86 | GR_DEFINE_EFFECT_TEST(GrCustomCoordsTextureEffect); |
| 87 | |
| 88 | GrEffectRef* GrCustomCoordsTextureEffect::TestCreate(SkRandom* random, |
| 89 | GrContext*, |
| 90 | const GrDrawTargetCaps&, |
| 91 | GrTexture* textures[]) { |
| 92 | int texIdx = random->nextBool() ? GrEffectUnitTest::kSkiaPMTextureIdx : |
| 93 | GrEffectUnitTest::kAlphaTextureIdx; |
| 94 | static const SkShader::TileMode kTileModes[] = { |
| 95 | SkShader::kClamp_TileMode, |
| 96 | SkShader::kRepeat_TileMode, |
| 97 | SkShader::kMirror_TileMode, |
| 98 | }; |
| 99 | SkShader::TileMode tileModes[] = { |
| 100 | kTileModes[random->nextULessThan(SK_ARRAY_COUNT(kTileModes))], |
| 101 | kTileModes[random->nextULessThan(SK_ARRAY_COUNT(kTileModes))], |
| 102 | }; |
| 103 | GrTextureParams params(tileModes, random->nextBool() ? GrTextureParams::kBilerp_FilterMode : |
| 104 | GrTextureParams::kNone_FilterMode); |
| 105 | |
| 106 | return GrCustomCoordsTextureEffect::Create(textures[texIdx], params); |
| 107 | } |