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 544 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
555 MorphologyType type = random->nextBool() ? GrMorphologyEffect::kErode_Morpho
logyType : | 555 MorphologyType type = random->nextBool() ? GrMorphologyEffect::kErode_Morpho
logyType : |
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 GrTexture* texture, | 566 GrTexture* texture, |
566 const SkIRect& srcRect, | 567 const SkIRect& srcRect, |
567 const SkIRect& dstRect, | 568 const SkIRect& dstRect, |
568 int radius, | 569 int radius, |
569 GrMorphologyEffect::MorphologyType morphType, | 570 GrMorphologyEffect::MorphologyType morphType, |
570 float bounds[2], | 571 float bounds[2], |
571 Gr1DKernelEffect::Direction direction) { | 572 Gr1DKernelEffect::Direction direction) { |
572 GrPaint paint; | 573 GrPaint paint; |
573 paint.addColorProcessor(GrMorphologyEffect::Create(texture, | 574 paint.addColorProcessor(GrMorphologyEffect::Create(texture, |
574 direction, | 575 direction, |
575 radius, | 576 radius, |
576 morphType, | 577 morphType, |
577 bounds))->unref(); | 578 bounds))->unref(); |
578 context->drawNonAARectToRect(paint, SkMatrix::I(), SkRect::Make(dstRect), | 579 context->drawNonAARectToRect(rt, paint, SkMatrix::I(), SkRect::Make(dstRect)
, |
579 SkRect::Make(srcRect)); | 580 SkRect::Make(srcRect)); |
580 } | 581 } |
581 | 582 |
582 void apply_morphology_rect_no_bounds(GrContext* context, | 583 void apply_morphology_rect_no_bounds(GrContext* context, |
583 GrTexture* texture, | 584 GrRenderTarget* rt, |
584 const SkIRect& srcRect, | 585 GrTexture* texture, |
585 const SkIRect& dstRect, | 586 const SkIRect& srcRect, |
586 int radius, | 587 const SkIRect& dstRect, |
587 GrMorphologyEffect::MorphologyType morphType, | 588 int radius, |
588 Gr1DKernelEffect::Direction direction) { | 589 GrMorphologyEffect::MorphologyType morphTyp
e, |
| 590 Gr1DKernelEffect::Direction direction) { |
589 GrPaint paint; | 591 GrPaint paint; |
590 paint.addColorProcessor(GrMorphologyEffect::Create(texture, | 592 paint.addColorProcessor(GrMorphologyEffect::Create(texture, |
591 direction, | 593 direction, |
592 radius, | 594 radius, |
593 morphType))->unref(); | 595 morphType))->unref(); |
594 context->drawNonAARectToRect(paint, SkMatrix::I(), SkRect::Make(dstRect), | 596 context->drawNonAARectToRect(rt, paint, SkMatrix::I(), SkRect::Make(dstRect)
, |
595 SkRect::Make(srcRect)); | 597 SkRect::Make(srcRect)); |
596 } | 598 } |
597 | 599 |
598 void apply_morphology_pass(GrContext* context, | 600 void apply_morphology_pass(GrContext* context, |
| 601 GrRenderTarget* rt, |
599 GrTexture* texture, | 602 GrTexture* texture, |
600 const SkIRect& srcRect, | 603 const SkIRect& srcRect, |
601 const SkIRect& dstRect, | 604 const SkIRect& dstRect, |
602 int radius, | 605 int radius, |
603 GrMorphologyEffect::MorphologyType morphType, | 606 GrMorphologyEffect::MorphologyType morphType, |
604 Gr1DKernelEffect::Direction direction) { | 607 Gr1DKernelEffect::Direction direction) { |
605 float bounds[2] = { 0.0f, 1.0f }; | 608 float bounds[2] = { 0.0f, 1.0f }; |
606 SkIRect lowerSrcRect = srcRect, lowerDstRect = dstRect; | 609 SkIRect lowerSrcRect = srcRect, lowerDstRect = dstRect; |
607 SkIRect middleSrcRect = srcRect, middleDstRect = dstRect; | 610 SkIRect middleSrcRect = srcRect, middleDstRect = dstRect; |
608 SkIRect upperSrcRect = srcRect, upperDstRect = dstRect; | 611 SkIRect upperSrcRect = srcRect, upperDstRect = dstRect; |
(...skipping 11 matching lines...) Expand all Loading... |
620 bounds[1] = (SkIntToScalar(srcRect.bottom()) - 0.5f) / texture->height()
; | 623 bounds[1] = (SkIntToScalar(srcRect.bottom()) - 0.5f) / texture->height()
; |
621 lowerSrcRect.fBottom = srcRect.top() + radius; | 624 lowerSrcRect.fBottom = srcRect.top() + radius; |
622 lowerDstRect.fBottom = dstRect.top() + radius; | 625 lowerDstRect.fBottom = dstRect.top() + radius; |
623 upperSrcRect.fTop = srcRect.bottom() - radius; | 626 upperSrcRect.fTop = srcRect.bottom() - radius; |
624 upperDstRect.fTop = dstRect.bottom() - radius; | 627 upperDstRect.fTop = dstRect.bottom() - radius; |
625 middleSrcRect.inset(0, radius); | 628 middleSrcRect.inset(0, radius); |
626 middleDstRect.inset(0, radius); | 629 middleDstRect.inset(0, radius); |
627 } | 630 } |
628 if (middleSrcRect.fLeft - middleSrcRect.fRight >= 0) { | 631 if (middleSrcRect.fLeft - middleSrcRect.fRight >= 0) { |
629 // radius covers srcRect; use bounds over entire draw | 632 // radius covers srcRect; use bounds over entire draw |
630 apply_morphology_rect(context, texture, srcRect, dstRect, radius, | 633 apply_morphology_rect(context, rt, texture, srcRect, dstRect, radius, |
631 morphType, bounds, direction); | 634 morphType, bounds, direction); |
632 } else { | 635 } else { |
633 // Draw upper and lower margins with bounds; middle without. | 636 // Draw upper and lower margins with bounds; middle without. |
634 apply_morphology_rect(context, texture, lowerSrcRect, lowerDstRect, radi
us, | 637 apply_morphology_rect(context, rt,texture, lowerSrcRect, lowerDstRect, r
adius, |
635 morphType, bounds, direction); | 638 morphType, bounds, direction); |
636 apply_morphology_rect(context, texture, upperSrcRect, upperDstRect, radi
us, | 639 apply_morphology_rect(context, rt, texture, upperSrcRect, upperDstRect,
radius, |
637 morphType, bounds, direction); | 640 morphType, bounds, direction); |
638 apply_morphology_rect_no_bounds(context, texture, middleSrcRect, middleD
stRect, radius, | 641 apply_morphology_rect_no_bounds(context, rt, texture, middleSrcRect, mid
dleDstRect, radius, |
639 morphType, direction); | 642 morphType, direction); |
640 } | 643 } |
641 } | 644 } |
642 | 645 |
643 bool apply_morphology(const SkBitmap& input, | 646 bool apply_morphology(const SkBitmap& input, |
644 const SkIRect& rect, | 647 const SkIRect& rect, |
645 GrMorphologyEffect::MorphologyType morphType, | 648 GrMorphologyEffect::MorphologyType morphType, |
646 SkISize radius, | 649 SkISize radius, |
647 SkBitmap* dst) { | 650 SkBitmap* dst) { |
648 SkAutoTUnref<GrTexture> srcTexture(SkRef(input.getTexture())); | 651 SkAutoTUnref<GrTexture> srcTexture(SkRef(input.getTexture())); |
649 SkASSERT(srcTexture); | 652 SkASSERT(srcTexture); |
650 GrContext* context = srcTexture->getContext(); | 653 GrContext* context = srcTexture->getContext(); |
651 | 654 |
652 GrContext::AutoClip acs(context, SkRect::MakeWH(SkIntToScalar(srcTexture->wi
dth()), | 655 GrContext::AutoClip acs(context, SkRect::MakeWH(SkIntToScalar(srcTexture->wi
dth()), |
653 SkIntToScalar(srcTexture->he
ight()))); | 656 SkIntToScalar(srcTexture->he
ight()))); |
654 | 657 |
655 SkIRect dstRect = SkIRect::MakeWH(rect.width(), rect.height()); | 658 SkIRect dstRect = SkIRect::MakeWH(rect.width(), rect.height()); |
656 GrSurfaceDesc desc; | 659 GrSurfaceDesc desc; |
657 desc.fFlags = kRenderTarget_GrSurfaceFlag | kNoStencil_GrSurfaceFlag; | 660 desc.fFlags = kRenderTarget_GrSurfaceFlag | kNoStencil_GrSurfaceFlag; |
658 desc.fWidth = rect.width(); | 661 desc.fWidth = rect.width(); |
659 desc.fHeight = rect.height(); | 662 desc.fHeight = rect.height(); |
660 desc.fConfig = kSkia8888_GrPixelConfig; | 663 desc.fConfig = kSkia8888_GrPixelConfig; |
661 SkIRect srcRect = rect; | 664 SkIRect srcRect = rect; |
662 | 665 |
663 if (radius.fWidth > 0) { | 666 if (radius.fWidth > 0) { |
664 GrTexture* texture = context->refScratchTexture(desc, GrContext::kApprox
_ScratchTexMatch); | 667 GrTexture* texture = context->refScratchTexture(desc, GrContext::kApprox
_ScratchTexMatch); |
665 if (NULL == texture) { | 668 if (NULL == texture) { |
666 return false; | 669 return false; |
667 } | 670 } |
668 GrContext::AutoRenderTarget art(context, texture->asRenderTarget()); | 671 apply_morphology_pass(context, texture->asRenderTarget(), srcTexture, sr
cRect, dstRect, |
669 apply_morphology_pass(context, srcTexture, srcRect, dstRect, radius.fWid
th, | 672 radius.fWidth, morphType, Gr1DKernelEffect::kX_Dir
ection); |
670 morphType, Gr1DKernelEffect::kX_Direction); | |
671 SkIRect clearRect = SkIRect::MakeXYWH(dstRect.fLeft, dstRect.fBottom, | 673 SkIRect clearRect = SkIRect::MakeXYWH(dstRect.fLeft, dstRect.fBottom, |
672 dstRect.width(), radius.fHeight); | 674 dstRect.width(), radius.fHeight); |
673 GrColor clearColor = GrMorphologyEffect::kErode_MorphologyType == morphT
ype ? | 675 GrColor clearColor = GrMorphologyEffect::kErode_MorphologyType == morphT
ype ? |
674 SK_ColorWHITE : | 676 SK_ColorWHITE : |
675 SK_ColorTRANSPARENT; | 677 SK_ColorTRANSPARENT; |
676 context->clear(&clearRect, clearColor, false, texture->asRenderTarget())
; | 678 context->clear(&clearRect, clearColor, false, texture->asRenderTarget())
; |
677 srcTexture.reset(texture); | 679 srcTexture.reset(texture); |
678 srcRect = dstRect; | 680 srcRect = dstRect; |
679 } | 681 } |
680 if (radius.fHeight > 0) { | 682 if (radius.fHeight > 0) { |
681 GrTexture* texture = context->refScratchTexture(desc, GrContext::kApprox
_ScratchTexMatch); | 683 GrTexture* texture = context->refScratchTexture(desc, GrContext::kApprox
_ScratchTexMatch); |
682 if (NULL == texture) { | 684 if (NULL == texture) { |
683 return false; | 685 return false; |
684 } | 686 } |
685 GrContext::AutoRenderTarget art(context, texture->asRenderTarget()); | 687 apply_morphology_pass(context, texture->asRenderTarget(), srcTexture, sr
cRect, dstRect, |
686 apply_morphology_pass(context, srcTexture, srcRect, dstRect, radius.fHei
ght, | 688 radius.fHeight, morphType, Gr1DKernelEffect::kY_Di
rection); |
687 morphType, Gr1DKernelEffect::kY_Direction); | |
688 srcTexture.reset(texture); | 689 srcTexture.reset(texture); |
689 } | 690 } |
690 SkImageFilter::WrapTexture(srcTexture, rect.width(), rect.height(), dst); | 691 SkImageFilter::WrapTexture(srcTexture, rect.width(), rect.height(), dst); |
691 return true; | 692 return true; |
692 } | 693 } |
693 | 694 |
694 }; | 695 }; |
695 | 696 |
696 bool SkMorphologyImageFilter::filterImageGPUGeneric(bool dilate, | 697 bool SkMorphologyImageFilter::filterImageGPUGeneric(bool dilate, |
697 Proxy* proxy, | 698 Proxy* proxy, |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
741 SkBitmap* result, SkIPoint* offset) con
st { | 742 SkBitmap* result, SkIPoint* offset) con
st { |
742 return this->filterImageGPUGeneric(true, proxy, src, ctx, result, offset); | 743 return this->filterImageGPUGeneric(true, proxy, src, ctx, result, offset); |
743 } | 744 } |
744 | 745 |
745 bool SkErodeImageFilter::filterImageGPU(Proxy* proxy, const SkBitmap& src, const
Context& ctx, | 746 bool SkErodeImageFilter::filterImageGPU(Proxy* proxy, const SkBitmap& src, const
Context& ctx, |
746 SkBitmap* result, SkIPoint* offset) cons
t { | 747 SkBitmap* result, SkIPoint* offset) cons
t { |
747 return this->filterImageGPUGeneric(false, proxy, src, ctx, result, offset); | 748 return this->filterImageGPUGeneric(false, proxy, src, ctx, result, offset); |
748 } | 749 } |
749 | 750 |
750 #endif | 751 #endif |
OLD | NEW |