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