Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(2)

Side by Side Diff: src/effects/SkAlphaThresholdFilter.cpp

Issue 115633002: Add AlphaThreshold filter. (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: Created 7 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 /*
2 * Copyright 2013 The Android Open Source Project
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"
10 #include "SkFlattenableBuffers.h"
11 #include "SkRegion.h"
12
13 #if SK_SUPPORT_GPU
14 #include "GrContext.h"
15 #include "GrCoordTransform.h"
16 #include "GrEffect.h"
17 #include "gl/GrGLEffect.h"
18 #include "GrTBackendEffectFactory.h"
19 #include "GrTextureAccess.h"
20
21 #include "SkGr.h"
22
23 class GrGLAlphaThresholdEffect;
24
25 class AlphaThresholdEffect : public GrEffect {
26
27 public:
28 static GrEffectRef* Create(GrTexture* texture,
29 GrTexture* maskTexture,
30 float innerThreshold,
31 float outerThreshold) {
32 AutoEffectUnref effect(SkNEW_ARGS(AlphaThresholdEffect, (texture,
33 maskTexture,
34 innerThreshold,
35 outerThreshold) ));
36 return CreateEffectRef(effect);
37 }
38
39 virtual ~AlphaThresholdEffect() {};
40
41 static const char* Name() { return "Alpha Threshold"; }
42
43 virtual const GrBackendEffectFactory& getFactory() const SK_OVERRIDE;
44 virtual void getConstantColorComponents(GrColor* color, uint32_t* validFlags ) const SK_OVERRIDE;
45
46 float innerThreshold() const { return fInnerThreshold; }
47 float outerThreshold() const { return fOuterThreshold; }
48
49 typedef GrGLAlphaThresholdEffect GLEffect;
50
51 private:
52 AlphaThresholdEffect(GrTexture* texture,
53 GrTexture* maskTexture,
54 float innerThreshold,
55 float outerThreshold)
56 : fInnerThreshold(innerThreshold)
57 , fOuterThreshold(outerThreshold)
58 , fImageCoordTransform(kLocal_GrCoordSet, MakeDivByTextureWHMatrix(textu re), texture)
59 , fImageTextureAccess(texture)
60 , fMaskCoordTransform(kLocal_GrCoordSet, MakeDivByTextureWHMatrix(maskTe xture), maskTexture)
61 , fMaskTextureAccess(maskTexture) {
62 this->addCoordTransform(&fImageCoordTransform);
63 this->addTextureAccess(&fImageTextureAccess);
64 this->addCoordTransform(&fMaskCoordTransform);
65 this->addTextureAccess(&fMaskTextureAccess);
66 }
67
68 virtual bool onIsEqual(const GrEffect&) const SK_OVERRIDE;
69
70 GR_DECLARE_EFFECT_TEST;
71
72 float fInnerThreshold;
73 float fOuterThreshold;
74 GrCoordTransform fImageCoordTransform;
75 GrTextureAccess fImageTextureAccess;
76 GrCoordTransform fMaskCoordTransform;
77 GrTextureAccess fMaskTextureAccess;
78
79 typedef GrEffect INHERITED;
80 };
81
82 // For brevity
83 typedef GrGLUniformManager::UniformHandle UniformHandle;
Wez 2013/12/17 06:39:04 nit: Why not just using GrGL...::UniformHandle?
Zachary Kuznia 2013/12/18 00:06:27 Done.
84
85 class GrGLAlphaThresholdEffect : public GrGLEffect {
86 public:
87 GrGLAlphaThresholdEffect(const GrBackendEffectFactory&, const GrDrawEffect&) ;
88
89 virtual void emitCode(GrGLShaderBuilder*,
90 const GrDrawEffect&,
91 EffectKey,
92 const char* outputColor,
93 const char* inputColor,
94 const TransformedCoordsArray&,
95 const TextureSamplerArray&) SK_OVERRIDE;
96
97 virtual void setData(const GrGLUniformManager&, const GrDrawEffect&) SK_OVER RIDE;
98
99 private:
100 UniformHandle fInnerThresholdVar;
101 UniformHandle fOuterThresholdVar;
102
103 typedef GrGLEffect INHERITED;
104 };
105
106 GrGLAlphaThresholdEffect::GrGLAlphaThresholdEffect(const GrBackendEffectFactory& factory, const GrDrawEffect&)
107 : INHERITED(factory) {
108 }
109
110 void GrGLAlphaThresholdEffect::emitCode(GrGLShaderBuilder* builder,
111 const GrDrawEffect&,
112 EffectKey key,
113 const char* outputColor,
114 const char* inputColor,
115 const TransformedCoordsArray& coords,
116 const TextureSamplerArray& samplers) {
117 SkString coords2D = builder->ensureFSCoords2D(coords, 0);
118 SkString maskCoords2D = builder->ensureFSCoords2D(coords, 1);
119 fInnerThresholdVar = builder->addUniform(
120 GrGLShaderBuilder::kFragment_Visibility,
121 kFloat_GrSLType, "inner_threshold");
122 fOuterThresholdVar = builder->addUniform(
123 GrGLShaderBuilder::kFragment_Visibility,
124 kFloat_GrSLType, "outer_threshold");
125
126 builder->fsCodeAppendf("\t\tvec2 coord = %s;\n", coords2D.c_str());
127 builder->fsCodeAppendf("\t\tvec2 mask_coord = %s;\n", maskCoords2D.c_str());
128 builder->fsCodeAppend("\t\tvec4 color = ");
129 builder->fsAppendTextureLookup(samplers[0], "coord");
130 builder->fsCodeAppend(";\n");
131 builder->fsCodeAppend("\t\tvec4 mask_color = ");
132 builder->fsAppendTextureLookup(samplers[1], "mask_coord");
133 builder->fsCodeAppend(";\n");
134
135 builder->fsCodeAppendf("\t\tfloat inner_thresh = %s;\n",
136 builder->getUniformCStr(fInnerThresholdVar));
137 builder->fsCodeAppendf("\t\tfloat outer_thresh = %s;\n",
138 builder->getUniformCStr(fOuterThresholdVar));
139 builder->fsCodeAppend("\t\tfloat mask = mask_color.a;\n");
140
141 builder->fsCodeAppend("\t\tif (mask < 0.5) {\n"
142 "\t\t\tif (color.a > outer_thresh) {\n"
143 "\t\t\t\tfloat scale = outer_thresh / color.a;\n"
144 "\t\t\t\tcolor.rgb *= scale;\n"
145 "\t\t\t\tcolor.a = outer_thresh;\n"
146 "\t\t\t}\n"
147 "\t\t} else if (color.a < inner_thresh) {\n"
148 "\t\t\tfloat scale = inner_thresh / color.a;\n"
149 "\t\t\tcolor.rgb *= scale;\n"
150 "\t\t\tcolor.a = inner_thresh;\n"
151 "\t\t}\n");
152
153 builder->fsCodeAppendf("%s = %s;\n", outputColor,
154 (GrGLSLExpr4(inputColor) * GrGLSLExpr4("color")).c_st r());
155 }
156
157 void GrGLAlphaThresholdEffect::setData(const GrGLUniformManager& uman,
158 const GrDrawEffect& drawEffect) {
159 const AlphaThresholdEffect& alpha_threshold =
160 drawEffect.castEffect<AlphaThresholdEffect>();
161 uman.set1f(fInnerThresholdVar, alpha_threshold.innerThreshold());
162 uman.set1f(fOuterThresholdVar, alpha_threshold.outerThreshold());
163 }
164
165 /////////////////////////////////////////////////////////////////////
166
167 GR_DEFINE_EFFECT_TEST(AlphaThresholdEffect);
168
169 GrEffectRef* AlphaThresholdEffect::TestCreate(SkRandom* random,
170 GrContext* context,
171 const GrDrawTargetCaps&,
172 GrTexture** textures) {
173 GrTexture* bmpTex = textures[GrEffectUnitTest::kSkiaPMTextureIdx];
174 GrTexture* maskTex = textures[GrEffectUnitTest::kAlphaTextureIdx];
175 U8CPU inner_thresh = random->nextU() % 0xff;
176 U8CPU outer_thresh = random->nextU() % 0xff;
177 return AlphaThresholdEffect::Create(bmpTex, maskTex, inner_thresh, outer_thr esh);
178 }
179
180 ///////////////////////////////////////////////////////////////////////////////
181
182 const GrBackendEffectFactory& AlphaThresholdEffect::getFactory() const {
183 return GrTBackendEffectFactory<AlphaThresholdEffect>::getInstance();
184 }
185
186 bool AlphaThresholdEffect::onIsEqual(const GrEffect& sBase) const {
187 const AlphaThresholdEffect& s = CastEffect<AlphaThresholdEffect>(sBase);
188 return (this->texture(0) == s.texture(0) &&
189 this->fInnerThreshold == s.fInnerThreshold &&
190 this->fOuterThreshold == s.fOuterThreshold);
191 }
192
193 void AlphaThresholdEffect::getConstantColorComponents(GrColor* color, uint32_t* validFlags) const {
194 if ((*validFlags & kA_GrColorComponentFlag) && 0xFF == GrColorUnpackA(*color ) &&
195 GrPixelConfigIsOpaque(this->texture(0)->config())) {
196 *validFlags = kA_GrColorComponentFlag;
197 } else {
198 *validFlags = 0;
199 }
200 }
201
202 #endif
203
204 SkAlphaThresholdFilter::SkAlphaThresholdFilter(SkFlattenableReadBuffer& buffer)
205 : INHERITED(1, buffer) {
206 fInnerThreshold = buffer.readScalar();
207 fOuterThreshold = buffer.readScalar();
208 buffer.readRegion(&fRegion);
209 }
210
211 SkAlphaThresholdFilter::SkAlphaThresholdFilter(SkRegion region, SkScalar innerTh reshold, SkScalar outerThreshold)
212 : INHERITED(0), fRegion(region), fInnerThreshold(innerThreshold), fOuterThre shold(outerThreshold) {
213 }
214
215 #if SK_SUPPORT_GPU
216 bool SkAlphaThresholdFilter::asNewEffect(GrEffectRef** effect, GrTexture* textur e, const SkMatrix& in_matrix, const SkIRect&) const {
217 if (effect) {
218 GrContext* context = texture->getContext();
219 GrTextureDesc maskDesc;
220 if (context->isConfigRenderable(kAlpha_8_GrPixelConfig, false)) {
221 maskDesc.fConfig = kAlpha_8_GrPixelConfig;
222 } else {
223 maskDesc.fConfig = kRGBA_8888_GrPixelConfig;
224 }
225 maskDesc.fFlags = kRenderTarget_GrTextureFlagBit | kNoStencil_GrTextureF lagBit;
226 // Add one pixel of border to ensure that clamp mode will be all zeros
227 // the outside.
228 maskDesc.fWidth = texture->width();
229 maskDesc.fHeight = texture->height();
230 GrAutoScratchTexture ast(context, maskDesc, GrContext::kApprox_ScratchTe xMatch);
231 GrTexture* maskTexture = ast.texture();
232 if (NULL == maskTexture) {
233 return false;
234 }
235
236 {
237 GrContext::AutoRenderTarget art(context, ast.texture()->asRenderTarg et());
238 GrPaint grPaint;
239 grPaint.setBlendFunc(kOne_GrBlendCoeff, kZero_GrBlendCoeff);
240 SkRegion::Iterator iter(fRegion);
241 context->clear(NULL, 0x0, true);
242
243 SkMatrix old_matrix = context->getMatrix();
244 context->setMatrix(in_matrix);
245
246 while (!iter.done()) {
247 SkRect rect = SkRect::Make(iter.rect());
248 context->drawRect(grPaint, rect);
249 iter.next();
250 }
251 context->setMatrix(old_matrix);
252 }
253
254 *effect = AlphaThresholdEffect::Create(texture,
255 maskTexture,
256 fInnerThreshold,
257 fOuterThreshold);
258 }
259 return true;
260 }
261 #endif
262
263 void SkAlphaThresholdFilter::flatten(SkFlattenableWriteBuffer& buffer) const {
264 this->INHERITED::flatten(buffer);
265 buffer.writeScalar(fInnerThreshold);
266 buffer.writeScalar(fOuterThreshold);
267 buffer.writeRegion(fRegion);
268 }
269
270 bool SkAlphaThresholdFilter::onFilterImage(Proxy*, const SkBitmap& src,
271 const SkMatrix&, SkBitmap* dst,
272 SkIPoint* offset) {
273 SkASSERT(src.config() == SkBitmap::kARGB_8888_Config);
274
275 if (src.config() != SkBitmap::kARGB_8888_Config) {
276 return false;
277 }
278
279 SkAutoLockPixels alp(src);
280 SkASSERT(src.getPixels());
281 if (!src.getPixels() || src.width() <= 0 || src.height() <= 0) {
282 return false;
283 }
284
285 return true;
reed1 2013/12/16 14:30:49 Why do we return true, but don't set dst? Can we w
Zachary Kuznia 2013/12/18 00:06:27 Software version added.
286 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698