blob: a4a720d9686eeaa384628882be7705e38b8ca471 [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"
csmartdalton02fa32c2016-08-19 13:29:27 -070020#include "GrFixedClip.h"
Robert Phillips7f6cd902016-11-10 17:03:43 -050021#include "GrRenderTargetContext.h"
senorblanco@chromium.org302cffb2012-08-01 20:16:34 +000022#include "GrTexture.h"
Robert Phillips7f6cd902016-11-10 17:03:43 -050023#include "GrTextureProxy.h"
24
robertphillips1de87df2016-01-14 06:03:29 -080025#include "SkGr.h"
brianosman2695eaa2016-09-21 06:45:09 -070026#include "SkGrPriv.h"
joshualitteb2a6762014-12-04 11:35:33 -080027#include "effects/Gr1DKernelEffect.h"
Robert Phillipsdbc8eeb2017-02-21 10:04:31 -050028#include "effects/GrProxyMove.h"
egdaniel64c47282015-11-13 06:54:19 -080029#include "glsl/GrGLSLFragmentProcessor.h"
egdaniel2d721d32015-11-11 13:06:05 -080030#include "glsl/GrGLSLFragmentShaderBuilder.h"
egdaniel018fb622015-10-28 07:26:40 -070031#include "glsl/GrGLSLProgramDataManager.h"
egdaniel7ea439b2015-12-03 09:20:44 -080032#include "glsl/GrGLSLUniformHandler.h"
Brian Salomon94efbf52016-11-29 13:43:05 -050033#include "../private/GrGLSL.h"
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +000034#endif
senorblanco@chromium.org05054f12012-03-02 21:05:45 +000035
robertphillips225db442016-04-17 14:27:05 -070036sk_sp<SkImageFilter> SkDilateImageFilter::Make(int radiusX, int radiusY,
37 sk_sp<SkImageFilter> input,
38 const CropRect* cropRect) {
39 if (radiusX < 0 || radiusY < 0) {
40 return nullptr;
41 }
42 return sk_sp<SkImageFilter>(new SkDilateImageFilter(radiusX, radiusY,
43 std::move(input),
44 cropRect));
45}
46
47
48sk_sp<SkImageFilter> SkErodeImageFilter::Make(int radiusX, int radiusY,
49 sk_sp<SkImageFilter> input,
50 const CropRect* cropRect) {
51 if (radiusX < 0 || radiusY < 0) {
52 return nullptr;
53 }
54 return sk_sp<SkImageFilter>(new SkErodeImageFilter(radiusX, radiusY,
55 std::move(input),
56 cropRect));
57}
58
senorblanco@chromium.org2bfe36b2014-01-20 19:58:28 +000059SkMorphologyImageFilter::SkMorphologyImageFilter(int radiusX,
60 int radiusY,
robertphillipsfc11b0a2016-04-05 09:09:36 -070061 sk_sp<SkImageFilter> input,
senorblanco24e06d52015-03-18 12:11:33 -070062 const CropRect* cropRect)
robertphillipsfc11b0a2016-04-05 09:09:36 -070063 : INHERITED(&input, 1, cropRect)
robertphillipsf299e712016-03-25 04:49:22 -070064 , fRadius(SkISize::Make(radiusX, radiusY)) {
senorblanco@chromium.org05054f12012-03-02 21:05:45 +000065}
66
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +000067void SkMorphologyImageFilter::flatten(SkWriteBuffer& buffer) const {
senorblanco@chromium.org05054f12012-03-02 21:05:45 +000068 this->INHERITED::flatten(buffer);
tomhudson@google.com75589252012-04-10 17:42:21 +000069 buffer.writeInt(fRadius.fWidth);
70 buffer.writeInt(fRadius.fHeight);
senorblanco@chromium.org05054f12012-03-02 21:05:45 +000071}
72
robertphillipsf299e712016-03-25 04:49:22 -070073static void call_proc_X(SkMorphologyImageFilter::Proc procX,
robertphillips64612512016-04-08 12:10:42 -070074 const SkBitmap& src, SkBitmap* dst,
robertphillipsf299e712016-03-25 04:49:22 -070075 int radiusX, const SkIRect& bounds) {
robertphillips64612512016-04-08 12:10:42 -070076 procX(src.getAddr32(bounds.left(), bounds.top()), dst->getAddr32(0, 0),
senorblanco@chromium.org0ded88d2014-01-24 15:43:50 +000077 radiusX, bounds.width(), bounds.height(),
78 src.rowBytesAsPixels(), dst->rowBytesAsPixels());
senorblanco@chromium.org05054f12012-03-02 21:05:45 +000079}
80
robertphillipsf299e712016-03-25 04:49:22 -070081static void call_proc_Y(SkMorphologyImageFilter::Proc procY,
82 const SkPMColor* src, int srcRowBytesAsPixels, SkBitmap* dst,
83 int radiusY, const SkIRect& bounds) {
84 procY(src, dst->getAddr32(0, 0),
senorblanco@chromium.org0ded88d2014-01-24 15:43:50 +000085 radiusY, bounds.height(), bounds.width(),
robertphillipsf299e712016-03-25 04:49:22 -070086 srcRowBytesAsPixels, dst->rowBytesAsPixels());
senorblanco@chromium.org05054f12012-03-02 21:05:45 +000087}
88
senorblancoe5e79842016-03-21 14:51:59 -070089SkRect SkMorphologyImageFilter::computeFastBounds(const SkRect& src) const {
90 SkRect bounds = this->getInput(0) ? this->getInput(0)->computeFastBounds(src) : src;
91 bounds.outset(SkIntToScalar(fRadius.width()), SkIntToScalar(fRadius.height()));
92 return bounds;
senorblanco@chromium.org336d1d72014-01-27 21:03:17 +000093}
94
senorblancoe5e79842016-03-21 14:51:59 -070095SkIRect SkMorphologyImageFilter::onFilterNodeBounds(const SkIRect& src, const SkMatrix& ctm,
96 MapDirection) const {
senorblanco@chromium.orgc4b12f12014-02-05 17:51:22 +000097 SkVector radius = SkVector::Make(SkIntToScalar(this->radius().width()),
98 SkIntToScalar(this->radius().height()));
99 ctm.mapVectors(&radius, 1);
senorblancoe5e79842016-03-21 14:51:59 -0700100 return src.makeOutset(SkScalarCeilToInt(radius.x()), SkScalarCeilToInt(radius.y()));
senorblanco@chromium.orgc4b12f12014-02-05 17:51:22 +0000101}
102
reed60c9b582016-04-03 09:11:13 -0700103sk_sp<SkFlattenable> SkErodeImageFilter::CreateProc(SkReadBuffer& buffer) {
reed9fa60da2014-08-21 07:59:51 -0700104 SK_IMAGEFILTER_UNFLATTEN_COMMON(common, 1);
105 const int width = buffer.readInt();
106 const int height = buffer.readInt();
robertphillipsfc11b0a2016-04-05 09:09:36 -0700107 return Make(width, height, common.getInput(0), &common.cropRect());
reed9fa60da2014-08-21 07:59:51 -0700108}
109
reed60c9b582016-04-03 09:11:13 -0700110sk_sp<SkFlattenable> SkDilateImageFilter::CreateProc(SkReadBuffer& buffer) {
reed9fa60da2014-08-21 07:59:51 -0700111 SK_IMAGEFILTER_UNFLATTEN_COMMON(common, 1);
112 const int width = buffer.readInt();
113 const int height = buffer.readInt();
robertphillipsfc11b0a2016-04-05 09:09:36 -0700114 return Make(width, height, common.getInput(0), &common.cropRect());
reed9fa60da2014-08-21 07:59:51 -0700115}
116
robertphillipsf3f5bad2014-12-19 13:49:15 -0800117#ifndef SK_IGNORE_TO_STRING
118void SkErodeImageFilter::toString(SkString* str) const {
119 str->appendf("SkErodeImageFilter: (");
120 str->appendf("radius: (%d,%d)", this->radius().fWidth, this->radius().fHeight);
121 str->append(")");
122}
123#endif
124
125#ifndef SK_IGNORE_TO_STRING
126void SkDilateImageFilter::toString(SkString* str) const {
127 str->appendf("SkDilateImageFilter: (");
128 str->appendf("radius: (%d,%d)", this->radius().fWidth, this->radius().fHeight);
129 str->append(")");
130}
131#endif
132
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000133#if SK_SUPPORT_GPU
senorblanco@chromium.org84207c42012-08-22 20:51:19 +0000134
135///////////////////////////////////////////////////////////////////////////////
senorblanco@chromium.org84207c42012-08-22 20:51:19 +0000136/**
137 * Morphology effects. Depending upon the type of morphology, either the
138 * component-wise min (Erode_Type) or max (Dilate_Type) of all pixels in the
139 * kernel is selected as the new color. The new color is modulated by the input
140 * color.
141 */
142class GrMorphologyEffect : public Gr1DKernelEffect {
senorblanco@chromium.org84207c42012-08-22 20:51:19 +0000143public:
senorblanco@chromium.org84207c42012-08-22 20:51:19 +0000144 enum MorphologyType {
145 kErode_MorphologyType,
146 kDilate_MorphologyType,
147 };
148
bungeman06ca8ec2016-06-09 08:01:03 -0700149 static sk_sp<GrFragmentProcessor> Make(GrTexture* tex, Direction dir, int radius,
150 MorphologyType type) {
151 return sk_sp<GrFragmentProcessor>(new GrMorphologyEffect(tex, dir, radius, type));
bsalomon@google.com0ac6af42013-01-16 15:16:18 +0000152 }
153
bungeman06ca8ec2016-06-09 08:01:03 -0700154 static sk_sp<GrFragmentProcessor> Make(GrTexture* tex, Direction dir, int radius,
Robert Phillips7f6cd902016-11-10 17:03:43 -0500155 MorphologyType type, const float bounds[2]) {
bungeman06ca8ec2016-06-09 08:01:03 -0700156 return sk_sp<GrFragmentProcessor>(new GrMorphologyEffect(tex, dir, radius, type, bounds));
cwallez80a61df2015-01-26 12:20:14 -0800157 }
158
Robert Phillipsdbc8eeb2017-02-21 10:04:31 -0500159 static sk_sp<GrFragmentProcessor> Make(GrContext* context, sk_sp<GrTextureProxy> proxy,
160 Direction dir, int radius, MorphologyType type) {
161 return sk_sp<GrFragmentProcessor>(new GrMorphologyEffect(context, std::move(proxy),
162 dir, radius, type));
163 }
164
165 static sk_sp<GrFragmentProcessor> Make(GrContext* context, sk_sp<GrTextureProxy> proxy,
166 Direction dir, int radius,
167 MorphologyType type, const float bounds[2]) {
168 return sk_sp<GrFragmentProcessor>(new GrMorphologyEffect(context, std::move(proxy),
169 dir, radius, type, bounds));
170 }
171
172 ~GrMorphologyEffect() override;
senorblanco@chromium.org84207c42012-08-22 20:51:19 +0000173
174 MorphologyType type() const { return fType; }
cwallez80a61df2015-01-26 12:20:14 -0800175 bool useRange() const { return fUseRange; }
176 const float* range() const { return fRange; }
senorblanco@chromium.org84207c42012-08-22 20:51:19 +0000177
mtklein36352bf2015-03-25 18:17:31 -0700178 const char* name() const override { return "Morphology"; }
senorblanco@chromium.org84207c42012-08-22 20:51:19 +0000179
senorblanco@chromium.org84207c42012-08-22 20:51:19 +0000180protected:
181
182 MorphologyType fType;
cwallez80a61df2015-01-26 12:20:14 -0800183 bool fUseRange;
184 float fRange[2];
senorblanco@chromium.org84207c42012-08-22 20:51:19 +0000185
186private:
egdaniel57d3b032015-11-13 11:57:27 -0800187 GrGLSLFragmentProcessor* onCreateGLSLInstance() const override;
wangyixb1daa862015-08-18 11:29:31 -0700188
Brian Salomon94efbf52016-11-29 13:43:05 -0500189 void onGetGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder*) const override;
wangyix4b3050b2015-08-04 07:59:37 -0700190
mtklein36352bf2015-03-25 18:17:31 -0700191 bool onIsEqual(const GrFragmentProcessor&) const override;
bsalomon@google.com68b58c92013-01-17 16:50:08 +0000192
bsalomon4a339522015-10-06 08:40:50 -0700193 GrMorphologyEffect(GrTexture*, Direction, int radius, MorphologyType);
Robert Phillips7f6cd902016-11-10 17:03:43 -0500194 GrMorphologyEffect(GrTexture*, Direction, int radius, MorphologyType, const float bounds[2]);
bsalomon@google.com0ac6af42013-01-16 15:16:18 +0000195
Robert Phillipsdbc8eeb2017-02-21 10:04:31 -0500196 GrMorphologyEffect(GrContext*, sk_sp<GrTextureProxy>, Direction, int radius, MorphologyType);
197 GrMorphologyEffect(GrContext*, sk_sp<GrTextureProxy>,
198 Direction, int radius, MorphologyType, const float bounds[2]);
199
joshualittb0a8a372014-09-23 09:50:21 -0700200 GR_DECLARE_FRAGMENT_PROCESSOR_TEST;
senorblanco@chromium.org84207c42012-08-22 20:51:19 +0000201
202 typedef Gr1DKernelEffect INHERITED;
203};
204
205///////////////////////////////////////////////////////////////////////////////
206
egdaniel64c47282015-11-13 06:54:19 -0800207class GrGLMorphologyEffect : public GrGLSLFragmentProcessor {
senorblanco@chromium.org84207c42012-08-22 20:51:19 +0000208public:
robertphillips9cdb9922016-02-03 12:25:40 -0800209 void emitCode(EmitArgs&) override;
senorblanco@chromium.org84207c42012-08-22 20:51:19 +0000210
Brian Salomon94efbf52016-11-29 13:43:05 -0500211 static inline void GenKey(const GrProcessor&, const GrShaderCaps&, GrProcessorKeyBuilder*);
senorblanco@chromium.org84207c42012-08-22 20:51:19 +0000212
wangyixb1daa862015-08-18 11:29:31 -0700213protected:
egdaniel018fb622015-10-28 07:26:40 -0700214 void onSetData(const GrGLSLProgramDataManager&, const GrProcessor&) override;
senorblanco@chromium.org84207c42012-08-22 20:51:19 +0000215
216private:
egdaniel018fb622015-10-28 07:26:40 -0700217 GrGLSLProgramDataManager::UniformHandle fPixelSizeUni;
218 GrGLSLProgramDataManager::UniformHandle fRangeUni;
senorblanco@chromium.org84207c42012-08-22 20:51:19 +0000219
egdaniel64c47282015-11-13 06:54:19 -0800220 typedef GrGLSLFragmentProcessor INHERITED;
senorblanco@chromium.org84207c42012-08-22 20:51:19 +0000221};
222
wangyix7c157a92015-07-22 15:08:53 -0700223void GrGLMorphologyEffect::emitCode(EmitArgs& args) {
robertphillipsbf536af2016-02-04 06:11:53 -0800224 const GrMorphologyEffect& me = args.fFp.cast<GrMorphologyEffect>();
225
egdaniel7ea439b2015-12-03 09:20:44 -0800226 GrGLSLUniformHandler* uniformHandler = args.fUniformHandler;
cdalton5e58cee2016-02-11 12:49:47 -0800227 fPixelSizeUni = uniformHandler->addUniform(kFragment_GrShaderFlag,
egdaniel7ea439b2015-12-03 09:20:44 -0800228 kFloat_GrSLType, kDefault_GrSLPrecision,
229 "PixelSize");
230 const char* pixelSizeInc = uniformHandler->getUniformCStr(fPixelSizeUni);
cdalton5e58cee2016-02-11 12:49:47 -0800231 fRangeUni = uniformHandler->addUniform(kFragment_GrShaderFlag,
egdaniel7ea439b2015-12-03 09:20:44 -0800232 kVec2f_GrSLType, kDefault_GrSLPrecision,
233 "Range");
234 const char* range = uniformHandler->getUniformCStr(fRangeUni);
senorblanco@chromium.org84207c42012-08-22 20:51:19 +0000235
cdalton85285412016-02-18 12:37:07 -0800236 GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder;
bsalomon1a1aa932016-09-12 09:30:36 -0700237 SkString coords2D = fragBuilder->ensureCoords2D(args.fTransformedCoords[0]);
senorblanco@chromium.org84207c42012-08-22 20:51:19 +0000238 const char* func;
robertphillipsbf536af2016-02-04 06:11:53 -0800239 switch (me.type()) {
senorblanco@chromium.org84207c42012-08-22 20:51:19 +0000240 case GrMorphologyEffect::kErode_MorphologyType:
egdaniel4ca2e602015-11-18 08:01:26 -0800241 fragBuilder->codeAppendf("\t\t%s = vec4(1, 1, 1, 1);\n", args.fOutputColor);
senorblanco@chromium.org84207c42012-08-22 20:51:19 +0000242 func = "min";
243 break;
244 case GrMorphologyEffect::kDilate_MorphologyType:
egdaniel4ca2e602015-11-18 08:01:26 -0800245 fragBuilder->codeAppendf("\t\t%s = vec4(0, 0, 0, 0);\n", args.fOutputColor);
senorblanco@chromium.org84207c42012-08-22 20:51:19 +0000246 func = "max";
247 break;
248 default:
commit-bot@chromium.org88cb22b2014-04-30 14:17:00 +0000249 SkFAIL("Unexpected type");
senorblanco@chromium.org84207c42012-08-22 20:51:19 +0000250 func = ""; // suppress warning
251 break;
252 }
senorblanco@chromium.org84207c42012-08-22 20:51:19 +0000253
cwallez80a61df2015-01-26 12:20:14 -0800254 const char* dir;
robertphillipsbf536af2016-02-04 06:11:53 -0800255 switch (me.direction()) {
cwallez80a61df2015-01-26 12:20:14 -0800256 case Gr1DKernelEffect::kX_Direction:
257 dir = "x";
258 break;
259 case Gr1DKernelEffect::kY_Direction:
260 dir = "y";
261 break;
262 default:
263 SkFAIL("Unknown filter direction.");
264 dir = ""; // suppress warning
265 }
266
robertphillipsbf536af2016-02-04 06:11:53 -0800267 int width = GrMorphologyEffect::WidthFromRadius(me.radius());
268
cwallez80a61df2015-01-26 12:20:14 -0800269 // vec2 coord = coord2D;
egdaniel4ca2e602015-11-18 08:01:26 -0800270 fragBuilder->codeAppendf("\t\tvec2 coord = %s;\n", coords2D.c_str());
cwallez80a61df2015-01-26 12:20:14 -0800271 // coord.x -= radius * pixelSize;
robertphillipsbf536af2016-02-04 06:11:53 -0800272 fragBuilder->codeAppendf("\t\tcoord.%s -= %d.0 * %s; \n", dir, me.radius(), pixelSizeInc);
273 if (me.useRange()) {
cwallez80a61df2015-01-26 12:20:14 -0800274 // highBound = min(highBound, coord.x + (width-1) * pixelSize);
egdaniel4ca2e602015-11-18 08:01:26 -0800275 fragBuilder->codeAppendf("\t\tfloat highBound = min(%s.y, coord.%s + %f * %s);",
robertphillipsbf536af2016-02-04 06:11:53 -0800276 range, dir, float(width - 1), pixelSizeInc);
cwallez80a61df2015-01-26 12:20:14 -0800277 // coord.x = max(lowBound, coord.x);
egdaniel4ca2e602015-11-18 08:01:26 -0800278 fragBuilder->codeAppendf("\t\tcoord.%s = max(%s.x, coord.%s);", dir, range, dir);
cwallez80a61df2015-01-26 12:20:14 -0800279 }
robertphillipsbf536af2016-02-04 06:11:53 -0800280 fragBuilder->codeAppendf("\t\tfor (int i = 0; i < %d; i++) {\n", width);
egdaniel4ca2e602015-11-18 08:01:26 -0800281 fragBuilder->codeAppendf("\t\t\t%s = %s(%s, ", args.fOutputColor, func, args.fOutputColor);
cdalton3f6f76f2016-04-11 12:18:09 -0700282 fragBuilder->appendTextureLookup(args.fTexSamplers[0], "coord");
egdaniel4ca2e602015-11-18 08:01:26 -0800283 fragBuilder->codeAppend(");\n");
cwallez80a61df2015-01-26 12:20:14 -0800284 // coord.x += pixelSize;
egdaniel4ca2e602015-11-18 08:01:26 -0800285 fragBuilder->codeAppendf("\t\t\tcoord.%s += %s;\n", dir, pixelSizeInc);
robertphillipsbf536af2016-02-04 06:11:53 -0800286 if (me.useRange()) {
cwallez80a61df2015-01-26 12:20:14 -0800287 // coord.x = min(highBound, coord.x);
egdaniel4ca2e602015-11-18 08:01:26 -0800288 fragBuilder->codeAppendf("\t\t\tcoord.%s = min(highBound, coord.%s);", dir, dir);
cwallez80a61df2015-01-26 12:20:14 -0800289 }
egdaniel4ca2e602015-11-18 08:01:26 -0800290 fragBuilder->codeAppend("\t\t}\n");
bsalomon@google.comf910d3b2013-03-07 17:06:57 +0000291 SkString modulate;
wangyix7c157a92015-07-22 15:08:53 -0700292 GrGLSLMulVarBy4f(&modulate, args.fOutputColor, args.fInputColor);
egdaniel4ca2e602015-11-18 08:01:26 -0800293 fragBuilder->codeAppend(modulate.c_str());
senorblanco@chromium.org84207c42012-08-22 20:51:19 +0000294}
295
joshualittb0a8a372014-09-23 09:50:21 -0700296void GrGLMorphologyEffect::GenKey(const GrProcessor& proc,
Brian Salomon94efbf52016-11-29 13:43:05 -0500297 const GrShaderCaps&, GrProcessorKeyBuilder* b) {
joshualittb0a8a372014-09-23 09:50:21 -0700298 const GrMorphologyEffect& m = proc.cast<GrMorphologyEffect>();
bsalomon63e99f72014-07-21 08:03:14 -0700299 uint32_t key = static_cast<uint32_t>(m.radius());
senorblanco@chromium.org84207c42012-08-22 20:51:19 +0000300 key |= (m.type() << 8);
cwallez80a61df2015-01-26 12:20:14 -0800301 key |= (m.direction() << 9);
robertphillipsbf536af2016-02-04 06:11:53 -0800302 if (m.useRange()) {
303 key |= 1 << 10;
304 }
bsalomon63e99f72014-07-21 08:03:14 -0700305 b->add32(key);
senorblanco@chromium.org84207c42012-08-22 20:51:19 +0000306}
307
egdaniel018fb622015-10-28 07:26:40 -0700308void GrGLMorphologyEffect::onSetData(const GrGLSLProgramDataManager& pdman,
robertphillipsbf536af2016-02-04 06:11:53 -0800309 const GrProcessor& proc) {
cwallez80a61df2015-01-26 12:20:14 -0800310 const GrMorphologyEffect& m = proc.cast<GrMorphologyEffect>();
Brian Salomondb4183d2016-11-17 12:48:40 -0500311 GrTexture& texture = *m.textureSampler(0).texture();
cwallez80a61df2015-01-26 12:20:14 -0800312
313 float pixelSize = 0.0f;
robertphillipsbf536af2016-02-04 06:11:53 -0800314 switch (m.direction()) {
senorblanco@chromium.org84207c42012-08-22 20:51:19 +0000315 case Gr1DKernelEffect::kX_Direction:
cwallez80a61df2015-01-26 12:20:14 -0800316 pixelSize = 1.0f / texture.width();
senorblanco@chromium.org84207c42012-08-22 20:51:19 +0000317 break;
318 case Gr1DKernelEffect::kY_Direction:
cwallez80a61df2015-01-26 12:20:14 -0800319 pixelSize = 1.0f / texture.height();
senorblanco@chromium.org84207c42012-08-22 20:51:19 +0000320 break;
321 default:
commit-bot@chromium.org88cb22b2014-04-30 14:17:00 +0000322 SkFAIL("Unknown filter direction.");
senorblanco@chromium.org84207c42012-08-22 20:51:19 +0000323 }
cwallez80a61df2015-01-26 12:20:14 -0800324 pdman.set1f(fPixelSizeUni, pixelSize);
325
robertphillipsbf536af2016-02-04 06:11:53 -0800326 if (m.useRange()) {
cwallez80a61df2015-01-26 12:20:14 -0800327 const float* range = m.range();
Robert Phillips7f6cd902016-11-10 17:03:43 -0500328 if (Gr1DKernelEffect::kY_Direction == m.direction() &&
329 texture.origin() == kBottomLeft_GrSurfaceOrigin) {
330 pdman.set2f(fRangeUni, 1.0f - (range[1]*pixelSize), 1.0f - (range[0]*pixelSize));
cwallez80a61df2015-01-26 12:20:14 -0800331 } else {
Robert Phillips7f6cd902016-11-10 17:03:43 -0500332 pdman.set2f(fRangeUni, range[0] * pixelSize, range[1] * pixelSize);
cwallez80a61df2015-01-26 12:20:14 -0800333 }
334 }
senorblanco@chromium.org84207c42012-08-22 20:51:19 +0000335}
336
337///////////////////////////////////////////////////////////////////////////////
338
bsalomon4a339522015-10-06 08:40:50 -0700339GrMorphologyEffect::GrMorphologyEffect(GrTexture* texture,
senorblanco@chromium.org84207c42012-08-22 20:51:19 +0000340 Direction direction,
341 int radius,
342 MorphologyType type)
Brian Salomon587e08f2017-01-27 10:59:27 -0500343 : INHERITED(texture, direction, radius, ModulationFlags(texture->config()))
344 , fType(type)
345 , fUseRange(false) {
joshualitteb2a6762014-12-04 11:35:33 -0800346 this->initClassID<GrMorphologyEffect>();
senorblanco@chromium.org84207c42012-08-22 20:51:19 +0000347}
348
bsalomon4a339522015-10-06 08:40:50 -0700349GrMorphologyEffect::GrMorphologyEffect(GrTexture* texture,
cwallez80a61df2015-01-26 12:20:14 -0800350 Direction direction,
351 int radius,
352 MorphologyType type,
Robert Phillips7f6cd902016-11-10 17:03:43 -0500353 const float range[2])
Brian Salomon587e08f2017-01-27 10:59:27 -0500354 : INHERITED(texture, direction, radius, ModulationFlags(texture->config()))
355 , fType(type)
356 , fUseRange(true) {
cwallez80a61df2015-01-26 12:20:14 -0800357 this->initClassID<GrMorphologyEffect>();
358 fRange[0] = range[0];
359 fRange[1] = range[1];
360}
361
Robert Phillipsdbc8eeb2017-02-21 10:04:31 -0500362GrMorphologyEffect::GrMorphologyEffect(GrContext* context,
363 sk_sp<GrTextureProxy> proxy,
364 Direction direction,
365 int radius,
366 MorphologyType type)
367 : INHERITED{context,
368 ModulationFlags(proxy->config()),
369 GR_PROXY_MOVE(proxy),
370 direction, radius}
371 , fType(type)
372 , fUseRange(false) {
373 this->initClassID<GrMorphologyEffect>();
374}
375
376GrMorphologyEffect::GrMorphologyEffect(GrContext* context,
377 sk_sp<GrTextureProxy> proxy,
378 Direction direction,
379 int radius,
380 MorphologyType type,
381 const float range[2])
382 : INHERITED{context,
383 ModulationFlags(proxy->config()),
384 GR_PROXY_MOVE(proxy),
385 direction, radius}
386 , fType(type)
387 , fUseRange(true) {
388 this->initClassID<GrMorphologyEffect>();
389 fRange[0] = range[0];
390 fRange[1] = range[1];
391}
392
senorblanco@chromium.org84207c42012-08-22 20:51:19 +0000393GrMorphologyEffect::~GrMorphologyEffect() {
394}
395
Brian Salomon94efbf52016-11-29 13:43:05 -0500396void GrMorphologyEffect::onGetGLSLProcessorKey(const GrShaderCaps& caps,
egdaniel57d3b032015-11-13 11:57:27 -0800397 GrProcessorKeyBuilder* b) const {
joshualitteb2a6762014-12-04 11:35:33 -0800398 GrGLMorphologyEffect::GenKey(*this, caps, b);
senorblanco@chromium.org84207c42012-08-22 20:51:19 +0000399}
400
egdaniel57d3b032015-11-13 11:57:27 -0800401GrGLSLFragmentProcessor* GrMorphologyEffect::onCreateGLSLInstance() const {
robertphillipsbf536af2016-02-04 06:11:53 -0800402 return new GrGLMorphologyEffect;
joshualitteb2a6762014-12-04 11:35:33 -0800403}
bsalomon0e08fc12014-10-15 08:19:04 -0700404bool GrMorphologyEffect::onIsEqual(const GrFragmentProcessor& sBase) const {
joshualitt49586be2014-09-16 08:21:41 -0700405 const GrMorphologyEffect& s = sBase.cast<GrMorphologyEffect>();
bsalomon420d7e92014-10-16 09:18:09 -0700406 return (this->radius() == s.radius() &&
senorblanco@chromium.org84207c42012-08-22 20:51:19 +0000407 this->direction() == s.direction() &&
cwallez80a61df2015-01-26 12:20:14 -0800408 this->useRange() == s.useRange() &&
senorblanco@chromium.org84207c42012-08-22 20:51:19 +0000409 this->type() == s.type());
410}
411
412///////////////////////////////////////////////////////////////////////////////
413
joshualittb0a8a372014-09-23 09:50:21 -0700414GR_DEFINE_FRAGMENT_PROCESSOR_TEST(GrMorphologyEffect);
senorblanco@chromium.org84207c42012-08-22 20:51:19 +0000415
Hal Canary6f6961e2017-01-31 13:50:44 -0500416#if GR_TEST_UTILS
bungeman06ca8ec2016-06-09 08:01:03 -0700417sk_sp<GrFragmentProcessor> GrMorphologyEffect::TestCreate(GrProcessorTestData* d) {
Robert Phillipsdbc8eeb2017-02-21 10:04:31 -0500418 int texIdx = d->fRandom->nextBool() ? GrProcessorUnitTest::kSkiaPMTextureIdx
419 : GrProcessorUnitTest::kAlphaTextureIdx;
420 sk_sp<GrTextureProxy> proxy = d->textureProxy(texIdx);
421
joshualitt0067ff52015-07-08 14:26:19 -0700422 Direction dir = d->fRandom->nextBool() ? kX_Direction : kY_Direction;
senorblanco@chromium.org84207c42012-08-22 20:51:19 +0000423 static const int kMaxRadius = 10;
joshualitt0067ff52015-07-08 14:26:19 -0700424 int radius = d->fRandom->nextRangeU(1, kMaxRadius);
Robert Phillipsdbc8eeb2017-02-21 10:04:31 -0500425 MorphologyType type = d->fRandom->nextBool() ? GrMorphologyEffect::kErode_MorphologyType
426 : GrMorphologyEffect::kDilate_MorphologyType;
senorblanco@chromium.org84207c42012-08-22 20:51:19 +0000427
Robert Phillipsdbc8eeb2017-02-21 10:04:31 -0500428 return GrMorphologyEffect::Make(d->context(), std::move(proxy), dir, radius, type);
senorblanco@chromium.org84207c42012-08-22 20:51:19 +0000429}
Hal Canary6f6961e2017-01-31 13:50:44 -0500430#endif
senorblanco@chromium.org84207c42012-08-22 20:51:19 +0000431
senorblanco@chromium.org84207c42012-08-22 20:51:19 +0000432
Robert Phillips7f6cd902016-11-10 17:03:43 -0500433static void apply_morphology_rect(GrTextureProvider* provider,
434 GrRenderTargetContext* renderTargetContext,
robertphillipsf299e712016-03-25 04:49:22 -0700435 const GrClip& clip,
Robert Phillips7f6cd902016-11-10 17:03:43 -0500436 GrTextureProxy* textureProxy,
robertphillipsf299e712016-03-25 04:49:22 -0700437 const SkIRect& srcRect,
438 const SkIRect& dstRect,
439 int radius,
440 GrMorphologyEffect::MorphologyType morphType,
Robert Phillips7f6cd902016-11-10 17:03:43 -0500441 const float bounds[2],
robertphillipsf299e712016-03-25 04:49:22 -0700442 Gr1DKernelEffect::Direction direction) {
cwallez80a61df2015-01-26 12:20:14 -0800443 GrPaint paint;
Brian Osman11052242016-10-27 14:47:55 -0400444 paint.setGammaCorrect(renderTargetContext->isGammaCorrect());
Robert Phillipse60ad622016-11-17 10:22:48 -0500445 GrTexture* tex = textureProxy->instantiate(provider);
446 if (!tex) {
447 return;
448 }
449 paint.addColorFragmentProcessor(GrMorphologyEffect::Make(tex, direction, radius, morphType,
bungeman06ca8ec2016-06-09 08:01:03 -0700450 bounds));
reed374772b2016-10-05 17:33:02 -0700451 paint.setPorterDuffXPFactory(SkBlendMode::kSrc);
Brian Salomon82f44312017-01-11 13:42:54 -0500452 renderTargetContext->fillRectToRect(clip, std::move(paint), GrAA::kNo, SkMatrix::I(),
453 SkRect::Make(dstRect), SkRect::Make(srcRect));
cwallez80a61df2015-01-26 12:20:14 -0800454}
455
Robert Phillips7f6cd902016-11-10 17:03:43 -0500456static void apply_morphology_rect_no_bounds(GrTextureProvider* provider,
457 GrRenderTargetContext* renderTargetContext,
robertphillipsf299e712016-03-25 04:49:22 -0700458 const GrClip& clip,
Robert Phillips7f6cd902016-11-10 17:03:43 -0500459 GrTextureProxy* textureProxy,
robertphillipsf299e712016-03-25 04:49:22 -0700460 const SkIRect& srcRect,
461 const SkIRect& dstRect,
462 int radius,
463 GrMorphologyEffect::MorphologyType morphType,
464 Gr1DKernelEffect::Direction direction) {
senorblanco@chromium.org84207c42012-08-22 20:51:19 +0000465 GrPaint paint;
Brian Osman11052242016-10-27 14:47:55 -0400466 paint.setGammaCorrect(renderTargetContext->isGammaCorrect());
Robert Phillipse60ad622016-11-17 10:22:48 -0500467 GrTexture* tex = textureProxy->instantiate(provider);
468 if (!tex) {
469 return;
470 }
471 paint.addColorFragmentProcessor(GrMorphologyEffect::Make(tex, direction, radius, morphType));
reed374772b2016-10-05 17:33:02 -0700472 paint.setPorterDuffXPFactory(SkBlendMode::kSrc);
Brian Salomon82f44312017-01-11 13:42:54 -0500473 renderTargetContext->fillRectToRect(clip, std::move(paint), GrAA::kNo, SkMatrix::I(),
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500474 SkRect::Make(dstRect), SkRect::Make(srcRect));
senorblanco@chromium.org84207c42012-08-22 20:51:19 +0000475}
476
Robert Phillips7f6cd902016-11-10 17:03:43 -0500477static void apply_morphology_pass(GrTextureProvider* provider,
478 GrRenderTargetContext* renderTargetContext,
robertphillipsf299e712016-03-25 04:49:22 -0700479 const GrClip& clip,
Robert Phillips7f6cd902016-11-10 17:03:43 -0500480 GrTextureProxy* textureProxy,
robertphillipsf299e712016-03-25 04:49:22 -0700481 const SkIRect& srcRect,
482 const SkIRect& dstRect,
483 int radius,
484 GrMorphologyEffect::MorphologyType morphType,
485 Gr1DKernelEffect::Direction direction) {
cwallez80a61df2015-01-26 12:20:14 -0800486 float bounds[2] = { 0.0f, 1.0f };
487 SkIRect lowerSrcRect = srcRect, lowerDstRect = dstRect;
488 SkIRect middleSrcRect = srcRect, middleDstRect = dstRect;
489 SkIRect upperSrcRect = srcRect, upperDstRect = dstRect;
490 if (direction == Gr1DKernelEffect::kX_Direction) {
Robert Phillips7f6cd902016-11-10 17:03:43 -0500491 bounds[0] = SkIntToScalar(srcRect.left()) + 0.5f;
492 bounds[1] = SkIntToScalar(srcRect.right()) - 0.5f;
cwallez80a61df2015-01-26 12:20:14 -0800493 lowerSrcRect.fRight = srcRect.left() + radius;
494 lowerDstRect.fRight = dstRect.left() + radius;
495 upperSrcRect.fLeft = srcRect.right() - radius;
496 upperDstRect.fLeft = dstRect.right() - radius;
497 middleSrcRect.inset(radius, 0);
498 middleDstRect.inset(radius, 0);
499 } else {
Robert Phillips7f6cd902016-11-10 17:03:43 -0500500 bounds[0] = SkIntToScalar(srcRect.top()) + 0.5f;
501 bounds[1] = SkIntToScalar(srcRect.bottom()) - 0.5f;
cwallez80a61df2015-01-26 12:20:14 -0800502 lowerSrcRect.fBottom = srcRect.top() + radius;
503 lowerDstRect.fBottom = dstRect.top() + radius;
504 upperSrcRect.fTop = srcRect.bottom() - radius;
505 upperDstRect.fTop = dstRect.bottom() - radius;
506 middleSrcRect.inset(0, radius);
507 middleDstRect.inset(0, radius);
508 }
509 if (middleSrcRect.fLeft - middleSrcRect.fRight >= 0) {
510 // radius covers srcRect; use bounds over entire draw
Robert Phillips7f6cd902016-11-10 17:03:43 -0500511 apply_morphology_rect(provider, renderTargetContext, clip, textureProxy,
512 srcRect, dstRect, radius, morphType, bounds, direction);
cwallez80a61df2015-01-26 12:20:14 -0800513 } else {
514 // Draw upper and lower margins with bounds; middle without.
Robert Phillips7f6cd902016-11-10 17:03:43 -0500515 apply_morphology_rect(provider, renderTargetContext, clip, textureProxy,
516 lowerSrcRect, lowerDstRect, radius, morphType, bounds, direction);
517 apply_morphology_rect(provider, renderTargetContext, clip, textureProxy,
518 upperSrcRect, upperDstRect, radius, morphType, bounds, direction);
519 apply_morphology_rect_no_bounds(provider, renderTargetContext, clip, textureProxy,
520 middleSrcRect, middleDstRect, radius, morphType, direction);
cwallez80a61df2015-01-26 12:20:14 -0800521 }
522}
523
brianosman2a75e5d2016-09-22 07:15:37 -0700524static sk_sp<SkSpecialImage> apply_morphology(
525 GrContext* context,
526 SkSpecialImage* input,
527 const SkIRect& rect,
528 GrMorphologyEffect::MorphologyType morphType,
529 SkISize radius,
530 const SkImageFilter::OutputProperties& outputProperties) {
Robert Phillips8e1c4e62017-02-19 12:27:01 -0500531 sk_sp<GrTextureProxy> srcTexture(input->asTextureProxyRef(context));
bsalomon49f085d2014-09-05 13:34:00 -0700532 SkASSERT(srcTexture);
brianosman2a75e5d2016-09-22 07:15:37 -0700533 sk_sp<SkColorSpace> colorSpace = sk_ref_sp(outputProperties.colorSpace());
brianosman2695eaa2016-09-21 06:45:09 -0700534 GrPixelConfig config = GrRenderableConfigForColorSpace(colorSpace.get());
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +0000535
joshualitt570d2f82015-02-25 13:19:48 -0800536 // setup new clip
cdalton846c0512016-05-13 10:25:00 -0700537 const GrFixedClip clip(SkIRect::MakeWH(srcTexture->width(), srcTexture->height()));
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +0000538
robertphillipsd4c741e2016-04-28 09:55:15 -0700539 const SkIRect dstRect = SkIRect::MakeWH(rect.width(), rect.height());
senorblanco@chromium.org8fcad982013-09-17 13:41:43 +0000540 SkIRect srcRect = rect;
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +0000541
robertphillipsf299e712016-03-25 04:49:22 -0700542 SkASSERT(radius.width() > 0 || radius.height() > 0);
543
senorblanco@chromium.org84207c42012-08-22 20:51:19 +0000544 if (radius.fWidth > 0) {
Robert Phillips7f6cd902016-11-10 17:03:43 -0500545 sk_sp<GrRenderTargetContext> dstRTContext(context->makeDeferredRenderTargetContext(
Brian Osman11052242016-10-27 14:47:55 -0400546 SkBackingFit::kApprox, rect.width(), rect.height(), config, colorSpace));
Robert Phillips7f6cd902016-11-10 17:03:43 -0500547 if (!dstRTContext) {
robertphillipsf299e712016-03-25 04:49:22 -0700548 return nullptr;
robertphillipsff0ca5e2015-07-22 11:54:44 -0700549 }
550
Brian Osman320573f2016-12-21 13:36:02 -0500551 apply_morphology_pass(context->textureProvider(),
Robert Phillips7f6cd902016-11-10 17:03:43 -0500552 dstRTContext.get(), clip, srcTexture.get(),
joshualitt570d2f82015-02-25 13:19:48 -0800553 srcRect, dstRect, radius.fWidth, morphType,
554 Gr1DKernelEffect::kX_Direction);
senorblanco@chromium.org8fcad982013-09-17 13:41:43 +0000555 SkIRect clearRect = SkIRect::MakeXYWH(dstRect.fLeft, dstRect.fBottom,
556 dstRect.width(), radius.fHeight);
robertphillipsd4c741e2016-04-28 09:55:15 -0700557 GrColor clearColor = GrMorphologyEffect::kErode_MorphologyType == morphType
558 ? SK_ColorWHITE
559 : SK_ColorTRANSPARENT;
Robert Phillips7f6cd902016-11-10 17:03:43 -0500560 dstRTContext->clear(&clearRect, clearColor, false);
robertphillipsff0ca5e2015-07-22 11:54:44 -0700561
Robert Phillipsf200a902017-01-30 13:27:37 -0500562 srcTexture = dstRTContext->asTextureProxyRef();
senorblanco@chromium.org8fcad982013-09-17 13:41:43 +0000563 srcRect = dstRect;
senorblanco@chromium.org84207c42012-08-22 20:51:19 +0000564 }
565 if (radius.fHeight > 0) {
Robert Phillips7f6cd902016-11-10 17:03:43 -0500566 sk_sp<GrRenderTargetContext> dstRTContext(context->makeDeferredRenderTargetContext(
Brian Osman11052242016-10-27 14:47:55 -0400567 SkBackingFit::kApprox, rect.width(), rect.height(), config, colorSpace));
Robert Phillips7f6cd902016-11-10 17:03:43 -0500568 if (!dstRTContext) {
robertphillipsf299e712016-03-25 04:49:22 -0700569 return nullptr;
robertphillipsff0ca5e2015-07-22 11:54:44 -0700570 }
571
Robert Phillips7f6cd902016-11-10 17:03:43 -0500572 apply_morphology_pass(context->textureProvider(),
573 dstRTContext.get(), clip, srcTexture.get(),
joshualitt570d2f82015-02-25 13:19:48 -0800574 srcRect, dstRect, radius.fHeight, morphType,
575 Gr1DKernelEffect::kY_Direction);
robertphillipsff0ca5e2015-07-22 11:54:44 -0700576
Robert Phillipsf200a902017-01-30 13:27:37 -0500577 srcTexture = dstRTContext->asTextureProxyRef();
senorblanco@chromium.org84207c42012-08-22 20:51:19 +0000578 }
robertphillipsf299e712016-03-25 04:49:22 -0700579
Robert Phillips7f6cd902016-11-10 17:03:43 -0500580 return SkSpecialImage::MakeDeferredFromGpu(context,
581 SkIRect::MakeWH(rect.width(), rect.height()),
582 kNeedNewImageUniqueID_SpecialImage,
583 std::move(srcTexture), std::move(colorSpace),
584 &input->props());
senorblanco@chromium.org84207c42012-08-22 20:51:19 +0000585}
robertphillipsf299e712016-03-25 04:49:22 -0700586#endif
senorblanco@chromium.org84207c42012-08-22 20:51:19 +0000587
robertphillips40d8d622016-03-30 08:09:56 -0700588sk_sp<SkSpecialImage> SkMorphologyImageFilter::onFilterImage(SkSpecialImage* source,
589 const Context& ctx,
590 SkIPoint* offset) const {
robertphillipsf299e712016-03-25 04:49:22 -0700591 SkIPoint inputOffset = SkIPoint::Make(0, 0);
592 sk_sp<SkSpecialImage> input(this->filterInput(0, source, ctx, &inputOffset));
593 if (!input) {
594 return nullptr;
senorblanco@chromium.orgc2594f42013-01-30 19:08:47 +0000595 }
robertphillipsf299e712016-03-25 04:49:22 -0700596
senorblanco@chromium.orgc2594f42013-01-30 19:08:47 +0000597 SkIRect bounds;
robertphillipsf299e712016-03-25 04:49:22 -0700598 input = this->applyCropRect(this->mapContext(ctx), input.get(), &inputOffset, &bounds);
599 if (!input) {
600 return nullptr;
senorblanco@chromium.org8fcad982013-09-17 13:41:43 +0000601 }
robertphillipsf299e712016-03-25 04:49:22 -0700602
senorblanco@chromium.org2bfe36b2014-01-20 19:58:28 +0000603 SkVector radius = SkVector::Make(SkIntToScalar(this->radius().width()),
604 SkIntToScalar(this->radius().height()));
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +0000605 ctx.ctm().mapVectors(&radius, 1);
senorblanco@chromium.org2bfe36b2014-01-20 19:58:28 +0000606 int width = SkScalarFloorToInt(radius.fX);
607 int height = SkScalarFloorToInt(radius.fY);
senorblanco@chromium.org8fcad982013-09-17 13:41:43 +0000608
609 if (width < 0 || height < 0) {
robertphillipsf299e712016-03-25 04:49:22 -0700610 return nullptr;
senorblanco@chromium.org8fcad982013-09-17 13:41:43 +0000611 }
612
senorblanco@chromium.org2bfe36b2014-01-20 19:58:28 +0000613 SkIRect srcBounds = bounds;
robertphillipsf299e712016-03-25 04:49:22 -0700614 srcBounds.offset(-inputOffset);
615
616 if (0 == width && 0 == height) {
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 return input->makeSubset(srcBounds);
senorblanco@chromium.org8fcad982013-09-17 13:41:43 +0000620 }
621
robertphillipsf299e712016-03-25 04:49:22 -0700622#if SK_SUPPORT_GPU
robertphillips64612512016-04-08 12:10:42 -0700623 if (source->isTextureBacked()) {
624 GrContext* context = source->getContext();
625
Brian Osman615d66d2016-12-29 09:18:20 -0500626 // Ensure the input is in the destination color space. Typically applyCropRect will have
627 // called pad_image to account for our dilation of bounds, so the result will already be
628 // moved to the destination color space. If a filter DAG avoids that, then we use this
629 // fall-back, which saves us from having to do the xform during the filter itself.
630 input = ImageToColorSpace(input.get(), ctx.outputProperties());
Brian Osman320573f2016-12-21 13:36:02 -0500631
robertphillips40d8d622016-03-30 08:09:56 -0700632 auto type = (kDilate_Op == this->op()) ? GrMorphologyEffect::kDilate_MorphologyType
633 : GrMorphologyEffect::kErode_MorphologyType;
robertphillips64612512016-04-08 12:10:42 -0700634 sk_sp<SkSpecialImage> result(apply_morphology(context, input.get(), srcBounds, type,
brianosman2a75e5d2016-09-22 07:15:37 -0700635 SkISize::Make(width, height),
636 ctx.outputProperties()));
robertphillipsf299e712016-03-25 04:49:22 -0700637 if (result) {
638 offset->fX = bounds.left();
639 offset->fY = bounds.top();
640 }
641 return result;
642 }
643#endif
644
robertphillips64612512016-04-08 12:10:42 -0700645 SkBitmap inputBM;
robertphillipsf299e712016-03-25 04:49:22 -0700646
robertphillips64612512016-04-08 12:10:42 -0700647 if (!input->getROPixels(&inputBM)) {
robertphillipsf299e712016-03-25 04:49:22 -0700648 return nullptr;
649 }
650
robertphillips64612512016-04-08 12:10:42 -0700651 if (inputBM.colorType() != kN32_SkColorType) {
robertphillipsf299e712016-03-25 04:49:22 -0700652 return nullptr;
653 }
654
655 SkImageInfo info = SkImageInfo::Make(bounds.width(), bounds.height(),
robertphillips64612512016-04-08 12:10:42 -0700656 inputBM.colorType(), inputBM.alphaType());
robertphillipsf299e712016-03-25 04:49:22 -0700657
658 SkBitmap dst;
659 if (!dst.tryAllocPixels(info)) {
660 return nullptr;
661 }
662
robertphillips64612512016-04-08 12:10:42 -0700663 SkAutoLockPixels inputLock(inputBM), dstLock(dst);
robertphillipsf299e712016-03-25 04:49:22 -0700664
665 SkMorphologyImageFilter::Proc procX, procY;
666
robertphillips40d8d622016-03-30 08:09:56 -0700667 if (kDilate_Op == this->op()) {
robertphillipsf299e712016-03-25 04:49:22 -0700668 procX = SkOpts::dilate_x;
669 procY = SkOpts::dilate_y;
670 } else {
671 procX = SkOpts::erode_x;
672 procY = SkOpts::erode_y;
673 }
674
675 if (width > 0 && height > 0) {
676 SkBitmap tmp;
677 if (!tmp.tryAllocPixels(info)) {
678 return nullptr;
679 }
680
681 SkAutoLockPixels tmpLock(tmp);
682
robertphillips64612512016-04-08 12:10:42 -0700683 call_proc_X(procX, inputBM, &tmp, width, srcBounds);
robertphillipsf299e712016-03-25 04:49:22 -0700684 SkIRect tmpBounds = SkIRect::MakeWH(srcBounds.width(), srcBounds.height());
685 call_proc_Y(procY,
686 tmp.getAddr32(tmpBounds.left(), tmpBounds.top()), tmp.rowBytesAsPixels(),
687 &dst, height, tmpBounds);
688 } else if (width > 0) {
robertphillips64612512016-04-08 12:10:42 -0700689 call_proc_X(procX, inputBM, &dst, width, srcBounds);
robertphillipsf299e712016-03-25 04:49:22 -0700690 } else if (height > 0) {
691 call_proc_Y(procY,
robertphillips64612512016-04-08 12:10:42 -0700692 inputBM.getAddr32(srcBounds.left(), srcBounds.top()),
693 inputBM.rowBytesAsPixels(),
robertphillipsf299e712016-03-25 04:49:22 -0700694 &dst, height, srcBounds);
senorblanco@chromium.org8fcad982013-09-17 13:41:43 +0000695 }
senorblanco@chromium.org6776b822014-01-03 21:48:22 +0000696 offset->fX = bounds.left();
697 offset->fY = bounds.top();
robertphillipsf299e712016-03-25 04:49:22 -0700698
robertphillips3e302272016-04-20 11:48:36 -0700699 return SkSpecialImage::MakeFromRaster(SkIRect::MakeWH(bounds.width(), bounds.height()),
brianosman898235c2016-04-06 07:38:23 -0700700 dst, &source->props());
senorblanco@chromium.org05054f12012-03-02 21:05:45 +0000701}