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