| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2012 The Android Open Source Project | 2 * Copyright 2012 The Android Open Source Project |
| 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 "SkMorphologyImageFilter.h" | 8 #include "SkMorphologyImageFilter.h" |
| 9 #include "SkBitmap.h" | 9 #include "SkBitmap.h" |
| 10 #include "SkColorPriv.h" | 10 #include "SkColorPriv.h" |
| 11 #include "SkFlattenableBuffers.h" | 11 #include "SkFlattenableBuffers.h" |
| 12 #include "SkRect.h" | 12 #include "SkRect.h" |
| 13 #if SK_SUPPORT_GPU | 13 #if SK_SUPPORT_GPU |
| 14 #include "GrContext.h" | 14 #include "GrContext.h" |
| 15 #include "GrTexture.h" | 15 #include "GrTexture.h" |
| 16 #include "GrTBackendEffectFactory.h" | 16 #include "GrTBackendEffectFactory.h" |
| 17 #include "gl/GrGLEffect.h" | 17 #include "gl/GrGLEffect.h" |
| 18 #include "gl/GrGLEffectMatrix.h" | |
| 19 #include "effects/Gr1DKernelEffect.h" | 18 #include "effects/Gr1DKernelEffect.h" |
| 20 #include "SkImageFilterUtils.h" | 19 #include "SkImageFilterUtils.h" |
| 21 #endif | 20 #endif |
| 22 | 21 |
| 23 SkMorphologyImageFilter::SkMorphologyImageFilter(SkFlattenableReadBuffer& buffer
) | 22 SkMorphologyImageFilter::SkMorphologyImageFilter(SkFlattenableReadBuffer& buffer
) |
| 24 : INHERITED(buffer) { | 23 : INHERITED(buffer) { |
| 25 fRadius.fWidth = buffer.readInt(); | 24 fRadius.fWidth = buffer.readInt(); |
| 26 fRadius.fHeight = buffer.readInt(); | 25 fRadius.fHeight = buffer.readInt(); |
| 27 } | 26 } |
| 28 | 27 |
| (...skipping 278 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 307 | 306 |
| 308 class GrGLMorphologyEffect : public GrGLEffect { | 307 class GrGLMorphologyEffect : public GrGLEffect { |
| 309 public: | 308 public: |
| 310 GrGLMorphologyEffect (const GrBackendEffectFactory&, const GrDrawEffect&); | 309 GrGLMorphologyEffect (const GrBackendEffectFactory&, const GrDrawEffect&); |
| 311 | 310 |
| 312 virtual void emitCode(GrGLShaderBuilder*, | 311 virtual void emitCode(GrGLShaderBuilder*, |
| 313 const GrDrawEffect&, | 312 const GrDrawEffect&, |
| 314 EffectKey, | 313 EffectKey, |
| 315 const char* outputColor, | 314 const char* outputColor, |
| 316 const char* inputColor, | 315 const char* inputColor, |
| 316 const TransformedCoordsArray&, |
| 317 const TextureSamplerArray&) SK_OVERRIDE; | 317 const TextureSamplerArray&) SK_OVERRIDE; |
| 318 | 318 |
| 319 static inline EffectKey GenKey(const GrDrawEffect&, const GrGLCaps&); | 319 static inline EffectKey GenKey(const GrDrawEffect&, const GrGLCaps&); |
| 320 | 320 |
| 321 virtual void setData(const GrGLUniformManager&, const GrDrawEffect&) SK_OVER
RIDE; | 321 virtual void setData(const GrGLUniformManager&, const GrDrawEffect&) SK_OVER
RIDE; |
| 322 | 322 |
| 323 private: | 323 private: |
| 324 int width() const { return GrMorphologyEffect::WidthFromRadius(fRadius); } | 324 int width() const { return GrMorphologyEffect::WidthFromRadius(fRadius); } |
| 325 | 325 |
| 326 int fRadius; | 326 int fRadius; |
| 327 GrMorphologyEffect::MorphologyType fType; | 327 GrMorphologyEffect::MorphologyType fType; |
| 328 GrGLUniformManager::UniformHandle fImageIncrementUni; | 328 GrGLUniformManager::UniformHandle fImageIncrementUni; |
| 329 GrGLEffectMatrix fEffectMatrix; | |
| 330 | 329 |
| 331 typedef GrGLEffect INHERITED; | 330 typedef GrGLEffect INHERITED; |
| 332 }; | 331 }; |
| 333 | 332 |
| 334 GrGLMorphologyEffect::GrGLMorphologyEffect(const GrBackendEffectFactory& factory
, | 333 GrGLMorphologyEffect::GrGLMorphologyEffect(const GrBackendEffectFactory& factory
, |
| 335 const GrDrawEffect& drawEffect) | 334 const GrDrawEffect& drawEffect) |
| 336 : INHERITED(factory) | 335 : INHERITED(factory) { |
| 337 , fEffectMatrix(drawEffect.castEffect<GrMorphologyEffect>().coordsType()) { | |
| 338 const GrMorphologyEffect& m = drawEffect.castEffect<GrMorphologyEffect>(); | 336 const GrMorphologyEffect& m = drawEffect.castEffect<GrMorphologyEffect>(); |
| 339 fRadius = m.radius(); | 337 fRadius = m.radius(); |
| 340 fType = m.type(); | 338 fType = m.type(); |
| 341 } | 339 } |
| 342 | 340 |
| 343 void GrGLMorphologyEffect::emitCode(GrGLShaderBuilder* builder, | 341 void GrGLMorphologyEffect::emitCode(GrGLShaderBuilder* builder, |
| 344 const GrDrawEffect&, | 342 const GrDrawEffect&, |
| 345 EffectKey key, | 343 EffectKey key, |
| 346 const char* outputColor, | 344 const char* outputColor, |
| 347 const char* inputColor, | 345 const char* inputColor, |
| 346 const TransformedCoordsArray& coords, |
| 348 const TextureSamplerArray& samplers) { | 347 const TextureSamplerArray& samplers) { |
| 349 SkString coords; | 348 SkString coords2D = builder->ensureFSCoords2D(coords, 0); |
| 350 fEffectMatrix.emitCodeMakeFSCoords2D(builder, key, &coords); | |
| 351 fImageIncrementUni = builder->addUniform(GrGLShaderBuilder::kFragment_Visibi
lity, | 349 fImageIncrementUni = builder->addUniform(GrGLShaderBuilder::kFragment_Visibi
lity, |
| 352 kVec2f_GrSLType, "ImageIncrement"); | 350 kVec2f_GrSLType, "ImageIncrement"); |
| 353 | 351 |
| 354 const char* func; | 352 const char* func; |
| 355 switch (fType) { | 353 switch (fType) { |
| 356 case GrMorphologyEffect::kErode_MorphologyType: | 354 case GrMorphologyEffect::kErode_MorphologyType: |
| 357 builder->fsCodeAppendf("\t\t%s = vec4(1, 1, 1, 1);\n", outputColor); | 355 builder->fsCodeAppendf("\t\t%s = vec4(1, 1, 1, 1);\n", outputColor); |
| 358 func = "min"; | 356 func = "min"; |
| 359 break; | 357 break; |
| 360 case GrMorphologyEffect::kDilate_MorphologyType: | 358 case GrMorphologyEffect::kDilate_MorphologyType: |
| 361 builder->fsCodeAppendf("\t\t%s = vec4(0, 0, 0, 0);\n", outputColor); | 359 builder->fsCodeAppendf("\t\t%s = vec4(0, 0, 0, 0);\n", outputColor); |
| 362 func = "max"; | 360 func = "max"; |
| 363 break; | 361 break; |
| 364 default: | 362 default: |
| 365 GrCrash("Unexpected type"); | 363 GrCrash("Unexpected type"); |
| 366 func = ""; // suppress warning | 364 func = ""; // suppress warning |
| 367 break; | 365 break; |
| 368 } | 366 } |
| 369 const char* imgInc = builder->getUniformCStr(fImageIncrementUni); | 367 const char* imgInc = builder->getUniformCStr(fImageIncrementUni); |
| 370 | 368 |
| 371 builder->fsCodeAppendf("\t\tvec2 coord = %s - %d.0 * %s;\n", coords.c_str(),
fRadius, imgInc); | 369 builder->fsCodeAppendf("\t\tvec2 coord = %s - %d.0 * %s;\n", coords2D.c_str(
), fRadius, imgInc); |
| 372 builder->fsCodeAppendf("\t\tfor (int i = 0; i < %d; i++) {\n", this->width()
); | 370 builder->fsCodeAppendf("\t\tfor (int i = 0; i < %d; i++) {\n", this->width()
); |
| 373 builder->fsCodeAppendf("\t\t\t%s = %s(%s, ", outputColor, func, outputColor)
; | 371 builder->fsCodeAppendf("\t\t\t%s = %s(%s, ", outputColor, func, outputColor)
; |
| 374 builder->fsAppendTextureLookup(samplers[0], "coord"); | 372 builder->fsAppendTextureLookup(samplers[0], "coord"); |
| 375 builder->fsCodeAppend(");\n"); | 373 builder->fsCodeAppend(");\n"); |
| 376 builder->fsCodeAppendf("\t\t\tcoord += %s;\n", imgInc); | 374 builder->fsCodeAppendf("\t\t\tcoord += %s;\n", imgInc); |
| 377 builder->fsCodeAppend("\t\t}\n"); | 375 builder->fsCodeAppend("\t\t}\n"); |
| 378 SkString modulate; | 376 SkString modulate; |
| 379 GrGLSLMulVarBy4f(&modulate, 2, outputColor, inputColor); | 377 GrGLSLMulVarBy4f(&modulate, 2, outputColor, inputColor); |
| 380 builder->fsCodeAppend(modulate.c_str()); | 378 builder->fsCodeAppend(modulate.c_str()); |
| 381 } | 379 } |
| 382 | 380 |
| 383 GrGLEffect::EffectKey GrGLMorphologyEffect::GenKey(const GrDrawEffect& drawEffec
t, | 381 GrGLEffect::EffectKey GrGLMorphologyEffect::GenKey(const GrDrawEffect& drawEffec
t, |
| 384 const GrGLCaps&) { | 382 const GrGLCaps&) { |
| 385 const GrMorphologyEffect& m = drawEffect.castEffect<GrMorphologyEffect>(); | 383 const GrMorphologyEffect& m = drawEffect.castEffect<GrMorphologyEffect>(); |
| 386 EffectKey key = static_cast<EffectKey>(m.radius()); | 384 EffectKey key = static_cast<EffectKey>(m.radius()); |
| 387 key |= (m.type() << 8); | 385 key |= (m.type() << 8); |
| 388 key <<= GrGLEffectMatrix::kKeyBits; | 386 return key; |
| 389 EffectKey matrixKey = GrGLEffectMatrix::GenKey(m.getMatrix(), | |
| 390 drawEffect, | |
| 391 m.coordsType(), | |
| 392 m.texture(0)); | |
| 393 return key | matrixKey; | |
| 394 } | 387 } |
| 395 | 388 |
| 396 void GrGLMorphologyEffect::setData(const GrGLUniformManager& uman, | 389 void GrGLMorphologyEffect::setData(const GrGLUniformManager& uman, |
| 397 const GrDrawEffect& drawEffect) { | 390 const GrDrawEffect& drawEffect) { |
| 398 const Gr1DKernelEffect& kern = drawEffect.castEffect<Gr1DKernelEffect>(); | 391 const Gr1DKernelEffect& kern = drawEffect.castEffect<Gr1DKernelEffect>(); |
| 399 GrTexture& texture = *kern.texture(0); | 392 GrTexture& texture = *kern.texture(0); |
| 400 // the code we generated was for a specific kernel radius | 393 // the code we generated was for a specific kernel radius |
| 401 SkASSERT(kern.radius() == fRadius); | 394 SkASSERT(kern.radius() == fRadius); |
| 402 float imageIncrement[2] = { 0 }; | 395 float imageIncrement[2] = { 0 }; |
| 403 switch (kern.direction()) { | 396 switch (kern.direction()) { |
| 404 case Gr1DKernelEffect::kX_Direction: | 397 case Gr1DKernelEffect::kX_Direction: |
| 405 imageIncrement[0] = 1.0f / texture.width(); | 398 imageIncrement[0] = 1.0f / texture.width(); |
| 406 break; | 399 break; |
| 407 case Gr1DKernelEffect::kY_Direction: | 400 case Gr1DKernelEffect::kY_Direction: |
| 408 imageIncrement[1] = 1.0f / texture.height(); | 401 imageIncrement[1] = 1.0f / texture.height(); |
| 409 break; | 402 break; |
| 410 default: | 403 default: |
| 411 GrCrash("Unknown filter direction."); | 404 GrCrash("Unknown filter direction."); |
| 412 } | 405 } |
| 413 uman.set2fv(fImageIncrementUni, 0, 1, imageIncrement); | 406 uman.set2fv(fImageIncrementUni, 0, 1, imageIncrement); |
| 414 fEffectMatrix.setData(uman, kern.getMatrix(), drawEffect, kern.texture(0)); | |
| 415 } | 407 } |
| 416 | 408 |
| 417 /////////////////////////////////////////////////////////////////////////////// | 409 /////////////////////////////////////////////////////////////////////////////// |
| 418 | 410 |
| 419 GrMorphologyEffect::GrMorphologyEffect(GrTexture* texture, | 411 GrMorphologyEffect::GrMorphologyEffect(GrTexture* texture, |
| 420 Direction direction, | 412 Direction direction, |
| 421 int radius, | 413 int radius, |
| 422 MorphologyType type) | 414 MorphologyType type) |
| 423 : Gr1DKernelEffect(texture, direction, radius) | 415 : Gr1DKernelEffect(texture, direction, radius) |
| 424 , fType(type) { | 416 , fType(type) { |
| (...skipping 164 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 589 | 581 |
| 590 if (!apply_morphology(input, bounds, GrMorphologyEffect::kErode_MorphologyTy
pe, radius(), result)) { | 582 if (!apply_morphology(input, bounds, GrMorphologyEffect::kErode_MorphologyTy
pe, radius(), result)) { |
| 591 return false; | 583 return false; |
| 592 } | 584 } |
| 593 offset->fX += bounds.left(); | 585 offset->fX += bounds.left(); |
| 594 offset->fY += bounds.top(); | 586 offset->fY += bounds.top(); |
| 595 return true; | 587 return true; |
| 596 } | 588 } |
| 597 | 589 |
| 598 #endif | 590 #endif |
| OLD | NEW |