blob: a5c28c5c4d693b4298e2e06589691949a8e76468 [file] [log] [blame]
commit-bot@chromium.org76eaf742013-09-30 18:41:38 +00001/*
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/GrGLEffectMatrix.h"
11#include "gl/GrGLSL.h"
12#include "gl/GrGLTexture.h"
13#include "GrTBackendEffectFactory.h"
14#include "GrTexture.h"
15
16class GrGLCustomCoordsTextureEffect : public GrGLEffect {
17public:
18 GrGLCustomCoordsTextureEffect(const GrBackendEffectFactory& factory, const GrDrawEffect& drawEffect)
19 : INHERITED (factory) {}
20
21 virtual void emitCode(GrGLShaderBuilder* builder,
22 const GrDrawEffect& drawEffect,
23 EffectKey key,
24 const char* outputColor,
25 const char* inputColor,
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
49 static inline EffectKey GenKey(const GrDrawEffect& drawEffect, const GrGLCaps&) {
50 return 1 << GrGLEffectMatrix::kKeyBits;
51 }
52
53 virtual void setData(const GrGLUniformManager& uman,
54 const GrDrawEffect& drawEffect) SK_OVERRIDE {}
55
56private:
57 typedef GrGLEffect INHERITED;
58};
59
60///////////////////////////////////////////////////////////////////////////////
61
62GrCustomCoordsTextureEffect::GrCustomCoordsTextureEffect(GrTexture* texture,
63 const GrTextureParams& params)
64 : fTextureAccess(texture, params) {
65 this->addTextureAccess(&fTextureAccess);
66 this->addVertexAttrib(kVec2f_GrSLType);
67}
68
69bool GrCustomCoordsTextureEffect::onIsEqual(const GrEffect& other) const {
70 const GrCustomCoordsTextureEffect& cte = CastEffect<GrCustomCoordsTextureEffect>(other);
71 return fTextureAccess == cte.fTextureAccess;
72}
73
74void GrCustomCoordsTextureEffect::getConstantColorComponents(GrColor* color,
75 uint32_t* validFlags) const {
76 if ((*validFlags & kA_GrColorComponentFlag) && 0xFF == GrColorUnpackA(*color) &&
77 GrPixelConfigIsOpaque(this->texture(0)->config())) {
78 *validFlags = kA_GrColorComponentFlag;
79 } else {
80 *validFlags = 0;
81 }
82}
83
84const GrBackendEffectFactory& GrCustomCoordsTextureEffect::getFactory() const {
85 return GrTBackendEffectFactory<GrCustomCoordsTextureEffect>::getInstance();
86}
87
88///////////////////////////////////////////////////////////////////////////////
89
90GR_DEFINE_EFFECT_TEST(GrCustomCoordsTextureEffect);
91
92GrEffectRef* GrCustomCoordsTextureEffect::TestCreate(SkRandom* random,
93 GrContext*,
94 const GrDrawTargetCaps&,
95 GrTexture* textures[]) {
96 int texIdx = random->nextBool() ? GrEffectUnitTest::kSkiaPMTextureIdx :
97 GrEffectUnitTest::kAlphaTextureIdx;
98 static const SkShader::TileMode kTileModes[] = {
99 SkShader::kClamp_TileMode,
100 SkShader::kRepeat_TileMode,
101 SkShader::kMirror_TileMode,
102 };
103 SkShader::TileMode tileModes[] = {
104 kTileModes[random->nextULessThan(SK_ARRAY_COUNT(kTileModes))],
105 kTileModes[random->nextULessThan(SK_ARRAY_COUNT(kTileModes))],
106 };
107 GrTextureParams params(tileModes, random->nextBool() ? GrTextureParams::kBilerp_FilterMode :
108 GrTextureParams::kNone_FilterMode);
109
110 return GrCustomCoordsTextureEffect::Create(textures[texIdx], params);
111}