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 | 9bee2e5 | 2017-05-29 12:37:20 -0400 | [diff] [blame] | 296 | GrTexture& texture = *m.textureSampler(0).peekTexture(); |
cwallez | 80a61df | 2015-01-26 12:20:14 -0800 | [diff] [blame] | 297 | |
| 298 | float pixelSize = 0.0f; |
robertphillips | bf536af | 2016-02-04 06:11:53 -0800 | [diff] [blame] | 299 | switch (m.direction()) { |
senorblanco@chromium.org | 84207c4 | 2012-08-22 20:51:19 +0000 | [diff] [blame] | 300 | case Gr1DKernelEffect::kX_Direction: |
cwallez | 80a61df | 2015-01-26 12:20:14 -0800 | [diff] [blame] | 301 | pixelSize = 1.0f / texture.width(); |
senorblanco@chromium.org | 84207c4 | 2012-08-22 20:51:19 +0000 | [diff] [blame] | 302 | break; |
| 303 | case Gr1DKernelEffect::kY_Direction: |
cwallez | 80a61df | 2015-01-26 12:20:14 -0800 | [diff] [blame] | 304 | pixelSize = 1.0f / texture.height(); |
senorblanco@chromium.org | 84207c4 | 2012-08-22 20:51:19 +0000 | [diff] [blame] | 305 | break; |
| 306 | default: |
commit-bot@chromium.org | 88cb22b | 2014-04-30 14:17:00 +0000 | [diff] [blame] | 307 | SkFAIL("Unknown filter direction."); |
senorblanco@chromium.org | 84207c4 | 2012-08-22 20:51:19 +0000 | [diff] [blame] | 308 | } |
cwallez | 80a61df | 2015-01-26 12:20:14 -0800 | [diff] [blame] | 309 | pdman.set1f(fPixelSizeUni, pixelSize); |
| 310 | |
robertphillips | bf536af | 2016-02-04 06:11:53 -0800 | [diff] [blame] | 311 | if (m.useRange()) { |
cwallez | 80a61df | 2015-01-26 12:20:14 -0800 | [diff] [blame] | 312 | const float* range = m.range(); |
Robert Phillips | 7f6cd90 | 2016-11-10 17:03:43 -0500 | [diff] [blame] | 313 | if (Gr1DKernelEffect::kY_Direction == m.direction() && |
| 314 | texture.origin() == kBottomLeft_GrSurfaceOrigin) { |
| 315 | pdman.set2f(fRangeUni, 1.0f - (range[1]*pixelSize), 1.0f - (range[0]*pixelSize)); |
cwallez | 80a61df | 2015-01-26 12:20:14 -0800 | [diff] [blame] | 316 | } else { |
Robert Phillips | 7f6cd90 | 2016-11-10 17:03:43 -0500 | [diff] [blame] | 317 | pdman.set2f(fRangeUni, range[0] * pixelSize, range[1] * pixelSize); |
cwallez | 80a61df | 2015-01-26 12:20:14 -0800 | [diff] [blame] | 318 | } |
| 319 | } |
senorblanco@chromium.org | 84207c4 | 2012-08-22 20:51:19 +0000 | [diff] [blame] | 320 | } |
| 321 | |
| 322 | /////////////////////////////////////////////////////////////////////////////// |
| 323 | |
Robert Phillips | fbcef6e | 2017-06-15 12:07:18 -0400 | [diff] [blame] | 324 | GrMorphologyEffect::GrMorphologyEffect(sk_sp<GrTextureProxy> proxy, |
Robert Phillips | dbc8eeb | 2017-02-21 10:04:31 -0500 | [diff] [blame] | 325 | Direction direction, |
| 326 | int radius, |
| 327 | MorphologyType type) |
Robert Phillips | fbcef6e | 2017-06-15 12:07:18 -0400 | [diff] [blame] | 328 | : INHERITED{ModulationFlags(proxy->config()), GR_PROXY_MOVE(proxy), direction, radius} |
Robert Phillips | dbc8eeb | 2017-02-21 10:04:31 -0500 | [diff] [blame] | 329 | , fType(type) |
| 330 | , fUseRange(false) { |
| 331 | this->initClassID<GrMorphologyEffect>(); |
| 332 | } |
| 333 | |
Robert Phillips | fbcef6e | 2017-06-15 12:07:18 -0400 | [diff] [blame] | 334 | GrMorphologyEffect::GrMorphologyEffect(sk_sp<GrTextureProxy> proxy, |
Robert Phillips | dbc8eeb | 2017-02-21 10:04:31 -0500 | [diff] [blame] | 335 | Direction direction, |
| 336 | int radius, |
| 337 | MorphologyType type, |
| 338 | const float range[2]) |
Robert Phillips | fbcef6e | 2017-06-15 12:07:18 -0400 | [diff] [blame] | 339 | : INHERITED{ModulationFlags(proxy->config()), GR_PROXY_MOVE(proxy), direction, radius} |
Robert Phillips | dbc8eeb | 2017-02-21 10:04:31 -0500 | [diff] [blame] | 340 | , fType(type) |
| 341 | , fUseRange(true) { |
| 342 | this->initClassID<GrMorphologyEffect>(); |
| 343 | fRange[0] = range[0]; |
| 344 | fRange[1] = range[1]; |
| 345 | } |
| 346 | |
senorblanco@chromium.org | 84207c4 | 2012-08-22 20:51:19 +0000 | [diff] [blame] | 347 | GrMorphologyEffect::~GrMorphologyEffect() { |
| 348 | } |
| 349 | |
Brian Salomon | 94efbf5 | 2016-11-29 13:43:05 -0500 | [diff] [blame] | 350 | void GrMorphologyEffect::onGetGLSLProcessorKey(const GrShaderCaps& caps, |
egdaniel | 57d3b03 | 2015-11-13 11:57:27 -0800 | [diff] [blame] | 351 | GrProcessorKeyBuilder* b) const { |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 352 | GrGLMorphologyEffect::GenKey(*this, caps, b); |
senorblanco@chromium.org | 84207c4 | 2012-08-22 20:51:19 +0000 | [diff] [blame] | 353 | } |
| 354 | |
egdaniel | 57d3b03 | 2015-11-13 11:57:27 -0800 | [diff] [blame] | 355 | GrGLSLFragmentProcessor* GrMorphologyEffect::onCreateGLSLInstance() const { |
robertphillips | bf536af | 2016-02-04 06:11:53 -0800 | [diff] [blame] | 356 | return new GrGLMorphologyEffect; |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 357 | } |
bsalomon | 0e08fc1 | 2014-10-15 08:19:04 -0700 | [diff] [blame] | 358 | bool GrMorphologyEffect::onIsEqual(const GrFragmentProcessor& sBase) const { |
joshualitt | 49586be | 2014-09-16 08:21:41 -0700 | [diff] [blame] | 359 | const GrMorphologyEffect& s = sBase.cast<GrMorphologyEffect>(); |
bsalomon | 420d7e9 | 2014-10-16 09:18:09 -0700 | [diff] [blame] | 360 | return (this->radius() == s.radius() && |
senorblanco@chromium.org | 84207c4 | 2012-08-22 20:51:19 +0000 | [diff] [blame] | 361 | this->direction() == s.direction() && |
cwallez | 80a61df | 2015-01-26 12:20:14 -0800 | [diff] [blame] | 362 | this->useRange() == s.useRange() && |
senorblanco@chromium.org | 84207c4 | 2012-08-22 20:51:19 +0000 | [diff] [blame] | 363 | this->type() == s.type()); |
| 364 | } |
| 365 | |
| 366 | /////////////////////////////////////////////////////////////////////////////// |
| 367 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 368 | GR_DEFINE_FRAGMENT_PROCESSOR_TEST(GrMorphologyEffect); |
senorblanco@chromium.org | 84207c4 | 2012-08-22 20:51:19 +0000 | [diff] [blame] | 369 | |
Hal Canary | 6f6961e | 2017-01-31 13:50:44 -0500 | [diff] [blame] | 370 | #if GR_TEST_UTILS |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 371 | sk_sp<GrFragmentProcessor> GrMorphologyEffect::TestCreate(GrProcessorTestData* d) { |
Robert Phillips | dbc8eeb | 2017-02-21 10:04:31 -0500 | [diff] [blame] | 372 | int texIdx = d->fRandom->nextBool() ? GrProcessorUnitTest::kSkiaPMTextureIdx |
| 373 | : GrProcessorUnitTest::kAlphaTextureIdx; |
| 374 | sk_sp<GrTextureProxy> proxy = d->textureProxy(texIdx); |
| 375 | |
joshualitt | 0067ff5 | 2015-07-08 14:26:19 -0700 | [diff] [blame] | 376 | Direction dir = d->fRandom->nextBool() ? kX_Direction : kY_Direction; |
senorblanco@chromium.org | 84207c4 | 2012-08-22 20:51:19 +0000 | [diff] [blame] | 377 | static const int kMaxRadius = 10; |
joshualitt | 0067ff5 | 2015-07-08 14:26:19 -0700 | [diff] [blame] | 378 | int radius = d->fRandom->nextRangeU(1, kMaxRadius); |
Robert Phillips | dbc8eeb | 2017-02-21 10:04:31 -0500 | [diff] [blame] | 379 | MorphologyType type = d->fRandom->nextBool() ? GrMorphologyEffect::kErode_MorphologyType |
| 380 | : GrMorphologyEffect::kDilate_MorphologyType; |
senorblanco@chromium.org | 84207c4 | 2012-08-22 20:51:19 +0000 | [diff] [blame] | 381 | |
Robert Phillips | fbcef6e | 2017-06-15 12:07:18 -0400 | [diff] [blame] | 382 | return GrMorphologyEffect::Make(std::move(proxy), dir, radius, type); |
senorblanco@chromium.org | 84207c4 | 2012-08-22 20:51:19 +0000 | [diff] [blame] | 383 | } |
Hal Canary | 6f6961e | 2017-01-31 13:50:44 -0500 | [diff] [blame] | 384 | #endif |
senorblanco@chromium.org | 84207c4 | 2012-08-22 20:51:19 +0000 | [diff] [blame] | 385 | |
senorblanco@chromium.org | 84207c4 | 2012-08-22 20:51:19 +0000 | [diff] [blame] | 386 | |
Robert Phillips | 296b1cc | 2017-03-15 10:42:12 -0400 | [diff] [blame] | 387 | static void apply_morphology_rect(GrRenderTargetContext* renderTargetContext, |
robertphillips | f299e71 | 2016-03-25 04:49:22 -0700 | [diff] [blame] | 388 | const GrClip& clip, |
Robert Phillips | 32f2818 | 2017-02-28 16:20:03 -0500 | [diff] [blame] | 389 | sk_sp<GrTextureProxy> proxy, |
robertphillips | f299e71 | 2016-03-25 04:49:22 -0700 | [diff] [blame] | 390 | const SkIRect& srcRect, |
| 391 | const SkIRect& dstRect, |
| 392 | int radius, |
| 393 | GrMorphologyEffect::MorphologyType morphType, |
Robert Phillips | 7f6cd90 | 2016-11-10 17:03:43 -0500 | [diff] [blame] | 394 | const float bounds[2], |
robertphillips | f299e71 | 2016-03-25 04:49:22 -0700 | [diff] [blame] | 395 | Gr1DKernelEffect::Direction direction) { |
cwallez | 80a61df | 2015-01-26 12:20:14 -0800 | [diff] [blame] | 396 | GrPaint paint; |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 397 | paint.setGammaCorrect(renderTargetContext->isGammaCorrect()); |
Robert Phillips | 32f2818 | 2017-02-28 16:20:03 -0500 | [diff] [blame] | 398 | |
Robert Phillips | fbcef6e | 2017-06-15 12:07:18 -0400 | [diff] [blame] | 399 | paint.addColorFragmentProcessor(GrMorphologyEffect::Make(std::move(proxy), |
Robert Phillips | 32f2818 | 2017-02-28 16:20:03 -0500 | [diff] [blame] | 400 | direction, radius, morphType, |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 401 | bounds)); |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 402 | paint.setPorterDuffXPFactory(SkBlendMode::kSrc); |
Brian Salomon | 82f4431 | 2017-01-11 13:42:54 -0500 | [diff] [blame] | 403 | renderTargetContext->fillRectToRect(clip, std::move(paint), GrAA::kNo, SkMatrix::I(), |
| 404 | SkRect::Make(dstRect), SkRect::Make(srcRect)); |
cwallez | 80a61df | 2015-01-26 12:20:14 -0800 | [diff] [blame] | 405 | } |
| 406 | |
Robert Phillips | 296b1cc | 2017-03-15 10:42:12 -0400 | [diff] [blame] | 407 | static void apply_morphology_rect_no_bounds(GrRenderTargetContext* renderTargetContext, |
robertphillips | f299e71 | 2016-03-25 04:49:22 -0700 | [diff] [blame] | 408 | const GrClip& clip, |
Robert Phillips | 32f2818 | 2017-02-28 16:20:03 -0500 | [diff] [blame] | 409 | sk_sp<GrTextureProxy> proxy, |
robertphillips | f299e71 | 2016-03-25 04:49:22 -0700 | [diff] [blame] | 410 | const SkIRect& srcRect, |
| 411 | const SkIRect& dstRect, |
| 412 | int radius, |
| 413 | GrMorphologyEffect::MorphologyType morphType, |
| 414 | Gr1DKernelEffect::Direction direction) { |
senorblanco@chromium.org | 84207c4 | 2012-08-22 20:51:19 +0000 | [diff] [blame] | 415 | GrPaint paint; |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 416 | paint.setGammaCorrect(renderTargetContext->isGammaCorrect()); |
Robert Phillips | 32f2818 | 2017-02-28 16:20:03 -0500 | [diff] [blame] | 417 | |
Robert Phillips | fbcef6e | 2017-06-15 12:07:18 -0400 | [diff] [blame] | 418 | paint.addColorFragmentProcessor(GrMorphologyEffect::Make(std::move(proxy), |
Robert Phillips | 32f2818 | 2017-02-28 16:20:03 -0500 | [diff] [blame] | 419 | direction, radius, morphType)); |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 420 | paint.setPorterDuffXPFactory(SkBlendMode::kSrc); |
Brian Salomon | 82f4431 | 2017-01-11 13:42:54 -0500 | [diff] [blame] | 421 | renderTargetContext->fillRectToRect(clip, std::move(paint), GrAA::kNo, SkMatrix::I(), |
Brian Salomon | 0e8fc8b | 2016-12-09 15:10:07 -0500 | [diff] [blame] | 422 | SkRect::Make(dstRect), SkRect::Make(srcRect)); |
senorblanco@chromium.org | 84207c4 | 2012-08-22 20:51:19 +0000 | [diff] [blame] | 423 | } |
| 424 | |
Robert Phillips | 296b1cc | 2017-03-15 10:42:12 -0400 | [diff] [blame] | 425 | static void apply_morphology_pass(GrRenderTargetContext* renderTargetContext, |
robertphillips | f299e71 | 2016-03-25 04:49:22 -0700 | [diff] [blame] | 426 | const GrClip& clip, |
Robert Phillips | 32f2818 | 2017-02-28 16:20:03 -0500 | [diff] [blame] | 427 | sk_sp<GrTextureProxy> textureProxy, |
robertphillips | f299e71 | 2016-03-25 04:49:22 -0700 | [diff] [blame] | 428 | const SkIRect& srcRect, |
| 429 | const SkIRect& dstRect, |
| 430 | int radius, |
| 431 | GrMorphologyEffect::MorphologyType morphType, |
| 432 | Gr1DKernelEffect::Direction direction) { |
cwallez | 80a61df | 2015-01-26 12:20:14 -0800 | [diff] [blame] | 433 | float bounds[2] = { 0.0f, 1.0f }; |
| 434 | SkIRect lowerSrcRect = srcRect, lowerDstRect = dstRect; |
| 435 | SkIRect middleSrcRect = srcRect, middleDstRect = dstRect; |
| 436 | SkIRect upperSrcRect = srcRect, upperDstRect = dstRect; |
| 437 | if (direction == Gr1DKernelEffect::kX_Direction) { |
Robert Phillips | 7f6cd90 | 2016-11-10 17:03:43 -0500 | [diff] [blame] | 438 | bounds[0] = SkIntToScalar(srcRect.left()) + 0.5f; |
| 439 | bounds[1] = SkIntToScalar(srcRect.right()) - 0.5f; |
cwallez | 80a61df | 2015-01-26 12:20:14 -0800 | [diff] [blame] | 440 | lowerSrcRect.fRight = srcRect.left() + radius; |
| 441 | lowerDstRect.fRight = dstRect.left() + radius; |
| 442 | upperSrcRect.fLeft = srcRect.right() - radius; |
| 443 | upperDstRect.fLeft = dstRect.right() - radius; |
| 444 | middleSrcRect.inset(radius, 0); |
| 445 | middleDstRect.inset(radius, 0); |
| 446 | } else { |
Robert Phillips | 7f6cd90 | 2016-11-10 17:03:43 -0500 | [diff] [blame] | 447 | bounds[0] = SkIntToScalar(srcRect.top()) + 0.5f; |
| 448 | bounds[1] = SkIntToScalar(srcRect.bottom()) - 0.5f; |
cwallez | 80a61df | 2015-01-26 12:20:14 -0800 | [diff] [blame] | 449 | lowerSrcRect.fBottom = srcRect.top() + radius; |
| 450 | lowerDstRect.fBottom = dstRect.top() + radius; |
| 451 | upperSrcRect.fTop = srcRect.bottom() - radius; |
| 452 | upperDstRect.fTop = dstRect.bottom() - radius; |
| 453 | middleSrcRect.inset(0, radius); |
| 454 | middleDstRect.inset(0, radius); |
| 455 | } |
| 456 | if (middleSrcRect.fLeft - middleSrcRect.fRight >= 0) { |
| 457 | // radius covers srcRect; use bounds over entire draw |
Robert Phillips | 296b1cc | 2017-03-15 10:42:12 -0400 | [diff] [blame] | 458 | apply_morphology_rect(renderTargetContext, clip, std::move(textureProxy), |
Robert Phillips | 7f6cd90 | 2016-11-10 17:03:43 -0500 | [diff] [blame] | 459 | srcRect, dstRect, radius, morphType, bounds, direction); |
cwallez | 80a61df | 2015-01-26 12:20:14 -0800 | [diff] [blame] | 460 | } else { |
| 461 | // Draw upper and lower margins with bounds; middle without. |
Robert Phillips | 296b1cc | 2017-03-15 10:42:12 -0400 | [diff] [blame] | 462 | apply_morphology_rect(renderTargetContext, clip, textureProxy, |
Robert Phillips | 7f6cd90 | 2016-11-10 17:03:43 -0500 | [diff] [blame] | 463 | lowerSrcRect, lowerDstRect, radius, morphType, bounds, direction); |
Robert Phillips | 296b1cc | 2017-03-15 10:42:12 -0400 | [diff] [blame] | 464 | apply_morphology_rect(renderTargetContext, clip, textureProxy, |
Robert Phillips | 7f6cd90 | 2016-11-10 17:03:43 -0500 | [diff] [blame] | 465 | upperSrcRect, upperDstRect, radius, morphType, bounds, direction); |
Robert Phillips | 296b1cc | 2017-03-15 10:42:12 -0400 | [diff] [blame] | 466 | apply_morphology_rect_no_bounds(renderTargetContext, clip, std::move(textureProxy), |
Robert Phillips | 7f6cd90 | 2016-11-10 17:03:43 -0500 | [diff] [blame] | 467 | middleSrcRect, middleDstRect, radius, morphType, direction); |
cwallez | 80a61df | 2015-01-26 12:20:14 -0800 | [diff] [blame] | 468 | } |
| 469 | } |
| 470 | |
brianosman | 2a75e5d | 2016-09-22 07:15:37 -0700 | [diff] [blame] | 471 | static sk_sp<SkSpecialImage> apply_morphology( |
| 472 | GrContext* context, |
| 473 | SkSpecialImage* input, |
| 474 | const SkIRect& rect, |
| 475 | GrMorphologyEffect::MorphologyType morphType, |
| 476 | SkISize radius, |
| 477 | const SkImageFilter::OutputProperties& outputProperties) { |
Robert Phillips | 8e1c4e6 | 2017-02-19 12:27:01 -0500 | [diff] [blame] | 478 | sk_sp<GrTextureProxy> srcTexture(input->asTextureProxyRef(context)); |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 479 | SkASSERT(srcTexture); |
brianosman | 2a75e5d | 2016-09-22 07:15:37 -0700 | [diff] [blame] | 480 | sk_sp<SkColorSpace> colorSpace = sk_ref_sp(outputProperties.colorSpace()); |
brianosman | 2695eaa | 2016-09-21 06:45:09 -0700 | [diff] [blame] | 481 | GrPixelConfig config = GrRenderableConfigForColorSpace(colorSpace.get()); |
bsalomon@google.com | 3cbaa2d | 2012-10-12 14:51:52 +0000 | [diff] [blame] | 482 | |
joshualitt | 570d2f8 | 2015-02-25 13:19:48 -0800 | [diff] [blame] | 483 | // setup new clip |
cdalton | 846c051 | 2016-05-13 10:25:00 -0700 | [diff] [blame] | 484 | const GrFixedClip clip(SkIRect::MakeWH(srcTexture->width(), srcTexture->height())); |
bsalomon@google.com | 3cbaa2d | 2012-10-12 14:51:52 +0000 | [diff] [blame] | 485 | |
robertphillips | d4c741e | 2016-04-28 09:55:15 -0700 | [diff] [blame] | 486 | const SkIRect dstRect = SkIRect::MakeWH(rect.width(), rect.height()); |
senorblanco@chromium.org | 8fcad98 | 2013-09-17 13:41:43 +0000 | [diff] [blame] | 487 | SkIRect srcRect = rect; |
bsalomon@google.com | 3cbaa2d | 2012-10-12 14:51:52 +0000 | [diff] [blame] | 488 | |
robertphillips | f299e71 | 2016-03-25 04:49:22 -0700 | [diff] [blame] | 489 | SkASSERT(radius.width() > 0 || radius.height() > 0); |
| 490 | |
senorblanco@chromium.org | 84207c4 | 2012-08-22 20:51:19 +0000 | [diff] [blame] | 491 | if (radius.fWidth > 0) { |
Robert Phillips | 7f6cd90 | 2016-11-10 17:03:43 -0500 | [diff] [blame] | 492 | sk_sp<GrRenderTargetContext> dstRTContext(context->makeDeferredRenderTargetContext( |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 493 | SkBackingFit::kApprox, rect.width(), rect.height(), config, colorSpace)); |
Robert Phillips | 7f6cd90 | 2016-11-10 17:03:43 -0500 | [diff] [blame] | 494 | if (!dstRTContext) { |
robertphillips | f299e71 | 2016-03-25 04:49:22 -0700 | [diff] [blame] | 495 | return nullptr; |
robertphillips | ff0ca5e | 2015-07-22 11:54:44 -0700 | [diff] [blame] | 496 | } |
| 497 | |
Robert Phillips | 296b1cc | 2017-03-15 10:42:12 -0400 | [diff] [blame] | 498 | apply_morphology_pass(dstRTContext.get(), clip, std::move(srcTexture), |
joshualitt | 570d2f8 | 2015-02-25 13:19:48 -0800 | [diff] [blame] | 499 | srcRect, dstRect, radius.fWidth, morphType, |
| 500 | Gr1DKernelEffect::kX_Direction); |
senorblanco@chromium.org | 8fcad98 | 2013-09-17 13:41:43 +0000 | [diff] [blame] | 501 | SkIRect clearRect = SkIRect::MakeXYWH(dstRect.fLeft, dstRect.fBottom, |
| 502 | dstRect.width(), radius.fHeight); |
robertphillips | d4c741e | 2016-04-28 09:55:15 -0700 | [diff] [blame] | 503 | GrColor clearColor = GrMorphologyEffect::kErode_MorphologyType == morphType |
| 504 | ? SK_ColorWHITE |
| 505 | : SK_ColorTRANSPARENT; |
Robert Phillips | 7f6cd90 | 2016-11-10 17:03:43 -0500 | [diff] [blame] | 506 | dstRTContext->clear(&clearRect, clearColor, false); |
robertphillips | ff0ca5e | 2015-07-22 11:54:44 -0700 | [diff] [blame] | 507 | |
Robert Phillips | f200a90 | 2017-01-30 13:27:37 -0500 | [diff] [blame] | 508 | srcTexture = dstRTContext->asTextureProxyRef(); |
senorblanco@chromium.org | 8fcad98 | 2013-09-17 13:41:43 +0000 | [diff] [blame] | 509 | srcRect = dstRect; |
senorblanco@chromium.org | 84207c4 | 2012-08-22 20:51:19 +0000 | [diff] [blame] | 510 | } |
| 511 | if (radius.fHeight > 0) { |
Robert Phillips | 7f6cd90 | 2016-11-10 17:03:43 -0500 | [diff] [blame] | 512 | sk_sp<GrRenderTargetContext> dstRTContext(context->makeDeferredRenderTargetContext( |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 513 | SkBackingFit::kApprox, rect.width(), rect.height(), config, colorSpace)); |
Robert Phillips | 7f6cd90 | 2016-11-10 17:03:43 -0500 | [diff] [blame] | 514 | if (!dstRTContext) { |
robertphillips | f299e71 | 2016-03-25 04:49:22 -0700 | [diff] [blame] | 515 | return nullptr; |
robertphillips | ff0ca5e | 2015-07-22 11:54:44 -0700 | [diff] [blame] | 516 | } |
| 517 | |
Robert Phillips | 296b1cc | 2017-03-15 10:42:12 -0400 | [diff] [blame] | 518 | apply_morphology_pass(dstRTContext.get(), clip, std::move(srcTexture), |
joshualitt | 570d2f8 | 2015-02-25 13:19:48 -0800 | [diff] [blame] | 519 | srcRect, dstRect, radius.fHeight, morphType, |
| 520 | Gr1DKernelEffect::kY_Direction); |
robertphillips | ff0ca5e | 2015-07-22 11:54:44 -0700 | [diff] [blame] | 521 | |
Robert Phillips | f200a90 | 2017-01-30 13:27:37 -0500 | [diff] [blame] | 522 | srcTexture = dstRTContext->asTextureProxyRef(); |
senorblanco@chromium.org | 84207c4 | 2012-08-22 20:51:19 +0000 | [diff] [blame] | 523 | } |
robertphillips | f299e71 | 2016-03-25 04:49:22 -0700 | [diff] [blame] | 524 | |
Robert Phillips | 7f6cd90 | 2016-11-10 17:03:43 -0500 | [diff] [blame] | 525 | return SkSpecialImage::MakeDeferredFromGpu(context, |
| 526 | SkIRect::MakeWH(rect.width(), rect.height()), |
| 527 | kNeedNewImageUniqueID_SpecialImage, |
| 528 | std::move(srcTexture), std::move(colorSpace), |
| 529 | &input->props()); |
senorblanco@chromium.org | 84207c4 | 2012-08-22 20:51:19 +0000 | [diff] [blame] | 530 | } |
robertphillips | f299e71 | 2016-03-25 04:49:22 -0700 | [diff] [blame] | 531 | #endif |
senorblanco@chromium.org | 84207c4 | 2012-08-22 20:51:19 +0000 | [diff] [blame] | 532 | |
robertphillips | 40d8d62 | 2016-03-30 08:09:56 -0700 | [diff] [blame] | 533 | sk_sp<SkSpecialImage> SkMorphologyImageFilter::onFilterImage(SkSpecialImage* source, |
| 534 | const Context& ctx, |
| 535 | SkIPoint* offset) const { |
robertphillips | f299e71 | 2016-03-25 04:49:22 -0700 | [diff] [blame] | 536 | SkIPoint inputOffset = SkIPoint::Make(0, 0); |
| 537 | sk_sp<SkSpecialImage> input(this->filterInput(0, source, ctx, &inputOffset)); |
| 538 | if (!input) { |
| 539 | return nullptr; |
senorblanco@chromium.org | c2594f4 | 2013-01-30 19:08:47 +0000 | [diff] [blame] | 540 | } |
robertphillips | f299e71 | 2016-03-25 04:49:22 -0700 | [diff] [blame] | 541 | |
senorblanco@chromium.org | c2594f4 | 2013-01-30 19:08:47 +0000 | [diff] [blame] | 542 | SkIRect bounds; |
robertphillips | f299e71 | 2016-03-25 04:49:22 -0700 | [diff] [blame] | 543 | input = this->applyCropRect(this->mapContext(ctx), input.get(), &inputOffset, &bounds); |
| 544 | if (!input) { |
| 545 | return nullptr; |
senorblanco@chromium.org | 8fcad98 | 2013-09-17 13:41:43 +0000 | [diff] [blame] | 546 | } |
robertphillips | f299e71 | 2016-03-25 04:49:22 -0700 | [diff] [blame] | 547 | |
senorblanco@chromium.org | 2bfe36b | 2014-01-20 19:58:28 +0000 | [diff] [blame] | 548 | SkVector radius = SkVector::Make(SkIntToScalar(this->radius().width()), |
| 549 | SkIntToScalar(this->radius().height())); |
senorblanco@chromium.org | 4cb543d | 2014-03-14 15:44:01 +0000 | [diff] [blame] | 550 | ctx.ctm().mapVectors(&radius, 1); |
senorblanco@chromium.org | 2bfe36b | 2014-01-20 19:58:28 +0000 | [diff] [blame] | 551 | int width = SkScalarFloorToInt(radius.fX); |
| 552 | int height = SkScalarFloorToInt(radius.fY); |
senorblanco@chromium.org | 8fcad98 | 2013-09-17 13:41:43 +0000 | [diff] [blame] | 553 | |
| 554 | if (width < 0 || height < 0) { |
robertphillips | f299e71 | 2016-03-25 04:49:22 -0700 | [diff] [blame] | 555 | return nullptr; |
senorblanco@chromium.org | 8fcad98 | 2013-09-17 13:41:43 +0000 | [diff] [blame] | 556 | } |
| 557 | |
senorblanco@chromium.org | 2bfe36b | 2014-01-20 19:58:28 +0000 | [diff] [blame] | 558 | SkIRect srcBounds = bounds; |
robertphillips | f299e71 | 2016-03-25 04:49:22 -0700 | [diff] [blame] | 559 | srcBounds.offset(-inputOffset); |
| 560 | |
| 561 | if (0 == width && 0 == height) { |
senorblanco@chromium.org | 6776b82 | 2014-01-03 21:48:22 +0000 | [diff] [blame] | 562 | offset->fX = bounds.left(); |
| 563 | offset->fY = bounds.top(); |
robertphillips | f299e71 | 2016-03-25 04:49:22 -0700 | [diff] [blame] | 564 | return input->makeSubset(srcBounds); |
senorblanco@chromium.org | 8fcad98 | 2013-09-17 13:41:43 +0000 | [diff] [blame] | 565 | } |
| 566 | |
robertphillips | f299e71 | 2016-03-25 04:49:22 -0700 | [diff] [blame] | 567 | #if SK_SUPPORT_GPU |
robertphillips | 6461251 | 2016-04-08 12:10:42 -0700 | [diff] [blame] | 568 | if (source->isTextureBacked()) { |
| 569 | GrContext* context = source->getContext(); |
| 570 | |
Brian Osman | 615d66d | 2016-12-29 09:18:20 -0500 | [diff] [blame] | 571 | // Ensure the input is in the destination color space. Typically applyCropRect will have |
| 572 | // called pad_image to account for our dilation of bounds, so the result will already be |
| 573 | // moved to the destination color space. If a filter DAG avoids that, then we use this |
| 574 | // fall-back, which saves us from having to do the xform during the filter itself. |
| 575 | input = ImageToColorSpace(input.get(), ctx.outputProperties()); |
Brian Osman | 320573f | 2016-12-21 13:36:02 -0500 | [diff] [blame] | 576 | |
robertphillips | 40d8d62 | 2016-03-30 08:09:56 -0700 | [diff] [blame] | 577 | auto type = (kDilate_Op == this->op()) ? GrMorphologyEffect::kDilate_MorphologyType |
| 578 | : GrMorphologyEffect::kErode_MorphologyType; |
robertphillips | 6461251 | 2016-04-08 12:10:42 -0700 | [diff] [blame] | 579 | sk_sp<SkSpecialImage> result(apply_morphology(context, input.get(), srcBounds, type, |
brianosman | 2a75e5d | 2016-09-22 07:15:37 -0700 | [diff] [blame] | 580 | SkISize::Make(width, height), |
| 581 | ctx.outputProperties())); |
robertphillips | f299e71 | 2016-03-25 04:49:22 -0700 | [diff] [blame] | 582 | if (result) { |
| 583 | offset->fX = bounds.left(); |
| 584 | offset->fY = bounds.top(); |
| 585 | } |
| 586 | return result; |
| 587 | } |
| 588 | #endif |
| 589 | |
robertphillips | 6461251 | 2016-04-08 12:10:42 -0700 | [diff] [blame] | 590 | SkBitmap inputBM; |
robertphillips | f299e71 | 2016-03-25 04:49:22 -0700 | [diff] [blame] | 591 | |
robertphillips | 6461251 | 2016-04-08 12:10:42 -0700 | [diff] [blame] | 592 | if (!input->getROPixels(&inputBM)) { |
robertphillips | f299e71 | 2016-03-25 04:49:22 -0700 | [diff] [blame] | 593 | return nullptr; |
| 594 | } |
| 595 | |
robertphillips | 6461251 | 2016-04-08 12:10:42 -0700 | [diff] [blame] | 596 | if (inputBM.colorType() != kN32_SkColorType) { |
robertphillips | f299e71 | 2016-03-25 04:49:22 -0700 | [diff] [blame] | 597 | return nullptr; |
| 598 | } |
| 599 | |
| 600 | SkImageInfo info = SkImageInfo::Make(bounds.width(), bounds.height(), |
robertphillips | 6461251 | 2016-04-08 12:10:42 -0700 | [diff] [blame] | 601 | inputBM.colorType(), inputBM.alphaType()); |
robertphillips | f299e71 | 2016-03-25 04:49:22 -0700 | [diff] [blame] | 602 | |
| 603 | SkBitmap dst; |
| 604 | if (!dst.tryAllocPixels(info)) { |
| 605 | return nullptr; |
| 606 | } |
| 607 | |
robertphillips | f299e71 | 2016-03-25 04:49:22 -0700 | [diff] [blame] | 608 | SkMorphologyImageFilter::Proc procX, procY; |
| 609 | |
robertphillips | 40d8d62 | 2016-03-30 08:09:56 -0700 | [diff] [blame] | 610 | if (kDilate_Op == this->op()) { |
robertphillips | f299e71 | 2016-03-25 04:49:22 -0700 | [diff] [blame] | 611 | procX = SkOpts::dilate_x; |
| 612 | procY = SkOpts::dilate_y; |
| 613 | } else { |
| 614 | procX = SkOpts::erode_x; |
| 615 | procY = SkOpts::erode_y; |
| 616 | } |
| 617 | |
| 618 | if (width > 0 && height > 0) { |
| 619 | SkBitmap tmp; |
| 620 | if (!tmp.tryAllocPixels(info)) { |
| 621 | return nullptr; |
| 622 | } |
| 623 | |
robertphillips | 6461251 | 2016-04-08 12:10:42 -0700 | [diff] [blame] | 624 | call_proc_X(procX, inputBM, &tmp, width, srcBounds); |
robertphillips | f299e71 | 2016-03-25 04:49:22 -0700 | [diff] [blame] | 625 | SkIRect tmpBounds = SkIRect::MakeWH(srcBounds.width(), srcBounds.height()); |
| 626 | call_proc_Y(procY, |
| 627 | tmp.getAddr32(tmpBounds.left(), tmpBounds.top()), tmp.rowBytesAsPixels(), |
| 628 | &dst, height, tmpBounds); |
| 629 | } else if (width > 0) { |
robertphillips | 6461251 | 2016-04-08 12:10:42 -0700 | [diff] [blame] | 630 | call_proc_X(procX, inputBM, &dst, width, srcBounds); |
robertphillips | f299e71 | 2016-03-25 04:49:22 -0700 | [diff] [blame] | 631 | } else if (height > 0) { |
| 632 | call_proc_Y(procY, |
robertphillips | 6461251 | 2016-04-08 12:10:42 -0700 | [diff] [blame] | 633 | inputBM.getAddr32(srcBounds.left(), srcBounds.top()), |
| 634 | inputBM.rowBytesAsPixels(), |
robertphillips | f299e71 | 2016-03-25 04:49:22 -0700 | [diff] [blame] | 635 | &dst, height, srcBounds); |
senorblanco@chromium.org | 8fcad98 | 2013-09-17 13:41:43 +0000 | [diff] [blame] | 636 | } |
senorblanco@chromium.org | 6776b82 | 2014-01-03 21:48:22 +0000 | [diff] [blame] | 637 | offset->fX = bounds.left(); |
| 638 | offset->fY = bounds.top(); |
robertphillips | f299e71 | 2016-03-25 04:49:22 -0700 | [diff] [blame] | 639 | |
robertphillips | 3e30227 | 2016-04-20 11:48:36 -0700 | [diff] [blame] | 640 | return SkSpecialImage::MakeFromRaster(SkIRect::MakeWH(bounds.width(), bounds.height()), |
brianosman | 898235c | 2016-04-06 07:38:23 -0700 | [diff] [blame] | 641 | dst, &source->props()); |
senorblanco@chromium.org | 05054f1 | 2012-03-02 21:05:45 +0000 | [diff] [blame] | 642 | } |
Matt Sarett | 6d72ed9 | 2017-04-10 16:35:33 -0400 | [diff] [blame] | 643 | |
Mike Reed | 6d9f429 | 2017-07-06 12:32:55 -0400 | [diff] [blame^] | 644 | sk_sp<SkImageFilter> SkMorphologyImageFilter::onMakeColorSpace(SkColorSpaceXformer* xformer) const{ |
Matt Sarett | 6d72ed9 | 2017-04-10 16:35:33 -0400 | [diff] [blame] | 645 | SkASSERT(1 == this->countInputs()); |
Mike Reed | 6d9f429 | 2017-07-06 12:32:55 -0400 | [diff] [blame^] | 646 | auto input = xformer->apply(this->getInput(0)); |
| 647 | if (input.get() != this->getInput(0)) { |
| 648 | return (SkMorphologyImageFilter::kDilate_Op == this->op()) |
| 649 | ? SkDilateImageFilter::Make(fRadius.width(), fRadius.height(), std::move(input), |
| 650 | this->getCropRectIfSet()) |
| 651 | : SkErodeImageFilter::Make(fRadius.width(), fRadius.height(), std::move(input), |
| 652 | this->getCropRectIfSet()); |
Matt Sarett | 6d72ed9 | 2017-04-10 16:35:33 -0400 | [diff] [blame] | 653 | } |
Mike Reed | 6d9f429 | 2017-07-06 12:32:55 -0400 | [diff] [blame^] | 654 | return this->refMe(); |
Matt Sarett | 6d72ed9 | 2017-04-10 16:35:33 -0400 | [diff] [blame] | 655 | } |