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