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: |
robertphillips@google.com | 0255a5d | 2013-10-24 14:03:01 +0000 | [diff] [blame] | 12 | SK_DECLARE_INST_COUNT(BATShader); |
commit-bot@chromium.org | 91a798f | 2013-09-06 15:31:06 +0000 | [diff] [blame] | 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 | |
robertphillips@google.com | 0255a5d | 2013-10-24 14:03:01 +0000 | [diff] [blame] | 37 | SK_DEFINE_INST_COUNT(BATShader) |
| 38 | |
commit-bot@chromium.org | 91a798f | 2013-09-06 15:31:06 +0000 | [diff] [blame] | 39 | SkShader* 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 | |
| 46 | BATShader::BATShader(const SkBitmap& bitmap, SkRegion region, U8CPU threshold) |
| 47 | : fBitmap(bitmap) |
| 48 | , fRegion(region) |
| 49 | , fThreshold(threshold) { |
| 50 | }; |
| 51 | |
| 52 | |
| 53 | #ifdef SK_DEVELOPER |
| 54 | void 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.com | 77af680 | 2013-10-02 13:04:56 +0000 | [diff] [blame] | 67 | #include "GrCoordTransform.h" |
commit-bot@chromium.org | 91a798f | 2013-09-06 15:31:06 +0000 | [diff] [blame] | 68 | #include "GrEffect.h" |
| 69 | #include "gl/GrGLEffect.h" |
commit-bot@chromium.org | 91a798f | 2013-09-06 15:31:06 +0000 | [diff] [blame] | 70 | #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 | */ |
| 81 | class ThresholdEffect : public GrEffect { |
| 82 | public: |
| 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.org | 91a798f | 2013-09-06 15:31:06 +0000 | [diff] [blame] | 114 | , fPrevThreshold(-SK_Scalar1) { |
| 115 | } |
| 116 | |
| 117 | virtual void emitCode(GrGLShaderBuilder* builder, |
bsalomon@google.com | 77af680 | 2013-10-02 13:04:56 +0000 | [diff] [blame] | 118 | 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.org | 91a798f | 2013-09-06 15:31:06 +0000 | [diff] [blame] | 124 | // put bitmap color in "color" |
| 125 | builder->fsCodeAppend("\t\tvec4 color = "); |
bsalomon@google.com | 77af680 | 2013-10-02 13:04:56 +0000 | [diff] [blame] | 126 | builder->fsAppendTextureLookup(samplers[0], coords[0].c_str(), coords[0].type()); |
commit-bot@chromium.org | 91a798f | 2013-09-06 15:31:06 +0000 | [diff] [blame] | 127 | builder->fsCodeAppend(";\n"); |
| 128 | |
| 129 | // put alpha from mask texture in "mask" |
| 130 | builder->fsCodeAppend("\t\tfloat mask = "); |
bsalomon@google.com | 77af680 | 2013-10-02 13:04:56 +0000 | [diff] [blame] | 131 | builder->fsAppendTextureLookup(samplers[1], coords[1].c_str(), coords[1].type()); |
commit-bot@chromium.org | 91a798f | 2013-09-06 15:31:06 +0000 | [diff] [blame] | 132 | 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.org | 824c346 | 2013-10-10 06:30:18 +0000 | [diff] [blame] | 154 | builder->fsCodeAppendf("color = %s = %s;\n", outputColor, |
commit-bot@chromium.org | a34995e | 2013-10-23 05:42:03 +0000 | [diff] [blame] | 155 | (GrGLSLExpr4(inputColor) * GrGLSLExpr4("color")).c_str()); |
commit-bot@chromium.org | 91a798f | 2013-09-06 15:31:06 +0000 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | virtual void setData(const GrGLUniformManager& uman, const GrDrawEffect& e) SK_OVERRIDE { |
| 159 | const ThresholdEffect& effect = e.castEffect<ThresholdEffect>(); |
commit-bot@chromium.org | 91a798f | 2013-09-06 15:31:06 +0000 | [diff] [blame] | 160 | if (fPrevThreshold != effect.fThreshold) { |
| 161 | uman.set1f(fThresholdUniHandle, effect.fThreshold); |
| 162 | } |
| 163 | } |
| 164 | |
commit-bot@chromium.org | 91a798f | 2013-09-06 15:31:06 +0000 | [diff] [blame] | 165 | private: |
commit-bot@chromium.org | 91a798f | 2013-09-06 15:31:06 +0000 | [diff] [blame] | 166 | GrGLUniformManager::UniformHandle fThresholdUniHandle; |
| 167 | SkScalar fPrevThreshold; |
| 168 | }; |
| 169 | |
| 170 | GR_DECLARE_EFFECT_TEST; |
| 171 | |
| 172 | private: |
| 173 | ThresholdEffect(GrTexture* bmpTexture, const SkMatrix& bmpMatrix, |
| 174 | GrTexture* maskTexture, const SkMatrix& maskMatrix, |
| 175 | SkScalar threshold) |
bsalomon@google.com | 77af680 | 2013-10-02 13:04:56 +0000 | [diff] [blame] | 176 | : fBmpTransform(kLocal_GrCoordSet, bmpMatrix, bmpTexture) |
| 177 | , fBmpAccess(bmpTexture, GrTextureParams()) |
| 178 | , fMaskTransform(kLocal_GrCoordSet, maskMatrix, maskTexture) |
commit-bot@chromium.org | 91a798f | 2013-09-06 15:31:06 +0000 | [diff] [blame] | 179 | , fMaskAccess(maskTexture, GrTextureParams()) |
commit-bot@chromium.org | 91a798f | 2013-09-06 15:31:06 +0000 | [diff] [blame] | 180 | , fThreshold(threshold) { |
bsalomon@google.com | 77af680 | 2013-10-02 13:04:56 +0000 | [diff] [blame] | 181 | this->addCoordTransform(&fBmpTransform); |
commit-bot@chromium.org | 91a798f | 2013-09-06 15:31:06 +0000 | [diff] [blame] | 182 | this->addTextureAccess(&fBmpAccess); |
bsalomon@google.com | 77af680 | 2013-10-02 13:04:56 +0000 | [diff] [blame] | 183 | this->addCoordTransform(&fMaskTransform); |
commit-bot@chromium.org | 91a798f | 2013-09-06 15:31:06 +0000 | [diff] [blame] | 184 | 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.com | 77af680 | 2013-10-02 13:04:56 +0000 | [diff] [blame] | 191 | e.fBmpTransform.getMatrix() == fBmpTransform.getMatrix() && |
| 192 | e.fMaskTransform.getMatrix() == fMaskTransform.getMatrix() && |
commit-bot@chromium.org | 91a798f | 2013-09-06 15:31:06 +0000 | [diff] [blame] | 193 | e.fThreshold == fThreshold; |
| 194 | } |
| 195 | |
bsalomon@google.com | 77af680 | 2013-10-02 13:04:56 +0000 | [diff] [blame] | 196 | GrCoordTransform fBmpTransform; |
| 197 | GrTextureAccess fBmpAccess; |
| 198 | GrCoordTransform fMaskTransform; |
| 199 | GrTextureAccess fMaskAccess; |
commit-bot@chromium.org | 91a798f | 2013-09-06 15:31:06 +0000 | [diff] [blame] | 200 | |
| 201 | SkScalar fThreshold; |
| 202 | }; |
| 203 | |
| 204 | GR_DEFINE_EFFECT_TEST(ThresholdEffect); |
| 205 | |
commit-bot@chromium.org | e0e7cfe | 2013-09-09 20:09:12 +0000 | [diff] [blame] | 206 | GrEffectRef* ThresholdEffect::TestCreate(SkRandom* rand, |
commit-bot@chromium.org | 91a798f | 2013-09-06 15:31:06 +0000 | [diff] [blame] | 207 | 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 | |
| 216 | GrEffectRef* 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.org | 6b7938f | 2013-10-15 14:18:16 +0000 | [diff] [blame] | 223 | if (context->isConfigRenderable(kAlpha_8_GrPixelConfig, false)) { |
commit-bot@chromium.org | 91a798f | 2013-09-06 15:31:06 +0000 | [diff] [blame] | 224 | 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.com | 56ce48a | 2013-10-31 21:44:25 +0000 | [diff] [blame] | 244 | context->clear(NULL, 0x0, true); |
commit-bot@chromium.org | 91a798f | 2013-09-06 15:31:06 +0000 | [diff] [blame] | 245 | |
| 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.com | 4469938 | 2013-10-31 17:28:30 +0000 | [diff] [blame] | 252 | SkRect rect = SkRect::Make(iter.rect()); |
commit-bot@chromium.org | 91a798f | 2013-09-06 15:31:06 +0000 | [diff] [blame] | 253 | 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 |