| 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 "GrDrawContext.h" |
| 17 #include "GrInvariantOutput.h" | 18 #include "GrInvariantOutput.h" |
| 18 #include "GrTexture.h" | 19 #include "GrTexture.h" |
| 19 #include "effects/Gr1DKernelEffect.h" | 20 #include "effects/Gr1DKernelEffect.h" |
| 20 #include "gl/GrGLProcessor.h" | 21 #include "gl/GrGLProcessor.h" |
| 21 #include "gl/builders/GrGLProgramBuilder.h" | 22 #include "gl/builders/GrGLProgramBuilder.h" |
| 22 #endif | 23 #endif |
| 23 | 24 |
| 24 SkMorphologyImageFilter::SkMorphologyImageFilter(int radiusX, | 25 SkMorphologyImageFilter::SkMorphologyImageFilter(int radiusX, |
| 25 int radiusY, | 26 int radiusY, |
| 26 SkImageFilter* input, | 27 SkImageFilter* input, |
| (...skipping 526 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 553 int radius = random->nextRangeU(1, kMaxRadius); | 554 int radius = random->nextRangeU(1, kMaxRadius); |
| 554 MorphologyType type = random->nextBool() ? GrMorphologyEffect::kErode_Morpho
logyType : | 555 MorphologyType type = random->nextBool() ? GrMorphologyEffect::kErode_Morpho
logyType : |
| 555 GrMorphologyEffect::kDilate_Morph
ologyType; | 556 GrMorphologyEffect::kDilate_Morph
ologyType; |
| 556 | 557 |
| 557 return GrMorphologyEffect::Create(textures[texIdx], dir, radius, type); | 558 return GrMorphologyEffect::Create(textures[texIdx], dir, radius, type); |
| 558 } | 559 } |
| 559 | 560 |
| 560 namespace { | 561 namespace { |
| 561 | 562 |
| 562 | 563 |
| 563 void apply_morphology_rect(GrContext* context, | 564 void apply_morphology_rect(GrDrawContext* drawContext, |
| 564 GrRenderTarget* rt, | 565 GrRenderTarget* rt, |
| 565 const GrClip& clip, | 566 const GrClip& clip, |
| 566 GrTexture* texture, | 567 GrTexture* texture, |
| 567 const SkIRect& srcRect, | 568 const SkIRect& srcRect, |
| 568 const SkIRect& dstRect, | 569 const SkIRect& dstRect, |
| 569 int radius, | 570 int radius, |
| 570 GrMorphologyEffect::MorphologyType morphType, | 571 GrMorphologyEffect::MorphologyType morphType, |
| 571 float bounds[2], | 572 float bounds[2], |
| 572 Gr1DKernelEffect::Direction direction) { | 573 Gr1DKernelEffect::Direction direction) { |
| 573 GrPaint paint; | 574 GrPaint paint; |
| 574 paint.addColorProcessor(GrMorphologyEffect::Create(texture, | 575 paint.addColorProcessor(GrMorphologyEffect::Create(texture, |
| 575 direction, | 576 direction, |
| 576 radius, | 577 radius, |
| 577 morphType, | 578 morphType, |
| 578 bounds))->unref(); | 579 bounds))->unref(); |
| 579 context->drawNonAARectToRect(rt, clip, paint, SkMatrix::I(), SkRect::Make(ds
tRect), | 580 drawContext->drawNonAARectToRect(rt, clip, paint, SkMatrix::I(), SkRect::Mak
e(dstRect), |
| 580 SkRect::Make(srcRect)); | 581 SkRect::Make(srcRect)); |
| 581 } | 582 } |
| 582 | 583 |
| 583 void apply_morphology_rect_no_bounds(GrContext* context, | 584 void apply_morphology_rect_no_bounds(GrDrawContext* drawContext, |
| 584 GrRenderTarget* rt, | 585 GrRenderTarget* rt, |
| 585 const GrClip& clip, | 586 const GrClip& clip, |
| 586 GrTexture* texture, | 587 GrTexture* texture, |
| 587 const SkIRect& srcRect, | 588 const SkIRect& srcRect, |
| 588 const SkIRect& dstRect, | 589 const SkIRect& dstRect, |
| 589 int radius, | 590 int radius, |
| 590 GrMorphologyEffect::MorphologyType morphTyp
e, | 591 GrMorphologyEffect::MorphologyType morphTyp
e, |
| 591 Gr1DKernelEffect::Direction direction) { | 592 Gr1DKernelEffect::Direction direction) { |
| 592 GrPaint paint; | 593 GrPaint paint; |
| 593 paint.addColorProcessor(GrMorphologyEffect::Create(texture, | 594 paint.addColorProcessor(GrMorphologyEffect::Create(texture, |
| 594 direction, | 595 direction, |
| 595 radius, | 596 radius, |
| 596 morphType))->unref(); | 597 morphType))->unref(); |
| 597 context->drawNonAARectToRect(rt, clip, paint, SkMatrix::I(), SkRect::Make(ds
tRect), | 598 drawContext->drawNonAARectToRect(rt, clip, paint, SkMatrix::I(), SkRect::Mak
e(dstRect), |
| 598 SkRect::Make(srcRect)); | 599 SkRect::Make(srcRect)); |
| 599 } | 600 } |
| 600 | 601 |
| 601 void apply_morphology_pass(GrContext* context, | 602 void apply_morphology_pass(GrDrawContext* drawContext, |
| 602 GrRenderTarget* rt, | 603 GrRenderTarget* rt, |
| 603 const GrClip& clip, | 604 const GrClip& clip, |
| 604 GrTexture* texture, | 605 GrTexture* texture, |
| 605 const SkIRect& srcRect, | 606 const SkIRect& srcRect, |
| 606 const SkIRect& dstRect, | 607 const SkIRect& dstRect, |
| 607 int radius, | 608 int radius, |
| 608 GrMorphologyEffect::MorphologyType morphType, | 609 GrMorphologyEffect::MorphologyType morphType, |
| 609 Gr1DKernelEffect::Direction direction) { | 610 Gr1DKernelEffect::Direction direction) { |
| 610 float bounds[2] = { 0.0f, 1.0f }; | 611 float bounds[2] = { 0.0f, 1.0f }; |
| 611 SkIRect lowerSrcRect = srcRect, lowerDstRect = dstRect; | 612 SkIRect lowerSrcRect = srcRect, lowerDstRect = dstRect; |
| (...skipping 13 matching lines...) Expand all Loading... |
| 625 bounds[1] = (SkIntToScalar(srcRect.bottom()) - 0.5f) / texture->height()
; | 626 bounds[1] = (SkIntToScalar(srcRect.bottom()) - 0.5f) / texture->height()
; |
| 626 lowerSrcRect.fBottom = srcRect.top() + radius; | 627 lowerSrcRect.fBottom = srcRect.top() + radius; |
| 627 lowerDstRect.fBottom = dstRect.top() + radius; | 628 lowerDstRect.fBottom = dstRect.top() + radius; |
| 628 upperSrcRect.fTop = srcRect.bottom() - radius; | 629 upperSrcRect.fTop = srcRect.bottom() - radius; |
| 629 upperDstRect.fTop = dstRect.bottom() - radius; | 630 upperDstRect.fTop = dstRect.bottom() - radius; |
| 630 middleSrcRect.inset(0, radius); | 631 middleSrcRect.inset(0, radius); |
| 631 middleDstRect.inset(0, radius); | 632 middleDstRect.inset(0, radius); |
| 632 } | 633 } |
| 633 if (middleSrcRect.fLeft - middleSrcRect.fRight >= 0) { | 634 if (middleSrcRect.fLeft - middleSrcRect.fRight >= 0) { |
| 634 // radius covers srcRect; use bounds over entire draw | 635 // radius covers srcRect; use bounds over entire draw |
| 635 apply_morphology_rect(context, rt, clip, texture, srcRect, dstRect, radi
us, | 636 apply_morphology_rect(drawContext, rt, clip, texture, srcRect, dstRect,
radius, |
| 636 morphType, bounds, direction); | 637 morphType, bounds, direction); |
| 637 } else { | 638 } else { |
| 638 // Draw upper and lower margins with bounds; middle without. | 639 // Draw upper and lower margins with bounds; middle without. |
| 639 apply_morphology_rect(context, rt, clip, texture, lowerSrcRect, lowerDst
Rect, radius, | 640 apply_morphology_rect(drawContext, rt, clip, texture, lowerSrcRect, lowe
rDstRect, radius, |
| 640 morphType, bounds, direction); | 641 morphType, bounds, direction); |
| 641 apply_morphology_rect(context, rt, clip, texture, upperSrcRect, upperDst
Rect, radius, | 642 apply_morphology_rect(drawContext, rt, clip, texture, upperSrcRect, uppe
rDstRect, radius, |
| 642 morphType, bounds, direction); | 643 morphType, bounds, direction); |
| 643 apply_morphology_rect_no_bounds(context, rt, clip, texture, middleSrcRec
t, middleDstRect, | 644 apply_morphology_rect_no_bounds(drawContext, rt, clip, texture, middleSr
cRect, middleDstRect, |
| 644 radius, morphType, direction); | 645 radius, morphType, direction); |
| 645 } | 646 } |
| 646 } | 647 } |
| 647 | 648 |
| 648 bool apply_morphology(const SkBitmap& input, | 649 bool apply_morphology(const SkBitmap& input, |
| 649 const SkIRect& rect, | 650 const SkIRect& rect, |
| 650 GrMorphologyEffect::MorphologyType morphType, | 651 GrMorphologyEffect::MorphologyType morphType, |
| 651 SkISize radius, | 652 SkISize radius, |
| 652 SkBitmap* dst) { | 653 SkBitmap* dst) { |
| 653 SkAutoTUnref<GrTexture> srcTexture(SkRef(input.getTexture())); | 654 SkAutoTUnref<GrTexture> srcTexture(SkRef(input.getTexture())); |
| 654 SkASSERT(srcTexture); | 655 SkASSERT(srcTexture); |
| 655 GrContext* context = srcTexture->getContext(); | 656 GrContext* context = srcTexture->getContext(); |
| 656 | 657 |
| 657 // setup new clip | 658 // setup new clip |
| 658 GrClip clip(SkRect::MakeWH(SkIntToScalar(srcTexture->width()), | 659 GrClip clip(SkRect::MakeWH(SkIntToScalar(srcTexture->width()), |
| 659 SkIntToScalar(srcTexture->height()))); | 660 SkIntToScalar(srcTexture->height()))); |
| 660 | 661 |
| 661 SkIRect dstRect = SkIRect::MakeWH(rect.width(), rect.height()); | 662 SkIRect dstRect = SkIRect::MakeWH(rect.width(), rect.height()); |
| 662 GrSurfaceDesc desc; | 663 GrSurfaceDesc desc; |
| 663 desc.fFlags = kRenderTarget_GrSurfaceFlag; | 664 desc.fFlags = kRenderTarget_GrSurfaceFlag; |
| 664 desc.fWidth = rect.width(); | 665 desc.fWidth = rect.width(); |
| 665 desc.fHeight = rect.height(); | 666 desc.fHeight = rect.height(); |
| 666 desc.fConfig = kSkia8888_GrPixelConfig; | 667 desc.fConfig = kSkia8888_GrPixelConfig; |
| 667 SkIRect srcRect = rect; | 668 SkIRect srcRect = rect; |
| 668 | 669 |
| 670 GrDrawContext* drawContext = context->drawContext(); |
| 671 if (!drawContext) { |
| 672 return false; |
| 673 } |
| 674 |
| 669 if (radius.fWidth > 0) { | 675 if (radius.fWidth > 0) { |
| 670 GrTexture* texture = context->textureProvider()->refScratchTexture( | 676 GrTexture* texture = context->textureProvider()->refScratchTexture( |
| 671 desc, GrTextureProvider::kApprox_ScratchTexMatch); | 677 desc, GrTextureProvider::kApprox_ScratchTexMatch); |
| 672 if (NULL == texture) { | 678 if (NULL == texture) { |
| 673 return false; | 679 return false; |
| 674 } | 680 } |
| 675 apply_morphology_pass(context, texture->asRenderTarget(), clip, srcTextu
re, | 681 apply_morphology_pass(drawContext, texture->asRenderTarget(), clip, srcT
exture, |
| 676 srcRect, dstRect, radius.fWidth, morphType, | 682 srcRect, dstRect, radius.fWidth, morphType, |
| 677 Gr1DKernelEffect::kX_Direction); | 683 Gr1DKernelEffect::kX_Direction); |
| 678 SkIRect clearRect = SkIRect::MakeXYWH(dstRect.fLeft, dstRect.fBottom, | 684 SkIRect clearRect = SkIRect::MakeXYWH(dstRect.fLeft, dstRect.fBottom, |
| 679 dstRect.width(), radius.fHeight); | 685 dstRect.width(), radius.fHeight); |
| 680 GrColor clearColor = GrMorphologyEffect::kErode_MorphologyType == morphT
ype ? | 686 GrColor clearColor = GrMorphologyEffect::kErode_MorphologyType == morphT
ype ? |
| 681 SK_ColorWHITE : | 687 SK_ColorWHITE : |
| 682 SK_ColorTRANSPARENT; | 688 SK_ColorTRANSPARENT; |
| 683 context->clear(&clearRect, clearColor, false, texture->asRenderTarget())
; | 689 drawContext->clear(texture->asRenderTarget(), &clearRect, clearColor, fa
lse); |
| 684 srcTexture.reset(texture); | 690 srcTexture.reset(texture); |
| 685 srcRect = dstRect; | 691 srcRect = dstRect; |
| 686 } | 692 } |
| 687 if (radius.fHeight > 0) { | 693 if (radius.fHeight > 0) { |
| 688 GrTexture* texture = context->textureProvider()->refScratchTexture(desc, | 694 GrTexture* texture = context->textureProvider()->refScratchTexture(desc, |
| 689 GrTextureProvider::kApprox_ScratchTexMatch); | 695 GrTextureProvider::kApprox_ScratchTexMatch); |
| 690 if (NULL == texture) { | 696 if (NULL == texture) { |
| 691 return false; | 697 return false; |
| 692 } | 698 } |
| 693 apply_morphology_pass(context, texture->asRenderTarget(), clip, srcTextu
re, | 699 apply_morphology_pass(drawContext, texture->asRenderTarget(), clip, srcT
exture, |
| 694 srcRect, dstRect, radius.fHeight, morphType, | 700 srcRect, dstRect, radius.fHeight, morphType, |
| 695 Gr1DKernelEffect::kY_Direction); | 701 Gr1DKernelEffect::kY_Direction); |
| 696 srcTexture.reset(texture); | 702 srcTexture.reset(texture); |
| 697 } | 703 } |
| 698 SkImageFilter::WrapTexture(srcTexture, rect.width(), rect.height(), dst); | 704 SkImageFilter::WrapTexture(srcTexture, rect.width(), rect.height(), dst); |
| 699 return true; | 705 return true; |
| 700 } | 706 } |
| 701 | 707 |
| 702 }; | 708 }; |
| 703 | 709 |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 749 SkBitmap* result, SkIPoint* offset) con
st { | 755 SkBitmap* result, SkIPoint* offset) con
st { |
| 750 return this->filterImageGPUGeneric(true, proxy, src, ctx, result, offset); | 756 return this->filterImageGPUGeneric(true, proxy, src, ctx, result, offset); |
| 751 } | 757 } |
| 752 | 758 |
| 753 bool SkErodeImageFilter::filterImageGPU(Proxy* proxy, const SkBitmap& src, const
Context& ctx, | 759 bool SkErodeImageFilter::filterImageGPU(Proxy* proxy, const SkBitmap& src, const
Context& ctx, |
| 754 SkBitmap* result, SkIPoint* offset) cons
t { | 760 SkBitmap* result, SkIPoint* offset) cons
t { |
| 755 return this->filterImageGPUGeneric(false, proxy, src, ctx, result, offset); | 761 return this->filterImageGPUGeneric(false, proxy, src, ctx, result, offset); |
| 756 } | 762 } |
| 757 | 763 |
| 758 #endif | 764 #endif |
| OLD | NEW |