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