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