blob: bf8811b88702a7caf69e8cacde999b200a1194ac [file] [log] [blame]
senorblanco@chromium.org05054f12012-03-02 21:05:45 +00001/*
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"
robertphillipsf299e712016-03-25 04:49:22 -07009
djsollen@google.com64a0ec32012-06-12 15:17:27 +000010#include "SkBitmap.h"
senorblanco@chromium.org05054f12012-03-02 21:05:45 +000011#include "SkColorPriv.h"
mtkleind029ded2015-08-04 14:09:09 -070012#include "SkOpts.h"
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +000013#include "SkReadBuffer.h"
djsollen@google.comc73dd5c2012-08-07 15:54:32 +000014#include "SkRect.h"
robertphillipsf299e712016-03-25 04:49:22 -070015#include "SkSpecialImage.h"
mtkleind029ded2015-08-04 14:09:09 -070016#include "SkWriteBuffer.h"
robertphillipsf299e712016-03-25 04:49:22 -070017
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +000018#if SK_SUPPORT_GPU
senorblanco@chromium.org302cffb2012-08-01 20:16:34 +000019#include "GrContext.h"
robertphillipsea461502015-05-26 11:38:03 -070020#include "GrDrawContext.h"
egdaniel605dd0f2014-11-12 08:35:25 -080021#include "GrInvariantOutput.h"
senorblanco@chromium.org302cffb2012-08-01 20:16:34 +000022#include "GrTexture.h"
robertphillips1de87df2016-01-14 06:03:29 -080023#include "SkGr.h"
joshualitteb2a6762014-12-04 11:35:33 -080024#include "effects/Gr1DKernelEffect.h"
egdaniel64c47282015-11-13 06:54:19 -080025#include "glsl/GrGLSLFragmentProcessor.h"
egdaniel2d721d32015-11-11 13:06:05 -080026#include "glsl/GrGLSLFragmentShaderBuilder.h"
egdaniel018fb622015-10-28 07:26:40 -070027#include "glsl/GrGLSLProgramDataManager.h"
egdaniel7ea439b2015-12-03 09:20:44 -080028#include "glsl/GrGLSLUniformHandler.h"
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +000029#endif
senorblanco@chromium.org05054f12012-03-02 21:05:45 +000030
senorblanco@chromium.org2bfe36b2014-01-20 19:58:28 +000031SkMorphologyImageFilter::SkMorphologyImageFilter(int radiusX,
32 int radiusY,
33 SkImageFilter* input,
senorblanco24e06d52015-03-18 12:11:33 -070034 const CropRect* cropRect)
robertphillipsf299e712016-03-25 04:49:22 -070035 : INHERITED(1, &input, cropRect)
36 , fRadius(SkISize::Make(radiusX, radiusY)) {
senorblanco@chromium.org05054f12012-03-02 21:05:45 +000037}
38
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +000039void SkMorphologyImageFilter::flatten(SkWriteBuffer& buffer) const {
senorblanco@chromium.org05054f12012-03-02 21:05:45 +000040 this->INHERITED::flatten(buffer);
tomhudson@google.com75589252012-04-10 17:42:21 +000041 buffer.writeInt(fRadius.fWidth);
42 buffer.writeInt(fRadius.fHeight);
senorblanco@chromium.org05054f12012-03-02 21:05:45 +000043}
44
robertphillipsf299e712016-03-25 04:49:22 -070045static 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.org0ded88d2014-01-24 15:43:50 +000049 radiusX, bounds.width(), bounds.height(),
50 src.rowBytesAsPixels(), dst->rowBytesAsPixels());
senorblanco@chromium.org05054f12012-03-02 21:05:45 +000051}
52
robertphillipsf299e712016-03-25 04:49:22 -070053static 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.org0ded88d2014-01-24 15:43:50 +000057 radiusY, bounds.height(), bounds.width(),
robertphillipsf299e712016-03-25 04:49:22 -070058 srcRowBytesAsPixels, dst->rowBytesAsPixels());
senorblanco@chromium.org05054f12012-03-02 21:05:45 +000059}
60
senorblancoe5e79842016-03-21 14:51:59 -070061SkRect 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.org336d1d72014-01-27 21:03:17 +000065}
66
senorblancoe5e79842016-03-21 14:51:59 -070067SkIRect SkMorphologyImageFilter::onFilterNodeBounds(const SkIRect& src, const SkMatrix& ctm,
68 MapDirection) const {
senorblanco@chromium.orgc4b12f12014-02-05 17:51:22 +000069 SkVector radius = SkVector::Make(SkIntToScalar(this->radius().width()),
70 SkIntToScalar(this->radius().height()));
71 ctm.mapVectors(&radius, 1);
senorblancoe5e79842016-03-21 14:51:59 -070072 return src.makeOutset(SkScalarCeilToInt(radius.x()), SkScalarCeilToInt(radius.y()));
senorblanco@chromium.orgc4b12f12014-02-05 17:51:22 +000073}
74
reed60c9b582016-04-03 09:11:13 -070075sk_sp<SkFlattenable> SkErodeImageFilter::CreateProc(SkReadBuffer& buffer) {
reed9fa60da2014-08-21 07:59:51 -070076 SK_IMAGEFILTER_UNFLATTEN_COMMON(common, 1);
77 const int width = buffer.readInt();
78 const int height = buffer.readInt();
reed60c9b582016-04-03 09:11:13 -070079 return sk_sp<SkFlattenable>(Create(width, height, common.getInput(0).get(),
80 &common.cropRect()));
reed9fa60da2014-08-21 07:59:51 -070081}
82
reed60c9b582016-04-03 09:11:13 -070083sk_sp<SkFlattenable> SkDilateImageFilter::CreateProc(SkReadBuffer& buffer) {
reed9fa60da2014-08-21 07:59:51 -070084 SK_IMAGEFILTER_UNFLATTEN_COMMON(common, 1);
85 const int width = buffer.readInt();
86 const int height = buffer.readInt();
reed60c9b582016-04-03 09:11:13 -070087 return sk_sp<SkFlattenable>(Create(width, height, common.getInput(0).get(),
88 &common.cropRect()));
reed9fa60da2014-08-21 07:59:51 -070089}
90
robertphillipsf3f5bad2014-12-19 13:49:15 -080091#ifndef SK_IGNORE_TO_STRING
92void SkErodeImageFilter::toString(SkString* str) const {
93 str->appendf("SkErodeImageFilter: (");
94 str->appendf("radius: (%d,%d)", this->radius().fWidth, this->radius().fHeight);
95 str->append(")");
96}
97#endif
98
99#ifndef SK_IGNORE_TO_STRING
100void SkDilateImageFilter::toString(SkString* str) const {
101 str->appendf("SkDilateImageFilter: (");
102 str->appendf("radius: (%d,%d)", this->radius().fWidth, this->radius().fHeight);
103 str->append(")");
104}
105#endif
106
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000107#if SK_SUPPORT_GPU
senorblanco@chromium.org84207c42012-08-22 20:51:19 +0000108
109///////////////////////////////////////////////////////////////////////////////
senorblanco@chromium.org84207c42012-08-22 20:51:19 +0000110/**
111 * Morphology effects. Depending upon the type of morphology, either the
112 * component-wise min (Erode_Type) or max (Dilate_Type) of all pixels in the
113 * kernel is selected as the new color. The new color is modulated by the input
114 * color.
115 */
116class GrMorphologyEffect : public Gr1DKernelEffect {
117
118public:
119
120 enum MorphologyType {
121 kErode_MorphologyType,
122 kDilate_MorphologyType,
123 };
124
bsalomon4a339522015-10-06 08:40:50 -0700125 static GrFragmentProcessor* Create(GrTexture* tex, Direction dir, int radius,
126 MorphologyType type) {
127 return new GrMorphologyEffect(tex, dir, radius, type);
bsalomon@google.com0ac6af42013-01-16 15:16:18 +0000128 }
129
bsalomon4a339522015-10-06 08:40:50 -0700130 static GrFragmentProcessor* Create(GrTexture* tex, Direction dir, int radius,
131 MorphologyType type, float bounds[2]) {
132 return new GrMorphologyEffect(tex, dir, radius, type, bounds);
cwallez80a61df2015-01-26 12:20:14 -0800133 }
134
senorblanco@chromium.org84207c42012-08-22 20:51:19 +0000135 virtual ~GrMorphologyEffect();
136
137 MorphologyType type() const { return fType; }
cwallez80a61df2015-01-26 12:20:14 -0800138 bool useRange() const { return fUseRange; }
139 const float* range() const { return fRange; }
senorblanco@chromium.org84207c42012-08-22 20:51:19 +0000140
mtklein36352bf2015-03-25 18:17:31 -0700141 const char* name() const override { return "Morphology"; }
senorblanco@chromium.org84207c42012-08-22 20:51:19 +0000142
senorblanco@chromium.org84207c42012-08-22 20:51:19 +0000143protected:
144
145 MorphologyType fType;
cwallez80a61df2015-01-26 12:20:14 -0800146 bool fUseRange;
147 float fRange[2];
senorblanco@chromium.org84207c42012-08-22 20:51:19 +0000148
149private:
egdaniel57d3b032015-11-13 11:57:27 -0800150 GrGLSLFragmentProcessor* onCreateGLSLInstance() const override;
wangyixb1daa862015-08-18 11:29:31 -0700151
egdaniel57d3b032015-11-13 11:57:27 -0800152 void onGetGLSLProcessorKey(const GrGLSLCaps&, GrProcessorKeyBuilder*) const override;
wangyix4b3050b2015-08-04 07:59:37 -0700153
mtklein36352bf2015-03-25 18:17:31 -0700154 bool onIsEqual(const GrFragmentProcessor&) const override;
bsalomon@google.com68b58c92013-01-17 16:50:08 +0000155
mtklein36352bf2015-03-25 18:17:31 -0700156 void onComputeInvariantOutput(GrInvariantOutput* inout) const override;
egdaniel1a8ecdf2014-10-03 06:24:12 -0700157
bsalomon4a339522015-10-06 08:40:50 -0700158 GrMorphologyEffect(GrTexture*, Direction, int radius, MorphologyType);
159 GrMorphologyEffect(GrTexture*, Direction, int radius, MorphologyType,
joshualitt5f10b5c2015-07-09 10:24:35 -0700160 float bounds[2]);
bsalomon@google.com0ac6af42013-01-16 15:16:18 +0000161
joshualittb0a8a372014-09-23 09:50:21 -0700162 GR_DECLARE_FRAGMENT_PROCESSOR_TEST;
senorblanco@chromium.org84207c42012-08-22 20:51:19 +0000163
164 typedef Gr1DKernelEffect INHERITED;
165};
166
167///////////////////////////////////////////////////////////////////////////////
168
egdaniel64c47282015-11-13 06:54:19 -0800169class GrGLMorphologyEffect : public GrGLSLFragmentProcessor {
senorblanco@chromium.org84207c42012-08-22 20:51:19 +0000170public:
robertphillips9cdb9922016-02-03 12:25:40 -0800171 void emitCode(EmitArgs&) override;
senorblanco@chromium.org84207c42012-08-22 20:51:19 +0000172
robertphillipsbf536af2016-02-04 06:11:53 -0800173 static inline void GenKey(const GrProcessor&, const GrGLSLCaps&, GrProcessorKeyBuilder*);
senorblanco@chromium.org84207c42012-08-22 20:51:19 +0000174
wangyixb1daa862015-08-18 11:29:31 -0700175protected:
egdaniel018fb622015-10-28 07:26:40 -0700176 void onSetData(const GrGLSLProgramDataManager&, const GrProcessor&) override;
senorblanco@chromium.org84207c42012-08-22 20:51:19 +0000177
178private:
egdaniel018fb622015-10-28 07:26:40 -0700179 GrGLSLProgramDataManager::UniformHandle fPixelSizeUni;
180 GrGLSLProgramDataManager::UniformHandle fRangeUni;
senorblanco@chromium.org84207c42012-08-22 20:51:19 +0000181
egdaniel64c47282015-11-13 06:54:19 -0800182 typedef GrGLSLFragmentProcessor INHERITED;
senorblanco@chromium.org84207c42012-08-22 20:51:19 +0000183};
184
wangyix7c157a92015-07-22 15:08:53 -0700185void GrGLMorphologyEffect::emitCode(EmitArgs& args) {
robertphillipsbf536af2016-02-04 06:11:53 -0800186 const GrMorphologyEffect& me = args.fFp.cast<GrMorphologyEffect>();
187
egdaniel7ea439b2015-12-03 09:20:44 -0800188 GrGLSLUniformHandler* uniformHandler = args.fUniformHandler;
cdalton5e58cee2016-02-11 12:49:47 -0800189 fPixelSizeUni = uniformHandler->addUniform(kFragment_GrShaderFlag,
egdaniel7ea439b2015-12-03 09:20:44 -0800190 kFloat_GrSLType, kDefault_GrSLPrecision,
191 "PixelSize");
192 const char* pixelSizeInc = uniformHandler->getUniformCStr(fPixelSizeUni);
cdalton5e58cee2016-02-11 12:49:47 -0800193 fRangeUni = uniformHandler->addUniform(kFragment_GrShaderFlag,
egdaniel7ea439b2015-12-03 09:20:44 -0800194 kVec2f_GrSLType, kDefault_GrSLPrecision,
195 "Range");
196 const char* range = uniformHandler->getUniformCStr(fRangeUni);
senorblanco@chromium.org84207c42012-08-22 20:51:19 +0000197
cdalton85285412016-02-18 12:37:07 -0800198 GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder;
egdaniel4ca2e602015-11-18 08:01:26 -0800199 SkString coords2D = fragBuilder->ensureFSCoords2D(args.fCoords, 0);
senorblanco@chromium.org84207c42012-08-22 20:51:19 +0000200 const char* func;
robertphillipsbf536af2016-02-04 06:11:53 -0800201 switch (me.type()) {
senorblanco@chromium.org84207c42012-08-22 20:51:19 +0000202 case GrMorphologyEffect::kErode_MorphologyType:
egdaniel4ca2e602015-11-18 08:01:26 -0800203 fragBuilder->codeAppendf("\t\t%s = vec4(1, 1, 1, 1);\n", args.fOutputColor);
senorblanco@chromium.org84207c42012-08-22 20:51:19 +0000204 func = "min";
205 break;
206 case GrMorphologyEffect::kDilate_MorphologyType:
egdaniel4ca2e602015-11-18 08:01:26 -0800207 fragBuilder->codeAppendf("\t\t%s = vec4(0, 0, 0, 0);\n", args.fOutputColor);
senorblanco@chromium.org84207c42012-08-22 20:51:19 +0000208 func = "max";
209 break;
210 default:
commit-bot@chromium.org88cb22b2014-04-30 14:17:00 +0000211 SkFAIL("Unexpected type");
senorblanco@chromium.org84207c42012-08-22 20:51:19 +0000212 func = ""; // suppress warning
213 break;
214 }
senorblanco@chromium.org84207c42012-08-22 20:51:19 +0000215
cwallez80a61df2015-01-26 12:20:14 -0800216 const char* dir;
robertphillipsbf536af2016-02-04 06:11:53 -0800217 switch (me.direction()) {
cwallez80a61df2015-01-26 12:20:14 -0800218 case Gr1DKernelEffect::kX_Direction:
219 dir = "x";
220 break;
221 case Gr1DKernelEffect::kY_Direction:
222 dir = "y";
223 break;
224 default:
225 SkFAIL("Unknown filter direction.");
226 dir = ""; // suppress warning
227 }
228
robertphillipsbf536af2016-02-04 06:11:53 -0800229 int width = GrMorphologyEffect::WidthFromRadius(me.radius());
230
cwallez80a61df2015-01-26 12:20:14 -0800231 // vec2 coord = coord2D;
egdaniel4ca2e602015-11-18 08:01:26 -0800232 fragBuilder->codeAppendf("\t\tvec2 coord = %s;\n", coords2D.c_str());
cwallez80a61df2015-01-26 12:20:14 -0800233 // coord.x -= radius * pixelSize;
robertphillipsbf536af2016-02-04 06:11:53 -0800234 fragBuilder->codeAppendf("\t\tcoord.%s -= %d.0 * %s; \n", dir, me.radius(), pixelSizeInc);
235 if (me.useRange()) {
cwallez80a61df2015-01-26 12:20:14 -0800236 // highBound = min(highBound, coord.x + (width-1) * pixelSize);
egdaniel4ca2e602015-11-18 08:01:26 -0800237 fragBuilder->codeAppendf("\t\tfloat highBound = min(%s.y, coord.%s + %f * %s);",
robertphillipsbf536af2016-02-04 06:11:53 -0800238 range, dir, float(width - 1), pixelSizeInc);
cwallez80a61df2015-01-26 12:20:14 -0800239 // coord.x = max(lowBound, coord.x);
egdaniel4ca2e602015-11-18 08:01:26 -0800240 fragBuilder->codeAppendf("\t\tcoord.%s = max(%s.x, coord.%s);", dir, range, dir);
cwallez80a61df2015-01-26 12:20:14 -0800241 }
robertphillipsbf536af2016-02-04 06:11:53 -0800242 fragBuilder->codeAppendf("\t\tfor (int i = 0; i < %d; i++) {\n", width);
egdaniel4ca2e602015-11-18 08:01:26 -0800243 fragBuilder->codeAppendf("\t\t\t%s = %s(%s, ", args.fOutputColor, func, args.fOutputColor);
244 fragBuilder->appendTextureLookup(args.fSamplers[0], "coord");
245 fragBuilder->codeAppend(");\n");
cwallez80a61df2015-01-26 12:20:14 -0800246 // coord.x += pixelSize;
egdaniel4ca2e602015-11-18 08:01:26 -0800247 fragBuilder->codeAppendf("\t\t\tcoord.%s += %s;\n", dir, pixelSizeInc);
robertphillipsbf536af2016-02-04 06:11:53 -0800248 if (me.useRange()) {
cwallez80a61df2015-01-26 12:20:14 -0800249 // coord.x = min(highBound, coord.x);
egdaniel4ca2e602015-11-18 08:01:26 -0800250 fragBuilder->codeAppendf("\t\t\tcoord.%s = min(highBound, coord.%s);", dir, dir);
cwallez80a61df2015-01-26 12:20:14 -0800251 }
egdaniel4ca2e602015-11-18 08:01:26 -0800252 fragBuilder->codeAppend("\t\t}\n");
bsalomon@google.comf910d3b2013-03-07 17:06:57 +0000253 SkString modulate;
wangyix7c157a92015-07-22 15:08:53 -0700254 GrGLSLMulVarBy4f(&modulate, args.fOutputColor, args.fInputColor);
egdaniel4ca2e602015-11-18 08:01:26 -0800255 fragBuilder->codeAppend(modulate.c_str());
senorblanco@chromium.org84207c42012-08-22 20:51:19 +0000256}
257
joshualittb0a8a372014-09-23 09:50:21 -0700258void GrGLMorphologyEffect::GenKey(const GrProcessor& proc,
jvanverthcfc18862015-04-28 08:48:20 -0700259 const GrGLSLCaps&, GrProcessorKeyBuilder* b) {
joshualittb0a8a372014-09-23 09:50:21 -0700260 const GrMorphologyEffect& m = proc.cast<GrMorphologyEffect>();
bsalomon63e99f72014-07-21 08:03:14 -0700261 uint32_t key = static_cast<uint32_t>(m.radius());
senorblanco@chromium.org84207c42012-08-22 20:51:19 +0000262 key |= (m.type() << 8);
cwallez80a61df2015-01-26 12:20:14 -0800263 key |= (m.direction() << 9);
robertphillipsbf536af2016-02-04 06:11:53 -0800264 if (m.useRange()) {
265 key |= 1 << 10;
266 }
bsalomon63e99f72014-07-21 08:03:14 -0700267 b->add32(key);
senorblanco@chromium.org84207c42012-08-22 20:51:19 +0000268}
269
egdaniel018fb622015-10-28 07:26:40 -0700270void GrGLMorphologyEffect::onSetData(const GrGLSLProgramDataManager& pdman,
robertphillipsbf536af2016-02-04 06:11:53 -0800271 const GrProcessor& proc) {
cwallez80a61df2015-01-26 12:20:14 -0800272 const GrMorphologyEffect& m = proc.cast<GrMorphologyEffect>();
273 GrTexture& texture = *m.texture(0);
cwallez80a61df2015-01-26 12:20:14 -0800274
275 float pixelSize = 0.0f;
robertphillipsbf536af2016-02-04 06:11:53 -0800276 switch (m.direction()) {
senorblanco@chromium.org84207c42012-08-22 20:51:19 +0000277 case Gr1DKernelEffect::kX_Direction:
cwallez80a61df2015-01-26 12:20:14 -0800278 pixelSize = 1.0f / texture.width();
senorblanco@chromium.org84207c42012-08-22 20:51:19 +0000279 break;
280 case Gr1DKernelEffect::kY_Direction:
cwallez80a61df2015-01-26 12:20:14 -0800281 pixelSize = 1.0f / texture.height();
senorblanco@chromium.org84207c42012-08-22 20:51:19 +0000282 break;
283 default:
commit-bot@chromium.org88cb22b2014-04-30 14:17:00 +0000284 SkFAIL("Unknown filter direction.");
senorblanco@chromium.org84207c42012-08-22 20:51:19 +0000285 }
cwallez80a61df2015-01-26 12:20:14 -0800286 pdman.set1f(fPixelSizeUni, pixelSize);
287
robertphillipsbf536af2016-02-04 06:11:53 -0800288 if (m.useRange()) {
cwallez80a61df2015-01-26 12:20:14 -0800289 const float* range = m.range();
robertphillipsbf536af2016-02-04 06:11:53 -0800290 if (m.direction() && texture.origin() == kBottomLeft_GrSurfaceOrigin) {
cwallez80a61df2015-01-26 12:20:14 -0800291 pdman.set2f(fRangeUni, 1.0f - range[1], 1.0f - range[0]);
292 } else {
293 pdman.set2f(fRangeUni, range[0], range[1]);
294 }
295 }
senorblanco@chromium.org84207c42012-08-22 20:51:19 +0000296}
297
298///////////////////////////////////////////////////////////////////////////////
299
bsalomon4a339522015-10-06 08:40:50 -0700300GrMorphologyEffect::GrMorphologyEffect(GrTexture* texture,
senorblanco@chromium.org84207c42012-08-22 20:51:19 +0000301 Direction direction,
302 int radius,
303 MorphologyType type)
bsalomon4a339522015-10-06 08:40:50 -0700304 : INHERITED(texture, direction, radius)
robertphillipsf299e712016-03-25 04:49:22 -0700305 , fType(type)
306 , fUseRange(false) {
joshualitteb2a6762014-12-04 11:35:33 -0800307 this->initClassID<GrMorphologyEffect>();
senorblanco@chromium.org84207c42012-08-22 20:51:19 +0000308}
309
bsalomon4a339522015-10-06 08:40:50 -0700310GrMorphologyEffect::GrMorphologyEffect(GrTexture* texture,
cwallez80a61df2015-01-26 12:20:14 -0800311 Direction direction,
312 int radius,
313 MorphologyType type,
314 float range[2])
bsalomon4a339522015-10-06 08:40:50 -0700315 : INHERITED(texture, direction, radius)
robertphillipsf299e712016-03-25 04:49:22 -0700316 , fType(type)
317 , fUseRange(true) {
cwallez80a61df2015-01-26 12:20:14 -0800318 this->initClassID<GrMorphologyEffect>();
319 fRange[0] = range[0];
320 fRange[1] = range[1];
321}
322
senorblanco@chromium.org84207c42012-08-22 20:51:19 +0000323GrMorphologyEffect::~GrMorphologyEffect() {
324}
325
egdaniel57d3b032015-11-13 11:57:27 -0800326void GrMorphologyEffect::onGetGLSLProcessorKey(const GrGLSLCaps& caps,
327 GrProcessorKeyBuilder* b) const {
joshualitteb2a6762014-12-04 11:35:33 -0800328 GrGLMorphologyEffect::GenKey(*this, caps, b);
senorblanco@chromium.org84207c42012-08-22 20:51:19 +0000329}
330
egdaniel57d3b032015-11-13 11:57:27 -0800331GrGLSLFragmentProcessor* GrMorphologyEffect::onCreateGLSLInstance() const {
robertphillipsbf536af2016-02-04 06:11:53 -0800332 return new GrGLMorphologyEffect;
joshualitteb2a6762014-12-04 11:35:33 -0800333}
bsalomon0e08fc12014-10-15 08:19:04 -0700334bool GrMorphologyEffect::onIsEqual(const GrFragmentProcessor& sBase) const {
joshualitt49586be2014-09-16 08:21:41 -0700335 const GrMorphologyEffect& s = sBase.cast<GrMorphologyEffect>();
bsalomon420d7e92014-10-16 09:18:09 -0700336 return (this->radius() == s.radius() &&
senorblanco@chromium.org84207c42012-08-22 20:51:19 +0000337 this->direction() == s.direction() &&
cwallez80a61df2015-01-26 12:20:14 -0800338 this->useRange() == s.useRange() &&
senorblanco@chromium.org84207c42012-08-22 20:51:19 +0000339 this->type() == s.type());
340}
341
egdaniel605dd0f2014-11-12 08:35:25 -0800342void GrMorphologyEffect::onComputeInvariantOutput(GrInvariantOutput* inout) const {
bsalomon@google.com68b58c92013-01-17 16:50:08 +0000343 // This is valid because the color components of the result of the kernel all come
344 // exactly from existing values in the source texture.
egdaniel1a8ecdf2014-10-03 06:24:12 -0700345 this->updateInvariantOutputForModulation(inout);
bsalomon@google.com68b58c92013-01-17 16:50:08 +0000346}
347
senorblanco@chromium.org84207c42012-08-22 20:51:19 +0000348///////////////////////////////////////////////////////////////////////////////
349
joshualittb0a8a372014-09-23 09:50:21 -0700350GR_DEFINE_FRAGMENT_PROCESSOR_TEST(GrMorphologyEffect);
senorblanco@chromium.org84207c42012-08-22 20:51:19 +0000351
bsalomonc21b09e2015-08-28 18:46:56 -0700352const GrFragmentProcessor* GrMorphologyEffect::TestCreate(GrProcessorTestData* d) {
joshualitt0067ff52015-07-08 14:26:19 -0700353 int texIdx = d->fRandom->nextBool() ? GrProcessorUnitTest::kSkiaPMTextureIdx :
joshualittb0a8a372014-09-23 09:50:21 -0700354 GrProcessorUnitTest::kAlphaTextureIdx;
joshualitt0067ff52015-07-08 14:26:19 -0700355 Direction dir = d->fRandom->nextBool() ? kX_Direction : kY_Direction;
senorblanco@chromium.org84207c42012-08-22 20:51:19 +0000356 static const int kMaxRadius = 10;
joshualitt0067ff52015-07-08 14:26:19 -0700357 int radius = d->fRandom->nextRangeU(1, kMaxRadius);
358 MorphologyType type = d->fRandom->nextBool() ? GrMorphologyEffect::kErode_MorphologyType :
senorblanco@chromium.org84207c42012-08-22 20:51:19 +0000359 GrMorphologyEffect::kDilate_MorphologyType;
360
bsalomon4a339522015-10-06 08:40:50 -0700361 return GrMorphologyEffect::Create(d->fTextures[texIdx], dir, radius, type);
senorblanco@chromium.org84207c42012-08-22 20:51:19 +0000362}
363
senorblanco@chromium.org84207c42012-08-22 20:51:19 +0000364
robertphillipsf299e712016-03-25 04:49:22 -0700365static void apply_morphology_rect(GrDrawContext* drawContext,
366 const GrClip& clip,
367 GrTexture* texture,
368 const SkIRect& srcRect,
369 const SkIRect& dstRect,
370 int radius,
371 GrMorphologyEffect::MorphologyType morphType,
372 float bounds[2],
373 Gr1DKernelEffect::Direction direction) {
cwallez80a61df2015-01-26 12:20:14 -0800374 GrPaint paint;
bsalomon4a339522015-10-06 08:40:50 -0700375 paint.addColorFragmentProcessor(GrMorphologyEffect::Create(texture,
bsalomonac856c92015-08-27 06:30:17 -0700376 direction,
377 radius,
378 morphType,
379 bounds))->unref();
egdanielc4b72722015-11-23 13:20:41 -0800380 paint.setPorterDuffXPFactory(SkXfermode::kSrc_Mode);
bsalomona2e69fc2015-11-05 10:41:43 -0800381 drawContext->fillRectToRect(clip, paint, SkMatrix::I(), SkRect::Make(dstRect),
robertphillipsea461502015-05-26 11:38:03 -0700382 SkRect::Make(srcRect));
cwallez80a61df2015-01-26 12:20:14 -0800383}
384
robertphillipsf299e712016-03-25 04:49:22 -0700385static void apply_morphology_rect_no_bounds(GrDrawContext* drawContext,
386 const GrClip& clip,
387 GrTexture* texture,
388 const SkIRect& srcRect,
389 const SkIRect& dstRect,
390 int radius,
391 GrMorphologyEffect::MorphologyType morphType,
392 Gr1DKernelEffect::Direction direction) {
senorblanco@chromium.org84207c42012-08-22 20:51:19 +0000393 GrPaint paint;
bsalomon4a339522015-10-06 08:40:50 -0700394 paint.addColorFragmentProcessor(GrMorphologyEffect::Create(texture,
bsalomonac856c92015-08-27 06:30:17 -0700395 direction,
396 radius,
397 morphType))->unref();
egdanielc4b72722015-11-23 13:20:41 -0800398 paint.setPorterDuffXPFactory(SkXfermode::kSrc_Mode);
bsalomona2e69fc2015-11-05 10:41:43 -0800399 drawContext->fillRectToRect(clip, paint, SkMatrix::I(), SkRect::Make(dstRect),
400 SkRect::Make(srcRect));
senorblanco@chromium.org84207c42012-08-22 20:51:19 +0000401}
402
robertphillipsf299e712016-03-25 04:49:22 -0700403static void apply_morphology_pass(GrDrawContext* drawContext,
404 const GrClip& clip,
405 GrTexture* texture,
406 const SkIRect& srcRect,
407 const SkIRect& dstRect,
408 int radius,
409 GrMorphologyEffect::MorphologyType morphType,
410 Gr1DKernelEffect::Direction direction) {
cwallez80a61df2015-01-26 12:20:14 -0800411 float bounds[2] = { 0.0f, 1.0f };
412 SkIRect lowerSrcRect = srcRect, lowerDstRect = dstRect;
413 SkIRect middleSrcRect = srcRect, middleDstRect = dstRect;
414 SkIRect upperSrcRect = srcRect, upperDstRect = dstRect;
415 if (direction == Gr1DKernelEffect::kX_Direction) {
416 bounds[0] = (SkIntToScalar(srcRect.left()) + 0.5f) / texture->width();
417 bounds[1] = (SkIntToScalar(srcRect.right()) - 0.5f) / texture->width();
418 lowerSrcRect.fRight = srcRect.left() + radius;
419 lowerDstRect.fRight = dstRect.left() + radius;
420 upperSrcRect.fLeft = srcRect.right() - radius;
421 upperDstRect.fLeft = dstRect.right() - radius;
422 middleSrcRect.inset(radius, 0);
423 middleDstRect.inset(radius, 0);
424 } else {
425 bounds[0] = (SkIntToScalar(srcRect.top()) + 0.5f) / texture->height();
426 bounds[1] = (SkIntToScalar(srcRect.bottom()) - 0.5f) / texture->height();
427 lowerSrcRect.fBottom = srcRect.top() + radius;
428 lowerDstRect.fBottom = dstRect.top() + radius;
429 upperSrcRect.fTop = srcRect.bottom() - radius;
430 upperDstRect.fTop = dstRect.bottom() - radius;
431 middleSrcRect.inset(0, radius);
432 middleDstRect.inset(0, radius);
433 }
434 if (middleSrcRect.fLeft - middleSrcRect.fRight >= 0) {
435 // radius covers srcRect; use bounds over entire draw
robertphillips2e1e51f2015-10-15 08:01:48 -0700436 apply_morphology_rect(drawContext, clip, texture, srcRect, dstRect, radius,
cwallez80a61df2015-01-26 12:20:14 -0800437 morphType, bounds, direction);
438 } else {
439 // Draw upper and lower margins with bounds; middle without.
robertphillips2e1e51f2015-10-15 08:01:48 -0700440 apply_morphology_rect(drawContext, clip, texture, lowerSrcRect, lowerDstRect, radius,
cwallez80a61df2015-01-26 12:20:14 -0800441 morphType, bounds, direction);
robertphillips2e1e51f2015-10-15 08:01:48 -0700442 apply_morphology_rect(drawContext, clip, texture, upperSrcRect, upperDstRect, radius,
cwallez80a61df2015-01-26 12:20:14 -0800443 morphType, bounds, direction);
robertphillips2e1e51f2015-10-15 08:01:48 -0700444 apply_morphology_rect_no_bounds(drawContext, clip, texture, middleSrcRect, middleDstRect,
joshualitt570d2f82015-02-25 13:19:48 -0800445 radius, morphType, direction);
cwallez80a61df2015-01-26 12:20:14 -0800446 }
447}
448
robertphillipsf299e712016-03-25 04:49:22 -0700449static sk_sp<SkSpecialImage> apply_morphology(SkSpecialImage* input,
450 const SkIRect& rect,
451 GrMorphologyEffect::MorphologyType morphType,
452 SkISize radius) {
453 SkAutoTUnref<GrTexture> srcTexture(SkRef(input->peekTexture()));
bsalomon49f085d2014-09-05 13:34:00 -0700454 SkASSERT(srcTexture);
senorblanco@chromium.org84207c42012-08-22 20:51:19 +0000455 GrContext* context = srcTexture->getContext();
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +0000456
joshualitt570d2f82015-02-25 13:19:48 -0800457 // setup new clip
458 GrClip clip(SkRect::MakeWH(SkIntToScalar(srcTexture->width()),
459 SkIntToScalar(srcTexture->height())));
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +0000460
senorblanco@chromium.org8fcad982013-09-17 13:41:43 +0000461 SkIRect dstRect = SkIRect::MakeWH(rect.width(), rect.height());
bsalomonf2703d82014-10-28 14:33:06 -0700462 GrSurfaceDesc desc;
bsalomon6bc1b5f2015-02-23 09:06:38 -0800463 desc.fFlags = kRenderTarget_GrSurfaceFlag;
senorblanco@chromium.orgc2594f42013-01-30 19:08:47 +0000464 desc.fWidth = rect.width();
465 desc.fHeight = rect.height();
466 desc.fConfig = kSkia8888_GrPixelConfig;
senorblanco@chromium.org8fcad982013-09-17 13:41:43 +0000467 SkIRect srcRect = rect;
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +0000468
robertphillipsf299e712016-03-25 04:49:22 -0700469 SkASSERT(radius.width() > 0 || radius.height() > 0);
470
senorblanco@chromium.org84207c42012-08-22 20:51:19 +0000471 if (radius.fWidth > 0) {
reed4e23cda2016-01-11 10:56:59 -0800472 GrTexture* scratch = context->textureProvider()->createApproxTexture(desc);
robertphillipsf299e712016-03-25 04:49:22 -0700473 if (!scratch) {
474 return nullptr;
senorblanco673d9732014-08-15 10:48:43 -0700475 }
robertphillips77a2e522015-10-17 07:43:27 -0700476 SkAutoTUnref<GrDrawContext> dstDrawContext(
477 context->drawContext(scratch->asRenderTarget()));
robertphillipsff0ca5e2015-07-22 11:54:44 -0700478 if (!dstDrawContext) {
robertphillipsf299e712016-03-25 04:49:22 -0700479 return nullptr;
robertphillipsff0ca5e2015-07-22 11:54:44 -0700480 }
481
robertphillips2e1e51f2015-10-15 08:01:48 -0700482 apply_morphology_pass(dstDrawContext, clip, srcTexture,
joshualitt570d2f82015-02-25 13:19:48 -0800483 srcRect, dstRect, radius.fWidth, morphType,
484 Gr1DKernelEffect::kX_Direction);
senorblanco@chromium.org8fcad982013-09-17 13:41:43 +0000485 SkIRect clearRect = SkIRect::MakeXYWH(dstRect.fLeft, dstRect.fBottom,
486 dstRect.width(), radius.fHeight);
bsalomon89c62982014-11-03 12:08:42 -0800487 GrColor clearColor = GrMorphologyEffect::kErode_MorphologyType == morphType ?
488 SK_ColorWHITE :
489 SK_ColorTRANSPARENT;
robertphillips2e1e51f2015-10-15 08:01:48 -0700490 dstDrawContext->clear(&clearRect, clearColor, false);
robertphillipsff0ca5e2015-07-22 11:54:44 -0700491
robertphillipseb86b552015-07-23 11:35:08 -0700492 srcTexture.reset(scratch);
senorblanco@chromium.org8fcad982013-09-17 13:41:43 +0000493 srcRect = dstRect;
senorblanco@chromium.org84207c42012-08-22 20:51:19 +0000494 }
495 if (radius.fHeight > 0) {
reed4e23cda2016-01-11 10:56:59 -0800496 GrTexture* scratch = context->textureProvider()->createApproxTexture(desc);
robertphillipsf299e712016-03-25 04:49:22 -0700497 if (!scratch) {
498 return nullptr;
senorblanco673d9732014-08-15 10:48:43 -0700499 }
robertphillips77a2e522015-10-17 07:43:27 -0700500 SkAutoTUnref<GrDrawContext> dstDrawContext(
501 context->drawContext(scratch->asRenderTarget()));
robertphillipsff0ca5e2015-07-22 11:54:44 -0700502 if (!dstDrawContext) {
robertphillipsf299e712016-03-25 04:49:22 -0700503 return nullptr;
robertphillipsff0ca5e2015-07-22 11:54:44 -0700504 }
505
robertphillips2e1e51f2015-10-15 08:01:48 -0700506 apply_morphology_pass(dstDrawContext, clip, srcTexture,
joshualitt570d2f82015-02-25 13:19:48 -0800507 srcRect, dstRect, radius.fHeight, morphType,
508 Gr1DKernelEffect::kY_Direction);
robertphillipsff0ca5e2015-07-22 11:54:44 -0700509
robertphillipseb86b552015-07-23 11:35:08 -0700510 srcTexture.reset(scratch);
senorblanco@chromium.org84207c42012-08-22 20:51:19 +0000511 }
robertphillipsf299e712016-03-25 04:49:22 -0700512
513 return SkSpecialImage::MakeFromGpu(input->internal_getProxy(),
514 SkIRect::MakeWH(rect.width(), rect.height()),
515 kNeedNewImageUniqueID_SpecialImage,
516 srcTexture);
senorblanco@chromium.org84207c42012-08-22 20:51:19 +0000517}
robertphillipsf299e712016-03-25 04:49:22 -0700518#endif
senorblanco@chromium.org84207c42012-08-22 20:51:19 +0000519
robertphillips40d8d622016-03-30 08:09:56 -0700520sk_sp<SkSpecialImage> SkMorphologyImageFilter::onFilterImage(SkSpecialImage* source,
521 const Context& ctx,
522 SkIPoint* offset) const {
robertphillipsf299e712016-03-25 04:49:22 -0700523 SkIPoint inputOffset = SkIPoint::Make(0, 0);
524 sk_sp<SkSpecialImage> input(this->filterInput(0, source, ctx, &inputOffset));
525 if (!input) {
526 return nullptr;
senorblanco@chromium.orgc2594f42013-01-30 19:08:47 +0000527 }
robertphillipsf299e712016-03-25 04:49:22 -0700528
senorblanco@chromium.orgc2594f42013-01-30 19:08:47 +0000529 SkIRect bounds;
robertphillipsf299e712016-03-25 04:49:22 -0700530 input = this->applyCropRect(this->mapContext(ctx), input.get(), &inputOffset, &bounds);
531 if (!input) {
532 return nullptr;
senorblanco@chromium.org8fcad982013-09-17 13:41:43 +0000533 }
robertphillipsf299e712016-03-25 04:49:22 -0700534
senorblanco@chromium.org2bfe36b2014-01-20 19:58:28 +0000535 SkVector radius = SkVector::Make(SkIntToScalar(this->radius().width()),
536 SkIntToScalar(this->radius().height()));
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +0000537 ctx.ctm().mapVectors(&radius, 1);
senorblanco@chromium.org2bfe36b2014-01-20 19:58:28 +0000538 int width = SkScalarFloorToInt(radius.fX);
539 int height = SkScalarFloorToInt(radius.fY);
senorblanco@chromium.org8fcad982013-09-17 13:41:43 +0000540
541 if (width < 0 || height < 0) {
robertphillipsf299e712016-03-25 04:49:22 -0700542 return nullptr;
senorblanco@chromium.org8fcad982013-09-17 13:41:43 +0000543 }
544
senorblanco@chromium.org2bfe36b2014-01-20 19:58:28 +0000545 SkIRect srcBounds = bounds;
robertphillipsf299e712016-03-25 04:49:22 -0700546 srcBounds.offset(-inputOffset);
547
548 if (0 == width && 0 == height) {
senorblanco@chromium.org6776b822014-01-03 21:48:22 +0000549 offset->fX = bounds.left();
550 offset->fY = bounds.top();
robertphillipsf299e712016-03-25 04:49:22 -0700551 return input->makeSubset(srcBounds);
senorblanco@chromium.org8fcad982013-09-17 13:41:43 +0000552 }
553
robertphillipsf299e712016-03-25 04:49:22 -0700554#if SK_SUPPORT_GPU
robertphillips090b7622016-03-28 11:07:43 -0700555 if (input->peekTexture() && input->peekTexture()->getContext()) {
robertphillips40d8d622016-03-30 08:09:56 -0700556 auto type = (kDilate_Op == this->op()) ? GrMorphologyEffect::kDilate_MorphologyType
557 : GrMorphologyEffect::kErode_MorphologyType;
robertphillipsf299e712016-03-25 04:49:22 -0700558 sk_sp<SkSpecialImage> result(apply_morphology(input.get(), srcBounds, type,
559 SkISize::Make(width, height)));
560 if (result) {
561 offset->fX = bounds.left();
562 offset->fY = bounds.top();
563 }
564 return result;
565 }
566#endif
567
568 SkPixmap inputPixmap;
569
570 if (!input->peekPixels(&inputPixmap)) {
571 return nullptr;
572 }
573
574 if (inputPixmap.colorType() != kN32_SkColorType) {
575 return nullptr;
576 }
577
578 SkImageInfo info = SkImageInfo::Make(bounds.width(), bounds.height(),
579 inputPixmap.colorType(), inputPixmap.alphaType());
580
581 SkBitmap dst;
582 if (!dst.tryAllocPixels(info)) {
583 return nullptr;
584 }
585
586 SkAutoLockPixels dstLock(dst);
587
588 SkMorphologyImageFilter::Proc procX, procY;
589
robertphillips40d8d622016-03-30 08:09:56 -0700590 if (kDilate_Op == this->op()) {
robertphillipsf299e712016-03-25 04:49:22 -0700591 procX = SkOpts::dilate_x;
592 procY = SkOpts::dilate_y;
593 } else {
594 procX = SkOpts::erode_x;
595 procY = SkOpts::erode_y;
596 }
597
598 if (width > 0 && height > 0) {
599 SkBitmap tmp;
600 if (!tmp.tryAllocPixels(info)) {
601 return nullptr;
602 }
603
604 SkAutoLockPixels tmpLock(tmp);
605
606 call_proc_X(procX, inputPixmap, &tmp, width, srcBounds);
607 SkIRect tmpBounds = SkIRect::MakeWH(srcBounds.width(), srcBounds.height());
608 call_proc_Y(procY,
609 tmp.getAddr32(tmpBounds.left(), tmpBounds.top()), tmp.rowBytesAsPixels(),
610 &dst, height, tmpBounds);
611 } else if (width > 0) {
612 call_proc_X(procX, inputPixmap, &dst, width, srcBounds);
613 } else if (height > 0) {
614 call_proc_Y(procY,
halcanary9d524f22016-03-29 09:03:52 -0700615 inputPixmap.addr32(srcBounds.left(), srcBounds.top()),
robertphillipsf299e712016-03-25 04:49:22 -0700616 inputPixmap.rowBytesAsPixels(),
617 &dst, height, srcBounds);
senorblanco@chromium.org8fcad982013-09-17 13:41:43 +0000618 }
senorblanco@chromium.org6776b822014-01-03 21:48:22 +0000619 offset->fX = bounds.left();
620 offset->fY = bounds.top();
robertphillipsf299e712016-03-25 04:49:22 -0700621
622 return SkSpecialImage::MakeFromRaster(source->internal_getProxy(),
623 SkIRect::MakeWH(bounds.width(), bounds.height()),
624 dst);
senorblanco@chromium.org05054f12012-03-02 21:05:45 +0000625}