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 | f1369ce | 2012-08-20 14:53:21 +0000 | [diff] [blame] | 29 | SkMorphologyImageFilter::SkMorphologyImageFilter(int radiusX, int radiusY, SkImageFilter* input) |
| 30 | : INHERITED(input), 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 | |
| 74 | static void erodeX(const SkBitmap& src, SkBitmap* dst, int radiusX) |
| 75 | { |
| 76 | erode(src.getAddr32(0, 0), dst->getAddr32(0, 0), |
| 77 | radiusX, src.width(), src.height(), |
| 78 | 1, src.rowBytesAsPixels(), 1, dst->rowBytesAsPixels()); |
| 79 | } |
| 80 | |
| 81 | static void erodeY(const SkBitmap& src, SkBitmap* dst, int radiusY) |
| 82 | { |
| 83 | erode(src.getAddr32(0, 0), dst->getAddr32(0, 0), |
| 84 | radiusY, src.height(), src.width(), |
| 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 | |
| 122 | static void dilateX(const SkBitmap& src, SkBitmap* dst, int radiusX) |
| 123 | { |
| 124 | dilate(src.getAddr32(0, 0), dst->getAddr32(0, 0), |
| 125 | radiusX, src.width(), src.height(), |
| 126 | 1, src.rowBytesAsPixels(), 1, dst->rowBytesAsPixels()); |
| 127 | } |
| 128 | |
| 129 | static void dilateY(const SkBitmap& src, SkBitmap* dst, int radiusY) |
| 130 | { |
| 131 | dilate(src.getAddr32(0, 0), dst->getAddr32(0, 0), |
| 132 | radiusY, src.height(), src.width(), |
| 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 | 377c14a | 2013-02-04 22:57:21 +0000 | [diff] [blame] | 139 | SkBitmap src = this->getInputResult(0, proxy, source, ctm, offset); |
senorblanco@chromium.org | 05054f1 | 2012-03-02 21:05:45 +0000 | [diff] [blame] | 140 | if (src.config() != SkBitmap::kARGB_8888_Config) { |
| 141 | return false; |
| 142 | } |
| 143 | |
| 144 | SkAutoLockPixels alp(src); |
| 145 | if (!src.getPixels()) { |
| 146 | return false; |
| 147 | } |
| 148 | |
| 149 | dst->setConfig(src.config(), src.width(), src.height()); |
| 150 | dst->allocPixels(); |
| 151 | |
| 152 | int width = radius().width(); |
| 153 | int height = radius().height(); |
| 154 | |
| 155 | if (width < 0 || height < 0) { |
| 156 | return false; |
| 157 | } |
| 158 | |
| 159 | if (width == 0 && height == 0) { |
| 160 | src.copyTo(dst, dst->config()); |
| 161 | return true; |
| 162 | } |
| 163 | |
| 164 | SkBitmap temp; |
| 165 | temp.setConfig(dst->config(), dst->width(), dst->height()); |
| 166 | if (!temp.allocPixels()) { |
| 167 | return false; |
| 168 | } |
| 169 | |
| 170 | if (width > 0 && height > 0) { |
| 171 | erodeX(src, &temp, width); |
| 172 | erodeY(temp, dst, height); |
| 173 | } else if (width > 0) { |
| 174 | erodeX(src, dst, width); |
| 175 | } else if (height > 0) { |
| 176 | erodeY(src, dst, height); |
| 177 | } |
| 178 | return true; |
| 179 | } |
| 180 | |
senorblanco@chromium.org | f1369ce | 2012-08-20 14:53:21 +0000 | [diff] [blame] | 181 | bool SkDilateImageFilter::onFilterImage(Proxy* proxy, |
| 182 | const SkBitmap& source, const SkMatrix& ctm, |
| 183 | SkBitmap* dst, SkIPoint* offset) { |
senorblanco@chromium.org | 377c14a | 2013-02-04 22:57:21 +0000 | [diff] [blame] | 184 | SkBitmap src = this->getInputResult(0, proxy, source, ctm, offset); |
senorblanco@chromium.org | 05054f1 | 2012-03-02 21:05:45 +0000 | [diff] [blame] | 185 | if (src.config() != SkBitmap::kARGB_8888_Config) { |
| 186 | return false; |
| 187 | } |
| 188 | |
| 189 | SkAutoLockPixels alp(src); |
| 190 | if (!src.getPixels()) { |
| 191 | return false; |
| 192 | } |
| 193 | |
| 194 | dst->setConfig(src.config(), src.width(), src.height()); |
| 195 | dst->allocPixels(); |
| 196 | |
| 197 | int width = radius().width(); |
| 198 | int height = radius().height(); |
| 199 | |
| 200 | if (width < 0 || height < 0) { |
| 201 | return false; |
| 202 | } |
| 203 | |
| 204 | if (width == 0 && height == 0) { |
| 205 | src.copyTo(dst, dst->config()); |
| 206 | return true; |
| 207 | } |
| 208 | |
| 209 | SkBitmap temp; |
| 210 | temp.setConfig(dst->config(), dst->width(), dst->height()); |
| 211 | if (!temp.allocPixels()) { |
| 212 | return false; |
| 213 | } |
| 214 | |
| 215 | if (width > 0 && height > 0) { |
| 216 | dilateX(src, &temp, width); |
| 217 | dilateY(temp, dst, height); |
| 218 | } else if (width > 0) { |
| 219 | dilateX(src, dst, width); |
| 220 | } else if (height > 0) { |
| 221 | dilateY(src, dst, height); |
| 222 | } |
| 223 | return true; |
| 224 | } |
| 225 | |
bsalomon@google.com | cf8fb1f | 2012-08-02 14:03:32 +0000 | [diff] [blame] | 226 | #if SK_SUPPORT_GPU |
senorblanco@chromium.org | 84207c4 | 2012-08-22 20:51:19 +0000 | [diff] [blame] | 227 | |
| 228 | /////////////////////////////////////////////////////////////////////////////// |
| 229 | |
| 230 | class GrGLMorphologyEffect; |
| 231 | |
| 232 | /** |
| 233 | * Morphology effects. Depending upon the type of morphology, either the |
| 234 | * component-wise min (Erode_Type) or max (Dilate_Type) of all pixels in the |
| 235 | * kernel is selected as the new color. The new color is modulated by the input |
| 236 | * color. |
| 237 | */ |
| 238 | class GrMorphologyEffect : public Gr1DKernelEffect { |
| 239 | |
| 240 | public: |
| 241 | |
| 242 | enum MorphologyType { |
| 243 | kErode_MorphologyType, |
| 244 | kDilate_MorphologyType, |
| 245 | }; |
| 246 | |
bsalomon@google.com | 0ac6af4 | 2013-01-16 15:16:18 +0000 | [diff] [blame] | 247 | static GrEffectRef* Create(GrTexture* tex, Direction dir, int radius, MorphologyType type) { |
bsalomon@google.com | 6340a41 | 2013-01-22 19:55:59 +0000 | [diff] [blame] | 248 | AutoEffectUnref effect(SkNEW_ARGS(GrMorphologyEffect, (tex, dir, radius, type))); |
bsalomon@google.com | a1ebbe4 | 2013-01-16 15:51:47 +0000 | [diff] [blame] | 249 | return CreateEffectRef(effect); |
bsalomon@google.com | 0ac6af4 | 2013-01-16 15:16:18 +0000 | [diff] [blame] | 250 | } |
| 251 | |
senorblanco@chromium.org | 84207c4 | 2012-08-22 20:51:19 +0000 | [diff] [blame] | 252 | virtual ~GrMorphologyEffect(); |
| 253 | |
| 254 | MorphologyType type() const { return fType; } |
| 255 | |
| 256 | static const char* Name() { return "Morphology"; } |
| 257 | |
bsalomon@google.com | 422e81a | 2012-10-25 14:11:03 +0000 | [diff] [blame] | 258 | typedef GrGLMorphologyEffect GLEffect; |
senorblanco@chromium.org | 84207c4 | 2012-08-22 20:51:19 +0000 | [diff] [blame] | 259 | |
bsalomon@google.com | 396e61f | 2012-10-25 19:00:29 +0000 | [diff] [blame] | 260 | virtual const GrBackendEffectFactory& getFactory() const SK_OVERRIDE; |
bsalomon@google.com | 68b58c9 | 2013-01-17 16:50:08 +0000 | [diff] [blame] | 261 | virtual void getConstantColorComponents(GrColor* color, uint32_t* validFlags) const SK_OVERRIDE; |
senorblanco@chromium.org | 84207c4 | 2012-08-22 20:51:19 +0000 | [diff] [blame] | 262 | |
| 263 | protected: |
| 264 | |
| 265 | MorphologyType fType; |
| 266 | |
| 267 | private: |
bsalomon@google.com | 8a252f7 | 2013-01-22 20:35:13 +0000 | [diff] [blame] | 268 | virtual bool onIsEqual(const GrEffect&) const SK_OVERRIDE; |
bsalomon@google.com | 68b58c9 | 2013-01-17 16:50:08 +0000 | [diff] [blame] | 269 | |
bsalomon@google.com | 0ac6af4 | 2013-01-16 15:16:18 +0000 | [diff] [blame] | 270 | GrMorphologyEffect(GrTexture*, Direction, int radius, MorphologyType); |
| 271 | |
bsalomon@google.com | f271cc7 | 2012-10-24 19:35:13 +0000 | [diff] [blame] | 272 | GR_DECLARE_EFFECT_TEST; |
senorblanco@chromium.org | 84207c4 | 2012-08-22 20:51:19 +0000 | [diff] [blame] | 273 | |
| 274 | typedef Gr1DKernelEffect INHERITED; |
| 275 | }; |
| 276 | |
| 277 | /////////////////////////////////////////////////////////////////////////////// |
| 278 | |
senorblanco@chromium.org | c2594f4 | 2013-01-30 19:08:47 +0000 | [diff] [blame] | 279 | class GrGLMorphologyEffect : public GrGLEffect { |
senorblanco@chromium.org | 84207c4 | 2012-08-22 20:51:19 +0000 | [diff] [blame] | 280 | public: |
bsalomon@google.com | 6340a41 | 2013-01-22 19:55:59 +0000 | [diff] [blame] | 281 | GrGLMorphologyEffect (const GrBackendEffectFactory&, const GrEffectRef&); |
senorblanco@chromium.org | 84207c4 | 2012-08-22 20:51:19 +0000 | [diff] [blame] | 282 | |
bsalomon@google.com | 47d7a88 | 2012-10-29 12:47:51 +0000 | [diff] [blame] | 283 | virtual void emitCode(GrGLShaderBuilder*, |
bsalomon@google.com | 2eaaefd | 2012-10-29 19:51:22 +0000 | [diff] [blame] | 284 | const GrEffectStage&, |
bsalomon@google.com | 47d7a88 | 2012-10-29 12:47:51 +0000 | [diff] [blame] | 285 | EffectKey, |
| 286 | const char* vertexCoords, |
| 287 | const char* outputColor, |
| 288 | const char* inputColor, |
| 289 | const TextureSamplerArray&) SK_OVERRIDE; |
senorblanco@chromium.org | 84207c4 | 2012-08-22 20:51:19 +0000 | [diff] [blame] | 290 | |
bsalomon@google.com | 2eaaefd | 2012-10-29 19:51:22 +0000 | [diff] [blame] | 291 | static inline EffectKey GenKey(const GrEffectStage&, const GrGLCaps&); |
senorblanco@chromium.org | 84207c4 | 2012-08-22 20:51:19 +0000 | [diff] [blame] | 292 | |
bsalomon@google.com | 28a15fb | 2012-10-26 17:53:18 +0000 | [diff] [blame] | 293 | virtual void setData(const GrGLUniformManager&, const GrEffectStage&) SK_OVERRIDE; |
senorblanco@chromium.org | 84207c4 | 2012-08-22 20:51:19 +0000 | [diff] [blame] | 294 | |
| 295 | private: |
| 296 | int width() const { return GrMorphologyEffect::WidthFromRadius(fRadius); } |
| 297 | |
| 298 | int fRadius; |
| 299 | GrMorphologyEffect::MorphologyType fType; |
| 300 | GrGLUniformManager::UniformHandle fImageIncrementUni; |
bsalomon@google.com | b4a55b7 | 2012-11-02 20:45:37 +0000 | [diff] [blame] | 301 | GrGLEffectMatrix fEffectMatrix; |
senorblanco@chromium.org | 84207c4 | 2012-08-22 20:51:19 +0000 | [diff] [blame] | 302 | |
bsalomon@google.com | 47d7a88 | 2012-10-29 12:47:51 +0000 | [diff] [blame] | 303 | typedef GrGLEffect INHERITED; |
senorblanco@chromium.org | 84207c4 | 2012-08-22 20:51:19 +0000 | [diff] [blame] | 304 | }; |
| 305 | |
bsalomon@google.com | 396e61f | 2012-10-25 19:00:29 +0000 | [diff] [blame] | 306 | GrGLMorphologyEffect::GrGLMorphologyEffect(const GrBackendEffectFactory& factory, |
bsalomon@google.com | 6340a41 | 2013-01-22 19:55:59 +0000 | [diff] [blame] | 307 | const GrEffectRef& effect) |
bsalomon@google.com | 374e759 | 2012-10-23 17:30:45 +0000 | [diff] [blame] | 308 | : INHERITED(factory) |
senorblanco@chromium.org | 84207c4 | 2012-08-22 20:51:19 +0000 | [diff] [blame] | 309 | , fImageIncrementUni(GrGLUniformManager::kInvalidUniformHandle) { |
bsalomon@google.com | 6340a41 | 2013-01-22 19:55:59 +0000 | [diff] [blame] | 310 | const GrMorphologyEffect& m = CastEffect<GrMorphologyEffect>(effect); |
senorblanco@chromium.org | 84207c4 | 2012-08-22 20:51:19 +0000 | [diff] [blame] | 311 | fRadius = m.radius(); |
| 312 | fType = m.type(); |
| 313 | } |
| 314 | |
bsalomon@google.com | 47d7a88 | 2012-10-29 12:47:51 +0000 | [diff] [blame] | 315 | void GrGLMorphologyEffect::emitCode(GrGLShaderBuilder* builder, |
bsalomon@google.com | 2eaaefd | 2012-10-29 19:51:22 +0000 | [diff] [blame] | 316 | const GrEffectStage&, |
bsalomon@google.com | b4a55b7 | 2012-11-02 20:45:37 +0000 | [diff] [blame] | 317 | EffectKey key, |
bsalomon@google.com | 47d7a88 | 2012-10-29 12:47:51 +0000 | [diff] [blame] | 318 | const char* vertexCoords, |
| 319 | const char* outputColor, |
| 320 | const char* inputColor, |
| 321 | const TextureSamplerArray& samplers) { |
bsalomon@google.com | b4a55b7 | 2012-11-02 20:45:37 +0000 | [diff] [blame] | 322 | const char* coords; |
| 323 | fEffectMatrix.emitCodeMakeFSCoords2D(builder, key, vertexCoords, &coords); |
senorblanco@chromium.org | 84207c4 | 2012-08-22 20:51:19 +0000 | [diff] [blame] | 324 | fImageIncrementUni = builder->addUniform(GrGLShaderBuilder::kFragment_ShaderType, |
| 325 | kVec2f_GrSLType, "ImageIncrement"); |
senorblanco@chromium.org | 84207c4 | 2012-08-22 20:51:19 +0000 | [diff] [blame] | 326 | |
senorblanco@chromium.org | 84207c4 | 2012-08-22 20:51:19 +0000 | [diff] [blame] | 327 | const char* func; |
| 328 | switch (fType) { |
| 329 | case GrMorphologyEffect::kErode_MorphologyType: |
bsalomon@google.com | f910d3b | 2013-03-07 17:06:57 +0000 | [diff] [blame] | 330 | 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] | 331 | func = "min"; |
| 332 | break; |
| 333 | case GrMorphologyEffect::kDilate_MorphologyType: |
bsalomon@google.com | f910d3b | 2013-03-07 17:06:57 +0000 | [diff] [blame] | 334 | 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] | 335 | func = "max"; |
| 336 | break; |
| 337 | default: |
| 338 | GrCrash("Unexpected type"); |
| 339 | func = ""; // suppress warning |
| 340 | break; |
| 341 | } |
| 342 | const char* imgInc = builder->getUniformCStr(fImageIncrementUni); |
| 343 | |
bsalomon@google.com | f910d3b | 2013-03-07 17:06:57 +0000 | [diff] [blame] | 344 | builder->fsCodeAppendf("\t\tvec2 coord = %s - %d.0 * %s;\n", coords, fRadius, imgInc); |
| 345 | builder->fsCodeAppendf("\t\tfor (int i = 0; i < %d; i++) {\n", this->width()); |
| 346 | builder->fsCodeAppendf("\t\t\t%s = %s(%s, ", outputColor, func, outputColor); |
| 347 | builder->appendTextureLookup(GrGLShaderBuilder::kFragment_ShaderType, samplers[0], "coord"); |
| 348 | builder->fsCodeAppend(");\n"); |
| 349 | builder->fsCodeAppendf("\t\t\tcoord += %s;\n", imgInc); |
| 350 | builder->fsCodeAppend("\t\t}\n"); |
| 351 | SkString modulate; |
| 352 | GrGLSLMulVarBy4f(&modulate, 2, outputColor, inputColor); |
| 353 | builder->fsCodeAppend(modulate.c_str()); |
senorblanco@chromium.org | 84207c4 | 2012-08-22 20:51:19 +0000 | [diff] [blame] | 354 | } |
| 355 | |
bsalomon@google.com | 2eaaefd | 2012-10-29 19:51:22 +0000 | [diff] [blame] | 356 | GrGLEffect::EffectKey GrGLMorphologyEffect::GenKey(const GrEffectStage& s, const GrGLCaps&) { |
bsalomon@google.com | 6340a41 | 2013-01-22 19:55:59 +0000 | [diff] [blame] | 357 | const GrMorphologyEffect& m = GetEffectFromStage<GrMorphologyEffect>(s); |
bsalomon@google.com | 46fba0d | 2012-10-25 21:42:05 +0000 | [diff] [blame] | 358 | EffectKey key = static_cast<EffectKey>(m.radius()); |
senorblanco@chromium.org | 84207c4 | 2012-08-22 20:51:19 +0000 | [diff] [blame] | 359 | key |= (m.type() << 8); |
bsalomon@google.com | b4a55b7 | 2012-11-02 20:45:37 +0000 | [diff] [blame] | 360 | key <<= GrGLEffectMatrix::kKeyBits; |
| 361 | EffectKey matrixKey = GrGLEffectMatrix::GenKey(m.getMatrix(), |
| 362 | s.getCoordChangeMatrix(), |
| 363 | m.texture(0)); |
| 364 | return key | matrixKey; |
senorblanco@chromium.org | 84207c4 | 2012-08-22 20:51:19 +0000 | [diff] [blame] | 365 | } |
| 366 | |
bsalomon@google.com | 28a15fb | 2012-10-26 17:53:18 +0000 | [diff] [blame] | 367 | void GrGLMorphologyEffect::setData(const GrGLUniformManager& uman, const GrEffectStage& stage) { |
bsalomon@google.com | 6340a41 | 2013-01-22 19:55:59 +0000 | [diff] [blame] | 368 | const Gr1DKernelEffect& kern = GetEffectFromStage<Gr1DKernelEffect>(stage); |
bsalomon@google.com | 2d0bade | 2012-10-26 19:01:17 +0000 | [diff] [blame] | 369 | GrTexture& texture = *kern.texture(0); |
senorblanco@chromium.org | 84207c4 | 2012-08-22 20:51:19 +0000 | [diff] [blame] | 370 | // the code we generated was for a specific kernel radius |
| 371 | GrAssert(kern.radius() == fRadius); |
| 372 | float imageIncrement[2] = { 0 }; |
| 373 | switch (kern.direction()) { |
| 374 | case Gr1DKernelEffect::kX_Direction: |
| 375 | imageIncrement[0] = 1.0f / texture.width(); |
| 376 | break; |
| 377 | case Gr1DKernelEffect::kY_Direction: |
| 378 | imageIncrement[1] = 1.0f / texture.height(); |
| 379 | break; |
| 380 | default: |
| 381 | GrCrash("Unknown filter direction."); |
| 382 | } |
| 383 | uman.set2fv(fImageIncrementUni, 0, 1, imageIncrement); |
bsalomon@google.com | b4a55b7 | 2012-11-02 20:45:37 +0000 | [diff] [blame] | 384 | fEffectMatrix.setData(uman, kern.getMatrix(), stage.getCoordChangeMatrix(), kern.texture(0)); |
senorblanco@chromium.org | 84207c4 | 2012-08-22 20:51:19 +0000 | [diff] [blame] | 385 | } |
| 386 | |
| 387 | /////////////////////////////////////////////////////////////////////////////// |
| 388 | |
| 389 | GrMorphologyEffect::GrMorphologyEffect(GrTexture* texture, |
| 390 | Direction direction, |
| 391 | int radius, |
| 392 | MorphologyType type) |
| 393 | : Gr1DKernelEffect(texture, direction, radius) |
| 394 | , fType(type) { |
| 395 | } |
| 396 | |
| 397 | GrMorphologyEffect::~GrMorphologyEffect() { |
| 398 | } |
| 399 | |
bsalomon@google.com | 396e61f | 2012-10-25 19:00:29 +0000 | [diff] [blame] | 400 | const GrBackendEffectFactory& GrMorphologyEffect::getFactory() const { |
| 401 | return GrTBackendEffectFactory<GrMorphologyEffect>::getInstance(); |
senorblanco@chromium.org | 84207c4 | 2012-08-22 20:51:19 +0000 | [diff] [blame] | 402 | } |
| 403 | |
bsalomon@google.com | 8a252f7 | 2013-01-22 20:35:13 +0000 | [diff] [blame] | 404 | bool GrMorphologyEffect::onIsEqual(const GrEffect& sBase) const { |
bsalomon@google.com | 6340a41 | 2013-01-22 19:55:59 +0000 | [diff] [blame] | 405 | const GrMorphologyEffect& s = CastEffect<GrMorphologyEffect>(sBase); |
bsalomon@google.com | 68b58c9 | 2013-01-17 16:50:08 +0000 | [diff] [blame] | 406 | return (this->texture(0) == s.texture(0) && |
senorblanco@chromium.org | 84207c4 | 2012-08-22 20:51:19 +0000 | [diff] [blame] | 407 | this->radius() == s.radius() && |
| 408 | this->direction() == s.direction() && |
| 409 | this->type() == s.type()); |
| 410 | } |
| 411 | |
bsalomon@google.com | 68b58c9 | 2013-01-17 16:50:08 +0000 | [diff] [blame] | 412 | void GrMorphologyEffect::getConstantColorComponents(GrColor* color, uint32_t* validFlags) const { |
| 413 | // This is valid because the color components of the result of the kernel all come |
| 414 | // exactly from existing values in the source texture. |
| 415 | this->updateConstantColorComponentsForModulation(color, validFlags); |
| 416 | } |
| 417 | |
senorblanco@chromium.org | 84207c4 | 2012-08-22 20:51:19 +0000 | [diff] [blame] | 418 | /////////////////////////////////////////////////////////////////////////////// |
| 419 | |
bsalomon@google.com | f271cc7 | 2012-10-24 19:35:13 +0000 | [diff] [blame] | 420 | GR_DEFINE_EFFECT_TEST(GrMorphologyEffect); |
senorblanco@chromium.org | 84207c4 | 2012-08-22 20:51:19 +0000 | [diff] [blame] | 421 | |
bsalomon@google.com | 73a9694 | 2013-02-13 16:31:19 +0000 | [diff] [blame] | 422 | GrEffectRef* GrMorphologyEffect::TestCreate(SkMWCRandom* random, |
sugoi@google.com | e0e385c | 2013-03-11 18:50:03 +0000 | [diff] [blame^] | 423 | GrContext*, |
bsalomon@google.com | 0ac6af4 | 2013-01-16 15:16:18 +0000 | [diff] [blame] | 424 | GrTexture* textures[]) { |
bsalomon@google.com | 6f261be | 2012-10-24 19:07:10 +0000 | [diff] [blame] | 425 | int texIdx = random->nextBool() ? GrEffectUnitTest::kSkiaPMTextureIdx : |
| 426 | GrEffectUnitTest::kAlphaTextureIdx; |
senorblanco@chromium.org | 84207c4 | 2012-08-22 20:51:19 +0000 | [diff] [blame] | 427 | Direction dir = random->nextBool() ? kX_Direction : kY_Direction; |
| 428 | static const int kMaxRadius = 10; |
| 429 | int radius = random->nextRangeU(1, kMaxRadius); |
| 430 | MorphologyType type = random->nextBool() ? GrMorphologyEffect::kErode_MorphologyType : |
| 431 | GrMorphologyEffect::kDilate_MorphologyType; |
| 432 | |
bsalomon@google.com | 0ac6af4 | 2013-01-16 15:16:18 +0000 | [diff] [blame] | 433 | return GrMorphologyEffect::Create(textures[texIdx], dir, radius, type); |
senorblanco@chromium.org | 84207c4 | 2012-08-22 20:51:19 +0000 | [diff] [blame] | 434 | } |
| 435 | |
| 436 | namespace { |
| 437 | |
| 438 | void apply_morphology_pass(GrContext* context, |
| 439 | GrTexture* texture, |
senorblanco@chromium.org | c2594f4 | 2013-01-30 19:08:47 +0000 | [diff] [blame] | 440 | const SkIRect& rect, |
senorblanco@chromium.org | 84207c4 | 2012-08-22 20:51:19 +0000 | [diff] [blame] | 441 | int radius, |
| 442 | GrMorphologyEffect::MorphologyType morphType, |
| 443 | Gr1DKernelEffect::Direction direction) { |
senorblanco@chromium.org | 84207c4 | 2012-08-22 20:51:19 +0000 | [diff] [blame] | 444 | GrPaint paint; |
bsalomon@google.com | 0ac6af4 | 2013-01-16 15:16:18 +0000 | [diff] [blame] | 445 | paint.colorStage(0)->setEffect(GrMorphologyEffect::Create(texture, |
| 446 | direction, |
| 447 | radius, |
| 448 | morphType))->unref(); |
senorblanco@chromium.org | c2594f4 | 2013-01-30 19:08:47 +0000 | [diff] [blame] | 449 | context->drawRect(paint, SkRect::MakeFromIRect(rect)); |
senorblanco@chromium.org | 84207c4 | 2012-08-22 20:51:19 +0000 | [diff] [blame] | 450 | } |
| 451 | |
| 452 | GrTexture* apply_morphology(GrTexture* srcTexture, |
senorblanco@chromium.org | c2594f4 | 2013-01-30 19:08:47 +0000 | [diff] [blame] | 453 | const SkIRect& rect, |
senorblanco@chromium.org | 84207c4 | 2012-08-22 20:51:19 +0000 | [diff] [blame] | 454 | GrMorphologyEffect::MorphologyType morphType, |
| 455 | SkISize radius) { |
| 456 | GrContext* context = srcTexture->getContext(); |
| 457 | srcTexture->ref(); |
bsalomon@google.com | 3cbaa2d | 2012-10-12 14:51:52 +0000 | [diff] [blame] | 458 | |
| 459 | GrContext::AutoMatrix am; |
| 460 | am.setIdentity(context); |
| 461 | |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 462 | GrContext::AutoClip acs(context, GrRect::MakeWH(SkIntToScalar(srcTexture->width()), |
senorblanco@chromium.org | 84207c4 | 2012-08-22 20:51:19 +0000 | [diff] [blame] | 463 | SkIntToScalar(srcTexture->height()))); |
bsalomon@google.com | 3cbaa2d | 2012-10-12 14:51:52 +0000 | [diff] [blame] | 464 | |
senorblanco@chromium.org | 84207c4 | 2012-08-22 20:51:19 +0000 | [diff] [blame] | 465 | GrTextureDesc desc; |
| 466 | desc.fFlags = kRenderTarget_GrTextureFlagBit | kNoStencil_GrTextureFlagBit; |
senorblanco@chromium.org | c2594f4 | 2013-01-30 19:08:47 +0000 | [diff] [blame] | 467 | desc.fWidth = rect.width(); |
| 468 | desc.fHeight = rect.height(); |
| 469 | desc.fConfig = kSkia8888_GrPixelConfig; |
bsalomon@google.com | 3cbaa2d | 2012-10-12 14:51:52 +0000 | [diff] [blame] | 470 | |
senorblanco@chromium.org | 84207c4 | 2012-08-22 20:51:19 +0000 | [diff] [blame] | 471 | if (radius.fWidth > 0) { |
| 472 | GrAutoScratchTexture ast(context, desc); |
| 473 | GrContext::AutoRenderTarget art(context, ast.texture()->asRenderTarget()); |
| 474 | apply_morphology_pass(context, srcTexture, rect, radius.fWidth, |
| 475 | morphType, Gr1DKernelEffect::kX_Direction); |
senorblanco@chromium.org | c2594f4 | 2013-01-30 19:08:47 +0000 | [diff] [blame] | 476 | SkIRect clearRect = SkIRect::MakeXYWH(rect.fLeft, rect.fBottom, |
| 477 | rect.width(), radius.fHeight); |
senorblanco@chromium.org | 84207c4 | 2012-08-22 20:51:19 +0000 | [diff] [blame] | 478 | context->clear(&clearRect, 0x0); |
| 479 | srcTexture->unref(); |
| 480 | srcTexture = ast.detach(); |
| 481 | } |
| 482 | if (radius.fHeight > 0) { |
| 483 | GrAutoScratchTexture ast(context, desc); |
| 484 | GrContext::AutoRenderTarget art(context, ast.texture()->asRenderTarget()); |
| 485 | apply_morphology_pass(context, srcTexture, rect, radius.fHeight, |
| 486 | morphType, Gr1DKernelEffect::kY_Direction); |
| 487 | srcTexture->unref(); |
| 488 | srcTexture = ast.detach(); |
| 489 | } |
| 490 | return srcTexture; |
| 491 | } |
| 492 | |
| 493 | }; |
| 494 | |
senorblanco@chromium.org | c2594f4 | 2013-01-30 19:08:47 +0000 | [diff] [blame] | 495 | bool SkDilateImageFilter::filterImageGPU(Proxy* proxy, const SkBitmap& src, SkBitmap* result) { |
| 496 | SkBitmap inputBM; |
| 497 | if (!SkImageFilterUtils::GetInputResultGPU(getInput(0), proxy, src, &inputBM)) { |
| 498 | return false; |
| 499 | } |
| 500 | GrTexture* input = (GrTexture*) inputBM.getTexture(); |
| 501 | SkIRect bounds; |
| 502 | src.getBounds(&bounds); |
| 503 | SkAutoTUnref<GrTexture> resultTex(apply_morphology(input, bounds, |
| 504 | GrMorphologyEffect::kDilate_MorphologyType, radius())); |
| 505 | return SkImageFilterUtils::WrapTexture(resultTex, src.width(), src.height(), result); |
senorblanco@chromium.org | 05054f1 | 2012-03-02 21:05:45 +0000 | [diff] [blame] | 506 | } |
| 507 | |
senorblanco@chromium.org | c2594f4 | 2013-01-30 19:08:47 +0000 | [diff] [blame] | 508 | bool SkErodeImageFilter::filterImageGPU(Proxy* proxy, const SkBitmap& src, SkBitmap* result) { |
| 509 | SkBitmap inputBM; |
| 510 | if (!SkImageFilterUtils::GetInputResultGPU(getInput(0), proxy, src, &inputBM)) { |
| 511 | return false; |
| 512 | } |
| 513 | GrTexture* input = (GrTexture*) inputBM.getTexture(); |
| 514 | SkIRect bounds; |
| 515 | src.getBounds(&bounds); |
| 516 | SkAutoTUnref<GrTexture> resultTex(apply_morphology(input, bounds, |
| 517 | GrMorphologyEffect::kErode_MorphologyType, radius())); |
| 518 | return SkImageFilterUtils::WrapTexture(resultTex, src.width(), src.height(), result); |
senorblanco@chromium.org | 05054f1 | 2012-03-02 21:05:45 +0000 | [diff] [blame] | 519 | } |
| 520 | |
senorblanco@chromium.org | 84207c4 | 2012-08-22 20:51:19 +0000 | [diff] [blame] | 521 | #endif |