OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2012 The Android Open Source Project | 2 * Copyright 2012 The Android Open Source Project |
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 "SkMorphologyImageFilter.h" | 8 #include "SkMorphologyImageFilter.h" |
9 #include "SkBitmap.h" | 9 #include "SkBitmap.h" |
10 #include "SkColorPriv.h" | 10 #include "SkColorPriv.h" |
11 #include "SkDevice.h" | 11 #include "SkDevice.h" |
12 #include "SkOpts.h" | 12 #include "SkOpts.h" |
13 #include "SkReadBuffer.h" | 13 #include "SkReadBuffer.h" |
14 #include "SkRect.h" | 14 #include "SkRect.h" |
15 #include "SkWriteBuffer.h" | 15 #include "SkWriteBuffer.h" |
16 #if SK_SUPPORT_GPU | 16 #if SK_SUPPORT_GPU |
| 17 #include "SkGR.h" |
17 #include "GrContext.h" | 18 #include "GrContext.h" |
18 #include "GrDrawContext.h" | 19 #include "GrDrawContext.h" |
19 #include "GrInvariantOutput.h" | 20 #include "GrInvariantOutput.h" |
20 #include "GrTexture.h" | 21 #include "GrTexture.h" |
21 #include "effects/Gr1DKernelEffect.h" | 22 #include "effects/Gr1DKernelEffect.h" |
22 #include "gl/GrGLFragmentProcessor.h" | 23 #include "gl/GrGLFragmentProcessor.h" |
23 #include "glsl/GrGLSLFragmentShaderBuilder.h" | 24 #include "glsl/GrGLSLFragmentShaderBuilder.h" |
24 #include "glsl/GrGLSLProgramBuilder.h" | 25 #include "glsl/GrGLSLProgramBuilder.h" |
25 #include "glsl/GrGLSLProgramDataManager.h" | 26 #include "glsl/GrGLSLProgramDataManager.h" |
26 #endif | 27 #endif |
(...skipping 177 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
204 */ | 205 */ |
205 class GrMorphologyEffect : public Gr1DKernelEffect { | 206 class GrMorphologyEffect : public Gr1DKernelEffect { |
206 | 207 |
207 public: | 208 public: |
208 | 209 |
209 enum MorphologyType { | 210 enum MorphologyType { |
210 kErode_MorphologyType, | 211 kErode_MorphologyType, |
211 kDilate_MorphologyType, | 212 kDilate_MorphologyType, |
212 }; | 213 }; |
213 | 214 |
214 static GrFragmentProcessor* Create(GrTexture* tex, Direction dir, int radius
, | 215 static GrFragmentProcessor* Create(GrTexture* tex, |
215 MorphologyType type) { | 216 Direction dir, int radius, MorphologyType
type, GrRenderTarget* dst) { |
216 return new GrMorphologyEffect(tex, dir, radius, type); | 217 return new GrMorphologyEffect(tex, dir, radius, type, dst); |
217 } | 218 } |
218 | 219 |
219 static GrFragmentProcessor* Create(GrTexture* tex, Direction dir, int radius
, | 220 static GrFragmentProcessor* Create(GrTexture* tex, |
220 MorphologyType type, float bounds[2]) { | 221 Direction dir, int radius, MorphologyType
type, |
221 return new GrMorphologyEffect(tex, dir, radius, type, bounds); | 222 float bounds[2], GrRenderTarget* dst) { |
| 223 return new GrMorphologyEffect(tex, dir, radius, type, bounds, dst); |
222 } | 224 } |
223 | 225 |
224 virtual ~GrMorphologyEffect(); | 226 virtual ~GrMorphologyEffect(); |
225 | 227 |
226 MorphologyType type() const { return fType; } | 228 MorphologyType type() const { return fType; } |
227 bool useRange() const { return fUseRange; } | 229 bool useRange() const { return fUseRange; } |
228 const float* range() const { return fRange; } | 230 const float* range() const { return fRange; } |
229 | 231 |
230 const char* name() const override { return "Morphology"; } | 232 const char* name() const override { return "Morphology"; } |
231 | 233 |
232 protected: | 234 protected: |
233 | 235 |
234 MorphologyType fType; | 236 MorphologyType fType; |
235 bool fUseRange; | 237 bool fUseRange; |
236 float fRange[2]; | 238 float fRange[2]; |
237 | 239 |
238 private: | 240 private: |
239 GrGLFragmentProcessor* onCreateGLInstance() const override; | 241 GrGLFragmentProcessor* onCreateGLInstance() const override; |
240 | 242 |
241 void onGetGLProcessorKey(const GrGLSLCaps&, GrProcessorKeyBuilder*) const ov
erride; | 243 void onGetGLProcessorKey(const GrGLSLCaps&, GrProcessorKeyBuilder*) const ov
erride; |
242 | 244 |
243 bool onIsEqual(const GrFragmentProcessor&) const override; | 245 bool onIsEqual(const GrFragmentProcessor&) const override; |
244 | 246 |
245 void onComputeInvariantOutput(GrInvariantOutput* inout) const override; | 247 void onComputeInvariantOutput(GrInvariantOutput* inout) const override; |
246 | 248 |
247 GrMorphologyEffect(GrTexture*, Direction, int radius, MorphologyType); | 249 GrMorphologyEffect(GrTexture*, Direction, int radius, MorphologyType, GrRend
erTarget* dst); |
248 GrMorphologyEffect(GrTexture*, Direction, int radius, MorphologyType, | 250 GrMorphologyEffect(GrTexture*, Direction, int radius, MorphologyType, |
249 float bounds[2]); | 251 float bounds[2], GrRenderTarget* dst); |
250 | 252 |
251 GR_DECLARE_FRAGMENT_PROCESSOR_TEST; | 253 GR_DECLARE_FRAGMENT_PROCESSOR_TEST; |
252 | 254 |
253 typedef Gr1DKernelEffect INHERITED; | 255 typedef Gr1DKernelEffect INHERITED; |
254 }; | 256 }; |
255 | 257 |
256 /////////////////////////////////////////////////////////////////////////////// | 258 /////////////////////////////////////////////////////////////////////////////// |
257 | 259 |
258 class GrGLMorphologyEffect : public GrGLFragmentProcessor { | 260 class GrGLMorphologyEffect : public GrGLFragmentProcessor { |
259 public: | 261 public: |
(...skipping 135 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
395 pdman.set2f(fRangeUni, range[0], range[1]); | 397 pdman.set2f(fRangeUni, range[0], range[1]); |
396 } | 398 } |
397 } | 399 } |
398 } | 400 } |
399 | 401 |
400 /////////////////////////////////////////////////////////////////////////////// | 402 /////////////////////////////////////////////////////////////////////////////// |
401 | 403 |
402 GrMorphologyEffect::GrMorphologyEffect(GrTexture* texture, | 404 GrMorphologyEffect::GrMorphologyEffect(GrTexture* texture, |
403 Direction direction, | 405 Direction direction, |
404 int radius, | 406 int radius, |
405 MorphologyType type) | 407 MorphologyType type, GrRenderTarget* dst) |
406 : INHERITED(texture, direction, radius) | 408 : INHERITED(texture, direction, radius, dst) |
407 , fType(type), fUseRange(false) { | 409 , fType(type), fUseRange(false) { |
408 this->initClassID<GrMorphologyEffect>(); | 410 this->initClassID<GrMorphologyEffect>(); |
409 } | 411 } |
410 | 412 |
411 GrMorphologyEffect::GrMorphologyEffect(GrTexture* texture, | 413 GrMorphologyEffect::GrMorphologyEffect(GrTexture* texture, |
412 Direction direction, | 414 Direction direction, |
413 int radius, | 415 int radius, |
414 MorphologyType type, | 416 MorphologyType type, |
415 float range[2]) | 417 float range[2], GrRenderTarget* dst) |
416 : INHERITED(texture, direction, radius) | 418 : INHERITED(texture, direction, radius, dst) |
417 , fType(type), fUseRange(true) { | 419 , fType(type), fUseRange(true) { |
418 this->initClassID<GrMorphologyEffect>(); | 420 this->initClassID<GrMorphologyEffect>(); |
419 fRange[0] = range[0]; | 421 fRange[0] = range[0]; |
420 fRange[1] = range[1]; | 422 fRange[1] = range[1]; |
421 } | 423 } |
422 | 424 |
423 GrMorphologyEffect::~GrMorphologyEffect() { | 425 GrMorphologyEffect::~GrMorphologyEffect() { |
424 } | 426 } |
425 | 427 |
426 void GrMorphologyEffect::onGetGLProcessorKey(const GrGLSLCaps& caps, GrProcessor
KeyBuilder* b) const { | 428 void GrMorphologyEffect::onGetGLProcessorKey(const GrGLSLCaps& caps, GrProcessor
KeyBuilder* b) const { |
(...skipping 23 matching lines...) Expand all Loading... |
450 | 452 |
451 const GrFragmentProcessor* GrMorphologyEffect::TestCreate(GrProcessorTestData* d
) { | 453 const GrFragmentProcessor* GrMorphologyEffect::TestCreate(GrProcessorTestData* d
) { |
452 int texIdx = d->fRandom->nextBool() ? GrProcessorUnitTest::kSkiaPMTextureIdx
: | 454 int texIdx = d->fRandom->nextBool() ? GrProcessorUnitTest::kSkiaPMTextureIdx
: |
453 GrProcessorUnitTest::kAlphaTextureIdx; | 455 GrProcessorUnitTest::kAlphaTextureIdx; |
454 Direction dir = d->fRandom->nextBool() ? kX_Direction : kY_Direction; | 456 Direction dir = d->fRandom->nextBool() ? kX_Direction : kY_Direction; |
455 static const int kMaxRadius = 10; | 457 static const int kMaxRadius = 10; |
456 int radius = d->fRandom->nextRangeU(1, kMaxRadius); | 458 int radius = d->fRandom->nextRangeU(1, kMaxRadius); |
457 MorphologyType type = d->fRandom->nextBool() ? GrMorphologyEffect::kErode_Mo
rphologyType : | 459 MorphologyType type = d->fRandom->nextBool() ? GrMorphologyEffect::kErode_Mo
rphologyType : |
458 GrMorphologyEffect::kDilate_Morph
ologyType; | 460 GrMorphologyEffect::kDilate_Morph
ologyType; |
459 | 461 |
460 return GrMorphologyEffect::Create(d->fTextures[texIdx], dir, radius, type); | 462 return GrMorphologyEffect::Create(d->fTextures[texIdx], dir, radius, type, N
ULL); |
461 } | 463 } |
462 | 464 |
463 namespace { | 465 namespace { |
464 | 466 |
465 | 467 |
466 void apply_morphology_rect(GrDrawContext* drawContext, | 468 void apply_morphology_rect(GrDrawContext* drawContext, |
467 const GrClip& clip, | 469 const GrClip& clip, |
468 GrTexture* texture, | 470 GrTexture* texture, |
469 const SkIRect& srcRect, | 471 const SkIRect& srcRect, |
470 const SkIRect& dstRect, | 472 const SkIRect& dstRect, |
471 int radius, | 473 int radius, |
472 GrMorphologyEffect::MorphologyType morphType, | 474 GrMorphologyEffect::MorphologyType morphType, |
473 float bounds[2], | 475 float bounds[2], |
474 Gr1DKernelEffect::Direction direction) { | 476 Gr1DKernelEffect::Direction direction) { |
475 GrPaint paint; | 477 GrPaint paint; |
476 paint.addColorFragmentProcessor(GrMorphologyEffect::Create(texture, | 478 paint.addColorFragmentProcessor(GrMorphologyEffect::Create(texture, |
477 direction, | 479 direction, |
478 radius, | 480 radius, |
479 morphType, | 481 morphType, |
480 bounds))->unref()
; | 482 bounds, drawConte
xt->rt_remove_me()))->unref(); |
| 483 |
481 drawContext->fillRectToRect(clip, paint, SkMatrix::I(), SkRect::Make(dstRect
), | 484 drawContext->fillRectToRect(clip, paint, SkMatrix::I(), SkRect::Make(dstRect
), |
482 SkRect::Make(srcRect)); | 485 SkRect::Make(srcRect)); |
483 } | 486 } |
484 | 487 |
485 void apply_morphology_rect_no_bounds(GrDrawContext* drawContext, | 488 void apply_morphology_rect_no_bounds(GrDrawContext* drawContext, |
486 const GrClip& clip, | 489 const GrClip& clip, |
487 GrTexture* texture, | 490 GrTexture* texture, |
488 const SkIRect& srcRect, | 491 const SkIRect& srcRect, |
489 const SkIRect& dstRect, | 492 const SkIRect& dstRect, |
490 int radius, | 493 int radius, |
491 GrMorphologyEffect::MorphologyType morphTyp
e, | 494 GrMorphologyEffect::MorphologyType morphTyp
e, |
492 Gr1DKernelEffect::Direction direction) { | 495 Gr1DKernelEffect::Direction direction) { |
493 GrPaint paint; | 496 GrPaint paint; |
494 paint.addColorFragmentProcessor(GrMorphologyEffect::Create(texture, | 497 paint.addColorFragmentProcessor(GrMorphologyEffect::Create(texture, |
495 direction, | 498 direction, |
496 radius, | 499 radius, |
497 morphType))->unre
f(); | 500 morphType, drawCo
ntext->rt_remove_me()))->unref(); |
| 501 |
498 drawContext->fillRectToRect(clip, paint, SkMatrix::I(), SkRect::Make(dstRect
), | 502 drawContext->fillRectToRect(clip, paint, SkMatrix::I(), SkRect::Make(dstRect
), |
499 SkRect::Make(srcRect)); | 503 SkRect::Make(srcRect)); |
500 } | 504 } |
501 | 505 |
502 void apply_morphology_pass(GrDrawContext* drawContext, | 506 void apply_morphology_pass(GrDrawContext* drawContext, |
503 const GrClip& clip, | 507 const GrClip& clip, |
504 GrTexture* texture, | 508 GrTexture* texture, |
505 const SkIRect& srcRect, | 509 const SkIRect& srcRect, |
506 const SkIRect& dstRect, | 510 const SkIRect& dstRect, |
507 int radius, | 511 int radius, |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
572 if (radius.fHeight > 0) { | 576 if (radius.fHeight > 0) { |
573 // Optimization: we will fall through and allocate the "real" textur
e after this one | 577 // Optimization: we will fall through and allocate the "real" textur
e after this one |
574 // so ours can be approximate (likely faster to allocate) | 578 // so ours can be approximate (likely faster to allocate) |
575 horiConstraint = GrTextureProvider::kApprox_SizeConstraint; | 579 horiConstraint = GrTextureProvider::kApprox_SizeConstraint; |
576 } | 580 } |
577 | 581 |
578 GrTexture* scratch = context->textureProvider()->createTexture(desc, hor
iConstraint); | 582 GrTexture* scratch = context->textureProvider()->createTexture(desc, hor
iConstraint); |
579 if (nullptr == scratch) { | 583 if (nullptr == scratch) { |
580 return false; | 584 return false; |
581 } | 585 } |
| 586 |
582 SkAutoTUnref<GrDrawContext> dstDrawContext( | 587 SkAutoTUnref<GrDrawContext> dstDrawContext( |
583 context->drawContext(scratch->as
RenderTarget())); | 588 context->drawContext(scratch->as
RenderTarget())); |
584 if (!dstDrawContext) { | 589 if (!dstDrawContext) { |
585 return false; | 590 return false; |
586 } | 591 } |
587 | 592 |
588 apply_morphology_pass(dstDrawContext, clip, srcTexture, | 593 apply_morphology_pass(dstDrawContext, clip, srcTexture, |
589 srcRect, dstRect, radius.fWidth, morphType, | 594 srcRect, dstRect, radius.fWidth, morphType, |
590 Gr1DKernelEffect::kX_Direction); | 595 Gr1DKernelEffect::kX_Direction); |
591 SkIRect clearRect = SkIRect::MakeXYWH(dstRect.fLeft, dstRect.fBottom, | 596 SkIRect clearRect = SkIRect::MakeXYWH(dstRect.fLeft, dstRect.fBottom, |
592 dstRect.width(), radius.fHeight); | 597 dstRect.width(), radius.fHeight); |
593 GrColor clearColor = GrMorphologyEffect::kErode_MorphologyType == morphT
ype ? | 598 GrColor clearColor = GrMorphologyEffect::kErode_MorphologyType == morphT
ype ? |
594 SK_ColorWHITE : | 599 SK_ColorWHITE : |
595 SK_ColorTRANSPARENT; | 600 SK_ColorTRANSPARENT; |
| 601 |
596 dstDrawContext->clear(&clearRect, clearColor, false); | 602 dstDrawContext->clear(&clearRect, clearColor, false); |
597 | 603 |
598 srcTexture.reset(scratch); | 604 srcTexture.reset(scratch); |
599 srcRect = dstRect; | 605 srcRect = dstRect; |
600 } | 606 } |
601 if (radius.fHeight > 0) { | 607 if (radius.fHeight > 0) { |
602 GrTexture* scratch = context->textureProvider()->createTexture(desc, con
straint); | 608 GrTexture* scratch = context->textureProvider()->createTexture(desc, con
straint); |
603 if (nullptr == scratch) { | 609 if (nullptr == scratch) { |
604 return false; | 610 return false; |
605 } | 611 } |
| 612 |
606 SkAutoTUnref<GrDrawContext> dstDrawContext( | 613 SkAutoTUnref<GrDrawContext> dstDrawContext( |
607 context->drawContext(scratch->as
RenderTarget())); | 614 context->drawContext(scratch->as
RenderTarget())); |
608 if (!dstDrawContext) { | 615 if (!dstDrawContext) { |
609 return false; | 616 return false; |
610 } | 617 } |
611 | 618 |
612 apply_morphology_pass(dstDrawContext, clip, srcTexture, | 619 apply_morphology_pass(dstDrawContext, clip, srcTexture, |
613 srcRect, dstRect, radius.fHeight, morphType, | 620 srcRect, dstRect, radius.fHeight, morphType, |
614 Gr1DKernelEffect::kY_Direction); | 621 Gr1DKernelEffect::kY_Direction); |
615 | 622 |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
670 SkBitmap* result, SkIPoint* offset) con
st { | 677 SkBitmap* result, SkIPoint* offset) con
st { |
671 return this->filterImageGPUGeneric(true, proxy, src, ctx, result, offset); | 678 return this->filterImageGPUGeneric(true, proxy, src, ctx, result, offset); |
672 } | 679 } |
673 | 680 |
674 bool SkErodeImageFilter::filterImageGPU(Proxy* proxy, const SkBitmap& src, const
Context& ctx, | 681 bool SkErodeImageFilter::filterImageGPU(Proxy* proxy, const SkBitmap& src, const
Context& ctx, |
675 SkBitmap* result, SkIPoint* offset) cons
t { | 682 SkBitmap* result, SkIPoint* offset) cons
t { |
676 return this->filterImageGPUGeneric(false, proxy, src, ctx, result, offset); | 683 return this->filterImageGPUGeneric(false, proxy, src, ctx, result, offset); |
677 } | 684 } |
678 | 685 |
679 #endif | 686 #endif |
OLD | NEW |