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