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" |
robertphillips | f299e71 | 2016-03-25 04:49:22 -0700 | [diff] [blame] | 9 | |
djsollen@google.com | 64a0ec3 | 2012-06-12 15:17:27 +0000 | [diff] [blame] | 10 | #include "SkBitmap.h" |
senorblanco@chromium.org | 05054f1 | 2012-03-02 21:05:45 +0000 | [diff] [blame] | 11 | #include "SkColorPriv.h" |
Mike Reed | 6d9f429 | 2017-07-06 12:32:55 -0400 | [diff] [blame] | 12 | #include "SkColorSpaceXformer.h" |
mtklein | d029ded | 2015-08-04 14:09:09 -0700 | [diff] [blame] | 13 | #include "SkOpts.h" |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 14 | #include "SkReadBuffer.h" |
djsollen@google.com | c73dd5c | 2012-08-07 15:54:32 +0000 | [diff] [blame] | 15 | #include "SkRect.h" |
robertphillips | f299e71 | 2016-03-25 04:49:22 -0700 | [diff] [blame] | 16 | #include "SkSpecialImage.h" |
mtklein | d029ded | 2015-08-04 14:09:09 -0700 | [diff] [blame] | 17 | #include "SkWriteBuffer.h" |
robertphillips | f299e71 | 2016-03-25 04:49:22 -0700 | [diff] [blame] | 18 | |
bsalomon@google.com | cf8fb1f | 2012-08-02 14:03:32 +0000 | [diff] [blame] | 19 | #if SK_SUPPORT_GPU |
senorblanco@chromium.org | 302cffb | 2012-08-01 20:16:34 +0000 | [diff] [blame] | 20 | #include "GrContext.h" |
csmartdalton | 02fa32c | 2016-08-19 13:29:27 -0700 | [diff] [blame] | 21 | #include "GrFixedClip.h" |
Robert Phillips | 7f6cd90 | 2016-11-10 17:03:43 -0500 | [diff] [blame] | 22 | #include "GrRenderTargetContext.h" |
Robert Phillips | 646e429 | 2017-06-13 12:44:56 -0400 | [diff] [blame] | 23 | #include "GrTexture.h" |
Robert Phillips | 7f6cd90 | 2016-11-10 17:03:43 -0500 | [diff] [blame] | 24 | #include "GrTextureProxy.h" |
| 25 | |
robertphillips | 1de87df | 2016-01-14 06:03:29 -0800 | [diff] [blame] | 26 | #include "SkGr.h" |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 27 | #include "effects/Gr1DKernelEffect.h" |
Robert Phillips | dbc8eeb | 2017-02-21 10:04:31 -0500 | [diff] [blame] | 28 | #include "effects/GrProxyMove.h" |
egdaniel | 64c4728 | 2015-11-13 06:54:19 -0800 | [diff] [blame] | 29 | #include "glsl/GrGLSLFragmentProcessor.h" |
egdaniel | 2d721d3 | 2015-11-11 13:06:05 -0800 | [diff] [blame] | 30 | #include "glsl/GrGLSLFragmentShaderBuilder.h" |
egdaniel | 018fb62 | 2015-10-28 07:26:40 -0700 | [diff] [blame] | 31 | #include "glsl/GrGLSLProgramDataManager.h" |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 32 | #include "glsl/GrGLSLUniformHandler.h" |
Brian Salomon | 94efbf5 | 2016-11-29 13:43:05 -0500 | [diff] [blame] | 33 | #include "../private/GrGLSL.h" |
bsalomon@google.com | cf8fb1f | 2012-08-02 14:03:32 +0000 | [diff] [blame] | 34 | #endif |
senorblanco@chromium.org | 05054f1 | 2012-03-02 21:05:45 +0000 | [diff] [blame] | 35 | |
robertphillips | 225db44 | 2016-04-17 14:27:05 -0700 | [diff] [blame] | 36 | sk_sp<SkImageFilter> SkDilateImageFilter::Make(int radiusX, int radiusY, |
| 37 | sk_sp<SkImageFilter> input, |
| 38 | const CropRect* cropRect) { |
| 39 | if (radiusX < 0 || radiusY < 0) { |
| 40 | return nullptr; |
| 41 | } |
| 42 | return sk_sp<SkImageFilter>(new SkDilateImageFilter(radiusX, radiusY, |
| 43 | std::move(input), |
| 44 | cropRect)); |
| 45 | } |
| 46 | |
| 47 | |
| 48 | sk_sp<SkImageFilter> SkErodeImageFilter::Make(int radiusX, int radiusY, |
| 49 | sk_sp<SkImageFilter> input, |
| 50 | const CropRect* cropRect) { |
| 51 | if (radiusX < 0 || radiusY < 0) { |
| 52 | return nullptr; |
| 53 | } |
| 54 | return sk_sp<SkImageFilter>(new SkErodeImageFilter(radiusX, radiusY, |
| 55 | std::move(input), |
| 56 | cropRect)); |
| 57 | } |
| 58 | |
senorblanco@chromium.org | 2bfe36b | 2014-01-20 19:58:28 +0000 | [diff] [blame] | 59 | SkMorphologyImageFilter::SkMorphologyImageFilter(int radiusX, |
| 60 | int radiusY, |
robertphillips | fc11b0a | 2016-04-05 09:09:36 -0700 | [diff] [blame] | 61 | sk_sp<SkImageFilter> input, |
senorblanco | 24e06d5 | 2015-03-18 12:11:33 -0700 | [diff] [blame] | 62 | const CropRect* cropRect) |
robertphillips | fc11b0a | 2016-04-05 09:09:36 -0700 | [diff] [blame] | 63 | : INHERITED(&input, 1, cropRect) |
robertphillips | f299e71 | 2016-03-25 04:49:22 -0700 | [diff] [blame] | 64 | , fRadius(SkISize::Make(radiusX, radiusY)) { |
senorblanco@chromium.org | 05054f1 | 2012-03-02 21:05:45 +0000 | [diff] [blame] | 65 | } |
| 66 | |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 67 | void SkMorphologyImageFilter::flatten(SkWriteBuffer& buffer) const { |
senorblanco@chromium.org | 05054f1 | 2012-03-02 21:05:45 +0000 | [diff] [blame] | 68 | this->INHERITED::flatten(buffer); |
tomhudson@google.com | 7558925 | 2012-04-10 17:42:21 +0000 | [diff] [blame] | 69 | buffer.writeInt(fRadius.fWidth); |
| 70 | buffer.writeInt(fRadius.fHeight); |
senorblanco@chromium.org | 05054f1 | 2012-03-02 21:05:45 +0000 | [diff] [blame] | 71 | } |
| 72 | |
robertphillips | f299e71 | 2016-03-25 04:49:22 -0700 | [diff] [blame] | 73 | static void call_proc_X(SkMorphologyImageFilter::Proc procX, |
robertphillips | 6461251 | 2016-04-08 12:10:42 -0700 | [diff] [blame] | 74 | const SkBitmap& src, SkBitmap* dst, |
robertphillips | f299e71 | 2016-03-25 04:49:22 -0700 | [diff] [blame] | 75 | int radiusX, const SkIRect& bounds) { |
robertphillips | 6461251 | 2016-04-08 12:10:42 -0700 | [diff] [blame] | 76 | procX(src.getAddr32(bounds.left(), bounds.top()), dst->getAddr32(0, 0), |
senorblanco@chromium.org | 0ded88d | 2014-01-24 15:43:50 +0000 | [diff] [blame] | 77 | radiusX, bounds.width(), bounds.height(), |
| 78 | src.rowBytesAsPixels(), dst->rowBytesAsPixels()); |
senorblanco@chromium.org | 05054f1 | 2012-03-02 21:05:45 +0000 | [diff] [blame] | 79 | } |
| 80 | |
robertphillips | f299e71 | 2016-03-25 04:49:22 -0700 | [diff] [blame] | 81 | static void call_proc_Y(SkMorphologyImageFilter::Proc procY, |
| 82 | const SkPMColor* src, int srcRowBytesAsPixels, SkBitmap* dst, |
| 83 | int radiusY, const SkIRect& bounds) { |
| 84 | procY(src, dst->getAddr32(0, 0), |
senorblanco@chromium.org | 0ded88d | 2014-01-24 15:43:50 +0000 | [diff] [blame] | 85 | radiusY, bounds.height(), bounds.width(), |
robertphillips | f299e71 | 2016-03-25 04:49:22 -0700 | [diff] [blame] | 86 | srcRowBytesAsPixels, dst->rowBytesAsPixels()); |
senorblanco@chromium.org | 05054f1 | 2012-03-02 21:05:45 +0000 | [diff] [blame] | 87 | } |
| 88 | |
senorblanco | e5e7984 | 2016-03-21 14:51:59 -0700 | [diff] [blame] | 89 | SkRect SkMorphologyImageFilter::computeFastBounds(const SkRect& src) const { |
| 90 | SkRect bounds = this->getInput(0) ? this->getInput(0)->computeFastBounds(src) : src; |
| 91 | bounds.outset(SkIntToScalar(fRadius.width()), SkIntToScalar(fRadius.height())); |
| 92 | return bounds; |
senorblanco@chromium.org | 336d1d7 | 2014-01-27 21:03:17 +0000 | [diff] [blame] | 93 | } |
| 94 | |
senorblanco | e5e7984 | 2016-03-21 14:51:59 -0700 | [diff] [blame] | 95 | SkIRect SkMorphologyImageFilter::onFilterNodeBounds(const SkIRect& src, const SkMatrix& ctm, |
| 96 | MapDirection) const { |
senorblanco@chromium.org | c4b12f1 | 2014-02-05 17:51:22 +0000 | [diff] [blame] | 97 | SkVector radius = SkVector::Make(SkIntToScalar(this->radius().width()), |
| 98 | SkIntToScalar(this->radius().height())); |
| 99 | ctm.mapVectors(&radius, 1); |
senorblanco | e5e7984 | 2016-03-21 14:51:59 -0700 | [diff] [blame] | 100 | return src.makeOutset(SkScalarCeilToInt(radius.x()), SkScalarCeilToInt(radius.y())); |
senorblanco@chromium.org | c4b12f1 | 2014-02-05 17:51:22 +0000 | [diff] [blame] | 101 | } |
| 102 | |
reed | 60c9b58 | 2016-04-03 09:11:13 -0700 | [diff] [blame] | 103 | sk_sp<SkFlattenable> SkErodeImageFilter::CreateProc(SkReadBuffer& buffer) { |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 104 | SK_IMAGEFILTER_UNFLATTEN_COMMON(common, 1); |
| 105 | const int width = buffer.readInt(); |
| 106 | const int height = buffer.readInt(); |
robertphillips | fc11b0a | 2016-04-05 09:09:36 -0700 | [diff] [blame] | 107 | return Make(width, height, common.getInput(0), &common.cropRect()); |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 108 | } |
| 109 | |
reed | 60c9b58 | 2016-04-03 09:11:13 -0700 | [diff] [blame] | 110 | sk_sp<SkFlattenable> SkDilateImageFilter::CreateProc(SkReadBuffer& buffer) { |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 111 | SK_IMAGEFILTER_UNFLATTEN_COMMON(common, 1); |
| 112 | const int width = buffer.readInt(); |
| 113 | const int height = buffer.readInt(); |
robertphillips | fc11b0a | 2016-04-05 09:09:36 -0700 | [diff] [blame] | 114 | return Make(width, height, common.getInput(0), &common.cropRect()); |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 115 | } |
| 116 | |
robertphillips | f3f5bad | 2014-12-19 13:49:15 -0800 | [diff] [blame] | 117 | #ifndef SK_IGNORE_TO_STRING |
| 118 | void SkErodeImageFilter::toString(SkString* str) const { |
| 119 | str->appendf("SkErodeImageFilter: ("); |
| 120 | str->appendf("radius: (%d,%d)", this->radius().fWidth, this->radius().fHeight); |
| 121 | str->append(")"); |
| 122 | } |
| 123 | #endif |
| 124 | |
| 125 | #ifndef SK_IGNORE_TO_STRING |
| 126 | void SkDilateImageFilter::toString(SkString* str) const { |
| 127 | str->appendf("SkDilateImageFilter: ("); |
| 128 | str->appendf("radius: (%d,%d)", this->radius().fWidth, this->radius().fHeight); |
| 129 | str->append(")"); |
| 130 | } |
| 131 | #endif |
| 132 | |
bsalomon@google.com | cf8fb1f | 2012-08-02 14:03:32 +0000 | [diff] [blame] | 133 | #if SK_SUPPORT_GPU |
senorblanco@chromium.org | 84207c4 | 2012-08-22 20:51:19 +0000 | [diff] [blame] | 134 | |
| 135 | /////////////////////////////////////////////////////////////////////////////// |
senorblanco@chromium.org | 84207c4 | 2012-08-22 20:51:19 +0000 | [diff] [blame] | 136 | /** |
| 137 | * Morphology effects. Depending upon the type of morphology, either the |
| 138 | * component-wise min (Erode_Type) or max (Dilate_Type) of all pixels in the |
| 139 | * kernel is selected as the new color. The new color is modulated by the input |
| 140 | * color. |
| 141 | */ |
| 142 | class GrMorphologyEffect : public Gr1DKernelEffect { |
senorblanco@chromium.org | 84207c4 | 2012-08-22 20:51:19 +0000 | [diff] [blame] | 143 | public: |
senorblanco@chromium.org | 84207c4 | 2012-08-22 20:51:19 +0000 | [diff] [blame] | 144 | enum MorphologyType { |
| 145 | kErode_MorphologyType, |
| 146 | kDilate_MorphologyType, |
| 147 | }; |
| 148 | |
Robert Phillips | fbcef6e | 2017-06-15 12:07:18 -0400 | [diff] [blame] | 149 | static sk_sp<GrFragmentProcessor> Make(sk_sp<GrTextureProxy> proxy, |
Robert Phillips | dbc8eeb | 2017-02-21 10:04:31 -0500 | [diff] [blame] | 150 | Direction dir, int radius, MorphologyType type) { |
Robert Phillips | fbcef6e | 2017-06-15 12:07:18 -0400 | [diff] [blame] | 151 | return sk_sp<GrFragmentProcessor>(new GrMorphologyEffect(std::move(proxy), |
Robert Phillips | dbc8eeb | 2017-02-21 10:04:31 -0500 | [diff] [blame] | 152 | dir, radius, type)); |
| 153 | } |
| 154 | |
Robert Phillips | fbcef6e | 2017-06-15 12:07:18 -0400 | [diff] [blame] | 155 | static sk_sp<GrFragmentProcessor> Make(sk_sp<GrTextureProxy> proxy, Direction dir, int radius, |
Robert Phillips | dbc8eeb | 2017-02-21 10:04:31 -0500 | [diff] [blame] | 156 | MorphologyType type, const float bounds[2]) { |
Robert Phillips | fbcef6e | 2017-06-15 12:07:18 -0400 | [diff] [blame] | 157 | return sk_sp<GrFragmentProcessor>(new GrMorphologyEffect(std::move(proxy), |
Robert Phillips | dbc8eeb | 2017-02-21 10:04:31 -0500 | [diff] [blame] | 158 | dir, radius, type, bounds)); |
| 159 | } |
| 160 | |
| 161 | ~GrMorphologyEffect() override; |
senorblanco@chromium.org | 84207c4 | 2012-08-22 20:51:19 +0000 | [diff] [blame] | 162 | |
| 163 | MorphologyType type() const { return fType; } |
cwallez | 80a61df | 2015-01-26 12:20:14 -0800 | [diff] [blame] | 164 | bool useRange() const { return fUseRange; } |
| 165 | const float* range() const { return fRange; } |
senorblanco@chromium.org | 84207c4 | 2012-08-22 20:51:19 +0000 | [diff] [blame] | 166 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 167 | const char* name() const override { return "Morphology"; } |
senorblanco@chromium.org | 84207c4 | 2012-08-22 20:51:19 +0000 | [diff] [blame] | 168 | |
senorblanco@chromium.org | 84207c4 | 2012-08-22 20:51:19 +0000 | [diff] [blame] | 169 | protected: |
| 170 | |
| 171 | MorphologyType fType; |
cwallez | 80a61df | 2015-01-26 12:20:14 -0800 | [diff] [blame] | 172 | bool fUseRange; |
| 173 | float fRange[2]; |
senorblanco@chromium.org | 84207c4 | 2012-08-22 20:51:19 +0000 | [diff] [blame] | 174 | |
| 175 | private: |
egdaniel | 57d3b03 | 2015-11-13 11:57:27 -0800 | [diff] [blame] | 176 | GrGLSLFragmentProcessor* onCreateGLSLInstance() const override; |
wangyix | b1daa86 | 2015-08-18 11:29:31 -0700 | [diff] [blame] | 177 | |
Brian Salomon | 94efbf5 | 2016-11-29 13:43:05 -0500 | [diff] [blame] | 178 | void onGetGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder*) const override; |
wangyix | 4b3050b | 2015-08-04 07:59:37 -0700 | [diff] [blame] | 179 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 180 | bool onIsEqual(const GrFragmentProcessor&) const override; |
bsalomon@google.com | 68b58c9 | 2013-01-17 16:50:08 +0000 | [diff] [blame] | 181 | |
Robert Phillips | fbcef6e | 2017-06-15 12:07:18 -0400 | [diff] [blame] | 182 | GrMorphologyEffect(sk_sp<GrTextureProxy>, |
Robert Phillips | 296b1cc | 2017-03-15 10:42:12 -0400 | [diff] [blame] | 183 | Direction, int radius, MorphologyType); |
Robert Phillips | fbcef6e | 2017-06-15 12:07:18 -0400 | [diff] [blame] | 184 | GrMorphologyEffect(sk_sp<GrTextureProxy>, |
Robert Phillips | dbc8eeb | 2017-02-21 10:04:31 -0500 | [diff] [blame] | 185 | Direction, int radius, MorphologyType, const float bounds[2]); |
| 186 | |
Brian Salomon | 0c26a9d | 2017-07-06 10:09:38 -0400 | [diff] [blame] | 187 | GR_DECLARE_FRAGMENT_PROCESSOR_TEST |
senorblanco@chromium.org | 84207c4 | 2012-08-22 20:51:19 +0000 | [diff] [blame] | 188 | |
| 189 | typedef Gr1DKernelEffect INHERITED; |
| 190 | }; |
| 191 | |
| 192 | /////////////////////////////////////////////////////////////////////////////// |
| 193 | |
egdaniel | 64c4728 | 2015-11-13 06:54:19 -0800 | [diff] [blame] | 194 | class GrGLMorphologyEffect : public GrGLSLFragmentProcessor { |
senorblanco@chromium.org | 84207c4 | 2012-08-22 20:51:19 +0000 | [diff] [blame] | 195 | public: |
robertphillips | 9cdb992 | 2016-02-03 12:25:40 -0800 | [diff] [blame] | 196 | void emitCode(EmitArgs&) override; |
senorblanco@chromium.org | 84207c4 | 2012-08-22 20:51:19 +0000 | [diff] [blame] | 197 | |
Brian Salomon | 94efbf5 | 2016-11-29 13:43:05 -0500 | [diff] [blame] | 198 | static inline void GenKey(const GrProcessor&, const GrShaderCaps&, GrProcessorKeyBuilder*); |
senorblanco@chromium.org | 84207c4 | 2012-08-22 20:51:19 +0000 | [diff] [blame] | 199 | |
wangyix | b1daa86 | 2015-08-18 11:29:31 -0700 | [diff] [blame] | 200 | protected: |
Brian Salomon | ab015ef | 2017-04-04 10:15:51 -0400 | [diff] [blame] | 201 | void onSetData(const GrGLSLProgramDataManager&, const GrFragmentProcessor&) override; |
senorblanco@chromium.org | 84207c4 | 2012-08-22 20:51:19 +0000 | [diff] [blame] | 202 | |
| 203 | private: |
egdaniel | 018fb62 | 2015-10-28 07:26:40 -0700 | [diff] [blame] | 204 | GrGLSLProgramDataManager::UniformHandle fPixelSizeUni; |
| 205 | GrGLSLProgramDataManager::UniformHandle fRangeUni; |
senorblanco@chromium.org | 84207c4 | 2012-08-22 20:51:19 +0000 | [diff] [blame] | 206 | |
egdaniel | 64c4728 | 2015-11-13 06:54:19 -0800 | [diff] [blame] | 207 | typedef GrGLSLFragmentProcessor INHERITED; |
senorblanco@chromium.org | 84207c4 | 2012-08-22 20:51:19 +0000 | [diff] [blame] | 208 | }; |
| 209 | |
wangyix | 7c157a9 | 2015-07-22 15:08:53 -0700 | [diff] [blame] | 210 | void GrGLMorphologyEffect::emitCode(EmitArgs& args) { |
robertphillips | bf536af | 2016-02-04 06:11:53 -0800 | [diff] [blame] | 211 | const GrMorphologyEffect& me = args.fFp.cast<GrMorphologyEffect>(); |
| 212 | |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 213 | GrGLSLUniformHandler* uniformHandler = args.fUniformHandler; |
cdalton | 5e58cee | 2016-02-11 12:49:47 -0800 | [diff] [blame] | 214 | fPixelSizeUni = uniformHandler->addUniform(kFragment_GrShaderFlag, |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 215 | kFloat_GrSLType, kDefault_GrSLPrecision, |
| 216 | "PixelSize"); |
| 217 | const char* pixelSizeInc = uniformHandler->getUniformCStr(fPixelSizeUni); |
cdalton | 5e58cee | 2016-02-11 12:49:47 -0800 | [diff] [blame] | 218 | fRangeUni = uniformHandler->addUniform(kFragment_GrShaderFlag, |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 219 | kVec2f_GrSLType, kDefault_GrSLPrecision, |
| 220 | "Range"); |
| 221 | const char* range = uniformHandler->getUniformCStr(fRangeUni); |
senorblanco@chromium.org | 84207c4 | 2012-08-22 20:51:19 +0000 | [diff] [blame] | 222 | |
cdalton | 8528541 | 2016-02-18 12:37:07 -0800 | [diff] [blame] | 223 | GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder; |
bsalomon | 1a1aa93 | 2016-09-12 09:30:36 -0700 | [diff] [blame] | 224 | SkString coords2D = fragBuilder->ensureCoords2D(args.fTransformedCoords[0]); |
senorblanco@chromium.org | 84207c4 | 2012-08-22 20:51:19 +0000 | [diff] [blame] | 225 | const char* func; |
robertphillips | bf536af | 2016-02-04 06:11:53 -0800 | [diff] [blame] | 226 | switch (me.type()) { |
senorblanco@chromium.org | 84207c4 | 2012-08-22 20:51:19 +0000 | [diff] [blame] | 227 | case GrMorphologyEffect::kErode_MorphologyType: |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 228 | 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] | 229 | func = "min"; |
| 230 | break; |
| 231 | case GrMorphologyEffect::kDilate_MorphologyType: |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 232 | 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] | 233 | func = "max"; |
| 234 | break; |
| 235 | default: |
commit-bot@chromium.org | 88cb22b | 2014-04-30 14:17:00 +0000 | [diff] [blame] | 236 | SkFAIL("Unexpected type"); |
senorblanco@chromium.org | 84207c4 | 2012-08-22 20:51:19 +0000 | [diff] [blame] | 237 | func = ""; // suppress warning |
| 238 | break; |
| 239 | } |
senorblanco@chromium.org | 84207c4 | 2012-08-22 20:51:19 +0000 | [diff] [blame] | 240 | |
cwallez | 80a61df | 2015-01-26 12:20:14 -0800 | [diff] [blame] | 241 | const char* dir; |
robertphillips | bf536af | 2016-02-04 06:11:53 -0800 | [diff] [blame] | 242 | switch (me.direction()) { |
cwallez | 80a61df | 2015-01-26 12:20:14 -0800 | [diff] [blame] | 243 | case Gr1DKernelEffect::kX_Direction: |
| 244 | dir = "x"; |
| 245 | break; |
| 246 | case Gr1DKernelEffect::kY_Direction: |
| 247 | dir = "y"; |
| 248 | break; |
| 249 | default: |
| 250 | SkFAIL("Unknown filter direction."); |
| 251 | dir = ""; // suppress warning |
| 252 | } |
| 253 | |
robertphillips | bf536af | 2016-02-04 06:11:53 -0800 | [diff] [blame] | 254 | int width = GrMorphologyEffect::WidthFromRadius(me.radius()); |
| 255 | |
cwallez | 80a61df | 2015-01-26 12:20:14 -0800 | [diff] [blame] | 256 | // vec2 coord = coord2D; |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 257 | fragBuilder->codeAppendf("\t\tvec2 coord = %s;\n", coords2D.c_str()); |
cwallez | 80a61df | 2015-01-26 12:20:14 -0800 | [diff] [blame] | 258 | // coord.x -= radius * pixelSize; |
robertphillips | bf536af | 2016-02-04 06:11:53 -0800 | [diff] [blame] | 259 | fragBuilder->codeAppendf("\t\tcoord.%s -= %d.0 * %s; \n", dir, me.radius(), pixelSizeInc); |
| 260 | if (me.useRange()) { |
cwallez | 80a61df | 2015-01-26 12:20:14 -0800 | [diff] [blame] | 261 | // highBound = min(highBound, coord.x + (width-1) * pixelSize); |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 262 | fragBuilder->codeAppendf("\t\tfloat highBound = min(%s.y, coord.%s + %f * %s);", |
robertphillips | bf536af | 2016-02-04 06:11:53 -0800 | [diff] [blame] | 263 | range, dir, float(width - 1), pixelSizeInc); |
cwallez | 80a61df | 2015-01-26 12:20:14 -0800 | [diff] [blame] | 264 | // coord.x = max(lowBound, coord.x); |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 265 | fragBuilder->codeAppendf("\t\tcoord.%s = max(%s.x, coord.%s);", dir, range, dir); |
cwallez | 80a61df | 2015-01-26 12:20:14 -0800 | [diff] [blame] | 266 | } |
robertphillips | bf536af | 2016-02-04 06:11:53 -0800 | [diff] [blame] | 267 | fragBuilder->codeAppendf("\t\tfor (int i = 0; i < %d; i++) {\n", width); |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 268 | fragBuilder->codeAppendf("\t\t\t%s = %s(%s, ", args.fOutputColor, func, args.fOutputColor); |
cdalton | 3f6f76f | 2016-04-11 12:18:09 -0700 | [diff] [blame] | 269 | fragBuilder->appendTextureLookup(args.fTexSamplers[0], "coord"); |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 270 | fragBuilder->codeAppend(");\n"); |
cwallez | 80a61df | 2015-01-26 12:20:14 -0800 | [diff] [blame] | 271 | // coord.x += pixelSize; |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 272 | fragBuilder->codeAppendf("\t\t\tcoord.%s += %s;\n", dir, pixelSizeInc); |
robertphillips | bf536af | 2016-02-04 06:11:53 -0800 | [diff] [blame] | 273 | if (me.useRange()) { |
cwallez | 80a61df | 2015-01-26 12:20:14 -0800 | [diff] [blame] | 274 | // coord.x = min(highBound, coord.x); |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 275 | fragBuilder->codeAppendf("\t\t\tcoord.%s = min(highBound, coord.%s);", dir, dir); |
cwallez | 80a61df | 2015-01-26 12:20:14 -0800 | [diff] [blame] | 276 | } |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 277 | fragBuilder->codeAppend("\t\t}\n"); |
Ethan Nicholas | 2983f40 | 2017-05-08 09:36:08 -0400 | [diff] [blame] | 278 | fragBuilder->codeAppendf("%s *= %s;\n", args.fOutputColor, args.fInputColor); |
senorblanco@chromium.org | 84207c4 | 2012-08-22 20:51:19 +0000 | [diff] [blame] | 279 | } |
| 280 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 281 | void GrGLMorphologyEffect::GenKey(const GrProcessor& proc, |
Brian Salomon | 94efbf5 | 2016-11-29 13:43:05 -0500 | [diff] [blame] | 282 | const GrShaderCaps&, GrProcessorKeyBuilder* b) { |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 283 | const GrMorphologyEffect& m = proc.cast<GrMorphologyEffect>(); |
bsalomon | 63e99f7 | 2014-07-21 08:03:14 -0700 | [diff] [blame] | 284 | uint32_t key = static_cast<uint32_t>(m.radius()); |
senorblanco@chromium.org | 84207c4 | 2012-08-22 20:51:19 +0000 | [diff] [blame] | 285 | key |= (m.type() << 8); |
cwallez | 80a61df | 2015-01-26 12:20:14 -0800 | [diff] [blame] | 286 | key |= (m.direction() << 9); |
robertphillips | bf536af | 2016-02-04 06:11:53 -0800 | [diff] [blame] | 287 | if (m.useRange()) { |
| 288 | key |= 1 << 10; |
| 289 | } |
bsalomon | 63e99f7 | 2014-07-21 08:03:14 -0700 | [diff] [blame] | 290 | b->add32(key); |
senorblanco@chromium.org | 84207c4 | 2012-08-22 20:51:19 +0000 | [diff] [blame] | 291 | } |
| 292 | |
egdaniel | 018fb62 | 2015-10-28 07:26:40 -0700 | [diff] [blame] | 293 | void GrGLMorphologyEffect::onSetData(const GrGLSLProgramDataManager& pdman, |
Brian Salomon | ab015ef | 2017-04-04 10:15:51 -0400 | [diff] [blame] | 294 | const GrFragmentProcessor& proc) { |
cwallez | 80a61df | 2015-01-26 12:20:14 -0800 | [diff] [blame] | 295 | const GrMorphologyEffect& m = proc.cast<GrMorphologyEffect>(); |
Robert Phillips | c686ce3 | 2017-07-21 14:12:29 -0400 | [diff] [blame^] | 296 | GrSurfaceProxy* proxy = m.textureSampler(0).proxy(); |
| 297 | GrTexture& texture = *proxy->priv().peekTexture(); |
cwallez | 80a61df | 2015-01-26 12:20:14 -0800 | [diff] [blame] | 298 | |
| 299 | float pixelSize = 0.0f; |
robertphillips | bf536af | 2016-02-04 06:11:53 -0800 | [diff] [blame] | 300 | switch (m.direction()) { |
senorblanco@chromium.org | 84207c4 | 2012-08-22 20:51:19 +0000 | [diff] [blame] | 301 | case Gr1DKernelEffect::kX_Direction: |
cwallez | 80a61df | 2015-01-26 12:20:14 -0800 | [diff] [blame] | 302 | pixelSize = 1.0f / texture.width(); |
senorblanco@chromium.org | 84207c4 | 2012-08-22 20:51:19 +0000 | [diff] [blame] | 303 | break; |
| 304 | case Gr1DKernelEffect::kY_Direction: |
cwallez | 80a61df | 2015-01-26 12:20:14 -0800 | [diff] [blame] | 305 | pixelSize = 1.0f / texture.height(); |
senorblanco@chromium.org | 84207c4 | 2012-08-22 20:51:19 +0000 | [diff] [blame] | 306 | break; |
| 307 | default: |
commit-bot@chromium.org | 88cb22b | 2014-04-30 14:17:00 +0000 | [diff] [blame] | 308 | SkFAIL("Unknown filter direction."); |
senorblanco@chromium.org | 84207c4 | 2012-08-22 20:51:19 +0000 | [diff] [blame] | 309 | } |
cwallez | 80a61df | 2015-01-26 12:20:14 -0800 | [diff] [blame] | 310 | pdman.set1f(fPixelSizeUni, pixelSize); |
| 311 | |
robertphillips | bf536af | 2016-02-04 06:11:53 -0800 | [diff] [blame] | 312 | if (m.useRange()) { |
cwallez | 80a61df | 2015-01-26 12:20:14 -0800 | [diff] [blame] | 313 | const float* range = m.range(); |
Robert Phillips | 7f6cd90 | 2016-11-10 17:03:43 -0500 | [diff] [blame] | 314 | if (Gr1DKernelEffect::kY_Direction == m.direction() && |
Robert Phillips | c686ce3 | 2017-07-21 14:12:29 -0400 | [diff] [blame^] | 315 | proxy->origin() == kBottomLeft_GrSurfaceOrigin) { |
Robert Phillips | 7f6cd90 | 2016-11-10 17:03:43 -0500 | [diff] [blame] | 316 | pdman.set2f(fRangeUni, 1.0f - (range[1]*pixelSize), 1.0f - (range[0]*pixelSize)); |
cwallez | 80a61df | 2015-01-26 12:20:14 -0800 | [diff] [blame] | 317 | } else { |
Robert Phillips | 7f6cd90 | 2016-11-10 17:03:43 -0500 | [diff] [blame] | 318 | pdman.set2f(fRangeUni, range[0] * pixelSize, range[1] * pixelSize); |
cwallez | 80a61df | 2015-01-26 12:20:14 -0800 | [diff] [blame] | 319 | } |
| 320 | } |
senorblanco@chromium.org | 84207c4 | 2012-08-22 20:51:19 +0000 | [diff] [blame] | 321 | } |
| 322 | |
| 323 | /////////////////////////////////////////////////////////////////////////////// |
| 324 | |
Robert Phillips | fbcef6e | 2017-06-15 12:07:18 -0400 | [diff] [blame] | 325 | GrMorphologyEffect::GrMorphologyEffect(sk_sp<GrTextureProxy> proxy, |
Robert Phillips | dbc8eeb | 2017-02-21 10:04:31 -0500 | [diff] [blame] | 326 | Direction direction, |
| 327 | int radius, |
| 328 | MorphologyType type) |
Robert Phillips | fbcef6e | 2017-06-15 12:07:18 -0400 | [diff] [blame] | 329 | : INHERITED{ModulationFlags(proxy->config()), GR_PROXY_MOVE(proxy), direction, radius} |
Robert Phillips | dbc8eeb | 2017-02-21 10:04:31 -0500 | [diff] [blame] | 330 | , fType(type) |
| 331 | , fUseRange(false) { |
| 332 | this->initClassID<GrMorphologyEffect>(); |
| 333 | } |
| 334 | |
Robert Phillips | fbcef6e | 2017-06-15 12:07:18 -0400 | [diff] [blame] | 335 | GrMorphologyEffect::GrMorphologyEffect(sk_sp<GrTextureProxy> proxy, |
Robert Phillips | dbc8eeb | 2017-02-21 10:04:31 -0500 | [diff] [blame] | 336 | Direction direction, |
| 337 | int radius, |
| 338 | MorphologyType type, |
| 339 | const float range[2]) |
Robert Phillips | fbcef6e | 2017-06-15 12:07:18 -0400 | [diff] [blame] | 340 | : INHERITED{ModulationFlags(proxy->config()), GR_PROXY_MOVE(proxy), direction, radius} |
Robert Phillips | dbc8eeb | 2017-02-21 10:04:31 -0500 | [diff] [blame] | 341 | , fType(type) |
| 342 | , fUseRange(true) { |
| 343 | this->initClassID<GrMorphologyEffect>(); |
| 344 | fRange[0] = range[0]; |
| 345 | fRange[1] = range[1]; |
| 346 | } |
| 347 | |
senorblanco@chromium.org | 84207c4 | 2012-08-22 20:51:19 +0000 | [diff] [blame] | 348 | GrMorphologyEffect::~GrMorphologyEffect() { |
| 349 | } |
| 350 | |
Brian Salomon | 94efbf5 | 2016-11-29 13:43:05 -0500 | [diff] [blame] | 351 | void GrMorphologyEffect::onGetGLSLProcessorKey(const GrShaderCaps& caps, |
egdaniel | 57d3b03 | 2015-11-13 11:57:27 -0800 | [diff] [blame] | 352 | GrProcessorKeyBuilder* b) const { |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 353 | GrGLMorphologyEffect::GenKey(*this, caps, b); |
senorblanco@chromium.org | 84207c4 | 2012-08-22 20:51:19 +0000 | [diff] [blame] | 354 | } |
| 355 | |
egdaniel | 57d3b03 | 2015-11-13 11:57:27 -0800 | [diff] [blame] | 356 | GrGLSLFragmentProcessor* GrMorphologyEffect::onCreateGLSLInstance() const { |
robertphillips | bf536af | 2016-02-04 06:11:53 -0800 | [diff] [blame] | 357 | return new GrGLMorphologyEffect; |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 358 | } |
bsalomon | 0e08fc1 | 2014-10-15 08:19:04 -0700 | [diff] [blame] | 359 | bool GrMorphologyEffect::onIsEqual(const GrFragmentProcessor& sBase) const { |
joshualitt | 49586be | 2014-09-16 08:21:41 -0700 | [diff] [blame] | 360 | const GrMorphologyEffect& s = sBase.cast<GrMorphologyEffect>(); |
bsalomon | 420d7e9 | 2014-10-16 09:18:09 -0700 | [diff] [blame] | 361 | return (this->radius() == s.radius() && |
senorblanco@chromium.org | 84207c4 | 2012-08-22 20:51:19 +0000 | [diff] [blame] | 362 | this->direction() == s.direction() && |
cwallez | 80a61df | 2015-01-26 12:20:14 -0800 | [diff] [blame] | 363 | this->useRange() == s.useRange() && |
senorblanco@chromium.org | 84207c4 | 2012-08-22 20:51:19 +0000 | [diff] [blame] | 364 | this->type() == s.type()); |
| 365 | } |
| 366 | |
| 367 | /////////////////////////////////////////////////////////////////////////////// |
| 368 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 369 | GR_DEFINE_FRAGMENT_PROCESSOR_TEST(GrMorphologyEffect); |
senorblanco@chromium.org | 84207c4 | 2012-08-22 20:51:19 +0000 | [diff] [blame] | 370 | |
Hal Canary | 6f6961e | 2017-01-31 13:50:44 -0500 | [diff] [blame] | 371 | #if GR_TEST_UTILS |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 372 | sk_sp<GrFragmentProcessor> GrMorphologyEffect::TestCreate(GrProcessorTestData* d) { |
Robert Phillips | dbc8eeb | 2017-02-21 10:04:31 -0500 | [diff] [blame] | 373 | int texIdx = d->fRandom->nextBool() ? GrProcessorUnitTest::kSkiaPMTextureIdx |
| 374 | : GrProcessorUnitTest::kAlphaTextureIdx; |
| 375 | sk_sp<GrTextureProxy> proxy = d->textureProxy(texIdx); |
| 376 | |
joshualitt | 0067ff5 | 2015-07-08 14:26:19 -0700 | [diff] [blame] | 377 | Direction dir = d->fRandom->nextBool() ? kX_Direction : kY_Direction; |
senorblanco@chromium.org | 84207c4 | 2012-08-22 20:51:19 +0000 | [diff] [blame] | 378 | static const int kMaxRadius = 10; |
joshualitt | 0067ff5 | 2015-07-08 14:26:19 -0700 | [diff] [blame] | 379 | int radius = d->fRandom->nextRangeU(1, kMaxRadius); |
Robert Phillips | dbc8eeb | 2017-02-21 10:04:31 -0500 | [diff] [blame] | 380 | MorphologyType type = d->fRandom->nextBool() ? GrMorphologyEffect::kErode_MorphologyType |
| 381 | : GrMorphologyEffect::kDilate_MorphologyType; |
senorblanco@chromium.org | 84207c4 | 2012-08-22 20:51:19 +0000 | [diff] [blame] | 382 | |
Robert Phillips | fbcef6e | 2017-06-15 12:07:18 -0400 | [diff] [blame] | 383 | return GrMorphologyEffect::Make(std::move(proxy), dir, radius, type); |
senorblanco@chromium.org | 84207c4 | 2012-08-22 20:51:19 +0000 | [diff] [blame] | 384 | } |
Hal Canary | 6f6961e | 2017-01-31 13:50:44 -0500 | [diff] [blame] | 385 | #endif |
senorblanco@chromium.org | 84207c4 | 2012-08-22 20:51:19 +0000 | [diff] [blame] | 386 | |
senorblanco@chromium.org | 84207c4 | 2012-08-22 20:51:19 +0000 | [diff] [blame] | 387 | |
Robert Phillips | 296b1cc | 2017-03-15 10:42:12 -0400 | [diff] [blame] | 388 | static void apply_morphology_rect(GrRenderTargetContext* renderTargetContext, |
robertphillips | f299e71 | 2016-03-25 04:49:22 -0700 | [diff] [blame] | 389 | const GrClip& clip, |
Robert Phillips | 32f2818 | 2017-02-28 16:20:03 -0500 | [diff] [blame] | 390 | sk_sp<GrTextureProxy> proxy, |
robertphillips | f299e71 | 2016-03-25 04:49:22 -0700 | [diff] [blame] | 391 | const SkIRect& srcRect, |
| 392 | const SkIRect& dstRect, |
| 393 | int radius, |
| 394 | GrMorphologyEffect::MorphologyType morphType, |
Robert Phillips | 7f6cd90 | 2016-11-10 17:03:43 -0500 | [diff] [blame] | 395 | const float bounds[2], |
robertphillips | f299e71 | 2016-03-25 04:49:22 -0700 | [diff] [blame] | 396 | Gr1DKernelEffect::Direction direction) { |
cwallez | 80a61df | 2015-01-26 12:20:14 -0800 | [diff] [blame] | 397 | GrPaint paint; |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 398 | paint.setGammaCorrect(renderTargetContext->isGammaCorrect()); |
Robert Phillips | 32f2818 | 2017-02-28 16:20:03 -0500 | [diff] [blame] | 399 | |
Robert Phillips | fbcef6e | 2017-06-15 12:07:18 -0400 | [diff] [blame] | 400 | paint.addColorFragmentProcessor(GrMorphologyEffect::Make(std::move(proxy), |
Robert Phillips | 32f2818 | 2017-02-28 16:20:03 -0500 | [diff] [blame] | 401 | direction, radius, morphType, |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 402 | bounds)); |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 403 | paint.setPorterDuffXPFactory(SkBlendMode::kSrc); |
Brian Salomon | 82f4431 | 2017-01-11 13:42:54 -0500 | [diff] [blame] | 404 | renderTargetContext->fillRectToRect(clip, std::move(paint), GrAA::kNo, SkMatrix::I(), |
| 405 | SkRect::Make(dstRect), SkRect::Make(srcRect)); |
cwallez | 80a61df | 2015-01-26 12:20:14 -0800 | [diff] [blame] | 406 | } |
| 407 | |
Robert Phillips | 296b1cc | 2017-03-15 10:42:12 -0400 | [diff] [blame] | 408 | static void apply_morphology_rect_no_bounds(GrRenderTargetContext* renderTargetContext, |
robertphillips | f299e71 | 2016-03-25 04:49:22 -0700 | [diff] [blame] | 409 | const GrClip& clip, |
Robert Phillips | 32f2818 | 2017-02-28 16:20:03 -0500 | [diff] [blame] | 410 | sk_sp<GrTextureProxy> proxy, |
robertphillips | f299e71 | 2016-03-25 04:49:22 -0700 | [diff] [blame] | 411 | const SkIRect& srcRect, |
| 412 | const SkIRect& dstRect, |
| 413 | int radius, |
| 414 | GrMorphologyEffect::MorphologyType morphType, |
| 415 | Gr1DKernelEffect::Direction direction) { |
senorblanco@chromium.org | 84207c4 | 2012-08-22 20:51:19 +0000 | [diff] [blame] | 416 | GrPaint paint; |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 417 | paint.setGammaCorrect(renderTargetContext->isGammaCorrect()); |
Robert Phillips | 32f2818 | 2017-02-28 16:20:03 -0500 | [diff] [blame] | 418 | |
Robert Phillips | fbcef6e | 2017-06-15 12:07:18 -0400 | [diff] [blame] | 419 | paint.addColorFragmentProcessor(GrMorphologyEffect::Make(std::move(proxy), |
Robert Phillips | 32f2818 | 2017-02-28 16:20:03 -0500 | [diff] [blame] | 420 | direction, radius, morphType)); |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 421 | paint.setPorterDuffXPFactory(SkBlendMode::kSrc); |
Brian Salomon | 82f4431 | 2017-01-11 13:42:54 -0500 | [diff] [blame] | 422 | renderTargetContext->fillRectToRect(clip, std::move(paint), GrAA::kNo, SkMatrix::I(), |
Brian Salomon | 0e8fc8b | 2016-12-09 15:10:07 -0500 | [diff] [blame] | 423 | SkRect::Make(dstRect), SkRect::Make(srcRect)); |
senorblanco@chromium.org | 84207c4 | 2012-08-22 20:51:19 +0000 | [diff] [blame] | 424 | } |
| 425 | |
Robert Phillips | 296b1cc | 2017-03-15 10:42:12 -0400 | [diff] [blame] | 426 | static void apply_morphology_pass(GrRenderTargetContext* renderTargetContext, |
robertphillips | f299e71 | 2016-03-25 04:49:22 -0700 | [diff] [blame] | 427 | const GrClip& clip, |
Robert Phillips | 32f2818 | 2017-02-28 16:20:03 -0500 | [diff] [blame] | 428 | sk_sp<GrTextureProxy> textureProxy, |
robertphillips | f299e71 | 2016-03-25 04:49:22 -0700 | [diff] [blame] | 429 | const SkIRect& srcRect, |
| 430 | const SkIRect& dstRect, |
| 431 | int radius, |
| 432 | GrMorphologyEffect::MorphologyType morphType, |
| 433 | Gr1DKernelEffect::Direction direction) { |
cwallez | 80a61df | 2015-01-26 12:20:14 -0800 | [diff] [blame] | 434 | float bounds[2] = { 0.0f, 1.0f }; |
| 435 | SkIRect lowerSrcRect = srcRect, lowerDstRect = dstRect; |
| 436 | SkIRect middleSrcRect = srcRect, middleDstRect = dstRect; |
| 437 | SkIRect upperSrcRect = srcRect, upperDstRect = dstRect; |
| 438 | if (direction == Gr1DKernelEffect::kX_Direction) { |
Robert Phillips | 7f6cd90 | 2016-11-10 17:03:43 -0500 | [diff] [blame] | 439 | bounds[0] = SkIntToScalar(srcRect.left()) + 0.5f; |
| 440 | bounds[1] = SkIntToScalar(srcRect.right()) - 0.5f; |
cwallez | 80a61df | 2015-01-26 12:20:14 -0800 | [diff] [blame] | 441 | lowerSrcRect.fRight = srcRect.left() + radius; |
| 442 | lowerDstRect.fRight = dstRect.left() + radius; |
| 443 | upperSrcRect.fLeft = srcRect.right() - radius; |
| 444 | upperDstRect.fLeft = dstRect.right() - radius; |
| 445 | middleSrcRect.inset(radius, 0); |
| 446 | middleDstRect.inset(radius, 0); |
| 447 | } else { |
Robert Phillips | 7f6cd90 | 2016-11-10 17:03:43 -0500 | [diff] [blame] | 448 | bounds[0] = SkIntToScalar(srcRect.top()) + 0.5f; |
| 449 | bounds[1] = SkIntToScalar(srcRect.bottom()) - 0.5f; |
cwallez | 80a61df | 2015-01-26 12:20:14 -0800 | [diff] [blame] | 450 | lowerSrcRect.fBottom = srcRect.top() + radius; |
| 451 | lowerDstRect.fBottom = dstRect.top() + radius; |
| 452 | upperSrcRect.fTop = srcRect.bottom() - radius; |
| 453 | upperDstRect.fTop = dstRect.bottom() - radius; |
| 454 | middleSrcRect.inset(0, radius); |
| 455 | middleDstRect.inset(0, radius); |
| 456 | } |
| 457 | if (middleSrcRect.fLeft - middleSrcRect.fRight >= 0) { |
| 458 | // radius covers srcRect; use bounds over entire draw |
Robert Phillips | 296b1cc | 2017-03-15 10:42:12 -0400 | [diff] [blame] | 459 | apply_morphology_rect(renderTargetContext, clip, std::move(textureProxy), |
Robert Phillips | 7f6cd90 | 2016-11-10 17:03:43 -0500 | [diff] [blame] | 460 | srcRect, dstRect, radius, morphType, bounds, direction); |
cwallez | 80a61df | 2015-01-26 12:20:14 -0800 | [diff] [blame] | 461 | } else { |
| 462 | // Draw upper and lower margins with bounds; middle without. |
Robert Phillips | 296b1cc | 2017-03-15 10:42:12 -0400 | [diff] [blame] | 463 | apply_morphology_rect(renderTargetContext, clip, textureProxy, |
Robert Phillips | 7f6cd90 | 2016-11-10 17:03:43 -0500 | [diff] [blame] | 464 | lowerSrcRect, lowerDstRect, radius, morphType, bounds, direction); |
Robert Phillips | 296b1cc | 2017-03-15 10:42:12 -0400 | [diff] [blame] | 465 | apply_morphology_rect(renderTargetContext, clip, textureProxy, |
Robert Phillips | 7f6cd90 | 2016-11-10 17:03:43 -0500 | [diff] [blame] | 466 | upperSrcRect, upperDstRect, radius, morphType, bounds, direction); |
Robert Phillips | 296b1cc | 2017-03-15 10:42:12 -0400 | [diff] [blame] | 467 | apply_morphology_rect_no_bounds(renderTargetContext, clip, std::move(textureProxy), |
Robert Phillips | 7f6cd90 | 2016-11-10 17:03:43 -0500 | [diff] [blame] | 468 | middleSrcRect, middleDstRect, radius, morphType, direction); |
cwallez | 80a61df | 2015-01-26 12:20:14 -0800 | [diff] [blame] | 469 | } |
| 470 | } |
| 471 | |
brianosman | 2a75e5d | 2016-09-22 07:15:37 -0700 | [diff] [blame] | 472 | static sk_sp<SkSpecialImage> apply_morphology( |
| 473 | GrContext* context, |
| 474 | SkSpecialImage* input, |
| 475 | const SkIRect& rect, |
| 476 | GrMorphologyEffect::MorphologyType morphType, |
| 477 | SkISize radius, |
| 478 | const SkImageFilter::OutputProperties& outputProperties) { |
Robert Phillips | 8e1c4e6 | 2017-02-19 12:27:01 -0500 | [diff] [blame] | 479 | sk_sp<GrTextureProxy> srcTexture(input->asTextureProxyRef(context)); |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 480 | SkASSERT(srcTexture); |
brianosman | 2a75e5d | 2016-09-22 07:15:37 -0700 | [diff] [blame] | 481 | sk_sp<SkColorSpace> colorSpace = sk_ref_sp(outputProperties.colorSpace()); |
brianosman | 2695eaa | 2016-09-21 06:45:09 -0700 | [diff] [blame] | 482 | GrPixelConfig config = GrRenderableConfigForColorSpace(colorSpace.get()); |
bsalomon@google.com | 3cbaa2d | 2012-10-12 14:51:52 +0000 | [diff] [blame] | 483 | |
joshualitt | 570d2f8 | 2015-02-25 13:19:48 -0800 | [diff] [blame] | 484 | // setup new clip |
cdalton | 846c051 | 2016-05-13 10:25:00 -0700 | [diff] [blame] | 485 | const GrFixedClip clip(SkIRect::MakeWH(srcTexture->width(), srcTexture->height())); |
bsalomon@google.com | 3cbaa2d | 2012-10-12 14:51:52 +0000 | [diff] [blame] | 486 | |
robertphillips | d4c741e | 2016-04-28 09:55:15 -0700 | [diff] [blame] | 487 | const SkIRect dstRect = SkIRect::MakeWH(rect.width(), rect.height()); |
senorblanco@chromium.org | 8fcad98 | 2013-09-17 13:41:43 +0000 | [diff] [blame] | 488 | SkIRect srcRect = rect; |
bsalomon@google.com | 3cbaa2d | 2012-10-12 14:51:52 +0000 | [diff] [blame] | 489 | |
robertphillips | f299e71 | 2016-03-25 04:49:22 -0700 | [diff] [blame] | 490 | SkASSERT(radius.width() > 0 || radius.height() > 0); |
| 491 | |
senorblanco@chromium.org | 84207c4 | 2012-08-22 20:51:19 +0000 | [diff] [blame] | 492 | if (radius.fWidth > 0) { |
Robert Phillips | 7f6cd90 | 2016-11-10 17:03:43 -0500 | [diff] [blame] | 493 | sk_sp<GrRenderTargetContext> dstRTContext(context->makeDeferredRenderTargetContext( |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 494 | SkBackingFit::kApprox, rect.width(), rect.height(), config, colorSpace)); |
Robert Phillips | 7f6cd90 | 2016-11-10 17:03:43 -0500 | [diff] [blame] | 495 | if (!dstRTContext) { |
robertphillips | f299e71 | 2016-03-25 04:49:22 -0700 | [diff] [blame] | 496 | return nullptr; |
robertphillips | ff0ca5e | 2015-07-22 11:54:44 -0700 | [diff] [blame] | 497 | } |
| 498 | |
Robert Phillips | 296b1cc | 2017-03-15 10:42:12 -0400 | [diff] [blame] | 499 | apply_morphology_pass(dstRTContext.get(), clip, std::move(srcTexture), |
joshualitt | 570d2f8 | 2015-02-25 13:19:48 -0800 | [diff] [blame] | 500 | srcRect, dstRect, radius.fWidth, morphType, |
| 501 | Gr1DKernelEffect::kX_Direction); |
senorblanco@chromium.org | 8fcad98 | 2013-09-17 13:41:43 +0000 | [diff] [blame] | 502 | SkIRect clearRect = SkIRect::MakeXYWH(dstRect.fLeft, dstRect.fBottom, |
| 503 | dstRect.width(), radius.fHeight); |
robertphillips | d4c741e | 2016-04-28 09:55:15 -0700 | [diff] [blame] | 504 | GrColor clearColor = GrMorphologyEffect::kErode_MorphologyType == morphType |
| 505 | ? SK_ColorWHITE |
| 506 | : SK_ColorTRANSPARENT; |
Robert Phillips | 7f6cd90 | 2016-11-10 17:03:43 -0500 | [diff] [blame] | 507 | dstRTContext->clear(&clearRect, clearColor, false); |
robertphillips | ff0ca5e | 2015-07-22 11:54:44 -0700 | [diff] [blame] | 508 | |
Robert Phillips | f200a90 | 2017-01-30 13:27:37 -0500 | [diff] [blame] | 509 | srcTexture = dstRTContext->asTextureProxyRef(); |
senorblanco@chromium.org | 8fcad98 | 2013-09-17 13:41:43 +0000 | [diff] [blame] | 510 | srcRect = dstRect; |
senorblanco@chromium.org | 84207c4 | 2012-08-22 20:51:19 +0000 | [diff] [blame] | 511 | } |
| 512 | if (radius.fHeight > 0) { |
Robert Phillips | 7f6cd90 | 2016-11-10 17:03:43 -0500 | [diff] [blame] | 513 | sk_sp<GrRenderTargetContext> dstRTContext(context->makeDeferredRenderTargetContext( |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 514 | SkBackingFit::kApprox, rect.width(), rect.height(), config, colorSpace)); |
Robert Phillips | 7f6cd90 | 2016-11-10 17:03:43 -0500 | [diff] [blame] | 515 | if (!dstRTContext) { |
robertphillips | f299e71 | 2016-03-25 04:49:22 -0700 | [diff] [blame] | 516 | return nullptr; |
robertphillips | ff0ca5e | 2015-07-22 11:54:44 -0700 | [diff] [blame] | 517 | } |
| 518 | |
Robert Phillips | 296b1cc | 2017-03-15 10:42:12 -0400 | [diff] [blame] | 519 | apply_morphology_pass(dstRTContext.get(), clip, std::move(srcTexture), |
joshualitt | 570d2f8 | 2015-02-25 13:19:48 -0800 | [diff] [blame] | 520 | srcRect, dstRect, radius.fHeight, morphType, |
| 521 | Gr1DKernelEffect::kY_Direction); |
robertphillips | ff0ca5e | 2015-07-22 11:54:44 -0700 | [diff] [blame] | 522 | |
Robert Phillips | f200a90 | 2017-01-30 13:27:37 -0500 | [diff] [blame] | 523 | srcTexture = dstRTContext->asTextureProxyRef(); |
senorblanco@chromium.org | 84207c4 | 2012-08-22 20:51:19 +0000 | [diff] [blame] | 524 | } |
robertphillips | f299e71 | 2016-03-25 04:49:22 -0700 | [diff] [blame] | 525 | |
Robert Phillips | 7f6cd90 | 2016-11-10 17:03:43 -0500 | [diff] [blame] | 526 | return SkSpecialImage::MakeDeferredFromGpu(context, |
| 527 | SkIRect::MakeWH(rect.width(), rect.height()), |
| 528 | kNeedNewImageUniqueID_SpecialImage, |
| 529 | std::move(srcTexture), std::move(colorSpace), |
| 530 | &input->props()); |
senorblanco@chromium.org | 84207c4 | 2012-08-22 20:51:19 +0000 | [diff] [blame] | 531 | } |
robertphillips | f299e71 | 2016-03-25 04:49:22 -0700 | [diff] [blame] | 532 | #endif |
senorblanco@chromium.org | 84207c4 | 2012-08-22 20:51:19 +0000 | [diff] [blame] | 533 | |
robertphillips | 40d8d62 | 2016-03-30 08:09:56 -0700 | [diff] [blame] | 534 | sk_sp<SkSpecialImage> SkMorphologyImageFilter::onFilterImage(SkSpecialImage* source, |
| 535 | const Context& ctx, |
| 536 | SkIPoint* offset) const { |
robertphillips | f299e71 | 2016-03-25 04:49:22 -0700 | [diff] [blame] | 537 | SkIPoint inputOffset = SkIPoint::Make(0, 0); |
| 538 | sk_sp<SkSpecialImage> input(this->filterInput(0, source, ctx, &inputOffset)); |
| 539 | if (!input) { |
| 540 | return nullptr; |
senorblanco@chromium.org | c2594f4 | 2013-01-30 19:08:47 +0000 | [diff] [blame] | 541 | } |
robertphillips | f299e71 | 2016-03-25 04:49:22 -0700 | [diff] [blame] | 542 | |
senorblanco@chromium.org | c2594f4 | 2013-01-30 19:08:47 +0000 | [diff] [blame] | 543 | SkIRect bounds; |
robertphillips | f299e71 | 2016-03-25 04:49:22 -0700 | [diff] [blame] | 544 | input = this->applyCropRect(this->mapContext(ctx), input.get(), &inputOffset, &bounds); |
| 545 | if (!input) { |
| 546 | return nullptr; |
senorblanco@chromium.org | 8fcad98 | 2013-09-17 13:41:43 +0000 | [diff] [blame] | 547 | } |
robertphillips | f299e71 | 2016-03-25 04:49:22 -0700 | [diff] [blame] | 548 | |
senorblanco@chromium.org | 2bfe36b | 2014-01-20 19:58:28 +0000 | [diff] [blame] | 549 | SkVector radius = SkVector::Make(SkIntToScalar(this->radius().width()), |
| 550 | SkIntToScalar(this->radius().height())); |
senorblanco@chromium.org | 4cb543d | 2014-03-14 15:44:01 +0000 | [diff] [blame] | 551 | ctx.ctm().mapVectors(&radius, 1); |
senorblanco@chromium.org | 2bfe36b | 2014-01-20 19:58:28 +0000 | [diff] [blame] | 552 | int width = SkScalarFloorToInt(radius.fX); |
| 553 | int height = SkScalarFloorToInt(radius.fY); |
senorblanco@chromium.org | 8fcad98 | 2013-09-17 13:41:43 +0000 | [diff] [blame] | 554 | |
| 555 | if (width < 0 || height < 0) { |
robertphillips | f299e71 | 2016-03-25 04:49:22 -0700 | [diff] [blame] | 556 | return nullptr; |
senorblanco@chromium.org | 8fcad98 | 2013-09-17 13:41:43 +0000 | [diff] [blame] | 557 | } |
| 558 | |
senorblanco@chromium.org | 2bfe36b | 2014-01-20 19:58:28 +0000 | [diff] [blame] | 559 | SkIRect srcBounds = bounds; |
robertphillips | f299e71 | 2016-03-25 04:49:22 -0700 | [diff] [blame] | 560 | srcBounds.offset(-inputOffset); |
| 561 | |
| 562 | if (0 == width && 0 == height) { |
senorblanco@chromium.org | 6776b82 | 2014-01-03 21:48:22 +0000 | [diff] [blame] | 563 | offset->fX = bounds.left(); |
| 564 | offset->fY = bounds.top(); |
robertphillips | f299e71 | 2016-03-25 04:49:22 -0700 | [diff] [blame] | 565 | return input->makeSubset(srcBounds); |
senorblanco@chromium.org | 8fcad98 | 2013-09-17 13:41:43 +0000 | [diff] [blame] | 566 | } |
| 567 | |
robertphillips | f299e71 | 2016-03-25 04:49:22 -0700 | [diff] [blame] | 568 | #if SK_SUPPORT_GPU |
robertphillips | 6461251 | 2016-04-08 12:10:42 -0700 | [diff] [blame] | 569 | if (source->isTextureBacked()) { |
| 570 | GrContext* context = source->getContext(); |
| 571 | |
Brian Osman | 615d66d | 2016-12-29 09:18:20 -0500 | [diff] [blame] | 572 | // Ensure the input is in the destination color space. Typically applyCropRect will have |
| 573 | // called pad_image to account for our dilation of bounds, so the result will already be |
| 574 | // moved to the destination color space. If a filter DAG avoids that, then we use this |
| 575 | // fall-back, which saves us from having to do the xform during the filter itself. |
| 576 | input = ImageToColorSpace(input.get(), ctx.outputProperties()); |
Brian Osman | 320573f | 2016-12-21 13:36:02 -0500 | [diff] [blame] | 577 | |
robertphillips | 40d8d62 | 2016-03-30 08:09:56 -0700 | [diff] [blame] | 578 | auto type = (kDilate_Op == this->op()) ? GrMorphologyEffect::kDilate_MorphologyType |
| 579 | : GrMorphologyEffect::kErode_MorphologyType; |
robertphillips | 6461251 | 2016-04-08 12:10:42 -0700 | [diff] [blame] | 580 | sk_sp<SkSpecialImage> result(apply_morphology(context, input.get(), srcBounds, type, |
brianosman | 2a75e5d | 2016-09-22 07:15:37 -0700 | [diff] [blame] | 581 | SkISize::Make(width, height), |
| 582 | ctx.outputProperties())); |
robertphillips | f299e71 | 2016-03-25 04:49:22 -0700 | [diff] [blame] | 583 | if (result) { |
| 584 | offset->fX = bounds.left(); |
| 585 | offset->fY = bounds.top(); |
| 586 | } |
| 587 | return result; |
| 588 | } |
| 589 | #endif |
| 590 | |
robertphillips | 6461251 | 2016-04-08 12:10:42 -0700 | [diff] [blame] | 591 | SkBitmap inputBM; |
robertphillips | f299e71 | 2016-03-25 04:49:22 -0700 | [diff] [blame] | 592 | |
robertphillips | 6461251 | 2016-04-08 12:10:42 -0700 | [diff] [blame] | 593 | if (!input->getROPixels(&inputBM)) { |
robertphillips | f299e71 | 2016-03-25 04:49:22 -0700 | [diff] [blame] | 594 | return nullptr; |
| 595 | } |
| 596 | |
robertphillips | 6461251 | 2016-04-08 12:10:42 -0700 | [diff] [blame] | 597 | if (inputBM.colorType() != kN32_SkColorType) { |
robertphillips | f299e71 | 2016-03-25 04:49:22 -0700 | [diff] [blame] | 598 | return nullptr; |
| 599 | } |
| 600 | |
| 601 | SkImageInfo info = SkImageInfo::Make(bounds.width(), bounds.height(), |
robertphillips | 6461251 | 2016-04-08 12:10:42 -0700 | [diff] [blame] | 602 | inputBM.colorType(), inputBM.alphaType()); |
robertphillips | f299e71 | 2016-03-25 04:49:22 -0700 | [diff] [blame] | 603 | |
| 604 | SkBitmap dst; |
| 605 | if (!dst.tryAllocPixels(info)) { |
| 606 | return nullptr; |
| 607 | } |
| 608 | |
robertphillips | f299e71 | 2016-03-25 04:49:22 -0700 | [diff] [blame] | 609 | SkMorphologyImageFilter::Proc procX, procY; |
| 610 | |
robertphillips | 40d8d62 | 2016-03-30 08:09:56 -0700 | [diff] [blame] | 611 | if (kDilate_Op == this->op()) { |
robertphillips | f299e71 | 2016-03-25 04:49:22 -0700 | [diff] [blame] | 612 | procX = SkOpts::dilate_x; |
| 613 | procY = SkOpts::dilate_y; |
| 614 | } else { |
| 615 | procX = SkOpts::erode_x; |
| 616 | procY = SkOpts::erode_y; |
| 617 | } |
| 618 | |
| 619 | if (width > 0 && height > 0) { |
| 620 | SkBitmap tmp; |
| 621 | if (!tmp.tryAllocPixels(info)) { |
| 622 | return nullptr; |
| 623 | } |
| 624 | |
robertphillips | 6461251 | 2016-04-08 12:10:42 -0700 | [diff] [blame] | 625 | call_proc_X(procX, inputBM, &tmp, width, srcBounds); |
robertphillips | f299e71 | 2016-03-25 04:49:22 -0700 | [diff] [blame] | 626 | SkIRect tmpBounds = SkIRect::MakeWH(srcBounds.width(), srcBounds.height()); |
| 627 | call_proc_Y(procY, |
| 628 | tmp.getAddr32(tmpBounds.left(), tmpBounds.top()), tmp.rowBytesAsPixels(), |
| 629 | &dst, height, tmpBounds); |
| 630 | } else if (width > 0) { |
robertphillips | 6461251 | 2016-04-08 12:10:42 -0700 | [diff] [blame] | 631 | call_proc_X(procX, inputBM, &dst, width, srcBounds); |
robertphillips | f299e71 | 2016-03-25 04:49:22 -0700 | [diff] [blame] | 632 | } else if (height > 0) { |
| 633 | call_proc_Y(procY, |
robertphillips | 6461251 | 2016-04-08 12:10:42 -0700 | [diff] [blame] | 634 | inputBM.getAddr32(srcBounds.left(), srcBounds.top()), |
| 635 | inputBM.rowBytesAsPixels(), |
robertphillips | f299e71 | 2016-03-25 04:49:22 -0700 | [diff] [blame] | 636 | &dst, height, srcBounds); |
senorblanco@chromium.org | 8fcad98 | 2013-09-17 13:41:43 +0000 | [diff] [blame] | 637 | } |
senorblanco@chromium.org | 6776b82 | 2014-01-03 21:48:22 +0000 | [diff] [blame] | 638 | offset->fX = bounds.left(); |
| 639 | offset->fY = bounds.top(); |
robertphillips | f299e71 | 2016-03-25 04:49:22 -0700 | [diff] [blame] | 640 | |
robertphillips | 3e30227 | 2016-04-20 11:48:36 -0700 | [diff] [blame] | 641 | return SkSpecialImage::MakeFromRaster(SkIRect::MakeWH(bounds.width(), bounds.height()), |
brianosman | 898235c | 2016-04-06 07:38:23 -0700 | [diff] [blame] | 642 | dst, &source->props()); |
senorblanco@chromium.org | 05054f1 | 2012-03-02 21:05:45 +0000 | [diff] [blame] | 643 | } |
Matt Sarett | 6d72ed9 | 2017-04-10 16:35:33 -0400 | [diff] [blame] | 644 | |
Mike Reed | 6d9f429 | 2017-07-06 12:32:55 -0400 | [diff] [blame] | 645 | sk_sp<SkImageFilter> SkMorphologyImageFilter::onMakeColorSpace(SkColorSpaceXformer* xformer) const{ |
Matt Sarett | 6d72ed9 | 2017-04-10 16:35:33 -0400 | [diff] [blame] | 646 | SkASSERT(1 == this->countInputs()); |
Mike Reed | 6d9f429 | 2017-07-06 12:32:55 -0400 | [diff] [blame] | 647 | auto input = xformer->apply(this->getInput(0)); |
| 648 | if (input.get() != this->getInput(0)) { |
| 649 | return (SkMorphologyImageFilter::kDilate_Op == this->op()) |
| 650 | ? SkDilateImageFilter::Make(fRadius.width(), fRadius.height(), std::move(input), |
| 651 | this->getCropRectIfSet()) |
| 652 | : SkErodeImageFilter::Make(fRadius.width(), fRadius.height(), std::move(input), |
| 653 | this->getCropRectIfSet()); |
Matt Sarett | 6d72ed9 | 2017-04-10 16:35:33 -0400 | [diff] [blame] | 654 | } |
Mike Reed | 6d9f429 | 2017-07-06 12:32:55 -0400 | [diff] [blame] | 655 | return this->refMe(); |
Matt Sarett | 6d72ed9 | 2017-04-10 16:35:33 -0400 | [diff] [blame] | 656 | } |