blob: c01dd975d0641b1fe6d4de06ed6d209415d807ef [file] [log] [blame]
commit-bot@chromium.org40eb3c12014-01-06 23:41:14 +00001/*
2 * Copyright 2013 Google Inc.
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 "SkAlphaThresholdFilter.h"
9#include "SkBitmap.h"
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +000010#include "SkReadBuffer.h"
11#include "SkWriteBuffer.h"
commit-bot@chromium.org40eb3c12014-01-06 23:41:14 +000012#include "SkRegion.h"
13
14class SK_API SkAlphaThresholdFilterImpl : public SkImageFilter {
15public:
senorblanco9ea3d572014-07-08 09:16:22 -070016 SkAlphaThresholdFilterImpl(const SkRegion& region, SkScalar innerThreshold,
17 SkScalar outerThreshold, SkImageFilter* input);
commit-bot@chromium.org40eb3c12014-01-06 23:41:14 +000018
19 SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(SkAlphaThresholdFilterImpl)
20
21protected:
reed9fa60da2014-08-21 07:59:51 -070022#ifdef SK_SUPPORT_LEGACY_DEEPFLATTENING
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +000023 explicit SkAlphaThresholdFilterImpl(SkReadBuffer& buffer);
reed9fa60da2014-08-21 07:59:51 -070024#endif
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +000025 virtual void flatten(SkWriteBuffer&) const SK_OVERRIDE;
commit-bot@chromium.org40eb3c12014-01-06 23:41:14 +000026
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +000027 virtual bool onFilterImage(Proxy*, const SkBitmap& src, const Context&,
commit-bot@chromium.orgae761f72014-02-05 22:32:02 +000028 SkBitmap* result, SkIPoint* offset) const SK_OVERRIDE;
commit-bot@chromium.org40eb3c12014-01-06 23:41:14 +000029#if SK_SUPPORT_GPU
joshualittb0a8a372014-09-23 09:50:21 -070030 virtual bool asFragmentProcessor(GrFragmentProcessor**, GrTexture*, const SkMatrix&,
31 const SkIRect& bounds) const SK_OVERRIDE;
commit-bot@chromium.org40eb3c12014-01-06 23:41:14 +000032#endif
33
34private:
35 SkRegion fRegion;
36 SkScalar fInnerThreshold;
37 SkScalar fOuterThreshold;
38 typedef SkImageFilter INHERITED;
39};
40
41SkImageFilter* SkAlphaThresholdFilter::Create(const SkRegion& region,
42 SkScalar innerThreshold,
senorblanco9ea3d572014-07-08 09:16:22 -070043 SkScalar outerThreshold,
44 SkImageFilter* input) {
45 return SkNEW_ARGS(SkAlphaThresholdFilterImpl, (region, innerThreshold, outerThreshold, input));
commit-bot@chromium.org40eb3c12014-01-06 23:41:14 +000046}
47
48#if SK_SUPPORT_GPU
49#include "GrContext.h"
50#include "GrCoordTransform.h"
bsalomon6251d172014-10-15 10:50:36 -070051#include "GrFragmentProcessor.h"
egdaniel605dd0f2014-11-12 08:35:25 -080052#include "GrInvariantOutput.h"
joshualittb0a8a372014-09-23 09:50:21 -070053#include "gl/GrGLProcessor.h"
joshualitt30ba4362014-08-21 20:18:45 -070054#include "gl/builders/GrGLProgramBuilder.h"
joshualittb0a8a372014-09-23 09:50:21 -070055#include "GrTBackendProcessorFactory.h"
commit-bot@chromium.org40eb3c12014-01-06 23:41:14 +000056#include "GrTextureAccess.h"
57
58#include "SkGr.h"
59
60class GrGLAlphaThresholdEffect;
61
joshualittb0a8a372014-09-23 09:50:21 -070062class AlphaThresholdEffect : public GrFragmentProcessor {
commit-bot@chromium.org40eb3c12014-01-06 23:41:14 +000063
64public:
joshualittb0a8a372014-09-23 09:50:21 -070065 static GrFragmentProcessor* Create(GrTexture* texture,
66 GrTexture* maskTexture,
67 float innerThreshold,
68 float outerThreshold) {
bsalomon55fad7a2014-07-08 07:34:20 -070069 return SkNEW_ARGS(AlphaThresholdEffect, (texture,
70 maskTexture,
71 innerThreshold,
72 outerThreshold));
commit-bot@chromium.org40eb3c12014-01-06 23:41:14 +000073 }
74
75 virtual ~AlphaThresholdEffect() {};
76
77 static const char* Name() { return "Alpha Threshold"; }
78
joshualittb0a8a372014-09-23 09:50:21 -070079 virtual const GrBackendFragmentProcessorFactory& getFactory() const SK_OVERRIDE;
commit-bot@chromium.org40eb3c12014-01-06 23:41:14 +000080 float innerThreshold() const { return fInnerThreshold; }
81 float outerThreshold() const { return fOuterThreshold; }
82
joshualittb0a8a372014-09-23 09:50:21 -070083 typedef GrGLAlphaThresholdEffect GLProcessor;
commit-bot@chromium.org40eb3c12014-01-06 23:41:14 +000084
85private:
86 AlphaThresholdEffect(GrTexture* texture,
87 GrTexture* maskTexture,
88 float innerThreshold,
89 float outerThreshold)
90 : fInnerThreshold(innerThreshold)
91 , fOuterThreshold(outerThreshold)
bsalomon6267f812014-08-29 15:05:53 -070092 , fImageCoordTransform(kLocal_GrCoordSet,
93 GrCoordTransform::MakeDivByTextureWHMatrix(texture), texture)
commit-bot@chromium.org40eb3c12014-01-06 23:41:14 +000094 , fImageTextureAccess(texture)
bsalomon6267f812014-08-29 15:05:53 -070095 , fMaskCoordTransform(kLocal_GrCoordSet,
96 GrCoordTransform::MakeDivByTextureWHMatrix(maskTexture), maskTexture)
commit-bot@chromium.org40eb3c12014-01-06 23:41:14 +000097 , fMaskTextureAccess(maskTexture) {
98 this->addCoordTransform(&fImageCoordTransform);
99 this->addTextureAccess(&fImageTextureAccess);
100 this->addCoordTransform(&fMaskCoordTransform);
101 this->addTextureAccess(&fMaskTextureAccess);
102 }
103
bsalomon0e08fc12014-10-15 08:19:04 -0700104 virtual bool onIsEqual(const GrFragmentProcessor&) const SK_OVERRIDE;
commit-bot@chromium.org40eb3c12014-01-06 23:41:14 +0000105
egdaniel605dd0f2014-11-12 08:35:25 -0800106 virtual void onComputeInvariantOutput(GrInvariantOutput* inout) const SK_OVERRIDE;
egdaniel1a8ecdf2014-10-03 06:24:12 -0700107
joshualittb0a8a372014-09-23 09:50:21 -0700108 GR_DECLARE_FRAGMENT_PROCESSOR_TEST;
commit-bot@chromium.org40eb3c12014-01-06 23:41:14 +0000109
110 float fInnerThreshold;
111 float fOuterThreshold;
112 GrCoordTransform fImageCoordTransform;
113 GrTextureAccess fImageTextureAccess;
114 GrCoordTransform fMaskCoordTransform;
115 GrTextureAccess fMaskTextureAccess;
116
joshualittb0a8a372014-09-23 09:50:21 -0700117 typedef GrFragmentProcessor INHERITED;
commit-bot@chromium.org40eb3c12014-01-06 23:41:14 +0000118};
119
joshualittb0a8a372014-09-23 09:50:21 -0700120class GrGLAlphaThresholdEffect : public GrGLFragmentProcessor {
commit-bot@chromium.org40eb3c12014-01-06 23:41:14 +0000121public:
joshualittb0a8a372014-09-23 09:50:21 -0700122 GrGLAlphaThresholdEffect(const GrBackendProcessorFactory&, const GrProcessor&);
commit-bot@chromium.org40eb3c12014-01-06 23:41:14 +0000123
joshualitt15988992014-10-09 15:04:05 -0700124 virtual void emitCode(GrGLFPBuilder*,
joshualittb0a8a372014-09-23 09:50:21 -0700125 const GrFragmentProcessor&,
126 const GrProcessorKey&,
commit-bot@chromium.org40eb3c12014-01-06 23:41:14 +0000127 const char* outputColor,
128 const char* inputColor,
129 const TransformedCoordsArray&,
130 const TextureSamplerArray&) SK_OVERRIDE;
131
joshualittb0a8a372014-09-23 09:50:21 -0700132 virtual void setData(const GrGLProgramDataManager&, const GrProcessor&) SK_OVERRIDE;
commit-bot@chromium.org40eb3c12014-01-06 23:41:14 +0000133
134private:
135
kkinnunen7510b222014-07-30 00:04:16 -0700136 GrGLProgramDataManager::UniformHandle fInnerThresholdVar;
137 GrGLProgramDataManager::UniformHandle fOuterThresholdVar;
commit-bot@chromium.org40eb3c12014-01-06 23:41:14 +0000138
joshualittb0a8a372014-09-23 09:50:21 -0700139 typedef GrGLFragmentProcessor INHERITED;
commit-bot@chromium.org40eb3c12014-01-06 23:41:14 +0000140};
141
joshualittb0a8a372014-09-23 09:50:21 -0700142GrGLAlphaThresholdEffect::GrGLAlphaThresholdEffect(const GrBackendProcessorFactory& factory,
143 const GrProcessor&)
commit-bot@chromium.org40eb3c12014-01-06 23:41:14 +0000144 : INHERITED(factory) {
145}
146
joshualitt15988992014-10-09 15:04:05 -0700147void GrGLAlphaThresholdEffect::emitCode(GrGLFPBuilder* builder,
joshualittb0a8a372014-09-23 09:50:21 -0700148 const GrFragmentProcessor&,
149 const GrProcessorKey& key,
commit-bot@chromium.org40eb3c12014-01-06 23:41:14 +0000150 const char* outputColor,
151 const char* inputColor,
152 const TransformedCoordsArray& coords,
153 const TextureSamplerArray& samplers) {
commit-bot@chromium.org40eb3c12014-01-06 23:41:14 +0000154 fInnerThresholdVar = builder->addUniform(
joshualitt30ba4362014-08-21 20:18:45 -0700155 GrGLProgramBuilder::kFragment_Visibility,
commit-bot@chromium.org40eb3c12014-01-06 23:41:14 +0000156 kFloat_GrSLType, "inner_threshold");
157 fOuterThresholdVar = builder->addUniform(
joshualitt30ba4362014-08-21 20:18:45 -0700158 GrGLProgramBuilder::kFragment_Visibility,
commit-bot@chromium.org40eb3c12014-01-06 23:41:14 +0000159 kFloat_GrSLType, "outer_threshold");
160
joshualitt15988992014-10-09 15:04:05 -0700161 GrGLFPFragmentBuilder* fsBuilder = builder->getFragmentShaderBuilder();
joshualitt30ba4362014-08-21 20:18:45 -0700162 SkString coords2D = fsBuilder->ensureFSCoords2D(coords, 0);
163 SkString maskCoords2D = fsBuilder->ensureFSCoords2D(coords, 1);
commit-bot@chromium.org40eb3c12014-01-06 23:41:14 +0000164
joshualitt30ba4362014-08-21 20:18:45 -0700165 fsBuilder->codeAppendf("\t\tvec2 coord = %s;\n", coords2D.c_str());
166 fsBuilder->codeAppendf("\t\tvec2 mask_coord = %s;\n", maskCoords2D.c_str());
167 fsBuilder->codeAppend("\t\tvec4 input_color = ");
168 fsBuilder->appendTextureLookup(samplers[0], "coord");
169 fsBuilder->codeAppend(";\n");
170 fsBuilder->codeAppend("\t\tvec4 mask_color = ");
171 fsBuilder->appendTextureLookup(samplers[1], "mask_coord");
172 fsBuilder->codeAppend(";\n");
173
174 fsBuilder->codeAppendf("\t\tfloat inner_thresh = %s;\n",
commit-bot@chromium.org40eb3c12014-01-06 23:41:14 +0000175 builder->getUniformCStr(fInnerThresholdVar));
joshualitt30ba4362014-08-21 20:18:45 -0700176 fsBuilder->codeAppendf("\t\tfloat outer_thresh = %s;\n",
commit-bot@chromium.org40eb3c12014-01-06 23:41:14 +0000177 builder->getUniformCStr(fOuterThresholdVar));
joshualitt30ba4362014-08-21 20:18:45 -0700178 fsBuilder->codeAppend("\t\tfloat mask = mask_color.a;\n");
commit-bot@chromium.org40eb3c12014-01-06 23:41:14 +0000179
joshualitt30ba4362014-08-21 20:18:45 -0700180 fsBuilder->codeAppend("vec4 color = input_color;\n");
181 fsBuilder->codeAppend("\t\tif (mask < 0.5) {\n"
commit-bot@chromium.org40eb3c12014-01-06 23:41:14 +0000182 "\t\t\tif (color.a > outer_thresh) {\n"
183 "\t\t\t\tfloat scale = outer_thresh / color.a;\n"
184 "\t\t\t\tcolor.rgb *= scale;\n"
185 "\t\t\t\tcolor.a = outer_thresh;\n"
186 "\t\t\t}\n"
187 "\t\t} else if (color.a < inner_thresh) {\n"
188 "\t\t\tfloat scale = inner_thresh / max(0.001, color.a);\n"
189 "\t\t\tcolor.rgb *= scale;\n"
190 "\t\t\tcolor.a = inner_thresh;\n"
191 "\t\t}\n");
192
joshualitt30ba4362014-08-21 20:18:45 -0700193 fsBuilder->codeAppendf("%s = %s;\n", outputColor,
commit-bot@chromium.org40eb3c12014-01-06 23:41:14 +0000194 (GrGLSLExpr4(inputColor) * GrGLSLExpr4("color")).c_str());
195}
196
kkinnunen7510b222014-07-30 00:04:16 -0700197void GrGLAlphaThresholdEffect::setData(const GrGLProgramDataManager& pdman,
joshualittb0a8a372014-09-23 09:50:21 -0700198 const GrProcessor& proc) {
199 const AlphaThresholdEffect& alpha_threshold = proc.cast<AlphaThresholdEffect>();
kkinnunen7510b222014-07-30 00:04:16 -0700200 pdman.set1f(fInnerThresholdVar, alpha_threshold.innerThreshold());
201 pdman.set1f(fOuterThresholdVar, alpha_threshold.outerThreshold());
commit-bot@chromium.org40eb3c12014-01-06 23:41:14 +0000202}
203
204/////////////////////////////////////////////////////////////////////
205
joshualittb0a8a372014-09-23 09:50:21 -0700206GR_DEFINE_FRAGMENT_PROCESSOR_TEST(AlphaThresholdEffect);
commit-bot@chromium.org40eb3c12014-01-06 23:41:14 +0000207
joshualittb0a8a372014-09-23 09:50:21 -0700208GrFragmentProcessor* AlphaThresholdEffect::TestCreate(SkRandom* random,
bsalomon83d081a2014-07-08 09:56:10 -0700209 GrContext* context,
210 const GrDrawTargetCaps&,
211 GrTexture** textures) {
joshualittb0a8a372014-09-23 09:50:21 -0700212 GrTexture* bmpTex = textures[GrProcessorUnitTest::kSkiaPMTextureIdx];
213 GrTexture* maskTex = textures[GrProcessorUnitTest::kAlphaTextureIdx];
commit-bot@chromium.org40eb3c12014-01-06 23:41:14 +0000214 float inner_thresh = random->nextUScalar1();
215 float outer_thresh = random->nextUScalar1();
216 return AlphaThresholdEffect::Create(bmpTex, maskTex, inner_thresh, outer_thresh);
217}
218
219///////////////////////////////////////////////////////////////////////////////
220
joshualittb0a8a372014-09-23 09:50:21 -0700221const GrBackendFragmentProcessorFactory& AlphaThresholdEffect::getFactory() const {
222 return GrTBackendFragmentProcessorFactory<AlphaThresholdEffect>::getInstance();
commit-bot@chromium.org40eb3c12014-01-06 23:41:14 +0000223}
224
bsalomon0e08fc12014-10-15 08:19:04 -0700225bool AlphaThresholdEffect::onIsEqual(const GrFragmentProcessor& sBase) const {
joshualitt49586be2014-09-16 08:21:41 -0700226 const AlphaThresholdEffect& s = sBase.cast<AlphaThresholdEffect>();
bsalomon420d7e92014-10-16 09:18:09 -0700227 return (this->fInnerThreshold == s.fInnerThreshold &&
commit-bot@chromium.org40eb3c12014-01-06 23:41:14 +0000228 this->fOuterThreshold == s.fOuterThreshold);
229}
230
egdaniel605dd0f2014-11-12 08:35:25 -0800231void AlphaThresholdEffect::onComputeInvariantOutput(GrInvariantOutput* inout) const {
egdanielf8449ba2014-11-25 10:24:56 -0800232 if (GrPixelConfigIsAlphaOnly(this->texture(0)->config())) {
233 inout->mulByUnknownAlpha();
234 } else if (GrPixelConfigIsOpaque(this->texture(0)->config()) && fOuterThreshold >= 1.f) {
egdanielccb2e382014-10-13 12:53:46 -0700235 inout->mulByUnknownOpaqueColor();
commit-bot@chromium.org40eb3c12014-01-06 23:41:14 +0000236 } else {
egdanielccb2e382014-10-13 12:53:46 -0700237 inout->mulByUnknownColor();
commit-bot@chromium.org40eb3c12014-01-06 23:41:14 +0000238 }
239}
240
241#endif
242
reed9fa60da2014-08-21 07:59:51 -0700243#ifdef SK_SUPPORT_LEGACY_DEEPFLATTENING
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +0000244SkAlphaThresholdFilterImpl::SkAlphaThresholdFilterImpl(SkReadBuffer& buffer)
commit-bot@chromium.org40eb3c12014-01-06 23:41:14 +0000245 : INHERITED(1, buffer) {
246 fInnerThreshold = buffer.readScalar();
247 fOuterThreshold = buffer.readScalar();
248 buffer.readRegion(&fRegion);
249}
reed9fa60da2014-08-21 07:59:51 -0700250#endif
251
252SkFlattenable* SkAlphaThresholdFilterImpl::CreateProc(SkReadBuffer& buffer) {
253 SK_IMAGEFILTER_UNFLATTEN_COMMON(common, 1);
254 SkScalar inner = buffer.readScalar();
255 SkScalar outer = buffer.readScalar();
256 SkRegion rgn;
257 buffer.readRegion(&rgn);
258 return SkAlphaThresholdFilter::Create(rgn, inner, outer, common.getInput(0));
259}
commit-bot@chromium.org40eb3c12014-01-06 23:41:14 +0000260
261SkAlphaThresholdFilterImpl::SkAlphaThresholdFilterImpl(const SkRegion& region,
262 SkScalar innerThreshold,
senorblanco9ea3d572014-07-08 09:16:22 -0700263 SkScalar outerThreshold,
264 SkImageFilter* input)
265 : INHERITED(1, &input)
commit-bot@chromium.org40eb3c12014-01-06 23:41:14 +0000266 , fRegion(region)
267 , fInnerThreshold(innerThreshold)
268 , fOuterThreshold(outerThreshold) {
269}
270
271#if SK_SUPPORT_GPU
joshualittb0a8a372014-09-23 09:50:21 -0700272bool SkAlphaThresholdFilterImpl::asFragmentProcessor(GrFragmentProcessor** fp,
273 GrTexture* texture,
274 const SkMatrix& in_matrix,
275 const SkIRect&) const {
276 if (fp) {
commit-bot@chromium.org40eb3c12014-01-06 23:41:14 +0000277 GrContext* context = texture->getContext();
bsalomonf2703d82014-10-28 14:33:06 -0700278 GrSurfaceDesc maskDesc;
commit-bot@chromium.org40eb3c12014-01-06 23:41:14 +0000279 if (context->isConfigRenderable(kAlpha_8_GrPixelConfig, false)) {
280 maskDesc.fConfig = kAlpha_8_GrPixelConfig;
281 } else {
282 maskDesc.fConfig = kRGBA_8888_GrPixelConfig;
283 }
bsalomonf2703d82014-10-28 14:33:06 -0700284 maskDesc.fFlags = kRenderTarget_GrSurfaceFlag | kNoStencil_GrSurfaceFlag;
commit-bot@chromium.org40eb3c12014-01-06 23:41:14 +0000285 // Add one pixel of border to ensure that clamp mode will be all zeros
286 // the outside.
287 maskDesc.fWidth = texture->width();
288 maskDesc.fHeight = texture->height();
bsalomone3059732014-10-14 11:47:22 -0700289 SkAutoTUnref<GrTexture> maskTexture(
290 context->refScratchTexture(maskDesc, GrContext::kApprox_ScratchTexMatch));
291 if (!maskTexture) {
commit-bot@chromium.org40eb3c12014-01-06 23:41:14 +0000292 return false;
293 }
294
295 {
bsalomone3059732014-10-14 11:47:22 -0700296 GrContext::AutoRenderTarget art(context, maskTexture->asRenderTarget());
commit-bot@chromium.org40eb3c12014-01-06 23:41:14 +0000297 GrPaint grPaint;
298 grPaint.setBlendFunc(kOne_GrBlendCoeff, kZero_GrBlendCoeff);
299 SkRegion::Iterator iter(fRegion);
bsalomon89c62982014-11-03 12:08:42 -0800300 context->clear(NULL, 0x0, true, maskTexture->asRenderTarget());
commit-bot@chromium.org40eb3c12014-01-06 23:41:14 +0000301
302 SkMatrix old_matrix = context->getMatrix();
303 context->setMatrix(in_matrix);
304
305 while (!iter.done()) {
306 SkRect rect = SkRect::Make(iter.rect());
307 context->drawRect(grPaint, rect);
308 iter.next();
309 }
310 context->setMatrix(old_matrix);
311 }
312
joshualittb0a8a372014-09-23 09:50:21 -0700313 *fp = AlphaThresholdEffect::Create(texture,
314 maskTexture,
315 fInnerThreshold,
316 fOuterThreshold);
commit-bot@chromium.org40eb3c12014-01-06 23:41:14 +0000317 }
318 return true;
319}
320#endif
321
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +0000322void SkAlphaThresholdFilterImpl::flatten(SkWriteBuffer& buffer) const {
commit-bot@chromium.org40eb3c12014-01-06 23:41:14 +0000323 this->INHERITED::flatten(buffer);
324 buffer.writeScalar(fInnerThreshold);
325 buffer.writeScalar(fOuterThreshold);
326 buffer.writeRegion(fRegion);
327}
328
329bool SkAlphaThresholdFilterImpl::onFilterImage(Proxy*, const SkBitmap& src,
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +0000330 const Context& ctx, SkBitmap* dst,
commit-bot@chromium.orgae761f72014-02-05 22:32:02 +0000331 SkIPoint* offset) const {
commit-bot@chromium.org28fcae22014-04-11 17:15:40 +0000332 SkASSERT(src.colorType() == kN32_SkColorType);
commit-bot@chromium.org40eb3c12014-01-06 23:41:14 +0000333
commit-bot@chromium.org28fcae22014-04-11 17:15:40 +0000334 if (src.colorType() != kN32_SkColorType) {
commit-bot@chromium.org9109e182014-01-07 16:04:01 +0000335 return false;
commit-bot@chromium.org40eb3c12014-01-06 23:41:14 +0000336 }
337
338 SkMatrix localInverse;
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +0000339 if (!ctx.ctm().invert(&localInverse)) {
commit-bot@chromium.org9109e182014-01-07 16:04:01 +0000340 return false;
commit-bot@chromium.org40eb3c12014-01-06 23:41:14 +0000341 }
342
343 SkAutoLockPixels alp(src);
344 SkASSERT(src.getPixels());
345 if (!src.getPixels() || src.width() <= 0 || src.height() <= 0) {
commit-bot@chromium.org9109e182014-01-07 16:04:01 +0000346 return false;
commit-bot@chromium.org40eb3c12014-01-06 23:41:14 +0000347 }
348
reed84825042014-09-02 12:50:45 -0700349 if (!dst->tryAllocPixels(src.info())) {
commit-bot@chromium.org40eb3c12014-01-06 23:41:14 +0000350 return false;
351 }
352
commit-bot@chromium.org9109e182014-01-07 16:04:01 +0000353 U8CPU innerThreshold = (U8CPU)(fInnerThreshold * 0xFF);
354 U8CPU outerThreshold = (U8CPU)(fOuterThreshold * 0xFF);
commit-bot@chromium.org40eb3c12014-01-06 23:41:14 +0000355 SkColor* sptr = src.getAddr32(0, 0);
356 SkColor* dptr = dst->getAddr32(0, 0);
357 int width = src.width(), height = src.height();
358 for (int y = 0; y < height; ++y) {
359 for (int x = 0; x < width; ++x) {
360 const SkColor& source = sptr[y * width + x];
361 SkColor output_color(source);
362 SkPoint position;
commit-bot@chromium.org9109e182014-01-07 16:04:01 +0000363 localInverse.mapXY((SkScalar)x, (SkScalar)y, &position);
364 if (fRegion.contains((int32_t)position.x(), (int32_t)position.y())) {
commit-bot@chromium.org40eb3c12014-01-06 23:41:14 +0000365 if (SkColorGetA(source) < innerThreshold) {
366 U8CPU alpha = SkColorGetA(source);
367 if (alpha == 0)
368 alpha = 1;
369 float scale = (float)innerThreshold / alpha;
370 output_color = SkColorSetARGB(innerThreshold,
commit-bot@chromium.org9109e182014-01-07 16:04:01 +0000371 (U8CPU)(SkColorGetR(source) * scale),
372 (U8CPU)(SkColorGetG(source) * scale),
373 (U8CPU)(SkColorGetB(source) * scale));
commit-bot@chromium.org40eb3c12014-01-06 23:41:14 +0000374 }
375 } else {
376 if (SkColorGetA(source) > outerThreshold) {
377 float scale = (float)outerThreshold / SkColorGetA(source);
378 output_color = SkColorSetARGB(outerThreshold,
commit-bot@chromium.org9109e182014-01-07 16:04:01 +0000379 (U8CPU)(SkColorGetR(source) * scale),
380 (U8CPU)(SkColorGetG(source) * scale),
381 (U8CPU)(SkColorGetB(source) * scale));
commit-bot@chromium.org40eb3c12014-01-06 23:41:14 +0000382 }
383 }
384 dptr[y * dst->width() + x] = output_color;
385 }
386 }
387
388 return true;
389}