commit-bot@chromium.org | 40eb3c1 | 2014-01-06 23:41:14 +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 "SkAlphaThresholdFilter.h" |
| 9 | #include "SkBitmap.h" |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 10 | #include "SkReadBuffer.h" |
| 11 | #include "SkWriteBuffer.h" |
commit-bot@chromium.org | 40eb3c1 | 2014-01-06 23:41:14 +0000 | [diff] [blame] | 12 | #include "SkRegion.h" |
| 13 | |
| 14 | class SK_API SkAlphaThresholdFilterImpl : public SkImageFilter { |
| 15 | public: |
senorblanco | 9ea3d57 | 2014-07-08 09:16:22 -0700 | [diff] [blame] | 16 | SkAlphaThresholdFilterImpl(const SkRegion& region, SkScalar innerThreshold, |
| 17 | SkScalar outerThreshold, SkImageFilter* input); |
commit-bot@chromium.org | 40eb3c1 | 2014-01-06 23:41:14 +0000 | [diff] [blame] | 18 | |
| 19 | SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(SkAlphaThresholdFilterImpl) |
| 20 | |
| 21 | protected: |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 22 | #ifdef SK_SUPPORT_LEGACY_DEEPFLATTENING |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 23 | explicit SkAlphaThresholdFilterImpl(SkReadBuffer& buffer); |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 24 | #endif |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 25 | virtual void flatten(SkWriteBuffer&) const SK_OVERRIDE; |
commit-bot@chromium.org | 40eb3c1 | 2014-01-06 23:41:14 +0000 | [diff] [blame] | 26 | |
senorblanco@chromium.org | 4cb543d | 2014-03-14 15:44:01 +0000 | [diff] [blame] | 27 | virtual bool onFilterImage(Proxy*, const SkBitmap& src, const Context&, |
commit-bot@chromium.org | ae761f7 | 2014-02-05 22:32:02 +0000 | [diff] [blame] | 28 | SkBitmap* result, SkIPoint* offset) const SK_OVERRIDE; |
commit-bot@chromium.org | 40eb3c1 | 2014-01-06 23:41:14 +0000 | [diff] [blame] | 29 | #if SK_SUPPORT_GPU |
bsalomon | 97b9ab7 | 2014-07-08 06:52:35 -0700 | [diff] [blame] | 30 | virtual bool asNewEffect(GrEffect** effect, GrTexture* texture, |
commit-bot@chromium.org | 40eb3c1 | 2014-01-06 23:41:14 +0000 | [diff] [blame] | 31 | const SkMatrix& matrix, const SkIRect& bounds) const SK_OVERRIDE; |
| 32 | #endif |
| 33 | |
| 34 | private: |
| 35 | SkRegion fRegion; |
| 36 | SkScalar fInnerThreshold; |
| 37 | SkScalar fOuterThreshold; |
| 38 | typedef SkImageFilter INHERITED; |
| 39 | }; |
| 40 | |
| 41 | SkImageFilter* SkAlphaThresholdFilter::Create(const SkRegion& region, |
| 42 | SkScalar innerThreshold, |
senorblanco | 9ea3d57 | 2014-07-08 09:16:22 -0700 | [diff] [blame] | 43 | SkScalar outerThreshold, |
| 44 | SkImageFilter* input) { |
| 45 | return SkNEW_ARGS(SkAlphaThresholdFilterImpl, (region, innerThreshold, outerThreshold, input)); |
commit-bot@chromium.org | 40eb3c1 | 2014-01-06 23:41:14 +0000 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | #if SK_SUPPORT_GPU |
| 49 | #include "GrContext.h" |
| 50 | #include "GrCoordTransform.h" |
| 51 | #include "GrEffect.h" |
| 52 | #include "gl/GrGLEffect.h" |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 53 | #include "gl/builders/GrGLProgramBuilder.h" |
commit-bot@chromium.org | 40eb3c1 | 2014-01-06 23:41:14 +0000 | [diff] [blame] | 54 | #include "GrTBackendEffectFactory.h" |
| 55 | #include "GrTextureAccess.h" |
| 56 | |
| 57 | #include "SkGr.h" |
| 58 | |
| 59 | class GrGLAlphaThresholdEffect; |
| 60 | |
| 61 | class AlphaThresholdEffect : public GrEffect { |
| 62 | |
| 63 | public: |
bsalomon | 83d081a | 2014-07-08 09:56:10 -0700 | [diff] [blame] | 64 | static GrEffect* Create(GrTexture* texture, |
| 65 | GrTexture* maskTexture, |
| 66 | float innerThreshold, |
| 67 | float outerThreshold) { |
bsalomon | 55fad7a | 2014-07-08 07:34:20 -0700 | [diff] [blame] | 68 | return SkNEW_ARGS(AlphaThresholdEffect, (texture, |
| 69 | maskTexture, |
| 70 | innerThreshold, |
| 71 | outerThreshold)); |
commit-bot@chromium.org | 40eb3c1 | 2014-01-06 23:41:14 +0000 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | virtual ~AlphaThresholdEffect() {}; |
| 75 | |
| 76 | static const char* Name() { return "Alpha Threshold"; } |
| 77 | |
| 78 | virtual const GrBackendEffectFactory& getFactory() const SK_OVERRIDE; |
| 79 | virtual void getConstantColorComponents(GrColor* color, uint32_t* validFlags) const SK_OVERRIDE; |
| 80 | |
| 81 | float innerThreshold() const { return fInnerThreshold; } |
| 82 | float outerThreshold() const { return fOuterThreshold; } |
| 83 | |
| 84 | typedef GrGLAlphaThresholdEffect GLEffect; |
| 85 | |
| 86 | private: |
| 87 | AlphaThresholdEffect(GrTexture* texture, |
| 88 | GrTexture* maskTexture, |
| 89 | float innerThreshold, |
| 90 | float outerThreshold) |
| 91 | : fInnerThreshold(innerThreshold) |
| 92 | , fOuterThreshold(outerThreshold) |
bsalomon | 6267f81 | 2014-08-29 15:05:53 -0700 | [diff] [blame^] | 93 | , fImageCoordTransform(kLocal_GrCoordSet, |
| 94 | GrCoordTransform::MakeDivByTextureWHMatrix(texture), texture) |
commit-bot@chromium.org | 40eb3c1 | 2014-01-06 23:41:14 +0000 | [diff] [blame] | 95 | , fImageTextureAccess(texture) |
bsalomon | 6267f81 | 2014-08-29 15:05:53 -0700 | [diff] [blame^] | 96 | , fMaskCoordTransform(kLocal_GrCoordSet, |
| 97 | GrCoordTransform::MakeDivByTextureWHMatrix(maskTexture), maskTexture) |
commit-bot@chromium.org | 40eb3c1 | 2014-01-06 23:41:14 +0000 | [diff] [blame] | 98 | , fMaskTextureAccess(maskTexture) { |
| 99 | this->addCoordTransform(&fImageCoordTransform); |
| 100 | this->addTextureAccess(&fImageTextureAccess); |
| 101 | this->addCoordTransform(&fMaskCoordTransform); |
| 102 | this->addTextureAccess(&fMaskTextureAccess); |
| 103 | } |
| 104 | |
| 105 | virtual bool onIsEqual(const GrEffect&) const SK_OVERRIDE; |
| 106 | |
| 107 | GR_DECLARE_EFFECT_TEST; |
| 108 | |
| 109 | float fInnerThreshold; |
| 110 | float fOuterThreshold; |
| 111 | GrCoordTransform fImageCoordTransform; |
| 112 | GrTextureAccess fImageTextureAccess; |
| 113 | GrCoordTransform fMaskCoordTransform; |
| 114 | GrTextureAccess fMaskTextureAccess; |
| 115 | |
| 116 | typedef GrEffect INHERITED; |
| 117 | }; |
| 118 | |
| 119 | class GrGLAlphaThresholdEffect : public GrGLEffect { |
| 120 | public: |
| 121 | GrGLAlphaThresholdEffect(const GrBackendEffectFactory&, const GrDrawEffect&); |
| 122 | |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 123 | virtual void emitCode(GrGLProgramBuilder*, |
commit-bot@chromium.org | 40eb3c1 | 2014-01-06 23:41:14 +0000 | [diff] [blame] | 124 | const GrDrawEffect&, |
bsalomon | 63e99f7 | 2014-07-21 08:03:14 -0700 | [diff] [blame] | 125 | const GrEffectKey&, |
commit-bot@chromium.org | 40eb3c1 | 2014-01-06 23:41:14 +0000 | [diff] [blame] | 126 | const char* outputColor, |
| 127 | const char* inputColor, |
| 128 | const TransformedCoordsArray&, |
| 129 | const TextureSamplerArray&) SK_OVERRIDE; |
| 130 | |
kkinnunen | 7510b22 | 2014-07-30 00:04:16 -0700 | [diff] [blame] | 131 | virtual void setData(const GrGLProgramDataManager&, const GrDrawEffect&) SK_OVERRIDE; |
commit-bot@chromium.org | 40eb3c1 | 2014-01-06 23:41:14 +0000 | [diff] [blame] | 132 | |
| 133 | private: |
| 134 | |
kkinnunen | 7510b22 | 2014-07-30 00:04:16 -0700 | [diff] [blame] | 135 | GrGLProgramDataManager::UniformHandle fInnerThresholdVar; |
| 136 | GrGLProgramDataManager::UniformHandle fOuterThresholdVar; |
commit-bot@chromium.org | 40eb3c1 | 2014-01-06 23:41:14 +0000 | [diff] [blame] | 137 | |
| 138 | typedef GrGLEffect INHERITED; |
| 139 | }; |
| 140 | |
| 141 | GrGLAlphaThresholdEffect::GrGLAlphaThresholdEffect(const GrBackendEffectFactory& factory, const GrDrawEffect&) |
| 142 | : INHERITED(factory) { |
| 143 | } |
| 144 | |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 145 | void GrGLAlphaThresholdEffect::emitCode(GrGLProgramBuilder* builder, |
commit-bot@chromium.org | 40eb3c1 | 2014-01-06 23:41:14 +0000 | [diff] [blame] | 146 | const GrDrawEffect&, |
bsalomon | 63e99f7 | 2014-07-21 08:03:14 -0700 | [diff] [blame] | 147 | const GrEffectKey& key, |
commit-bot@chromium.org | 40eb3c1 | 2014-01-06 23:41:14 +0000 | [diff] [blame] | 148 | const char* outputColor, |
| 149 | const char* inputColor, |
| 150 | const TransformedCoordsArray& coords, |
| 151 | const TextureSamplerArray& samplers) { |
commit-bot@chromium.org | 40eb3c1 | 2014-01-06 23:41:14 +0000 | [diff] [blame] | 152 | fInnerThresholdVar = builder->addUniform( |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 153 | GrGLProgramBuilder::kFragment_Visibility, |
commit-bot@chromium.org | 40eb3c1 | 2014-01-06 23:41:14 +0000 | [diff] [blame] | 154 | kFloat_GrSLType, "inner_threshold"); |
| 155 | fOuterThresholdVar = builder->addUniform( |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 156 | GrGLProgramBuilder::kFragment_Visibility, |
commit-bot@chromium.org | 40eb3c1 | 2014-01-06 23:41:14 +0000 | [diff] [blame] | 157 | kFloat_GrSLType, "outer_threshold"); |
| 158 | |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 159 | GrGLFragmentShaderBuilder* fsBuilder = builder->getFragmentShaderBuilder(); |
| 160 | SkString coords2D = fsBuilder->ensureFSCoords2D(coords, 0); |
| 161 | SkString maskCoords2D = fsBuilder->ensureFSCoords2D(coords, 1); |
commit-bot@chromium.org | 40eb3c1 | 2014-01-06 23:41:14 +0000 | [diff] [blame] | 162 | |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 163 | fsBuilder->codeAppendf("\t\tvec2 coord = %s;\n", coords2D.c_str()); |
| 164 | fsBuilder->codeAppendf("\t\tvec2 mask_coord = %s;\n", maskCoords2D.c_str()); |
| 165 | fsBuilder->codeAppend("\t\tvec4 input_color = "); |
| 166 | fsBuilder->appendTextureLookup(samplers[0], "coord"); |
| 167 | fsBuilder->codeAppend(";\n"); |
| 168 | fsBuilder->codeAppend("\t\tvec4 mask_color = "); |
| 169 | fsBuilder->appendTextureLookup(samplers[1], "mask_coord"); |
| 170 | fsBuilder->codeAppend(";\n"); |
| 171 | |
| 172 | fsBuilder->codeAppendf("\t\tfloat inner_thresh = %s;\n", |
commit-bot@chromium.org | 40eb3c1 | 2014-01-06 23:41:14 +0000 | [diff] [blame] | 173 | builder->getUniformCStr(fInnerThresholdVar)); |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 174 | fsBuilder->codeAppendf("\t\tfloat outer_thresh = %s;\n", |
commit-bot@chromium.org | 40eb3c1 | 2014-01-06 23:41:14 +0000 | [diff] [blame] | 175 | builder->getUniformCStr(fOuterThresholdVar)); |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 176 | fsBuilder->codeAppend("\t\tfloat mask = mask_color.a;\n"); |
commit-bot@chromium.org | 40eb3c1 | 2014-01-06 23:41:14 +0000 | [diff] [blame] | 177 | |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 178 | fsBuilder->codeAppend("vec4 color = input_color;\n"); |
| 179 | fsBuilder->codeAppend("\t\tif (mask < 0.5) {\n" |
commit-bot@chromium.org | 40eb3c1 | 2014-01-06 23:41:14 +0000 | [diff] [blame] | 180 | "\t\t\tif (color.a > outer_thresh) {\n" |
| 181 | "\t\t\t\tfloat scale = outer_thresh / color.a;\n" |
| 182 | "\t\t\t\tcolor.rgb *= scale;\n" |
| 183 | "\t\t\t\tcolor.a = outer_thresh;\n" |
| 184 | "\t\t\t}\n" |
| 185 | "\t\t} else if (color.a < inner_thresh) {\n" |
| 186 | "\t\t\tfloat scale = inner_thresh / max(0.001, color.a);\n" |
| 187 | "\t\t\tcolor.rgb *= scale;\n" |
| 188 | "\t\t\tcolor.a = inner_thresh;\n" |
| 189 | "\t\t}\n"); |
| 190 | |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 191 | fsBuilder->codeAppendf("%s = %s;\n", outputColor, |
commit-bot@chromium.org | 40eb3c1 | 2014-01-06 23:41:14 +0000 | [diff] [blame] | 192 | (GrGLSLExpr4(inputColor) * GrGLSLExpr4("color")).c_str()); |
| 193 | } |
| 194 | |
kkinnunen | 7510b22 | 2014-07-30 00:04:16 -0700 | [diff] [blame] | 195 | void GrGLAlphaThresholdEffect::setData(const GrGLProgramDataManager& pdman, |
commit-bot@chromium.org | 40eb3c1 | 2014-01-06 23:41:14 +0000 | [diff] [blame] | 196 | const GrDrawEffect& drawEffect) { |
| 197 | const AlphaThresholdEffect& alpha_threshold = |
| 198 | drawEffect.castEffect<AlphaThresholdEffect>(); |
kkinnunen | 7510b22 | 2014-07-30 00:04:16 -0700 | [diff] [blame] | 199 | pdman.set1f(fInnerThresholdVar, alpha_threshold.innerThreshold()); |
| 200 | pdman.set1f(fOuterThresholdVar, alpha_threshold.outerThreshold()); |
commit-bot@chromium.org | 40eb3c1 | 2014-01-06 23:41:14 +0000 | [diff] [blame] | 201 | } |
| 202 | |
| 203 | ///////////////////////////////////////////////////////////////////// |
| 204 | |
| 205 | GR_DEFINE_EFFECT_TEST(AlphaThresholdEffect); |
| 206 | |
bsalomon | 83d081a | 2014-07-08 09:56:10 -0700 | [diff] [blame] | 207 | GrEffect* AlphaThresholdEffect::TestCreate(SkRandom* random, |
| 208 | GrContext* context, |
| 209 | const GrDrawTargetCaps&, |
| 210 | GrTexture** textures) { |
commit-bot@chromium.org | 40eb3c1 | 2014-01-06 23:41:14 +0000 | [diff] [blame] | 211 | GrTexture* bmpTex = textures[GrEffectUnitTest::kSkiaPMTextureIdx]; |
| 212 | GrTexture* maskTex = textures[GrEffectUnitTest::kAlphaTextureIdx]; |
| 213 | float inner_thresh = random->nextUScalar1(); |
| 214 | float outer_thresh = random->nextUScalar1(); |
| 215 | return AlphaThresholdEffect::Create(bmpTex, maskTex, inner_thresh, outer_thresh); |
| 216 | } |
| 217 | |
| 218 | /////////////////////////////////////////////////////////////////////////////// |
| 219 | |
| 220 | const GrBackendEffectFactory& AlphaThresholdEffect::getFactory() const { |
| 221 | return GrTBackendEffectFactory<AlphaThresholdEffect>::getInstance(); |
| 222 | } |
| 223 | |
| 224 | bool AlphaThresholdEffect::onIsEqual(const GrEffect& sBase) const { |
| 225 | const AlphaThresholdEffect& s = CastEffect<AlphaThresholdEffect>(sBase); |
| 226 | return (this->texture(0) == s.texture(0) && |
| 227 | this->fInnerThreshold == s.fInnerThreshold && |
| 228 | this->fOuterThreshold == s.fOuterThreshold); |
| 229 | } |
| 230 | |
| 231 | void AlphaThresholdEffect::getConstantColorComponents(GrColor* color, uint32_t* validFlags) const { |
| 232 | if ((*validFlags & kA_GrColorComponentFlag) && 0xFF == GrColorUnpackA(*color) && |
| 233 | GrPixelConfigIsOpaque(this->texture(0)->config())) { |
| 234 | *validFlags = kA_GrColorComponentFlag; |
| 235 | } else { |
| 236 | *validFlags = 0; |
| 237 | } |
| 238 | } |
| 239 | |
| 240 | #endif |
| 241 | |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 242 | #ifdef SK_SUPPORT_LEGACY_DEEPFLATTENING |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 243 | SkAlphaThresholdFilterImpl::SkAlphaThresholdFilterImpl(SkReadBuffer& buffer) |
commit-bot@chromium.org | 40eb3c1 | 2014-01-06 23:41:14 +0000 | [diff] [blame] | 244 | : INHERITED(1, buffer) { |
| 245 | fInnerThreshold = buffer.readScalar(); |
| 246 | fOuterThreshold = buffer.readScalar(); |
| 247 | buffer.readRegion(&fRegion); |
| 248 | } |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 249 | #endif |
| 250 | |
| 251 | SkFlattenable* SkAlphaThresholdFilterImpl::CreateProc(SkReadBuffer& buffer) { |
| 252 | SK_IMAGEFILTER_UNFLATTEN_COMMON(common, 1); |
| 253 | SkScalar inner = buffer.readScalar(); |
| 254 | SkScalar outer = buffer.readScalar(); |
| 255 | SkRegion rgn; |
| 256 | buffer.readRegion(&rgn); |
| 257 | return SkAlphaThresholdFilter::Create(rgn, inner, outer, common.getInput(0)); |
| 258 | } |
commit-bot@chromium.org | 40eb3c1 | 2014-01-06 23:41:14 +0000 | [diff] [blame] | 259 | |
| 260 | SkAlphaThresholdFilterImpl::SkAlphaThresholdFilterImpl(const SkRegion& region, |
| 261 | SkScalar innerThreshold, |
senorblanco | 9ea3d57 | 2014-07-08 09:16:22 -0700 | [diff] [blame] | 262 | SkScalar outerThreshold, |
| 263 | SkImageFilter* input) |
| 264 | : INHERITED(1, &input) |
commit-bot@chromium.org | 40eb3c1 | 2014-01-06 23:41:14 +0000 | [diff] [blame] | 265 | , fRegion(region) |
| 266 | , fInnerThreshold(innerThreshold) |
| 267 | , fOuterThreshold(outerThreshold) { |
| 268 | } |
| 269 | |
| 270 | #if SK_SUPPORT_GPU |
bsalomon | 97b9ab7 | 2014-07-08 06:52:35 -0700 | [diff] [blame] | 271 | bool SkAlphaThresholdFilterImpl::asNewEffect(GrEffect** effect, GrTexture* texture, |
commit-bot@chromium.org | 40eb3c1 | 2014-01-06 23:41:14 +0000 | [diff] [blame] | 272 | const SkMatrix& in_matrix, const SkIRect&) const { |
| 273 | if (effect) { |
| 274 | GrContext* context = texture->getContext(); |
| 275 | GrTextureDesc maskDesc; |
| 276 | if (context->isConfigRenderable(kAlpha_8_GrPixelConfig, false)) { |
| 277 | maskDesc.fConfig = kAlpha_8_GrPixelConfig; |
| 278 | } else { |
| 279 | maskDesc.fConfig = kRGBA_8888_GrPixelConfig; |
| 280 | } |
| 281 | maskDesc.fFlags = kRenderTarget_GrTextureFlagBit | kNoStencil_GrTextureFlagBit; |
| 282 | // Add one pixel of border to ensure that clamp mode will be all zeros |
| 283 | // the outside. |
| 284 | maskDesc.fWidth = texture->width(); |
| 285 | maskDesc.fHeight = texture->height(); |
| 286 | GrAutoScratchTexture ast(context, maskDesc, GrContext::kApprox_ScratchTexMatch); |
| 287 | GrTexture* maskTexture = ast.texture(); |
| 288 | if (NULL == maskTexture) { |
| 289 | return false; |
| 290 | } |
| 291 | |
| 292 | { |
| 293 | GrContext::AutoRenderTarget art(context, ast.texture()->asRenderTarget()); |
| 294 | GrPaint grPaint; |
| 295 | grPaint.setBlendFunc(kOne_GrBlendCoeff, kZero_GrBlendCoeff); |
| 296 | SkRegion::Iterator iter(fRegion); |
| 297 | context->clear(NULL, 0x0, true); |
| 298 | |
| 299 | SkMatrix old_matrix = context->getMatrix(); |
| 300 | context->setMatrix(in_matrix); |
| 301 | |
| 302 | while (!iter.done()) { |
| 303 | SkRect rect = SkRect::Make(iter.rect()); |
| 304 | context->drawRect(grPaint, rect); |
| 305 | iter.next(); |
| 306 | } |
| 307 | context->setMatrix(old_matrix); |
| 308 | } |
| 309 | |
| 310 | *effect = AlphaThresholdEffect::Create(texture, |
| 311 | maskTexture, |
| 312 | fInnerThreshold, |
| 313 | fOuterThreshold); |
| 314 | } |
| 315 | return true; |
| 316 | } |
| 317 | #endif |
| 318 | |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 319 | void SkAlphaThresholdFilterImpl::flatten(SkWriteBuffer& buffer) const { |
commit-bot@chromium.org | 40eb3c1 | 2014-01-06 23:41:14 +0000 | [diff] [blame] | 320 | this->INHERITED::flatten(buffer); |
| 321 | buffer.writeScalar(fInnerThreshold); |
| 322 | buffer.writeScalar(fOuterThreshold); |
| 323 | buffer.writeRegion(fRegion); |
| 324 | } |
| 325 | |
| 326 | bool SkAlphaThresholdFilterImpl::onFilterImage(Proxy*, const SkBitmap& src, |
senorblanco@chromium.org | 4cb543d | 2014-03-14 15:44:01 +0000 | [diff] [blame] | 327 | const Context& ctx, SkBitmap* dst, |
commit-bot@chromium.org | ae761f7 | 2014-02-05 22:32:02 +0000 | [diff] [blame] | 328 | SkIPoint* offset) const { |
commit-bot@chromium.org | 28fcae2 | 2014-04-11 17:15:40 +0000 | [diff] [blame] | 329 | SkASSERT(src.colorType() == kN32_SkColorType); |
commit-bot@chromium.org | 40eb3c1 | 2014-01-06 23:41:14 +0000 | [diff] [blame] | 330 | |
commit-bot@chromium.org | 28fcae2 | 2014-04-11 17:15:40 +0000 | [diff] [blame] | 331 | if (src.colorType() != kN32_SkColorType) { |
commit-bot@chromium.org | 9109e18 | 2014-01-07 16:04:01 +0000 | [diff] [blame] | 332 | return false; |
commit-bot@chromium.org | 40eb3c1 | 2014-01-06 23:41:14 +0000 | [diff] [blame] | 333 | } |
| 334 | |
| 335 | SkMatrix localInverse; |
senorblanco@chromium.org | 4cb543d | 2014-03-14 15:44:01 +0000 | [diff] [blame] | 336 | if (!ctx.ctm().invert(&localInverse)) { |
commit-bot@chromium.org | 9109e18 | 2014-01-07 16:04:01 +0000 | [diff] [blame] | 337 | return false; |
commit-bot@chromium.org | 40eb3c1 | 2014-01-06 23:41:14 +0000 | [diff] [blame] | 338 | } |
| 339 | |
| 340 | SkAutoLockPixels alp(src); |
| 341 | SkASSERT(src.getPixels()); |
| 342 | if (!src.getPixels() || src.width() <= 0 || src.height() <= 0) { |
commit-bot@chromium.org | 9109e18 | 2014-01-07 16:04:01 +0000 | [diff] [blame] | 343 | return false; |
commit-bot@chromium.org | 40eb3c1 | 2014-01-06 23:41:14 +0000 | [diff] [blame] | 344 | } |
| 345 | |
reed | c77392e | 2014-06-02 13:07:26 -0700 | [diff] [blame] | 346 | if (!dst->allocPixels(src.info())) { |
commit-bot@chromium.org | 40eb3c1 | 2014-01-06 23:41:14 +0000 | [diff] [blame] | 347 | return false; |
| 348 | } |
| 349 | |
commit-bot@chromium.org | 9109e18 | 2014-01-07 16:04:01 +0000 | [diff] [blame] | 350 | U8CPU innerThreshold = (U8CPU)(fInnerThreshold * 0xFF); |
| 351 | U8CPU outerThreshold = (U8CPU)(fOuterThreshold * 0xFF); |
commit-bot@chromium.org | 40eb3c1 | 2014-01-06 23:41:14 +0000 | [diff] [blame] | 352 | SkColor* sptr = src.getAddr32(0, 0); |
| 353 | SkColor* dptr = dst->getAddr32(0, 0); |
| 354 | int width = src.width(), height = src.height(); |
| 355 | for (int y = 0; y < height; ++y) { |
| 356 | for (int x = 0; x < width; ++x) { |
| 357 | const SkColor& source = sptr[y * width + x]; |
| 358 | SkColor output_color(source); |
| 359 | SkPoint position; |
commit-bot@chromium.org | 9109e18 | 2014-01-07 16:04:01 +0000 | [diff] [blame] | 360 | localInverse.mapXY((SkScalar)x, (SkScalar)y, &position); |
| 361 | if (fRegion.contains((int32_t)position.x(), (int32_t)position.y())) { |
commit-bot@chromium.org | 40eb3c1 | 2014-01-06 23:41:14 +0000 | [diff] [blame] | 362 | if (SkColorGetA(source) < innerThreshold) { |
| 363 | U8CPU alpha = SkColorGetA(source); |
| 364 | if (alpha == 0) |
| 365 | alpha = 1; |
| 366 | float scale = (float)innerThreshold / alpha; |
| 367 | output_color = SkColorSetARGB(innerThreshold, |
commit-bot@chromium.org | 9109e18 | 2014-01-07 16:04:01 +0000 | [diff] [blame] | 368 | (U8CPU)(SkColorGetR(source) * scale), |
| 369 | (U8CPU)(SkColorGetG(source) * scale), |
| 370 | (U8CPU)(SkColorGetB(source) * scale)); |
commit-bot@chromium.org | 40eb3c1 | 2014-01-06 23:41:14 +0000 | [diff] [blame] | 371 | } |
| 372 | } else { |
| 373 | if (SkColorGetA(source) > outerThreshold) { |
| 374 | float scale = (float)outerThreshold / SkColorGetA(source); |
| 375 | output_color = SkColorSetARGB(outerThreshold, |
commit-bot@chromium.org | 9109e18 | 2014-01-07 16:04:01 +0000 | [diff] [blame] | 376 | (U8CPU)(SkColorGetR(source) * scale), |
| 377 | (U8CPU)(SkColorGetG(source) * scale), |
| 378 | (U8CPU)(SkColorGetB(source) * scale)); |
commit-bot@chromium.org | 40eb3c1 | 2014-01-06 23:41:14 +0000 | [diff] [blame] | 379 | } |
| 380 | } |
| 381 | dptr[y * dst->width() + x] = output_color; |
| 382 | } |
| 383 | } |
| 384 | |
| 385 | return true; |
| 386 | } |