blob: f82c62f3ff8707b3742f48f5f4adcf33cba9f620 [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.com2eaaefd2012-10-29 19:51:22 +00009#include "GrTBackendEffectFactory.h"
bsalomon@google.comd698f772012-10-25 13:22:00 +000010#include "gl/GrGLEffect.h"
bsalomon@google.com92b6a942012-11-02 19:50:26 +000011#include "gl/GrGLEffectMatrix.h"
12#include "SkFloatingPoint.h"
tomhudson@google.com2f68e762012-07-17 18:43:21 +000013
bsalomon@google.com22a800a2012-10-26 19:16:46 +000014class GrGLTextureDomainEffect : public GrGLEffect {
tomhudson@google.com2f68e762012-07-17 18:43:21 +000015public:
bsalomon@google.com396e61f2012-10-25 19:00:29 +000016 GrGLTextureDomainEffect(const GrBackendEffectFactory&, const GrEffect&);
tomhudson@google.com2f68e762012-07-17 18:43:21 +000017
bsalomon@google.com22a800a2012-10-26 19:16:46 +000018 virtual void emitCode(GrGLShaderBuilder*,
bsalomon@google.com2eaaefd2012-10-29 19:51:22 +000019 const GrEffectStage&,
bsalomon@google.com22a800a2012-10-26 19:16:46 +000020 EffectKey,
21 const char* vertexCoords,
22 const char* outputColor,
23 const char* inputColor,
24 const TextureSamplerArray&) SK_OVERRIDE;
tomhudson@google.com2f68e762012-07-17 18:43:21 +000025
bsalomon@google.com28a15fb2012-10-26 17:53:18 +000026 virtual void setData(const GrGLUniformManager&, const GrEffectStage&) SK_OVERRIDE;
tomhudson@google.com2f68e762012-07-17 18:43:21 +000027
bsalomon@google.com92b6a942012-11-02 19:50:26 +000028 static inline EffectKey GenKey(const GrEffectStage&, const GrGLCaps&);
tomhudson@google.com2f68e762012-07-17 18:43:21 +000029
30private:
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +000031 GrGLUniformManager::UniformHandle fNameUni;
bsalomon@google.com92b6a942012-11-02 19:50:26 +000032 GrGLEffectMatrix fEffectMatrix;
33 GrGLfloat fPrevDomain[4];
tomhudson@google.com2f68e762012-07-17 18:43:21 +000034
bsalomon@google.com22a800a2012-10-26 19:16:46 +000035 typedef GrGLEffect INHERITED;
tomhudson@google.com2f68e762012-07-17 18:43:21 +000036};
37
bsalomon@google.com396e61f2012-10-25 19:00:29 +000038GrGLTextureDomainEffect::GrGLTextureDomainEffect(const GrBackendEffectFactory& factory,
bsalomon@google.com021fc732012-10-25 12:47:42 +000039 const GrEffect&)
bsalomon@google.com374e7592012-10-23 17:30:45 +000040 : INHERITED(factory)
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +000041 , fNameUni(GrGLUniformManager::kInvalidUniformHandle) {
bsalomon@google.com92b6a942012-11-02 19:50:26 +000042 fPrevDomain[0] = SK_FloatNaN;
tomhudson@google.com2f68e762012-07-17 18:43:21 +000043}
44
bsalomon@google.com22a800a2012-10-26 19:16:46 +000045void GrGLTextureDomainEffect::emitCode(GrGLShaderBuilder* builder,
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +000046 const GrEffectStage& stage,
bsalomon@google.com92b6a942012-11-02 19:50:26 +000047 EffectKey key,
bsalomon@google.com22a800a2012-10-26 19:16:46 +000048 const char* vertexCoords,
49 const char* outputColor,
50 const char* inputColor,
51 const TextureSamplerArray& samplers) {
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +000052 const GrTextureDomainEffect& effect =
53 static_cast<const GrTextureDomainEffect&>(*stage.getEffect());
54
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) {
83 const GrTextureDomainEffect& effect =
84 static_cast<const GrTextureDomainEffect&>(*stage.getEffect());
tomhudson@google.com2f68e762012-07-17 18:43:21 +000085 const GrRect& domain = effect.domain();
86
87 float values[4] = {
bsalomon@google.com81712882012-11-01 17:12:34 +000088 SkScalarToFloat(domain.left()),
89 SkScalarToFloat(domain.top()),
90 SkScalarToFloat(domain.right()),
91 SkScalarToFloat(domain.bottom())
tomhudson@google.com2f68e762012-07-17 18:43:21 +000092 };
93 // vertical flip if necessary
bsalomon@google.com2d0bade2012-10-26 19:01:17 +000094 if (GrSurface::kBottomLeft_Origin == effect.texture(0)->origin()) {
tomhudson@google.com2f68e762012-07-17 18:43:21 +000095 values[1] = 1.0f - values[1];
96 values[3] = 1.0f - values[3];
97 // The top and bottom were just flipped, so correct the ordering
98 // of elements so that values = (l, t, r, b).
99 SkTSwap(values[1], values[3]);
100 }
bsalomon@google.com92b6a942012-11-02 19:50:26 +0000101 if (0 != memcmp(values, fPrevDomain, 4 * sizeof(GrGLfloat))) {
102 uman.set4fv(fNameUni, 0, 1, values);
103 }
104 fEffectMatrix.setData(uman,
105 effect.getMatrix(),
106 stage.getCoordChangeMatrix(),
107 effect.texture(0));
skia.committer@gmail.com1aa90cf2012-11-06 13:18:25 +0000108}
bsalomon@google.com92b6a942012-11-02 19:50:26 +0000109
110GrGLEffect::EffectKey GrGLTextureDomainEffect::GenKey(const GrEffectStage& stage, const GrGLCaps&) {
111 const GrTextureDomainEffect& effect =
112 static_cast<const GrTextureDomainEffect&>(*stage.getEffect());
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000113 EffectKey key = effect.wrapMode();
114 key <<= GrGLEffectMatrix::kKeyBits;
115 EffectKey matrixKey = GrGLEffectMatrix::GenKey(effect.getMatrix(),
116 stage.getCoordChangeMatrix(),
117 effect.texture(0));
118 return key | matrixKey;
tomhudson@google.com2f68e762012-07-17 18:43:21 +0000119}
120
121
122///////////////////////////////////////////////////////////////////////////////
123
bsalomon@google.com0ac6af42013-01-16 15:16:18 +0000124GrEffectRef* GrTextureDomainEffect::Create(GrTexture* texture,
125 const SkMatrix& matrix,
126 const GrRect& domain,
127 WrapMode wrapMode,
128 bool bilerp) {
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000129 static const SkRect kFullRect = {0, 0, SK_Scalar1, SK_Scalar1};
130 if (kClamp_WrapMode == wrapMode && domain.contains(kFullRect)) {
bsalomon@google.com0ac6af42013-01-16 15:16:18 +0000131 return GrSingleTextureEffect::Create(texture, matrix, bilerp);
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000132 } else {
133 SkRect clippedDomain;
134 // We don't currently handle domains that are empty or don't intersect the texture.
bsalomon@google.comfe4e4912012-11-08 20:32:13 +0000135 // It is OK if the domain rect is a line or point, but it should not be inverted. We do not
136 // handle rects that do not intersect the [0..1]x[0..1] rect.
137 GrAssert(domain.fLeft <= domain.fRight);
138 GrAssert(domain.fTop <= domain.fBottom);
139 clippedDomain.fLeft = SkMaxScalar(domain.fLeft, kFullRect.fLeft);
140 clippedDomain.fRight = SkMinScalar(domain.fRight, kFullRect.fRight);
141 clippedDomain.fTop = SkMaxScalar(domain.fTop, kFullRect.fTop);
142 clippedDomain.fBottom = SkMinScalar(domain.fBottom, kFullRect.fBottom);
143 GrAssert(clippedDomain.fLeft <= clippedDomain.fRight);
144 GrAssert(clippedDomain.fTop <= clippedDomain.fBottom);
bsalomon@google.com0ac6af42013-01-16 15:16:18 +0000145
146 SkAutoTUnref<GrEffect> effect(SkNEW_ARGS(GrTextureDomainEffect, (texture,
147 matrix,
148 clippedDomain,
149 wrapMode,
150 bilerp)));
bsalomon@google.coma1ebbe42013-01-16 15:51:47 +0000151 return CreateEffectRef(effect);
bsalomon@google.com0ac6af42013-01-16 15:16:18 +0000152
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000153 }
tomhudson@google.com2f68e762012-07-17 18:43:21 +0000154}
155
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +0000156GrTextureDomainEffect::GrTextureDomainEffect(GrTexture* texture,
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000157 const SkMatrix& matrix,
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +0000158 const GrRect& domain,
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000159 WrapMode wrapMode,
160 bool bilerp)
161 : GrSingleTextureEffect(texture, matrix, bilerp)
162 , fWrapMode(wrapMode)
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +0000163 , fTextureDomain(domain) {
164}
165
tomhudson@google.com2f68e762012-07-17 18:43:21 +0000166GrTextureDomainEffect::~GrTextureDomainEffect() {
167
168}
169
bsalomon@google.com396e61f2012-10-25 19:00:29 +0000170const GrBackendEffectFactory& GrTextureDomainEffect::getFactory() const {
171 return GrTBackendEffectFactory<GrTextureDomainEffect>::getInstance();
tomhudson@google.com2f68e762012-07-17 18:43:21 +0000172}
173
bsalomon@google.coma469c282012-10-24 18:28:34 +0000174bool GrTextureDomainEffect::isEqual(const GrEffect& sBase) const {
tomhudson@google.com2f68e762012-07-17 18:43:21 +0000175 const GrTextureDomainEffect& s = static_cast<const GrTextureDomainEffect&>(sBase);
176 return (INHERITED::isEqual(sBase) && this->fTextureDomain == s.fTextureDomain);
177}
bsalomon@google.com0a7672f2012-08-03 18:12:20 +0000178
179///////////////////////////////////////////////////////////////////////////////
180
bsalomon@google.comf271cc72012-10-24 19:35:13 +0000181GR_DEFINE_EFFECT_TEST(GrTextureDomainEffect);
bsalomon@google.com0a7672f2012-08-03 18:12:20 +0000182
bsalomon@google.com0ac6af42013-01-16 15:16:18 +0000183GrEffectRef* GrTextureDomainEffect::TestCreate(SkRandom* random,
184 GrContext* context,
185 GrTexture* textures[]) {
bsalomon@google.com6f261be2012-10-24 19:07:10 +0000186 int texIdx = random->nextBool() ? GrEffectUnitTest::kSkiaPMTextureIdx :
187 GrEffectUnitTest::kAlphaTextureIdx;
bsalomon@google.com0a7672f2012-08-03 18:12:20 +0000188 GrRect domain;
189 domain.fLeft = random->nextUScalar1();
190 domain.fRight = random->nextRangeScalar(domain.fLeft, SK_Scalar1);
191 domain.fTop = random->nextUScalar1();
192 domain.fBottom = random->nextRangeScalar(domain.fTop, SK_Scalar1);
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000193 WrapMode wrapMode = random->nextBool() ? kClamp_WrapMode : kDecal_WrapMode;
194 const SkMatrix& matrix = GrEffectUnitTest::TestMatrix(random);
195 return GrTextureDomainEffect::Create(textures[texIdx], matrix, domain, wrapMode);
bsalomon@google.com0a7672f2012-08-03 18:12:20 +0000196}