blob: 9c59347f73dc063b8e3dfda60a20e6c2cb765e0c [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
bsalomon97b9ab72014-07-08 06:52:35 -070030 virtual bool asNewEffect(GrEffect** effect, GrTexture* texture,
commit-bot@chromium.org40eb3c12014-01-06 23:41:14 +000031 const SkMatrix& matrix, const SkIRect& bounds) const SK_OVERRIDE;
32#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"
51#include "GrEffect.h"
52#include "gl/GrGLEffect.h"
bsalomon848faf02014-07-11 10:01:02 -070053#include "gl/GrGLShaderBuilder.h"
commit-bot@chromium.org40eb3c12014-01-06 23:41:14 +000054#include "GrTBackendEffectFactory.h"
55#include "GrTextureAccess.h"
56
57#include "SkGr.h"
58
59class GrGLAlphaThresholdEffect;
60
61class AlphaThresholdEffect : public GrEffect {
62
63public:
bsalomon83d081a2014-07-08 09:56:10 -070064 static GrEffect* 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
78 virtual const GrBackendEffectFactory& getFactory() const SK_OVERRIDE;
79 virtual void getConstantColorComponents(GrColor* color, uint32_t* validFlags) const SK_OVERRIDE;
80
81 float innerThreshold() const { return fInnerThreshold; }
82 float outerThreshold() const { return fOuterThreshold; }
83
84 typedef GrGLAlphaThresholdEffect GLEffect;
85
86private:
87 AlphaThresholdEffect(GrTexture* texture,
88 GrTexture* maskTexture,
89 float innerThreshold,
90 float outerThreshold)
91 : fInnerThreshold(innerThreshold)
92 , fOuterThreshold(outerThreshold)
93 , fImageCoordTransform(kLocal_GrCoordSet, MakeDivByTextureWHMatrix(texture), texture)
94 , fImageTextureAccess(texture)
95 , fMaskCoordTransform(kLocal_GrCoordSet, MakeDivByTextureWHMatrix(maskTexture), maskTexture)
96 , fMaskTextureAccess(maskTexture) {
97 this->addCoordTransform(&fImageCoordTransform);
98 this->addTextureAccess(&fImageTextureAccess);
99 this->addCoordTransform(&fMaskCoordTransform);
100 this->addTextureAccess(&fMaskTextureAccess);
101 }
102
103 virtual bool onIsEqual(const GrEffect&) const SK_OVERRIDE;
104
105 GR_DECLARE_EFFECT_TEST;
106
107 float fInnerThreshold;
108 float fOuterThreshold;
109 GrCoordTransform fImageCoordTransform;
110 GrTextureAccess fImageTextureAccess;
111 GrCoordTransform fMaskCoordTransform;
112 GrTextureAccess fMaskTextureAccess;
113
114 typedef GrEffect INHERITED;
115};
116
117class GrGLAlphaThresholdEffect : public GrGLEffect {
118public:
119 GrGLAlphaThresholdEffect(const GrBackendEffectFactory&, const GrDrawEffect&);
120
121 virtual void emitCode(GrGLShaderBuilder*,
122 const GrDrawEffect&,
bsalomon63e99f72014-07-21 08:03:14 -0700123 const GrEffectKey&,
commit-bot@chromium.org40eb3c12014-01-06 23:41:14 +0000124 const char* outputColor,
125 const char* inputColor,
126 const TransformedCoordsArray&,
127 const TextureSamplerArray&) SK_OVERRIDE;
128
kkinnunen7510b222014-07-30 00:04:16 -0700129 virtual void setData(const GrGLProgramDataManager&, const GrDrawEffect&) SK_OVERRIDE;
commit-bot@chromium.org40eb3c12014-01-06 23:41:14 +0000130
131private:
132
kkinnunen7510b222014-07-30 00:04:16 -0700133 GrGLProgramDataManager::UniformHandle fInnerThresholdVar;
134 GrGLProgramDataManager::UniformHandle fOuterThresholdVar;
commit-bot@chromium.org40eb3c12014-01-06 23:41:14 +0000135
136 typedef GrGLEffect INHERITED;
137};
138
139GrGLAlphaThresholdEffect::GrGLAlphaThresholdEffect(const GrBackendEffectFactory& factory, const GrDrawEffect&)
140 : INHERITED(factory) {
141}
142
143void GrGLAlphaThresholdEffect::emitCode(GrGLShaderBuilder* builder,
144 const GrDrawEffect&,
bsalomon63e99f72014-07-21 08:03:14 -0700145 const GrEffectKey& key,
commit-bot@chromium.org40eb3c12014-01-06 23:41:14 +0000146 const char* outputColor,
147 const char* inputColor,
148 const TransformedCoordsArray& coords,
149 const TextureSamplerArray& samplers) {
150 SkString coords2D = builder->ensureFSCoords2D(coords, 0);
151 SkString maskCoords2D = builder->ensureFSCoords2D(coords, 1);
152 fInnerThresholdVar = builder->addUniform(
153 GrGLShaderBuilder::kFragment_Visibility,
154 kFloat_GrSLType, "inner_threshold");
155 fOuterThresholdVar = builder->addUniform(
156 GrGLShaderBuilder::kFragment_Visibility,
157 kFloat_GrSLType, "outer_threshold");
158
159 builder->fsCodeAppendf("\t\tvec2 coord = %s;\n", coords2D.c_str());
160 builder->fsCodeAppendf("\t\tvec2 mask_coord = %s;\n", maskCoords2D.c_str());
161 builder->fsCodeAppend("\t\tvec4 input_color = ");
162 builder->fsAppendTextureLookup(samplers[0], "coord");
163 builder->fsCodeAppend(";\n");
164 builder->fsCodeAppend("\t\tvec4 mask_color = ");
165 builder->fsAppendTextureLookup(samplers[1], "mask_coord");
166 builder->fsCodeAppend(";\n");
167
168 builder->fsCodeAppendf("\t\tfloat inner_thresh = %s;\n",
169 builder->getUniformCStr(fInnerThresholdVar));
170 builder->fsCodeAppendf("\t\tfloat outer_thresh = %s;\n",
171 builder->getUniformCStr(fOuterThresholdVar));
172 builder->fsCodeAppend("\t\tfloat mask = mask_color.a;\n");
173
174 builder->fsCodeAppend("vec4 color = input_color;\n");
175 builder->fsCodeAppend("\t\tif (mask < 0.5) {\n"
176 "\t\t\tif (color.a > outer_thresh) {\n"
177 "\t\t\t\tfloat scale = outer_thresh / color.a;\n"
178 "\t\t\t\tcolor.rgb *= scale;\n"
179 "\t\t\t\tcolor.a = outer_thresh;\n"
180 "\t\t\t}\n"
181 "\t\t} else if (color.a < inner_thresh) {\n"
182 "\t\t\tfloat scale = inner_thresh / max(0.001, color.a);\n"
183 "\t\t\tcolor.rgb *= scale;\n"
184 "\t\t\tcolor.a = inner_thresh;\n"
185 "\t\t}\n");
186
187 builder->fsCodeAppendf("%s = %s;\n", outputColor,
188 (GrGLSLExpr4(inputColor) * GrGLSLExpr4("color")).c_str());
189}
190
kkinnunen7510b222014-07-30 00:04:16 -0700191void GrGLAlphaThresholdEffect::setData(const GrGLProgramDataManager& pdman,
commit-bot@chromium.org40eb3c12014-01-06 23:41:14 +0000192 const GrDrawEffect& drawEffect) {
193 const AlphaThresholdEffect& alpha_threshold =
194 drawEffect.castEffect<AlphaThresholdEffect>();
kkinnunen7510b222014-07-30 00:04:16 -0700195 pdman.set1f(fInnerThresholdVar, alpha_threshold.innerThreshold());
196 pdman.set1f(fOuterThresholdVar, alpha_threshold.outerThreshold());
commit-bot@chromium.org40eb3c12014-01-06 23:41:14 +0000197}
198
199/////////////////////////////////////////////////////////////////////
200
201GR_DEFINE_EFFECT_TEST(AlphaThresholdEffect);
202
bsalomon83d081a2014-07-08 09:56:10 -0700203GrEffect* AlphaThresholdEffect::TestCreate(SkRandom* random,
204 GrContext* context,
205 const GrDrawTargetCaps&,
206 GrTexture** textures) {
commit-bot@chromium.org40eb3c12014-01-06 23:41:14 +0000207 GrTexture* bmpTex = textures[GrEffectUnitTest::kSkiaPMTextureIdx];
208 GrTexture* maskTex = textures[GrEffectUnitTest::kAlphaTextureIdx];
209 float inner_thresh = random->nextUScalar1();
210 float outer_thresh = random->nextUScalar1();
211 return AlphaThresholdEffect::Create(bmpTex, maskTex, inner_thresh, outer_thresh);
212}
213
214///////////////////////////////////////////////////////////////////////////////
215
216const GrBackendEffectFactory& AlphaThresholdEffect::getFactory() const {
217 return GrTBackendEffectFactory<AlphaThresholdEffect>::getInstance();
218}
219
220bool AlphaThresholdEffect::onIsEqual(const GrEffect& sBase) const {
221 const AlphaThresholdEffect& s = CastEffect<AlphaThresholdEffect>(sBase);
222 return (this->texture(0) == s.texture(0) &&
223 this->fInnerThreshold == s.fInnerThreshold &&
224 this->fOuterThreshold == s.fOuterThreshold);
225}
226
227void AlphaThresholdEffect::getConstantColorComponents(GrColor* color, uint32_t* validFlags) const {
228 if ((*validFlags & kA_GrColorComponentFlag) && 0xFF == GrColorUnpackA(*color) &&
229 GrPixelConfigIsOpaque(this->texture(0)->config())) {
230 *validFlags = kA_GrColorComponentFlag;
231 } else {
232 *validFlags = 0;
233 }
234}
235
236#endif
237
reed9fa60da2014-08-21 07:59:51 -0700238#ifdef SK_SUPPORT_LEGACY_DEEPFLATTENING
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +0000239SkAlphaThresholdFilterImpl::SkAlphaThresholdFilterImpl(SkReadBuffer& buffer)
commit-bot@chromium.org40eb3c12014-01-06 23:41:14 +0000240 : INHERITED(1, buffer) {
241 fInnerThreshold = buffer.readScalar();
242 fOuterThreshold = buffer.readScalar();
243 buffer.readRegion(&fRegion);
244}
reed9fa60da2014-08-21 07:59:51 -0700245#endif
246
247SkFlattenable* SkAlphaThresholdFilterImpl::CreateProc(SkReadBuffer& buffer) {
248 SK_IMAGEFILTER_UNFLATTEN_COMMON(common, 1);
249 SkScalar inner = buffer.readScalar();
250 SkScalar outer = buffer.readScalar();
251 SkRegion rgn;
252 buffer.readRegion(&rgn);
253 return SkAlphaThresholdFilter::Create(rgn, inner, outer, common.getInput(0));
254}
commit-bot@chromium.org40eb3c12014-01-06 23:41:14 +0000255
256SkAlphaThresholdFilterImpl::SkAlphaThresholdFilterImpl(const SkRegion& region,
257 SkScalar innerThreshold,
senorblanco9ea3d572014-07-08 09:16:22 -0700258 SkScalar outerThreshold,
259 SkImageFilter* input)
260 : INHERITED(1, &input)
commit-bot@chromium.org40eb3c12014-01-06 23:41:14 +0000261 , fRegion(region)
262 , fInnerThreshold(innerThreshold)
263 , fOuterThreshold(outerThreshold) {
264}
265
266#if SK_SUPPORT_GPU
bsalomon97b9ab72014-07-08 06:52:35 -0700267bool SkAlphaThresholdFilterImpl::asNewEffect(GrEffect** effect, GrTexture* texture,
commit-bot@chromium.org40eb3c12014-01-06 23:41:14 +0000268 const SkMatrix& in_matrix, const SkIRect&) const {
269 if (effect) {
270 GrContext* context = texture->getContext();
271 GrTextureDesc maskDesc;
272 if (context->isConfigRenderable(kAlpha_8_GrPixelConfig, false)) {
273 maskDesc.fConfig = kAlpha_8_GrPixelConfig;
274 } else {
275 maskDesc.fConfig = kRGBA_8888_GrPixelConfig;
276 }
277 maskDesc.fFlags = kRenderTarget_GrTextureFlagBit | kNoStencil_GrTextureFlagBit;
278 // Add one pixel of border to ensure that clamp mode will be all zeros
279 // the outside.
280 maskDesc.fWidth = texture->width();
281 maskDesc.fHeight = texture->height();
282 GrAutoScratchTexture ast(context, maskDesc, GrContext::kApprox_ScratchTexMatch);
283 GrTexture* maskTexture = ast.texture();
284 if (NULL == maskTexture) {
285 return false;
286 }
287
288 {
289 GrContext::AutoRenderTarget art(context, ast.texture()->asRenderTarget());
290 GrPaint grPaint;
291 grPaint.setBlendFunc(kOne_GrBlendCoeff, kZero_GrBlendCoeff);
292 SkRegion::Iterator iter(fRegion);
293 context->clear(NULL, 0x0, true);
294
295 SkMatrix old_matrix = context->getMatrix();
296 context->setMatrix(in_matrix);
297
298 while (!iter.done()) {
299 SkRect rect = SkRect::Make(iter.rect());
300 context->drawRect(grPaint, rect);
301 iter.next();
302 }
303 context->setMatrix(old_matrix);
304 }
305
306 *effect = AlphaThresholdEffect::Create(texture,
307 maskTexture,
308 fInnerThreshold,
309 fOuterThreshold);
310 }
311 return true;
312}
313#endif
314
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +0000315void SkAlphaThresholdFilterImpl::flatten(SkWriteBuffer& buffer) const {
commit-bot@chromium.org40eb3c12014-01-06 23:41:14 +0000316 this->INHERITED::flatten(buffer);
317 buffer.writeScalar(fInnerThreshold);
318 buffer.writeScalar(fOuterThreshold);
319 buffer.writeRegion(fRegion);
320}
321
322bool SkAlphaThresholdFilterImpl::onFilterImage(Proxy*, const SkBitmap& src,
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +0000323 const Context& ctx, SkBitmap* dst,
commit-bot@chromium.orgae761f72014-02-05 22:32:02 +0000324 SkIPoint* offset) const {
commit-bot@chromium.org28fcae22014-04-11 17:15:40 +0000325 SkASSERT(src.colorType() == kN32_SkColorType);
commit-bot@chromium.org40eb3c12014-01-06 23:41:14 +0000326
commit-bot@chromium.org28fcae22014-04-11 17:15:40 +0000327 if (src.colorType() != kN32_SkColorType) {
commit-bot@chromium.org9109e182014-01-07 16:04:01 +0000328 return false;
commit-bot@chromium.org40eb3c12014-01-06 23:41:14 +0000329 }
330
331 SkMatrix localInverse;
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +0000332 if (!ctx.ctm().invert(&localInverse)) {
commit-bot@chromium.org9109e182014-01-07 16:04:01 +0000333 return false;
commit-bot@chromium.org40eb3c12014-01-06 23:41:14 +0000334 }
335
336 SkAutoLockPixels alp(src);
337 SkASSERT(src.getPixels());
338 if (!src.getPixels() || src.width() <= 0 || src.height() <= 0) {
commit-bot@chromium.org9109e182014-01-07 16:04:01 +0000339 return false;
commit-bot@chromium.org40eb3c12014-01-06 23:41:14 +0000340 }
341
reedc77392e2014-06-02 13:07:26 -0700342 if (!dst->allocPixels(src.info())) {
commit-bot@chromium.org40eb3c12014-01-06 23:41:14 +0000343 return false;
344 }
345
commit-bot@chromium.org9109e182014-01-07 16:04:01 +0000346 U8CPU innerThreshold = (U8CPU)(fInnerThreshold * 0xFF);
347 U8CPU outerThreshold = (U8CPU)(fOuterThreshold * 0xFF);
commit-bot@chromium.org40eb3c12014-01-06 23:41:14 +0000348 SkColor* sptr = src.getAddr32(0, 0);
349 SkColor* dptr = dst->getAddr32(0, 0);
350 int width = src.width(), height = src.height();
351 for (int y = 0; y < height; ++y) {
352 for (int x = 0; x < width; ++x) {
353 const SkColor& source = sptr[y * width + x];
354 SkColor output_color(source);
355 SkPoint position;
commit-bot@chromium.org9109e182014-01-07 16:04:01 +0000356 localInverse.mapXY((SkScalar)x, (SkScalar)y, &position);
357 if (fRegion.contains((int32_t)position.x(), (int32_t)position.y())) {
commit-bot@chromium.org40eb3c12014-01-06 23:41:14 +0000358 if (SkColorGetA(source) < innerThreshold) {
359 U8CPU alpha = SkColorGetA(source);
360 if (alpha == 0)
361 alpha = 1;
362 float scale = (float)innerThreshold / alpha;
363 output_color = SkColorSetARGB(innerThreshold,
commit-bot@chromium.org9109e182014-01-07 16:04:01 +0000364 (U8CPU)(SkColorGetR(source) * scale),
365 (U8CPU)(SkColorGetG(source) * scale),
366 (U8CPU)(SkColorGetB(source) * scale));
commit-bot@chromium.org40eb3c12014-01-06 23:41:14 +0000367 }
368 } else {
369 if (SkColorGetA(source) > outerThreshold) {
370 float scale = (float)outerThreshold / SkColorGetA(source);
371 output_color = SkColorSetARGB(outerThreshold,
commit-bot@chromium.org9109e182014-01-07 16:04:01 +0000372 (U8CPU)(SkColorGetR(source) * scale),
373 (U8CPU)(SkColorGetG(source) * scale),
374 (U8CPU)(SkColorGetB(source) * scale));
commit-bot@chromium.org40eb3c12014-01-06 23:41:14 +0000375 }
376 }
377 dptr[y * dst->width() + x] = output_color;
378 }
379 }
380
381 return true;
382}