blob: 18e35e43490c517547e6b55346c7cbc5ccee5c44 [file] [log] [blame]
tomhudson@google.comd0c1a062012-07-12 17:23:52 +00001/*
2 * Copyright 2012 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 "effects/GrSingleTextureEffect.h"
bsalomon@google.comd698f772012-10-25 13:22:00 +00009#include "gl/GrGLEffect.h"
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +000010#include "gl/GrGLEffectMatrix.h"
tomhudson@google.comaa72eab2012-07-19 18:01:07 +000011#include "gl/GrGLSL.h"
12#include "gl/GrGLTexture.h"
bsalomon@google.com2eaaefd2012-10-29 19:51:22 +000013#include "GrTBackendEffectFactory.h"
tomhudson@google.comd0c1a062012-07-12 17:23:52 +000014#include "GrTexture.h"
15
bsalomon@google.com22a800a2012-10-26 19:16:46 +000016class GrGLSingleTextureEffect : public GrGLEffect {
tomhudson@google.comaa72eab2012-07-19 18:01:07 +000017public:
bsalomon@google.com396e61f2012-10-25 19:00:29 +000018 GrGLSingleTextureEffect(const GrBackendEffectFactory& factory, const GrEffect&)
bsalomon@google.comdbe49f72012-11-05 16:36:02 +000019 : INHERITED (factory) {}
tomhudson@google.comaa72eab2012-07-19 18:01:07 +000020
bsalomon@google.com22a800a2012-10-26 19:16:46 +000021 virtual void emitCode(GrGLShaderBuilder* builder,
bsalomon@google.com2eaaefd2012-10-29 19:51:22 +000022 const GrEffectStage&,
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +000023 EffectKey key,
bsalomon@google.com22a800a2012-10-26 19:16:46 +000024 const char* vertexCoords,
25 const char* outputColor,
26 const char* inputColor,
27 const TextureSamplerArray& samplers) SK_OVERRIDE {
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +000028 const char* coordName;
29 GrSLType coordType = fEffectMatrix.emitCode(builder, key, vertexCoords, &coordName);
bsalomon@google.com868a8e72012-08-30 19:11:34 +000030 builder->fFSCode.appendf("\t%s = ", outputColor);
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +000031 builder->appendTextureLookupAndModulate(&builder->fFSCode,
32 inputColor,
33 samplers[0],
34 coordName,
35 coordType);
bsalomon@google.com868a8e72012-08-30 19:11:34 +000036 builder->fFSCode.append(";\n");
tomhudson@google.comaa72eab2012-07-19 18:01:07 +000037 }
38
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +000039 static inline EffectKey GenKey(const GrEffectStage& stage, const GrGLCaps&) {
40 const GrSingleTextureEffect& ste =
41 static_cast<const GrSingleTextureEffect&>(*stage.getEffect());
42 return GrGLEffectMatrix::GenKey(ste.getMatrix(),
43 stage.getCoordChangeMatrix(),
44 ste.texture(0));
45 }
46
47 virtual void setData(const GrGLUniformManager& uman, const GrEffectStage& stage) SK_OVERRIDE {
48 const GrSingleTextureEffect& ste =
49 static_cast<const GrSingleTextureEffect&>(*stage.getEffect());
50 fEffectMatrix.setData(uman, ste.getMatrix(), stage.getCoordChangeMatrix(), ste.texture(0));
51 }
tomhudson@google.comaa72eab2012-07-19 18:01:07 +000052
53private:
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +000054 GrGLEffectMatrix fEffectMatrix;
bsalomon@google.com22a800a2012-10-26 19:16:46 +000055 typedef GrGLEffect INHERITED;
tomhudson@google.comaa72eab2012-07-19 18:01:07 +000056};
57
58///////////////////////////////////////////////////////////////////////////////
59
bsalomon@google.comb9086a02012-11-01 18:02:54 +000060GrSingleTextureEffect::GrSingleTextureEffect(GrTexture* texture, const SkMatrix& m)
bsalomon@google.com50db75c2013-01-11 13:54:30 +000061 : fTextureAccess(texture)
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +000062 , fMatrix(m) {
bsalomon@google.com50db75c2013-01-11 13:54:30 +000063 this->addTextureAccess(&fTextureAccess);
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +000064}
65
bsalomon@google.comb9086a02012-11-01 18:02:54 +000066GrSingleTextureEffect::GrSingleTextureEffect(GrTexture* texture, const SkMatrix& m, bool bilerp)
bsalomon@google.com50db75c2013-01-11 13:54:30 +000067 : fTextureAccess(texture, bilerp)
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +000068 , fMatrix(m) {
bsalomon@google.com50db75c2013-01-11 13:54:30 +000069 this->addTextureAccess(&fTextureAccess);
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +000070}
71
72GrSingleTextureEffect::GrSingleTextureEffect(GrTexture* texture,
bsalomon@google.comb9086a02012-11-01 18:02:54 +000073 const SkMatrix& m,
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +000074 const GrTextureParams& params)
bsalomon@google.com50db75c2013-01-11 13:54:30 +000075 : fTextureAccess(texture, params)
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +000076 , fMatrix(m) {
bsalomon@google.com50db75c2013-01-11 13:54:30 +000077 this->addTextureAccess(&fTextureAccess);
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +000078}
79
tomhudson@google.comd0c1a062012-07-12 17:23:52 +000080GrSingleTextureEffect::~GrSingleTextureEffect() {
tomhudson@google.comd0c1a062012-07-12 17:23:52 +000081}
82
bsalomon@google.com371e1052013-01-11 21:08:55 +000083void GrSingleTextureEffect::getConstantColorComponents(GrColor* color, uint32_t* validFlags) const {
84 // If the input alpha is 0xff and the texture has no alpha channel, then the output alpha is
85 // 0xff
86 if ((*validFlags & kA_ValidComponentFlag) && 0xFF == GrColorUnpackA(*color) &&
87 GrPixelConfigIsOpaque(fTextureAccess.getTexture()->config())) {
88 *validFlags = kA_ValidComponentFlag;
89 } else {
90 *validFlags = 0;
91 }
92}
93
bsalomon@google.com396e61f2012-10-25 19:00:29 +000094const GrBackendEffectFactory& GrSingleTextureEffect::getFactory() const {
95 return GrTBackendEffectFactory<GrSingleTextureEffect>::getInstance();
tomhudson@google.comaa72eab2012-07-19 18:01:07 +000096}
bsalomon@google.com0a7672f2012-08-03 18:12:20 +000097
98///////////////////////////////////////////////////////////////////////////////
99
bsalomon@google.comf271cc72012-10-24 19:35:13 +0000100GR_DEFINE_EFFECT_TEST(GrSingleTextureEffect);
bsalomon@google.com0a7672f2012-08-03 18:12:20 +0000101
bsalomon@google.com0ac6af42013-01-16 15:16:18 +0000102GrEffectRef* GrSingleTextureEffect::TestCreate(SkRandom* random,
103 GrContext* context,
104 GrTexture* textures[]) {
bsalomon@google.com6f261be2012-10-24 19:07:10 +0000105 int texIdx = random->nextBool() ? GrEffectUnitTest::kSkiaPMTextureIdx :
106 GrEffectUnitTest::kAlphaTextureIdx;
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +0000107 const SkMatrix& matrix = GrEffectUnitTest::TestMatrix(random);
bsalomon@google.com0ac6af42013-01-16 15:16:18 +0000108 return GrSingleTextureEffect::Create(textures[texIdx], matrix);
bsalomon@google.com0a7672f2012-08-03 18:12:20 +0000109}