blob: 58c8b807621493ce9a99b18329a99727e92d768b [file] [log] [blame]
tomhudson@google.com2f68e762012-07-17 18:43:21 +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 "GrTextureDomainEffect.h"
bsalomon@google.com68b58c92013-01-17 16:50:08 +00009#include "GrSimpleTextureEffect.h"
bsalomon@google.com2eaaefd2012-10-29 19:51:22 +000010#include "GrTBackendEffectFactory.h"
bsalomon@google.comd698f772012-10-25 13:22:00 +000011#include "gl/GrGLEffect.h"
bsalomon@google.com92b6a942012-11-02 19:50:26 +000012#include "gl/GrGLEffectMatrix.h"
13#include "SkFloatingPoint.h"
tomhudson@google.com2f68e762012-07-17 18:43:21 +000014
bsalomon@google.com22a800a2012-10-26 19:16:46 +000015class GrGLTextureDomainEffect : public GrGLEffect {
tomhudson@google.com2f68e762012-07-17 18:43:21 +000016public:
bsalomon@google.com6340a412013-01-22 19:55:59 +000017 GrGLTextureDomainEffect(const GrBackendEffectFactory&, const GrEffectRef&);
tomhudson@google.com2f68e762012-07-17 18:43:21 +000018
bsalomon@google.com22a800a2012-10-26 19:16:46 +000019 virtual void emitCode(GrGLShaderBuilder*,
bsalomon@google.com2eaaefd2012-10-29 19:51:22 +000020 const GrEffectStage&,
bsalomon@google.com22a800a2012-10-26 19:16:46 +000021 EffectKey,
22 const char* vertexCoords,
23 const char* outputColor,
24 const char* inputColor,
25 const TextureSamplerArray&) SK_OVERRIDE;
tomhudson@google.com2f68e762012-07-17 18:43:21 +000026
bsalomon@google.com28a15fb2012-10-26 17:53:18 +000027 virtual void setData(const GrGLUniformManager&, const GrEffectStage&) SK_OVERRIDE;
tomhudson@google.com2f68e762012-07-17 18:43:21 +000028
bsalomon@google.com92b6a942012-11-02 19:50:26 +000029 static inline EffectKey GenKey(const GrEffectStage&, const GrGLCaps&);
tomhudson@google.com2f68e762012-07-17 18:43:21 +000030
31private:
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +000032 GrGLUniformManager::UniformHandle fNameUni;
bsalomon@google.com92b6a942012-11-02 19:50:26 +000033 GrGLEffectMatrix fEffectMatrix;
34 GrGLfloat fPrevDomain[4];
tomhudson@google.com2f68e762012-07-17 18:43:21 +000035
bsalomon@google.com22a800a2012-10-26 19:16:46 +000036 typedef GrGLEffect INHERITED;
tomhudson@google.com2f68e762012-07-17 18:43:21 +000037};
38
bsalomon@google.com396e61f2012-10-25 19:00:29 +000039GrGLTextureDomainEffect::GrGLTextureDomainEffect(const GrBackendEffectFactory& factory,
bsalomon@google.com6340a412013-01-22 19:55:59 +000040 const GrEffectRef&)
bsalomon@google.com374e7592012-10-23 17:30:45 +000041 : INHERITED(factory)
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +000042 , fNameUni(GrGLUniformManager::kInvalidUniformHandle) {
bsalomon@google.com92b6a942012-11-02 19:50:26 +000043 fPrevDomain[0] = SK_FloatNaN;
tomhudson@google.com2f68e762012-07-17 18:43:21 +000044}
45
bsalomon@google.com22a800a2012-10-26 19:16:46 +000046void GrGLTextureDomainEffect::emitCode(GrGLShaderBuilder* builder,
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +000047 const GrEffectStage& stage,
bsalomon@google.com92b6a942012-11-02 19:50:26 +000048 EffectKey key,
bsalomon@google.com22a800a2012-10-26 19:16:46 +000049 const char* vertexCoords,
50 const char* outputColor,
51 const char* inputColor,
52 const TextureSamplerArray& samplers) {
bsalomon@google.com6340a412013-01-22 19:55:59 +000053 const GrTextureDomainEffect& effect = GetEffectFromStage<GrTextureDomainEffect>(stage);
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +000054
bsalomon@google.com92b6a942012-11-02 19:50:26 +000055 const char* coords;
56 fEffectMatrix.emitCodeMakeFSCoords2D(builder, key, vertexCoords, &coords);
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +000057 const char* domain;
tomhudson@google.com57143a22012-07-17 19:07:35 +000058 fNameUni = builder->addUniform(GrGLShaderBuilder::kFragment_ShaderType,
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +000059 kVec4f_GrSLType, "TexDom", &domain);
60 if (GrTextureDomainEffect::kClamp_WrapMode == effect.wrapMode()) {
tomhudson@google.com2f68e762012-07-17 18:43:21 +000061
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +000062 builder->fFSCode.appendf("\tvec2 clampCoord = clamp(%s, %s.xy, %s.zw);\n",
63 coords, domain, domain);
tomhudson@google.com2f68e762012-07-17 18:43:21 +000064
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +000065 builder->fFSCode.appendf("\t%s = ", outputColor);
66 builder->appendTextureLookupAndModulate(&builder->fFSCode,
67 inputColor,
68 samplers[0],
69 "clampCoord");
70 builder->fFSCode.append(";\n");
71 } else {
72 GrAssert(GrTextureDomainEffect::kDecal_WrapMode == effect.wrapMode());
73 builder->fFSCode.append("\tbvec4 outside;\n");
74 builder->fFSCode.appendf("\toutside.xy = lessThan(%s, %s.xy);\n", coords, domain);
75 builder->fFSCode.appendf("\toutside.zw = greaterThan(%s, %s.zw);\n", coords, domain);
76 builder->fFSCode.appendf("\t%s = any(outside) ? vec4(0.0, 0.0, 0.0, 0.0) : ", outputColor);
77 builder->appendTextureLookupAndModulate(&builder->fFSCode, inputColor, samplers[0], coords);
78 builder->fFSCode.append(";\n");
79 }
tomhudson@google.com2f68e762012-07-17 18:43:21 +000080}
81
bsalomon@google.com28a15fb2012-10-26 17:53:18 +000082void GrGLTextureDomainEffect::setData(const GrGLUniformManager& uman, const GrEffectStage& stage) {
bsalomon@google.com6340a412013-01-22 19:55:59 +000083 const GrTextureDomainEffect& effect = GetEffectFromStage<GrTextureDomainEffect>(stage);
tomhudson@google.com2f68e762012-07-17 18:43:21 +000084 const GrRect& domain = effect.domain();
85
86 float values[4] = {
bsalomon@google.com81712882012-11-01 17:12:34 +000087 SkScalarToFloat(domain.left()),
88 SkScalarToFloat(domain.top()),
89 SkScalarToFloat(domain.right()),
90 SkScalarToFloat(domain.bottom())
tomhudson@google.com2f68e762012-07-17 18:43:21 +000091 };
92 // vertical flip if necessary
bsalomon@google.com2d0bade2012-10-26 19:01:17 +000093 if (GrSurface::kBottomLeft_Origin == effect.texture(0)->origin()) {
tomhudson@google.com2f68e762012-07-17 18:43:21 +000094 values[1] = 1.0f - values[1];
95 values[3] = 1.0f - values[3];
96 // The top and bottom were just flipped, so correct the ordering
97 // of elements so that values = (l, t, r, b).
98 SkTSwap(values[1], values[3]);
99 }
bsalomon@google.com92b6a942012-11-02 19:50:26 +0000100 if (0 != memcmp(values, fPrevDomain, 4 * sizeof(GrGLfloat))) {
101 uman.set4fv(fNameUni, 0, 1, values);
102 }
103 fEffectMatrix.setData(uman,
104 effect.getMatrix(),
105 stage.getCoordChangeMatrix(),
106 effect.texture(0));
skia.committer@gmail.com1aa90cf2012-11-06 13:18:25 +0000107}
bsalomon@google.com92b6a942012-11-02 19:50:26 +0000108
109GrGLEffect::EffectKey GrGLTextureDomainEffect::GenKey(const GrEffectStage& stage, const GrGLCaps&) {
bsalomon@google.com6340a412013-01-22 19:55:59 +0000110 const GrTextureDomainEffect& effect = GetEffectFromStage<GrTextureDomainEffect>(stage);
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000111 EffectKey key = effect.wrapMode();
112 key <<= GrGLEffectMatrix::kKeyBits;
113 EffectKey matrixKey = GrGLEffectMatrix::GenKey(effect.getMatrix(),
114 stage.getCoordChangeMatrix(),
115 effect.texture(0));
116 return key | matrixKey;
tomhudson@google.com2f68e762012-07-17 18:43:21 +0000117}
118
119
120///////////////////////////////////////////////////////////////////////////////
121
bsalomon@google.com0ac6af42013-01-16 15:16:18 +0000122GrEffectRef* GrTextureDomainEffect::Create(GrTexture* texture,
123 const SkMatrix& matrix,
124 const GrRect& domain,
125 WrapMode wrapMode,
126 bool bilerp) {
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000127 static const SkRect kFullRect = {0, 0, SK_Scalar1, SK_Scalar1};
128 if (kClamp_WrapMode == wrapMode && domain.contains(kFullRect)) {
bsalomon@google.com68b58c92013-01-17 16:50:08 +0000129 return GrSimpleTextureEffect::Create(texture, matrix, bilerp);
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000130 } else {
131 SkRect clippedDomain;
132 // We don't currently handle domains that are empty or don't intersect the texture.
bsalomon@google.comfe4e4912012-11-08 20:32:13 +0000133 // It is OK if the domain rect is a line or point, but it should not be inverted. We do not
134 // handle rects that do not intersect the [0..1]x[0..1] rect.
135 GrAssert(domain.fLeft <= domain.fRight);
136 GrAssert(domain.fTop <= domain.fBottom);
137 clippedDomain.fLeft = SkMaxScalar(domain.fLeft, kFullRect.fLeft);
138 clippedDomain.fRight = SkMinScalar(domain.fRight, kFullRect.fRight);
139 clippedDomain.fTop = SkMaxScalar(domain.fTop, kFullRect.fTop);
140 clippedDomain.fBottom = SkMinScalar(domain.fBottom, kFullRect.fBottom);
141 GrAssert(clippedDomain.fLeft <= clippedDomain.fRight);
142 GrAssert(clippedDomain.fTop <= clippedDomain.fBottom);
bsalomon@google.com0ac6af42013-01-16 15:16:18 +0000143
bsalomon@google.com6340a412013-01-22 19:55:59 +0000144 AutoEffectUnref effect(SkNEW_ARGS(GrTextureDomainEffect, (texture,
145 matrix,
146 clippedDomain,
147 wrapMode,
148 bilerp)));
bsalomon@google.coma1ebbe42013-01-16 15:51:47 +0000149 return CreateEffectRef(effect);
bsalomon@google.com0ac6af42013-01-16 15:16:18 +0000150
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000151 }
tomhudson@google.com2f68e762012-07-17 18:43:21 +0000152}
153
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +0000154GrTextureDomainEffect::GrTextureDomainEffect(GrTexture* texture,
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000155 const SkMatrix& matrix,
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +0000156 const GrRect& domain,
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000157 WrapMode wrapMode,
158 bool bilerp)
159 : GrSingleTextureEffect(texture, matrix, bilerp)
160 , fWrapMode(wrapMode)
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +0000161 , fTextureDomain(domain) {
162}
163
tomhudson@google.com2f68e762012-07-17 18:43:21 +0000164GrTextureDomainEffect::~GrTextureDomainEffect() {
165
166}
167
bsalomon@google.com396e61f2012-10-25 19:00:29 +0000168const GrBackendEffectFactory& GrTextureDomainEffect::getFactory() const {
169 return GrTBackendEffectFactory<GrTextureDomainEffect>::getInstance();
tomhudson@google.com2f68e762012-07-17 18:43:21 +0000170}
171
bsalomon@google.com6340a412013-01-22 19:55:59 +0000172bool GrTextureDomainEffect::onIsEqual(const GrEffectRef& sBase) const {
173 const GrTextureDomainEffect& s = CastEffect<GrTextureDomainEffect>(sBase);
bsalomon@google.com68b58c92013-01-17 16:50:08 +0000174 return this->hasSameTextureParamsAndMatrix(s) && this->fTextureDomain == s.fTextureDomain;
175}
176
177void GrTextureDomainEffect::getConstantColorComponents(GrColor* color, uint32_t* validFlags) const {
178 if (kDecal_WrapMode == fWrapMode) {
179 *validFlags = 0;
180 } else {
181 this->updateConstantColorComponentsForModulation(color, validFlags);
182 }
tomhudson@google.com2f68e762012-07-17 18:43:21 +0000183}
bsalomon@google.com0a7672f2012-08-03 18:12:20 +0000184
185///////////////////////////////////////////////////////////////////////////////
186
bsalomon@google.comf271cc72012-10-24 19:35:13 +0000187GR_DEFINE_EFFECT_TEST(GrTextureDomainEffect);
bsalomon@google.com0a7672f2012-08-03 18:12:20 +0000188
bsalomon@google.com0ac6af42013-01-16 15:16:18 +0000189GrEffectRef* GrTextureDomainEffect::TestCreate(SkRandom* random,
190 GrContext* context,
191 GrTexture* textures[]) {
bsalomon@google.com6f261be2012-10-24 19:07:10 +0000192 int texIdx = random->nextBool() ? GrEffectUnitTest::kSkiaPMTextureIdx :
193 GrEffectUnitTest::kAlphaTextureIdx;
bsalomon@google.com0a7672f2012-08-03 18:12:20 +0000194 GrRect domain;
195 domain.fLeft = random->nextUScalar1();
196 domain.fRight = random->nextRangeScalar(domain.fLeft, SK_Scalar1);
197 domain.fTop = random->nextUScalar1();
198 domain.fBottom = random->nextRangeScalar(domain.fTop, SK_Scalar1);
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000199 WrapMode wrapMode = random->nextBool() ? kClamp_WrapMode : kDecal_WrapMode;
200 const SkMatrix& matrix = GrEffectUnitTest::TestMatrix(random);
201 return GrTextureDomainEffect::Create(textures[texIdx], matrix, domain, wrapMode);
bsalomon@google.com0a7672f2012-08-03 18:12:20 +0000202}