blob: bbd236ea72bc5c177c4bdfbc4daa449a1f976912 [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
robertphillips225db442016-04-17 14:27:05 -070031sk_sp<SkImageFilter> SkDilateImageFilter::Make(int radiusX, int radiusY,
32 sk_sp<SkImageFilter> input,
33 const CropRect* cropRect) {
34 if (radiusX < 0 || radiusY < 0) {
35 return nullptr;
36 }
37 return sk_sp<SkImageFilter>(new SkDilateImageFilter(radiusX, radiusY,
38 std::move(input),
39 cropRect));
40}
41
42
43sk_sp<SkImageFilter> SkErodeImageFilter::Make(int radiusX, int radiusY,
44 sk_sp<SkImageFilter> input,
45 const CropRect* cropRect) {
46 if (radiusX < 0 || radiusY < 0) {
47 return nullptr;
48 }
49 return sk_sp<SkImageFilter>(new SkErodeImageFilter(radiusX, radiusY,
50 std::move(input),
51 cropRect));
52}
53
senorblanco@chromium.org2bfe36b2014-01-20 19:58:28 +000054SkMorphologyImageFilter::SkMorphologyImageFilter(int radiusX,
55 int radiusY,
robertphillipsfc11b0a2016-04-05 09:09:36 -070056 sk_sp<SkImageFilter> input,
senorblanco24e06d52015-03-18 12:11:33 -070057 const CropRect* cropRect)
robertphillipsfc11b0a2016-04-05 09:09:36 -070058 : INHERITED(&input, 1, cropRect)
robertphillipsf299e712016-03-25 04:49:22 -070059 , fRadius(SkISize::Make(radiusX, radiusY)) {
senorblanco@chromium.org05054f12012-03-02 21:05:45 +000060}
61
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +000062void SkMorphologyImageFilter::flatten(SkWriteBuffer& buffer) const {
senorblanco@chromium.org05054f12012-03-02 21:05:45 +000063 this->INHERITED::flatten(buffer);
tomhudson@google.com75589252012-04-10 17:42:21 +000064 buffer.writeInt(fRadius.fWidth);
65 buffer.writeInt(fRadius.fHeight);
senorblanco@chromium.org05054f12012-03-02 21:05:45 +000066}
67
robertphillipsf299e712016-03-25 04:49:22 -070068static void call_proc_X(SkMorphologyImageFilter::Proc procX,
robertphillips64612512016-04-08 12:10:42 -070069 const SkBitmap& src, SkBitmap* dst,
robertphillipsf299e712016-03-25 04:49:22 -070070 int radiusX, const SkIRect& bounds) {
robertphillips64612512016-04-08 12:10:42 -070071 procX(src.getAddr32(bounds.left(), bounds.top()), dst->getAddr32(0, 0),
senorblanco@chromium.org0ded88d2014-01-24 15:43:50 +000072 radiusX, bounds.width(), bounds.height(),
73 src.rowBytesAsPixels(), dst->rowBytesAsPixels());
senorblanco@chromium.org05054f12012-03-02 21:05:45 +000074}
75
robertphillipsf299e712016-03-25 04:49:22 -070076static void call_proc_Y(SkMorphologyImageFilter::Proc procY,
77 const SkPMColor* src, int srcRowBytesAsPixels, SkBitmap* dst,
78 int radiusY, const SkIRect& bounds) {
79 procY(src, dst->getAddr32(0, 0),
senorblanco@chromium.org0ded88d2014-01-24 15:43:50 +000080 radiusY, bounds.height(), bounds.width(),
robertphillipsf299e712016-03-25 04:49:22 -070081 srcRowBytesAsPixels, dst->rowBytesAsPixels());
senorblanco@chromium.org05054f12012-03-02 21:05:45 +000082}
83
senorblancoe5e79842016-03-21 14:51:59 -070084SkRect SkMorphologyImageFilter::computeFastBounds(const SkRect& src) const {
85 SkRect bounds = this->getInput(0) ? this->getInput(0)->computeFastBounds(src) : src;
86 bounds.outset(SkIntToScalar(fRadius.width()), SkIntToScalar(fRadius.height()));
87 return bounds;
senorblanco@chromium.org336d1d72014-01-27 21:03:17 +000088}
89
senorblancoe5e79842016-03-21 14:51:59 -070090SkIRect SkMorphologyImageFilter::onFilterNodeBounds(const SkIRect& src, const SkMatrix& ctm,
91 MapDirection) const {
senorblanco@chromium.orgc4b12f12014-02-05 17:51:22 +000092 SkVector radius = SkVector::Make(SkIntToScalar(this->radius().width()),
93 SkIntToScalar(this->radius().height()));
94 ctm.mapVectors(&radius, 1);
senorblancoe5e79842016-03-21 14:51:59 -070095 return src.makeOutset(SkScalarCeilToInt(radius.x()), SkScalarCeilToInt(radius.y()));
senorblanco@chromium.orgc4b12f12014-02-05 17:51:22 +000096}
97
reed60c9b582016-04-03 09:11:13 -070098sk_sp<SkFlattenable> SkErodeImageFilter::CreateProc(SkReadBuffer& buffer) {
reed9fa60da2014-08-21 07:59:51 -070099 SK_IMAGEFILTER_UNFLATTEN_COMMON(common, 1);
100 const int width = buffer.readInt();
101 const int height = buffer.readInt();
robertphillipsfc11b0a2016-04-05 09:09:36 -0700102 return Make(width, height, common.getInput(0), &common.cropRect());
reed9fa60da2014-08-21 07:59:51 -0700103}
104
reed60c9b582016-04-03 09:11:13 -0700105sk_sp<SkFlattenable> SkDilateImageFilter::CreateProc(SkReadBuffer& buffer) {
reed9fa60da2014-08-21 07:59:51 -0700106 SK_IMAGEFILTER_UNFLATTEN_COMMON(common, 1);
107 const int width = buffer.readInt();
108 const int height = buffer.readInt();
robertphillipsfc11b0a2016-04-05 09:09:36 -0700109 return Make(width, height, common.getInput(0), &common.cropRect());
reed9fa60da2014-08-21 07:59:51 -0700110}
111
robertphillipsf3f5bad2014-12-19 13:49:15 -0800112#ifndef SK_IGNORE_TO_STRING
113void SkErodeImageFilter::toString(SkString* str) const {
114 str->appendf("SkErodeImageFilter: (");
115 str->appendf("radius: (%d,%d)", this->radius().fWidth, this->radius().fHeight);
116 str->append(")");
117}
118#endif
119
120#ifndef SK_IGNORE_TO_STRING
121void SkDilateImageFilter::toString(SkString* str) const {
122 str->appendf("SkDilateImageFilter: (");
123 str->appendf("radius: (%d,%d)", this->radius().fWidth, this->radius().fHeight);
124 str->append(")");
125}
126#endif
127
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000128#if SK_SUPPORT_GPU
senorblanco@chromium.org84207c42012-08-22 20:51:19 +0000129
130///////////////////////////////////////////////////////////////////////////////
senorblanco@chromium.org84207c42012-08-22 20:51:19 +0000131/**
132 * Morphology effects. Depending upon the type of morphology, either the
133 * component-wise min (Erode_Type) or max (Dilate_Type) of all pixels in the
134 * kernel is selected as the new color. The new color is modulated by the input
135 * color.
136 */
137class GrMorphologyEffect : public Gr1DKernelEffect {
138
139public:
140
141 enum MorphologyType {
142 kErode_MorphologyType,
143 kDilate_MorphologyType,
144 };
145
bsalomon4a339522015-10-06 08:40:50 -0700146 static GrFragmentProcessor* Create(GrTexture* tex, Direction dir, int radius,
147 MorphologyType type) {
148 return new GrMorphologyEffect(tex, dir, radius, type);
bsalomon@google.com0ac6af42013-01-16 15:16:18 +0000149 }
150
bsalomon4a339522015-10-06 08:40:50 -0700151 static GrFragmentProcessor* Create(GrTexture* tex, Direction dir, int radius,
152 MorphologyType type, float bounds[2]) {
153 return new GrMorphologyEffect(tex, dir, radius, type, bounds);
cwallez80a61df2015-01-26 12:20:14 -0800154 }
155
senorblanco@chromium.org84207c42012-08-22 20:51:19 +0000156 virtual ~GrMorphologyEffect();
157
158 MorphologyType type() const { return fType; }
cwallez80a61df2015-01-26 12:20:14 -0800159 bool useRange() const { return fUseRange; }
160 const float* range() const { return fRange; }
senorblanco@chromium.org84207c42012-08-22 20:51:19 +0000161
mtklein36352bf2015-03-25 18:17:31 -0700162 const char* name() const override { return "Morphology"; }
senorblanco@chromium.org84207c42012-08-22 20:51:19 +0000163
senorblanco@chromium.org84207c42012-08-22 20:51:19 +0000164protected:
165
166 MorphologyType fType;
cwallez80a61df2015-01-26 12:20:14 -0800167 bool fUseRange;
168 float fRange[2];
senorblanco@chromium.org84207c42012-08-22 20:51:19 +0000169
170private:
egdaniel57d3b032015-11-13 11:57:27 -0800171 GrGLSLFragmentProcessor* onCreateGLSLInstance() const override;
wangyixb1daa862015-08-18 11:29:31 -0700172
egdaniel57d3b032015-11-13 11:57:27 -0800173 void onGetGLSLProcessorKey(const GrGLSLCaps&, GrProcessorKeyBuilder*) const override;
wangyix4b3050b2015-08-04 07:59:37 -0700174
mtklein36352bf2015-03-25 18:17:31 -0700175 bool onIsEqual(const GrFragmentProcessor&) const override;
bsalomon@google.com68b58c92013-01-17 16:50:08 +0000176
mtklein36352bf2015-03-25 18:17:31 -0700177 void onComputeInvariantOutput(GrInvariantOutput* inout) const override;
egdaniel1a8ecdf2014-10-03 06:24:12 -0700178
bsalomon4a339522015-10-06 08:40:50 -0700179 GrMorphologyEffect(GrTexture*, Direction, int radius, MorphologyType);
180 GrMorphologyEffect(GrTexture*, Direction, int radius, MorphologyType,
joshualitt5f10b5c2015-07-09 10:24:35 -0700181 float bounds[2]);
bsalomon@google.com0ac6af42013-01-16 15:16:18 +0000182
joshualittb0a8a372014-09-23 09:50:21 -0700183 GR_DECLARE_FRAGMENT_PROCESSOR_TEST;
senorblanco@chromium.org84207c42012-08-22 20:51:19 +0000184
185 typedef Gr1DKernelEffect INHERITED;
186};
187
188///////////////////////////////////////////////////////////////////////////////
189
egdaniel64c47282015-11-13 06:54:19 -0800190class GrGLMorphologyEffect : public GrGLSLFragmentProcessor {
senorblanco@chromium.org84207c42012-08-22 20:51:19 +0000191public:
robertphillips9cdb9922016-02-03 12:25:40 -0800192 void emitCode(EmitArgs&) override;
senorblanco@chromium.org84207c42012-08-22 20:51:19 +0000193
robertphillipsbf536af2016-02-04 06:11:53 -0800194 static inline void GenKey(const GrProcessor&, const GrGLSLCaps&, GrProcessorKeyBuilder*);
senorblanco@chromium.org84207c42012-08-22 20:51:19 +0000195
wangyixb1daa862015-08-18 11:29:31 -0700196protected:
egdaniel018fb622015-10-28 07:26:40 -0700197 void onSetData(const GrGLSLProgramDataManager&, const GrProcessor&) override;
senorblanco@chromium.org84207c42012-08-22 20:51:19 +0000198
199private:
egdaniel018fb622015-10-28 07:26:40 -0700200 GrGLSLProgramDataManager::UniformHandle fPixelSizeUni;
201 GrGLSLProgramDataManager::UniformHandle fRangeUni;
senorblanco@chromium.org84207c42012-08-22 20:51:19 +0000202
egdaniel64c47282015-11-13 06:54:19 -0800203 typedef GrGLSLFragmentProcessor INHERITED;
senorblanco@chromium.org84207c42012-08-22 20:51:19 +0000204};
205
wangyix7c157a92015-07-22 15:08:53 -0700206void GrGLMorphologyEffect::emitCode(EmitArgs& args) {
robertphillipsbf536af2016-02-04 06:11:53 -0800207 const GrMorphologyEffect& me = args.fFp.cast<GrMorphologyEffect>();
208
egdaniel7ea439b2015-12-03 09:20:44 -0800209 GrGLSLUniformHandler* uniformHandler = args.fUniformHandler;
cdalton5e58cee2016-02-11 12:49:47 -0800210 fPixelSizeUni = uniformHandler->addUniform(kFragment_GrShaderFlag,
egdaniel7ea439b2015-12-03 09:20:44 -0800211 kFloat_GrSLType, kDefault_GrSLPrecision,
212 "PixelSize");
213 const char* pixelSizeInc = uniformHandler->getUniformCStr(fPixelSizeUni);
cdalton5e58cee2016-02-11 12:49:47 -0800214 fRangeUni = uniformHandler->addUniform(kFragment_GrShaderFlag,
egdaniel7ea439b2015-12-03 09:20:44 -0800215 kVec2f_GrSLType, kDefault_GrSLPrecision,
216 "Range");
217 const char* range = uniformHandler->getUniformCStr(fRangeUni);
senorblanco@chromium.org84207c42012-08-22 20:51:19 +0000218
cdalton85285412016-02-18 12:37:07 -0800219 GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder;
egdaniel4ca2e602015-11-18 08:01:26 -0800220 SkString coords2D = fragBuilder->ensureFSCoords2D(args.fCoords, 0);
senorblanco@chromium.org84207c42012-08-22 20:51:19 +0000221 const char* func;
robertphillipsbf536af2016-02-04 06:11:53 -0800222 switch (me.type()) {
senorblanco@chromium.org84207c42012-08-22 20:51:19 +0000223 case GrMorphologyEffect::kErode_MorphologyType:
egdaniel4ca2e602015-11-18 08:01:26 -0800224 fragBuilder->codeAppendf("\t\t%s = vec4(1, 1, 1, 1);\n", args.fOutputColor);
senorblanco@chromium.org84207c42012-08-22 20:51:19 +0000225 func = "min";
226 break;
227 case GrMorphologyEffect::kDilate_MorphologyType:
egdaniel4ca2e602015-11-18 08:01:26 -0800228 fragBuilder->codeAppendf("\t\t%s = vec4(0, 0, 0, 0);\n", args.fOutputColor);
senorblanco@chromium.org84207c42012-08-22 20:51:19 +0000229 func = "max";
230 break;
231 default:
commit-bot@chromium.org88cb22b2014-04-30 14:17:00 +0000232 SkFAIL("Unexpected type");
senorblanco@chromium.org84207c42012-08-22 20:51:19 +0000233 func = ""; // suppress warning
234 break;
235 }
senorblanco@chromium.org84207c42012-08-22 20:51:19 +0000236
cwallez80a61df2015-01-26 12:20:14 -0800237 const char* dir;
robertphillipsbf536af2016-02-04 06:11:53 -0800238 switch (me.direction()) {
cwallez80a61df2015-01-26 12:20:14 -0800239 case Gr1DKernelEffect::kX_Direction:
240 dir = "x";
241 break;
242 case Gr1DKernelEffect::kY_Direction:
243 dir = "y";
244 break;
245 default:
246 SkFAIL("Unknown filter direction.");
247 dir = ""; // suppress warning
248 }
249
robertphillipsbf536af2016-02-04 06:11:53 -0800250 int width = GrMorphologyEffect::WidthFromRadius(me.radius());
251
cwallez80a61df2015-01-26 12:20:14 -0800252 // vec2 coord = coord2D;
egdaniel4ca2e602015-11-18 08:01:26 -0800253 fragBuilder->codeAppendf("\t\tvec2 coord = %s;\n", coords2D.c_str());
cwallez80a61df2015-01-26 12:20:14 -0800254 // coord.x -= radius * pixelSize;
robertphillipsbf536af2016-02-04 06:11:53 -0800255 fragBuilder->codeAppendf("\t\tcoord.%s -= %d.0 * %s; \n", dir, me.radius(), pixelSizeInc);
256 if (me.useRange()) {
cwallez80a61df2015-01-26 12:20:14 -0800257 // highBound = min(highBound, coord.x + (width-1) * pixelSize);
egdaniel4ca2e602015-11-18 08:01:26 -0800258 fragBuilder->codeAppendf("\t\tfloat highBound = min(%s.y, coord.%s + %f * %s);",
robertphillipsbf536af2016-02-04 06:11:53 -0800259 range, dir, float(width - 1), pixelSizeInc);
cwallez80a61df2015-01-26 12:20:14 -0800260 // coord.x = max(lowBound, coord.x);
egdaniel4ca2e602015-11-18 08:01:26 -0800261 fragBuilder->codeAppendf("\t\tcoord.%s = max(%s.x, coord.%s);", dir, range, dir);
cwallez80a61df2015-01-26 12:20:14 -0800262 }
robertphillipsbf536af2016-02-04 06:11:53 -0800263 fragBuilder->codeAppendf("\t\tfor (int i = 0; i < %d; i++) {\n", width);
egdaniel4ca2e602015-11-18 08:01:26 -0800264 fragBuilder->codeAppendf("\t\t\t%s = %s(%s, ", args.fOutputColor, func, args.fOutputColor);
cdalton3f6f76f2016-04-11 12:18:09 -0700265 fragBuilder->appendTextureLookup(args.fTexSamplers[0], "coord");
egdaniel4ca2e602015-11-18 08:01:26 -0800266 fragBuilder->codeAppend(");\n");
cwallez80a61df2015-01-26 12:20:14 -0800267 // coord.x += pixelSize;
egdaniel4ca2e602015-11-18 08:01:26 -0800268 fragBuilder->codeAppendf("\t\t\tcoord.%s += %s;\n", dir, pixelSizeInc);
robertphillipsbf536af2016-02-04 06:11:53 -0800269 if (me.useRange()) {
cwallez80a61df2015-01-26 12:20:14 -0800270 // coord.x = min(highBound, coord.x);
egdaniel4ca2e602015-11-18 08:01:26 -0800271 fragBuilder->codeAppendf("\t\t\tcoord.%s = min(highBound, coord.%s);", dir, dir);
cwallez80a61df2015-01-26 12:20:14 -0800272 }
egdaniel4ca2e602015-11-18 08:01:26 -0800273 fragBuilder->codeAppend("\t\t}\n");
bsalomon@google.comf910d3b2013-03-07 17:06:57 +0000274 SkString modulate;
wangyix7c157a92015-07-22 15:08:53 -0700275 GrGLSLMulVarBy4f(&modulate, args.fOutputColor, args.fInputColor);
egdaniel4ca2e602015-11-18 08:01:26 -0800276 fragBuilder->codeAppend(modulate.c_str());
senorblanco@chromium.org84207c42012-08-22 20:51:19 +0000277}
278
joshualittb0a8a372014-09-23 09:50:21 -0700279void GrGLMorphologyEffect::GenKey(const GrProcessor& proc,
jvanverthcfc18862015-04-28 08:48:20 -0700280 const GrGLSLCaps&, GrProcessorKeyBuilder* b) {
joshualittb0a8a372014-09-23 09:50:21 -0700281 const GrMorphologyEffect& m = proc.cast<GrMorphologyEffect>();
bsalomon63e99f72014-07-21 08:03:14 -0700282 uint32_t key = static_cast<uint32_t>(m.radius());
senorblanco@chromium.org84207c42012-08-22 20:51:19 +0000283 key |= (m.type() << 8);
cwallez80a61df2015-01-26 12:20:14 -0800284 key |= (m.direction() << 9);
robertphillipsbf536af2016-02-04 06:11:53 -0800285 if (m.useRange()) {
286 key |= 1 << 10;
287 }
bsalomon63e99f72014-07-21 08:03:14 -0700288 b->add32(key);
senorblanco@chromium.org84207c42012-08-22 20:51:19 +0000289}
290
egdaniel018fb622015-10-28 07:26:40 -0700291void GrGLMorphologyEffect::onSetData(const GrGLSLProgramDataManager& pdman,
robertphillipsbf536af2016-02-04 06:11:53 -0800292 const GrProcessor& proc) {
cwallez80a61df2015-01-26 12:20:14 -0800293 const GrMorphologyEffect& m = proc.cast<GrMorphologyEffect>();
294 GrTexture& texture = *m.texture(0);
cwallez80a61df2015-01-26 12:20:14 -0800295
296 float pixelSize = 0.0f;
robertphillipsbf536af2016-02-04 06:11:53 -0800297 switch (m.direction()) {
senorblanco@chromium.org84207c42012-08-22 20:51:19 +0000298 case Gr1DKernelEffect::kX_Direction:
cwallez80a61df2015-01-26 12:20:14 -0800299 pixelSize = 1.0f / texture.width();
senorblanco@chromium.org84207c42012-08-22 20:51:19 +0000300 break;
301 case Gr1DKernelEffect::kY_Direction:
cwallez80a61df2015-01-26 12:20:14 -0800302 pixelSize = 1.0f / texture.height();
senorblanco@chromium.org84207c42012-08-22 20:51:19 +0000303 break;
304 default:
commit-bot@chromium.org88cb22b2014-04-30 14:17:00 +0000305 SkFAIL("Unknown filter direction.");
senorblanco@chromium.org84207c42012-08-22 20:51:19 +0000306 }
cwallez80a61df2015-01-26 12:20:14 -0800307 pdman.set1f(fPixelSizeUni, pixelSize);
308
robertphillipsbf536af2016-02-04 06:11:53 -0800309 if (m.useRange()) {
cwallez80a61df2015-01-26 12:20:14 -0800310 const float* range = m.range();
robertphillipsbf536af2016-02-04 06:11:53 -0800311 if (m.direction() && texture.origin() == kBottomLeft_GrSurfaceOrigin) {
cwallez80a61df2015-01-26 12:20:14 -0800312 pdman.set2f(fRangeUni, 1.0f - range[1], 1.0f - range[0]);
313 } else {
314 pdman.set2f(fRangeUni, range[0], range[1]);
315 }
316 }
senorblanco@chromium.org84207c42012-08-22 20:51:19 +0000317}
318
319///////////////////////////////////////////////////////////////////////////////
320
bsalomon4a339522015-10-06 08:40:50 -0700321GrMorphologyEffect::GrMorphologyEffect(GrTexture* texture,
senorblanco@chromium.org84207c42012-08-22 20:51:19 +0000322 Direction direction,
323 int radius,
324 MorphologyType type)
bsalomon4a339522015-10-06 08:40:50 -0700325 : INHERITED(texture, direction, radius)
robertphillipsf299e712016-03-25 04:49:22 -0700326 , fType(type)
327 , fUseRange(false) {
joshualitteb2a6762014-12-04 11:35:33 -0800328 this->initClassID<GrMorphologyEffect>();
senorblanco@chromium.org84207c42012-08-22 20:51:19 +0000329}
330
bsalomon4a339522015-10-06 08:40:50 -0700331GrMorphologyEffect::GrMorphologyEffect(GrTexture* texture,
cwallez80a61df2015-01-26 12:20:14 -0800332 Direction direction,
333 int radius,
334 MorphologyType type,
335 float range[2])
bsalomon4a339522015-10-06 08:40:50 -0700336 : INHERITED(texture, direction, radius)
robertphillipsf299e712016-03-25 04:49:22 -0700337 , fType(type)
338 , fUseRange(true) {
cwallez80a61df2015-01-26 12:20:14 -0800339 this->initClassID<GrMorphologyEffect>();
340 fRange[0] = range[0];
341 fRange[1] = range[1];
342}
343
senorblanco@chromium.org84207c42012-08-22 20:51:19 +0000344GrMorphologyEffect::~GrMorphologyEffect() {
345}
346
egdaniel57d3b032015-11-13 11:57:27 -0800347void GrMorphologyEffect::onGetGLSLProcessorKey(const GrGLSLCaps& caps,
348 GrProcessorKeyBuilder* b) const {
joshualitteb2a6762014-12-04 11:35:33 -0800349 GrGLMorphologyEffect::GenKey(*this, caps, b);
senorblanco@chromium.org84207c42012-08-22 20:51:19 +0000350}
351
egdaniel57d3b032015-11-13 11:57:27 -0800352GrGLSLFragmentProcessor* GrMorphologyEffect::onCreateGLSLInstance() const {
robertphillipsbf536af2016-02-04 06:11:53 -0800353 return new GrGLMorphologyEffect;
joshualitteb2a6762014-12-04 11:35:33 -0800354}
bsalomon0e08fc12014-10-15 08:19:04 -0700355bool GrMorphologyEffect::onIsEqual(const GrFragmentProcessor& sBase) const {
joshualitt49586be2014-09-16 08:21:41 -0700356 const GrMorphologyEffect& s = sBase.cast<GrMorphologyEffect>();
bsalomon420d7e92014-10-16 09:18:09 -0700357 return (this->radius() == s.radius() &&
senorblanco@chromium.org84207c42012-08-22 20:51:19 +0000358 this->direction() == s.direction() &&
cwallez80a61df2015-01-26 12:20:14 -0800359 this->useRange() == s.useRange() &&
senorblanco@chromium.org84207c42012-08-22 20:51:19 +0000360 this->type() == s.type());
361}
362
egdaniel605dd0f2014-11-12 08:35:25 -0800363void GrMorphologyEffect::onComputeInvariantOutput(GrInvariantOutput* inout) const {
bsalomon@google.com68b58c92013-01-17 16:50:08 +0000364 // This is valid because the color components of the result of the kernel all come
365 // exactly from existing values in the source texture.
egdaniel1a8ecdf2014-10-03 06:24:12 -0700366 this->updateInvariantOutputForModulation(inout);
bsalomon@google.com68b58c92013-01-17 16:50:08 +0000367}
368
senorblanco@chromium.org84207c42012-08-22 20:51:19 +0000369///////////////////////////////////////////////////////////////////////////////
370
joshualittb0a8a372014-09-23 09:50:21 -0700371GR_DEFINE_FRAGMENT_PROCESSOR_TEST(GrMorphologyEffect);
senorblanco@chromium.org84207c42012-08-22 20:51:19 +0000372
bsalomonc21b09e2015-08-28 18:46:56 -0700373const GrFragmentProcessor* GrMorphologyEffect::TestCreate(GrProcessorTestData* d) {
joshualitt0067ff52015-07-08 14:26:19 -0700374 int texIdx = d->fRandom->nextBool() ? GrProcessorUnitTest::kSkiaPMTextureIdx :
joshualittb0a8a372014-09-23 09:50:21 -0700375 GrProcessorUnitTest::kAlphaTextureIdx;
joshualitt0067ff52015-07-08 14:26:19 -0700376 Direction dir = d->fRandom->nextBool() ? kX_Direction : kY_Direction;
senorblanco@chromium.org84207c42012-08-22 20:51:19 +0000377 static const int kMaxRadius = 10;
joshualitt0067ff52015-07-08 14:26:19 -0700378 int radius = d->fRandom->nextRangeU(1, kMaxRadius);
379 MorphologyType type = d->fRandom->nextBool() ? GrMorphologyEffect::kErode_MorphologyType :
senorblanco@chromium.org84207c42012-08-22 20:51:19 +0000380 GrMorphologyEffect::kDilate_MorphologyType;
381
bsalomon4a339522015-10-06 08:40:50 -0700382 return GrMorphologyEffect::Create(d->fTextures[texIdx], dir, radius, type);
senorblanco@chromium.org84207c42012-08-22 20:51:19 +0000383}
384
senorblanco@chromium.org84207c42012-08-22 20:51:19 +0000385
robertphillipsf299e712016-03-25 04:49:22 -0700386static void apply_morphology_rect(GrDrawContext* drawContext,
387 const GrClip& clip,
388 GrTexture* texture,
389 const SkIRect& srcRect,
390 const SkIRect& dstRect,
391 int radius,
392 GrMorphologyEffect::MorphologyType morphType,
393 float bounds[2],
394 Gr1DKernelEffect::Direction direction) {
cwallez80a61df2015-01-26 12:20:14 -0800395 GrPaint paint;
brianosman898235c2016-04-06 07:38:23 -0700396 // SRGBTODO: AllowSRGBInputs?
bsalomon4a339522015-10-06 08:40:50 -0700397 paint.addColorFragmentProcessor(GrMorphologyEffect::Create(texture,
bsalomonac856c92015-08-27 06:30:17 -0700398 direction,
399 radius,
400 morphType,
401 bounds))->unref();
egdanielc4b72722015-11-23 13:20:41 -0800402 paint.setPorterDuffXPFactory(SkXfermode::kSrc_Mode);
bsalomona2e69fc2015-11-05 10:41:43 -0800403 drawContext->fillRectToRect(clip, paint, SkMatrix::I(), SkRect::Make(dstRect),
robertphillipsea461502015-05-26 11:38:03 -0700404 SkRect::Make(srcRect));
cwallez80a61df2015-01-26 12:20:14 -0800405}
406
robertphillipsf299e712016-03-25 04:49:22 -0700407static void apply_morphology_rect_no_bounds(GrDrawContext* drawContext,
408 const GrClip& clip,
409 GrTexture* texture,
410 const SkIRect& srcRect,
411 const SkIRect& dstRect,
412 int radius,
413 GrMorphologyEffect::MorphologyType morphType,
414 Gr1DKernelEffect::Direction direction) {
senorblanco@chromium.org84207c42012-08-22 20:51:19 +0000415 GrPaint paint;
brianosman898235c2016-04-06 07:38:23 -0700416 // SRGBTODO: AllowSRGBInputs?
bsalomon4a339522015-10-06 08:40:50 -0700417 paint.addColorFragmentProcessor(GrMorphologyEffect::Create(texture,
bsalomonac856c92015-08-27 06:30:17 -0700418 direction,
419 radius,
420 morphType))->unref();
egdanielc4b72722015-11-23 13:20:41 -0800421 paint.setPorterDuffXPFactory(SkXfermode::kSrc_Mode);
bsalomona2e69fc2015-11-05 10:41:43 -0800422 drawContext->fillRectToRect(clip, paint, SkMatrix::I(), SkRect::Make(dstRect),
423 SkRect::Make(srcRect));
senorblanco@chromium.org84207c42012-08-22 20:51:19 +0000424}
425
robertphillipsf299e712016-03-25 04:49:22 -0700426static void apply_morphology_pass(GrDrawContext* drawContext,
427 const GrClip& clip,
428 GrTexture* texture,
429 const SkIRect& srcRect,
430 const SkIRect& dstRect,
431 int radius,
432 GrMorphologyEffect::MorphologyType morphType,
433 Gr1DKernelEffect::Direction direction) {
cwallez80a61df2015-01-26 12:20:14 -0800434 float bounds[2] = { 0.0f, 1.0f };
435 SkIRect lowerSrcRect = srcRect, lowerDstRect = dstRect;
436 SkIRect middleSrcRect = srcRect, middleDstRect = dstRect;
437 SkIRect upperSrcRect = srcRect, upperDstRect = dstRect;
438 if (direction == Gr1DKernelEffect::kX_Direction) {
439 bounds[0] = (SkIntToScalar(srcRect.left()) + 0.5f) / texture->width();
440 bounds[1] = (SkIntToScalar(srcRect.right()) - 0.5f) / texture->width();
441 lowerSrcRect.fRight = srcRect.left() + radius;
442 lowerDstRect.fRight = dstRect.left() + radius;
443 upperSrcRect.fLeft = srcRect.right() - radius;
444 upperDstRect.fLeft = dstRect.right() - radius;
445 middleSrcRect.inset(radius, 0);
446 middleDstRect.inset(radius, 0);
447 } else {
448 bounds[0] = (SkIntToScalar(srcRect.top()) + 0.5f) / texture->height();
449 bounds[1] = (SkIntToScalar(srcRect.bottom()) - 0.5f) / texture->height();
450 lowerSrcRect.fBottom = srcRect.top() + radius;
451 lowerDstRect.fBottom = dstRect.top() + radius;
452 upperSrcRect.fTop = srcRect.bottom() - radius;
453 upperDstRect.fTop = dstRect.bottom() - radius;
454 middleSrcRect.inset(0, radius);
455 middleDstRect.inset(0, radius);
456 }
457 if (middleSrcRect.fLeft - middleSrcRect.fRight >= 0) {
458 // radius covers srcRect; use bounds over entire draw
robertphillips2e1e51f2015-10-15 08:01:48 -0700459 apply_morphology_rect(drawContext, clip, texture, srcRect, dstRect, radius,
cwallez80a61df2015-01-26 12:20:14 -0800460 morphType, bounds, direction);
461 } else {
462 // Draw upper and lower margins with bounds; middle without.
robertphillips2e1e51f2015-10-15 08:01:48 -0700463 apply_morphology_rect(drawContext, clip, texture, lowerSrcRect, lowerDstRect, radius,
cwallez80a61df2015-01-26 12:20:14 -0800464 morphType, bounds, direction);
robertphillips2e1e51f2015-10-15 08:01:48 -0700465 apply_morphology_rect(drawContext, clip, texture, upperSrcRect, upperDstRect, radius,
cwallez80a61df2015-01-26 12:20:14 -0800466 morphType, bounds, direction);
robertphillips2e1e51f2015-10-15 08:01:48 -0700467 apply_morphology_rect_no_bounds(drawContext, clip, texture, middleSrcRect, middleDstRect,
joshualitt570d2f82015-02-25 13:19:48 -0800468 radius, morphType, direction);
cwallez80a61df2015-01-26 12:20:14 -0800469 }
470}
471
robertphillips64612512016-04-08 12:10:42 -0700472static sk_sp<SkSpecialImage> apply_morphology(GrContext* context,
473 SkSpecialImage* input,
robertphillipsf299e712016-03-25 04:49:22 -0700474 const SkIRect& rect,
475 GrMorphologyEffect::MorphologyType morphType,
476 SkISize radius) {
robertphillipsc91fd342016-04-25 12:32:54 -0700477 sk_sp<GrTexture> srcTexture(input->asTextureRef(context));
bsalomon49f085d2014-09-05 13:34:00 -0700478 SkASSERT(srcTexture);
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +0000479
joshualitt570d2f82015-02-25 13:19:48 -0800480 // setup new clip
cdalton846c0512016-05-13 10:25:00 -0700481 const GrFixedClip clip(SkIRect::MakeWH(srcTexture->width(), srcTexture->height()));
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +0000482
robertphillipsd4c741e2016-04-28 09:55:15 -0700483 const SkIRect dstRect = SkIRect::MakeWH(rect.width(), rect.height());
senorblanco@chromium.org8fcad982013-09-17 13:41:43 +0000484 SkIRect srcRect = rect;
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +0000485
robertphillipsf299e712016-03-25 04:49:22 -0700486 SkASSERT(radius.width() > 0 || radius.height() > 0);
487
senorblanco@chromium.org84207c42012-08-22 20:51:19 +0000488 if (radius.fWidth > 0) {
robertphillips76948d42016-05-04 12:47:41 -0700489 sk_sp<GrDrawContext> dstDrawContext(context->newDrawContext(SkBackingFit::kApprox,
robertphillipsd4c741e2016-04-28 09:55:15 -0700490 rect.width(), rect.height(),
491 kSkia8888_GrPixelConfig));
robertphillipsff0ca5e2015-07-22 11:54:44 -0700492 if (!dstDrawContext) {
robertphillipsf299e712016-03-25 04:49:22 -0700493 return nullptr;
robertphillipsff0ca5e2015-07-22 11:54:44 -0700494 }
495
robertphillips6c7e3252016-04-27 10:47:51 -0700496 apply_morphology_pass(dstDrawContext.get(), clip, srcTexture.get(),
joshualitt570d2f82015-02-25 13:19:48 -0800497 srcRect, dstRect, radius.fWidth, morphType,
498 Gr1DKernelEffect::kX_Direction);
senorblanco@chromium.org8fcad982013-09-17 13:41:43 +0000499 SkIRect clearRect = SkIRect::MakeXYWH(dstRect.fLeft, dstRect.fBottom,
500 dstRect.width(), radius.fHeight);
robertphillipsd4c741e2016-04-28 09:55:15 -0700501 GrColor clearColor = GrMorphologyEffect::kErode_MorphologyType == morphType
502 ? SK_ColorWHITE
503 : SK_ColorTRANSPARENT;
robertphillips2e1e51f2015-10-15 08:01:48 -0700504 dstDrawContext->clear(&clearRect, clearColor, false);
robertphillipsff0ca5e2015-07-22 11:54:44 -0700505
robertphillipsd4c741e2016-04-28 09:55:15 -0700506 srcTexture = dstDrawContext->asTexture();
senorblanco@chromium.org8fcad982013-09-17 13:41:43 +0000507 srcRect = dstRect;
senorblanco@chromium.org84207c42012-08-22 20:51:19 +0000508 }
509 if (radius.fHeight > 0) {
robertphillips76948d42016-05-04 12:47:41 -0700510 sk_sp<GrDrawContext> dstDrawContext(context->newDrawContext(SkBackingFit::kApprox,
robertphillipsd4c741e2016-04-28 09:55:15 -0700511 rect.width(), rect.height(),
512 kSkia8888_GrPixelConfig));
robertphillipsff0ca5e2015-07-22 11:54:44 -0700513 if (!dstDrawContext) {
robertphillipsf299e712016-03-25 04:49:22 -0700514 return nullptr;
robertphillipsff0ca5e2015-07-22 11:54:44 -0700515 }
516
robertphillips6c7e3252016-04-27 10:47:51 -0700517 apply_morphology_pass(dstDrawContext.get(), clip, srcTexture.get(),
joshualitt570d2f82015-02-25 13:19:48 -0800518 srcRect, dstRect, radius.fHeight, morphType,
519 Gr1DKernelEffect::kY_Direction);
robertphillipsff0ca5e2015-07-22 11:54:44 -0700520
robertphillipsd4c741e2016-04-28 09:55:15 -0700521 srcTexture = dstDrawContext->asTexture();
senorblanco@chromium.org84207c42012-08-22 20:51:19 +0000522 }
robertphillipsf299e712016-03-25 04:49:22 -0700523
robertphillips3e302272016-04-20 11:48:36 -0700524 return SkSpecialImage::MakeFromGpu(SkIRect::MakeWH(rect.width(), rect.height()),
robertphillipsf299e712016-03-25 04:49:22 -0700525 kNeedNewImageUniqueID_SpecialImage,
robertphillipsc91fd342016-04-25 12:32:54 -0700526 std::move(srcTexture), &input->props());
senorblanco@chromium.org84207c42012-08-22 20:51:19 +0000527}
robertphillipsf299e712016-03-25 04:49:22 -0700528#endif
senorblanco@chromium.org84207c42012-08-22 20:51:19 +0000529
robertphillips40d8d622016-03-30 08:09:56 -0700530sk_sp<SkSpecialImage> SkMorphologyImageFilter::onFilterImage(SkSpecialImage* source,
531 const Context& ctx,
532 SkIPoint* offset) const {
robertphillipsf299e712016-03-25 04:49:22 -0700533 SkIPoint inputOffset = SkIPoint::Make(0, 0);
534 sk_sp<SkSpecialImage> input(this->filterInput(0, source, ctx, &inputOffset));
535 if (!input) {
536 return nullptr;
senorblanco@chromium.orgc2594f42013-01-30 19:08:47 +0000537 }
robertphillipsf299e712016-03-25 04:49:22 -0700538
senorblanco@chromium.orgc2594f42013-01-30 19:08:47 +0000539 SkIRect bounds;
robertphillipsf299e712016-03-25 04:49:22 -0700540 input = this->applyCropRect(this->mapContext(ctx), input.get(), &inputOffset, &bounds);
541 if (!input) {
542 return nullptr;
senorblanco@chromium.org8fcad982013-09-17 13:41:43 +0000543 }
robertphillipsf299e712016-03-25 04:49:22 -0700544
senorblanco@chromium.org2bfe36b2014-01-20 19:58:28 +0000545 SkVector radius = SkVector::Make(SkIntToScalar(this->radius().width()),
546 SkIntToScalar(this->radius().height()));
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +0000547 ctx.ctm().mapVectors(&radius, 1);
senorblanco@chromium.org2bfe36b2014-01-20 19:58:28 +0000548 int width = SkScalarFloorToInt(radius.fX);
549 int height = SkScalarFloorToInt(radius.fY);
senorblanco@chromium.org8fcad982013-09-17 13:41:43 +0000550
551 if (width < 0 || height < 0) {
robertphillipsf299e712016-03-25 04:49:22 -0700552 return nullptr;
senorblanco@chromium.org8fcad982013-09-17 13:41:43 +0000553 }
554
senorblanco@chromium.org2bfe36b2014-01-20 19:58:28 +0000555 SkIRect srcBounds = bounds;
robertphillipsf299e712016-03-25 04:49:22 -0700556 srcBounds.offset(-inputOffset);
557
558 if (0 == width && 0 == height) {
senorblanco@chromium.org6776b822014-01-03 21:48:22 +0000559 offset->fX = bounds.left();
560 offset->fY = bounds.top();
robertphillipsf299e712016-03-25 04:49:22 -0700561 return input->makeSubset(srcBounds);
senorblanco@chromium.org8fcad982013-09-17 13:41:43 +0000562 }
563
robertphillipsf299e712016-03-25 04:49:22 -0700564#if SK_SUPPORT_GPU
robertphillips64612512016-04-08 12:10:42 -0700565 if (source->isTextureBacked()) {
566 GrContext* context = source->getContext();
567
robertphillips40d8d622016-03-30 08:09:56 -0700568 auto type = (kDilate_Op == this->op()) ? GrMorphologyEffect::kDilate_MorphologyType
569 : GrMorphologyEffect::kErode_MorphologyType;
robertphillips64612512016-04-08 12:10:42 -0700570 sk_sp<SkSpecialImage> result(apply_morphology(context, input.get(), srcBounds, type,
robertphillipsf299e712016-03-25 04:49:22 -0700571 SkISize::Make(width, height)));
572 if (result) {
573 offset->fX = bounds.left();
574 offset->fY = bounds.top();
575 }
576 return result;
577 }
578#endif
579
robertphillips64612512016-04-08 12:10:42 -0700580 SkBitmap inputBM;
robertphillipsf299e712016-03-25 04:49:22 -0700581
robertphillips64612512016-04-08 12:10:42 -0700582 if (!input->getROPixels(&inputBM)) {
robertphillipsf299e712016-03-25 04:49:22 -0700583 return nullptr;
584 }
585
robertphillips64612512016-04-08 12:10:42 -0700586 if (inputBM.colorType() != kN32_SkColorType) {
robertphillipsf299e712016-03-25 04:49:22 -0700587 return nullptr;
588 }
589
590 SkImageInfo info = SkImageInfo::Make(bounds.width(), bounds.height(),
robertphillips64612512016-04-08 12:10:42 -0700591 inputBM.colorType(), inputBM.alphaType());
robertphillipsf299e712016-03-25 04:49:22 -0700592
593 SkBitmap dst;
594 if (!dst.tryAllocPixels(info)) {
595 return nullptr;
596 }
597
robertphillips64612512016-04-08 12:10:42 -0700598 SkAutoLockPixels inputLock(inputBM), dstLock(dst);
robertphillipsf299e712016-03-25 04:49:22 -0700599
600 SkMorphologyImageFilter::Proc procX, procY;
601
robertphillips40d8d622016-03-30 08:09:56 -0700602 if (kDilate_Op == this->op()) {
robertphillipsf299e712016-03-25 04:49:22 -0700603 procX = SkOpts::dilate_x;
604 procY = SkOpts::dilate_y;
605 } else {
606 procX = SkOpts::erode_x;
607 procY = SkOpts::erode_y;
608 }
609
610 if (width > 0 && height > 0) {
611 SkBitmap tmp;
612 if (!tmp.tryAllocPixels(info)) {
613 return nullptr;
614 }
615
616 SkAutoLockPixels tmpLock(tmp);
617
robertphillips64612512016-04-08 12:10:42 -0700618 call_proc_X(procX, inputBM, &tmp, width, srcBounds);
robertphillipsf299e712016-03-25 04:49:22 -0700619 SkIRect tmpBounds = SkIRect::MakeWH(srcBounds.width(), srcBounds.height());
620 call_proc_Y(procY,
621 tmp.getAddr32(tmpBounds.left(), tmpBounds.top()), tmp.rowBytesAsPixels(),
622 &dst, height, tmpBounds);
623 } else if (width > 0) {
robertphillips64612512016-04-08 12:10:42 -0700624 call_proc_X(procX, inputBM, &dst, width, srcBounds);
robertphillipsf299e712016-03-25 04:49:22 -0700625 } else if (height > 0) {
626 call_proc_Y(procY,
robertphillips64612512016-04-08 12:10:42 -0700627 inputBM.getAddr32(srcBounds.left(), srcBounds.top()),
628 inputBM.rowBytesAsPixels(),
robertphillipsf299e712016-03-25 04:49:22 -0700629 &dst, height, srcBounds);
senorblanco@chromium.org8fcad982013-09-17 13:41:43 +0000630 }
senorblanco@chromium.org6776b822014-01-03 21:48:22 +0000631 offset->fX = bounds.left();
632 offset->fY = bounds.top();
robertphillipsf299e712016-03-25 04:49:22 -0700633
robertphillips3e302272016-04-20 11:48:36 -0700634 return SkSpecialImage::MakeFromRaster(SkIRect::MakeWH(bounds.width(), bounds.height()),
brianosman898235c2016-04-06 07:38:23 -0700635 dst, &source->props());
senorblanco@chromium.org05054f12012-03-02 21:05:45 +0000636}