gfx/skia/trunk/src/gpu/effects/GrCustomCoordsTextureEffect.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/gfx/skia/trunk/src/gpu/effects/GrCustomCoordsTextureEffect.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,106 @@
     1.4 +/*
     1.5 + * Copyright 2013 Google Inc.
     1.6 + *
     1.7 + * Use of this source code is governed by a BSD-style license that can be
     1.8 + * found in the LICENSE file.
     1.9 + */
    1.10 +
    1.11 +#include "GrCustomCoordsTextureEffect.h"
    1.12 +#include "gl/GrGLEffect.h"
    1.13 +#include "gl/GrGLSL.h"
    1.14 +#include "gl/GrGLTexture.h"
    1.15 +#include "gl/GrGLVertexEffect.h"
    1.16 +#include "GrTBackendEffectFactory.h"
    1.17 +#include "GrTexture.h"
    1.18 +
    1.19 +class GrGLCustomCoordsTextureEffect : public GrGLVertexEffect {
    1.20 +public:
    1.21 +    GrGLCustomCoordsTextureEffect(const GrBackendEffectFactory& factory, const GrDrawEffect& drawEffect)
    1.22 +        : INHERITED (factory) {}
    1.23 +
    1.24 +    virtual void emitCode(GrGLFullShaderBuilder* builder,
    1.25 +                          const GrDrawEffect& drawEffect,
    1.26 +                          EffectKey key,
    1.27 +                          const char* outputColor,
    1.28 +                          const char* inputColor,
    1.29 +                          const TransformedCoordsArray&,
    1.30 +                          const TextureSamplerArray& samplers) SK_OVERRIDE {
    1.31 +        SkASSERT(1 == drawEffect.castEffect<GrCustomCoordsTextureEffect>().numVertexAttribs());
    1.32 +
    1.33 +        SkString fsCoordName;
    1.34 +        const char* vsVaryingName;
    1.35 +        const char* fsVaryingNamePtr;
    1.36 +        builder->addVarying(kVec2f_GrSLType, "textureCoords", &vsVaryingName, &fsVaryingNamePtr);
    1.37 +        fsCoordName = fsVaryingNamePtr;
    1.38 +
    1.39 +        const char* attrName =
    1.40 +            builder->getEffectAttributeName(drawEffect.getVertexAttribIndices()[0])->c_str();
    1.41 +        builder->vsCodeAppendf("\t%s = %s;\n", vsVaryingName, attrName);
    1.42 +
    1.43 +        builder->fsCodeAppendf("\t%s = ", outputColor);
    1.44 +        builder->fsAppendTextureLookupAndModulate(inputColor,
    1.45 +                                                  samplers[0],
    1.46 +                                                  fsCoordName.c_str(),
    1.47 +                                                  kVec2f_GrSLType);
    1.48 +        builder->fsCodeAppend(";\n");
    1.49 +    }
    1.50 +
    1.51 +    virtual void setData(const GrGLUniformManager& uman,
    1.52 +                         const GrDrawEffect& drawEffect) SK_OVERRIDE {}
    1.53 +
    1.54 +private:
    1.55 +    typedef GrGLVertexEffect INHERITED;
    1.56 +};
    1.57 +
    1.58 +///////////////////////////////////////////////////////////////////////////////
    1.59 +
    1.60 +GrCustomCoordsTextureEffect::GrCustomCoordsTextureEffect(GrTexture* texture,
    1.61 +                                                         const GrTextureParams& params)
    1.62 +    : fTextureAccess(texture, params) {
    1.63 +    this->addTextureAccess(&fTextureAccess);
    1.64 +    this->addVertexAttrib(kVec2f_GrSLType);
    1.65 +}
    1.66 +
    1.67 +bool GrCustomCoordsTextureEffect::onIsEqual(const GrEffect& other) const {
    1.68 +    const GrCustomCoordsTextureEffect& cte = CastEffect<GrCustomCoordsTextureEffect>(other);
    1.69 +    return fTextureAccess == cte.fTextureAccess;
    1.70 +}
    1.71 +
    1.72 +void GrCustomCoordsTextureEffect::getConstantColorComponents(GrColor* color,
    1.73 +                                                             uint32_t* validFlags) const {
    1.74 +    if ((*validFlags & kA_GrColorComponentFlag) && 0xFF == GrColorUnpackA(*color) &&
    1.75 +        GrPixelConfigIsOpaque(this->texture(0)->config())) {
    1.76 +        *validFlags = kA_GrColorComponentFlag;
    1.77 +    } else {
    1.78 +        *validFlags = 0;
    1.79 +    }
    1.80 +}
    1.81 +
    1.82 +const GrBackendEffectFactory& GrCustomCoordsTextureEffect::getFactory() const {
    1.83 +    return GrTBackendEffectFactory<GrCustomCoordsTextureEffect>::getInstance();
    1.84 +}
    1.85 +
    1.86 +///////////////////////////////////////////////////////////////////////////////
    1.87 +
    1.88 +GR_DEFINE_EFFECT_TEST(GrCustomCoordsTextureEffect);
    1.89 +
    1.90 +GrEffectRef* GrCustomCoordsTextureEffect::TestCreate(SkRandom* random,
    1.91 +                                                     GrContext*,
    1.92 +                                                     const GrDrawTargetCaps&,
    1.93 +                                                     GrTexture* textures[]) {
    1.94 +    int texIdx = random->nextBool() ? GrEffectUnitTest::kSkiaPMTextureIdx :
    1.95 +                                      GrEffectUnitTest::kAlphaTextureIdx;
    1.96 +    static const SkShader::TileMode kTileModes[] = {
    1.97 +        SkShader::kClamp_TileMode,
    1.98 +        SkShader::kRepeat_TileMode,
    1.99 +        SkShader::kMirror_TileMode,
   1.100 +    };
   1.101 +    SkShader::TileMode tileModes[] = {
   1.102 +        kTileModes[random->nextULessThan(SK_ARRAY_COUNT(kTileModes))],
   1.103 +        kTileModes[random->nextULessThan(SK_ARRAY_COUNT(kTileModes))],
   1.104 +    };
   1.105 +    GrTextureParams params(tileModes, random->nextBool() ? GrTextureParams::kBilerp_FilterMode :
   1.106 +                                                           GrTextureParams::kNone_FilterMode);
   1.107 +
   1.108 +    return GrCustomCoordsTextureEffect::Create(textures[texIdx], params);
   1.109 +}

mercurial