| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2013 Google Inc. | 2 * Copyright 2013 Google Inc. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
| 5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
| 6 */ | 6 */ |
| 7 | 7 |
| 8 #include "SkAlphaThresholdFilter.h" | 8 #include "SkAlphaThresholdFilter.h" |
| 9 #include "SkBitmap.h" | 9 #include "SkBitmap.h" |
| 10 #include "SkDevice.h" | 10 #include "SkDevice.h" |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 61 #include "GrTextureAccess.h" | 61 #include "GrTextureAccess.h" |
| 62 #include "effects/GrPorterDuffXferProcessor.h" | 62 #include "effects/GrPorterDuffXferProcessor.h" |
| 63 | 63 |
| 64 #include "SkGr.h" | 64 #include "SkGr.h" |
| 65 | 65 |
| 66 #include "glsl/GrGLSLFragmentProcessor.h" | 66 #include "glsl/GrGLSLFragmentProcessor.h" |
| 67 #include "glsl/GrGLSLFragmentShaderBuilder.h" | 67 #include "glsl/GrGLSLFragmentShaderBuilder.h" |
| 68 #include "glsl/GrGLSLProgramDataManager.h" | 68 #include "glsl/GrGLSLProgramDataManager.h" |
| 69 #include "glsl/GrGLSLUniformHandler.h" | 69 #include "glsl/GrGLSLUniformHandler.h" |
| 70 | 70 |
| 71 namespace { |
| 72 |
| 73 SkMatrix make_div_and_translate_matrix(GrTexture* texture, int x, int y) { |
| 74 SkMatrix matrix = GrCoordTransform::MakeDivByTextureWHMatrix(texture); |
| 75 matrix.preTranslate(SkIntToScalar(x), SkIntToScalar(y)); |
| 76 return matrix; |
| 77 } |
| 78 |
| 79 }; |
| 80 |
| 71 class AlphaThresholdEffect : public GrFragmentProcessor { | 81 class AlphaThresholdEffect : public GrFragmentProcessor { |
| 72 | 82 |
| 73 public: | 83 public: |
| 74 static GrFragmentProcessor* Create(GrTexture* texture, | 84 static GrFragmentProcessor* Create(GrTexture* texture, |
| 75 GrTexture* maskTexture, | 85 GrTexture* maskTexture, |
| 76 float innerThreshold, | 86 float innerThreshold, |
| 77 float outerThreshold) { | 87 float outerThreshold, |
| 78 return new AlphaThresholdEffect(texture, maskTexture, innerThreshold, ou
terThreshold); | 88 const SkIRect& bounds) { |
| 89 return new AlphaThresholdEffect(texture, maskTexture, innerThreshold, ou
terThreshold, |
| 90 bounds); |
| 79 } | 91 } |
| 80 | 92 |
| 81 virtual ~AlphaThresholdEffect() {}; | 93 virtual ~AlphaThresholdEffect() {}; |
| 82 | 94 |
| 83 const char* name() const override { return "Alpha Threshold"; } | 95 const char* name() const override { return "Alpha Threshold"; } |
| 84 | 96 |
| 85 float innerThreshold() const { return fInnerThreshold; } | 97 float innerThreshold() const { return fInnerThreshold; } |
| 86 float outerThreshold() const { return fOuterThreshold; } | 98 float outerThreshold() const { return fOuterThreshold; } |
| 87 | 99 |
| 88 private: | 100 private: |
| 89 AlphaThresholdEffect(GrTexture* texture, | 101 AlphaThresholdEffect(GrTexture* texture, |
| 90 GrTexture* maskTexture, | 102 GrTexture* maskTexture, |
| 91 float innerThreshold, | 103 float innerThreshold, |
| 92 float outerThreshold) | 104 float outerThreshold, |
| 105 const SkIRect& bounds) |
| 93 : fInnerThreshold(innerThreshold) | 106 : fInnerThreshold(innerThreshold) |
| 94 , fOuterThreshold(outerThreshold) | 107 , fOuterThreshold(outerThreshold) |
| 95 , fImageCoordTransform(kLocal_GrCoordSet, | 108 , fImageCoordTransform(kLocal_GrCoordSet, |
| 96 GrCoordTransform::MakeDivByTextureWHMatrix(textur
e), texture, | 109 GrCoordTransform::MakeDivByTextureWHMatrix(textur
e), texture, |
| 97 GrTextureParams::kNone_FilterMode) | 110 GrTextureParams::kNone_FilterMode) |
| 98 , fImageTextureAccess(texture) | 111 , fImageTextureAccess(texture) |
| 99 , fMaskCoordTransform(kLocal_GrCoordSet, | 112 , fMaskCoordTransform(kLocal_GrCoordSet, |
| 100 GrCoordTransform::MakeDivByTextureWHMatrix(maskTex
ture), maskTexture, | 113 make_div_and_translate_matrix(maskTexture, -bounds
.x(), -bounds.y()), |
| 114 maskTexture, |
| 101 GrTextureParams::kNone_FilterMode) | 115 GrTextureParams::kNone_FilterMode) |
| 102 , fMaskTextureAccess(maskTexture) { | 116 , fMaskTextureAccess(maskTexture) { |
| 103 this->initClassID<AlphaThresholdEffect>(); | 117 this->initClassID<AlphaThresholdEffect>(); |
| 104 this->addCoordTransform(&fImageCoordTransform); | 118 this->addCoordTransform(&fImageCoordTransform); |
| 105 this->addTextureAccess(&fImageTextureAccess); | 119 this->addTextureAccess(&fImageTextureAccess); |
| 106 this->addCoordTransform(&fMaskCoordTransform); | 120 this->addCoordTransform(&fMaskCoordTransform); |
| 107 this->addTextureAccess(&fMaskTextureAccess); | 121 this->addTextureAccess(&fMaskTextureAccess); |
| 108 } | 122 } |
| 109 | 123 |
| 110 GrGLSLFragmentProcessor* onCreateGLSLInstance() const override; | 124 GrGLSLFragmentProcessor* onCreateGLSLInstance() const override; |
| (...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 198 | 212 |
| 199 ///////////////////////////////////////////////////////////////////// | 213 ///////////////////////////////////////////////////////////////////// |
| 200 | 214 |
| 201 GR_DEFINE_FRAGMENT_PROCESSOR_TEST(AlphaThresholdEffect); | 215 GR_DEFINE_FRAGMENT_PROCESSOR_TEST(AlphaThresholdEffect); |
| 202 | 216 |
| 203 const GrFragmentProcessor* AlphaThresholdEffect::TestCreate(GrProcessorTestData*
d) { | 217 const GrFragmentProcessor* AlphaThresholdEffect::TestCreate(GrProcessorTestData*
d) { |
| 204 GrTexture* bmpTex = d->fTextures[GrProcessorUnitTest::kSkiaPMTextureIdx]; | 218 GrTexture* bmpTex = d->fTextures[GrProcessorUnitTest::kSkiaPMTextureIdx]; |
| 205 GrTexture* maskTex = d->fTextures[GrProcessorUnitTest::kAlphaTextureIdx]; | 219 GrTexture* maskTex = d->fTextures[GrProcessorUnitTest::kAlphaTextureIdx]; |
| 206 float innerThresh = d->fRandom->nextUScalar1(); | 220 float innerThresh = d->fRandom->nextUScalar1(); |
| 207 float outerThresh = d->fRandom->nextUScalar1(); | 221 float outerThresh = d->fRandom->nextUScalar1(); |
| 208 return AlphaThresholdEffect::Create(bmpTex, maskTex, innerThresh, outerThres
h); | 222 const int kMaxWidth = 1000; |
| 223 const int kMaxHeight = 1000; |
| 224 uint32_t width = d->fRandom->nextULessThan(kMaxWidth); |
| 225 uint32_t height = d->fRandom->nextULessThan(kMaxHeight); |
| 226 uint32_t x = d->fRandom->nextULessThan(kMaxWidth - width); |
| 227 uint32_t y = d->fRandom->nextULessThan(kMaxHeight - height); |
| 228 SkIRect bounds = SkIRect::MakeXYWH(x, y, width, height); |
| 229 return AlphaThresholdEffect::Create(bmpTex, maskTex, innerThresh, outerThres
h, bounds); |
| 209 } | 230 } |
| 210 | 231 |
| 211 /////////////////////////////////////////////////////////////////////////////// | 232 /////////////////////////////////////////////////////////////////////////////// |
| 212 | 233 |
| 213 void AlphaThresholdEffect::onGetGLSLProcessorKey(const GrGLSLCaps& caps, | 234 void AlphaThresholdEffect::onGetGLSLProcessorKey(const GrGLSLCaps& caps, |
| 214 GrProcessorKeyBuilder* b) const
{ | 235 GrProcessorKeyBuilder* b) const
{ |
| 215 GrGLAlphaThresholdEffect::GenKey(*this, caps, b); | 236 GrGLAlphaThresholdEffect::GenKey(*this, caps, b); |
| 216 } | 237 } |
| 217 | 238 |
| 218 GrGLSLFragmentProcessor* AlphaThresholdEffect::onCreateGLSLInstance() const { | 239 GrGLSLFragmentProcessor* AlphaThresholdEffect::onCreateGLSLInstance() const { |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 253 : INHERITED(1, &input) | 274 : INHERITED(1, &input) |
| 254 , fRegion(region) | 275 , fRegion(region) |
| 255 , fInnerThreshold(innerThreshold) | 276 , fInnerThreshold(innerThreshold) |
| 256 , fOuterThreshold(outerThreshold) { | 277 , fOuterThreshold(outerThreshold) { |
| 257 } | 278 } |
| 258 | 279 |
| 259 #if SK_SUPPORT_GPU | 280 #if SK_SUPPORT_GPU |
| 260 bool SkAlphaThresholdFilterImpl::asFragmentProcessor(GrFragmentProcessor** fp, | 281 bool SkAlphaThresholdFilterImpl::asFragmentProcessor(GrFragmentProcessor** fp, |
| 261 GrTexture* texture, | 282 GrTexture* texture, |
| 262 const SkMatrix& inMatrix, | 283 const SkMatrix& inMatrix, |
| 263 const SkIRect&) const { | 284 const SkIRect& bounds) cons
t { |
| 264 if (fp) { | 285 if (fp) { |
| 265 GrContext* context = texture->getContext(); | 286 GrContext* context = texture->getContext(); |
| 266 GrSurfaceDesc maskDesc; | 287 GrSurfaceDesc maskDesc; |
| 267 if (context->caps()->isConfigRenderable(kAlpha_8_GrPixelConfig, false))
{ | 288 if (context->caps()->isConfigRenderable(kAlpha_8_GrPixelConfig, false))
{ |
| 268 maskDesc.fConfig = kAlpha_8_GrPixelConfig; | 289 maskDesc.fConfig = kAlpha_8_GrPixelConfig; |
| 269 } else { | 290 } else { |
| 270 maskDesc.fConfig = kRGBA_8888_GrPixelConfig; | 291 maskDesc.fConfig = kRGBA_8888_GrPixelConfig; |
| 271 } | 292 } |
| 272 maskDesc.fFlags = kRenderTarget_GrSurfaceFlag; | 293 maskDesc.fFlags = kRenderTarget_GrSurfaceFlag; |
| 273 // Add one pixel of border to ensure that clamp mode will be all zeros | 294 // Add one pixel of border to ensure that clamp mode will be all zeros |
| 274 // the outside. | 295 // the outside. |
| 275 maskDesc.fWidth = texture->width(); | 296 maskDesc.fWidth = bounds.width(); |
| 276 maskDesc.fHeight = texture->height(); | 297 maskDesc.fHeight = bounds.height(); |
| 277 SkAutoTUnref<GrTexture> maskTexture( | 298 SkAutoTUnref<GrTexture> maskTexture( |
| 278 context->textureProvider()->createApproxTexture(maskDesc)); | 299 context->textureProvider()->createApproxTexture(maskDesc)); |
| 279 if (!maskTexture) { | 300 if (!maskTexture) { |
| 280 return false; | 301 return false; |
| 281 } | 302 } |
| 282 | 303 |
| 283 SkAutoTUnref<GrDrawContext> drawContext( | 304 SkAutoTUnref<GrDrawContext> drawContext( |
| 284 context->drawContext(maskTexture->as
RenderTarget())); | 305 context->drawContext(maskTexture->as
RenderTarget())); |
| 285 if (drawContext) { | 306 if (drawContext) { |
| 286 GrPaint grPaint; | 307 GrPaint grPaint; |
| 287 grPaint.setPorterDuffXPFactory(SkXfermode::kSrc_Mode); | 308 grPaint.setPorterDuffXPFactory(SkXfermode::kSrc_Mode); |
| 288 SkRegion::Iterator iter(fRegion); | 309 SkRegion::Iterator iter(fRegion); |
| 289 drawContext->clear(nullptr, 0x0, true); | 310 drawContext->clear(nullptr, 0x0, true); |
| 290 | 311 |
| 312 GrClip clip(SkRect::Make(SkIRect::MakeWH(bounds.width(), bounds.heig
ht()))); |
| 291 while (!iter.done()) { | 313 while (!iter.done()) { |
| 292 SkRect rect = SkRect::Make(iter.rect()); | 314 SkRect rect = SkRect::Make(iter.rect()); |
| 293 drawContext->drawRect(GrClip::WideOpen(), grPaint, inMatrix, rec
t); | 315 drawContext->drawRect(clip, grPaint, inMatrix, rect); |
| 294 iter.next(); | 316 iter.next(); |
| 295 } | 317 } |
| 296 } | 318 } |
| 297 | 319 |
| 298 *fp = AlphaThresholdEffect::Create(texture, | 320 *fp = AlphaThresholdEffect::Create(texture, |
| 299 maskTexture, | 321 maskTexture, |
| 300 fInnerThreshold, | 322 fInnerThreshold, |
| 301 fOuterThreshold); | 323 fOuterThreshold, |
| 324 bounds); |
| 302 } | 325 } |
| 303 return true; | 326 return true; |
| 304 } | 327 } |
| 305 #endif | 328 #endif |
| 306 | 329 |
| 307 void SkAlphaThresholdFilterImpl::flatten(SkWriteBuffer& buffer) const { | 330 void SkAlphaThresholdFilterImpl::flatten(SkWriteBuffer& buffer) const { |
| 308 this->INHERITED::flatten(buffer); | 331 this->INHERITED::flatten(buffer); |
| 309 buffer.writeScalar(fInnerThreshold); | 332 buffer.writeScalar(fInnerThreshold); |
| 310 buffer.writeScalar(fOuterThreshold); | 333 buffer.writeScalar(fOuterThreshold); |
| 311 buffer.writeRegion(fRegion); | 334 buffer.writeRegion(fRegion); |
| (...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 377 } | 400 } |
| 378 | 401 |
| 379 #ifndef SK_IGNORE_TO_STRING | 402 #ifndef SK_IGNORE_TO_STRING |
| 380 void SkAlphaThresholdFilterImpl::toString(SkString* str) const { | 403 void SkAlphaThresholdFilterImpl::toString(SkString* str) const { |
| 381 str->appendf("SkAlphaThresholdImageFilter: ("); | 404 str->appendf("SkAlphaThresholdImageFilter: ("); |
| 382 str->appendf("inner: %f outer: %f", fInnerThreshold, fOuterThreshold); | 405 str->appendf("inner: %f outer: %f", fInnerThreshold, fOuterThreshold); |
| 383 str->append(")"); | 406 str->append(")"); |
| 384 } | 407 } |
| 385 #endif | 408 #endif |
| 386 | 409 |
| OLD | NEW |