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