blob: a77891a38526b889edc1dab23a699192cb4d2b9e [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:
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +000022 explicit SkAlphaThresholdFilterImpl(SkReadBuffer& buffer);
23 virtual void flatten(SkWriteBuffer&) const SK_OVERRIDE;
commit-bot@chromium.org40eb3c12014-01-06 23:41:14 +000024
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +000025 virtual bool onFilterImage(Proxy*, const SkBitmap& src, const Context&,
commit-bot@chromium.orgae761f72014-02-05 22:32:02 +000026 SkBitmap* result, SkIPoint* offset) const SK_OVERRIDE;
commit-bot@chromium.org40eb3c12014-01-06 23:41:14 +000027#if SK_SUPPORT_GPU
bsalomon97b9ab72014-07-08 06:52:35 -070028 virtual bool asNewEffect(GrEffect** effect, GrTexture* texture,
commit-bot@chromium.org40eb3c12014-01-06 23:41:14 +000029 const SkMatrix& matrix, const SkIRect& bounds) const SK_OVERRIDE;
30#endif
31
32private:
33 SkRegion fRegion;
34 SkScalar fInnerThreshold;
35 SkScalar fOuterThreshold;
36 typedef SkImageFilter INHERITED;
37};
38
39SkImageFilter* SkAlphaThresholdFilter::Create(const SkRegion& region,
40 SkScalar innerThreshold,
senorblanco9ea3d572014-07-08 09:16:22 -070041 SkScalar outerThreshold,
42 SkImageFilter* input) {
43 return SkNEW_ARGS(SkAlphaThresholdFilterImpl, (region, innerThreshold, outerThreshold, input));
commit-bot@chromium.org40eb3c12014-01-06 23:41:14 +000044}
45
46#if SK_SUPPORT_GPU
47#include "GrContext.h"
48#include "GrCoordTransform.h"
49#include "GrEffect.h"
50#include "gl/GrGLEffect.h"
51#include "GrTBackendEffectFactory.h"
52#include "GrTextureAccess.h"
53
54#include "SkGr.h"
55
56class GrGLAlphaThresholdEffect;
57
58class AlphaThresholdEffect : public GrEffect {
59
60public:
61 static GrEffectRef* Create(GrTexture* texture,
62 GrTexture* maskTexture,
63 float innerThreshold,
64 float outerThreshold) {
bsalomon55fad7a2014-07-08 07:34:20 -070065 return SkNEW_ARGS(AlphaThresholdEffect, (texture,
66 maskTexture,
67 innerThreshold,
68 outerThreshold));
commit-bot@chromium.org40eb3c12014-01-06 23:41:14 +000069 }
70
71 virtual ~AlphaThresholdEffect() {};
72
73 static const char* Name() { return "Alpha Threshold"; }
74
75 virtual const GrBackendEffectFactory& getFactory() const SK_OVERRIDE;
76 virtual void getConstantColorComponents(GrColor* color, uint32_t* validFlags) const SK_OVERRIDE;
77
78 float innerThreshold() const { return fInnerThreshold; }
79 float outerThreshold() const { return fOuterThreshold; }
80
81 typedef GrGLAlphaThresholdEffect GLEffect;
82
83private:
84 AlphaThresholdEffect(GrTexture* texture,
85 GrTexture* maskTexture,
86 float innerThreshold,
87 float outerThreshold)
88 : fInnerThreshold(innerThreshold)
89 , fOuterThreshold(outerThreshold)
90 , fImageCoordTransform(kLocal_GrCoordSet, MakeDivByTextureWHMatrix(texture), texture)
91 , fImageTextureAccess(texture)
92 , fMaskCoordTransform(kLocal_GrCoordSet, MakeDivByTextureWHMatrix(maskTexture), maskTexture)
93 , fMaskTextureAccess(maskTexture) {
94 this->addCoordTransform(&fImageCoordTransform);
95 this->addTextureAccess(&fImageTextureAccess);
96 this->addCoordTransform(&fMaskCoordTransform);
97 this->addTextureAccess(&fMaskTextureAccess);
98 }
99
100 virtual bool onIsEqual(const GrEffect&) const SK_OVERRIDE;
101
102 GR_DECLARE_EFFECT_TEST;
103
104 float fInnerThreshold;
105 float fOuterThreshold;
106 GrCoordTransform fImageCoordTransform;
107 GrTextureAccess fImageTextureAccess;
108 GrCoordTransform fMaskCoordTransform;
109 GrTextureAccess fMaskTextureAccess;
110
111 typedef GrEffect INHERITED;
112};
113
114class GrGLAlphaThresholdEffect : public GrGLEffect {
115public:
116 GrGLAlphaThresholdEffect(const GrBackendEffectFactory&, const GrDrawEffect&);
117
118 virtual void emitCode(GrGLShaderBuilder*,
119 const GrDrawEffect&,
120 EffectKey,
121 const char* outputColor,
122 const char* inputColor,
123 const TransformedCoordsArray&,
124 const TextureSamplerArray&) SK_OVERRIDE;
125
126 virtual void setData(const GrGLUniformManager&, const GrDrawEffect&) SK_OVERRIDE;
127
128private:
129
130 GrGLUniformManager::UniformHandle fInnerThresholdVar;
131 GrGLUniformManager::UniformHandle fOuterThresholdVar;
132
133 typedef GrGLEffect INHERITED;
134};
135
136GrGLAlphaThresholdEffect::GrGLAlphaThresholdEffect(const GrBackendEffectFactory& factory, const GrDrawEffect&)
137 : INHERITED(factory) {
138}
139
140void GrGLAlphaThresholdEffect::emitCode(GrGLShaderBuilder* builder,
141 const GrDrawEffect&,
142 EffectKey key,
143 const char* outputColor,
144 const char* inputColor,
145 const TransformedCoordsArray& coords,
146 const TextureSamplerArray& samplers) {
147 SkString coords2D = builder->ensureFSCoords2D(coords, 0);
148 SkString maskCoords2D = builder->ensureFSCoords2D(coords, 1);
149 fInnerThresholdVar = builder->addUniform(
150 GrGLShaderBuilder::kFragment_Visibility,
151 kFloat_GrSLType, "inner_threshold");
152 fOuterThresholdVar = builder->addUniform(
153 GrGLShaderBuilder::kFragment_Visibility,
154 kFloat_GrSLType, "outer_threshold");
155
156 builder->fsCodeAppendf("\t\tvec2 coord = %s;\n", coords2D.c_str());
157 builder->fsCodeAppendf("\t\tvec2 mask_coord = %s;\n", maskCoords2D.c_str());
158 builder->fsCodeAppend("\t\tvec4 input_color = ");
159 builder->fsAppendTextureLookup(samplers[0], "coord");
160 builder->fsCodeAppend(";\n");
161 builder->fsCodeAppend("\t\tvec4 mask_color = ");
162 builder->fsAppendTextureLookup(samplers[1], "mask_coord");
163 builder->fsCodeAppend(";\n");
164
165 builder->fsCodeAppendf("\t\tfloat inner_thresh = %s;\n",
166 builder->getUniformCStr(fInnerThresholdVar));
167 builder->fsCodeAppendf("\t\tfloat outer_thresh = %s;\n",
168 builder->getUniformCStr(fOuterThresholdVar));
169 builder->fsCodeAppend("\t\tfloat mask = mask_color.a;\n");
170
171 builder->fsCodeAppend("vec4 color = input_color;\n");
172 builder->fsCodeAppend("\t\tif (mask < 0.5) {\n"
173 "\t\t\tif (color.a > outer_thresh) {\n"
174 "\t\t\t\tfloat scale = outer_thresh / color.a;\n"
175 "\t\t\t\tcolor.rgb *= scale;\n"
176 "\t\t\t\tcolor.a = outer_thresh;\n"
177 "\t\t\t}\n"
178 "\t\t} else if (color.a < inner_thresh) {\n"
179 "\t\t\tfloat scale = inner_thresh / max(0.001, color.a);\n"
180 "\t\t\tcolor.rgb *= scale;\n"
181 "\t\t\tcolor.a = inner_thresh;\n"
182 "\t\t}\n");
183
184 builder->fsCodeAppendf("%s = %s;\n", outputColor,
185 (GrGLSLExpr4(inputColor) * GrGLSLExpr4("color")).c_str());
186}
187
188void GrGLAlphaThresholdEffect::setData(const GrGLUniformManager& uman,
189 const GrDrawEffect& drawEffect) {
190 const AlphaThresholdEffect& alpha_threshold =
191 drawEffect.castEffect<AlphaThresholdEffect>();
192 uman.set1f(fInnerThresholdVar, alpha_threshold.innerThreshold());
193 uman.set1f(fOuterThresholdVar, alpha_threshold.outerThreshold());
194}
195
196/////////////////////////////////////////////////////////////////////
197
198GR_DEFINE_EFFECT_TEST(AlphaThresholdEffect);
199
200GrEffectRef* AlphaThresholdEffect::TestCreate(SkRandom* random,
201 GrContext* context,
202 const GrDrawTargetCaps&,
203 GrTexture** textures) {
204 GrTexture* bmpTex = textures[GrEffectUnitTest::kSkiaPMTextureIdx];
205 GrTexture* maskTex = textures[GrEffectUnitTest::kAlphaTextureIdx];
206 float inner_thresh = random->nextUScalar1();
207 float outer_thresh = random->nextUScalar1();
208 return AlphaThresholdEffect::Create(bmpTex, maskTex, inner_thresh, outer_thresh);
209}
210
211///////////////////////////////////////////////////////////////////////////////
212
213const GrBackendEffectFactory& AlphaThresholdEffect::getFactory() const {
214 return GrTBackendEffectFactory<AlphaThresholdEffect>::getInstance();
215}
216
217bool AlphaThresholdEffect::onIsEqual(const GrEffect& sBase) const {
218 const AlphaThresholdEffect& s = CastEffect<AlphaThresholdEffect>(sBase);
219 return (this->texture(0) == s.texture(0) &&
220 this->fInnerThreshold == s.fInnerThreshold &&
221 this->fOuterThreshold == s.fOuterThreshold);
222}
223
224void AlphaThresholdEffect::getConstantColorComponents(GrColor* color, uint32_t* validFlags) const {
225 if ((*validFlags & kA_GrColorComponentFlag) && 0xFF == GrColorUnpackA(*color) &&
226 GrPixelConfigIsOpaque(this->texture(0)->config())) {
227 *validFlags = kA_GrColorComponentFlag;
228 } else {
229 *validFlags = 0;
230 }
231}
232
233#endif
234
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +0000235SkAlphaThresholdFilterImpl::SkAlphaThresholdFilterImpl(SkReadBuffer& buffer)
commit-bot@chromium.org40eb3c12014-01-06 23:41:14 +0000236 : INHERITED(1, buffer) {
237 fInnerThreshold = buffer.readScalar();
238 fOuterThreshold = buffer.readScalar();
239 buffer.readRegion(&fRegion);
240}
241
242SkAlphaThresholdFilterImpl::SkAlphaThresholdFilterImpl(const SkRegion& region,
243 SkScalar innerThreshold,
senorblanco9ea3d572014-07-08 09:16:22 -0700244 SkScalar outerThreshold,
245 SkImageFilter* input)
246 : INHERITED(1, &input)
commit-bot@chromium.org40eb3c12014-01-06 23:41:14 +0000247 , fRegion(region)
248 , fInnerThreshold(innerThreshold)
249 , fOuterThreshold(outerThreshold) {
250}
251
252#if SK_SUPPORT_GPU
bsalomon97b9ab72014-07-08 06:52:35 -0700253bool SkAlphaThresholdFilterImpl::asNewEffect(GrEffect** effect, GrTexture* texture,
commit-bot@chromium.org40eb3c12014-01-06 23:41:14 +0000254 const SkMatrix& in_matrix, const SkIRect&) const {
255 if (effect) {
256 GrContext* context = texture->getContext();
257 GrTextureDesc maskDesc;
258 if (context->isConfigRenderable(kAlpha_8_GrPixelConfig, false)) {
259 maskDesc.fConfig = kAlpha_8_GrPixelConfig;
260 } else {
261 maskDesc.fConfig = kRGBA_8888_GrPixelConfig;
262 }
263 maskDesc.fFlags = kRenderTarget_GrTextureFlagBit | kNoStencil_GrTextureFlagBit;
264 // Add one pixel of border to ensure that clamp mode will be all zeros
265 // the outside.
266 maskDesc.fWidth = texture->width();
267 maskDesc.fHeight = texture->height();
268 GrAutoScratchTexture ast(context, maskDesc, GrContext::kApprox_ScratchTexMatch);
269 GrTexture* maskTexture = ast.texture();
270 if (NULL == maskTexture) {
271 return false;
272 }
273
274 {
275 GrContext::AutoRenderTarget art(context, ast.texture()->asRenderTarget());
276 GrPaint grPaint;
277 grPaint.setBlendFunc(kOne_GrBlendCoeff, kZero_GrBlendCoeff);
278 SkRegion::Iterator iter(fRegion);
279 context->clear(NULL, 0x0, true);
280
281 SkMatrix old_matrix = context->getMatrix();
282 context->setMatrix(in_matrix);
283
284 while (!iter.done()) {
285 SkRect rect = SkRect::Make(iter.rect());
286 context->drawRect(grPaint, rect);
287 iter.next();
288 }
289 context->setMatrix(old_matrix);
290 }
291
292 *effect = AlphaThresholdEffect::Create(texture,
293 maskTexture,
294 fInnerThreshold,
295 fOuterThreshold);
296 }
297 return true;
298}
299#endif
300
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +0000301void SkAlphaThresholdFilterImpl::flatten(SkWriteBuffer& buffer) const {
commit-bot@chromium.org40eb3c12014-01-06 23:41:14 +0000302 this->INHERITED::flatten(buffer);
303 buffer.writeScalar(fInnerThreshold);
304 buffer.writeScalar(fOuterThreshold);
305 buffer.writeRegion(fRegion);
306}
307
308bool SkAlphaThresholdFilterImpl::onFilterImage(Proxy*, const SkBitmap& src,
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +0000309 const Context& ctx, SkBitmap* dst,
commit-bot@chromium.orgae761f72014-02-05 22:32:02 +0000310 SkIPoint* offset) const {
commit-bot@chromium.org28fcae22014-04-11 17:15:40 +0000311 SkASSERT(src.colorType() == kN32_SkColorType);
commit-bot@chromium.org40eb3c12014-01-06 23:41:14 +0000312
commit-bot@chromium.org28fcae22014-04-11 17:15:40 +0000313 if (src.colorType() != kN32_SkColorType) {
commit-bot@chromium.org9109e182014-01-07 16:04:01 +0000314 return false;
commit-bot@chromium.org40eb3c12014-01-06 23:41:14 +0000315 }
316
317 SkMatrix localInverse;
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +0000318 if (!ctx.ctm().invert(&localInverse)) {
commit-bot@chromium.org9109e182014-01-07 16:04:01 +0000319 return false;
commit-bot@chromium.org40eb3c12014-01-06 23:41:14 +0000320 }
321
322 SkAutoLockPixels alp(src);
323 SkASSERT(src.getPixels());
324 if (!src.getPixels() || src.width() <= 0 || src.height() <= 0) {
commit-bot@chromium.org9109e182014-01-07 16:04:01 +0000325 return false;
commit-bot@chromium.org40eb3c12014-01-06 23:41:14 +0000326 }
327
reedc77392e2014-06-02 13:07:26 -0700328 if (!dst->allocPixels(src.info())) {
commit-bot@chromium.org40eb3c12014-01-06 23:41:14 +0000329 return false;
330 }
331
commit-bot@chromium.org9109e182014-01-07 16:04:01 +0000332 U8CPU innerThreshold = (U8CPU)(fInnerThreshold * 0xFF);
333 U8CPU outerThreshold = (U8CPU)(fOuterThreshold * 0xFF);
commit-bot@chromium.org40eb3c12014-01-06 23:41:14 +0000334 SkColor* sptr = src.getAddr32(0, 0);
335 SkColor* dptr = dst->getAddr32(0, 0);
336 int width = src.width(), height = src.height();
337 for (int y = 0; y < height; ++y) {
338 for (int x = 0; x < width; ++x) {
339 const SkColor& source = sptr[y * width + x];
340 SkColor output_color(source);
341 SkPoint position;
commit-bot@chromium.org9109e182014-01-07 16:04:01 +0000342 localInverse.mapXY((SkScalar)x, (SkScalar)y, &position);
343 if (fRegion.contains((int32_t)position.x(), (int32_t)position.y())) {
commit-bot@chromium.org40eb3c12014-01-06 23:41:14 +0000344 if (SkColorGetA(source) < innerThreshold) {
345 U8CPU alpha = SkColorGetA(source);
346 if (alpha == 0)
347 alpha = 1;
348 float scale = (float)innerThreshold / alpha;
349 output_color = SkColorSetARGB(innerThreshold,
commit-bot@chromium.org9109e182014-01-07 16:04:01 +0000350 (U8CPU)(SkColorGetR(source) * scale),
351 (U8CPU)(SkColorGetG(source) * scale),
352 (U8CPU)(SkColorGetB(source) * scale));
commit-bot@chromium.org40eb3c12014-01-06 23:41:14 +0000353 }
354 } else {
355 if (SkColorGetA(source) > outerThreshold) {
356 float scale = (float)outerThreshold / SkColorGetA(source);
357 output_color = SkColorSetARGB(outerThreshold,
commit-bot@chromium.org9109e182014-01-07 16:04:01 +0000358 (U8CPU)(SkColorGetR(source) * scale),
359 (U8CPU)(SkColorGetG(source) * scale),
360 (U8CPU)(SkColorGetB(source) * scale));
commit-bot@chromium.org40eb3c12014-01-06 23:41:14 +0000361 }
362 }
363 dptr[y * dst->width() + x] = output_color;
364 }
365 }
366
367 return true;
368}