blob: c8db3a56cad6462ed70a019cbc74d124fe976784 [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:
12 SK_DECLARE_INST_COUNT(SkThresholdShader);
13
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
37SkShader* SkBitmapAlphaThresholdShader::Create(const SkBitmap& bitmap,
38 const SkRegion& region,
39 U8CPU threshold) {
40 SkASSERT(threshold < 256);
41 return SkNEW_ARGS(BATShader, (bitmap, region, threshold));
42}
43
44BATShader::BATShader(const SkBitmap& bitmap, SkRegion region, U8CPU threshold)
45: fBitmap(bitmap)
46, fRegion(region)
47, fThreshold(threshold) {
48};
49
50
51#ifdef SK_DEVELOPER
52void BATShader::toString(SkString* str) const {
53 str->append("BATShader: (");
54
55 fBitmap.toString(str);
56
57 this->INHERITED::toString(str);
58
59 str->append(")");
60}
61#endif
62
63#if SK_SUPPORT_GPU
64#include "GrContext.h"
bsalomon@google.com77af6802013-10-02 13:04:56 +000065#include "GrCoordTransform.h"
commit-bot@chromium.org91a798f2013-09-06 15:31:06 +000066#include "GrEffect.h"
67#include "gl/GrGLEffect.h"
commit-bot@chromium.org91a798f2013-09-06 15:31:06 +000068#include "GrTBackendEffectFactory.h"
69#include "GrTextureAccess.h"
70
71#include "SkGr.h"
72
73/**
74 * Could create specializations for some simple cases:
75 * - The region is empty.
76 * - The region fully contains the bitmap.
77 * - The regions is 1 rect (or maybe a small number of rects).
78 */
79class ThresholdEffect : public GrEffect {
80public:
81 virtual const GrBackendEffectFactory& getFactory() const SK_OVERRIDE {
82 return GrTBackendEffectFactory<ThresholdEffect>::getInstance();
83 }
84
85 static GrEffectRef* Create(GrTexture* bmpTexture, const SkMatrix& bmpMatrix,
86 GrTexture* maskTexture, const SkMatrix& maskMatrix,
87 U8CPU threshold) {
88 SkScalar thresh = SkIntToScalar(threshold) / 255;
89
90 AutoEffectUnref effect(SkNEW_ARGS(ThresholdEffect, (bmpTexture, bmpMatrix,
91 maskTexture, maskMatrix,
92 thresh)));
93 return CreateEffectRef(effect);
94 }
95
96 virtual void getConstantColorComponents(GrColor* color,
97 uint32_t* validFlags) const SK_OVERRIDE {
98 if ((kA_GrColorComponentFlag & *validFlags) && 0 == GrColorUnpackA(*color)) {
99 return;
100 }
101 *validFlags = 0;
102 return;
103 }
104
105 static const char* Name() { return "Bitmap Alpha Threshold"; }
106
107 class GLEffect : public GrGLEffect {
108 public:
109 GLEffect(const GrBackendEffectFactory& factory,
110 const GrDrawEffect& e)
111 : GrGLEffect(factory)
commit-bot@chromium.org91a798f2013-09-06 15:31:06 +0000112 , fPrevThreshold(-SK_Scalar1) {
113 }
114
115 virtual void emitCode(GrGLShaderBuilder* builder,
bsalomon@google.com77af6802013-10-02 13:04:56 +0000116 const GrDrawEffect& drawEffect,
117 EffectKey key,
118 const char* outputColor,
119 const char* inputColor,
120 const TransformedCoordsArray& coords,
121 const TextureSamplerArray& samplers) SK_OVERRIDE {
commit-bot@chromium.org91a798f2013-09-06 15:31:06 +0000122 // put bitmap color in "color"
123 builder->fsCodeAppend("\t\tvec4 color = ");
bsalomon@google.com77af6802013-10-02 13:04:56 +0000124 builder->fsAppendTextureLookup(samplers[0], coords[0].c_str(), coords[0].type());
commit-bot@chromium.org91a798f2013-09-06 15:31:06 +0000125 builder->fsCodeAppend(";\n");
126
127 // put alpha from mask texture in "mask"
128 builder->fsCodeAppend("\t\tfloat mask = ");
bsalomon@google.com77af6802013-10-02 13:04:56 +0000129 builder->fsAppendTextureLookup(samplers[1], coords[1].c_str(), coords[1].type());
commit-bot@chromium.org91a798f2013-09-06 15:31:06 +0000130 builder->fsCodeAppend(".a;\n");
131
132 const char* threshold;
133
134 fThresholdUniHandle = builder->addUniform(GrGLShaderBuilder::kFragment_Visibility,
135 kFloat_GrSLType,
136 "threshold",
137 &threshold);
138 builder->fsCodeAppendf("\t\tfloat thresh = %s;\n", threshold);
139
140 builder->fsCodeAppend("\t\tif (mask < 0.5) {\n"
141 "\t\t\tif (color.a > thresh) {\n"
142 "\t\t\t\tfloat scale = thresh / color.a;\n"
143 "\t\t\t\tcolor.rgb *= scale;\n"
144 "\t\t\t\tcolor.a = thresh;\n"
145 "\t\t\t}\n"
146 "\t\t} else if (color.a < thresh) {\n"
147 "\t\t\tfloat scale = thresh / color.a;\n"
148 "\t\t\tcolor.rgb *= scale;\n"
149 "\t\t\tcolor.a = thresh;\n"
150 "\t\t}\n");
151
152 builder->fsCodeAppend("color = ");
153 SkString outStr;
154 outStr.appendf("\t\t%s = ", outputColor);
155 GrGLSLModulatef<4>(&outStr, inputColor, "color");
156 outStr.append(";\n");
157 builder->fsCodeAppend(outStr.c_str());
158 }
159
160 virtual void setData(const GrGLUniformManager& uman, const GrDrawEffect& e) SK_OVERRIDE {
161 const ThresholdEffect& effect = e.castEffect<ThresholdEffect>();
commit-bot@chromium.org91a798f2013-09-06 15:31:06 +0000162 if (fPrevThreshold != effect.fThreshold) {
163 uman.set1f(fThresholdUniHandle, effect.fThreshold);
164 }
165 }
166
commit-bot@chromium.org91a798f2013-09-06 15:31:06 +0000167 private:
commit-bot@chromium.org91a798f2013-09-06 15:31:06 +0000168 GrGLUniformManager::UniformHandle fThresholdUniHandle;
169 SkScalar fPrevThreshold;
170 };
171
172 GR_DECLARE_EFFECT_TEST;
173
174private:
175 ThresholdEffect(GrTexture* bmpTexture, const SkMatrix& bmpMatrix,
176 GrTexture* maskTexture, const SkMatrix& maskMatrix,
177 SkScalar threshold)
bsalomon@google.com77af6802013-10-02 13:04:56 +0000178 : fBmpTransform(kLocal_GrCoordSet, bmpMatrix, bmpTexture)
179 , fBmpAccess(bmpTexture, GrTextureParams())
180 , fMaskTransform(kLocal_GrCoordSet, maskMatrix, maskTexture)
commit-bot@chromium.org91a798f2013-09-06 15:31:06 +0000181 , fMaskAccess(maskTexture, GrTextureParams())
commit-bot@chromium.org91a798f2013-09-06 15:31:06 +0000182 , fThreshold(threshold) {
bsalomon@google.com77af6802013-10-02 13:04:56 +0000183 this->addCoordTransform(&fBmpTransform);
commit-bot@chromium.org91a798f2013-09-06 15:31:06 +0000184 this->addTextureAccess(&fBmpAccess);
bsalomon@google.com77af6802013-10-02 13:04:56 +0000185 this->addCoordTransform(&fMaskTransform);
commit-bot@chromium.org91a798f2013-09-06 15:31:06 +0000186 this->addTextureAccess(&fMaskAccess);
187 }
188
189 virtual bool onIsEqual(const GrEffect& other) const SK_OVERRIDE {
190 const ThresholdEffect& e = CastEffect<ThresholdEffect>(other);
191 return e.fBmpAccess.getTexture() == fBmpAccess.getTexture() &&
192 e.fMaskAccess.getTexture() == fMaskAccess.getTexture() &&
bsalomon@google.com77af6802013-10-02 13:04:56 +0000193 e.fBmpTransform.getMatrix() == fBmpTransform.getMatrix() &&
194 e.fMaskTransform.getMatrix() == fMaskTransform.getMatrix() &&
commit-bot@chromium.org91a798f2013-09-06 15:31:06 +0000195 e.fThreshold == fThreshold;
196 }
197
bsalomon@google.com77af6802013-10-02 13:04:56 +0000198 GrCoordTransform fBmpTransform;
199 GrTextureAccess fBmpAccess;
200 GrCoordTransform fMaskTransform;
201 GrTextureAccess fMaskAccess;
commit-bot@chromium.org91a798f2013-09-06 15:31:06 +0000202
203 SkScalar fThreshold;
204};
205
206GR_DEFINE_EFFECT_TEST(ThresholdEffect);
207
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000208GrEffectRef* ThresholdEffect::TestCreate(SkRandom* rand,
commit-bot@chromium.org91a798f2013-09-06 15:31:06 +0000209 GrContext*,
210 const GrDrawTargetCaps&,
211 GrTexture* textures[]) {
212 GrTexture* bmpTex = textures[GrEffectUnitTest::kSkiaPMTextureIdx];
213 GrTexture* maskTex = textures[GrEffectUnitTest::kAlphaTextureIdx];
214 U8CPU thresh = rand->nextU() % 0xff;
215 return ThresholdEffect::Create(bmpTex, SkMatrix::I(), maskTex, SkMatrix::I(), thresh);
216}
217
218GrEffectRef* BATShader::asNewEffect(GrContext* context, const SkPaint& paint) const {
219 SkMatrix localInverse;
220 if (!this->getLocalMatrix().invert(&localInverse)) {
221 return NULL;
222 }
223
224 GrTextureDesc maskDesc;
225 if (context->isConfigRenderable(kAlpha_8_GrPixelConfig)) {
226 maskDesc.fConfig = kAlpha_8_GrPixelConfig;
227 } else {
228 maskDesc.fConfig = kRGBA_8888_GrPixelConfig;
229 }
230 maskDesc.fFlags = kRenderTarget_GrTextureFlagBit | kNoStencil_GrTextureFlagBit;
231 const SkIRect& bounds = fRegion.getBounds();
232 // Add one pixel of border to ensure that clamp mode will be all zeros
233 // the outside.
234 maskDesc.fWidth = bounds.width() + 2;
235 maskDesc.fHeight = bounds.height() + 2;
236 GrAutoScratchTexture ast(context, maskDesc, GrContext::kApprox_ScratchTexMatch);
237 GrTexture* maskTexture = ast.texture();
238 if (NULL == maskTexture) {
239 return NULL;
240 }
241
242 GrPaint grPaint;
243 grPaint.setBlendFunc(kOne_GrBlendCoeff, kZero_GrBlendCoeff);
244 SkRegion::Iterator iter(fRegion);
245 context->setRenderTarget(maskTexture->asRenderTarget());
246 context->clear(NULL, 0x0);
247
248 // offset to ensure border is zero on top/left
249 SkMatrix matrix;
250 matrix.setTranslate(SK_Scalar1, SK_Scalar1);
251 context->setMatrix(matrix);
252
253 while (!iter.done()) {
254 SkRect rect = SkRect::MakeFromIRect(iter.rect());
255 context->drawRect(grPaint, rect);
256 iter.next();
257 }
258
259 GrTexture* bmpTexture = GrLockAndRefCachedBitmapTexture(context, fBitmap, NULL);
260 if (NULL == bmpTexture) {
261 return NULL;
262 }
263
264 SkMatrix bmpMatrix = localInverse;
265 bmpMatrix.postIDiv(bmpTexture->width(), bmpTexture->height());
266
267 SkMatrix maskMatrix = localInverse;
268 // compensate for the border
269 maskMatrix.postTranslate(SK_Scalar1, SK_Scalar1);
270 maskMatrix.postIDiv(maskTexture->width(), maskTexture->height());
271
272 GrEffectRef* effect = ThresholdEffect::Create(bmpTexture, bmpMatrix,
273 maskTexture, maskMatrix,
274 fThreshold);
275
276 GrUnlockAndUnrefCachedBitmapTexture(bmpTexture);
277
278 return effect;
279}
280
281#endif