michael@0: /* michael@0: * Copyright 2013 Google Inc. michael@0: * michael@0: * Use of this source code is governed by a BSD-style license that can be michael@0: * found in the LICENSE file. michael@0: */ michael@0: michael@0: #include "GrCustomCoordsTextureEffect.h" michael@0: #include "gl/GrGLEffect.h" michael@0: #include "gl/GrGLSL.h" michael@0: #include "gl/GrGLTexture.h" michael@0: #include "gl/GrGLVertexEffect.h" michael@0: #include "GrTBackendEffectFactory.h" michael@0: #include "GrTexture.h" michael@0: michael@0: class GrGLCustomCoordsTextureEffect : public GrGLVertexEffect { michael@0: public: michael@0: GrGLCustomCoordsTextureEffect(const GrBackendEffectFactory& factory, const GrDrawEffect& drawEffect) michael@0: : INHERITED (factory) {} michael@0: michael@0: virtual void emitCode(GrGLFullShaderBuilder* builder, michael@0: const GrDrawEffect& drawEffect, michael@0: EffectKey key, michael@0: const char* outputColor, michael@0: const char* inputColor, michael@0: const TransformedCoordsArray&, michael@0: const TextureSamplerArray& samplers) SK_OVERRIDE { michael@0: SkASSERT(1 == drawEffect.castEffect().numVertexAttribs()); michael@0: michael@0: SkString fsCoordName; michael@0: const char* vsVaryingName; michael@0: const char* fsVaryingNamePtr; michael@0: builder->addVarying(kVec2f_GrSLType, "textureCoords", &vsVaryingName, &fsVaryingNamePtr); michael@0: fsCoordName = fsVaryingNamePtr; michael@0: michael@0: const char* attrName = michael@0: builder->getEffectAttributeName(drawEffect.getVertexAttribIndices()[0])->c_str(); michael@0: builder->vsCodeAppendf("\t%s = %s;\n", vsVaryingName, attrName); michael@0: michael@0: builder->fsCodeAppendf("\t%s = ", outputColor); michael@0: builder->fsAppendTextureLookupAndModulate(inputColor, michael@0: samplers[0], michael@0: fsCoordName.c_str(), michael@0: kVec2f_GrSLType); michael@0: builder->fsCodeAppend(";\n"); michael@0: } michael@0: michael@0: virtual void setData(const GrGLUniformManager& uman, michael@0: const GrDrawEffect& drawEffect) SK_OVERRIDE {} michael@0: michael@0: private: michael@0: typedef GrGLVertexEffect INHERITED; michael@0: }; michael@0: michael@0: /////////////////////////////////////////////////////////////////////////////// michael@0: michael@0: GrCustomCoordsTextureEffect::GrCustomCoordsTextureEffect(GrTexture* texture, michael@0: const GrTextureParams& params) michael@0: : fTextureAccess(texture, params) { michael@0: this->addTextureAccess(&fTextureAccess); michael@0: this->addVertexAttrib(kVec2f_GrSLType); michael@0: } michael@0: michael@0: bool GrCustomCoordsTextureEffect::onIsEqual(const GrEffect& other) const { michael@0: const GrCustomCoordsTextureEffect& cte = CastEffect(other); michael@0: return fTextureAccess == cte.fTextureAccess; michael@0: } michael@0: michael@0: void GrCustomCoordsTextureEffect::getConstantColorComponents(GrColor* color, michael@0: uint32_t* validFlags) const { michael@0: if ((*validFlags & kA_GrColorComponentFlag) && 0xFF == GrColorUnpackA(*color) && michael@0: GrPixelConfigIsOpaque(this->texture(0)->config())) { michael@0: *validFlags = kA_GrColorComponentFlag; michael@0: } else { michael@0: *validFlags = 0; michael@0: } michael@0: } michael@0: michael@0: const GrBackendEffectFactory& GrCustomCoordsTextureEffect::getFactory() const { michael@0: return GrTBackendEffectFactory::getInstance(); michael@0: } michael@0: michael@0: /////////////////////////////////////////////////////////////////////////////// michael@0: michael@0: GR_DEFINE_EFFECT_TEST(GrCustomCoordsTextureEffect); michael@0: michael@0: GrEffectRef* GrCustomCoordsTextureEffect::TestCreate(SkRandom* random, michael@0: GrContext*, michael@0: const GrDrawTargetCaps&, michael@0: GrTexture* textures[]) { michael@0: int texIdx = random->nextBool() ? GrEffectUnitTest::kSkiaPMTextureIdx : michael@0: GrEffectUnitTest::kAlphaTextureIdx; michael@0: static const SkShader::TileMode kTileModes[] = { michael@0: SkShader::kClamp_TileMode, michael@0: SkShader::kRepeat_TileMode, michael@0: SkShader::kMirror_TileMode, michael@0: }; michael@0: SkShader::TileMode tileModes[] = { michael@0: kTileModes[random->nextULessThan(SK_ARRAY_COUNT(kTileModes))], michael@0: kTileModes[random->nextULessThan(SK_ARRAY_COUNT(kTileModes))], michael@0: }; michael@0: GrTextureParams params(tileModes, random->nextBool() ? GrTextureParams::kBilerp_FilterMode : michael@0: GrTextureParams::kNone_FilterMode); michael@0: michael@0: return GrCustomCoordsTextureEffect::Create(textures[texIdx], params); michael@0: }