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