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