| 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 "SkOpts.h" |
| 11 #include "SkReadBuffer.h" | 12 #include "SkReadBuffer.h" |
| 13 #include "SkRect.h" |
| 12 #include "SkWriteBuffer.h" | 14 #include "SkWriteBuffer.h" |
| 13 #include "SkRect.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 "GrDrawContext.h" |
| 18 #include "GrInvariantOutput.h" | 18 #include "GrInvariantOutput.h" |
| 19 #include "GrTexture.h" | 19 #include "GrTexture.h" |
| 20 #include "effects/Gr1DKernelEffect.h" | 20 #include "effects/Gr1DKernelEffect.h" |
| 21 #include "gl/GrGLFragmentProcessor.h" | 21 #include "gl/GrGLFragmentProcessor.h" |
| 22 #include "gl/builders/GrGLProgramBuilder.h" | 22 #include "gl/builders/GrGLProgramBuilder.h" |
| 23 #endif | 23 #endif |
| 24 | 24 |
| 25 SkMorphologyImageFilter::SkMorphologyImageFilter(int radiusX, | 25 SkMorphologyImageFilter::SkMorphologyImageFilter(int radiusX, |
| 26 int radiusY, | 26 int radiusY, |
| 27 SkImageFilter* input, | 27 SkImageFilter* input, |
| 28 const CropRect* cropRect) | 28 const CropRect* cropRect) |
| 29 : INHERITED(1, &input, cropRect), fRadius(SkISize::Make(radiusX, radiusY)) { | 29 : INHERITED(1, &input, cropRect), fRadius(SkISize::Make(radiusX, radiusY)) { |
| 30 } | 30 } |
| 31 | 31 |
| 32 void SkMorphologyImageFilter::flatten(SkWriteBuffer& buffer) const { | 32 void SkMorphologyImageFilter::flatten(SkWriteBuffer& buffer) const { |
| 33 this->INHERITED::flatten(buffer); | 33 this->INHERITED::flatten(buffer); |
| 34 buffer.writeInt(fRadius.fWidth); | 34 buffer.writeInt(fRadius.fWidth); |
| 35 buffer.writeInt(fRadius.fHeight); | 35 buffer.writeInt(fRadius.fHeight); |
| 36 } | 36 } |
| 37 | 37 |
| 38 enum MorphDirection { | |
| 39 kX, kY | |
| 40 }; | |
| 41 | |
| 42 template<MorphDirection direction> | |
| 43 static void erode(const SkPMColor* src, SkPMColor* dst, | |
| 44 int radius, int width, int height, | |
| 45 int srcStride, int dstStride) | |
| 46 { | |
| 47 const int srcStrideX = direction == kX ? 1 : srcStride; | |
| 48 const int dstStrideX = direction == kX ? 1 : dstStride; | |
| 49 const int srcStrideY = direction == kX ? srcStride : 1; | |
| 50 const int dstStrideY = direction == kX ? dstStride : 1; | |
| 51 radius = SkMin32(radius, width - 1); | |
| 52 const SkPMColor* upperSrc = src + radius * srcStrideX; | |
| 53 for (int x = 0; x < width; ++x) { | |
| 54 const SkPMColor* lp = src; | |
| 55 const SkPMColor* up = upperSrc; | |
| 56 SkPMColor* dptr = dst; | |
| 57 for (int y = 0; y < height; ++y) { | |
| 58 int minB = 255, minG = 255, minR = 255, minA = 255; | |
| 59 for (const SkPMColor* p = lp; p <= up; p += srcStrideX) { | |
| 60 int b = SkGetPackedB32(*p); | |
| 61 int g = SkGetPackedG32(*p); | |
| 62 int r = SkGetPackedR32(*p); | |
| 63 int a = SkGetPackedA32(*p); | |
| 64 if (b < minB) minB = b; | |
| 65 if (g < minG) minG = g; | |
| 66 if (r < minR) minR = r; | |
| 67 if (a < minA) minA = a; | |
| 68 } | |
| 69 *dptr = SkPackARGB32(minA, minR, minG, minB); | |
| 70 dptr += dstStrideY; | |
| 71 lp += srcStrideY; | |
| 72 up += srcStrideY; | |
| 73 } | |
| 74 if (x >= radius) src += srcStrideX; | |
| 75 if (x + radius < width - 1) upperSrc += srcStrideX; | |
| 76 dst += dstStrideX; | |
| 77 } | |
| 78 } | |
| 79 | |
| 80 template<MorphDirection direction> | |
| 81 static void dilate(const SkPMColor* src, SkPMColor* dst, | |
| 82 int radius, int width, int height, | |
| 83 int srcStride, int dstStride) | |
| 84 { | |
| 85 const int srcStrideX = direction == kX ? 1 : srcStride; | |
| 86 const int dstStrideX = direction == kX ? 1 : dstStride; | |
| 87 const int srcStrideY = direction == kX ? srcStride : 1; | |
| 88 const int dstStrideY = direction == kX ? dstStride : 1; | |
| 89 radius = SkMin32(radius, width - 1); | |
| 90 const SkPMColor* upperSrc = src + radius * srcStrideX; | |
| 91 for (int x = 0; x < width; ++x) { | |
| 92 const SkPMColor* lp = src; | |
| 93 const SkPMColor* up = upperSrc; | |
| 94 SkPMColor* dptr = dst; | |
| 95 for (int y = 0; y < height; ++y) { | |
| 96 int maxB = 0, maxG = 0, maxR = 0, maxA = 0; | |
| 97 for (const SkPMColor* p = lp; p <= up; p += srcStrideX) { | |
| 98 int b = SkGetPackedB32(*p); | |
| 99 int g = SkGetPackedG32(*p); | |
| 100 int r = SkGetPackedR32(*p); | |
| 101 int a = SkGetPackedA32(*p); | |
| 102 if (b > maxB) maxB = b; | |
| 103 if (g > maxG) maxG = g; | |
| 104 if (r > maxR) maxR = r; | |
| 105 if (a > maxA) maxA = a; | |
| 106 } | |
| 107 *dptr = SkPackARGB32(maxA, maxR, maxG, maxB); | |
| 108 dptr += dstStrideY; | |
| 109 lp += srcStrideY; | |
| 110 up += srcStrideY; | |
| 111 } | |
| 112 if (x >= radius) src += srcStrideX; | |
| 113 if (x + radius < width - 1) upperSrc += srcStrideX; | |
| 114 dst += dstStrideX; | |
| 115 } | |
| 116 } | |
| 117 | |
| 118 static void callProcX(SkMorphologyImageFilter::Proc procX, const SkBitmap& src,
SkBitmap* dst, int radiusX, const SkIRect& bounds) | 38 static void callProcX(SkMorphologyImageFilter::Proc procX, const SkBitmap& src,
SkBitmap* dst, int radiusX, const SkIRect& bounds) |
| 119 { | 39 { |
| 120 procX(src.getAddr32(bounds.left(), bounds.top()), dst->getAddr32(0, 0), | 40 procX(src.getAddr32(bounds.left(), bounds.top()), dst->getAddr32(0, 0), |
| 121 radiusX, bounds.width(), bounds.height(), | 41 radiusX, bounds.width(), bounds.height(), |
| 122 src.rowBytesAsPixels(), dst->rowBytesAsPixels()); | 42 src.rowBytesAsPixels(), dst->rowBytesAsPixels()); |
| 123 } | 43 } |
| 124 | 44 |
| 125 static void callProcY(SkMorphologyImageFilter::Proc procY, const SkBitmap& src,
SkBitmap* dst, int radiusY, const SkIRect& bounds) | 45 static void callProcY(SkMorphologyImageFilter::Proc procY, const SkBitmap& src,
SkBitmap* dst, int radiusY, const SkIRect& bounds) |
| 126 { | 46 { |
| 127 procY(src.getAddr32(bounds.left(), bounds.top()), dst->getAddr32(0, 0), | 47 procY(src.getAddr32(bounds.left(), bounds.top()), dst->getAddr32(0, 0), |
| (...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 196 callProcY(procY, src, dst, height, srcBounds); | 116 callProcY(procY, src, dst, height, srcBounds); |
| 197 } | 117 } |
| 198 offset->fX = bounds.left(); | 118 offset->fX = bounds.left(); |
| 199 offset->fY = bounds.top(); | 119 offset->fY = bounds.top(); |
| 200 return true; | 120 return true; |
| 201 } | 121 } |
| 202 | 122 |
| 203 bool SkErodeImageFilter::onFilterImage(Proxy* proxy, | 123 bool SkErodeImageFilter::onFilterImage(Proxy* proxy, |
| 204 const SkBitmap& source, const Context& ct
x, | 124 const SkBitmap& source, const Context& ct
x, |
| 205 SkBitmap* dst, SkIPoint* offset) const { | 125 SkBitmap* dst, SkIPoint* offset) const { |
| 206 Proc erodeXProc = SkMorphologyGetPlatformProc(kErodeX_SkMorphologyProcType); | 126 return this->filterImageGeneric(SkOpts::erode_x, SkOpts::erode_y, |
| 207 if (!erodeXProc) { | 127 proxy, source, ctx, dst, offset); |
| 208 erodeXProc = erode<kX>; | |
| 209 } | |
| 210 Proc erodeYProc = SkMorphologyGetPlatformProc(kErodeY_SkMorphologyProcType); | |
| 211 if (!erodeYProc) { | |
| 212 erodeYProc = erode<kY>; | |
| 213 } | |
| 214 return this->filterImageGeneric(erodeXProc, erodeYProc, proxy, source, ctx,
dst, offset); | |
| 215 } | 128 } |
| 216 | 129 |
| 217 bool SkDilateImageFilter::onFilterImage(Proxy* proxy, | 130 bool SkDilateImageFilter::onFilterImage(Proxy* proxy, |
| 218 const SkBitmap& source, const Context& c
tx, | 131 const SkBitmap& source, const Context& c
tx, |
| 219 SkBitmap* dst, SkIPoint* offset) const { | 132 SkBitmap* dst, SkIPoint* offset) const { |
| 220 Proc dilateXProc = SkMorphologyGetPlatformProc(kDilateX_SkMorphologyProcType
); | 133 return this->filterImageGeneric(SkOpts::dilate_x, SkOpts::dilate_y, |
| 221 if (!dilateXProc) { | 134 proxy, source, ctx, dst, offset); |
| 222 dilateXProc = dilate<kX>; | |
| 223 } | |
| 224 Proc dilateYProc = SkMorphologyGetPlatformProc(kDilateY_SkMorphologyProcType
); | |
| 225 if (!dilateYProc) { | |
| 226 dilateYProc = dilate<kY>; | |
| 227 } | |
| 228 return this->filterImageGeneric(dilateXProc, dilateYProc, proxy, source, ctx
, dst, offset); | |
| 229 } | 135 } |
| 230 | 136 |
| 231 void SkMorphologyImageFilter::computeFastBounds(const SkRect& src, SkRect* dst)
const { | 137 void SkMorphologyImageFilter::computeFastBounds(const SkRect& src, SkRect* dst)
const { |
| 232 if (this->getInput(0)) { | 138 if (this->getInput(0)) { |
| 233 this->getInput(0)->computeFastBounds(src, dst); | 139 this->getInput(0)->computeFastBounds(src, dst); |
| 234 } else { | 140 } else { |
| 235 *dst = src; | 141 *dst = src; |
| 236 } | 142 } |
| 237 dst->outset(SkIntToScalar(fRadius.width()), SkIntToScalar(fRadius.height()))
; | 143 dst->outset(SkIntToScalar(fRadius.width()), SkIntToScalar(fRadius.height()))
; |
| 238 } | 144 } |
| (...skipping 468 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 707 }; | 613 }; |
| 708 | 614 |
| 709 bool SkMorphologyImageFilter::filterImageGPUGeneric(bool dilate, | 615 bool SkMorphologyImageFilter::filterImageGPUGeneric(bool dilate, |
| 710 Proxy* proxy, | 616 Proxy* proxy, |
| 711 const SkBitmap& src, | 617 const SkBitmap& src, |
| 712 const Context& ctx, | 618 const Context& ctx, |
| 713 SkBitmap* result, | 619 SkBitmap* result, |
| 714 SkIPoint* offset) const { | 620 SkIPoint* offset) const { |
| 715 SkBitmap input = src; | 621 SkBitmap input = src; |
| 716 SkIPoint srcOffset = SkIPoint::Make(0, 0); | 622 SkIPoint srcOffset = SkIPoint::Make(0, 0); |
| 717 if (this->getInput(0) && | 623 if (this->getInput(0) && |
| 718 !this->getInput(0)->getInputResultGPU(proxy, src, ctx, &input, &srcOffse
t)) { | 624 !this->getInput(0)->getInputResultGPU(proxy, src, ctx, &input, &srcOffse
t)) { |
| 719 return false; | 625 return false; |
| 720 } | 626 } |
| 721 SkIRect bounds; | 627 SkIRect bounds; |
| 722 if (!this->applyCropRect(ctx, proxy, input, &srcOffset, &bounds, &input)) { | 628 if (!this->applyCropRect(ctx, proxy, input, &srcOffset, &bounds, &input)) { |
| 723 return false; | 629 return false; |
| 724 } | 630 } |
| 725 SkVector radius = SkVector::Make(SkIntToScalar(this->radius().width()), | 631 SkVector radius = SkVector::Make(SkIntToScalar(this->radius().width()), |
| 726 SkIntToScalar(this->radius().height())); | 632 SkIntToScalar(this->radius().height())); |
| 727 ctx.ctm().mapVectors(&radius, 1); | 633 ctx.ctm().mapVectors(&radius, 1); |
| (...skipping 27 matching lines...) Expand all Loading... |
| 755 SkBitmap* result, SkIPoint* offset) con
st { | 661 SkBitmap* result, SkIPoint* offset) con
st { |
| 756 return this->filterImageGPUGeneric(true, proxy, src, ctx, result, offset); | 662 return this->filterImageGPUGeneric(true, proxy, src, ctx, result, offset); |
| 757 } | 663 } |
| 758 | 664 |
| 759 bool SkErodeImageFilter::filterImageGPU(Proxy* proxy, const SkBitmap& src, const
Context& ctx, | 665 bool SkErodeImageFilter::filterImageGPU(Proxy* proxy, const SkBitmap& src, const
Context& ctx, |
| 760 SkBitmap* result, SkIPoint* offset) cons
t { | 666 SkBitmap* result, SkIPoint* offset) cons
t { |
| 761 return this->filterImageGPUGeneric(false, proxy, src, ctx, result, offset); | 667 return this->filterImageGPUGeneric(false, proxy, src, ctx, result, offset); |
| 762 } | 668 } |
| 763 | 669 |
| 764 #endif | 670 #endif |
| OLD | NEW |