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