| 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 "SkReadBuffer.h" | 11 #include "SkReadBuffer.h" |
| 12 #include "SkWriteBuffer.h" | 12 #include "SkWriteBuffer.h" |
| 13 #include "SkRect.h" | 13 #include "SkRect.h" |
| 14 #include "SkMorphology_opts.h" | 14 #include "SkMorphology_opts.h" |
| 15 #if SK_SUPPORT_GPU | 15 #if SK_SUPPORT_GPU |
| 16 #include "GrContext.h" | 16 #include "GrContext.h" |
| 17 #include "GrTexture.h" | 17 #include "GrTexture.h" |
| 18 #include "GrTBackendEffectFactory.h" | 18 #include "GrTBackendProcessorFactory.h" |
| 19 #include "gl/GrGLEffect.h" | 19 #include "gl/GrGLProcessor.h" |
| 20 #include "gl/builders/GrGLProgramBuilder.h" | 20 #include "gl/builders/GrGLProgramBuilder.h" |
| 21 #include "effects/Gr1DKernelEffect.h" | 21 #include "effects/Gr1DKernelEffect.h" |
| 22 #endif | 22 #endif |
| 23 | 23 |
| 24 #ifdef SK_SUPPORT_LEGACY_DEEPFLATTENING | 24 #ifdef SK_SUPPORT_LEGACY_DEEPFLATTENING |
| 25 SkMorphologyImageFilter::SkMorphologyImageFilter(SkReadBuffer& buffer) | 25 SkMorphologyImageFilter::SkMorphologyImageFilter(SkReadBuffer& buffer) |
| 26 : INHERITED(1, buffer) { | 26 : INHERITED(1, buffer) { |
| 27 fRadius.fWidth = buffer.readInt(); | 27 fRadius.fWidth = buffer.readInt(); |
| 28 fRadius.fHeight = buffer.readInt(); | 28 fRadius.fHeight = buffer.readInt(); |
| 29 buffer.validate((fRadius.fWidth >= 0) && | 29 buffer.validate((fRadius.fWidth >= 0) && |
| (...skipping 258 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 288 */ | 288 */ |
| 289 class GrMorphologyEffect : public Gr1DKernelEffect { | 289 class GrMorphologyEffect : public Gr1DKernelEffect { |
| 290 | 290 |
| 291 public: | 291 public: |
| 292 | 292 |
| 293 enum MorphologyType { | 293 enum MorphologyType { |
| 294 kErode_MorphologyType, | 294 kErode_MorphologyType, |
| 295 kDilate_MorphologyType, | 295 kDilate_MorphologyType, |
| 296 }; | 296 }; |
| 297 | 297 |
| 298 static GrEffect* Create(GrTexture* tex, Direction dir, int radius, Morpholog
yType type) { | 298 static GrFragmentProcessor* Create(GrTexture* tex, Direction dir, int radius
, |
| 299 MorphologyType type) { |
| 299 return SkNEW_ARGS(GrMorphologyEffect, (tex, dir, radius, type)); | 300 return SkNEW_ARGS(GrMorphologyEffect, (tex, dir, radius, type)); |
| 300 } | 301 } |
| 301 | 302 |
| 302 virtual ~GrMorphologyEffect(); | 303 virtual ~GrMorphologyEffect(); |
| 303 | 304 |
| 304 MorphologyType type() const { return fType; } | 305 MorphologyType type() const { return fType; } |
| 305 | 306 |
| 306 static const char* Name() { return "Morphology"; } | 307 static const char* Name() { return "Morphology"; } |
| 307 | 308 |
| 308 typedef GrGLMorphologyEffect GLEffect; | 309 typedef GrGLMorphologyEffect GLProcessor; |
| 309 | 310 |
| 310 virtual const GrBackendEffectFactory& getFactory() const SK_OVERRIDE; | 311 virtual const GrBackendFragmentProcessorFactory& getFactory() const SK_OVERR
IDE; |
| 311 virtual void getConstantColorComponents(GrColor* color, uint32_t* validFlags
) const SK_OVERRIDE; | 312 virtual void getConstantColorComponents(GrColor* color, uint32_t* validFlags
) const SK_OVERRIDE; |
| 312 | 313 |
| 313 protected: | 314 protected: |
| 314 | 315 |
| 315 MorphologyType fType; | 316 MorphologyType fType; |
| 316 | 317 |
| 317 private: | 318 private: |
| 318 virtual bool onIsEqual(const GrEffect&) const SK_OVERRIDE; | 319 virtual bool onIsEqual(const GrProcessor&) const SK_OVERRIDE; |
| 319 | 320 |
| 320 GrMorphologyEffect(GrTexture*, Direction, int radius, MorphologyType); | 321 GrMorphologyEffect(GrTexture*, Direction, int radius, MorphologyType); |
| 321 | 322 |
| 322 GR_DECLARE_EFFECT_TEST; | 323 GR_DECLARE_FRAGMENT_PROCESSOR_TEST; |
| 323 | 324 |
| 324 typedef Gr1DKernelEffect INHERITED; | 325 typedef Gr1DKernelEffect INHERITED; |
| 325 }; | 326 }; |
| 326 | 327 |
| 327 /////////////////////////////////////////////////////////////////////////////// | 328 /////////////////////////////////////////////////////////////////////////////// |
| 328 | 329 |
| 329 class GrGLMorphologyEffect : public GrGLEffect { | 330 class GrGLMorphologyEffect : public GrGLFragmentProcessor { |
| 330 public: | 331 public: |
| 331 GrGLMorphologyEffect (const GrBackendEffectFactory&, const GrEffect&); | 332 GrGLMorphologyEffect (const GrBackendProcessorFactory&, const GrProcessor&); |
| 332 | 333 |
| 333 virtual void emitCode(GrGLProgramBuilder*, | 334 virtual void emitCode(GrGLProgramBuilder*, |
| 334 const GrEffect&, | 335 const GrFragmentProcessor&, |
| 335 const GrEffectKey&, | 336 const GrProcessorKey&, |
| 336 const char* outputColor, | 337 const char* outputColor, |
| 337 const char* inputColor, | 338 const char* inputColor, |
| 338 const TransformedCoordsArray&, | 339 const TransformedCoordsArray&, |
| 339 const TextureSamplerArray&) SK_OVERRIDE; | 340 const TextureSamplerArray&) SK_OVERRIDE; |
| 340 | 341 |
| 341 static inline void GenKey(const GrEffect&, const GrGLCaps&, GrEffectKeyBuild
er* b); | 342 static inline void GenKey(const GrProcessor&, const GrGLCaps&, GrProcessorKe
yBuilder* b); |
| 342 | 343 |
| 343 virtual void setData(const GrGLProgramDataManager&, const GrEffect&) SK_OVER
RIDE; | 344 virtual void setData(const GrGLProgramDataManager&, const GrProcessor&) SK_O
VERRIDE; |
| 344 | 345 |
| 345 private: | 346 private: |
| 346 int width() const { return GrMorphologyEffect::WidthFromRadius(fRadius); } | 347 int width() const { return GrMorphologyEffect::WidthFromRadius(fRadius); } |
| 347 | 348 |
| 348 int fRadius; | 349 int fRadius; |
| 349 GrMorphologyEffect::MorphologyType fType; | 350 GrMorphologyEffect::MorphologyType fType; |
| 350 GrGLProgramDataManager::UniformHandle fImageIncrementUni; | 351 GrGLProgramDataManager::UniformHandle fImageIncrementUni; |
| 351 | 352 |
| 352 typedef GrGLEffect INHERITED; | 353 typedef GrGLFragmentProcessor INHERITED; |
| 353 }; | 354 }; |
| 354 | 355 |
| 355 GrGLMorphologyEffect::GrGLMorphologyEffect(const GrBackendEffectFactory& factory
, | 356 GrGLMorphologyEffect::GrGLMorphologyEffect(const GrBackendProcessorFactory& fact
ory, |
| 356 const GrEffect& effect) | 357 const GrProcessor& proc) |
| 357 : INHERITED(factory) { | 358 : INHERITED(factory) { |
| 358 const GrMorphologyEffect& m = effect.cast<GrMorphologyEffect>(); | 359 const GrMorphologyEffect& m = proc.cast<GrMorphologyEffect>(); |
| 359 fRadius = m.radius(); | 360 fRadius = m.radius(); |
| 360 fType = m.type(); | 361 fType = m.type(); |
| 361 } | 362 } |
| 362 | 363 |
| 363 void GrGLMorphologyEffect::emitCode(GrGLProgramBuilder* builder, | 364 void GrGLMorphologyEffect::emitCode(GrGLProgramBuilder* builder, |
| 364 const GrEffect&, | 365 const GrFragmentProcessor&, |
| 365 const GrEffectKey& key, | 366 const GrProcessorKey& key, |
| 366 const char* outputColor, | 367 const char* outputColor, |
| 367 const char* inputColor, | 368 const char* inputColor, |
| 368 const TransformedCoordsArray& coords, | 369 const TransformedCoordsArray& coords, |
| 369 const TextureSamplerArray& samplers) { | 370 const TextureSamplerArray& samplers) { |
| 370 fImageIncrementUni = builder->addUniform(GrGLProgramBuilder::kFragment_Visib
ility, | 371 fImageIncrementUni = builder->addUniform(GrGLProgramBuilder::kFragment_Visib
ility, |
| 371 kVec2f_GrSLType, "ImageIncrement"); | 372 kVec2f_GrSLType, "ImageIncrement"); |
| 372 | 373 |
| 373 GrGLFragmentShaderBuilder* fsBuilder = builder->getFragmentShaderBuilder(); | 374 GrGLFragmentShaderBuilder* fsBuilder = builder->getFragmentShaderBuilder(); |
| 374 SkString coords2D = fsBuilder->ensureFSCoords2D(coords, 0); | 375 SkString coords2D = fsBuilder->ensureFSCoords2D(coords, 0); |
| 375 const char* func; | 376 const char* func; |
| (...skipping 18 matching lines...) Expand all Loading... |
| 394 fsBuilder->codeAppendf("\t\t\t%s = %s(%s, ", outputColor, func, outputColor)
; | 395 fsBuilder->codeAppendf("\t\t\t%s = %s(%s, ", outputColor, func, outputColor)
; |
| 395 fsBuilder->appendTextureLookup(samplers[0], "coord"); | 396 fsBuilder->appendTextureLookup(samplers[0], "coord"); |
| 396 fsBuilder->codeAppend(");\n"); | 397 fsBuilder->codeAppend(");\n"); |
| 397 fsBuilder->codeAppendf("\t\t\tcoord += %s;\n", imgInc); | 398 fsBuilder->codeAppendf("\t\t\tcoord += %s;\n", imgInc); |
| 398 fsBuilder->codeAppend("\t\t}\n"); | 399 fsBuilder->codeAppend("\t\t}\n"); |
| 399 SkString modulate; | 400 SkString modulate; |
| 400 GrGLSLMulVarBy4f(&modulate, 2, outputColor, inputColor); | 401 GrGLSLMulVarBy4f(&modulate, 2, outputColor, inputColor); |
| 401 fsBuilder->codeAppend(modulate.c_str()); | 402 fsBuilder->codeAppend(modulate.c_str()); |
| 402 } | 403 } |
| 403 | 404 |
| 404 void GrGLMorphologyEffect::GenKey(const GrEffect& effect, | 405 void GrGLMorphologyEffect::GenKey(const GrProcessor& proc, |
| 405 const GrGLCaps&, GrEffectKeyBuilder* b) { | 406 const GrGLCaps&, GrProcessorKeyBuilder* b) { |
| 406 const GrMorphologyEffect& m = effect.cast<GrMorphologyEffect>(); | 407 const GrMorphologyEffect& m = proc.cast<GrMorphologyEffect>(); |
| 407 uint32_t key = static_cast<uint32_t>(m.radius()); | 408 uint32_t key = static_cast<uint32_t>(m.radius()); |
| 408 key |= (m.type() << 8); | 409 key |= (m.type() << 8); |
| 409 b->add32(key); | 410 b->add32(key); |
| 410 } | 411 } |
| 411 | 412 |
| 412 void GrGLMorphologyEffect::setData(const GrGLProgramDataManager& pdman, | 413 void GrGLMorphologyEffect::setData(const GrGLProgramDataManager& pdman, |
| 413 const GrEffect& effect) { | 414 const GrProcessor& proc) { |
| 414 const Gr1DKernelEffect& kern = effect.cast<Gr1DKernelEffect>(); | 415 const Gr1DKernelEffect& kern = proc.cast<Gr1DKernelEffect>(); |
| 415 GrTexture& texture = *kern.texture(0); | 416 GrTexture& texture = *kern.texture(0); |
| 416 // the code we generated was for a specific kernel radius | 417 // the code we generated was for a specific kernel radius |
| 417 SkASSERT(kern.radius() == fRadius); | 418 SkASSERT(kern.radius() == fRadius); |
| 418 float imageIncrement[2] = { 0 }; | 419 float imageIncrement[2] = { 0 }; |
| 419 switch (kern.direction()) { | 420 switch (kern.direction()) { |
| 420 case Gr1DKernelEffect::kX_Direction: | 421 case Gr1DKernelEffect::kX_Direction: |
| 421 imageIncrement[0] = 1.0f / texture.width(); | 422 imageIncrement[0] = 1.0f / texture.width(); |
| 422 break; | 423 break; |
| 423 case Gr1DKernelEffect::kY_Direction: | 424 case Gr1DKernelEffect::kY_Direction: |
| 424 imageIncrement[1] = 1.0f / texture.height(); | 425 imageIncrement[1] = 1.0f / texture.height(); |
| (...skipping 10 matching lines...) Expand all Loading... |
| 435 Direction direction, | 436 Direction direction, |
| 436 int radius, | 437 int radius, |
| 437 MorphologyType type) | 438 MorphologyType type) |
| 438 : Gr1DKernelEffect(texture, direction, radius) | 439 : Gr1DKernelEffect(texture, direction, radius) |
| 439 , fType(type) { | 440 , fType(type) { |
| 440 } | 441 } |
| 441 | 442 |
| 442 GrMorphologyEffect::~GrMorphologyEffect() { | 443 GrMorphologyEffect::~GrMorphologyEffect() { |
| 443 } | 444 } |
| 444 | 445 |
| 445 const GrBackendEffectFactory& GrMorphologyEffect::getFactory() const { | 446 const GrBackendFragmentProcessorFactory& GrMorphologyEffect::getFactory() const
{ |
| 446 return GrTBackendEffectFactory<GrMorphologyEffect>::getInstance(); | 447 return GrTBackendFragmentProcessorFactory<GrMorphologyEffect>::getInstance()
; |
| 447 } | 448 } |
| 448 | 449 |
| 449 bool GrMorphologyEffect::onIsEqual(const GrEffect& sBase) const { | 450 bool GrMorphologyEffect::onIsEqual(const GrProcessor& sBase) const { |
| 450 const GrMorphologyEffect& s = sBase.cast<GrMorphologyEffect>(); | 451 const GrMorphologyEffect& s = sBase.cast<GrMorphologyEffect>(); |
| 451 return (this->texture(0) == s.texture(0) && | 452 return (this->texture(0) == s.texture(0) && |
| 452 this->radius() == s.radius() && | 453 this->radius() == s.radius() && |
| 453 this->direction() == s.direction() && | 454 this->direction() == s.direction() && |
| 454 this->type() == s.type()); | 455 this->type() == s.type()); |
| 455 } | 456 } |
| 456 | 457 |
| 457 void GrMorphologyEffect::getConstantColorComponents(GrColor* color, uint32_t* va
lidFlags) const { | 458 void GrMorphologyEffect::getConstantColorComponents(GrColor* color, uint32_t* va
lidFlags) const { |
| 458 // This is valid because the color components of the result of the kernel al
l come | 459 // This is valid because the color components of the result of the kernel al
l come |
| 459 // exactly from existing values in the source texture. | 460 // exactly from existing values in the source texture. |
| 460 this->updateConstantColorComponentsForModulation(color, validFlags); | 461 this->updateConstantColorComponentsForModulation(color, validFlags); |
| 461 } | 462 } |
| 462 | 463 |
| 463 /////////////////////////////////////////////////////////////////////////////// | 464 /////////////////////////////////////////////////////////////////////////////// |
| 464 | 465 |
| 465 GR_DEFINE_EFFECT_TEST(GrMorphologyEffect); | 466 GR_DEFINE_FRAGMENT_PROCESSOR_TEST(GrMorphologyEffect); |
| 466 | 467 |
| 467 GrEffect* GrMorphologyEffect::TestCreate(SkRandom* random, | 468 GrFragmentProcessor* GrMorphologyEffect::TestCreate(SkRandom* random, |
| 468 GrContext*, | 469 GrContext*, |
| 469 const GrDrawTargetCaps&, | 470 const GrDrawTargetCaps&, |
| 470 GrTexture* textures[]) { | 471 GrTexture* textures[]) { |
| 471 int texIdx = random->nextBool() ? GrEffectUnitTest::kSkiaPMTextureIdx : | 472 int texIdx = random->nextBool() ? GrProcessorUnitTest::kSkiaPMTextureIdx : |
| 472 GrEffectUnitTest::kAlphaTextureIdx; | 473 GrProcessorUnitTest::kAlphaTextureIdx; |
| 473 Direction dir = random->nextBool() ? kX_Direction : kY_Direction; | 474 Direction dir = random->nextBool() ? kX_Direction : kY_Direction; |
| 474 static const int kMaxRadius = 10; | 475 static const int kMaxRadius = 10; |
| 475 int radius = random->nextRangeU(1, kMaxRadius); | 476 int radius = random->nextRangeU(1, kMaxRadius); |
| 476 MorphologyType type = random->nextBool() ? GrMorphologyEffect::kErode_Morpho
logyType : | 477 MorphologyType type = random->nextBool() ? GrMorphologyEffect::kErode_Morpho
logyType : |
| 477 GrMorphologyEffect::kDilate_Morph
ologyType; | 478 GrMorphologyEffect::kDilate_Morph
ologyType; |
| 478 | 479 |
| 479 return GrMorphologyEffect::Create(textures[texIdx], dir, radius, type); | 480 return GrMorphologyEffect::Create(textures[texIdx], dir, radius, type); |
| 480 } | 481 } |
| 481 | 482 |
| 482 namespace { | 483 namespace { |
| 483 | 484 |
| 484 void apply_morphology_pass(GrContext* context, | 485 void apply_morphology_pass(GrContext* context, |
| 485 GrTexture* texture, | 486 GrTexture* texture, |
| 486 const SkIRect& srcRect, | 487 const SkIRect& srcRect, |
| 487 const SkIRect& dstRect, | 488 const SkIRect& dstRect, |
| 488 int radius, | 489 int radius, |
| 489 GrMorphologyEffect::MorphologyType morphType, | 490 GrMorphologyEffect::MorphologyType morphType, |
| 490 Gr1DKernelEffect::Direction direction) { | 491 Gr1DKernelEffect::Direction direction) { |
| 491 GrPaint paint; | 492 GrPaint paint; |
| 492 paint.addColorEffect(GrMorphologyEffect::Create(texture, | 493 paint.addColorProcessor(GrMorphologyEffect::Create(texture, |
| 493 direction, | 494 direction, |
| 494 radius, | 495 radius, |
| 495 morphType))->unref(); | 496 morphType))->unref(); |
| 496 context->drawRectToRect(paint, SkRect::Make(dstRect), SkRect::Make(srcRect))
; | 497 context->drawRectToRect(paint, SkRect::Make(dstRect), SkRect::Make(srcRect))
; |
| 497 } | 498 } |
| 498 | 499 |
| 499 bool apply_morphology(const SkBitmap& input, | 500 bool apply_morphology(const SkBitmap& input, |
| 500 const SkIRect& rect, | 501 const SkIRect& rect, |
| 501 GrMorphologyEffect::MorphologyType morphType, | 502 GrMorphologyEffect::MorphologyType morphType, |
| 502 SkISize radius, | 503 SkISize radius, |
| 503 SkBitmap* dst) { | 504 SkBitmap* dst) { |
| 504 GrTexture* srcTexture = input.getTexture(); | 505 GrTexture* srcTexture = input.getTexture(); |
| 505 SkASSERT(srcTexture); | 506 SkASSERT(srcTexture); |
| (...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 601 SkBitmap* result, SkIPoint* offset) con
st { | 602 SkBitmap* result, SkIPoint* offset) con
st { |
| 602 return this->filterImageGPUGeneric(true, proxy, src, ctx, result, offset); | 603 return this->filterImageGPUGeneric(true, proxy, src, ctx, result, offset); |
| 603 } | 604 } |
| 604 | 605 |
| 605 bool SkErodeImageFilter::filterImageGPU(Proxy* proxy, const SkBitmap& src, const
Context& ctx, | 606 bool SkErodeImageFilter::filterImageGPU(Proxy* proxy, const SkBitmap& src, const
Context& ctx, |
| 606 SkBitmap* result, SkIPoint* offset) cons
t { | 607 SkBitmap* result, SkIPoint* offset) cons
t { |
| 607 return this->filterImageGPUGeneric(false, proxy, src, ctx, result, offset); | 608 return this->filterImageGPUGeneric(false, proxy, src, ctx, result, offset); |
| 608 } | 609 } |
| 609 | 610 |
| 610 #endif | 611 #endif |
| OLD | NEW |