| commit-bot@chromium.org | 91a798f | 2013-09-06 15:31:06 +0000 | [diff] [blame] | 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 | class BATShader : public SkShader { |
| 11 | public: |
| 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.com | b3ec29d | 2013-09-07 07:01:16 +0000 | [diff] [blame^] | 18 | |
| 19 | |
| commit-bot@chromium.org | 91a798f | 2013-09-06 15:31:06 +0000 | [diff] [blame] | 20 | virtual void shadeSpan(int x, int y, SkPMColor[], int count) SK_OVERRIDE {}; |
| skia.committer@gmail.com | b3ec29d | 2013-09-07 07:01:16 +0000 | [diff] [blame^] | 21 | |
| commit-bot@chromium.org | 91a798f | 2013-09-06 15:31:06 +0000 | [diff] [blame] | 22 | #if SK_SUPPORT_GPU |
| 23 | virtual GrEffectRef* asNewEffect(GrContext* context, const SkPaint& paint) const SK_OVERRIDE; |
| 24 | #endif |
| skia.committer@gmail.com | b3ec29d | 2013-09-07 07:01:16 +0000 | [diff] [blame^] | 25 | |
| commit-bot@chromium.org | 91a798f | 2013-09-06 15:31:06 +0000 | [diff] [blame] | 26 | SK_DEVELOPER_TO_STRING(); |
| 27 | SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(BATShader) |
| skia.committer@gmail.com | b3ec29d | 2013-09-07 07:01:16 +0000 | [diff] [blame^] | 28 | |
| commit-bot@chromium.org | 91a798f | 2013-09-06 15:31:06 +0000 | [diff] [blame] | 29 | private: |
| 30 | SkBitmap fBitmap; |
| 31 | SkRegion fRegion; |
| 32 | U8CPU fThreshold; |
| skia.committer@gmail.com | b3ec29d | 2013-09-07 07:01:16 +0000 | [diff] [blame^] | 33 | |
| commit-bot@chromium.org | 91a798f | 2013-09-06 15:31:06 +0000 | [diff] [blame] | 34 | typedef SkShader INHERITED; |
| 35 | }; |
| 36 | |
| 37 | SkShader* 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 | |
| 44 | BATShader::BATShader(const SkBitmap& bitmap, SkRegion region, U8CPU threshold) |
| 45 | : fBitmap(bitmap) |
| 46 | , fRegion(region) |
| 47 | , fThreshold(threshold) { |
| 48 | }; |
| 49 | |
| 50 | |
| 51 | #ifdef SK_DEVELOPER |
| 52 | void 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" |
| 65 | #include "GrEffect.h" |
| 66 | #include "gl/GrGLEffect.h" |
| 67 | #include "gl/GrGLEffectMatrix.h" |
| 68 | #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 | */ |
| 79 | class ThresholdEffect : public GrEffect { |
| 80 | public: |
| 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) |
| 112 | , fBmpMatrix(GrEffect::kLocal_CoordsType) |
| 113 | , fMaskMatrix(GrEffect::kLocal_CoordsType) |
| 114 | , fPrevThreshold(-SK_Scalar1) { |
| 115 | } |
| 116 | |
| 117 | virtual void emitCode(GrGLShaderBuilder* builder, |
| 118 | const GrDrawEffect& drawEffect, |
| 119 | EffectKey key, |
| 120 | const char* outputColor, |
| 121 | const char* inputColor, |
| 122 | const TextureSamplerArray& samplers) SK_OVERRIDE { |
| 123 | SkString bmpCoord; |
| 124 | SkString maskCoord; |
| 125 | |
| 126 | GrSLType bmpCoordType = fBmpMatrix.emitCode(builder, key, &bmpCoord, NULL, "Bmp"); |
| 127 | EffectKey maskMatrixKey = key >> GrGLEffectMatrix::kKeyBits; |
| 128 | GrSLType maskCoordType = fMaskMatrix.emitCode(builder, |
| 129 | maskMatrixKey, |
| 130 | &maskCoord, |
| 131 | NULL, |
| 132 | "Mask"); |
| 133 | |
| 134 | // put bitmap color in "color" |
| 135 | builder->fsCodeAppend("\t\tvec4 color = "); |
| 136 | builder->fsAppendTextureLookup(samplers[0], bmpCoord.c_str(), bmpCoordType); |
| 137 | builder->fsCodeAppend(";\n"); |
| 138 | |
| 139 | // put alpha from mask texture in "mask" |
| 140 | builder->fsCodeAppend("\t\tfloat mask = "); |
| 141 | builder->fsAppendTextureLookup(samplers[1], maskCoord.c_str(), maskCoordType); |
| 142 | builder->fsCodeAppend(".a;\n"); |
| 143 | |
| 144 | const char* threshold; |
| 145 | |
| 146 | fThresholdUniHandle = builder->addUniform(GrGLShaderBuilder::kFragment_Visibility, |
| 147 | kFloat_GrSLType, |
| 148 | "threshold", |
| 149 | &threshold); |
| 150 | builder->fsCodeAppendf("\t\tfloat thresh = %s;\n", threshold); |
| 151 | |
| 152 | builder->fsCodeAppend("\t\tif (mask < 0.5) {\n" |
| 153 | "\t\t\tif (color.a > thresh) {\n" |
| 154 | "\t\t\t\tfloat scale = thresh / color.a;\n" |
| 155 | "\t\t\t\tcolor.rgb *= scale;\n" |
| 156 | "\t\t\t\tcolor.a = thresh;\n" |
| 157 | "\t\t\t}\n" |
| 158 | "\t\t} else if (color.a < thresh) {\n" |
| 159 | "\t\t\tfloat scale = thresh / color.a;\n" |
| 160 | "\t\t\tcolor.rgb *= scale;\n" |
| 161 | "\t\t\tcolor.a = thresh;\n" |
| 162 | "\t\t}\n"); |
| 163 | |
| 164 | builder->fsCodeAppend("color = "); |
| 165 | SkString outStr; |
| 166 | outStr.appendf("\t\t%s = ", outputColor); |
| 167 | GrGLSLModulatef<4>(&outStr, inputColor, "color"); |
| 168 | outStr.append(";\n"); |
| 169 | builder->fsCodeAppend(outStr.c_str()); |
| 170 | } |
| 171 | |
| 172 | virtual void setData(const GrGLUniformManager& uman, const GrDrawEffect& e) SK_OVERRIDE { |
| 173 | const ThresholdEffect& effect = e.castEffect<ThresholdEffect>(); |
| 174 | fBmpMatrix.setData(uman, effect.fBmpMatrix, e, effect.fBmpAccess.getTexture()); |
| 175 | fMaskMatrix.setData(uman, effect.fMaskMatrix, e, effect.fMaskAccess.getTexture()); |
| 176 | if (fPrevThreshold != effect.fThreshold) { |
| 177 | uman.set1f(fThresholdUniHandle, effect.fThreshold); |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | static inline EffectKey GenKey(const GrDrawEffect& e, const GrGLCaps&) { |
| 182 | const ThresholdEffect& effect = e.castEffect<ThresholdEffect>(); |
| 183 | |
| 184 | EffectKey bmpMKey = GrGLEffectMatrix::GenKey(effect.fBmpMatrix, |
| 185 | e, |
| 186 | GrEffect::kLocal_CoordsType, |
| 187 | effect.fBmpAccess.getTexture()); |
| 188 | EffectKey maskMKey = GrGLEffectMatrix::GenKey(effect.fMaskMatrix, |
| 189 | e, |
| 190 | GrEffect::kLocal_CoordsType, |
| 191 | effect.fMaskAccess.getTexture()); |
| 192 | return bmpMKey | (maskMKey << GrGLEffectMatrix::kKeyBits); |
| 193 | } |
| 194 | |
| 195 | private: |
| 196 | GrGLEffectMatrix fBmpMatrix; |
| 197 | GrGLEffectMatrix fMaskMatrix; |
| 198 | |
| 199 | GrGLUniformManager::UniformHandle fThresholdUniHandle; |
| 200 | SkScalar fPrevThreshold; |
| 201 | }; |
| 202 | |
| 203 | GR_DECLARE_EFFECT_TEST; |
| 204 | |
| 205 | private: |
| 206 | ThresholdEffect(GrTexture* bmpTexture, const SkMatrix& bmpMatrix, |
| 207 | GrTexture* maskTexture, const SkMatrix& maskMatrix, |
| 208 | SkScalar threshold) |
| 209 | : fBmpAccess(bmpTexture, GrTextureParams()) |
| 210 | , fMaskAccess(maskTexture, GrTextureParams()) |
| 211 | , fBmpMatrix(bmpMatrix) |
| 212 | , fMaskMatrix(maskMatrix) |
| 213 | , fThreshold(threshold) { |
| 214 | this->addTextureAccess(&fBmpAccess); |
| 215 | this->addTextureAccess(&fMaskAccess); |
| 216 | } |
| 217 | |
| 218 | virtual bool onIsEqual(const GrEffect& other) const SK_OVERRIDE { |
| 219 | const ThresholdEffect& e = CastEffect<ThresholdEffect>(other); |
| 220 | return e.fBmpAccess.getTexture() == fBmpAccess.getTexture() && |
| 221 | e.fMaskAccess.getTexture() == fMaskAccess.getTexture() && |
| 222 | e.fBmpMatrix == fBmpMatrix && |
| 223 | e.fMaskMatrix == fMaskMatrix && |
| 224 | e.fThreshold == fThreshold; |
| 225 | } |
| 226 | |
| 227 | GrTextureAccess fBmpAccess; |
| 228 | GrTextureAccess fMaskAccess; |
| 229 | |
| 230 | SkMatrix fBmpMatrix; |
| 231 | SkMatrix fMaskMatrix; |
| 232 | |
| 233 | SkScalar fThreshold; |
| 234 | }; |
| 235 | |
| 236 | GR_DEFINE_EFFECT_TEST(ThresholdEffect); |
| 237 | |
| 238 | GrEffectRef* ThresholdEffect::TestCreate(SkMWCRandom* rand, |
| 239 | GrContext*, |
| 240 | const GrDrawTargetCaps&, |
| 241 | GrTexture* textures[]) { |
| 242 | GrTexture* bmpTex = textures[GrEffectUnitTest::kSkiaPMTextureIdx]; |
| 243 | GrTexture* maskTex = textures[GrEffectUnitTest::kAlphaTextureIdx]; |
| 244 | U8CPU thresh = rand->nextU() % 0xff; |
| 245 | return ThresholdEffect::Create(bmpTex, SkMatrix::I(), maskTex, SkMatrix::I(), thresh); |
| 246 | } |
| 247 | |
| 248 | GrEffectRef* BATShader::asNewEffect(GrContext* context, const SkPaint& paint) const { |
| 249 | SkMatrix localInverse; |
| 250 | if (!this->getLocalMatrix().invert(&localInverse)) { |
| 251 | return NULL; |
| 252 | } |
| 253 | |
| 254 | GrTextureDesc maskDesc; |
| 255 | if (context->isConfigRenderable(kAlpha_8_GrPixelConfig)) { |
| 256 | maskDesc.fConfig = kAlpha_8_GrPixelConfig; |
| 257 | } else { |
| 258 | maskDesc.fConfig = kRGBA_8888_GrPixelConfig; |
| 259 | } |
| 260 | maskDesc.fFlags = kRenderTarget_GrTextureFlagBit | kNoStencil_GrTextureFlagBit; |
| 261 | const SkIRect& bounds = fRegion.getBounds(); |
| 262 | // Add one pixel of border to ensure that clamp mode will be all zeros |
| 263 | // the outside. |
| 264 | maskDesc.fWidth = bounds.width() + 2; |
| 265 | maskDesc.fHeight = bounds.height() + 2; |
| 266 | GrAutoScratchTexture ast(context, maskDesc, GrContext::kApprox_ScratchTexMatch); |
| 267 | GrTexture* maskTexture = ast.texture(); |
| 268 | if (NULL == maskTexture) { |
| 269 | return NULL; |
| 270 | } |
| 271 | |
| 272 | GrPaint grPaint; |
| 273 | grPaint.setBlendFunc(kOne_GrBlendCoeff, kZero_GrBlendCoeff); |
| 274 | SkRegion::Iterator iter(fRegion); |
| 275 | context->setRenderTarget(maskTexture->asRenderTarget()); |
| 276 | context->clear(NULL, 0x0); |
| 277 | |
| 278 | // offset to ensure border is zero on top/left |
| 279 | SkMatrix matrix; |
| 280 | matrix.setTranslate(SK_Scalar1, SK_Scalar1); |
| 281 | context->setMatrix(matrix); |
| 282 | |
| 283 | while (!iter.done()) { |
| 284 | SkRect rect = SkRect::MakeFromIRect(iter.rect()); |
| 285 | context->drawRect(grPaint, rect); |
| 286 | iter.next(); |
| 287 | } |
| 288 | |
| 289 | GrTexture* bmpTexture = GrLockAndRefCachedBitmapTexture(context, fBitmap, NULL); |
| 290 | if (NULL == bmpTexture) { |
| 291 | return NULL; |
| 292 | } |
| 293 | |
| 294 | SkMatrix bmpMatrix = localInverse; |
| 295 | bmpMatrix.postIDiv(bmpTexture->width(), bmpTexture->height()); |
| 296 | |
| 297 | SkMatrix maskMatrix = localInverse; |
| 298 | // compensate for the border |
| 299 | maskMatrix.postTranslate(SK_Scalar1, SK_Scalar1); |
| 300 | maskMatrix.postIDiv(maskTexture->width(), maskTexture->height()); |
| 301 | |
| 302 | GrEffectRef* effect = ThresholdEffect::Create(bmpTexture, bmpMatrix, |
| 303 | maskTexture, maskMatrix, |
| 304 | fThreshold); |
| 305 | |
| 306 | GrUnlockAndUnrefCachedBitmapTexture(bmpTexture); |
| 307 | |
| 308 | return effect; |
| 309 | } |
| 310 | |
| 311 | #endif |