| 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" |
| (...skipping 545 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 556 GrMorphologyEffect::kDilate_Morph
ologyType; | 556 GrMorphologyEffect::kDilate_Morph
ologyType; |
| 557 | 557 |
| 558 return GrMorphologyEffect::Create(textures[texIdx], dir, radius, type); | 558 return GrMorphologyEffect::Create(textures[texIdx], dir, radius, type); |
| 559 } | 559 } |
| 560 | 560 |
| 561 namespace { | 561 namespace { |
| 562 | 562 |
| 563 | 563 |
| 564 void apply_morphology_rect(GrContext* context, | 564 void apply_morphology_rect(GrContext* context, |
| 565 GrRenderTarget* rt, | 565 GrRenderTarget* rt, |
| 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, paint, SkMatrix::I(), SkRect::Make(dstRect)
, | 580 context->drawNonAARectToRect(rt, clip, paint, SkMatrix::I(), SkRect::Make(ds
tRect), |
| 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(GrContext* context, |
| 584 GrRenderTarget* rt, | 585 GrRenderTarget* rt, |
| 586 const GrClip& clip, |
| 585 GrTexture* texture, | 587 GrTexture* texture, |
| 586 const SkIRect& srcRect, | 588 const SkIRect& srcRect, |
| 587 const SkIRect& dstRect, | 589 const SkIRect& dstRect, |
| 588 int radius, | 590 int radius, |
| 589 GrMorphologyEffect::MorphologyType morphTyp
e, | 591 GrMorphologyEffect::MorphologyType morphTyp
e, |
| 590 Gr1DKernelEffect::Direction direction) { | 592 Gr1DKernelEffect::Direction direction) { |
| 591 GrPaint paint; | 593 GrPaint paint; |
| 592 paint.addColorProcessor(GrMorphologyEffect::Create(texture, | 594 paint.addColorProcessor(GrMorphologyEffect::Create(texture, |
| 593 direction, | 595 direction, |
| 594 radius, | 596 radius, |
| 595 morphType))->unref(); | 597 morphType))->unref(); |
| 596 context->drawNonAARectToRect(rt, paint, SkMatrix::I(), SkRect::Make(dstRect)
, | 598 context->drawNonAARectToRect(rt, clip, paint, SkMatrix::I(), SkRect::Make(ds
tRect), |
| 597 SkRect::Make(srcRect)); | 599 SkRect::Make(srcRect)); |
| 598 } | 600 } |
| 599 | 601 |
| 600 void apply_morphology_pass(GrContext* context, | 602 void apply_morphology_pass(GrContext* context, |
| 601 GrRenderTarget* rt, | 603 GrRenderTarget* rt, |
| 604 const GrClip& clip, |
| 602 GrTexture* texture, | 605 GrTexture* texture, |
| 603 const SkIRect& srcRect, | 606 const SkIRect& srcRect, |
| 604 const SkIRect& dstRect, | 607 const SkIRect& dstRect, |
| 605 int radius, | 608 int radius, |
| 606 GrMorphologyEffect::MorphologyType morphType, | 609 GrMorphologyEffect::MorphologyType morphType, |
| 607 Gr1DKernelEffect::Direction direction) { | 610 Gr1DKernelEffect::Direction direction) { |
| 608 float bounds[2] = { 0.0f, 1.0f }; | 611 float bounds[2] = { 0.0f, 1.0f }; |
| 609 SkIRect lowerSrcRect = srcRect, lowerDstRect = dstRect; | 612 SkIRect lowerSrcRect = srcRect, lowerDstRect = dstRect; |
| 610 SkIRect middleSrcRect = srcRect, middleDstRect = dstRect; | 613 SkIRect middleSrcRect = srcRect, middleDstRect = dstRect; |
| 611 SkIRect upperSrcRect = srcRect, upperDstRect = dstRect; | 614 SkIRect upperSrcRect = srcRect, upperDstRect = dstRect; |
| (...skipping 11 matching lines...) Expand all Loading... |
| 623 bounds[1] = (SkIntToScalar(srcRect.bottom()) - 0.5f) / texture->height()
; | 626 bounds[1] = (SkIntToScalar(srcRect.bottom()) - 0.5f) / texture->height()
; |
| 624 lowerSrcRect.fBottom = srcRect.top() + radius; | 627 lowerSrcRect.fBottom = srcRect.top() + radius; |
| 625 lowerDstRect.fBottom = dstRect.top() + radius; | 628 lowerDstRect.fBottom = dstRect.top() + radius; |
| 626 upperSrcRect.fTop = srcRect.bottom() - radius; | 629 upperSrcRect.fTop = srcRect.bottom() - radius; |
| 627 upperDstRect.fTop = dstRect.bottom() - radius; | 630 upperDstRect.fTop = dstRect.bottom() - radius; |
| 628 middleSrcRect.inset(0, radius); | 631 middleSrcRect.inset(0, radius); |
| 629 middleDstRect.inset(0, radius); | 632 middleDstRect.inset(0, radius); |
| 630 } | 633 } |
| 631 if (middleSrcRect.fLeft - middleSrcRect.fRight >= 0) { | 634 if (middleSrcRect.fLeft - middleSrcRect.fRight >= 0) { |
| 632 // radius covers srcRect; use bounds over entire draw | 635 // radius covers srcRect; use bounds over entire draw |
| 633 apply_morphology_rect(context, rt, texture, srcRect, dstRect, radius, | 636 apply_morphology_rect(context, rt, clip, texture, srcRect, dstRect, radi
us, |
| 634 morphType, bounds, direction); | 637 morphType, bounds, direction); |
| 635 } else { | 638 } else { |
| 636 // Draw upper and lower margins with bounds; middle without. | 639 // Draw upper and lower margins with bounds; middle without. |
| 637 apply_morphology_rect(context, rt,texture, lowerSrcRect, lowerDstRect, r
adius, | 640 apply_morphology_rect(context, rt, clip, texture, lowerSrcRect, lowerDst
Rect, radius, |
| 638 morphType, bounds, direction); | 641 morphType, bounds, direction); |
| 639 apply_morphology_rect(context, rt, texture, upperSrcRect, upperDstRect,
radius, | 642 apply_morphology_rect(context, rt, clip, texture, upperSrcRect, upperDst
Rect, radius, |
| 640 morphType, bounds, direction); | 643 morphType, bounds, direction); |
| 641 apply_morphology_rect_no_bounds(context, rt, texture, middleSrcRect, mid
dleDstRect, radius, | 644 apply_morphology_rect_no_bounds(context, rt, clip, texture, middleSrcRec
t, middleDstRect, |
| 642 morphType, direction); | 645 radius, morphType, direction); |
| 643 } | 646 } |
| 644 } | 647 } |
| 645 | 648 |
| 646 bool apply_morphology(const SkBitmap& input, | 649 bool apply_morphology(const SkBitmap& input, |
| 647 const SkIRect& rect, | 650 const SkIRect& rect, |
| 648 GrMorphologyEffect::MorphologyType morphType, | 651 GrMorphologyEffect::MorphologyType morphType, |
| 649 SkISize radius, | 652 SkISize radius, |
| 650 SkBitmap* dst) { | 653 SkBitmap* dst) { |
| 651 SkAutoTUnref<GrTexture> srcTexture(SkRef(input.getTexture())); | 654 SkAutoTUnref<GrTexture> srcTexture(SkRef(input.getTexture())); |
| 652 SkASSERT(srcTexture); | 655 SkASSERT(srcTexture); |
| 653 GrContext* context = srcTexture->getContext(); | 656 GrContext* context = srcTexture->getContext(); |
| 654 | 657 |
| 655 GrContext::AutoClip acs(context, SkRect::MakeWH(SkIntToScalar(srcTexture->wi
dth()), | 658 // setup new clip |
| 656 SkIntToScalar(srcTexture->he
ight()))); | 659 GrClip clip(SkRect::MakeWH(SkIntToScalar(srcTexture->width()), |
| 660 SkIntToScalar(srcTexture->height()))); |
| 657 | 661 |
| 658 SkIRect dstRect = SkIRect::MakeWH(rect.width(), rect.height()); | 662 SkIRect dstRect = SkIRect::MakeWH(rect.width(), rect.height()); |
| 659 GrSurfaceDesc desc; | 663 GrSurfaceDesc desc; |
| 660 desc.fFlags = kRenderTarget_GrSurfaceFlag; | 664 desc.fFlags = kRenderTarget_GrSurfaceFlag; |
| 661 desc.fWidth = rect.width(); | 665 desc.fWidth = rect.width(); |
| 662 desc.fHeight = rect.height(); | 666 desc.fHeight = rect.height(); |
| 663 desc.fConfig = kSkia8888_GrPixelConfig; | 667 desc.fConfig = kSkia8888_GrPixelConfig; |
| 664 SkIRect srcRect = rect; | 668 SkIRect srcRect = rect; |
| 665 | 669 |
| 666 if (radius.fWidth > 0) { | 670 if (radius.fWidth > 0) { |
| 667 GrTexture* texture = context->refScratchTexture(desc, GrContext::kApprox
_ScratchTexMatch); | 671 GrTexture* texture = context->refScratchTexture(desc, GrContext::kApprox
_ScratchTexMatch); |
| 668 if (NULL == texture) { | 672 if (NULL == texture) { |
| 669 return false; | 673 return false; |
| 670 } | 674 } |
| 671 apply_morphology_pass(context, texture->asRenderTarget(), srcTexture, sr
cRect, dstRect, | 675 apply_morphology_pass(context, texture->asRenderTarget(), clip, srcTextu
re, |
| 672 radius.fWidth, morphType, Gr1DKernelEffect::kX_Dir
ection); | 676 srcRect, dstRect, radius.fWidth, morphType, |
| 677 Gr1DKernelEffect::kX_Direction); |
| 673 SkIRect clearRect = SkIRect::MakeXYWH(dstRect.fLeft, dstRect.fBottom, | 678 SkIRect clearRect = SkIRect::MakeXYWH(dstRect.fLeft, dstRect.fBottom, |
| 674 dstRect.width(), radius.fHeight); | 679 dstRect.width(), radius.fHeight); |
| 675 GrColor clearColor = GrMorphologyEffect::kErode_MorphologyType == morphT
ype ? | 680 GrColor clearColor = GrMorphologyEffect::kErode_MorphologyType == morphT
ype ? |
| 676 SK_ColorWHITE : | 681 SK_ColorWHITE : |
| 677 SK_ColorTRANSPARENT; | 682 SK_ColorTRANSPARENT; |
| 678 context->clear(&clearRect, clearColor, false, texture->asRenderTarget())
; | 683 context->clear(&clearRect, clearColor, false, texture->asRenderTarget())
; |
| 679 srcTexture.reset(texture); | 684 srcTexture.reset(texture); |
| 680 srcRect = dstRect; | 685 srcRect = dstRect; |
| 681 } | 686 } |
| 682 if (radius.fHeight > 0) { | 687 if (radius.fHeight > 0) { |
| 683 GrTexture* texture = context->refScratchTexture(desc, GrContext::kApprox
_ScratchTexMatch); | 688 GrTexture* texture = context->refScratchTexture(desc, GrContext::kApprox
_ScratchTexMatch); |
| 684 if (NULL == texture) { | 689 if (NULL == texture) { |
| 685 return false; | 690 return false; |
| 686 } | 691 } |
| 687 apply_morphology_pass(context, texture->asRenderTarget(), srcTexture, sr
cRect, dstRect, | 692 apply_morphology_pass(context, texture->asRenderTarget(), clip, srcTextu
re, |
| 688 radius.fHeight, morphType, Gr1DKernelEffect::kY_Di
rection); | 693 srcRect, dstRect, radius.fHeight, morphType, |
| 694 Gr1DKernelEffect::kY_Direction); |
| 689 srcTexture.reset(texture); | 695 srcTexture.reset(texture); |
| 690 } | 696 } |
| 691 SkImageFilter::WrapTexture(srcTexture, rect.width(), rect.height(), dst); | 697 SkImageFilter::WrapTexture(srcTexture, rect.width(), rect.height(), dst); |
| 692 return true; | 698 return true; |
| 693 } | 699 } |
| 694 | 700 |
| 695 }; | 701 }; |
| 696 | 702 |
| 697 bool SkMorphologyImageFilter::filterImageGPUGeneric(bool dilate, | 703 bool SkMorphologyImageFilter::filterImageGPUGeneric(bool dilate, |
| 698 Proxy* proxy, | 704 Proxy* proxy, |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 742 SkBitmap* result, SkIPoint* offset) con
st { | 748 SkBitmap* result, SkIPoint* offset) con
st { |
| 743 return this->filterImageGPUGeneric(true, proxy, src, ctx, result, offset); | 749 return this->filterImageGPUGeneric(true, proxy, src, ctx, result, offset); |
| 744 } | 750 } |
| 745 | 751 |
| 746 bool SkErodeImageFilter::filterImageGPU(Proxy* proxy, const SkBitmap& src, const
Context& ctx, | 752 bool SkErodeImageFilter::filterImageGPU(Proxy* proxy, const SkBitmap& src, const
Context& ctx, |
| 747 SkBitmap* result, SkIPoint* offset) cons
t { | 753 SkBitmap* result, SkIPoint* offset) cons
t { |
| 748 return this->filterImageGPUGeneric(false, proxy, src, ctx, result, offset); | 754 return this->filterImageGPUGeneric(false, proxy, src, ctx, result, offset); |
| 749 } | 755 } |
| 750 | 756 |
| 751 #endif | 757 #endif |
| OLD | NEW |