| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2012 Google Inc. | 2 * Copyright 2012 Google Inc. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
| 5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
| 6 */ | 6 */ |
| 7 | 7 |
| 8 #include "GrTextureDomainEffect.h" | 8 #include "GrTextureDomainEffect.h" |
| 9 #include "GrSimpleTextureEffect.h" | 9 #include "GrSimpleTextureEffect.h" |
| 10 #include "GrTBackendEffectFactory.h" | 10 #include "GrTBackendEffectFactory.h" |
| 11 #include "gl/GrGLEffect.h" | 11 #include "gl/GrGLEffect.h" |
| 12 #include "gl/GrGLEffectMatrix.h" | 12 #include "gl/GrGLEffectMatrix.h" |
| 13 #include "SkFloatingPoint.h" | 13 #include "SkFloatingPoint.h" |
| 14 | 14 |
| 15 class GrGLTextureDomainEffect : public GrGLEffect { | 15 class GrGLTextureDomainEffect : public GrGLEffect { |
| 16 public: | 16 public: |
| 17 GrGLTextureDomainEffect(const GrBackendEffectFactory&, const GrEffectRef&); | 17 GrGLTextureDomainEffect(const GrBackendEffectFactory&, const GrDrawEffect&); |
| 18 | 18 |
| 19 virtual void emitCode(GrGLShaderBuilder*, | 19 virtual void emitCode(GrGLShaderBuilder*, |
| 20 const GrEffectStage&, | 20 const GrDrawEffect&, |
| 21 EffectKey, | 21 EffectKey, |
| 22 const char* vertexCoords, | |
| 23 const char* outputColor, | 22 const char* outputColor, |
| 24 const char* inputColor, | 23 const char* inputColor, |
| 25 const TextureSamplerArray&) SK_OVERRIDE; | 24 const TextureSamplerArray&) SK_OVERRIDE; |
| 26 | 25 |
| 27 virtual void setData(const GrGLUniformManager&, const GrEffectStage&) SK_OVE
RRIDE; | 26 virtual void setData(const GrGLUniformManager&, const GrDrawEffect&) SK_OVER
RIDE; |
| 28 | 27 |
| 29 static inline EffectKey GenKey(const GrEffectStage&, const GrGLCaps&); | 28 static inline EffectKey GenKey(const GrDrawEffect&, const GrGLCaps&); |
| 30 | 29 |
| 31 private: | 30 private: |
| 32 GrGLUniformManager::UniformHandle fNameUni; | 31 GrGLUniformManager::UniformHandle fNameUni; |
| 33 GrGLEffectMatrix fEffectMatrix; | 32 GrGLEffectMatrix fEffectMatrix; |
| 34 GrGLfloat fPrevDomain[4]; | 33 GrGLfloat fPrevDomain[4]; |
| 35 | 34 |
| 36 typedef GrGLEffect INHERITED; | 35 typedef GrGLEffect INHERITED; |
| 37 }; | 36 }; |
| 38 | 37 |
| 39 GrGLTextureDomainEffect::GrGLTextureDomainEffect(const GrBackendEffectFactory& f
actory, | 38 GrGLTextureDomainEffect::GrGLTextureDomainEffect(const GrBackendEffectFactory& f
actory, |
| 40 const GrEffectRef&) | 39 const GrDrawEffect& drawEffect) |
| 41 : INHERITED(factory) | 40 : INHERITED(factory) |
| 42 , fNameUni(GrGLUniformManager::kInvalidUniformHandle) { | 41 , fNameUni(GrGLUniformManager::kInvalidUniformHandle) |
| 42 , fEffectMatrix(drawEffect.castEffect<GrTextureDomainEffect>().coordsType())
{ |
| 43 fPrevDomain[0] = SK_FloatNaN; | 43 fPrevDomain[0] = SK_FloatNaN; |
| 44 } | 44 } |
| 45 | 45 |
| 46 void GrGLTextureDomainEffect::emitCode(GrGLShaderBuilder* builder, | 46 void GrGLTextureDomainEffect::emitCode(GrGLShaderBuilder* builder, |
| 47 const GrEffectStage& stage, | 47 const GrDrawEffect& drawEffect, |
| 48 EffectKey key, | 48 EffectKey key, |
| 49 const char* vertexCoords, | |
| 50 const char* outputColor, | 49 const char* outputColor, |
| 51 const char* inputColor, | 50 const char* inputColor, |
| 52 const TextureSamplerArray& samplers) { | 51 const TextureSamplerArray& samplers) { |
| 53 const GrTextureDomainEffect& effect = GetEffectFromStage<GrTextureDomainEffe
ct>(stage); | 52 const GrTextureDomainEffect& texDom = drawEffect.castEffect<GrTextureDomainE
ffect>(); |
| 54 | 53 |
| 55 const char* coords; | 54 const char* coords; |
| 56 fEffectMatrix.emitCodeMakeFSCoords2D(builder, key, vertexCoords, &coords); | 55 fEffectMatrix.emitCodeMakeFSCoords2D(builder, key, &coords); |
| 57 const char* domain; | 56 const char* domain; |
| 58 fNameUni = builder->addUniform(GrGLShaderBuilder::kFragment_ShaderType, | 57 fNameUni = builder->addUniform(GrGLShaderBuilder::kFragment_ShaderType, |
| 59 kVec4f_GrSLType, "TexDom", &domain); | 58 kVec4f_GrSLType, "TexDom", &domain); |
| 60 if (GrTextureDomainEffect::kClamp_WrapMode == effect.wrapMode()) { | 59 if (GrTextureDomainEffect::kClamp_WrapMode == texDom.wrapMode()) { |
| 61 | 60 |
| 62 builder->fsCodeAppendf("\tvec2 clampCoord = clamp(%s, %s.xy, %s.zw);\n", | 61 builder->fsCodeAppendf("\tvec2 clampCoord = clamp(%s, %s.xy, %s.zw);\n", |
| 63 coords, domain, domain); | 62 coords, domain, domain); |
| 64 | 63 |
| 65 builder->fsCodeAppendf("\t%s = ", outputColor); | 64 builder->fsCodeAppendf("\t%s = ", outputColor); |
| 66 builder->appendTextureLookupAndModulate(GrGLShaderBuilder::kFragment_Sha
derType, | 65 builder->appendTextureLookupAndModulate(GrGLShaderBuilder::kFragment_Sha
derType, |
| 67 inputColor, | 66 inputColor, |
| 68 samplers[0], | 67 samplers[0], |
| 69 "clampCoord"); | 68 "clampCoord"); |
| 70 builder->fsCodeAppend(";\n"); | 69 builder->fsCodeAppend(";\n"); |
| 71 } else { | 70 } else { |
| 72 GrAssert(GrTextureDomainEffect::kDecal_WrapMode == effect.wrapMode()); | 71 GrAssert(GrTextureDomainEffect::kDecal_WrapMode == texDom.wrapMode()); |
| 73 | 72 |
| 74 if (kImagination_GrGLVendor == builder->ctxInfo().vendor()) { | 73 if (kImagination_GrGLVendor == builder->ctxInfo().vendor()) { |
| 75 // On the NexusS and GalaxyNexus, the other path (with the 'any' | 74 // On the NexusS and GalaxyNexus, the other path (with the 'any' |
| 76 // call) causes the compilation error "Calls to any function that | 75 // call) causes the compilation error "Calls to any function that |
| 77 // may require a gradient calculation inside a conditional block | 76 // may require a gradient calculation inside a conditional block |
| 78 // may return undefined results". This appears to be an issue with | 77 // may return undefined results". This appears to be an issue with |
| 79 // the 'any' call since even the simple "result=black; if (any()) | 78 // the 'any' call since even the simple "result=black; if (any()) |
| 80 // result=white;" code fails to compile. | 79 // result=white;" code fails to compile. |
| 81 builder->fsCodeAppend("\tvec4 outside = vec4(0.0, 0.0, 0.0, 0.0);\n"
); | 80 builder->fsCodeAppend("\tvec4 outside = vec4(0.0, 0.0, 0.0, 0.0);\n"
); |
| 82 builder->fsCodeAppend("\tvec4 inside = "); | 81 builder->fsCodeAppend("\tvec4 inside = "); |
| (...skipping 16 matching lines...) Expand all Loading... |
| 99 builder->fsCodeAppendf("\t%s = any(outside) ? vec4(0.0, 0.0, 0.0, 0.
0) : ", outputColor); | 98 builder->fsCodeAppendf("\t%s = any(outside) ? vec4(0.0, 0.0, 0.0, 0.
0) : ", outputColor); |
| 100 builder->appendTextureLookupAndModulate(GrGLShaderBuilder::kFragment
_ShaderType, | 99 builder->appendTextureLookupAndModulate(GrGLShaderBuilder::kFragment
_ShaderType, |
| 101 inputColor, | 100 inputColor, |
| 102 samplers[0], | 101 samplers[0], |
| 103 coords); | 102 coords); |
| 104 builder->fsCodeAppend(";\n"); | 103 builder->fsCodeAppend(";\n"); |
| 105 } | 104 } |
| 106 } | 105 } |
| 107 } | 106 } |
| 108 | 107 |
| 109 void GrGLTextureDomainEffect::setData(const GrGLUniformManager& uman, const GrEf
fectStage& stage) { | 108 void GrGLTextureDomainEffect::setData(const GrGLUniformManager& uman, |
| 110 const GrTextureDomainEffect& effect = GetEffectFromStage<GrTextureDomainEffe
ct>(stage); | 109 const GrDrawEffect& drawEffect) { |
| 111 const GrRect& domain = effect.domain(); | 110 const GrTextureDomainEffect& texDom = drawEffect.castEffect<GrTextureDomainE
ffect>(); |
| 111 const GrRect& domain = texDom.domain(); |
| 112 | 112 |
| 113 float values[4] = { | 113 float values[4] = { |
| 114 SkScalarToFloat(domain.left()), | 114 SkScalarToFloat(domain.left()), |
| 115 SkScalarToFloat(domain.top()), | 115 SkScalarToFloat(domain.top()), |
| 116 SkScalarToFloat(domain.right()), | 116 SkScalarToFloat(domain.right()), |
| 117 SkScalarToFloat(domain.bottom()) | 117 SkScalarToFloat(domain.bottom()) |
| 118 }; | 118 }; |
| 119 // vertical flip if necessary | 119 // vertical flip if necessary |
| 120 if (kBottomLeft_GrSurfaceOrigin == effect.texture(0)->origin()) { | 120 if (kBottomLeft_GrSurfaceOrigin == texDom.texture(0)->origin()) { |
| 121 values[1] = 1.0f - values[1]; | 121 values[1] = 1.0f - values[1]; |
| 122 values[3] = 1.0f - values[3]; | 122 values[3] = 1.0f - values[3]; |
| 123 // The top and bottom were just flipped, so correct the ordering | 123 // The top and bottom were just flipped, so correct the ordering |
| 124 // of elements so that values = (l, t, r, b). | 124 // of elements so that values = (l, t, r, b). |
| 125 SkTSwap(values[1], values[3]); | 125 SkTSwap(values[1], values[3]); |
| 126 } | 126 } |
| 127 if (0 != memcmp(values, fPrevDomain, 4 * sizeof(GrGLfloat))) { | 127 if (0 != memcmp(values, fPrevDomain, 4 * sizeof(GrGLfloat))) { |
| 128 uman.set4fv(fNameUni, 0, 1, values); | 128 uman.set4fv(fNameUni, 0, 1, values); |
| 129 } | 129 } |
| 130 fEffectMatrix.setData(uman, | 130 fEffectMatrix.setData(uman, |
| 131 effect.getMatrix(), | 131 texDom.getMatrix(), |
| 132 stage.getCoordChangeMatrix(), | 132 drawEffect, |
| 133 effect.texture(0)); | 133 texDom.texture(0)); |
| 134 } | 134 } |
| 135 | 135 |
| 136 GrGLEffect::EffectKey GrGLTextureDomainEffect::GenKey(const GrEffectStage& stage
, const GrGLCaps&) { | 136 GrGLEffect::EffectKey GrGLTextureDomainEffect::GenKey(const GrDrawEffect& drawEf
fect, |
| 137 const GrTextureDomainEffect& effect = GetEffectFromStage<GrTextureDomainEffe
ct>(stage); | 137 const GrGLCaps&) { |
| 138 EffectKey key = effect.wrapMode(); | 138 const GrTextureDomainEffect& texDom = drawEffect.castEffect<GrTextureDomainE
ffect>(); |
| 139 EffectKey key = texDom.wrapMode(); |
| 139 key <<= GrGLEffectMatrix::kKeyBits; | 140 key <<= GrGLEffectMatrix::kKeyBits; |
| 140 EffectKey matrixKey = GrGLEffectMatrix::GenKey(effect.getMatrix(), | 141 EffectKey matrixKey = GrGLEffectMatrix::GenKey(texDom.getMatrix(), |
| 141 stage.getCoordChangeMatrix(), | 142 drawEffect, |
| 142 effect.texture(0)); | 143 texDom.coordsType(), |
| 144 texDom.texture(0)); |
| 143 return key | matrixKey; | 145 return key | matrixKey; |
| 144 } | 146 } |
| 145 | 147 |
| 146 | 148 |
| 147 /////////////////////////////////////////////////////////////////////////////// | 149 /////////////////////////////////////////////////////////////////////////////// |
| 148 | 150 |
| 149 GrEffectRef* GrTextureDomainEffect::Create(GrTexture* texture, | 151 GrEffectRef* GrTextureDomainEffect::Create(GrTexture* texture, |
| 150 const SkMatrix& matrix, | 152 const SkMatrix& matrix, |
| 151 const GrRect& domain, | 153 const GrRect& domain, |
| 152 WrapMode wrapMode, | 154 WrapMode wrapMode, |
| 153 bool bilerp) { | 155 bool bilerp, |
| 156 CoordsType coordsType) { |
| 154 static const SkRect kFullRect = {0, 0, SK_Scalar1, SK_Scalar1}; | 157 static const SkRect kFullRect = {0, 0, SK_Scalar1, SK_Scalar1}; |
| 155 if (kClamp_WrapMode == wrapMode && domain.contains(kFullRect)) { | 158 if (kClamp_WrapMode == wrapMode && domain.contains(kFullRect)) { |
| 156 return GrSimpleTextureEffect::Create(texture, matrix, bilerp); | 159 return GrSimpleTextureEffect::Create(texture, matrix, bilerp); |
| 157 } else { | 160 } else { |
| 158 SkRect clippedDomain; | 161 SkRect clippedDomain; |
| 159 // We don't currently handle domains that are empty or don't intersect t
he texture. | 162 // We don't currently handle domains that are empty or don't intersect t
he texture. |
| 160 // It is OK if the domain rect is a line or point, but it should not be
inverted. We do not | 163 // It is OK if the domain rect is a line or point, but it should not be
inverted. We do not |
| 161 // handle rects that do not intersect the [0..1]x[0..1] rect. | 164 // handle rects that do not intersect the [0..1]x[0..1] rect. |
| 162 GrAssert(domain.fLeft <= domain.fRight); | 165 GrAssert(domain.fLeft <= domain.fRight); |
| 163 GrAssert(domain.fTop <= domain.fBottom); | 166 GrAssert(domain.fTop <= domain.fBottom); |
| 164 clippedDomain.fLeft = SkMaxScalar(domain.fLeft, kFullRect.fLeft); | 167 clippedDomain.fLeft = SkMaxScalar(domain.fLeft, kFullRect.fLeft); |
| 165 clippedDomain.fRight = SkMinScalar(domain.fRight, kFullRect.fRight); | 168 clippedDomain.fRight = SkMinScalar(domain.fRight, kFullRect.fRight); |
| 166 clippedDomain.fTop = SkMaxScalar(domain.fTop, kFullRect.fTop); | 169 clippedDomain.fTop = SkMaxScalar(domain.fTop, kFullRect.fTop); |
| 167 clippedDomain.fBottom = SkMinScalar(domain.fBottom, kFullRect.fBottom); | 170 clippedDomain.fBottom = SkMinScalar(domain.fBottom, kFullRect.fBottom); |
| 168 GrAssert(clippedDomain.fLeft <= clippedDomain.fRight); | 171 GrAssert(clippedDomain.fLeft <= clippedDomain.fRight); |
| 169 GrAssert(clippedDomain.fTop <= clippedDomain.fBottom); | 172 GrAssert(clippedDomain.fTop <= clippedDomain.fBottom); |
| 170 | 173 |
| 171 AutoEffectUnref effect(SkNEW_ARGS(GrTextureDomainEffect, (texture, | 174 AutoEffectUnref effect(SkNEW_ARGS(GrTextureDomainEffect, (texture, |
| 172 matrix, | 175 matrix, |
| 173 clippedDomain, | 176 clippedDomain, |
| 174 wrapMode, | 177 wrapMode, |
| 175 bilerp))); | 178 bilerp, |
| 179 coordsType))); |
| 176 return CreateEffectRef(effect); | 180 return CreateEffectRef(effect); |
| 177 | 181 |
| 178 } | 182 } |
| 179 } | 183 } |
| 180 | 184 |
| 181 GrTextureDomainEffect::GrTextureDomainEffect(GrTexture* texture, | 185 GrTextureDomainEffect::GrTextureDomainEffect(GrTexture* texture, |
| 182 const SkMatrix& matrix, | 186 const SkMatrix& matrix, |
| 183 const GrRect& domain, | 187 const GrRect& domain, |
| 184 WrapMode wrapMode, | 188 WrapMode wrapMode, |
| 185 bool bilerp) | 189 bool bilerp, |
| 186 : GrSingleTextureEffect(texture, matrix, bilerp) | 190 CoordsType coordsType) |
| 191 : GrSingleTextureEffect(texture, matrix, bilerp, coordsType) |
| 187 , fWrapMode(wrapMode) | 192 , fWrapMode(wrapMode) |
| 188 , fTextureDomain(domain) { | 193 , fTextureDomain(domain) { |
| 189 } | 194 } |
| 190 | 195 |
| 191 GrTextureDomainEffect::~GrTextureDomainEffect() { | 196 GrTextureDomainEffect::~GrTextureDomainEffect() { |
| 192 | 197 |
| 193 } | 198 } |
| 194 | 199 |
| 195 const GrBackendEffectFactory& GrTextureDomainEffect::getFactory() const { | 200 const GrBackendEffectFactory& GrTextureDomainEffect::getFactory() const { |
| 196 return GrTBackendEffectFactory<GrTextureDomainEffect>::getInstance(); | 201 return GrTBackendEffectFactory<GrTextureDomainEffect>::getInstance(); |
| 197 } | 202 } |
| 198 | 203 |
| 199 bool GrTextureDomainEffect::onIsEqual(const GrEffect& sBase) const { | 204 bool GrTextureDomainEffect::onIsEqual(const GrEffect& sBase) const { |
| 200 const GrTextureDomainEffect& s = CastEffect<GrTextureDomainEffect>(sBase); | 205 const GrTextureDomainEffect& s = CastEffect<GrTextureDomainEffect>(sBase); |
| 201 return this->hasSameTextureParamsAndMatrix(s) && this->fTextureDomain == s.f
TextureDomain; | 206 return this->hasSameTextureParamsMatrixAndCoordsType(s) && |
| 207 this->fTextureDomain == s.fTextureDomain; |
| 202 } | 208 } |
| 203 | 209 |
| 204 void GrTextureDomainEffect::getConstantColorComponents(GrColor* color, uint32_t*
validFlags) const { | 210 void GrTextureDomainEffect::getConstantColorComponents(GrColor* color, uint32_t*
validFlags) const { |
| 205 if (kDecal_WrapMode == fWrapMode) { | 211 if (kDecal_WrapMode == fWrapMode) { |
| 206 *validFlags = 0; | 212 *validFlags = 0; |
| 207 } else { | 213 } else { |
| 208 this->updateConstantColorComponentsForModulation(color, validFlags); | 214 this->updateConstantColorComponentsForModulation(color, validFlags); |
| 209 } | 215 } |
| 210 } | 216 } |
| 211 | 217 |
| 212 /////////////////////////////////////////////////////////////////////////////// | 218 /////////////////////////////////////////////////////////////////////////////// |
| 213 | 219 |
| 214 GR_DEFINE_EFFECT_TEST(GrTextureDomainEffect); | 220 GR_DEFINE_EFFECT_TEST(GrTextureDomainEffect); |
| 215 | 221 |
| 216 GrEffectRef* GrTextureDomainEffect::TestCreate(SkMWCRandom* random, | 222 GrEffectRef* GrTextureDomainEffect::TestCreate(SkMWCRandom* random, |
| 217 GrContext*, | 223 GrContext*, |
| 218 GrTexture* textures[]) { | 224 GrTexture* textures[]) { |
| 219 int texIdx = random->nextBool() ? GrEffectUnitTest::kSkiaPMTextureIdx : | 225 int texIdx = random->nextBool() ? GrEffectUnitTest::kSkiaPMTextureIdx : |
| 220 GrEffectUnitTest::kAlphaTextureIdx; | 226 GrEffectUnitTest::kAlphaTextureIdx; |
| 221 GrRect domain; | 227 GrRect domain; |
| 222 domain.fLeft = random->nextUScalar1(); | 228 domain.fLeft = random->nextUScalar1(); |
| 223 domain.fRight = random->nextRangeScalar(domain.fLeft, SK_Scalar1); | 229 domain.fRight = random->nextRangeScalar(domain.fLeft, SK_Scalar1); |
| 224 domain.fTop = random->nextUScalar1(); | 230 domain.fTop = random->nextUScalar1(); |
| 225 domain.fBottom = random->nextRangeScalar(domain.fTop, SK_Scalar1); | 231 domain.fBottom = random->nextRangeScalar(domain.fTop, SK_Scalar1); |
| 226 WrapMode wrapMode = random->nextBool() ? kClamp_WrapMode : kDecal_WrapMode; | 232 WrapMode wrapMode = random->nextBool() ? kClamp_WrapMode : kDecal_WrapMode; |
| 227 const SkMatrix& matrix = GrEffectUnitTest::TestMatrix(random); | 233 const SkMatrix& matrix = GrEffectUnitTest::TestMatrix(random); |
| 228 return GrTextureDomainEffect::Create(textures[texIdx], matrix, domain, wrapM
ode); | 234 bool bilerp = random->nextBool(); |
| 235 CoordsType coords = random->nextBool() ? kLocal_CoordsType : kPosition_Coord
sType; |
| 236 return GrTextureDomainEffect::Create(textures[texIdx], |
| 237 matrix, |
| 238 domain, |
| 239 wrapMode, |
| 240 bilerp, |
| 241 coords); |
| 229 } | 242 } |
| OLD | NEW |