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