blob: 05d7ba30d38b9064cb1b577e8ef1865853efad42 [file] [log] [blame]
commit-bot@chromium.org91a798f2013-09-06 15:31:06 +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 "SkBitmapAlphaThresholdShader.h"
9
10class BATShader : public SkShader {
11public:
robertphillips@google.com0255a5d2013-10-24 14:03:01 +000012 SK_DECLARE_INST_COUNT(BATShader);
commit-bot@chromium.org91a798f2013-09-06 15:31:06 +000013
14 BATShader(const SkBitmap& bitmap, SkRegion region, U8CPU);
15 BATShader(SkFlattenableReadBuffer& buffer) : INHERITED(buffer) {
16 // We should probably do something here.
17 }
skia.committer@gmail.comb3ec29d2013-09-07 07:01:16 +000018
19
commit-bot@chromium.org91a798f2013-09-06 15:31:06 +000020 virtual void shadeSpan(int x, int y, SkPMColor[], int count) SK_OVERRIDE {};
skia.committer@gmail.comb3ec29d2013-09-07 07:01:16 +000021
commit-bot@chromium.org91a798f2013-09-06 15:31:06 +000022#if SK_SUPPORT_GPU
23 virtual GrEffectRef* asNewEffect(GrContext* context, const SkPaint& paint) const SK_OVERRIDE;
24#endif
skia.committer@gmail.comb3ec29d2013-09-07 07:01:16 +000025
commit-bot@chromium.org91a798f2013-09-06 15:31:06 +000026 SK_DEVELOPER_TO_STRING();
27 SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(BATShader)
skia.committer@gmail.comb3ec29d2013-09-07 07:01:16 +000028
commit-bot@chromium.org91a798f2013-09-06 15:31:06 +000029private:
30 SkBitmap fBitmap;
31 SkRegion fRegion;
32 U8CPU fThreshold;
skia.committer@gmail.comb3ec29d2013-09-07 07:01:16 +000033
commit-bot@chromium.org91a798f2013-09-06 15:31:06 +000034 typedef SkShader INHERITED;
35};
36
robertphillips@google.com0255a5d2013-10-24 14:03:01 +000037SK_DEFINE_INST_COUNT(BATShader)
38
commit-bot@chromium.org91a798f2013-09-06 15:31:06 +000039SkShader* SkBitmapAlphaThresholdShader::Create(const SkBitmap& bitmap,
40 const SkRegion& region,
41 U8CPU threshold) {
42 SkASSERT(threshold < 256);
43 return SkNEW_ARGS(BATShader, (bitmap, region, threshold));
44}
45
46BATShader::BATShader(const SkBitmap& bitmap, SkRegion region, U8CPU threshold)
47: fBitmap(bitmap)
48, fRegion(region)
49, fThreshold(threshold) {
50};
51
52
53#ifdef SK_DEVELOPER
54void BATShader::toString(SkString* str) const {
55 str->append("BATShader: (");
56
57 fBitmap.toString(str);
58
59 this->INHERITED::toString(str);
60
61 str->append(")");
62}
63#endif
64
65#if SK_SUPPORT_GPU
66#include "GrContext.h"
bsalomon@google.com77af6802013-10-02 13:04:56 +000067#include "GrCoordTransform.h"
commit-bot@chromium.org91a798f2013-09-06 15:31:06 +000068#include "GrEffect.h"
69#include "gl/GrGLEffect.h"
commit-bot@chromium.org91a798f2013-09-06 15:31:06 +000070#include "GrTBackendEffectFactory.h"
71#include "GrTextureAccess.h"
72
73#include "SkGr.h"
74
75/**
76 * Could create specializations for some simple cases:
77 * - The region is empty.
78 * - The region fully contains the bitmap.
79 * - The regions is 1 rect (or maybe a small number of rects).
80 */
81class ThresholdEffect : public GrEffect {
82public:
83 virtual const GrBackendEffectFactory& getFactory() const SK_OVERRIDE {
84 return GrTBackendEffectFactory<ThresholdEffect>::getInstance();
85 }
86
87 static GrEffectRef* Create(GrTexture* bmpTexture, const SkMatrix& bmpMatrix,
88 GrTexture* maskTexture, const SkMatrix& maskMatrix,
89 U8CPU threshold) {
90 SkScalar thresh = SkIntToScalar(threshold) / 255;
91
92 AutoEffectUnref effect(SkNEW_ARGS(ThresholdEffect, (bmpTexture, bmpMatrix,
93 maskTexture, maskMatrix,
94 thresh)));
95 return CreateEffectRef(effect);
96 }
97
98 virtual void getConstantColorComponents(GrColor* color,
99 uint32_t* validFlags) const SK_OVERRIDE {
100 if ((kA_GrColorComponentFlag & *validFlags) && 0 == GrColorUnpackA(*color)) {
101 return;
102 }
103 *validFlags = 0;
104 return;
105 }
106
107 static const char* Name() { return "Bitmap Alpha Threshold"; }
108
109 class GLEffect : public GrGLEffect {
110 public:
111 GLEffect(const GrBackendEffectFactory& factory,
112 const GrDrawEffect& e)
113 : GrGLEffect(factory)
commit-bot@chromium.org91a798f2013-09-06 15:31:06 +0000114 , fPrevThreshold(-SK_Scalar1) {
115 }
116
117 virtual void emitCode(GrGLShaderBuilder* builder,
bsalomon@google.com77af6802013-10-02 13:04:56 +0000118 const GrDrawEffect& drawEffect,
119 EffectKey key,
120 const char* outputColor,
121 const char* inputColor,
122 const TransformedCoordsArray& coords,
123 const TextureSamplerArray& samplers) SK_OVERRIDE {
commit-bot@chromium.org91a798f2013-09-06 15:31:06 +0000124 // put bitmap color in "color"
125 builder->fsCodeAppend("\t\tvec4 color = ");
bsalomon@google.com77af6802013-10-02 13:04:56 +0000126 builder->fsAppendTextureLookup(samplers[0], coords[0].c_str(), coords[0].type());
commit-bot@chromium.org91a798f2013-09-06 15:31:06 +0000127 builder->fsCodeAppend(";\n");
128
129 // put alpha from mask texture in "mask"
130 builder->fsCodeAppend("\t\tfloat mask = ");
bsalomon@google.com77af6802013-10-02 13:04:56 +0000131 builder->fsAppendTextureLookup(samplers[1], coords[1].c_str(), coords[1].type());
commit-bot@chromium.org91a798f2013-09-06 15:31:06 +0000132 builder->fsCodeAppend(".a;\n");
133
134 const char* threshold;
135
136 fThresholdUniHandle = builder->addUniform(GrGLShaderBuilder::kFragment_Visibility,
137 kFloat_GrSLType,
138 "threshold",
139 &threshold);
140 builder->fsCodeAppendf("\t\tfloat thresh = %s;\n", threshold);
141
142 builder->fsCodeAppend("\t\tif (mask < 0.5) {\n"
143 "\t\t\tif (color.a > thresh) {\n"
144 "\t\t\t\tfloat scale = thresh / color.a;\n"
145 "\t\t\t\tcolor.rgb *= scale;\n"
146 "\t\t\t\tcolor.a = thresh;\n"
147 "\t\t\t}\n"
148 "\t\t} else if (color.a < thresh) {\n"
149 "\t\t\tfloat scale = thresh / color.a;\n"
150 "\t\t\tcolor.rgb *= scale;\n"
151 "\t\t\tcolor.a = thresh;\n"
152 "\t\t}\n");
153
commit-bot@chromium.org824c3462013-10-10 06:30:18 +0000154 builder->fsCodeAppendf("color = %s = %s;\n", outputColor,
commit-bot@chromium.orga34995e2013-10-23 05:42:03 +0000155 (GrGLSLExpr4(inputColor) * GrGLSLExpr4("color")).c_str());
commit-bot@chromium.org91a798f2013-09-06 15:31:06 +0000156 }
157
158 virtual void setData(const GrGLUniformManager& uman, const GrDrawEffect& e) SK_OVERRIDE {
159 const ThresholdEffect& effect = e.castEffect<ThresholdEffect>();
commit-bot@chromium.org91a798f2013-09-06 15:31:06 +0000160 if (fPrevThreshold != effect.fThreshold) {
161 uman.set1f(fThresholdUniHandle, effect.fThreshold);
162 }
163 }
164
commit-bot@chromium.org91a798f2013-09-06 15:31:06 +0000165 private:
commit-bot@chromium.org91a798f2013-09-06 15:31:06 +0000166 GrGLUniformManager::UniformHandle fThresholdUniHandle;
167 SkScalar fPrevThreshold;
168 };
169
170 GR_DECLARE_EFFECT_TEST;
171
172private:
173 ThresholdEffect(GrTexture* bmpTexture, const SkMatrix& bmpMatrix,
174 GrTexture* maskTexture, const SkMatrix& maskMatrix,
175 SkScalar threshold)
bsalomon@google.com77af6802013-10-02 13:04:56 +0000176 : fBmpTransform(kLocal_GrCoordSet, bmpMatrix, bmpTexture)
177 , fBmpAccess(bmpTexture, GrTextureParams())
178 , fMaskTransform(kLocal_GrCoordSet, maskMatrix, maskTexture)
commit-bot@chromium.org91a798f2013-09-06 15:31:06 +0000179 , fMaskAccess(maskTexture, GrTextureParams())
commit-bot@chromium.org91a798f2013-09-06 15:31:06 +0000180 , fThreshold(threshold) {
bsalomon@google.com77af6802013-10-02 13:04:56 +0000181 this->addCoordTransform(&fBmpTransform);
commit-bot@chromium.org91a798f2013-09-06 15:31:06 +0000182 this->addTextureAccess(&fBmpAccess);
bsalomon@google.com77af6802013-10-02 13:04:56 +0000183 this->addCoordTransform(&fMaskTransform);
commit-bot@chromium.org91a798f2013-09-06 15:31:06 +0000184 this->addTextureAccess(&fMaskAccess);
185 }
186
187 virtual bool onIsEqual(const GrEffect& other) const SK_OVERRIDE {
188 const ThresholdEffect& e = CastEffect<ThresholdEffect>(other);
189 return e.fBmpAccess.getTexture() == fBmpAccess.getTexture() &&
190 e.fMaskAccess.getTexture() == fMaskAccess.getTexture() &&
bsalomon@google.com77af6802013-10-02 13:04:56 +0000191 e.fBmpTransform.getMatrix() == fBmpTransform.getMatrix() &&
192 e.fMaskTransform.getMatrix() == fMaskTransform.getMatrix() &&
commit-bot@chromium.org91a798f2013-09-06 15:31:06 +0000193 e.fThreshold == fThreshold;
194 }
195
bsalomon@google.com77af6802013-10-02 13:04:56 +0000196 GrCoordTransform fBmpTransform;
197 GrTextureAccess fBmpAccess;
198 GrCoordTransform fMaskTransform;
199 GrTextureAccess fMaskAccess;
commit-bot@chromium.org91a798f2013-09-06 15:31:06 +0000200
201 SkScalar fThreshold;
202};
203
204GR_DEFINE_EFFECT_TEST(ThresholdEffect);
205
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000206GrEffectRef* ThresholdEffect::TestCreate(SkRandom* rand,
commit-bot@chromium.org91a798f2013-09-06 15:31:06 +0000207 GrContext*,
208 const GrDrawTargetCaps&,
209 GrTexture* textures[]) {
210 GrTexture* bmpTex = textures[GrEffectUnitTest::kSkiaPMTextureIdx];
211 GrTexture* maskTex = textures[GrEffectUnitTest::kAlphaTextureIdx];
212 U8CPU thresh = rand->nextU() % 0xff;
213 return ThresholdEffect::Create(bmpTex, SkMatrix::I(), maskTex, SkMatrix::I(), thresh);
214}
215
216GrEffectRef* BATShader::asNewEffect(GrContext* context, const SkPaint& paint) const {
217 SkMatrix localInverse;
218 if (!this->getLocalMatrix().invert(&localInverse)) {
219 return NULL;
220 }
221
222 GrTextureDesc maskDesc;
commit-bot@chromium.org6b7938f2013-10-15 14:18:16 +0000223 if (context->isConfigRenderable(kAlpha_8_GrPixelConfig, false)) {
commit-bot@chromium.org91a798f2013-09-06 15:31:06 +0000224 maskDesc.fConfig = kAlpha_8_GrPixelConfig;
225 } else {
226 maskDesc.fConfig = kRGBA_8888_GrPixelConfig;
227 }
228 maskDesc.fFlags = kRenderTarget_GrTextureFlagBit | kNoStencil_GrTextureFlagBit;
229 const SkIRect& bounds = fRegion.getBounds();
230 // Add one pixel of border to ensure that clamp mode will be all zeros
231 // the outside.
232 maskDesc.fWidth = bounds.width() + 2;
233 maskDesc.fHeight = bounds.height() + 2;
234 GrAutoScratchTexture ast(context, maskDesc, GrContext::kApprox_ScratchTexMatch);
235 GrTexture* maskTexture = ast.texture();
236 if (NULL == maskTexture) {
237 return NULL;
238 }
239
240 GrPaint grPaint;
241 grPaint.setBlendFunc(kOne_GrBlendCoeff, kZero_GrBlendCoeff);
242 SkRegion::Iterator iter(fRegion);
243 context->setRenderTarget(maskTexture->asRenderTarget());
robertphillips@google.com56ce48a2013-10-31 21:44:25 +0000244 context->clear(NULL, 0x0, true);
commit-bot@chromium.org91a798f2013-09-06 15:31:06 +0000245
246 // offset to ensure border is zero on top/left
247 SkMatrix matrix;
248 matrix.setTranslate(SK_Scalar1, SK_Scalar1);
249 context->setMatrix(matrix);
250
251 while (!iter.done()) {
reed@google.com44699382013-10-31 17:28:30 +0000252 SkRect rect = SkRect::Make(iter.rect());
commit-bot@chromium.org91a798f2013-09-06 15:31:06 +0000253 context->drawRect(grPaint, rect);
254 iter.next();
255 }
256
257 GrTexture* bmpTexture = GrLockAndRefCachedBitmapTexture(context, fBitmap, NULL);
258 if (NULL == bmpTexture) {
259 return NULL;
260 }
261
262 SkMatrix bmpMatrix = localInverse;
263 bmpMatrix.postIDiv(bmpTexture->width(), bmpTexture->height());
264
265 SkMatrix maskMatrix = localInverse;
266 // compensate for the border
267 maskMatrix.postTranslate(SK_Scalar1, SK_Scalar1);
268 maskMatrix.postIDiv(maskTexture->width(), maskTexture->height());
269
270 GrEffectRef* effect = ThresholdEffect::Create(bmpTexture, bmpMatrix,
271 maskTexture, maskMatrix,
272 fThreshold);
273
274 GrUnlockAndUnrefCachedBitmapTexture(bmpTexture);
275
276 return effect;
277}
278
279#endif