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" |
senorblanco | 1d3ff43 | 2015-10-20 10:17:34 -0700 | [diff] [blame] | 11 | #include "SkDevice.h" |
mtklein | d029ded | 2015-08-04 14:09:09 -0700 | [diff] [blame] | 12 | #include "SkOpts.h" |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 13 | #include "SkReadBuffer.h" |
djsollen@google.com | c73dd5c | 2012-08-07 15:54:32 +0000 | [diff] [blame] | 14 | #include "SkRect.h" |
mtklein | d029ded | 2015-08-04 14:09:09 -0700 | [diff] [blame] | 15 | #include "SkWriteBuffer.h" |
bsalomon@google.com | cf8fb1f | 2012-08-02 14:03:32 +0000 | [diff] [blame] | 16 | #if SK_SUPPORT_GPU |
senorblanco@chromium.org | 302cffb | 2012-08-01 20:16:34 +0000 | [diff] [blame] | 17 | #include "GrContext.h" |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 18 | #include "GrDrawContext.h" |
egdaniel | 605dd0f | 2014-11-12 08:35:25 -0800 | [diff] [blame] | 19 | #include "GrInvariantOutput.h" |
senorblanco@chromium.org | 302cffb | 2012-08-01 20:16:34 +0000 | [diff] [blame] | 20 | #include "GrTexture.h" |
robertphillips | 1de87df | 2016-01-14 06:03:29 -0800 | [diff] [blame] | 21 | #include "SkGr.h" |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 22 | #include "effects/Gr1DKernelEffect.h" |
egdaniel | 64c4728 | 2015-11-13 06:54:19 -0800 | [diff] [blame] | 23 | #include "glsl/GrGLSLFragmentProcessor.h" |
egdaniel | 2d721d3 | 2015-11-11 13:06:05 -0800 | [diff] [blame] | 24 | #include "glsl/GrGLSLFragmentShaderBuilder.h" |
egdaniel | 018fb62 | 2015-10-28 07:26:40 -0700 | [diff] [blame] | 25 | #include "glsl/GrGLSLProgramDataManager.h" |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 26 | #include "glsl/GrGLSLUniformHandler.h" |
bsalomon@google.com | cf8fb1f | 2012-08-02 14:03:32 +0000 | [diff] [blame] | 27 | #endif |
senorblanco@chromium.org | 05054f1 | 2012-03-02 21:05:45 +0000 | [diff] [blame] | 28 | |
senorblanco@chromium.org | 2bfe36b | 2014-01-20 19:58:28 +0000 | [diff] [blame] | 29 | SkMorphologyImageFilter::SkMorphologyImageFilter(int radiusX, |
| 30 | int radiusY, |
| 31 | SkImageFilter* input, |
senorblanco | 24e06d5 | 2015-03-18 12:11:33 -0700 | [diff] [blame] | 32 | const CropRect* cropRect) |
| 33 | : INHERITED(1, &input, cropRect), fRadius(SkISize::Make(radiusX, radiusY)) { |
senorblanco@chromium.org | 05054f1 | 2012-03-02 21:05:45 +0000 | [diff] [blame] | 34 | } |
| 35 | |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 36 | void SkMorphologyImageFilter::flatten(SkWriteBuffer& buffer) const { |
senorblanco@chromium.org | 05054f1 | 2012-03-02 21:05:45 +0000 | [diff] [blame] | 37 | this->INHERITED::flatten(buffer); |
tomhudson@google.com | 7558925 | 2012-04-10 17:42:21 +0000 | [diff] [blame] | 38 | buffer.writeInt(fRadius.fWidth); |
| 39 | buffer.writeInt(fRadius.fHeight); |
senorblanco@chromium.org | 05054f1 | 2012-03-02 21:05:45 +0000 | [diff] [blame] | 40 | } |
| 41 | |
senorblanco@chromium.org | 0ded88d | 2014-01-24 15:43:50 +0000 | [diff] [blame] | 42 | static void callProcX(SkMorphologyImageFilter::Proc procX, const SkBitmap& src, SkBitmap* dst, int radiusX, const SkIRect& bounds) |
senorblanco@chromium.org | 05054f1 | 2012-03-02 21:05:45 +0000 | [diff] [blame] | 43 | { |
senorblanco@chromium.org | 0ded88d | 2014-01-24 15:43:50 +0000 | [diff] [blame] | 44 | procX(src.getAddr32(bounds.left(), bounds.top()), dst->getAddr32(0, 0), |
| 45 | radiusX, bounds.width(), bounds.height(), |
| 46 | src.rowBytesAsPixels(), dst->rowBytesAsPixels()); |
senorblanco@chromium.org | 05054f1 | 2012-03-02 21:05:45 +0000 | [diff] [blame] | 47 | } |
| 48 | |
senorblanco@chromium.org | 0ded88d | 2014-01-24 15:43:50 +0000 | [diff] [blame] | 49 | static void callProcY(SkMorphologyImageFilter::Proc procY, const SkBitmap& src, SkBitmap* dst, int radiusY, const SkIRect& bounds) |
senorblanco@chromium.org | 05054f1 | 2012-03-02 21:05:45 +0000 | [diff] [blame] | 50 | { |
senorblanco@chromium.org | 0ded88d | 2014-01-24 15:43:50 +0000 | [diff] [blame] | 51 | procY(src.getAddr32(bounds.left(), bounds.top()), dst->getAddr32(0, 0), |
| 52 | radiusY, bounds.height(), bounds.width(), |
| 53 | src.rowBytesAsPixels(), dst->rowBytesAsPixels()); |
| 54 | } |
| 55 | |
| 56 | bool SkMorphologyImageFilter::filterImageGeneric(SkMorphologyImageFilter::Proc procX, |
| 57 | SkMorphologyImageFilter::Proc procY, |
| 58 | Proxy* proxy, |
| 59 | const SkBitmap& source, |
senorblanco@chromium.org | 4cb543d | 2014-03-14 15:44:01 +0000 | [diff] [blame] | 60 | const Context& ctx, |
senorblanco@chromium.org | 0ded88d | 2014-01-24 15:43:50 +0000 | [diff] [blame] | 61 | SkBitmap* dst, |
commit-bot@chromium.org | ae761f7 | 2014-02-05 22:32:02 +0000 | [diff] [blame] | 62 | SkIPoint* offset) const { |
senorblanco@chromium.org | 0ded88d | 2014-01-24 15:43:50 +0000 | [diff] [blame] | 63 | SkBitmap src = source; |
| 64 | SkIPoint srcOffset = SkIPoint::Make(0, 0); |
senorblanco | b9519f8 | 2015-10-15 12:15:13 -0700 | [diff] [blame] | 65 | if (!this->filterInput(0, proxy, source, ctx, &src, &srcOffset)) { |
senorblanco@chromium.org | 0ded88d | 2014-01-24 15:43:50 +0000 | [diff] [blame] | 66 | return false; |
skia.committer@gmail.com | 1878a44 | 2014-01-23 18:48:56 +0000 | [diff] [blame] | 67 | } |
senorblanco@chromium.org | 0ded88d | 2014-01-24 15:43:50 +0000 | [diff] [blame] | 68 | |
commit-bot@chromium.org | 28fcae2 | 2014-04-11 17:15:40 +0000 | [diff] [blame] | 69 | if (src.colorType() != kN32_SkColorType) { |
senorblanco@chromium.org | 0ded88d | 2014-01-24 15:43:50 +0000 | [diff] [blame] | 70 | return false; |
| 71 | } |
| 72 | |
| 73 | SkIRect bounds; |
senorblanco | db64af3 | 2015-12-09 10:11:43 -0800 | [diff] [blame] | 74 | if (!this->applyCropRect(this->mapContext(ctx), proxy, src, &srcOffset, &bounds, &src)) { |
senorblanco@chromium.org | 0ded88d | 2014-01-24 15:43:50 +0000 | [diff] [blame] | 75 | return false; |
| 76 | } |
| 77 | |
| 78 | SkAutoLockPixels alp(src); |
| 79 | if (!src.getPixels()) { |
| 80 | return false; |
| 81 | } |
| 82 | |
senorblanco@chromium.org | 0ded88d | 2014-01-24 15:43:50 +0000 | [diff] [blame] | 83 | SkVector radius = SkVector::Make(SkIntToScalar(this->radius().width()), |
| 84 | SkIntToScalar(this->radius().height())); |
senorblanco@chromium.org | 4cb543d | 2014-03-14 15:44:01 +0000 | [diff] [blame] | 85 | ctx.ctm().mapVectors(&radius, 1); |
senorblanco@chromium.org | 0ded88d | 2014-01-24 15:43:50 +0000 | [diff] [blame] | 86 | int width = SkScalarFloorToInt(radius.fX); |
| 87 | int height = SkScalarFloorToInt(radius.fY); |
| 88 | |
| 89 | if (width < 0 || height < 0) { |
| 90 | return false; |
| 91 | } |
| 92 | |
| 93 | SkIRect srcBounds = bounds; |
| 94 | srcBounds.offset(-srcOffset); |
| 95 | |
| 96 | if (width == 0 && height == 0) { |
| 97 | src.extractSubset(dst, srcBounds); |
| 98 | offset->fX = bounds.left(); |
| 99 | offset->fY = bounds.top(); |
| 100 | return true; |
| 101 | } |
| 102 | |
senorblanco | 1d3ff43 | 2015-10-20 10:17:34 -0700 | [diff] [blame] | 103 | SkAutoTUnref<SkBaseDevice> device(proxy->createDevice(bounds.width(), bounds.height())); |
| 104 | if (!device) { |
senorblanco@chromium.org | 0ded88d | 2014-01-24 15:43:50 +0000 | [diff] [blame] | 105 | return false; |
| 106 | } |
senorblanco | 1d3ff43 | 2015-10-20 10:17:34 -0700 | [diff] [blame] | 107 | *dst = device->accessBitmap(false); |
| 108 | SkAutoLockPixels alp_dst(*dst); |
senorblanco@chromium.org | 0ded88d | 2014-01-24 15:43:50 +0000 | [diff] [blame] | 109 | |
| 110 | if (width > 0 && height > 0) { |
senorblanco | 1d3ff43 | 2015-10-20 10:17:34 -0700 | [diff] [blame] | 111 | SkAutoTUnref<SkBaseDevice> tempDevice(proxy->createDevice(dst->width(), dst->height())); |
| 112 | if (!tempDevice) { |
| 113 | return false; |
| 114 | } |
| 115 | SkBitmap temp = tempDevice->accessBitmap(false); |
| 116 | SkAutoLockPixels alp_temp(temp); |
senorblanco@chromium.org | 0ded88d | 2014-01-24 15:43:50 +0000 | [diff] [blame] | 117 | callProcX(procX, src, &temp, width, srcBounds); |
| 118 | SkIRect tmpBounds = SkIRect::MakeWH(srcBounds.width(), srcBounds.height()); |
| 119 | callProcY(procY, temp, dst, height, tmpBounds); |
| 120 | } else if (width > 0) { |
| 121 | callProcX(procX, src, dst, width, srcBounds); |
| 122 | } else if (height > 0) { |
| 123 | callProcY(procY, src, dst, height, srcBounds); |
| 124 | } |
| 125 | offset->fX = bounds.left(); |
| 126 | offset->fY = bounds.top(); |
| 127 | return true; |
senorblanco@chromium.org | 76d4d04 | 2014-01-23 18:45:23 +0000 | [diff] [blame] | 128 | } |
| 129 | |
skia.committer@gmail.com | 1878a44 | 2014-01-23 18:48:56 +0000 | [diff] [blame] | 130 | bool SkErodeImageFilter::onFilterImage(Proxy* proxy, |
senorblanco@chromium.org | 4cb543d | 2014-03-14 15:44:01 +0000 | [diff] [blame] | 131 | const SkBitmap& source, const Context& ctx, |
commit-bot@chromium.org | ae761f7 | 2014-02-05 22:32:02 +0000 | [diff] [blame] | 132 | SkBitmap* dst, SkIPoint* offset) const { |
mtklein | d029ded | 2015-08-04 14:09:09 -0700 | [diff] [blame] | 133 | return this->filterImageGeneric(SkOpts::erode_x, SkOpts::erode_y, |
| 134 | proxy, source, ctx, dst, offset); |
senorblanco@chromium.org | 05054f1 | 2012-03-02 21:05:45 +0000 | [diff] [blame] | 135 | } |
| 136 | |
senorblanco@chromium.org | f1369ce | 2012-08-20 14:53:21 +0000 | [diff] [blame] | 137 | bool SkDilateImageFilter::onFilterImage(Proxy* proxy, |
senorblanco@chromium.org | 4cb543d | 2014-03-14 15:44:01 +0000 | [diff] [blame] | 138 | const SkBitmap& source, const Context& ctx, |
commit-bot@chromium.org | ae761f7 | 2014-02-05 22:32:02 +0000 | [diff] [blame] | 139 | SkBitmap* dst, SkIPoint* offset) const { |
mtklein | d029ded | 2015-08-04 14:09:09 -0700 | [diff] [blame] | 140 | return this->filterImageGeneric(SkOpts::dilate_x, SkOpts::dilate_y, |
| 141 | proxy, source, ctx, dst, offset); |
senorblanco@chromium.org | 05054f1 | 2012-03-02 21:05:45 +0000 | [diff] [blame] | 142 | } |
| 143 | |
senorblanco@chromium.org | 336d1d7 | 2014-01-27 21:03:17 +0000 | [diff] [blame] | 144 | void SkMorphologyImageFilter::computeFastBounds(const SkRect& src, SkRect* dst) const { |
robertphillips | ff0ca5e | 2015-07-22 11:54:44 -0700 | [diff] [blame] | 145 | if (this->getInput(0)) { |
| 146 | this->getInput(0)->computeFastBounds(src, dst); |
senorblanco@chromium.org | 336d1d7 | 2014-01-27 21:03:17 +0000 | [diff] [blame] | 147 | } else { |
| 148 | *dst = src; |
| 149 | } |
| 150 | dst->outset(SkIntToScalar(fRadius.width()), SkIntToScalar(fRadius.height())); |
| 151 | } |
| 152 | |
senorblanco | db64af3 | 2015-12-09 10:11:43 -0800 | [diff] [blame] | 153 | void SkMorphologyImageFilter::onFilterNodeBounds(const SkIRect& src, const SkMatrix& ctm, |
| 154 | SkIRect* dst, MapDirection) const { |
| 155 | *dst = src; |
senorblanco@chromium.org | c4b12f1 | 2014-02-05 17:51:22 +0000 | [diff] [blame] | 156 | SkVector radius = SkVector::Make(SkIntToScalar(this->radius().width()), |
| 157 | SkIntToScalar(this->radius().height())); |
| 158 | ctm.mapVectors(&radius, 1); |
senorblanco | db64af3 | 2015-12-09 10:11:43 -0800 | [diff] [blame] | 159 | dst->outset(SkScalarCeilToInt(radius.x()), SkScalarCeilToInt(radius.y())); |
senorblanco@chromium.org | c4b12f1 | 2014-02-05 17:51:22 +0000 | [diff] [blame] | 160 | } |
| 161 | |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 162 | SkFlattenable* SkErodeImageFilter::CreateProc(SkReadBuffer& buffer) { |
| 163 | SK_IMAGEFILTER_UNFLATTEN_COMMON(common, 1); |
| 164 | const int width = buffer.readInt(); |
| 165 | const int height = buffer.readInt(); |
senorblanco | 24e06d5 | 2015-03-18 12:11:33 -0700 | [diff] [blame] | 166 | return Create(width, height, common.getInput(0), &common.cropRect()); |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | SkFlattenable* SkDilateImageFilter::CreateProc(SkReadBuffer& buffer) { |
| 170 | SK_IMAGEFILTER_UNFLATTEN_COMMON(common, 1); |
| 171 | const int width = buffer.readInt(); |
| 172 | const int height = buffer.readInt(); |
senorblanco | 24e06d5 | 2015-03-18 12:11:33 -0700 | [diff] [blame] | 173 | return Create(width, height, common.getInput(0), &common.cropRect()); |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 174 | } |
| 175 | |
robertphillips | f3f5bad | 2014-12-19 13:49:15 -0800 | [diff] [blame] | 176 | #ifndef SK_IGNORE_TO_STRING |
| 177 | void SkErodeImageFilter::toString(SkString* str) const { |
| 178 | str->appendf("SkErodeImageFilter: ("); |
| 179 | str->appendf("radius: (%d,%d)", this->radius().fWidth, this->radius().fHeight); |
| 180 | str->append(")"); |
| 181 | } |
| 182 | #endif |
| 183 | |
| 184 | #ifndef SK_IGNORE_TO_STRING |
| 185 | void SkDilateImageFilter::toString(SkString* str) const { |
| 186 | str->appendf("SkDilateImageFilter: ("); |
| 187 | str->appendf("radius: (%d,%d)", this->radius().fWidth, this->radius().fHeight); |
| 188 | str->append(")"); |
| 189 | } |
| 190 | #endif |
| 191 | |
bsalomon@google.com | cf8fb1f | 2012-08-02 14:03:32 +0000 | [diff] [blame] | 192 | #if SK_SUPPORT_GPU |
senorblanco@chromium.org | 84207c4 | 2012-08-22 20:51:19 +0000 | [diff] [blame] | 193 | |
| 194 | /////////////////////////////////////////////////////////////////////////////// |
senorblanco@chromium.org | 84207c4 | 2012-08-22 20:51:19 +0000 | [diff] [blame] | 195 | /** |
| 196 | * Morphology effects. Depending upon the type of morphology, either the |
| 197 | * component-wise min (Erode_Type) or max (Dilate_Type) of all pixels in the |
| 198 | * kernel is selected as the new color. The new color is modulated by the input |
| 199 | * color. |
| 200 | */ |
| 201 | class GrMorphologyEffect : public Gr1DKernelEffect { |
| 202 | |
| 203 | public: |
| 204 | |
| 205 | enum MorphologyType { |
| 206 | kErode_MorphologyType, |
| 207 | kDilate_MorphologyType, |
| 208 | }; |
| 209 | |
bsalomon | 4a33952 | 2015-10-06 08:40:50 -0700 | [diff] [blame] | 210 | static GrFragmentProcessor* Create(GrTexture* tex, Direction dir, int radius, |
| 211 | MorphologyType type) { |
| 212 | return new GrMorphologyEffect(tex, dir, radius, type); |
bsalomon@google.com | 0ac6af4 | 2013-01-16 15:16:18 +0000 | [diff] [blame] | 213 | } |
| 214 | |
bsalomon | 4a33952 | 2015-10-06 08:40:50 -0700 | [diff] [blame] | 215 | static GrFragmentProcessor* Create(GrTexture* tex, Direction dir, int radius, |
| 216 | MorphologyType type, float bounds[2]) { |
| 217 | return new GrMorphologyEffect(tex, dir, radius, type, bounds); |
cwallez | 80a61df | 2015-01-26 12:20:14 -0800 | [diff] [blame] | 218 | } |
| 219 | |
senorblanco@chromium.org | 84207c4 | 2012-08-22 20:51:19 +0000 | [diff] [blame] | 220 | virtual ~GrMorphologyEffect(); |
| 221 | |
| 222 | MorphologyType type() const { return fType; } |
cwallez | 80a61df | 2015-01-26 12:20:14 -0800 | [diff] [blame] | 223 | bool useRange() const { return fUseRange; } |
| 224 | const float* range() const { return fRange; } |
senorblanco@chromium.org | 84207c4 | 2012-08-22 20:51:19 +0000 | [diff] [blame] | 225 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 226 | const char* name() const override { return "Morphology"; } |
senorblanco@chromium.org | 84207c4 | 2012-08-22 20:51:19 +0000 | [diff] [blame] | 227 | |
senorblanco@chromium.org | 84207c4 | 2012-08-22 20:51:19 +0000 | [diff] [blame] | 228 | protected: |
| 229 | |
| 230 | MorphologyType fType; |
cwallez | 80a61df | 2015-01-26 12:20:14 -0800 | [diff] [blame] | 231 | bool fUseRange; |
| 232 | float fRange[2]; |
senorblanco@chromium.org | 84207c4 | 2012-08-22 20:51:19 +0000 | [diff] [blame] | 233 | |
| 234 | private: |
egdaniel | 57d3b03 | 2015-11-13 11:57:27 -0800 | [diff] [blame] | 235 | GrGLSLFragmentProcessor* onCreateGLSLInstance() const override; |
wangyix | b1daa86 | 2015-08-18 11:29:31 -0700 | [diff] [blame] | 236 | |
egdaniel | 57d3b03 | 2015-11-13 11:57:27 -0800 | [diff] [blame] | 237 | void onGetGLSLProcessorKey(const GrGLSLCaps&, GrProcessorKeyBuilder*) const override; |
wangyix | 4b3050b | 2015-08-04 07:59:37 -0700 | [diff] [blame] | 238 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 239 | bool onIsEqual(const GrFragmentProcessor&) const override; |
bsalomon@google.com | 68b58c9 | 2013-01-17 16:50:08 +0000 | [diff] [blame] | 240 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 241 | void onComputeInvariantOutput(GrInvariantOutput* inout) const override; |
egdaniel | 1a8ecdf | 2014-10-03 06:24:12 -0700 | [diff] [blame] | 242 | |
bsalomon | 4a33952 | 2015-10-06 08:40:50 -0700 | [diff] [blame] | 243 | GrMorphologyEffect(GrTexture*, Direction, int radius, MorphologyType); |
| 244 | GrMorphologyEffect(GrTexture*, Direction, int radius, MorphologyType, |
joshualitt | 5f10b5c | 2015-07-09 10:24:35 -0700 | [diff] [blame] | 245 | float bounds[2]); |
bsalomon@google.com | 0ac6af4 | 2013-01-16 15:16:18 +0000 | [diff] [blame] | 246 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 247 | GR_DECLARE_FRAGMENT_PROCESSOR_TEST; |
senorblanco@chromium.org | 84207c4 | 2012-08-22 20:51:19 +0000 | [diff] [blame] | 248 | |
| 249 | typedef Gr1DKernelEffect INHERITED; |
| 250 | }; |
| 251 | |
| 252 | /////////////////////////////////////////////////////////////////////////////// |
| 253 | |
egdaniel | 64c4728 | 2015-11-13 06:54:19 -0800 | [diff] [blame] | 254 | class GrGLMorphologyEffect : public GrGLSLFragmentProcessor { |
senorblanco@chromium.org | 84207c4 | 2012-08-22 20:51:19 +0000 | [diff] [blame] | 255 | public: |
robertphillips | 9cdb992 | 2016-02-03 12:25:40 -0800 | [diff] [blame] | 256 | void emitCode(EmitArgs&) override; |
senorblanco@chromium.org | 84207c4 | 2012-08-22 20:51:19 +0000 | [diff] [blame] | 257 | |
robertphillips | bf536af | 2016-02-04 06:11:53 -0800 | [diff] [blame^] | 258 | static inline void GenKey(const GrProcessor&, const GrGLSLCaps&, GrProcessorKeyBuilder*); |
senorblanco@chromium.org | 84207c4 | 2012-08-22 20:51:19 +0000 | [diff] [blame] | 259 | |
wangyix | b1daa86 | 2015-08-18 11:29:31 -0700 | [diff] [blame] | 260 | protected: |
egdaniel | 018fb62 | 2015-10-28 07:26:40 -0700 | [diff] [blame] | 261 | void onSetData(const GrGLSLProgramDataManager&, const GrProcessor&) override; |
senorblanco@chromium.org | 84207c4 | 2012-08-22 20:51:19 +0000 | [diff] [blame] | 262 | |
| 263 | private: |
egdaniel | 018fb62 | 2015-10-28 07:26:40 -0700 | [diff] [blame] | 264 | GrGLSLProgramDataManager::UniformHandle fPixelSizeUni; |
| 265 | GrGLSLProgramDataManager::UniformHandle fRangeUni; |
senorblanco@chromium.org | 84207c4 | 2012-08-22 20:51:19 +0000 | [diff] [blame] | 266 | |
egdaniel | 64c4728 | 2015-11-13 06:54:19 -0800 | [diff] [blame] | 267 | typedef GrGLSLFragmentProcessor INHERITED; |
senorblanco@chromium.org | 84207c4 | 2012-08-22 20:51:19 +0000 | [diff] [blame] | 268 | }; |
| 269 | |
wangyix | 7c157a9 | 2015-07-22 15:08:53 -0700 | [diff] [blame] | 270 | void GrGLMorphologyEffect::emitCode(EmitArgs& args) { |
robertphillips | bf536af | 2016-02-04 06:11:53 -0800 | [diff] [blame^] | 271 | const GrMorphologyEffect& me = args.fFp.cast<GrMorphologyEffect>(); |
| 272 | |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 273 | GrGLSLUniformHandler* uniformHandler = args.fUniformHandler; |
| 274 | fPixelSizeUni = uniformHandler->addUniform(GrGLSLUniformHandler::kFragment_Visibility, |
| 275 | kFloat_GrSLType, kDefault_GrSLPrecision, |
| 276 | "PixelSize"); |
| 277 | const char* pixelSizeInc = uniformHandler->getUniformCStr(fPixelSizeUni); |
| 278 | fRangeUni = uniformHandler->addUniform(GrGLSLUniformHandler::kFragment_Visibility, |
| 279 | kVec2f_GrSLType, kDefault_GrSLPrecision, |
| 280 | "Range"); |
| 281 | const char* range = uniformHandler->getUniformCStr(fRangeUni); |
senorblanco@chromium.org | 84207c4 | 2012-08-22 20:51:19 +0000 | [diff] [blame] | 282 | |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 283 | GrGLSLFragmentBuilder* fragBuilder = args.fFragBuilder; |
| 284 | SkString coords2D = fragBuilder->ensureFSCoords2D(args.fCoords, 0); |
senorblanco@chromium.org | 84207c4 | 2012-08-22 20:51:19 +0000 | [diff] [blame] | 285 | const char* func; |
robertphillips | bf536af | 2016-02-04 06:11:53 -0800 | [diff] [blame^] | 286 | switch (me.type()) { |
senorblanco@chromium.org | 84207c4 | 2012-08-22 20:51:19 +0000 | [diff] [blame] | 287 | case GrMorphologyEffect::kErode_MorphologyType: |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 288 | fragBuilder->codeAppendf("\t\t%s = vec4(1, 1, 1, 1);\n", args.fOutputColor); |
senorblanco@chromium.org | 84207c4 | 2012-08-22 20:51:19 +0000 | [diff] [blame] | 289 | func = "min"; |
| 290 | break; |
| 291 | case GrMorphologyEffect::kDilate_MorphologyType: |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 292 | fragBuilder->codeAppendf("\t\t%s = vec4(0, 0, 0, 0);\n", args.fOutputColor); |
senorblanco@chromium.org | 84207c4 | 2012-08-22 20:51:19 +0000 | [diff] [blame] | 293 | func = "max"; |
| 294 | break; |
| 295 | default: |
commit-bot@chromium.org | 88cb22b | 2014-04-30 14:17:00 +0000 | [diff] [blame] | 296 | SkFAIL("Unexpected type"); |
senorblanco@chromium.org | 84207c4 | 2012-08-22 20:51:19 +0000 | [diff] [blame] | 297 | func = ""; // suppress warning |
| 298 | break; |
| 299 | } |
senorblanco@chromium.org | 84207c4 | 2012-08-22 20:51:19 +0000 | [diff] [blame] | 300 | |
cwallez | 80a61df | 2015-01-26 12:20:14 -0800 | [diff] [blame] | 301 | const char* dir; |
robertphillips | bf536af | 2016-02-04 06:11:53 -0800 | [diff] [blame^] | 302 | switch (me.direction()) { |
cwallez | 80a61df | 2015-01-26 12:20:14 -0800 | [diff] [blame] | 303 | case Gr1DKernelEffect::kX_Direction: |
| 304 | dir = "x"; |
| 305 | break; |
| 306 | case Gr1DKernelEffect::kY_Direction: |
| 307 | dir = "y"; |
| 308 | break; |
| 309 | default: |
| 310 | SkFAIL("Unknown filter direction."); |
| 311 | dir = ""; // suppress warning |
| 312 | } |
| 313 | |
robertphillips | bf536af | 2016-02-04 06:11:53 -0800 | [diff] [blame^] | 314 | int width = GrMorphologyEffect::WidthFromRadius(me.radius()); |
| 315 | |
cwallez | 80a61df | 2015-01-26 12:20:14 -0800 | [diff] [blame] | 316 | // vec2 coord = coord2D; |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 317 | fragBuilder->codeAppendf("\t\tvec2 coord = %s;\n", coords2D.c_str()); |
cwallez | 80a61df | 2015-01-26 12:20:14 -0800 | [diff] [blame] | 318 | // coord.x -= radius * pixelSize; |
robertphillips | bf536af | 2016-02-04 06:11:53 -0800 | [diff] [blame^] | 319 | fragBuilder->codeAppendf("\t\tcoord.%s -= %d.0 * %s; \n", dir, me.radius(), pixelSizeInc); |
| 320 | if (me.useRange()) { |
cwallez | 80a61df | 2015-01-26 12:20:14 -0800 | [diff] [blame] | 321 | // highBound = min(highBound, coord.x + (width-1) * pixelSize); |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 322 | fragBuilder->codeAppendf("\t\tfloat highBound = min(%s.y, coord.%s + %f * %s);", |
robertphillips | bf536af | 2016-02-04 06:11:53 -0800 | [diff] [blame^] | 323 | range, dir, float(width - 1), pixelSizeInc); |
cwallez | 80a61df | 2015-01-26 12:20:14 -0800 | [diff] [blame] | 324 | // coord.x = max(lowBound, coord.x); |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 325 | fragBuilder->codeAppendf("\t\tcoord.%s = max(%s.x, coord.%s);", dir, range, dir); |
cwallez | 80a61df | 2015-01-26 12:20:14 -0800 | [diff] [blame] | 326 | } |
robertphillips | bf536af | 2016-02-04 06:11:53 -0800 | [diff] [blame^] | 327 | fragBuilder->codeAppendf("\t\tfor (int i = 0; i < %d; i++) {\n", width); |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 328 | fragBuilder->codeAppendf("\t\t\t%s = %s(%s, ", args.fOutputColor, func, args.fOutputColor); |
| 329 | fragBuilder->appendTextureLookup(args.fSamplers[0], "coord"); |
| 330 | fragBuilder->codeAppend(");\n"); |
cwallez | 80a61df | 2015-01-26 12:20:14 -0800 | [diff] [blame] | 331 | // coord.x += pixelSize; |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 332 | fragBuilder->codeAppendf("\t\t\tcoord.%s += %s;\n", dir, pixelSizeInc); |
robertphillips | bf536af | 2016-02-04 06:11:53 -0800 | [diff] [blame^] | 333 | if (me.useRange()) { |
cwallez | 80a61df | 2015-01-26 12:20:14 -0800 | [diff] [blame] | 334 | // coord.x = min(highBound, coord.x); |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 335 | fragBuilder->codeAppendf("\t\t\tcoord.%s = min(highBound, coord.%s);", dir, dir); |
cwallez | 80a61df | 2015-01-26 12:20:14 -0800 | [diff] [blame] | 336 | } |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 337 | fragBuilder->codeAppend("\t\t}\n"); |
bsalomon@google.com | f910d3b | 2013-03-07 17:06:57 +0000 | [diff] [blame] | 338 | SkString modulate; |
wangyix | 7c157a9 | 2015-07-22 15:08:53 -0700 | [diff] [blame] | 339 | GrGLSLMulVarBy4f(&modulate, args.fOutputColor, args.fInputColor); |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 340 | fragBuilder->codeAppend(modulate.c_str()); |
senorblanco@chromium.org | 84207c4 | 2012-08-22 20:51:19 +0000 | [diff] [blame] | 341 | } |
| 342 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 343 | void GrGLMorphologyEffect::GenKey(const GrProcessor& proc, |
jvanverth | cfc1886 | 2015-04-28 08:48:20 -0700 | [diff] [blame] | 344 | const GrGLSLCaps&, GrProcessorKeyBuilder* b) { |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 345 | const GrMorphologyEffect& m = proc.cast<GrMorphologyEffect>(); |
bsalomon | 63e99f7 | 2014-07-21 08:03:14 -0700 | [diff] [blame] | 346 | uint32_t key = static_cast<uint32_t>(m.radius()); |
senorblanco@chromium.org | 84207c4 | 2012-08-22 20:51:19 +0000 | [diff] [blame] | 347 | key |= (m.type() << 8); |
cwallez | 80a61df | 2015-01-26 12:20:14 -0800 | [diff] [blame] | 348 | key |= (m.direction() << 9); |
robertphillips | bf536af | 2016-02-04 06:11:53 -0800 | [diff] [blame^] | 349 | if (m.useRange()) { |
| 350 | key |= 1 << 10; |
| 351 | } |
bsalomon | 63e99f7 | 2014-07-21 08:03:14 -0700 | [diff] [blame] | 352 | b->add32(key); |
senorblanco@chromium.org | 84207c4 | 2012-08-22 20:51:19 +0000 | [diff] [blame] | 353 | } |
| 354 | |
egdaniel | 018fb62 | 2015-10-28 07:26:40 -0700 | [diff] [blame] | 355 | void GrGLMorphologyEffect::onSetData(const GrGLSLProgramDataManager& pdman, |
robertphillips | bf536af | 2016-02-04 06:11:53 -0800 | [diff] [blame^] | 356 | const GrProcessor& proc) { |
cwallez | 80a61df | 2015-01-26 12:20:14 -0800 | [diff] [blame] | 357 | const GrMorphologyEffect& m = proc.cast<GrMorphologyEffect>(); |
| 358 | GrTexture& texture = *m.texture(0); |
cwallez | 80a61df | 2015-01-26 12:20:14 -0800 | [diff] [blame] | 359 | |
| 360 | float pixelSize = 0.0f; |
robertphillips | bf536af | 2016-02-04 06:11:53 -0800 | [diff] [blame^] | 361 | switch (m.direction()) { |
senorblanco@chromium.org | 84207c4 | 2012-08-22 20:51:19 +0000 | [diff] [blame] | 362 | case Gr1DKernelEffect::kX_Direction: |
cwallez | 80a61df | 2015-01-26 12:20:14 -0800 | [diff] [blame] | 363 | pixelSize = 1.0f / texture.width(); |
senorblanco@chromium.org | 84207c4 | 2012-08-22 20:51:19 +0000 | [diff] [blame] | 364 | break; |
| 365 | case Gr1DKernelEffect::kY_Direction: |
cwallez | 80a61df | 2015-01-26 12:20:14 -0800 | [diff] [blame] | 366 | pixelSize = 1.0f / texture.height(); |
senorblanco@chromium.org | 84207c4 | 2012-08-22 20:51:19 +0000 | [diff] [blame] | 367 | break; |
| 368 | default: |
commit-bot@chromium.org | 88cb22b | 2014-04-30 14:17:00 +0000 | [diff] [blame] | 369 | SkFAIL("Unknown filter direction."); |
senorblanco@chromium.org | 84207c4 | 2012-08-22 20:51:19 +0000 | [diff] [blame] | 370 | } |
cwallez | 80a61df | 2015-01-26 12:20:14 -0800 | [diff] [blame] | 371 | pdman.set1f(fPixelSizeUni, pixelSize); |
| 372 | |
robertphillips | bf536af | 2016-02-04 06:11:53 -0800 | [diff] [blame^] | 373 | if (m.useRange()) { |
cwallez | 80a61df | 2015-01-26 12:20:14 -0800 | [diff] [blame] | 374 | const float* range = m.range(); |
robertphillips | bf536af | 2016-02-04 06:11:53 -0800 | [diff] [blame^] | 375 | if (m.direction() && texture.origin() == kBottomLeft_GrSurfaceOrigin) { |
cwallez | 80a61df | 2015-01-26 12:20:14 -0800 | [diff] [blame] | 376 | pdman.set2f(fRangeUni, 1.0f - range[1], 1.0f - range[0]); |
| 377 | } else { |
| 378 | pdman.set2f(fRangeUni, range[0], range[1]); |
| 379 | } |
| 380 | } |
senorblanco@chromium.org | 84207c4 | 2012-08-22 20:51:19 +0000 | [diff] [blame] | 381 | } |
| 382 | |
| 383 | /////////////////////////////////////////////////////////////////////////////// |
| 384 | |
bsalomon | 4a33952 | 2015-10-06 08:40:50 -0700 | [diff] [blame] | 385 | GrMorphologyEffect::GrMorphologyEffect(GrTexture* texture, |
senorblanco@chromium.org | 84207c4 | 2012-08-22 20:51:19 +0000 | [diff] [blame] | 386 | Direction direction, |
| 387 | int radius, |
| 388 | MorphologyType type) |
bsalomon | 4a33952 | 2015-10-06 08:40:50 -0700 | [diff] [blame] | 389 | : INHERITED(texture, direction, radius) |
cwallez | 80a61df | 2015-01-26 12:20:14 -0800 | [diff] [blame] | 390 | , fType(type), fUseRange(false) { |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 391 | this->initClassID<GrMorphologyEffect>(); |
senorblanco@chromium.org | 84207c4 | 2012-08-22 20:51:19 +0000 | [diff] [blame] | 392 | } |
| 393 | |
bsalomon | 4a33952 | 2015-10-06 08:40:50 -0700 | [diff] [blame] | 394 | GrMorphologyEffect::GrMorphologyEffect(GrTexture* texture, |
cwallez | 80a61df | 2015-01-26 12:20:14 -0800 | [diff] [blame] | 395 | Direction direction, |
| 396 | int radius, |
| 397 | MorphologyType type, |
| 398 | float range[2]) |
bsalomon | 4a33952 | 2015-10-06 08:40:50 -0700 | [diff] [blame] | 399 | : INHERITED(texture, direction, radius) |
cwallez | 80a61df | 2015-01-26 12:20:14 -0800 | [diff] [blame] | 400 | , fType(type), fUseRange(true) { |
| 401 | this->initClassID<GrMorphologyEffect>(); |
| 402 | fRange[0] = range[0]; |
| 403 | fRange[1] = range[1]; |
| 404 | } |
| 405 | |
senorblanco@chromium.org | 84207c4 | 2012-08-22 20:51:19 +0000 | [diff] [blame] | 406 | GrMorphologyEffect::~GrMorphologyEffect() { |
| 407 | } |
| 408 | |
egdaniel | 57d3b03 | 2015-11-13 11:57:27 -0800 | [diff] [blame] | 409 | void GrMorphologyEffect::onGetGLSLProcessorKey(const GrGLSLCaps& caps, |
| 410 | GrProcessorKeyBuilder* b) const { |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 411 | GrGLMorphologyEffect::GenKey(*this, caps, b); |
senorblanco@chromium.org | 84207c4 | 2012-08-22 20:51:19 +0000 | [diff] [blame] | 412 | } |
| 413 | |
egdaniel | 57d3b03 | 2015-11-13 11:57:27 -0800 | [diff] [blame] | 414 | GrGLSLFragmentProcessor* GrMorphologyEffect::onCreateGLSLInstance() const { |
robertphillips | bf536af | 2016-02-04 06:11:53 -0800 | [diff] [blame^] | 415 | return new GrGLMorphologyEffect; |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 416 | } |
bsalomon | 0e08fc1 | 2014-10-15 08:19:04 -0700 | [diff] [blame] | 417 | bool GrMorphologyEffect::onIsEqual(const GrFragmentProcessor& sBase) const { |
joshualitt | 49586be | 2014-09-16 08:21:41 -0700 | [diff] [blame] | 418 | const GrMorphologyEffect& s = sBase.cast<GrMorphologyEffect>(); |
bsalomon | 420d7e9 | 2014-10-16 09:18:09 -0700 | [diff] [blame] | 419 | return (this->radius() == s.radius() && |
senorblanco@chromium.org | 84207c4 | 2012-08-22 20:51:19 +0000 | [diff] [blame] | 420 | this->direction() == s.direction() && |
cwallez | 80a61df | 2015-01-26 12:20:14 -0800 | [diff] [blame] | 421 | this->useRange() == s.useRange() && |
senorblanco@chromium.org | 84207c4 | 2012-08-22 20:51:19 +0000 | [diff] [blame] | 422 | this->type() == s.type()); |
| 423 | } |
| 424 | |
egdaniel | 605dd0f | 2014-11-12 08:35:25 -0800 | [diff] [blame] | 425 | void GrMorphologyEffect::onComputeInvariantOutput(GrInvariantOutput* inout) const { |
bsalomon@google.com | 68b58c9 | 2013-01-17 16:50:08 +0000 | [diff] [blame] | 426 | // This is valid because the color components of the result of the kernel all come |
| 427 | // exactly from existing values in the source texture. |
egdaniel | 1a8ecdf | 2014-10-03 06:24:12 -0700 | [diff] [blame] | 428 | this->updateInvariantOutputForModulation(inout); |
bsalomon@google.com | 68b58c9 | 2013-01-17 16:50:08 +0000 | [diff] [blame] | 429 | } |
| 430 | |
senorblanco@chromium.org | 84207c4 | 2012-08-22 20:51:19 +0000 | [diff] [blame] | 431 | /////////////////////////////////////////////////////////////////////////////// |
| 432 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 433 | GR_DEFINE_FRAGMENT_PROCESSOR_TEST(GrMorphologyEffect); |
senorblanco@chromium.org | 84207c4 | 2012-08-22 20:51:19 +0000 | [diff] [blame] | 434 | |
bsalomon | c21b09e | 2015-08-28 18:46:56 -0700 | [diff] [blame] | 435 | const GrFragmentProcessor* GrMorphologyEffect::TestCreate(GrProcessorTestData* d) { |
joshualitt | 0067ff5 | 2015-07-08 14:26:19 -0700 | [diff] [blame] | 436 | int texIdx = d->fRandom->nextBool() ? GrProcessorUnitTest::kSkiaPMTextureIdx : |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 437 | GrProcessorUnitTest::kAlphaTextureIdx; |
joshualitt | 0067ff5 | 2015-07-08 14:26:19 -0700 | [diff] [blame] | 438 | Direction dir = d->fRandom->nextBool() ? kX_Direction : kY_Direction; |
senorblanco@chromium.org | 84207c4 | 2012-08-22 20:51:19 +0000 | [diff] [blame] | 439 | static const int kMaxRadius = 10; |
joshualitt | 0067ff5 | 2015-07-08 14:26:19 -0700 | [diff] [blame] | 440 | int radius = d->fRandom->nextRangeU(1, kMaxRadius); |
| 441 | MorphologyType type = d->fRandom->nextBool() ? GrMorphologyEffect::kErode_MorphologyType : |
senorblanco@chromium.org | 84207c4 | 2012-08-22 20:51:19 +0000 | [diff] [blame] | 442 | GrMorphologyEffect::kDilate_MorphologyType; |
| 443 | |
bsalomon | 4a33952 | 2015-10-06 08:40:50 -0700 | [diff] [blame] | 444 | return GrMorphologyEffect::Create(d->fTextures[texIdx], dir, radius, type); |
senorblanco@chromium.org | 84207c4 | 2012-08-22 20:51:19 +0000 | [diff] [blame] | 445 | } |
| 446 | |
| 447 | namespace { |
| 448 | |
cwallez | 80a61df | 2015-01-26 12:20:14 -0800 | [diff] [blame] | 449 | |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 450 | void apply_morphology_rect(GrDrawContext* drawContext, |
joshualitt | 570d2f8 | 2015-02-25 13:19:48 -0800 | [diff] [blame] | 451 | const GrClip& clip, |
cwallez | 80a61df | 2015-01-26 12:20:14 -0800 | [diff] [blame] | 452 | GrTexture* texture, |
| 453 | const SkIRect& srcRect, |
| 454 | const SkIRect& dstRect, |
| 455 | int radius, |
| 456 | GrMorphologyEffect::MorphologyType morphType, |
| 457 | float bounds[2], |
| 458 | Gr1DKernelEffect::Direction direction) { |
| 459 | GrPaint paint; |
bsalomon | 4a33952 | 2015-10-06 08:40:50 -0700 | [diff] [blame] | 460 | paint.addColorFragmentProcessor(GrMorphologyEffect::Create(texture, |
bsalomon | ac856c9 | 2015-08-27 06:30:17 -0700 | [diff] [blame] | 461 | direction, |
| 462 | radius, |
| 463 | morphType, |
| 464 | bounds))->unref(); |
egdaniel | c4b7272 | 2015-11-23 13:20:41 -0800 | [diff] [blame] | 465 | paint.setPorterDuffXPFactory(SkXfermode::kSrc_Mode); |
bsalomon | a2e69fc | 2015-11-05 10:41:43 -0800 | [diff] [blame] | 466 | drawContext->fillRectToRect(clip, paint, SkMatrix::I(), SkRect::Make(dstRect), |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 467 | SkRect::Make(srcRect)); |
cwallez | 80a61df | 2015-01-26 12:20:14 -0800 | [diff] [blame] | 468 | } |
| 469 | |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 470 | void apply_morphology_rect_no_bounds(GrDrawContext* drawContext, |
joshualitt | 570d2f8 | 2015-02-25 13:19:48 -0800 | [diff] [blame] | 471 | const GrClip& clip, |
joshualitt | 25d9c15 | 2015-02-18 12:29:52 -0800 | [diff] [blame] | 472 | GrTexture* texture, |
| 473 | const SkIRect& srcRect, |
| 474 | const SkIRect& dstRect, |
| 475 | int radius, |
| 476 | GrMorphologyEffect::MorphologyType morphType, |
| 477 | Gr1DKernelEffect::Direction direction) { |
senorblanco@chromium.org | 84207c4 | 2012-08-22 20:51:19 +0000 | [diff] [blame] | 478 | GrPaint paint; |
bsalomon | 4a33952 | 2015-10-06 08:40:50 -0700 | [diff] [blame] | 479 | paint.addColorFragmentProcessor(GrMorphologyEffect::Create(texture, |
bsalomon | ac856c9 | 2015-08-27 06:30:17 -0700 | [diff] [blame] | 480 | direction, |
| 481 | radius, |
| 482 | morphType))->unref(); |
egdaniel | c4b7272 | 2015-11-23 13:20:41 -0800 | [diff] [blame] | 483 | paint.setPorterDuffXPFactory(SkXfermode::kSrc_Mode); |
bsalomon | a2e69fc | 2015-11-05 10:41:43 -0800 | [diff] [blame] | 484 | drawContext->fillRectToRect(clip, paint, SkMatrix::I(), SkRect::Make(dstRect), |
| 485 | SkRect::Make(srcRect)); |
senorblanco@chromium.org | 84207c4 | 2012-08-22 20:51:19 +0000 | [diff] [blame] | 486 | } |
| 487 | |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 488 | void apply_morphology_pass(GrDrawContext* drawContext, |
joshualitt | 570d2f8 | 2015-02-25 13:19:48 -0800 | [diff] [blame] | 489 | const GrClip& clip, |
cwallez | 80a61df | 2015-01-26 12:20:14 -0800 | [diff] [blame] | 490 | GrTexture* texture, |
| 491 | const SkIRect& srcRect, |
| 492 | const SkIRect& dstRect, |
| 493 | int radius, |
| 494 | GrMorphologyEffect::MorphologyType morphType, |
| 495 | Gr1DKernelEffect::Direction direction) { |
| 496 | float bounds[2] = { 0.0f, 1.0f }; |
| 497 | SkIRect lowerSrcRect = srcRect, lowerDstRect = dstRect; |
| 498 | SkIRect middleSrcRect = srcRect, middleDstRect = dstRect; |
| 499 | SkIRect upperSrcRect = srcRect, upperDstRect = dstRect; |
| 500 | if (direction == Gr1DKernelEffect::kX_Direction) { |
| 501 | bounds[0] = (SkIntToScalar(srcRect.left()) + 0.5f) / texture->width(); |
| 502 | bounds[1] = (SkIntToScalar(srcRect.right()) - 0.5f) / texture->width(); |
| 503 | lowerSrcRect.fRight = srcRect.left() + radius; |
| 504 | lowerDstRect.fRight = dstRect.left() + radius; |
| 505 | upperSrcRect.fLeft = srcRect.right() - radius; |
| 506 | upperDstRect.fLeft = dstRect.right() - radius; |
| 507 | middleSrcRect.inset(radius, 0); |
| 508 | middleDstRect.inset(radius, 0); |
| 509 | } else { |
| 510 | bounds[0] = (SkIntToScalar(srcRect.top()) + 0.5f) / texture->height(); |
| 511 | bounds[1] = (SkIntToScalar(srcRect.bottom()) - 0.5f) / texture->height(); |
| 512 | lowerSrcRect.fBottom = srcRect.top() + radius; |
| 513 | lowerDstRect.fBottom = dstRect.top() + radius; |
| 514 | upperSrcRect.fTop = srcRect.bottom() - radius; |
| 515 | upperDstRect.fTop = dstRect.bottom() - radius; |
| 516 | middleSrcRect.inset(0, radius); |
| 517 | middleDstRect.inset(0, radius); |
| 518 | } |
| 519 | if (middleSrcRect.fLeft - middleSrcRect.fRight >= 0) { |
| 520 | // radius covers srcRect; use bounds over entire draw |
robertphillips | 2e1e51f | 2015-10-15 08:01:48 -0700 | [diff] [blame] | 521 | apply_morphology_rect(drawContext, clip, texture, srcRect, dstRect, radius, |
cwallez | 80a61df | 2015-01-26 12:20:14 -0800 | [diff] [blame] | 522 | morphType, bounds, direction); |
| 523 | } else { |
| 524 | // Draw upper and lower margins with bounds; middle without. |
robertphillips | 2e1e51f | 2015-10-15 08:01:48 -0700 | [diff] [blame] | 525 | apply_morphology_rect(drawContext, clip, texture, lowerSrcRect, lowerDstRect, radius, |
cwallez | 80a61df | 2015-01-26 12:20:14 -0800 | [diff] [blame] | 526 | morphType, bounds, direction); |
robertphillips | 2e1e51f | 2015-10-15 08:01:48 -0700 | [diff] [blame] | 527 | apply_morphology_rect(drawContext, clip, texture, upperSrcRect, upperDstRect, radius, |
cwallez | 80a61df | 2015-01-26 12:20:14 -0800 | [diff] [blame] | 528 | morphType, bounds, direction); |
robertphillips | 2e1e51f | 2015-10-15 08:01:48 -0700 | [diff] [blame] | 529 | apply_morphology_rect_no_bounds(drawContext, clip, texture, middleSrcRect, middleDstRect, |
joshualitt | 570d2f8 | 2015-02-25 13:19:48 -0800 | [diff] [blame] | 530 | radius, morphType, direction); |
cwallez | 80a61df | 2015-01-26 12:20:14 -0800 | [diff] [blame] | 531 | } |
| 532 | } |
| 533 | |
senorblanco@chromium.org | 8fcad98 | 2013-09-17 13:41:43 +0000 | [diff] [blame] | 534 | bool apply_morphology(const SkBitmap& input, |
| 535 | const SkIRect& rect, |
| 536 | GrMorphologyEffect::MorphologyType morphType, |
| 537 | SkISize radius, |
reed | 4e23cda | 2016-01-11 10:56:59 -0800 | [diff] [blame] | 538 | SkBitmap* dst) { |
bsalomon | e305973 | 2014-10-14 11:47:22 -0700 | [diff] [blame] | 539 | SkAutoTUnref<GrTexture> srcTexture(SkRef(input.getTexture())); |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 540 | SkASSERT(srcTexture); |
senorblanco@chromium.org | 84207c4 | 2012-08-22 20:51:19 +0000 | [diff] [blame] | 541 | GrContext* context = srcTexture->getContext(); |
bsalomon@google.com | 3cbaa2d | 2012-10-12 14:51:52 +0000 | [diff] [blame] | 542 | |
joshualitt | 570d2f8 | 2015-02-25 13:19:48 -0800 | [diff] [blame] | 543 | // setup new clip |
| 544 | GrClip clip(SkRect::MakeWH(SkIntToScalar(srcTexture->width()), |
| 545 | SkIntToScalar(srcTexture->height()))); |
bsalomon@google.com | 3cbaa2d | 2012-10-12 14:51:52 +0000 | [diff] [blame] | 546 | |
senorblanco@chromium.org | 8fcad98 | 2013-09-17 13:41:43 +0000 | [diff] [blame] | 547 | SkIRect dstRect = SkIRect::MakeWH(rect.width(), rect.height()); |
bsalomon | f2703d8 | 2014-10-28 14:33:06 -0700 | [diff] [blame] | 548 | GrSurfaceDesc desc; |
bsalomon | 6bc1b5f | 2015-02-23 09:06:38 -0800 | [diff] [blame] | 549 | desc.fFlags = kRenderTarget_GrSurfaceFlag; |
senorblanco@chromium.org | c2594f4 | 2013-01-30 19:08:47 +0000 | [diff] [blame] | 550 | desc.fWidth = rect.width(); |
| 551 | desc.fHeight = rect.height(); |
| 552 | desc.fConfig = kSkia8888_GrPixelConfig; |
senorblanco@chromium.org | 8fcad98 | 2013-09-17 13:41:43 +0000 | [diff] [blame] | 553 | SkIRect srcRect = rect; |
bsalomon@google.com | 3cbaa2d | 2012-10-12 14:51:52 +0000 | [diff] [blame] | 554 | |
senorblanco@chromium.org | 84207c4 | 2012-08-22 20:51:19 +0000 | [diff] [blame] | 555 | if (radius.fWidth > 0) { |
reed | 4e23cda | 2016-01-11 10:56:59 -0800 | [diff] [blame] | 556 | GrTexture* scratch = context->textureProvider()->createApproxTexture(desc); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 557 | if (nullptr == scratch) { |
senorblanco | 673d973 | 2014-08-15 10:48:43 -0700 | [diff] [blame] | 558 | return false; |
| 559 | } |
robertphillips | 77a2e52 | 2015-10-17 07:43:27 -0700 | [diff] [blame] | 560 | SkAutoTUnref<GrDrawContext> dstDrawContext( |
| 561 | context->drawContext(scratch->asRenderTarget())); |
robertphillips | ff0ca5e | 2015-07-22 11:54:44 -0700 | [diff] [blame] | 562 | if (!dstDrawContext) { |
| 563 | return false; |
| 564 | } |
| 565 | |
robertphillips | 2e1e51f | 2015-10-15 08:01:48 -0700 | [diff] [blame] | 566 | apply_morphology_pass(dstDrawContext, clip, srcTexture, |
joshualitt | 570d2f8 | 2015-02-25 13:19:48 -0800 | [diff] [blame] | 567 | srcRect, dstRect, radius.fWidth, morphType, |
| 568 | Gr1DKernelEffect::kX_Direction); |
senorblanco@chromium.org | 8fcad98 | 2013-09-17 13:41:43 +0000 | [diff] [blame] | 569 | SkIRect clearRect = SkIRect::MakeXYWH(dstRect.fLeft, dstRect.fBottom, |
| 570 | dstRect.width(), radius.fHeight); |
bsalomon | 89c6298 | 2014-11-03 12:08:42 -0800 | [diff] [blame] | 571 | GrColor clearColor = GrMorphologyEffect::kErode_MorphologyType == morphType ? |
| 572 | SK_ColorWHITE : |
| 573 | SK_ColorTRANSPARENT; |
robertphillips | 2e1e51f | 2015-10-15 08:01:48 -0700 | [diff] [blame] | 574 | dstDrawContext->clear(&clearRect, clearColor, false); |
robertphillips | ff0ca5e | 2015-07-22 11:54:44 -0700 | [diff] [blame] | 575 | |
robertphillips | eb86b55 | 2015-07-23 11:35:08 -0700 | [diff] [blame] | 576 | srcTexture.reset(scratch); |
senorblanco@chromium.org | 8fcad98 | 2013-09-17 13:41:43 +0000 | [diff] [blame] | 577 | srcRect = dstRect; |
senorblanco@chromium.org | 84207c4 | 2012-08-22 20:51:19 +0000 | [diff] [blame] | 578 | } |
| 579 | if (radius.fHeight > 0) { |
reed | 4e23cda | 2016-01-11 10:56:59 -0800 | [diff] [blame] | 580 | GrTexture* scratch = context->textureProvider()->createApproxTexture(desc); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 581 | if (nullptr == scratch) { |
senorblanco | 673d973 | 2014-08-15 10:48:43 -0700 | [diff] [blame] | 582 | return false; |
| 583 | } |
robertphillips | 77a2e52 | 2015-10-17 07:43:27 -0700 | [diff] [blame] | 584 | SkAutoTUnref<GrDrawContext> dstDrawContext( |
| 585 | context->drawContext(scratch->asRenderTarget())); |
robertphillips | ff0ca5e | 2015-07-22 11:54:44 -0700 | [diff] [blame] | 586 | if (!dstDrawContext) { |
| 587 | return false; |
| 588 | } |
| 589 | |
robertphillips | 2e1e51f | 2015-10-15 08:01:48 -0700 | [diff] [blame] | 590 | apply_morphology_pass(dstDrawContext, clip, srcTexture, |
joshualitt | 570d2f8 | 2015-02-25 13:19:48 -0800 | [diff] [blame] | 591 | srcRect, dstRect, radius.fHeight, morphType, |
| 592 | Gr1DKernelEffect::kY_Direction); |
robertphillips | ff0ca5e | 2015-07-22 11:54:44 -0700 | [diff] [blame] | 593 | |
robertphillips | eb86b55 | 2015-07-23 11:35:08 -0700 | [diff] [blame] | 594 | srcTexture.reset(scratch); |
senorblanco@chromium.org | 84207c4 | 2012-08-22 20:51:19 +0000 | [diff] [blame] | 595 | } |
robertphillips | 1de87df | 2016-01-14 06:03:29 -0800 | [diff] [blame] | 596 | GrWrapTextureInBitmap(srcTexture, rect.width(), rect.height(), false, dst); |
senorblanco@chromium.org | 6aa6fec | 2014-03-03 22:13:56 +0000 | [diff] [blame] | 597 | return true; |
senorblanco@chromium.org | 84207c4 | 2012-08-22 20:51:19 +0000 | [diff] [blame] | 598 | } |
| 599 | |
| 600 | }; |
| 601 | |
senorblanco@chromium.org | 0ded88d | 2014-01-24 15:43:50 +0000 | [diff] [blame] | 602 | bool SkMorphologyImageFilter::filterImageGPUGeneric(bool dilate, |
| 603 | Proxy* proxy, |
| 604 | const SkBitmap& src, |
senorblanco@chromium.org | 4cb543d | 2014-03-14 15:44:01 +0000 | [diff] [blame] | 605 | const Context& ctx, |
senorblanco@chromium.org | 0ded88d | 2014-01-24 15:43:50 +0000 | [diff] [blame] | 606 | SkBitmap* result, |
commit-bot@chromium.org | ae761f7 | 2014-02-05 22:32:02 +0000 | [diff] [blame] | 607 | SkIPoint* offset) const { |
senorblanco@chromium.org | 6aa6fec | 2014-03-03 22:13:56 +0000 | [diff] [blame] | 608 | SkBitmap input = src; |
senorblanco@chromium.org | 2bfe36b | 2014-01-20 19:58:28 +0000 | [diff] [blame] | 609 | SkIPoint srcOffset = SkIPoint::Make(0, 0); |
senorblanco | 9a70b6e | 2015-10-16 11:35:14 -0700 | [diff] [blame] | 610 | if (!this->filterInputGPU(0, proxy, src, ctx, &input, &srcOffset)) { |
senorblanco@chromium.org | c2594f4 | 2013-01-30 19:08:47 +0000 | [diff] [blame] | 611 | return false; |
| 612 | } |
senorblanco@chromium.org | c2594f4 | 2013-01-30 19:08:47 +0000 | [diff] [blame] | 613 | SkIRect bounds; |
senorblanco | db64af3 | 2015-12-09 10:11:43 -0800 | [diff] [blame] | 614 | if (!this->applyCropRect(this->mapContext(ctx), proxy, input, &srcOffset, &bounds, &input)) { |
senorblanco@chromium.org | 8fcad98 | 2013-09-17 13:41:43 +0000 | [diff] [blame] | 615 | return false; |
| 616 | } |
senorblanco@chromium.org | 2bfe36b | 2014-01-20 19:58:28 +0000 | [diff] [blame] | 617 | SkVector radius = SkVector::Make(SkIntToScalar(this->radius().width()), |
| 618 | SkIntToScalar(this->radius().height())); |
senorblanco@chromium.org | 4cb543d | 2014-03-14 15:44:01 +0000 | [diff] [blame] | 619 | ctx.ctm().mapVectors(&radius, 1); |
senorblanco@chromium.org | 2bfe36b | 2014-01-20 19:58:28 +0000 | [diff] [blame] | 620 | int width = SkScalarFloorToInt(radius.fX); |
| 621 | int height = SkScalarFloorToInt(radius.fY); |
senorblanco@chromium.org | 8fcad98 | 2013-09-17 13:41:43 +0000 | [diff] [blame] | 622 | |
| 623 | if (width < 0 || height < 0) { |
| 624 | return false; |
| 625 | } |
| 626 | |
senorblanco@chromium.org | 2bfe36b | 2014-01-20 19:58:28 +0000 | [diff] [blame] | 627 | SkIRect srcBounds = bounds; |
| 628 | srcBounds.offset(-srcOffset); |
senorblanco@chromium.org | 8fcad98 | 2013-09-17 13:41:43 +0000 | [diff] [blame] | 629 | if (width == 0 && height == 0) { |
senorblanco@chromium.org | 2bfe36b | 2014-01-20 19:58:28 +0000 | [diff] [blame] | 630 | input.extractSubset(result, srcBounds); |
senorblanco@chromium.org | 6776b82 | 2014-01-03 21:48:22 +0000 | [diff] [blame] | 631 | offset->fX = bounds.left(); |
| 632 | offset->fY = bounds.top(); |
senorblanco@chromium.org | 8fcad98 | 2013-09-17 13:41:43 +0000 | [diff] [blame] | 633 | return true; |
| 634 | } |
| 635 | |
robertphillips | ff0ca5e | 2015-07-22 11:54:44 -0700 | [diff] [blame] | 636 | GrMorphologyEffect::MorphologyType type = dilate ? GrMorphologyEffect::kDilate_MorphologyType |
| 637 | : GrMorphologyEffect::kErode_MorphologyType; |
reed | 4e23cda | 2016-01-11 10:56:59 -0800 | [diff] [blame] | 638 | if (!apply_morphology(input, srcBounds, type, SkISize::Make(width, height), result)) { |
senorblanco@chromium.org | 8fcad98 | 2013-09-17 13:41:43 +0000 | [diff] [blame] | 639 | return false; |
| 640 | } |
senorblanco@chromium.org | 6776b82 | 2014-01-03 21:48:22 +0000 | [diff] [blame] | 641 | offset->fX = bounds.left(); |
| 642 | offset->fY = bounds.top(); |
senorblanco@chromium.org | 8fcad98 | 2013-09-17 13:41:43 +0000 | [diff] [blame] | 643 | return true; |
senorblanco@chromium.org | 05054f1 | 2012-03-02 21:05:45 +0000 | [diff] [blame] | 644 | } |
| 645 | |
senorblanco@chromium.org | 4cb543d | 2014-03-14 15:44:01 +0000 | [diff] [blame] | 646 | bool SkDilateImageFilter::filterImageGPU(Proxy* proxy, const SkBitmap& src, const Context& ctx, |
commit-bot@chromium.org | ae761f7 | 2014-02-05 22:32:02 +0000 | [diff] [blame] | 647 | SkBitmap* result, SkIPoint* offset) const { |
senorblanco@chromium.org | 4cb543d | 2014-03-14 15:44:01 +0000 | [diff] [blame] | 648 | return this->filterImageGPUGeneric(true, proxy, src, ctx, result, offset); |
senorblanco@chromium.org | 0ded88d | 2014-01-24 15:43:50 +0000 | [diff] [blame] | 649 | } |
| 650 | |
senorblanco@chromium.org | 4cb543d | 2014-03-14 15:44:01 +0000 | [diff] [blame] | 651 | bool SkErodeImageFilter::filterImageGPU(Proxy* proxy, const SkBitmap& src, const Context& ctx, |
commit-bot@chromium.org | ae761f7 | 2014-02-05 22:32:02 +0000 | [diff] [blame] | 652 | SkBitmap* result, SkIPoint* offset) const { |
senorblanco@chromium.org | 4cb543d | 2014-03-14 15:44:01 +0000 | [diff] [blame] | 653 | return this->filterImageGPUGeneric(false, proxy, src, ctx, result, offset); |
senorblanco@chromium.org | 05054f1 | 2012-03-02 21:05:45 +0000 | [diff] [blame] | 654 | } |
| 655 | |
senorblanco@chromium.org | 84207c4 | 2012-08-22 20:51:19 +0000 | [diff] [blame] | 656 | #endif |