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

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

Issue 23707019: alpha threshold bitmap shader (Closed) Base URL: https://skia.googlecode.com/svn/trunk
Patch Set: cleanup Created 7 years, 3 months 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 | Annotate | Revision Log
OLDNEW
(Empty)
1 /*
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 "SkBitmapAlphaThresholdShader.h"
9
10 SkBitmapAlphaThresholdShader::SkBitmapAlphaThresholdShader(SkBitmap bitmap,
11 SkRegion region,
12 U8CPU threshold)
13 : fBitmap(bitmap)
14 , fRegion(region)
15 , fThreshold(threshold) {
16 }
17
18 SkShader* SkBitmapAlphaThresholdShader::Create(SkBitmap bitmap,
19 const SkRegion& region,
20 U8CPU threshold) {
21 SkASSERT(threshold < 256);
22 return SkNEW_ARGS(SkBitmapAlphaThresholdShader, (bitmap, region, threshold)) ;
23 }
24
25 #ifdef SK_DEVELOPER
26 void SkBitmapAlphaThresholdShader::toString(SkString* str) const {
27 str->append("ThresholdShader: (");
28
29 fBitmap.toString(str);
30
31 this->INHERITED::toString(str);
32
33 str->append(")");
34 }
35 #endif
36
37 #if SK_SUPPORT_GPU
38 #include "GrContext.h"
39 #include "GrEffect.h"
40 #include "gl/GrGLEffect.h"
41 #include "gl/GrGLEffectMatrix.h"
42 #include "GrTBackendEffectFactory.h"
43 #include "GrTextureAccess.h"
44
45 #include "SkGr.h"
46
47 /**
48 * Could create specializations for some simple cases:
49 * - The region is empty.
50 * - The region fully contains the bitmap.
51 * - The regions is 1 rect (or maybe a small number of rects).
52 */
53 class ThresholdEffect : public GrEffect {
54 public:
55 virtual const GrBackendEffectFactory& getFactory() const SK_OVERRIDE {
56 return GrTBackendEffectFactory<ThresholdEffect>::getInstance();
57 }
58
59 static GrEffectRef* Create(GrTexture* bmpTexture, const SkMatrix& bmpMatrix,
60 GrTexture* maskTexture, const SkMatrix& maskMatri x,
61 U8CPU threshold) {
62 SkScalar thresh = SkIntToScalar(threshold) / 255;
63
64 AutoEffectUnref effect(SkNEW_ARGS(ThresholdEffect, (bmpTexture, bmpMatri x,
65 maskTexture, maskMat rix,
66 thresh)));
67 return CreateEffectRef(effect);
68 }
69
70 virtual void getConstantColorComponents(GrColor* color,
71 uint32_t* validFlags) const SK_OVERR IDE {
72 if ((kA_GrColorComponentFlag & *validFlags) && 0 == GrColorUnpackA(*colo r)) {
73 return;
74 }
75 *validFlags = 0;
76 return;
77 }
78
79 static const char* Name() { return "Bitmap Alpha Threshold"; }
80
81 class GLEffect : public GrGLEffect {
82 public:
83 GLEffect(const GrBackendEffectFactory& factory,
84 const GrDrawEffect& e)
85 : GrGLEffect(factory)
86 , fBmpMatrix(GrEffect::kLocal_CoordsType)
87 , fMaskMatrix(GrEffect::kLocal_CoordsType)
88 , fPrevThreshold(-SK_Scalar1) {
89 }
90
91 virtual void emitCode(GrGLShaderBuilder* builder,
92 const GrDrawEffect& drawEffect,
93 EffectKey key,
94 const char* outputColor,
95 const char* inputColor,
96 const TextureSamplerArray& samplers) SK_OVERRIDE {
97 SkString bmpCoord;
98 SkString maskCoord;
99
100 GrSLType bmpCoordType = fBmpMatrix.emitCode(builder, key, &bmpCoord, NULL, "Bmp");
101 EffectKey maskMatrixKey = key >> GrGLEffectMatrix::kKeyBits;
102 GrSLType maskCoordType = fMaskMatrix.emitCode(builder,
103 maskMatrixKey,
104 &maskCoord,
105 NULL,
106 "Mask");
107
108 // put bitmap color in "color"
109 builder->fsCodeAppend("\t\tvec4 color = ");
110 builder->fsAppendTextureLookup(samplers[0], bmpCoord.c_str(), bmpCoo rdType);
111 builder->fsCodeAppend(";\n");
112
113 // put alpha from mask texture in "mask"
114 builder->fsCodeAppend("\t\tfloat mask = ");
115 builder->fsAppendTextureLookup(samplers[1], maskCoord.c_str(), maskC oordType);
116 builder->fsCodeAppend(".a;\n");
117
118 const char* threshold;
119
120 fThresholdUniHandle = builder->addUniform(GrGLShaderBuilder::kFragme nt_Visibility,
121 kFloat_GrSLType,
122 "threshold",
123 &threshold);
124 builder->fsCodeAppendf("\t\tfloat thresh = %s;\n", threshold);
125
126 builder->fsCodeAppend("\t\tif (mask < 0.5) {\n"
127 "\t\t\tif (color.a > thresh) {\n"
128 "\t\t\t\tfloat scale = thresh / color.a;\n"
129 "\t\t\t\tcolor.rgb *= scale;\n"
130 "\t\t\t\tcolor.a = thresh;\n"
131 "\t\t\t}\n"
132 "\t\t} else if (color.a < thresh) {\n"
133 "\t\t\tfloat scale = thresh / color.a;\n"
134 "\t\t\tcolor.rgb *= scale;\n"
135 "\t\t\tcolor.a = thresh;\n"
136 "\t\t}\n");
137
138 builder->fsCodeAppend("color = ");
139 SkString outStr;
140 outStr.appendf("\t\t%s = ", outputColor);
141 GrGLSLModulatef<4>(&outStr, inputColor, "color");
142 outStr.append(";\n");
143 builder->fsCodeAppend(outStr.c_str());
144 }
145
146 virtual void setData(const GrGLUniformManager& uman, const GrDrawEffect& e) SK_OVERRIDE {
147 const ThresholdEffect& effect = e.castEffect<ThresholdEffect>();
148 fBmpMatrix.setData(uman, effect.fBmpMatrix, e, effect.fBmpAccess.get Texture());
149 fMaskMatrix.setData(uman, effect.fMaskMatrix, e, effect.fMaskAccess. getTexture());
150 if (fPrevThreshold != effect.fThreshold) {
151 uman.set1f(fThresholdUniHandle, effect.fThreshold);
152 }
153 }
154
155 static inline EffectKey GenKey(const GrDrawEffect& e, const GrGLCaps&) {
156 const ThresholdEffect& effect = e.castEffect<ThresholdEffect>();
157
158 EffectKey bmpMKey = GrGLEffectMatrix::GenKey(effect.fBmpMatrix,
159 e,
160 GrEffect::kLocal_Coords Type,
161 effect.fBmpAccess.getTe xture());
162 EffectKey maskMKey = GrGLEffectMatrix::GenKey(effect.fMaskMatrix,
163 e,
164 GrEffect::kLocal_Coord sType,
165 effect.fMaskAccess.get Texture());
166 return bmpMKey | (maskMKey << GrGLEffectMatrix::kKeyBits);
167 }
168
169 private:
170 GrGLEffectMatrix fBmpMatrix;
171 GrGLEffectMatrix fMaskMatrix;
172
173 GrGLUniformManager::UniformHandle fThresholdUniHandle;
174 SkScalar fPrevThreshold;
175 };
176
177 GR_DECLARE_EFFECT_TEST;
178
179 private:
180 ThresholdEffect(GrTexture* bmpTexture, const SkMatrix& bmpMatrix,
181 GrTexture* maskTexture, const SkMatrix& maskMatrix,
182 SkScalar threshold)
183 : fBmpAccess(bmpTexture, GrTextureParams())
184 , fMaskAccess(maskTexture, GrTextureParams())
185 , fBmpMatrix(bmpMatrix)
186 , fMaskMatrix(maskMatrix)
187 , fThreshold(threshold) {
188 this->addTextureAccess(&fBmpAccess);
189 this->addTextureAccess(&fMaskAccess);
190 }
191
192 virtual bool onIsEqual(const GrEffect& other) const SK_OVERRIDE {
193 const ThresholdEffect& e = CastEffect<ThresholdEffect>(other);
194 return e.fBmpAccess.getTexture() == fBmpAccess.getTexture() &&
195 e.fMaskAccess.getTexture() == fMaskAccess.getTexture() &&
196 e.fBmpMatrix == fBmpMatrix &&
197 e.fMaskMatrix == fMaskMatrix &&
198 e.fThreshold == fThreshold;
199 }
200
201 GrTextureAccess fBmpAccess;
202 GrTextureAccess fMaskAccess;
203
204 SkMatrix fBmpMatrix;
205 SkMatrix fMaskMatrix;
206
207 SkScalar fThreshold;
208 };
209
210 GR_DEFINE_EFFECT_TEST(ThresholdEffect);
211
212 GrEffectRef* ThresholdEffect::TestCreate(SkMWCRandom* rand,
213 GrContext*,
214 const GrDrawTargetCaps&,
215 GrTexture* textures[]) {
216 GrTexture* bmpTex = textures[GrEffectUnitTest::kSkiaPMTextureIdx];
217 GrTexture* maskTex = textures[GrEffectUnitTest::kAlphaTextureIdx];
218 U8CPU thresh = rand->nextU() % 0xff;
219 return ThresholdEffect::Create(bmpTex, SkMatrix::I(), maskTex, SkMatrix::I() , thresh);
220 }
221
222 GrEffectRef* SkBitmapAlphaThresholdShader::asNewEffect(GrContext* context,
223 const SkPaint& paint) con st {
224 SkMatrix localInverse;
225 if (!this->getLocalMatrix().invert(&localInverse)) {
226 return NULL;
227 }
228
229 GrTextureDesc maskDesc;
230 if (context->isConfigRenderable(kAlpha_8_GrPixelConfig)) {
231 maskDesc.fConfig = kAlpha_8_GrPixelConfig;
232 } else {
233 maskDesc.fConfig = kRGBA_8888_GrPixelConfig;
234 }
235 maskDesc.fFlags = kRenderTarget_GrTextureFlagBit | kNoStencil_GrTextureFlagB it;
236 const SkIRect& bounds = fRegion.getBounds();
237 // Add one pixel of border to ensure that clamp mode will be all zeros
238 // the outside.
239 maskDesc.fWidth = bounds.width() + 2;
240 maskDesc.fHeight = bounds.height() + 2;
241 GrAutoScratchTexture ast(context, maskDesc, GrContext::kApprox_ScratchTexMat ch);
242 GrTexture* maskTexture = ast.texture();
243 if (NULL == maskTexture) {
244 return false;
245 }
246
247 GrPaint grPaint;
248 grPaint.setBlendFunc(kOne_GrBlendCoeff, kZero_GrBlendCoeff);
249 SkRegion::Iterator iter(fRegion);
250 context->setRenderTarget(maskTexture->asRenderTarget());
251 context->clear(NULL, 0x0);
252
253 // offset to ensure border is zero on top/left
254 SkMatrix matrix;
255 matrix.setTranslate(SK_Scalar1, SK_Scalar1);
256 context->setMatrix(matrix);
257
258 while (!iter.done()) {
259 SkRect rect = SkRect::MakeFromIRect(iter.rect());
260 context->drawRect(grPaint, rect);
261 iter.next();
262 }
263
264 GrTexture* bmpTexture = GrLockAndRefCachedBitmapTexture(context, fBitmap, NU LL);
265 if (NULL == bmpTexture) {
266 return NULL;
267 }
268
269 SkMatrix bmpMatrix = localInverse;
270 bmpMatrix.postIDiv(bmpTexture->width(), bmpTexture->height());
271
272 SkMatrix maskMatrix = localInverse;
273 // compensate for the border
274 maskMatrix.postTranslate(SK_Scalar1, SK_Scalar1);
275 maskMatrix.postIDiv(maskTexture->width(), maskTexture->height());
276
277 GrEffectRef* effect = ThresholdEffect::Create(bmpTexture, bmpMatrix,
278 maskTexture, maskMatrix,
279 fThreshold);
280
281 GrUnlockAndUnrefCachedBitmapTexture(bmpTexture);
282
283 return effect;
284 }
285
286 #endif
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698