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