blob: ea714e59f69b1579bc26d2e87c95c8c58915a6f5 [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,
robertphillipsfc11b0a2016-04-05 09:09:36 -070033 sk_sp<SkImageFilter> input,
senorblanco24e06d52015-03-18 12:11:33 -070034 const CropRect* cropRect)
robertphillipsfc11b0a2016-04-05 09:09:36 -070035 : INHERITED(&input, 1, cropRect)
robertphillipsf299e712016-03-25 04:49:22 -070036 , 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();
robertphillipsfc11b0a2016-04-05 09:09:36 -070079 return Make(width, height, common.getInput(0), &common.cropRect());
reed9fa60da2014-08-21 07:59:51 -070080}
81
reed60c9b582016-04-03 09:11:13 -070082sk_sp<SkFlattenable> SkDilateImageFilter::CreateProc(SkReadBuffer& buffer) {
reed9fa60da2014-08-21 07:59:51 -070083 SK_IMAGEFILTER_UNFLATTEN_COMMON(common, 1);
84 const int width = buffer.readInt();
85 const int height = buffer.readInt();
robertphillipsfc11b0a2016-04-05 09:09:36 -070086 return Make(width, height, common.getInput(0), &common.cropRect());
reed9fa60da2014-08-21 07:59:51 -070087}
88
robertphillipsf3f5bad2014-12-19 13:49:15 -080089#ifndef SK_IGNORE_TO_STRING
90void 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
98void 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.comcf8fb1f2012-08-02 14:03:32 +0000105#if SK_SUPPORT_GPU
senorblanco@chromium.org84207c42012-08-22 20:51:19 +0000106
107///////////////////////////////////////////////////////////////////////////////
senorblanco@chromium.org84207c42012-08-22 20:51:19 +0000108/**
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 */
114class GrMorphologyEffect : public Gr1DKernelEffect {
115
116public:
117
118 enum MorphologyType {
119 kErode_MorphologyType,
120 kDilate_MorphologyType,
121 };
122
bsalomon4a339522015-10-06 08:40:50 -0700123 static GrFragmentProcessor* Create(GrTexture* tex, Direction dir, int radius,
124 MorphologyType type) {
125 return new GrMorphologyEffect(tex, dir, radius, type);
bsalomon@google.com0ac6af42013-01-16 15:16:18 +0000126 }
127
bsalomon4a339522015-10-06 08:40:50 -0700128 static GrFragmentProcessor* Create(GrTexture* tex, Direction dir, int radius,
129 MorphologyType type, float bounds[2]) {
130 return new GrMorphologyEffect(tex, dir, radius, type, bounds);
cwallez80a61df2015-01-26 12:20:14 -0800131 }
132
senorblanco@chromium.org84207c42012-08-22 20:51:19 +0000133 virtual ~GrMorphologyEffect();
134
135 MorphologyType type() const { return fType; }
cwallez80a61df2015-01-26 12:20:14 -0800136 bool useRange() const { return fUseRange; }
137 const float* range() const { return fRange; }
senorblanco@chromium.org84207c42012-08-22 20:51:19 +0000138
mtklein36352bf2015-03-25 18:17:31 -0700139 const char* name() const override { return "Morphology"; }
senorblanco@chromium.org84207c42012-08-22 20:51:19 +0000140
senorblanco@chromium.org84207c42012-08-22 20:51:19 +0000141protected:
142
143 MorphologyType fType;
cwallez80a61df2015-01-26 12:20:14 -0800144 bool fUseRange;
145 float fRange[2];
senorblanco@chromium.org84207c42012-08-22 20:51:19 +0000146
147private:
egdaniel57d3b032015-11-13 11:57:27 -0800148 GrGLSLFragmentProcessor* onCreateGLSLInstance() const override;
wangyixb1daa862015-08-18 11:29:31 -0700149
egdaniel57d3b032015-11-13 11:57:27 -0800150 void onGetGLSLProcessorKey(const GrGLSLCaps&, GrProcessorKeyBuilder*) const override;
wangyix4b3050b2015-08-04 07:59:37 -0700151
mtklein36352bf2015-03-25 18:17:31 -0700152 bool onIsEqual(const GrFragmentProcessor&) const override;
bsalomon@google.com68b58c92013-01-17 16:50:08 +0000153
mtklein36352bf2015-03-25 18:17:31 -0700154 void onComputeInvariantOutput(GrInvariantOutput* inout) const override;
egdaniel1a8ecdf2014-10-03 06:24:12 -0700155
bsalomon4a339522015-10-06 08:40:50 -0700156 GrMorphologyEffect(GrTexture*, Direction, int radius, MorphologyType);
157 GrMorphologyEffect(GrTexture*, Direction, int radius, MorphologyType,
joshualitt5f10b5c2015-07-09 10:24:35 -0700158 float bounds[2]);
bsalomon@google.com0ac6af42013-01-16 15:16:18 +0000159
joshualittb0a8a372014-09-23 09:50:21 -0700160 GR_DECLARE_FRAGMENT_PROCESSOR_TEST;
senorblanco@chromium.org84207c42012-08-22 20:51:19 +0000161
162 typedef Gr1DKernelEffect INHERITED;
163};
164
165///////////////////////////////////////////////////////////////////////////////
166
egdaniel64c47282015-11-13 06:54:19 -0800167class GrGLMorphologyEffect : public GrGLSLFragmentProcessor {
senorblanco@chromium.org84207c42012-08-22 20:51:19 +0000168public:
robertphillips9cdb9922016-02-03 12:25:40 -0800169 void emitCode(EmitArgs&) override;
senorblanco@chromium.org84207c42012-08-22 20:51:19 +0000170
robertphillipsbf536af2016-02-04 06:11:53 -0800171 static inline void GenKey(const GrProcessor&, const GrGLSLCaps&, GrProcessorKeyBuilder*);
senorblanco@chromium.org84207c42012-08-22 20:51:19 +0000172
wangyixb1daa862015-08-18 11:29:31 -0700173protected:
egdaniel018fb622015-10-28 07:26:40 -0700174 void onSetData(const GrGLSLProgramDataManager&, const GrProcessor&) override;
senorblanco@chromium.org84207c42012-08-22 20:51:19 +0000175
176private:
egdaniel018fb622015-10-28 07:26:40 -0700177 GrGLSLProgramDataManager::UniformHandle fPixelSizeUni;
178 GrGLSLProgramDataManager::UniformHandle fRangeUni;
senorblanco@chromium.org84207c42012-08-22 20:51:19 +0000179
egdaniel64c47282015-11-13 06:54:19 -0800180 typedef GrGLSLFragmentProcessor INHERITED;
senorblanco@chromium.org84207c42012-08-22 20:51:19 +0000181};
182
wangyix7c157a92015-07-22 15:08:53 -0700183void GrGLMorphologyEffect::emitCode(EmitArgs& args) {
robertphillipsbf536af2016-02-04 06:11:53 -0800184 const GrMorphologyEffect& me = args.fFp.cast<GrMorphologyEffect>();
185
egdaniel7ea439b2015-12-03 09:20:44 -0800186 GrGLSLUniformHandler* uniformHandler = args.fUniformHandler;
cdalton5e58cee2016-02-11 12:49:47 -0800187 fPixelSizeUni = uniformHandler->addUniform(kFragment_GrShaderFlag,
egdaniel7ea439b2015-12-03 09:20:44 -0800188 kFloat_GrSLType, kDefault_GrSLPrecision,
189 "PixelSize");
190 const char* pixelSizeInc = uniformHandler->getUniformCStr(fPixelSizeUni);
cdalton5e58cee2016-02-11 12:49:47 -0800191 fRangeUni = uniformHandler->addUniform(kFragment_GrShaderFlag,
egdaniel7ea439b2015-12-03 09:20:44 -0800192 kVec2f_GrSLType, kDefault_GrSLPrecision,
193 "Range");
194 const char* range = uniformHandler->getUniformCStr(fRangeUni);
senorblanco@chromium.org84207c42012-08-22 20:51:19 +0000195
cdalton85285412016-02-18 12:37:07 -0800196 GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder;
egdaniel4ca2e602015-11-18 08:01:26 -0800197 SkString coords2D = fragBuilder->ensureFSCoords2D(args.fCoords, 0);
senorblanco@chromium.org84207c42012-08-22 20:51:19 +0000198 const char* func;
robertphillipsbf536af2016-02-04 06:11:53 -0800199 switch (me.type()) {
senorblanco@chromium.org84207c42012-08-22 20:51:19 +0000200 case GrMorphologyEffect::kErode_MorphologyType:
egdaniel4ca2e602015-11-18 08:01:26 -0800201 fragBuilder->codeAppendf("\t\t%s = vec4(1, 1, 1, 1);\n", args.fOutputColor);
senorblanco@chromium.org84207c42012-08-22 20:51:19 +0000202 func = "min";
203 break;
204 case GrMorphologyEffect::kDilate_MorphologyType:
egdaniel4ca2e602015-11-18 08:01:26 -0800205 fragBuilder->codeAppendf("\t\t%s = vec4(0, 0, 0, 0);\n", args.fOutputColor);
senorblanco@chromium.org84207c42012-08-22 20:51:19 +0000206 func = "max";
207 break;
208 default:
commit-bot@chromium.org88cb22b2014-04-30 14:17:00 +0000209 SkFAIL("Unexpected type");
senorblanco@chromium.org84207c42012-08-22 20:51:19 +0000210 func = ""; // suppress warning
211 break;
212 }
senorblanco@chromium.org84207c42012-08-22 20:51:19 +0000213
cwallez80a61df2015-01-26 12:20:14 -0800214 const char* dir;
robertphillipsbf536af2016-02-04 06:11:53 -0800215 switch (me.direction()) {
cwallez80a61df2015-01-26 12:20:14 -0800216 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
robertphillipsbf536af2016-02-04 06:11:53 -0800227 int width = GrMorphologyEffect::WidthFromRadius(me.radius());
228
cwallez80a61df2015-01-26 12:20:14 -0800229 // vec2 coord = coord2D;
egdaniel4ca2e602015-11-18 08:01:26 -0800230 fragBuilder->codeAppendf("\t\tvec2 coord = %s;\n", coords2D.c_str());
cwallez80a61df2015-01-26 12:20:14 -0800231 // coord.x -= radius * pixelSize;
robertphillipsbf536af2016-02-04 06:11:53 -0800232 fragBuilder->codeAppendf("\t\tcoord.%s -= %d.0 * %s; \n", dir, me.radius(), pixelSizeInc);
233 if (me.useRange()) {
cwallez80a61df2015-01-26 12:20:14 -0800234 // highBound = min(highBound, coord.x + (width-1) * pixelSize);
egdaniel4ca2e602015-11-18 08:01:26 -0800235 fragBuilder->codeAppendf("\t\tfloat highBound = min(%s.y, coord.%s + %f * %s);",
robertphillipsbf536af2016-02-04 06:11:53 -0800236 range, dir, float(width - 1), pixelSizeInc);
cwallez80a61df2015-01-26 12:20:14 -0800237 // coord.x = max(lowBound, coord.x);
egdaniel4ca2e602015-11-18 08:01:26 -0800238 fragBuilder->codeAppendf("\t\tcoord.%s = max(%s.x, coord.%s);", dir, range, dir);
cwallez80a61df2015-01-26 12:20:14 -0800239 }
robertphillipsbf536af2016-02-04 06:11:53 -0800240 fragBuilder->codeAppendf("\t\tfor (int i = 0; i < %d; i++) {\n", width);
egdaniel4ca2e602015-11-18 08:01:26 -0800241 fragBuilder->codeAppendf("\t\t\t%s = %s(%s, ", args.fOutputColor, func, args.fOutputColor);
242 fragBuilder->appendTextureLookup(args.fSamplers[0], "coord");
243 fragBuilder->codeAppend(");\n");
cwallez80a61df2015-01-26 12:20:14 -0800244 // coord.x += pixelSize;
egdaniel4ca2e602015-11-18 08:01:26 -0800245 fragBuilder->codeAppendf("\t\t\tcoord.%s += %s;\n", dir, pixelSizeInc);
robertphillipsbf536af2016-02-04 06:11:53 -0800246 if (me.useRange()) {
cwallez80a61df2015-01-26 12:20:14 -0800247 // coord.x = min(highBound, coord.x);
egdaniel4ca2e602015-11-18 08:01:26 -0800248 fragBuilder->codeAppendf("\t\t\tcoord.%s = min(highBound, coord.%s);", dir, dir);
cwallez80a61df2015-01-26 12:20:14 -0800249 }
egdaniel4ca2e602015-11-18 08:01:26 -0800250 fragBuilder->codeAppend("\t\t}\n");
bsalomon@google.comf910d3b2013-03-07 17:06:57 +0000251 SkString modulate;
wangyix7c157a92015-07-22 15:08:53 -0700252 GrGLSLMulVarBy4f(&modulate, args.fOutputColor, args.fInputColor);
egdaniel4ca2e602015-11-18 08:01:26 -0800253 fragBuilder->codeAppend(modulate.c_str());
senorblanco@chromium.org84207c42012-08-22 20:51:19 +0000254}
255
joshualittb0a8a372014-09-23 09:50:21 -0700256void GrGLMorphologyEffect::GenKey(const GrProcessor& proc,
jvanverthcfc18862015-04-28 08:48:20 -0700257 const GrGLSLCaps&, GrProcessorKeyBuilder* b) {
joshualittb0a8a372014-09-23 09:50:21 -0700258 const GrMorphologyEffect& m = proc.cast<GrMorphologyEffect>();
bsalomon63e99f72014-07-21 08:03:14 -0700259 uint32_t key = static_cast<uint32_t>(m.radius());
senorblanco@chromium.org84207c42012-08-22 20:51:19 +0000260 key |= (m.type() << 8);
cwallez80a61df2015-01-26 12:20:14 -0800261 key |= (m.direction() << 9);
robertphillipsbf536af2016-02-04 06:11:53 -0800262 if (m.useRange()) {
263 key |= 1 << 10;
264 }
bsalomon63e99f72014-07-21 08:03:14 -0700265 b->add32(key);
senorblanco@chromium.org84207c42012-08-22 20:51:19 +0000266}
267
egdaniel018fb622015-10-28 07:26:40 -0700268void GrGLMorphologyEffect::onSetData(const GrGLSLProgramDataManager& pdman,
robertphillipsbf536af2016-02-04 06:11:53 -0800269 const GrProcessor& proc) {
cwallez80a61df2015-01-26 12:20:14 -0800270 const GrMorphologyEffect& m = proc.cast<GrMorphologyEffect>();
271 GrTexture& texture = *m.texture(0);
cwallez80a61df2015-01-26 12:20:14 -0800272
273 float pixelSize = 0.0f;
robertphillipsbf536af2016-02-04 06:11:53 -0800274 switch (m.direction()) {
senorblanco@chromium.org84207c42012-08-22 20:51:19 +0000275 case Gr1DKernelEffect::kX_Direction:
cwallez80a61df2015-01-26 12:20:14 -0800276 pixelSize = 1.0f / texture.width();
senorblanco@chromium.org84207c42012-08-22 20:51:19 +0000277 break;
278 case Gr1DKernelEffect::kY_Direction:
cwallez80a61df2015-01-26 12:20:14 -0800279 pixelSize = 1.0f / texture.height();
senorblanco@chromium.org84207c42012-08-22 20:51:19 +0000280 break;
281 default:
commit-bot@chromium.org88cb22b2014-04-30 14:17:00 +0000282 SkFAIL("Unknown filter direction.");
senorblanco@chromium.org84207c42012-08-22 20:51:19 +0000283 }
cwallez80a61df2015-01-26 12:20:14 -0800284 pdman.set1f(fPixelSizeUni, pixelSize);
285
robertphillipsbf536af2016-02-04 06:11:53 -0800286 if (m.useRange()) {
cwallez80a61df2015-01-26 12:20:14 -0800287 const float* range = m.range();
robertphillipsbf536af2016-02-04 06:11:53 -0800288 if (m.direction() && texture.origin() == kBottomLeft_GrSurfaceOrigin) {
cwallez80a61df2015-01-26 12:20:14 -0800289 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.org84207c42012-08-22 20:51:19 +0000294}
295
296///////////////////////////////////////////////////////////////////////////////
297
bsalomon4a339522015-10-06 08:40:50 -0700298GrMorphologyEffect::GrMorphologyEffect(GrTexture* texture,
senorblanco@chromium.org84207c42012-08-22 20:51:19 +0000299 Direction direction,
300 int radius,
301 MorphologyType type)
bsalomon4a339522015-10-06 08:40:50 -0700302 : INHERITED(texture, direction, radius)
robertphillipsf299e712016-03-25 04:49:22 -0700303 , fType(type)
304 , fUseRange(false) {
joshualitteb2a6762014-12-04 11:35:33 -0800305 this->initClassID<GrMorphologyEffect>();
senorblanco@chromium.org84207c42012-08-22 20:51:19 +0000306}
307
bsalomon4a339522015-10-06 08:40:50 -0700308GrMorphologyEffect::GrMorphologyEffect(GrTexture* texture,
cwallez80a61df2015-01-26 12:20:14 -0800309 Direction direction,
310 int radius,
311 MorphologyType type,
312 float range[2])
bsalomon4a339522015-10-06 08:40:50 -0700313 : INHERITED(texture, direction, radius)
robertphillipsf299e712016-03-25 04:49:22 -0700314 , fType(type)
315 , fUseRange(true) {
cwallez80a61df2015-01-26 12:20:14 -0800316 this->initClassID<GrMorphologyEffect>();
317 fRange[0] = range[0];
318 fRange[1] = range[1];
319}
320
senorblanco@chromium.org84207c42012-08-22 20:51:19 +0000321GrMorphologyEffect::~GrMorphologyEffect() {
322}
323
egdaniel57d3b032015-11-13 11:57:27 -0800324void GrMorphologyEffect::onGetGLSLProcessorKey(const GrGLSLCaps& caps,
325 GrProcessorKeyBuilder* b) const {
joshualitteb2a6762014-12-04 11:35:33 -0800326 GrGLMorphologyEffect::GenKey(*this, caps, b);
senorblanco@chromium.org84207c42012-08-22 20:51:19 +0000327}
328
egdaniel57d3b032015-11-13 11:57:27 -0800329GrGLSLFragmentProcessor* GrMorphologyEffect::onCreateGLSLInstance() const {
robertphillipsbf536af2016-02-04 06:11:53 -0800330 return new GrGLMorphologyEffect;
joshualitteb2a6762014-12-04 11:35:33 -0800331}
bsalomon0e08fc12014-10-15 08:19:04 -0700332bool GrMorphologyEffect::onIsEqual(const GrFragmentProcessor& sBase) const {
joshualitt49586be2014-09-16 08:21:41 -0700333 const GrMorphologyEffect& s = sBase.cast<GrMorphologyEffect>();
bsalomon420d7e92014-10-16 09:18:09 -0700334 return (this->radius() == s.radius() &&
senorblanco@chromium.org84207c42012-08-22 20:51:19 +0000335 this->direction() == s.direction() &&
cwallez80a61df2015-01-26 12:20:14 -0800336 this->useRange() == s.useRange() &&
senorblanco@chromium.org84207c42012-08-22 20:51:19 +0000337 this->type() == s.type());
338}
339
egdaniel605dd0f2014-11-12 08:35:25 -0800340void GrMorphologyEffect::onComputeInvariantOutput(GrInvariantOutput* inout) const {
bsalomon@google.com68b58c92013-01-17 16:50:08 +0000341 // This is valid because the color components of the result of the kernel all come
342 // exactly from existing values in the source texture.
egdaniel1a8ecdf2014-10-03 06:24:12 -0700343 this->updateInvariantOutputForModulation(inout);
bsalomon@google.com68b58c92013-01-17 16:50:08 +0000344}
345
senorblanco@chromium.org84207c42012-08-22 20:51:19 +0000346///////////////////////////////////////////////////////////////////////////////
347
joshualittb0a8a372014-09-23 09:50:21 -0700348GR_DEFINE_FRAGMENT_PROCESSOR_TEST(GrMorphologyEffect);
senorblanco@chromium.org84207c42012-08-22 20:51:19 +0000349
bsalomonc21b09e2015-08-28 18:46:56 -0700350const GrFragmentProcessor* GrMorphologyEffect::TestCreate(GrProcessorTestData* d) {
joshualitt0067ff52015-07-08 14:26:19 -0700351 int texIdx = d->fRandom->nextBool() ? GrProcessorUnitTest::kSkiaPMTextureIdx :
joshualittb0a8a372014-09-23 09:50:21 -0700352 GrProcessorUnitTest::kAlphaTextureIdx;
joshualitt0067ff52015-07-08 14:26:19 -0700353 Direction dir = d->fRandom->nextBool() ? kX_Direction : kY_Direction;
senorblanco@chromium.org84207c42012-08-22 20:51:19 +0000354 static const int kMaxRadius = 10;
joshualitt0067ff52015-07-08 14:26:19 -0700355 int radius = d->fRandom->nextRangeU(1, kMaxRadius);
356 MorphologyType type = d->fRandom->nextBool() ? GrMorphologyEffect::kErode_MorphologyType :
senorblanco@chromium.org84207c42012-08-22 20:51:19 +0000357 GrMorphologyEffect::kDilate_MorphologyType;
358
bsalomon4a339522015-10-06 08:40:50 -0700359 return GrMorphologyEffect::Create(d->fTextures[texIdx], dir, radius, type);
senorblanco@chromium.org84207c42012-08-22 20:51:19 +0000360}
361
senorblanco@chromium.org84207c42012-08-22 20:51:19 +0000362
robertphillipsf299e712016-03-25 04:49:22 -0700363static 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) {
cwallez80a61df2015-01-26 12:20:14 -0800372 GrPaint paint;
bsalomon4a339522015-10-06 08:40:50 -0700373 paint.addColorFragmentProcessor(GrMorphologyEffect::Create(texture,
bsalomonac856c92015-08-27 06:30:17 -0700374 direction,
375 radius,
376 morphType,
377 bounds))->unref();
egdanielc4b72722015-11-23 13:20:41 -0800378 paint.setPorterDuffXPFactory(SkXfermode::kSrc_Mode);
bsalomona2e69fc2015-11-05 10:41:43 -0800379 drawContext->fillRectToRect(clip, paint, SkMatrix::I(), SkRect::Make(dstRect),
robertphillipsea461502015-05-26 11:38:03 -0700380 SkRect::Make(srcRect));
cwallez80a61df2015-01-26 12:20:14 -0800381}
382
robertphillipsf299e712016-03-25 04:49:22 -0700383static 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.org84207c42012-08-22 20:51:19 +0000391 GrPaint paint;
bsalomon4a339522015-10-06 08:40:50 -0700392 paint.addColorFragmentProcessor(GrMorphologyEffect::Create(texture,
bsalomonac856c92015-08-27 06:30:17 -0700393 direction,
394 radius,
395 morphType))->unref();
egdanielc4b72722015-11-23 13:20:41 -0800396 paint.setPorterDuffXPFactory(SkXfermode::kSrc_Mode);
bsalomona2e69fc2015-11-05 10:41:43 -0800397 drawContext->fillRectToRect(clip, paint, SkMatrix::I(), SkRect::Make(dstRect),
398 SkRect::Make(srcRect));
senorblanco@chromium.org84207c42012-08-22 20:51:19 +0000399}
400
robertphillipsf299e712016-03-25 04:49:22 -0700401static 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) {
cwallez80a61df2015-01-26 12:20:14 -0800409 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
robertphillips2e1e51f2015-10-15 08:01:48 -0700434 apply_morphology_rect(drawContext, clip, texture, srcRect, dstRect, radius,
cwallez80a61df2015-01-26 12:20:14 -0800435 morphType, bounds, direction);
436 } else {
437 // Draw upper and lower margins with bounds; middle without.
robertphillips2e1e51f2015-10-15 08:01:48 -0700438 apply_morphology_rect(drawContext, clip, texture, lowerSrcRect, lowerDstRect, radius,
cwallez80a61df2015-01-26 12:20:14 -0800439 morphType, bounds, direction);
robertphillips2e1e51f2015-10-15 08:01:48 -0700440 apply_morphology_rect(drawContext, clip, texture, upperSrcRect, upperDstRect, radius,
cwallez80a61df2015-01-26 12:20:14 -0800441 morphType, bounds, direction);
robertphillips2e1e51f2015-10-15 08:01:48 -0700442 apply_morphology_rect_no_bounds(drawContext, clip, texture, middleSrcRect, middleDstRect,
joshualitt570d2f82015-02-25 13:19:48 -0800443 radius, morphType, direction);
cwallez80a61df2015-01-26 12:20:14 -0800444 }
445}
446
robertphillipsf299e712016-03-25 04:49:22 -0700447static 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()));
bsalomon49f085d2014-09-05 13:34:00 -0700452 SkASSERT(srcTexture);
senorblanco@chromium.org84207c42012-08-22 20:51:19 +0000453 GrContext* context = srcTexture->getContext();
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +0000454
joshualitt570d2f82015-02-25 13:19:48 -0800455 // setup new clip
456 GrClip clip(SkRect::MakeWH(SkIntToScalar(srcTexture->width()),
457 SkIntToScalar(srcTexture->height())));
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +0000458
senorblanco@chromium.org8fcad982013-09-17 13:41:43 +0000459 SkIRect dstRect = SkIRect::MakeWH(rect.width(), rect.height());
bsalomonf2703d82014-10-28 14:33:06 -0700460 GrSurfaceDesc desc;
bsalomon6bc1b5f2015-02-23 09:06:38 -0800461 desc.fFlags = kRenderTarget_GrSurfaceFlag;
senorblanco@chromium.orgc2594f42013-01-30 19:08:47 +0000462 desc.fWidth = rect.width();
463 desc.fHeight = rect.height();
464 desc.fConfig = kSkia8888_GrPixelConfig;
senorblanco@chromium.org8fcad982013-09-17 13:41:43 +0000465 SkIRect srcRect = rect;
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +0000466
robertphillipsf299e712016-03-25 04:49:22 -0700467 SkASSERT(radius.width() > 0 || radius.height() > 0);
468
senorblanco@chromium.org84207c42012-08-22 20:51:19 +0000469 if (radius.fWidth > 0) {
reed4e23cda2016-01-11 10:56:59 -0800470 GrTexture* scratch = context->textureProvider()->createApproxTexture(desc);
robertphillipsf299e712016-03-25 04:49:22 -0700471 if (!scratch) {
472 return nullptr;
senorblanco673d9732014-08-15 10:48:43 -0700473 }
robertphillips77a2e522015-10-17 07:43:27 -0700474 SkAutoTUnref<GrDrawContext> dstDrawContext(
475 context->drawContext(scratch->asRenderTarget()));
robertphillipsff0ca5e2015-07-22 11:54:44 -0700476 if (!dstDrawContext) {
robertphillipsf299e712016-03-25 04:49:22 -0700477 return nullptr;
robertphillipsff0ca5e2015-07-22 11:54:44 -0700478 }
479
robertphillips2e1e51f2015-10-15 08:01:48 -0700480 apply_morphology_pass(dstDrawContext, clip, srcTexture,
joshualitt570d2f82015-02-25 13:19:48 -0800481 srcRect, dstRect, radius.fWidth, morphType,
482 Gr1DKernelEffect::kX_Direction);
senorblanco@chromium.org8fcad982013-09-17 13:41:43 +0000483 SkIRect clearRect = SkIRect::MakeXYWH(dstRect.fLeft, dstRect.fBottom,
484 dstRect.width(), radius.fHeight);
bsalomon89c62982014-11-03 12:08:42 -0800485 GrColor clearColor = GrMorphologyEffect::kErode_MorphologyType == morphType ?
486 SK_ColorWHITE :
487 SK_ColorTRANSPARENT;
robertphillips2e1e51f2015-10-15 08:01:48 -0700488 dstDrawContext->clear(&clearRect, clearColor, false);
robertphillipsff0ca5e2015-07-22 11:54:44 -0700489
robertphillipseb86b552015-07-23 11:35:08 -0700490 srcTexture.reset(scratch);
senorblanco@chromium.org8fcad982013-09-17 13:41:43 +0000491 srcRect = dstRect;
senorblanco@chromium.org84207c42012-08-22 20:51:19 +0000492 }
493 if (radius.fHeight > 0) {
reed4e23cda2016-01-11 10:56:59 -0800494 GrTexture* scratch = context->textureProvider()->createApproxTexture(desc);
robertphillipsf299e712016-03-25 04:49:22 -0700495 if (!scratch) {
496 return nullptr;
senorblanco673d9732014-08-15 10:48:43 -0700497 }
robertphillips77a2e522015-10-17 07:43:27 -0700498 SkAutoTUnref<GrDrawContext> dstDrawContext(
499 context->drawContext(scratch->asRenderTarget()));
robertphillipsff0ca5e2015-07-22 11:54:44 -0700500 if (!dstDrawContext) {
robertphillipsf299e712016-03-25 04:49:22 -0700501 return nullptr;
robertphillipsff0ca5e2015-07-22 11:54:44 -0700502 }
503
robertphillips2e1e51f2015-10-15 08:01:48 -0700504 apply_morphology_pass(dstDrawContext, clip, srcTexture,
joshualitt570d2f82015-02-25 13:19:48 -0800505 srcRect, dstRect, radius.fHeight, morphType,
506 Gr1DKernelEffect::kY_Direction);
robertphillipsff0ca5e2015-07-22 11:54:44 -0700507
robertphillipseb86b552015-07-23 11:35:08 -0700508 srcTexture.reset(scratch);
senorblanco@chromium.org84207c42012-08-22 20:51:19 +0000509 }
robertphillipsf299e712016-03-25 04:49:22 -0700510
511 return SkSpecialImage::MakeFromGpu(input->internal_getProxy(),
512 SkIRect::MakeWH(rect.width(), rect.height()),
513 kNeedNewImageUniqueID_SpecialImage,
514 srcTexture);
senorblanco@chromium.org84207c42012-08-22 20:51:19 +0000515}
robertphillipsf299e712016-03-25 04:49:22 -0700516#endif
senorblanco@chromium.org84207c42012-08-22 20:51:19 +0000517
robertphillips40d8d622016-03-30 08:09:56 -0700518sk_sp<SkSpecialImage> SkMorphologyImageFilter::onFilterImage(SkSpecialImage* source,
519 const Context& ctx,
520 SkIPoint* offset) const {
robertphillipsf299e712016-03-25 04:49:22 -0700521 SkIPoint inputOffset = SkIPoint::Make(0, 0);
522 sk_sp<SkSpecialImage> input(this->filterInput(0, source, ctx, &inputOffset));
523 if (!input) {
524 return nullptr;
senorblanco@chromium.orgc2594f42013-01-30 19:08:47 +0000525 }
robertphillipsf299e712016-03-25 04:49:22 -0700526
senorblanco@chromium.orgc2594f42013-01-30 19:08:47 +0000527 SkIRect bounds;
robertphillipsf299e712016-03-25 04:49:22 -0700528 input = this->applyCropRect(this->mapContext(ctx), input.get(), &inputOffset, &bounds);
529 if (!input) {
530 return nullptr;
senorblanco@chromium.org8fcad982013-09-17 13:41:43 +0000531 }
robertphillipsf299e712016-03-25 04:49:22 -0700532
senorblanco@chromium.org2bfe36b2014-01-20 19:58:28 +0000533 SkVector radius = SkVector::Make(SkIntToScalar(this->radius().width()),
534 SkIntToScalar(this->radius().height()));
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +0000535 ctx.ctm().mapVectors(&radius, 1);
senorblanco@chromium.org2bfe36b2014-01-20 19:58:28 +0000536 int width = SkScalarFloorToInt(radius.fX);
537 int height = SkScalarFloorToInt(radius.fY);
senorblanco@chromium.org8fcad982013-09-17 13:41:43 +0000538
539 if (width < 0 || height < 0) {
robertphillipsf299e712016-03-25 04:49:22 -0700540 return nullptr;
senorblanco@chromium.org8fcad982013-09-17 13:41:43 +0000541 }
542
senorblanco@chromium.org2bfe36b2014-01-20 19:58:28 +0000543 SkIRect srcBounds = bounds;
robertphillipsf299e712016-03-25 04:49:22 -0700544 srcBounds.offset(-inputOffset);
545
546 if (0 == width && 0 == height) {
senorblanco@chromium.org6776b822014-01-03 21:48:22 +0000547 offset->fX = bounds.left();
548 offset->fY = bounds.top();
robertphillipsf299e712016-03-25 04:49:22 -0700549 return input->makeSubset(srcBounds);
senorblanco@chromium.org8fcad982013-09-17 13:41:43 +0000550 }
551
robertphillipsf299e712016-03-25 04:49:22 -0700552#if SK_SUPPORT_GPU
robertphillips090b7622016-03-28 11:07:43 -0700553 if (input->peekTexture() && input->peekTexture()->getContext()) {
robertphillips40d8d622016-03-30 08:09:56 -0700554 auto type = (kDilate_Op == this->op()) ? GrMorphologyEffect::kDilate_MorphologyType
555 : GrMorphologyEffect::kErode_MorphologyType;
robertphillipsf299e712016-03-25 04:49:22 -0700556 sk_sp<SkSpecialImage> result(apply_morphology(input.get(), srcBounds, type,
557 SkISize::Make(width, height)));
558 if (result) {
559 offset->fX = bounds.left();
560 offset->fY = bounds.top();
561 }
562 return result;
563 }
564#endif
565
566 SkPixmap inputPixmap;
567
568 if (!input->peekPixels(&inputPixmap)) {
569 return nullptr;
570 }
571
572 if (inputPixmap.colorType() != kN32_SkColorType) {
573 return nullptr;
574 }
575
576 SkImageInfo info = SkImageInfo::Make(bounds.width(), bounds.height(),
577 inputPixmap.colorType(), inputPixmap.alphaType());
578
579 SkBitmap dst;
580 if (!dst.tryAllocPixels(info)) {
581 return nullptr;
582 }
583
584 SkAutoLockPixels dstLock(dst);
585
586 SkMorphologyImageFilter::Proc procX, procY;
587
robertphillips40d8d622016-03-30 08:09:56 -0700588 if (kDilate_Op == this->op()) {
robertphillipsf299e712016-03-25 04:49:22 -0700589 procX = SkOpts::dilate_x;
590 procY = SkOpts::dilate_y;
591 } else {
592 procX = SkOpts::erode_x;
593 procY = SkOpts::erode_y;
594 }
595
596 if (width > 0 && height > 0) {
597 SkBitmap tmp;
598 if (!tmp.tryAllocPixels(info)) {
599 return nullptr;
600 }
601
602 SkAutoLockPixels tmpLock(tmp);
603
604 call_proc_X(procX, inputPixmap, &tmp, width, srcBounds);
605 SkIRect tmpBounds = SkIRect::MakeWH(srcBounds.width(), srcBounds.height());
606 call_proc_Y(procY,
607 tmp.getAddr32(tmpBounds.left(), tmpBounds.top()), tmp.rowBytesAsPixels(),
608 &dst, height, tmpBounds);
609 } else if (width > 0) {
610 call_proc_X(procX, inputPixmap, &dst, width, srcBounds);
611 } else if (height > 0) {
612 call_proc_Y(procY,
halcanary9d524f22016-03-29 09:03:52 -0700613 inputPixmap.addr32(srcBounds.left(), srcBounds.top()),
robertphillipsf299e712016-03-25 04:49:22 -0700614 inputPixmap.rowBytesAsPixels(),
615 &dst, height, srcBounds);
senorblanco@chromium.org8fcad982013-09-17 13:41:43 +0000616 }
senorblanco@chromium.org6776b822014-01-03 21:48:22 +0000617 offset->fX = bounds.left();
618 offset->fY = bounds.top();
robertphillipsf299e712016-03-25 04:49:22 -0700619
620 return SkSpecialImage::MakeFromRaster(source->internal_getProxy(),
621 SkIRect::MakeWH(bounds.width(), bounds.height()),
622 dst);
senorblanco@chromium.org05054f12012-03-02 21:05:45 +0000623}