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