senorblanco@chromium.org | 05054f1 | 2012-03-02 21:05:45 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2012 The Android Open Source Project |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
| 7 | |
| 8 | #include "SkMorphologyImageFilter.h" |
djsollen@google.com | 64a0ec3 | 2012-06-12 15:17:27 +0000 | [diff] [blame] | 9 | #include "SkBitmap.h" |
senorblanco@chromium.org | 05054f1 | 2012-03-02 21:05:45 +0000 | [diff] [blame] | 10 | #include "SkColorPriv.h" |
djsollen@google.com | c73dd5c | 2012-08-07 15:54:32 +0000 | [diff] [blame] | 11 | #include "SkFlattenableBuffers.h" |
| 12 | #include "SkRect.h" |
senorblanco@chromium.org | 7a47ad3 | 2013-10-30 21:57:04 +0000 | [diff] [blame] | 13 | #include "SkMorphology_opts.h" |
bsalomon@google.com | cf8fb1f | 2012-08-02 14:03:32 +0000 | [diff] [blame] | 14 | #if SK_SUPPORT_GPU |
senorblanco@chromium.org | 302cffb | 2012-08-01 20:16:34 +0000 | [diff] [blame] | 15 | #include "GrContext.h" |
| 16 | #include "GrTexture.h" |
bsalomon@google.com | 2eaaefd | 2012-10-29 19:51:22 +0000 | [diff] [blame] | 17 | #include "GrTBackendEffectFactory.h" |
bsalomon@google.com | d698f77 | 2012-10-25 13:22:00 +0000 | [diff] [blame] | 18 | #include "gl/GrGLEffect.h" |
senorblanco@chromium.org | 84207c4 | 2012-08-22 20:51:19 +0000 | [diff] [blame] | 19 | #include "effects/Gr1DKernelEffect.h" |
senorblanco@chromium.org | c2594f4 | 2013-01-30 19:08:47 +0000 | [diff] [blame] | 20 | #include "SkImageFilterUtils.h" |
bsalomon@google.com | cf8fb1f | 2012-08-02 14:03:32 +0000 | [diff] [blame] | 21 | #endif |
senorblanco@chromium.org | 05054f1 | 2012-03-02 21:05:45 +0000 | [diff] [blame] | 22 | |
| 23 | SkMorphologyImageFilter::SkMorphologyImageFilter(SkFlattenableReadBuffer& buffer) |
commit-bot@chromium.org | ce33d60 | 2013-11-25 21:46:31 +0000 | [diff] [blame] | 24 | : INHERITED(1, buffer) { |
tomhudson@google.com | 7558925 | 2012-04-10 17:42:21 +0000 | [diff] [blame] | 25 | fRadius.fWidth = buffer.readInt(); |
| 26 | fRadius.fHeight = buffer.readInt(); |
commit-bot@chromium.org | c0b7e10 | 2013-10-23 17:06:21 +0000 | [diff] [blame] | 27 | buffer.validate((fRadius.fWidth >= 0) && |
| 28 | (fRadius.fHeight >= 0)); |
senorblanco@chromium.org | 05054f1 | 2012-03-02 21:05:45 +0000 | [diff] [blame] | 29 | } |
| 30 | |
senorblanco@chromium.org | b295fb6 | 2013-10-10 13:51:19 +0000 | [diff] [blame] | 31 | SkMorphologyImageFilter::SkMorphologyImageFilter(int radiusX, int radiusY, SkImageFilter* input, const CropRect* cropRect) |
senorblanco@chromium.org | 8fcad98 | 2013-09-17 13:41:43 +0000 | [diff] [blame] | 32 | : INHERITED(input, cropRect), fRadius(SkISize::Make(radiusX, radiusY)) { |
senorblanco@chromium.org | 05054f1 | 2012-03-02 21:05:45 +0000 | [diff] [blame] | 33 | } |
| 34 | |
| 35 | |
djsollen@google.com | 5492424 | 2012-03-29 15:18:04 +0000 | [diff] [blame] | 36 | void SkMorphologyImageFilter::flatten(SkFlattenableWriteBuffer& buffer) const { |
senorblanco@chromium.org | 05054f1 | 2012-03-02 21:05:45 +0000 | [diff] [blame] | 37 | this->INHERITED::flatten(buffer); |
tomhudson@google.com | 7558925 | 2012-04-10 17:42:21 +0000 | [diff] [blame] | 38 | buffer.writeInt(fRadius.fWidth); |
| 39 | buffer.writeInt(fRadius.fHeight); |
senorblanco@chromium.org | 05054f1 | 2012-03-02 21:05:45 +0000 | [diff] [blame] | 40 | } |
| 41 | |
senorblanco@chromium.org | 7a47ad3 | 2013-10-30 21:57:04 +0000 | [diff] [blame] | 42 | enum MorphDirection { |
| 43 | kX, kY |
| 44 | }; |
| 45 | |
| 46 | template<MorphDirection direction> |
senorblanco@chromium.org | 05054f1 | 2012-03-02 21:05:45 +0000 | [diff] [blame] | 47 | static void erode(const SkPMColor* src, SkPMColor* dst, |
| 48 | int radius, int width, int height, |
senorblanco@chromium.org | 7a47ad3 | 2013-10-30 21:57:04 +0000 | [diff] [blame] | 49 | int srcStride, int dstStride) |
senorblanco@chromium.org | 05054f1 | 2012-03-02 21:05:45 +0000 | [diff] [blame] | 50 | { |
senorblanco@chromium.org | 7a47ad3 | 2013-10-30 21:57:04 +0000 | [diff] [blame] | 51 | const int srcStrideX = direction == kX ? 1 : srcStride; |
| 52 | const int dstStrideX = direction == kX ? 1 : dstStride; |
| 53 | const int srcStrideY = direction == kX ? srcStride : 1; |
| 54 | const int dstStrideY = direction == kX ? dstStride : 1; |
senorblanco@chromium.org | 56dd630 | 2012-04-10 17:25:44 +0000 | [diff] [blame] | 55 | radius = SkMin32(radius, width - 1); |
| 56 | const SkPMColor* upperSrc = src + radius * srcStrideX; |
senorblanco@chromium.org | 05054f1 | 2012-03-02 21:05:45 +0000 | [diff] [blame] | 57 | for (int x = 0; x < width; ++x) { |
| 58 | const SkPMColor* lp = src; |
| 59 | const SkPMColor* up = upperSrc; |
| 60 | SkPMColor* dptr = dst; |
| 61 | for (int y = 0; y < height; ++y) { |
| 62 | int minB = 255, minG = 255, minR = 255, minA = 255; |
| 63 | for (const SkPMColor* p = lp; p <= up; p += srcStrideX) { |
| 64 | int b = SkGetPackedB32(*p); |
| 65 | int g = SkGetPackedG32(*p); |
| 66 | int r = SkGetPackedR32(*p); |
| 67 | int a = SkGetPackedA32(*p); |
| 68 | if (b < minB) minB = b; |
| 69 | if (g < minG) minG = g; |
| 70 | if (r < minR) minR = r; |
| 71 | if (a < minA) minA = a; |
| 72 | } |
| 73 | *dptr = SkPackARGB32(minA, minR, minG, minB); |
| 74 | dptr += dstStrideY; |
| 75 | lp += srcStrideY; |
| 76 | up += srcStrideY; |
| 77 | } |
| 78 | if (x >= radius) src += srcStrideX; |
| 79 | if (x + radius < width - 1) upperSrc += srcStrideX; |
| 80 | dst += dstStrideX; |
| 81 | } |
| 82 | } |
| 83 | |
senorblanco@chromium.org | 8fcad98 | 2013-09-17 13:41:43 +0000 | [diff] [blame] | 84 | static void erodeX(const SkBitmap& src, SkBitmap* dst, int radiusX, const SkIRect& bounds) |
senorblanco@chromium.org | 05054f1 | 2012-03-02 21:05:45 +0000 | [diff] [blame] | 85 | { |
senorblanco@chromium.org | 7a47ad3 | 2013-10-30 21:57:04 +0000 | [diff] [blame] | 86 | SkMorphologyProc erodeXProc = SkMorphologyGetPlatformProc(kErodeX_SkMorphologyProcType); |
| 87 | if (!erodeXProc) { |
| 88 | erodeXProc = erode<kX>; |
| 89 | } |
| 90 | erodeXProc(src.getAddr32(bounds.left(), bounds.top()), dst->getAddr32(0, 0), |
| 91 | radiusX, bounds.width(), bounds.height(), |
| 92 | src.rowBytesAsPixels(), dst->rowBytesAsPixels()); |
senorblanco@chromium.org | 05054f1 | 2012-03-02 21:05:45 +0000 | [diff] [blame] | 93 | } |
| 94 | |
senorblanco@chromium.org | 8fcad98 | 2013-09-17 13:41:43 +0000 | [diff] [blame] | 95 | static void erodeY(const SkBitmap& src, SkBitmap* dst, int radiusY, const SkIRect& bounds) |
senorblanco@chromium.org | 05054f1 | 2012-03-02 21:05:45 +0000 | [diff] [blame] | 96 | { |
senorblanco@chromium.org | 7a47ad3 | 2013-10-30 21:57:04 +0000 | [diff] [blame] | 97 | SkMorphologyProc erodeYProc = SkMorphologyGetPlatformProc(kErodeY_SkMorphologyProcType); |
| 98 | if (!erodeYProc) { |
| 99 | erodeYProc = erode<kY>; |
| 100 | } |
| 101 | erodeYProc(src.getAddr32(bounds.left(), bounds.top()), dst->getAddr32(0, 0), |
| 102 | radiusY, bounds.height(), bounds.width(), |
| 103 | src.rowBytesAsPixels(), dst->rowBytesAsPixels()); |
senorblanco@chromium.org | 05054f1 | 2012-03-02 21:05:45 +0000 | [diff] [blame] | 104 | } |
| 105 | |
senorblanco@chromium.org | 7a47ad3 | 2013-10-30 21:57:04 +0000 | [diff] [blame] | 106 | template<MorphDirection direction> |
senorblanco@chromium.org | 05054f1 | 2012-03-02 21:05:45 +0000 | [diff] [blame] | 107 | static void dilate(const SkPMColor* src, SkPMColor* dst, |
| 108 | int radius, int width, int height, |
senorblanco@chromium.org | 7a47ad3 | 2013-10-30 21:57:04 +0000 | [diff] [blame] | 109 | int srcStride, int dstStride) |
senorblanco@chromium.org | 05054f1 | 2012-03-02 21:05:45 +0000 | [diff] [blame] | 110 | { |
senorblanco@chromium.org | 7a47ad3 | 2013-10-30 21:57:04 +0000 | [diff] [blame] | 111 | const int srcStrideX = direction == kX ? 1 : srcStride; |
| 112 | const int dstStrideX = direction == kX ? 1 : dstStride; |
| 113 | const int srcStrideY = direction == kX ? srcStride : 1; |
| 114 | const int dstStrideY = direction == kX ? dstStride : 1; |
senorblanco@chromium.org | 56dd630 | 2012-04-10 17:25:44 +0000 | [diff] [blame] | 115 | radius = SkMin32(radius, width - 1); |
| 116 | const SkPMColor* upperSrc = src + radius * srcStrideX; |
senorblanco@chromium.org | 05054f1 | 2012-03-02 21:05:45 +0000 | [diff] [blame] | 117 | for (int x = 0; x < width; ++x) { |
| 118 | const SkPMColor* lp = src; |
| 119 | const SkPMColor* up = upperSrc; |
| 120 | SkPMColor* dptr = dst; |
| 121 | for (int y = 0; y < height; ++y) { |
| 122 | int maxB = 0, maxG = 0, maxR = 0, maxA = 0; |
| 123 | for (const SkPMColor* p = lp; p <= up; p += srcStrideX) { |
| 124 | int b = SkGetPackedB32(*p); |
| 125 | int g = SkGetPackedG32(*p); |
| 126 | int r = SkGetPackedR32(*p); |
| 127 | int a = SkGetPackedA32(*p); |
| 128 | if (b > maxB) maxB = b; |
| 129 | if (g > maxG) maxG = g; |
| 130 | if (r > maxR) maxR = r; |
| 131 | if (a > maxA) maxA = a; |
| 132 | } |
| 133 | *dptr = SkPackARGB32(maxA, maxR, maxG, maxB); |
| 134 | dptr += dstStrideY; |
| 135 | lp += srcStrideY; |
| 136 | up += srcStrideY; |
| 137 | } |
| 138 | if (x >= radius) src += srcStrideX; |
| 139 | if (x + radius < width - 1) upperSrc += srcStrideX; |
| 140 | dst += dstStrideX; |
| 141 | } |
| 142 | } |
| 143 | |
senorblanco@chromium.org | 8fcad98 | 2013-09-17 13:41:43 +0000 | [diff] [blame] | 144 | static void dilateX(const SkBitmap& src, SkBitmap* dst, int radiusX, const SkIRect& bounds) |
senorblanco@chromium.org | 05054f1 | 2012-03-02 21:05:45 +0000 | [diff] [blame] | 145 | { |
senorblanco@chromium.org | 7a47ad3 | 2013-10-30 21:57:04 +0000 | [diff] [blame] | 146 | SkMorphologyProc dilateXProc = SkMorphologyGetPlatformProc(kDilateX_SkMorphologyProcType); |
| 147 | if (!dilateXProc) { |
| 148 | dilateXProc = dilate<kX>; |
| 149 | } |
| 150 | dilateXProc(src.getAddr32(bounds.left(), bounds.top()), dst->getAddr32(0, 0), |
| 151 | radiusX, bounds.width(), bounds.height(), |
| 152 | src.rowBytesAsPixels(), dst->rowBytesAsPixels()); |
senorblanco@chromium.org | 05054f1 | 2012-03-02 21:05:45 +0000 | [diff] [blame] | 153 | } |
| 154 | |
senorblanco@chromium.org | 8fcad98 | 2013-09-17 13:41:43 +0000 | [diff] [blame] | 155 | static void dilateY(const SkBitmap& src, SkBitmap* dst, int radiusY, const SkIRect& bounds) |
senorblanco@chromium.org | 05054f1 | 2012-03-02 21:05:45 +0000 | [diff] [blame] | 156 | { |
senorblanco@chromium.org | 7a47ad3 | 2013-10-30 21:57:04 +0000 | [diff] [blame] | 157 | SkMorphologyProc dilateYProc = SkMorphologyGetPlatformProc(kDilateY_SkMorphologyProcType); |
| 158 | if (!dilateYProc) { |
| 159 | dilateYProc = dilate<kY>; |
| 160 | } |
| 161 | dilateYProc(src.getAddr32(bounds.left(), bounds.top()), dst->getAddr32(0, 0), |
| 162 | radiusY, bounds.height(), bounds.width(), |
| 163 | src.rowBytesAsPixels(), dst->rowBytesAsPixels()); |
senorblanco@chromium.org | 05054f1 | 2012-03-02 21:05:45 +0000 | [diff] [blame] | 164 | } |
| 165 | |
senorblanco@chromium.org | f1369ce | 2012-08-20 14:53:21 +0000 | [diff] [blame] | 166 | bool SkErodeImageFilter::onFilterImage(Proxy* proxy, |
| 167 | const SkBitmap& source, const SkMatrix& ctm, |
| 168 | SkBitmap* dst, SkIPoint* offset) { |
senorblanco@chromium.org | 6840076 | 2013-05-24 15:04:07 +0000 | [diff] [blame] | 169 | SkBitmap src = source; |
senorblanco@chromium.org | 6776b82 | 2014-01-03 21:48:22 +0000 | [diff] [blame] | 170 | SkIPoint srcOffset = SkIPoint::Make(0, 0); |
| 171 | if (getInput(0) && !getInput(0)->filterImage(proxy, source, ctm, &src, &srcOffset)) { |
senorblanco@chromium.org | 6840076 | 2013-05-24 15:04:07 +0000 | [diff] [blame] | 172 | return false; |
| 173 | } |
| 174 | |
senorblanco@chromium.org | 05054f1 | 2012-03-02 21:05:45 +0000 | [diff] [blame] | 175 | if (src.config() != SkBitmap::kARGB_8888_Config) { |
| 176 | return false; |
| 177 | } |
| 178 | |
senorblanco@chromium.org | 8fcad98 | 2013-09-17 13:41:43 +0000 | [diff] [blame] | 179 | SkIRect bounds; |
| 180 | src.getBounds(&bounds); |
senorblanco@chromium.org | 6776b82 | 2014-01-03 21:48:22 +0000 | [diff] [blame] | 181 | bounds.offset(srcOffset); |
senorblanco@chromium.org | 8fcad98 | 2013-09-17 13:41:43 +0000 | [diff] [blame] | 182 | if (!this->applyCropRect(&bounds, ctm)) { |
| 183 | return false; |
| 184 | } |
| 185 | |
senorblanco@chromium.org | 05054f1 | 2012-03-02 21:05:45 +0000 | [diff] [blame] | 186 | SkAutoLockPixels alp(src); |
| 187 | if (!src.getPixels()) { |
| 188 | return false; |
| 189 | } |
| 190 | |
senorblanco@chromium.org | 8fcad98 | 2013-09-17 13:41:43 +0000 | [diff] [blame] | 191 | dst->setConfig(src.config(), bounds.width(), bounds.height()); |
senorblanco@chromium.org | 05054f1 | 2012-03-02 21:05:45 +0000 | [diff] [blame] | 192 | dst->allocPixels(); |
commit-bot@chromium.org | cd3b15c | 2013-12-04 17:06:49 +0000 | [diff] [blame] | 193 | if (!dst->getPixels()) { |
| 194 | return false; |
| 195 | } |
senorblanco@chromium.org | 05054f1 | 2012-03-02 21:05:45 +0000 | [diff] [blame] | 196 | |
| 197 | int width = radius().width(); |
| 198 | int height = radius().height(); |
| 199 | |
| 200 | if (width < 0 || height < 0) { |
| 201 | return false; |
| 202 | } |
| 203 | |
| 204 | if (width == 0 && height == 0) { |
senorblanco@chromium.org | 8fcad98 | 2013-09-17 13:41:43 +0000 | [diff] [blame] | 205 | src.extractSubset(dst, bounds); |
senorblanco@chromium.org | 6776b82 | 2014-01-03 21:48:22 +0000 | [diff] [blame] | 206 | offset->fX = bounds.left(); |
| 207 | offset->fY = bounds.top(); |
senorblanco@chromium.org | 05054f1 | 2012-03-02 21:05:45 +0000 | [diff] [blame] | 208 | return true; |
| 209 | } |
| 210 | |
| 211 | SkBitmap temp; |
| 212 | temp.setConfig(dst->config(), dst->width(), dst->height()); |
| 213 | if (!temp.allocPixels()) { |
| 214 | return false; |
| 215 | } |
| 216 | |
senorblanco@chromium.org | 6776b82 | 2014-01-03 21:48:22 +0000 | [diff] [blame] | 217 | offset->fX = bounds.left(); |
| 218 | offset->fY = bounds.top(); |
| 219 | bounds.offset(-srcOffset); |
senorblanco@chromium.org | 05054f1 | 2012-03-02 21:05:45 +0000 | [diff] [blame] | 220 | if (width > 0 && height > 0) { |
senorblanco@chromium.org | 8fcad98 | 2013-09-17 13:41:43 +0000 | [diff] [blame] | 221 | erodeX(src, &temp, width, bounds); |
| 222 | SkIRect tmpBounds = SkIRect::MakeWH(bounds.width(), bounds.height()); |
| 223 | erodeY(temp, dst, height, tmpBounds); |
senorblanco@chromium.org | 05054f1 | 2012-03-02 21:05:45 +0000 | [diff] [blame] | 224 | } else if (width > 0) { |
senorblanco@chromium.org | 8fcad98 | 2013-09-17 13:41:43 +0000 | [diff] [blame] | 225 | erodeX(src, dst, width, bounds); |
senorblanco@chromium.org | 05054f1 | 2012-03-02 21:05:45 +0000 | [diff] [blame] | 226 | } else if (height > 0) { |
senorblanco@chromium.org | 8fcad98 | 2013-09-17 13:41:43 +0000 | [diff] [blame] | 227 | erodeY(src, dst, height, bounds); |
senorblanco@chromium.org | 05054f1 | 2012-03-02 21:05:45 +0000 | [diff] [blame] | 228 | } |
| 229 | return true; |
| 230 | } |
| 231 | |
senorblanco@chromium.org | f1369ce | 2012-08-20 14:53:21 +0000 | [diff] [blame] | 232 | bool SkDilateImageFilter::onFilterImage(Proxy* proxy, |
| 233 | const SkBitmap& source, const SkMatrix& ctm, |
| 234 | SkBitmap* dst, SkIPoint* offset) { |
senorblanco@chromium.org | 6840076 | 2013-05-24 15:04:07 +0000 | [diff] [blame] | 235 | SkBitmap src = source; |
senorblanco@chromium.org | 6776b82 | 2014-01-03 21:48:22 +0000 | [diff] [blame] | 236 | SkIPoint srcOffset = SkIPoint::Make(0, 0); |
| 237 | if (getInput(0) && !getInput(0)->filterImage(proxy, source, ctm, &src, &srcOffset)) { |
senorblanco@chromium.org | 6840076 | 2013-05-24 15:04:07 +0000 | [diff] [blame] | 238 | return false; |
| 239 | } |
senorblanco@chromium.org | 05054f1 | 2012-03-02 21:05:45 +0000 | [diff] [blame] | 240 | if (src.config() != SkBitmap::kARGB_8888_Config) { |
| 241 | return false; |
| 242 | } |
| 243 | |
senorblanco@chromium.org | 8fcad98 | 2013-09-17 13:41:43 +0000 | [diff] [blame] | 244 | SkIRect bounds; |
| 245 | src.getBounds(&bounds); |
senorblanco@chromium.org | 6776b82 | 2014-01-03 21:48:22 +0000 | [diff] [blame] | 246 | bounds.offset(srcOffset); |
senorblanco@chromium.org | 8fcad98 | 2013-09-17 13:41:43 +0000 | [diff] [blame] | 247 | if (!this->applyCropRect(&bounds, ctm)) { |
| 248 | return false; |
| 249 | } |
| 250 | |
senorblanco@chromium.org | 05054f1 | 2012-03-02 21:05:45 +0000 | [diff] [blame] | 251 | SkAutoLockPixels alp(src); |
| 252 | if (!src.getPixels()) { |
| 253 | return false; |
| 254 | } |
| 255 | |
senorblanco@chromium.org | 8fcad98 | 2013-09-17 13:41:43 +0000 | [diff] [blame] | 256 | dst->setConfig(src.config(), bounds.width(), bounds.height()); |
senorblanco@chromium.org | 05054f1 | 2012-03-02 21:05:45 +0000 | [diff] [blame] | 257 | dst->allocPixels(); |
commit-bot@chromium.org | cd3b15c | 2013-12-04 17:06:49 +0000 | [diff] [blame] | 258 | if (!dst->getPixels()) { |
| 259 | return false; |
| 260 | } |
senorblanco@chromium.org | 05054f1 | 2012-03-02 21:05:45 +0000 | [diff] [blame] | 261 | |
| 262 | int width = radius().width(); |
| 263 | int height = radius().height(); |
| 264 | |
| 265 | if (width < 0 || height < 0) { |
| 266 | return false; |
| 267 | } |
| 268 | |
| 269 | if (width == 0 && height == 0) { |
senorblanco@chromium.org | 8fcad98 | 2013-09-17 13:41:43 +0000 | [diff] [blame] | 270 | src.extractSubset(dst, bounds); |
senorblanco@chromium.org | 6776b82 | 2014-01-03 21:48:22 +0000 | [diff] [blame] | 271 | offset->fX = bounds.left(); |
| 272 | offset->fY = bounds.top(); |
senorblanco@chromium.org | 05054f1 | 2012-03-02 21:05:45 +0000 | [diff] [blame] | 273 | return true; |
| 274 | } |
| 275 | |
| 276 | SkBitmap temp; |
| 277 | temp.setConfig(dst->config(), dst->width(), dst->height()); |
| 278 | if (!temp.allocPixels()) { |
| 279 | return false; |
| 280 | } |
| 281 | |
senorblanco@chromium.org | 6776b82 | 2014-01-03 21:48:22 +0000 | [diff] [blame] | 282 | offset->fX = bounds.left(); |
| 283 | offset->fY = bounds.top(); |
| 284 | bounds.offset(-srcOffset); |
senorblanco@chromium.org | 05054f1 | 2012-03-02 21:05:45 +0000 | [diff] [blame] | 285 | if (width > 0 && height > 0) { |
senorblanco@chromium.org | 8fcad98 | 2013-09-17 13:41:43 +0000 | [diff] [blame] | 286 | dilateX(src, &temp, width, bounds); |
| 287 | SkIRect tmpBounds = SkIRect::MakeWH(bounds.width(), bounds.height()); |
| 288 | dilateY(temp, dst, height, tmpBounds); |
senorblanco@chromium.org | 05054f1 | 2012-03-02 21:05:45 +0000 | [diff] [blame] | 289 | } else if (width > 0) { |
senorblanco@chromium.org | 8fcad98 | 2013-09-17 13:41:43 +0000 | [diff] [blame] | 290 | dilateX(src, dst, width, bounds); |
senorblanco@chromium.org | 05054f1 | 2012-03-02 21:05:45 +0000 | [diff] [blame] | 291 | } else if (height > 0) { |
senorblanco@chromium.org | 8fcad98 | 2013-09-17 13:41:43 +0000 | [diff] [blame] | 292 | dilateY(src, dst, height, bounds); |
senorblanco@chromium.org | 05054f1 | 2012-03-02 21:05:45 +0000 | [diff] [blame] | 293 | } |
| 294 | return true; |
| 295 | } |
| 296 | |
bsalomon@google.com | cf8fb1f | 2012-08-02 14:03:32 +0000 | [diff] [blame] | 297 | #if SK_SUPPORT_GPU |
senorblanco@chromium.org | 84207c4 | 2012-08-22 20:51:19 +0000 | [diff] [blame] | 298 | |
| 299 | /////////////////////////////////////////////////////////////////////////////// |
| 300 | |
| 301 | class GrGLMorphologyEffect; |
| 302 | |
| 303 | /** |
| 304 | * Morphology effects. Depending upon the type of morphology, either the |
| 305 | * component-wise min (Erode_Type) or max (Dilate_Type) of all pixels in the |
| 306 | * kernel is selected as the new color. The new color is modulated by the input |
| 307 | * color. |
| 308 | */ |
| 309 | class GrMorphologyEffect : public Gr1DKernelEffect { |
| 310 | |
| 311 | public: |
| 312 | |
| 313 | enum MorphologyType { |
| 314 | kErode_MorphologyType, |
| 315 | kDilate_MorphologyType, |
| 316 | }; |
| 317 | |
bsalomon@google.com | 0ac6af4 | 2013-01-16 15:16:18 +0000 | [diff] [blame] | 318 | static GrEffectRef* Create(GrTexture* tex, Direction dir, int radius, MorphologyType type) { |
bsalomon@google.com | 6340a41 | 2013-01-22 19:55:59 +0000 | [diff] [blame] | 319 | AutoEffectUnref effect(SkNEW_ARGS(GrMorphologyEffect, (tex, dir, radius, type))); |
bsalomon@google.com | a1ebbe4 | 2013-01-16 15:51:47 +0000 | [diff] [blame] | 320 | return CreateEffectRef(effect); |
bsalomon@google.com | 0ac6af4 | 2013-01-16 15:16:18 +0000 | [diff] [blame] | 321 | } |
| 322 | |
senorblanco@chromium.org | 84207c4 | 2012-08-22 20:51:19 +0000 | [diff] [blame] | 323 | virtual ~GrMorphologyEffect(); |
| 324 | |
| 325 | MorphologyType type() const { return fType; } |
| 326 | |
| 327 | static const char* Name() { return "Morphology"; } |
| 328 | |
bsalomon@google.com | 422e81a | 2012-10-25 14:11:03 +0000 | [diff] [blame] | 329 | typedef GrGLMorphologyEffect GLEffect; |
senorblanco@chromium.org | 84207c4 | 2012-08-22 20:51:19 +0000 | [diff] [blame] | 330 | |
bsalomon@google.com | 396e61f | 2012-10-25 19:00:29 +0000 | [diff] [blame] | 331 | virtual const GrBackendEffectFactory& getFactory() const SK_OVERRIDE; |
bsalomon@google.com | 68b58c9 | 2013-01-17 16:50:08 +0000 | [diff] [blame] | 332 | virtual void getConstantColorComponents(GrColor* color, uint32_t* validFlags) const SK_OVERRIDE; |
senorblanco@chromium.org | 84207c4 | 2012-08-22 20:51:19 +0000 | [diff] [blame] | 333 | |
| 334 | protected: |
| 335 | |
| 336 | MorphologyType fType; |
| 337 | |
| 338 | private: |
bsalomon@google.com | 8a252f7 | 2013-01-22 20:35:13 +0000 | [diff] [blame] | 339 | virtual bool onIsEqual(const GrEffect&) const SK_OVERRIDE; |
bsalomon@google.com | 68b58c9 | 2013-01-17 16:50:08 +0000 | [diff] [blame] | 340 | |
bsalomon@google.com | 0ac6af4 | 2013-01-16 15:16:18 +0000 | [diff] [blame] | 341 | GrMorphologyEffect(GrTexture*, Direction, int radius, MorphologyType); |
| 342 | |
bsalomon@google.com | f271cc7 | 2012-10-24 19:35:13 +0000 | [diff] [blame] | 343 | GR_DECLARE_EFFECT_TEST; |
senorblanco@chromium.org | 84207c4 | 2012-08-22 20:51:19 +0000 | [diff] [blame] | 344 | |
| 345 | typedef Gr1DKernelEffect INHERITED; |
| 346 | }; |
| 347 | |
| 348 | /////////////////////////////////////////////////////////////////////////////// |
| 349 | |
senorblanco@chromium.org | c2594f4 | 2013-01-30 19:08:47 +0000 | [diff] [blame] | 350 | class GrGLMorphologyEffect : public GrGLEffect { |
senorblanco@chromium.org | 84207c4 | 2012-08-22 20:51:19 +0000 | [diff] [blame] | 351 | public: |
bsalomon@google.com | c781888 | 2013-03-20 19:19:53 +0000 | [diff] [blame] | 352 | GrGLMorphologyEffect (const GrBackendEffectFactory&, const GrDrawEffect&); |
senorblanco@chromium.org | 84207c4 | 2012-08-22 20:51:19 +0000 | [diff] [blame] | 353 | |
bsalomon@google.com | 47d7a88 | 2012-10-29 12:47:51 +0000 | [diff] [blame] | 354 | virtual void emitCode(GrGLShaderBuilder*, |
bsalomon@google.com | c781888 | 2013-03-20 19:19:53 +0000 | [diff] [blame] | 355 | const GrDrawEffect&, |
bsalomon@google.com | 47d7a88 | 2012-10-29 12:47:51 +0000 | [diff] [blame] | 356 | EffectKey, |
bsalomon@google.com | 47d7a88 | 2012-10-29 12:47:51 +0000 | [diff] [blame] | 357 | const char* outputColor, |
| 358 | const char* inputColor, |
bsalomon@google.com | 77af680 | 2013-10-02 13:04:56 +0000 | [diff] [blame] | 359 | const TransformedCoordsArray&, |
bsalomon@google.com | 47d7a88 | 2012-10-29 12:47:51 +0000 | [diff] [blame] | 360 | const TextureSamplerArray&) SK_OVERRIDE; |
senorblanco@chromium.org | 84207c4 | 2012-08-22 20:51:19 +0000 | [diff] [blame] | 361 | |
bsalomon@google.com | c781888 | 2013-03-20 19:19:53 +0000 | [diff] [blame] | 362 | static inline EffectKey GenKey(const GrDrawEffect&, const GrGLCaps&); |
senorblanco@chromium.org | 84207c4 | 2012-08-22 20:51:19 +0000 | [diff] [blame] | 363 | |
bsalomon@google.com | c781888 | 2013-03-20 19:19:53 +0000 | [diff] [blame] | 364 | virtual void setData(const GrGLUniformManager&, const GrDrawEffect&) SK_OVERRIDE; |
senorblanco@chromium.org | 84207c4 | 2012-08-22 20:51:19 +0000 | [diff] [blame] | 365 | |
| 366 | private: |
| 367 | int width() const { return GrMorphologyEffect::WidthFromRadius(fRadius); } |
| 368 | |
| 369 | int fRadius; |
| 370 | GrMorphologyEffect::MorphologyType fType; |
| 371 | GrGLUniformManager::UniformHandle fImageIncrementUni; |
| 372 | |
bsalomon@google.com | 47d7a88 | 2012-10-29 12:47:51 +0000 | [diff] [blame] | 373 | typedef GrGLEffect INHERITED; |
senorblanco@chromium.org | 84207c4 | 2012-08-22 20:51:19 +0000 | [diff] [blame] | 374 | }; |
| 375 | |
bsalomon@google.com | 396e61f | 2012-10-25 19:00:29 +0000 | [diff] [blame] | 376 | GrGLMorphologyEffect::GrGLMorphologyEffect(const GrBackendEffectFactory& factory, |
bsalomon@google.com | c781888 | 2013-03-20 19:19:53 +0000 | [diff] [blame] | 377 | const GrDrawEffect& drawEffect) |
bsalomon@google.com | 77af680 | 2013-10-02 13:04:56 +0000 | [diff] [blame] | 378 | : INHERITED(factory) { |
bsalomon@google.com | c781888 | 2013-03-20 19:19:53 +0000 | [diff] [blame] | 379 | const GrMorphologyEffect& m = drawEffect.castEffect<GrMorphologyEffect>(); |
senorblanco@chromium.org | 84207c4 | 2012-08-22 20:51:19 +0000 | [diff] [blame] | 380 | fRadius = m.radius(); |
| 381 | fType = m.type(); |
| 382 | } |
| 383 | |
bsalomon@google.com | 47d7a88 | 2012-10-29 12:47:51 +0000 | [diff] [blame] | 384 | void GrGLMorphologyEffect::emitCode(GrGLShaderBuilder* builder, |
bsalomon@google.com | c781888 | 2013-03-20 19:19:53 +0000 | [diff] [blame] | 385 | const GrDrawEffect&, |
bsalomon@google.com | b4a55b7 | 2012-11-02 20:45:37 +0000 | [diff] [blame] | 386 | EffectKey key, |
bsalomon@google.com | 47d7a88 | 2012-10-29 12:47:51 +0000 | [diff] [blame] | 387 | const char* outputColor, |
| 388 | const char* inputColor, |
bsalomon@google.com | 77af680 | 2013-10-02 13:04:56 +0000 | [diff] [blame] | 389 | const TransformedCoordsArray& coords, |
bsalomon@google.com | 47d7a88 | 2012-10-29 12:47:51 +0000 | [diff] [blame] | 390 | const TextureSamplerArray& samplers) { |
bsalomon@google.com | 77af680 | 2013-10-02 13:04:56 +0000 | [diff] [blame] | 391 | SkString coords2D = builder->ensureFSCoords2D(coords, 0); |
commit-bot@chromium.org | 74a3a21 | 2013-08-30 19:43:59 +0000 | [diff] [blame] | 392 | fImageIncrementUni = builder->addUniform(GrGLShaderBuilder::kFragment_Visibility, |
senorblanco@chromium.org | 84207c4 | 2012-08-22 20:51:19 +0000 | [diff] [blame] | 393 | kVec2f_GrSLType, "ImageIncrement"); |
senorblanco@chromium.org | 84207c4 | 2012-08-22 20:51:19 +0000 | [diff] [blame] | 394 | |
senorblanco@chromium.org | 84207c4 | 2012-08-22 20:51:19 +0000 | [diff] [blame] | 395 | const char* func; |
| 396 | switch (fType) { |
| 397 | case GrMorphologyEffect::kErode_MorphologyType: |
bsalomon@google.com | f910d3b | 2013-03-07 17:06:57 +0000 | [diff] [blame] | 398 | builder->fsCodeAppendf("\t\t%s = vec4(1, 1, 1, 1);\n", outputColor); |
senorblanco@chromium.org | 84207c4 | 2012-08-22 20:51:19 +0000 | [diff] [blame] | 399 | func = "min"; |
| 400 | break; |
| 401 | case GrMorphologyEffect::kDilate_MorphologyType: |
bsalomon@google.com | f910d3b | 2013-03-07 17:06:57 +0000 | [diff] [blame] | 402 | builder->fsCodeAppendf("\t\t%s = vec4(0, 0, 0, 0);\n", outputColor); |
senorblanco@chromium.org | 84207c4 | 2012-08-22 20:51:19 +0000 | [diff] [blame] | 403 | func = "max"; |
| 404 | break; |
| 405 | default: |
| 406 | GrCrash("Unexpected type"); |
| 407 | func = ""; // suppress warning |
| 408 | break; |
| 409 | } |
| 410 | const char* imgInc = builder->getUniformCStr(fImageIncrementUni); |
| 411 | |
bsalomon@google.com | 77af680 | 2013-10-02 13:04:56 +0000 | [diff] [blame] | 412 | builder->fsCodeAppendf("\t\tvec2 coord = %s - %d.0 * %s;\n", coords2D.c_str(), fRadius, imgInc); |
bsalomon@google.com | f910d3b | 2013-03-07 17:06:57 +0000 | [diff] [blame] | 413 | builder->fsCodeAppendf("\t\tfor (int i = 0; i < %d; i++) {\n", this->width()); |
| 414 | builder->fsCodeAppendf("\t\t\t%s = %s(%s, ", outputColor, func, outputColor); |
commit-bot@chromium.org | 74a3a21 | 2013-08-30 19:43:59 +0000 | [diff] [blame] | 415 | builder->fsAppendTextureLookup(samplers[0], "coord"); |
bsalomon@google.com | f910d3b | 2013-03-07 17:06:57 +0000 | [diff] [blame] | 416 | builder->fsCodeAppend(");\n"); |
| 417 | builder->fsCodeAppendf("\t\t\tcoord += %s;\n", imgInc); |
| 418 | builder->fsCodeAppend("\t\t}\n"); |
| 419 | SkString modulate; |
| 420 | GrGLSLMulVarBy4f(&modulate, 2, outputColor, inputColor); |
| 421 | builder->fsCodeAppend(modulate.c_str()); |
senorblanco@chromium.org | 84207c4 | 2012-08-22 20:51:19 +0000 | [diff] [blame] | 422 | } |
| 423 | |
bsalomon@google.com | c781888 | 2013-03-20 19:19:53 +0000 | [diff] [blame] | 424 | GrGLEffect::EffectKey GrGLMorphologyEffect::GenKey(const GrDrawEffect& drawEffect, |
| 425 | const GrGLCaps&) { |
| 426 | const GrMorphologyEffect& m = drawEffect.castEffect<GrMorphologyEffect>(); |
bsalomon@google.com | 46fba0d | 2012-10-25 21:42:05 +0000 | [diff] [blame] | 427 | EffectKey key = static_cast<EffectKey>(m.radius()); |
senorblanco@chromium.org | 84207c4 | 2012-08-22 20:51:19 +0000 | [diff] [blame] | 428 | key |= (m.type() << 8); |
bsalomon@google.com | 77af680 | 2013-10-02 13:04:56 +0000 | [diff] [blame] | 429 | return key; |
senorblanco@chromium.org | 84207c4 | 2012-08-22 20:51:19 +0000 | [diff] [blame] | 430 | } |
| 431 | |
bsalomon@google.com | c781888 | 2013-03-20 19:19:53 +0000 | [diff] [blame] | 432 | void GrGLMorphologyEffect::setData(const GrGLUniformManager& uman, |
| 433 | const GrDrawEffect& drawEffect) { |
| 434 | const Gr1DKernelEffect& kern = drawEffect.castEffect<Gr1DKernelEffect>(); |
bsalomon@google.com | 2d0bade | 2012-10-26 19:01:17 +0000 | [diff] [blame] | 435 | GrTexture& texture = *kern.texture(0); |
senorblanco@chromium.org | 84207c4 | 2012-08-22 20:51:19 +0000 | [diff] [blame] | 436 | // the code we generated was for a specific kernel radius |
commit-bot@chromium.org | 96ae688 | 2013-08-14 12:09:00 +0000 | [diff] [blame] | 437 | SkASSERT(kern.radius() == fRadius); |
senorblanco@chromium.org | 84207c4 | 2012-08-22 20:51:19 +0000 | [diff] [blame] | 438 | float imageIncrement[2] = { 0 }; |
| 439 | switch (kern.direction()) { |
| 440 | case Gr1DKernelEffect::kX_Direction: |
| 441 | imageIncrement[0] = 1.0f / texture.width(); |
| 442 | break; |
| 443 | case Gr1DKernelEffect::kY_Direction: |
| 444 | imageIncrement[1] = 1.0f / texture.height(); |
| 445 | break; |
| 446 | default: |
| 447 | GrCrash("Unknown filter direction."); |
| 448 | } |
commit-bot@chromium.org | d3baf20 | 2013-11-07 22:06:08 +0000 | [diff] [blame] | 449 | uman.set2fv(fImageIncrementUni, 1, imageIncrement); |
senorblanco@chromium.org | 84207c4 | 2012-08-22 20:51:19 +0000 | [diff] [blame] | 450 | } |
| 451 | |
| 452 | /////////////////////////////////////////////////////////////////////////////// |
| 453 | |
| 454 | GrMorphologyEffect::GrMorphologyEffect(GrTexture* texture, |
| 455 | Direction direction, |
| 456 | int radius, |
| 457 | MorphologyType type) |
| 458 | : Gr1DKernelEffect(texture, direction, radius) |
| 459 | , fType(type) { |
| 460 | } |
| 461 | |
| 462 | GrMorphologyEffect::~GrMorphologyEffect() { |
| 463 | } |
| 464 | |
bsalomon@google.com | 396e61f | 2012-10-25 19:00:29 +0000 | [diff] [blame] | 465 | const GrBackendEffectFactory& GrMorphologyEffect::getFactory() const { |
| 466 | return GrTBackendEffectFactory<GrMorphologyEffect>::getInstance(); |
senorblanco@chromium.org | 84207c4 | 2012-08-22 20:51:19 +0000 | [diff] [blame] | 467 | } |
| 468 | |
bsalomon@google.com | 8a252f7 | 2013-01-22 20:35:13 +0000 | [diff] [blame] | 469 | bool GrMorphologyEffect::onIsEqual(const GrEffect& sBase) const { |
bsalomon@google.com | 6340a41 | 2013-01-22 19:55:59 +0000 | [diff] [blame] | 470 | const GrMorphologyEffect& s = CastEffect<GrMorphologyEffect>(sBase); |
bsalomon@google.com | 68b58c9 | 2013-01-17 16:50:08 +0000 | [diff] [blame] | 471 | return (this->texture(0) == s.texture(0) && |
senorblanco@chromium.org | 84207c4 | 2012-08-22 20:51:19 +0000 | [diff] [blame] | 472 | this->radius() == s.radius() && |
| 473 | this->direction() == s.direction() && |
| 474 | this->type() == s.type()); |
| 475 | } |
| 476 | |
bsalomon@google.com | 68b58c9 | 2013-01-17 16:50:08 +0000 | [diff] [blame] | 477 | void GrMorphologyEffect::getConstantColorComponents(GrColor* color, uint32_t* validFlags) const { |
| 478 | // This is valid because the color components of the result of the kernel all come |
| 479 | // exactly from existing values in the source texture. |
| 480 | this->updateConstantColorComponentsForModulation(color, validFlags); |
| 481 | } |
| 482 | |
senorblanco@chromium.org | 84207c4 | 2012-08-22 20:51:19 +0000 | [diff] [blame] | 483 | /////////////////////////////////////////////////////////////////////////////// |
| 484 | |
bsalomon@google.com | f271cc7 | 2012-10-24 19:35:13 +0000 | [diff] [blame] | 485 | GR_DEFINE_EFFECT_TEST(GrMorphologyEffect); |
senorblanco@chromium.org | 84207c4 | 2012-08-22 20:51:19 +0000 | [diff] [blame] | 486 | |
commit-bot@chromium.org | e0e7cfe | 2013-09-09 20:09:12 +0000 | [diff] [blame] | 487 | GrEffectRef* GrMorphologyEffect::TestCreate(SkRandom* random, |
sugoi@google.com | e0e385c | 2013-03-11 18:50:03 +0000 | [diff] [blame] | 488 | GrContext*, |
bsalomon@google.com | c26d94f | 2013-03-25 18:19:00 +0000 | [diff] [blame] | 489 | const GrDrawTargetCaps&, |
bsalomon@google.com | 0ac6af4 | 2013-01-16 15:16:18 +0000 | [diff] [blame] | 490 | GrTexture* textures[]) { |
bsalomon@google.com | 6f261be | 2012-10-24 19:07:10 +0000 | [diff] [blame] | 491 | int texIdx = random->nextBool() ? GrEffectUnitTest::kSkiaPMTextureIdx : |
| 492 | GrEffectUnitTest::kAlphaTextureIdx; |
senorblanco@chromium.org | 84207c4 | 2012-08-22 20:51:19 +0000 | [diff] [blame] | 493 | Direction dir = random->nextBool() ? kX_Direction : kY_Direction; |
| 494 | static const int kMaxRadius = 10; |
| 495 | int radius = random->nextRangeU(1, kMaxRadius); |
| 496 | MorphologyType type = random->nextBool() ? GrMorphologyEffect::kErode_MorphologyType : |
| 497 | GrMorphologyEffect::kDilate_MorphologyType; |
| 498 | |
bsalomon@google.com | 0ac6af4 | 2013-01-16 15:16:18 +0000 | [diff] [blame] | 499 | return GrMorphologyEffect::Create(textures[texIdx], dir, radius, type); |
senorblanco@chromium.org | 84207c4 | 2012-08-22 20:51:19 +0000 | [diff] [blame] | 500 | } |
| 501 | |
| 502 | namespace { |
| 503 | |
| 504 | void apply_morphology_pass(GrContext* context, |
| 505 | GrTexture* texture, |
senorblanco@chromium.org | 8fcad98 | 2013-09-17 13:41:43 +0000 | [diff] [blame] | 506 | const SkIRect& srcRect, |
| 507 | const SkIRect& dstRect, |
senorblanco@chromium.org | 84207c4 | 2012-08-22 20:51:19 +0000 | [diff] [blame] | 508 | int radius, |
| 509 | GrMorphologyEffect::MorphologyType morphType, |
| 510 | Gr1DKernelEffect::Direction direction) { |
senorblanco@chromium.org | 84207c4 | 2012-08-22 20:51:19 +0000 | [diff] [blame] | 511 | GrPaint paint; |
commit-bot@chromium.org | 42dacab | 2013-07-13 17:24:24 +0000 | [diff] [blame] | 512 | paint.addColorEffect(GrMorphologyEffect::Create(texture, |
| 513 | direction, |
| 514 | radius, |
| 515 | morphType))->unref(); |
reed@google.com | 4469938 | 2013-10-31 17:28:30 +0000 | [diff] [blame] | 516 | context->drawRectToRect(paint, SkRect::Make(dstRect), SkRect::Make(srcRect)); |
senorblanco@chromium.org | 84207c4 | 2012-08-22 20:51:19 +0000 | [diff] [blame] | 517 | } |
| 518 | |
senorblanco@chromium.org | 8fcad98 | 2013-09-17 13:41:43 +0000 | [diff] [blame] | 519 | bool apply_morphology(const SkBitmap& input, |
| 520 | const SkIRect& rect, |
| 521 | GrMorphologyEffect::MorphologyType morphType, |
| 522 | SkISize radius, |
| 523 | SkBitmap* dst) { |
| 524 | GrTexture* srcTexture = input.getTexture(); |
| 525 | SkASSERT(NULL != srcTexture); |
senorblanco@chromium.org | 84207c4 | 2012-08-22 20:51:19 +0000 | [diff] [blame] | 526 | GrContext* context = srcTexture->getContext(); |
| 527 | srcTexture->ref(); |
senorblanco@chromium.org | 8fcad98 | 2013-09-17 13:41:43 +0000 | [diff] [blame] | 528 | SkAutoTUnref<GrTexture> src(srcTexture); |
bsalomon@google.com | 3cbaa2d | 2012-10-12 14:51:52 +0000 | [diff] [blame] | 529 | |
| 530 | GrContext::AutoMatrix am; |
| 531 | am.setIdentity(context); |
| 532 | |
commit-bot@chromium.org | fd03d4a | 2013-07-17 21:39:42 +0000 | [diff] [blame] | 533 | GrContext::AutoClip acs(context, SkRect::MakeWH(SkIntToScalar(srcTexture->width()), |
senorblanco@chromium.org | 84207c4 | 2012-08-22 20:51:19 +0000 | [diff] [blame] | 534 | SkIntToScalar(srcTexture->height()))); |
bsalomon@google.com | 3cbaa2d | 2012-10-12 14:51:52 +0000 | [diff] [blame] | 535 | |
senorblanco@chromium.org | 8fcad98 | 2013-09-17 13:41:43 +0000 | [diff] [blame] | 536 | SkIRect dstRect = SkIRect::MakeWH(rect.width(), rect.height()); |
senorblanco@chromium.org | 84207c4 | 2012-08-22 20:51:19 +0000 | [diff] [blame] | 537 | GrTextureDesc desc; |
| 538 | desc.fFlags = kRenderTarget_GrTextureFlagBit | kNoStencil_GrTextureFlagBit; |
senorblanco@chromium.org | c2594f4 | 2013-01-30 19:08:47 +0000 | [diff] [blame] | 539 | desc.fWidth = rect.width(); |
| 540 | desc.fHeight = rect.height(); |
| 541 | desc.fConfig = kSkia8888_GrPixelConfig; |
senorblanco@chromium.org | 8fcad98 | 2013-09-17 13:41:43 +0000 | [diff] [blame] | 542 | SkIRect srcRect = rect; |
bsalomon@google.com | 3cbaa2d | 2012-10-12 14:51:52 +0000 | [diff] [blame] | 543 | |
senorblanco@chromium.org | 84207c4 | 2012-08-22 20:51:19 +0000 | [diff] [blame] | 544 | if (radius.fWidth > 0) { |
| 545 | GrAutoScratchTexture ast(context, desc); |
| 546 | GrContext::AutoRenderTarget art(context, ast.texture()->asRenderTarget()); |
senorblanco@chromium.org | 8fcad98 | 2013-09-17 13:41:43 +0000 | [diff] [blame] | 547 | apply_morphology_pass(context, src, srcRect, dstRect, radius.fWidth, |
senorblanco@chromium.org | 84207c4 | 2012-08-22 20:51:19 +0000 | [diff] [blame] | 548 | morphType, Gr1DKernelEffect::kX_Direction); |
senorblanco@chromium.org | 8fcad98 | 2013-09-17 13:41:43 +0000 | [diff] [blame] | 549 | SkIRect clearRect = SkIRect::MakeXYWH(dstRect.fLeft, dstRect.fBottom, |
| 550 | dstRect.width(), radius.fHeight); |
skia.committer@gmail.com | b77f0f4 | 2013-10-30 07:01:56 +0000 | [diff] [blame] | 551 | context->clear(&clearRect, GrMorphologyEffect::kErode_MorphologyType == morphType ? |
| 552 | SK_ColorWHITE : |
robertphillips@google.com | 56ce48a | 2013-10-31 21:44:25 +0000 | [diff] [blame] | 553 | SK_ColorTRANSPARENT, false); |
senorblanco@chromium.org | 8fcad98 | 2013-09-17 13:41:43 +0000 | [diff] [blame] | 554 | src.reset(ast.detach()); |
| 555 | srcRect = dstRect; |
senorblanco@chromium.org | 84207c4 | 2012-08-22 20:51:19 +0000 | [diff] [blame] | 556 | } |
| 557 | if (radius.fHeight > 0) { |
| 558 | GrAutoScratchTexture ast(context, desc); |
| 559 | GrContext::AutoRenderTarget art(context, ast.texture()->asRenderTarget()); |
senorblanco@chromium.org | 8fcad98 | 2013-09-17 13:41:43 +0000 | [diff] [blame] | 560 | apply_morphology_pass(context, src, srcRect, dstRect, radius.fHeight, |
senorblanco@chromium.org | 84207c4 | 2012-08-22 20:51:19 +0000 | [diff] [blame] | 561 | morphType, Gr1DKernelEffect::kY_Direction); |
senorblanco@chromium.org | 8fcad98 | 2013-09-17 13:41:43 +0000 | [diff] [blame] | 562 | src.reset(ast.detach()); |
senorblanco@chromium.org | 84207c4 | 2012-08-22 20:51:19 +0000 | [diff] [blame] | 563 | } |
senorblanco@chromium.org | 8fcad98 | 2013-09-17 13:41:43 +0000 | [diff] [blame] | 564 | return SkImageFilterUtils::WrapTexture(src, rect.width(), rect.height(), dst); |
senorblanco@chromium.org | 84207c4 | 2012-08-22 20:51:19 +0000 | [diff] [blame] | 565 | } |
| 566 | |
| 567 | }; |
| 568 | |
commit-bot@chromium.org | 1aa54bf | 2013-08-05 16:53:50 +0000 | [diff] [blame] | 569 | bool SkDilateImageFilter::filterImageGPU(Proxy* proxy, const SkBitmap& src, const SkMatrix& ctm, |
| 570 | SkBitmap* result, SkIPoint* offset) { |
senorblanco@chromium.org | 8fcad98 | 2013-09-17 13:41:43 +0000 | [diff] [blame] | 571 | SkBitmap input; |
| 572 | if (!SkImageFilterUtils::GetInputResultGPU(getInput(0), proxy, src, ctm, &input, offset)) { |
senorblanco@chromium.org | c2594f4 | 2013-01-30 19:08:47 +0000 | [diff] [blame] | 573 | return false; |
| 574 | } |
senorblanco@chromium.org | c2594f4 | 2013-01-30 19:08:47 +0000 | [diff] [blame] | 575 | SkIRect bounds; |
| 576 | src.getBounds(&bounds); |
senorblanco@chromium.org | 8fcad98 | 2013-09-17 13:41:43 +0000 | [diff] [blame] | 577 | if (!this->applyCropRect(&bounds, ctm)) { |
| 578 | return false; |
| 579 | } |
| 580 | int width = radius().width(); |
| 581 | int height = radius().height(); |
| 582 | |
| 583 | if (width < 0 || height < 0) { |
| 584 | return false; |
| 585 | } |
| 586 | |
| 587 | if (width == 0 && height == 0) { |
| 588 | src.extractSubset(result, bounds); |
senorblanco@chromium.org | 6776b82 | 2014-01-03 21:48:22 +0000 | [diff] [blame] | 589 | offset->fX = bounds.left(); |
| 590 | offset->fY = bounds.top(); |
senorblanco@chromium.org | 8fcad98 | 2013-09-17 13:41:43 +0000 | [diff] [blame] | 591 | return true; |
| 592 | } |
| 593 | |
| 594 | if (!apply_morphology(input, bounds, GrMorphologyEffect::kDilate_MorphologyType, radius(), result)) { |
| 595 | return false; |
| 596 | } |
senorblanco@chromium.org | 6776b82 | 2014-01-03 21:48:22 +0000 | [diff] [blame] | 597 | offset->fX = bounds.left(); |
| 598 | offset->fY = bounds.top(); |
senorblanco@chromium.org | 8fcad98 | 2013-09-17 13:41:43 +0000 | [diff] [blame] | 599 | return true; |
senorblanco@chromium.org | 05054f1 | 2012-03-02 21:05:45 +0000 | [diff] [blame] | 600 | } |
| 601 | |
commit-bot@chromium.org | 1aa54bf | 2013-08-05 16:53:50 +0000 | [diff] [blame] | 602 | bool SkErodeImageFilter::filterImageGPU(Proxy* proxy, const SkBitmap& src, const SkMatrix& ctm, |
| 603 | SkBitmap* result, SkIPoint* offset) { |
senorblanco@chromium.org | 8fcad98 | 2013-09-17 13:41:43 +0000 | [diff] [blame] | 604 | SkBitmap input; |
| 605 | if (!SkImageFilterUtils::GetInputResultGPU(getInput(0), proxy, src, ctm, &input, offset)) { |
senorblanco@chromium.org | c2594f4 | 2013-01-30 19:08:47 +0000 | [diff] [blame] | 606 | return false; |
| 607 | } |
senorblanco@chromium.org | c2594f4 | 2013-01-30 19:08:47 +0000 | [diff] [blame] | 608 | SkIRect bounds; |
| 609 | src.getBounds(&bounds); |
senorblanco@chromium.org | 8fcad98 | 2013-09-17 13:41:43 +0000 | [diff] [blame] | 610 | if (!this->applyCropRect(&bounds, ctm)) { |
| 611 | return false; |
| 612 | } |
| 613 | int width = radius().width(); |
| 614 | int height = radius().height(); |
| 615 | |
| 616 | if (width < 0 || height < 0) { |
| 617 | return false; |
| 618 | } |
| 619 | |
| 620 | if (width == 0 && height == 0) { |
| 621 | src.extractSubset(result, bounds); |
senorblanco@chromium.org | 6776b82 | 2014-01-03 21:48:22 +0000 | [diff] [blame] | 622 | offset->fX = bounds.left(); |
| 623 | offset->fY = bounds.top(); |
senorblanco@chromium.org | 8fcad98 | 2013-09-17 13:41:43 +0000 | [diff] [blame] | 624 | return true; |
| 625 | } |
| 626 | |
| 627 | if (!apply_morphology(input, bounds, GrMorphologyEffect::kErode_MorphologyType, radius(), result)) { |
| 628 | return false; |
| 629 | } |
senorblanco@chromium.org | 6776b82 | 2014-01-03 21:48:22 +0000 | [diff] [blame] | 630 | offset->fX = bounds.left(); |
| 631 | offset->fY = bounds.top(); |
senorblanco@chromium.org | 8fcad98 | 2013-09-17 13:41:43 +0000 | [diff] [blame] | 632 | return true; |
senorblanco@chromium.org | 05054f1 | 2012-03-02 21:05:45 +0000 | [diff] [blame] | 633 | } |
| 634 | |
senorblanco@chromium.org | 84207c4 | 2012-08-22 20:51:19 +0000 | [diff] [blame] | 635 | #endif |