blob: 326f005e36290310c1685e747d05c3e8507e7056 [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"
joshualittb0a8a372014-09-23 09:50:21 -070052#include "gl/GrGLProcessor.h"
joshualitt30ba4362014-08-21 20:18:45 -070053#include "gl/builders/GrGLProgramBuilder.h"
joshualittb0a8a372014-09-23 09:50:21 -070054#include "GrTBackendProcessorFactory.h"
commit-bot@chromium.org40eb3c12014-01-06 23:41:14 +000055#include "GrTextureAccess.h"
56
57#include "SkGr.h"
58
59class GrGLAlphaThresholdEffect;
60
joshualittb0a8a372014-09-23 09:50:21 -070061class AlphaThresholdEffect : public GrFragmentProcessor {
commit-bot@chromium.org40eb3c12014-01-06 23:41:14 +000062
63public:
joshualittb0a8a372014-09-23 09:50:21 -070064 static GrFragmentProcessor* Create(GrTexture* texture,
65 GrTexture* maskTexture,
66 float innerThreshold,
67 float outerThreshold) {
bsalomon55fad7a2014-07-08 07:34:20 -070068 return SkNEW_ARGS(AlphaThresholdEffect, (texture,
69 maskTexture,
70 innerThreshold,
71 outerThreshold));
commit-bot@chromium.org40eb3c12014-01-06 23:41:14 +000072 }
73
74 virtual ~AlphaThresholdEffect() {};
75
76 static const char* Name() { return "Alpha Threshold"; }
77
joshualittb0a8a372014-09-23 09:50:21 -070078 virtual const GrBackendFragmentProcessorFactory& getFactory() const SK_OVERRIDE;
commit-bot@chromium.org40eb3c12014-01-06 23:41:14 +000079 float innerThreshold() const { return fInnerThreshold; }
80 float outerThreshold() const { return fOuterThreshold; }
81
joshualittb0a8a372014-09-23 09:50:21 -070082 typedef GrGLAlphaThresholdEffect GLProcessor;
commit-bot@chromium.org40eb3c12014-01-06 23:41:14 +000083
84private:
85 AlphaThresholdEffect(GrTexture* texture,
86 GrTexture* maskTexture,
87 float innerThreshold,
88 float outerThreshold)
89 : fInnerThreshold(innerThreshold)
90 , fOuterThreshold(outerThreshold)
bsalomon6267f812014-08-29 15:05:53 -070091 , fImageCoordTransform(kLocal_GrCoordSet,
92 GrCoordTransform::MakeDivByTextureWHMatrix(texture), texture)
commit-bot@chromium.org40eb3c12014-01-06 23:41:14 +000093 , fImageTextureAccess(texture)
bsalomon6267f812014-08-29 15:05:53 -070094 , fMaskCoordTransform(kLocal_GrCoordSet,
95 GrCoordTransform::MakeDivByTextureWHMatrix(maskTexture), maskTexture)
commit-bot@chromium.org40eb3c12014-01-06 23:41:14 +000096 , fMaskTextureAccess(maskTexture) {
97 this->addCoordTransform(&fImageCoordTransform);
98 this->addTextureAccess(&fImageTextureAccess);
99 this->addCoordTransform(&fMaskCoordTransform);
100 this->addTextureAccess(&fMaskTextureAccess);
101 }
102
bsalomon0e08fc12014-10-15 08:19:04 -0700103 virtual bool onIsEqual(const GrFragmentProcessor&) const SK_OVERRIDE;
commit-bot@chromium.org40eb3c12014-01-06 23:41:14 +0000104
egdaniel1a8ecdf2014-10-03 06:24:12 -0700105 virtual void onComputeInvariantOutput(InvariantOutput* inout) const SK_OVERRIDE;
106
joshualittb0a8a372014-09-23 09:50:21 -0700107 GR_DECLARE_FRAGMENT_PROCESSOR_TEST;
commit-bot@chromium.org40eb3c12014-01-06 23:41:14 +0000108
109 float fInnerThreshold;
110 float fOuterThreshold;
111 GrCoordTransform fImageCoordTransform;
112 GrTextureAccess fImageTextureAccess;
113 GrCoordTransform fMaskCoordTransform;
114 GrTextureAccess fMaskTextureAccess;
115
joshualittb0a8a372014-09-23 09:50:21 -0700116 typedef GrFragmentProcessor INHERITED;
commit-bot@chromium.org40eb3c12014-01-06 23:41:14 +0000117};
118
joshualittb0a8a372014-09-23 09:50:21 -0700119class GrGLAlphaThresholdEffect : public GrGLFragmentProcessor {
commit-bot@chromium.org40eb3c12014-01-06 23:41:14 +0000120public:
joshualittb0a8a372014-09-23 09:50:21 -0700121 GrGLAlphaThresholdEffect(const GrBackendProcessorFactory&, const GrProcessor&);
commit-bot@chromium.org40eb3c12014-01-06 23:41:14 +0000122
joshualitt15988992014-10-09 15:04:05 -0700123 virtual void emitCode(GrGLFPBuilder*,
joshualittb0a8a372014-09-23 09:50:21 -0700124 const GrFragmentProcessor&,
125 const GrProcessorKey&,
commit-bot@chromium.org40eb3c12014-01-06 23:41:14 +0000126 const char* outputColor,
127 const char* inputColor,
128 const TransformedCoordsArray&,
129 const TextureSamplerArray&) SK_OVERRIDE;
130
joshualittb0a8a372014-09-23 09:50:21 -0700131 virtual void setData(const GrGLProgramDataManager&, const GrProcessor&) SK_OVERRIDE;
commit-bot@chromium.org40eb3c12014-01-06 23:41:14 +0000132
133private:
134
kkinnunen7510b222014-07-30 00:04:16 -0700135 GrGLProgramDataManager::UniformHandle fInnerThresholdVar;
136 GrGLProgramDataManager::UniformHandle fOuterThresholdVar;
commit-bot@chromium.org40eb3c12014-01-06 23:41:14 +0000137
joshualittb0a8a372014-09-23 09:50:21 -0700138 typedef GrGLFragmentProcessor INHERITED;
commit-bot@chromium.org40eb3c12014-01-06 23:41:14 +0000139};
140
joshualittb0a8a372014-09-23 09:50:21 -0700141GrGLAlphaThresholdEffect::GrGLAlphaThresholdEffect(const GrBackendProcessorFactory& factory,
142 const GrProcessor&)
commit-bot@chromium.org40eb3c12014-01-06 23:41:14 +0000143 : INHERITED(factory) {
144}
145
joshualitt15988992014-10-09 15:04:05 -0700146void GrGLAlphaThresholdEffect::emitCode(GrGLFPBuilder* builder,
joshualittb0a8a372014-09-23 09:50:21 -0700147 const GrFragmentProcessor&,
148 const GrProcessorKey& key,
commit-bot@chromium.org40eb3c12014-01-06 23:41:14 +0000149 const char* outputColor,
150 const char* inputColor,
151 const TransformedCoordsArray& coords,
152 const TextureSamplerArray& samplers) {
commit-bot@chromium.org40eb3c12014-01-06 23:41:14 +0000153 fInnerThresholdVar = builder->addUniform(
joshualitt30ba4362014-08-21 20:18:45 -0700154 GrGLProgramBuilder::kFragment_Visibility,
commit-bot@chromium.org40eb3c12014-01-06 23:41:14 +0000155 kFloat_GrSLType, "inner_threshold");
156 fOuterThresholdVar = builder->addUniform(
joshualitt30ba4362014-08-21 20:18:45 -0700157 GrGLProgramBuilder::kFragment_Visibility,
commit-bot@chromium.org40eb3c12014-01-06 23:41:14 +0000158 kFloat_GrSLType, "outer_threshold");
159
joshualitt15988992014-10-09 15:04:05 -0700160 GrGLFPFragmentBuilder* fsBuilder = builder->getFragmentShaderBuilder();
joshualitt30ba4362014-08-21 20:18:45 -0700161 SkString coords2D = fsBuilder->ensureFSCoords2D(coords, 0);
162 SkString maskCoords2D = fsBuilder->ensureFSCoords2D(coords, 1);
commit-bot@chromium.org40eb3c12014-01-06 23:41:14 +0000163
joshualitt30ba4362014-08-21 20:18:45 -0700164 fsBuilder->codeAppendf("\t\tvec2 coord = %s;\n", coords2D.c_str());
165 fsBuilder->codeAppendf("\t\tvec2 mask_coord = %s;\n", maskCoords2D.c_str());
166 fsBuilder->codeAppend("\t\tvec4 input_color = ");
167 fsBuilder->appendTextureLookup(samplers[0], "coord");
168 fsBuilder->codeAppend(";\n");
169 fsBuilder->codeAppend("\t\tvec4 mask_color = ");
170 fsBuilder->appendTextureLookup(samplers[1], "mask_coord");
171 fsBuilder->codeAppend(";\n");
172
173 fsBuilder->codeAppendf("\t\tfloat inner_thresh = %s;\n",
commit-bot@chromium.org40eb3c12014-01-06 23:41:14 +0000174 builder->getUniformCStr(fInnerThresholdVar));
joshualitt30ba4362014-08-21 20:18:45 -0700175 fsBuilder->codeAppendf("\t\tfloat outer_thresh = %s;\n",
commit-bot@chromium.org40eb3c12014-01-06 23:41:14 +0000176 builder->getUniformCStr(fOuterThresholdVar));
joshualitt30ba4362014-08-21 20:18:45 -0700177 fsBuilder->codeAppend("\t\tfloat mask = mask_color.a;\n");
commit-bot@chromium.org40eb3c12014-01-06 23:41:14 +0000178
joshualitt30ba4362014-08-21 20:18:45 -0700179 fsBuilder->codeAppend("vec4 color = input_color;\n");
180 fsBuilder->codeAppend("\t\tif (mask < 0.5) {\n"
commit-bot@chromium.org40eb3c12014-01-06 23:41:14 +0000181 "\t\t\tif (color.a > outer_thresh) {\n"
182 "\t\t\t\tfloat scale = outer_thresh / color.a;\n"
183 "\t\t\t\tcolor.rgb *= scale;\n"
184 "\t\t\t\tcolor.a = outer_thresh;\n"
185 "\t\t\t}\n"
186 "\t\t} else if (color.a < inner_thresh) {\n"
187 "\t\t\tfloat scale = inner_thresh / max(0.001, color.a);\n"
188 "\t\t\tcolor.rgb *= scale;\n"
189 "\t\t\tcolor.a = inner_thresh;\n"
190 "\t\t}\n");
191
joshualitt30ba4362014-08-21 20:18:45 -0700192 fsBuilder->codeAppendf("%s = %s;\n", outputColor,
commit-bot@chromium.org40eb3c12014-01-06 23:41:14 +0000193 (GrGLSLExpr4(inputColor) * GrGLSLExpr4("color")).c_str());
194}
195
kkinnunen7510b222014-07-30 00:04:16 -0700196void GrGLAlphaThresholdEffect::setData(const GrGLProgramDataManager& pdman,
joshualittb0a8a372014-09-23 09:50:21 -0700197 const GrProcessor& proc) {
198 const AlphaThresholdEffect& alpha_threshold = proc.cast<AlphaThresholdEffect>();
kkinnunen7510b222014-07-30 00:04:16 -0700199 pdman.set1f(fInnerThresholdVar, alpha_threshold.innerThreshold());
200 pdman.set1f(fOuterThresholdVar, alpha_threshold.outerThreshold());
commit-bot@chromium.org40eb3c12014-01-06 23:41:14 +0000201}
202
203/////////////////////////////////////////////////////////////////////
204
joshualittb0a8a372014-09-23 09:50:21 -0700205GR_DEFINE_FRAGMENT_PROCESSOR_TEST(AlphaThresholdEffect);
commit-bot@chromium.org40eb3c12014-01-06 23:41:14 +0000206
joshualittb0a8a372014-09-23 09:50:21 -0700207GrFragmentProcessor* AlphaThresholdEffect::TestCreate(SkRandom* random,
bsalomon83d081a2014-07-08 09:56:10 -0700208 GrContext* context,
209 const GrDrawTargetCaps&,
210 GrTexture** textures) {
joshualittb0a8a372014-09-23 09:50:21 -0700211 GrTexture* bmpTex = textures[GrProcessorUnitTest::kSkiaPMTextureIdx];
212 GrTexture* maskTex = textures[GrProcessorUnitTest::kAlphaTextureIdx];
commit-bot@chromium.org40eb3c12014-01-06 23:41:14 +0000213 float inner_thresh = random->nextUScalar1();
214 float outer_thresh = random->nextUScalar1();
215 return AlphaThresholdEffect::Create(bmpTex, maskTex, inner_thresh, outer_thresh);
216}
217
218///////////////////////////////////////////////////////////////////////////////
219
joshualittb0a8a372014-09-23 09:50:21 -0700220const GrBackendFragmentProcessorFactory& AlphaThresholdEffect::getFactory() const {
221 return GrTBackendFragmentProcessorFactory<AlphaThresholdEffect>::getInstance();
commit-bot@chromium.org40eb3c12014-01-06 23:41:14 +0000222}
223
bsalomon0e08fc12014-10-15 08:19:04 -0700224bool AlphaThresholdEffect::onIsEqual(const GrFragmentProcessor& sBase) const {
joshualitt49586be2014-09-16 08:21:41 -0700225 const AlphaThresholdEffect& s = sBase.cast<AlphaThresholdEffect>();
bsalomon420d7e92014-10-16 09:18:09 -0700226 return (this->fInnerThreshold == s.fInnerThreshold &&
commit-bot@chromium.org40eb3c12014-01-06 23:41:14 +0000227 this->fOuterThreshold == s.fOuterThreshold);
228}
229
egdaniel1a8ecdf2014-10-03 06:24:12 -0700230void AlphaThresholdEffect::onComputeInvariantOutput(InvariantOutput* inout) const {
egdanielccb2e382014-10-13 12:53:46 -0700231 if (GrPixelConfigIsOpaque(this->texture(0)->config()) && fOuterThreshold >= 1.f) {
232 inout->mulByUnknownOpaqueColor();
commit-bot@chromium.org40eb3c12014-01-06 23:41:14 +0000233 } else {
egdanielccb2e382014-10-13 12:53:46 -0700234 inout->mulByUnknownColor();
commit-bot@chromium.org40eb3c12014-01-06 23:41:14 +0000235 }
236}
237
238#endif
239
reed9fa60da2014-08-21 07:59:51 -0700240#ifdef SK_SUPPORT_LEGACY_DEEPFLATTENING
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +0000241SkAlphaThresholdFilterImpl::SkAlphaThresholdFilterImpl(SkReadBuffer& buffer)
commit-bot@chromium.org40eb3c12014-01-06 23:41:14 +0000242 : INHERITED(1, buffer) {
243 fInnerThreshold = buffer.readScalar();
244 fOuterThreshold = buffer.readScalar();
245 buffer.readRegion(&fRegion);
246}
reed9fa60da2014-08-21 07:59:51 -0700247#endif
248
249SkFlattenable* SkAlphaThresholdFilterImpl::CreateProc(SkReadBuffer& buffer) {
250 SK_IMAGEFILTER_UNFLATTEN_COMMON(common, 1);
251 SkScalar inner = buffer.readScalar();
252 SkScalar outer = buffer.readScalar();
253 SkRegion rgn;
254 buffer.readRegion(&rgn);
255 return SkAlphaThresholdFilter::Create(rgn, inner, outer, common.getInput(0));
256}
commit-bot@chromium.org40eb3c12014-01-06 23:41:14 +0000257
258SkAlphaThresholdFilterImpl::SkAlphaThresholdFilterImpl(const SkRegion& region,
259 SkScalar innerThreshold,
senorblanco9ea3d572014-07-08 09:16:22 -0700260 SkScalar outerThreshold,
261 SkImageFilter* input)
262 : INHERITED(1, &input)
commit-bot@chromium.org40eb3c12014-01-06 23:41:14 +0000263 , fRegion(region)
264 , fInnerThreshold(innerThreshold)
265 , fOuterThreshold(outerThreshold) {
266}
267
268#if SK_SUPPORT_GPU
joshualittb0a8a372014-09-23 09:50:21 -0700269bool SkAlphaThresholdFilterImpl::asFragmentProcessor(GrFragmentProcessor** fp,
270 GrTexture* texture,
271 const SkMatrix& in_matrix,
272 const SkIRect&) const {
273 if (fp) {
commit-bot@chromium.org40eb3c12014-01-06 23:41:14 +0000274 GrContext* context = texture->getContext();
bsalomonf2703d82014-10-28 14:33:06 -0700275 GrSurfaceDesc maskDesc;
commit-bot@chromium.org40eb3c12014-01-06 23:41:14 +0000276 if (context->isConfigRenderable(kAlpha_8_GrPixelConfig, false)) {
277 maskDesc.fConfig = kAlpha_8_GrPixelConfig;
278 } else {
279 maskDesc.fConfig = kRGBA_8888_GrPixelConfig;
280 }
bsalomonf2703d82014-10-28 14:33:06 -0700281 maskDesc.fFlags = kRenderTarget_GrSurfaceFlag | kNoStencil_GrSurfaceFlag;
commit-bot@chromium.org40eb3c12014-01-06 23:41:14 +0000282 // Add one pixel of border to ensure that clamp mode will be all zeros
283 // the outside.
284 maskDesc.fWidth = texture->width();
285 maskDesc.fHeight = texture->height();
bsalomone3059732014-10-14 11:47:22 -0700286 SkAutoTUnref<GrTexture> maskTexture(
287 context->refScratchTexture(maskDesc, GrContext::kApprox_ScratchTexMatch));
288 if (!maskTexture) {
commit-bot@chromium.org40eb3c12014-01-06 23:41:14 +0000289 return false;
290 }
291
292 {
bsalomone3059732014-10-14 11:47:22 -0700293 GrContext::AutoRenderTarget art(context, maskTexture->asRenderTarget());
commit-bot@chromium.org40eb3c12014-01-06 23:41:14 +0000294 GrPaint grPaint;
295 grPaint.setBlendFunc(kOne_GrBlendCoeff, kZero_GrBlendCoeff);
296 SkRegion::Iterator iter(fRegion);
297 context->clear(NULL, 0x0, true);
298
299 SkMatrix old_matrix = context->getMatrix();
300 context->setMatrix(in_matrix);
301
302 while (!iter.done()) {
303 SkRect rect = SkRect::Make(iter.rect());
304 context->drawRect(grPaint, rect);
305 iter.next();
306 }
307 context->setMatrix(old_matrix);
308 }
309
joshualittb0a8a372014-09-23 09:50:21 -0700310 *fp = AlphaThresholdEffect::Create(texture,
311 maskTexture,
312 fInnerThreshold,
313 fOuterThreshold);
commit-bot@chromium.org40eb3c12014-01-06 23:41:14 +0000314 }
315 return true;
316}
317#endif
318
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +0000319void SkAlphaThresholdFilterImpl::flatten(SkWriteBuffer& buffer) const {
commit-bot@chromium.org40eb3c12014-01-06 23:41:14 +0000320 this->INHERITED::flatten(buffer);
321 buffer.writeScalar(fInnerThreshold);
322 buffer.writeScalar(fOuterThreshold);
323 buffer.writeRegion(fRegion);
324}
325
326bool SkAlphaThresholdFilterImpl::onFilterImage(Proxy*, const SkBitmap& src,
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +0000327 const Context& ctx, SkBitmap* dst,
commit-bot@chromium.orgae761f72014-02-05 22:32:02 +0000328 SkIPoint* offset) const {
commit-bot@chromium.org28fcae22014-04-11 17:15:40 +0000329 SkASSERT(src.colorType() == kN32_SkColorType);
commit-bot@chromium.org40eb3c12014-01-06 23:41:14 +0000330
commit-bot@chromium.org28fcae22014-04-11 17:15:40 +0000331 if (src.colorType() != kN32_SkColorType) {
commit-bot@chromium.org9109e182014-01-07 16:04:01 +0000332 return false;
commit-bot@chromium.org40eb3c12014-01-06 23:41:14 +0000333 }
334
335 SkMatrix localInverse;
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +0000336 if (!ctx.ctm().invert(&localInverse)) {
commit-bot@chromium.org9109e182014-01-07 16:04:01 +0000337 return false;
commit-bot@chromium.org40eb3c12014-01-06 23:41:14 +0000338 }
339
340 SkAutoLockPixels alp(src);
341 SkASSERT(src.getPixels());
342 if (!src.getPixels() || src.width() <= 0 || src.height() <= 0) {
commit-bot@chromium.org9109e182014-01-07 16:04:01 +0000343 return false;
commit-bot@chromium.org40eb3c12014-01-06 23:41:14 +0000344 }
345
reed84825042014-09-02 12:50:45 -0700346 if (!dst->tryAllocPixels(src.info())) {
commit-bot@chromium.org40eb3c12014-01-06 23:41:14 +0000347 return false;
348 }
349
commit-bot@chromium.org9109e182014-01-07 16:04:01 +0000350 U8CPU innerThreshold = (U8CPU)(fInnerThreshold * 0xFF);
351 U8CPU outerThreshold = (U8CPU)(fOuterThreshold * 0xFF);
commit-bot@chromium.org40eb3c12014-01-06 23:41:14 +0000352 SkColor* sptr = src.getAddr32(0, 0);
353 SkColor* dptr = dst->getAddr32(0, 0);
354 int width = src.width(), height = src.height();
355 for (int y = 0; y < height; ++y) {
356 for (int x = 0; x < width; ++x) {
357 const SkColor& source = sptr[y * width + x];
358 SkColor output_color(source);
359 SkPoint position;
commit-bot@chromium.org9109e182014-01-07 16:04:01 +0000360 localInverse.mapXY((SkScalar)x, (SkScalar)y, &position);
361 if (fRegion.contains((int32_t)position.x(), (int32_t)position.y())) {
commit-bot@chromium.org40eb3c12014-01-06 23:41:14 +0000362 if (SkColorGetA(source) < innerThreshold) {
363 U8CPU alpha = SkColorGetA(source);
364 if (alpha == 0)
365 alpha = 1;
366 float scale = (float)innerThreshold / alpha;
367 output_color = SkColorSetARGB(innerThreshold,
commit-bot@chromium.org9109e182014-01-07 16:04:01 +0000368 (U8CPU)(SkColorGetR(source) * scale),
369 (U8CPU)(SkColorGetG(source) * scale),
370 (U8CPU)(SkColorGetB(source) * scale));
commit-bot@chromium.org40eb3c12014-01-06 23:41:14 +0000371 }
372 } else {
373 if (SkColorGetA(source) > outerThreshold) {
374 float scale = (float)outerThreshold / SkColorGetA(source);
375 output_color = SkColorSetARGB(outerThreshold,
commit-bot@chromium.org9109e182014-01-07 16:04:01 +0000376 (U8CPU)(SkColorGetR(source) * scale),
377 (U8CPU)(SkColorGetG(source) * scale),
378 (U8CPU)(SkColorGetB(source) * scale));
commit-bot@chromium.org40eb3c12014-01-06 23:41:14 +0000379 }
380 }
381 dptr[y * dst->width() + x] = output_color;
382 }
383 }
384
385 return true;
386}